* trans-mem.c (execute_tm_mark): Release bb_regions.
[official-gcc.git] / gcc / tree.c
blob8b2a4f9f7356784ee870a03f25debd14e05872e5
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 /* Size types *are* sign extended. */
1078 bool sign_extended_type = !TYPE_UNSIGNED (type);
1080 double_int ext
1081 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1083 return cst == ext;
1086 /* We force the double_int CST to the range of the type TYPE by sign or
1087 zero extending it. OVERFLOWABLE indicates if we are interested in
1088 overflow of the value, when >0 we are only interested in signed
1089 overflow, for <0 we are interested in any overflow. OVERFLOWED
1090 indicates whether overflow has already occurred. CONST_OVERFLOWED
1091 indicates whether constant overflow has already occurred. We force
1092 T's value to be within range of T's type (by setting to 0 or 1 all
1093 the bits outside the type's range). We set TREE_OVERFLOWED if,
1094 OVERFLOWED is nonzero,
1095 or OVERFLOWABLE is >0 and signed overflow occurs
1096 or OVERFLOWABLE is <0 and any overflow occurs
1097 We return a new tree node for the extended double_int. The node
1098 is shared if no overflow flags are set. */
1101 tree
1102 force_fit_type_double (tree type, double_int cst, int overflowable,
1103 bool overflowed)
1105 bool sign_extended_type;
1107 /* Size types *are* sign extended. */
1108 sign_extended_type = !TYPE_UNSIGNED (type);
1110 /* If we need to set overflow flags, return a new unshared node. */
1111 if (overflowed || !double_int_fits_to_tree_p(type, cst))
1113 if (overflowed
1114 || overflowable < 0
1115 || (overflowable > 0 && sign_extended_type))
1117 tree t = make_node (INTEGER_CST);
1118 TREE_INT_CST (t) = cst.ext (TYPE_PRECISION (type),
1119 !sign_extended_type);
1120 TREE_TYPE (t) = type;
1121 TREE_OVERFLOW (t) = 1;
1122 return t;
1126 /* Else build a shared node. */
1127 return double_int_to_tree (type, cst);
1130 /* These are the hash table functions for the hash table of INTEGER_CST
1131 nodes of a sizetype. */
1133 /* Return the hash code code X, an INTEGER_CST. */
1135 static hashval_t
1136 int_cst_hash_hash (const void *x)
1138 const_tree const t = (const_tree) x;
1140 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1141 ^ htab_hash_pointer (TREE_TYPE (t)));
1144 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1145 is the same as that given by *Y, which is the same. */
1147 static int
1148 int_cst_hash_eq (const void *x, const void *y)
1150 const_tree const xt = (const_tree) x;
1151 const_tree const yt = (const_tree) y;
1153 return (TREE_TYPE (xt) == TREE_TYPE (yt)
1154 && TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1155 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt));
1158 /* Create an INT_CST node of TYPE and value HI:LOW.
1159 The returned node is always shared. For small integers we use a
1160 per-type vector cache, for larger ones we use a single hash table. */
1162 tree
1163 build_int_cst_wide (tree type, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
1165 tree t;
1166 int ix = -1;
1167 int limit = 0;
1169 gcc_assert (type);
1171 switch (TREE_CODE (type))
1173 case NULLPTR_TYPE:
1174 gcc_assert (hi == 0 && low == 0);
1175 /* Fallthru. */
1177 case POINTER_TYPE:
1178 case REFERENCE_TYPE:
1179 /* Cache NULL pointer. */
1180 if (!hi && !low)
1182 limit = 1;
1183 ix = 0;
1185 break;
1187 case BOOLEAN_TYPE:
1188 /* Cache false or true. */
1189 limit = 2;
1190 if (!hi && low < 2)
1191 ix = low;
1192 break;
1194 case INTEGER_TYPE:
1195 case OFFSET_TYPE:
1196 if (TYPE_UNSIGNED (type))
1198 /* Cache 0..N */
1199 limit = INTEGER_SHARE_LIMIT;
1200 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1201 ix = low;
1203 else
1205 /* Cache -1..N */
1206 limit = INTEGER_SHARE_LIMIT + 1;
1207 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1208 ix = low + 1;
1209 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
1210 ix = 0;
1212 break;
1214 case ENUMERAL_TYPE:
1215 break;
1217 default:
1218 gcc_unreachable ();
1221 if (ix >= 0)
1223 /* Look for it in the type's vector of small shared ints. */
1224 if (!TYPE_CACHED_VALUES_P (type))
1226 TYPE_CACHED_VALUES_P (type) = 1;
1227 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1230 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1231 if (t)
1233 /* Make sure no one is clobbering the shared constant. */
1234 gcc_assert (TREE_TYPE (t) == type);
1235 gcc_assert (TREE_INT_CST_LOW (t) == low);
1236 gcc_assert (TREE_INT_CST_HIGH (t) == hi);
1238 else
1240 /* Create a new shared int. */
1241 t = make_node (INTEGER_CST);
1243 TREE_INT_CST_LOW (t) = low;
1244 TREE_INT_CST_HIGH (t) = hi;
1245 TREE_TYPE (t) = type;
1247 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1250 else
1252 /* Use the cache of larger shared ints. */
1253 void **slot;
1255 TREE_INT_CST_LOW (int_cst_node) = low;
1256 TREE_INT_CST_HIGH (int_cst_node) = hi;
1257 TREE_TYPE (int_cst_node) = type;
1259 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
1260 t = (tree) *slot;
1261 if (!t)
1263 /* Insert this one into the hash table. */
1264 t = int_cst_node;
1265 *slot = t;
1266 /* Make a new node for next time round. */
1267 int_cst_node = make_node (INTEGER_CST);
1271 return t;
1274 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1275 and the rest are zeros. */
1277 tree
1278 build_low_bits_mask (tree type, unsigned bits)
1280 double_int mask;
1282 gcc_assert (bits <= TYPE_PRECISION (type));
1284 if (bits == TYPE_PRECISION (type)
1285 && !TYPE_UNSIGNED (type))
1286 /* Sign extended all-ones mask. */
1287 mask = double_int_minus_one;
1288 else
1289 mask = double_int::mask (bits);
1291 return build_int_cst_wide (type, mask.low, mask.high);
1294 /* Checks that X is integer constant that can be expressed in (unsigned)
1295 HOST_WIDE_INT without loss of precision. */
1297 bool
1298 cst_and_fits_in_hwi (const_tree x)
1300 if (TREE_CODE (x) != INTEGER_CST)
1301 return false;
1303 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1304 return false;
1306 return (TREE_INT_CST_HIGH (x) == 0
1307 || TREE_INT_CST_HIGH (x) == -1);
1310 /* Build a newly constructed TREE_VEC node of length LEN. */
1312 tree
1313 make_vector_stat (unsigned len MEM_STAT_DECL)
1315 tree t;
1316 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1318 record_node_allocation_statistics (VECTOR_CST, length);
1320 t = ggc_alloc_zone_cleared_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
1322 TREE_SET_CODE (t, VECTOR_CST);
1323 TREE_CONSTANT (t) = 1;
1325 return t;
1328 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1329 are in a list pointed to by VALS. */
1331 tree
1332 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1334 int over = 0;
1335 unsigned cnt = 0;
1336 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1337 TREE_TYPE (v) = type;
1339 /* Iterate through elements and check for overflow. */
1340 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1342 tree value = vals[cnt];
1344 VECTOR_CST_ELT (v, cnt) = value;
1346 /* Don't crash if we get an address constant. */
1347 if (!CONSTANT_CLASS_P (value))
1348 continue;
1350 over |= TREE_OVERFLOW (value);
1353 TREE_OVERFLOW (v) = over;
1354 return v;
1357 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1358 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1360 tree
1361 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1363 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1364 unsigned HOST_WIDE_INT idx;
1365 tree value;
1367 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1368 vec[idx] = value;
1369 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1370 vec[idx] = build_zero_cst (TREE_TYPE (type));
1372 return build_vector (type, vec);
1375 /* Build a vector of type VECTYPE where all the elements are SCs. */
1376 tree
1377 build_vector_from_val (tree vectype, tree sc)
1379 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1381 if (sc == error_mark_node)
1382 return sc;
1384 /* Verify that the vector type is suitable for SC. Note that there
1385 is some inconsistency in the type-system with respect to restrict
1386 qualifications of pointers. Vector types always have a main-variant
1387 element type and the qualification is applied to the vector-type.
1388 So TREE_TYPE (vector-type) does not return a properly qualified
1389 vector element-type. */
1390 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1391 TREE_TYPE (vectype)));
1393 if (CONSTANT_CLASS_P (sc))
1395 tree *v = XALLOCAVEC (tree, nunits);
1396 for (i = 0; i < nunits; ++i)
1397 v[i] = sc;
1398 return build_vector (vectype, v);
1400 else
1402 vec<constructor_elt, va_gc> *v;
1403 vec_alloc (v, nunits);
1404 for (i = 0; i < nunits; ++i)
1405 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1406 return build_constructor (vectype, v);
1410 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1411 are in the vec pointed to by VALS. */
1412 tree
1413 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1415 tree c = make_node (CONSTRUCTOR);
1416 unsigned int i;
1417 constructor_elt *elt;
1418 bool constant_p = true;
1419 bool side_effects_p = false;
1421 TREE_TYPE (c) = type;
1422 CONSTRUCTOR_ELTS (c) = vals;
1424 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1426 /* Mostly ctors will have elts that don't have side-effects, so
1427 the usual case is to scan all the elements. Hence a single
1428 loop for both const and side effects, rather than one loop
1429 each (with early outs). */
1430 if (!TREE_CONSTANT (elt->value))
1431 constant_p = false;
1432 if (TREE_SIDE_EFFECTS (elt->value))
1433 side_effects_p = true;
1436 TREE_SIDE_EFFECTS (c) = side_effects_p;
1437 TREE_CONSTANT (c) = constant_p;
1439 return c;
1442 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1443 INDEX and VALUE. */
1444 tree
1445 build_constructor_single (tree type, tree index, tree value)
1447 vec<constructor_elt, va_gc> *v;
1448 constructor_elt elt = {index, value};
1450 vec_alloc (v, 1);
1451 v->quick_push (elt);
1453 return build_constructor (type, v);
1457 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1458 are in a list pointed to by VALS. */
1459 tree
1460 build_constructor_from_list (tree type, tree vals)
1462 tree t;
1463 vec<constructor_elt, va_gc> *v = NULL;
1465 if (vals)
1467 vec_alloc (v, list_length (vals));
1468 for (t = vals; t; t = TREE_CHAIN (t))
1469 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1472 return build_constructor (type, v);
1475 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1477 tree
1478 build_fixed (tree type, FIXED_VALUE_TYPE f)
1480 tree v;
1481 FIXED_VALUE_TYPE *fp;
1483 v = make_node (FIXED_CST);
1484 fp = ggc_alloc_fixed_value ();
1485 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1487 TREE_TYPE (v) = type;
1488 TREE_FIXED_CST_PTR (v) = fp;
1489 return v;
1492 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1494 tree
1495 build_real (tree type, REAL_VALUE_TYPE d)
1497 tree v;
1498 REAL_VALUE_TYPE *dp;
1499 int overflow = 0;
1501 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1502 Consider doing it via real_convert now. */
1504 v = make_node (REAL_CST);
1505 dp = ggc_alloc_real_value ();
1506 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1508 TREE_TYPE (v) = type;
1509 TREE_REAL_CST_PTR (v) = dp;
1510 TREE_OVERFLOW (v) = overflow;
1511 return v;
1514 /* Return a new REAL_CST node whose type is TYPE
1515 and whose value is the integer value of the INTEGER_CST node I. */
1517 REAL_VALUE_TYPE
1518 real_value_from_int_cst (const_tree type, const_tree i)
1520 REAL_VALUE_TYPE d;
1522 /* Clear all bits of the real value type so that we can later do
1523 bitwise comparisons to see if two values are the same. */
1524 memset (&d, 0, sizeof d);
1526 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
1527 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
1528 TYPE_UNSIGNED (TREE_TYPE (i)));
1529 return d;
1532 /* Given a tree representing an integer constant I, return a tree
1533 representing the same value as a floating-point constant of type TYPE. */
1535 tree
1536 build_real_from_int_cst (tree type, const_tree i)
1538 tree v;
1539 int overflow = TREE_OVERFLOW (i);
1541 v = build_real (type, real_value_from_int_cst (type, i));
1543 TREE_OVERFLOW (v) |= overflow;
1544 return v;
1547 /* Return a newly constructed STRING_CST node whose value is
1548 the LEN characters at STR.
1549 Note that for a C string literal, LEN should include the trailing NUL.
1550 The TREE_TYPE is not initialized. */
1552 tree
1553 build_string (int len, const char *str)
1555 tree s;
1556 size_t length;
1558 /* Do not waste bytes provided by padding of struct tree_string. */
1559 length = len + offsetof (struct tree_string, str) + 1;
1561 record_node_allocation_statistics (STRING_CST, length);
1563 s = ggc_alloc_tree_node (length);
1565 memset (s, 0, sizeof (struct tree_typed));
1566 TREE_SET_CODE (s, STRING_CST);
1567 TREE_CONSTANT (s) = 1;
1568 TREE_STRING_LENGTH (s) = len;
1569 memcpy (s->string.str, str, len);
1570 s->string.str[len] = '\0';
1572 return s;
1575 /* Return a newly constructed COMPLEX_CST node whose value is
1576 specified by the real and imaginary parts REAL and IMAG.
1577 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1578 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1580 tree
1581 build_complex (tree type, tree real, tree imag)
1583 tree t = make_node (COMPLEX_CST);
1585 TREE_REALPART (t) = real;
1586 TREE_IMAGPART (t) = imag;
1587 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1588 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1589 return t;
1592 /* Return a constant of arithmetic type TYPE which is the
1593 multiplicative identity of the set TYPE. */
1595 tree
1596 build_one_cst (tree type)
1598 switch (TREE_CODE (type))
1600 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1601 case POINTER_TYPE: case REFERENCE_TYPE:
1602 case OFFSET_TYPE:
1603 return build_int_cst (type, 1);
1605 case REAL_TYPE:
1606 return build_real (type, dconst1);
1608 case FIXED_POINT_TYPE:
1609 /* We can only generate 1 for accum types. */
1610 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1611 return build_fixed (type, FCONST1(TYPE_MODE (type)));
1613 case VECTOR_TYPE:
1615 tree scalar = build_one_cst (TREE_TYPE (type));
1617 return build_vector_from_val (type, scalar);
1620 case COMPLEX_TYPE:
1621 return build_complex (type,
1622 build_one_cst (TREE_TYPE (type)),
1623 build_zero_cst (TREE_TYPE (type)));
1625 default:
1626 gcc_unreachable ();
1630 /* Build 0 constant of type TYPE. This is used by constructor folding
1631 and thus the constant should be represented in memory by
1632 zero(es). */
1634 tree
1635 build_zero_cst (tree type)
1637 switch (TREE_CODE (type))
1639 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1640 case POINTER_TYPE: case REFERENCE_TYPE:
1641 case OFFSET_TYPE: case NULLPTR_TYPE:
1642 return build_int_cst (type, 0);
1644 case REAL_TYPE:
1645 return build_real (type, dconst0);
1647 case FIXED_POINT_TYPE:
1648 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
1650 case VECTOR_TYPE:
1652 tree scalar = build_zero_cst (TREE_TYPE (type));
1654 return build_vector_from_val (type, scalar);
1657 case COMPLEX_TYPE:
1659 tree zero = build_zero_cst (TREE_TYPE (type));
1661 return build_complex (type, zero, zero);
1664 default:
1665 if (!AGGREGATE_TYPE_P (type))
1666 return fold_convert (type, integer_zero_node);
1667 return build_constructor (type, NULL);
1672 /* Build a BINFO with LEN language slots. */
1674 tree
1675 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1677 tree t;
1678 size_t length = (offsetof (struct tree_binfo, base_binfos)
1679 + vec<tree, va_gc>::embedded_size (base_binfos));
1681 record_node_allocation_statistics (TREE_BINFO, length);
1683 t = ggc_alloc_zone_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
1685 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1687 TREE_SET_CODE (t, TREE_BINFO);
1689 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
1691 return t;
1694 /* Create a CASE_LABEL_EXPR tree node and return it. */
1696 tree
1697 build_case_label (tree low_value, tree high_value, tree label_decl)
1699 tree t = make_node (CASE_LABEL_EXPR);
1701 TREE_TYPE (t) = void_type_node;
1702 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
1704 CASE_LOW (t) = low_value;
1705 CASE_HIGH (t) = high_value;
1706 CASE_LABEL (t) = label_decl;
1707 CASE_CHAIN (t) = NULL_TREE;
1709 return t;
1712 /* Build a newly constructed TREE_VEC node of length LEN. */
1714 tree
1715 make_tree_vec_stat (int len MEM_STAT_DECL)
1717 tree t;
1718 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1720 record_node_allocation_statistics (TREE_VEC, length);
1722 t = ggc_alloc_zone_cleared_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
1724 TREE_SET_CODE (t, TREE_VEC);
1725 TREE_VEC_LENGTH (t) = len;
1727 return t;
1730 /* Return 1 if EXPR is the integer constant zero or a complex constant
1731 of zero. */
1734 integer_zerop (const_tree expr)
1736 STRIP_NOPS (expr);
1738 switch (TREE_CODE (expr))
1740 case INTEGER_CST:
1741 return (TREE_INT_CST_LOW (expr) == 0
1742 && TREE_INT_CST_HIGH (expr) == 0);
1743 case COMPLEX_CST:
1744 return (integer_zerop (TREE_REALPART (expr))
1745 && integer_zerop (TREE_IMAGPART (expr)));
1746 case VECTOR_CST:
1748 unsigned i;
1749 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1750 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
1751 return false;
1752 return true;
1754 default:
1755 return false;
1759 /* Return 1 if EXPR is the integer constant one or the corresponding
1760 complex constant. */
1763 integer_onep (const_tree expr)
1765 STRIP_NOPS (expr);
1767 switch (TREE_CODE (expr))
1769 case INTEGER_CST:
1770 return (TREE_INT_CST_LOW (expr) == 1
1771 && TREE_INT_CST_HIGH (expr) == 0);
1772 case COMPLEX_CST:
1773 return (integer_onep (TREE_REALPART (expr))
1774 && integer_zerop (TREE_IMAGPART (expr)));
1775 case VECTOR_CST:
1777 unsigned i;
1778 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1779 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
1780 return false;
1781 return true;
1783 default:
1784 return false;
1788 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1789 it contains. Likewise for the corresponding complex constant. */
1792 integer_all_onesp (const_tree expr)
1794 int prec;
1795 int uns;
1797 STRIP_NOPS (expr);
1799 if (TREE_CODE (expr) == COMPLEX_CST
1800 && integer_all_onesp (TREE_REALPART (expr))
1801 && integer_zerop (TREE_IMAGPART (expr)))
1802 return 1;
1804 else if (TREE_CODE (expr) == VECTOR_CST)
1806 unsigned i;
1807 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1808 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
1809 return 0;
1810 return 1;
1813 else if (TREE_CODE (expr) != INTEGER_CST)
1814 return 0;
1816 uns = TYPE_UNSIGNED (TREE_TYPE (expr));
1817 if (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1818 && TREE_INT_CST_HIGH (expr) == -1)
1819 return 1;
1820 if (!uns)
1821 return 0;
1823 prec = TYPE_PRECISION (TREE_TYPE (expr));
1824 if (prec >= HOST_BITS_PER_WIDE_INT)
1826 HOST_WIDE_INT high_value;
1827 int shift_amount;
1829 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1831 /* Can not handle precisions greater than twice the host int size. */
1832 gcc_assert (shift_amount <= HOST_BITS_PER_WIDE_INT);
1833 if (shift_amount == HOST_BITS_PER_WIDE_INT)
1834 /* Shifting by the host word size is undefined according to the ANSI
1835 standard, so we must handle this as a special case. */
1836 high_value = -1;
1837 else
1838 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1840 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1841 && TREE_INT_CST_HIGH (expr) == high_value);
1843 else
1844 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1847 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1848 one bit on). */
1851 integer_pow2p (const_tree expr)
1853 int prec;
1854 unsigned HOST_WIDE_INT high, low;
1856 STRIP_NOPS (expr);
1858 if (TREE_CODE (expr) == COMPLEX_CST
1859 && integer_pow2p (TREE_REALPART (expr))
1860 && integer_zerop (TREE_IMAGPART (expr)))
1861 return 1;
1863 if (TREE_CODE (expr) != INTEGER_CST)
1864 return 0;
1866 prec = TYPE_PRECISION (TREE_TYPE (expr));
1867 high = TREE_INT_CST_HIGH (expr);
1868 low = TREE_INT_CST_LOW (expr);
1870 /* First clear all bits that are beyond the type's precision in case
1871 we've been sign extended. */
1873 if (prec == HOST_BITS_PER_DOUBLE_INT)
1875 else if (prec > HOST_BITS_PER_WIDE_INT)
1876 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1877 else
1879 high = 0;
1880 if (prec < HOST_BITS_PER_WIDE_INT)
1881 low &= ~((HOST_WIDE_INT) (-1) << prec);
1884 if (high == 0 && low == 0)
1885 return 0;
1887 return ((high == 0 && (low & (low - 1)) == 0)
1888 || (low == 0 && (high & (high - 1)) == 0));
1891 /* Return 1 if EXPR is an integer constant other than zero or a
1892 complex constant other than zero. */
1895 integer_nonzerop (const_tree expr)
1897 STRIP_NOPS (expr);
1899 return ((TREE_CODE (expr) == INTEGER_CST
1900 && (TREE_INT_CST_LOW (expr) != 0
1901 || TREE_INT_CST_HIGH (expr) != 0))
1902 || (TREE_CODE (expr) == COMPLEX_CST
1903 && (integer_nonzerop (TREE_REALPART (expr))
1904 || integer_nonzerop (TREE_IMAGPART (expr)))));
1907 /* Return 1 if EXPR is the fixed-point constant zero. */
1910 fixed_zerop (const_tree expr)
1912 return (TREE_CODE (expr) == FIXED_CST
1913 && TREE_FIXED_CST (expr).data.is_zero ());
1916 /* Return the power of two represented by a tree node known to be a
1917 power of two. */
1920 tree_log2 (const_tree expr)
1922 int prec;
1923 HOST_WIDE_INT high, low;
1925 STRIP_NOPS (expr);
1927 if (TREE_CODE (expr) == COMPLEX_CST)
1928 return tree_log2 (TREE_REALPART (expr));
1930 prec = TYPE_PRECISION (TREE_TYPE (expr));
1931 high = TREE_INT_CST_HIGH (expr);
1932 low = TREE_INT_CST_LOW (expr);
1934 /* First clear all bits that are beyond the type's precision in case
1935 we've been sign extended. */
1937 if (prec == HOST_BITS_PER_DOUBLE_INT)
1939 else if (prec > HOST_BITS_PER_WIDE_INT)
1940 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1941 else
1943 high = 0;
1944 if (prec < HOST_BITS_PER_WIDE_INT)
1945 low &= ~((HOST_WIDE_INT) (-1) << prec);
1948 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
1949 : exact_log2 (low));
1952 /* Similar, but return the largest integer Y such that 2 ** Y is less
1953 than or equal to EXPR. */
1956 tree_floor_log2 (const_tree expr)
1958 int prec;
1959 HOST_WIDE_INT high, low;
1961 STRIP_NOPS (expr);
1963 if (TREE_CODE (expr) == COMPLEX_CST)
1964 return tree_log2 (TREE_REALPART (expr));
1966 prec = TYPE_PRECISION (TREE_TYPE (expr));
1967 high = TREE_INT_CST_HIGH (expr);
1968 low = TREE_INT_CST_LOW (expr);
1970 /* First clear all bits that are beyond the type's precision in case
1971 we've been sign extended. Ignore if type's precision hasn't been set
1972 since what we are doing is setting it. */
1974 if (prec == HOST_BITS_PER_DOUBLE_INT || prec == 0)
1976 else if (prec > HOST_BITS_PER_WIDE_INT)
1977 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1978 else
1980 high = 0;
1981 if (prec < HOST_BITS_PER_WIDE_INT)
1982 low &= ~((HOST_WIDE_INT) (-1) << prec);
1985 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
1986 : floor_log2 (low));
1989 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
1990 decimal float constants, so don't return 1 for them. */
1993 real_zerop (const_tree expr)
1995 STRIP_NOPS (expr);
1997 switch (TREE_CODE (expr))
1999 case REAL_CST:
2000 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
2001 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2002 case COMPLEX_CST:
2003 return real_zerop (TREE_REALPART (expr))
2004 && real_zerop (TREE_IMAGPART (expr));
2005 case VECTOR_CST:
2007 unsigned i;
2008 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2009 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2010 return false;
2011 return true;
2013 default:
2014 return false;
2018 /* Return 1 if EXPR is the real constant one in real or complex form.
2019 Trailing zeroes matter for decimal float constants, so don't return
2020 1 for them. */
2023 real_onep (const_tree expr)
2025 STRIP_NOPS (expr);
2027 switch (TREE_CODE (expr))
2029 case REAL_CST:
2030 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
2031 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2032 case COMPLEX_CST:
2033 return real_onep (TREE_REALPART (expr))
2034 && real_zerop (TREE_IMAGPART (expr));
2035 case VECTOR_CST:
2037 unsigned i;
2038 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2039 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2040 return false;
2041 return true;
2043 default:
2044 return false;
2048 /* Return 1 if EXPR is the real constant two. Trailing zeroes matter
2049 for decimal float constants, so don't return 1 for them. */
2052 real_twop (const_tree expr)
2054 STRIP_NOPS (expr);
2056 switch (TREE_CODE (expr))
2058 case REAL_CST:
2059 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2)
2060 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2061 case COMPLEX_CST:
2062 return real_twop (TREE_REALPART (expr))
2063 && real_zerop (TREE_IMAGPART (expr));
2064 case VECTOR_CST:
2066 unsigned i;
2067 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2068 if (!real_twop (VECTOR_CST_ELT (expr, i)))
2069 return false;
2070 return true;
2072 default:
2073 return false;
2077 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2078 matter for decimal float constants, so don't return 1 for them. */
2081 real_minus_onep (const_tree expr)
2083 STRIP_NOPS (expr);
2085 switch (TREE_CODE (expr))
2087 case REAL_CST:
2088 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2089 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2090 case COMPLEX_CST:
2091 return real_minus_onep (TREE_REALPART (expr))
2092 && real_zerop (TREE_IMAGPART (expr));
2093 case VECTOR_CST:
2095 unsigned i;
2096 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2097 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2098 return false;
2099 return true;
2101 default:
2102 return false;
2106 /* Nonzero if EXP is a constant or a cast of a constant. */
2109 really_constant_p (const_tree exp)
2111 /* This is not quite the same as STRIP_NOPS. It does more. */
2112 while (CONVERT_EXPR_P (exp)
2113 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2114 exp = TREE_OPERAND (exp, 0);
2115 return TREE_CONSTANT (exp);
2118 /* Return first list element whose TREE_VALUE is ELEM.
2119 Return 0 if ELEM is not in LIST. */
2121 tree
2122 value_member (tree elem, tree list)
2124 while (list)
2126 if (elem == TREE_VALUE (list))
2127 return list;
2128 list = TREE_CHAIN (list);
2130 return NULL_TREE;
2133 /* Return first list element whose TREE_PURPOSE is ELEM.
2134 Return 0 if ELEM is not in LIST. */
2136 tree
2137 purpose_member (const_tree elem, tree list)
2139 while (list)
2141 if (elem == TREE_PURPOSE (list))
2142 return list;
2143 list = TREE_CHAIN (list);
2145 return NULL_TREE;
2148 /* Return true if ELEM is in V. */
2150 bool
2151 vec_member (const_tree elem, vec<tree, va_gc> *v)
2153 unsigned ix;
2154 tree t;
2155 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2156 if (elem == t)
2157 return true;
2158 return false;
2161 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2162 NULL_TREE. */
2164 tree
2165 chain_index (int idx, tree chain)
2167 for (; chain && idx > 0; --idx)
2168 chain = TREE_CHAIN (chain);
2169 return chain;
2172 /* Return nonzero if ELEM is part of the chain CHAIN. */
2175 chain_member (const_tree elem, const_tree chain)
2177 while (chain)
2179 if (elem == chain)
2180 return 1;
2181 chain = DECL_CHAIN (chain);
2184 return 0;
2187 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2188 We expect a null pointer to mark the end of the chain.
2189 This is the Lisp primitive `length'. */
2192 list_length (const_tree t)
2194 const_tree p = t;
2195 #ifdef ENABLE_TREE_CHECKING
2196 const_tree q = t;
2197 #endif
2198 int len = 0;
2200 while (p)
2202 p = TREE_CHAIN (p);
2203 #ifdef ENABLE_TREE_CHECKING
2204 if (len % 2)
2205 q = TREE_CHAIN (q);
2206 gcc_assert (p != q);
2207 #endif
2208 len++;
2211 return len;
2214 /* Returns the number of FIELD_DECLs in TYPE. */
2217 fields_length (const_tree type)
2219 tree t = TYPE_FIELDS (type);
2220 int count = 0;
2222 for (; t; t = DECL_CHAIN (t))
2223 if (TREE_CODE (t) == FIELD_DECL)
2224 ++count;
2226 return count;
2229 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2230 UNION_TYPE TYPE, or NULL_TREE if none. */
2232 tree
2233 first_field (const_tree type)
2235 tree t = TYPE_FIELDS (type);
2236 while (t && TREE_CODE (t) != FIELD_DECL)
2237 t = TREE_CHAIN (t);
2238 return t;
2241 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2242 by modifying the last node in chain 1 to point to chain 2.
2243 This is the Lisp primitive `nconc'. */
2245 tree
2246 chainon (tree op1, tree op2)
2248 tree t1;
2250 if (!op1)
2251 return op2;
2252 if (!op2)
2253 return op1;
2255 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2256 continue;
2257 TREE_CHAIN (t1) = op2;
2259 #ifdef ENABLE_TREE_CHECKING
2261 tree t2;
2262 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2263 gcc_assert (t2 != t1);
2265 #endif
2267 return op1;
2270 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2272 tree
2273 tree_last (tree chain)
2275 tree next;
2276 if (chain)
2277 while ((next = TREE_CHAIN (chain)))
2278 chain = next;
2279 return chain;
2282 /* Reverse the order of elements in the chain T,
2283 and return the new head of the chain (old last element). */
2285 tree
2286 nreverse (tree t)
2288 tree prev = 0, decl, next;
2289 for (decl = t; decl; decl = next)
2291 /* We shouldn't be using this function to reverse BLOCK chains; we
2292 have blocks_nreverse for that. */
2293 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2294 next = TREE_CHAIN (decl);
2295 TREE_CHAIN (decl) = prev;
2296 prev = decl;
2298 return prev;
2301 /* Return a newly created TREE_LIST node whose
2302 purpose and value fields are PARM and VALUE. */
2304 tree
2305 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2307 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2308 TREE_PURPOSE (t) = parm;
2309 TREE_VALUE (t) = value;
2310 return t;
2313 /* Build a chain of TREE_LIST nodes from a vector. */
2315 tree
2316 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2318 tree ret = NULL_TREE;
2319 tree *pp = &ret;
2320 unsigned int i;
2321 tree t;
2322 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2324 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2325 pp = &TREE_CHAIN (*pp);
2327 return ret;
2330 /* Return a newly created TREE_LIST node whose
2331 purpose and value fields are PURPOSE and VALUE
2332 and whose TREE_CHAIN is CHAIN. */
2334 tree
2335 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2337 tree node;
2339 node = ggc_alloc_zone_tree_node_stat (&tree_zone, sizeof (struct tree_list)
2340 PASS_MEM_STAT);
2341 memset (node, 0, sizeof (struct tree_common));
2343 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2345 TREE_SET_CODE (node, TREE_LIST);
2346 TREE_CHAIN (node) = chain;
2347 TREE_PURPOSE (node) = purpose;
2348 TREE_VALUE (node) = value;
2349 return node;
2352 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2353 trees. */
2355 vec<tree, va_gc> *
2356 ctor_to_vec (tree ctor)
2358 vec<tree, va_gc> *vec;
2359 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2360 unsigned int ix;
2361 tree val;
2363 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2364 vec->quick_push (val);
2366 return vec;
2369 /* Return the size nominally occupied by an object of type TYPE
2370 when it resides in memory. The value is measured in units of bytes,
2371 and its data type is that normally used for type sizes
2372 (which is the first type created by make_signed_type or
2373 make_unsigned_type). */
2375 tree
2376 size_in_bytes (const_tree type)
2378 tree t;
2380 if (type == error_mark_node)
2381 return integer_zero_node;
2383 type = TYPE_MAIN_VARIANT (type);
2384 t = TYPE_SIZE_UNIT (type);
2386 if (t == 0)
2388 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2389 return size_zero_node;
2392 return t;
2395 /* Return the size of TYPE (in bytes) as a wide integer
2396 or return -1 if the size can vary or is larger than an integer. */
2398 HOST_WIDE_INT
2399 int_size_in_bytes (const_tree type)
2401 tree t;
2403 if (type == error_mark_node)
2404 return 0;
2406 type = TYPE_MAIN_VARIANT (type);
2407 t = TYPE_SIZE_UNIT (type);
2408 if (t == 0
2409 || TREE_CODE (t) != INTEGER_CST
2410 || TREE_INT_CST_HIGH (t) != 0
2411 /* If the result would appear negative, it's too big to represent. */
2412 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
2413 return -1;
2415 return TREE_INT_CST_LOW (t);
2418 /* Return the maximum size of TYPE (in bytes) as a wide integer
2419 or return -1 if the size can vary or is larger than an integer. */
2421 HOST_WIDE_INT
2422 max_int_size_in_bytes (const_tree type)
2424 HOST_WIDE_INT size = -1;
2425 tree size_tree;
2427 /* If this is an array type, check for a possible MAX_SIZE attached. */
2429 if (TREE_CODE (type) == ARRAY_TYPE)
2431 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2433 if (size_tree && host_integerp (size_tree, 1))
2434 size = tree_low_cst (size_tree, 1);
2437 /* If we still haven't been able to get a size, see if the language
2438 can compute a maximum size. */
2440 if (size == -1)
2442 size_tree = lang_hooks.types.max_size (type);
2444 if (size_tree && host_integerp (size_tree, 1))
2445 size = tree_low_cst (size_tree, 1);
2448 return size;
2451 /* Returns a tree for the size of EXP in bytes. */
2453 tree
2454 tree_expr_size (const_tree exp)
2456 if (DECL_P (exp)
2457 && DECL_SIZE_UNIT (exp) != 0)
2458 return DECL_SIZE_UNIT (exp);
2459 else
2460 return size_in_bytes (TREE_TYPE (exp));
2463 /* Return the bit position of FIELD, in bits from the start of the record.
2464 This is a tree of type bitsizetype. */
2466 tree
2467 bit_position (const_tree field)
2469 return bit_from_pos (DECL_FIELD_OFFSET (field),
2470 DECL_FIELD_BIT_OFFSET (field));
2473 /* Likewise, but return as an integer. It must be representable in
2474 that way (since it could be a signed value, we don't have the
2475 option of returning -1 like int_size_in_byte can. */
2477 HOST_WIDE_INT
2478 int_bit_position (const_tree field)
2480 return tree_low_cst (bit_position (field), 0);
2483 /* Return the byte position of FIELD, in bytes from the start of the record.
2484 This is a tree of type sizetype. */
2486 tree
2487 byte_position (const_tree field)
2489 return byte_from_pos (DECL_FIELD_OFFSET (field),
2490 DECL_FIELD_BIT_OFFSET (field));
2493 /* Likewise, but return as an integer. It must be representable in
2494 that way (since it could be a signed value, we don't have the
2495 option of returning -1 like int_size_in_byte can. */
2497 HOST_WIDE_INT
2498 int_byte_position (const_tree field)
2500 return tree_low_cst (byte_position (field), 0);
2503 /* Return the strictest alignment, in bits, that T is known to have. */
2505 unsigned int
2506 expr_align (const_tree t)
2508 unsigned int align0, align1;
2510 switch (TREE_CODE (t))
2512 CASE_CONVERT: case NON_LVALUE_EXPR:
2513 /* If we have conversions, we know that the alignment of the
2514 object must meet each of the alignments of the types. */
2515 align0 = expr_align (TREE_OPERAND (t, 0));
2516 align1 = TYPE_ALIGN (TREE_TYPE (t));
2517 return MAX (align0, align1);
2519 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2520 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2521 case CLEANUP_POINT_EXPR:
2522 /* These don't change the alignment of an object. */
2523 return expr_align (TREE_OPERAND (t, 0));
2525 case COND_EXPR:
2526 /* The best we can do is say that the alignment is the least aligned
2527 of the two arms. */
2528 align0 = expr_align (TREE_OPERAND (t, 1));
2529 align1 = expr_align (TREE_OPERAND (t, 2));
2530 return MIN (align0, align1);
2532 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2533 meaningfully, it's always 1. */
2534 case LABEL_DECL: case CONST_DECL:
2535 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2536 case FUNCTION_DECL:
2537 gcc_assert (DECL_ALIGN (t) != 0);
2538 return DECL_ALIGN (t);
2540 default:
2541 break;
2544 /* Otherwise take the alignment from that of the type. */
2545 return TYPE_ALIGN (TREE_TYPE (t));
2548 /* Return, as a tree node, the number of elements for TYPE (which is an
2549 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2551 tree
2552 array_type_nelts (const_tree type)
2554 tree index_type, min, max;
2556 /* If they did it with unspecified bounds, then we should have already
2557 given an error about it before we got here. */
2558 if (! TYPE_DOMAIN (type))
2559 return error_mark_node;
2561 index_type = TYPE_DOMAIN (type);
2562 min = TYPE_MIN_VALUE (index_type);
2563 max = TYPE_MAX_VALUE (index_type);
2565 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
2566 if (!max)
2567 return error_mark_node;
2569 return (integer_zerop (min)
2570 ? max
2571 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2574 /* If arg is static -- a reference to an object in static storage -- then
2575 return the object. This is not the same as the C meaning of `static'.
2576 If arg isn't static, return NULL. */
2578 tree
2579 staticp (tree arg)
2581 switch (TREE_CODE (arg))
2583 case FUNCTION_DECL:
2584 /* Nested functions are static, even though taking their address will
2585 involve a trampoline as we unnest the nested function and create
2586 the trampoline on the tree level. */
2587 return arg;
2589 case VAR_DECL:
2590 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2591 && ! DECL_THREAD_LOCAL_P (arg)
2592 && ! DECL_DLLIMPORT_P (arg)
2593 ? arg : NULL);
2595 case CONST_DECL:
2596 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2597 ? arg : NULL);
2599 case CONSTRUCTOR:
2600 return TREE_STATIC (arg) ? arg : NULL;
2602 case LABEL_DECL:
2603 case STRING_CST:
2604 return arg;
2606 case COMPONENT_REF:
2607 /* If the thing being referenced is not a field, then it is
2608 something language specific. */
2609 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
2611 /* If we are referencing a bitfield, we can't evaluate an
2612 ADDR_EXPR at compile time and so it isn't a constant. */
2613 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2614 return NULL;
2616 return staticp (TREE_OPERAND (arg, 0));
2618 case BIT_FIELD_REF:
2619 return NULL;
2621 case INDIRECT_REF:
2622 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2624 case ARRAY_REF:
2625 case ARRAY_RANGE_REF:
2626 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2627 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2628 return staticp (TREE_OPERAND (arg, 0));
2629 else
2630 return NULL;
2632 case COMPOUND_LITERAL_EXPR:
2633 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
2635 default:
2636 return NULL;
2643 /* Return whether OP is a DECL whose address is function-invariant. */
2645 bool
2646 decl_address_invariant_p (const_tree op)
2648 /* The conditions below are slightly less strict than the one in
2649 staticp. */
2651 switch (TREE_CODE (op))
2653 case PARM_DECL:
2654 case RESULT_DECL:
2655 case LABEL_DECL:
2656 case FUNCTION_DECL:
2657 return true;
2659 case VAR_DECL:
2660 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2661 || DECL_THREAD_LOCAL_P (op)
2662 || DECL_CONTEXT (op) == current_function_decl
2663 || decl_function_context (op) == current_function_decl)
2664 return true;
2665 break;
2667 case CONST_DECL:
2668 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2669 || decl_function_context (op) == current_function_decl)
2670 return true;
2671 break;
2673 default:
2674 break;
2677 return false;
2680 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
2682 bool
2683 decl_address_ip_invariant_p (const_tree op)
2685 /* The conditions below are slightly less strict than the one in
2686 staticp. */
2688 switch (TREE_CODE (op))
2690 case LABEL_DECL:
2691 case FUNCTION_DECL:
2692 case STRING_CST:
2693 return true;
2695 case VAR_DECL:
2696 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2697 && !DECL_DLLIMPORT_P (op))
2698 || DECL_THREAD_LOCAL_P (op))
2699 return true;
2700 break;
2702 case CONST_DECL:
2703 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
2704 return true;
2705 break;
2707 default:
2708 break;
2711 return false;
2715 /* Return true if T is function-invariant (internal function, does
2716 not handle arithmetic; that's handled in skip_simple_arithmetic and
2717 tree_invariant_p). */
2719 static bool tree_invariant_p (tree t);
2721 static bool
2722 tree_invariant_p_1 (tree t)
2724 tree op;
2726 if (TREE_CONSTANT (t)
2727 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
2728 return true;
2730 switch (TREE_CODE (t))
2732 case SAVE_EXPR:
2733 return true;
2735 case ADDR_EXPR:
2736 op = TREE_OPERAND (t, 0);
2737 while (handled_component_p (op))
2739 switch (TREE_CODE (op))
2741 case ARRAY_REF:
2742 case ARRAY_RANGE_REF:
2743 if (!tree_invariant_p (TREE_OPERAND (op, 1))
2744 || TREE_OPERAND (op, 2) != NULL_TREE
2745 || TREE_OPERAND (op, 3) != NULL_TREE)
2746 return false;
2747 break;
2749 case COMPONENT_REF:
2750 if (TREE_OPERAND (op, 2) != NULL_TREE)
2751 return false;
2752 break;
2754 default:;
2756 op = TREE_OPERAND (op, 0);
2759 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2761 default:
2762 break;
2765 return false;
2768 /* Return true if T is function-invariant. */
2770 static bool
2771 tree_invariant_p (tree t)
2773 tree inner = skip_simple_arithmetic (t);
2774 return tree_invariant_p_1 (inner);
2777 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
2778 Do this to any expression which may be used in more than one place,
2779 but must be evaluated only once.
2781 Normally, expand_expr would reevaluate the expression each time.
2782 Calling save_expr produces something that is evaluated and recorded
2783 the first time expand_expr is called on it. Subsequent calls to
2784 expand_expr just reuse the recorded value.
2786 The call to expand_expr that generates code that actually computes
2787 the value is the first call *at compile time*. Subsequent calls
2788 *at compile time* generate code to use the saved value.
2789 This produces correct result provided that *at run time* control
2790 always flows through the insns made by the first expand_expr
2791 before reaching the other places where the save_expr was evaluated.
2792 You, the caller of save_expr, must make sure this is so.
2794 Constants, and certain read-only nodes, are returned with no
2795 SAVE_EXPR because that is safe. Expressions containing placeholders
2796 are not touched; see tree.def for an explanation of what these
2797 are used for. */
2799 tree
2800 save_expr (tree expr)
2802 tree t = fold (expr);
2803 tree inner;
2805 /* If the tree evaluates to a constant, then we don't want to hide that
2806 fact (i.e. this allows further folding, and direct checks for constants).
2807 However, a read-only object that has side effects cannot be bypassed.
2808 Since it is no problem to reevaluate literals, we just return the
2809 literal node. */
2810 inner = skip_simple_arithmetic (t);
2811 if (TREE_CODE (inner) == ERROR_MARK)
2812 return inner;
2814 if (tree_invariant_p_1 (inner))
2815 return t;
2817 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
2818 it means that the size or offset of some field of an object depends on
2819 the value within another field.
2821 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
2822 and some variable since it would then need to be both evaluated once and
2823 evaluated more than once. Front-ends must assure this case cannot
2824 happen by surrounding any such subexpressions in their own SAVE_EXPR
2825 and forcing evaluation at the proper time. */
2826 if (contains_placeholder_p (inner))
2827 return t;
2829 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
2830 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
2832 /* This expression might be placed ahead of a jump to ensure that the
2833 value was computed on both sides of the jump. So make sure it isn't
2834 eliminated as dead. */
2835 TREE_SIDE_EFFECTS (t) = 1;
2836 return t;
2839 /* Look inside EXPR and into any simple arithmetic operations. Return
2840 the innermost non-arithmetic node. */
2842 tree
2843 skip_simple_arithmetic (tree expr)
2845 tree inner;
2847 /* We don't care about whether this can be used as an lvalue in this
2848 context. */
2849 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
2850 expr = TREE_OPERAND (expr, 0);
2852 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
2853 a constant, it will be more efficient to not make another SAVE_EXPR since
2854 it will allow better simplification and GCSE will be able to merge the
2855 computations if they actually occur. */
2856 inner = expr;
2857 while (1)
2859 if (UNARY_CLASS_P (inner))
2860 inner = TREE_OPERAND (inner, 0);
2861 else if (BINARY_CLASS_P (inner))
2863 if (tree_invariant_p (TREE_OPERAND (inner, 1)))
2864 inner = TREE_OPERAND (inner, 0);
2865 else if (tree_invariant_p (TREE_OPERAND (inner, 0)))
2866 inner = TREE_OPERAND (inner, 1);
2867 else
2868 break;
2870 else
2871 break;
2874 return inner;
2878 /* Return which tree structure is used by T. */
2880 enum tree_node_structure_enum
2881 tree_node_structure (const_tree t)
2883 const enum tree_code code = TREE_CODE (t);
2884 return tree_node_structure_for_code (code);
2887 /* Set various status flags when building a CALL_EXPR object T. */
2889 static void
2890 process_call_operands (tree t)
2892 bool side_effects = TREE_SIDE_EFFECTS (t);
2893 bool read_only = false;
2894 int i = call_expr_flags (t);
2896 /* Calls have side-effects, except those to const or pure functions. */
2897 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
2898 side_effects = true;
2899 /* Propagate TREE_READONLY of arguments for const functions. */
2900 if (i & ECF_CONST)
2901 read_only = true;
2903 if (!side_effects || read_only)
2904 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
2906 tree op = TREE_OPERAND (t, i);
2907 if (op && TREE_SIDE_EFFECTS (op))
2908 side_effects = true;
2909 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
2910 read_only = false;
2913 TREE_SIDE_EFFECTS (t) = side_effects;
2914 TREE_READONLY (t) = read_only;
2917 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
2918 size or offset that depends on a field within a record. */
2920 bool
2921 contains_placeholder_p (const_tree exp)
2923 enum tree_code code;
2925 if (!exp)
2926 return 0;
2928 code = TREE_CODE (exp);
2929 if (code == PLACEHOLDER_EXPR)
2930 return 1;
2932 switch (TREE_CODE_CLASS (code))
2934 case tcc_reference:
2935 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
2936 position computations since they will be converted into a
2937 WITH_RECORD_EXPR involving the reference, which will assume
2938 here will be valid. */
2939 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2941 case tcc_exceptional:
2942 if (code == TREE_LIST)
2943 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
2944 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
2945 break;
2947 case tcc_unary:
2948 case tcc_binary:
2949 case tcc_comparison:
2950 case tcc_expression:
2951 switch (code)
2953 case COMPOUND_EXPR:
2954 /* Ignoring the first operand isn't quite right, but works best. */
2955 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
2957 case COND_EXPR:
2958 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2959 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
2960 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
2962 case SAVE_EXPR:
2963 /* The save_expr function never wraps anything containing
2964 a PLACEHOLDER_EXPR. */
2965 return 0;
2967 default:
2968 break;
2971 switch (TREE_CODE_LENGTH (code))
2973 case 1:
2974 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2975 case 2:
2976 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2977 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
2978 default:
2979 return 0;
2982 case tcc_vl_exp:
2983 switch (code)
2985 case CALL_EXPR:
2987 const_tree arg;
2988 const_call_expr_arg_iterator iter;
2989 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
2990 if (CONTAINS_PLACEHOLDER_P (arg))
2991 return 1;
2992 return 0;
2994 default:
2995 return 0;
2998 default:
2999 return 0;
3001 return 0;
3004 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3005 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3006 field positions. */
3008 static bool
3009 type_contains_placeholder_1 (const_tree type)
3011 /* If the size contains a placeholder or the parent type (component type in
3012 the case of arrays) type involves a placeholder, this type does. */
3013 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3014 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3015 || (!POINTER_TYPE_P (type)
3016 && TREE_TYPE (type)
3017 && type_contains_placeholder_p (TREE_TYPE (type))))
3018 return true;
3020 /* Now do type-specific checks. Note that the last part of the check above
3021 greatly limits what we have to do below. */
3022 switch (TREE_CODE (type))
3024 case VOID_TYPE:
3025 case COMPLEX_TYPE:
3026 case ENUMERAL_TYPE:
3027 case BOOLEAN_TYPE:
3028 case POINTER_TYPE:
3029 case OFFSET_TYPE:
3030 case REFERENCE_TYPE:
3031 case METHOD_TYPE:
3032 case FUNCTION_TYPE:
3033 case VECTOR_TYPE:
3034 case NULLPTR_TYPE:
3035 return false;
3037 case INTEGER_TYPE:
3038 case REAL_TYPE:
3039 case FIXED_POINT_TYPE:
3040 /* Here we just check the bounds. */
3041 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3042 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3044 case ARRAY_TYPE:
3045 /* We have already checked the component type above, so just check the
3046 domain type. */
3047 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3049 case RECORD_TYPE:
3050 case UNION_TYPE:
3051 case QUAL_UNION_TYPE:
3053 tree field;
3055 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3056 if (TREE_CODE (field) == FIELD_DECL
3057 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3058 || (TREE_CODE (type) == QUAL_UNION_TYPE
3059 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3060 || type_contains_placeholder_p (TREE_TYPE (field))))
3061 return true;
3063 return false;
3066 default:
3067 gcc_unreachable ();
3071 /* Wrapper around above function used to cache its result. */
3073 bool
3074 type_contains_placeholder_p (tree type)
3076 bool result;
3078 /* If the contains_placeholder_bits field has been initialized,
3079 then we know the answer. */
3080 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3081 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3083 /* Indicate that we've seen this type node, and the answer is false.
3084 This is what we want to return if we run into recursion via fields. */
3085 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3087 /* Compute the real value. */
3088 result = type_contains_placeholder_1 (type);
3090 /* Store the real value. */
3091 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3093 return result;
3096 /* Push tree EXP onto vector QUEUE if it is not already present. */
3098 static void
3099 push_without_duplicates (tree exp, vec<tree> *queue)
3101 unsigned int i;
3102 tree iter;
3104 FOR_EACH_VEC_ELT (*queue, i, iter)
3105 if (simple_cst_equal (iter, exp) == 1)
3106 break;
3108 if (!iter)
3109 queue->safe_push (exp);
3112 /* Given a tree EXP, find all occurrences of references to fields
3113 in a PLACEHOLDER_EXPR and place them in vector REFS without
3114 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3115 we assume here that EXP contains only arithmetic expressions
3116 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3117 argument list. */
3119 void
3120 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3122 enum tree_code code = TREE_CODE (exp);
3123 tree inner;
3124 int i;
3126 /* We handle TREE_LIST and COMPONENT_REF separately. */
3127 if (code == TREE_LIST)
3129 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3130 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3132 else if (code == COMPONENT_REF)
3134 for (inner = TREE_OPERAND (exp, 0);
3135 REFERENCE_CLASS_P (inner);
3136 inner = TREE_OPERAND (inner, 0))
3139 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3140 push_without_duplicates (exp, refs);
3141 else
3142 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3144 else
3145 switch (TREE_CODE_CLASS (code))
3147 case tcc_constant:
3148 break;
3150 case tcc_declaration:
3151 /* Variables allocated to static storage can stay. */
3152 if (!TREE_STATIC (exp))
3153 push_without_duplicates (exp, refs);
3154 break;
3156 case tcc_expression:
3157 /* This is the pattern built in ada/make_aligning_type. */
3158 if (code == ADDR_EXPR
3159 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3161 push_without_duplicates (exp, refs);
3162 break;
3165 /* Fall through... */
3167 case tcc_exceptional:
3168 case tcc_unary:
3169 case tcc_binary:
3170 case tcc_comparison:
3171 case tcc_reference:
3172 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3173 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3174 break;
3176 case tcc_vl_exp:
3177 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3178 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3179 break;
3181 default:
3182 gcc_unreachable ();
3186 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3187 return a tree with all occurrences of references to F in a
3188 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3189 CONST_DECLs. Note that we assume here that EXP contains only
3190 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3191 occurring only in their argument list. */
3193 tree
3194 substitute_in_expr (tree exp, tree f, tree r)
3196 enum tree_code code = TREE_CODE (exp);
3197 tree op0, op1, op2, op3;
3198 tree new_tree;
3200 /* We handle TREE_LIST and COMPONENT_REF separately. */
3201 if (code == TREE_LIST)
3203 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3204 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3205 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3206 return exp;
3208 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3210 else if (code == COMPONENT_REF)
3212 tree inner;
3214 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3215 and it is the right field, replace it with R. */
3216 for (inner = TREE_OPERAND (exp, 0);
3217 REFERENCE_CLASS_P (inner);
3218 inner = TREE_OPERAND (inner, 0))
3221 /* The field. */
3222 op1 = TREE_OPERAND (exp, 1);
3224 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3225 return r;
3227 /* If this expression hasn't been completed let, leave it alone. */
3228 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3229 return exp;
3231 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3232 if (op0 == TREE_OPERAND (exp, 0))
3233 return exp;
3235 new_tree
3236 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3238 else
3239 switch (TREE_CODE_CLASS (code))
3241 case tcc_constant:
3242 return exp;
3244 case tcc_declaration:
3245 if (exp == f)
3246 return r;
3247 else
3248 return exp;
3250 case tcc_expression:
3251 if (exp == f)
3252 return r;
3254 /* Fall through... */
3256 case tcc_exceptional:
3257 case tcc_unary:
3258 case tcc_binary:
3259 case tcc_comparison:
3260 case tcc_reference:
3261 switch (TREE_CODE_LENGTH (code))
3263 case 0:
3264 return exp;
3266 case 1:
3267 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3268 if (op0 == TREE_OPERAND (exp, 0))
3269 return exp;
3271 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3272 break;
3274 case 2:
3275 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3276 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3278 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3279 return exp;
3281 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3282 break;
3284 case 3:
3285 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3286 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3287 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3289 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3290 && op2 == TREE_OPERAND (exp, 2))
3291 return exp;
3293 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3294 break;
3296 case 4:
3297 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3298 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3299 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3300 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3302 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3303 && op2 == TREE_OPERAND (exp, 2)
3304 && op3 == TREE_OPERAND (exp, 3))
3305 return exp;
3307 new_tree
3308 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3309 break;
3311 default:
3312 gcc_unreachable ();
3314 break;
3316 case tcc_vl_exp:
3318 int i;
3320 new_tree = NULL_TREE;
3322 /* If we are trying to replace F with a constant, inline back
3323 functions which do nothing else than computing a value from
3324 the arguments they are passed. This makes it possible to
3325 fold partially or entirely the replacement expression. */
3326 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3328 tree t = maybe_inline_call_in_expr (exp);
3329 if (t)
3330 return SUBSTITUTE_IN_EXPR (t, f, r);
3333 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3335 tree op = TREE_OPERAND (exp, i);
3336 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3337 if (new_op != op)
3339 if (!new_tree)
3340 new_tree = copy_node (exp);
3341 TREE_OPERAND (new_tree, i) = new_op;
3345 if (new_tree)
3347 new_tree = fold (new_tree);
3348 if (TREE_CODE (new_tree) == CALL_EXPR)
3349 process_call_operands (new_tree);
3351 else
3352 return exp;
3354 break;
3356 default:
3357 gcc_unreachable ();
3360 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3362 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3363 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3365 return new_tree;
3368 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3369 for it within OBJ, a tree that is an object or a chain of references. */
3371 tree
3372 substitute_placeholder_in_expr (tree exp, tree obj)
3374 enum tree_code code = TREE_CODE (exp);
3375 tree op0, op1, op2, op3;
3376 tree new_tree;
3378 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3379 in the chain of OBJ. */
3380 if (code == PLACEHOLDER_EXPR)
3382 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3383 tree elt;
3385 for (elt = obj; elt != 0;
3386 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3387 || TREE_CODE (elt) == COND_EXPR)
3388 ? TREE_OPERAND (elt, 1)
3389 : (REFERENCE_CLASS_P (elt)
3390 || UNARY_CLASS_P (elt)
3391 || BINARY_CLASS_P (elt)
3392 || VL_EXP_CLASS_P (elt)
3393 || EXPRESSION_CLASS_P (elt))
3394 ? TREE_OPERAND (elt, 0) : 0))
3395 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3396 return elt;
3398 for (elt = obj; elt != 0;
3399 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3400 || TREE_CODE (elt) == COND_EXPR)
3401 ? TREE_OPERAND (elt, 1)
3402 : (REFERENCE_CLASS_P (elt)
3403 || UNARY_CLASS_P (elt)
3404 || BINARY_CLASS_P (elt)
3405 || VL_EXP_CLASS_P (elt)
3406 || EXPRESSION_CLASS_P (elt))
3407 ? TREE_OPERAND (elt, 0) : 0))
3408 if (POINTER_TYPE_P (TREE_TYPE (elt))
3409 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3410 == need_type))
3411 return fold_build1 (INDIRECT_REF, need_type, elt);
3413 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3414 survives until RTL generation, there will be an error. */
3415 return exp;
3418 /* TREE_LIST is special because we need to look at TREE_VALUE
3419 and TREE_CHAIN, not TREE_OPERANDS. */
3420 else if (code == TREE_LIST)
3422 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3423 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3424 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3425 return exp;
3427 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3429 else
3430 switch (TREE_CODE_CLASS (code))
3432 case tcc_constant:
3433 case tcc_declaration:
3434 return exp;
3436 case tcc_exceptional:
3437 case tcc_unary:
3438 case tcc_binary:
3439 case tcc_comparison:
3440 case tcc_expression:
3441 case tcc_reference:
3442 case tcc_statement:
3443 switch (TREE_CODE_LENGTH (code))
3445 case 0:
3446 return exp;
3448 case 1:
3449 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3450 if (op0 == TREE_OPERAND (exp, 0))
3451 return exp;
3453 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3454 break;
3456 case 2:
3457 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3458 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3460 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3461 return exp;
3463 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3464 break;
3466 case 3:
3467 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3468 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3469 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3471 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3472 && op2 == TREE_OPERAND (exp, 2))
3473 return exp;
3475 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3476 break;
3478 case 4:
3479 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3480 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3481 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3482 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3484 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3485 && op2 == TREE_OPERAND (exp, 2)
3486 && op3 == TREE_OPERAND (exp, 3))
3487 return exp;
3489 new_tree
3490 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3491 break;
3493 default:
3494 gcc_unreachable ();
3496 break;
3498 case tcc_vl_exp:
3500 int i;
3502 new_tree = NULL_TREE;
3504 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3506 tree op = TREE_OPERAND (exp, i);
3507 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
3508 if (new_op != op)
3510 if (!new_tree)
3511 new_tree = copy_node (exp);
3512 TREE_OPERAND (new_tree, i) = new_op;
3516 if (new_tree)
3518 new_tree = fold (new_tree);
3519 if (TREE_CODE (new_tree) == CALL_EXPR)
3520 process_call_operands (new_tree);
3522 else
3523 return exp;
3525 break;
3527 default:
3528 gcc_unreachable ();
3531 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3533 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3534 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3536 return new_tree;
3539 /* Stabilize a reference so that we can use it any number of times
3540 without causing its operands to be evaluated more than once.
3541 Returns the stabilized reference. This works by means of save_expr,
3542 so see the caveats in the comments about save_expr.
3544 Also allows conversion expressions whose operands are references.
3545 Any other kind of expression is returned unchanged. */
3547 tree
3548 stabilize_reference (tree ref)
3550 tree result;
3551 enum tree_code code = TREE_CODE (ref);
3553 switch (code)
3555 case VAR_DECL:
3556 case PARM_DECL:
3557 case RESULT_DECL:
3558 /* No action is needed in this case. */
3559 return ref;
3561 CASE_CONVERT:
3562 case FLOAT_EXPR:
3563 case FIX_TRUNC_EXPR:
3564 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
3565 break;
3567 case INDIRECT_REF:
3568 result = build_nt (INDIRECT_REF,
3569 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
3570 break;
3572 case COMPONENT_REF:
3573 result = build_nt (COMPONENT_REF,
3574 stabilize_reference (TREE_OPERAND (ref, 0)),
3575 TREE_OPERAND (ref, 1), NULL_TREE);
3576 break;
3578 case BIT_FIELD_REF:
3579 result = build_nt (BIT_FIELD_REF,
3580 stabilize_reference (TREE_OPERAND (ref, 0)),
3581 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
3582 break;
3584 case ARRAY_REF:
3585 result = build_nt (ARRAY_REF,
3586 stabilize_reference (TREE_OPERAND (ref, 0)),
3587 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3588 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3589 break;
3591 case ARRAY_RANGE_REF:
3592 result = build_nt (ARRAY_RANGE_REF,
3593 stabilize_reference (TREE_OPERAND (ref, 0)),
3594 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3595 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3596 break;
3598 case COMPOUND_EXPR:
3599 /* We cannot wrap the first expression in a SAVE_EXPR, as then
3600 it wouldn't be ignored. This matters when dealing with
3601 volatiles. */
3602 return stabilize_reference_1 (ref);
3604 /* If arg isn't a kind of lvalue we recognize, make no change.
3605 Caller should recognize the error for an invalid lvalue. */
3606 default:
3607 return ref;
3609 case ERROR_MARK:
3610 return error_mark_node;
3613 TREE_TYPE (result) = TREE_TYPE (ref);
3614 TREE_READONLY (result) = TREE_READONLY (ref);
3615 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
3616 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
3618 return result;
3621 /* Subroutine of stabilize_reference; this is called for subtrees of
3622 references. Any expression with side-effects must be put in a SAVE_EXPR
3623 to ensure that it is only evaluated once.
3625 We don't put SAVE_EXPR nodes around everything, because assigning very
3626 simple expressions to temporaries causes us to miss good opportunities
3627 for optimizations. Among other things, the opportunity to fold in the
3628 addition of a constant into an addressing mode often gets lost, e.g.
3629 "y[i+1] += x;". In general, we take the approach that we should not make
3630 an assignment unless we are forced into it - i.e., that any non-side effect
3631 operator should be allowed, and that cse should take care of coalescing
3632 multiple utterances of the same expression should that prove fruitful. */
3634 tree
3635 stabilize_reference_1 (tree e)
3637 tree result;
3638 enum tree_code code = TREE_CODE (e);
3640 /* We cannot ignore const expressions because it might be a reference
3641 to a const array but whose index contains side-effects. But we can
3642 ignore things that are actual constant or that already have been
3643 handled by this function. */
3645 if (tree_invariant_p (e))
3646 return e;
3648 switch (TREE_CODE_CLASS (code))
3650 case tcc_exceptional:
3651 case tcc_type:
3652 case tcc_declaration:
3653 case tcc_comparison:
3654 case tcc_statement:
3655 case tcc_expression:
3656 case tcc_reference:
3657 case tcc_vl_exp:
3658 /* If the expression has side-effects, then encase it in a SAVE_EXPR
3659 so that it will only be evaluated once. */
3660 /* The reference (r) and comparison (<) classes could be handled as
3661 below, but it is generally faster to only evaluate them once. */
3662 if (TREE_SIDE_EFFECTS (e))
3663 return save_expr (e);
3664 return e;
3666 case tcc_constant:
3667 /* Constants need no processing. In fact, we should never reach
3668 here. */
3669 return e;
3671 case tcc_binary:
3672 /* Division is slow and tends to be compiled with jumps,
3673 especially the division by powers of 2 that is often
3674 found inside of an array reference. So do it just once. */
3675 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3676 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3677 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3678 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3679 return save_expr (e);
3680 /* Recursively stabilize each operand. */
3681 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3682 stabilize_reference_1 (TREE_OPERAND (e, 1)));
3683 break;
3685 case tcc_unary:
3686 /* Recursively stabilize each operand. */
3687 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3688 break;
3690 default:
3691 gcc_unreachable ();
3694 TREE_TYPE (result) = TREE_TYPE (e);
3695 TREE_READONLY (result) = TREE_READONLY (e);
3696 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3697 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3699 return result;
3702 /* Low-level constructors for expressions. */
3704 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
3705 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
3707 void
3708 recompute_tree_invariant_for_addr_expr (tree t)
3710 tree node;
3711 bool tc = true, se = false;
3713 /* We started out assuming this address is both invariant and constant, but
3714 does not have side effects. Now go down any handled components and see if
3715 any of them involve offsets that are either non-constant or non-invariant.
3716 Also check for side-effects.
3718 ??? Note that this code makes no attempt to deal with the case where
3719 taking the address of something causes a copy due to misalignment. */
3721 #define UPDATE_FLAGS(NODE) \
3722 do { tree _node = (NODE); \
3723 if (_node && !TREE_CONSTANT (_node)) tc = false; \
3724 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
3726 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
3727 node = TREE_OPERAND (node, 0))
3729 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
3730 array reference (probably made temporarily by the G++ front end),
3731 so ignore all the operands. */
3732 if ((TREE_CODE (node) == ARRAY_REF
3733 || TREE_CODE (node) == ARRAY_RANGE_REF)
3734 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
3736 UPDATE_FLAGS (TREE_OPERAND (node, 1));
3737 if (TREE_OPERAND (node, 2))
3738 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3739 if (TREE_OPERAND (node, 3))
3740 UPDATE_FLAGS (TREE_OPERAND (node, 3));
3742 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
3743 FIELD_DECL, apparently. The G++ front end can put something else
3744 there, at least temporarily. */
3745 else if (TREE_CODE (node) == COMPONENT_REF
3746 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
3748 if (TREE_OPERAND (node, 2))
3749 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3753 node = lang_hooks.expr_to_decl (node, &tc, &se);
3755 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
3756 the address, since &(*a)->b is a form of addition. If it's a constant, the
3757 address is constant too. If it's a decl, its address is constant if the
3758 decl is static. Everything else is not constant and, furthermore,
3759 taking the address of a volatile variable is not volatile. */
3760 if (TREE_CODE (node) == INDIRECT_REF
3761 || TREE_CODE (node) == MEM_REF)
3762 UPDATE_FLAGS (TREE_OPERAND (node, 0));
3763 else if (CONSTANT_CLASS_P (node))
3765 else if (DECL_P (node))
3766 tc &= (staticp (node) != NULL_TREE);
3767 else
3769 tc = false;
3770 se |= TREE_SIDE_EFFECTS (node);
3774 TREE_CONSTANT (t) = tc;
3775 TREE_SIDE_EFFECTS (t) = se;
3776 #undef UPDATE_FLAGS
3779 /* Build an expression of code CODE, data type TYPE, and operands as
3780 specified. Expressions and reference nodes can be created this way.
3781 Constants, decls, types and misc nodes cannot be.
3783 We define 5 non-variadic functions, from 0 to 4 arguments. This is
3784 enough for all extant tree codes. */
3786 tree
3787 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
3789 tree t;
3791 gcc_assert (TREE_CODE_LENGTH (code) == 0);
3793 t = make_node_stat (code PASS_MEM_STAT);
3794 TREE_TYPE (t) = tt;
3796 return t;
3799 tree
3800 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
3802 int length = sizeof (struct tree_exp);
3803 tree t;
3805 record_node_allocation_statistics (code, length);
3807 gcc_assert (TREE_CODE_LENGTH (code) == 1);
3809 t = ggc_alloc_zone_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
3811 memset (t, 0, sizeof (struct tree_common));
3813 TREE_SET_CODE (t, code);
3815 TREE_TYPE (t) = type;
3816 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
3817 TREE_OPERAND (t, 0) = node;
3818 if (node && !TYPE_P (node))
3820 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
3821 TREE_READONLY (t) = TREE_READONLY (node);
3824 if (TREE_CODE_CLASS (code) == tcc_statement)
3825 TREE_SIDE_EFFECTS (t) = 1;
3826 else switch (code)
3828 case VA_ARG_EXPR:
3829 /* All of these have side-effects, no matter what their
3830 operands are. */
3831 TREE_SIDE_EFFECTS (t) = 1;
3832 TREE_READONLY (t) = 0;
3833 break;
3835 case INDIRECT_REF:
3836 /* Whether a dereference is readonly has nothing to do with whether
3837 its operand is readonly. */
3838 TREE_READONLY (t) = 0;
3839 break;
3841 case ADDR_EXPR:
3842 if (node)
3843 recompute_tree_invariant_for_addr_expr (t);
3844 break;
3846 default:
3847 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
3848 && node && !TYPE_P (node)
3849 && TREE_CONSTANT (node))
3850 TREE_CONSTANT (t) = 1;
3851 if (TREE_CODE_CLASS (code) == tcc_reference
3852 && node && TREE_THIS_VOLATILE (node))
3853 TREE_THIS_VOLATILE (t) = 1;
3854 break;
3857 return t;
3860 #define PROCESS_ARG(N) \
3861 do { \
3862 TREE_OPERAND (t, N) = arg##N; \
3863 if (arg##N &&!TYPE_P (arg##N)) \
3865 if (TREE_SIDE_EFFECTS (arg##N)) \
3866 side_effects = 1; \
3867 if (!TREE_READONLY (arg##N) \
3868 && !CONSTANT_CLASS_P (arg##N)) \
3869 (void) (read_only = 0); \
3870 if (!TREE_CONSTANT (arg##N)) \
3871 (void) (constant = 0); \
3873 } while (0)
3875 tree
3876 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
3878 bool constant, read_only, side_effects;
3879 tree t;
3881 gcc_assert (TREE_CODE_LENGTH (code) == 2);
3883 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
3884 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
3885 /* When sizetype precision doesn't match that of pointers
3886 we need to be able to build explicit extensions or truncations
3887 of the offset argument. */
3888 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
3889 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
3890 && TREE_CODE (arg1) == INTEGER_CST);
3892 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
3893 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
3894 && ptrofftype_p (TREE_TYPE (arg1)));
3896 t = make_node_stat (code PASS_MEM_STAT);
3897 TREE_TYPE (t) = tt;
3899 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
3900 result based on those same flags for the arguments. But if the
3901 arguments aren't really even `tree' expressions, we shouldn't be trying
3902 to do this. */
3904 /* Expressions without side effects may be constant if their
3905 arguments are as well. */
3906 constant = (TREE_CODE_CLASS (code) == tcc_comparison
3907 || TREE_CODE_CLASS (code) == tcc_binary);
3908 read_only = 1;
3909 side_effects = TREE_SIDE_EFFECTS (t);
3911 PROCESS_ARG(0);
3912 PROCESS_ARG(1);
3914 TREE_READONLY (t) = read_only;
3915 TREE_CONSTANT (t) = constant;
3916 TREE_SIDE_EFFECTS (t) = side_effects;
3917 TREE_THIS_VOLATILE (t)
3918 = (TREE_CODE_CLASS (code) == tcc_reference
3919 && arg0 && TREE_THIS_VOLATILE (arg0));
3921 return t;
3925 tree
3926 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3927 tree arg2 MEM_STAT_DECL)
3929 bool constant, read_only, side_effects;
3930 tree t;
3932 gcc_assert (TREE_CODE_LENGTH (code) == 3);
3933 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3935 t = make_node_stat (code PASS_MEM_STAT);
3936 TREE_TYPE (t) = tt;
3938 read_only = 1;
3940 /* As a special exception, if COND_EXPR has NULL branches, we
3941 assume that it is a gimple statement and always consider
3942 it to have side effects. */
3943 if (code == COND_EXPR
3944 && tt == void_type_node
3945 && arg1 == NULL_TREE
3946 && arg2 == NULL_TREE)
3947 side_effects = true;
3948 else
3949 side_effects = TREE_SIDE_EFFECTS (t);
3951 PROCESS_ARG(0);
3952 PROCESS_ARG(1);
3953 PROCESS_ARG(2);
3955 if (code == COND_EXPR)
3956 TREE_READONLY (t) = read_only;
3958 TREE_SIDE_EFFECTS (t) = side_effects;
3959 TREE_THIS_VOLATILE (t)
3960 = (TREE_CODE_CLASS (code) == tcc_reference
3961 && arg0 && TREE_THIS_VOLATILE (arg0));
3963 return t;
3966 tree
3967 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3968 tree arg2, tree arg3 MEM_STAT_DECL)
3970 bool constant, read_only, side_effects;
3971 tree t;
3973 gcc_assert (TREE_CODE_LENGTH (code) == 4);
3975 t = make_node_stat (code PASS_MEM_STAT);
3976 TREE_TYPE (t) = tt;
3978 side_effects = TREE_SIDE_EFFECTS (t);
3980 PROCESS_ARG(0);
3981 PROCESS_ARG(1);
3982 PROCESS_ARG(2);
3983 PROCESS_ARG(3);
3985 TREE_SIDE_EFFECTS (t) = side_effects;
3986 TREE_THIS_VOLATILE (t)
3987 = (TREE_CODE_CLASS (code) == tcc_reference
3988 && arg0 && TREE_THIS_VOLATILE (arg0));
3990 return t;
3993 tree
3994 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3995 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
3997 bool constant, read_only, side_effects;
3998 tree t;
4000 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4002 t = make_node_stat (code PASS_MEM_STAT);
4003 TREE_TYPE (t) = tt;
4005 side_effects = TREE_SIDE_EFFECTS (t);
4007 PROCESS_ARG(0);
4008 PROCESS_ARG(1);
4009 PROCESS_ARG(2);
4010 PROCESS_ARG(3);
4011 PROCESS_ARG(4);
4013 TREE_SIDE_EFFECTS (t) = side_effects;
4014 TREE_THIS_VOLATILE (t)
4015 = (TREE_CODE_CLASS (code) == tcc_reference
4016 && arg0 && TREE_THIS_VOLATILE (arg0));
4018 return t;
4021 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4022 on the pointer PTR. */
4024 tree
4025 build_simple_mem_ref_loc (location_t loc, tree ptr)
4027 HOST_WIDE_INT offset = 0;
4028 tree ptype = TREE_TYPE (ptr);
4029 tree tem;
4030 /* For convenience allow addresses that collapse to a simple base
4031 and offset. */
4032 if (TREE_CODE (ptr) == ADDR_EXPR
4033 && (handled_component_p (TREE_OPERAND (ptr, 0))
4034 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4036 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4037 gcc_assert (ptr);
4038 ptr = build_fold_addr_expr (ptr);
4039 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4041 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4042 ptr, build_int_cst (ptype, offset));
4043 SET_EXPR_LOCATION (tem, loc);
4044 return tem;
4047 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4049 double_int
4050 mem_ref_offset (const_tree t)
4052 tree toff = TREE_OPERAND (t, 1);
4053 return tree_to_double_int (toff).sext (TYPE_PRECISION (TREE_TYPE (toff)));
4056 /* Return the pointer-type relevant for TBAA purposes from the
4057 gimple memory reference tree T. This is the type to be used for
4058 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T. */
4060 tree
4061 reference_alias_ptr_type (const_tree t)
4063 const_tree base = t;
4064 while (handled_component_p (base))
4065 base = TREE_OPERAND (base, 0);
4066 if (TREE_CODE (base) == MEM_REF)
4067 return TREE_TYPE (TREE_OPERAND (base, 1));
4068 else if (TREE_CODE (base) == TARGET_MEM_REF)
4069 return TREE_TYPE (TMR_OFFSET (base));
4070 else
4071 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (base)));
4074 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4075 offsetted by OFFSET units. */
4077 tree
4078 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4080 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4081 build_fold_addr_expr (base),
4082 build_int_cst (ptr_type_node, offset));
4083 tree addr = build1 (ADDR_EXPR, type, ref);
4084 recompute_tree_invariant_for_addr_expr (addr);
4085 return addr;
4088 /* Similar except don't specify the TREE_TYPE
4089 and leave the TREE_SIDE_EFFECTS as 0.
4090 It is permissible for arguments to be null,
4091 or even garbage if their values do not matter. */
4093 tree
4094 build_nt (enum tree_code code, ...)
4096 tree t;
4097 int length;
4098 int i;
4099 va_list p;
4101 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4103 va_start (p, code);
4105 t = make_node (code);
4106 length = TREE_CODE_LENGTH (code);
4108 for (i = 0; i < length; i++)
4109 TREE_OPERAND (t, i) = va_arg (p, tree);
4111 va_end (p);
4112 return t;
4115 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4116 tree vec. */
4118 tree
4119 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4121 tree ret, t;
4122 unsigned int ix;
4124 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4125 CALL_EXPR_FN (ret) = fn;
4126 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4127 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4128 CALL_EXPR_ARG (ret, ix) = t;
4129 return ret;
4132 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4133 We do NOT enter this node in any sort of symbol table.
4135 LOC is the location of the decl.
4137 layout_decl is used to set up the decl's storage layout.
4138 Other slots are initialized to 0 or null pointers. */
4140 tree
4141 build_decl_stat (location_t loc, enum tree_code code, tree name,
4142 tree type MEM_STAT_DECL)
4144 tree t;
4146 t = make_node_stat (code PASS_MEM_STAT);
4147 DECL_SOURCE_LOCATION (t) = loc;
4149 /* if (type == error_mark_node)
4150 type = integer_type_node; */
4151 /* That is not done, deliberately, so that having error_mark_node
4152 as the type can suppress useless errors in the use of this variable. */
4154 DECL_NAME (t) = name;
4155 TREE_TYPE (t) = type;
4157 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4158 layout_decl (t, 0);
4160 return t;
4163 /* Builds and returns function declaration with NAME and TYPE. */
4165 tree
4166 build_fn_decl (const char *name, tree type)
4168 tree id = get_identifier (name);
4169 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4171 DECL_EXTERNAL (decl) = 1;
4172 TREE_PUBLIC (decl) = 1;
4173 DECL_ARTIFICIAL (decl) = 1;
4174 TREE_NOTHROW (decl) = 1;
4176 return decl;
4179 vec<tree, va_gc> *all_translation_units;
4181 /* Builds a new translation-unit decl with name NAME, queues it in the
4182 global list of translation-unit decls and returns it. */
4184 tree
4185 build_translation_unit_decl (tree name)
4187 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4188 name, NULL_TREE);
4189 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4190 vec_safe_push (all_translation_units, tu);
4191 return tu;
4195 /* BLOCK nodes are used to represent the structure of binding contours
4196 and declarations, once those contours have been exited and their contents
4197 compiled. This information is used for outputting debugging info. */
4199 tree
4200 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4202 tree block = make_node (BLOCK);
4204 BLOCK_VARS (block) = vars;
4205 BLOCK_SUBBLOCKS (block) = subblocks;
4206 BLOCK_SUPERCONTEXT (block) = supercontext;
4207 BLOCK_CHAIN (block) = chain;
4208 return block;
4212 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4214 LOC is the location to use in tree T. */
4216 void
4217 protected_set_expr_location (tree t, location_t loc)
4219 if (t && CAN_HAVE_LOCATION_P (t))
4220 SET_EXPR_LOCATION (t, loc);
4223 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4224 is ATTRIBUTE. */
4226 tree
4227 build_decl_attribute_variant (tree ddecl, tree attribute)
4229 DECL_ATTRIBUTES (ddecl) = attribute;
4230 return ddecl;
4233 /* Borrowed from hashtab.c iterative_hash implementation. */
4234 #define mix(a,b,c) \
4236 a -= b; a -= c; a ^= (c>>13); \
4237 b -= c; b -= a; b ^= (a<< 8); \
4238 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
4239 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
4240 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
4241 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
4242 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
4243 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
4244 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
4248 /* Produce good hash value combining VAL and VAL2. */
4249 hashval_t
4250 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
4252 /* the golden ratio; an arbitrary value. */
4253 hashval_t a = 0x9e3779b9;
4255 mix (a, val, val2);
4256 return val2;
4259 /* Produce good hash value combining VAL and VAL2. */
4260 hashval_t
4261 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
4263 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
4264 return iterative_hash_hashval_t (val, val2);
4265 else
4267 hashval_t a = (hashval_t) val;
4268 /* Avoid warnings about shifting of more than the width of the type on
4269 hosts that won't execute this path. */
4270 int zero = 0;
4271 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
4272 mix (a, b, val2);
4273 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
4275 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
4276 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
4277 mix (a, b, val2);
4279 return val2;
4283 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4284 is ATTRIBUTE and its qualifiers are QUALS.
4286 Record such modified types already made so we don't make duplicates. */
4288 tree
4289 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4291 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4293 hashval_t hashcode = 0;
4294 tree ntype;
4295 enum tree_code code = TREE_CODE (ttype);
4297 /* Building a distinct copy of a tagged type is inappropriate; it
4298 causes breakage in code that expects there to be a one-to-one
4299 relationship between a struct and its fields.
4300 build_duplicate_type is another solution (as used in
4301 handle_transparent_union_attribute), but that doesn't play well
4302 with the stronger C++ type identity model. */
4303 if (TREE_CODE (ttype) == RECORD_TYPE
4304 || TREE_CODE (ttype) == UNION_TYPE
4305 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4306 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4308 warning (OPT_Wattributes,
4309 "ignoring attributes applied to %qT after definition",
4310 TYPE_MAIN_VARIANT (ttype));
4311 return build_qualified_type (ttype, quals);
4314 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4315 ntype = build_distinct_type_copy (ttype);
4317 TYPE_ATTRIBUTES (ntype) = attribute;
4319 hashcode = iterative_hash_object (code, hashcode);
4320 if (TREE_TYPE (ntype))
4321 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
4322 hashcode);
4323 hashcode = attribute_hash_list (attribute, hashcode);
4325 switch (TREE_CODE (ntype))
4327 case FUNCTION_TYPE:
4328 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
4329 break;
4330 case ARRAY_TYPE:
4331 if (TYPE_DOMAIN (ntype))
4332 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
4333 hashcode);
4334 break;
4335 case INTEGER_TYPE:
4336 hashcode = iterative_hash_object
4337 (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
4338 hashcode = iterative_hash_object
4339 (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
4340 break;
4341 case REAL_TYPE:
4342 case FIXED_POINT_TYPE:
4344 unsigned int precision = TYPE_PRECISION (ntype);
4345 hashcode = iterative_hash_object (precision, hashcode);
4347 break;
4348 default:
4349 break;
4352 ntype = type_hash_canon (hashcode, ntype);
4354 /* If the target-dependent attributes make NTYPE different from
4355 its canonical type, we will need to use structural equality
4356 checks for this type. */
4357 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4358 || !comp_type_attributes (ntype, ttype))
4359 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4360 else if (TYPE_CANONICAL (ntype) == ntype)
4361 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4363 ttype = build_qualified_type (ntype, quals);
4365 else if (TYPE_QUALS (ttype) != quals)
4366 ttype = build_qualified_type (ttype, quals);
4368 return ttype;
4371 /* Compare two attributes for their value identity. Return true if the
4372 attribute values are known to be equal; otherwise return false.
4375 static bool
4376 attribute_value_equal (const_tree attr1, const_tree attr2)
4378 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4379 return true;
4381 if (TREE_VALUE (attr1) != NULL_TREE
4382 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4383 && TREE_VALUE (attr2) != NULL
4384 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4385 return (simple_cst_list_equal (TREE_VALUE (attr1),
4386 TREE_VALUE (attr2)) == 1);
4388 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4391 /* Return 0 if the attributes for two types are incompatible, 1 if they
4392 are compatible, and 2 if they are nearly compatible (which causes a
4393 warning to be generated). */
4395 comp_type_attributes (const_tree type1, const_tree type2)
4397 const_tree a1 = TYPE_ATTRIBUTES (type1);
4398 const_tree a2 = TYPE_ATTRIBUTES (type2);
4399 const_tree a;
4401 if (a1 == a2)
4402 return 1;
4403 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4405 const struct attribute_spec *as;
4406 const_tree attr;
4408 as = lookup_attribute_spec (get_attribute_name (a));
4409 if (!as || as->affects_type_identity == false)
4410 continue;
4412 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4413 if (!attr || !attribute_value_equal (a, attr))
4414 break;
4416 if (!a)
4418 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
4420 const struct attribute_spec *as;
4422 as = lookup_attribute_spec (get_attribute_name (a));
4423 if (!as || as->affects_type_identity == false)
4424 continue;
4426 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
4427 break;
4428 /* We don't need to compare trees again, as we did this
4429 already in first loop. */
4431 /* All types - affecting identity - are equal, so
4432 there is no need to call target hook for comparison. */
4433 if (!a)
4434 return 1;
4436 /* As some type combinations - like default calling-convention - might
4437 be compatible, we have to call the target hook to get the final result. */
4438 return targetm.comp_type_attributes (type1, type2);
4441 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4442 is ATTRIBUTE.
4444 Record such modified types already made so we don't make duplicates. */
4446 tree
4447 build_type_attribute_variant (tree ttype, tree attribute)
4449 return build_type_attribute_qual_variant (ttype, attribute,
4450 TYPE_QUALS (ttype));
4454 /* Reset the expression *EXPR_P, a size or position.
4456 ??? We could reset all non-constant sizes or positions. But it's cheap
4457 enough to not do so and refrain from adding workarounds to dwarf2out.c.
4459 We need to reset self-referential sizes or positions because they cannot
4460 be gimplified and thus can contain a CALL_EXPR after the gimplification
4461 is finished, which will run afoul of LTO streaming. And they need to be
4462 reset to something essentially dummy but not constant, so as to preserve
4463 the properties of the object they are attached to. */
4465 static inline void
4466 free_lang_data_in_one_sizepos (tree *expr_p)
4468 tree expr = *expr_p;
4469 if (CONTAINS_PLACEHOLDER_P (expr))
4470 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
4474 /* Reset all the fields in a binfo node BINFO. We only keep
4475 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
4477 static void
4478 free_lang_data_in_binfo (tree binfo)
4480 unsigned i;
4481 tree t;
4483 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
4485 BINFO_VIRTUALS (binfo) = NULL_TREE;
4486 BINFO_BASE_ACCESSES (binfo) = NULL;
4487 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
4488 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
4490 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
4491 free_lang_data_in_binfo (t);
4495 /* Reset all language specific information still present in TYPE. */
4497 static void
4498 free_lang_data_in_type (tree type)
4500 gcc_assert (TYPE_P (type));
4502 /* Give the FE a chance to remove its own data first. */
4503 lang_hooks.free_lang_data (type);
4505 TREE_LANG_FLAG_0 (type) = 0;
4506 TREE_LANG_FLAG_1 (type) = 0;
4507 TREE_LANG_FLAG_2 (type) = 0;
4508 TREE_LANG_FLAG_3 (type) = 0;
4509 TREE_LANG_FLAG_4 (type) = 0;
4510 TREE_LANG_FLAG_5 (type) = 0;
4511 TREE_LANG_FLAG_6 (type) = 0;
4513 if (TREE_CODE (type) == FUNCTION_TYPE)
4515 /* Remove the const and volatile qualifiers from arguments. The
4516 C++ front end removes them, but the C front end does not,
4517 leading to false ODR violation errors when merging two
4518 instances of the same function signature compiled by
4519 different front ends. */
4520 tree p;
4522 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
4524 tree arg_type = TREE_VALUE (p);
4526 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
4528 int quals = TYPE_QUALS (arg_type)
4529 & ~TYPE_QUAL_CONST
4530 & ~TYPE_QUAL_VOLATILE;
4531 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
4532 free_lang_data_in_type (TREE_VALUE (p));
4537 /* Remove members that are not actually FIELD_DECLs from the field
4538 list of an aggregate. These occur in C++. */
4539 if (RECORD_OR_UNION_TYPE_P (type))
4541 tree prev, member;
4543 /* Note that TYPE_FIELDS can be shared across distinct
4544 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
4545 to be removed, we cannot set its TREE_CHAIN to NULL.
4546 Otherwise, we would not be able to find all the other fields
4547 in the other instances of this TREE_TYPE.
4549 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
4550 prev = NULL_TREE;
4551 member = TYPE_FIELDS (type);
4552 while (member)
4554 if (TREE_CODE (member) == FIELD_DECL
4555 || TREE_CODE (member) == TYPE_DECL)
4557 if (prev)
4558 TREE_CHAIN (prev) = member;
4559 else
4560 TYPE_FIELDS (type) = member;
4561 prev = member;
4564 member = TREE_CHAIN (member);
4567 if (prev)
4568 TREE_CHAIN (prev) = NULL_TREE;
4569 else
4570 TYPE_FIELDS (type) = NULL_TREE;
4572 TYPE_METHODS (type) = NULL_TREE;
4573 if (TYPE_BINFO (type))
4574 free_lang_data_in_binfo (TYPE_BINFO (type));
4576 else
4578 /* For non-aggregate types, clear out the language slot (which
4579 overloads TYPE_BINFO). */
4580 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
4582 if (INTEGRAL_TYPE_P (type)
4583 || SCALAR_FLOAT_TYPE_P (type)
4584 || FIXED_POINT_TYPE_P (type))
4586 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
4587 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
4591 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
4592 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
4594 if (TYPE_CONTEXT (type)
4595 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
4597 tree ctx = TYPE_CONTEXT (type);
4600 ctx = BLOCK_SUPERCONTEXT (ctx);
4602 while (ctx && TREE_CODE (ctx) == BLOCK);
4603 TYPE_CONTEXT (type) = ctx;
4608 /* Return true if DECL may need an assembler name to be set. */
4610 static inline bool
4611 need_assembler_name_p (tree decl)
4613 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
4614 if (TREE_CODE (decl) != FUNCTION_DECL
4615 && TREE_CODE (decl) != VAR_DECL)
4616 return false;
4618 /* If DECL already has its assembler name set, it does not need a
4619 new one. */
4620 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
4621 || DECL_ASSEMBLER_NAME_SET_P (decl))
4622 return false;
4624 /* Abstract decls do not need an assembler name. */
4625 if (DECL_ABSTRACT (decl))
4626 return false;
4628 /* For VAR_DECLs, only static, public and external symbols need an
4629 assembler name. */
4630 if (TREE_CODE (decl) == VAR_DECL
4631 && !TREE_STATIC (decl)
4632 && !TREE_PUBLIC (decl)
4633 && !DECL_EXTERNAL (decl))
4634 return false;
4636 if (TREE_CODE (decl) == FUNCTION_DECL)
4638 /* Do not set assembler name on builtins. Allow RTL expansion to
4639 decide whether to expand inline or via a regular call. */
4640 if (DECL_BUILT_IN (decl)
4641 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
4642 return false;
4644 /* Functions represented in the callgraph need an assembler name. */
4645 if (cgraph_get_node (decl) != NULL)
4646 return true;
4648 /* Unused and not public functions don't need an assembler name. */
4649 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
4650 return false;
4653 return true;
4657 /* Reset all language specific information still present in symbol
4658 DECL. */
4660 static void
4661 free_lang_data_in_decl (tree decl)
4663 gcc_assert (DECL_P (decl));
4665 /* Give the FE a chance to remove its own data first. */
4666 lang_hooks.free_lang_data (decl);
4668 TREE_LANG_FLAG_0 (decl) = 0;
4669 TREE_LANG_FLAG_1 (decl) = 0;
4670 TREE_LANG_FLAG_2 (decl) = 0;
4671 TREE_LANG_FLAG_3 (decl) = 0;
4672 TREE_LANG_FLAG_4 (decl) = 0;
4673 TREE_LANG_FLAG_5 (decl) = 0;
4674 TREE_LANG_FLAG_6 (decl) = 0;
4676 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
4677 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
4678 if (TREE_CODE (decl) == FIELD_DECL)
4680 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
4681 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
4682 DECL_QUALIFIER (decl) = NULL_TREE;
4685 if (TREE_CODE (decl) == FUNCTION_DECL)
4687 if (gimple_has_body_p (decl))
4689 tree t;
4691 /* If DECL has a gimple body, then the context for its
4692 arguments must be DECL. Otherwise, it doesn't really
4693 matter, as we will not be emitting any code for DECL. In
4694 general, there may be other instances of DECL created by
4695 the front end and since PARM_DECLs are generally shared,
4696 their DECL_CONTEXT changes as the replicas of DECL are
4697 created. The only time where DECL_CONTEXT is important
4698 is for the FUNCTION_DECLs that have a gimple body (since
4699 the PARM_DECL will be used in the function's body). */
4700 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
4701 DECL_CONTEXT (t) = decl;
4704 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
4705 At this point, it is not needed anymore. */
4706 DECL_SAVED_TREE (decl) = NULL_TREE;
4708 /* Clear the abstract origin if it refers to a method. Otherwise
4709 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
4710 origin will not be output correctly. */
4711 if (DECL_ABSTRACT_ORIGIN (decl)
4712 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
4713 && RECORD_OR_UNION_TYPE_P
4714 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
4715 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
4717 /* Sometimes the C++ frontend doesn't manage to transform a temporary
4718 DECL_VINDEX referring to itself into a vtable slot number as it
4719 should. Happens with functions that are copied and then forgotten
4720 about. Just clear it, it won't matter anymore. */
4721 if (DECL_VINDEX (decl) && !host_integerp (DECL_VINDEX (decl), 0))
4722 DECL_VINDEX (decl) = NULL_TREE;
4724 else if (TREE_CODE (decl) == VAR_DECL)
4726 if ((DECL_EXTERNAL (decl)
4727 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
4728 || (decl_function_context (decl) && !TREE_STATIC (decl)))
4729 DECL_INITIAL (decl) = NULL_TREE;
4731 else if (TREE_CODE (decl) == TYPE_DECL
4732 || TREE_CODE (decl) == FIELD_DECL)
4733 DECL_INITIAL (decl) = NULL_TREE;
4734 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
4735 && DECL_INITIAL (decl)
4736 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
4738 /* Strip builtins from the translation-unit BLOCK. We still have targets
4739 without builtin_decl_explicit support and also builtins are shared
4740 nodes and thus we can't use TREE_CHAIN in multiple lists. */
4741 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
4742 while (*nextp)
4744 tree var = *nextp;
4745 if (TREE_CODE (var) == FUNCTION_DECL
4746 && DECL_BUILT_IN (var))
4747 *nextp = TREE_CHAIN (var);
4748 else
4749 nextp = &TREE_CHAIN (var);
4755 /* Data used when collecting DECLs and TYPEs for language data removal. */
4757 struct free_lang_data_d
4759 /* Worklist to avoid excessive recursion. */
4760 vec<tree> worklist;
4762 /* Set of traversed objects. Used to avoid duplicate visits. */
4763 struct pointer_set_t *pset;
4765 /* Array of symbols to process with free_lang_data_in_decl. */
4766 vec<tree> decls;
4768 /* Array of types to process with free_lang_data_in_type. */
4769 vec<tree> types;
4773 /* Save all language fields needed to generate proper debug information
4774 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
4776 static void
4777 save_debug_info_for_decl (tree t)
4779 /*struct saved_debug_info_d *sdi;*/
4781 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
4783 /* FIXME. Partial implementation for saving debug info removed. */
4787 /* Save all language fields needed to generate proper debug information
4788 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
4790 static void
4791 save_debug_info_for_type (tree t)
4793 /*struct saved_debug_info_d *sdi;*/
4795 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
4797 /* FIXME. Partial implementation for saving debug info removed. */
4801 /* Add type or decl T to one of the list of tree nodes that need their
4802 language data removed. The lists are held inside FLD. */
4804 static void
4805 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
4807 if (DECL_P (t))
4809 fld->decls.safe_push (t);
4810 if (debug_info_level > DINFO_LEVEL_TERSE)
4811 save_debug_info_for_decl (t);
4813 else if (TYPE_P (t))
4815 fld->types.safe_push (t);
4816 if (debug_info_level > DINFO_LEVEL_TERSE)
4817 save_debug_info_for_type (t);
4819 else
4820 gcc_unreachable ();
4823 /* Push tree node T into FLD->WORKLIST. */
4825 static inline void
4826 fld_worklist_push (tree t, struct free_lang_data_d *fld)
4828 if (t && !is_lang_specific (t) && !pointer_set_contains (fld->pset, t))
4829 fld->worklist.safe_push ((t));
4833 /* Operand callback helper for free_lang_data_in_node. *TP is the
4834 subtree operand being considered. */
4836 static tree
4837 find_decls_types_r (tree *tp, int *ws, void *data)
4839 tree t = *tp;
4840 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
4842 if (TREE_CODE (t) == TREE_LIST)
4843 return NULL_TREE;
4845 /* Language specific nodes will be removed, so there is no need
4846 to gather anything under them. */
4847 if (is_lang_specific (t))
4849 *ws = 0;
4850 return NULL_TREE;
4853 if (DECL_P (t))
4855 /* Note that walk_tree does not traverse every possible field in
4856 decls, so we have to do our own traversals here. */
4857 add_tree_to_fld_list (t, fld);
4859 fld_worklist_push (DECL_NAME (t), fld);
4860 fld_worklist_push (DECL_CONTEXT (t), fld);
4861 fld_worklist_push (DECL_SIZE (t), fld);
4862 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
4864 /* We are going to remove everything under DECL_INITIAL for
4865 TYPE_DECLs. No point walking them. */
4866 if (TREE_CODE (t) != TYPE_DECL)
4867 fld_worklist_push (DECL_INITIAL (t), fld);
4869 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
4870 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
4872 if (TREE_CODE (t) == FUNCTION_DECL)
4874 fld_worklist_push (DECL_ARGUMENTS (t), fld);
4875 fld_worklist_push (DECL_RESULT (t), fld);
4877 else if (TREE_CODE (t) == TYPE_DECL)
4879 fld_worklist_push (DECL_ARGUMENT_FLD (t), fld);
4880 fld_worklist_push (DECL_VINDEX (t), fld);
4881 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
4883 else if (TREE_CODE (t) == FIELD_DECL)
4885 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
4886 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
4887 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
4888 fld_worklist_push (DECL_FCONTEXT (t), fld);
4890 else if (TREE_CODE (t) == VAR_DECL)
4892 fld_worklist_push (DECL_SECTION_NAME (t), fld);
4893 fld_worklist_push (DECL_COMDAT_GROUP (t), fld);
4896 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
4897 && DECL_HAS_VALUE_EXPR_P (t))
4898 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
4900 if (TREE_CODE (t) != FIELD_DECL
4901 && TREE_CODE (t) != TYPE_DECL)
4902 fld_worklist_push (TREE_CHAIN (t), fld);
4903 *ws = 0;
4905 else if (TYPE_P (t))
4907 /* Note that walk_tree does not traverse every possible field in
4908 types, so we have to do our own traversals here. */
4909 add_tree_to_fld_list (t, fld);
4911 if (!RECORD_OR_UNION_TYPE_P (t))
4912 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
4913 fld_worklist_push (TYPE_SIZE (t), fld);
4914 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
4915 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
4916 fld_worklist_push (TYPE_POINTER_TO (t), fld);
4917 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
4918 fld_worklist_push (TYPE_NAME (t), fld);
4919 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
4920 them and thus do not and want not to reach unused pointer types
4921 this way. */
4922 if (!POINTER_TYPE_P (t))
4923 fld_worklist_push (TYPE_MINVAL (t), fld);
4924 if (!RECORD_OR_UNION_TYPE_P (t))
4925 fld_worklist_push (TYPE_MAXVAL (t), fld);
4926 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
4927 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
4928 do not and want not to reach unused variants this way. */
4929 if (TYPE_CONTEXT (t))
4931 tree ctx = TYPE_CONTEXT (t);
4932 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
4933 So push that instead. */
4934 while (ctx && TREE_CODE (ctx) == BLOCK)
4935 ctx = BLOCK_SUPERCONTEXT (ctx);
4936 fld_worklist_push (ctx, fld);
4938 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
4939 and want not to reach unused types this way. */
4941 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
4943 unsigned i;
4944 tree tem;
4945 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
4946 fld_worklist_push (TREE_TYPE (tem), fld);
4947 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
4948 if (tem
4949 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
4950 && TREE_CODE (tem) == TREE_LIST)
4953 fld_worklist_push (TREE_VALUE (tem), fld);
4954 tem = TREE_CHAIN (tem);
4956 while (tem);
4958 if (RECORD_OR_UNION_TYPE_P (t))
4960 tree tem;
4961 /* Push all TYPE_FIELDS - there can be interleaving interesting
4962 and non-interesting things. */
4963 tem = TYPE_FIELDS (t);
4964 while (tem)
4966 if (TREE_CODE (tem) == FIELD_DECL
4967 || TREE_CODE (tem) == TYPE_DECL)
4968 fld_worklist_push (tem, fld);
4969 tem = TREE_CHAIN (tem);
4973 fld_worklist_push (TYPE_STUB_DECL (t), fld);
4974 *ws = 0;
4976 else if (TREE_CODE (t) == BLOCK)
4978 tree tem;
4979 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
4980 fld_worklist_push (tem, fld);
4981 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
4982 fld_worklist_push (tem, fld);
4983 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
4986 if (TREE_CODE (t) != IDENTIFIER_NODE
4987 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
4988 fld_worklist_push (TREE_TYPE (t), fld);
4990 return NULL_TREE;
4994 /* Find decls and types in T. */
4996 static void
4997 find_decls_types (tree t, struct free_lang_data_d *fld)
4999 while (1)
5001 if (!pointer_set_contains (fld->pset, t))
5002 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5003 if (fld->worklist.is_empty ())
5004 break;
5005 t = fld->worklist.pop ();
5009 /* Translate all the types in LIST with the corresponding runtime
5010 types. */
5012 static tree
5013 get_eh_types_for_runtime (tree list)
5015 tree head, prev;
5017 if (list == NULL_TREE)
5018 return NULL_TREE;
5020 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5021 prev = head;
5022 list = TREE_CHAIN (list);
5023 while (list)
5025 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5026 TREE_CHAIN (prev) = n;
5027 prev = TREE_CHAIN (prev);
5028 list = TREE_CHAIN (list);
5031 return head;
5035 /* Find decls and types referenced in EH region R and store them in
5036 FLD->DECLS and FLD->TYPES. */
5038 static void
5039 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5041 switch (r->type)
5043 case ERT_CLEANUP:
5044 break;
5046 case ERT_TRY:
5048 eh_catch c;
5050 /* The types referenced in each catch must first be changed to the
5051 EH types used at runtime. This removes references to FE types
5052 in the region. */
5053 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5055 c->type_list = get_eh_types_for_runtime (c->type_list);
5056 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5059 break;
5061 case ERT_ALLOWED_EXCEPTIONS:
5062 r->u.allowed.type_list
5063 = get_eh_types_for_runtime (r->u.allowed.type_list);
5064 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5065 break;
5067 case ERT_MUST_NOT_THROW:
5068 walk_tree (&r->u.must_not_throw.failure_decl,
5069 find_decls_types_r, fld, fld->pset);
5070 break;
5075 /* Find decls and types referenced in cgraph node N and store them in
5076 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5077 look for *every* kind of DECL and TYPE node reachable from N,
5078 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5079 NAMESPACE_DECLs, etc). */
5081 static void
5082 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5084 basic_block bb;
5085 struct function *fn;
5086 unsigned ix;
5087 tree t;
5089 find_decls_types (n->symbol.decl, fld);
5091 if (!gimple_has_body_p (n->symbol.decl))
5092 return;
5094 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5096 fn = DECL_STRUCT_FUNCTION (n->symbol.decl);
5098 /* Traverse locals. */
5099 FOR_EACH_LOCAL_DECL (fn, ix, t)
5100 find_decls_types (t, fld);
5102 /* Traverse EH regions in FN. */
5104 eh_region r;
5105 FOR_ALL_EH_REGION_FN (r, fn)
5106 find_decls_types_in_eh_region (r, fld);
5109 /* Traverse every statement in FN. */
5110 FOR_EACH_BB_FN (bb, fn)
5112 gimple_stmt_iterator si;
5113 unsigned i;
5115 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5117 gimple phi = gsi_stmt (si);
5119 for (i = 0; i < gimple_phi_num_args (phi); i++)
5121 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5122 find_decls_types (*arg_p, fld);
5126 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5128 gimple stmt = gsi_stmt (si);
5130 if (is_gimple_call (stmt))
5131 find_decls_types (gimple_call_fntype (stmt), fld);
5133 for (i = 0; i < gimple_num_ops (stmt); i++)
5135 tree arg = gimple_op (stmt, i);
5136 find_decls_types (arg, fld);
5143 /* Find decls and types referenced in varpool node N and store them in
5144 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5145 look for *every* kind of DECL and TYPE node reachable from N,
5146 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5147 NAMESPACE_DECLs, etc). */
5149 static void
5150 find_decls_types_in_var (struct varpool_node *v, struct free_lang_data_d *fld)
5152 find_decls_types (v->symbol.decl, fld);
5155 /* If T needs an assembler name, have one created for it. */
5157 void
5158 assign_assembler_name_if_neeeded (tree t)
5160 if (need_assembler_name_p (t))
5162 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5163 diagnostics that use input_location to show locus
5164 information. The problem here is that, at this point,
5165 input_location is generally anchored to the end of the file
5166 (since the parser is long gone), so we don't have a good
5167 position to pin it to.
5169 To alleviate this problem, this uses the location of T's
5170 declaration. Examples of this are
5171 testsuite/g++.dg/template/cond2.C and
5172 testsuite/g++.dg/template/pr35240.C. */
5173 location_t saved_location = input_location;
5174 input_location = DECL_SOURCE_LOCATION (t);
5176 decl_assembler_name (t);
5178 input_location = saved_location;
5183 /* Free language specific information for every operand and expression
5184 in every node of the call graph. This process operates in three stages:
5186 1- Every callgraph node and varpool node is traversed looking for
5187 decls and types embedded in them. This is a more exhaustive
5188 search than that done by find_referenced_vars, because it will
5189 also collect individual fields, decls embedded in types, etc.
5191 2- All the decls found are sent to free_lang_data_in_decl.
5193 3- All the types found are sent to free_lang_data_in_type.
5195 The ordering between decls and types is important because
5196 free_lang_data_in_decl sets assembler names, which includes
5197 mangling. So types cannot be freed up until assembler names have
5198 been set up. */
5200 static void
5201 free_lang_data_in_cgraph (void)
5203 struct cgraph_node *n;
5204 struct varpool_node *v;
5205 struct free_lang_data_d fld;
5206 tree t;
5207 unsigned i;
5208 alias_pair *p;
5210 /* Initialize sets and arrays to store referenced decls and types. */
5211 fld.pset = pointer_set_create ();
5212 fld.worklist.create (0);
5213 fld.decls.create (100);
5214 fld.types.create (100);
5216 /* Find decls and types in the body of every function in the callgraph. */
5217 FOR_EACH_FUNCTION (n)
5218 find_decls_types_in_node (n, &fld);
5220 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5221 find_decls_types (p->decl, &fld);
5223 /* Find decls and types in every varpool symbol. */
5224 FOR_EACH_VARIABLE (v)
5225 find_decls_types_in_var (v, &fld);
5227 /* Set the assembler name on every decl found. We need to do this
5228 now because free_lang_data_in_decl will invalidate data needed
5229 for mangling. This breaks mangling on interdependent decls. */
5230 FOR_EACH_VEC_ELT (fld.decls, i, t)
5231 assign_assembler_name_if_neeeded (t);
5233 /* Traverse every decl found freeing its language data. */
5234 FOR_EACH_VEC_ELT (fld.decls, i, t)
5235 free_lang_data_in_decl (t);
5237 /* Traverse every type found freeing its language data. */
5238 FOR_EACH_VEC_ELT (fld.types, i, t)
5239 free_lang_data_in_type (t);
5241 pointer_set_destroy (fld.pset);
5242 fld.worklist.release ();
5243 fld.decls.release ();
5244 fld.types.release ();
5248 /* Free resources that are used by FE but are not needed once they are done. */
5250 static unsigned
5251 free_lang_data (void)
5253 unsigned i;
5255 /* If we are the LTO frontend we have freed lang-specific data already. */
5256 if (in_lto_p
5257 || !flag_generate_lto)
5258 return 0;
5260 /* Allocate and assign alias sets to the standard integer types
5261 while the slots are still in the way the frontends generated them. */
5262 for (i = 0; i < itk_none; ++i)
5263 if (integer_types[i])
5264 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5266 /* Traverse the IL resetting language specific information for
5267 operands, expressions, etc. */
5268 free_lang_data_in_cgraph ();
5270 /* Create gimple variants for common types. */
5271 ptrdiff_type_node = integer_type_node;
5272 fileptr_type_node = ptr_type_node;
5274 /* Reset some langhooks. Do not reset types_compatible_p, it may
5275 still be used indirectly via the get_alias_set langhook. */
5276 lang_hooks.dwarf_name = lhd_dwarf_name;
5277 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5278 /* We do not want the default decl_assembler_name implementation,
5279 rather if we have fixed everything we want a wrapper around it
5280 asserting that all non-local symbols already got their assembler
5281 name and only produce assembler names for local symbols. Or rather
5282 make sure we never call decl_assembler_name on local symbols and
5283 devise a separate, middle-end private scheme for it. */
5285 /* Reset diagnostic machinery. */
5286 tree_diagnostics_defaults (global_dc);
5288 return 0;
5292 struct simple_ipa_opt_pass pass_ipa_free_lang_data =
5295 SIMPLE_IPA_PASS,
5296 "*free_lang_data", /* name */
5297 OPTGROUP_NONE, /* optinfo_flags */
5298 NULL, /* gate */
5299 free_lang_data, /* execute */
5300 NULL, /* sub */
5301 NULL, /* next */
5302 0, /* static_pass_number */
5303 TV_IPA_FREE_LANG_DATA, /* tv_id */
5304 0, /* properties_required */
5305 0, /* properties_provided */
5306 0, /* properties_destroyed */
5307 0, /* todo_flags_start */
5308 TODO_ggc_collect /* todo_flags_finish */
5312 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
5313 ATTR_NAME. Also used internally by remove_attribute(). */
5314 bool
5315 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
5317 size_t ident_len = IDENTIFIER_LENGTH (ident);
5319 if (ident_len == attr_len)
5321 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
5322 return true;
5324 else if (ident_len == attr_len + 4)
5326 /* There is the possibility that ATTR is 'text' and IDENT is
5327 '__text__'. */
5328 const char *p = IDENTIFIER_POINTER (ident);
5329 if (p[0] == '_' && p[1] == '_'
5330 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5331 && strncmp (attr_name, p + 2, attr_len) == 0)
5332 return true;
5335 return false;
5338 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
5339 of ATTR_NAME, and LIST is not NULL_TREE. */
5340 tree
5341 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
5343 while (list)
5345 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5347 if (ident_len == attr_len)
5349 if (!strcmp (attr_name,
5350 IDENTIFIER_POINTER (get_attribute_name (list))))
5351 break;
5353 /* TODO: If we made sure that attributes were stored in the
5354 canonical form without '__...__' (ie, as in 'text' as opposed
5355 to '__text__') then we could avoid the following case. */
5356 else if (ident_len == attr_len + 4)
5358 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5359 if (p[0] == '_' && p[1] == '_'
5360 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5361 && strncmp (attr_name, p + 2, attr_len) == 0)
5362 break;
5364 list = TREE_CHAIN (list);
5367 return list;
5370 /* A variant of lookup_attribute() that can be used with an identifier
5371 as the first argument, and where the identifier can be either
5372 'text' or '__text__'.
5374 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
5375 return a pointer to the attribute's list element if the attribute
5376 is part of the list, or NULL_TREE if not found. If the attribute
5377 appears more than once, this only returns the first occurrence; the
5378 TREE_CHAIN of the return value should be passed back in if further
5379 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
5380 can be in the form 'text' or '__text__'. */
5381 static tree
5382 lookup_ident_attribute (tree attr_identifier, tree list)
5384 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
5386 while (list)
5388 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
5389 == IDENTIFIER_NODE);
5391 /* Identifiers can be compared directly for equality. */
5392 if (attr_identifier == get_attribute_name (list))
5393 break;
5395 /* If they are not equal, they may still be one in the form
5396 'text' while the other one is in the form '__text__'. TODO:
5397 If we were storing attributes in normalized 'text' form, then
5398 this could all go away and we could take full advantage of
5399 the fact that we're comparing identifiers. :-) */
5401 size_t attr_len = IDENTIFIER_LENGTH (attr_identifier);
5402 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5404 if (ident_len == attr_len + 4)
5406 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5407 const char *q = IDENTIFIER_POINTER (attr_identifier);
5408 if (p[0] == '_' && p[1] == '_'
5409 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5410 && strncmp (q, p + 2, attr_len) == 0)
5411 break;
5413 else if (ident_len + 4 == attr_len)
5415 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5416 const char *q = IDENTIFIER_POINTER (attr_identifier);
5417 if (q[0] == '_' && q[1] == '_'
5418 && q[attr_len - 2] == '_' && q[attr_len - 1] == '_'
5419 && strncmp (q + 2, p, ident_len) == 0)
5420 break;
5423 list = TREE_CHAIN (list);
5426 return list;
5429 /* Remove any instances of attribute ATTR_NAME in LIST and return the
5430 modified list. */
5432 tree
5433 remove_attribute (const char *attr_name, tree list)
5435 tree *p;
5436 size_t attr_len = strlen (attr_name);
5438 gcc_checking_assert (attr_name[0] != '_');
5440 for (p = &list; *p; )
5442 tree l = *p;
5443 /* TODO: If we were storing attributes in normalized form, here
5444 we could use a simple strcmp(). */
5445 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
5446 *p = TREE_CHAIN (l);
5447 else
5448 p = &TREE_CHAIN (l);
5451 return list;
5454 /* Return an attribute list that is the union of a1 and a2. */
5456 tree
5457 merge_attributes (tree a1, tree a2)
5459 tree attributes;
5461 /* Either one unset? Take the set one. */
5463 if ((attributes = a1) == 0)
5464 attributes = a2;
5466 /* One that completely contains the other? Take it. */
5468 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
5470 if (attribute_list_contained (a2, a1))
5471 attributes = a2;
5472 else
5474 /* Pick the longest list, and hang on the other list. */
5476 if (list_length (a1) < list_length (a2))
5477 attributes = a2, a2 = a1;
5479 for (; a2 != 0; a2 = TREE_CHAIN (a2))
5481 tree a;
5482 for (a = lookup_ident_attribute (get_attribute_name (a2),
5483 attributes);
5484 a != NULL_TREE && !attribute_value_equal (a, a2);
5485 a = lookup_ident_attribute (get_attribute_name (a2),
5486 TREE_CHAIN (a)))
5488 if (a == NULL_TREE)
5490 a1 = copy_node (a2);
5491 TREE_CHAIN (a1) = attributes;
5492 attributes = a1;
5497 return attributes;
5500 /* Given types T1 and T2, merge their attributes and return
5501 the result. */
5503 tree
5504 merge_type_attributes (tree t1, tree t2)
5506 return merge_attributes (TYPE_ATTRIBUTES (t1),
5507 TYPE_ATTRIBUTES (t2));
5510 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
5511 the result. */
5513 tree
5514 merge_decl_attributes (tree olddecl, tree newdecl)
5516 return merge_attributes (DECL_ATTRIBUTES (olddecl),
5517 DECL_ATTRIBUTES (newdecl));
5520 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
5522 /* Specialization of merge_decl_attributes for various Windows targets.
5524 This handles the following situation:
5526 __declspec (dllimport) int foo;
5527 int foo;
5529 The second instance of `foo' nullifies the dllimport. */
5531 tree
5532 merge_dllimport_decl_attributes (tree old, tree new_tree)
5534 tree a;
5535 int delete_dllimport_p = 1;
5537 /* What we need to do here is remove from `old' dllimport if it doesn't
5538 appear in `new'. dllimport behaves like extern: if a declaration is
5539 marked dllimport and a definition appears later, then the object
5540 is not dllimport'd. We also remove a `new' dllimport if the old list
5541 contains dllexport: dllexport always overrides dllimport, regardless
5542 of the order of declaration. */
5543 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
5544 delete_dllimport_p = 0;
5545 else if (DECL_DLLIMPORT_P (new_tree)
5546 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
5548 DECL_DLLIMPORT_P (new_tree) = 0;
5549 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
5550 "dllimport ignored", new_tree);
5552 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
5554 /* Warn about overriding a symbol that has already been used, e.g.:
5555 extern int __attribute__ ((dllimport)) foo;
5556 int* bar () {return &foo;}
5557 int foo;
5559 if (TREE_USED (old))
5561 warning (0, "%q+D redeclared without dllimport attribute "
5562 "after being referenced with dll linkage", new_tree);
5563 /* If we have used a variable's address with dllimport linkage,
5564 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
5565 decl may already have had TREE_CONSTANT computed.
5566 We still remove the attribute so that assembler code refers
5567 to '&foo rather than '_imp__foo'. */
5568 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
5569 DECL_DLLIMPORT_P (new_tree) = 1;
5572 /* Let an inline definition silently override the external reference,
5573 but otherwise warn about attribute inconsistency. */
5574 else if (TREE_CODE (new_tree) == VAR_DECL
5575 || !DECL_DECLARED_INLINE_P (new_tree))
5576 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
5577 "previous dllimport ignored", new_tree);
5579 else
5580 delete_dllimport_p = 0;
5582 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
5584 if (delete_dllimport_p)
5585 a = remove_attribute ("dllimport", a);
5587 return a;
5590 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
5591 struct attribute_spec.handler. */
5593 tree
5594 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
5595 bool *no_add_attrs)
5597 tree node = *pnode;
5598 bool is_dllimport;
5600 /* These attributes may apply to structure and union types being created,
5601 but otherwise should pass to the declaration involved. */
5602 if (!DECL_P (node))
5604 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
5605 | (int) ATTR_FLAG_ARRAY_NEXT))
5607 *no_add_attrs = true;
5608 return tree_cons (name, args, NULL_TREE);
5610 if (TREE_CODE (node) == RECORD_TYPE
5611 || TREE_CODE (node) == UNION_TYPE)
5613 node = TYPE_NAME (node);
5614 if (!node)
5615 return NULL_TREE;
5617 else
5619 warning (OPT_Wattributes, "%qE attribute ignored",
5620 name);
5621 *no_add_attrs = true;
5622 return NULL_TREE;
5626 if (TREE_CODE (node) != FUNCTION_DECL
5627 && TREE_CODE (node) != VAR_DECL
5628 && TREE_CODE (node) != TYPE_DECL)
5630 *no_add_attrs = true;
5631 warning (OPT_Wattributes, "%qE attribute ignored",
5632 name);
5633 return NULL_TREE;
5636 if (TREE_CODE (node) == TYPE_DECL
5637 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
5638 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
5640 *no_add_attrs = true;
5641 warning (OPT_Wattributes, "%qE attribute ignored",
5642 name);
5643 return NULL_TREE;
5646 is_dllimport = is_attribute_p ("dllimport", name);
5648 /* Report error on dllimport ambiguities seen now before they cause
5649 any damage. */
5650 if (is_dllimport)
5652 /* Honor any target-specific overrides. */
5653 if (!targetm.valid_dllimport_attribute_p (node))
5654 *no_add_attrs = true;
5656 else if (TREE_CODE (node) == FUNCTION_DECL
5657 && DECL_DECLARED_INLINE_P (node))
5659 warning (OPT_Wattributes, "inline function %q+D declared as "
5660 " dllimport: attribute ignored", node);
5661 *no_add_attrs = true;
5663 /* Like MS, treat definition of dllimported variables and
5664 non-inlined functions on declaration as syntax errors. */
5665 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
5667 error ("function %q+D definition is marked dllimport", node);
5668 *no_add_attrs = true;
5671 else if (TREE_CODE (node) == VAR_DECL)
5673 if (DECL_INITIAL (node))
5675 error ("variable %q+D definition is marked dllimport",
5676 node);
5677 *no_add_attrs = true;
5680 /* `extern' needn't be specified with dllimport.
5681 Specify `extern' now and hope for the best. Sigh. */
5682 DECL_EXTERNAL (node) = 1;
5683 /* Also, implicitly give dllimport'd variables declared within
5684 a function global scope, unless declared static. */
5685 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
5686 TREE_PUBLIC (node) = 1;
5689 if (*no_add_attrs == false)
5690 DECL_DLLIMPORT_P (node) = 1;
5692 else if (TREE_CODE (node) == FUNCTION_DECL
5693 && DECL_DECLARED_INLINE_P (node)
5694 && flag_keep_inline_dllexport)
5695 /* An exported function, even if inline, must be emitted. */
5696 DECL_EXTERNAL (node) = 0;
5698 /* Report error if symbol is not accessible at global scope. */
5699 if (!TREE_PUBLIC (node)
5700 && (TREE_CODE (node) == VAR_DECL
5701 || TREE_CODE (node) == FUNCTION_DECL))
5703 error ("external linkage required for symbol %q+D because of "
5704 "%qE attribute", node, name);
5705 *no_add_attrs = true;
5708 /* A dllexport'd entity must have default visibility so that other
5709 program units (shared libraries or the main executable) can see
5710 it. A dllimport'd entity must have default visibility so that
5711 the linker knows that undefined references within this program
5712 unit can be resolved by the dynamic linker. */
5713 if (!*no_add_attrs)
5715 if (DECL_VISIBILITY_SPECIFIED (node)
5716 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
5717 error ("%qE implies default visibility, but %qD has already "
5718 "been declared with a different visibility",
5719 name, node);
5720 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
5721 DECL_VISIBILITY_SPECIFIED (node) = 1;
5724 return NULL_TREE;
5727 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
5729 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
5730 of the various TYPE_QUAL values. */
5732 static void
5733 set_type_quals (tree type, int type_quals)
5735 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
5736 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
5737 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
5738 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
5741 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
5743 bool
5744 check_qualified_type (const_tree cand, const_tree base, int type_quals)
5746 return (TYPE_QUALS (cand) == type_quals
5747 && TYPE_NAME (cand) == TYPE_NAME (base)
5748 /* Apparently this is needed for Objective-C. */
5749 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5750 /* Check alignment. */
5751 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
5752 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5753 TYPE_ATTRIBUTES (base)));
5756 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
5758 static bool
5759 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
5761 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
5762 && TYPE_NAME (cand) == TYPE_NAME (base)
5763 /* Apparently this is needed for Objective-C. */
5764 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5765 /* Check alignment. */
5766 && TYPE_ALIGN (cand) == align
5767 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5768 TYPE_ATTRIBUTES (base)));
5771 /* Return a version of the TYPE, qualified as indicated by the
5772 TYPE_QUALS, if one exists. If no qualified version exists yet,
5773 return NULL_TREE. */
5775 tree
5776 get_qualified_type (tree type, int type_quals)
5778 tree t;
5780 if (TYPE_QUALS (type) == type_quals)
5781 return type;
5783 /* Search the chain of variants to see if there is already one there just
5784 like the one we need to have. If so, use that existing one. We must
5785 preserve the TYPE_NAME, since there is code that depends on this. */
5786 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5787 if (check_qualified_type (t, type, type_quals))
5788 return t;
5790 return NULL_TREE;
5793 /* Like get_qualified_type, but creates the type if it does not
5794 exist. This function never returns NULL_TREE. */
5796 tree
5797 build_qualified_type (tree type, int type_quals)
5799 tree t;
5801 /* See if we already have the appropriate qualified variant. */
5802 t = get_qualified_type (type, type_quals);
5804 /* If not, build it. */
5805 if (!t)
5807 t = build_variant_type_copy (type);
5808 set_type_quals (t, type_quals);
5810 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5811 /* Propagate structural equality. */
5812 SET_TYPE_STRUCTURAL_EQUALITY (t);
5813 else if (TYPE_CANONICAL (type) != type)
5814 /* Build the underlying canonical type, since it is different
5815 from TYPE. */
5816 TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
5817 type_quals);
5818 else
5819 /* T is its own canonical type. */
5820 TYPE_CANONICAL (t) = t;
5824 return t;
5827 /* Create a variant of type T with alignment ALIGN. */
5829 tree
5830 build_aligned_type (tree type, unsigned int align)
5832 tree t;
5834 if (TYPE_PACKED (type)
5835 || TYPE_ALIGN (type) == align)
5836 return type;
5838 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5839 if (check_aligned_type (t, type, align))
5840 return t;
5842 t = build_variant_type_copy (type);
5843 TYPE_ALIGN (t) = align;
5845 return t;
5848 /* Create a new distinct copy of TYPE. The new type is made its own
5849 MAIN_VARIANT. If TYPE requires structural equality checks, the
5850 resulting type requires structural equality checks; otherwise, its
5851 TYPE_CANONICAL points to itself. */
5853 tree
5854 build_distinct_type_copy (tree type)
5856 tree t = copy_node (type);
5858 TYPE_POINTER_TO (t) = 0;
5859 TYPE_REFERENCE_TO (t) = 0;
5861 /* Set the canonical type either to a new equivalence class, or
5862 propagate the need for structural equality checks. */
5863 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5864 SET_TYPE_STRUCTURAL_EQUALITY (t);
5865 else
5866 TYPE_CANONICAL (t) = t;
5868 /* Make it its own variant. */
5869 TYPE_MAIN_VARIANT (t) = t;
5870 TYPE_NEXT_VARIANT (t) = 0;
5872 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
5873 whose TREE_TYPE is not t. This can also happen in the Ada
5874 frontend when using subtypes. */
5876 return t;
5879 /* Create a new variant of TYPE, equivalent but distinct. This is so
5880 the caller can modify it. TYPE_CANONICAL for the return type will
5881 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
5882 are considered equal by the language itself (or that both types
5883 require structural equality checks). */
5885 tree
5886 build_variant_type_copy (tree type)
5888 tree t, m = TYPE_MAIN_VARIANT (type);
5890 t = build_distinct_type_copy (type);
5892 /* Since we're building a variant, assume that it is a non-semantic
5893 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
5894 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
5896 /* Add the new type to the chain of variants of TYPE. */
5897 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
5898 TYPE_NEXT_VARIANT (m) = t;
5899 TYPE_MAIN_VARIANT (t) = m;
5901 return t;
5904 /* Return true if the from tree in both tree maps are equal. */
5907 tree_map_base_eq (const void *va, const void *vb)
5909 const struct tree_map_base *const a = (const struct tree_map_base *) va,
5910 *const b = (const struct tree_map_base *) vb;
5911 return (a->from == b->from);
5914 /* Hash a from tree in a tree_base_map. */
5916 unsigned int
5917 tree_map_base_hash (const void *item)
5919 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
5922 /* Return true if this tree map structure is marked for garbage collection
5923 purposes. We simply return true if the from tree is marked, so that this
5924 structure goes away when the from tree goes away. */
5927 tree_map_base_marked_p (const void *p)
5929 return ggc_marked_p (((const struct tree_map_base *) p)->from);
5932 /* Hash a from tree in a tree_map. */
5934 unsigned int
5935 tree_map_hash (const void *item)
5937 return (((const struct tree_map *) item)->hash);
5940 /* Hash a from tree in a tree_decl_map. */
5942 unsigned int
5943 tree_decl_map_hash (const void *item)
5945 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
5948 /* Return the initialization priority for DECL. */
5950 priority_type
5951 decl_init_priority_lookup (tree decl)
5953 struct tree_priority_map *h;
5954 struct tree_map_base in;
5956 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
5957 in.from = decl;
5958 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
5959 return h ? h->init : DEFAULT_INIT_PRIORITY;
5962 /* Return the finalization priority for DECL. */
5964 priority_type
5965 decl_fini_priority_lookup (tree decl)
5967 struct tree_priority_map *h;
5968 struct tree_map_base in;
5970 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5971 in.from = decl;
5972 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
5973 return h ? h->fini : DEFAULT_INIT_PRIORITY;
5976 /* Return the initialization and finalization priority information for
5977 DECL. If there is no previous priority information, a freshly
5978 allocated structure is returned. */
5980 static struct tree_priority_map *
5981 decl_priority_info (tree decl)
5983 struct tree_priority_map in;
5984 struct tree_priority_map *h;
5985 void **loc;
5987 in.base.from = decl;
5988 loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
5989 h = (struct tree_priority_map *) *loc;
5990 if (!h)
5992 h = ggc_alloc_cleared_tree_priority_map ();
5993 *loc = h;
5994 h->base.from = decl;
5995 h->init = DEFAULT_INIT_PRIORITY;
5996 h->fini = DEFAULT_INIT_PRIORITY;
5999 return h;
6002 /* Set the initialization priority for DECL to PRIORITY. */
6004 void
6005 decl_init_priority_insert (tree decl, priority_type priority)
6007 struct tree_priority_map *h;
6009 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6010 if (priority == DEFAULT_INIT_PRIORITY)
6011 return;
6012 h = decl_priority_info (decl);
6013 h->init = priority;
6016 /* Set the finalization priority for DECL to PRIORITY. */
6018 void
6019 decl_fini_priority_insert (tree decl, priority_type priority)
6021 struct tree_priority_map *h;
6023 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6024 if (priority == DEFAULT_INIT_PRIORITY)
6025 return;
6026 h = decl_priority_info (decl);
6027 h->fini = priority;
6030 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6032 static void
6033 print_debug_expr_statistics (void)
6035 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6036 (long) htab_size (debug_expr_for_decl),
6037 (long) htab_elements (debug_expr_for_decl),
6038 htab_collisions (debug_expr_for_decl));
6041 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6043 static void
6044 print_value_expr_statistics (void)
6046 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6047 (long) htab_size (value_expr_for_decl),
6048 (long) htab_elements (value_expr_for_decl),
6049 htab_collisions (value_expr_for_decl));
6052 /* Lookup a debug expression for FROM, and return it if we find one. */
6054 tree
6055 decl_debug_expr_lookup (tree from)
6057 struct tree_decl_map *h, in;
6058 in.base.from = from;
6060 h = (struct tree_decl_map *)
6061 htab_find_with_hash (debug_expr_for_decl, &in, DECL_UID (from));
6062 if (h)
6063 return h->to;
6064 return NULL_TREE;
6067 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6069 void
6070 decl_debug_expr_insert (tree from, tree to)
6072 struct tree_decl_map *h;
6073 void **loc;
6075 h = ggc_alloc_tree_decl_map ();
6076 h->base.from = from;
6077 h->to = to;
6078 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, DECL_UID (from),
6079 INSERT);
6080 *(struct tree_decl_map **) loc = h;
6083 /* Lookup a value expression for FROM, and return it if we find one. */
6085 tree
6086 decl_value_expr_lookup (tree from)
6088 struct tree_decl_map *h, in;
6089 in.base.from = from;
6091 h = (struct tree_decl_map *)
6092 htab_find_with_hash (value_expr_for_decl, &in, DECL_UID (from));
6093 if (h)
6094 return h->to;
6095 return NULL_TREE;
6098 /* Insert a mapping FROM->TO in the value expression hashtable. */
6100 void
6101 decl_value_expr_insert (tree from, tree to)
6103 struct tree_decl_map *h;
6104 void **loc;
6106 h = ggc_alloc_tree_decl_map ();
6107 h->base.from = from;
6108 h->to = to;
6109 loc = htab_find_slot_with_hash (value_expr_for_decl, h, DECL_UID (from),
6110 INSERT);
6111 *(struct tree_decl_map **) loc = h;
6114 /* Lookup a vector of debug arguments for FROM, and return it if we
6115 find one. */
6117 vec<tree, va_gc> **
6118 decl_debug_args_lookup (tree from)
6120 struct tree_vec_map *h, in;
6122 if (!DECL_HAS_DEBUG_ARGS_P (from))
6123 return NULL;
6124 gcc_checking_assert (debug_args_for_decl != NULL);
6125 in.base.from = from;
6126 h = (struct tree_vec_map *)
6127 htab_find_with_hash (debug_args_for_decl, &in, DECL_UID (from));
6128 if (h)
6129 return &h->to;
6130 return NULL;
6133 /* Insert a mapping FROM->empty vector of debug arguments in the value
6134 expression hashtable. */
6136 vec<tree, va_gc> **
6137 decl_debug_args_insert (tree from)
6139 struct tree_vec_map *h;
6140 void **loc;
6142 if (DECL_HAS_DEBUG_ARGS_P (from))
6143 return decl_debug_args_lookup (from);
6144 if (debug_args_for_decl == NULL)
6145 debug_args_for_decl = htab_create_ggc (64, tree_vec_map_hash,
6146 tree_vec_map_eq, 0);
6147 h = ggc_alloc_tree_vec_map ();
6148 h->base.from = from;
6149 h->to = NULL;
6150 loc = htab_find_slot_with_hash (debug_args_for_decl, h, DECL_UID (from),
6151 INSERT);
6152 *(struct tree_vec_map **) loc = h;
6153 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6154 return &h->to;
6157 /* Hashing of types so that we don't make duplicates.
6158 The entry point is `type_hash_canon'. */
6160 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6161 with types in the TREE_VALUE slots), by adding the hash codes
6162 of the individual types. */
6164 static unsigned int
6165 type_hash_list (const_tree list, hashval_t hashcode)
6167 const_tree tail;
6169 for (tail = list; tail; tail = TREE_CHAIN (tail))
6170 if (TREE_VALUE (tail) != error_mark_node)
6171 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
6172 hashcode);
6174 return hashcode;
6177 /* These are the Hashtable callback functions. */
6179 /* Returns true iff the types are equivalent. */
6181 static int
6182 type_hash_eq (const void *va, const void *vb)
6184 const struct type_hash *const a = (const struct type_hash *) va,
6185 *const b = (const struct type_hash *) vb;
6187 /* First test the things that are the same for all types. */
6188 if (a->hash != b->hash
6189 || TREE_CODE (a->type) != TREE_CODE (b->type)
6190 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6191 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6192 TYPE_ATTRIBUTES (b->type))
6193 || (TREE_CODE (a->type) != COMPLEX_TYPE
6194 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6195 return 0;
6197 /* Be careful about comparing arrays before and after the element type
6198 has been completed; don't compare TYPE_ALIGN unless both types are
6199 complete. */
6200 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6201 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6202 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6203 return 0;
6205 switch (TREE_CODE (a->type))
6207 case VOID_TYPE:
6208 case COMPLEX_TYPE:
6209 case POINTER_TYPE:
6210 case REFERENCE_TYPE:
6211 case NULLPTR_TYPE:
6212 return 1;
6214 case VECTOR_TYPE:
6215 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6217 case ENUMERAL_TYPE:
6218 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6219 && !(TYPE_VALUES (a->type)
6220 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6221 && TYPE_VALUES (b->type)
6222 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6223 && type_list_equal (TYPE_VALUES (a->type),
6224 TYPE_VALUES (b->type))))
6225 return 0;
6227 /* ... fall through ... */
6229 case INTEGER_TYPE:
6230 case REAL_TYPE:
6231 case BOOLEAN_TYPE:
6232 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6233 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6234 TYPE_MAX_VALUE (b->type)))
6235 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6236 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6237 TYPE_MIN_VALUE (b->type))));
6239 case FIXED_POINT_TYPE:
6240 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6242 case OFFSET_TYPE:
6243 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6245 case METHOD_TYPE:
6246 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6247 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6248 || (TYPE_ARG_TYPES (a->type)
6249 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6250 && TYPE_ARG_TYPES (b->type)
6251 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6252 && type_list_equal (TYPE_ARG_TYPES (a->type),
6253 TYPE_ARG_TYPES (b->type)))))
6254 break;
6255 return 0;
6256 case ARRAY_TYPE:
6257 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6259 case RECORD_TYPE:
6260 case UNION_TYPE:
6261 case QUAL_UNION_TYPE:
6262 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6263 || (TYPE_FIELDS (a->type)
6264 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6265 && TYPE_FIELDS (b->type)
6266 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6267 && type_list_equal (TYPE_FIELDS (a->type),
6268 TYPE_FIELDS (b->type))));
6270 case FUNCTION_TYPE:
6271 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6272 || (TYPE_ARG_TYPES (a->type)
6273 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6274 && TYPE_ARG_TYPES (b->type)
6275 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6276 && type_list_equal (TYPE_ARG_TYPES (a->type),
6277 TYPE_ARG_TYPES (b->type))))
6278 break;
6279 return 0;
6281 default:
6282 return 0;
6285 if (lang_hooks.types.type_hash_eq != NULL)
6286 return lang_hooks.types.type_hash_eq (a->type, b->type);
6288 return 1;
6291 /* Return the cached hash value. */
6293 static hashval_t
6294 type_hash_hash (const void *item)
6296 return ((const struct type_hash *) item)->hash;
6299 /* Look in the type hash table for a type isomorphic to TYPE.
6300 If one is found, return it. Otherwise return 0. */
6302 tree
6303 type_hash_lookup (hashval_t hashcode, tree type)
6305 struct type_hash *h, in;
6307 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6308 must call that routine before comparing TYPE_ALIGNs. */
6309 layout_type (type);
6311 in.hash = hashcode;
6312 in.type = type;
6314 h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
6315 hashcode);
6316 if (h)
6317 return h->type;
6318 return NULL_TREE;
6321 /* Add an entry to the type-hash-table
6322 for a type TYPE whose hash code is HASHCODE. */
6324 void
6325 type_hash_add (hashval_t hashcode, tree type)
6327 struct type_hash *h;
6328 void **loc;
6330 h = ggc_alloc_type_hash ();
6331 h->hash = hashcode;
6332 h->type = type;
6333 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
6334 *loc = (void *)h;
6337 /* Given TYPE, and HASHCODE its hash code, return the canonical
6338 object for an identical type if one already exists.
6339 Otherwise, return TYPE, and record it as the canonical object.
6341 To use this function, first create a type of the sort you want.
6342 Then compute its hash code from the fields of the type that
6343 make it different from other similar types.
6344 Then call this function and use the value. */
6346 tree
6347 type_hash_canon (unsigned int hashcode, tree type)
6349 tree t1;
6351 /* The hash table only contains main variants, so ensure that's what we're
6352 being passed. */
6353 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6355 /* See if the type is in the hash table already. If so, return it.
6356 Otherwise, add the type. */
6357 t1 = type_hash_lookup (hashcode, type);
6358 if (t1 != 0)
6360 if (GATHER_STATISTICS)
6362 tree_code_counts[(int) TREE_CODE (type)]--;
6363 tree_node_counts[(int) t_kind]--;
6364 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
6366 return t1;
6368 else
6370 type_hash_add (hashcode, type);
6371 return type;
6375 /* See if the data pointed to by the type hash table is marked. We consider
6376 it marked if the type is marked or if a debug type number or symbol
6377 table entry has been made for the type. */
6379 static int
6380 type_hash_marked_p (const void *p)
6382 const_tree const type = ((const struct type_hash *) p)->type;
6384 return ggc_marked_p (type);
6387 static void
6388 print_type_hash_statistics (void)
6390 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6391 (long) htab_size (type_hash_table),
6392 (long) htab_elements (type_hash_table),
6393 htab_collisions (type_hash_table));
6396 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
6397 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
6398 by adding the hash codes of the individual attributes. */
6400 static unsigned int
6401 attribute_hash_list (const_tree list, hashval_t hashcode)
6403 const_tree tail;
6405 for (tail = list; tail; tail = TREE_CHAIN (tail))
6406 /* ??? Do we want to add in TREE_VALUE too? */
6407 hashcode = iterative_hash_object
6408 (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)), hashcode);
6409 return hashcode;
6412 /* Given two lists of attributes, return true if list l2 is
6413 equivalent to l1. */
6416 attribute_list_equal (const_tree l1, const_tree l2)
6418 if (l1 == l2)
6419 return 1;
6421 return attribute_list_contained (l1, l2)
6422 && attribute_list_contained (l2, l1);
6425 /* Given two lists of attributes, return true if list L2 is
6426 completely contained within L1. */
6427 /* ??? This would be faster if attribute names were stored in a canonicalized
6428 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
6429 must be used to show these elements are equivalent (which they are). */
6430 /* ??? It's not clear that attributes with arguments will always be handled
6431 correctly. */
6434 attribute_list_contained (const_tree l1, const_tree l2)
6436 const_tree t1, t2;
6438 /* First check the obvious, maybe the lists are identical. */
6439 if (l1 == l2)
6440 return 1;
6442 /* Maybe the lists are similar. */
6443 for (t1 = l1, t2 = l2;
6444 t1 != 0 && t2 != 0
6445 && get_attribute_name (t1) == get_attribute_name (t2)
6446 && TREE_VALUE (t1) == TREE_VALUE (t2);
6447 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6450 /* Maybe the lists are equal. */
6451 if (t1 == 0 && t2 == 0)
6452 return 1;
6454 for (; t2 != 0; t2 = TREE_CHAIN (t2))
6456 const_tree attr;
6457 /* This CONST_CAST is okay because lookup_attribute does not
6458 modify its argument and the return value is assigned to a
6459 const_tree. */
6460 for (attr = lookup_ident_attribute (get_attribute_name (t2), CONST_CAST_TREE(l1));
6461 attr != NULL_TREE && !attribute_value_equal (t2, attr);
6462 attr = lookup_ident_attribute (get_attribute_name (t2), TREE_CHAIN (attr)))
6465 if (attr == NULL_TREE)
6466 return 0;
6469 return 1;
6472 /* Given two lists of types
6473 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6474 return 1 if the lists contain the same types in the same order.
6475 Also, the TREE_PURPOSEs must match. */
6478 type_list_equal (const_tree l1, const_tree l2)
6480 const_tree t1, t2;
6482 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6483 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6484 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6485 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6486 && (TREE_TYPE (TREE_PURPOSE (t1))
6487 == TREE_TYPE (TREE_PURPOSE (t2))))))
6488 return 0;
6490 return t1 == t2;
6493 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6494 given by TYPE. If the argument list accepts variable arguments,
6495 then this function counts only the ordinary arguments. */
6498 type_num_arguments (const_tree type)
6500 int i = 0;
6501 tree t;
6503 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6504 /* If the function does not take a variable number of arguments,
6505 the last element in the list will have type `void'. */
6506 if (VOID_TYPE_P (TREE_VALUE (t)))
6507 break;
6508 else
6509 ++i;
6511 return i;
6514 /* Nonzero if integer constants T1 and T2
6515 represent the same constant value. */
6518 tree_int_cst_equal (const_tree t1, const_tree t2)
6520 if (t1 == t2)
6521 return 1;
6523 if (t1 == 0 || t2 == 0)
6524 return 0;
6526 if (TREE_CODE (t1) == INTEGER_CST
6527 && TREE_CODE (t2) == INTEGER_CST
6528 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6529 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
6530 return 1;
6532 return 0;
6535 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
6536 The precise way of comparison depends on their data type. */
6539 tree_int_cst_lt (const_tree t1, const_tree t2)
6541 if (t1 == t2)
6542 return 0;
6544 if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
6546 int t1_sgn = tree_int_cst_sgn (t1);
6547 int t2_sgn = tree_int_cst_sgn (t2);
6549 if (t1_sgn < t2_sgn)
6550 return 1;
6551 else if (t1_sgn > t2_sgn)
6552 return 0;
6553 /* Otherwise, both are non-negative, so we compare them as
6554 unsigned just in case one of them would overflow a signed
6555 type. */
6557 else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
6558 return INT_CST_LT (t1, t2);
6560 return INT_CST_LT_UNSIGNED (t1, t2);
6563 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
6566 tree_int_cst_compare (const_tree t1, const_tree t2)
6568 if (tree_int_cst_lt (t1, t2))
6569 return -1;
6570 else if (tree_int_cst_lt (t2, t1))
6571 return 1;
6572 else
6573 return 0;
6576 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
6577 the host. If POS is zero, the value can be represented in a single
6578 HOST_WIDE_INT. If POS is nonzero, the value must be non-negative and can
6579 be represented in a single unsigned HOST_WIDE_INT. */
6582 host_integerp (const_tree t, int pos)
6584 if (t == NULL_TREE)
6585 return 0;
6587 return (TREE_CODE (t) == INTEGER_CST
6588 && ((TREE_INT_CST_HIGH (t) == 0
6589 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
6590 || (! pos && TREE_INT_CST_HIGH (t) == -1
6591 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
6592 && !TYPE_UNSIGNED (TREE_TYPE (t)))
6593 || (pos && TREE_INT_CST_HIGH (t) == 0)));
6596 /* Return the HOST_WIDE_INT least significant bits of T if it is an
6597 INTEGER_CST and there is no overflow. POS is nonzero if the result must
6598 be non-negative. We must be able to satisfy the above conditions. */
6600 HOST_WIDE_INT
6601 tree_low_cst (const_tree t, int pos)
6603 gcc_assert (host_integerp (t, pos));
6604 return TREE_INT_CST_LOW (t);
6607 /* Return the HOST_WIDE_INT least significant bits of T, a sizetype
6608 kind INTEGER_CST. This makes sure to properly sign-extend the
6609 constant. */
6611 HOST_WIDE_INT
6612 size_low_cst (const_tree t)
6614 double_int d = tree_to_double_int (t);
6615 return d.sext (TYPE_PRECISION (TREE_TYPE (t))).low;
6618 /* Return the most significant (sign) bit of T. */
6621 tree_int_cst_sign_bit (const_tree t)
6623 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
6624 unsigned HOST_WIDE_INT w;
6626 if (bitno < HOST_BITS_PER_WIDE_INT)
6627 w = TREE_INT_CST_LOW (t);
6628 else
6630 w = TREE_INT_CST_HIGH (t);
6631 bitno -= HOST_BITS_PER_WIDE_INT;
6634 return (w >> bitno) & 1;
6637 /* Return an indication of the sign of the integer constant T.
6638 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
6639 Note that -1 will never be returned if T's type is unsigned. */
6642 tree_int_cst_sgn (const_tree t)
6644 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
6645 return 0;
6646 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
6647 return 1;
6648 else if (TREE_INT_CST_HIGH (t) < 0)
6649 return -1;
6650 else
6651 return 1;
6654 /* Return the minimum number of bits needed to represent VALUE in a
6655 signed or unsigned type, UNSIGNEDP says which. */
6657 unsigned int
6658 tree_int_cst_min_precision (tree value, bool unsignedp)
6660 int log;
6662 /* If the value is negative, compute its negative minus 1. The latter
6663 adjustment is because the absolute value of the largest negative value
6664 is one larger than the largest positive value. This is equivalent to
6665 a bit-wise negation, so use that operation instead. */
6667 if (tree_int_cst_sgn (value) < 0)
6668 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
6670 /* Return the number of bits needed, taking into account the fact
6671 that we need one more bit for a signed than unsigned type. */
6673 if (integer_zerop (value))
6674 log = 0;
6675 else
6676 log = tree_floor_log2 (value);
6678 return log + 1 + !unsignedp;
6681 /* Compare two constructor-element-type constants. Return 1 if the lists
6682 are known to be equal; otherwise return 0. */
6685 simple_cst_list_equal (const_tree l1, const_tree l2)
6687 while (l1 != NULL_TREE && l2 != NULL_TREE)
6689 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
6690 return 0;
6692 l1 = TREE_CHAIN (l1);
6693 l2 = TREE_CHAIN (l2);
6696 return l1 == l2;
6699 /* Return truthvalue of whether T1 is the same tree structure as T2.
6700 Return 1 if they are the same.
6701 Return 0 if they are understandably different.
6702 Return -1 if either contains tree structure not understood by
6703 this function. */
6706 simple_cst_equal (const_tree t1, const_tree t2)
6708 enum tree_code code1, code2;
6709 int cmp;
6710 int i;
6712 if (t1 == t2)
6713 return 1;
6714 if (t1 == 0 || t2 == 0)
6715 return 0;
6717 code1 = TREE_CODE (t1);
6718 code2 = TREE_CODE (t2);
6720 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
6722 if (CONVERT_EXPR_CODE_P (code2)
6723 || code2 == NON_LVALUE_EXPR)
6724 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6725 else
6726 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
6729 else if (CONVERT_EXPR_CODE_P (code2)
6730 || code2 == NON_LVALUE_EXPR)
6731 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
6733 if (code1 != code2)
6734 return 0;
6736 switch (code1)
6738 case INTEGER_CST:
6739 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6740 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
6742 case REAL_CST:
6743 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
6745 case FIXED_CST:
6746 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
6748 case STRING_CST:
6749 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
6750 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
6751 TREE_STRING_LENGTH (t1)));
6753 case CONSTRUCTOR:
6755 unsigned HOST_WIDE_INT idx;
6756 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
6757 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
6759 if (vec_safe_length (v1) != vec_safe_length (v2))
6760 return false;
6762 for (idx = 0; idx < vec_safe_length (v1); ++idx)
6763 /* ??? Should we handle also fields here? */
6764 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
6765 return false;
6766 return true;
6769 case SAVE_EXPR:
6770 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6772 case CALL_EXPR:
6773 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
6774 if (cmp <= 0)
6775 return cmp;
6776 if (call_expr_nargs (t1) != call_expr_nargs (t2))
6777 return 0;
6779 const_tree arg1, arg2;
6780 const_call_expr_arg_iterator iter1, iter2;
6781 for (arg1 = first_const_call_expr_arg (t1, &iter1),
6782 arg2 = first_const_call_expr_arg (t2, &iter2);
6783 arg1 && arg2;
6784 arg1 = next_const_call_expr_arg (&iter1),
6785 arg2 = next_const_call_expr_arg (&iter2))
6787 cmp = simple_cst_equal (arg1, arg2);
6788 if (cmp <= 0)
6789 return cmp;
6791 return arg1 == arg2;
6794 case TARGET_EXPR:
6795 /* Special case: if either target is an unallocated VAR_DECL,
6796 it means that it's going to be unified with whatever the
6797 TARGET_EXPR is really supposed to initialize, so treat it
6798 as being equivalent to anything. */
6799 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
6800 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
6801 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
6802 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
6803 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
6804 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
6805 cmp = 1;
6806 else
6807 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6809 if (cmp <= 0)
6810 return cmp;
6812 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
6814 case WITH_CLEANUP_EXPR:
6815 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6816 if (cmp <= 0)
6817 return cmp;
6819 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
6821 case COMPONENT_REF:
6822 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
6823 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6825 return 0;
6827 case VAR_DECL:
6828 case PARM_DECL:
6829 case CONST_DECL:
6830 case FUNCTION_DECL:
6831 return 0;
6833 default:
6834 break;
6837 /* This general rule works for most tree codes. All exceptions should be
6838 handled above. If this is a language-specific tree code, we can't
6839 trust what might be in the operand, so say we don't know
6840 the situation. */
6841 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
6842 return -1;
6844 switch (TREE_CODE_CLASS (code1))
6846 case tcc_unary:
6847 case tcc_binary:
6848 case tcc_comparison:
6849 case tcc_expression:
6850 case tcc_reference:
6851 case tcc_statement:
6852 cmp = 1;
6853 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
6855 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
6856 if (cmp <= 0)
6857 return cmp;
6860 return cmp;
6862 default:
6863 return -1;
6867 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
6868 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
6869 than U, respectively. */
6872 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
6874 if (tree_int_cst_sgn (t) < 0)
6875 return -1;
6876 else if (TREE_INT_CST_HIGH (t) != 0)
6877 return 1;
6878 else if (TREE_INT_CST_LOW (t) == u)
6879 return 0;
6880 else if (TREE_INT_CST_LOW (t) < u)
6881 return -1;
6882 else
6883 return 1;
6886 /* Return true if SIZE represents a constant size that is in bounds of
6887 what the middle-end and the backend accepts (covering not more than
6888 half of the address-space). */
6890 bool
6891 valid_constant_size_p (const_tree size)
6893 if (! host_integerp (size, 1)
6894 || TREE_OVERFLOW (size)
6895 || tree_int_cst_sign_bit (size) != 0)
6896 return false;
6897 return true;
6900 /* Return true if CODE represents an associative tree code. Otherwise
6901 return false. */
6902 bool
6903 associative_tree_code (enum tree_code code)
6905 switch (code)
6907 case BIT_IOR_EXPR:
6908 case BIT_AND_EXPR:
6909 case BIT_XOR_EXPR:
6910 case PLUS_EXPR:
6911 case MULT_EXPR:
6912 case MIN_EXPR:
6913 case MAX_EXPR:
6914 return true;
6916 default:
6917 break;
6919 return false;
6922 /* Return true if CODE represents a commutative tree code. Otherwise
6923 return false. */
6924 bool
6925 commutative_tree_code (enum tree_code code)
6927 switch (code)
6929 case PLUS_EXPR:
6930 case MULT_EXPR:
6931 case MULT_HIGHPART_EXPR:
6932 case MIN_EXPR:
6933 case MAX_EXPR:
6934 case BIT_IOR_EXPR:
6935 case BIT_XOR_EXPR:
6936 case BIT_AND_EXPR:
6937 case NE_EXPR:
6938 case EQ_EXPR:
6939 case UNORDERED_EXPR:
6940 case ORDERED_EXPR:
6941 case UNEQ_EXPR:
6942 case LTGT_EXPR:
6943 case TRUTH_AND_EXPR:
6944 case TRUTH_XOR_EXPR:
6945 case TRUTH_OR_EXPR:
6946 case WIDEN_MULT_EXPR:
6947 case VEC_WIDEN_MULT_HI_EXPR:
6948 case VEC_WIDEN_MULT_LO_EXPR:
6949 case VEC_WIDEN_MULT_EVEN_EXPR:
6950 case VEC_WIDEN_MULT_ODD_EXPR:
6951 return true;
6953 default:
6954 break;
6956 return false;
6959 /* Return true if CODE represents a ternary tree code for which the
6960 first two operands are commutative. Otherwise return false. */
6961 bool
6962 commutative_ternary_tree_code (enum tree_code code)
6964 switch (code)
6966 case WIDEN_MULT_PLUS_EXPR:
6967 case WIDEN_MULT_MINUS_EXPR:
6968 return true;
6970 default:
6971 break;
6973 return false;
6976 /* Generate a hash value for an expression. This can be used iteratively
6977 by passing a previous result as the VAL argument.
6979 This function is intended to produce the same hash for expressions which
6980 would compare equal using operand_equal_p. */
6982 hashval_t
6983 iterative_hash_expr (const_tree t, hashval_t val)
6985 int i;
6986 enum tree_code code;
6987 char tclass;
6989 if (t == NULL_TREE)
6990 return iterative_hash_hashval_t (0, val);
6992 code = TREE_CODE (t);
6994 switch (code)
6996 /* Alas, constants aren't shared, so we can't rely on pointer
6997 identity. */
6998 case INTEGER_CST:
6999 val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
7000 return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
7001 case REAL_CST:
7003 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7005 return iterative_hash_hashval_t (val2, val);
7007 case FIXED_CST:
7009 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7011 return iterative_hash_hashval_t (val2, val);
7013 case STRING_CST:
7014 return iterative_hash (TREE_STRING_POINTER (t),
7015 TREE_STRING_LENGTH (t), val);
7016 case COMPLEX_CST:
7017 val = iterative_hash_expr (TREE_REALPART (t), val);
7018 return iterative_hash_expr (TREE_IMAGPART (t), val);
7019 case VECTOR_CST:
7021 unsigned i;
7022 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7023 val = iterative_hash_expr (VECTOR_CST_ELT (t, i), val);
7024 return val;
7026 case SSA_NAME:
7027 /* We can just compare by pointer. */
7028 return iterative_hash_host_wide_int (SSA_NAME_VERSION (t), val);
7029 case PLACEHOLDER_EXPR:
7030 /* The node itself doesn't matter. */
7031 return val;
7032 case TREE_LIST:
7033 /* A list of expressions, for a CALL_EXPR or as the elements of a
7034 VECTOR_CST. */
7035 for (; t; t = TREE_CHAIN (t))
7036 val = iterative_hash_expr (TREE_VALUE (t), val);
7037 return val;
7038 case CONSTRUCTOR:
7040 unsigned HOST_WIDE_INT idx;
7041 tree field, value;
7042 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7044 val = iterative_hash_expr (field, val);
7045 val = iterative_hash_expr (value, val);
7047 return val;
7049 case MEM_REF:
7051 /* The type of the second operand is relevant, except for
7052 its top-level qualifiers. */
7053 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (t, 1)));
7055 val = iterative_hash_object (TYPE_HASH (type), val);
7057 /* We could use the standard hash computation from this point
7058 on. */
7059 val = iterative_hash_object (code, val);
7060 val = iterative_hash_expr (TREE_OPERAND (t, 1), val);
7061 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7062 return val;
7064 case FUNCTION_DECL:
7065 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7066 Otherwise nodes that compare equal according to operand_equal_p might
7067 get different hash codes. However, don't do this for machine specific
7068 or front end builtins, since the function code is overloaded in those
7069 cases. */
7070 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7071 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7073 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7074 code = TREE_CODE (t);
7076 /* FALL THROUGH */
7077 default:
7078 tclass = TREE_CODE_CLASS (code);
7080 if (tclass == tcc_declaration)
7082 /* DECL's have a unique ID */
7083 val = iterative_hash_host_wide_int (DECL_UID (t), val);
7085 else
7087 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7089 val = iterative_hash_object (code, val);
7091 /* Don't hash the type, that can lead to having nodes which
7092 compare equal according to operand_equal_p, but which
7093 have different hash codes. */
7094 if (CONVERT_EXPR_CODE_P (code)
7095 || code == NON_LVALUE_EXPR)
7097 /* Make sure to include signness in the hash computation. */
7098 val += TYPE_UNSIGNED (TREE_TYPE (t));
7099 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7102 else if (commutative_tree_code (code))
7104 /* It's a commutative expression. We want to hash it the same
7105 however it appears. We do this by first hashing both operands
7106 and then rehashing based on the order of their independent
7107 hashes. */
7108 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
7109 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
7110 hashval_t t;
7112 if (one > two)
7113 t = one, one = two, two = t;
7115 val = iterative_hash_hashval_t (one, val);
7116 val = iterative_hash_hashval_t (two, val);
7118 else
7119 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7120 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
7122 return val;
7126 /* Generate a hash value for a pair of expressions. This can be used
7127 iteratively by passing a previous result as the VAL argument.
7129 The same hash value is always returned for a given pair of expressions,
7130 regardless of the order in which they are presented. This is useful in
7131 hashing the operands of commutative functions. */
7133 hashval_t
7134 iterative_hash_exprs_commutative (const_tree t1,
7135 const_tree t2, hashval_t val)
7137 hashval_t one = iterative_hash_expr (t1, 0);
7138 hashval_t two = iterative_hash_expr (t2, 0);
7139 hashval_t t;
7141 if (one > two)
7142 t = one, one = two, two = t;
7143 val = iterative_hash_hashval_t (one, val);
7144 val = iterative_hash_hashval_t (two, val);
7146 return val;
7149 /* Constructors for pointer, array and function types.
7150 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7151 constructed by language-dependent code, not here.) */
7153 /* Construct, lay out and return the type of pointers to TO_TYPE with
7154 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7155 reference all of memory. If such a type has already been
7156 constructed, reuse it. */
7158 tree
7159 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
7160 bool can_alias_all)
7162 tree t;
7164 if (to_type == error_mark_node)
7165 return error_mark_node;
7167 /* If the pointed-to type has the may_alias attribute set, force
7168 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7169 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7170 can_alias_all = true;
7172 /* In some cases, languages will have things that aren't a POINTER_TYPE
7173 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7174 In that case, return that type without regard to the rest of our
7175 operands.
7177 ??? This is a kludge, but consistent with the way this function has
7178 always operated and there doesn't seem to be a good way to avoid this
7179 at the moment. */
7180 if (TYPE_POINTER_TO (to_type) != 0
7181 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7182 return TYPE_POINTER_TO (to_type);
7184 /* First, if we already have a type for pointers to TO_TYPE and it's
7185 the proper mode, use it. */
7186 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7187 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7188 return t;
7190 t = make_node (POINTER_TYPE);
7192 TREE_TYPE (t) = to_type;
7193 SET_TYPE_MODE (t, mode);
7194 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7195 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7196 TYPE_POINTER_TO (to_type) = t;
7198 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7199 SET_TYPE_STRUCTURAL_EQUALITY (t);
7200 else if (TYPE_CANONICAL (to_type) != to_type)
7201 TYPE_CANONICAL (t)
7202 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7203 mode, can_alias_all);
7205 /* Lay out the type. This function has many callers that are concerned
7206 with expression-construction, and this simplifies them all. */
7207 layout_type (t);
7209 return t;
7212 /* By default build pointers in ptr_mode. */
7214 tree
7215 build_pointer_type (tree to_type)
7217 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7218 : TYPE_ADDR_SPACE (to_type);
7219 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7220 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7223 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7225 tree
7226 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
7227 bool can_alias_all)
7229 tree t;
7231 if (to_type == error_mark_node)
7232 return error_mark_node;
7234 /* If the pointed-to type has the may_alias attribute set, force
7235 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7236 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7237 can_alias_all = true;
7239 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7240 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7241 In that case, return that type without regard to the rest of our
7242 operands.
7244 ??? This is a kludge, but consistent with the way this function has
7245 always operated and there doesn't seem to be a good way to avoid this
7246 at the moment. */
7247 if (TYPE_REFERENCE_TO (to_type) != 0
7248 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7249 return TYPE_REFERENCE_TO (to_type);
7251 /* First, if we already have a type for pointers to TO_TYPE and it's
7252 the proper mode, use it. */
7253 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7254 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7255 return t;
7257 t = make_node (REFERENCE_TYPE);
7259 TREE_TYPE (t) = to_type;
7260 SET_TYPE_MODE (t, mode);
7261 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7262 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7263 TYPE_REFERENCE_TO (to_type) = t;
7265 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7266 SET_TYPE_STRUCTURAL_EQUALITY (t);
7267 else if (TYPE_CANONICAL (to_type) != to_type)
7268 TYPE_CANONICAL (t)
7269 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7270 mode, can_alias_all);
7272 layout_type (t);
7274 return t;
7278 /* Build the node for the type of references-to-TO_TYPE by default
7279 in ptr_mode. */
7281 tree
7282 build_reference_type (tree to_type)
7284 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7285 : TYPE_ADDR_SPACE (to_type);
7286 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7287 return build_reference_type_for_mode (to_type, pointer_mode, false);
7290 /* Build a type that is compatible with t but has no cv quals anywhere
7291 in its type, thus
7293 const char *const *const * -> char ***. */
7295 tree
7296 build_type_no_quals (tree t)
7298 switch (TREE_CODE (t))
7300 case POINTER_TYPE:
7301 return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7302 TYPE_MODE (t),
7303 TYPE_REF_CAN_ALIAS_ALL (t));
7304 case REFERENCE_TYPE:
7305 return
7306 build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7307 TYPE_MODE (t),
7308 TYPE_REF_CAN_ALIAS_ALL (t));
7309 default:
7310 return TYPE_MAIN_VARIANT (t);
7314 #define MAX_INT_CACHED_PREC \
7315 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7316 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7318 /* Builds a signed or unsigned integer type of precision PRECISION.
7319 Used for C bitfields whose precision does not match that of
7320 built-in target types. */
7321 tree
7322 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7323 int unsignedp)
7325 tree itype, ret;
7327 if (unsignedp)
7328 unsignedp = MAX_INT_CACHED_PREC + 1;
7330 if (precision <= MAX_INT_CACHED_PREC)
7332 itype = nonstandard_integer_type_cache[precision + unsignedp];
7333 if (itype)
7334 return itype;
7337 itype = make_node (INTEGER_TYPE);
7338 TYPE_PRECISION (itype) = precision;
7340 if (unsignedp)
7341 fixup_unsigned_type (itype);
7342 else
7343 fixup_signed_type (itype);
7345 ret = itype;
7346 if (host_integerp (TYPE_MAX_VALUE (itype), 1))
7347 ret = type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
7348 if (precision <= MAX_INT_CACHED_PREC)
7349 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7351 return ret;
7354 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7355 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7356 is true, reuse such a type that has already been constructed. */
7358 static tree
7359 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7361 tree itype = make_node (INTEGER_TYPE);
7362 hashval_t hashcode = 0;
7364 TREE_TYPE (itype) = type;
7366 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7367 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7369 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7370 SET_TYPE_MODE (itype, TYPE_MODE (type));
7371 TYPE_SIZE (itype) = TYPE_SIZE (type);
7372 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7373 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7374 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7376 if (!shared)
7377 return itype;
7379 if ((TYPE_MIN_VALUE (itype)
7380 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7381 || (TYPE_MAX_VALUE (itype)
7382 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7384 /* Since we cannot reliably merge this type, we need to compare it using
7385 structural equality checks. */
7386 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7387 return itype;
7390 hashcode = iterative_hash_expr (TYPE_MIN_VALUE (itype), hashcode);
7391 hashcode = iterative_hash_expr (TYPE_MAX_VALUE (itype), hashcode);
7392 hashcode = iterative_hash_hashval_t (TYPE_HASH (type), hashcode);
7393 itype = type_hash_canon (hashcode, itype);
7395 return itype;
7398 /* Wrapper around build_range_type_1 with SHARED set to true. */
7400 tree
7401 build_range_type (tree type, tree lowval, tree highval)
7403 return build_range_type_1 (type, lowval, highval, true);
7406 /* Wrapper around build_range_type_1 with SHARED set to false. */
7408 tree
7409 build_nonshared_range_type (tree type, tree lowval, tree highval)
7411 return build_range_type_1 (type, lowval, highval, false);
7414 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7415 MAXVAL should be the maximum value in the domain
7416 (one less than the length of the array).
7418 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7419 We don't enforce this limit, that is up to caller (e.g. language front end).
7420 The limit exists because the result is a signed type and we don't handle
7421 sizes that use more than one HOST_WIDE_INT. */
7423 tree
7424 build_index_type (tree maxval)
7426 return build_range_type (sizetype, size_zero_node, maxval);
7429 /* Return true if the debug information for TYPE, a subtype, should be emitted
7430 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7431 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7432 debug info and doesn't reflect the source code. */
7434 bool
7435 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7437 tree base_type = TREE_TYPE (type), low, high;
7439 /* Subrange types have a base type which is an integral type. */
7440 if (!INTEGRAL_TYPE_P (base_type))
7441 return false;
7443 /* Get the real bounds of the subtype. */
7444 if (lang_hooks.types.get_subrange_bounds)
7445 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7446 else
7448 low = TYPE_MIN_VALUE (type);
7449 high = TYPE_MAX_VALUE (type);
7452 /* If the type and its base type have the same representation and the same
7453 name, then the type is not a subrange but a copy of the base type. */
7454 if ((TREE_CODE (base_type) == INTEGER_TYPE
7455 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7456 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7457 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7458 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type)))
7460 tree type_name = TYPE_NAME (type);
7461 tree base_type_name = TYPE_NAME (base_type);
7463 if (type_name && TREE_CODE (type_name) == TYPE_DECL)
7464 type_name = DECL_NAME (type_name);
7466 if (base_type_name && TREE_CODE (base_type_name) == TYPE_DECL)
7467 base_type_name = DECL_NAME (base_type_name);
7469 if (type_name == base_type_name)
7470 return false;
7473 if (lowval)
7474 *lowval = low;
7475 if (highval)
7476 *highval = high;
7477 return true;
7480 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7481 and number of elements specified by the range of values of INDEX_TYPE.
7482 If SHARED is true, reuse such a type that has already been constructed. */
7484 static tree
7485 build_array_type_1 (tree elt_type, tree index_type, bool shared)
7487 tree t;
7489 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7491 error ("arrays of functions are not meaningful");
7492 elt_type = integer_type_node;
7495 t = make_node (ARRAY_TYPE);
7496 TREE_TYPE (t) = elt_type;
7497 TYPE_DOMAIN (t) = index_type;
7498 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7499 layout_type (t);
7501 /* If the element type is incomplete at this point we get marked for
7502 structural equality. Do not record these types in the canonical
7503 type hashtable. */
7504 if (TYPE_STRUCTURAL_EQUALITY_P (t))
7505 return t;
7507 if (shared)
7509 hashval_t hashcode = iterative_hash_object (TYPE_HASH (elt_type), 0);
7510 if (index_type)
7511 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
7512 t = type_hash_canon (hashcode, t);
7515 if (TYPE_CANONICAL (t) == t)
7517 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7518 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7519 SET_TYPE_STRUCTURAL_EQUALITY (t);
7520 else if (TYPE_CANONICAL (elt_type) != elt_type
7521 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7522 TYPE_CANONICAL (t)
7523 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7524 index_type
7525 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7526 shared);
7529 return t;
7532 /* Wrapper around build_array_type_1 with SHARED set to true. */
7534 tree
7535 build_array_type (tree elt_type, tree index_type)
7537 return build_array_type_1 (elt_type, index_type, true);
7540 /* Wrapper around build_array_type_1 with SHARED set to false. */
7542 tree
7543 build_nonshared_array_type (tree elt_type, tree index_type)
7545 return build_array_type_1 (elt_type, index_type, false);
7548 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7549 sizetype. */
7551 tree
7552 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
7554 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7557 /* Recursively examines the array elements of TYPE, until a non-array
7558 element type is found. */
7560 tree
7561 strip_array_types (tree type)
7563 while (TREE_CODE (type) == ARRAY_TYPE)
7564 type = TREE_TYPE (type);
7566 return type;
7569 /* Computes the canonical argument types from the argument type list
7570 ARGTYPES.
7572 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7573 on entry to this function, or if any of the ARGTYPES are
7574 structural.
7576 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7577 true on entry to this function, or if any of the ARGTYPES are
7578 non-canonical.
7580 Returns a canonical argument list, which may be ARGTYPES when the
7581 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7582 true) or would not differ from ARGTYPES. */
7584 static tree
7585 maybe_canonicalize_argtypes(tree argtypes,
7586 bool *any_structural_p,
7587 bool *any_noncanonical_p)
7589 tree arg;
7590 bool any_noncanonical_argtypes_p = false;
7592 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7594 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7595 /* Fail gracefully by stating that the type is structural. */
7596 *any_structural_p = true;
7597 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7598 *any_structural_p = true;
7599 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7600 || TREE_PURPOSE (arg))
7601 /* If the argument has a default argument, we consider it
7602 non-canonical even though the type itself is canonical.
7603 That way, different variants of function and method types
7604 with default arguments will all point to the variant with
7605 no defaults as their canonical type. */
7606 any_noncanonical_argtypes_p = true;
7609 if (*any_structural_p)
7610 return argtypes;
7612 if (any_noncanonical_argtypes_p)
7614 /* Build the canonical list of argument types. */
7615 tree canon_argtypes = NULL_TREE;
7616 bool is_void = false;
7618 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7620 if (arg == void_list_node)
7621 is_void = true;
7622 else
7623 canon_argtypes = tree_cons (NULL_TREE,
7624 TYPE_CANONICAL (TREE_VALUE (arg)),
7625 canon_argtypes);
7628 canon_argtypes = nreverse (canon_argtypes);
7629 if (is_void)
7630 canon_argtypes = chainon (canon_argtypes, void_list_node);
7632 /* There is a non-canonical type. */
7633 *any_noncanonical_p = true;
7634 return canon_argtypes;
7637 /* The canonical argument types are the same as ARGTYPES. */
7638 return argtypes;
7641 /* Construct, lay out and return
7642 the type of functions returning type VALUE_TYPE
7643 given arguments of types ARG_TYPES.
7644 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7645 are data type nodes for the arguments of the function.
7646 If such a type has already been constructed, reuse it. */
7648 tree
7649 build_function_type (tree value_type, tree arg_types)
7651 tree t;
7652 hashval_t hashcode = 0;
7653 bool any_structural_p, any_noncanonical_p;
7654 tree canon_argtypes;
7656 if (TREE_CODE (value_type) == FUNCTION_TYPE)
7658 error ("function return type cannot be function");
7659 value_type = integer_type_node;
7662 /* Make a node of the sort we want. */
7663 t = make_node (FUNCTION_TYPE);
7664 TREE_TYPE (t) = value_type;
7665 TYPE_ARG_TYPES (t) = arg_types;
7667 /* If we already have such a type, use the old one. */
7668 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
7669 hashcode = type_hash_list (arg_types, hashcode);
7670 t = type_hash_canon (hashcode, t);
7672 /* Set up the canonical type. */
7673 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7674 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7675 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
7676 &any_structural_p,
7677 &any_noncanonical_p);
7678 if (any_structural_p)
7679 SET_TYPE_STRUCTURAL_EQUALITY (t);
7680 else if (any_noncanonical_p)
7681 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
7682 canon_argtypes);
7684 if (!COMPLETE_TYPE_P (t))
7685 layout_type (t);
7686 return t;
7689 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP and the
7690 return value if SKIP_RETURN is true. */
7692 static tree
7693 build_function_type_skip_args (tree orig_type, bitmap args_to_skip,
7694 bool skip_return)
7696 tree new_type = NULL;
7697 tree args, new_args = NULL, t;
7698 tree new_reversed;
7699 int i = 0;
7701 for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
7702 args = TREE_CHAIN (args), i++)
7703 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
7704 new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
7706 new_reversed = nreverse (new_args);
7707 if (args)
7709 if (new_reversed)
7710 TREE_CHAIN (new_args) = void_list_node;
7711 else
7712 new_reversed = void_list_node;
7715 /* Use copy_node to preserve as much as possible from original type
7716 (debug info, attribute lists etc.)
7717 Exception is METHOD_TYPEs must have THIS argument.
7718 When we are asked to remove it, we need to build new FUNCTION_TYPE
7719 instead. */
7720 if (TREE_CODE (orig_type) != METHOD_TYPE
7721 || !args_to_skip
7722 || !bitmap_bit_p (args_to_skip, 0))
7724 new_type = build_distinct_type_copy (orig_type);
7725 TYPE_ARG_TYPES (new_type) = new_reversed;
7727 else
7729 new_type
7730 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
7731 new_reversed));
7732 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
7735 if (skip_return)
7736 TREE_TYPE (new_type) = void_type_node;
7738 /* This is a new type, not a copy of an old type. Need to reassociate
7739 variants. We can handle everything except the main variant lazily. */
7740 t = TYPE_MAIN_VARIANT (orig_type);
7741 if (t != orig_type)
7743 t = build_function_type_skip_args (t, args_to_skip, skip_return);
7744 TYPE_MAIN_VARIANT (new_type) = t;
7745 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
7746 TYPE_NEXT_VARIANT (t) = new_type;
7748 else
7750 TYPE_MAIN_VARIANT (new_type) = new_type;
7751 TYPE_NEXT_VARIANT (new_type) = NULL;
7754 return new_type;
7757 /* Build variant of function decl ORIG_DECL skipping ARGS_TO_SKIP and the
7758 return value if SKIP_RETURN is true.
7760 Arguments from DECL_ARGUMENTS list can't be removed now, since they are
7761 linked by TREE_CHAIN directly. The caller is responsible for eliminating
7762 them when they are being duplicated (i.e. copy_arguments_for_versioning). */
7764 tree
7765 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip,
7766 bool skip_return)
7768 tree new_decl = copy_node (orig_decl);
7769 tree new_type;
7771 new_type = TREE_TYPE (orig_decl);
7772 if (prototype_p (new_type)
7773 || (skip_return && !VOID_TYPE_P (TREE_TYPE (new_type))))
7774 new_type
7775 = build_function_type_skip_args (new_type, args_to_skip, skip_return);
7776 TREE_TYPE (new_decl) = new_type;
7778 /* For declarations setting DECL_VINDEX (i.e. methods)
7779 we expect first argument to be THIS pointer. */
7780 if (args_to_skip && bitmap_bit_p (args_to_skip, 0))
7781 DECL_VINDEX (new_decl) = NULL_TREE;
7783 /* When signature changes, we need to clear builtin info. */
7784 if (DECL_BUILT_IN (new_decl)
7785 && args_to_skip
7786 && !bitmap_empty_p (args_to_skip))
7788 DECL_BUILT_IN_CLASS (new_decl) = NOT_BUILT_IN;
7789 DECL_FUNCTION_CODE (new_decl) = (enum built_in_function) 0;
7791 return new_decl;
7794 /* Build a function type. The RETURN_TYPE is the type returned by the
7795 function. If VAARGS is set, no void_type_node is appended to the
7796 the list. ARGP must be always be terminated be a NULL_TREE. */
7798 static tree
7799 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
7801 tree t, args, last;
7803 t = va_arg (argp, tree);
7804 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
7805 args = tree_cons (NULL_TREE, t, args);
7807 if (vaargs)
7809 last = args;
7810 if (args != NULL_TREE)
7811 args = nreverse (args);
7812 gcc_assert (last != void_list_node);
7814 else if (args == NULL_TREE)
7815 args = void_list_node;
7816 else
7818 last = args;
7819 args = nreverse (args);
7820 TREE_CHAIN (last) = void_list_node;
7822 args = build_function_type (return_type, args);
7824 return args;
7827 /* Build a function type. The RETURN_TYPE is the type returned by the
7828 function. If additional arguments are provided, they are
7829 additional argument types. The list of argument types must always
7830 be terminated by NULL_TREE. */
7832 tree
7833 build_function_type_list (tree return_type, ...)
7835 tree args;
7836 va_list p;
7838 va_start (p, return_type);
7839 args = build_function_type_list_1 (false, return_type, p);
7840 va_end (p);
7841 return args;
7844 /* Build a variable argument function type. The RETURN_TYPE is the
7845 type returned by the function. If additional arguments are provided,
7846 they are additional argument types. The list of argument types must
7847 always be terminated by NULL_TREE. */
7849 tree
7850 build_varargs_function_type_list (tree return_type, ...)
7852 tree args;
7853 va_list p;
7855 va_start (p, return_type);
7856 args = build_function_type_list_1 (true, return_type, p);
7857 va_end (p);
7859 return args;
7862 /* Build a function type. RETURN_TYPE is the type returned by the
7863 function; VAARGS indicates whether the function takes varargs. The
7864 function takes N named arguments, the types of which are provided in
7865 ARG_TYPES. */
7867 static tree
7868 build_function_type_array_1 (bool vaargs, tree return_type, int n,
7869 tree *arg_types)
7871 int i;
7872 tree t = vaargs ? NULL_TREE : void_list_node;
7874 for (i = n - 1; i >= 0; i--)
7875 t = tree_cons (NULL_TREE, arg_types[i], t);
7877 return build_function_type (return_type, t);
7880 /* Build a function type. RETURN_TYPE is the type returned by the
7881 function. The function takes N named arguments, the types of which
7882 are provided in ARG_TYPES. */
7884 tree
7885 build_function_type_array (tree return_type, int n, tree *arg_types)
7887 return build_function_type_array_1 (false, return_type, n, arg_types);
7890 /* Build a variable argument function type. RETURN_TYPE is the type
7891 returned by the function. The function takes N named arguments, the
7892 types of which are provided in ARG_TYPES. */
7894 tree
7895 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
7897 return build_function_type_array_1 (true, return_type, n, arg_types);
7900 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
7901 and ARGTYPES (a TREE_LIST) are the return type and arguments types
7902 for the method. An implicit additional parameter (of type
7903 pointer-to-BASETYPE) is added to the ARGTYPES. */
7905 tree
7906 build_method_type_directly (tree basetype,
7907 tree rettype,
7908 tree argtypes)
7910 tree t;
7911 tree ptype;
7912 int hashcode = 0;
7913 bool any_structural_p, any_noncanonical_p;
7914 tree canon_argtypes;
7916 /* Make a node of the sort we want. */
7917 t = make_node (METHOD_TYPE);
7919 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7920 TREE_TYPE (t) = rettype;
7921 ptype = build_pointer_type (basetype);
7923 /* The actual arglist for this function includes a "hidden" argument
7924 which is "this". Put it into the list of argument types. */
7925 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
7926 TYPE_ARG_TYPES (t) = argtypes;
7928 /* If we already have such a type, use the old one. */
7929 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
7930 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
7931 hashcode = type_hash_list (argtypes, hashcode);
7932 t = type_hash_canon (hashcode, t);
7934 /* Set up the canonical type. */
7935 any_structural_p
7936 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7937 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
7938 any_noncanonical_p
7939 = (TYPE_CANONICAL (basetype) != basetype
7940 || TYPE_CANONICAL (rettype) != rettype);
7941 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
7942 &any_structural_p,
7943 &any_noncanonical_p);
7944 if (any_structural_p)
7945 SET_TYPE_STRUCTURAL_EQUALITY (t);
7946 else if (any_noncanonical_p)
7947 TYPE_CANONICAL (t)
7948 = build_method_type_directly (TYPE_CANONICAL (basetype),
7949 TYPE_CANONICAL (rettype),
7950 canon_argtypes);
7951 if (!COMPLETE_TYPE_P (t))
7952 layout_type (t);
7954 return t;
7957 /* Construct, lay out and return the type of methods belonging to class
7958 BASETYPE and whose arguments and values are described by TYPE.
7959 If that type exists already, reuse it.
7960 TYPE must be a FUNCTION_TYPE node. */
7962 tree
7963 build_method_type (tree basetype, tree type)
7965 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
7967 return build_method_type_directly (basetype,
7968 TREE_TYPE (type),
7969 TYPE_ARG_TYPES (type));
7972 /* Construct, lay out and return the type of offsets to a value
7973 of type TYPE, within an object of type BASETYPE.
7974 If a suitable offset type exists already, reuse it. */
7976 tree
7977 build_offset_type (tree basetype, tree type)
7979 tree t;
7980 hashval_t hashcode = 0;
7982 /* Make a node of the sort we want. */
7983 t = make_node (OFFSET_TYPE);
7985 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7986 TREE_TYPE (t) = type;
7988 /* If we already have such a type, use the old one. */
7989 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
7990 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
7991 t = type_hash_canon (hashcode, t);
7993 if (!COMPLETE_TYPE_P (t))
7994 layout_type (t);
7996 if (TYPE_CANONICAL (t) == t)
7998 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7999 || TYPE_STRUCTURAL_EQUALITY_P (type))
8000 SET_TYPE_STRUCTURAL_EQUALITY (t);
8001 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8002 || TYPE_CANONICAL (type) != type)
8003 TYPE_CANONICAL (t)
8004 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8005 TYPE_CANONICAL (type));
8008 return t;
8011 /* Create a complex type whose components are COMPONENT_TYPE. */
8013 tree
8014 build_complex_type (tree component_type)
8016 tree t;
8017 hashval_t hashcode;
8019 gcc_assert (INTEGRAL_TYPE_P (component_type)
8020 || SCALAR_FLOAT_TYPE_P (component_type)
8021 || FIXED_POINT_TYPE_P (component_type));
8023 /* Make a node of the sort we want. */
8024 t = make_node (COMPLEX_TYPE);
8026 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8028 /* If we already have such a type, use the old one. */
8029 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
8030 t = type_hash_canon (hashcode, t);
8032 if (!COMPLETE_TYPE_P (t))
8033 layout_type (t);
8035 if (TYPE_CANONICAL (t) == t)
8037 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8038 SET_TYPE_STRUCTURAL_EQUALITY (t);
8039 else if (TYPE_CANONICAL (component_type) != component_type)
8040 TYPE_CANONICAL (t)
8041 = build_complex_type (TYPE_CANONICAL (component_type));
8044 /* We need to create a name, since complex is a fundamental type. */
8045 if (! TYPE_NAME (t))
8047 const char *name;
8048 if (component_type == char_type_node)
8049 name = "complex char";
8050 else if (component_type == signed_char_type_node)
8051 name = "complex signed char";
8052 else if (component_type == unsigned_char_type_node)
8053 name = "complex unsigned char";
8054 else if (component_type == short_integer_type_node)
8055 name = "complex short int";
8056 else if (component_type == short_unsigned_type_node)
8057 name = "complex short unsigned int";
8058 else if (component_type == integer_type_node)
8059 name = "complex int";
8060 else if (component_type == unsigned_type_node)
8061 name = "complex unsigned int";
8062 else if (component_type == long_integer_type_node)
8063 name = "complex long int";
8064 else if (component_type == long_unsigned_type_node)
8065 name = "complex long unsigned int";
8066 else if (component_type == long_long_integer_type_node)
8067 name = "complex long long int";
8068 else if (component_type == long_long_unsigned_type_node)
8069 name = "complex long long unsigned int";
8070 else
8071 name = 0;
8073 if (name != 0)
8074 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8075 get_identifier (name), t);
8078 return build_qualified_type (t, TYPE_QUALS (component_type));
8081 /* If TYPE is a real or complex floating-point type and the target
8082 does not directly support arithmetic on TYPE then return the wider
8083 type to be used for arithmetic on TYPE. Otherwise, return
8084 NULL_TREE. */
8086 tree
8087 excess_precision_type (tree type)
8089 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8091 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8092 switch (TREE_CODE (type))
8094 case REAL_TYPE:
8095 switch (flt_eval_method)
8097 case 1:
8098 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8099 return double_type_node;
8100 break;
8101 case 2:
8102 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8103 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8104 return long_double_type_node;
8105 break;
8106 default:
8107 gcc_unreachable ();
8109 break;
8110 case COMPLEX_TYPE:
8111 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8112 return NULL_TREE;
8113 switch (flt_eval_method)
8115 case 1:
8116 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8117 return complex_double_type_node;
8118 break;
8119 case 2:
8120 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8121 || (TYPE_MODE (TREE_TYPE (type))
8122 == TYPE_MODE (double_type_node)))
8123 return complex_long_double_type_node;
8124 break;
8125 default:
8126 gcc_unreachable ();
8128 break;
8129 default:
8130 break;
8133 return NULL_TREE;
8136 /* Return OP, stripped of any conversions to wider types as much as is safe.
8137 Converting the value back to OP's type makes a value equivalent to OP.
8139 If FOR_TYPE is nonzero, we return a value which, if converted to
8140 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8142 OP must have integer, real or enumeral type. Pointers are not allowed!
8144 There are some cases where the obvious value we could return
8145 would regenerate to OP if converted to OP's type,
8146 but would not extend like OP to wider types.
8147 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8148 For example, if OP is (unsigned short)(signed char)-1,
8149 we avoid returning (signed char)-1 if FOR_TYPE is int,
8150 even though extending that to an unsigned short would regenerate OP,
8151 since the result of extending (signed char)-1 to (int)
8152 is different from (int) OP. */
8154 tree
8155 get_unwidened (tree op, tree for_type)
8157 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8158 tree type = TREE_TYPE (op);
8159 unsigned final_prec
8160 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8161 int uns
8162 = (for_type != 0 && for_type != type
8163 && final_prec > TYPE_PRECISION (type)
8164 && TYPE_UNSIGNED (type));
8165 tree win = op;
8167 while (CONVERT_EXPR_P (op))
8169 int bitschange;
8171 /* TYPE_PRECISION on vector types has different meaning
8172 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8173 so avoid them here. */
8174 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8175 break;
8177 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8178 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8180 /* Truncations are many-one so cannot be removed.
8181 Unless we are later going to truncate down even farther. */
8182 if (bitschange < 0
8183 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8184 break;
8186 /* See what's inside this conversion. If we decide to strip it,
8187 we will set WIN. */
8188 op = TREE_OPERAND (op, 0);
8190 /* If we have not stripped any zero-extensions (uns is 0),
8191 we can strip any kind of extension.
8192 If we have previously stripped a zero-extension,
8193 only zero-extensions can safely be stripped.
8194 Any extension can be stripped if the bits it would produce
8195 are all going to be discarded later by truncating to FOR_TYPE. */
8197 if (bitschange > 0)
8199 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8200 win = op;
8201 /* TYPE_UNSIGNED says whether this is a zero-extension.
8202 Let's avoid computing it if it does not affect WIN
8203 and if UNS will not be needed again. */
8204 if ((uns
8205 || CONVERT_EXPR_P (op))
8206 && TYPE_UNSIGNED (TREE_TYPE (op)))
8208 uns = 1;
8209 win = op;
8214 /* If we finally reach a constant see if it fits in for_type and
8215 in that case convert it. */
8216 if (for_type
8217 && TREE_CODE (win) == INTEGER_CST
8218 && TREE_TYPE (win) != for_type
8219 && int_fits_type_p (win, for_type))
8220 win = fold_convert (for_type, win);
8222 return win;
8225 /* Return OP or a simpler expression for a narrower value
8226 which can be sign-extended or zero-extended to give back OP.
8227 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8228 or 0 if the value should be sign-extended. */
8230 tree
8231 get_narrower (tree op, int *unsignedp_ptr)
8233 int uns = 0;
8234 int first = 1;
8235 tree win = op;
8236 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8238 while (TREE_CODE (op) == NOP_EXPR)
8240 int bitschange
8241 = (TYPE_PRECISION (TREE_TYPE (op))
8242 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8244 /* Truncations are many-one so cannot be removed. */
8245 if (bitschange < 0)
8246 break;
8248 /* See what's inside this conversion. If we decide to strip it,
8249 we will set WIN. */
8251 if (bitschange > 0)
8253 op = TREE_OPERAND (op, 0);
8254 /* An extension: the outermost one can be stripped,
8255 but remember whether it is zero or sign extension. */
8256 if (first)
8257 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8258 /* Otherwise, if a sign extension has been stripped,
8259 only sign extensions can now be stripped;
8260 if a zero extension has been stripped, only zero-extensions. */
8261 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8262 break;
8263 first = 0;
8265 else /* bitschange == 0 */
8267 /* A change in nominal type can always be stripped, but we must
8268 preserve the unsignedness. */
8269 if (first)
8270 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8271 first = 0;
8272 op = TREE_OPERAND (op, 0);
8273 /* Keep trying to narrow, but don't assign op to win if it
8274 would turn an integral type into something else. */
8275 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8276 continue;
8279 win = op;
8282 if (TREE_CODE (op) == COMPONENT_REF
8283 /* Since type_for_size always gives an integer type. */
8284 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8285 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8286 /* Ensure field is laid out already. */
8287 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8288 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
8290 unsigned HOST_WIDE_INT innerprec
8291 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
8292 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8293 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8294 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8296 /* We can get this structure field in a narrower type that fits it,
8297 but the resulting extension to its nominal type (a fullword type)
8298 must satisfy the same conditions as for other extensions.
8300 Do this only for fields that are aligned (not bit-fields),
8301 because when bit-field insns will be used there is no
8302 advantage in doing this. */
8304 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8305 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8306 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8307 && type != 0)
8309 if (first)
8310 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8311 win = fold_convert (type, op);
8315 *unsignedp_ptr = uns;
8316 return win;
8319 /* Returns true if integer constant C has a value that is permissible
8320 for type TYPE (an INTEGER_TYPE). */
8322 bool
8323 int_fits_type_p (const_tree c, const_tree type)
8325 tree type_low_bound, type_high_bound;
8326 bool ok_for_low_bound, ok_for_high_bound, unsc;
8327 double_int dc, dd;
8329 dc = tree_to_double_int (c);
8330 unsc = TYPE_UNSIGNED (TREE_TYPE (c));
8332 retry:
8333 type_low_bound = TYPE_MIN_VALUE (type);
8334 type_high_bound = TYPE_MAX_VALUE (type);
8336 /* If at least one bound of the type is a constant integer, we can check
8337 ourselves and maybe make a decision. If no such decision is possible, but
8338 this type is a subtype, try checking against that. Otherwise, use
8339 double_int_fits_to_tree_p, which checks against the precision.
8341 Compute the status for each possibly constant bound, and return if we see
8342 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8343 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8344 for "constant known to fit". */
8346 /* Check if c >= type_low_bound. */
8347 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8349 dd = tree_to_double_int (type_low_bound);
8350 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
8352 int c_neg = (!unsc && dc.is_negative ());
8353 int t_neg = (unsc && dd.is_negative ());
8355 if (c_neg && !t_neg)
8356 return false;
8357 if ((c_neg || !t_neg) && dc.ult (dd))
8358 return false;
8360 else if (dc.cmp (dd, unsc) < 0)
8361 return false;
8362 ok_for_low_bound = true;
8364 else
8365 ok_for_low_bound = false;
8367 /* Check if c <= type_high_bound. */
8368 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8370 dd = tree_to_double_int (type_high_bound);
8371 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
8373 int c_neg = (!unsc && dc.is_negative ());
8374 int t_neg = (unsc && dd.is_negative ());
8376 if (t_neg && !c_neg)
8377 return false;
8378 if ((t_neg || !c_neg) && dc.ugt (dd))
8379 return false;
8381 else if (dc.cmp (dd, unsc) > 0)
8382 return false;
8383 ok_for_high_bound = true;
8385 else
8386 ok_for_high_bound = false;
8388 /* If the constant fits both bounds, the result is known. */
8389 if (ok_for_low_bound && ok_for_high_bound)
8390 return true;
8392 /* Perform some generic filtering which may allow making a decision
8393 even if the bounds are not constant. First, negative integers
8394 never fit in unsigned types, */
8395 if (TYPE_UNSIGNED (type) && !unsc && dc.is_negative ())
8396 return false;
8398 /* Second, narrower types always fit in wider ones. */
8399 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8400 return true;
8402 /* Third, unsigned integers with top bit set never fit signed types. */
8403 if (! TYPE_UNSIGNED (type) && unsc)
8405 int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (c))) - 1;
8406 if (prec < HOST_BITS_PER_WIDE_INT)
8408 if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
8409 return false;
8411 else if (((((unsigned HOST_WIDE_INT) 1)
8412 << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
8413 return false;
8416 /* If we haven't been able to decide at this point, there nothing more we
8417 can check ourselves here. Look at the base type if we have one and it
8418 has the same precision. */
8419 if (TREE_CODE (type) == INTEGER_TYPE
8420 && TREE_TYPE (type) != 0
8421 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8423 type = TREE_TYPE (type);
8424 goto retry;
8427 /* Or to double_int_fits_to_tree_p, if nothing else. */
8428 return double_int_fits_to_tree_p (type, dc);
8431 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8432 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8433 represented (assuming two's-complement arithmetic) within the bit
8434 precision of the type are returned instead. */
8436 void
8437 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8439 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8440 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8441 mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
8442 TYPE_UNSIGNED (type));
8443 else
8445 if (TYPE_UNSIGNED (type))
8446 mpz_set_ui (min, 0);
8447 else
8449 double_int mn;
8450 mn = double_int::mask (TYPE_PRECISION (type) - 1);
8451 mn = (mn + double_int_one).sext (TYPE_PRECISION (type));
8452 mpz_set_double_int (min, mn, false);
8456 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8457 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8458 mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
8459 TYPE_UNSIGNED (type));
8460 else
8462 if (TYPE_UNSIGNED (type))
8463 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)),
8464 true);
8465 else
8466 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type) - 1),
8467 true);
8471 /* Return true if VAR is an automatic variable defined in function FN. */
8473 bool
8474 auto_var_in_fn_p (const_tree var, const_tree fn)
8476 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8477 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8478 || TREE_CODE (var) == PARM_DECL)
8479 && ! TREE_STATIC (var))
8480 || TREE_CODE (var) == LABEL_DECL
8481 || TREE_CODE (var) == RESULT_DECL));
8484 /* Subprogram of following function. Called by walk_tree.
8486 Return *TP if it is an automatic variable or parameter of the
8487 function passed in as DATA. */
8489 static tree
8490 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8492 tree fn = (tree) data;
8494 if (TYPE_P (*tp))
8495 *walk_subtrees = 0;
8497 else if (DECL_P (*tp)
8498 && auto_var_in_fn_p (*tp, fn))
8499 return *tp;
8501 return NULL_TREE;
8504 /* Returns true if T is, contains, or refers to a type with variable
8505 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8506 arguments, but not the return type. If FN is nonzero, only return
8507 true if a modifier of the type or position of FN is a variable or
8508 parameter inside FN.
8510 This concept is more general than that of C99 'variably modified types':
8511 in C99, a struct type is never variably modified because a VLA may not
8512 appear as a structure member. However, in GNU C code like:
8514 struct S { int i[f()]; };
8516 is valid, and other languages may define similar constructs. */
8518 bool
8519 variably_modified_type_p (tree type, tree fn)
8521 tree t;
8523 /* Test if T is either variable (if FN is zero) or an expression containing
8524 a variable in FN. If TYPE isn't gimplified, return true also if
8525 gimplify_one_sizepos would gimplify the expression into a local
8526 variable. */
8527 #define RETURN_TRUE_IF_VAR(T) \
8528 do { tree _t = (T); \
8529 if (_t != NULL_TREE \
8530 && _t != error_mark_node \
8531 && TREE_CODE (_t) != INTEGER_CST \
8532 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8533 && (!fn \
8534 || (!TYPE_SIZES_GIMPLIFIED (type) \
8535 && !is_gimple_sizepos (_t)) \
8536 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8537 return true; } while (0)
8539 if (type == error_mark_node)
8540 return false;
8542 /* If TYPE itself has variable size, it is variably modified. */
8543 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8544 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8546 switch (TREE_CODE (type))
8548 case POINTER_TYPE:
8549 case REFERENCE_TYPE:
8550 case VECTOR_TYPE:
8551 if (variably_modified_type_p (TREE_TYPE (type), fn))
8552 return true;
8553 break;
8555 case FUNCTION_TYPE:
8556 case METHOD_TYPE:
8557 /* If TYPE is a function type, it is variably modified if the
8558 return type is variably modified. */
8559 if (variably_modified_type_p (TREE_TYPE (type), fn))
8560 return true;
8561 break;
8563 case INTEGER_TYPE:
8564 case REAL_TYPE:
8565 case FIXED_POINT_TYPE:
8566 case ENUMERAL_TYPE:
8567 case BOOLEAN_TYPE:
8568 /* Scalar types are variably modified if their end points
8569 aren't constant. */
8570 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8571 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8572 break;
8574 case RECORD_TYPE:
8575 case UNION_TYPE:
8576 case QUAL_UNION_TYPE:
8577 /* We can't see if any of the fields are variably-modified by the
8578 definition we normally use, since that would produce infinite
8579 recursion via pointers. */
8580 /* This is variably modified if some field's type is. */
8581 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8582 if (TREE_CODE (t) == FIELD_DECL)
8584 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8585 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8586 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8588 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8589 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8591 break;
8593 case ARRAY_TYPE:
8594 /* Do not call ourselves to avoid infinite recursion. This is
8595 variably modified if the element type is. */
8596 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8597 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8598 break;
8600 default:
8601 break;
8604 /* The current language may have other cases to check, but in general,
8605 all other types are not variably modified. */
8606 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8608 #undef RETURN_TRUE_IF_VAR
8611 /* Given a DECL or TYPE, return the scope in which it was declared, or
8612 NULL_TREE if there is no containing scope. */
8614 tree
8615 get_containing_scope (const_tree t)
8617 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8620 /* Return the innermost context enclosing DECL that is
8621 a FUNCTION_DECL, or zero if none. */
8623 tree
8624 decl_function_context (const_tree decl)
8626 tree context;
8628 if (TREE_CODE (decl) == ERROR_MARK)
8629 return 0;
8631 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8632 where we look up the function at runtime. Such functions always take
8633 a first argument of type 'pointer to real context'.
8635 C++ should really be fixed to use DECL_CONTEXT for the real context,
8636 and use something else for the "virtual context". */
8637 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8638 context
8639 = TYPE_MAIN_VARIANT
8640 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8641 else
8642 context = DECL_CONTEXT (decl);
8644 while (context && TREE_CODE (context) != FUNCTION_DECL)
8646 if (TREE_CODE (context) == BLOCK)
8647 context = BLOCK_SUPERCONTEXT (context);
8648 else
8649 context = get_containing_scope (context);
8652 return context;
8655 /* Return the innermost context enclosing DECL that is
8656 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8657 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8659 tree
8660 decl_type_context (const_tree decl)
8662 tree context = DECL_CONTEXT (decl);
8664 while (context)
8665 switch (TREE_CODE (context))
8667 case NAMESPACE_DECL:
8668 case TRANSLATION_UNIT_DECL:
8669 return NULL_TREE;
8671 case RECORD_TYPE:
8672 case UNION_TYPE:
8673 case QUAL_UNION_TYPE:
8674 return context;
8676 case TYPE_DECL:
8677 case FUNCTION_DECL:
8678 context = DECL_CONTEXT (context);
8679 break;
8681 case BLOCK:
8682 context = BLOCK_SUPERCONTEXT (context);
8683 break;
8685 default:
8686 gcc_unreachable ();
8689 return NULL_TREE;
8692 /* CALL is a CALL_EXPR. Return the declaration for the function
8693 called, or NULL_TREE if the called function cannot be
8694 determined. */
8696 tree
8697 get_callee_fndecl (const_tree call)
8699 tree addr;
8701 if (call == error_mark_node)
8702 return error_mark_node;
8704 /* It's invalid to call this function with anything but a
8705 CALL_EXPR. */
8706 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8708 /* The first operand to the CALL is the address of the function
8709 called. */
8710 addr = CALL_EXPR_FN (call);
8712 STRIP_NOPS (addr);
8714 /* If this is a readonly function pointer, extract its initial value. */
8715 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8716 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8717 && DECL_INITIAL (addr))
8718 addr = DECL_INITIAL (addr);
8720 /* If the address is just `&f' for some function `f', then we know
8721 that `f' is being called. */
8722 if (TREE_CODE (addr) == ADDR_EXPR
8723 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8724 return TREE_OPERAND (addr, 0);
8726 /* We couldn't figure out what was being called. */
8727 return NULL_TREE;
8730 /* Print debugging information about tree nodes generated during the compile,
8731 and any language-specific information. */
8733 void
8734 dump_tree_statistics (void)
8736 if (GATHER_STATISTICS)
8738 int i;
8739 int total_nodes, total_bytes;
8740 fprintf (stderr, "Kind Nodes Bytes\n");
8741 fprintf (stderr, "---------------------------------------\n");
8742 total_nodes = total_bytes = 0;
8743 for (i = 0; i < (int) all_kinds; i++)
8745 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
8746 tree_node_counts[i], tree_node_sizes[i]);
8747 total_nodes += tree_node_counts[i];
8748 total_bytes += tree_node_sizes[i];
8750 fprintf (stderr, "---------------------------------------\n");
8751 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
8752 fprintf (stderr, "---------------------------------------\n");
8753 fprintf (stderr, "Code Nodes\n");
8754 fprintf (stderr, "----------------------------\n");
8755 for (i = 0; i < (int) MAX_TREE_CODES; i++)
8756 fprintf (stderr, "%-20s %7d\n", tree_code_name[i], tree_code_counts[i]);
8757 fprintf (stderr, "----------------------------\n");
8758 ssanames_print_statistics ();
8759 phinodes_print_statistics ();
8761 else
8762 fprintf (stderr, "(No per-node statistics)\n");
8764 print_type_hash_statistics ();
8765 print_debug_expr_statistics ();
8766 print_value_expr_statistics ();
8767 lang_hooks.print_statistics ();
8770 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8772 /* Generate a crc32 of a byte. */
8774 static unsigned
8775 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
8777 unsigned ix;
8779 for (ix = bits; ix--; value <<= 1)
8781 unsigned feedback;
8783 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
8784 chksum <<= 1;
8785 chksum ^= feedback;
8787 return chksum;
8790 /* Generate a crc32 of a 32-bit unsigned. */
8792 unsigned
8793 crc32_unsigned (unsigned chksum, unsigned value)
8795 return crc32_unsigned_bits (chksum, value, 32);
8798 /* Generate a crc32 of a byte. */
8800 unsigned
8801 crc32_byte (unsigned chksum, char byte)
8803 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
8806 /* Generate a crc32 of a string. */
8808 unsigned
8809 crc32_string (unsigned chksum, const char *string)
8813 chksum = crc32_byte (chksum, *string);
8815 while (*string++);
8816 return chksum;
8819 /* P is a string that will be used in a symbol. Mask out any characters
8820 that are not valid in that context. */
8822 void
8823 clean_symbol_name (char *p)
8825 for (; *p; p++)
8826 if (! (ISALNUM (*p)
8827 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
8828 || *p == '$'
8829 #endif
8830 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
8831 || *p == '.'
8832 #endif
8834 *p = '_';
8837 /* Generate a name for a special-purpose function.
8838 The generated name may need to be unique across the whole link.
8839 Changes to this function may also require corresponding changes to
8840 xstrdup_mask_random.
8841 TYPE is some string to identify the purpose of this function to the
8842 linker or collect2; it must start with an uppercase letter,
8843 one of:
8844 I - for constructors
8845 D - for destructors
8846 N - for C++ anonymous namespaces
8847 F - for DWARF unwind frame information. */
8849 tree
8850 get_file_function_name (const char *type)
8852 char *buf;
8853 const char *p;
8854 char *q;
8856 /* If we already have a name we know to be unique, just use that. */
8857 if (first_global_object_name)
8858 p = q = ASTRDUP (first_global_object_name);
8859 /* If the target is handling the constructors/destructors, they
8860 will be local to this file and the name is only necessary for
8861 debugging purposes.
8862 We also assign sub_I and sub_D sufixes to constructors called from
8863 the global static constructors. These are always local. */
8864 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
8865 || (strncmp (type, "sub_", 4) == 0
8866 && (type[4] == 'I' || type[4] == 'D')))
8868 const char *file = main_input_filename;
8869 if (! file)
8870 file = input_filename;
8871 /* Just use the file's basename, because the full pathname
8872 might be quite long. */
8873 p = q = ASTRDUP (lbasename (file));
8875 else
8877 /* Otherwise, the name must be unique across the entire link.
8878 We don't have anything that we know to be unique to this translation
8879 unit, so use what we do have and throw in some randomness. */
8880 unsigned len;
8881 const char *name = weak_global_object_name;
8882 const char *file = main_input_filename;
8884 if (! name)
8885 name = "";
8886 if (! file)
8887 file = input_filename;
8889 len = strlen (file);
8890 q = (char *) alloca (9 + 17 + len + 1);
8891 memcpy (q, file, len + 1);
8893 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
8894 crc32_string (0, name), get_random_seed (false));
8896 p = q;
8899 clean_symbol_name (q);
8900 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
8901 + strlen (type));
8903 /* Set up the name of the file-level functions we may need.
8904 Use a global object (which is already required to be unique over
8905 the program) rather than the file name (which imposes extra
8906 constraints). */
8907 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
8909 return get_identifier (buf);
8912 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
8914 /* Complain that the tree code of NODE does not match the expected 0
8915 terminated list of trailing codes. The trailing code list can be
8916 empty, for a more vague error message. FILE, LINE, and FUNCTION
8917 are of the caller. */
8919 void
8920 tree_check_failed (const_tree node, const char *file,
8921 int line, const char *function, ...)
8923 va_list args;
8924 const char *buffer;
8925 unsigned length = 0;
8926 int code;
8928 va_start (args, function);
8929 while ((code = va_arg (args, int)))
8930 length += 4 + strlen (tree_code_name[code]);
8931 va_end (args);
8932 if (length)
8934 char *tmp;
8935 va_start (args, function);
8936 length += strlen ("expected ");
8937 buffer = tmp = (char *) alloca (length);
8938 length = 0;
8939 while ((code = va_arg (args, int)))
8941 const char *prefix = length ? " or " : "expected ";
8943 strcpy (tmp + length, prefix);
8944 length += strlen (prefix);
8945 strcpy (tmp + length, tree_code_name[code]);
8946 length += strlen (tree_code_name[code]);
8948 va_end (args);
8950 else
8951 buffer = "unexpected node";
8953 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8954 buffer, tree_code_name[TREE_CODE (node)],
8955 function, trim_filename (file), line);
8958 /* Complain that the tree code of NODE does match the expected 0
8959 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
8960 the caller. */
8962 void
8963 tree_not_check_failed (const_tree node, const char *file,
8964 int line, const char *function, ...)
8966 va_list args;
8967 char *buffer;
8968 unsigned length = 0;
8969 int code;
8971 va_start (args, function);
8972 while ((code = va_arg (args, int)))
8973 length += 4 + strlen (tree_code_name[code]);
8974 va_end (args);
8975 va_start (args, function);
8976 buffer = (char *) alloca (length);
8977 length = 0;
8978 while ((code = va_arg (args, int)))
8980 if (length)
8982 strcpy (buffer + length, " or ");
8983 length += 4;
8985 strcpy (buffer + length, tree_code_name[code]);
8986 length += strlen (tree_code_name[code]);
8988 va_end (args);
8990 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
8991 buffer, tree_code_name[TREE_CODE (node)],
8992 function, trim_filename (file), line);
8995 /* Similar to tree_check_failed, except that we check for a class of tree
8996 code, given in CL. */
8998 void
8999 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9000 const char *file, int line, const char *function)
9002 internal_error
9003 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9004 TREE_CODE_CLASS_STRING (cl),
9005 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9006 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9009 /* Similar to tree_check_failed, except that instead of specifying a
9010 dozen codes, use the knowledge that they're all sequential. */
9012 void
9013 tree_range_check_failed (const_tree node, const char *file, int line,
9014 const char *function, enum tree_code c1,
9015 enum tree_code c2)
9017 char *buffer;
9018 unsigned length = 0;
9019 unsigned int c;
9021 for (c = c1; c <= c2; ++c)
9022 length += 4 + strlen (tree_code_name[c]);
9024 length += strlen ("expected ");
9025 buffer = (char *) alloca (length);
9026 length = 0;
9028 for (c = c1; c <= c2; ++c)
9030 const char *prefix = length ? " or " : "expected ";
9032 strcpy (buffer + length, prefix);
9033 length += strlen (prefix);
9034 strcpy (buffer + length, tree_code_name[c]);
9035 length += strlen (tree_code_name[c]);
9038 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9039 buffer, tree_code_name[TREE_CODE (node)],
9040 function, trim_filename (file), line);
9044 /* Similar to tree_check_failed, except that we check that a tree does
9045 not have the specified code, given in CL. */
9047 void
9048 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9049 const char *file, int line, const char *function)
9051 internal_error
9052 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9053 TREE_CODE_CLASS_STRING (cl),
9054 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9055 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9059 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9061 void
9062 omp_clause_check_failed (const_tree node, const char *file, int line,
9063 const char *function, enum omp_clause_code code)
9065 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9066 omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
9067 function, trim_filename (file), line);
9071 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9073 void
9074 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9075 const char *function, enum omp_clause_code c1,
9076 enum omp_clause_code c2)
9078 char *buffer;
9079 unsigned length = 0;
9080 unsigned int c;
9082 for (c = c1; c <= c2; ++c)
9083 length += 4 + strlen (omp_clause_code_name[c]);
9085 length += strlen ("expected ");
9086 buffer = (char *) alloca (length);
9087 length = 0;
9089 for (c = c1; c <= c2; ++c)
9091 const char *prefix = length ? " or " : "expected ";
9093 strcpy (buffer + length, prefix);
9094 length += strlen (prefix);
9095 strcpy (buffer + length, omp_clause_code_name[c]);
9096 length += strlen (omp_clause_code_name[c]);
9099 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9100 buffer, omp_clause_code_name[TREE_CODE (node)],
9101 function, trim_filename (file), line);
9105 #undef DEFTREESTRUCT
9106 #define DEFTREESTRUCT(VAL, NAME) NAME,
9108 static const char *ts_enum_names[] = {
9109 #include "treestruct.def"
9111 #undef DEFTREESTRUCT
9113 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9115 /* Similar to tree_class_check_failed, except that we check for
9116 whether CODE contains the tree structure identified by EN. */
9118 void
9119 tree_contains_struct_check_failed (const_tree node,
9120 const enum tree_node_structure_enum en,
9121 const char *file, int line,
9122 const char *function)
9124 internal_error
9125 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9126 TS_ENUM_NAME(en),
9127 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9131 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9132 (dynamically sized) vector. */
9134 void
9135 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9136 const char *function)
9138 internal_error
9139 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9140 idx + 1, len, function, trim_filename (file), line);
9143 /* Similar to above, except that the check is for the bounds of the operand
9144 vector of an expression node EXP. */
9146 void
9147 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9148 int line, const char *function)
9150 int code = TREE_CODE (exp);
9151 internal_error
9152 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9153 idx + 1, tree_code_name[code], TREE_OPERAND_LENGTH (exp),
9154 function, trim_filename (file), line);
9157 /* Similar to above, except that the check is for the number of
9158 operands of an OMP_CLAUSE node. */
9160 void
9161 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9162 int line, const char *function)
9164 internal_error
9165 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9166 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9167 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9168 trim_filename (file), line);
9170 #endif /* ENABLE_TREE_CHECKING */
9172 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9173 and mapped to the machine mode MODE. Initialize its fields and build
9174 the information necessary for debugging output. */
9176 static tree
9177 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
9179 tree t;
9180 hashval_t hashcode = 0;
9182 t = make_node (VECTOR_TYPE);
9183 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9184 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9185 SET_TYPE_MODE (t, mode);
9187 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9188 SET_TYPE_STRUCTURAL_EQUALITY (t);
9189 else if (TYPE_CANONICAL (innertype) != innertype
9190 || mode != VOIDmode)
9191 TYPE_CANONICAL (t)
9192 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9194 layout_type (t);
9196 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
9197 hashcode = iterative_hash_host_wide_int (nunits, hashcode);
9198 hashcode = iterative_hash_host_wide_int (mode, hashcode);
9199 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (t)), hashcode);
9200 t = type_hash_canon (hashcode, t);
9202 /* We have built a main variant, based on the main variant of the
9203 inner type. Use it to build the variant we return. */
9204 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9205 && TREE_TYPE (t) != innertype)
9206 return build_type_attribute_qual_variant (t,
9207 TYPE_ATTRIBUTES (innertype),
9208 TYPE_QUALS (innertype));
9210 return t;
9213 static tree
9214 make_or_reuse_type (unsigned size, int unsignedp)
9216 if (size == INT_TYPE_SIZE)
9217 return unsignedp ? unsigned_type_node : integer_type_node;
9218 if (size == CHAR_TYPE_SIZE)
9219 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9220 if (size == SHORT_TYPE_SIZE)
9221 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9222 if (size == LONG_TYPE_SIZE)
9223 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9224 if (size == LONG_LONG_TYPE_SIZE)
9225 return (unsignedp ? long_long_unsigned_type_node
9226 : long_long_integer_type_node);
9227 if (size == 128 && int128_integer_type_node)
9228 return (unsignedp ? int128_unsigned_type_node
9229 : int128_integer_type_node);
9231 if (unsignedp)
9232 return make_unsigned_type (size);
9233 else
9234 return make_signed_type (size);
9237 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9239 static tree
9240 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9242 if (satp)
9244 if (size == SHORT_FRACT_TYPE_SIZE)
9245 return unsignedp ? sat_unsigned_short_fract_type_node
9246 : sat_short_fract_type_node;
9247 if (size == FRACT_TYPE_SIZE)
9248 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9249 if (size == LONG_FRACT_TYPE_SIZE)
9250 return unsignedp ? sat_unsigned_long_fract_type_node
9251 : sat_long_fract_type_node;
9252 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9253 return unsignedp ? sat_unsigned_long_long_fract_type_node
9254 : sat_long_long_fract_type_node;
9256 else
9258 if (size == SHORT_FRACT_TYPE_SIZE)
9259 return unsignedp ? unsigned_short_fract_type_node
9260 : short_fract_type_node;
9261 if (size == FRACT_TYPE_SIZE)
9262 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9263 if (size == LONG_FRACT_TYPE_SIZE)
9264 return unsignedp ? unsigned_long_fract_type_node
9265 : long_fract_type_node;
9266 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9267 return unsignedp ? unsigned_long_long_fract_type_node
9268 : long_long_fract_type_node;
9271 return make_fract_type (size, unsignedp, satp);
9274 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9276 static tree
9277 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9279 if (satp)
9281 if (size == SHORT_ACCUM_TYPE_SIZE)
9282 return unsignedp ? sat_unsigned_short_accum_type_node
9283 : sat_short_accum_type_node;
9284 if (size == ACCUM_TYPE_SIZE)
9285 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9286 if (size == LONG_ACCUM_TYPE_SIZE)
9287 return unsignedp ? sat_unsigned_long_accum_type_node
9288 : sat_long_accum_type_node;
9289 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9290 return unsignedp ? sat_unsigned_long_long_accum_type_node
9291 : sat_long_long_accum_type_node;
9293 else
9295 if (size == SHORT_ACCUM_TYPE_SIZE)
9296 return unsignedp ? unsigned_short_accum_type_node
9297 : short_accum_type_node;
9298 if (size == ACCUM_TYPE_SIZE)
9299 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9300 if (size == LONG_ACCUM_TYPE_SIZE)
9301 return unsignedp ? unsigned_long_accum_type_node
9302 : long_accum_type_node;
9303 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9304 return unsignedp ? unsigned_long_long_accum_type_node
9305 : long_long_accum_type_node;
9308 return make_accum_type (size, unsignedp, satp);
9311 /* Create nodes for all integer types (and error_mark_node) using the sizes
9312 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9313 SHORT_DOUBLE specifies whether double should be of the same precision
9314 as float. */
9316 void
9317 build_common_tree_nodes (bool signed_char, bool short_double)
9319 error_mark_node = make_node (ERROR_MARK);
9320 TREE_TYPE (error_mark_node) = error_mark_node;
9322 initialize_sizetypes ();
9324 /* Define both `signed char' and `unsigned char'. */
9325 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9326 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9327 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9328 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9330 /* Define `char', which is like either `signed char' or `unsigned char'
9331 but not the same as either. */
9332 char_type_node
9333 = (signed_char
9334 ? make_signed_type (CHAR_TYPE_SIZE)
9335 : make_unsigned_type (CHAR_TYPE_SIZE));
9336 TYPE_STRING_FLAG (char_type_node) = 1;
9338 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9339 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9340 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9341 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9342 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9343 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9344 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9345 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9346 #if HOST_BITS_PER_WIDE_INT >= 64
9347 /* TODO: This isn't correct, but as logic depends at the moment on
9348 host's instead of target's wide-integer.
9349 If there is a target not supporting TImode, but has an 128-bit
9350 integer-scalar register, this target check needs to be adjusted. */
9351 if (targetm.scalar_mode_supported_p (TImode))
9353 int128_integer_type_node = make_signed_type (128);
9354 int128_unsigned_type_node = make_unsigned_type (128);
9356 #endif
9358 /* Define a boolean type. This type only represents boolean values but
9359 may be larger than char depending on the value of BOOL_TYPE_SIZE.
9360 Front ends which want to override this size (i.e. Java) can redefine
9361 boolean_type_node before calling build_common_tree_nodes_2. */
9362 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9363 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9364 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9365 TYPE_PRECISION (boolean_type_node) = 1;
9367 /* Define what type to use for size_t. */
9368 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9369 size_type_node = unsigned_type_node;
9370 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9371 size_type_node = long_unsigned_type_node;
9372 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9373 size_type_node = long_long_unsigned_type_node;
9374 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9375 size_type_node = short_unsigned_type_node;
9376 else
9377 gcc_unreachable ();
9379 /* Fill in the rest of the sized types. Reuse existing type nodes
9380 when possible. */
9381 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9382 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9383 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9384 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9385 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9387 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9388 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9389 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9390 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9391 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9393 access_public_node = get_identifier ("public");
9394 access_protected_node = get_identifier ("protected");
9395 access_private_node = get_identifier ("private");
9397 /* Define these next since types below may used them. */
9398 integer_zero_node = build_int_cst (integer_type_node, 0);
9399 integer_one_node = build_int_cst (integer_type_node, 1);
9400 integer_three_node = build_int_cst (integer_type_node, 3);
9401 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9403 size_zero_node = size_int (0);
9404 size_one_node = size_int (1);
9405 bitsize_zero_node = bitsize_int (0);
9406 bitsize_one_node = bitsize_int (1);
9407 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9409 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9410 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9412 void_type_node = make_node (VOID_TYPE);
9413 layout_type (void_type_node);
9415 /* We are not going to have real types in C with less than byte alignment,
9416 so we might as well not have any types that claim to have it. */
9417 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9418 TYPE_USER_ALIGN (void_type_node) = 0;
9420 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9421 layout_type (TREE_TYPE (null_pointer_node));
9423 ptr_type_node = build_pointer_type (void_type_node);
9424 const_ptr_type_node
9425 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9426 fileptr_type_node = ptr_type_node;
9428 float_type_node = make_node (REAL_TYPE);
9429 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9430 layout_type (float_type_node);
9432 double_type_node = make_node (REAL_TYPE);
9433 if (short_double)
9434 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9435 else
9436 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9437 layout_type (double_type_node);
9439 long_double_type_node = make_node (REAL_TYPE);
9440 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9441 layout_type (long_double_type_node);
9443 float_ptr_type_node = build_pointer_type (float_type_node);
9444 double_ptr_type_node = build_pointer_type (double_type_node);
9445 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9446 integer_ptr_type_node = build_pointer_type (integer_type_node);
9448 /* Fixed size integer types. */
9449 uint16_type_node = build_nonstandard_integer_type (16, true);
9450 uint32_type_node = build_nonstandard_integer_type (32, true);
9451 uint64_type_node = build_nonstandard_integer_type (64, true);
9453 /* Decimal float types. */
9454 dfloat32_type_node = make_node (REAL_TYPE);
9455 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9456 layout_type (dfloat32_type_node);
9457 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9458 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9460 dfloat64_type_node = make_node (REAL_TYPE);
9461 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9462 layout_type (dfloat64_type_node);
9463 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9464 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9466 dfloat128_type_node = make_node (REAL_TYPE);
9467 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9468 layout_type (dfloat128_type_node);
9469 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9470 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9472 complex_integer_type_node = build_complex_type (integer_type_node);
9473 complex_float_type_node = build_complex_type (float_type_node);
9474 complex_double_type_node = build_complex_type (double_type_node);
9475 complex_long_double_type_node = build_complex_type (long_double_type_node);
9477 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9478 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9479 sat_ ## KIND ## _type_node = \
9480 make_sat_signed_ ## KIND ## _type (SIZE); \
9481 sat_unsigned_ ## KIND ## _type_node = \
9482 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9483 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9484 unsigned_ ## KIND ## _type_node = \
9485 make_unsigned_ ## KIND ## _type (SIZE);
9487 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9488 sat_ ## WIDTH ## KIND ## _type_node = \
9489 make_sat_signed_ ## KIND ## _type (SIZE); \
9490 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9491 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9492 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9493 unsigned_ ## WIDTH ## KIND ## _type_node = \
9494 make_unsigned_ ## KIND ## _type (SIZE);
9496 /* Make fixed-point type nodes based on four different widths. */
9497 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9498 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9499 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9500 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9501 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9503 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9504 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9505 NAME ## _type_node = \
9506 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9507 u ## NAME ## _type_node = \
9508 make_or_reuse_unsigned_ ## KIND ## _type \
9509 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9510 sat_ ## NAME ## _type_node = \
9511 make_or_reuse_sat_signed_ ## KIND ## _type \
9512 (GET_MODE_BITSIZE (MODE ## mode)); \
9513 sat_u ## NAME ## _type_node = \
9514 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9515 (GET_MODE_BITSIZE (U ## MODE ## mode));
9517 /* Fixed-point type and mode nodes. */
9518 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9519 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9520 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9521 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9522 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9523 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9524 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9525 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9526 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9527 MAKE_FIXED_MODE_NODE (accum, da, DA)
9528 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9531 tree t = targetm.build_builtin_va_list ();
9533 /* Many back-ends define record types without setting TYPE_NAME.
9534 If we copied the record type here, we'd keep the original
9535 record type without a name. This breaks name mangling. So,
9536 don't copy record types and let c_common_nodes_and_builtins()
9537 declare the type to be __builtin_va_list. */
9538 if (TREE_CODE (t) != RECORD_TYPE)
9539 t = build_variant_type_copy (t);
9541 va_list_type_node = t;
9545 /* Modify DECL for given flags. */
9546 void
9547 set_call_expr_flags (tree decl, int flags)
9549 if (flags & ECF_NOTHROW)
9550 TREE_NOTHROW (decl) = 1;
9551 if (flags & ECF_CONST)
9552 TREE_READONLY (decl) = 1;
9553 if (flags & ECF_PURE)
9554 DECL_PURE_P (decl) = 1;
9555 if (flags & ECF_LOOPING_CONST_OR_PURE)
9556 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9557 if (flags & ECF_NOVOPS)
9558 DECL_IS_NOVOPS (decl) = 1;
9559 if (flags & ECF_NORETURN)
9560 TREE_THIS_VOLATILE (decl) = 1;
9561 if (flags & ECF_MALLOC)
9562 DECL_IS_MALLOC (decl) = 1;
9563 if (flags & ECF_RETURNS_TWICE)
9564 DECL_IS_RETURNS_TWICE (decl) = 1;
9565 if (flags & ECF_LEAF)
9566 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9567 NULL, DECL_ATTRIBUTES (decl));
9568 if ((flags & ECF_TM_PURE) && flag_tm)
9569 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("transaction_pure"),
9570 NULL, DECL_ATTRIBUTES (decl));
9571 /* Looping const or pure is implied by noreturn.
9572 There is currently no way to declare looping const or looping pure alone. */
9573 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9574 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9578 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9580 static void
9581 local_define_builtin (const char *name, tree type, enum built_in_function code,
9582 const char *library_name, int ecf_flags)
9584 tree decl;
9586 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9587 library_name, NULL_TREE);
9588 set_call_expr_flags (decl, ecf_flags);
9590 set_builtin_decl (code, decl, true);
9593 /* Call this function after instantiating all builtins that the language
9594 front end cares about. This will build the rest of the builtins that
9595 are relied upon by the tree optimizers and the middle-end. */
9597 void
9598 build_common_builtin_nodes (void)
9600 tree tmp, ftype;
9601 int ecf_flags;
9603 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9605 ftype = build_function_type (void_type_node, void_list_node);
9606 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
9607 "__builtin_unreachable",
9608 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9609 | ECF_CONST | ECF_LEAF);
9612 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9613 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9615 ftype = build_function_type_list (ptr_type_node,
9616 ptr_type_node, const_ptr_type_node,
9617 size_type_node, NULL_TREE);
9619 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
9620 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9621 "memcpy", ECF_NOTHROW | ECF_LEAF);
9622 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9623 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9624 "memmove", ECF_NOTHROW | ECF_LEAF);
9627 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
9629 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9630 const_ptr_type_node, size_type_node,
9631 NULL_TREE);
9632 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9633 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9636 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
9638 ftype = build_function_type_list (ptr_type_node,
9639 ptr_type_node, integer_type_node,
9640 size_type_node, NULL_TREE);
9641 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9642 "memset", ECF_NOTHROW | ECF_LEAF);
9645 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
9647 ftype = build_function_type_list (ptr_type_node,
9648 size_type_node, NULL_TREE);
9649 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9650 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9653 ftype = build_function_type_list (ptr_type_node, size_type_node,
9654 size_type_node, NULL_TREE);
9655 local_define_builtin ("__builtin_alloca_with_align", ftype,
9656 BUILT_IN_ALLOCA_WITH_ALIGN, "alloca",
9657 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9659 /* If we're checking the stack, `alloca' can throw. */
9660 if (flag_stack_check)
9662 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
9663 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
9666 ftype = build_function_type_list (void_type_node,
9667 ptr_type_node, ptr_type_node,
9668 ptr_type_node, NULL_TREE);
9669 local_define_builtin ("__builtin_init_trampoline", ftype,
9670 BUILT_IN_INIT_TRAMPOLINE,
9671 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9672 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
9673 BUILT_IN_INIT_HEAP_TRAMPOLINE,
9674 "__builtin_init_heap_trampoline",
9675 ECF_NOTHROW | ECF_LEAF);
9677 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9678 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9679 BUILT_IN_ADJUST_TRAMPOLINE,
9680 "__builtin_adjust_trampoline",
9681 ECF_CONST | ECF_NOTHROW);
9683 ftype = build_function_type_list (void_type_node,
9684 ptr_type_node, ptr_type_node, NULL_TREE);
9685 local_define_builtin ("__builtin_nonlocal_goto", ftype,
9686 BUILT_IN_NONLOCAL_GOTO,
9687 "__builtin_nonlocal_goto",
9688 ECF_NORETURN | ECF_NOTHROW);
9690 ftype = build_function_type_list (void_type_node,
9691 ptr_type_node, ptr_type_node, NULL_TREE);
9692 local_define_builtin ("__builtin_setjmp_setup", ftype,
9693 BUILT_IN_SETJMP_SETUP,
9694 "__builtin_setjmp_setup", ECF_NOTHROW);
9696 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9697 local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
9698 BUILT_IN_SETJMP_DISPATCHER,
9699 "__builtin_setjmp_dispatcher",
9700 ECF_PURE | ECF_NOTHROW);
9702 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9703 local_define_builtin ("__builtin_setjmp_receiver", ftype,
9704 BUILT_IN_SETJMP_RECEIVER,
9705 "__builtin_setjmp_receiver", ECF_NOTHROW);
9707 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9708 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
9709 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9711 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9712 local_define_builtin ("__builtin_stack_restore", ftype,
9713 BUILT_IN_STACK_RESTORE,
9714 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9716 /* If there's a possibility that we might use the ARM EABI, build the
9717 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
9718 if (targetm.arm_eabi_unwinder)
9720 ftype = build_function_type_list (void_type_node, NULL_TREE);
9721 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
9722 BUILT_IN_CXA_END_CLEANUP,
9723 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
9726 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9727 local_define_builtin ("__builtin_unwind_resume", ftype,
9728 BUILT_IN_UNWIND_RESUME,
9729 ((targetm_common.except_unwind_info (&global_options)
9730 == UI_SJLJ)
9731 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
9732 ECF_NORETURN);
9734 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
9736 ftype = build_function_type_list (ptr_type_node, integer_type_node,
9737 NULL_TREE);
9738 local_define_builtin ("__builtin_return_address", ftype,
9739 BUILT_IN_RETURN_ADDRESS,
9740 "__builtin_return_address",
9741 ECF_NOTHROW);
9744 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
9745 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9747 ftype = build_function_type_list (void_type_node, ptr_type_node,
9748 ptr_type_node, NULL_TREE);
9749 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
9750 local_define_builtin ("__cyg_profile_func_enter", ftype,
9751 BUILT_IN_PROFILE_FUNC_ENTER,
9752 "__cyg_profile_func_enter", 0);
9753 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9754 local_define_builtin ("__cyg_profile_func_exit", ftype,
9755 BUILT_IN_PROFILE_FUNC_EXIT,
9756 "__cyg_profile_func_exit", 0);
9759 /* The exception object and filter values from the runtime. The argument
9760 must be zero before exception lowering, i.e. from the front end. After
9761 exception lowering, it will be the region number for the exception
9762 landing pad. These functions are PURE instead of CONST to prevent
9763 them from being hoisted past the exception edge that will initialize
9764 its value in the landing pad. */
9765 ftype = build_function_type_list (ptr_type_node,
9766 integer_type_node, NULL_TREE);
9767 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
9768 /* Only use TM_PURE if we we have TM language support. */
9769 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
9770 ecf_flags |= ECF_TM_PURE;
9771 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
9772 "__builtin_eh_pointer", ecf_flags);
9774 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
9775 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
9776 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
9777 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9779 ftype = build_function_type_list (void_type_node,
9780 integer_type_node, integer_type_node,
9781 NULL_TREE);
9782 local_define_builtin ("__builtin_eh_copy_values", ftype,
9783 BUILT_IN_EH_COPY_VALUES,
9784 "__builtin_eh_copy_values", ECF_NOTHROW);
9786 /* Complex multiplication and division. These are handled as builtins
9787 rather than optabs because emit_library_call_value doesn't support
9788 complex. Further, we can do slightly better with folding these
9789 beasties if the real and complex parts of the arguments are separate. */
9791 int mode;
9793 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
9795 char mode_name_buf[4], *q;
9796 const char *p;
9797 enum built_in_function mcode, dcode;
9798 tree type, inner_type;
9799 const char *prefix = "__";
9801 if (targetm.libfunc_gnu_prefix)
9802 prefix = "__gnu_";
9804 type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
9805 if (type == NULL)
9806 continue;
9807 inner_type = TREE_TYPE (type);
9809 ftype = build_function_type_list (type, inner_type, inner_type,
9810 inner_type, inner_type, NULL_TREE);
9812 mcode = ((enum built_in_function)
9813 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9814 dcode = ((enum built_in_function)
9815 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9817 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
9818 *q = TOLOWER (*p);
9819 *q = '\0';
9821 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
9822 NULL);
9823 local_define_builtin (built_in_names[mcode], ftype, mcode,
9824 built_in_names[mcode],
9825 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
9827 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
9828 NULL);
9829 local_define_builtin (built_in_names[dcode], ftype, dcode,
9830 built_in_names[dcode],
9831 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
9836 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
9837 better way.
9839 If we requested a pointer to a vector, build up the pointers that
9840 we stripped off while looking for the inner type. Similarly for
9841 return values from functions.
9843 The argument TYPE is the top of the chain, and BOTTOM is the
9844 new type which we will point to. */
9846 tree
9847 reconstruct_complex_type (tree type, tree bottom)
9849 tree inner, outer;
9851 if (TREE_CODE (type) == POINTER_TYPE)
9853 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9854 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
9855 TYPE_REF_CAN_ALIAS_ALL (type));
9857 else if (TREE_CODE (type) == REFERENCE_TYPE)
9859 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9860 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
9861 TYPE_REF_CAN_ALIAS_ALL (type));
9863 else if (TREE_CODE (type) == ARRAY_TYPE)
9865 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9866 outer = build_array_type (inner, TYPE_DOMAIN (type));
9868 else if (TREE_CODE (type) == FUNCTION_TYPE)
9870 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9871 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
9873 else if (TREE_CODE (type) == METHOD_TYPE)
9875 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9876 /* The build_method_type_directly() routine prepends 'this' to argument list,
9877 so we must compensate by getting rid of it. */
9878 outer
9879 = build_method_type_directly
9880 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
9881 inner,
9882 TREE_CHAIN (TYPE_ARG_TYPES (type)));
9884 else if (TREE_CODE (type) == OFFSET_TYPE)
9886 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9887 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
9889 else
9890 return bottom;
9892 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
9893 TYPE_QUALS (type));
9896 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
9897 the inner type. */
9898 tree
9899 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
9901 int nunits;
9903 switch (GET_MODE_CLASS (mode))
9905 case MODE_VECTOR_INT:
9906 case MODE_VECTOR_FLOAT:
9907 case MODE_VECTOR_FRACT:
9908 case MODE_VECTOR_UFRACT:
9909 case MODE_VECTOR_ACCUM:
9910 case MODE_VECTOR_UACCUM:
9911 nunits = GET_MODE_NUNITS (mode);
9912 break;
9914 case MODE_INT:
9915 /* Check that there are no leftover bits. */
9916 gcc_assert (GET_MODE_BITSIZE (mode)
9917 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
9919 nunits = GET_MODE_BITSIZE (mode)
9920 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
9921 break;
9923 default:
9924 gcc_unreachable ();
9927 return make_vector_type (innertype, nunits, mode);
9930 /* Similarly, but takes the inner type and number of units, which must be
9931 a power of two. */
9933 tree
9934 build_vector_type (tree innertype, int nunits)
9936 return make_vector_type (innertype, nunits, VOIDmode);
9939 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
9941 tree
9942 build_opaque_vector_type (tree innertype, int nunits)
9944 tree t = make_vector_type (innertype, nunits, VOIDmode);
9945 tree cand;
9946 /* We always build the non-opaque variant before the opaque one,
9947 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
9948 cand = TYPE_NEXT_VARIANT (t);
9949 if (cand
9950 && TYPE_VECTOR_OPAQUE (cand)
9951 && check_qualified_type (cand, t, TYPE_QUALS (t)))
9952 return cand;
9953 /* Othewise build a variant type and make sure to queue it after
9954 the non-opaque type. */
9955 cand = build_distinct_type_copy (t);
9956 TYPE_VECTOR_OPAQUE (cand) = true;
9957 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
9958 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
9959 TYPE_NEXT_VARIANT (t) = cand;
9960 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
9961 return cand;
9965 /* Given an initializer INIT, return TRUE if INIT is zero or some
9966 aggregate of zeros. Otherwise return FALSE. */
9967 bool
9968 initializer_zerop (const_tree init)
9970 tree elt;
9972 STRIP_NOPS (init);
9974 switch (TREE_CODE (init))
9976 case INTEGER_CST:
9977 return integer_zerop (init);
9979 case REAL_CST:
9980 /* ??? Note that this is not correct for C4X float formats. There,
9981 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
9982 negative exponent. */
9983 return real_zerop (init)
9984 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
9986 case FIXED_CST:
9987 return fixed_zerop (init);
9989 case COMPLEX_CST:
9990 return integer_zerop (init)
9991 || (real_zerop (init)
9992 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
9993 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
9995 case VECTOR_CST:
9997 unsigned i;
9998 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
9999 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10000 return false;
10001 return true;
10004 case CONSTRUCTOR:
10006 unsigned HOST_WIDE_INT idx;
10008 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10009 if (!initializer_zerop (elt))
10010 return false;
10011 return true;
10014 case STRING_CST:
10016 int i;
10018 /* We need to loop through all elements to handle cases like
10019 "\0" and "\0foobar". */
10020 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10021 if (TREE_STRING_POINTER (init)[i] != '\0')
10022 return false;
10024 return true;
10027 default:
10028 return false;
10032 /* Build an empty statement at location LOC. */
10034 tree
10035 build_empty_stmt (location_t loc)
10037 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10038 SET_EXPR_LOCATION (t, loc);
10039 return t;
10043 /* Build an OpenMP clause with code CODE. LOC is the location of the
10044 clause. */
10046 tree
10047 build_omp_clause (location_t loc, enum omp_clause_code code)
10049 tree t;
10050 int size, length;
10052 length = omp_clause_num_ops[code];
10053 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10055 record_node_allocation_statistics (OMP_CLAUSE, size);
10057 t = ggc_alloc_tree_node (size);
10058 memset (t, 0, size);
10059 TREE_SET_CODE (t, OMP_CLAUSE);
10060 OMP_CLAUSE_SET_CODE (t, code);
10061 OMP_CLAUSE_LOCATION (t) = loc;
10063 return t;
10066 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10067 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10068 Except for the CODE and operand count field, other storage for the
10069 object is initialized to zeros. */
10071 tree
10072 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10074 tree t;
10075 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10077 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10078 gcc_assert (len >= 1);
10080 record_node_allocation_statistics (code, length);
10082 t = ggc_alloc_zone_cleared_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
10084 TREE_SET_CODE (t, code);
10086 /* Can't use TREE_OPERAND to store the length because if checking is
10087 enabled, it will try to check the length before we store it. :-P */
10088 t->exp.operands[0] = build_int_cst (sizetype, len);
10090 return t;
10093 /* Helper function for build_call_* functions; build a CALL_EXPR with
10094 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10095 the argument slots. */
10097 static tree
10098 build_call_1 (tree return_type, tree fn, int nargs)
10100 tree t;
10102 t = build_vl_exp (CALL_EXPR, nargs + 3);
10103 TREE_TYPE (t) = return_type;
10104 CALL_EXPR_FN (t) = fn;
10105 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10107 return t;
10110 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10111 FN and a null static chain slot. NARGS is the number of call arguments
10112 which are specified as "..." arguments. */
10114 tree
10115 build_call_nary (tree return_type, tree fn, int nargs, ...)
10117 tree ret;
10118 va_list args;
10119 va_start (args, nargs);
10120 ret = build_call_valist (return_type, fn, nargs, args);
10121 va_end (args);
10122 return ret;
10125 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10126 FN and a null static chain slot. NARGS is the number of call arguments
10127 which are specified as a va_list ARGS. */
10129 tree
10130 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10132 tree t;
10133 int i;
10135 t = build_call_1 (return_type, fn, nargs);
10136 for (i = 0; i < nargs; i++)
10137 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10138 process_call_operands (t);
10139 return t;
10142 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10143 FN and a null static chain slot. NARGS is the number of call arguments
10144 which are specified as a tree array ARGS. */
10146 tree
10147 build_call_array_loc (location_t loc, tree return_type, tree fn,
10148 int nargs, const tree *args)
10150 tree t;
10151 int i;
10153 t = build_call_1 (return_type, fn, nargs);
10154 for (i = 0; i < nargs; i++)
10155 CALL_EXPR_ARG (t, i) = args[i];
10156 process_call_operands (t);
10157 SET_EXPR_LOCATION (t, loc);
10158 return t;
10161 /* Like build_call_array, but takes a vec. */
10163 tree
10164 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10166 tree ret, t;
10167 unsigned int ix;
10169 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10170 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10171 CALL_EXPR_ARG (ret, ix) = t;
10172 process_call_operands (ret);
10173 return ret;
10177 /* Returns true if it is possible to prove that the index of
10178 an array access REF (an ARRAY_REF expression) falls into the
10179 array bounds. */
10181 bool
10182 in_array_bounds_p (tree ref)
10184 tree idx = TREE_OPERAND (ref, 1);
10185 tree min, max;
10187 if (TREE_CODE (idx) != INTEGER_CST)
10188 return false;
10190 min = array_ref_low_bound (ref);
10191 max = array_ref_up_bound (ref);
10192 if (!min
10193 || !max
10194 || TREE_CODE (min) != INTEGER_CST
10195 || TREE_CODE (max) != INTEGER_CST)
10196 return false;
10198 if (tree_int_cst_lt (idx, min)
10199 || tree_int_cst_lt (max, idx))
10200 return false;
10202 return true;
10205 /* Returns true if it is possible to prove that the range of
10206 an array access REF (an ARRAY_RANGE_REF expression) falls
10207 into the array bounds. */
10209 bool
10210 range_in_array_bounds_p (tree ref)
10212 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
10213 tree range_min, range_max, min, max;
10215 range_min = TYPE_MIN_VALUE (domain_type);
10216 range_max = TYPE_MAX_VALUE (domain_type);
10217 if (!range_min
10218 || !range_max
10219 || TREE_CODE (range_min) != INTEGER_CST
10220 || TREE_CODE (range_max) != INTEGER_CST)
10221 return false;
10223 min = array_ref_low_bound (ref);
10224 max = array_ref_up_bound (ref);
10225 if (!min
10226 || !max
10227 || TREE_CODE (min) != INTEGER_CST
10228 || TREE_CODE (max) != INTEGER_CST)
10229 return false;
10231 if (tree_int_cst_lt (range_min, min)
10232 || tree_int_cst_lt (max, range_max))
10233 return false;
10235 return true;
10238 /* Return true if T (assumed to be a DECL) must be assigned a memory
10239 location. */
10241 bool
10242 needs_to_live_in_memory (const_tree t)
10244 return (TREE_ADDRESSABLE (t)
10245 || is_global_var (t)
10246 || (TREE_CODE (t) == RESULT_DECL
10247 && !DECL_BY_REFERENCE (t)
10248 && aggregate_value_p (t, current_function_decl)));
10251 /* Return value of a constant X and sign-extend it. */
10253 HOST_WIDE_INT
10254 int_cst_value (const_tree x)
10256 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10257 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10259 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10260 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10261 || TREE_INT_CST_HIGH (x) == -1);
10263 if (bits < HOST_BITS_PER_WIDE_INT)
10265 bool negative = ((val >> (bits - 1)) & 1) != 0;
10266 if (negative)
10267 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10268 else
10269 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10272 return val;
10275 /* Return value of a constant X and sign-extend it. */
10277 HOST_WIDEST_INT
10278 widest_int_cst_value (const_tree x)
10280 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10281 unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
10283 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
10284 gcc_assert (HOST_BITS_PER_WIDEST_INT >= HOST_BITS_PER_DOUBLE_INT);
10285 val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_HIGH (x))
10286 << HOST_BITS_PER_WIDE_INT);
10287 #else
10288 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10289 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10290 || TREE_INT_CST_HIGH (x) == -1);
10291 #endif
10293 if (bits < HOST_BITS_PER_WIDEST_INT)
10295 bool negative = ((val >> (bits - 1)) & 1) != 0;
10296 if (negative)
10297 val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
10298 else
10299 val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
10302 return val;
10305 /* If TYPE is an integral or pointer type, return an integer type with
10306 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10307 if TYPE is already an integer type of signedness UNSIGNEDP. */
10309 tree
10310 signed_or_unsigned_type_for (int unsignedp, tree type)
10312 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10313 return type;
10315 if (TREE_CODE (type) == VECTOR_TYPE)
10317 tree inner = TREE_TYPE (type);
10318 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10319 if (!inner2)
10320 return NULL_TREE;
10321 if (inner == inner2)
10322 return type;
10323 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10326 if (!INTEGRAL_TYPE_P (type)
10327 && !POINTER_TYPE_P (type))
10328 return NULL_TREE;
10330 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10333 /* If TYPE is an integral or pointer type, return an integer type with
10334 the same precision which is unsigned, or itself if TYPE is already an
10335 unsigned integer type. */
10337 tree
10338 unsigned_type_for (tree type)
10340 return signed_or_unsigned_type_for (1, type);
10343 /* If TYPE is an integral or pointer type, return an integer type with
10344 the same precision which is signed, or itself if TYPE is already a
10345 signed integer type. */
10347 tree
10348 signed_type_for (tree type)
10350 return signed_or_unsigned_type_for (0, type);
10353 /* If TYPE is a vector type, return a signed integer vector type with the
10354 same width and number of subparts. Otherwise return boolean_type_node. */
10356 tree
10357 truth_type_for (tree type)
10359 if (TREE_CODE (type) == VECTOR_TYPE)
10361 tree elem = lang_hooks.types.type_for_size
10362 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10363 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10365 else
10366 return boolean_type_node;
10369 /* Returns the largest value obtainable by casting something in INNER type to
10370 OUTER type. */
10372 tree
10373 upper_bound_in_type (tree outer, tree inner)
10375 double_int high;
10376 unsigned int det = 0;
10377 unsigned oprec = TYPE_PRECISION (outer);
10378 unsigned iprec = TYPE_PRECISION (inner);
10379 unsigned prec;
10381 /* Compute a unique number for every combination. */
10382 det |= (oprec > iprec) ? 4 : 0;
10383 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10384 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10386 /* Determine the exponent to use. */
10387 switch (det)
10389 case 0:
10390 case 1:
10391 /* oprec <= iprec, outer: signed, inner: don't care. */
10392 prec = oprec - 1;
10393 break;
10394 case 2:
10395 case 3:
10396 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10397 prec = oprec;
10398 break;
10399 case 4:
10400 /* oprec > iprec, outer: signed, inner: signed. */
10401 prec = iprec - 1;
10402 break;
10403 case 5:
10404 /* oprec > iprec, outer: signed, inner: unsigned. */
10405 prec = iprec;
10406 break;
10407 case 6:
10408 /* oprec > iprec, outer: unsigned, inner: signed. */
10409 prec = oprec;
10410 break;
10411 case 7:
10412 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10413 prec = iprec;
10414 break;
10415 default:
10416 gcc_unreachable ();
10419 /* Compute 2^^prec - 1. */
10420 if (prec <= HOST_BITS_PER_WIDE_INT)
10422 high.high = 0;
10423 high.low = ((~(unsigned HOST_WIDE_INT) 0)
10424 >> (HOST_BITS_PER_WIDE_INT - prec));
10426 else
10428 high.high = ((~(unsigned HOST_WIDE_INT) 0)
10429 >> (HOST_BITS_PER_DOUBLE_INT - prec));
10430 high.low = ~(unsigned HOST_WIDE_INT) 0;
10433 return double_int_to_tree (outer, high);
10436 /* Returns the smallest value obtainable by casting something in INNER type to
10437 OUTER type. */
10439 tree
10440 lower_bound_in_type (tree outer, tree inner)
10442 double_int low;
10443 unsigned oprec = TYPE_PRECISION (outer);
10444 unsigned iprec = TYPE_PRECISION (inner);
10446 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10447 and obtain 0. */
10448 if (TYPE_UNSIGNED (outer)
10449 /* If we are widening something of an unsigned type, OUTER type
10450 contains all values of INNER type. In particular, both INNER
10451 and OUTER types have zero in common. */
10452 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10453 low.low = low.high = 0;
10454 else
10456 /* If we are widening a signed type to another signed type, we
10457 want to obtain -2^^(iprec-1). If we are keeping the
10458 precision or narrowing to a signed type, we want to obtain
10459 -2^(oprec-1). */
10460 unsigned prec = oprec > iprec ? iprec : oprec;
10462 if (prec <= HOST_BITS_PER_WIDE_INT)
10464 low.high = ~(unsigned HOST_WIDE_INT) 0;
10465 low.low = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
10467 else
10469 low.high = ((~(unsigned HOST_WIDE_INT) 0)
10470 << (prec - HOST_BITS_PER_WIDE_INT - 1));
10471 low.low = 0;
10475 return double_int_to_tree (outer, low);
10478 /* Return nonzero if two operands that are suitable for PHI nodes are
10479 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10480 SSA_NAME or invariant. Note that this is strictly an optimization.
10481 That is, callers of this function can directly call operand_equal_p
10482 and get the same result, only slower. */
10485 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10487 if (arg0 == arg1)
10488 return 1;
10489 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10490 return 0;
10491 return operand_equal_p (arg0, arg1, 0);
10494 /* Returns number of zeros at the end of binary representation of X.
10496 ??? Use ffs if available? */
10498 tree
10499 num_ending_zeros (const_tree x)
10501 unsigned HOST_WIDE_INT fr, nfr;
10502 unsigned num, abits;
10503 tree type = TREE_TYPE (x);
10505 if (TREE_INT_CST_LOW (x) == 0)
10507 num = HOST_BITS_PER_WIDE_INT;
10508 fr = TREE_INT_CST_HIGH (x);
10510 else
10512 num = 0;
10513 fr = TREE_INT_CST_LOW (x);
10516 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
10518 nfr = fr >> abits;
10519 if (nfr << abits == fr)
10521 num += abits;
10522 fr = nfr;
10526 if (num > TYPE_PRECISION (type))
10527 num = TYPE_PRECISION (type);
10529 return build_int_cst_type (type, num);
10533 #define WALK_SUBTREE(NODE) \
10534 do \
10536 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10537 if (result) \
10538 return result; \
10540 while (0)
10542 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10543 be walked whenever a type is seen in the tree. Rest of operands and return
10544 value are as for walk_tree. */
10546 static tree
10547 walk_type_fields (tree type, walk_tree_fn func, void *data,
10548 struct pointer_set_t *pset, walk_tree_lh lh)
10550 tree result = NULL_TREE;
10552 switch (TREE_CODE (type))
10554 case POINTER_TYPE:
10555 case REFERENCE_TYPE:
10556 /* We have to worry about mutually recursive pointers. These can't
10557 be written in C. They can in Ada. It's pathological, but
10558 there's an ACATS test (c38102a) that checks it. Deal with this
10559 by checking if we're pointing to another pointer, that one
10560 points to another pointer, that one does too, and we have no htab.
10561 If so, get a hash table. We check three levels deep to avoid
10562 the cost of the hash table if we don't need one. */
10563 if (POINTER_TYPE_P (TREE_TYPE (type))
10564 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10565 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10566 && !pset)
10568 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10569 func, data);
10570 if (result)
10571 return result;
10573 break;
10576 /* ... fall through ... */
10578 case COMPLEX_TYPE:
10579 WALK_SUBTREE (TREE_TYPE (type));
10580 break;
10582 case METHOD_TYPE:
10583 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10585 /* Fall through. */
10587 case FUNCTION_TYPE:
10588 WALK_SUBTREE (TREE_TYPE (type));
10590 tree arg;
10592 /* We never want to walk into default arguments. */
10593 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10594 WALK_SUBTREE (TREE_VALUE (arg));
10596 break;
10598 case ARRAY_TYPE:
10599 /* Don't follow this nodes's type if a pointer for fear that
10600 we'll have infinite recursion. If we have a PSET, then we
10601 need not fear. */
10602 if (pset
10603 || (!POINTER_TYPE_P (TREE_TYPE (type))
10604 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10605 WALK_SUBTREE (TREE_TYPE (type));
10606 WALK_SUBTREE (TYPE_DOMAIN (type));
10607 break;
10609 case OFFSET_TYPE:
10610 WALK_SUBTREE (TREE_TYPE (type));
10611 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
10612 break;
10614 default:
10615 break;
10618 return NULL_TREE;
10621 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
10622 called with the DATA and the address of each sub-tree. If FUNC returns a
10623 non-NULL value, the traversal is stopped, and the value returned by FUNC
10624 is returned. If PSET is non-NULL it is used to record the nodes visited,
10625 and to avoid visiting a node more than once. */
10627 tree
10628 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
10629 struct pointer_set_t *pset, walk_tree_lh lh)
10631 enum tree_code code;
10632 int walk_subtrees;
10633 tree result;
10635 #define WALK_SUBTREE_TAIL(NODE) \
10636 do \
10638 tp = & (NODE); \
10639 goto tail_recurse; \
10641 while (0)
10643 tail_recurse:
10644 /* Skip empty subtrees. */
10645 if (!*tp)
10646 return NULL_TREE;
10648 /* Don't walk the same tree twice, if the user has requested
10649 that we avoid doing so. */
10650 if (pset && pointer_set_insert (pset, *tp))
10651 return NULL_TREE;
10653 /* Call the function. */
10654 walk_subtrees = 1;
10655 result = (*func) (tp, &walk_subtrees, data);
10657 /* If we found something, return it. */
10658 if (result)
10659 return result;
10661 code = TREE_CODE (*tp);
10663 /* Even if we didn't, FUNC may have decided that there was nothing
10664 interesting below this point in the tree. */
10665 if (!walk_subtrees)
10667 /* But we still need to check our siblings. */
10668 if (code == TREE_LIST)
10669 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10670 else if (code == OMP_CLAUSE)
10671 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10672 else
10673 return NULL_TREE;
10676 if (lh)
10678 result = (*lh) (tp, &walk_subtrees, func, data, pset);
10679 if (result || !walk_subtrees)
10680 return result;
10683 switch (code)
10685 case ERROR_MARK:
10686 case IDENTIFIER_NODE:
10687 case INTEGER_CST:
10688 case REAL_CST:
10689 case FIXED_CST:
10690 case VECTOR_CST:
10691 case STRING_CST:
10692 case BLOCK:
10693 case PLACEHOLDER_EXPR:
10694 case SSA_NAME:
10695 case FIELD_DECL:
10696 case RESULT_DECL:
10697 /* None of these have subtrees other than those already walked
10698 above. */
10699 break;
10701 case TREE_LIST:
10702 WALK_SUBTREE (TREE_VALUE (*tp));
10703 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10704 break;
10706 case TREE_VEC:
10708 int len = TREE_VEC_LENGTH (*tp);
10710 if (len == 0)
10711 break;
10713 /* Walk all elements but the first. */
10714 while (--len)
10715 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
10717 /* Now walk the first one as a tail call. */
10718 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
10721 case COMPLEX_CST:
10722 WALK_SUBTREE (TREE_REALPART (*tp));
10723 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
10725 case CONSTRUCTOR:
10727 unsigned HOST_WIDE_INT idx;
10728 constructor_elt *ce;
10730 for (idx = 0; vec_safe_iterate(CONSTRUCTOR_ELTS (*tp), idx, &ce); idx++)
10731 WALK_SUBTREE (ce->value);
10733 break;
10735 case SAVE_EXPR:
10736 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
10738 case BIND_EXPR:
10740 tree decl;
10741 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
10743 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
10744 into declarations that are just mentioned, rather than
10745 declared; they don't really belong to this part of the tree.
10746 And, we can see cycles: the initializer for a declaration
10747 can refer to the declaration itself. */
10748 WALK_SUBTREE (DECL_INITIAL (decl));
10749 WALK_SUBTREE (DECL_SIZE (decl));
10750 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
10752 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
10755 case STATEMENT_LIST:
10757 tree_stmt_iterator i;
10758 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
10759 WALK_SUBTREE (*tsi_stmt_ptr (i));
10761 break;
10763 case OMP_CLAUSE:
10764 switch (OMP_CLAUSE_CODE (*tp))
10766 case OMP_CLAUSE_PRIVATE:
10767 case OMP_CLAUSE_SHARED:
10768 case OMP_CLAUSE_FIRSTPRIVATE:
10769 case OMP_CLAUSE_COPYIN:
10770 case OMP_CLAUSE_COPYPRIVATE:
10771 case OMP_CLAUSE_FINAL:
10772 case OMP_CLAUSE_IF:
10773 case OMP_CLAUSE_NUM_THREADS:
10774 case OMP_CLAUSE_SCHEDULE:
10775 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
10776 /* FALLTHRU */
10778 case OMP_CLAUSE_NOWAIT:
10779 case OMP_CLAUSE_ORDERED:
10780 case OMP_CLAUSE_DEFAULT:
10781 case OMP_CLAUSE_UNTIED:
10782 case OMP_CLAUSE_MERGEABLE:
10783 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10785 case OMP_CLAUSE_LASTPRIVATE:
10786 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
10787 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
10788 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10790 case OMP_CLAUSE_COLLAPSE:
10792 int i;
10793 for (i = 0; i < 3; i++)
10794 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10795 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10798 case OMP_CLAUSE_REDUCTION:
10800 int i;
10801 for (i = 0; i < 4; i++)
10802 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10803 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10806 default:
10807 gcc_unreachable ();
10809 break;
10811 case TARGET_EXPR:
10813 int i, len;
10815 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
10816 But, we only want to walk once. */
10817 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
10818 for (i = 0; i < len; ++i)
10819 WALK_SUBTREE (TREE_OPERAND (*tp, i));
10820 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
10823 case DECL_EXPR:
10824 /* If this is a TYPE_DECL, walk into the fields of the type that it's
10825 defining. We only want to walk into these fields of a type in this
10826 case and not in the general case of a mere reference to the type.
10828 The criterion is as follows: if the field can be an expression, it
10829 must be walked only here. This should be in keeping with the fields
10830 that are directly gimplified in gimplify_type_sizes in order for the
10831 mark/copy-if-shared/unmark machinery of the gimplifier to work with
10832 variable-sized types.
10834 Note that DECLs get walked as part of processing the BIND_EXPR. */
10835 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
10837 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
10838 if (TREE_CODE (*type_p) == ERROR_MARK)
10839 return NULL_TREE;
10841 /* Call the function for the type. See if it returns anything or
10842 doesn't want us to continue. If we are to continue, walk both
10843 the normal fields and those for the declaration case. */
10844 result = (*func) (type_p, &walk_subtrees, data);
10845 if (result || !walk_subtrees)
10846 return result;
10848 /* But do not walk a pointed-to type since it may itself need to
10849 be walked in the declaration case if it isn't anonymous. */
10850 if (!POINTER_TYPE_P (*type_p))
10852 result = walk_type_fields (*type_p, func, data, pset, lh);
10853 if (result)
10854 return result;
10857 /* If this is a record type, also walk the fields. */
10858 if (RECORD_OR_UNION_TYPE_P (*type_p))
10860 tree field;
10862 for (field = TYPE_FIELDS (*type_p); field;
10863 field = DECL_CHAIN (field))
10865 /* We'd like to look at the type of the field, but we can
10866 easily get infinite recursion. So assume it's pointed
10867 to elsewhere in the tree. Also, ignore things that
10868 aren't fields. */
10869 if (TREE_CODE (field) != FIELD_DECL)
10870 continue;
10872 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
10873 WALK_SUBTREE (DECL_SIZE (field));
10874 WALK_SUBTREE (DECL_SIZE_UNIT (field));
10875 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
10876 WALK_SUBTREE (DECL_QUALIFIER (field));
10880 /* Same for scalar types. */
10881 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
10882 || TREE_CODE (*type_p) == ENUMERAL_TYPE
10883 || TREE_CODE (*type_p) == INTEGER_TYPE
10884 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
10885 || TREE_CODE (*type_p) == REAL_TYPE)
10887 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
10888 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
10891 WALK_SUBTREE (TYPE_SIZE (*type_p));
10892 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
10894 /* FALLTHRU */
10896 default:
10897 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
10899 int i, len;
10901 /* Walk over all the sub-trees of this operand. */
10902 len = TREE_OPERAND_LENGTH (*tp);
10904 /* Go through the subtrees. We need to do this in forward order so
10905 that the scope of a FOR_EXPR is handled properly. */
10906 if (len)
10908 for (i = 0; i < len - 1; ++i)
10909 WALK_SUBTREE (TREE_OPERAND (*tp, i));
10910 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
10913 /* If this is a type, walk the needed fields in the type. */
10914 else if (TYPE_P (*tp))
10915 return walk_type_fields (*tp, func, data, pset, lh);
10916 break;
10919 /* We didn't find what we were looking for. */
10920 return NULL_TREE;
10922 #undef WALK_SUBTREE_TAIL
10924 #undef WALK_SUBTREE
10926 /* Like walk_tree, but does not walk duplicate nodes more than once. */
10928 tree
10929 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
10930 walk_tree_lh lh)
10932 tree result;
10933 struct pointer_set_t *pset;
10935 pset = pointer_set_create ();
10936 result = walk_tree_1 (tp, func, data, pset, lh);
10937 pointer_set_destroy (pset);
10938 return result;
10942 tree
10943 tree_block (tree t)
10945 char const c = TREE_CODE_CLASS (TREE_CODE (t));
10947 if (IS_EXPR_CODE_CLASS (c))
10948 return LOCATION_BLOCK (t->exp.locus);
10949 gcc_unreachable ();
10950 return NULL;
10953 void
10954 tree_set_block (tree t, tree b)
10956 char const c = TREE_CODE_CLASS (TREE_CODE (t));
10958 if (IS_EXPR_CODE_CLASS (c))
10960 if (b)
10961 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
10962 else
10963 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
10965 else
10966 gcc_unreachable ();
10969 /* Create a nameless artificial label and put it in the current
10970 function context. The label has a location of LOC. Returns the
10971 newly created label. */
10973 tree
10974 create_artificial_label (location_t loc)
10976 tree lab = build_decl (loc,
10977 LABEL_DECL, NULL_TREE, void_type_node);
10979 DECL_ARTIFICIAL (lab) = 1;
10980 DECL_IGNORED_P (lab) = 1;
10981 DECL_CONTEXT (lab) = current_function_decl;
10982 return lab;
10985 /* Given a tree, try to return a useful variable name that we can use
10986 to prefix a temporary that is being assigned the value of the tree.
10987 I.E. given <temp> = &A, return A. */
10989 const char *
10990 get_name (tree t)
10992 tree stripped_decl;
10994 stripped_decl = t;
10995 STRIP_NOPS (stripped_decl);
10996 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
10997 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
10998 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11000 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11001 if (!name)
11002 return NULL;
11003 return IDENTIFIER_POINTER (name);
11005 else
11007 switch (TREE_CODE (stripped_decl))
11009 case ADDR_EXPR:
11010 return get_name (TREE_OPERAND (stripped_decl, 0));
11011 default:
11012 return NULL;
11017 /* Return true if TYPE has a variable argument list. */
11019 bool
11020 stdarg_p (const_tree fntype)
11022 function_args_iterator args_iter;
11023 tree n = NULL_TREE, t;
11025 if (!fntype)
11026 return false;
11028 FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
11030 n = t;
11033 return n != NULL_TREE && n != void_type_node;
11036 /* Return true if TYPE has a prototype. */
11038 bool
11039 prototype_p (tree fntype)
11041 tree t;
11043 gcc_assert (fntype != NULL_TREE);
11045 t = TYPE_ARG_TYPES (fntype);
11046 return (t != NULL_TREE);
11049 /* If BLOCK is inlined from an __attribute__((__artificial__))
11050 routine, return pointer to location from where it has been
11051 called. */
11052 location_t *
11053 block_nonartificial_location (tree block)
11055 location_t *ret = NULL;
11057 while (block && TREE_CODE (block) == BLOCK
11058 && BLOCK_ABSTRACT_ORIGIN (block))
11060 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11062 while (TREE_CODE (ao) == BLOCK
11063 && BLOCK_ABSTRACT_ORIGIN (ao)
11064 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11065 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11067 if (TREE_CODE (ao) == FUNCTION_DECL)
11069 /* If AO is an artificial inline, point RET to the
11070 call site locus at which it has been inlined and continue
11071 the loop, in case AO's caller is also an artificial
11072 inline. */
11073 if (DECL_DECLARED_INLINE_P (ao)
11074 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11075 ret = &BLOCK_SOURCE_LOCATION (block);
11076 else
11077 break;
11079 else if (TREE_CODE (ao) != BLOCK)
11080 break;
11082 block = BLOCK_SUPERCONTEXT (block);
11084 return ret;
11088 /* If EXP is inlined from an __attribute__((__artificial__))
11089 function, return the location of the original call expression. */
11091 location_t
11092 tree_nonartificial_location (tree exp)
11094 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11096 if (loc)
11097 return *loc;
11098 else
11099 return EXPR_LOCATION (exp);
11103 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11104 nodes. */
11106 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11108 static hashval_t
11109 cl_option_hash_hash (const void *x)
11111 const_tree const t = (const_tree) x;
11112 const char *p;
11113 size_t i;
11114 size_t len = 0;
11115 hashval_t hash = 0;
11117 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11119 p = (const char *)TREE_OPTIMIZATION (t);
11120 len = sizeof (struct cl_optimization);
11123 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11125 p = (const char *)TREE_TARGET_OPTION (t);
11126 len = sizeof (struct cl_target_option);
11129 else
11130 gcc_unreachable ();
11132 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11133 something else. */
11134 for (i = 0; i < len; i++)
11135 if (p[i])
11136 hash = (hash << 4) ^ ((i << 2) | p[i]);
11138 return hash;
11141 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11142 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11143 same. */
11145 static int
11146 cl_option_hash_eq (const void *x, const void *y)
11148 const_tree const xt = (const_tree) x;
11149 const_tree const yt = (const_tree) y;
11150 const char *xp;
11151 const char *yp;
11152 size_t len;
11154 if (TREE_CODE (xt) != TREE_CODE (yt))
11155 return 0;
11157 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11159 xp = (const char *)TREE_OPTIMIZATION (xt);
11160 yp = (const char *)TREE_OPTIMIZATION (yt);
11161 len = sizeof (struct cl_optimization);
11164 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11166 xp = (const char *)TREE_TARGET_OPTION (xt);
11167 yp = (const char *)TREE_TARGET_OPTION (yt);
11168 len = sizeof (struct cl_target_option);
11171 else
11172 gcc_unreachable ();
11174 return (memcmp (xp, yp, len) == 0);
11177 /* Build an OPTIMIZATION_NODE based on the current options. */
11179 tree
11180 build_optimization_node (void)
11182 tree t;
11183 void **slot;
11185 /* Use the cache of optimization nodes. */
11187 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11188 &global_options);
11190 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
11191 t = (tree) *slot;
11192 if (!t)
11194 /* Insert this one into the hash table. */
11195 t = cl_optimization_node;
11196 *slot = t;
11198 /* Make a new node for next time round. */
11199 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11202 return t;
11205 /* Build a TARGET_OPTION_NODE based on the current options. */
11207 tree
11208 build_target_option_node (void)
11210 tree t;
11211 void **slot;
11213 /* Use the cache of optimization nodes. */
11215 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11216 &global_options);
11218 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
11219 t = (tree) *slot;
11220 if (!t)
11222 /* Insert this one into the hash table. */
11223 t = cl_target_option_node;
11224 *slot = t;
11226 /* Make a new node for next time round. */
11227 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11230 return t;
11233 /* Determine the "ultimate origin" of a block. The block may be an inlined
11234 instance of an inlined instance of a block which is local to an inline
11235 function, so we have to trace all of the way back through the origin chain
11236 to find out what sort of node actually served as the original seed for the
11237 given block. */
11239 tree
11240 block_ultimate_origin (const_tree block)
11242 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11244 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
11245 nodes in the function to point to themselves; ignore that if
11246 we're trying to output the abstract instance of this function. */
11247 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11248 return NULL_TREE;
11250 if (immediate_origin == NULL_TREE)
11251 return NULL_TREE;
11252 else
11254 tree ret_val;
11255 tree lookahead = immediate_origin;
11259 ret_val = lookahead;
11260 lookahead = (TREE_CODE (ret_val) == BLOCK
11261 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11263 while (lookahead != NULL && lookahead != ret_val);
11265 /* The block's abstract origin chain may not be the *ultimate* origin of
11266 the block. It could lead to a DECL that has an abstract origin set.
11267 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11268 will give us if it has one). Note that DECL's abstract origins are
11269 supposed to be the most distant ancestor (or so decl_ultimate_origin
11270 claims), so we don't need to loop following the DECL origins. */
11271 if (DECL_P (ret_val))
11272 return DECL_ORIGIN (ret_val);
11274 return ret_val;
11278 /* Return true if T1 and T2 are equivalent lists. */
11280 bool
11281 list_equal_p (const_tree t1, const_tree t2)
11283 for (; t1 && t2; t1 = TREE_CHAIN (t1) , t2 = TREE_CHAIN (t2))
11284 if (TREE_VALUE (t1) != TREE_VALUE (t2))
11285 return false;
11286 return !t1 && !t2;
11289 /* Return true iff conversion in EXP generates no instruction. Mark
11290 it inline so that we fully inline into the stripping functions even
11291 though we have two uses of this function. */
11293 static inline bool
11294 tree_nop_conversion (const_tree exp)
11296 tree outer_type, inner_type;
11298 if (!CONVERT_EXPR_P (exp)
11299 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11300 return false;
11301 if (TREE_OPERAND (exp, 0) == error_mark_node)
11302 return false;
11304 outer_type = TREE_TYPE (exp);
11305 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11307 if (!inner_type)
11308 return false;
11310 /* Use precision rather then machine mode when we can, which gives
11311 the correct answer even for submode (bit-field) types. */
11312 if ((INTEGRAL_TYPE_P (outer_type)
11313 || POINTER_TYPE_P (outer_type)
11314 || TREE_CODE (outer_type) == OFFSET_TYPE)
11315 && (INTEGRAL_TYPE_P (inner_type)
11316 || POINTER_TYPE_P (inner_type)
11317 || TREE_CODE (inner_type) == OFFSET_TYPE))
11318 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11320 /* Otherwise fall back on comparing machine modes (e.g. for
11321 aggregate types, floats). */
11322 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11325 /* Return true iff conversion in EXP generates no instruction. Don't
11326 consider conversions changing the signedness. */
11328 static bool
11329 tree_sign_nop_conversion (const_tree exp)
11331 tree outer_type, inner_type;
11333 if (!tree_nop_conversion (exp))
11334 return false;
11336 outer_type = TREE_TYPE (exp);
11337 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11339 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11340 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11343 /* Strip conversions from EXP according to tree_nop_conversion and
11344 return the resulting expression. */
11346 tree
11347 tree_strip_nop_conversions (tree exp)
11349 while (tree_nop_conversion (exp))
11350 exp = TREE_OPERAND (exp, 0);
11351 return exp;
11354 /* Strip conversions from EXP according to tree_sign_nop_conversion
11355 and return the resulting expression. */
11357 tree
11358 tree_strip_sign_nop_conversions (tree exp)
11360 while (tree_sign_nop_conversion (exp))
11361 exp = TREE_OPERAND (exp, 0);
11362 return exp;
11365 /* Avoid any floating point extensions from EXP. */
11366 tree
11367 strip_float_extensions (tree exp)
11369 tree sub, expt, subt;
11371 /* For floating point constant look up the narrowest type that can hold
11372 it properly and handle it like (type)(narrowest_type)constant.
11373 This way we can optimize for instance a=a*2.0 where "a" is float
11374 but 2.0 is double constant. */
11375 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11377 REAL_VALUE_TYPE orig;
11378 tree type = NULL;
11380 orig = TREE_REAL_CST (exp);
11381 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11382 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11383 type = float_type_node;
11384 else if (TYPE_PRECISION (TREE_TYPE (exp))
11385 > TYPE_PRECISION (double_type_node)
11386 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11387 type = double_type_node;
11388 if (type)
11389 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11392 if (!CONVERT_EXPR_P (exp))
11393 return exp;
11395 sub = TREE_OPERAND (exp, 0);
11396 subt = TREE_TYPE (sub);
11397 expt = TREE_TYPE (exp);
11399 if (!FLOAT_TYPE_P (subt))
11400 return exp;
11402 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11403 return exp;
11405 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11406 return exp;
11408 return strip_float_extensions (sub);
11411 /* Strip out all handled components that produce invariant
11412 offsets. */
11414 const_tree
11415 strip_invariant_refs (const_tree op)
11417 while (handled_component_p (op))
11419 switch (TREE_CODE (op))
11421 case ARRAY_REF:
11422 case ARRAY_RANGE_REF:
11423 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11424 || TREE_OPERAND (op, 2) != NULL_TREE
11425 || TREE_OPERAND (op, 3) != NULL_TREE)
11426 return NULL;
11427 break;
11429 case COMPONENT_REF:
11430 if (TREE_OPERAND (op, 2) != NULL_TREE)
11431 return NULL;
11432 break;
11434 default:;
11436 op = TREE_OPERAND (op, 0);
11439 return op;
11442 static GTY(()) tree gcc_eh_personality_decl;
11444 /* Return the GCC personality function decl. */
11446 tree
11447 lhd_gcc_personality (void)
11449 if (!gcc_eh_personality_decl)
11450 gcc_eh_personality_decl = build_personality_function ("gcc");
11451 return gcc_eh_personality_decl;
11454 /* Try to find a base info of BINFO that would have its field decl at offset
11455 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11456 found, return, otherwise return NULL_TREE. */
11458 tree
11459 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
11461 tree type = BINFO_TYPE (binfo);
11463 while (true)
11465 HOST_WIDE_INT pos, size;
11466 tree fld;
11467 int i;
11469 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (expected_type))
11470 return binfo;
11471 if (offset < 0)
11472 return NULL_TREE;
11474 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
11476 if (TREE_CODE (fld) != FIELD_DECL)
11477 continue;
11479 pos = int_bit_position (fld);
11480 size = tree_low_cst (DECL_SIZE (fld), 1);
11481 if (pos <= offset && (pos + size) > offset)
11482 break;
11484 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11485 return NULL_TREE;
11487 if (!DECL_ARTIFICIAL (fld))
11489 binfo = TYPE_BINFO (TREE_TYPE (fld));
11490 if (!binfo)
11491 return NULL_TREE;
11493 /* Offset 0 indicates the primary base, whose vtable contents are
11494 represented in the binfo for the derived class. */
11495 else if (offset != 0)
11497 tree base_binfo, found_binfo = NULL_TREE;
11498 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11499 if (TREE_TYPE (base_binfo) == TREE_TYPE (fld))
11501 found_binfo = base_binfo;
11502 break;
11504 if (!found_binfo)
11505 return NULL_TREE;
11506 binfo = found_binfo;
11509 type = TREE_TYPE (fld);
11510 offset -= pos;
11514 /* Returns true if X is a typedef decl. */
11516 bool
11517 is_typedef_decl (tree x)
11519 return (x && TREE_CODE (x) == TYPE_DECL
11520 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
11523 /* Returns true iff TYPE is a type variant created for a typedef. */
11525 bool
11526 typedef_variant_p (tree type)
11528 return is_typedef_decl (TYPE_NAME (type));
11531 /* Warn about a use of an identifier which was marked deprecated. */
11532 void
11533 warn_deprecated_use (tree node, tree attr)
11535 const char *msg;
11537 if (node == 0 || !warn_deprecated_decl)
11538 return;
11540 if (!attr)
11542 if (DECL_P (node))
11543 attr = DECL_ATTRIBUTES (node);
11544 else if (TYPE_P (node))
11546 tree decl = TYPE_STUB_DECL (node);
11547 if (decl)
11548 attr = lookup_attribute ("deprecated",
11549 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
11553 if (attr)
11554 attr = lookup_attribute ("deprecated", attr);
11556 if (attr)
11557 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
11558 else
11559 msg = NULL;
11561 if (DECL_P (node))
11563 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
11564 if (msg)
11565 warning (OPT_Wdeprecated_declarations,
11566 "%qD is deprecated (declared at %s:%d): %s",
11567 node, xloc.file, xloc.line, msg);
11568 else
11569 warning (OPT_Wdeprecated_declarations,
11570 "%qD is deprecated (declared at %s:%d)",
11571 node, xloc.file, xloc.line);
11573 else if (TYPE_P (node))
11575 tree what = NULL_TREE;
11576 tree decl = TYPE_STUB_DECL (node);
11578 if (TYPE_NAME (node))
11580 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
11581 what = TYPE_NAME (node);
11582 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
11583 && DECL_NAME (TYPE_NAME (node)))
11584 what = DECL_NAME (TYPE_NAME (node));
11587 if (decl)
11589 expanded_location xloc
11590 = expand_location (DECL_SOURCE_LOCATION (decl));
11591 if (what)
11593 if (msg)
11594 warning (OPT_Wdeprecated_declarations,
11595 "%qE is deprecated (declared at %s:%d): %s",
11596 what, xloc.file, xloc.line, msg);
11597 else
11598 warning (OPT_Wdeprecated_declarations,
11599 "%qE is deprecated (declared at %s:%d)", what,
11600 xloc.file, xloc.line);
11602 else
11604 if (msg)
11605 warning (OPT_Wdeprecated_declarations,
11606 "type is deprecated (declared at %s:%d): %s",
11607 xloc.file, xloc.line, msg);
11608 else
11609 warning (OPT_Wdeprecated_declarations,
11610 "type is deprecated (declared at %s:%d)",
11611 xloc.file, xloc.line);
11614 else
11616 if (what)
11618 if (msg)
11619 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
11620 what, msg);
11621 else
11622 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
11624 else
11626 if (msg)
11627 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
11628 msg);
11629 else
11630 warning (OPT_Wdeprecated_declarations, "type is deprecated");
11636 #include "gt-tree.h"