gcc/ChangeLog
[official-gcc.git] / gcc / tree.c
blob6c71025b6b4845717430ad9bcedf645090ae0914
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 /* DECL_DEBUG_EXPR is copied explicitely by callers. */
979 if (TREE_CODE (node) == VAR_DECL)
980 DECL_HAS_DEBUG_EXPR_P (t) = 0;
981 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
983 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
984 DECL_HAS_INIT_PRIORITY_P (t) = 1;
986 if (TREE_CODE (node) == FUNCTION_DECL)
987 DECL_STRUCT_FUNCTION (t) = NULL;
989 else if (TREE_CODE_CLASS (code) == tcc_type)
991 TYPE_UID (t) = next_type_uid++;
992 /* The following is so that the debug code for
993 the copy is different from the original type.
994 The two statements usually duplicate each other
995 (because they clear fields of the same union),
996 but the optimizer should catch that. */
997 TYPE_SYMTAB_POINTER (t) = 0;
998 TYPE_SYMTAB_ADDRESS (t) = 0;
1000 /* Do not copy the values cache. */
1001 if (TYPE_CACHED_VALUES_P(t))
1003 TYPE_CACHED_VALUES_P (t) = 0;
1004 TYPE_CACHED_VALUES (t) = NULL_TREE;
1008 return t;
1011 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1012 For example, this can copy a list made of TREE_LIST nodes. */
1014 tree
1015 copy_list (tree list)
1017 tree head;
1018 tree prev, next;
1020 if (list == 0)
1021 return 0;
1023 head = prev = copy_node (list);
1024 next = TREE_CHAIN (list);
1025 while (next)
1027 TREE_CHAIN (prev) = copy_node (next);
1028 prev = TREE_CHAIN (prev);
1029 next = TREE_CHAIN (next);
1031 return head;
1035 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1037 tree
1038 build_int_cst (tree type, HOST_WIDE_INT low)
1040 /* Support legacy code. */
1041 if (!type)
1042 type = integer_type_node;
1044 return double_int_to_tree (type, double_int::from_shwi (low));
1047 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1049 tree
1050 build_int_cst_type (tree type, HOST_WIDE_INT low)
1052 gcc_assert (type);
1054 return double_int_to_tree (type, double_int::from_shwi (low));
1057 /* Constructs tree in type TYPE from with value given by CST. Signedness
1058 of CST is assumed to be the same as the signedness of TYPE. */
1060 tree
1061 double_int_to_tree (tree type, double_int cst)
1063 bool sign_extended_type = !TYPE_UNSIGNED (type);
1065 cst = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1067 return build_int_cst_wide (type, cst.low, cst.high);
1070 /* Returns true if CST fits into range of TYPE. Signedness of CST is assumed
1071 to be the same as the signedness of TYPE. */
1073 bool
1074 double_int_fits_to_tree_p (const_tree type, double_int cst)
1076 bool sign_extended_type = !TYPE_UNSIGNED (type);
1078 double_int ext
1079 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1081 return cst == ext;
1084 /* We force the double_int CST to the range of the type TYPE by sign or
1085 zero extending it. OVERFLOWABLE indicates if we are interested in
1086 overflow of the value, when >0 we are only interested in signed
1087 overflow, for <0 we are interested in any overflow. OVERFLOWED
1088 indicates whether overflow has already occurred. CONST_OVERFLOWED
1089 indicates whether constant overflow has already occurred. We force
1090 T's value to be within range of T's type (by setting to 0 or 1 all
1091 the bits outside the type's range). We set TREE_OVERFLOWED if,
1092 OVERFLOWED is nonzero,
1093 or OVERFLOWABLE is >0 and signed overflow occurs
1094 or OVERFLOWABLE is <0 and any overflow occurs
1095 We return a new tree node for the extended double_int. The node
1096 is shared if no overflow flags are set. */
1099 tree
1100 force_fit_type_double (tree type, double_int cst, int overflowable,
1101 bool overflowed)
1103 bool sign_extended_type = !TYPE_UNSIGNED (type);
1105 /* If we need to set overflow flags, return a new unshared node. */
1106 if (overflowed || !double_int_fits_to_tree_p(type, cst))
1108 if (overflowed
1109 || overflowable < 0
1110 || (overflowable > 0 && sign_extended_type))
1112 tree t = make_node (INTEGER_CST);
1113 TREE_INT_CST (t)
1114 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1115 TREE_TYPE (t) = type;
1116 TREE_OVERFLOW (t) = 1;
1117 return t;
1121 /* Else build a shared node. */
1122 return double_int_to_tree (type, cst);
1125 /* These are the hash table functions for the hash table of INTEGER_CST
1126 nodes of a sizetype. */
1128 /* Return the hash code code X, an INTEGER_CST. */
1130 static hashval_t
1131 int_cst_hash_hash (const void *x)
1133 const_tree const t = (const_tree) x;
1135 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1136 ^ htab_hash_pointer (TREE_TYPE (t)));
1139 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1140 is the same as that given by *Y, which is the same. */
1142 static int
1143 int_cst_hash_eq (const void *x, const void *y)
1145 const_tree const xt = (const_tree) x;
1146 const_tree const yt = (const_tree) y;
1148 return (TREE_TYPE (xt) == TREE_TYPE (yt)
1149 && TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1150 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt));
1153 /* Create an INT_CST node of TYPE and value HI:LOW.
1154 The returned node is always shared. For small integers we use a
1155 per-type vector cache, for larger ones we use a single hash table. */
1157 tree
1158 build_int_cst_wide (tree type, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
1160 tree t;
1161 int ix = -1;
1162 int limit = 0;
1164 gcc_assert (type);
1166 switch (TREE_CODE (type))
1168 case NULLPTR_TYPE:
1169 gcc_assert (hi == 0 && low == 0);
1170 /* Fallthru. */
1172 case POINTER_TYPE:
1173 case REFERENCE_TYPE:
1174 /* Cache NULL pointer. */
1175 if (!hi && !low)
1177 limit = 1;
1178 ix = 0;
1180 break;
1182 case BOOLEAN_TYPE:
1183 /* Cache false or true. */
1184 limit = 2;
1185 if (!hi && low < 2)
1186 ix = low;
1187 break;
1189 case INTEGER_TYPE:
1190 case OFFSET_TYPE:
1191 if (TYPE_UNSIGNED (type))
1193 /* Cache 0..N */
1194 limit = INTEGER_SHARE_LIMIT;
1195 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1196 ix = low;
1198 else
1200 /* Cache -1..N */
1201 limit = INTEGER_SHARE_LIMIT + 1;
1202 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1203 ix = low + 1;
1204 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
1205 ix = 0;
1207 break;
1209 case ENUMERAL_TYPE:
1210 break;
1212 default:
1213 gcc_unreachable ();
1216 if (ix >= 0)
1218 /* Look for it in the type's vector of small shared ints. */
1219 if (!TYPE_CACHED_VALUES_P (type))
1221 TYPE_CACHED_VALUES_P (type) = 1;
1222 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1225 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1226 if (t)
1228 /* Make sure no one is clobbering the shared constant. */
1229 gcc_assert (TREE_TYPE (t) == type);
1230 gcc_assert (TREE_INT_CST_LOW (t) == low);
1231 gcc_assert (TREE_INT_CST_HIGH (t) == hi);
1233 else
1235 /* Create a new shared int. */
1236 t = make_node (INTEGER_CST);
1238 TREE_INT_CST_LOW (t) = low;
1239 TREE_INT_CST_HIGH (t) = hi;
1240 TREE_TYPE (t) = type;
1242 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1245 else
1247 /* Use the cache of larger shared ints. */
1248 void **slot;
1250 TREE_INT_CST_LOW (int_cst_node) = low;
1251 TREE_INT_CST_HIGH (int_cst_node) = hi;
1252 TREE_TYPE (int_cst_node) = type;
1254 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
1255 t = (tree) *slot;
1256 if (!t)
1258 /* Insert this one into the hash table. */
1259 t = int_cst_node;
1260 *slot = t;
1261 /* Make a new node for next time round. */
1262 int_cst_node = make_node (INTEGER_CST);
1266 return t;
1269 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1270 and the rest are zeros. */
1272 tree
1273 build_low_bits_mask (tree type, unsigned bits)
1275 double_int mask;
1277 gcc_assert (bits <= TYPE_PRECISION (type));
1279 if (bits == TYPE_PRECISION (type)
1280 && !TYPE_UNSIGNED (type))
1281 /* Sign extended all-ones mask. */
1282 mask = double_int_minus_one;
1283 else
1284 mask = double_int::mask (bits);
1286 return build_int_cst_wide (type, mask.low, mask.high);
1289 /* Checks that X is integer constant that can be expressed in (unsigned)
1290 HOST_WIDE_INT without loss of precision. */
1292 bool
1293 cst_and_fits_in_hwi (const_tree x)
1295 if (TREE_CODE (x) != INTEGER_CST)
1296 return false;
1298 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1299 return false;
1301 return (TREE_INT_CST_HIGH (x) == 0
1302 || TREE_INT_CST_HIGH (x) == -1);
1305 /* Build a newly constructed TREE_VEC node of length LEN. */
1307 tree
1308 make_vector_stat (unsigned len MEM_STAT_DECL)
1310 tree t;
1311 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1313 record_node_allocation_statistics (VECTOR_CST, length);
1315 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1317 TREE_SET_CODE (t, VECTOR_CST);
1318 TREE_CONSTANT (t) = 1;
1320 return t;
1323 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1324 are in a list pointed to by VALS. */
1326 tree
1327 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1329 int over = 0;
1330 unsigned cnt = 0;
1331 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1332 TREE_TYPE (v) = type;
1334 /* Iterate through elements and check for overflow. */
1335 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1337 tree value = vals[cnt];
1339 VECTOR_CST_ELT (v, cnt) = value;
1341 /* Don't crash if we get an address constant. */
1342 if (!CONSTANT_CLASS_P (value))
1343 continue;
1345 over |= TREE_OVERFLOW (value);
1348 TREE_OVERFLOW (v) = over;
1349 return v;
1352 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1353 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1355 tree
1356 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1358 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1359 unsigned HOST_WIDE_INT idx;
1360 tree value;
1362 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1363 vec[idx] = value;
1364 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1365 vec[idx] = build_zero_cst (TREE_TYPE (type));
1367 return build_vector (type, vec);
1370 /* Build a vector of type VECTYPE where all the elements are SCs. */
1371 tree
1372 build_vector_from_val (tree vectype, tree sc)
1374 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1376 if (sc == error_mark_node)
1377 return sc;
1379 /* Verify that the vector type is suitable for SC. Note that there
1380 is some inconsistency in the type-system with respect to restrict
1381 qualifications of pointers. Vector types always have a main-variant
1382 element type and the qualification is applied to the vector-type.
1383 So TREE_TYPE (vector-type) does not return a properly qualified
1384 vector element-type. */
1385 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1386 TREE_TYPE (vectype)));
1388 if (CONSTANT_CLASS_P (sc))
1390 tree *v = XALLOCAVEC (tree, nunits);
1391 for (i = 0; i < nunits; ++i)
1392 v[i] = sc;
1393 return build_vector (vectype, v);
1395 else
1397 vec<constructor_elt, va_gc> *v;
1398 vec_alloc (v, nunits);
1399 for (i = 0; i < nunits; ++i)
1400 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1401 return build_constructor (vectype, v);
1405 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1406 are in the vec pointed to by VALS. */
1407 tree
1408 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1410 tree c = make_node (CONSTRUCTOR);
1411 unsigned int i;
1412 constructor_elt *elt;
1413 bool constant_p = true;
1414 bool side_effects_p = false;
1416 TREE_TYPE (c) = type;
1417 CONSTRUCTOR_ELTS (c) = vals;
1419 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1421 /* Mostly ctors will have elts that don't have side-effects, so
1422 the usual case is to scan all the elements. Hence a single
1423 loop for both const and side effects, rather than one loop
1424 each (with early outs). */
1425 if (!TREE_CONSTANT (elt->value))
1426 constant_p = false;
1427 if (TREE_SIDE_EFFECTS (elt->value))
1428 side_effects_p = true;
1431 TREE_SIDE_EFFECTS (c) = side_effects_p;
1432 TREE_CONSTANT (c) = constant_p;
1434 return c;
1437 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1438 INDEX and VALUE. */
1439 tree
1440 build_constructor_single (tree type, tree index, tree value)
1442 vec<constructor_elt, va_gc> *v;
1443 constructor_elt elt = {index, value};
1445 vec_alloc (v, 1);
1446 v->quick_push (elt);
1448 return build_constructor (type, v);
1452 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1453 are in a list pointed to by VALS. */
1454 tree
1455 build_constructor_from_list (tree type, tree vals)
1457 tree t;
1458 vec<constructor_elt, va_gc> *v = NULL;
1460 if (vals)
1462 vec_alloc (v, list_length (vals));
1463 for (t = vals; t; t = TREE_CHAIN (t))
1464 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1467 return build_constructor (type, v);
1470 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
1471 of elements, provided as index/value pairs. */
1473 tree
1474 build_constructor_va (tree type, int nelts, ...)
1476 vec<constructor_elt, va_gc> *v = NULL;
1477 va_list p;
1479 va_start (p, nelts);
1480 vec_alloc (v, nelts);
1481 while (nelts--)
1483 tree index = va_arg (p, tree);
1484 tree value = va_arg (p, tree);
1485 CONSTRUCTOR_APPEND_ELT (v, index, value);
1487 va_end (p);
1488 return build_constructor (type, v);
1491 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1493 tree
1494 build_fixed (tree type, FIXED_VALUE_TYPE f)
1496 tree v;
1497 FIXED_VALUE_TYPE *fp;
1499 v = make_node (FIXED_CST);
1500 fp = ggc_alloc_fixed_value ();
1501 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1503 TREE_TYPE (v) = type;
1504 TREE_FIXED_CST_PTR (v) = fp;
1505 return v;
1508 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1510 tree
1511 build_real (tree type, REAL_VALUE_TYPE d)
1513 tree v;
1514 REAL_VALUE_TYPE *dp;
1515 int overflow = 0;
1517 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1518 Consider doing it via real_convert now. */
1520 v = make_node (REAL_CST);
1521 dp = ggc_alloc_real_value ();
1522 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1524 TREE_TYPE (v) = type;
1525 TREE_REAL_CST_PTR (v) = dp;
1526 TREE_OVERFLOW (v) = overflow;
1527 return v;
1530 /* Return a new REAL_CST node whose type is TYPE
1531 and whose value is the integer value of the INTEGER_CST node I. */
1533 REAL_VALUE_TYPE
1534 real_value_from_int_cst (const_tree type, const_tree i)
1536 REAL_VALUE_TYPE d;
1538 /* Clear all bits of the real value type so that we can later do
1539 bitwise comparisons to see if two values are the same. */
1540 memset (&d, 0, sizeof d);
1542 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
1543 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
1544 TYPE_UNSIGNED (TREE_TYPE (i)));
1545 return d;
1548 /* Given a tree representing an integer constant I, return a tree
1549 representing the same value as a floating-point constant of type TYPE. */
1551 tree
1552 build_real_from_int_cst (tree type, const_tree i)
1554 tree v;
1555 int overflow = TREE_OVERFLOW (i);
1557 v = build_real (type, real_value_from_int_cst (type, i));
1559 TREE_OVERFLOW (v) |= overflow;
1560 return v;
1563 /* Return a newly constructed STRING_CST node whose value is
1564 the LEN characters at STR.
1565 Note that for a C string literal, LEN should include the trailing NUL.
1566 The TREE_TYPE is not initialized. */
1568 tree
1569 build_string (int len, const char *str)
1571 tree s;
1572 size_t length;
1574 /* Do not waste bytes provided by padding of struct tree_string. */
1575 length = len + offsetof (struct tree_string, str) + 1;
1577 record_node_allocation_statistics (STRING_CST, length);
1579 s = ggc_alloc_tree_node (length);
1581 memset (s, 0, sizeof (struct tree_typed));
1582 TREE_SET_CODE (s, STRING_CST);
1583 TREE_CONSTANT (s) = 1;
1584 TREE_STRING_LENGTH (s) = len;
1585 memcpy (s->string.str, str, len);
1586 s->string.str[len] = '\0';
1588 return s;
1591 /* Return a newly constructed COMPLEX_CST node whose value is
1592 specified by the real and imaginary parts REAL and IMAG.
1593 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1594 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1596 tree
1597 build_complex (tree type, tree real, tree imag)
1599 tree t = make_node (COMPLEX_CST);
1601 TREE_REALPART (t) = real;
1602 TREE_IMAGPART (t) = imag;
1603 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1604 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1605 return t;
1608 /* Return a constant of arithmetic type TYPE which is the
1609 multiplicative identity of the set TYPE. */
1611 tree
1612 build_one_cst (tree type)
1614 switch (TREE_CODE (type))
1616 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1617 case POINTER_TYPE: case REFERENCE_TYPE:
1618 case OFFSET_TYPE:
1619 return build_int_cst (type, 1);
1621 case REAL_TYPE:
1622 return build_real (type, dconst1);
1624 case FIXED_POINT_TYPE:
1625 /* We can only generate 1 for accum types. */
1626 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1627 return build_fixed (type, FCONST1(TYPE_MODE (type)));
1629 case VECTOR_TYPE:
1631 tree scalar = build_one_cst (TREE_TYPE (type));
1633 return build_vector_from_val (type, scalar);
1636 case COMPLEX_TYPE:
1637 return build_complex (type,
1638 build_one_cst (TREE_TYPE (type)),
1639 build_zero_cst (TREE_TYPE (type)));
1641 default:
1642 gcc_unreachable ();
1646 /* Return a constant of arithmetic type TYPE which is the
1647 opposite of the multiplicative identity of the set TYPE. */
1649 tree
1650 build_minus_one_cst (tree type)
1652 switch (TREE_CODE (type))
1654 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1655 case POINTER_TYPE: case REFERENCE_TYPE:
1656 case OFFSET_TYPE:
1657 return build_int_cst (type, -1);
1659 case REAL_TYPE:
1660 return build_real (type, dconstm1);
1662 case FIXED_POINT_TYPE:
1663 /* We can only generate 1 for accum types. */
1664 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1665 return build_fixed (type, fixed_from_double_int (double_int_minus_one,
1666 TYPE_MODE (type)));
1668 case VECTOR_TYPE:
1670 tree scalar = build_minus_one_cst (TREE_TYPE (type));
1672 return build_vector_from_val (type, scalar);
1675 case COMPLEX_TYPE:
1676 return build_complex (type,
1677 build_minus_one_cst (TREE_TYPE (type)),
1678 build_zero_cst (TREE_TYPE (type)));
1680 default:
1681 gcc_unreachable ();
1685 /* Build 0 constant of type TYPE. This is used by constructor folding
1686 and thus the constant should be represented in memory by
1687 zero(es). */
1689 tree
1690 build_zero_cst (tree type)
1692 switch (TREE_CODE (type))
1694 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1695 case POINTER_TYPE: case REFERENCE_TYPE:
1696 case OFFSET_TYPE: case NULLPTR_TYPE:
1697 return build_int_cst (type, 0);
1699 case REAL_TYPE:
1700 return build_real (type, dconst0);
1702 case FIXED_POINT_TYPE:
1703 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
1705 case VECTOR_TYPE:
1707 tree scalar = build_zero_cst (TREE_TYPE (type));
1709 return build_vector_from_val (type, scalar);
1712 case COMPLEX_TYPE:
1714 tree zero = build_zero_cst (TREE_TYPE (type));
1716 return build_complex (type, zero, zero);
1719 default:
1720 if (!AGGREGATE_TYPE_P (type))
1721 return fold_convert (type, integer_zero_node);
1722 return build_constructor (type, NULL);
1727 /* Build a BINFO with LEN language slots. */
1729 tree
1730 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1732 tree t;
1733 size_t length = (offsetof (struct tree_binfo, base_binfos)
1734 + vec<tree, va_gc>::embedded_size (base_binfos));
1736 record_node_allocation_statistics (TREE_BINFO, length);
1738 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1740 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1742 TREE_SET_CODE (t, TREE_BINFO);
1744 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
1746 return t;
1749 /* Create a CASE_LABEL_EXPR tree node and return it. */
1751 tree
1752 build_case_label (tree low_value, tree high_value, tree label_decl)
1754 tree t = make_node (CASE_LABEL_EXPR);
1756 TREE_TYPE (t) = void_type_node;
1757 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
1759 CASE_LOW (t) = low_value;
1760 CASE_HIGH (t) = high_value;
1761 CASE_LABEL (t) = label_decl;
1762 CASE_CHAIN (t) = NULL_TREE;
1764 return t;
1767 /* Build a newly constructed TREE_VEC node of length LEN. */
1769 tree
1770 make_tree_vec_stat (int len MEM_STAT_DECL)
1772 tree t;
1773 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1775 record_node_allocation_statistics (TREE_VEC, length);
1777 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1779 TREE_SET_CODE (t, TREE_VEC);
1780 TREE_VEC_LENGTH (t) = len;
1782 return t;
1785 /* Return 1 if EXPR is the integer constant zero or a complex constant
1786 of zero. */
1789 integer_zerop (const_tree expr)
1791 STRIP_NOPS (expr);
1793 switch (TREE_CODE (expr))
1795 case INTEGER_CST:
1796 return (TREE_INT_CST_LOW (expr) == 0
1797 && TREE_INT_CST_HIGH (expr) == 0);
1798 case COMPLEX_CST:
1799 return (integer_zerop (TREE_REALPART (expr))
1800 && integer_zerop (TREE_IMAGPART (expr)));
1801 case VECTOR_CST:
1803 unsigned i;
1804 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1805 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
1806 return false;
1807 return true;
1809 default:
1810 return false;
1814 /* Return 1 if EXPR is the integer constant one or the corresponding
1815 complex constant. */
1818 integer_onep (const_tree expr)
1820 STRIP_NOPS (expr);
1822 switch (TREE_CODE (expr))
1824 case INTEGER_CST:
1825 return (TREE_INT_CST_LOW (expr) == 1
1826 && TREE_INT_CST_HIGH (expr) == 0);
1827 case COMPLEX_CST:
1828 return (integer_onep (TREE_REALPART (expr))
1829 && integer_zerop (TREE_IMAGPART (expr)));
1830 case VECTOR_CST:
1832 unsigned i;
1833 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1834 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
1835 return false;
1836 return true;
1838 default:
1839 return false;
1843 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1844 it contains, or a complex or vector whose subparts are such integers. */
1847 integer_all_onesp (const_tree expr)
1849 int prec;
1850 int uns;
1852 STRIP_NOPS (expr);
1854 if (TREE_CODE (expr) == COMPLEX_CST
1855 && integer_all_onesp (TREE_REALPART (expr))
1856 && integer_all_onesp (TREE_IMAGPART (expr)))
1857 return 1;
1859 else if (TREE_CODE (expr) == VECTOR_CST)
1861 unsigned i;
1862 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1863 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
1864 return 0;
1865 return 1;
1868 else if (TREE_CODE (expr) != INTEGER_CST)
1869 return 0;
1871 uns = TYPE_UNSIGNED (TREE_TYPE (expr));
1872 if (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1873 && TREE_INT_CST_HIGH (expr) == -1)
1874 return 1;
1875 if (!uns)
1876 return 0;
1878 prec = TYPE_PRECISION (TREE_TYPE (expr));
1879 if (prec >= HOST_BITS_PER_WIDE_INT)
1881 HOST_WIDE_INT high_value;
1882 int shift_amount;
1884 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1886 /* Can not handle precisions greater than twice the host int size. */
1887 gcc_assert (shift_amount <= HOST_BITS_PER_WIDE_INT);
1888 if (shift_amount == HOST_BITS_PER_WIDE_INT)
1889 /* Shifting by the host word size is undefined according to the ANSI
1890 standard, so we must handle this as a special case. */
1891 high_value = -1;
1892 else
1893 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1895 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1896 && TREE_INT_CST_HIGH (expr) == high_value);
1898 else
1899 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1902 /* Return 1 if EXPR is the integer constant minus one. */
1905 integer_minus_onep (const_tree expr)
1907 STRIP_NOPS (expr);
1909 if (TREE_CODE (expr) == COMPLEX_CST)
1910 return (integer_all_onesp (TREE_REALPART (expr))
1911 && integer_zerop (TREE_IMAGPART (expr)));
1912 else
1913 return integer_all_onesp (expr);
1916 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1917 one bit on). */
1920 integer_pow2p (const_tree expr)
1922 int prec;
1923 unsigned HOST_WIDE_INT high, low;
1925 STRIP_NOPS (expr);
1927 if (TREE_CODE (expr) == COMPLEX_CST
1928 && integer_pow2p (TREE_REALPART (expr))
1929 && integer_zerop (TREE_IMAGPART (expr)))
1930 return 1;
1932 if (TREE_CODE (expr) != INTEGER_CST)
1933 return 0;
1935 prec = TYPE_PRECISION (TREE_TYPE (expr));
1936 high = TREE_INT_CST_HIGH (expr);
1937 low = TREE_INT_CST_LOW (expr);
1939 /* First clear all bits that are beyond the type's precision in case
1940 we've been sign extended. */
1942 if (prec == HOST_BITS_PER_DOUBLE_INT)
1944 else if (prec > HOST_BITS_PER_WIDE_INT)
1945 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1946 else
1948 high = 0;
1949 if (prec < HOST_BITS_PER_WIDE_INT)
1950 low &= ~((HOST_WIDE_INT) (-1) << prec);
1953 if (high == 0 && low == 0)
1954 return 0;
1956 return ((high == 0 && (low & (low - 1)) == 0)
1957 || (low == 0 && (high & (high - 1)) == 0));
1960 /* Return 1 if EXPR is an integer constant other than zero or a
1961 complex constant other than zero. */
1964 integer_nonzerop (const_tree expr)
1966 STRIP_NOPS (expr);
1968 return ((TREE_CODE (expr) == INTEGER_CST
1969 && (TREE_INT_CST_LOW (expr) != 0
1970 || TREE_INT_CST_HIGH (expr) != 0))
1971 || (TREE_CODE (expr) == COMPLEX_CST
1972 && (integer_nonzerop (TREE_REALPART (expr))
1973 || integer_nonzerop (TREE_IMAGPART (expr)))));
1976 /* Return 1 if EXPR is the fixed-point constant zero. */
1979 fixed_zerop (const_tree expr)
1981 return (TREE_CODE (expr) == FIXED_CST
1982 && TREE_FIXED_CST (expr).data.is_zero ());
1985 /* Return the power of two represented by a tree node known to be a
1986 power of two. */
1989 tree_log2 (const_tree expr)
1991 int prec;
1992 HOST_WIDE_INT high, low;
1994 STRIP_NOPS (expr);
1996 if (TREE_CODE (expr) == COMPLEX_CST)
1997 return tree_log2 (TREE_REALPART (expr));
1999 prec = TYPE_PRECISION (TREE_TYPE (expr));
2000 high = TREE_INT_CST_HIGH (expr);
2001 low = TREE_INT_CST_LOW (expr);
2003 /* First clear all bits that are beyond the type's precision in case
2004 we've been sign extended. */
2006 if (prec == HOST_BITS_PER_DOUBLE_INT)
2008 else if (prec > HOST_BITS_PER_WIDE_INT)
2009 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
2010 else
2012 high = 0;
2013 if (prec < HOST_BITS_PER_WIDE_INT)
2014 low &= ~((HOST_WIDE_INT) (-1) << prec);
2017 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
2018 : exact_log2 (low));
2021 /* Similar, but return the largest integer Y such that 2 ** Y is less
2022 than or equal to EXPR. */
2025 tree_floor_log2 (const_tree expr)
2027 int prec;
2028 HOST_WIDE_INT high, low;
2030 STRIP_NOPS (expr);
2032 if (TREE_CODE (expr) == COMPLEX_CST)
2033 return tree_log2 (TREE_REALPART (expr));
2035 prec = TYPE_PRECISION (TREE_TYPE (expr));
2036 high = TREE_INT_CST_HIGH (expr);
2037 low = TREE_INT_CST_LOW (expr);
2039 /* First clear all bits that are beyond the type's precision in case
2040 we've been sign extended. Ignore if type's precision hasn't been set
2041 since what we are doing is setting it. */
2043 if (prec == HOST_BITS_PER_DOUBLE_INT || prec == 0)
2045 else if (prec > HOST_BITS_PER_WIDE_INT)
2046 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
2047 else
2049 high = 0;
2050 if (prec < HOST_BITS_PER_WIDE_INT)
2051 low &= ~((HOST_WIDE_INT) (-1) << prec);
2054 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
2055 : floor_log2 (low));
2058 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
2059 decimal float constants, so don't return 1 for them. */
2062 real_zerop (const_tree expr)
2064 STRIP_NOPS (expr);
2066 switch (TREE_CODE (expr))
2068 case REAL_CST:
2069 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
2070 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2071 case COMPLEX_CST:
2072 return real_zerop (TREE_REALPART (expr))
2073 && real_zerop (TREE_IMAGPART (expr));
2074 case VECTOR_CST:
2076 unsigned i;
2077 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2078 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2079 return false;
2080 return true;
2082 default:
2083 return false;
2087 /* Return 1 if EXPR is the real constant one in real or complex form.
2088 Trailing zeroes matter for decimal float constants, so don't return
2089 1 for them. */
2092 real_onep (const_tree expr)
2094 STRIP_NOPS (expr);
2096 switch (TREE_CODE (expr))
2098 case REAL_CST:
2099 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
2100 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2101 case COMPLEX_CST:
2102 return real_onep (TREE_REALPART (expr))
2103 && real_zerop (TREE_IMAGPART (expr));
2104 case VECTOR_CST:
2106 unsigned i;
2107 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2108 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2109 return false;
2110 return true;
2112 default:
2113 return false;
2117 /* Return 1 if EXPR is the real constant two. Trailing zeroes matter
2118 for decimal float constants, so don't return 1 for them. */
2121 real_twop (const_tree expr)
2123 STRIP_NOPS (expr);
2125 switch (TREE_CODE (expr))
2127 case REAL_CST:
2128 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2)
2129 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2130 case COMPLEX_CST:
2131 return real_twop (TREE_REALPART (expr))
2132 && real_zerop (TREE_IMAGPART (expr));
2133 case VECTOR_CST:
2135 unsigned i;
2136 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2137 if (!real_twop (VECTOR_CST_ELT (expr, i)))
2138 return false;
2139 return true;
2141 default:
2142 return false;
2146 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2147 matter for decimal float constants, so don't return 1 for them. */
2150 real_minus_onep (const_tree expr)
2152 STRIP_NOPS (expr);
2154 switch (TREE_CODE (expr))
2156 case REAL_CST:
2157 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2158 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2159 case COMPLEX_CST:
2160 return real_minus_onep (TREE_REALPART (expr))
2161 && real_zerop (TREE_IMAGPART (expr));
2162 case VECTOR_CST:
2164 unsigned i;
2165 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2166 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2167 return false;
2168 return true;
2170 default:
2171 return false;
2175 /* Nonzero if EXP is a constant or a cast of a constant. */
2178 really_constant_p (const_tree exp)
2180 /* This is not quite the same as STRIP_NOPS. It does more. */
2181 while (CONVERT_EXPR_P (exp)
2182 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2183 exp = TREE_OPERAND (exp, 0);
2184 return TREE_CONSTANT (exp);
2187 /* Return first list element whose TREE_VALUE is ELEM.
2188 Return 0 if ELEM is not in LIST. */
2190 tree
2191 value_member (tree elem, tree list)
2193 while (list)
2195 if (elem == TREE_VALUE (list))
2196 return list;
2197 list = TREE_CHAIN (list);
2199 return NULL_TREE;
2202 /* Return first list element whose TREE_PURPOSE is ELEM.
2203 Return 0 if ELEM is not in LIST. */
2205 tree
2206 purpose_member (const_tree elem, tree list)
2208 while (list)
2210 if (elem == TREE_PURPOSE (list))
2211 return list;
2212 list = TREE_CHAIN (list);
2214 return NULL_TREE;
2217 /* Return true if ELEM is in V. */
2219 bool
2220 vec_member (const_tree elem, vec<tree, va_gc> *v)
2222 unsigned ix;
2223 tree t;
2224 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2225 if (elem == t)
2226 return true;
2227 return false;
2230 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2231 NULL_TREE. */
2233 tree
2234 chain_index (int idx, tree chain)
2236 for (; chain && idx > 0; --idx)
2237 chain = TREE_CHAIN (chain);
2238 return chain;
2241 /* Return nonzero if ELEM is part of the chain CHAIN. */
2244 chain_member (const_tree elem, const_tree chain)
2246 while (chain)
2248 if (elem == chain)
2249 return 1;
2250 chain = DECL_CHAIN (chain);
2253 return 0;
2256 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2257 We expect a null pointer to mark the end of the chain.
2258 This is the Lisp primitive `length'. */
2261 list_length (const_tree t)
2263 const_tree p = t;
2264 #ifdef ENABLE_TREE_CHECKING
2265 const_tree q = t;
2266 #endif
2267 int len = 0;
2269 while (p)
2271 p = TREE_CHAIN (p);
2272 #ifdef ENABLE_TREE_CHECKING
2273 if (len % 2)
2274 q = TREE_CHAIN (q);
2275 gcc_assert (p != q);
2276 #endif
2277 len++;
2280 return len;
2283 /* Returns the number of FIELD_DECLs in TYPE. */
2286 fields_length (const_tree type)
2288 tree t = TYPE_FIELDS (type);
2289 int count = 0;
2291 for (; t; t = DECL_CHAIN (t))
2292 if (TREE_CODE (t) == FIELD_DECL)
2293 ++count;
2295 return count;
2298 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2299 UNION_TYPE TYPE, or NULL_TREE if none. */
2301 tree
2302 first_field (const_tree type)
2304 tree t = TYPE_FIELDS (type);
2305 while (t && TREE_CODE (t) != FIELD_DECL)
2306 t = TREE_CHAIN (t);
2307 return t;
2310 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2311 by modifying the last node in chain 1 to point to chain 2.
2312 This is the Lisp primitive `nconc'. */
2314 tree
2315 chainon (tree op1, tree op2)
2317 tree t1;
2319 if (!op1)
2320 return op2;
2321 if (!op2)
2322 return op1;
2324 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2325 continue;
2326 TREE_CHAIN (t1) = op2;
2328 #ifdef ENABLE_TREE_CHECKING
2330 tree t2;
2331 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2332 gcc_assert (t2 != t1);
2334 #endif
2336 return op1;
2339 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2341 tree
2342 tree_last (tree chain)
2344 tree next;
2345 if (chain)
2346 while ((next = TREE_CHAIN (chain)))
2347 chain = next;
2348 return chain;
2351 /* Reverse the order of elements in the chain T,
2352 and return the new head of the chain (old last element). */
2354 tree
2355 nreverse (tree t)
2357 tree prev = 0, decl, next;
2358 for (decl = t; decl; decl = next)
2360 /* We shouldn't be using this function to reverse BLOCK chains; we
2361 have blocks_nreverse for that. */
2362 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2363 next = TREE_CHAIN (decl);
2364 TREE_CHAIN (decl) = prev;
2365 prev = decl;
2367 return prev;
2370 /* Return a newly created TREE_LIST node whose
2371 purpose and value fields are PARM and VALUE. */
2373 tree
2374 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2376 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2377 TREE_PURPOSE (t) = parm;
2378 TREE_VALUE (t) = value;
2379 return t;
2382 /* Build a chain of TREE_LIST nodes from a vector. */
2384 tree
2385 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2387 tree ret = NULL_TREE;
2388 tree *pp = &ret;
2389 unsigned int i;
2390 tree t;
2391 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2393 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2394 pp = &TREE_CHAIN (*pp);
2396 return ret;
2399 /* Return a newly created TREE_LIST node whose
2400 purpose and value fields are PURPOSE and VALUE
2401 and whose TREE_CHAIN is CHAIN. */
2403 tree
2404 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2406 tree node;
2408 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
2409 memset (node, 0, sizeof (struct tree_common));
2411 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2413 TREE_SET_CODE (node, TREE_LIST);
2414 TREE_CHAIN (node) = chain;
2415 TREE_PURPOSE (node) = purpose;
2416 TREE_VALUE (node) = value;
2417 return node;
2420 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2421 trees. */
2423 vec<tree, va_gc> *
2424 ctor_to_vec (tree ctor)
2426 vec<tree, va_gc> *vec;
2427 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2428 unsigned int ix;
2429 tree val;
2431 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2432 vec->quick_push (val);
2434 return vec;
2437 /* Return the size nominally occupied by an object of type TYPE
2438 when it resides in memory. The value is measured in units of bytes,
2439 and its data type is that normally used for type sizes
2440 (which is the first type created by make_signed_type or
2441 make_unsigned_type). */
2443 tree
2444 size_in_bytes (const_tree type)
2446 tree t;
2448 if (type == error_mark_node)
2449 return integer_zero_node;
2451 type = TYPE_MAIN_VARIANT (type);
2452 t = TYPE_SIZE_UNIT (type);
2454 if (t == 0)
2456 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2457 return size_zero_node;
2460 return t;
2463 /* Return the 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 int_size_in_bytes (const_tree type)
2469 tree t;
2471 if (type == error_mark_node)
2472 return 0;
2474 type = TYPE_MAIN_VARIANT (type);
2475 t = TYPE_SIZE_UNIT (type);
2476 if (t == 0
2477 || TREE_CODE (t) != INTEGER_CST
2478 || TREE_INT_CST_HIGH (t) != 0
2479 /* If the result would appear negative, it's too big to represent. */
2480 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
2481 return -1;
2483 return TREE_INT_CST_LOW (t);
2486 /* Return the maximum size of TYPE (in bytes) as a wide integer
2487 or return -1 if the size can vary or is larger than an integer. */
2489 HOST_WIDE_INT
2490 max_int_size_in_bytes (const_tree type)
2492 HOST_WIDE_INT size = -1;
2493 tree size_tree;
2495 /* If this is an array type, check for a possible MAX_SIZE attached. */
2497 if (TREE_CODE (type) == ARRAY_TYPE)
2499 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2501 if (size_tree && host_integerp (size_tree, 1))
2502 size = tree_low_cst (size_tree, 1);
2505 /* If we still haven't been able to get a size, see if the language
2506 can compute a maximum size. */
2508 if (size == -1)
2510 size_tree = lang_hooks.types.max_size (type);
2512 if (size_tree && host_integerp (size_tree, 1))
2513 size = tree_low_cst (size_tree, 1);
2516 return size;
2519 /* Returns a tree for the size of EXP in bytes. */
2521 tree
2522 tree_expr_size (const_tree exp)
2524 if (DECL_P (exp)
2525 && DECL_SIZE_UNIT (exp) != 0)
2526 return DECL_SIZE_UNIT (exp);
2527 else
2528 return size_in_bytes (TREE_TYPE (exp));
2531 /* Return the bit position of FIELD, in bits from the start of the record.
2532 This is a tree of type bitsizetype. */
2534 tree
2535 bit_position (const_tree field)
2537 return bit_from_pos (DECL_FIELD_OFFSET (field),
2538 DECL_FIELD_BIT_OFFSET (field));
2541 /* Likewise, but return as an integer. It must be representable in
2542 that way (since it could be a signed value, we don't have the
2543 option of returning -1 like int_size_in_byte can. */
2545 HOST_WIDE_INT
2546 int_bit_position (const_tree field)
2548 return tree_low_cst (bit_position (field), 0);
2551 /* Return the byte position of FIELD, in bytes from the start of the record.
2552 This is a tree of type sizetype. */
2554 tree
2555 byte_position (const_tree field)
2557 return byte_from_pos (DECL_FIELD_OFFSET (field),
2558 DECL_FIELD_BIT_OFFSET (field));
2561 /* Likewise, but return as an integer. It must be representable in
2562 that way (since it could be a signed value, we don't have the
2563 option of returning -1 like int_size_in_byte can. */
2565 HOST_WIDE_INT
2566 int_byte_position (const_tree field)
2568 return tree_low_cst (byte_position (field), 0);
2571 /* Return the strictest alignment, in bits, that T is known to have. */
2573 unsigned int
2574 expr_align (const_tree t)
2576 unsigned int align0, align1;
2578 switch (TREE_CODE (t))
2580 CASE_CONVERT: case NON_LVALUE_EXPR:
2581 /* If we have conversions, we know that the alignment of the
2582 object must meet each of the alignments of the types. */
2583 align0 = expr_align (TREE_OPERAND (t, 0));
2584 align1 = TYPE_ALIGN (TREE_TYPE (t));
2585 return MAX (align0, align1);
2587 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2588 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2589 case CLEANUP_POINT_EXPR:
2590 /* These don't change the alignment of an object. */
2591 return expr_align (TREE_OPERAND (t, 0));
2593 case COND_EXPR:
2594 /* The best we can do is say that the alignment is the least aligned
2595 of the two arms. */
2596 align0 = expr_align (TREE_OPERAND (t, 1));
2597 align1 = expr_align (TREE_OPERAND (t, 2));
2598 return MIN (align0, align1);
2600 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2601 meaningfully, it's always 1. */
2602 case LABEL_DECL: case CONST_DECL:
2603 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2604 case FUNCTION_DECL:
2605 gcc_assert (DECL_ALIGN (t) != 0);
2606 return DECL_ALIGN (t);
2608 default:
2609 break;
2612 /* Otherwise take the alignment from that of the type. */
2613 return TYPE_ALIGN (TREE_TYPE (t));
2616 /* Return, as a tree node, the number of elements for TYPE (which is an
2617 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2619 tree
2620 array_type_nelts (const_tree type)
2622 tree index_type, min, max;
2624 /* If they did it with unspecified bounds, then we should have already
2625 given an error about it before we got here. */
2626 if (! TYPE_DOMAIN (type))
2627 return error_mark_node;
2629 index_type = TYPE_DOMAIN (type);
2630 min = TYPE_MIN_VALUE (index_type);
2631 max = TYPE_MAX_VALUE (index_type);
2633 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
2634 if (!max)
2635 return error_mark_node;
2637 return (integer_zerop (min)
2638 ? max
2639 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2642 /* If arg is static -- a reference to an object in static storage -- then
2643 return the object. This is not the same as the C meaning of `static'.
2644 If arg isn't static, return NULL. */
2646 tree
2647 staticp (tree arg)
2649 switch (TREE_CODE (arg))
2651 case FUNCTION_DECL:
2652 /* Nested functions are static, even though taking their address will
2653 involve a trampoline as we unnest the nested function and create
2654 the trampoline on the tree level. */
2655 return arg;
2657 case VAR_DECL:
2658 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2659 && ! DECL_THREAD_LOCAL_P (arg)
2660 && ! DECL_DLLIMPORT_P (arg)
2661 ? arg : NULL);
2663 case CONST_DECL:
2664 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2665 ? arg : NULL);
2667 case CONSTRUCTOR:
2668 return TREE_STATIC (arg) ? arg : NULL;
2670 case LABEL_DECL:
2671 case STRING_CST:
2672 return arg;
2674 case COMPONENT_REF:
2675 /* If the thing being referenced is not a field, then it is
2676 something language specific. */
2677 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
2679 /* If we are referencing a bitfield, we can't evaluate an
2680 ADDR_EXPR at compile time and so it isn't a constant. */
2681 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2682 return NULL;
2684 return staticp (TREE_OPERAND (arg, 0));
2686 case BIT_FIELD_REF:
2687 return NULL;
2689 case INDIRECT_REF:
2690 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2692 case ARRAY_REF:
2693 case ARRAY_RANGE_REF:
2694 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2695 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2696 return staticp (TREE_OPERAND (arg, 0));
2697 else
2698 return NULL;
2700 case COMPOUND_LITERAL_EXPR:
2701 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
2703 default:
2704 return NULL;
2711 /* Return whether OP is a DECL whose address is function-invariant. */
2713 bool
2714 decl_address_invariant_p (const_tree op)
2716 /* The conditions below are slightly less strict than the one in
2717 staticp. */
2719 switch (TREE_CODE (op))
2721 case PARM_DECL:
2722 case RESULT_DECL:
2723 case LABEL_DECL:
2724 case FUNCTION_DECL:
2725 return true;
2727 case VAR_DECL:
2728 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2729 || DECL_THREAD_LOCAL_P (op)
2730 || DECL_CONTEXT (op) == current_function_decl
2731 || decl_function_context (op) == current_function_decl)
2732 return true;
2733 break;
2735 case CONST_DECL:
2736 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2737 || decl_function_context (op) == current_function_decl)
2738 return true;
2739 break;
2741 default:
2742 break;
2745 return false;
2748 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
2750 bool
2751 decl_address_ip_invariant_p (const_tree op)
2753 /* The conditions below are slightly less strict than the one in
2754 staticp. */
2756 switch (TREE_CODE (op))
2758 case LABEL_DECL:
2759 case FUNCTION_DECL:
2760 case STRING_CST:
2761 return true;
2763 case VAR_DECL:
2764 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2765 && !DECL_DLLIMPORT_P (op))
2766 || DECL_THREAD_LOCAL_P (op))
2767 return true;
2768 break;
2770 case CONST_DECL:
2771 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
2772 return true;
2773 break;
2775 default:
2776 break;
2779 return false;
2783 /* Return true if T is function-invariant (internal function, does
2784 not handle arithmetic; that's handled in skip_simple_arithmetic and
2785 tree_invariant_p). */
2787 static bool tree_invariant_p (tree t);
2789 static bool
2790 tree_invariant_p_1 (tree t)
2792 tree op;
2794 if (TREE_CONSTANT (t)
2795 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
2796 return true;
2798 switch (TREE_CODE (t))
2800 case SAVE_EXPR:
2801 return true;
2803 case ADDR_EXPR:
2804 op = TREE_OPERAND (t, 0);
2805 while (handled_component_p (op))
2807 switch (TREE_CODE (op))
2809 case ARRAY_REF:
2810 case ARRAY_RANGE_REF:
2811 if (!tree_invariant_p (TREE_OPERAND (op, 1))
2812 || TREE_OPERAND (op, 2) != NULL_TREE
2813 || TREE_OPERAND (op, 3) != NULL_TREE)
2814 return false;
2815 break;
2817 case COMPONENT_REF:
2818 if (TREE_OPERAND (op, 2) != NULL_TREE)
2819 return false;
2820 break;
2822 default:;
2824 op = TREE_OPERAND (op, 0);
2827 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2829 default:
2830 break;
2833 return false;
2836 /* Return true if T is function-invariant. */
2838 static bool
2839 tree_invariant_p (tree t)
2841 tree inner = skip_simple_arithmetic (t);
2842 return tree_invariant_p_1 (inner);
2845 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
2846 Do this to any expression which may be used in more than one place,
2847 but must be evaluated only once.
2849 Normally, expand_expr would reevaluate the expression each time.
2850 Calling save_expr produces something that is evaluated and recorded
2851 the first time expand_expr is called on it. Subsequent calls to
2852 expand_expr just reuse the recorded value.
2854 The call to expand_expr that generates code that actually computes
2855 the value is the first call *at compile time*. Subsequent calls
2856 *at compile time* generate code to use the saved value.
2857 This produces correct result provided that *at run time* control
2858 always flows through the insns made by the first expand_expr
2859 before reaching the other places where the save_expr was evaluated.
2860 You, the caller of save_expr, must make sure this is so.
2862 Constants, and certain read-only nodes, are returned with no
2863 SAVE_EXPR because that is safe. Expressions containing placeholders
2864 are not touched; see tree.def for an explanation of what these
2865 are used for. */
2867 tree
2868 save_expr (tree expr)
2870 tree t = fold (expr);
2871 tree inner;
2873 /* If the tree evaluates to a constant, then we don't want to hide that
2874 fact (i.e. this allows further folding, and direct checks for constants).
2875 However, a read-only object that has side effects cannot be bypassed.
2876 Since it is no problem to reevaluate literals, we just return the
2877 literal node. */
2878 inner = skip_simple_arithmetic (t);
2879 if (TREE_CODE (inner) == ERROR_MARK)
2880 return inner;
2882 if (tree_invariant_p_1 (inner))
2883 return t;
2885 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
2886 it means that the size or offset of some field of an object depends on
2887 the value within another field.
2889 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
2890 and some variable since it would then need to be both evaluated once and
2891 evaluated more than once. Front-ends must assure this case cannot
2892 happen by surrounding any such subexpressions in their own SAVE_EXPR
2893 and forcing evaluation at the proper time. */
2894 if (contains_placeholder_p (inner))
2895 return t;
2897 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
2898 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
2900 /* This expression might be placed ahead of a jump to ensure that the
2901 value was computed on both sides of the jump. So make sure it isn't
2902 eliminated as dead. */
2903 TREE_SIDE_EFFECTS (t) = 1;
2904 return t;
2907 /* Look inside EXPR into any simple arithmetic operations. Return the
2908 outermost non-arithmetic or non-invariant node. */
2910 tree
2911 skip_simple_arithmetic (tree expr)
2913 /* We don't care about whether this can be used as an lvalue in this
2914 context. */
2915 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
2916 expr = TREE_OPERAND (expr, 0);
2918 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
2919 a constant, it will be more efficient to not make another SAVE_EXPR since
2920 it will allow better simplification and GCSE will be able to merge the
2921 computations if they actually occur. */
2922 while (true)
2924 if (UNARY_CLASS_P (expr))
2925 expr = TREE_OPERAND (expr, 0);
2926 else if (BINARY_CLASS_P (expr))
2928 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
2929 expr = TREE_OPERAND (expr, 0);
2930 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
2931 expr = TREE_OPERAND (expr, 1);
2932 else
2933 break;
2935 else
2936 break;
2939 return expr;
2942 /* Look inside EXPR into simple arithmetic operations involving constants.
2943 Return the outermost non-arithmetic or non-constant node. */
2945 tree
2946 skip_simple_constant_arithmetic (tree expr)
2948 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
2949 expr = TREE_OPERAND (expr, 0);
2951 while (true)
2953 if (UNARY_CLASS_P (expr))
2954 expr = TREE_OPERAND (expr, 0);
2955 else if (BINARY_CLASS_P (expr))
2957 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
2958 expr = TREE_OPERAND (expr, 0);
2959 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
2960 expr = TREE_OPERAND (expr, 1);
2961 else
2962 break;
2964 else
2965 break;
2968 return expr;
2971 /* Return which tree structure is used by T. */
2973 enum tree_node_structure_enum
2974 tree_node_structure (const_tree t)
2976 const enum tree_code code = TREE_CODE (t);
2977 return tree_node_structure_for_code (code);
2980 /* Set various status flags when building a CALL_EXPR object T. */
2982 static void
2983 process_call_operands (tree t)
2985 bool side_effects = TREE_SIDE_EFFECTS (t);
2986 bool read_only = false;
2987 int i = call_expr_flags (t);
2989 /* Calls have side-effects, except those to const or pure functions. */
2990 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
2991 side_effects = true;
2992 /* Propagate TREE_READONLY of arguments for const functions. */
2993 if (i & ECF_CONST)
2994 read_only = true;
2996 if (!side_effects || read_only)
2997 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
2999 tree op = TREE_OPERAND (t, i);
3000 if (op && TREE_SIDE_EFFECTS (op))
3001 side_effects = true;
3002 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
3003 read_only = false;
3006 TREE_SIDE_EFFECTS (t) = side_effects;
3007 TREE_READONLY (t) = read_only;
3010 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
3011 size or offset that depends on a field within a record. */
3013 bool
3014 contains_placeholder_p (const_tree exp)
3016 enum tree_code code;
3018 if (!exp)
3019 return 0;
3021 code = TREE_CODE (exp);
3022 if (code == PLACEHOLDER_EXPR)
3023 return 1;
3025 switch (TREE_CODE_CLASS (code))
3027 case tcc_reference:
3028 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
3029 position computations since they will be converted into a
3030 WITH_RECORD_EXPR involving the reference, which will assume
3031 here will be valid. */
3032 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3034 case tcc_exceptional:
3035 if (code == TREE_LIST)
3036 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
3037 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
3038 break;
3040 case tcc_unary:
3041 case tcc_binary:
3042 case tcc_comparison:
3043 case tcc_expression:
3044 switch (code)
3046 case COMPOUND_EXPR:
3047 /* Ignoring the first operand isn't quite right, but works best. */
3048 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
3050 case COND_EXPR:
3051 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3052 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
3053 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
3055 case SAVE_EXPR:
3056 /* The save_expr function never wraps anything containing
3057 a PLACEHOLDER_EXPR. */
3058 return 0;
3060 default:
3061 break;
3064 switch (TREE_CODE_LENGTH (code))
3066 case 1:
3067 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3068 case 2:
3069 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3070 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
3071 default:
3072 return 0;
3075 case tcc_vl_exp:
3076 switch (code)
3078 case CALL_EXPR:
3080 const_tree arg;
3081 const_call_expr_arg_iterator iter;
3082 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
3083 if (CONTAINS_PLACEHOLDER_P (arg))
3084 return 1;
3085 return 0;
3087 default:
3088 return 0;
3091 default:
3092 return 0;
3094 return 0;
3097 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3098 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3099 field positions. */
3101 static bool
3102 type_contains_placeholder_1 (const_tree type)
3104 /* If the size contains a placeholder or the parent type (component type in
3105 the case of arrays) type involves a placeholder, this type does. */
3106 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3107 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3108 || (!POINTER_TYPE_P (type)
3109 && TREE_TYPE (type)
3110 && type_contains_placeholder_p (TREE_TYPE (type))))
3111 return true;
3113 /* Now do type-specific checks. Note that the last part of the check above
3114 greatly limits what we have to do below. */
3115 switch (TREE_CODE (type))
3117 case VOID_TYPE:
3118 case COMPLEX_TYPE:
3119 case ENUMERAL_TYPE:
3120 case BOOLEAN_TYPE:
3121 case POINTER_TYPE:
3122 case OFFSET_TYPE:
3123 case REFERENCE_TYPE:
3124 case METHOD_TYPE:
3125 case FUNCTION_TYPE:
3126 case VECTOR_TYPE:
3127 case NULLPTR_TYPE:
3128 return false;
3130 case INTEGER_TYPE:
3131 case REAL_TYPE:
3132 case FIXED_POINT_TYPE:
3133 /* Here we just check the bounds. */
3134 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3135 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3137 case ARRAY_TYPE:
3138 /* We have already checked the component type above, so just check the
3139 domain type. */
3140 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3142 case RECORD_TYPE:
3143 case UNION_TYPE:
3144 case QUAL_UNION_TYPE:
3146 tree field;
3148 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3149 if (TREE_CODE (field) == FIELD_DECL
3150 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3151 || (TREE_CODE (type) == QUAL_UNION_TYPE
3152 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3153 || type_contains_placeholder_p (TREE_TYPE (field))))
3154 return true;
3156 return false;
3159 default:
3160 gcc_unreachable ();
3164 /* Wrapper around above function used to cache its result. */
3166 bool
3167 type_contains_placeholder_p (tree type)
3169 bool result;
3171 /* If the contains_placeholder_bits field has been initialized,
3172 then we know the answer. */
3173 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3174 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3176 /* Indicate that we've seen this type node, and the answer is false.
3177 This is what we want to return if we run into recursion via fields. */
3178 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3180 /* Compute the real value. */
3181 result = type_contains_placeholder_1 (type);
3183 /* Store the real value. */
3184 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3186 return result;
3189 /* Push tree EXP onto vector QUEUE if it is not already present. */
3191 static void
3192 push_without_duplicates (tree exp, vec<tree> *queue)
3194 unsigned int i;
3195 tree iter;
3197 FOR_EACH_VEC_ELT (*queue, i, iter)
3198 if (simple_cst_equal (iter, exp) == 1)
3199 break;
3201 if (!iter)
3202 queue->safe_push (exp);
3205 /* Given a tree EXP, find all occurrences of references to fields
3206 in a PLACEHOLDER_EXPR and place them in vector REFS without
3207 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3208 we assume here that EXP contains only arithmetic expressions
3209 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3210 argument list. */
3212 void
3213 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3215 enum tree_code code = TREE_CODE (exp);
3216 tree inner;
3217 int i;
3219 /* We handle TREE_LIST and COMPONENT_REF separately. */
3220 if (code == TREE_LIST)
3222 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3223 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3225 else if (code == COMPONENT_REF)
3227 for (inner = TREE_OPERAND (exp, 0);
3228 REFERENCE_CLASS_P (inner);
3229 inner = TREE_OPERAND (inner, 0))
3232 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3233 push_without_duplicates (exp, refs);
3234 else
3235 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3237 else
3238 switch (TREE_CODE_CLASS (code))
3240 case tcc_constant:
3241 break;
3243 case tcc_declaration:
3244 /* Variables allocated to static storage can stay. */
3245 if (!TREE_STATIC (exp))
3246 push_without_duplicates (exp, refs);
3247 break;
3249 case tcc_expression:
3250 /* This is the pattern built in ada/make_aligning_type. */
3251 if (code == ADDR_EXPR
3252 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3254 push_without_duplicates (exp, refs);
3255 break;
3258 /* Fall through... */
3260 case tcc_exceptional:
3261 case tcc_unary:
3262 case tcc_binary:
3263 case tcc_comparison:
3264 case tcc_reference:
3265 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3266 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3267 break;
3269 case tcc_vl_exp:
3270 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3271 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3272 break;
3274 default:
3275 gcc_unreachable ();
3279 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3280 return a tree with all occurrences of references to F in a
3281 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3282 CONST_DECLs. Note that we assume here that EXP contains only
3283 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3284 occurring only in their argument list. */
3286 tree
3287 substitute_in_expr (tree exp, tree f, tree r)
3289 enum tree_code code = TREE_CODE (exp);
3290 tree op0, op1, op2, op3;
3291 tree new_tree;
3293 /* We handle TREE_LIST and COMPONENT_REF separately. */
3294 if (code == TREE_LIST)
3296 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3297 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3298 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3299 return exp;
3301 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3303 else if (code == COMPONENT_REF)
3305 tree inner;
3307 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3308 and it is the right field, replace it with R. */
3309 for (inner = TREE_OPERAND (exp, 0);
3310 REFERENCE_CLASS_P (inner);
3311 inner = TREE_OPERAND (inner, 0))
3314 /* The field. */
3315 op1 = TREE_OPERAND (exp, 1);
3317 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3318 return r;
3320 /* If this expression hasn't been completed let, leave it alone. */
3321 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3322 return exp;
3324 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3325 if (op0 == TREE_OPERAND (exp, 0))
3326 return exp;
3328 new_tree
3329 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3331 else
3332 switch (TREE_CODE_CLASS (code))
3334 case tcc_constant:
3335 return exp;
3337 case tcc_declaration:
3338 if (exp == f)
3339 return r;
3340 else
3341 return exp;
3343 case tcc_expression:
3344 if (exp == f)
3345 return r;
3347 /* Fall through... */
3349 case tcc_exceptional:
3350 case tcc_unary:
3351 case tcc_binary:
3352 case tcc_comparison:
3353 case tcc_reference:
3354 switch (TREE_CODE_LENGTH (code))
3356 case 0:
3357 return exp;
3359 case 1:
3360 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3361 if (op0 == TREE_OPERAND (exp, 0))
3362 return exp;
3364 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3365 break;
3367 case 2:
3368 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3369 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3371 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3372 return exp;
3374 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3375 break;
3377 case 3:
3378 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3379 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3380 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3382 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3383 && op2 == TREE_OPERAND (exp, 2))
3384 return exp;
3386 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3387 break;
3389 case 4:
3390 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3391 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3392 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3393 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3395 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3396 && op2 == TREE_OPERAND (exp, 2)
3397 && op3 == TREE_OPERAND (exp, 3))
3398 return exp;
3400 new_tree
3401 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3402 break;
3404 default:
3405 gcc_unreachable ();
3407 break;
3409 case tcc_vl_exp:
3411 int i;
3413 new_tree = NULL_TREE;
3415 /* If we are trying to replace F with a constant, inline back
3416 functions which do nothing else than computing a value from
3417 the arguments they are passed. This makes it possible to
3418 fold partially or entirely the replacement expression. */
3419 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3421 tree t = maybe_inline_call_in_expr (exp);
3422 if (t)
3423 return SUBSTITUTE_IN_EXPR (t, f, r);
3426 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3428 tree op = TREE_OPERAND (exp, i);
3429 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3430 if (new_op != op)
3432 if (!new_tree)
3433 new_tree = copy_node (exp);
3434 TREE_OPERAND (new_tree, i) = new_op;
3438 if (new_tree)
3440 new_tree = fold (new_tree);
3441 if (TREE_CODE (new_tree) == CALL_EXPR)
3442 process_call_operands (new_tree);
3444 else
3445 return exp;
3447 break;
3449 default:
3450 gcc_unreachable ();
3453 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3455 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3456 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3458 return new_tree;
3461 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3462 for it within OBJ, a tree that is an object or a chain of references. */
3464 tree
3465 substitute_placeholder_in_expr (tree exp, tree obj)
3467 enum tree_code code = TREE_CODE (exp);
3468 tree op0, op1, op2, op3;
3469 tree new_tree;
3471 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3472 in the chain of OBJ. */
3473 if (code == PLACEHOLDER_EXPR)
3475 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3476 tree elt;
3478 for (elt = obj; elt != 0;
3479 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3480 || TREE_CODE (elt) == COND_EXPR)
3481 ? TREE_OPERAND (elt, 1)
3482 : (REFERENCE_CLASS_P (elt)
3483 || UNARY_CLASS_P (elt)
3484 || BINARY_CLASS_P (elt)
3485 || VL_EXP_CLASS_P (elt)
3486 || EXPRESSION_CLASS_P (elt))
3487 ? TREE_OPERAND (elt, 0) : 0))
3488 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3489 return elt;
3491 for (elt = obj; elt != 0;
3492 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3493 || TREE_CODE (elt) == COND_EXPR)
3494 ? TREE_OPERAND (elt, 1)
3495 : (REFERENCE_CLASS_P (elt)
3496 || UNARY_CLASS_P (elt)
3497 || BINARY_CLASS_P (elt)
3498 || VL_EXP_CLASS_P (elt)
3499 || EXPRESSION_CLASS_P (elt))
3500 ? TREE_OPERAND (elt, 0) : 0))
3501 if (POINTER_TYPE_P (TREE_TYPE (elt))
3502 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3503 == need_type))
3504 return fold_build1 (INDIRECT_REF, need_type, elt);
3506 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3507 survives until RTL generation, there will be an error. */
3508 return exp;
3511 /* TREE_LIST is special because we need to look at TREE_VALUE
3512 and TREE_CHAIN, not TREE_OPERANDS. */
3513 else if (code == TREE_LIST)
3515 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3516 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3517 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3518 return exp;
3520 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3522 else
3523 switch (TREE_CODE_CLASS (code))
3525 case tcc_constant:
3526 case tcc_declaration:
3527 return exp;
3529 case tcc_exceptional:
3530 case tcc_unary:
3531 case tcc_binary:
3532 case tcc_comparison:
3533 case tcc_expression:
3534 case tcc_reference:
3535 case tcc_statement:
3536 switch (TREE_CODE_LENGTH (code))
3538 case 0:
3539 return exp;
3541 case 1:
3542 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3543 if (op0 == TREE_OPERAND (exp, 0))
3544 return exp;
3546 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3547 break;
3549 case 2:
3550 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3551 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3553 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3554 return exp;
3556 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3557 break;
3559 case 3:
3560 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3561 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3562 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3564 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3565 && op2 == TREE_OPERAND (exp, 2))
3566 return exp;
3568 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3569 break;
3571 case 4:
3572 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3573 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3574 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3575 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3577 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3578 && op2 == TREE_OPERAND (exp, 2)
3579 && op3 == TREE_OPERAND (exp, 3))
3580 return exp;
3582 new_tree
3583 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3584 break;
3586 default:
3587 gcc_unreachable ();
3589 break;
3591 case tcc_vl_exp:
3593 int i;
3595 new_tree = NULL_TREE;
3597 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3599 tree op = TREE_OPERAND (exp, i);
3600 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
3601 if (new_op != op)
3603 if (!new_tree)
3604 new_tree = copy_node (exp);
3605 TREE_OPERAND (new_tree, i) = new_op;
3609 if (new_tree)
3611 new_tree = fold (new_tree);
3612 if (TREE_CODE (new_tree) == CALL_EXPR)
3613 process_call_operands (new_tree);
3615 else
3616 return exp;
3618 break;
3620 default:
3621 gcc_unreachable ();
3624 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3626 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3627 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3629 return new_tree;
3632 /* Stabilize a reference so that we can use it any number of times
3633 without causing its operands to be evaluated more than once.
3634 Returns the stabilized reference. This works by means of save_expr,
3635 so see the caveats in the comments about save_expr.
3637 Also allows conversion expressions whose operands are references.
3638 Any other kind of expression is returned unchanged. */
3640 tree
3641 stabilize_reference (tree ref)
3643 tree result;
3644 enum tree_code code = TREE_CODE (ref);
3646 switch (code)
3648 case VAR_DECL:
3649 case PARM_DECL:
3650 case RESULT_DECL:
3651 /* No action is needed in this case. */
3652 return ref;
3654 CASE_CONVERT:
3655 case FLOAT_EXPR:
3656 case FIX_TRUNC_EXPR:
3657 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
3658 break;
3660 case INDIRECT_REF:
3661 result = build_nt (INDIRECT_REF,
3662 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
3663 break;
3665 case COMPONENT_REF:
3666 result = build_nt (COMPONENT_REF,
3667 stabilize_reference (TREE_OPERAND (ref, 0)),
3668 TREE_OPERAND (ref, 1), NULL_TREE);
3669 break;
3671 case BIT_FIELD_REF:
3672 result = build_nt (BIT_FIELD_REF,
3673 stabilize_reference (TREE_OPERAND (ref, 0)),
3674 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
3675 break;
3677 case ARRAY_REF:
3678 result = build_nt (ARRAY_REF,
3679 stabilize_reference (TREE_OPERAND (ref, 0)),
3680 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3681 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3682 break;
3684 case ARRAY_RANGE_REF:
3685 result = build_nt (ARRAY_RANGE_REF,
3686 stabilize_reference (TREE_OPERAND (ref, 0)),
3687 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3688 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3689 break;
3691 case COMPOUND_EXPR:
3692 /* We cannot wrap the first expression in a SAVE_EXPR, as then
3693 it wouldn't be ignored. This matters when dealing with
3694 volatiles. */
3695 return stabilize_reference_1 (ref);
3697 /* If arg isn't a kind of lvalue we recognize, make no change.
3698 Caller should recognize the error for an invalid lvalue. */
3699 default:
3700 return ref;
3702 case ERROR_MARK:
3703 return error_mark_node;
3706 TREE_TYPE (result) = TREE_TYPE (ref);
3707 TREE_READONLY (result) = TREE_READONLY (ref);
3708 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
3709 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
3711 return result;
3714 /* Subroutine of stabilize_reference; this is called for subtrees of
3715 references. Any expression with side-effects must be put in a SAVE_EXPR
3716 to ensure that it is only evaluated once.
3718 We don't put SAVE_EXPR nodes around everything, because assigning very
3719 simple expressions to temporaries causes us to miss good opportunities
3720 for optimizations. Among other things, the opportunity to fold in the
3721 addition of a constant into an addressing mode often gets lost, e.g.
3722 "y[i+1] += x;". In general, we take the approach that we should not make
3723 an assignment unless we are forced into it - i.e., that any non-side effect
3724 operator should be allowed, and that cse should take care of coalescing
3725 multiple utterances of the same expression should that prove fruitful. */
3727 tree
3728 stabilize_reference_1 (tree e)
3730 tree result;
3731 enum tree_code code = TREE_CODE (e);
3733 /* We cannot ignore const expressions because it might be a reference
3734 to a const array but whose index contains side-effects. But we can
3735 ignore things that are actual constant or that already have been
3736 handled by this function. */
3738 if (tree_invariant_p (e))
3739 return e;
3741 switch (TREE_CODE_CLASS (code))
3743 case tcc_exceptional:
3744 case tcc_type:
3745 case tcc_declaration:
3746 case tcc_comparison:
3747 case tcc_statement:
3748 case tcc_expression:
3749 case tcc_reference:
3750 case tcc_vl_exp:
3751 /* If the expression has side-effects, then encase it in a SAVE_EXPR
3752 so that it will only be evaluated once. */
3753 /* The reference (r) and comparison (<) classes could be handled as
3754 below, but it is generally faster to only evaluate them once. */
3755 if (TREE_SIDE_EFFECTS (e))
3756 return save_expr (e);
3757 return e;
3759 case tcc_constant:
3760 /* Constants need no processing. In fact, we should never reach
3761 here. */
3762 return e;
3764 case tcc_binary:
3765 /* Division is slow and tends to be compiled with jumps,
3766 especially the division by powers of 2 that is often
3767 found inside of an array reference. So do it just once. */
3768 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3769 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3770 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3771 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3772 return save_expr (e);
3773 /* Recursively stabilize each operand. */
3774 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3775 stabilize_reference_1 (TREE_OPERAND (e, 1)));
3776 break;
3778 case tcc_unary:
3779 /* Recursively stabilize each operand. */
3780 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3781 break;
3783 default:
3784 gcc_unreachable ();
3787 TREE_TYPE (result) = TREE_TYPE (e);
3788 TREE_READONLY (result) = TREE_READONLY (e);
3789 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3790 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3792 return result;
3795 /* Low-level constructors for expressions. */
3797 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
3798 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
3800 void
3801 recompute_tree_invariant_for_addr_expr (tree t)
3803 tree node;
3804 bool tc = true, se = false;
3806 /* We started out assuming this address is both invariant and constant, but
3807 does not have side effects. Now go down any handled components and see if
3808 any of them involve offsets that are either non-constant or non-invariant.
3809 Also check for side-effects.
3811 ??? Note that this code makes no attempt to deal with the case where
3812 taking the address of something causes a copy due to misalignment. */
3814 #define UPDATE_FLAGS(NODE) \
3815 do { tree _node = (NODE); \
3816 if (_node && !TREE_CONSTANT (_node)) tc = false; \
3817 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
3819 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
3820 node = TREE_OPERAND (node, 0))
3822 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
3823 array reference (probably made temporarily by the G++ front end),
3824 so ignore all the operands. */
3825 if ((TREE_CODE (node) == ARRAY_REF
3826 || TREE_CODE (node) == ARRAY_RANGE_REF)
3827 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
3829 UPDATE_FLAGS (TREE_OPERAND (node, 1));
3830 if (TREE_OPERAND (node, 2))
3831 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3832 if (TREE_OPERAND (node, 3))
3833 UPDATE_FLAGS (TREE_OPERAND (node, 3));
3835 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
3836 FIELD_DECL, apparently. The G++ front end can put something else
3837 there, at least temporarily. */
3838 else if (TREE_CODE (node) == COMPONENT_REF
3839 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
3841 if (TREE_OPERAND (node, 2))
3842 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3846 node = lang_hooks.expr_to_decl (node, &tc, &se);
3848 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
3849 the address, since &(*a)->b is a form of addition. If it's a constant, the
3850 address is constant too. If it's a decl, its address is constant if the
3851 decl is static. Everything else is not constant and, furthermore,
3852 taking the address of a volatile variable is not volatile. */
3853 if (TREE_CODE (node) == INDIRECT_REF
3854 || TREE_CODE (node) == MEM_REF)
3855 UPDATE_FLAGS (TREE_OPERAND (node, 0));
3856 else if (CONSTANT_CLASS_P (node))
3858 else if (DECL_P (node))
3859 tc &= (staticp (node) != NULL_TREE);
3860 else
3862 tc = false;
3863 se |= TREE_SIDE_EFFECTS (node);
3867 TREE_CONSTANT (t) = tc;
3868 TREE_SIDE_EFFECTS (t) = se;
3869 #undef UPDATE_FLAGS
3872 /* Build an expression of code CODE, data type TYPE, and operands as
3873 specified. Expressions and reference nodes can be created this way.
3874 Constants, decls, types and misc nodes cannot be.
3876 We define 5 non-variadic functions, from 0 to 4 arguments. This is
3877 enough for all extant tree codes. */
3879 tree
3880 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
3882 tree t;
3884 gcc_assert (TREE_CODE_LENGTH (code) == 0);
3886 t = make_node_stat (code PASS_MEM_STAT);
3887 TREE_TYPE (t) = tt;
3889 return t;
3892 tree
3893 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
3895 int length = sizeof (struct tree_exp);
3896 tree t;
3898 record_node_allocation_statistics (code, length);
3900 gcc_assert (TREE_CODE_LENGTH (code) == 1);
3902 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
3904 memset (t, 0, sizeof (struct tree_common));
3906 TREE_SET_CODE (t, code);
3908 TREE_TYPE (t) = type;
3909 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
3910 TREE_OPERAND (t, 0) = node;
3911 if (node && !TYPE_P (node))
3913 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
3914 TREE_READONLY (t) = TREE_READONLY (node);
3917 if (TREE_CODE_CLASS (code) == tcc_statement)
3918 TREE_SIDE_EFFECTS (t) = 1;
3919 else switch (code)
3921 case VA_ARG_EXPR:
3922 /* All of these have side-effects, no matter what their
3923 operands are. */
3924 TREE_SIDE_EFFECTS (t) = 1;
3925 TREE_READONLY (t) = 0;
3926 break;
3928 case INDIRECT_REF:
3929 /* Whether a dereference is readonly has nothing to do with whether
3930 its operand is readonly. */
3931 TREE_READONLY (t) = 0;
3932 break;
3934 case ADDR_EXPR:
3935 if (node)
3936 recompute_tree_invariant_for_addr_expr (t);
3937 break;
3939 default:
3940 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
3941 && node && !TYPE_P (node)
3942 && TREE_CONSTANT (node))
3943 TREE_CONSTANT (t) = 1;
3944 if (TREE_CODE_CLASS (code) == tcc_reference
3945 && node && TREE_THIS_VOLATILE (node))
3946 TREE_THIS_VOLATILE (t) = 1;
3947 break;
3950 return t;
3953 #define PROCESS_ARG(N) \
3954 do { \
3955 TREE_OPERAND (t, N) = arg##N; \
3956 if (arg##N &&!TYPE_P (arg##N)) \
3958 if (TREE_SIDE_EFFECTS (arg##N)) \
3959 side_effects = 1; \
3960 if (!TREE_READONLY (arg##N) \
3961 && !CONSTANT_CLASS_P (arg##N)) \
3962 (void) (read_only = 0); \
3963 if (!TREE_CONSTANT (arg##N)) \
3964 (void) (constant = 0); \
3966 } while (0)
3968 tree
3969 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
3971 bool constant, read_only, side_effects;
3972 tree t;
3974 gcc_assert (TREE_CODE_LENGTH (code) == 2);
3976 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
3977 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
3978 /* When sizetype precision doesn't match that of pointers
3979 we need to be able to build explicit extensions or truncations
3980 of the offset argument. */
3981 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
3982 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
3983 && TREE_CODE (arg1) == INTEGER_CST);
3985 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
3986 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
3987 && ptrofftype_p (TREE_TYPE (arg1)));
3989 t = make_node_stat (code PASS_MEM_STAT);
3990 TREE_TYPE (t) = tt;
3992 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
3993 result based on those same flags for the arguments. But if the
3994 arguments aren't really even `tree' expressions, we shouldn't be trying
3995 to do this. */
3997 /* Expressions without side effects may be constant if their
3998 arguments are as well. */
3999 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4000 || TREE_CODE_CLASS (code) == tcc_binary);
4001 read_only = 1;
4002 side_effects = TREE_SIDE_EFFECTS (t);
4004 PROCESS_ARG(0);
4005 PROCESS_ARG(1);
4007 TREE_READONLY (t) = read_only;
4008 TREE_CONSTANT (t) = constant;
4009 TREE_SIDE_EFFECTS (t) = side_effects;
4010 TREE_THIS_VOLATILE (t)
4011 = (TREE_CODE_CLASS (code) == tcc_reference
4012 && arg0 && TREE_THIS_VOLATILE (arg0));
4014 return t;
4018 tree
4019 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4020 tree arg2 MEM_STAT_DECL)
4022 bool constant, read_only, side_effects;
4023 tree t;
4025 gcc_assert (TREE_CODE_LENGTH (code) == 3);
4026 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4028 t = make_node_stat (code PASS_MEM_STAT);
4029 TREE_TYPE (t) = tt;
4031 read_only = 1;
4033 /* As a special exception, if COND_EXPR has NULL branches, we
4034 assume that it is a gimple statement and always consider
4035 it to have side effects. */
4036 if (code == COND_EXPR
4037 && tt == void_type_node
4038 && arg1 == NULL_TREE
4039 && arg2 == NULL_TREE)
4040 side_effects = true;
4041 else
4042 side_effects = TREE_SIDE_EFFECTS (t);
4044 PROCESS_ARG(0);
4045 PROCESS_ARG(1);
4046 PROCESS_ARG(2);
4048 if (code == COND_EXPR)
4049 TREE_READONLY (t) = read_only;
4051 TREE_SIDE_EFFECTS (t) = side_effects;
4052 TREE_THIS_VOLATILE (t)
4053 = (TREE_CODE_CLASS (code) == tcc_reference
4054 && arg0 && TREE_THIS_VOLATILE (arg0));
4056 return t;
4059 tree
4060 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4061 tree arg2, tree arg3 MEM_STAT_DECL)
4063 bool constant, read_only, side_effects;
4064 tree t;
4066 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4068 t = make_node_stat (code PASS_MEM_STAT);
4069 TREE_TYPE (t) = tt;
4071 side_effects = TREE_SIDE_EFFECTS (t);
4073 PROCESS_ARG(0);
4074 PROCESS_ARG(1);
4075 PROCESS_ARG(2);
4076 PROCESS_ARG(3);
4078 TREE_SIDE_EFFECTS (t) = side_effects;
4079 TREE_THIS_VOLATILE (t)
4080 = (TREE_CODE_CLASS (code) == tcc_reference
4081 && arg0 && TREE_THIS_VOLATILE (arg0));
4083 return t;
4086 tree
4087 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4088 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4090 bool constant, read_only, side_effects;
4091 tree t;
4093 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4095 t = make_node_stat (code PASS_MEM_STAT);
4096 TREE_TYPE (t) = tt;
4098 side_effects = TREE_SIDE_EFFECTS (t);
4100 PROCESS_ARG(0);
4101 PROCESS_ARG(1);
4102 PROCESS_ARG(2);
4103 PROCESS_ARG(3);
4104 PROCESS_ARG(4);
4106 TREE_SIDE_EFFECTS (t) = side_effects;
4107 TREE_THIS_VOLATILE (t)
4108 = (TREE_CODE_CLASS (code) == tcc_reference
4109 && arg0 && TREE_THIS_VOLATILE (arg0));
4111 return t;
4114 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4115 on the pointer PTR. */
4117 tree
4118 build_simple_mem_ref_loc (location_t loc, tree ptr)
4120 HOST_WIDE_INT offset = 0;
4121 tree ptype = TREE_TYPE (ptr);
4122 tree tem;
4123 /* For convenience allow addresses that collapse to a simple base
4124 and offset. */
4125 if (TREE_CODE (ptr) == ADDR_EXPR
4126 && (handled_component_p (TREE_OPERAND (ptr, 0))
4127 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4129 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4130 gcc_assert (ptr);
4131 ptr = build_fold_addr_expr (ptr);
4132 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4134 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4135 ptr, build_int_cst (ptype, offset));
4136 SET_EXPR_LOCATION (tem, loc);
4137 return tem;
4140 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4142 double_int
4143 mem_ref_offset (const_tree t)
4145 tree toff = TREE_OPERAND (t, 1);
4146 return tree_to_double_int (toff).sext (TYPE_PRECISION (TREE_TYPE (toff)));
4149 /* Return the pointer-type relevant for TBAA purposes from the
4150 gimple memory reference tree T. This is the type to be used for
4151 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T. */
4153 tree
4154 reference_alias_ptr_type (const_tree t)
4156 const_tree base = t;
4157 while (handled_component_p (base))
4158 base = TREE_OPERAND (base, 0);
4159 if (TREE_CODE (base) == MEM_REF)
4160 return TREE_TYPE (TREE_OPERAND (base, 1));
4161 else if (TREE_CODE (base) == TARGET_MEM_REF)
4162 return TREE_TYPE (TMR_OFFSET (base));
4163 else
4164 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (base)));
4167 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4168 offsetted by OFFSET units. */
4170 tree
4171 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4173 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4174 build_fold_addr_expr (base),
4175 build_int_cst (ptr_type_node, offset));
4176 tree addr = build1 (ADDR_EXPR, type, ref);
4177 recompute_tree_invariant_for_addr_expr (addr);
4178 return addr;
4181 /* Similar except don't specify the TREE_TYPE
4182 and leave the TREE_SIDE_EFFECTS as 0.
4183 It is permissible for arguments to be null,
4184 or even garbage if their values do not matter. */
4186 tree
4187 build_nt (enum tree_code code, ...)
4189 tree t;
4190 int length;
4191 int i;
4192 va_list p;
4194 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4196 va_start (p, code);
4198 t = make_node (code);
4199 length = TREE_CODE_LENGTH (code);
4201 for (i = 0; i < length; i++)
4202 TREE_OPERAND (t, i) = va_arg (p, tree);
4204 va_end (p);
4205 return t;
4208 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4209 tree vec. */
4211 tree
4212 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4214 tree ret, t;
4215 unsigned int ix;
4217 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4218 CALL_EXPR_FN (ret) = fn;
4219 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4220 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4221 CALL_EXPR_ARG (ret, ix) = t;
4222 return ret;
4225 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4226 We do NOT enter this node in any sort of symbol table.
4228 LOC is the location of the decl.
4230 layout_decl is used to set up the decl's storage layout.
4231 Other slots are initialized to 0 or null pointers. */
4233 tree
4234 build_decl_stat (location_t loc, enum tree_code code, tree name,
4235 tree type MEM_STAT_DECL)
4237 tree t;
4239 t = make_node_stat (code PASS_MEM_STAT);
4240 DECL_SOURCE_LOCATION (t) = loc;
4242 /* if (type == error_mark_node)
4243 type = integer_type_node; */
4244 /* That is not done, deliberately, so that having error_mark_node
4245 as the type can suppress useless errors in the use of this variable. */
4247 DECL_NAME (t) = name;
4248 TREE_TYPE (t) = type;
4250 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4251 layout_decl (t, 0);
4253 return t;
4256 /* Builds and returns function declaration with NAME and TYPE. */
4258 tree
4259 build_fn_decl (const char *name, tree type)
4261 tree id = get_identifier (name);
4262 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4264 DECL_EXTERNAL (decl) = 1;
4265 TREE_PUBLIC (decl) = 1;
4266 DECL_ARTIFICIAL (decl) = 1;
4267 TREE_NOTHROW (decl) = 1;
4269 return decl;
4272 vec<tree, va_gc> *all_translation_units;
4274 /* Builds a new translation-unit decl with name NAME, queues it in the
4275 global list of translation-unit decls and returns it. */
4277 tree
4278 build_translation_unit_decl (tree name)
4280 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4281 name, NULL_TREE);
4282 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4283 vec_safe_push (all_translation_units, tu);
4284 return tu;
4288 /* BLOCK nodes are used to represent the structure of binding contours
4289 and declarations, once those contours have been exited and their contents
4290 compiled. This information is used for outputting debugging info. */
4292 tree
4293 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4295 tree block = make_node (BLOCK);
4297 BLOCK_VARS (block) = vars;
4298 BLOCK_SUBBLOCKS (block) = subblocks;
4299 BLOCK_SUPERCONTEXT (block) = supercontext;
4300 BLOCK_CHAIN (block) = chain;
4301 return block;
4305 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4307 LOC is the location to use in tree T. */
4309 void
4310 protected_set_expr_location (tree t, location_t loc)
4312 if (t && CAN_HAVE_LOCATION_P (t))
4313 SET_EXPR_LOCATION (t, loc);
4316 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4317 is ATTRIBUTE. */
4319 tree
4320 build_decl_attribute_variant (tree ddecl, tree attribute)
4322 DECL_ATTRIBUTES (ddecl) = attribute;
4323 return ddecl;
4326 /* Borrowed from hashtab.c iterative_hash implementation. */
4327 #define mix(a,b,c) \
4329 a -= b; a -= c; a ^= (c>>13); \
4330 b -= c; b -= a; b ^= (a<< 8); \
4331 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
4332 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
4333 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
4334 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
4335 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
4336 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
4337 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
4341 /* Produce good hash value combining VAL and VAL2. */
4342 hashval_t
4343 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
4345 /* the golden ratio; an arbitrary value. */
4346 hashval_t a = 0x9e3779b9;
4348 mix (a, val, val2);
4349 return val2;
4352 /* Produce good hash value combining VAL and VAL2. */
4353 hashval_t
4354 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
4356 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
4357 return iterative_hash_hashval_t (val, val2);
4358 else
4360 hashval_t a = (hashval_t) val;
4361 /* Avoid warnings about shifting of more than the width of the type on
4362 hosts that won't execute this path. */
4363 int zero = 0;
4364 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
4365 mix (a, b, val2);
4366 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
4368 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
4369 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
4370 mix (a, b, val2);
4372 return val2;
4376 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4377 is ATTRIBUTE and its qualifiers are QUALS.
4379 Record such modified types already made so we don't make duplicates. */
4381 tree
4382 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4384 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4386 hashval_t hashcode = 0;
4387 tree ntype;
4388 enum tree_code code = TREE_CODE (ttype);
4390 /* Building a distinct copy of a tagged type is inappropriate; it
4391 causes breakage in code that expects there to be a one-to-one
4392 relationship between a struct and its fields.
4393 build_duplicate_type is another solution (as used in
4394 handle_transparent_union_attribute), but that doesn't play well
4395 with the stronger C++ type identity model. */
4396 if (TREE_CODE (ttype) == RECORD_TYPE
4397 || TREE_CODE (ttype) == UNION_TYPE
4398 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4399 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4401 warning (OPT_Wattributes,
4402 "ignoring attributes applied to %qT after definition",
4403 TYPE_MAIN_VARIANT (ttype));
4404 return build_qualified_type (ttype, quals);
4407 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4408 ntype = build_distinct_type_copy (ttype);
4410 TYPE_ATTRIBUTES (ntype) = attribute;
4412 hashcode = iterative_hash_object (code, hashcode);
4413 if (TREE_TYPE (ntype))
4414 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
4415 hashcode);
4416 hashcode = attribute_hash_list (attribute, hashcode);
4418 switch (TREE_CODE (ntype))
4420 case FUNCTION_TYPE:
4421 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
4422 break;
4423 case ARRAY_TYPE:
4424 if (TYPE_DOMAIN (ntype))
4425 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
4426 hashcode);
4427 break;
4428 case INTEGER_TYPE:
4429 hashcode = iterative_hash_object
4430 (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
4431 hashcode = iterative_hash_object
4432 (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
4433 break;
4434 case REAL_TYPE:
4435 case FIXED_POINT_TYPE:
4437 unsigned int precision = TYPE_PRECISION (ntype);
4438 hashcode = iterative_hash_object (precision, hashcode);
4440 break;
4441 default:
4442 break;
4445 ntype = type_hash_canon (hashcode, ntype);
4447 /* If the target-dependent attributes make NTYPE different from
4448 its canonical type, we will need to use structural equality
4449 checks for this type. */
4450 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4451 || !comp_type_attributes (ntype, ttype))
4452 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4453 else if (TYPE_CANONICAL (ntype) == ntype)
4454 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4456 ttype = build_qualified_type (ntype, quals);
4458 else if (TYPE_QUALS (ttype) != quals)
4459 ttype = build_qualified_type (ttype, quals);
4461 return ttype;
4464 /* Compare two attributes for their value identity. Return true if the
4465 attribute values are known to be equal; otherwise return false.
4468 static bool
4469 attribute_value_equal (const_tree attr1, const_tree attr2)
4471 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4472 return true;
4474 if (TREE_VALUE (attr1) != NULL_TREE
4475 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4476 && TREE_VALUE (attr2) != NULL
4477 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4478 return (simple_cst_list_equal (TREE_VALUE (attr1),
4479 TREE_VALUE (attr2)) == 1);
4481 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4484 /* Return 0 if the attributes for two types are incompatible, 1 if they
4485 are compatible, and 2 if they are nearly compatible (which causes a
4486 warning to be generated). */
4488 comp_type_attributes (const_tree type1, const_tree type2)
4490 const_tree a1 = TYPE_ATTRIBUTES (type1);
4491 const_tree a2 = TYPE_ATTRIBUTES (type2);
4492 const_tree a;
4494 if (a1 == a2)
4495 return 1;
4496 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4498 const struct attribute_spec *as;
4499 const_tree attr;
4501 as = lookup_attribute_spec (get_attribute_name (a));
4502 if (!as || as->affects_type_identity == false)
4503 continue;
4505 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4506 if (!attr || !attribute_value_equal (a, attr))
4507 break;
4509 if (!a)
4511 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
4513 const struct attribute_spec *as;
4515 as = lookup_attribute_spec (get_attribute_name (a));
4516 if (!as || as->affects_type_identity == false)
4517 continue;
4519 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
4520 break;
4521 /* We don't need to compare trees again, as we did this
4522 already in first loop. */
4524 /* All types - affecting identity - are equal, so
4525 there is no need to call target hook for comparison. */
4526 if (!a)
4527 return 1;
4529 /* As some type combinations - like default calling-convention - might
4530 be compatible, we have to call the target hook to get the final result. */
4531 return targetm.comp_type_attributes (type1, type2);
4534 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4535 is ATTRIBUTE.
4537 Record such modified types already made so we don't make duplicates. */
4539 tree
4540 build_type_attribute_variant (tree ttype, tree attribute)
4542 return build_type_attribute_qual_variant (ttype, attribute,
4543 TYPE_QUALS (ttype));
4547 /* Reset the expression *EXPR_P, a size or position.
4549 ??? We could reset all non-constant sizes or positions. But it's cheap
4550 enough to not do so and refrain from adding workarounds to dwarf2out.c.
4552 We need to reset self-referential sizes or positions because they cannot
4553 be gimplified and thus can contain a CALL_EXPR after the gimplification
4554 is finished, which will run afoul of LTO streaming. And they need to be
4555 reset to something essentially dummy but not constant, so as to preserve
4556 the properties of the object they are attached to. */
4558 static inline void
4559 free_lang_data_in_one_sizepos (tree *expr_p)
4561 tree expr = *expr_p;
4562 if (CONTAINS_PLACEHOLDER_P (expr))
4563 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
4567 /* Reset all the fields in a binfo node BINFO. We only keep
4568 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
4570 static void
4571 free_lang_data_in_binfo (tree binfo)
4573 unsigned i;
4574 tree t;
4576 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
4578 BINFO_VIRTUALS (binfo) = NULL_TREE;
4579 BINFO_BASE_ACCESSES (binfo) = NULL;
4580 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
4581 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
4583 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
4584 free_lang_data_in_binfo (t);
4588 /* Reset all language specific information still present in TYPE. */
4590 static void
4591 free_lang_data_in_type (tree type)
4593 gcc_assert (TYPE_P (type));
4595 /* Give the FE a chance to remove its own data first. */
4596 lang_hooks.free_lang_data (type);
4598 TREE_LANG_FLAG_0 (type) = 0;
4599 TREE_LANG_FLAG_1 (type) = 0;
4600 TREE_LANG_FLAG_2 (type) = 0;
4601 TREE_LANG_FLAG_3 (type) = 0;
4602 TREE_LANG_FLAG_4 (type) = 0;
4603 TREE_LANG_FLAG_5 (type) = 0;
4604 TREE_LANG_FLAG_6 (type) = 0;
4606 if (TREE_CODE (type) == FUNCTION_TYPE)
4608 /* Remove the const and volatile qualifiers from arguments. The
4609 C++ front end removes them, but the C front end does not,
4610 leading to false ODR violation errors when merging two
4611 instances of the same function signature compiled by
4612 different front ends. */
4613 tree p;
4615 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
4617 tree arg_type = TREE_VALUE (p);
4619 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
4621 int quals = TYPE_QUALS (arg_type)
4622 & ~TYPE_QUAL_CONST
4623 & ~TYPE_QUAL_VOLATILE;
4624 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
4625 free_lang_data_in_type (TREE_VALUE (p));
4630 /* Remove members that are not actually FIELD_DECLs from the field
4631 list of an aggregate. These occur in C++. */
4632 if (RECORD_OR_UNION_TYPE_P (type))
4634 tree prev, member;
4636 /* Note that TYPE_FIELDS can be shared across distinct
4637 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
4638 to be removed, we cannot set its TREE_CHAIN to NULL.
4639 Otherwise, we would not be able to find all the other fields
4640 in the other instances of this TREE_TYPE.
4642 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
4643 prev = NULL_TREE;
4644 member = TYPE_FIELDS (type);
4645 while (member)
4647 if (TREE_CODE (member) == FIELD_DECL
4648 || TREE_CODE (member) == TYPE_DECL)
4650 if (prev)
4651 TREE_CHAIN (prev) = member;
4652 else
4653 TYPE_FIELDS (type) = member;
4654 prev = member;
4657 member = TREE_CHAIN (member);
4660 if (prev)
4661 TREE_CHAIN (prev) = NULL_TREE;
4662 else
4663 TYPE_FIELDS (type) = NULL_TREE;
4665 TYPE_METHODS (type) = NULL_TREE;
4666 if (TYPE_BINFO (type))
4667 free_lang_data_in_binfo (TYPE_BINFO (type));
4669 else
4671 /* For non-aggregate types, clear out the language slot (which
4672 overloads TYPE_BINFO). */
4673 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
4675 if (INTEGRAL_TYPE_P (type)
4676 || SCALAR_FLOAT_TYPE_P (type)
4677 || FIXED_POINT_TYPE_P (type))
4679 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
4680 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
4684 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
4685 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
4687 if (TYPE_CONTEXT (type)
4688 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
4690 tree ctx = TYPE_CONTEXT (type);
4693 ctx = BLOCK_SUPERCONTEXT (ctx);
4695 while (ctx && TREE_CODE (ctx) == BLOCK);
4696 TYPE_CONTEXT (type) = ctx;
4701 /* Return true if DECL may need an assembler name to be set. */
4703 static inline bool
4704 need_assembler_name_p (tree decl)
4706 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
4707 if (TREE_CODE (decl) != FUNCTION_DECL
4708 && TREE_CODE (decl) != VAR_DECL)
4709 return false;
4711 /* If DECL already has its assembler name set, it does not need a
4712 new one. */
4713 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
4714 || DECL_ASSEMBLER_NAME_SET_P (decl))
4715 return false;
4717 /* Abstract decls do not need an assembler name. */
4718 if (DECL_ABSTRACT (decl))
4719 return false;
4721 /* For VAR_DECLs, only static, public and external symbols need an
4722 assembler name. */
4723 if (TREE_CODE (decl) == VAR_DECL
4724 && !TREE_STATIC (decl)
4725 && !TREE_PUBLIC (decl)
4726 && !DECL_EXTERNAL (decl))
4727 return false;
4729 if (TREE_CODE (decl) == FUNCTION_DECL)
4731 /* Do not set assembler name on builtins. Allow RTL expansion to
4732 decide whether to expand inline or via a regular call. */
4733 if (DECL_BUILT_IN (decl)
4734 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
4735 return false;
4737 /* Functions represented in the callgraph need an assembler name. */
4738 if (cgraph_get_node (decl) != NULL)
4739 return true;
4741 /* Unused and not public functions don't need an assembler name. */
4742 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
4743 return false;
4746 return true;
4750 /* Reset all language specific information still present in symbol
4751 DECL. */
4753 static void
4754 free_lang_data_in_decl (tree decl)
4756 gcc_assert (DECL_P (decl));
4758 /* Give the FE a chance to remove its own data first. */
4759 lang_hooks.free_lang_data (decl);
4761 TREE_LANG_FLAG_0 (decl) = 0;
4762 TREE_LANG_FLAG_1 (decl) = 0;
4763 TREE_LANG_FLAG_2 (decl) = 0;
4764 TREE_LANG_FLAG_3 (decl) = 0;
4765 TREE_LANG_FLAG_4 (decl) = 0;
4766 TREE_LANG_FLAG_5 (decl) = 0;
4767 TREE_LANG_FLAG_6 (decl) = 0;
4769 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
4770 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
4771 if (TREE_CODE (decl) == FIELD_DECL)
4773 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
4774 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
4775 DECL_QUALIFIER (decl) = NULL_TREE;
4778 if (TREE_CODE (decl) == FUNCTION_DECL)
4780 if (gimple_has_body_p (decl))
4782 tree t;
4784 /* If DECL has a gimple body, then the context for its
4785 arguments must be DECL. Otherwise, it doesn't really
4786 matter, as we will not be emitting any code for DECL. In
4787 general, there may be other instances of DECL created by
4788 the front end and since PARM_DECLs are generally shared,
4789 their DECL_CONTEXT changes as the replicas of DECL are
4790 created. The only time where DECL_CONTEXT is important
4791 is for the FUNCTION_DECLs that have a gimple body (since
4792 the PARM_DECL will be used in the function's body). */
4793 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
4794 DECL_CONTEXT (t) = decl;
4797 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
4798 At this point, it is not needed anymore. */
4799 DECL_SAVED_TREE (decl) = NULL_TREE;
4801 /* Clear the abstract origin if it refers to a method. Otherwise
4802 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
4803 origin will not be output correctly. */
4804 if (DECL_ABSTRACT_ORIGIN (decl)
4805 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
4806 && RECORD_OR_UNION_TYPE_P
4807 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
4808 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
4810 /* Sometimes the C++ frontend doesn't manage to transform a temporary
4811 DECL_VINDEX referring to itself into a vtable slot number as it
4812 should. Happens with functions that are copied and then forgotten
4813 about. Just clear it, it won't matter anymore. */
4814 if (DECL_VINDEX (decl) && !host_integerp (DECL_VINDEX (decl), 0))
4815 DECL_VINDEX (decl) = NULL_TREE;
4817 else if (TREE_CODE (decl) == VAR_DECL)
4819 if ((DECL_EXTERNAL (decl)
4820 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
4821 || (decl_function_context (decl) && !TREE_STATIC (decl)))
4822 DECL_INITIAL (decl) = NULL_TREE;
4824 else if (TREE_CODE (decl) == TYPE_DECL
4825 || TREE_CODE (decl) == FIELD_DECL)
4826 DECL_INITIAL (decl) = NULL_TREE;
4827 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
4828 && DECL_INITIAL (decl)
4829 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
4831 /* Strip builtins from the translation-unit BLOCK. We still have targets
4832 without builtin_decl_explicit support and also builtins are shared
4833 nodes and thus we can't use TREE_CHAIN in multiple lists. */
4834 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
4835 while (*nextp)
4837 tree var = *nextp;
4838 if (TREE_CODE (var) == FUNCTION_DECL
4839 && DECL_BUILT_IN (var))
4840 *nextp = TREE_CHAIN (var);
4841 else
4842 nextp = &TREE_CHAIN (var);
4848 /* Data used when collecting DECLs and TYPEs for language data removal. */
4850 struct free_lang_data_d
4852 /* Worklist to avoid excessive recursion. */
4853 vec<tree> worklist;
4855 /* Set of traversed objects. Used to avoid duplicate visits. */
4856 struct pointer_set_t *pset;
4858 /* Array of symbols to process with free_lang_data_in_decl. */
4859 vec<tree> decls;
4861 /* Array of types to process with free_lang_data_in_type. */
4862 vec<tree> types;
4866 /* Save all language fields needed to generate proper debug information
4867 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
4869 static void
4870 save_debug_info_for_decl (tree t)
4872 /*struct saved_debug_info_d *sdi;*/
4874 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
4876 /* FIXME. Partial implementation for saving debug info removed. */
4880 /* Save all language fields needed to generate proper debug information
4881 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
4883 static void
4884 save_debug_info_for_type (tree t)
4886 /*struct saved_debug_info_d *sdi;*/
4888 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
4890 /* FIXME. Partial implementation for saving debug info removed. */
4894 /* Add type or decl T to one of the list of tree nodes that need their
4895 language data removed. The lists are held inside FLD. */
4897 static void
4898 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
4900 if (DECL_P (t))
4902 fld->decls.safe_push (t);
4903 if (debug_info_level > DINFO_LEVEL_TERSE)
4904 save_debug_info_for_decl (t);
4906 else if (TYPE_P (t))
4908 fld->types.safe_push (t);
4909 if (debug_info_level > DINFO_LEVEL_TERSE)
4910 save_debug_info_for_type (t);
4912 else
4913 gcc_unreachable ();
4916 /* Push tree node T into FLD->WORKLIST. */
4918 static inline void
4919 fld_worklist_push (tree t, struct free_lang_data_d *fld)
4921 if (t && !is_lang_specific (t) && !pointer_set_contains (fld->pset, t))
4922 fld->worklist.safe_push ((t));
4926 /* Operand callback helper for free_lang_data_in_node. *TP is the
4927 subtree operand being considered. */
4929 static tree
4930 find_decls_types_r (tree *tp, int *ws, void *data)
4932 tree t = *tp;
4933 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
4935 if (TREE_CODE (t) == TREE_LIST)
4936 return NULL_TREE;
4938 /* Language specific nodes will be removed, so there is no need
4939 to gather anything under them. */
4940 if (is_lang_specific (t))
4942 *ws = 0;
4943 return NULL_TREE;
4946 if (DECL_P (t))
4948 /* Note that walk_tree does not traverse every possible field in
4949 decls, so we have to do our own traversals here. */
4950 add_tree_to_fld_list (t, fld);
4952 fld_worklist_push (DECL_NAME (t), fld);
4953 fld_worklist_push (DECL_CONTEXT (t), fld);
4954 fld_worklist_push (DECL_SIZE (t), fld);
4955 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
4957 /* We are going to remove everything under DECL_INITIAL for
4958 TYPE_DECLs. No point walking them. */
4959 if (TREE_CODE (t) != TYPE_DECL)
4960 fld_worklist_push (DECL_INITIAL (t), fld);
4962 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
4963 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
4965 if (TREE_CODE (t) == FUNCTION_DECL)
4967 fld_worklist_push (DECL_ARGUMENTS (t), fld);
4968 fld_worklist_push (DECL_RESULT (t), fld);
4970 else if (TREE_CODE (t) == TYPE_DECL)
4972 fld_worklist_push (DECL_ARGUMENT_FLD (t), fld);
4973 fld_worklist_push (DECL_VINDEX (t), fld);
4974 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
4976 else if (TREE_CODE (t) == FIELD_DECL)
4978 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
4979 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
4980 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
4981 fld_worklist_push (DECL_FCONTEXT (t), fld);
4983 else if (TREE_CODE (t) == VAR_DECL)
4985 fld_worklist_push (DECL_SECTION_NAME (t), fld);
4986 fld_worklist_push (DECL_COMDAT_GROUP (t), fld);
4989 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
4990 && DECL_HAS_VALUE_EXPR_P (t))
4991 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
4993 if (TREE_CODE (t) != FIELD_DECL
4994 && TREE_CODE (t) != TYPE_DECL)
4995 fld_worklist_push (TREE_CHAIN (t), fld);
4996 *ws = 0;
4998 else if (TYPE_P (t))
5000 /* Note that walk_tree does not traverse every possible field in
5001 types, so we have to do our own traversals here. */
5002 add_tree_to_fld_list (t, fld);
5004 if (!RECORD_OR_UNION_TYPE_P (t))
5005 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
5006 fld_worklist_push (TYPE_SIZE (t), fld);
5007 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
5008 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
5009 fld_worklist_push (TYPE_POINTER_TO (t), fld);
5010 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
5011 fld_worklist_push (TYPE_NAME (t), fld);
5012 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
5013 them and thus do not and want not to reach unused pointer types
5014 this way. */
5015 if (!POINTER_TYPE_P (t))
5016 fld_worklist_push (TYPE_MINVAL (t), fld);
5017 if (!RECORD_OR_UNION_TYPE_P (t))
5018 fld_worklist_push (TYPE_MAXVAL (t), fld);
5019 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
5020 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
5021 do not and want not to reach unused variants this way. */
5022 if (TYPE_CONTEXT (t))
5024 tree ctx = TYPE_CONTEXT (t);
5025 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
5026 So push that instead. */
5027 while (ctx && TREE_CODE (ctx) == BLOCK)
5028 ctx = BLOCK_SUPERCONTEXT (ctx);
5029 fld_worklist_push (ctx, fld);
5031 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
5032 and want not to reach unused types this way. */
5034 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
5036 unsigned i;
5037 tree tem;
5038 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
5039 fld_worklist_push (TREE_TYPE (tem), fld);
5040 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
5041 if (tem
5042 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
5043 && TREE_CODE (tem) == TREE_LIST)
5046 fld_worklist_push (TREE_VALUE (tem), fld);
5047 tem = TREE_CHAIN (tem);
5049 while (tem);
5051 if (RECORD_OR_UNION_TYPE_P (t))
5053 tree tem;
5054 /* Push all TYPE_FIELDS - there can be interleaving interesting
5055 and non-interesting things. */
5056 tem = TYPE_FIELDS (t);
5057 while (tem)
5059 if (TREE_CODE (tem) == FIELD_DECL
5060 || TREE_CODE (tem) == TYPE_DECL)
5061 fld_worklist_push (tem, fld);
5062 tem = TREE_CHAIN (tem);
5066 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5067 *ws = 0;
5069 else if (TREE_CODE (t) == BLOCK)
5071 tree tem;
5072 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5073 fld_worklist_push (tem, fld);
5074 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5075 fld_worklist_push (tem, fld);
5076 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5079 if (TREE_CODE (t) != IDENTIFIER_NODE
5080 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5081 fld_worklist_push (TREE_TYPE (t), fld);
5083 return NULL_TREE;
5087 /* Find decls and types in T. */
5089 static void
5090 find_decls_types (tree t, struct free_lang_data_d *fld)
5092 while (1)
5094 if (!pointer_set_contains (fld->pset, t))
5095 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5096 if (fld->worklist.is_empty ())
5097 break;
5098 t = fld->worklist.pop ();
5102 /* Translate all the types in LIST with the corresponding runtime
5103 types. */
5105 static tree
5106 get_eh_types_for_runtime (tree list)
5108 tree head, prev;
5110 if (list == NULL_TREE)
5111 return NULL_TREE;
5113 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5114 prev = head;
5115 list = TREE_CHAIN (list);
5116 while (list)
5118 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5119 TREE_CHAIN (prev) = n;
5120 prev = TREE_CHAIN (prev);
5121 list = TREE_CHAIN (list);
5124 return head;
5128 /* Find decls and types referenced in EH region R and store them in
5129 FLD->DECLS and FLD->TYPES. */
5131 static void
5132 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5134 switch (r->type)
5136 case ERT_CLEANUP:
5137 break;
5139 case ERT_TRY:
5141 eh_catch c;
5143 /* The types referenced in each catch must first be changed to the
5144 EH types used at runtime. This removes references to FE types
5145 in the region. */
5146 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5148 c->type_list = get_eh_types_for_runtime (c->type_list);
5149 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5152 break;
5154 case ERT_ALLOWED_EXCEPTIONS:
5155 r->u.allowed.type_list
5156 = get_eh_types_for_runtime (r->u.allowed.type_list);
5157 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5158 break;
5160 case ERT_MUST_NOT_THROW:
5161 walk_tree (&r->u.must_not_throw.failure_decl,
5162 find_decls_types_r, fld, fld->pset);
5163 break;
5168 /* Find decls and types referenced in cgraph node N and store them in
5169 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5170 look for *every* kind of DECL and TYPE node reachable from N,
5171 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5172 NAMESPACE_DECLs, etc). */
5174 static void
5175 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5177 basic_block bb;
5178 struct function *fn;
5179 unsigned ix;
5180 tree t;
5182 find_decls_types (n->symbol.decl, fld);
5184 if (!gimple_has_body_p (n->symbol.decl))
5185 return;
5187 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5189 fn = DECL_STRUCT_FUNCTION (n->symbol.decl);
5191 /* Traverse locals. */
5192 FOR_EACH_LOCAL_DECL (fn, ix, t)
5193 find_decls_types (t, fld);
5195 /* Traverse EH regions in FN. */
5197 eh_region r;
5198 FOR_ALL_EH_REGION_FN (r, fn)
5199 find_decls_types_in_eh_region (r, fld);
5202 /* Traverse every statement in FN. */
5203 FOR_EACH_BB_FN (bb, fn)
5205 gimple_stmt_iterator si;
5206 unsigned i;
5208 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5210 gimple phi = gsi_stmt (si);
5212 for (i = 0; i < gimple_phi_num_args (phi); i++)
5214 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5215 find_decls_types (*arg_p, fld);
5219 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5221 gimple stmt = gsi_stmt (si);
5223 if (is_gimple_call (stmt))
5224 find_decls_types (gimple_call_fntype (stmt), fld);
5226 for (i = 0; i < gimple_num_ops (stmt); i++)
5228 tree arg = gimple_op (stmt, i);
5229 find_decls_types (arg, fld);
5236 /* Find decls and types referenced in varpool node N and store them in
5237 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5238 look for *every* kind of DECL and TYPE node reachable from N,
5239 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5240 NAMESPACE_DECLs, etc). */
5242 static void
5243 find_decls_types_in_var (struct varpool_node *v, struct free_lang_data_d *fld)
5245 find_decls_types (v->symbol.decl, fld);
5248 /* If T needs an assembler name, have one created for it. */
5250 void
5251 assign_assembler_name_if_neeeded (tree t)
5253 if (need_assembler_name_p (t))
5255 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5256 diagnostics that use input_location to show locus
5257 information. The problem here is that, at this point,
5258 input_location is generally anchored to the end of the file
5259 (since the parser is long gone), so we don't have a good
5260 position to pin it to.
5262 To alleviate this problem, this uses the location of T's
5263 declaration. Examples of this are
5264 testsuite/g++.dg/template/cond2.C and
5265 testsuite/g++.dg/template/pr35240.C. */
5266 location_t saved_location = input_location;
5267 input_location = DECL_SOURCE_LOCATION (t);
5269 decl_assembler_name (t);
5271 input_location = saved_location;
5276 /* Free language specific information for every operand and expression
5277 in every node of the call graph. This process operates in three stages:
5279 1- Every callgraph node and varpool node is traversed looking for
5280 decls and types embedded in them. This is a more exhaustive
5281 search than that done by find_referenced_vars, because it will
5282 also collect individual fields, decls embedded in types, etc.
5284 2- All the decls found are sent to free_lang_data_in_decl.
5286 3- All the types found are sent to free_lang_data_in_type.
5288 The ordering between decls and types is important because
5289 free_lang_data_in_decl sets assembler names, which includes
5290 mangling. So types cannot be freed up until assembler names have
5291 been set up. */
5293 static void
5294 free_lang_data_in_cgraph (void)
5296 struct cgraph_node *n;
5297 struct varpool_node *v;
5298 struct free_lang_data_d fld;
5299 tree t;
5300 unsigned i;
5301 alias_pair *p;
5303 /* Initialize sets and arrays to store referenced decls and types. */
5304 fld.pset = pointer_set_create ();
5305 fld.worklist.create (0);
5306 fld.decls.create (100);
5307 fld.types.create (100);
5309 /* Find decls and types in the body of every function in the callgraph. */
5310 FOR_EACH_FUNCTION (n)
5311 find_decls_types_in_node (n, &fld);
5313 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5314 find_decls_types (p->decl, &fld);
5316 /* Find decls and types in every varpool symbol. */
5317 FOR_EACH_VARIABLE (v)
5318 find_decls_types_in_var (v, &fld);
5320 /* Set the assembler name on every decl found. We need to do this
5321 now because free_lang_data_in_decl will invalidate data needed
5322 for mangling. This breaks mangling on interdependent decls. */
5323 FOR_EACH_VEC_ELT (fld.decls, i, t)
5324 assign_assembler_name_if_neeeded (t);
5326 /* Traverse every decl found freeing its language data. */
5327 FOR_EACH_VEC_ELT (fld.decls, i, t)
5328 free_lang_data_in_decl (t);
5330 /* Traverse every type found freeing its language data. */
5331 FOR_EACH_VEC_ELT (fld.types, i, t)
5332 free_lang_data_in_type (t);
5334 pointer_set_destroy (fld.pset);
5335 fld.worklist.release ();
5336 fld.decls.release ();
5337 fld.types.release ();
5341 /* Free resources that are used by FE but are not needed once they are done. */
5343 static unsigned
5344 free_lang_data (void)
5346 unsigned i;
5348 /* If we are the LTO frontend we have freed lang-specific data already. */
5349 if (in_lto_p
5350 || !flag_generate_lto)
5351 return 0;
5353 /* Allocate and assign alias sets to the standard integer types
5354 while the slots are still in the way the frontends generated them. */
5355 for (i = 0; i < itk_none; ++i)
5356 if (integer_types[i])
5357 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5359 /* Traverse the IL resetting language specific information for
5360 operands, expressions, etc. */
5361 free_lang_data_in_cgraph ();
5363 /* Create gimple variants for common types. */
5364 ptrdiff_type_node = integer_type_node;
5365 fileptr_type_node = ptr_type_node;
5367 /* Reset some langhooks. Do not reset types_compatible_p, it may
5368 still be used indirectly via the get_alias_set langhook. */
5369 lang_hooks.dwarf_name = lhd_dwarf_name;
5370 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5371 /* We do not want the default decl_assembler_name implementation,
5372 rather if we have fixed everything we want a wrapper around it
5373 asserting that all non-local symbols already got their assembler
5374 name and only produce assembler names for local symbols. Or rather
5375 make sure we never call decl_assembler_name on local symbols and
5376 devise a separate, middle-end private scheme for it. */
5378 /* Reset diagnostic machinery. */
5379 tree_diagnostics_defaults (global_dc);
5381 return 0;
5385 struct simple_ipa_opt_pass pass_ipa_free_lang_data =
5388 SIMPLE_IPA_PASS,
5389 "*free_lang_data", /* name */
5390 OPTGROUP_NONE, /* optinfo_flags */
5391 NULL, /* gate */
5392 free_lang_data, /* execute */
5393 NULL, /* sub */
5394 NULL, /* next */
5395 0, /* static_pass_number */
5396 TV_IPA_FREE_LANG_DATA, /* tv_id */
5397 0, /* properties_required */
5398 0, /* properties_provided */
5399 0, /* properties_destroyed */
5400 0, /* todo_flags_start */
5401 0 /* todo_flags_finish */
5405 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
5406 ATTR_NAME. Also used internally by remove_attribute(). */
5407 bool
5408 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
5410 size_t ident_len = IDENTIFIER_LENGTH (ident);
5412 if (ident_len == attr_len)
5414 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
5415 return true;
5417 else if (ident_len == attr_len + 4)
5419 /* There is the possibility that ATTR is 'text' and IDENT is
5420 '__text__'. */
5421 const char *p = IDENTIFIER_POINTER (ident);
5422 if (p[0] == '_' && p[1] == '_'
5423 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5424 && strncmp (attr_name, p + 2, attr_len) == 0)
5425 return true;
5428 return false;
5431 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
5432 of ATTR_NAME, and LIST is not NULL_TREE. */
5433 tree
5434 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
5436 while (list)
5438 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5440 if (ident_len == attr_len)
5442 if (!strcmp (attr_name,
5443 IDENTIFIER_POINTER (get_attribute_name (list))))
5444 break;
5446 /* TODO: If we made sure that attributes were stored in the
5447 canonical form without '__...__' (ie, as in 'text' as opposed
5448 to '__text__') then we could avoid the following case. */
5449 else if (ident_len == attr_len + 4)
5451 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5452 if (p[0] == '_' && p[1] == '_'
5453 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5454 && strncmp (attr_name, p + 2, attr_len) == 0)
5455 break;
5457 list = TREE_CHAIN (list);
5460 return list;
5463 /* A variant of lookup_attribute() that can be used with an identifier
5464 as the first argument, and where the identifier can be either
5465 'text' or '__text__'.
5467 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
5468 return a pointer to the attribute's list element if the attribute
5469 is part of the list, or NULL_TREE if not found. If the attribute
5470 appears more than once, this only returns the first occurrence; the
5471 TREE_CHAIN of the return value should be passed back in if further
5472 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
5473 can be in the form 'text' or '__text__'. */
5474 static tree
5475 lookup_ident_attribute (tree attr_identifier, tree list)
5477 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
5479 while (list)
5481 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
5482 == IDENTIFIER_NODE);
5484 /* Identifiers can be compared directly for equality. */
5485 if (attr_identifier == get_attribute_name (list))
5486 break;
5488 /* If they are not equal, they may still be one in the form
5489 'text' while the other one is in the form '__text__'. TODO:
5490 If we were storing attributes in normalized 'text' form, then
5491 this could all go away and we could take full advantage of
5492 the fact that we're comparing identifiers. :-) */
5494 size_t attr_len = IDENTIFIER_LENGTH (attr_identifier);
5495 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5497 if (ident_len == attr_len + 4)
5499 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5500 const char *q = IDENTIFIER_POINTER (attr_identifier);
5501 if (p[0] == '_' && p[1] == '_'
5502 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5503 && strncmp (q, p + 2, attr_len) == 0)
5504 break;
5506 else if (ident_len + 4 == attr_len)
5508 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5509 const char *q = IDENTIFIER_POINTER (attr_identifier);
5510 if (q[0] == '_' && q[1] == '_'
5511 && q[attr_len - 2] == '_' && q[attr_len - 1] == '_'
5512 && strncmp (q + 2, p, ident_len) == 0)
5513 break;
5516 list = TREE_CHAIN (list);
5519 return list;
5522 /* Remove any instances of attribute ATTR_NAME in LIST and return the
5523 modified list. */
5525 tree
5526 remove_attribute (const char *attr_name, tree list)
5528 tree *p;
5529 size_t attr_len = strlen (attr_name);
5531 gcc_checking_assert (attr_name[0] != '_');
5533 for (p = &list; *p; )
5535 tree l = *p;
5536 /* TODO: If we were storing attributes in normalized form, here
5537 we could use a simple strcmp(). */
5538 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
5539 *p = TREE_CHAIN (l);
5540 else
5541 p = &TREE_CHAIN (l);
5544 return list;
5547 /* Return an attribute list that is the union of a1 and a2. */
5549 tree
5550 merge_attributes (tree a1, tree a2)
5552 tree attributes;
5554 /* Either one unset? Take the set one. */
5556 if ((attributes = a1) == 0)
5557 attributes = a2;
5559 /* One that completely contains the other? Take it. */
5561 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
5563 if (attribute_list_contained (a2, a1))
5564 attributes = a2;
5565 else
5567 /* Pick the longest list, and hang on the other list. */
5569 if (list_length (a1) < list_length (a2))
5570 attributes = a2, a2 = a1;
5572 for (; a2 != 0; a2 = TREE_CHAIN (a2))
5574 tree a;
5575 for (a = lookup_ident_attribute (get_attribute_name (a2),
5576 attributes);
5577 a != NULL_TREE && !attribute_value_equal (a, a2);
5578 a = lookup_ident_attribute (get_attribute_name (a2),
5579 TREE_CHAIN (a)))
5581 if (a == NULL_TREE)
5583 a1 = copy_node (a2);
5584 TREE_CHAIN (a1) = attributes;
5585 attributes = a1;
5590 return attributes;
5593 /* Given types T1 and T2, merge their attributes and return
5594 the result. */
5596 tree
5597 merge_type_attributes (tree t1, tree t2)
5599 return merge_attributes (TYPE_ATTRIBUTES (t1),
5600 TYPE_ATTRIBUTES (t2));
5603 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
5604 the result. */
5606 tree
5607 merge_decl_attributes (tree olddecl, tree newdecl)
5609 return merge_attributes (DECL_ATTRIBUTES (olddecl),
5610 DECL_ATTRIBUTES (newdecl));
5613 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
5615 /* Specialization of merge_decl_attributes for various Windows targets.
5617 This handles the following situation:
5619 __declspec (dllimport) int foo;
5620 int foo;
5622 The second instance of `foo' nullifies the dllimport. */
5624 tree
5625 merge_dllimport_decl_attributes (tree old, tree new_tree)
5627 tree a;
5628 int delete_dllimport_p = 1;
5630 /* What we need to do here is remove from `old' dllimport if it doesn't
5631 appear in `new'. dllimport behaves like extern: if a declaration is
5632 marked dllimport and a definition appears later, then the object
5633 is not dllimport'd. We also remove a `new' dllimport if the old list
5634 contains dllexport: dllexport always overrides dllimport, regardless
5635 of the order of declaration. */
5636 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
5637 delete_dllimport_p = 0;
5638 else if (DECL_DLLIMPORT_P (new_tree)
5639 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
5641 DECL_DLLIMPORT_P (new_tree) = 0;
5642 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
5643 "dllimport ignored", new_tree);
5645 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
5647 /* Warn about overriding a symbol that has already been used, e.g.:
5648 extern int __attribute__ ((dllimport)) foo;
5649 int* bar () {return &foo;}
5650 int foo;
5652 if (TREE_USED (old))
5654 warning (0, "%q+D redeclared without dllimport attribute "
5655 "after being referenced with dll linkage", new_tree);
5656 /* If we have used a variable's address with dllimport linkage,
5657 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
5658 decl may already have had TREE_CONSTANT computed.
5659 We still remove the attribute so that assembler code refers
5660 to '&foo rather than '_imp__foo'. */
5661 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
5662 DECL_DLLIMPORT_P (new_tree) = 1;
5665 /* Let an inline definition silently override the external reference,
5666 but otherwise warn about attribute inconsistency. */
5667 else if (TREE_CODE (new_tree) == VAR_DECL
5668 || !DECL_DECLARED_INLINE_P (new_tree))
5669 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
5670 "previous dllimport ignored", new_tree);
5672 else
5673 delete_dllimport_p = 0;
5675 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
5677 if (delete_dllimport_p)
5678 a = remove_attribute ("dllimport", a);
5680 return a;
5683 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
5684 struct attribute_spec.handler. */
5686 tree
5687 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
5688 bool *no_add_attrs)
5690 tree node = *pnode;
5691 bool is_dllimport;
5693 /* These attributes may apply to structure and union types being created,
5694 but otherwise should pass to the declaration involved. */
5695 if (!DECL_P (node))
5697 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
5698 | (int) ATTR_FLAG_ARRAY_NEXT))
5700 *no_add_attrs = true;
5701 return tree_cons (name, args, NULL_TREE);
5703 if (TREE_CODE (node) == RECORD_TYPE
5704 || TREE_CODE (node) == UNION_TYPE)
5706 node = TYPE_NAME (node);
5707 if (!node)
5708 return NULL_TREE;
5710 else
5712 warning (OPT_Wattributes, "%qE attribute ignored",
5713 name);
5714 *no_add_attrs = true;
5715 return NULL_TREE;
5719 if (TREE_CODE (node) != FUNCTION_DECL
5720 && TREE_CODE (node) != VAR_DECL
5721 && TREE_CODE (node) != TYPE_DECL)
5723 *no_add_attrs = true;
5724 warning (OPT_Wattributes, "%qE attribute ignored",
5725 name);
5726 return NULL_TREE;
5729 if (TREE_CODE (node) == TYPE_DECL
5730 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
5731 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
5733 *no_add_attrs = true;
5734 warning (OPT_Wattributes, "%qE attribute ignored",
5735 name);
5736 return NULL_TREE;
5739 is_dllimport = is_attribute_p ("dllimport", name);
5741 /* Report error on dllimport ambiguities seen now before they cause
5742 any damage. */
5743 if (is_dllimport)
5745 /* Honor any target-specific overrides. */
5746 if (!targetm.valid_dllimport_attribute_p (node))
5747 *no_add_attrs = true;
5749 else if (TREE_CODE (node) == FUNCTION_DECL
5750 && DECL_DECLARED_INLINE_P (node))
5752 warning (OPT_Wattributes, "inline function %q+D declared as "
5753 " dllimport: attribute ignored", node);
5754 *no_add_attrs = true;
5756 /* Like MS, treat definition of dllimported variables and
5757 non-inlined functions on declaration as syntax errors. */
5758 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
5760 error ("function %q+D definition is marked dllimport", node);
5761 *no_add_attrs = true;
5764 else if (TREE_CODE (node) == VAR_DECL)
5766 if (DECL_INITIAL (node))
5768 error ("variable %q+D definition is marked dllimport",
5769 node);
5770 *no_add_attrs = true;
5773 /* `extern' needn't be specified with dllimport.
5774 Specify `extern' now and hope for the best. Sigh. */
5775 DECL_EXTERNAL (node) = 1;
5776 /* Also, implicitly give dllimport'd variables declared within
5777 a function global scope, unless declared static. */
5778 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
5779 TREE_PUBLIC (node) = 1;
5782 if (*no_add_attrs == false)
5783 DECL_DLLIMPORT_P (node) = 1;
5785 else if (TREE_CODE (node) == FUNCTION_DECL
5786 && DECL_DECLARED_INLINE_P (node)
5787 && flag_keep_inline_dllexport)
5788 /* An exported function, even if inline, must be emitted. */
5789 DECL_EXTERNAL (node) = 0;
5791 /* Report error if symbol is not accessible at global scope. */
5792 if (!TREE_PUBLIC (node)
5793 && (TREE_CODE (node) == VAR_DECL
5794 || TREE_CODE (node) == FUNCTION_DECL))
5796 error ("external linkage required for symbol %q+D because of "
5797 "%qE attribute", node, name);
5798 *no_add_attrs = true;
5801 /* A dllexport'd entity must have default visibility so that other
5802 program units (shared libraries or the main executable) can see
5803 it. A dllimport'd entity must have default visibility so that
5804 the linker knows that undefined references within this program
5805 unit can be resolved by the dynamic linker. */
5806 if (!*no_add_attrs)
5808 if (DECL_VISIBILITY_SPECIFIED (node)
5809 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
5810 error ("%qE implies default visibility, but %qD has already "
5811 "been declared with a different visibility",
5812 name, node);
5813 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
5814 DECL_VISIBILITY_SPECIFIED (node) = 1;
5817 return NULL_TREE;
5820 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
5822 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
5823 of the various TYPE_QUAL values. */
5825 static void
5826 set_type_quals (tree type, int type_quals)
5828 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
5829 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
5830 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
5831 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
5834 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
5836 bool
5837 check_qualified_type (const_tree cand, const_tree base, int type_quals)
5839 return (TYPE_QUALS (cand) == type_quals
5840 && TYPE_NAME (cand) == TYPE_NAME (base)
5841 /* Apparently this is needed for Objective-C. */
5842 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5843 /* Check alignment. */
5844 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
5845 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5846 TYPE_ATTRIBUTES (base)));
5849 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
5851 static bool
5852 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
5854 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
5855 && TYPE_NAME (cand) == TYPE_NAME (base)
5856 /* Apparently this is needed for Objective-C. */
5857 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5858 /* Check alignment. */
5859 && TYPE_ALIGN (cand) == align
5860 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5861 TYPE_ATTRIBUTES (base)));
5864 /* Return a version of the TYPE, qualified as indicated by the
5865 TYPE_QUALS, if one exists. If no qualified version exists yet,
5866 return NULL_TREE. */
5868 tree
5869 get_qualified_type (tree type, int type_quals)
5871 tree t;
5873 if (TYPE_QUALS (type) == type_quals)
5874 return type;
5876 /* Search the chain of variants to see if there is already one there just
5877 like the one we need to have. If so, use that existing one. We must
5878 preserve the TYPE_NAME, since there is code that depends on this. */
5879 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5880 if (check_qualified_type (t, type, type_quals))
5881 return t;
5883 return NULL_TREE;
5886 /* Like get_qualified_type, but creates the type if it does not
5887 exist. This function never returns NULL_TREE. */
5889 tree
5890 build_qualified_type (tree type, int type_quals)
5892 tree t;
5894 /* See if we already have the appropriate qualified variant. */
5895 t = get_qualified_type (type, type_quals);
5897 /* If not, build it. */
5898 if (!t)
5900 t = build_variant_type_copy (type);
5901 set_type_quals (t, type_quals);
5903 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5904 /* Propagate structural equality. */
5905 SET_TYPE_STRUCTURAL_EQUALITY (t);
5906 else if (TYPE_CANONICAL (type) != type)
5907 /* Build the underlying canonical type, since it is different
5908 from TYPE. */
5909 TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
5910 type_quals);
5911 else
5912 /* T is its own canonical type. */
5913 TYPE_CANONICAL (t) = t;
5917 return t;
5920 /* Create a variant of type T with alignment ALIGN. */
5922 tree
5923 build_aligned_type (tree type, unsigned int align)
5925 tree t;
5927 if (TYPE_PACKED (type)
5928 || TYPE_ALIGN (type) == align)
5929 return type;
5931 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5932 if (check_aligned_type (t, type, align))
5933 return t;
5935 t = build_variant_type_copy (type);
5936 TYPE_ALIGN (t) = align;
5938 return t;
5941 /* Create a new distinct copy of TYPE. The new type is made its own
5942 MAIN_VARIANT. If TYPE requires structural equality checks, the
5943 resulting type requires structural equality checks; otherwise, its
5944 TYPE_CANONICAL points to itself. */
5946 tree
5947 build_distinct_type_copy (tree type)
5949 tree t = copy_node (type);
5951 TYPE_POINTER_TO (t) = 0;
5952 TYPE_REFERENCE_TO (t) = 0;
5954 /* Set the canonical type either to a new equivalence class, or
5955 propagate the need for structural equality checks. */
5956 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5957 SET_TYPE_STRUCTURAL_EQUALITY (t);
5958 else
5959 TYPE_CANONICAL (t) = t;
5961 /* Make it its own variant. */
5962 TYPE_MAIN_VARIANT (t) = t;
5963 TYPE_NEXT_VARIANT (t) = 0;
5965 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
5966 whose TREE_TYPE is not t. This can also happen in the Ada
5967 frontend when using subtypes. */
5969 return t;
5972 /* Create a new variant of TYPE, equivalent but distinct. This is so
5973 the caller can modify it. TYPE_CANONICAL for the return type will
5974 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
5975 are considered equal by the language itself (or that both types
5976 require structural equality checks). */
5978 tree
5979 build_variant_type_copy (tree type)
5981 tree t, m = TYPE_MAIN_VARIANT (type);
5983 t = build_distinct_type_copy (type);
5985 /* Since we're building a variant, assume that it is a non-semantic
5986 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
5987 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
5989 /* Add the new type to the chain of variants of TYPE. */
5990 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
5991 TYPE_NEXT_VARIANT (m) = t;
5992 TYPE_MAIN_VARIANT (t) = m;
5994 return t;
5997 /* Return true if the from tree in both tree maps are equal. */
6000 tree_map_base_eq (const void *va, const void *vb)
6002 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6003 *const b = (const struct tree_map_base *) vb;
6004 return (a->from == b->from);
6007 /* Hash a from tree in a tree_base_map. */
6009 unsigned int
6010 tree_map_base_hash (const void *item)
6012 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6015 /* Return true if this tree map structure is marked for garbage collection
6016 purposes. We simply return true if the from tree is marked, so that this
6017 structure goes away when the from tree goes away. */
6020 tree_map_base_marked_p (const void *p)
6022 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6025 /* Hash a from tree in a tree_map. */
6027 unsigned int
6028 tree_map_hash (const void *item)
6030 return (((const struct tree_map *) item)->hash);
6033 /* Hash a from tree in a tree_decl_map. */
6035 unsigned int
6036 tree_decl_map_hash (const void *item)
6038 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6041 /* Return the initialization priority for DECL. */
6043 priority_type
6044 decl_init_priority_lookup (tree decl)
6046 struct tree_priority_map *h;
6047 struct tree_map_base in;
6049 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6050 in.from = decl;
6051 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6052 return h ? h->init : DEFAULT_INIT_PRIORITY;
6055 /* Return the finalization priority for DECL. */
6057 priority_type
6058 decl_fini_priority_lookup (tree decl)
6060 struct tree_priority_map *h;
6061 struct tree_map_base in;
6063 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6064 in.from = decl;
6065 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6066 return h ? h->fini : DEFAULT_INIT_PRIORITY;
6069 /* Return the initialization and finalization priority information for
6070 DECL. If there is no previous priority information, a freshly
6071 allocated structure is returned. */
6073 static struct tree_priority_map *
6074 decl_priority_info (tree decl)
6076 struct tree_priority_map in;
6077 struct tree_priority_map *h;
6078 void **loc;
6080 in.base.from = decl;
6081 loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
6082 h = (struct tree_priority_map *) *loc;
6083 if (!h)
6085 h = ggc_alloc_cleared_tree_priority_map ();
6086 *loc = h;
6087 h->base.from = decl;
6088 h->init = DEFAULT_INIT_PRIORITY;
6089 h->fini = DEFAULT_INIT_PRIORITY;
6092 return h;
6095 /* Set the initialization priority for DECL to PRIORITY. */
6097 void
6098 decl_init_priority_insert (tree decl, priority_type priority)
6100 struct tree_priority_map *h;
6102 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6103 if (priority == DEFAULT_INIT_PRIORITY)
6104 return;
6105 h = decl_priority_info (decl);
6106 h->init = priority;
6109 /* Set the finalization priority for DECL to PRIORITY. */
6111 void
6112 decl_fini_priority_insert (tree decl, priority_type priority)
6114 struct tree_priority_map *h;
6116 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6117 if (priority == DEFAULT_INIT_PRIORITY)
6118 return;
6119 h = decl_priority_info (decl);
6120 h->fini = priority;
6123 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6125 static void
6126 print_debug_expr_statistics (void)
6128 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6129 (long) htab_size (debug_expr_for_decl),
6130 (long) htab_elements (debug_expr_for_decl),
6131 htab_collisions (debug_expr_for_decl));
6134 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6136 static void
6137 print_value_expr_statistics (void)
6139 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6140 (long) htab_size (value_expr_for_decl),
6141 (long) htab_elements (value_expr_for_decl),
6142 htab_collisions (value_expr_for_decl));
6145 /* Lookup a debug expression for FROM, and return it if we find one. */
6147 tree
6148 decl_debug_expr_lookup (tree from)
6150 struct tree_decl_map *h, in;
6151 in.base.from = from;
6153 h = (struct tree_decl_map *)
6154 htab_find_with_hash (debug_expr_for_decl, &in, DECL_UID (from));
6155 if (h)
6156 return h->to;
6157 return NULL_TREE;
6160 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6162 void
6163 decl_debug_expr_insert (tree from, tree to)
6165 struct tree_decl_map *h;
6166 void **loc;
6168 h = ggc_alloc_tree_decl_map ();
6169 h->base.from = from;
6170 h->to = to;
6171 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, DECL_UID (from),
6172 INSERT);
6173 *(struct tree_decl_map **) loc = h;
6176 /* Lookup a value expression for FROM, and return it if we find one. */
6178 tree
6179 decl_value_expr_lookup (tree from)
6181 struct tree_decl_map *h, in;
6182 in.base.from = from;
6184 h = (struct tree_decl_map *)
6185 htab_find_with_hash (value_expr_for_decl, &in, DECL_UID (from));
6186 if (h)
6187 return h->to;
6188 return NULL_TREE;
6191 /* Insert a mapping FROM->TO in the value expression hashtable. */
6193 void
6194 decl_value_expr_insert (tree from, tree to)
6196 struct tree_decl_map *h;
6197 void **loc;
6199 h = ggc_alloc_tree_decl_map ();
6200 h->base.from = from;
6201 h->to = to;
6202 loc = htab_find_slot_with_hash (value_expr_for_decl, h, DECL_UID (from),
6203 INSERT);
6204 *(struct tree_decl_map **) loc = h;
6207 /* Lookup a vector of debug arguments for FROM, and return it if we
6208 find one. */
6210 vec<tree, va_gc> **
6211 decl_debug_args_lookup (tree from)
6213 struct tree_vec_map *h, in;
6215 if (!DECL_HAS_DEBUG_ARGS_P (from))
6216 return NULL;
6217 gcc_checking_assert (debug_args_for_decl != NULL);
6218 in.base.from = from;
6219 h = (struct tree_vec_map *)
6220 htab_find_with_hash (debug_args_for_decl, &in, DECL_UID (from));
6221 if (h)
6222 return &h->to;
6223 return NULL;
6226 /* Insert a mapping FROM->empty vector of debug arguments in the value
6227 expression hashtable. */
6229 vec<tree, va_gc> **
6230 decl_debug_args_insert (tree from)
6232 struct tree_vec_map *h;
6233 void **loc;
6235 if (DECL_HAS_DEBUG_ARGS_P (from))
6236 return decl_debug_args_lookup (from);
6237 if (debug_args_for_decl == NULL)
6238 debug_args_for_decl = htab_create_ggc (64, tree_vec_map_hash,
6239 tree_vec_map_eq, 0);
6240 h = ggc_alloc_tree_vec_map ();
6241 h->base.from = from;
6242 h->to = NULL;
6243 loc = htab_find_slot_with_hash (debug_args_for_decl, h, DECL_UID (from),
6244 INSERT);
6245 *(struct tree_vec_map **) loc = h;
6246 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6247 return &h->to;
6250 /* Hashing of types so that we don't make duplicates.
6251 The entry point is `type_hash_canon'. */
6253 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6254 with types in the TREE_VALUE slots), by adding the hash codes
6255 of the individual types. */
6257 static unsigned int
6258 type_hash_list (const_tree list, hashval_t hashcode)
6260 const_tree tail;
6262 for (tail = list; tail; tail = TREE_CHAIN (tail))
6263 if (TREE_VALUE (tail) != error_mark_node)
6264 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
6265 hashcode);
6267 return hashcode;
6270 /* These are the Hashtable callback functions. */
6272 /* Returns true iff the types are equivalent. */
6274 static int
6275 type_hash_eq (const void *va, const void *vb)
6277 const struct type_hash *const a = (const struct type_hash *) va,
6278 *const b = (const struct type_hash *) vb;
6280 /* First test the things that are the same for all types. */
6281 if (a->hash != b->hash
6282 || TREE_CODE (a->type) != TREE_CODE (b->type)
6283 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6284 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6285 TYPE_ATTRIBUTES (b->type))
6286 || (TREE_CODE (a->type) != COMPLEX_TYPE
6287 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6288 return 0;
6290 /* Be careful about comparing arrays before and after the element type
6291 has been completed; don't compare TYPE_ALIGN unless both types are
6292 complete. */
6293 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6294 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6295 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6296 return 0;
6298 switch (TREE_CODE (a->type))
6300 case VOID_TYPE:
6301 case COMPLEX_TYPE:
6302 case POINTER_TYPE:
6303 case REFERENCE_TYPE:
6304 case NULLPTR_TYPE:
6305 return 1;
6307 case VECTOR_TYPE:
6308 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6310 case ENUMERAL_TYPE:
6311 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6312 && !(TYPE_VALUES (a->type)
6313 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6314 && TYPE_VALUES (b->type)
6315 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6316 && type_list_equal (TYPE_VALUES (a->type),
6317 TYPE_VALUES (b->type))))
6318 return 0;
6320 /* ... fall through ... */
6322 case INTEGER_TYPE:
6323 case REAL_TYPE:
6324 case BOOLEAN_TYPE:
6325 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6326 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6327 TYPE_MAX_VALUE (b->type)))
6328 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6329 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6330 TYPE_MIN_VALUE (b->type))));
6332 case FIXED_POINT_TYPE:
6333 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6335 case OFFSET_TYPE:
6336 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6338 case METHOD_TYPE:
6339 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6340 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6341 || (TYPE_ARG_TYPES (a->type)
6342 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6343 && TYPE_ARG_TYPES (b->type)
6344 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6345 && type_list_equal (TYPE_ARG_TYPES (a->type),
6346 TYPE_ARG_TYPES (b->type)))))
6347 break;
6348 return 0;
6349 case ARRAY_TYPE:
6350 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6352 case RECORD_TYPE:
6353 case UNION_TYPE:
6354 case QUAL_UNION_TYPE:
6355 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6356 || (TYPE_FIELDS (a->type)
6357 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6358 && TYPE_FIELDS (b->type)
6359 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6360 && type_list_equal (TYPE_FIELDS (a->type),
6361 TYPE_FIELDS (b->type))));
6363 case FUNCTION_TYPE:
6364 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6365 || (TYPE_ARG_TYPES (a->type)
6366 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6367 && TYPE_ARG_TYPES (b->type)
6368 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6369 && type_list_equal (TYPE_ARG_TYPES (a->type),
6370 TYPE_ARG_TYPES (b->type))))
6371 break;
6372 return 0;
6374 default:
6375 return 0;
6378 if (lang_hooks.types.type_hash_eq != NULL)
6379 return lang_hooks.types.type_hash_eq (a->type, b->type);
6381 return 1;
6384 /* Return the cached hash value. */
6386 static hashval_t
6387 type_hash_hash (const void *item)
6389 return ((const struct type_hash *) item)->hash;
6392 /* Look in the type hash table for a type isomorphic to TYPE.
6393 If one is found, return it. Otherwise return 0. */
6395 static tree
6396 type_hash_lookup (hashval_t hashcode, tree type)
6398 struct type_hash *h, in;
6400 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6401 must call that routine before comparing TYPE_ALIGNs. */
6402 layout_type (type);
6404 in.hash = hashcode;
6405 in.type = type;
6407 h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
6408 hashcode);
6409 if (h)
6410 return h->type;
6411 return NULL_TREE;
6414 /* Add an entry to the type-hash-table
6415 for a type TYPE whose hash code is HASHCODE. */
6417 static void
6418 type_hash_add (hashval_t hashcode, tree type)
6420 struct type_hash *h;
6421 void **loc;
6423 h = ggc_alloc_type_hash ();
6424 h->hash = hashcode;
6425 h->type = type;
6426 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
6427 *loc = (void *)h;
6430 /* Given TYPE, and HASHCODE its hash code, return the canonical
6431 object for an identical type if one already exists.
6432 Otherwise, return TYPE, and record it as the canonical object.
6434 To use this function, first create a type of the sort you want.
6435 Then compute its hash code from the fields of the type that
6436 make it different from other similar types.
6437 Then call this function and use the value. */
6439 tree
6440 type_hash_canon (unsigned int hashcode, tree type)
6442 tree t1;
6444 /* The hash table only contains main variants, so ensure that's what we're
6445 being passed. */
6446 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6448 /* See if the type is in the hash table already. If so, return it.
6449 Otherwise, add the type. */
6450 t1 = type_hash_lookup (hashcode, type);
6451 if (t1 != 0)
6453 if (GATHER_STATISTICS)
6455 tree_code_counts[(int) TREE_CODE (type)]--;
6456 tree_node_counts[(int) t_kind]--;
6457 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
6459 return t1;
6461 else
6463 type_hash_add (hashcode, type);
6464 return type;
6468 /* See if the data pointed to by the type hash table is marked. We consider
6469 it marked if the type is marked or if a debug type number or symbol
6470 table entry has been made for the type. */
6472 static int
6473 type_hash_marked_p (const void *p)
6475 const_tree const type = ((const struct type_hash *) p)->type;
6477 return ggc_marked_p (type);
6480 static void
6481 print_type_hash_statistics (void)
6483 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6484 (long) htab_size (type_hash_table),
6485 (long) htab_elements (type_hash_table),
6486 htab_collisions (type_hash_table));
6489 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
6490 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
6491 by adding the hash codes of the individual attributes. */
6493 static unsigned int
6494 attribute_hash_list (const_tree list, hashval_t hashcode)
6496 const_tree tail;
6498 for (tail = list; tail; tail = TREE_CHAIN (tail))
6499 /* ??? Do we want to add in TREE_VALUE too? */
6500 hashcode = iterative_hash_object
6501 (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)), hashcode);
6502 return hashcode;
6505 /* Given two lists of attributes, return true if list l2 is
6506 equivalent to l1. */
6509 attribute_list_equal (const_tree l1, const_tree l2)
6511 if (l1 == l2)
6512 return 1;
6514 return attribute_list_contained (l1, l2)
6515 && attribute_list_contained (l2, l1);
6518 /* Given two lists of attributes, return true if list L2 is
6519 completely contained within L1. */
6520 /* ??? This would be faster if attribute names were stored in a canonicalized
6521 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
6522 must be used to show these elements are equivalent (which they are). */
6523 /* ??? It's not clear that attributes with arguments will always be handled
6524 correctly. */
6527 attribute_list_contained (const_tree l1, const_tree l2)
6529 const_tree t1, t2;
6531 /* First check the obvious, maybe the lists are identical. */
6532 if (l1 == l2)
6533 return 1;
6535 /* Maybe the lists are similar. */
6536 for (t1 = l1, t2 = l2;
6537 t1 != 0 && t2 != 0
6538 && get_attribute_name (t1) == get_attribute_name (t2)
6539 && TREE_VALUE (t1) == TREE_VALUE (t2);
6540 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6543 /* Maybe the lists are equal. */
6544 if (t1 == 0 && t2 == 0)
6545 return 1;
6547 for (; t2 != 0; t2 = TREE_CHAIN (t2))
6549 const_tree attr;
6550 /* This CONST_CAST is okay because lookup_attribute does not
6551 modify its argument and the return value is assigned to a
6552 const_tree. */
6553 for (attr = lookup_ident_attribute (get_attribute_name (t2), CONST_CAST_TREE(l1));
6554 attr != NULL_TREE && !attribute_value_equal (t2, attr);
6555 attr = lookup_ident_attribute (get_attribute_name (t2), TREE_CHAIN (attr)))
6558 if (attr == NULL_TREE)
6559 return 0;
6562 return 1;
6565 /* Given two lists of types
6566 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6567 return 1 if the lists contain the same types in the same order.
6568 Also, the TREE_PURPOSEs must match. */
6571 type_list_equal (const_tree l1, const_tree l2)
6573 const_tree t1, t2;
6575 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6576 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6577 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6578 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6579 && (TREE_TYPE (TREE_PURPOSE (t1))
6580 == TREE_TYPE (TREE_PURPOSE (t2))))))
6581 return 0;
6583 return t1 == t2;
6586 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6587 given by TYPE. If the argument list accepts variable arguments,
6588 then this function counts only the ordinary arguments. */
6591 type_num_arguments (const_tree type)
6593 int i = 0;
6594 tree t;
6596 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6597 /* If the function does not take a variable number of arguments,
6598 the last element in the list will have type `void'. */
6599 if (VOID_TYPE_P (TREE_VALUE (t)))
6600 break;
6601 else
6602 ++i;
6604 return i;
6607 /* Nonzero if integer constants T1 and T2
6608 represent the same constant value. */
6611 tree_int_cst_equal (const_tree t1, const_tree t2)
6613 if (t1 == t2)
6614 return 1;
6616 if (t1 == 0 || t2 == 0)
6617 return 0;
6619 if (TREE_CODE (t1) == INTEGER_CST
6620 && TREE_CODE (t2) == INTEGER_CST
6621 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6622 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
6623 return 1;
6625 return 0;
6628 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
6629 The precise way of comparison depends on their data type. */
6632 tree_int_cst_lt (const_tree t1, const_tree t2)
6634 if (t1 == t2)
6635 return 0;
6637 if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
6639 int t1_sgn = tree_int_cst_sgn (t1);
6640 int t2_sgn = tree_int_cst_sgn (t2);
6642 if (t1_sgn < t2_sgn)
6643 return 1;
6644 else if (t1_sgn > t2_sgn)
6645 return 0;
6646 /* Otherwise, both are non-negative, so we compare them as
6647 unsigned just in case one of them would overflow a signed
6648 type. */
6650 else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
6651 return INT_CST_LT (t1, t2);
6653 return INT_CST_LT_UNSIGNED (t1, t2);
6656 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
6659 tree_int_cst_compare (const_tree t1, const_tree t2)
6661 if (tree_int_cst_lt (t1, t2))
6662 return -1;
6663 else if (tree_int_cst_lt (t2, t1))
6664 return 1;
6665 else
6666 return 0;
6669 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
6670 the host. If POS is zero, the value can be represented in a single
6671 HOST_WIDE_INT. If POS is nonzero, the value must be non-negative and can
6672 be represented in a single unsigned HOST_WIDE_INT. */
6675 host_integerp (const_tree t, int pos)
6677 if (t == NULL_TREE)
6678 return 0;
6680 return (TREE_CODE (t) == INTEGER_CST
6681 && ((TREE_INT_CST_HIGH (t) == 0
6682 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
6683 || (! pos && TREE_INT_CST_HIGH (t) == -1
6684 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
6685 && !TYPE_UNSIGNED (TREE_TYPE (t)))
6686 || (pos && TREE_INT_CST_HIGH (t) == 0)));
6689 /* Return the HOST_WIDE_INT least significant bits of T if it is an
6690 INTEGER_CST and there is no overflow. POS is nonzero if the result must
6691 be non-negative. We must be able to satisfy the above conditions. */
6693 HOST_WIDE_INT
6694 tree_low_cst (const_tree t, int pos)
6696 gcc_assert (host_integerp (t, pos));
6697 return TREE_INT_CST_LOW (t);
6700 /* Return the HOST_WIDE_INT least significant bits of T, a sizetype
6701 kind INTEGER_CST. This makes sure to properly sign-extend the
6702 constant. */
6704 HOST_WIDE_INT
6705 size_low_cst (const_tree t)
6707 double_int d = tree_to_double_int (t);
6708 return d.sext (TYPE_PRECISION (TREE_TYPE (t))).low;
6711 /* Return the most significant (sign) bit of T. */
6714 tree_int_cst_sign_bit (const_tree t)
6716 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
6717 unsigned HOST_WIDE_INT w;
6719 if (bitno < HOST_BITS_PER_WIDE_INT)
6720 w = TREE_INT_CST_LOW (t);
6721 else
6723 w = TREE_INT_CST_HIGH (t);
6724 bitno -= HOST_BITS_PER_WIDE_INT;
6727 return (w >> bitno) & 1;
6730 /* Return an indication of the sign of the integer constant T.
6731 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
6732 Note that -1 will never be returned if T's type is unsigned. */
6735 tree_int_cst_sgn (const_tree t)
6737 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
6738 return 0;
6739 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
6740 return 1;
6741 else if (TREE_INT_CST_HIGH (t) < 0)
6742 return -1;
6743 else
6744 return 1;
6747 /* Return the minimum number of bits needed to represent VALUE in a
6748 signed or unsigned type, UNSIGNEDP says which. */
6750 unsigned int
6751 tree_int_cst_min_precision (tree value, bool unsignedp)
6753 /* If the value is negative, compute its negative minus 1. The latter
6754 adjustment is because the absolute value of the largest negative value
6755 is one larger than the largest positive value. This is equivalent to
6756 a bit-wise negation, so use that operation instead. */
6758 if (tree_int_cst_sgn (value) < 0)
6759 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
6761 /* Return the number of bits needed, taking into account the fact
6762 that we need one more bit for a signed than unsigned type.
6763 If value is 0 or -1, the minimum precision is 1 no matter
6764 whether unsignedp is true or false. */
6766 if (integer_zerop (value))
6767 return 1;
6768 else
6769 return tree_floor_log2 (value) + 1 + !unsignedp;
6772 /* Compare two constructor-element-type constants. Return 1 if the lists
6773 are known to be equal; otherwise return 0. */
6776 simple_cst_list_equal (const_tree l1, const_tree l2)
6778 while (l1 != NULL_TREE && l2 != NULL_TREE)
6780 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
6781 return 0;
6783 l1 = TREE_CHAIN (l1);
6784 l2 = TREE_CHAIN (l2);
6787 return l1 == l2;
6790 /* Return truthvalue of whether T1 is the same tree structure as T2.
6791 Return 1 if they are the same.
6792 Return 0 if they are understandably different.
6793 Return -1 if either contains tree structure not understood by
6794 this function. */
6797 simple_cst_equal (const_tree t1, const_tree t2)
6799 enum tree_code code1, code2;
6800 int cmp;
6801 int i;
6803 if (t1 == t2)
6804 return 1;
6805 if (t1 == 0 || t2 == 0)
6806 return 0;
6808 code1 = TREE_CODE (t1);
6809 code2 = TREE_CODE (t2);
6811 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
6813 if (CONVERT_EXPR_CODE_P (code2)
6814 || code2 == NON_LVALUE_EXPR)
6815 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6816 else
6817 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
6820 else if (CONVERT_EXPR_CODE_P (code2)
6821 || code2 == NON_LVALUE_EXPR)
6822 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
6824 if (code1 != code2)
6825 return 0;
6827 switch (code1)
6829 case INTEGER_CST:
6830 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6831 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
6833 case REAL_CST:
6834 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
6836 case FIXED_CST:
6837 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
6839 case STRING_CST:
6840 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
6841 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
6842 TREE_STRING_LENGTH (t1)));
6844 case CONSTRUCTOR:
6846 unsigned HOST_WIDE_INT idx;
6847 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
6848 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
6850 if (vec_safe_length (v1) != vec_safe_length (v2))
6851 return false;
6853 for (idx = 0; idx < vec_safe_length (v1); ++idx)
6854 /* ??? Should we handle also fields here? */
6855 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
6856 return false;
6857 return true;
6860 case SAVE_EXPR:
6861 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6863 case CALL_EXPR:
6864 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
6865 if (cmp <= 0)
6866 return cmp;
6867 if (call_expr_nargs (t1) != call_expr_nargs (t2))
6868 return 0;
6870 const_tree arg1, arg2;
6871 const_call_expr_arg_iterator iter1, iter2;
6872 for (arg1 = first_const_call_expr_arg (t1, &iter1),
6873 arg2 = first_const_call_expr_arg (t2, &iter2);
6874 arg1 && arg2;
6875 arg1 = next_const_call_expr_arg (&iter1),
6876 arg2 = next_const_call_expr_arg (&iter2))
6878 cmp = simple_cst_equal (arg1, arg2);
6879 if (cmp <= 0)
6880 return cmp;
6882 return arg1 == arg2;
6885 case TARGET_EXPR:
6886 /* Special case: if either target is an unallocated VAR_DECL,
6887 it means that it's going to be unified with whatever the
6888 TARGET_EXPR is really supposed to initialize, so treat it
6889 as being equivalent to anything. */
6890 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
6891 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
6892 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
6893 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
6894 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
6895 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
6896 cmp = 1;
6897 else
6898 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6900 if (cmp <= 0)
6901 return cmp;
6903 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
6905 case WITH_CLEANUP_EXPR:
6906 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6907 if (cmp <= 0)
6908 return cmp;
6910 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
6912 case COMPONENT_REF:
6913 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
6914 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6916 return 0;
6918 case VAR_DECL:
6919 case PARM_DECL:
6920 case CONST_DECL:
6921 case FUNCTION_DECL:
6922 return 0;
6924 default:
6925 break;
6928 /* This general rule works for most tree codes. All exceptions should be
6929 handled above. If this is a language-specific tree code, we can't
6930 trust what might be in the operand, so say we don't know
6931 the situation. */
6932 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
6933 return -1;
6935 switch (TREE_CODE_CLASS (code1))
6937 case tcc_unary:
6938 case tcc_binary:
6939 case tcc_comparison:
6940 case tcc_expression:
6941 case tcc_reference:
6942 case tcc_statement:
6943 cmp = 1;
6944 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
6946 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
6947 if (cmp <= 0)
6948 return cmp;
6951 return cmp;
6953 default:
6954 return -1;
6958 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
6959 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
6960 than U, respectively. */
6963 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
6965 if (tree_int_cst_sgn (t) < 0)
6966 return -1;
6967 else if (TREE_INT_CST_HIGH (t) != 0)
6968 return 1;
6969 else if (TREE_INT_CST_LOW (t) == u)
6970 return 0;
6971 else if (TREE_INT_CST_LOW (t) < u)
6972 return -1;
6973 else
6974 return 1;
6977 /* Return true if SIZE represents a constant size that is in bounds of
6978 what the middle-end and the backend accepts (covering not more than
6979 half of the address-space). */
6981 bool
6982 valid_constant_size_p (const_tree size)
6984 if (! host_integerp (size, 1)
6985 || TREE_OVERFLOW (size)
6986 || tree_int_cst_sign_bit (size) != 0)
6987 return false;
6988 return true;
6991 /* Return the precision of the type, or for a complex or vector type the
6992 precision of the type of its elements. */
6994 unsigned int
6995 element_precision (const_tree type)
6997 enum tree_code code = TREE_CODE (type);
6998 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
6999 type = TREE_TYPE (type);
7001 return TYPE_PRECISION (type);
7004 /* Return true if CODE represents an associative tree code. Otherwise
7005 return false. */
7006 bool
7007 associative_tree_code (enum tree_code code)
7009 switch (code)
7011 case BIT_IOR_EXPR:
7012 case BIT_AND_EXPR:
7013 case BIT_XOR_EXPR:
7014 case PLUS_EXPR:
7015 case MULT_EXPR:
7016 case MIN_EXPR:
7017 case MAX_EXPR:
7018 return true;
7020 default:
7021 break;
7023 return false;
7026 /* Return true if CODE represents a commutative tree code. Otherwise
7027 return false. */
7028 bool
7029 commutative_tree_code (enum tree_code code)
7031 switch (code)
7033 case PLUS_EXPR:
7034 case MULT_EXPR:
7035 case MULT_HIGHPART_EXPR:
7036 case MIN_EXPR:
7037 case MAX_EXPR:
7038 case BIT_IOR_EXPR:
7039 case BIT_XOR_EXPR:
7040 case BIT_AND_EXPR:
7041 case NE_EXPR:
7042 case EQ_EXPR:
7043 case UNORDERED_EXPR:
7044 case ORDERED_EXPR:
7045 case UNEQ_EXPR:
7046 case LTGT_EXPR:
7047 case TRUTH_AND_EXPR:
7048 case TRUTH_XOR_EXPR:
7049 case TRUTH_OR_EXPR:
7050 case WIDEN_MULT_EXPR:
7051 case VEC_WIDEN_MULT_HI_EXPR:
7052 case VEC_WIDEN_MULT_LO_EXPR:
7053 case VEC_WIDEN_MULT_EVEN_EXPR:
7054 case VEC_WIDEN_MULT_ODD_EXPR:
7055 return true;
7057 default:
7058 break;
7060 return false;
7063 /* Return true if CODE represents a ternary tree code for which the
7064 first two operands are commutative. Otherwise return false. */
7065 bool
7066 commutative_ternary_tree_code (enum tree_code code)
7068 switch (code)
7070 case WIDEN_MULT_PLUS_EXPR:
7071 case WIDEN_MULT_MINUS_EXPR:
7072 return true;
7074 default:
7075 break;
7077 return false;
7080 /* Generate a hash value for an expression. This can be used iteratively
7081 by passing a previous result as the VAL argument.
7083 This function is intended to produce the same hash for expressions which
7084 would compare equal using operand_equal_p. */
7086 hashval_t
7087 iterative_hash_expr (const_tree t, hashval_t val)
7089 int i;
7090 enum tree_code code;
7091 char tclass;
7093 if (t == NULL_TREE)
7094 return iterative_hash_hashval_t (0, val);
7096 code = TREE_CODE (t);
7098 switch (code)
7100 /* Alas, constants aren't shared, so we can't rely on pointer
7101 identity. */
7102 case INTEGER_CST:
7103 val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
7104 return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
7105 case REAL_CST:
7107 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7109 return iterative_hash_hashval_t (val2, val);
7111 case FIXED_CST:
7113 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7115 return iterative_hash_hashval_t (val2, val);
7117 case STRING_CST:
7118 return iterative_hash (TREE_STRING_POINTER (t),
7119 TREE_STRING_LENGTH (t), val);
7120 case COMPLEX_CST:
7121 val = iterative_hash_expr (TREE_REALPART (t), val);
7122 return iterative_hash_expr (TREE_IMAGPART (t), val);
7123 case VECTOR_CST:
7125 unsigned i;
7126 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7127 val = iterative_hash_expr (VECTOR_CST_ELT (t, i), val);
7128 return val;
7130 case SSA_NAME:
7131 /* We can just compare by pointer. */
7132 return iterative_hash_host_wide_int (SSA_NAME_VERSION (t), val);
7133 case PLACEHOLDER_EXPR:
7134 /* The node itself doesn't matter. */
7135 return val;
7136 case TREE_LIST:
7137 /* A list of expressions, for a CALL_EXPR or as the elements of a
7138 VECTOR_CST. */
7139 for (; t; t = TREE_CHAIN (t))
7140 val = iterative_hash_expr (TREE_VALUE (t), val);
7141 return val;
7142 case CONSTRUCTOR:
7144 unsigned HOST_WIDE_INT idx;
7145 tree field, value;
7146 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7148 val = iterative_hash_expr (field, val);
7149 val = iterative_hash_expr (value, val);
7151 return val;
7153 case MEM_REF:
7155 /* The type of the second operand is relevant, except for
7156 its top-level qualifiers. */
7157 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (t, 1)));
7159 val = iterative_hash_object (TYPE_HASH (type), val);
7161 /* We could use the standard hash computation from this point
7162 on. */
7163 val = iterative_hash_object (code, val);
7164 val = iterative_hash_expr (TREE_OPERAND (t, 1), val);
7165 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7166 return val;
7168 case FUNCTION_DECL:
7169 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7170 Otherwise nodes that compare equal according to operand_equal_p might
7171 get different hash codes. However, don't do this for machine specific
7172 or front end builtins, since the function code is overloaded in those
7173 cases. */
7174 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7175 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7177 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7178 code = TREE_CODE (t);
7180 /* FALL THROUGH */
7181 default:
7182 tclass = TREE_CODE_CLASS (code);
7184 if (tclass == tcc_declaration)
7186 /* DECL's have a unique ID */
7187 val = iterative_hash_host_wide_int (DECL_UID (t), val);
7189 else
7191 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7193 val = iterative_hash_object (code, val);
7195 /* Don't hash the type, that can lead to having nodes which
7196 compare equal according to operand_equal_p, but which
7197 have different hash codes. */
7198 if (CONVERT_EXPR_CODE_P (code)
7199 || code == NON_LVALUE_EXPR)
7201 /* Make sure to include signness in the hash computation. */
7202 val += TYPE_UNSIGNED (TREE_TYPE (t));
7203 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7206 else if (commutative_tree_code (code))
7208 /* It's a commutative expression. We want to hash it the same
7209 however it appears. We do this by first hashing both operands
7210 and then rehashing based on the order of their independent
7211 hashes. */
7212 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
7213 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
7214 hashval_t t;
7216 if (one > two)
7217 t = one, one = two, two = t;
7219 val = iterative_hash_hashval_t (one, val);
7220 val = iterative_hash_hashval_t (two, val);
7222 else
7223 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7224 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
7226 return val;
7230 /* Generate a hash value for a pair of expressions. This can be used
7231 iteratively by passing a previous result as the VAL argument.
7233 The same hash value is always returned for a given pair of expressions,
7234 regardless of the order in which they are presented. This is useful in
7235 hashing the operands of commutative functions. */
7237 hashval_t
7238 iterative_hash_exprs_commutative (const_tree t1,
7239 const_tree t2, hashval_t val)
7241 hashval_t one = iterative_hash_expr (t1, 0);
7242 hashval_t two = iterative_hash_expr (t2, 0);
7243 hashval_t t;
7245 if (one > two)
7246 t = one, one = two, two = t;
7247 val = iterative_hash_hashval_t (one, val);
7248 val = iterative_hash_hashval_t (two, val);
7250 return val;
7253 /* Constructors for pointer, array and function types.
7254 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7255 constructed by language-dependent code, not here.) */
7257 /* Construct, lay out and return the type of pointers to TO_TYPE with
7258 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7259 reference all of memory. If such a type has already been
7260 constructed, reuse it. */
7262 tree
7263 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
7264 bool can_alias_all)
7266 tree t;
7268 if (to_type == error_mark_node)
7269 return error_mark_node;
7271 /* If the pointed-to type has the may_alias attribute set, force
7272 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7273 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7274 can_alias_all = true;
7276 /* In some cases, languages will have things that aren't a POINTER_TYPE
7277 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7278 In that case, return that type without regard to the rest of our
7279 operands.
7281 ??? This is a kludge, but consistent with the way this function has
7282 always operated and there doesn't seem to be a good way to avoid this
7283 at the moment. */
7284 if (TYPE_POINTER_TO (to_type) != 0
7285 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7286 return TYPE_POINTER_TO (to_type);
7288 /* First, if we already have a type for pointers to TO_TYPE and it's
7289 the proper mode, use it. */
7290 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7291 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7292 return t;
7294 t = make_node (POINTER_TYPE);
7296 TREE_TYPE (t) = to_type;
7297 SET_TYPE_MODE (t, mode);
7298 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7299 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7300 TYPE_POINTER_TO (to_type) = t;
7302 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7303 SET_TYPE_STRUCTURAL_EQUALITY (t);
7304 else if (TYPE_CANONICAL (to_type) != to_type)
7305 TYPE_CANONICAL (t)
7306 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7307 mode, can_alias_all);
7309 /* Lay out the type. This function has many callers that are concerned
7310 with expression-construction, and this simplifies them all. */
7311 layout_type (t);
7313 return t;
7316 /* By default build pointers in ptr_mode. */
7318 tree
7319 build_pointer_type (tree to_type)
7321 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7322 : TYPE_ADDR_SPACE (to_type);
7323 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7324 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7327 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7329 tree
7330 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
7331 bool can_alias_all)
7333 tree t;
7335 if (to_type == error_mark_node)
7336 return error_mark_node;
7338 /* If the pointed-to type has the may_alias attribute set, force
7339 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7340 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7341 can_alias_all = true;
7343 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7344 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7345 In that case, return that type without regard to the rest of our
7346 operands.
7348 ??? This is a kludge, but consistent with the way this function has
7349 always operated and there doesn't seem to be a good way to avoid this
7350 at the moment. */
7351 if (TYPE_REFERENCE_TO (to_type) != 0
7352 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7353 return TYPE_REFERENCE_TO (to_type);
7355 /* First, if we already have a type for pointers to TO_TYPE and it's
7356 the proper mode, use it. */
7357 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7358 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7359 return t;
7361 t = make_node (REFERENCE_TYPE);
7363 TREE_TYPE (t) = to_type;
7364 SET_TYPE_MODE (t, mode);
7365 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7366 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7367 TYPE_REFERENCE_TO (to_type) = t;
7369 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7370 SET_TYPE_STRUCTURAL_EQUALITY (t);
7371 else if (TYPE_CANONICAL (to_type) != to_type)
7372 TYPE_CANONICAL (t)
7373 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7374 mode, can_alias_all);
7376 layout_type (t);
7378 return t;
7382 /* Build the node for the type of references-to-TO_TYPE by default
7383 in ptr_mode. */
7385 tree
7386 build_reference_type (tree to_type)
7388 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7389 : TYPE_ADDR_SPACE (to_type);
7390 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7391 return build_reference_type_for_mode (to_type, pointer_mode, false);
7394 /* Build a type that is compatible with t but has no cv quals anywhere
7395 in its type, thus
7397 const char *const *const * -> char ***. */
7399 tree
7400 build_type_no_quals (tree t)
7402 switch (TREE_CODE (t))
7404 case POINTER_TYPE:
7405 return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7406 TYPE_MODE (t),
7407 TYPE_REF_CAN_ALIAS_ALL (t));
7408 case REFERENCE_TYPE:
7409 return
7410 build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7411 TYPE_MODE (t),
7412 TYPE_REF_CAN_ALIAS_ALL (t));
7413 default:
7414 return TYPE_MAIN_VARIANT (t);
7418 #define MAX_INT_CACHED_PREC \
7419 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7420 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7422 /* Builds a signed or unsigned integer type of precision PRECISION.
7423 Used for C bitfields whose precision does not match that of
7424 built-in target types. */
7425 tree
7426 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7427 int unsignedp)
7429 tree itype, ret;
7431 if (unsignedp)
7432 unsignedp = MAX_INT_CACHED_PREC + 1;
7434 if (precision <= MAX_INT_CACHED_PREC)
7436 itype = nonstandard_integer_type_cache[precision + unsignedp];
7437 if (itype)
7438 return itype;
7441 itype = make_node (INTEGER_TYPE);
7442 TYPE_PRECISION (itype) = precision;
7444 if (unsignedp)
7445 fixup_unsigned_type (itype);
7446 else
7447 fixup_signed_type (itype);
7449 ret = itype;
7450 if (host_integerp (TYPE_MAX_VALUE (itype), 1))
7451 ret = type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
7452 if (precision <= MAX_INT_CACHED_PREC)
7453 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7455 return ret;
7458 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7459 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7460 is true, reuse such a type that has already been constructed. */
7462 static tree
7463 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7465 tree itype = make_node (INTEGER_TYPE);
7466 hashval_t hashcode = 0;
7468 TREE_TYPE (itype) = type;
7470 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7471 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7473 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7474 SET_TYPE_MODE (itype, TYPE_MODE (type));
7475 TYPE_SIZE (itype) = TYPE_SIZE (type);
7476 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7477 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7478 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7480 if (!shared)
7481 return itype;
7483 if ((TYPE_MIN_VALUE (itype)
7484 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7485 || (TYPE_MAX_VALUE (itype)
7486 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7488 /* Since we cannot reliably merge this type, we need to compare it using
7489 structural equality checks. */
7490 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7491 return itype;
7494 hashcode = iterative_hash_expr (TYPE_MIN_VALUE (itype), hashcode);
7495 hashcode = iterative_hash_expr (TYPE_MAX_VALUE (itype), hashcode);
7496 hashcode = iterative_hash_hashval_t (TYPE_HASH (type), hashcode);
7497 itype = type_hash_canon (hashcode, itype);
7499 return itype;
7502 /* Wrapper around build_range_type_1 with SHARED set to true. */
7504 tree
7505 build_range_type (tree type, tree lowval, tree highval)
7507 return build_range_type_1 (type, lowval, highval, true);
7510 /* Wrapper around build_range_type_1 with SHARED set to false. */
7512 tree
7513 build_nonshared_range_type (tree type, tree lowval, tree highval)
7515 return build_range_type_1 (type, lowval, highval, false);
7518 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7519 MAXVAL should be the maximum value in the domain
7520 (one less than the length of the array).
7522 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7523 We don't enforce this limit, that is up to caller (e.g. language front end).
7524 The limit exists because the result is a signed type and we don't handle
7525 sizes that use more than one HOST_WIDE_INT. */
7527 tree
7528 build_index_type (tree maxval)
7530 return build_range_type (sizetype, size_zero_node, maxval);
7533 /* Return true if the debug information for TYPE, a subtype, should be emitted
7534 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7535 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7536 debug info and doesn't reflect the source code. */
7538 bool
7539 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7541 tree base_type = TREE_TYPE (type), low, high;
7543 /* Subrange types have a base type which is an integral type. */
7544 if (!INTEGRAL_TYPE_P (base_type))
7545 return false;
7547 /* Get the real bounds of the subtype. */
7548 if (lang_hooks.types.get_subrange_bounds)
7549 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7550 else
7552 low = TYPE_MIN_VALUE (type);
7553 high = TYPE_MAX_VALUE (type);
7556 /* If the type and its base type have the same representation and the same
7557 name, then the type is not a subrange but a copy of the base type. */
7558 if ((TREE_CODE (base_type) == INTEGER_TYPE
7559 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7560 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7561 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7562 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type)))
7564 tree type_name = TYPE_NAME (type);
7565 tree base_type_name = TYPE_NAME (base_type);
7567 if (type_name && TREE_CODE (type_name) == TYPE_DECL)
7568 type_name = DECL_NAME (type_name);
7570 if (base_type_name && TREE_CODE (base_type_name) == TYPE_DECL)
7571 base_type_name = DECL_NAME (base_type_name);
7573 if (type_name == base_type_name)
7574 return false;
7577 if (lowval)
7578 *lowval = low;
7579 if (highval)
7580 *highval = high;
7581 return true;
7584 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7585 and number of elements specified by the range of values of INDEX_TYPE.
7586 If SHARED is true, reuse such a type that has already been constructed. */
7588 static tree
7589 build_array_type_1 (tree elt_type, tree index_type, bool shared)
7591 tree t;
7593 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7595 error ("arrays of functions are not meaningful");
7596 elt_type = integer_type_node;
7599 t = make_node (ARRAY_TYPE);
7600 TREE_TYPE (t) = elt_type;
7601 TYPE_DOMAIN (t) = index_type;
7602 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7603 layout_type (t);
7605 /* If the element type is incomplete at this point we get marked for
7606 structural equality. Do not record these types in the canonical
7607 type hashtable. */
7608 if (TYPE_STRUCTURAL_EQUALITY_P (t))
7609 return t;
7611 if (shared)
7613 hashval_t hashcode = iterative_hash_object (TYPE_HASH (elt_type), 0);
7614 if (index_type)
7615 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
7616 t = type_hash_canon (hashcode, t);
7619 if (TYPE_CANONICAL (t) == t)
7621 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7622 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7623 SET_TYPE_STRUCTURAL_EQUALITY (t);
7624 else if (TYPE_CANONICAL (elt_type) != elt_type
7625 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7626 TYPE_CANONICAL (t)
7627 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7628 index_type
7629 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7630 shared);
7633 return t;
7636 /* Wrapper around build_array_type_1 with SHARED set to true. */
7638 tree
7639 build_array_type (tree elt_type, tree index_type)
7641 return build_array_type_1 (elt_type, index_type, true);
7644 /* Wrapper around build_array_type_1 with SHARED set to false. */
7646 tree
7647 build_nonshared_array_type (tree elt_type, tree index_type)
7649 return build_array_type_1 (elt_type, index_type, false);
7652 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7653 sizetype. */
7655 tree
7656 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
7658 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7661 /* Recursively examines the array elements of TYPE, until a non-array
7662 element type is found. */
7664 tree
7665 strip_array_types (tree type)
7667 while (TREE_CODE (type) == ARRAY_TYPE)
7668 type = TREE_TYPE (type);
7670 return type;
7673 /* Computes the canonical argument types from the argument type list
7674 ARGTYPES.
7676 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7677 on entry to this function, or if any of the ARGTYPES are
7678 structural.
7680 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7681 true on entry to this function, or if any of the ARGTYPES are
7682 non-canonical.
7684 Returns a canonical argument list, which may be ARGTYPES when the
7685 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7686 true) or would not differ from ARGTYPES. */
7688 static tree
7689 maybe_canonicalize_argtypes(tree argtypes,
7690 bool *any_structural_p,
7691 bool *any_noncanonical_p)
7693 tree arg;
7694 bool any_noncanonical_argtypes_p = false;
7696 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7698 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7699 /* Fail gracefully by stating that the type is structural. */
7700 *any_structural_p = true;
7701 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7702 *any_structural_p = true;
7703 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7704 || TREE_PURPOSE (arg))
7705 /* If the argument has a default argument, we consider it
7706 non-canonical even though the type itself is canonical.
7707 That way, different variants of function and method types
7708 with default arguments will all point to the variant with
7709 no defaults as their canonical type. */
7710 any_noncanonical_argtypes_p = true;
7713 if (*any_structural_p)
7714 return argtypes;
7716 if (any_noncanonical_argtypes_p)
7718 /* Build the canonical list of argument types. */
7719 tree canon_argtypes = NULL_TREE;
7720 bool is_void = false;
7722 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7724 if (arg == void_list_node)
7725 is_void = true;
7726 else
7727 canon_argtypes = tree_cons (NULL_TREE,
7728 TYPE_CANONICAL (TREE_VALUE (arg)),
7729 canon_argtypes);
7732 canon_argtypes = nreverse (canon_argtypes);
7733 if (is_void)
7734 canon_argtypes = chainon (canon_argtypes, void_list_node);
7736 /* There is a non-canonical type. */
7737 *any_noncanonical_p = true;
7738 return canon_argtypes;
7741 /* The canonical argument types are the same as ARGTYPES. */
7742 return argtypes;
7745 /* Construct, lay out and return
7746 the type of functions returning type VALUE_TYPE
7747 given arguments of types ARG_TYPES.
7748 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7749 are data type nodes for the arguments of the function.
7750 If such a type has already been constructed, reuse it. */
7752 tree
7753 build_function_type (tree value_type, tree arg_types)
7755 tree t;
7756 hashval_t hashcode = 0;
7757 bool any_structural_p, any_noncanonical_p;
7758 tree canon_argtypes;
7760 if (TREE_CODE (value_type) == FUNCTION_TYPE)
7762 error ("function return type cannot be function");
7763 value_type = integer_type_node;
7766 /* Make a node of the sort we want. */
7767 t = make_node (FUNCTION_TYPE);
7768 TREE_TYPE (t) = value_type;
7769 TYPE_ARG_TYPES (t) = arg_types;
7771 /* If we already have such a type, use the old one. */
7772 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
7773 hashcode = type_hash_list (arg_types, hashcode);
7774 t = type_hash_canon (hashcode, t);
7776 /* Set up the canonical type. */
7777 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7778 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7779 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
7780 &any_structural_p,
7781 &any_noncanonical_p);
7782 if (any_structural_p)
7783 SET_TYPE_STRUCTURAL_EQUALITY (t);
7784 else if (any_noncanonical_p)
7785 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
7786 canon_argtypes);
7788 if (!COMPLETE_TYPE_P (t))
7789 layout_type (t);
7790 return t;
7793 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP and the
7794 return value if SKIP_RETURN is true. */
7796 static tree
7797 build_function_type_skip_args (tree orig_type, bitmap args_to_skip,
7798 bool skip_return)
7800 tree new_type = NULL;
7801 tree args, new_args = NULL, t;
7802 tree new_reversed;
7803 int i = 0;
7805 for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
7806 args = TREE_CHAIN (args), i++)
7807 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
7808 new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
7810 new_reversed = nreverse (new_args);
7811 if (args)
7813 if (new_reversed)
7814 TREE_CHAIN (new_args) = void_list_node;
7815 else
7816 new_reversed = void_list_node;
7819 /* Use copy_node to preserve as much as possible from original type
7820 (debug info, attribute lists etc.)
7821 Exception is METHOD_TYPEs must have THIS argument.
7822 When we are asked to remove it, we need to build new FUNCTION_TYPE
7823 instead. */
7824 if (TREE_CODE (orig_type) != METHOD_TYPE
7825 || !args_to_skip
7826 || !bitmap_bit_p (args_to_skip, 0))
7828 new_type = build_distinct_type_copy (orig_type);
7829 TYPE_ARG_TYPES (new_type) = new_reversed;
7831 else
7833 new_type
7834 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
7835 new_reversed));
7836 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
7839 if (skip_return)
7840 TREE_TYPE (new_type) = void_type_node;
7842 /* This is a new type, not a copy of an old type. Need to reassociate
7843 variants. We can handle everything except the main variant lazily. */
7844 t = TYPE_MAIN_VARIANT (orig_type);
7845 if (t != orig_type)
7847 t = build_function_type_skip_args (t, args_to_skip, skip_return);
7848 TYPE_MAIN_VARIANT (new_type) = t;
7849 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
7850 TYPE_NEXT_VARIANT (t) = new_type;
7852 else
7854 TYPE_MAIN_VARIANT (new_type) = new_type;
7855 TYPE_NEXT_VARIANT (new_type) = NULL;
7858 return new_type;
7861 /* Build variant of function decl ORIG_DECL skipping ARGS_TO_SKIP and the
7862 return value if SKIP_RETURN is true.
7864 Arguments from DECL_ARGUMENTS list can't be removed now, since they are
7865 linked by TREE_CHAIN directly. The caller is responsible for eliminating
7866 them when they are being duplicated (i.e. copy_arguments_for_versioning). */
7868 tree
7869 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip,
7870 bool skip_return)
7872 tree new_decl = copy_node (orig_decl);
7873 tree new_type;
7875 new_type = TREE_TYPE (orig_decl);
7876 if (prototype_p (new_type)
7877 || (skip_return && !VOID_TYPE_P (TREE_TYPE (new_type))))
7878 new_type
7879 = build_function_type_skip_args (new_type, args_to_skip, skip_return);
7880 TREE_TYPE (new_decl) = new_type;
7882 /* For declarations setting DECL_VINDEX (i.e. methods)
7883 we expect first argument to be THIS pointer. */
7884 if (args_to_skip && bitmap_bit_p (args_to_skip, 0))
7885 DECL_VINDEX (new_decl) = NULL_TREE;
7887 /* When signature changes, we need to clear builtin info. */
7888 if (DECL_BUILT_IN (new_decl)
7889 && args_to_skip
7890 && !bitmap_empty_p (args_to_skip))
7892 DECL_BUILT_IN_CLASS (new_decl) = NOT_BUILT_IN;
7893 DECL_FUNCTION_CODE (new_decl) = (enum built_in_function) 0;
7895 return new_decl;
7898 /* Build a function type. The RETURN_TYPE is the type returned by the
7899 function. If VAARGS is set, no void_type_node is appended to the
7900 the list. ARGP must be always be terminated be a NULL_TREE. */
7902 static tree
7903 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
7905 tree t, args, last;
7907 t = va_arg (argp, tree);
7908 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
7909 args = tree_cons (NULL_TREE, t, args);
7911 if (vaargs)
7913 last = args;
7914 if (args != NULL_TREE)
7915 args = nreverse (args);
7916 gcc_assert (last != void_list_node);
7918 else if (args == NULL_TREE)
7919 args = void_list_node;
7920 else
7922 last = args;
7923 args = nreverse (args);
7924 TREE_CHAIN (last) = void_list_node;
7926 args = build_function_type (return_type, args);
7928 return args;
7931 /* Build a function type. The RETURN_TYPE is the type returned by the
7932 function. If additional arguments are provided, they are
7933 additional argument types. The list of argument types must always
7934 be terminated by NULL_TREE. */
7936 tree
7937 build_function_type_list (tree return_type, ...)
7939 tree args;
7940 va_list p;
7942 va_start (p, return_type);
7943 args = build_function_type_list_1 (false, return_type, p);
7944 va_end (p);
7945 return args;
7948 /* Build a variable argument function type. The RETURN_TYPE is the
7949 type returned by the function. If additional arguments are provided,
7950 they are additional argument types. The list of argument types must
7951 always be terminated by NULL_TREE. */
7953 tree
7954 build_varargs_function_type_list (tree return_type, ...)
7956 tree args;
7957 va_list p;
7959 va_start (p, return_type);
7960 args = build_function_type_list_1 (true, return_type, p);
7961 va_end (p);
7963 return args;
7966 /* Build a function type. RETURN_TYPE is the type returned by the
7967 function; VAARGS indicates whether the function takes varargs. The
7968 function takes N named arguments, the types of which are provided in
7969 ARG_TYPES. */
7971 static tree
7972 build_function_type_array_1 (bool vaargs, tree return_type, int n,
7973 tree *arg_types)
7975 int i;
7976 tree t = vaargs ? NULL_TREE : void_list_node;
7978 for (i = n - 1; i >= 0; i--)
7979 t = tree_cons (NULL_TREE, arg_types[i], t);
7981 return build_function_type (return_type, t);
7984 /* Build a function type. RETURN_TYPE is the type returned by the
7985 function. The function takes N named arguments, the types of which
7986 are provided in ARG_TYPES. */
7988 tree
7989 build_function_type_array (tree return_type, int n, tree *arg_types)
7991 return build_function_type_array_1 (false, return_type, n, arg_types);
7994 /* Build a variable argument function type. RETURN_TYPE is the type
7995 returned by the function. The function takes N named arguments, the
7996 types of which are provided in ARG_TYPES. */
7998 tree
7999 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8001 return build_function_type_array_1 (true, return_type, n, arg_types);
8004 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8005 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8006 for the method. An implicit additional parameter (of type
8007 pointer-to-BASETYPE) is added to the ARGTYPES. */
8009 tree
8010 build_method_type_directly (tree basetype,
8011 tree rettype,
8012 tree argtypes)
8014 tree t;
8015 tree ptype;
8016 int hashcode = 0;
8017 bool any_structural_p, any_noncanonical_p;
8018 tree canon_argtypes;
8020 /* Make a node of the sort we want. */
8021 t = make_node (METHOD_TYPE);
8023 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8024 TREE_TYPE (t) = rettype;
8025 ptype = build_pointer_type (basetype);
8027 /* The actual arglist for this function includes a "hidden" argument
8028 which is "this". Put it into the list of argument types. */
8029 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8030 TYPE_ARG_TYPES (t) = argtypes;
8032 /* If we already have such a type, use the old one. */
8033 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8034 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
8035 hashcode = type_hash_list (argtypes, hashcode);
8036 t = type_hash_canon (hashcode, t);
8038 /* Set up the canonical type. */
8039 any_structural_p
8040 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8041 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8042 any_noncanonical_p
8043 = (TYPE_CANONICAL (basetype) != basetype
8044 || TYPE_CANONICAL (rettype) != rettype);
8045 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8046 &any_structural_p,
8047 &any_noncanonical_p);
8048 if (any_structural_p)
8049 SET_TYPE_STRUCTURAL_EQUALITY (t);
8050 else if (any_noncanonical_p)
8051 TYPE_CANONICAL (t)
8052 = build_method_type_directly (TYPE_CANONICAL (basetype),
8053 TYPE_CANONICAL (rettype),
8054 canon_argtypes);
8055 if (!COMPLETE_TYPE_P (t))
8056 layout_type (t);
8058 return t;
8061 /* Construct, lay out and return the type of methods belonging to class
8062 BASETYPE and whose arguments and values are described by TYPE.
8063 If that type exists already, reuse it.
8064 TYPE must be a FUNCTION_TYPE node. */
8066 tree
8067 build_method_type (tree basetype, tree type)
8069 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8071 return build_method_type_directly (basetype,
8072 TREE_TYPE (type),
8073 TYPE_ARG_TYPES (type));
8076 /* Construct, lay out and return the type of offsets to a value
8077 of type TYPE, within an object of type BASETYPE.
8078 If a suitable offset type exists already, reuse it. */
8080 tree
8081 build_offset_type (tree basetype, tree type)
8083 tree t;
8084 hashval_t hashcode = 0;
8086 /* Make a node of the sort we want. */
8087 t = make_node (OFFSET_TYPE);
8089 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8090 TREE_TYPE (t) = type;
8092 /* If we already have such a type, use the old one. */
8093 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8094 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
8095 t = type_hash_canon (hashcode, t);
8097 if (!COMPLETE_TYPE_P (t))
8098 layout_type (t);
8100 if (TYPE_CANONICAL (t) == t)
8102 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8103 || TYPE_STRUCTURAL_EQUALITY_P (type))
8104 SET_TYPE_STRUCTURAL_EQUALITY (t);
8105 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8106 || TYPE_CANONICAL (type) != type)
8107 TYPE_CANONICAL (t)
8108 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8109 TYPE_CANONICAL (type));
8112 return t;
8115 /* Create a complex type whose components are COMPONENT_TYPE. */
8117 tree
8118 build_complex_type (tree component_type)
8120 tree t;
8121 hashval_t hashcode;
8123 gcc_assert (INTEGRAL_TYPE_P (component_type)
8124 || SCALAR_FLOAT_TYPE_P (component_type)
8125 || FIXED_POINT_TYPE_P (component_type));
8127 /* Make a node of the sort we want. */
8128 t = make_node (COMPLEX_TYPE);
8130 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8132 /* If we already have such a type, use the old one. */
8133 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
8134 t = type_hash_canon (hashcode, t);
8136 if (!COMPLETE_TYPE_P (t))
8137 layout_type (t);
8139 if (TYPE_CANONICAL (t) == t)
8141 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8142 SET_TYPE_STRUCTURAL_EQUALITY (t);
8143 else if (TYPE_CANONICAL (component_type) != component_type)
8144 TYPE_CANONICAL (t)
8145 = build_complex_type (TYPE_CANONICAL (component_type));
8148 /* We need to create a name, since complex is a fundamental type. */
8149 if (! TYPE_NAME (t))
8151 const char *name;
8152 if (component_type == char_type_node)
8153 name = "complex char";
8154 else if (component_type == signed_char_type_node)
8155 name = "complex signed char";
8156 else if (component_type == unsigned_char_type_node)
8157 name = "complex unsigned char";
8158 else if (component_type == short_integer_type_node)
8159 name = "complex short int";
8160 else if (component_type == short_unsigned_type_node)
8161 name = "complex short unsigned int";
8162 else if (component_type == integer_type_node)
8163 name = "complex int";
8164 else if (component_type == unsigned_type_node)
8165 name = "complex unsigned int";
8166 else if (component_type == long_integer_type_node)
8167 name = "complex long int";
8168 else if (component_type == long_unsigned_type_node)
8169 name = "complex long unsigned int";
8170 else if (component_type == long_long_integer_type_node)
8171 name = "complex long long int";
8172 else if (component_type == long_long_unsigned_type_node)
8173 name = "complex long long unsigned int";
8174 else
8175 name = 0;
8177 if (name != 0)
8178 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8179 get_identifier (name), t);
8182 return build_qualified_type (t, TYPE_QUALS (component_type));
8185 /* If TYPE is a real or complex floating-point type and the target
8186 does not directly support arithmetic on TYPE then return the wider
8187 type to be used for arithmetic on TYPE. Otherwise, return
8188 NULL_TREE. */
8190 tree
8191 excess_precision_type (tree type)
8193 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8195 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8196 switch (TREE_CODE (type))
8198 case REAL_TYPE:
8199 switch (flt_eval_method)
8201 case 1:
8202 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8203 return double_type_node;
8204 break;
8205 case 2:
8206 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8207 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8208 return long_double_type_node;
8209 break;
8210 default:
8211 gcc_unreachable ();
8213 break;
8214 case COMPLEX_TYPE:
8215 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8216 return NULL_TREE;
8217 switch (flt_eval_method)
8219 case 1:
8220 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8221 return complex_double_type_node;
8222 break;
8223 case 2:
8224 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8225 || (TYPE_MODE (TREE_TYPE (type))
8226 == TYPE_MODE (double_type_node)))
8227 return complex_long_double_type_node;
8228 break;
8229 default:
8230 gcc_unreachable ();
8232 break;
8233 default:
8234 break;
8237 return NULL_TREE;
8240 /* Return OP, stripped of any conversions to wider types as much as is safe.
8241 Converting the value back to OP's type makes a value equivalent to OP.
8243 If FOR_TYPE is nonzero, we return a value which, if converted to
8244 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8246 OP must have integer, real or enumeral type. Pointers are not allowed!
8248 There are some cases where the obvious value we could return
8249 would regenerate to OP if converted to OP's type,
8250 but would not extend like OP to wider types.
8251 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8252 For example, if OP is (unsigned short)(signed char)-1,
8253 we avoid returning (signed char)-1 if FOR_TYPE is int,
8254 even though extending that to an unsigned short would regenerate OP,
8255 since the result of extending (signed char)-1 to (int)
8256 is different from (int) OP. */
8258 tree
8259 get_unwidened (tree op, tree for_type)
8261 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8262 tree type = TREE_TYPE (op);
8263 unsigned final_prec
8264 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8265 int uns
8266 = (for_type != 0 && for_type != type
8267 && final_prec > TYPE_PRECISION (type)
8268 && TYPE_UNSIGNED (type));
8269 tree win = op;
8271 while (CONVERT_EXPR_P (op))
8273 int bitschange;
8275 /* TYPE_PRECISION on vector types has different meaning
8276 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8277 so avoid them here. */
8278 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8279 break;
8281 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8282 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8284 /* Truncations are many-one so cannot be removed.
8285 Unless we are later going to truncate down even farther. */
8286 if (bitschange < 0
8287 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8288 break;
8290 /* See what's inside this conversion. If we decide to strip it,
8291 we will set WIN. */
8292 op = TREE_OPERAND (op, 0);
8294 /* If we have not stripped any zero-extensions (uns is 0),
8295 we can strip any kind of extension.
8296 If we have previously stripped a zero-extension,
8297 only zero-extensions can safely be stripped.
8298 Any extension can be stripped if the bits it would produce
8299 are all going to be discarded later by truncating to FOR_TYPE. */
8301 if (bitschange > 0)
8303 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8304 win = op;
8305 /* TYPE_UNSIGNED says whether this is a zero-extension.
8306 Let's avoid computing it if it does not affect WIN
8307 and if UNS will not be needed again. */
8308 if ((uns
8309 || CONVERT_EXPR_P (op))
8310 && TYPE_UNSIGNED (TREE_TYPE (op)))
8312 uns = 1;
8313 win = op;
8318 /* If we finally reach a constant see if it fits in for_type and
8319 in that case convert it. */
8320 if (for_type
8321 && TREE_CODE (win) == INTEGER_CST
8322 && TREE_TYPE (win) != for_type
8323 && int_fits_type_p (win, for_type))
8324 win = fold_convert (for_type, win);
8326 return win;
8329 /* Return OP or a simpler expression for a narrower value
8330 which can be sign-extended or zero-extended to give back OP.
8331 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8332 or 0 if the value should be sign-extended. */
8334 tree
8335 get_narrower (tree op, int *unsignedp_ptr)
8337 int uns = 0;
8338 int first = 1;
8339 tree win = op;
8340 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8342 while (TREE_CODE (op) == NOP_EXPR)
8344 int bitschange
8345 = (TYPE_PRECISION (TREE_TYPE (op))
8346 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8348 /* Truncations are many-one so cannot be removed. */
8349 if (bitschange < 0)
8350 break;
8352 /* See what's inside this conversion. If we decide to strip it,
8353 we will set WIN. */
8355 if (bitschange > 0)
8357 op = TREE_OPERAND (op, 0);
8358 /* An extension: the outermost one can be stripped,
8359 but remember whether it is zero or sign extension. */
8360 if (first)
8361 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8362 /* Otherwise, if a sign extension has been stripped,
8363 only sign extensions can now be stripped;
8364 if a zero extension has been stripped, only zero-extensions. */
8365 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8366 break;
8367 first = 0;
8369 else /* bitschange == 0 */
8371 /* A change in nominal type can always be stripped, but we must
8372 preserve the unsignedness. */
8373 if (first)
8374 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8375 first = 0;
8376 op = TREE_OPERAND (op, 0);
8377 /* Keep trying to narrow, but don't assign op to win if it
8378 would turn an integral type into something else. */
8379 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8380 continue;
8383 win = op;
8386 if (TREE_CODE (op) == COMPONENT_REF
8387 /* Since type_for_size always gives an integer type. */
8388 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8389 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8390 /* Ensure field is laid out already. */
8391 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8392 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
8394 unsigned HOST_WIDE_INT innerprec
8395 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
8396 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8397 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8398 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8400 /* We can get this structure field in a narrower type that fits it,
8401 but the resulting extension to its nominal type (a fullword type)
8402 must satisfy the same conditions as for other extensions.
8404 Do this only for fields that are aligned (not bit-fields),
8405 because when bit-field insns will be used there is no
8406 advantage in doing this. */
8408 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8409 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8410 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8411 && type != 0)
8413 if (first)
8414 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8415 win = fold_convert (type, op);
8419 *unsignedp_ptr = uns;
8420 return win;
8423 /* Returns true if integer constant C has a value that is permissible
8424 for type TYPE (an INTEGER_TYPE). */
8426 bool
8427 int_fits_type_p (const_tree c, const_tree type)
8429 tree type_low_bound, type_high_bound;
8430 bool ok_for_low_bound, ok_for_high_bound, unsc;
8431 double_int dc, dd;
8433 dc = tree_to_double_int (c);
8434 unsc = TYPE_UNSIGNED (TREE_TYPE (c));
8436 retry:
8437 type_low_bound = TYPE_MIN_VALUE (type);
8438 type_high_bound = TYPE_MAX_VALUE (type);
8440 /* If at least one bound of the type is a constant integer, we can check
8441 ourselves and maybe make a decision. If no such decision is possible, but
8442 this type is a subtype, try checking against that. Otherwise, use
8443 double_int_fits_to_tree_p, which checks against the precision.
8445 Compute the status for each possibly constant bound, and return if we see
8446 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8447 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8448 for "constant known to fit". */
8450 /* Check if c >= type_low_bound. */
8451 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8453 dd = tree_to_double_int (type_low_bound);
8454 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
8456 int c_neg = (!unsc && dc.is_negative ());
8457 int t_neg = (unsc && dd.is_negative ());
8459 if (c_neg && !t_neg)
8460 return false;
8461 if ((c_neg || !t_neg) && dc.ult (dd))
8462 return false;
8464 else if (dc.cmp (dd, unsc) < 0)
8465 return false;
8466 ok_for_low_bound = true;
8468 else
8469 ok_for_low_bound = false;
8471 /* Check if c <= type_high_bound. */
8472 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8474 dd = tree_to_double_int (type_high_bound);
8475 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
8477 int c_neg = (!unsc && dc.is_negative ());
8478 int t_neg = (unsc && dd.is_negative ());
8480 if (t_neg && !c_neg)
8481 return false;
8482 if ((t_neg || !c_neg) && dc.ugt (dd))
8483 return false;
8485 else if (dc.cmp (dd, unsc) > 0)
8486 return false;
8487 ok_for_high_bound = true;
8489 else
8490 ok_for_high_bound = false;
8492 /* If the constant fits both bounds, the result is known. */
8493 if (ok_for_low_bound && ok_for_high_bound)
8494 return true;
8496 /* Perform some generic filtering which may allow making a decision
8497 even if the bounds are not constant. First, negative integers
8498 never fit in unsigned types, */
8499 if (TYPE_UNSIGNED (type) && !unsc && dc.is_negative ())
8500 return false;
8502 /* Second, narrower types always fit in wider ones. */
8503 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8504 return true;
8506 /* Third, unsigned integers with top bit set never fit signed types. */
8507 if (! TYPE_UNSIGNED (type) && unsc)
8509 int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (c))) - 1;
8510 if (prec < HOST_BITS_PER_WIDE_INT)
8512 if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
8513 return false;
8515 else if (((((unsigned HOST_WIDE_INT) 1)
8516 << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
8517 return false;
8520 /* If we haven't been able to decide at this point, there nothing more we
8521 can check ourselves here. Look at the base type if we have one and it
8522 has the same precision. */
8523 if (TREE_CODE (type) == INTEGER_TYPE
8524 && TREE_TYPE (type) != 0
8525 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8527 type = TREE_TYPE (type);
8528 goto retry;
8531 /* Or to double_int_fits_to_tree_p, if nothing else. */
8532 return double_int_fits_to_tree_p (type, dc);
8535 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8536 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8537 represented (assuming two's-complement arithmetic) within the bit
8538 precision of the type are returned instead. */
8540 void
8541 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8543 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8544 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8545 mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
8546 TYPE_UNSIGNED (type));
8547 else
8549 if (TYPE_UNSIGNED (type))
8550 mpz_set_ui (min, 0);
8551 else
8553 double_int mn;
8554 mn = double_int::mask (TYPE_PRECISION (type) - 1);
8555 mn = (mn + double_int_one).sext (TYPE_PRECISION (type));
8556 mpz_set_double_int (min, mn, false);
8560 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8561 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8562 mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
8563 TYPE_UNSIGNED (type));
8564 else
8566 if (TYPE_UNSIGNED (type))
8567 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)),
8568 true);
8569 else
8570 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type) - 1),
8571 true);
8575 /* Return true if VAR is an automatic variable defined in function FN. */
8577 bool
8578 auto_var_in_fn_p (const_tree var, const_tree fn)
8580 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8581 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8582 || TREE_CODE (var) == PARM_DECL)
8583 && ! TREE_STATIC (var))
8584 || TREE_CODE (var) == LABEL_DECL
8585 || TREE_CODE (var) == RESULT_DECL));
8588 /* Subprogram of following function. Called by walk_tree.
8590 Return *TP if it is an automatic variable or parameter of the
8591 function passed in as DATA. */
8593 static tree
8594 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8596 tree fn = (tree) data;
8598 if (TYPE_P (*tp))
8599 *walk_subtrees = 0;
8601 else if (DECL_P (*tp)
8602 && auto_var_in_fn_p (*tp, fn))
8603 return *tp;
8605 return NULL_TREE;
8608 /* Returns true if T is, contains, or refers to a type with variable
8609 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8610 arguments, but not the return type. If FN is nonzero, only return
8611 true if a modifier of the type or position of FN is a variable or
8612 parameter inside FN.
8614 This concept is more general than that of C99 'variably modified types':
8615 in C99, a struct type is never variably modified because a VLA may not
8616 appear as a structure member. However, in GNU C code like:
8618 struct S { int i[f()]; };
8620 is valid, and other languages may define similar constructs. */
8622 bool
8623 variably_modified_type_p (tree type, tree fn)
8625 tree t;
8627 /* Test if T is either variable (if FN is zero) or an expression containing
8628 a variable in FN. If TYPE isn't gimplified, return true also if
8629 gimplify_one_sizepos would gimplify the expression into a local
8630 variable. */
8631 #define RETURN_TRUE_IF_VAR(T) \
8632 do { tree _t = (T); \
8633 if (_t != NULL_TREE \
8634 && _t != error_mark_node \
8635 && TREE_CODE (_t) != INTEGER_CST \
8636 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8637 && (!fn \
8638 || (!TYPE_SIZES_GIMPLIFIED (type) \
8639 && !is_gimple_sizepos (_t)) \
8640 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8641 return true; } while (0)
8643 if (type == error_mark_node)
8644 return false;
8646 /* If TYPE itself has variable size, it is variably modified. */
8647 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8648 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8650 switch (TREE_CODE (type))
8652 case POINTER_TYPE:
8653 case REFERENCE_TYPE:
8654 case VECTOR_TYPE:
8655 if (variably_modified_type_p (TREE_TYPE (type), fn))
8656 return true;
8657 break;
8659 case FUNCTION_TYPE:
8660 case METHOD_TYPE:
8661 /* If TYPE is a function type, it is variably modified if the
8662 return type is variably modified. */
8663 if (variably_modified_type_p (TREE_TYPE (type), fn))
8664 return true;
8665 break;
8667 case INTEGER_TYPE:
8668 case REAL_TYPE:
8669 case FIXED_POINT_TYPE:
8670 case ENUMERAL_TYPE:
8671 case BOOLEAN_TYPE:
8672 /* Scalar types are variably modified if their end points
8673 aren't constant. */
8674 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8675 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8676 break;
8678 case RECORD_TYPE:
8679 case UNION_TYPE:
8680 case QUAL_UNION_TYPE:
8681 /* We can't see if any of the fields are variably-modified by the
8682 definition we normally use, since that would produce infinite
8683 recursion via pointers. */
8684 /* This is variably modified if some field's type is. */
8685 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8686 if (TREE_CODE (t) == FIELD_DECL)
8688 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8689 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8690 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8692 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8693 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8695 break;
8697 case ARRAY_TYPE:
8698 /* Do not call ourselves to avoid infinite recursion. This is
8699 variably modified if the element type is. */
8700 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8701 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8702 break;
8704 default:
8705 break;
8708 /* The current language may have other cases to check, but in general,
8709 all other types are not variably modified. */
8710 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8712 #undef RETURN_TRUE_IF_VAR
8715 /* Given a DECL or TYPE, return the scope in which it was declared, or
8716 NULL_TREE if there is no containing scope. */
8718 tree
8719 get_containing_scope (const_tree t)
8721 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8724 /* Return the innermost context enclosing DECL that is
8725 a FUNCTION_DECL, or zero if none. */
8727 tree
8728 decl_function_context (const_tree decl)
8730 tree context;
8732 if (TREE_CODE (decl) == ERROR_MARK)
8733 return 0;
8735 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8736 where we look up the function at runtime. Such functions always take
8737 a first argument of type 'pointer to real context'.
8739 C++ should really be fixed to use DECL_CONTEXT for the real context,
8740 and use something else for the "virtual context". */
8741 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8742 context
8743 = TYPE_MAIN_VARIANT
8744 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8745 else
8746 context = DECL_CONTEXT (decl);
8748 while (context && TREE_CODE (context) != FUNCTION_DECL)
8750 if (TREE_CODE (context) == BLOCK)
8751 context = BLOCK_SUPERCONTEXT (context);
8752 else
8753 context = get_containing_scope (context);
8756 return context;
8759 /* Return the innermost context enclosing DECL that is
8760 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8761 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8763 tree
8764 decl_type_context (const_tree decl)
8766 tree context = DECL_CONTEXT (decl);
8768 while (context)
8769 switch (TREE_CODE (context))
8771 case NAMESPACE_DECL:
8772 case TRANSLATION_UNIT_DECL:
8773 return NULL_TREE;
8775 case RECORD_TYPE:
8776 case UNION_TYPE:
8777 case QUAL_UNION_TYPE:
8778 return context;
8780 case TYPE_DECL:
8781 case FUNCTION_DECL:
8782 context = DECL_CONTEXT (context);
8783 break;
8785 case BLOCK:
8786 context = BLOCK_SUPERCONTEXT (context);
8787 break;
8789 default:
8790 gcc_unreachable ();
8793 return NULL_TREE;
8796 /* CALL is a CALL_EXPR. Return the declaration for the function
8797 called, or NULL_TREE if the called function cannot be
8798 determined. */
8800 tree
8801 get_callee_fndecl (const_tree call)
8803 tree addr;
8805 if (call == error_mark_node)
8806 return error_mark_node;
8808 /* It's invalid to call this function with anything but a
8809 CALL_EXPR. */
8810 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8812 /* The first operand to the CALL is the address of the function
8813 called. */
8814 addr = CALL_EXPR_FN (call);
8816 STRIP_NOPS (addr);
8818 /* If this is a readonly function pointer, extract its initial value. */
8819 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8820 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8821 && DECL_INITIAL (addr))
8822 addr = DECL_INITIAL (addr);
8824 /* If the address is just `&f' for some function `f', then we know
8825 that `f' is being called. */
8826 if (TREE_CODE (addr) == ADDR_EXPR
8827 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8828 return TREE_OPERAND (addr, 0);
8830 /* We couldn't figure out what was being called. */
8831 return NULL_TREE;
8834 /* Print debugging information about tree nodes generated during the compile,
8835 and any language-specific information. */
8837 void
8838 dump_tree_statistics (void)
8840 if (GATHER_STATISTICS)
8842 int i;
8843 int total_nodes, total_bytes;
8844 fprintf (stderr, "Kind Nodes Bytes\n");
8845 fprintf (stderr, "---------------------------------------\n");
8846 total_nodes = total_bytes = 0;
8847 for (i = 0; i < (int) all_kinds; i++)
8849 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
8850 tree_node_counts[i], tree_node_sizes[i]);
8851 total_nodes += tree_node_counts[i];
8852 total_bytes += tree_node_sizes[i];
8854 fprintf (stderr, "---------------------------------------\n");
8855 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
8856 fprintf (stderr, "---------------------------------------\n");
8857 fprintf (stderr, "Code Nodes\n");
8858 fprintf (stderr, "----------------------------\n");
8859 for (i = 0; i < (int) MAX_TREE_CODES; i++)
8860 fprintf (stderr, "%-20s %7d\n", tree_code_name[i], tree_code_counts[i]);
8861 fprintf (stderr, "----------------------------\n");
8862 ssanames_print_statistics ();
8863 phinodes_print_statistics ();
8865 else
8866 fprintf (stderr, "(No per-node statistics)\n");
8868 print_type_hash_statistics ();
8869 print_debug_expr_statistics ();
8870 print_value_expr_statistics ();
8871 lang_hooks.print_statistics ();
8874 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8876 /* Generate a crc32 of a byte. */
8878 static unsigned
8879 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
8881 unsigned ix;
8883 for (ix = bits; ix--; value <<= 1)
8885 unsigned feedback;
8887 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
8888 chksum <<= 1;
8889 chksum ^= feedback;
8891 return chksum;
8894 /* Generate a crc32 of a 32-bit unsigned. */
8896 unsigned
8897 crc32_unsigned (unsigned chksum, unsigned value)
8899 return crc32_unsigned_bits (chksum, value, 32);
8902 /* Generate a crc32 of a byte. */
8904 unsigned
8905 crc32_byte (unsigned chksum, char byte)
8907 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
8910 /* Generate a crc32 of a string. */
8912 unsigned
8913 crc32_string (unsigned chksum, const char *string)
8917 chksum = crc32_byte (chksum, *string);
8919 while (*string++);
8920 return chksum;
8923 /* P is a string that will be used in a symbol. Mask out any characters
8924 that are not valid in that context. */
8926 void
8927 clean_symbol_name (char *p)
8929 for (; *p; p++)
8930 if (! (ISALNUM (*p)
8931 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
8932 || *p == '$'
8933 #endif
8934 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
8935 || *p == '.'
8936 #endif
8938 *p = '_';
8941 /* Generate a name for a special-purpose function.
8942 The generated name may need to be unique across the whole link.
8943 Changes to this function may also require corresponding changes to
8944 xstrdup_mask_random.
8945 TYPE is some string to identify the purpose of this function to the
8946 linker or collect2; it must start with an uppercase letter,
8947 one of:
8948 I - for constructors
8949 D - for destructors
8950 N - for C++ anonymous namespaces
8951 F - for DWARF unwind frame information. */
8953 tree
8954 get_file_function_name (const char *type)
8956 char *buf;
8957 const char *p;
8958 char *q;
8960 /* If we already have a name we know to be unique, just use that. */
8961 if (first_global_object_name)
8962 p = q = ASTRDUP (first_global_object_name);
8963 /* If the target is handling the constructors/destructors, they
8964 will be local to this file and the name is only necessary for
8965 debugging purposes.
8966 We also assign sub_I and sub_D sufixes to constructors called from
8967 the global static constructors. These are always local. */
8968 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
8969 || (strncmp (type, "sub_", 4) == 0
8970 && (type[4] == 'I' || type[4] == 'D')))
8972 const char *file = main_input_filename;
8973 if (! file)
8974 file = input_filename;
8975 /* Just use the file's basename, because the full pathname
8976 might be quite long. */
8977 p = q = ASTRDUP (lbasename (file));
8979 else
8981 /* Otherwise, the name must be unique across the entire link.
8982 We don't have anything that we know to be unique to this translation
8983 unit, so use what we do have and throw in some randomness. */
8984 unsigned len;
8985 const char *name = weak_global_object_name;
8986 const char *file = main_input_filename;
8988 if (! name)
8989 name = "";
8990 if (! file)
8991 file = input_filename;
8993 len = strlen (file);
8994 q = (char *) alloca (9 + 17 + len + 1);
8995 memcpy (q, file, len + 1);
8997 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
8998 crc32_string (0, name), get_random_seed (false));
9000 p = q;
9003 clean_symbol_name (q);
9004 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9005 + strlen (type));
9007 /* Set up the name of the file-level functions we may need.
9008 Use a global object (which is already required to be unique over
9009 the program) rather than the file name (which imposes extra
9010 constraints). */
9011 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9013 return get_identifier (buf);
9016 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9018 /* Complain that the tree code of NODE does not match the expected 0
9019 terminated list of trailing codes. The trailing code list can be
9020 empty, for a more vague error message. FILE, LINE, and FUNCTION
9021 are of the caller. */
9023 void
9024 tree_check_failed (const_tree node, const char *file,
9025 int line, const char *function, ...)
9027 va_list args;
9028 const char *buffer;
9029 unsigned length = 0;
9030 int code;
9032 va_start (args, function);
9033 while ((code = va_arg (args, int)))
9034 length += 4 + strlen (tree_code_name[code]);
9035 va_end (args);
9036 if (length)
9038 char *tmp;
9039 va_start (args, function);
9040 length += strlen ("expected ");
9041 buffer = tmp = (char *) alloca (length);
9042 length = 0;
9043 while ((code = va_arg (args, int)))
9045 const char *prefix = length ? " or " : "expected ";
9047 strcpy (tmp + length, prefix);
9048 length += strlen (prefix);
9049 strcpy (tmp + length, tree_code_name[code]);
9050 length += strlen (tree_code_name[code]);
9052 va_end (args);
9054 else
9055 buffer = "unexpected node";
9057 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9058 buffer, tree_code_name[TREE_CODE (node)],
9059 function, trim_filename (file), line);
9062 /* Complain that the tree code of NODE does match the expected 0
9063 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9064 the caller. */
9066 void
9067 tree_not_check_failed (const_tree node, const char *file,
9068 int line, const char *function, ...)
9070 va_list args;
9071 char *buffer;
9072 unsigned length = 0;
9073 int code;
9075 va_start (args, function);
9076 while ((code = va_arg (args, int)))
9077 length += 4 + strlen (tree_code_name[code]);
9078 va_end (args);
9079 va_start (args, function);
9080 buffer = (char *) alloca (length);
9081 length = 0;
9082 while ((code = va_arg (args, int)))
9084 if (length)
9086 strcpy (buffer + length, " or ");
9087 length += 4;
9089 strcpy (buffer + length, tree_code_name[code]);
9090 length += strlen (tree_code_name[code]);
9092 va_end (args);
9094 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9095 buffer, tree_code_name[TREE_CODE (node)],
9096 function, trim_filename (file), line);
9099 /* Similar to tree_check_failed, except that we check for a class of tree
9100 code, given in CL. */
9102 void
9103 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9104 const char *file, int line, const char *function)
9106 internal_error
9107 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9108 TREE_CODE_CLASS_STRING (cl),
9109 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9110 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9113 /* Similar to tree_check_failed, except that instead of specifying a
9114 dozen codes, use the knowledge that they're all sequential. */
9116 void
9117 tree_range_check_failed (const_tree node, const char *file, int line,
9118 const char *function, enum tree_code c1,
9119 enum tree_code c2)
9121 char *buffer;
9122 unsigned length = 0;
9123 unsigned int c;
9125 for (c = c1; c <= c2; ++c)
9126 length += 4 + strlen (tree_code_name[c]);
9128 length += strlen ("expected ");
9129 buffer = (char *) alloca (length);
9130 length = 0;
9132 for (c = c1; c <= c2; ++c)
9134 const char *prefix = length ? " or " : "expected ";
9136 strcpy (buffer + length, prefix);
9137 length += strlen (prefix);
9138 strcpy (buffer + length, tree_code_name[c]);
9139 length += strlen (tree_code_name[c]);
9142 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9143 buffer, tree_code_name[TREE_CODE (node)],
9144 function, trim_filename (file), line);
9148 /* Similar to tree_check_failed, except that we check that a tree does
9149 not have the specified code, given in CL. */
9151 void
9152 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9153 const char *file, int line, const char *function)
9155 internal_error
9156 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9157 TREE_CODE_CLASS_STRING (cl),
9158 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9159 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9163 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9165 void
9166 omp_clause_check_failed (const_tree node, const char *file, int line,
9167 const char *function, enum omp_clause_code code)
9169 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9170 omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
9171 function, trim_filename (file), line);
9175 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9177 void
9178 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9179 const char *function, enum omp_clause_code c1,
9180 enum omp_clause_code c2)
9182 char *buffer;
9183 unsigned length = 0;
9184 unsigned int c;
9186 for (c = c1; c <= c2; ++c)
9187 length += 4 + strlen (omp_clause_code_name[c]);
9189 length += strlen ("expected ");
9190 buffer = (char *) alloca (length);
9191 length = 0;
9193 for (c = c1; c <= c2; ++c)
9195 const char *prefix = length ? " or " : "expected ";
9197 strcpy (buffer + length, prefix);
9198 length += strlen (prefix);
9199 strcpy (buffer + length, omp_clause_code_name[c]);
9200 length += strlen (omp_clause_code_name[c]);
9203 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9204 buffer, omp_clause_code_name[TREE_CODE (node)],
9205 function, trim_filename (file), line);
9209 #undef DEFTREESTRUCT
9210 #define DEFTREESTRUCT(VAL, NAME) NAME,
9212 static const char *ts_enum_names[] = {
9213 #include "treestruct.def"
9215 #undef DEFTREESTRUCT
9217 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9219 /* Similar to tree_class_check_failed, except that we check for
9220 whether CODE contains the tree structure identified by EN. */
9222 void
9223 tree_contains_struct_check_failed (const_tree node,
9224 const enum tree_node_structure_enum en,
9225 const char *file, int line,
9226 const char *function)
9228 internal_error
9229 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9230 TS_ENUM_NAME(en),
9231 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9235 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9236 (dynamically sized) vector. */
9238 void
9239 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9240 const char *function)
9242 internal_error
9243 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9244 idx + 1, len, function, trim_filename (file), line);
9247 /* Similar to above, except that the check is for the bounds of the operand
9248 vector of an expression node EXP. */
9250 void
9251 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9252 int line, const char *function)
9254 int code = TREE_CODE (exp);
9255 internal_error
9256 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9257 idx + 1, tree_code_name[code], TREE_OPERAND_LENGTH (exp),
9258 function, trim_filename (file), line);
9261 /* Similar to above, except that the check is for the number of
9262 operands of an OMP_CLAUSE node. */
9264 void
9265 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9266 int line, const char *function)
9268 internal_error
9269 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9270 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9271 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9272 trim_filename (file), line);
9274 #endif /* ENABLE_TREE_CHECKING */
9276 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9277 and mapped to the machine mode MODE. Initialize its fields and build
9278 the information necessary for debugging output. */
9280 static tree
9281 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
9283 tree t;
9284 hashval_t hashcode = 0;
9286 t = make_node (VECTOR_TYPE);
9287 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9288 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9289 SET_TYPE_MODE (t, mode);
9291 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9292 SET_TYPE_STRUCTURAL_EQUALITY (t);
9293 else if (TYPE_CANONICAL (innertype) != innertype
9294 || mode != VOIDmode)
9295 TYPE_CANONICAL (t)
9296 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9298 layout_type (t);
9300 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
9301 hashcode = iterative_hash_host_wide_int (nunits, hashcode);
9302 hashcode = iterative_hash_host_wide_int (mode, hashcode);
9303 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (t)), hashcode);
9304 t = type_hash_canon (hashcode, t);
9306 /* We have built a main variant, based on the main variant of the
9307 inner type. Use it to build the variant we return. */
9308 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9309 && TREE_TYPE (t) != innertype)
9310 return build_type_attribute_qual_variant (t,
9311 TYPE_ATTRIBUTES (innertype),
9312 TYPE_QUALS (innertype));
9314 return t;
9317 static tree
9318 make_or_reuse_type (unsigned size, int unsignedp)
9320 if (size == INT_TYPE_SIZE)
9321 return unsignedp ? unsigned_type_node : integer_type_node;
9322 if (size == CHAR_TYPE_SIZE)
9323 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9324 if (size == SHORT_TYPE_SIZE)
9325 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9326 if (size == LONG_TYPE_SIZE)
9327 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9328 if (size == LONG_LONG_TYPE_SIZE)
9329 return (unsignedp ? long_long_unsigned_type_node
9330 : long_long_integer_type_node);
9331 if (size == 128 && int128_integer_type_node)
9332 return (unsignedp ? int128_unsigned_type_node
9333 : int128_integer_type_node);
9335 if (unsignedp)
9336 return make_unsigned_type (size);
9337 else
9338 return make_signed_type (size);
9341 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9343 static tree
9344 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9346 if (satp)
9348 if (size == SHORT_FRACT_TYPE_SIZE)
9349 return unsignedp ? sat_unsigned_short_fract_type_node
9350 : sat_short_fract_type_node;
9351 if (size == FRACT_TYPE_SIZE)
9352 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9353 if (size == LONG_FRACT_TYPE_SIZE)
9354 return unsignedp ? sat_unsigned_long_fract_type_node
9355 : sat_long_fract_type_node;
9356 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9357 return unsignedp ? sat_unsigned_long_long_fract_type_node
9358 : sat_long_long_fract_type_node;
9360 else
9362 if (size == SHORT_FRACT_TYPE_SIZE)
9363 return unsignedp ? unsigned_short_fract_type_node
9364 : short_fract_type_node;
9365 if (size == FRACT_TYPE_SIZE)
9366 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9367 if (size == LONG_FRACT_TYPE_SIZE)
9368 return unsignedp ? unsigned_long_fract_type_node
9369 : long_fract_type_node;
9370 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9371 return unsignedp ? unsigned_long_long_fract_type_node
9372 : long_long_fract_type_node;
9375 return make_fract_type (size, unsignedp, satp);
9378 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9380 static tree
9381 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9383 if (satp)
9385 if (size == SHORT_ACCUM_TYPE_SIZE)
9386 return unsignedp ? sat_unsigned_short_accum_type_node
9387 : sat_short_accum_type_node;
9388 if (size == ACCUM_TYPE_SIZE)
9389 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9390 if (size == LONG_ACCUM_TYPE_SIZE)
9391 return unsignedp ? sat_unsigned_long_accum_type_node
9392 : sat_long_accum_type_node;
9393 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9394 return unsignedp ? sat_unsigned_long_long_accum_type_node
9395 : sat_long_long_accum_type_node;
9397 else
9399 if (size == SHORT_ACCUM_TYPE_SIZE)
9400 return unsignedp ? unsigned_short_accum_type_node
9401 : short_accum_type_node;
9402 if (size == ACCUM_TYPE_SIZE)
9403 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9404 if (size == LONG_ACCUM_TYPE_SIZE)
9405 return unsignedp ? unsigned_long_accum_type_node
9406 : long_accum_type_node;
9407 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9408 return unsignedp ? unsigned_long_long_accum_type_node
9409 : long_long_accum_type_node;
9412 return make_accum_type (size, unsignedp, satp);
9415 /* Create nodes for all integer types (and error_mark_node) using the sizes
9416 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9417 SHORT_DOUBLE specifies whether double should be of the same precision
9418 as float. */
9420 void
9421 build_common_tree_nodes (bool signed_char, bool short_double)
9423 error_mark_node = make_node (ERROR_MARK);
9424 TREE_TYPE (error_mark_node) = error_mark_node;
9426 initialize_sizetypes ();
9428 /* Define both `signed char' and `unsigned char'. */
9429 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9430 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9431 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9432 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9434 /* Define `char', which is like either `signed char' or `unsigned char'
9435 but not the same as either. */
9436 char_type_node
9437 = (signed_char
9438 ? make_signed_type (CHAR_TYPE_SIZE)
9439 : make_unsigned_type (CHAR_TYPE_SIZE));
9440 TYPE_STRING_FLAG (char_type_node) = 1;
9442 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9443 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9444 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9445 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9446 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9447 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9448 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9449 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9450 #if HOST_BITS_PER_WIDE_INT >= 64
9451 /* TODO: This isn't correct, but as logic depends at the moment on
9452 host's instead of target's wide-integer.
9453 If there is a target not supporting TImode, but has an 128-bit
9454 integer-scalar register, this target check needs to be adjusted. */
9455 if (targetm.scalar_mode_supported_p (TImode))
9457 int128_integer_type_node = make_signed_type (128);
9458 int128_unsigned_type_node = make_unsigned_type (128);
9460 #endif
9462 /* Define a boolean type. This type only represents boolean values but
9463 may be larger than char depending on the value of BOOL_TYPE_SIZE.
9464 Front ends which want to override this size (i.e. Java) can redefine
9465 boolean_type_node before calling build_common_tree_nodes_2. */
9466 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9467 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9468 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9469 TYPE_PRECISION (boolean_type_node) = 1;
9471 /* Define what type to use for size_t. */
9472 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9473 size_type_node = unsigned_type_node;
9474 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9475 size_type_node = long_unsigned_type_node;
9476 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9477 size_type_node = long_long_unsigned_type_node;
9478 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9479 size_type_node = short_unsigned_type_node;
9480 else
9481 gcc_unreachable ();
9483 /* Fill in the rest of the sized types. Reuse existing type nodes
9484 when possible. */
9485 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9486 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9487 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9488 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9489 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9491 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9492 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9493 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9494 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9495 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9497 access_public_node = get_identifier ("public");
9498 access_protected_node = get_identifier ("protected");
9499 access_private_node = get_identifier ("private");
9501 /* Define these next since types below may used them. */
9502 integer_zero_node = build_int_cst (integer_type_node, 0);
9503 integer_one_node = build_int_cst (integer_type_node, 1);
9504 integer_three_node = build_int_cst (integer_type_node, 3);
9505 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9507 size_zero_node = size_int (0);
9508 size_one_node = size_int (1);
9509 bitsize_zero_node = bitsize_int (0);
9510 bitsize_one_node = bitsize_int (1);
9511 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9513 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9514 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9516 void_type_node = make_node (VOID_TYPE);
9517 layout_type (void_type_node);
9519 /* We are not going to have real types in C with less than byte alignment,
9520 so we might as well not have any types that claim to have it. */
9521 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9522 TYPE_USER_ALIGN (void_type_node) = 0;
9524 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9525 layout_type (TREE_TYPE (null_pointer_node));
9527 ptr_type_node = build_pointer_type (void_type_node);
9528 const_ptr_type_node
9529 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9530 fileptr_type_node = ptr_type_node;
9532 float_type_node = make_node (REAL_TYPE);
9533 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9534 layout_type (float_type_node);
9536 double_type_node = make_node (REAL_TYPE);
9537 if (short_double)
9538 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9539 else
9540 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9541 layout_type (double_type_node);
9543 long_double_type_node = make_node (REAL_TYPE);
9544 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9545 layout_type (long_double_type_node);
9547 float_ptr_type_node = build_pointer_type (float_type_node);
9548 double_ptr_type_node = build_pointer_type (double_type_node);
9549 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9550 integer_ptr_type_node = build_pointer_type (integer_type_node);
9552 /* Fixed size integer types. */
9553 uint16_type_node = build_nonstandard_integer_type (16, true);
9554 uint32_type_node = build_nonstandard_integer_type (32, true);
9555 uint64_type_node = build_nonstandard_integer_type (64, true);
9557 /* Decimal float types. */
9558 dfloat32_type_node = make_node (REAL_TYPE);
9559 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9560 layout_type (dfloat32_type_node);
9561 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9562 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9564 dfloat64_type_node = make_node (REAL_TYPE);
9565 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9566 layout_type (dfloat64_type_node);
9567 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9568 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9570 dfloat128_type_node = make_node (REAL_TYPE);
9571 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9572 layout_type (dfloat128_type_node);
9573 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9574 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9576 complex_integer_type_node = build_complex_type (integer_type_node);
9577 complex_float_type_node = build_complex_type (float_type_node);
9578 complex_double_type_node = build_complex_type (double_type_node);
9579 complex_long_double_type_node = build_complex_type (long_double_type_node);
9581 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9582 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9583 sat_ ## KIND ## _type_node = \
9584 make_sat_signed_ ## KIND ## _type (SIZE); \
9585 sat_unsigned_ ## KIND ## _type_node = \
9586 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9587 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9588 unsigned_ ## KIND ## _type_node = \
9589 make_unsigned_ ## KIND ## _type (SIZE);
9591 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9592 sat_ ## WIDTH ## KIND ## _type_node = \
9593 make_sat_signed_ ## KIND ## _type (SIZE); \
9594 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9595 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9596 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9597 unsigned_ ## WIDTH ## KIND ## _type_node = \
9598 make_unsigned_ ## KIND ## _type (SIZE);
9600 /* Make fixed-point type nodes based on four different widths. */
9601 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9602 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9603 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9604 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9605 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9607 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9608 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9609 NAME ## _type_node = \
9610 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9611 u ## NAME ## _type_node = \
9612 make_or_reuse_unsigned_ ## KIND ## _type \
9613 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9614 sat_ ## NAME ## _type_node = \
9615 make_or_reuse_sat_signed_ ## KIND ## _type \
9616 (GET_MODE_BITSIZE (MODE ## mode)); \
9617 sat_u ## NAME ## _type_node = \
9618 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9619 (GET_MODE_BITSIZE (U ## MODE ## mode));
9621 /* Fixed-point type and mode nodes. */
9622 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9623 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9624 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9625 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9626 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9627 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9628 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9629 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9630 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9631 MAKE_FIXED_MODE_NODE (accum, da, DA)
9632 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9635 tree t = targetm.build_builtin_va_list ();
9637 /* Many back-ends define record types without setting TYPE_NAME.
9638 If we copied the record type here, we'd keep the original
9639 record type without a name. This breaks name mangling. So,
9640 don't copy record types and let c_common_nodes_and_builtins()
9641 declare the type to be __builtin_va_list. */
9642 if (TREE_CODE (t) != RECORD_TYPE)
9643 t = build_variant_type_copy (t);
9645 va_list_type_node = t;
9649 /* Modify DECL for given flags. */
9650 void
9651 set_call_expr_flags (tree decl, int flags)
9653 if (flags & ECF_NOTHROW)
9654 TREE_NOTHROW (decl) = 1;
9655 if (flags & ECF_CONST)
9656 TREE_READONLY (decl) = 1;
9657 if (flags & ECF_PURE)
9658 DECL_PURE_P (decl) = 1;
9659 if (flags & ECF_LOOPING_CONST_OR_PURE)
9660 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9661 if (flags & ECF_NOVOPS)
9662 DECL_IS_NOVOPS (decl) = 1;
9663 if (flags & ECF_NORETURN)
9664 TREE_THIS_VOLATILE (decl) = 1;
9665 if (flags & ECF_MALLOC)
9666 DECL_IS_MALLOC (decl) = 1;
9667 if (flags & ECF_RETURNS_TWICE)
9668 DECL_IS_RETURNS_TWICE (decl) = 1;
9669 if (flags & ECF_LEAF)
9670 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9671 NULL, DECL_ATTRIBUTES (decl));
9672 if ((flags & ECF_TM_PURE) && flag_tm)
9673 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("transaction_pure"),
9674 NULL, DECL_ATTRIBUTES (decl));
9675 /* Looping const or pure is implied by noreturn.
9676 There is currently no way to declare looping const or looping pure alone. */
9677 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9678 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9682 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9684 static void
9685 local_define_builtin (const char *name, tree type, enum built_in_function code,
9686 const char *library_name, int ecf_flags)
9688 tree decl;
9690 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9691 library_name, NULL_TREE);
9692 set_call_expr_flags (decl, ecf_flags);
9694 set_builtin_decl (code, decl, true);
9697 /* Call this function after instantiating all builtins that the language
9698 front end cares about. This will build the rest of the builtins that
9699 are relied upon by the tree optimizers and the middle-end. */
9701 void
9702 build_common_builtin_nodes (void)
9704 tree tmp, ftype;
9705 int ecf_flags;
9707 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9709 ftype = build_function_type (void_type_node, void_list_node);
9710 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
9711 "__builtin_unreachable",
9712 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9713 | ECF_CONST | ECF_LEAF);
9716 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9717 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9719 ftype = build_function_type_list (ptr_type_node,
9720 ptr_type_node, const_ptr_type_node,
9721 size_type_node, NULL_TREE);
9723 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
9724 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9725 "memcpy", ECF_NOTHROW | ECF_LEAF);
9726 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9727 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9728 "memmove", ECF_NOTHROW | ECF_LEAF);
9731 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
9733 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9734 const_ptr_type_node, size_type_node,
9735 NULL_TREE);
9736 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9737 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9740 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
9742 ftype = build_function_type_list (ptr_type_node,
9743 ptr_type_node, integer_type_node,
9744 size_type_node, NULL_TREE);
9745 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9746 "memset", ECF_NOTHROW | ECF_LEAF);
9749 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
9751 ftype = build_function_type_list (ptr_type_node,
9752 size_type_node, NULL_TREE);
9753 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9754 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9757 ftype = build_function_type_list (ptr_type_node, size_type_node,
9758 size_type_node, NULL_TREE);
9759 local_define_builtin ("__builtin_alloca_with_align", ftype,
9760 BUILT_IN_ALLOCA_WITH_ALIGN, "alloca",
9761 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9763 /* If we're checking the stack, `alloca' can throw. */
9764 if (flag_stack_check)
9766 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
9767 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
9770 ftype = build_function_type_list (void_type_node,
9771 ptr_type_node, ptr_type_node,
9772 ptr_type_node, NULL_TREE);
9773 local_define_builtin ("__builtin_init_trampoline", ftype,
9774 BUILT_IN_INIT_TRAMPOLINE,
9775 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9776 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
9777 BUILT_IN_INIT_HEAP_TRAMPOLINE,
9778 "__builtin_init_heap_trampoline",
9779 ECF_NOTHROW | ECF_LEAF);
9781 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9782 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9783 BUILT_IN_ADJUST_TRAMPOLINE,
9784 "__builtin_adjust_trampoline",
9785 ECF_CONST | ECF_NOTHROW);
9787 ftype = build_function_type_list (void_type_node,
9788 ptr_type_node, ptr_type_node, NULL_TREE);
9789 local_define_builtin ("__builtin_nonlocal_goto", ftype,
9790 BUILT_IN_NONLOCAL_GOTO,
9791 "__builtin_nonlocal_goto",
9792 ECF_NORETURN | ECF_NOTHROW);
9794 ftype = build_function_type_list (void_type_node,
9795 ptr_type_node, ptr_type_node, NULL_TREE);
9796 local_define_builtin ("__builtin_setjmp_setup", ftype,
9797 BUILT_IN_SETJMP_SETUP,
9798 "__builtin_setjmp_setup", ECF_NOTHROW);
9800 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9801 local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
9802 BUILT_IN_SETJMP_DISPATCHER,
9803 "__builtin_setjmp_dispatcher",
9804 ECF_PURE | ECF_NOTHROW);
9806 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9807 local_define_builtin ("__builtin_setjmp_receiver", ftype,
9808 BUILT_IN_SETJMP_RECEIVER,
9809 "__builtin_setjmp_receiver", ECF_NOTHROW);
9811 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9812 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
9813 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9815 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9816 local_define_builtin ("__builtin_stack_restore", ftype,
9817 BUILT_IN_STACK_RESTORE,
9818 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9820 /* If there's a possibility that we might use the ARM EABI, build the
9821 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
9822 if (targetm.arm_eabi_unwinder)
9824 ftype = build_function_type_list (void_type_node, NULL_TREE);
9825 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
9826 BUILT_IN_CXA_END_CLEANUP,
9827 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
9830 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9831 local_define_builtin ("__builtin_unwind_resume", ftype,
9832 BUILT_IN_UNWIND_RESUME,
9833 ((targetm_common.except_unwind_info (&global_options)
9834 == UI_SJLJ)
9835 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
9836 ECF_NORETURN);
9838 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
9840 ftype = build_function_type_list (ptr_type_node, integer_type_node,
9841 NULL_TREE);
9842 local_define_builtin ("__builtin_return_address", ftype,
9843 BUILT_IN_RETURN_ADDRESS,
9844 "__builtin_return_address",
9845 ECF_NOTHROW);
9848 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
9849 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9851 ftype = build_function_type_list (void_type_node, ptr_type_node,
9852 ptr_type_node, NULL_TREE);
9853 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
9854 local_define_builtin ("__cyg_profile_func_enter", ftype,
9855 BUILT_IN_PROFILE_FUNC_ENTER,
9856 "__cyg_profile_func_enter", 0);
9857 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9858 local_define_builtin ("__cyg_profile_func_exit", ftype,
9859 BUILT_IN_PROFILE_FUNC_EXIT,
9860 "__cyg_profile_func_exit", 0);
9863 /* The exception object and filter values from the runtime. The argument
9864 must be zero before exception lowering, i.e. from the front end. After
9865 exception lowering, it will be the region number for the exception
9866 landing pad. These functions are PURE instead of CONST to prevent
9867 them from being hoisted past the exception edge that will initialize
9868 its value in the landing pad. */
9869 ftype = build_function_type_list (ptr_type_node,
9870 integer_type_node, NULL_TREE);
9871 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
9872 /* Only use TM_PURE if we we have TM language support. */
9873 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
9874 ecf_flags |= ECF_TM_PURE;
9875 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
9876 "__builtin_eh_pointer", ecf_flags);
9878 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
9879 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
9880 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
9881 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9883 ftype = build_function_type_list (void_type_node,
9884 integer_type_node, integer_type_node,
9885 NULL_TREE);
9886 local_define_builtin ("__builtin_eh_copy_values", ftype,
9887 BUILT_IN_EH_COPY_VALUES,
9888 "__builtin_eh_copy_values", ECF_NOTHROW);
9890 /* Complex multiplication and division. These are handled as builtins
9891 rather than optabs because emit_library_call_value doesn't support
9892 complex. Further, we can do slightly better with folding these
9893 beasties if the real and complex parts of the arguments are separate. */
9895 int mode;
9897 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
9899 char mode_name_buf[4], *q;
9900 const char *p;
9901 enum built_in_function mcode, dcode;
9902 tree type, inner_type;
9903 const char *prefix = "__";
9905 if (targetm.libfunc_gnu_prefix)
9906 prefix = "__gnu_";
9908 type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
9909 if (type == NULL)
9910 continue;
9911 inner_type = TREE_TYPE (type);
9913 ftype = build_function_type_list (type, inner_type, inner_type,
9914 inner_type, inner_type, NULL_TREE);
9916 mcode = ((enum built_in_function)
9917 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9918 dcode = ((enum built_in_function)
9919 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9921 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
9922 *q = TOLOWER (*p);
9923 *q = '\0';
9925 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
9926 NULL);
9927 local_define_builtin (built_in_names[mcode], ftype, mcode,
9928 built_in_names[mcode],
9929 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
9931 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
9932 NULL);
9933 local_define_builtin (built_in_names[dcode], ftype, dcode,
9934 built_in_names[dcode],
9935 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
9940 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
9941 better way.
9943 If we requested a pointer to a vector, build up the pointers that
9944 we stripped off while looking for the inner type. Similarly for
9945 return values from functions.
9947 The argument TYPE is the top of the chain, and BOTTOM is the
9948 new type which we will point to. */
9950 tree
9951 reconstruct_complex_type (tree type, tree bottom)
9953 tree inner, outer;
9955 if (TREE_CODE (type) == POINTER_TYPE)
9957 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9958 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
9959 TYPE_REF_CAN_ALIAS_ALL (type));
9961 else if (TREE_CODE (type) == REFERENCE_TYPE)
9963 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9964 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
9965 TYPE_REF_CAN_ALIAS_ALL (type));
9967 else if (TREE_CODE (type) == ARRAY_TYPE)
9969 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9970 outer = build_array_type (inner, TYPE_DOMAIN (type));
9972 else if (TREE_CODE (type) == FUNCTION_TYPE)
9974 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9975 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
9977 else if (TREE_CODE (type) == METHOD_TYPE)
9979 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9980 /* The build_method_type_directly() routine prepends 'this' to argument list,
9981 so we must compensate by getting rid of it. */
9982 outer
9983 = build_method_type_directly
9984 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
9985 inner,
9986 TREE_CHAIN (TYPE_ARG_TYPES (type)));
9988 else if (TREE_CODE (type) == OFFSET_TYPE)
9990 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9991 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
9993 else
9994 return bottom;
9996 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
9997 TYPE_QUALS (type));
10000 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10001 the inner type. */
10002 tree
10003 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
10005 int nunits;
10007 switch (GET_MODE_CLASS (mode))
10009 case MODE_VECTOR_INT:
10010 case MODE_VECTOR_FLOAT:
10011 case MODE_VECTOR_FRACT:
10012 case MODE_VECTOR_UFRACT:
10013 case MODE_VECTOR_ACCUM:
10014 case MODE_VECTOR_UACCUM:
10015 nunits = GET_MODE_NUNITS (mode);
10016 break;
10018 case MODE_INT:
10019 /* Check that there are no leftover bits. */
10020 gcc_assert (GET_MODE_BITSIZE (mode)
10021 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10023 nunits = GET_MODE_BITSIZE (mode)
10024 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10025 break;
10027 default:
10028 gcc_unreachable ();
10031 return make_vector_type (innertype, nunits, mode);
10034 /* Similarly, but takes the inner type and number of units, which must be
10035 a power of two. */
10037 tree
10038 build_vector_type (tree innertype, int nunits)
10040 return make_vector_type (innertype, nunits, VOIDmode);
10043 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10045 tree
10046 build_opaque_vector_type (tree innertype, int nunits)
10048 tree t = make_vector_type (innertype, nunits, VOIDmode);
10049 tree cand;
10050 /* We always build the non-opaque variant before the opaque one,
10051 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10052 cand = TYPE_NEXT_VARIANT (t);
10053 if (cand
10054 && TYPE_VECTOR_OPAQUE (cand)
10055 && check_qualified_type (cand, t, TYPE_QUALS (t)))
10056 return cand;
10057 /* Othewise build a variant type and make sure to queue it after
10058 the non-opaque type. */
10059 cand = build_distinct_type_copy (t);
10060 TYPE_VECTOR_OPAQUE (cand) = true;
10061 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10062 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10063 TYPE_NEXT_VARIANT (t) = cand;
10064 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10065 return cand;
10069 /* Given an initializer INIT, return TRUE if INIT is zero or some
10070 aggregate of zeros. Otherwise return FALSE. */
10071 bool
10072 initializer_zerop (const_tree init)
10074 tree elt;
10076 STRIP_NOPS (init);
10078 switch (TREE_CODE (init))
10080 case INTEGER_CST:
10081 return integer_zerop (init);
10083 case REAL_CST:
10084 /* ??? Note that this is not correct for C4X float formats. There,
10085 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10086 negative exponent. */
10087 return real_zerop (init)
10088 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10090 case FIXED_CST:
10091 return fixed_zerop (init);
10093 case COMPLEX_CST:
10094 return integer_zerop (init)
10095 || (real_zerop (init)
10096 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10097 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10099 case VECTOR_CST:
10101 unsigned i;
10102 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10103 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10104 return false;
10105 return true;
10108 case CONSTRUCTOR:
10110 unsigned HOST_WIDE_INT idx;
10112 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10113 if (!initializer_zerop (elt))
10114 return false;
10115 return true;
10118 case STRING_CST:
10120 int i;
10122 /* We need to loop through all elements to handle cases like
10123 "\0" and "\0foobar". */
10124 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10125 if (TREE_STRING_POINTER (init)[i] != '\0')
10126 return false;
10128 return true;
10131 default:
10132 return false;
10136 /* Check if vector VEC consists of all the equal elements and
10137 that the number of elements corresponds to the type of VEC.
10138 The function returns first element of the vector
10139 or NULL_TREE if the vector is not uniform. */
10140 tree
10141 uniform_vector_p (const_tree vec)
10143 tree first, t;
10144 unsigned i;
10146 if (vec == NULL_TREE)
10147 return NULL_TREE;
10149 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10151 if (TREE_CODE (vec) == VECTOR_CST)
10153 first = VECTOR_CST_ELT (vec, 0);
10154 for (i = 1; i < VECTOR_CST_NELTS (vec); ++i)
10155 if (!operand_equal_p (first, VECTOR_CST_ELT (vec, i), 0))
10156 return NULL_TREE;
10158 return first;
10161 else if (TREE_CODE (vec) == CONSTRUCTOR)
10163 first = error_mark_node;
10165 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10167 if (i == 0)
10169 first = t;
10170 continue;
10172 if (!operand_equal_p (first, t, 0))
10173 return NULL_TREE;
10175 if (i != TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)))
10176 return NULL_TREE;
10178 return first;
10181 return NULL_TREE;
10184 /* Build an empty statement at location LOC. */
10186 tree
10187 build_empty_stmt (location_t loc)
10189 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10190 SET_EXPR_LOCATION (t, loc);
10191 return t;
10195 /* Build an OpenMP clause with code CODE. LOC is the location of the
10196 clause. */
10198 tree
10199 build_omp_clause (location_t loc, enum omp_clause_code code)
10201 tree t;
10202 int size, length;
10204 length = omp_clause_num_ops[code];
10205 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10207 record_node_allocation_statistics (OMP_CLAUSE, size);
10209 t = ggc_alloc_tree_node (size);
10210 memset (t, 0, size);
10211 TREE_SET_CODE (t, OMP_CLAUSE);
10212 OMP_CLAUSE_SET_CODE (t, code);
10213 OMP_CLAUSE_LOCATION (t) = loc;
10215 return t;
10218 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10219 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10220 Except for the CODE and operand count field, other storage for the
10221 object is initialized to zeros. */
10223 tree
10224 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10226 tree t;
10227 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10229 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10230 gcc_assert (len >= 1);
10232 record_node_allocation_statistics (code, length);
10234 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10236 TREE_SET_CODE (t, code);
10238 /* Can't use TREE_OPERAND to store the length because if checking is
10239 enabled, it will try to check the length before we store it. :-P */
10240 t->exp.operands[0] = build_int_cst (sizetype, len);
10242 return t;
10245 /* Helper function for build_call_* functions; build a CALL_EXPR with
10246 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10247 the argument slots. */
10249 static tree
10250 build_call_1 (tree return_type, tree fn, int nargs)
10252 tree t;
10254 t = build_vl_exp (CALL_EXPR, nargs + 3);
10255 TREE_TYPE (t) = return_type;
10256 CALL_EXPR_FN (t) = fn;
10257 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10259 return t;
10262 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10263 FN and a null static chain slot. NARGS is the number of call arguments
10264 which are specified as "..." arguments. */
10266 tree
10267 build_call_nary (tree return_type, tree fn, int nargs, ...)
10269 tree ret;
10270 va_list args;
10271 va_start (args, nargs);
10272 ret = build_call_valist (return_type, fn, nargs, args);
10273 va_end (args);
10274 return ret;
10277 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10278 FN and a null static chain slot. NARGS is the number of call arguments
10279 which are specified as a va_list ARGS. */
10281 tree
10282 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10284 tree t;
10285 int i;
10287 t = build_call_1 (return_type, fn, nargs);
10288 for (i = 0; i < nargs; i++)
10289 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10290 process_call_operands (t);
10291 return t;
10294 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10295 FN and a null static chain slot. NARGS is the number of call arguments
10296 which are specified as a tree array ARGS. */
10298 tree
10299 build_call_array_loc (location_t loc, tree return_type, tree fn,
10300 int nargs, const tree *args)
10302 tree t;
10303 int i;
10305 t = build_call_1 (return_type, fn, nargs);
10306 for (i = 0; i < nargs; i++)
10307 CALL_EXPR_ARG (t, i) = args[i];
10308 process_call_operands (t);
10309 SET_EXPR_LOCATION (t, loc);
10310 return t;
10313 /* Like build_call_array, but takes a vec. */
10315 tree
10316 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10318 tree ret, t;
10319 unsigned int ix;
10321 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10322 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10323 CALL_EXPR_ARG (ret, ix) = t;
10324 process_call_operands (ret);
10325 return ret;
10329 /* Returns true if it is possible to prove that the index of
10330 an array access REF (an ARRAY_REF expression) falls into the
10331 array bounds. */
10333 bool
10334 in_array_bounds_p (tree ref)
10336 tree idx = TREE_OPERAND (ref, 1);
10337 tree min, max;
10339 if (TREE_CODE (idx) != INTEGER_CST)
10340 return false;
10342 min = array_ref_low_bound (ref);
10343 max = array_ref_up_bound (ref);
10344 if (!min
10345 || !max
10346 || TREE_CODE (min) != INTEGER_CST
10347 || TREE_CODE (max) != INTEGER_CST)
10348 return false;
10350 if (tree_int_cst_lt (idx, min)
10351 || tree_int_cst_lt (max, idx))
10352 return false;
10354 return true;
10357 /* Returns true if it is possible to prove that the range of
10358 an array access REF (an ARRAY_RANGE_REF expression) falls
10359 into the array bounds. */
10361 bool
10362 range_in_array_bounds_p (tree ref)
10364 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
10365 tree range_min, range_max, min, max;
10367 range_min = TYPE_MIN_VALUE (domain_type);
10368 range_max = TYPE_MAX_VALUE (domain_type);
10369 if (!range_min
10370 || !range_max
10371 || TREE_CODE (range_min) != INTEGER_CST
10372 || TREE_CODE (range_max) != INTEGER_CST)
10373 return false;
10375 min = array_ref_low_bound (ref);
10376 max = array_ref_up_bound (ref);
10377 if (!min
10378 || !max
10379 || TREE_CODE (min) != INTEGER_CST
10380 || TREE_CODE (max) != INTEGER_CST)
10381 return false;
10383 if (tree_int_cst_lt (range_min, min)
10384 || tree_int_cst_lt (max, range_max))
10385 return false;
10387 return true;
10390 /* Return true if T (assumed to be a DECL) must be assigned a memory
10391 location. */
10393 bool
10394 needs_to_live_in_memory (const_tree t)
10396 return (TREE_ADDRESSABLE (t)
10397 || is_global_var (t)
10398 || (TREE_CODE (t) == RESULT_DECL
10399 && !DECL_BY_REFERENCE (t)
10400 && aggregate_value_p (t, current_function_decl)));
10403 /* Return value of a constant X and sign-extend it. */
10405 HOST_WIDE_INT
10406 int_cst_value (const_tree x)
10408 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10409 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10411 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10412 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10413 || TREE_INT_CST_HIGH (x) == -1);
10415 if (bits < HOST_BITS_PER_WIDE_INT)
10417 bool negative = ((val >> (bits - 1)) & 1) != 0;
10418 if (negative)
10419 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10420 else
10421 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10424 return val;
10427 /* Return value of a constant X and sign-extend it. */
10429 HOST_WIDEST_INT
10430 widest_int_cst_value (const_tree x)
10432 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10433 unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
10435 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
10436 gcc_assert (HOST_BITS_PER_WIDEST_INT >= HOST_BITS_PER_DOUBLE_INT);
10437 val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_HIGH (x))
10438 << HOST_BITS_PER_WIDE_INT);
10439 #else
10440 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10441 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10442 || TREE_INT_CST_HIGH (x) == -1);
10443 #endif
10445 if (bits < HOST_BITS_PER_WIDEST_INT)
10447 bool negative = ((val >> (bits - 1)) & 1) != 0;
10448 if (negative)
10449 val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
10450 else
10451 val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
10454 return val;
10457 /* If TYPE is an integral or pointer type, return an integer type with
10458 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10459 if TYPE is already an integer type of signedness UNSIGNEDP. */
10461 tree
10462 signed_or_unsigned_type_for (int unsignedp, tree type)
10464 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10465 return type;
10467 if (TREE_CODE (type) == VECTOR_TYPE)
10469 tree inner = TREE_TYPE (type);
10470 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10471 if (!inner2)
10472 return NULL_TREE;
10473 if (inner == inner2)
10474 return type;
10475 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10478 if (!INTEGRAL_TYPE_P (type)
10479 && !POINTER_TYPE_P (type))
10480 return NULL_TREE;
10482 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10485 /* If TYPE is an integral or pointer type, return an integer type with
10486 the same precision which is unsigned, or itself if TYPE is already an
10487 unsigned integer type. */
10489 tree
10490 unsigned_type_for (tree type)
10492 return signed_or_unsigned_type_for (1, type);
10495 /* If TYPE is an integral or pointer type, return an integer type with
10496 the same precision which is signed, or itself if TYPE is already a
10497 signed integer type. */
10499 tree
10500 signed_type_for (tree type)
10502 return signed_or_unsigned_type_for (0, type);
10505 /* If TYPE is a vector type, return a signed integer vector type with the
10506 same width and number of subparts. Otherwise return boolean_type_node. */
10508 tree
10509 truth_type_for (tree type)
10511 if (TREE_CODE (type) == VECTOR_TYPE)
10513 tree elem = lang_hooks.types.type_for_size
10514 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10515 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10517 else
10518 return boolean_type_node;
10521 /* Returns the largest value obtainable by casting something in INNER type to
10522 OUTER type. */
10524 tree
10525 upper_bound_in_type (tree outer, tree inner)
10527 double_int high;
10528 unsigned int det = 0;
10529 unsigned oprec = TYPE_PRECISION (outer);
10530 unsigned iprec = TYPE_PRECISION (inner);
10531 unsigned prec;
10533 /* Compute a unique number for every combination. */
10534 det |= (oprec > iprec) ? 4 : 0;
10535 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10536 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10538 /* Determine the exponent to use. */
10539 switch (det)
10541 case 0:
10542 case 1:
10543 /* oprec <= iprec, outer: signed, inner: don't care. */
10544 prec = oprec - 1;
10545 break;
10546 case 2:
10547 case 3:
10548 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10549 prec = oprec;
10550 break;
10551 case 4:
10552 /* oprec > iprec, outer: signed, inner: signed. */
10553 prec = iprec - 1;
10554 break;
10555 case 5:
10556 /* oprec > iprec, outer: signed, inner: unsigned. */
10557 prec = iprec;
10558 break;
10559 case 6:
10560 /* oprec > iprec, outer: unsigned, inner: signed. */
10561 prec = oprec;
10562 break;
10563 case 7:
10564 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10565 prec = iprec;
10566 break;
10567 default:
10568 gcc_unreachable ();
10571 /* Compute 2^^prec - 1. */
10572 if (prec <= HOST_BITS_PER_WIDE_INT)
10574 high.high = 0;
10575 high.low = ((~(unsigned HOST_WIDE_INT) 0)
10576 >> (HOST_BITS_PER_WIDE_INT - prec));
10578 else
10580 high.high = ((~(unsigned HOST_WIDE_INT) 0)
10581 >> (HOST_BITS_PER_DOUBLE_INT - prec));
10582 high.low = ~(unsigned HOST_WIDE_INT) 0;
10585 return double_int_to_tree (outer, high);
10588 /* Returns the smallest value obtainable by casting something in INNER type to
10589 OUTER type. */
10591 tree
10592 lower_bound_in_type (tree outer, tree inner)
10594 double_int low;
10595 unsigned oprec = TYPE_PRECISION (outer);
10596 unsigned iprec = TYPE_PRECISION (inner);
10598 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10599 and obtain 0. */
10600 if (TYPE_UNSIGNED (outer)
10601 /* If we are widening something of an unsigned type, OUTER type
10602 contains all values of INNER type. In particular, both INNER
10603 and OUTER types have zero in common. */
10604 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10605 low.low = low.high = 0;
10606 else
10608 /* If we are widening a signed type to another signed type, we
10609 want to obtain -2^^(iprec-1). If we are keeping the
10610 precision or narrowing to a signed type, we want to obtain
10611 -2^(oprec-1). */
10612 unsigned prec = oprec > iprec ? iprec : oprec;
10614 if (prec <= HOST_BITS_PER_WIDE_INT)
10616 low.high = ~(unsigned HOST_WIDE_INT) 0;
10617 low.low = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
10619 else
10621 low.high = ((~(unsigned HOST_WIDE_INT) 0)
10622 << (prec - HOST_BITS_PER_WIDE_INT - 1));
10623 low.low = 0;
10627 return double_int_to_tree (outer, low);
10630 /* Return nonzero if two operands that are suitable for PHI nodes are
10631 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10632 SSA_NAME or invariant. Note that this is strictly an optimization.
10633 That is, callers of this function can directly call operand_equal_p
10634 and get the same result, only slower. */
10637 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10639 if (arg0 == arg1)
10640 return 1;
10641 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10642 return 0;
10643 return operand_equal_p (arg0, arg1, 0);
10646 /* Returns number of zeros at the end of binary representation of X.
10648 ??? Use ffs if available? */
10650 tree
10651 num_ending_zeros (const_tree x)
10653 unsigned HOST_WIDE_INT fr, nfr;
10654 unsigned num, abits;
10655 tree type = TREE_TYPE (x);
10657 if (TREE_INT_CST_LOW (x) == 0)
10659 num = HOST_BITS_PER_WIDE_INT;
10660 fr = TREE_INT_CST_HIGH (x);
10662 else
10664 num = 0;
10665 fr = TREE_INT_CST_LOW (x);
10668 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
10670 nfr = fr >> abits;
10671 if (nfr << abits == fr)
10673 num += abits;
10674 fr = nfr;
10678 if (num > TYPE_PRECISION (type))
10679 num = TYPE_PRECISION (type);
10681 return build_int_cst_type (type, num);
10685 #define WALK_SUBTREE(NODE) \
10686 do \
10688 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10689 if (result) \
10690 return result; \
10692 while (0)
10694 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10695 be walked whenever a type is seen in the tree. Rest of operands and return
10696 value are as for walk_tree. */
10698 static tree
10699 walk_type_fields (tree type, walk_tree_fn func, void *data,
10700 struct pointer_set_t *pset, walk_tree_lh lh)
10702 tree result = NULL_TREE;
10704 switch (TREE_CODE (type))
10706 case POINTER_TYPE:
10707 case REFERENCE_TYPE:
10708 /* We have to worry about mutually recursive pointers. These can't
10709 be written in C. They can in Ada. It's pathological, but
10710 there's an ACATS test (c38102a) that checks it. Deal with this
10711 by checking if we're pointing to another pointer, that one
10712 points to another pointer, that one does too, and we have no htab.
10713 If so, get a hash table. We check three levels deep to avoid
10714 the cost of the hash table if we don't need one. */
10715 if (POINTER_TYPE_P (TREE_TYPE (type))
10716 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10717 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10718 && !pset)
10720 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10721 func, data);
10722 if (result)
10723 return result;
10725 break;
10728 /* ... fall through ... */
10730 case COMPLEX_TYPE:
10731 WALK_SUBTREE (TREE_TYPE (type));
10732 break;
10734 case METHOD_TYPE:
10735 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10737 /* Fall through. */
10739 case FUNCTION_TYPE:
10740 WALK_SUBTREE (TREE_TYPE (type));
10742 tree arg;
10744 /* We never want to walk into default arguments. */
10745 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10746 WALK_SUBTREE (TREE_VALUE (arg));
10748 break;
10750 case ARRAY_TYPE:
10751 /* Don't follow this nodes's type if a pointer for fear that
10752 we'll have infinite recursion. If we have a PSET, then we
10753 need not fear. */
10754 if (pset
10755 || (!POINTER_TYPE_P (TREE_TYPE (type))
10756 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10757 WALK_SUBTREE (TREE_TYPE (type));
10758 WALK_SUBTREE (TYPE_DOMAIN (type));
10759 break;
10761 case OFFSET_TYPE:
10762 WALK_SUBTREE (TREE_TYPE (type));
10763 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
10764 break;
10766 default:
10767 break;
10770 return NULL_TREE;
10773 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
10774 called with the DATA and the address of each sub-tree. If FUNC returns a
10775 non-NULL value, the traversal is stopped, and the value returned by FUNC
10776 is returned. If PSET is non-NULL it is used to record the nodes visited,
10777 and to avoid visiting a node more than once. */
10779 tree
10780 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
10781 struct pointer_set_t *pset, walk_tree_lh lh)
10783 enum tree_code code;
10784 int walk_subtrees;
10785 tree result;
10787 #define WALK_SUBTREE_TAIL(NODE) \
10788 do \
10790 tp = & (NODE); \
10791 goto tail_recurse; \
10793 while (0)
10795 tail_recurse:
10796 /* Skip empty subtrees. */
10797 if (!*tp)
10798 return NULL_TREE;
10800 /* Don't walk the same tree twice, if the user has requested
10801 that we avoid doing so. */
10802 if (pset && pointer_set_insert (pset, *tp))
10803 return NULL_TREE;
10805 /* Call the function. */
10806 walk_subtrees = 1;
10807 result = (*func) (tp, &walk_subtrees, data);
10809 /* If we found something, return it. */
10810 if (result)
10811 return result;
10813 code = TREE_CODE (*tp);
10815 /* Even if we didn't, FUNC may have decided that there was nothing
10816 interesting below this point in the tree. */
10817 if (!walk_subtrees)
10819 /* But we still need to check our siblings. */
10820 if (code == TREE_LIST)
10821 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10822 else if (code == OMP_CLAUSE)
10823 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10824 else
10825 return NULL_TREE;
10828 if (lh)
10830 result = (*lh) (tp, &walk_subtrees, func, data, pset);
10831 if (result || !walk_subtrees)
10832 return result;
10835 switch (code)
10837 case ERROR_MARK:
10838 case IDENTIFIER_NODE:
10839 case INTEGER_CST:
10840 case REAL_CST:
10841 case FIXED_CST:
10842 case VECTOR_CST:
10843 case STRING_CST:
10844 case BLOCK:
10845 case PLACEHOLDER_EXPR:
10846 case SSA_NAME:
10847 case FIELD_DECL:
10848 case RESULT_DECL:
10849 /* None of these have subtrees other than those already walked
10850 above. */
10851 break;
10853 case TREE_LIST:
10854 WALK_SUBTREE (TREE_VALUE (*tp));
10855 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10856 break;
10858 case TREE_VEC:
10860 int len = TREE_VEC_LENGTH (*tp);
10862 if (len == 0)
10863 break;
10865 /* Walk all elements but the first. */
10866 while (--len)
10867 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
10869 /* Now walk the first one as a tail call. */
10870 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
10873 case COMPLEX_CST:
10874 WALK_SUBTREE (TREE_REALPART (*tp));
10875 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
10877 case CONSTRUCTOR:
10879 unsigned HOST_WIDE_INT idx;
10880 constructor_elt *ce;
10882 for (idx = 0; vec_safe_iterate(CONSTRUCTOR_ELTS (*tp), idx, &ce); idx++)
10883 WALK_SUBTREE (ce->value);
10885 break;
10887 case SAVE_EXPR:
10888 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
10890 case BIND_EXPR:
10892 tree decl;
10893 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
10895 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
10896 into declarations that are just mentioned, rather than
10897 declared; they don't really belong to this part of the tree.
10898 And, we can see cycles: the initializer for a declaration
10899 can refer to the declaration itself. */
10900 WALK_SUBTREE (DECL_INITIAL (decl));
10901 WALK_SUBTREE (DECL_SIZE (decl));
10902 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
10904 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
10907 case STATEMENT_LIST:
10909 tree_stmt_iterator i;
10910 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
10911 WALK_SUBTREE (*tsi_stmt_ptr (i));
10913 break;
10915 case OMP_CLAUSE:
10916 switch (OMP_CLAUSE_CODE (*tp))
10918 case OMP_CLAUSE_PRIVATE:
10919 case OMP_CLAUSE_SHARED:
10920 case OMP_CLAUSE_FIRSTPRIVATE:
10921 case OMP_CLAUSE_COPYIN:
10922 case OMP_CLAUSE_COPYPRIVATE:
10923 case OMP_CLAUSE_FINAL:
10924 case OMP_CLAUSE_IF:
10925 case OMP_CLAUSE_NUM_THREADS:
10926 case OMP_CLAUSE_SCHEDULE:
10927 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
10928 /* FALLTHRU */
10930 case OMP_CLAUSE_NOWAIT:
10931 case OMP_CLAUSE_ORDERED:
10932 case OMP_CLAUSE_DEFAULT:
10933 case OMP_CLAUSE_UNTIED:
10934 case OMP_CLAUSE_MERGEABLE:
10935 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10937 case OMP_CLAUSE_LASTPRIVATE:
10938 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
10939 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
10940 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10942 case OMP_CLAUSE_COLLAPSE:
10944 int i;
10945 for (i = 0; i < 3; i++)
10946 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10947 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10950 case OMP_CLAUSE_REDUCTION:
10952 int i;
10953 for (i = 0; i < 4; i++)
10954 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10955 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10958 default:
10959 gcc_unreachable ();
10961 break;
10963 case TARGET_EXPR:
10965 int i, len;
10967 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
10968 But, we only want to walk once. */
10969 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
10970 for (i = 0; i < len; ++i)
10971 WALK_SUBTREE (TREE_OPERAND (*tp, i));
10972 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
10975 case DECL_EXPR:
10976 /* If this is a TYPE_DECL, walk into the fields of the type that it's
10977 defining. We only want to walk into these fields of a type in this
10978 case and not in the general case of a mere reference to the type.
10980 The criterion is as follows: if the field can be an expression, it
10981 must be walked only here. This should be in keeping with the fields
10982 that are directly gimplified in gimplify_type_sizes in order for the
10983 mark/copy-if-shared/unmark machinery of the gimplifier to work with
10984 variable-sized types.
10986 Note that DECLs get walked as part of processing the BIND_EXPR. */
10987 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
10989 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
10990 if (TREE_CODE (*type_p) == ERROR_MARK)
10991 return NULL_TREE;
10993 /* Call the function for the type. See if it returns anything or
10994 doesn't want us to continue. If we are to continue, walk both
10995 the normal fields and those for the declaration case. */
10996 result = (*func) (type_p, &walk_subtrees, data);
10997 if (result || !walk_subtrees)
10998 return result;
11000 /* But do not walk a pointed-to type since it may itself need to
11001 be walked in the declaration case if it isn't anonymous. */
11002 if (!POINTER_TYPE_P (*type_p))
11004 result = walk_type_fields (*type_p, func, data, pset, lh);
11005 if (result)
11006 return result;
11009 /* If this is a record type, also walk the fields. */
11010 if (RECORD_OR_UNION_TYPE_P (*type_p))
11012 tree field;
11014 for (field = TYPE_FIELDS (*type_p); field;
11015 field = DECL_CHAIN (field))
11017 /* We'd like to look at the type of the field, but we can
11018 easily get infinite recursion. So assume it's pointed
11019 to elsewhere in the tree. Also, ignore things that
11020 aren't fields. */
11021 if (TREE_CODE (field) != FIELD_DECL)
11022 continue;
11024 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11025 WALK_SUBTREE (DECL_SIZE (field));
11026 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11027 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11028 WALK_SUBTREE (DECL_QUALIFIER (field));
11032 /* Same for scalar types. */
11033 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11034 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11035 || TREE_CODE (*type_p) == INTEGER_TYPE
11036 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11037 || TREE_CODE (*type_p) == REAL_TYPE)
11039 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11040 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11043 WALK_SUBTREE (TYPE_SIZE (*type_p));
11044 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11046 /* FALLTHRU */
11048 default:
11049 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11051 int i, len;
11053 /* Walk over all the sub-trees of this operand. */
11054 len = TREE_OPERAND_LENGTH (*tp);
11056 /* Go through the subtrees. We need to do this in forward order so
11057 that the scope of a FOR_EXPR is handled properly. */
11058 if (len)
11060 for (i = 0; i < len - 1; ++i)
11061 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11062 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11065 /* If this is a type, walk the needed fields in the type. */
11066 else if (TYPE_P (*tp))
11067 return walk_type_fields (*tp, func, data, pset, lh);
11068 break;
11071 /* We didn't find what we were looking for. */
11072 return NULL_TREE;
11074 #undef WALK_SUBTREE_TAIL
11076 #undef WALK_SUBTREE
11078 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11080 tree
11081 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11082 walk_tree_lh lh)
11084 tree result;
11085 struct pointer_set_t *pset;
11087 pset = pointer_set_create ();
11088 result = walk_tree_1 (tp, func, data, pset, lh);
11089 pointer_set_destroy (pset);
11090 return result;
11094 tree
11095 tree_block (tree t)
11097 char const c = TREE_CODE_CLASS (TREE_CODE (t));
11099 if (IS_EXPR_CODE_CLASS (c))
11100 return LOCATION_BLOCK (t->exp.locus);
11101 gcc_unreachable ();
11102 return NULL;
11105 void
11106 tree_set_block (tree t, tree b)
11108 char const c = TREE_CODE_CLASS (TREE_CODE (t));
11110 if (IS_EXPR_CODE_CLASS (c))
11112 if (b)
11113 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
11114 else
11115 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
11117 else
11118 gcc_unreachable ();
11121 /* Create a nameless artificial label and put it in the current
11122 function context. The label has a location of LOC. Returns the
11123 newly created label. */
11125 tree
11126 create_artificial_label (location_t loc)
11128 tree lab = build_decl (loc,
11129 LABEL_DECL, NULL_TREE, void_type_node);
11131 DECL_ARTIFICIAL (lab) = 1;
11132 DECL_IGNORED_P (lab) = 1;
11133 DECL_CONTEXT (lab) = current_function_decl;
11134 return lab;
11137 /* Given a tree, try to return a useful variable name that we can use
11138 to prefix a temporary that is being assigned the value of the tree.
11139 I.E. given <temp> = &A, return A. */
11141 const char *
11142 get_name (tree t)
11144 tree stripped_decl;
11146 stripped_decl = t;
11147 STRIP_NOPS (stripped_decl);
11148 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11149 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11150 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11152 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11153 if (!name)
11154 return NULL;
11155 return IDENTIFIER_POINTER (name);
11157 else
11159 switch (TREE_CODE (stripped_decl))
11161 case ADDR_EXPR:
11162 return get_name (TREE_OPERAND (stripped_decl, 0));
11163 default:
11164 return NULL;
11169 /* Return true if TYPE has a variable argument list. */
11171 bool
11172 stdarg_p (const_tree fntype)
11174 function_args_iterator args_iter;
11175 tree n = NULL_TREE, t;
11177 if (!fntype)
11178 return false;
11180 FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
11182 n = t;
11185 return n != NULL_TREE && n != void_type_node;
11188 /* Return true if TYPE has a prototype. */
11190 bool
11191 prototype_p (tree fntype)
11193 tree t;
11195 gcc_assert (fntype != NULL_TREE);
11197 t = TYPE_ARG_TYPES (fntype);
11198 return (t != NULL_TREE);
11201 /* If BLOCK is inlined from an __attribute__((__artificial__))
11202 routine, return pointer to location from where it has been
11203 called. */
11204 location_t *
11205 block_nonartificial_location (tree block)
11207 location_t *ret = NULL;
11209 while (block && TREE_CODE (block) == BLOCK
11210 && BLOCK_ABSTRACT_ORIGIN (block))
11212 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11214 while (TREE_CODE (ao) == BLOCK
11215 && BLOCK_ABSTRACT_ORIGIN (ao)
11216 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11217 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11219 if (TREE_CODE (ao) == FUNCTION_DECL)
11221 /* If AO is an artificial inline, point RET to the
11222 call site locus at which it has been inlined and continue
11223 the loop, in case AO's caller is also an artificial
11224 inline. */
11225 if (DECL_DECLARED_INLINE_P (ao)
11226 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11227 ret = &BLOCK_SOURCE_LOCATION (block);
11228 else
11229 break;
11231 else if (TREE_CODE (ao) != BLOCK)
11232 break;
11234 block = BLOCK_SUPERCONTEXT (block);
11236 return ret;
11240 /* If EXP is inlined from an __attribute__((__artificial__))
11241 function, return the location of the original call expression. */
11243 location_t
11244 tree_nonartificial_location (tree exp)
11246 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11248 if (loc)
11249 return *loc;
11250 else
11251 return EXPR_LOCATION (exp);
11255 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11256 nodes. */
11258 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11260 static hashval_t
11261 cl_option_hash_hash (const void *x)
11263 const_tree const t = (const_tree) x;
11264 const char *p;
11265 size_t i;
11266 size_t len = 0;
11267 hashval_t hash = 0;
11269 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11271 p = (const char *)TREE_OPTIMIZATION (t);
11272 len = sizeof (struct cl_optimization);
11275 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11277 p = (const char *)TREE_TARGET_OPTION (t);
11278 len = sizeof (struct cl_target_option);
11281 else
11282 gcc_unreachable ();
11284 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11285 something else. */
11286 for (i = 0; i < len; i++)
11287 if (p[i])
11288 hash = (hash << 4) ^ ((i << 2) | p[i]);
11290 return hash;
11293 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11294 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11295 same. */
11297 static int
11298 cl_option_hash_eq (const void *x, const void *y)
11300 const_tree const xt = (const_tree) x;
11301 const_tree const yt = (const_tree) y;
11302 const char *xp;
11303 const char *yp;
11304 size_t len;
11306 if (TREE_CODE (xt) != TREE_CODE (yt))
11307 return 0;
11309 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11311 xp = (const char *)TREE_OPTIMIZATION (xt);
11312 yp = (const char *)TREE_OPTIMIZATION (yt);
11313 len = sizeof (struct cl_optimization);
11316 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11318 xp = (const char *)TREE_TARGET_OPTION (xt);
11319 yp = (const char *)TREE_TARGET_OPTION (yt);
11320 len = sizeof (struct cl_target_option);
11323 else
11324 gcc_unreachable ();
11326 return (memcmp (xp, yp, len) == 0);
11329 /* Build an OPTIMIZATION_NODE based on the current options. */
11331 tree
11332 build_optimization_node (void)
11334 tree t;
11335 void **slot;
11337 /* Use the cache of optimization nodes. */
11339 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11340 &global_options);
11342 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
11343 t = (tree) *slot;
11344 if (!t)
11346 /* Insert this one into the hash table. */
11347 t = cl_optimization_node;
11348 *slot = t;
11350 /* Make a new node for next time round. */
11351 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11354 return t;
11357 /* Build a TARGET_OPTION_NODE based on the current options. */
11359 tree
11360 build_target_option_node (void)
11362 tree t;
11363 void **slot;
11365 /* Use the cache of optimization nodes. */
11367 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11368 &global_options);
11370 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
11371 t = (tree) *slot;
11372 if (!t)
11374 /* Insert this one into the hash table. */
11375 t = cl_target_option_node;
11376 *slot = t;
11378 /* Make a new node for next time round. */
11379 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11382 return t;
11385 /* Determine the "ultimate origin" of a block. The block may be an inlined
11386 instance of an inlined instance of a block which is local to an inline
11387 function, so we have to trace all of the way back through the origin chain
11388 to find out what sort of node actually served as the original seed for the
11389 given block. */
11391 tree
11392 block_ultimate_origin (const_tree block)
11394 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11396 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
11397 nodes in the function to point to themselves; ignore that if
11398 we're trying to output the abstract instance of this function. */
11399 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11400 return NULL_TREE;
11402 if (immediate_origin == NULL_TREE)
11403 return NULL_TREE;
11404 else
11406 tree ret_val;
11407 tree lookahead = immediate_origin;
11411 ret_val = lookahead;
11412 lookahead = (TREE_CODE (ret_val) == BLOCK
11413 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11415 while (lookahead != NULL && lookahead != ret_val);
11417 /* The block's abstract origin chain may not be the *ultimate* origin of
11418 the block. It could lead to a DECL that has an abstract origin set.
11419 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11420 will give us if it has one). Note that DECL's abstract origins are
11421 supposed to be the most distant ancestor (or so decl_ultimate_origin
11422 claims), so we don't need to loop following the DECL origins. */
11423 if (DECL_P (ret_val))
11424 return DECL_ORIGIN (ret_val);
11426 return ret_val;
11430 /* Return true if T1 and T2 are equivalent lists. */
11432 bool
11433 list_equal_p (const_tree t1, const_tree t2)
11435 for (; t1 && t2; t1 = TREE_CHAIN (t1) , t2 = TREE_CHAIN (t2))
11436 if (TREE_VALUE (t1) != TREE_VALUE (t2))
11437 return false;
11438 return !t1 && !t2;
11441 /* Return true iff conversion in EXP generates no instruction. Mark
11442 it inline so that we fully inline into the stripping functions even
11443 though we have two uses of this function. */
11445 static inline bool
11446 tree_nop_conversion (const_tree exp)
11448 tree outer_type, inner_type;
11450 if (!CONVERT_EXPR_P (exp)
11451 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11452 return false;
11453 if (TREE_OPERAND (exp, 0) == error_mark_node)
11454 return false;
11456 outer_type = TREE_TYPE (exp);
11457 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11459 if (!inner_type)
11460 return false;
11462 /* Use precision rather then machine mode when we can, which gives
11463 the correct answer even for submode (bit-field) types. */
11464 if ((INTEGRAL_TYPE_P (outer_type)
11465 || POINTER_TYPE_P (outer_type)
11466 || TREE_CODE (outer_type) == OFFSET_TYPE)
11467 && (INTEGRAL_TYPE_P (inner_type)
11468 || POINTER_TYPE_P (inner_type)
11469 || TREE_CODE (inner_type) == OFFSET_TYPE))
11470 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11472 /* Otherwise fall back on comparing machine modes (e.g. for
11473 aggregate types, floats). */
11474 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11477 /* Return true iff conversion in EXP generates no instruction. Don't
11478 consider conversions changing the signedness. */
11480 static bool
11481 tree_sign_nop_conversion (const_tree exp)
11483 tree outer_type, inner_type;
11485 if (!tree_nop_conversion (exp))
11486 return false;
11488 outer_type = TREE_TYPE (exp);
11489 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11491 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11492 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11495 /* Strip conversions from EXP according to tree_nop_conversion and
11496 return the resulting expression. */
11498 tree
11499 tree_strip_nop_conversions (tree exp)
11501 while (tree_nop_conversion (exp))
11502 exp = TREE_OPERAND (exp, 0);
11503 return exp;
11506 /* Strip conversions from EXP according to tree_sign_nop_conversion
11507 and return the resulting expression. */
11509 tree
11510 tree_strip_sign_nop_conversions (tree exp)
11512 while (tree_sign_nop_conversion (exp))
11513 exp = TREE_OPERAND (exp, 0);
11514 return exp;
11517 /* Avoid any floating point extensions from EXP. */
11518 tree
11519 strip_float_extensions (tree exp)
11521 tree sub, expt, subt;
11523 /* For floating point constant look up the narrowest type that can hold
11524 it properly and handle it like (type)(narrowest_type)constant.
11525 This way we can optimize for instance a=a*2.0 where "a" is float
11526 but 2.0 is double constant. */
11527 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11529 REAL_VALUE_TYPE orig;
11530 tree type = NULL;
11532 orig = TREE_REAL_CST (exp);
11533 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11534 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11535 type = float_type_node;
11536 else if (TYPE_PRECISION (TREE_TYPE (exp))
11537 > TYPE_PRECISION (double_type_node)
11538 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11539 type = double_type_node;
11540 if (type)
11541 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11544 if (!CONVERT_EXPR_P (exp))
11545 return exp;
11547 sub = TREE_OPERAND (exp, 0);
11548 subt = TREE_TYPE (sub);
11549 expt = TREE_TYPE (exp);
11551 if (!FLOAT_TYPE_P (subt))
11552 return exp;
11554 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11555 return exp;
11557 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11558 return exp;
11560 return strip_float_extensions (sub);
11563 /* Strip out all handled components that produce invariant
11564 offsets. */
11566 const_tree
11567 strip_invariant_refs (const_tree op)
11569 while (handled_component_p (op))
11571 switch (TREE_CODE (op))
11573 case ARRAY_REF:
11574 case ARRAY_RANGE_REF:
11575 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11576 || TREE_OPERAND (op, 2) != NULL_TREE
11577 || TREE_OPERAND (op, 3) != NULL_TREE)
11578 return NULL;
11579 break;
11581 case COMPONENT_REF:
11582 if (TREE_OPERAND (op, 2) != NULL_TREE)
11583 return NULL;
11584 break;
11586 default:;
11588 op = TREE_OPERAND (op, 0);
11591 return op;
11594 static GTY(()) tree gcc_eh_personality_decl;
11596 /* Return the GCC personality function decl. */
11598 tree
11599 lhd_gcc_personality (void)
11601 if (!gcc_eh_personality_decl)
11602 gcc_eh_personality_decl = build_personality_function ("gcc");
11603 return gcc_eh_personality_decl;
11606 /* Try to find a base info of BINFO that would have its field decl at offset
11607 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11608 found, return, otherwise return NULL_TREE. */
11610 tree
11611 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
11613 tree type = BINFO_TYPE (binfo);
11615 while (true)
11617 HOST_WIDE_INT pos, size;
11618 tree fld;
11619 int i;
11621 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (expected_type))
11622 return binfo;
11623 if (offset < 0)
11624 return NULL_TREE;
11626 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
11628 if (TREE_CODE (fld) != FIELD_DECL)
11629 continue;
11631 pos = int_bit_position (fld);
11632 size = tree_low_cst (DECL_SIZE (fld), 1);
11633 if (pos <= offset && (pos + size) > offset)
11634 break;
11636 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11637 return NULL_TREE;
11639 if (!DECL_ARTIFICIAL (fld))
11641 binfo = TYPE_BINFO (TREE_TYPE (fld));
11642 if (!binfo)
11643 return NULL_TREE;
11645 /* Offset 0 indicates the primary base, whose vtable contents are
11646 represented in the binfo for the derived class. */
11647 else if (offset != 0)
11649 tree base_binfo, found_binfo = NULL_TREE;
11650 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11651 if (TREE_TYPE (base_binfo) == TREE_TYPE (fld))
11653 found_binfo = base_binfo;
11654 break;
11656 if (!found_binfo)
11657 return NULL_TREE;
11658 binfo = found_binfo;
11661 type = TREE_TYPE (fld);
11662 offset -= pos;
11666 /* Returns true if X is a typedef decl. */
11668 bool
11669 is_typedef_decl (tree x)
11671 return (x && TREE_CODE (x) == TYPE_DECL
11672 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
11675 /* Returns true iff TYPE is a type variant created for a typedef. */
11677 bool
11678 typedef_variant_p (tree type)
11680 return is_typedef_decl (TYPE_NAME (type));
11683 /* Warn about a use of an identifier which was marked deprecated. */
11684 void
11685 warn_deprecated_use (tree node, tree attr)
11687 const char *msg;
11689 if (node == 0 || !warn_deprecated_decl)
11690 return;
11692 if (!attr)
11694 if (DECL_P (node))
11695 attr = DECL_ATTRIBUTES (node);
11696 else if (TYPE_P (node))
11698 tree decl = TYPE_STUB_DECL (node);
11699 if (decl)
11700 attr = lookup_attribute ("deprecated",
11701 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
11705 if (attr)
11706 attr = lookup_attribute ("deprecated", attr);
11708 if (attr)
11709 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
11710 else
11711 msg = NULL;
11713 if (DECL_P (node))
11715 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
11716 if (msg)
11717 warning (OPT_Wdeprecated_declarations,
11718 "%qD is deprecated (declared at %r%s:%d%R): %s",
11719 node, "locus", xloc.file, xloc.line, msg);
11720 else
11721 warning (OPT_Wdeprecated_declarations,
11722 "%qD is deprecated (declared at %r%s:%d%R)",
11723 node, "locus", xloc.file, xloc.line);
11725 else if (TYPE_P (node))
11727 tree what = NULL_TREE;
11728 tree decl = TYPE_STUB_DECL (node);
11730 if (TYPE_NAME (node))
11732 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
11733 what = TYPE_NAME (node);
11734 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
11735 && DECL_NAME (TYPE_NAME (node)))
11736 what = DECL_NAME (TYPE_NAME (node));
11739 if (decl)
11741 expanded_location xloc
11742 = expand_location (DECL_SOURCE_LOCATION (decl));
11743 if (what)
11745 if (msg)
11746 warning (OPT_Wdeprecated_declarations,
11747 "%qE is deprecated (declared at %r%s:%d%R): %s",
11748 what, "locus", xloc.file, xloc.line, msg);
11749 else
11750 warning (OPT_Wdeprecated_declarations,
11751 "%qE is deprecated (declared at %r%s:%d%R)",
11752 what, "locus", xloc.file, xloc.line);
11754 else
11756 if (msg)
11757 warning (OPT_Wdeprecated_declarations,
11758 "type is deprecated (declared at %r%s:%d%R): %s",
11759 "locus", xloc.file, xloc.line, msg);
11760 else
11761 warning (OPT_Wdeprecated_declarations,
11762 "type is deprecated (declared at %r%s:%d%R)",
11763 "locus", xloc.file, xloc.line);
11766 else
11768 if (what)
11770 if (msg)
11771 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
11772 what, msg);
11773 else
11774 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
11776 else
11778 if (msg)
11779 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
11780 msg);
11781 else
11782 warning (OPT_Wdeprecated_declarations, "type is deprecated");
11788 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
11789 somewhere in it. */
11791 bool
11792 contains_bitfld_component_ref_p (const_tree ref)
11794 while (handled_component_p (ref))
11796 if (TREE_CODE (ref) == COMPONENT_REF
11797 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
11798 return true;
11799 ref = TREE_OPERAND (ref, 0);
11802 return false;
11805 #include "gt-tree.h"