* varasm.c (bss_initializer_p): Remove static.
[official-gcc.git] / gcc / tree.c
blob429db49e1a8c68b4528722d8e42a00e1ea5f59c4
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 2011, 2012 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file contains the low level primitives for operating on tree nodes,
23 including allocation, list operations, interning of identifiers,
24 construction of data type nodes and statement nodes,
25 and construction of type conversion nodes. It also contains
26 tables index by tree code that describe how to take apart
27 nodes of that code.
29 It is intended to be language-independent, but occasionally
30 calls language-dependent routines defined (for C) in typecheck.c. */
32 #include "config.h"
33 #include "system.h"
34 #include "coretypes.h"
35 #include "tm.h"
36 #include "flags.h"
37 #include "tree.h"
38 #include "tm_p.h"
39 #include "function.h"
40 #include "obstack.h"
41 #include "toplev.h" /* get_random_seed */
42 #include "ggc.h"
43 #include "hashtab.h"
44 #include "filenames.h"
45 #include "output.h"
46 #include "target.h"
47 #include "common/common-target.h"
48 #include "langhooks.h"
49 #include "tree-inline.h"
50 #include "tree-iterator.h"
51 #include "basic-block.h"
52 #include "tree-flow.h"
53 #include "params.h"
54 #include "pointer-set.h"
55 #include "tree-pass.h"
56 #include "langhooks-def.h"
57 #include "diagnostic.h"
58 #include "tree-diagnostic.h"
59 #include "tree-pretty-print.h"
60 #include "cgraph.h"
61 #include "except.h"
62 #include "debug.h"
63 #include "intl.h"
65 /* Tree code classes. */
67 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
68 #define END_OF_BASE_TREE_CODES tcc_exceptional,
70 const enum tree_code_class tree_code_type[] = {
71 #include "all-tree.def"
74 #undef DEFTREECODE
75 #undef END_OF_BASE_TREE_CODES
77 /* Table indexed by tree code giving number of expression
78 operands beyond the fixed part of the node structure.
79 Not used for types or decls. */
81 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
82 #define END_OF_BASE_TREE_CODES 0,
84 const unsigned char tree_code_length[] = {
85 #include "all-tree.def"
88 #undef DEFTREECODE
89 #undef END_OF_BASE_TREE_CODES
91 /* Names of tree components.
92 Used for printing out the tree and error messages. */
93 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
94 #define END_OF_BASE_TREE_CODES "@dummy",
96 const char *const tree_code_name[] = {
97 #include "all-tree.def"
100 #undef DEFTREECODE
101 #undef END_OF_BASE_TREE_CODES
103 /* Each tree code class has an associated string representation.
104 These must correspond to the tree_code_class entries. */
106 const char *const tree_code_class_strings[] =
108 "exceptional",
109 "constant",
110 "type",
111 "declaration",
112 "reference",
113 "comparison",
114 "unary",
115 "binary",
116 "statement",
117 "vl_exp",
118 "expression"
121 /* obstack.[ch] explicitly declined to prototype this. */
122 extern int _obstack_allocated_p (struct obstack *h, void *obj);
124 /* Statistics-gathering stuff. */
126 static int tree_code_counts[MAX_TREE_CODES];
127 int tree_node_counts[(int) all_kinds];
128 int tree_node_sizes[(int) all_kinds];
130 /* Keep in sync with tree.h:enum tree_node_kind. */
131 static const char * const tree_node_kind_names[] = {
132 "decls",
133 "types",
134 "blocks",
135 "stmts",
136 "refs",
137 "exprs",
138 "constants",
139 "identifiers",
140 "vecs",
141 "binfos",
142 "ssa names",
143 "constructors",
144 "random kinds",
145 "lang_decl kinds",
146 "lang_type kinds",
147 "omp clauses",
150 /* Unique id for next decl created. */
151 static GTY(()) int next_decl_uid;
152 /* Unique id for next type created. */
153 static GTY(()) int next_type_uid = 1;
154 /* Unique id for next debug decl created. Use negative numbers,
155 to catch erroneous uses. */
156 static GTY(()) int next_debug_decl_uid;
158 /* Since we cannot rehash a type after it is in the table, we have to
159 keep the hash code. */
161 struct GTY(()) type_hash {
162 unsigned long hash;
163 tree type;
166 /* Initial size of the hash table (rounded to next prime). */
167 #define TYPE_HASH_INITIAL_SIZE 1000
169 /* Now here is the hash table. When recording a type, it is added to
170 the slot whose index is the hash code. Note that the hash table is
171 used for several kinds of types (function types, array types and
172 array index range types, for now). While all these live in the
173 same table, they are completely independent, and the hash code is
174 computed differently for each of these. */
176 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
177 htab_t type_hash_table;
179 /* Hash table and temporary node for larger integer const values. */
180 static GTY (()) tree int_cst_node;
181 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
182 htab_t int_cst_hash_table;
184 /* Hash table for optimization flags and target option flags. Use the same
185 hash table for both sets of options. Nodes for building the current
186 optimization and target option nodes. The assumption is most of the time
187 the options created will already be in the hash table, so we avoid
188 allocating and freeing up a node repeatably. */
189 static GTY (()) tree cl_optimization_node;
190 static GTY (()) tree cl_target_option_node;
191 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
192 htab_t cl_option_hash_table;
194 /* General tree->tree mapping structure for use in hash tables. */
197 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
198 htab_t debug_expr_for_decl;
200 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
201 htab_t value_expr_for_decl;
203 static GTY ((if_marked ("tree_vec_map_marked_p"), param_is (struct tree_vec_map)))
204 htab_t debug_args_for_decl;
206 static GTY ((if_marked ("tree_priority_map_marked_p"),
207 param_is (struct tree_priority_map)))
208 htab_t init_priority_for_decl;
210 static void set_type_quals (tree, int);
211 static int type_hash_eq (const void *, const void *);
212 static hashval_t type_hash_hash (const void *);
213 static hashval_t int_cst_hash_hash (const void *);
214 static int int_cst_hash_eq (const void *, const void *);
215 static hashval_t cl_option_hash_hash (const void *);
216 static int cl_option_hash_eq (const void *, const void *);
217 static void print_type_hash_statistics (void);
218 static void print_debug_expr_statistics (void);
219 static void print_value_expr_statistics (void);
220 static int type_hash_marked_p (const void *);
221 static unsigned int type_hash_list (const_tree, hashval_t);
222 static unsigned int attribute_hash_list (const_tree, hashval_t);
224 tree global_trees[TI_MAX];
225 tree integer_types[itk_none];
227 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
229 /* Number of operands for each OpenMP clause. */
230 unsigned const char omp_clause_num_ops[] =
232 0, /* OMP_CLAUSE_ERROR */
233 1, /* OMP_CLAUSE_PRIVATE */
234 1, /* OMP_CLAUSE_SHARED */
235 1, /* OMP_CLAUSE_FIRSTPRIVATE */
236 2, /* OMP_CLAUSE_LASTPRIVATE */
237 4, /* OMP_CLAUSE_REDUCTION */
238 1, /* OMP_CLAUSE_COPYIN */
239 1, /* OMP_CLAUSE_COPYPRIVATE */
240 1, /* OMP_CLAUSE_IF */
241 1, /* OMP_CLAUSE_NUM_THREADS */
242 1, /* OMP_CLAUSE_SCHEDULE */
243 0, /* OMP_CLAUSE_NOWAIT */
244 0, /* OMP_CLAUSE_ORDERED */
245 0, /* OMP_CLAUSE_DEFAULT */
246 3, /* OMP_CLAUSE_COLLAPSE */
247 0, /* OMP_CLAUSE_UNTIED */
248 1, /* OMP_CLAUSE_FINAL */
249 0 /* OMP_CLAUSE_MERGEABLE */
252 const char * const omp_clause_code_name[] =
254 "error_clause",
255 "private",
256 "shared",
257 "firstprivate",
258 "lastprivate",
259 "reduction",
260 "copyin",
261 "copyprivate",
262 "if",
263 "num_threads",
264 "schedule",
265 "nowait",
266 "ordered",
267 "default",
268 "collapse",
269 "untied",
270 "final",
271 "mergeable"
275 /* Return the tree node structure used by tree code CODE. */
277 static inline enum tree_node_structure_enum
278 tree_node_structure_for_code (enum tree_code code)
280 switch (TREE_CODE_CLASS (code))
282 case tcc_declaration:
284 switch (code)
286 case FIELD_DECL:
287 return TS_FIELD_DECL;
288 case PARM_DECL:
289 return TS_PARM_DECL;
290 case VAR_DECL:
291 return TS_VAR_DECL;
292 case LABEL_DECL:
293 return TS_LABEL_DECL;
294 case RESULT_DECL:
295 return TS_RESULT_DECL;
296 case DEBUG_EXPR_DECL:
297 return TS_DECL_WRTL;
298 case CONST_DECL:
299 return TS_CONST_DECL;
300 case TYPE_DECL:
301 return TS_TYPE_DECL;
302 case FUNCTION_DECL:
303 return TS_FUNCTION_DECL;
304 case TRANSLATION_UNIT_DECL:
305 return TS_TRANSLATION_UNIT_DECL;
306 default:
307 return TS_DECL_NON_COMMON;
310 case tcc_type:
311 return TS_TYPE_NON_COMMON;
312 case tcc_reference:
313 case tcc_comparison:
314 case tcc_unary:
315 case tcc_binary:
316 case tcc_expression:
317 case tcc_statement:
318 case tcc_vl_exp:
319 return TS_EXP;
320 default: /* tcc_constant and tcc_exceptional */
321 break;
323 switch (code)
325 /* tcc_constant cases. */
326 case INTEGER_CST: return TS_INT_CST;
327 case REAL_CST: return TS_REAL_CST;
328 case FIXED_CST: return TS_FIXED_CST;
329 case COMPLEX_CST: return TS_COMPLEX;
330 case VECTOR_CST: return TS_VECTOR;
331 case STRING_CST: return TS_STRING;
332 /* tcc_exceptional cases. */
333 case ERROR_MARK: return TS_COMMON;
334 case IDENTIFIER_NODE: return TS_IDENTIFIER;
335 case TREE_LIST: return TS_LIST;
336 case TREE_VEC: return TS_VEC;
337 case SSA_NAME: return TS_SSA_NAME;
338 case PLACEHOLDER_EXPR: return TS_COMMON;
339 case STATEMENT_LIST: return TS_STATEMENT_LIST;
340 case BLOCK: return TS_BLOCK;
341 case CONSTRUCTOR: return TS_CONSTRUCTOR;
342 case TREE_BINFO: return TS_BINFO;
343 case OMP_CLAUSE: return TS_OMP_CLAUSE;
344 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
345 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
347 default:
348 gcc_unreachable ();
353 /* Initialize tree_contains_struct to describe the hierarchy of tree
354 nodes. */
356 static void
357 initialize_tree_contains_struct (void)
359 unsigned i;
361 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
363 enum tree_code code;
364 enum tree_node_structure_enum ts_code;
366 code = (enum tree_code) i;
367 ts_code = tree_node_structure_for_code (code);
369 /* Mark the TS structure itself. */
370 tree_contains_struct[code][ts_code] = 1;
372 /* Mark all the structures that TS is derived from. */
373 switch (ts_code)
375 case TS_TYPED:
376 case TS_BLOCK:
377 MARK_TS_BASE (code);
378 break;
380 case TS_COMMON:
381 case TS_INT_CST:
382 case TS_REAL_CST:
383 case TS_FIXED_CST:
384 case TS_VECTOR:
385 case TS_STRING:
386 case TS_COMPLEX:
387 case TS_SSA_NAME:
388 case TS_CONSTRUCTOR:
389 case TS_EXP:
390 case TS_STATEMENT_LIST:
391 MARK_TS_TYPED (code);
392 break;
394 case TS_IDENTIFIER:
395 case TS_DECL_MINIMAL:
396 case TS_TYPE_COMMON:
397 case TS_LIST:
398 case TS_VEC:
399 case TS_BINFO:
400 case TS_OMP_CLAUSE:
401 case TS_OPTIMIZATION:
402 case TS_TARGET_OPTION:
403 MARK_TS_COMMON (code);
404 break;
406 case TS_TYPE_WITH_LANG_SPECIFIC:
407 MARK_TS_TYPE_COMMON (code);
408 break;
410 case TS_TYPE_NON_COMMON:
411 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
412 break;
414 case TS_DECL_COMMON:
415 MARK_TS_DECL_MINIMAL (code);
416 break;
418 case TS_DECL_WRTL:
419 case TS_CONST_DECL:
420 MARK_TS_DECL_COMMON (code);
421 break;
423 case TS_DECL_NON_COMMON:
424 MARK_TS_DECL_WITH_VIS (code);
425 break;
427 case TS_DECL_WITH_VIS:
428 case TS_PARM_DECL:
429 case TS_LABEL_DECL:
430 case TS_RESULT_DECL:
431 MARK_TS_DECL_WRTL (code);
432 break;
434 case TS_FIELD_DECL:
435 MARK_TS_DECL_COMMON (code);
436 break;
438 case TS_VAR_DECL:
439 MARK_TS_DECL_WITH_VIS (code);
440 break;
442 case TS_TYPE_DECL:
443 case TS_FUNCTION_DECL:
444 MARK_TS_DECL_NON_COMMON (code);
445 break;
447 case TS_TRANSLATION_UNIT_DECL:
448 MARK_TS_DECL_COMMON (code);
449 break;
451 default:
452 gcc_unreachable ();
456 /* Basic consistency checks for attributes used in fold. */
457 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
458 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
459 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
460 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
461 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
462 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
463 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
464 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
465 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
466 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
467 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
468 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
469 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
470 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
471 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
472 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
473 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
474 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
475 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
476 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
477 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
478 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
479 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
480 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
481 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
482 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
483 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
484 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
485 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
486 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
487 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
488 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
489 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
490 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
491 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
492 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
493 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
494 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
498 /* Init tree.c. */
500 void
501 init_ttree (void)
503 /* Initialize the hash table of types. */
504 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
505 type_hash_eq, 0);
507 debug_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
508 tree_decl_map_eq, 0);
510 value_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
511 tree_decl_map_eq, 0);
512 init_priority_for_decl = htab_create_ggc (512, tree_priority_map_hash,
513 tree_priority_map_eq, 0);
515 int_cst_hash_table = htab_create_ggc (1024, int_cst_hash_hash,
516 int_cst_hash_eq, NULL);
518 int_cst_node = make_node (INTEGER_CST);
520 cl_option_hash_table = htab_create_ggc (64, cl_option_hash_hash,
521 cl_option_hash_eq, NULL);
523 cl_optimization_node = make_node (OPTIMIZATION_NODE);
524 cl_target_option_node = make_node (TARGET_OPTION_NODE);
526 /* Initialize the tree_contains_struct array. */
527 initialize_tree_contains_struct ();
528 lang_hooks.init_ts ();
532 /* The name of the object as the assembler will see it (but before any
533 translations made by ASM_OUTPUT_LABELREF). Often this is the same
534 as DECL_NAME. It is an IDENTIFIER_NODE. */
535 tree
536 decl_assembler_name (tree decl)
538 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
539 lang_hooks.set_decl_assembler_name (decl);
540 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
543 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
545 bool
546 decl_assembler_name_equal (tree decl, const_tree asmname)
548 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
549 const char *decl_str;
550 const char *asmname_str;
551 bool test = false;
553 if (decl_asmname == asmname)
554 return true;
556 decl_str = IDENTIFIER_POINTER (decl_asmname);
557 asmname_str = IDENTIFIER_POINTER (asmname);
560 /* If the target assembler name was set by the user, things are trickier.
561 We have a leading '*' to begin with. After that, it's arguable what
562 is the correct thing to do with -fleading-underscore. Arguably, we've
563 historically been doing the wrong thing in assemble_alias by always
564 printing the leading underscore. Since we're not changing that, make
565 sure user_label_prefix follows the '*' before matching. */
566 if (decl_str[0] == '*')
568 size_t ulp_len = strlen (user_label_prefix);
570 decl_str ++;
572 if (ulp_len == 0)
573 test = true;
574 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
575 decl_str += ulp_len, test=true;
576 else
577 decl_str --;
579 if (asmname_str[0] == '*')
581 size_t ulp_len = strlen (user_label_prefix);
583 asmname_str ++;
585 if (ulp_len == 0)
586 test = true;
587 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
588 asmname_str += ulp_len, test=true;
589 else
590 asmname_str --;
593 if (!test)
594 return false;
595 return strcmp (decl_str, asmname_str) == 0;
598 /* Hash asmnames ignoring the user specified marks. */
600 hashval_t
601 decl_assembler_name_hash (const_tree asmname)
603 if (IDENTIFIER_POINTER (asmname)[0] == '*')
605 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
606 size_t ulp_len = strlen (user_label_prefix);
608 if (ulp_len == 0)
610 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
611 decl_str += ulp_len;
613 return htab_hash_string (decl_str);
616 return htab_hash_string (IDENTIFIER_POINTER (asmname));
619 /* Compute the number of bytes occupied by a tree with code CODE.
620 This function cannot be used for nodes that have variable sizes,
621 including TREE_VEC, STRING_CST, and CALL_EXPR. */
622 size_t
623 tree_code_size (enum tree_code code)
625 switch (TREE_CODE_CLASS (code))
627 case tcc_declaration: /* A decl node */
629 switch (code)
631 case FIELD_DECL:
632 return sizeof (struct tree_field_decl);
633 case PARM_DECL:
634 return sizeof (struct tree_parm_decl);
635 case VAR_DECL:
636 return sizeof (struct tree_var_decl);
637 case LABEL_DECL:
638 return sizeof (struct tree_label_decl);
639 case RESULT_DECL:
640 return sizeof (struct tree_result_decl);
641 case CONST_DECL:
642 return sizeof (struct tree_const_decl);
643 case TYPE_DECL:
644 return sizeof (struct tree_type_decl);
645 case FUNCTION_DECL:
646 return sizeof (struct tree_function_decl);
647 case DEBUG_EXPR_DECL:
648 return sizeof (struct tree_decl_with_rtl);
649 default:
650 return sizeof (struct tree_decl_non_common);
654 case tcc_type: /* a type node */
655 return sizeof (struct tree_type_non_common);
657 case tcc_reference: /* a reference */
658 case tcc_expression: /* an expression */
659 case tcc_statement: /* an expression with side effects */
660 case tcc_comparison: /* a comparison expression */
661 case tcc_unary: /* a unary arithmetic expression */
662 case tcc_binary: /* a binary arithmetic expression */
663 return (sizeof (struct tree_exp)
664 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
666 case tcc_constant: /* a constant */
667 switch (code)
669 case INTEGER_CST: return sizeof (struct tree_int_cst);
670 case REAL_CST: return sizeof (struct tree_real_cst);
671 case FIXED_CST: return sizeof (struct tree_fixed_cst);
672 case COMPLEX_CST: return sizeof (struct tree_complex);
673 case VECTOR_CST: return sizeof (struct tree_vector);
674 case STRING_CST: gcc_unreachable ();
675 default:
676 return lang_hooks.tree_size (code);
679 case tcc_exceptional: /* something random, like an identifier. */
680 switch (code)
682 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
683 case TREE_LIST: return sizeof (struct tree_list);
685 case ERROR_MARK:
686 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
688 case TREE_VEC:
689 case OMP_CLAUSE: gcc_unreachable ();
691 case SSA_NAME: return sizeof (struct tree_ssa_name);
693 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
694 case BLOCK: return sizeof (struct tree_block);
695 case CONSTRUCTOR: return sizeof (struct tree_constructor);
696 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
697 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
699 default:
700 return lang_hooks.tree_size (code);
703 default:
704 gcc_unreachable ();
708 /* Compute the number of bytes occupied by NODE. This routine only
709 looks at TREE_CODE, except for those nodes that have variable sizes. */
710 size_t
711 tree_size (const_tree node)
713 const enum tree_code code = TREE_CODE (node);
714 switch (code)
716 case TREE_BINFO:
717 return (offsetof (struct tree_binfo, base_binfos)
718 + vec<tree, va_gc>
719 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
721 case TREE_VEC:
722 return (sizeof (struct tree_vec)
723 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
725 case VECTOR_CST:
726 return (sizeof (struct tree_vector)
727 + (TYPE_VECTOR_SUBPARTS (TREE_TYPE (node)) - 1) * sizeof (tree));
729 case STRING_CST:
730 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
732 case OMP_CLAUSE:
733 return (sizeof (struct tree_omp_clause)
734 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
735 * sizeof (tree));
737 default:
738 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
739 return (sizeof (struct tree_exp)
740 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
741 else
742 return tree_code_size (code);
746 /* Record interesting allocation statistics for a tree node with CODE
747 and LENGTH. */
749 static void
750 record_node_allocation_statistics (enum tree_code code ATTRIBUTE_UNUSED,
751 size_t length ATTRIBUTE_UNUSED)
753 enum tree_code_class type = TREE_CODE_CLASS (code);
754 tree_node_kind kind;
756 if (!GATHER_STATISTICS)
757 return;
759 switch (type)
761 case tcc_declaration: /* A decl node */
762 kind = d_kind;
763 break;
765 case tcc_type: /* a type node */
766 kind = t_kind;
767 break;
769 case tcc_statement: /* an expression with side effects */
770 kind = s_kind;
771 break;
773 case tcc_reference: /* a reference */
774 kind = r_kind;
775 break;
777 case tcc_expression: /* an expression */
778 case tcc_comparison: /* a comparison expression */
779 case tcc_unary: /* a unary arithmetic expression */
780 case tcc_binary: /* a binary arithmetic expression */
781 kind = e_kind;
782 break;
784 case tcc_constant: /* a constant */
785 kind = c_kind;
786 break;
788 case tcc_exceptional: /* something random, like an identifier. */
789 switch (code)
791 case IDENTIFIER_NODE:
792 kind = id_kind;
793 break;
795 case TREE_VEC:
796 kind = vec_kind;
797 break;
799 case TREE_BINFO:
800 kind = binfo_kind;
801 break;
803 case SSA_NAME:
804 kind = ssa_name_kind;
805 break;
807 case BLOCK:
808 kind = b_kind;
809 break;
811 case CONSTRUCTOR:
812 kind = constr_kind;
813 break;
815 case OMP_CLAUSE:
816 kind = omp_clause_kind;
817 break;
819 default:
820 kind = x_kind;
821 break;
823 break;
825 case tcc_vl_exp:
826 kind = e_kind;
827 break;
829 default:
830 gcc_unreachable ();
833 tree_code_counts[(int) code]++;
834 tree_node_counts[(int) kind]++;
835 tree_node_sizes[(int) kind] += length;
838 /* Allocate and return a new UID from the DECL_UID namespace. */
841 allocate_decl_uid (void)
843 return next_decl_uid++;
846 /* Return a newly allocated node of code CODE. For decl and type
847 nodes, some other fields are initialized. The rest of the node is
848 initialized to zero. This function cannot be used for TREE_VEC or
849 OMP_CLAUSE nodes, which is enforced by asserts in tree_code_size.
851 Achoo! I got a code in the node. */
853 tree
854 make_node_stat (enum tree_code code MEM_STAT_DECL)
856 tree t;
857 enum tree_code_class type = TREE_CODE_CLASS (code);
858 size_t length = tree_code_size (code);
860 record_node_allocation_statistics (code, length);
862 t = ggc_alloc_zone_cleared_tree_node_stat (
863 (code == IDENTIFIER_NODE) ? &tree_id_zone : &tree_zone,
864 length PASS_MEM_STAT);
865 TREE_SET_CODE (t, code);
867 switch (type)
869 case tcc_statement:
870 TREE_SIDE_EFFECTS (t) = 1;
871 break;
873 case tcc_declaration:
874 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
876 if (code == FUNCTION_DECL)
878 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
879 DECL_MODE (t) = FUNCTION_MODE;
881 else
882 DECL_ALIGN (t) = 1;
884 DECL_SOURCE_LOCATION (t) = input_location;
885 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
886 DECL_UID (t) = --next_debug_decl_uid;
887 else
889 DECL_UID (t) = allocate_decl_uid ();
890 SET_DECL_PT_UID (t, -1);
892 if (TREE_CODE (t) == LABEL_DECL)
893 LABEL_DECL_UID (t) = -1;
895 break;
897 case tcc_type:
898 TYPE_UID (t) = next_type_uid++;
899 TYPE_ALIGN (t) = BITS_PER_UNIT;
900 TYPE_USER_ALIGN (t) = 0;
901 TYPE_MAIN_VARIANT (t) = t;
902 TYPE_CANONICAL (t) = t;
904 /* Default to no attributes for type, but let target change that. */
905 TYPE_ATTRIBUTES (t) = NULL_TREE;
906 targetm.set_default_type_attributes (t);
908 /* We have not yet computed the alias set for this type. */
909 TYPE_ALIAS_SET (t) = -1;
910 break;
912 case tcc_constant:
913 TREE_CONSTANT (t) = 1;
914 break;
916 case tcc_expression:
917 switch (code)
919 case INIT_EXPR:
920 case MODIFY_EXPR:
921 case VA_ARG_EXPR:
922 case PREDECREMENT_EXPR:
923 case PREINCREMENT_EXPR:
924 case POSTDECREMENT_EXPR:
925 case POSTINCREMENT_EXPR:
926 /* All of these have side-effects, no matter what their
927 operands are. */
928 TREE_SIDE_EFFECTS (t) = 1;
929 break;
931 default:
932 break;
934 break;
936 default:
937 /* Other classes need no special treatment. */
938 break;
941 return t;
944 /* Return a new node with the same contents as NODE except that its
945 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
947 tree
948 copy_node_stat (tree node MEM_STAT_DECL)
950 tree t;
951 enum tree_code code = TREE_CODE (node);
952 size_t length;
954 gcc_assert (code != STATEMENT_LIST);
956 length = tree_size (node);
957 record_node_allocation_statistics (code, length);
958 t = ggc_alloc_zone_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
959 memcpy (t, node, length);
961 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
962 TREE_CHAIN (t) = 0;
963 TREE_ASM_WRITTEN (t) = 0;
964 TREE_VISITED (t) = 0;
966 if (TREE_CODE_CLASS (code) == tcc_declaration)
968 if (code == DEBUG_EXPR_DECL)
969 DECL_UID (t) = --next_debug_decl_uid;
970 else
972 DECL_UID (t) = allocate_decl_uid ();
973 if (DECL_PT_UID_SET_P (node))
974 SET_DECL_PT_UID (t, DECL_PT_UID (node));
976 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
977 && DECL_HAS_VALUE_EXPR_P (node))
979 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
980 DECL_HAS_VALUE_EXPR_P (t) = 1;
982 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
984 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
985 DECL_HAS_INIT_PRIORITY_P (t) = 1;
987 if (TREE_CODE (node) == FUNCTION_DECL)
988 DECL_STRUCT_FUNCTION (t) = NULL;
990 else if (TREE_CODE_CLASS (code) == tcc_type)
992 TYPE_UID (t) = next_type_uid++;
993 /* The following is so that the debug code for
994 the copy is different from the original type.
995 The two statements usually duplicate each other
996 (because they clear fields of the same union),
997 but the optimizer should catch that. */
998 TYPE_SYMTAB_POINTER (t) = 0;
999 TYPE_SYMTAB_ADDRESS (t) = 0;
1001 /* Do not copy the values cache. */
1002 if (TYPE_CACHED_VALUES_P(t))
1004 TYPE_CACHED_VALUES_P (t) = 0;
1005 TYPE_CACHED_VALUES (t) = NULL_TREE;
1009 return t;
1012 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1013 For example, this can copy a list made of TREE_LIST nodes. */
1015 tree
1016 copy_list (tree list)
1018 tree head;
1019 tree prev, next;
1021 if (list == 0)
1022 return 0;
1024 head = prev = copy_node (list);
1025 next = TREE_CHAIN (list);
1026 while (next)
1028 TREE_CHAIN (prev) = copy_node (next);
1029 prev = TREE_CHAIN (prev);
1030 next = TREE_CHAIN (next);
1032 return head;
1036 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1038 tree
1039 build_int_cst (tree type, HOST_WIDE_INT low)
1041 /* Support legacy code. */
1042 if (!type)
1043 type = integer_type_node;
1045 return double_int_to_tree (type, double_int::from_shwi (low));
1048 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1050 tree
1051 build_int_cst_type (tree type, HOST_WIDE_INT low)
1053 gcc_assert (type);
1055 return double_int_to_tree (type, double_int::from_shwi (low));
1058 /* Constructs tree in type TYPE from with value given by CST. Signedness
1059 of CST is assumed to be the same as the signedness of TYPE. */
1061 tree
1062 double_int_to_tree (tree type, double_int cst)
1064 bool sign_extended_type = !TYPE_UNSIGNED (type);
1066 cst = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1068 return build_int_cst_wide (type, cst.low, cst.high);
1071 /* Returns true if CST fits into range of TYPE. Signedness of CST is assumed
1072 to be the same as the signedness of TYPE. */
1074 bool
1075 double_int_fits_to_tree_p (const_tree type, double_int cst)
1077 bool sign_extended_type = !TYPE_UNSIGNED (type);
1079 double_int ext
1080 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1082 return cst == ext;
1085 /* We force the double_int CST to the range of the type TYPE by sign or
1086 zero extending it. OVERFLOWABLE indicates if we are interested in
1087 overflow of the value, when >0 we are only interested in signed
1088 overflow, for <0 we are interested in any overflow. OVERFLOWED
1089 indicates whether overflow has already occurred. CONST_OVERFLOWED
1090 indicates whether constant overflow has already occurred. We force
1091 T's value to be within range of T's type (by setting to 0 or 1 all
1092 the bits outside the type's range). We set TREE_OVERFLOWED if,
1093 OVERFLOWED is nonzero,
1094 or OVERFLOWABLE is >0 and signed overflow occurs
1095 or OVERFLOWABLE is <0 and any overflow occurs
1096 We return a new tree node for the extended double_int. The node
1097 is shared if no overflow flags are set. */
1100 tree
1101 force_fit_type_double (tree type, double_int cst, int overflowable,
1102 bool overflowed)
1104 bool sign_extended_type = !TYPE_UNSIGNED (type);
1106 /* If we need to set overflow flags, return a new unshared node. */
1107 if (overflowed || !double_int_fits_to_tree_p(type, cst))
1109 if (overflowed
1110 || overflowable < 0
1111 || (overflowable > 0 && sign_extended_type))
1113 tree t = make_node (INTEGER_CST);
1114 TREE_INT_CST (t)
1115 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1116 TREE_TYPE (t) = type;
1117 TREE_OVERFLOW (t) = 1;
1118 return t;
1122 /* Else build a shared node. */
1123 return double_int_to_tree (type, cst);
1126 /* These are the hash table functions for the hash table of INTEGER_CST
1127 nodes of a sizetype. */
1129 /* Return the hash code code X, an INTEGER_CST. */
1131 static hashval_t
1132 int_cst_hash_hash (const void *x)
1134 const_tree const t = (const_tree) x;
1136 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1137 ^ htab_hash_pointer (TREE_TYPE (t)));
1140 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1141 is the same as that given by *Y, which is the same. */
1143 static int
1144 int_cst_hash_eq (const void *x, const void *y)
1146 const_tree const xt = (const_tree) x;
1147 const_tree const yt = (const_tree) y;
1149 return (TREE_TYPE (xt) == TREE_TYPE (yt)
1150 && TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1151 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt));
1154 /* Create an INT_CST node of TYPE and value HI:LOW.
1155 The returned node is always shared. For small integers we use a
1156 per-type vector cache, for larger ones we use a single hash table. */
1158 tree
1159 build_int_cst_wide (tree type, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
1161 tree t;
1162 int ix = -1;
1163 int limit = 0;
1165 gcc_assert (type);
1167 switch (TREE_CODE (type))
1169 case NULLPTR_TYPE:
1170 gcc_assert (hi == 0 && low == 0);
1171 /* Fallthru. */
1173 case POINTER_TYPE:
1174 case REFERENCE_TYPE:
1175 /* Cache NULL pointer. */
1176 if (!hi && !low)
1178 limit = 1;
1179 ix = 0;
1181 break;
1183 case BOOLEAN_TYPE:
1184 /* Cache false or true. */
1185 limit = 2;
1186 if (!hi && low < 2)
1187 ix = low;
1188 break;
1190 case INTEGER_TYPE:
1191 case OFFSET_TYPE:
1192 if (TYPE_UNSIGNED (type))
1194 /* Cache 0..N */
1195 limit = INTEGER_SHARE_LIMIT;
1196 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1197 ix = low;
1199 else
1201 /* Cache -1..N */
1202 limit = INTEGER_SHARE_LIMIT + 1;
1203 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1204 ix = low + 1;
1205 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
1206 ix = 0;
1208 break;
1210 case ENUMERAL_TYPE:
1211 break;
1213 default:
1214 gcc_unreachable ();
1217 if (ix >= 0)
1219 /* Look for it in the type's vector of small shared ints. */
1220 if (!TYPE_CACHED_VALUES_P (type))
1222 TYPE_CACHED_VALUES_P (type) = 1;
1223 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1226 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1227 if (t)
1229 /* Make sure no one is clobbering the shared constant. */
1230 gcc_assert (TREE_TYPE (t) == type);
1231 gcc_assert (TREE_INT_CST_LOW (t) == low);
1232 gcc_assert (TREE_INT_CST_HIGH (t) == hi);
1234 else
1236 /* Create a new shared int. */
1237 t = make_node (INTEGER_CST);
1239 TREE_INT_CST_LOW (t) = low;
1240 TREE_INT_CST_HIGH (t) = hi;
1241 TREE_TYPE (t) = type;
1243 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1246 else
1248 /* Use the cache of larger shared ints. */
1249 void **slot;
1251 TREE_INT_CST_LOW (int_cst_node) = low;
1252 TREE_INT_CST_HIGH (int_cst_node) = hi;
1253 TREE_TYPE (int_cst_node) = type;
1255 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
1256 t = (tree) *slot;
1257 if (!t)
1259 /* Insert this one into the hash table. */
1260 t = int_cst_node;
1261 *slot = t;
1262 /* Make a new node for next time round. */
1263 int_cst_node = make_node (INTEGER_CST);
1267 return t;
1270 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1271 and the rest are zeros. */
1273 tree
1274 build_low_bits_mask (tree type, unsigned bits)
1276 double_int mask;
1278 gcc_assert (bits <= TYPE_PRECISION (type));
1280 if (bits == TYPE_PRECISION (type)
1281 && !TYPE_UNSIGNED (type))
1282 /* Sign extended all-ones mask. */
1283 mask = double_int_minus_one;
1284 else
1285 mask = double_int::mask (bits);
1287 return build_int_cst_wide (type, mask.low, mask.high);
1290 /* Checks that X is integer constant that can be expressed in (unsigned)
1291 HOST_WIDE_INT without loss of precision. */
1293 bool
1294 cst_and_fits_in_hwi (const_tree x)
1296 if (TREE_CODE (x) != INTEGER_CST)
1297 return false;
1299 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1300 return false;
1302 return (TREE_INT_CST_HIGH (x) == 0
1303 || TREE_INT_CST_HIGH (x) == -1);
1306 /* Build a newly constructed TREE_VEC node of length LEN. */
1308 tree
1309 make_vector_stat (unsigned len MEM_STAT_DECL)
1311 tree t;
1312 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1314 record_node_allocation_statistics (VECTOR_CST, length);
1316 t = ggc_alloc_zone_cleared_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
1318 TREE_SET_CODE (t, VECTOR_CST);
1319 TREE_CONSTANT (t) = 1;
1321 return t;
1324 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1325 are in a list pointed to by VALS. */
1327 tree
1328 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1330 int over = 0;
1331 unsigned cnt = 0;
1332 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1333 TREE_TYPE (v) = type;
1335 /* Iterate through elements and check for overflow. */
1336 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1338 tree value = vals[cnt];
1340 VECTOR_CST_ELT (v, cnt) = value;
1342 /* Don't crash if we get an address constant. */
1343 if (!CONSTANT_CLASS_P (value))
1344 continue;
1346 over |= TREE_OVERFLOW (value);
1349 TREE_OVERFLOW (v) = over;
1350 return v;
1353 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1354 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1356 tree
1357 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1359 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1360 unsigned HOST_WIDE_INT idx;
1361 tree value;
1363 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1364 vec[idx] = value;
1365 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1366 vec[idx] = build_zero_cst (TREE_TYPE (type));
1368 return build_vector (type, vec);
1371 /* Build a vector of type VECTYPE where all the elements are SCs. */
1372 tree
1373 build_vector_from_val (tree vectype, tree sc)
1375 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1377 if (sc == error_mark_node)
1378 return sc;
1380 /* Verify that the vector type is suitable for SC. Note that there
1381 is some inconsistency in the type-system with respect to restrict
1382 qualifications of pointers. Vector types always have a main-variant
1383 element type and the qualification is applied to the vector-type.
1384 So TREE_TYPE (vector-type) does not return a properly qualified
1385 vector element-type. */
1386 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1387 TREE_TYPE (vectype)));
1389 if (CONSTANT_CLASS_P (sc))
1391 tree *v = XALLOCAVEC (tree, nunits);
1392 for (i = 0; i < nunits; ++i)
1393 v[i] = sc;
1394 return build_vector (vectype, v);
1396 else
1398 vec<constructor_elt, va_gc> *v;
1399 vec_alloc (v, nunits);
1400 for (i = 0; i < nunits; ++i)
1401 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1402 return build_constructor (vectype, v);
1406 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1407 are in the vec pointed to by VALS. */
1408 tree
1409 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1411 tree c = make_node (CONSTRUCTOR);
1412 unsigned int i;
1413 constructor_elt *elt;
1414 bool constant_p = true;
1415 bool side_effects_p = false;
1417 TREE_TYPE (c) = type;
1418 CONSTRUCTOR_ELTS (c) = vals;
1420 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1422 /* Mostly ctors will have elts that don't have side-effects, so
1423 the usual case is to scan all the elements. Hence a single
1424 loop for both const and side effects, rather than one loop
1425 each (with early outs). */
1426 if (!TREE_CONSTANT (elt->value))
1427 constant_p = false;
1428 if (TREE_SIDE_EFFECTS (elt->value))
1429 side_effects_p = true;
1432 TREE_SIDE_EFFECTS (c) = side_effects_p;
1433 TREE_CONSTANT (c) = constant_p;
1435 return c;
1438 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1439 INDEX and VALUE. */
1440 tree
1441 build_constructor_single (tree type, tree index, tree value)
1443 vec<constructor_elt, va_gc> *v;
1444 constructor_elt elt = {index, value};
1446 vec_alloc (v, 1);
1447 v->quick_push (elt);
1449 return build_constructor (type, v);
1453 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1454 are in a list pointed to by VALS. */
1455 tree
1456 build_constructor_from_list (tree type, tree vals)
1458 tree t;
1459 vec<constructor_elt, va_gc> *v = NULL;
1461 if (vals)
1463 vec_alloc (v, list_length (vals));
1464 for (t = vals; t; t = TREE_CHAIN (t))
1465 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1468 return build_constructor (type, v);
1471 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1473 tree
1474 build_fixed (tree type, FIXED_VALUE_TYPE f)
1476 tree v;
1477 FIXED_VALUE_TYPE *fp;
1479 v = make_node (FIXED_CST);
1480 fp = ggc_alloc_fixed_value ();
1481 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1483 TREE_TYPE (v) = type;
1484 TREE_FIXED_CST_PTR (v) = fp;
1485 return v;
1488 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1490 tree
1491 build_real (tree type, REAL_VALUE_TYPE d)
1493 tree v;
1494 REAL_VALUE_TYPE *dp;
1495 int overflow = 0;
1497 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1498 Consider doing it via real_convert now. */
1500 v = make_node (REAL_CST);
1501 dp = ggc_alloc_real_value ();
1502 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1504 TREE_TYPE (v) = type;
1505 TREE_REAL_CST_PTR (v) = dp;
1506 TREE_OVERFLOW (v) = overflow;
1507 return v;
1510 /* Return a new REAL_CST node whose type is TYPE
1511 and whose value is the integer value of the INTEGER_CST node I. */
1513 REAL_VALUE_TYPE
1514 real_value_from_int_cst (const_tree type, const_tree i)
1516 REAL_VALUE_TYPE d;
1518 /* Clear all bits of the real value type so that we can later do
1519 bitwise comparisons to see if two values are the same. */
1520 memset (&d, 0, sizeof d);
1522 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
1523 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
1524 TYPE_UNSIGNED (TREE_TYPE (i)));
1525 return d;
1528 /* Given a tree representing an integer constant I, return a tree
1529 representing the same value as a floating-point constant of type TYPE. */
1531 tree
1532 build_real_from_int_cst (tree type, const_tree i)
1534 tree v;
1535 int overflow = TREE_OVERFLOW (i);
1537 v = build_real (type, real_value_from_int_cst (type, i));
1539 TREE_OVERFLOW (v) |= overflow;
1540 return v;
1543 /* Return a newly constructed STRING_CST node whose value is
1544 the LEN characters at STR.
1545 Note that for a C string literal, LEN should include the trailing NUL.
1546 The TREE_TYPE is not initialized. */
1548 tree
1549 build_string (int len, const char *str)
1551 tree s;
1552 size_t length;
1554 /* Do not waste bytes provided by padding of struct tree_string. */
1555 length = len + offsetof (struct tree_string, str) + 1;
1557 record_node_allocation_statistics (STRING_CST, length);
1559 s = ggc_alloc_tree_node (length);
1561 memset (s, 0, sizeof (struct tree_typed));
1562 TREE_SET_CODE (s, STRING_CST);
1563 TREE_CONSTANT (s) = 1;
1564 TREE_STRING_LENGTH (s) = len;
1565 memcpy (s->string.str, str, len);
1566 s->string.str[len] = '\0';
1568 return s;
1571 /* Return a newly constructed COMPLEX_CST node whose value is
1572 specified by the real and imaginary parts REAL and IMAG.
1573 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1574 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1576 tree
1577 build_complex (tree type, tree real, tree imag)
1579 tree t = make_node (COMPLEX_CST);
1581 TREE_REALPART (t) = real;
1582 TREE_IMAGPART (t) = imag;
1583 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1584 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1585 return t;
1588 /* Return a constant of arithmetic type TYPE which is the
1589 multiplicative identity of the set TYPE. */
1591 tree
1592 build_one_cst (tree type)
1594 switch (TREE_CODE (type))
1596 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1597 case POINTER_TYPE: case REFERENCE_TYPE:
1598 case OFFSET_TYPE:
1599 return build_int_cst (type, 1);
1601 case REAL_TYPE:
1602 return build_real (type, dconst1);
1604 case FIXED_POINT_TYPE:
1605 /* We can only generate 1 for accum types. */
1606 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1607 return build_fixed (type, FCONST1(TYPE_MODE (type)));
1609 case VECTOR_TYPE:
1611 tree scalar = build_one_cst (TREE_TYPE (type));
1613 return build_vector_from_val (type, scalar);
1616 case COMPLEX_TYPE:
1617 return build_complex (type,
1618 build_one_cst (TREE_TYPE (type)),
1619 build_zero_cst (TREE_TYPE (type)));
1621 default:
1622 gcc_unreachable ();
1626 /* Build 0 constant of type TYPE. This is used by constructor folding
1627 and thus the constant should be represented in memory by
1628 zero(es). */
1630 tree
1631 build_zero_cst (tree type)
1633 switch (TREE_CODE (type))
1635 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1636 case POINTER_TYPE: case REFERENCE_TYPE:
1637 case OFFSET_TYPE: case NULLPTR_TYPE:
1638 return build_int_cst (type, 0);
1640 case REAL_TYPE:
1641 return build_real (type, dconst0);
1643 case FIXED_POINT_TYPE:
1644 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
1646 case VECTOR_TYPE:
1648 tree scalar = build_zero_cst (TREE_TYPE (type));
1650 return build_vector_from_val (type, scalar);
1653 case COMPLEX_TYPE:
1655 tree zero = build_zero_cst (TREE_TYPE (type));
1657 return build_complex (type, zero, zero);
1660 default:
1661 if (!AGGREGATE_TYPE_P (type))
1662 return fold_convert (type, integer_zero_node);
1663 return build_constructor (type, NULL);
1668 /* Build a BINFO with LEN language slots. */
1670 tree
1671 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1673 tree t;
1674 size_t length = (offsetof (struct tree_binfo, base_binfos)
1675 + vec<tree, va_gc>::embedded_size (base_binfos));
1677 record_node_allocation_statistics (TREE_BINFO, length);
1679 t = ggc_alloc_zone_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
1681 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1683 TREE_SET_CODE (t, TREE_BINFO);
1685 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
1687 return t;
1690 /* Create a CASE_LABEL_EXPR tree node and return it. */
1692 tree
1693 build_case_label (tree low_value, tree high_value, tree label_decl)
1695 tree t = make_node (CASE_LABEL_EXPR);
1697 TREE_TYPE (t) = void_type_node;
1698 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
1700 CASE_LOW (t) = low_value;
1701 CASE_HIGH (t) = high_value;
1702 CASE_LABEL (t) = label_decl;
1703 CASE_CHAIN (t) = NULL_TREE;
1705 return t;
1708 /* Build a newly constructed TREE_VEC node of length LEN. */
1710 tree
1711 make_tree_vec_stat (int len MEM_STAT_DECL)
1713 tree t;
1714 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1716 record_node_allocation_statistics (TREE_VEC, length);
1718 t = ggc_alloc_zone_cleared_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
1720 TREE_SET_CODE (t, TREE_VEC);
1721 TREE_VEC_LENGTH (t) = len;
1723 return t;
1726 /* Return 1 if EXPR is the integer constant zero or a complex constant
1727 of zero. */
1730 integer_zerop (const_tree expr)
1732 STRIP_NOPS (expr);
1734 switch (TREE_CODE (expr))
1736 case INTEGER_CST:
1737 return (TREE_INT_CST_LOW (expr) == 0
1738 && TREE_INT_CST_HIGH (expr) == 0);
1739 case COMPLEX_CST:
1740 return (integer_zerop (TREE_REALPART (expr))
1741 && integer_zerop (TREE_IMAGPART (expr)));
1742 case VECTOR_CST:
1744 unsigned i;
1745 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1746 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
1747 return false;
1748 return true;
1750 default:
1751 return false;
1755 /* Return 1 if EXPR is the integer constant one or the corresponding
1756 complex constant. */
1759 integer_onep (const_tree expr)
1761 STRIP_NOPS (expr);
1763 switch (TREE_CODE (expr))
1765 case INTEGER_CST:
1766 return (TREE_INT_CST_LOW (expr) == 1
1767 && TREE_INT_CST_HIGH (expr) == 0);
1768 case COMPLEX_CST:
1769 return (integer_onep (TREE_REALPART (expr))
1770 && integer_zerop (TREE_IMAGPART (expr)));
1771 case VECTOR_CST:
1773 unsigned i;
1774 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1775 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
1776 return false;
1777 return true;
1779 default:
1780 return false;
1784 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1785 it contains. Likewise for the corresponding complex constant. */
1788 integer_all_onesp (const_tree expr)
1790 int prec;
1791 int uns;
1793 STRIP_NOPS (expr);
1795 if (TREE_CODE (expr) == COMPLEX_CST
1796 && integer_all_onesp (TREE_REALPART (expr))
1797 && integer_zerop (TREE_IMAGPART (expr)))
1798 return 1;
1800 else if (TREE_CODE (expr) == VECTOR_CST)
1802 unsigned i;
1803 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1804 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
1805 return 0;
1806 return 1;
1809 else if (TREE_CODE (expr) != INTEGER_CST)
1810 return 0;
1812 uns = TYPE_UNSIGNED (TREE_TYPE (expr));
1813 if (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1814 && TREE_INT_CST_HIGH (expr) == -1)
1815 return 1;
1816 if (!uns)
1817 return 0;
1819 prec = TYPE_PRECISION (TREE_TYPE (expr));
1820 if (prec >= HOST_BITS_PER_WIDE_INT)
1822 HOST_WIDE_INT high_value;
1823 int shift_amount;
1825 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1827 /* Can not handle precisions greater than twice the host int size. */
1828 gcc_assert (shift_amount <= HOST_BITS_PER_WIDE_INT);
1829 if (shift_amount == HOST_BITS_PER_WIDE_INT)
1830 /* Shifting by the host word size is undefined according to the ANSI
1831 standard, so we must handle this as a special case. */
1832 high_value = -1;
1833 else
1834 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1836 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1837 && TREE_INT_CST_HIGH (expr) == high_value);
1839 else
1840 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1843 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1844 one bit on). */
1847 integer_pow2p (const_tree expr)
1849 int prec;
1850 unsigned HOST_WIDE_INT high, low;
1852 STRIP_NOPS (expr);
1854 if (TREE_CODE (expr) == COMPLEX_CST
1855 && integer_pow2p (TREE_REALPART (expr))
1856 && integer_zerop (TREE_IMAGPART (expr)))
1857 return 1;
1859 if (TREE_CODE (expr) != INTEGER_CST)
1860 return 0;
1862 prec = TYPE_PRECISION (TREE_TYPE (expr));
1863 high = TREE_INT_CST_HIGH (expr);
1864 low = TREE_INT_CST_LOW (expr);
1866 /* First clear all bits that are beyond the type's precision in case
1867 we've been sign extended. */
1869 if (prec == HOST_BITS_PER_DOUBLE_INT)
1871 else if (prec > HOST_BITS_PER_WIDE_INT)
1872 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1873 else
1875 high = 0;
1876 if (prec < HOST_BITS_PER_WIDE_INT)
1877 low &= ~((HOST_WIDE_INT) (-1) << prec);
1880 if (high == 0 && low == 0)
1881 return 0;
1883 return ((high == 0 && (low & (low - 1)) == 0)
1884 || (low == 0 && (high & (high - 1)) == 0));
1887 /* Return 1 if EXPR is an integer constant other than zero or a
1888 complex constant other than zero. */
1891 integer_nonzerop (const_tree expr)
1893 STRIP_NOPS (expr);
1895 return ((TREE_CODE (expr) == INTEGER_CST
1896 && (TREE_INT_CST_LOW (expr) != 0
1897 || TREE_INT_CST_HIGH (expr) != 0))
1898 || (TREE_CODE (expr) == COMPLEX_CST
1899 && (integer_nonzerop (TREE_REALPART (expr))
1900 || integer_nonzerop (TREE_IMAGPART (expr)))));
1903 /* Return 1 if EXPR is the fixed-point constant zero. */
1906 fixed_zerop (const_tree expr)
1908 return (TREE_CODE (expr) == FIXED_CST
1909 && TREE_FIXED_CST (expr).data.is_zero ());
1912 /* Return the power of two represented by a tree node known to be a
1913 power of two. */
1916 tree_log2 (const_tree expr)
1918 int prec;
1919 HOST_WIDE_INT high, low;
1921 STRIP_NOPS (expr);
1923 if (TREE_CODE (expr) == COMPLEX_CST)
1924 return tree_log2 (TREE_REALPART (expr));
1926 prec = TYPE_PRECISION (TREE_TYPE (expr));
1927 high = TREE_INT_CST_HIGH (expr);
1928 low = TREE_INT_CST_LOW (expr);
1930 /* First clear all bits that are beyond the type's precision in case
1931 we've been sign extended. */
1933 if (prec == HOST_BITS_PER_DOUBLE_INT)
1935 else if (prec > HOST_BITS_PER_WIDE_INT)
1936 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1937 else
1939 high = 0;
1940 if (prec < HOST_BITS_PER_WIDE_INT)
1941 low &= ~((HOST_WIDE_INT) (-1) << prec);
1944 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
1945 : exact_log2 (low));
1948 /* Similar, but return the largest integer Y such that 2 ** Y is less
1949 than or equal to EXPR. */
1952 tree_floor_log2 (const_tree expr)
1954 int prec;
1955 HOST_WIDE_INT high, low;
1957 STRIP_NOPS (expr);
1959 if (TREE_CODE (expr) == COMPLEX_CST)
1960 return tree_log2 (TREE_REALPART (expr));
1962 prec = TYPE_PRECISION (TREE_TYPE (expr));
1963 high = TREE_INT_CST_HIGH (expr);
1964 low = TREE_INT_CST_LOW (expr);
1966 /* First clear all bits that are beyond the type's precision in case
1967 we've been sign extended. Ignore if type's precision hasn't been set
1968 since what we are doing is setting it. */
1970 if (prec == HOST_BITS_PER_DOUBLE_INT || prec == 0)
1972 else if (prec > HOST_BITS_PER_WIDE_INT)
1973 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1974 else
1976 high = 0;
1977 if (prec < HOST_BITS_PER_WIDE_INT)
1978 low &= ~((HOST_WIDE_INT) (-1) << prec);
1981 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
1982 : floor_log2 (low));
1985 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
1986 decimal float constants, so don't return 1 for them. */
1989 real_zerop (const_tree expr)
1991 STRIP_NOPS (expr);
1993 switch (TREE_CODE (expr))
1995 case REAL_CST:
1996 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
1997 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
1998 case COMPLEX_CST:
1999 return real_zerop (TREE_REALPART (expr))
2000 && real_zerop (TREE_IMAGPART (expr));
2001 case VECTOR_CST:
2003 unsigned i;
2004 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2005 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2006 return false;
2007 return true;
2009 default:
2010 return false;
2014 /* Return 1 if EXPR is the real constant one in real or complex form.
2015 Trailing zeroes matter for decimal float constants, so don't return
2016 1 for them. */
2019 real_onep (const_tree expr)
2021 STRIP_NOPS (expr);
2023 switch (TREE_CODE (expr))
2025 case REAL_CST:
2026 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
2027 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2028 case COMPLEX_CST:
2029 return real_onep (TREE_REALPART (expr))
2030 && real_zerop (TREE_IMAGPART (expr));
2031 case VECTOR_CST:
2033 unsigned i;
2034 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2035 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2036 return false;
2037 return true;
2039 default:
2040 return false;
2044 /* Return 1 if EXPR is the real constant two. Trailing zeroes matter
2045 for decimal float constants, so don't return 1 for them. */
2048 real_twop (const_tree expr)
2050 STRIP_NOPS (expr);
2052 switch (TREE_CODE (expr))
2054 case REAL_CST:
2055 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2)
2056 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2057 case COMPLEX_CST:
2058 return real_twop (TREE_REALPART (expr))
2059 && real_zerop (TREE_IMAGPART (expr));
2060 case VECTOR_CST:
2062 unsigned i;
2063 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2064 if (!real_twop (VECTOR_CST_ELT (expr, i)))
2065 return false;
2066 return true;
2068 default:
2069 return false;
2073 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2074 matter for decimal float constants, so don't return 1 for them. */
2077 real_minus_onep (const_tree expr)
2079 STRIP_NOPS (expr);
2081 switch (TREE_CODE (expr))
2083 case REAL_CST:
2084 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2085 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2086 case COMPLEX_CST:
2087 return real_minus_onep (TREE_REALPART (expr))
2088 && real_zerop (TREE_IMAGPART (expr));
2089 case VECTOR_CST:
2091 unsigned i;
2092 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2093 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2094 return false;
2095 return true;
2097 default:
2098 return false;
2102 /* Nonzero if EXP is a constant or a cast of a constant. */
2105 really_constant_p (const_tree exp)
2107 /* This is not quite the same as STRIP_NOPS. It does more. */
2108 while (CONVERT_EXPR_P (exp)
2109 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2110 exp = TREE_OPERAND (exp, 0);
2111 return TREE_CONSTANT (exp);
2114 /* Return first list element whose TREE_VALUE is ELEM.
2115 Return 0 if ELEM is not in LIST. */
2117 tree
2118 value_member (tree elem, tree list)
2120 while (list)
2122 if (elem == TREE_VALUE (list))
2123 return list;
2124 list = TREE_CHAIN (list);
2126 return NULL_TREE;
2129 /* Return first list element whose TREE_PURPOSE is ELEM.
2130 Return 0 if ELEM is not in LIST. */
2132 tree
2133 purpose_member (const_tree elem, tree list)
2135 while (list)
2137 if (elem == TREE_PURPOSE (list))
2138 return list;
2139 list = TREE_CHAIN (list);
2141 return NULL_TREE;
2144 /* Return true if ELEM is in V. */
2146 bool
2147 vec_member (const_tree elem, vec<tree, va_gc> *v)
2149 unsigned ix;
2150 tree t;
2151 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2152 if (elem == t)
2153 return true;
2154 return false;
2157 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2158 NULL_TREE. */
2160 tree
2161 chain_index (int idx, tree chain)
2163 for (; chain && idx > 0; --idx)
2164 chain = TREE_CHAIN (chain);
2165 return chain;
2168 /* Return nonzero if ELEM is part of the chain CHAIN. */
2171 chain_member (const_tree elem, const_tree chain)
2173 while (chain)
2175 if (elem == chain)
2176 return 1;
2177 chain = DECL_CHAIN (chain);
2180 return 0;
2183 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2184 We expect a null pointer to mark the end of the chain.
2185 This is the Lisp primitive `length'. */
2188 list_length (const_tree t)
2190 const_tree p = t;
2191 #ifdef ENABLE_TREE_CHECKING
2192 const_tree q = t;
2193 #endif
2194 int len = 0;
2196 while (p)
2198 p = TREE_CHAIN (p);
2199 #ifdef ENABLE_TREE_CHECKING
2200 if (len % 2)
2201 q = TREE_CHAIN (q);
2202 gcc_assert (p != q);
2203 #endif
2204 len++;
2207 return len;
2210 /* Returns the number of FIELD_DECLs in TYPE. */
2213 fields_length (const_tree type)
2215 tree t = TYPE_FIELDS (type);
2216 int count = 0;
2218 for (; t; t = DECL_CHAIN (t))
2219 if (TREE_CODE (t) == FIELD_DECL)
2220 ++count;
2222 return count;
2225 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2226 UNION_TYPE TYPE, or NULL_TREE if none. */
2228 tree
2229 first_field (const_tree type)
2231 tree t = TYPE_FIELDS (type);
2232 while (t && TREE_CODE (t) != FIELD_DECL)
2233 t = TREE_CHAIN (t);
2234 return t;
2237 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2238 by modifying the last node in chain 1 to point to chain 2.
2239 This is the Lisp primitive `nconc'. */
2241 tree
2242 chainon (tree op1, tree op2)
2244 tree t1;
2246 if (!op1)
2247 return op2;
2248 if (!op2)
2249 return op1;
2251 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2252 continue;
2253 TREE_CHAIN (t1) = op2;
2255 #ifdef ENABLE_TREE_CHECKING
2257 tree t2;
2258 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2259 gcc_assert (t2 != t1);
2261 #endif
2263 return op1;
2266 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2268 tree
2269 tree_last (tree chain)
2271 tree next;
2272 if (chain)
2273 while ((next = TREE_CHAIN (chain)))
2274 chain = next;
2275 return chain;
2278 /* Reverse the order of elements in the chain T,
2279 and return the new head of the chain (old last element). */
2281 tree
2282 nreverse (tree t)
2284 tree prev = 0, decl, next;
2285 for (decl = t; decl; decl = next)
2287 /* We shouldn't be using this function to reverse BLOCK chains; we
2288 have blocks_nreverse for that. */
2289 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2290 next = TREE_CHAIN (decl);
2291 TREE_CHAIN (decl) = prev;
2292 prev = decl;
2294 return prev;
2297 /* Return a newly created TREE_LIST node whose
2298 purpose and value fields are PARM and VALUE. */
2300 tree
2301 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2303 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2304 TREE_PURPOSE (t) = parm;
2305 TREE_VALUE (t) = value;
2306 return t;
2309 /* Build a chain of TREE_LIST nodes from a vector. */
2311 tree
2312 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2314 tree ret = NULL_TREE;
2315 tree *pp = &ret;
2316 unsigned int i;
2317 tree t;
2318 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2320 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2321 pp = &TREE_CHAIN (*pp);
2323 return ret;
2326 /* Return a newly created TREE_LIST node whose
2327 purpose and value fields are PURPOSE and VALUE
2328 and whose TREE_CHAIN is CHAIN. */
2330 tree
2331 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2333 tree node;
2335 node = ggc_alloc_zone_tree_node_stat (&tree_zone, sizeof (struct tree_list)
2336 PASS_MEM_STAT);
2337 memset (node, 0, sizeof (struct tree_common));
2339 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2341 TREE_SET_CODE (node, TREE_LIST);
2342 TREE_CHAIN (node) = chain;
2343 TREE_PURPOSE (node) = purpose;
2344 TREE_VALUE (node) = value;
2345 return node;
2348 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2349 trees. */
2351 vec<tree, va_gc> *
2352 ctor_to_vec (tree ctor)
2354 vec<tree, va_gc> *vec;
2355 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2356 unsigned int ix;
2357 tree val;
2359 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2360 vec->quick_push (val);
2362 return vec;
2365 /* Return the size nominally occupied by an object of type TYPE
2366 when it resides in memory. The value is measured in units of bytes,
2367 and its data type is that normally used for type sizes
2368 (which is the first type created by make_signed_type or
2369 make_unsigned_type). */
2371 tree
2372 size_in_bytes (const_tree type)
2374 tree t;
2376 if (type == error_mark_node)
2377 return integer_zero_node;
2379 type = TYPE_MAIN_VARIANT (type);
2380 t = TYPE_SIZE_UNIT (type);
2382 if (t == 0)
2384 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2385 return size_zero_node;
2388 return t;
2391 /* Return the size of TYPE (in bytes) as a wide integer
2392 or return -1 if the size can vary or is larger than an integer. */
2394 HOST_WIDE_INT
2395 int_size_in_bytes (const_tree type)
2397 tree t;
2399 if (type == error_mark_node)
2400 return 0;
2402 type = TYPE_MAIN_VARIANT (type);
2403 t = TYPE_SIZE_UNIT (type);
2404 if (t == 0
2405 || TREE_CODE (t) != INTEGER_CST
2406 || TREE_INT_CST_HIGH (t) != 0
2407 /* If the result would appear negative, it's too big to represent. */
2408 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
2409 return -1;
2411 return TREE_INT_CST_LOW (t);
2414 /* Return the maximum size of TYPE (in bytes) as a wide integer
2415 or return -1 if the size can vary or is larger than an integer. */
2417 HOST_WIDE_INT
2418 max_int_size_in_bytes (const_tree type)
2420 HOST_WIDE_INT size = -1;
2421 tree size_tree;
2423 /* If this is an array type, check for a possible MAX_SIZE attached. */
2425 if (TREE_CODE (type) == ARRAY_TYPE)
2427 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2429 if (size_tree && host_integerp (size_tree, 1))
2430 size = tree_low_cst (size_tree, 1);
2433 /* If we still haven't been able to get a size, see if the language
2434 can compute a maximum size. */
2436 if (size == -1)
2438 size_tree = lang_hooks.types.max_size (type);
2440 if (size_tree && host_integerp (size_tree, 1))
2441 size = tree_low_cst (size_tree, 1);
2444 return size;
2447 /* Returns a tree for the size of EXP in bytes. */
2449 tree
2450 tree_expr_size (const_tree exp)
2452 if (DECL_P (exp)
2453 && DECL_SIZE_UNIT (exp) != 0)
2454 return DECL_SIZE_UNIT (exp);
2455 else
2456 return size_in_bytes (TREE_TYPE (exp));
2459 /* Return the bit position of FIELD, in bits from the start of the record.
2460 This is a tree of type bitsizetype. */
2462 tree
2463 bit_position (const_tree field)
2465 return bit_from_pos (DECL_FIELD_OFFSET (field),
2466 DECL_FIELD_BIT_OFFSET (field));
2469 /* Likewise, but return as an integer. It must be representable in
2470 that way (since it could be a signed value, we don't have the
2471 option of returning -1 like int_size_in_byte can. */
2473 HOST_WIDE_INT
2474 int_bit_position (const_tree field)
2476 return tree_low_cst (bit_position (field), 0);
2479 /* Return the byte position of FIELD, in bytes from the start of the record.
2480 This is a tree of type sizetype. */
2482 tree
2483 byte_position (const_tree field)
2485 return byte_from_pos (DECL_FIELD_OFFSET (field),
2486 DECL_FIELD_BIT_OFFSET (field));
2489 /* Likewise, but return as an integer. It must be representable in
2490 that way (since it could be a signed value, we don't have the
2491 option of returning -1 like int_size_in_byte can. */
2493 HOST_WIDE_INT
2494 int_byte_position (const_tree field)
2496 return tree_low_cst (byte_position (field), 0);
2499 /* Return the strictest alignment, in bits, that T is known to have. */
2501 unsigned int
2502 expr_align (const_tree t)
2504 unsigned int align0, align1;
2506 switch (TREE_CODE (t))
2508 CASE_CONVERT: case NON_LVALUE_EXPR:
2509 /* If we have conversions, we know that the alignment of the
2510 object must meet each of the alignments of the types. */
2511 align0 = expr_align (TREE_OPERAND (t, 0));
2512 align1 = TYPE_ALIGN (TREE_TYPE (t));
2513 return MAX (align0, align1);
2515 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2516 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2517 case CLEANUP_POINT_EXPR:
2518 /* These don't change the alignment of an object. */
2519 return expr_align (TREE_OPERAND (t, 0));
2521 case COND_EXPR:
2522 /* The best we can do is say that the alignment is the least aligned
2523 of the two arms. */
2524 align0 = expr_align (TREE_OPERAND (t, 1));
2525 align1 = expr_align (TREE_OPERAND (t, 2));
2526 return MIN (align0, align1);
2528 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2529 meaningfully, it's always 1. */
2530 case LABEL_DECL: case CONST_DECL:
2531 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2532 case FUNCTION_DECL:
2533 gcc_assert (DECL_ALIGN (t) != 0);
2534 return DECL_ALIGN (t);
2536 default:
2537 break;
2540 /* Otherwise take the alignment from that of the type. */
2541 return TYPE_ALIGN (TREE_TYPE (t));
2544 /* Return, as a tree node, the number of elements for TYPE (which is an
2545 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2547 tree
2548 array_type_nelts (const_tree type)
2550 tree index_type, min, max;
2552 /* If they did it with unspecified bounds, then we should have already
2553 given an error about it before we got here. */
2554 if (! TYPE_DOMAIN (type))
2555 return error_mark_node;
2557 index_type = TYPE_DOMAIN (type);
2558 min = TYPE_MIN_VALUE (index_type);
2559 max = TYPE_MAX_VALUE (index_type);
2561 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
2562 if (!max)
2563 return error_mark_node;
2565 return (integer_zerop (min)
2566 ? max
2567 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2570 /* If arg is static -- a reference to an object in static storage -- then
2571 return the object. This is not the same as the C meaning of `static'.
2572 If arg isn't static, return NULL. */
2574 tree
2575 staticp (tree arg)
2577 switch (TREE_CODE (arg))
2579 case FUNCTION_DECL:
2580 /* Nested functions are static, even though taking their address will
2581 involve a trampoline as we unnest the nested function and create
2582 the trampoline on the tree level. */
2583 return arg;
2585 case VAR_DECL:
2586 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2587 && ! DECL_THREAD_LOCAL_P (arg)
2588 && ! DECL_DLLIMPORT_P (arg)
2589 ? arg : NULL);
2591 case CONST_DECL:
2592 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2593 ? arg : NULL);
2595 case CONSTRUCTOR:
2596 return TREE_STATIC (arg) ? arg : NULL;
2598 case LABEL_DECL:
2599 case STRING_CST:
2600 return arg;
2602 case COMPONENT_REF:
2603 /* If the thing being referenced is not a field, then it is
2604 something language specific. */
2605 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
2607 /* If we are referencing a bitfield, we can't evaluate an
2608 ADDR_EXPR at compile time and so it isn't a constant. */
2609 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2610 return NULL;
2612 return staticp (TREE_OPERAND (arg, 0));
2614 case BIT_FIELD_REF:
2615 return NULL;
2617 case INDIRECT_REF:
2618 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2620 case ARRAY_REF:
2621 case ARRAY_RANGE_REF:
2622 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2623 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2624 return staticp (TREE_OPERAND (arg, 0));
2625 else
2626 return NULL;
2628 case COMPOUND_LITERAL_EXPR:
2629 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
2631 default:
2632 return NULL;
2639 /* Return whether OP is a DECL whose address is function-invariant. */
2641 bool
2642 decl_address_invariant_p (const_tree op)
2644 /* The conditions below are slightly less strict than the one in
2645 staticp. */
2647 switch (TREE_CODE (op))
2649 case PARM_DECL:
2650 case RESULT_DECL:
2651 case LABEL_DECL:
2652 case FUNCTION_DECL:
2653 return true;
2655 case VAR_DECL:
2656 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2657 || DECL_THREAD_LOCAL_P (op)
2658 || DECL_CONTEXT (op) == current_function_decl
2659 || decl_function_context (op) == current_function_decl)
2660 return true;
2661 break;
2663 case CONST_DECL:
2664 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2665 || decl_function_context (op) == current_function_decl)
2666 return true;
2667 break;
2669 default:
2670 break;
2673 return false;
2676 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
2678 bool
2679 decl_address_ip_invariant_p (const_tree op)
2681 /* The conditions below are slightly less strict than the one in
2682 staticp. */
2684 switch (TREE_CODE (op))
2686 case LABEL_DECL:
2687 case FUNCTION_DECL:
2688 case STRING_CST:
2689 return true;
2691 case VAR_DECL:
2692 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2693 && !DECL_DLLIMPORT_P (op))
2694 || DECL_THREAD_LOCAL_P (op))
2695 return true;
2696 break;
2698 case CONST_DECL:
2699 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
2700 return true;
2701 break;
2703 default:
2704 break;
2707 return false;
2711 /* Return true if T is function-invariant (internal function, does
2712 not handle arithmetic; that's handled in skip_simple_arithmetic and
2713 tree_invariant_p). */
2715 static bool tree_invariant_p (tree t);
2717 static bool
2718 tree_invariant_p_1 (tree t)
2720 tree op;
2722 if (TREE_CONSTANT (t)
2723 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
2724 return true;
2726 switch (TREE_CODE (t))
2728 case SAVE_EXPR:
2729 return true;
2731 case ADDR_EXPR:
2732 op = TREE_OPERAND (t, 0);
2733 while (handled_component_p (op))
2735 switch (TREE_CODE (op))
2737 case ARRAY_REF:
2738 case ARRAY_RANGE_REF:
2739 if (!tree_invariant_p (TREE_OPERAND (op, 1))
2740 || TREE_OPERAND (op, 2) != NULL_TREE
2741 || TREE_OPERAND (op, 3) != NULL_TREE)
2742 return false;
2743 break;
2745 case COMPONENT_REF:
2746 if (TREE_OPERAND (op, 2) != NULL_TREE)
2747 return false;
2748 break;
2750 default:;
2752 op = TREE_OPERAND (op, 0);
2755 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2757 default:
2758 break;
2761 return false;
2764 /* Return true if T is function-invariant. */
2766 static bool
2767 tree_invariant_p (tree t)
2769 tree inner = skip_simple_arithmetic (t);
2770 return tree_invariant_p_1 (inner);
2773 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
2774 Do this to any expression which may be used in more than one place,
2775 but must be evaluated only once.
2777 Normally, expand_expr would reevaluate the expression each time.
2778 Calling save_expr produces something that is evaluated and recorded
2779 the first time expand_expr is called on it. Subsequent calls to
2780 expand_expr just reuse the recorded value.
2782 The call to expand_expr that generates code that actually computes
2783 the value is the first call *at compile time*. Subsequent calls
2784 *at compile time* generate code to use the saved value.
2785 This produces correct result provided that *at run time* control
2786 always flows through the insns made by the first expand_expr
2787 before reaching the other places where the save_expr was evaluated.
2788 You, the caller of save_expr, must make sure this is so.
2790 Constants, and certain read-only nodes, are returned with no
2791 SAVE_EXPR because that is safe. Expressions containing placeholders
2792 are not touched; see tree.def for an explanation of what these
2793 are used for. */
2795 tree
2796 save_expr (tree expr)
2798 tree t = fold (expr);
2799 tree inner;
2801 /* If the tree evaluates to a constant, then we don't want to hide that
2802 fact (i.e. this allows further folding, and direct checks for constants).
2803 However, a read-only object that has side effects cannot be bypassed.
2804 Since it is no problem to reevaluate literals, we just return the
2805 literal node. */
2806 inner = skip_simple_arithmetic (t);
2807 if (TREE_CODE (inner) == ERROR_MARK)
2808 return inner;
2810 if (tree_invariant_p_1 (inner))
2811 return t;
2813 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
2814 it means that the size or offset of some field of an object depends on
2815 the value within another field.
2817 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
2818 and some variable since it would then need to be both evaluated once and
2819 evaluated more than once. Front-ends must assure this case cannot
2820 happen by surrounding any such subexpressions in their own SAVE_EXPR
2821 and forcing evaluation at the proper time. */
2822 if (contains_placeholder_p (inner))
2823 return t;
2825 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
2826 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
2828 /* This expression might be placed ahead of a jump to ensure that the
2829 value was computed on both sides of the jump. So make sure it isn't
2830 eliminated as dead. */
2831 TREE_SIDE_EFFECTS (t) = 1;
2832 return t;
2835 /* Look inside EXPR and into any simple arithmetic operations. Return
2836 the innermost non-arithmetic node. */
2838 tree
2839 skip_simple_arithmetic (tree expr)
2841 tree inner;
2843 /* We don't care about whether this can be used as an lvalue in this
2844 context. */
2845 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
2846 expr = TREE_OPERAND (expr, 0);
2848 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
2849 a constant, it will be more efficient to not make another SAVE_EXPR since
2850 it will allow better simplification and GCSE will be able to merge the
2851 computations if they actually occur. */
2852 inner = expr;
2853 while (1)
2855 if (UNARY_CLASS_P (inner))
2856 inner = TREE_OPERAND (inner, 0);
2857 else if (BINARY_CLASS_P (inner))
2859 if (tree_invariant_p (TREE_OPERAND (inner, 1)))
2860 inner = TREE_OPERAND (inner, 0);
2861 else if (tree_invariant_p (TREE_OPERAND (inner, 0)))
2862 inner = TREE_OPERAND (inner, 1);
2863 else
2864 break;
2866 else
2867 break;
2870 return inner;
2874 /* Return which tree structure is used by T. */
2876 enum tree_node_structure_enum
2877 tree_node_structure (const_tree t)
2879 const enum tree_code code = TREE_CODE (t);
2880 return tree_node_structure_for_code (code);
2883 /* Set various status flags when building a CALL_EXPR object T. */
2885 static void
2886 process_call_operands (tree t)
2888 bool side_effects = TREE_SIDE_EFFECTS (t);
2889 bool read_only = false;
2890 int i = call_expr_flags (t);
2892 /* Calls have side-effects, except those to const or pure functions. */
2893 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
2894 side_effects = true;
2895 /* Propagate TREE_READONLY of arguments for const functions. */
2896 if (i & ECF_CONST)
2897 read_only = true;
2899 if (!side_effects || read_only)
2900 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
2902 tree op = TREE_OPERAND (t, i);
2903 if (op && TREE_SIDE_EFFECTS (op))
2904 side_effects = true;
2905 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
2906 read_only = false;
2909 TREE_SIDE_EFFECTS (t) = side_effects;
2910 TREE_READONLY (t) = read_only;
2913 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
2914 size or offset that depends on a field within a record. */
2916 bool
2917 contains_placeholder_p (const_tree exp)
2919 enum tree_code code;
2921 if (!exp)
2922 return 0;
2924 code = TREE_CODE (exp);
2925 if (code == PLACEHOLDER_EXPR)
2926 return 1;
2928 switch (TREE_CODE_CLASS (code))
2930 case tcc_reference:
2931 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
2932 position computations since they will be converted into a
2933 WITH_RECORD_EXPR involving the reference, which will assume
2934 here will be valid. */
2935 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2937 case tcc_exceptional:
2938 if (code == TREE_LIST)
2939 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
2940 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
2941 break;
2943 case tcc_unary:
2944 case tcc_binary:
2945 case tcc_comparison:
2946 case tcc_expression:
2947 switch (code)
2949 case COMPOUND_EXPR:
2950 /* Ignoring the first operand isn't quite right, but works best. */
2951 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
2953 case COND_EXPR:
2954 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2955 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
2956 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
2958 case SAVE_EXPR:
2959 /* The save_expr function never wraps anything containing
2960 a PLACEHOLDER_EXPR. */
2961 return 0;
2963 default:
2964 break;
2967 switch (TREE_CODE_LENGTH (code))
2969 case 1:
2970 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2971 case 2:
2972 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2973 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
2974 default:
2975 return 0;
2978 case tcc_vl_exp:
2979 switch (code)
2981 case CALL_EXPR:
2983 const_tree arg;
2984 const_call_expr_arg_iterator iter;
2985 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
2986 if (CONTAINS_PLACEHOLDER_P (arg))
2987 return 1;
2988 return 0;
2990 default:
2991 return 0;
2994 default:
2995 return 0;
2997 return 0;
3000 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3001 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3002 field positions. */
3004 static bool
3005 type_contains_placeholder_1 (const_tree type)
3007 /* If the size contains a placeholder or the parent type (component type in
3008 the case of arrays) type involves a placeholder, this type does. */
3009 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3010 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3011 || (!POINTER_TYPE_P (type)
3012 && TREE_TYPE (type)
3013 && type_contains_placeholder_p (TREE_TYPE (type))))
3014 return true;
3016 /* Now do type-specific checks. Note that the last part of the check above
3017 greatly limits what we have to do below. */
3018 switch (TREE_CODE (type))
3020 case VOID_TYPE:
3021 case COMPLEX_TYPE:
3022 case ENUMERAL_TYPE:
3023 case BOOLEAN_TYPE:
3024 case POINTER_TYPE:
3025 case OFFSET_TYPE:
3026 case REFERENCE_TYPE:
3027 case METHOD_TYPE:
3028 case FUNCTION_TYPE:
3029 case VECTOR_TYPE:
3030 case NULLPTR_TYPE:
3031 return false;
3033 case INTEGER_TYPE:
3034 case REAL_TYPE:
3035 case FIXED_POINT_TYPE:
3036 /* Here we just check the bounds. */
3037 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3038 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3040 case ARRAY_TYPE:
3041 /* We have already checked the component type above, so just check the
3042 domain type. */
3043 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3045 case RECORD_TYPE:
3046 case UNION_TYPE:
3047 case QUAL_UNION_TYPE:
3049 tree field;
3051 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3052 if (TREE_CODE (field) == FIELD_DECL
3053 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3054 || (TREE_CODE (type) == QUAL_UNION_TYPE
3055 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3056 || type_contains_placeholder_p (TREE_TYPE (field))))
3057 return true;
3059 return false;
3062 default:
3063 gcc_unreachable ();
3067 /* Wrapper around above function used to cache its result. */
3069 bool
3070 type_contains_placeholder_p (tree type)
3072 bool result;
3074 /* If the contains_placeholder_bits field has been initialized,
3075 then we know the answer. */
3076 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3077 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3079 /* Indicate that we've seen this type node, and the answer is false.
3080 This is what we want to return if we run into recursion via fields. */
3081 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3083 /* Compute the real value. */
3084 result = type_contains_placeholder_1 (type);
3086 /* Store the real value. */
3087 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3089 return result;
3092 /* Push tree EXP onto vector QUEUE if it is not already present. */
3094 static void
3095 push_without_duplicates (tree exp, vec<tree> *queue)
3097 unsigned int i;
3098 tree iter;
3100 FOR_EACH_VEC_ELT (*queue, i, iter)
3101 if (simple_cst_equal (iter, exp) == 1)
3102 break;
3104 if (!iter)
3105 queue->safe_push (exp);
3108 /* Given a tree EXP, find all occurrences of references to fields
3109 in a PLACEHOLDER_EXPR and place them in vector REFS without
3110 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3111 we assume here that EXP contains only arithmetic expressions
3112 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3113 argument list. */
3115 void
3116 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3118 enum tree_code code = TREE_CODE (exp);
3119 tree inner;
3120 int i;
3122 /* We handle TREE_LIST and COMPONENT_REF separately. */
3123 if (code == TREE_LIST)
3125 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3126 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3128 else if (code == COMPONENT_REF)
3130 for (inner = TREE_OPERAND (exp, 0);
3131 REFERENCE_CLASS_P (inner);
3132 inner = TREE_OPERAND (inner, 0))
3135 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3136 push_without_duplicates (exp, refs);
3137 else
3138 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3140 else
3141 switch (TREE_CODE_CLASS (code))
3143 case tcc_constant:
3144 break;
3146 case tcc_declaration:
3147 /* Variables allocated to static storage can stay. */
3148 if (!TREE_STATIC (exp))
3149 push_without_duplicates (exp, refs);
3150 break;
3152 case tcc_expression:
3153 /* This is the pattern built in ada/make_aligning_type. */
3154 if (code == ADDR_EXPR
3155 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3157 push_without_duplicates (exp, refs);
3158 break;
3161 /* Fall through... */
3163 case tcc_exceptional:
3164 case tcc_unary:
3165 case tcc_binary:
3166 case tcc_comparison:
3167 case tcc_reference:
3168 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3169 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3170 break;
3172 case tcc_vl_exp:
3173 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3174 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3175 break;
3177 default:
3178 gcc_unreachable ();
3182 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3183 return a tree with all occurrences of references to F in a
3184 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3185 CONST_DECLs. Note that we assume here that EXP contains only
3186 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3187 occurring only in their argument list. */
3189 tree
3190 substitute_in_expr (tree exp, tree f, tree r)
3192 enum tree_code code = TREE_CODE (exp);
3193 tree op0, op1, op2, op3;
3194 tree new_tree;
3196 /* We handle TREE_LIST and COMPONENT_REF separately. */
3197 if (code == TREE_LIST)
3199 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3200 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3201 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3202 return exp;
3204 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3206 else if (code == COMPONENT_REF)
3208 tree inner;
3210 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3211 and it is the right field, replace it with R. */
3212 for (inner = TREE_OPERAND (exp, 0);
3213 REFERENCE_CLASS_P (inner);
3214 inner = TREE_OPERAND (inner, 0))
3217 /* The field. */
3218 op1 = TREE_OPERAND (exp, 1);
3220 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3221 return r;
3223 /* If this expression hasn't been completed let, leave it alone. */
3224 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3225 return exp;
3227 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3228 if (op0 == TREE_OPERAND (exp, 0))
3229 return exp;
3231 new_tree
3232 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3234 else
3235 switch (TREE_CODE_CLASS (code))
3237 case tcc_constant:
3238 return exp;
3240 case tcc_declaration:
3241 if (exp == f)
3242 return r;
3243 else
3244 return exp;
3246 case tcc_expression:
3247 if (exp == f)
3248 return r;
3250 /* Fall through... */
3252 case tcc_exceptional:
3253 case tcc_unary:
3254 case tcc_binary:
3255 case tcc_comparison:
3256 case tcc_reference:
3257 switch (TREE_CODE_LENGTH (code))
3259 case 0:
3260 return exp;
3262 case 1:
3263 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3264 if (op0 == TREE_OPERAND (exp, 0))
3265 return exp;
3267 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3268 break;
3270 case 2:
3271 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3272 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3274 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3275 return exp;
3277 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3278 break;
3280 case 3:
3281 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3282 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3283 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3285 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3286 && op2 == TREE_OPERAND (exp, 2))
3287 return exp;
3289 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3290 break;
3292 case 4:
3293 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3294 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3295 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3296 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3298 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3299 && op2 == TREE_OPERAND (exp, 2)
3300 && op3 == TREE_OPERAND (exp, 3))
3301 return exp;
3303 new_tree
3304 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3305 break;
3307 default:
3308 gcc_unreachable ();
3310 break;
3312 case tcc_vl_exp:
3314 int i;
3316 new_tree = NULL_TREE;
3318 /* If we are trying to replace F with a constant, inline back
3319 functions which do nothing else than computing a value from
3320 the arguments they are passed. This makes it possible to
3321 fold partially or entirely the replacement expression. */
3322 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3324 tree t = maybe_inline_call_in_expr (exp);
3325 if (t)
3326 return SUBSTITUTE_IN_EXPR (t, f, r);
3329 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3331 tree op = TREE_OPERAND (exp, i);
3332 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3333 if (new_op != op)
3335 if (!new_tree)
3336 new_tree = copy_node (exp);
3337 TREE_OPERAND (new_tree, i) = new_op;
3341 if (new_tree)
3343 new_tree = fold (new_tree);
3344 if (TREE_CODE (new_tree) == CALL_EXPR)
3345 process_call_operands (new_tree);
3347 else
3348 return exp;
3350 break;
3352 default:
3353 gcc_unreachable ();
3356 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3358 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3359 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3361 return new_tree;
3364 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3365 for it within OBJ, a tree that is an object or a chain of references. */
3367 tree
3368 substitute_placeholder_in_expr (tree exp, tree obj)
3370 enum tree_code code = TREE_CODE (exp);
3371 tree op0, op1, op2, op3;
3372 tree new_tree;
3374 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3375 in the chain of OBJ. */
3376 if (code == PLACEHOLDER_EXPR)
3378 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3379 tree elt;
3381 for (elt = obj; elt != 0;
3382 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3383 || TREE_CODE (elt) == COND_EXPR)
3384 ? TREE_OPERAND (elt, 1)
3385 : (REFERENCE_CLASS_P (elt)
3386 || UNARY_CLASS_P (elt)
3387 || BINARY_CLASS_P (elt)
3388 || VL_EXP_CLASS_P (elt)
3389 || EXPRESSION_CLASS_P (elt))
3390 ? TREE_OPERAND (elt, 0) : 0))
3391 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3392 return elt;
3394 for (elt = obj; elt != 0;
3395 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3396 || TREE_CODE (elt) == COND_EXPR)
3397 ? TREE_OPERAND (elt, 1)
3398 : (REFERENCE_CLASS_P (elt)
3399 || UNARY_CLASS_P (elt)
3400 || BINARY_CLASS_P (elt)
3401 || VL_EXP_CLASS_P (elt)
3402 || EXPRESSION_CLASS_P (elt))
3403 ? TREE_OPERAND (elt, 0) : 0))
3404 if (POINTER_TYPE_P (TREE_TYPE (elt))
3405 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3406 == need_type))
3407 return fold_build1 (INDIRECT_REF, need_type, elt);
3409 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3410 survives until RTL generation, there will be an error. */
3411 return exp;
3414 /* TREE_LIST is special because we need to look at TREE_VALUE
3415 and TREE_CHAIN, not TREE_OPERANDS. */
3416 else if (code == TREE_LIST)
3418 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3419 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3420 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3421 return exp;
3423 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3425 else
3426 switch (TREE_CODE_CLASS (code))
3428 case tcc_constant:
3429 case tcc_declaration:
3430 return exp;
3432 case tcc_exceptional:
3433 case tcc_unary:
3434 case tcc_binary:
3435 case tcc_comparison:
3436 case tcc_expression:
3437 case tcc_reference:
3438 case tcc_statement:
3439 switch (TREE_CODE_LENGTH (code))
3441 case 0:
3442 return exp;
3444 case 1:
3445 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3446 if (op0 == TREE_OPERAND (exp, 0))
3447 return exp;
3449 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3450 break;
3452 case 2:
3453 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3454 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3456 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3457 return exp;
3459 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3460 break;
3462 case 3:
3463 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3464 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3465 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3467 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3468 && op2 == TREE_OPERAND (exp, 2))
3469 return exp;
3471 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3472 break;
3474 case 4:
3475 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3476 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3477 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3478 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3480 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3481 && op2 == TREE_OPERAND (exp, 2)
3482 && op3 == TREE_OPERAND (exp, 3))
3483 return exp;
3485 new_tree
3486 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3487 break;
3489 default:
3490 gcc_unreachable ();
3492 break;
3494 case tcc_vl_exp:
3496 int i;
3498 new_tree = NULL_TREE;
3500 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3502 tree op = TREE_OPERAND (exp, i);
3503 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
3504 if (new_op != op)
3506 if (!new_tree)
3507 new_tree = copy_node (exp);
3508 TREE_OPERAND (new_tree, i) = new_op;
3512 if (new_tree)
3514 new_tree = fold (new_tree);
3515 if (TREE_CODE (new_tree) == CALL_EXPR)
3516 process_call_operands (new_tree);
3518 else
3519 return exp;
3521 break;
3523 default:
3524 gcc_unreachable ();
3527 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3529 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3530 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3532 return new_tree;
3535 /* Stabilize a reference so that we can use it any number of times
3536 without causing its operands to be evaluated more than once.
3537 Returns the stabilized reference. This works by means of save_expr,
3538 so see the caveats in the comments about save_expr.
3540 Also allows conversion expressions whose operands are references.
3541 Any other kind of expression is returned unchanged. */
3543 tree
3544 stabilize_reference (tree ref)
3546 tree result;
3547 enum tree_code code = TREE_CODE (ref);
3549 switch (code)
3551 case VAR_DECL:
3552 case PARM_DECL:
3553 case RESULT_DECL:
3554 /* No action is needed in this case. */
3555 return ref;
3557 CASE_CONVERT:
3558 case FLOAT_EXPR:
3559 case FIX_TRUNC_EXPR:
3560 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
3561 break;
3563 case INDIRECT_REF:
3564 result = build_nt (INDIRECT_REF,
3565 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
3566 break;
3568 case COMPONENT_REF:
3569 result = build_nt (COMPONENT_REF,
3570 stabilize_reference (TREE_OPERAND (ref, 0)),
3571 TREE_OPERAND (ref, 1), NULL_TREE);
3572 break;
3574 case BIT_FIELD_REF:
3575 result = build_nt (BIT_FIELD_REF,
3576 stabilize_reference (TREE_OPERAND (ref, 0)),
3577 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
3578 break;
3580 case ARRAY_REF:
3581 result = build_nt (ARRAY_REF,
3582 stabilize_reference (TREE_OPERAND (ref, 0)),
3583 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3584 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3585 break;
3587 case ARRAY_RANGE_REF:
3588 result = build_nt (ARRAY_RANGE_REF,
3589 stabilize_reference (TREE_OPERAND (ref, 0)),
3590 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3591 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3592 break;
3594 case COMPOUND_EXPR:
3595 /* We cannot wrap the first expression in a SAVE_EXPR, as then
3596 it wouldn't be ignored. This matters when dealing with
3597 volatiles. */
3598 return stabilize_reference_1 (ref);
3600 /* If arg isn't a kind of lvalue we recognize, make no change.
3601 Caller should recognize the error for an invalid lvalue. */
3602 default:
3603 return ref;
3605 case ERROR_MARK:
3606 return error_mark_node;
3609 TREE_TYPE (result) = TREE_TYPE (ref);
3610 TREE_READONLY (result) = TREE_READONLY (ref);
3611 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
3612 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
3614 return result;
3617 /* Subroutine of stabilize_reference; this is called for subtrees of
3618 references. Any expression with side-effects must be put in a SAVE_EXPR
3619 to ensure that it is only evaluated once.
3621 We don't put SAVE_EXPR nodes around everything, because assigning very
3622 simple expressions to temporaries causes us to miss good opportunities
3623 for optimizations. Among other things, the opportunity to fold in the
3624 addition of a constant into an addressing mode often gets lost, e.g.
3625 "y[i+1] += x;". In general, we take the approach that we should not make
3626 an assignment unless we are forced into it - i.e., that any non-side effect
3627 operator should be allowed, and that cse should take care of coalescing
3628 multiple utterances of the same expression should that prove fruitful. */
3630 tree
3631 stabilize_reference_1 (tree e)
3633 tree result;
3634 enum tree_code code = TREE_CODE (e);
3636 /* We cannot ignore const expressions because it might be a reference
3637 to a const array but whose index contains side-effects. But we can
3638 ignore things that are actual constant or that already have been
3639 handled by this function. */
3641 if (tree_invariant_p (e))
3642 return e;
3644 switch (TREE_CODE_CLASS (code))
3646 case tcc_exceptional:
3647 case tcc_type:
3648 case tcc_declaration:
3649 case tcc_comparison:
3650 case tcc_statement:
3651 case tcc_expression:
3652 case tcc_reference:
3653 case tcc_vl_exp:
3654 /* If the expression has side-effects, then encase it in a SAVE_EXPR
3655 so that it will only be evaluated once. */
3656 /* The reference (r) and comparison (<) classes could be handled as
3657 below, but it is generally faster to only evaluate them once. */
3658 if (TREE_SIDE_EFFECTS (e))
3659 return save_expr (e);
3660 return e;
3662 case tcc_constant:
3663 /* Constants need no processing. In fact, we should never reach
3664 here. */
3665 return e;
3667 case tcc_binary:
3668 /* Division is slow and tends to be compiled with jumps,
3669 especially the division by powers of 2 that is often
3670 found inside of an array reference. So do it just once. */
3671 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3672 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3673 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3674 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3675 return save_expr (e);
3676 /* Recursively stabilize each operand. */
3677 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3678 stabilize_reference_1 (TREE_OPERAND (e, 1)));
3679 break;
3681 case tcc_unary:
3682 /* Recursively stabilize each operand. */
3683 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3684 break;
3686 default:
3687 gcc_unreachable ();
3690 TREE_TYPE (result) = TREE_TYPE (e);
3691 TREE_READONLY (result) = TREE_READONLY (e);
3692 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3693 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3695 return result;
3698 /* Low-level constructors for expressions. */
3700 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
3701 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
3703 void
3704 recompute_tree_invariant_for_addr_expr (tree t)
3706 tree node;
3707 bool tc = true, se = false;
3709 /* We started out assuming this address is both invariant and constant, but
3710 does not have side effects. Now go down any handled components and see if
3711 any of them involve offsets that are either non-constant or non-invariant.
3712 Also check for side-effects.
3714 ??? Note that this code makes no attempt to deal with the case where
3715 taking the address of something causes a copy due to misalignment. */
3717 #define UPDATE_FLAGS(NODE) \
3718 do { tree _node = (NODE); \
3719 if (_node && !TREE_CONSTANT (_node)) tc = false; \
3720 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
3722 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
3723 node = TREE_OPERAND (node, 0))
3725 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
3726 array reference (probably made temporarily by the G++ front end),
3727 so ignore all the operands. */
3728 if ((TREE_CODE (node) == ARRAY_REF
3729 || TREE_CODE (node) == ARRAY_RANGE_REF)
3730 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
3732 UPDATE_FLAGS (TREE_OPERAND (node, 1));
3733 if (TREE_OPERAND (node, 2))
3734 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3735 if (TREE_OPERAND (node, 3))
3736 UPDATE_FLAGS (TREE_OPERAND (node, 3));
3738 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
3739 FIELD_DECL, apparently. The G++ front end can put something else
3740 there, at least temporarily. */
3741 else if (TREE_CODE (node) == COMPONENT_REF
3742 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
3744 if (TREE_OPERAND (node, 2))
3745 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3749 node = lang_hooks.expr_to_decl (node, &tc, &se);
3751 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
3752 the address, since &(*a)->b is a form of addition. If it's a constant, the
3753 address is constant too. If it's a decl, its address is constant if the
3754 decl is static. Everything else is not constant and, furthermore,
3755 taking the address of a volatile variable is not volatile. */
3756 if (TREE_CODE (node) == INDIRECT_REF
3757 || TREE_CODE (node) == MEM_REF)
3758 UPDATE_FLAGS (TREE_OPERAND (node, 0));
3759 else if (CONSTANT_CLASS_P (node))
3761 else if (DECL_P (node))
3762 tc &= (staticp (node) != NULL_TREE);
3763 else
3765 tc = false;
3766 se |= TREE_SIDE_EFFECTS (node);
3770 TREE_CONSTANT (t) = tc;
3771 TREE_SIDE_EFFECTS (t) = se;
3772 #undef UPDATE_FLAGS
3775 /* Build an expression of code CODE, data type TYPE, and operands as
3776 specified. Expressions and reference nodes can be created this way.
3777 Constants, decls, types and misc nodes cannot be.
3779 We define 5 non-variadic functions, from 0 to 4 arguments. This is
3780 enough for all extant tree codes. */
3782 tree
3783 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
3785 tree t;
3787 gcc_assert (TREE_CODE_LENGTH (code) == 0);
3789 t = make_node_stat (code PASS_MEM_STAT);
3790 TREE_TYPE (t) = tt;
3792 return t;
3795 tree
3796 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
3798 int length = sizeof (struct tree_exp);
3799 tree t;
3801 record_node_allocation_statistics (code, length);
3803 gcc_assert (TREE_CODE_LENGTH (code) == 1);
3805 t = ggc_alloc_zone_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
3807 memset (t, 0, sizeof (struct tree_common));
3809 TREE_SET_CODE (t, code);
3811 TREE_TYPE (t) = type;
3812 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
3813 TREE_OPERAND (t, 0) = node;
3814 if (node && !TYPE_P (node))
3816 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
3817 TREE_READONLY (t) = TREE_READONLY (node);
3820 if (TREE_CODE_CLASS (code) == tcc_statement)
3821 TREE_SIDE_EFFECTS (t) = 1;
3822 else switch (code)
3824 case VA_ARG_EXPR:
3825 /* All of these have side-effects, no matter what their
3826 operands are. */
3827 TREE_SIDE_EFFECTS (t) = 1;
3828 TREE_READONLY (t) = 0;
3829 break;
3831 case INDIRECT_REF:
3832 /* Whether a dereference is readonly has nothing to do with whether
3833 its operand is readonly. */
3834 TREE_READONLY (t) = 0;
3835 break;
3837 case ADDR_EXPR:
3838 if (node)
3839 recompute_tree_invariant_for_addr_expr (t);
3840 break;
3842 default:
3843 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
3844 && node && !TYPE_P (node)
3845 && TREE_CONSTANT (node))
3846 TREE_CONSTANT (t) = 1;
3847 if (TREE_CODE_CLASS (code) == tcc_reference
3848 && node && TREE_THIS_VOLATILE (node))
3849 TREE_THIS_VOLATILE (t) = 1;
3850 break;
3853 return t;
3856 #define PROCESS_ARG(N) \
3857 do { \
3858 TREE_OPERAND (t, N) = arg##N; \
3859 if (arg##N &&!TYPE_P (arg##N)) \
3861 if (TREE_SIDE_EFFECTS (arg##N)) \
3862 side_effects = 1; \
3863 if (!TREE_READONLY (arg##N) \
3864 && !CONSTANT_CLASS_P (arg##N)) \
3865 (void) (read_only = 0); \
3866 if (!TREE_CONSTANT (arg##N)) \
3867 (void) (constant = 0); \
3869 } while (0)
3871 tree
3872 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
3874 bool constant, read_only, side_effects;
3875 tree t;
3877 gcc_assert (TREE_CODE_LENGTH (code) == 2);
3879 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
3880 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
3881 /* When sizetype precision doesn't match that of pointers
3882 we need to be able to build explicit extensions or truncations
3883 of the offset argument. */
3884 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
3885 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
3886 && TREE_CODE (arg1) == INTEGER_CST);
3888 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
3889 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
3890 && ptrofftype_p (TREE_TYPE (arg1)));
3892 t = make_node_stat (code PASS_MEM_STAT);
3893 TREE_TYPE (t) = tt;
3895 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
3896 result based on those same flags for the arguments. But if the
3897 arguments aren't really even `tree' expressions, we shouldn't be trying
3898 to do this. */
3900 /* Expressions without side effects may be constant if their
3901 arguments are as well. */
3902 constant = (TREE_CODE_CLASS (code) == tcc_comparison
3903 || TREE_CODE_CLASS (code) == tcc_binary);
3904 read_only = 1;
3905 side_effects = TREE_SIDE_EFFECTS (t);
3907 PROCESS_ARG(0);
3908 PROCESS_ARG(1);
3910 TREE_READONLY (t) = read_only;
3911 TREE_CONSTANT (t) = constant;
3912 TREE_SIDE_EFFECTS (t) = side_effects;
3913 TREE_THIS_VOLATILE (t)
3914 = (TREE_CODE_CLASS (code) == tcc_reference
3915 && arg0 && TREE_THIS_VOLATILE (arg0));
3917 return t;
3921 tree
3922 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3923 tree arg2 MEM_STAT_DECL)
3925 bool constant, read_only, side_effects;
3926 tree t;
3928 gcc_assert (TREE_CODE_LENGTH (code) == 3);
3929 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3931 t = make_node_stat (code PASS_MEM_STAT);
3932 TREE_TYPE (t) = tt;
3934 read_only = 1;
3936 /* As a special exception, if COND_EXPR has NULL branches, we
3937 assume that it is a gimple statement and always consider
3938 it to have side effects. */
3939 if (code == COND_EXPR
3940 && tt == void_type_node
3941 && arg1 == NULL_TREE
3942 && arg2 == NULL_TREE)
3943 side_effects = true;
3944 else
3945 side_effects = TREE_SIDE_EFFECTS (t);
3947 PROCESS_ARG(0);
3948 PROCESS_ARG(1);
3949 PROCESS_ARG(2);
3951 if (code == COND_EXPR)
3952 TREE_READONLY (t) = read_only;
3954 TREE_SIDE_EFFECTS (t) = side_effects;
3955 TREE_THIS_VOLATILE (t)
3956 = (TREE_CODE_CLASS (code) == tcc_reference
3957 && arg0 && TREE_THIS_VOLATILE (arg0));
3959 return t;
3962 tree
3963 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3964 tree arg2, tree arg3 MEM_STAT_DECL)
3966 bool constant, read_only, side_effects;
3967 tree t;
3969 gcc_assert (TREE_CODE_LENGTH (code) == 4);
3971 t = make_node_stat (code PASS_MEM_STAT);
3972 TREE_TYPE (t) = tt;
3974 side_effects = TREE_SIDE_EFFECTS (t);
3976 PROCESS_ARG(0);
3977 PROCESS_ARG(1);
3978 PROCESS_ARG(2);
3979 PROCESS_ARG(3);
3981 TREE_SIDE_EFFECTS (t) = side_effects;
3982 TREE_THIS_VOLATILE (t)
3983 = (TREE_CODE_CLASS (code) == tcc_reference
3984 && arg0 && TREE_THIS_VOLATILE (arg0));
3986 return t;
3989 tree
3990 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3991 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
3993 bool constant, read_only, side_effects;
3994 tree t;
3996 gcc_assert (TREE_CODE_LENGTH (code) == 5);
3998 t = make_node_stat (code PASS_MEM_STAT);
3999 TREE_TYPE (t) = tt;
4001 side_effects = TREE_SIDE_EFFECTS (t);
4003 PROCESS_ARG(0);
4004 PROCESS_ARG(1);
4005 PROCESS_ARG(2);
4006 PROCESS_ARG(3);
4007 PROCESS_ARG(4);
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;
4017 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4018 on the pointer PTR. */
4020 tree
4021 build_simple_mem_ref_loc (location_t loc, tree ptr)
4023 HOST_WIDE_INT offset = 0;
4024 tree ptype = TREE_TYPE (ptr);
4025 tree tem;
4026 /* For convenience allow addresses that collapse to a simple base
4027 and offset. */
4028 if (TREE_CODE (ptr) == ADDR_EXPR
4029 && (handled_component_p (TREE_OPERAND (ptr, 0))
4030 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4032 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4033 gcc_assert (ptr);
4034 ptr = build_fold_addr_expr (ptr);
4035 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4037 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4038 ptr, build_int_cst (ptype, offset));
4039 SET_EXPR_LOCATION (tem, loc);
4040 return tem;
4043 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4045 double_int
4046 mem_ref_offset (const_tree t)
4048 tree toff = TREE_OPERAND (t, 1);
4049 return tree_to_double_int (toff).sext (TYPE_PRECISION (TREE_TYPE (toff)));
4052 /* Return the pointer-type relevant for TBAA purposes from the
4053 gimple memory reference tree T. This is the type to be used for
4054 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T. */
4056 tree
4057 reference_alias_ptr_type (const_tree t)
4059 const_tree base = t;
4060 while (handled_component_p (base))
4061 base = TREE_OPERAND (base, 0);
4062 if (TREE_CODE (base) == MEM_REF)
4063 return TREE_TYPE (TREE_OPERAND (base, 1));
4064 else if (TREE_CODE (base) == TARGET_MEM_REF)
4065 return TREE_TYPE (TMR_OFFSET (base));
4066 else
4067 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (base)));
4070 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4071 offsetted by OFFSET units. */
4073 tree
4074 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4076 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4077 build_fold_addr_expr (base),
4078 build_int_cst (ptr_type_node, offset));
4079 tree addr = build1 (ADDR_EXPR, type, ref);
4080 recompute_tree_invariant_for_addr_expr (addr);
4081 return addr;
4084 /* Similar except don't specify the TREE_TYPE
4085 and leave the TREE_SIDE_EFFECTS as 0.
4086 It is permissible for arguments to be null,
4087 or even garbage if their values do not matter. */
4089 tree
4090 build_nt (enum tree_code code, ...)
4092 tree t;
4093 int length;
4094 int i;
4095 va_list p;
4097 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4099 va_start (p, code);
4101 t = make_node (code);
4102 length = TREE_CODE_LENGTH (code);
4104 for (i = 0; i < length; i++)
4105 TREE_OPERAND (t, i) = va_arg (p, tree);
4107 va_end (p);
4108 return t;
4111 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4112 tree vec. */
4114 tree
4115 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4117 tree ret, t;
4118 unsigned int ix;
4120 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4121 CALL_EXPR_FN (ret) = fn;
4122 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4123 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4124 CALL_EXPR_ARG (ret, ix) = t;
4125 return ret;
4128 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4129 We do NOT enter this node in any sort of symbol table.
4131 LOC is the location of the decl.
4133 layout_decl is used to set up the decl's storage layout.
4134 Other slots are initialized to 0 or null pointers. */
4136 tree
4137 build_decl_stat (location_t loc, enum tree_code code, tree name,
4138 tree type MEM_STAT_DECL)
4140 tree t;
4142 t = make_node_stat (code PASS_MEM_STAT);
4143 DECL_SOURCE_LOCATION (t) = loc;
4145 /* if (type == error_mark_node)
4146 type = integer_type_node; */
4147 /* That is not done, deliberately, so that having error_mark_node
4148 as the type can suppress useless errors in the use of this variable. */
4150 DECL_NAME (t) = name;
4151 TREE_TYPE (t) = type;
4153 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4154 layout_decl (t, 0);
4156 return t;
4159 /* Builds and returns function declaration with NAME and TYPE. */
4161 tree
4162 build_fn_decl (const char *name, tree type)
4164 tree id = get_identifier (name);
4165 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4167 DECL_EXTERNAL (decl) = 1;
4168 TREE_PUBLIC (decl) = 1;
4169 DECL_ARTIFICIAL (decl) = 1;
4170 TREE_NOTHROW (decl) = 1;
4172 return decl;
4175 vec<tree, va_gc> *all_translation_units;
4177 /* Builds a new translation-unit decl with name NAME, queues it in the
4178 global list of translation-unit decls and returns it. */
4180 tree
4181 build_translation_unit_decl (tree name)
4183 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4184 name, NULL_TREE);
4185 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4186 vec_safe_push (all_translation_units, tu);
4187 return tu;
4191 /* BLOCK nodes are used to represent the structure of binding contours
4192 and declarations, once those contours have been exited and their contents
4193 compiled. This information is used for outputting debugging info. */
4195 tree
4196 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4198 tree block = make_node (BLOCK);
4200 BLOCK_VARS (block) = vars;
4201 BLOCK_SUBBLOCKS (block) = subblocks;
4202 BLOCK_SUPERCONTEXT (block) = supercontext;
4203 BLOCK_CHAIN (block) = chain;
4204 return block;
4208 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4210 LOC is the location to use in tree T. */
4212 void
4213 protected_set_expr_location (tree t, location_t loc)
4215 if (t && CAN_HAVE_LOCATION_P (t))
4216 SET_EXPR_LOCATION (t, loc);
4219 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4220 is ATTRIBUTE. */
4222 tree
4223 build_decl_attribute_variant (tree ddecl, tree attribute)
4225 DECL_ATTRIBUTES (ddecl) = attribute;
4226 return ddecl;
4229 /* Borrowed from hashtab.c iterative_hash implementation. */
4230 #define mix(a,b,c) \
4232 a -= b; a -= c; a ^= (c>>13); \
4233 b -= c; b -= a; b ^= (a<< 8); \
4234 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
4235 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
4236 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
4237 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
4238 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
4239 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
4240 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
4244 /* Produce good hash value combining VAL and VAL2. */
4245 hashval_t
4246 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
4248 /* the golden ratio; an arbitrary value. */
4249 hashval_t a = 0x9e3779b9;
4251 mix (a, val, val2);
4252 return val2;
4255 /* Produce good hash value combining VAL and VAL2. */
4256 hashval_t
4257 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
4259 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
4260 return iterative_hash_hashval_t (val, val2);
4261 else
4263 hashval_t a = (hashval_t) val;
4264 /* Avoid warnings about shifting of more than the width of the type on
4265 hosts that won't execute this path. */
4266 int zero = 0;
4267 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
4268 mix (a, b, val2);
4269 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
4271 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
4272 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
4273 mix (a, b, val2);
4275 return val2;
4279 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4280 is ATTRIBUTE and its qualifiers are QUALS.
4282 Record such modified types already made so we don't make duplicates. */
4284 tree
4285 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4287 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4289 hashval_t hashcode = 0;
4290 tree ntype;
4291 enum tree_code code = TREE_CODE (ttype);
4293 /* Building a distinct copy of a tagged type is inappropriate; it
4294 causes breakage in code that expects there to be a one-to-one
4295 relationship between a struct and its fields.
4296 build_duplicate_type is another solution (as used in
4297 handle_transparent_union_attribute), but that doesn't play well
4298 with the stronger C++ type identity model. */
4299 if (TREE_CODE (ttype) == RECORD_TYPE
4300 || TREE_CODE (ttype) == UNION_TYPE
4301 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4302 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4304 warning (OPT_Wattributes,
4305 "ignoring attributes applied to %qT after definition",
4306 TYPE_MAIN_VARIANT (ttype));
4307 return build_qualified_type (ttype, quals);
4310 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4311 ntype = build_distinct_type_copy (ttype);
4313 TYPE_ATTRIBUTES (ntype) = attribute;
4315 hashcode = iterative_hash_object (code, hashcode);
4316 if (TREE_TYPE (ntype))
4317 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
4318 hashcode);
4319 hashcode = attribute_hash_list (attribute, hashcode);
4321 switch (TREE_CODE (ntype))
4323 case FUNCTION_TYPE:
4324 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
4325 break;
4326 case ARRAY_TYPE:
4327 if (TYPE_DOMAIN (ntype))
4328 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
4329 hashcode);
4330 break;
4331 case INTEGER_TYPE:
4332 hashcode = iterative_hash_object
4333 (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
4334 hashcode = iterative_hash_object
4335 (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
4336 break;
4337 case REAL_TYPE:
4338 case FIXED_POINT_TYPE:
4340 unsigned int precision = TYPE_PRECISION (ntype);
4341 hashcode = iterative_hash_object (precision, hashcode);
4343 break;
4344 default:
4345 break;
4348 ntype = type_hash_canon (hashcode, ntype);
4350 /* If the target-dependent attributes make NTYPE different from
4351 its canonical type, we will need to use structural equality
4352 checks for this type. */
4353 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4354 || !comp_type_attributes (ntype, ttype))
4355 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4356 else if (TYPE_CANONICAL (ntype) == ntype)
4357 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4359 ttype = build_qualified_type (ntype, quals);
4361 else if (TYPE_QUALS (ttype) != quals)
4362 ttype = build_qualified_type (ttype, quals);
4364 return ttype;
4367 /* Compare two attributes for their value identity. Return true if the
4368 attribute values are known to be equal; otherwise return false.
4371 static bool
4372 attribute_value_equal (const_tree attr1, const_tree attr2)
4374 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4375 return true;
4377 if (TREE_VALUE (attr1) != NULL_TREE
4378 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4379 && TREE_VALUE (attr2) != NULL
4380 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4381 return (simple_cst_list_equal (TREE_VALUE (attr1),
4382 TREE_VALUE (attr2)) == 1);
4384 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4387 /* Return 0 if the attributes for two types are incompatible, 1 if they
4388 are compatible, and 2 if they are nearly compatible (which causes a
4389 warning to be generated). */
4391 comp_type_attributes (const_tree type1, const_tree type2)
4393 const_tree a1 = TYPE_ATTRIBUTES (type1);
4394 const_tree a2 = TYPE_ATTRIBUTES (type2);
4395 const_tree a;
4397 if (a1 == a2)
4398 return 1;
4399 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4401 const struct attribute_spec *as;
4402 const_tree attr;
4404 as = lookup_attribute_spec (get_attribute_name (a));
4405 if (!as || as->affects_type_identity == false)
4406 continue;
4408 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4409 if (!attr || !attribute_value_equal (a, attr))
4410 break;
4412 if (!a)
4414 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
4416 const struct attribute_spec *as;
4418 as = lookup_attribute_spec (get_attribute_name (a));
4419 if (!as || as->affects_type_identity == false)
4420 continue;
4422 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
4423 break;
4424 /* We don't need to compare trees again, as we did this
4425 already in first loop. */
4427 /* All types - affecting identity - are equal, so
4428 there is no need to call target hook for comparison. */
4429 if (!a)
4430 return 1;
4432 /* As some type combinations - like default calling-convention - might
4433 be compatible, we have to call the target hook to get the final result. */
4434 return targetm.comp_type_attributes (type1, type2);
4437 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4438 is ATTRIBUTE.
4440 Record such modified types already made so we don't make duplicates. */
4442 tree
4443 build_type_attribute_variant (tree ttype, tree attribute)
4445 return build_type_attribute_qual_variant (ttype, attribute,
4446 TYPE_QUALS (ttype));
4450 /* Reset the expression *EXPR_P, a size or position.
4452 ??? We could reset all non-constant sizes or positions. But it's cheap
4453 enough to not do so and refrain from adding workarounds to dwarf2out.c.
4455 We need to reset self-referential sizes or positions because they cannot
4456 be gimplified and thus can contain a CALL_EXPR after the gimplification
4457 is finished, which will run afoul of LTO streaming. And they need to be
4458 reset to something essentially dummy but not constant, so as to preserve
4459 the properties of the object they are attached to. */
4461 static inline void
4462 free_lang_data_in_one_sizepos (tree *expr_p)
4464 tree expr = *expr_p;
4465 if (CONTAINS_PLACEHOLDER_P (expr))
4466 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
4470 /* Reset all the fields in a binfo node BINFO. We only keep
4471 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
4473 static void
4474 free_lang_data_in_binfo (tree binfo)
4476 unsigned i;
4477 tree t;
4479 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
4481 BINFO_VIRTUALS (binfo) = NULL_TREE;
4482 BINFO_BASE_ACCESSES (binfo) = NULL;
4483 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
4484 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
4486 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
4487 free_lang_data_in_binfo (t);
4491 /* Reset all language specific information still present in TYPE. */
4493 static void
4494 free_lang_data_in_type (tree type)
4496 gcc_assert (TYPE_P (type));
4498 /* Give the FE a chance to remove its own data first. */
4499 lang_hooks.free_lang_data (type);
4501 TREE_LANG_FLAG_0 (type) = 0;
4502 TREE_LANG_FLAG_1 (type) = 0;
4503 TREE_LANG_FLAG_2 (type) = 0;
4504 TREE_LANG_FLAG_3 (type) = 0;
4505 TREE_LANG_FLAG_4 (type) = 0;
4506 TREE_LANG_FLAG_5 (type) = 0;
4507 TREE_LANG_FLAG_6 (type) = 0;
4509 if (TREE_CODE (type) == FUNCTION_TYPE)
4511 /* Remove the const and volatile qualifiers from arguments. The
4512 C++ front end removes them, but the C front end does not,
4513 leading to false ODR violation errors when merging two
4514 instances of the same function signature compiled by
4515 different front ends. */
4516 tree p;
4518 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
4520 tree arg_type = TREE_VALUE (p);
4522 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
4524 int quals = TYPE_QUALS (arg_type)
4525 & ~TYPE_QUAL_CONST
4526 & ~TYPE_QUAL_VOLATILE;
4527 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
4528 free_lang_data_in_type (TREE_VALUE (p));
4533 /* Remove members that are not actually FIELD_DECLs from the field
4534 list of an aggregate. These occur in C++. */
4535 if (RECORD_OR_UNION_TYPE_P (type))
4537 tree prev, member;
4539 /* Note that TYPE_FIELDS can be shared across distinct
4540 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
4541 to be removed, we cannot set its TREE_CHAIN to NULL.
4542 Otherwise, we would not be able to find all the other fields
4543 in the other instances of this TREE_TYPE.
4545 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
4546 prev = NULL_TREE;
4547 member = TYPE_FIELDS (type);
4548 while (member)
4550 if (TREE_CODE (member) == FIELD_DECL
4551 || TREE_CODE (member) == TYPE_DECL)
4553 if (prev)
4554 TREE_CHAIN (prev) = member;
4555 else
4556 TYPE_FIELDS (type) = member;
4557 prev = member;
4560 member = TREE_CHAIN (member);
4563 if (prev)
4564 TREE_CHAIN (prev) = NULL_TREE;
4565 else
4566 TYPE_FIELDS (type) = NULL_TREE;
4568 TYPE_METHODS (type) = NULL_TREE;
4569 if (TYPE_BINFO (type))
4570 free_lang_data_in_binfo (TYPE_BINFO (type));
4572 else
4574 /* For non-aggregate types, clear out the language slot (which
4575 overloads TYPE_BINFO). */
4576 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
4578 if (INTEGRAL_TYPE_P (type)
4579 || SCALAR_FLOAT_TYPE_P (type)
4580 || FIXED_POINT_TYPE_P (type))
4582 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
4583 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
4587 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
4588 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
4590 if (TYPE_CONTEXT (type)
4591 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
4593 tree ctx = TYPE_CONTEXT (type);
4596 ctx = BLOCK_SUPERCONTEXT (ctx);
4598 while (ctx && TREE_CODE (ctx) == BLOCK);
4599 TYPE_CONTEXT (type) = ctx;
4604 /* Return true if DECL may need an assembler name to be set. */
4606 static inline bool
4607 need_assembler_name_p (tree decl)
4609 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
4610 if (TREE_CODE (decl) != FUNCTION_DECL
4611 && TREE_CODE (decl) != VAR_DECL)
4612 return false;
4614 /* If DECL already has its assembler name set, it does not need a
4615 new one. */
4616 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
4617 || DECL_ASSEMBLER_NAME_SET_P (decl))
4618 return false;
4620 /* Abstract decls do not need an assembler name. */
4621 if (DECL_ABSTRACT (decl))
4622 return false;
4624 /* For VAR_DECLs, only static, public and external symbols need an
4625 assembler name. */
4626 if (TREE_CODE (decl) == VAR_DECL
4627 && !TREE_STATIC (decl)
4628 && !TREE_PUBLIC (decl)
4629 && !DECL_EXTERNAL (decl))
4630 return false;
4632 if (TREE_CODE (decl) == FUNCTION_DECL)
4634 /* Do not set assembler name on builtins. Allow RTL expansion to
4635 decide whether to expand inline or via a regular call. */
4636 if (DECL_BUILT_IN (decl)
4637 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
4638 return false;
4640 /* Functions represented in the callgraph need an assembler name. */
4641 if (cgraph_get_node (decl) != NULL)
4642 return true;
4644 /* Unused and not public functions don't need an assembler name. */
4645 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
4646 return false;
4649 return true;
4653 /* Reset all language specific information still present in symbol
4654 DECL. */
4656 static void
4657 free_lang_data_in_decl (tree decl)
4659 gcc_assert (DECL_P (decl));
4661 /* Give the FE a chance to remove its own data first. */
4662 lang_hooks.free_lang_data (decl);
4664 TREE_LANG_FLAG_0 (decl) = 0;
4665 TREE_LANG_FLAG_1 (decl) = 0;
4666 TREE_LANG_FLAG_2 (decl) = 0;
4667 TREE_LANG_FLAG_3 (decl) = 0;
4668 TREE_LANG_FLAG_4 (decl) = 0;
4669 TREE_LANG_FLAG_5 (decl) = 0;
4670 TREE_LANG_FLAG_6 (decl) = 0;
4672 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
4673 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
4674 if (TREE_CODE (decl) == FIELD_DECL)
4676 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
4677 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
4678 DECL_QUALIFIER (decl) = NULL_TREE;
4681 if (TREE_CODE (decl) == FUNCTION_DECL)
4683 if (gimple_has_body_p (decl))
4685 tree t;
4687 /* If DECL has a gimple body, then the context for its
4688 arguments must be DECL. Otherwise, it doesn't really
4689 matter, as we will not be emitting any code for DECL. In
4690 general, there may be other instances of DECL created by
4691 the front end and since PARM_DECLs are generally shared,
4692 their DECL_CONTEXT changes as the replicas of DECL are
4693 created. The only time where DECL_CONTEXT is important
4694 is for the FUNCTION_DECLs that have a gimple body (since
4695 the PARM_DECL will be used in the function's body). */
4696 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
4697 DECL_CONTEXT (t) = decl;
4700 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
4701 At this point, it is not needed anymore. */
4702 DECL_SAVED_TREE (decl) = NULL_TREE;
4704 /* Clear the abstract origin if it refers to a method. Otherwise
4705 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
4706 origin will not be output correctly. */
4707 if (DECL_ABSTRACT_ORIGIN (decl)
4708 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
4709 && RECORD_OR_UNION_TYPE_P
4710 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
4711 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
4713 /* Sometimes the C++ frontend doesn't manage to transform a temporary
4714 DECL_VINDEX referring to itself into a vtable slot number as it
4715 should. Happens with functions that are copied and then forgotten
4716 about. Just clear it, it won't matter anymore. */
4717 if (DECL_VINDEX (decl) && !host_integerp (DECL_VINDEX (decl), 0))
4718 DECL_VINDEX (decl) = NULL_TREE;
4720 else if (TREE_CODE (decl) == VAR_DECL)
4722 if ((DECL_EXTERNAL (decl)
4723 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
4724 || (decl_function_context (decl) && !TREE_STATIC (decl)))
4725 DECL_INITIAL (decl) = NULL_TREE;
4727 else if (TREE_CODE (decl) == TYPE_DECL
4728 || TREE_CODE (decl) == FIELD_DECL)
4729 DECL_INITIAL (decl) = NULL_TREE;
4730 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
4731 && DECL_INITIAL (decl)
4732 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
4734 /* Strip builtins from the translation-unit BLOCK. We still have targets
4735 without builtin_decl_explicit support and also builtins are shared
4736 nodes and thus we can't use TREE_CHAIN in multiple lists. */
4737 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
4738 while (*nextp)
4740 tree var = *nextp;
4741 if (TREE_CODE (var) == FUNCTION_DECL
4742 && DECL_BUILT_IN (var))
4743 *nextp = TREE_CHAIN (var);
4744 else
4745 nextp = &TREE_CHAIN (var);
4751 /* Data used when collecting DECLs and TYPEs for language data removal. */
4753 struct free_lang_data_d
4755 /* Worklist to avoid excessive recursion. */
4756 vec<tree> worklist;
4758 /* Set of traversed objects. Used to avoid duplicate visits. */
4759 struct pointer_set_t *pset;
4761 /* Array of symbols to process with free_lang_data_in_decl. */
4762 vec<tree> decls;
4764 /* Array of types to process with free_lang_data_in_type. */
4765 vec<tree> types;
4769 /* Save all language fields needed to generate proper debug information
4770 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
4772 static void
4773 save_debug_info_for_decl (tree t)
4775 /*struct saved_debug_info_d *sdi;*/
4777 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
4779 /* FIXME. Partial implementation for saving debug info removed. */
4783 /* Save all language fields needed to generate proper debug information
4784 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
4786 static void
4787 save_debug_info_for_type (tree t)
4789 /*struct saved_debug_info_d *sdi;*/
4791 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
4793 /* FIXME. Partial implementation for saving debug info removed. */
4797 /* Add type or decl T to one of the list of tree nodes that need their
4798 language data removed. The lists are held inside FLD. */
4800 static void
4801 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
4803 if (DECL_P (t))
4805 fld->decls.safe_push (t);
4806 if (debug_info_level > DINFO_LEVEL_TERSE)
4807 save_debug_info_for_decl (t);
4809 else if (TYPE_P (t))
4811 fld->types.safe_push (t);
4812 if (debug_info_level > DINFO_LEVEL_TERSE)
4813 save_debug_info_for_type (t);
4815 else
4816 gcc_unreachable ();
4819 /* Push tree node T into FLD->WORKLIST. */
4821 static inline void
4822 fld_worklist_push (tree t, struct free_lang_data_d *fld)
4824 if (t && !is_lang_specific (t) && !pointer_set_contains (fld->pset, t))
4825 fld->worklist.safe_push ((t));
4829 /* Operand callback helper for free_lang_data_in_node. *TP is the
4830 subtree operand being considered. */
4832 static tree
4833 find_decls_types_r (tree *tp, int *ws, void *data)
4835 tree t = *tp;
4836 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
4838 if (TREE_CODE (t) == TREE_LIST)
4839 return NULL_TREE;
4841 /* Language specific nodes will be removed, so there is no need
4842 to gather anything under them. */
4843 if (is_lang_specific (t))
4845 *ws = 0;
4846 return NULL_TREE;
4849 if (DECL_P (t))
4851 /* Note that walk_tree does not traverse every possible field in
4852 decls, so we have to do our own traversals here. */
4853 add_tree_to_fld_list (t, fld);
4855 fld_worklist_push (DECL_NAME (t), fld);
4856 fld_worklist_push (DECL_CONTEXT (t), fld);
4857 fld_worklist_push (DECL_SIZE (t), fld);
4858 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
4860 /* We are going to remove everything under DECL_INITIAL for
4861 TYPE_DECLs. No point walking them. */
4862 if (TREE_CODE (t) != TYPE_DECL)
4863 fld_worklist_push (DECL_INITIAL (t), fld);
4865 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
4866 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
4868 if (TREE_CODE (t) == FUNCTION_DECL)
4870 fld_worklist_push (DECL_ARGUMENTS (t), fld);
4871 fld_worklist_push (DECL_RESULT (t), fld);
4873 else if (TREE_CODE (t) == TYPE_DECL)
4875 fld_worklist_push (DECL_ARGUMENT_FLD (t), fld);
4876 fld_worklist_push (DECL_VINDEX (t), fld);
4877 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
4879 else if (TREE_CODE (t) == FIELD_DECL)
4881 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
4882 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
4883 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
4884 fld_worklist_push (DECL_FCONTEXT (t), fld);
4886 else if (TREE_CODE (t) == VAR_DECL)
4888 fld_worklist_push (DECL_SECTION_NAME (t), fld);
4889 fld_worklist_push (DECL_COMDAT_GROUP (t), fld);
4892 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
4893 && DECL_HAS_VALUE_EXPR_P (t))
4894 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
4896 if (TREE_CODE (t) != FIELD_DECL
4897 && TREE_CODE (t) != TYPE_DECL)
4898 fld_worklist_push (TREE_CHAIN (t), fld);
4899 *ws = 0;
4901 else if (TYPE_P (t))
4903 /* Note that walk_tree does not traverse every possible field in
4904 types, so we have to do our own traversals here. */
4905 add_tree_to_fld_list (t, fld);
4907 if (!RECORD_OR_UNION_TYPE_P (t))
4908 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
4909 fld_worklist_push (TYPE_SIZE (t), fld);
4910 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
4911 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
4912 fld_worklist_push (TYPE_POINTER_TO (t), fld);
4913 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
4914 fld_worklist_push (TYPE_NAME (t), fld);
4915 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
4916 them and thus do not and want not to reach unused pointer types
4917 this way. */
4918 if (!POINTER_TYPE_P (t))
4919 fld_worklist_push (TYPE_MINVAL (t), fld);
4920 if (!RECORD_OR_UNION_TYPE_P (t))
4921 fld_worklist_push (TYPE_MAXVAL (t), fld);
4922 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
4923 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
4924 do not and want not to reach unused variants this way. */
4925 if (TYPE_CONTEXT (t))
4927 tree ctx = TYPE_CONTEXT (t);
4928 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
4929 So push that instead. */
4930 while (ctx && TREE_CODE (ctx) == BLOCK)
4931 ctx = BLOCK_SUPERCONTEXT (ctx);
4932 fld_worklist_push (ctx, fld);
4934 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
4935 and want not to reach unused types this way. */
4937 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
4939 unsigned i;
4940 tree tem;
4941 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
4942 fld_worklist_push (TREE_TYPE (tem), fld);
4943 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
4944 if (tem
4945 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
4946 && TREE_CODE (tem) == TREE_LIST)
4949 fld_worklist_push (TREE_VALUE (tem), fld);
4950 tem = TREE_CHAIN (tem);
4952 while (tem);
4954 if (RECORD_OR_UNION_TYPE_P (t))
4956 tree tem;
4957 /* Push all TYPE_FIELDS - there can be interleaving interesting
4958 and non-interesting things. */
4959 tem = TYPE_FIELDS (t);
4960 while (tem)
4962 if (TREE_CODE (tem) == FIELD_DECL
4963 || TREE_CODE (tem) == TYPE_DECL)
4964 fld_worklist_push (tem, fld);
4965 tem = TREE_CHAIN (tem);
4969 fld_worklist_push (TYPE_STUB_DECL (t), fld);
4970 *ws = 0;
4972 else if (TREE_CODE (t) == BLOCK)
4974 tree tem;
4975 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
4976 fld_worklist_push (tem, fld);
4977 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
4978 fld_worklist_push (tem, fld);
4979 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
4982 if (TREE_CODE (t) != IDENTIFIER_NODE
4983 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
4984 fld_worklist_push (TREE_TYPE (t), fld);
4986 return NULL_TREE;
4990 /* Find decls and types in T. */
4992 static void
4993 find_decls_types (tree t, struct free_lang_data_d *fld)
4995 while (1)
4997 if (!pointer_set_contains (fld->pset, t))
4998 walk_tree (&t, find_decls_types_r, fld, fld->pset);
4999 if (fld->worklist.is_empty ())
5000 break;
5001 t = fld->worklist.pop ();
5005 /* Translate all the types in LIST with the corresponding runtime
5006 types. */
5008 static tree
5009 get_eh_types_for_runtime (tree list)
5011 tree head, prev;
5013 if (list == NULL_TREE)
5014 return NULL_TREE;
5016 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5017 prev = head;
5018 list = TREE_CHAIN (list);
5019 while (list)
5021 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5022 TREE_CHAIN (prev) = n;
5023 prev = TREE_CHAIN (prev);
5024 list = TREE_CHAIN (list);
5027 return head;
5031 /* Find decls and types referenced in EH region R and store them in
5032 FLD->DECLS and FLD->TYPES. */
5034 static void
5035 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5037 switch (r->type)
5039 case ERT_CLEANUP:
5040 break;
5042 case ERT_TRY:
5044 eh_catch c;
5046 /* The types referenced in each catch must first be changed to the
5047 EH types used at runtime. This removes references to FE types
5048 in the region. */
5049 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5051 c->type_list = get_eh_types_for_runtime (c->type_list);
5052 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5055 break;
5057 case ERT_ALLOWED_EXCEPTIONS:
5058 r->u.allowed.type_list
5059 = get_eh_types_for_runtime (r->u.allowed.type_list);
5060 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5061 break;
5063 case ERT_MUST_NOT_THROW:
5064 walk_tree (&r->u.must_not_throw.failure_decl,
5065 find_decls_types_r, fld, fld->pset);
5066 break;
5071 /* Find decls and types referenced in cgraph node N and store them in
5072 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5073 look for *every* kind of DECL and TYPE node reachable from N,
5074 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5075 NAMESPACE_DECLs, etc). */
5077 static void
5078 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5080 basic_block bb;
5081 struct function *fn;
5082 unsigned ix;
5083 tree t;
5085 find_decls_types (n->symbol.decl, fld);
5087 if (!gimple_has_body_p (n->symbol.decl))
5088 return;
5090 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5092 fn = DECL_STRUCT_FUNCTION (n->symbol.decl);
5094 /* Traverse locals. */
5095 FOR_EACH_LOCAL_DECL (fn, ix, t)
5096 find_decls_types (t, fld);
5098 /* Traverse EH regions in FN. */
5100 eh_region r;
5101 FOR_ALL_EH_REGION_FN (r, fn)
5102 find_decls_types_in_eh_region (r, fld);
5105 /* Traverse every statement in FN. */
5106 FOR_EACH_BB_FN (bb, fn)
5108 gimple_stmt_iterator si;
5109 unsigned i;
5111 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5113 gimple phi = gsi_stmt (si);
5115 for (i = 0; i < gimple_phi_num_args (phi); i++)
5117 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5118 find_decls_types (*arg_p, fld);
5122 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5124 gimple stmt = gsi_stmt (si);
5126 if (is_gimple_call (stmt))
5127 find_decls_types (gimple_call_fntype (stmt), fld);
5129 for (i = 0; i < gimple_num_ops (stmt); i++)
5131 tree arg = gimple_op (stmt, i);
5132 find_decls_types (arg, fld);
5139 /* Find decls and types referenced in varpool node N and store them in
5140 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5141 look for *every* kind of DECL and TYPE node reachable from N,
5142 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5143 NAMESPACE_DECLs, etc). */
5145 static void
5146 find_decls_types_in_var (struct varpool_node *v, struct free_lang_data_d *fld)
5148 find_decls_types (v->symbol.decl, fld);
5151 /* If T needs an assembler name, have one created for it. */
5153 void
5154 assign_assembler_name_if_neeeded (tree t)
5156 if (need_assembler_name_p (t))
5158 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5159 diagnostics that use input_location to show locus
5160 information. The problem here is that, at this point,
5161 input_location is generally anchored to the end of the file
5162 (since the parser is long gone), so we don't have a good
5163 position to pin it to.
5165 To alleviate this problem, this uses the location of T's
5166 declaration. Examples of this are
5167 testsuite/g++.dg/template/cond2.C and
5168 testsuite/g++.dg/template/pr35240.C. */
5169 location_t saved_location = input_location;
5170 input_location = DECL_SOURCE_LOCATION (t);
5172 decl_assembler_name (t);
5174 input_location = saved_location;
5179 /* Free language specific information for every operand and expression
5180 in every node of the call graph. This process operates in three stages:
5182 1- Every callgraph node and varpool node is traversed looking for
5183 decls and types embedded in them. This is a more exhaustive
5184 search than that done by find_referenced_vars, because it will
5185 also collect individual fields, decls embedded in types, etc.
5187 2- All the decls found are sent to free_lang_data_in_decl.
5189 3- All the types found are sent to free_lang_data_in_type.
5191 The ordering between decls and types is important because
5192 free_lang_data_in_decl sets assembler names, which includes
5193 mangling. So types cannot be freed up until assembler names have
5194 been set up. */
5196 static void
5197 free_lang_data_in_cgraph (void)
5199 struct cgraph_node *n;
5200 struct varpool_node *v;
5201 struct free_lang_data_d fld;
5202 tree t;
5203 unsigned i;
5204 alias_pair *p;
5206 /* Initialize sets and arrays to store referenced decls and types. */
5207 fld.pset = pointer_set_create ();
5208 fld.worklist.create (0);
5209 fld.decls.create (100);
5210 fld.types.create (100);
5212 /* Find decls and types in the body of every function in the callgraph. */
5213 FOR_EACH_FUNCTION (n)
5214 find_decls_types_in_node (n, &fld);
5216 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5217 find_decls_types (p->decl, &fld);
5219 /* Find decls and types in every varpool symbol. */
5220 FOR_EACH_VARIABLE (v)
5221 find_decls_types_in_var (v, &fld);
5223 /* Set the assembler name on every decl found. We need to do this
5224 now because free_lang_data_in_decl will invalidate data needed
5225 for mangling. This breaks mangling on interdependent decls. */
5226 FOR_EACH_VEC_ELT (fld.decls, i, t)
5227 assign_assembler_name_if_neeeded (t);
5229 /* Traverse every decl found freeing its language data. */
5230 FOR_EACH_VEC_ELT (fld.decls, i, t)
5231 free_lang_data_in_decl (t);
5233 /* Traverse every type found freeing its language data. */
5234 FOR_EACH_VEC_ELT (fld.types, i, t)
5235 free_lang_data_in_type (t);
5237 pointer_set_destroy (fld.pset);
5238 fld.worklist.release ();
5239 fld.decls.release ();
5240 fld.types.release ();
5244 /* Free resources that are used by FE but are not needed once they are done. */
5246 static unsigned
5247 free_lang_data (void)
5249 unsigned i;
5251 /* If we are the LTO frontend we have freed lang-specific data already. */
5252 if (in_lto_p
5253 || !flag_generate_lto)
5254 return 0;
5256 /* Allocate and assign alias sets to the standard integer types
5257 while the slots are still in the way the frontends generated them. */
5258 for (i = 0; i < itk_none; ++i)
5259 if (integer_types[i])
5260 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5262 /* Traverse the IL resetting language specific information for
5263 operands, expressions, etc. */
5264 free_lang_data_in_cgraph ();
5266 /* Create gimple variants for common types. */
5267 ptrdiff_type_node = integer_type_node;
5268 fileptr_type_node = ptr_type_node;
5270 /* Reset some langhooks. Do not reset types_compatible_p, it may
5271 still be used indirectly via the get_alias_set langhook. */
5272 lang_hooks.dwarf_name = lhd_dwarf_name;
5273 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5274 /* We do not want the default decl_assembler_name implementation,
5275 rather if we have fixed everything we want a wrapper around it
5276 asserting that all non-local symbols already got their assembler
5277 name and only produce assembler names for local symbols. Or rather
5278 make sure we never call decl_assembler_name on local symbols and
5279 devise a separate, middle-end private scheme for it. */
5281 /* Reset diagnostic machinery. */
5282 tree_diagnostics_defaults (global_dc);
5284 return 0;
5288 struct simple_ipa_opt_pass pass_ipa_free_lang_data =
5291 SIMPLE_IPA_PASS,
5292 "*free_lang_data", /* name */
5293 OPTGROUP_NONE, /* optinfo_flags */
5294 NULL, /* gate */
5295 free_lang_data, /* execute */
5296 NULL, /* sub */
5297 NULL, /* next */
5298 0, /* static_pass_number */
5299 TV_IPA_FREE_LANG_DATA, /* tv_id */
5300 0, /* properties_required */
5301 0, /* properties_provided */
5302 0, /* properties_destroyed */
5303 0, /* todo_flags_start */
5304 TODO_ggc_collect /* todo_flags_finish */
5308 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
5309 ATTR_NAME. Also used internally by remove_attribute(). */
5310 bool
5311 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
5313 size_t ident_len = IDENTIFIER_LENGTH (ident);
5315 if (ident_len == attr_len)
5317 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
5318 return true;
5320 else if (ident_len == attr_len + 4)
5322 /* There is the possibility that ATTR is 'text' and IDENT is
5323 '__text__'. */
5324 const char *p = IDENTIFIER_POINTER (ident);
5325 if (p[0] == '_' && p[1] == '_'
5326 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5327 && strncmp (attr_name, p + 2, attr_len) == 0)
5328 return true;
5331 return false;
5334 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
5335 of ATTR_NAME, and LIST is not NULL_TREE. */
5336 tree
5337 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
5339 while (list)
5341 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5343 if (ident_len == attr_len)
5345 if (!strcmp (attr_name,
5346 IDENTIFIER_POINTER (get_attribute_name (list))))
5347 break;
5349 /* TODO: If we made sure that attributes were stored in the
5350 canonical form without '__...__' (ie, as in 'text' as opposed
5351 to '__text__') then we could avoid the following case. */
5352 else if (ident_len == attr_len + 4)
5354 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5355 if (p[0] == '_' && p[1] == '_'
5356 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5357 && strncmp (attr_name, p + 2, attr_len) == 0)
5358 break;
5360 list = TREE_CHAIN (list);
5363 return list;
5366 /* A variant of lookup_attribute() that can be used with an identifier
5367 as the first argument, and where the identifier can be either
5368 'text' or '__text__'.
5370 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
5371 return a pointer to the attribute's list element if the attribute
5372 is part of the list, or NULL_TREE if not found. If the attribute
5373 appears more than once, this only returns the first occurrence; the
5374 TREE_CHAIN of the return value should be passed back in if further
5375 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
5376 can be in the form 'text' or '__text__'. */
5377 static tree
5378 lookup_ident_attribute (tree attr_identifier, tree list)
5380 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
5382 while (list)
5384 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
5385 == IDENTIFIER_NODE);
5387 /* Identifiers can be compared directly for equality. */
5388 if (attr_identifier == get_attribute_name (list))
5389 break;
5391 /* If they are not equal, they may still be one in the form
5392 'text' while the other one is in the form '__text__'. TODO:
5393 If we were storing attributes in normalized 'text' form, then
5394 this could all go away and we could take full advantage of
5395 the fact that we're comparing identifiers. :-) */
5397 size_t attr_len = IDENTIFIER_LENGTH (attr_identifier);
5398 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5400 if (ident_len == attr_len + 4)
5402 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5403 const char *q = IDENTIFIER_POINTER (attr_identifier);
5404 if (p[0] == '_' && p[1] == '_'
5405 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5406 && strncmp (q, p + 2, attr_len) == 0)
5407 break;
5409 else if (ident_len + 4 == attr_len)
5411 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5412 const char *q = IDENTIFIER_POINTER (attr_identifier);
5413 if (q[0] == '_' && q[1] == '_'
5414 && q[attr_len - 2] == '_' && q[attr_len - 1] == '_'
5415 && strncmp (q + 2, p, ident_len) == 0)
5416 break;
5419 list = TREE_CHAIN (list);
5422 return list;
5425 /* Remove any instances of attribute ATTR_NAME in LIST and return the
5426 modified list. */
5428 tree
5429 remove_attribute (const char *attr_name, tree list)
5431 tree *p;
5432 size_t attr_len = strlen (attr_name);
5434 gcc_checking_assert (attr_name[0] != '_');
5436 for (p = &list; *p; )
5438 tree l = *p;
5439 /* TODO: If we were storing attributes in normalized form, here
5440 we could use a simple strcmp(). */
5441 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
5442 *p = TREE_CHAIN (l);
5443 else
5444 p = &TREE_CHAIN (l);
5447 return list;
5450 /* Return an attribute list that is the union of a1 and a2. */
5452 tree
5453 merge_attributes (tree a1, tree a2)
5455 tree attributes;
5457 /* Either one unset? Take the set one. */
5459 if ((attributes = a1) == 0)
5460 attributes = a2;
5462 /* One that completely contains the other? Take it. */
5464 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
5466 if (attribute_list_contained (a2, a1))
5467 attributes = a2;
5468 else
5470 /* Pick the longest list, and hang on the other list. */
5472 if (list_length (a1) < list_length (a2))
5473 attributes = a2, a2 = a1;
5475 for (; a2 != 0; a2 = TREE_CHAIN (a2))
5477 tree a;
5478 for (a = lookup_ident_attribute (get_attribute_name (a2),
5479 attributes);
5480 a != NULL_TREE && !attribute_value_equal (a, a2);
5481 a = lookup_ident_attribute (get_attribute_name (a2),
5482 TREE_CHAIN (a)))
5484 if (a == NULL_TREE)
5486 a1 = copy_node (a2);
5487 TREE_CHAIN (a1) = attributes;
5488 attributes = a1;
5493 return attributes;
5496 /* Given types T1 and T2, merge their attributes and return
5497 the result. */
5499 tree
5500 merge_type_attributes (tree t1, tree t2)
5502 return merge_attributes (TYPE_ATTRIBUTES (t1),
5503 TYPE_ATTRIBUTES (t2));
5506 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
5507 the result. */
5509 tree
5510 merge_decl_attributes (tree olddecl, tree newdecl)
5512 return merge_attributes (DECL_ATTRIBUTES (olddecl),
5513 DECL_ATTRIBUTES (newdecl));
5516 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
5518 /* Specialization of merge_decl_attributes for various Windows targets.
5520 This handles the following situation:
5522 __declspec (dllimport) int foo;
5523 int foo;
5525 The second instance of `foo' nullifies the dllimport. */
5527 tree
5528 merge_dllimport_decl_attributes (tree old, tree new_tree)
5530 tree a;
5531 int delete_dllimport_p = 1;
5533 /* What we need to do here is remove from `old' dllimport if it doesn't
5534 appear in `new'. dllimport behaves like extern: if a declaration is
5535 marked dllimport and a definition appears later, then the object
5536 is not dllimport'd. We also remove a `new' dllimport if the old list
5537 contains dllexport: dllexport always overrides dllimport, regardless
5538 of the order of declaration. */
5539 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
5540 delete_dllimport_p = 0;
5541 else if (DECL_DLLIMPORT_P (new_tree)
5542 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
5544 DECL_DLLIMPORT_P (new_tree) = 0;
5545 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
5546 "dllimport ignored", new_tree);
5548 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
5550 /* Warn about overriding a symbol that has already been used, e.g.:
5551 extern int __attribute__ ((dllimport)) foo;
5552 int* bar () {return &foo;}
5553 int foo;
5555 if (TREE_USED (old))
5557 warning (0, "%q+D redeclared without dllimport attribute "
5558 "after being referenced with dll linkage", new_tree);
5559 /* If we have used a variable's address with dllimport linkage,
5560 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
5561 decl may already have had TREE_CONSTANT computed.
5562 We still remove the attribute so that assembler code refers
5563 to '&foo rather than '_imp__foo'. */
5564 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
5565 DECL_DLLIMPORT_P (new_tree) = 1;
5568 /* Let an inline definition silently override the external reference,
5569 but otherwise warn about attribute inconsistency. */
5570 else if (TREE_CODE (new_tree) == VAR_DECL
5571 || !DECL_DECLARED_INLINE_P (new_tree))
5572 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
5573 "previous dllimport ignored", new_tree);
5575 else
5576 delete_dllimport_p = 0;
5578 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
5580 if (delete_dllimport_p)
5581 a = remove_attribute ("dllimport", a);
5583 return a;
5586 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
5587 struct attribute_spec.handler. */
5589 tree
5590 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
5591 bool *no_add_attrs)
5593 tree node = *pnode;
5594 bool is_dllimport;
5596 /* These attributes may apply to structure and union types being created,
5597 but otherwise should pass to the declaration involved. */
5598 if (!DECL_P (node))
5600 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
5601 | (int) ATTR_FLAG_ARRAY_NEXT))
5603 *no_add_attrs = true;
5604 return tree_cons (name, args, NULL_TREE);
5606 if (TREE_CODE (node) == RECORD_TYPE
5607 || TREE_CODE (node) == UNION_TYPE)
5609 node = TYPE_NAME (node);
5610 if (!node)
5611 return NULL_TREE;
5613 else
5615 warning (OPT_Wattributes, "%qE attribute ignored",
5616 name);
5617 *no_add_attrs = true;
5618 return NULL_TREE;
5622 if (TREE_CODE (node) != FUNCTION_DECL
5623 && TREE_CODE (node) != VAR_DECL
5624 && TREE_CODE (node) != TYPE_DECL)
5626 *no_add_attrs = true;
5627 warning (OPT_Wattributes, "%qE attribute ignored",
5628 name);
5629 return NULL_TREE;
5632 if (TREE_CODE (node) == TYPE_DECL
5633 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
5634 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
5636 *no_add_attrs = true;
5637 warning (OPT_Wattributes, "%qE attribute ignored",
5638 name);
5639 return NULL_TREE;
5642 is_dllimport = is_attribute_p ("dllimport", name);
5644 /* Report error on dllimport ambiguities seen now before they cause
5645 any damage. */
5646 if (is_dllimport)
5648 /* Honor any target-specific overrides. */
5649 if (!targetm.valid_dllimport_attribute_p (node))
5650 *no_add_attrs = true;
5652 else if (TREE_CODE (node) == FUNCTION_DECL
5653 && DECL_DECLARED_INLINE_P (node))
5655 warning (OPT_Wattributes, "inline function %q+D declared as "
5656 " dllimport: attribute ignored", node);
5657 *no_add_attrs = true;
5659 /* Like MS, treat definition of dllimported variables and
5660 non-inlined functions on declaration as syntax errors. */
5661 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
5663 error ("function %q+D definition is marked dllimport", node);
5664 *no_add_attrs = true;
5667 else if (TREE_CODE (node) == VAR_DECL)
5669 if (DECL_INITIAL (node))
5671 error ("variable %q+D definition is marked dllimport",
5672 node);
5673 *no_add_attrs = true;
5676 /* `extern' needn't be specified with dllimport.
5677 Specify `extern' now and hope for the best. Sigh. */
5678 DECL_EXTERNAL (node) = 1;
5679 /* Also, implicitly give dllimport'd variables declared within
5680 a function global scope, unless declared static. */
5681 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
5682 TREE_PUBLIC (node) = 1;
5685 if (*no_add_attrs == false)
5686 DECL_DLLIMPORT_P (node) = 1;
5688 else if (TREE_CODE (node) == FUNCTION_DECL
5689 && DECL_DECLARED_INLINE_P (node)
5690 && flag_keep_inline_dllexport)
5691 /* An exported function, even if inline, must be emitted. */
5692 DECL_EXTERNAL (node) = 0;
5694 /* Report error if symbol is not accessible at global scope. */
5695 if (!TREE_PUBLIC (node)
5696 && (TREE_CODE (node) == VAR_DECL
5697 || TREE_CODE (node) == FUNCTION_DECL))
5699 error ("external linkage required for symbol %q+D because of "
5700 "%qE attribute", node, name);
5701 *no_add_attrs = true;
5704 /* A dllexport'd entity must have default visibility so that other
5705 program units (shared libraries or the main executable) can see
5706 it. A dllimport'd entity must have default visibility so that
5707 the linker knows that undefined references within this program
5708 unit can be resolved by the dynamic linker. */
5709 if (!*no_add_attrs)
5711 if (DECL_VISIBILITY_SPECIFIED (node)
5712 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
5713 error ("%qE implies default visibility, but %qD has already "
5714 "been declared with a different visibility",
5715 name, node);
5716 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
5717 DECL_VISIBILITY_SPECIFIED (node) = 1;
5720 return NULL_TREE;
5723 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
5725 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
5726 of the various TYPE_QUAL values. */
5728 static void
5729 set_type_quals (tree type, int type_quals)
5731 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
5732 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
5733 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
5734 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
5737 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
5739 bool
5740 check_qualified_type (const_tree cand, const_tree base, int type_quals)
5742 return (TYPE_QUALS (cand) == type_quals
5743 && TYPE_NAME (cand) == TYPE_NAME (base)
5744 /* Apparently this is needed for Objective-C. */
5745 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5746 /* Check alignment. */
5747 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
5748 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5749 TYPE_ATTRIBUTES (base)));
5752 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
5754 static bool
5755 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
5757 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
5758 && TYPE_NAME (cand) == TYPE_NAME (base)
5759 /* Apparently this is needed for Objective-C. */
5760 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5761 /* Check alignment. */
5762 && TYPE_ALIGN (cand) == align
5763 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5764 TYPE_ATTRIBUTES (base)));
5767 /* Return a version of the TYPE, qualified as indicated by the
5768 TYPE_QUALS, if one exists. If no qualified version exists yet,
5769 return NULL_TREE. */
5771 tree
5772 get_qualified_type (tree type, int type_quals)
5774 tree t;
5776 if (TYPE_QUALS (type) == type_quals)
5777 return type;
5779 /* Search the chain of variants to see if there is already one there just
5780 like the one we need to have. If so, use that existing one. We must
5781 preserve the TYPE_NAME, since there is code that depends on this. */
5782 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5783 if (check_qualified_type (t, type, type_quals))
5784 return t;
5786 return NULL_TREE;
5789 /* Like get_qualified_type, but creates the type if it does not
5790 exist. This function never returns NULL_TREE. */
5792 tree
5793 build_qualified_type (tree type, int type_quals)
5795 tree t;
5797 /* See if we already have the appropriate qualified variant. */
5798 t = get_qualified_type (type, type_quals);
5800 /* If not, build it. */
5801 if (!t)
5803 t = build_variant_type_copy (type);
5804 set_type_quals (t, type_quals);
5806 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5807 /* Propagate structural equality. */
5808 SET_TYPE_STRUCTURAL_EQUALITY (t);
5809 else if (TYPE_CANONICAL (type) != type)
5810 /* Build the underlying canonical type, since it is different
5811 from TYPE. */
5812 TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
5813 type_quals);
5814 else
5815 /* T is its own canonical type. */
5816 TYPE_CANONICAL (t) = t;
5820 return t;
5823 /* Create a variant of type T with alignment ALIGN. */
5825 tree
5826 build_aligned_type (tree type, unsigned int align)
5828 tree t;
5830 if (TYPE_PACKED (type)
5831 || TYPE_ALIGN (type) == align)
5832 return type;
5834 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5835 if (check_aligned_type (t, type, align))
5836 return t;
5838 t = build_variant_type_copy (type);
5839 TYPE_ALIGN (t) = align;
5841 return t;
5844 /* Create a new distinct copy of TYPE. The new type is made its own
5845 MAIN_VARIANT. If TYPE requires structural equality checks, the
5846 resulting type requires structural equality checks; otherwise, its
5847 TYPE_CANONICAL points to itself. */
5849 tree
5850 build_distinct_type_copy (tree type)
5852 tree t = copy_node (type);
5854 TYPE_POINTER_TO (t) = 0;
5855 TYPE_REFERENCE_TO (t) = 0;
5857 /* Set the canonical type either to a new equivalence class, or
5858 propagate the need for structural equality checks. */
5859 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5860 SET_TYPE_STRUCTURAL_EQUALITY (t);
5861 else
5862 TYPE_CANONICAL (t) = t;
5864 /* Make it its own variant. */
5865 TYPE_MAIN_VARIANT (t) = t;
5866 TYPE_NEXT_VARIANT (t) = 0;
5868 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
5869 whose TREE_TYPE is not t. This can also happen in the Ada
5870 frontend when using subtypes. */
5872 return t;
5875 /* Create a new variant of TYPE, equivalent but distinct. This is so
5876 the caller can modify it. TYPE_CANONICAL for the return type will
5877 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
5878 are considered equal by the language itself (or that both types
5879 require structural equality checks). */
5881 tree
5882 build_variant_type_copy (tree type)
5884 tree t, m = TYPE_MAIN_VARIANT (type);
5886 t = build_distinct_type_copy (type);
5888 /* Since we're building a variant, assume that it is a non-semantic
5889 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
5890 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
5892 /* Add the new type to the chain of variants of TYPE. */
5893 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
5894 TYPE_NEXT_VARIANT (m) = t;
5895 TYPE_MAIN_VARIANT (t) = m;
5897 return t;
5900 /* Return true if the from tree in both tree maps are equal. */
5903 tree_map_base_eq (const void *va, const void *vb)
5905 const struct tree_map_base *const a = (const struct tree_map_base *) va,
5906 *const b = (const struct tree_map_base *) vb;
5907 return (a->from == b->from);
5910 /* Hash a from tree in a tree_base_map. */
5912 unsigned int
5913 tree_map_base_hash (const void *item)
5915 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
5918 /* Return true if this tree map structure is marked for garbage collection
5919 purposes. We simply return true if the from tree is marked, so that this
5920 structure goes away when the from tree goes away. */
5923 tree_map_base_marked_p (const void *p)
5925 return ggc_marked_p (((const struct tree_map_base *) p)->from);
5928 /* Hash a from tree in a tree_map. */
5930 unsigned int
5931 tree_map_hash (const void *item)
5933 return (((const struct tree_map *) item)->hash);
5936 /* Hash a from tree in a tree_decl_map. */
5938 unsigned int
5939 tree_decl_map_hash (const void *item)
5941 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
5944 /* Return the initialization priority for DECL. */
5946 priority_type
5947 decl_init_priority_lookup (tree decl)
5949 struct tree_priority_map *h;
5950 struct tree_map_base in;
5952 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
5953 in.from = decl;
5954 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
5955 return h ? h->init : DEFAULT_INIT_PRIORITY;
5958 /* Return the finalization priority for DECL. */
5960 priority_type
5961 decl_fini_priority_lookup (tree decl)
5963 struct tree_priority_map *h;
5964 struct tree_map_base in;
5966 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5967 in.from = decl;
5968 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
5969 return h ? h->fini : DEFAULT_INIT_PRIORITY;
5972 /* Return the initialization and finalization priority information for
5973 DECL. If there is no previous priority information, a freshly
5974 allocated structure is returned. */
5976 static struct tree_priority_map *
5977 decl_priority_info (tree decl)
5979 struct tree_priority_map in;
5980 struct tree_priority_map *h;
5981 void **loc;
5983 in.base.from = decl;
5984 loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
5985 h = (struct tree_priority_map *) *loc;
5986 if (!h)
5988 h = ggc_alloc_cleared_tree_priority_map ();
5989 *loc = h;
5990 h->base.from = decl;
5991 h->init = DEFAULT_INIT_PRIORITY;
5992 h->fini = DEFAULT_INIT_PRIORITY;
5995 return h;
5998 /* Set the initialization priority for DECL to PRIORITY. */
6000 void
6001 decl_init_priority_insert (tree decl, priority_type priority)
6003 struct tree_priority_map *h;
6005 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6006 if (priority == DEFAULT_INIT_PRIORITY)
6007 return;
6008 h = decl_priority_info (decl);
6009 h->init = priority;
6012 /* Set the finalization priority for DECL to PRIORITY. */
6014 void
6015 decl_fini_priority_insert (tree decl, priority_type priority)
6017 struct tree_priority_map *h;
6019 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6020 if (priority == DEFAULT_INIT_PRIORITY)
6021 return;
6022 h = decl_priority_info (decl);
6023 h->fini = priority;
6026 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6028 static void
6029 print_debug_expr_statistics (void)
6031 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6032 (long) htab_size (debug_expr_for_decl),
6033 (long) htab_elements (debug_expr_for_decl),
6034 htab_collisions (debug_expr_for_decl));
6037 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6039 static void
6040 print_value_expr_statistics (void)
6042 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6043 (long) htab_size (value_expr_for_decl),
6044 (long) htab_elements (value_expr_for_decl),
6045 htab_collisions (value_expr_for_decl));
6048 /* Lookup a debug expression for FROM, and return it if we find one. */
6050 tree
6051 decl_debug_expr_lookup (tree from)
6053 struct tree_decl_map *h, in;
6054 in.base.from = from;
6056 h = (struct tree_decl_map *)
6057 htab_find_with_hash (debug_expr_for_decl, &in, DECL_UID (from));
6058 if (h)
6059 return h->to;
6060 return NULL_TREE;
6063 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6065 void
6066 decl_debug_expr_insert (tree from, tree to)
6068 struct tree_decl_map *h;
6069 void **loc;
6071 h = ggc_alloc_tree_decl_map ();
6072 h->base.from = from;
6073 h->to = to;
6074 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, DECL_UID (from),
6075 INSERT);
6076 *(struct tree_decl_map **) loc = h;
6079 /* Lookup a value expression for FROM, and return it if we find one. */
6081 tree
6082 decl_value_expr_lookup (tree from)
6084 struct tree_decl_map *h, in;
6085 in.base.from = from;
6087 h = (struct tree_decl_map *)
6088 htab_find_with_hash (value_expr_for_decl, &in, DECL_UID (from));
6089 if (h)
6090 return h->to;
6091 return NULL_TREE;
6094 /* Insert a mapping FROM->TO in the value expression hashtable. */
6096 void
6097 decl_value_expr_insert (tree from, tree to)
6099 struct tree_decl_map *h;
6100 void **loc;
6102 h = ggc_alloc_tree_decl_map ();
6103 h->base.from = from;
6104 h->to = to;
6105 loc = htab_find_slot_with_hash (value_expr_for_decl, h, DECL_UID (from),
6106 INSERT);
6107 *(struct tree_decl_map **) loc = h;
6110 /* Lookup a vector of debug arguments for FROM, and return it if we
6111 find one. */
6113 vec<tree, va_gc> **
6114 decl_debug_args_lookup (tree from)
6116 struct tree_vec_map *h, in;
6118 if (!DECL_HAS_DEBUG_ARGS_P (from))
6119 return NULL;
6120 gcc_checking_assert (debug_args_for_decl != NULL);
6121 in.base.from = from;
6122 h = (struct tree_vec_map *)
6123 htab_find_with_hash (debug_args_for_decl, &in, DECL_UID (from));
6124 if (h)
6125 return &h->to;
6126 return NULL;
6129 /* Insert a mapping FROM->empty vector of debug arguments in the value
6130 expression hashtable. */
6132 vec<tree, va_gc> **
6133 decl_debug_args_insert (tree from)
6135 struct tree_vec_map *h;
6136 void **loc;
6138 if (DECL_HAS_DEBUG_ARGS_P (from))
6139 return decl_debug_args_lookup (from);
6140 if (debug_args_for_decl == NULL)
6141 debug_args_for_decl = htab_create_ggc (64, tree_vec_map_hash,
6142 tree_vec_map_eq, 0);
6143 h = ggc_alloc_tree_vec_map ();
6144 h->base.from = from;
6145 h->to = NULL;
6146 loc = htab_find_slot_with_hash (debug_args_for_decl, h, DECL_UID (from),
6147 INSERT);
6148 *(struct tree_vec_map **) loc = h;
6149 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6150 return &h->to;
6153 /* Hashing of types so that we don't make duplicates.
6154 The entry point is `type_hash_canon'. */
6156 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6157 with types in the TREE_VALUE slots), by adding the hash codes
6158 of the individual types. */
6160 static unsigned int
6161 type_hash_list (const_tree list, hashval_t hashcode)
6163 const_tree tail;
6165 for (tail = list; tail; tail = TREE_CHAIN (tail))
6166 if (TREE_VALUE (tail) != error_mark_node)
6167 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
6168 hashcode);
6170 return hashcode;
6173 /* These are the Hashtable callback functions. */
6175 /* Returns true iff the types are equivalent. */
6177 static int
6178 type_hash_eq (const void *va, const void *vb)
6180 const struct type_hash *const a = (const struct type_hash *) va,
6181 *const b = (const struct type_hash *) vb;
6183 /* First test the things that are the same for all types. */
6184 if (a->hash != b->hash
6185 || TREE_CODE (a->type) != TREE_CODE (b->type)
6186 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6187 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6188 TYPE_ATTRIBUTES (b->type))
6189 || (TREE_CODE (a->type) != COMPLEX_TYPE
6190 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6191 return 0;
6193 /* Be careful about comparing arrays before and after the element type
6194 has been completed; don't compare TYPE_ALIGN unless both types are
6195 complete. */
6196 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6197 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6198 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6199 return 0;
6201 switch (TREE_CODE (a->type))
6203 case VOID_TYPE:
6204 case COMPLEX_TYPE:
6205 case POINTER_TYPE:
6206 case REFERENCE_TYPE:
6207 case NULLPTR_TYPE:
6208 return 1;
6210 case VECTOR_TYPE:
6211 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6213 case ENUMERAL_TYPE:
6214 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6215 && !(TYPE_VALUES (a->type)
6216 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6217 && TYPE_VALUES (b->type)
6218 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6219 && type_list_equal (TYPE_VALUES (a->type),
6220 TYPE_VALUES (b->type))))
6221 return 0;
6223 /* ... fall through ... */
6225 case INTEGER_TYPE:
6226 case REAL_TYPE:
6227 case BOOLEAN_TYPE:
6228 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6229 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6230 TYPE_MAX_VALUE (b->type)))
6231 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6232 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6233 TYPE_MIN_VALUE (b->type))));
6235 case FIXED_POINT_TYPE:
6236 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6238 case OFFSET_TYPE:
6239 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6241 case METHOD_TYPE:
6242 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6243 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6244 || (TYPE_ARG_TYPES (a->type)
6245 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6246 && TYPE_ARG_TYPES (b->type)
6247 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6248 && type_list_equal (TYPE_ARG_TYPES (a->type),
6249 TYPE_ARG_TYPES (b->type)))))
6250 break;
6251 return 0;
6252 case ARRAY_TYPE:
6253 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6255 case RECORD_TYPE:
6256 case UNION_TYPE:
6257 case QUAL_UNION_TYPE:
6258 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6259 || (TYPE_FIELDS (a->type)
6260 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6261 && TYPE_FIELDS (b->type)
6262 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6263 && type_list_equal (TYPE_FIELDS (a->type),
6264 TYPE_FIELDS (b->type))));
6266 case FUNCTION_TYPE:
6267 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6268 || (TYPE_ARG_TYPES (a->type)
6269 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6270 && TYPE_ARG_TYPES (b->type)
6271 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6272 && type_list_equal (TYPE_ARG_TYPES (a->type),
6273 TYPE_ARG_TYPES (b->type))))
6274 break;
6275 return 0;
6277 default:
6278 return 0;
6281 if (lang_hooks.types.type_hash_eq != NULL)
6282 return lang_hooks.types.type_hash_eq (a->type, b->type);
6284 return 1;
6287 /* Return the cached hash value. */
6289 static hashval_t
6290 type_hash_hash (const void *item)
6292 return ((const struct type_hash *) item)->hash;
6295 /* Look in the type hash table for a type isomorphic to TYPE.
6296 If one is found, return it. Otherwise return 0. */
6298 tree
6299 type_hash_lookup (hashval_t hashcode, tree type)
6301 struct type_hash *h, in;
6303 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6304 must call that routine before comparing TYPE_ALIGNs. */
6305 layout_type (type);
6307 in.hash = hashcode;
6308 in.type = type;
6310 h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
6311 hashcode);
6312 if (h)
6313 return h->type;
6314 return NULL_TREE;
6317 /* Add an entry to the type-hash-table
6318 for a type TYPE whose hash code is HASHCODE. */
6320 void
6321 type_hash_add (hashval_t hashcode, tree type)
6323 struct type_hash *h;
6324 void **loc;
6326 h = ggc_alloc_type_hash ();
6327 h->hash = hashcode;
6328 h->type = type;
6329 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
6330 *loc = (void *)h;
6333 /* Given TYPE, and HASHCODE its hash code, return the canonical
6334 object for an identical type if one already exists.
6335 Otherwise, return TYPE, and record it as the canonical object.
6337 To use this function, first create a type of the sort you want.
6338 Then compute its hash code from the fields of the type that
6339 make it different from other similar types.
6340 Then call this function and use the value. */
6342 tree
6343 type_hash_canon (unsigned int hashcode, tree type)
6345 tree t1;
6347 /* The hash table only contains main variants, so ensure that's what we're
6348 being passed. */
6349 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6351 /* See if the type is in the hash table already. If so, return it.
6352 Otherwise, add the type. */
6353 t1 = type_hash_lookup (hashcode, type);
6354 if (t1 != 0)
6356 if (GATHER_STATISTICS)
6358 tree_code_counts[(int) TREE_CODE (type)]--;
6359 tree_node_counts[(int) t_kind]--;
6360 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
6362 return t1;
6364 else
6366 type_hash_add (hashcode, type);
6367 return type;
6371 /* See if the data pointed to by the type hash table is marked. We consider
6372 it marked if the type is marked or if a debug type number or symbol
6373 table entry has been made for the type. */
6375 static int
6376 type_hash_marked_p (const void *p)
6378 const_tree const type = ((const struct type_hash *) p)->type;
6380 return ggc_marked_p (type);
6383 static void
6384 print_type_hash_statistics (void)
6386 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6387 (long) htab_size (type_hash_table),
6388 (long) htab_elements (type_hash_table),
6389 htab_collisions (type_hash_table));
6392 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
6393 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
6394 by adding the hash codes of the individual attributes. */
6396 static unsigned int
6397 attribute_hash_list (const_tree list, hashval_t hashcode)
6399 const_tree tail;
6401 for (tail = list; tail; tail = TREE_CHAIN (tail))
6402 /* ??? Do we want to add in TREE_VALUE too? */
6403 hashcode = iterative_hash_object
6404 (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)), hashcode);
6405 return hashcode;
6408 /* Given two lists of attributes, return true if list l2 is
6409 equivalent to l1. */
6412 attribute_list_equal (const_tree l1, const_tree l2)
6414 if (l1 == l2)
6415 return 1;
6417 return attribute_list_contained (l1, l2)
6418 && attribute_list_contained (l2, l1);
6421 /* Given two lists of attributes, return true if list L2 is
6422 completely contained within L1. */
6423 /* ??? This would be faster if attribute names were stored in a canonicalized
6424 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
6425 must be used to show these elements are equivalent (which they are). */
6426 /* ??? It's not clear that attributes with arguments will always be handled
6427 correctly. */
6430 attribute_list_contained (const_tree l1, const_tree l2)
6432 const_tree t1, t2;
6434 /* First check the obvious, maybe the lists are identical. */
6435 if (l1 == l2)
6436 return 1;
6438 /* Maybe the lists are similar. */
6439 for (t1 = l1, t2 = l2;
6440 t1 != 0 && t2 != 0
6441 && get_attribute_name (t1) == get_attribute_name (t2)
6442 && TREE_VALUE (t1) == TREE_VALUE (t2);
6443 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6446 /* Maybe the lists are equal. */
6447 if (t1 == 0 && t2 == 0)
6448 return 1;
6450 for (; t2 != 0; t2 = TREE_CHAIN (t2))
6452 const_tree attr;
6453 /* This CONST_CAST is okay because lookup_attribute does not
6454 modify its argument and the return value is assigned to a
6455 const_tree. */
6456 for (attr = lookup_ident_attribute (get_attribute_name (t2), CONST_CAST_TREE(l1));
6457 attr != NULL_TREE && !attribute_value_equal (t2, attr);
6458 attr = lookup_ident_attribute (get_attribute_name (t2), TREE_CHAIN (attr)))
6461 if (attr == NULL_TREE)
6462 return 0;
6465 return 1;
6468 /* Given two lists of types
6469 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6470 return 1 if the lists contain the same types in the same order.
6471 Also, the TREE_PURPOSEs must match. */
6474 type_list_equal (const_tree l1, const_tree l2)
6476 const_tree t1, t2;
6478 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6479 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6480 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6481 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6482 && (TREE_TYPE (TREE_PURPOSE (t1))
6483 == TREE_TYPE (TREE_PURPOSE (t2))))))
6484 return 0;
6486 return t1 == t2;
6489 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6490 given by TYPE. If the argument list accepts variable arguments,
6491 then this function counts only the ordinary arguments. */
6494 type_num_arguments (const_tree type)
6496 int i = 0;
6497 tree t;
6499 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6500 /* If the function does not take a variable number of arguments,
6501 the last element in the list will have type `void'. */
6502 if (VOID_TYPE_P (TREE_VALUE (t)))
6503 break;
6504 else
6505 ++i;
6507 return i;
6510 /* Nonzero if integer constants T1 and T2
6511 represent the same constant value. */
6514 tree_int_cst_equal (const_tree t1, const_tree t2)
6516 if (t1 == t2)
6517 return 1;
6519 if (t1 == 0 || t2 == 0)
6520 return 0;
6522 if (TREE_CODE (t1) == INTEGER_CST
6523 && TREE_CODE (t2) == INTEGER_CST
6524 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6525 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
6526 return 1;
6528 return 0;
6531 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
6532 The precise way of comparison depends on their data type. */
6535 tree_int_cst_lt (const_tree t1, const_tree t2)
6537 if (t1 == t2)
6538 return 0;
6540 if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
6542 int t1_sgn = tree_int_cst_sgn (t1);
6543 int t2_sgn = tree_int_cst_sgn (t2);
6545 if (t1_sgn < t2_sgn)
6546 return 1;
6547 else if (t1_sgn > t2_sgn)
6548 return 0;
6549 /* Otherwise, both are non-negative, so we compare them as
6550 unsigned just in case one of them would overflow a signed
6551 type. */
6553 else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
6554 return INT_CST_LT (t1, t2);
6556 return INT_CST_LT_UNSIGNED (t1, t2);
6559 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
6562 tree_int_cst_compare (const_tree t1, const_tree t2)
6564 if (tree_int_cst_lt (t1, t2))
6565 return -1;
6566 else if (tree_int_cst_lt (t2, t1))
6567 return 1;
6568 else
6569 return 0;
6572 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
6573 the host. If POS is zero, the value can be represented in a single
6574 HOST_WIDE_INT. If POS is nonzero, the value must be non-negative and can
6575 be represented in a single unsigned HOST_WIDE_INT. */
6578 host_integerp (const_tree t, int pos)
6580 if (t == NULL_TREE)
6581 return 0;
6583 return (TREE_CODE (t) == INTEGER_CST
6584 && ((TREE_INT_CST_HIGH (t) == 0
6585 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
6586 || (! pos && TREE_INT_CST_HIGH (t) == -1
6587 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
6588 && !TYPE_UNSIGNED (TREE_TYPE (t)))
6589 || (pos && TREE_INT_CST_HIGH (t) == 0)));
6592 /* Return the HOST_WIDE_INT least significant bits of T if it is an
6593 INTEGER_CST and there is no overflow. POS is nonzero if the result must
6594 be non-negative. We must be able to satisfy the above conditions. */
6596 HOST_WIDE_INT
6597 tree_low_cst (const_tree t, int pos)
6599 gcc_assert (host_integerp (t, pos));
6600 return TREE_INT_CST_LOW (t);
6603 /* Return the HOST_WIDE_INT least significant bits of T, a sizetype
6604 kind INTEGER_CST. This makes sure to properly sign-extend the
6605 constant. */
6607 HOST_WIDE_INT
6608 size_low_cst (const_tree t)
6610 double_int d = tree_to_double_int (t);
6611 return d.sext (TYPE_PRECISION (TREE_TYPE (t))).low;
6614 /* Return the most significant (sign) bit of T. */
6617 tree_int_cst_sign_bit (const_tree t)
6619 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
6620 unsigned HOST_WIDE_INT w;
6622 if (bitno < HOST_BITS_PER_WIDE_INT)
6623 w = TREE_INT_CST_LOW (t);
6624 else
6626 w = TREE_INT_CST_HIGH (t);
6627 bitno -= HOST_BITS_PER_WIDE_INT;
6630 return (w >> bitno) & 1;
6633 /* Return an indication of the sign of the integer constant T.
6634 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
6635 Note that -1 will never be returned if T's type is unsigned. */
6638 tree_int_cst_sgn (const_tree t)
6640 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
6641 return 0;
6642 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
6643 return 1;
6644 else if (TREE_INT_CST_HIGH (t) < 0)
6645 return -1;
6646 else
6647 return 1;
6650 /* Return the minimum number of bits needed to represent VALUE in a
6651 signed or unsigned type, UNSIGNEDP says which. */
6653 unsigned int
6654 tree_int_cst_min_precision (tree value, bool unsignedp)
6656 int log;
6658 /* If the value is negative, compute its negative minus 1. The latter
6659 adjustment is because the absolute value of the largest negative value
6660 is one larger than the largest positive value. This is equivalent to
6661 a bit-wise negation, so use that operation instead. */
6663 if (tree_int_cst_sgn (value) < 0)
6664 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
6666 /* Return the number of bits needed, taking into account the fact
6667 that we need one more bit for a signed than unsigned type. */
6669 if (integer_zerop (value))
6670 log = 0;
6671 else
6672 log = tree_floor_log2 (value);
6674 return log + 1 + !unsignedp;
6677 /* Compare two constructor-element-type constants. Return 1 if the lists
6678 are known to be equal; otherwise return 0. */
6681 simple_cst_list_equal (const_tree l1, const_tree l2)
6683 while (l1 != NULL_TREE && l2 != NULL_TREE)
6685 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
6686 return 0;
6688 l1 = TREE_CHAIN (l1);
6689 l2 = TREE_CHAIN (l2);
6692 return l1 == l2;
6695 /* Return truthvalue of whether T1 is the same tree structure as T2.
6696 Return 1 if they are the same.
6697 Return 0 if they are understandably different.
6698 Return -1 if either contains tree structure not understood by
6699 this function. */
6702 simple_cst_equal (const_tree t1, const_tree t2)
6704 enum tree_code code1, code2;
6705 int cmp;
6706 int i;
6708 if (t1 == t2)
6709 return 1;
6710 if (t1 == 0 || t2 == 0)
6711 return 0;
6713 code1 = TREE_CODE (t1);
6714 code2 = TREE_CODE (t2);
6716 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
6718 if (CONVERT_EXPR_CODE_P (code2)
6719 || code2 == NON_LVALUE_EXPR)
6720 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6721 else
6722 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
6725 else if (CONVERT_EXPR_CODE_P (code2)
6726 || code2 == NON_LVALUE_EXPR)
6727 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
6729 if (code1 != code2)
6730 return 0;
6732 switch (code1)
6734 case INTEGER_CST:
6735 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6736 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
6738 case REAL_CST:
6739 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
6741 case FIXED_CST:
6742 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
6744 case STRING_CST:
6745 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
6746 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
6747 TREE_STRING_LENGTH (t1)));
6749 case CONSTRUCTOR:
6751 unsigned HOST_WIDE_INT idx;
6752 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
6753 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
6755 if (vec_safe_length (v1) != vec_safe_length (v2))
6756 return false;
6758 for (idx = 0; idx < vec_safe_length (v1); ++idx)
6759 /* ??? Should we handle also fields here? */
6760 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
6761 return false;
6762 return true;
6765 case SAVE_EXPR:
6766 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6768 case CALL_EXPR:
6769 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
6770 if (cmp <= 0)
6771 return cmp;
6772 if (call_expr_nargs (t1) != call_expr_nargs (t2))
6773 return 0;
6775 const_tree arg1, arg2;
6776 const_call_expr_arg_iterator iter1, iter2;
6777 for (arg1 = first_const_call_expr_arg (t1, &iter1),
6778 arg2 = first_const_call_expr_arg (t2, &iter2);
6779 arg1 && arg2;
6780 arg1 = next_const_call_expr_arg (&iter1),
6781 arg2 = next_const_call_expr_arg (&iter2))
6783 cmp = simple_cst_equal (arg1, arg2);
6784 if (cmp <= 0)
6785 return cmp;
6787 return arg1 == arg2;
6790 case TARGET_EXPR:
6791 /* Special case: if either target is an unallocated VAR_DECL,
6792 it means that it's going to be unified with whatever the
6793 TARGET_EXPR is really supposed to initialize, so treat it
6794 as being equivalent to anything. */
6795 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
6796 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
6797 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
6798 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
6799 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
6800 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
6801 cmp = 1;
6802 else
6803 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6805 if (cmp <= 0)
6806 return cmp;
6808 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
6810 case WITH_CLEANUP_EXPR:
6811 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6812 if (cmp <= 0)
6813 return cmp;
6815 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
6817 case COMPONENT_REF:
6818 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
6819 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6821 return 0;
6823 case VAR_DECL:
6824 case PARM_DECL:
6825 case CONST_DECL:
6826 case FUNCTION_DECL:
6827 return 0;
6829 default:
6830 break;
6833 /* This general rule works for most tree codes. All exceptions should be
6834 handled above. If this is a language-specific tree code, we can't
6835 trust what might be in the operand, so say we don't know
6836 the situation. */
6837 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
6838 return -1;
6840 switch (TREE_CODE_CLASS (code1))
6842 case tcc_unary:
6843 case tcc_binary:
6844 case tcc_comparison:
6845 case tcc_expression:
6846 case tcc_reference:
6847 case tcc_statement:
6848 cmp = 1;
6849 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
6851 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
6852 if (cmp <= 0)
6853 return cmp;
6856 return cmp;
6858 default:
6859 return -1;
6863 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
6864 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
6865 than U, respectively. */
6868 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
6870 if (tree_int_cst_sgn (t) < 0)
6871 return -1;
6872 else if (TREE_INT_CST_HIGH (t) != 0)
6873 return 1;
6874 else if (TREE_INT_CST_LOW (t) == u)
6875 return 0;
6876 else if (TREE_INT_CST_LOW (t) < u)
6877 return -1;
6878 else
6879 return 1;
6882 /* Return true if SIZE represents a constant size that is in bounds of
6883 what the middle-end and the backend accepts (covering not more than
6884 half of the address-space). */
6886 bool
6887 valid_constant_size_p (const_tree size)
6889 if (! host_integerp (size, 1)
6890 || TREE_OVERFLOW (size)
6891 || tree_int_cst_sign_bit (size) != 0)
6892 return false;
6893 return true;
6896 /* Return true if CODE represents an associative tree code. Otherwise
6897 return false. */
6898 bool
6899 associative_tree_code (enum tree_code code)
6901 switch (code)
6903 case BIT_IOR_EXPR:
6904 case BIT_AND_EXPR:
6905 case BIT_XOR_EXPR:
6906 case PLUS_EXPR:
6907 case MULT_EXPR:
6908 case MIN_EXPR:
6909 case MAX_EXPR:
6910 return true;
6912 default:
6913 break;
6915 return false;
6918 /* Return true if CODE represents a commutative tree code. Otherwise
6919 return false. */
6920 bool
6921 commutative_tree_code (enum tree_code code)
6923 switch (code)
6925 case PLUS_EXPR:
6926 case MULT_EXPR:
6927 case MULT_HIGHPART_EXPR:
6928 case MIN_EXPR:
6929 case MAX_EXPR:
6930 case BIT_IOR_EXPR:
6931 case BIT_XOR_EXPR:
6932 case BIT_AND_EXPR:
6933 case NE_EXPR:
6934 case EQ_EXPR:
6935 case UNORDERED_EXPR:
6936 case ORDERED_EXPR:
6937 case UNEQ_EXPR:
6938 case LTGT_EXPR:
6939 case TRUTH_AND_EXPR:
6940 case TRUTH_XOR_EXPR:
6941 case TRUTH_OR_EXPR:
6942 case WIDEN_MULT_EXPR:
6943 case VEC_WIDEN_MULT_HI_EXPR:
6944 case VEC_WIDEN_MULT_LO_EXPR:
6945 case VEC_WIDEN_MULT_EVEN_EXPR:
6946 case VEC_WIDEN_MULT_ODD_EXPR:
6947 return true;
6949 default:
6950 break;
6952 return false;
6955 /* Return true if CODE represents a ternary tree code for which the
6956 first two operands are commutative. Otherwise return false. */
6957 bool
6958 commutative_ternary_tree_code (enum tree_code code)
6960 switch (code)
6962 case WIDEN_MULT_PLUS_EXPR:
6963 case WIDEN_MULT_MINUS_EXPR:
6964 return true;
6966 default:
6967 break;
6969 return false;
6972 /* Generate a hash value for an expression. This can be used iteratively
6973 by passing a previous result as the VAL argument.
6975 This function is intended to produce the same hash for expressions which
6976 would compare equal using operand_equal_p. */
6978 hashval_t
6979 iterative_hash_expr (const_tree t, hashval_t val)
6981 int i;
6982 enum tree_code code;
6983 char tclass;
6985 if (t == NULL_TREE)
6986 return iterative_hash_hashval_t (0, val);
6988 code = TREE_CODE (t);
6990 switch (code)
6992 /* Alas, constants aren't shared, so we can't rely on pointer
6993 identity. */
6994 case INTEGER_CST:
6995 val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
6996 return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
6997 case REAL_CST:
6999 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7001 return iterative_hash_hashval_t (val2, val);
7003 case FIXED_CST:
7005 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7007 return iterative_hash_hashval_t (val2, val);
7009 case STRING_CST:
7010 return iterative_hash (TREE_STRING_POINTER (t),
7011 TREE_STRING_LENGTH (t), val);
7012 case COMPLEX_CST:
7013 val = iterative_hash_expr (TREE_REALPART (t), val);
7014 return iterative_hash_expr (TREE_IMAGPART (t), val);
7015 case VECTOR_CST:
7017 unsigned i;
7018 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7019 val = iterative_hash_expr (VECTOR_CST_ELT (t, i), val);
7020 return val;
7022 case SSA_NAME:
7023 /* We can just compare by pointer. */
7024 return iterative_hash_host_wide_int (SSA_NAME_VERSION (t), val);
7025 case PLACEHOLDER_EXPR:
7026 /* The node itself doesn't matter. */
7027 return val;
7028 case TREE_LIST:
7029 /* A list of expressions, for a CALL_EXPR or as the elements of a
7030 VECTOR_CST. */
7031 for (; t; t = TREE_CHAIN (t))
7032 val = iterative_hash_expr (TREE_VALUE (t), val);
7033 return val;
7034 case CONSTRUCTOR:
7036 unsigned HOST_WIDE_INT idx;
7037 tree field, value;
7038 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7040 val = iterative_hash_expr (field, val);
7041 val = iterative_hash_expr (value, val);
7043 return val;
7045 case MEM_REF:
7047 /* The type of the second operand is relevant, except for
7048 its top-level qualifiers. */
7049 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (t, 1)));
7051 val = iterative_hash_object (TYPE_HASH (type), val);
7053 /* We could use the standard hash computation from this point
7054 on. */
7055 val = iterative_hash_object (code, val);
7056 val = iterative_hash_expr (TREE_OPERAND (t, 1), val);
7057 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7058 return val;
7060 case FUNCTION_DECL:
7061 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7062 Otherwise nodes that compare equal according to operand_equal_p might
7063 get different hash codes. However, don't do this for machine specific
7064 or front end builtins, since the function code is overloaded in those
7065 cases. */
7066 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7067 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7069 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7070 code = TREE_CODE (t);
7072 /* FALL THROUGH */
7073 default:
7074 tclass = TREE_CODE_CLASS (code);
7076 if (tclass == tcc_declaration)
7078 /* DECL's have a unique ID */
7079 val = iterative_hash_host_wide_int (DECL_UID (t), val);
7081 else
7083 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7085 val = iterative_hash_object (code, val);
7087 /* Don't hash the type, that can lead to having nodes which
7088 compare equal according to operand_equal_p, but which
7089 have different hash codes. */
7090 if (CONVERT_EXPR_CODE_P (code)
7091 || code == NON_LVALUE_EXPR)
7093 /* Make sure to include signness in the hash computation. */
7094 val += TYPE_UNSIGNED (TREE_TYPE (t));
7095 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7098 else if (commutative_tree_code (code))
7100 /* It's a commutative expression. We want to hash it the same
7101 however it appears. We do this by first hashing both operands
7102 and then rehashing based on the order of their independent
7103 hashes. */
7104 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
7105 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
7106 hashval_t t;
7108 if (one > two)
7109 t = one, one = two, two = t;
7111 val = iterative_hash_hashval_t (one, val);
7112 val = iterative_hash_hashval_t (two, val);
7114 else
7115 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7116 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
7118 return val;
7122 /* Generate a hash value for a pair of expressions. This can be used
7123 iteratively by passing a previous result as the VAL argument.
7125 The same hash value is always returned for a given pair of expressions,
7126 regardless of the order in which they are presented. This is useful in
7127 hashing the operands of commutative functions. */
7129 hashval_t
7130 iterative_hash_exprs_commutative (const_tree t1,
7131 const_tree t2, hashval_t val)
7133 hashval_t one = iterative_hash_expr (t1, 0);
7134 hashval_t two = iterative_hash_expr (t2, 0);
7135 hashval_t t;
7137 if (one > two)
7138 t = one, one = two, two = t;
7139 val = iterative_hash_hashval_t (one, val);
7140 val = iterative_hash_hashval_t (two, val);
7142 return val;
7145 /* Constructors for pointer, array and function types.
7146 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7147 constructed by language-dependent code, not here.) */
7149 /* Construct, lay out and return the type of pointers to TO_TYPE with
7150 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7151 reference all of memory. If such a type has already been
7152 constructed, reuse it. */
7154 tree
7155 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
7156 bool can_alias_all)
7158 tree t;
7160 if (to_type == error_mark_node)
7161 return error_mark_node;
7163 /* If the pointed-to type has the may_alias attribute set, force
7164 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7165 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7166 can_alias_all = true;
7168 /* In some cases, languages will have things that aren't a POINTER_TYPE
7169 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7170 In that case, return that type without regard to the rest of our
7171 operands.
7173 ??? This is a kludge, but consistent with the way this function has
7174 always operated and there doesn't seem to be a good way to avoid this
7175 at the moment. */
7176 if (TYPE_POINTER_TO (to_type) != 0
7177 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7178 return TYPE_POINTER_TO (to_type);
7180 /* First, if we already have a type for pointers to TO_TYPE and it's
7181 the proper mode, use it. */
7182 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7183 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7184 return t;
7186 t = make_node (POINTER_TYPE);
7188 TREE_TYPE (t) = to_type;
7189 SET_TYPE_MODE (t, mode);
7190 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7191 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7192 TYPE_POINTER_TO (to_type) = t;
7194 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7195 SET_TYPE_STRUCTURAL_EQUALITY (t);
7196 else if (TYPE_CANONICAL (to_type) != to_type)
7197 TYPE_CANONICAL (t)
7198 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7199 mode, can_alias_all);
7201 /* Lay out the type. This function has many callers that are concerned
7202 with expression-construction, and this simplifies them all. */
7203 layout_type (t);
7205 return t;
7208 /* By default build pointers in ptr_mode. */
7210 tree
7211 build_pointer_type (tree to_type)
7213 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7214 : TYPE_ADDR_SPACE (to_type);
7215 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7216 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7219 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7221 tree
7222 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
7223 bool can_alias_all)
7225 tree t;
7227 if (to_type == error_mark_node)
7228 return error_mark_node;
7230 /* If the pointed-to type has the may_alias attribute set, force
7231 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7232 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7233 can_alias_all = true;
7235 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7236 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7237 In that case, return that type without regard to the rest of our
7238 operands.
7240 ??? This is a kludge, but consistent with the way this function has
7241 always operated and there doesn't seem to be a good way to avoid this
7242 at the moment. */
7243 if (TYPE_REFERENCE_TO (to_type) != 0
7244 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7245 return TYPE_REFERENCE_TO (to_type);
7247 /* First, if we already have a type for pointers to TO_TYPE and it's
7248 the proper mode, use it. */
7249 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7250 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7251 return t;
7253 t = make_node (REFERENCE_TYPE);
7255 TREE_TYPE (t) = to_type;
7256 SET_TYPE_MODE (t, mode);
7257 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7258 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7259 TYPE_REFERENCE_TO (to_type) = t;
7261 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7262 SET_TYPE_STRUCTURAL_EQUALITY (t);
7263 else if (TYPE_CANONICAL (to_type) != to_type)
7264 TYPE_CANONICAL (t)
7265 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7266 mode, can_alias_all);
7268 layout_type (t);
7270 return t;
7274 /* Build the node for the type of references-to-TO_TYPE by default
7275 in ptr_mode. */
7277 tree
7278 build_reference_type (tree to_type)
7280 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7281 : TYPE_ADDR_SPACE (to_type);
7282 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7283 return build_reference_type_for_mode (to_type, pointer_mode, false);
7286 /* Build a type that is compatible with t but has no cv quals anywhere
7287 in its type, thus
7289 const char *const *const * -> char ***. */
7291 tree
7292 build_type_no_quals (tree t)
7294 switch (TREE_CODE (t))
7296 case POINTER_TYPE:
7297 return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7298 TYPE_MODE (t),
7299 TYPE_REF_CAN_ALIAS_ALL (t));
7300 case REFERENCE_TYPE:
7301 return
7302 build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7303 TYPE_MODE (t),
7304 TYPE_REF_CAN_ALIAS_ALL (t));
7305 default:
7306 return TYPE_MAIN_VARIANT (t);
7310 #define MAX_INT_CACHED_PREC \
7311 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7312 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7314 /* Builds a signed or unsigned integer type of precision PRECISION.
7315 Used for C bitfields whose precision does not match that of
7316 built-in target types. */
7317 tree
7318 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7319 int unsignedp)
7321 tree itype, ret;
7323 if (unsignedp)
7324 unsignedp = MAX_INT_CACHED_PREC + 1;
7326 if (precision <= MAX_INT_CACHED_PREC)
7328 itype = nonstandard_integer_type_cache[precision + unsignedp];
7329 if (itype)
7330 return itype;
7333 itype = make_node (INTEGER_TYPE);
7334 TYPE_PRECISION (itype) = precision;
7336 if (unsignedp)
7337 fixup_unsigned_type (itype);
7338 else
7339 fixup_signed_type (itype);
7341 ret = itype;
7342 if (host_integerp (TYPE_MAX_VALUE (itype), 1))
7343 ret = type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
7344 if (precision <= MAX_INT_CACHED_PREC)
7345 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7347 return ret;
7350 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7351 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7352 is true, reuse such a type that has already been constructed. */
7354 static tree
7355 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7357 tree itype = make_node (INTEGER_TYPE);
7358 hashval_t hashcode = 0;
7360 TREE_TYPE (itype) = type;
7362 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7363 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7365 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7366 SET_TYPE_MODE (itype, TYPE_MODE (type));
7367 TYPE_SIZE (itype) = TYPE_SIZE (type);
7368 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7369 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7370 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7372 if (!shared)
7373 return itype;
7375 if ((TYPE_MIN_VALUE (itype)
7376 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7377 || (TYPE_MAX_VALUE (itype)
7378 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7380 /* Since we cannot reliably merge this type, we need to compare it using
7381 structural equality checks. */
7382 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7383 return itype;
7386 hashcode = iterative_hash_expr (TYPE_MIN_VALUE (itype), hashcode);
7387 hashcode = iterative_hash_expr (TYPE_MAX_VALUE (itype), hashcode);
7388 hashcode = iterative_hash_hashval_t (TYPE_HASH (type), hashcode);
7389 itype = type_hash_canon (hashcode, itype);
7391 return itype;
7394 /* Wrapper around build_range_type_1 with SHARED set to true. */
7396 tree
7397 build_range_type (tree type, tree lowval, tree highval)
7399 return build_range_type_1 (type, lowval, highval, true);
7402 /* Wrapper around build_range_type_1 with SHARED set to false. */
7404 tree
7405 build_nonshared_range_type (tree type, tree lowval, tree highval)
7407 return build_range_type_1 (type, lowval, highval, false);
7410 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7411 MAXVAL should be the maximum value in the domain
7412 (one less than the length of the array).
7414 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7415 We don't enforce this limit, that is up to caller (e.g. language front end).
7416 The limit exists because the result is a signed type and we don't handle
7417 sizes that use more than one HOST_WIDE_INT. */
7419 tree
7420 build_index_type (tree maxval)
7422 return build_range_type (sizetype, size_zero_node, maxval);
7425 /* Return true if the debug information for TYPE, a subtype, should be emitted
7426 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7427 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7428 debug info and doesn't reflect the source code. */
7430 bool
7431 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7433 tree base_type = TREE_TYPE (type), low, high;
7435 /* Subrange types have a base type which is an integral type. */
7436 if (!INTEGRAL_TYPE_P (base_type))
7437 return false;
7439 /* Get the real bounds of the subtype. */
7440 if (lang_hooks.types.get_subrange_bounds)
7441 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7442 else
7444 low = TYPE_MIN_VALUE (type);
7445 high = TYPE_MAX_VALUE (type);
7448 /* If the type and its base type have the same representation and the same
7449 name, then the type is not a subrange but a copy of the base type. */
7450 if ((TREE_CODE (base_type) == INTEGER_TYPE
7451 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7452 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7453 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7454 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type)))
7456 tree type_name = TYPE_NAME (type);
7457 tree base_type_name = TYPE_NAME (base_type);
7459 if (type_name && TREE_CODE (type_name) == TYPE_DECL)
7460 type_name = DECL_NAME (type_name);
7462 if (base_type_name && TREE_CODE (base_type_name) == TYPE_DECL)
7463 base_type_name = DECL_NAME (base_type_name);
7465 if (type_name == base_type_name)
7466 return false;
7469 if (lowval)
7470 *lowval = low;
7471 if (highval)
7472 *highval = high;
7473 return true;
7476 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7477 and number of elements specified by the range of values of INDEX_TYPE.
7478 If SHARED is true, reuse such a type that has already been constructed. */
7480 static tree
7481 build_array_type_1 (tree elt_type, tree index_type, bool shared)
7483 tree t;
7485 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7487 error ("arrays of functions are not meaningful");
7488 elt_type = integer_type_node;
7491 t = make_node (ARRAY_TYPE);
7492 TREE_TYPE (t) = elt_type;
7493 TYPE_DOMAIN (t) = index_type;
7494 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7495 layout_type (t);
7497 /* If the element type is incomplete at this point we get marked for
7498 structural equality. Do not record these types in the canonical
7499 type hashtable. */
7500 if (TYPE_STRUCTURAL_EQUALITY_P (t))
7501 return t;
7503 if (shared)
7505 hashval_t hashcode = iterative_hash_object (TYPE_HASH (elt_type), 0);
7506 if (index_type)
7507 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
7508 tree old_t = t;
7509 t = type_hash_canon (hashcode, t);
7510 if (t != old_t)
7511 /* Lay it out again in case the element type has been completed since
7512 the array was added to the hash table. */
7513 layout_type (t);
7516 if (TYPE_CANONICAL (t) == t)
7518 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7519 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7520 SET_TYPE_STRUCTURAL_EQUALITY (t);
7521 else if (TYPE_CANONICAL (elt_type) != elt_type
7522 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7523 TYPE_CANONICAL (t)
7524 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7525 index_type
7526 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7527 shared);
7530 return t;
7533 /* Wrapper around build_array_type_1 with SHARED set to true. */
7535 tree
7536 build_array_type (tree elt_type, tree index_type)
7538 return build_array_type_1 (elt_type, index_type, true);
7541 /* Wrapper around build_array_type_1 with SHARED set to false. */
7543 tree
7544 build_nonshared_array_type (tree elt_type, tree index_type)
7546 return build_array_type_1 (elt_type, index_type, false);
7549 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7550 sizetype. */
7552 tree
7553 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
7555 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7558 /* Recursively examines the array elements of TYPE, until a non-array
7559 element type is found. */
7561 tree
7562 strip_array_types (tree type)
7564 while (TREE_CODE (type) == ARRAY_TYPE)
7565 type = TREE_TYPE (type);
7567 return type;
7570 /* Computes the canonical argument types from the argument type list
7571 ARGTYPES.
7573 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7574 on entry to this function, or if any of the ARGTYPES are
7575 structural.
7577 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7578 true on entry to this function, or if any of the ARGTYPES are
7579 non-canonical.
7581 Returns a canonical argument list, which may be ARGTYPES when the
7582 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7583 true) or would not differ from ARGTYPES. */
7585 static tree
7586 maybe_canonicalize_argtypes(tree argtypes,
7587 bool *any_structural_p,
7588 bool *any_noncanonical_p)
7590 tree arg;
7591 bool any_noncanonical_argtypes_p = false;
7593 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7595 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7596 /* Fail gracefully by stating that the type is structural. */
7597 *any_structural_p = true;
7598 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7599 *any_structural_p = true;
7600 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7601 || TREE_PURPOSE (arg))
7602 /* If the argument has a default argument, we consider it
7603 non-canonical even though the type itself is canonical.
7604 That way, different variants of function and method types
7605 with default arguments will all point to the variant with
7606 no defaults as their canonical type. */
7607 any_noncanonical_argtypes_p = true;
7610 if (*any_structural_p)
7611 return argtypes;
7613 if (any_noncanonical_argtypes_p)
7615 /* Build the canonical list of argument types. */
7616 tree canon_argtypes = NULL_TREE;
7617 bool is_void = false;
7619 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7621 if (arg == void_list_node)
7622 is_void = true;
7623 else
7624 canon_argtypes = tree_cons (NULL_TREE,
7625 TYPE_CANONICAL (TREE_VALUE (arg)),
7626 canon_argtypes);
7629 canon_argtypes = nreverse (canon_argtypes);
7630 if (is_void)
7631 canon_argtypes = chainon (canon_argtypes, void_list_node);
7633 /* There is a non-canonical type. */
7634 *any_noncanonical_p = true;
7635 return canon_argtypes;
7638 /* The canonical argument types are the same as ARGTYPES. */
7639 return argtypes;
7642 /* Construct, lay out and return
7643 the type of functions returning type VALUE_TYPE
7644 given arguments of types ARG_TYPES.
7645 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7646 are data type nodes for the arguments of the function.
7647 If such a type has already been constructed, reuse it. */
7649 tree
7650 build_function_type (tree value_type, tree arg_types)
7652 tree t;
7653 hashval_t hashcode = 0;
7654 bool any_structural_p, any_noncanonical_p;
7655 tree canon_argtypes;
7657 if (TREE_CODE (value_type) == FUNCTION_TYPE)
7659 error ("function return type cannot be function");
7660 value_type = integer_type_node;
7663 /* Make a node of the sort we want. */
7664 t = make_node (FUNCTION_TYPE);
7665 TREE_TYPE (t) = value_type;
7666 TYPE_ARG_TYPES (t) = arg_types;
7668 /* If we already have such a type, use the old one. */
7669 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
7670 hashcode = type_hash_list (arg_types, hashcode);
7671 t = type_hash_canon (hashcode, t);
7673 /* Set up the canonical type. */
7674 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7675 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7676 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
7677 &any_structural_p,
7678 &any_noncanonical_p);
7679 if (any_structural_p)
7680 SET_TYPE_STRUCTURAL_EQUALITY (t);
7681 else if (any_noncanonical_p)
7682 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
7683 canon_argtypes);
7685 if (!COMPLETE_TYPE_P (t))
7686 layout_type (t);
7687 return t;
7690 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP and the
7691 return value if SKIP_RETURN is true. */
7693 static tree
7694 build_function_type_skip_args (tree orig_type, bitmap args_to_skip,
7695 bool skip_return)
7697 tree new_type = NULL;
7698 tree args, new_args = NULL, t;
7699 tree new_reversed;
7700 int i = 0;
7702 for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
7703 args = TREE_CHAIN (args), i++)
7704 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
7705 new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
7707 new_reversed = nreverse (new_args);
7708 if (args)
7710 if (new_reversed)
7711 TREE_CHAIN (new_args) = void_list_node;
7712 else
7713 new_reversed = void_list_node;
7716 /* Use copy_node to preserve as much as possible from original type
7717 (debug info, attribute lists etc.)
7718 Exception is METHOD_TYPEs must have THIS argument.
7719 When we are asked to remove it, we need to build new FUNCTION_TYPE
7720 instead. */
7721 if (TREE_CODE (orig_type) != METHOD_TYPE
7722 || !args_to_skip
7723 || !bitmap_bit_p (args_to_skip, 0))
7725 new_type = build_distinct_type_copy (orig_type);
7726 TYPE_ARG_TYPES (new_type) = new_reversed;
7728 else
7730 new_type
7731 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
7732 new_reversed));
7733 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
7736 if (skip_return)
7737 TREE_TYPE (new_type) = void_type_node;
7739 /* This is a new type, not a copy of an old type. Need to reassociate
7740 variants. We can handle everything except the main variant lazily. */
7741 t = TYPE_MAIN_VARIANT (orig_type);
7742 if (t != orig_type)
7744 t = build_function_type_skip_args (t, args_to_skip, skip_return);
7745 TYPE_MAIN_VARIANT (new_type) = t;
7746 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
7747 TYPE_NEXT_VARIANT (t) = new_type;
7749 else
7751 TYPE_MAIN_VARIANT (new_type) = new_type;
7752 TYPE_NEXT_VARIANT (new_type) = NULL;
7755 return new_type;
7758 /* Build variant of function decl ORIG_DECL skipping ARGS_TO_SKIP and the
7759 return value if SKIP_RETURN is true.
7761 Arguments from DECL_ARGUMENTS list can't be removed now, since they are
7762 linked by TREE_CHAIN directly. The caller is responsible for eliminating
7763 them when they are being duplicated (i.e. copy_arguments_for_versioning). */
7765 tree
7766 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip,
7767 bool skip_return)
7769 tree new_decl = copy_node (orig_decl);
7770 tree new_type;
7772 new_type = TREE_TYPE (orig_decl);
7773 if (prototype_p (new_type)
7774 || (skip_return && !VOID_TYPE_P (TREE_TYPE (new_type))))
7775 new_type
7776 = build_function_type_skip_args (new_type, args_to_skip, skip_return);
7777 TREE_TYPE (new_decl) = new_type;
7779 /* For declarations setting DECL_VINDEX (i.e. methods)
7780 we expect first argument to be THIS pointer. */
7781 if (args_to_skip && bitmap_bit_p (args_to_skip, 0))
7782 DECL_VINDEX (new_decl) = NULL_TREE;
7784 /* When signature changes, we need to clear builtin info. */
7785 if (DECL_BUILT_IN (new_decl)
7786 && args_to_skip
7787 && !bitmap_empty_p (args_to_skip))
7789 DECL_BUILT_IN_CLASS (new_decl) = NOT_BUILT_IN;
7790 DECL_FUNCTION_CODE (new_decl) = (enum built_in_function) 0;
7792 return new_decl;
7795 /* Build a function type. The RETURN_TYPE is the type returned by the
7796 function. If VAARGS is set, no void_type_node is appended to the
7797 the list. ARGP must be always be terminated be a NULL_TREE. */
7799 static tree
7800 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
7802 tree t, args, last;
7804 t = va_arg (argp, tree);
7805 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
7806 args = tree_cons (NULL_TREE, t, args);
7808 if (vaargs)
7810 last = args;
7811 if (args != NULL_TREE)
7812 args = nreverse (args);
7813 gcc_assert (last != void_list_node);
7815 else if (args == NULL_TREE)
7816 args = void_list_node;
7817 else
7819 last = args;
7820 args = nreverse (args);
7821 TREE_CHAIN (last) = void_list_node;
7823 args = build_function_type (return_type, args);
7825 return args;
7828 /* Build a function type. The RETURN_TYPE is the type returned by the
7829 function. If additional arguments are provided, they are
7830 additional argument types. The list of argument types must always
7831 be terminated by NULL_TREE. */
7833 tree
7834 build_function_type_list (tree return_type, ...)
7836 tree args;
7837 va_list p;
7839 va_start (p, return_type);
7840 args = build_function_type_list_1 (false, return_type, p);
7841 va_end (p);
7842 return args;
7845 /* Build a variable argument function type. The RETURN_TYPE is the
7846 type returned by the function. If additional arguments are provided,
7847 they are additional argument types. The list of argument types must
7848 always be terminated by NULL_TREE. */
7850 tree
7851 build_varargs_function_type_list (tree return_type, ...)
7853 tree args;
7854 va_list p;
7856 va_start (p, return_type);
7857 args = build_function_type_list_1 (true, return_type, p);
7858 va_end (p);
7860 return args;
7863 /* Build a function type. RETURN_TYPE is the type returned by the
7864 function; VAARGS indicates whether the function takes varargs. The
7865 function takes N named arguments, the types of which are provided in
7866 ARG_TYPES. */
7868 static tree
7869 build_function_type_array_1 (bool vaargs, tree return_type, int n,
7870 tree *arg_types)
7872 int i;
7873 tree t = vaargs ? NULL_TREE : void_list_node;
7875 for (i = n - 1; i >= 0; i--)
7876 t = tree_cons (NULL_TREE, arg_types[i], t);
7878 return build_function_type (return_type, t);
7881 /* Build a function type. RETURN_TYPE is the type returned by the
7882 function. The function takes N named arguments, the types of which
7883 are provided in ARG_TYPES. */
7885 tree
7886 build_function_type_array (tree return_type, int n, tree *arg_types)
7888 return build_function_type_array_1 (false, return_type, n, arg_types);
7891 /* Build a variable argument function type. RETURN_TYPE is the type
7892 returned by the function. The function takes N named arguments, the
7893 types of which are provided in ARG_TYPES. */
7895 tree
7896 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
7898 return build_function_type_array_1 (true, return_type, n, arg_types);
7901 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
7902 and ARGTYPES (a TREE_LIST) are the return type and arguments types
7903 for the method. An implicit additional parameter (of type
7904 pointer-to-BASETYPE) is added to the ARGTYPES. */
7906 tree
7907 build_method_type_directly (tree basetype,
7908 tree rettype,
7909 tree argtypes)
7911 tree t;
7912 tree ptype;
7913 int hashcode = 0;
7914 bool any_structural_p, any_noncanonical_p;
7915 tree canon_argtypes;
7917 /* Make a node of the sort we want. */
7918 t = make_node (METHOD_TYPE);
7920 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7921 TREE_TYPE (t) = rettype;
7922 ptype = build_pointer_type (basetype);
7924 /* The actual arglist for this function includes a "hidden" argument
7925 which is "this". Put it into the list of argument types. */
7926 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
7927 TYPE_ARG_TYPES (t) = argtypes;
7929 /* If we already have such a type, use the old one. */
7930 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
7931 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
7932 hashcode = type_hash_list (argtypes, hashcode);
7933 t = type_hash_canon (hashcode, t);
7935 /* Set up the canonical type. */
7936 any_structural_p
7937 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7938 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
7939 any_noncanonical_p
7940 = (TYPE_CANONICAL (basetype) != basetype
7941 || TYPE_CANONICAL (rettype) != rettype);
7942 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
7943 &any_structural_p,
7944 &any_noncanonical_p);
7945 if (any_structural_p)
7946 SET_TYPE_STRUCTURAL_EQUALITY (t);
7947 else if (any_noncanonical_p)
7948 TYPE_CANONICAL (t)
7949 = build_method_type_directly (TYPE_CANONICAL (basetype),
7950 TYPE_CANONICAL (rettype),
7951 canon_argtypes);
7952 if (!COMPLETE_TYPE_P (t))
7953 layout_type (t);
7955 return t;
7958 /* Construct, lay out and return the type of methods belonging to class
7959 BASETYPE and whose arguments and values are described by TYPE.
7960 If that type exists already, reuse it.
7961 TYPE must be a FUNCTION_TYPE node. */
7963 tree
7964 build_method_type (tree basetype, tree type)
7966 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
7968 return build_method_type_directly (basetype,
7969 TREE_TYPE (type),
7970 TYPE_ARG_TYPES (type));
7973 /* Construct, lay out and return the type of offsets to a value
7974 of type TYPE, within an object of type BASETYPE.
7975 If a suitable offset type exists already, reuse it. */
7977 tree
7978 build_offset_type (tree basetype, tree type)
7980 tree t;
7981 hashval_t hashcode = 0;
7983 /* Make a node of the sort we want. */
7984 t = make_node (OFFSET_TYPE);
7986 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7987 TREE_TYPE (t) = type;
7989 /* If we already have such a type, use the old one. */
7990 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
7991 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
7992 t = type_hash_canon (hashcode, t);
7994 if (!COMPLETE_TYPE_P (t))
7995 layout_type (t);
7997 if (TYPE_CANONICAL (t) == t)
7999 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8000 || TYPE_STRUCTURAL_EQUALITY_P (type))
8001 SET_TYPE_STRUCTURAL_EQUALITY (t);
8002 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8003 || TYPE_CANONICAL (type) != type)
8004 TYPE_CANONICAL (t)
8005 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8006 TYPE_CANONICAL (type));
8009 return t;
8012 /* Create a complex type whose components are COMPONENT_TYPE. */
8014 tree
8015 build_complex_type (tree component_type)
8017 tree t;
8018 hashval_t hashcode;
8020 gcc_assert (INTEGRAL_TYPE_P (component_type)
8021 || SCALAR_FLOAT_TYPE_P (component_type)
8022 || FIXED_POINT_TYPE_P (component_type));
8024 /* Make a node of the sort we want. */
8025 t = make_node (COMPLEX_TYPE);
8027 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8029 /* If we already have such a type, use the old one. */
8030 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
8031 t = type_hash_canon (hashcode, t);
8033 if (!COMPLETE_TYPE_P (t))
8034 layout_type (t);
8036 if (TYPE_CANONICAL (t) == t)
8038 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8039 SET_TYPE_STRUCTURAL_EQUALITY (t);
8040 else if (TYPE_CANONICAL (component_type) != component_type)
8041 TYPE_CANONICAL (t)
8042 = build_complex_type (TYPE_CANONICAL (component_type));
8045 /* We need to create a name, since complex is a fundamental type. */
8046 if (! TYPE_NAME (t))
8048 const char *name;
8049 if (component_type == char_type_node)
8050 name = "complex char";
8051 else if (component_type == signed_char_type_node)
8052 name = "complex signed char";
8053 else if (component_type == unsigned_char_type_node)
8054 name = "complex unsigned char";
8055 else if (component_type == short_integer_type_node)
8056 name = "complex short int";
8057 else if (component_type == short_unsigned_type_node)
8058 name = "complex short unsigned int";
8059 else if (component_type == integer_type_node)
8060 name = "complex int";
8061 else if (component_type == unsigned_type_node)
8062 name = "complex unsigned int";
8063 else if (component_type == long_integer_type_node)
8064 name = "complex long int";
8065 else if (component_type == long_unsigned_type_node)
8066 name = "complex long unsigned int";
8067 else if (component_type == long_long_integer_type_node)
8068 name = "complex long long int";
8069 else if (component_type == long_long_unsigned_type_node)
8070 name = "complex long long unsigned int";
8071 else
8072 name = 0;
8074 if (name != 0)
8075 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8076 get_identifier (name), t);
8079 return build_qualified_type (t, TYPE_QUALS (component_type));
8082 /* If TYPE is a real or complex floating-point type and the target
8083 does not directly support arithmetic on TYPE then return the wider
8084 type to be used for arithmetic on TYPE. Otherwise, return
8085 NULL_TREE. */
8087 tree
8088 excess_precision_type (tree type)
8090 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8092 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8093 switch (TREE_CODE (type))
8095 case REAL_TYPE:
8096 switch (flt_eval_method)
8098 case 1:
8099 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8100 return double_type_node;
8101 break;
8102 case 2:
8103 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8104 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8105 return long_double_type_node;
8106 break;
8107 default:
8108 gcc_unreachable ();
8110 break;
8111 case COMPLEX_TYPE:
8112 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8113 return NULL_TREE;
8114 switch (flt_eval_method)
8116 case 1:
8117 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8118 return complex_double_type_node;
8119 break;
8120 case 2:
8121 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8122 || (TYPE_MODE (TREE_TYPE (type))
8123 == TYPE_MODE (double_type_node)))
8124 return complex_long_double_type_node;
8125 break;
8126 default:
8127 gcc_unreachable ();
8129 break;
8130 default:
8131 break;
8134 return NULL_TREE;
8137 /* Return OP, stripped of any conversions to wider types as much as is safe.
8138 Converting the value back to OP's type makes a value equivalent to OP.
8140 If FOR_TYPE is nonzero, we return a value which, if converted to
8141 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8143 OP must have integer, real or enumeral type. Pointers are not allowed!
8145 There are some cases where the obvious value we could return
8146 would regenerate to OP if converted to OP's type,
8147 but would not extend like OP to wider types.
8148 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8149 For example, if OP is (unsigned short)(signed char)-1,
8150 we avoid returning (signed char)-1 if FOR_TYPE is int,
8151 even though extending that to an unsigned short would regenerate OP,
8152 since the result of extending (signed char)-1 to (int)
8153 is different from (int) OP. */
8155 tree
8156 get_unwidened (tree op, tree for_type)
8158 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8159 tree type = TREE_TYPE (op);
8160 unsigned final_prec
8161 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8162 int uns
8163 = (for_type != 0 && for_type != type
8164 && final_prec > TYPE_PRECISION (type)
8165 && TYPE_UNSIGNED (type));
8166 tree win = op;
8168 while (CONVERT_EXPR_P (op))
8170 int bitschange;
8172 /* TYPE_PRECISION on vector types has different meaning
8173 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8174 so avoid them here. */
8175 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8176 break;
8178 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8179 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8181 /* Truncations are many-one so cannot be removed.
8182 Unless we are later going to truncate down even farther. */
8183 if (bitschange < 0
8184 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8185 break;
8187 /* See what's inside this conversion. If we decide to strip it,
8188 we will set WIN. */
8189 op = TREE_OPERAND (op, 0);
8191 /* If we have not stripped any zero-extensions (uns is 0),
8192 we can strip any kind of extension.
8193 If we have previously stripped a zero-extension,
8194 only zero-extensions can safely be stripped.
8195 Any extension can be stripped if the bits it would produce
8196 are all going to be discarded later by truncating to FOR_TYPE. */
8198 if (bitschange > 0)
8200 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8201 win = op;
8202 /* TYPE_UNSIGNED says whether this is a zero-extension.
8203 Let's avoid computing it if it does not affect WIN
8204 and if UNS will not be needed again. */
8205 if ((uns
8206 || CONVERT_EXPR_P (op))
8207 && TYPE_UNSIGNED (TREE_TYPE (op)))
8209 uns = 1;
8210 win = op;
8215 /* If we finally reach a constant see if it fits in for_type and
8216 in that case convert it. */
8217 if (for_type
8218 && TREE_CODE (win) == INTEGER_CST
8219 && TREE_TYPE (win) != for_type
8220 && int_fits_type_p (win, for_type))
8221 win = fold_convert (for_type, win);
8223 return win;
8226 /* Return OP or a simpler expression for a narrower value
8227 which can be sign-extended or zero-extended to give back OP.
8228 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8229 or 0 if the value should be sign-extended. */
8231 tree
8232 get_narrower (tree op, int *unsignedp_ptr)
8234 int uns = 0;
8235 int first = 1;
8236 tree win = op;
8237 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8239 while (TREE_CODE (op) == NOP_EXPR)
8241 int bitschange
8242 = (TYPE_PRECISION (TREE_TYPE (op))
8243 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8245 /* Truncations are many-one so cannot be removed. */
8246 if (bitschange < 0)
8247 break;
8249 /* See what's inside this conversion. If we decide to strip it,
8250 we will set WIN. */
8252 if (bitschange > 0)
8254 op = TREE_OPERAND (op, 0);
8255 /* An extension: the outermost one can be stripped,
8256 but remember whether it is zero or sign extension. */
8257 if (first)
8258 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8259 /* Otherwise, if a sign extension has been stripped,
8260 only sign extensions can now be stripped;
8261 if a zero extension has been stripped, only zero-extensions. */
8262 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8263 break;
8264 first = 0;
8266 else /* bitschange == 0 */
8268 /* A change in nominal type can always be stripped, but we must
8269 preserve the unsignedness. */
8270 if (first)
8271 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8272 first = 0;
8273 op = TREE_OPERAND (op, 0);
8274 /* Keep trying to narrow, but don't assign op to win if it
8275 would turn an integral type into something else. */
8276 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8277 continue;
8280 win = op;
8283 if (TREE_CODE (op) == COMPONENT_REF
8284 /* Since type_for_size always gives an integer type. */
8285 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8286 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8287 /* Ensure field is laid out already. */
8288 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8289 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
8291 unsigned HOST_WIDE_INT innerprec
8292 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
8293 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8294 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8295 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8297 /* We can get this structure field in a narrower type that fits it,
8298 but the resulting extension to its nominal type (a fullword type)
8299 must satisfy the same conditions as for other extensions.
8301 Do this only for fields that are aligned (not bit-fields),
8302 because when bit-field insns will be used there is no
8303 advantage in doing this. */
8305 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8306 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8307 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8308 && type != 0)
8310 if (first)
8311 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8312 win = fold_convert (type, op);
8316 *unsignedp_ptr = uns;
8317 return win;
8320 /* Returns true if integer constant C has a value that is permissible
8321 for type TYPE (an INTEGER_TYPE). */
8323 bool
8324 int_fits_type_p (const_tree c, const_tree type)
8326 tree type_low_bound, type_high_bound;
8327 bool ok_for_low_bound, ok_for_high_bound, unsc;
8328 double_int dc, dd;
8330 dc = tree_to_double_int (c);
8331 unsc = TYPE_UNSIGNED (TREE_TYPE (c));
8333 retry:
8334 type_low_bound = TYPE_MIN_VALUE (type);
8335 type_high_bound = TYPE_MAX_VALUE (type);
8337 /* If at least one bound of the type is a constant integer, we can check
8338 ourselves and maybe make a decision. If no such decision is possible, but
8339 this type is a subtype, try checking against that. Otherwise, use
8340 double_int_fits_to_tree_p, which checks against the precision.
8342 Compute the status for each possibly constant bound, and return if we see
8343 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8344 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8345 for "constant known to fit". */
8347 /* Check if c >= type_low_bound. */
8348 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8350 dd = tree_to_double_int (type_low_bound);
8351 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
8353 int c_neg = (!unsc && dc.is_negative ());
8354 int t_neg = (unsc && dd.is_negative ());
8356 if (c_neg && !t_neg)
8357 return false;
8358 if ((c_neg || !t_neg) && dc.ult (dd))
8359 return false;
8361 else if (dc.cmp (dd, unsc) < 0)
8362 return false;
8363 ok_for_low_bound = true;
8365 else
8366 ok_for_low_bound = false;
8368 /* Check if c <= type_high_bound. */
8369 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8371 dd = tree_to_double_int (type_high_bound);
8372 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
8374 int c_neg = (!unsc && dc.is_negative ());
8375 int t_neg = (unsc && dd.is_negative ());
8377 if (t_neg && !c_neg)
8378 return false;
8379 if ((t_neg || !c_neg) && dc.ugt (dd))
8380 return false;
8382 else if (dc.cmp (dd, unsc) > 0)
8383 return false;
8384 ok_for_high_bound = true;
8386 else
8387 ok_for_high_bound = false;
8389 /* If the constant fits both bounds, the result is known. */
8390 if (ok_for_low_bound && ok_for_high_bound)
8391 return true;
8393 /* Perform some generic filtering which may allow making a decision
8394 even if the bounds are not constant. First, negative integers
8395 never fit in unsigned types, */
8396 if (TYPE_UNSIGNED (type) && !unsc && dc.is_negative ())
8397 return false;
8399 /* Second, narrower types always fit in wider ones. */
8400 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8401 return true;
8403 /* Third, unsigned integers with top bit set never fit signed types. */
8404 if (! TYPE_UNSIGNED (type) && unsc)
8406 int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (c))) - 1;
8407 if (prec < HOST_BITS_PER_WIDE_INT)
8409 if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
8410 return false;
8412 else if (((((unsigned HOST_WIDE_INT) 1)
8413 << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
8414 return false;
8417 /* If we haven't been able to decide at this point, there nothing more we
8418 can check ourselves here. Look at the base type if we have one and it
8419 has the same precision. */
8420 if (TREE_CODE (type) == INTEGER_TYPE
8421 && TREE_TYPE (type) != 0
8422 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8424 type = TREE_TYPE (type);
8425 goto retry;
8428 /* Or to double_int_fits_to_tree_p, if nothing else. */
8429 return double_int_fits_to_tree_p (type, dc);
8432 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8433 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8434 represented (assuming two's-complement arithmetic) within the bit
8435 precision of the type are returned instead. */
8437 void
8438 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8440 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8441 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8442 mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
8443 TYPE_UNSIGNED (type));
8444 else
8446 if (TYPE_UNSIGNED (type))
8447 mpz_set_ui (min, 0);
8448 else
8450 double_int mn;
8451 mn = double_int::mask (TYPE_PRECISION (type) - 1);
8452 mn = (mn + double_int_one).sext (TYPE_PRECISION (type));
8453 mpz_set_double_int (min, mn, false);
8457 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8458 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8459 mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
8460 TYPE_UNSIGNED (type));
8461 else
8463 if (TYPE_UNSIGNED (type))
8464 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)),
8465 true);
8466 else
8467 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type) - 1),
8468 true);
8472 /* Return true if VAR is an automatic variable defined in function FN. */
8474 bool
8475 auto_var_in_fn_p (const_tree var, const_tree fn)
8477 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8478 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8479 || TREE_CODE (var) == PARM_DECL)
8480 && ! TREE_STATIC (var))
8481 || TREE_CODE (var) == LABEL_DECL
8482 || TREE_CODE (var) == RESULT_DECL));
8485 /* Subprogram of following function. Called by walk_tree.
8487 Return *TP if it is an automatic variable or parameter of the
8488 function passed in as DATA. */
8490 static tree
8491 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8493 tree fn = (tree) data;
8495 if (TYPE_P (*tp))
8496 *walk_subtrees = 0;
8498 else if (DECL_P (*tp)
8499 && auto_var_in_fn_p (*tp, fn))
8500 return *tp;
8502 return NULL_TREE;
8505 /* Returns true if T is, contains, or refers to a type with variable
8506 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8507 arguments, but not the return type. If FN is nonzero, only return
8508 true if a modifier of the type or position of FN is a variable or
8509 parameter inside FN.
8511 This concept is more general than that of C99 'variably modified types':
8512 in C99, a struct type is never variably modified because a VLA may not
8513 appear as a structure member. However, in GNU C code like:
8515 struct S { int i[f()]; };
8517 is valid, and other languages may define similar constructs. */
8519 bool
8520 variably_modified_type_p (tree type, tree fn)
8522 tree t;
8524 /* Test if T is either variable (if FN is zero) or an expression containing
8525 a variable in FN. If TYPE isn't gimplified, return true also if
8526 gimplify_one_sizepos would gimplify the expression into a local
8527 variable. */
8528 #define RETURN_TRUE_IF_VAR(T) \
8529 do { tree _t = (T); \
8530 if (_t != NULL_TREE \
8531 && _t != error_mark_node \
8532 && TREE_CODE (_t) != INTEGER_CST \
8533 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8534 && (!fn \
8535 || (!TYPE_SIZES_GIMPLIFIED (type) \
8536 && !is_gimple_sizepos (_t)) \
8537 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8538 return true; } while (0)
8540 if (type == error_mark_node)
8541 return false;
8543 /* If TYPE itself has variable size, it is variably modified. */
8544 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8545 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8547 switch (TREE_CODE (type))
8549 case POINTER_TYPE:
8550 case REFERENCE_TYPE:
8551 case VECTOR_TYPE:
8552 if (variably_modified_type_p (TREE_TYPE (type), fn))
8553 return true;
8554 break;
8556 case FUNCTION_TYPE:
8557 case METHOD_TYPE:
8558 /* If TYPE is a function type, it is variably modified if the
8559 return type is variably modified. */
8560 if (variably_modified_type_p (TREE_TYPE (type), fn))
8561 return true;
8562 break;
8564 case INTEGER_TYPE:
8565 case REAL_TYPE:
8566 case FIXED_POINT_TYPE:
8567 case ENUMERAL_TYPE:
8568 case BOOLEAN_TYPE:
8569 /* Scalar types are variably modified if their end points
8570 aren't constant. */
8571 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8572 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8573 break;
8575 case RECORD_TYPE:
8576 case UNION_TYPE:
8577 case QUAL_UNION_TYPE:
8578 /* We can't see if any of the fields are variably-modified by the
8579 definition we normally use, since that would produce infinite
8580 recursion via pointers. */
8581 /* This is variably modified if some field's type is. */
8582 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8583 if (TREE_CODE (t) == FIELD_DECL)
8585 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8586 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8587 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8589 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8590 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8592 break;
8594 case ARRAY_TYPE:
8595 /* Do not call ourselves to avoid infinite recursion. This is
8596 variably modified if the element type is. */
8597 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8598 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8599 break;
8601 default:
8602 break;
8605 /* The current language may have other cases to check, but in general,
8606 all other types are not variably modified. */
8607 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8609 #undef RETURN_TRUE_IF_VAR
8612 /* Given a DECL or TYPE, return the scope in which it was declared, or
8613 NULL_TREE if there is no containing scope. */
8615 tree
8616 get_containing_scope (const_tree t)
8618 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8621 /* Return the innermost context enclosing DECL that is
8622 a FUNCTION_DECL, or zero if none. */
8624 tree
8625 decl_function_context (const_tree decl)
8627 tree context;
8629 if (TREE_CODE (decl) == ERROR_MARK)
8630 return 0;
8632 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8633 where we look up the function at runtime. Such functions always take
8634 a first argument of type 'pointer to real context'.
8636 C++ should really be fixed to use DECL_CONTEXT for the real context,
8637 and use something else for the "virtual context". */
8638 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8639 context
8640 = TYPE_MAIN_VARIANT
8641 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8642 else
8643 context = DECL_CONTEXT (decl);
8645 while (context && TREE_CODE (context) != FUNCTION_DECL)
8647 if (TREE_CODE (context) == BLOCK)
8648 context = BLOCK_SUPERCONTEXT (context);
8649 else
8650 context = get_containing_scope (context);
8653 return context;
8656 /* Return the innermost context enclosing DECL that is
8657 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8658 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8660 tree
8661 decl_type_context (const_tree decl)
8663 tree context = DECL_CONTEXT (decl);
8665 while (context)
8666 switch (TREE_CODE (context))
8668 case NAMESPACE_DECL:
8669 case TRANSLATION_UNIT_DECL:
8670 return NULL_TREE;
8672 case RECORD_TYPE:
8673 case UNION_TYPE:
8674 case QUAL_UNION_TYPE:
8675 return context;
8677 case TYPE_DECL:
8678 case FUNCTION_DECL:
8679 context = DECL_CONTEXT (context);
8680 break;
8682 case BLOCK:
8683 context = BLOCK_SUPERCONTEXT (context);
8684 break;
8686 default:
8687 gcc_unreachable ();
8690 return NULL_TREE;
8693 /* CALL is a CALL_EXPR. Return the declaration for the function
8694 called, or NULL_TREE if the called function cannot be
8695 determined. */
8697 tree
8698 get_callee_fndecl (const_tree call)
8700 tree addr;
8702 if (call == error_mark_node)
8703 return error_mark_node;
8705 /* It's invalid to call this function with anything but a
8706 CALL_EXPR. */
8707 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8709 /* The first operand to the CALL is the address of the function
8710 called. */
8711 addr = CALL_EXPR_FN (call);
8713 STRIP_NOPS (addr);
8715 /* If this is a readonly function pointer, extract its initial value. */
8716 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8717 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8718 && DECL_INITIAL (addr))
8719 addr = DECL_INITIAL (addr);
8721 /* If the address is just `&f' for some function `f', then we know
8722 that `f' is being called. */
8723 if (TREE_CODE (addr) == ADDR_EXPR
8724 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8725 return TREE_OPERAND (addr, 0);
8727 /* We couldn't figure out what was being called. */
8728 return NULL_TREE;
8731 /* Print debugging information about tree nodes generated during the compile,
8732 and any language-specific information. */
8734 void
8735 dump_tree_statistics (void)
8737 if (GATHER_STATISTICS)
8739 int i;
8740 int total_nodes, total_bytes;
8741 fprintf (stderr, "Kind Nodes Bytes\n");
8742 fprintf (stderr, "---------------------------------------\n");
8743 total_nodes = total_bytes = 0;
8744 for (i = 0; i < (int) all_kinds; i++)
8746 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
8747 tree_node_counts[i], tree_node_sizes[i]);
8748 total_nodes += tree_node_counts[i];
8749 total_bytes += tree_node_sizes[i];
8751 fprintf (stderr, "---------------------------------------\n");
8752 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
8753 fprintf (stderr, "---------------------------------------\n");
8754 fprintf (stderr, "Code Nodes\n");
8755 fprintf (stderr, "----------------------------\n");
8756 for (i = 0; i < (int) MAX_TREE_CODES; i++)
8757 fprintf (stderr, "%-20s %7d\n", tree_code_name[i], tree_code_counts[i]);
8758 fprintf (stderr, "----------------------------\n");
8759 ssanames_print_statistics ();
8760 phinodes_print_statistics ();
8762 else
8763 fprintf (stderr, "(No per-node statistics)\n");
8765 print_type_hash_statistics ();
8766 print_debug_expr_statistics ();
8767 print_value_expr_statistics ();
8768 lang_hooks.print_statistics ();
8771 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8773 /* Generate a crc32 of a byte. */
8775 static unsigned
8776 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
8778 unsigned ix;
8780 for (ix = bits; ix--; value <<= 1)
8782 unsigned feedback;
8784 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
8785 chksum <<= 1;
8786 chksum ^= feedback;
8788 return chksum;
8791 /* Generate a crc32 of a 32-bit unsigned. */
8793 unsigned
8794 crc32_unsigned (unsigned chksum, unsigned value)
8796 return crc32_unsigned_bits (chksum, value, 32);
8799 /* Generate a crc32 of a byte. */
8801 unsigned
8802 crc32_byte (unsigned chksum, char byte)
8804 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
8807 /* Generate a crc32 of a string. */
8809 unsigned
8810 crc32_string (unsigned chksum, const char *string)
8814 chksum = crc32_byte (chksum, *string);
8816 while (*string++);
8817 return chksum;
8820 /* P is a string that will be used in a symbol. Mask out any characters
8821 that are not valid in that context. */
8823 void
8824 clean_symbol_name (char *p)
8826 for (; *p; p++)
8827 if (! (ISALNUM (*p)
8828 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
8829 || *p == '$'
8830 #endif
8831 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
8832 || *p == '.'
8833 #endif
8835 *p = '_';
8838 /* Generate a name for a special-purpose function.
8839 The generated name may need to be unique across the whole link.
8840 Changes to this function may also require corresponding changes to
8841 xstrdup_mask_random.
8842 TYPE is some string to identify the purpose of this function to the
8843 linker or collect2; it must start with an uppercase letter,
8844 one of:
8845 I - for constructors
8846 D - for destructors
8847 N - for C++ anonymous namespaces
8848 F - for DWARF unwind frame information. */
8850 tree
8851 get_file_function_name (const char *type)
8853 char *buf;
8854 const char *p;
8855 char *q;
8857 /* If we already have a name we know to be unique, just use that. */
8858 if (first_global_object_name)
8859 p = q = ASTRDUP (first_global_object_name);
8860 /* If the target is handling the constructors/destructors, they
8861 will be local to this file and the name is only necessary for
8862 debugging purposes.
8863 We also assign sub_I and sub_D sufixes to constructors called from
8864 the global static constructors. These are always local. */
8865 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
8866 || (strncmp (type, "sub_", 4) == 0
8867 && (type[4] == 'I' || type[4] == 'D')))
8869 const char *file = main_input_filename;
8870 if (! file)
8871 file = input_filename;
8872 /* Just use the file's basename, because the full pathname
8873 might be quite long. */
8874 p = q = ASTRDUP (lbasename (file));
8876 else
8878 /* Otherwise, the name must be unique across the entire link.
8879 We don't have anything that we know to be unique to this translation
8880 unit, so use what we do have and throw in some randomness. */
8881 unsigned len;
8882 const char *name = weak_global_object_name;
8883 const char *file = main_input_filename;
8885 if (! name)
8886 name = "";
8887 if (! file)
8888 file = input_filename;
8890 len = strlen (file);
8891 q = (char *) alloca (9 + 17 + len + 1);
8892 memcpy (q, file, len + 1);
8894 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
8895 crc32_string (0, name), get_random_seed (false));
8897 p = q;
8900 clean_symbol_name (q);
8901 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
8902 + strlen (type));
8904 /* Set up the name of the file-level functions we may need.
8905 Use a global object (which is already required to be unique over
8906 the program) rather than the file name (which imposes extra
8907 constraints). */
8908 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
8910 return get_identifier (buf);
8913 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
8915 /* Complain that the tree code of NODE does not match the expected 0
8916 terminated list of trailing codes. The trailing code list can be
8917 empty, for a more vague error message. FILE, LINE, and FUNCTION
8918 are of the caller. */
8920 void
8921 tree_check_failed (const_tree node, const char *file,
8922 int line, const char *function, ...)
8924 va_list args;
8925 const char *buffer;
8926 unsigned length = 0;
8927 int code;
8929 va_start (args, function);
8930 while ((code = va_arg (args, int)))
8931 length += 4 + strlen (tree_code_name[code]);
8932 va_end (args);
8933 if (length)
8935 char *tmp;
8936 va_start (args, function);
8937 length += strlen ("expected ");
8938 buffer = tmp = (char *) alloca (length);
8939 length = 0;
8940 while ((code = va_arg (args, int)))
8942 const char *prefix = length ? " or " : "expected ";
8944 strcpy (tmp + length, prefix);
8945 length += strlen (prefix);
8946 strcpy (tmp + length, tree_code_name[code]);
8947 length += strlen (tree_code_name[code]);
8949 va_end (args);
8951 else
8952 buffer = "unexpected node";
8954 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8955 buffer, tree_code_name[TREE_CODE (node)],
8956 function, trim_filename (file), line);
8959 /* Complain that the tree code of NODE does match the expected 0
8960 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
8961 the caller. */
8963 void
8964 tree_not_check_failed (const_tree node, const char *file,
8965 int line, const char *function, ...)
8967 va_list args;
8968 char *buffer;
8969 unsigned length = 0;
8970 int code;
8972 va_start (args, function);
8973 while ((code = va_arg (args, int)))
8974 length += 4 + strlen (tree_code_name[code]);
8975 va_end (args);
8976 va_start (args, function);
8977 buffer = (char *) alloca (length);
8978 length = 0;
8979 while ((code = va_arg (args, int)))
8981 if (length)
8983 strcpy (buffer + length, " or ");
8984 length += 4;
8986 strcpy (buffer + length, tree_code_name[code]);
8987 length += strlen (tree_code_name[code]);
8989 va_end (args);
8991 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
8992 buffer, tree_code_name[TREE_CODE (node)],
8993 function, trim_filename (file), line);
8996 /* Similar to tree_check_failed, except that we check for a class of tree
8997 code, given in CL. */
8999 void
9000 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9001 const char *file, int line, const char *function)
9003 internal_error
9004 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9005 TREE_CODE_CLASS_STRING (cl),
9006 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9007 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9010 /* Similar to tree_check_failed, except that instead of specifying a
9011 dozen codes, use the knowledge that they're all sequential. */
9013 void
9014 tree_range_check_failed (const_tree node, const char *file, int line,
9015 const char *function, enum tree_code c1,
9016 enum tree_code c2)
9018 char *buffer;
9019 unsigned length = 0;
9020 unsigned int c;
9022 for (c = c1; c <= c2; ++c)
9023 length += 4 + strlen (tree_code_name[c]);
9025 length += strlen ("expected ");
9026 buffer = (char *) alloca (length);
9027 length = 0;
9029 for (c = c1; c <= c2; ++c)
9031 const char *prefix = length ? " or " : "expected ";
9033 strcpy (buffer + length, prefix);
9034 length += strlen (prefix);
9035 strcpy (buffer + length, tree_code_name[c]);
9036 length += strlen (tree_code_name[c]);
9039 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9040 buffer, tree_code_name[TREE_CODE (node)],
9041 function, trim_filename (file), line);
9045 /* Similar to tree_check_failed, except that we check that a tree does
9046 not have the specified code, given in CL. */
9048 void
9049 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9050 const char *file, int line, const char *function)
9052 internal_error
9053 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9054 TREE_CODE_CLASS_STRING (cl),
9055 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9056 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9060 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9062 void
9063 omp_clause_check_failed (const_tree node, const char *file, int line,
9064 const char *function, enum omp_clause_code code)
9066 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9067 omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
9068 function, trim_filename (file), line);
9072 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9074 void
9075 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9076 const char *function, enum omp_clause_code c1,
9077 enum omp_clause_code c2)
9079 char *buffer;
9080 unsigned length = 0;
9081 unsigned int c;
9083 for (c = c1; c <= c2; ++c)
9084 length += 4 + strlen (omp_clause_code_name[c]);
9086 length += strlen ("expected ");
9087 buffer = (char *) alloca (length);
9088 length = 0;
9090 for (c = c1; c <= c2; ++c)
9092 const char *prefix = length ? " or " : "expected ";
9094 strcpy (buffer + length, prefix);
9095 length += strlen (prefix);
9096 strcpy (buffer + length, omp_clause_code_name[c]);
9097 length += strlen (omp_clause_code_name[c]);
9100 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9101 buffer, omp_clause_code_name[TREE_CODE (node)],
9102 function, trim_filename (file), line);
9106 #undef DEFTREESTRUCT
9107 #define DEFTREESTRUCT(VAL, NAME) NAME,
9109 static const char *ts_enum_names[] = {
9110 #include "treestruct.def"
9112 #undef DEFTREESTRUCT
9114 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9116 /* Similar to tree_class_check_failed, except that we check for
9117 whether CODE contains the tree structure identified by EN. */
9119 void
9120 tree_contains_struct_check_failed (const_tree node,
9121 const enum tree_node_structure_enum en,
9122 const char *file, int line,
9123 const char *function)
9125 internal_error
9126 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9127 TS_ENUM_NAME(en),
9128 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9132 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9133 (dynamically sized) vector. */
9135 void
9136 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9137 const char *function)
9139 internal_error
9140 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9141 idx + 1, len, function, trim_filename (file), line);
9144 /* Similar to above, except that the check is for the bounds of the operand
9145 vector of an expression node EXP. */
9147 void
9148 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9149 int line, const char *function)
9151 int code = TREE_CODE (exp);
9152 internal_error
9153 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9154 idx + 1, tree_code_name[code], TREE_OPERAND_LENGTH (exp),
9155 function, trim_filename (file), line);
9158 /* Similar to above, except that the check is for the number of
9159 operands of an OMP_CLAUSE node. */
9161 void
9162 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9163 int line, const char *function)
9165 internal_error
9166 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9167 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9168 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9169 trim_filename (file), line);
9171 #endif /* ENABLE_TREE_CHECKING */
9173 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9174 and mapped to the machine mode MODE. Initialize its fields and build
9175 the information necessary for debugging output. */
9177 static tree
9178 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
9180 tree t;
9181 hashval_t hashcode = 0;
9183 t = make_node (VECTOR_TYPE);
9184 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9185 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9186 SET_TYPE_MODE (t, mode);
9188 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9189 SET_TYPE_STRUCTURAL_EQUALITY (t);
9190 else if (TYPE_CANONICAL (innertype) != innertype
9191 || mode != VOIDmode)
9192 TYPE_CANONICAL (t)
9193 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9195 layout_type (t);
9197 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
9198 hashcode = iterative_hash_host_wide_int (nunits, hashcode);
9199 hashcode = iterative_hash_host_wide_int (mode, hashcode);
9200 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (t)), hashcode);
9201 t = type_hash_canon (hashcode, t);
9203 /* We have built a main variant, based on the main variant of the
9204 inner type. Use it to build the variant we return. */
9205 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9206 && TREE_TYPE (t) != innertype)
9207 return build_type_attribute_qual_variant (t,
9208 TYPE_ATTRIBUTES (innertype),
9209 TYPE_QUALS (innertype));
9211 return t;
9214 static tree
9215 make_or_reuse_type (unsigned size, int unsignedp)
9217 if (size == INT_TYPE_SIZE)
9218 return unsignedp ? unsigned_type_node : integer_type_node;
9219 if (size == CHAR_TYPE_SIZE)
9220 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9221 if (size == SHORT_TYPE_SIZE)
9222 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9223 if (size == LONG_TYPE_SIZE)
9224 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9225 if (size == LONG_LONG_TYPE_SIZE)
9226 return (unsignedp ? long_long_unsigned_type_node
9227 : long_long_integer_type_node);
9228 if (size == 128 && int128_integer_type_node)
9229 return (unsignedp ? int128_unsigned_type_node
9230 : int128_integer_type_node);
9232 if (unsignedp)
9233 return make_unsigned_type (size);
9234 else
9235 return make_signed_type (size);
9238 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9240 static tree
9241 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9243 if (satp)
9245 if (size == SHORT_FRACT_TYPE_SIZE)
9246 return unsignedp ? sat_unsigned_short_fract_type_node
9247 : sat_short_fract_type_node;
9248 if (size == FRACT_TYPE_SIZE)
9249 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9250 if (size == LONG_FRACT_TYPE_SIZE)
9251 return unsignedp ? sat_unsigned_long_fract_type_node
9252 : sat_long_fract_type_node;
9253 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9254 return unsignedp ? sat_unsigned_long_long_fract_type_node
9255 : sat_long_long_fract_type_node;
9257 else
9259 if (size == SHORT_FRACT_TYPE_SIZE)
9260 return unsignedp ? unsigned_short_fract_type_node
9261 : short_fract_type_node;
9262 if (size == FRACT_TYPE_SIZE)
9263 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9264 if (size == LONG_FRACT_TYPE_SIZE)
9265 return unsignedp ? unsigned_long_fract_type_node
9266 : long_fract_type_node;
9267 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9268 return unsignedp ? unsigned_long_long_fract_type_node
9269 : long_long_fract_type_node;
9272 return make_fract_type (size, unsignedp, satp);
9275 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9277 static tree
9278 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9280 if (satp)
9282 if (size == SHORT_ACCUM_TYPE_SIZE)
9283 return unsignedp ? sat_unsigned_short_accum_type_node
9284 : sat_short_accum_type_node;
9285 if (size == ACCUM_TYPE_SIZE)
9286 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9287 if (size == LONG_ACCUM_TYPE_SIZE)
9288 return unsignedp ? sat_unsigned_long_accum_type_node
9289 : sat_long_accum_type_node;
9290 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9291 return unsignedp ? sat_unsigned_long_long_accum_type_node
9292 : sat_long_long_accum_type_node;
9294 else
9296 if (size == SHORT_ACCUM_TYPE_SIZE)
9297 return unsignedp ? unsigned_short_accum_type_node
9298 : short_accum_type_node;
9299 if (size == ACCUM_TYPE_SIZE)
9300 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9301 if (size == LONG_ACCUM_TYPE_SIZE)
9302 return unsignedp ? unsigned_long_accum_type_node
9303 : long_accum_type_node;
9304 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9305 return unsignedp ? unsigned_long_long_accum_type_node
9306 : long_long_accum_type_node;
9309 return make_accum_type (size, unsignedp, satp);
9312 /* Create nodes for all integer types (and error_mark_node) using the sizes
9313 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9314 SHORT_DOUBLE specifies whether double should be of the same precision
9315 as float. */
9317 void
9318 build_common_tree_nodes (bool signed_char, bool short_double)
9320 error_mark_node = make_node (ERROR_MARK);
9321 TREE_TYPE (error_mark_node) = error_mark_node;
9323 initialize_sizetypes ();
9325 /* Define both `signed char' and `unsigned char'. */
9326 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9327 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9328 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9329 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9331 /* Define `char', which is like either `signed char' or `unsigned char'
9332 but not the same as either. */
9333 char_type_node
9334 = (signed_char
9335 ? make_signed_type (CHAR_TYPE_SIZE)
9336 : make_unsigned_type (CHAR_TYPE_SIZE));
9337 TYPE_STRING_FLAG (char_type_node) = 1;
9339 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9340 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9341 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9342 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9343 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9344 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9345 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9346 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9347 #if HOST_BITS_PER_WIDE_INT >= 64
9348 /* TODO: This isn't correct, but as logic depends at the moment on
9349 host's instead of target's wide-integer.
9350 If there is a target not supporting TImode, but has an 128-bit
9351 integer-scalar register, this target check needs to be adjusted. */
9352 if (targetm.scalar_mode_supported_p (TImode))
9354 int128_integer_type_node = make_signed_type (128);
9355 int128_unsigned_type_node = make_unsigned_type (128);
9357 #endif
9359 /* Define a boolean type. This type only represents boolean values but
9360 may be larger than char depending on the value of BOOL_TYPE_SIZE.
9361 Front ends which want to override this size (i.e. Java) can redefine
9362 boolean_type_node before calling build_common_tree_nodes_2. */
9363 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9364 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9365 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9366 TYPE_PRECISION (boolean_type_node) = 1;
9368 /* Define what type to use for size_t. */
9369 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9370 size_type_node = unsigned_type_node;
9371 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9372 size_type_node = long_unsigned_type_node;
9373 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9374 size_type_node = long_long_unsigned_type_node;
9375 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9376 size_type_node = short_unsigned_type_node;
9377 else
9378 gcc_unreachable ();
9380 /* Fill in the rest of the sized types. Reuse existing type nodes
9381 when possible. */
9382 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9383 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9384 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9385 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9386 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9388 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9389 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9390 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9391 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9392 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9394 access_public_node = get_identifier ("public");
9395 access_protected_node = get_identifier ("protected");
9396 access_private_node = get_identifier ("private");
9398 /* Define these next since types below may used them. */
9399 integer_zero_node = build_int_cst (integer_type_node, 0);
9400 integer_one_node = build_int_cst (integer_type_node, 1);
9401 integer_three_node = build_int_cst (integer_type_node, 3);
9402 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9404 size_zero_node = size_int (0);
9405 size_one_node = size_int (1);
9406 bitsize_zero_node = bitsize_int (0);
9407 bitsize_one_node = bitsize_int (1);
9408 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9410 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9411 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9413 void_type_node = make_node (VOID_TYPE);
9414 layout_type (void_type_node);
9416 /* We are not going to have real types in C with less than byte alignment,
9417 so we might as well not have any types that claim to have it. */
9418 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9419 TYPE_USER_ALIGN (void_type_node) = 0;
9421 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9422 layout_type (TREE_TYPE (null_pointer_node));
9424 ptr_type_node = build_pointer_type (void_type_node);
9425 const_ptr_type_node
9426 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9427 fileptr_type_node = ptr_type_node;
9429 float_type_node = make_node (REAL_TYPE);
9430 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9431 layout_type (float_type_node);
9433 double_type_node = make_node (REAL_TYPE);
9434 if (short_double)
9435 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9436 else
9437 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9438 layout_type (double_type_node);
9440 long_double_type_node = make_node (REAL_TYPE);
9441 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9442 layout_type (long_double_type_node);
9444 float_ptr_type_node = build_pointer_type (float_type_node);
9445 double_ptr_type_node = build_pointer_type (double_type_node);
9446 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9447 integer_ptr_type_node = build_pointer_type (integer_type_node);
9449 /* Fixed size integer types. */
9450 uint16_type_node = build_nonstandard_integer_type (16, true);
9451 uint32_type_node = build_nonstandard_integer_type (32, true);
9452 uint64_type_node = build_nonstandard_integer_type (64, true);
9454 /* Decimal float types. */
9455 dfloat32_type_node = make_node (REAL_TYPE);
9456 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9457 layout_type (dfloat32_type_node);
9458 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9459 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9461 dfloat64_type_node = make_node (REAL_TYPE);
9462 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9463 layout_type (dfloat64_type_node);
9464 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9465 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9467 dfloat128_type_node = make_node (REAL_TYPE);
9468 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9469 layout_type (dfloat128_type_node);
9470 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9471 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9473 complex_integer_type_node = build_complex_type (integer_type_node);
9474 complex_float_type_node = build_complex_type (float_type_node);
9475 complex_double_type_node = build_complex_type (double_type_node);
9476 complex_long_double_type_node = build_complex_type (long_double_type_node);
9478 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9479 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9480 sat_ ## KIND ## _type_node = \
9481 make_sat_signed_ ## KIND ## _type (SIZE); \
9482 sat_unsigned_ ## KIND ## _type_node = \
9483 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9484 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9485 unsigned_ ## KIND ## _type_node = \
9486 make_unsigned_ ## KIND ## _type (SIZE);
9488 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9489 sat_ ## WIDTH ## KIND ## _type_node = \
9490 make_sat_signed_ ## KIND ## _type (SIZE); \
9491 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9492 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9493 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9494 unsigned_ ## WIDTH ## KIND ## _type_node = \
9495 make_unsigned_ ## KIND ## _type (SIZE);
9497 /* Make fixed-point type nodes based on four different widths. */
9498 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9499 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9500 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9501 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9502 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9504 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9505 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9506 NAME ## _type_node = \
9507 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9508 u ## NAME ## _type_node = \
9509 make_or_reuse_unsigned_ ## KIND ## _type \
9510 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9511 sat_ ## NAME ## _type_node = \
9512 make_or_reuse_sat_signed_ ## KIND ## _type \
9513 (GET_MODE_BITSIZE (MODE ## mode)); \
9514 sat_u ## NAME ## _type_node = \
9515 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9516 (GET_MODE_BITSIZE (U ## MODE ## mode));
9518 /* Fixed-point type and mode nodes. */
9519 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9520 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9521 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9522 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9523 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9524 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9525 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9526 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9527 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9528 MAKE_FIXED_MODE_NODE (accum, da, DA)
9529 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9532 tree t = targetm.build_builtin_va_list ();
9534 /* Many back-ends define record types without setting TYPE_NAME.
9535 If we copied the record type here, we'd keep the original
9536 record type without a name. This breaks name mangling. So,
9537 don't copy record types and let c_common_nodes_and_builtins()
9538 declare the type to be __builtin_va_list. */
9539 if (TREE_CODE (t) != RECORD_TYPE)
9540 t = build_variant_type_copy (t);
9542 va_list_type_node = t;
9546 /* Modify DECL for given flags. */
9547 void
9548 set_call_expr_flags (tree decl, int flags)
9550 if (flags & ECF_NOTHROW)
9551 TREE_NOTHROW (decl) = 1;
9552 if (flags & ECF_CONST)
9553 TREE_READONLY (decl) = 1;
9554 if (flags & ECF_PURE)
9555 DECL_PURE_P (decl) = 1;
9556 if (flags & ECF_LOOPING_CONST_OR_PURE)
9557 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9558 if (flags & ECF_NOVOPS)
9559 DECL_IS_NOVOPS (decl) = 1;
9560 if (flags & ECF_NORETURN)
9561 TREE_THIS_VOLATILE (decl) = 1;
9562 if (flags & ECF_MALLOC)
9563 DECL_IS_MALLOC (decl) = 1;
9564 if (flags & ECF_RETURNS_TWICE)
9565 DECL_IS_RETURNS_TWICE (decl) = 1;
9566 if (flags & ECF_LEAF)
9567 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9568 NULL, DECL_ATTRIBUTES (decl));
9569 if ((flags & ECF_TM_PURE) && flag_tm)
9570 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("transaction_pure"),
9571 NULL, DECL_ATTRIBUTES (decl));
9572 /* Looping const or pure is implied by noreturn.
9573 There is currently no way to declare looping const or looping pure alone. */
9574 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9575 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9579 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9581 static void
9582 local_define_builtin (const char *name, tree type, enum built_in_function code,
9583 const char *library_name, int ecf_flags)
9585 tree decl;
9587 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9588 library_name, NULL_TREE);
9589 set_call_expr_flags (decl, ecf_flags);
9591 set_builtin_decl (code, decl, true);
9594 /* Call this function after instantiating all builtins that the language
9595 front end cares about. This will build the rest of the builtins that
9596 are relied upon by the tree optimizers and the middle-end. */
9598 void
9599 build_common_builtin_nodes (void)
9601 tree tmp, ftype;
9602 int ecf_flags;
9604 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9606 ftype = build_function_type (void_type_node, void_list_node);
9607 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
9608 "__builtin_unreachable",
9609 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9610 | ECF_CONST | ECF_LEAF);
9613 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9614 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9616 ftype = build_function_type_list (ptr_type_node,
9617 ptr_type_node, const_ptr_type_node,
9618 size_type_node, NULL_TREE);
9620 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
9621 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9622 "memcpy", ECF_NOTHROW | ECF_LEAF);
9623 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9624 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9625 "memmove", ECF_NOTHROW | ECF_LEAF);
9628 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
9630 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9631 const_ptr_type_node, size_type_node,
9632 NULL_TREE);
9633 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9634 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9637 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
9639 ftype = build_function_type_list (ptr_type_node,
9640 ptr_type_node, integer_type_node,
9641 size_type_node, NULL_TREE);
9642 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9643 "memset", ECF_NOTHROW | ECF_LEAF);
9646 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
9648 ftype = build_function_type_list (ptr_type_node,
9649 size_type_node, NULL_TREE);
9650 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9651 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9654 ftype = build_function_type_list (ptr_type_node, size_type_node,
9655 size_type_node, NULL_TREE);
9656 local_define_builtin ("__builtin_alloca_with_align", ftype,
9657 BUILT_IN_ALLOCA_WITH_ALIGN, "alloca",
9658 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9660 /* If we're checking the stack, `alloca' can throw. */
9661 if (flag_stack_check)
9663 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
9664 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
9667 ftype = build_function_type_list (void_type_node,
9668 ptr_type_node, ptr_type_node,
9669 ptr_type_node, NULL_TREE);
9670 local_define_builtin ("__builtin_init_trampoline", ftype,
9671 BUILT_IN_INIT_TRAMPOLINE,
9672 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9673 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
9674 BUILT_IN_INIT_HEAP_TRAMPOLINE,
9675 "__builtin_init_heap_trampoline",
9676 ECF_NOTHROW | ECF_LEAF);
9678 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9679 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9680 BUILT_IN_ADJUST_TRAMPOLINE,
9681 "__builtin_adjust_trampoline",
9682 ECF_CONST | ECF_NOTHROW);
9684 ftype = build_function_type_list (void_type_node,
9685 ptr_type_node, ptr_type_node, NULL_TREE);
9686 local_define_builtin ("__builtin_nonlocal_goto", ftype,
9687 BUILT_IN_NONLOCAL_GOTO,
9688 "__builtin_nonlocal_goto",
9689 ECF_NORETURN | ECF_NOTHROW);
9691 ftype = build_function_type_list (void_type_node,
9692 ptr_type_node, ptr_type_node, NULL_TREE);
9693 local_define_builtin ("__builtin_setjmp_setup", ftype,
9694 BUILT_IN_SETJMP_SETUP,
9695 "__builtin_setjmp_setup", ECF_NOTHROW);
9697 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9698 local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
9699 BUILT_IN_SETJMP_DISPATCHER,
9700 "__builtin_setjmp_dispatcher",
9701 ECF_PURE | ECF_NOTHROW);
9703 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9704 local_define_builtin ("__builtin_setjmp_receiver", ftype,
9705 BUILT_IN_SETJMP_RECEIVER,
9706 "__builtin_setjmp_receiver", ECF_NOTHROW);
9708 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9709 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
9710 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9712 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9713 local_define_builtin ("__builtin_stack_restore", ftype,
9714 BUILT_IN_STACK_RESTORE,
9715 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9717 /* If there's a possibility that we might use the ARM EABI, build the
9718 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
9719 if (targetm.arm_eabi_unwinder)
9721 ftype = build_function_type_list (void_type_node, NULL_TREE);
9722 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
9723 BUILT_IN_CXA_END_CLEANUP,
9724 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
9727 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9728 local_define_builtin ("__builtin_unwind_resume", ftype,
9729 BUILT_IN_UNWIND_RESUME,
9730 ((targetm_common.except_unwind_info (&global_options)
9731 == UI_SJLJ)
9732 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
9733 ECF_NORETURN);
9735 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
9737 ftype = build_function_type_list (ptr_type_node, integer_type_node,
9738 NULL_TREE);
9739 local_define_builtin ("__builtin_return_address", ftype,
9740 BUILT_IN_RETURN_ADDRESS,
9741 "__builtin_return_address",
9742 ECF_NOTHROW);
9745 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
9746 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9748 ftype = build_function_type_list (void_type_node, ptr_type_node,
9749 ptr_type_node, NULL_TREE);
9750 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
9751 local_define_builtin ("__cyg_profile_func_enter", ftype,
9752 BUILT_IN_PROFILE_FUNC_ENTER,
9753 "__cyg_profile_func_enter", 0);
9754 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9755 local_define_builtin ("__cyg_profile_func_exit", ftype,
9756 BUILT_IN_PROFILE_FUNC_EXIT,
9757 "__cyg_profile_func_exit", 0);
9760 /* The exception object and filter values from the runtime. The argument
9761 must be zero before exception lowering, i.e. from the front end. After
9762 exception lowering, it will be the region number for the exception
9763 landing pad. These functions are PURE instead of CONST to prevent
9764 them from being hoisted past the exception edge that will initialize
9765 its value in the landing pad. */
9766 ftype = build_function_type_list (ptr_type_node,
9767 integer_type_node, NULL_TREE);
9768 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
9769 /* Only use TM_PURE if we we have TM language support. */
9770 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
9771 ecf_flags |= ECF_TM_PURE;
9772 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
9773 "__builtin_eh_pointer", ecf_flags);
9775 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
9776 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
9777 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
9778 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9780 ftype = build_function_type_list (void_type_node,
9781 integer_type_node, integer_type_node,
9782 NULL_TREE);
9783 local_define_builtin ("__builtin_eh_copy_values", ftype,
9784 BUILT_IN_EH_COPY_VALUES,
9785 "__builtin_eh_copy_values", ECF_NOTHROW);
9787 /* Complex multiplication and division. These are handled as builtins
9788 rather than optabs because emit_library_call_value doesn't support
9789 complex. Further, we can do slightly better with folding these
9790 beasties if the real and complex parts of the arguments are separate. */
9792 int mode;
9794 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
9796 char mode_name_buf[4], *q;
9797 const char *p;
9798 enum built_in_function mcode, dcode;
9799 tree type, inner_type;
9800 const char *prefix = "__";
9802 if (targetm.libfunc_gnu_prefix)
9803 prefix = "__gnu_";
9805 type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
9806 if (type == NULL)
9807 continue;
9808 inner_type = TREE_TYPE (type);
9810 ftype = build_function_type_list (type, inner_type, inner_type,
9811 inner_type, inner_type, NULL_TREE);
9813 mcode = ((enum built_in_function)
9814 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9815 dcode = ((enum built_in_function)
9816 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9818 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
9819 *q = TOLOWER (*p);
9820 *q = '\0';
9822 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
9823 NULL);
9824 local_define_builtin (built_in_names[mcode], ftype, mcode,
9825 built_in_names[mcode],
9826 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
9828 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
9829 NULL);
9830 local_define_builtin (built_in_names[dcode], ftype, dcode,
9831 built_in_names[dcode],
9832 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
9837 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
9838 better way.
9840 If we requested a pointer to a vector, build up the pointers that
9841 we stripped off while looking for the inner type. Similarly for
9842 return values from functions.
9844 The argument TYPE is the top of the chain, and BOTTOM is the
9845 new type which we will point to. */
9847 tree
9848 reconstruct_complex_type (tree type, tree bottom)
9850 tree inner, outer;
9852 if (TREE_CODE (type) == POINTER_TYPE)
9854 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9855 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
9856 TYPE_REF_CAN_ALIAS_ALL (type));
9858 else if (TREE_CODE (type) == REFERENCE_TYPE)
9860 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9861 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
9862 TYPE_REF_CAN_ALIAS_ALL (type));
9864 else if (TREE_CODE (type) == ARRAY_TYPE)
9866 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9867 outer = build_array_type (inner, TYPE_DOMAIN (type));
9869 else if (TREE_CODE (type) == FUNCTION_TYPE)
9871 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9872 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
9874 else if (TREE_CODE (type) == METHOD_TYPE)
9876 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9877 /* The build_method_type_directly() routine prepends 'this' to argument list,
9878 so we must compensate by getting rid of it. */
9879 outer
9880 = build_method_type_directly
9881 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
9882 inner,
9883 TREE_CHAIN (TYPE_ARG_TYPES (type)));
9885 else if (TREE_CODE (type) == OFFSET_TYPE)
9887 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9888 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
9890 else
9891 return bottom;
9893 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
9894 TYPE_QUALS (type));
9897 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
9898 the inner type. */
9899 tree
9900 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
9902 int nunits;
9904 switch (GET_MODE_CLASS (mode))
9906 case MODE_VECTOR_INT:
9907 case MODE_VECTOR_FLOAT:
9908 case MODE_VECTOR_FRACT:
9909 case MODE_VECTOR_UFRACT:
9910 case MODE_VECTOR_ACCUM:
9911 case MODE_VECTOR_UACCUM:
9912 nunits = GET_MODE_NUNITS (mode);
9913 break;
9915 case MODE_INT:
9916 /* Check that there are no leftover bits. */
9917 gcc_assert (GET_MODE_BITSIZE (mode)
9918 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
9920 nunits = GET_MODE_BITSIZE (mode)
9921 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
9922 break;
9924 default:
9925 gcc_unreachable ();
9928 return make_vector_type (innertype, nunits, mode);
9931 /* Similarly, but takes the inner type and number of units, which must be
9932 a power of two. */
9934 tree
9935 build_vector_type (tree innertype, int nunits)
9937 return make_vector_type (innertype, nunits, VOIDmode);
9940 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
9942 tree
9943 build_opaque_vector_type (tree innertype, int nunits)
9945 tree t = make_vector_type (innertype, nunits, VOIDmode);
9946 tree cand;
9947 /* We always build the non-opaque variant before the opaque one,
9948 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
9949 cand = TYPE_NEXT_VARIANT (t);
9950 if (cand
9951 && TYPE_VECTOR_OPAQUE (cand)
9952 && check_qualified_type (cand, t, TYPE_QUALS (t)))
9953 return cand;
9954 /* Othewise build a variant type and make sure to queue it after
9955 the non-opaque type. */
9956 cand = build_distinct_type_copy (t);
9957 TYPE_VECTOR_OPAQUE (cand) = true;
9958 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
9959 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
9960 TYPE_NEXT_VARIANT (t) = cand;
9961 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
9962 return cand;
9966 /* Given an initializer INIT, return TRUE if INIT is zero or some
9967 aggregate of zeros. Otherwise return FALSE. */
9968 bool
9969 initializer_zerop (const_tree init)
9971 tree elt;
9973 STRIP_NOPS (init);
9975 switch (TREE_CODE (init))
9977 case INTEGER_CST:
9978 return integer_zerop (init);
9980 case REAL_CST:
9981 /* ??? Note that this is not correct for C4X float formats. There,
9982 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
9983 negative exponent. */
9984 return real_zerop (init)
9985 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
9987 case FIXED_CST:
9988 return fixed_zerop (init);
9990 case COMPLEX_CST:
9991 return integer_zerop (init)
9992 || (real_zerop (init)
9993 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
9994 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
9996 case VECTOR_CST:
9998 unsigned i;
9999 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10000 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10001 return false;
10002 return true;
10005 case CONSTRUCTOR:
10007 unsigned HOST_WIDE_INT idx;
10009 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10010 if (!initializer_zerop (elt))
10011 return false;
10012 return true;
10015 case STRING_CST:
10017 int i;
10019 /* We need to loop through all elements to handle cases like
10020 "\0" and "\0foobar". */
10021 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10022 if (TREE_STRING_POINTER (init)[i] != '\0')
10023 return false;
10025 return true;
10028 default:
10029 return false;
10033 /* Build an empty statement at location LOC. */
10035 tree
10036 build_empty_stmt (location_t loc)
10038 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10039 SET_EXPR_LOCATION (t, loc);
10040 return t;
10044 /* Build an OpenMP clause with code CODE. LOC is the location of the
10045 clause. */
10047 tree
10048 build_omp_clause (location_t loc, enum omp_clause_code code)
10050 tree t;
10051 int size, length;
10053 length = omp_clause_num_ops[code];
10054 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10056 record_node_allocation_statistics (OMP_CLAUSE, size);
10058 t = ggc_alloc_tree_node (size);
10059 memset (t, 0, size);
10060 TREE_SET_CODE (t, OMP_CLAUSE);
10061 OMP_CLAUSE_SET_CODE (t, code);
10062 OMP_CLAUSE_LOCATION (t) = loc;
10064 return t;
10067 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10068 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10069 Except for the CODE and operand count field, other storage for the
10070 object is initialized to zeros. */
10072 tree
10073 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10075 tree t;
10076 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10078 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10079 gcc_assert (len >= 1);
10081 record_node_allocation_statistics (code, length);
10083 t = ggc_alloc_zone_cleared_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
10085 TREE_SET_CODE (t, code);
10087 /* Can't use TREE_OPERAND to store the length because if checking is
10088 enabled, it will try to check the length before we store it. :-P */
10089 t->exp.operands[0] = build_int_cst (sizetype, len);
10091 return t;
10094 /* Helper function for build_call_* functions; build a CALL_EXPR with
10095 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10096 the argument slots. */
10098 static tree
10099 build_call_1 (tree return_type, tree fn, int nargs)
10101 tree t;
10103 t = build_vl_exp (CALL_EXPR, nargs + 3);
10104 TREE_TYPE (t) = return_type;
10105 CALL_EXPR_FN (t) = fn;
10106 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10108 return t;
10111 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10112 FN and a null static chain slot. NARGS is the number of call arguments
10113 which are specified as "..." arguments. */
10115 tree
10116 build_call_nary (tree return_type, tree fn, int nargs, ...)
10118 tree ret;
10119 va_list args;
10120 va_start (args, nargs);
10121 ret = build_call_valist (return_type, fn, nargs, args);
10122 va_end (args);
10123 return ret;
10126 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10127 FN and a null static chain slot. NARGS is the number of call arguments
10128 which are specified as a va_list ARGS. */
10130 tree
10131 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10133 tree t;
10134 int i;
10136 t = build_call_1 (return_type, fn, nargs);
10137 for (i = 0; i < nargs; i++)
10138 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10139 process_call_operands (t);
10140 return t;
10143 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10144 FN and a null static chain slot. NARGS is the number of call arguments
10145 which are specified as a tree array ARGS. */
10147 tree
10148 build_call_array_loc (location_t loc, tree return_type, tree fn,
10149 int nargs, const tree *args)
10151 tree t;
10152 int i;
10154 t = build_call_1 (return_type, fn, nargs);
10155 for (i = 0; i < nargs; i++)
10156 CALL_EXPR_ARG (t, i) = args[i];
10157 process_call_operands (t);
10158 SET_EXPR_LOCATION (t, loc);
10159 return t;
10162 /* Like build_call_array, but takes a vec. */
10164 tree
10165 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10167 tree ret, t;
10168 unsigned int ix;
10170 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10171 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10172 CALL_EXPR_ARG (ret, ix) = t;
10173 process_call_operands (ret);
10174 return ret;
10178 /* Returns true if it is possible to prove that the index of
10179 an array access REF (an ARRAY_REF expression) falls into the
10180 array bounds. */
10182 bool
10183 in_array_bounds_p (tree ref)
10185 tree idx = TREE_OPERAND (ref, 1);
10186 tree min, max;
10188 if (TREE_CODE (idx) != INTEGER_CST)
10189 return false;
10191 min = array_ref_low_bound (ref);
10192 max = array_ref_up_bound (ref);
10193 if (!min
10194 || !max
10195 || TREE_CODE (min) != INTEGER_CST
10196 || TREE_CODE (max) != INTEGER_CST)
10197 return false;
10199 if (tree_int_cst_lt (idx, min)
10200 || tree_int_cst_lt (max, idx))
10201 return false;
10203 return true;
10206 /* Returns true if it is possible to prove that the range of
10207 an array access REF (an ARRAY_RANGE_REF expression) falls
10208 into the array bounds. */
10210 bool
10211 range_in_array_bounds_p (tree ref)
10213 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
10214 tree range_min, range_max, min, max;
10216 range_min = TYPE_MIN_VALUE (domain_type);
10217 range_max = TYPE_MAX_VALUE (domain_type);
10218 if (!range_min
10219 || !range_max
10220 || TREE_CODE (range_min) != INTEGER_CST
10221 || TREE_CODE (range_max) != INTEGER_CST)
10222 return false;
10224 min = array_ref_low_bound (ref);
10225 max = array_ref_up_bound (ref);
10226 if (!min
10227 || !max
10228 || TREE_CODE (min) != INTEGER_CST
10229 || TREE_CODE (max) != INTEGER_CST)
10230 return false;
10232 if (tree_int_cst_lt (range_min, min)
10233 || tree_int_cst_lt (max, range_max))
10234 return false;
10236 return true;
10239 /* Return true if T (assumed to be a DECL) must be assigned a memory
10240 location. */
10242 bool
10243 needs_to_live_in_memory (const_tree t)
10245 return (TREE_ADDRESSABLE (t)
10246 || is_global_var (t)
10247 || (TREE_CODE (t) == RESULT_DECL
10248 && !DECL_BY_REFERENCE (t)
10249 && aggregate_value_p (t, current_function_decl)));
10252 /* Return value of a constant X and sign-extend it. */
10254 HOST_WIDE_INT
10255 int_cst_value (const_tree x)
10257 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10258 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10260 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10261 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10262 || TREE_INT_CST_HIGH (x) == -1);
10264 if (bits < HOST_BITS_PER_WIDE_INT)
10266 bool negative = ((val >> (bits - 1)) & 1) != 0;
10267 if (negative)
10268 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10269 else
10270 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10273 return val;
10276 /* Return value of a constant X and sign-extend it. */
10278 HOST_WIDEST_INT
10279 widest_int_cst_value (const_tree x)
10281 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10282 unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
10284 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
10285 gcc_assert (HOST_BITS_PER_WIDEST_INT >= HOST_BITS_PER_DOUBLE_INT);
10286 val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_HIGH (x))
10287 << HOST_BITS_PER_WIDE_INT);
10288 #else
10289 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10290 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10291 || TREE_INT_CST_HIGH (x) == -1);
10292 #endif
10294 if (bits < HOST_BITS_PER_WIDEST_INT)
10296 bool negative = ((val >> (bits - 1)) & 1) != 0;
10297 if (negative)
10298 val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
10299 else
10300 val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
10303 return val;
10306 /* If TYPE is an integral or pointer type, return an integer type with
10307 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10308 if TYPE is already an integer type of signedness UNSIGNEDP. */
10310 tree
10311 signed_or_unsigned_type_for (int unsignedp, tree type)
10313 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10314 return type;
10316 if (TREE_CODE (type) == VECTOR_TYPE)
10318 tree inner = TREE_TYPE (type);
10319 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10320 if (!inner2)
10321 return NULL_TREE;
10322 if (inner == inner2)
10323 return type;
10324 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10327 if (!INTEGRAL_TYPE_P (type)
10328 && !POINTER_TYPE_P (type))
10329 return NULL_TREE;
10331 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10334 /* If TYPE is an integral or pointer type, return an integer type with
10335 the same precision which is unsigned, or itself if TYPE is already an
10336 unsigned integer type. */
10338 tree
10339 unsigned_type_for (tree type)
10341 return signed_or_unsigned_type_for (1, type);
10344 /* If TYPE is an integral or pointer type, return an integer type with
10345 the same precision which is signed, or itself if TYPE is already a
10346 signed integer type. */
10348 tree
10349 signed_type_for (tree type)
10351 return signed_or_unsigned_type_for (0, type);
10354 /* If TYPE is a vector type, return a signed integer vector type with the
10355 same width and number of subparts. Otherwise return boolean_type_node. */
10357 tree
10358 truth_type_for (tree type)
10360 if (TREE_CODE (type) == VECTOR_TYPE)
10362 tree elem = lang_hooks.types.type_for_size
10363 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10364 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10366 else
10367 return boolean_type_node;
10370 /* Returns the largest value obtainable by casting something in INNER type to
10371 OUTER type. */
10373 tree
10374 upper_bound_in_type (tree outer, tree inner)
10376 double_int high;
10377 unsigned int det = 0;
10378 unsigned oprec = TYPE_PRECISION (outer);
10379 unsigned iprec = TYPE_PRECISION (inner);
10380 unsigned prec;
10382 /* Compute a unique number for every combination. */
10383 det |= (oprec > iprec) ? 4 : 0;
10384 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10385 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10387 /* Determine the exponent to use. */
10388 switch (det)
10390 case 0:
10391 case 1:
10392 /* oprec <= iprec, outer: signed, inner: don't care. */
10393 prec = oprec - 1;
10394 break;
10395 case 2:
10396 case 3:
10397 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10398 prec = oprec;
10399 break;
10400 case 4:
10401 /* oprec > iprec, outer: signed, inner: signed. */
10402 prec = iprec - 1;
10403 break;
10404 case 5:
10405 /* oprec > iprec, outer: signed, inner: unsigned. */
10406 prec = iprec;
10407 break;
10408 case 6:
10409 /* oprec > iprec, outer: unsigned, inner: signed. */
10410 prec = oprec;
10411 break;
10412 case 7:
10413 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10414 prec = iprec;
10415 break;
10416 default:
10417 gcc_unreachable ();
10420 /* Compute 2^^prec - 1. */
10421 if (prec <= HOST_BITS_PER_WIDE_INT)
10423 high.high = 0;
10424 high.low = ((~(unsigned HOST_WIDE_INT) 0)
10425 >> (HOST_BITS_PER_WIDE_INT - prec));
10427 else
10429 high.high = ((~(unsigned HOST_WIDE_INT) 0)
10430 >> (HOST_BITS_PER_DOUBLE_INT - prec));
10431 high.low = ~(unsigned HOST_WIDE_INT) 0;
10434 return double_int_to_tree (outer, high);
10437 /* Returns the smallest value obtainable by casting something in INNER type to
10438 OUTER type. */
10440 tree
10441 lower_bound_in_type (tree outer, tree inner)
10443 double_int low;
10444 unsigned oprec = TYPE_PRECISION (outer);
10445 unsigned iprec = TYPE_PRECISION (inner);
10447 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10448 and obtain 0. */
10449 if (TYPE_UNSIGNED (outer)
10450 /* If we are widening something of an unsigned type, OUTER type
10451 contains all values of INNER type. In particular, both INNER
10452 and OUTER types have zero in common. */
10453 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10454 low.low = low.high = 0;
10455 else
10457 /* If we are widening a signed type to another signed type, we
10458 want to obtain -2^^(iprec-1). If we are keeping the
10459 precision or narrowing to a signed type, we want to obtain
10460 -2^(oprec-1). */
10461 unsigned prec = oprec > iprec ? iprec : oprec;
10463 if (prec <= HOST_BITS_PER_WIDE_INT)
10465 low.high = ~(unsigned HOST_WIDE_INT) 0;
10466 low.low = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
10468 else
10470 low.high = ((~(unsigned HOST_WIDE_INT) 0)
10471 << (prec - HOST_BITS_PER_WIDE_INT - 1));
10472 low.low = 0;
10476 return double_int_to_tree (outer, low);
10479 /* Return nonzero if two operands that are suitable for PHI nodes are
10480 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10481 SSA_NAME or invariant. Note that this is strictly an optimization.
10482 That is, callers of this function can directly call operand_equal_p
10483 and get the same result, only slower. */
10486 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10488 if (arg0 == arg1)
10489 return 1;
10490 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10491 return 0;
10492 return operand_equal_p (arg0, arg1, 0);
10495 /* Returns number of zeros at the end of binary representation of X.
10497 ??? Use ffs if available? */
10499 tree
10500 num_ending_zeros (const_tree x)
10502 unsigned HOST_WIDE_INT fr, nfr;
10503 unsigned num, abits;
10504 tree type = TREE_TYPE (x);
10506 if (TREE_INT_CST_LOW (x) == 0)
10508 num = HOST_BITS_PER_WIDE_INT;
10509 fr = TREE_INT_CST_HIGH (x);
10511 else
10513 num = 0;
10514 fr = TREE_INT_CST_LOW (x);
10517 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
10519 nfr = fr >> abits;
10520 if (nfr << abits == fr)
10522 num += abits;
10523 fr = nfr;
10527 if (num > TYPE_PRECISION (type))
10528 num = TYPE_PRECISION (type);
10530 return build_int_cst_type (type, num);
10534 #define WALK_SUBTREE(NODE) \
10535 do \
10537 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10538 if (result) \
10539 return result; \
10541 while (0)
10543 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10544 be walked whenever a type is seen in the tree. Rest of operands and return
10545 value are as for walk_tree. */
10547 static tree
10548 walk_type_fields (tree type, walk_tree_fn func, void *data,
10549 struct pointer_set_t *pset, walk_tree_lh lh)
10551 tree result = NULL_TREE;
10553 switch (TREE_CODE (type))
10555 case POINTER_TYPE:
10556 case REFERENCE_TYPE:
10557 /* We have to worry about mutually recursive pointers. These can't
10558 be written in C. They can in Ada. It's pathological, but
10559 there's an ACATS test (c38102a) that checks it. Deal with this
10560 by checking if we're pointing to another pointer, that one
10561 points to another pointer, that one does too, and we have no htab.
10562 If so, get a hash table. We check three levels deep to avoid
10563 the cost of the hash table if we don't need one. */
10564 if (POINTER_TYPE_P (TREE_TYPE (type))
10565 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10566 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10567 && !pset)
10569 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10570 func, data);
10571 if (result)
10572 return result;
10574 break;
10577 /* ... fall through ... */
10579 case COMPLEX_TYPE:
10580 WALK_SUBTREE (TREE_TYPE (type));
10581 break;
10583 case METHOD_TYPE:
10584 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10586 /* Fall through. */
10588 case FUNCTION_TYPE:
10589 WALK_SUBTREE (TREE_TYPE (type));
10591 tree arg;
10593 /* We never want to walk into default arguments. */
10594 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10595 WALK_SUBTREE (TREE_VALUE (arg));
10597 break;
10599 case ARRAY_TYPE:
10600 /* Don't follow this nodes's type if a pointer for fear that
10601 we'll have infinite recursion. If we have a PSET, then we
10602 need not fear. */
10603 if (pset
10604 || (!POINTER_TYPE_P (TREE_TYPE (type))
10605 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10606 WALK_SUBTREE (TREE_TYPE (type));
10607 WALK_SUBTREE (TYPE_DOMAIN (type));
10608 break;
10610 case OFFSET_TYPE:
10611 WALK_SUBTREE (TREE_TYPE (type));
10612 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
10613 break;
10615 default:
10616 break;
10619 return NULL_TREE;
10622 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
10623 called with the DATA and the address of each sub-tree. If FUNC returns a
10624 non-NULL value, the traversal is stopped, and the value returned by FUNC
10625 is returned. If PSET is non-NULL it is used to record the nodes visited,
10626 and to avoid visiting a node more than once. */
10628 tree
10629 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
10630 struct pointer_set_t *pset, walk_tree_lh lh)
10632 enum tree_code code;
10633 int walk_subtrees;
10634 tree result;
10636 #define WALK_SUBTREE_TAIL(NODE) \
10637 do \
10639 tp = & (NODE); \
10640 goto tail_recurse; \
10642 while (0)
10644 tail_recurse:
10645 /* Skip empty subtrees. */
10646 if (!*tp)
10647 return NULL_TREE;
10649 /* Don't walk the same tree twice, if the user has requested
10650 that we avoid doing so. */
10651 if (pset && pointer_set_insert (pset, *tp))
10652 return NULL_TREE;
10654 /* Call the function. */
10655 walk_subtrees = 1;
10656 result = (*func) (tp, &walk_subtrees, data);
10658 /* If we found something, return it. */
10659 if (result)
10660 return result;
10662 code = TREE_CODE (*tp);
10664 /* Even if we didn't, FUNC may have decided that there was nothing
10665 interesting below this point in the tree. */
10666 if (!walk_subtrees)
10668 /* But we still need to check our siblings. */
10669 if (code == TREE_LIST)
10670 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10671 else if (code == OMP_CLAUSE)
10672 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10673 else
10674 return NULL_TREE;
10677 if (lh)
10679 result = (*lh) (tp, &walk_subtrees, func, data, pset);
10680 if (result || !walk_subtrees)
10681 return result;
10684 switch (code)
10686 case ERROR_MARK:
10687 case IDENTIFIER_NODE:
10688 case INTEGER_CST:
10689 case REAL_CST:
10690 case FIXED_CST:
10691 case VECTOR_CST:
10692 case STRING_CST:
10693 case BLOCK:
10694 case PLACEHOLDER_EXPR:
10695 case SSA_NAME:
10696 case FIELD_DECL:
10697 case RESULT_DECL:
10698 /* None of these have subtrees other than those already walked
10699 above. */
10700 break;
10702 case TREE_LIST:
10703 WALK_SUBTREE (TREE_VALUE (*tp));
10704 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10705 break;
10707 case TREE_VEC:
10709 int len = TREE_VEC_LENGTH (*tp);
10711 if (len == 0)
10712 break;
10714 /* Walk all elements but the first. */
10715 while (--len)
10716 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
10718 /* Now walk the first one as a tail call. */
10719 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
10722 case COMPLEX_CST:
10723 WALK_SUBTREE (TREE_REALPART (*tp));
10724 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
10726 case CONSTRUCTOR:
10728 unsigned HOST_WIDE_INT idx;
10729 constructor_elt *ce;
10731 for (idx = 0; vec_safe_iterate(CONSTRUCTOR_ELTS (*tp), idx, &ce); idx++)
10732 WALK_SUBTREE (ce->value);
10734 break;
10736 case SAVE_EXPR:
10737 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
10739 case BIND_EXPR:
10741 tree decl;
10742 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
10744 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
10745 into declarations that are just mentioned, rather than
10746 declared; they don't really belong to this part of the tree.
10747 And, we can see cycles: the initializer for a declaration
10748 can refer to the declaration itself. */
10749 WALK_SUBTREE (DECL_INITIAL (decl));
10750 WALK_SUBTREE (DECL_SIZE (decl));
10751 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
10753 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
10756 case STATEMENT_LIST:
10758 tree_stmt_iterator i;
10759 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
10760 WALK_SUBTREE (*tsi_stmt_ptr (i));
10762 break;
10764 case OMP_CLAUSE:
10765 switch (OMP_CLAUSE_CODE (*tp))
10767 case OMP_CLAUSE_PRIVATE:
10768 case OMP_CLAUSE_SHARED:
10769 case OMP_CLAUSE_FIRSTPRIVATE:
10770 case OMP_CLAUSE_COPYIN:
10771 case OMP_CLAUSE_COPYPRIVATE:
10772 case OMP_CLAUSE_FINAL:
10773 case OMP_CLAUSE_IF:
10774 case OMP_CLAUSE_NUM_THREADS:
10775 case OMP_CLAUSE_SCHEDULE:
10776 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
10777 /* FALLTHRU */
10779 case OMP_CLAUSE_NOWAIT:
10780 case OMP_CLAUSE_ORDERED:
10781 case OMP_CLAUSE_DEFAULT:
10782 case OMP_CLAUSE_UNTIED:
10783 case OMP_CLAUSE_MERGEABLE:
10784 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10786 case OMP_CLAUSE_LASTPRIVATE:
10787 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
10788 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
10789 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10791 case OMP_CLAUSE_COLLAPSE:
10793 int i;
10794 for (i = 0; i < 3; i++)
10795 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10796 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10799 case OMP_CLAUSE_REDUCTION:
10801 int i;
10802 for (i = 0; i < 4; i++)
10803 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10804 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10807 default:
10808 gcc_unreachable ();
10810 break;
10812 case TARGET_EXPR:
10814 int i, len;
10816 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
10817 But, we only want to walk once. */
10818 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
10819 for (i = 0; i < len; ++i)
10820 WALK_SUBTREE (TREE_OPERAND (*tp, i));
10821 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
10824 case DECL_EXPR:
10825 /* If this is a TYPE_DECL, walk into the fields of the type that it's
10826 defining. We only want to walk into these fields of a type in this
10827 case and not in the general case of a mere reference to the type.
10829 The criterion is as follows: if the field can be an expression, it
10830 must be walked only here. This should be in keeping with the fields
10831 that are directly gimplified in gimplify_type_sizes in order for the
10832 mark/copy-if-shared/unmark machinery of the gimplifier to work with
10833 variable-sized types.
10835 Note that DECLs get walked as part of processing the BIND_EXPR. */
10836 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
10838 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
10839 if (TREE_CODE (*type_p) == ERROR_MARK)
10840 return NULL_TREE;
10842 /* Call the function for the type. See if it returns anything or
10843 doesn't want us to continue. If we are to continue, walk both
10844 the normal fields and those for the declaration case. */
10845 result = (*func) (type_p, &walk_subtrees, data);
10846 if (result || !walk_subtrees)
10847 return result;
10849 /* But do not walk a pointed-to type since it may itself need to
10850 be walked in the declaration case if it isn't anonymous. */
10851 if (!POINTER_TYPE_P (*type_p))
10853 result = walk_type_fields (*type_p, func, data, pset, lh);
10854 if (result)
10855 return result;
10858 /* If this is a record type, also walk the fields. */
10859 if (RECORD_OR_UNION_TYPE_P (*type_p))
10861 tree field;
10863 for (field = TYPE_FIELDS (*type_p); field;
10864 field = DECL_CHAIN (field))
10866 /* We'd like to look at the type of the field, but we can
10867 easily get infinite recursion. So assume it's pointed
10868 to elsewhere in the tree. Also, ignore things that
10869 aren't fields. */
10870 if (TREE_CODE (field) != FIELD_DECL)
10871 continue;
10873 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
10874 WALK_SUBTREE (DECL_SIZE (field));
10875 WALK_SUBTREE (DECL_SIZE_UNIT (field));
10876 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
10877 WALK_SUBTREE (DECL_QUALIFIER (field));
10881 /* Same for scalar types. */
10882 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
10883 || TREE_CODE (*type_p) == ENUMERAL_TYPE
10884 || TREE_CODE (*type_p) == INTEGER_TYPE
10885 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
10886 || TREE_CODE (*type_p) == REAL_TYPE)
10888 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
10889 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
10892 WALK_SUBTREE (TYPE_SIZE (*type_p));
10893 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
10895 /* FALLTHRU */
10897 default:
10898 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
10900 int i, len;
10902 /* Walk over all the sub-trees of this operand. */
10903 len = TREE_OPERAND_LENGTH (*tp);
10905 /* Go through the subtrees. We need to do this in forward order so
10906 that the scope of a FOR_EXPR is handled properly. */
10907 if (len)
10909 for (i = 0; i < len - 1; ++i)
10910 WALK_SUBTREE (TREE_OPERAND (*tp, i));
10911 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
10914 /* If this is a type, walk the needed fields in the type. */
10915 else if (TYPE_P (*tp))
10916 return walk_type_fields (*tp, func, data, pset, lh);
10917 break;
10920 /* We didn't find what we were looking for. */
10921 return NULL_TREE;
10923 #undef WALK_SUBTREE_TAIL
10925 #undef WALK_SUBTREE
10927 /* Like walk_tree, but does not walk duplicate nodes more than once. */
10929 tree
10930 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
10931 walk_tree_lh lh)
10933 tree result;
10934 struct pointer_set_t *pset;
10936 pset = pointer_set_create ();
10937 result = walk_tree_1 (tp, func, data, pset, lh);
10938 pointer_set_destroy (pset);
10939 return result;
10943 tree
10944 tree_block (tree t)
10946 char const c = TREE_CODE_CLASS (TREE_CODE (t));
10948 if (IS_EXPR_CODE_CLASS (c))
10949 return LOCATION_BLOCK (t->exp.locus);
10950 gcc_unreachable ();
10951 return NULL;
10954 void
10955 tree_set_block (tree t, tree b)
10957 char const c = TREE_CODE_CLASS (TREE_CODE (t));
10959 if (IS_EXPR_CODE_CLASS (c))
10961 if (b)
10962 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
10963 else
10964 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
10966 else
10967 gcc_unreachable ();
10970 /* Create a nameless artificial label and put it in the current
10971 function context. The label has a location of LOC. Returns the
10972 newly created label. */
10974 tree
10975 create_artificial_label (location_t loc)
10977 tree lab = build_decl (loc,
10978 LABEL_DECL, NULL_TREE, void_type_node);
10980 DECL_ARTIFICIAL (lab) = 1;
10981 DECL_IGNORED_P (lab) = 1;
10982 DECL_CONTEXT (lab) = current_function_decl;
10983 return lab;
10986 /* Given a tree, try to return a useful variable name that we can use
10987 to prefix a temporary that is being assigned the value of the tree.
10988 I.E. given <temp> = &A, return A. */
10990 const char *
10991 get_name (tree t)
10993 tree stripped_decl;
10995 stripped_decl = t;
10996 STRIP_NOPS (stripped_decl);
10997 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
10998 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
10999 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11001 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11002 if (!name)
11003 return NULL;
11004 return IDENTIFIER_POINTER (name);
11006 else
11008 switch (TREE_CODE (stripped_decl))
11010 case ADDR_EXPR:
11011 return get_name (TREE_OPERAND (stripped_decl, 0));
11012 default:
11013 return NULL;
11018 /* Return true if TYPE has a variable argument list. */
11020 bool
11021 stdarg_p (const_tree fntype)
11023 function_args_iterator args_iter;
11024 tree n = NULL_TREE, t;
11026 if (!fntype)
11027 return false;
11029 FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
11031 n = t;
11034 return n != NULL_TREE && n != void_type_node;
11037 /* Return true if TYPE has a prototype. */
11039 bool
11040 prototype_p (tree fntype)
11042 tree t;
11044 gcc_assert (fntype != NULL_TREE);
11046 t = TYPE_ARG_TYPES (fntype);
11047 return (t != NULL_TREE);
11050 /* If BLOCK is inlined from an __attribute__((__artificial__))
11051 routine, return pointer to location from where it has been
11052 called. */
11053 location_t *
11054 block_nonartificial_location (tree block)
11056 location_t *ret = NULL;
11058 while (block && TREE_CODE (block) == BLOCK
11059 && BLOCK_ABSTRACT_ORIGIN (block))
11061 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11063 while (TREE_CODE (ao) == BLOCK
11064 && BLOCK_ABSTRACT_ORIGIN (ao)
11065 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11066 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11068 if (TREE_CODE (ao) == FUNCTION_DECL)
11070 /* If AO is an artificial inline, point RET to the
11071 call site locus at which it has been inlined and continue
11072 the loop, in case AO's caller is also an artificial
11073 inline. */
11074 if (DECL_DECLARED_INLINE_P (ao)
11075 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11076 ret = &BLOCK_SOURCE_LOCATION (block);
11077 else
11078 break;
11080 else if (TREE_CODE (ao) != BLOCK)
11081 break;
11083 block = BLOCK_SUPERCONTEXT (block);
11085 return ret;
11089 /* If EXP is inlined from an __attribute__((__artificial__))
11090 function, return the location of the original call expression. */
11092 location_t
11093 tree_nonartificial_location (tree exp)
11095 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11097 if (loc)
11098 return *loc;
11099 else
11100 return EXPR_LOCATION (exp);
11104 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11105 nodes. */
11107 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11109 static hashval_t
11110 cl_option_hash_hash (const void *x)
11112 const_tree const t = (const_tree) x;
11113 const char *p;
11114 size_t i;
11115 size_t len = 0;
11116 hashval_t hash = 0;
11118 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11120 p = (const char *)TREE_OPTIMIZATION (t);
11121 len = sizeof (struct cl_optimization);
11124 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11126 p = (const char *)TREE_TARGET_OPTION (t);
11127 len = sizeof (struct cl_target_option);
11130 else
11131 gcc_unreachable ();
11133 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11134 something else. */
11135 for (i = 0; i < len; i++)
11136 if (p[i])
11137 hash = (hash << 4) ^ ((i << 2) | p[i]);
11139 return hash;
11142 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11143 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11144 same. */
11146 static int
11147 cl_option_hash_eq (const void *x, const void *y)
11149 const_tree const xt = (const_tree) x;
11150 const_tree const yt = (const_tree) y;
11151 const char *xp;
11152 const char *yp;
11153 size_t len;
11155 if (TREE_CODE (xt) != TREE_CODE (yt))
11156 return 0;
11158 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11160 xp = (const char *)TREE_OPTIMIZATION (xt);
11161 yp = (const char *)TREE_OPTIMIZATION (yt);
11162 len = sizeof (struct cl_optimization);
11165 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11167 xp = (const char *)TREE_TARGET_OPTION (xt);
11168 yp = (const char *)TREE_TARGET_OPTION (yt);
11169 len = sizeof (struct cl_target_option);
11172 else
11173 gcc_unreachable ();
11175 return (memcmp (xp, yp, len) == 0);
11178 /* Build an OPTIMIZATION_NODE based on the current options. */
11180 tree
11181 build_optimization_node (void)
11183 tree t;
11184 void **slot;
11186 /* Use the cache of optimization nodes. */
11188 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11189 &global_options);
11191 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
11192 t = (tree) *slot;
11193 if (!t)
11195 /* Insert this one into the hash table. */
11196 t = cl_optimization_node;
11197 *slot = t;
11199 /* Make a new node for next time round. */
11200 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11203 return t;
11206 /* Build a TARGET_OPTION_NODE based on the current options. */
11208 tree
11209 build_target_option_node (void)
11211 tree t;
11212 void **slot;
11214 /* Use the cache of optimization nodes. */
11216 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11217 &global_options);
11219 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
11220 t = (tree) *slot;
11221 if (!t)
11223 /* Insert this one into the hash table. */
11224 t = cl_target_option_node;
11225 *slot = t;
11227 /* Make a new node for next time round. */
11228 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11231 return t;
11234 /* Determine the "ultimate origin" of a block. The block may be an inlined
11235 instance of an inlined instance of a block which is local to an inline
11236 function, so we have to trace all of the way back through the origin chain
11237 to find out what sort of node actually served as the original seed for the
11238 given block. */
11240 tree
11241 block_ultimate_origin (const_tree block)
11243 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11245 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
11246 nodes in the function to point to themselves; ignore that if
11247 we're trying to output the abstract instance of this function. */
11248 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11249 return NULL_TREE;
11251 if (immediate_origin == NULL_TREE)
11252 return NULL_TREE;
11253 else
11255 tree ret_val;
11256 tree lookahead = immediate_origin;
11260 ret_val = lookahead;
11261 lookahead = (TREE_CODE (ret_val) == BLOCK
11262 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11264 while (lookahead != NULL && lookahead != ret_val);
11266 /* The block's abstract origin chain may not be the *ultimate* origin of
11267 the block. It could lead to a DECL that has an abstract origin set.
11268 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11269 will give us if it has one). Note that DECL's abstract origins are
11270 supposed to be the most distant ancestor (or so decl_ultimate_origin
11271 claims), so we don't need to loop following the DECL origins. */
11272 if (DECL_P (ret_val))
11273 return DECL_ORIGIN (ret_val);
11275 return ret_val;
11279 /* Return true if T1 and T2 are equivalent lists. */
11281 bool
11282 list_equal_p (const_tree t1, const_tree t2)
11284 for (; t1 && t2; t1 = TREE_CHAIN (t1) , t2 = TREE_CHAIN (t2))
11285 if (TREE_VALUE (t1) != TREE_VALUE (t2))
11286 return false;
11287 return !t1 && !t2;
11290 /* Return true iff conversion in EXP generates no instruction. Mark
11291 it inline so that we fully inline into the stripping functions even
11292 though we have two uses of this function. */
11294 static inline bool
11295 tree_nop_conversion (const_tree exp)
11297 tree outer_type, inner_type;
11299 if (!CONVERT_EXPR_P (exp)
11300 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11301 return false;
11302 if (TREE_OPERAND (exp, 0) == error_mark_node)
11303 return false;
11305 outer_type = TREE_TYPE (exp);
11306 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11308 if (!inner_type)
11309 return false;
11311 /* Use precision rather then machine mode when we can, which gives
11312 the correct answer even for submode (bit-field) types. */
11313 if ((INTEGRAL_TYPE_P (outer_type)
11314 || POINTER_TYPE_P (outer_type)
11315 || TREE_CODE (outer_type) == OFFSET_TYPE)
11316 && (INTEGRAL_TYPE_P (inner_type)
11317 || POINTER_TYPE_P (inner_type)
11318 || TREE_CODE (inner_type) == OFFSET_TYPE))
11319 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11321 /* Otherwise fall back on comparing machine modes (e.g. for
11322 aggregate types, floats). */
11323 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11326 /* Return true iff conversion in EXP generates no instruction. Don't
11327 consider conversions changing the signedness. */
11329 static bool
11330 tree_sign_nop_conversion (const_tree exp)
11332 tree outer_type, inner_type;
11334 if (!tree_nop_conversion (exp))
11335 return false;
11337 outer_type = TREE_TYPE (exp);
11338 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11340 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11341 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11344 /* Strip conversions from EXP according to tree_nop_conversion and
11345 return the resulting expression. */
11347 tree
11348 tree_strip_nop_conversions (tree exp)
11350 while (tree_nop_conversion (exp))
11351 exp = TREE_OPERAND (exp, 0);
11352 return exp;
11355 /* Strip conversions from EXP according to tree_sign_nop_conversion
11356 and return the resulting expression. */
11358 tree
11359 tree_strip_sign_nop_conversions (tree exp)
11361 while (tree_sign_nop_conversion (exp))
11362 exp = TREE_OPERAND (exp, 0);
11363 return exp;
11366 /* Avoid any floating point extensions from EXP. */
11367 tree
11368 strip_float_extensions (tree exp)
11370 tree sub, expt, subt;
11372 /* For floating point constant look up the narrowest type that can hold
11373 it properly and handle it like (type)(narrowest_type)constant.
11374 This way we can optimize for instance a=a*2.0 where "a" is float
11375 but 2.0 is double constant. */
11376 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11378 REAL_VALUE_TYPE orig;
11379 tree type = NULL;
11381 orig = TREE_REAL_CST (exp);
11382 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11383 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11384 type = float_type_node;
11385 else if (TYPE_PRECISION (TREE_TYPE (exp))
11386 > TYPE_PRECISION (double_type_node)
11387 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11388 type = double_type_node;
11389 if (type)
11390 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11393 if (!CONVERT_EXPR_P (exp))
11394 return exp;
11396 sub = TREE_OPERAND (exp, 0);
11397 subt = TREE_TYPE (sub);
11398 expt = TREE_TYPE (exp);
11400 if (!FLOAT_TYPE_P (subt))
11401 return exp;
11403 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11404 return exp;
11406 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11407 return exp;
11409 return strip_float_extensions (sub);
11412 /* Strip out all handled components that produce invariant
11413 offsets. */
11415 const_tree
11416 strip_invariant_refs (const_tree op)
11418 while (handled_component_p (op))
11420 switch (TREE_CODE (op))
11422 case ARRAY_REF:
11423 case ARRAY_RANGE_REF:
11424 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11425 || TREE_OPERAND (op, 2) != NULL_TREE
11426 || TREE_OPERAND (op, 3) != NULL_TREE)
11427 return NULL;
11428 break;
11430 case COMPONENT_REF:
11431 if (TREE_OPERAND (op, 2) != NULL_TREE)
11432 return NULL;
11433 break;
11435 default:;
11437 op = TREE_OPERAND (op, 0);
11440 return op;
11443 static GTY(()) tree gcc_eh_personality_decl;
11445 /* Return the GCC personality function decl. */
11447 tree
11448 lhd_gcc_personality (void)
11450 if (!gcc_eh_personality_decl)
11451 gcc_eh_personality_decl = build_personality_function ("gcc");
11452 return gcc_eh_personality_decl;
11455 /* Try to find a base info of BINFO that would have its field decl at offset
11456 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11457 found, return, otherwise return NULL_TREE. */
11459 tree
11460 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
11462 tree type = BINFO_TYPE (binfo);
11464 while (true)
11466 HOST_WIDE_INT pos, size;
11467 tree fld;
11468 int i;
11470 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (expected_type))
11471 return binfo;
11472 if (offset < 0)
11473 return NULL_TREE;
11475 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
11477 if (TREE_CODE (fld) != FIELD_DECL)
11478 continue;
11480 pos = int_bit_position (fld);
11481 size = tree_low_cst (DECL_SIZE (fld), 1);
11482 if (pos <= offset && (pos + size) > offset)
11483 break;
11485 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11486 return NULL_TREE;
11488 if (!DECL_ARTIFICIAL (fld))
11490 binfo = TYPE_BINFO (TREE_TYPE (fld));
11491 if (!binfo)
11492 return NULL_TREE;
11494 /* Offset 0 indicates the primary base, whose vtable contents are
11495 represented in the binfo for the derived class. */
11496 else if (offset != 0)
11498 tree base_binfo, found_binfo = NULL_TREE;
11499 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11500 if (TREE_TYPE (base_binfo) == TREE_TYPE (fld))
11502 found_binfo = base_binfo;
11503 break;
11505 if (!found_binfo)
11506 return NULL_TREE;
11507 binfo = found_binfo;
11510 type = TREE_TYPE (fld);
11511 offset -= pos;
11515 /* Returns true if X is a typedef decl. */
11517 bool
11518 is_typedef_decl (tree x)
11520 return (x && TREE_CODE (x) == TYPE_DECL
11521 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
11524 /* Returns true iff TYPE is a type variant created for a typedef. */
11526 bool
11527 typedef_variant_p (tree type)
11529 return is_typedef_decl (TYPE_NAME (type));
11532 /* Warn about a use of an identifier which was marked deprecated. */
11533 void
11534 warn_deprecated_use (tree node, tree attr)
11536 const char *msg;
11538 if (node == 0 || !warn_deprecated_decl)
11539 return;
11541 if (!attr)
11543 if (DECL_P (node))
11544 attr = DECL_ATTRIBUTES (node);
11545 else if (TYPE_P (node))
11547 tree decl = TYPE_STUB_DECL (node);
11548 if (decl)
11549 attr = lookup_attribute ("deprecated",
11550 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
11554 if (attr)
11555 attr = lookup_attribute ("deprecated", attr);
11557 if (attr)
11558 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
11559 else
11560 msg = NULL;
11562 if (DECL_P (node))
11564 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
11565 if (msg)
11566 warning (OPT_Wdeprecated_declarations,
11567 "%qD is deprecated (declared at %s:%d): %s",
11568 node, xloc.file, xloc.line, msg);
11569 else
11570 warning (OPT_Wdeprecated_declarations,
11571 "%qD is deprecated (declared at %s:%d)",
11572 node, xloc.file, xloc.line);
11574 else if (TYPE_P (node))
11576 tree what = NULL_TREE;
11577 tree decl = TYPE_STUB_DECL (node);
11579 if (TYPE_NAME (node))
11581 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
11582 what = TYPE_NAME (node);
11583 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
11584 && DECL_NAME (TYPE_NAME (node)))
11585 what = DECL_NAME (TYPE_NAME (node));
11588 if (decl)
11590 expanded_location xloc
11591 = expand_location (DECL_SOURCE_LOCATION (decl));
11592 if (what)
11594 if (msg)
11595 warning (OPT_Wdeprecated_declarations,
11596 "%qE is deprecated (declared at %s:%d): %s",
11597 what, xloc.file, xloc.line, msg);
11598 else
11599 warning (OPT_Wdeprecated_declarations,
11600 "%qE is deprecated (declared at %s:%d)", what,
11601 xloc.file, xloc.line);
11603 else
11605 if (msg)
11606 warning (OPT_Wdeprecated_declarations,
11607 "type is deprecated (declared at %s:%d): %s",
11608 xloc.file, xloc.line, msg);
11609 else
11610 warning (OPT_Wdeprecated_declarations,
11611 "type is deprecated (declared at %s:%d)",
11612 xloc.file, xloc.line);
11615 else
11617 if (what)
11619 if (msg)
11620 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
11621 what, msg);
11622 else
11623 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
11625 else
11627 if (msg)
11628 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
11629 msg);
11630 else
11631 warning (OPT_Wdeprecated_declarations, "type is deprecated");
11637 #include "gt-tree.h"