2013-06-17 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / tree.c
blob67553b89294d5d2d984f46b76a52e8d89a3a65ad
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);
222 tree global_trees[TI_MAX];
223 tree integer_types[itk_none];
225 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
227 /* Number of operands for each OpenMP clause. */
228 unsigned const char omp_clause_num_ops[] =
230 0, /* OMP_CLAUSE_ERROR */
231 1, /* OMP_CLAUSE_PRIVATE */
232 1, /* OMP_CLAUSE_SHARED */
233 1, /* OMP_CLAUSE_FIRSTPRIVATE */
234 2, /* OMP_CLAUSE_LASTPRIVATE */
235 4, /* OMP_CLAUSE_REDUCTION */
236 1, /* OMP_CLAUSE_COPYIN */
237 1, /* OMP_CLAUSE_COPYPRIVATE */
238 1, /* OMP_CLAUSE_IF */
239 1, /* OMP_CLAUSE_NUM_THREADS */
240 1, /* OMP_CLAUSE_SCHEDULE */
241 0, /* OMP_CLAUSE_NOWAIT */
242 0, /* OMP_CLAUSE_ORDERED */
243 0, /* OMP_CLAUSE_DEFAULT */
244 3, /* OMP_CLAUSE_COLLAPSE */
245 0, /* OMP_CLAUSE_UNTIED */
246 1, /* OMP_CLAUSE_FINAL */
247 0 /* OMP_CLAUSE_MERGEABLE */
250 const char * const omp_clause_code_name[] =
252 "error_clause",
253 "private",
254 "shared",
255 "firstprivate",
256 "lastprivate",
257 "reduction",
258 "copyin",
259 "copyprivate",
260 "if",
261 "num_threads",
262 "schedule",
263 "nowait",
264 "ordered",
265 "default",
266 "collapse",
267 "untied",
268 "final",
269 "mergeable"
273 /* Return the tree node structure used by tree code CODE. */
275 static inline enum tree_node_structure_enum
276 tree_node_structure_for_code (enum tree_code code)
278 switch (TREE_CODE_CLASS (code))
280 case tcc_declaration:
282 switch (code)
284 case FIELD_DECL:
285 return TS_FIELD_DECL;
286 case PARM_DECL:
287 return TS_PARM_DECL;
288 case VAR_DECL:
289 return TS_VAR_DECL;
290 case LABEL_DECL:
291 return TS_LABEL_DECL;
292 case RESULT_DECL:
293 return TS_RESULT_DECL;
294 case DEBUG_EXPR_DECL:
295 return TS_DECL_WRTL;
296 case CONST_DECL:
297 return TS_CONST_DECL;
298 case TYPE_DECL:
299 return TS_TYPE_DECL;
300 case FUNCTION_DECL:
301 return TS_FUNCTION_DECL;
302 case TRANSLATION_UNIT_DECL:
303 return TS_TRANSLATION_UNIT_DECL;
304 default:
305 return TS_DECL_NON_COMMON;
308 case tcc_type:
309 return TS_TYPE_NON_COMMON;
310 case tcc_reference:
311 case tcc_comparison:
312 case tcc_unary:
313 case tcc_binary:
314 case tcc_expression:
315 case tcc_statement:
316 case tcc_vl_exp:
317 return TS_EXP;
318 default: /* tcc_constant and tcc_exceptional */
319 break;
321 switch (code)
323 /* tcc_constant cases. */
324 case INTEGER_CST: return TS_INT_CST;
325 case REAL_CST: return TS_REAL_CST;
326 case FIXED_CST: return TS_FIXED_CST;
327 case COMPLEX_CST: return TS_COMPLEX;
328 case VECTOR_CST: return TS_VECTOR;
329 case STRING_CST: return TS_STRING;
330 /* tcc_exceptional cases. */
331 case ERROR_MARK: return TS_COMMON;
332 case IDENTIFIER_NODE: return TS_IDENTIFIER;
333 case TREE_LIST: return TS_LIST;
334 case TREE_VEC: return TS_VEC;
335 case SSA_NAME: return TS_SSA_NAME;
336 case PLACEHOLDER_EXPR: return TS_COMMON;
337 case STATEMENT_LIST: return TS_STATEMENT_LIST;
338 case BLOCK: return TS_BLOCK;
339 case CONSTRUCTOR: return TS_CONSTRUCTOR;
340 case TREE_BINFO: return TS_BINFO;
341 case OMP_CLAUSE: return TS_OMP_CLAUSE;
342 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
343 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
345 default:
346 gcc_unreachable ();
351 /* Initialize tree_contains_struct to describe the hierarchy of tree
352 nodes. */
354 static void
355 initialize_tree_contains_struct (void)
357 unsigned i;
359 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
361 enum tree_code code;
362 enum tree_node_structure_enum ts_code;
364 code = (enum tree_code) i;
365 ts_code = tree_node_structure_for_code (code);
367 /* Mark the TS structure itself. */
368 tree_contains_struct[code][ts_code] = 1;
370 /* Mark all the structures that TS is derived from. */
371 switch (ts_code)
373 case TS_TYPED:
374 case TS_BLOCK:
375 MARK_TS_BASE (code);
376 break;
378 case TS_COMMON:
379 case TS_INT_CST:
380 case TS_REAL_CST:
381 case TS_FIXED_CST:
382 case TS_VECTOR:
383 case TS_STRING:
384 case TS_COMPLEX:
385 case TS_SSA_NAME:
386 case TS_CONSTRUCTOR:
387 case TS_EXP:
388 case TS_STATEMENT_LIST:
389 MARK_TS_TYPED (code);
390 break;
392 case TS_IDENTIFIER:
393 case TS_DECL_MINIMAL:
394 case TS_TYPE_COMMON:
395 case TS_LIST:
396 case TS_VEC:
397 case TS_BINFO:
398 case TS_OMP_CLAUSE:
399 case TS_OPTIMIZATION:
400 case TS_TARGET_OPTION:
401 MARK_TS_COMMON (code);
402 break;
404 case TS_TYPE_WITH_LANG_SPECIFIC:
405 MARK_TS_TYPE_COMMON (code);
406 break;
408 case TS_TYPE_NON_COMMON:
409 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
410 break;
412 case TS_DECL_COMMON:
413 MARK_TS_DECL_MINIMAL (code);
414 break;
416 case TS_DECL_WRTL:
417 case TS_CONST_DECL:
418 MARK_TS_DECL_COMMON (code);
419 break;
421 case TS_DECL_NON_COMMON:
422 MARK_TS_DECL_WITH_VIS (code);
423 break;
425 case TS_DECL_WITH_VIS:
426 case TS_PARM_DECL:
427 case TS_LABEL_DECL:
428 case TS_RESULT_DECL:
429 MARK_TS_DECL_WRTL (code);
430 break;
432 case TS_FIELD_DECL:
433 MARK_TS_DECL_COMMON (code);
434 break;
436 case TS_VAR_DECL:
437 MARK_TS_DECL_WITH_VIS (code);
438 break;
440 case TS_TYPE_DECL:
441 case TS_FUNCTION_DECL:
442 MARK_TS_DECL_NON_COMMON (code);
443 break;
445 case TS_TRANSLATION_UNIT_DECL:
446 MARK_TS_DECL_COMMON (code);
447 break;
449 default:
450 gcc_unreachable ();
454 /* Basic consistency checks for attributes used in fold. */
455 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
456 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
457 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
458 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
459 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
460 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
461 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
462 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
463 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
464 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
465 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
466 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
467 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
468 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
469 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
470 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
471 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
472 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
473 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
474 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
475 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
476 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
477 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
478 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
479 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
480 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
481 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
482 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
483 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
484 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
485 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
486 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
487 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
488 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
489 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
490 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
491 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
492 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
496 /* Init tree.c. */
498 void
499 init_ttree (void)
501 /* Initialize the hash table of types. */
502 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
503 type_hash_eq, 0);
505 debug_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
506 tree_decl_map_eq, 0);
508 value_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
509 tree_decl_map_eq, 0);
510 init_priority_for_decl = htab_create_ggc (512, tree_priority_map_hash,
511 tree_priority_map_eq, 0);
513 int_cst_hash_table = htab_create_ggc (1024, int_cst_hash_hash,
514 int_cst_hash_eq, NULL);
516 int_cst_node = make_node (INTEGER_CST);
518 cl_option_hash_table = htab_create_ggc (64, cl_option_hash_hash,
519 cl_option_hash_eq, NULL);
521 cl_optimization_node = make_node (OPTIMIZATION_NODE);
522 cl_target_option_node = make_node (TARGET_OPTION_NODE);
524 /* Initialize the tree_contains_struct array. */
525 initialize_tree_contains_struct ();
526 lang_hooks.init_ts ();
530 /* The name of the object as the assembler will see it (but before any
531 translations made by ASM_OUTPUT_LABELREF). Often this is the same
532 as DECL_NAME. It is an IDENTIFIER_NODE. */
533 tree
534 decl_assembler_name (tree decl)
536 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
537 lang_hooks.set_decl_assembler_name (decl);
538 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
541 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
543 bool
544 decl_assembler_name_equal (tree decl, const_tree asmname)
546 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
547 const char *decl_str;
548 const char *asmname_str;
549 bool test = false;
551 if (decl_asmname == asmname)
552 return true;
554 decl_str = IDENTIFIER_POINTER (decl_asmname);
555 asmname_str = IDENTIFIER_POINTER (asmname);
558 /* If the target assembler name was set by the user, things are trickier.
559 We have a leading '*' to begin with. After that, it's arguable what
560 is the correct thing to do with -fleading-underscore. Arguably, we've
561 historically been doing the wrong thing in assemble_alias by always
562 printing the leading underscore. Since we're not changing that, make
563 sure user_label_prefix follows the '*' before matching. */
564 if (decl_str[0] == '*')
566 size_t ulp_len = strlen (user_label_prefix);
568 decl_str ++;
570 if (ulp_len == 0)
571 test = true;
572 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
573 decl_str += ulp_len, test=true;
574 else
575 decl_str --;
577 if (asmname_str[0] == '*')
579 size_t ulp_len = strlen (user_label_prefix);
581 asmname_str ++;
583 if (ulp_len == 0)
584 test = true;
585 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
586 asmname_str += ulp_len, test=true;
587 else
588 asmname_str --;
591 if (!test)
592 return false;
593 return strcmp (decl_str, asmname_str) == 0;
596 /* Hash asmnames ignoring the user specified marks. */
598 hashval_t
599 decl_assembler_name_hash (const_tree asmname)
601 if (IDENTIFIER_POINTER (asmname)[0] == '*')
603 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
604 size_t ulp_len = strlen (user_label_prefix);
606 if (ulp_len == 0)
608 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
609 decl_str += ulp_len;
611 return htab_hash_string (decl_str);
614 return htab_hash_string (IDENTIFIER_POINTER (asmname));
617 /* Compute the number of bytes occupied by a tree with code CODE.
618 This function cannot be used for nodes that have variable sizes,
619 including TREE_VEC, STRING_CST, and CALL_EXPR. */
620 size_t
621 tree_code_size (enum tree_code code)
623 switch (TREE_CODE_CLASS (code))
625 case tcc_declaration: /* A decl node */
627 switch (code)
629 case FIELD_DECL:
630 return sizeof (struct tree_field_decl);
631 case PARM_DECL:
632 return sizeof (struct tree_parm_decl);
633 case VAR_DECL:
634 return sizeof (struct tree_var_decl);
635 case LABEL_DECL:
636 return sizeof (struct tree_label_decl);
637 case RESULT_DECL:
638 return sizeof (struct tree_result_decl);
639 case CONST_DECL:
640 return sizeof (struct tree_const_decl);
641 case TYPE_DECL:
642 return sizeof (struct tree_type_decl);
643 case FUNCTION_DECL:
644 return sizeof (struct tree_function_decl);
645 case DEBUG_EXPR_DECL:
646 return sizeof (struct tree_decl_with_rtl);
647 default:
648 return sizeof (struct tree_decl_non_common);
652 case tcc_type: /* a type node */
653 return sizeof (struct tree_type_non_common);
655 case tcc_reference: /* a reference */
656 case tcc_expression: /* an expression */
657 case tcc_statement: /* an expression with side effects */
658 case tcc_comparison: /* a comparison expression */
659 case tcc_unary: /* a unary arithmetic expression */
660 case tcc_binary: /* a binary arithmetic expression */
661 return (sizeof (struct tree_exp)
662 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
664 case tcc_constant: /* a constant */
665 switch (code)
667 case INTEGER_CST: return sizeof (struct tree_int_cst);
668 case REAL_CST: return sizeof (struct tree_real_cst);
669 case FIXED_CST: return sizeof (struct tree_fixed_cst);
670 case COMPLEX_CST: return sizeof (struct tree_complex);
671 case VECTOR_CST: return sizeof (struct tree_vector);
672 case STRING_CST: gcc_unreachable ();
673 default:
674 return lang_hooks.tree_size (code);
677 case tcc_exceptional: /* something random, like an identifier. */
678 switch (code)
680 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
681 case TREE_LIST: return sizeof (struct tree_list);
683 case ERROR_MARK:
684 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
686 case TREE_VEC:
687 case OMP_CLAUSE: gcc_unreachable ();
689 case SSA_NAME: return sizeof (struct tree_ssa_name);
691 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
692 case BLOCK: return sizeof (struct tree_block);
693 case CONSTRUCTOR: return sizeof (struct tree_constructor);
694 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
695 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
697 default:
698 return lang_hooks.tree_size (code);
701 default:
702 gcc_unreachable ();
706 /* Compute the number of bytes occupied by NODE. This routine only
707 looks at TREE_CODE, except for those nodes that have variable sizes. */
708 size_t
709 tree_size (const_tree node)
711 const enum tree_code code = TREE_CODE (node);
712 switch (code)
714 case TREE_BINFO:
715 return (offsetof (struct tree_binfo, base_binfos)
716 + vec<tree, va_gc>
717 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
719 case TREE_VEC:
720 return (sizeof (struct tree_vec)
721 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
723 case VECTOR_CST:
724 return (sizeof (struct tree_vector)
725 + (TYPE_VECTOR_SUBPARTS (TREE_TYPE (node)) - 1) * sizeof (tree));
727 case STRING_CST:
728 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
730 case OMP_CLAUSE:
731 return (sizeof (struct tree_omp_clause)
732 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
733 * sizeof (tree));
735 default:
736 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
737 return (sizeof (struct tree_exp)
738 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
739 else
740 return tree_code_size (code);
744 /* Record interesting allocation statistics for a tree node with CODE
745 and LENGTH. */
747 static void
748 record_node_allocation_statistics (enum tree_code code ATTRIBUTE_UNUSED,
749 size_t length ATTRIBUTE_UNUSED)
751 enum tree_code_class type = TREE_CODE_CLASS (code);
752 tree_node_kind kind;
754 if (!GATHER_STATISTICS)
755 return;
757 switch (type)
759 case tcc_declaration: /* A decl node */
760 kind = d_kind;
761 break;
763 case tcc_type: /* a type node */
764 kind = t_kind;
765 break;
767 case tcc_statement: /* an expression with side effects */
768 kind = s_kind;
769 break;
771 case tcc_reference: /* a reference */
772 kind = r_kind;
773 break;
775 case tcc_expression: /* an expression */
776 case tcc_comparison: /* a comparison expression */
777 case tcc_unary: /* a unary arithmetic expression */
778 case tcc_binary: /* a binary arithmetic expression */
779 kind = e_kind;
780 break;
782 case tcc_constant: /* a constant */
783 kind = c_kind;
784 break;
786 case tcc_exceptional: /* something random, like an identifier. */
787 switch (code)
789 case IDENTIFIER_NODE:
790 kind = id_kind;
791 break;
793 case TREE_VEC:
794 kind = vec_kind;
795 break;
797 case TREE_BINFO:
798 kind = binfo_kind;
799 break;
801 case SSA_NAME:
802 kind = ssa_name_kind;
803 break;
805 case BLOCK:
806 kind = b_kind;
807 break;
809 case CONSTRUCTOR:
810 kind = constr_kind;
811 break;
813 case OMP_CLAUSE:
814 kind = omp_clause_kind;
815 break;
817 default:
818 kind = x_kind;
819 break;
821 break;
823 case tcc_vl_exp:
824 kind = e_kind;
825 break;
827 default:
828 gcc_unreachable ();
831 tree_code_counts[(int) code]++;
832 tree_node_counts[(int) kind]++;
833 tree_node_sizes[(int) kind] += length;
836 /* Allocate and return a new UID from the DECL_UID namespace. */
839 allocate_decl_uid (void)
841 return next_decl_uid++;
844 /* Return a newly allocated node of code CODE. For decl and type
845 nodes, some other fields are initialized. The rest of the node is
846 initialized to zero. This function cannot be used for TREE_VEC or
847 OMP_CLAUSE nodes, which is enforced by asserts in tree_code_size.
849 Achoo! I got a code in the node. */
851 tree
852 make_node_stat (enum tree_code code MEM_STAT_DECL)
854 tree t;
855 enum tree_code_class type = TREE_CODE_CLASS (code);
856 size_t length = tree_code_size (code);
858 record_node_allocation_statistics (code, length);
860 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
861 TREE_SET_CODE (t, code);
863 switch (type)
865 case tcc_statement:
866 TREE_SIDE_EFFECTS (t) = 1;
867 break;
869 case tcc_declaration:
870 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
872 if (code == FUNCTION_DECL)
874 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
875 DECL_MODE (t) = FUNCTION_MODE;
877 else
878 DECL_ALIGN (t) = 1;
880 DECL_SOURCE_LOCATION (t) = input_location;
881 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
882 DECL_UID (t) = --next_debug_decl_uid;
883 else
885 DECL_UID (t) = allocate_decl_uid ();
886 SET_DECL_PT_UID (t, -1);
888 if (TREE_CODE (t) == LABEL_DECL)
889 LABEL_DECL_UID (t) = -1;
891 break;
893 case tcc_type:
894 TYPE_UID (t) = next_type_uid++;
895 TYPE_ALIGN (t) = BITS_PER_UNIT;
896 TYPE_USER_ALIGN (t) = 0;
897 TYPE_MAIN_VARIANT (t) = t;
898 TYPE_CANONICAL (t) = t;
900 /* Default to no attributes for type, but let target change that. */
901 TYPE_ATTRIBUTES (t) = NULL_TREE;
902 targetm.set_default_type_attributes (t);
904 /* We have not yet computed the alias set for this type. */
905 TYPE_ALIAS_SET (t) = -1;
906 break;
908 case tcc_constant:
909 TREE_CONSTANT (t) = 1;
910 break;
912 case tcc_expression:
913 switch (code)
915 case INIT_EXPR:
916 case MODIFY_EXPR:
917 case VA_ARG_EXPR:
918 case PREDECREMENT_EXPR:
919 case PREINCREMENT_EXPR:
920 case POSTDECREMENT_EXPR:
921 case POSTINCREMENT_EXPR:
922 /* All of these have side-effects, no matter what their
923 operands are. */
924 TREE_SIDE_EFFECTS (t) = 1;
925 break;
927 default:
928 break;
930 break;
932 default:
933 /* Other classes need no special treatment. */
934 break;
937 return t;
940 /* Return a new node with the same contents as NODE except that its
941 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
943 tree
944 copy_node_stat (tree node MEM_STAT_DECL)
946 tree t;
947 enum tree_code code = TREE_CODE (node);
948 size_t length;
950 gcc_assert (code != STATEMENT_LIST);
952 length = tree_size (node);
953 record_node_allocation_statistics (code, length);
954 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
955 memcpy (t, node, length);
957 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
958 TREE_CHAIN (t) = 0;
959 TREE_ASM_WRITTEN (t) = 0;
960 TREE_VISITED (t) = 0;
962 if (TREE_CODE_CLASS (code) == tcc_declaration)
964 if (code == DEBUG_EXPR_DECL)
965 DECL_UID (t) = --next_debug_decl_uid;
966 else
968 DECL_UID (t) = allocate_decl_uid ();
969 if (DECL_PT_UID_SET_P (node))
970 SET_DECL_PT_UID (t, DECL_PT_UID (node));
972 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
973 && DECL_HAS_VALUE_EXPR_P (node))
975 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
976 DECL_HAS_VALUE_EXPR_P (t) = 1;
978 /* DECL_DEBUG_EXPR is copied explicitely by callers. */
979 if (TREE_CODE (node) == VAR_DECL)
980 DECL_HAS_DEBUG_EXPR_P (t) = 0;
981 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
983 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
984 DECL_HAS_INIT_PRIORITY_P (t) = 1;
986 if (TREE_CODE (node) == FUNCTION_DECL)
987 DECL_STRUCT_FUNCTION (t) = NULL;
989 else if (TREE_CODE_CLASS (code) == tcc_type)
991 TYPE_UID (t) = next_type_uid++;
992 /* The following is so that the debug code for
993 the copy is different from the original type.
994 The two statements usually duplicate each other
995 (because they clear fields of the same union),
996 but the optimizer should catch that. */
997 TYPE_SYMTAB_POINTER (t) = 0;
998 TYPE_SYMTAB_ADDRESS (t) = 0;
1000 /* Do not copy the values cache. */
1001 if (TYPE_CACHED_VALUES_P(t))
1003 TYPE_CACHED_VALUES_P (t) = 0;
1004 TYPE_CACHED_VALUES (t) = NULL_TREE;
1008 return t;
1011 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1012 For example, this can copy a list made of TREE_LIST nodes. */
1014 tree
1015 copy_list (tree list)
1017 tree head;
1018 tree prev, next;
1020 if (list == 0)
1021 return 0;
1023 head = prev = copy_node (list);
1024 next = TREE_CHAIN (list);
1025 while (next)
1027 TREE_CHAIN (prev) = copy_node (next);
1028 prev = TREE_CHAIN (prev);
1029 next = TREE_CHAIN (next);
1031 return head;
1035 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1037 tree
1038 build_int_cst (tree type, HOST_WIDE_INT low)
1040 /* Support legacy code. */
1041 if (!type)
1042 type = integer_type_node;
1044 return double_int_to_tree (type, double_int::from_shwi (low));
1047 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1049 tree
1050 build_int_cst_type (tree type, HOST_WIDE_INT low)
1052 gcc_assert (type);
1054 return double_int_to_tree (type, double_int::from_shwi (low));
1057 /* Constructs tree in type TYPE from with value given by CST. Signedness
1058 of CST is assumed to be the same as the signedness of TYPE. */
1060 tree
1061 double_int_to_tree (tree type, double_int cst)
1063 bool sign_extended_type = !TYPE_UNSIGNED (type);
1065 cst = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1067 return build_int_cst_wide (type, cst.low, cst.high);
1070 /* Returns true if CST fits into range of TYPE. Signedness of CST is assumed
1071 to be the same as the signedness of TYPE. */
1073 bool
1074 double_int_fits_to_tree_p (const_tree type, double_int cst)
1076 bool sign_extended_type = !TYPE_UNSIGNED (type);
1078 double_int ext
1079 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1081 return cst == ext;
1084 /* We force the double_int CST to the range of the type TYPE by sign or
1085 zero extending it. OVERFLOWABLE indicates if we are interested in
1086 overflow of the value, when >0 we are only interested in signed
1087 overflow, for <0 we are interested in any overflow. OVERFLOWED
1088 indicates whether overflow has already occurred. CONST_OVERFLOWED
1089 indicates whether constant overflow has already occurred. We force
1090 T's value to be within range of T's type (by setting to 0 or 1 all
1091 the bits outside the type's range). We set TREE_OVERFLOWED if,
1092 OVERFLOWED is nonzero,
1093 or OVERFLOWABLE is >0 and signed overflow occurs
1094 or OVERFLOWABLE is <0 and any overflow occurs
1095 We return a new tree node for the extended double_int. The node
1096 is shared if no overflow flags are set. */
1099 tree
1100 force_fit_type_double (tree type, double_int cst, int overflowable,
1101 bool overflowed)
1103 bool sign_extended_type = !TYPE_UNSIGNED (type);
1105 /* If we need to set overflow flags, return a new unshared node. */
1106 if (overflowed || !double_int_fits_to_tree_p(type, cst))
1108 if (overflowed
1109 || overflowable < 0
1110 || (overflowable > 0 && sign_extended_type))
1112 tree t = make_node (INTEGER_CST);
1113 TREE_INT_CST (t)
1114 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1115 TREE_TYPE (t) = type;
1116 TREE_OVERFLOW (t) = 1;
1117 return t;
1121 /* Else build a shared node. */
1122 return double_int_to_tree (type, cst);
1125 /* These are the hash table functions for the hash table of INTEGER_CST
1126 nodes of a sizetype. */
1128 /* Return the hash code code X, an INTEGER_CST. */
1130 static hashval_t
1131 int_cst_hash_hash (const void *x)
1133 const_tree const t = (const_tree) x;
1135 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1136 ^ htab_hash_pointer (TREE_TYPE (t)));
1139 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1140 is the same as that given by *Y, which is the same. */
1142 static int
1143 int_cst_hash_eq (const void *x, const void *y)
1145 const_tree const xt = (const_tree) x;
1146 const_tree const yt = (const_tree) y;
1148 return (TREE_TYPE (xt) == TREE_TYPE (yt)
1149 && TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1150 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt));
1153 /* Create an INT_CST node of TYPE and value HI:LOW.
1154 The returned node is always shared. For small integers we use a
1155 per-type vector cache, for larger ones we use a single hash table. */
1157 tree
1158 build_int_cst_wide (tree type, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
1160 tree t;
1161 int ix = -1;
1162 int limit = 0;
1164 gcc_assert (type);
1166 switch (TREE_CODE (type))
1168 case NULLPTR_TYPE:
1169 gcc_assert (hi == 0 && low == 0);
1170 /* Fallthru. */
1172 case POINTER_TYPE:
1173 case REFERENCE_TYPE:
1174 /* Cache NULL pointer. */
1175 if (!hi && !low)
1177 limit = 1;
1178 ix = 0;
1180 break;
1182 case BOOLEAN_TYPE:
1183 /* Cache false or true. */
1184 limit = 2;
1185 if (!hi && low < 2)
1186 ix = low;
1187 break;
1189 case INTEGER_TYPE:
1190 case OFFSET_TYPE:
1191 if (TYPE_UNSIGNED (type))
1193 /* Cache 0..N */
1194 limit = INTEGER_SHARE_LIMIT;
1195 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1196 ix = low;
1198 else
1200 /* Cache -1..N */
1201 limit = INTEGER_SHARE_LIMIT + 1;
1202 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1203 ix = low + 1;
1204 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
1205 ix = 0;
1207 break;
1209 case ENUMERAL_TYPE:
1210 break;
1212 default:
1213 gcc_unreachable ();
1216 if (ix >= 0)
1218 /* Look for it in the type's vector of small shared ints. */
1219 if (!TYPE_CACHED_VALUES_P (type))
1221 TYPE_CACHED_VALUES_P (type) = 1;
1222 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1225 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1226 if (t)
1228 /* Make sure no one is clobbering the shared constant. */
1229 gcc_assert (TREE_TYPE (t) == type);
1230 gcc_assert (TREE_INT_CST_LOW (t) == low);
1231 gcc_assert (TREE_INT_CST_HIGH (t) == hi);
1233 else
1235 /* Create a new shared int. */
1236 t = make_node (INTEGER_CST);
1238 TREE_INT_CST_LOW (t) = low;
1239 TREE_INT_CST_HIGH (t) = hi;
1240 TREE_TYPE (t) = type;
1242 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1245 else
1247 /* Use the cache of larger shared ints. */
1248 void **slot;
1250 TREE_INT_CST_LOW (int_cst_node) = low;
1251 TREE_INT_CST_HIGH (int_cst_node) = hi;
1252 TREE_TYPE (int_cst_node) = type;
1254 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
1255 t = (tree) *slot;
1256 if (!t)
1258 /* Insert this one into the hash table. */
1259 t = int_cst_node;
1260 *slot = t;
1261 /* Make a new node for next time round. */
1262 int_cst_node = make_node (INTEGER_CST);
1266 return t;
1269 void
1270 cache_integer_cst (tree t)
1272 tree type = TREE_TYPE (t);
1273 HOST_WIDE_INT hi = TREE_INT_CST_HIGH (t);
1274 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (t);
1275 int ix = -1;
1276 int limit = 0;
1278 gcc_assert (!TREE_OVERFLOW (t));
1280 switch (TREE_CODE (type))
1282 case NULLPTR_TYPE:
1283 gcc_assert (hi == 0 && low == 0);
1284 /* Fallthru. */
1286 case POINTER_TYPE:
1287 case REFERENCE_TYPE:
1288 /* Cache NULL pointer. */
1289 if (!hi && !low)
1291 limit = 1;
1292 ix = 0;
1294 break;
1296 case BOOLEAN_TYPE:
1297 /* Cache false or true. */
1298 limit = 2;
1299 if (!hi && low < 2)
1300 ix = low;
1301 break;
1303 case INTEGER_TYPE:
1304 case OFFSET_TYPE:
1305 if (TYPE_UNSIGNED (type))
1307 /* Cache 0..N */
1308 limit = INTEGER_SHARE_LIMIT;
1309 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1310 ix = low;
1312 else
1314 /* Cache -1..N */
1315 limit = INTEGER_SHARE_LIMIT + 1;
1316 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1317 ix = low + 1;
1318 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
1319 ix = 0;
1321 break;
1323 case ENUMERAL_TYPE:
1324 break;
1326 default:
1327 gcc_unreachable ();
1330 if (ix >= 0)
1332 /* Look for it in the type's vector of small shared ints. */
1333 if (!TYPE_CACHED_VALUES_P (type))
1335 TYPE_CACHED_VALUES_P (type) = 1;
1336 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1339 gcc_assert (TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) == NULL_TREE);
1340 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1342 else
1344 /* Use the cache of larger shared ints. */
1345 void **slot;
1347 slot = htab_find_slot (int_cst_hash_table, t, INSERT);
1348 /* If there is already an entry for the number verify it's the
1349 same. */
1350 if (*slot)
1352 gcc_assert (TREE_INT_CST_LOW ((tree)*slot) == low
1353 && TREE_INT_CST_HIGH ((tree)*slot) == hi);
1354 return;
1356 /* Otherwise insert this one into the hash table. */
1357 *slot = t;
1362 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1363 and the rest are zeros. */
1365 tree
1366 build_low_bits_mask (tree type, unsigned bits)
1368 double_int mask;
1370 gcc_assert (bits <= TYPE_PRECISION (type));
1372 if (bits == TYPE_PRECISION (type)
1373 && !TYPE_UNSIGNED (type))
1374 /* Sign extended all-ones mask. */
1375 mask = double_int_minus_one;
1376 else
1377 mask = double_int::mask (bits);
1379 return build_int_cst_wide (type, mask.low, mask.high);
1382 /* Checks that X is integer constant that can be expressed in (unsigned)
1383 HOST_WIDE_INT without loss of precision. */
1385 bool
1386 cst_and_fits_in_hwi (const_tree x)
1388 if (TREE_CODE (x) != INTEGER_CST)
1389 return false;
1391 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1392 return false;
1394 return (TREE_INT_CST_HIGH (x) == 0
1395 || TREE_INT_CST_HIGH (x) == -1);
1398 /* Build a newly constructed TREE_VEC node of length LEN. */
1400 tree
1401 make_vector_stat (unsigned len MEM_STAT_DECL)
1403 tree t;
1404 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1406 record_node_allocation_statistics (VECTOR_CST, length);
1408 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1410 TREE_SET_CODE (t, VECTOR_CST);
1411 TREE_CONSTANT (t) = 1;
1413 return t;
1416 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1417 are in a list pointed to by VALS. */
1419 tree
1420 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1422 int over = 0;
1423 unsigned cnt = 0;
1424 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1425 TREE_TYPE (v) = type;
1427 /* Iterate through elements and check for overflow. */
1428 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1430 tree value = vals[cnt];
1432 VECTOR_CST_ELT (v, cnt) = value;
1434 /* Don't crash if we get an address constant. */
1435 if (!CONSTANT_CLASS_P (value))
1436 continue;
1438 over |= TREE_OVERFLOW (value);
1441 TREE_OVERFLOW (v) = over;
1442 return v;
1445 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1446 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1448 tree
1449 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1451 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1452 unsigned HOST_WIDE_INT idx;
1453 tree value;
1455 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1456 vec[idx] = value;
1457 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1458 vec[idx] = build_zero_cst (TREE_TYPE (type));
1460 return build_vector (type, vec);
1463 /* Build a vector of type VECTYPE where all the elements are SCs. */
1464 tree
1465 build_vector_from_val (tree vectype, tree sc)
1467 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1469 if (sc == error_mark_node)
1470 return sc;
1472 /* Verify that the vector type is suitable for SC. Note that there
1473 is some inconsistency in the type-system with respect to restrict
1474 qualifications of pointers. Vector types always have a main-variant
1475 element type and the qualification is applied to the vector-type.
1476 So TREE_TYPE (vector-type) does not return a properly qualified
1477 vector element-type. */
1478 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1479 TREE_TYPE (vectype)));
1481 if (CONSTANT_CLASS_P (sc))
1483 tree *v = XALLOCAVEC (tree, nunits);
1484 for (i = 0; i < nunits; ++i)
1485 v[i] = sc;
1486 return build_vector (vectype, v);
1488 else
1490 vec<constructor_elt, va_gc> *v;
1491 vec_alloc (v, nunits);
1492 for (i = 0; i < nunits; ++i)
1493 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1494 return build_constructor (vectype, v);
1498 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1499 are in the vec pointed to by VALS. */
1500 tree
1501 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1503 tree c = make_node (CONSTRUCTOR);
1504 unsigned int i;
1505 constructor_elt *elt;
1506 bool constant_p = true;
1507 bool side_effects_p = false;
1509 TREE_TYPE (c) = type;
1510 CONSTRUCTOR_ELTS (c) = vals;
1512 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1514 /* Mostly ctors will have elts that don't have side-effects, so
1515 the usual case is to scan all the elements. Hence a single
1516 loop for both const and side effects, rather than one loop
1517 each (with early outs). */
1518 if (!TREE_CONSTANT (elt->value))
1519 constant_p = false;
1520 if (TREE_SIDE_EFFECTS (elt->value))
1521 side_effects_p = true;
1524 TREE_SIDE_EFFECTS (c) = side_effects_p;
1525 TREE_CONSTANT (c) = constant_p;
1527 return c;
1530 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1531 INDEX and VALUE. */
1532 tree
1533 build_constructor_single (tree type, tree index, tree value)
1535 vec<constructor_elt, va_gc> *v;
1536 constructor_elt elt = {index, value};
1538 vec_alloc (v, 1);
1539 v->quick_push (elt);
1541 return build_constructor (type, v);
1545 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1546 are in a list pointed to by VALS. */
1547 tree
1548 build_constructor_from_list (tree type, tree vals)
1550 tree t;
1551 vec<constructor_elt, va_gc> *v = NULL;
1553 if (vals)
1555 vec_alloc (v, list_length (vals));
1556 for (t = vals; t; t = TREE_CHAIN (t))
1557 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1560 return build_constructor (type, v);
1563 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
1564 of elements, provided as index/value pairs. */
1566 tree
1567 build_constructor_va (tree type, int nelts, ...)
1569 vec<constructor_elt, va_gc> *v = NULL;
1570 va_list p;
1572 va_start (p, nelts);
1573 vec_alloc (v, nelts);
1574 while (nelts--)
1576 tree index = va_arg (p, tree);
1577 tree value = va_arg (p, tree);
1578 CONSTRUCTOR_APPEND_ELT (v, index, value);
1580 va_end (p);
1581 return build_constructor (type, v);
1584 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1586 tree
1587 build_fixed (tree type, FIXED_VALUE_TYPE f)
1589 tree v;
1590 FIXED_VALUE_TYPE *fp;
1592 v = make_node (FIXED_CST);
1593 fp = ggc_alloc_fixed_value ();
1594 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1596 TREE_TYPE (v) = type;
1597 TREE_FIXED_CST_PTR (v) = fp;
1598 return v;
1601 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1603 tree
1604 build_real (tree type, REAL_VALUE_TYPE d)
1606 tree v;
1607 REAL_VALUE_TYPE *dp;
1608 int overflow = 0;
1610 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1611 Consider doing it via real_convert now. */
1613 v = make_node (REAL_CST);
1614 dp = ggc_alloc_real_value ();
1615 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1617 TREE_TYPE (v) = type;
1618 TREE_REAL_CST_PTR (v) = dp;
1619 TREE_OVERFLOW (v) = overflow;
1620 return v;
1623 /* Return a new REAL_CST node whose type is TYPE
1624 and whose value is the integer value of the INTEGER_CST node I. */
1626 REAL_VALUE_TYPE
1627 real_value_from_int_cst (const_tree type, const_tree i)
1629 REAL_VALUE_TYPE d;
1631 /* Clear all bits of the real value type so that we can later do
1632 bitwise comparisons to see if two values are the same. */
1633 memset (&d, 0, sizeof d);
1635 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
1636 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
1637 TYPE_UNSIGNED (TREE_TYPE (i)));
1638 return d;
1641 /* Given a tree representing an integer constant I, return a tree
1642 representing the same value as a floating-point constant of type TYPE. */
1644 tree
1645 build_real_from_int_cst (tree type, const_tree i)
1647 tree v;
1648 int overflow = TREE_OVERFLOW (i);
1650 v = build_real (type, real_value_from_int_cst (type, i));
1652 TREE_OVERFLOW (v) |= overflow;
1653 return v;
1656 /* Return a newly constructed STRING_CST node whose value is
1657 the LEN characters at STR.
1658 Note that for a C string literal, LEN should include the trailing NUL.
1659 The TREE_TYPE is not initialized. */
1661 tree
1662 build_string (int len, const char *str)
1664 tree s;
1665 size_t length;
1667 /* Do not waste bytes provided by padding of struct tree_string. */
1668 length = len + offsetof (struct tree_string, str) + 1;
1670 record_node_allocation_statistics (STRING_CST, length);
1672 s = ggc_alloc_tree_node (length);
1674 memset (s, 0, sizeof (struct tree_typed));
1675 TREE_SET_CODE (s, STRING_CST);
1676 TREE_CONSTANT (s) = 1;
1677 TREE_STRING_LENGTH (s) = len;
1678 memcpy (s->string.str, str, len);
1679 s->string.str[len] = '\0';
1681 return s;
1684 /* Return a newly constructed COMPLEX_CST node whose value is
1685 specified by the real and imaginary parts REAL and IMAG.
1686 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1687 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1689 tree
1690 build_complex (tree type, tree real, tree imag)
1692 tree t = make_node (COMPLEX_CST);
1694 TREE_REALPART (t) = real;
1695 TREE_IMAGPART (t) = imag;
1696 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1697 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1698 return t;
1701 /* Return a constant of arithmetic type TYPE which is the
1702 multiplicative identity of the set TYPE. */
1704 tree
1705 build_one_cst (tree type)
1707 switch (TREE_CODE (type))
1709 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1710 case POINTER_TYPE: case REFERENCE_TYPE:
1711 case OFFSET_TYPE:
1712 return build_int_cst (type, 1);
1714 case REAL_TYPE:
1715 return build_real (type, dconst1);
1717 case FIXED_POINT_TYPE:
1718 /* We can only generate 1 for accum types. */
1719 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1720 return build_fixed (type, FCONST1(TYPE_MODE (type)));
1722 case VECTOR_TYPE:
1724 tree scalar = build_one_cst (TREE_TYPE (type));
1726 return build_vector_from_val (type, scalar);
1729 case COMPLEX_TYPE:
1730 return build_complex (type,
1731 build_one_cst (TREE_TYPE (type)),
1732 build_zero_cst (TREE_TYPE (type)));
1734 default:
1735 gcc_unreachable ();
1739 /* Return an integer of type TYPE containing all 1's in as much precision as
1740 it contains, or a complex or vector whose subparts are such integers. */
1742 tree
1743 build_all_ones_cst (tree type)
1745 if (TREE_CODE (type) == COMPLEX_TYPE)
1747 tree scalar = build_all_ones_cst (TREE_TYPE (type));
1748 return build_complex (type, scalar, scalar);
1750 else
1751 return build_minus_one_cst (type);
1754 /* Return a constant of arithmetic type TYPE which is the
1755 opposite of the multiplicative identity of the set TYPE. */
1757 tree
1758 build_minus_one_cst (tree type)
1760 switch (TREE_CODE (type))
1762 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1763 case POINTER_TYPE: case REFERENCE_TYPE:
1764 case OFFSET_TYPE:
1765 return build_int_cst (type, -1);
1767 case REAL_TYPE:
1768 return build_real (type, dconstm1);
1770 case FIXED_POINT_TYPE:
1771 /* We can only generate 1 for accum types. */
1772 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1773 return build_fixed (type, fixed_from_double_int (double_int_minus_one,
1774 TYPE_MODE (type)));
1776 case VECTOR_TYPE:
1778 tree scalar = build_minus_one_cst (TREE_TYPE (type));
1780 return build_vector_from_val (type, scalar);
1783 case COMPLEX_TYPE:
1784 return build_complex (type,
1785 build_minus_one_cst (TREE_TYPE (type)),
1786 build_zero_cst (TREE_TYPE (type)));
1788 default:
1789 gcc_unreachable ();
1793 /* Build 0 constant of type TYPE. This is used by constructor folding
1794 and thus the constant should be represented in memory by
1795 zero(es). */
1797 tree
1798 build_zero_cst (tree type)
1800 switch (TREE_CODE (type))
1802 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1803 case POINTER_TYPE: case REFERENCE_TYPE:
1804 case OFFSET_TYPE: case NULLPTR_TYPE:
1805 return build_int_cst (type, 0);
1807 case REAL_TYPE:
1808 return build_real (type, dconst0);
1810 case FIXED_POINT_TYPE:
1811 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
1813 case VECTOR_TYPE:
1815 tree scalar = build_zero_cst (TREE_TYPE (type));
1817 return build_vector_from_val (type, scalar);
1820 case COMPLEX_TYPE:
1822 tree zero = build_zero_cst (TREE_TYPE (type));
1824 return build_complex (type, zero, zero);
1827 default:
1828 if (!AGGREGATE_TYPE_P (type))
1829 return fold_convert (type, integer_zero_node);
1830 return build_constructor (type, NULL);
1835 /* Build a BINFO with LEN language slots. */
1837 tree
1838 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1840 tree t;
1841 size_t length = (offsetof (struct tree_binfo, base_binfos)
1842 + vec<tree, va_gc>::embedded_size (base_binfos));
1844 record_node_allocation_statistics (TREE_BINFO, length);
1846 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1848 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1850 TREE_SET_CODE (t, TREE_BINFO);
1852 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
1854 return t;
1857 /* Create a CASE_LABEL_EXPR tree node and return it. */
1859 tree
1860 build_case_label (tree low_value, tree high_value, tree label_decl)
1862 tree t = make_node (CASE_LABEL_EXPR);
1864 TREE_TYPE (t) = void_type_node;
1865 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
1867 CASE_LOW (t) = low_value;
1868 CASE_HIGH (t) = high_value;
1869 CASE_LABEL (t) = label_decl;
1870 CASE_CHAIN (t) = NULL_TREE;
1872 return t;
1875 /* Build a newly constructed TREE_VEC node of length LEN. */
1877 tree
1878 make_tree_vec_stat (int len MEM_STAT_DECL)
1880 tree t;
1881 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1883 record_node_allocation_statistics (TREE_VEC, length);
1885 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1887 TREE_SET_CODE (t, TREE_VEC);
1888 TREE_VEC_LENGTH (t) = len;
1890 return t;
1893 /* Return 1 if EXPR is the integer constant zero or a complex constant
1894 of zero. */
1897 integer_zerop (const_tree expr)
1899 STRIP_NOPS (expr);
1901 switch (TREE_CODE (expr))
1903 case INTEGER_CST:
1904 return (TREE_INT_CST_LOW (expr) == 0
1905 && TREE_INT_CST_HIGH (expr) == 0);
1906 case COMPLEX_CST:
1907 return (integer_zerop (TREE_REALPART (expr))
1908 && integer_zerop (TREE_IMAGPART (expr)));
1909 case VECTOR_CST:
1911 unsigned i;
1912 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1913 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
1914 return false;
1915 return true;
1917 default:
1918 return false;
1922 /* Return 1 if EXPR is the integer constant one or the corresponding
1923 complex constant. */
1926 integer_onep (const_tree expr)
1928 STRIP_NOPS (expr);
1930 switch (TREE_CODE (expr))
1932 case INTEGER_CST:
1933 return (TREE_INT_CST_LOW (expr) == 1
1934 && TREE_INT_CST_HIGH (expr) == 0);
1935 case COMPLEX_CST:
1936 return (integer_onep (TREE_REALPART (expr))
1937 && integer_zerop (TREE_IMAGPART (expr)));
1938 case VECTOR_CST:
1940 unsigned i;
1941 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1942 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
1943 return false;
1944 return true;
1946 default:
1947 return false;
1951 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1952 it contains, or a complex or vector whose subparts are such integers. */
1955 integer_all_onesp (const_tree expr)
1957 int prec;
1958 int uns;
1960 STRIP_NOPS (expr);
1962 if (TREE_CODE (expr) == COMPLEX_CST
1963 && integer_all_onesp (TREE_REALPART (expr))
1964 && integer_all_onesp (TREE_IMAGPART (expr)))
1965 return 1;
1967 else if (TREE_CODE (expr) == VECTOR_CST)
1969 unsigned i;
1970 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1971 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
1972 return 0;
1973 return 1;
1976 else if (TREE_CODE (expr) != INTEGER_CST)
1977 return 0;
1979 uns = TYPE_UNSIGNED (TREE_TYPE (expr));
1980 if (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1981 && TREE_INT_CST_HIGH (expr) == -1)
1982 return 1;
1983 if (!uns)
1984 return 0;
1986 prec = TYPE_PRECISION (TREE_TYPE (expr));
1987 if (prec >= HOST_BITS_PER_WIDE_INT)
1989 HOST_WIDE_INT high_value;
1990 int shift_amount;
1992 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1994 /* Can not handle precisions greater than twice the host int size. */
1995 gcc_assert (shift_amount <= HOST_BITS_PER_WIDE_INT);
1996 if (shift_amount == HOST_BITS_PER_WIDE_INT)
1997 /* Shifting by the host word size is undefined according to the ANSI
1998 standard, so we must handle this as a special case. */
1999 high_value = -1;
2000 else
2001 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
2003 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
2004 && TREE_INT_CST_HIGH (expr) == high_value);
2006 else
2007 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
2010 /* Return 1 if EXPR is the integer constant minus one. */
2013 integer_minus_onep (const_tree expr)
2015 STRIP_NOPS (expr);
2017 if (TREE_CODE (expr) == COMPLEX_CST)
2018 return (integer_all_onesp (TREE_REALPART (expr))
2019 && integer_zerop (TREE_IMAGPART (expr)));
2020 else
2021 return integer_all_onesp (expr);
2024 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
2025 one bit on). */
2028 integer_pow2p (const_tree expr)
2030 int prec;
2031 unsigned HOST_WIDE_INT high, low;
2033 STRIP_NOPS (expr);
2035 if (TREE_CODE (expr) == COMPLEX_CST
2036 && integer_pow2p (TREE_REALPART (expr))
2037 && integer_zerop (TREE_IMAGPART (expr)))
2038 return 1;
2040 if (TREE_CODE (expr) != INTEGER_CST)
2041 return 0;
2043 prec = TYPE_PRECISION (TREE_TYPE (expr));
2044 high = TREE_INT_CST_HIGH (expr);
2045 low = TREE_INT_CST_LOW (expr);
2047 /* First clear all bits that are beyond the type's precision in case
2048 we've been sign extended. */
2050 if (prec == HOST_BITS_PER_DOUBLE_INT)
2052 else if (prec > HOST_BITS_PER_WIDE_INT)
2053 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
2054 else
2056 high = 0;
2057 if (prec < HOST_BITS_PER_WIDE_INT)
2058 low &= ~((HOST_WIDE_INT) (-1) << prec);
2061 if (high == 0 && low == 0)
2062 return 0;
2064 return ((high == 0 && (low & (low - 1)) == 0)
2065 || (low == 0 && (high & (high - 1)) == 0));
2068 /* Return 1 if EXPR is an integer constant other than zero or a
2069 complex constant other than zero. */
2072 integer_nonzerop (const_tree expr)
2074 STRIP_NOPS (expr);
2076 return ((TREE_CODE (expr) == INTEGER_CST
2077 && (TREE_INT_CST_LOW (expr) != 0
2078 || TREE_INT_CST_HIGH (expr) != 0))
2079 || (TREE_CODE (expr) == COMPLEX_CST
2080 && (integer_nonzerop (TREE_REALPART (expr))
2081 || integer_nonzerop (TREE_IMAGPART (expr)))));
2084 /* Return 1 if EXPR is the fixed-point constant zero. */
2087 fixed_zerop (const_tree expr)
2089 return (TREE_CODE (expr) == FIXED_CST
2090 && TREE_FIXED_CST (expr).data.is_zero ());
2093 /* Return the power of two represented by a tree node known to be a
2094 power of two. */
2097 tree_log2 (const_tree expr)
2099 int prec;
2100 HOST_WIDE_INT high, low;
2102 STRIP_NOPS (expr);
2104 if (TREE_CODE (expr) == COMPLEX_CST)
2105 return tree_log2 (TREE_REALPART (expr));
2107 prec = TYPE_PRECISION (TREE_TYPE (expr));
2108 high = TREE_INT_CST_HIGH (expr);
2109 low = TREE_INT_CST_LOW (expr);
2111 /* First clear all bits that are beyond the type's precision in case
2112 we've been sign extended. */
2114 if (prec == HOST_BITS_PER_DOUBLE_INT)
2116 else if (prec > HOST_BITS_PER_WIDE_INT)
2117 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
2118 else
2120 high = 0;
2121 if (prec < HOST_BITS_PER_WIDE_INT)
2122 low &= ~((HOST_WIDE_INT) (-1) << prec);
2125 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
2126 : exact_log2 (low));
2129 /* Similar, but return the largest integer Y such that 2 ** Y is less
2130 than or equal to EXPR. */
2133 tree_floor_log2 (const_tree expr)
2135 int prec;
2136 HOST_WIDE_INT high, low;
2138 STRIP_NOPS (expr);
2140 if (TREE_CODE (expr) == COMPLEX_CST)
2141 return tree_log2 (TREE_REALPART (expr));
2143 prec = TYPE_PRECISION (TREE_TYPE (expr));
2144 high = TREE_INT_CST_HIGH (expr);
2145 low = TREE_INT_CST_LOW (expr);
2147 /* First clear all bits that are beyond the type's precision in case
2148 we've been sign extended. Ignore if type's precision hasn't been set
2149 since what we are doing is setting it. */
2151 if (prec == HOST_BITS_PER_DOUBLE_INT || prec == 0)
2153 else if (prec > HOST_BITS_PER_WIDE_INT)
2154 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
2155 else
2157 high = 0;
2158 if (prec < HOST_BITS_PER_WIDE_INT)
2159 low &= ~((HOST_WIDE_INT) (-1) << prec);
2162 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
2163 : floor_log2 (low));
2166 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
2167 decimal float constants, so don't return 1 for them. */
2170 real_zerop (const_tree expr)
2172 STRIP_NOPS (expr);
2174 switch (TREE_CODE (expr))
2176 case REAL_CST:
2177 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
2178 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2179 case COMPLEX_CST:
2180 return real_zerop (TREE_REALPART (expr))
2181 && real_zerop (TREE_IMAGPART (expr));
2182 case VECTOR_CST:
2184 unsigned i;
2185 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2186 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2187 return false;
2188 return true;
2190 default:
2191 return false;
2195 /* Return 1 if EXPR is the real constant one in real or complex form.
2196 Trailing zeroes matter for decimal float constants, so don't return
2197 1 for them. */
2200 real_onep (const_tree expr)
2202 STRIP_NOPS (expr);
2204 switch (TREE_CODE (expr))
2206 case REAL_CST:
2207 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
2208 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2209 case COMPLEX_CST:
2210 return real_onep (TREE_REALPART (expr))
2211 && real_zerop (TREE_IMAGPART (expr));
2212 case VECTOR_CST:
2214 unsigned i;
2215 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2216 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2217 return false;
2218 return true;
2220 default:
2221 return false;
2225 /* Return 1 if EXPR is the real constant two. Trailing zeroes matter
2226 for decimal float constants, so don't return 1 for them. */
2229 real_twop (const_tree expr)
2231 STRIP_NOPS (expr);
2233 switch (TREE_CODE (expr))
2235 case REAL_CST:
2236 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2)
2237 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2238 case COMPLEX_CST:
2239 return real_twop (TREE_REALPART (expr))
2240 && real_zerop (TREE_IMAGPART (expr));
2241 case VECTOR_CST:
2243 unsigned i;
2244 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2245 if (!real_twop (VECTOR_CST_ELT (expr, i)))
2246 return false;
2247 return true;
2249 default:
2250 return false;
2254 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2255 matter for decimal float constants, so don't return 1 for them. */
2258 real_minus_onep (const_tree expr)
2260 STRIP_NOPS (expr);
2262 switch (TREE_CODE (expr))
2264 case REAL_CST:
2265 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2266 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2267 case COMPLEX_CST:
2268 return real_minus_onep (TREE_REALPART (expr))
2269 && real_zerop (TREE_IMAGPART (expr));
2270 case VECTOR_CST:
2272 unsigned i;
2273 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2274 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2275 return false;
2276 return true;
2278 default:
2279 return false;
2283 /* Nonzero if EXP is a constant or a cast of a constant. */
2286 really_constant_p (const_tree exp)
2288 /* This is not quite the same as STRIP_NOPS. It does more. */
2289 while (CONVERT_EXPR_P (exp)
2290 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2291 exp = TREE_OPERAND (exp, 0);
2292 return TREE_CONSTANT (exp);
2295 /* Return first list element whose TREE_VALUE is ELEM.
2296 Return 0 if ELEM is not in LIST. */
2298 tree
2299 value_member (tree elem, tree list)
2301 while (list)
2303 if (elem == TREE_VALUE (list))
2304 return list;
2305 list = TREE_CHAIN (list);
2307 return NULL_TREE;
2310 /* Return first list element whose TREE_PURPOSE is ELEM.
2311 Return 0 if ELEM is not in LIST. */
2313 tree
2314 purpose_member (const_tree elem, tree list)
2316 while (list)
2318 if (elem == TREE_PURPOSE (list))
2319 return list;
2320 list = TREE_CHAIN (list);
2322 return NULL_TREE;
2325 /* Return true if ELEM is in V. */
2327 bool
2328 vec_member (const_tree elem, vec<tree, va_gc> *v)
2330 unsigned ix;
2331 tree t;
2332 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2333 if (elem == t)
2334 return true;
2335 return false;
2338 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2339 NULL_TREE. */
2341 tree
2342 chain_index (int idx, tree chain)
2344 for (; chain && idx > 0; --idx)
2345 chain = TREE_CHAIN (chain);
2346 return chain;
2349 /* Return nonzero if ELEM is part of the chain CHAIN. */
2352 chain_member (const_tree elem, const_tree chain)
2354 while (chain)
2356 if (elem == chain)
2357 return 1;
2358 chain = DECL_CHAIN (chain);
2361 return 0;
2364 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2365 We expect a null pointer to mark the end of the chain.
2366 This is the Lisp primitive `length'. */
2369 list_length (const_tree t)
2371 const_tree p = t;
2372 #ifdef ENABLE_TREE_CHECKING
2373 const_tree q = t;
2374 #endif
2375 int len = 0;
2377 while (p)
2379 p = TREE_CHAIN (p);
2380 #ifdef ENABLE_TREE_CHECKING
2381 if (len % 2)
2382 q = TREE_CHAIN (q);
2383 gcc_assert (p != q);
2384 #endif
2385 len++;
2388 return len;
2391 /* Returns the number of FIELD_DECLs in TYPE. */
2394 fields_length (const_tree type)
2396 tree t = TYPE_FIELDS (type);
2397 int count = 0;
2399 for (; t; t = DECL_CHAIN (t))
2400 if (TREE_CODE (t) == FIELD_DECL)
2401 ++count;
2403 return count;
2406 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2407 UNION_TYPE TYPE, or NULL_TREE if none. */
2409 tree
2410 first_field (const_tree type)
2412 tree t = TYPE_FIELDS (type);
2413 while (t && TREE_CODE (t) != FIELD_DECL)
2414 t = TREE_CHAIN (t);
2415 return t;
2418 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2419 by modifying the last node in chain 1 to point to chain 2.
2420 This is the Lisp primitive `nconc'. */
2422 tree
2423 chainon (tree op1, tree op2)
2425 tree t1;
2427 if (!op1)
2428 return op2;
2429 if (!op2)
2430 return op1;
2432 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2433 continue;
2434 TREE_CHAIN (t1) = op2;
2436 #ifdef ENABLE_TREE_CHECKING
2438 tree t2;
2439 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2440 gcc_assert (t2 != t1);
2442 #endif
2444 return op1;
2447 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2449 tree
2450 tree_last (tree chain)
2452 tree next;
2453 if (chain)
2454 while ((next = TREE_CHAIN (chain)))
2455 chain = next;
2456 return chain;
2459 /* Reverse the order of elements in the chain T,
2460 and return the new head of the chain (old last element). */
2462 tree
2463 nreverse (tree t)
2465 tree prev = 0, decl, next;
2466 for (decl = t; decl; decl = next)
2468 /* We shouldn't be using this function to reverse BLOCK chains; we
2469 have blocks_nreverse for that. */
2470 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2471 next = TREE_CHAIN (decl);
2472 TREE_CHAIN (decl) = prev;
2473 prev = decl;
2475 return prev;
2478 /* Return a newly created TREE_LIST node whose
2479 purpose and value fields are PARM and VALUE. */
2481 tree
2482 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2484 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2485 TREE_PURPOSE (t) = parm;
2486 TREE_VALUE (t) = value;
2487 return t;
2490 /* Build a chain of TREE_LIST nodes from a vector. */
2492 tree
2493 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2495 tree ret = NULL_TREE;
2496 tree *pp = &ret;
2497 unsigned int i;
2498 tree t;
2499 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2501 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2502 pp = &TREE_CHAIN (*pp);
2504 return ret;
2507 /* Return a newly created TREE_LIST node whose
2508 purpose and value fields are PURPOSE and VALUE
2509 and whose TREE_CHAIN is CHAIN. */
2511 tree
2512 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2514 tree node;
2516 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
2517 memset (node, 0, sizeof (struct tree_common));
2519 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2521 TREE_SET_CODE (node, TREE_LIST);
2522 TREE_CHAIN (node) = chain;
2523 TREE_PURPOSE (node) = purpose;
2524 TREE_VALUE (node) = value;
2525 return node;
2528 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2529 trees. */
2531 vec<tree, va_gc> *
2532 ctor_to_vec (tree ctor)
2534 vec<tree, va_gc> *vec;
2535 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2536 unsigned int ix;
2537 tree val;
2539 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2540 vec->quick_push (val);
2542 return vec;
2545 /* Return the size nominally occupied by an object of type TYPE
2546 when it resides in memory. The value is measured in units of bytes,
2547 and its data type is that normally used for type sizes
2548 (which is the first type created by make_signed_type or
2549 make_unsigned_type). */
2551 tree
2552 size_in_bytes (const_tree type)
2554 tree t;
2556 if (type == error_mark_node)
2557 return integer_zero_node;
2559 type = TYPE_MAIN_VARIANT (type);
2560 t = TYPE_SIZE_UNIT (type);
2562 if (t == 0)
2564 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2565 return size_zero_node;
2568 return t;
2571 /* Return the size of TYPE (in bytes) as a wide integer
2572 or return -1 if the size can vary or is larger than an integer. */
2574 HOST_WIDE_INT
2575 int_size_in_bytes (const_tree type)
2577 tree t;
2579 if (type == error_mark_node)
2580 return 0;
2582 type = TYPE_MAIN_VARIANT (type);
2583 t = TYPE_SIZE_UNIT (type);
2584 if (t == 0
2585 || TREE_CODE (t) != INTEGER_CST
2586 || TREE_INT_CST_HIGH (t) != 0
2587 /* If the result would appear negative, it's too big to represent. */
2588 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
2589 return -1;
2591 return TREE_INT_CST_LOW (t);
2594 /* Return the maximum size of TYPE (in bytes) as a wide integer
2595 or return -1 if the size can vary or is larger than an integer. */
2597 HOST_WIDE_INT
2598 max_int_size_in_bytes (const_tree type)
2600 HOST_WIDE_INT size = -1;
2601 tree size_tree;
2603 /* If this is an array type, check for a possible MAX_SIZE attached. */
2605 if (TREE_CODE (type) == ARRAY_TYPE)
2607 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2609 if (size_tree && host_integerp (size_tree, 1))
2610 size = tree_low_cst (size_tree, 1);
2613 /* If we still haven't been able to get a size, see if the language
2614 can compute a maximum size. */
2616 if (size == -1)
2618 size_tree = lang_hooks.types.max_size (type);
2620 if (size_tree && host_integerp (size_tree, 1))
2621 size = tree_low_cst (size_tree, 1);
2624 return size;
2627 /* Returns a tree for the size of EXP in bytes. */
2629 tree
2630 tree_expr_size (const_tree exp)
2632 if (DECL_P (exp)
2633 && DECL_SIZE_UNIT (exp) != 0)
2634 return DECL_SIZE_UNIT (exp);
2635 else
2636 return size_in_bytes (TREE_TYPE (exp));
2639 /* Return the bit position of FIELD, in bits from the start of the record.
2640 This is a tree of type bitsizetype. */
2642 tree
2643 bit_position (const_tree field)
2645 return bit_from_pos (DECL_FIELD_OFFSET (field),
2646 DECL_FIELD_BIT_OFFSET (field));
2649 /* Likewise, but return as an integer. It must be representable in
2650 that way (since it could be a signed value, we don't have the
2651 option of returning -1 like int_size_in_byte can. */
2653 HOST_WIDE_INT
2654 int_bit_position (const_tree field)
2656 return tree_low_cst (bit_position (field), 0);
2659 /* Return the byte position of FIELD, in bytes from the start of the record.
2660 This is a tree of type sizetype. */
2662 tree
2663 byte_position (const_tree field)
2665 return byte_from_pos (DECL_FIELD_OFFSET (field),
2666 DECL_FIELD_BIT_OFFSET (field));
2669 /* Likewise, but return as an integer. It must be representable in
2670 that way (since it could be a signed value, we don't have the
2671 option of returning -1 like int_size_in_byte can. */
2673 HOST_WIDE_INT
2674 int_byte_position (const_tree field)
2676 return tree_low_cst (byte_position (field), 0);
2679 /* Return the strictest alignment, in bits, that T is known to have. */
2681 unsigned int
2682 expr_align (const_tree t)
2684 unsigned int align0, align1;
2686 switch (TREE_CODE (t))
2688 CASE_CONVERT: case NON_LVALUE_EXPR:
2689 /* If we have conversions, we know that the alignment of the
2690 object must meet each of the alignments of the types. */
2691 align0 = expr_align (TREE_OPERAND (t, 0));
2692 align1 = TYPE_ALIGN (TREE_TYPE (t));
2693 return MAX (align0, align1);
2695 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2696 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2697 case CLEANUP_POINT_EXPR:
2698 /* These don't change the alignment of an object. */
2699 return expr_align (TREE_OPERAND (t, 0));
2701 case COND_EXPR:
2702 /* The best we can do is say that the alignment is the least aligned
2703 of the two arms. */
2704 align0 = expr_align (TREE_OPERAND (t, 1));
2705 align1 = expr_align (TREE_OPERAND (t, 2));
2706 return MIN (align0, align1);
2708 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2709 meaningfully, it's always 1. */
2710 case LABEL_DECL: case CONST_DECL:
2711 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2712 case FUNCTION_DECL:
2713 gcc_assert (DECL_ALIGN (t) != 0);
2714 return DECL_ALIGN (t);
2716 default:
2717 break;
2720 /* Otherwise take the alignment from that of the type. */
2721 return TYPE_ALIGN (TREE_TYPE (t));
2724 /* Return, as a tree node, the number of elements for TYPE (which is an
2725 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2727 tree
2728 array_type_nelts (const_tree type)
2730 tree index_type, min, max;
2732 /* If they did it with unspecified bounds, then we should have already
2733 given an error about it before we got here. */
2734 if (! TYPE_DOMAIN (type))
2735 return error_mark_node;
2737 index_type = TYPE_DOMAIN (type);
2738 min = TYPE_MIN_VALUE (index_type);
2739 max = TYPE_MAX_VALUE (index_type);
2741 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
2742 if (!max)
2743 return error_mark_node;
2745 return (integer_zerop (min)
2746 ? max
2747 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2750 /* If arg is static -- a reference to an object in static storage -- then
2751 return the object. This is not the same as the C meaning of `static'.
2752 If arg isn't static, return NULL. */
2754 tree
2755 staticp (tree arg)
2757 switch (TREE_CODE (arg))
2759 case FUNCTION_DECL:
2760 /* Nested functions are static, even though taking their address will
2761 involve a trampoline as we unnest the nested function and create
2762 the trampoline on the tree level. */
2763 return arg;
2765 case VAR_DECL:
2766 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2767 && ! DECL_THREAD_LOCAL_P (arg)
2768 && ! DECL_DLLIMPORT_P (arg)
2769 ? arg : NULL);
2771 case CONST_DECL:
2772 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2773 ? arg : NULL);
2775 case CONSTRUCTOR:
2776 return TREE_STATIC (arg) ? arg : NULL;
2778 case LABEL_DECL:
2779 case STRING_CST:
2780 return arg;
2782 case COMPONENT_REF:
2783 /* If the thing being referenced is not a field, then it is
2784 something language specific. */
2785 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
2787 /* If we are referencing a bitfield, we can't evaluate an
2788 ADDR_EXPR at compile time and so it isn't a constant. */
2789 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2790 return NULL;
2792 return staticp (TREE_OPERAND (arg, 0));
2794 case BIT_FIELD_REF:
2795 return NULL;
2797 case INDIRECT_REF:
2798 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2800 case ARRAY_REF:
2801 case ARRAY_RANGE_REF:
2802 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2803 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2804 return staticp (TREE_OPERAND (arg, 0));
2805 else
2806 return NULL;
2808 case COMPOUND_LITERAL_EXPR:
2809 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
2811 default:
2812 return NULL;
2819 /* Return whether OP is a DECL whose address is function-invariant. */
2821 bool
2822 decl_address_invariant_p (const_tree op)
2824 /* The conditions below are slightly less strict than the one in
2825 staticp. */
2827 switch (TREE_CODE (op))
2829 case PARM_DECL:
2830 case RESULT_DECL:
2831 case LABEL_DECL:
2832 case FUNCTION_DECL:
2833 return true;
2835 case VAR_DECL:
2836 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2837 || DECL_THREAD_LOCAL_P (op)
2838 || DECL_CONTEXT (op) == current_function_decl
2839 || decl_function_context (op) == current_function_decl)
2840 return true;
2841 break;
2843 case CONST_DECL:
2844 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2845 || decl_function_context (op) == current_function_decl)
2846 return true;
2847 break;
2849 default:
2850 break;
2853 return false;
2856 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
2858 bool
2859 decl_address_ip_invariant_p (const_tree op)
2861 /* The conditions below are slightly less strict than the one in
2862 staticp. */
2864 switch (TREE_CODE (op))
2866 case LABEL_DECL:
2867 case FUNCTION_DECL:
2868 case STRING_CST:
2869 return true;
2871 case VAR_DECL:
2872 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2873 && !DECL_DLLIMPORT_P (op))
2874 || DECL_THREAD_LOCAL_P (op))
2875 return true;
2876 break;
2878 case CONST_DECL:
2879 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
2880 return true;
2881 break;
2883 default:
2884 break;
2887 return false;
2891 /* Return true if T is function-invariant (internal function, does
2892 not handle arithmetic; that's handled in skip_simple_arithmetic and
2893 tree_invariant_p). */
2895 static bool tree_invariant_p (tree t);
2897 static bool
2898 tree_invariant_p_1 (tree t)
2900 tree op;
2902 if (TREE_CONSTANT (t)
2903 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
2904 return true;
2906 switch (TREE_CODE (t))
2908 case SAVE_EXPR:
2909 return true;
2911 case ADDR_EXPR:
2912 op = TREE_OPERAND (t, 0);
2913 while (handled_component_p (op))
2915 switch (TREE_CODE (op))
2917 case ARRAY_REF:
2918 case ARRAY_RANGE_REF:
2919 if (!tree_invariant_p (TREE_OPERAND (op, 1))
2920 || TREE_OPERAND (op, 2) != NULL_TREE
2921 || TREE_OPERAND (op, 3) != NULL_TREE)
2922 return false;
2923 break;
2925 case COMPONENT_REF:
2926 if (TREE_OPERAND (op, 2) != NULL_TREE)
2927 return false;
2928 break;
2930 default:;
2932 op = TREE_OPERAND (op, 0);
2935 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2937 default:
2938 break;
2941 return false;
2944 /* Return true if T is function-invariant. */
2946 static bool
2947 tree_invariant_p (tree t)
2949 tree inner = skip_simple_arithmetic (t);
2950 return tree_invariant_p_1 (inner);
2953 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
2954 Do this to any expression which may be used in more than one place,
2955 but must be evaluated only once.
2957 Normally, expand_expr would reevaluate the expression each time.
2958 Calling save_expr produces something that is evaluated and recorded
2959 the first time expand_expr is called on it. Subsequent calls to
2960 expand_expr just reuse the recorded value.
2962 The call to expand_expr that generates code that actually computes
2963 the value is the first call *at compile time*. Subsequent calls
2964 *at compile time* generate code to use the saved value.
2965 This produces correct result provided that *at run time* control
2966 always flows through the insns made by the first expand_expr
2967 before reaching the other places where the save_expr was evaluated.
2968 You, the caller of save_expr, must make sure this is so.
2970 Constants, and certain read-only nodes, are returned with no
2971 SAVE_EXPR because that is safe. Expressions containing placeholders
2972 are not touched; see tree.def for an explanation of what these
2973 are used for. */
2975 tree
2976 save_expr (tree expr)
2978 tree t = fold (expr);
2979 tree inner;
2981 /* If the tree evaluates to a constant, then we don't want to hide that
2982 fact (i.e. this allows further folding, and direct checks for constants).
2983 However, a read-only object that has side effects cannot be bypassed.
2984 Since it is no problem to reevaluate literals, we just return the
2985 literal node. */
2986 inner = skip_simple_arithmetic (t);
2987 if (TREE_CODE (inner) == ERROR_MARK)
2988 return inner;
2990 if (tree_invariant_p_1 (inner))
2991 return t;
2993 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
2994 it means that the size or offset of some field of an object depends on
2995 the value within another field.
2997 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
2998 and some variable since it would then need to be both evaluated once and
2999 evaluated more than once. Front-ends must assure this case cannot
3000 happen by surrounding any such subexpressions in their own SAVE_EXPR
3001 and forcing evaluation at the proper time. */
3002 if (contains_placeholder_p (inner))
3003 return t;
3005 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
3006 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
3008 /* This expression might be placed ahead of a jump to ensure that the
3009 value was computed on both sides of the jump. So make sure it isn't
3010 eliminated as dead. */
3011 TREE_SIDE_EFFECTS (t) = 1;
3012 return t;
3015 /* Look inside EXPR into any simple arithmetic operations. Return the
3016 outermost non-arithmetic or non-invariant node. */
3018 tree
3019 skip_simple_arithmetic (tree expr)
3021 /* We don't care about whether this can be used as an lvalue in this
3022 context. */
3023 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3024 expr = TREE_OPERAND (expr, 0);
3026 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
3027 a constant, it will be more efficient to not make another SAVE_EXPR since
3028 it will allow better simplification and GCSE will be able to merge the
3029 computations if they actually occur. */
3030 while (true)
3032 if (UNARY_CLASS_P (expr))
3033 expr = TREE_OPERAND (expr, 0);
3034 else if (BINARY_CLASS_P (expr))
3036 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
3037 expr = TREE_OPERAND (expr, 0);
3038 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
3039 expr = TREE_OPERAND (expr, 1);
3040 else
3041 break;
3043 else
3044 break;
3047 return expr;
3050 /* Look inside EXPR into simple arithmetic operations involving constants.
3051 Return the outermost non-arithmetic or non-constant node. */
3053 tree
3054 skip_simple_constant_arithmetic (tree expr)
3056 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3057 expr = TREE_OPERAND (expr, 0);
3059 while (true)
3061 if (UNARY_CLASS_P (expr))
3062 expr = TREE_OPERAND (expr, 0);
3063 else if (BINARY_CLASS_P (expr))
3065 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3066 expr = TREE_OPERAND (expr, 0);
3067 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3068 expr = TREE_OPERAND (expr, 1);
3069 else
3070 break;
3072 else
3073 break;
3076 return expr;
3079 /* Return which tree structure is used by T. */
3081 enum tree_node_structure_enum
3082 tree_node_structure (const_tree t)
3084 const enum tree_code code = TREE_CODE (t);
3085 return tree_node_structure_for_code (code);
3088 /* Set various status flags when building a CALL_EXPR object T. */
3090 static void
3091 process_call_operands (tree t)
3093 bool side_effects = TREE_SIDE_EFFECTS (t);
3094 bool read_only = false;
3095 int i = call_expr_flags (t);
3097 /* Calls have side-effects, except those to const or pure functions. */
3098 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
3099 side_effects = true;
3100 /* Propagate TREE_READONLY of arguments for const functions. */
3101 if (i & ECF_CONST)
3102 read_only = true;
3104 if (!side_effects || read_only)
3105 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
3107 tree op = TREE_OPERAND (t, i);
3108 if (op && TREE_SIDE_EFFECTS (op))
3109 side_effects = true;
3110 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
3111 read_only = false;
3114 TREE_SIDE_EFFECTS (t) = side_effects;
3115 TREE_READONLY (t) = read_only;
3118 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
3119 size or offset that depends on a field within a record. */
3121 bool
3122 contains_placeholder_p (const_tree exp)
3124 enum tree_code code;
3126 if (!exp)
3127 return 0;
3129 code = TREE_CODE (exp);
3130 if (code == PLACEHOLDER_EXPR)
3131 return 1;
3133 switch (TREE_CODE_CLASS (code))
3135 case tcc_reference:
3136 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
3137 position computations since they will be converted into a
3138 WITH_RECORD_EXPR involving the reference, which will assume
3139 here will be valid. */
3140 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3142 case tcc_exceptional:
3143 if (code == TREE_LIST)
3144 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
3145 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
3146 break;
3148 case tcc_unary:
3149 case tcc_binary:
3150 case tcc_comparison:
3151 case tcc_expression:
3152 switch (code)
3154 case COMPOUND_EXPR:
3155 /* Ignoring the first operand isn't quite right, but works best. */
3156 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
3158 case COND_EXPR:
3159 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3160 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
3161 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
3163 case SAVE_EXPR:
3164 /* The save_expr function never wraps anything containing
3165 a PLACEHOLDER_EXPR. */
3166 return 0;
3168 default:
3169 break;
3172 switch (TREE_CODE_LENGTH (code))
3174 case 1:
3175 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3176 case 2:
3177 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3178 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
3179 default:
3180 return 0;
3183 case tcc_vl_exp:
3184 switch (code)
3186 case CALL_EXPR:
3188 const_tree arg;
3189 const_call_expr_arg_iterator iter;
3190 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
3191 if (CONTAINS_PLACEHOLDER_P (arg))
3192 return 1;
3193 return 0;
3195 default:
3196 return 0;
3199 default:
3200 return 0;
3202 return 0;
3205 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3206 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3207 field positions. */
3209 static bool
3210 type_contains_placeholder_1 (const_tree type)
3212 /* If the size contains a placeholder or the parent type (component type in
3213 the case of arrays) type involves a placeholder, this type does. */
3214 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3215 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3216 || (!POINTER_TYPE_P (type)
3217 && TREE_TYPE (type)
3218 && type_contains_placeholder_p (TREE_TYPE (type))))
3219 return true;
3221 /* Now do type-specific checks. Note that the last part of the check above
3222 greatly limits what we have to do below. */
3223 switch (TREE_CODE (type))
3225 case VOID_TYPE:
3226 case COMPLEX_TYPE:
3227 case ENUMERAL_TYPE:
3228 case BOOLEAN_TYPE:
3229 case POINTER_TYPE:
3230 case OFFSET_TYPE:
3231 case REFERENCE_TYPE:
3232 case METHOD_TYPE:
3233 case FUNCTION_TYPE:
3234 case VECTOR_TYPE:
3235 case NULLPTR_TYPE:
3236 return false;
3238 case INTEGER_TYPE:
3239 case REAL_TYPE:
3240 case FIXED_POINT_TYPE:
3241 /* Here we just check the bounds. */
3242 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3243 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3245 case ARRAY_TYPE:
3246 /* We have already checked the component type above, so just check the
3247 domain type. */
3248 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3250 case RECORD_TYPE:
3251 case UNION_TYPE:
3252 case QUAL_UNION_TYPE:
3254 tree field;
3256 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3257 if (TREE_CODE (field) == FIELD_DECL
3258 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3259 || (TREE_CODE (type) == QUAL_UNION_TYPE
3260 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3261 || type_contains_placeholder_p (TREE_TYPE (field))))
3262 return true;
3264 return false;
3267 default:
3268 gcc_unreachable ();
3272 /* Wrapper around above function used to cache its result. */
3274 bool
3275 type_contains_placeholder_p (tree type)
3277 bool result;
3279 /* If the contains_placeholder_bits field has been initialized,
3280 then we know the answer. */
3281 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3282 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3284 /* Indicate that we've seen this type node, and the answer is false.
3285 This is what we want to return if we run into recursion via fields. */
3286 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3288 /* Compute the real value. */
3289 result = type_contains_placeholder_1 (type);
3291 /* Store the real value. */
3292 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3294 return result;
3297 /* Push tree EXP onto vector QUEUE if it is not already present. */
3299 static void
3300 push_without_duplicates (tree exp, vec<tree> *queue)
3302 unsigned int i;
3303 tree iter;
3305 FOR_EACH_VEC_ELT (*queue, i, iter)
3306 if (simple_cst_equal (iter, exp) == 1)
3307 break;
3309 if (!iter)
3310 queue->safe_push (exp);
3313 /* Given a tree EXP, find all occurrences of references to fields
3314 in a PLACEHOLDER_EXPR and place them in vector REFS without
3315 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3316 we assume here that EXP contains only arithmetic expressions
3317 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3318 argument list. */
3320 void
3321 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3323 enum tree_code code = TREE_CODE (exp);
3324 tree inner;
3325 int i;
3327 /* We handle TREE_LIST and COMPONENT_REF separately. */
3328 if (code == TREE_LIST)
3330 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3331 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3333 else if (code == COMPONENT_REF)
3335 for (inner = TREE_OPERAND (exp, 0);
3336 REFERENCE_CLASS_P (inner);
3337 inner = TREE_OPERAND (inner, 0))
3340 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3341 push_without_duplicates (exp, refs);
3342 else
3343 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3345 else
3346 switch (TREE_CODE_CLASS (code))
3348 case tcc_constant:
3349 break;
3351 case tcc_declaration:
3352 /* Variables allocated to static storage can stay. */
3353 if (!TREE_STATIC (exp))
3354 push_without_duplicates (exp, refs);
3355 break;
3357 case tcc_expression:
3358 /* This is the pattern built in ada/make_aligning_type. */
3359 if (code == ADDR_EXPR
3360 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3362 push_without_duplicates (exp, refs);
3363 break;
3366 /* Fall through... */
3368 case tcc_exceptional:
3369 case tcc_unary:
3370 case tcc_binary:
3371 case tcc_comparison:
3372 case tcc_reference:
3373 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3374 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3375 break;
3377 case tcc_vl_exp:
3378 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3379 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3380 break;
3382 default:
3383 gcc_unreachable ();
3387 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3388 return a tree with all occurrences of references to F in a
3389 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3390 CONST_DECLs. Note that we assume here that EXP contains only
3391 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3392 occurring only in their argument list. */
3394 tree
3395 substitute_in_expr (tree exp, tree f, tree r)
3397 enum tree_code code = TREE_CODE (exp);
3398 tree op0, op1, op2, op3;
3399 tree new_tree;
3401 /* We handle TREE_LIST and COMPONENT_REF separately. */
3402 if (code == TREE_LIST)
3404 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3405 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3406 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3407 return exp;
3409 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3411 else if (code == COMPONENT_REF)
3413 tree inner;
3415 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3416 and it is the right field, replace it with R. */
3417 for (inner = TREE_OPERAND (exp, 0);
3418 REFERENCE_CLASS_P (inner);
3419 inner = TREE_OPERAND (inner, 0))
3422 /* The field. */
3423 op1 = TREE_OPERAND (exp, 1);
3425 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3426 return r;
3428 /* If this expression hasn't been completed let, leave it alone. */
3429 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3430 return exp;
3432 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3433 if (op0 == TREE_OPERAND (exp, 0))
3434 return exp;
3436 new_tree
3437 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3439 else
3440 switch (TREE_CODE_CLASS (code))
3442 case tcc_constant:
3443 return exp;
3445 case tcc_declaration:
3446 if (exp == f)
3447 return r;
3448 else
3449 return exp;
3451 case tcc_expression:
3452 if (exp == f)
3453 return r;
3455 /* Fall through... */
3457 case tcc_exceptional:
3458 case tcc_unary:
3459 case tcc_binary:
3460 case tcc_comparison:
3461 case tcc_reference:
3462 switch (TREE_CODE_LENGTH (code))
3464 case 0:
3465 return exp;
3467 case 1:
3468 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3469 if (op0 == TREE_OPERAND (exp, 0))
3470 return exp;
3472 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3473 break;
3475 case 2:
3476 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3477 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3479 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3480 return exp;
3482 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3483 break;
3485 case 3:
3486 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3487 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3488 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3490 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3491 && op2 == TREE_OPERAND (exp, 2))
3492 return exp;
3494 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3495 break;
3497 case 4:
3498 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3499 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3500 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3501 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3503 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3504 && op2 == TREE_OPERAND (exp, 2)
3505 && op3 == TREE_OPERAND (exp, 3))
3506 return exp;
3508 new_tree
3509 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3510 break;
3512 default:
3513 gcc_unreachable ();
3515 break;
3517 case tcc_vl_exp:
3519 int i;
3521 new_tree = NULL_TREE;
3523 /* If we are trying to replace F with a constant, inline back
3524 functions which do nothing else than computing a value from
3525 the arguments they are passed. This makes it possible to
3526 fold partially or entirely the replacement expression. */
3527 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3529 tree t = maybe_inline_call_in_expr (exp);
3530 if (t)
3531 return SUBSTITUTE_IN_EXPR (t, f, r);
3534 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3536 tree op = TREE_OPERAND (exp, i);
3537 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3538 if (new_op != op)
3540 if (!new_tree)
3541 new_tree = copy_node (exp);
3542 TREE_OPERAND (new_tree, i) = new_op;
3546 if (new_tree)
3548 new_tree = fold (new_tree);
3549 if (TREE_CODE (new_tree) == CALL_EXPR)
3550 process_call_operands (new_tree);
3552 else
3553 return exp;
3555 break;
3557 default:
3558 gcc_unreachable ();
3561 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3563 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3564 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3566 return new_tree;
3569 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3570 for it within OBJ, a tree that is an object or a chain of references. */
3572 tree
3573 substitute_placeholder_in_expr (tree exp, tree obj)
3575 enum tree_code code = TREE_CODE (exp);
3576 tree op0, op1, op2, op3;
3577 tree new_tree;
3579 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3580 in the chain of OBJ. */
3581 if (code == PLACEHOLDER_EXPR)
3583 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3584 tree elt;
3586 for (elt = obj; elt != 0;
3587 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3588 || TREE_CODE (elt) == COND_EXPR)
3589 ? TREE_OPERAND (elt, 1)
3590 : (REFERENCE_CLASS_P (elt)
3591 || UNARY_CLASS_P (elt)
3592 || BINARY_CLASS_P (elt)
3593 || VL_EXP_CLASS_P (elt)
3594 || EXPRESSION_CLASS_P (elt))
3595 ? TREE_OPERAND (elt, 0) : 0))
3596 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3597 return elt;
3599 for (elt = obj; elt != 0;
3600 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3601 || TREE_CODE (elt) == COND_EXPR)
3602 ? TREE_OPERAND (elt, 1)
3603 : (REFERENCE_CLASS_P (elt)
3604 || UNARY_CLASS_P (elt)
3605 || BINARY_CLASS_P (elt)
3606 || VL_EXP_CLASS_P (elt)
3607 || EXPRESSION_CLASS_P (elt))
3608 ? TREE_OPERAND (elt, 0) : 0))
3609 if (POINTER_TYPE_P (TREE_TYPE (elt))
3610 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3611 == need_type))
3612 return fold_build1 (INDIRECT_REF, need_type, elt);
3614 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3615 survives until RTL generation, there will be an error. */
3616 return exp;
3619 /* TREE_LIST is special because we need to look at TREE_VALUE
3620 and TREE_CHAIN, not TREE_OPERANDS. */
3621 else if (code == TREE_LIST)
3623 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3624 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3625 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3626 return exp;
3628 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3630 else
3631 switch (TREE_CODE_CLASS (code))
3633 case tcc_constant:
3634 case tcc_declaration:
3635 return exp;
3637 case tcc_exceptional:
3638 case tcc_unary:
3639 case tcc_binary:
3640 case tcc_comparison:
3641 case tcc_expression:
3642 case tcc_reference:
3643 case tcc_statement:
3644 switch (TREE_CODE_LENGTH (code))
3646 case 0:
3647 return exp;
3649 case 1:
3650 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3651 if (op0 == TREE_OPERAND (exp, 0))
3652 return exp;
3654 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3655 break;
3657 case 2:
3658 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3659 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3661 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3662 return exp;
3664 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3665 break;
3667 case 3:
3668 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3669 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3670 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3672 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3673 && op2 == TREE_OPERAND (exp, 2))
3674 return exp;
3676 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3677 break;
3679 case 4:
3680 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3681 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3682 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3683 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3685 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3686 && op2 == TREE_OPERAND (exp, 2)
3687 && op3 == TREE_OPERAND (exp, 3))
3688 return exp;
3690 new_tree
3691 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3692 break;
3694 default:
3695 gcc_unreachable ();
3697 break;
3699 case tcc_vl_exp:
3701 int i;
3703 new_tree = NULL_TREE;
3705 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3707 tree op = TREE_OPERAND (exp, i);
3708 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
3709 if (new_op != op)
3711 if (!new_tree)
3712 new_tree = copy_node (exp);
3713 TREE_OPERAND (new_tree, i) = new_op;
3717 if (new_tree)
3719 new_tree = fold (new_tree);
3720 if (TREE_CODE (new_tree) == CALL_EXPR)
3721 process_call_operands (new_tree);
3723 else
3724 return exp;
3726 break;
3728 default:
3729 gcc_unreachable ();
3732 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3734 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3735 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3737 return new_tree;
3740 /* Stabilize a reference so that we can use it any number of times
3741 without causing its operands to be evaluated more than once.
3742 Returns the stabilized reference. This works by means of save_expr,
3743 so see the caveats in the comments about save_expr.
3745 Also allows conversion expressions whose operands are references.
3746 Any other kind of expression is returned unchanged. */
3748 tree
3749 stabilize_reference (tree ref)
3751 tree result;
3752 enum tree_code code = TREE_CODE (ref);
3754 switch (code)
3756 case VAR_DECL:
3757 case PARM_DECL:
3758 case RESULT_DECL:
3759 /* No action is needed in this case. */
3760 return ref;
3762 CASE_CONVERT:
3763 case FLOAT_EXPR:
3764 case FIX_TRUNC_EXPR:
3765 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
3766 break;
3768 case INDIRECT_REF:
3769 result = build_nt (INDIRECT_REF,
3770 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
3771 break;
3773 case COMPONENT_REF:
3774 result = build_nt (COMPONENT_REF,
3775 stabilize_reference (TREE_OPERAND (ref, 0)),
3776 TREE_OPERAND (ref, 1), NULL_TREE);
3777 break;
3779 case BIT_FIELD_REF:
3780 result = build_nt (BIT_FIELD_REF,
3781 stabilize_reference (TREE_OPERAND (ref, 0)),
3782 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
3783 break;
3785 case ARRAY_REF:
3786 result = build_nt (ARRAY_REF,
3787 stabilize_reference (TREE_OPERAND (ref, 0)),
3788 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3789 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3790 break;
3792 case ARRAY_RANGE_REF:
3793 result = build_nt (ARRAY_RANGE_REF,
3794 stabilize_reference (TREE_OPERAND (ref, 0)),
3795 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3796 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3797 break;
3799 case COMPOUND_EXPR:
3800 /* We cannot wrap the first expression in a SAVE_EXPR, as then
3801 it wouldn't be ignored. This matters when dealing with
3802 volatiles. */
3803 return stabilize_reference_1 (ref);
3805 /* If arg isn't a kind of lvalue we recognize, make no change.
3806 Caller should recognize the error for an invalid lvalue. */
3807 default:
3808 return ref;
3810 case ERROR_MARK:
3811 return error_mark_node;
3814 TREE_TYPE (result) = TREE_TYPE (ref);
3815 TREE_READONLY (result) = TREE_READONLY (ref);
3816 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
3817 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
3819 return result;
3822 /* Subroutine of stabilize_reference; this is called for subtrees of
3823 references. Any expression with side-effects must be put in a SAVE_EXPR
3824 to ensure that it is only evaluated once.
3826 We don't put SAVE_EXPR nodes around everything, because assigning very
3827 simple expressions to temporaries causes us to miss good opportunities
3828 for optimizations. Among other things, the opportunity to fold in the
3829 addition of a constant into an addressing mode often gets lost, e.g.
3830 "y[i+1] += x;". In general, we take the approach that we should not make
3831 an assignment unless we are forced into it - i.e., that any non-side effect
3832 operator should be allowed, and that cse should take care of coalescing
3833 multiple utterances of the same expression should that prove fruitful. */
3835 tree
3836 stabilize_reference_1 (tree e)
3838 tree result;
3839 enum tree_code code = TREE_CODE (e);
3841 /* We cannot ignore const expressions because it might be a reference
3842 to a const array but whose index contains side-effects. But we can
3843 ignore things that are actual constant or that already have been
3844 handled by this function. */
3846 if (tree_invariant_p (e))
3847 return e;
3849 switch (TREE_CODE_CLASS (code))
3851 case tcc_exceptional:
3852 case tcc_type:
3853 case tcc_declaration:
3854 case tcc_comparison:
3855 case tcc_statement:
3856 case tcc_expression:
3857 case tcc_reference:
3858 case tcc_vl_exp:
3859 /* If the expression has side-effects, then encase it in a SAVE_EXPR
3860 so that it will only be evaluated once. */
3861 /* The reference (r) and comparison (<) classes could be handled as
3862 below, but it is generally faster to only evaluate them once. */
3863 if (TREE_SIDE_EFFECTS (e))
3864 return save_expr (e);
3865 return e;
3867 case tcc_constant:
3868 /* Constants need no processing. In fact, we should never reach
3869 here. */
3870 return e;
3872 case tcc_binary:
3873 /* Division is slow and tends to be compiled with jumps,
3874 especially the division by powers of 2 that is often
3875 found inside of an array reference. So do it just once. */
3876 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3877 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3878 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3879 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3880 return save_expr (e);
3881 /* Recursively stabilize each operand. */
3882 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3883 stabilize_reference_1 (TREE_OPERAND (e, 1)));
3884 break;
3886 case tcc_unary:
3887 /* Recursively stabilize each operand. */
3888 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3889 break;
3891 default:
3892 gcc_unreachable ();
3895 TREE_TYPE (result) = TREE_TYPE (e);
3896 TREE_READONLY (result) = TREE_READONLY (e);
3897 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3898 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3900 return result;
3903 /* Low-level constructors for expressions. */
3905 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
3906 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
3908 void
3909 recompute_tree_invariant_for_addr_expr (tree t)
3911 tree node;
3912 bool tc = true, se = false;
3914 /* We started out assuming this address is both invariant and constant, but
3915 does not have side effects. Now go down any handled components and see if
3916 any of them involve offsets that are either non-constant or non-invariant.
3917 Also check for side-effects.
3919 ??? Note that this code makes no attempt to deal with the case where
3920 taking the address of something causes a copy due to misalignment. */
3922 #define UPDATE_FLAGS(NODE) \
3923 do { tree _node = (NODE); \
3924 if (_node && !TREE_CONSTANT (_node)) tc = false; \
3925 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
3927 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
3928 node = TREE_OPERAND (node, 0))
3930 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
3931 array reference (probably made temporarily by the G++ front end),
3932 so ignore all the operands. */
3933 if ((TREE_CODE (node) == ARRAY_REF
3934 || TREE_CODE (node) == ARRAY_RANGE_REF)
3935 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
3937 UPDATE_FLAGS (TREE_OPERAND (node, 1));
3938 if (TREE_OPERAND (node, 2))
3939 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3940 if (TREE_OPERAND (node, 3))
3941 UPDATE_FLAGS (TREE_OPERAND (node, 3));
3943 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
3944 FIELD_DECL, apparently. The G++ front end can put something else
3945 there, at least temporarily. */
3946 else if (TREE_CODE (node) == COMPONENT_REF
3947 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
3949 if (TREE_OPERAND (node, 2))
3950 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3954 node = lang_hooks.expr_to_decl (node, &tc, &se);
3956 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
3957 the address, since &(*a)->b is a form of addition. If it's a constant, the
3958 address is constant too. If it's a decl, its address is constant if the
3959 decl is static. Everything else is not constant and, furthermore,
3960 taking the address of a volatile variable is not volatile. */
3961 if (TREE_CODE (node) == INDIRECT_REF
3962 || TREE_CODE (node) == MEM_REF)
3963 UPDATE_FLAGS (TREE_OPERAND (node, 0));
3964 else if (CONSTANT_CLASS_P (node))
3966 else if (DECL_P (node))
3967 tc &= (staticp (node) != NULL_TREE);
3968 else
3970 tc = false;
3971 se |= TREE_SIDE_EFFECTS (node);
3975 TREE_CONSTANT (t) = tc;
3976 TREE_SIDE_EFFECTS (t) = se;
3977 #undef UPDATE_FLAGS
3980 /* Build an expression of code CODE, data type TYPE, and operands as
3981 specified. Expressions and reference nodes can be created this way.
3982 Constants, decls, types and misc nodes cannot be.
3984 We define 5 non-variadic functions, from 0 to 4 arguments. This is
3985 enough for all extant tree codes. */
3987 tree
3988 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
3990 tree t;
3992 gcc_assert (TREE_CODE_LENGTH (code) == 0);
3994 t = make_node_stat (code PASS_MEM_STAT);
3995 TREE_TYPE (t) = tt;
3997 return t;
4000 tree
4001 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4003 int length = sizeof (struct tree_exp);
4004 tree t;
4006 record_node_allocation_statistics (code, length);
4008 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4010 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4012 memset (t, 0, sizeof (struct tree_common));
4014 TREE_SET_CODE (t, code);
4016 TREE_TYPE (t) = type;
4017 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4018 TREE_OPERAND (t, 0) = node;
4019 if (node && !TYPE_P (node))
4021 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4022 TREE_READONLY (t) = TREE_READONLY (node);
4025 if (TREE_CODE_CLASS (code) == tcc_statement)
4026 TREE_SIDE_EFFECTS (t) = 1;
4027 else switch (code)
4029 case VA_ARG_EXPR:
4030 /* All of these have side-effects, no matter what their
4031 operands are. */
4032 TREE_SIDE_EFFECTS (t) = 1;
4033 TREE_READONLY (t) = 0;
4034 break;
4036 case INDIRECT_REF:
4037 /* Whether a dereference is readonly has nothing to do with whether
4038 its operand is readonly. */
4039 TREE_READONLY (t) = 0;
4040 break;
4042 case ADDR_EXPR:
4043 if (node)
4044 recompute_tree_invariant_for_addr_expr (t);
4045 break;
4047 default:
4048 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4049 && node && !TYPE_P (node)
4050 && TREE_CONSTANT (node))
4051 TREE_CONSTANT (t) = 1;
4052 if (TREE_CODE_CLASS (code) == tcc_reference
4053 && node && TREE_THIS_VOLATILE (node))
4054 TREE_THIS_VOLATILE (t) = 1;
4055 break;
4058 return t;
4061 #define PROCESS_ARG(N) \
4062 do { \
4063 TREE_OPERAND (t, N) = arg##N; \
4064 if (arg##N &&!TYPE_P (arg##N)) \
4066 if (TREE_SIDE_EFFECTS (arg##N)) \
4067 side_effects = 1; \
4068 if (!TREE_READONLY (arg##N) \
4069 && !CONSTANT_CLASS_P (arg##N)) \
4070 (void) (read_only = 0); \
4071 if (!TREE_CONSTANT (arg##N)) \
4072 (void) (constant = 0); \
4074 } while (0)
4076 tree
4077 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
4079 bool constant, read_only, side_effects;
4080 tree t;
4082 gcc_assert (TREE_CODE_LENGTH (code) == 2);
4084 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
4085 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
4086 /* When sizetype precision doesn't match that of pointers
4087 we need to be able to build explicit extensions or truncations
4088 of the offset argument. */
4089 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
4090 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
4091 && TREE_CODE (arg1) == INTEGER_CST);
4093 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
4094 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
4095 && ptrofftype_p (TREE_TYPE (arg1)));
4097 t = make_node_stat (code PASS_MEM_STAT);
4098 TREE_TYPE (t) = tt;
4100 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
4101 result based on those same flags for the arguments. But if the
4102 arguments aren't really even `tree' expressions, we shouldn't be trying
4103 to do this. */
4105 /* Expressions without side effects may be constant if their
4106 arguments are as well. */
4107 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4108 || TREE_CODE_CLASS (code) == tcc_binary);
4109 read_only = 1;
4110 side_effects = TREE_SIDE_EFFECTS (t);
4112 PROCESS_ARG(0);
4113 PROCESS_ARG(1);
4115 TREE_READONLY (t) = read_only;
4116 TREE_CONSTANT (t) = constant;
4117 TREE_SIDE_EFFECTS (t) = side_effects;
4118 TREE_THIS_VOLATILE (t)
4119 = (TREE_CODE_CLASS (code) == tcc_reference
4120 && arg0 && TREE_THIS_VOLATILE (arg0));
4122 return t;
4126 tree
4127 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4128 tree arg2 MEM_STAT_DECL)
4130 bool constant, read_only, side_effects;
4131 tree t;
4133 gcc_assert (TREE_CODE_LENGTH (code) == 3);
4134 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4136 t = make_node_stat (code PASS_MEM_STAT);
4137 TREE_TYPE (t) = tt;
4139 read_only = 1;
4141 /* As a special exception, if COND_EXPR has NULL branches, we
4142 assume that it is a gimple statement and always consider
4143 it to have side effects. */
4144 if (code == COND_EXPR
4145 && tt == void_type_node
4146 && arg1 == NULL_TREE
4147 && arg2 == NULL_TREE)
4148 side_effects = true;
4149 else
4150 side_effects = TREE_SIDE_EFFECTS (t);
4152 PROCESS_ARG(0);
4153 PROCESS_ARG(1);
4154 PROCESS_ARG(2);
4156 if (code == COND_EXPR)
4157 TREE_READONLY (t) = read_only;
4159 TREE_SIDE_EFFECTS (t) = side_effects;
4160 TREE_THIS_VOLATILE (t)
4161 = (TREE_CODE_CLASS (code) == tcc_reference
4162 && arg0 && TREE_THIS_VOLATILE (arg0));
4164 return t;
4167 tree
4168 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4169 tree arg2, tree arg3 MEM_STAT_DECL)
4171 bool constant, read_only, side_effects;
4172 tree t;
4174 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4176 t = make_node_stat (code PASS_MEM_STAT);
4177 TREE_TYPE (t) = tt;
4179 side_effects = TREE_SIDE_EFFECTS (t);
4181 PROCESS_ARG(0);
4182 PROCESS_ARG(1);
4183 PROCESS_ARG(2);
4184 PROCESS_ARG(3);
4186 TREE_SIDE_EFFECTS (t) = side_effects;
4187 TREE_THIS_VOLATILE (t)
4188 = (TREE_CODE_CLASS (code) == tcc_reference
4189 && arg0 && TREE_THIS_VOLATILE (arg0));
4191 return t;
4194 tree
4195 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4196 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4198 bool constant, read_only, side_effects;
4199 tree t;
4201 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4203 t = make_node_stat (code PASS_MEM_STAT);
4204 TREE_TYPE (t) = tt;
4206 side_effects = TREE_SIDE_EFFECTS (t);
4208 PROCESS_ARG(0);
4209 PROCESS_ARG(1);
4210 PROCESS_ARG(2);
4211 PROCESS_ARG(3);
4212 PROCESS_ARG(4);
4214 TREE_SIDE_EFFECTS (t) = side_effects;
4215 TREE_THIS_VOLATILE (t)
4216 = (TREE_CODE_CLASS (code) == tcc_reference
4217 && arg0 && TREE_THIS_VOLATILE (arg0));
4219 return t;
4222 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4223 on the pointer PTR. */
4225 tree
4226 build_simple_mem_ref_loc (location_t loc, tree ptr)
4228 HOST_WIDE_INT offset = 0;
4229 tree ptype = TREE_TYPE (ptr);
4230 tree tem;
4231 /* For convenience allow addresses that collapse to a simple base
4232 and offset. */
4233 if (TREE_CODE (ptr) == ADDR_EXPR
4234 && (handled_component_p (TREE_OPERAND (ptr, 0))
4235 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4237 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4238 gcc_assert (ptr);
4239 ptr = build_fold_addr_expr (ptr);
4240 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4242 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4243 ptr, build_int_cst (ptype, offset));
4244 SET_EXPR_LOCATION (tem, loc);
4245 return tem;
4248 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4250 double_int
4251 mem_ref_offset (const_tree t)
4253 tree toff = TREE_OPERAND (t, 1);
4254 return tree_to_double_int (toff).sext (TYPE_PRECISION (TREE_TYPE (toff)));
4257 /* Return the pointer-type relevant for TBAA purposes from the
4258 gimple memory reference tree T. This is the type to be used for
4259 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T. */
4261 tree
4262 reference_alias_ptr_type (const_tree t)
4264 const_tree base = t;
4265 while (handled_component_p (base))
4266 base = TREE_OPERAND (base, 0);
4267 if (TREE_CODE (base) == MEM_REF)
4268 return TREE_TYPE (TREE_OPERAND (base, 1));
4269 else if (TREE_CODE (base) == TARGET_MEM_REF)
4270 return TREE_TYPE (TMR_OFFSET (base));
4271 else
4272 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (base)));
4275 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4276 offsetted by OFFSET units. */
4278 tree
4279 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4281 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4282 build_fold_addr_expr (base),
4283 build_int_cst (ptr_type_node, offset));
4284 tree addr = build1 (ADDR_EXPR, type, ref);
4285 recompute_tree_invariant_for_addr_expr (addr);
4286 return addr;
4289 /* Similar except don't specify the TREE_TYPE
4290 and leave the TREE_SIDE_EFFECTS as 0.
4291 It is permissible for arguments to be null,
4292 or even garbage if their values do not matter. */
4294 tree
4295 build_nt (enum tree_code code, ...)
4297 tree t;
4298 int length;
4299 int i;
4300 va_list p;
4302 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4304 va_start (p, code);
4306 t = make_node (code);
4307 length = TREE_CODE_LENGTH (code);
4309 for (i = 0; i < length; i++)
4310 TREE_OPERAND (t, i) = va_arg (p, tree);
4312 va_end (p);
4313 return t;
4316 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4317 tree vec. */
4319 tree
4320 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4322 tree ret, t;
4323 unsigned int ix;
4325 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4326 CALL_EXPR_FN (ret) = fn;
4327 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4328 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4329 CALL_EXPR_ARG (ret, ix) = t;
4330 return ret;
4333 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4334 We do NOT enter this node in any sort of symbol table.
4336 LOC is the location of the decl.
4338 layout_decl is used to set up the decl's storage layout.
4339 Other slots are initialized to 0 or null pointers. */
4341 tree
4342 build_decl_stat (location_t loc, enum tree_code code, tree name,
4343 tree type MEM_STAT_DECL)
4345 tree t;
4347 t = make_node_stat (code PASS_MEM_STAT);
4348 DECL_SOURCE_LOCATION (t) = loc;
4350 /* if (type == error_mark_node)
4351 type = integer_type_node; */
4352 /* That is not done, deliberately, so that having error_mark_node
4353 as the type can suppress useless errors in the use of this variable. */
4355 DECL_NAME (t) = name;
4356 TREE_TYPE (t) = type;
4358 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4359 layout_decl (t, 0);
4361 return t;
4364 /* Builds and returns function declaration with NAME and TYPE. */
4366 tree
4367 build_fn_decl (const char *name, tree type)
4369 tree id = get_identifier (name);
4370 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4372 DECL_EXTERNAL (decl) = 1;
4373 TREE_PUBLIC (decl) = 1;
4374 DECL_ARTIFICIAL (decl) = 1;
4375 TREE_NOTHROW (decl) = 1;
4377 return decl;
4380 vec<tree, va_gc> *all_translation_units;
4382 /* Builds a new translation-unit decl with name NAME, queues it in the
4383 global list of translation-unit decls and returns it. */
4385 tree
4386 build_translation_unit_decl (tree name)
4388 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4389 name, NULL_TREE);
4390 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4391 vec_safe_push (all_translation_units, tu);
4392 return tu;
4396 /* BLOCK nodes are used to represent the structure of binding contours
4397 and declarations, once those contours have been exited and their contents
4398 compiled. This information is used for outputting debugging info. */
4400 tree
4401 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4403 tree block = make_node (BLOCK);
4405 BLOCK_VARS (block) = vars;
4406 BLOCK_SUBBLOCKS (block) = subblocks;
4407 BLOCK_SUPERCONTEXT (block) = supercontext;
4408 BLOCK_CHAIN (block) = chain;
4409 return block;
4413 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4415 LOC is the location to use in tree T. */
4417 void
4418 protected_set_expr_location (tree t, location_t loc)
4420 if (t && CAN_HAVE_LOCATION_P (t))
4421 SET_EXPR_LOCATION (t, loc);
4424 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4425 is ATTRIBUTE. */
4427 tree
4428 build_decl_attribute_variant (tree ddecl, tree attribute)
4430 DECL_ATTRIBUTES (ddecl) = attribute;
4431 return ddecl;
4434 /* Borrowed from hashtab.c iterative_hash implementation. */
4435 #define mix(a,b,c) \
4437 a -= b; a -= c; a ^= (c>>13); \
4438 b -= c; b -= a; b ^= (a<< 8); \
4439 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
4440 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
4441 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
4442 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
4443 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
4444 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
4445 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
4449 /* Produce good hash value combining VAL and VAL2. */
4450 hashval_t
4451 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
4453 /* the golden ratio; an arbitrary value. */
4454 hashval_t a = 0x9e3779b9;
4456 mix (a, val, val2);
4457 return val2;
4460 /* Produce good hash value combining VAL and VAL2. */
4461 hashval_t
4462 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
4464 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
4465 return iterative_hash_hashval_t (val, val2);
4466 else
4468 hashval_t a = (hashval_t) val;
4469 /* Avoid warnings about shifting of more than the width of the type on
4470 hosts that won't execute this path. */
4471 int zero = 0;
4472 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
4473 mix (a, b, val2);
4474 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
4476 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
4477 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
4478 mix (a, b, val2);
4480 return val2;
4484 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4485 is ATTRIBUTE and its qualifiers are QUALS.
4487 Record such modified types already made so we don't make duplicates. */
4489 tree
4490 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4492 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4494 hashval_t hashcode = 0;
4495 tree ntype;
4496 enum tree_code code = TREE_CODE (ttype);
4498 /* Building a distinct copy of a tagged type is inappropriate; it
4499 causes breakage in code that expects there to be a one-to-one
4500 relationship between a struct and its fields.
4501 build_duplicate_type is another solution (as used in
4502 handle_transparent_union_attribute), but that doesn't play well
4503 with the stronger C++ type identity model. */
4504 if (TREE_CODE (ttype) == RECORD_TYPE
4505 || TREE_CODE (ttype) == UNION_TYPE
4506 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4507 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4509 warning (OPT_Wattributes,
4510 "ignoring attributes applied to %qT after definition",
4511 TYPE_MAIN_VARIANT (ttype));
4512 return build_qualified_type (ttype, quals);
4515 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4516 ntype = build_distinct_type_copy (ttype);
4518 TYPE_ATTRIBUTES (ntype) = attribute;
4520 hashcode = iterative_hash_object (code, hashcode);
4521 if (TREE_TYPE (ntype))
4522 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
4523 hashcode);
4524 hashcode = attribute_hash_list (attribute, hashcode);
4526 switch (TREE_CODE (ntype))
4528 case FUNCTION_TYPE:
4529 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
4530 break;
4531 case ARRAY_TYPE:
4532 if (TYPE_DOMAIN (ntype))
4533 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
4534 hashcode);
4535 break;
4536 case INTEGER_TYPE:
4537 hashcode = iterative_hash_object
4538 (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
4539 hashcode = iterative_hash_object
4540 (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
4541 break;
4542 case REAL_TYPE:
4543 case FIXED_POINT_TYPE:
4545 unsigned int precision = TYPE_PRECISION (ntype);
4546 hashcode = iterative_hash_object (precision, hashcode);
4548 break;
4549 default:
4550 break;
4553 ntype = type_hash_canon (hashcode, ntype);
4555 /* If the target-dependent attributes make NTYPE different from
4556 its canonical type, we will need to use structural equality
4557 checks for this type. */
4558 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4559 || !comp_type_attributes (ntype, ttype))
4560 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4561 else if (TYPE_CANONICAL (ntype) == ntype)
4562 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4564 ttype = build_qualified_type (ntype, quals);
4566 else if (TYPE_QUALS (ttype) != quals)
4567 ttype = build_qualified_type (ttype, quals);
4569 return ttype;
4572 /* Compare two attributes for their value identity. Return true if the
4573 attribute values are known to be equal; otherwise return false.
4576 static bool
4577 attribute_value_equal (const_tree attr1, const_tree attr2)
4579 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4580 return true;
4582 if (TREE_VALUE (attr1) != NULL_TREE
4583 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4584 && TREE_VALUE (attr2) != NULL
4585 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4586 return (simple_cst_list_equal (TREE_VALUE (attr1),
4587 TREE_VALUE (attr2)) == 1);
4589 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4592 /* Return 0 if the attributes for two types are incompatible, 1 if they
4593 are compatible, and 2 if they are nearly compatible (which causes a
4594 warning to be generated). */
4596 comp_type_attributes (const_tree type1, const_tree type2)
4598 const_tree a1 = TYPE_ATTRIBUTES (type1);
4599 const_tree a2 = TYPE_ATTRIBUTES (type2);
4600 const_tree a;
4602 if (a1 == a2)
4603 return 1;
4604 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4606 const struct attribute_spec *as;
4607 const_tree attr;
4609 as = lookup_attribute_spec (get_attribute_name (a));
4610 if (!as || as->affects_type_identity == false)
4611 continue;
4613 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4614 if (!attr || !attribute_value_equal (a, attr))
4615 break;
4617 if (!a)
4619 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
4621 const struct attribute_spec *as;
4623 as = lookup_attribute_spec (get_attribute_name (a));
4624 if (!as || as->affects_type_identity == false)
4625 continue;
4627 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
4628 break;
4629 /* We don't need to compare trees again, as we did this
4630 already in first loop. */
4632 /* All types - affecting identity - are equal, so
4633 there is no need to call target hook for comparison. */
4634 if (!a)
4635 return 1;
4637 /* As some type combinations - like default calling-convention - might
4638 be compatible, we have to call the target hook to get the final result. */
4639 return targetm.comp_type_attributes (type1, type2);
4642 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4643 is ATTRIBUTE.
4645 Record such modified types already made so we don't make duplicates. */
4647 tree
4648 build_type_attribute_variant (tree ttype, tree attribute)
4650 return build_type_attribute_qual_variant (ttype, attribute,
4651 TYPE_QUALS (ttype));
4655 /* Reset the expression *EXPR_P, a size or position.
4657 ??? We could reset all non-constant sizes or positions. But it's cheap
4658 enough to not do so and refrain from adding workarounds to dwarf2out.c.
4660 We need to reset self-referential sizes or positions because they cannot
4661 be gimplified and thus can contain a CALL_EXPR after the gimplification
4662 is finished, which will run afoul of LTO streaming. And they need to be
4663 reset to something essentially dummy but not constant, so as to preserve
4664 the properties of the object they are attached to. */
4666 static inline void
4667 free_lang_data_in_one_sizepos (tree *expr_p)
4669 tree expr = *expr_p;
4670 if (CONTAINS_PLACEHOLDER_P (expr))
4671 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
4675 /* Reset all the fields in a binfo node BINFO. We only keep
4676 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
4678 static void
4679 free_lang_data_in_binfo (tree binfo)
4681 unsigned i;
4682 tree t;
4684 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
4686 BINFO_VIRTUALS (binfo) = NULL_TREE;
4687 BINFO_BASE_ACCESSES (binfo) = NULL;
4688 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
4689 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
4691 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
4692 free_lang_data_in_binfo (t);
4696 /* Reset all language specific information still present in TYPE. */
4698 static void
4699 free_lang_data_in_type (tree type)
4701 gcc_assert (TYPE_P (type));
4703 /* Give the FE a chance to remove its own data first. */
4704 lang_hooks.free_lang_data (type);
4706 TREE_LANG_FLAG_0 (type) = 0;
4707 TREE_LANG_FLAG_1 (type) = 0;
4708 TREE_LANG_FLAG_2 (type) = 0;
4709 TREE_LANG_FLAG_3 (type) = 0;
4710 TREE_LANG_FLAG_4 (type) = 0;
4711 TREE_LANG_FLAG_5 (type) = 0;
4712 TREE_LANG_FLAG_6 (type) = 0;
4714 if (TREE_CODE (type) == FUNCTION_TYPE)
4716 /* Remove the const and volatile qualifiers from arguments. The
4717 C++ front end removes them, but the C front end does not,
4718 leading to false ODR violation errors when merging two
4719 instances of the same function signature compiled by
4720 different front ends. */
4721 tree p;
4723 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
4725 tree arg_type = TREE_VALUE (p);
4727 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
4729 int quals = TYPE_QUALS (arg_type)
4730 & ~TYPE_QUAL_CONST
4731 & ~TYPE_QUAL_VOLATILE;
4732 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
4733 free_lang_data_in_type (TREE_VALUE (p));
4738 /* Remove members that are not actually FIELD_DECLs from the field
4739 list of an aggregate. These occur in C++. */
4740 if (RECORD_OR_UNION_TYPE_P (type))
4742 tree prev, member;
4744 /* Note that TYPE_FIELDS can be shared across distinct
4745 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
4746 to be removed, we cannot set its TREE_CHAIN to NULL.
4747 Otherwise, we would not be able to find all the other fields
4748 in the other instances of this TREE_TYPE.
4750 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
4751 prev = NULL_TREE;
4752 member = TYPE_FIELDS (type);
4753 while (member)
4755 if (TREE_CODE (member) == FIELD_DECL
4756 || TREE_CODE (member) == TYPE_DECL)
4758 if (prev)
4759 TREE_CHAIN (prev) = member;
4760 else
4761 TYPE_FIELDS (type) = member;
4762 prev = member;
4765 member = TREE_CHAIN (member);
4768 if (prev)
4769 TREE_CHAIN (prev) = NULL_TREE;
4770 else
4771 TYPE_FIELDS (type) = NULL_TREE;
4773 TYPE_METHODS (type) = NULL_TREE;
4774 if (TYPE_BINFO (type))
4775 free_lang_data_in_binfo (TYPE_BINFO (type));
4777 else
4779 /* For non-aggregate types, clear out the language slot (which
4780 overloads TYPE_BINFO). */
4781 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
4783 if (INTEGRAL_TYPE_P (type)
4784 || SCALAR_FLOAT_TYPE_P (type)
4785 || FIXED_POINT_TYPE_P (type))
4787 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
4788 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
4792 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
4793 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
4795 if (TYPE_CONTEXT (type)
4796 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
4798 tree ctx = TYPE_CONTEXT (type);
4801 ctx = BLOCK_SUPERCONTEXT (ctx);
4803 while (ctx && TREE_CODE (ctx) == BLOCK);
4804 TYPE_CONTEXT (type) = ctx;
4809 /* Return true if DECL may need an assembler name to be set. */
4811 static inline bool
4812 need_assembler_name_p (tree decl)
4814 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
4815 if (TREE_CODE (decl) != FUNCTION_DECL
4816 && TREE_CODE (decl) != VAR_DECL)
4817 return false;
4819 /* If DECL already has its assembler name set, it does not need a
4820 new one. */
4821 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
4822 || DECL_ASSEMBLER_NAME_SET_P (decl))
4823 return false;
4825 /* Abstract decls do not need an assembler name. */
4826 if (DECL_ABSTRACT (decl))
4827 return false;
4829 /* For VAR_DECLs, only static, public and external symbols need an
4830 assembler name. */
4831 if (TREE_CODE (decl) == VAR_DECL
4832 && !TREE_STATIC (decl)
4833 && !TREE_PUBLIC (decl)
4834 && !DECL_EXTERNAL (decl))
4835 return false;
4837 if (TREE_CODE (decl) == FUNCTION_DECL)
4839 /* Do not set assembler name on builtins. Allow RTL expansion to
4840 decide whether to expand inline or via a regular call. */
4841 if (DECL_BUILT_IN (decl)
4842 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
4843 return false;
4845 /* Functions represented in the callgraph need an assembler name. */
4846 if (cgraph_get_node (decl) != NULL)
4847 return true;
4849 /* Unused and not public functions don't need an assembler name. */
4850 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
4851 return false;
4854 return true;
4858 /* Reset all language specific information still present in symbol
4859 DECL. */
4861 static void
4862 free_lang_data_in_decl (tree decl)
4864 gcc_assert (DECL_P (decl));
4866 /* Give the FE a chance to remove its own data first. */
4867 lang_hooks.free_lang_data (decl);
4869 TREE_LANG_FLAG_0 (decl) = 0;
4870 TREE_LANG_FLAG_1 (decl) = 0;
4871 TREE_LANG_FLAG_2 (decl) = 0;
4872 TREE_LANG_FLAG_3 (decl) = 0;
4873 TREE_LANG_FLAG_4 (decl) = 0;
4874 TREE_LANG_FLAG_5 (decl) = 0;
4875 TREE_LANG_FLAG_6 (decl) = 0;
4877 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
4878 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
4879 if (TREE_CODE (decl) == FIELD_DECL)
4881 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
4882 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
4883 DECL_QUALIFIER (decl) = NULL_TREE;
4886 if (TREE_CODE (decl) == FUNCTION_DECL)
4888 if (gimple_has_body_p (decl))
4890 tree t;
4892 /* If DECL has a gimple body, then the context for its
4893 arguments must be DECL. Otherwise, it doesn't really
4894 matter, as we will not be emitting any code for DECL. In
4895 general, there may be other instances of DECL created by
4896 the front end and since PARM_DECLs are generally shared,
4897 their DECL_CONTEXT changes as the replicas of DECL are
4898 created. The only time where DECL_CONTEXT is important
4899 is for the FUNCTION_DECLs that have a gimple body (since
4900 the PARM_DECL will be used in the function's body). */
4901 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
4902 DECL_CONTEXT (t) = decl;
4905 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
4906 At this point, it is not needed anymore. */
4907 DECL_SAVED_TREE (decl) = NULL_TREE;
4909 /* Clear the abstract origin if it refers to a method. Otherwise
4910 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
4911 origin will not be output correctly. */
4912 if (DECL_ABSTRACT_ORIGIN (decl)
4913 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
4914 && RECORD_OR_UNION_TYPE_P
4915 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
4916 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
4918 /* Sometimes the C++ frontend doesn't manage to transform a temporary
4919 DECL_VINDEX referring to itself into a vtable slot number as it
4920 should. Happens with functions that are copied and then forgotten
4921 about. Just clear it, it won't matter anymore. */
4922 if (DECL_VINDEX (decl) && !host_integerp (DECL_VINDEX (decl), 0))
4923 DECL_VINDEX (decl) = NULL_TREE;
4925 else if (TREE_CODE (decl) == VAR_DECL)
4927 if ((DECL_EXTERNAL (decl)
4928 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
4929 || (decl_function_context (decl) && !TREE_STATIC (decl)))
4930 DECL_INITIAL (decl) = NULL_TREE;
4932 else if (TREE_CODE (decl) == TYPE_DECL
4933 || TREE_CODE (decl) == FIELD_DECL)
4934 DECL_INITIAL (decl) = NULL_TREE;
4935 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
4936 && DECL_INITIAL (decl)
4937 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
4939 /* Strip builtins from the translation-unit BLOCK. We still have targets
4940 without builtin_decl_explicit support and also builtins are shared
4941 nodes and thus we can't use TREE_CHAIN in multiple lists. */
4942 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
4943 while (*nextp)
4945 tree var = *nextp;
4946 if (TREE_CODE (var) == FUNCTION_DECL
4947 && DECL_BUILT_IN (var))
4948 *nextp = TREE_CHAIN (var);
4949 else
4950 nextp = &TREE_CHAIN (var);
4956 /* Data used when collecting DECLs and TYPEs for language data removal. */
4958 struct free_lang_data_d
4960 /* Worklist to avoid excessive recursion. */
4961 vec<tree> worklist;
4963 /* Set of traversed objects. Used to avoid duplicate visits. */
4964 struct pointer_set_t *pset;
4966 /* Array of symbols to process with free_lang_data_in_decl. */
4967 vec<tree> decls;
4969 /* Array of types to process with free_lang_data_in_type. */
4970 vec<tree> types;
4974 /* Save all language fields needed to generate proper debug information
4975 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
4977 static void
4978 save_debug_info_for_decl (tree t)
4980 /*struct saved_debug_info_d *sdi;*/
4982 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
4984 /* FIXME. Partial implementation for saving debug info removed. */
4988 /* Save all language fields needed to generate proper debug information
4989 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
4991 static void
4992 save_debug_info_for_type (tree t)
4994 /*struct saved_debug_info_d *sdi;*/
4996 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
4998 /* FIXME. Partial implementation for saving debug info removed. */
5002 /* Add type or decl T to one of the list of tree nodes that need their
5003 language data removed. The lists are held inside FLD. */
5005 static void
5006 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
5008 if (DECL_P (t))
5010 fld->decls.safe_push (t);
5011 if (debug_info_level > DINFO_LEVEL_TERSE)
5012 save_debug_info_for_decl (t);
5014 else if (TYPE_P (t))
5016 fld->types.safe_push (t);
5017 if (debug_info_level > DINFO_LEVEL_TERSE)
5018 save_debug_info_for_type (t);
5020 else
5021 gcc_unreachable ();
5024 /* Push tree node T into FLD->WORKLIST. */
5026 static inline void
5027 fld_worklist_push (tree t, struct free_lang_data_d *fld)
5029 if (t && !is_lang_specific (t) && !pointer_set_contains (fld->pset, t))
5030 fld->worklist.safe_push ((t));
5034 /* Operand callback helper for free_lang_data_in_node. *TP is the
5035 subtree operand being considered. */
5037 static tree
5038 find_decls_types_r (tree *tp, int *ws, void *data)
5040 tree t = *tp;
5041 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
5043 if (TREE_CODE (t) == TREE_LIST)
5044 return NULL_TREE;
5046 /* Language specific nodes will be removed, so there is no need
5047 to gather anything under them. */
5048 if (is_lang_specific (t))
5050 *ws = 0;
5051 return NULL_TREE;
5054 if (DECL_P (t))
5056 /* Note that walk_tree does not traverse every possible field in
5057 decls, so we have to do our own traversals here. */
5058 add_tree_to_fld_list (t, fld);
5060 fld_worklist_push (DECL_NAME (t), fld);
5061 fld_worklist_push (DECL_CONTEXT (t), fld);
5062 fld_worklist_push (DECL_SIZE (t), fld);
5063 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
5065 /* We are going to remove everything under DECL_INITIAL for
5066 TYPE_DECLs. No point walking them. */
5067 if (TREE_CODE (t) != TYPE_DECL)
5068 fld_worklist_push (DECL_INITIAL (t), fld);
5070 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
5071 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
5073 if (TREE_CODE (t) == FUNCTION_DECL)
5075 fld_worklist_push (DECL_ARGUMENTS (t), fld);
5076 fld_worklist_push (DECL_RESULT (t), fld);
5078 else if (TREE_CODE (t) == TYPE_DECL)
5080 fld_worklist_push (DECL_ARGUMENT_FLD (t), fld);
5081 fld_worklist_push (DECL_VINDEX (t), fld);
5082 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
5084 else if (TREE_CODE (t) == FIELD_DECL)
5086 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
5087 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
5088 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
5089 fld_worklist_push (DECL_FCONTEXT (t), fld);
5091 else if (TREE_CODE (t) == VAR_DECL)
5093 fld_worklist_push (DECL_SECTION_NAME (t), fld);
5094 fld_worklist_push (DECL_COMDAT_GROUP (t), fld);
5097 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
5098 && DECL_HAS_VALUE_EXPR_P (t))
5099 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
5101 if (TREE_CODE (t) != FIELD_DECL
5102 && TREE_CODE (t) != TYPE_DECL)
5103 fld_worklist_push (TREE_CHAIN (t), fld);
5104 *ws = 0;
5106 else if (TYPE_P (t))
5108 /* Note that walk_tree does not traverse every possible field in
5109 types, so we have to do our own traversals here. */
5110 add_tree_to_fld_list (t, fld);
5112 if (!RECORD_OR_UNION_TYPE_P (t))
5113 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
5114 fld_worklist_push (TYPE_SIZE (t), fld);
5115 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
5116 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
5117 fld_worklist_push (TYPE_POINTER_TO (t), fld);
5118 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
5119 fld_worklist_push (TYPE_NAME (t), fld);
5120 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
5121 them and thus do not and want not to reach unused pointer types
5122 this way. */
5123 if (!POINTER_TYPE_P (t))
5124 fld_worklist_push (TYPE_MINVAL (t), fld);
5125 if (!RECORD_OR_UNION_TYPE_P (t))
5126 fld_worklist_push (TYPE_MAXVAL (t), fld);
5127 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
5128 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
5129 do not and want not to reach unused variants this way. */
5130 if (TYPE_CONTEXT (t))
5132 tree ctx = TYPE_CONTEXT (t);
5133 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
5134 So push that instead. */
5135 while (ctx && TREE_CODE (ctx) == BLOCK)
5136 ctx = BLOCK_SUPERCONTEXT (ctx);
5137 fld_worklist_push (ctx, fld);
5139 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
5140 and want not to reach unused types this way. */
5142 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
5144 unsigned i;
5145 tree tem;
5146 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
5147 fld_worklist_push (TREE_TYPE (tem), fld);
5148 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
5149 if (tem
5150 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
5151 && TREE_CODE (tem) == TREE_LIST)
5154 fld_worklist_push (TREE_VALUE (tem), fld);
5155 tem = TREE_CHAIN (tem);
5157 while (tem);
5159 if (RECORD_OR_UNION_TYPE_P (t))
5161 tree tem;
5162 /* Push all TYPE_FIELDS - there can be interleaving interesting
5163 and non-interesting things. */
5164 tem = TYPE_FIELDS (t);
5165 while (tem)
5167 if (TREE_CODE (tem) == FIELD_DECL
5168 || TREE_CODE (tem) == TYPE_DECL)
5169 fld_worklist_push (tem, fld);
5170 tem = TREE_CHAIN (tem);
5174 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5175 *ws = 0;
5177 else if (TREE_CODE (t) == BLOCK)
5179 tree tem;
5180 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5181 fld_worklist_push (tem, fld);
5182 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5183 fld_worklist_push (tem, fld);
5184 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5187 if (TREE_CODE (t) != IDENTIFIER_NODE
5188 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5189 fld_worklist_push (TREE_TYPE (t), fld);
5191 return NULL_TREE;
5195 /* Find decls and types in T. */
5197 static void
5198 find_decls_types (tree t, struct free_lang_data_d *fld)
5200 while (1)
5202 if (!pointer_set_contains (fld->pset, t))
5203 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5204 if (fld->worklist.is_empty ())
5205 break;
5206 t = fld->worklist.pop ();
5210 /* Translate all the types in LIST with the corresponding runtime
5211 types. */
5213 static tree
5214 get_eh_types_for_runtime (tree list)
5216 tree head, prev;
5218 if (list == NULL_TREE)
5219 return NULL_TREE;
5221 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5222 prev = head;
5223 list = TREE_CHAIN (list);
5224 while (list)
5226 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5227 TREE_CHAIN (prev) = n;
5228 prev = TREE_CHAIN (prev);
5229 list = TREE_CHAIN (list);
5232 return head;
5236 /* Find decls and types referenced in EH region R and store them in
5237 FLD->DECLS and FLD->TYPES. */
5239 static void
5240 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5242 switch (r->type)
5244 case ERT_CLEANUP:
5245 break;
5247 case ERT_TRY:
5249 eh_catch c;
5251 /* The types referenced in each catch must first be changed to the
5252 EH types used at runtime. This removes references to FE types
5253 in the region. */
5254 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5256 c->type_list = get_eh_types_for_runtime (c->type_list);
5257 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5260 break;
5262 case ERT_ALLOWED_EXCEPTIONS:
5263 r->u.allowed.type_list
5264 = get_eh_types_for_runtime (r->u.allowed.type_list);
5265 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5266 break;
5268 case ERT_MUST_NOT_THROW:
5269 walk_tree (&r->u.must_not_throw.failure_decl,
5270 find_decls_types_r, fld, fld->pset);
5271 break;
5276 /* Find decls and types referenced in cgraph node N and store them in
5277 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5278 look for *every* kind of DECL and TYPE node reachable from N,
5279 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5280 NAMESPACE_DECLs, etc). */
5282 static void
5283 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5285 basic_block bb;
5286 struct function *fn;
5287 unsigned ix;
5288 tree t;
5290 find_decls_types (n->symbol.decl, fld);
5292 if (!gimple_has_body_p (n->symbol.decl))
5293 return;
5295 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5297 fn = DECL_STRUCT_FUNCTION (n->symbol.decl);
5299 /* Traverse locals. */
5300 FOR_EACH_LOCAL_DECL (fn, ix, t)
5301 find_decls_types (t, fld);
5303 /* Traverse EH regions in FN. */
5305 eh_region r;
5306 FOR_ALL_EH_REGION_FN (r, fn)
5307 find_decls_types_in_eh_region (r, fld);
5310 /* Traverse every statement in FN. */
5311 FOR_EACH_BB_FN (bb, fn)
5313 gimple_stmt_iterator si;
5314 unsigned i;
5316 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5318 gimple phi = gsi_stmt (si);
5320 for (i = 0; i < gimple_phi_num_args (phi); i++)
5322 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5323 find_decls_types (*arg_p, fld);
5327 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5329 gimple stmt = gsi_stmt (si);
5331 if (is_gimple_call (stmt))
5332 find_decls_types (gimple_call_fntype (stmt), fld);
5334 for (i = 0; i < gimple_num_ops (stmt); i++)
5336 tree arg = gimple_op (stmt, i);
5337 find_decls_types (arg, fld);
5344 /* Find decls and types referenced in varpool node N and store them in
5345 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5346 look for *every* kind of DECL and TYPE node reachable from N,
5347 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5348 NAMESPACE_DECLs, etc). */
5350 static void
5351 find_decls_types_in_var (struct varpool_node *v, struct free_lang_data_d *fld)
5353 find_decls_types (v->symbol.decl, fld);
5356 /* If T needs an assembler name, have one created for it. */
5358 void
5359 assign_assembler_name_if_neeeded (tree t)
5361 if (need_assembler_name_p (t))
5363 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5364 diagnostics that use input_location to show locus
5365 information. The problem here is that, at this point,
5366 input_location is generally anchored to the end of the file
5367 (since the parser is long gone), so we don't have a good
5368 position to pin it to.
5370 To alleviate this problem, this uses the location of T's
5371 declaration. Examples of this are
5372 testsuite/g++.dg/template/cond2.C and
5373 testsuite/g++.dg/template/pr35240.C. */
5374 location_t saved_location = input_location;
5375 input_location = DECL_SOURCE_LOCATION (t);
5377 decl_assembler_name (t);
5379 input_location = saved_location;
5384 /* Free language specific information for every operand and expression
5385 in every node of the call graph. This process operates in three stages:
5387 1- Every callgraph node and varpool node is traversed looking for
5388 decls and types embedded in them. This is a more exhaustive
5389 search than that done by find_referenced_vars, because it will
5390 also collect individual fields, decls embedded in types, etc.
5392 2- All the decls found are sent to free_lang_data_in_decl.
5394 3- All the types found are sent to free_lang_data_in_type.
5396 The ordering between decls and types is important because
5397 free_lang_data_in_decl sets assembler names, which includes
5398 mangling. So types cannot be freed up until assembler names have
5399 been set up. */
5401 static void
5402 free_lang_data_in_cgraph (void)
5404 struct cgraph_node *n;
5405 struct varpool_node *v;
5406 struct free_lang_data_d fld;
5407 tree t;
5408 unsigned i;
5409 alias_pair *p;
5411 /* Initialize sets and arrays to store referenced decls and types. */
5412 fld.pset = pointer_set_create ();
5413 fld.worklist.create (0);
5414 fld.decls.create (100);
5415 fld.types.create (100);
5417 /* Find decls and types in the body of every function in the callgraph. */
5418 FOR_EACH_FUNCTION (n)
5419 find_decls_types_in_node (n, &fld);
5421 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5422 find_decls_types (p->decl, &fld);
5424 /* Find decls and types in every varpool symbol. */
5425 FOR_EACH_VARIABLE (v)
5426 find_decls_types_in_var (v, &fld);
5428 /* Set the assembler name on every decl found. We need to do this
5429 now because free_lang_data_in_decl will invalidate data needed
5430 for mangling. This breaks mangling on interdependent decls. */
5431 FOR_EACH_VEC_ELT (fld.decls, i, t)
5432 assign_assembler_name_if_neeeded (t);
5434 /* Traverse every decl found freeing its language data. */
5435 FOR_EACH_VEC_ELT (fld.decls, i, t)
5436 free_lang_data_in_decl (t);
5438 /* Traverse every type found freeing its language data. */
5439 FOR_EACH_VEC_ELT (fld.types, i, t)
5440 free_lang_data_in_type (t);
5442 pointer_set_destroy (fld.pset);
5443 fld.worklist.release ();
5444 fld.decls.release ();
5445 fld.types.release ();
5449 /* Free resources that are used by FE but are not needed once they are done. */
5451 static unsigned
5452 free_lang_data (void)
5454 unsigned i;
5456 /* If we are the LTO frontend we have freed lang-specific data already. */
5457 if (in_lto_p
5458 || !flag_generate_lto)
5459 return 0;
5461 /* Allocate and assign alias sets to the standard integer types
5462 while the slots are still in the way the frontends generated them. */
5463 for (i = 0; i < itk_none; ++i)
5464 if (integer_types[i])
5465 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5467 /* Traverse the IL resetting language specific information for
5468 operands, expressions, etc. */
5469 free_lang_data_in_cgraph ();
5471 /* Create gimple variants for common types. */
5472 ptrdiff_type_node = integer_type_node;
5473 fileptr_type_node = ptr_type_node;
5475 /* Reset some langhooks. Do not reset types_compatible_p, it may
5476 still be used indirectly via the get_alias_set langhook. */
5477 lang_hooks.dwarf_name = lhd_dwarf_name;
5478 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5479 /* We do not want the default decl_assembler_name implementation,
5480 rather if we have fixed everything we want a wrapper around it
5481 asserting that all non-local symbols already got their assembler
5482 name and only produce assembler names for local symbols. Or rather
5483 make sure we never call decl_assembler_name on local symbols and
5484 devise a separate, middle-end private scheme for it. */
5486 /* Reset diagnostic machinery. */
5487 tree_diagnostics_defaults (global_dc);
5489 return 0;
5493 struct simple_ipa_opt_pass pass_ipa_free_lang_data =
5496 SIMPLE_IPA_PASS,
5497 "*free_lang_data", /* name */
5498 OPTGROUP_NONE, /* optinfo_flags */
5499 NULL, /* gate */
5500 free_lang_data, /* execute */
5501 NULL, /* sub */
5502 NULL, /* next */
5503 0, /* static_pass_number */
5504 TV_IPA_FREE_LANG_DATA, /* tv_id */
5505 0, /* properties_required */
5506 0, /* properties_provided */
5507 0, /* properties_destroyed */
5508 0, /* todo_flags_start */
5509 0 /* todo_flags_finish */
5513 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
5514 ATTR_NAME. Also used internally by remove_attribute(). */
5515 bool
5516 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
5518 size_t ident_len = IDENTIFIER_LENGTH (ident);
5520 if (ident_len == attr_len)
5522 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
5523 return true;
5525 else if (ident_len == attr_len + 4)
5527 /* There is the possibility that ATTR is 'text' and IDENT is
5528 '__text__'. */
5529 const char *p = IDENTIFIER_POINTER (ident);
5530 if (p[0] == '_' && p[1] == '_'
5531 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5532 && strncmp (attr_name, p + 2, attr_len) == 0)
5533 return true;
5536 return false;
5539 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
5540 of ATTR_NAME, and LIST is not NULL_TREE. */
5541 tree
5542 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
5544 while (list)
5546 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5548 if (ident_len == attr_len)
5550 if (!strcmp (attr_name,
5551 IDENTIFIER_POINTER (get_attribute_name (list))))
5552 break;
5554 /* TODO: If we made sure that attributes were stored in the
5555 canonical form without '__...__' (ie, as in 'text' as opposed
5556 to '__text__') then we could avoid the following case. */
5557 else if (ident_len == attr_len + 4)
5559 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5560 if (p[0] == '_' && p[1] == '_'
5561 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5562 && strncmp (attr_name, p + 2, attr_len) == 0)
5563 break;
5565 list = TREE_CHAIN (list);
5568 return list;
5571 /* A variant of lookup_attribute() that can be used with an identifier
5572 as the first argument, and where the identifier can be either
5573 'text' or '__text__'.
5575 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
5576 return a pointer to the attribute's list element if the attribute
5577 is part of the list, or NULL_TREE if not found. If the attribute
5578 appears more than once, this only returns the first occurrence; the
5579 TREE_CHAIN of the return value should be passed back in if further
5580 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
5581 can be in the form 'text' or '__text__'. */
5582 static tree
5583 lookup_ident_attribute (tree attr_identifier, tree list)
5585 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
5587 while (list)
5589 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
5590 == IDENTIFIER_NODE);
5592 /* Identifiers can be compared directly for equality. */
5593 if (attr_identifier == get_attribute_name (list))
5594 break;
5596 /* If they are not equal, they may still be one in the form
5597 'text' while the other one is in the form '__text__'. TODO:
5598 If we were storing attributes in normalized 'text' form, then
5599 this could all go away and we could take full advantage of
5600 the fact that we're comparing identifiers. :-) */
5602 size_t attr_len = IDENTIFIER_LENGTH (attr_identifier);
5603 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5605 if (ident_len == attr_len + 4)
5607 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5608 const char *q = IDENTIFIER_POINTER (attr_identifier);
5609 if (p[0] == '_' && p[1] == '_'
5610 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5611 && strncmp (q, p + 2, attr_len) == 0)
5612 break;
5614 else if (ident_len + 4 == attr_len)
5616 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5617 const char *q = IDENTIFIER_POINTER (attr_identifier);
5618 if (q[0] == '_' && q[1] == '_'
5619 && q[attr_len - 2] == '_' && q[attr_len - 1] == '_'
5620 && strncmp (q + 2, p, ident_len) == 0)
5621 break;
5624 list = TREE_CHAIN (list);
5627 return list;
5630 /* Remove any instances of attribute ATTR_NAME in LIST and return the
5631 modified list. */
5633 tree
5634 remove_attribute (const char *attr_name, tree list)
5636 tree *p;
5637 size_t attr_len = strlen (attr_name);
5639 gcc_checking_assert (attr_name[0] != '_');
5641 for (p = &list; *p; )
5643 tree l = *p;
5644 /* TODO: If we were storing attributes in normalized form, here
5645 we could use a simple strcmp(). */
5646 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
5647 *p = TREE_CHAIN (l);
5648 else
5649 p = &TREE_CHAIN (l);
5652 return list;
5655 /* Return an attribute list that is the union of a1 and a2. */
5657 tree
5658 merge_attributes (tree a1, tree a2)
5660 tree attributes;
5662 /* Either one unset? Take the set one. */
5664 if ((attributes = a1) == 0)
5665 attributes = a2;
5667 /* One that completely contains the other? Take it. */
5669 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
5671 if (attribute_list_contained (a2, a1))
5672 attributes = a2;
5673 else
5675 /* Pick the longest list, and hang on the other list. */
5677 if (list_length (a1) < list_length (a2))
5678 attributes = a2, a2 = a1;
5680 for (; a2 != 0; a2 = TREE_CHAIN (a2))
5682 tree a;
5683 for (a = lookup_ident_attribute (get_attribute_name (a2),
5684 attributes);
5685 a != NULL_TREE && !attribute_value_equal (a, a2);
5686 a = lookup_ident_attribute (get_attribute_name (a2),
5687 TREE_CHAIN (a)))
5689 if (a == NULL_TREE)
5691 a1 = copy_node (a2);
5692 TREE_CHAIN (a1) = attributes;
5693 attributes = a1;
5698 return attributes;
5701 /* Given types T1 and T2, merge their attributes and return
5702 the result. */
5704 tree
5705 merge_type_attributes (tree t1, tree t2)
5707 return merge_attributes (TYPE_ATTRIBUTES (t1),
5708 TYPE_ATTRIBUTES (t2));
5711 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
5712 the result. */
5714 tree
5715 merge_decl_attributes (tree olddecl, tree newdecl)
5717 return merge_attributes (DECL_ATTRIBUTES (olddecl),
5718 DECL_ATTRIBUTES (newdecl));
5721 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
5723 /* Specialization of merge_decl_attributes for various Windows targets.
5725 This handles the following situation:
5727 __declspec (dllimport) int foo;
5728 int foo;
5730 The second instance of `foo' nullifies the dllimport. */
5732 tree
5733 merge_dllimport_decl_attributes (tree old, tree new_tree)
5735 tree a;
5736 int delete_dllimport_p = 1;
5738 /* What we need to do here is remove from `old' dllimport if it doesn't
5739 appear in `new'. dllimport behaves like extern: if a declaration is
5740 marked dllimport and a definition appears later, then the object
5741 is not dllimport'd. We also remove a `new' dllimport if the old list
5742 contains dllexport: dllexport always overrides dllimport, regardless
5743 of the order of declaration. */
5744 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
5745 delete_dllimport_p = 0;
5746 else if (DECL_DLLIMPORT_P (new_tree)
5747 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
5749 DECL_DLLIMPORT_P (new_tree) = 0;
5750 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
5751 "dllimport ignored", new_tree);
5753 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
5755 /* Warn about overriding a symbol that has already been used, e.g.:
5756 extern int __attribute__ ((dllimport)) foo;
5757 int* bar () {return &foo;}
5758 int foo;
5760 if (TREE_USED (old))
5762 warning (0, "%q+D redeclared without dllimport attribute "
5763 "after being referenced with dll linkage", new_tree);
5764 /* If we have used a variable's address with dllimport linkage,
5765 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
5766 decl may already have had TREE_CONSTANT computed.
5767 We still remove the attribute so that assembler code refers
5768 to '&foo rather than '_imp__foo'. */
5769 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
5770 DECL_DLLIMPORT_P (new_tree) = 1;
5773 /* Let an inline definition silently override the external reference,
5774 but otherwise warn about attribute inconsistency. */
5775 else if (TREE_CODE (new_tree) == VAR_DECL
5776 || !DECL_DECLARED_INLINE_P (new_tree))
5777 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
5778 "previous dllimport ignored", new_tree);
5780 else
5781 delete_dllimport_p = 0;
5783 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
5785 if (delete_dllimport_p)
5786 a = remove_attribute ("dllimport", a);
5788 return a;
5791 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
5792 struct attribute_spec.handler. */
5794 tree
5795 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
5796 bool *no_add_attrs)
5798 tree node = *pnode;
5799 bool is_dllimport;
5801 /* These attributes may apply to structure and union types being created,
5802 but otherwise should pass to the declaration involved. */
5803 if (!DECL_P (node))
5805 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
5806 | (int) ATTR_FLAG_ARRAY_NEXT))
5808 *no_add_attrs = true;
5809 return tree_cons (name, args, NULL_TREE);
5811 if (TREE_CODE (node) == RECORD_TYPE
5812 || TREE_CODE (node) == UNION_TYPE)
5814 node = TYPE_NAME (node);
5815 if (!node)
5816 return NULL_TREE;
5818 else
5820 warning (OPT_Wattributes, "%qE attribute ignored",
5821 name);
5822 *no_add_attrs = true;
5823 return NULL_TREE;
5827 if (TREE_CODE (node) != FUNCTION_DECL
5828 && TREE_CODE (node) != VAR_DECL
5829 && TREE_CODE (node) != TYPE_DECL)
5831 *no_add_attrs = true;
5832 warning (OPT_Wattributes, "%qE attribute ignored",
5833 name);
5834 return NULL_TREE;
5837 if (TREE_CODE (node) == TYPE_DECL
5838 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
5839 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
5841 *no_add_attrs = true;
5842 warning (OPT_Wattributes, "%qE attribute ignored",
5843 name);
5844 return NULL_TREE;
5847 is_dllimport = is_attribute_p ("dllimport", name);
5849 /* Report error on dllimport ambiguities seen now before they cause
5850 any damage. */
5851 if (is_dllimport)
5853 /* Honor any target-specific overrides. */
5854 if (!targetm.valid_dllimport_attribute_p (node))
5855 *no_add_attrs = true;
5857 else if (TREE_CODE (node) == FUNCTION_DECL
5858 && DECL_DECLARED_INLINE_P (node))
5860 warning (OPT_Wattributes, "inline function %q+D declared as "
5861 " dllimport: attribute ignored", node);
5862 *no_add_attrs = true;
5864 /* Like MS, treat definition of dllimported variables and
5865 non-inlined functions on declaration as syntax errors. */
5866 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
5868 error ("function %q+D definition is marked dllimport", node);
5869 *no_add_attrs = true;
5872 else if (TREE_CODE (node) == VAR_DECL)
5874 if (DECL_INITIAL (node))
5876 error ("variable %q+D definition is marked dllimport",
5877 node);
5878 *no_add_attrs = true;
5881 /* `extern' needn't be specified with dllimport.
5882 Specify `extern' now and hope for the best. Sigh. */
5883 DECL_EXTERNAL (node) = 1;
5884 /* Also, implicitly give dllimport'd variables declared within
5885 a function global scope, unless declared static. */
5886 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
5887 TREE_PUBLIC (node) = 1;
5890 if (*no_add_attrs == false)
5891 DECL_DLLIMPORT_P (node) = 1;
5893 else if (TREE_CODE (node) == FUNCTION_DECL
5894 && DECL_DECLARED_INLINE_P (node)
5895 && flag_keep_inline_dllexport)
5896 /* An exported function, even if inline, must be emitted. */
5897 DECL_EXTERNAL (node) = 0;
5899 /* Report error if symbol is not accessible at global scope. */
5900 if (!TREE_PUBLIC (node)
5901 && (TREE_CODE (node) == VAR_DECL
5902 || TREE_CODE (node) == FUNCTION_DECL))
5904 error ("external linkage required for symbol %q+D because of "
5905 "%qE attribute", node, name);
5906 *no_add_attrs = true;
5909 /* A dllexport'd entity must have default visibility so that other
5910 program units (shared libraries or the main executable) can see
5911 it. A dllimport'd entity must have default visibility so that
5912 the linker knows that undefined references within this program
5913 unit can be resolved by the dynamic linker. */
5914 if (!*no_add_attrs)
5916 if (DECL_VISIBILITY_SPECIFIED (node)
5917 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
5918 error ("%qE implies default visibility, but %qD has already "
5919 "been declared with a different visibility",
5920 name, node);
5921 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
5922 DECL_VISIBILITY_SPECIFIED (node) = 1;
5925 return NULL_TREE;
5928 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
5930 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
5931 of the various TYPE_QUAL values. */
5933 static void
5934 set_type_quals (tree type, int type_quals)
5936 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
5937 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
5938 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
5939 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
5942 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
5944 bool
5945 check_qualified_type (const_tree cand, const_tree base, int type_quals)
5947 return (TYPE_QUALS (cand) == type_quals
5948 && TYPE_NAME (cand) == TYPE_NAME (base)
5949 /* Apparently this is needed for Objective-C. */
5950 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5951 /* Check alignment. */
5952 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
5953 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5954 TYPE_ATTRIBUTES (base)));
5957 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
5959 static bool
5960 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
5962 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
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) == align
5968 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5969 TYPE_ATTRIBUTES (base)));
5972 /* Return a version of the TYPE, qualified as indicated by the
5973 TYPE_QUALS, if one exists. If no qualified version exists yet,
5974 return NULL_TREE. */
5976 tree
5977 get_qualified_type (tree type, int type_quals)
5979 tree t;
5981 if (TYPE_QUALS (type) == type_quals)
5982 return type;
5984 /* Search the chain of variants to see if there is already one there just
5985 like the one we need to have. If so, use that existing one. We must
5986 preserve the TYPE_NAME, since there is code that depends on this. */
5987 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5988 if (check_qualified_type (t, type, type_quals))
5989 return t;
5991 return NULL_TREE;
5994 /* Like get_qualified_type, but creates the type if it does not
5995 exist. This function never returns NULL_TREE. */
5997 tree
5998 build_qualified_type (tree type, int type_quals)
6000 tree t;
6002 /* See if we already have the appropriate qualified variant. */
6003 t = get_qualified_type (type, type_quals);
6005 /* If not, build it. */
6006 if (!t)
6008 t = build_variant_type_copy (type);
6009 set_type_quals (t, type_quals);
6011 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6012 /* Propagate structural equality. */
6013 SET_TYPE_STRUCTURAL_EQUALITY (t);
6014 else if (TYPE_CANONICAL (type) != type)
6015 /* Build the underlying canonical type, since it is different
6016 from TYPE. */
6017 TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
6018 type_quals);
6019 else
6020 /* T is its own canonical type. */
6021 TYPE_CANONICAL (t) = t;
6025 return t;
6028 /* Create a variant of type T with alignment ALIGN. */
6030 tree
6031 build_aligned_type (tree type, unsigned int align)
6033 tree t;
6035 if (TYPE_PACKED (type)
6036 || TYPE_ALIGN (type) == align)
6037 return type;
6039 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6040 if (check_aligned_type (t, type, align))
6041 return t;
6043 t = build_variant_type_copy (type);
6044 TYPE_ALIGN (t) = align;
6046 return t;
6049 /* Create a new distinct copy of TYPE. The new type is made its own
6050 MAIN_VARIANT. If TYPE requires structural equality checks, the
6051 resulting type requires structural equality checks; otherwise, its
6052 TYPE_CANONICAL points to itself. */
6054 tree
6055 build_distinct_type_copy (tree type)
6057 tree t = copy_node (type);
6059 TYPE_POINTER_TO (t) = 0;
6060 TYPE_REFERENCE_TO (t) = 0;
6062 /* Set the canonical type either to a new equivalence class, or
6063 propagate the need for structural equality checks. */
6064 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6065 SET_TYPE_STRUCTURAL_EQUALITY (t);
6066 else
6067 TYPE_CANONICAL (t) = t;
6069 /* Make it its own variant. */
6070 TYPE_MAIN_VARIANT (t) = t;
6071 TYPE_NEXT_VARIANT (t) = 0;
6073 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
6074 whose TREE_TYPE is not t. This can also happen in the Ada
6075 frontend when using subtypes. */
6077 return t;
6080 /* Create a new variant of TYPE, equivalent but distinct. This is so
6081 the caller can modify it. TYPE_CANONICAL for the return type will
6082 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
6083 are considered equal by the language itself (or that both types
6084 require structural equality checks). */
6086 tree
6087 build_variant_type_copy (tree type)
6089 tree t, m = TYPE_MAIN_VARIANT (type);
6091 t = build_distinct_type_copy (type);
6093 /* Since we're building a variant, assume that it is a non-semantic
6094 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
6095 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
6097 /* Add the new type to the chain of variants of TYPE. */
6098 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
6099 TYPE_NEXT_VARIANT (m) = t;
6100 TYPE_MAIN_VARIANT (t) = m;
6102 return t;
6105 /* Return true if the from tree in both tree maps are equal. */
6108 tree_map_base_eq (const void *va, const void *vb)
6110 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6111 *const b = (const struct tree_map_base *) vb;
6112 return (a->from == b->from);
6115 /* Hash a from tree in a tree_base_map. */
6117 unsigned int
6118 tree_map_base_hash (const void *item)
6120 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6123 /* Return true if this tree map structure is marked for garbage collection
6124 purposes. We simply return true if the from tree is marked, so that this
6125 structure goes away when the from tree goes away. */
6128 tree_map_base_marked_p (const void *p)
6130 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6133 /* Hash a from tree in a tree_map. */
6135 unsigned int
6136 tree_map_hash (const void *item)
6138 return (((const struct tree_map *) item)->hash);
6141 /* Hash a from tree in a tree_decl_map. */
6143 unsigned int
6144 tree_decl_map_hash (const void *item)
6146 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6149 /* Return the initialization priority for DECL. */
6151 priority_type
6152 decl_init_priority_lookup (tree decl)
6154 struct tree_priority_map *h;
6155 struct tree_map_base in;
6157 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6158 in.from = decl;
6159 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6160 return h ? h->init : DEFAULT_INIT_PRIORITY;
6163 /* Return the finalization priority for DECL. */
6165 priority_type
6166 decl_fini_priority_lookup (tree decl)
6168 struct tree_priority_map *h;
6169 struct tree_map_base in;
6171 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6172 in.from = decl;
6173 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6174 return h ? h->fini : DEFAULT_INIT_PRIORITY;
6177 /* Return the initialization and finalization priority information for
6178 DECL. If there is no previous priority information, a freshly
6179 allocated structure is returned. */
6181 static struct tree_priority_map *
6182 decl_priority_info (tree decl)
6184 struct tree_priority_map in;
6185 struct tree_priority_map *h;
6186 void **loc;
6188 in.base.from = decl;
6189 loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
6190 h = (struct tree_priority_map *) *loc;
6191 if (!h)
6193 h = ggc_alloc_cleared_tree_priority_map ();
6194 *loc = h;
6195 h->base.from = decl;
6196 h->init = DEFAULT_INIT_PRIORITY;
6197 h->fini = DEFAULT_INIT_PRIORITY;
6200 return h;
6203 /* Set the initialization priority for DECL to PRIORITY. */
6205 void
6206 decl_init_priority_insert (tree decl, priority_type priority)
6208 struct tree_priority_map *h;
6210 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6211 if (priority == DEFAULT_INIT_PRIORITY)
6212 return;
6213 h = decl_priority_info (decl);
6214 h->init = priority;
6217 /* Set the finalization priority for DECL to PRIORITY. */
6219 void
6220 decl_fini_priority_insert (tree decl, priority_type priority)
6222 struct tree_priority_map *h;
6224 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6225 if (priority == DEFAULT_INIT_PRIORITY)
6226 return;
6227 h = decl_priority_info (decl);
6228 h->fini = priority;
6231 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6233 static void
6234 print_debug_expr_statistics (void)
6236 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6237 (long) htab_size (debug_expr_for_decl),
6238 (long) htab_elements (debug_expr_for_decl),
6239 htab_collisions (debug_expr_for_decl));
6242 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6244 static void
6245 print_value_expr_statistics (void)
6247 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6248 (long) htab_size (value_expr_for_decl),
6249 (long) htab_elements (value_expr_for_decl),
6250 htab_collisions (value_expr_for_decl));
6253 /* Lookup a debug expression for FROM, and return it if we find one. */
6255 tree
6256 decl_debug_expr_lookup (tree from)
6258 struct tree_decl_map *h, in;
6259 in.base.from = from;
6261 h = (struct tree_decl_map *)
6262 htab_find_with_hash (debug_expr_for_decl, &in, DECL_UID (from));
6263 if (h)
6264 return h->to;
6265 return NULL_TREE;
6268 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6270 void
6271 decl_debug_expr_insert (tree from, tree to)
6273 struct tree_decl_map *h;
6274 void **loc;
6276 h = ggc_alloc_tree_decl_map ();
6277 h->base.from = from;
6278 h->to = to;
6279 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, DECL_UID (from),
6280 INSERT);
6281 *(struct tree_decl_map **) loc = h;
6284 /* Lookup a value expression for FROM, and return it if we find one. */
6286 tree
6287 decl_value_expr_lookup (tree from)
6289 struct tree_decl_map *h, in;
6290 in.base.from = from;
6292 h = (struct tree_decl_map *)
6293 htab_find_with_hash (value_expr_for_decl, &in, DECL_UID (from));
6294 if (h)
6295 return h->to;
6296 return NULL_TREE;
6299 /* Insert a mapping FROM->TO in the value expression hashtable. */
6301 void
6302 decl_value_expr_insert (tree from, tree to)
6304 struct tree_decl_map *h;
6305 void **loc;
6307 h = ggc_alloc_tree_decl_map ();
6308 h->base.from = from;
6309 h->to = to;
6310 loc = htab_find_slot_with_hash (value_expr_for_decl, h, DECL_UID (from),
6311 INSERT);
6312 *(struct tree_decl_map **) loc = h;
6315 /* Lookup a vector of debug arguments for FROM, and return it if we
6316 find one. */
6318 vec<tree, va_gc> **
6319 decl_debug_args_lookup (tree from)
6321 struct tree_vec_map *h, in;
6323 if (!DECL_HAS_DEBUG_ARGS_P (from))
6324 return NULL;
6325 gcc_checking_assert (debug_args_for_decl != NULL);
6326 in.base.from = from;
6327 h = (struct tree_vec_map *)
6328 htab_find_with_hash (debug_args_for_decl, &in, DECL_UID (from));
6329 if (h)
6330 return &h->to;
6331 return NULL;
6334 /* Insert a mapping FROM->empty vector of debug arguments in the value
6335 expression hashtable. */
6337 vec<tree, va_gc> **
6338 decl_debug_args_insert (tree from)
6340 struct tree_vec_map *h;
6341 void **loc;
6343 if (DECL_HAS_DEBUG_ARGS_P (from))
6344 return decl_debug_args_lookup (from);
6345 if (debug_args_for_decl == NULL)
6346 debug_args_for_decl = htab_create_ggc (64, tree_vec_map_hash,
6347 tree_vec_map_eq, 0);
6348 h = ggc_alloc_tree_vec_map ();
6349 h->base.from = from;
6350 h->to = NULL;
6351 loc = htab_find_slot_with_hash (debug_args_for_decl, h, DECL_UID (from),
6352 INSERT);
6353 *(struct tree_vec_map **) loc = h;
6354 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6355 return &h->to;
6358 /* Hashing of types so that we don't make duplicates.
6359 The entry point is `type_hash_canon'. */
6361 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6362 with types in the TREE_VALUE slots), by adding the hash codes
6363 of the individual types. */
6365 static unsigned int
6366 type_hash_list (const_tree list, hashval_t hashcode)
6368 const_tree tail;
6370 for (tail = list; tail; tail = TREE_CHAIN (tail))
6371 if (TREE_VALUE (tail) != error_mark_node)
6372 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
6373 hashcode);
6375 return hashcode;
6378 /* These are the Hashtable callback functions. */
6380 /* Returns true iff the types are equivalent. */
6382 static int
6383 type_hash_eq (const void *va, const void *vb)
6385 const struct type_hash *const a = (const struct type_hash *) va,
6386 *const b = (const struct type_hash *) vb;
6388 /* First test the things that are the same for all types. */
6389 if (a->hash != b->hash
6390 || TREE_CODE (a->type) != TREE_CODE (b->type)
6391 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6392 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6393 TYPE_ATTRIBUTES (b->type))
6394 || (TREE_CODE (a->type) != COMPLEX_TYPE
6395 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6396 return 0;
6398 /* Be careful about comparing arrays before and after the element type
6399 has been completed; don't compare TYPE_ALIGN unless both types are
6400 complete. */
6401 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6402 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6403 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6404 return 0;
6406 switch (TREE_CODE (a->type))
6408 case VOID_TYPE:
6409 case COMPLEX_TYPE:
6410 case POINTER_TYPE:
6411 case REFERENCE_TYPE:
6412 case NULLPTR_TYPE:
6413 return 1;
6415 case VECTOR_TYPE:
6416 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6418 case ENUMERAL_TYPE:
6419 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6420 && !(TYPE_VALUES (a->type)
6421 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6422 && TYPE_VALUES (b->type)
6423 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6424 && type_list_equal (TYPE_VALUES (a->type),
6425 TYPE_VALUES (b->type))))
6426 return 0;
6428 /* ... fall through ... */
6430 case INTEGER_TYPE:
6431 case REAL_TYPE:
6432 case BOOLEAN_TYPE:
6433 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6434 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6435 TYPE_MAX_VALUE (b->type)))
6436 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6437 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6438 TYPE_MIN_VALUE (b->type))));
6440 case FIXED_POINT_TYPE:
6441 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6443 case OFFSET_TYPE:
6444 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6446 case METHOD_TYPE:
6447 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6448 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6449 || (TYPE_ARG_TYPES (a->type)
6450 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6451 && TYPE_ARG_TYPES (b->type)
6452 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6453 && type_list_equal (TYPE_ARG_TYPES (a->type),
6454 TYPE_ARG_TYPES (b->type)))))
6455 break;
6456 return 0;
6457 case ARRAY_TYPE:
6458 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6460 case RECORD_TYPE:
6461 case UNION_TYPE:
6462 case QUAL_UNION_TYPE:
6463 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6464 || (TYPE_FIELDS (a->type)
6465 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6466 && TYPE_FIELDS (b->type)
6467 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6468 && type_list_equal (TYPE_FIELDS (a->type),
6469 TYPE_FIELDS (b->type))));
6471 case FUNCTION_TYPE:
6472 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6473 || (TYPE_ARG_TYPES (a->type)
6474 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6475 && TYPE_ARG_TYPES (b->type)
6476 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6477 && type_list_equal (TYPE_ARG_TYPES (a->type),
6478 TYPE_ARG_TYPES (b->type))))
6479 break;
6480 return 0;
6482 default:
6483 return 0;
6486 if (lang_hooks.types.type_hash_eq != NULL)
6487 return lang_hooks.types.type_hash_eq (a->type, b->type);
6489 return 1;
6492 /* Return the cached hash value. */
6494 static hashval_t
6495 type_hash_hash (const void *item)
6497 return ((const struct type_hash *) item)->hash;
6500 /* Look in the type hash table for a type isomorphic to TYPE.
6501 If one is found, return it. Otherwise return 0. */
6503 static tree
6504 type_hash_lookup (hashval_t hashcode, tree type)
6506 struct type_hash *h, in;
6508 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6509 must call that routine before comparing TYPE_ALIGNs. */
6510 layout_type (type);
6512 in.hash = hashcode;
6513 in.type = type;
6515 h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
6516 hashcode);
6517 if (h)
6518 return h->type;
6519 return NULL_TREE;
6522 /* Add an entry to the type-hash-table
6523 for a type TYPE whose hash code is HASHCODE. */
6525 static void
6526 type_hash_add (hashval_t hashcode, tree type)
6528 struct type_hash *h;
6529 void **loc;
6531 h = ggc_alloc_type_hash ();
6532 h->hash = hashcode;
6533 h->type = type;
6534 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
6535 *loc = (void *)h;
6538 /* Given TYPE, and HASHCODE its hash code, return the canonical
6539 object for an identical type if one already exists.
6540 Otherwise, return TYPE, and record it as the canonical object.
6542 To use this function, first create a type of the sort you want.
6543 Then compute its hash code from the fields of the type that
6544 make it different from other similar types.
6545 Then call this function and use the value. */
6547 tree
6548 type_hash_canon (unsigned int hashcode, tree type)
6550 tree t1;
6552 /* The hash table only contains main variants, so ensure that's what we're
6553 being passed. */
6554 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6556 /* See if the type is in the hash table already. If so, return it.
6557 Otherwise, add the type. */
6558 t1 = type_hash_lookup (hashcode, type);
6559 if (t1 != 0)
6561 if (GATHER_STATISTICS)
6563 tree_code_counts[(int) TREE_CODE (type)]--;
6564 tree_node_counts[(int) t_kind]--;
6565 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
6567 return t1;
6569 else
6571 type_hash_add (hashcode, type);
6572 return type;
6576 /* See if the data pointed to by the type hash table is marked. We consider
6577 it marked if the type is marked or if a debug type number or symbol
6578 table entry has been made for the type. */
6580 static int
6581 type_hash_marked_p (const void *p)
6583 const_tree const type = ((const struct type_hash *) p)->type;
6585 return ggc_marked_p (type);
6588 static void
6589 print_type_hash_statistics (void)
6591 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6592 (long) htab_size (type_hash_table),
6593 (long) htab_elements (type_hash_table),
6594 htab_collisions (type_hash_table));
6597 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
6598 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
6599 by adding the hash codes of the individual attributes. */
6601 static unsigned int
6602 attribute_hash_list (const_tree list, hashval_t hashcode)
6604 const_tree tail;
6606 for (tail = list; tail; tail = TREE_CHAIN (tail))
6607 /* ??? Do we want to add in TREE_VALUE too? */
6608 hashcode = iterative_hash_object
6609 (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)), hashcode);
6610 return hashcode;
6613 /* Given two lists of attributes, return true if list l2 is
6614 equivalent to l1. */
6617 attribute_list_equal (const_tree l1, const_tree l2)
6619 if (l1 == l2)
6620 return 1;
6622 return attribute_list_contained (l1, l2)
6623 && attribute_list_contained (l2, l1);
6626 /* Given two lists of attributes, return true if list L2 is
6627 completely contained within L1. */
6628 /* ??? This would be faster if attribute names were stored in a canonicalized
6629 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
6630 must be used to show these elements are equivalent (which they are). */
6631 /* ??? It's not clear that attributes with arguments will always be handled
6632 correctly. */
6635 attribute_list_contained (const_tree l1, const_tree l2)
6637 const_tree t1, t2;
6639 /* First check the obvious, maybe the lists are identical. */
6640 if (l1 == l2)
6641 return 1;
6643 /* Maybe the lists are similar. */
6644 for (t1 = l1, t2 = l2;
6645 t1 != 0 && t2 != 0
6646 && get_attribute_name (t1) == get_attribute_name (t2)
6647 && TREE_VALUE (t1) == TREE_VALUE (t2);
6648 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6651 /* Maybe the lists are equal. */
6652 if (t1 == 0 && t2 == 0)
6653 return 1;
6655 for (; t2 != 0; t2 = TREE_CHAIN (t2))
6657 const_tree attr;
6658 /* This CONST_CAST is okay because lookup_attribute does not
6659 modify its argument and the return value is assigned to a
6660 const_tree. */
6661 for (attr = lookup_ident_attribute (get_attribute_name (t2), CONST_CAST_TREE(l1));
6662 attr != NULL_TREE && !attribute_value_equal (t2, attr);
6663 attr = lookup_ident_attribute (get_attribute_name (t2), TREE_CHAIN (attr)))
6666 if (attr == NULL_TREE)
6667 return 0;
6670 return 1;
6673 /* Given two lists of types
6674 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6675 return 1 if the lists contain the same types in the same order.
6676 Also, the TREE_PURPOSEs must match. */
6679 type_list_equal (const_tree l1, const_tree l2)
6681 const_tree t1, t2;
6683 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6684 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6685 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6686 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6687 && (TREE_TYPE (TREE_PURPOSE (t1))
6688 == TREE_TYPE (TREE_PURPOSE (t2))))))
6689 return 0;
6691 return t1 == t2;
6694 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6695 given by TYPE. If the argument list accepts variable arguments,
6696 then this function counts only the ordinary arguments. */
6699 type_num_arguments (const_tree type)
6701 int i = 0;
6702 tree t;
6704 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6705 /* If the function does not take a variable number of arguments,
6706 the last element in the list will have type `void'. */
6707 if (VOID_TYPE_P (TREE_VALUE (t)))
6708 break;
6709 else
6710 ++i;
6712 return i;
6715 /* Nonzero if integer constants T1 and T2
6716 represent the same constant value. */
6719 tree_int_cst_equal (const_tree t1, const_tree t2)
6721 if (t1 == t2)
6722 return 1;
6724 if (t1 == 0 || t2 == 0)
6725 return 0;
6727 if (TREE_CODE (t1) == INTEGER_CST
6728 && TREE_CODE (t2) == INTEGER_CST
6729 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6730 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
6731 return 1;
6733 return 0;
6736 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
6737 The precise way of comparison depends on their data type. */
6740 tree_int_cst_lt (const_tree t1, const_tree t2)
6742 if (t1 == t2)
6743 return 0;
6745 if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
6747 int t1_sgn = tree_int_cst_sgn (t1);
6748 int t2_sgn = tree_int_cst_sgn (t2);
6750 if (t1_sgn < t2_sgn)
6751 return 1;
6752 else if (t1_sgn > t2_sgn)
6753 return 0;
6754 /* Otherwise, both are non-negative, so we compare them as
6755 unsigned just in case one of them would overflow a signed
6756 type. */
6758 else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
6759 return INT_CST_LT (t1, t2);
6761 return INT_CST_LT_UNSIGNED (t1, t2);
6764 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
6767 tree_int_cst_compare (const_tree t1, const_tree t2)
6769 if (tree_int_cst_lt (t1, t2))
6770 return -1;
6771 else if (tree_int_cst_lt (t2, t1))
6772 return 1;
6773 else
6774 return 0;
6777 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
6778 the host. If POS is zero, the value can be represented in a single
6779 HOST_WIDE_INT. If POS is nonzero, the value must be non-negative and can
6780 be represented in a single unsigned HOST_WIDE_INT. */
6783 host_integerp (const_tree t, int pos)
6785 if (t == NULL_TREE)
6786 return 0;
6788 return (TREE_CODE (t) == INTEGER_CST
6789 && ((TREE_INT_CST_HIGH (t) == 0
6790 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
6791 || (! pos && TREE_INT_CST_HIGH (t) == -1
6792 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
6793 && !TYPE_UNSIGNED (TREE_TYPE (t)))
6794 || (pos && TREE_INT_CST_HIGH (t) == 0)));
6797 /* Return the HOST_WIDE_INT least significant bits of T if it is an
6798 INTEGER_CST and there is no overflow. POS is nonzero if the result must
6799 be non-negative. We must be able to satisfy the above conditions. */
6801 HOST_WIDE_INT
6802 tree_low_cst (const_tree t, int pos)
6804 gcc_assert (host_integerp (t, pos));
6805 return TREE_INT_CST_LOW (t);
6808 /* Return the HOST_WIDE_INT least significant bits of T, a sizetype
6809 kind INTEGER_CST. This makes sure to properly sign-extend the
6810 constant. */
6812 HOST_WIDE_INT
6813 size_low_cst (const_tree t)
6815 double_int d = tree_to_double_int (t);
6816 return d.sext (TYPE_PRECISION (TREE_TYPE (t))).low;
6819 /* Return the most significant (sign) bit of T. */
6822 tree_int_cst_sign_bit (const_tree t)
6824 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
6825 unsigned HOST_WIDE_INT w;
6827 if (bitno < HOST_BITS_PER_WIDE_INT)
6828 w = TREE_INT_CST_LOW (t);
6829 else
6831 w = TREE_INT_CST_HIGH (t);
6832 bitno -= HOST_BITS_PER_WIDE_INT;
6835 return (w >> bitno) & 1;
6838 /* Return an indication of the sign of the integer constant T.
6839 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
6840 Note that -1 will never be returned if T's type is unsigned. */
6843 tree_int_cst_sgn (const_tree t)
6845 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
6846 return 0;
6847 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
6848 return 1;
6849 else if (TREE_INT_CST_HIGH (t) < 0)
6850 return -1;
6851 else
6852 return 1;
6855 /* Return the minimum number of bits needed to represent VALUE in a
6856 signed or unsigned type, UNSIGNEDP says which. */
6858 unsigned int
6859 tree_int_cst_min_precision (tree value, bool unsignedp)
6861 /* If the value is negative, compute its negative minus 1. The latter
6862 adjustment is because the absolute value of the largest negative value
6863 is one larger than the largest positive value. This is equivalent to
6864 a bit-wise negation, so use that operation instead. */
6866 if (tree_int_cst_sgn (value) < 0)
6867 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
6869 /* Return the number of bits needed, taking into account the fact
6870 that we need one more bit for a signed than unsigned type.
6871 If value is 0 or -1, the minimum precision is 1 no matter
6872 whether unsignedp is true or false. */
6874 if (integer_zerop (value))
6875 return 1;
6876 else
6877 return tree_floor_log2 (value) + 1 + !unsignedp;
6880 /* Compare two constructor-element-type constants. Return 1 if the lists
6881 are known to be equal; otherwise return 0. */
6884 simple_cst_list_equal (const_tree l1, const_tree l2)
6886 while (l1 != NULL_TREE && l2 != NULL_TREE)
6888 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
6889 return 0;
6891 l1 = TREE_CHAIN (l1);
6892 l2 = TREE_CHAIN (l2);
6895 return l1 == l2;
6898 /* Return truthvalue of whether T1 is the same tree structure as T2.
6899 Return 1 if they are the same.
6900 Return 0 if they are understandably different.
6901 Return -1 if either contains tree structure not understood by
6902 this function. */
6905 simple_cst_equal (const_tree t1, const_tree t2)
6907 enum tree_code code1, code2;
6908 int cmp;
6909 int i;
6911 if (t1 == t2)
6912 return 1;
6913 if (t1 == 0 || t2 == 0)
6914 return 0;
6916 code1 = TREE_CODE (t1);
6917 code2 = TREE_CODE (t2);
6919 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
6921 if (CONVERT_EXPR_CODE_P (code2)
6922 || code2 == NON_LVALUE_EXPR)
6923 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6924 else
6925 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
6928 else if (CONVERT_EXPR_CODE_P (code2)
6929 || code2 == NON_LVALUE_EXPR)
6930 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
6932 if (code1 != code2)
6933 return 0;
6935 switch (code1)
6937 case INTEGER_CST:
6938 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6939 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
6941 case REAL_CST:
6942 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
6944 case FIXED_CST:
6945 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
6947 case STRING_CST:
6948 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
6949 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
6950 TREE_STRING_LENGTH (t1)));
6952 case CONSTRUCTOR:
6954 unsigned HOST_WIDE_INT idx;
6955 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
6956 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
6958 if (vec_safe_length (v1) != vec_safe_length (v2))
6959 return false;
6961 for (idx = 0; idx < vec_safe_length (v1); ++idx)
6962 /* ??? Should we handle also fields here? */
6963 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
6964 return false;
6965 return true;
6968 case SAVE_EXPR:
6969 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6971 case CALL_EXPR:
6972 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
6973 if (cmp <= 0)
6974 return cmp;
6975 if (call_expr_nargs (t1) != call_expr_nargs (t2))
6976 return 0;
6978 const_tree arg1, arg2;
6979 const_call_expr_arg_iterator iter1, iter2;
6980 for (arg1 = first_const_call_expr_arg (t1, &iter1),
6981 arg2 = first_const_call_expr_arg (t2, &iter2);
6982 arg1 && arg2;
6983 arg1 = next_const_call_expr_arg (&iter1),
6984 arg2 = next_const_call_expr_arg (&iter2))
6986 cmp = simple_cst_equal (arg1, arg2);
6987 if (cmp <= 0)
6988 return cmp;
6990 return arg1 == arg2;
6993 case TARGET_EXPR:
6994 /* Special case: if either target is an unallocated VAR_DECL,
6995 it means that it's going to be unified with whatever the
6996 TARGET_EXPR is really supposed to initialize, so treat it
6997 as being equivalent to anything. */
6998 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
6999 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
7000 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
7001 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
7002 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
7003 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
7004 cmp = 1;
7005 else
7006 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7008 if (cmp <= 0)
7009 return cmp;
7011 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
7013 case WITH_CLEANUP_EXPR:
7014 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7015 if (cmp <= 0)
7016 return cmp;
7018 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
7020 case COMPONENT_REF:
7021 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
7022 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7024 return 0;
7026 case VAR_DECL:
7027 case PARM_DECL:
7028 case CONST_DECL:
7029 case FUNCTION_DECL:
7030 return 0;
7032 default:
7033 break;
7036 /* This general rule works for most tree codes. All exceptions should be
7037 handled above. If this is a language-specific tree code, we can't
7038 trust what might be in the operand, so say we don't know
7039 the situation. */
7040 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
7041 return -1;
7043 switch (TREE_CODE_CLASS (code1))
7045 case tcc_unary:
7046 case tcc_binary:
7047 case tcc_comparison:
7048 case tcc_expression:
7049 case tcc_reference:
7050 case tcc_statement:
7051 cmp = 1;
7052 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
7054 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
7055 if (cmp <= 0)
7056 return cmp;
7059 return cmp;
7061 default:
7062 return -1;
7066 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
7067 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
7068 than U, respectively. */
7071 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
7073 if (tree_int_cst_sgn (t) < 0)
7074 return -1;
7075 else if (TREE_INT_CST_HIGH (t) != 0)
7076 return 1;
7077 else if (TREE_INT_CST_LOW (t) == u)
7078 return 0;
7079 else if (TREE_INT_CST_LOW (t) < u)
7080 return -1;
7081 else
7082 return 1;
7085 /* Return true if SIZE represents a constant size that is in bounds of
7086 what the middle-end and the backend accepts (covering not more than
7087 half of the address-space). */
7089 bool
7090 valid_constant_size_p (const_tree size)
7092 if (! host_integerp (size, 1)
7093 || TREE_OVERFLOW (size)
7094 || tree_int_cst_sign_bit (size) != 0)
7095 return false;
7096 return true;
7099 /* Return the precision of the type, or for a complex or vector type the
7100 precision of the type of its elements. */
7102 unsigned int
7103 element_precision (const_tree type)
7105 enum tree_code code = TREE_CODE (type);
7106 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
7107 type = TREE_TYPE (type);
7109 return TYPE_PRECISION (type);
7112 /* Return true if CODE represents an associative tree code. Otherwise
7113 return false. */
7114 bool
7115 associative_tree_code (enum tree_code code)
7117 switch (code)
7119 case BIT_IOR_EXPR:
7120 case BIT_AND_EXPR:
7121 case BIT_XOR_EXPR:
7122 case PLUS_EXPR:
7123 case MULT_EXPR:
7124 case MIN_EXPR:
7125 case MAX_EXPR:
7126 return true;
7128 default:
7129 break;
7131 return false;
7134 /* Return true if CODE represents a commutative tree code. Otherwise
7135 return false. */
7136 bool
7137 commutative_tree_code (enum tree_code code)
7139 switch (code)
7141 case PLUS_EXPR:
7142 case MULT_EXPR:
7143 case MULT_HIGHPART_EXPR:
7144 case MIN_EXPR:
7145 case MAX_EXPR:
7146 case BIT_IOR_EXPR:
7147 case BIT_XOR_EXPR:
7148 case BIT_AND_EXPR:
7149 case NE_EXPR:
7150 case EQ_EXPR:
7151 case UNORDERED_EXPR:
7152 case ORDERED_EXPR:
7153 case UNEQ_EXPR:
7154 case LTGT_EXPR:
7155 case TRUTH_AND_EXPR:
7156 case TRUTH_XOR_EXPR:
7157 case TRUTH_OR_EXPR:
7158 case WIDEN_MULT_EXPR:
7159 case VEC_WIDEN_MULT_HI_EXPR:
7160 case VEC_WIDEN_MULT_LO_EXPR:
7161 case VEC_WIDEN_MULT_EVEN_EXPR:
7162 case VEC_WIDEN_MULT_ODD_EXPR:
7163 return true;
7165 default:
7166 break;
7168 return false;
7171 /* Return true if CODE represents a ternary tree code for which the
7172 first two operands are commutative. Otherwise return false. */
7173 bool
7174 commutative_ternary_tree_code (enum tree_code code)
7176 switch (code)
7178 case WIDEN_MULT_PLUS_EXPR:
7179 case WIDEN_MULT_MINUS_EXPR:
7180 return true;
7182 default:
7183 break;
7185 return false;
7188 /* Generate a hash value for an expression. This can be used iteratively
7189 by passing a previous result as the VAL argument.
7191 This function is intended to produce the same hash for expressions which
7192 would compare equal using operand_equal_p. */
7194 hashval_t
7195 iterative_hash_expr (const_tree t, hashval_t val)
7197 int i;
7198 enum tree_code code;
7199 char tclass;
7201 if (t == NULL_TREE)
7202 return iterative_hash_hashval_t (0, val);
7204 code = TREE_CODE (t);
7206 switch (code)
7208 /* Alas, constants aren't shared, so we can't rely on pointer
7209 identity. */
7210 case INTEGER_CST:
7211 val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
7212 return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
7213 case REAL_CST:
7215 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7217 return iterative_hash_hashval_t (val2, val);
7219 case FIXED_CST:
7221 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7223 return iterative_hash_hashval_t (val2, val);
7225 case STRING_CST:
7226 return iterative_hash (TREE_STRING_POINTER (t),
7227 TREE_STRING_LENGTH (t), val);
7228 case COMPLEX_CST:
7229 val = iterative_hash_expr (TREE_REALPART (t), val);
7230 return iterative_hash_expr (TREE_IMAGPART (t), val);
7231 case VECTOR_CST:
7233 unsigned i;
7234 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7235 val = iterative_hash_expr (VECTOR_CST_ELT (t, i), val);
7236 return val;
7238 case SSA_NAME:
7239 /* We can just compare by pointer. */
7240 return iterative_hash_host_wide_int (SSA_NAME_VERSION (t), val);
7241 case PLACEHOLDER_EXPR:
7242 /* The node itself doesn't matter. */
7243 return val;
7244 case TREE_LIST:
7245 /* A list of expressions, for a CALL_EXPR or as the elements of a
7246 VECTOR_CST. */
7247 for (; t; t = TREE_CHAIN (t))
7248 val = iterative_hash_expr (TREE_VALUE (t), val);
7249 return val;
7250 case CONSTRUCTOR:
7252 unsigned HOST_WIDE_INT idx;
7253 tree field, value;
7254 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7256 val = iterative_hash_expr (field, val);
7257 val = iterative_hash_expr (value, val);
7259 return val;
7261 case MEM_REF:
7263 /* The type of the second operand is relevant, except for
7264 its top-level qualifiers. */
7265 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (t, 1)));
7267 val = iterative_hash_object (TYPE_HASH (type), val);
7269 /* We could use the standard hash computation from this point
7270 on. */
7271 val = iterative_hash_object (code, val);
7272 val = iterative_hash_expr (TREE_OPERAND (t, 1), val);
7273 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7274 return val;
7276 case FUNCTION_DECL:
7277 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7278 Otherwise nodes that compare equal according to operand_equal_p might
7279 get different hash codes. However, don't do this for machine specific
7280 or front end builtins, since the function code is overloaded in those
7281 cases. */
7282 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7283 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7285 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7286 code = TREE_CODE (t);
7288 /* FALL THROUGH */
7289 default:
7290 tclass = TREE_CODE_CLASS (code);
7292 if (tclass == tcc_declaration)
7294 /* DECL's have a unique ID */
7295 val = iterative_hash_host_wide_int (DECL_UID (t), val);
7297 else
7299 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7301 val = iterative_hash_object (code, val);
7303 /* Don't hash the type, that can lead to having nodes which
7304 compare equal according to operand_equal_p, but which
7305 have different hash codes. */
7306 if (CONVERT_EXPR_CODE_P (code)
7307 || code == NON_LVALUE_EXPR)
7309 /* Make sure to include signness in the hash computation. */
7310 val += TYPE_UNSIGNED (TREE_TYPE (t));
7311 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7314 else if (commutative_tree_code (code))
7316 /* It's a commutative expression. We want to hash it the same
7317 however it appears. We do this by first hashing both operands
7318 and then rehashing based on the order of their independent
7319 hashes. */
7320 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
7321 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
7322 hashval_t t;
7324 if (one > two)
7325 t = one, one = two, two = t;
7327 val = iterative_hash_hashval_t (one, val);
7328 val = iterative_hash_hashval_t (two, val);
7330 else
7331 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7332 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
7334 return val;
7338 /* Generate a hash value for a pair of expressions. This can be used
7339 iteratively by passing a previous result as the VAL argument.
7341 The same hash value is always returned for a given pair of expressions,
7342 regardless of the order in which they are presented. This is useful in
7343 hashing the operands of commutative functions. */
7345 hashval_t
7346 iterative_hash_exprs_commutative (const_tree t1,
7347 const_tree t2, hashval_t val)
7349 hashval_t one = iterative_hash_expr (t1, 0);
7350 hashval_t two = iterative_hash_expr (t2, 0);
7351 hashval_t t;
7353 if (one > two)
7354 t = one, one = two, two = t;
7355 val = iterative_hash_hashval_t (one, val);
7356 val = iterative_hash_hashval_t (two, val);
7358 return val;
7361 /* Constructors for pointer, array and function types.
7362 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7363 constructed by language-dependent code, not here.) */
7365 /* Construct, lay out and return the type of pointers to TO_TYPE with
7366 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7367 reference all of memory. If such a type has already been
7368 constructed, reuse it. */
7370 tree
7371 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
7372 bool can_alias_all)
7374 tree t;
7376 if (to_type == error_mark_node)
7377 return error_mark_node;
7379 /* If the pointed-to type has the may_alias attribute set, force
7380 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7381 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7382 can_alias_all = true;
7384 /* In some cases, languages will have things that aren't a POINTER_TYPE
7385 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7386 In that case, return that type without regard to the rest of our
7387 operands.
7389 ??? This is a kludge, but consistent with the way this function has
7390 always operated and there doesn't seem to be a good way to avoid this
7391 at the moment. */
7392 if (TYPE_POINTER_TO (to_type) != 0
7393 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7394 return TYPE_POINTER_TO (to_type);
7396 /* First, if we already have a type for pointers to TO_TYPE and it's
7397 the proper mode, use it. */
7398 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7399 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7400 return t;
7402 t = make_node (POINTER_TYPE);
7404 TREE_TYPE (t) = to_type;
7405 SET_TYPE_MODE (t, mode);
7406 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7407 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7408 TYPE_POINTER_TO (to_type) = t;
7410 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7411 SET_TYPE_STRUCTURAL_EQUALITY (t);
7412 else if (TYPE_CANONICAL (to_type) != to_type)
7413 TYPE_CANONICAL (t)
7414 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7415 mode, can_alias_all);
7417 /* Lay out the type. This function has many callers that are concerned
7418 with expression-construction, and this simplifies them all. */
7419 layout_type (t);
7421 return t;
7424 /* By default build pointers in ptr_mode. */
7426 tree
7427 build_pointer_type (tree to_type)
7429 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7430 : TYPE_ADDR_SPACE (to_type);
7431 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7432 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7435 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7437 tree
7438 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
7439 bool can_alias_all)
7441 tree t;
7443 if (to_type == error_mark_node)
7444 return error_mark_node;
7446 /* If the pointed-to type has the may_alias attribute set, force
7447 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7448 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7449 can_alias_all = true;
7451 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7452 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7453 In that case, return that type without regard to the rest of our
7454 operands.
7456 ??? This is a kludge, but consistent with the way this function has
7457 always operated and there doesn't seem to be a good way to avoid this
7458 at the moment. */
7459 if (TYPE_REFERENCE_TO (to_type) != 0
7460 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7461 return TYPE_REFERENCE_TO (to_type);
7463 /* First, if we already have a type for pointers to TO_TYPE and it's
7464 the proper mode, use it. */
7465 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7466 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7467 return t;
7469 t = make_node (REFERENCE_TYPE);
7471 TREE_TYPE (t) = to_type;
7472 SET_TYPE_MODE (t, mode);
7473 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7474 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7475 TYPE_REFERENCE_TO (to_type) = t;
7477 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7478 SET_TYPE_STRUCTURAL_EQUALITY (t);
7479 else if (TYPE_CANONICAL (to_type) != to_type)
7480 TYPE_CANONICAL (t)
7481 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7482 mode, can_alias_all);
7484 layout_type (t);
7486 return t;
7490 /* Build the node for the type of references-to-TO_TYPE by default
7491 in ptr_mode. */
7493 tree
7494 build_reference_type (tree to_type)
7496 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7497 : TYPE_ADDR_SPACE (to_type);
7498 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7499 return build_reference_type_for_mode (to_type, pointer_mode, false);
7502 /* Build a type that is compatible with t but has no cv quals anywhere
7503 in its type, thus
7505 const char *const *const * -> char ***. */
7507 tree
7508 build_type_no_quals (tree t)
7510 switch (TREE_CODE (t))
7512 case POINTER_TYPE:
7513 return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7514 TYPE_MODE (t),
7515 TYPE_REF_CAN_ALIAS_ALL (t));
7516 case REFERENCE_TYPE:
7517 return
7518 build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7519 TYPE_MODE (t),
7520 TYPE_REF_CAN_ALIAS_ALL (t));
7521 default:
7522 return TYPE_MAIN_VARIANT (t);
7526 #define MAX_INT_CACHED_PREC \
7527 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7528 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7530 /* Builds a signed or unsigned integer type of precision PRECISION.
7531 Used for C bitfields whose precision does not match that of
7532 built-in target types. */
7533 tree
7534 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7535 int unsignedp)
7537 tree itype, ret;
7539 if (unsignedp)
7540 unsignedp = MAX_INT_CACHED_PREC + 1;
7542 if (precision <= MAX_INT_CACHED_PREC)
7544 itype = nonstandard_integer_type_cache[precision + unsignedp];
7545 if (itype)
7546 return itype;
7549 itype = make_node (INTEGER_TYPE);
7550 TYPE_PRECISION (itype) = precision;
7552 if (unsignedp)
7553 fixup_unsigned_type (itype);
7554 else
7555 fixup_signed_type (itype);
7557 ret = itype;
7558 if (host_integerp (TYPE_MAX_VALUE (itype), 1))
7559 ret = type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
7560 if (precision <= MAX_INT_CACHED_PREC)
7561 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7563 return ret;
7566 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7567 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7568 is true, reuse such a type that has already been constructed. */
7570 static tree
7571 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7573 tree itype = make_node (INTEGER_TYPE);
7574 hashval_t hashcode = 0;
7576 TREE_TYPE (itype) = type;
7578 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7579 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7581 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7582 SET_TYPE_MODE (itype, TYPE_MODE (type));
7583 TYPE_SIZE (itype) = TYPE_SIZE (type);
7584 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7585 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7586 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7588 if (!shared)
7589 return itype;
7591 if ((TYPE_MIN_VALUE (itype)
7592 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7593 || (TYPE_MAX_VALUE (itype)
7594 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7596 /* Since we cannot reliably merge this type, we need to compare it using
7597 structural equality checks. */
7598 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7599 return itype;
7602 hashcode = iterative_hash_expr (TYPE_MIN_VALUE (itype), hashcode);
7603 hashcode = iterative_hash_expr (TYPE_MAX_VALUE (itype), hashcode);
7604 hashcode = iterative_hash_hashval_t (TYPE_HASH (type), hashcode);
7605 itype = type_hash_canon (hashcode, itype);
7607 return itype;
7610 /* Wrapper around build_range_type_1 with SHARED set to true. */
7612 tree
7613 build_range_type (tree type, tree lowval, tree highval)
7615 return build_range_type_1 (type, lowval, highval, true);
7618 /* Wrapper around build_range_type_1 with SHARED set to false. */
7620 tree
7621 build_nonshared_range_type (tree type, tree lowval, tree highval)
7623 return build_range_type_1 (type, lowval, highval, false);
7626 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7627 MAXVAL should be the maximum value in the domain
7628 (one less than the length of the array).
7630 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7631 We don't enforce this limit, that is up to caller (e.g. language front end).
7632 The limit exists because the result is a signed type and we don't handle
7633 sizes that use more than one HOST_WIDE_INT. */
7635 tree
7636 build_index_type (tree maxval)
7638 return build_range_type (sizetype, size_zero_node, maxval);
7641 /* Return true if the debug information for TYPE, a subtype, should be emitted
7642 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7643 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7644 debug info and doesn't reflect the source code. */
7646 bool
7647 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7649 tree base_type = TREE_TYPE (type), low, high;
7651 /* Subrange types have a base type which is an integral type. */
7652 if (!INTEGRAL_TYPE_P (base_type))
7653 return false;
7655 /* Get the real bounds of the subtype. */
7656 if (lang_hooks.types.get_subrange_bounds)
7657 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7658 else
7660 low = TYPE_MIN_VALUE (type);
7661 high = TYPE_MAX_VALUE (type);
7664 /* If the type and its base type have the same representation and the same
7665 name, then the type is not a subrange but a copy of the base type. */
7666 if ((TREE_CODE (base_type) == INTEGER_TYPE
7667 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7668 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7669 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7670 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type)))
7672 tree type_name = TYPE_NAME (type);
7673 tree base_type_name = TYPE_NAME (base_type);
7675 if (type_name && TREE_CODE (type_name) == TYPE_DECL)
7676 type_name = DECL_NAME (type_name);
7678 if (base_type_name && TREE_CODE (base_type_name) == TYPE_DECL)
7679 base_type_name = DECL_NAME (base_type_name);
7681 if (type_name == base_type_name)
7682 return false;
7685 if (lowval)
7686 *lowval = low;
7687 if (highval)
7688 *highval = high;
7689 return true;
7692 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7693 and number of elements specified by the range of values of INDEX_TYPE.
7694 If SHARED is true, reuse such a type that has already been constructed. */
7696 static tree
7697 build_array_type_1 (tree elt_type, tree index_type, bool shared)
7699 tree t;
7701 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7703 error ("arrays of functions are not meaningful");
7704 elt_type = integer_type_node;
7707 t = make_node (ARRAY_TYPE);
7708 TREE_TYPE (t) = elt_type;
7709 TYPE_DOMAIN (t) = index_type;
7710 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7711 layout_type (t);
7713 /* If the element type is incomplete at this point we get marked for
7714 structural equality. Do not record these types in the canonical
7715 type hashtable. */
7716 if (TYPE_STRUCTURAL_EQUALITY_P (t))
7717 return t;
7719 if (shared)
7721 hashval_t hashcode = iterative_hash_object (TYPE_HASH (elt_type), 0);
7722 if (index_type)
7723 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
7724 t = type_hash_canon (hashcode, t);
7727 if (TYPE_CANONICAL (t) == t)
7729 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7730 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7731 SET_TYPE_STRUCTURAL_EQUALITY (t);
7732 else if (TYPE_CANONICAL (elt_type) != elt_type
7733 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7734 TYPE_CANONICAL (t)
7735 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7736 index_type
7737 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7738 shared);
7741 return t;
7744 /* Wrapper around build_array_type_1 with SHARED set to true. */
7746 tree
7747 build_array_type (tree elt_type, tree index_type)
7749 return build_array_type_1 (elt_type, index_type, true);
7752 /* Wrapper around build_array_type_1 with SHARED set to false. */
7754 tree
7755 build_nonshared_array_type (tree elt_type, tree index_type)
7757 return build_array_type_1 (elt_type, index_type, false);
7760 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7761 sizetype. */
7763 tree
7764 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
7766 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7769 /* Recursively examines the array elements of TYPE, until a non-array
7770 element type is found. */
7772 tree
7773 strip_array_types (tree type)
7775 while (TREE_CODE (type) == ARRAY_TYPE)
7776 type = TREE_TYPE (type);
7778 return type;
7781 /* Computes the canonical argument types from the argument type list
7782 ARGTYPES.
7784 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7785 on entry to this function, or if any of the ARGTYPES are
7786 structural.
7788 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7789 true on entry to this function, or if any of the ARGTYPES are
7790 non-canonical.
7792 Returns a canonical argument list, which may be ARGTYPES when the
7793 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7794 true) or would not differ from ARGTYPES. */
7796 static tree
7797 maybe_canonicalize_argtypes(tree argtypes,
7798 bool *any_structural_p,
7799 bool *any_noncanonical_p)
7801 tree arg;
7802 bool any_noncanonical_argtypes_p = false;
7804 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7806 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7807 /* Fail gracefully by stating that the type is structural. */
7808 *any_structural_p = true;
7809 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7810 *any_structural_p = true;
7811 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7812 || TREE_PURPOSE (arg))
7813 /* If the argument has a default argument, we consider it
7814 non-canonical even though the type itself is canonical.
7815 That way, different variants of function and method types
7816 with default arguments will all point to the variant with
7817 no defaults as their canonical type. */
7818 any_noncanonical_argtypes_p = true;
7821 if (*any_structural_p)
7822 return argtypes;
7824 if (any_noncanonical_argtypes_p)
7826 /* Build the canonical list of argument types. */
7827 tree canon_argtypes = NULL_TREE;
7828 bool is_void = false;
7830 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7832 if (arg == void_list_node)
7833 is_void = true;
7834 else
7835 canon_argtypes = tree_cons (NULL_TREE,
7836 TYPE_CANONICAL (TREE_VALUE (arg)),
7837 canon_argtypes);
7840 canon_argtypes = nreverse (canon_argtypes);
7841 if (is_void)
7842 canon_argtypes = chainon (canon_argtypes, void_list_node);
7844 /* There is a non-canonical type. */
7845 *any_noncanonical_p = true;
7846 return canon_argtypes;
7849 /* The canonical argument types are the same as ARGTYPES. */
7850 return argtypes;
7853 /* Construct, lay out and return
7854 the type of functions returning type VALUE_TYPE
7855 given arguments of types ARG_TYPES.
7856 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7857 are data type nodes for the arguments of the function.
7858 If such a type has already been constructed, reuse it. */
7860 tree
7861 build_function_type (tree value_type, tree arg_types)
7863 tree t;
7864 hashval_t hashcode = 0;
7865 bool any_structural_p, any_noncanonical_p;
7866 tree canon_argtypes;
7868 if (TREE_CODE (value_type) == FUNCTION_TYPE)
7870 error ("function return type cannot be function");
7871 value_type = integer_type_node;
7874 /* Make a node of the sort we want. */
7875 t = make_node (FUNCTION_TYPE);
7876 TREE_TYPE (t) = value_type;
7877 TYPE_ARG_TYPES (t) = arg_types;
7879 /* If we already have such a type, use the old one. */
7880 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
7881 hashcode = type_hash_list (arg_types, hashcode);
7882 t = type_hash_canon (hashcode, t);
7884 /* Set up the canonical type. */
7885 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7886 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7887 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
7888 &any_structural_p,
7889 &any_noncanonical_p);
7890 if (any_structural_p)
7891 SET_TYPE_STRUCTURAL_EQUALITY (t);
7892 else if (any_noncanonical_p)
7893 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
7894 canon_argtypes);
7896 if (!COMPLETE_TYPE_P (t))
7897 layout_type (t);
7898 return t;
7901 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP and the
7902 return value if SKIP_RETURN is true. */
7904 static tree
7905 build_function_type_skip_args (tree orig_type, bitmap args_to_skip,
7906 bool skip_return)
7908 tree new_type = NULL;
7909 tree args, new_args = NULL, t;
7910 tree new_reversed;
7911 int i = 0;
7913 for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
7914 args = TREE_CHAIN (args), i++)
7915 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
7916 new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
7918 new_reversed = nreverse (new_args);
7919 if (args)
7921 if (new_reversed)
7922 TREE_CHAIN (new_args) = void_list_node;
7923 else
7924 new_reversed = void_list_node;
7927 /* Use copy_node to preserve as much as possible from original type
7928 (debug info, attribute lists etc.)
7929 Exception is METHOD_TYPEs must have THIS argument.
7930 When we are asked to remove it, we need to build new FUNCTION_TYPE
7931 instead. */
7932 if (TREE_CODE (orig_type) != METHOD_TYPE
7933 || !args_to_skip
7934 || !bitmap_bit_p (args_to_skip, 0))
7936 new_type = build_distinct_type_copy (orig_type);
7937 TYPE_ARG_TYPES (new_type) = new_reversed;
7939 else
7941 new_type
7942 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
7943 new_reversed));
7944 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
7947 if (skip_return)
7948 TREE_TYPE (new_type) = void_type_node;
7950 /* This is a new type, not a copy of an old type. Need to reassociate
7951 variants. We can handle everything except the main variant lazily. */
7952 t = TYPE_MAIN_VARIANT (orig_type);
7953 if (t != orig_type)
7955 t = build_function_type_skip_args (t, args_to_skip, skip_return);
7956 TYPE_MAIN_VARIANT (new_type) = t;
7957 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
7958 TYPE_NEXT_VARIANT (t) = new_type;
7960 else
7962 TYPE_MAIN_VARIANT (new_type) = new_type;
7963 TYPE_NEXT_VARIANT (new_type) = NULL;
7966 return new_type;
7969 /* Build variant of function decl ORIG_DECL skipping ARGS_TO_SKIP and the
7970 return value if SKIP_RETURN is true.
7972 Arguments from DECL_ARGUMENTS list can't be removed now, since they are
7973 linked by TREE_CHAIN directly. The caller is responsible for eliminating
7974 them when they are being duplicated (i.e. copy_arguments_for_versioning). */
7976 tree
7977 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip,
7978 bool skip_return)
7980 tree new_decl = copy_node (orig_decl);
7981 tree new_type;
7983 new_type = TREE_TYPE (orig_decl);
7984 if (prototype_p (new_type)
7985 || (skip_return && !VOID_TYPE_P (TREE_TYPE (new_type))))
7986 new_type
7987 = build_function_type_skip_args (new_type, args_to_skip, skip_return);
7988 TREE_TYPE (new_decl) = new_type;
7990 /* For declarations setting DECL_VINDEX (i.e. methods)
7991 we expect first argument to be THIS pointer. */
7992 if (args_to_skip && bitmap_bit_p (args_to_skip, 0))
7993 DECL_VINDEX (new_decl) = NULL_TREE;
7995 /* When signature changes, we need to clear builtin info. */
7996 if (DECL_BUILT_IN (new_decl)
7997 && args_to_skip
7998 && !bitmap_empty_p (args_to_skip))
8000 DECL_BUILT_IN_CLASS (new_decl) = NOT_BUILT_IN;
8001 DECL_FUNCTION_CODE (new_decl) = (enum built_in_function) 0;
8003 return new_decl;
8006 /* Build a function type. The RETURN_TYPE is the type returned by the
8007 function. If VAARGS is set, no void_type_node is appended to the
8008 the list. ARGP must be always be terminated be a NULL_TREE. */
8010 static tree
8011 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
8013 tree t, args, last;
8015 t = va_arg (argp, tree);
8016 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
8017 args = tree_cons (NULL_TREE, t, args);
8019 if (vaargs)
8021 last = args;
8022 if (args != NULL_TREE)
8023 args = nreverse (args);
8024 gcc_assert (last != void_list_node);
8026 else if (args == NULL_TREE)
8027 args = void_list_node;
8028 else
8030 last = args;
8031 args = nreverse (args);
8032 TREE_CHAIN (last) = void_list_node;
8034 args = build_function_type (return_type, args);
8036 return args;
8039 /* Build a function type. The RETURN_TYPE is the type returned by the
8040 function. If additional arguments are provided, they are
8041 additional argument types. The list of argument types must always
8042 be terminated by NULL_TREE. */
8044 tree
8045 build_function_type_list (tree return_type, ...)
8047 tree args;
8048 va_list p;
8050 va_start (p, return_type);
8051 args = build_function_type_list_1 (false, return_type, p);
8052 va_end (p);
8053 return args;
8056 /* Build a variable argument function type. The RETURN_TYPE is the
8057 type returned by the function. If additional arguments are provided,
8058 they are additional argument types. The list of argument types must
8059 always be terminated by NULL_TREE. */
8061 tree
8062 build_varargs_function_type_list (tree return_type, ...)
8064 tree args;
8065 va_list p;
8067 va_start (p, return_type);
8068 args = build_function_type_list_1 (true, return_type, p);
8069 va_end (p);
8071 return args;
8074 /* Build a function type. RETURN_TYPE is the type returned by the
8075 function; VAARGS indicates whether the function takes varargs. The
8076 function takes N named arguments, the types of which are provided in
8077 ARG_TYPES. */
8079 static tree
8080 build_function_type_array_1 (bool vaargs, tree return_type, int n,
8081 tree *arg_types)
8083 int i;
8084 tree t = vaargs ? NULL_TREE : void_list_node;
8086 for (i = n - 1; i >= 0; i--)
8087 t = tree_cons (NULL_TREE, arg_types[i], t);
8089 return build_function_type (return_type, t);
8092 /* Build a function type. RETURN_TYPE is the type returned by the
8093 function. The function takes N named arguments, the types of which
8094 are provided in ARG_TYPES. */
8096 tree
8097 build_function_type_array (tree return_type, int n, tree *arg_types)
8099 return build_function_type_array_1 (false, return_type, n, arg_types);
8102 /* Build a variable argument function type. RETURN_TYPE is the type
8103 returned by the function. The function takes N named arguments, the
8104 types of which are provided in ARG_TYPES. */
8106 tree
8107 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8109 return build_function_type_array_1 (true, return_type, n, arg_types);
8112 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8113 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8114 for the method. An implicit additional parameter (of type
8115 pointer-to-BASETYPE) is added to the ARGTYPES. */
8117 tree
8118 build_method_type_directly (tree basetype,
8119 tree rettype,
8120 tree argtypes)
8122 tree t;
8123 tree ptype;
8124 int hashcode = 0;
8125 bool any_structural_p, any_noncanonical_p;
8126 tree canon_argtypes;
8128 /* Make a node of the sort we want. */
8129 t = make_node (METHOD_TYPE);
8131 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8132 TREE_TYPE (t) = rettype;
8133 ptype = build_pointer_type (basetype);
8135 /* The actual arglist for this function includes a "hidden" argument
8136 which is "this". Put it into the list of argument types. */
8137 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8138 TYPE_ARG_TYPES (t) = argtypes;
8140 /* If we already have such a type, use the old one. */
8141 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8142 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
8143 hashcode = type_hash_list (argtypes, hashcode);
8144 t = type_hash_canon (hashcode, t);
8146 /* Set up the canonical type. */
8147 any_structural_p
8148 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8149 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8150 any_noncanonical_p
8151 = (TYPE_CANONICAL (basetype) != basetype
8152 || TYPE_CANONICAL (rettype) != rettype);
8153 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8154 &any_structural_p,
8155 &any_noncanonical_p);
8156 if (any_structural_p)
8157 SET_TYPE_STRUCTURAL_EQUALITY (t);
8158 else if (any_noncanonical_p)
8159 TYPE_CANONICAL (t)
8160 = build_method_type_directly (TYPE_CANONICAL (basetype),
8161 TYPE_CANONICAL (rettype),
8162 canon_argtypes);
8163 if (!COMPLETE_TYPE_P (t))
8164 layout_type (t);
8166 return t;
8169 /* Construct, lay out and return the type of methods belonging to class
8170 BASETYPE and whose arguments and values are described by TYPE.
8171 If that type exists already, reuse it.
8172 TYPE must be a FUNCTION_TYPE node. */
8174 tree
8175 build_method_type (tree basetype, tree type)
8177 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8179 return build_method_type_directly (basetype,
8180 TREE_TYPE (type),
8181 TYPE_ARG_TYPES (type));
8184 /* Construct, lay out and return the type of offsets to a value
8185 of type TYPE, within an object of type BASETYPE.
8186 If a suitable offset type exists already, reuse it. */
8188 tree
8189 build_offset_type (tree basetype, tree type)
8191 tree t;
8192 hashval_t hashcode = 0;
8194 /* Make a node of the sort we want. */
8195 t = make_node (OFFSET_TYPE);
8197 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8198 TREE_TYPE (t) = type;
8200 /* If we already have such a type, use the old one. */
8201 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8202 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
8203 t = type_hash_canon (hashcode, t);
8205 if (!COMPLETE_TYPE_P (t))
8206 layout_type (t);
8208 if (TYPE_CANONICAL (t) == t)
8210 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8211 || TYPE_STRUCTURAL_EQUALITY_P (type))
8212 SET_TYPE_STRUCTURAL_EQUALITY (t);
8213 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8214 || TYPE_CANONICAL (type) != type)
8215 TYPE_CANONICAL (t)
8216 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8217 TYPE_CANONICAL (type));
8220 return t;
8223 /* Create a complex type whose components are COMPONENT_TYPE. */
8225 tree
8226 build_complex_type (tree component_type)
8228 tree t;
8229 hashval_t hashcode;
8231 gcc_assert (INTEGRAL_TYPE_P (component_type)
8232 || SCALAR_FLOAT_TYPE_P (component_type)
8233 || FIXED_POINT_TYPE_P (component_type));
8235 /* Make a node of the sort we want. */
8236 t = make_node (COMPLEX_TYPE);
8238 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8240 /* If we already have such a type, use the old one. */
8241 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
8242 t = type_hash_canon (hashcode, t);
8244 if (!COMPLETE_TYPE_P (t))
8245 layout_type (t);
8247 if (TYPE_CANONICAL (t) == t)
8249 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8250 SET_TYPE_STRUCTURAL_EQUALITY (t);
8251 else if (TYPE_CANONICAL (component_type) != component_type)
8252 TYPE_CANONICAL (t)
8253 = build_complex_type (TYPE_CANONICAL (component_type));
8256 /* We need to create a name, since complex is a fundamental type. */
8257 if (! TYPE_NAME (t))
8259 const char *name;
8260 if (component_type == char_type_node)
8261 name = "complex char";
8262 else if (component_type == signed_char_type_node)
8263 name = "complex signed char";
8264 else if (component_type == unsigned_char_type_node)
8265 name = "complex unsigned char";
8266 else if (component_type == short_integer_type_node)
8267 name = "complex short int";
8268 else if (component_type == short_unsigned_type_node)
8269 name = "complex short unsigned int";
8270 else if (component_type == integer_type_node)
8271 name = "complex int";
8272 else if (component_type == unsigned_type_node)
8273 name = "complex unsigned int";
8274 else if (component_type == long_integer_type_node)
8275 name = "complex long int";
8276 else if (component_type == long_unsigned_type_node)
8277 name = "complex long unsigned int";
8278 else if (component_type == long_long_integer_type_node)
8279 name = "complex long long int";
8280 else if (component_type == long_long_unsigned_type_node)
8281 name = "complex long long unsigned int";
8282 else
8283 name = 0;
8285 if (name != 0)
8286 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8287 get_identifier (name), t);
8290 return build_qualified_type (t, TYPE_QUALS (component_type));
8293 /* If TYPE is a real or complex floating-point type and the target
8294 does not directly support arithmetic on TYPE then return the wider
8295 type to be used for arithmetic on TYPE. Otherwise, return
8296 NULL_TREE. */
8298 tree
8299 excess_precision_type (tree type)
8301 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8303 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8304 switch (TREE_CODE (type))
8306 case REAL_TYPE:
8307 switch (flt_eval_method)
8309 case 1:
8310 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8311 return double_type_node;
8312 break;
8313 case 2:
8314 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8315 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8316 return long_double_type_node;
8317 break;
8318 default:
8319 gcc_unreachable ();
8321 break;
8322 case COMPLEX_TYPE:
8323 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8324 return NULL_TREE;
8325 switch (flt_eval_method)
8327 case 1:
8328 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8329 return complex_double_type_node;
8330 break;
8331 case 2:
8332 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8333 || (TYPE_MODE (TREE_TYPE (type))
8334 == TYPE_MODE (double_type_node)))
8335 return complex_long_double_type_node;
8336 break;
8337 default:
8338 gcc_unreachable ();
8340 break;
8341 default:
8342 break;
8345 return NULL_TREE;
8348 /* Return OP, stripped of any conversions to wider types as much as is safe.
8349 Converting the value back to OP's type makes a value equivalent to OP.
8351 If FOR_TYPE is nonzero, we return a value which, if converted to
8352 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8354 OP must have integer, real or enumeral type. Pointers are not allowed!
8356 There are some cases where the obvious value we could return
8357 would regenerate to OP if converted to OP's type,
8358 but would not extend like OP to wider types.
8359 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8360 For example, if OP is (unsigned short)(signed char)-1,
8361 we avoid returning (signed char)-1 if FOR_TYPE is int,
8362 even though extending that to an unsigned short would regenerate OP,
8363 since the result of extending (signed char)-1 to (int)
8364 is different from (int) OP. */
8366 tree
8367 get_unwidened (tree op, tree for_type)
8369 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8370 tree type = TREE_TYPE (op);
8371 unsigned final_prec
8372 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8373 int uns
8374 = (for_type != 0 && for_type != type
8375 && final_prec > TYPE_PRECISION (type)
8376 && TYPE_UNSIGNED (type));
8377 tree win = op;
8379 while (CONVERT_EXPR_P (op))
8381 int bitschange;
8383 /* TYPE_PRECISION on vector types has different meaning
8384 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8385 so avoid them here. */
8386 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8387 break;
8389 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8390 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8392 /* Truncations are many-one so cannot be removed.
8393 Unless we are later going to truncate down even farther. */
8394 if (bitschange < 0
8395 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8396 break;
8398 /* See what's inside this conversion. If we decide to strip it,
8399 we will set WIN. */
8400 op = TREE_OPERAND (op, 0);
8402 /* If we have not stripped any zero-extensions (uns is 0),
8403 we can strip any kind of extension.
8404 If we have previously stripped a zero-extension,
8405 only zero-extensions can safely be stripped.
8406 Any extension can be stripped if the bits it would produce
8407 are all going to be discarded later by truncating to FOR_TYPE. */
8409 if (bitschange > 0)
8411 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8412 win = op;
8413 /* TYPE_UNSIGNED says whether this is a zero-extension.
8414 Let's avoid computing it if it does not affect WIN
8415 and if UNS will not be needed again. */
8416 if ((uns
8417 || CONVERT_EXPR_P (op))
8418 && TYPE_UNSIGNED (TREE_TYPE (op)))
8420 uns = 1;
8421 win = op;
8426 /* If we finally reach a constant see if it fits in for_type and
8427 in that case convert it. */
8428 if (for_type
8429 && TREE_CODE (win) == INTEGER_CST
8430 && TREE_TYPE (win) != for_type
8431 && int_fits_type_p (win, for_type))
8432 win = fold_convert (for_type, win);
8434 return win;
8437 /* Return OP or a simpler expression for a narrower value
8438 which can be sign-extended or zero-extended to give back OP.
8439 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8440 or 0 if the value should be sign-extended. */
8442 tree
8443 get_narrower (tree op, int *unsignedp_ptr)
8445 int uns = 0;
8446 int first = 1;
8447 tree win = op;
8448 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8450 while (TREE_CODE (op) == NOP_EXPR)
8452 int bitschange
8453 = (TYPE_PRECISION (TREE_TYPE (op))
8454 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8456 /* Truncations are many-one so cannot be removed. */
8457 if (bitschange < 0)
8458 break;
8460 /* See what's inside this conversion. If we decide to strip it,
8461 we will set WIN. */
8463 if (bitschange > 0)
8465 op = TREE_OPERAND (op, 0);
8466 /* An extension: the outermost one can be stripped,
8467 but remember whether it is zero or sign extension. */
8468 if (first)
8469 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8470 /* Otherwise, if a sign extension has been stripped,
8471 only sign extensions can now be stripped;
8472 if a zero extension has been stripped, only zero-extensions. */
8473 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8474 break;
8475 first = 0;
8477 else /* bitschange == 0 */
8479 /* A change in nominal type can always be stripped, but we must
8480 preserve the unsignedness. */
8481 if (first)
8482 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8483 first = 0;
8484 op = TREE_OPERAND (op, 0);
8485 /* Keep trying to narrow, but don't assign op to win if it
8486 would turn an integral type into something else. */
8487 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8488 continue;
8491 win = op;
8494 if (TREE_CODE (op) == COMPONENT_REF
8495 /* Since type_for_size always gives an integer type. */
8496 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8497 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8498 /* Ensure field is laid out already. */
8499 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8500 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
8502 unsigned HOST_WIDE_INT innerprec
8503 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
8504 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8505 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8506 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8508 /* We can get this structure field in a narrower type that fits it,
8509 but the resulting extension to its nominal type (a fullword type)
8510 must satisfy the same conditions as for other extensions.
8512 Do this only for fields that are aligned (not bit-fields),
8513 because when bit-field insns will be used there is no
8514 advantage in doing this. */
8516 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8517 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8518 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8519 && type != 0)
8521 if (first)
8522 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8523 win = fold_convert (type, op);
8527 *unsignedp_ptr = uns;
8528 return win;
8531 /* Returns true if integer constant C has a value that is permissible
8532 for type TYPE (an INTEGER_TYPE). */
8534 bool
8535 int_fits_type_p (const_tree c, const_tree type)
8537 tree type_low_bound, type_high_bound;
8538 bool ok_for_low_bound, ok_for_high_bound, unsc;
8539 double_int dc, dd;
8541 dc = tree_to_double_int (c);
8542 unsc = TYPE_UNSIGNED (TREE_TYPE (c));
8544 retry:
8545 type_low_bound = TYPE_MIN_VALUE (type);
8546 type_high_bound = TYPE_MAX_VALUE (type);
8548 /* If at least one bound of the type is a constant integer, we can check
8549 ourselves and maybe make a decision. If no such decision is possible, but
8550 this type is a subtype, try checking against that. Otherwise, use
8551 double_int_fits_to_tree_p, which checks against the precision.
8553 Compute the status for each possibly constant bound, and return if we see
8554 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8555 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8556 for "constant known to fit". */
8558 /* Check if c >= type_low_bound. */
8559 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8561 dd = tree_to_double_int (type_low_bound);
8562 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
8564 int c_neg = (!unsc && dc.is_negative ());
8565 int t_neg = (unsc && dd.is_negative ());
8567 if (c_neg && !t_neg)
8568 return false;
8569 if ((c_neg || !t_neg) && dc.ult (dd))
8570 return false;
8572 else if (dc.cmp (dd, unsc) < 0)
8573 return false;
8574 ok_for_low_bound = true;
8576 else
8577 ok_for_low_bound = false;
8579 /* Check if c <= type_high_bound. */
8580 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8582 dd = tree_to_double_int (type_high_bound);
8583 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
8585 int c_neg = (!unsc && dc.is_negative ());
8586 int t_neg = (unsc && dd.is_negative ());
8588 if (t_neg && !c_neg)
8589 return false;
8590 if ((t_neg || !c_neg) && dc.ugt (dd))
8591 return false;
8593 else if (dc.cmp (dd, unsc) > 0)
8594 return false;
8595 ok_for_high_bound = true;
8597 else
8598 ok_for_high_bound = false;
8600 /* If the constant fits both bounds, the result is known. */
8601 if (ok_for_low_bound && ok_for_high_bound)
8602 return true;
8604 /* Perform some generic filtering which may allow making a decision
8605 even if the bounds are not constant. First, negative integers
8606 never fit in unsigned types, */
8607 if (TYPE_UNSIGNED (type) && !unsc && dc.is_negative ())
8608 return false;
8610 /* Second, narrower types always fit in wider ones. */
8611 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8612 return true;
8614 /* Third, unsigned integers with top bit set never fit signed types. */
8615 if (! TYPE_UNSIGNED (type) && unsc)
8617 int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (c))) - 1;
8618 if (prec < HOST_BITS_PER_WIDE_INT)
8620 if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
8621 return false;
8623 else if (((((unsigned HOST_WIDE_INT) 1)
8624 << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
8625 return false;
8628 /* If we haven't been able to decide at this point, there nothing more we
8629 can check ourselves here. Look at the base type if we have one and it
8630 has the same precision. */
8631 if (TREE_CODE (type) == INTEGER_TYPE
8632 && TREE_TYPE (type) != 0
8633 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8635 type = TREE_TYPE (type);
8636 goto retry;
8639 /* Or to double_int_fits_to_tree_p, if nothing else. */
8640 return double_int_fits_to_tree_p (type, dc);
8643 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8644 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8645 represented (assuming two's-complement arithmetic) within the bit
8646 precision of the type are returned instead. */
8648 void
8649 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8651 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8652 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8653 mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
8654 TYPE_UNSIGNED (type));
8655 else
8657 if (TYPE_UNSIGNED (type))
8658 mpz_set_ui (min, 0);
8659 else
8661 double_int mn;
8662 mn = double_int::mask (TYPE_PRECISION (type) - 1);
8663 mn = (mn + double_int_one).sext (TYPE_PRECISION (type));
8664 mpz_set_double_int (min, mn, false);
8668 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8669 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8670 mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
8671 TYPE_UNSIGNED (type));
8672 else
8674 if (TYPE_UNSIGNED (type))
8675 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)),
8676 true);
8677 else
8678 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type) - 1),
8679 true);
8683 /* Return true if VAR is an automatic variable defined in function FN. */
8685 bool
8686 auto_var_in_fn_p (const_tree var, const_tree fn)
8688 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8689 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8690 || TREE_CODE (var) == PARM_DECL)
8691 && ! TREE_STATIC (var))
8692 || TREE_CODE (var) == LABEL_DECL
8693 || TREE_CODE (var) == RESULT_DECL));
8696 /* Subprogram of following function. Called by walk_tree.
8698 Return *TP if it is an automatic variable or parameter of the
8699 function passed in as DATA. */
8701 static tree
8702 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8704 tree fn = (tree) data;
8706 if (TYPE_P (*tp))
8707 *walk_subtrees = 0;
8709 else if (DECL_P (*tp)
8710 && auto_var_in_fn_p (*tp, fn))
8711 return *tp;
8713 return NULL_TREE;
8716 /* Returns true if T is, contains, or refers to a type with variable
8717 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8718 arguments, but not the return type. If FN is nonzero, only return
8719 true if a modifier of the type or position of FN is a variable or
8720 parameter inside FN.
8722 This concept is more general than that of C99 'variably modified types':
8723 in C99, a struct type is never variably modified because a VLA may not
8724 appear as a structure member. However, in GNU C code like:
8726 struct S { int i[f()]; };
8728 is valid, and other languages may define similar constructs. */
8730 bool
8731 variably_modified_type_p (tree type, tree fn)
8733 tree t;
8735 /* Test if T is either variable (if FN is zero) or an expression containing
8736 a variable in FN. If TYPE isn't gimplified, return true also if
8737 gimplify_one_sizepos would gimplify the expression into a local
8738 variable. */
8739 #define RETURN_TRUE_IF_VAR(T) \
8740 do { tree _t = (T); \
8741 if (_t != NULL_TREE \
8742 && _t != error_mark_node \
8743 && TREE_CODE (_t) != INTEGER_CST \
8744 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8745 && (!fn \
8746 || (!TYPE_SIZES_GIMPLIFIED (type) \
8747 && !is_gimple_sizepos (_t)) \
8748 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8749 return true; } while (0)
8751 if (type == error_mark_node)
8752 return false;
8754 /* If TYPE itself has variable size, it is variably modified. */
8755 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8756 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8758 switch (TREE_CODE (type))
8760 case POINTER_TYPE:
8761 case REFERENCE_TYPE:
8762 case VECTOR_TYPE:
8763 if (variably_modified_type_p (TREE_TYPE (type), fn))
8764 return true;
8765 break;
8767 case FUNCTION_TYPE:
8768 case METHOD_TYPE:
8769 /* If TYPE is a function type, it is variably modified if the
8770 return type is variably modified. */
8771 if (variably_modified_type_p (TREE_TYPE (type), fn))
8772 return true;
8773 break;
8775 case INTEGER_TYPE:
8776 case REAL_TYPE:
8777 case FIXED_POINT_TYPE:
8778 case ENUMERAL_TYPE:
8779 case BOOLEAN_TYPE:
8780 /* Scalar types are variably modified if their end points
8781 aren't constant. */
8782 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8783 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8784 break;
8786 case RECORD_TYPE:
8787 case UNION_TYPE:
8788 case QUAL_UNION_TYPE:
8789 /* We can't see if any of the fields are variably-modified by the
8790 definition we normally use, since that would produce infinite
8791 recursion via pointers. */
8792 /* This is variably modified if some field's type is. */
8793 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8794 if (TREE_CODE (t) == FIELD_DECL)
8796 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8797 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8798 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8800 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8801 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8803 break;
8805 case ARRAY_TYPE:
8806 /* Do not call ourselves to avoid infinite recursion. This is
8807 variably modified if the element type is. */
8808 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8809 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8810 break;
8812 default:
8813 break;
8816 /* The current language may have other cases to check, but in general,
8817 all other types are not variably modified. */
8818 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8820 #undef RETURN_TRUE_IF_VAR
8823 /* Given a DECL or TYPE, return the scope in which it was declared, or
8824 NULL_TREE if there is no containing scope. */
8826 tree
8827 get_containing_scope (const_tree t)
8829 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8832 /* Return the innermost context enclosing DECL that is
8833 a FUNCTION_DECL, or zero if none. */
8835 tree
8836 decl_function_context (const_tree decl)
8838 tree context;
8840 if (TREE_CODE (decl) == ERROR_MARK)
8841 return 0;
8843 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8844 where we look up the function at runtime. Such functions always take
8845 a first argument of type 'pointer to real context'.
8847 C++ should really be fixed to use DECL_CONTEXT for the real context,
8848 and use something else for the "virtual context". */
8849 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8850 context
8851 = TYPE_MAIN_VARIANT
8852 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8853 else
8854 context = DECL_CONTEXT (decl);
8856 while (context && TREE_CODE (context) != FUNCTION_DECL)
8858 if (TREE_CODE (context) == BLOCK)
8859 context = BLOCK_SUPERCONTEXT (context);
8860 else
8861 context = get_containing_scope (context);
8864 return context;
8867 /* Return the innermost context enclosing DECL that is
8868 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8869 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8871 tree
8872 decl_type_context (const_tree decl)
8874 tree context = DECL_CONTEXT (decl);
8876 while (context)
8877 switch (TREE_CODE (context))
8879 case NAMESPACE_DECL:
8880 case TRANSLATION_UNIT_DECL:
8881 return NULL_TREE;
8883 case RECORD_TYPE:
8884 case UNION_TYPE:
8885 case QUAL_UNION_TYPE:
8886 return context;
8888 case TYPE_DECL:
8889 case FUNCTION_DECL:
8890 context = DECL_CONTEXT (context);
8891 break;
8893 case BLOCK:
8894 context = BLOCK_SUPERCONTEXT (context);
8895 break;
8897 default:
8898 gcc_unreachable ();
8901 return NULL_TREE;
8904 /* CALL is a CALL_EXPR. Return the declaration for the function
8905 called, or NULL_TREE if the called function cannot be
8906 determined. */
8908 tree
8909 get_callee_fndecl (const_tree call)
8911 tree addr;
8913 if (call == error_mark_node)
8914 return error_mark_node;
8916 /* It's invalid to call this function with anything but a
8917 CALL_EXPR. */
8918 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8920 /* The first operand to the CALL is the address of the function
8921 called. */
8922 addr = CALL_EXPR_FN (call);
8924 STRIP_NOPS (addr);
8926 /* If this is a readonly function pointer, extract its initial value. */
8927 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8928 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8929 && DECL_INITIAL (addr))
8930 addr = DECL_INITIAL (addr);
8932 /* If the address is just `&f' for some function `f', then we know
8933 that `f' is being called. */
8934 if (TREE_CODE (addr) == ADDR_EXPR
8935 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8936 return TREE_OPERAND (addr, 0);
8938 /* We couldn't figure out what was being called. */
8939 return NULL_TREE;
8942 /* Print debugging information about tree nodes generated during the compile,
8943 and any language-specific information. */
8945 void
8946 dump_tree_statistics (void)
8948 if (GATHER_STATISTICS)
8950 int i;
8951 int total_nodes, total_bytes;
8952 fprintf (stderr, "Kind Nodes Bytes\n");
8953 fprintf (stderr, "---------------------------------------\n");
8954 total_nodes = total_bytes = 0;
8955 for (i = 0; i < (int) all_kinds; i++)
8957 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
8958 tree_node_counts[i], tree_node_sizes[i]);
8959 total_nodes += tree_node_counts[i];
8960 total_bytes += tree_node_sizes[i];
8962 fprintf (stderr, "---------------------------------------\n");
8963 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
8964 fprintf (stderr, "---------------------------------------\n");
8965 fprintf (stderr, "Code Nodes\n");
8966 fprintf (stderr, "----------------------------\n");
8967 for (i = 0; i < (int) MAX_TREE_CODES; i++)
8968 fprintf (stderr, "%-20s %7d\n", tree_code_name[i], tree_code_counts[i]);
8969 fprintf (stderr, "----------------------------\n");
8970 ssanames_print_statistics ();
8971 phinodes_print_statistics ();
8973 else
8974 fprintf (stderr, "(No per-node statistics)\n");
8976 print_type_hash_statistics ();
8977 print_debug_expr_statistics ();
8978 print_value_expr_statistics ();
8979 lang_hooks.print_statistics ();
8982 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8984 /* Generate a crc32 of a byte. */
8986 static unsigned
8987 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
8989 unsigned ix;
8991 for (ix = bits; ix--; value <<= 1)
8993 unsigned feedback;
8995 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
8996 chksum <<= 1;
8997 chksum ^= feedback;
8999 return chksum;
9002 /* Generate a crc32 of a 32-bit unsigned. */
9004 unsigned
9005 crc32_unsigned (unsigned chksum, unsigned value)
9007 return crc32_unsigned_bits (chksum, value, 32);
9010 /* Generate a crc32 of a byte. */
9012 unsigned
9013 crc32_byte (unsigned chksum, char byte)
9015 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
9018 /* Generate a crc32 of a string. */
9020 unsigned
9021 crc32_string (unsigned chksum, const char *string)
9025 chksum = crc32_byte (chksum, *string);
9027 while (*string++);
9028 return chksum;
9031 /* P is a string that will be used in a symbol. Mask out any characters
9032 that are not valid in that context. */
9034 void
9035 clean_symbol_name (char *p)
9037 for (; *p; p++)
9038 if (! (ISALNUM (*p)
9039 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
9040 || *p == '$'
9041 #endif
9042 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
9043 || *p == '.'
9044 #endif
9046 *p = '_';
9049 /* Generate a name for a special-purpose function.
9050 The generated name may need to be unique across the whole link.
9051 Changes to this function may also require corresponding changes to
9052 xstrdup_mask_random.
9053 TYPE is some string to identify the purpose of this function to the
9054 linker or collect2; it must start with an uppercase letter,
9055 one of:
9056 I - for constructors
9057 D - for destructors
9058 N - for C++ anonymous namespaces
9059 F - for DWARF unwind frame information. */
9061 tree
9062 get_file_function_name (const char *type)
9064 char *buf;
9065 const char *p;
9066 char *q;
9068 /* If we already have a name we know to be unique, just use that. */
9069 if (first_global_object_name)
9070 p = q = ASTRDUP (first_global_object_name);
9071 /* If the target is handling the constructors/destructors, they
9072 will be local to this file and the name is only necessary for
9073 debugging purposes.
9074 We also assign sub_I and sub_D sufixes to constructors called from
9075 the global static constructors. These are always local. */
9076 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9077 || (strncmp (type, "sub_", 4) == 0
9078 && (type[4] == 'I' || type[4] == 'D')))
9080 const char *file = main_input_filename;
9081 if (! file)
9082 file = input_filename;
9083 /* Just use the file's basename, because the full pathname
9084 might be quite long. */
9085 p = q = ASTRDUP (lbasename (file));
9087 else
9089 /* Otherwise, the name must be unique across the entire link.
9090 We don't have anything that we know to be unique to this translation
9091 unit, so use what we do have and throw in some randomness. */
9092 unsigned len;
9093 const char *name = weak_global_object_name;
9094 const char *file = main_input_filename;
9096 if (! name)
9097 name = "";
9098 if (! file)
9099 file = input_filename;
9101 len = strlen (file);
9102 q = (char *) alloca (9 + 17 + len + 1);
9103 memcpy (q, file, len + 1);
9105 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9106 crc32_string (0, name), get_random_seed (false));
9108 p = q;
9111 clean_symbol_name (q);
9112 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9113 + strlen (type));
9115 /* Set up the name of the file-level functions we may need.
9116 Use a global object (which is already required to be unique over
9117 the program) rather than the file name (which imposes extra
9118 constraints). */
9119 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9121 return get_identifier (buf);
9124 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9126 /* Complain that the tree code of NODE does not match the expected 0
9127 terminated list of trailing codes. The trailing code list can be
9128 empty, for a more vague error message. FILE, LINE, and FUNCTION
9129 are of the caller. */
9131 void
9132 tree_check_failed (const_tree node, const char *file,
9133 int line, const char *function, ...)
9135 va_list args;
9136 const char *buffer;
9137 unsigned length = 0;
9138 int code;
9140 va_start (args, function);
9141 while ((code = va_arg (args, int)))
9142 length += 4 + strlen (tree_code_name[code]);
9143 va_end (args);
9144 if (length)
9146 char *tmp;
9147 va_start (args, function);
9148 length += strlen ("expected ");
9149 buffer = tmp = (char *) alloca (length);
9150 length = 0;
9151 while ((code = va_arg (args, int)))
9153 const char *prefix = length ? " or " : "expected ";
9155 strcpy (tmp + length, prefix);
9156 length += strlen (prefix);
9157 strcpy (tmp + length, tree_code_name[code]);
9158 length += strlen (tree_code_name[code]);
9160 va_end (args);
9162 else
9163 buffer = "unexpected node";
9165 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9166 buffer, tree_code_name[TREE_CODE (node)],
9167 function, trim_filename (file), line);
9170 /* Complain that the tree code of NODE does match the expected 0
9171 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9172 the caller. */
9174 void
9175 tree_not_check_failed (const_tree node, const char *file,
9176 int line, const char *function, ...)
9178 va_list args;
9179 char *buffer;
9180 unsigned length = 0;
9181 int code;
9183 va_start (args, function);
9184 while ((code = va_arg (args, int)))
9185 length += 4 + strlen (tree_code_name[code]);
9186 va_end (args);
9187 va_start (args, function);
9188 buffer = (char *) alloca (length);
9189 length = 0;
9190 while ((code = va_arg (args, int)))
9192 if (length)
9194 strcpy (buffer + length, " or ");
9195 length += 4;
9197 strcpy (buffer + length, tree_code_name[code]);
9198 length += strlen (tree_code_name[code]);
9200 va_end (args);
9202 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9203 buffer, tree_code_name[TREE_CODE (node)],
9204 function, trim_filename (file), line);
9207 /* Similar to tree_check_failed, except that we check for a class of tree
9208 code, given in CL. */
9210 void
9211 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9212 const char *file, int line, const char *function)
9214 internal_error
9215 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9216 TREE_CODE_CLASS_STRING (cl),
9217 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9218 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9221 /* Similar to tree_check_failed, except that instead of specifying a
9222 dozen codes, use the knowledge that they're all sequential. */
9224 void
9225 tree_range_check_failed (const_tree node, const char *file, int line,
9226 const char *function, enum tree_code c1,
9227 enum tree_code c2)
9229 char *buffer;
9230 unsigned length = 0;
9231 unsigned int c;
9233 for (c = c1; c <= c2; ++c)
9234 length += 4 + strlen (tree_code_name[c]);
9236 length += strlen ("expected ");
9237 buffer = (char *) alloca (length);
9238 length = 0;
9240 for (c = c1; c <= c2; ++c)
9242 const char *prefix = length ? " or " : "expected ";
9244 strcpy (buffer + length, prefix);
9245 length += strlen (prefix);
9246 strcpy (buffer + length, tree_code_name[c]);
9247 length += strlen (tree_code_name[c]);
9250 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9251 buffer, tree_code_name[TREE_CODE (node)],
9252 function, trim_filename (file), line);
9256 /* Similar to tree_check_failed, except that we check that a tree does
9257 not have the specified code, given in CL. */
9259 void
9260 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9261 const char *file, int line, const char *function)
9263 internal_error
9264 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9265 TREE_CODE_CLASS_STRING (cl),
9266 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9267 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9271 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9273 void
9274 omp_clause_check_failed (const_tree node, const char *file, int line,
9275 const char *function, enum omp_clause_code code)
9277 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9278 omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
9279 function, trim_filename (file), line);
9283 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9285 void
9286 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9287 const char *function, enum omp_clause_code c1,
9288 enum omp_clause_code c2)
9290 char *buffer;
9291 unsigned length = 0;
9292 unsigned int c;
9294 for (c = c1; c <= c2; ++c)
9295 length += 4 + strlen (omp_clause_code_name[c]);
9297 length += strlen ("expected ");
9298 buffer = (char *) alloca (length);
9299 length = 0;
9301 for (c = c1; c <= c2; ++c)
9303 const char *prefix = length ? " or " : "expected ";
9305 strcpy (buffer + length, prefix);
9306 length += strlen (prefix);
9307 strcpy (buffer + length, omp_clause_code_name[c]);
9308 length += strlen (omp_clause_code_name[c]);
9311 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9312 buffer, omp_clause_code_name[TREE_CODE (node)],
9313 function, trim_filename (file), line);
9317 #undef DEFTREESTRUCT
9318 #define DEFTREESTRUCT(VAL, NAME) NAME,
9320 static const char *ts_enum_names[] = {
9321 #include "treestruct.def"
9323 #undef DEFTREESTRUCT
9325 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9327 /* Similar to tree_class_check_failed, except that we check for
9328 whether CODE contains the tree structure identified by EN. */
9330 void
9331 tree_contains_struct_check_failed (const_tree node,
9332 const enum tree_node_structure_enum en,
9333 const char *file, int line,
9334 const char *function)
9336 internal_error
9337 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9338 TS_ENUM_NAME(en),
9339 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9343 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9344 (dynamically sized) vector. */
9346 void
9347 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9348 const char *function)
9350 internal_error
9351 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9352 idx + 1, len, function, trim_filename (file), line);
9355 /* Similar to above, except that the check is for the bounds of the operand
9356 vector of an expression node EXP. */
9358 void
9359 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9360 int line, const char *function)
9362 int code = TREE_CODE (exp);
9363 internal_error
9364 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9365 idx + 1, tree_code_name[code], TREE_OPERAND_LENGTH (exp),
9366 function, trim_filename (file), line);
9369 /* Similar to above, except that the check is for the number of
9370 operands of an OMP_CLAUSE node. */
9372 void
9373 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9374 int line, const char *function)
9376 internal_error
9377 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9378 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9379 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9380 trim_filename (file), line);
9382 #endif /* ENABLE_TREE_CHECKING */
9384 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9385 and mapped to the machine mode MODE. Initialize its fields and build
9386 the information necessary for debugging output. */
9388 static tree
9389 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
9391 tree t;
9392 hashval_t hashcode = 0;
9394 t = make_node (VECTOR_TYPE);
9395 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9396 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9397 SET_TYPE_MODE (t, mode);
9399 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9400 SET_TYPE_STRUCTURAL_EQUALITY (t);
9401 else if (TYPE_CANONICAL (innertype) != innertype
9402 || mode != VOIDmode)
9403 TYPE_CANONICAL (t)
9404 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9406 layout_type (t);
9408 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
9409 hashcode = iterative_hash_host_wide_int (nunits, hashcode);
9410 hashcode = iterative_hash_host_wide_int (mode, hashcode);
9411 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (t)), hashcode);
9412 t = type_hash_canon (hashcode, t);
9414 /* We have built a main variant, based on the main variant of the
9415 inner type. Use it to build the variant we return. */
9416 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9417 && TREE_TYPE (t) != innertype)
9418 return build_type_attribute_qual_variant (t,
9419 TYPE_ATTRIBUTES (innertype),
9420 TYPE_QUALS (innertype));
9422 return t;
9425 static tree
9426 make_or_reuse_type (unsigned size, int unsignedp)
9428 if (size == INT_TYPE_SIZE)
9429 return unsignedp ? unsigned_type_node : integer_type_node;
9430 if (size == CHAR_TYPE_SIZE)
9431 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9432 if (size == SHORT_TYPE_SIZE)
9433 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9434 if (size == LONG_TYPE_SIZE)
9435 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9436 if (size == LONG_LONG_TYPE_SIZE)
9437 return (unsignedp ? long_long_unsigned_type_node
9438 : long_long_integer_type_node);
9439 if (size == 128 && int128_integer_type_node)
9440 return (unsignedp ? int128_unsigned_type_node
9441 : int128_integer_type_node);
9443 if (unsignedp)
9444 return make_unsigned_type (size);
9445 else
9446 return make_signed_type (size);
9449 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9451 static tree
9452 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9454 if (satp)
9456 if (size == SHORT_FRACT_TYPE_SIZE)
9457 return unsignedp ? sat_unsigned_short_fract_type_node
9458 : sat_short_fract_type_node;
9459 if (size == FRACT_TYPE_SIZE)
9460 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9461 if (size == LONG_FRACT_TYPE_SIZE)
9462 return unsignedp ? sat_unsigned_long_fract_type_node
9463 : sat_long_fract_type_node;
9464 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9465 return unsignedp ? sat_unsigned_long_long_fract_type_node
9466 : sat_long_long_fract_type_node;
9468 else
9470 if (size == SHORT_FRACT_TYPE_SIZE)
9471 return unsignedp ? unsigned_short_fract_type_node
9472 : short_fract_type_node;
9473 if (size == FRACT_TYPE_SIZE)
9474 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9475 if (size == LONG_FRACT_TYPE_SIZE)
9476 return unsignedp ? unsigned_long_fract_type_node
9477 : long_fract_type_node;
9478 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9479 return unsignedp ? unsigned_long_long_fract_type_node
9480 : long_long_fract_type_node;
9483 return make_fract_type (size, unsignedp, satp);
9486 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9488 static tree
9489 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9491 if (satp)
9493 if (size == SHORT_ACCUM_TYPE_SIZE)
9494 return unsignedp ? sat_unsigned_short_accum_type_node
9495 : sat_short_accum_type_node;
9496 if (size == ACCUM_TYPE_SIZE)
9497 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9498 if (size == LONG_ACCUM_TYPE_SIZE)
9499 return unsignedp ? sat_unsigned_long_accum_type_node
9500 : sat_long_accum_type_node;
9501 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9502 return unsignedp ? sat_unsigned_long_long_accum_type_node
9503 : sat_long_long_accum_type_node;
9505 else
9507 if (size == SHORT_ACCUM_TYPE_SIZE)
9508 return unsignedp ? unsigned_short_accum_type_node
9509 : short_accum_type_node;
9510 if (size == ACCUM_TYPE_SIZE)
9511 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9512 if (size == LONG_ACCUM_TYPE_SIZE)
9513 return unsignedp ? unsigned_long_accum_type_node
9514 : long_accum_type_node;
9515 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9516 return unsignedp ? unsigned_long_long_accum_type_node
9517 : long_long_accum_type_node;
9520 return make_accum_type (size, unsignedp, satp);
9523 /* Create nodes for all integer types (and error_mark_node) using the sizes
9524 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9525 SHORT_DOUBLE specifies whether double should be of the same precision
9526 as float. */
9528 void
9529 build_common_tree_nodes (bool signed_char, bool short_double)
9531 error_mark_node = make_node (ERROR_MARK);
9532 TREE_TYPE (error_mark_node) = error_mark_node;
9534 initialize_sizetypes ();
9536 /* Define both `signed char' and `unsigned char'. */
9537 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9538 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9539 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9540 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9542 /* Define `char', which is like either `signed char' or `unsigned char'
9543 but not the same as either. */
9544 char_type_node
9545 = (signed_char
9546 ? make_signed_type (CHAR_TYPE_SIZE)
9547 : make_unsigned_type (CHAR_TYPE_SIZE));
9548 TYPE_STRING_FLAG (char_type_node) = 1;
9550 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9551 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9552 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9553 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9554 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9555 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9556 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9557 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9558 #if HOST_BITS_PER_WIDE_INT >= 64
9559 /* TODO: This isn't correct, but as logic depends at the moment on
9560 host's instead of target's wide-integer.
9561 If there is a target not supporting TImode, but has an 128-bit
9562 integer-scalar register, this target check needs to be adjusted. */
9563 if (targetm.scalar_mode_supported_p (TImode))
9565 int128_integer_type_node = make_signed_type (128);
9566 int128_unsigned_type_node = make_unsigned_type (128);
9568 #endif
9570 /* Define a boolean type. This type only represents boolean values but
9571 may be larger than char depending on the value of BOOL_TYPE_SIZE.
9572 Front ends which want to override this size (i.e. Java) can redefine
9573 boolean_type_node before calling build_common_tree_nodes_2. */
9574 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9575 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9576 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9577 TYPE_PRECISION (boolean_type_node) = 1;
9579 /* Define what type to use for size_t. */
9580 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9581 size_type_node = unsigned_type_node;
9582 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9583 size_type_node = long_unsigned_type_node;
9584 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9585 size_type_node = long_long_unsigned_type_node;
9586 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9587 size_type_node = short_unsigned_type_node;
9588 else
9589 gcc_unreachable ();
9591 /* Fill in the rest of the sized types. Reuse existing type nodes
9592 when possible. */
9593 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9594 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9595 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9596 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9597 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9599 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9600 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9601 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9602 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9603 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9605 access_public_node = get_identifier ("public");
9606 access_protected_node = get_identifier ("protected");
9607 access_private_node = get_identifier ("private");
9609 /* Define these next since types below may used them. */
9610 integer_zero_node = build_int_cst (integer_type_node, 0);
9611 integer_one_node = build_int_cst (integer_type_node, 1);
9612 integer_three_node = build_int_cst (integer_type_node, 3);
9613 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9615 size_zero_node = size_int (0);
9616 size_one_node = size_int (1);
9617 bitsize_zero_node = bitsize_int (0);
9618 bitsize_one_node = bitsize_int (1);
9619 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9621 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9622 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9624 void_type_node = make_node (VOID_TYPE);
9625 layout_type (void_type_node);
9627 /* We are not going to have real types in C with less than byte alignment,
9628 so we might as well not have any types that claim to have it. */
9629 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9630 TYPE_USER_ALIGN (void_type_node) = 0;
9632 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9633 layout_type (TREE_TYPE (null_pointer_node));
9635 ptr_type_node = build_pointer_type (void_type_node);
9636 const_ptr_type_node
9637 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9638 fileptr_type_node = ptr_type_node;
9640 float_type_node = make_node (REAL_TYPE);
9641 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9642 layout_type (float_type_node);
9644 double_type_node = make_node (REAL_TYPE);
9645 if (short_double)
9646 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9647 else
9648 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9649 layout_type (double_type_node);
9651 long_double_type_node = make_node (REAL_TYPE);
9652 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9653 layout_type (long_double_type_node);
9655 float_ptr_type_node = build_pointer_type (float_type_node);
9656 double_ptr_type_node = build_pointer_type (double_type_node);
9657 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9658 integer_ptr_type_node = build_pointer_type (integer_type_node);
9660 /* Fixed size integer types. */
9661 uint16_type_node = build_nonstandard_integer_type (16, true);
9662 uint32_type_node = build_nonstandard_integer_type (32, true);
9663 uint64_type_node = build_nonstandard_integer_type (64, true);
9665 /* Decimal float types. */
9666 dfloat32_type_node = make_node (REAL_TYPE);
9667 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9668 layout_type (dfloat32_type_node);
9669 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9670 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9672 dfloat64_type_node = make_node (REAL_TYPE);
9673 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9674 layout_type (dfloat64_type_node);
9675 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9676 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9678 dfloat128_type_node = make_node (REAL_TYPE);
9679 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9680 layout_type (dfloat128_type_node);
9681 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9682 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9684 complex_integer_type_node = build_complex_type (integer_type_node);
9685 complex_float_type_node = build_complex_type (float_type_node);
9686 complex_double_type_node = build_complex_type (double_type_node);
9687 complex_long_double_type_node = build_complex_type (long_double_type_node);
9689 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9690 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9691 sat_ ## KIND ## _type_node = \
9692 make_sat_signed_ ## KIND ## _type (SIZE); \
9693 sat_unsigned_ ## KIND ## _type_node = \
9694 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9695 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9696 unsigned_ ## KIND ## _type_node = \
9697 make_unsigned_ ## KIND ## _type (SIZE);
9699 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9700 sat_ ## WIDTH ## KIND ## _type_node = \
9701 make_sat_signed_ ## KIND ## _type (SIZE); \
9702 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9703 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9704 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9705 unsigned_ ## WIDTH ## KIND ## _type_node = \
9706 make_unsigned_ ## KIND ## _type (SIZE);
9708 /* Make fixed-point type nodes based on four different widths. */
9709 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9710 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9711 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9712 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9713 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9715 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9716 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9717 NAME ## _type_node = \
9718 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9719 u ## NAME ## _type_node = \
9720 make_or_reuse_unsigned_ ## KIND ## _type \
9721 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9722 sat_ ## NAME ## _type_node = \
9723 make_or_reuse_sat_signed_ ## KIND ## _type \
9724 (GET_MODE_BITSIZE (MODE ## mode)); \
9725 sat_u ## NAME ## _type_node = \
9726 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9727 (GET_MODE_BITSIZE (U ## MODE ## mode));
9729 /* Fixed-point type and mode nodes. */
9730 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9731 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9732 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9733 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9734 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9735 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9736 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9737 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9738 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9739 MAKE_FIXED_MODE_NODE (accum, da, DA)
9740 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9743 tree t = targetm.build_builtin_va_list ();
9745 /* Many back-ends define record types without setting TYPE_NAME.
9746 If we copied the record type here, we'd keep the original
9747 record type without a name. This breaks name mangling. So,
9748 don't copy record types and let c_common_nodes_and_builtins()
9749 declare the type to be __builtin_va_list. */
9750 if (TREE_CODE (t) != RECORD_TYPE)
9751 t = build_variant_type_copy (t);
9753 va_list_type_node = t;
9757 /* Modify DECL for given flags. */
9758 void
9759 set_call_expr_flags (tree decl, int flags)
9761 if (flags & ECF_NOTHROW)
9762 TREE_NOTHROW (decl) = 1;
9763 if (flags & ECF_CONST)
9764 TREE_READONLY (decl) = 1;
9765 if (flags & ECF_PURE)
9766 DECL_PURE_P (decl) = 1;
9767 if (flags & ECF_LOOPING_CONST_OR_PURE)
9768 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9769 if (flags & ECF_NOVOPS)
9770 DECL_IS_NOVOPS (decl) = 1;
9771 if (flags & ECF_NORETURN)
9772 TREE_THIS_VOLATILE (decl) = 1;
9773 if (flags & ECF_MALLOC)
9774 DECL_IS_MALLOC (decl) = 1;
9775 if (flags & ECF_RETURNS_TWICE)
9776 DECL_IS_RETURNS_TWICE (decl) = 1;
9777 if (flags & ECF_LEAF)
9778 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9779 NULL, DECL_ATTRIBUTES (decl));
9780 if ((flags & ECF_TM_PURE) && flag_tm)
9781 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("transaction_pure"),
9782 NULL, DECL_ATTRIBUTES (decl));
9783 /* Looping const or pure is implied by noreturn.
9784 There is currently no way to declare looping const or looping pure alone. */
9785 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9786 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9790 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9792 static void
9793 local_define_builtin (const char *name, tree type, enum built_in_function code,
9794 const char *library_name, int ecf_flags)
9796 tree decl;
9798 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9799 library_name, NULL_TREE);
9800 set_call_expr_flags (decl, ecf_flags);
9802 set_builtin_decl (code, decl, true);
9805 /* Call this function after instantiating all builtins that the language
9806 front end cares about. This will build the rest of the builtins that
9807 are relied upon by the tree optimizers and the middle-end. */
9809 void
9810 build_common_builtin_nodes (void)
9812 tree tmp, ftype;
9813 int ecf_flags;
9815 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9817 ftype = build_function_type (void_type_node, void_list_node);
9818 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
9819 "__builtin_unreachable",
9820 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9821 | ECF_CONST | ECF_LEAF);
9824 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9825 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9827 ftype = build_function_type_list (ptr_type_node,
9828 ptr_type_node, const_ptr_type_node,
9829 size_type_node, NULL_TREE);
9831 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
9832 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9833 "memcpy", ECF_NOTHROW | ECF_LEAF);
9834 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9835 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9836 "memmove", ECF_NOTHROW | ECF_LEAF);
9839 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
9841 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9842 const_ptr_type_node, size_type_node,
9843 NULL_TREE);
9844 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9845 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9848 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
9850 ftype = build_function_type_list (ptr_type_node,
9851 ptr_type_node, integer_type_node,
9852 size_type_node, NULL_TREE);
9853 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9854 "memset", ECF_NOTHROW | ECF_LEAF);
9857 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
9859 ftype = build_function_type_list (ptr_type_node,
9860 size_type_node, NULL_TREE);
9861 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9862 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9865 ftype = build_function_type_list (ptr_type_node, size_type_node,
9866 size_type_node, NULL_TREE);
9867 local_define_builtin ("__builtin_alloca_with_align", ftype,
9868 BUILT_IN_ALLOCA_WITH_ALIGN, "alloca",
9869 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9871 /* If we're checking the stack, `alloca' can throw. */
9872 if (flag_stack_check)
9874 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
9875 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
9878 ftype = build_function_type_list (void_type_node,
9879 ptr_type_node, ptr_type_node,
9880 ptr_type_node, NULL_TREE);
9881 local_define_builtin ("__builtin_init_trampoline", ftype,
9882 BUILT_IN_INIT_TRAMPOLINE,
9883 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9884 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
9885 BUILT_IN_INIT_HEAP_TRAMPOLINE,
9886 "__builtin_init_heap_trampoline",
9887 ECF_NOTHROW | ECF_LEAF);
9889 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9890 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9891 BUILT_IN_ADJUST_TRAMPOLINE,
9892 "__builtin_adjust_trampoline",
9893 ECF_CONST | ECF_NOTHROW);
9895 ftype = build_function_type_list (void_type_node,
9896 ptr_type_node, ptr_type_node, NULL_TREE);
9897 local_define_builtin ("__builtin_nonlocal_goto", ftype,
9898 BUILT_IN_NONLOCAL_GOTO,
9899 "__builtin_nonlocal_goto",
9900 ECF_NORETURN | ECF_NOTHROW);
9902 ftype = build_function_type_list (void_type_node,
9903 ptr_type_node, ptr_type_node, NULL_TREE);
9904 local_define_builtin ("__builtin_setjmp_setup", ftype,
9905 BUILT_IN_SETJMP_SETUP,
9906 "__builtin_setjmp_setup", ECF_NOTHROW);
9908 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9909 local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
9910 BUILT_IN_SETJMP_DISPATCHER,
9911 "__builtin_setjmp_dispatcher",
9912 ECF_PURE | ECF_NOTHROW);
9914 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9915 local_define_builtin ("__builtin_setjmp_receiver", ftype,
9916 BUILT_IN_SETJMP_RECEIVER,
9917 "__builtin_setjmp_receiver", ECF_NOTHROW);
9919 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9920 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
9921 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9923 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9924 local_define_builtin ("__builtin_stack_restore", ftype,
9925 BUILT_IN_STACK_RESTORE,
9926 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9928 /* If there's a possibility that we might use the ARM EABI, build the
9929 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
9930 if (targetm.arm_eabi_unwinder)
9932 ftype = build_function_type_list (void_type_node, NULL_TREE);
9933 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
9934 BUILT_IN_CXA_END_CLEANUP,
9935 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
9938 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9939 local_define_builtin ("__builtin_unwind_resume", ftype,
9940 BUILT_IN_UNWIND_RESUME,
9941 ((targetm_common.except_unwind_info (&global_options)
9942 == UI_SJLJ)
9943 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
9944 ECF_NORETURN);
9946 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
9948 ftype = build_function_type_list (ptr_type_node, integer_type_node,
9949 NULL_TREE);
9950 local_define_builtin ("__builtin_return_address", ftype,
9951 BUILT_IN_RETURN_ADDRESS,
9952 "__builtin_return_address",
9953 ECF_NOTHROW);
9956 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
9957 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9959 ftype = build_function_type_list (void_type_node, ptr_type_node,
9960 ptr_type_node, NULL_TREE);
9961 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
9962 local_define_builtin ("__cyg_profile_func_enter", ftype,
9963 BUILT_IN_PROFILE_FUNC_ENTER,
9964 "__cyg_profile_func_enter", 0);
9965 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9966 local_define_builtin ("__cyg_profile_func_exit", ftype,
9967 BUILT_IN_PROFILE_FUNC_EXIT,
9968 "__cyg_profile_func_exit", 0);
9971 /* The exception object and filter values from the runtime. The argument
9972 must be zero before exception lowering, i.e. from the front end. After
9973 exception lowering, it will be the region number for the exception
9974 landing pad. These functions are PURE instead of CONST to prevent
9975 them from being hoisted past the exception edge that will initialize
9976 its value in the landing pad. */
9977 ftype = build_function_type_list (ptr_type_node,
9978 integer_type_node, NULL_TREE);
9979 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
9980 /* Only use TM_PURE if we we have TM language support. */
9981 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
9982 ecf_flags |= ECF_TM_PURE;
9983 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
9984 "__builtin_eh_pointer", ecf_flags);
9986 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
9987 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
9988 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
9989 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9991 ftype = build_function_type_list (void_type_node,
9992 integer_type_node, integer_type_node,
9993 NULL_TREE);
9994 local_define_builtin ("__builtin_eh_copy_values", ftype,
9995 BUILT_IN_EH_COPY_VALUES,
9996 "__builtin_eh_copy_values", ECF_NOTHROW);
9998 /* Complex multiplication and division. These are handled as builtins
9999 rather than optabs because emit_library_call_value doesn't support
10000 complex. Further, we can do slightly better with folding these
10001 beasties if the real and complex parts of the arguments are separate. */
10003 int mode;
10005 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10007 char mode_name_buf[4], *q;
10008 const char *p;
10009 enum built_in_function mcode, dcode;
10010 tree type, inner_type;
10011 const char *prefix = "__";
10013 if (targetm.libfunc_gnu_prefix)
10014 prefix = "__gnu_";
10016 type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
10017 if (type == NULL)
10018 continue;
10019 inner_type = TREE_TYPE (type);
10021 ftype = build_function_type_list (type, inner_type, inner_type,
10022 inner_type, inner_type, NULL_TREE);
10024 mcode = ((enum built_in_function)
10025 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10026 dcode = ((enum built_in_function)
10027 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10029 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10030 *q = TOLOWER (*p);
10031 *q = '\0';
10033 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10034 NULL);
10035 local_define_builtin (built_in_names[mcode], ftype, mcode,
10036 built_in_names[mcode],
10037 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10039 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10040 NULL);
10041 local_define_builtin (built_in_names[dcode], ftype, dcode,
10042 built_in_names[dcode],
10043 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10048 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10049 better way.
10051 If we requested a pointer to a vector, build up the pointers that
10052 we stripped off while looking for the inner type. Similarly for
10053 return values from functions.
10055 The argument TYPE is the top of the chain, and BOTTOM is the
10056 new type which we will point to. */
10058 tree
10059 reconstruct_complex_type (tree type, tree bottom)
10061 tree inner, outer;
10063 if (TREE_CODE (type) == POINTER_TYPE)
10065 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10066 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10067 TYPE_REF_CAN_ALIAS_ALL (type));
10069 else if (TREE_CODE (type) == REFERENCE_TYPE)
10071 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10072 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10073 TYPE_REF_CAN_ALIAS_ALL (type));
10075 else if (TREE_CODE (type) == ARRAY_TYPE)
10077 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10078 outer = build_array_type (inner, TYPE_DOMAIN (type));
10080 else if (TREE_CODE (type) == FUNCTION_TYPE)
10082 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10083 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
10085 else if (TREE_CODE (type) == METHOD_TYPE)
10087 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10088 /* The build_method_type_directly() routine prepends 'this' to argument list,
10089 so we must compensate by getting rid of it. */
10090 outer
10091 = build_method_type_directly
10092 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10093 inner,
10094 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10096 else if (TREE_CODE (type) == OFFSET_TYPE)
10098 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10099 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10101 else
10102 return bottom;
10104 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10105 TYPE_QUALS (type));
10108 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10109 the inner type. */
10110 tree
10111 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
10113 int nunits;
10115 switch (GET_MODE_CLASS (mode))
10117 case MODE_VECTOR_INT:
10118 case MODE_VECTOR_FLOAT:
10119 case MODE_VECTOR_FRACT:
10120 case MODE_VECTOR_UFRACT:
10121 case MODE_VECTOR_ACCUM:
10122 case MODE_VECTOR_UACCUM:
10123 nunits = GET_MODE_NUNITS (mode);
10124 break;
10126 case MODE_INT:
10127 /* Check that there are no leftover bits. */
10128 gcc_assert (GET_MODE_BITSIZE (mode)
10129 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10131 nunits = GET_MODE_BITSIZE (mode)
10132 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10133 break;
10135 default:
10136 gcc_unreachable ();
10139 return make_vector_type (innertype, nunits, mode);
10142 /* Similarly, but takes the inner type and number of units, which must be
10143 a power of two. */
10145 tree
10146 build_vector_type (tree innertype, int nunits)
10148 return make_vector_type (innertype, nunits, VOIDmode);
10151 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10153 tree
10154 build_opaque_vector_type (tree innertype, int nunits)
10156 tree t = make_vector_type (innertype, nunits, VOIDmode);
10157 tree cand;
10158 /* We always build the non-opaque variant before the opaque one,
10159 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10160 cand = TYPE_NEXT_VARIANT (t);
10161 if (cand
10162 && TYPE_VECTOR_OPAQUE (cand)
10163 && check_qualified_type (cand, t, TYPE_QUALS (t)))
10164 return cand;
10165 /* Othewise build a variant type and make sure to queue it after
10166 the non-opaque type. */
10167 cand = build_distinct_type_copy (t);
10168 TYPE_VECTOR_OPAQUE (cand) = true;
10169 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10170 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10171 TYPE_NEXT_VARIANT (t) = cand;
10172 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10173 return cand;
10177 /* Given an initializer INIT, return TRUE if INIT is zero or some
10178 aggregate of zeros. Otherwise return FALSE. */
10179 bool
10180 initializer_zerop (const_tree init)
10182 tree elt;
10184 STRIP_NOPS (init);
10186 switch (TREE_CODE (init))
10188 case INTEGER_CST:
10189 return integer_zerop (init);
10191 case REAL_CST:
10192 /* ??? Note that this is not correct for C4X float formats. There,
10193 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10194 negative exponent. */
10195 return real_zerop (init)
10196 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10198 case FIXED_CST:
10199 return fixed_zerop (init);
10201 case COMPLEX_CST:
10202 return integer_zerop (init)
10203 || (real_zerop (init)
10204 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10205 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10207 case VECTOR_CST:
10209 unsigned i;
10210 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10211 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10212 return false;
10213 return true;
10216 case CONSTRUCTOR:
10218 unsigned HOST_WIDE_INT idx;
10220 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10221 if (!initializer_zerop (elt))
10222 return false;
10223 return true;
10226 case STRING_CST:
10228 int i;
10230 /* We need to loop through all elements to handle cases like
10231 "\0" and "\0foobar". */
10232 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10233 if (TREE_STRING_POINTER (init)[i] != '\0')
10234 return false;
10236 return true;
10239 default:
10240 return false;
10244 /* Check if vector VEC consists of all the equal elements and
10245 that the number of elements corresponds to the type of VEC.
10246 The function returns first element of the vector
10247 or NULL_TREE if the vector is not uniform. */
10248 tree
10249 uniform_vector_p (const_tree vec)
10251 tree first, t;
10252 unsigned i;
10254 if (vec == NULL_TREE)
10255 return NULL_TREE;
10257 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10259 if (TREE_CODE (vec) == VECTOR_CST)
10261 first = VECTOR_CST_ELT (vec, 0);
10262 for (i = 1; i < VECTOR_CST_NELTS (vec); ++i)
10263 if (!operand_equal_p (first, VECTOR_CST_ELT (vec, i), 0))
10264 return NULL_TREE;
10266 return first;
10269 else if (TREE_CODE (vec) == CONSTRUCTOR)
10271 first = error_mark_node;
10273 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10275 if (i == 0)
10277 first = t;
10278 continue;
10280 if (!operand_equal_p (first, t, 0))
10281 return NULL_TREE;
10283 if (i != TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)))
10284 return NULL_TREE;
10286 return first;
10289 return NULL_TREE;
10292 /* Build an empty statement at location LOC. */
10294 tree
10295 build_empty_stmt (location_t loc)
10297 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10298 SET_EXPR_LOCATION (t, loc);
10299 return t;
10303 /* Build an OpenMP clause with code CODE. LOC is the location of the
10304 clause. */
10306 tree
10307 build_omp_clause (location_t loc, enum omp_clause_code code)
10309 tree t;
10310 int size, length;
10312 length = omp_clause_num_ops[code];
10313 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10315 record_node_allocation_statistics (OMP_CLAUSE, size);
10317 t = ggc_alloc_tree_node (size);
10318 memset (t, 0, size);
10319 TREE_SET_CODE (t, OMP_CLAUSE);
10320 OMP_CLAUSE_SET_CODE (t, code);
10321 OMP_CLAUSE_LOCATION (t) = loc;
10323 return t;
10326 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10327 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10328 Except for the CODE and operand count field, other storage for the
10329 object is initialized to zeros. */
10331 tree
10332 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10334 tree t;
10335 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10337 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10338 gcc_assert (len >= 1);
10340 record_node_allocation_statistics (code, length);
10342 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10344 TREE_SET_CODE (t, code);
10346 /* Can't use TREE_OPERAND to store the length because if checking is
10347 enabled, it will try to check the length before we store it. :-P */
10348 t->exp.operands[0] = build_int_cst (sizetype, len);
10350 return t;
10353 /* Helper function for build_call_* functions; build a CALL_EXPR with
10354 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10355 the argument slots. */
10357 static tree
10358 build_call_1 (tree return_type, tree fn, int nargs)
10360 tree t;
10362 t = build_vl_exp (CALL_EXPR, nargs + 3);
10363 TREE_TYPE (t) = return_type;
10364 CALL_EXPR_FN (t) = fn;
10365 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10367 return t;
10370 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10371 FN and a null static chain slot. NARGS is the number of call arguments
10372 which are specified as "..." arguments. */
10374 tree
10375 build_call_nary (tree return_type, tree fn, int nargs, ...)
10377 tree ret;
10378 va_list args;
10379 va_start (args, nargs);
10380 ret = build_call_valist (return_type, fn, nargs, args);
10381 va_end (args);
10382 return ret;
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 a va_list ARGS. */
10389 tree
10390 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10392 tree t;
10393 int i;
10395 t = build_call_1 (return_type, fn, nargs);
10396 for (i = 0; i < nargs; i++)
10397 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10398 process_call_operands (t);
10399 return t;
10402 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10403 FN and a null static chain slot. NARGS is the number of call arguments
10404 which are specified as a tree array ARGS. */
10406 tree
10407 build_call_array_loc (location_t loc, tree return_type, tree fn,
10408 int nargs, const tree *args)
10410 tree t;
10411 int i;
10413 t = build_call_1 (return_type, fn, nargs);
10414 for (i = 0; i < nargs; i++)
10415 CALL_EXPR_ARG (t, i) = args[i];
10416 process_call_operands (t);
10417 SET_EXPR_LOCATION (t, loc);
10418 return t;
10421 /* Like build_call_array, but takes a vec. */
10423 tree
10424 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10426 tree ret, t;
10427 unsigned int ix;
10429 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10430 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10431 CALL_EXPR_ARG (ret, ix) = t;
10432 process_call_operands (ret);
10433 return ret;
10437 /* Returns true if it is possible to prove that the index of
10438 an array access REF (an ARRAY_REF expression) falls into the
10439 array bounds. */
10441 bool
10442 in_array_bounds_p (tree ref)
10444 tree idx = TREE_OPERAND (ref, 1);
10445 tree min, max;
10447 if (TREE_CODE (idx) != INTEGER_CST)
10448 return false;
10450 min = array_ref_low_bound (ref);
10451 max = array_ref_up_bound (ref);
10452 if (!min
10453 || !max
10454 || TREE_CODE (min) != INTEGER_CST
10455 || TREE_CODE (max) != INTEGER_CST)
10456 return false;
10458 if (tree_int_cst_lt (idx, min)
10459 || tree_int_cst_lt (max, idx))
10460 return false;
10462 return true;
10465 /* Returns true if it is possible to prove that the range of
10466 an array access REF (an ARRAY_RANGE_REF expression) falls
10467 into the array bounds. */
10469 bool
10470 range_in_array_bounds_p (tree ref)
10472 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
10473 tree range_min, range_max, min, max;
10475 range_min = TYPE_MIN_VALUE (domain_type);
10476 range_max = TYPE_MAX_VALUE (domain_type);
10477 if (!range_min
10478 || !range_max
10479 || TREE_CODE (range_min) != INTEGER_CST
10480 || TREE_CODE (range_max) != INTEGER_CST)
10481 return false;
10483 min = array_ref_low_bound (ref);
10484 max = array_ref_up_bound (ref);
10485 if (!min
10486 || !max
10487 || TREE_CODE (min) != INTEGER_CST
10488 || TREE_CODE (max) != INTEGER_CST)
10489 return false;
10491 if (tree_int_cst_lt (range_min, min)
10492 || tree_int_cst_lt (max, range_max))
10493 return false;
10495 return true;
10498 /* Return true if T (assumed to be a DECL) must be assigned a memory
10499 location. */
10501 bool
10502 needs_to_live_in_memory (const_tree t)
10504 return (TREE_ADDRESSABLE (t)
10505 || is_global_var (t)
10506 || (TREE_CODE (t) == RESULT_DECL
10507 && !DECL_BY_REFERENCE (t)
10508 && aggregate_value_p (t, current_function_decl)));
10511 /* Return value of a constant X and sign-extend it. */
10513 HOST_WIDE_INT
10514 int_cst_value (const_tree x)
10516 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10517 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10519 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10520 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10521 || TREE_INT_CST_HIGH (x) == -1);
10523 if (bits < HOST_BITS_PER_WIDE_INT)
10525 bool negative = ((val >> (bits - 1)) & 1) != 0;
10526 if (negative)
10527 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10528 else
10529 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10532 return val;
10535 /* Return value of a constant X and sign-extend it. */
10537 HOST_WIDEST_INT
10538 widest_int_cst_value (const_tree x)
10540 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10541 unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
10543 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
10544 gcc_assert (HOST_BITS_PER_WIDEST_INT >= HOST_BITS_PER_DOUBLE_INT);
10545 val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_HIGH (x))
10546 << HOST_BITS_PER_WIDE_INT);
10547 #else
10548 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10549 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10550 || TREE_INT_CST_HIGH (x) == -1);
10551 #endif
10553 if (bits < HOST_BITS_PER_WIDEST_INT)
10555 bool negative = ((val >> (bits - 1)) & 1) != 0;
10556 if (negative)
10557 val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
10558 else
10559 val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
10562 return val;
10565 /* If TYPE is an integral or pointer type, return an integer type with
10566 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10567 if TYPE is already an integer type of signedness UNSIGNEDP. */
10569 tree
10570 signed_or_unsigned_type_for (int unsignedp, tree type)
10572 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10573 return type;
10575 if (TREE_CODE (type) == VECTOR_TYPE)
10577 tree inner = TREE_TYPE (type);
10578 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10579 if (!inner2)
10580 return NULL_TREE;
10581 if (inner == inner2)
10582 return type;
10583 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10586 if (!INTEGRAL_TYPE_P (type)
10587 && !POINTER_TYPE_P (type))
10588 return NULL_TREE;
10590 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10593 /* If TYPE is an integral or pointer type, return an integer type with
10594 the same precision which is unsigned, or itself if TYPE is already an
10595 unsigned integer type. */
10597 tree
10598 unsigned_type_for (tree type)
10600 return signed_or_unsigned_type_for (1, type);
10603 /* If TYPE is an integral or pointer type, return an integer type with
10604 the same precision which is signed, or itself if TYPE is already a
10605 signed integer type. */
10607 tree
10608 signed_type_for (tree type)
10610 return signed_or_unsigned_type_for (0, type);
10613 /* If TYPE is a vector type, return a signed integer vector type with the
10614 same width and number of subparts. Otherwise return boolean_type_node. */
10616 tree
10617 truth_type_for (tree type)
10619 if (TREE_CODE (type) == VECTOR_TYPE)
10621 tree elem = lang_hooks.types.type_for_size
10622 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10623 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10625 else
10626 return boolean_type_node;
10629 /* Returns the largest value obtainable by casting something in INNER type to
10630 OUTER type. */
10632 tree
10633 upper_bound_in_type (tree outer, tree inner)
10635 double_int high;
10636 unsigned int det = 0;
10637 unsigned oprec = TYPE_PRECISION (outer);
10638 unsigned iprec = TYPE_PRECISION (inner);
10639 unsigned prec;
10641 /* Compute a unique number for every combination. */
10642 det |= (oprec > iprec) ? 4 : 0;
10643 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10644 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10646 /* Determine the exponent to use. */
10647 switch (det)
10649 case 0:
10650 case 1:
10651 /* oprec <= iprec, outer: signed, inner: don't care. */
10652 prec = oprec - 1;
10653 break;
10654 case 2:
10655 case 3:
10656 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10657 prec = oprec;
10658 break;
10659 case 4:
10660 /* oprec > iprec, outer: signed, inner: signed. */
10661 prec = iprec - 1;
10662 break;
10663 case 5:
10664 /* oprec > iprec, outer: signed, inner: unsigned. */
10665 prec = iprec;
10666 break;
10667 case 6:
10668 /* oprec > iprec, outer: unsigned, inner: signed. */
10669 prec = oprec;
10670 break;
10671 case 7:
10672 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10673 prec = iprec;
10674 break;
10675 default:
10676 gcc_unreachable ();
10679 /* Compute 2^^prec - 1. */
10680 if (prec <= HOST_BITS_PER_WIDE_INT)
10682 high.high = 0;
10683 high.low = ((~(unsigned HOST_WIDE_INT) 0)
10684 >> (HOST_BITS_PER_WIDE_INT - prec));
10686 else
10688 high.high = ((~(unsigned HOST_WIDE_INT) 0)
10689 >> (HOST_BITS_PER_DOUBLE_INT - prec));
10690 high.low = ~(unsigned HOST_WIDE_INT) 0;
10693 return double_int_to_tree (outer, high);
10696 /* Returns the smallest value obtainable by casting something in INNER type to
10697 OUTER type. */
10699 tree
10700 lower_bound_in_type (tree outer, tree inner)
10702 double_int low;
10703 unsigned oprec = TYPE_PRECISION (outer);
10704 unsigned iprec = TYPE_PRECISION (inner);
10706 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10707 and obtain 0. */
10708 if (TYPE_UNSIGNED (outer)
10709 /* If we are widening something of an unsigned type, OUTER type
10710 contains all values of INNER type. In particular, both INNER
10711 and OUTER types have zero in common. */
10712 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10713 low.low = low.high = 0;
10714 else
10716 /* If we are widening a signed type to another signed type, we
10717 want to obtain -2^^(iprec-1). If we are keeping the
10718 precision or narrowing to a signed type, we want to obtain
10719 -2^(oprec-1). */
10720 unsigned prec = oprec > iprec ? iprec : oprec;
10722 if (prec <= HOST_BITS_PER_WIDE_INT)
10724 low.high = ~(unsigned HOST_WIDE_INT) 0;
10725 low.low = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
10727 else
10729 low.high = ((~(unsigned HOST_WIDE_INT) 0)
10730 << (prec - HOST_BITS_PER_WIDE_INT - 1));
10731 low.low = 0;
10735 return double_int_to_tree (outer, low);
10738 /* Return nonzero if two operands that are suitable for PHI nodes are
10739 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10740 SSA_NAME or invariant. Note that this is strictly an optimization.
10741 That is, callers of this function can directly call operand_equal_p
10742 and get the same result, only slower. */
10745 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10747 if (arg0 == arg1)
10748 return 1;
10749 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10750 return 0;
10751 return operand_equal_p (arg0, arg1, 0);
10754 /* Returns number of zeros at the end of binary representation of X.
10756 ??? Use ffs if available? */
10758 tree
10759 num_ending_zeros (const_tree x)
10761 unsigned HOST_WIDE_INT fr, nfr;
10762 unsigned num, abits;
10763 tree type = TREE_TYPE (x);
10765 if (TREE_INT_CST_LOW (x) == 0)
10767 num = HOST_BITS_PER_WIDE_INT;
10768 fr = TREE_INT_CST_HIGH (x);
10770 else
10772 num = 0;
10773 fr = TREE_INT_CST_LOW (x);
10776 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
10778 nfr = fr >> abits;
10779 if (nfr << abits == fr)
10781 num += abits;
10782 fr = nfr;
10786 if (num > TYPE_PRECISION (type))
10787 num = TYPE_PRECISION (type);
10789 return build_int_cst_type (type, num);
10793 #define WALK_SUBTREE(NODE) \
10794 do \
10796 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10797 if (result) \
10798 return result; \
10800 while (0)
10802 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10803 be walked whenever a type is seen in the tree. Rest of operands and return
10804 value are as for walk_tree. */
10806 static tree
10807 walk_type_fields (tree type, walk_tree_fn func, void *data,
10808 struct pointer_set_t *pset, walk_tree_lh lh)
10810 tree result = NULL_TREE;
10812 switch (TREE_CODE (type))
10814 case POINTER_TYPE:
10815 case REFERENCE_TYPE:
10816 /* We have to worry about mutually recursive pointers. These can't
10817 be written in C. They can in Ada. It's pathological, but
10818 there's an ACATS test (c38102a) that checks it. Deal with this
10819 by checking if we're pointing to another pointer, that one
10820 points to another pointer, that one does too, and we have no htab.
10821 If so, get a hash table. We check three levels deep to avoid
10822 the cost of the hash table if we don't need one. */
10823 if (POINTER_TYPE_P (TREE_TYPE (type))
10824 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10825 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10826 && !pset)
10828 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10829 func, data);
10830 if (result)
10831 return result;
10833 break;
10836 /* ... fall through ... */
10838 case COMPLEX_TYPE:
10839 WALK_SUBTREE (TREE_TYPE (type));
10840 break;
10842 case METHOD_TYPE:
10843 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10845 /* Fall through. */
10847 case FUNCTION_TYPE:
10848 WALK_SUBTREE (TREE_TYPE (type));
10850 tree arg;
10852 /* We never want to walk into default arguments. */
10853 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10854 WALK_SUBTREE (TREE_VALUE (arg));
10856 break;
10858 case ARRAY_TYPE:
10859 /* Don't follow this nodes's type if a pointer for fear that
10860 we'll have infinite recursion. If we have a PSET, then we
10861 need not fear. */
10862 if (pset
10863 || (!POINTER_TYPE_P (TREE_TYPE (type))
10864 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10865 WALK_SUBTREE (TREE_TYPE (type));
10866 WALK_SUBTREE (TYPE_DOMAIN (type));
10867 break;
10869 case OFFSET_TYPE:
10870 WALK_SUBTREE (TREE_TYPE (type));
10871 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
10872 break;
10874 default:
10875 break;
10878 return NULL_TREE;
10881 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
10882 called with the DATA and the address of each sub-tree. If FUNC returns a
10883 non-NULL value, the traversal is stopped, and the value returned by FUNC
10884 is returned. If PSET is non-NULL it is used to record the nodes visited,
10885 and to avoid visiting a node more than once. */
10887 tree
10888 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
10889 struct pointer_set_t *pset, walk_tree_lh lh)
10891 enum tree_code code;
10892 int walk_subtrees;
10893 tree result;
10895 #define WALK_SUBTREE_TAIL(NODE) \
10896 do \
10898 tp = & (NODE); \
10899 goto tail_recurse; \
10901 while (0)
10903 tail_recurse:
10904 /* Skip empty subtrees. */
10905 if (!*tp)
10906 return NULL_TREE;
10908 /* Don't walk the same tree twice, if the user has requested
10909 that we avoid doing so. */
10910 if (pset && pointer_set_insert (pset, *tp))
10911 return NULL_TREE;
10913 /* Call the function. */
10914 walk_subtrees = 1;
10915 result = (*func) (tp, &walk_subtrees, data);
10917 /* If we found something, return it. */
10918 if (result)
10919 return result;
10921 code = TREE_CODE (*tp);
10923 /* Even if we didn't, FUNC may have decided that there was nothing
10924 interesting below this point in the tree. */
10925 if (!walk_subtrees)
10927 /* But we still need to check our siblings. */
10928 if (code == TREE_LIST)
10929 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10930 else if (code == OMP_CLAUSE)
10931 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10932 else
10933 return NULL_TREE;
10936 if (lh)
10938 result = (*lh) (tp, &walk_subtrees, func, data, pset);
10939 if (result || !walk_subtrees)
10940 return result;
10943 switch (code)
10945 case ERROR_MARK:
10946 case IDENTIFIER_NODE:
10947 case INTEGER_CST:
10948 case REAL_CST:
10949 case FIXED_CST:
10950 case VECTOR_CST:
10951 case STRING_CST:
10952 case BLOCK:
10953 case PLACEHOLDER_EXPR:
10954 case SSA_NAME:
10955 case FIELD_DECL:
10956 case RESULT_DECL:
10957 /* None of these have subtrees other than those already walked
10958 above. */
10959 break;
10961 case TREE_LIST:
10962 WALK_SUBTREE (TREE_VALUE (*tp));
10963 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10964 break;
10966 case TREE_VEC:
10968 int len = TREE_VEC_LENGTH (*tp);
10970 if (len == 0)
10971 break;
10973 /* Walk all elements but the first. */
10974 while (--len)
10975 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
10977 /* Now walk the first one as a tail call. */
10978 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
10981 case COMPLEX_CST:
10982 WALK_SUBTREE (TREE_REALPART (*tp));
10983 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
10985 case CONSTRUCTOR:
10987 unsigned HOST_WIDE_INT idx;
10988 constructor_elt *ce;
10990 for (idx = 0; vec_safe_iterate(CONSTRUCTOR_ELTS (*tp), idx, &ce); idx++)
10991 WALK_SUBTREE (ce->value);
10993 break;
10995 case SAVE_EXPR:
10996 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
10998 case BIND_EXPR:
11000 tree decl;
11001 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
11003 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11004 into declarations that are just mentioned, rather than
11005 declared; they don't really belong to this part of the tree.
11006 And, we can see cycles: the initializer for a declaration
11007 can refer to the declaration itself. */
11008 WALK_SUBTREE (DECL_INITIAL (decl));
11009 WALK_SUBTREE (DECL_SIZE (decl));
11010 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11012 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
11015 case STATEMENT_LIST:
11017 tree_stmt_iterator i;
11018 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
11019 WALK_SUBTREE (*tsi_stmt_ptr (i));
11021 break;
11023 case OMP_CLAUSE:
11024 switch (OMP_CLAUSE_CODE (*tp))
11026 case OMP_CLAUSE_PRIVATE:
11027 case OMP_CLAUSE_SHARED:
11028 case OMP_CLAUSE_FIRSTPRIVATE:
11029 case OMP_CLAUSE_COPYIN:
11030 case OMP_CLAUSE_COPYPRIVATE:
11031 case OMP_CLAUSE_FINAL:
11032 case OMP_CLAUSE_IF:
11033 case OMP_CLAUSE_NUM_THREADS:
11034 case OMP_CLAUSE_SCHEDULE:
11035 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
11036 /* FALLTHRU */
11038 case OMP_CLAUSE_NOWAIT:
11039 case OMP_CLAUSE_ORDERED:
11040 case OMP_CLAUSE_DEFAULT:
11041 case OMP_CLAUSE_UNTIED:
11042 case OMP_CLAUSE_MERGEABLE:
11043 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11045 case OMP_CLAUSE_LASTPRIVATE:
11046 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11047 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
11048 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11050 case OMP_CLAUSE_COLLAPSE:
11052 int i;
11053 for (i = 0; i < 3; i++)
11054 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11055 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11058 case OMP_CLAUSE_REDUCTION:
11060 int i;
11061 for (i = 0; i < 4; i++)
11062 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11063 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11066 default:
11067 gcc_unreachable ();
11069 break;
11071 case TARGET_EXPR:
11073 int i, len;
11075 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11076 But, we only want to walk once. */
11077 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11078 for (i = 0; i < len; ++i)
11079 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11080 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11083 case DECL_EXPR:
11084 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11085 defining. We only want to walk into these fields of a type in this
11086 case and not in the general case of a mere reference to the type.
11088 The criterion is as follows: if the field can be an expression, it
11089 must be walked only here. This should be in keeping with the fields
11090 that are directly gimplified in gimplify_type_sizes in order for the
11091 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11092 variable-sized types.
11094 Note that DECLs get walked as part of processing the BIND_EXPR. */
11095 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11097 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11098 if (TREE_CODE (*type_p) == ERROR_MARK)
11099 return NULL_TREE;
11101 /* Call the function for the type. See if it returns anything or
11102 doesn't want us to continue. If we are to continue, walk both
11103 the normal fields and those for the declaration case. */
11104 result = (*func) (type_p, &walk_subtrees, data);
11105 if (result || !walk_subtrees)
11106 return result;
11108 /* But do not walk a pointed-to type since it may itself need to
11109 be walked in the declaration case if it isn't anonymous. */
11110 if (!POINTER_TYPE_P (*type_p))
11112 result = walk_type_fields (*type_p, func, data, pset, lh);
11113 if (result)
11114 return result;
11117 /* If this is a record type, also walk the fields. */
11118 if (RECORD_OR_UNION_TYPE_P (*type_p))
11120 tree field;
11122 for (field = TYPE_FIELDS (*type_p); field;
11123 field = DECL_CHAIN (field))
11125 /* We'd like to look at the type of the field, but we can
11126 easily get infinite recursion. So assume it's pointed
11127 to elsewhere in the tree. Also, ignore things that
11128 aren't fields. */
11129 if (TREE_CODE (field) != FIELD_DECL)
11130 continue;
11132 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11133 WALK_SUBTREE (DECL_SIZE (field));
11134 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11135 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11136 WALK_SUBTREE (DECL_QUALIFIER (field));
11140 /* Same for scalar types. */
11141 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11142 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11143 || TREE_CODE (*type_p) == INTEGER_TYPE
11144 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11145 || TREE_CODE (*type_p) == REAL_TYPE)
11147 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11148 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11151 WALK_SUBTREE (TYPE_SIZE (*type_p));
11152 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11154 /* FALLTHRU */
11156 default:
11157 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11159 int i, len;
11161 /* Walk over all the sub-trees of this operand. */
11162 len = TREE_OPERAND_LENGTH (*tp);
11164 /* Go through the subtrees. We need to do this in forward order so
11165 that the scope of a FOR_EXPR is handled properly. */
11166 if (len)
11168 for (i = 0; i < len - 1; ++i)
11169 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11170 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11173 /* If this is a type, walk the needed fields in the type. */
11174 else if (TYPE_P (*tp))
11175 return walk_type_fields (*tp, func, data, pset, lh);
11176 break;
11179 /* We didn't find what we were looking for. */
11180 return NULL_TREE;
11182 #undef WALK_SUBTREE_TAIL
11184 #undef WALK_SUBTREE
11186 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11188 tree
11189 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11190 walk_tree_lh lh)
11192 tree result;
11193 struct pointer_set_t *pset;
11195 pset = pointer_set_create ();
11196 result = walk_tree_1 (tp, func, data, pset, lh);
11197 pointer_set_destroy (pset);
11198 return result;
11202 tree
11203 tree_block (tree t)
11205 char const c = TREE_CODE_CLASS (TREE_CODE (t));
11207 if (IS_EXPR_CODE_CLASS (c))
11208 return LOCATION_BLOCK (t->exp.locus);
11209 gcc_unreachable ();
11210 return NULL;
11213 void
11214 tree_set_block (tree t, tree b)
11216 char const c = TREE_CODE_CLASS (TREE_CODE (t));
11218 if (IS_EXPR_CODE_CLASS (c))
11220 if (b)
11221 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
11222 else
11223 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
11225 else
11226 gcc_unreachable ();
11229 /* Create a nameless artificial label and put it in the current
11230 function context. The label has a location of LOC. Returns the
11231 newly created label. */
11233 tree
11234 create_artificial_label (location_t loc)
11236 tree lab = build_decl (loc,
11237 LABEL_DECL, NULL_TREE, void_type_node);
11239 DECL_ARTIFICIAL (lab) = 1;
11240 DECL_IGNORED_P (lab) = 1;
11241 DECL_CONTEXT (lab) = current_function_decl;
11242 return lab;
11245 /* Given a tree, try to return a useful variable name that we can use
11246 to prefix a temporary that is being assigned the value of the tree.
11247 I.E. given <temp> = &A, return A. */
11249 const char *
11250 get_name (tree t)
11252 tree stripped_decl;
11254 stripped_decl = t;
11255 STRIP_NOPS (stripped_decl);
11256 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11257 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11258 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11260 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11261 if (!name)
11262 return NULL;
11263 return IDENTIFIER_POINTER (name);
11265 else
11267 switch (TREE_CODE (stripped_decl))
11269 case ADDR_EXPR:
11270 return get_name (TREE_OPERAND (stripped_decl, 0));
11271 default:
11272 return NULL;
11277 /* Return true if TYPE has a variable argument list. */
11279 bool
11280 stdarg_p (const_tree fntype)
11282 function_args_iterator args_iter;
11283 tree n = NULL_TREE, t;
11285 if (!fntype)
11286 return false;
11288 FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
11290 n = t;
11293 return n != NULL_TREE && n != void_type_node;
11296 /* Return true if TYPE has a prototype. */
11298 bool
11299 prototype_p (tree fntype)
11301 tree t;
11303 gcc_assert (fntype != NULL_TREE);
11305 t = TYPE_ARG_TYPES (fntype);
11306 return (t != NULL_TREE);
11309 /* If BLOCK is inlined from an __attribute__((__artificial__))
11310 routine, return pointer to location from where it has been
11311 called. */
11312 location_t *
11313 block_nonartificial_location (tree block)
11315 location_t *ret = NULL;
11317 while (block && TREE_CODE (block) == BLOCK
11318 && BLOCK_ABSTRACT_ORIGIN (block))
11320 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11322 while (TREE_CODE (ao) == BLOCK
11323 && BLOCK_ABSTRACT_ORIGIN (ao)
11324 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11325 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11327 if (TREE_CODE (ao) == FUNCTION_DECL)
11329 /* If AO is an artificial inline, point RET to the
11330 call site locus at which it has been inlined and continue
11331 the loop, in case AO's caller is also an artificial
11332 inline. */
11333 if (DECL_DECLARED_INLINE_P (ao)
11334 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11335 ret = &BLOCK_SOURCE_LOCATION (block);
11336 else
11337 break;
11339 else if (TREE_CODE (ao) != BLOCK)
11340 break;
11342 block = BLOCK_SUPERCONTEXT (block);
11344 return ret;
11348 /* If EXP is inlined from an __attribute__((__artificial__))
11349 function, return the location of the original call expression. */
11351 location_t
11352 tree_nonartificial_location (tree exp)
11354 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11356 if (loc)
11357 return *loc;
11358 else
11359 return EXPR_LOCATION (exp);
11363 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11364 nodes. */
11366 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11368 static hashval_t
11369 cl_option_hash_hash (const void *x)
11371 const_tree const t = (const_tree) x;
11372 const char *p;
11373 size_t i;
11374 size_t len = 0;
11375 hashval_t hash = 0;
11377 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11379 p = (const char *)TREE_OPTIMIZATION (t);
11380 len = sizeof (struct cl_optimization);
11383 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11385 p = (const char *)TREE_TARGET_OPTION (t);
11386 len = sizeof (struct cl_target_option);
11389 else
11390 gcc_unreachable ();
11392 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11393 something else. */
11394 for (i = 0; i < len; i++)
11395 if (p[i])
11396 hash = (hash << 4) ^ ((i << 2) | p[i]);
11398 return hash;
11401 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11402 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11403 same. */
11405 static int
11406 cl_option_hash_eq (const void *x, const void *y)
11408 const_tree const xt = (const_tree) x;
11409 const_tree const yt = (const_tree) y;
11410 const char *xp;
11411 const char *yp;
11412 size_t len;
11414 if (TREE_CODE (xt) != TREE_CODE (yt))
11415 return 0;
11417 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11419 xp = (const char *)TREE_OPTIMIZATION (xt);
11420 yp = (const char *)TREE_OPTIMIZATION (yt);
11421 len = sizeof (struct cl_optimization);
11424 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11426 xp = (const char *)TREE_TARGET_OPTION (xt);
11427 yp = (const char *)TREE_TARGET_OPTION (yt);
11428 len = sizeof (struct cl_target_option);
11431 else
11432 gcc_unreachable ();
11434 return (memcmp (xp, yp, len) == 0);
11437 /* Build an OPTIMIZATION_NODE based on the current options. */
11439 tree
11440 build_optimization_node (void)
11442 tree t;
11443 void **slot;
11445 /* Use the cache of optimization nodes. */
11447 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11448 &global_options);
11450 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
11451 t = (tree) *slot;
11452 if (!t)
11454 /* Insert this one into the hash table. */
11455 t = cl_optimization_node;
11456 *slot = t;
11458 /* Make a new node for next time round. */
11459 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11462 return t;
11465 /* Build a TARGET_OPTION_NODE based on the current options. */
11467 tree
11468 build_target_option_node (void)
11470 tree t;
11471 void **slot;
11473 /* Use the cache of optimization nodes. */
11475 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11476 &global_options);
11478 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
11479 t = (tree) *slot;
11480 if (!t)
11482 /* Insert this one into the hash table. */
11483 t = cl_target_option_node;
11484 *slot = t;
11486 /* Make a new node for next time round. */
11487 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11490 return t;
11493 /* Determine the "ultimate origin" of a block. The block may be an inlined
11494 instance of an inlined instance of a block which is local to an inline
11495 function, so we have to trace all of the way back through the origin chain
11496 to find out what sort of node actually served as the original seed for the
11497 given block. */
11499 tree
11500 block_ultimate_origin (const_tree block)
11502 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11504 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
11505 nodes in the function to point to themselves; ignore that if
11506 we're trying to output the abstract instance of this function. */
11507 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11508 return NULL_TREE;
11510 if (immediate_origin == NULL_TREE)
11511 return NULL_TREE;
11512 else
11514 tree ret_val;
11515 tree lookahead = immediate_origin;
11519 ret_val = lookahead;
11520 lookahead = (TREE_CODE (ret_val) == BLOCK
11521 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11523 while (lookahead != NULL && lookahead != ret_val);
11525 /* The block's abstract origin chain may not be the *ultimate* origin of
11526 the block. It could lead to a DECL that has an abstract origin set.
11527 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11528 will give us if it has one). Note that DECL's abstract origins are
11529 supposed to be the most distant ancestor (or so decl_ultimate_origin
11530 claims), so we don't need to loop following the DECL origins. */
11531 if (DECL_P (ret_val))
11532 return DECL_ORIGIN (ret_val);
11534 return ret_val;
11538 /* Return true if T1 and T2 are equivalent lists. */
11540 bool
11541 list_equal_p (const_tree t1, const_tree t2)
11543 for (; t1 && t2; t1 = TREE_CHAIN (t1) , t2 = TREE_CHAIN (t2))
11544 if (TREE_VALUE (t1) != TREE_VALUE (t2))
11545 return false;
11546 return !t1 && !t2;
11549 /* Return true iff conversion in EXP generates no instruction. Mark
11550 it inline so that we fully inline into the stripping functions even
11551 though we have two uses of this function. */
11553 static inline bool
11554 tree_nop_conversion (const_tree exp)
11556 tree outer_type, inner_type;
11558 if (!CONVERT_EXPR_P (exp)
11559 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11560 return false;
11561 if (TREE_OPERAND (exp, 0) == error_mark_node)
11562 return false;
11564 outer_type = TREE_TYPE (exp);
11565 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11567 if (!inner_type)
11568 return false;
11570 /* Use precision rather then machine mode when we can, which gives
11571 the correct answer even for submode (bit-field) types. */
11572 if ((INTEGRAL_TYPE_P (outer_type)
11573 || POINTER_TYPE_P (outer_type)
11574 || TREE_CODE (outer_type) == OFFSET_TYPE)
11575 && (INTEGRAL_TYPE_P (inner_type)
11576 || POINTER_TYPE_P (inner_type)
11577 || TREE_CODE (inner_type) == OFFSET_TYPE))
11578 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11580 /* Otherwise fall back on comparing machine modes (e.g. for
11581 aggregate types, floats). */
11582 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11585 /* Return true iff conversion in EXP generates no instruction. Don't
11586 consider conversions changing the signedness. */
11588 static bool
11589 tree_sign_nop_conversion (const_tree exp)
11591 tree outer_type, inner_type;
11593 if (!tree_nop_conversion (exp))
11594 return false;
11596 outer_type = TREE_TYPE (exp);
11597 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11599 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11600 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11603 /* Strip conversions from EXP according to tree_nop_conversion and
11604 return the resulting expression. */
11606 tree
11607 tree_strip_nop_conversions (tree exp)
11609 while (tree_nop_conversion (exp))
11610 exp = TREE_OPERAND (exp, 0);
11611 return exp;
11614 /* Strip conversions from EXP according to tree_sign_nop_conversion
11615 and return the resulting expression. */
11617 tree
11618 tree_strip_sign_nop_conversions (tree exp)
11620 while (tree_sign_nop_conversion (exp))
11621 exp = TREE_OPERAND (exp, 0);
11622 return exp;
11625 /* Avoid any floating point extensions from EXP. */
11626 tree
11627 strip_float_extensions (tree exp)
11629 tree sub, expt, subt;
11631 /* For floating point constant look up the narrowest type that can hold
11632 it properly and handle it like (type)(narrowest_type)constant.
11633 This way we can optimize for instance a=a*2.0 where "a" is float
11634 but 2.0 is double constant. */
11635 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11637 REAL_VALUE_TYPE orig;
11638 tree type = NULL;
11640 orig = TREE_REAL_CST (exp);
11641 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11642 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11643 type = float_type_node;
11644 else if (TYPE_PRECISION (TREE_TYPE (exp))
11645 > TYPE_PRECISION (double_type_node)
11646 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11647 type = double_type_node;
11648 if (type)
11649 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11652 if (!CONVERT_EXPR_P (exp))
11653 return exp;
11655 sub = TREE_OPERAND (exp, 0);
11656 subt = TREE_TYPE (sub);
11657 expt = TREE_TYPE (exp);
11659 if (!FLOAT_TYPE_P (subt))
11660 return exp;
11662 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11663 return exp;
11665 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11666 return exp;
11668 return strip_float_extensions (sub);
11671 /* Strip out all handled components that produce invariant
11672 offsets. */
11674 const_tree
11675 strip_invariant_refs (const_tree op)
11677 while (handled_component_p (op))
11679 switch (TREE_CODE (op))
11681 case ARRAY_REF:
11682 case ARRAY_RANGE_REF:
11683 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11684 || TREE_OPERAND (op, 2) != NULL_TREE
11685 || TREE_OPERAND (op, 3) != NULL_TREE)
11686 return NULL;
11687 break;
11689 case COMPONENT_REF:
11690 if (TREE_OPERAND (op, 2) != NULL_TREE)
11691 return NULL;
11692 break;
11694 default:;
11696 op = TREE_OPERAND (op, 0);
11699 return op;
11702 static GTY(()) tree gcc_eh_personality_decl;
11704 /* Return the GCC personality function decl. */
11706 tree
11707 lhd_gcc_personality (void)
11709 if (!gcc_eh_personality_decl)
11710 gcc_eh_personality_decl = build_personality_function ("gcc");
11711 return gcc_eh_personality_decl;
11714 /* Try to find a base info of BINFO that would have its field decl at offset
11715 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11716 found, return, otherwise return NULL_TREE. */
11718 tree
11719 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
11721 tree type = BINFO_TYPE (binfo);
11723 while (true)
11725 HOST_WIDE_INT pos, size;
11726 tree fld;
11727 int i;
11729 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (expected_type))
11730 return binfo;
11731 if (offset < 0)
11732 return NULL_TREE;
11734 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
11736 if (TREE_CODE (fld) != FIELD_DECL)
11737 continue;
11739 pos = int_bit_position (fld);
11740 size = tree_low_cst (DECL_SIZE (fld), 1);
11741 if (pos <= offset && (pos + size) > offset)
11742 break;
11744 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11745 return NULL_TREE;
11747 if (!DECL_ARTIFICIAL (fld))
11749 binfo = TYPE_BINFO (TREE_TYPE (fld));
11750 if (!binfo)
11751 return NULL_TREE;
11753 /* Offset 0 indicates the primary base, whose vtable contents are
11754 represented in the binfo for the derived class. */
11755 else if (offset != 0)
11757 tree base_binfo, found_binfo = NULL_TREE;
11758 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11759 if (TREE_TYPE (base_binfo) == TREE_TYPE (fld))
11761 found_binfo = base_binfo;
11762 break;
11764 if (!found_binfo)
11765 return NULL_TREE;
11766 binfo = found_binfo;
11769 type = TREE_TYPE (fld);
11770 offset -= pos;
11774 /* Returns true if X is a typedef decl. */
11776 bool
11777 is_typedef_decl (tree x)
11779 return (x && TREE_CODE (x) == TYPE_DECL
11780 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
11783 /* Returns true iff TYPE is a type variant created for a typedef. */
11785 bool
11786 typedef_variant_p (tree type)
11788 return is_typedef_decl (TYPE_NAME (type));
11791 /* Warn about a use of an identifier which was marked deprecated. */
11792 void
11793 warn_deprecated_use (tree node, tree attr)
11795 const char *msg;
11797 if (node == 0 || !warn_deprecated_decl)
11798 return;
11800 if (!attr)
11802 if (DECL_P (node))
11803 attr = DECL_ATTRIBUTES (node);
11804 else if (TYPE_P (node))
11806 tree decl = TYPE_STUB_DECL (node);
11807 if (decl)
11808 attr = lookup_attribute ("deprecated",
11809 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
11813 if (attr)
11814 attr = lookup_attribute ("deprecated", attr);
11816 if (attr)
11817 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
11818 else
11819 msg = NULL;
11821 if (DECL_P (node))
11823 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
11824 if (msg)
11825 warning (OPT_Wdeprecated_declarations,
11826 "%qD is deprecated (declared at %r%s:%d%R): %s",
11827 node, "locus", xloc.file, xloc.line, msg);
11828 else
11829 warning (OPT_Wdeprecated_declarations,
11830 "%qD is deprecated (declared at %r%s:%d%R)",
11831 node, "locus", xloc.file, xloc.line);
11833 else if (TYPE_P (node))
11835 tree what = NULL_TREE;
11836 tree decl = TYPE_STUB_DECL (node);
11838 if (TYPE_NAME (node))
11840 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
11841 what = TYPE_NAME (node);
11842 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
11843 && DECL_NAME (TYPE_NAME (node)))
11844 what = DECL_NAME (TYPE_NAME (node));
11847 if (decl)
11849 expanded_location xloc
11850 = expand_location (DECL_SOURCE_LOCATION (decl));
11851 if (what)
11853 if (msg)
11854 warning (OPT_Wdeprecated_declarations,
11855 "%qE is deprecated (declared at %r%s:%d%R): %s",
11856 what, "locus", xloc.file, xloc.line, msg);
11857 else
11858 warning (OPT_Wdeprecated_declarations,
11859 "%qE is deprecated (declared at %r%s:%d%R)",
11860 what, "locus", xloc.file, xloc.line);
11862 else
11864 if (msg)
11865 warning (OPT_Wdeprecated_declarations,
11866 "type is deprecated (declared at %r%s:%d%R): %s",
11867 "locus", xloc.file, xloc.line, msg);
11868 else
11869 warning (OPT_Wdeprecated_declarations,
11870 "type is deprecated (declared at %r%s:%d%R)",
11871 "locus", xloc.file, xloc.line);
11874 else
11876 if (what)
11878 if (msg)
11879 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
11880 what, msg);
11881 else
11882 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
11884 else
11886 if (msg)
11887 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
11888 msg);
11889 else
11890 warning (OPT_Wdeprecated_declarations, "type is deprecated");
11896 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
11897 somewhere in it. */
11899 bool
11900 contains_bitfld_component_ref_p (const_tree ref)
11902 while (handled_component_p (ref))
11904 if (TREE_CODE (ref) == COMPONENT_REF
11905 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
11906 return true;
11907 ref = TREE_OPERAND (ref, 0);
11910 return false;
11913 #include "gt-tree.h"