2013-01-08 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / tree.c
blob7cacb2a0d64c072457694eb19abd080c9a4ebe09
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 t = type_hash_canon (hashcode, t);
7511 if (TYPE_CANONICAL (t) == t)
7513 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7514 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7515 SET_TYPE_STRUCTURAL_EQUALITY (t);
7516 else if (TYPE_CANONICAL (elt_type) != elt_type
7517 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7518 TYPE_CANONICAL (t)
7519 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7520 index_type
7521 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7522 shared);
7525 return t;
7528 /* Wrapper around build_array_type_1 with SHARED set to true. */
7530 tree
7531 build_array_type (tree elt_type, tree index_type)
7533 return build_array_type_1 (elt_type, index_type, true);
7536 /* Wrapper around build_array_type_1 with SHARED set to false. */
7538 tree
7539 build_nonshared_array_type (tree elt_type, tree index_type)
7541 return build_array_type_1 (elt_type, index_type, false);
7544 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7545 sizetype. */
7547 tree
7548 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
7550 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7553 /* Recursively examines the array elements of TYPE, until a non-array
7554 element type is found. */
7556 tree
7557 strip_array_types (tree type)
7559 while (TREE_CODE (type) == ARRAY_TYPE)
7560 type = TREE_TYPE (type);
7562 return type;
7565 /* Computes the canonical argument types from the argument type list
7566 ARGTYPES.
7568 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7569 on entry to this function, or if any of the ARGTYPES are
7570 structural.
7572 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7573 true on entry to this function, or if any of the ARGTYPES are
7574 non-canonical.
7576 Returns a canonical argument list, which may be ARGTYPES when the
7577 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7578 true) or would not differ from ARGTYPES. */
7580 static tree
7581 maybe_canonicalize_argtypes(tree argtypes,
7582 bool *any_structural_p,
7583 bool *any_noncanonical_p)
7585 tree arg;
7586 bool any_noncanonical_argtypes_p = false;
7588 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7590 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7591 /* Fail gracefully by stating that the type is structural. */
7592 *any_structural_p = true;
7593 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7594 *any_structural_p = true;
7595 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7596 || TREE_PURPOSE (arg))
7597 /* If the argument has a default argument, we consider it
7598 non-canonical even though the type itself is canonical.
7599 That way, different variants of function and method types
7600 with default arguments will all point to the variant with
7601 no defaults as their canonical type. */
7602 any_noncanonical_argtypes_p = true;
7605 if (*any_structural_p)
7606 return argtypes;
7608 if (any_noncanonical_argtypes_p)
7610 /* Build the canonical list of argument types. */
7611 tree canon_argtypes = NULL_TREE;
7612 bool is_void = false;
7614 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7616 if (arg == void_list_node)
7617 is_void = true;
7618 else
7619 canon_argtypes = tree_cons (NULL_TREE,
7620 TYPE_CANONICAL (TREE_VALUE (arg)),
7621 canon_argtypes);
7624 canon_argtypes = nreverse (canon_argtypes);
7625 if (is_void)
7626 canon_argtypes = chainon (canon_argtypes, void_list_node);
7628 /* There is a non-canonical type. */
7629 *any_noncanonical_p = true;
7630 return canon_argtypes;
7633 /* The canonical argument types are the same as ARGTYPES. */
7634 return argtypes;
7637 /* Construct, lay out and return
7638 the type of functions returning type VALUE_TYPE
7639 given arguments of types ARG_TYPES.
7640 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7641 are data type nodes for the arguments of the function.
7642 If such a type has already been constructed, reuse it. */
7644 tree
7645 build_function_type (tree value_type, tree arg_types)
7647 tree t;
7648 hashval_t hashcode = 0;
7649 bool any_structural_p, any_noncanonical_p;
7650 tree canon_argtypes;
7652 if (TREE_CODE (value_type) == FUNCTION_TYPE)
7654 error ("function return type cannot be function");
7655 value_type = integer_type_node;
7658 /* Make a node of the sort we want. */
7659 t = make_node (FUNCTION_TYPE);
7660 TREE_TYPE (t) = value_type;
7661 TYPE_ARG_TYPES (t) = arg_types;
7663 /* If we already have such a type, use the old one. */
7664 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
7665 hashcode = type_hash_list (arg_types, hashcode);
7666 t = type_hash_canon (hashcode, t);
7668 /* Set up the canonical type. */
7669 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7670 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7671 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
7672 &any_structural_p,
7673 &any_noncanonical_p);
7674 if (any_structural_p)
7675 SET_TYPE_STRUCTURAL_EQUALITY (t);
7676 else if (any_noncanonical_p)
7677 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
7678 canon_argtypes);
7680 if (!COMPLETE_TYPE_P (t))
7681 layout_type (t);
7682 return t;
7685 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP and the
7686 return value if SKIP_RETURN is true. */
7688 static tree
7689 build_function_type_skip_args (tree orig_type, bitmap args_to_skip,
7690 bool skip_return)
7692 tree new_type = NULL;
7693 tree args, new_args = NULL, t;
7694 tree new_reversed;
7695 int i = 0;
7697 for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
7698 args = TREE_CHAIN (args), i++)
7699 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
7700 new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
7702 new_reversed = nreverse (new_args);
7703 if (args)
7705 if (new_reversed)
7706 TREE_CHAIN (new_args) = void_list_node;
7707 else
7708 new_reversed = void_list_node;
7711 /* Use copy_node to preserve as much as possible from original type
7712 (debug info, attribute lists etc.)
7713 Exception is METHOD_TYPEs must have THIS argument.
7714 When we are asked to remove it, we need to build new FUNCTION_TYPE
7715 instead. */
7716 if (TREE_CODE (orig_type) != METHOD_TYPE
7717 || !args_to_skip
7718 || !bitmap_bit_p (args_to_skip, 0))
7720 new_type = build_distinct_type_copy (orig_type);
7721 TYPE_ARG_TYPES (new_type) = new_reversed;
7723 else
7725 new_type
7726 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
7727 new_reversed));
7728 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
7731 if (skip_return)
7732 TREE_TYPE (new_type) = void_type_node;
7734 /* This is a new type, not a copy of an old type. Need to reassociate
7735 variants. We can handle everything except the main variant lazily. */
7736 t = TYPE_MAIN_VARIANT (orig_type);
7737 if (t != orig_type)
7739 t = build_function_type_skip_args (t, args_to_skip, skip_return);
7740 TYPE_MAIN_VARIANT (new_type) = t;
7741 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
7742 TYPE_NEXT_VARIANT (t) = new_type;
7744 else
7746 TYPE_MAIN_VARIANT (new_type) = new_type;
7747 TYPE_NEXT_VARIANT (new_type) = NULL;
7750 return new_type;
7753 /* Build variant of function decl ORIG_DECL skipping ARGS_TO_SKIP and the
7754 return value if SKIP_RETURN is true.
7756 Arguments from DECL_ARGUMENTS list can't be removed now, since they are
7757 linked by TREE_CHAIN directly. The caller is responsible for eliminating
7758 them when they are being duplicated (i.e. copy_arguments_for_versioning). */
7760 tree
7761 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip,
7762 bool skip_return)
7764 tree new_decl = copy_node (orig_decl);
7765 tree new_type;
7767 new_type = TREE_TYPE (orig_decl);
7768 if (prototype_p (new_type)
7769 || (skip_return && !VOID_TYPE_P (TREE_TYPE (new_type))))
7770 new_type
7771 = build_function_type_skip_args (new_type, args_to_skip, skip_return);
7772 TREE_TYPE (new_decl) = new_type;
7774 /* For declarations setting DECL_VINDEX (i.e. methods)
7775 we expect first argument to be THIS pointer. */
7776 if (args_to_skip && bitmap_bit_p (args_to_skip, 0))
7777 DECL_VINDEX (new_decl) = NULL_TREE;
7779 /* When signature changes, we need to clear builtin info. */
7780 if (DECL_BUILT_IN (new_decl)
7781 && args_to_skip
7782 && !bitmap_empty_p (args_to_skip))
7784 DECL_BUILT_IN_CLASS (new_decl) = NOT_BUILT_IN;
7785 DECL_FUNCTION_CODE (new_decl) = (enum built_in_function) 0;
7787 return new_decl;
7790 /* Build a function type. The RETURN_TYPE is the type returned by the
7791 function. If VAARGS is set, no void_type_node is appended to the
7792 the list. ARGP must be always be terminated be a NULL_TREE. */
7794 static tree
7795 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
7797 tree t, args, last;
7799 t = va_arg (argp, tree);
7800 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
7801 args = tree_cons (NULL_TREE, t, args);
7803 if (vaargs)
7805 last = args;
7806 if (args != NULL_TREE)
7807 args = nreverse (args);
7808 gcc_assert (last != void_list_node);
7810 else if (args == NULL_TREE)
7811 args = void_list_node;
7812 else
7814 last = args;
7815 args = nreverse (args);
7816 TREE_CHAIN (last) = void_list_node;
7818 args = build_function_type (return_type, args);
7820 return args;
7823 /* Build a function type. The RETURN_TYPE is the type returned by the
7824 function. If additional arguments are provided, they are
7825 additional argument types. The list of argument types must always
7826 be terminated by NULL_TREE. */
7828 tree
7829 build_function_type_list (tree return_type, ...)
7831 tree args;
7832 va_list p;
7834 va_start (p, return_type);
7835 args = build_function_type_list_1 (false, return_type, p);
7836 va_end (p);
7837 return args;
7840 /* Build a variable argument function type. The RETURN_TYPE is the
7841 type returned by the function. If additional arguments are provided,
7842 they are additional argument types. The list of argument types must
7843 always be terminated by NULL_TREE. */
7845 tree
7846 build_varargs_function_type_list (tree return_type, ...)
7848 tree args;
7849 va_list p;
7851 va_start (p, return_type);
7852 args = build_function_type_list_1 (true, return_type, p);
7853 va_end (p);
7855 return args;
7858 /* Build a function type. RETURN_TYPE is the type returned by the
7859 function; VAARGS indicates whether the function takes varargs. The
7860 function takes N named arguments, the types of which are provided in
7861 ARG_TYPES. */
7863 static tree
7864 build_function_type_array_1 (bool vaargs, tree return_type, int n,
7865 tree *arg_types)
7867 int i;
7868 tree t = vaargs ? NULL_TREE : void_list_node;
7870 for (i = n - 1; i >= 0; i--)
7871 t = tree_cons (NULL_TREE, arg_types[i], t);
7873 return build_function_type (return_type, t);
7876 /* Build a function type. RETURN_TYPE is the type returned by the
7877 function. The function takes N named arguments, the types of which
7878 are provided in ARG_TYPES. */
7880 tree
7881 build_function_type_array (tree return_type, int n, tree *arg_types)
7883 return build_function_type_array_1 (false, return_type, n, arg_types);
7886 /* Build a variable argument function type. RETURN_TYPE is the type
7887 returned by the function. The function takes N named arguments, the
7888 types of which are provided in ARG_TYPES. */
7890 tree
7891 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
7893 return build_function_type_array_1 (true, return_type, n, arg_types);
7896 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
7897 and ARGTYPES (a TREE_LIST) are the return type and arguments types
7898 for the method. An implicit additional parameter (of type
7899 pointer-to-BASETYPE) is added to the ARGTYPES. */
7901 tree
7902 build_method_type_directly (tree basetype,
7903 tree rettype,
7904 tree argtypes)
7906 tree t;
7907 tree ptype;
7908 int hashcode = 0;
7909 bool any_structural_p, any_noncanonical_p;
7910 tree canon_argtypes;
7912 /* Make a node of the sort we want. */
7913 t = make_node (METHOD_TYPE);
7915 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7916 TREE_TYPE (t) = rettype;
7917 ptype = build_pointer_type (basetype);
7919 /* The actual arglist for this function includes a "hidden" argument
7920 which is "this". Put it into the list of argument types. */
7921 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
7922 TYPE_ARG_TYPES (t) = argtypes;
7924 /* If we already have such a type, use the old one. */
7925 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
7926 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
7927 hashcode = type_hash_list (argtypes, hashcode);
7928 t = type_hash_canon (hashcode, t);
7930 /* Set up the canonical type. */
7931 any_structural_p
7932 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7933 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
7934 any_noncanonical_p
7935 = (TYPE_CANONICAL (basetype) != basetype
7936 || TYPE_CANONICAL (rettype) != rettype);
7937 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
7938 &any_structural_p,
7939 &any_noncanonical_p);
7940 if (any_structural_p)
7941 SET_TYPE_STRUCTURAL_EQUALITY (t);
7942 else if (any_noncanonical_p)
7943 TYPE_CANONICAL (t)
7944 = build_method_type_directly (TYPE_CANONICAL (basetype),
7945 TYPE_CANONICAL (rettype),
7946 canon_argtypes);
7947 if (!COMPLETE_TYPE_P (t))
7948 layout_type (t);
7950 return t;
7953 /* Construct, lay out and return the type of methods belonging to class
7954 BASETYPE and whose arguments and values are described by TYPE.
7955 If that type exists already, reuse it.
7956 TYPE must be a FUNCTION_TYPE node. */
7958 tree
7959 build_method_type (tree basetype, tree type)
7961 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
7963 return build_method_type_directly (basetype,
7964 TREE_TYPE (type),
7965 TYPE_ARG_TYPES (type));
7968 /* Construct, lay out and return the type of offsets to a value
7969 of type TYPE, within an object of type BASETYPE.
7970 If a suitable offset type exists already, reuse it. */
7972 tree
7973 build_offset_type (tree basetype, tree type)
7975 tree t;
7976 hashval_t hashcode = 0;
7978 /* Make a node of the sort we want. */
7979 t = make_node (OFFSET_TYPE);
7981 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7982 TREE_TYPE (t) = type;
7984 /* If we already have such a type, use the old one. */
7985 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
7986 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
7987 t = type_hash_canon (hashcode, t);
7989 if (!COMPLETE_TYPE_P (t))
7990 layout_type (t);
7992 if (TYPE_CANONICAL (t) == t)
7994 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7995 || TYPE_STRUCTURAL_EQUALITY_P (type))
7996 SET_TYPE_STRUCTURAL_EQUALITY (t);
7997 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
7998 || TYPE_CANONICAL (type) != type)
7999 TYPE_CANONICAL (t)
8000 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8001 TYPE_CANONICAL (type));
8004 return t;
8007 /* Create a complex type whose components are COMPONENT_TYPE. */
8009 tree
8010 build_complex_type (tree component_type)
8012 tree t;
8013 hashval_t hashcode;
8015 gcc_assert (INTEGRAL_TYPE_P (component_type)
8016 || SCALAR_FLOAT_TYPE_P (component_type)
8017 || FIXED_POINT_TYPE_P (component_type));
8019 /* Make a node of the sort we want. */
8020 t = make_node (COMPLEX_TYPE);
8022 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8024 /* If we already have such a type, use the old one. */
8025 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
8026 t = type_hash_canon (hashcode, t);
8028 if (!COMPLETE_TYPE_P (t))
8029 layout_type (t);
8031 if (TYPE_CANONICAL (t) == t)
8033 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8034 SET_TYPE_STRUCTURAL_EQUALITY (t);
8035 else if (TYPE_CANONICAL (component_type) != component_type)
8036 TYPE_CANONICAL (t)
8037 = build_complex_type (TYPE_CANONICAL (component_type));
8040 /* We need to create a name, since complex is a fundamental type. */
8041 if (! TYPE_NAME (t))
8043 const char *name;
8044 if (component_type == char_type_node)
8045 name = "complex char";
8046 else if (component_type == signed_char_type_node)
8047 name = "complex signed char";
8048 else if (component_type == unsigned_char_type_node)
8049 name = "complex unsigned char";
8050 else if (component_type == short_integer_type_node)
8051 name = "complex short int";
8052 else if (component_type == short_unsigned_type_node)
8053 name = "complex short unsigned int";
8054 else if (component_type == integer_type_node)
8055 name = "complex int";
8056 else if (component_type == unsigned_type_node)
8057 name = "complex unsigned int";
8058 else if (component_type == long_integer_type_node)
8059 name = "complex long int";
8060 else if (component_type == long_unsigned_type_node)
8061 name = "complex long unsigned int";
8062 else if (component_type == long_long_integer_type_node)
8063 name = "complex long long int";
8064 else if (component_type == long_long_unsigned_type_node)
8065 name = "complex long long unsigned int";
8066 else
8067 name = 0;
8069 if (name != 0)
8070 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8071 get_identifier (name), t);
8074 return build_qualified_type (t, TYPE_QUALS (component_type));
8077 /* If TYPE is a real or complex floating-point type and the target
8078 does not directly support arithmetic on TYPE then return the wider
8079 type to be used for arithmetic on TYPE. Otherwise, return
8080 NULL_TREE. */
8082 tree
8083 excess_precision_type (tree type)
8085 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8087 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8088 switch (TREE_CODE (type))
8090 case REAL_TYPE:
8091 switch (flt_eval_method)
8093 case 1:
8094 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8095 return double_type_node;
8096 break;
8097 case 2:
8098 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8099 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8100 return long_double_type_node;
8101 break;
8102 default:
8103 gcc_unreachable ();
8105 break;
8106 case COMPLEX_TYPE:
8107 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8108 return NULL_TREE;
8109 switch (flt_eval_method)
8111 case 1:
8112 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8113 return complex_double_type_node;
8114 break;
8115 case 2:
8116 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8117 || (TYPE_MODE (TREE_TYPE (type))
8118 == TYPE_MODE (double_type_node)))
8119 return complex_long_double_type_node;
8120 break;
8121 default:
8122 gcc_unreachable ();
8124 break;
8125 default:
8126 break;
8129 return NULL_TREE;
8132 /* Return OP, stripped of any conversions to wider types as much as is safe.
8133 Converting the value back to OP's type makes a value equivalent to OP.
8135 If FOR_TYPE is nonzero, we return a value which, if converted to
8136 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8138 OP must have integer, real or enumeral type. Pointers are not allowed!
8140 There are some cases where the obvious value we could return
8141 would regenerate to OP if converted to OP's type,
8142 but would not extend like OP to wider types.
8143 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8144 For example, if OP is (unsigned short)(signed char)-1,
8145 we avoid returning (signed char)-1 if FOR_TYPE is int,
8146 even though extending that to an unsigned short would regenerate OP,
8147 since the result of extending (signed char)-1 to (int)
8148 is different from (int) OP. */
8150 tree
8151 get_unwidened (tree op, tree for_type)
8153 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8154 tree type = TREE_TYPE (op);
8155 unsigned final_prec
8156 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8157 int uns
8158 = (for_type != 0 && for_type != type
8159 && final_prec > TYPE_PRECISION (type)
8160 && TYPE_UNSIGNED (type));
8161 tree win = op;
8163 while (CONVERT_EXPR_P (op))
8165 int bitschange;
8167 /* TYPE_PRECISION on vector types has different meaning
8168 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8169 so avoid them here. */
8170 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8171 break;
8173 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8174 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8176 /* Truncations are many-one so cannot be removed.
8177 Unless we are later going to truncate down even farther. */
8178 if (bitschange < 0
8179 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8180 break;
8182 /* See what's inside this conversion. If we decide to strip it,
8183 we will set WIN. */
8184 op = TREE_OPERAND (op, 0);
8186 /* If we have not stripped any zero-extensions (uns is 0),
8187 we can strip any kind of extension.
8188 If we have previously stripped a zero-extension,
8189 only zero-extensions can safely be stripped.
8190 Any extension can be stripped if the bits it would produce
8191 are all going to be discarded later by truncating to FOR_TYPE. */
8193 if (bitschange > 0)
8195 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8196 win = op;
8197 /* TYPE_UNSIGNED says whether this is a zero-extension.
8198 Let's avoid computing it if it does not affect WIN
8199 and if UNS will not be needed again. */
8200 if ((uns
8201 || CONVERT_EXPR_P (op))
8202 && TYPE_UNSIGNED (TREE_TYPE (op)))
8204 uns = 1;
8205 win = op;
8210 /* If we finally reach a constant see if it fits in for_type and
8211 in that case convert it. */
8212 if (for_type
8213 && TREE_CODE (win) == INTEGER_CST
8214 && TREE_TYPE (win) != for_type
8215 && int_fits_type_p (win, for_type))
8216 win = fold_convert (for_type, win);
8218 return win;
8221 /* Return OP or a simpler expression for a narrower value
8222 which can be sign-extended or zero-extended to give back OP.
8223 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8224 or 0 if the value should be sign-extended. */
8226 tree
8227 get_narrower (tree op, int *unsignedp_ptr)
8229 int uns = 0;
8230 int first = 1;
8231 tree win = op;
8232 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8234 while (TREE_CODE (op) == NOP_EXPR)
8236 int bitschange
8237 = (TYPE_PRECISION (TREE_TYPE (op))
8238 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8240 /* Truncations are many-one so cannot be removed. */
8241 if (bitschange < 0)
8242 break;
8244 /* See what's inside this conversion. If we decide to strip it,
8245 we will set WIN. */
8247 if (bitschange > 0)
8249 op = TREE_OPERAND (op, 0);
8250 /* An extension: the outermost one can be stripped,
8251 but remember whether it is zero or sign extension. */
8252 if (first)
8253 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8254 /* Otherwise, if a sign extension has been stripped,
8255 only sign extensions can now be stripped;
8256 if a zero extension has been stripped, only zero-extensions. */
8257 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8258 break;
8259 first = 0;
8261 else /* bitschange == 0 */
8263 /* A change in nominal type can always be stripped, but we must
8264 preserve the unsignedness. */
8265 if (first)
8266 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8267 first = 0;
8268 op = TREE_OPERAND (op, 0);
8269 /* Keep trying to narrow, but don't assign op to win if it
8270 would turn an integral type into something else. */
8271 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8272 continue;
8275 win = op;
8278 if (TREE_CODE (op) == COMPONENT_REF
8279 /* Since type_for_size always gives an integer type. */
8280 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8281 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8282 /* Ensure field is laid out already. */
8283 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8284 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
8286 unsigned HOST_WIDE_INT innerprec
8287 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
8288 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8289 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8290 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8292 /* We can get this structure field in a narrower type that fits it,
8293 but the resulting extension to its nominal type (a fullword type)
8294 must satisfy the same conditions as for other extensions.
8296 Do this only for fields that are aligned (not bit-fields),
8297 because when bit-field insns will be used there is no
8298 advantage in doing this. */
8300 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8301 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8302 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8303 && type != 0)
8305 if (first)
8306 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8307 win = fold_convert (type, op);
8311 *unsignedp_ptr = uns;
8312 return win;
8315 /* Returns true if integer constant C has a value that is permissible
8316 for type TYPE (an INTEGER_TYPE). */
8318 bool
8319 int_fits_type_p (const_tree c, const_tree type)
8321 tree type_low_bound, type_high_bound;
8322 bool ok_for_low_bound, ok_for_high_bound, unsc;
8323 double_int dc, dd;
8325 dc = tree_to_double_int (c);
8326 unsc = TYPE_UNSIGNED (TREE_TYPE (c));
8328 retry:
8329 type_low_bound = TYPE_MIN_VALUE (type);
8330 type_high_bound = TYPE_MAX_VALUE (type);
8332 /* If at least one bound of the type is a constant integer, we can check
8333 ourselves and maybe make a decision. If no such decision is possible, but
8334 this type is a subtype, try checking against that. Otherwise, use
8335 double_int_fits_to_tree_p, which checks against the precision.
8337 Compute the status for each possibly constant bound, and return if we see
8338 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8339 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8340 for "constant known to fit". */
8342 /* Check if c >= type_low_bound. */
8343 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8345 dd = tree_to_double_int (type_low_bound);
8346 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
8348 int c_neg = (!unsc && dc.is_negative ());
8349 int t_neg = (unsc && dd.is_negative ());
8351 if (c_neg && !t_neg)
8352 return false;
8353 if ((c_neg || !t_neg) && dc.ult (dd))
8354 return false;
8356 else if (dc.cmp (dd, unsc) < 0)
8357 return false;
8358 ok_for_low_bound = true;
8360 else
8361 ok_for_low_bound = false;
8363 /* Check if c <= type_high_bound. */
8364 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8366 dd = tree_to_double_int (type_high_bound);
8367 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
8369 int c_neg = (!unsc && dc.is_negative ());
8370 int t_neg = (unsc && dd.is_negative ());
8372 if (t_neg && !c_neg)
8373 return false;
8374 if ((t_neg || !c_neg) && dc.ugt (dd))
8375 return false;
8377 else if (dc.cmp (dd, unsc) > 0)
8378 return false;
8379 ok_for_high_bound = true;
8381 else
8382 ok_for_high_bound = false;
8384 /* If the constant fits both bounds, the result is known. */
8385 if (ok_for_low_bound && ok_for_high_bound)
8386 return true;
8388 /* Perform some generic filtering which may allow making a decision
8389 even if the bounds are not constant. First, negative integers
8390 never fit in unsigned types, */
8391 if (TYPE_UNSIGNED (type) && !unsc && dc.is_negative ())
8392 return false;
8394 /* Second, narrower types always fit in wider ones. */
8395 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8396 return true;
8398 /* Third, unsigned integers with top bit set never fit signed types. */
8399 if (! TYPE_UNSIGNED (type) && unsc)
8401 int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (c))) - 1;
8402 if (prec < HOST_BITS_PER_WIDE_INT)
8404 if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
8405 return false;
8407 else if (((((unsigned HOST_WIDE_INT) 1)
8408 << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
8409 return false;
8412 /* If we haven't been able to decide at this point, there nothing more we
8413 can check ourselves here. Look at the base type if we have one and it
8414 has the same precision. */
8415 if (TREE_CODE (type) == INTEGER_TYPE
8416 && TREE_TYPE (type) != 0
8417 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8419 type = TREE_TYPE (type);
8420 goto retry;
8423 /* Or to double_int_fits_to_tree_p, if nothing else. */
8424 return double_int_fits_to_tree_p (type, dc);
8427 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8428 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8429 represented (assuming two's-complement arithmetic) within the bit
8430 precision of the type are returned instead. */
8432 void
8433 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8435 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8436 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8437 mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
8438 TYPE_UNSIGNED (type));
8439 else
8441 if (TYPE_UNSIGNED (type))
8442 mpz_set_ui (min, 0);
8443 else
8445 double_int mn;
8446 mn = double_int::mask (TYPE_PRECISION (type) - 1);
8447 mn = (mn + double_int_one).sext (TYPE_PRECISION (type));
8448 mpz_set_double_int (min, mn, false);
8452 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8453 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8454 mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
8455 TYPE_UNSIGNED (type));
8456 else
8458 if (TYPE_UNSIGNED (type))
8459 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)),
8460 true);
8461 else
8462 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type) - 1),
8463 true);
8467 /* Return true if VAR is an automatic variable defined in function FN. */
8469 bool
8470 auto_var_in_fn_p (const_tree var, const_tree fn)
8472 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8473 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8474 || TREE_CODE (var) == PARM_DECL)
8475 && ! TREE_STATIC (var))
8476 || TREE_CODE (var) == LABEL_DECL
8477 || TREE_CODE (var) == RESULT_DECL));
8480 /* Subprogram of following function. Called by walk_tree.
8482 Return *TP if it is an automatic variable or parameter of the
8483 function passed in as DATA. */
8485 static tree
8486 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8488 tree fn = (tree) data;
8490 if (TYPE_P (*tp))
8491 *walk_subtrees = 0;
8493 else if (DECL_P (*tp)
8494 && auto_var_in_fn_p (*tp, fn))
8495 return *tp;
8497 return NULL_TREE;
8500 /* Returns true if T is, contains, or refers to a type with variable
8501 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8502 arguments, but not the return type. If FN is nonzero, only return
8503 true if a modifier of the type or position of FN is a variable or
8504 parameter inside FN.
8506 This concept is more general than that of C99 'variably modified types':
8507 in C99, a struct type is never variably modified because a VLA may not
8508 appear as a structure member. However, in GNU C code like:
8510 struct S { int i[f()]; };
8512 is valid, and other languages may define similar constructs. */
8514 bool
8515 variably_modified_type_p (tree type, tree fn)
8517 tree t;
8519 /* Test if T is either variable (if FN is zero) or an expression containing
8520 a variable in FN. If TYPE isn't gimplified, return true also if
8521 gimplify_one_sizepos would gimplify the expression into a local
8522 variable. */
8523 #define RETURN_TRUE_IF_VAR(T) \
8524 do { tree _t = (T); \
8525 if (_t != NULL_TREE \
8526 && _t != error_mark_node \
8527 && TREE_CODE (_t) != INTEGER_CST \
8528 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8529 && (!fn \
8530 || (!TYPE_SIZES_GIMPLIFIED (type) \
8531 && !is_gimple_sizepos (_t)) \
8532 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8533 return true; } while (0)
8535 if (type == error_mark_node)
8536 return false;
8538 /* If TYPE itself has variable size, it is variably modified. */
8539 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8540 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8542 switch (TREE_CODE (type))
8544 case POINTER_TYPE:
8545 case REFERENCE_TYPE:
8546 case VECTOR_TYPE:
8547 if (variably_modified_type_p (TREE_TYPE (type), fn))
8548 return true;
8549 break;
8551 case FUNCTION_TYPE:
8552 case METHOD_TYPE:
8553 /* If TYPE is a function type, it is variably modified if the
8554 return type is variably modified. */
8555 if (variably_modified_type_p (TREE_TYPE (type), fn))
8556 return true;
8557 break;
8559 case INTEGER_TYPE:
8560 case REAL_TYPE:
8561 case FIXED_POINT_TYPE:
8562 case ENUMERAL_TYPE:
8563 case BOOLEAN_TYPE:
8564 /* Scalar types are variably modified if their end points
8565 aren't constant. */
8566 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8567 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8568 break;
8570 case RECORD_TYPE:
8571 case UNION_TYPE:
8572 case QUAL_UNION_TYPE:
8573 /* We can't see if any of the fields are variably-modified by the
8574 definition we normally use, since that would produce infinite
8575 recursion via pointers. */
8576 /* This is variably modified if some field's type is. */
8577 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8578 if (TREE_CODE (t) == FIELD_DECL)
8580 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8581 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8582 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8584 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8585 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8587 break;
8589 case ARRAY_TYPE:
8590 /* Do not call ourselves to avoid infinite recursion. This is
8591 variably modified if the element type is. */
8592 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8593 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8594 break;
8596 default:
8597 break;
8600 /* The current language may have other cases to check, but in general,
8601 all other types are not variably modified. */
8602 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8604 #undef RETURN_TRUE_IF_VAR
8607 /* Given a DECL or TYPE, return the scope in which it was declared, or
8608 NULL_TREE if there is no containing scope. */
8610 tree
8611 get_containing_scope (const_tree t)
8613 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8616 /* Return the innermost context enclosing DECL that is
8617 a FUNCTION_DECL, or zero if none. */
8619 tree
8620 decl_function_context (const_tree decl)
8622 tree context;
8624 if (TREE_CODE (decl) == ERROR_MARK)
8625 return 0;
8627 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8628 where we look up the function at runtime. Such functions always take
8629 a first argument of type 'pointer to real context'.
8631 C++ should really be fixed to use DECL_CONTEXT for the real context,
8632 and use something else for the "virtual context". */
8633 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8634 context
8635 = TYPE_MAIN_VARIANT
8636 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8637 else
8638 context = DECL_CONTEXT (decl);
8640 while (context && TREE_CODE (context) != FUNCTION_DECL)
8642 if (TREE_CODE (context) == BLOCK)
8643 context = BLOCK_SUPERCONTEXT (context);
8644 else
8645 context = get_containing_scope (context);
8648 return context;
8651 /* Return the innermost context enclosing DECL that is
8652 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8653 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8655 tree
8656 decl_type_context (const_tree decl)
8658 tree context = DECL_CONTEXT (decl);
8660 while (context)
8661 switch (TREE_CODE (context))
8663 case NAMESPACE_DECL:
8664 case TRANSLATION_UNIT_DECL:
8665 return NULL_TREE;
8667 case RECORD_TYPE:
8668 case UNION_TYPE:
8669 case QUAL_UNION_TYPE:
8670 return context;
8672 case TYPE_DECL:
8673 case FUNCTION_DECL:
8674 context = DECL_CONTEXT (context);
8675 break;
8677 case BLOCK:
8678 context = BLOCK_SUPERCONTEXT (context);
8679 break;
8681 default:
8682 gcc_unreachable ();
8685 return NULL_TREE;
8688 /* CALL is a CALL_EXPR. Return the declaration for the function
8689 called, or NULL_TREE if the called function cannot be
8690 determined. */
8692 tree
8693 get_callee_fndecl (const_tree call)
8695 tree addr;
8697 if (call == error_mark_node)
8698 return error_mark_node;
8700 /* It's invalid to call this function with anything but a
8701 CALL_EXPR. */
8702 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8704 /* The first operand to the CALL is the address of the function
8705 called. */
8706 addr = CALL_EXPR_FN (call);
8708 STRIP_NOPS (addr);
8710 /* If this is a readonly function pointer, extract its initial value. */
8711 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8712 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8713 && DECL_INITIAL (addr))
8714 addr = DECL_INITIAL (addr);
8716 /* If the address is just `&f' for some function `f', then we know
8717 that `f' is being called. */
8718 if (TREE_CODE (addr) == ADDR_EXPR
8719 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8720 return TREE_OPERAND (addr, 0);
8722 /* We couldn't figure out what was being called. */
8723 return NULL_TREE;
8726 /* Print debugging information about tree nodes generated during the compile,
8727 and any language-specific information. */
8729 void
8730 dump_tree_statistics (void)
8732 if (GATHER_STATISTICS)
8734 int i;
8735 int total_nodes, total_bytes;
8736 fprintf (stderr, "Kind Nodes Bytes\n");
8737 fprintf (stderr, "---------------------------------------\n");
8738 total_nodes = total_bytes = 0;
8739 for (i = 0; i < (int) all_kinds; i++)
8741 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
8742 tree_node_counts[i], tree_node_sizes[i]);
8743 total_nodes += tree_node_counts[i];
8744 total_bytes += tree_node_sizes[i];
8746 fprintf (stderr, "---------------------------------------\n");
8747 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
8748 fprintf (stderr, "---------------------------------------\n");
8749 fprintf (stderr, "Code Nodes\n");
8750 fprintf (stderr, "----------------------------\n");
8751 for (i = 0; i < (int) MAX_TREE_CODES; i++)
8752 fprintf (stderr, "%-20s %7d\n", tree_code_name[i], tree_code_counts[i]);
8753 fprintf (stderr, "----------------------------\n");
8754 ssanames_print_statistics ();
8755 phinodes_print_statistics ();
8757 else
8758 fprintf (stderr, "(No per-node statistics)\n");
8760 print_type_hash_statistics ();
8761 print_debug_expr_statistics ();
8762 print_value_expr_statistics ();
8763 lang_hooks.print_statistics ();
8766 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8768 /* Generate a crc32 of a byte. */
8770 static unsigned
8771 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
8773 unsigned ix;
8775 for (ix = bits; ix--; value <<= 1)
8777 unsigned feedback;
8779 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
8780 chksum <<= 1;
8781 chksum ^= feedback;
8783 return chksum;
8786 /* Generate a crc32 of a 32-bit unsigned. */
8788 unsigned
8789 crc32_unsigned (unsigned chksum, unsigned value)
8791 return crc32_unsigned_bits (chksum, value, 32);
8794 /* Generate a crc32 of a byte. */
8796 unsigned
8797 crc32_byte (unsigned chksum, char byte)
8799 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
8802 /* Generate a crc32 of a string. */
8804 unsigned
8805 crc32_string (unsigned chksum, const char *string)
8809 chksum = crc32_byte (chksum, *string);
8811 while (*string++);
8812 return chksum;
8815 /* P is a string that will be used in a symbol. Mask out any characters
8816 that are not valid in that context. */
8818 void
8819 clean_symbol_name (char *p)
8821 for (; *p; p++)
8822 if (! (ISALNUM (*p)
8823 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
8824 || *p == '$'
8825 #endif
8826 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
8827 || *p == '.'
8828 #endif
8830 *p = '_';
8833 /* Generate a name for a special-purpose function.
8834 The generated name may need to be unique across the whole link.
8835 Changes to this function may also require corresponding changes to
8836 xstrdup_mask_random.
8837 TYPE is some string to identify the purpose of this function to the
8838 linker or collect2; it must start with an uppercase letter,
8839 one of:
8840 I - for constructors
8841 D - for destructors
8842 N - for C++ anonymous namespaces
8843 F - for DWARF unwind frame information. */
8845 tree
8846 get_file_function_name (const char *type)
8848 char *buf;
8849 const char *p;
8850 char *q;
8852 /* If we already have a name we know to be unique, just use that. */
8853 if (first_global_object_name)
8854 p = q = ASTRDUP (first_global_object_name);
8855 /* If the target is handling the constructors/destructors, they
8856 will be local to this file and the name is only necessary for
8857 debugging purposes.
8858 We also assign sub_I and sub_D sufixes to constructors called from
8859 the global static constructors. These are always local. */
8860 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
8861 || (strncmp (type, "sub_", 4) == 0
8862 && (type[4] == 'I' || type[4] == 'D')))
8864 const char *file = main_input_filename;
8865 if (! file)
8866 file = input_filename;
8867 /* Just use the file's basename, because the full pathname
8868 might be quite long. */
8869 p = q = ASTRDUP (lbasename (file));
8871 else
8873 /* Otherwise, the name must be unique across the entire link.
8874 We don't have anything that we know to be unique to this translation
8875 unit, so use what we do have and throw in some randomness. */
8876 unsigned len;
8877 const char *name = weak_global_object_name;
8878 const char *file = main_input_filename;
8880 if (! name)
8881 name = "";
8882 if (! file)
8883 file = input_filename;
8885 len = strlen (file);
8886 q = (char *) alloca (9 + 17 + len + 1);
8887 memcpy (q, file, len + 1);
8889 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
8890 crc32_string (0, name), get_random_seed (false));
8892 p = q;
8895 clean_symbol_name (q);
8896 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
8897 + strlen (type));
8899 /* Set up the name of the file-level functions we may need.
8900 Use a global object (which is already required to be unique over
8901 the program) rather than the file name (which imposes extra
8902 constraints). */
8903 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
8905 return get_identifier (buf);
8908 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
8910 /* Complain that the tree code of NODE does not match the expected 0
8911 terminated list of trailing codes. The trailing code list can be
8912 empty, for a more vague error message. FILE, LINE, and FUNCTION
8913 are of the caller. */
8915 void
8916 tree_check_failed (const_tree node, const char *file,
8917 int line, const char *function, ...)
8919 va_list args;
8920 const char *buffer;
8921 unsigned length = 0;
8922 int code;
8924 va_start (args, function);
8925 while ((code = va_arg (args, int)))
8926 length += 4 + strlen (tree_code_name[code]);
8927 va_end (args);
8928 if (length)
8930 char *tmp;
8931 va_start (args, function);
8932 length += strlen ("expected ");
8933 buffer = tmp = (char *) alloca (length);
8934 length = 0;
8935 while ((code = va_arg (args, int)))
8937 const char *prefix = length ? " or " : "expected ";
8939 strcpy (tmp + length, prefix);
8940 length += strlen (prefix);
8941 strcpy (tmp + length, tree_code_name[code]);
8942 length += strlen (tree_code_name[code]);
8944 va_end (args);
8946 else
8947 buffer = "unexpected node";
8949 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8950 buffer, tree_code_name[TREE_CODE (node)],
8951 function, trim_filename (file), line);
8954 /* Complain that the tree code of NODE does match the expected 0
8955 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
8956 the caller. */
8958 void
8959 tree_not_check_failed (const_tree node, const char *file,
8960 int line, const char *function, ...)
8962 va_list args;
8963 char *buffer;
8964 unsigned length = 0;
8965 int code;
8967 va_start (args, function);
8968 while ((code = va_arg (args, int)))
8969 length += 4 + strlen (tree_code_name[code]);
8970 va_end (args);
8971 va_start (args, function);
8972 buffer = (char *) alloca (length);
8973 length = 0;
8974 while ((code = va_arg (args, int)))
8976 if (length)
8978 strcpy (buffer + length, " or ");
8979 length += 4;
8981 strcpy (buffer + length, tree_code_name[code]);
8982 length += strlen (tree_code_name[code]);
8984 va_end (args);
8986 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
8987 buffer, tree_code_name[TREE_CODE (node)],
8988 function, trim_filename (file), line);
8991 /* Similar to tree_check_failed, except that we check for a class of tree
8992 code, given in CL. */
8994 void
8995 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
8996 const char *file, int line, const char *function)
8998 internal_error
8999 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9000 TREE_CODE_CLASS_STRING (cl),
9001 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9002 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9005 /* Similar to tree_check_failed, except that instead of specifying a
9006 dozen codes, use the knowledge that they're all sequential. */
9008 void
9009 tree_range_check_failed (const_tree node, const char *file, int line,
9010 const char *function, enum tree_code c1,
9011 enum tree_code c2)
9013 char *buffer;
9014 unsigned length = 0;
9015 unsigned int c;
9017 for (c = c1; c <= c2; ++c)
9018 length += 4 + strlen (tree_code_name[c]);
9020 length += strlen ("expected ");
9021 buffer = (char *) alloca (length);
9022 length = 0;
9024 for (c = c1; c <= c2; ++c)
9026 const char *prefix = length ? " or " : "expected ";
9028 strcpy (buffer + length, prefix);
9029 length += strlen (prefix);
9030 strcpy (buffer + length, tree_code_name[c]);
9031 length += strlen (tree_code_name[c]);
9034 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9035 buffer, tree_code_name[TREE_CODE (node)],
9036 function, trim_filename (file), line);
9040 /* Similar to tree_check_failed, except that we check that a tree does
9041 not have the specified code, given in CL. */
9043 void
9044 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9045 const char *file, int line, const char *function)
9047 internal_error
9048 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9049 TREE_CODE_CLASS_STRING (cl),
9050 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9051 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9055 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9057 void
9058 omp_clause_check_failed (const_tree node, const char *file, int line,
9059 const char *function, enum omp_clause_code code)
9061 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9062 omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
9063 function, trim_filename (file), line);
9067 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9069 void
9070 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9071 const char *function, enum omp_clause_code c1,
9072 enum omp_clause_code c2)
9074 char *buffer;
9075 unsigned length = 0;
9076 unsigned int c;
9078 for (c = c1; c <= c2; ++c)
9079 length += 4 + strlen (omp_clause_code_name[c]);
9081 length += strlen ("expected ");
9082 buffer = (char *) alloca (length);
9083 length = 0;
9085 for (c = c1; c <= c2; ++c)
9087 const char *prefix = length ? " or " : "expected ";
9089 strcpy (buffer + length, prefix);
9090 length += strlen (prefix);
9091 strcpy (buffer + length, omp_clause_code_name[c]);
9092 length += strlen (omp_clause_code_name[c]);
9095 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9096 buffer, omp_clause_code_name[TREE_CODE (node)],
9097 function, trim_filename (file), line);
9101 #undef DEFTREESTRUCT
9102 #define DEFTREESTRUCT(VAL, NAME) NAME,
9104 static const char *ts_enum_names[] = {
9105 #include "treestruct.def"
9107 #undef DEFTREESTRUCT
9109 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9111 /* Similar to tree_class_check_failed, except that we check for
9112 whether CODE contains the tree structure identified by EN. */
9114 void
9115 tree_contains_struct_check_failed (const_tree node,
9116 const enum tree_node_structure_enum en,
9117 const char *file, int line,
9118 const char *function)
9120 internal_error
9121 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9122 TS_ENUM_NAME(en),
9123 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9127 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9128 (dynamically sized) vector. */
9130 void
9131 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9132 const char *function)
9134 internal_error
9135 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9136 idx + 1, len, function, trim_filename (file), line);
9139 /* Similar to above, except that the check is for the bounds of the operand
9140 vector of an expression node EXP. */
9142 void
9143 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9144 int line, const char *function)
9146 int code = TREE_CODE (exp);
9147 internal_error
9148 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9149 idx + 1, tree_code_name[code], TREE_OPERAND_LENGTH (exp),
9150 function, trim_filename (file), line);
9153 /* Similar to above, except that the check is for the number of
9154 operands of an OMP_CLAUSE node. */
9156 void
9157 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9158 int line, const char *function)
9160 internal_error
9161 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9162 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9163 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9164 trim_filename (file), line);
9166 #endif /* ENABLE_TREE_CHECKING */
9168 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9169 and mapped to the machine mode MODE. Initialize its fields and build
9170 the information necessary for debugging output. */
9172 static tree
9173 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
9175 tree t;
9176 hashval_t hashcode = 0;
9178 t = make_node (VECTOR_TYPE);
9179 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9180 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9181 SET_TYPE_MODE (t, mode);
9183 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9184 SET_TYPE_STRUCTURAL_EQUALITY (t);
9185 else if (TYPE_CANONICAL (innertype) != innertype
9186 || mode != VOIDmode)
9187 TYPE_CANONICAL (t)
9188 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9190 layout_type (t);
9192 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
9193 hashcode = iterative_hash_host_wide_int (nunits, hashcode);
9194 hashcode = iterative_hash_host_wide_int (mode, hashcode);
9195 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (t)), hashcode);
9196 t = type_hash_canon (hashcode, t);
9198 /* We have built a main variant, based on the main variant of the
9199 inner type. Use it to build the variant we return. */
9200 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9201 && TREE_TYPE (t) != innertype)
9202 return build_type_attribute_qual_variant (t,
9203 TYPE_ATTRIBUTES (innertype),
9204 TYPE_QUALS (innertype));
9206 return t;
9209 static tree
9210 make_or_reuse_type (unsigned size, int unsignedp)
9212 if (size == INT_TYPE_SIZE)
9213 return unsignedp ? unsigned_type_node : integer_type_node;
9214 if (size == CHAR_TYPE_SIZE)
9215 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9216 if (size == SHORT_TYPE_SIZE)
9217 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9218 if (size == LONG_TYPE_SIZE)
9219 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9220 if (size == LONG_LONG_TYPE_SIZE)
9221 return (unsignedp ? long_long_unsigned_type_node
9222 : long_long_integer_type_node);
9223 if (size == 128 && int128_integer_type_node)
9224 return (unsignedp ? int128_unsigned_type_node
9225 : int128_integer_type_node);
9227 if (unsignedp)
9228 return make_unsigned_type (size);
9229 else
9230 return make_signed_type (size);
9233 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9235 static tree
9236 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9238 if (satp)
9240 if (size == SHORT_FRACT_TYPE_SIZE)
9241 return unsignedp ? sat_unsigned_short_fract_type_node
9242 : sat_short_fract_type_node;
9243 if (size == FRACT_TYPE_SIZE)
9244 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9245 if (size == LONG_FRACT_TYPE_SIZE)
9246 return unsignedp ? sat_unsigned_long_fract_type_node
9247 : sat_long_fract_type_node;
9248 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9249 return unsignedp ? sat_unsigned_long_long_fract_type_node
9250 : sat_long_long_fract_type_node;
9252 else
9254 if (size == SHORT_FRACT_TYPE_SIZE)
9255 return unsignedp ? unsigned_short_fract_type_node
9256 : short_fract_type_node;
9257 if (size == FRACT_TYPE_SIZE)
9258 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9259 if (size == LONG_FRACT_TYPE_SIZE)
9260 return unsignedp ? unsigned_long_fract_type_node
9261 : long_fract_type_node;
9262 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9263 return unsignedp ? unsigned_long_long_fract_type_node
9264 : long_long_fract_type_node;
9267 return make_fract_type (size, unsignedp, satp);
9270 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9272 static tree
9273 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9275 if (satp)
9277 if (size == SHORT_ACCUM_TYPE_SIZE)
9278 return unsignedp ? sat_unsigned_short_accum_type_node
9279 : sat_short_accum_type_node;
9280 if (size == ACCUM_TYPE_SIZE)
9281 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9282 if (size == LONG_ACCUM_TYPE_SIZE)
9283 return unsignedp ? sat_unsigned_long_accum_type_node
9284 : sat_long_accum_type_node;
9285 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9286 return unsignedp ? sat_unsigned_long_long_accum_type_node
9287 : sat_long_long_accum_type_node;
9289 else
9291 if (size == SHORT_ACCUM_TYPE_SIZE)
9292 return unsignedp ? unsigned_short_accum_type_node
9293 : short_accum_type_node;
9294 if (size == ACCUM_TYPE_SIZE)
9295 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9296 if (size == LONG_ACCUM_TYPE_SIZE)
9297 return unsignedp ? unsigned_long_accum_type_node
9298 : long_accum_type_node;
9299 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9300 return unsignedp ? unsigned_long_long_accum_type_node
9301 : long_long_accum_type_node;
9304 return make_accum_type (size, unsignedp, satp);
9307 /* Create nodes for all integer types (and error_mark_node) using the sizes
9308 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9309 SHORT_DOUBLE specifies whether double should be of the same precision
9310 as float. */
9312 void
9313 build_common_tree_nodes (bool signed_char, bool short_double)
9315 error_mark_node = make_node (ERROR_MARK);
9316 TREE_TYPE (error_mark_node) = error_mark_node;
9318 initialize_sizetypes ();
9320 /* Define both `signed char' and `unsigned char'. */
9321 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9322 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9323 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9324 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9326 /* Define `char', which is like either `signed char' or `unsigned char'
9327 but not the same as either. */
9328 char_type_node
9329 = (signed_char
9330 ? make_signed_type (CHAR_TYPE_SIZE)
9331 : make_unsigned_type (CHAR_TYPE_SIZE));
9332 TYPE_STRING_FLAG (char_type_node) = 1;
9334 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9335 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9336 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9337 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9338 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9339 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9340 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9341 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9342 #if HOST_BITS_PER_WIDE_INT >= 64
9343 /* TODO: This isn't correct, but as logic depends at the moment on
9344 host's instead of target's wide-integer.
9345 If there is a target not supporting TImode, but has an 128-bit
9346 integer-scalar register, this target check needs to be adjusted. */
9347 if (targetm.scalar_mode_supported_p (TImode))
9349 int128_integer_type_node = make_signed_type (128);
9350 int128_unsigned_type_node = make_unsigned_type (128);
9352 #endif
9354 /* Define a boolean type. This type only represents boolean values but
9355 may be larger than char depending on the value of BOOL_TYPE_SIZE.
9356 Front ends which want to override this size (i.e. Java) can redefine
9357 boolean_type_node before calling build_common_tree_nodes_2. */
9358 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9359 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9360 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9361 TYPE_PRECISION (boolean_type_node) = 1;
9363 /* Define what type to use for size_t. */
9364 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9365 size_type_node = unsigned_type_node;
9366 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9367 size_type_node = long_unsigned_type_node;
9368 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9369 size_type_node = long_long_unsigned_type_node;
9370 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9371 size_type_node = short_unsigned_type_node;
9372 else
9373 gcc_unreachable ();
9375 /* Fill in the rest of the sized types. Reuse existing type nodes
9376 when possible. */
9377 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9378 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9379 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9380 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9381 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9383 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9384 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9385 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9386 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9387 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9389 access_public_node = get_identifier ("public");
9390 access_protected_node = get_identifier ("protected");
9391 access_private_node = get_identifier ("private");
9393 /* Define these next since types below may used them. */
9394 integer_zero_node = build_int_cst (integer_type_node, 0);
9395 integer_one_node = build_int_cst (integer_type_node, 1);
9396 integer_three_node = build_int_cst (integer_type_node, 3);
9397 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9399 size_zero_node = size_int (0);
9400 size_one_node = size_int (1);
9401 bitsize_zero_node = bitsize_int (0);
9402 bitsize_one_node = bitsize_int (1);
9403 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9405 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9406 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9408 void_type_node = make_node (VOID_TYPE);
9409 layout_type (void_type_node);
9411 /* We are not going to have real types in C with less than byte alignment,
9412 so we might as well not have any types that claim to have it. */
9413 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9414 TYPE_USER_ALIGN (void_type_node) = 0;
9416 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9417 layout_type (TREE_TYPE (null_pointer_node));
9419 ptr_type_node = build_pointer_type (void_type_node);
9420 const_ptr_type_node
9421 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9422 fileptr_type_node = ptr_type_node;
9424 float_type_node = make_node (REAL_TYPE);
9425 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9426 layout_type (float_type_node);
9428 double_type_node = make_node (REAL_TYPE);
9429 if (short_double)
9430 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9431 else
9432 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9433 layout_type (double_type_node);
9435 long_double_type_node = make_node (REAL_TYPE);
9436 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9437 layout_type (long_double_type_node);
9439 float_ptr_type_node = build_pointer_type (float_type_node);
9440 double_ptr_type_node = build_pointer_type (double_type_node);
9441 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9442 integer_ptr_type_node = build_pointer_type (integer_type_node);
9444 /* Fixed size integer types. */
9445 uint16_type_node = build_nonstandard_integer_type (16, true);
9446 uint32_type_node = build_nonstandard_integer_type (32, true);
9447 uint64_type_node = build_nonstandard_integer_type (64, true);
9449 /* Decimal float types. */
9450 dfloat32_type_node = make_node (REAL_TYPE);
9451 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9452 layout_type (dfloat32_type_node);
9453 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9454 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9456 dfloat64_type_node = make_node (REAL_TYPE);
9457 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9458 layout_type (dfloat64_type_node);
9459 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9460 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9462 dfloat128_type_node = make_node (REAL_TYPE);
9463 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9464 layout_type (dfloat128_type_node);
9465 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9466 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9468 complex_integer_type_node = build_complex_type (integer_type_node);
9469 complex_float_type_node = build_complex_type (float_type_node);
9470 complex_double_type_node = build_complex_type (double_type_node);
9471 complex_long_double_type_node = build_complex_type (long_double_type_node);
9473 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9474 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9475 sat_ ## KIND ## _type_node = \
9476 make_sat_signed_ ## KIND ## _type (SIZE); \
9477 sat_unsigned_ ## KIND ## _type_node = \
9478 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9479 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9480 unsigned_ ## KIND ## _type_node = \
9481 make_unsigned_ ## KIND ## _type (SIZE);
9483 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9484 sat_ ## WIDTH ## KIND ## _type_node = \
9485 make_sat_signed_ ## KIND ## _type (SIZE); \
9486 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9487 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9488 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9489 unsigned_ ## WIDTH ## KIND ## _type_node = \
9490 make_unsigned_ ## KIND ## _type (SIZE);
9492 /* Make fixed-point type nodes based on four different widths. */
9493 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9494 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9495 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9496 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9497 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9499 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9500 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9501 NAME ## _type_node = \
9502 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9503 u ## NAME ## _type_node = \
9504 make_or_reuse_unsigned_ ## KIND ## _type \
9505 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9506 sat_ ## NAME ## _type_node = \
9507 make_or_reuse_sat_signed_ ## KIND ## _type \
9508 (GET_MODE_BITSIZE (MODE ## mode)); \
9509 sat_u ## NAME ## _type_node = \
9510 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9511 (GET_MODE_BITSIZE (U ## MODE ## mode));
9513 /* Fixed-point type and mode nodes. */
9514 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9515 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9516 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9517 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9518 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9519 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9520 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9521 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9522 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9523 MAKE_FIXED_MODE_NODE (accum, da, DA)
9524 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9527 tree t = targetm.build_builtin_va_list ();
9529 /* Many back-ends define record types without setting TYPE_NAME.
9530 If we copied the record type here, we'd keep the original
9531 record type without a name. This breaks name mangling. So,
9532 don't copy record types and let c_common_nodes_and_builtins()
9533 declare the type to be __builtin_va_list. */
9534 if (TREE_CODE (t) != RECORD_TYPE)
9535 t = build_variant_type_copy (t);
9537 va_list_type_node = t;
9541 /* Modify DECL for given flags. */
9542 void
9543 set_call_expr_flags (tree decl, int flags)
9545 if (flags & ECF_NOTHROW)
9546 TREE_NOTHROW (decl) = 1;
9547 if (flags & ECF_CONST)
9548 TREE_READONLY (decl) = 1;
9549 if (flags & ECF_PURE)
9550 DECL_PURE_P (decl) = 1;
9551 if (flags & ECF_LOOPING_CONST_OR_PURE)
9552 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9553 if (flags & ECF_NOVOPS)
9554 DECL_IS_NOVOPS (decl) = 1;
9555 if (flags & ECF_NORETURN)
9556 TREE_THIS_VOLATILE (decl) = 1;
9557 if (flags & ECF_MALLOC)
9558 DECL_IS_MALLOC (decl) = 1;
9559 if (flags & ECF_RETURNS_TWICE)
9560 DECL_IS_RETURNS_TWICE (decl) = 1;
9561 if (flags & ECF_LEAF)
9562 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9563 NULL, DECL_ATTRIBUTES (decl));
9564 if ((flags & ECF_TM_PURE) && flag_tm)
9565 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("transaction_pure"),
9566 NULL, DECL_ATTRIBUTES (decl));
9567 /* Looping const or pure is implied by noreturn.
9568 There is currently no way to declare looping const or looping pure alone. */
9569 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9570 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9574 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9576 static void
9577 local_define_builtin (const char *name, tree type, enum built_in_function code,
9578 const char *library_name, int ecf_flags)
9580 tree decl;
9582 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9583 library_name, NULL_TREE);
9584 set_call_expr_flags (decl, ecf_flags);
9586 set_builtin_decl (code, decl, true);
9589 /* Call this function after instantiating all builtins that the language
9590 front end cares about. This will build the rest of the builtins that
9591 are relied upon by the tree optimizers and the middle-end. */
9593 void
9594 build_common_builtin_nodes (void)
9596 tree tmp, ftype;
9597 int ecf_flags;
9599 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9601 ftype = build_function_type (void_type_node, void_list_node);
9602 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
9603 "__builtin_unreachable",
9604 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9605 | ECF_CONST | ECF_LEAF);
9608 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9609 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9611 ftype = build_function_type_list (ptr_type_node,
9612 ptr_type_node, const_ptr_type_node,
9613 size_type_node, NULL_TREE);
9615 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
9616 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9617 "memcpy", ECF_NOTHROW | ECF_LEAF);
9618 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9619 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9620 "memmove", ECF_NOTHROW | ECF_LEAF);
9623 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
9625 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9626 const_ptr_type_node, size_type_node,
9627 NULL_TREE);
9628 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9629 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9632 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
9634 ftype = build_function_type_list (ptr_type_node,
9635 ptr_type_node, integer_type_node,
9636 size_type_node, NULL_TREE);
9637 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9638 "memset", ECF_NOTHROW | ECF_LEAF);
9641 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
9643 ftype = build_function_type_list (ptr_type_node,
9644 size_type_node, NULL_TREE);
9645 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9646 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9649 ftype = build_function_type_list (ptr_type_node, size_type_node,
9650 size_type_node, NULL_TREE);
9651 local_define_builtin ("__builtin_alloca_with_align", ftype,
9652 BUILT_IN_ALLOCA_WITH_ALIGN, "alloca",
9653 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9655 /* If we're checking the stack, `alloca' can throw. */
9656 if (flag_stack_check)
9658 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
9659 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
9662 ftype = build_function_type_list (void_type_node,
9663 ptr_type_node, ptr_type_node,
9664 ptr_type_node, NULL_TREE);
9665 local_define_builtin ("__builtin_init_trampoline", ftype,
9666 BUILT_IN_INIT_TRAMPOLINE,
9667 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9668 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
9669 BUILT_IN_INIT_HEAP_TRAMPOLINE,
9670 "__builtin_init_heap_trampoline",
9671 ECF_NOTHROW | ECF_LEAF);
9673 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9674 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9675 BUILT_IN_ADJUST_TRAMPOLINE,
9676 "__builtin_adjust_trampoline",
9677 ECF_CONST | ECF_NOTHROW);
9679 ftype = build_function_type_list (void_type_node,
9680 ptr_type_node, ptr_type_node, NULL_TREE);
9681 local_define_builtin ("__builtin_nonlocal_goto", ftype,
9682 BUILT_IN_NONLOCAL_GOTO,
9683 "__builtin_nonlocal_goto",
9684 ECF_NORETURN | ECF_NOTHROW);
9686 ftype = build_function_type_list (void_type_node,
9687 ptr_type_node, ptr_type_node, NULL_TREE);
9688 local_define_builtin ("__builtin_setjmp_setup", ftype,
9689 BUILT_IN_SETJMP_SETUP,
9690 "__builtin_setjmp_setup", ECF_NOTHROW);
9692 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9693 local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
9694 BUILT_IN_SETJMP_DISPATCHER,
9695 "__builtin_setjmp_dispatcher",
9696 ECF_PURE | ECF_NOTHROW);
9698 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9699 local_define_builtin ("__builtin_setjmp_receiver", ftype,
9700 BUILT_IN_SETJMP_RECEIVER,
9701 "__builtin_setjmp_receiver", ECF_NOTHROW);
9703 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9704 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
9705 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9707 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9708 local_define_builtin ("__builtin_stack_restore", ftype,
9709 BUILT_IN_STACK_RESTORE,
9710 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9712 /* If there's a possibility that we might use the ARM EABI, build the
9713 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
9714 if (targetm.arm_eabi_unwinder)
9716 ftype = build_function_type_list (void_type_node, NULL_TREE);
9717 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
9718 BUILT_IN_CXA_END_CLEANUP,
9719 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
9722 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9723 local_define_builtin ("__builtin_unwind_resume", ftype,
9724 BUILT_IN_UNWIND_RESUME,
9725 ((targetm_common.except_unwind_info (&global_options)
9726 == UI_SJLJ)
9727 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
9728 ECF_NORETURN);
9730 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
9732 ftype = build_function_type_list (ptr_type_node, integer_type_node,
9733 NULL_TREE);
9734 local_define_builtin ("__builtin_return_address", ftype,
9735 BUILT_IN_RETURN_ADDRESS,
9736 "__builtin_return_address",
9737 ECF_NOTHROW);
9740 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
9741 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9743 ftype = build_function_type_list (void_type_node, ptr_type_node,
9744 ptr_type_node, NULL_TREE);
9745 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
9746 local_define_builtin ("__cyg_profile_func_enter", ftype,
9747 BUILT_IN_PROFILE_FUNC_ENTER,
9748 "__cyg_profile_func_enter", 0);
9749 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9750 local_define_builtin ("__cyg_profile_func_exit", ftype,
9751 BUILT_IN_PROFILE_FUNC_EXIT,
9752 "__cyg_profile_func_exit", 0);
9755 /* The exception object and filter values from the runtime. The argument
9756 must be zero before exception lowering, i.e. from the front end. After
9757 exception lowering, it will be the region number for the exception
9758 landing pad. These functions are PURE instead of CONST to prevent
9759 them from being hoisted past the exception edge that will initialize
9760 its value in the landing pad. */
9761 ftype = build_function_type_list (ptr_type_node,
9762 integer_type_node, NULL_TREE);
9763 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
9764 /* Only use TM_PURE if we we have TM language support. */
9765 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
9766 ecf_flags |= ECF_TM_PURE;
9767 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
9768 "__builtin_eh_pointer", ecf_flags);
9770 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
9771 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
9772 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
9773 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9775 ftype = build_function_type_list (void_type_node,
9776 integer_type_node, integer_type_node,
9777 NULL_TREE);
9778 local_define_builtin ("__builtin_eh_copy_values", ftype,
9779 BUILT_IN_EH_COPY_VALUES,
9780 "__builtin_eh_copy_values", ECF_NOTHROW);
9782 /* Complex multiplication and division. These are handled as builtins
9783 rather than optabs because emit_library_call_value doesn't support
9784 complex. Further, we can do slightly better with folding these
9785 beasties if the real and complex parts of the arguments are separate. */
9787 int mode;
9789 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
9791 char mode_name_buf[4], *q;
9792 const char *p;
9793 enum built_in_function mcode, dcode;
9794 tree type, inner_type;
9795 const char *prefix = "__";
9797 if (targetm.libfunc_gnu_prefix)
9798 prefix = "__gnu_";
9800 type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
9801 if (type == NULL)
9802 continue;
9803 inner_type = TREE_TYPE (type);
9805 ftype = build_function_type_list (type, inner_type, inner_type,
9806 inner_type, inner_type, NULL_TREE);
9808 mcode = ((enum built_in_function)
9809 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9810 dcode = ((enum built_in_function)
9811 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9813 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
9814 *q = TOLOWER (*p);
9815 *q = '\0';
9817 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
9818 NULL);
9819 local_define_builtin (built_in_names[mcode], ftype, mcode,
9820 built_in_names[mcode],
9821 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
9823 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
9824 NULL);
9825 local_define_builtin (built_in_names[dcode], ftype, dcode,
9826 built_in_names[dcode],
9827 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
9832 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
9833 better way.
9835 If we requested a pointer to a vector, build up the pointers that
9836 we stripped off while looking for the inner type. Similarly for
9837 return values from functions.
9839 The argument TYPE is the top of the chain, and BOTTOM is the
9840 new type which we will point to. */
9842 tree
9843 reconstruct_complex_type (tree type, tree bottom)
9845 tree inner, outer;
9847 if (TREE_CODE (type) == POINTER_TYPE)
9849 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9850 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
9851 TYPE_REF_CAN_ALIAS_ALL (type));
9853 else if (TREE_CODE (type) == REFERENCE_TYPE)
9855 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9856 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
9857 TYPE_REF_CAN_ALIAS_ALL (type));
9859 else if (TREE_CODE (type) == ARRAY_TYPE)
9861 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9862 outer = build_array_type (inner, TYPE_DOMAIN (type));
9864 else if (TREE_CODE (type) == FUNCTION_TYPE)
9866 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9867 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
9869 else if (TREE_CODE (type) == METHOD_TYPE)
9871 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9872 /* The build_method_type_directly() routine prepends 'this' to argument list,
9873 so we must compensate by getting rid of it. */
9874 outer
9875 = build_method_type_directly
9876 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
9877 inner,
9878 TREE_CHAIN (TYPE_ARG_TYPES (type)));
9880 else if (TREE_CODE (type) == OFFSET_TYPE)
9882 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9883 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
9885 else
9886 return bottom;
9888 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
9889 TYPE_QUALS (type));
9892 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
9893 the inner type. */
9894 tree
9895 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
9897 int nunits;
9899 switch (GET_MODE_CLASS (mode))
9901 case MODE_VECTOR_INT:
9902 case MODE_VECTOR_FLOAT:
9903 case MODE_VECTOR_FRACT:
9904 case MODE_VECTOR_UFRACT:
9905 case MODE_VECTOR_ACCUM:
9906 case MODE_VECTOR_UACCUM:
9907 nunits = GET_MODE_NUNITS (mode);
9908 break;
9910 case MODE_INT:
9911 /* Check that there are no leftover bits. */
9912 gcc_assert (GET_MODE_BITSIZE (mode)
9913 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
9915 nunits = GET_MODE_BITSIZE (mode)
9916 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
9917 break;
9919 default:
9920 gcc_unreachable ();
9923 return make_vector_type (innertype, nunits, mode);
9926 /* Similarly, but takes the inner type and number of units, which must be
9927 a power of two. */
9929 tree
9930 build_vector_type (tree innertype, int nunits)
9932 return make_vector_type (innertype, nunits, VOIDmode);
9935 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
9937 tree
9938 build_opaque_vector_type (tree innertype, int nunits)
9940 tree t = make_vector_type (innertype, nunits, VOIDmode);
9941 tree cand;
9942 /* We always build the non-opaque variant before the opaque one,
9943 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
9944 cand = TYPE_NEXT_VARIANT (t);
9945 if (cand
9946 && TYPE_VECTOR_OPAQUE (cand)
9947 && check_qualified_type (cand, t, TYPE_QUALS (t)))
9948 return cand;
9949 /* Othewise build a variant type and make sure to queue it after
9950 the non-opaque type. */
9951 cand = build_distinct_type_copy (t);
9952 TYPE_VECTOR_OPAQUE (cand) = true;
9953 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
9954 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
9955 TYPE_NEXT_VARIANT (t) = cand;
9956 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
9957 return cand;
9961 /* Given an initializer INIT, return TRUE if INIT is zero or some
9962 aggregate of zeros. Otherwise return FALSE. */
9963 bool
9964 initializer_zerop (const_tree init)
9966 tree elt;
9968 STRIP_NOPS (init);
9970 switch (TREE_CODE (init))
9972 case INTEGER_CST:
9973 return integer_zerop (init);
9975 case REAL_CST:
9976 /* ??? Note that this is not correct for C4X float formats. There,
9977 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
9978 negative exponent. */
9979 return real_zerop (init)
9980 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
9982 case FIXED_CST:
9983 return fixed_zerop (init);
9985 case COMPLEX_CST:
9986 return integer_zerop (init)
9987 || (real_zerop (init)
9988 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
9989 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
9991 case VECTOR_CST:
9993 unsigned i;
9994 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
9995 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
9996 return false;
9997 return true;
10000 case CONSTRUCTOR:
10002 unsigned HOST_WIDE_INT idx;
10004 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10005 if (!initializer_zerop (elt))
10006 return false;
10007 return true;
10010 case STRING_CST:
10012 int i;
10014 /* We need to loop through all elements to handle cases like
10015 "\0" and "\0foobar". */
10016 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10017 if (TREE_STRING_POINTER (init)[i] != '\0')
10018 return false;
10020 return true;
10023 default:
10024 return false;
10028 /* Build an empty statement at location LOC. */
10030 tree
10031 build_empty_stmt (location_t loc)
10033 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10034 SET_EXPR_LOCATION (t, loc);
10035 return t;
10039 /* Build an OpenMP clause with code CODE. LOC is the location of the
10040 clause. */
10042 tree
10043 build_omp_clause (location_t loc, enum omp_clause_code code)
10045 tree t;
10046 int size, length;
10048 length = omp_clause_num_ops[code];
10049 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10051 record_node_allocation_statistics (OMP_CLAUSE, size);
10053 t = ggc_alloc_tree_node (size);
10054 memset (t, 0, size);
10055 TREE_SET_CODE (t, OMP_CLAUSE);
10056 OMP_CLAUSE_SET_CODE (t, code);
10057 OMP_CLAUSE_LOCATION (t) = loc;
10059 return t;
10062 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10063 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10064 Except for the CODE and operand count field, other storage for the
10065 object is initialized to zeros. */
10067 tree
10068 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10070 tree t;
10071 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10073 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10074 gcc_assert (len >= 1);
10076 record_node_allocation_statistics (code, length);
10078 t = ggc_alloc_zone_cleared_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
10080 TREE_SET_CODE (t, code);
10082 /* Can't use TREE_OPERAND to store the length because if checking is
10083 enabled, it will try to check the length before we store it. :-P */
10084 t->exp.operands[0] = build_int_cst (sizetype, len);
10086 return t;
10089 /* Helper function for build_call_* functions; build a CALL_EXPR with
10090 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10091 the argument slots. */
10093 static tree
10094 build_call_1 (tree return_type, tree fn, int nargs)
10096 tree t;
10098 t = build_vl_exp (CALL_EXPR, nargs + 3);
10099 TREE_TYPE (t) = return_type;
10100 CALL_EXPR_FN (t) = fn;
10101 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10103 return t;
10106 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10107 FN and a null static chain slot. NARGS is the number of call arguments
10108 which are specified as "..." arguments. */
10110 tree
10111 build_call_nary (tree return_type, tree fn, int nargs, ...)
10113 tree ret;
10114 va_list args;
10115 va_start (args, nargs);
10116 ret = build_call_valist (return_type, fn, nargs, args);
10117 va_end (args);
10118 return ret;
10121 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10122 FN and a null static chain slot. NARGS is the number of call arguments
10123 which are specified as a va_list ARGS. */
10125 tree
10126 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10128 tree t;
10129 int i;
10131 t = build_call_1 (return_type, fn, nargs);
10132 for (i = 0; i < nargs; i++)
10133 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10134 process_call_operands (t);
10135 return t;
10138 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10139 FN and a null static chain slot. NARGS is the number of call arguments
10140 which are specified as a tree array ARGS. */
10142 tree
10143 build_call_array_loc (location_t loc, tree return_type, tree fn,
10144 int nargs, const tree *args)
10146 tree t;
10147 int i;
10149 t = build_call_1 (return_type, fn, nargs);
10150 for (i = 0; i < nargs; i++)
10151 CALL_EXPR_ARG (t, i) = args[i];
10152 process_call_operands (t);
10153 SET_EXPR_LOCATION (t, loc);
10154 return t;
10157 /* Like build_call_array, but takes a vec. */
10159 tree
10160 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10162 tree ret, t;
10163 unsigned int ix;
10165 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10166 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10167 CALL_EXPR_ARG (ret, ix) = t;
10168 process_call_operands (ret);
10169 return ret;
10173 /* Returns true if it is possible to prove that the index of
10174 an array access REF (an ARRAY_REF expression) falls into the
10175 array bounds. */
10177 bool
10178 in_array_bounds_p (tree ref)
10180 tree idx = TREE_OPERAND (ref, 1);
10181 tree min, max;
10183 if (TREE_CODE (idx) != INTEGER_CST)
10184 return false;
10186 min = array_ref_low_bound (ref);
10187 max = array_ref_up_bound (ref);
10188 if (!min
10189 || !max
10190 || TREE_CODE (min) != INTEGER_CST
10191 || TREE_CODE (max) != INTEGER_CST)
10192 return false;
10194 if (tree_int_cst_lt (idx, min)
10195 || tree_int_cst_lt (max, idx))
10196 return false;
10198 return true;
10201 /* Returns true if it is possible to prove that the range of
10202 an array access REF (an ARRAY_RANGE_REF expression) falls
10203 into the array bounds. */
10205 bool
10206 range_in_array_bounds_p (tree ref)
10208 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
10209 tree range_min, range_max, min, max;
10211 range_min = TYPE_MIN_VALUE (domain_type);
10212 range_max = TYPE_MAX_VALUE (domain_type);
10213 if (!range_min
10214 || !range_max
10215 || TREE_CODE (range_min) != INTEGER_CST
10216 || TREE_CODE (range_max) != INTEGER_CST)
10217 return false;
10219 min = array_ref_low_bound (ref);
10220 max = array_ref_up_bound (ref);
10221 if (!min
10222 || !max
10223 || TREE_CODE (min) != INTEGER_CST
10224 || TREE_CODE (max) != INTEGER_CST)
10225 return false;
10227 if (tree_int_cst_lt (range_min, min)
10228 || tree_int_cst_lt (max, range_max))
10229 return false;
10231 return true;
10234 /* Return true if T (assumed to be a DECL) must be assigned a memory
10235 location. */
10237 bool
10238 needs_to_live_in_memory (const_tree t)
10240 return (TREE_ADDRESSABLE (t)
10241 || is_global_var (t)
10242 || (TREE_CODE (t) == RESULT_DECL
10243 && !DECL_BY_REFERENCE (t)
10244 && aggregate_value_p (t, current_function_decl)));
10247 /* Return value of a constant X and sign-extend it. */
10249 HOST_WIDE_INT
10250 int_cst_value (const_tree x)
10252 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10253 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10255 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10256 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10257 || TREE_INT_CST_HIGH (x) == -1);
10259 if (bits < HOST_BITS_PER_WIDE_INT)
10261 bool negative = ((val >> (bits - 1)) & 1) != 0;
10262 if (negative)
10263 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10264 else
10265 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10268 return val;
10271 /* Return value of a constant X and sign-extend it. */
10273 HOST_WIDEST_INT
10274 widest_int_cst_value (const_tree x)
10276 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10277 unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
10279 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
10280 gcc_assert (HOST_BITS_PER_WIDEST_INT >= HOST_BITS_PER_DOUBLE_INT);
10281 val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_HIGH (x))
10282 << HOST_BITS_PER_WIDE_INT);
10283 #else
10284 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10285 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10286 || TREE_INT_CST_HIGH (x) == -1);
10287 #endif
10289 if (bits < HOST_BITS_PER_WIDEST_INT)
10291 bool negative = ((val >> (bits - 1)) & 1) != 0;
10292 if (negative)
10293 val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
10294 else
10295 val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
10298 return val;
10301 /* If TYPE is an integral or pointer type, return an integer type with
10302 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10303 if TYPE is already an integer type of signedness UNSIGNEDP. */
10305 tree
10306 signed_or_unsigned_type_for (int unsignedp, tree type)
10308 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10309 return type;
10311 if (TREE_CODE (type) == VECTOR_TYPE)
10313 tree inner = TREE_TYPE (type);
10314 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10315 if (!inner2)
10316 return NULL_TREE;
10317 if (inner == inner2)
10318 return type;
10319 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10322 if (!INTEGRAL_TYPE_P (type)
10323 && !POINTER_TYPE_P (type))
10324 return NULL_TREE;
10326 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10329 /* If TYPE is an integral or pointer type, return an integer type with
10330 the same precision which is unsigned, or itself if TYPE is already an
10331 unsigned integer type. */
10333 tree
10334 unsigned_type_for (tree type)
10336 return signed_or_unsigned_type_for (1, type);
10339 /* If TYPE is an integral or pointer type, return an integer type with
10340 the same precision which is signed, or itself if TYPE is already a
10341 signed integer type. */
10343 tree
10344 signed_type_for (tree type)
10346 return signed_or_unsigned_type_for (0, type);
10349 /* If TYPE is a vector type, return a signed integer vector type with the
10350 same width and number of subparts. Otherwise return boolean_type_node. */
10352 tree
10353 truth_type_for (tree type)
10355 if (TREE_CODE (type) == VECTOR_TYPE)
10357 tree elem = lang_hooks.types.type_for_size
10358 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10359 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10361 else
10362 return boolean_type_node;
10365 /* Returns the largest value obtainable by casting something in INNER type to
10366 OUTER type. */
10368 tree
10369 upper_bound_in_type (tree outer, tree inner)
10371 double_int high;
10372 unsigned int det = 0;
10373 unsigned oprec = TYPE_PRECISION (outer);
10374 unsigned iprec = TYPE_PRECISION (inner);
10375 unsigned prec;
10377 /* Compute a unique number for every combination. */
10378 det |= (oprec > iprec) ? 4 : 0;
10379 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10380 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10382 /* Determine the exponent to use. */
10383 switch (det)
10385 case 0:
10386 case 1:
10387 /* oprec <= iprec, outer: signed, inner: don't care. */
10388 prec = oprec - 1;
10389 break;
10390 case 2:
10391 case 3:
10392 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10393 prec = oprec;
10394 break;
10395 case 4:
10396 /* oprec > iprec, outer: signed, inner: signed. */
10397 prec = iprec - 1;
10398 break;
10399 case 5:
10400 /* oprec > iprec, outer: signed, inner: unsigned. */
10401 prec = iprec;
10402 break;
10403 case 6:
10404 /* oprec > iprec, outer: unsigned, inner: signed. */
10405 prec = oprec;
10406 break;
10407 case 7:
10408 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10409 prec = iprec;
10410 break;
10411 default:
10412 gcc_unreachable ();
10415 /* Compute 2^^prec - 1. */
10416 if (prec <= HOST_BITS_PER_WIDE_INT)
10418 high.high = 0;
10419 high.low = ((~(unsigned HOST_WIDE_INT) 0)
10420 >> (HOST_BITS_PER_WIDE_INT - prec));
10422 else
10424 high.high = ((~(unsigned HOST_WIDE_INT) 0)
10425 >> (HOST_BITS_PER_DOUBLE_INT - prec));
10426 high.low = ~(unsigned HOST_WIDE_INT) 0;
10429 return double_int_to_tree (outer, high);
10432 /* Returns the smallest value obtainable by casting something in INNER type to
10433 OUTER type. */
10435 tree
10436 lower_bound_in_type (tree outer, tree inner)
10438 double_int low;
10439 unsigned oprec = TYPE_PRECISION (outer);
10440 unsigned iprec = TYPE_PRECISION (inner);
10442 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10443 and obtain 0. */
10444 if (TYPE_UNSIGNED (outer)
10445 /* If we are widening something of an unsigned type, OUTER type
10446 contains all values of INNER type. In particular, both INNER
10447 and OUTER types have zero in common. */
10448 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10449 low.low = low.high = 0;
10450 else
10452 /* If we are widening a signed type to another signed type, we
10453 want to obtain -2^^(iprec-1). If we are keeping the
10454 precision or narrowing to a signed type, we want to obtain
10455 -2^(oprec-1). */
10456 unsigned prec = oprec > iprec ? iprec : oprec;
10458 if (prec <= HOST_BITS_PER_WIDE_INT)
10460 low.high = ~(unsigned HOST_WIDE_INT) 0;
10461 low.low = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
10463 else
10465 low.high = ((~(unsigned HOST_WIDE_INT) 0)
10466 << (prec - HOST_BITS_PER_WIDE_INT - 1));
10467 low.low = 0;
10471 return double_int_to_tree (outer, low);
10474 /* Return nonzero if two operands that are suitable for PHI nodes are
10475 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10476 SSA_NAME or invariant. Note that this is strictly an optimization.
10477 That is, callers of this function can directly call operand_equal_p
10478 and get the same result, only slower. */
10481 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10483 if (arg0 == arg1)
10484 return 1;
10485 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10486 return 0;
10487 return operand_equal_p (arg0, arg1, 0);
10490 /* Returns number of zeros at the end of binary representation of X.
10492 ??? Use ffs if available? */
10494 tree
10495 num_ending_zeros (const_tree x)
10497 unsigned HOST_WIDE_INT fr, nfr;
10498 unsigned num, abits;
10499 tree type = TREE_TYPE (x);
10501 if (TREE_INT_CST_LOW (x) == 0)
10503 num = HOST_BITS_PER_WIDE_INT;
10504 fr = TREE_INT_CST_HIGH (x);
10506 else
10508 num = 0;
10509 fr = TREE_INT_CST_LOW (x);
10512 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
10514 nfr = fr >> abits;
10515 if (nfr << abits == fr)
10517 num += abits;
10518 fr = nfr;
10522 if (num > TYPE_PRECISION (type))
10523 num = TYPE_PRECISION (type);
10525 return build_int_cst_type (type, num);
10529 #define WALK_SUBTREE(NODE) \
10530 do \
10532 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10533 if (result) \
10534 return result; \
10536 while (0)
10538 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10539 be walked whenever a type is seen in the tree. Rest of operands and return
10540 value are as for walk_tree. */
10542 static tree
10543 walk_type_fields (tree type, walk_tree_fn func, void *data,
10544 struct pointer_set_t *pset, walk_tree_lh lh)
10546 tree result = NULL_TREE;
10548 switch (TREE_CODE (type))
10550 case POINTER_TYPE:
10551 case REFERENCE_TYPE:
10552 /* We have to worry about mutually recursive pointers. These can't
10553 be written in C. They can in Ada. It's pathological, but
10554 there's an ACATS test (c38102a) that checks it. Deal with this
10555 by checking if we're pointing to another pointer, that one
10556 points to another pointer, that one does too, and we have no htab.
10557 If so, get a hash table. We check three levels deep to avoid
10558 the cost of the hash table if we don't need one. */
10559 if (POINTER_TYPE_P (TREE_TYPE (type))
10560 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10561 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10562 && !pset)
10564 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10565 func, data);
10566 if (result)
10567 return result;
10569 break;
10572 /* ... fall through ... */
10574 case COMPLEX_TYPE:
10575 WALK_SUBTREE (TREE_TYPE (type));
10576 break;
10578 case METHOD_TYPE:
10579 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10581 /* Fall through. */
10583 case FUNCTION_TYPE:
10584 WALK_SUBTREE (TREE_TYPE (type));
10586 tree arg;
10588 /* We never want to walk into default arguments. */
10589 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10590 WALK_SUBTREE (TREE_VALUE (arg));
10592 break;
10594 case ARRAY_TYPE:
10595 /* Don't follow this nodes's type if a pointer for fear that
10596 we'll have infinite recursion. If we have a PSET, then we
10597 need not fear. */
10598 if (pset
10599 || (!POINTER_TYPE_P (TREE_TYPE (type))
10600 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10601 WALK_SUBTREE (TREE_TYPE (type));
10602 WALK_SUBTREE (TYPE_DOMAIN (type));
10603 break;
10605 case OFFSET_TYPE:
10606 WALK_SUBTREE (TREE_TYPE (type));
10607 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
10608 break;
10610 default:
10611 break;
10614 return NULL_TREE;
10617 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
10618 called with the DATA and the address of each sub-tree. If FUNC returns a
10619 non-NULL value, the traversal is stopped, and the value returned by FUNC
10620 is returned. If PSET is non-NULL it is used to record the nodes visited,
10621 and to avoid visiting a node more than once. */
10623 tree
10624 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
10625 struct pointer_set_t *pset, walk_tree_lh lh)
10627 enum tree_code code;
10628 int walk_subtrees;
10629 tree result;
10631 #define WALK_SUBTREE_TAIL(NODE) \
10632 do \
10634 tp = & (NODE); \
10635 goto tail_recurse; \
10637 while (0)
10639 tail_recurse:
10640 /* Skip empty subtrees. */
10641 if (!*tp)
10642 return NULL_TREE;
10644 /* Don't walk the same tree twice, if the user has requested
10645 that we avoid doing so. */
10646 if (pset && pointer_set_insert (pset, *tp))
10647 return NULL_TREE;
10649 /* Call the function. */
10650 walk_subtrees = 1;
10651 result = (*func) (tp, &walk_subtrees, data);
10653 /* If we found something, return it. */
10654 if (result)
10655 return result;
10657 code = TREE_CODE (*tp);
10659 /* Even if we didn't, FUNC may have decided that there was nothing
10660 interesting below this point in the tree. */
10661 if (!walk_subtrees)
10663 /* But we still need to check our siblings. */
10664 if (code == TREE_LIST)
10665 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10666 else if (code == OMP_CLAUSE)
10667 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10668 else
10669 return NULL_TREE;
10672 if (lh)
10674 result = (*lh) (tp, &walk_subtrees, func, data, pset);
10675 if (result || !walk_subtrees)
10676 return result;
10679 switch (code)
10681 case ERROR_MARK:
10682 case IDENTIFIER_NODE:
10683 case INTEGER_CST:
10684 case REAL_CST:
10685 case FIXED_CST:
10686 case VECTOR_CST:
10687 case STRING_CST:
10688 case BLOCK:
10689 case PLACEHOLDER_EXPR:
10690 case SSA_NAME:
10691 case FIELD_DECL:
10692 case RESULT_DECL:
10693 /* None of these have subtrees other than those already walked
10694 above. */
10695 break;
10697 case TREE_LIST:
10698 WALK_SUBTREE (TREE_VALUE (*tp));
10699 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10700 break;
10702 case TREE_VEC:
10704 int len = TREE_VEC_LENGTH (*tp);
10706 if (len == 0)
10707 break;
10709 /* Walk all elements but the first. */
10710 while (--len)
10711 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
10713 /* Now walk the first one as a tail call. */
10714 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
10717 case COMPLEX_CST:
10718 WALK_SUBTREE (TREE_REALPART (*tp));
10719 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
10721 case CONSTRUCTOR:
10723 unsigned HOST_WIDE_INT idx;
10724 constructor_elt *ce;
10726 for (idx = 0; vec_safe_iterate(CONSTRUCTOR_ELTS (*tp), idx, &ce); idx++)
10727 WALK_SUBTREE (ce->value);
10729 break;
10731 case SAVE_EXPR:
10732 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
10734 case BIND_EXPR:
10736 tree decl;
10737 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
10739 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
10740 into declarations that are just mentioned, rather than
10741 declared; they don't really belong to this part of the tree.
10742 And, we can see cycles: the initializer for a declaration
10743 can refer to the declaration itself. */
10744 WALK_SUBTREE (DECL_INITIAL (decl));
10745 WALK_SUBTREE (DECL_SIZE (decl));
10746 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
10748 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
10751 case STATEMENT_LIST:
10753 tree_stmt_iterator i;
10754 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
10755 WALK_SUBTREE (*tsi_stmt_ptr (i));
10757 break;
10759 case OMP_CLAUSE:
10760 switch (OMP_CLAUSE_CODE (*tp))
10762 case OMP_CLAUSE_PRIVATE:
10763 case OMP_CLAUSE_SHARED:
10764 case OMP_CLAUSE_FIRSTPRIVATE:
10765 case OMP_CLAUSE_COPYIN:
10766 case OMP_CLAUSE_COPYPRIVATE:
10767 case OMP_CLAUSE_FINAL:
10768 case OMP_CLAUSE_IF:
10769 case OMP_CLAUSE_NUM_THREADS:
10770 case OMP_CLAUSE_SCHEDULE:
10771 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
10772 /* FALLTHRU */
10774 case OMP_CLAUSE_NOWAIT:
10775 case OMP_CLAUSE_ORDERED:
10776 case OMP_CLAUSE_DEFAULT:
10777 case OMP_CLAUSE_UNTIED:
10778 case OMP_CLAUSE_MERGEABLE:
10779 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10781 case OMP_CLAUSE_LASTPRIVATE:
10782 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
10783 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
10784 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10786 case OMP_CLAUSE_COLLAPSE:
10788 int i;
10789 for (i = 0; i < 3; i++)
10790 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10791 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10794 case OMP_CLAUSE_REDUCTION:
10796 int i;
10797 for (i = 0; i < 4; i++)
10798 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10799 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10802 default:
10803 gcc_unreachable ();
10805 break;
10807 case TARGET_EXPR:
10809 int i, len;
10811 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
10812 But, we only want to walk once. */
10813 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
10814 for (i = 0; i < len; ++i)
10815 WALK_SUBTREE (TREE_OPERAND (*tp, i));
10816 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
10819 case DECL_EXPR:
10820 /* If this is a TYPE_DECL, walk into the fields of the type that it's
10821 defining. We only want to walk into these fields of a type in this
10822 case and not in the general case of a mere reference to the type.
10824 The criterion is as follows: if the field can be an expression, it
10825 must be walked only here. This should be in keeping with the fields
10826 that are directly gimplified in gimplify_type_sizes in order for the
10827 mark/copy-if-shared/unmark machinery of the gimplifier to work with
10828 variable-sized types.
10830 Note that DECLs get walked as part of processing the BIND_EXPR. */
10831 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
10833 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
10834 if (TREE_CODE (*type_p) == ERROR_MARK)
10835 return NULL_TREE;
10837 /* Call the function for the type. See if it returns anything or
10838 doesn't want us to continue. If we are to continue, walk both
10839 the normal fields and those for the declaration case. */
10840 result = (*func) (type_p, &walk_subtrees, data);
10841 if (result || !walk_subtrees)
10842 return result;
10844 /* But do not walk a pointed-to type since it may itself need to
10845 be walked in the declaration case if it isn't anonymous. */
10846 if (!POINTER_TYPE_P (*type_p))
10848 result = walk_type_fields (*type_p, func, data, pset, lh);
10849 if (result)
10850 return result;
10853 /* If this is a record type, also walk the fields. */
10854 if (RECORD_OR_UNION_TYPE_P (*type_p))
10856 tree field;
10858 for (field = TYPE_FIELDS (*type_p); field;
10859 field = DECL_CHAIN (field))
10861 /* We'd like to look at the type of the field, but we can
10862 easily get infinite recursion. So assume it's pointed
10863 to elsewhere in the tree. Also, ignore things that
10864 aren't fields. */
10865 if (TREE_CODE (field) != FIELD_DECL)
10866 continue;
10868 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
10869 WALK_SUBTREE (DECL_SIZE (field));
10870 WALK_SUBTREE (DECL_SIZE_UNIT (field));
10871 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
10872 WALK_SUBTREE (DECL_QUALIFIER (field));
10876 /* Same for scalar types. */
10877 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
10878 || TREE_CODE (*type_p) == ENUMERAL_TYPE
10879 || TREE_CODE (*type_p) == INTEGER_TYPE
10880 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
10881 || TREE_CODE (*type_p) == REAL_TYPE)
10883 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
10884 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
10887 WALK_SUBTREE (TYPE_SIZE (*type_p));
10888 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
10890 /* FALLTHRU */
10892 default:
10893 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
10895 int i, len;
10897 /* Walk over all the sub-trees of this operand. */
10898 len = TREE_OPERAND_LENGTH (*tp);
10900 /* Go through the subtrees. We need to do this in forward order so
10901 that the scope of a FOR_EXPR is handled properly. */
10902 if (len)
10904 for (i = 0; i < len - 1; ++i)
10905 WALK_SUBTREE (TREE_OPERAND (*tp, i));
10906 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
10909 /* If this is a type, walk the needed fields in the type. */
10910 else if (TYPE_P (*tp))
10911 return walk_type_fields (*tp, func, data, pset, lh);
10912 break;
10915 /* We didn't find what we were looking for. */
10916 return NULL_TREE;
10918 #undef WALK_SUBTREE_TAIL
10920 #undef WALK_SUBTREE
10922 /* Like walk_tree, but does not walk duplicate nodes more than once. */
10924 tree
10925 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
10926 walk_tree_lh lh)
10928 tree result;
10929 struct pointer_set_t *pset;
10931 pset = pointer_set_create ();
10932 result = walk_tree_1 (tp, func, data, pset, lh);
10933 pointer_set_destroy (pset);
10934 return result;
10938 tree
10939 tree_block (tree t)
10941 char const c = TREE_CODE_CLASS (TREE_CODE (t));
10943 if (IS_EXPR_CODE_CLASS (c))
10944 return LOCATION_BLOCK (t->exp.locus);
10945 gcc_unreachable ();
10946 return NULL;
10949 void
10950 tree_set_block (tree t, tree b)
10952 char const c = TREE_CODE_CLASS (TREE_CODE (t));
10954 if (IS_EXPR_CODE_CLASS (c))
10956 if (b)
10957 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
10958 else
10959 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
10961 else
10962 gcc_unreachable ();
10965 /* Create a nameless artificial label and put it in the current
10966 function context. The label has a location of LOC. Returns the
10967 newly created label. */
10969 tree
10970 create_artificial_label (location_t loc)
10972 tree lab = build_decl (loc,
10973 LABEL_DECL, NULL_TREE, void_type_node);
10975 DECL_ARTIFICIAL (lab) = 1;
10976 DECL_IGNORED_P (lab) = 1;
10977 DECL_CONTEXT (lab) = current_function_decl;
10978 return lab;
10981 /* Given a tree, try to return a useful variable name that we can use
10982 to prefix a temporary that is being assigned the value of the tree.
10983 I.E. given <temp> = &A, return A. */
10985 const char *
10986 get_name (tree t)
10988 tree stripped_decl;
10990 stripped_decl = t;
10991 STRIP_NOPS (stripped_decl);
10992 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
10993 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
10994 else if (TREE_CODE (stripped_decl) == SSA_NAME)
10996 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
10997 if (!name)
10998 return NULL;
10999 return IDENTIFIER_POINTER (name);
11001 else
11003 switch (TREE_CODE (stripped_decl))
11005 case ADDR_EXPR:
11006 return get_name (TREE_OPERAND (stripped_decl, 0));
11007 default:
11008 return NULL;
11013 /* Return true if TYPE has a variable argument list. */
11015 bool
11016 stdarg_p (const_tree fntype)
11018 function_args_iterator args_iter;
11019 tree n = NULL_TREE, t;
11021 if (!fntype)
11022 return false;
11024 FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
11026 n = t;
11029 return n != NULL_TREE && n != void_type_node;
11032 /* Return true if TYPE has a prototype. */
11034 bool
11035 prototype_p (tree fntype)
11037 tree t;
11039 gcc_assert (fntype != NULL_TREE);
11041 t = TYPE_ARG_TYPES (fntype);
11042 return (t != NULL_TREE);
11045 /* If BLOCK is inlined from an __attribute__((__artificial__))
11046 routine, return pointer to location from where it has been
11047 called. */
11048 location_t *
11049 block_nonartificial_location (tree block)
11051 location_t *ret = NULL;
11053 while (block && TREE_CODE (block) == BLOCK
11054 && BLOCK_ABSTRACT_ORIGIN (block))
11056 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11058 while (TREE_CODE (ao) == BLOCK
11059 && BLOCK_ABSTRACT_ORIGIN (ao)
11060 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11061 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11063 if (TREE_CODE (ao) == FUNCTION_DECL)
11065 /* If AO is an artificial inline, point RET to the
11066 call site locus at which it has been inlined and continue
11067 the loop, in case AO's caller is also an artificial
11068 inline. */
11069 if (DECL_DECLARED_INLINE_P (ao)
11070 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11071 ret = &BLOCK_SOURCE_LOCATION (block);
11072 else
11073 break;
11075 else if (TREE_CODE (ao) != BLOCK)
11076 break;
11078 block = BLOCK_SUPERCONTEXT (block);
11080 return ret;
11084 /* If EXP is inlined from an __attribute__((__artificial__))
11085 function, return the location of the original call expression. */
11087 location_t
11088 tree_nonartificial_location (tree exp)
11090 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11092 if (loc)
11093 return *loc;
11094 else
11095 return EXPR_LOCATION (exp);
11099 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11100 nodes. */
11102 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11104 static hashval_t
11105 cl_option_hash_hash (const void *x)
11107 const_tree const t = (const_tree) x;
11108 const char *p;
11109 size_t i;
11110 size_t len = 0;
11111 hashval_t hash = 0;
11113 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11115 p = (const char *)TREE_OPTIMIZATION (t);
11116 len = sizeof (struct cl_optimization);
11119 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11121 p = (const char *)TREE_TARGET_OPTION (t);
11122 len = sizeof (struct cl_target_option);
11125 else
11126 gcc_unreachable ();
11128 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11129 something else. */
11130 for (i = 0; i < len; i++)
11131 if (p[i])
11132 hash = (hash << 4) ^ ((i << 2) | p[i]);
11134 return hash;
11137 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11138 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11139 same. */
11141 static int
11142 cl_option_hash_eq (const void *x, const void *y)
11144 const_tree const xt = (const_tree) x;
11145 const_tree const yt = (const_tree) y;
11146 const char *xp;
11147 const char *yp;
11148 size_t len;
11150 if (TREE_CODE (xt) != TREE_CODE (yt))
11151 return 0;
11153 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11155 xp = (const char *)TREE_OPTIMIZATION (xt);
11156 yp = (const char *)TREE_OPTIMIZATION (yt);
11157 len = sizeof (struct cl_optimization);
11160 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11162 xp = (const char *)TREE_TARGET_OPTION (xt);
11163 yp = (const char *)TREE_TARGET_OPTION (yt);
11164 len = sizeof (struct cl_target_option);
11167 else
11168 gcc_unreachable ();
11170 return (memcmp (xp, yp, len) == 0);
11173 /* Build an OPTIMIZATION_NODE based on the current options. */
11175 tree
11176 build_optimization_node (void)
11178 tree t;
11179 void **slot;
11181 /* Use the cache of optimization nodes. */
11183 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11184 &global_options);
11186 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
11187 t = (tree) *slot;
11188 if (!t)
11190 /* Insert this one into the hash table. */
11191 t = cl_optimization_node;
11192 *slot = t;
11194 /* Make a new node for next time round. */
11195 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11198 return t;
11201 /* Build a TARGET_OPTION_NODE based on the current options. */
11203 tree
11204 build_target_option_node (void)
11206 tree t;
11207 void **slot;
11209 /* Use the cache of optimization nodes. */
11211 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11212 &global_options);
11214 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
11215 t = (tree) *slot;
11216 if (!t)
11218 /* Insert this one into the hash table. */
11219 t = cl_target_option_node;
11220 *slot = t;
11222 /* Make a new node for next time round. */
11223 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11226 return t;
11229 /* Determine the "ultimate origin" of a block. The block may be an inlined
11230 instance of an inlined instance of a block which is local to an inline
11231 function, so we have to trace all of the way back through the origin chain
11232 to find out what sort of node actually served as the original seed for the
11233 given block. */
11235 tree
11236 block_ultimate_origin (const_tree block)
11238 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11240 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
11241 nodes in the function to point to themselves; ignore that if
11242 we're trying to output the abstract instance of this function. */
11243 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11244 return NULL_TREE;
11246 if (immediate_origin == NULL_TREE)
11247 return NULL_TREE;
11248 else
11250 tree ret_val;
11251 tree lookahead = immediate_origin;
11255 ret_val = lookahead;
11256 lookahead = (TREE_CODE (ret_val) == BLOCK
11257 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11259 while (lookahead != NULL && lookahead != ret_val);
11261 /* The block's abstract origin chain may not be the *ultimate* origin of
11262 the block. It could lead to a DECL that has an abstract origin set.
11263 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11264 will give us if it has one). Note that DECL's abstract origins are
11265 supposed to be the most distant ancestor (or so decl_ultimate_origin
11266 claims), so we don't need to loop following the DECL origins. */
11267 if (DECL_P (ret_val))
11268 return DECL_ORIGIN (ret_val);
11270 return ret_val;
11274 /* Return true if T1 and T2 are equivalent lists. */
11276 bool
11277 list_equal_p (const_tree t1, const_tree t2)
11279 for (; t1 && t2; t1 = TREE_CHAIN (t1) , t2 = TREE_CHAIN (t2))
11280 if (TREE_VALUE (t1) != TREE_VALUE (t2))
11281 return false;
11282 return !t1 && !t2;
11285 /* Return true iff conversion in EXP generates no instruction. Mark
11286 it inline so that we fully inline into the stripping functions even
11287 though we have two uses of this function. */
11289 static inline bool
11290 tree_nop_conversion (const_tree exp)
11292 tree outer_type, inner_type;
11294 if (!CONVERT_EXPR_P (exp)
11295 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11296 return false;
11297 if (TREE_OPERAND (exp, 0) == error_mark_node)
11298 return false;
11300 outer_type = TREE_TYPE (exp);
11301 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11303 if (!inner_type)
11304 return false;
11306 /* Use precision rather then machine mode when we can, which gives
11307 the correct answer even for submode (bit-field) types. */
11308 if ((INTEGRAL_TYPE_P (outer_type)
11309 || POINTER_TYPE_P (outer_type)
11310 || TREE_CODE (outer_type) == OFFSET_TYPE)
11311 && (INTEGRAL_TYPE_P (inner_type)
11312 || POINTER_TYPE_P (inner_type)
11313 || TREE_CODE (inner_type) == OFFSET_TYPE))
11314 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11316 /* Otherwise fall back on comparing machine modes (e.g. for
11317 aggregate types, floats). */
11318 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11321 /* Return true iff conversion in EXP generates no instruction. Don't
11322 consider conversions changing the signedness. */
11324 static bool
11325 tree_sign_nop_conversion (const_tree exp)
11327 tree outer_type, inner_type;
11329 if (!tree_nop_conversion (exp))
11330 return false;
11332 outer_type = TREE_TYPE (exp);
11333 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11335 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11336 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11339 /* Strip conversions from EXP according to tree_nop_conversion and
11340 return the resulting expression. */
11342 tree
11343 tree_strip_nop_conversions (tree exp)
11345 while (tree_nop_conversion (exp))
11346 exp = TREE_OPERAND (exp, 0);
11347 return exp;
11350 /* Strip conversions from EXP according to tree_sign_nop_conversion
11351 and return the resulting expression. */
11353 tree
11354 tree_strip_sign_nop_conversions (tree exp)
11356 while (tree_sign_nop_conversion (exp))
11357 exp = TREE_OPERAND (exp, 0);
11358 return exp;
11361 /* Avoid any floating point extensions from EXP. */
11362 tree
11363 strip_float_extensions (tree exp)
11365 tree sub, expt, subt;
11367 /* For floating point constant look up the narrowest type that can hold
11368 it properly and handle it like (type)(narrowest_type)constant.
11369 This way we can optimize for instance a=a*2.0 where "a" is float
11370 but 2.0 is double constant. */
11371 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11373 REAL_VALUE_TYPE orig;
11374 tree type = NULL;
11376 orig = TREE_REAL_CST (exp);
11377 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11378 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11379 type = float_type_node;
11380 else if (TYPE_PRECISION (TREE_TYPE (exp))
11381 > TYPE_PRECISION (double_type_node)
11382 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11383 type = double_type_node;
11384 if (type)
11385 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11388 if (!CONVERT_EXPR_P (exp))
11389 return exp;
11391 sub = TREE_OPERAND (exp, 0);
11392 subt = TREE_TYPE (sub);
11393 expt = TREE_TYPE (exp);
11395 if (!FLOAT_TYPE_P (subt))
11396 return exp;
11398 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11399 return exp;
11401 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11402 return exp;
11404 return strip_float_extensions (sub);
11407 /* Strip out all handled components that produce invariant
11408 offsets. */
11410 const_tree
11411 strip_invariant_refs (const_tree op)
11413 while (handled_component_p (op))
11415 switch (TREE_CODE (op))
11417 case ARRAY_REF:
11418 case ARRAY_RANGE_REF:
11419 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11420 || TREE_OPERAND (op, 2) != NULL_TREE
11421 || TREE_OPERAND (op, 3) != NULL_TREE)
11422 return NULL;
11423 break;
11425 case COMPONENT_REF:
11426 if (TREE_OPERAND (op, 2) != NULL_TREE)
11427 return NULL;
11428 break;
11430 default:;
11432 op = TREE_OPERAND (op, 0);
11435 return op;
11438 static GTY(()) tree gcc_eh_personality_decl;
11440 /* Return the GCC personality function decl. */
11442 tree
11443 lhd_gcc_personality (void)
11445 if (!gcc_eh_personality_decl)
11446 gcc_eh_personality_decl = build_personality_function ("gcc");
11447 return gcc_eh_personality_decl;
11450 /* Try to find a base info of BINFO that would have its field decl at offset
11451 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11452 found, return, otherwise return NULL_TREE. */
11454 tree
11455 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
11457 tree type = BINFO_TYPE (binfo);
11459 while (true)
11461 HOST_WIDE_INT pos, size;
11462 tree fld;
11463 int i;
11465 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (expected_type))
11466 return binfo;
11467 if (offset < 0)
11468 return NULL_TREE;
11470 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
11472 if (TREE_CODE (fld) != FIELD_DECL)
11473 continue;
11475 pos = int_bit_position (fld);
11476 size = tree_low_cst (DECL_SIZE (fld), 1);
11477 if (pos <= offset && (pos + size) > offset)
11478 break;
11480 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11481 return NULL_TREE;
11483 if (!DECL_ARTIFICIAL (fld))
11485 binfo = TYPE_BINFO (TREE_TYPE (fld));
11486 if (!binfo)
11487 return NULL_TREE;
11489 /* Offset 0 indicates the primary base, whose vtable contents are
11490 represented in the binfo for the derived class. */
11491 else if (offset != 0)
11493 tree base_binfo, found_binfo = NULL_TREE;
11494 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11495 if (TREE_TYPE (base_binfo) == TREE_TYPE (fld))
11497 found_binfo = base_binfo;
11498 break;
11500 if (!found_binfo)
11501 return NULL_TREE;
11502 binfo = found_binfo;
11505 type = TREE_TYPE (fld);
11506 offset -= pos;
11510 /* Returns true if X is a typedef decl. */
11512 bool
11513 is_typedef_decl (tree x)
11515 return (x && TREE_CODE (x) == TYPE_DECL
11516 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
11519 /* Returns true iff TYPE is a type variant created for a typedef. */
11521 bool
11522 typedef_variant_p (tree type)
11524 return is_typedef_decl (TYPE_NAME (type));
11527 /* Warn about a use of an identifier which was marked deprecated. */
11528 void
11529 warn_deprecated_use (tree node, tree attr)
11531 const char *msg;
11533 if (node == 0 || !warn_deprecated_decl)
11534 return;
11536 if (!attr)
11538 if (DECL_P (node))
11539 attr = DECL_ATTRIBUTES (node);
11540 else if (TYPE_P (node))
11542 tree decl = TYPE_STUB_DECL (node);
11543 if (decl)
11544 attr = lookup_attribute ("deprecated",
11545 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
11549 if (attr)
11550 attr = lookup_attribute ("deprecated", attr);
11552 if (attr)
11553 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
11554 else
11555 msg = NULL;
11557 if (DECL_P (node))
11559 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
11560 if (msg)
11561 warning (OPT_Wdeprecated_declarations,
11562 "%qD is deprecated (declared at %s:%d): %s",
11563 node, xloc.file, xloc.line, msg);
11564 else
11565 warning (OPT_Wdeprecated_declarations,
11566 "%qD is deprecated (declared at %s:%d)",
11567 node, xloc.file, xloc.line);
11569 else if (TYPE_P (node))
11571 tree what = NULL_TREE;
11572 tree decl = TYPE_STUB_DECL (node);
11574 if (TYPE_NAME (node))
11576 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
11577 what = TYPE_NAME (node);
11578 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
11579 && DECL_NAME (TYPE_NAME (node)))
11580 what = DECL_NAME (TYPE_NAME (node));
11583 if (decl)
11585 expanded_location xloc
11586 = expand_location (DECL_SOURCE_LOCATION (decl));
11587 if (what)
11589 if (msg)
11590 warning (OPT_Wdeprecated_declarations,
11591 "%qE is deprecated (declared at %s:%d): %s",
11592 what, xloc.file, xloc.line, msg);
11593 else
11594 warning (OPT_Wdeprecated_declarations,
11595 "%qE is deprecated (declared at %s:%d)", what,
11596 xloc.file, xloc.line);
11598 else
11600 if (msg)
11601 warning (OPT_Wdeprecated_declarations,
11602 "type is deprecated (declared at %s:%d): %s",
11603 xloc.file, xloc.line, msg);
11604 else
11605 warning (OPT_Wdeprecated_declarations,
11606 "type is deprecated (declared at %s:%d)",
11607 xloc.file, xloc.line);
11610 else
11612 if (what)
11614 if (msg)
11615 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
11616 what, msg);
11617 else
11618 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
11620 else
11622 if (msg)
11623 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
11624 msg);
11625 else
11626 warning (OPT_Wdeprecated_declarations, "type is deprecated");
11632 #include "gt-tree.h"