* c-family/array-notation-common.c (length_mismatch_in_expr_p): Delete
[official-gcc.git] / gcc / tree.c
blobab1173525c71396621f4888cf3a434d61660e62a
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file contains the low level primitives for operating on tree nodes,
21 including allocation, list operations, interning of identifiers,
22 construction of data type nodes and statement nodes,
23 and construction of type conversion nodes. It also contains
24 tables index by tree code that describe how to take apart
25 nodes of that code.
27 It is intended to be language-independent, but occasionally
28 calls language-dependent routines defined (for C) in typecheck.c. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "flags.h"
35 #include "tree.h"
36 #include "tm_p.h"
37 #include "function.h"
38 #include "obstack.h"
39 #include "toplev.h" /* get_random_seed */
40 #include "ggc.h"
41 #include "hashtab.h"
42 #include "filenames.h"
43 #include "output.h"
44 #include "target.h"
45 #include "common/common-target.h"
46 #include "langhooks.h"
47 #include "tree-inline.h"
48 #include "tree-iterator.h"
49 #include "basic-block.h"
50 #include "tree-flow.h"
51 #include "params.h"
52 #include "pointer-set.h"
53 #include "tree-pass.h"
54 #include "langhooks-def.h"
55 #include "diagnostic.h"
56 #include "tree-diagnostic.h"
57 #include "tree-pretty-print.h"
58 #include "cgraph.h"
59 #include "except.h"
60 #include "debug.h"
61 #include "intl.h"
63 /* Tree code classes. */
65 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
66 #define END_OF_BASE_TREE_CODES tcc_exceptional,
68 const enum tree_code_class tree_code_type[] = {
69 #include "all-tree.def"
72 #undef DEFTREECODE
73 #undef END_OF_BASE_TREE_CODES
75 /* Table indexed by tree code giving number of expression
76 operands beyond the fixed part of the node structure.
77 Not used for types or decls. */
79 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
80 #define END_OF_BASE_TREE_CODES 0,
82 const unsigned char tree_code_length[] = {
83 #include "all-tree.def"
86 #undef DEFTREECODE
87 #undef END_OF_BASE_TREE_CODES
89 /* Names of tree components.
90 Used for printing out the tree and error messages. */
91 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
92 #define END_OF_BASE_TREE_CODES "@dummy",
94 const char *const tree_code_name[] = {
95 #include "all-tree.def"
98 #undef DEFTREECODE
99 #undef END_OF_BASE_TREE_CODES
101 /* Each tree code class has an associated string representation.
102 These must correspond to the tree_code_class entries. */
104 const char *const tree_code_class_strings[] =
106 "exceptional",
107 "constant",
108 "type",
109 "declaration",
110 "reference",
111 "comparison",
112 "unary",
113 "binary",
114 "statement",
115 "vl_exp",
116 "expression"
119 /* obstack.[ch] explicitly declined to prototype this. */
120 extern int _obstack_allocated_p (struct obstack *h, void *obj);
122 /* Statistics-gathering stuff. */
124 static int tree_code_counts[MAX_TREE_CODES];
125 int tree_node_counts[(int) all_kinds];
126 int tree_node_sizes[(int) all_kinds];
128 /* Keep in sync with tree.h:enum tree_node_kind. */
129 static const char * const tree_node_kind_names[] = {
130 "decls",
131 "types",
132 "blocks",
133 "stmts",
134 "refs",
135 "exprs",
136 "constants",
137 "identifiers",
138 "vecs",
139 "binfos",
140 "ssa names",
141 "constructors",
142 "random kinds",
143 "lang_decl kinds",
144 "lang_type kinds",
145 "omp clauses",
148 /* Unique id for next decl created. */
149 static GTY(()) int next_decl_uid;
150 /* Unique id for next type created. */
151 static GTY(()) int next_type_uid = 1;
152 /* Unique id for next debug decl created. Use negative numbers,
153 to catch erroneous uses. */
154 static GTY(()) int next_debug_decl_uid;
156 /* Since we cannot rehash a type after it is in the table, we have to
157 keep the hash code. */
159 struct GTY(()) type_hash {
160 unsigned long hash;
161 tree type;
164 /* Initial size of the hash table (rounded to next prime). */
165 #define TYPE_HASH_INITIAL_SIZE 1000
167 /* Now here is the hash table. When recording a type, it is added to
168 the slot whose index is the hash code. Note that the hash table is
169 used for several kinds of types (function types, array types and
170 array index range types, for now). While all these live in the
171 same table, they are completely independent, and the hash code is
172 computed differently for each of these. */
174 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
175 htab_t type_hash_table;
177 /* Hash table and temporary node for larger integer const values. */
178 static GTY (()) tree int_cst_node;
179 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
180 htab_t int_cst_hash_table;
182 /* Hash table for optimization flags and target option flags. Use the same
183 hash table for both sets of options. Nodes for building the current
184 optimization and target option nodes. The assumption is most of the time
185 the options created will already be in the hash table, so we avoid
186 allocating and freeing up a node repeatably. */
187 static GTY (()) tree cl_optimization_node;
188 static GTY (()) tree cl_target_option_node;
189 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
190 htab_t cl_option_hash_table;
192 /* General tree->tree mapping structure for use in hash tables. */
195 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
196 htab_t debug_expr_for_decl;
198 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
199 htab_t value_expr_for_decl;
201 static GTY ((if_marked ("tree_vec_map_marked_p"), param_is (struct tree_vec_map)))
202 htab_t debug_args_for_decl;
204 static GTY ((if_marked ("tree_priority_map_marked_p"),
205 param_is (struct tree_priority_map)))
206 htab_t init_priority_for_decl;
208 static void set_type_quals (tree, int);
209 static int type_hash_eq (const void *, const void *);
210 static hashval_t type_hash_hash (const void *);
211 static hashval_t int_cst_hash_hash (const void *);
212 static int int_cst_hash_eq (const void *, const void *);
213 static hashval_t cl_option_hash_hash (const void *);
214 static int cl_option_hash_eq (const void *, const void *);
215 static void print_type_hash_statistics (void);
216 static void print_debug_expr_statistics (void);
217 static void print_value_expr_statistics (void);
218 static int type_hash_marked_p (const void *);
219 static unsigned int type_hash_list (const_tree, hashval_t);
220 static unsigned int attribute_hash_list (const_tree, hashval_t);
221 static bool decls_same_for_odr (tree decl1, tree decl2);
223 tree global_trees[TI_MAX];
224 tree integer_types[itk_none];
226 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
228 /* Number of operands for each OpenMP clause. */
229 unsigned const char omp_clause_num_ops[] =
231 0, /* OMP_CLAUSE_ERROR */
232 1, /* OMP_CLAUSE_PRIVATE */
233 1, /* OMP_CLAUSE_SHARED */
234 1, /* OMP_CLAUSE_FIRSTPRIVATE */
235 2, /* OMP_CLAUSE_LASTPRIVATE */
236 4, /* OMP_CLAUSE_REDUCTION */
237 1, /* OMP_CLAUSE_COPYIN */
238 1, /* OMP_CLAUSE_COPYPRIVATE */
239 1, /* OMP_CLAUSE_IF */
240 1, /* OMP_CLAUSE_NUM_THREADS */
241 1, /* OMP_CLAUSE_SCHEDULE */
242 0, /* OMP_CLAUSE_NOWAIT */
243 0, /* OMP_CLAUSE_ORDERED */
244 0, /* OMP_CLAUSE_DEFAULT */
245 3, /* OMP_CLAUSE_COLLAPSE */
246 0, /* OMP_CLAUSE_UNTIED */
247 1, /* OMP_CLAUSE_FINAL */
248 0 /* OMP_CLAUSE_MERGEABLE */
251 const char * const omp_clause_code_name[] =
253 "error_clause",
254 "private",
255 "shared",
256 "firstprivate",
257 "lastprivate",
258 "reduction",
259 "copyin",
260 "copyprivate",
261 "if",
262 "num_threads",
263 "schedule",
264 "nowait",
265 "ordered",
266 "default",
267 "collapse",
268 "untied",
269 "final",
270 "mergeable"
274 /* Return the tree node structure used by tree code CODE. */
276 static inline enum tree_node_structure_enum
277 tree_node_structure_for_code (enum tree_code code)
279 switch (TREE_CODE_CLASS (code))
281 case tcc_declaration:
283 switch (code)
285 case FIELD_DECL:
286 return TS_FIELD_DECL;
287 case PARM_DECL:
288 return TS_PARM_DECL;
289 case VAR_DECL:
290 return TS_VAR_DECL;
291 case LABEL_DECL:
292 return TS_LABEL_DECL;
293 case RESULT_DECL:
294 return TS_RESULT_DECL;
295 case DEBUG_EXPR_DECL:
296 return TS_DECL_WRTL;
297 case CONST_DECL:
298 return TS_CONST_DECL;
299 case TYPE_DECL:
300 return TS_TYPE_DECL;
301 case FUNCTION_DECL:
302 return TS_FUNCTION_DECL;
303 case TRANSLATION_UNIT_DECL:
304 return TS_TRANSLATION_UNIT_DECL;
305 default:
306 return TS_DECL_NON_COMMON;
309 case tcc_type:
310 return TS_TYPE_NON_COMMON;
311 case tcc_reference:
312 case tcc_comparison:
313 case tcc_unary:
314 case tcc_binary:
315 case tcc_expression:
316 case tcc_statement:
317 case tcc_vl_exp:
318 return TS_EXP;
319 default: /* tcc_constant and tcc_exceptional */
320 break;
322 switch (code)
324 /* tcc_constant cases. */
325 case INTEGER_CST: return TS_INT_CST;
326 case REAL_CST: return TS_REAL_CST;
327 case FIXED_CST: return TS_FIXED_CST;
328 case COMPLEX_CST: return TS_COMPLEX;
329 case VECTOR_CST: return TS_VECTOR;
330 case STRING_CST: return TS_STRING;
331 /* tcc_exceptional cases. */
332 case ERROR_MARK: return TS_COMMON;
333 case IDENTIFIER_NODE: return TS_IDENTIFIER;
334 case TREE_LIST: return TS_LIST;
335 case TREE_VEC: return TS_VEC;
336 case SSA_NAME: return TS_SSA_NAME;
337 case PLACEHOLDER_EXPR: return TS_COMMON;
338 case STATEMENT_LIST: return TS_STATEMENT_LIST;
339 case BLOCK: return TS_BLOCK;
340 case CONSTRUCTOR: return TS_CONSTRUCTOR;
341 case TREE_BINFO: return TS_BINFO;
342 case OMP_CLAUSE: return TS_OMP_CLAUSE;
343 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
344 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
346 default:
347 gcc_unreachable ();
352 /* Initialize tree_contains_struct to describe the hierarchy of tree
353 nodes. */
355 static void
356 initialize_tree_contains_struct (void)
358 unsigned i;
360 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
362 enum tree_code code;
363 enum tree_node_structure_enum ts_code;
365 code = (enum tree_code) i;
366 ts_code = tree_node_structure_for_code (code);
368 /* Mark the TS structure itself. */
369 tree_contains_struct[code][ts_code] = 1;
371 /* Mark all the structures that TS is derived from. */
372 switch (ts_code)
374 case TS_TYPED:
375 case TS_BLOCK:
376 MARK_TS_BASE (code);
377 break;
379 case TS_COMMON:
380 case TS_INT_CST:
381 case TS_REAL_CST:
382 case TS_FIXED_CST:
383 case TS_VECTOR:
384 case TS_STRING:
385 case TS_COMPLEX:
386 case TS_SSA_NAME:
387 case TS_CONSTRUCTOR:
388 case TS_EXP:
389 case TS_STATEMENT_LIST:
390 MARK_TS_TYPED (code);
391 break;
393 case TS_IDENTIFIER:
394 case TS_DECL_MINIMAL:
395 case TS_TYPE_COMMON:
396 case TS_LIST:
397 case TS_VEC:
398 case TS_BINFO:
399 case TS_OMP_CLAUSE:
400 case TS_OPTIMIZATION:
401 case TS_TARGET_OPTION:
402 MARK_TS_COMMON (code);
403 break;
405 case TS_TYPE_WITH_LANG_SPECIFIC:
406 MARK_TS_TYPE_COMMON (code);
407 break;
409 case TS_TYPE_NON_COMMON:
410 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
411 break;
413 case TS_DECL_COMMON:
414 MARK_TS_DECL_MINIMAL (code);
415 break;
417 case TS_DECL_WRTL:
418 case TS_CONST_DECL:
419 MARK_TS_DECL_COMMON (code);
420 break;
422 case TS_DECL_NON_COMMON:
423 MARK_TS_DECL_WITH_VIS (code);
424 break;
426 case TS_DECL_WITH_VIS:
427 case TS_PARM_DECL:
428 case TS_LABEL_DECL:
429 case TS_RESULT_DECL:
430 MARK_TS_DECL_WRTL (code);
431 break;
433 case TS_FIELD_DECL:
434 MARK_TS_DECL_COMMON (code);
435 break;
437 case TS_VAR_DECL:
438 MARK_TS_DECL_WITH_VIS (code);
439 break;
441 case TS_TYPE_DECL:
442 case TS_FUNCTION_DECL:
443 MARK_TS_DECL_NON_COMMON (code);
444 break;
446 case TS_TRANSLATION_UNIT_DECL:
447 MARK_TS_DECL_COMMON (code);
448 break;
450 default:
451 gcc_unreachable ();
455 /* Basic consistency checks for attributes used in fold. */
456 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
457 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
458 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
459 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
460 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
461 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
462 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
463 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
464 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
465 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
466 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
467 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
468 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
469 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
470 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
471 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
472 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
473 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
474 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
475 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
476 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
477 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
478 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
479 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
480 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
481 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
482 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
483 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
484 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
485 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
486 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
487 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
488 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
489 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
490 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
491 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
492 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
493 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
497 /* Init tree.c. */
499 void
500 init_ttree (void)
502 /* Initialize the hash table of types. */
503 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
504 type_hash_eq, 0);
506 debug_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
507 tree_decl_map_eq, 0);
509 value_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
510 tree_decl_map_eq, 0);
511 init_priority_for_decl = htab_create_ggc (512, tree_priority_map_hash,
512 tree_priority_map_eq, 0);
514 int_cst_hash_table = htab_create_ggc (1024, int_cst_hash_hash,
515 int_cst_hash_eq, NULL);
517 int_cst_node = make_node (INTEGER_CST);
519 cl_option_hash_table = htab_create_ggc (64, cl_option_hash_hash,
520 cl_option_hash_eq, NULL);
522 cl_optimization_node = make_node (OPTIMIZATION_NODE);
523 cl_target_option_node = make_node (TARGET_OPTION_NODE);
525 /* Initialize the tree_contains_struct array. */
526 initialize_tree_contains_struct ();
527 lang_hooks.init_ts ();
531 /* The name of the object as the assembler will see it (but before any
532 translations made by ASM_OUTPUT_LABELREF). Often this is the same
533 as DECL_NAME. It is an IDENTIFIER_NODE. */
534 tree
535 decl_assembler_name (tree decl)
537 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
538 lang_hooks.set_decl_assembler_name (decl);
539 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
542 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
544 bool
545 decl_assembler_name_equal (tree decl, const_tree asmname)
547 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
548 const char *decl_str;
549 const char *asmname_str;
550 bool test = false;
552 if (decl_asmname == asmname)
553 return true;
555 decl_str = IDENTIFIER_POINTER (decl_asmname);
556 asmname_str = IDENTIFIER_POINTER (asmname);
559 /* If the target assembler name was set by the user, things are trickier.
560 We have a leading '*' to begin with. After that, it's arguable what
561 is the correct thing to do with -fleading-underscore. Arguably, we've
562 historically been doing the wrong thing in assemble_alias by always
563 printing the leading underscore. Since we're not changing that, make
564 sure user_label_prefix follows the '*' before matching. */
565 if (decl_str[0] == '*')
567 size_t ulp_len = strlen (user_label_prefix);
569 decl_str ++;
571 if (ulp_len == 0)
572 test = true;
573 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
574 decl_str += ulp_len, test=true;
575 else
576 decl_str --;
578 if (asmname_str[0] == '*')
580 size_t ulp_len = strlen (user_label_prefix);
582 asmname_str ++;
584 if (ulp_len == 0)
585 test = true;
586 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
587 asmname_str += ulp_len, test=true;
588 else
589 asmname_str --;
592 if (!test)
593 return false;
594 return strcmp (decl_str, asmname_str) == 0;
597 /* Hash asmnames ignoring the user specified marks. */
599 hashval_t
600 decl_assembler_name_hash (const_tree asmname)
602 if (IDENTIFIER_POINTER (asmname)[0] == '*')
604 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
605 size_t ulp_len = strlen (user_label_prefix);
607 if (ulp_len == 0)
609 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
610 decl_str += ulp_len;
612 return htab_hash_string (decl_str);
615 return htab_hash_string (IDENTIFIER_POINTER (asmname));
618 /* Compute the number of bytes occupied by a tree with code CODE.
619 This function cannot be used for nodes that have variable sizes,
620 including TREE_VEC, STRING_CST, and CALL_EXPR. */
621 size_t
622 tree_code_size (enum tree_code code)
624 switch (TREE_CODE_CLASS (code))
626 case tcc_declaration: /* A decl node */
628 switch (code)
630 case FIELD_DECL:
631 return sizeof (struct tree_field_decl);
632 case PARM_DECL:
633 return sizeof (struct tree_parm_decl);
634 case VAR_DECL:
635 return sizeof (struct tree_var_decl);
636 case LABEL_DECL:
637 return sizeof (struct tree_label_decl);
638 case RESULT_DECL:
639 return sizeof (struct tree_result_decl);
640 case CONST_DECL:
641 return sizeof (struct tree_const_decl);
642 case TYPE_DECL:
643 return sizeof (struct tree_type_decl);
644 case FUNCTION_DECL:
645 return sizeof (struct tree_function_decl);
646 case DEBUG_EXPR_DECL:
647 return sizeof (struct tree_decl_with_rtl);
648 default:
649 return sizeof (struct tree_decl_non_common);
653 case tcc_type: /* a type node */
654 return sizeof (struct tree_type_non_common);
656 case tcc_reference: /* a reference */
657 case tcc_expression: /* an expression */
658 case tcc_statement: /* an expression with side effects */
659 case tcc_comparison: /* a comparison expression */
660 case tcc_unary: /* a unary arithmetic expression */
661 case tcc_binary: /* a binary arithmetic expression */
662 return (sizeof (struct tree_exp)
663 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
665 case tcc_constant: /* a constant */
666 switch (code)
668 case INTEGER_CST: return sizeof (struct tree_int_cst);
669 case REAL_CST: return sizeof (struct tree_real_cst);
670 case FIXED_CST: return sizeof (struct tree_fixed_cst);
671 case COMPLEX_CST: return sizeof (struct tree_complex);
672 case VECTOR_CST: return sizeof (struct tree_vector);
673 case STRING_CST: gcc_unreachable ();
674 default:
675 return lang_hooks.tree_size (code);
678 case tcc_exceptional: /* something random, like an identifier. */
679 switch (code)
681 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
682 case TREE_LIST: return sizeof (struct tree_list);
684 case ERROR_MARK:
685 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
687 case TREE_VEC:
688 case OMP_CLAUSE: gcc_unreachable ();
690 case SSA_NAME: return sizeof (struct tree_ssa_name);
692 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
693 case BLOCK: return sizeof (struct tree_block);
694 case CONSTRUCTOR: return sizeof (struct tree_constructor);
695 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
696 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
698 default:
699 return lang_hooks.tree_size (code);
702 default:
703 gcc_unreachable ();
707 /* Compute the number of bytes occupied by NODE. This routine only
708 looks at TREE_CODE, except for those nodes that have variable sizes. */
709 size_t
710 tree_size (const_tree node)
712 const enum tree_code code = TREE_CODE (node);
713 switch (code)
715 case TREE_BINFO:
716 return (offsetof (struct tree_binfo, base_binfos)
717 + vec<tree, va_gc>
718 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
720 case TREE_VEC:
721 return (sizeof (struct tree_vec)
722 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
724 case VECTOR_CST:
725 return (sizeof (struct tree_vector)
726 + (TYPE_VECTOR_SUBPARTS (TREE_TYPE (node)) - 1) * sizeof (tree));
728 case STRING_CST:
729 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
731 case OMP_CLAUSE:
732 return (sizeof (struct tree_omp_clause)
733 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
734 * sizeof (tree));
736 default:
737 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
738 return (sizeof (struct tree_exp)
739 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
740 else
741 return tree_code_size (code);
745 /* Record interesting allocation statistics for a tree node with CODE
746 and LENGTH. */
748 static void
749 record_node_allocation_statistics (enum tree_code code ATTRIBUTE_UNUSED,
750 size_t length ATTRIBUTE_UNUSED)
752 enum tree_code_class type = TREE_CODE_CLASS (code);
753 tree_node_kind kind;
755 if (!GATHER_STATISTICS)
756 return;
758 switch (type)
760 case tcc_declaration: /* A decl node */
761 kind = d_kind;
762 break;
764 case tcc_type: /* a type node */
765 kind = t_kind;
766 break;
768 case tcc_statement: /* an expression with side effects */
769 kind = s_kind;
770 break;
772 case tcc_reference: /* a reference */
773 kind = r_kind;
774 break;
776 case tcc_expression: /* an expression */
777 case tcc_comparison: /* a comparison expression */
778 case tcc_unary: /* a unary arithmetic expression */
779 case tcc_binary: /* a binary arithmetic expression */
780 kind = e_kind;
781 break;
783 case tcc_constant: /* a constant */
784 kind = c_kind;
785 break;
787 case tcc_exceptional: /* something random, like an identifier. */
788 switch (code)
790 case IDENTIFIER_NODE:
791 kind = id_kind;
792 break;
794 case TREE_VEC:
795 kind = vec_kind;
796 break;
798 case TREE_BINFO:
799 kind = binfo_kind;
800 break;
802 case SSA_NAME:
803 kind = ssa_name_kind;
804 break;
806 case BLOCK:
807 kind = b_kind;
808 break;
810 case CONSTRUCTOR:
811 kind = constr_kind;
812 break;
814 case OMP_CLAUSE:
815 kind = omp_clause_kind;
816 break;
818 default:
819 kind = x_kind;
820 break;
822 break;
824 case tcc_vl_exp:
825 kind = e_kind;
826 break;
828 default:
829 gcc_unreachable ();
832 tree_code_counts[(int) code]++;
833 tree_node_counts[(int) kind]++;
834 tree_node_sizes[(int) kind] += length;
837 /* Allocate and return a new UID from the DECL_UID namespace. */
840 allocate_decl_uid (void)
842 return next_decl_uid++;
845 /* Return a newly allocated node of code CODE. For decl and type
846 nodes, some other fields are initialized. The rest of the node is
847 initialized to zero. This function cannot be used for TREE_VEC or
848 OMP_CLAUSE nodes, which is enforced by asserts in tree_code_size.
850 Achoo! I got a code in the node. */
852 tree
853 make_node_stat (enum tree_code code MEM_STAT_DECL)
855 tree t;
856 enum tree_code_class type = TREE_CODE_CLASS (code);
857 size_t length = tree_code_size (code);
859 record_node_allocation_statistics (code, length);
861 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
862 TREE_SET_CODE (t, code);
864 switch (type)
866 case tcc_statement:
867 TREE_SIDE_EFFECTS (t) = 1;
868 break;
870 case tcc_declaration:
871 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
873 if (code == FUNCTION_DECL)
875 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
876 DECL_MODE (t) = FUNCTION_MODE;
878 else
879 DECL_ALIGN (t) = 1;
881 DECL_SOURCE_LOCATION (t) = input_location;
882 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
883 DECL_UID (t) = --next_debug_decl_uid;
884 else
886 DECL_UID (t) = allocate_decl_uid ();
887 SET_DECL_PT_UID (t, -1);
889 if (TREE_CODE (t) == LABEL_DECL)
890 LABEL_DECL_UID (t) = -1;
892 break;
894 case tcc_type:
895 TYPE_UID (t) = next_type_uid++;
896 TYPE_ALIGN (t) = BITS_PER_UNIT;
897 TYPE_USER_ALIGN (t) = 0;
898 TYPE_MAIN_VARIANT (t) = t;
899 TYPE_CANONICAL (t) = t;
901 /* Default to no attributes for type, but let target change that. */
902 TYPE_ATTRIBUTES (t) = NULL_TREE;
903 targetm.set_default_type_attributes (t);
905 /* We have not yet computed the alias set for this type. */
906 TYPE_ALIAS_SET (t) = -1;
907 break;
909 case tcc_constant:
910 TREE_CONSTANT (t) = 1;
911 break;
913 case tcc_expression:
914 switch (code)
916 case INIT_EXPR:
917 case MODIFY_EXPR:
918 case VA_ARG_EXPR:
919 case PREDECREMENT_EXPR:
920 case PREINCREMENT_EXPR:
921 case POSTDECREMENT_EXPR:
922 case POSTINCREMENT_EXPR:
923 /* All of these have side-effects, no matter what their
924 operands are. */
925 TREE_SIDE_EFFECTS (t) = 1;
926 break;
928 default:
929 break;
931 break;
933 default:
934 /* Other classes need no special treatment. */
935 break;
938 return t;
941 /* Return a new node with the same contents as NODE except that its
942 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
944 tree
945 copy_node_stat (tree node MEM_STAT_DECL)
947 tree t;
948 enum tree_code code = TREE_CODE (node);
949 size_t length;
951 gcc_assert (code != STATEMENT_LIST);
953 length = tree_size (node);
954 record_node_allocation_statistics (code, length);
955 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
956 memcpy (t, node, length);
958 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
959 TREE_CHAIN (t) = 0;
960 TREE_ASM_WRITTEN (t) = 0;
961 TREE_VISITED (t) = 0;
963 if (TREE_CODE_CLASS (code) == tcc_declaration)
965 if (code == DEBUG_EXPR_DECL)
966 DECL_UID (t) = --next_debug_decl_uid;
967 else
969 DECL_UID (t) = allocate_decl_uid ();
970 if (DECL_PT_UID_SET_P (node))
971 SET_DECL_PT_UID (t, DECL_PT_UID (node));
973 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
974 && DECL_HAS_VALUE_EXPR_P (node))
976 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
977 DECL_HAS_VALUE_EXPR_P (t) = 1;
979 /* DECL_DEBUG_EXPR is copied explicitely by callers. */
980 if (TREE_CODE (node) == VAR_DECL)
981 DECL_HAS_DEBUG_EXPR_P (t) = 0;
982 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
984 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
985 DECL_HAS_INIT_PRIORITY_P (t) = 1;
987 if (TREE_CODE (node) == FUNCTION_DECL)
988 DECL_STRUCT_FUNCTION (t) = NULL;
990 else if (TREE_CODE_CLASS (code) == tcc_type)
992 TYPE_UID (t) = next_type_uid++;
993 /* The following is so that the debug code for
994 the copy is different from the original type.
995 The two statements usually duplicate each other
996 (because they clear fields of the same union),
997 but the optimizer should catch that. */
998 TYPE_SYMTAB_POINTER (t) = 0;
999 TYPE_SYMTAB_ADDRESS (t) = 0;
1001 /* Do not copy the values cache. */
1002 if (TYPE_CACHED_VALUES_P(t))
1004 TYPE_CACHED_VALUES_P (t) = 0;
1005 TYPE_CACHED_VALUES (t) = NULL_TREE;
1009 return t;
1012 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1013 For example, this can copy a list made of TREE_LIST nodes. */
1015 tree
1016 copy_list (tree list)
1018 tree head;
1019 tree prev, next;
1021 if (list == 0)
1022 return 0;
1024 head = prev = copy_node (list);
1025 next = TREE_CHAIN (list);
1026 while (next)
1028 TREE_CHAIN (prev) = copy_node (next);
1029 prev = TREE_CHAIN (prev);
1030 next = TREE_CHAIN (next);
1032 return head;
1036 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1038 tree
1039 build_int_cst (tree type, HOST_WIDE_INT low)
1041 /* Support legacy code. */
1042 if (!type)
1043 type = integer_type_node;
1045 return double_int_to_tree (type, double_int::from_shwi (low));
1048 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1050 tree
1051 build_int_cst_type (tree type, HOST_WIDE_INT low)
1053 gcc_assert (type);
1055 return double_int_to_tree (type, double_int::from_shwi (low));
1058 /* Constructs tree in type TYPE from with value given by CST. Signedness
1059 of CST is assumed to be the same as the signedness of TYPE. */
1061 tree
1062 double_int_to_tree (tree type, double_int cst)
1064 bool sign_extended_type = !TYPE_UNSIGNED (type);
1066 cst = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1068 return build_int_cst_wide (type, cst.low, cst.high);
1071 /* Returns true if CST fits into range of TYPE. Signedness of CST is assumed
1072 to be the same as the signedness of TYPE. */
1074 bool
1075 double_int_fits_to_tree_p (const_tree type, double_int cst)
1077 bool sign_extended_type = !TYPE_UNSIGNED (type);
1079 double_int ext
1080 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1082 return cst == ext;
1085 /* We force the double_int CST to the range of the type TYPE by sign or
1086 zero extending it. OVERFLOWABLE indicates if we are interested in
1087 overflow of the value, when >0 we are only interested in signed
1088 overflow, for <0 we are interested in any overflow. OVERFLOWED
1089 indicates whether overflow has already occurred. CONST_OVERFLOWED
1090 indicates whether constant overflow has already occurred. We force
1091 T's value to be within range of T's type (by setting to 0 or 1 all
1092 the bits outside the type's range). We set TREE_OVERFLOWED if,
1093 OVERFLOWED is nonzero,
1094 or OVERFLOWABLE is >0 and signed overflow occurs
1095 or OVERFLOWABLE is <0 and any overflow occurs
1096 We return a new tree node for the extended double_int. The node
1097 is shared if no overflow flags are set. */
1100 tree
1101 force_fit_type_double (tree type, double_int cst, int overflowable,
1102 bool overflowed)
1104 bool sign_extended_type = !TYPE_UNSIGNED (type);
1106 /* If we need to set overflow flags, return a new unshared node. */
1107 if (overflowed || !double_int_fits_to_tree_p(type, cst))
1109 if (overflowed
1110 || overflowable < 0
1111 || (overflowable > 0 && sign_extended_type))
1113 tree t = make_node (INTEGER_CST);
1114 TREE_INT_CST (t)
1115 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1116 TREE_TYPE (t) = type;
1117 TREE_OVERFLOW (t) = 1;
1118 return t;
1122 /* Else build a shared node. */
1123 return double_int_to_tree (type, cst);
1126 /* These are the hash table functions for the hash table of INTEGER_CST
1127 nodes of a sizetype. */
1129 /* Return the hash code code X, an INTEGER_CST. */
1131 static hashval_t
1132 int_cst_hash_hash (const void *x)
1134 const_tree const t = (const_tree) x;
1136 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1137 ^ htab_hash_pointer (TREE_TYPE (t)));
1140 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1141 is the same as that given by *Y, which is the same. */
1143 static int
1144 int_cst_hash_eq (const void *x, const void *y)
1146 const_tree const xt = (const_tree) x;
1147 const_tree const yt = (const_tree) y;
1149 return (TREE_TYPE (xt) == TREE_TYPE (yt)
1150 && TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1151 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt));
1154 /* Create an INT_CST node of TYPE and value HI:LOW.
1155 The returned node is always shared. For small integers we use a
1156 per-type vector cache, for larger ones we use a single hash table. */
1158 tree
1159 build_int_cst_wide (tree type, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
1161 tree t;
1162 int ix = -1;
1163 int limit = 0;
1165 gcc_assert (type);
1167 switch (TREE_CODE (type))
1169 case NULLPTR_TYPE:
1170 gcc_assert (hi == 0 && low == 0);
1171 /* Fallthru. */
1173 case POINTER_TYPE:
1174 case REFERENCE_TYPE:
1175 /* Cache NULL pointer. */
1176 if (!hi && !low)
1178 limit = 1;
1179 ix = 0;
1181 break;
1183 case BOOLEAN_TYPE:
1184 /* Cache false or true. */
1185 limit = 2;
1186 if (!hi && low < 2)
1187 ix = low;
1188 break;
1190 case INTEGER_TYPE:
1191 case OFFSET_TYPE:
1192 if (TYPE_UNSIGNED (type))
1194 /* Cache 0..N */
1195 limit = INTEGER_SHARE_LIMIT;
1196 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1197 ix = low;
1199 else
1201 /* Cache -1..N */
1202 limit = INTEGER_SHARE_LIMIT + 1;
1203 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1204 ix = low + 1;
1205 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
1206 ix = 0;
1208 break;
1210 case ENUMERAL_TYPE:
1211 break;
1213 default:
1214 gcc_unreachable ();
1217 if (ix >= 0)
1219 /* Look for it in the type's vector of small shared ints. */
1220 if (!TYPE_CACHED_VALUES_P (type))
1222 TYPE_CACHED_VALUES_P (type) = 1;
1223 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1226 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1227 if (t)
1229 /* Make sure no one is clobbering the shared constant. */
1230 gcc_assert (TREE_TYPE (t) == type);
1231 gcc_assert (TREE_INT_CST_LOW (t) == low);
1232 gcc_assert (TREE_INT_CST_HIGH (t) == hi);
1234 else
1236 /* Create a new shared int. */
1237 t = make_node (INTEGER_CST);
1239 TREE_INT_CST_LOW (t) = low;
1240 TREE_INT_CST_HIGH (t) = hi;
1241 TREE_TYPE (t) = type;
1243 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1246 else
1248 /* Use the cache of larger shared ints. */
1249 void **slot;
1251 TREE_INT_CST_LOW (int_cst_node) = low;
1252 TREE_INT_CST_HIGH (int_cst_node) = hi;
1253 TREE_TYPE (int_cst_node) = type;
1255 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
1256 t = (tree) *slot;
1257 if (!t)
1259 /* Insert this one into the hash table. */
1260 t = int_cst_node;
1261 *slot = t;
1262 /* Make a new node for next time round. */
1263 int_cst_node = make_node (INTEGER_CST);
1267 return t;
1270 void
1271 cache_integer_cst (tree t)
1273 tree type = TREE_TYPE (t);
1274 HOST_WIDE_INT hi = TREE_INT_CST_HIGH (t);
1275 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (t);
1276 int ix = -1;
1277 int limit = 0;
1279 gcc_assert (!TREE_OVERFLOW (t));
1281 switch (TREE_CODE (type))
1283 case NULLPTR_TYPE:
1284 gcc_assert (hi == 0 && low == 0);
1285 /* Fallthru. */
1287 case POINTER_TYPE:
1288 case REFERENCE_TYPE:
1289 /* Cache NULL pointer. */
1290 if (!hi && !low)
1292 limit = 1;
1293 ix = 0;
1295 break;
1297 case BOOLEAN_TYPE:
1298 /* Cache false or true. */
1299 limit = 2;
1300 if (!hi && low < 2)
1301 ix = low;
1302 break;
1304 case INTEGER_TYPE:
1305 case OFFSET_TYPE:
1306 if (TYPE_UNSIGNED (type))
1308 /* Cache 0..N */
1309 limit = INTEGER_SHARE_LIMIT;
1310 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1311 ix = low;
1313 else
1315 /* Cache -1..N */
1316 limit = INTEGER_SHARE_LIMIT + 1;
1317 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1318 ix = low + 1;
1319 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
1320 ix = 0;
1322 break;
1324 case ENUMERAL_TYPE:
1325 break;
1327 default:
1328 gcc_unreachable ();
1331 if (ix >= 0)
1333 /* Look for it in the type's vector of small shared ints. */
1334 if (!TYPE_CACHED_VALUES_P (type))
1336 TYPE_CACHED_VALUES_P (type) = 1;
1337 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1340 gcc_assert (TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) == NULL_TREE);
1341 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1343 else
1345 /* Use the cache of larger shared ints. */
1346 void **slot;
1348 slot = htab_find_slot (int_cst_hash_table, t, INSERT);
1349 /* If there is already an entry for the number verify it's the
1350 same. */
1351 if (*slot)
1353 gcc_assert (TREE_INT_CST_LOW ((tree)*slot) == low
1354 && TREE_INT_CST_HIGH ((tree)*slot) == hi);
1355 return;
1357 /* Otherwise insert this one into the hash table. */
1358 *slot = t;
1363 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1364 and the rest are zeros. */
1366 tree
1367 build_low_bits_mask (tree type, unsigned bits)
1369 double_int mask;
1371 gcc_assert (bits <= TYPE_PRECISION (type));
1373 if (bits == TYPE_PRECISION (type)
1374 && !TYPE_UNSIGNED (type))
1375 /* Sign extended all-ones mask. */
1376 mask = double_int_minus_one;
1377 else
1378 mask = double_int::mask (bits);
1380 return build_int_cst_wide (type, mask.low, mask.high);
1383 /* Checks that X is integer constant that can be expressed in (unsigned)
1384 HOST_WIDE_INT without loss of precision. */
1386 bool
1387 cst_and_fits_in_hwi (const_tree x)
1389 if (TREE_CODE (x) != INTEGER_CST)
1390 return false;
1392 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1393 return false;
1395 return (TREE_INT_CST_HIGH (x) == 0
1396 || TREE_INT_CST_HIGH (x) == -1);
1399 /* Build a newly constructed TREE_VEC node of length LEN. */
1401 tree
1402 make_vector_stat (unsigned len MEM_STAT_DECL)
1404 tree t;
1405 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1407 record_node_allocation_statistics (VECTOR_CST, length);
1409 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1411 TREE_SET_CODE (t, VECTOR_CST);
1412 TREE_CONSTANT (t) = 1;
1414 return t;
1417 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1418 are in a list pointed to by VALS. */
1420 tree
1421 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1423 int over = 0;
1424 unsigned cnt = 0;
1425 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1426 TREE_TYPE (v) = type;
1428 /* Iterate through elements and check for overflow. */
1429 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1431 tree value = vals[cnt];
1433 VECTOR_CST_ELT (v, cnt) = value;
1435 /* Don't crash if we get an address constant. */
1436 if (!CONSTANT_CLASS_P (value))
1437 continue;
1439 over |= TREE_OVERFLOW (value);
1442 TREE_OVERFLOW (v) = over;
1443 return v;
1446 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1447 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1449 tree
1450 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1452 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1453 unsigned HOST_WIDE_INT idx;
1454 tree value;
1456 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1457 vec[idx] = value;
1458 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1459 vec[idx] = build_zero_cst (TREE_TYPE (type));
1461 return build_vector (type, vec);
1464 /* Build a vector of type VECTYPE where all the elements are SCs. */
1465 tree
1466 build_vector_from_val (tree vectype, tree sc)
1468 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1470 if (sc == error_mark_node)
1471 return sc;
1473 /* Verify that the vector type is suitable for SC. Note that there
1474 is some inconsistency in the type-system with respect to restrict
1475 qualifications of pointers. Vector types always have a main-variant
1476 element type and the qualification is applied to the vector-type.
1477 So TREE_TYPE (vector-type) does not return a properly qualified
1478 vector element-type. */
1479 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1480 TREE_TYPE (vectype)));
1482 if (CONSTANT_CLASS_P (sc))
1484 tree *v = XALLOCAVEC (tree, nunits);
1485 for (i = 0; i < nunits; ++i)
1486 v[i] = sc;
1487 return build_vector (vectype, v);
1489 else
1491 vec<constructor_elt, va_gc> *v;
1492 vec_alloc (v, nunits);
1493 for (i = 0; i < nunits; ++i)
1494 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1495 return build_constructor (vectype, v);
1499 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1500 are in the vec pointed to by VALS. */
1501 tree
1502 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1504 tree c = make_node (CONSTRUCTOR);
1505 unsigned int i;
1506 constructor_elt *elt;
1507 bool constant_p = true;
1508 bool side_effects_p = false;
1510 TREE_TYPE (c) = type;
1511 CONSTRUCTOR_ELTS (c) = vals;
1513 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1515 /* Mostly ctors will have elts that don't have side-effects, so
1516 the usual case is to scan all the elements. Hence a single
1517 loop for both const and side effects, rather than one loop
1518 each (with early outs). */
1519 if (!TREE_CONSTANT (elt->value))
1520 constant_p = false;
1521 if (TREE_SIDE_EFFECTS (elt->value))
1522 side_effects_p = true;
1525 TREE_SIDE_EFFECTS (c) = side_effects_p;
1526 TREE_CONSTANT (c) = constant_p;
1528 return c;
1531 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1532 INDEX and VALUE. */
1533 tree
1534 build_constructor_single (tree type, tree index, tree value)
1536 vec<constructor_elt, va_gc> *v;
1537 constructor_elt elt = {index, value};
1539 vec_alloc (v, 1);
1540 v->quick_push (elt);
1542 return build_constructor (type, v);
1546 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1547 are in a list pointed to by VALS. */
1548 tree
1549 build_constructor_from_list (tree type, tree vals)
1551 tree t;
1552 vec<constructor_elt, va_gc> *v = NULL;
1554 if (vals)
1556 vec_alloc (v, list_length (vals));
1557 for (t = vals; t; t = TREE_CHAIN (t))
1558 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1561 return build_constructor (type, v);
1564 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
1565 of elements, provided as index/value pairs. */
1567 tree
1568 build_constructor_va (tree type, int nelts, ...)
1570 vec<constructor_elt, va_gc> *v = NULL;
1571 va_list p;
1573 va_start (p, nelts);
1574 vec_alloc (v, nelts);
1575 while (nelts--)
1577 tree index = va_arg (p, tree);
1578 tree value = va_arg (p, tree);
1579 CONSTRUCTOR_APPEND_ELT (v, index, value);
1581 va_end (p);
1582 return build_constructor (type, v);
1585 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1587 tree
1588 build_fixed (tree type, FIXED_VALUE_TYPE f)
1590 tree v;
1591 FIXED_VALUE_TYPE *fp;
1593 v = make_node (FIXED_CST);
1594 fp = ggc_alloc_fixed_value ();
1595 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1597 TREE_TYPE (v) = type;
1598 TREE_FIXED_CST_PTR (v) = fp;
1599 return v;
1602 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1604 tree
1605 build_real (tree type, REAL_VALUE_TYPE d)
1607 tree v;
1608 REAL_VALUE_TYPE *dp;
1609 int overflow = 0;
1611 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1612 Consider doing it via real_convert now. */
1614 v = make_node (REAL_CST);
1615 dp = ggc_alloc_real_value ();
1616 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1618 TREE_TYPE (v) = type;
1619 TREE_REAL_CST_PTR (v) = dp;
1620 TREE_OVERFLOW (v) = overflow;
1621 return v;
1624 /* Return a new REAL_CST node whose type is TYPE
1625 and whose value is the integer value of the INTEGER_CST node I. */
1627 REAL_VALUE_TYPE
1628 real_value_from_int_cst (const_tree type, const_tree i)
1630 REAL_VALUE_TYPE d;
1632 /* Clear all bits of the real value type so that we can later do
1633 bitwise comparisons to see if two values are the same. */
1634 memset (&d, 0, sizeof d);
1636 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
1637 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
1638 TYPE_UNSIGNED (TREE_TYPE (i)));
1639 return d;
1642 /* Given a tree representing an integer constant I, return a tree
1643 representing the same value as a floating-point constant of type TYPE. */
1645 tree
1646 build_real_from_int_cst (tree type, const_tree i)
1648 tree v;
1649 int overflow = TREE_OVERFLOW (i);
1651 v = build_real (type, real_value_from_int_cst (type, i));
1653 TREE_OVERFLOW (v) |= overflow;
1654 return v;
1657 /* Return a newly constructed STRING_CST node whose value is
1658 the LEN characters at STR.
1659 Note that for a C string literal, LEN should include the trailing NUL.
1660 The TREE_TYPE is not initialized. */
1662 tree
1663 build_string (int len, const char *str)
1665 tree s;
1666 size_t length;
1668 /* Do not waste bytes provided by padding of struct tree_string. */
1669 length = len + offsetof (struct tree_string, str) + 1;
1671 record_node_allocation_statistics (STRING_CST, length);
1673 s = ggc_alloc_tree_node (length);
1675 memset (s, 0, sizeof (struct tree_typed));
1676 TREE_SET_CODE (s, STRING_CST);
1677 TREE_CONSTANT (s) = 1;
1678 TREE_STRING_LENGTH (s) = len;
1679 memcpy (s->string.str, str, len);
1680 s->string.str[len] = '\0';
1682 return s;
1685 /* Return a newly constructed COMPLEX_CST node whose value is
1686 specified by the real and imaginary parts REAL and IMAG.
1687 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1688 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1690 tree
1691 build_complex (tree type, tree real, tree imag)
1693 tree t = make_node (COMPLEX_CST);
1695 TREE_REALPART (t) = real;
1696 TREE_IMAGPART (t) = imag;
1697 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1698 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1699 return t;
1702 /* Return a constant of arithmetic type TYPE which is the
1703 multiplicative identity of the set TYPE. */
1705 tree
1706 build_one_cst (tree type)
1708 switch (TREE_CODE (type))
1710 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1711 case POINTER_TYPE: case REFERENCE_TYPE:
1712 case OFFSET_TYPE:
1713 return build_int_cst (type, 1);
1715 case REAL_TYPE:
1716 return build_real (type, dconst1);
1718 case FIXED_POINT_TYPE:
1719 /* We can only generate 1 for accum types. */
1720 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1721 return build_fixed (type, FCONST1(TYPE_MODE (type)));
1723 case VECTOR_TYPE:
1725 tree scalar = build_one_cst (TREE_TYPE (type));
1727 return build_vector_from_val (type, scalar);
1730 case COMPLEX_TYPE:
1731 return build_complex (type,
1732 build_one_cst (TREE_TYPE (type)),
1733 build_zero_cst (TREE_TYPE (type)));
1735 default:
1736 gcc_unreachable ();
1740 /* Return an integer of type TYPE containing all 1's in as much precision as
1741 it contains, or a complex or vector whose subparts are such integers. */
1743 tree
1744 build_all_ones_cst (tree type)
1746 if (TREE_CODE (type) == COMPLEX_TYPE)
1748 tree scalar = build_all_ones_cst (TREE_TYPE (type));
1749 return build_complex (type, scalar, scalar);
1751 else
1752 return build_minus_one_cst (type);
1755 /* Return a constant of arithmetic type TYPE which is the
1756 opposite of the multiplicative identity of the set TYPE. */
1758 tree
1759 build_minus_one_cst (tree type)
1761 switch (TREE_CODE (type))
1763 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1764 case POINTER_TYPE: case REFERENCE_TYPE:
1765 case OFFSET_TYPE:
1766 return build_int_cst (type, -1);
1768 case REAL_TYPE:
1769 return build_real (type, dconstm1);
1771 case FIXED_POINT_TYPE:
1772 /* We can only generate 1 for accum types. */
1773 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1774 return build_fixed (type, fixed_from_double_int (double_int_minus_one,
1775 TYPE_MODE (type)));
1777 case VECTOR_TYPE:
1779 tree scalar = build_minus_one_cst (TREE_TYPE (type));
1781 return build_vector_from_val (type, scalar);
1784 case COMPLEX_TYPE:
1785 return build_complex (type,
1786 build_minus_one_cst (TREE_TYPE (type)),
1787 build_zero_cst (TREE_TYPE (type)));
1789 default:
1790 gcc_unreachable ();
1794 /* Build 0 constant of type TYPE. This is used by constructor folding
1795 and thus the constant should be represented in memory by
1796 zero(es). */
1798 tree
1799 build_zero_cst (tree type)
1801 switch (TREE_CODE (type))
1803 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1804 case POINTER_TYPE: case REFERENCE_TYPE:
1805 case OFFSET_TYPE: case NULLPTR_TYPE:
1806 return build_int_cst (type, 0);
1808 case REAL_TYPE:
1809 return build_real (type, dconst0);
1811 case FIXED_POINT_TYPE:
1812 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
1814 case VECTOR_TYPE:
1816 tree scalar = build_zero_cst (TREE_TYPE (type));
1818 return build_vector_from_val (type, scalar);
1821 case COMPLEX_TYPE:
1823 tree zero = build_zero_cst (TREE_TYPE (type));
1825 return build_complex (type, zero, zero);
1828 default:
1829 if (!AGGREGATE_TYPE_P (type))
1830 return fold_convert (type, integer_zero_node);
1831 return build_constructor (type, NULL);
1836 /* Build a BINFO with LEN language slots. */
1838 tree
1839 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1841 tree t;
1842 size_t length = (offsetof (struct tree_binfo, base_binfos)
1843 + vec<tree, va_gc>::embedded_size (base_binfos));
1845 record_node_allocation_statistics (TREE_BINFO, length);
1847 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1849 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1851 TREE_SET_CODE (t, TREE_BINFO);
1853 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
1855 return t;
1858 /* Create a CASE_LABEL_EXPR tree node and return it. */
1860 tree
1861 build_case_label (tree low_value, tree high_value, tree label_decl)
1863 tree t = make_node (CASE_LABEL_EXPR);
1865 TREE_TYPE (t) = void_type_node;
1866 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
1868 CASE_LOW (t) = low_value;
1869 CASE_HIGH (t) = high_value;
1870 CASE_LABEL (t) = label_decl;
1871 CASE_CHAIN (t) = NULL_TREE;
1873 return t;
1876 /* Build a newly constructed TREE_VEC node of length LEN. */
1878 tree
1879 make_tree_vec_stat (int len MEM_STAT_DECL)
1881 tree t;
1882 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1884 record_node_allocation_statistics (TREE_VEC, length);
1886 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1888 TREE_SET_CODE (t, TREE_VEC);
1889 TREE_VEC_LENGTH (t) = len;
1891 return t;
1894 /* Return 1 if EXPR is the integer constant zero or a complex constant
1895 of zero. */
1898 integer_zerop (const_tree expr)
1900 STRIP_NOPS (expr);
1902 switch (TREE_CODE (expr))
1904 case INTEGER_CST:
1905 return (TREE_INT_CST_LOW (expr) == 0
1906 && TREE_INT_CST_HIGH (expr) == 0);
1907 case COMPLEX_CST:
1908 return (integer_zerop (TREE_REALPART (expr))
1909 && integer_zerop (TREE_IMAGPART (expr)));
1910 case VECTOR_CST:
1912 unsigned i;
1913 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1914 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
1915 return false;
1916 return true;
1918 default:
1919 return false;
1923 /* Return 1 if EXPR is the integer constant one or the corresponding
1924 complex constant. */
1927 integer_onep (const_tree expr)
1929 STRIP_NOPS (expr);
1931 switch (TREE_CODE (expr))
1933 case INTEGER_CST:
1934 return (TREE_INT_CST_LOW (expr) == 1
1935 && TREE_INT_CST_HIGH (expr) == 0);
1936 case COMPLEX_CST:
1937 return (integer_onep (TREE_REALPART (expr))
1938 && integer_zerop (TREE_IMAGPART (expr)));
1939 case VECTOR_CST:
1941 unsigned i;
1942 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1943 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
1944 return false;
1945 return true;
1947 default:
1948 return false;
1952 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1953 it contains, or a complex or vector whose subparts are such integers. */
1956 integer_all_onesp (const_tree expr)
1958 int prec;
1959 int uns;
1961 STRIP_NOPS (expr);
1963 if (TREE_CODE (expr) == COMPLEX_CST
1964 && integer_all_onesp (TREE_REALPART (expr))
1965 && integer_all_onesp (TREE_IMAGPART (expr)))
1966 return 1;
1968 else if (TREE_CODE (expr) == VECTOR_CST)
1970 unsigned i;
1971 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1972 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
1973 return 0;
1974 return 1;
1977 else if (TREE_CODE (expr) != INTEGER_CST)
1978 return 0;
1980 uns = TYPE_UNSIGNED (TREE_TYPE (expr));
1981 if (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1982 && TREE_INT_CST_HIGH (expr) == -1)
1983 return 1;
1984 if (!uns)
1985 return 0;
1987 prec = TYPE_PRECISION (TREE_TYPE (expr));
1988 if (prec >= HOST_BITS_PER_WIDE_INT)
1990 HOST_WIDE_INT high_value;
1991 int shift_amount;
1993 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1995 /* Can not handle precisions greater than twice the host int size. */
1996 gcc_assert (shift_amount <= HOST_BITS_PER_WIDE_INT);
1997 if (shift_amount == HOST_BITS_PER_WIDE_INT)
1998 /* Shifting by the host word size is undefined according to the ANSI
1999 standard, so we must handle this as a special case. */
2000 high_value = -1;
2001 else
2002 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
2004 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
2005 && TREE_INT_CST_HIGH (expr) == high_value);
2007 else
2008 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
2011 /* Return 1 if EXPR is the integer constant minus one. */
2014 integer_minus_onep (const_tree expr)
2016 STRIP_NOPS (expr);
2018 if (TREE_CODE (expr) == COMPLEX_CST)
2019 return (integer_all_onesp (TREE_REALPART (expr))
2020 && integer_zerop (TREE_IMAGPART (expr)));
2021 else
2022 return integer_all_onesp (expr);
2025 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
2026 one bit on). */
2029 integer_pow2p (const_tree expr)
2031 int prec;
2032 unsigned HOST_WIDE_INT high, low;
2034 STRIP_NOPS (expr);
2036 if (TREE_CODE (expr) == COMPLEX_CST
2037 && integer_pow2p (TREE_REALPART (expr))
2038 && integer_zerop (TREE_IMAGPART (expr)))
2039 return 1;
2041 if (TREE_CODE (expr) != INTEGER_CST)
2042 return 0;
2044 prec = TYPE_PRECISION (TREE_TYPE (expr));
2045 high = TREE_INT_CST_HIGH (expr);
2046 low = TREE_INT_CST_LOW (expr);
2048 /* First clear all bits that are beyond the type's precision in case
2049 we've been sign extended. */
2051 if (prec == HOST_BITS_PER_DOUBLE_INT)
2053 else if (prec > HOST_BITS_PER_WIDE_INT)
2054 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
2055 else
2057 high = 0;
2058 if (prec < HOST_BITS_PER_WIDE_INT)
2059 low &= ~((HOST_WIDE_INT) (-1) << prec);
2062 if (high == 0 && low == 0)
2063 return 0;
2065 return ((high == 0 && (low & (low - 1)) == 0)
2066 || (low == 0 && (high & (high - 1)) == 0));
2069 /* Return 1 if EXPR is an integer constant other than zero or a
2070 complex constant other than zero. */
2073 integer_nonzerop (const_tree expr)
2075 STRIP_NOPS (expr);
2077 return ((TREE_CODE (expr) == INTEGER_CST
2078 && (TREE_INT_CST_LOW (expr) != 0
2079 || TREE_INT_CST_HIGH (expr) != 0))
2080 || (TREE_CODE (expr) == COMPLEX_CST
2081 && (integer_nonzerop (TREE_REALPART (expr))
2082 || integer_nonzerop (TREE_IMAGPART (expr)))));
2085 /* Return 1 if EXPR is the fixed-point constant zero. */
2088 fixed_zerop (const_tree expr)
2090 return (TREE_CODE (expr) == FIXED_CST
2091 && TREE_FIXED_CST (expr).data.is_zero ());
2094 /* Return the power of two represented by a tree node known to be a
2095 power of two. */
2098 tree_log2 (const_tree expr)
2100 int prec;
2101 HOST_WIDE_INT high, low;
2103 STRIP_NOPS (expr);
2105 if (TREE_CODE (expr) == COMPLEX_CST)
2106 return tree_log2 (TREE_REALPART (expr));
2108 prec = TYPE_PRECISION (TREE_TYPE (expr));
2109 high = TREE_INT_CST_HIGH (expr);
2110 low = TREE_INT_CST_LOW (expr);
2112 /* First clear all bits that are beyond the type's precision in case
2113 we've been sign extended. */
2115 if (prec == HOST_BITS_PER_DOUBLE_INT)
2117 else if (prec > HOST_BITS_PER_WIDE_INT)
2118 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
2119 else
2121 high = 0;
2122 if (prec < HOST_BITS_PER_WIDE_INT)
2123 low &= ~((HOST_WIDE_INT) (-1) << prec);
2126 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
2127 : exact_log2 (low));
2130 /* Similar, but return the largest integer Y such that 2 ** Y is less
2131 than or equal to EXPR. */
2134 tree_floor_log2 (const_tree expr)
2136 int prec;
2137 HOST_WIDE_INT high, low;
2139 STRIP_NOPS (expr);
2141 if (TREE_CODE (expr) == COMPLEX_CST)
2142 return tree_log2 (TREE_REALPART (expr));
2144 prec = TYPE_PRECISION (TREE_TYPE (expr));
2145 high = TREE_INT_CST_HIGH (expr);
2146 low = TREE_INT_CST_LOW (expr);
2148 /* First clear all bits that are beyond the type's precision in case
2149 we've been sign extended. Ignore if type's precision hasn't been set
2150 since what we are doing is setting it. */
2152 if (prec == HOST_BITS_PER_DOUBLE_INT || prec == 0)
2154 else if (prec > HOST_BITS_PER_WIDE_INT)
2155 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
2156 else
2158 high = 0;
2159 if (prec < HOST_BITS_PER_WIDE_INT)
2160 low &= ~((HOST_WIDE_INT) (-1) << prec);
2163 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
2164 : floor_log2 (low));
2167 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
2168 decimal float constants, so don't return 1 for them. */
2171 real_zerop (const_tree expr)
2173 STRIP_NOPS (expr);
2175 switch (TREE_CODE (expr))
2177 case REAL_CST:
2178 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
2179 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2180 case COMPLEX_CST:
2181 return real_zerop (TREE_REALPART (expr))
2182 && real_zerop (TREE_IMAGPART (expr));
2183 case VECTOR_CST:
2185 unsigned i;
2186 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2187 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2188 return false;
2189 return true;
2191 default:
2192 return false;
2196 /* Return 1 if EXPR is the real constant one in real or complex form.
2197 Trailing zeroes matter for decimal float constants, so don't return
2198 1 for them. */
2201 real_onep (const_tree expr)
2203 STRIP_NOPS (expr);
2205 switch (TREE_CODE (expr))
2207 case REAL_CST:
2208 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
2209 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2210 case COMPLEX_CST:
2211 return real_onep (TREE_REALPART (expr))
2212 && real_zerop (TREE_IMAGPART (expr));
2213 case VECTOR_CST:
2215 unsigned i;
2216 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2217 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2218 return false;
2219 return true;
2221 default:
2222 return false;
2226 /* Return 1 if EXPR is the real constant two. Trailing zeroes matter
2227 for decimal float constants, so don't return 1 for them. */
2230 real_twop (const_tree expr)
2232 STRIP_NOPS (expr);
2234 switch (TREE_CODE (expr))
2236 case REAL_CST:
2237 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2)
2238 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2239 case COMPLEX_CST:
2240 return real_twop (TREE_REALPART (expr))
2241 && real_zerop (TREE_IMAGPART (expr));
2242 case VECTOR_CST:
2244 unsigned i;
2245 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2246 if (!real_twop (VECTOR_CST_ELT (expr, i)))
2247 return false;
2248 return true;
2250 default:
2251 return false;
2255 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2256 matter for decimal float constants, so don't return 1 for them. */
2259 real_minus_onep (const_tree expr)
2261 STRIP_NOPS (expr);
2263 switch (TREE_CODE (expr))
2265 case REAL_CST:
2266 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2267 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2268 case COMPLEX_CST:
2269 return real_minus_onep (TREE_REALPART (expr))
2270 && real_zerop (TREE_IMAGPART (expr));
2271 case VECTOR_CST:
2273 unsigned i;
2274 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2275 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2276 return false;
2277 return true;
2279 default:
2280 return false;
2284 /* Nonzero if EXP is a constant or a cast of a constant. */
2287 really_constant_p (const_tree exp)
2289 /* This is not quite the same as STRIP_NOPS. It does more. */
2290 while (CONVERT_EXPR_P (exp)
2291 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2292 exp = TREE_OPERAND (exp, 0);
2293 return TREE_CONSTANT (exp);
2296 /* Return first list element whose TREE_VALUE is ELEM.
2297 Return 0 if ELEM is not in LIST. */
2299 tree
2300 value_member (tree elem, tree list)
2302 while (list)
2304 if (elem == TREE_VALUE (list))
2305 return list;
2306 list = TREE_CHAIN (list);
2308 return NULL_TREE;
2311 /* Return first list element whose TREE_PURPOSE is ELEM.
2312 Return 0 if ELEM is not in LIST. */
2314 tree
2315 purpose_member (const_tree elem, tree list)
2317 while (list)
2319 if (elem == TREE_PURPOSE (list))
2320 return list;
2321 list = TREE_CHAIN (list);
2323 return NULL_TREE;
2326 /* Return true if ELEM is in V. */
2328 bool
2329 vec_member (const_tree elem, vec<tree, va_gc> *v)
2331 unsigned ix;
2332 tree t;
2333 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2334 if (elem == t)
2335 return true;
2336 return false;
2339 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2340 NULL_TREE. */
2342 tree
2343 chain_index (int idx, tree chain)
2345 for (; chain && idx > 0; --idx)
2346 chain = TREE_CHAIN (chain);
2347 return chain;
2350 /* Return nonzero if ELEM is part of the chain CHAIN. */
2353 chain_member (const_tree elem, const_tree chain)
2355 while (chain)
2357 if (elem == chain)
2358 return 1;
2359 chain = DECL_CHAIN (chain);
2362 return 0;
2365 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2366 We expect a null pointer to mark the end of the chain.
2367 This is the Lisp primitive `length'. */
2370 list_length (const_tree t)
2372 const_tree p = t;
2373 #ifdef ENABLE_TREE_CHECKING
2374 const_tree q = t;
2375 #endif
2376 int len = 0;
2378 while (p)
2380 p = TREE_CHAIN (p);
2381 #ifdef ENABLE_TREE_CHECKING
2382 if (len % 2)
2383 q = TREE_CHAIN (q);
2384 gcc_assert (p != q);
2385 #endif
2386 len++;
2389 return len;
2392 /* Returns the number of FIELD_DECLs in TYPE. */
2395 fields_length (const_tree type)
2397 tree t = TYPE_FIELDS (type);
2398 int count = 0;
2400 for (; t; t = DECL_CHAIN (t))
2401 if (TREE_CODE (t) == FIELD_DECL)
2402 ++count;
2404 return count;
2407 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2408 UNION_TYPE TYPE, or NULL_TREE if none. */
2410 tree
2411 first_field (const_tree type)
2413 tree t = TYPE_FIELDS (type);
2414 while (t && TREE_CODE (t) != FIELD_DECL)
2415 t = TREE_CHAIN (t);
2416 return t;
2419 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2420 by modifying the last node in chain 1 to point to chain 2.
2421 This is the Lisp primitive `nconc'. */
2423 tree
2424 chainon (tree op1, tree op2)
2426 tree t1;
2428 if (!op1)
2429 return op2;
2430 if (!op2)
2431 return op1;
2433 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2434 continue;
2435 TREE_CHAIN (t1) = op2;
2437 #ifdef ENABLE_TREE_CHECKING
2439 tree t2;
2440 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2441 gcc_assert (t2 != t1);
2443 #endif
2445 return op1;
2448 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2450 tree
2451 tree_last (tree chain)
2453 tree next;
2454 if (chain)
2455 while ((next = TREE_CHAIN (chain)))
2456 chain = next;
2457 return chain;
2460 /* Reverse the order of elements in the chain T,
2461 and return the new head of the chain (old last element). */
2463 tree
2464 nreverse (tree t)
2466 tree prev = 0, decl, next;
2467 for (decl = t; decl; decl = next)
2469 /* We shouldn't be using this function to reverse BLOCK chains; we
2470 have blocks_nreverse for that. */
2471 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2472 next = TREE_CHAIN (decl);
2473 TREE_CHAIN (decl) = prev;
2474 prev = decl;
2476 return prev;
2479 /* Return a newly created TREE_LIST node whose
2480 purpose and value fields are PARM and VALUE. */
2482 tree
2483 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2485 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2486 TREE_PURPOSE (t) = parm;
2487 TREE_VALUE (t) = value;
2488 return t;
2491 /* Build a chain of TREE_LIST nodes from a vector. */
2493 tree
2494 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2496 tree ret = NULL_TREE;
2497 tree *pp = &ret;
2498 unsigned int i;
2499 tree t;
2500 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2502 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2503 pp = &TREE_CHAIN (*pp);
2505 return ret;
2508 /* Return a newly created TREE_LIST node whose
2509 purpose and value fields are PURPOSE and VALUE
2510 and whose TREE_CHAIN is CHAIN. */
2512 tree
2513 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2515 tree node;
2517 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
2518 memset (node, 0, sizeof (struct tree_common));
2520 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2522 TREE_SET_CODE (node, TREE_LIST);
2523 TREE_CHAIN (node) = chain;
2524 TREE_PURPOSE (node) = purpose;
2525 TREE_VALUE (node) = value;
2526 return node;
2529 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2530 trees. */
2532 vec<tree, va_gc> *
2533 ctor_to_vec (tree ctor)
2535 vec<tree, va_gc> *vec;
2536 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2537 unsigned int ix;
2538 tree val;
2540 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2541 vec->quick_push (val);
2543 return vec;
2546 /* Return the size nominally occupied by an object of type TYPE
2547 when it resides in memory. The value is measured in units of bytes,
2548 and its data type is that normally used for type sizes
2549 (which is the first type created by make_signed_type or
2550 make_unsigned_type). */
2552 tree
2553 size_in_bytes (const_tree type)
2555 tree t;
2557 if (type == error_mark_node)
2558 return integer_zero_node;
2560 type = TYPE_MAIN_VARIANT (type);
2561 t = TYPE_SIZE_UNIT (type);
2563 if (t == 0)
2565 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2566 return size_zero_node;
2569 return t;
2572 /* Return the size of TYPE (in bytes) as a wide integer
2573 or return -1 if the size can vary or is larger than an integer. */
2575 HOST_WIDE_INT
2576 int_size_in_bytes (const_tree type)
2578 tree t;
2580 if (type == error_mark_node)
2581 return 0;
2583 type = TYPE_MAIN_VARIANT (type);
2584 t = TYPE_SIZE_UNIT (type);
2585 if (t == 0
2586 || TREE_CODE (t) != INTEGER_CST
2587 || TREE_INT_CST_HIGH (t) != 0
2588 /* If the result would appear negative, it's too big to represent. */
2589 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
2590 return -1;
2592 return TREE_INT_CST_LOW (t);
2595 /* Return the maximum size of TYPE (in bytes) as a wide integer
2596 or return -1 if the size can vary or is larger than an integer. */
2598 HOST_WIDE_INT
2599 max_int_size_in_bytes (const_tree type)
2601 HOST_WIDE_INT size = -1;
2602 tree size_tree;
2604 /* If this is an array type, check for a possible MAX_SIZE attached. */
2606 if (TREE_CODE (type) == ARRAY_TYPE)
2608 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2610 if (size_tree && host_integerp (size_tree, 1))
2611 size = tree_low_cst (size_tree, 1);
2614 /* If we still haven't been able to get a size, see if the language
2615 can compute a maximum size. */
2617 if (size == -1)
2619 size_tree = lang_hooks.types.max_size (type);
2621 if (size_tree && host_integerp (size_tree, 1))
2622 size = tree_low_cst (size_tree, 1);
2625 return size;
2628 /* Returns a tree for the size of EXP in bytes. */
2630 tree
2631 tree_expr_size (const_tree exp)
2633 if (DECL_P (exp)
2634 && DECL_SIZE_UNIT (exp) != 0)
2635 return DECL_SIZE_UNIT (exp);
2636 else
2637 return size_in_bytes (TREE_TYPE (exp));
2640 /* Return the bit position of FIELD, in bits from the start of the record.
2641 This is a tree of type bitsizetype. */
2643 tree
2644 bit_position (const_tree field)
2646 return bit_from_pos (DECL_FIELD_OFFSET (field),
2647 DECL_FIELD_BIT_OFFSET (field));
2650 /* Likewise, but return as an integer. It must be representable in
2651 that way (since it could be a signed value, we don't have the
2652 option of returning -1 like int_size_in_byte can. */
2654 HOST_WIDE_INT
2655 int_bit_position (const_tree field)
2657 return tree_low_cst (bit_position (field), 0);
2660 /* Return the byte position of FIELD, in bytes from the start of the record.
2661 This is a tree of type sizetype. */
2663 tree
2664 byte_position (const_tree field)
2666 return byte_from_pos (DECL_FIELD_OFFSET (field),
2667 DECL_FIELD_BIT_OFFSET (field));
2670 /* Likewise, but return as an integer. It must be representable in
2671 that way (since it could be a signed value, we don't have the
2672 option of returning -1 like int_size_in_byte can. */
2674 HOST_WIDE_INT
2675 int_byte_position (const_tree field)
2677 return tree_low_cst (byte_position (field), 0);
2680 /* Return the strictest alignment, in bits, that T is known to have. */
2682 unsigned int
2683 expr_align (const_tree t)
2685 unsigned int align0, align1;
2687 switch (TREE_CODE (t))
2689 CASE_CONVERT: case NON_LVALUE_EXPR:
2690 /* If we have conversions, we know that the alignment of the
2691 object must meet each of the alignments of the types. */
2692 align0 = expr_align (TREE_OPERAND (t, 0));
2693 align1 = TYPE_ALIGN (TREE_TYPE (t));
2694 return MAX (align0, align1);
2696 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2697 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2698 case CLEANUP_POINT_EXPR:
2699 /* These don't change the alignment of an object. */
2700 return expr_align (TREE_OPERAND (t, 0));
2702 case COND_EXPR:
2703 /* The best we can do is say that the alignment is the least aligned
2704 of the two arms. */
2705 align0 = expr_align (TREE_OPERAND (t, 1));
2706 align1 = expr_align (TREE_OPERAND (t, 2));
2707 return MIN (align0, align1);
2709 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2710 meaningfully, it's always 1. */
2711 case LABEL_DECL: case CONST_DECL:
2712 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2713 case FUNCTION_DECL:
2714 gcc_assert (DECL_ALIGN (t) != 0);
2715 return DECL_ALIGN (t);
2717 default:
2718 break;
2721 /* Otherwise take the alignment from that of the type. */
2722 return TYPE_ALIGN (TREE_TYPE (t));
2725 /* Return, as a tree node, the number of elements for TYPE (which is an
2726 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2728 tree
2729 array_type_nelts (const_tree type)
2731 tree index_type, min, max;
2733 /* If they did it with unspecified bounds, then we should have already
2734 given an error about it before we got here. */
2735 if (! TYPE_DOMAIN (type))
2736 return error_mark_node;
2738 index_type = TYPE_DOMAIN (type);
2739 min = TYPE_MIN_VALUE (index_type);
2740 max = TYPE_MAX_VALUE (index_type);
2742 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
2743 if (!max)
2744 return error_mark_node;
2746 return (integer_zerop (min)
2747 ? max
2748 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2751 /* If arg is static -- a reference to an object in static storage -- then
2752 return the object. This is not the same as the C meaning of `static'.
2753 If arg isn't static, return NULL. */
2755 tree
2756 staticp (tree arg)
2758 switch (TREE_CODE (arg))
2760 case FUNCTION_DECL:
2761 /* Nested functions are static, even though taking their address will
2762 involve a trampoline as we unnest the nested function and create
2763 the trampoline on the tree level. */
2764 return arg;
2766 case VAR_DECL:
2767 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2768 && ! DECL_THREAD_LOCAL_P (arg)
2769 && ! DECL_DLLIMPORT_P (arg)
2770 ? arg : NULL);
2772 case CONST_DECL:
2773 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2774 ? arg : NULL);
2776 case CONSTRUCTOR:
2777 return TREE_STATIC (arg) ? arg : NULL;
2779 case LABEL_DECL:
2780 case STRING_CST:
2781 return arg;
2783 case COMPONENT_REF:
2784 /* If the thing being referenced is not a field, then it is
2785 something language specific. */
2786 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
2788 /* If we are referencing a bitfield, we can't evaluate an
2789 ADDR_EXPR at compile time and so it isn't a constant. */
2790 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2791 return NULL;
2793 return staticp (TREE_OPERAND (arg, 0));
2795 case BIT_FIELD_REF:
2796 return NULL;
2798 case INDIRECT_REF:
2799 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2801 case ARRAY_REF:
2802 case ARRAY_RANGE_REF:
2803 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2804 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2805 return staticp (TREE_OPERAND (arg, 0));
2806 else
2807 return NULL;
2809 case COMPOUND_LITERAL_EXPR:
2810 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
2812 default:
2813 return NULL;
2820 /* Return whether OP is a DECL whose address is function-invariant. */
2822 bool
2823 decl_address_invariant_p (const_tree op)
2825 /* The conditions below are slightly less strict than the one in
2826 staticp. */
2828 switch (TREE_CODE (op))
2830 case PARM_DECL:
2831 case RESULT_DECL:
2832 case LABEL_DECL:
2833 case FUNCTION_DECL:
2834 return true;
2836 case VAR_DECL:
2837 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2838 || DECL_THREAD_LOCAL_P (op)
2839 || DECL_CONTEXT (op) == current_function_decl
2840 || decl_function_context (op) == current_function_decl)
2841 return true;
2842 break;
2844 case CONST_DECL:
2845 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2846 || decl_function_context (op) == current_function_decl)
2847 return true;
2848 break;
2850 default:
2851 break;
2854 return false;
2857 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
2859 bool
2860 decl_address_ip_invariant_p (const_tree op)
2862 /* The conditions below are slightly less strict than the one in
2863 staticp. */
2865 switch (TREE_CODE (op))
2867 case LABEL_DECL:
2868 case FUNCTION_DECL:
2869 case STRING_CST:
2870 return true;
2872 case VAR_DECL:
2873 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2874 && !DECL_DLLIMPORT_P (op))
2875 || DECL_THREAD_LOCAL_P (op))
2876 return true;
2877 break;
2879 case CONST_DECL:
2880 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
2881 return true;
2882 break;
2884 default:
2885 break;
2888 return false;
2892 /* Return true if T is function-invariant (internal function, does
2893 not handle arithmetic; that's handled in skip_simple_arithmetic and
2894 tree_invariant_p). */
2896 static bool tree_invariant_p (tree t);
2898 static bool
2899 tree_invariant_p_1 (tree t)
2901 tree op;
2903 if (TREE_CONSTANT (t)
2904 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
2905 return true;
2907 switch (TREE_CODE (t))
2909 case SAVE_EXPR:
2910 return true;
2912 case ADDR_EXPR:
2913 op = TREE_OPERAND (t, 0);
2914 while (handled_component_p (op))
2916 switch (TREE_CODE (op))
2918 case ARRAY_REF:
2919 case ARRAY_RANGE_REF:
2920 if (!tree_invariant_p (TREE_OPERAND (op, 1))
2921 || TREE_OPERAND (op, 2) != NULL_TREE
2922 || TREE_OPERAND (op, 3) != NULL_TREE)
2923 return false;
2924 break;
2926 case COMPONENT_REF:
2927 if (TREE_OPERAND (op, 2) != NULL_TREE)
2928 return false;
2929 break;
2931 default:;
2933 op = TREE_OPERAND (op, 0);
2936 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2938 default:
2939 break;
2942 return false;
2945 /* Return true if T is function-invariant. */
2947 static bool
2948 tree_invariant_p (tree t)
2950 tree inner = skip_simple_arithmetic (t);
2951 return tree_invariant_p_1 (inner);
2954 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
2955 Do this to any expression which may be used in more than one place,
2956 but must be evaluated only once.
2958 Normally, expand_expr would reevaluate the expression each time.
2959 Calling save_expr produces something that is evaluated and recorded
2960 the first time expand_expr is called on it. Subsequent calls to
2961 expand_expr just reuse the recorded value.
2963 The call to expand_expr that generates code that actually computes
2964 the value is the first call *at compile time*. Subsequent calls
2965 *at compile time* generate code to use the saved value.
2966 This produces correct result provided that *at run time* control
2967 always flows through the insns made by the first expand_expr
2968 before reaching the other places where the save_expr was evaluated.
2969 You, the caller of save_expr, must make sure this is so.
2971 Constants, and certain read-only nodes, are returned with no
2972 SAVE_EXPR because that is safe. Expressions containing placeholders
2973 are not touched; see tree.def for an explanation of what these
2974 are used for. */
2976 tree
2977 save_expr (tree expr)
2979 tree t = fold (expr);
2980 tree inner;
2982 /* If the tree evaluates to a constant, then we don't want to hide that
2983 fact (i.e. this allows further folding, and direct checks for constants).
2984 However, a read-only object that has side effects cannot be bypassed.
2985 Since it is no problem to reevaluate literals, we just return the
2986 literal node. */
2987 inner = skip_simple_arithmetic (t);
2988 if (TREE_CODE (inner) == ERROR_MARK)
2989 return inner;
2991 if (tree_invariant_p_1 (inner))
2992 return t;
2994 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
2995 it means that the size or offset of some field of an object depends on
2996 the value within another field.
2998 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
2999 and some variable since it would then need to be both evaluated once and
3000 evaluated more than once. Front-ends must assure this case cannot
3001 happen by surrounding any such subexpressions in their own SAVE_EXPR
3002 and forcing evaluation at the proper time. */
3003 if (contains_placeholder_p (inner))
3004 return t;
3006 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
3007 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
3009 /* This expression might be placed ahead of a jump to ensure that the
3010 value was computed on both sides of the jump. So make sure it isn't
3011 eliminated as dead. */
3012 TREE_SIDE_EFFECTS (t) = 1;
3013 return t;
3016 /* Look inside EXPR into any simple arithmetic operations. Return the
3017 outermost non-arithmetic or non-invariant node. */
3019 tree
3020 skip_simple_arithmetic (tree expr)
3022 /* We don't care about whether this can be used as an lvalue in this
3023 context. */
3024 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3025 expr = TREE_OPERAND (expr, 0);
3027 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
3028 a constant, it will be more efficient to not make another SAVE_EXPR since
3029 it will allow better simplification and GCSE will be able to merge the
3030 computations if they actually occur. */
3031 while (true)
3033 if (UNARY_CLASS_P (expr))
3034 expr = TREE_OPERAND (expr, 0);
3035 else if (BINARY_CLASS_P (expr))
3037 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
3038 expr = TREE_OPERAND (expr, 0);
3039 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
3040 expr = TREE_OPERAND (expr, 1);
3041 else
3042 break;
3044 else
3045 break;
3048 return expr;
3051 /* Look inside EXPR into simple arithmetic operations involving constants.
3052 Return the outermost non-arithmetic or non-constant node. */
3054 tree
3055 skip_simple_constant_arithmetic (tree expr)
3057 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3058 expr = TREE_OPERAND (expr, 0);
3060 while (true)
3062 if (UNARY_CLASS_P (expr))
3063 expr = TREE_OPERAND (expr, 0);
3064 else if (BINARY_CLASS_P (expr))
3066 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3067 expr = TREE_OPERAND (expr, 0);
3068 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3069 expr = TREE_OPERAND (expr, 1);
3070 else
3071 break;
3073 else
3074 break;
3077 return expr;
3080 /* Return which tree structure is used by T. */
3082 enum tree_node_structure_enum
3083 tree_node_structure (const_tree t)
3085 const enum tree_code code = TREE_CODE (t);
3086 return tree_node_structure_for_code (code);
3089 /* Set various status flags when building a CALL_EXPR object T. */
3091 static void
3092 process_call_operands (tree t)
3094 bool side_effects = TREE_SIDE_EFFECTS (t);
3095 bool read_only = false;
3096 int i = call_expr_flags (t);
3098 /* Calls have side-effects, except those to const or pure functions. */
3099 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
3100 side_effects = true;
3101 /* Propagate TREE_READONLY of arguments for const functions. */
3102 if (i & ECF_CONST)
3103 read_only = true;
3105 if (!side_effects || read_only)
3106 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
3108 tree op = TREE_OPERAND (t, i);
3109 if (op && TREE_SIDE_EFFECTS (op))
3110 side_effects = true;
3111 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
3112 read_only = false;
3115 TREE_SIDE_EFFECTS (t) = side_effects;
3116 TREE_READONLY (t) = read_only;
3119 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
3120 size or offset that depends on a field within a record. */
3122 bool
3123 contains_placeholder_p (const_tree exp)
3125 enum tree_code code;
3127 if (!exp)
3128 return 0;
3130 code = TREE_CODE (exp);
3131 if (code == PLACEHOLDER_EXPR)
3132 return 1;
3134 switch (TREE_CODE_CLASS (code))
3136 case tcc_reference:
3137 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
3138 position computations since they will be converted into a
3139 WITH_RECORD_EXPR involving the reference, which will assume
3140 here will be valid. */
3141 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3143 case tcc_exceptional:
3144 if (code == TREE_LIST)
3145 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
3146 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
3147 break;
3149 case tcc_unary:
3150 case tcc_binary:
3151 case tcc_comparison:
3152 case tcc_expression:
3153 switch (code)
3155 case COMPOUND_EXPR:
3156 /* Ignoring the first operand isn't quite right, but works best. */
3157 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
3159 case COND_EXPR:
3160 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3161 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
3162 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
3164 case SAVE_EXPR:
3165 /* The save_expr function never wraps anything containing
3166 a PLACEHOLDER_EXPR. */
3167 return 0;
3169 default:
3170 break;
3173 switch (TREE_CODE_LENGTH (code))
3175 case 1:
3176 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3177 case 2:
3178 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3179 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
3180 default:
3181 return 0;
3184 case tcc_vl_exp:
3185 switch (code)
3187 case CALL_EXPR:
3189 const_tree arg;
3190 const_call_expr_arg_iterator iter;
3191 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
3192 if (CONTAINS_PLACEHOLDER_P (arg))
3193 return 1;
3194 return 0;
3196 default:
3197 return 0;
3200 default:
3201 return 0;
3203 return 0;
3206 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3207 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3208 field positions. */
3210 static bool
3211 type_contains_placeholder_1 (const_tree type)
3213 /* If the size contains a placeholder or the parent type (component type in
3214 the case of arrays) type involves a placeholder, this type does. */
3215 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3216 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3217 || (!POINTER_TYPE_P (type)
3218 && TREE_TYPE (type)
3219 && type_contains_placeholder_p (TREE_TYPE (type))))
3220 return true;
3222 /* Now do type-specific checks. Note that the last part of the check above
3223 greatly limits what we have to do below. */
3224 switch (TREE_CODE (type))
3226 case VOID_TYPE:
3227 case COMPLEX_TYPE:
3228 case ENUMERAL_TYPE:
3229 case BOOLEAN_TYPE:
3230 case POINTER_TYPE:
3231 case OFFSET_TYPE:
3232 case REFERENCE_TYPE:
3233 case METHOD_TYPE:
3234 case FUNCTION_TYPE:
3235 case VECTOR_TYPE:
3236 case NULLPTR_TYPE:
3237 return false;
3239 case INTEGER_TYPE:
3240 case REAL_TYPE:
3241 case FIXED_POINT_TYPE:
3242 /* Here we just check the bounds. */
3243 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3244 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3246 case ARRAY_TYPE:
3247 /* We have already checked the component type above, so just check the
3248 domain type. */
3249 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3251 case RECORD_TYPE:
3252 case UNION_TYPE:
3253 case QUAL_UNION_TYPE:
3255 tree field;
3257 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3258 if (TREE_CODE (field) == FIELD_DECL
3259 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3260 || (TREE_CODE (type) == QUAL_UNION_TYPE
3261 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3262 || type_contains_placeholder_p (TREE_TYPE (field))))
3263 return true;
3265 return false;
3268 default:
3269 gcc_unreachable ();
3273 /* Wrapper around above function used to cache its result. */
3275 bool
3276 type_contains_placeholder_p (tree type)
3278 bool result;
3280 /* If the contains_placeholder_bits field has been initialized,
3281 then we know the answer. */
3282 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3283 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3285 /* Indicate that we've seen this type node, and the answer is false.
3286 This is what we want to return if we run into recursion via fields. */
3287 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3289 /* Compute the real value. */
3290 result = type_contains_placeholder_1 (type);
3292 /* Store the real value. */
3293 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3295 return result;
3298 /* Push tree EXP onto vector QUEUE if it is not already present. */
3300 static void
3301 push_without_duplicates (tree exp, vec<tree> *queue)
3303 unsigned int i;
3304 tree iter;
3306 FOR_EACH_VEC_ELT (*queue, i, iter)
3307 if (simple_cst_equal (iter, exp) == 1)
3308 break;
3310 if (!iter)
3311 queue->safe_push (exp);
3314 /* Given a tree EXP, find all occurrences of references to fields
3315 in a PLACEHOLDER_EXPR and place them in vector REFS without
3316 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3317 we assume here that EXP contains only arithmetic expressions
3318 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3319 argument list. */
3321 void
3322 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3324 enum tree_code code = TREE_CODE (exp);
3325 tree inner;
3326 int i;
3328 /* We handle TREE_LIST and COMPONENT_REF separately. */
3329 if (code == TREE_LIST)
3331 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3332 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3334 else if (code == COMPONENT_REF)
3336 for (inner = TREE_OPERAND (exp, 0);
3337 REFERENCE_CLASS_P (inner);
3338 inner = TREE_OPERAND (inner, 0))
3341 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3342 push_without_duplicates (exp, refs);
3343 else
3344 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3346 else
3347 switch (TREE_CODE_CLASS (code))
3349 case tcc_constant:
3350 break;
3352 case tcc_declaration:
3353 /* Variables allocated to static storage can stay. */
3354 if (!TREE_STATIC (exp))
3355 push_without_duplicates (exp, refs);
3356 break;
3358 case tcc_expression:
3359 /* This is the pattern built in ada/make_aligning_type. */
3360 if (code == ADDR_EXPR
3361 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3363 push_without_duplicates (exp, refs);
3364 break;
3367 /* Fall through... */
3369 case tcc_exceptional:
3370 case tcc_unary:
3371 case tcc_binary:
3372 case tcc_comparison:
3373 case tcc_reference:
3374 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3375 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3376 break;
3378 case tcc_vl_exp:
3379 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3380 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3381 break;
3383 default:
3384 gcc_unreachable ();
3388 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3389 return a tree with all occurrences of references to F in a
3390 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3391 CONST_DECLs. Note that we assume here that EXP contains only
3392 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3393 occurring only in their argument list. */
3395 tree
3396 substitute_in_expr (tree exp, tree f, tree r)
3398 enum tree_code code = TREE_CODE (exp);
3399 tree op0, op1, op2, op3;
3400 tree new_tree;
3402 /* We handle TREE_LIST and COMPONENT_REF separately. */
3403 if (code == TREE_LIST)
3405 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3406 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3407 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3408 return exp;
3410 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3412 else if (code == COMPONENT_REF)
3414 tree inner;
3416 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3417 and it is the right field, replace it with R. */
3418 for (inner = TREE_OPERAND (exp, 0);
3419 REFERENCE_CLASS_P (inner);
3420 inner = TREE_OPERAND (inner, 0))
3423 /* The field. */
3424 op1 = TREE_OPERAND (exp, 1);
3426 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3427 return r;
3429 /* If this expression hasn't been completed let, leave it alone. */
3430 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3431 return exp;
3433 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3434 if (op0 == TREE_OPERAND (exp, 0))
3435 return exp;
3437 new_tree
3438 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3440 else
3441 switch (TREE_CODE_CLASS (code))
3443 case tcc_constant:
3444 return exp;
3446 case tcc_declaration:
3447 if (exp == f)
3448 return r;
3449 else
3450 return exp;
3452 case tcc_expression:
3453 if (exp == f)
3454 return r;
3456 /* Fall through... */
3458 case tcc_exceptional:
3459 case tcc_unary:
3460 case tcc_binary:
3461 case tcc_comparison:
3462 case tcc_reference:
3463 switch (TREE_CODE_LENGTH (code))
3465 case 0:
3466 return exp;
3468 case 1:
3469 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3470 if (op0 == TREE_OPERAND (exp, 0))
3471 return exp;
3473 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3474 break;
3476 case 2:
3477 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3478 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3480 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3481 return exp;
3483 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3484 break;
3486 case 3:
3487 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3488 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3489 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3491 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3492 && op2 == TREE_OPERAND (exp, 2))
3493 return exp;
3495 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3496 break;
3498 case 4:
3499 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3500 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3501 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3502 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3504 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3505 && op2 == TREE_OPERAND (exp, 2)
3506 && op3 == TREE_OPERAND (exp, 3))
3507 return exp;
3509 new_tree
3510 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3511 break;
3513 default:
3514 gcc_unreachable ();
3516 break;
3518 case tcc_vl_exp:
3520 int i;
3522 new_tree = NULL_TREE;
3524 /* If we are trying to replace F with a constant, inline back
3525 functions which do nothing else than computing a value from
3526 the arguments they are passed. This makes it possible to
3527 fold partially or entirely the replacement expression. */
3528 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3530 tree t = maybe_inline_call_in_expr (exp);
3531 if (t)
3532 return SUBSTITUTE_IN_EXPR (t, f, r);
3535 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3537 tree op = TREE_OPERAND (exp, i);
3538 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3539 if (new_op != op)
3541 if (!new_tree)
3542 new_tree = copy_node (exp);
3543 TREE_OPERAND (new_tree, i) = new_op;
3547 if (new_tree)
3549 new_tree = fold (new_tree);
3550 if (TREE_CODE (new_tree) == CALL_EXPR)
3551 process_call_operands (new_tree);
3553 else
3554 return exp;
3556 break;
3558 default:
3559 gcc_unreachable ();
3562 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3564 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3565 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3567 return new_tree;
3570 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3571 for it within OBJ, a tree that is an object or a chain of references. */
3573 tree
3574 substitute_placeholder_in_expr (tree exp, tree obj)
3576 enum tree_code code = TREE_CODE (exp);
3577 tree op0, op1, op2, op3;
3578 tree new_tree;
3580 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3581 in the chain of OBJ. */
3582 if (code == PLACEHOLDER_EXPR)
3584 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3585 tree elt;
3587 for (elt = obj; elt != 0;
3588 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3589 || TREE_CODE (elt) == COND_EXPR)
3590 ? TREE_OPERAND (elt, 1)
3591 : (REFERENCE_CLASS_P (elt)
3592 || UNARY_CLASS_P (elt)
3593 || BINARY_CLASS_P (elt)
3594 || VL_EXP_CLASS_P (elt)
3595 || EXPRESSION_CLASS_P (elt))
3596 ? TREE_OPERAND (elt, 0) : 0))
3597 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3598 return elt;
3600 for (elt = obj; elt != 0;
3601 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3602 || TREE_CODE (elt) == COND_EXPR)
3603 ? TREE_OPERAND (elt, 1)
3604 : (REFERENCE_CLASS_P (elt)
3605 || UNARY_CLASS_P (elt)
3606 || BINARY_CLASS_P (elt)
3607 || VL_EXP_CLASS_P (elt)
3608 || EXPRESSION_CLASS_P (elt))
3609 ? TREE_OPERAND (elt, 0) : 0))
3610 if (POINTER_TYPE_P (TREE_TYPE (elt))
3611 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3612 == need_type))
3613 return fold_build1 (INDIRECT_REF, need_type, elt);
3615 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3616 survives until RTL generation, there will be an error. */
3617 return exp;
3620 /* TREE_LIST is special because we need to look at TREE_VALUE
3621 and TREE_CHAIN, not TREE_OPERANDS. */
3622 else if (code == TREE_LIST)
3624 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3625 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3626 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3627 return exp;
3629 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3631 else
3632 switch (TREE_CODE_CLASS (code))
3634 case tcc_constant:
3635 case tcc_declaration:
3636 return exp;
3638 case tcc_exceptional:
3639 case tcc_unary:
3640 case tcc_binary:
3641 case tcc_comparison:
3642 case tcc_expression:
3643 case tcc_reference:
3644 case tcc_statement:
3645 switch (TREE_CODE_LENGTH (code))
3647 case 0:
3648 return exp;
3650 case 1:
3651 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3652 if (op0 == TREE_OPERAND (exp, 0))
3653 return exp;
3655 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3656 break;
3658 case 2:
3659 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3660 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3662 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3663 return exp;
3665 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3666 break;
3668 case 3:
3669 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3670 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3671 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3673 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3674 && op2 == TREE_OPERAND (exp, 2))
3675 return exp;
3677 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3678 break;
3680 case 4:
3681 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3682 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3683 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3684 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3686 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3687 && op2 == TREE_OPERAND (exp, 2)
3688 && op3 == TREE_OPERAND (exp, 3))
3689 return exp;
3691 new_tree
3692 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3693 break;
3695 default:
3696 gcc_unreachable ();
3698 break;
3700 case tcc_vl_exp:
3702 int i;
3704 new_tree = NULL_TREE;
3706 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3708 tree op = TREE_OPERAND (exp, i);
3709 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
3710 if (new_op != op)
3712 if (!new_tree)
3713 new_tree = copy_node (exp);
3714 TREE_OPERAND (new_tree, i) = new_op;
3718 if (new_tree)
3720 new_tree = fold (new_tree);
3721 if (TREE_CODE (new_tree) == CALL_EXPR)
3722 process_call_operands (new_tree);
3724 else
3725 return exp;
3727 break;
3729 default:
3730 gcc_unreachable ();
3733 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3735 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3736 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3738 return new_tree;
3741 /* Stabilize a reference so that we can use it any number of times
3742 without causing its operands to be evaluated more than once.
3743 Returns the stabilized reference. This works by means of save_expr,
3744 so see the caveats in the comments about save_expr.
3746 Also allows conversion expressions whose operands are references.
3747 Any other kind of expression is returned unchanged. */
3749 tree
3750 stabilize_reference (tree ref)
3752 tree result;
3753 enum tree_code code = TREE_CODE (ref);
3755 switch (code)
3757 case VAR_DECL:
3758 case PARM_DECL:
3759 case RESULT_DECL:
3760 /* No action is needed in this case. */
3761 return ref;
3763 CASE_CONVERT:
3764 case FLOAT_EXPR:
3765 case FIX_TRUNC_EXPR:
3766 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
3767 break;
3769 case INDIRECT_REF:
3770 result = build_nt (INDIRECT_REF,
3771 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
3772 break;
3774 case COMPONENT_REF:
3775 result = build_nt (COMPONENT_REF,
3776 stabilize_reference (TREE_OPERAND (ref, 0)),
3777 TREE_OPERAND (ref, 1), NULL_TREE);
3778 break;
3780 case BIT_FIELD_REF:
3781 result = build_nt (BIT_FIELD_REF,
3782 stabilize_reference (TREE_OPERAND (ref, 0)),
3783 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
3784 break;
3786 case ARRAY_REF:
3787 result = build_nt (ARRAY_REF,
3788 stabilize_reference (TREE_OPERAND (ref, 0)),
3789 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3790 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3791 break;
3793 case ARRAY_RANGE_REF:
3794 result = build_nt (ARRAY_RANGE_REF,
3795 stabilize_reference (TREE_OPERAND (ref, 0)),
3796 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3797 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3798 break;
3800 case COMPOUND_EXPR:
3801 /* We cannot wrap the first expression in a SAVE_EXPR, as then
3802 it wouldn't be ignored. This matters when dealing with
3803 volatiles. */
3804 return stabilize_reference_1 (ref);
3806 /* If arg isn't a kind of lvalue we recognize, make no change.
3807 Caller should recognize the error for an invalid lvalue. */
3808 default:
3809 return ref;
3811 case ERROR_MARK:
3812 return error_mark_node;
3815 TREE_TYPE (result) = TREE_TYPE (ref);
3816 TREE_READONLY (result) = TREE_READONLY (ref);
3817 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
3818 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
3820 return result;
3823 /* Subroutine of stabilize_reference; this is called for subtrees of
3824 references. Any expression with side-effects must be put in a SAVE_EXPR
3825 to ensure that it is only evaluated once.
3827 We don't put SAVE_EXPR nodes around everything, because assigning very
3828 simple expressions to temporaries causes us to miss good opportunities
3829 for optimizations. Among other things, the opportunity to fold in the
3830 addition of a constant into an addressing mode often gets lost, e.g.
3831 "y[i+1] += x;". In general, we take the approach that we should not make
3832 an assignment unless we are forced into it - i.e., that any non-side effect
3833 operator should be allowed, and that cse should take care of coalescing
3834 multiple utterances of the same expression should that prove fruitful. */
3836 tree
3837 stabilize_reference_1 (tree e)
3839 tree result;
3840 enum tree_code code = TREE_CODE (e);
3842 /* We cannot ignore const expressions because it might be a reference
3843 to a const array but whose index contains side-effects. But we can
3844 ignore things that are actual constant or that already have been
3845 handled by this function. */
3847 if (tree_invariant_p (e))
3848 return e;
3850 switch (TREE_CODE_CLASS (code))
3852 case tcc_exceptional:
3853 case tcc_type:
3854 case tcc_declaration:
3855 case tcc_comparison:
3856 case tcc_statement:
3857 case tcc_expression:
3858 case tcc_reference:
3859 case tcc_vl_exp:
3860 /* If the expression has side-effects, then encase it in a SAVE_EXPR
3861 so that it will only be evaluated once. */
3862 /* The reference (r) and comparison (<) classes could be handled as
3863 below, but it is generally faster to only evaluate them once. */
3864 if (TREE_SIDE_EFFECTS (e))
3865 return save_expr (e);
3866 return e;
3868 case tcc_constant:
3869 /* Constants need no processing. In fact, we should never reach
3870 here. */
3871 return e;
3873 case tcc_binary:
3874 /* Division is slow and tends to be compiled with jumps,
3875 especially the division by powers of 2 that is often
3876 found inside of an array reference. So do it just once. */
3877 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3878 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3879 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3880 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3881 return save_expr (e);
3882 /* Recursively stabilize each operand. */
3883 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3884 stabilize_reference_1 (TREE_OPERAND (e, 1)));
3885 break;
3887 case tcc_unary:
3888 /* Recursively stabilize each operand. */
3889 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3890 break;
3892 default:
3893 gcc_unreachable ();
3896 TREE_TYPE (result) = TREE_TYPE (e);
3897 TREE_READONLY (result) = TREE_READONLY (e);
3898 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3899 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3901 return result;
3904 /* Low-level constructors for expressions. */
3906 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
3907 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
3909 void
3910 recompute_tree_invariant_for_addr_expr (tree t)
3912 tree node;
3913 bool tc = true, se = false;
3915 /* We started out assuming this address is both invariant and constant, but
3916 does not have side effects. Now go down any handled components and see if
3917 any of them involve offsets that are either non-constant or non-invariant.
3918 Also check for side-effects.
3920 ??? Note that this code makes no attempt to deal with the case where
3921 taking the address of something causes a copy due to misalignment. */
3923 #define UPDATE_FLAGS(NODE) \
3924 do { tree _node = (NODE); \
3925 if (_node && !TREE_CONSTANT (_node)) tc = false; \
3926 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
3928 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
3929 node = TREE_OPERAND (node, 0))
3931 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
3932 array reference (probably made temporarily by the G++ front end),
3933 so ignore all the operands. */
3934 if ((TREE_CODE (node) == ARRAY_REF
3935 || TREE_CODE (node) == ARRAY_RANGE_REF)
3936 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
3938 UPDATE_FLAGS (TREE_OPERAND (node, 1));
3939 if (TREE_OPERAND (node, 2))
3940 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3941 if (TREE_OPERAND (node, 3))
3942 UPDATE_FLAGS (TREE_OPERAND (node, 3));
3944 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
3945 FIELD_DECL, apparently. The G++ front end can put something else
3946 there, at least temporarily. */
3947 else if (TREE_CODE (node) == COMPONENT_REF
3948 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
3950 if (TREE_OPERAND (node, 2))
3951 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3955 node = lang_hooks.expr_to_decl (node, &tc, &se);
3957 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
3958 the address, since &(*a)->b is a form of addition. If it's a constant, the
3959 address is constant too. If it's a decl, its address is constant if the
3960 decl is static. Everything else is not constant and, furthermore,
3961 taking the address of a volatile variable is not volatile. */
3962 if (TREE_CODE (node) == INDIRECT_REF
3963 || TREE_CODE (node) == MEM_REF)
3964 UPDATE_FLAGS (TREE_OPERAND (node, 0));
3965 else if (CONSTANT_CLASS_P (node))
3967 else if (DECL_P (node))
3968 tc &= (staticp (node) != NULL_TREE);
3969 else
3971 tc = false;
3972 se |= TREE_SIDE_EFFECTS (node);
3976 TREE_CONSTANT (t) = tc;
3977 TREE_SIDE_EFFECTS (t) = se;
3978 #undef UPDATE_FLAGS
3981 /* Build an expression of code CODE, data type TYPE, and operands as
3982 specified. Expressions and reference nodes can be created this way.
3983 Constants, decls, types and misc nodes cannot be.
3985 We define 5 non-variadic functions, from 0 to 4 arguments. This is
3986 enough for all extant tree codes. */
3988 tree
3989 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
3991 tree t;
3993 gcc_assert (TREE_CODE_LENGTH (code) == 0);
3995 t = make_node_stat (code PASS_MEM_STAT);
3996 TREE_TYPE (t) = tt;
3998 return t;
4001 tree
4002 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4004 int length = sizeof (struct tree_exp);
4005 tree t;
4007 record_node_allocation_statistics (code, length);
4009 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4011 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4013 memset (t, 0, sizeof (struct tree_common));
4015 TREE_SET_CODE (t, code);
4017 TREE_TYPE (t) = type;
4018 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4019 TREE_OPERAND (t, 0) = node;
4020 if (node && !TYPE_P (node))
4022 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4023 TREE_READONLY (t) = TREE_READONLY (node);
4026 if (TREE_CODE_CLASS (code) == tcc_statement)
4027 TREE_SIDE_EFFECTS (t) = 1;
4028 else switch (code)
4030 case VA_ARG_EXPR:
4031 /* All of these have side-effects, no matter what their
4032 operands are. */
4033 TREE_SIDE_EFFECTS (t) = 1;
4034 TREE_READONLY (t) = 0;
4035 break;
4037 case INDIRECT_REF:
4038 /* Whether a dereference is readonly has nothing to do with whether
4039 its operand is readonly. */
4040 TREE_READONLY (t) = 0;
4041 break;
4043 case ADDR_EXPR:
4044 if (node)
4045 recompute_tree_invariant_for_addr_expr (t);
4046 break;
4048 default:
4049 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4050 && node && !TYPE_P (node)
4051 && TREE_CONSTANT (node))
4052 TREE_CONSTANT (t) = 1;
4053 if (TREE_CODE_CLASS (code) == tcc_reference
4054 && node && TREE_THIS_VOLATILE (node))
4055 TREE_THIS_VOLATILE (t) = 1;
4056 break;
4059 return t;
4062 #define PROCESS_ARG(N) \
4063 do { \
4064 TREE_OPERAND (t, N) = arg##N; \
4065 if (arg##N &&!TYPE_P (arg##N)) \
4067 if (TREE_SIDE_EFFECTS (arg##N)) \
4068 side_effects = 1; \
4069 if (!TREE_READONLY (arg##N) \
4070 && !CONSTANT_CLASS_P (arg##N)) \
4071 (void) (read_only = 0); \
4072 if (!TREE_CONSTANT (arg##N)) \
4073 (void) (constant = 0); \
4075 } while (0)
4077 tree
4078 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
4080 bool constant, read_only, side_effects;
4081 tree t;
4083 gcc_assert (TREE_CODE_LENGTH (code) == 2);
4085 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
4086 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
4087 /* When sizetype precision doesn't match that of pointers
4088 we need to be able to build explicit extensions or truncations
4089 of the offset argument. */
4090 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
4091 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
4092 && TREE_CODE (arg1) == INTEGER_CST);
4094 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
4095 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
4096 && ptrofftype_p (TREE_TYPE (arg1)));
4098 t = make_node_stat (code PASS_MEM_STAT);
4099 TREE_TYPE (t) = tt;
4101 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
4102 result based on those same flags for the arguments. But if the
4103 arguments aren't really even `tree' expressions, we shouldn't be trying
4104 to do this. */
4106 /* Expressions without side effects may be constant if their
4107 arguments are as well. */
4108 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4109 || TREE_CODE_CLASS (code) == tcc_binary);
4110 read_only = 1;
4111 side_effects = TREE_SIDE_EFFECTS (t);
4113 PROCESS_ARG(0);
4114 PROCESS_ARG(1);
4116 TREE_READONLY (t) = read_only;
4117 TREE_CONSTANT (t) = constant;
4118 TREE_SIDE_EFFECTS (t) = side_effects;
4119 TREE_THIS_VOLATILE (t)
4120 = (TREE_CODE_CLASS (code) == tcc_reference
4121 && arg0 && TREE_THIS_VOLATILE (arg0));
4123 return t;
4127 tree
4128 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4129 tree arg2 MEM_STAT_DECL)
4131 bool constant, read_only, side_effects;
4132 tree t;
4134 gcc_assert (TREE_CODE_LENGTH (code) == 3);
4135 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4137 t = make_node_stat (code PASS_MEM_STAT);
4138 TREE_TYPE (t) = tt;
4140 read_only = 1;
4142 /* As a special exception, if COND_EXPR has NULL branches, we
4143 assume that it is a gimple statement and always consider
4144 it to have side effects. */
4145 if (code == COND_EXPR
4146 && tt == void_type_node
4147 && arg1 == NULL_TREE
4148 && arg2 == NULL_TREE)
4149 side_effects = true;
4150 else
4151 side_effects = TREE_SIDE_EFFECTS (t);
4153 PROCESS_ARG(0);
4154 PROCESS_ARG(1);
4155 PROCESS_ARG(2);
4157 if (code == COND_EXPR)
4158 TREE_READONLY (t) = read_only;
4160 TREE_SIDE_EFFECTS (t) = side_effects;
4161 TREE_THIS_VOLATILE (t)
4162 = (TREE_CODE_CLASS (code) == tcc_reference
4163 && arg0 && TREE_THIS_VOLATILE (arg0));
4165 return t;
4168 tree
4169 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4170 tree arg2, tree arg3 MEM_STAT_DECL)
4172 bool constant, read_only, side_effects;
4173 tree t;
4175 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4177 t = make_node_stat (code PASS_MEM_STAT);
4178 TREE_TYPE (t) = tt;
4180 side_effects = TREE_SIDE_EFFECTS (t);
4182 PROCESS_ARG(0);
4183 PROCESS_ARG(1);
4184 PROCESS_ARG(2);
4185 PROCESS_ARG(3);
4187 TREE_SIDE_EFFECTS (t) = side_effects;
4188 TREE_THIS_VOLATILE (t)
4189 = (TREE_CODE_CLASS (code) == tcc_reference
4190 && arg0 && TREE_THIS_VOLATILE (arg0));
4192 return t;
4195 tree
4196 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4197 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4199 bool constant, read_only, side_effects;
4200 tree t;
4202 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4204 t = make_node_stat (code PASS_MEM_STAT);
4205 TREE_TYPE (t) = tt;
4207 side_effects = TREE_SIDE_EFFECTS (t);
4209 PROCESS_ARG(0);
4210 PROCESS_ARG(1);
4211 PROCESS_ARG(2);
4212 PROCESS_ARG(3);
4213 PROCESS_ARG(4);
4215 TREE_SIDE_EFFECTS (t) = side_effects;
4216 TREE_THIS_VOLATILE (t)
4217 = (TREE_CODE_CLASS (code) == tcc_reference
4218 && arg0 && TREE_THIS_VOLATILE (arg0));
4220 return t;
4223 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4224 on the pointer PTR. */
4226 tree
4227 build_simple_mem_ref_loc (location_t loc, tree ptr)
4229 HOST_WIDE_INT offset = 0;
4230 tree ptype = TREE_TYPE (ptr);
4231 tree tem;
4232 /* For convenience allow addresses that collapse to a simple base
4233 and offset. */
4234 if (TREE_CODE (ptr) == ADDR_EXPR
4235 && (handled_component_p (TREE_OPERAND (ptr, 0))
4236 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4238 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4239 gcc_assert (ptr);
4240 ptr = build_fold_addr_expr (ptr);
4241 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4243 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4244 ptr, build_int_cst (ptype, offset));
4245 SET_EXPR_LOCATION (tem, loc);
4246 return tem;
4249 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4251 double_int
4252 mem_ref_offset (const_tree t)
4254 tree toff = TREE_OPERAND (t, 1);
4255 return tree_to_double_int (toff).sext (TYPE_PRECISION (TREE_TYPE (toff)));
4258 /* Return the pointer-type relevant for TBAA purposes from the
4259 gimple memory reference tree T. This is the type to be used for
4260 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T. */
4262 tree
4263 reference_alias_ptr_type (const_tree t)
4265 const_tree base = t;
4266 while (handled_component_p (base))
4267 base = TREE_OPERAND (base, 0);
4268 if (TREE_CODE (base) == MEM_REF)
4269 return TREE_TYPE (TREE_OPERAND (base, 1));
4270 else if (TREE_CODE (base) == TARGET_MEM_REF)
4271 return TREE_TYPE (TMR_OFFSET (base));
4272 else
4273 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (base)));
4276 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4277 offsetted by OFFSET units. */
4279 tree
4280 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4282 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4283 build_fold_addr_expr (base),
4284 build_int_cst (ptr_type_node, offset));
4285 tree addr = build1 (ADDR_EXPR, type, ref);
4286 recompute_tree_invariant_for_addr_expr (addr);
4287 return addr;
4290 /* Similar except don't specify the TREE_TYPE
4291 and leave the TREE_SIDE_EFFECTS as 0.
4292 It is permissible for arguments to be null,
4293 or even garbage if their values do not matter. */
4295 tree
4296 build_nt (enum tree_code code, ...)
4298 tree t;
4299 int length;
4300 int i;
4301 va_list p;
4303 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4305 va_start (p, code);
4307 t = make_node (code);
4308 length = TREE_CODE_LENGTH (code);
4310 for (i = 0; i < length; i++)
4311 TREE_OPERAND (t, i) = va_arg (p, tree);
4313 va_end (p);
4314 return t;
4317 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4318 tree vec. */
4320 tree
4321 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4323 tree ret, t;
4324 unsigned int ix;
4326 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4327 CALL_EXPR_FN (ret) = fn;
4328 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4329 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4330 CALL_EXPR_ARG (ret, ix) = t;
4331 return ret;
4334 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4335 We do NOT enter this node in any sort of symbol table.
4337 LOC is the location of the decl.
4339 layout_decl is used to set up the decl's storage layout.
4340 Other slots are initialized to 0 or null pointers. */
4342 tree
4343 build_decl_stat (location_t loc, enum tree_code code, tree name,
4344 tree type MEM_STAT_DECL)
4346 tree t;
4348 t = make_node_stat (code PASS_MEM_STAT);
4349 DECL_SOURCE_LOCATION (t) = loc;
4351 /* if (type == error_mark_node)
4352 type = integer_type_node; */
4353 /* That is not done, deliberately, so that having error_mark_node
4354 as the type can suppress useless errors in the use of this variable. */
4356 DECL_NAME (t) = name;
4357 TREE_TYPE (t) = type;
4359 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4360 layout_decl (t, 0);
4362 return t;
4365 /* Builds and returns function declaration with NAME and TYPE. */
4367 tree
4368 build_fn_decl (const char *name, tree type)
4370 tree id = get_identifier (name);
4371 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4373 DECL_EXTERNAL (decl) = 1;
4374 TREE_PUBLIC (decl) = 1;
4375 DECL_ARTIFICIAL (decl) = 1;
4376 TREE_NOTHROW (decl) = 1;
4378 return decl;
4381 vec<tree, va_gc> *all_translation_units;
4383 /* Builds a new translation-unit decl with name NAME, queues it in the
4384 global list of translation-unit decls and returns it. */
4386 tree
4387 build_translation_unit_decl (tree name)
4389 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4390 name, NULL_TREE);
4391 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4392 vec_safe_push (all_translation_units, tu);
4393 return tu;
4397 /* BLOCK nodes are used to represent the structure of binding contours
4398 and declarations, once those contours have been exited and their contents
4399 compiled. This information is used for outputting debugging info. */
4401 tree
4402 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4404 tree block = make_node (BLOCK);
4406 BLOCK_VARS (block) = vars;
4407 BLOCK_SUBBLOCKS (block) = subblocks;
4408 BLOCK_SUPERCONTEXT (block) = supercontext;
4409 BLOCK_CHAIN (block) = chain;
4410 return block;
4414 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4416 LOC is the location to use in tree T. */
4418 void
4419 protected_set_expr_location (tree t, location_t loc)
4421 if (t && CAN_HAVE_LOCATION_P (t))
4422 SET_EXPR_LOCATION (t, loc);
4425 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4426 is ATTRIBUTE. */
4428 tree
4429 build_decl_attribute_variant (tree ddecl, tree attribute)
4431 DECL_ATTRIBUTES (ddecl) = attribute;
4432 return ddecl;
4435 /* Borrowed from hashtab.c iterative_hash implementation. */
4436 #define mix(a,b,c) \
4438 a -= b; a -= c; a ^= (c>>13); \
4439 b -= c; b -= a; b ^= (a<< 8); \
4440 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
4441 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
4442 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
4443 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
4444 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
4445 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
4446 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
4450 /* Produce good hash value combining VAL and VAL2. */
4451 hashval_t
4452 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
4454 /* the golden ratio; an arbitrary value. */
4455 hashval_t a = 0x9e3779b9;
4457 mix (a, val, val2);
4458 return val2;
4461 /* Produce good hash value combining VAL and VAL2. */
4462 hashval_t
4463 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
4465 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
4466 return iterative_hash_hashval_t (val, val2);
4467 else
4469 hashval_t a = (hashval_t) val;
4470 /* Avoid warnings about shifting of more than the width of the type on
4471 hosts that won't execute this path. */
4472 int zero = 0;
4473 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
4474 mix (a, b, val2);
4475 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
4477 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
4478 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
4479 mix (a, b, val2);
4481 return val2;
4485 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4486 is ATTRIBUTE and its qualifiers are QUALS.
4488 Record such modified types already made so we don't make duplicates. */
4490 tree
4491 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4493 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4495 hashval_t hashcode = 0;
4496 tree ntype;
4497 enum tree_code code = TREE_CODE (ttype);
4499 /* Building a distinct copy of a tagged type is inappropriate; it
4500 causes breakage in code that expects there to be a one-to-one
4501 relationship between a struct and its fields.
4502 build_duplicate_type is another solution (as used in
4503 handle_transparent_union_attribute), but that doesn't play well
4504 with the stronger C++ type identity model. */
4505 if (TREE_CODE (ttype) == RECORD_TYPE
4506 || TREE_CODE (ttype) == UNION_TYPE
4507 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4508 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4510 warning (OPT_Wattributes,
4511 "ignoring attributes applied to %qT after definition",
4512 TYPE_MAIN_VARIANT (ttype));
4513 return build_qualified_type (ttype, quals);
4516 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4517 ntype = build_distinct_type_copy (ttype);
4519 TYPE_ATTRIBUTES (ntype) = attribute;
4521 hashcode = iterative_hash_object (code, hashcode);
4522 if (TREE_TYPE (ntype))
4523 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
4524 hashcode);
4525 hashcode = attribute_hash_list (attribute, hashcode);
4527 switch (TREE_CODE (ntype))
4529 case FUNCTION_TYPE:
4530 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
4531 break;
4532 case ARRAY_TYPE:
4533 if (TYPE_DOMAIN (ntype))
4534 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
4535 hashcode);
4536 break;
4537 case INTEGER_TYPE:
4538 hashcode = iterative_hash_object
4539 (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
4540 hashcode = iterative_hash_object
4541 (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
4542 break;
4543 case REAL_TYPE:
4544 case FIXED_POINT_TYPE:
4546 unsigned int precision = TYPE_PRECISION (ntype);
4547 hashcode = iterative_hash_object (precision, hashcode);
4549 break;
4550 default:
4551 break;
4554 ntype = type_hash_canon (hashcode, ntype);
4556 /* If the target-dependent attributes make NTYPE different from
4557 its canonical type, we will need to use structural equality
4558 checks for this type. */
4559 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4560 || !comp_type_attributes (ntype, ttype))
4561 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4562 else if (TYPE_CANONICAL (ntype) == ntype)
4563 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4565 ttype = build_qualified_type (ntype, quals);
4567 else if (TYPE_QUALS (ttype) != quals)
4568 ttype = build_qualified_type (ttype, quals);
4570 return ttype;
4573 /* Compare two attributes for their value identity. Return true if the
4574 attribute values are known to be equal; otherwise return false.
4577 static bool
4578 attribute_value_equal (const_tree attr1, const_tree attr2)
4580 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4581 return true;
4583 if (TREE_VALUE (attr1) != NULL_TREE
4584 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4585 && TREE_VALUE (attr2) != NULL
4586 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4587 return (simple_cst_list_equal (TREE_VALUE (attr1),
4588 TREE_VALUE (attr2)) == 1);
4590 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4593 /* Return 0 if the attributes for two types are incompatible, 1 if they
4594 are compatible, and 2 if they are nearly compatible (which causes a
4595 warning to be generated). */
4597 comp_type_attributes (const_tree type1, const_tree type2)
4599 const_tree a1 = TYPE_ATTRIBUTES (type1);
4600 const_tree a2 = TYPE_ATTRIBUTES (type2);
4601 const_tree a;
4603 if (a1 == a2)
4604 return 1;
4605 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4607 const struct attribute_spec *as;
4608 const_tree attr;
4610 as = lookup_attribute_spec (get_attribute_name (a));
4611 if (!as || as->affects_type_identity == false)
4612 continue;
4614 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4615 if (!attr || !attribute_value_equal (a, attr))
4616 break;
4618 if (!a)
4620 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
4622 const struct attribute_spec *as;
4624 as = lookup_attribute_spec (get_attribute_name (a));
4625 if (!as || as->affects_type_identity == false)
4626 continue;
4628 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
4629 break;
4630 /* We don't need to compare trees again, as we did this
4631 already in first loop. */
4633 /* All types - affecting identity - are equal, so
4634 there is no need to call target hook for comparison. */
4635 if (!a)
4636 return 1;
4638 /* As some type combinations - like default calling-convention - might
4639 be compatible, we have to call the target hook to get the final result. */
4640 return targetm.comp_type_attributes (type1, type2);
4643 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4644 is ATTRIBUTE.
4646 Record such modified types already made so we don't make duplicates. */
4648 tree
4649 build_type_attribute_variant (tree ttype, tree attribute)
4651 return build_type_attribute_qual_variant (ttype, attribute,
4652 TYPE_QUALS (ttype));
4656 /* Reset the expression *EXPR_P, a size or position.
4658 ??? We could reset all non-constant sizes or positions. But it's cheap
4659 enough to not do so and refrain from adding workarounds to dwarf2out.c.
4661 We need to reset self-referential sizes or positions because they cannot
4662 be gimplified and thus can contain a CALL_EXPR after the gimplification
4663 is finished, which will run afoul of LTO streaming. And they need to be
4664 reset to something essentially dummy but not constant, so as to preserve
4665 the properties of the object they are attached to. */
4667 static inline void
4668 free_lang_data_in_one_sizepos (tree *expr_p)
4670 tree expr = *expr_p;
4671 if (CONTAINS_PLACEHOLDER_P (expr))
4672 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
4676 /* Reset all the fields in a binfo node BINFO. We only keep
4677 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
4679 static void
4680 free_lang_data_in_binfo (tree binfo)
4682 unsigned i;
4683 tree t;
4685 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
4687 BINFO_VIRTUALS (binfo) = NULL_TREE;
4688 BINFO_BASE_ACCESSES (binfo) = NULL;
4689 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
4690 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
4692 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
4693 free_lang_data_in_binfo (t);
4697 /* Reset all language specific information still present in TYPE. */
4699 static void
4700 free_lang_data_in_type (tree type)
4702 gcc_assert (TYPE_P (type));
4704 /* Give the FE a chance to remove its own data first. */
4705 lang_hooks.free_lang_data (type);
4707 TREE_LANG_FLAG_0 (type) = 0;
4708 TREE_LANG_FLAG_1 (type) = 0;
4709 TREE_LANG_FLAG_2 (type) = 0;
4710 TREE_LANG_FLAG_3 (type) = 0;
4711 TREE_LANG_FLAG_4 (type) = 0;
4712 TREE_LANG_FLAG_5 (type) = 0;
4713 TREE_LANG_FLAG_6 (type) = 0;
4715 if (TREE_CODE (type) == FUNCTION_TYPE)
4717 /* Remove the const and volatile qualifiers from arguments. The
4718 C++ front end removes them, but the C front end does not,
4719 leading to false ODR violation errors when merging two
4720 instances of the same function signature compiled by
4721 different front ends. */
4722 tree p;
4724 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
4726 tree arg_type = TREE_VALUE (p);
4728 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
4730 int quals = TYPE_QUALS (arg_type)
4731 & ~TYPE_QUAL_CONST
4732 & ~TYPE_QUAL_VOLATILE;
4733 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
4734 free_lang_data_in_type (TREE_VALUE (p));
4739 /* Remove members that are not actually FIELD_DECLs from the field
4740 list of an aggregate. These occur in C++. */
4741 if (RECORD_OR_UNION_TYPE_P (type))
4743 tree prev, member;
4745 /* Note that TYPE_FIELDS can be shared across distinct
4746 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
4747 to be removed, we cannot set its TREE_CHAIN to NULL.
4748 Otherwise, we would not be able to find all the other fields
4749 in the other instances of this TREE_TYPE.
4751 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
4752 prev = NULL_TREE;
4753 member = TYPE_FIELDS (type);
4754 while (member)
4756 if (TREE_CODE (member) == FIELD_DECL
4757 || TREE_CODE (member) == TYPE_DECL)
4759 if (prev)
4760 TREE_CHAIN (prev) = member;
4761 else
4762 TYPE_FIELDS (type) = member;
4763 prev = member;
4766 member = TREE_CHAIN (member);
4769 if (prev)
4770 TREE_CHAIN (prev) = NULL_TREE;
4771 else
4772 TYPE_FIELDS (type) = NULL_TREE;
4774 TYPE_METHODS (type) = NULL_TREE;
4775 if (TYPE_BINFO (type))
4776 free_lang_data_in_binfo (TYPE_BINFO (type));
4778 else
4780 /* For non-aggregate types, clear out the language slot (which
4781 overloads TYPE_BINFO). */
4782 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
4784 if (INTEGRAL_TYPE_P (type)
4785 || SCALAR_FLOAT_TYPE_P (type)
4786 || FIXED_POINT_TYPE_P (type))
4788 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
4789 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
4793 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
4794 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
4796 if (TYPE_CONTEXT (type)
4797 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
4799 tree ctx = TYPE_CONTEXT (type);
4802 ctx = BLOCK_SUPERCONTEXT (ctx);
4804 while (ctx && TREE_CODE (ctx) == BLOCK);
4805 TYPE_CONTEXT (type) = ctx;
4810 /* Return true if DECL may need an assembler name to be set. */
4812 static inline bool
4813 need_assembler_name_p (tree decl)
4815 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
4816 if (TREE_CODE (decl) != FUNCTION_DECL
4817 && TREE_CODE (decl) != VAR_DECL)
4818 return false;
4820 /* If DECL already has its assembler name set, it does not need a
4821 new one. */
4822 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
4823 || DECL_ASSEMBLER_NAME_SET_P (decl))
4824 return false;
4826 /* Abstract decls do not need an assembler name. */
4827 if (DECL_ABSTRACT (decl))
4828 return false;
4830 /* For VAR_DECLs, only static, public and external symbols need an
4831 assembler name. */
4832 if (TREE_CODE (decl) == VAR_DECL
4833 && !TREE_STATIC (decl)
4834 && !TREE_PUBLIC (decl)
4835 && !DECL_EXTERNAL (decl))
4836 return false;
4838 if (TREE_CODE (decl) == FUNCTION_DECL)
4840 /* Do not set assembler name on builtins. Allow RTL expansion to
4841 decide whether to expand inline or via a regular call. */
4842 if (DECL_BUILT_IN (decl)
4843 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
4844 return false;
4846 /* Functions represented in the callgraph need an assembler name. */
4847 if (cgraph_get_node (decl) != NULL)
4848 return true;
4850 /* Unused and not public functions don't need an assembler name. */
4851 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
4852 return false;
4855 return true;
4859 /* Reset all language specific information still present in symbol
4860 DECL. */
4862 static void
4863 free_lang_data_in_decl (tree decl)
4865 gcc_assert (DECL_P (decl));
4867 /* Give the FE a chance to remove its own data first. */
4868 lang_hooks.free_lang_data (decl);
4870 TREE_LANG_FLAG_0 (decl) = 0;
4871 TREE_LANG_FLAG_1 (decl) = 0;
4872 TREE_LANG_FLAG_2 (decl) = 0;
4873 TREE_LANG_FLAG_3 (decl) = 0;
4874 TREE_LANG_FLAG_4 (decl) = 0;
4875 TREE_LANG_FLAG_5 (decl) = 0;
4876 TREE_LANG_FLAG_6 (decl) = 0;
4878 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
4879 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
4880 if (TREE_CODE (decl) == FIELD_DECL)
4882 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
4883 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
4884 DECL_QUALIFIER (decl) = NULL_TREE;
4887 if (TREE_CODE (decl) == FUNCTION_DECL)
4889 if (gimple_has_body_p (decl))
4891 tree t;
4893 /* If DECL has a gimple body, then the context for its
4894 arguments must be DECL. Otherwise, it doesn't really
4895 matter, as we will not be emitting any code for DECL. In
4896 general, there may be other instances of DECL created by
4897 the front end and since PARM_DECLs are generally shared,
4898 their DECL_CONTEXT changes as the replicas of DECL are
4899 created. The only time where DECL_CONTEXT is important
4900 is for the FUNCTION_DECLs that have a gimple body (since
4901 the PARM_DECL will be used in the function's body). */
4902 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
4903 DECL_CONTEXT (t) = decl;
4906 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
4907 At this point, it is not needed anymore. */
4908 DECL_SAVED_TREE (decl) = NULL_TREE;
4910 /* Clear the abstract origin if it refers to a method. Otherwise
4911 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
4912 origin will not be output correctly. */
4913 if (DECL_ABSTRACT_ORIGIN (decl)
4914 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
4915 && RECORD_OR_UNION_TYPE_P
4916 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
4917 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
4919 /* Sometimes the C++ frontend doesn't manage to transform a temporary
4920 DECL_VINDEX referring to itself into a vtable slot number as it
4921 should. Happens with functions that are copied and then forgotten
4922 about. Just clear it, it won't matter anymore. */
4923 if (DECL_VINDEX (decl) && !host_integerp (DECL_VINDEX (decl), 0))
4924 DECL_VINDEX (decl) = NULL_TREE;
4926 else if (TREE_CODE (decl) == VAR_DECL)
4928 if ((DECL_EXTERNAL (decl)
4929 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
4930 || (decl_function_context (decl) && !TREE_STATIC (decl)))
4931 DECL_INITIAL (decl) = NULL_TREE;
4933 else if (TREE_CODE (decl) == TYPE_DECL
4934 || TREE_CODE (decl) == FIELD_DECL)
4935 DECL_INITIAL (decl) = NULL_TREE;
4936 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
4937 && DECL_INITIAL (decl)
4938 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
4940 /* Strip builtins from the translation-unit BLOCK. We still have targets
4941 without builtin_decl_explicit support and also builtins are shared
4942 nodes and thus we can't use TREE_CHAIN in multiple lists. */
4943 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
4944 while (*nextp)
4946 tree var = *nextp;
4947 if (TREE_CODE (var) == FUNCTION_DECL
4948 && DECL_BUILT_IN (var))
4949 *nextp = TREE_CHAIN (var);
4950 else
4951 nextp = &TREE_CHAIN (var);
4957 /* Data used when collecting DECLs and TYPEs for language data removal. */
4959 struct free_lang_data_d
4961 /* Worklist to avoid excessive recursion. */
4962 vec<tree> worklist;
4964 /* Set of traversed objects. Used to avoid duplicate visits. */
4965 struct pointer_set_t *pset;
4967 /* Array of symbols to process with free_lang_data_in_decl. */
4968 vec<tree> decls;
4970 /* Array of types to process with free_lang_data_in_type. */
4971 vec<tree> types;
4975 /* Save all language fields needed to generate proper debug information
4976 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
4978 static void
4979 save_debug_info_for_decl (tree t)
4981 /*struct saved_debug_info_d *sdi;*/
4983 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
4985 /* FIXME. Partial implementation for saving debug info removed. */
4989 /* Save all language fields needed to generate proper debug information
4990 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
4992 static void
4993 save_debug_info_for_type (tree t)
4995 /*struct saved_debug_info_d *sdi;*/
4997 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
4999 /* FIXME. Partial implementation for saving debug info removed. */
5003 /* Add type or decl T to one of the list of tree nodes that need their
5004 language data removed. The lists are held inside FLD. */
5006 static void
5007 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
5009 if (DECL_P (t))
5011 fld->decls.safe_push (t);
5012 if (debug_info_level > DINFO_LEVEL_TERSE)
5013 save_debug_info_for_decl (t);
5015 else if (TYPE_P (t))
5017 fld->types.safe_push (t);
5018 if (debug_info_level > DINFO_LEVEL_TERSE)
5019 save_debug_info_for_type (t);
5021 else
5022 gcc_unreachable ();
5025 /* Push tree node T into FLD->WORKLIST. */
5027 static inline void
5028 fld_worklist_push (tree t, struct free_lang_data_d *fld)
5030 if (t && !is_lang_specific (t) && !pointer_set_contains (fld->pset, t))
5031 fld->worklist.safe_push ((t));
5035 /* Operand callback helper for free_lang_data_in_node. *TP is the
5036 subtree operand being considered. */
5038 static tree
5039 find_decls_types_r (tree *tp, int *ws, void *data)
5041 tree t = *tp;
5042 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
5044 if (TREE_CODE (t) == TREE_LIST)
5045 return NULL_TREE;
5047 /* Language specific nodes will be removed, so there is no need
5048 to gather anything under them. */
5049 if (is_lang_specific (t))
5051 *ws = 0;
5052 return NULL_TREE;
5055 if (DECL_P (t))
5057 /* Note that walk_tree does not traverse every possible field in
5058 decls, so we have to do our own traversals here. */
5059 add_tree_to_fld_list (t, fld);
5061 fld_worklist_push (DECL_NAME (t), fld);
5062 fld_worklist_push (DECL_CONTEXT (t), fld);
5063 fld_worklist_push (DECL_SIZE (t), fld);
5064 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
5066 /* We are going to remove everything under DECL_INITIAL for
5067 TYPE_DECLs. No point walking them. */
5068 if (TREE_CODE (t) != TYPE_DECL)
5069 fld_worklist_push (DECL_INITIAL (t), fld);
5071 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
5072 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
5074 if (TREE_CODE (t) == FUNCTION_DECL)
5076 fld_worklist_push (DECL_ARGUMENTS (t), fld);
5077 fld_worklist_push (DECL_RESULT (t), fld);
5079 else if (TREE_CODE (t) == TYPE_DECL)
5081 fld_worklist_push (DECL_ARGUMENT_FLD (t), fld);
5082 fld_worklist_push (DECL_VINDEX (t), fld);
5083 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
5085 else if (TREE_CODE (t) == FIELD_DECL)
5087 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
5088 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
5089 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
5090 fld_worklist_push (DECL_FCONTEXT (t), fld);
5092 else if (TREE_CODE (t) == VAR_DECL)
5094 fld_worklist_push (DECL_SECTION_NAME (t), fld);
5095 fld_worklist_push (DECL_COMDAT_GROUP (t), fld);
5098 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
5099 && DECL_HAS_VALUE_EXPR_P (t))
5100 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
5102 if (TREE_CODE (t) != FIELD_DECL
5103 && TREE_CODE (t) != TYPE_DECL)
5104 fld_worklist_push (TREE_CHAIN (t), fld);
5105 *ws = 0;
5107 else if (TYPE_P (t))
5109 /* Note that walk_tree does not traverse every possible field in
5110 types, so we have to do our own traversals here. */
5111 add_tree_to_fld_list (t, fld);
5113 if (!RECORD_OR_UNION_TYPE_P (t))
5114 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
5115 fld_worklist_push (TYPE_SIZE (t), fld);
5116 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
5117 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
5118 fld_worklist_push (TYPE_POINTER_TO (t), fld);
5119 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
5120 fld_worklist_push (TYPE_NAME (t), fld);
5121 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
5122 them and thus do not and want not to reach unused pointer types
5123 this way. */
5124 if (!POINTER_TYPE_P (t))
5125 fld_worklist_push (TYPE_MINVAL (t), fld);
5126 if (!RECORD_OR_UNION_TYPE_P (t))
5127 fld_worklist_push (TYPE_MAXVAL (t), fld);
5128 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
5129 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
5130 do not and want not to reach unused variants this way. */
5131 if (TYPE_CONTEXT (t))
5133 tree ctx = TYPE_CONTEXT (t);
5134 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
5135 So push that instead. */
5136 while (ctx && TREE_CODE (ctx) == BLOCK)
5137 ctx = BLOCK_SUPERCONTEXT (ctx);
5138 fld_worklist_push (ctx, fld);
5140 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
5141 and want not to reach unused types this way. */
5143 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
5145 unsigned i;
5146 tree tem;
5147 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
5148 fld_worklist_push (TREE_TYPE (tem), fld);
5149 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
5150 if (tem
5151 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
5152 && TREE_CODE (tem) == TREE_LIST)
5155 fld_worklist_push (TREE_VALUE (tem), fld);
5156 tem = TREE_CHAIN (tem);
5158 while (tem);
5160 if (RECORD_OR_UNION_TYPE_P (t))
5162 tree tem;
5163 /* Push all TYPE_FIELDS - there can be interleaving interesting
5164 and non-interesting things. */
5165 tem = TYPE_FIELDS (t);
5166 while (tem)
5168 if (TREE_CODE (tem) == FIELD_DECL
5169 || TREE_CODE (tem) == TYPE_DECL)
5170 fld_worklist_push (tem, fld);
5171 tem = TREE_CHAIN (tem);
5175 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5176 *ws = 0;
5178 else if (TREE_CODE (t) == BLOCK)
5180 tree tem;
5181 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5182 fld_worklist_push (tem, fld);
5183 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5184 fld_worklist_push (tem, fld);
5185 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5188 if (TREE_CODE (t) != IDENTIFIER_NODE
5189 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5190 fld_worklist_push (TREE_TYPE (t), fld);
5192 return NULL_TREE;
5196 /* Find decls and types in T. */
5198 static void
5199 find_decls_types (tree t, struct free_lang_data_d *fld)
5201 while (1)
5203 if (!pointer_set_contains (fld->pset, t))
5204 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5205 if (fld->worklist.is_empty ())
5206 break;
5207 t = fld->worklist.pop ();
5211 /* Translate all the types in LIST with the corresponding runtime
5212 types. */
5214 static tree
5215 get_eh_types_for_runtime (tree list)
5217 tree head, prev;
5219 if (list == NULL_TREE)
5220 return NULL_TREE;
5222 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5223 prev = head;
5224 list = TREE_CHAIN (list);
5225 while (list)
5227 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5228 TREE_CHAIN (prev) = n;
5229 prev = TREE_CHAIN (prev);
5230 list = TREE_CHAIN (list);
5233 return head;
5237 /* Find decls and types referenced in EH region R and store them in
5238 FLD->DECLS and FLD->TYPES. */
5240 static void
5241 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5243 switch (r->type)
5245 case ERT_CLEANUP:
5246 break;
5248 case ERT_TRY:
5250 eh_catch c;
5252 /* The types referenced in each catch must first be changed to the
5253 EH types used at runtime. This removes references to FE types
5254 in the region. */
5255 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5257 c->type_list = get_eh_types_for_runtime (c->type_list);
5258 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5261 break;
5263 case ERT_ALLOWED_EXCEPTIONS:
5264 r->u.allowed.type_list
5265 = get_eh_types_for_runtime (r->u.allowed.type_list);
5266 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5267 break;
5269 case ERT_MUST_NOT_THROW:
5270 walk_tree (&r->u.must_not_throw.failure_decl,
5271 find_decls_types_r, fld, fld->pset);
5272 break;
5277 /* Find decls and types referenced in cgraph node N and store them in
5278 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5279 look for *every* kind of DECL and TYPE node reachable from N,
5280 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5281 NAMESPACE_DECLs, etc). */
5283 static void
5284 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5286 basic_block bb;
5287 struct function *fn;
5288 unsigned ix;
5289 tree t;
5291 find_decls_types (n->symbol.decl, fld);
5293 if (!gimple_has_body_p (n->symbol.decl))
5294 return;
5296 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5298 fn = DECL_STRUCT_FUNCTION (n->symbol.decl);
5300 /* Traverse locals. */
5301 FOR_EACH_LOCAL_DECL (fn, ix, t)
5302 find_decls_types (t, fld);
5304 /* Traverse EH regions in FN. */
5306 eh_region r;
5307 FOR_ALL_EH_REGION_FN (r, fn)
5308 find_decls_types_in_eh_region (r, fld);
5311 /* Traverse every statement in FN. */
5312 FOR_EACH_BB_FN (bb, fn)
5314 gimple_stmt_iterator si;
5315 unsigned i;
5317 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5319 gimple phi = gsi_stmt (si);
5321 for (i = 0; i < gimple_phi_num_args (phi); i++)
5323 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5324 find_decls_types (*arg_p, fld);
5328 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5330 gimple stmt = gsi_stmt (si);
5332 if (is_gimple_call (stmt))
5333 find_decls_types (gimple_call_fntype (stmt), fld);
5335 for (i = 0; i < gimple_num_ops (stmt); i++)
5337 tree arg = gimple_op (stmt, i);
5338 find_decls_types (arg, fld);
5345 /* Find decls and types referenced in varpool node N and store them in
5346 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5347 look for *every* kind of DECL and TYPE node reachable from N,
5348 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5349 NAMESPACE_DECLs, etc). */
5351 static void
5352 find_decls_types_in_var (struct varpool_node *v, struct free_lang_data_d *fld)
5354 find_decls_types (v->symbol.decl, fld);
5357 /* If T needs an assembler name, have one created for it. */
5359 void
5360 assign_assembler_name_if_neeeded (tree t)
5362 if (need_assembler_name_p (t))
5364 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5365 diagnostics that use input_location to show locus
5366 information. The problem here is that, at this point,
5367 input_location is generally anchored to the end of the file
5368 (since the parser is long gone), so we don't have a good
5369 position to pin it to.
5371 To alleviate this problem, this uses the location of T's
5372 declaration. Examples of this are
5373 testsuite/g++.dg/template/cond2.C and
5374 testsuite/g++.dg/template/pr35240.C. */
5375 location_t saved_location = input_location;
5376 input_location = DECL_SOURCE_LOCATION (t);
5378 decl_assembler_name (t);
5380 input_location = saved_location;
5385 /* Free language specific information for every operand and expression
5386 in every node of the call graph. This process operates in three stages:
5388 1- Every callgraph node and varpool node is traversed looking for
5389 decls and types embedded in them. This is a more exhaustive
5390 search than that done by find_referenced_vars, because it will
5391 also collect individual fields, decls embedded in types, etc.
5393 2- All the decls found are sent to free_lang_data_in_decl.
5395 3- All the types found are sent to free_lang_data_in_type.
5397 The ordering between decls and types is important because
5398 free_lang_data_in_decl sets assembler names, which includes
5399 mangling. So types cannot be freed up until assembler names have
5400 been set up. */
5402 static void
5403 free_lang_data_in_cgraph (void)
5405 struct cgraph_node *n;
5406 struct varpool_node *v;
5407 struct free_lang_data_d fld;
5408 tree t;
5409 unsigned i;
5410 alias_pair *p;
5412 /* Initialize sets and arrays to store referenced decls and types. */
5413 fld.pset = pointer_set_create ();
5414 fld.worklist.create (0);
5415 fld.decls.create (100);
5416 fld.types.create (100);
5418 /* Find decls and types in the body of every function in the callgraph. */
5419 FOR_EACH_FUNCTION (n)
5420 find_decls_types_in_node (n, &fld);
5422 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5423 find_decls_types (p->decl, &fld);
5425 /* Find decls and types in every varpool symbol. */
5426 FOR_EACH_VARIABLE (v)
5427 find_decls_types_in_var (v, &fld);
5429 /* Set the assembler name on every decl found. We need to do this
5430 now because free_lang_data_in_decl will invalidate data needed
5431 for mangling. This breaks mangling on interdependent decls. */
5432 FOR_EACH_VEC_ELT (fld.decls, i, t)
5433 assign_assembler_name_if_neeeded (t);
5435 /* Traverse every decl found freeing its language data. */
5436 FOR_EACH_VEC_ELT (fld.decls, i, t)
5437 free_lang_data_in_decl (t);
5439 /* Traverse every type found freeing its language data. */
5440 FOR_EACH_VEC_ELT (fld.types, i, t)
5441 free_lang_data_in_type (t);
5443 pointer_set_destroy (fld.pset);
5444 fld.worklist.release ();
5445 fld.decls.release ();
5446 fld.types.release ();
5450 /* Free resources that are used by FE but are not needed once they are done. */
5452 static unsigned
5453 free_lang_data (void)
5455 unsigned i;
5457 /* If we are the LTO frontend we have freed lang-specific data already. */
5458 if (in_lto_p
5459 || !flag_generate_lto)
5460 return 0;
5462 /* Allocate and assign alias sets to the standard integer types
5463 while the slots are still in the way the frontends generated them. */
5464 for (i = 0; i < itk_none; ++i)
5465 if (integer_types[i])
5466 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5468 /* Traverse the IL resetting language specific information for
5469 operands, expressions, etc. */
5470 free_lang_data_in_cgraph ();
5472 /* Create gimple variants for common types. */
5473 ptrdiff_type_node = integer_type_node;
5474 fileptr_type_node = ptr_type_node;
5476 /* Reset some langhooks. Do not reset types_compatible_p, it may
5477 still be used indirectly via the get_alias_set langhook. */
5478 lang_hooks.dwarf_name = lhd_dwarf_name;
5479 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5480 /* We do not want the default decl_assembler_name implementation,
5481 rather if we have fixed everything we want a wrapper around it
5482 asserting that all non-local symbols already got their assembler
5483 name and only produce assembler names for local symbols. Or rather
5484 make sure we never call decl_assembler_name on local symbols and
5485 devise a separate, middle-end private scheme for it. */
5487 /* Reset diagnostic machinery. */
5488 tree_diagnostics_defaults (global_dc);
5490 return 0;
5494 struct simple_ipa_opt_pass pass_ipa_free_lang_data =
5497 SIMPLE_IPA_PASS,
5498 "*free_lang_data", /* name */
5499 OPTGROUP_NONE, /* optinfo_flags */
5500 NULL, /* gate */
5501 free_lang_data, /* execute */
5502 NULL, /* sub */
5503 NULL, /* next */
5504 0, /* static_pass_number */
5505 TV_IPA_FREE_LANG_DATA, /* tv_id */
5506 0, /* properties_required */
5507 0, /* properties_provided */
5508 0, /* properties_destroyed */
5509 0, /* todo_flags_start */
5510 0 /* todo_flags_finish */
5514 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
5515 ATTR_NAME. Also used internally by remove_attribute(). */
5516 bool
5517 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
5519 size_t ident_len = IDENTIFIER_LENGTH (ident);
5521 if (ident_len == attr_len)
5523 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
5524 return true;
5526 else if (ident_len == attr_len + 4)
5528 /* There is the possibility that ATTR is 'text' and IDENT is
5529 '__text__'. */
5530 const char *p = IDENTIFIER_POINTER (ident);
5531 if (p[0] == '_' && p[1] == '_'
5532 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5533 && strncmp (attr_name, p + 2, attr_len) == 0)
5534 return true;
5537 return false;
5540 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
5541 of ATTR_NAME, and LIST is not NULL_TREE. */
5542 tree
5543 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
5545 while (list)
5547 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5549 if (ident_len == attr_len)
5551 if (!strcmp (attr_name,
5552 IDENTIFIER_POINTER (get_attribute_name (list))))
5553 break;
5555 /* TODO: If we made sure that attributes were stored in the
5556 canonical form without '__...__' (ie, as in 'text' as opposed
5557 to '__text__') then we could avoid the following case. */
5558 else if (ident_len == attr_len + 4)
5560 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5561 if (p[0] == '_' && p[1] == '_'
5562 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5563 && strncmp (attr_name, p + 2, attr_len) == 0)
5564 break;
5566 list = TREE_CHAIN (list);
5569 return list;
5572 /* A variant of lookup_attribute() that can be used with an identifier
5573 as the first argument, and where the identifier can be either
5574 'text' or '__text__'.
5576 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
5577 return a pointer to the attribute's list element if the attribute
5578 is part of the list, or NULL_TREE if not found. If the attribute
5579 appears more than once, this only returns the first occurrence; the
5580 TREE_CHAIN of the return value should be passed back in if further
5581 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
5582 can be in the form 'text' or '__text__'. */
5583 static tree
5584 lookup_ident_attribute (tree attr_identifier, tree list)
5586 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
5588 while (list)
5590 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
5591 == IDENTIFIER_NODE);
5593 /* Identifiers can be compared directly for equality. */
5594 if (attr_identifier == get_attribute_name (list))
5595 break;
5597 /* If they are not equal, they may still be one in the form
5598 'text' while the other one is in the form '__text__'. TODO:
5599 If we were storing attributes in normalized 'text' form, then
5600 this could all go away and we could take full advantage of
5601 the fact that we're comparing identifiers. :-) */
5603 size_t attr_len = IDENTIFIER_LENGTH (attr_identifier);
5604 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5606 if (ident_len == attr_len + 4)
5608 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5609 const char *q = IDENTIFIER_POINTER (attr_identifier);
5610 if (p[0] == '_' && p[1] == '_'
5611 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5612 && strncmp (q, p + 2, attr_len) == 0)
5613 break;
5615 else if (ident_len + 4 == attr_len)
5617 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5618 const char *q = IDENTIFIER_POINTER (attr_identifier);
5619 if (q[0] == '_' && q[1] == '_'
5620 && q[attr_len - 2] == '_' && q[attr_len - 1] == '_'
5621 && strncmp (q + 2, p, ident_len) == 0)
5622 break;
5625 list = TREE_CHAIN (list);
5628 return list;
5631 /* Remove any instances of attribute ATTR_NAME in LIST and return the
5632 modified list. */
5634 tree
5635 remove_attribute (const char *attr_name, tree list)
5637 tree *p;
5638 size_t attr_len = strlen (attr_name);
5640 gcc_checking_assert (attr_name[0] != '_');
5642 for (p = &list; *p; )
5644 tree l = *p;
5645 /* TODO: If we were storing attributes in normalized form, here
5646 we could use a simple strcmp(). */
5647 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
5648 *p = TREE_CHAIN (l);
5649 else
5650 p = &TREE_CHAIN (l);
5653 return list;
5656 /* Return an attribute list that is the union of a1 and a2. */
5658 tree
5659 merge_attributes (tree a1, tree a2)
5661 tree attributes;
5663 /* Either one unset? Take the set one. */
5665 if ((attributes = a1) == 0)
5666 attributes = a2;
5668 /* One that completely contains the other? Take it. */
5670 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
5672 if (attribute_list_contained (a2, a1))
5673 attributes = a2;
5674 else
5676 /* Pick the longest list, and hang on the other list. */
5678 if (list_length (a1) < list_length (a2))
5679 attributes = a2, a2 = a1;
5681 for (; a2 != 0; a2 = TREE_CHAIN (a2))
5683 tree a;
5684 for (a = lookup_ident_attribute (get_attribute_name (a2),
5685 attributes);
5686 a != NULL_TREE && !attribute_value_equal (a, a2);
5687 a = lookup_ident_attribute (get_attribute_name (a2),
5688 TREE_CHAIN (a)))
5690 if (a == NULL_TREE)
5692 a1 = copy_node (a2);
5693 TREE_CHAIN (a1) = attributes;
5694 attributes = a1;
5699 return attributes;
5702 /* Given types T1 and T2, merge their attributes and return
5703 the result. */
5705 tree
5706 merge_type_attributes (tree t1, tree t2)
5708 return merge_attributes (TYPE_ATTRIBUTES (t1),
5709 TYPE_ATTRIBUTES (t2));
5712 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
5713 the result. */
5715 tree
5716 merge_decl_attributes (tree olddecl, tree newdecl)
5718 return merge_attributes (DECL_ATTRIBUTES (olddecl),
5719 DECL_ATTRIBUTES (newdecl));
5722 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
5724 /* Specialization of merge_decl_attributes for various Windows targets.
5726 This handles the following situation:
5728 __declspec (dllimport) int foo;
5729 int foo;
5731 The second instance of `foo' nullifies the dllimport. */
5733 tree
5734 merge_dllimport_decl_attributes (tree old, tree new_tree)
5736 tree a;
5737 int delete_dllimport_p = 1;
5739 /* What we need to do here is remove from `old' dllimport if it doesn't
5740 appear in `new'. dllimport behaves like extern: if a declaration is
5741 marked dllimport and a definition appears later, then the object
5742 is not dllimport'd. We also remove a `new' dllimport if the old list
5743 contains dllexport: dllexport always overrides dllimport, regardless
5744 of the order of declaration. */
5745 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
5746 delete_dllimport_p = 0;
5747 else if (DECL_DLLIMPORT_P (new_tree)
5748 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
5750 DECL_DLLIMPORT_P (new_tree) = 0;
5751 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
5752 "dllimport ignored", new_tree);
5754 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
5756 /* Warn about overriding a symbol that has already been used, e.g.:
5757 extern int __attribute__ ((dllimport)) foo;
5758 int* bar () {return &foo;}
5759 int foo;
5761 if (TREE_USED (old))
5763 warning (0, "%q+D redeclared without dllimport attribute "
5764 "after being referenced with dll linkage", new_tree);
5765 /* If we have used a variable's address with dllimport linkage,
5766 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
5767 decl may already have had TREE_CONSTANT computed.
5768 We still remove the attribute so that assembler code refers
5769 to '&foo rather than '_imp__foo'. */
5770 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
5771 DECL_DLLIMPORT_P (new_tree) = 1;
5774 /* Let an inline definition silently override the external reference,
5775 but otherwise warn about attribute inconsistency. */
5776 else if (TREE_CODE (new_tree) == VAR_DECL
5777 || !DECL_DECLARED_INLINE_P (new_tree))
5778 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
5779 "previous dllimport ignored", new_tree);
5781 else
5782 delete_dllimport_p = 0;
5784 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
5786 if (delete_dllimport_p)
5787 a = remove_attribute ("dllimport", a);
5789 return a;
5792 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
5793 struct attribute_spec.handler. */
5795 tree
5796 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
5797 bool *no_add_attrs)
5799 tree node = *pnode;
5800 bool is_dllimport;
5802 /* These attributes may apply to structure and union types being created,
5803 but otherwise should pass to the declaration involved. */
5804 if (!DECL_P (node))
5806 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
5807 | (int) ATTR_FLAG_ARRAY_NEXT))
5809 *no_add_attrs = true;
5810 return tree_cons (name, args, NULL_TREE);
5812 if (TREE_CODE (node) == RECORD_TYPE
5813 || TREE_CODE (node) == UNION_TYPE)
5815 node = TYPE_NAME (node);
5816 if (!node)
5817 return NULL_TREE;
5819 else
5821 warning (OPT_Wattributes, "%qE attribute ignored",
5822 name);
5823 *no_add_attrs = true;
5824 return NULL_TREE;
5828 if (TREE_CODE (node) != FUNCTION_DECL
5829 && TREE_CODE (node) != VAR_DECL
5830 && TREE_CODE (node) != TYPE_DECL)
5832 *no_add_attrs = true;
5833 warning (OPT_Wattributes, "%qE attribute ignored",
5834 name);
5835 return NULL_TREE;
5838 if (TREE_CODE (node) == TYPE_DECL
5839 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
5840 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
5842 *no_add_attrs = true;
5843 warning (OPT_Wattributes, "%qE attribute ignored",
5844 name);
5845 return NULL_TREE;
5848 is_dllimport = is_attribute_p ("dllimport", name);
5850 /* Report error on dllimport ambiguities seen now before they cause
5851 any damage. */
5852 if (is_dllimport)
5854 /* Honor any target-specific overrides. */
5855 if (!targetm.valid_dllimport_attribute_p (node))
5856 *no_add_attrs = true;
5858 else if (TREE_CODE (node) == FUNCTION_DECL
5859 && DECL_DECLARED_INLINE_P (node))
5861 warning (OPT_Wattributes, "inline function %q+D declared as "
5862 " dllimport: attribute ignored", node);
5863 *no_add_attrs = true;
5865 /* Like MS, treat definition of dllimported variables and
5866 non-inlined functions on declaration as syntax errors. */
5867 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
5869 error ("function %q+D definition is marked dllimport", node);
5870 *no_add_attrs = true;
5873 else if (TREE_CODE (node) == VAR_DECL)
5875 if (DECL_INITIAL (node))
5877 error ("variable %q+D definition is marked dllimport",
5878 node);
5879 *no_add_attrs = true;
5882 /* `extern' needn't be specified with dllimport.
5883 Specify `extern' now and hope for the best. Sigh. */
5884 DECL_EXTERNAL (node) = 1;
5885 /* Also, implicitly give dllimport'd variables declared within
5886 a function global scope, unless declared static. */
5887 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
5888 TREE_PUBLIC (node) = 1;
5891 if (*no_add_attrs == false)
5892 DECL_DLLIMPORT_P (node) = 1;
5894 else if (TREE_CODE (node) == FUNCTION_DECL
5895 && DECL_DECLARED_INLINE_P (node)
5896 && flag_keep_inline_dllexport)
5897 /* An exported function, even if inline, must be emitted. */
5898 DECL_EXTERNAL (node) = 0;
5900 /* Report error if symbol is not accessible at global scope. */
5901 if (!TREE_PUBLIC (node)
5902 && (TREE_CODE (node) == VAR_DECL
5903 || TREE_CODE (node) == FUNCTION_DECL))
5905 error ("external linkage required for symbol %q+D because of "
5906 "%qE attribute", node, name);
5907 *no_add_attrs = true;
5910 /* A dllexport'd entity must have default visibility so that other
5911 program units (shared libraries or the main executable) can see
5912 it. A dllimport'd entity must have default visibility so that
5913 the linker knows that undefined references within this program
5914 unit can be resolved by the dynamic linker. */
5915 if (!*no_add_attrs)
5917 if (DECL_VISIBILITY_SPECIFIED (node)
5918 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
5919 error ("%qE implies default visibility, but %qD has already "
5920 "been declared with a different visibility",
5921 name, node);
5922 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
5923 DECL_VISIBILITY_SPECIFIED (node) = 1;
5926 return NULL_TREE;
5929 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
5931 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
5932 of the various TYPE_QUAL values. */
5934 static void
5935 set_type_quals (tree type, int type_quals)
5937 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
5938 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
5939 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
5940 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
5943 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
5945 bool
5946 check_qualified_type (const_tree cand, const_tree base, int type_quals)
5948 return (TYPE_QUALS (cand) == type_quals
5949 && TYPE_NAME (cand) == TYPE_NAME (base)
5950 /* Apparently this is needed for Objective-C. */
5951 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5952 /* Check alignment. */
5953 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
5954 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5955 TYPE_ATTRIBUTES (base)));
5958 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
5960 static bool
5961 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
5963 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
5964 && TYPE_NAME (cand) == TYPE_NAME (base)
5965 /* Apparently this is needed for Objective-C. */
5966 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5967 /* Check alignment. */
5968 && TYPE_ALIGN (cand) == align
5969 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5970 TYPE_ATTRIBUTES (base)));
5973 /* Return a version of the TYPE, qualified as indicated by the
5974 TYPE_QUALS, if one exists. If no qualified version exists yet,
5975 return NULL_TREE. */
5977 tree
5978 get_qualified_type (tree type, int type_quals)
5980 tree t;
5982 if (TYPE_QUALS (type) == type_quals)
5983 return type;
5985 /* Search the chain of variants to see if there is already one there just
5986 like the one we need to have. If so, use that existing one. We must
5987 preserve the TYPE_NAME, since there is code that depends on this. */
5988 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5989 if (check_qualified_type (t, type, type_quals))
5990 return t;
5992 return NULL_TREE;
5995 /* Like get_qualified_type, but creates the type if it does not
5996 exist. This function never returns NULL_TREE. */
5998 tree
5999 build_qualified_type (tree type, int type_quals)
6001 tree t;
6003 /* See if we already have the appropriate qualified variant. */
6004 t = get_qualified_type (type, type_quals);
6006 /* If not, build it. */
6007 if (!t)
6009 t = build_variant_type_copy (type);
6010 set_type_quals (t, type_quals);
6012 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6013 /* Propagate structural equality. */
6014 SET_TYPE_STRUCTURAL_EQUALITY (t);
6015 else if (TYPE_CANONICAL (type) != type)
6016 /* Build the underlying canonical type, since it is different
6017 from TYPE. */
6018 TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
6019 type_quals);
6020 else
6021 /* T is its own canonical type. */
6022 TYPE_CANONICAL (t) = t;
6026 return t;
6029 /* Create a variant of type T with alignment ALIGN. */
6031 tree
6032 build_aligned_type (tree type, unsigned int align)
6034 tree t;
6036 if (TYPE_PACKED (type)
6037 || TYPE_ALIGN (type) == align)
6038 return type;
6040 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6041 if (check_aligned_type (t, type, align))
6042 return t;
6044 t = build_variant_type_copy (type);
6045 TYPE_ALIGN (t) = align;
6047 return t;
6050 /* Create a new distinct copy of TYPE. The new type is made its own
6051 MAIN_VARIANT. If TYPE requires structural equality checks, the
6052 resulting type requires structural equality checks; otherwise, its
6053 TYPE_CANONICAL points to itself. */
6055 tree
6056 build_distinct_type_copy (tree type)
6058 tree t = copy_node (type);
6060 TYPE_POINTER_TO (t) = 0;
6061 TYPE_REFERENCE_TO (t) = 0;
6063 /* Set the canonical type either to a new equivalence class, or
6064 propagate the need for structural equality checks. */
6065 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6066 SET_TYPE_STRUCTURAL_EQUALITY (t);
6067 else
6068 TYPE_CANONICAL (t) = t;
6070 /* Make it its own variant. */
6071 TYPE_MAIN_VARIANT (t) = t;
6072 TYPE_NEXT_VARIANT (t) = 0;
6074 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
6075 whose TREE_TYPE is not t. This can also happen in the Ada
6076 frontend when using subtypes. */
6078 return t;
6081 /* Create a new variant of TYPE, equivalent but distinct. This is so
6082 the caller can modify it. TYPE_CANONICAL for the return type will
6083 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
6084 are considered equal by the language itself (or that both types
6085 require structural equality checks). */
6087 tree
6088 build_variant_type_copy (tree type)
6090 tree t, m = TYPE_MAIN_VARIANT (type);
6092 t = build_distinct_type_copy (type);
6094 /* Since we're building a variant, assume that it is a non-semantic
6095 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
6096 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
6098 /* Add the new type to the chain of variants of TYPE. */
6099 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
6100 TYPE_NEXT_VARIANT (m) = t;
6101 TYPE_MAIN_VARIANT (t) = m;
6103 return t;
6106 /* Return true if the from tree in both tree maps are equal. */
6109 tree_map_base_eq (const void *va, const void *vb)
6111 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6112 *const b = (const struct tree_map_base *) vb;
6113 return (a->from == b->from);
6116 /* Hash a from tree in a tree_base_map. */
6118 unsigned int
6119 tree_map_base_hash (const void *item)
6121 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6124 /* Return true if this tree map structure is marked for garbage collection
6125 purposes. We simply return true if the from tree is marked, so that this
6126 structure goes away when the from tree goes away. */
6129 tree_map_base_marked_p (const void *p)
6131 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6134 /* Hash a from tree in a tree_map. */
6136 unsigned int
6137 tree_map_hash (const void *item)
6139 return (((const struct tree_map *) item)->hash);
6142 /* Hash a from tree in a tree_decl_map. */
6144 unsigned int
6145 tree_decl_map_hash (const void *item)
6147 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6150 /* Return the initialization priority for DECL. */
6152 priority_type
6153 decl_init_priority_lookup (tree decl)
6155 struct tree_priority_map *h;
6156 struct tree_map_base in;
6158 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6159 in.from = decl;
6160 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6161 return h ? h->init : DEFAULT_INIT_PRIORITY;
6164 /* Return the finalization priority for DECL. */
6166 priority_type
6167 decl_fini_priority_lookup (tree decl)
6169 struct tree_priority_map *h;
6170 struct tree_map_base in;
6172 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6173 in.from = decl;
6174 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6175 return h ? h->fini : DEFAULT_INIT_PRIORITY;
6178 /* Return the initialization and finalization priority information for
6179 DECL. If there is no previous priority information, a freshly
6180 allocated structure is returned. */
6182 static struct tree_priority_map *
6183 decl_priority_info (tree decl)
6185 struct tree_priority_map in;
6186 struct tree_priority_map *h;
6187 void **loc;
6189 in.base.from = decl;
6190 loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
6191 h = (struct tree_priority_map *) *loc;
6192 if (!h)
6194 h = ggc_alloc_cleared_tree_priority_map ();
6195 *loc = h;
6196 h->base.from = decl;
6197 h->init = DEFAULT_INIT_PRIORITY;
6198 h->fini = DEFAULT_INIT_PRIORITY;
6201 return h;
6204 /* Set the initialization priority for DECL to PRIORITY. */
6206 void
6207 decl_init_priority_insert (tree decl, priority_type priority)
6209 struct tree_priority_map *h;
6211 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6212 if (priority == DEFAULT_INIT_PRIORITY)
6213 return;
6214 h = decl_priority_info (decl);
6215 h->init = priority;
6218 /* Set the finalization priority for DECL to PRIORITY. */
6220 void
6221 decl_fini_priority_insert (tree decl, priority_type priority)
6223 struct tree_priority_map *h;
6225 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6226 if (priority == DEFAULT_INIT_PRIORITY)
6227 return;
6228 h = decl_priority_info (decl);
6229 h->fini = priority;
6232 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6234 static void
6235 print_debug_expr_statistics (void)
6237 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6238 (long) htab_size (debug_expr_for_decl),
6239 (long) htab_elements (debug_expr_for_decl),
6240 htab_collisions (debug_expr_for_decl));
6243 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6245 static void
6246 print_value_expr_statistics (void)
6248 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6249 (long) htab_size (value_expr_for_decl),
6250 (long) htab_elements (value_expr_for_decl),
6251 htab_collisions (value_expr_for_decl));
6254 /* Lookup a debug expression for FROM, and return it if we find one. */
6256 tree
6257 decl_debug_expr_lookup (tree from)
6259 struct tree_decl_map *h, in;
6260 in.base.from = from;
6262 h = (struct tree_decl_map *)
6263 htab_find_with_hash (debug_expr_for_decl, &in, DECL_UID (from));
6264 if (h)
6265 return h->to;
6266 return NULL_TREE;
6269 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6271 void
6272 decl_debug_expr_insert (tree from, tree to)
6274 struct tree_decl_map *h;
6275 void **loc;
6277 h = ggc_alloc_tree_decl_map ();
6278 h->base.from = from;
6279 h->to = to;
6280 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, DECL_UID (from),
6281 INSERT);
6282 *(struct tree_decl_map **) loc = h;
6285 /* Lookup a value expression for FROM, and return it if we find one. */
6287 tree
6288 decl_value_expr_lookup (tree from)
6290 struct tree_decl_map *h, in;
6291 in.base.from = from;
6293 h = (struct tree_decl_map *)
6294 htab_find_with_hash (value_expr_for_decl, &in, DECL_UID (from));
6295 if (h)
6296 return h->to;
6297 return NULL_TREE;
6300 /* Insert a mapping FROM->TO in the value expression hashtable. */
6302 void
6303 decl_value_expr_insert (tree from, tree to)
6305 struct tree_decl_map *h;
6306 void **loc;
6308 h = ggc_alloc_tree_decl_map ();
6309 h->base.from = from;
6310 h->to = to;
6311 loc = htab_find_slot_with_hash (value_expr_for_decl, h, DECL_UID (from),
6312 INSERT);
6313 *(struct tree_decl_map **) loc = h;
6316 /* Lookup a vector of debug arguments for FROM, and return it if we
6317 find one. */
6319 vec<tree, va_gc> **
6320 decl_debug_args_lookup (tree from)
6322 struct tree_vec_map *h, in;
6324 if (!DECL_HAS_DEBUG_ARGS_P (from))
6325 return NULL;
6326 gcc_checking_assert (debug_args_for_decl != NULL);
6327 in.base.from = from;
6328 h = (struct tree_vec_map *)
6329 htab_find_with_hash (debug_args_for_decl, &in, DECL_UID (from));
6330 if (h)
6331 return &h->to;
6332 return NULL;
6335 /* Insert a mapping FROM->empty vector of debug arguments in the value
6336 expression hashtable. */
6338 vec<tree, va_gc> **
6339 decl_debug_args_insert (tree from)
6341 struct tree_vec_map *h;
6342 void **loc;
6344 if (DECL_HAS_DEBUG_ARGS_P (from))
6345 return decl_debug_args_lookup (from);
6346 if (debug_args_for_decl == NULL)
6347 debug_args_for_decl = htab_create_ggc (64, tree_vec_map_hash,
6348 tree_vec_map_eq, 0);
6349 h = ggc_alloc_tree_vec_map ();
6350 h->base.from = from;
6351 h->to = NULL;
6352 loc = htab_find_slot_with_hash (debug_args_for_decl, h, DECL_UID (from),
6353 INSERT);
6354 *(struct tree_vec_map **) loc = h;
6355 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6356 return &h->to;
6359 /* Hashing of types so that we don't make duplicates.
6360 The entry point is `type_hash_canon'. */
6362 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6363 with types in the TREE_VALUE slots), by adding the hash codes
6364 of the individual types. */
6366 static unsigned int
6367 type_hash_list (const_tree list, hashval_t hashcode)
6369 const_tree tail;
6371 for (tail = list; tail; tail = TREE_CHAIN (tail))
6372 if (TREE_VALUE (tail) != error_mark_node)
6373 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
6374 hashcode);
6376 return hashcode;
6379 /* These are the Hashtable callback functions. */
6381 /* Returns true iff the types are equivalent. */
6383 static int
6384 type_hash_eq (const void *va, const void *vb)
6386 const struct type_hash *const a = (const struct type_hash *) va,
6387 *const b = (const struct type_hash *) vb;
6389 /* First test the things that are the same for all types. */
6390 if (a->hash != b->hash
6391 || TREE_CODE (a->type) != TREE_CODE (b->type)
6392 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6393 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6394 TYPE_ATTRIBUTES (b->type))
6395 || (TREE_CODE (a->type) != COMPLEX_TYPE
6396 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6397 return 0;
6399 /* Be careful about comparing arrays before and after the element type
6400 has been completed; don't compare TYPE_ALIGN unless both types are
6401 complete. */
6402 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6403 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6404 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6405 return 0;
6407 switch (TREE_CODE (a->type))
6409 case VOID_TYPE:
6410 case COMPLEX_TYPE:
6411 case POINTER_TYPE:
6412 case REFERENCE_TYPE:
6413 case NULLPTR_TYPE:
6414 return 1;
6416 case VECTOR_TYPE:
6417 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6419 case ENUMERAL_TYPE:
6420 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6421 && !(TYPE_VALUES (a->type)
6422 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6423 && TYPE_VALUES (b->type)
6424 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6425 && type_list_equal (TYPE_VALUES (a->type),
6426 TYPE_VALUES (b->type))))
6427 return 0;
6429 /* ... fall through ... */
6431 case INTEGER_TYPE:
6432 case REAL_TYPE:
6433 case BOOLEAN_TYPE:
6434 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6435 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6436 TYPE_MAX_VALUE (b->type)))
6437 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6438 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6439 TYPE_MIN_VALUE (b->type))));
6441 case FIXED_POINT_TYPE:
6442 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6444 case OFFSET_TYPE:
6445 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6447 case METHOD_TYPE:
6448 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6449 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6450 || (TYPE_ARG_TYPES (a->type)
6451 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6452 && TYPE_ARG_TYPES (b->type)
6453 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6454 && type_list_equal (TYPE_ARG_TYPES (a->type),
6455 TYPE_ARG_TYPES (b->type)))))
6456 break;
6457 return 0;
6458 case ARRAY_TYPE:
6459 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6461 case RECORD_TYPE:
6462 case UNION_TYPE:
6463 case QUAL_UNION_TYPE:
6464 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6465 || (TYPE_FIELDS (a->type)
6466 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6467 && TYPE_FIELDS (b->type)
6468 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6469 && type_list_equal (TYPE_FIELDS (a->type),
6470 TYPE_FIELDS (b->type))));
6472 case FUNCTION_TYPE:
6473 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6474 || (TYPE_ARG_TYPES (a->type)
6475 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6476 && TYPE_ARG_TYPES (b->type)
6477 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6478 && type_list_equal (TYPE_ARG_TYPES (a->type),
6479 TYPE_ARG_TYPES (b->type))))
6480 break;
6481 return 0;
6483 default:
6484 return 0;
6487 if (lang_hooks.types.type_hash_eq != NULL)
6488 return lang_hooks.types.type_hash_eq (a->type, b->type);
6490 return 1;
6493 /* Return the cached hash value. */
6495 static hashval_t
6496 type_hash_hash (const void *item)
6498 return ((const struct type_hash *) item)->hash;
6501 /* Look in the type hash table for a type isomorphic to TYPE.
6502 If one is found, return it. Otherwise return 0. */
6504 static tree
6505 type_hash_lookup (hashval_t hashcode, tree type)
6507 struct type_hash *h, in;
6509 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6510 must call that routine before comparing TYPE_ALIGNs. */
6511 layout_type (type);
6513 in.hash = hashcode;
6514 in.type = type;
6516 h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
6517 hashcode);
6518 if (h)
6519 return h->type;
6520 return NULL_TREE;
6523 /* Add an entry to the type-hash-table
6524 for a type TYPE whose hash code is HASHCODE. */
6526 static void
6527 type_hash_add (hashval_t hashcode, tree type)
6529 struct type_hash *h;
6530 void **loc;
6532 h = ggc_alloc_type_hash ();
6533 h->hash = hashcode;
6534 h->type = type;
6535 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
6536 *loc = (void *)h;
6539 /* Given TYPE, and HASHCODE its hash code, return the canonical
6540 object for an identical type if one already exists.
6541 Otherwise, return TYPE, and record it as the canonical object.
6543 To use this function, first create a type of the sort you want.
6544 Then compute its hash code from the fields of the type that
6545 make it different from other similar types.
6546 Then call this function and use the value. */
6548 tree
6549 type_hash_canon (unsigned int hashcode, tree type)
6551 tree t1;
6553 /* The hash table only contains main variants, so ensure that's what we're
6554 being passed. */
6555 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6557 /* See if the type is in the hash table already. If so, return it.
6558 Otherwise, add the type. */
6559 t1 = type_hash_lookup (hashcode, type);
6560 if (t1 != 0)
6562 if (GATHER_STATISTICS)
6564 tree_code_counts[(int) TREE_CODE (type)]--;
6565 tree_node_counts[(int) t_kind]--;
6566 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
6568 return t1;
6570 else
6572 type_hash_add (hashcode, type);
6573 return type;
6577 /* See if the data pointed to by the type hash table is marked. We consider
6578 it marked if the type is marked or if a debug type number or symbol
6579 table entry has been made for the type. */
6581 static int
6582 type_hash_marked_p (const void *p)
6584 const_tree const type = ((const struct type_hash *) p)->type;
6586 return ggc_marked_p (type);
6589 static void
6590 print_type_hash_statistics (void)
6592 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6593 (long) htab_size (type_hash_table),
6594 (long) htab_elements (type_hash_table),
6595 htab_collisions (type_hash_table));
6598 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
6599 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
6600 by adding the hash codes of the individual attributes. */
6602 static unsigned int
6603 attribute_hash_list (const_tree list, hashval_t hashcode)
6605 const_tree tail;
6607 for (tail = list; tail; tail = TREE_CHAIN (tail))
6608 /* ??? Do we want to add in TREE_VALUE too? */
6609 hashcode = iterative_hash_object
6610 (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)), hashcode);
6611 return hashcode;
6614 /* Given two lists of attributes, return true if list l2 is
6615 equivalent to l1. */
6618 attribute_list_equal (const_tree l1, const_tree l2)
6620 if (l1 == l2)
6621 return 1;
6623 return attribute_list_contained (l1, l2)
6624 && attribute_list_contained (l2, l1);
6627 /* Given two lists of attributes, return true if list L2 is
6628 completely contained within L1. */
6629 /* ??? This would be faster if attribute names were stored in a canonicalized
6630 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
6631 must be used to show these elements are equivalent (which they are). */
6632 /* ??? It's not clear that attributes with arguments will always be handled
6633 correctly. */
6636 attribute_list_contained (const_tree l1, const_tree l2)
6638 const_tree t1, t2;
6640 /* First check the obvious, maybe the lists are identical. */
6641 if (l1 == l2)
6642 return 1;
6644 /* Maybe the lists are similar. */
6645 for (t1 = l1, t2 = l2;
6646 t1 != 0 && t2 != 0
6647 && get_attribute_name (t1) == get_attribute_name (t2)
6648 && TREE_VALUE (t1) == TREE_VALUE (t2);
6649 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6652 /* Maybe the lists are equal. */
6653 if (t1 == 0 && t2 == 0)
6654 return 1;
6656 for (; t2 != 0; t2 = TREE_CHAIN (t2))
6658 const_tree attr;
6659 /* This CONST_CAST is okay because lookup_attribute does not
6660 modify its argument and the return value is assigned to a
6661 const_tree. */
6662 for (attr = lookup_ident_attribute (get_attribute_name (t2), CONST_CAST_TREE(l1));
6663 attr != NULL_TREE && !attribute_value_equal (t2, attr);
6664 attr = lookup_ident_attribute (get_attribute_name (t2), TREE_CHAIN (attr)))
6667 if (attr == NULL_TREE)
6668 return 0;
6671 return 1;
6674 /* Given two lists of types
6675 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6676 return 1 if the lists contain the same types in the same order.
6677 Also, the TREE_PURPOSEs must match. */
6680 type_list_equal (const_tree l1, const_tree l2)
6682 const_tree t1, t2;
6684 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6685 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6686 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6687 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6688 && (TREE_TYPE (TREE_PURPOSE (t1))
6689 == TREE_TYPE (TREE_PURPOSE (t2))))))
6690 return 0;
6692 return t1 == t2;
6695 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6696 given by TYPE. If the argument list accepts variable arguments,
6697 then this function counts only the ordinary arguments. */
6700 type_num_arguments (const_tree type)
6702 int i = 0;
6703 tree t;
6705 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6706 /* If the function does not take a variable number of arguments,
6707 the last element in the list will have type `void'. */
6708 if (VOID_TYPE_P (TREE_VALUE (t)))
6709 break;
6710 else
6711 ++i;
6713 return i;
6716 /* Nonzero if integer constants T1 and T2
6717 represent the same constant value. */
6720 tree_int_cst_equal (const_tree t1, const_tree t2)
6722 if (t1 == t2)
6723 return 1;
6725 if (t1 == 0 || t2 == 0)
6726 return 0;
6728 if (TREE_CODE (t1) == INTEGER_CST
6729 && TREE_CODE (t2) == INTEGER_CST
6730 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6731 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
6732 return 1;
6734 return 0;
6737 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
6738 The precise way of comparison depends on their data type. */
6741 tree_int_cst_lt (const_tree t1, const_tree t2)
6743 if (t1 == t2)
6744 return 0;
6746 if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
6748 int t1_sgn = tree_int_cst_sgn (t1);
6749 int t2_sgn = tree_int_cst_sgn (t2);
6751 if (t1_sgn < t2_sgn)
6752 return 1;
6753 else if (t1_sgn > t2_sgn)
6754 return 0;
6755 /* Otherwise, both are non-negative, so we compare them as
6756 unsigned just in case one of them would overflow a signed
6757 type. */
6759 else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
6760 return INT_CST_LT (t1, t2);
6762 return INT_CST_LT_UNSIGNED (t1, t2);
6765 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
6768 tree_int_cst_compare (const_tree t1, const_tree t2)
6770 if (tree_int_cst_lt (t1, t2))
6771 return -1;
6772 else if (tree_int_cst_lt (t2, t1))
6773 return 1;
6774 else
6775 return 0;
6778 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
6779 the host. If POS is zero, the value can be represented in a single
6780 HOST_WIDE_INT. If POS is nonzero, the value must be non-negative and can
6781 be represented in a single unsigned HOST_WIDE_INT. */
6784 host_integerp (const_tree t, int pos)
6786 if (t == NULL_TREE)
6787 return 0;
6789 return (TREE_CODE (t) == INTEGER_CST
6790 && ((TREE_INT_CST_HIGH (t) == 0
6791 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
6792 || (! pos && TREE_INT_CST_HIGH (t) == -1
6793 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
6794 && !TYPE_UNSIGNED (TREE_TYPE (t)))
6795 || (pos && TREE_INT_CST_HIGH (t) == 0)));
6798 /* Return the HOST_WIDE_INT least significant bits of T if it is an
6799 INTEGER_CST and there is no overflow. POS is nonzero if the result must
6800 be non-negative. We must be able to satisfy the above conditions. */
6802 HOST_WIDE_INT
6803 tree_low_cst (const_tree t, int pos)
6805 gcc_assert (host_integerp (t, pos));
6806 return TREE_INT_CST_LOW (t);
6809 /* Return the HOST_WIDE_INT least significant bits of T, a sizetype
6810 kind INTEGER_CST. This makes sure to properly sign-extend the
6811 constant. */
6813 HOST_WIDE_INT
6814 size_low_cst (const_tree t)
6816 double_int d = tree_to_double_int (t);
6817 return d.sext (TYPE_PRECISION (TREE_TYPE (t))).low;
6820 /* Return the most significant (sign) bit of T. */
6823 tree_int_cst_sign_bit (const_tree t)
6825 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
6826 unsigned HOST_WIDE_INT w;
6828 if (bitno < HOST_BITS_PER_WIDE_INT)
6829 w = TREE_INT_CST_LOW (t);
6830 else
6832 w = TREE_INT_CST_HIGH (t);
6833 bitno -= HOST_BITS_PER_WIDE_INT;
6836 return (w >> bitno) & 1;
6839 /* Return an indication of the sign of the integer constant T.
6840 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
6841 Note that -1 will never be returned if T's type is unsigned. */
6844 tree_int_cst_sgn (const_tree t)
6846 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
6847 return 0;
6848 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
6849 return 1;
6850 else if (TREE_INT_CST_HIGH (t) < 0)
6851 return -1;
6852 else
6853 return 1;
6856 /* Return the minimum number of bits needed to represent VALUE in a
6857 signed or unsigned type, UNSIGNEDP says which. */
6859 unsigned int
6860 tree_int_cst_min_precision (tree value, bool unsignedp)
6862 /* If the value is negative, compute its negative minus 1. The latter
6863 adjustment is because the absolute value of the largest negative value
6864 is one larger than the largest positive value. This is equivalent to
6865 a bit-wise negation, so use that operation instead. */
6867 if (tree_int_cst_sgn (value) < 0)
6868 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
6870 /* Return the number of bits needed, taking into account the fact
6871 that we need one more bit for a signed than unsigned type.
6872 If value is 0 or -1, the minimum precision is 1 no matter
6873 whether unsignedp is true or false. */
6875 if (integer_zerop (value))
6876 return 1;
6877 else
6878 return tree_floor_log2 (value) + 1 + !unsignedp;
6881 /* Compare two constructor-element-type constants. Return 1 if the lists
6882 are known to be equal; otherwise return 0. */
6885 simple_cst_list_equal (const_tree l1, const_tree l2)
6887 while (l1 != NULL_TREE && l2 != NULL_TREE)
6889 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
6890 return 0;
6892 l1 = TREE_CHAIN (l1);
6893 l2 = TREE_CHAIN (l2);
6896 return l1 == l2;
6899 /* Return truthvalue of whether T1 is the same tree structure as T2.
6900 Return 1 if they are the same.
6901 Return 0 if they are understandably different.
6902 Return -1 if either contains tree structure not understood by
6903 this function. */
6906 simple_cst_equal (const_tree t1, const_tree t2)
6908 enum tree_code code1, code2;
6909 int cmp;
6910 int i;
6912 if (t1 == t2)
6913 return 1;
6914 if (t1 == 0 || t2 == 0)
6915 return 0;
6917 code1 = TREE_CODE (t1);
6918 code2 = TREE_CODE (t2);
6920 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
6922 if (CONVERT_EXPR_CODE_P (code2)
6923 || code2 == NON_LVALUE_EXPR)
6924 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6925 else
6926 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
6929 else if (CONVERT_EXPR_CODE_P (code2)
6930 || code2 == NON_LVALUE_EXPR)
6931 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
6933 if (code1 != code2)
6934 return 0;
6936 switch (code1)
6938 case INTEGER_CST:
6939 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6940 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
6942 case REAL_CST:
6943 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
6945 case FIXED_CST:
6946 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
6948 case STRING_CST:
6949 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
6950 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
6951 TREE_STRING_LENGTH (t1)));
6953 case CONSTRUCTOR:
6955 unsigned HOST_WIDE_INT idx;
6956 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
6957 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
6959 if (vec_safe_length (v1) != vec_safe_length (v2))
6960 return false;
6962 for (idx = 0; idx < vec_safe_length (v1); ++idx)
6963 /* ??? Should we handle also fields here? */
6964 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
6965 return false;
6966 return true;
6969 case SAVE_EXPR:
6970 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6972 case CALL_EXPR:
6973 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
6974 if (cmp <= 0)
6975 return cmp;
6976 if (call_expr_nargs (t1) != call_expr_nargs (t2))
6977 return 0;
6979 const_tree arg1, arg2;
6980 const_call_expr_arg_iterator iter1, iter2;
6981 for (arg1 = first_const_call_expr_arg (t1, &iter1),
6982 arg2 = first_const_call_expr_arg (t2, &iter2);
6983 arg1 && arg2;
6984 arg1 = next_const_call_expr_arg (&iter1),
6985 arg2 = next_const_call_expr_arg (&iter2))
6987 cmp = simple_cst_equal (arg1, arg2);
6988 if (cmp <= 0)
6989 return cmp;
6991 return arg1 == arg2;
6994 case TARGET_EXPR:
6995 /* Special case: if either target is an unallocated VAR_DECL,
6996 it means that it's going to be unified with whatever the
6997 TARGET_EXPR is really supposed to initialize, so treat it
6998 as being equivalent to anything. */
6999 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
7000 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
7001 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
7002 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
7003 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
7004 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
7005 cmp = 1;
7006 else
7007 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7009 if (cmp <= 0)
7010 return cmp;
7012 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
7014 case WITH_CLEANUP_EXPR:
7015 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7016 if (cmp <= 0)
7017 return cmp;
7019 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
7021 case COMPONENT_REF:
7022 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
7023 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7025 return 0;
7027 case VAR_DECL:
7028 case PARM_DECL:
7029 case CONST_DECL:
7030 case FUNCTION_DECL:
7031 return 0;
7033 default:
7034 break;
7037 /* This general rule works for most tree codes. All exceptions should be
7038 handled above. If this is a language-specific tree code, we can't
7039 trust what might be in the operand, so say we don't know
7040 the situation. */
7041 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
7042 return -1;
7044 switch (TREE_CODE_CLASS (code1))
7046 case tcc_unary:
7047 case tcc_binary:
7048 case tcc_comparison:
7049 case tcc_expression:
7050 case tcc_reference:
7051 case tcc_statement:
7052 cmp = 1;
7053 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
7055 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
7056 if (cmp <= 0)
7057 return cmp;
7060 return cmp;
7062 default:
7063 return -1;
7067 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
7068 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
7069 than U, respectively. */
7072 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
7074 if (tree_int_cst_sgn (t) < 0)
7075 return -1;
7076 else if (TREE_INT_CST_HIGH (t) != 0)
7077 return 1;
7078 else if (TREE_INT_CST_LOW (t) == u)
7079 return 0;
7080 else if (TREE_INT_CST_LOW (t) < u)
7081 return -1;
7082 else
7083 return 1;
7086 /* Return true if SIZE represents a constant size that is in bounds of
7087 what the middle-end and the backend accepts (covering not more than
7088 half of the address-space). */
7090 bool
7091 valid_constant_size_p (const_tree size)
7093 if (! host_integerp (size, 1)
7094 || TREE_OVERFLOW (size)
7095 || tree_int_cst_sign_bit (size) != 0)
7096 return false;
7097 return true;
7100 /* Return the precision of the type, or for a complex or vector type the
7101 precision of the type of its elements. */
7103 unsigned int
7104 element_precision (const_tree type)
7106 enum tree_code code = TREE_CODE (type);
7107 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
7108 type = TREE_TYPE (type);
7110 return TYPE_PRECISION (type);
7113 /* Return true if CODE represents an associative tree code. Otherwise
7114 return false. */
7115 bool
7116 associative_tree_code (enum tree_code code)
7118 switch (code)
7120 case BIT_IOR_EXPR:
7121 case BIT_AND_EXPR:
7122 case BIT_XOR_EXPR:
7123 case PLUS_EXPR:
7124 case MULT_EXPR:
7125 case MIN_EXPR:
7126 case MAX_EXPR:
7127 return true;
7129 default:
7130 break;
7132 return false;
7135 /* Return true if CODE represents a commutative tree code. Otherwise
7136 return false. */
7137 bool
7138 commutative_tree_code (enum tree_code code)
7140 switch (code)
7142 case PLUS_EXPR:
7143 case MULT_EXPR:
7144 case MULT_HIGHPART_EXPR:
7145 case MIN_EXPR:
7146 case MAX_EXPR:
7147 case BIT_IOR_EXPR:
7148 case BIT_XOR_EXPR:
7149 case BIT_AND_EXPR:
7150 case NE_EXPR:
7151 case EQ_EXPR:
7152 case UNORDERED_EXPR:
7153 case ORDERED_EXPR:
7154 case UNEQ_EXPR:
7155 case LTGT_EXPR:
7156 case TRUTH_AND_EXPR:
7157 case TRUTH_XOR_EXPR:
7158 case TRUTH_OR_EXPR:
7159 case WIDEN_MULT_EXPR:
7160 case VEC_WIDEN_MULT_HI_EXPR:
7161 case VEC_WIDEN_MULT_LO_EXPR:
7162 case VEC_WIDEN_MULT_EVEN_EXPR:
7163 case VEC_WIDEN_MULT_ODD_EXPR:
7164 return true;
7166 default:
7167 break;
7169 return false;
7172 /* Return true if CODE represents a ternary tree code for which the
7173 first two operands are commutative. Otherwise return false. */
7174 bool
7175 commutative_ternary_tree_code (enum tree_code code)
7177 switch (code)
7179 case WIDEN_MULT_PLUS_EXPR:
7180 case WIDEN_MULT_MINUS_EXPR:
7181 return true;
7183 default:
7184 break;
7186 return false;
7189 /* Generate a hash value for an expression. This can be used iteratively
7190 by passing a previous result as the VAL argument.
7192 This function is intended to produce the same hash for expressions which
7193 would compare equal using operand_equal_p. */
7195 hashval_t
7196 iterative_hash_expr (const_tree t, hashval_t val)
7198 int i;
7199 enum tree_code code;
7200 char tclass;
7202 if (t == NULL_TREE)
7203 return iterative_hash_hashval_t (0, val);
7205 code = TREE_CODE (t);
7207 switch (code)
7209 /* Alas, constants aren't shared, so we can't rely on pointer
7210 identity. */
7211 case INTEGER_CST:
7212 val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
7213 return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
7214 case REAL_CST:
7216 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7218 return iterative_hash_hashval_t (val2, val);
7220 case FIXED_CST:
7222 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7224 return iterative_hash_hashval_t (val2, val);
7226 case STRING_CST:
7227 return iterative_hash (TREE_STRING_POINTER (t),
7228 TREE_STRING_LENGTH (t), val);
7229 case COMPLEX_CST:
7230 val = iterative_hash_expr (TREE_REALPART (t), val);
7231 return iterative_hash_expr (TREE_IMAGPART (t), val);
7232 case VECTOR_CST:
7234 unsigned i;
7235 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7236 val = iterative_hash_expr (VECTOR_CST_ELT (t, i), val);
7237 return val;
7239 case SSA_NAME:
7240 /* We can just compare by pointer. */
7241 return iterative_hash_host_wide_int (SSA_NAME_VERSION (t), val);
7242 case PLACEHOLDER_EXPR:
7243 /* The node itself doesn't matter. */
7244 return val;
7245 case TREE_LIST:
7246 /* A list of expressions, for a CALL_EXPR or as the elements of a
7247 VECTOR_CST. */
7248 for (; t; t = TREE_CHAIN (t))
7249 val = iterative_hash_expr (TREE_VALUE (t), val);
7250 return val;
7251 case CONSTRUCTOR:
7253 unsigned HOST_WIDE_INT idx;
7254 tree field, value;
7255 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7257 val = iterative_hash_expr (field, val);
7258 val = iterative_hash_expr (value, val);
7260 return val;
7262 case MEM_REF:
7264 /* The type of the second operand is relevant, except for
7265 its top-level qualifiers. */
7266 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (t, 1)));
7268 val = iterative_hash_object (TYPE_HASH (type), val);
7270 /* We could use the standard hash computation from this point
7271 on. */
7272 val = iterative_hash_object (code, val);
7273 val = iterative_hash_expr (TREE_OPERAND (t, 1), val);
7274 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7275 return val;
7277 case FUNCTION_DECL:
7278 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7279 Otherwise nodes that compare equal according to operand_equal_p might
7280 get different hash codes. However, don't do this for machine specific
7281 or front end builtins, since the function code is overloaded in those
7282 cases. */
7283 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7284 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7286 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7287 code = TREE_CODE (t);
7289 /* FALL THROUGH */
7290 default:
7291 tclass = TREE_CODE_CLASS (code);
7293 if (tclass == tcc_declaration)
7295 /* DECL's have a unique ID */
7296 val = iterative_hash_host_wide_int (DECL_UID (t), val);
7298 else
7300 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7302 val = iterative_hash_object (code, val);
7304 /* Don't hash the type, that can lead to having nodes which
7305 compare equal according to operand_equal_p, but which
7306 have different hash codes. */
7307 if (CONVERT_EXPR_CODE_P (code)
7308 || code == NON_LVALUE_EXPR)
7310 /* Make sure to include signness in the hash computation. */
7311 val += TYPE_UNSIGNED (TREE_TYPE (t));
7312 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7315 else if (commutative_tree_code (code))
7317 /* It's a commutative expression. We want to hash it the same
7318 however it appears. We do this by first hashing both operands
7319 and then rehashing based on the order of their independent
7320 hashes. */
7321 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
7322 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
7323 hashval_t t;
7325 if (one > two)
7326 t = one, one = two, two = t;
7328 val = iterative_hash_hashval_t (one, val);
7329 val = iterative_hash_hashval_t (two, val);
7331 else
7332 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7333 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
7335 return val;
7339 /* Generate a hash value for a pair of expressions. This can be used
7340 iteratively by passing a previous result as the VAL argument.
7342 The same hash value is always returned for a given pair of expressions,
7343 regardless of the order in which they are presented. This is useful in
7344 hashing the operands of commutative functions. */
7346 hashval_t
7347 iterative_hash_exprs_commutative (const_tree t1,
7348 const_tree t2, hashval_t val)
7350 hashval_t one = iterative_hash_expr (t1, 0);
7351 hashval_t two = iterative_hash_expr (t2, 0);
7352 hashval_t t;
7354 if (one > two)
7355 t = one, one = two, two = t;
7356 val = iterative_hash_hashval_t (one, val);
7357 val = iterative_hash_hashval_t (two, val);
7359 return val;
7362 /* Constructors for pointer, array and function types.
7363 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7364 constructed by language-dependent code, not here.) */
7366 /* Construct, lay out and return the type of pointers to TO_TYPE with
7367 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7368 reference all of memory. If such a type has already been
7369 constructed, reuse it. */
7371 tree
7372 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
7373 bool can_alias_all)
7375 tree t;
7377 if (to_type == error_mark_node)
7378 return error_mark_node;
7380 /* If the pointed-to type has the may_alias attribute set, force
7381 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7382 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7383 can_alias_all = true;
7385 /* In some cases, languages will have things that aren't a POINTER_TYPE
7386 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7387 In that case, return that type without regard to the rest of our
7388 operands.
7390 ??? This is a kludge, but consistent with the way this function has
7391 always operated and there doesn't seem to be a good way to avoid this
7392 at the moment. */
7393 if (TYPE_POINTER_TO (to_type) != 0
7394 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7395 return TYPE_POINTER_TO (to_type);
7397 /* First, if we already have a type for pointers to TO_TYPE and it's
7398 the proper mode, use it. */
7399 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7400 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7401 return t;
7403 t = make_node (POINTER_TYPE);
7405 TREE_TYPE (t) = to_type;
7406 SET_TYPE_MODE (t, mode);
7407 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7408 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7409 TYPE_POINTER_TO (to_type) = t;
7411 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7412 SET_TYPE_STRUCTURAL_EQUALITY (t);
7413 else if (TYPE_CANONICAL (to_type) != to_type)
7414 TYPE_CANONICAL (t)
7415 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7416 mode, can_alias_all);
7418 /* Lay out the type. This function has many callers that are concerned
7419 with expression-construction, and this simplifies them all. */
7420 layout_type (t);
7422 return t;
7425 /* By default build pointers in ptr_mode. */
7427 tree
7428 build_pointer_type (tree to_type)
7430 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7431 : TYPE_ADDR_SPACE (to_type);
7432 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7433 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7436 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7438 tree
7439 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
7440 bool can_alias_all)
7442 tree t;
7444 if (to_type == error_mark_node)
7445 return error_mark_node;
7447 /* If the pointed-to type has the may_alias attribute set, force
7448 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7449 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7450 can_alias_all = true;
7452 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7453 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7454 In that case, return that type without regard to the rest of our
7455 operands.
7457 ??? This is a kludge, but consistent with the way this function has
7458 always operated and there doesn't seem to be a good way to avoid this
7459 at the moment. */
7460 if (TYPE_REFERENCE_TO (to_type) != 0
7461 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7462 return TYPE_REFERENCE_TO (to_type);
7464 /* First, if we already have a type for pointers to TO_TYPE and it's
7465 the proper mode, use it. */
7466 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7467 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7468 return t;
7470 t = make_node (REFERENCE_TYPE);
7472 TREE_TYPE (t) = to_type;
7473 SET_TYPE_MODE (t, mode);
7474 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7475 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7476 TYPE_REFERENCE_TO (to_type) = t;
7478 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7479 SET_TYPE_STRUCTURAL_EQUALITY (t);
7480 else if (TYPE_CANONICAL (to_type) != to_type)
7481 TYPE_CANONICAL (t)
7482 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7483 mode, can_alias_all);
7485 layout_type (t);
7487 return t;
7491 /* Build the node for the type of references-to-TO_TYPE by default
7492 in ptr_mode. */
7494 tree
7495 build_reference_type (tree to_type)
7497 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7498 : TYPE_ADDR_SPACE (to_type);
7499 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7500 return build_reference_type_for_mode (to_type, pointer_mode, false);
7503 /* Build a type that is compatible with t but has no cv quals anywhere
7504 in its type, thus
7506 const char *const *const * -> char ***. */
7508 tree
7509 build_type_no_quals (tree t)
7511 switch (TREE_CODE (t))
7513 case POINTER_TYPE:
7514 return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7515 TYPE_MODE (t),
7516 TYPE_REF_CAN_ALIAS_ALL (t));
7517 case REFERENCE_TYPE:
7518 return
7519 build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7520 TYPE_MODE (t),
7521 TYPE_REF_CAN_ALIAS_ALL (t));
7522 default:
7523 return TYPE_MAIN_VARIANT (t);
7527 #define MAX_INT_CACHED_PREC \
7528 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7529 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7531 /* Builds a signed or unsigned integer type of precision PRECISION.
7532 Used for C bitfields whose precision does not match that of
7533 built-in target types. */
7534 tree
7535 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7536 int unsignedp)
7538 tree itype, ret;
7540 if (unsignedp)
7541 unsignedp = MAX_INT_CACHED_PREC + 1;
7543 if (precision <= MAX_INT_CACHED_PREC)
7545 itype = nonstandard_integer_type_cache[precision + unsignedp];
7546 if (itype)
7547 return itype;
7550 itype = make_node (INTEGER_TYPE);
7551 TYPE_PRECISION (itype) = precision;
7553 if (unsignedp)
7554 fixup_unsigned_type (itype);
7555 else
7556 fixup_signed_type (itype);
7558 ret = itype;
7559 if (host_integerp (TYPE_MAX_VALUE (itype), 1))
7560 ret = type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
7561 if (precision <= MAX_INT_CACHED_PREC)
7562 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7564 return ret;
7567 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7568 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7569 is true, reuse such a type that has already been constructed. */
7571 static tree
7572 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7574 tree itype = make_node (INTEGER_TYPE);
7575 hashval_t hashcode = 0;
7577 TREE_TYPE (itype) = type;
7579 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7580 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7582 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7583 SET_TYPE_MODE (itype, TYPE_MODE (type));
7584 TYPE_SIZE (itype) = TYPE_SIZE (type);
7585 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7586 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7587 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7589 if (!shared)
7590 return itype;
7592 if ((TYPE_MIN_VALUE (itype)
7593 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7594 || (TYPE_MAX_VALUE (itype)
7595 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7597 /* Since we cannot reliably merge this type, we need to compare it using
7598 structural equality checks. */
7599 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7600 return itype;
7603 hashcode = iterative_hash_expr (TYPE_MIN_VALUE (itype), hashcode);
7604 hashcode = iterative_hash_expr (TYPE_MAX_VALUE (itype), hashcode);
7605 hashcode = iterative_hash_hashval_t (TYPE_HASH (type), hashcode);
7606 itype = type_hash_canon (hashcode, itype);
7608 return itype;
7611 /* Wrapper around build_range_type_1 with SHARED set to true. */
7613 tree
7614 build_range_type (tree type, tree lowval, tree highval)
7616 return build_range_type_1 (type, lowval, highval, true);
7619 /* Wrapper around build_range_type_1 with SHARED set to false. */
7621 tree
7622 build_nonshared_range_type (tree type, tree lowval, tree highval)
7624 return build_range_type_1 (type, lowval, highval, false);
7627 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7628 MAXVAL should be the maximum value in the domain
7629 (one less than the length of the array).
7631 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7632 We don't enforce this limit, that is up to caller (e.g. language front end).
7633 The limit exists because the result is a signed type and we don't handle
7634 sizes that use more than one HOST_WIDE_INT. */
7636 tree
7637 build_index_type (tree maxval)
7639 return build_range_type (sizetype, size_zero_node, maxval);
7642 /* Return true if the debug information for TYPE, a subtype, should be emitted
7643 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7644 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7645 debug info and doesn't reflect the source code. */
7647 bool
7648 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7650 tree base_type = TREE_TYPE (type), low, high;
7652 /* Subrange types have a base type which is an integral type. */
7653 if (!INTEGRAL_TYPE_P (base_type))
7654 return false;
7656 /* Get the real bounds of the subtype. */
7657 if (lang_hooks.types.get_subrange_bounds)
7658 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7659 else
7661 low = TYPE_MIN_VALUE (type);
7662 high = TYPE_MAX_VALUE (type);
7665 /* If the type and its base type have the same representation and the same
7666 name, then the type is not a subrange but a copy of the base type. */
7667 if ((TREE_CODE (base_type) == INTEGER_TYPE
7668 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7669 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7670 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7671 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type)))
7673 tree type_name = TYPE_NAME (type);
7674 tree base_type_name = TYPE_NAME (base_type);
7676 if (type_name && TREE_CODE (type_name) == TYPE_DECL)
7677 type_name = DECL_NAME (type_name);
7679 if (base_type_name && TREE_CODE (base_type_name) == TYPE_DECL)
7680 base_type_name = DECL_NAME (base_type_name);
7682 if (type_name == base_type_name)
7683 return false;
7686 if (lowval)
7687 *lowval = low;
7688 if (highval)
7689 *highval = high;
7690 return true;
7693 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7694 and number of elements specified by the range of values of INDEX_TYPE.
7695 If SHARED is true, reuse such a type that has already been constructed. */
7697 static tree
7698 build_array_type_1 (tree elt_type, tree index_type, bool shared)
7700 tree t;
7702 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7704 error ("arrays of functions are not meaningful");
7705 elt_type = integer_type_node;
7708 t = make_node (ARRAY_TYPE);
7709 TREE_TYPE (t) = elt_type;
7710 TYPE_DOMAIN (t) = index_type;
7711 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7712 layout_type (t);
7714 /* If the element type is incomplete at this point we get marked for
7715 structural equality. Do not record these types in the canonical
7716 type hashtable. */
7717 if (TYPE_STRUCTURAL_EQUALITY_P (t))
7718 return t;
7720 if (shared)
7722 hashval_t hashcode = iterative_hash_object (TYPE_HASH (elt_type), 0);
7723 if (index_type)
7724 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
7725 t = type_hash_canon (hashcode, t);
7728 if (TYPE_CANONICAL (t) == t)
7730 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7731 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7732 SET_TYPE_STRUCTURAL_EQUALITY (t);
7733 else if (TYPE_CANONICAL (elt_type) != elt_type
7734 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7735 TYPE_CANONICAL (t)
7736 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7737 index_type
7738 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7739 shared);
7742 return t;
7745 /* Wrapper around build_array_type_1 with SHARED set to true. */
7747 tree
7748 build_array_type (tree elt_type, tree index_type)
7750 return build_array_type_1 (elt_type, index_type, true);
7753 /* Wrapper around build_array_type_1 with SHARED set to false. */
7755 tree
7756 build_nonshared_array_type (tree elt_type, tree index_type)
7758 return build_array_type_1 (elt_type, index_type, false);
7761 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7762 sizetype. */
7764 tree
7765 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
7767 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7770 /* Recursively examines the array elements of TYPE, until a non-array
7771 element type is found. */
7773 tree
7774 strip_array_types (tree type)
7776 while (TREE_CODE (type) == ARRAY_TYPE)
7777 type = TREE_TYPE (type);
7779 return type;
7782 /* Computes the canonical argument types from the argument type list
7783 ARGTYPES.
7785 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7786 on entry to this function, or if any of the ARGTYPES are
7787 structural.
7789 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7790 true on entry to this function, or if any of the ARGTYPES are
7791 non-canonical.
7793 Returns a canonical argument list, which may be ARGTYPES when the
7794 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7795 true) or would not differ from ARGTYPES. */
7797 static tree
7798 maybe_canonicalize_argtypes(tree argtypes,
7799 bool *any_structural_p,
7800 bool *any_noncanonical_p)
7802 tree arg;
7803 bool any_noncanonical_argtypes_p = false;
7805 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7807 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7808 /* Fail gracefully by stating that the type is structural. */
7809 *any_structural_p = true;
7810 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7811 *any_structural_p = true;
7812 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7813 || TREE_PURPOSE (arg))
7814 /* If the argument has a default argument, we consider it
7815 non-canonical even though the type itself is canonical.
7816 That way, different variants of function and method types
7817 with default arguments will all point to the variant with
7818 no defaults as their canonical type. */
7819 any_noncanonical_argtypes_p = true;
7822 if (*any_structural_p)
7823 return argtypes;
7825 if (any_noncanonical_argtypes_p)
7827 /* Build the canonical list of argument types. */
7828 tree canon_argtypes = NULL_TREE;
7829 bool is_void = false;
7831 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7833 if (arg == void_list_node)
7834 is_void = true;
7835 else
7836 canon_argtypes = tree_cons (NULL_TREE,
7837 TYPE_CANONICAL (TREE_VALUE (arg)),
7838 canon_argtypes);
7841 canon_argtypes = nreverse (canon_argtypes);
7842 if (is_void)
7843 canon_argtypes = chainon (canon_argtypes, void_list_node);
7845 /* There is a non-canonical type. */
7846 *any_noncanonical_p = true;
7847 return canon_argtypes;
7850 /* The canonical argument types are the same as ARGTYPES. */
7851 return argtypes;
7854 /* Construct, lay out and return
7855 the type of functions returning type VALUE_TYPE
7856 given arguments of types ARG_TYPES.
7857 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7858 are data type nodes for the arguments of the function.
7859 If such a type has already been constructed, reuse it. */
7861 tree
7862 build_function_type (tree value_type, tree arg_types)
7864 tree t;
7865 hashval_t hashcode = 0;
7866 bool any_structural_p, any_noncanonical_p;
7867 tree canon_argtypes;
7869 if (TREE_CODE (value_type) == FUNCTION_TYPE)
7871 error ("function return type cannot be function");
7872 value_type = integer_type_node;
7875 /* Make a node of the sort we want. */
7876 t = make_node (FUNCTION_TYPE);
7877 TREE_TYPE (t) = value_type;
7878 TYPE_ARG_TYPES (t) = arg_types;
7880 /* If we already have such a type, use the old one. */
7881 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
7882 hashcode = type_hash_list (arg_types, hashcode);
7883 t = type_hash_canon (hashcode, t);
7885 /* Set up the canonical type. */
7886 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7887 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7888 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
7889 &any_structural_p,
7890 &any_noncanonical_p);
7891 if (any_structural_p)
7892 SET_TYPE_STRUCTURAL_EQUALITY (t);
7893 else if (any_noncanonical_p)
7894 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
7895 canon_argtypes);
7897 if (!COMPLETE_TYPE_P (t))
7898 layout_type (t);
7899 return t;
7902 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP and the
7903 return value if SKIP_RETURN is true. */
7905 static tree
7906 build_function_type_skip_args (tree orig_type, bitmap args_to_skip,
7907 bool skip_return)
7909 tree new_type = NULL;
7910 tree args, new_args = NULL, t;
7911 tree new_reversed;
7912 int i = 0;
7914 for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
7915 args = TREE_CHAIN (args), i++)
7916 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
7917 new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
7919 new_reversed = nreverse (new_args);
7920 if (args)
7922 if (new_reversed)
7923 TREE_CHAIN (new_args) = void_list_node;
7924 else
7925 new_reversed = void_list_node;
7928 /* Use copy_node to preserve as much as possible from original type
7929 (debug info, attribute lists etc.)
7930 Exception is METHOD_TYPEs must have THIS argument.
7931 When we are asked to remove it, we need to build new FUNCTION_TYPE
7932 instead. */
7933 if (TREE_CODE (orig_type) != METHOD_TYPE
7934 || !args_to_skip
7935 || !bitmap_bit_p (args_to_skip, 0))
7937 new_type = build_distinct_type_copy (orig_type);
7938 TYPE_ARG_TYPES (new_type) = new_reversed;
7940 else
7942 new_type
7943 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
7944 new_reversed));
7945 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
7948 if (skip_return)
7949 TREE_TYPE (new_type) = void_type_node;
7951 /* This is a new type, not a copy of an old type. Need to reassociate
7952 variants. We can handle everything except the main variant lazily. */
7953 t = TYPE_MAIN_VARIANT (orig_type);
7954 if (t != orig_type)
7956 t = build_function_type_skip_args (t, args_to_skip, skip_return);
7957 TYPE_MAIN_VARIANT (new_type) = t;
7958 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
7959 TYPE_NEXT_VARIANT (t) = new_type;
7961 else
7963 TYPE_MAIN_VARIANT (new_type) = new_type;
7964 TYPE_NEXT_VARIANT (new_type) = NULL;
7967 return new_type;
7970 /* Build variant of function decl ORIG_DECL skipping ARGS_TO_SKIP and the
7971 return value if SKIP_RETURN is true.
7973 Arguments from DECL_ARGUMENTS list can't be removed now, since they are
7974 linked by TREE_CHAIN directly. The caller is responsible for eliminating
7975 them when they are being duplicated (i.e. copy_arguments_for_versioning). */
7977 tree
7978 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip,
7979 bool skip_return)
7981 tree new_decl = copy_node (orig_decl);
7982 tree new_type;
7984 new_type = TREE_TYPE (orig_decl);
7985 if (prototype_p (new_type)
7986 || (skip_return && !VOID_TYPE_P (TREE_TYPE (new_type))))
7987 new_type
7988 = build_function_type_skip_args (new_type, args_to_skip, skip_return);
7989 TREE_TYPE (new_decl) = new_type;
7991 /* For declarations setting DECL_VINDEX (i.e. methods)
7992 we expect first argument to be THIS pointer. */
7993 if (args_to_skip && bitmap_bit_p (args_to_skip, 0))
7994 DECL_VINDEX (new_decl) = NULL_TREE;
7996 /* When signature changes, we need to clear builtin info. */
7997 if (DECL_BUILT_IN (new_decl)
7998 && args_to_skip
7999 && !bitmap_empty_p (args_to_skip))
8001 DECL_BUILT_IN_CLASS (new_decl) = NOT_BUILT_IN;
8002 DECL_FUNCTION_CODE (new_decl) = (enum built_in_function) 0;
8004 return new_decl;
8007 /* Build a function type. The RETURN_TYPE is the type returned by the
8008 function. If VAARGS is set, no void_type_node is appended to the
8009 the list. ARGP must be always be terminated be a NULL_TREE. */
8011 static tree
8012 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
8014 tree t, args, last;
8016 t = va_arg (argp, tree);
8017 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
8018 args = tree_cons (NULL_TREE, t, args);
8020 if (vaargs)
8022 last = args;
8023 if (args != NULL_TREE)
8024 args = nreverse (args);
8025 gcc_assert (last != void_list_node);
8027 else if (args == NULL_TREE)
8028 args = void_list_node;
8029 else
8031 last = args;
8032 args = nreverse (args);
8033 TREE_CHAIN (last) = void_list_node;
8035 args = build_function_type (return_type, args);
8037 return args;
8040 /* Build a function type. The RETURN_TYPE is the type returned by the
8041 function. If additional arguments are provided, they are
8042 additional argument types. The list of argument types must always
8043 be terminated by NULL_TREE. */
8045 tree
8046 build_function_type_list (tree return_type, ...)
8048 tree args;
8049 va_list p;
8051 va_start (p, return_type);
8052 args = build_function_type_list_1 (false, return_type, p);
8053 va_end (p);
8054 return args;
8057 /* Build a variable argument function type. The RETURN_TYPE is the
8058 type returned by the function. If additional arguments are provided,
8059 they are additional argument types. The list of argument types must
8060 always be terminated by NULL_TREE. */
8062 tree
8063 build_varargs_function_type_list (tree return_type, ...)
8065 tree args;
8066 va_list p;
8068 va_start (p, return_type);
8069 args = build_function_type_list_1 (true, return_type, p);
8070 va_end (p);
8072 return args;
8075 /* Build a function type. RETURN_TYPE is the type returned by the
8076 function; VAARGS indicates whether the function takes varargs. The
8077 function takes N named arguments, the types of which are provided in
8078 ARG_TYPES. */
8080 static tree
8081 build_function_type_array_1 (bool vaargs, tree return_type, int n,
8082 tree *arg_types)
8084 int i;
8085 tree t = vaargs ? NULL_TREE : void_list_node;
8087 for (i = n - 1; i >= 0; i--)
8088 t = tree_cons (NULL_TREE, arg_types[i], t);
8090 return build_function_type (return_type, t);
8093 /* Build a function type. RETURN_TYPE is the type returned by the
8094 function. The function takes N named arguments, the types of which
8095 are provided in ARG_TYPES. */
8097 tree
8098 build_function_type_array (tree return_type, int n, tree *arg_types)
8100 return build_function_type_array_1 (false, return_type, n, arg_types);
8103 /* Build a variable argument function type. RETURN_TYPE is the type
8104 returned by the function. The function takes N named arguments, the
8105 types of which are provided in ARG_TYPES. */
8107 tree
8108 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8110 return build_function_type_array_1 (true, return_type, n, arg_types);
8113 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8114 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8115 for the method. An implicit additional parameter (of type
8116 pointer-to-BASETYPE) is added to the ARGTYPES. */
8118 tree
8119 build_method_type_directly (tree basetype,
8120 tree rettype,
8121 tree argtypes)
8123 tree t;
8124 tree ptype;
8125 int hashcode = 0;
8126 bool any_structural_p, any_noncanonical_p;
8127 tree canon_argtypes;
8129 /* Make a node of the sort we want. */
8130 t = make_node (METHOD_TYPE);
8132 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8133 TREE_TYPE (t) = rettype;
8134 ptype = build_pointer_type (basetype);
8136 /* The actual arglist for this function includes a "hidden" argument
8137 which is "this". Put it into the list of argument types. */
8138 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8139 TYPE_ARG_TYPES (t) = argtypes;
8141 /* If we already have such a type, use the old one. */
8142 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8143 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
8144 hashcode = type_hash_list (argtypes, hashcode);
8145 t = type_hash_canon (hashcode, t);
8147 /* Set up the canonical type. */
8148 any_structural_p
8149 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8150 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8151 any_noncanonical_p
8152 = (TYPE_CANONICAL (basetype) != basetype
8153 || TYPE_CANONICAL (rettype) != rettype);
8154 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8155 &any_structural_p,
8156 &any_noncanonical_p);
8157 if (any_structural_p)
8158 SET_TYPE_STRUCTURAL_EQUALITY (t);
8159 else if (any_noncanonical_p)
8160 TYPE_CANONICAL (t)
8161 = build_method_type_directly (TYPE_CANONICAL (basetype),
8162 TYPE_CANONICAL (rettype),
8163 canon_argtypes);
8164 if (!COMPLETE_TYPE_P (t))
8165 layout_type (t);
8167 return t;
8170 /* Construct, lay out and return the type of methods belonging to class
8171 BASETYPE and whose arguments and values are described by TYPE.
8172 If that type exists already, reuse it.
8173 TYPE must be a FUNCTION_TYPE node. */
8175 tree
8176 build_method_type (tree basetype, tree type)
8178 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8180 return build_method_type_directly (basetype,
8181 TREE_TYPE (type),
8182 TYPE_ARG_TYPES (type));
8185 /* Construct, lay out and return the type of offsets to a value
8186 of type TYPE, within an object of type BASETYPE.
8187 If a suitable offset type exists already, reuse it. */
8189 tree
8190 build_offset_type (tree basetype, tree type)
8192 tree t;
8193 hashval_t hashcode = 0;
8195 /* Make a node of the sort we want. */
8196 t = make_node (OFFSET_TYPE);
8198 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8199 TREE_TYPE (t) = type;
8201 /* If we already have such a type, use the old one. */
8202 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8203 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
8204 t = type_hash_canon (hashcode, t);
8206 if (!COMPLETE_TYPE_P (t))
8207 layout_type (t);
8209 if (TYPE_CANONICAL (t) == t)
8211 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8212 || TYPE_STRUCTURAL_EQUALITY_P (type))
8213 SET_TYPE_STRUCTURAL_EQUALITY (t);
8214 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8215 || TYPE_CANONICAL (type) != type)
8216 TYPE_CANONICAL (t)
8217 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8218 TYPE_CANONICAL (type));
8221 return t;
8224 /* Create a complex type whose components are COMPONENT_TYPE. */
8226 tree
8227 build_complex_type (tree component_type)
8229 tree t;
8230 hashval_t hashcode;
8232 gcc_assert (INTEGRAL_TYPE_P (component_type)
8233 || SCALAR_FLOAT_TYPE_P (component_type)
8234 || FIXED_POINT_TYPE_P (component_type));
8236 /* Make a node of the sort we want. */
8237 t = make_node (COMPLEX_TYPE);
8239 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8241 /* If we already have such a type, use the old one. */
8242 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
8243 t = type_hash_canon (hashcode, t);
8245 if (!COMPLETE_TYPE_P (t))
8246 layout_type (t);
8248 if (TYPE_CANONICAL (t) == t)
8250 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8251 SET_TYPE_STRUCTURAL_EQUALITY (t);
8252 else if (TYPE_CANONICAL (component_type) != component_type)
8253 TYPE_CANONICAL (t)
8254 = build_complex_type (TYPE_CANONICAL (component_type));
8257 /* We need to create a name, since complex is a fundamental type. */
8258 if (! TYPE_NAME (t))
8260 const char *name;
8261 if (component_type == char_type_node)
8262 name = "complex char";
8263 else if (component_type == signed_char_type_node)
8264 name = "complex signed char";
8265 else if (component_type == unsigned_char_type_node)
8266 name = "complex unsigned char";
8267 else if (component_type == short_integer_type_node)
8268 name = "complex short int";
8269 else if (component_type == short_unsigned_type_node)
8270 name = "complex short unsigned int";
8271 else if (component_type == integer_type_node)
8272 name = "complex int";
8273 else if (component_type == unsigned_type_node)
8274 name = "complex unsigned int";
8275 else if (component_type == long_integer_type_node)
8276 name = "complex long int";
8277 else if (component_type == long_unsigned_type_node)
8278 name = "complex long unsigned int";
8279 else if (component_type == long_long_integer_type_node)
8280 name = "complex long long int";
8281 else if (component_type == long_long_unsigned_type_node)
8282 name = "complex long long unsigned int";
8283 else
8284 name = 0;
8286 if (name != 0)
8287 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8288 get_identifier (name), t);
8291 return build_qualified_type (t, TYPE_QUALS (component_type));
8294 /* If TYPE is a real or complex floating-point type and the target
8295 does not directly support arithmetic on TYPE then return the wider
8296 type to be used for arithmetic on TYPE. Otherwise, return
8297 NULL_TREE. */
8299 tree
8300 excess_precision_type (tree type)
8302 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8304 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8305 switch (TREE_CODE (type))
8307 case REAL_TYPE:
8308 switch (flt_eval_method)
8310 case 1:
8311 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8312 return double_type_node;
8313 break;
8314 case 2:
8315 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8316 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8317 return long_double_type_node;
8318 break;
8319 default:
8320 gcc_unreachable ();
8322 break;
8323 case COMPLEX_TYPE:
8324 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8325 return NULL_TREE;
8326 switch (flt_eval_method)
8328 case 1:
8329 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8330 return complex_double_type_node;
8331 break;
8332 case 2:
8333 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8334 || (TYPE_MODE (TREE_TYPE (type))
8335 == TYPE_MODE (double_type_node)))
8336 return complex_long_double_type_node;
8337 break;
8338 default:
8339 gcc_unreachable ();
8341 break;
8342 default:
8343 break;
8346 return NULL_TREE;
8349 /* Return OP, stripped of any conversions to wider types as much as is safe.
8350 Converting the value back to OP's type makes a value equivalent to OP.
8352 If FOR_TYPE is nonzero, we return a value which, if converted to
8353 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8355 OP must have integer, real or enumeral type. Pointers are not allowed!
8357 There are some cases where the obvious value we could return
8358 would regenerate to OP if converted to OP's type,
8359 but would not extend like OP to wider types.
8360 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8361 For example, if OP is (unsigned short)(signed char)-1,
8362 we avoid returning (signed char)-1 if FOR_TYPE is int,
8363 even though extending that to an unsigned short would regenerate OP,
8364 since the result of extending (signed char)-1 to (int)
8365 is different from (int) OP. */
8367 tree
8368 get_unwidened (tree op, tree for_type)
8370 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8371 tree type = TREE_TYPE (op);
8372 unsigned final_prec
8373 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8374 int uns
8375 = (for_type != 0 && for_type != type
8376 && final_prec > TYPE_PRECISION (type)
8377 && TYPE_UNSIGNED (type));
8378 tree win = op;
8380 while (CONVERT_EXPR_P (op))
8382 int bitschange;
8384 /* TYPE_PRECISION on vector types has different meaning
8385 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8386 so avoid them here. */
8387 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8388 break;
8390 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8391 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8393 /* Truncations are many-one so cannot be removed.
8394 Unless we are later going to truncate down even farther. */
8395 if (bitschange < 0
8396 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8397 break;
8399 /* See what's inside this conversion. If we decide to strip it,
8400 we will set WIN. */
8401 op = TREE_OPERAND (op, 0);
8403 /* If we have not stripped any zero-extensions (uns is 0),
8404 we can strip any kind of extension.
8405 If we have previously stripped a zero-extension,
8406 only zero-extensions can safely be stripped.
8407 Any extension can be stripped if the bits it would produce
8408 are all going to be discarded later by truncating to FOR_TYPE. */
8410 if (bitschange > 0)
8412 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8413 win = op;
8414 /* TYPE_UNSIGNED says whether this is a zero-extension.
8415 Let's avoid computing it if it does not affect WIN
8416 and if UNS will not be needed again. */
8417 if ((uns
8418 || CONVERT_EXPR_P (op))
8419 && TYPE_UNSIGNED (TREE_TYPE (op)))
8421 uns = 1;
8422 win = op;
8427 /* If we finally reach a constant see if it fits in for_type and
8428 in that case convert it. */
8429 if (for_type
8430 && TREE_CODE (win) == INTEGER_CST
8431 && TREE_TYPE (win) != for_type
8432 && int_fits_type_p (win, for_type))
8433 win = fold_convert (for_type, win);
8435 return win;
8438 /* Return OP or a simpler expression for a narrower value
8439 which can be sign-extended or zero-extended to give back OP.
8440 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8441 or 0 if the value should be sign-extended. */
8443 tree
8444 get_narrower (tree op, int *unsignedp_ptr)
8446 int uns = 0;
8447 int first = 1;
8448 tree win = op;
8449 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8451 while (TREE_CODE (op) == NOP_EXPR)
8453 int bitschange
8454 = (TYPE_PRECISION (TREE_TYPE (op))
8455 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8457 /* Truncations are many-one so cannot be removed. */
8458 if (bitschange < 0)
8459 break;
8461 /* See what's inside this conversion. If we decide to strip it,
8462 we will set WIN. */
8464 if (bitschange > 0)
8466 op = TREE_OPERAND (op, 0);
8467 /* An extension: the outermost one can be stripped,
8468 but remember whether it is zero or sign extension. */
8469 if (first)
8470 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8471 /* Otherwise, if a sign extension has been stripped,
8472 only sign extensions can now be stripped;
8473 if a zero extension has been stripped, only zero-extensions. */
8474 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8475 break;
8476 first = 0;
8478 else /* bitschange == 0 */
8480 /* A change in nominal type can always be stripped, but we must
8481 preserve the unsignedness. */
8482 if (first)
8483 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8484 first = 0;
8485 op = TREE_OPERAND (op, 0);
8486 /* Keep trying to narrow, but don't assign op to win if it
8487 would turn an integral type into something else. */
8488 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8489 continue;
8492 win = op;
8495 if (TREE_CODE (op) == COMPONENT_REF
8496 /* Since type_for_size always gives an integer type. */
8497 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8498 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8499 /* Ensure field is laid out already. */
8500 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8501 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
8503 unsigned HOST_WIDE_INT innerprec
8504 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
8505 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8506 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8507 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8509 /* We can get this structure field in a narrower type that fits it,
8510 but the resulting extension to its nominal type (a fullword type)
8511 must satisfy the same conditions as for other extensions.
8513 Do this only for fields that are aligned (not bit-fields),
8514 because when bit-field insns will be used there is no
8515 advantage in doing this. */
8517 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8518 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8519 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8520 && type != 0)
8522 if (first)
8523 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8524 win = fold_convert (type, op);
8528 *unsignedp_ptr = uns;
8529 return win;
8532 /* Returns true if integer constant C has a value that is permissible
8533 for type TYPE (an INTEGER_TYPE). */
8535 bool
8536 int_fits_type_p (const_tree c, const_tree type)
8538 tree type_low_bound, type_high_bound;
8539 bool ok_for_low_bound, ok_for_high_bound, unsc;
8540 double_int dc, dd;
8542 dc = tree_to_double_int (c);
8543 unsc = TYPE_UNSIGNED (TREE_TYPE (c));
8545 retry:
8546 type_low_bound = TYPE_MIN_VALUE (type);
8547 type_high_bound = TYPE_MAX_VALUE (type);
8549 /* If at least one bound of the type is a constant integer, we can check
8550 ourselves and maybe make a decision. If no such decision is possible, but
8551 this type is a subtype, try checking against that. Otherwise, use
8552 double_int_fits_to_tree_p, which checks against the precision.
8554 Compute the status for each possibly constant bound, and return if we see
8555 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8556 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8557 for "constant known to fit". */
8559 /* Check if c >= type_low_bound. */
8560 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8562 dd = tree_to_double_int (type_low_bound);
8563 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
8565 int c_neg = (!unsc && dc.is_negative ());
8566 int t_neg = (unsc && dd.is_negative ());
8568 if (c_neg && !t_neg)
8569 return false;
8570 if ((c_neg || !t_neg) && dc.ult (dd))
8571 return false;
8573 else if (dc.cmp (dd, unsc) < 0)
8574 return false;
8575 ok_for_low_bound = true;
8577 else
8578 ok_for_low_bound = false;
8580 /* Check if c <= type_high_bound. */
8581 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8583 dd = tree_to_double_int (type_high_bound);
8584 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
8586 int c_neg = (!unsc && dc.is_negative ());
8587 int t_neg = (unsc && dd.is_negative ());
8589 if (t_neg && !c_neg)
8590 return false;
8591 if ((t_neg || !c_neg) && dc.ugt (dd))
8592 return false;
8594 else if (dc.cmp (dd, unsc) > 0)
8595 return false;
8596 ok_for_high_bound = true;
8598 else
8599 ok_for_high_bound = false;
8601 /* If the constant fits both bounds, the result is known. */
8602 if (ok_for_low_bound && ok_for_high_bound)
8603 return true;
8605 /* Perform some generic filtering which may allow making a decision
8606 even if the bounds are not constant. First, negative integers
8607 never fit in unsigned types, */
8608 if (TYPE_UNSIGNED (type) && !unsc && dc.is_negative ())
8609 return false;
8611 /* Second, narrower types always fit in wider ones. */
8612 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8613 return true;
8615 /* Third, unsigned integers with top bit set never fit signed types. */
8616 if (! TYPE_UNSIGNED (type) && unsc)
8618 int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (c))) - 1;
8619 if (prec < HOST_BITS_PER_WIDE_INT)
8621 if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
8622 return false;
8624 else if (((((unsigned HOST_WIDE_INT) 1)
8625 << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
8626 return false;
8629 /* If we haven't been able to decide at this point, there nothing more we
8630 can check ourselves here. Look at the base type if we have one and it
8631 has the same precision. */
8632 if (TREE_CODE (type) == INTEGER_TYPE
8633 && TREE_TYPE (type) != 0
8634 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8636 type = TREE_TYPE (type);
8637 goto retry;
8640 /* Or to double_int_fits_to_tree_p, if nothing else. */
8641 return double_int_fits_to_tree_p (type, dc);
8644 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8645 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8646 represented (assuming two's-complement arithmetic) within the bit
8647 precision of the type are returned instead. */
8649 void
8650 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8652 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8653 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8654 mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
8655 TYPE_UNSIGNED (type));
8656 else
8658 if (TYPE_UNSIGNED (type))
8659 mpz_set_ui (min, 0);
8660 else
8662 double_int mn;
8663 mn = double_int::mask (TYPE_PRECISION (type) - 1);
8664 mn = (mn + double_int_one).sext (TYPE_PRECISION (type));
8665 mpz_set_double_int (min, mn, false);
8669 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8670 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8671 mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
8672 TYPE_UNSIGNED (type));
8673 else
8675 if (TYPE_UNSIGNED (type))
8676 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)),
8677 true);
8678 else
8679 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type) - 1),
8680 true);
8684 /* Return true if VAR is an automatic variable defined in function FN. */
8686 bool
8687 auto_var_in_fn_p (const_tree var, const_tree fn)
8689 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8690 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8691 || TREE_CODE (var) == PARM_DECL)
8692 && ! TREE_STATIC (var))
8693 || TREE_CODE (var) == LABEL_DECL
8694 || TREE_CODE (var) == RESULT_DECL));
8697 /* Subprogram of following function. Called by walk_tree.
8699 Return *TP if it is an automatic variable or parameter of the
8700 function passed in as DATA. */
8702 static tree
8703 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8705 tree fn = (tree) data;
8707 if (TYPE_P (*tp))
8708 *walk_subtrees = 0;
8710 else if (DECL_P (*tp)
8711 && auto_var_in_fn_p (*tp, fn))
8712 return *tp;
8714 return NULL_TREE;
8717 /* Returns true if T is, contains, or refers to a type with variable
8718 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8719 arguments, but not the return type. If FN is nonzero, only return
8720 true if a modifier of the type or position of FN is a variable or
8721 parameter inside FN.
8723 This concept is more general than that of C99 'variably modified types':
8724 in C99, a struct type is never variably modified because a VLA may not
8725 appear as a structure member. However, in GNU C code like:
8727 struct S { int i[f()]; };
8729 is valid, and other languages may define similar constructs. */
8731 bool
8732 variably_modified_type_p (tree type, tree fn)
8734 tree t;
8736 /* Test if T is either variable (if FN is zero) or an expression containing
8737 a variable in FN. If TYPE isn't gimplified, return true also if
8738 gimplify_one_sizepos would gimplify the expression into a local
8739 variable. */
8740 #define RETURN_TRUE_IF_VAR(T) \
8741 do { tree _t = (T); \
8742 if (_t != NULL_TREE \
8743 && _t != error_mark_node \
8744 && TREE_CODE (_t) != INTEGER_CST \
8745 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8746 && (!fn \
8747 || (!TYPE_SIZES_GIMPLIFIED (type) \
8748 && !is_gimple_sizepos (_t)) \
8749 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8750 return true; } while (0)
8752 if (type == error_mark_node)
8753 return false;
8755 /* If TYPE itself has variable size, it is variably modified. */
8756 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8757 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8759 switch (TREE_CODE (type))
8761 case POINTER_TYPE:
8762 case REFERENCE_TYPE:
8763 case VECTOR_TYPE:
8764 if (variably_modified_type_p (TREE_TYPE (type), fn))
8765 return true;
8766 break;
8768 case FUNCTION_TYPE:
8769 case METHOD_TYPE:
8770 /* If TYPE is a function type, it is variably modified if the
8771 return type is variably modified. */
8772 if (variably_modified_type_p (TREE_TYPE (type), fn))
8773 return true;
8774 break;
8776 case INTEGER_TYPE:
8777 case REAL_TYPE:
8778 case FIXED_POINT_TYPE:
8779 case ENUMERAL_TYPE:
8780 case BOOLEAN_TYPE:
8781 /* Scalar types are variably modified if their end points
8782 aren't constant. */
8783 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8784 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8785 break;
8787 case RECORD_TYPE:
8788 case UNION_TYPE:
8789 case QUAL_UNION_TYPE:
8790 /* We can't see if any of the fields are variably-modified by the
8791 definition we normally use, since that would produce infinite
8792 recursion via pointers. */
8793 /* This is variably modified if some field's type is. */
8794 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8795 if (TREE_CODE (t) == FIELD_DECL)
8797 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8798 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8799 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8801 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8802 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8804 break;
8806 case ARRAY_TYPE:
8807 /* Do not call ourselves to avoid infinite recursion. This is
8808 variably modified if the element type is. */
8809 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8810 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8811 break;
8813 default:
8814 break;
8817 /* The current language may have other cases to check, but in general,
8818 all other types are not variably modified. */
8819 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8821 #undef RETURN_TRUE_IF_VAR
8824 /* Given a DECL or TYPE, return the scope in which it was declared, or
8825 NULL_TREE if there is no containing scope. */
8827 tree
8828 get_containing_scope (const_tree t)
8830 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8833 /* Return the innermost context enclosing DECL that is
8834 a FUNCTION_DECL, or zero if none. */
8836 tree
8837 decl_function_context (const_tree decl)
8839 tree context;
8841 if (TREE_CODE (decl) == ERROR_MARK)
8842 return 0;
8844 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8845 where we look up the function at runtime. Such functions always take
8846 a first argument of type 'pointer to real context'.
8848 C++ should really be fixed to use DECL_CONTEXT for the real context,
8849 and use something else for the "virtual context". */
8850 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8851 context
8852 = TYPE_MAIN_VARIANT
8853 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8854 else
8855 context = DECL_CONTEXT (decl);
8857 while (context && TREE_CODE (context) != FUNCTION_DECL)
8859 if (TREE_CODE (context) == BLOCK)
8860 context = BLOCK_SUPERCONTEXT (context);
8861 else
8862 context = get_containing_scope (context);
8865 return context;
8868 /* Return the innermost context enclosing DECL that is
8869 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8870 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8872 tree
8873 decl_type_context (const_tree decl)
8875 tree context = DECL_CONTEXT (decl);
8877 while (context)
8878 switch (TREE_CODE (context))
8880 case NAMESPACE_DECL:
8881 case TRANSLATION_UNIT_DECL:
8882 return NULL_TREE;
8884 case RECORD_TYPE:
8885 case UNION_TYPE:
8886 case QUAL_UNION_TYPE:
8887 return context;
8889 case TYPE_DECL:
8890 case FUNCTION_DECL:
8891 context = DECL_CONTEXT (context);
8892 break;
8894 case BLOCK:
8895 context = BLOCK_SUPERCONTEXT (context);
8896 break;
8898 default:
8899 gcc_unreachable ();
8902 return NULL_TREE;
8905 /* CALL is a CALL_EXPR. Return the declaration for the function
8906 called, or NULL_TREE if the called function cannot be
8907 determined. */
8909 tree
8910 get_callee_fndecl (const_tree call)
8912 tree addr;
8914 if (call == error_mark_node)
8915 return error_mark_node;
8917 /* It's invalid to call this function with anything but a
8918 CALL_EXPR. */
8919 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8921 /* The first operand to the CALL is the address of the function
8922 called. */
8923 addr = CALL_EXPR_FN (call);
8925 STRIP_NOPS (addr);
8927 /* If this is a readonly function pointer, extract its initial value. */
8928 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8929 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8930 && DECL_INITIAL (addr))
8931 addr = DECL_INITIAL (addr);
8933 /* If the address is just `&f' for some function `f', then we know
8934 that `f' is being called. */
8935 if (TREE_CODE (addr) == ADDR_EXPR
8936 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8937 return TREE_OPERAND (addr, 0);
8939 /* We couldn't figure out what was being called. */
8940 return NULL_TREE;
8943 /* Print debugging information about tree nodes generated during the compile,
8944 and any language-specific information. */
8946 void
8947 dump_tree_statistics (void)
8949 if (GATHER_STATISTICS)
8951 int i;
8952 int total_nodes, total_bytes;
8953 fprintf (stderr, "Kind Nodes Bytes\n");
8954 fprintf (stderr, "---------------------------------------\n");
8955 total_nodes = total_bytes = 0;
8956 for (i = 0; i < (int) all_kinds; i++)
8958 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
8959 tree_node_counts[i], tree_node_sizes[i]);
8960 total_nodes += tree_node_counts[i];
8961 total_bytes += tree_node_sizes[i];
8963 fprintf (stderr, "---------------------------------------\n");
8964 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
8965 fprintf (stderr, "---------------------------------------\n");
8966 fprintf (stderr, "Code Nodes\n");
8967 fprintf (stderr, "----------------------------\n");
8968 for (i = 0; i < (int) MAX_TREE_CODES; i++)
8969 fprintf (stderr, "%-20s %7d\n", tree_code_name[i], tree_code_counts[i]);
8970 fprintf (stderr, "----------------------------\n");
8971 ssanames_print_statistics ();
8972 phinodes_print_statistics ();
8974 else
8975 fprintf (stderr, "(No per-node statistics)\n");
8977 print_type_hash_statistics ();
8978 print_debug_expr_statistics ();
8979 print_value_expr_statistics ();
8980 lang_hooks.print_statistics ();
8983 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8985 /* Generate a crc32 of a byte. */
8987 static unsigned
8988 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
8990 unsigned ix;
8992 for (ix = bits; ix--; value <<= 1)
8994 unsigned feedback;
8996 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
8997 chksum <<= 1;
8998 chksum ^= feedback;
9000 return chksum;
9003 /* Generate a crc32 of a 32-bit unsigned. */
9005 unsigned
9006 crc32_unsigned (unsigned chksum, unsigned value)
9008 return crc32_unsigned_bits (chksum, value, 32);
9011 /* Generate a crc32 of a byte. */
9013 unsigned
9014 crc32_byte (unsigned chksum, char byte)
9016 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
9019 /* Generate a crc32 of a string. */
9021 unsigned
9022 crc32_string (unsigned chksum, const char *string)
9026 chksum = crc32_byte (chksum, *string);
9028 while (*string++);
9029 return chksum;
9032 /* P is a string that will be used in a symbol. Mask out any characters
9033 that are not valid in that context. */
9035 void
9036 clean_symbol_name (char *p)
9038 for (; *p; p++)
9039 if (! (ISALNUM (*p)
9040 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
9041 || *p == '$'
9042 #endif
9043 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
9044 || *p == '.'
9045 #endif
9047 *p = '_';
9050 /* Generate a name for a special-purpose function.
9051 The generated name may need to be unique across the whole link.
9052 Changes to this function may also require corresponding changes to
9053 xstrdup_mask_random.
9054 TYPE is some string to identify the purpose of this function to the
9055 linker or collect2; it must start with an uppercase letter,
9056 one of:
9057 I - for constructors
9058 D - for destructors
9059 N - for C++ anonymous namespaces
9060 F - for DWARF unwind frame information. */
9062 tree
9063 get_file_function_name (const char *type)
9065 char *buf;
9066 const char *p;
9067 char *q;
9069 /* If we already have a name we know to be unique, just use that. */
9070 if (first_global_object_name)
9071 p = q = ASTRDUP (first_global_object_name);
9072 /* If the target is handling the constructors/destructors, they
9073 will be local to this file and the name is only necessary for
9074 debugging purposes.
9075 We also assign sub_I and sub_D sufixes to constructors called from
9076 the global static constructors. These are always local. */
9077 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9078 || (strncmp (type, "sub_", 4) == 0
9079 && (type[4] == 'I' || type[4] == 'D')))
9081 const char *file = main_input_filename;
9082 if (! file)
9083 file = input_filename;
9084 /* Just use the file's basename, because the full pathname
9085 might be quite long. */
9086 p = q = ASTRDUP (lbasename (file));
9088 else
9090 /* Otherwise, the name must be unique across the entire link.
9091 We don't have anything that we know to be unique to this translation
9092 unit, so use what we do have and throw in some randomness. */
9093 unsigned len;
9094 const char *name = weak_global_object_name;
9095 const char *file = main_input_filename;
9097 if (! name)
9098 name = "";
9099 if (! file)
9100 file = input_filename;
9102 len = strlen (file);
9103 q = (char *) alloca (9 + 17 + len + 1);
9104 memcpy (q, file, len + 1);
9106 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9107 crc32_string (0, name), get_random_seed (false));
9109 p = q;
9112 clean_symbol_name (q);
9113 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9114 + strlen (type));
9116 /* Set up the name of the file-level functions we may need.
9117 Use a global object (which is already required to be unique over
9118 the program) rather than the file name (which imposes extra
9119 constraints). */
9120 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9122 return get_identifier (buf);
9125 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9127 /* Complain that the tree code of NODE does not match the expected 0
9128 terminated list of trailing codes. The trailing code list can be
9129 empty, for a more vague error message. FILE, LINE, and FUNCTION
9130 are of the caller. */
9132 void
9133 tree_check_failed (const_tree node, const char *file,
9134 int line, const char *function, ...)
9136 va_list args;
9137 const char *buffer;
9138 unsigned length = 0;
9139 int code;
9141 va_start (args, function);
9142 while ((code = va_arg (args, int)))
9143 length += 4 + strlen (tree_code_name[code]);
9144 va_end (args);
9145 if (length)
9147 char *tmp;
9148 va_start (args, function);
9149 length += strlen ("expected ");
9150 buffer = tmp = (char *) alloca (length);
9151 length = 0;
9152 while ((code = va_arg (args, int)))
9154 const char *prefix = length ? " or " : "expected ";
9156 strcpy (tmp + length, prefix);
9157 length += strlen (prefix);
9158 strcpy (tmp + length, tree_code_name[code]);
9159 length += strlen (tree_code_name[code]);
9161 va_end (args);
9163 else
9164 buffer = "unexpected node";
9166 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9167 buffer, tree_code_name[TREE_CODE (node)],
9168 function, trim_filename (file), line);
9171 /* Complain that the tree code of NODE does match the expected 0
9172 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9173 the caller. */
9175 void
9176 tree_not_check_failed (const_tree node, const char *file,
9177 int line, const char *function, ...)
9179 va_list args;
9180 char *buffer;
9181 unsigned length = 0;
9182 int code;
9184 va_start (args, function);
9185 while ((code = va_arg (args, int)))
9186 length += 4 + strlen (tree_code_name[code]);
9187 va_end (args);
9188 va_start (args, function);
9189 buffer = (char *) alloca (length);
9190 length = 0;
9191 while ((code = va_arg (args, int)))
9193 if (length)
9195 strcpy (buffer + length, " or ");
9196 length += 4;
9198 strcpy (buffer + length, tree_code_name[code]);
9199 length += strlen (tree_code_name[code]);
9201 va_end (args);
9203 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9204 buffer, tree_code_name[TREE_CODE (node)],
9205 function, trim_filename (file), line);
9208 /* Similar to tree_check_failed, except that we check for a class of tree
9209 code, given in CL. */
9211 void
9212 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9213 const char *file, int line, const char *function)
9215 internal_error
9216 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9217 TREE_CODE_CLASS_STRING (cl),
9218 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9219 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9222 /* Similar to tree_check_failed, except that instead of specifying a
9223 dozen codes, use the knowledge that they're all sequential. */
9225 void
9226 tree_range_check_failed (const_tree node, const char *file, int line,
9227 const char *function, enum tree_code c1,
9228 enum tree_code c2)
9230 char *buffer;
9231 unsigned length = 0;
9232 unsigned int c;
9234 for (c = c1; c <= c2; ++c)
9235 length += 4 + strlen (tree_code_name[c]);
9237 length += strlen ("expected ");
9238 buffer = (char *) alloca (length);
9239 length = 0;
9241 for (c = c1; c <= c2; ++c)
9243 const char *prefix = length ? " or " : "expected ";
9245 strcpy (buffer + length, prefix);
9246 length += strlen (prefix);
9247 strcpy (buffer + length, tree_code_name[c]);
9248 length += strlen (tree_code_name[c]);
9251 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9252 buffer, tree_code_name[TREE_CODE (node)],
9253 function, trim_filename (file), line);
9257 /* Similar to tree_check_failed, except that we check that a tree does
9258 not have the specified code, given in CL. */
9260 void
9261 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9262 const char *file, int line, const char *function)
9264 internal_error
9265 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9266 TREE_CODE_CLASS_STRING (cl),
9267 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9268 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9272 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9274 void
9275 omp_clause_check_failed (const_tree node, const char *file, int line,
9276 const char *function, enum omp_clause_code code)
9278 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9279 omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
9280 function, trim_filename (file), line);
9284 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9286 void
9287 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9288 const char *function, enum omp_clause_code c1,
9289 enum omp_clause_code c2)
9291 char *buffer;
9292 unsigned length = 0;
9293 unsigned int c;
9295 for (c = c1; c <= c2; ++c)
9296 length += 4 + strlen (omp_clause_code_name[c]);
9298 length += strlen ("expected ");
9299 buffer = (char *) alloca (length);
9300 length = 0;
9302 for (c = c1; c <= c2; ++c)
9304 const char *prefix = length ? " or " : "expected ";
9306 strcpy (buffer + length, prefix);
9307 length += strlen (prefix);
9308 strcpy (buffer + length, omp_clause_code_name[c]);
9309 length += strlen (omp_clause_code_name[c]);
9312 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9313 buffer, omp_clause_code_name[TREE_CODE (node)],
9314 function, trim_filename (file), line);
9318 #undef DEFTREESTRUCT
9319 #define DEFTREESTRUCT(VAL, NAME) NAME,
9321 static const char *ts_enum_names[] = {
9322 #include "treestruct.def"
9324 #undef DEFTREESTRUCT
9326 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9328 /* Similar to tree_class_check_failed, except that we check for
9329 whether CODE contains the tree structure identified by EN. */
9331 void
9332 tree_contains_struct_check_failed (const_tree node,
9333 const enum tree_node_structure_enum en,
9334 const char *file, int line,
9335 const char *function)
9337 internal_error
9338 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9339 TS_ENUM_NAME(en),
9340 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9344 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9345 (dynamically sized) vector. */
9347 void
9348 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9349 const char *function)
9351 internal_error
9352 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9353 idx + 1, len, function, trim_filename (file), line);
9356 /* Similar to above, except that the check is for the bounds of the operand
9357 vector of an expression node EXP. */
9359 void
9360 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9361 int line, const char *function)
9363 int code = TREE_CODE (exp);
9364 internal_error
9365 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9366 idx + 1, tree_code_name[code], TREE_OPERAND_LENGTH (exp),
9367 function, trim_filename (file), line);
9370 /* Similar to above, except that the check is for the number of
9371 operands of an OMP_CLAUSE node. */
9373 void
9374 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9375 int line, const char *function)
9377 internal_error
9378 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9379 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9380 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9381 trim_filename (file), line);
9383 #endif /* ENABLE_TREE_CHECKING */
9385 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9386 and mapped to the machine mode MODE. Initialize its fields and build
9387 the information necessary for debugging output. */
9389 static tree
9390 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
9392 tree t;
9393 hashval_t hashcode = 0;
9395 t = make_node (VECTOR_TYPE);
9396 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9397 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9398 SET_TYPE_MODE (t, mode);
9400 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9401 SET_TYPE_STRUCTURAL_EQUALITY (t);
9402 else if (TYPE_CANONICAL (innertype) != innertype
9403 || mode != VOIDmode)
9404 TYPE_CANONICAL (t)
9405 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9407 layout_type (t);
9409 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
9410 hashcode = iterative_hash_host_wide_int (nunits, hashcode);
9411 hashcode = iterative_hash_host_wide_int (mode, hashcode);
9412 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (t)), hashcode);
9413 t = type_hash_canon (hashcode, t);
9415 /* We have built a main variant, based on the main variant of the
9416 inner type. Use it to build the variant we return. */
9417 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9418 && TREE_TYPE (t) != innertype)
9419 return build_type_attribute_qual_variant (t,
9420 TYPE_ATTRIBUTES (innertype),
9421 TYPE_QUALS (innertype));
9423 return t;
9426 static tree
9427 make_or_reuse_type (unsigned size, int unsignedp)
9429 if (size == INT_TYPE_SIZE)
9430 return unsignedp ? unsigned_type_node : integer_type_node;
9431 if (size == CHAR_TYPE_SIZE)
9432 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9433 if (size == SHORT_TYPE_SIZE)
9434 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9435 if (size == LONG_TYPE_SIZE)
9436 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9437 if (size == LONG_LONG_TYPE_SIZE)
9438 return (unsignedp ? long_long_unsigned_type_node
9439 : long_long_integer_type_node);
9440 if (size == 128 && int128_integer_type_node)
9441 return (unsignedp ? int128_unsigned_type_node
9442 : int128_integer_type_node);
9444 if (unsignedp)
9445 return make_unsigned_type (size);
9446 else
9447 return make_signed_type (size);
9450 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9452 static tree
9453 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9455 if (satp)
9457 if (size == SHORT_FRACT_TYPE_SIZE)
9458 return unsignedp ? sat_unsigned_short_fract_type_node
9459 : sat_short_fract_type_node;
9460 if (size == FRACT_TYPE_SIZE)
9461 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9462 if (size == LONG_FRACT_TYPE_SIZE)
9463 return unsignedp ? sat_unsigned_long_fract_type_node
9464 : sat_long_fract_type_node;
9465 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9466 return unsignedp ? sat_unsigned_long_long_fract_type_node
9467 : sat_long_long_fract_type_node;
9469 else
9471 if (size == SHORT_FRACT_TYPE_SIZE)
9472 return unsignedp ? unsigned_short_fract_type_node
9473 : short_fract_type_node;
9474 if (size == FRACT_TYPE_SIZE)
9475 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9476 if (size == LONG_FRACT_TYPE_SIZE)
9477 return unsignedp ? unsigned_long_fract_type_node
9478 : long_fract_type_node;
9479 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9480 return unsignedp ? unsigned_long_long_fract_type_node
9481 : long_long_fract_type_node;
9484 return make_fract_type (size, unsignedp, satp);
9487 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9489 static tree
9490 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9492 if (satp)
9494 if (size == SHORT_ACCUM_TYPE_SIZE)
9495 return unsignedp ? sat_unsigned_short_accum_type_node
9496 : sat_short_accum_type_node;
9497 if (size == ACCUM_TYPE_SIZE)
9498 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9499 if (size == LONG_ACCUM_TYPE_SIZE)
9500 return unsignedp ? sat_unsigned_long_accum_type_node
9501 : sat_long_accum_type_node;
9502 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9503 return unsignedp ? sat_unsigned_long_long_accum_type_node
9504 : sat_long_long_accum_type_node;
9506 else
9508 if (size == SHORT_ACCUM_TYPE_SIZE)
9509 return unsignedp ? unsigned_short_accum_type_node
9510 : short_accum_type_node;
9511 if (size == ACCUM_TYPE_SIZE)
9512 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9513 if (size == LONG_ACCUM_TYPE_SIZE)
9514 return unsignedp ? unsigned_long_accum_type_node
9515 : long_accum_type_node;
9516 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9517 return unsignedp ? unsigned_long_long_accum_type_node
9518 : long_long_accum_type_node;
9521 return make_accum_type (size, unsignedp, satp);
9524 /* Create nodes for all integer types (and error_mark_node) using the sizes
9525 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9526 SHORT_DOUBLE specifies whether double should be of the same precision
9527 as float. */
9529 void
9530 build_common_tree_nodes (bool signed_char, bool short_double)
9532 error_mark_node = make_node (ERROR_MARK);
9533 TREE_TYPE (error_mark_node) = error_mark_node;
9535 initialize_sizetypes ();
9537 /* Define both `signed char' and `unsigned char'. */
9538 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9539 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9540 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9541 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9543 /* Define `char', which is like either `signed char' or `unsigned char'
9544 but not the same as either. */
9545 char_type_node
9546 = (signed_char
9547 ? make_signed_type (CHAR_TYPE_SIZE)
9548 : make_unsigned_type (CHAR_TYPE_SIZE));
9549 TYPE_STRING_FLAG (char_type_node) = 1;
9551 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9552 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9553 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9554 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9555 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9556 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9557 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9558 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9559 #if HOST_BITS_PER_WIDE_INT >= 64
9560 /* TODO: This isn't correct, but as logic depends at the moment on
9561 host's instead of target's wide-integer.
9562 If there is a target not supporting TImode, but has an 128-bit
9563 integer-scalar register, this target check needs to be adjusted. */
9564 if (targetm.scalar_mode_supported_p (TImode))
9566 int128_integer_type_node = make_signed_type (128);
9567 int128_unsigned_type_node = make_unsigned_type (128);
9569 #endif
9571 /* Define a boolean type. This type only represents boolean values but
9572 may be larger than char depending on the value of BOOL_TYPE_SIZE.
9573 Front ends which want to override this size (i.e. Java) can redefine
9574 boolean_type_node before calling build_common_tree_nodes_2. */
9575 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9576 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9577 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9578 TYPE_PRECISION (boolean_type_node) = 1;
9580 /* Define what type to use for size_t. */
9581 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9582 size_type_node = unsigned_type_node;
9583 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9584 size_type_node = long_unsigned_type_node;
9585 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9586 size_type_node = long_long_unsigned_type_node;
9587 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9588 size_type_node = short_unsigned_type_node;
9589 else
9590 gcc_unreachable ();
9592 /* Fill in the rest of the sized types. Reuse existing type nodes
9593 when possible. */
9594 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9595 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9596 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9597 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9598 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9600 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9601 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9602 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9603 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9604 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9606 access_public_node = get_identifier ("public");
9607 access_protected_node = get_identifier ("protected");
9608 access_private_node = get_identifier ("private");
9610 /* Define these next since types below may used them. */
9611 integer_zero_node = build_int_cst (integer_type_node, 0);
9612 integer_one_node = build_int_cst (integer_type_node, 1);
9613 integer_three_node = build_int_cst (integer_type_node, 3);
9614 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9616 size_zero_node = size_int (0);
9617 size_one_node = size_int (1);
9618 bitsize_zero_node = bitsize_int (0);
9619 bitsize_one_node = bitsize_int (1);
9620 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9622 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9623 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9625 void_type_node = make_node (VOID_TYPE);
9626 layout_type (void_type_node);
9628 /* We are not going to have real types in C with less than byte alignment,
9629 so we might as well not have any types that claim to have it. */
9630 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9631 TYPE_USER_ALIGN (void_type_node) = 0;
9633 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9634 layout_type (TREE_TYPE (null_pointer_node));
9636 ptr_type_node = build_pointer_type (void_type_node);
9637 const_ptr_type_node
9638 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9639 fileptr_type_node = ptr_type_node;
9641 float_type_node = make_node (REAL_TYPE);
9642 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9643 layout_type (float_type_node);
9645 double_type_node = make_node (REAL_TYPE);
9646 if (short_double)
9647 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9648 else
9649 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9650 layout_type (double_type_node);
9652 long_double_type_node = make_node (REAL_TYPE);
9653 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9654 layout_type (long_double_type_node);
9656 float_ptr_type_node = build_pointer_type (float_type_node);
9657 double_ptr_type_node = build_pointer_type (double_type_node);
9658 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9659 integer_ptr_type_node = build_pointer_type (integer_type_node);
9661 /* Fixed size integer types. */
9662 uint16_type_node = build_nonstandard_integer_type (16, true);
9663 uint32_type_node = build_nonstandard_integer_type (32, true);
9664 uint64_type_node = build_nonstandard_integer_type (64, true);
9666 /* Decimal float types. */
9667 dfloat32_type_node = make_node (REAL_TYPE);
9668 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9669 layout_type (dfloat32_type_node);
9670 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9671 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9673 dfloat64_type_node = make_node (REAL_TYPE);
9674 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9675 layout_type (dfloat64_type_node);
9676 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9677 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9679 dfloat128_type_node = make_node (REAL_TYPE);
9680 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9681 layout_type (dfloat128_type_node);
9682 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9683 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9685 complex_integer_type_node = build_complex_type (integer_type_node);
9686 complex_float_type_node = build_complex_type (float_type_node);
9687 complex_double_type_node = build_complex_type (double_type_node);
9688 complex_long_double_type_node = build_complex_type (long_double_type_node);
9690 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9691 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9692 sat_ ## KIND ## _type_node = \
9693 make_sat_signed_ ## KIND ## _type (SIZE); \
9694 sat_unsigned_ ## KIND ## _type_node = \
9695 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9696 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9697 unsigned_ ## KIND ## _type_node = \
9698 make_unsigned_ ## KIND ## _type (SIZE);
9700 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9701 sat_ ## WIDTH ## KIND ## _type_node = \
9702 make_sat_signed_ ## KIND ## _type (SIZE); \
9703 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9704 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9705 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9706 unsigned_ ## WIDTH ## KIND ## _type_node = \
9707 make_unsigned_ ## KIND ## _type (SIZE);
9709 /* Make fixed-point type nodes based on four different widths. */
9710 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9711 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9712 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9713 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9714 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9716 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9717 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9718 NAME ## _type_node = \
9719 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9720 u ## NAME ## _type_node = \
9721 make_or_reuse_unsigned_ ## KIND ## _type \
9722 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9723 sat_ ## NAME ## _type_node = \
9724 make_or_reuse_sat_signed_ ## KIND ## _type \
9725 (GET_MODE_BITSIZE (MODE ## mode)); \
9726 sat_u ## NAME ## _type_node = \
9727 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9728 (GET_MODE_BITSIZE (U ## MODE ## mode));
9730 /* Fixed-point type and mode nodes. */
9731 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9732 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9733 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9734 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9735 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9736 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9737 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9738 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9739 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9740 MAKE_FIXED_MODE_NODE (accum, da, DA)
9741 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9744 tree t = targetm.build_builtin_va_list ();
9746 /* Many back-ends define record types without setting TYPE_NAME.
9747 If we copied the record type here, we'd keep the original
9748 record type without a name. This breaks name mangling. So,
9749 don't copy record types and let c_common_nodes_and_builtins()
9750 declare the type to be __builtin_va_list. */
9751 if (TREE_CODE (t) != RECORD_TYPE)
9752 t = build_variant_type_copy (t);
9754 va_list_type_node = t;
9758 /* Modify DECL for given flags. */
9759 void
9760 set_call_expr_flags (tree decl, int flags)
9762 if (flags & ECF_NOTHROW)
9763 TREE_NOTHROW (decl) = 1;
9764 if (flags & ECF_CONST)
9765 TREE_READONLY (decl) = 1;
9766 if (flags & ECF_PURE)
9767 DECL_PURE_P (decl) = 1;
9768 if (flags & ECF_LOOPING_CONST_OR_PURE)
9769 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9770 if (flags & ECF_NOVOPS)
9771 DECL_IS_NOVOPS (decl) = 1;
9772 if (flags & ECF_NORETURN)
9773 TREE_THIS_VOLATILE (decl) = 1;
9774 if (flags & ECF_MALLOC)
9775 DECL_IS_MALLOC (decl) = 1;
9776 if (flags & ECF_RETURNS_TWICE)
9777 DECL_IS_RETURNS_TWICE (decl) = 1;
9778 if (flags & ECF_LEAF)
9779 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9780 NULL, DECL_ATTRIBUTES (decl));
9781 if ((flags & ECF_TM_PURE) && flag_tm)
9782 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("transaction_pure"),
9783 NULL, DECL_ATTRIBUTES (decl));
9784 /* Looping const or pure is implied by noreturn.
9785 There is currently no way to declare looping const or looping pure alone. */
9786 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9787 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9791 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9793 static void
9794 local_define_builtin (const char *name, tree type, enum built_in_function code,
9795 const char *library_name, int ecf_flags)
9797 tree decl;
9799 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9800 library_name, NULL_TREE);
9801 set_call_expr_flags (decl, ecf_flags);
9803 set_builtin_decl (code, decl, true);
9806 /* Call this function after instantiating all builtins that the language
9807 front end cares about. This will build the rest of the builtins that
9808 are relied upon by the tree optimizers and the middle-end. */
9810 void
9811 build_common_builtin_nodes (void)
9813 tree tmp, ftype;
9814 int ecf_flags;
9816 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9818 ftype = build_function_type (void_type_node, void_list_node);
9819 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
9820 "__builtin_unreachable",
9821 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9822 | ECF_CONST | ECF_LEAF);
9825 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9826 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9828 ftype = build_function_type_list (ptr_type_node,
9829 ptr_type_node, const_ptr_type_node,
9830 size_type_node, NULL_TREE);
9832 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
9833 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9834 "memcpy", ECF_NOTHROW | ECF_LEAF);
9835 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9836 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9837 "memmove", ECF_NOTHROW | ECF_LEAF);
9840 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
9842 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9843 const_ptr_type_node, size_type_node,
9844 NULL_TREE);
9845 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9846 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9849 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
9851 ftype = build_function_type_list (ptr_type_node,
9852 ptr_type_node, integer_type_node,
9853 size_type_node, NULL_TREE);
9854 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9855 "memset", ECF_NOTHROW | ECF_LEAF);
9858 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
9860 ftype = build_function_type_list (ptr_type_node,
9861 size_type_node, NULL_TREE);
9862 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9863 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9866 ftype = build_function_type_list (ptr_type_node, size_type_node,
9867 size_type_node, NULL_TREE);
9868 local_define_builtin ("__builtin_alloca_with_align", ftype,
9869 BUILT_IN_ALLOCA_WITH_ALIGN, "alloca",
9870 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9872 /* If we're checking the stack, `alloca' can throw. */
9873 if (flag_stack_check)
9875 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
9876 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
9879 ftype = build_function_type_list (void_type_node,
9880 ptr_type_node, ptr_type_node,
9881 ptr_type_node, NULL_TREE);
9882 local_define_builtin ("__builtin_init_trampoline", ftype,
9883 BUILT_IN_INIT_TRAMPOLINE,
9884 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9885 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
9886 BUILT_IN_INIT_HEAP_TRAMPOLINE,
9887 "__builtin_init_heap_trampoline",
9888 ECF_NOTHROW | ECF_LEAF);
9890 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9891 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9892 BUILT_IN_ADJUST_TRAMPOLINE,
9893 "__builtin_adjust_trampoline",
9894 ECF_CONST | ECF_NOTHROW);
9896 ftype = build_function_type_list (void_type_node,
9897 ptr_type_node, ptr_type_node, NULL_TREE);
9898 local_define_builtin ("__builtin_nonlocal_goto", ftype,
9899 BUILT_IN_NONLOCAL_GOTO,
9900 "__builtin_nonlocal_goto",
9901 ECF_NORETURN | ECF_NOTHROW);
9903 ftype = build_function_type_list (void_type_node,
9904 ptr_type_node, ptr_type_node, NULL_TREE);
9905 local_define_builtin ("__builtin_setjmp_setup", ftype,
9906 BUILT_IN_SETJMP_SETUP,
9907 "__builtin_setjmp_setup", ECF_NOTHROW);
9909 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9910 local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
9911 BUILT_IN_SETJMP_DISPATCHER,
9912 "__builtin_setjmp_dispatcher",
9913 ECF_PURE | ECF_NOTHROW);
9915 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9916 local_define_builtin ("__builtin_setjmp_receiver", ftype,
9917 BUILT_IN_SETJMP_RECEIVER,
9918 "__builtin_setjmp_receiver", ECF_NOTHROW);
9920 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9921 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
9922 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9924 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9925 local_define_builtin ("__builtin_stack_restore", ftype,
9926 BUILT_IN_STACK_RESTORE,
9927 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9929 /* If there's a possibility that we might use the ARM EABI, build the
9930 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
9931 if (targetm.arm_eabi_unwinder)
9933 ftype = build_function_type_list (void_type_node, NULL_TREE);
9934 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
9935 BUILT_IN_CXA_END_CLEANUP,
9936 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
9939 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9940 local_define_builtin ("__builtin_unwind_resume", ftype,
9941 BUILT_IN_UNWIND_RESUME,
9942 ((targetm_common.except_unwind_info (&global_options)
9943 == UI_SJLJ)
9944 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
9945 ECF_NORETURN);
9947 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
9949 ftype = build_function_type_list (ptr_type_node, integer_type_node,
9950 NULL_TREE);
9951 local_define_builtin ("__builtin_return_address", ftype,
9952 BUILT_IN_RETURN_ADDRESS,
9953 "__builtin_return_address",
9954 ECF_NOTHROW);
9957 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
9958 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9960 ftype = build_function_type_list (void_type_node, ptr_type_node,
9961 ptr_type_node, NULL_TREE);
9962 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
9963 local_define_builtin ("__cyg_profile_func_enter", ftype,
9964 BUILT_IN_PROFILE_FUNC_ENTER,
9965 "__cyg_profile_func_enter", 0);
9966 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9967 local_define_builtin ("__cyg_profile_func_exit", ftype,
9968 BUILT_IN_PROFILE_FUNC_EXIT,
9969 "__cyg_profile_func_exit", 0);
9972 /* The exception object and filter values from the runtime. The argument
9973 must be zero before exception lowering, i.e. from the front end. After
9974 exception lowering, it will be the region number for the exception
9975 landing pad. These functions are PURE instead of CONST to prevent
9976 them from being hoisted past the exception edge that will initialize
9977 its value in the landing pad. */
9978 ftype = build_function_type_list (ptr_type_node,
9979 integer_type_node, NULL_TREE);
9980 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
9981 /* Only use TM_PURE if we we have TM language support. */
9982 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
9983 ecf_flags |= ECF_TM_PURE;
9984 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
9985 "__builtin_eh_pointer", ecf_flags);
9987 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
9988 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
9989 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
9990 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9992 ftype = build_function_type_list (void_type_node,
9993 integer_type_node, integer_type_node,
9994 NULL_TREE);
9995 local_define_builtin ("__builtin_eh_copy_values", ftype,
9996 BUILT_IN_EH_COPY_VALUES,
9997 "__builtin_eh_copy_values", ECF_NOTHROW);
9999 /* Complex multiplication and division. These are handled as builtins
10000 rather than optabs because emit_library_call_value doesn't support
10001 complex. Further, we can do slightly better with folding these
10002 beasties if the real and complex parts of the arguments are separate. */
10004 int mode;
10006 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10008 char mode_name_buf[4], *q;
10009 const char *p;
10010 enum built_in_function mcode, dcode;
10011 tree type, inner_type;
10012 const char *prefix = "__";
10014 if (targetm.libfunc_gnu_prefix)
10015 prefix = "__gnu_";
10017 type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
10018 if (type == NULL)
10019 continue;
10020 inner_type = TREE_TYPE (type);
10022 ftype = build_function_type_list (type, inner_type, inner_type,
10023 inner_type, inner_type, NULL_TREE);
10025 mcode = ((enum built_in_function)
10026 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10027 dcode = ((enum built_in_function)
10028 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10030 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10031 *q = TOLOWER (*p);
10032 *q = '\0';
10034 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10035 NULL);
10036 local_define_builtin (built_in_names[mcode], ftype, mcode,
10037 built_in_names[mcode],
10038 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10040 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10041 NULL);
10042 local_define_builtin (built_in_names[dcode], ftype, dcode,
10043 built_in_names[dcode],
10044 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10049 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10050 better way.
10052 If we requested a pointer to a vector, build up the pointers that
10053 we stripped off while looking for the inner type. Similarly for
10054 return values from functions.
10056 The argument TYPE is the top of the chain, and BOTTOM is the
10057 new type which we will point to. */
10059 tree
10060 reconstruct_complex_type (tree type, tree bottom)
10062 tree inner, outer;
10064 if (TREE_CODE (type) == POINTER_TYPE)
10066 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10067 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10068 TYPE_REF_CAN_ALIAS_ALL (type));
10070 else if (TREE_CODE (type) == REFERENCE_TYPE)
10072 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10073 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10074 TYPE_REF_CAN_ALIAS_ALL (type));
10076 else if (TREE_CODE (type) == ARRAY_TYPE)
10078 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10079 outer = build_array_type (inner, TYPE_DOMAIN (type));
10081 else if (TREE_CODE (type) == FUNCTION_TYPE)
10083 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10084 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
10086 else if (TREE_CODE (type) == METHOD_TYPE)
10088 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10089 /* The build_method_type_directly() routine prepends 'this' to argument list,
10090 so we must compensate by getting rid of it. */
10091 outer
10092 = build_method_type_directly
10093 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10094 inner,
10095 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10097 else if (TREE_CODE (type) == OFFSET_TYPE)
10099 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10100 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10102 else
10103 return bottom;
10105 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10106 TYPE_QUALS (type));
10109 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10110 the inner type. */
10111 tree
10112 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
10114 int nunits;
10116 switch (GET_MODE_CLASS (mode))
10118 case MODE_VECTOR_INT:
10119 case MODE_VECTOR_FLOAT:
10120 case MODE_VECTOR_FRACT:
10121 case MODE_VECTOR_UFRACT:
10122 case MODE_VECTOR_ACCUM:
10123 case MODE_VECTOR_UACCUM:
10124 nunits = GET_MODE_NUNITS (mode);
10125 break;
10127 case MODE_INT:
10128 /* Check that there are no leftover bits. */
10129 gcc_assert (GET_MODE_BITSIZE (mode)
10130 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10132 nunits = GET_MODE_BITSIZE (mode)
10133 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10134 break;
10136 default:
10137 gcc_unreachable ();
10140 return make_vector_type (innertype, nunits, mode);
10143 /* Similarly, but takes the inner type and number of units, which must be
10144 a power of two. */
10146 tree
10147 build_vector_type (tree innertype, int nunits)
10149 return make_vector_type (innertype, nunits, VOIDmode);
10152 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10154 tree
10155 build_opaque_vector_type (tree innertype, int nunits)
10157 tree t = make_vector_type (innertype, nunits, VOIDmode);
10158 tree cand;
10159 /* We always build the non-opaque variant before the opaque one,
10160 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10161 cand = TYPE_NEXT_VARIANT (t);
10162 if (cand
10163 && TYPE_VECTOR_OPAQUE (cand)
10164 && check_qualified_type (cand, t, TYPE_QUALS (t)))
10165 return cand;
10166 /* Othewise build a variant type and make sure to queue it after
10167 the non-opaque type. */
10168 cand = build_distinct_type_copy (t);
10169 TYPE_VECTOR_OPAQUE (cand) = true;
10170 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10171 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10172 TYPE_NEXT_VARIANT (t) = cand;
10173 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10174 return cand;
10178 /* Given an initializer INIT, return TRUE if INIT is zero or some
10179 aggregate of zeros. Otherwise return FALSE. */
10180 bool
10181 initializer_zerop (const_tree init)
10183 tree elt;
10185 STRIP_NOPS (init);
10187 switch (TREE_CODE (init))
10189 case INTEGER_CST:
10190 return integer_zerop (init);
10192 case REAL_CST:
10193 /* ??? Note that this is not correct for C4X float formats. There,
10194 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10195 negative exponent. */
10196 return real_zerop (init)
10197 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10199 case FIXED_CST:
10200 return fixed_zerop (init);
10202 case COMPLEX_CST:
10203 return integer_zerop (init)
10204 || (real_zerop (init)
10205 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10206 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10208 case VECTOR_CST:
10210 unsigned i;
10211 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10212 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10213 return false;
10214 return true;
10217 case CONSTRUCTOR:
10219 unsigned HOST_WIDE_INT idx;
10221 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10222 if (!initializer_zerop (elt))
10223 return false;
10224 return true;
10227 case STRING_CST:
10229 int i;
10231 /* We need to loop through all elements to handle cases like
10232 "\0" and "\0foobar". */
10233 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10234 if (TREE_STRING_POINTER (init)[i] != '\0')
10235 return false;
10237 return true;
10240 default:
10241 return false;
10245 /* Check if vector VEC consists of all the equal elements and
10246 that the number of elements corresponds to the type of VEC.
10247 The function returns first element of the vector
10248 or NULL_TREE if the vector is not uniform. */
10249 tree
10250 uniform_vector_p (const_tree vec)
10252 tree first, t;
10253 unsigned i;
10255 if (vec == NULL_TREE)
10256 return NULL_TREE;
10258 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10260 if (TREE_CODE (vec) == VECTOR_CST)
10262 first = VECTOR_CST_ELT (vec, 0);
10263 for (i = 1; i < VECTOR_CST_NELTS (vec); ++i)
10264 if (!operand_equal_p (first, VECTOR_CST_ELT (vec, i), 0))
10265 return NULL_TREE;
10267 return first;
10270 else if (TREE_CODE (vec) == CONSTRUCTOR)
10272 first = error_mark_node;
10274 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10276 if (i == 0)
10278 first = t;
10279 continue;
10281 if (!operand_equal_p (first, t, 0))
10282 return NULL_TREE;
10284 if (i != TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)))
10285 return NULL_TREE;
10287 return first;
10290 return NULL_TREE;
10293 /* Build an empty statement at location LOC. */
10295 tree
10296 build_empty_stmt (location_t loc)
10298 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10299 SET_EXPR_LOCATION (t, loc);
10300 return t;
10304 /* Build an OpenMP clause with code CODE. LOC is the location of the
10305 clause. */
10307 tree
10308 build_omp_clause (location_t loc, enum omp_clause_code code)
10310 tree t;
10311 int size, length;
10313 length = omp_clause_num_ops[code];
10314 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10316 record_node_allocation_statistics (OMP_CLAUSE, size);
10318 t = ggc_alloc_tree_node (size);
10319 memset (t, 0, size);
10320 TREE_SET_CODE (t, OMP_CLAUSE);
10321 OMP_CLAUSE_SET_CODE (t, code);
10322 OMP_CLAUSE_LOCATION (t) = loc;
10324 return t;
10327 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10328 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10329 Except for the CODE and operand count field, other storage for the
10330 object is initialized to zeros. */
10332 tree
10333 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10335 tree t;
10336 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10338 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10339 gcc_assert (len >= 1);
10341 record_node_allocation_statistics (code, length);
10343 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10345 TREE_SET_CODE (t, code);
10347 /* Can't use TREE_OPERAND to store the length because if checking is
10348 enabled, it will try to check the length before we store it. :-P */
10349 t->exp.operands[0] = build_int_cst (sizetype, len);
10351 return t;
10354 /* Helper function for build_call_* functions; build a CALL_EXPR with
10355 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10356 the argument slots. */
10358 static tree
10359 build_call_1 (tree return_type, tree fn, int nargs)
10361 tree t;
10363 t = build_vl_exp (CALL_EXPR, nargs + 3);
10364 TREE_TYPE (t) = return_type;
10365 CALL_EXPR_FN (t) = fn;
10366 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10368 return t;
10371 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10372 FN and a null static chain slot. NARGS is the number of call arguments
10373 which are specified as "..." arguments. */
10375 tree
10376 build_call_nary (tree return_type, tree fn, int nargs, ...)
10378 tree ret;
10379 va_list args;
10380 va_start (args, nargs);
10381 ret = build_call_valist (return_type, fn, nargs, args);
10382 va_end (args);
10383 return ret;
10386 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10387 FN and a null static chain slot. NARGS is the number of call arguments
10388 which are specified as a va_list ARGS. */
10390 tree
10391 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10393 tree t;
10394 int i;
10396 t = build_call_1 (return_type, fn, nargs);
10397 for (i = 0; i < nargs; i++)
10398 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10399 process_call_operands (t);
10400 return t;
10403 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10404 FN and a null static chain slot. NARGS is the number of call arguments
10405 which are specified as a tree array ARGS. */
10407 tree
10408 build_call_array_loc (location_t loc, tree return_type, tree fn,
10409 int nargs, const tree *args)
10411 tree t;
10412 int i;
10414 t = build_call_1 (return_type, fn, nargs);
10415 for (i = 0; i < nargs; i++)
10416 CALL_EXPR_ARG (t, i) = args[i];
10417 process_call_operands (t);
10418 SET_EXPR_LOCATION (t, loc);
10419 return t;
10422 /* Like build_call_array, but takes a vec. */
10424 tree
10425 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10427 tree ret, t;
10428 unsigned int ix;
10430 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10431 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10432 CALL_EXPR_ARG (ret, ix) = t;
10433 process_call_operands (ret);
10434 return ret;
10438 /* Returns true if it is possible to prove that the index of
10439 an array access REF (an ARRAY_REF expression) falls into the
10440 array bounds. */
10442 bool
10443 in_array_bounds_p (tree ref)
10445 tree idx = TREE_OPERAND (ref, 1);
10446 tree min, max;
10448 if (TREE_CODE (idx) != INTEGER_CST)
10449 return false;
10451 min = array_ref_low_bound (ref);
10452 max = array_ref_up_bound (ref);
10453 if (!min
10454 || !max
10455 || TREE_CODE (min) != INTEGER_CST
10456 || TREE_CODE (max) != INTEGER_CST)
10457 return false;
10459 if (tree_int_cst_lt (idx, min)
10460 || tree_int_cst_lt (max, idx))
10461 return false;
10463 return true;
10466 /* Returns true if it is possible to prove that the range of
10467 an array access REF (an ARRAY_RANGE_REF expression) falls
10468 into the array bounds. */
10470 bool
10471 range_in_array_bounds_p (tree ref)
10473 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
10474 tree range_min, range_max, min, max;
10476 range_min = TYPE_MIN_VALUE (domain_type);
10477 range_max = TYPE_MAX_VALUE (domain_type);
10478 if (!range_min
10479 || !range_max
10480 || TREE_CODE (range_min) != INTEGER_CST
10481 || TREE_CODE (range_max) != INTEGER_CST)
10482 return false;
10484 min = array_ref_low_bound (ref);
10485 max = array_ref_up_bound (ref);
10486 if (!min
10487 || !max
10488 || TREE_CODE (min) != INTEGER_CST
10489 || TREE_CODE (max) != INTEGER_CST)
10490 return false;
10492 if (tree_int_cst_lt (range_min, min)
10493 || tree_int_cst_lt (max, range_max))
10494 return false;
10496 return true;
10499 /* Return true if T (assumed to be a DECL) must be assigned a memory
10500 location. */
10502 bool
10503 needs_to_live_in_memory (const_tree t)
10505 return (TREE_ADDRESSABLE (t)
10506 || is_global_var (t)
10507 || (TREE_CODE (t) == RESULT_DECL
10508 && !DECL_BY_REFERENCE (t)
10509 && aggregate_value_p (t, current_function_decl)));
10512 /* Return value of a constant X and sign-extend it. */
10514 HOST_WIDE_INT
10515 int_cst_value (const_tree x)
10517 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10518 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10520 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10521 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10522 || TREE_INT_CST_HIGH (x) == -1);
10524 if (bits < HOST_BITS_PER_WIDE_INT)
10526 bool negative = ((val >> (bits - 1)) & 1) != 0;
10527 if (negative)
10528 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10529 else
10530 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10533 return val;
10536 /* Return value of a constant X and sign-extend it. */
10538 HOST_WIDEST_INT
10539 widest_int_cst_value (const_tree x)
10541 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10542 unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
10544 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
10545 gcc_assert (HOST_BITS_PER_WIDEST_INT >= HOST_BITS_PER_DOUBLE_INT);
10546 val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_HIGH (x))
10547 << HOST_BITS_PER_WIDE_INT);
10548 #else
10549 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10550 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10551 || TREE_INT_CST_HIGH (x) == -1);
10552 #endif
10554 if (bits < HOST_BITS_PER_WIDEST_INT)
10556 bool negative = ((val >> (bits - 1)) & 1) != 0;
10557 if (negative)
10558 val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
10559 else
10560 val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
10563 return val;
10566 /* If TYPE is an integral or pointer type, return an integer type with
10567 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10568 if TYPE is already an integer type of signedness UNSIGNEDP. */
10570 tree
10571 signed_or_unsigned_type_for (int unsignedp, tree type)
10573 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10574 return type;
10576 if (TREE_CODE (type) == VECTOR_TYPE)
10578 tree inner = TREE_TYPE (type);
10579 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10580 if (!inner2)
10581 return NULL_TREE;
10582 if (inner == inner2)
10583 return type;
10584 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10587 if (!INTEGRAL_TYPE_P (type)
10588 && !POINTER_TYPE_P (type))
10589 return NULL_TREE;
10591 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10594 /* If TYPE is an integral or pointer type, return an integer type with
10595 the same precision which is unsigned, or itself if TYPE is already an
10596 unsigned integer type. */
10598 tree
10599 unsigned_type_for (tree type)
10601 return signed_or_unsigned_type_for (1, type);
10604 /* If TYPE is an integral or pointer type, return an integer type with
10605 the same precision which is signed, or itself if TYPE is already a
10606 signed integer type. */
10608 tree
10609 signed_type_for (tree type)
10611 return signed_or_unsigned_type_for (0, type);
10614 /* If TYPE is a vector type, return a signed integer vector type with the
10615 same width and number of subparts. Otherwise return boolean_type_node. */
10617 tree
10618 truth_type_for (tree type)
10620 if (TREE_CODE (type) == VECTOR_TYPE)
10622 tree elem = lang_hooks.types.type_for_size
10623 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10624 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10626 else
10627 return boolean_type_node;
10630 /* Returns the largest value obtainable by casting something in INNER type to
10631 OUTER type. */
10633 tree
10634 upper_bound_in_type (tree outer, tree inner)
10636 double_int high;
10637 unsigned int det = 0;
10638 unsigned oprec = TYPE_PRECISION (outer);
10639 unsigned iprec = TYPE_PRECISION (inner);
10640 unsigned prec;
10642 /* Compute a unique number for every combination. */
10643 det |= (oprec > iprec) ? 4 : 0;
10644 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10645 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10647 /* Determine the exponent to use. */
10648 switch (det)
10650 case 0:
10651 case 1:
10652 /* oprec <= iprec, outer: signed, inner: don't care. */
10653 prec = oprec - 1;
10654 break;
10655 case 2:
10656 case 3:
10657 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10658 prec = oprec;
10659 break;
10660 case 4:
10661 /* oprec > iprec, outer: signed, inner: signed. */
10662 prec = iprec - 1;
10663 break;
10664 case 5:
10665 /* oprec > iprec, outer: signed, inner: unsigned. */
10666 prec = iprec;
10667 break;
10668 case 6:
10669 /* oprec > iprec, outer: unsigned, inner: signed. */
10670 prec = oprec;
10671 break;
10672 case 7:
10673 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10674 prec = iprec;
10675 break;
10676 default:
10677 gcc_unreachable ();
10680 /* Compute 2^^prec - 1. */
10681 if (prec <= HOST_BITS_PER_WIDE_INT)
10683 high.high = 0;
10684 high.low = ((~(unsigned HOST_WIDE_INT) 0)
10685 >> (HOST_BITS_PER_WIDE_INT - prec));
10687 else
10689 high.high = ((~(unsigned HOST_WIDE_INT) 0)
10690 >> (HOST_BITS_PER_DOUBLE_INT - prec));
10691 high.low = ~(unsigned HOST_WIDE_INT) 0;
10694 return double_int_to_tree (outer, high);
10697 /* Returns the smallest value obtainable by casting something in INNER type to
10698 OUTER type. */
10700 tree
10701 lower_bound_in_type (tree outer, tree inner)
10703 double_int low;
10704 unsigned oprec = TYPE_PRECISION (outer);
10705 unsigned iprec = TYPE_PRECISION (inner);
10707 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10708 and obtain 0. */
10709 if (TYPE_UNSIGNED (outer)
10710 /* If we are widening something of an unsigned type, OUTER type
10711 contains all values of INNER type. In particular, both INNER
10712 and OUTER types have zero in common. */
10713 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10714 low.low = low.high = 0;
10715 else
10717 /* If we are widening a signed type to another signed type, we
10718 want to obtain -2^^(iprec-1). If we are keeping the
10719 precision or narrowing to a signed type, we want to obtain
10720 -2^(oprec-1). */
10721 unsigned prec = oprec > iprec ? iprec : oprec;
10723 if (prec <= HOST_BITS_PER_WIDE_INT)
10725 low.high = ~(unsigned HOST_WIDE_INT) 0;
10726 low.low = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
10728 else
10730 low.high = ((~(unsigned HOST_WIDE_INT) 0)
10731 << (prec - HOST_BITS_PER_WIDE_INT - 1));
10732 low.low = 0;
10736 return double_int_to_tree (outer, low);
10739 /* Return nonzero if two operands that are suitable for PHI nodes are
10740 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10741 SSA_NAME or invariant. Note that this is strictly an optimization.
10742 That is, callers of this function can directly call operand_equal_p
10743 and get the same result, only slower. */
10746 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10748 if (arg0 == arg1)
10749 return 1;
10750 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10751 return 0;
10752 return operand_equal_p (arg0, arg1, 0);
10755 /* Returns number of zeros at the end of binary representation of X.
10757 ??? Use ffs if available? */
10759 tree
10760 num_ending_zeros (const_tree x)
10762 unsigned HOST_WIDE_INT fr, nfr;
10763 unsigned num, abits;
10764 tree type = TREE_TYPE (x);
10766 if (TREE_INT_CST_LOW (x) == 0)
10768 num = HOST_BITS_PER_WIDE_INT;
10769 fr = TREE_INT_CST_HIGH (x);
10771 else
10773 num = 0;
10774 fr = TREE_INT_CST_LOW (x);
10777 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
10779 nfr = fr >> abits;
10780 if (nfr << abits == fr)
10782 num += abits;
10783 fr = nfr;
10787 if (num > TYPE_PRECISION (type))
10788 num = TYPE_PRECISION (type);
10790 return build_int_cst_type (type, num);
10794 #define WALK_SUBTREE(NODE) \
10795 do \
10797 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10798 if (result) \
10799 return result; \
10801 while (0)
10803 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10804 be walked whenever a type is seen in the tree. Rest of operands and return
10805 value are as for walk_tree. */
10807 static tree
10808 walk_type_fields (tree type, walk_tree_fn func, void *data,
10809 struct pointer_set_t *pset, walk_tree_lh lh)
10811 tree result = NULL_TREE;
10813 switch (TREE_CODE (type))
10815 case POINTER_TYPE:
10816 case REFERENCE_TYPE:
10817 /* We have to worry about mutually recursive pointers. These can't
10818 be written in C. They can in Ada. It's pathological, but
10819 there's an ACATS test (c38102a) that checks it. Deal with this
10820 by checking if we're pointing to another pointer, that one
10821 points to another pointer, that one does too, and we have no htab.
10822 If so, get a hash table. We check three levels deep to avoid
10823 the cost of the hash table if we don't need one. */
10824 if (POINTER_TYPE_P (TREE_TYPE (type))
10825 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10826 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10827 && !pset)
10829 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10830 func, data);
10831 if (result)
10832 return result;
10834 break;
10837 /* ... fall through ... */
10839 case COMPLEX_TYPE:
10840 WALK_SUBTREE (TREE_TYPE (type));
10841 break;
10843 case METHOD_TYPE:
10844 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10846 /* Fall through. */
10848 case FUNCTION_TYPE:
10849 WALK_SUBTREE (TREE_TYPE (type));
10851 tree arg;
10853 /* We never want to walk into default arguments. */
10854 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10855 WALK_SUBTREE (TREE_VALUE (arg));
10857 break;
10859 case ARRAY_TYPE:
10860 /* Don't follow this nodes's type if a pointer for fear that
10861 we'll have infinite recursion. If we have a PSET, then we
10862 need not fear. */
10863 if (pset
10864 || (!POINTER_TYPE_P (TREE_TYPE (type))
10865 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10866 WALK_SUBTREE (TREE_TYPE (type));
10867 WALK_SUBTREE (TYPE_DOMAIN (type));
10868 break;
10870 case OFFSET_TYPE:
10871 WALK_SUBTREE (TREE_TYPE (type));
10872 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
10873 break;
10875 default:
10876 break;
10879 return NULL_TREE;
10882 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
10883 called with the DATA and the address of each sub-tree. If FUNC returns a
10884 non-NULL value, the traversal is stopped, and the value returned by FUNC
10885 is returned. If PSET is non-NULL it is used to record the nodes visited,
10886 and to avoid visiting a node more than once. */
10888 tree
10889 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
10890 struct pointer_set_t *pset, walk_tree_lh lh)
10892 enum tree_code code;
10893 int walk_subtrees;
10894 tree result;
10896 #define WALK_SUBTREE_TAIL(NODE) \
10897 do \
10899 tp = & (NODE); \
10900 goto tail_recurse; \
10902 while (0)
10904 tail_recurse:
10905 /* Skip empty subtrees. */
10906 if (!*tp)
10907 return NULL_TREE;
10909 /* Don't walk the same tree twice, if the user has requested
10910 that we avoid doing so. */
10911 if (pset && pointer_set_insert (pset, *tp))
10912 return NULL_TREE;
10914 /* Call the function. */
10915 walk_subtrees = 1;
10916 result = (*func) (tp, &walk_subtrees, data);
10918 /* If we found something, return it. */
10919 if (result)
10920 return result;
10922 code = TREE_CODE (*tp);
10924 /* Even if we didn't, FUNC may have decided that there was nothing
10925 interesting below this point in the tree. */
10926 if (!walk_subtrees)
10928 /* But we still need to check our siblings. */
10929 if (code == TREE_LIST)
10930 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10931 else if (code == OMP_CLAUSE)
10932 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10933 else
10934 return NULL_TREE;
10937 if (lh)
10939 result = (*lh) (tp, &walk_subtrees, func, data, pset);
10940 if (result || !walk_subtrees)
10941 return result;
10944 switch (code)
10946 case ERROR_MARK:
10947 case IDENTIFIER_NODE:
10948 case INTEGER_CST:
10949 case REAL_CST:
10950 case FIXED_CST:
10951 case VECTOR_CST:
10952 case STRING_CST:
10953 case BLOCK:
10954 case PLACEHOLDER_EXPR:
10955 case SSA_NAME:
10956 case FIELD_DECL:
10957 case RESULT_DECL:
10958 /* None of these have subtrees other than those already walked
10959 above. */
10960 break;
10962 case TREE_LIST:
10963 WALK_SUBTREE (TREE_VALUE (*tp));
10964 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10965 break;
10967 case TREE_VEC:
10969 int len = TREE_VEC_LENGTH (*tp);
10971 if (len == 0)
10972 break;
10974 /* Walk all elements but the first. */
10975 while (--len)
10976 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
10978 /* Now walk the first one as a tail call. */
10979 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
10982 case COMPLEX_CST:
10983 WALK_SUBTREE (TREE_REALPART (*tp));
10984 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
10986 case CONSTRUCTOR:
10988 unsigned HOST_WIDE_INT idx;
10989 constructor_elt *ce;
10991 for (idx = 0; vec_safe_iterate(CONSTRUCTOR_ELTS (*tp), idx, &ce); idx++)
10992 WALK_SUBTREE (ce->value);
10994 break;
10996 case SAVE_EXPR:
10997 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
10999 case BIND_EXPR:
11001 tree decl;
11002 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
11004 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11005 into declarations that are just mentioned, rather than
11006 declared; they don't really belong to this part of the tree.
11007 And, we can see cycles: the initializer for a declaration
11008 can refer to the declaration itself. */
11009 WALK_SUBTREE (DECL_INITIAL (decl));
11010 WALK_SUBTREE (DECL_SIZE (decl));
11011 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11013 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
11016 case STATEMENT_LIST:
11018 tree_stmt_iterator i;
11019 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
11020 WALK_SUBTREE (*tsi_stmt_ptr (i));
11022 break;
11024 case OMP_CLAUSE:
11025 switch (OMP_CLAUSE_CODE (*tp))
11027 case OMP_CLAUSE_PRIVATE:
11028 case OMP_CLAUSE_SHARED:
11029 case OMP_CLAUSE_FIRSTPRIVATE:
11030 case OMP_CLAUSE_COPYIN:
11031 case OMP_CLAUSE_COPYPRIVATE:
11032 case OMP_CLAUSE_FINAL:
11033 case OMP_CLAUSE_IF:
11034 case OMP_CLAUSE_NUM_THREADS:
11035 case OMP_CLAUSE_SCHEDULE:
11036 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
11037 /* FALLTHRU */
11039 case OMP_CLAUSE_NOWAIT:
11040 case OMP_CLAUSE_ORDERED:
11041 case OMP_CLAUSE_DEFAULT:
11042 case OMP_CLAUSE_UNTIED:
11043 case OMP_CLAUSE_MERGEABLE:
11044 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11046 case OMP_CLAUSE_LASTPRIVATE:
11047 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11048 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
11049 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11051 case OMP_CLAUSE_COLLAPSE:
11053 int i;
11054 for (i = 0; i < 3; i++)
11055 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11056 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11059 case OMP_CLAUSE_REDUCTION:
11061 int i;
11062 for (i = 0; i < 4; i++)
11063 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11064 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11067 default:
11068 gcc_unreachable ();
11070 break;
11072 case TARGET_EXPR:
11074 int i, len;
11076 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11077 But, we only want to walk once. */
11078 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11079 for (i = 0; i < len; ++i)
11080 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11081 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11084 case DECL_EXPR:
11085 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11086 defining. We only want to walk into these fields of a type in this
11087 case and not in the general case of a mere reference to the type.
11089 The criterion is as follows: if the field can be an expression, it
11090 must be walked only here. This should be in keeping with the fields
11091 that are directly gimplified in gimplify_type_sizes in order for the
11092 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11093 variable-sized types.
11095 Note that DECLs get walked as part of processing the BIND_EXPR. */
11096 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11098 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11099 if (TREE_CODE (*type_p) == ERROR_MARK)
11100 return NULL_TREE;
11102 /* Call the function for the type. See if it returns anything or
11103 doesn't want us to continue. If we are to continue, walk both
11104 the normal fields and those for the declaration case. */
11105 result = (*func) (type_p, &walk_subtrees, data);
11106 if (result || !walk_subtrees)
11107 return result;
11109 /* But do not walk a pointed-to type since it may itself need to
11110 be walked in the declaration case if it isn't anonymous. */
11111 if (!POINTER_TYPE_P (*type_p))
11113 result = walk_type_fields (*type_p, func, data, pset, lh);
11114 if (result)
11115 return result;
11118 /* If this is a record type, also walk the fields. */
11119 if (RECORD_OR_UNION_TYPE_P (*type_p))
11121 tree field;
11123 for (field = TYPE_FIELDS (*type_p); field;
11124 field = DECL_CHAIN (field))
11126 /* We'd like to look at the type of the field, but we can
11127 easily get infinite recursion. So assume it's pointed
11128 to elsewhere in the tree. Also, ignore things that
11129 aren't fields. */
11130 if (TREE_CODE (field) != FIELD_DECL)
11131 continue;
11133 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11134 WALK_SUBTREE (DECL_SIZE (field));
11135 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11136 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11137 WALK_SUBTREE (DECL_QUALIFIER (field));
11141 /* Same for scalar types. */
11142 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11143 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11144 || TREE_CODE (*type_p) == INTEGER_TYPE
11145 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11146 || TREE_CODE (*type_p) == REAL_TYPE)
11148 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11149 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11152 WALK_SUBTREE (TYPE_SIZE (*type_p));
11153 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11155 /* FALLTHRU */
11157 default:
11158 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11160 int i, len;
11162 /* Walk over all the sub-trees of this operand. */
11163 len = TREE_OPERAND_LENGTH (*tp);
11165 /* Go through the subtrees. We need to do this in forward order so
11166 that the scope of a FOR_EXPR is handled properly. */
11167 if (len)
11169 for (i = 0; i < len - 1; ++i)
11170 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11171 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11174 /* If this is a type, walk the needed fields in the type. */
11175 else if (TYPE_P (*tp))
11176 return walk_type_fields (*tp, func, data, pset, lh);
11177 break;
11180 /* We didn't find what we were looking for. */
11181 return NULL_TREE;
11183 #undef WALK_SUBTREE_TAIL
11185 #undef WALK_SUBTREE
11187 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11189 tree
11190 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11191 walk_tree_lh lh)
11193 tree result;
11194 struct pointer_set_t *pset;
11196 pset = pointer_set_create ();
11197 result = walk_tree_1 (tp, func, data, pset, lh);
11198 pointer_set_destroy (pset);
11199 return result;
11203 tree
11204 tree_block (tree t)
11206 char const c = TREE_CODE_CLASS (TREE_CODE (t));
11208 if (IS_EXPR_CODE_CLASS (c))
11209 return LOCATION_BLOCK (t->exp.locus);
11210 gcc_unreachable ();
11211 return NULL;
11214 void
11215 tree_set_block (tree t, tree b)
11217 char const c = TREE_CODE_CLASS (TREE_CODE (t));
11219 if (IS_EXPR_CODE_CLASS (c))
11221 if (b)
11222 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
11223 else
11224 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
11226 else
11227 gcc_unreachable ();
11230 /* Create a nameless artificial label and put it in the current
11231 function context. The label has a location of LOC. Returns the
11232 newly created label. */
11234 tree
11235 create_artificial_label (location_t loc)
11237 tree lab = build_decl (loc,
11238 LABEL_DECL, NULL_TREE, void_type_node);
11240 DECL_ARTIFICIAL (lab) = 1;
11241 DECL_IGNORED_P (lab) = 1;
11242 DECL_CONTEXT (lab) = current_function_decl;
11243 return lab;
11246 /* Given a tree, try to return a useful variable name that we can use
11247 to prefix a temporary that is being assigned the value of the tree.
11248 I.E. given <temp> = &A, return A. */
11250 const char *
11251 get_name (tree t)
11253 tree stripped_decl;
11255 stripped_decl = t;
11256 STRIP_NOPS (stripped_decl);
11257 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11258 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11259 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11261 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11262 if (!name)
11263 return NULL;
11264 return IDENTIFIER_POINTER (name);
11266 else
11268 switch (TREE_CODE (stripped_decl))
11270 case ADDR_EXPR:
11271 return get_name (TREE_OPERAND (stripped_decl, 0));
11272 default:
11273 return NULL;
11278 /* Return true if TYPE has a variable argument list. */
11280 bool
11281 stdarg_p (const_tree fntype)
11283 function_args_iterator args_iter;
11284 tree n = NULL_TREE, t;
11286 if (!fntype)
11287 return false;
11289 FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
11291 n = t;
11294 return n != NULL_TREE && n != void_type_node;
11297 /* Return true if TYPE has a prototype. */
11299 bool
11300 prototype_p (tree fntype)
11302 tree t;
11304 gcc_assert (fntype != NULL_TREE);
11306 t = TYPE_ARG_TYPES (fntype);
11307 return (t != NULL_TREE);
11310 /* If BLOCK is inlined from an __attribute__((__artificial__))
11311 routine, return pointer to location from where it has been
11312 called. */
11313 location_t *
11314 block_nonartificial_location (tree block)
11316 location_t *ret = NULL;
11318 while (block && TREE_CODE (block) == BLOCK
11319 && BLOCK_ABSTRACT_ORIGIN (block))
11321 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11323 while (TREE_CODE (ao) == BLOCK
11324 && BLOCK_ABSTRACT_ORIGIN (ao)
11325 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11326 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11328 if (TREE_CODE (ao) == FUNCTION_DECL)
11330 /* If AO is an artificial inline, point RET to the
11331 call site locus at which it has been inlined and continue
11332 the loop, in case AO's caller is also an artificial
11333 inline. */
11334 if (DECL_DECLARED_INLINE_P (ao)
11335 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11336 ret = &BLOCK_SOURCE_LOCATION (block);
11337 else
11338 break;
11340 else if (TREE_CODE (ao) != BLOCK)
11341 break;
11343 block = BLOCK_SUPERCONTEXT (block);
11345 return ret;
11349 /* If EXP is inlined from an __attribute__((__artificial__))
11350 function, return the location of the original call expression. */
11352 location_t
11353 tree_nonartificial_location (tree exp)
11355 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11357 if (loc)
11358 return *loc;
11359 else
11360 return EXPR_LOCATION (exp);
11364 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11365 nodes. */
11367 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11369 static hashval_t
11370 cl_option_hash_hash (const void *x)
11372 const_tree const t = (const_tree) x;
11373 const char *p;
11374 size_t i;
11375 size_t len = 0;
11376 hashval_t hash = 0;
11378 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11380 p = (const char *)TREE_OPTIMIZATION (t);
11381 len = sizeof (struct cl_optimization);
11384 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11386 p = (const char *)TREE_TARGET_OPTION (t);
11387 len = sizeof (struct cl_target_option);
11390 else
11391 gcc_unreachable ();
11393 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11394 something else. */
11395 for (i = 0; i < len; i++)
11396 if (p[i])
11397 hash = (hash << 4) ^ ((i << 2) | p[i]);
11399 return hash;
11402 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11403 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11404 same. */
11406 static int
11407 cl_option_hash_eq (const void *x, const void *y)
11409 const_tree const xt = (const_tree) x;
11410 const_tree const yt = (const_tree) y;
11411 const char *xp;
11412 const char *yp;
11413 size_t len;
11415 if (TREE_CODE (xt) != TREE_CODE (yt))
11416 return 0;
11418 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11420 xp = (const char *)TREE_OPTIMIZATION (xt);
11421 yp = (const char *)TREE_OPTIMIZATION (yt);
11422 len = sizeof (struct cl_optimization);
11425 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11427 xp = (const char *)TREE_TARGET_OPTION (xt);
11428 yp = (const char *)TREE_TARGET_OPTION (yt);
11429 len = sizeof (struct cl_target_option);
11432 else
11433 gcc_unreachable ();
11435 return (memcmp (xp, yp, len) == 0);
11438 /* Build an OPTIMIZATION_NODE based on the current options. */
11440 tree
11441 build_optimization_node (void)
11443 tree t;
11444 void **slot;
11446 /* Use the cache of optimization nodes. */
11448 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11449 &global_options);
11451 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
11452 t = (tree) *slot;
11453 if (!t)
11455 /* Insert this one into the hash table. */
11456 t = cl_optimization_node;
11457 *slot = t;
11459 /* Make a new node for next time round. */
11460 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11463 return t;
11466 /* Build a TARGET_OPTION_NODE based on the current options. */
11468 tree
11469 build_target_option_node (void)
11471 tree t;
11472 void **slot;
11474 /* Use the cache of optimization nodes. */
11476 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11477 &global_options);
11479 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
11480 t = (tree) *slot;
11481 if (!t)
11483 /* Insert this one into the hash table. */
11484 t = cl_target_option_node;
11485 *slot = t;
11487 /* Make a new node for next time round. */
11488 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11491 return t;
11494 /* Determine the "ultimate origin" of a block. The block may be an inlined
11495 instance of an inlined instance of a block which is local to an inline
11496 function, so we have to trace all of the way back through the origin chain
11497 to find out what sort of node actually served as the original seed for the
11498 given block. */
11500 tree
11501 block_ultimate_origin (const_tree block)
11503 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11505 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
11506 nodes in the function to point to themselves; ignore that if
11507 we're trying to output the abstract instance of this function. */
11508 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11509 return NULL_TREE;
11511 if (immediate_origin == NULL_TREE)
11512 return NULL_TREE;
11513 else
11515 tree ret_val;
11516 tree lookahead = immediate_origin;
11520 ret_val = lookahead;
11521 lookahead = (TREE_CODE (ret_val) == BLOCK
11522 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11524 while (lookahead != NULL && lookahead != ret_val);
11526 /* The block's abstract origin chain may not be the *ultimate* origin of
11527 the block. It could lead to a DECL that has an abstract origin set.
11528 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11529 will give us if it has one). Note that DECL's abstract origins are
11530 supposed to be the most distant ancestor (or so decl_ultimate_origin
11531 claims), so we don't need to loop following the DECL origins. */
11532 if (DECL_P (ret_val))
11533 return DECL_ORIGIN (ret_val);
11535 return ret_val;
11539 /* Return true if T1 and T2 are equivalent lists. */
11541 bool
11542 list_equal_p (const_tree t1, const_tree t2)
11544 for (; t1 && t2; t1 = TREE_CHAIN (t1) , t2 = TREE_CHAIN (t2))
11545 if (TREE_VALUE (t1) != TREE_VALUE (t2))
11546 return false;
11547 return !t1 && !t2;
11550 /* Return true iff conversion in EXP generates no instruction. Mark
11551 it inline so that we fully inline into the stripping functions even
11552 though we have two uses of this function. */
11554 static inline bool
11555 tree_nop_conversion (const_tree exp)
11557 tree outer_type, inner_type;
11559 if (!CONVERT_EXPR_P (exp)
11560 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11561 return false;
11562 if (TREE_OPERAND (exp, 0) == error_mark_node)
11563 return false;
11565 outer_type = TREE_TYPE (exp);
11566 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11568 if (!inner_type)
11569 return false;
11571 /* Use precision rather then machine mode when we can, which gives
11572 the correct answer even for submode (bit-field) types. */
11573 if ((INTEGRAL_TYPE_P (outer_type)
11574 || POINTER_TYPE_P (outer_type)
11575 || TREE_CODE (outer_type) == OFFSET_TYPE)
11576 && (INTEGRAL_TYPE_P (inner_type)
11577 || POINTER_TYPE_P (inner_type)
11578 || TREE_CODE (inner_type) == OFFSET_TYPE))
11579 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11581 /* Otherwise fall back on comparing machine modes (e.g. for
11582 aggregate types, floats). */
11583 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11586 /* Return true iff conversion in EXP generates no instruction. Don't
11587 consider conversions changing the signedness. */
11589 static bool
11590 tree_sign_nop_conversion (const_tree exp)
11592 tree outer_type, inner_type;
11594 if (!tree_nop_conversion (exp))
11595 return false;
11597 outer_type = TREE_TYPE (exp);
11598 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11600 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11601 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11604 /* Strip conversions from EXP according to tree_nop_conversion and
11605 return the resulting expression. */
11607 tree
11608 tree_strip_nop_conversions (tree exp)
11610 while (tree_nop_conversion (exp))
11611 exp = TREE_OPERAND (exp, 0);
11612 return exp;
11615 /* Strip conversions from EXP according to tree_sign_nop_conversion
11616 and return the resulting expression. */
11618 tree
11619 tree_strip_sign_nop_conversions (tree exp)
11621 while (tree_sign_nop_conversion (exp))
11622 exp = TREE_OPERAND (exp, 0);
11623 return exp;
11626 /* Avoid any floating point extensions from EXP. */
11627 tree
11628 strip_float_extensions (tree exp)
11630 tree sub, expt, subt;
11632 /* For floating point constant look up the narrowest type that can hold
11633 it properly and handle it like (type)(narrowest_type)constant.
11634 This way we can optimize for instance a=a*2.0 where "a" is float
11635 but 2.0 is double constant. */
11636 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11638 REAL_VALUE_TYPE orig;
11639 tree type = NULL;
11641 orig = TREE_REAL_CST (exp);
11642 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11643 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11644 type = float_type_node;
11645 else if (TYPE_PRECISION (TREE_TYPE (exp))
11646 > TYPE_PRECISION (double_type_node)
11647 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11648 type = double_type_node;
11649 if (type)
11650 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11653 if (!CONVERT_EXPR_P (exp))
11654 return exp;
11656 sub = TREE_OPERAND (exp, 0);
11657 subt = TREE_TYPE (sub);
11658 expt = TREE_TYPE (exp);
11660 if (!FLOAT_TYPE_P (subt))
11661 return exp;
11663 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11664 return exp;
11666 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11667 return exp;
11669 return strip_float_extensions (sub);
11672 /* Strip out all handled components that produce invariant
11673 offsets. */
11675 const_tree
11676 strip_invariant_refs (const_tree op)
11678 while (handled_component_p (op))
11680 switch (TREE_CODE (op))
11682 case ARRAY_REF:
11683 case ARRAY_RANGE_REF:
11684 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11685 || TREE_OPERAND (op, 2) != NULL_TREE
11686 || TREE_OPERAND (op, 3) != NULL_TREE)
11687 return NULL;
11688 break;
11690 case COMPONENT_REF:
11691 if (TREE_OPERAND (op, 2) != NULL_TREE)
11692 return NULL;
11693 break;
11695 default:;
11697 op = TREE_OPERAND (op, 0);
11700 return op;
11703 static GTY(()) tree gcc_eh_personality_decl;
11705 /* Return the GCC personality function decl. */
11707 tree
11708 lhd_gcc_personality (void)
11710 if (!gcc_eh_personality_decl)
11711 gcc_eh_personality_decl = build_personality_function ("gcc");
11712 return gcc_eh_personality_decl;
11715 /* For languages with One Definition Rule, work out if
11716 trees are actually the same even if the tree representation
11717 differs. This handles only decls appearing in TYPE_NAME
11718 and TYPE_CONTEXT. That is NAMESPACE_DECL, TYPE_DECL,
11719 RECORD_TYPE and IDENTIFIER_NODE. */
11721 static bool
11722 same_for_odr (tree t1, tree t2)
11724 if (t1 == t2)
11725 return true;
11726 if (!t1 || !t2)
11727 return false;
11728 /* C and C++ FEs differ by using IDENTIFIER_NODE and TYPE_DECL. */
11729 if (TREE_CODE (t1) == IDENTIFIER_NODE
11730 && TREE_CODE (t2) == TYPE_DECL
11731 && DECL_FILE_SCOPE_P (t1))
11733 t2 = DECL_NAME (t2);
11734 gcc_assert (TREE_CODE (t2) == IDENTIFIER_NODE);
11736 if (TREE_CODE (t2) == IDENTIFIER_NODE
11737 && TREE_CODE (t1) == TYPE_DECL
11738 && DECL_FILE_SCOPE_P (t2))
11740 t1 = DECL_NAME (t1);
11741 gcc_assert (TREE_CODE (t1) == IDENTIFIER_NODE);
11743 if (TREE_CODE (t1) != TREE_CODE (t2))
11744 return false;
11745 if (TYPE_P (t1))
11746 return types_same_for_odr (t1, t2);
11747 if (DECL_P (t1))
11748 return decls_same_for_odr (t1, t2);
11749 return false;
11752 /* For languages with One Definition Rule, work out if
11753 decls are actually the same even if the tree representation
11754 differs. This handles only decls appearing in TYPE_NAME
11755 and TYPE_CONTEXT. That is NAMESPACE_DECL, TYPE_DECL,
11756 RECORD_TYPE and IDENTIFIER_NODE. */
11758 static bool
11759 decls_same_for_odr (tree decl1, tree decl2)
11761 if (decl1 && TREE_CODE (decl1) == TYPE_DECL
11762 && DECL_ORIGINAL_TYPE (decl1))
11763 decl1 = DECL_ORIGINAL_TYPE (decl1);
11764 if (decl2 && TREE_CODE (decl2) == TYPE_DECL
11765 && DECL_ORIGINAL_TYPE (decl2))
11766 decl2 = DECL_ORIGINAL_TYPE (decl2);
11767 if (decl1 == decl2)
11768 return true;
11769 if (!decl1 || !decl2)
11770 return false;
11771 gcc_checking_assert (DECL_P (decl1) && DECL_P (decl2));
11772 if (TREE_CODE (decl1) != TREE_CODE (decl2))
11773 return false;
11774 if (TREE_CODE (decl1) == TRANSLATION_UNIT_DECL)
11775 return true;
11776 if (TREE_CODE (decl1) != NAMESPACE_DECL
11777 && TREE_CODE (decl1) != TYPE_DECL)
11778 return false;
11779 if (!DECL_NAME (decl1))
11780 return false;
11781 gcc_checking_assert (TREE_CODE (DECL_NAME (decl1)) == IDENTIFIER_NODE);
11782 gcc_checking_assert (!DECL_NAME (decl2)
11783 || TREE_CODE (DECL_NAME (decl2)) == IDENTIFIER_NODE);
11784 if (DECL_NAME (decl1) != DECL_NAME (decl2))
11785 return false;
11786 return same_for_odr (DECL_CONTEXT (decl1),
11787 DECL_CONTEXT (decl2));
11790 /* For languages with One Definition Rule, work out if
11791 types are same even if the tree representation differs.
11792 This is non-trivial for LTO where minnor differences in
11793 the type representation may have prevented type merging
11794 to merge two copies of otherwise equivalent type. */
11796 bool
11797 types_same_for_odr (tree type1, tree type2)
11799 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
11800 type1 = TYPE_MAIN_VARIANT (type1);
11801 type2 = TYPE_MAIN_VARIANT (type2);
11802 if (type1 == type2)
11803 return true;
11805 /* If types are not structuraly same, do not bother to contnue.
11806 Match in the remainder of code would mean ODR violation. */
11807 if (!types_compatible_p (type1, type2))
11808 return false;
11810 #ifndef ENABLE_CHECKING
11811 if (!in_lto_p)
11812 return false;
11813 #endif
11815 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
11816 on the corresponding TYPE_STUB_DECL. */
11817 if (TYPE_STUB_DECL (type1) != TYPE_STUB_DECL (type2)
11818 && (!TYPE_STUB_DECL (type1)
11819 || !TYPE_STUB_DECL (type2)
11820 || !TREE_PUBLIC (TYPE_STUB_DECL (type1))
11821 || !TREE_PUBLIC (TYPE_STUB_DECL (type2))))
11822 return false;
11824 if (!TYPE_NAME (type1))
11825 return false;
11826 if (!decls_same_for_odr (TYPE_NAME (type1), TYPE_NAME (type2)))
11827 return false;
11828 if (!same_for_odr (TYPE_CONTEXT (type1), TYPE_CONTEXT (type2)))
11829 return false;
11830 /* When not in LTO the MAIN_VARIANT check should be the same. */
11831 gcc_assert (in_lto_p);
11833 return true;
11836 /* Try to find a base info of BINFO that would have its field decl at offset
11837 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11838 found, return, otherwise return NULL_TREE. */
11840 tree
11841 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
11843 tree type = BINFO_TYPE (binfo);
11845 while (true)
11847 HOST_WIDE_INT pos, size;
11848 tree fld;
11849 int i;
11851 if (types_same_for_odr (type, expected_type))
11852 return binfo;
11853 if (offset < 0)
11854 return NULL_TREE;
11856 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
11858 if (TREE_CODE (fld) != FIELD_DECL)
11859 continue;
11861 pos = int_bit_position (fld);
11862 size = tree_low_cst (DECL_SIZE (fld), 1);
11863 if (pos <= offset && (pos + size) > offset)
11864 break;
11866 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11867 return NULL_TREE;
11869 if (!DECL_ARTIFICIAL (fld))
11871 binfo = TYPE_BINFO (TREE_TYPE (fld));
11872 if (!binfo)
11873 return NULL_TREE;
11875 /* Offset 0 indicates the primary base, whose vtable contents are
11876 represented in the binfo for the derived class. */
11877 else if (offset != 0)
11879 tree base_binfo, found_binfo = NULL_TREE;
11880 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11881 if (types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
11883 found_binfo = base_binfo;
11884 break;
11886 if (!found_binfo)
11887 return NULL_TREE;
11888 binfo = found_binfo;
11891 type = TREE_TYPE (fld);
11892 offset -= pos;
11896 /* Returns true if X is a typedef decl. */
11898 bool
11899 is_typedef_decl (tree x)
11901 return (x && TREE_CODE (x) == TYPE_DECL
11902 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
11905 /* Returns true iff TYPE is a type variant created for a typedef. */
11907 bool
11908 typedef_variant_p (tree type)
11910 return is_typedef_decl (TYPE_NAME (type));
11913 /* Warn about a use of an identifier which was marked deprecated. */
11914 void
11915 warn_deprecated_use (tree node, tree attr)
11917 const char *msg;
11919 if (node == 0 || !warn_deprecated_decl)
11920 return;
11922 if (!attr)
11924 if (DECL_P (node))
11925 attr = DECL_ATTRIBUTES (node);
11926 else if (TYPE_P (node))
11928 tree decl = TYPE_STUB_DECL (node);
11929 if (decl)
11930 attr = lookup_attribute ("deprecated",
11931 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
11935 if (attr)
11936 attr = lookup_attribute ("deprecated", attr);
11938 if (attr)
11939 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
11940 else
11941 msg = NULL;
11943 if (DECL_P (node))
11945 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
11946 if (msg)
11947 warning (OPT_Wdeprecated_declarations,
11948 "%qD is deprecated (declared at %r%s:%d%R): %s",
11949 node, "locus", xloc.file, xloc.line, msg);
11950 else
11951 warning (OPT_Wdeprecated_declarations,
11952 "%qD is deprecated (declared at %r%s:%d%R)",
11953 node, "locus", xloc.file, xloc.line);
11955 else if (TYPE_P (node))
11957 tree what = NULL_TREE;
11958 tree decl = TYPE_STUB_DECL (node);
11960 if (TYPE_NAME (node))
11962 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
11963 what = TYPE_NAME (node);
11964 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
11965 && DECL_NAME (TYPE_NAME (node)))
11966 what = DECL_NAME (TYPE_NAME (node));
11969 if (decl)
11971 expanded_location xloc
11972 = expand_location (DECL_SOURCE_LOCATION (decl));
11973 if (what)
11975 if (msg)
11976 warning (OPT_Wdeprecated_declarations,
11977 "%qE is deprecated (declared at %r%s:%d%R): %s",
11978 what, "locus", xloc.file, xloc.line, msg);
11979 else
11980 warning (OPT_Wdeprecated_declarations,
11981 "%qE is deprecated (declared at %r%s:%d%R)",
11982 what, "locus", xloc.file, xloc.line);
11984 else
11986 if (msg)
11987 warning (OPT_Wdeprecated_declarations,
11988 "type is deprecated (declared at %r%s:%d%R): %s",
11989 "locus", xloc.file, xloc.line, msg);
11990 else
11991 warning (OPT_Wdeprecated_declarations,
11992 "type is deprecated (declared at %r%s:%d%R)",
11993 "locus", xloc.file, xloc.line);
11996 else
11998 if (what)
12000 if (msg)
12001 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
12002 what, msg);
12003 else
12004 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
12006 else
12008 if (msg)
12009 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
12010 msg);
12011 else
12012 warning (OPT_Wdeprecated_declarations, "type is deprecated");
12018 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12019 somewhere in it. */
12021 bool
12022 contains_bitfld_component_ref_p (const_tree ref)
12024 while (handled_component_p (ref))
12026 if (TREE_CODE (ref) == COMPONENT_REF
12027 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12028 return true;
12029 ref = TREE_OPERAND (ref, 0);
12032 return false;
12035 #include "gt-tree.h"