pretty-print.h (pp_base): Remove.
[official-gcc.git] / gcc / tree.c
blobc9f47d26260d1f3e5de4c24ac717f1ee84e27e9c
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 struct cgraph_node *node;
4890 if (!(node = cgraph_get_node (decl))
4891 || (!node->symbol.definition && !node->clones))
4893 if (node)
4894 cgraph_release_function_body (node);
4895 else
4897 release_function_body (decl);
4898 DECL_ARGUMENTS (decl) = NULL;
4899 DECL_RESULT (decl) = NULL;
4900 DECL_INITIAL (decl) = error_mark_node;
4903 if (gimple_has_body_p (decl))
4905 tree t;
4907 /* If DECL has a gimple body, then the context for its
4908 arguments must be DECL. Otherwise, it doesn't really
4909 matter, as we will not be emitting any code for DECL. In
4910 general, there may be other instances of DECL created by
4911 the front end and since PARM_DECLs are generally shared,
4912 their DECL_CONTEXT changes as the replicas of DECL are
4913 created. The only time where DECL_CONTEXT is important
4914 is for the FUNCTION_DECLs that have a gimple body (since
4915 the PARM_DECL will be used in the function's body). */
4916 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
4917 DECL_CONTEXT (t) = decl;
4920 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
4921 At this point, it is not needed anymore. */
4922 DECL_SAVED_TREE (decl) = NULL_TREE;
4924 /* Clear the abstract origin if it refers to a method. Otherwise
4925 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
4926 origin will not be output correctly. */
4927 if (DECL_ABSTRACT_ORIGIN (decl)
4928 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
4929 && RECORD_OR_UNION_TYPE_P
4930 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
4931 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
4933 /* Sometimes the C++ frontend doesn't manage to transform a temporary
4934 DECL_VINDEX referring to itself into a vtable slot number as it
4935 should. Happens with functions that are copied and then forgotten
4936 about. Just clear it, it won't matter anymore. */
4937 if (DECL_VINDEX (decl) && !host_integerp (DECL_VINDEX (decl), 0))
4938 DECL_VINDEX (decl) = NULL_TREE;
4940 else if (TREE_CODE (decl) == VAR_DECL)
4942 if ((DECL_EXTERNAL (decl)
4943 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
4944 || (decl_function_context (decl) && !TREE_STATIC (decl)))
4945 DECL_INITIAL (decl) = NULL_TREE;
4947 else if (TREE_CODE (decl) == TYPE_DECL
4948 || TREE_CODE (decl) == FIELD_DECL)
4949 DECL_INITIAL (decl) = NULL_TREE;
4950 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
4951 && DECL_INITIAL (decl)
4952 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
4954 /* Strip builtins from the translation-unit BLOCK. We still have targets
4955 without builtin_decl_explicit support and also builtins are shared
4956 nodes and thus we can't use TREE_CHAIN in multiple lists. */
4957 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
4958 while (*nextp)
4960 tree var = *nextp;
4961 if (TREE_CODE (var) == FUNCTION_DECL
4962 && DECL_BUILT_IN (var))
4963 *nextp = TREE_CHAIN (var);
4964 else
4965 nextp = &TREE_CHAIN (var);
4971 /* Data used when collecting DECLs and TYPEs for language data removal. */
4973 struct free_lang_data_d
4975 /* Worklist to avoid excessive recursion. */
4976 vec<tree> worklist;
4978 /* Set of traversed objects. Used to avoid duplicate visits. */
4979 struct pointer_set_t *pset;
4981 /* Array of symbols to process with free_lang_data_in_decl. */
4982 vec<tree> decls;
4984 /* Array of types to process with free_lang_data_in_type. */
4985 vec<tree> types;
4989 /* Save all language fields needed to generate proper debug information
4990 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
4992 static void
4993 save_debug_info_for_decl (tree t)
4995 /*struct saved_debug_info_d *sdi;*/
4997 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
4999 /* FIXME. Partial implementation for saving debug info removed. */
5003 /* Save all language fields needed to generate proper debug information
5004 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
5006 static void
5007 save_debug_info_for_type (tree t)
5009 /*struct saved_debug_info_d *sdi;*/
5011 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
5013 /* FIXME. Partial implementation for saving debug info removed. */
5017 /* Add type or decl T to one of the list of tree nodes that need their
5018 language data removed. The lists are held inside FLD. */
5020 static void
5021 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
5023 if (DECL_P (t))
5025 fld->decls.safe_push (t);
5026 if (debug_info_level > DINFO_LEVEL_TERSE)
5027 save_debug_info_for_decl (t);
5029 else if (TYPE_P (t))
5031 fld->types.safe_push (t);
5032 if (debug_info_level > DINFO_LEVEL_TERSE)
5033 save_debug_info_for_type (t);
5035 else
5036 gcc_unreachable ();
5039 /* Push tree node T into FLD->WORKLIST. */
5041 static inline void
5042 fld_worklist_push (tree t, struct free_lang_data_d *fld)
5044 if (t && !is_lang_specific (t) && !pointer_set_contains (fld->pset, t))
5045 fld->worklist.safe_push ((t));
5049 /* Operand callback helper for free_lang_data_in_node. *TP is the
5050 subtree operand being considered. */
5052 static tree
5053 find_decls_types_r (tree *tp, int *ws, void *data)
5055 tree t = *tp;
5056 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
5058 if (TREE_CODE (t) == TREE_LIST)
5059 return NULL_TREE;
5061 /* Language specific nodes will be removed, so there is no need
5062 to gather anything under them. */
5063 if (is_lang_specific (t))
5065 *ws = 0;
5066 return NULL_TREE;
5069 if (DECL_P (t))
5071 /* Note that walk_tree does not traverse every possible field in
5072 decls, so we have to do our own traversals here. */
5073 add_tree_to_fld_list (t, fld);
5075 fld_worklist_push (DECL_NAME (t), fld);
5076 fld_worklist_push (DECL_CONTEXT (t), fld);
5077 fld_worklist_push (DECL_SIZE (t), fld);
5078 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
5080 /* We are going to remove everything under DECL_INITIAL for
5081 TYPE_DECLs. No point walking them. */
5082 if (TREE_CODE (t) != TYPE_DECL)
5083 fld_worklist_push (DECL_INITIAL (t), fld);
5085 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
5086 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
5088 if (TREE_CODE (t) == FUNCTION_DECL)
5090 fld_worklist_push (DECL_ARGUMENTS (t), fld);
5091 fld_worklist_push (DECL_RESULT (t), fld);
5093 else if (TREE_CODE (t) == TYPE_DECL)
5095 fld_worklist_push (DECL_ARGUMENT_FLD (t), fld);
5096 fld_worklist_push (DECL_VINDEX (t), fld);
5097 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
5099 else if (TREE_CODE (t) == FIELD_DECL)
5101 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
5102 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
5103 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
5104 fld_worklist_push (DECL_FCONTEXT (t), fld);
5106 else if (TREE_CODE (t) == VAR_DECL)
5108 fld_worklist_push (DECL_SECTION_NAME (t), fld);
5109 fld_worklist_push (DECL_COMDAT_GROUP (t), fld);
5112 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
5113 && DECL_HAS_VALUE_EXPR_P (t))
5114 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
5116 if (TREE_CODE (t) != FIELD_DECL
5117 && TREE_CODE (t) != TYPE_DECL)
5118 fld_worklist_push (TREE_CHAIN (t), fld);
5119 *ws = 0;
5121 else if (TYPE_P (t))
5123 /* Note that walk_tree does not traverse every possible field in
5124 types, so we have to do our own traversals here. */
5125 add_tree_to_fld_list (t, fld);
5127 if (!RECORD_OR_UNION_TYPE_P (t))
5128 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
5129 fld_worklist_push (TYPE_SIZE (t), fld);
5130 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
5131 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
5132 fld_worklist_push (TYPE_POINTER_TO (t), fld);
5133 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
5134 fld_worklist_push (TYPE_NAME (t), fld);
5135 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
5136 them and thus do not and want not to reach unused pointer types
5137 this way. */
5138 if (!POINTER_TYPE_P (t))
5139 fld_worklist_push (TYPE_MINVAL (t), fld);
5140 if (!RECORD_OR_UNION_TYPE_P (t))
5141 fld_worklist_push (TYPE_MAXVAL (t), fld);
5142 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
5143 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
5144 do not and want not to reach unused variants this way. */
5145 if (TYPE_CONTEXT (t))
5147 tree ctx = TYPE_CONTEXT (t);
5148 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
5149 So push that instead. */
5150 while (ctx && TREE_CODE (ctx) == BLOCK)
5151 ctx = BLOCK_SUPERCONTEXT (ctx);
5152 fld_worklist_push (ctx, fld);
5154 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
5155 and want not to reach unused types this way. */
5157 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
5159 unsigned i;
5160 tree tem;
5161 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
5162 fld_worklist_push (TREE_TYPE (tem), fld);
5163 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
5164 if (tem
5165 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
5166 && TREE_CODE (tem) == TREE_LIST)
5169 fld_worklist_push (TREE_VALUE (tem), fld);
5170 tem = TREE_CHAIN (tem);
5172 while (tem);
5174 if (RECORD_OR_UNION_TYPE_P (t))
5176 tree tem;
5177 /* Push all TYPE_FIELDS - there can be interleaving interesting
5178 and non-interesting things. */
5179 tem = TYPE_FIELDS (t);
5180 while (tem)
5182 if (TREE_CODE (tem) == FIELD_DECL
5183 || TREE_CODE (tem) == TYPE_DECL)
5184 fld_worklist_push (tem, fld);
5185 tem = TREE_CHAIN (tem);
5189 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5190 *ws = 0;
5192 else if (TREE_CODE (t) == BLOCK)
5194 tree tem;
5195 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5196 fld_worklist_push (tem, fld);
5197 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5198 fld_worklist_push (tem, fld);
5199 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5202 if (TREE_CODE (t) != IDENTIFIER_NODE
5203 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5204 fld_worklist_push (TREE_TYPE (t), fld);
5206 return NULL_TREE;
5210 /* Find decls and types in T. */
5212 static void
5213 find_decls_types (tree t, struct free_lang_data_d *fld)
5215 while (1)
5217 if (!pointer_set_contains (fld->pset, t))
5218 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5219 if (fld->worklist.is_empty ())
5220 break;
5221 t = fld->worklist.pop ();
5225 /* Translate all the types in LIST with the corresponding runtime
5226 types. */
5228 static tree
5229 get_eh_types_for_runtime (tree list)
5231 tree head, prev;
5233 if (list == NULL_TREE)
5234 return NULL_TREE;
5236 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5237 prev = head;
5238 list = TREE_CHAIN (list);
5239 while (list)
5241 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5242 TREE_CHAIN (prev) = n;
5243 prev = TREE_CHAIN (prev);
5244 list = TREE_CHAIN (list);
5247 return head;
5251 /* Find decls and types referenced in EH region R and store them in
5252 FLD->DECLS and FLD->TYPES. */
5254 static void
5255 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5257 switch (r->type)
5259 case ERT_CLEANUP:
5260 break;
5262 case ERT_TRY:
5264 eh_catch c;
5266 /* The types referenced in each catch must first be changed to the
5267 EH types used at runtime. This removes references to FE types
5268 in the region. */
5269 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5271 c->type_list = get_eh_types_for_runtime (c->type_list);
5272 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5275 break;
5277 case ERT_ALLOWED_EXCEPTIONS:
5278 r->u.allowed.type_list
5279 = get_eh_types_for_runtime (r->u.allowed.type_list);
5280 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5281 break;
5283 case ERT_MUST_NOT_THROW:
5284 walk_tree (&r->u.must_not_throw.failure_decl,
5285 find_decls_types_r, fld, fld->pset);
5286 break;
5291 /* Find decls and types referenced in cgraph node N and store them in
5292 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5293 look for *every* kind of DECL and TYPE node reachable from N,
5294 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5295 NAMESPACE_DECLs, etc). */
5297 static void
5298 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5300 basic_block bb;
5301 struct function *fn;
5302 unsigned ix;
5303 tree t;
5305 find_decls_types (n->symbol.decl, fld);
5307 if (!gimple_has_body_p (n->symbol.decl))
5308 return;
5310 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5312 fn = DECL_STRUCT_FUNCTION (n->symbol.decl);
5314 /* Traverse locals. */
5315 FOR_EACH_LOCAL_DECL (fn, ix, t)
5316 find_decls_types (t, fld);
5318 /* Traverse EH regions in FN. */
5320 eh_region r;
5321 FOR_ALL_EH_REGION_FN (r, fn)
5322 find_decls_types_in_eh_region (r, fld);
5325 /* Traverse every statement in FN. */
5326 FOR_EACH_BB_FN (bb, fn)
5328 gimple_stmt_iterator si;
5329 unsigned i;
5331 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5333 gimple phi = gsi_stmt (si);
5335 for (i = 0; i < gimple_phi_num_args (phi); i++)
5337 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5338 find_decls_types (*arg_p, fld);
5342 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5344 gimple stmt = gsi_stmt (si);
5346 if (is_gimple_call (stmt))
5347 find_decls_types (gimple_call_fntype (stmt), fld);
5349 for (i = 0; i < gimple_num_ops (stmt); i++)
5351 tree arg = gimple_op (stmt, i);
5352 find_decls_types (arg, fld);
5359 /* Find decls and types referenced in varpool node N and store them in
5360 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5361 look for *every* kind of DECL and TYPE node reachable from N,
5362 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5363 NAMESPACE_DECLs, etc). */
5365 static void
5366 find_decls_types_in_var (struct varpool_node *v, struct free_lang_data_d *fld)
5368 find_decls_types (v->symbol.decl, fld);
5371 /* If T needs an assembler name, have one created for it. */
5373 void
5374 assign_assembler_name_if_neeeded (tree t)
5376 if (need_assembler_name_p (t))
5378 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5379 diagnostics that use input_location to show locus
5380 information. The problem here is that, at this point,
5381 input_location is generally anchored to the end of the file
5382 (since the parser is long gone), so we don't have a good
5383 position to pin it to.
5385 To alleviate this problem, this uses the location of T's
5386 declaration. Examples of this are
5387 testsuite/g++.dg/template/cond2.C and
5388 testsuite/g++.dg/template/pr35240.C. */
5389 location_t saved_location = input_location;
5390 input_location = DECL_SOURCE_LOCATION (t);
5392 decl_assembler_name (t);
5394 input_location = saved_location;
5399 /* Free language specific information for every operand and expression
5400 in every node of the call graph. This process operates in three stages:
5402 1- Every callgraph node and varpool node is traversed looking for
5403 decls and types embedded in them. This is a more exhaustive
5404 search than that done by find_referenced_vars, because it will
5405 also collect individual fields, decls embedded in types, etc.
5407 2- All the decls found are sent to free_lang_data_in_decl.
5409 3- All the types found are sent to free_lang_data_in_type.
5411 The ordering between decls and types is important because
5412 free_lang_data_in_decl sets assembler names, which includes
5413 mangling. So types cannot be freed up until assembler names have
5414 been set up. */
5416 static void
5417 free_lang_data_in_cgraph (void)
5419 struct cgraph_node *n;
5420 struct varpool_node *v;
5421 struct free_lang_data_d fld;
5422 tree t;
5423 unsigned i;
5424 alias_pair *p;
5426 /* Initialize sets and arrays to store referenced decls and types. */
5427 fld.pset = pointer_set_create ();
5428 fld.worklist.create (0);
5429 fld.decls.create (100);
5430 fld.types.create (100);
5432 /* Find decls and types in the body of every function in the callgraph. */
5433 FOR_EACH_FUNCTION (n)
5434 find_decls_types_in_node (n, &fld);
5436 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5437 find_decls_types (p->decl, &fld);
5439 /* Find decls and types in every varpool symbol. */
5440 FOR_EACH_VARIABLE (v)
5441 find_decls_types_in_var (v, &fld);
5443 /* Set the assembler name on every decl found. We need to do this
5444 now because free_lang_data_in_decl will invalidate data needed
5445 for mangling. This breaks mangling on interdependent decls. */
5446 FOR_EACH_VEC_ELT (fld.decls, i, t)
5447 assign_assembler_name_if_neeeded (t);
5449 /* Traverse every decl found freeing its language data. */
5450 FOR_EACH_VEC_ELT (fld.decls, i, t)
5451 free_lang_data_in_decl (t);
5453 /* Traverse every type found freeing its language data. */
5454 FOR_EACH_VEC_ELT (fld.types, i, t)
5455 free_lang_data_in_type (t);
5457 pointer_set_destroy (fld.pset);
5458 fld.worklist.release ();
5459 fld.decls.release ();
5460 fld.types.release ();
5464 /* Free resources that are used by FE but are not needed once they are done. */
5466 static unsigned
5467 free_lang_data (void)
5469 unsigned i;
5471 /* If we are the LTO frontend we have freed lang-specific data already. */
5472 if (in_lto_p
5473 || !flag_generate_lto)
5474 return 0;
5476 /* Allocate and assign alias sets to the standard integer types
5477 while the slots are still in the way the frontends generated them. */
5478 for (i = 0; i < itk_none; ++i)
5479 if (integer_types[i])
5480 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5482 /* Traverse the IL resetting language specific information for
5483 operands, expressions, etc. */
5484 free_lang_data_in_cgraph ();
5486 /* Create gimple variants for common types. */
5487 ptrdiff_type_node = integer_type_node;
5488 fileptr_type_node = ptr_type_node;
5490 /* Reset some langhooks. Do not reset types_compatible_p, it may
5491 still be used indirectly via the get_alias_set langhook. */
5492 lang_hooks.dwarf_name = lhd_dwarf_name;
5493 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5494 /* We do not want the default decl_assembler_name implementation,
5495 rather if we have fixed everything we want a wrapper around it
5496 asserting that all non-local symbols already got their assembler
5497 name and only produce assembler names for local symbols. Or rather
5498 make sure we never call decl_assembler_name on local symbols and
5499 devise a separate, middle-end private scheme for it. */
5501 /* Reset diagnostic machinery. */
5502 tree_diagnostics_defaults (global_dc);
5504 return 0;
5508 struct simple_ipa_opt_pass pass_ipa_free_lang_data =
5511 SIMPLE_IPA_PASS,
5512 "*free_lang_data", /* name */
5513 OPTGROUP_NONE, /* optinfo_flags */
5514 NULL, /* gate */
5515 free_lang_data, /* execute */
5516 NULL, /* sub */
5517 NULL, /* next */
5518 0, /* static_pass_number */
5519 TV_IPA_FREE_LANG_DATA, /* tv_id */
5520 0, /* properties_required */
5521 0, /* properties_provided */
5522 0, /* properties_destroyed */
5523 0, /* todo_flags_start */
5524 0 /* todo_flags_finish */
5528 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
5529 ATTR_NAME. Also used internally by remove_attribute(). */
5530 bool
5531 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
5533 size_t ident_len = IDENTIFIER_LENGTH (ident);
5535 if (ident_len == attr_len)
5537 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
5538 return true;
5540 else if (ident_len == attr_len + 4)
5542 /* There is the possibility that ATTR is 'text' and IDENT is
5543 '__text__'. */
5544 const char *p = IDENTIFIER_POINTER (ident);
5545 if (p[0] == '_' && p[1] == '_'
5546 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5547 && strncmp (attr_name, p + 2, attr_len) == 0)
5548 return true;
5551 return false;
5554 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
5555 of ATTR_NAME, and LIST is not NULL_TREE. */
5556 tree
5557 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
5559 while (list)
5561 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5563 if (ident_len == attr_len)
5565 if (!strcmp (attr_name,
5566 IDENTIFIER_POINTER (get_attribute_name (list))))
5567 break;
5569 /* TODO: If we made sure that attributes were stored in the
5570 canonical form without '__...__' (ie, as in 'text' as opposed
5571 to '__text__') then we could avoid the following case. */
5572 else if (ident_len == attr_len + 4)
5574 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5575 if (p[0] == '_' && p[1] == '_'
5576 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5577 && strncmp (attr_name, p + 2, attr_len) == 0)
5578 break;
5580 list = TREE_CHAIN (list);
5583 return list;
5586 /* A variant of lookup_attribute() that can be used with an identifier
5587 as the first argument, and where the identifier can be either
5588 'text' or '__text__'.
5590 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
5591 return a pointer to the attribute's list element if the attribute
5592 is part of the list, or NULL_TREE if not found. If the attribute
5593 appears more than once, this only returns the first occurrence; the
5594 TREE_CHAIN of the return value should be passed back in if further
5595 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
5596 can be in the form 'text' or '__text__'. */
5597 static tree
5598 lookup_ident_attribute (tree attr_identifier, tree list)
5600 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
5602 while (list)
5604 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
5605 == IDENTIFIER_NODE);
5607 /* Identifiers can be compared directly for equality. */
5608 if (attr_identifier == get_attribute_name (list))
5609 break;
5611 /* If they are not equal, they may still be one in the form
5612 'text' while the other one is in the form '__text__'. TODO:
5613 If we were storing attributes in normalized 'text' form, then
5614 this could all go away and we could take full advantage of
5615 the fact that we're comparing identifiers. :-) */
5617 size_t attr_len = IDENTIFIER_LENGTH (attr_identifier);
5618 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5620 if (ident_len == attr_len + 4)
5622 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5623 const char *q = IDENTIFIER_POINTER (attr_identifier);
5624 if (p[0] == '_' && p[1] == '_'
5625 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5626 && strncmp (q, p + 2, attr_len) == 0)
5627 break;
5629 else if (ident_len + 4 == attr_len)
5631 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5632 const char *q = IDENTIFIER_POINTER (attr_identifier);
5633 if (q[0] == '_' && q[1] == '_'
5634 && q[attr_len - 2] == '_' && q[attr_len - 1] == '_'
5635 && strncmp (q + 2, p, ident_len) == 0)
5636 break;
5639 list = TREE_CHAIN (list);
5642 return list;
5645 /* Remove any instances of attribute ATTR_NAME in LIST and return the
5646 modified list. */
5648 tree
5649 remove_attribute (const char *attr_name, tree list)
5651 tree *p;
5652 size_t attr_len = strlen (attr_name);
5654 gcc_checking_assert (attr_name[0] != '_');
5656 for (p = &list; *p; )
5658 tree l = *p;
5659 /* TODO: If we were storing attributes in normalized form, here
5660 we could use a simple strcmp(). */
5661 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
5662 *p = TREE_CHAIN (l);
5663 else
5664 p = &TREE_CHAIN (l);
5667 return list;
5670 /* Return an attribute list that is the union of a1 and a2. */
5672 tree
5673 merge_attributes (tree a1, tree a2)
5675 tree attributes;
5677 /* Either one unset? Take the set one. */
5679 if ((attributes = a1) == 0)
5680 attributes = a2;
5682 /* One that completely contains the other? Take it. */
5684 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
5686 if (attribute_list_contained (a2, a1))
5687 attributes = a2;
5688 else
5690 /* Pick the longest list, and hang on the other list. */
5692 if (list_length (a1) < list_length (a2))
5693 attributes = a2, a2 = a1;
5695 for (; a2 != 0; a2 = TREE_CHAIN (a2))
5697 tree a;
5698 for (a = lookup_ident_attribute (get_attribute_name (a2),
5699 attributes);
5700 a != NULL_TREE && !attribute_value_equal (a, a2);
5701 a = lookup_ident_attribute (get_attribute_name (a2),
5702 TREE_CHAIN (a)))
5704 if (a == NULL_TREE)
5706 a1 = copy_node (a2);
5707 TREE_CHAIN (a1) = attributes;
5708 attributes = a1;
5713 return attributes;
5716 /* Given types T1 and T2, merge their attributes and return
5717 the result. */
5719 tree
5720 merge_type_attributes (tree t1, tree t2)
5722 return merge_attributes (TYPE_ATTRIBUTES (t1),
5723 TYPE_ATTRIBUTES (t2));
5726 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
5727 the result. */
5729 tree
5730 merge_decl_attributes (tree olddecl, tree newdecl)
5732 return merge_attributes (DECL_ATTRIBUTES (olddecl),
5733 DECL_ATTRIBUTES (newdecl));
5736 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
5738 /* Specialization of merge_decl_attributes for various Windows targets.
5740 This handles the following situation:
5742 __declspec (dllimport) int foo;
5743 int foo;
5745 The second instance of `foo' nullifies the dllimport. */
5747 tree
5748 merge_dllimport_decl_attributes (tree old, tree new_tree)
5750 tree a;
5751 int delete_dllimport_p = 1;
5753 /* What we need to do here is remove from `old' dllimport if it doesn't
5754 appear in `new'. dllimport behaves like extern: if a declaration is
5755 marked dllimport and a definition appears later, then the object
5756 is not dllimport'd. We also remove a `new' dllimport if the old list
5757 contains dllexport: dllexport always overrides dllimport, regardless
5758 of the order of declaration. */
5759 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
5760 delete_dllimport_p = 0;
5761 else if (DECL_DLLIMPORT_P (new_tree)
5762 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
5764 DECL_DLLIMPORT_P (new_tree) = 0;
5765 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
5766 "dllimport ignored", new_tree);
5768 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
5770 /* Warn about overriding a symbol that has already been used, e.g.:
5771 extern int __attribute__ ((dllimport)) foo;
5772 int* bar () {return &foo;}
5773 int foo;
5775 if (TREE_USED (old))
5777 warning (0, "%q+D redeclared without dllimport attribute "
5778 "after being referenced with dll linkage", new_tree);
5779 /* If we have used a variable's address with dllimport linkage,
5780 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
5781 decl may already have had TREE_CONSTANT computed.
5782 We still remove the attribute so that assembler code refers
5783 to '&foo rather than '_imp__foo'. */
5784 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
5785 DECL_DLLIMPORT_P (new_tree) = 1;
5788 /* Let an inline definition silently override the external reference,
5789 but otherwise warn about attribute inconsistency. */
5790 else if (TREE_CODE (new_tree) == VAR_DECL
5791 || !DECL_DECLARED_INLINE_P (new_tree))
5792 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
5793 "previous dllimport ignored", new_tree);
5795 else
5796 delete_dllimport_p = 0;
5798 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
5800 if (delete_dllimport_p)
5801 a = remove_attribute ("dllimport", a);
5803 return a;
5806 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
5807 struct attribute_spec.handler. */
5809 tree
5810 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
5811 bool *no_add_attrs)
5813 tree node = *pnode;
5814 bool is_dllimport;
5816 /* These attributes may apply to structure and union types being created,
5817 but otherwise should pass to the declaration involved. */
5818 if (!DECL_P (node))
5820 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
5821 | (int) ATTR_FLAG_ARRAY_NEXT))
5823 *no_add_attrs = true;
5824 return tree_cons (name, args, NULL_TREE);
5826 if (TREE_CODE (node) == RECORD_TYPE
5827 || TREE_CODE (node) == UNION_TYPE)
5829 node = TYPE_NAME (node);
5830 if (!node)
5831 return NULL_TREE;
5833 else
5835 warning (OPT_Wattributes, "%qE attribute ignored",
5836 name);
5837 *no_add_attrs = true;
5838 return NULL_TREE;
5842 if (TREE_CODE (node) != FUNCTION_DECL
5843 && TREE_CODE (node) != VAR_DECL
5844 && TREE_CODE (node) != TYPE_DECL)
5846 *no_add_attrs = true;
5847 warning (OPT_Wattributes, "%qE attribute ignored",
5848 name);
5849 return NULL_TREE;
5852 if (TREE_CODE (node) == TYPE_DECL
5853 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
5854 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
5856 *no_add_attrs = true;
5857 warning (OPT_Wattributes, "%qE attribute ignored",
5858 name);
5859 return NULL_TREE;
5862 is_dllimport = is_attribute_p ("dllimport", name);
5864 /* Report error on dllimport ambiguities seen now before they cause
5865 any damage. */
5866 if (is_dllimport)
5868 /* Honor any target-specific overrides. */
5869 if (!targetm.valid_dllimport_attribute_p (node))
5870 *no_add_attrs = true;
5872 else if (TREE_CODE (node) == FUNCTION_DECL
5873 && DECL_DECLARED_INLINE_P (node))
5875 warning (OPT_Wattributes, "inline function %q+D declared as "
5876 " dllimport: attribute ignored", node);
5877 *no_add_attrs = true;
5879 /* Like MS, treat definition of dllimported variables and
5880 non-inlined functions on declaration as syntax errors. */
5881 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
5883 error ("function %q+D definition is marked dllimport", node);
5884 *no_add_attrs = true;
5887 else if (TREE_CODE (node) == VAR_DECL)
5889 if (DECL_INITIAL (node))
5891 error ("variable %q+D definition is marked dllimport",
5892 node);
5893 *no_add_attrs = true;
5896 /* `extern' needn't be specified with dllimport.
5897 Specify `extern' now and hope for the best. Sigh. */
5898 DECL_EXTERNAL (node) = 1;
5899 /* Also, implicitly give dllimport'd variables declared within
5900 a function global scope, unless declared static. */
5901 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
5902 TREE_PUBLIC (node) = 1;
5905 if (*no_add_attrs == false)
5906 DECL_DLLIMPORT_P (node) = 1;
5908 else if (TREE_CODE (node) == FUNCTION_DECL
5909 && DECL_DECLARED_INLINE_P (node)
5910 && flag_keep_inline_dllexport)
5911 /* An exported function, even if inline, must be emitted. */
5912 DECL_EXTERNAL (node) = 0;
5914 /* Report error if symbol is not accessible at global scope. */
5915 if (!TREE_PUBLIC (node)
5916 && (TREE_CODE (node) == VAR_DECL
5917 || TREE_CODE (node) == FUNCTION_DECL))
5919 error ("external linkage required for symbol %q+D because of "
5920 "%qE attribute", node, name);
5921 *no_add_attrs = true;
5924 /* A dllexport'd entity must have default visibility so that other
5925 program units (shared libraries or the main executable) can see
5926 it. A dllimport'd entity must have default visibility so that
5927 the linker knows that undefined references within this program
5928 unit can be resolved by the dynamic linker. */
5929 if (!*no_add_attrs)
5931 if (DECL_VISIBILITY_SPECIFIED (node)
5932 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
5933 error ("%qE implies default visibility, but %qD has already "
5934 "been declared with a different visibility",
5935 name, node);
5936 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
5937 DECL_VISIBILITY_SPECIFIED (node) = 1;
5940 return NULL_TREE;
5943 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
5945 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
5946 of the various TYPE_QUAL values. */
5948 static void
5949 set_type_quals (tree type, int type_quals)
5951 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
5952 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
5953 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
5954 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
5957 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
5959 bool
5960 check_qualified_type (const_tree cand, const_tree base, int type_quals)
5962 return (TYPE_QUALS (cand) == type_quals
5963 && TYPE_NAME (cand) == TYPE_NAME (base)
5964 /* Apparently this is needed for Objective-C. */
5965 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5966 /* Check alignment. */
5967 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
5968 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5969 TYPE_ATTRIBUTES (base)));
5972 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
5974 static bool
5975 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
5977 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
5978 && TYPE_NAME (cand) == TYPE_NAME (base)
5979 /* Apparently this is needed for Objective-C. */
5980 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5981 /* Check alignment. */
5982 && TYPE_ALIGN (cand) == align
5983 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5984 TYPE_ATTRIBUTES (base)));
5987 /* Return a version of the TYPE, qualified as indicated by the
5988 TYPE_QUALS, if one exists. If no qualified version exists yet,
5989 return NULL_TREE. */
5991 tree
5992 get_qualified_type (tree type, int type_quals)
5994 tree t;
5996 if (TYPE_QUALS (type) == type_quals)
5997 return type;
5999 /* Search the chain of variants to see if there is already one there just
6000 like the one we need to have. If so, use that existing one. We must
6001 preserve the TYPE_NAME, since there is code that depends on this. */
6002 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6003 if (check_qualified_type (t, type, type_quals))
6004 return t;
6006 return NULL_TREE;
6009 /* Like get_qualified_type, but creates the type if it does not
6010 exist. This function never returns NULL_TREE. */
6012 tree
6013 build_qualified_type (tree type, int type_quals)
6015 tree t;
6017 /* See if we already have the appropriate qualified variant. */
6018 t = get_qualified_type (type, type_quals);
6020 /* If not, build it. */
6021 if (!t)
6023 t = build_variant_type_copy (type);
6024 set_type_quals (t, type_quals);
6026 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6027 /* Propagate structural equality. */
6028 SET_TYPE_STRUCTURAL_EQUALITY (t);
6029 else if (TYPE_CANONICAL (type) != type)
6030 /* Build the underlying canonical type, since it is different
6031 from TYPE. */
6032 TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
6033 type_quals);
6034 else
6035 /* T is its own canonical type. */
6036 TYPE_CANONICAL (t) = t;
6040 return t;
6043 /* Create a variant of type T with alignment ALIGN. */
6045 tree
6046 build_aligned_type (tree type, unsigned int align)
6048 tree t;
6050 if (TYPE_PACKED (type)
6051 || TYPE_ALIGN (type) == align)
6052 return type;
6054 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6055 if (check_aligned_type (t, type, align))
6056 return t;
6058 t = build_variant_type_copy (type);
6059 TYPE_ALIGN (t) = align;
6061 return t;
6064 /* Create a new distinct copy of TYPE. The new type is made its own
6065 MAIN_VARIANT. If TYPE requires structural equality checks, the
6066 resulting type requires structural equality checks; otherwise, its
6067 TYPE_CANONICAL points to itself. */
6069 tree
6070 build_distinct_type_copy (tree type)
6072 tree t = copy_node (type);
6074 TYPE_POINTER_TO (t) = 0;
6075 TYPE_REFERENCE_TO (t) = 0;
6077 /* Set the canonical type either to a new equivalence class, or
6078 propagate the need for structural equality checks. */
6079 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6080 SET_TYPE_STRUCTURAL_EQUALITY (t);
6081 else
6082 TYPE_CANONICAL (t) = t;
6084 /* Make it its own variant. */
6085 TYPE_MAIN_VARIANT (t) = t;
6086 TYPE_NEXT_VARIANT (t) = 0;
6088 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
6089 whose TREE_TYPE is not t. This can also happen in the Ada
6090 frontend when using subtypes. */
6092 return t;
6095 /* Create a new variant of TYPE, equivalent but distinct. This is so
6096 the caller can modify it. TYPE_CANONICAL for the return type will
6097 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
6098 are considered equal by the language itself (or that both types
6099 require structural equality checks). */
6101 tree
6102 build_variant_type_copy (tree type)
6104 tree t, m = TYPE_MAIN_VARIANT (type);
6106 t = build_distinct_type_copy (type);
6108 /* Since we're building a variant, assume that it is a non-semantic
6109 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
6110 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
6112 /* Add the new type to the chain of variants of TYPE. */
6113 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
6114 TYPE_NEXT_VARIANT (m) = t;
6115 TYPE_MAIN_VARIANT (t) = m;
6117 return t;
6120 /* Return true if the from tree in both tree maps are equal. */
6123 tree_map_base_eq (const void *va, const void *vb)
6125 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6126 *const b = (const struct tree_map_base *) vb;
6127 return (a->from == b->from);
6130 /* Hash a from tree in a tree_base_map. */
6132 unsigned int
6133 tree_map_base_hash (const void *item)
6135 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6138 /* Return true if this tree map structure is marked for garbage collection
6139 purposes. We simply return true if the from tree is marked, so that this
6140 structure goes away when the from tree goes away. */
6143 tree_map_base_marked_p (const void *p)
6145 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6148 /* Hash a from tree in a tree_map. */
6150 unsigned int
6151 tree_map_hash (const void *item)
6153 return (((const struct tree_map *) item)->hash);
6156 /* Hash a from tree in a tree_decl_map. */
6158 unsigned int
6159 tree_decl_map_hash (const void *item)
6161 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6164 /* Return the initialization priority for DECL. */
6166 priority_type
6167 decl_init_priority_lookup (tree decl)
6169 struct tree_priority_map *h;
6170 struct tree_map_base in;
6172 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6173 in.from = decl;
6174 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6175 return h ? h->init : DEFAULT_INIT_PRIORITY;
6178 /* Return the finalization priority for DECL. */
6180 priority_type
6181 decl_fini_priority_lookup (tree decl)
6183 struct tree_priority_map *h;
6184 struct tree_map_base in;
6186 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6187 in.from = decl;
6188 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6189 return h ? h->fini : DEFAULT_INIT_PRIORITY;
6192 /* Return the initialization and finalization priority information for
6193 DECL. If there is no previous priority information, a freshly
6194 allocated structure is returned. */
6196 static struct tree_priority_map *
6197 decl_priority_info (tree decl)
6199 struct tree_priority_map in;
6200 struct tree_priority_map *h;
6201 void **loc;
6203 in.base.from = decl;
6204 loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
6205 h = (struct tree_priority_map *) *loc;
6206 if (!h)
6208 h = ggc_alloc_cleared_tree_priority_map ();
6209 *loc = h;
6210 h->base.from = decl;
6211 h->init = DEFAULT_INIT_PRIORITY;
6212 h->fini = DEFAULT_INIT_PRIORITY;
6215 return h;
6218 /* Set the initialization priority for DECL to PRIORITY. */
6220 void
6221 decl_init_priority_insert (tree decl, priority_type priority)
6223 struct tree_priority_map *h;
6225 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6226 if (priority == DEFAULT_INIT_PRIORITY)
6227 return;
6228 h = decl_priority_info (decl);
6229 h->init = priority;
6232 /* Set the finalization priority for DECL to PRIORITY. */
6234 void
6235 decl_fini_priority_insert (tree decl, priority_type priority)
6237 struct tree_priority_map *h;
6239 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6240 if (priority == DEFAULT_INIT_PRIORITY)
6241 return;
6242 h = decl_priority_info (decl);
6243 h->fini = priority;
6246 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6248 static void
6249 print_debug_expr_statistics (void)
6251 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6252 (long) htab_size (debug_expr_for_decl),
6253 (long) htab_elements (debug_expr_for_decl),
6254 htab_collisions (debug_expr_for_decl));
6257 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6259 static void
6260 print_value_expr_statistics (void)
6262 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6263 (long) htab_size (value_expr_for_decl),
6264 (long) htab_elements (value_expr_for_decl),
6265 htab_collisions (value_expr_for_decl));
6268 /* Lookup a debug expression for FROM, and return it if we find one. */
6270 tree
6271 decl_debug_expr_lookup (tree from)
6273 struct tree_decl_map *h, in;
6274 in.base.from = from;
6276 h = (struct tree_decl_map *)
6277 htab_find_with_hash (debug_expr_for_decl, &in, DECL_UID (from));
6278 if (h)
6279 return h->to;
6280 return NULL_TREE;
6283 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6285 void
6286 decl_debug_expr_insert (tree from, tree to)
6288 struct tree_decl_map *h;
6289 void **loc;
6291 h = ggc_alloc_tree_decl_map ();
6292 h->base.from = from;
6293 h->to = to;
6294 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, DECL_UID (from),
6295 INSERT);
6296 *(struct tree_decl_map **) loc = h;
6299 /* Lookup a value expression for FROM, and return it if we find one. */
6301 tree
6302 decl_value_expr_lookup (tree from)
6304 struct tree_decl_map *h, in;
6305 in.base.from = from;
6307 h = (struct tree_decl_map *)
6308 htab_find_with_hash (value_expr_for_decl, &in, DECL_UID (from));
6309 if (h)
6310 return h->to;
6311 return NULL_TREE;
6314 /* Insert a mapping FROM->TO in the value expression hashtable. */
6316 void
6317 decl_value_expr_insert (tree from, tree to)
6319 struct tree_decl_map *h;
6320 void **loc;
6322 h = ggc_alloc_tree_decl_map ();
6323 h->base.from = from;
6324 h->to = to;
6325 loc = htab_find_slot_with_hash (value_expr_for_decl, h, DECL_UID (from),
6326 INSERT);
6327 *(struct tree_decl_map **) loc = h;
6330 /* Lookup a vector of debug arguments for FROM, and return it if we
6331 find one. */
6333 vec<tree, va_gc> **
6334 decl_debug_args_lookup (tree from)
6336 struct tree_vec_map *h, in;
6338 if (!DECL_HAS_DEBUG_ARGS_P (from))
6339 return NULL;
6340 gcc_checking_assert (debug_args_for_decl != NULL);
6341 in.base.from = from;
6342 h = (struct tree_vec_map *)
6343 htab_find_with_hash (debug_args_for_decl, &in, DECL_UID (from));
6344 if (h)
6345 return &h->to;
6346 return NULL;
6349 /* Insert a mapping FROM->empty vector of debug arguments in the value
6350 expression hashtable. */
6352 vec<tree, va_gc> **
6353 decl_debug_args_insert (tree from)
6355 struct tree_vec_map *h;
6356 void **loc;
6358 if (DECL_HAS_DEBUG_ARGS_P (from))
6359 return decl_debug_args_lookup (from);
6360 if (debug_args_for_decl == NULL)
6361 debug_args_for_decl = htab_create_ggc (64, tree_vec_map_hash,
6362 tree_vec_map_eq, 0);
6363 h = ggc_alloc_tree_vec_map ();
6364 h->base.from = from;
6365 h->to = NULL;
6366 loc = htab_find_slot_with_hash (debug_args_for_decl, h, DECL_UID (from),
6367 INSERT);
6368 *(struct tree_vec_map **) loc = h;
6369 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6370 return &h->to;
6373 /* Hashing of types so that we don't make duplicates.
6374 The entry point is `type_hash_canon'. */
6376 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6377 with types in the TREE_VALUE slots), by adding the hash codes
6378 of the individual types. */
6380 static unsigned int
6381 type_hash_list (const_tree list, hashval_t hashcode)
6383 const_tree tail;
6385 for (tail = list; tail; tail = TREE_CHAIN (tail))
6386 if (TREE_VALUE (tail) != error_mark_node)
6387 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
6388 hashcode);
6390 return hashcode;
6393 /* These are the Hashtable callback functions. */
6395 /* Returns true iff the types are equivalent. */
6397 static int
6398 type_hash_eq (const void *va, const void *vb)
6400 const struct type_hash *const a = (const struct type_hash *) va,
6401 *const b = (const struct type_hash *) vb;
6403 /* First test the things that are the same for all types. */
6404 if (a->hash != b->hash
6405 || TREE_CODE (a->type) != TREE_CODE (b->type)
6406 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6407 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6408 TYPE_ATTRIBUTES (b->type))
6409 || (TREE_CODE (a->type) != COMPLEX_TYPE
6410 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6411 return 0;
6413 /* Be careful about comparing arrays before and after the element type
6414 has been completed; don't compare TYPE_ALIGN unless both types are
6415 complete. */
6416 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6417 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6418 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6419 return 0;
6421 switch (TREE_CODE (a->type))
6423 case VOID_TYPE:
6424 case COMPLEX_TYPE:
6425 case POINTER_TYPE:
6426 case REFERENCE_TYPE:
6427 case NULLPTR_TYPE:
6428 return 1;
6430 case VECTOR_TYPE:
6431 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6433 case ENUMERAL_TYPE:
6434 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6435 && !(TYPE_VALUES (a->type)
6436 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6437 && TYPE_VALUES (b->type)
6438 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6439 && type_list_equal (TYPE_VALUES (a->type),
6440 TYPE_VALUES (b->type))))
6441 return 0;
6443 /* ... fall through ... */
6445 case INTEGER_TYPE:
6446 case REAL_TYPE:
6447 case BOOLEAN_TYPE:
6448 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6449 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6450 TYPE_MAX_VALUE (b->type)))
6451 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6452 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6453 TYPE_MIN_VALUE (b->type))));
6455 case FIXED_POINT_TYPE:
6456 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6458 case OFFSET_TYPE:
6459 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6461 case METHOD_TYPE:
6462 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6463 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6464 || (TYPE_ARG_TYPES (a->type)
6465 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6466 && TYPE_ARG_TYPES (b->type)
6467 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6468 && type_list_equal (TYPE_ARG_TYPES (a->type),
6469 TYPE_ARG_TYPES (b->type)))))
6470 break;
6471 return 0;
6472 case ARRAY_TYPE:
6473 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6475 case RECORD_TYPE:
6476 case UNION_TYPE:
6477 case QUAL_UNION_TYPE:
6478 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6479 || (TYPE_FIELDS (a->type)
6480 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6481 && TYPE_FIELDS (b->type)
6482 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6483 && type_list_equal (TYPE_FIELDS (a->type),
6484 TYPE_FIELDS (b->type))));
6486 case FUNCTION_TYPE:
6487 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6488 || (TYPE_ARG_TYPES (a->type)
6489 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6490 && TYPE_ARG_TYPES (b->type)
6491 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6492 && type_list_equal (TYPE_ARG_TYPES (a->type),
6493 TYPE_ARG_TYPES (b->type))))
6494 break;
6495 return 0;
6497 default:
6498 return 0;
6501 if (lang_hooks.types.type_hash_eq != NULL)
6502 return lang_hooks.types.type_hash_eq (a->type, b->type);
6504 return 1;
6507 /* Return the cached hash value. */
6509 static hashval_t
6510 type_hash_hash (const void *item)
6512 return ((const struct type_hash *) item)->hash;
6515 /* Look in the type hash table for a type isomorphic to TYPE.
6516 If one is found, return it. Otherwise return 0. */
6518 static tree
6519 type_hash_lookup (hashval_t hashcode, tree type)
6521 struct type_hash *h, in;
6523 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6524 must call that routine before comparing TYPE_ALIGNs. */
6525 layout_type (type);
6527 in.hash = hashcode;
6528 in.type = type;
6530 h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
6531 hashcode);
6532 if (h)
6533 return h->type;
6534 return NULL_TREE;
6537 /* Add an entry to the type-hash-table
6538 for a type TYPE whose hash code is HASHCODE. */
6540 static void
6541 type_hash_add (hashval_t hashcode, tree type)
6543 struct type_hash *h;
6544 void **loc;
6546 h = ggc_alloc_type_hash ();
6547 h->hash = hashcode;
6548 h->type = type;
6549 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
6550 *loc = (void *)h;
6553 /* Given TYPE, and HASHCODE its hash code, return the canonical
6554 object for an identical type if one already exists.
6555 Otherwise, return TYPE, and record it as the canonical object.
6557 To use this function, first create a type of the sort you want.
6558 Then compute its hash code from the fields of the type that
6559 make it different from other similar types.
6560 Then call this function and use the value. */
6562 tree
6563 type_hash_canon (unsigned int hashcode, tree type)
6565 tree t1;
6567 /* The hash table only contains main variants, so ensure that's what we're
6568 being passed. */
6569 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6571 /* See if the type is in the hash table already. If so, return it.
6572 Otherwise, add the type. */
6573 t1 = type_hash_lookup (hashcode, type);
6574 if (t1 != 0)
6576 if (GATHER_STATISTICS)
6578 tree_code_counts[(int) TREE_CODE (type)]--;
6579 tree_node_counts[(int) t_kind]--;
6580 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
6582 return t1;
6584 else
6586 type_hash_add (hashcode, type);
6587 return type;
6591 /* See if the data pointed to by the type hash table is marked. We consider
6592 it marked if the type is marked or if a debug type number or symbol
6593 table entry has been made for the type. */
6595 static int
6596 type_hash_marked_p (const void *p)
6598 const_tree const type = ((const struct type_hash *) p)->type;
6600 return ggc_marked_p (type);
6603 static void
6604 print_type_hash_statistics (void)
6606 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6607 (long) htab_size (type_hash_table),
6608 (long) htab_elements (type_hash_table),
6609 htab_collisions (type_hash_table));
6612 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
6613 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
6614 by adding the hash codes of the individual attributes. */
6616 static unsigned int
6617 attribute_hash_list (const_tree list, hashval_t hashcode)
6619 const_tree tail;
6621 for (tail = list; tail; tail = TREE_CHAIN (tail))
6622 /* ??? Do we want to add in TREE_VALUE too? */
6623 hashcode = iterative_hash_object
6624 (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)), hashcode);
6625 return hashcode;
6628 /* Given two lists of attributes, return true if list l2 is
6629 equivalent to l1. */
6632 attribute_list_equal (const_tree l1, const_tree l2)
6634 if (l1 == l2)
6635 return 1;
6637 return attribute_list_contained (l1, l2)
6638 && attribute_list_contained (l2, l1);
6641 /* Given two lists of attributes, return true if list L2 is
6642 completely contained within L1. */
6643 /* ??? This would be faster if attribute names were stored in a canonicalized
6644 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
6645 must be used to show these elements are equivalent (which they are). */
6646 /* ??? It's not clear that attributes with arguments will always be handled
6647 correctly. */
6650 attribute_list_contained (const_tree l1, const_tree l2)
6652 const_tree t1, t2;
6654 /* First check the obvious, maybe the lists are identical. */
6655 if (l1 == l2)
6656 return 1;
6658 /* Maybe the lists are similar. */
6659 for (t1 = l1, t2 = l2;
6660 t1 != 0 && t2 != 0
6661 && get_attribute_name (t1) == get_attribute_name (t2)
6662 && TREE_VALUE (t1) == TREE_VALUE (t2);
6663 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6666 /* Maybe the lists are equal. */
6667 if (t1 == 0 && t2 == 0)
6668 return 1;
6670 for (; t2 != 0; t2 = TREE_CHAIN (t2))
6672 const_tree attr;
6673 /* This CONST_CAST is okay because lookup_attribute does not
6674 modify its argument and the return value is assigned to a
6675 const_tree. */
6676 for (attr = lookup_ident_attribute (get_attribute_name (t2), CONST_CAST_TREE(l1));
6677 attr != NULL_TREE && !attribute_value_equal (t2, attr);
6678 attr = lookup_ident_attribute (get_attribute_name (t2), TREE_CHAIN (attr)))
6681 if (attr == NULL_TREE)
6682 return 0;
6685 return 1;
6688 /* Given two lists of types
6689 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6690 return 1 if the lists contain the same types in the same order.
6691 Also, the TREE_PURPOSEs must match. */
6694 type_list_equal (const_tree l1, const_tree l2)
6696 const_tree t1, t2;
6698 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6699 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6700 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6701 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6702 && (TREE_TYPE (TREE_PURPOSE (t1))
6703 == TREE_TYPE (TREE_PURPOSE (t2))))))
6704 return 0;
6706 return t1 == t2;
6709 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6710 given by TYPE. If the argument list accepts variable arguments,
6711 then this function counts only the ordinary arguments. */
6714 type_num_arguments (const_tree type)
6716 int i = 0;
6717 tree t;
6719 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6720 /* If the function does not take a variable number of arguments,
6721 the last element in the list will have type `void'. */
6722 if (VOID_TYPE_P (TREE_VALUE (t)))
6723 break;
6724 else
6725 ++i;
6727 return i;
6730 /* Nonzero if integer constants T1 and T2
6731 represent the same constant value. */
6734 tree_int_cst_equal (const_tree t1, const_tree t2)
6736 if (t1 == t2)
6737 return 1;
6739 if (t1 == 0 || t2 == 0)
6740 return 0;
6742 if (TREE_CODE (t1) == INTEGER_CST
6743 && TREE_CODE (t2) == INTEGER_CST
6744 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6745 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
6746 return 1;
6748 return 0;
6751 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
6752 The precise way of comparison depends on their data type. */
6755 tree_int_cst_lt (const_tree t1, const_tree t2)
6757 if (t1 == t2)
6758 return 0;
6760 if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
6762 int t1_sgn = tree_int_cst_sgn (t1);
6763 int t2_sgn = tree_int_cst_sgn (t2);
6765 if (t1_sgn < t2_sgn)
6766 return 1;
6767 else if (t1_sgn > t2_sgn)
6768 return 0;
6769 /* Otherwise, both are non-negative, so we compare them as
6770 unsigned just in case one of them would overflow a signed
6771 type. */
6773 else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
6774 return INT_CST_LT (t1, t2);
6776 return INT_CST_LT_UNSIGNED (t1, t2);
6779 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
6782 tree_int_cst_compare (const_tree t1, const_tree t2)
6784 if (tree_int_cst_lt (t1, t2))
6785 return -1;
6786 else if (tree_int_cst_lt (t2, t1))
6787 return 1;
6788 else
6789 return 0;
6792 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
6793 the host. If POS is zero, the value can be represented in a single
6794 HOST_WIDE_INT. If POS is nonzero, the value must be non-negative and can
6795 be represented in a single unsigned HOST_WIDE_INT. */
6798 host_integerp (const_tree t, int pos)
6800 if (t == NULL_TREE)
6801 return 0;
6803 return (TREE_CODE (t) == INTEGER_CST
6804 && ((TREE_INT_CST_HIGH (t) == 0
6805 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
6806 || (! pos && TREE_INT_CST_HIGH (t) == -1
6807 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
6808 && !TYPE_UNSIGNED (TREE_TYPE (t)))
6809 || (pos && TREE_INT_CST_HIGH (t) == 0)));
6812 /* Return the HOST_WIDE_INT least significant bits of T if it is an
6813 INTEGER_CST and there is no overflow. POS is nonzero if the result must
6814 be non-negative. We must be able to satisfy the above conditions. */
6816 HOST_WIDE_INT
6817 tree_low_cst (const_tree t, int pos)
6819 gcc_assert (host_integerp (t, pos));
6820 return TREE_INT_CST_LOW (t);
6823 /* Return the HOST_WIDE_INT least significant bits of T, a sizetype
6824 kind INTEGER_CST. This makes sure to properly sign-extend the
6825 constant. */
6827 HOST_WIDE_INT
6828 size_low_cst (const_tree t)
6830 double_int d = tree_to_double_int (t);
6831 return d.sext (TYPE_PRECISION (TREE_TYPE (t))).low;
6834 /* Return the most significant (sign) bit of T. */
6837 tree_int_cst_sign_bit (const_tree t)
6839 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
6840 unsigned HOST_WIDE_INT w;
6842 if (bitno < HOST_BITS_PER_WIDE_INT)
6843 w = TREE_INT_CST_LOW (t);
6844 else
6846 w = TREE_INT_CST_HIGH (t);
6847 bitno -= HOST_BITS_PER_WIDE_INT;
6850 return (w >> bitno) & 1;
6853 /* Return an indication of the sign of the integer constant T.
6854 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
6855 Note that -1 will never be returned if T's type is unsigned. */
6858 tree_int_cst_sgn (const_tree t)
6860 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
6861 return 0;
6862 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
6863 return 1;
6864 else if (TREE_INT_CST_HIGH (t) < 0)
6865 return -1;
6866 else
6867 return 1;
6870 /* Return the minimum number of bits needed to represent VALUE in a
6871 signed or unsigned type, UNSIGNEDP says which. */
6873 unsigned int
6874 tree_int_cst_min_precision (tree value, bool unsignedp)
6876 /* If the value is negative, compute its negative minus 1. The latter
6877 adjustment is because the absolute value of the largest negative value
6878 is one larger than the largest positive value. This is equivalent to
6879 a bit-wise negation, so use that operation instead. */
6881 if (tree_int_cst_sgn (value) < 0)
6882 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
6884 /* Return the number of bits needed, taking into account the fact
6885 that we need one more bit for a signed than unsigned type.
6886 If value is 0 or -1, the minimum precision is 1 no matter
6887 whether unsignedp is true or false. */
6889 if (integer_zerop (value))
6890 return 1;
6891 else
6892 return tree_floor_log2 (value) + 1 + !unsignedp;
6895 /* Compare two constructor-element-type constants. Return 1 if the lists
6896 are known to be equal; otherwise return 0. */
6899 simple_cst_list_equal (const_tree l1, const_tree l2)
6901 while (l1 != NULL_TREE && l2 != NULL_TREE)
6903 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
6904 return 0;
6906 l1 = TREE_CHAIN (l1);
6907 l2 = TREE_CHAIN (l2);
6910 return l1 == l2;
6913 /* Return truthvalue of whether T1 is the same tree structure as T2.
6914 Return 1 if they are the same.
6915 Return 0 if they are understandably different.
6916 Return -1 if either contains tree structure not understood by
6917 this function. */
6920 simple_cst_equal (const_tree t1, const_tree t2)
6922 enum tree_code code1, code2;
6923 int cmp;
6924 int i;
6926 if (t1 == t2)
6927 return 1;
6928 if (t1 == 0 || t2 == 0)
6929 return 0;
6931 code1 = TREE_CODE (t1);
6932 code2 = TREE_CODE (t2);
6934 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
6936 if (CONVERT_EXPR_CODE_P (code2)
6937 || code2 == NON_LVALUE_EXPR)
6938 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6939 else
6940 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
6943 else if (CONVERT_EXPR_CODE_P (code2)
6944 || code2 == NON_LVALUE_EXPR)
6945 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
6947 if (code1 != code2)
6948 return 0;
6950 switch (code1)
6952 case INTEGER_CST:
6953 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6954 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
6956 case REAL_CST:
6957 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
6959 case FIXED_CST:
6960 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
6962 case STRING_CST:
6963 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
6964 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
6965 TREE_STRING_LENGTH (t1)));
6967 case CONSTRUCTOR:
6969 unsigned HOST_WIDE_INT idx;
6970 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
6971 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
6973 if (vec_safe_length (v1) != vec_safe_length (v2))
6974 return false;
6976 for (idx = 0; idx < vec_safe_length (v1); ++idx)
6977 /* ??? Should we handle also fields here? */
6978 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
6979 return false;
6980 return true;
6983 case SAVE_EXPR:
6984 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6986 case CALL_EXPR:
6987 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
6988 if (cmp <= 0)
6989 return cmp;
6990 if (call_expr_nargs (t1) != call_expr_nargs (t2))
6991 return 0;
6993 const_tree arg1, arg2;
6994 const_call_expr_arg_iterator iter1, iter2;
6995 for (arg1 = first_const_call_expr_arg (t1, &iter1),
6996 arg2 = first_const_call_expr_arg (t2, &iter2);
6997 arg1 && arg2;
6998 arg1 = next_const_call_expr_arg (&iter1),
6999 arg2 = next_const_call_expr_arg (&iter2))
7001 cmp = simple_cst_equal (arg1, arg2);
7002 if (cmp <= 0)
7003 return cmp;
7005 return arg1 == arg2;
7008 case TARGET_EXPR:
7009 /* Special case: if either target is an unallocated VAR_DECL,
7010 it means that it's going to be unified with whatever the
7011 TARGET_EXPR is really supposed to initialize, so treat it
7012 as being equivalent to anything. */
7013 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
7014 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
7015 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
7016 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
7017 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
7018 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
7019 cmp = 1;
7020 else
7021 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7023 if (cmp <= 0)
7024 return cmp;
7026 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
7028 case WITH_CLEANUP_EXPR:
7029 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7030 if (cmp <= 0)
7031 return cmp;
7033 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
7035 case COMPONENT_REF:
7036 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
7037 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7039 return 0;
7041 case VAR_DECL:
7042 case PARM_DECL:
7043 case CONST_DECL:
7044 case FUNCTION_DECL:
7045 return 0;
7047 default:
7048 break;
7051 /* This general rule works for most tree codes. All exceptions should be
7052 handled above. If this is a language-specific tree code, we can't
7053 trust what might be in the operand, so say we don't know
7054 the situation. */
7055 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
7056 return -1;
7058 switch (TREE_CODE_CLASS (code1))
7060 case tcc_unary:
7061 case tcc_binary:
7062 case tcc_comparison:
7063 case tcc_expression:
7064 case tcc_reference:
7065 case tcc_statement:
7066 cmp = 1;
7067 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
7069 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
7070 if (cmp <= 0)
7071 return cmp;
7074 return cmp;
7076 default:
7077 return -1;
7081 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
7082 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
7083 than U, respectively. */
7086 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
7088 if (tree_int_cst_sgn (t) < 0)
7089 return -1;
7090 else if (TREE_INT_CST_HIGH (t) != 0)
7091 return 1;
7092 else if (TREE_INT_CST_LOW (t) == u)
7093 return 0;
7094 else if (TREE_INT_CST_LOW (t) < u)
7095 return -1;
7096 else
7097 return 1;
7100 /* Return true if SIZE represents a constant size that is in bounds of
7101 what the middle-end and the backend accepts (covering not more than
7102 half of the address-space). */
7104 bool
7105 valid_constant_size_p (const_tree size)
7107 if (! host_integerp (size, 1)
7108 || TREE_OVERFLOW (size)
7109 || tree_int_cst_sign_bit (size) != 0)
7110 return false;
7111 return true;
7114 /* Return the precision of the type, or for a complex or vector type the
7115 precision of the type of its elements. */
7117 unsigned int
7118 element_precision (const_tree type)
7120 enum tree_code code = TREE_CODE (type);
7121 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
7122 type = TREE_TYPE (type);
7124 return TYPE_PRECISION (type);
7127 /* Return true if CODE represents an associative tree code. Otherwise
7128 return false. */
7129 bool
7130 associative_tree_code (enum tree_code code)
7132 switch (code)
7134 case BIT_IOR_EXPR:
7135 case BIT_AND_EXPR:
7136 case BIT_XOR_EXPR:
7137 case PLUS_EXPR:
7138 case MULT_EXPR:
7139 case MIN_EXPR:
7140 case MAX_EXPR:
7141 return true;
7143 default:
7144 break;
7146 return false;
7149 /* Return true if CODE represents a commutative tree code. Otherwise
7150 return false. */
7151 bool
7152 commutative_tree_code (enum tree_code code)
7154 switch (code)
7156 case PLUS_EXPR:
7157 case MULT_EXPR:
7158 case MULT_HIGHPART_EXPR:
7159 case MIN_EXPR:
7160 case MAX_EXPR:
7161 case BIT_IOR_EXPR:
7162 case BIT_XOR_EXPR:
7163 case BIT_AND_EXPR:
7164 case NE_EXPR:
7165 case EQ_EXPR:
7166 case UNORDERED_EXPR:
7167 case ORDERED_EXPR:
7168 case UNEQ_EXPR:
7169 case LTGT_EXPR:
7170 case TRUTH_AND_EXPR:
7171 case TRUTH_XOR_EXPR:
7172 case TRUTH_OR_EXPR:
7173 case WIDEN_MULT_EXPR:
7174 case VEC_WIDEN_MULT_HI_EXPR:
7175 case VEC_WIDEN_MULT_LO_EXPR:
7176 case VEC_WIDEN_MULT_EVEN_EXPR:
7177 case VEC_WIDEN_MULT_ODD_EXPR:
7178 return true;
7180 default:
7181 break;
7183 return false;
7186 /* Return true if CODE represents a ternary tree code for which the
7187 first two operands are commutative. Otherwise return false. */
7188 bool
7189 commutative_ternary_tree_code (enum tree_code code)
7191 switch (code)
7193 case WIDEN_MULT_PLUS_EXPR:
7194 case WIDEN_MULT_MINUS_EXPR:
7195 return true;
7197 default:
7198 break;
7200 return false;
7203 /* Generate a hash value for an expression. This can be used iteratively
7204 by passing a previous result as the VAL argument.
7206 This function is intended to produce the same hash for expressions which
7207 would compare equal using operand_equal_p. */
7209 hashval_t
7210 iterative_hash_expr (const_tree t, hashval_t val)
7212 int i;
7213 enum tree_code code;
7214 char tclass;
7216 if (t == NULL_TREE)
7217 return iterative_hash_hashval_t (0, val);
7219 code = TREE_CODE (t);
7221 switch (code)
7223 /* Alas, constants aren't shared, so we can't rely on pointer
7224 identity. */
7225 case INTEGER_CST:
7226 val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
7227 return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
7228 case REAL_CST:
7230 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7232 return iterative_hash_hashval_t (val2, val);
7234 case FIXED_CST:
7236 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7238 return iterative_hash_hashval_t (val2, val);
7240 case STRING_CST:
7241 return iterative_hash (TREE_STRING_POINTER (t),
7242 TREE_STRING_LENGTH (t), val);
7243 case COMPLEX_CST:
7244 val = iterative_hash_expr (TREE_REALPART (t), val);
7245 return iterative_hash_expr (TREE_IMAGPART (t), val);
7246 case VECTOR_CST:
7248 unsigned i;
7249 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7250 val = iterative_hash_expr (VECTOR_CST_ELT (t, i), val);
7251 return val;
7253 case SSA_NAME:
7254 /* We can just compare by pointer. */
7255 return iterative_hash_host_wide_int (SSA_NAME_VERSION (t), val);
7256 case PLACEHOLDER_EXPR:
7257 /* The node itself doesn't matter. */
7258 return val;
7259 case TREE_LIST:
7260 /* A list of expressions, for a CALL_EXPR or as the elements of a
7261 VECTOR_CST. */
7262 for (; t; t = TREE_CHAIN (t))
7263 val = iterative_hash_expr (TREE_VALUE (t), val);
7264 return val;
7265 case CONSTRUCTOR:
7267 unsigned HOST_WIDE_INT idx;
7268 tree field, value;
7269 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7271 val = iterative_hash_expr (field, val);
7272 val = iterative_hash_expr (value, val);
7274 return val;
7276 case MEM_REF:
7278 /* The type of the second operand is relevant, except for
7279 its top-level qualifiers. */
7280 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (t, 1)));
7282 val = iterative_hash_object (TYPE_HASH (type), val);
7284 /* We could use the standard hash computation from this point
7285 on. */
7286 val = iterative_hash_object (code, val);
7287 val = iterative_hash_expr (TREE_OPERAND (t, 1), val);
7288 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7289 return val;
7291 case FUNCTION_DECL:
7292 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7293 Otherwise nodes that compare equal according to operand_equal_p might
7294 get different hash codes. However, don't do this for machine specific
7295 or front end builtins, since the function code is overloaded in those
7296 cases. */
7297 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7298 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7300 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7301 code = TREE_CODE (t);
7303 /* FALL THROUGH */
7304 default:
7305 tclass = TREE_CODE_CLASS (code);
7307 if (tclass == tcc_declaration)
7309 /* DECL's have a unique ID */
7310 val = iterative_hash_host_wide_int (DECL_UID (t), val);
7312 else
7314 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7316 val = iterative_hash_object (code, val);
7318 /* Don't hash the type, that can lead to having nodes which
7319 compare equal according to operand_equal_p, but which
7320 have different hash codes. */
7321 if (CONVERT_EXPR_CODE_P (code)
7322 || code == NON_LVALUE_EXPR)
7324 /* Make sure to include signness in the hash computation. */
7325 val += TYPE_UNSIGNED (TREE_TYPE (t));
7326 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7329 else if (commutative_tree_code (code))
7331 /* It's a commutative expression. We want to hash it the same
7332 however it appears. We do this by first hashing both operands
7333 and then rehashing based on the order of their independent
7334 hashes. */
7335 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
7336 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
7337 hashval_t t;
7339 if (one > two)
7340 t = one, one = two, two = t;
7342 val = iterative_hash_hashval_t (one, val);
7343 val = iterative_hash_hashval_t (two, val);
7345 else
7346 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7347 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
7349 return val;
7353 /* Generate a hash value for a pair of expressions. This can be used
7354 iteratively by passing a previous result as the VAL argument.
7356 The same hash value is always returned for a given pair of expressions,
7357 regardless of the order in which they are presented. This is useful in
7358 hashing the operands of commutative functions. */
7360 hashval_t
7361 iterative_hash_exprs_commutative (const_tree t1,
7362 const_tree t2, hashval_t val)
7364 hashval_t one = iterative_hash_expr (t1, 0);
7365 hashval_t two = iterative_hash_expr (t2, 0);
7366 hashval_t t;
7368 if (one > two)
7369 t = one, one = two, two = t;
7370 val = iterative_hash_hashval_t (one, val);
7371 val = iterative_hash_hashval_t (two, val);
7373 return val;
7376 /* Constructors for pointer, array and function types.
7377 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7378 constructed by language-dependent code, not here.) */
7380 /* Construct, lay out and return the type of pointers to TO_TYPE with
7381 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7382 reference all of memory. If such a type has already been
7383 constructed, reuse it. */
7385 tree
7386 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
7387 bool can_alias_all)
7389 tree t;
7391 if (to_type == error_mark_node)
7392 return error_mark_node;
7394 /* If the pointed-to type has the may_alias attribute set, force
7395 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7396 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7397 can_alias_all = true;
7399 /* In some cases, languages will have things that aren't a POINTER_TYPE
7400 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7401 In that case, return that type without regard to the rest of our
7402 operands.
7404 ??? This is a kludge, but consistent with the way this function has
7405 always operated and there doesn't seem to be a good way to avoid this
7406 at the moment. */
7407 if (TYPE_POINTER_TO (to_type) != 0
7408 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7409 return TYPE_POINTER_TO (to_type);
7411 /* First, if we already have a type for pointers to TO_TYPE and it's
7412 the proper mode, use it. */
7413 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7414 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7415 return t;
7417 t = make_node (POINTER_TYPE);
7419 TREE_TYPE (t) = to_type;
7420 SET_TYPE_MODE (t, mode);
7421 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7422 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7423 TYPE_POINTER_TO (to_type) = t;
7425 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7426 SET_TYPE_STRUCTURAL_EQUALITY (t);
7427 else if (TYPE_CANONICAL (to_type) != to_type)
7428 TYPE_CANONICAL (t)
7429 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7430 mode, can_alias_all);
7432 /* Lay out the type. This function has many callers that are concerned
7433 with expression-construction, and this simplifies them all. */
7434 layout_type (t);
7436 return t;
7439 /* By default build pointers in ptr_mode. */
7441 tree
7442 build_pointer_type (tree to_type)
7444 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7445 : TYPE_ADDR_SPACE (to_type);
7446 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7447 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7450 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7452 tree
7453 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
7454 bool can_alias_all)
7456 tree t;
7458 if (to_type == error_mark_node)
7459 return error_mark_node;
7461 /* If the pointed-to type has the may_alias attribute set, force
7462 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7463 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7464 can_alias_all = true;
7466 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7467 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7468 In that case, return that type without regard to the rest of our
7469 operands.
7471 ??? This is a kludge, but consistent with the way this function has
7472 always operated and there doesn't seem to be a good way to avoid this
7473 at the moment. */
7474 if (TYPE_REFERENCE_TO (to_type) != 0
7475 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7476 return TYPE_REFERENCE_TO (to_type);
7478 /* First, if we already have a type for pointers to TO_TYPE and it's
7479 the proper mode, use it. */
7480 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7481 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7482 return t;
7484 t = make_node (REFERENCE_TYPE);
7486 TREE_TYPE (t) = to_type;
7487 SET_TYPE_MODE (t, mode);
7488 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7489 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7490 TYPE_REFERENCE_TO (to_type) = t;
7492 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7493 SET_TYPE_STRUCTURAL_EQUALITY (t);
7494 else if (TYPE_CANONICAL (to_type) != to_type)
7495 TYPE_CANONICAL (t)
7496 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7497 mode, can_alias_all);
7499 layout_type (t);
7501 return t;
7505 /* Build the node for the type of references-to-TO_TYPE by default
7506 in ptr_mode. */
7508 tree
7509 build_reference_type (tree to_type)
7511 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7512 : TYPE_ADDR_SPACE (to_type);
7513 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7514 return build_reference_type_for_mode (to_type, pointer_mode, false);
7517 /* Build a type that is compatible with t but has no cv quals anywhere
7518 in its type, thus
7520 const char *const *const * -> char ***. */
7522 tree
7523 build_type_no_quals (tree t)
7525 switch (TREE_CODE (t))
7527 case POINTER_TYPE:
7528 return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7529 TYPE_MODE (t),
7530 TYPE_REF_CAN_ALIAS_ALL (t));
7531 case REFERENCE_TYPE:
7532 return
7533 build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7534 TYPE_MODE (t),
7535 TYPE_REF_CAN_ALIAS_ALL (t));
7536 default:
7537 return TYPE_MAIN_VARIANT (t);
7541 #define MAX_INT_CACHED_PREC \
7542 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7543 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7545 /* Builds a signed or unsigned integer type of precision PRECISION.
7546 Used for C bitfields whose precision does not match that of
7547 built-in target types. */
7548 tree
7549 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7550 int unsignedp)
7552 tree itype, ret;
7554 if (unsignedp)
7555 unsignedp = MAX_INT_CACHED_PREC + 1;
7557 if (precision <= MAX_INT_CACHED_PREC)
7559 itype = nonstandard_integer_type_cache[precision + unsignedp];
7560 if (itype)
7561 return itype;
7564 itype = make_node (INTEGER_TYPE);
7565 TYPE_PRECISION (itype) = precision;
7567 if (unsignedp)
7568 fixup_unsigned_type (itype);
7569 else
7570 fixup_signed_type (itype);
7572 ret = itype;
7573 if (host_integerp (TYPE_MAX_VALUE (itype), 1))
7574 ret = type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
7575 if (precision <= MAX_INT_CACHED_PREC)
7576 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7578 return ret;
7581 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7582 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7583 is true, reuse such a type that has already been constructed. */
7585 static tree
7586 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7588 tree itype = make_node (INTEGER_TYPE);
7589 hashval_t hashcode = 0;
7591 TREE_TYPE (itype) = type;
7593 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7594 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7596 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7597 SET_TYPE_MODE (itype, TYPE_MODE (type));
7598 TYPE_SIZE (itype) = TYPE_SIZE (type);
7599 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7600 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7601 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7603 if (!shared)
7604 return itype;
7606 if ((TYPE_MIN_VALUE (itype)
7607 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7608 || (TYPE_MAX_VALUE (itype)
7609 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7611 /* Since we cannot reliably merge this type, we need to compare it using
7612 structural equality checks. */
7613 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7614 return itype;
7617 hashcode = iterative_hash_expr (TYPE_MIN_VALUE (itype), hashcode);
7618 hashcode = iterative_hash_expr (TYPE_MAX_VALUE (itype), hashcode);
7619 hashcode = iterative_hash_hashval_t (TYPE_HASH (type), hashcode);
7620 itype = type_hash_canon (hashcode, itype);
7622 return itype;
7625 /* Wrapper around build_range_type_1 with SHARED set to true. */
7627 tree
7628 build_range_type (tree type, tree lowval, tree highval)
7630 return build_range_type_1 (type, lowval, highval, true);
7633 /* Wrapper around build_range_type_1 with SHARED set to false. */
7635 tree
7636 build_nonshared_range_type (tree type, tree lowval, tree highval)
7638 return build_range_type_1 (type, lowval, highval, false);
7641 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7642 MAXVAL should be the maximum value in the domain
7643 (one less than the length of the array).
7645 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7646 We don't enforce this limit, that is up to caller (e.g. language front end).
7647 The limit exists because the result is a signed type and we don't handle
7648 sizes that use more than one HOST_WIDE_INT. */
7650 tree
7651 build_index_type (tree maxval)
7653 return build_range_type (sizetype, size_zero_node, maxval);
7656 /* Return true if the debug information for TYPE, a subtype, should be emitted
7657 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7658 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7659 debug info and doesn't reflect the source code. */
7661 bool
7662 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7664 tree base_type = TREE_TYPE (type), low, high;
7666 /* Subrange types have a base type which is an integral type. */
7667 if (!INTEGRAL_TYPE_P (base_type))
7668 return false;
7670 /* Get the real bounds of the subtype. */
7671 if (lang_hooks.types.get_subrange_bounds)
7672 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7673 else
7675 low = TYPE_MIN_VALUE (type);
7676 high = TYPE_MAX_VALUE (type);
7679 /* If the type and its base type have the same representation and the same
7680 name, then the type is not a subrange but a copy of the base type. */
7681 if ((TREE_CODE (base_type) == INTEGER_TYPE
7682 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7683 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7684 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7685 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type)))
7687 tree type_name = TYPE_NAME (type);
7688 tree base_type_name = TYPE_NAME (base_type);
7690 if (type_name && TREE_CODE (type_name) == TYPE_DECL)
7691 type_name = DECL_NAME (type_name);
7693 if (base_type_name && TREE_CODE (base_type_name) == TYPE_DECL)
7694 base_type_name = DECL_NAME (base_type_name);
7696 if (type_name == base_type_name)
7697 return false;
7700 if (lowval)
7701 *lowval = low;
7702 if (highval)
7703 *highval = high;
7704 return true;
7707 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7708 and number of elements specified by the range of values of INDEX_TYPE.
7709 If SHARED is true, reuse such a type that has already been constructed. */
7711 static tree
7712 build_array_type_1 (tree elt_type, tree index_type, bool shared)
7714 tree t;
7716 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7718 error ("arrays of functions are not meaningful");
7719 elt_type = integer_type_node;
7722 t = make_node (ARRAY_TYPE);
7723 TREE_TYPE (t) = elt_type;
7724 TYPE_DOMAIN (t) = index_type;
7725 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7726 layout_type (t);
7728 /* If the element type is incomplete at this point we get marked for
7729 structural equality. Do not record these types in the canonical
7730 type hashtable. */
7731 if (TYPE_STRUCTURAL_EQUALITY_P (t))
7732 return t;
7734 if (shared)
7736 hashval_t hashcode = iterative_hash_object (TYPE_HASH (elt_type), 0);
7737 if (index_type)
7738 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
7739 t = type_hash_canon (hashcode, t);
7742 if (TYPE_CANONICAL (t) == t)
7744 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7745 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7746 SET_TYPE_STRUCTURAL_EQUALITY (t);
7747 else if (TYPE_CANONICAL (elt_type) != elt_type
7748 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7749 TYPE_CANONICAL (t)
7750 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7751 index_type
7752 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7753 shared);
7756 return t;
7759 /* Wrapper around build_array_type_1 with SHARED set to true. */
7761 tree
7762 build_array_type (tree elt_type, tree index_type)
7764 return build_array_type_1 (elt_type, index_type, true);
7767 /* Wrapper around build_array_type_1 with SHARED set to false. */
7769 tree
7770 build_nonshared_array_type (tree elt_type, tree index_type)
7772 return build_array_type_1 (elt_type, index_type, false);
7775 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7776 sizetype. */
7778 tree
7779 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
7781 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7784 /* Recursively examines the array elements of TYPE, until a non-array
7785 element type is found. */
7787 tree
7788 strip_array_types (tree type)
7790 while (TREE_CODE (type) == ARRAY_TYPE)
7791 type = TREE_TYPE (type);
7793 return type;
7796 /* Computes the canonical argument types from the argument type list
7797 ARGTYPES.
7799 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7800 on entry to this function, or if any of the ARGTYPES are
7801 structural.
7803 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7804 true on entry to this function, or if any of the ARGTYPES are
7805 non-canonical.
7807 Returns a canonical argument list, which may be ARGTYPES when the
7808 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7809 true) or would not differ from ARGTYPES. */
7811 static tree
7812 maybe_canonicalize_argtypes(tree argtypes,
7813 bool *any_structural_p,
7814 bool *any_noncanonical_p)
7816 tree arg;
7817 bool any_noncanonical_argtypes_p = false;
7819 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7821 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7822 /* Fail gracefully by stating that the type is structural. */
7823 *any_structural_p = true;
7824 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7825 *any_structural_p = true;
7826 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7827 || TREE_PURPOSE (arg))
7828 /* If the argument has a default argument, we consider it
7829 non-canonical even though the type itself is canonical.
7830 That way, different variants of function and method types
7831 with default arguments will all point to the variant with
7832 no defaults as their canonical type. */
7833 any_noncanonical_argtypes_p = true;
7836 if (*any_structural_p)
7837 return argtypes;
7839 if (any_noncanonical_argtypes_p)
7841 /* Build the canonical list of argument types. */
7842 tree canon_argtypes = NULL_TREE;
7843 bool is_void = false;
7845 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7847 if (arg == void_list_node)
7848 is_void = true;
7849 else
7850 canon_argtypes = tree_cons (NULL_TREE,
7851 TYPE_CANONICAL (TREE_VALUE (arg)),
7852 canon_argtypes);
7855 canon_argtypes = nreverse (canon_argtypes);
7856 if (is_void)
7857 canon_argtypes = chainon (canon_argtypes, void_list_node);
7859 /* There is a non-canonical type. */
7860 *any_noncanonical_p = true;
7861 return canon_argtypes;
7864 /* The canonical argument types are the same as ARGTYPES. */
7865 return argtypes;
7868 /* Construct, lay out and return
7869 the type of functions returning type VALUE_TYPE
7870 given arguments of types ARG_TYPES.
7871 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7872 are data type nodes for the arguments of the function.
7873 If such a type has already been constructed, reuse it. */
7875 tree
7876 build_function_type (tree value_type, tree arg_types)
7878 tree t;
7879 hashval_t hashcode = 0;
7880 bool any_structural_p, any_noncanonical_p;
7881 tree canon_argtypes;
7883 if (TREE_CODE (value_type) == FUNCTION_TYPE)
7885 error ("function return type cannot be function");
7886 value_type = integer_type_node;
7889 /* Make a node of the sort we want. */
7890 t = make_node (FUNCTION_TYPE);
7891 TREE_TYPE (t) = value_type;
7892 TYPE_ARG_TYPES (t) = arg_types;
7894 /* If we already have such a type, use the old one. */
7895 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
7896 hashcode = type_hash_list (arg_types, hashcode);
7897 t = type_hash_canon (hashcode, t);
7899 /* Set up the canonical type. */
7900 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7901 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7902 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
7903 &any_structural_p,
7904 &any_noncanonical_p);
7905 if (any_structural_p)
7906 SET_TYPE_STRUCTURAL_EQUALITY (t);
7907 else if (any_noncanonical_p)
7908 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
7909 canon_argtypes);
7911 if (!COMPLETE_TYPE_P (t))
7912 layout_type (t);
7913 return t;
7916 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP and the
7917 return value if SKIP_RETURN is true. */
7919 static tree
7920 build_function_type_skip_args (tree orig_type, bitmap args_to_skip,
7921 bool skip_return)
7923 tree new_type = NULL;
7924 tree args, new_args = NULL, t;
7925 tree new_reversed;
7926 int i = 0;
7928 for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
7929 args = TREE_CHAIN (args), i++)
7930 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
7931 new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
7933 new_reversed = nreverse (new_args);
7934 if (args)
7936 if (new_reversed)
7937 TREE_CHAIN (new_args) = void_list_node;
7938 else
7939 new_reversed = void_list_node;
7942 /* Use copy_node to preserve as much as possible from original type
7943 (debug info, attribute lists etc.)
7944 Exception is METHOD_TYPEs must have THIS argument.
7945 When we are asked to remove it, we need to build new FUNCTION_TYPE
7946 instead. */
7947 if (TREE_CODE (orig_type) != METHOD_TYPE
7948 || !args_to_skip
7949 || !bitmap_bit_p (args_to_skip, 0))
7951 new_type = build_distinct_type_copy (orig_type);
7952 TYPE_ARG_TYPES (new_type) = new_reversed;
7954 else
7956 new_type
7957 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
7958 new_reversed));
7959 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
7962 if (skip_return)
7963 TREE_TYPE (new_type) = void_type_node;
7965 /* This is a new type, not a copy of an old type. Need to reassociate
7966 variants. We can handle everything except the main variant lazily. */
7967 t = TYPE_MAIN_VARIANT (orig_type);
7968 if (t != orig_type)
7970 t = build_function_type_skip_args (t, args_to_skip, skip_return);
7971 TYPE_MAIN_VARIANT (new_type) = t;
7972 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
7973 TYPE_NEXT_VARIANT (t) = new_type;
7975 else
7977 TYPE_MAIN_VARIANT (new_type) = new_type;
7978 TYPE_NEXT_VARIANT (new_type) = NULL;
7981 return new_type;
7984 /* Build variant of function decl ORIG_DECL skipping ARGS_TO_SKIP and the
7985 return value if SKIP_RETURN is true.
7987 Arguments from DECL_ARGUMENTS list can't be removed now, since they are
7988 linked by TREE_CHAIN directly. The caller is responsible for eliminating
7989 them when they are being duplicated (i.e. copy_arguments_for_versioning). */
7991 tree
7992 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip,
7993 bool skip_return)
7995 tree new_decl = copy_node (orig_decl);
7996 tree new_type;
7998 new_type = TREE_TYPE (orig_decl);
7999 if (prototype_p (new_type)
8000 || (skip_return && !VOID_TYPE_P (TREE_TYPE (new_type))))
8001 new_type
8002 = build_function_type_skip_args (new_type, args_to_skip, skip_return);
8003 TREE_TYPE (new_decl) = new_type;
8005 /* For declarations setting DECL_VINDEX (i.e. methods)
8006 we expect first argument to be THIS pointer. */
8007 if (args_to_skip && bitmap_bit_p (args_to_skip, 0))
8008 DECL_VINDEX (new_decl) = NULL_TREE;
8010 /* When signature changes, we need to clear builtin info. */
8011 if (DECL_BUILT_IN (new_decl)
8012 && args_to_skip
8013 && !bitmap_empty_p (args_to_skip))
8015 DECL_BUILT_IN_CLASS (new_decl) = NOT_BUILT_IN;
8016 DECL_FUNCTION_CODE (new_decl) = (enum built_in_function) 0;
8018 return new_decl;
8021 /* Build a function type. The RETURN_TYPE is the type returned by the
8022 function. If VAARGS is set, no void_type_node is appended to the
8023 the list. ARGP must be always be terminated be a NULL_TREE. */
8025 static tree
8026 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
8028 tree t, args, last;
8030 t = va_arg (argp, tree);
8031 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
8032 args = tree_cons (NULL_TREE, t, args);
8034 if (vaargs)
8036 last = args;
8037 if (args != NULL_TREE)
8038 args = nreverse (args);
8039 gcc_assert (last != void_list_node);
8041 else if (args == NULL_TREE)
8042 args = void_list_node;
8043 else
8045 last = args;
8046 args = nreverse (args);
8047 TREE_CHAIN (last) = void_list_node;
8049 args = build_function_type (return_type, args);
8051 return args;
8054 /* Build a function type. The RETURN_TYPE is the type returned by the
8055 function. If additional arguments are provided, they are
8056 additional argument types. The list of argument types must always
8057 be terminated by NULL_TREE. */
8059 tree
8060 build_function_type_list (tree return_type, ...)
8062 tree args;
8063 va_list p;
8065 va_start (p, return_type);
8066 args = build_function_type_list_1 (false, return_type, p);
8067 va_end (p);
8068 return args;
8071 /* Build a variable argument function type. The RETURN_TYPE is the
8072 type returned by the function. If additional arguments are provided,
8073 they are additional argument types. The list of argument types must
8074 always be terminated by NULL_TREE. */
8076 tree
8077 build_varargs_function_type_list (tree return_type, ...)
8079 tree args;
8080 va_list p;
8082 va_start (p, return_type);
8083 args = build_function_type_list_1 (true, return_type, p);
8084 va_end (p);
8086 return args;
8089 /* Build a function type. RETURN_TYPE is the type returned by the
8090 function; VAARGS indicates whether the function takes varargs. The
8091 function takes N named arguments, the types of which are provided in
8092 ARG_TYPES. */
8094 static tree
8095 build_function_type_array_1 (bool vaargs, tree return_type, int n,
8096 tree *arg_types)
8098 int i;
8099 tree t = vaargs ? NULL_TREE : void_list_node;
8101 for (i = n - 1; i >= 0; i--)
8102 t = tree_cons (NULL_TREE, arg_types[i], t);
8104 return build_function_type (return_type, t);
8107 /* Build a function type. RETURN_TYPE is the type returned by the
8108 function. The function takes N named arguments, the types of which
8109 are provided in ARG_TYPES. */
8111 tree
8112 build_function_type_array (tree return_type, int n, tree *arg_types)
8114 return build_function_type_array_1 (false, return_type, n, arg_types);
8117 /* Build a variable argument function type. RETURN_TYPE is the type
8118 returned by the function. The function takes N named arguments, the
8119 types of which are provided in ARG_TYPES. */
8121 tree
8122 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8124 return build_function_type_array_1 (true, return_type, n, arg_types);
8127 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8128 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8129 for the method. An implicit additional parameter (of type
8130 pointer-to-BASETYPE) is added to the ARGTYPES. */
8132 tree
8133 build_method_type_directly (tree basetype,
8134 tree rettype,
8135 tree argtypes)
8137 tree t;
8138 tree ptype;
8139 int hashcode = 0;
8140 bool any_structural_p, any_noncanonical_p;
8141 tree canon_argtypes;
8143 /* Make a node of the sort we want. */
8144 t = make_node (METHOD_TYPE);
8146 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8147 TREE_TYPE (t) = rettype;
8148 ptype = build_pointer_type (basetype);
8150 /* The actual arglist for this function includes a "hidden" argument
8151 which is "this". Put it into the list of argument types. */
8152 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8153 TYPE_ARG_TYPES (t) = argtypes;
8155 /* If we already have such a type, use the old one. */
8156 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8157 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
8158 hashcode = type_hash_list (argtypes, hashcode);
8159 t = type_hash_canon (hashcode, t);
8161 /* Set up the canonical type. */
8162 any_structural_p
8163 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8164 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8165 any_noncanonical_p
8166 = (TYPE_CANONICAL (basetype) != basetype
8167 || TYPE_CANONICAL (rettype) != rettype);
8168 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8169 &any_structural_p,
8170 &any_noncanonical_p);
8171 if (any_structural_p)
8172 SET_TYPE_STRUCTURAL_EQUALITY (t);
8173 else if (any_noncanonical_p)
8174 TYPE_CANONICAL (t)
8175 = build_method_type_directly (TYPE_CANONICAL (basetype),
8176 TYPE_CANONICAL (rettype),
8177 canon_argtypes);
8178 if (!COMPLETE_TYPE_P (t))
8179 layout_type (t);
8181 return t;
8184 /* Construct, lay out and return the type of methods belonging to class
8185 BASETYPE and whose arguments and values are described by TYPE.
8186 If that type exists already, reuse it.
8187 TYPE must be a FUNCTION_TYPE node. */
8189 tree
8190 build_method_type (tree basetype, tree type)
8192 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8194 return build_method_type_directly (basetype,
8195 TREE_TYPE (type),
8196 TYPE_ARG_TYPES (type));
8199 /* Construct, lay out and return the type of offsets to a value
8200 of type TYPE, within an object of type BASETYPE.
8201 If a suitable offset type exists already, reuse it. */
8203 tree
8204 build_offset_type (tree basetype, tree type)
8206 tree t;
8207 hashval_t hashcode = 0;
8209 /* Make a node of the sort we want. */
8210 t = make_node (OFFSET_TYPE);
8212 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8213 TREE_TYPE (t) = type;
8215 /* If we already have such a type, use the old one. */
8216 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8217 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
8218 t = type_hash_canon (hashcode, t);
8220 if (!COMPLETE_TYPE_P (t))
8221 layout_type (t);
8223 if (TYPE_CANONICAL (t) == t)
8225 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8226 || TYPE_STRUCTURAL_EQUALITY_P (type))
8227 SET_TYPE_STRUCTURAL_EQUALITY (t);
8228 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8229 || TYPE_CANONICAL (type) != type)
8230 TYPE_CANONICAL (t)
8231 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8232 TYPE_CANONICAL (type));
8235 return t;
8238 /* Create a complex type whose components are COMPONENT_TYPE. */
8240 tree
8241 build_complex_type (tree component_type)
8243 tree t;
8244 hashval_t hashcode;
8246 gcc_assert (INTEGRAL_TYPE_P (component_type)
8247 || SCALAR_FLOAT_TYPE_P (component_type)
8248 || FIXED_POINT_TYPE_P (component_type));
8250 /* Make a node of the sort we want. */
8251 t = make_node (COMPLEX_TYPE);
8253 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8255 /* If we already have such a type, use the old one. */
8256 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
8257 t = type_hash_canon (hashcode, t);
8259 if (!COMPLETE_TYPE_P (t))
8260 layout_type (t);
8262 if (TYPE_CANONICAL (t) == t)
8264 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8265 SET_TYPE_STRUCTURAL_EQUALITY (t);
8266 else if (TYPE_CANONICAL (component_type) != component_type)
8267 TYPE_CANONICAL (t)
8268 = build_complex_type (TYPE_CANONICAL (component_type));
8271 /* We need to create a name, since complex is a fundamental type. */
8272 if (! TYPE_NAME (t))
8274 const char *name;
8275 if (component_type == char_type_node)
8276 name = "complex char";
8277 else if (component_type == signed_char_type_node)
8278 name = "complex signed char";
8279 else if (component_type == unsigned_char_type_node)
8280 name = "complex unsigned char";
8281 else if (component_type == short_integer_type_node)
8282 name = "complex short int";
8283 else if (component_type == short_unsigned_type_node)
8284 name = "complex short unsigned int";
8285 else if (component_type == integer_type_node)
8286 name = "complex int";
8287 else if (component_type == unsigned_type_node)
8288 name = "complex unsigned int";
8289 else if (component_type == long_integer_type_node)
8290 name = "complex long int";
8291 else if (component_type == long_unsigned_type_node)
8292 name = "complex long unsigned int";
8293 else if (component_type == long_long_integer_type_node)
8294 name = "complex long long int";
8295 else if (component_type == long_long_unsigned_type_node)
8296 name = "complex long long unsigned int";
8297 else
8298 name = 0;
8300 if (name != 0)
8301 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8302 get_identifier (name), t);
8305 return build_qualified_type (t, TYPE_QUALS (component_type));
8308 /* If TYPE is a real or complex floating-point type and the target
8309 does not directly support arithmetic on TYPE then return the wider
8310 type to be used for arithmetic on TYPE. Otherwise, return
8311 NULL_TREE. */
8313 tree
8314 excess_precision_type (tree type)
8316 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8318 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8319 switch (TREE_CODE (type))
8321 case REAL_TYPE:
8322 switch (flt_eval_method)
8324 case 1:
8325 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8326 return double_type_node;
8327 break;
8328 case 2:
8329 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8330 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8331 return long_double_type_node;
8332 break;
8333 default:
8334 gcc_unreachable ();
8336 break;
8337 case COMPLEX_TYPE:
8338 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8339 return NULL_TREE;
8340 switch (flt_eval_method)
8342 case 1:
8343 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8344 return complex_double_type_node;
8345 break;
8346 case 2:
8347 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8348 || (TYPE_MODE (TREE_TYPE (type))
8349 == TYPE_MODE (double_type_node)))
8350 return complex_long_double_type_node;
8351 break;
8352 default:
8353 gcc_unreachable ();
8355 break;
8356 default:
8357 break;
8360 return NULL_TREE;
8363 /* Return OP, stripped of any conversions to wider types as much as is safe.
8364 Converting the value back to OP's type makes a value equivalent to OP.
8366 If FOR_TYPE is nonzero, we return a value which, if converted to
8367 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8369 OP must have integer, real or enumeral type. Pointers are not allowed!
8371 There are some cases where the obvious value we could return
8372 would regenerate to OP if converted to OP's type,
8373 but would not extend like OP to wider types.
8374 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8375 For example, if OP is (unsigned short)(signed char)-1,
8376 we avoid returning (signed char)-1 if FOR_TYPE is int,
8377 even though extending that to an unsigned short would regenerate OP,
8378 since the result of extending (signed char)-1 to (int)
8379 is different from (int) OP. */
8381 tree
8382 get_unwidened (tree op, tree for_type)
8384 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8385 tree type = TREE_TYPE (op);
8386 unsigned final_prec
8387 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8388 int uns
8389 = (for_type != 0 && for_type != type
8390 && final_prec > TYPE_PRECISION (type)
8391 && TYPE_UNSIGNED (type));
8392 tree win = op;
8394 while (CONVERT_EXPR_P (op))
8396 int bitschange;
8398 /* TYPE_PRECISION on vector types has different meaning
8399 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8400 so avoid them here. */
8401 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8402 break;
8404 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8405 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8407 /* Truncations are many-one so cannot be removed.
8408 Unless we are later going to truncate down even farther. */
8409 if (bitschange < 0
8410 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8411 break;
8413 /* See what's inside this conversion. If we decide to strip it,
8414 we will set WIN. */
8415 op = TREE_OPERAND (op, 0);
8417 /* If we have not stripped any zero-extensions (uns is 0),
8418 we can strip any kind of extension.
8419 If we have previously stripped a zero-extension,
8420 only zero-extensions can safely be stripped.
8421 Any extension can be stripped if the bits it would produce
8422 are all going to be discarded later by truncating to FOR_TYPE. */
8424 if (bitschange > 0)
8426 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8427 win = op;
8428 /* TYPE_UNSIGNED says whether this is a zero-extension.
8429 Let's avoid computing it if it does not affect WIN
8430 and if UNS will not be needed again. */
8431 if ((uns
8432 || CONVERT_EXPR_P (op))
8433 && TYPE_UNSIGNED (TREE_TYPE (op)))
8435 uns = 1;
8436 win = op;
8441 /* If we finally reach a constant see if it fits in for_type and
8442 in that case convert it. */
8443 if (for_type
8444 && TREE_CODE (win) == INTEGER_CST
8445 && TREE_TYPE (win) != for_type
8446 && int_fits_type_p (win, for_type))
8447 win = fold_convert (for_type, win);
8449 return win;
8452 /* Return OP or a simpler expression for a narrower value
8453 which can be sign-extended or zero-extended to give back OP.
8454 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8455 or 0 if the value should be sign-extended. */
8457 tree
8458 get_narrower (tree op, int *unsignedp_ptr)
8460 int uns = 0;
8461 int first = 1;
8462 tree win = op;
8463 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8465 while (TREE_CODE (op) == NOP_EXPR)
8467 int bitschange
8468 = (TYPE_PRECISION (TREE_TYPE (op))
8469 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8471 /* Truncations are many-one so cannot be removed. */
8472 if (bitschange < 0)
8473 break;
8475 /* See what's inside this conversion. If we decide to strip it,
8476 we will set WIN. */
8478 if (bitschange > 0)
8480 op = TREE_OPERAND (op, 0);
8481 /* An extension: the outermost one can be stripped,
8482 but remember whether it is zero or sign extension. */
8483 if (first)
8484 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8485 /* Otherwise, if a sign extension has been stripped,
8486 only sign extensions can now be stripped;
8487 if a zero extension has been stripped, only zero-extensions. */
8488 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8489 break;
8490 first = 0;
8492 else /* bitschange == 0 */
8494 /* A change in nominal type can always be stripped, but we must
8495 preserve the unsignedness. */
8496 if (first)
8497 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8498 first = 0;
8499 op = TREE_OPERAND (op, 0);
8500 /* Keep trying to narrow, but don't assign op to win if it
8501 would turn an integral type into something else. */
8502 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8503 continue;
8506 win = op;
8509 if (TREE_CODE (op) == COMPONENT_REF
8510 /* Since type_for_size always gives an integer type. */
8511 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8512 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8513 /* Ensure field is laid out already. */
8514 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8515 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
8517 unsigned HOST_WIDE_INT innerprec
8518 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
8519 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8520 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8521 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8523 /* We can get this structure field in a narrower type that fits it,
8524 but the resulting extension to its nominal type (a fullword type)
8525 must satisfy the same conditions as for other extensions.
8527 Do this only for fields that are aligned (not bit-fields),
8528 because when bit-field insns will be used there is no
8529 advantage in doing this. */
8531 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8532 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8533 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8534 && type != 0)
8536 if (first)
8537 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8538 win = fold_convert (type, op);
8542 *unsignedp_ptr = uns;
8543 return win;
8546 /* Returns true if integer constant C has a value that is permissible
8547 for type TYPE (an INTEGER_TYPE). */
8549 bool
8550 int_fits_type_p (const_tree c, const_tree type)
8552 tree type_low_bound, type_high_bound;
8553 bool ok_for_low_bound, ok_for_high_bound, unsc;
8554 double_int dc, dd;
8556 dc = tree_to_double_int (c);
8557 unsc = TYPE_UNSIGNED (TREE_TYPE (c));
8559 retry:
8560 type_low_bound = TYPE_MIN_VALUE (type);
8561 type_high_bound = TYPE_MAX_VALUE (type);
8563 /* If at least one bound of the type is a constant integer, we can check
8564 ourselves and maybe make a decision. If no such decision is possible, but
8565 this type is a subtype, try checking against that. Otherwise, use
8566 double_int_fits_to_tree_p, which checks against the precision.
8568 Compute the status for each possibly constant bound, and return if we see
8569 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8570 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8571 for "constant known to fit". */
8573 /* Check if c >= type_low_bound. */
8574 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8576 dd = tree_to_double_int (type_low_bound);
8577 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
8579 int c_neg = (!unsc && dc.is_negative ());
8580 int t_neg = (unsc && dd.is_negative ());
8582 if (c_neg && !t_neg)
8583 return false;
8584 if ((c_neg || !t_neg) && dc.ult (dd))
8585 return false;
8587 else if (dc.cmp (dd, unsc) < 0)
8588 return false;
8589 ok_for_low_bound = true;
8591 else
8592 ok_for_low_bound = false;
8594 /* Check if c <= type_high_bound. */
8595 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8597 dd = tree_to_double_int (type_high_bound);
8598 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
8600 int c_neg = (!unsc && dc.is_negative ());
8601 int t_neg = (unsc && dd.is_negative ());
8603 if (t_neg && !c_neg)
8604 return false;
8605 if ((t_neg || !c_neg) && dc.ugt (dd))
8606 return false;
8608 else if (dc.cmp (dd, unsc) > 0)
8609 return false;
8610 ok_for_high_bound = true;
8612 else
8613 ok_for_high_bound = false;
8615 /* If the constant fits both bounds, the result is known. */
8616 if (ok_for_low_bound && ok_for_high_bound)
8617 return true;
8619 /* Perform some generic filtering which may allow making a decision
8620 even if the bounds are not constant. First, negative integers
8621 never fit in unsigned types, */
8622 if (TYPE_UNSIGNED (type) && !unsc && dc.is_negative ())
8623 return false;
8625 /* Second, narrower types always fit in wider ones. */
8626 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8627 return true;
8629 /* Third, unsigned integers with top bit set never fit signed types. */
8630 if (! TYPE_UNSIGNED (type) && unsc)
8632 int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (c))) - 1;
8633 if (prec < HOST_BITS_PER_WIDE_INT)
8635 if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
8636 return false;
8638 else if (((((unsigned HOST_WIDE_INT) 1)
8639 << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
8640 return false;
8643 /* If we haven't been able to decide at this point, there nothing more we
8644 can check ourselves here. Look at the base type if we have one and it
8645 has the same precision. */
8646 if (TREE_CODE (type) == INTEGER_TYPE
8647 && TREE_TYPE (type) != 0
8648 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8650 type = TREE_TYPE (type);
8651 goto retry;
8654 /* Or to double_int_fits_to_tree_p, if nothing else. */
8655 return double_int_fits_to_tree_p (type, dc);
8658 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8659 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8660 represented (assuming two's-complement arithmetic) within the bit
8661 precision of the type are returned instead. */
8663 void
8664 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8666 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8667 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8668 mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
8669 TYPE_UNSIGNED (type));
8670 else
8672 if (TYPE_UNSIGNED (type))
8673 mpz_set_ui (min, 0);
8674 else
8676 double_int mn;
8677 mn = double_int::mask (TYPE_PRECISION (type) - 1);
8678 mn = (mn + double_int_one).sext (TYPE_PRECISION (type));
8679 mpz_set_double_int (min, mn, false);
8683 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8684 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8685 mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
8686 TYPE_UNSIGNED (type));
8687 else
8689 if (TYPE_UNSIGNED (type))
8690 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)),
8691 true);
8692 else
8693 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type) - 1),
8694 true);
8698 /* Return true if VAR is an automatic variable defined in function FN. */
8700 bool
8701 auto_var_in_fn_p (const_tree var, const_tree fn)
8703 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8704 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8705 || TREE_CODE (var) == PARM_DECL)
8706 && ! TREE_STATIC (var))
8707 || TREE_CODE (var) == LABEL_DECL
8708 || TREE_CODE (var) == RESULT_DECL));
8711 /* Subprogram of following function. Called by walk_tree.
8713 Return *TP if it is an automatic variable or parameter of the
8714 function passed in as DATA. */
8716 static tree
8717 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8719 tree fn = (tree) data;
8721 if (TYPE_P (*tp))
8722 *walk_subtrees = 0;
8724 else if (DECL_P (*tp)
8725 && auto_var_in_fn_p (*tp, fn))
8726 return *tp;
8728 return NULL_TREE;
8731 /* Returns true if T is, contains, or refers to a type with variable
8732 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8733 arguments, but not the return type. If FN is nonzero, only return
8734 true if a modifier of the type or position of FN is a variable or
8735 parameter inside FN.
8737 This concept is more general than that of C99 'variably modified types':
8738 in C99, a struct type is never variably modified because a VLA may not
8739 appear as a structure member. However, in GNU C code like:
8741 struct S { int i[f()]; };
8743 is valid, and other languages may define similar constructs. */
8745 bool
8746 variably_modified_type_p (tree type, tree fn)
8748 tree t;
8750 /* Test if T is either variable (if FN is zero) or an expression containing
8751 a variable in FN. If TYPE isn't gimplified, return true also if
8752 gimplify_one_sizepos would gimplify the expression into a local
8753 variable. */
8754 #define RETURN_TRUE_IF_VAR(T) \
8755 do { tree _t = (T); \
8756 if (_t != NULL_TREE \
8757 && _t != error_mark_node \
8758 && TREE_CODE (_t) != INTEGER_CST \
8759 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8760 && (!fn \
8761 || (!TYPE_SIZES_GIMPLIFIED (type) \
8762 && !is_gimple_sizepos (_t)) \
8763 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8764 return true; } while (0)
8766 if (type == error_mark_node)
8767 return false;
8769 /* If TYPE itself has variable size, it is variably modified. */
8770 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8771 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8773 switch (TREE_CODE (type))
8775 case POINTER_TYPE:
8776 case REFERENCE_TYPE:
8777 case VECTOR_TYPE:
8778 if (variably_modified_type_p (TREE_TYPE (type), fn))
8779 return true;
8780 break;
8782 case FUNCTION_TYPE:
8783 case METHOD_TYPE:
8784 /* If TYPE is a function type, it is variably modified if the
8785 return type is variably modified. */
8786 if (variably_modified_type_p (TREE_TYPE (type), fn))
8787 return true;
8788 break;
8790 case INTEGER_TYPE:
8791 case REAL_TYPE:
8792 case FIXED_POINT_TYPE:
8793 case ENUMERAL_TYPE:
8794 case BOOLEAN_TYPE:
8795 /* Scalar types are variably modified if their end points
8796 aren't constant. */
8797 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8798 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8799 break;
8801 case RECORD_TYPE:
8802 case UNION_TYPE:
8803 case QUAL_UNION_TYPE:
8804 /* We can't see if any of the fields are variably-modified by the
8805 definition we normally use, since that would produce infinite
8806 recursion via pointers. */
8807 /* This is variably modified if some field's type is. */
8808 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8809 if (TREE_CODE (t) == FIELD_DECL)
8811 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8812 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8813 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8815 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8816 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8818 break;
8820 case ARRAY_TYPE:
8821 /* Do not call ourselves to avoid infinite recursion. This is
8822 variably modified if the element type is. */
8823 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8824 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8825 break;
8827 default:
8828 break;
8831 /* The current language may have other cases to check, but in general,
8832 all other types are not variably modified. */
8833 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8835 #undef RETURN_TRUE_IF_VAR
8838 /* Given a DECL or TYPE, return the scope in which it was declared, or
8839 NULL_TREE if there is no containing scope. */
8841 tree
8842 get_containing_scope (const_tree t)
8844 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8847 /* Return the innermost context enclosing DECL that is
8848 a FUNCTION_DECL, or zero if none. */
8850 tree
8851 decl_function_context (const_tree decl)
8853 tree context;
8855 if (TREE_CODE (decl) == ERROR_MARK)
8856 return 0;
8858 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8859 where we look up the function at runtime. Such functions always take
8860 a first argument of type 'pointer to real context'.
8862 C++ should really be fixed to use DECL_CONTEXT for the real context,
8863 and use something else for the "virtual context". */
8864 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8865 context
8866 = TYPE_MAIN_VARIANT
8867 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8868 else
8869 context = DECL_CONTEXT (decl);
8871 while (context && TREE_CODE (context) != FUNCTION_DECL)
8873 if (TREE_CODE (context) == BLOCK)
8874 context = BLOCK_SUPERCONTEXT (context);
8875 else
8876 context = get_containing_scope (context);
8879 return context;
8882 /* Return the innermost context enclosing DECL that is
8883 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8884 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8886 tree
8887 decl_type_context (const_tree decl)
8889 tree context = DECL_CONTEXT (decl);
8891 while (context)
8892 switch (TREE_CODE (context))
8894 case NAMESPACE_DECL:
8895 case TRANSLATION_UNIT_DECL:
8896 return NULL_TREE;
8898 case RECORD_TYPE:
8899 case UNION_TYPE:
8900 case QUAL_UNION_TYPE:
8901 return context;
8903 case TYPE_DECL:
8904 case FUNCTION_DECL:
8905 context = DECL_CONTEXT (context);
8906 break;
8908 case BLOCK:
8909 context = BLOCK_SUPERCONTEXT (context);
8910 break;
8912 default:
8913 gcc_unreachable ();
8916 return NULL_TREE;
8919 /* CALL is a CALL_EXPR. Return the declaration for the function
8920 called, or NULL_TREE if the called function cannot be
8921 determined. */
8923 tree
8924 get_callee_fndecl (const_tree call)
8926 tree addr;
8928 if (call == error_mark_node)
8929 return error_mark_node;
8931 /* It's invalid to call this function with anything but a
8932 CALL_EXPR. */
8933 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8935 /* The first operand to the CALL is the address of the function
8936 called. */
8937 addr = CALL_EXPR_FN (call);
8939 STRIP_NOPS (addr);
8941 /* If this is a readonly function pointer, extract its initial value. */
8942 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8943 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8944 && DECL_INITIAL (addr))
8945 addr = DECL_INITIAL (addr);
8947 /* If the address is just `&f' for some function `f', then we know
8948 that `f' is being called. */
8949 if (TREE_CODE (addr) == ADDR_EXPR
8950 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8951 return TREE_OPERAND (addr, 0);
8953 /* We couldn't figure out what was being called. */
8954 return NULL_TREE;
8957 /* Print debugging information about tree nodes generated during the compile,
8958 and any language-specific information. */
8960 void
8961 dump_tree_statistics (void)
8963 if (GATHER_STATISTICS)
8965 int i;
8966 int total_nodes, total_bytes;
8967 fprintf (stderr, "Kind Nodes Bytes\n");
8968 fprintf (stderr, "---------------------------------------\n");
8969 total_nodes = total_bytes = 0;
8970 for (i = 0; i < (int) all_kinds; i++)
8972 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
8973 tree_node_counts[i], tree_node_sizes[i]);
8974 total_nodes += tree_node_counts[i];
8975 total_bytes += tree_node_sizes[i];
8977 fprintf (stderr, "---------------------------------------\n");
8978 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
8979 fprintf (stderr, "---------------------------------------\n");
8980 fprintf (stderr, "Code Nodes\n");
8981 fprintf (stderr, "----------------------------\n");
8982 for (i = 0; i < (int) MAX_TREE_CODES; i++)
8983 fprintf (stderr, "%-20s %7d\n", tree_code_name[i], tree_code_counts[i]);
8984 fprintf (stderr, "----------------------------\n");
8985 ssanames_print_statistics ();
8986 phinodes_print_statistics ();
8988 else
8989 fprintf (stderr, "(No per-node statistics)\n");
8991 print_type_hash_statistics ();
8992 print_debug_expr_statistics ();
8993 print_value_expr_statistics ();
8994 lang_hooks.print_statistics ();
8997 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8999 /* Generate a crc32 of a byte. */
9001 static unsigned
9002 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
9004 unsigned ix;
9006 for (ix = bits; ix--; value <<= 1)
9008 unsigned feedback;
9010 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
9011 chksum <<= 1;
9012 chksum ^= feedback;
9014 return chksum;
9017 /* Generate a crc32 of a 32-bit unsigned. */
9019 unsigned
9020 crc32_unsigned (unsigned chksum, unsigned value)
9022 return crc32_unsigned_bits (chksum, value, 32);
9025 /* Generate a crc32 of a byte. */
9027 unsigned
9028 crc32_byte (unsigned chksum, char byte)
9030 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
9033 /* Generate a crc32 of a string. */
9035 unsigned
9036 crc32_string (unsigned chksum, const char *string)
9040 chksum = crc32_byte (chksum, *string);
9042 while (*string++);
9043 return chksum;
9046 /* P is a string that will be used in a symbol. Mask out any characters
9047 that are not valid in that context. */
9049 void
9050 clean_symbol_name (char *p)
9052 for (; *p; p++)
9053 if (! (ISALNUM (*p)
9054 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
9055 || *p == '$'
9056 #endif
9057 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
9058 || *p == '.'
9059 #endif
9061 *p = '_';
9064 /* Generate a name for a special-purpose function.
9065 The generated name may need to be unique across the whole link.
9066 Changes to this function may also require corresponding changes to
9067 xstrdup_mask_random.
9068 TYPE is some string to identify the purpose of this function to the
9069 linker or collect2; it must start with an uppercase letter,
9070 one of:
9071 I - for constructors
9072 D - for destructors
9073 N - for C++ anonymous namespaces
9074 F - for DWARF unwind frame information. */
9076 tree
9077 get_file_function_name (const char *type)
9079 char *buf;
9080 const char *p;
9081 char *q;
9083 /* If we already have a name we know to be unique, just use that. */
9084 if (first_global_object_name)
9085 p = q = ASTRDUP (first_global_object_name);
9086 /* If the target is handling the constructors/destructors, they
9087 will be local to this file and the name is only necessary for
9088 debugging purposes.
9089 We also assign sub_I and sub_D sufixes to constructors called from
9090 the global static constructors. These are always local. */
9091 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9092 || (strncmp (type, "sub_", 4) == 0
9093 && (type[4] == 'I' || type[4] == 'D')))
9095 const char *file = main_input_filename;
9096 if (! file)
9097 file = input_filename;
9098 /* Just use the file's basename, because the full pathname
9099 might be quite long. */
9100 p = q = ASTRDUP (lbasename (file));
9102 else
9104 /* Otherwise, the name must be unique across the entire link.
9105 We don't have anything that we know to be unique to this translation
9106 unit, so use what we do have and throw in some randomness. */
9107 unsigned len;
9108 const char *name = weak_global_object_name;
9109 const char *file = main_input_filename;
9111 if (! name)
9112 name = "";
9113 if (! file)
9114 file = input_filename;
9116 len = strlen (file);
9117 q = (char *) alloca (9 + 17 + len + 1);
9118 memcpy (q, file, len + 1);
9120 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9121 crc32_string (0, name), get_random_seed (false));
9123 p = q;
9126 clean_symbol_name (q);
9127 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9128 + strlen (type));
9130 /* Set up the name of the file-level functions we may need.
9131 Use a global object (which is already required to be unique over
9132 the program) rather than the file name (which imposes extra
9133 constraints). */
9134 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9136 return get_identifier (buf);
9139 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9141 /* Complain that the tree code of NODE does not match the expected 0
9142 terminated list of trailing codes. The trailing code list can be
9143 empty, for a more vague error message. FILE, LINE, and FUNCTION
9144 are of the caller. */
9146 void
9147 tree_check_failed (const_tree node, const char *file,
9148 int line, const char *function, ...)
9150 va_list args;
9151 const char *buffer;
9152 unsigned length = 0;
9153 int code;
9155 va_start (args, function);
9156 while ((code = va_arg (args, int)))
9157 length += 4 + strlen (tree_code_name[code]);
9158 va_end (args);
9159 if (length)
9161 char *tmp;
9162 va_start (args, function);
9163 length += strlen ("expected ");
9164 buffer = tmp = (char *) alloca (length);
9165 length = 0;
9166 while ((code = va_arg (args, int)))
9168 const char *prefix = length ? " or " : "expected ";
9170 strcpy (tmp + length, prefix);
9171 length += strlen (prefix);
9172 strcpy (tmp + length, tree_code_name[code]);
9173 length += strlen (tree_code_name[code]);
9175 va_end (args);
9177 else
9178 buffer = "unexpected node";
9180 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9181 buffer, tree_code_name[TREE_CODE (node)],
9182 function, trim_filename (file), line);
9185 /* Complain that the tree code of NODE does match the expected 0
9186 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9187 the caller. */
9189 void
9190 tree_not_check_failed (const_tree node, const char *file,
9191 int line, const char *function, ...)
9193 va_list args;
9194 char *buffer;
9195 unsigned length = 0;
9196 int code;
9198 va_start (args, function);
9199 while ((code = va_arg (args, int)))
9200 length += 4 + strlen (tree_code_name[code]);
9201 va_end (args);
9202 va_start (args, function);
9203 buffer = (char *) alloca (length);
9204 length = 0;
9205 while ((code = va_arg (args, int)))
9207 if (length)
9209 strcpy (buffer + length, " or ");
9210 length += 4;
9212 strcpy (buffer + length, tree_code_name[code]);
9213 length += strlen (tree_code_name[code]);
9215 va_end (args);
9217 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9218 buffer, tree_code_name[TREE_CODE (node)],
9219 function, trim_filename (file), line);
9222 /* Similar to tree_check_failed, except that we check for a class of tree
9223 code, given in CL. */
9225 void
9226 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9227 const char *file, int line, const char *function)
9229 internal_error
9230 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9231 TREE_CODE_CLASS_STRING (cl),
9232 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9233 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9236 /* Similar to tree_check_failed, except that instead of specifying a
9237 dozen codes, use the knowledge that they're all sequential. */
9239 void
9240 tree_range_check_failed (const_tree node, const char *file, int line,
9241 const char *function, enum tree_code c1,
9242 enum tree_code c2)
9244 char *buffer;
9245 unsigned length = 0;
9246 unsigned int c;
9248 for (c = c1; c <= c2; ++c)
9249 length += 4 + strlen (tree_code_name[c]);
9251 length += strlen ("expected ");
9252 buffer = (char *) alloca (length);
9253 length = 0;
9255 for (c = c1; c <= c2; ++c)
9257 const char *prefix = length ? " or " : "expected ";
9259 strcpy (buffer + length, prefix);
9260 length += strlen (prefix);
9261 strcpy (buffer + length, tree_code_name[c]);
9262 length += strlen (tree_code_name[c]);
9265 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9266 buffer, tree_code_name[TREE_CODE (node)],
9267 function, trim_filename (file), line);
9271 /* Similar to tree_check_failed, except that we check that a tree does
9272 not have the specified code, given in CL. */
9274 void
9275 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9276 const char *file, int line, const char *function)
9278 internal_error
9279 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9280 TREE_CODE_CLASS_STRING (cl),
9281 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9282 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9286 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9288 void
9289 omp_clause_check_failed (const_tree node, const char *file, int line,
9290 const char *function, enum omp_clause_code code)
9292 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9293 omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
9294 function, trim_filename (file), line);
9298 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9300 void
9301 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9302 const char *function, enum omp_clause_code c1,
9303 enum omp_clause_code c2)
9305 char *buffer;
9306 unsigned length = 0;
9307 unsigned int c;
9309 for (c = c1; c <= c2; ++c)
9310 length += 4 + strlen (omp_clause_code_name[c]);
9312 length += strlen ("expected ");
9313 buffer = (char *) alloca (length);
9314 length = 0;
9316 for (c = c1; c <= c2; ++c)
9318 const char *prefix = length ? " or " : "expected ";
9320 strcpy (buffer + length, prefix);
9321 length += strlen (prefix);
9322 strcpy (buffer + length, omp_clause_code_name[c]);
9323 length += strlen (omp_clause_code_name[c]);
9326 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9327 buffer, omp_clause_code_name[TREE_CODE (node)],
9328 function, trim_filename (file), line);
9332 #undef DEFTREESTRUCT
9333 #define DEFTREESTRUCT(VAL, NAME) NAME,
9335 static const char *ts_enum_names[] = {
9336 #include "treestruct.def"
9338 #undef DEFTREESTRUCT
9340 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9342 /* Similar to tree_class_check_failed, except that we check for
9343 whether CODE contains the tree structure identified by EN. */
9345 void
9346 tree_contains_struct_check_failed (const_tree node,
9347 const enum tree_node_structure_enum en,
9348 const char *file, int line,
9349 const char *function)
9351 internal_error
9352 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9353 TS_ENUM_NAME(en),
9354 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9358 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9359 (dynamically sized) vector. */
9361 void
9362 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9363 const char *function)
9365 internal_error
9366 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9367 idx + 1, len, function, trim_filename (file), line);
9370 /* Similar to above, except that the check is for the bounds of the operand
9371 vector of an expression node EXP. */
9373 void
9374 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9375 int line, const char *function)
9377 int code = TREE_CODE (exp);
9378 internal_error
9379 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9380 idx + 1, tree_code_name[code], TREE_OPERAND_LENGTH (exp),
9381 function, trim_filename (file), line);
9384 /* Similar to above, except that the check is for the number of
9385 operands of an OMP_CLAUSE node. */
9387 void
9388 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9389 int line, const char *function)
9391 internal_error
9392 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9393 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9394 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9395 trim_filename (file), line);
9397 #endif /* ENABLE_TREE_CHECKING */
9399 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9400 and mapped to the machine mode MODE. Initialize its fields and build
9401 the information necessary for debugging output. */
9403 static tree
9404 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
9406 tree t;
9407 hashval_t hashcode = 0;
9409 t = make_node (VECTOR_TYPE);
9410 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9411 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9412 SET_TYPE_MODE (t, mode);
9414 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9415 SET_TYPE_STRUCTURAL_EQUALITY (t);
9416 else if (TYPE_CANONICAL (innertype) != innertype
9417 || mode != VOIDmode)
9418 TYPE_CANONICAL (t)
9419 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9421 layout_type (t);
9423 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
9424 hashcode = iterative_hash_host_wide_int (nunits, hashcode);
9425 hashcode = iterative_hash_host_wide_int (mode, hashcode);
9426 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (t)), hashcode);
9427 t = type_hash_canon (hashcode, t);
9429 /* We have built a main variant, based on the main variant of the
9430 inner type. Use it to build the variant we return. */
9431 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9432 && TREE_TYPE (t) != innertype)
9433 return build_type_attribute_qual_variant (t,
9434 TYPE_ATTRIBUTES (innertype),
9435 TYPE_QUALS (innertype));
9437 return t;
9440 static tree
9441 make_or_reuse_type (unsigned size, int unsignedp)
9443 if (size == INT_TYPE_SIZE)
9444 return unsignedp ? unsigned_type_node : integer_type_node;
9445 if (size == CHAR_TYPE_SIZE)
9446 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9447 if (size == SHORT_TYPE_SIZE)
9448 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9449 if (size == LONG_TYPE_SIZE)
9450 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9451 if (size == LONG_LONG_TYPE_SIZE)
9452 return (unsignedp ? long_long_unsigned_type_node
9453 : long_long_integer_type_node);
9454 if (size == 128 && int128_integer_type_node)
9455 return (unsignedp ? int128_unsigned_type_node
9456 : int128_integer_type_node);
9458 if (unsignedp)
9459 return make_unsigned_type (size);
9460 else
9461 return make_signed_type (size);
9464 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9466 static tree
9467 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9469 if (satp)
9471 if (size == SHORT_FRACT_TYPE_SIZE)
9472 return unsignedp ? sat_unsigned_short_fract_type_node
9473 : sat_short_fract_type_node;
9474 if (size == FRACT_TYPE_SIZE)
9475 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9476 if (size == LONG_FRACT_TYPE_SIZE)
9477 return unsignedp ? sat_unsigned_long_fract_type_node
9478 : sat_long_fract_type_node;
9479 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9480 return unsignedp ? sat_unsigned_long_long_fract_type_node
9481 : sat_long_long_fract_type_node;
9483 else
9485 if (size == SHORT_FRACT_TYPE_SIZE)
9486 return unsignedp ? unsigned_short_fract_type_node
9487 : short_fract_type_node;
9488 if (size == FRACT_TYPE_SIZE)
9489 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9490 if (size == LONG_FRACT_TYPE_SIZE)
9491 return unsignedp ? unsigned_long_fract_type_node
9492 : long_fract_type_node;
9493 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9494 return unsignedp ? unsigned_long_long_fract_type_node
9495 : long_long_fract_type_node;
9498 return make_fract_type (size, unsignedp, satp);
9501 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9503 static tree
9504 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9506 if (satp)
9508 if (size == SHORT_ACCUM_TYPE_SIZE)
9509 return unsignedp ? sat_unsigned_short_accum_type_node
9510 : sat_short_accum_type_node;
9511 if (size == ACCUM_TYPE_SIZE)
9512 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9513 if (size == LONG_ACCUM_TYPE_SIZE)
9514 return unsignedp ? sat_unsigned_long_accum_type_node
9515 : sat_long_accum_type_node;
9516 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9517 return unsignedp ? sat_unsigned_long_long_accum_type_node
9518 : sat_long_long_accum_type_node;
9520 else
9522 if (size == SHORT_ACCUM_TYPE_SIZE)
9523 return unsignedp ? unsigned_short_accum_type_node
9524 : short_accum_type_node;
9525 if (size == ACCUM_TYPE_SIZE)
9526 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9527 if (size == LONG_ACCUM_TYPE_SIZE)
9528 return unsignedp ? unsigned_long_accum_type_node
9529 : long_accum_type_node;
9530 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9531 return unsignedp ? unsigned_long_long_accum_type_node
9532 : long_long_accum_type_node;
9535 return make_accum_type (size, unsignedp, satp);
9538 /* Create nodes for all integer types (and error_mark_node) using the sizes
9539 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9540 SHORT_DOUBLE specifies whether double should be of the same precision
9541 as float. */
9543 void
9544 build_common_tree_nodes (bool signed_char, bool short_double)
9546 error_mark_node = make_node (ERROR_MARK);
9547 TREE_TYPE (error_mark_node) = error_mark_node;
9549 initialize_sizetypes ();
9551 /* Define both `signed char' and `unsigned char'. */
9552 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9553 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9554 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9555 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9557 /* Define `char', which is like either `signed char' or `unsigned char'
9558 but not the same as either. */
9559 char_type_node
9560 = (signed_char
9561 ? make_signed_type (CHAR_TYPE_SIZE)
9562 : make_unsigned_type (CHAR_TYPE_SIZE));
9563 TYPE_STRING_FLAG (char_type_node) = 1;
9565 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9566 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9567 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9568 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9569 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9570 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9571 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9572 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9573 #if HOST_BITS_PER_WIDE_INT >= 64
9574 /* TODO: This isn't correct, but as logic depends at the moment on
9575 host's instead of target's wide-integer.
9576 If there is a target not supporting TImode, but has an 128-bit
9577 integer-scalar register, this target check needs to be adjusted. */
9578 if (targetm.scalar_mode_supported_p (TImode))
9580 int128_integer_type_node = make_signed_type (128);
9581 int128_unsigned_type_node = make_unsigned_type (128);
9583 #endif
9585 /* Define a boolean type. This type only represents boolean values but
9586 may be larger than char depending on the value of BOOL_TYPE_SIZE.
9587 Front ends which want to override this size (i.e. Java) can redefine
9588 boolean_type_node before calling build_common_tree_nodes_2. */
9589 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9590 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9591 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9592 TYPE_PRECISION (boolean_type_node) = 1;
9594 /* Define what type to use for size_t. */
9595 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9596 size_type_node = unsigned_type_node;
9597 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9598 size_type_node = long_unsigned_type_node;
9599 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9600 size_type_node = long_long_unsigned_type_node;
9601 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9602 size_type_node = short_unsigned_type_node;
9603 else
9604 gcc_unreachable ();
9606 /* Fill in the rest of the sized types. Reuse existing type nodes
9607 when possible. */
9608 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9609 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9610 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9611 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9612 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9614 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9615 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9616 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9617 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9618 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9620 access_public_node = get_identifier ("public");
9621 access_protected_node = get_identifier ("protected");
9622 access_private_node = get_identifier ("private");
9624 /* Define these next since types below may used them. */
9625 integer_zero_node = build_int_cst (integer_type_node, 0);
9626 integer_one_node = build_int_cst (integer_type_node, 1);
9627 integer_three_node = build_int_cst (integer_type_node, 3);
9628 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9630 size_zero_node = size_int (0);
9631 size_one_node = size_int (1);
9632 bitsize_zero_node = bitsize_int (0);
9633 bitsize_one_node = bitsize_int (1);
9634 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9636 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9637 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9639 void_type_node = make_node (VOID_TYPE);
9640 layout_type (void_type_node);
9642 /* We are not going to have real types in C with less than byte alignment,
9643 so we might as well not have any types that claim to have it. */
9644 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9645 TYPE_USER_ALIGN (void_type_node) = 0;
9647 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9648 layout_type (TREE_TYPE (null_pointer_node));
9650 ptr_type_node = build_pointer_type (void_type_node);
9651 const_ptr_type_node
9652 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9653 fileptr_type_node = ptr_type_node;
9655 float_type_node = make_node (REAL_TYPE);
9656 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9657 layout_type (float_type_node);
9659 double_type_node = make_node (REAL_TYPE);
9660 if (short_double)
9661 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9662 else
9663 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9664 layout_type (double_type_node);
9666 long_double_type_node = make_node (REAL_TYPE);
9667 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9668 layout_type (long_double_type_node);
9670 float_ptr_type_node = build_pointer_type (float_type_node);
9671 double_ptr_type_node = build_pointer_type (double_type_node);
9672 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9673 integer_ptr_type_node = build_pointer_type (integer_type_node);
9675 /* Fixed size integer types. */
9676 uint16_type_node = build_nonstandard_integer_type (16, true);
9677 uint32_type_node = build_nonstandard_integer_type (32, true);
9678 uint64_type_node = build_nonstandard_integer_type (64, true);
9680 /* Decimal float types. */
9681 dfloat32_type_node = make_node (REAL_TYPE);
9682 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9683 layout_type (dfloat32_type_node);
9684 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9685 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9687 dfloat64_type_node = make_node (REAL_TYPE);
9688 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9689 layout_type (dfloat64_type_node);
9690 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9691 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9693 dfloat128_type_node = make_node (REAL_TYPE);
9694 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9695 layout_type (dfloat128_type_node);
9696 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9697 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9699 complex_integer_type_node = build_complex_type (integer_type_node);
9700 complex_float_type_node = build_complex_type (float_type_node);
9701 complex_double_type_node = build_complex_type (double_type_node);
9702 complex_long_double_type_node = build_complex_type (long_double_type_node);
9704 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9705 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9706 sat_ ## KIND ## _type_node = \
9707 make_sat_signed_ ## KIND ## _type (SIZE); \
9708 sat_unsigned_ ## KIND ## _type_node = \
9709 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9710 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9711 unsigned_ ## KIND ## _type_node = \
9712 make_unsigned_ ## KIND ## _type (SIZE);
9714 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9715 sat_ ## WIDTH ## KIND ## _type_node = \
9716 make_sat_signed_ ## KIND ## _type (SIZE); \
9717 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9718 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9719 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9720 unsigned_ ## WIDTH ## KIND ## _type_node = \
9721 make_unsigned_ ## KIND ## _type (SIZE);
9723 /* Make fixed-point type nodes based on four different widths. */
9724 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9725 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9726 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9727 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9728 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9730 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9731 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9732 NAME ## _type_node = \
9733 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9734 u ## NAME ## _type_node = \
9735 make_or_reuse_unsigned_ ## KIND ## _type \
9736 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9737 sat_ ## NAME ## _type_node = \
9738 make_or_reuse_sat_signed_ ## KIND ## _type \
9739 (GET_MODE_BITSIZE (MODE ## mode)); \
9740 sat_u ## NAME ## _type_node = \
9741 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9742 (GET_MODE_BITSIZE (U ## MODE ## mode));
9744 /* Fixed-point type and mode nodes. */
9745 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9746 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9747 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9748 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9749 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9750 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9751 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9752 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9753 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9754 MAKE_FIXED_MODE_NODE (accum, da, DA)
9755 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9758 tree t = targetm.build_builtin_va_list ();
9760 /* Many back-ends define record types without setting TYPE_NAME.
9761 If we copied the record type here, we'd keep the original
9762 record type without a name. This breaks name mangling. So,
9763 don't copy record types and let c_common_nodes_and_builtins()
9764 declare the type to be __builtin_va_list. */
9765 if (TREE_CODE (t) != RECORD_TYPE)
9766 t = build_variant_type_copy (t);
9768 va_list_type_node = t;
9772 /* Modify DECL for given flags. */
9773 void
9774 set_call_expr_flags (tree decl, int flags)
9776 if (flags & ECF_NOTHROW)
9777 TREE_NOTHROW (decl) = 1;
9778 if (flags & ECF_CONST)
9779 TREE_READONLY (decl) = 1;
9780 if (flags & ECF_PURE)
9781 DECL_PURE_P (decl) = 1;
9782 if (flags & ECF_LOOPING_CONST_OR_PURE)
9783 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9784 if (flags & ECF_NOVOPS)
9785 DECL_IS_NOVOPS (decl) = 1;
9786 if (flags & ECF_NORETURN)
9787 TREE_THIS_VOLATILE (decl) = 1;
9788 if (flags & ECF_MALLOC)
9789 DECL_IS_MALLOC (decl) = 1;
9790 if (flags & ECF_RETURNS_TWICE)
9791 DECL_IS_RETURNS_TWICE (decl) = 1;
9792 if (flags & ECF_LEAF)
9793 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9794 NULL, DECL_ATTRIBUTES (decl));
9795 if ((flags & ECF_TM_PURE) && flag_tm)
9796 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("transaction_pure"),
9797 NULL, DECL_ATTRIBUTES (decl));
9798 /* Looping const or pure is implied by noreturn.
9799 There is currently no way to declare looping const or looping pure alone. */
9800 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9801 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9805 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9807 static void
9808 local_define_builtin (const char *name, tree type, enum built_in_function code,
9809 const char *library_name, int ecf_flags)
9811 tree decl;
9813 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9814 library_name, NULL_TREE);
9815 set_call_expr_flags (decl, ecf_flags);
9817 set_builtin_decl (code, decl, true);
9820 /* Call this function after instantiating all builtins that the language
9821 front end cares about. This will build the rest of the builtins that
9822 are relied upon by the tree optimizers and the middle-end. */
9824 void
9825 build_common_builtin_nodes (void)
9827 tree tmp, ftype;
9828 int ecf_flags;
9830 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9832 ftype = build_function_type (void_type_node, void_list_node);
9833 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
9834 "__builtin_unreachable",
9835 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9836 | ECF_CONST | ECF_LEAF);
9839 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9840 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9842 ftype = build_function_type_list (ptr_type_node,
9843 ptr_type_node, const_ptr_type_node,
9844 size_type_node, NULL_TREE);
9846 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
9847 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9848 "memcpy", ECF_NOTHROW | ECF_LEAF);
9849 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9850 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9851 "memmove", ECF_NOTHROW | ECF_LEAF);
9854 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
9856 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9857 const_ptr_type_node, size_type_node,
9858 NULL_TREE);
9859 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9860 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9863 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
9865 ftype = build_function_type_list (ptr_type_node,
9866 ptr_type_node, integer_type_node,
9867 size_type_node, NULL_TREE);
9868 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9869 "memset", ECF_NOTHROW | ECF_LEAF);
9872 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
9874 ftype = build_function_type_list (ptr_type_node,
9875 size_type_node, NULL_TREE);
9876 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9877 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9880 ftype = build_function_type_list (ptr_type_node, size_type_node,
9881 size_type_node, NULL_TREE);
9882 local_define_builtin ("__builtin_alloca_with_align", ftype,
9883 BUILT_IN_ALLOCA_WITH_ALIGN, "alloca",
9884 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9886 /* If we're checking the stack, `alloca' can throw. */
9887 if (flag_stack_check)
9889 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
9890 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
9893 ftype = build_function_type_list (void_type_node,
9894 ptr_type_node, ptr_type_node,
9895 ptr_type_node, NULL_TREE);
9896 local_define_builtin ("__builtin_init_trampoline", ftype,
9897 BUILT_IN_INIT_TRAMPOLINE,
9898 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9899 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
9900 BUILT_IN_INIT_HEAP_TRAMPOLINE,
9901 "__builtin_init_heap_trampoline",
9902 ECF_NOTHROW | ECF_LEAF);
9904 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9905 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9906 BUILT_IN_ADJUST_TRAMPOLINE,
9907 "__builtin_adjust_trampoline",
9908 ECF_CONST | ECF_NOTHROW);
9910 ftype = build_function_type_list (void_type_node,
9911 ptr_type_node, ptr_type_node, NULL_TREE);
9912 local_define_builtin ("__builtin_nonlocal_goto", ftype,
9913 BUILT_IN_NONLOCAL_GOTO,
9914 "__builtin_nonlocal_goto",
9915 ECF_NORETURN | ECF_NOTHROW);
9917 ftype = build_function_type_list (void_type_node,
9918 ptr_type_node, ptr_type_node, NULL_TREE);
9919 local_define_builtin ("__builtin_setjmp_setup", ftype,
9920 BUILT_IN_SETJMP_SETUP,
9921 "__builtin_setjmp_setup", ECF_NOTHROW);
9923 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9924 local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
9925 BUILT_IN_SETJMP_DISPATCHER,
9926 "__builtin_setjmp_dispatcher",
9927 ECF_PURE | ECF_NOTHROW);
9929 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9930 local_define_builtin ("__builtin_setjmp_receiver", ftype,
9931 BUILT_IN_SETJMP_RECEIVER,
9932 "__builtin_setjmp_receiver", ECF_NOTHROW);
9934 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9935 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
9936 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9938 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9939 local_define_builtin ("__builtin_stack_restore", ftype,
9940 BUILT_IN_STACK_RESTORE,
9941 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9943 /* If there's a possibility that we might use the ARM EABI, build the
9944 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
9945 if (targetm.arm_eabi_unwinder)
9947 ftype = build_function_type_list (void_type_node, NULL_TREE);
9948 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
9949 BUILT_IN_CXA_END_CLEANUP,
9950 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
9953 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9954 local_define_builtin ("__builtin_unwind_resume", ftype,
9955 BUILT_IN_UNWIND_RESUME,
9956 ((targetm_common.except_unwind_info (&global_options)
9957 == UI_SJLJ)
9958 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
9959 ECF_NORETURN);
9961 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
9963 ftype = build_function_type_list (ptr_type_node, integer_type_node,
9964 NULL_TREE);
9965 local_define_builtin ("__builtin_return_address", ftype,
9966 BUILT_IN_RETURN_ADDRESS,
9967 "__builtin_return_address",
9968 ECF_NOTHROW);
9971 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
9972 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9974 ftype = build_function_type_list (void_type_node, ptr_type_node,
9975 ptr_type_node, NULL_TREE);
9976 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
9977 local_define_builtin ("__cyg_profile_func_enter", ftype,
9978 BUILT_IN_PROFILE_FUNC_ENTER,
9979 "__cyg_profile_func_enter", 0);
9980 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9981 local_define_builtin ("__cyg_profile_func_exit", ftype,
9982 BUILT_IN_PROFILE_FUNC_EXIT,
9983 "__cyg_profile_func_exit", 0);
9986 /* The exception object and filter values from the runtime. The argument
9987 must be zero before exception lowering, i.e. from the front end. After
9988 exception lowering, it will be the region number for the exception
9989 landing pad. These functions are PURE instead of CONST to prevent
9990 them from being hoisted past the exception edge that will initialize
9991 its value in the landing pad. */
9992 ftype = build_function_type_list (ptr_type_node,
9993 integer_type_node, NULL_TREE);
9994 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
9995 /* Only use TM_PURE if we we have TM language support. */
9996 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
9997 ecf_flags |= ECF_TM_PURE;
9998 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
9999 "__builtin_eh_pointer", ecf_flags);
10001 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
10002 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
10003 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
10004 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10006 ftype = build_function_type_list (void_type_node,
10007 integer_type_node, integer_type_node,
10008 NULL_TREE);
10009 local_define_builtin ("__builtin_eh_copy_values", ftype,
10010 BUILT_IN_EH_COPY_VALUES,
10011 "__builtin_eh_copy_values", ECF_NOTHROW);
10013 /* Complex multiplication and division. These are handled as builtins
10014 rather than optabs because emit_library_call_value doesn't support
10015 complex. Further, we can do slightly better with folding these
10016 beasties if the real and complex parts of the arguments are separate. */
10018 int mode;
10020 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10022 char mode_name_buf[4], *q;
10023 const char *p;
10024 enum built_in_function mcode, dcode;
10025 tree type, inner_type;
10026 const char *prefix = "__";
10028 if (targetm.libfunc_gnu_prefix)
10029 prefix = "__gnu_";
10031 type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
10032 if (type == NULL)
10033 continue;
10034 inner_type = TREE_TYPE (type);
10036 ftype = build_function_type_list (type, inner_type, inner_type,
10037 inner_type, inner_type, NULL_TREE);
10039 mcode = ((enum built_in_function)
10040 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10041 dcode = ((enum built_in_function)
10042 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10044 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10045 *q = TOLOWER (*p);
10046 *q = '\0';
10048 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10049 NULL);
10050 local_define_builtin (built_in_names[mcode], ftype, mcode,
10051 built_in_names[mcode],
10052 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10054 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10055 NULL);
10056 local_define_builtin (built_in_names[dcode], ftype, dcode,
10057 built_in_names[dcode],
10058 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10063 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10064 better way.
10066 If we requested a pointer to a vector, build up the pointers that
10067 we stripped off while looking for the inner type. Similarly for
10068 return values from functions.
10070 The argument TYPE is the top of the chain, and BOTTOM is the
10071 new type which we will point to. */
10073 tree
10074 reconstruct_complex_type (tree type, tree bottom)
10076 tree inner, outer;
10078 if (TREE_CODE (type) == POINTER_TYPE)
10080 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10081 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10082 TYPE_REF_CAN_ALIAS_ALL (type));
10084 else if (TREE_CODE (type) == REFERENCE_TYPE)
10086 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10087 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10088 TYPE_REF_CAN_ALIAS_ALL (type));
10090 else if (TREE_CODE (type) == ARRAY_TYPE)
10092 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10093 outer = build_array_type (inner, TYPE_DOMAIN (type));
10095 else if (TREE_CODE (type) == FUNCTION_TYPE)
10097 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10098 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
10100 else if (TREE_CODE (type) == METHOD_TYPE)
10102 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10103 /* The build_method_type_directly() routine prepends 'this' to argument list,
10104 so we must compensate by getting rid of it. */
10105 outer
10106 = build_method_type_directly
10107 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10108 inner,
10109 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10111 else if (TREE_CODE (type) == OFFSET_TYPE)
10113 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10114 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10116 else
10117 return bottom;
10119 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10120 TYPE_QUALS (type));
10123 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10124 the inner type. */
10125 tree
10126 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
10128 int nunits;
10130 switch (GET_MODE_CLASS (mode))
10132 case MODE_VECTOR_INT:
10133 case MODE_VECTOR_FLOAT:
10134 case MODE_VECTOR_FRACT:
10135 case MODE_VECTOR_UFRACT:
10136 case MODE_VECTOR_ACCUM:
10137 case MODE_VECTOR_UACCUM:
10138 nunits = GET_MODE_NUNITS (mode);
10139 break;
10141 case MODE_INT:
10142 /* Check that there are no leftover bits. */
10143 gcc_assert (GET_MODE_BITSIZE (mode)
10144 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10146 nunits = GET_MODE_BITSIZE (mode)
10147 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10148 break;
10150 default:
10151 gcc_unreachable ();
10154 return make_vector_type (innertype, nunits, mode);
10157 /* Similarly, but takes the inner type and number of units, which must be
10158 a power of two. */
10160 tree
10161 build_vector_type (tree innertype, int nunits)
10163 return make_vector_type (innertype, nunits, VOIDmode);
10166 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10168 tree
10169 build_opaque_vector_type (tree innertype, int nunits)
10171 tree t = make_vector_type (innertype, nunits, VOIDmode);
10172 tree cand;
10173 /* We always build the non-opaque variant before the opaque one,
10174 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10175 cand = TYPE_NEXT_VARIANT (t);
10176 if (cand
10177 && TYPE_VECTOR_OPAQUE (cand)
10178 && check_qualified_type (cand, t, TYPE_QUALS (t)))
10179 return cand;
10180 /* Othewise build a variant type and make sure to queue it after
10181 the non-opaque type. */
10182 cand = build_distinct_type_copy (t);
10183 TYPE_VECTOR_OPAQUE (cand) = true;
10184 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10185 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10186 TYPE_NEXT_VARIANT (t) = cand;
10187 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10188 return cand;
10192 /* Given an initializer INIT, return TRUE if INIT is zero or some
10193 aggregate of zeros. Otherwise return FALSE. */
10194 bool
10195 initializer_zerop (const_tree init)
10197 tree elt;
10199 STRIP_NOPS (init);
10201 switch (TREE_CODE (init))
10203 case INTEGER_CST:
10204 return integer_zerop (init);
10206 case REAL_CST:
10207 /* ??? Note that this is not correct for C4X float formats. There,
10208 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10209 negative exponent. */
10210 return real_zerop (init)
10211 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10213 case FIXED_CST:
10214 return fixed_zerop (init);
10216 case COMPLEX_CST:
10217 return integer_zerop (init)
10218 || (real_zerop (init)
10219 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10220 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10222 case VECTOR_CST:
10224 unsigned i;
10225 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10226 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10227 return false;
10228 return true;
10231 case CONSTRUCTOR:
10233 unsigned HOST_WIDE_INT idx;
10235 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10236 if (!initializer_zerop (elt))
10237 return false;
10238 return true;
10241 case STRING_CST:
10243 int i;
10245 /* We need to loop through all elements to handle cases like
10246 "\0" and "\0foobar". */
10247 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10248 if (TREE_STRING_POINTER (init)[i] != '\0')
10249 return false;
10251 return true;
10254 default:
10255 return false;
10259 /* Check if vector VEC consists of all the equal elements and
10260 that the number of elements corresponds to the type of VEC.
10261 The function returns first element of the vector
10262 or NULL_TREE if the vector is not uniform. */
10263 tree
10264 uniform_vector_p (const_tree vec)
10266 tree first, t;
10267 unsigned i;
10269 if (vec == NULL_TREE)
10270 return NULL_TREE;
10272 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10274 if (TREE_CODE (vec) == VECTOR_CST)
10276 first = VECTOR_CST_ELT (vec, 0);
10277 for (i = 1; i < VECTOR_CST_NELTS (vec); ++i)
10278 if (!operand_equal_p (first, VECTOR_CST_ELT (vec, i), 0))
10279 return NULL_TREE;
10281 return first;
10284 else if (TREE_CODE (vec) == CONSTRUCTOR)
10286 first = error_mark_node;
10288 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10290 if (i == 0)
10292 first = t;
10293 continue;
10295 if (!operand_equal_p (first, t, 0))
10296 return NULL_TREE;
10298 if (i != TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)))
10299 return NULL_TREE;
10301 return first;
10304 return NULL_TREE;
10307 /* Build an empty statement at location LOC. */
10309 tree
10310 build_empty_stmt (location_t loc)
10312 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10313 SET_EXPR_LOCATION (t, loc);
10314 return t;
10318 /* Build an OpenMP clause with code CODE. LOC is the location of the
10319 clause. */
10321 tree
10322 build_omp_clause (location_t loc, enum omp_clause_code code)
10324 tree t;
10325 int size, length;
10327 length = omp_clause_num_ops[code];
10328 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10330 record_node_allocation_statistics (OMP_CLAUSE, size);
10332 t = ggc_alloc_tree_node (size);
10333 memset (t, 0, size);
10334 TREE_SET_CODE (t, OMP_CLAUSE);
10335 OMP_CLAUSE_SET_CODE (t, code);
10336 OMP_CLAUSE_LOCATION (t) = loc;
10338 return t;
10341 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10342 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10343 Except for the CODE and operand count field, other storage for the
10344 object is initialized to zeros. */
10346 tree
10347 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10349 tree t;
10350 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10352 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10353 gcc_assert (len >= 1);
10355 record_node_allocation_statistics (code, length);
10357 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10359 TREE_SET_CODE (t, code);
10361 /* Can't use TREE_OPERAND to store the length because if checking is
10362 enabled, it will try to check the length before we store it. :-P */
10363 t->exp.operands[0] = build_int_cst (sizetype, len);
10365 return t;
10368 /* Helper function for build_call_* functions; build a CALL_EXPR with
10369 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10370 the argument slots. */
10372 static tree
10373 build_call_1 (tree return_type, tree fn, int nargs)
10375 tree t;
10377 t = build_vl_exp (CALL_EXPR, nargs + 3);
10378 TREE_TYPE (t) = return_type;
10379 CALL_EXPR_FN (t) = fn;
10380 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10382 return t;
10385 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10386 FN and a null static chain slot. NARGS is the number of call arguments
10387 which are specified as "..." arguments. */
10389 tree
10390 build_call_nary (tree return_type, tree fn, int nargs, ...)
10392 tree ret;
10393 va_list args;
10394 va_start (args, nargs);
10395 ret = build_call_valist (return_type, fn, nargs, args);
10396 va_end (args);
10397 return ret;
10400 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10401 FN and a null static chain slot. NARGS is the number of call arguments
10402 which are specified as a va_list ARGS. */
10404 tree
10405 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10407 tree t;
10408 int i;
10410 t = build_call_1 (return_type, fn, nargs);
10411 for (i = 0; i < nargs; i++)
10412 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10413 process_call_operands (t);
10414 return t;
10417 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10418 FN and a null static chain slot. NARGS is the number of call arguments
10419 which are specified as a tree array ARGS. */
10421 tree
10422 build_call_array_loc (location_t loc, tree return_type, tree fn,
10423 int nargs, const tree *args)
10425 tree t;
10426 int i;
10428 t = build_call_1 (return_type, fn, nargs);
10429 for (i = 0; i < nargs; i++)
10430 CALL_EXPR_ARG (t, i) = args[i];
10431 process_call_operands (t);
10432 SET_EXPR_LOCATION (t, loc);
10433 return t;
10436 /* Like build_call_array, but takes a vec. */
10438 tree
10439 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10441 tree ret, t;
10442 unsigned int ix;
10444 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10445 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10446 CALL_EXPR_ARG (ret, ix) = t;
10447 process_call_operands (ret);
10448 return ret;
10452 /* Returns true if it is possible to prove that the index of
10453 an array access REF (an ARRAY_REF expression) falls into the
10454 array bounds. */
10456 bool
10457 in_array_bounds_p (tree ref)
10459 tree idx = TREE_OPERAND (ref, 1);
10460 tree min, max;
10462 if (TREE_CODE (idx) != INTEGER_CST)
10463 return false;
10465 min = array_ref_low_bound (ref);
10466 max = array_ref_up_bound (ref);
10467 if (!min
10468 || !max
10469 || TREE_CODE (min) != INTEGER_CST
10470 || TREE_CODE (max) != INTEGER_CST)
10471 return false;
10473 if (tree_int_cst_lt (idx, min)
10474 || tree_int_cst_lt (max, idx))
10475 return false;
10477 return true;
10480 /* Returns true if it is possible to prove that the range of
10481 an array access REF (an ARRAY_RANGE_REF expression) falls
10482 into the array bounds. */
10484 bool
10485 range_in_array_bounds_p (tree ref)
10487 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
10488 tree range_min, range_max, min, max;
10490 range_min = TYPE_MIN_VALUE (domain_type);
10491 range_max = TYPE_MAX_VALUE (domain_type);
10492 if (!range_min
10493 || !range_max
10494 || TREE_CODE (range_min) != INTEGER_CST
10495 || TREE_CODE (range_max) != INTEGER_CST)
10496 return false;
10498 min = array_ref_low_bound (ref);
10499 max = array_ref_up_bound (ref);
10500 if (!min
10501 || !max
10502 || TREE_CODE (min) != INTEGER_CST
10503 || TREE_CODE (max) != INTEGER_CST)
10504 return false;
10506 if (tree_int_cst_lt (range_min, min)
10507 || tree_int_cst_lt (max, range_max))
10508 return false;
10510 return true;
10513 /* Return true if T (assumed to be a DECL) must be assigned a memory
10514 location. */
10516 bool
10517 needs_to_live_in_memory (const_tree t)
10519 return (TREE_ADDRESSABLE (t)
10520 || is_global_var (t)
10521 || (TREE_CODE (t) == RESULT_DECL
10522 && !DECL_BY_REFERENCE (t)
10523 && aggregate_value_p (t, current_function_decl)));
10526 /* Return value of a constant X and sign-extend it. */
10528 HOST_WIDE_INT
10529 int_cst_value (const_tree x)
10531 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10532 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10534 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10535 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10536 || TREE_INT_CST_HIGH (x) == -1);
10538 if (bits < HOST_BITS_PER_WIDE_INT)
10540 bool negative = ((val >> (bits - 1)) & 1) != 0;
10541 if (negative)
10542 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10543 else
10544 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10547 return val;
10550 /* Return value of a constant X and sign-extend it. */
10552 HOST_WIDEST_INT
10553 widest_int_cst_value (const_tree x)
10555 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10556 unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
10558 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
10559 gcc_assert (HOST_BITS_PER_WIDEST_INT >= HOST_BITS_PER_DOUBLE_INT);
10560 val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_HIGH (x))
10561 << HOST_BITS_PER_WIDE_INT);
10562 #else
10563 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10564 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10565 || TREE_INT_CST_HIGH (x) == -1);
10566 #endif
10568 if (bits < HOST_BITS_PER_WIDEST_INT)
10570 bool negative = ((val >> (bits - 1)) & 1) != 0;
10571 if (negative)
10572 val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
10573 else
10574 val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
10577 return val;
10580 /* If TYPE is an integral or pointer type, return an integer type with
10581 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10582 if TYPE is already an integer type of signedness UNSIGNEDP. */
10584 tree
10585 signed_or_unsigned_type_for (int unsignedp, tree type)
10587 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10588 return type;
10590 if (TREE_CODE (type) == VECTOR_TYPE)
10592 tree inner = TREE_TYPE (type);
10593 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10594 if (!inner2)
10595 return NULL_TREE;
10596 if (inner == inner2)
10597 return type;
10598 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10601 if (!INTEGRAL_TYPE_P (type)
10602 && !POINTER_TYPE_P (type))
10603 return NULL_TREE;
10605 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10608 /* If TYPE is an integral or pointer type, return an integer type with
10609 the same precision which is unsigned, or itself if TYPE is already an
10610 unsigned integer type. */
10612 tree
10613 unsigned_type_for (tree type)
10615 return signed_or_unsigned_type_for (1, type);
10618 /* If TYPE is an integral or pointer type, return an integer type with
10619 the same precision which is signed, or itself if TYPE is already a
10620 signed integer type. */
10622 tree
10623 signed_type_for (tree type)
10625 return signed_or_unsigned_type_for (0, type);
10628 /* If TYPE is a vector type, return a signed integer vector type with the
10629 same width and number of subparts. Otherwise return boolean_type_node. */
10631 tree
10632 truth_type_for (tree type)
10634 if (TREE_CODE (type) == VECTOR_TYPE)
10636 tree elem = lang_hooks.types.type_for_size
10637 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10638 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10640 else
10641 return boolean_type_node;
10644 /* Returns the largest value obtainable by casting something in INNER type to
10645 OUTER type. */
10647 tree
10648 upper_bound_in_type (tree outer, tree inner)
10650 double_int high;
10651 unsigned int det = 0;
10652 unsigned oprec = TYPE_PRECISION (outer);
10653 unsigned iprec = TYPE_PRECISION (inner);
10654 unsigned prec;
10656 /* Compute a unique number for every combination. */
10657 det |= (oprec > iprec) ? 4 : 0;
10658 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10659 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10661 /* Determine the exponent to use. */
10662 switch (det)
10664 case 0:
10665 case 1:
10666 /* oprec <= iprec, outer: signed, inner: don't care. */
10667 prec = oprec - 1;
10668 break;
10669 case 2:
10670 case 3:
10671 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10672 prec = oprec;
10673 break;
10674 case 4:
10675 /* oprec > iprec, outer: signed, inner: signed. */
10676 prec = iprec - 1;
10677 break;
10678 case 5:
10679 /* oprec > iprec, outer: signed, inner: unsigned. */
10680 prec = iprec;
10681 break;
10682 case 6:
10683 /* oprec > iprec, outer: unsigned, inner: signed. */
10684 prec = oprec;
10685 break;
10686 case 7:
10687 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10688 prec = iprec;
10689 break;
10690 default:
10691 gcc_unreachable ();
10694 /* Compute 2^^prec - 1. */
10695 if (prec <= HOST_BITS_PER_WIDE_INT)
10697 high.high = 0;
10698 high.low = ((~(unsigned HOST_WIDE_INT) 0)
10699 >> (HOST_BITS_PER_WIDE_INT - prec));
10701 else
10703 high.high = ((~(unsigned HOST_WIDE_INT) 0)
10704 >> (HOST_BITS_PER_DOUBLE_INT - prec));
10705 high.low = ~(unsigned HOST_WIDE_INT) 0;
10708 return double_int_to_tree (outer, high);
10711 /* Returns the smallest value obtainable by casting something in INNER type to
10712 OUTER type. */
10714 tree
10715 lower_bound_in_type (tree outer, tree inner)
10717 double_int low;
10718 unsigned oprec = TYPE_PRECISION (outer);
10719 unsigned iprec = TYPE_PRECISION (inner);
10721 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10722 and obtain 0. */
10723 if (TYPE_UNSIGNED (outer)
10724 /* If we are widening something of an unsigned type, OUTER type
10725 contains all values of INNER type. In particular, both INNER
10726 and OUTER types have zero in common. */
10727 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10728 low.low = low.high = 0;
10729 else
10731 /* If we are widening a signed type to another signed type, we
10732 want to obtain -2^^(iprec-1). If we are keeping the
10733 precision or narrowing to a signed type, we want to obtain
10734 -2^(oprec-1). */
10735 unsigned prec = oprec > iprec ? iprec : oprec;
10737 if (prec <= HOST_BITS_PER_WIDE_INT)
10739 low.high = ~(unsigned HOST_WIDE_INT) 0;
10740 low.low = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
10742 else
10744 low.high = ((~(unsigned HOST_WIDE_INT) 0)
10745 << (prec - HOST_BITS_PER_WIDE_INT - 1));
10746 low.low = 0;
10750 return double_int_to_tree (outer, low);
10753 /* Return nonzero if two operands that are suitable for PHI nodes are
10754 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10755 SSA_NAME or invariant. Note that this is strictly an optimization.
10756 That is, callers of this function can directly call operand_equal_p
10757 and get the same result, only slower. */
10760 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10762 if (arg0 == arg1)
10763 return 1;
10764 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10765 return 0;
10766 return operand_equal_p (arg0, arg1, 0);
10769 /* Returns number of zeros at the end of binary representation of X.
10771 ??? Use ffs if available? */
10773 tree
10774 num_ending_zeros (const_tree x)
10776 unsigned HOST_WIDE_INT fr, nfr;
10777 unsigned num, abits;
10778 tree type = TREE_TYPE (x);
10780 if (TREE_INT_CST_LOW (x) == 0)
10782 num = HOST_BITS_PER_WIDE_INT;
10783 fr = TREE_INT_CST_HIGH (x);
10785 else
10787 num = 0;
10788 fr = TREE_INT_CST_LOW (x);
10791 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
10793 nfr = fr >> abits;
10794 if (nfr << abits == fr)
10796 num += abits;
10797 fr = nfr;
10801 if (num > TYPE_PRECISION (type))
10802 num = TYPE_PRECISION (type);
10804 return build_int_cst_type (type, num);
10808 #define WALK_SUBTREE(NODE) \
10809 do \
10811 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10812 if (result) \
10813 return result; \
10815 while (0)
10817 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10818 be walked whenever a type is seen in the tree. Rest of operands and return
10819 value are as for walk_tree. */
10821 static tree
10822 walk_type_fields (tree type, walk_tree_fn func, void *data,
10823 struct pointer_set_t *pset, walk_tree_lh lh)
10825 tree result = NULL_TREE;
10827 switch (TREE_CODE (type))
10829 case POINTER_TYPE:
10830 case REFERENCE_TYPE:
10831 /* We have to worry about mutually recursive pointers. These can't
10832 be written in C. They can in Ada. It's pathological, but
10833 there's an ACATS test (c38102a) that checks it. Deal with this
10834 by checking if we're pointing to another pointer, that one
10835 points to another pointer, that one does too, and we have no htab.
10836 If so, get a hash table. We check three levels deep to avoid
10837 the cost of the hash table if we don't need one. */
10838 if (POINTER_TYPE_P (TREE_TYPE (type))
10839 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10840 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10841 && !pset)
10843 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10844 func, data);
10845 if (result)
10846 return result;
10848 break;
10851 /* ... fall through ... */
10853 case COMPLEX_TYPE:
10854 WALK_SUBTREE (TREE_TYPE (type));
10855 break;
10857 case METHOD_TYPE:
10858 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10860 /* Fall through. */
10862 case FUNCTION_TYPE:
10863 WALK_SUBTREE (TREE_TYPE (type));
10865 tree arg;
10867 /* We never want to walk into default arguments. */
10868 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10869 WALK_SUBTREE (TREE_VALUE (arg));
10871 break;
10873 case ARRAY_TYPE:
10874 /* Don't follow this nodes's type if a pointer for fear that
10875 we'll have infinite recursion. If we have a PSET, then we
10876 need not fear. */
10877 if (pset
10878 || (!POINTER_TYPE_P (TREE_TYPE (type))
10879 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10880 WALK_SUBTREE (TREE_TYPE (type));
10881 WALK_SUBTREE (TYPE_DOMAIN (type));
10882 break;
10884 case OFFSET_TYPE:
10885 WALK_SUBTREE (TREE_TYPE (type));
10886 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
10887 break;
10889 default:
10890 break;
10893 return NULL_TREE;
10896 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
10897 called with the DATA and the address of each sub-tree. If FUNC returns a
10898 non-NULL value, the traversal is stopped, and the value returned by FUNC
10899 is returned. If PSET is non-NULL it is used to record the nodes visited,
10900 and to avoid visiting a node more than once. */
10902 tree
10903 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
10904 struct pointer_set_t *pset, walk_tree_lh lh)
10906 enum tree_code code;
10907 int walk_subtrees;
10908 tree result;
10910 #define WALK_SUBTREE_TAIL(NODE) \
10911 do \
10913 tp = & (NODE); \
10914 goto tail_recurse; \
10916 while (0)
10918 tail_recurse:
10919 /* Skip empty subtrees. */
10920 if (!*tp)
10921 return NULL_TREE;
10923 /* Don't walk the same tree twice, if the user has requested
10924 that we avoid doing so. */
10925 if (pset && pointer_set_insert (pset, *tp))
10926 return NULL_TREE;
10928 /* Call the function. */
10929 walk_subtrees = 1;
10930 result = (*func) (tp, &walk_subtrees, data);
10932 /* If we found something, return it. */
10933 if (result)
10934 return result;
10936 code = TREE_CODE (*tp);
10938 /* Even if we didn't, FUNC may have decided that there was nothing
10939 interesting below this point in the tree. */
10940 if (!walk_subtrees)
10942 /* But we still need to check our siblings. */
10943 if (code == TREE_LIST)
10944 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10945 else if (code == OMP_CLAUSE)
10946 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10947 else
10948 return NULL_TREE;
10951 if (lh)
10953 result = (*lh) (tp, &walk_subtrees, func, data, pset);
10954 if (result || !walk_subtrees)
10955 return result;
10958 switch (code)
10960 case ERROR_MARK:
10961 case IDENTIFIER_NODE:
10962 case INTEGER_CST:
10963 case REAL_CST:
10964 case FIXED_CST:
10965 case VECTOR_CST:
10966 case STRING_CST:
10967 case BLOCK:
10968 case PLACEHOLDER_EXPR:
10969 case SSA_NAME:
10970 case FIELD_DECL:
10971 case RESULT_DECL:
10972 /* None of these have subtrees other than those already walked
10973 above. */
10974 break;
10976 case TREE_LIST:
10977 WALK_SUBTREE (TREE_VALUE (*tp));
10978 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10979 break;
10981 case TREE_VEC:
10983 int len = TREE_VEC_LENGTH (*tp);
10985 if (len == 0)
10986 break;
10988 /* Walk all elements but the first. */
10989 while (--len)
10990 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
10992 /* Now walk the first one as a tail call. */
10993 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
10996 case COMPLEX_CST:
10997 WALK_SUBTREE (TREE_REALPART (*tp));
10998 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
11000 case CONSTRUCTOR:
11002 unsigned HOST_WIDE_INT idx;
11003 constructor_elt *ce;
11005 for (idx = 0; vec_safe_iterate(CONSTRUCTOR_ELTS (*tp), idx, &ce); idx++)
11006 WALK_SUBTREE (ce->value);
11008 break;
11010 case SAVE_EXPR:
11011 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
11013 case BIND_EXPR:
11015 tree decl;
11016 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
11018 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11019 into declarations that are just mentioned, rather than
11020 declared; they don't really belong to this part of the tree.
11021 And, we can see cycles: the initializer for a declaration
11022 can refer to the declaration itself. */
11023 WALK_SUBTREE (DECL_INITIAL (decl));
11024 WALK_SUBTREE (DECL_SIZE (decl));
11025 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11027 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
11030 case STATEMENT_LIST:
11032 tree_stmt_iterator i;
11033 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
11034 WALK_SUBTREE (*tsi_stmt_ptr (i));
11036 break;
11038 case OMP_CLAUSE:
11039 switch (OMP_CLAUSE_CODE (*tp))
11041 case OMP_CLAUSE_PRIVATE:
11042 case OMP_CLAUSE_SHARED:
11043 case OMP_CLAUSE_FIRSTPRIVATE:
11044 case OMP_CLAUSE_COPYIN:
11045 case OMP_CLAUSE_COPYPRIVATE:
11046 case OMP_CLAUSE_FINAL:
11047 case OMP_CLAUSE_IF:
11048 case OMP_CLAUSE_NUM_THREADS:
11049 case OMP_CLAUSE_SCHEDULE:
11050 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
11051 /* FALLTHRU */
11053 case OMP_CLAUSE_NOWAIT:
11054 case OMP_CLAUSE_ORDERED:
11055 case OMP_CLAUSE_DEFAULT:
11056 case OMP_CLAUSE_UNTIED:
11057 case OMP_CLAUSE_MERGEABLE:
11058 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11060 case OMP_CLAUSE_LASTPRIVATE:
11061 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11062 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
11063 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11065 case OMP_CLAUSE_COLLAPSE:
11067 int i;
11068 for (i = 0; i < 3; i++)
11069 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11070 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11073 case OMP_CLAUSE_REDUCTION:
11075 int i;
11076 for (i = 0; i < 4; i++)
11077 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11078 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11081 default:
11082 gcc_unreachable ();
11084 break;
11086 case TARGET_EXPR:
11088 int i, len;
11090 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11091 But, we only want to walk once. */
11092 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11093 for (i = 0; i < len; ++i)
11094 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11095 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11098 case DECL_EXPR:
11099 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11100 defining. We only want to walk into these fields of a type in this
11101 case and not in the general case of a mere reference to the type.
11103 The criterion is as follows: if the field can be an expression, it
11104 must be walked only here. This should be in keeping with the fields
11105 that are directly gimplified in gimplify_type_sizes in order for the
11106 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11107 variable-sized types.
11109 Note that DECLs get walked as part of processing the BIND_EXPR. */
11110 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11112 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11113 if (TREE_CODE (*type_p) == ERROR_MARK)
11114 return NULL_TREE;
11116 /* Call the function for the type. See if it returns anything or
11117 doesn't want us to continue. If we are to continue, walk both
11118 the normal fields and those for the declaration case. */
11119 result = (*func) (type_p, &walk_subtrees, data);
11120 if (result || !walk_subtrees)
11121 return result;
11123 /* But do not walk a pointed-to type since it may itself need to
11124 be walked in the declaration case if it isn't anonymous. */
11125 if (!POINTER_TYPE_P (*type_p))
11127 result = walk_type_fields (*type_p, func, data, pset, lh);
11128 if (result)
11129 return result;
11132 /* If this is a record type, also walk the fields. */
11133 if (RECORD_OR_UNION_TYPE_P (*type_p))
11135 tree field;
11137 for (field = TYPE_FIELDS (*type_p); field;
11138 field = DECL_CHAIN (field))
11140 /* We'd like to look at the type of the field, but we can
11141 easily get infinite recursion. So assume it's pointed
11142 to elsewhere in the tree. Also, ignore things that
11143 aren't fields. */
11144 if (TREE_CODE (field) != FIELD_DECL)
11145 continue;
11147 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11148 WALK_SUBTREE (DECL_SIZE (field));
11149 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11150 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11151 WALK_SUBTREE (DECL_QUALIFIER (field));
11155 /* Same for scalar types. */
11156 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11157 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11158 || TREE_CODE (*type_p) == INTEGER_TYPE
11159 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11160 || TREE_CODE (*type_p) == REAL_TYPE)
11162 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11163 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11166 WALK_SUBTREE (TYPE_SIZE (*type_p));
11167 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11169 /* FALLTHRU */
11171 default:
11172 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11174 int i, len;
11176 /* Walk over all the sub-trees of this operand. */
11177 len = TREE_OPERAND_LENGTH (*tp);
11179 /* Go through the subtrees. We need to do this in forward order so
11180 that the scope of a FOR_EXPR is handled properly. */
11181 if (len)
11183 for (i = 0; i < len - 1; ++i)
11184 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11185 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11188 /* If this is a type, walk the needed fields in the type. */
11189 else if (TYPE_P (*tp))
11190 return walk_type_fields (*tp, func, data, pset, lh);
11191 break;
11194 /* We didn't find what we were looking for. */
11195 return NULL_TREE;
11197 #undef WALK_SUBTREE_TAIL
11199 #undef WALK_SUBTREE
11201 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11203 tree
11204 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11205 walk_tree_lh lh)
11207 tree result;
11208 struct pointer_set_t *pset;
11210 pset = pointer_set_create ();
11211 result = walk_tree_1 (tp, func, data, pset, lh);
11212 pointer_set_destroy (pset);
11213 return result;
11217 tree
11218 tree_block (tree t)
11220 char const c = TREE_CODE_CLASS (TREE_CODE (t));
11222 if (IS_EXPR_CODE_CLASS (c))
11223 return LOCATION_BLOCK (t->exp.locus);
11224 gcc_unreachable ();
11225 return NULL;
11228 void
11229 tree_set_block (tree t, tree b)
11231 char const c = TREE_CODE_CLASS (TREE_CODE (t));
11233 if (IS_EXPR_CODE_CLASS (c))
11235 if (b)
11236 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
11237 else
11238 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
11240 else
11241 gcc_unreachable ();
11244 /* Create a nameless artificial label and put it in the current
11245 function context. The label has a location of LOC. Returns the
11246 newly created label. */
11248 tree
11249 create_artificial_label (location_t loc)
11251 tree lab = build_decl (loc,
11252 LABEL_DECL, NULL_TREE, void_type_node);
11254 DECL_ARTIFICIAL (lab) = 1;
11255 DECL_IGNORED_P (lab) = 1;
11256 DECL_CONTEXT (lab) = current_function_decl;
11257 return lab;
11260 /* Given a tree, try to return a useful variable name that we can use
11261 to prefix a temporary that is being assigned the value of the tree.
11262 I.E. given <temp> = &A, return A. */
11264 const char *
11265 get_name (tree t)
11267 tree stripped_decl;
11269 stripped_decl = t;
11270 STRIP_NOPS (stripped_decl);
11271 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11272 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11273 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11275 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11276 if (!name)
11277 return NULL;
11278 return IDENTIFIER_POINTER (name);
11280 else
11282 switch (TREE_CODE (stripped_decl))
11284 case ADDR_EXPR:
11285 return get_name (TREE_OPERAND (stripped_decl, 0));
11286 default:
11287 return NULL;
11292 /* Return true if TYPE has a variable argument list. */
11294 bool
11295 stdarg_p (const_tree fntype)
11297 function_args_iterator args_iter;
11298 tree n = NULL_TREE, t;
11300 if (!fntype)
11301 return false;
11303 FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
11305 n = t;
11308 return n != NULL_TREE && n != void_type_node;
11311 /* Return true if TYPE has a prototype. */
11313 bool
11314 prototype_p (tree fntype)
11316 tree t;
11318 gcc_assert (fntype != NULL_TREE);
11320 t = TYPE_ARG_TYPES (fntype);
11321 return (t != NULL_TREE);
11324 /* If BLOCK is inlined from an __attribute__((__artificial__))
11325 routine, return pointer to location from where it has been
11326 called. */
11327 location_t *
11328 block_nonartificial_location (tree block)
11330 location_t *ret = NULL;
11332 while (block && TREE_CODE (block) == BLOCK
11333 && BLOCK_ABSTRACT_ORIGIN (block))
11335 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11337 while (TREE_CODE (ao) == BLOCK
11338 && BLOCK_ABSTRACT_ORIGIN (ao)
11339 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11340 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11342 if (TREE_CODE (ao) == FUNCTION_DECL)
11344 /* If AO is an artificial inline, point RET to the
11345 call site locus at which it has been inlined and continue
11346 the loop, in case AO's caller is also an artificial
11347 inline. */
11348 if (DECL_DECLARED_INLINE_P (ao)
11349 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11350 ret = &BLOCK_SOURCE_LOCATION (block);
11351 else
11352 break;
11354 else if (TREE_CODE (ao) != BLOCK)
11355 break;
11357 block = BLOCK_SUPERCONTEXT (block);
11359 return ret;
11363 /* If EXP is inlined from an __attribute__((__artificial__))
11364 function, return the location of the original call expression. */
11366 location_t
11367 tree_nonartificial_location (tree exp)
11369 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11371 if (loc)
11372 return *loc;
11373 else
11374 return EXPR_LOCATION (exp);
11378 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11379 nodes. */
11381 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11383 static hashval_t
11384 cl_option_hash_hash (const void *x)
11386 const_tree const t = (const_tree) x;
11387 const char *p;
11388 size_t i;
11389 size_t len = 0;
11390 hashval_t hash = 0;
11392 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11394 p = (const char *)TREE_OPTIMIZATION (t);
11395 len = sizeof (struct cl_optimization);
11398 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11400 p = (const char *)TREE_TARGET_OPTION (t);
11401 len = sizeof (struct cl_target_option);
11404 else
11405 gcc_unreachable ();
11407 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11408 something else. */
11409 for (i = 0; i < len; i++)
11410 if (p[i])
11411 hash = (hash << 4) ^ ((i << 2) | p[i]);
11413 return hash;
11416 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11417 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11418 same. */
11420 static int
11421 cl_option_hash_eq (const void *x, const void *y)
11423 const_tree const xt = (const_tree) x;
11424 const_tree const yt = (const_tree) y;
11425 const char *xp;
11426 const char *yp;
11427 size_t len;
11429 if (TREE_CODE (xt) != TREE_CODE (yt))
11430 return 0;
11432 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11434 xp = (const char *)TREE_OPTIMIZATION (xt);
11435 yp = (const char *)TREE_OPTIMIZATION (yt);
11436 len = sizeof (struct cl_optimization);
11439 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11441 xp = (const char *)TREE_TARGET_OPTION (xt);
11442 yp = (const char *)TREE_TARGET_OPTION (yt);
11443 len = sizeof (struct cl_target_option);
11446 else
11447 gcc_unreachable ();
11449 return (memcmp (xp, yp, len) == 0);
11452 /* Build an OPTIMIZATION_NODE based on the current options. */
11454 tree
11455 build_optimization_node (void)
11457 tree t;
11458 void **slot;
11460 /* Use the cache of optimization nodes. */
11462 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11463 &global_options);
11465 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
11466 t = (tree) *slot;
11467 if (!t)
11469 /* Insert this one into the hash table. */
11470 t = cl_optimization_node;
11471 *slot = t;
11473 /* Make a new node for next time round. */
11474 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11477 return t;
11480 /* Build a TARGET_OPTION_NODE based on the current options. */
11482 tree
11483 build_target_option_node (void)
11485 tree t;
11486 void **slot;
11488 /* Use the cache of optimization nodes. */
11490 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11491 &global_options);
11493 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
11494 t = (tree) *slot;
11495 if (!t)
11497 /* Insert this one into the hash table. */
11498 t = cl_target_option_node;
11499 *slot = t;
11501 /* Make a new node for next time round. */
11502 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11505 return t;
11508 /* Determine the "ultimate origin" of a block. The block may be an inlined
11509 instance of an inlined instance of a block which is local to an inline
11510 function, so we have to trace all of the way back through the origin chain
11511 to find out what sort of node actually served as the original seed for the
11512 given block. */
11514 tree
11515 block_ultimate_origin (const_tree block)
11517 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11519 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
11520 nodes in the function to point to themselves; ignore that if
11521 we're trying to output the abstract instance of this function. */
11522 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11523 return NULL_TREE;
11525 if (immediate_origin == NULL_TREE)
11526 return NULL_TREE;
11527 else
11529 tree ret_val;
11530 tree lookahead = immediate_origin;
11534 ret_val = lookahead;
11535 lookahead = (TREE_CODE (ret_val) == BLOCK
11536 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11538 while (lookahead != NULL && lookahead != ret_val);
11540 /* The block's abstract origin chain may not be the *ultimate* origin of
11541 the block. It could lead to a DECL that has an abstract origin set.
11542 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11543 will give us if it has one). Note that DECL's abstract origins are
11544 supposed to be the most distant ancestor (or so decl_ultimate_origin
11545 claims), so we don't need to loop following the DECL origins. */
11546 if (DECL_P (ret_val))
11547 return DECL_ORIGIN (ret_val);
11549 return ret_val;
11553 /* Return true if T1 and T2 are equivalent lists. */
11555 bool
11556 list_equal_p (const_tree t1, const_tree t2)
11558 for (; t1 && t2; t1 = TREE_CHAIN (t1) , t2 = TREE_CHAIN (t2))
11559 if (TREE_VALUE (t1) != TREE_VALUE (t2))
11560 return false;
11561 return !t1 && !t2;
11564 /* Return true iff conversion in EXP generates no instruction. Mark
11565 it inline so that we fully inline into the stripping functions even
11566 though we have two uses of this function. */
11568 static inline bool
11569 tree_nop_conversion (const_tree exp)
11571 tree outer_type, inner_type;
11573 if (!CONVERT_EXPR_P (exp)
11574 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11575 return false;
11576 if (TREE_OPERAND (exp, 0) == error_mark_node)
11577 return false;
11579 outer_type = TREE_TYPE (exp);
11580 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11582 if (!inner_type)
11583 return false;
11585 /* Use precision rather then machine mode when we can, which gives
11586 the correct answer even for submode (bit-field) types. */
11587 if ((INTEGRAL_TYPE_P (outer_type)
11588 || POINTER_TYPE_P (outer_type)
11589 || TREE_CODE (outer_type) == OFFSET_TYPE)
11590 && (INTEGRAL_TYPE_P (inner_type)
11591 || POINTER_TYPE_P (inner_type)
11592 || TREE_CODE (inner_type) == OFFSET_TYPE))
11593 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11595 /* Otherwise fall back on comparing machine modes (e.g. for
11596 aggregate types, floats). */
11597 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11600 /* Return true iff conversion in EXP generates no instruction. Don't
11601 consider conversions changing the signedness. */
11603 static bool
11604 tree_sign_nop_conversion (const_tree exp)
11606 tree outer_type, inner_type;
11608 if (!tree_nop_conversion (exp))
11609 return false;
11611 outer_type = TREE_TYPE (exp);
11612 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11614 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11615 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11618 /* Strip conversions from EXP according to tree_nop_conversion and
11619 return the resulting expression. */
11621 tree
11622 tree_strip_nop_conversions (tree exp)
11624 while (tree_nop_conversion (exp))
11625 exp = TREE_OPERAND (exp, 0);
11626 return exp;
11629 /* Strip conversions from EXP according to tree_sign_nop_conversion
11630 and return the resulting expression. */
11632 tree
11633 tree_strip_sign_nop_conversions (tree exp)
11635 while (tree_sign_nop_conversion (exp))
11636 exp = TREE_OPERAND (exp, 0);
11637 return exp;
11640 /* Avoid any floating point extensions from EXP. */
11641 tree
11642 strip_float_extensions (tree exp)
11644 tree sub, expt, subt;
11646 /* For floating point constant look up the narrowest type that can hold
11647 it properly and handle it like (type)(narrowest_type)constant.
11648 This way we can optimize for instance a=a*2.0 where "a" is float
11649 but 2.0 is double constant. */
11650 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11652 REAL_VALUE_TYPE orig;
11653 tree type = NULL;
11655 orig = TREE_REAL_CST (exp);
11656 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11657 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11658 type = float_type_node;
11659 else if (TYPE_PRECISION (TREE_TYPE (exp))
11660 > TYPE_PRECISION (double_type_node)
11661 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11662 type = double_type_node;
11663 if (type)
11664 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11667 if (!CONVERT_EXPR_P (exp))
11668 return exp;
11670 sub = TREE_OPERAND (exp, 0);
11671 subt = TREE_TYPE (sub);
11672 expt = TREE_TYPE (exp);
11674 if (!FLOAT_TYPE_P (subt))
11675 return exp;
11677 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11678 return exp;
11680 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11681 return exp;
11683 return strip_float_extensions (sub);
11686 /* Strip out all handled components that produce invariant
11687 offsets. */
11689 const_tree
11690 strip_invariant_refs (const_tree op)
11692 while (handled_component_p (op))
11694 switch (TREE_CODE (op))
11696 case ARRAY_REF:
11697 case ARRAY_RANGE_REF:
11698 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11699 || TREE_OPERAND (op, 2) != NULL_TREE
11700 || TREE_OPERAND (op, 3) != NULL_TREE)
11701 return NULL;
11702 break;
11704 case COMPONENT_REF:
11705 if (TREE_OPERAND (op, 2) != NULL_TREE)
11706 return NULL;
11707 break;
11709 default:;
11711 op = TREE_OPERAND (op, 0);
11714 return op;
11717 static GTY(()) tree gcc_eh_personality_decl;
11719 /* Return the GCC personality function decl. */
11721 tree
11722 lhd_gcc_personality (void)
11724 if (!gcc_eh_personality_decl)
11725 gcc_eh_personality_decl = build_personality_function ("gcc");
11726 return gcc_eh_personality_decl;
11729 /* For languages with One Definition Rule, work out if
11730 trees are actually the same even if the tree representation
11731 differs. This handles only decls appearing in TYPE_NAME
11732 and TYPE_CONTEXT. That is NAMESPACE_DECL, TYPE_DECL,
11733 RECORD_TYPE and IDENTIFIER_NODE. */
11735 static bool
11736 same_for_odr (tree t1, tree t2)
11738 if (t1 == t2)
11739 return true;
11740 if (!t1 || !t2)
11741 return false;
11742 /* C and C++ FEs differ by using IDENTIFIER_NODE and TYPE_DECL. */
11743 if (TREE_CODE (t1) == IDENTIFIER_NODE
11744 && TREE_CODE (t2) == TYPE_DECL
11745 && DECL_FILE_SCOPE_P (t1))
11747 t2 = DECL_NAME (t2);
11748 gcc_assert (TREE_CODE (t2) == IDENTIFIER_NODE);
11750 if (TREE_CODE (t2) == IDENTIFIER_NODE
11751 && TREE_CODE (t1) == TYPE_DECL
11752 && DECL_FILE_SCOPE_P (t2))
11754 t1 = DECL_NAME (t1);
11755 gcc_assert (TREE_CODE (t1) == IDENTIFIER_NODE);
11757 if (TREE_CODE (t1) != TREE_CODE (t2))
11758 return false;
11759 if (TYPE_P (t1))
11760 return types_same_for_odr (t1, t2);
11761 if (DECL_P (t1))
11762 return decls_same_for_odr (t1, t2);
11763 return false;
11766 /* For languages with One Definition Rule, work out if
11767 decls are actually the same even if the tree representation
11768 differs. This handles only decls appearing in TYPE_NAME
11769 and TYPE_CONTEXT. That is NAMESPACE_DECL, TYPE_DECL,
11770 RECORD_TYPE and IDENTIFIER_NODE. */
11772 static bool
11773 decls_same_for_odr (tree decl1, tree decl2)
11775 if (decl1 && TREE_CODE (decl1) == TYPE_DECL
11776 && DECL_ORIGINAL_TYPE (decl1))
11777 decl1 = DECL_ORIGINAL_TYPE (decl1);
11778 if (decl2 && TREE_CODE (decl2) == TYPE_DECL
11779 && DECL_ORIGINAL_TYPE (decl2))
11780 decl2 = DECL_ORIGINAL_TYPE (decl2);
11781 if (decl1 == decl2)
11782 return true;
11783 if (!decl1 || !decl2)
11784 return false;
11785 gcc_checking_assert (DECL_P (decl1) && DECL_P (decl2));
11786 if (TREE_CODE (decl1) != TREE_CODE (decl2))
11787 return false;
11788 if (TREE_CODE (decl1) == TRANSLATION_UNIT_DECL)
11789 return true;
11790 if (TREE_CODE (decl1) != NAMESPACE_DECL
11791 && TREE_CODE (decl1) != TYPE_DECL)
11792 return false;
11793 if (!DECL_NAME (decl1))
11794 return false;
11795 gcc_checking_assert (TREE_CODE (DECL_NAME (decl1)) == IDENTIFIER_NODE);
11796 gcc_checking_assert (!DECL_NAME (decl2)
11797 || TREE_CODE (DECL_NAME (decl2)) == IDENTIFIER_NODE);
11798 if (DECL_NAME (decl1) != DECL_NAME (decl2))
11799 return false;
11800 return same_for_odr (DECL_CONTEXT (decl1),
11801 DECL_CONTEXT (decl2));
11804 /* For languages with One Definition Rule, work out if
11805 types are same even if the tree representation differs.
11806 This is non-trivial for LTO where minnor differences in
11807 the type representation may have prevented type merging
11808 to merge two copies of otherwise equivalent type. */
11810 bool
11811 types_same_for_odr (tree type1, tree type2)
11813 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
11814 type1 = TYPE_MAIN_VARIANT (type1);
11815 type2 = TYPE_MAIN_VARIANT (type2);
11816 if (type1 == type2)
11817 return true;
11819 /* If types are not structuraly same, do not bother to contnue.
11820 Match in the remainder of code would mean ODR violation. */
11821 if (!types_compatible_p (type1, type2))
11822 return false;
11824 #ifndef ENABLE_CHECKING
11825 if (!in_lto_p)
11826 return false;
11827 #endif
11829 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
11830 on the corresponding TYPE_STUB_DECL. */
11831 if (TYPE_STUB_DECL (type1) != TYPE_STUB_DECL (type2)
11832 && (!TYPE_STUB_DECL (type1)
11833 || !TYPE_STUB_DECL (type2)
11834 || !TREE_PUBLIC (TYPE_STUB_DECL (type1))
11835 || !TREE_PUBLIC (TYPE_STUB_DECL (type2))))
11836 return false;
11838 if (!TYPE_NAME (type1))
11839 return false;
11840 if (!decls_same_for_odr (TYPE_NAME (type1), TYPE_NAME (type2)))
11841 return false;
11842 if (!same_for_odr (TYPE_CONTEXT (type1), TYPE_CONTEXT (type2)))
11843 return false;
11844 /* When not in LTO the MAIN_VARIANT check should be the same. */
11845 gcc_assert (in_lto_p);
11847 return true;
11850 /* Try to find a base info of BINFO that would have its field decl at offset
11851 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11852 found, return, otherwise return NULL_TREE. */
11854 tree
11855 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
11857 tree type = BINFO_TYPE (binfo);
11859 while (true)
11861 HOST_WIDE_INT pos, size;
11862 tree fld;
11863 int i;
11865 if (types_same_for_odr (type, expected_type))
11866 return binfo;
11867 if (offset < 0)
11868 return NULL_TREE;
11870 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
11872 if (TREE_CODE (fld) != FIELD_DECL)
11873 continue;
11875 pos = int_bit_position (fld);
11876 size = tree_low_cst (DECL_SIZE (fld), 1);
11877 if (pos <= offset && (pos + size) > offset)
11878 break;
11880 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11881 return NULL_TREE;
11883 if (!DECL_ARTIFICIAL (fld))
11885 binfo = TYPE_BINFO (TREE_TYPE (fld));
11886 if (!binfo)
11887 return NULL_TREE;
11889 /* Offset 0 indicates the primary base, whose vtable contents are
11890 represented in the binfo for the derived class. */
11891 else if (offset != 0)
11893 tree base_binfo, found_binfo = NULL_TREE;
11894 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11895 if (types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
11897 found_binfo = base_binfo;
11898 break;
11900 if (!found_binfo)
11901 return NULL_TREE;
11902 binfo = found_binfo;
11905 type = TREE_TYPE (fld);
11906 offset -= pos;
11910 /* Returns true if X is a typedef decl. */
11912 bool
11913 is_typedef_decl (tree x)
11915 return (x && TREE_CODE (x) == TYPE_DECL
11916 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
11919 /* Returns true iff TYPE is a type variant created for a typedef. */
11921 bool
11922 typedef_variant_p (tree type)
11924 return is_typedef_decl (TYPE_NAME (type));
11927 /* Warn about a use of an identifier which was marked deprecated. */
11928 void
11929 warn_deprecated_use (tree node, tree attr)
11931 const char *msg;
11933 if (node == 0 || !warn_deprecated_decl)
11934 return;
11936 if (!attr)
11938 if (DECL_P (node))
11939 attr = DECL_ATTRIBUTES (node);
11940 else if (TYPE_P (node))
11942 tree decl = TYPE_STUB_DECL (node);
11943 if (decl)
11944 attr = lookup_attribute ("deprecated",
11945 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
11949 if (attr)
11950 attr = lookup_attribute ("deprecated", attr);
11952 if (attr)
11953 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
11954 else
11955 msg = NULL;
11957 if (DECL_P (node))
11959 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
11960 if (msg)
11961 warning (OPT_Wdeprecated_declarations,
11962 "%qD is deprecated (declared at %r%s:%d%R): %s",
11963 node, "locus", xloc.file, xloc.line, msg);
11964 else
11965 warning (OPT_Wdeprecated_declarations,
11966 "%qD is deprecated (declared at %r%s:%d%R)",
11967 node, "locus", xloc.file, xloc.line);
11969 else if (TYPE_P (node))
11971 tree what = NULL_TREE;
11972 tree decl = TYPE_STUB_DECL (node);
11974 if (TYPE_NAME (node))
11976 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
11977 what = TYPE_NAME (node);
11978 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
11979 && DECL_NAME (TYPE_NAME (node)))
11980 what = DECL_NAME (TYPE_NAME (node));
11983 if (decl)
11985 expanded_location xloc
11986 = expand_location (DECL_SOURCE_LOCATION (decl));
11987 if (what)
11989 if (msg)
11990 warning (OPT_Wdeprecated_declarations,
11991 "%qE is deprecated (declared at %r%s:%d%R): %s",
11992 what, "locus", xloc.file, xloc.line, msg);
11993 else
11994 warning (OPT_Wdeprecated_declarations,
11995 "%qE is deprecated (declared at %r%s:%d%R)",
11996 what, "locus", xloc.file, xloc.line);
11998 else
12000 if (msg)
12001 warning (OPT_Wdeprecated_declarations,
12002 "type is deprecated (declared at %r%s:%d%R): %s",
12003 "locus", xloc.file, xloc.line, msg);
12004 else
12005 warning (OPT_Wdeprecated_declarations,
12006 "type is deprecated (declared at %r%s:%d%R)",
12007 "locus", xloc.file, xloc.line);
12010 else
12012 if (what)
12014 if (msg)
12015 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
12016 what, msg);
12017 else
12018 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
12020 else
12022 if (msg)
12023 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
12024 msg);
12025 else
12026 warning (OPT_Wdeprecated_declarations, "type is deprecated");
12032 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12033 somewhere in it. */
12035 bool
12036 contains_bitfld_component_ref_p (const_tree ref)
12038 while (handled_component_p (ref))
12040 if (TREE_CODE (ref) == COMPONENT_REF
12041 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12042 return true;
12043 ref = TREE_OPERAND (ref, 0);
12046 return false;
12049 #include "gt-tree.h"