Merge aosp-toolchain/gcc/gcc-4_9 changes.
[official-gcc.git] / gcc-4_9 / gcc / tree.c
blob3b1ff7645599c6808bc5f5c6da1a73bdc4db200d
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2014 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 "stor-layout.h"
37 #include "calls.h"
38 #include "attribs.h"
39 #include "varasm.h"
40 #include "tm_p.h"
41 #include "function.h"
42 #include "obstack.h"
43 #include "toplev.h" /* get_random_seed */
44 #include "hashtab.h"
45 #include "filenames.h"
46 #include "output.h"
47 #include "target.h"
48 #include "common/common-target.h"
49 #include "langhooks.h"
50 #include "tree-inline.h"
51 #include "tree-iterator.h"
52 #include "basic-block.h"
53 #include "bitmap.h"
54 #include "pointer-set.h"
55 #include "tree-ssa-alias.h"
56 #include "internal-fn.h"
57 #include "gimple-expr.h"
58 #include "is-a.h"
59 #include "gimple.h"
60 #include "gimple-iterator.h"
61 #include "gimplify.h"
62 #include "gimple-ssa.h"
63 #include "cgraph.h"
64 #include "tree-phinodes.h"
65 #include "stringpool.h"
66 #include "tree-ssanames.h"
67 #include "expr.h"
68 #include "tree-dfa.h"
69 #include "params.h"
70 #include "tree-pass.h"
71 #include "langhooks-def.h"
72 #include "diagnostic.h"
73 #include "tree-diagnostic.h"
74 #include "tree-pretty-print.h"
75 #include "except.h"
76 #include "debug.h"
77 #include "intl.h"
78 #include "l-ipo.h"
80 /* Tree code classes. */
82 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
83 #define END_OF_BASE_TREE_CODES tcc_exceptional,
85 const enum tree_code_class tree_code_type[] = {
86 #include "all-tree.def"
89 #undef DEFTREECODE
90 #undef END_OF_BASE_TREE_CODES
92 /* Table indexed by tree code giving number of expression
93 operands beyond the fixed part of the node structure.
94 Not used for types or decls. */
96 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
97 #define END_OF_BASE_TREE_CODES 0,
99 const unsigned char tree_code_length[] = {
100 #include "all-tree.def"
103 #undef DEFTREECODE
104 #undef END_OF_BASE_TREE_CODES
106 /* Names of tree components.
107 Used for printing out the tree and error messages. */
108 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
109 #define END_OF_BASE_TREE_CODES "@dummy",
111 static const char *const tree_code_name[] = {
112 #include "all-tree.def"
115 #undef DEFTREECODE
116 #undef END_OF_BASE_TREE_CODES
118 /* Each tree code class has an associated string representation.
119 These must correspond to the tree_code_class entries. */
121 const char *const tree_code_class_strings[] =
123 "exceptional",
124 "constant",
125 "type",
126 "declaration",
127 "reference",
128 "comparison",
129 "unary",
130 "binary",
131 "statement",
132 "vl_exp",
133 "expression"
136 /* obstack.[ch] explicitly declined to prototype this. */
137 extern int _obstack_allocated_p (struct obstack *h, void *obj);
139 /* Statistics-gathering stuff. */
141 static int tree_code_counts[MAX_TREE_CODES];
142 int tree_node_counts[(int) all_kinds];
143 int tree_node_sizes[(int) all_kinds];
145 /* Keep in sync with tree.h:enum tree_node_kind. */
146 static const char * const tree_node_kind_names[] = {
147 "decls",
148 "types",
149 "blocks",
150 "stmts",
151 "refs",
152 "exprs",
153 "constants",
154 "identifiers",
155 "vecs",
156 "binfos",
157 "ssa names",
158 "constructors",
159 "random kinds",
160 "lang_decl kinds",
161 "lang_type kinds",
162 "omp clauses",
165 /* Unique id for next decl created. */
166 static GTY(()) int next_decl_uid;
167 /* Unique id for next type created. */
168 static GTY(()) int next_type_uid = 1;
169 /* Unique id for next debug decl created. Use negative numbers,
170 to catch erroneous uses. */
171 static GTY(()) int next_debug_decl_uid;
173 /* Since we cannot rehash a type after it is in the table, we have to
174 keep the hash code. */
176 struct GTY(()) type_hash {
177 unsigned long hash;
178 tree type;
181 /* Initial size of the hash table (rounded to next prime). */
182 #define TYPE_HASH_INITIAL_SIZE 1000
184 /* Now here is the hash table. When recording a type, it is added to
185 the slot whose index is the hash code. Note that the hash table is
186 used for several kinds of types (function types, array types and
187 array index range types, for now). While all these live in the
188 same table, they are completely independent, and the hash code is
189 computed differently for each of these. */
191 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
192 htab_t type_hash_table;
194 /* Hash table and temporary node for larger integer const values. */
195 static GTY (()) tree int_cst_node;
196 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
197 htab_t int_cst_hash_table;
199 /* Hash table for optimization flags and target option flags. Use the same
200 hash table for both sets of options. Nodes for building the current
201 optimization and target option nodes. The assumption is most of the time
202 the options created will already be in the hash table, so we avoid
203 allocating and freeing up a node repeatably. */
204 static GTY (()) tree cl_optimization_node;
205 static GTY (()) tree cl_target_option_node;
206 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
207 htab_t cl_option_hash_table;
209 /* General tree->tree mapping structure for use in hash tables. */
212 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
213 htab_t debug_expr_for_decl;
215 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
216 htab_t value_expr_for_decl;
218 static GTY ((if_marked ("tree_vec_map_marked_p"), param_is (struct tree_vec_map)))
219 htab_t debug_args_for_decl;
221 static GTY ((if_marked ("tree_priority_map_marked_p"),
222 param_is (struct tree_priority_map)))
223 htab_t init_priority_for_decl;
225 static void set_type_quals (tree, int);
226 static int type_hash_eq (const void *, const void *);
227 static hashval_t type_hash_hash (const void *);
228 static hashval_t int_cst_hash_hash (const void *);
229 static int int_cst_hash_eq (const void *, const void *);
230 static hashval_t cl_option_hash_hash (const void *);
231 static int cl_option_hash_eq (const void *, const void *);
232 static void print_type_hash_statistics (void);
233 static void print_debug_expr_statistics (void);
234 static void print_value_expr_statistics (void);
235 static int type_hash_marked_p (const void *);
236 static unsigned int type_hash_list (const_tree, hashval_t);
237 static unsigned int attribute_hash_list (const_tree, hashval_t);
238 static bool decls_same_for_odr (tree decl1, tree decl2);
240 tree global_trees[TI_MAX];
241 tree integer_types[itk_none];
243 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
245 /* Number of operands for each OpenMP clause. */
246 unsigned const char omp_clause_num_ops[] =
248 0, /* OMP_CLAUSE_ERROR */
249 1, /* OMP_CLAUSE_PRIVATE */
250 1, /* OMP_CLAUSE_SHARED */
251 1, /* OMP_CLAUSE_FIRSTPRIVATE */
252 2, /* OMP_CLAUSE_LASTPRIVATE */
253 4, /* OMP_CLAUSE_REDUCTION */
254 1, /* OMP_CLAUSE_COPYIN */
255 1, /* OMP_CLAUSE_COPYPRIVATE */
256 3, /* OMP_CLAUSE_LINEAR */
257 2, /* OMP_CLAUSE_ALIGNED */
258 1, /* OMP_CLAUSE_DEPEND */
259 1, /* OMP_CLAUSE_UNIFORM */
260 2, /* OMP_CLAUSE_FROM */
261 2, /* OMP_CLAUSE_TO */
262 2, /* OMP_CLAUSE_MAP */
263 1, /* OMP_CLAUSE__LOOPTEMP_ */
264 1, /* OMP_CLAUSE_IF */
265 1, /* OMP_CLAUSE_NUM_THREADS */
266 1, /* OMP_CLAUSE_SCHEDULE */
267 0, /* OMP_CLAUSE_NOWAIT */
268 0, /* OMP_CLAUSE_ORDERED */
269 0, /* OMP_CLAUSE_DEFAULT */
270 3, /* OMP_CLAUSE_COLLAPSE */
271 0, /* OMP_CLAUSE_UNTIED */
272 1, /* OMP_CLAUSE_FINAL */
273 0, /* OMP_CLAUSE_MERGEABLE */
274 1, /* OMP_CLAUSE_DEVICE */
275 1, /* OMP_CLAUSE_DIST_SCHEDULE */
276 0, /* OMP_CLAUSE_INBRANCH */
277 0, /* OMP_CLAUSE_NOTINBRANCH */
278 1, /* OMP_CLAUSE_NUM_TEAMS */
279 1, /* OMP_CLAUSE_THREAD_LIMIT */
280 0, /* OMP_CLAUSE_PROC_BIND */
281 1, /* OMP_CLAUSE_SAFELEN */
282 1, /* OMP_CLAUSE_SIMDLEN */
283 0, /* OMP_CLAUSE_FOR */
284 0, /* OMP_CLAUSE_PARALLEL */
285 0, /* OMP_CLAUSE_SECTIONS */
286 0, /* OMP_CLAUSE_TASKGROUP */
287 1, /* OMP_CLAUSE__SIMDUID_ */
290 const char * const omp_clause_code_name[] =
292 "error_clause",
293 "private",
294 "shared",
295 "firstprivate",
296 "lastprivate",
297 "reduction",
298 "copyin",
299 "copyprivate",
300 "linear",
301 "aligned",
302 "depend",
303 "uniform",
304 "from",
305 "to",
306 "map",
307 "_looptemp_",
308 "if",
309 "num_threads",
310 "schedule",
311 "nowait",
312 "ordered",
313 "default",
314 "collapse",
315 "untied",
316 "final",
317 "mergeable",
318 "device",
319 "dist_schedule",
320 "inbranch",
321 "notinbranch",
322 "num_teams",
323 "thread_limit",
324 "proc_bind",
325 "safelen",
326 "simdlen",
327 "for",
328 "parallel",
329 "sections",
330 "taskgroup",
331 "_simduid_"
335 /* Return the tree node structure used by tree code CODE. */
337 static inline enum tree_node_structure_enum
338 tree_node_structure_for_code (enum tree_code code)
340 switch (TREE_CODE_CLASS (code))
342 case tcc_declaration:
344 switch (code)
346 case FIELD_DECL:
347 return TS_FIELD_DECL;
348 case PARM_DECL:
349 return TS_PARM_DECL;
350 case VAR_DECL:
351 return TS_VAR_DECL;
352 case LABEL_DECL:
353 return TS_LABEL_DECL;
354 case RESULT_DECL:
355 return TS_RESULT_DECL;
356 case DEBUG_EXPR_DECL:
357 return TS_DECL_WRTL;
358 case CONST_DECL:
359 return TS_CONST_DECL;
360 case TYPE_DECL:
361 return TS_TYPE_DECL;
362 case FUNCTION_DECL:
363 return TS_FUNCTION_DECL;
364 case TRANSLATION_UNIT_DECL:
365 return TS_TRANSLATION_UNIT_DECL;
366 default:
367 return TS_DECL_NON_COMMON;
370 case tcc_type:
371 return TS_TYPE_NON_COMMON;
372 case tcc_reference:
373 case tcc_comparison:
374 case tcc_unary:
375 case tcc_binary:
376 case tcc_expression:
377 case tcc_statement:
378 case tcc_vl_exp:
379 return TS_EXP;
380 default: /* tcc_constant and tcc_exceptional */
381 break;
383 switch (code)
385 /* tcc_constant cases. */
386 case INTEGER_CST: return TS_INT_CST;
387 case REAL_CST: return TS_REAL_CST;
388 case FIXED_CST: return TS_FIXED_CST;
389 case COMPLEX_CST: return TS_COMPLEX;
390 case VECTOR_CST: return TS_VECTOR;
391 case STRING_CST: return TS_STRING;
392 /* tcc_exceptional cases. */
393 case ERROR_MARK: return TS_COMMON;
394 case IDENTIFIER_NODE: return TS_IDENTIFIER;
395 case TREE_LIST: return TS_LIST;
396 case TREE_VEC: return TS_VEC;
397 case SSA_NAME: return TS_SSA_NAME;
398 case PLACEHOLDER_EXPR: return TS_COMMON;
399 case STATEMENT_LIST: return TS_STATEMENT_LIST;
400 case BLOCK: return TS_BLOCK;
401 case CONSTRUCTOR: return TS_CONSTRUCTOR;
402 case TREE_BINFO: return TS_BINFO;
403 case OMP_CLAUSE: return TS_OMP_CLAUSE;
404 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
405 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
407 default:
408 gcc_unreachable ();
413 /* Initialize tree_contains_struct to describe the hierarchy of tree
414 nodes. */
416 static void
417 initialize_tree_contains_struct (void)
419 unsigned i;
421 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
423 enum tree_code code;
424 enum tree_node_structure_enum ts_code;
426 code = (enum tree_code) i;
427 ts_code = tree_node_structure_for_code (code);
429 /* Mark the TS structure itself. */
430 tree_contains_struct[code][ts_code] = 1;
432 /* Mark all the structures that TS is derived from. */
433 switch (ts_code)
435 case TS_TYPED:
436 case TS_BLOCK:
437 MARK_TS_BASE (code);
438 break;
440 case TS_COMMON:
441 case TS_INT_CST:
442 case TS_REAL_CST:
443 case TS_FIXED_CST:
444 case TS_VECTOR:
445 case TS_STRING:
446 case TS_COMPLEX:
447 case TS_SSA_NAME:
448 case TS_CONSTRUCTOR:
449 case TS_EXP:
450 case TS_STATEMENT_LIST:
451 MARK_TS_TYPED (code);
452 break;
454 case TS_IDENTIFIER:
455 case TS_DECL_MINIMAL:
456 case TS_TYPE_COMMON:
457 case TS_LIST:
458 case TS_VEC:
459 case TS_BINFO:
460 case TS_OMP_CLAUSE:
461 case TS_OPTIMIZATION:
462 case TS_TARGET_OPTION:
463 MARK_TS_COMMON (code);
464 break;
466 case TS_TYPE_WITH_LANG_SPECIFIC:
467 MARK_TS_TYPE_COMMON (code);
468 break;
470 case TS_TYPE_NON_COMMON:
471 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
472 break;
474 case TS_DECL_COMMON:
475 MARK_TS_DECL_MINIMAL (code);
476 break;
478 case TS_DECL_WRTL:
479 case TS_CONST_DECL:
480 MARK_TS_DECL_COMMON (code);
481 break;
483 case TS_DECL_NON_COMMON:
484 MARK_TS_DECL_WITH_VIS (code);
485 break;
487 case TS_DECL_WITH_VIS:
488 case TS_PARM_DECL:
489 case TS_LABEL_DECL:
490 case TS_RESULT_DECL:
491 MARK_TS_DECL_WRTL (code);
492 break;
494 case TS_FIELD_DECL:
495 MARK_TS_DECL_COMMON (code);
496 break;
498 case TS_VAR_DECL:
499 MARK_TS_DECL_WITH_VIS (code);
500 break;
502 case TS_TYPE_DECL:
503 case TS_FUNCTION_DECL:
504 MARK_TS_DECL_NON_COMMON (code);
505 break;
507 case TS_TRANSLATION_UNIT_DECL:
508 MARK_TS_DECL_COMMON (code);
509 break;
511 default:
512 gcc_unreachable ();
516 /* Basic consistency checks for attributes used in fold. */
517 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
518 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
519 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
520 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
521 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
522 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
523 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
524 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
525 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
526 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
527 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
528 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
529 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
530 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
531 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
532 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
533 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
534 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
535 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
536 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
537 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
538 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
539 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
540 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
541 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
542 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
543 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
544 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
545 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
546 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
547 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
548 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
549 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
550 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
551 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
552 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
553 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
554 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
555 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_MINIMAL]);
556 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_COMMON]);
560 /* Init tree.c. */
562 void
563 init_ttree (void)
565 /* Initialize the hash table of types. */
566 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
567 type_hash_eq, 0);
569 debug_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
570 tree_decl_map_eq, 0);
572 value_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
573 tree_decl_map_eq, 0);
574 init_priority_for_decl = htab_create_ggc (512, tree_priority_map_hash,
575 tree_priority_map_eq, 0);
577 int_cst_hash_table = htab_create_ggc (1024, int_cst_hash_hash,
578 int_cst_hash_eq, NULL);
580 int_cst_node = make_node (INTEGER_CST);
582 cl_option_hash_table = htab_create_ggc (64, cl_option_hash_hash,
583 cl_option_hash_eq, NULL);
585 cl_optimization_node = make_node (OPTIMIZATION_NODE);
586 cl_target_option_node = make_node (TARGET_OPTION_NODE);
588 /* Initialize the tree_contains_struct array. */
589 initialize_tree_contains_struct ();
590 lang_hooks.init_ts ();
594 /* The name of the object as the assembler will see it (but before any
595 translations made by ASM_OUTPUT_LABELREF). Often this is the same
596 as DECL_NAME. It is an IDENTIFIER_NODE. */
597 tree
598 decl_assembler_name (tree decl)
600 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
601 lang_hooks.set_decl_assembler_name (decl);
602 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
605 /* Compute the number of bytes occupied by a tree with code CODE.
606 This function cannot be used for nodes that have variable sizes,
607 including TREE_VEC, STRING_CST, and CALL_EXPR. */
608 size_t
609 tree_code_size (enum tree_code code)
611 switch (TREE_CODE_CLASS (code))
613 case tcc_declaration: /* A decl node */
615 switch (code)
617 case FIELD_DECL:
618 return sizeof (struct tree_field_decl);
619 case PARM_DECL:
620 return sizeof (struct tree_parm_decl);
621 case VAR_DECL:
622 return sizeof (struct tree_var_decl);
623 case LABEL_DECL:
624 return sizeof (struct tree_label_decl);
625 case RESULT_DECL:
626 return sizeof (struct tree_result_decl);
627 case CONST_DECL:
628 return sizeof (struct tree_const_decl);
629 case TYPE_DECL:
630 return sizeof (struct tree_type_decl);
631 case FUNCTION_DECL:
632 return sizeof (struct tree_function_decl);
633 case DEBUG_EXPR_DECL:
634 return sizeof (struct tree_decl_with_rtl);
635 default:
636 return sizeof (struct tree_decl_non_common);
640 case tcc_type: /* a type node */
641 return sizeof (struct tree_type_non_common);
643 case tcc_reference: /* a reference */
644 case tcc_expression: /* an expression */
645 case tcc_statement: /* an expression with side effects */
646 case tcc_comparison: /* a comparison expression */
647 case tcc_unary: /* a unary arithmetic expression */
648 case tcc_binary: /* a binary arithmetic expression */
649 return (sizeof (struct tree_exp)
650 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
652 case tcc_constant: /* a constant */
653 switch (code)
655 case INTEGER_CST: return sizeof (struct tree_int_cst);
656 case REAL_CST: return sizeof (struct tree_real_cst);
657 case FIXED_CST: return sizeof (struct tree_fixed_cst);
658 case COMPLEX_CST: return sizeof (struct tree_complex);
659 case VECTOR_CST: return sizeof (struct tree_vector);
660 case STRING_CST: gcc_unreachable ();
661 default:
662 return lang_hooks.tree_size (code);
665 case tcc_exceptional: /* something random, like an identifier. */
666 switch (code)
668 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
669 case TREE_LIST: return sizeof (struct tree_list);
671 case ERROR_MARK:
672 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
674 case TREE_VEC:
675 case OMP_CLAUSE: gcc_unreachable ();
677 case SSA_NAME: return sizeof (struct tree_ssa_name);
679 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
680 case BLOCK: return sizeof (struct tree_block);
681 case CONSTRUCTOR: return sizeof (struct tree_constructor);
682 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
683 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
685 default:
686 return lang_hooks.tree_size (code);
689 default:
690 gcc_unreachable ();
694 /* Compute the number of bytes occupied by NODE. This routine only
695 looks at TREE_CODE, except for those nodes that have variable sizes. */
696 size_t
697 tree_size (const_tree node)
699 const enum tree_code code = TREE_CODE (node);
700 switch (code)
702 case TREE_BINFO:
703 return (offsetof (struct tree_binfo, base_binfos)
704 + vec<tree, va_gc>
705 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
707 case TREE_VEC:
708 return (sizeof (struct tree_vec)
709 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
711 case VECTOR_CST:
712 return (sizeof (struct tree_vector)
713 + (TYPE_VECTOR_SUBPARTS (TREE_TYPE (node)) - 1) * sizeof (tree));
715 case STRING_CST:
716 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
718 case OMP_CLAUSE:
719 return (sizeof (struct tree_omp_clause)
720 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
721 * sizeof (tree));
723 default:
724 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
725 return (sizeof (struct tree_exp)
726 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
727 else
728 return tree_code_size (code);
732 /* Record interesting allocation statistics for a tree node with CODE
733 and LENGTH. */
735 static void
736 record_node_allocation_statistics (enum tree_code code ATTRIBUTE_UNUSED,
737 size_t length ATTRIBUTE_UNUSED)
739 enum tree_code_class type = TREE_CODE_CLASS (code);
740 tree_node_kind kind;
742 if (!GATHER_STATISTICS)
743 return;
745 switch (type)
747 case tcc_declaration: /* A decl node */
748 kind = d_kind;
749 break;
751 case tcc_type: /* a type node */
752 kind = t_kind;
753 break;
755 case tcc_statement: /* an expression with side effects */
756 kind = s_kind;
757 break;
759 case tcc_reference: /* a reference */
760 kind = r_kind;
761 break;
763 case tcc_expression: /* an expression */
764 case tcc_comparison: /* a comparison expression */
765 case tcc_unary: /* a unary arithmetic expression */
766 case tcc_binary: /* a binary arithmetic expression */
767 kind = e_kind;
768 break;
770 case tcc_constant: /* a constant */
771 kind = c_kind;
772 break;
774 case tcc_exceptional: /* something random, like an identifier. */
775 switch (code)
777 case IDENTIFIER_NODE:
778 kind = id_kind;
779 break;
781 case TREE_VEC:
782 kind = vec_kind;
783 break;
785 case TREE_BINFO:
786 kind = binfo_kind;
787 break;
789 case SSA_NAME:
790 kind = ssa_name_kind;
791 break;
793 case BLOCK:
794 kind = b_kind;
795 break;
797 case CONSTRUCTOR:
798 kind = constr_kind;
799 break;
801 case OMP_CLAUSE:
802 kind = omp_clause_kind;
803 break;
805 default:
806 kind = x_kind;
807 break;
809 break;
811 case tcc_vl_exp:
812 kind = e_kind;
813 break;
815 default:
816 gcc_unreachable ();
819 tree_code_counts[(int) code]++;
820 tree_node_counts[(int) kind]++;
821 tree_node_sizes[(int) kind] += length;
824 /* Allocate and return a new UID from the DECL_UID namespace. */
827 allocate_decl_uid (void)
829 return next_decl_uid++;
832 /* Return a newly allocated node of code CODE. For decl and type
833 nodes, some other fields are initialized. The rest of the node is
834 initialized to zero. This function cannot be used for TREE_VEC or
835 OMP_CLAUSE nodes, which is enforced by asserts in tree_code_size.
837 Achoo! I got a code in the node. */
839 tree
840 make_node_stat (enum tree_code code MEM_STAT_DECL)
842 tree t;
843 enum tree_code_class type = TREE_CODE_CLASS (code);
844 size_t length = tree_code_size (code);
846 record_node_allocation_statistics (code, length);
848 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
849 TREE_SET_CODE (t, code);
851 switch (type)
853 case tcc_statement:
854 TREE_SIDE_EFFECTS (t) = 1;
855 break;
857 case tcc_declaration:
858 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
860 if (code == FUNCTION_DECL)
862 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
863 DECL_MODE (t) = FUNCTION_MODE;
865 else
866 DECL_ALIGN (t) = 1;
868 DECL_SOURCE_LOCATION (t) = input_location;
869 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
870 DECL_UID (t) = --next_debug_decl_uid;
871 else
873 DECL_UID (t) = allocate_decl_uid ();
874 SET_DECL_PT_UID (t, -1);
876 if (TREE_CODE (t) == LABEL_DECL)
877 LABEL_DECL_UID (t) = -1;
879 break;
881 case tcc_type:
882 TYPE_UID (t) = next_type_uid++;
883 TYPE_ALIGN (t) = BITS_PER_UNIT;
884 TYPE_USER_ALIGN (t) = 0;
885 TYPE_MAIN_VARIANT (t) = t;
886 TYPE_CANONICAL (t) = t;
888 /* Default to no attributes for type, but let target change that. */
889 TYPE_ATTRIBUTES (t) = NULL_TREE;
890 targetm.set_default_type_attributes (t);
892 /* We have not yet computed the alias set for this type. */
893 TYPE_ALIAS_SET (t) = -1;
894 break;
896 case tcc_constant:
897 TREE_CONSTANT (t) = 1;
898 break;
900 case tcc_expression:
901 switch (code)
903 case INIT_EXPR:
904 case MODIFY_EXPR:
905 case VA_ARG_EXPR:
906 case PREDECREMENT_EXPR:
907 case PREINCREMENT_EXPR:
908 case POSTDECREMENT_EXPR:
909 case POSTINCREMENT_EXPR:
910 /* All of these have side-effects, no matter what their
911 operands are. */
912 TREE_SIDE_EFFECTS (t) = 1;
913 break;
915 default:
916 break;
918 break;
920 default:
921 /* Other classes need no special treatment. */
922 break;
925 return t;
928 /* Return a new node with the same contents as NODE except that its
929 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
931 tree
932 copy_node_stat (tree node MEM_STAT_DECL)
934 tree t;
935 enum tree_code code = TREE_CODE (node);
936 size_t length;
938 gcc_assert (code != STATEMENT_LIST);
940 length = tree_size (node);
941 record_node_allocation_statistics (code, length);
942 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
943 memcpy (t, node, length);
945 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
946 TREE_CHAIN (t) = 0;
947 TREE_ASM_WRITTEN (t) = 0;
948 TREE_VISITED (t) = 0;
950 if (TREE_CODE_CLASS (code) == tcc_declaration)
952 if (code == DEBUG_EXPR_DECL)
953 DECL_UID (t) = --next_debug_decl_uid;
954 else
956 DECL_UID (t) = allocate_decl_uid ();
957 if (DECL_PT_UID_SET_P (node))
958 SET_DECL_PT_UID (t, DECL_PT_UID (node));
960 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
961 && DECL_HAS_VALUE_EXPR_P (node))
963 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
964 DECL_HAS_VALUE_EXPR_P (t) = 1;
966 /* DECL_DEBUG_EXPR is copied explicitely by callers. */
967 if (TREE_CODE (node) == VAR_DECL)
968 DECL_HAS_DEBUG_EXPR_P (t) = 0;
969 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
971 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
972 DECL_HAS_INIT_PRIORITY_P (t) = 1;
974 if (TREE_CODE (node) == FUNCTION_DECL)
975 DECL_STRUCT_FUNCTION (t) = NULL;
977 else if (TREE_CODE_CLASS (code) == tcc_type)
979 TYPE_UID (t) = next_type_uid++;
980 /* The following is so that the debug code for
981 the copy is different from the original type.
982 The two statements usually duplicate each other
983 (because they clear fields of the same union),
984 but the optimizer should catch that. */
985 TYPE_SYMTAB_POINTER (t) = 0;
986 TYPE_SYMTAB_ADDRESS (t) = 0;
988 /* Do not copy the values cache. */
989 if (TYPE_CACHED_VALUES_P (t))
991 TYPE_CACHED_VALUES_P (t) = 0;
992 TYPE_CACHED_VALUES (t) = NULL_TREE;
996 return t;
999 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1000 For example, this can copy a list made of TREE_LIST nodes. */
1002 tree
1003 copy_list (tree list)
1005 tree head;
1006 tree prev, next;
1008 if (list == 0)
1009 return 0;
1011 head = prev = copy_node (list);
1012 next = TREE_CHAIN (list);
1013 while (next)
1015 TREE_CHAIN (prev) = copy_node (next);
1016 prev = TREE_CHAIN (prev);
1017 next = TREE_CHAIN (next);
1019 return head;
1023 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1025 tree
1026 build_int_cst (tree type, HOST_WIDE_INT low)
1028 /* Support legacy code. */
1029 if (!type)
1030 type = integer_type_node;
1032 return double_int_to_tree (type, double_int::from_shwi (low));
1035 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1037 tree
1038 build_int_cst_type (tree type, HOST_WIDE_INT low)
1040 gcc_assert (type);
1042 return double_int_to_tree (type, double_int::from_shwi (low));
1045 /* Constructs tree in type TYPE from with value given by CST. Signedness
1046 of CST is assumed to be the same as the signedness of TYPE. */
1048 tree
1049 double_int_to_tree (tree type, double_int cst)
1051 bool sign_extended_type = !TYPE_UNSIGNED (type);
1053 cst = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1055 return build_int_cst_wide (type, cst.low, cst.high);
1058 /* Returns true if CST fits into range of TYPE. Signedness of CST is assumed
1059 to be the same as the signedness of TYPE. */
1061 bool
1062 double_int_fits_to_tree_p (const_tree type, double_int cst)
1064 bool sign_extended_type = !TYPE_UNSIGNED (type);
1066 double_int ext
1067 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1069 return cst == ext;
1072 /* We force the double_int CST to the range of the type TYPE by sign or
1073 zero extending it. OVERFLOWABLE indicates if we are interested in
1074 overflow of the value, when >0 we are only interested in signed
1075 overflow, for <0 we are interested in any overflow. OVERFLOWED
1076 indicates whether overflow has already occurred. CONST_OVERFLOWED
1077 indicates whether constant overflow has already occurred. We force
1078 T's value to be within range of T's type (by setting to 0 or 1 all
1079 the bits outside the type's range). We set TREE_OVERFLOWED if,
1080 OVERFLOWED is nonzero,
1081 or OVERFLOWABLE is >0 and signed overflow occurs
1082 or OVERFLOWABLE is <0 and any overflow occurs
1083 We return a new tree node for the extended double_int. The node
1084 is shared if no overflow flags are set. */
1087 tree
1088 force_fit_type_double (tree type, double_int cst, int overflowable,
1089 bool overflowed)
1091 bool sign_extended_type = !TYPE_UNSIGNED (type);
1093 /* If we need to set overflow flags, return a new unshared node. */
1094 if (overflowed || !double_int_fits_to_tree_p (type, cst))
1096 if (overflowed
1097 || overflowable < 0
1098 || (overflowable > 0 && sign_extended_type))
1100 tree t = make_node (INTEGER_CST);
1101 TREE_INT_CST (t)
1102 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1103 TREE_TYPE (t) = type;
1104 TREE_OVERFLOW (t) = 1;
1105 return t;
1109 /* Else build a shared node. */
1110 return double_int_to_tree (type, cst);
1113 /* These are the hash table functions for the hash table of INTEGER_CST
1114 nodes of a sizetype. */
1116 /* Return the hash code code X, an INTEGER_CST. */
1118 static hashval_t
1119 int_cst_hash_hash (const void *x)
1121 const_tree const t = (const_tree) x;
1123 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1124 ^ TYPE_UID (TREE_TYPE (t)));
1127 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1128 is the same as that given by *Y, which is the same. */
1130 static int
1131 int_cst_hash_eq (const void *x, const void *y)
1133 const_tree const xt = (const_tree) x;
1134 const_tree const yt = (const_tree) y;
1136 return (TREE_TYPE (xt) == TREE_TYPE (yt)
1137 && TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1138 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt));
1141 /* Create an INT_CST node of TYPE and value HI:LOW.
1142 The returned node is always shared. For small integers we use a
1143 per-type vector cache, for larger ones we use a single hash table. */
1145 tree
1146 build_int_cst_wide (tree type, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
1148 tree t;
1149 int ix = -1;
1150 int limit = 0;
1152 gcc_assert (type);
1154 switch (TREE_CODE (type))
1156 case NULLPTR_TYPE:
1157 gcc_assert (hi == 0 && low == 0);
1158 /* Fallthru. */
1160 case POINTER_TYPE:
1161 case REFERENCE_TYPE:
1162 /* Cache NULL pointer. */
1163 if (!hi && !low)
1165 limit = 1;
1166 ix = 0;
1168 break;
1170 case BOOLEAN_TYPE:
1171 /* Cache false or true. */
1172 limit = 2;
1173 if (!hi && low < 2)
1174 ix = low;
1175 break;
1177 case INTEGER_TYPE:
1178 case OFFSET_TYPE:
1179 if (TYPE_UNSIGNED (type))
1181 /* Cache 0..N */
1182 limit = INTEGER_SHARE_LIMIT;
1183 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1184 ix = low;
1186 else
1188 /* Cache -1..N */
1189 limit = INTEGER_SHARE_LIMIT + 1;
1190 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1191 ix = low + 1;
1192 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
1193 ix = 0;
1195 break;
1197 case ENUMERAL_TYPE:
1198 break;
1200 default:
1201 gcc_unreachable ();
1204 if (ix >= 0)
1206 /* Look for it in the type's vector of small shared ints. */
1207 if (!TYPE_CACHED_VALUES_P (type))
1209 TYPE_CACHED_VALUES_P (type) = 1;
1210 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1213 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1214 if (t)
1216 /* Make sure no one is clobbering the shared constant. */
1217 gcc_assert (TREE_TYPE (t) == type);
1218 gcc_assert (TREE_INT_CST_LOW (t) == low);
1219 gcc_assert (TREE_INT_CST_HIGH (t) == hi);
1221 else
1223 /* Create a new shared int. */
1224 t = make_node (INTEGER_CST);
1226 TREE_INT_CST_LOW (t) = low;
1227 TREE_INT_CST_HIGH (t) = hi;
1228 TREE_TYPE (t) = type;
1230 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1233 else
1235 /* Use the cache of larger shared ints. */
1236 void **slot;
1238 TREE_INT_CST_LOW (int_cst_node) = low;
1239 TREE_INT_CST_HIGH (int_cst_node) = hi;
1240 TREE_TYPE (int_cst_node) = type;
1242 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
1243 t = (tree) *slot;
1244 if (!t)
1246 /* Insert this one into the hash table. */
1247 t = int_cst_node;
1248 *slot = t;
1249 /* Make a new node for next time round. */
1250 int_cst_node = make_node (INTEGER_CST);
1254 return t;
1257 void
1258 cache_integer_cst (tree t)
1260 tree type = TREE_TYPE (t);
1261 HOST_WIDE_INT hi = TREE_INT_CST_HIGH (t);
1262 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (t);
1263 int ix = -1;
1264 int limit = 0;
1266 gcc_assert (!TREE_OVERFLOW (t));
1268 switch (TREE_CODE (type))
1270 case NULLPTR_TYPE:
1271 gcc_assert (hi == 0 && low == 0);
1272 /* Fallthru. */
1274 case POINTER_TYPE:
1275 case REFERENCE_TYPE:
1276 /* Cache NULL pointer. */
1277 if (!hi && !low)
1279 limit = 1;
1280 ix = 0;
1282 break;
1284 case BOOLEAN_TYPE:
1285 /* Cache false or true. */
1286 limit = 2;
1287 if (!hi && low < 2)
1288 ix = low;
1289 break;
1291 case INTEGER_TYPE:
1292 case OFFSET_TYPE:
1293 if (TYPE_UNSIGNED (type))
1295 /* Cache 0..N */
1296 limit = INTEGER_SHARE_LIMIT;
1297 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1298 ix = low;
1300 else
1302 /* Cache -1..N */
1303 limit = INTEGER_SHARE_LIMIT + 1;
1304 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1305 ix = low + 1;
1306 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
1307 ix = 0;
1309 break;
1311 case ENUMERAL_TYPE:
1312 break;
1314 default:
1315 gcc_unreachable ();
1318 if (ix >= 0)
1320 /* Look for it in the type's vector of small shared ints. */
1321 if (!TYPE_CACHED_VALUES_P (type))
1323 TYPE_CACHED_VALUES_P (type) = 1;
1324 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1327 gcc_assert (TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) == NULL_TREE);
1328 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1330 else
1332 /* Use the cache of larger shared ints. */
1333 void **slot;
1335 slot = htab_find_slot (int_cst_hash_table, t, INSERT);
1336 /* If there is already an entry for the number verify it's the
1337 same. */
1338 if (*slot)
1340 gcc_assert (TREE_INT_CST_LOW ((tree)*slot) == low
1341 && TREE_INT_CST_HIGH ((tree)*slot) == hi);
1342 return;
1344 /* Otherwise insert this one into the hash table. */
1345 *slot = t;
1350 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1351 and the rest are zeros. */
1353 tree
1354 build_low_bits_mask (tree type, unsigned bits)
1356 double_int mask;
1358 gcc_assert (bits <= TYPE_PRECISION (type));
1360 if (bits == TYPE_PRECISION (type)
1361 && !TYPE_UNSIGNED (type))
1362 /* Sign extended all-ones mask. */
1363 mask = double_int_minus_one;
1364 else
1365 mask = double_int::mask (bits);
1367 return build_int_cst_wide (type, mask.low, mask.high);
1370 /* Checks that X is integer constant that can be expressed in (unsigned)
1371 HOST_WIDE_INT without loss of precision. */
1373 bool
1374 cst_and_fits_in_hwi (const_tree x)
1376 if (TREE_CODE (x) != INTEGER_CST)
1377 return false;
1379 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1380 return false;
1382 return (TREE_INT_CST_HIGH (x) == 0
1383 || TREE_INT_CST_HIGH (x) == -1);
1386 /* Build a newly constructed TREE_VEC node of length LEN. */
1388 tree
1389 make_vector_stat (unsigned len MEM_STAT_DECL)
1391 tree t;
1392 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1394 record_node_allocation_statistics (VECTOR_CST, length);
1396 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1398 TREE_SET_CODE (t, VECTOR_CST);
1399 TREE_CONSTANT (t) = 1;
1401 return t;
1404 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1405 are in a list pointed to by VALS. */
1407 tree
1408 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1410 int over = 0;
1411 unsigned cnt = 0;
1412 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1413 TREE_TYPE (v) = type;
1415 /* Iterate through elements and check for overflow. */
1416 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1418 tree value = vals[cnt];
1420 VECTOR_CST_ELT (v, cnt) = value;
1422 /* Don't crash if we get an address constant. */
1423 if (!CONSTANT_CLASS_P (value))
1424 continue;
1426 over |= TREE_OVERFLOW (value);
1429 TREE_OVERFLOW (v) = over;
1430 return v;
1433 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1434 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1436 tree
1437 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1439 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1440 unsigned HOST_WIDE_INT idx;
1441 tree value;
1443 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1444 vec[idx] = value;
1445 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1446 vec[idx] = build_zero_cst (TREE_TYPE (type));
1448 return build_vector (type, vec);
1451 /* Build a vector of type VECTYPE where all the elements are SCs. */
1452 tree
1453 build_vector_from_val (tree vectype, tree sc)
1455 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1457 if (sc == error_mark_node)
1458 return sc;
1460 /* Verify that the vector type is suitable for SC. Note that there
1461 is some inconsistency in the type-system with respect to restrict
1462 qualifications of pointers. Vector types always have a main-variant
1463 element type and the qualification is applied to the vector-type.
1464 So TREE_TYPE (vector-type) does not return a properly qualified
1465 vector element-type. */
1466 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1467 TREE_TYPE (vectype)));
1469 if (CONSTANT_CLASS_P (sc))
1471 tree *v = XALLOCAVEC (tree, nunits);
1472 for (i = 0; i < nunits; ++i)
1473 v[i] = sc;
1474 return build_vector (vectype, v);
1476 else
1478 vec<constructor_elt, va_gc> *v;
1479 vec_alloc (v, nunits);
1480 for (i = 0; i < nunits; ++i)
1481 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1482 return build_constructor (vectype, v);
1486 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1487 are in the vec pointed to by VALS. */
1488 tree
1489 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1491 tree c = make_node (CONSTRUCTOR);
1492 unsigned int i;
1493 constructor_elt *elt;
1494 bool constant_p = true;
1495 bool side_effects_p = false;
1497 TREE_TYPE (c) = type;
1498 CONSTRUCTOR_ELTS (c) = vals;
1500 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1502 /* Mostly ctors will have elts that don't have side-effects, so
1503 the usual case is to scan all the elements. Hence a single
1504 loop for both const and side effects, rather than one loop
1505 each (with early outs). */
1506 if (!TREE_CONSTANT (elt->value))
1507 constant_p = false;
1508 if (TREE_SIDE_EFFECTS (elt->value))
1509 side_effects_p = true;
1512 TREE_SIDE_EFFECTS (c) = side_effects_p;
1513 TREE_CONSTANT (c) = constant_p;
1515 return c;
1518 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1519 INDEX and VALUE. */
1520 tree
1521 build_constructor_single (tree type, tree index, tree value)
1523 vec<constructor_elt, va_gc> *v;
1524 constructor_elt elt = {index, value};
1526 vec_alloc (v, 1);
1527 v->quick_push (elt);
1529 return build_constructor (type, v);
1533 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1534 are in a list pointed to by VALS. */
1535 tree
1536 build_constructor_from_list (tree type, tree vals)
1538 tree t;
1539 vec<constructor_elt, va_gc> *v = NULL;
1541 if (vals)
1543 vec_alloc (v, list_length (vals));
1544 for (t = vals; t; t = TREE_CHAIN (t))
1545 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1548 return build_constructor (type, v);
1551 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
1552 of elements, provided as index/value pairs. */
1554 tree
1555 build_constructor_va (tree type, int nelts, ...)
1557 vec<constructor_elt, va_gc> *v = NULL;
1558 va_list p;
1560 va_start (p, nelts);
1561 vec_alloc (v, nelts);
1562 while (nelts--)
1564 tree index = va_arg (p, tree);
1565 tree value = va_arg (p, tree);
1566 CONSTRUCTOR_APPEND_ELT (v, index, value);
1568 va_end (p);
1569 return build_constructor (type, v);
1572 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1574 tree
1575 build_fixed (tree type, FIXED_VALUE_TYPE f)
1577 tree v;
1578 FIXED_VALUE_TYPE *fp;
1580 v = make_node (FIXED_CST);
1581 fp = ggc_alloc_fixed_value ();
1582 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1584 TREE_TYPE (v) = type;
1585 TREE_FIXED_CST_PTR (v) = fp;
1586 return v;
1589 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1591 tree
1592 build_real (tree type, REAL_VALUE_TYPE d)
1594 tree v;
1595 REAL_VALUE_TYPE *dp;
1596 int overflow = 0;
1598 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1599 Consider doing it via real_convert now. */
1601 v = make_node (REAL_CST);
1602 dp = ggc_alloc_real_value ();
1603 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1605 TREE_TYPE (v) = type;
1606 TREE_REAL_CST_PTR (v) = dp;
1607 TREE_OVERFLOW (v) = overflow;
1608 return v;
1611 /* Return a new REAL_CST node whose type is TYPE
1612 and whose value is the integer value of the INTEGER_CST node I. */
1614 REAL_VALUE_TYPE
1615 real_value_from_int_cst (const_tree type, const_tree i)
1617 REAL_VALUE_TYPE d;
1619 /* Clear all bits of the real value type so that we can later do
1620 bitwise comparisons to see if two values are the same. */
1621 memset (&d, 0, sizeof d);
1623 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
1624 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
1625 TYPE_UNSIGNED (TREE_TYPE (i)));
1626 return d;
1629 /* Given a tree representing an integer constant I, return a tree
1630 representing the same value as a floating-point constant of type TYPE. */
1632 tree
1633 build_real_from_int_cst (tree type, const_tree i)
1635 tree v;
1636 int overflow = TREE_OVERFLOW (i);
1638 v = build_real (type, real_value_from_int_cst (type, i));
1640 TREE_OVERFLOW (v) |= overflow;
1641 return v;
1644 /* Return a newly constructed STRING_CST node whose value is
1645 the LEN characters at STR.
1646 Note that for a C string literal, LEN should include the trailing NUL.
1647 The TREE_TYPE is not initialized. */
1649 tree
1650 build_string (int len, const char *str)
1652 tree s;
1653 size_t length;
1655 /* Do not waste bytes provided by padding of struct tree_string. */
1656 length = len + offsetof (struct tree_string, str) + 1;
1658 record_node_allocation_statistics (STRING_CST, length);
1660 s = ggc_alloc_tree_node (length);
1662 memset (s, 0, sizeof (struct tree_typed));
1663 TREE_SET_CODE (s, STRING_CST);
1664 TREE_CONSTANT (s) = 1;
1665 TREE_STRING_LENGTH (s) = len;
1666 memcpy (s->string.str, str, len);
1667 s->string.str[len] = '\0';
1669 return s;
1672 /* Return a newly constructed COMPLEX_CST node whose value is
1673 specified by the real and imaginary parts REAL and IMAG.
1674 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1675 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1677 tree
1678 build_complex (tree type, tree real, tree imag)
1680 tree t = make_node (COMPLEX_CST);
1682 TREE_REALPART (t) = real;
1683 TREE_IMAGPART (t) = imag;
1684 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1685 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1686 return t;
1689 /* Return a constant of arithmetic type TYPE which is the
1690 multiplicative identity of the set TYPE. */
1692 tree
1693 build_one_cst (tree type)
1695 switch (TREE_CODE (type))
1697 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1698 case POINTER_TYPE: case REFERENCE_TYPE:
1699 case OFFSET_TYPE:
1700 return build_int_cst (type, 1);
1702 case REAL_TYPE:
1703 return build_real (type, dconst1);
1705 case FIXED_POINT_TYPE:
1706 /* We can only generate 1 for accum types. */
1707 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1708 return build_fixed (type, FCONST1 (TYPE_MODE (type)));
1710 case VECTOR_TYPE:
1712 tree scalar = build_one_cst (TREE_TYPE (type));
1714 return build_vector_from_val (type, scalar);
1717 case COMPLEX_TYPE:
1718 return build_complex (type,
1719 build_one_cst (TREE_TYPE (type)),
1720 build_zero_cst (TREE_TYPE (type)));
1722 default:
1723 gcc_unreachable ();
1727 /* Return an integer of type TYPE containing all 1's in as much precision as
1728 it contains, or a complex or vector whose subparts are such integers. */
1730 tree
1731 build_all_ones_cst (tree type)
1733 if (TREE_CODE (type) == COMPLEX_TYPE)
1735 tree scalar = build_all_ones_cst (TREE_TYPE (type));
1736 return build_complex (type, scalar, scalar);
1738 else
1739 return build_minus_one_cst (type);
1742 /* Return a constant of arithmetic type TYPE which is the
1743 opposite of the multiplicative identity of the set TYPE. */
1745 tree
1746 build_minus_one_cst (tree type)
1748 switch (TREE_CODE (type))
1750 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1751 case POINTER_TYPE: case REFERENCE_TYPE:
1752 case OFFSET_TYPE:
1753 return build_int_cst (type, -1);
1755 case REAL_TYPE:
1756 return build_real (type, dconstm1);
1758 case FIXED_POINT_TYPE:
1759 /* We can only generate 1 for accum types. */
1760 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1761 return build_fixed (type, fixed_from_double_int (double_int_minus_one,
1762 TYPE_MODE (type)));
1764 case VECTOR_TYPE:
1766 tree scalar = build_minus_one_cst (TREE_TYPE (type));
1768 return build_vector_from_val (type, scalar);
1771 case COMPLEX_TYPE:
1772 return build_complex (type,
1773 build_minus_one_cst (TREE_TYPE (type)),
1774 build_zero_cst (TREE_TYPE (type)));
1776 default:
1777 gcc_unreachable ();
1781 /* Build 0 constant of type TYPE. This is used by constructor folding
1782 and thus the constant should be represented in memory by
1783 zero(es). */
1785 tree
1786 build_zero_cst (tree type)
1788 switch (TREE_CODE (type))
1790 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1791 case POINTER_TYPE: case REFERENCE_TYPE:
1792 case OFFSET_TYPE: case NULLPTR_TYPE:
1793 return build_int_cst (type, 0);
1795 case REAL_TYPE:
1796 return build_real (type, dconst0);
1798 case FIXED_POINT_TYPE:
1799 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
1801 case VECTOR_TYPE:
1803 tree scalar = build_zero_cst (TREE_TYPE (type));
1805 return build_vector_from_val (type, scalar);
1808 case COMPLEX_TYPE:
1810 tree zero = build_zero_cst (TREE_TYPE (type));
1812 return build_complex (type, zero, zero);
1815 default:
1816 if (!AGGREGATE_TYPE_P (type))
1817 return fold_convert (type, integer_zero_node);
1818 return build_constructor (type, NULL);
1823 /* Build a BINFO with LEN language slots. */
1825 tree
1826 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1828 tree t;
1829 size_t length = (offsetof (struct tree_binfo, base_binfos)
1830 + vec<tree, va_gc>::embedded_size (base_binfos));
1832 record_node_allocation_statistics (TREE_BINFO, length);
1834 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1836 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1838 TREE_SET_CODE (t, TREE_BINFO);
1840 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
1842 return t;
1845 /* Create a CASE_LABEL_EXPR tree node and return it. */
1847 tree
1848 build_case_label (tree low_value, tree high_value, tree label_decl)
1850 tree t = make_node (CASE_LABEL_EXPR);
1852 TREE_TYPE (t) = void_type_node;
1853 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
1855 CASE_LOW (t) = low_value;
1856 CASE_HIGH (t) = high_value;
1857 CASE_LABEL (t) = label_decl;
1858 CASE_CHAIN (t) = NULL_TREE;
1860 return t;
1863 /* Build a newly constructed TREE_VEC node of length LEN. */
1865 tree
1866 make_tree_vec_stat (int len MEM_STAT_DECL)
1868 tree t;
1869 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1871 record_node_allocation_statistics (TREE_VEC, length);
1873 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1875 TREE_SET_CODE (t, TREE_VEC);
1876 TREE_VEC_LENGTH (t) = len;
1878 return t;
1881 /* Grow a TREE_VEC node to new length LEN. */
1883 tree
1884 grow_tree_vec_stat (tree v, int len MEM_STAT_DECL)
1886 gcc_assert (TREE_CODE (v) == TREE_VEC);
1888 int oldlen = TREE_VEC_LENGTH (v);
1889 gcc_assert (len > oldlen);
1891 int oldlength = (oldlen - 1) * sizeof (tree) + sizeof (struct tree_vec);
1892 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1894 record_node_allocation_statistics (TREE_VEC, length - oldlength);
1896 v = (tree) ggc_realloc_stat (v, length PASS_MEM_STAT);
1898 TREE_VEC_LENGTH (v) = len;
1900 return v;
1903 /* Return 1 if EXPR is the integer constant zero or a complex constant
1904 of zero. */
1907 integer_zerop (const_tree expr)
1909 STRIP_NOPS (expr);
1911 switch (TREE_CODE (expr))
1913 case INTEGER_CST:
1914 return (TREE_INT_CST_LOW (expr) == 0
1915 && TREE_INT_CST_HIGH (expr) == 0);
1916 case COMPLEX_CST:
1917 return (integer_zerop (TREE_REALPART (expr))
1918 && integer_zerop (TREE_IMAGPART (expr)));
1919 case VECTOR_CST:
1921 unsigned i;
1922 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1923 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
1924 return false;
1925 return true;
1927 default:
1928 return false;
1932 /* Return 1 if EXPR is the integer constant one or the corresponding
1933 complex constant. */
1936 integer_onep (const_tree expr)
1938 STRIP_NOPS (expr);
1940 switch (TREE_CODE (expr))
1942 case INTEGER_CST:
1943 return (TREE_INT_CST_LOW (expr) == 1
1944 && TREE_INT_CST_HIGH (expr) == 0);
1945 case COMPLEX_CST:
1946 return (integer_onep (TREE_REALPART (expr))
1947 && integer_zerop (TREE_IMAGPART (expr)));
1948 case VECTOR_CST:
1950 unsigned i;
1951 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1952 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
1953 return false;
1954 return true;
1956 default:
1957 return false;
1961 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1962 it contains, or a complex or vector whose subparts are such integers. */
1965 integer_all_onesp (const_tree expr)
1967 int prec;
1968 int uns;
1970 STRIP_NOPS (expr);
1972 if (TREE_CODE (expr) == COMPLEX_CST
1973 && integer_all_onesp (TREE_REALPART (expr))
1974 && integer_all_onesp (TREE_IMAGPART (expr)))
1975 return 1;
1977 else if (TREE_CODE (expr) == VECTOR_CST)
1979 unsigned i;
1980 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1981 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
1982 return 0;
1983 return 1;
1986 else if (TREE_CODE (expr) != INTEGER_CST)
1987 return 0;
1989 uns = TYPE_UNSIGNED (TREE_TYPE (expr));
1990 if (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1991 && TREE_INT_CST_HIGH (expr) == -1)
1992 return 1;
1993 if (!uns)
1994 return 0;
1996 prec = TYPE_PRECISION (TREE_TYPE (expr));
1997 if (prec >= HOST_BITS_PER_WIDE_INT)
1999 HOST_WIDE_INT high_value;
2000 int shift_amount;
2002 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
2004 /* Can not handle precisions greater than twice the host int size. */
2005 gcc_assert (shift_amount <= HOST_BITS_PER_WIDE_INT);
2006 if (shift_amount == HOST_BITS_PER_WIDE_INT)
2007 /* Shifting by the host word size is undefined according to the ANSI
2008 standard, so we must handle this as a special case. */
2009 high_value = -1;
2010 else
2011 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
2013 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
2014 && TREE_INT_CST_HIGH (expr) == high_value);
2016 else
2017 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
2020 /* Return 1 if EXPR is the integer constant minus one. */
2023 integer_minus_onep (const_tree expr)
2025 STRIP_NOPS (expr);
2027 if (TREE_CODE (expr) == COMPLEX_CST)
2028 return (integer_all_onesp (TREE_REALPART (expr))
2029 && integer_zerop (TREE_IMAGPART (expr)));
2030 else
2031 return integer_all_onesp (expr);
2034 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
2035 one bit on). */
2038 integer_pow2p (const_tree expr)
2040 int prec;
2041 unsigned HOST_WIDE_INT high, low;
2043 STRIP_NOPS (expr);
2045 if (TREE_CODE (expr) == COMPLEX_CST
2046 && integer_pow2p (TREE_REALPART (expr))
2047 && integer_zerop (TREE_IMAGPART (expr)))
2048 return 1;
2050 if (TREE_CODE (expr) != INTEGER_CST)
2051 return 0;
2053 prec = TYPE_PRECISION (TREE_TYPE (expr));
2054 high = TREE_INT_CST_HIGH (expr);
2055 low = TREE_INT_CST_LOW (expr);
2057 /* First clear all bits that are beyond the type's precision in case
2058 we've been sign extended. */
2060 if (prec == HOST_BITS_PER_DOUBLE_INT)
2062 else if (prec > HOST_BITS_PER_WIDE_INT)
2063 high &= ~(HOST_WIDE_INT_M1U << (prec - HOST_BITS_PER_WIDE_INT));
2064 else
2066 high = 0;
2067 if (prec < HOST_BITS_PER_WIDE_INT)
2068 low &= ~(HOST_WIDE_INT_M1U << prec);
2071 if (high == 0 && low == 0)
2072 return 0;
2074 return ((high == 0 && (low & (low - 1)) == 0)
2075 || (low == 0 && (high & (high - 1)) == 0));
2078 /* Return 1 if EXPR is an integer constant other than zero or a
2079 complex constant other than zero. */
2082 integer_nonzerop (const_tree expr)
2084 STRIP_NOPS (expr);
2086 return ((TREE_CODE (expr) == INTEGER_CST
2087 && (TREE_INT_CST_LOW (expr) != 0
2088 || TREE_INT_CST_HIGH (expr) != 0))
2089 || (TREE_CODE (expr) == COMPLEX_CST
2090 && (integer_nonzerop (TREE_REALPART (expr))
2091 || integer_nonzerop (TREE_IMAGPART (expr)))));
2094 /* Return 1 if EXPR is the fixed-point constant zero. */
2097 fixed_zerop (const_tree expr)
2099 return (TREE_CODE (expr) == FIXED_CST
2100 && TREE_FIXED_CST (expr).data.is_zero ());
2103 /* Return the power of two represented by a tree node known to be a
2104 power of two. */
2107 tree_log2 (const_tree expr)
2109 int prec;
2110 HOST_WIDE_INT high, low;
2112 STRIP_NOPS (expr);
2114 if (TREE_CODE (expr) == COMPLEX_CST)
2115 return tree_log2 (TREE_REALPART (expr));
2117 prec = TYPE_PRECISION (TREE_TYPE (expr));
2118 high = TREE_INT_CST_HIGH (expr);
2119 low = TREE_INT_CST_LOW (expr);
2121 /* First clear all bits that are beyond the type's precision in case
2122 we've been sign extended. */
2124 if (prec == HOST_BITS_PER_DOUBLE_INT)
2126 else if (prec > HOST_BITS_PER_WIDE_INT)
2127 high &= ~(HOST_WIDE_INT_M1U << (prec - HOST_BITS_PER_WIDE_INT));
2128 else
2130 high = 0;
2131 if (prec < HOST_BITS_PER_WIDE_INT)
2132 low &= ~(HOST_WIDE_INT_M1U << prec);
2135 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
2136 : exact_log2 (low));
2139 /* Similar, but return the largest integer Y such that 2 ** Y is less
2140 than or equal to EXPR. */
2143 tree_floor_log2 (const_tree expr)
2145 int prec;
2146 HOST_WIDE_INT high, low;
2148 STRIP_NOPS (expr);
2150 if (TREE_CODE (expr) == COMPLEX_CST)
2151 return tree_log2 (TREE_REALPART (expr));
2153 prec = TYPE_PRECISION (TREE_TYPE (expr));
2154 high = TREE_INT_CST_HIGH (expr);
2155 low = TREE_INT_CST_LOW (expr);
2157 /* First clear all bits that are beyond the type's precision in case
2158 we've been sign extended. Ignore if type's precision hasn't been set
2159 since what we are doing is setting it. */
2161 if (prec == HOST_BITS_PER_DOUBLE_INT || prec == 0)
2163 else if (prec > HOST_BITS_PER_WIDE_INT)
2164 high &= ~(HOST_WIDE_INT_M1U << (prec - HOST_BITS_PER_WIDE_INT));
2165 else
2167 high = 0;
2168 if (prec < HOST_BITS_PER_WIDE_INT)
2169 low &= ~(HOST_WIDE_INT_M1U << prec);
2172 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
2173 : floor_log2 (low));
2176 /* Return number of known trailing zero bits in EXPR, or, if the value of
2177 EXPR is known to be zero, the precision of it's type. */
2179 unsigned int
2180 tree_ctz (const_tree expr)
2182 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
2183 && !POINTER_TYPE_P (TREE_TYPE (expr)))
2184 return 0;
2186 unsigned int ret1, ret2, prec = TYPE_PRECISION (TREE_TYPE (expr));
2187 switch (TREE_CODE (expr))
2189 case INTEGER_CST:
2190 ret1 = tree_to_double_int (expr).trailing_zeros ();
2191 return MIN (ret1, prec);
2192 case SSA_NAME:
2193 ret1 = get_nonzero_bits (expr).trailing_zeros ();
2194 return MIN (ret1, prec);
2195 case PLUS_EXPR:
2196 case MINUS_EXPR:
2197 case BIT_IOR_EXPR:
2198 case BIT_XOR_EXPR:
2199 case MIN_EXPR:
2200 case MAX_EXPR:
2201 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2202 if (ret1 == 0)
2203 return ret1;
2204 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2205 return MIN (ret1, ret2);
2206 case POINTER_PLUS_EXPR:
2207 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2208 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2209 /* Second operand is sizetype, which could be in theory
2210 wider than pointer's precision. Make sure we never
2211 return more than prec. */
2212 ret2 = MIN (ret2, prec);
2213 return MIN (ret1, ret2);
2214 case BIT_AND_EXPR:
2215 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2216 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2217 return MAX (ret1, ret2);
2218 case MULT_EXPR:
2219 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2220 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2221 return MIN (ret1 + ret2, prec);
2222 case LSHIFT_EXPR:
2223 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2224 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2225 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2227 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2228 return MIN (ret1 + ret2, prec);
2230 return ret1;
2231 case RSHIFT_EXPR:
2232 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2233 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2235 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2236 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2237 if (ret1 > ret2)
2238 return ret1 - ret2;
2240 return 0;
2241 case TRUNC_DIV_EXPR:
2242 case CEIL_DIV_EXPR:
2243 case FLOOR_DIV_EXPR:
2244 case ROUND_DIV_EXPR:
2245 case EXACT_DIV_EXPR:
2246 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
2247 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) == 1)
2249 int l = tree_log2 (TREE_OPERAND (expr, 1));
2250 if (l >= 0)
2252 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2253 ret2 = l;
2254 if (ret1 > ret2)
2255 return ret1 - ret2;
2258 return 0;
2259 CASE_CONVERT:
2260 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2261 if (ret1 && ret1 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2262 ret1 = prec;
2263 return MIN (ret1, prec);
2264 case SAVE_EXPR:
2265 return tree_ctz (TREE_OPERAND (expr, 0));
2266 case COND_EXPR:
2267 ret1 = tree_ctz (TREE_OPERAND (expr, 1));
2268 if (ret1 == 0)
2269 return 0;
2270 ret2 = tree_ctz (TREE_OPERAND (expr, 2));
2271 return MIN (ret1, ret2);
2272 case COMPOUND_EXPR:
2273 return tree_ctz (TREE_OPERAND (expr, 1));
2274 case ADDR_EXPR:
2275 ret1 = get_pointer_alignment (CONST_CAST_TREE (expr));
2276 if (ret1 > BITS_PER_UNIT)
2278 ret1 = ctz_hwi (ret1 / BITS_PER_UNIT);
2279 return MIN (ret1, prec);
2281 return 0;
2282 default:
2283 return 0;
2287 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
2288 decimal float constants, so don't return 1 for them. */
2291 real_zerop (const_tree expr)
2293 STRIP_NOPS (expr);
2295 switch (TREE_CODE (expr))
2297 case REAL_CST:
2298 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
2299 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2300 case COMPLEX_CST:
2301 return real_zerop (TREE_REALPART (expr))
2302 && real_zerop (TREE_IMAGPART (expr));
2303 case VECTOR_CST:
2305 unsigned i;
2306 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2307 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2308 return false;
2309 return true;
2311 default:
2312 return false;
2316 /* Return 1 if EXPR is the real constant one in real or complex form.
2317 Trailing zeroes matter for decimal float constants, so don't return
2318 1 for them. */
2321 real_onep (const_tree expr)
2323 STRIP_NOPS (expr);
2325 switch (TREE_CODE (expr))
2327 case REAL_CST:
2328 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
2329 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2330 case COMPLEX_CST:
2331 return real_onep (TREE_REALPART (expr))
2332 && real_zerop (TREE_IMAGPART (expr));
2333 case VECTOR_CST:
2335 unsigned i;
2336 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2337 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2338 return false;
2339 return true;
2341 default:
2342 return false;
2346 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2347 matter for decimal float constants, so don't return 1 for them. */
2350 real_minus_onep (const_tree expr)
2352 STRIP_NOPS (expr);
2354 switch (TREE_CODE (expr))
2356 case REAL_CST:
2357 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2358 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2359 case COMPLEX_CST:
2360 return real_minus_onep (TREE_REALPART (expr))
2361 && real_zerop (TREE_IMAGPART (expr));
2362 case VECTOR_CST:
2364 unsigned i;
2365 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2366 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2367 return false;
2368 return true;
2370 default:
2371 return false;
2375 /* Nonzero if EXP is a constant or a cast of a constant. */
2378 really_constant_p (const_tree exp)
2380 /* This is not quite the same as STRIP_NOPS. It does more. */
2381 while (CONVERT_EXPR_P (exp)
2382 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2383 exp = TREE_OPERAND (exp, 0);
2384 return TREE_CONSTANT (exp);
2387 /* Return first list element whose TREE_VALUE is ELEM.
2388 Return 0 if ELEM is not in LIST. */
2390 tree
2391 value_member (tree elem, tree list)
2393 while (list)
2395 if (elem == TREE_VALUE (list))
2396 return list;
2397 list = TREE_CHAIN (list);
2399 return NULL_TREE;
2402 /* Return first list element whose TREE_PURPOSE is ELEM.
2403 Return 0 if ELEM is not in LIST. */
2405 tree
2406 purpose_member (const_tree elem, tree list)
2408 while (list)
2410 if (elem == TREE_PURPOSE (list))
2411 return list;
2412 list = TREE_CHAIN (list);
2414 return NULL_TREE;
2417 /* Return true if ELEM is in V. */
2419 bool
2420 vec_member (const_tree elem, vec<tree, va_gc> *v)
2422 unsigned ix;
2423 tree t;
2424 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2425 if (elem == t)
2426 return true;
2427 return false;
2430 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2431 NULL_TREE. */
2433 tree
2434 chain_index (int idx, tree chain)
2436 for (; chain && idx > 0; --idx)
2437 chain = TREE_CHAIN (chain);
2438 return chain;
2441 /* Return nonzero if ELEM is part of the chain CHAIN. */
2444 chain_member (const_tree elem, const_tree chain)
2446 while (chain)
2448 if (elem == chain)
2449 return 1;
2450 chain = DECL_CHAIN (chain);
2453 return 0;
2456 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2457 We expect a null pointer to mark the end of the chain.
2458 This is the Lisp primitive `length'. */
2461 list_length (const_tree t)
2463 const_tree p = t;
2464 #ifdef ENABLE_TREE_CHECKING
2465 const_tree q = t;
2466 #endif
2467 int len = 0;
2469 while (p)
2471 p = TREE_CHAIN (p);
2472 #ifdef ENABLE_TREE_CHECKING
2473 if (len % 2)
2474 q = TREE_CHAIN (q);
2475 gcc_assert (p != q);
2476 #endif
2477 len++;
2480 return len;
2483 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2484 UNION_TYPE TYPE, or NULL_TREE if none. */
2486 tree
2487 first_field (const_tree type)
2489 tree t = TYPE_FIELDS (type);
2490 while (t && TREE_CODE (t) != FIELD_DECL)
2491 t = TREE_CHAIN (t);
2492 return t;
2495 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2496 by modifying the last node in chain 1 to point to chain 2.
2497 This is the Lisp primitive `nconc'. */
2499 tree
2500 chainon (tree op1, tree op2)
2502 tree t1;
2504 if (!op1)
2505 return op2;
2506 if (!op2)
2507 return op1;
2509 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2510 continue;
2511 TREE_CHAIN (t1) = op2;
2513 #ifdef ENABLE_TREE_CHECKING
2515 tree t2;
2516 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2517 gcc_assert (t2 != t1);
2519 #endif
2521 return op1;
2524 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2526 tree
2527 tree_last (tree chain)
2529 tree next;
2530 if (chain)
2531 while ((next = TREE_CHAIN (chain)))
2532 chain = next;
2533 return chain;
2536 /* Reverse the order of elements in the chain T,
2537 and return the new head of the chain (old last element). */
2539 tree
2540 nreverse (tree t)
2542 tree prev = 0, decl, next;
2543 for (decl = t; decl; decl = next)
2545 /* We shouldn't be using this function to reverse BLOCK chains; we
2546 have blocks_nreverse for that. */
2547 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2548 next = TREE_CHAIN (decl);
2549 TREE_CHAIN (decl) = prev;
2550 prev = decl;
2552 return prev;
2555 /* Return a newly created TREE_LIST node whose
2556 purpose and value fields are PARM and VALUE. */
2558 tree
2559 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2561 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2562 TREE_PURPOSE (t) = parm;
2563 TREE_VALUE (t) = value;
2564 return t;
2567 /* Build a chain of TREE_LIST nodes from a vector. */
2569 tree
2570 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2572 tree ret = NULL_TREE;
2573 tree *pp = &ret;
2574 unsigned int i;
2575 tree t;
2576 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2578 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2579 pp = &TREE_CHAIN (*pp);
2581 return ret;
2584 /* Return a newly created TREE_LIST node whose
2585 purpose and value fields are PURPOSE and VALUE
2586 and whose TREE_CHAIN is CHAIN. */
2588 tree
2589 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2591 tree node;
2593 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
2594 memset (node, 0, sizeof (struct tree_common));
2596 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2598 TREE_SET_CODE (node, TREE_LIST);
2599 TREE_CHAIN (node) = chain;
2600 TREE_PURPOSE (node) = purpose;
2601 TREE_VALUE (node) = value;
2602 return node;
2605 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2606 trees. */
2608 vec<tree, va_gc> *
2609 ctor_to_vec (tree ctor)
2611 vec<tree, va_gc> *vec;
2612 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2613 unsigned int ix;
2614 tree val;
2616 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2617 vec->quick_push (val);
2619 return vec;
2622 /* Return the size nominally occupied by an object of type TYPE
2623 when it resides in memory. The value is measured in units of bytes,
2624 and its data type is that normally used for type sizes
2625 (which is the first type created by make_signed_type or
2626 make_unsigned_type). */
2628 tree
2629 size_in_bytes (const_tree type)
2631 tree t;
2633 if (type == error_mark_node)
2634 return integer_zero_node;
2636 type = TYPE_MAIN_VARIANT (type);
2637 t = TYPE_SIZE_UNIT (type);
2639 if (t == 0)
2641 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2642 return size_zero_node;
2645 return t;
2648 /* Return the size of TYPE (in bytes) as a wide integer
2649 or return -1 if the size can vary or is larger than an integer. */
2651 HOST_WIDE_INT
2652 int_size_in_bytes (const_tree type)
2654 tree t;
2656 if (type == error_mark_node)
2657 return 0;
2659 type = TYPE_MAIN_VARIANT (type);
2660 t = TYPE_SIZE_UNIT (type);
2661 if (t == 0
2662 || TREE_CODE (t) != INTEGER_CST
2663 || TREE_INT_CST_HIGH (t) != 0
2664 /* If the result would appear negative, it's too big to represent. */
2665 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
2666 return -1;
2668 return TREE_INT_CST_LOW (t);
2671 /* Return the maximum size of TYPE (in bytes) as a wide integer
2672 or return -1 if the size can vary or is larger than an integer. */
2674 HOST_WIDE_INT
2675 max_int_size_in_bytes (const_tree type)
2677 HOST_WIDE_INT size = -1;
2678 tree size_tree;
2680 /* If this is an array type, check for a possible MAX_SIZE attached. */
2682 if (TREE_CODE (type) == ARRAY_TYPE)
2684 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2686 if (size_tree && tree_fits_uhwi_p (size_tree))
2687 size = tree_to_uhwi (size_tree);
2690 /* If we still haven't been able to get a size, see if the language
2691 can compute a maximum size. */
2693 if (size == -1)
2695 size_tree = lang_hooks.types.max_size (type);
2697 if (size_tree && tree_fits_uhwi_p (size_tree))
2698 size = tree_to_uhwi (size_tree);
2701 return size;
2704 /* Return the bit position of FIELD, in bits from the start of the record.
2705 This is a tree of type bitsizetype. */
2707 tree
2708 bit_position (const_tree field)
2710 return bit_from_pos (DECL_FIELD_OFFSET (field),
2711 DECL_FIELD_BIT_OFFSET (field));
2714 /* Likewise, but return as an integer. It must be representable in
2715 that way (since it could be a signed value, we don't have the
2716 option of returning -1 like int_size_in_byte can. */
2718 HOST_WIDE_INT
2719 int_bit_position (const_tree field)
2721 return tree_to_shwi (bit_position (field));
2724 /* Return the byte position of FIELD, in bytes from the start of the record.
2725 This is a tree of type sizetype. */
2727 tree
2728 byte_position (const_tree field)
2730 return byte_from_pos (DECL_FIELD_OFFSET (field),
2731 DECL_FIELD_BIT_OFFSET (field));
2734 /* Likewise, but return as an integer. It must be representable in
2735 that way (since it could be a signed value, we don't have the
2736 option of returning -1 like int_size_in_byte can. */
2738 HOST_WIDE_INT
2739 int_byte_position (const_tree field)
2741 return tree_to_shwi (byte_position (field));
2744 /* Return the strictest alignment, in bits, that T is known to have. */
2746 unsigned int
2747 expr_align (const_tree t)
2749 unsigned int align0, align1;
2751 switch (TREE_CODE (t))
2753 CASE_CONVERT: case NON_LVALUE_EXPR:
2754 /* If we have conversions, we know that the alignment of the
2755 object must meet each of the alignments of the types. */
2756 align0 = expr_align (TREE_OPERAND (t, 0));
2757 align1 = TYPE_ALIGN (TREE_TYPE (t));
2758 return MAX (align0, align1);
2760 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2761 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2762 case CLEANUP_POINT_EXPR:
2763 /* These don't change the alignment of an object. */
2764 return expr_align (TREE_OPERAND (t, 0));
2766 case COND_EXPR:
2767 /* The best we can do is say that the alignment is the least aligned
2768 of the two arms. */
2769 align0 = expr_align (TREE_OPERAND (t, 1));
2770 align1 = expr_align (TREE_OPERAND (t, 2));
2771 return MIN (align0, align1);
2773 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2774 meaningfully, it's always 1. */
2775 case LABEL_DECL: case CONST_DECL:
2776 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2777 case FUNCTION_DECL:
2778 gcc_assert (DECL_ALIGN (t) != 0);
2779 return DECL_ALIGN (t);
2781 default:
2782 break;
2785 /* Otherwise take the alignment from that of the type. */
2786 return TYPE_ALIGN (TREE_TYPE (t));
2789 /* Return, as a tree node, the number of elements for TYPE (which is an
2790 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2792 tree
2793 array_type_nelts (const_tree type)
2795 tree index_type, min, max;
2797 /* If they did it with unspecified bounds, then we should have already
2798 given an error about it before we got here. */
2799 if (! TYPE_DOMAIN (type))
2800 return error_mark_node;
2802 index_type = TYPE_DOMAIN (type);
2803 min = TYPE_MIN_VALUE (index_type);
2804 max = TYPE_MAX_VALUE (index_type);
2806 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
2807 if (!max)
2808 return error_mark_node;
2810 return (integer_zerop (min)
2811 ? max
2812 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2815 /* If arg is static -- a reference to an object in static storage -- then
2816 return the object. This is not the same as the C meaning of `static'.
2817 If arg isn't static, return NULL. */
2819 tree
2820 staticp (tree arg)
2822 switch (TREE_CODE (arg))
2824 case FUNCTION_DECL:
2825 /* Nested functions are static, even though taking their address will
2826 involve a trampoline as we unnest the nested function and create
2827 the trampoline on the tree level. */
2828 return arg;
2830 case VAR_DECL:
2831 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2832 && ! DECL_THREAD_LOCAL_P (arg)
2833 && ! DECL_DLLIMPORT_P (arg)
2834 ? arg : NULL);
2836 case CONST_DECL:
2837 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2838 ? arg : NULL);
2840 case CONSTRUCTOR:
2841 return TREE_STATIC (arg) ? arg : NULL;
2843 case LABEL_DECL:
2844 case STRING_CST:
2845 return arg;
2847 case COMPONENT_REF:
2848 /* If the thing being referenced is not a field, then it is
2849 something language specific. */
2850 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
2852 /* If we are referencing a bitfield, we can't evaluate an
2853 ADDR_EXPR at compile time and so it isn't a constant. */
2854 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2855 return NULL;
2857 return staticp (TREE_OPERAND (arg, 0));
2859 case BIT_FIELD_REF:
2860 return NULL;
2862 case INDIRECT_REF:
2863 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2865 case ARRAY_REF:
2866 case ARRAY_RANGE_REF:
2867 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2868 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2869 return staticp (TREE_OPERAND (arg, 0));
2870 else
2871 return NULL;
2873 case COMPOUND_LITERAL_EXPR:
2874 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
2876 default:
2877 return NULL;
2884 /* Return whether OP is a DECL whose address is function-invariant. */
2886 bool
2887 decl_address_invariant_p (const_tree op)
2889 /* The conditions below are slightly less strict than the one in
2890 staticp. */
2892 switch (TREE_CODE (op))
2894 case PARM_DECL:
2895 case RESULT_DECL:
2896 case LABEL_DECL:
2897 case FUNCTION_DECL:
2898 return true;
2900 case VAR_DECL:
2901 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2902 || DECL_THREAD_LOCAL_P (op)
2903 || DECL_CONTEXT (op) == current_function_decl
2904 || decl_function_context (op) == current_function_decl)
2905 return true;
2906 break;
2908 case CONST_DECL:
2909 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2910 || decl_function_context (op) == current_function_decl)
2911 return true;
2912 break;
2914 default:
2915 break;
2918 return false;
2921 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
2923 bool
2924 decl_address_ip_invariant_p (const_tree op)
2926 /* The conditions below are slightly less strict than the one in
2927 staticp. */
2929 switch (TREE_CODE (op))
2931 case LABEL_DECL:
2932 case FUNCTION_DECL:
2933 case STRING_CST:
2934 return true;
2936 case VAR_DECL:
2937 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2938 && !DECL_DLLIMPORT_P (op))
2939 || DECL_THREAD_LOCAL_P (op))
2940 return true;
2941 break;
2943 case CONST_DECL:
2944 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
2945 return true;
2946 break;
2948 default:
2949 break;
2952 return false;
2956 /* Return true if T is function-invariant (internal function, does
2957 not handle arithmetic; that's handled in skip_simple_arithmetic and
2958 tree_invariant_p). */
2960 static bool tree_invariant_p (tree t);
2962 static bool
2963 tree_invariant_p_1 (tree t)
2965 tree op;
2967 if (TREE_CONSTANT (t)
2968 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
2969 return true;
2971 switch (TREE_CODE (t))
2973 case SAVE_EXPR:
2974 return true;
2976 case ADDR_EXPR:
2977 op = TREE_OPERAND (t, 0);
2978 while (handled_component_p (op))
2980 switch (TREE_CODE (op))
2982 case ARRAY_REF:
2983 case ARRAY_RANGE_REF:
2984 if (!tree_invariant_p (TREE_OPERAND (op, 1))
2985 || TREE_OPERAND (op, 2) != NULL_TREE
2986 || TREE_OPERAND (op, 3) != NULL_TREE)
2987 return false;
2988 break;
2990 case COMPONENT_REF:
2991 if (TREE_OPERAND (op, 2) != NULL_TREE)
2992 return false;
2993 break;
2995 default:;
2997 op = TREE_OPERAND (op, 0);
3000 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
3002 default:
3003 break;
3006 return false;
3009 /* Return true if T is function-invariant. */
3011 static bool
3012 tree_invariant_p (tree t)
3014 tree inner = skip_simple_arithmetic (t);
3015 return tree_invariant_p_1 (inner);
3018 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
3019 Do this to any expression which may be used in more than one place,
3020 but must be evaluated only once.
3022 Normally, expand_expr would reevaluate the expression each time.
3023 Calling save_expr produces something that is evaluated and recorded
3024 the first time expand_expr is called on it. Subsequent calls to
3025 expand_expr just reuse the recorded value.
3027 The call to expand_expr that generates code that actually computes
3028 the value is the first call *at compile time*. Subsequent calls
3029 *at compile time* generate code to use the saved value.
3030 This produces correct result provided that *at run time* control
3031 always flows through the insns made by the first expand_expr
3032 before reaching the other places where the save_expr was evaluated.
3033 You, the caller of save_expr, must make sure this is so.
3035 Constants, and certain read-only nodes, are returned with no
3036 SAVE_EXPR because that is safe. Expressions containing placeholders
3037 are not touched; see tree.def for an explanation of what these
3038 are used for. */
3040 tree
3041 save_expr (tree expr)
3043 tree t = fold (expr);
3044 tree inner;
3046 /* If the tree evaluates to a constant, then we don't want to hide that
3047 fact (i.e. this allows further folding, and direct checks for constants).
3048 However, a read-only object that has side effects cannot be bypassed.
3049 Since it is no problem to reevaluate literals, we just return the
3050 literal node. */
3051 inner = skip_simple_arithmetic (t);
3052 if (TREE_CODE (inner) == ERROR_MARK)
3053 return inner;
3055 if (tree_invariant_p_1 (inner))
3056 return t;
3058 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
3059 it means that the size or offset of some field of an object depends on
3060 the value within another field.
3062 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
3063 and some variable since it would then need to be both evaluated once and
3064 evaluated more than once. Front-ends must assure this case cannot
3065 happen by surrounding any such subexpressions in their own SAVE_EXPR
3066 and forcing evaluation at the proper time. */
3067 if (contains_placeholder_p (inner))
3068 return t;
3070 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
3071 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
3073 /* This expression might be placed ahead of a jump to ensure that the
3074 value was computed on both sides of the jump. So make sure it isn't
3075 eliminated as dead. */
3076 TREE_SIDE_EFFECTS (t) = 1;
3077 return t;
3080 /* Look inside EXPR into any simple arithmetic operations. Return the
3081 outermost non-arithmetic or non-invariant node. */
3083 tree
3084 skip_simple_arithmetic (tree expr)
3086 /* We don't care about whether this can be used as an lvalue in this
3087 context. */
3088 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3089 expr = TREE_OPERAND (expr, 0);
3091 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
3092 a constant, it will be more efficient to not make another SAVE_EXPR since
3093 it will allow better simplification and GCSE will be able to merge the
3094 computations if they actually occur. */
3095 while (true)
3097 if (UNARY_CLASS_P (expr))
3098 expr = TREE_OPERAND (expr, 0);
3099 else if (BINARY_CLASS_P (expr))
3101 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
3102 expr = TREE_OPERAND (expr, 0);
3103 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
3104 expr = TREE_OPERAND (expr, 1);
3105 else
3106 break;
3108 else
3109 break;
3112 return expr;
3115 /* Look inside EXPR into simple arithmetic operations involving constants.
3116 Return the outermost non-arithmetic or non-constant node. */
3118 tree
3119 skip_simple_constant_arithmetic (tree expr)
3121 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3122 expr = TREE_OPERAND (expr, 0);
3124 while (true)
3126 if (UNARY_CLASS_P (expr))
3127 expr = TREE_OPERAND (expr, 0);
3128 else if (BINARY_CLASS_P (expr))
3130 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3131 expr = TREE_OPERAND (expr, 0);
3132 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3133 expr = TREE_OPERAND (expr, 1);
3134 else
3135 break;
3137 else
3138 break;
3141 return expr;
3144 /* Return which tree structure is used by T. */
3146 enum tree_node_structure_enum
3147 tree_node_structure (const_tree t)
3149 const enum tree_code code = TREE_CODE (t);
3150 return tree_node_structure_for_code (code);
3153 /* Set various status flags when building a CALL_EXPR object T. */
3155 static void
3156 process_call_operands (tree t)
3158 bool side_effects = TREE_SIDE_EFFECTS (t);
3159 bool read_only = false;
3160 int i = call_expr_flags (t);
3162 /* Calls have side-effects, except those to const or pure functions. */
3163 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
3164 side_effects = true;
3165 /* Propagate TREE_READONLY of arguments for const functions. */
3166 if (i & ECF_CONST)
3167 read_only = true;
3169 if (!side_effects || read_only)
3170 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
3172 tree op = TREE_OPERAND (t, i);
3173 if (op && TREE_SIDE_EFFECTS (op))
3174 side_effects = true;
3175 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
3176 read_only = false;
3179 TREE_SIDE_EFFECTS (t) = side_effects;
3180 TREE_READONLY (t) = read_only;
3183 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
3184 size or offset that depends on a field within a record. */
3186 bool
3187 contains_placeholder_p (const_tree exp)
3189 enum tree_code code;
3191 if (!exp)
3192 return 0;
3194 code = TREE_CODE (exp);
3195 if (code == PLACEHOLDER_EXPR)
3196 return 1;
3198 switch (TREE_CODE_CLASS (code))
3200 case tcc_reference:
3201 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
3202 position computations since they will be converted into a
3203 WITH_RECORD_EXPR involving the reference, which will assume
3204 here will be valid. */
3205 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3207 case tcc_exceptional:
3208 if (code == TREE_LIST)
3209 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
3210 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
3211 break;
3213 case tcc_unary:
3214 case tcc_binary:
3215 case tcc_comparison:
3216 case tcc_expression:
3217 switch (code)
3219 case COMPOUND_EXPR:
3220 /* Ignoring the first operand isn't quite right, but works best. */
3221 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
3223 case COND_EXPR:
3224 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3225 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
3226 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
3228 case SAVE_EXPR:
3229 /* The save_expr function never wraps anything containing
3230 a PLACEHOLDER_EXPR. */
3231 return 0;
3233 default:
3234 break;
3237 switch (TREE_CODE_LENGTH (code))
3239 case 1:
3240 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3241 case 2:
3242 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3243 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
3244 default:
3245 return 0;
3248 case tcc_vl_exp:
3249 switch (code)
3251 case CALL_EXPR:
3253 const_tree arg;
3254 const_call_expr_arg_iterator iter;
3255 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
3256 if (CONTAINS_PLACEHOLDER_P (arg))
3257 return 1;
3258 return 0;
3260 default:
3261 return 0;
3264 default:
3265 return 0;
3267 return 0;
3270 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3271 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3272 field positions. */
3274 static bool
3275 type_contains_placeholder_1 (const_tree type)
3277 /* If the size contains a placeholder or the parent type (component type in
3278 the case of arrays) type involves a placeholder, this type does. */
3279 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3280 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3281 || (!POINTER_TYPE_P (type)
3282 && TREE_TYPE (type)
3283 && type_contains_placeholder_p (TREE_TYPE (type))))
3284 return true;
3286 /* Now do type-specific checks. Note that the last part of the check above
3287 greatly limits what we have to do below. */
3288 switch (TREE_CODE (type))
3290 case VOID_TYPE:
3291 case COMPLEX_TYPE:
3292 case ENUMERAL_TYPE:
3293 case BOOLEAN_TYPE:
3294 case POINTER_TYPE:
3295 case OFFSET_TYPE:
3296 case REFERENCE_TYPE:
3297 case METHOD_TYPE:
3298 case FUNCTION_TYPE:
3299 case VECTOR_TYPE:
3300 case NULLPTR_TYPE:
3301 return false;
3303 case INTEGER_TYPE:
3304 case REAL_TYPE:
3305 case FIXED_POINT_TYPE:
3306 /* Here we just check the bounds. */
3307 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3308 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3310 case ARRAY_TYPE:
3311 /* We have already checked the component type above, so just check the
3312 domain type. */
3313 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3315 case RECORD_TYPE:
3316 case UNION_TYPE:
3317 case QUAL_UNION_TYPE:
3319 tree field;
3321 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3322 if (TREE_CODE (field) == FIELD_DECL
3323 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3324 || (TREE_CODE (type) == QUAL_UNION_TYPE
3325 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3326 || type_contains_placeholder_p (TREE_TYPE (field))))
3327 return true;
3329 return false;
3332 default:
3333 gcc_unreachable ();
3337 /* Wrapper around above function used to cache its result. */
3339 bool
3340 type_contains_placeholder_p (tree type)
3342 bool result;
3344 /* If the contains_placeholder_bits field has been initialized,
3345 then we know the answer. */
3346 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3347 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3349 /* Indicate that we've seen this type node, and the answer is false.
3350 This is what we want to return if we run into recursion via fields. */
3351 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3353 /* Compute the real value. */
3354 result = type_contains_placeholder_1 (type);
3356 /* Store the real value. */
3357 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3359 return result;
3362 /* Push tree EXP onto vector QUEUE if it is not already present. */
3364 static void
3365 push_without_duplicates (tree exp, vec<tree> *queue)
3367 unsigned int i;
3368 tree iter;
3370 FOR_EACH_VEC_ELT (*queue, i, iter)
3371 if (simple_cst_equal (iter, exp) == 1)
3372 break;
3374 if (!iter)
3375 queue->safe_push (exp);
3378 /* Given a tree EXP, find all occurrences of references to fields
3379 in a PLACEHOLDER_EXPR and place them in vector REFS without
3380 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3381 we assume here that EXP contains only arithmetic expressions
3382 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3383 argument list. */
3385 void
3386 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3388 enum tree_code code = TREE_CODE (exp);
3389 tree inner;
3390 int i;
3392 /* We handle TREE_LIST and COMPONENT_REF separately. */
3393 if (code == TREE_LIST)
3395 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3396 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3398 else if (code == COMPONENT_REF)
3400 for (inner = TREE_OPERAND (exp, 0);
3401 REFERENCE_CLASS_P (inner);
3402 inner = TREE_OPERAND (inner, 0))
3405 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3406 push_without_duplicates (exp, refs);
3407 else
3408 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3410 else
3411 switch (TREE_CODE_CLASS (code))
3413 case tcc_constant:
3414 break;
3416 case tcc_declaration:
3417 /* Variables allocated to static storage can stay. */
3418 if (!TREE_STATIC (exp))
3419 push_without_duplicates (exp, refs);
3420 break;
3422 case tcc_expression:
3423 /* This is the pattern built in ada/make_aligning_type. */
3424 if (code == ADDR_EXPR
3425 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3427 push_without_duplicates (exp, refs);
3428 break;
3431 /* Fall through... */
3433 case tcc_exceptional:
3434 case tcc_unary:
3435 case tcc_binary:
3436 case tcc_comparison:
3437 case tcc_reference:
3438 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3439 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3440 break;
3442 case tcc_vl_exp:
3443 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3444 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3445 break;
3447 default:
3448 gcc_unreachable ();
3452 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3453 return a tree with all occurrences of references to F in a
3454 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3455 CONST_DECLs. Note that we assume here that EXP contains only
3456 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3457 occurring only in their argument list. */
3459 tree
3460 substitute_in_expr (tree exp, tree f, tree r)
3462 enum tree_code code = TREE_CODE (exp);
3463 tree op0, op1, op2, op3;
3464 tree new_tree;
3466 /* We handle TREE_LIST and COMPONENT_REF separately. */
3467 if (code == TREE_LIST)
3469 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3470 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3471 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3472 return exp;
3474 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3476 else if (code == COMPONENT_REF)
3478 tree inner;
3480 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3481 and it is the right field, replace it with R. */
3482 for (inner = TREE_OPERAND (exp, 0);
3483 REFERENCE_CLASS_P (inner);
3484 inner = TREE_OPERAND (inner, 0))
3487 /* The field. */
3488 op1 = TREE_OPERAND (exp, 1);
3490 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3491 return r;
3493 /* If this expression hasn't been completed let, leave it alone. */
3494 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3495 return exp;
3497 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3498 if (op0 == TREE_OPERAND (exp, 0))
3499 return exp;
3501 new_tree
3502 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3504 else
3505 switch (TREE_CODE_CLASS (code))
3507 case tcc_constant:
3508 return exp;
3510 case tcc_declaration:
3511 if (exp == f)
3512 return r;
3513 else
3514 return exp;
3516 case tcc_expression:
3517 if (exp == f)
3518 return r;
3520 /* Fall through... */
3522 case tcc_exceptional:
3523 case tcc_unary:
3524 case tcc_binary:
3525 case tcc_comparison:
3526 case tcc_reference:
3527 switch (TREE_CODE_LENGTH (code))
3529 case 0:
3530 return exp;
3532 case 1:
3533 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3534 if (op0 == TREE_OPERAND (exp, 0))
3535 return exp;
3537 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3538 break;
3540 case 2:
3541 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3542 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3544 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3545 return exp;
3547 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3548 break;
3550 case 3:
3551 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3552 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3553 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3555 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3556 && op2 == TREE_OPERAND (exp, 2))
3557 return exp;
3559 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3560 break;
3562 case 4:
3563 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3564 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3565 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3566 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3568 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3569 && op2 == TREE_OPERAND (exp, 2)
3570 && op3 == TREE_OPERAND (exp, 3))
3571 return exp;
3573 new_tree
3574 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3575 break;
3577 default:
3578 gcc_unreachable ();
3580 break;
3582 case tcc_vl_exp:
3584 int i;
3586 new_tree = NULL_TREE;
3588 /* If we are trying to replace F with a constant, inline back
3589 functions which do nothing else than computing a value from
3590 the arguments they are passed. This makes it possible to
3591 fold partially or entirely the replacement expression. */
3592 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3594 tree t = maybe_inline_call_in_expr (exp);
3595 if (t)
3596 return SUBSTITUTE_IN_EXPR (t, f, r);
3599 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3601 tree op = TREE_OPERAND (exp, i);
3602 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3603 if (new_op != op)
3605 if (!new_tree)
3606 new_tree = copy_node (exp);
3607 TREE_OPERAND (new_tree, i) = new_op;
3611 if (new_tree)
3613 new_tree = fold (new_tree);
3614 if (TREE_CODE (new_tree) == CALL_EXPR)
3615 process_call_operands (new_tree);
3617 else
3618 return exp;
3620 break;
3622 default:
3623 gcc_unreachable ();
3626 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3628 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3629 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3631 return new_tree;
3634 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3635 for it within OBJ, a tree that is an object or a chain of references. */
3637 tree
3638 substitute_placeholder_in_expr (tree exp, tree obj)
3640 enum tree_code code = TREE_CODE (exp);
3641 tree op0, op1, op2, op3;
3642 tree new_tree;
3644 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3645 in the chain of OBJ. */
3646 if (code == PLACEHOLDER_EXPR)
3648 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3649 tree elt;
3651 for (elt = obj; elt != 0;
3652 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3653 || TREE_CODE (elt) == COND_EXPR)
3654 ? TREE_OPERAND (elt, 1)
3655 : (REFERENCE_CLASS_P (elt)
3656 || UNARY_CLASS_P (elt)
3657 || BINARY_CLASS_P (elt)
3658 || VL_EXP_CLASS_P (elt)
3659 || EXPRESSION_CLASS_P (elt))
3660 ? TREE_OPERAND (elt, 0) : 0))
3661 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3662 return elt;
3664 for (elt = obj; elt != 0;
3665 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3666 || TREE_CODE (elt) == COND_EXPR)
3667 ? TREE_OPERAND (elt, 1)
3668 : (REFERENCE_CLASS_P (elt)
3669 || UNARY_CLASS_P (elt)
3670 || BINARY_CLASS_P (elt)
3671 || VL_EXP_CLASS_P (elt)
3672 || EXPRESSION_CLASS_P (elt))
3673 ? TREE_OPERAND (elt, 0) : 0))
3674 if (POINTER_TYPE_P (TREE_TYPE (elt))
3675 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3676 == need_type))
3677 return fold_build1 (INDIRECT_REF, need_type, elt);
3679 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3680 survives until RTL generation, there will be an error. */
3681 return exp;
3684 /* TREE_LIST is special because we need to look at TREE_VALUE
3685 and TREE_CHAIN, not TREE_OPERANDS. */
3686 else if (code == TREE_LIST)
3688 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3689 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3690 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3691 return exp;
3693 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3695 else
3696 switch (TREE_CODE_CLASS (code))
3698 case tcc_constant:
3699 case tcc_declaration:
3700 return exp;
3702 case tcc_exceptional:
3703 case tcc_unary:
3704 case tcc_binary:
3705 case tcc_comparison:
3706 case tcc_expression:
3707 case tcc_reference:
3708 case tcc_statement:
3709 switch (TREE_CODE_LENGTH (code))
3711 case 0:
3712 return exp;
3714 case 1:
3715 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3716 if (op0 == TREE_OPERAND (exp, 0))
3717 return exp;
3719 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3720 break;
3722 case 2:
3723 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3724 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3726 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3727 return exp;
3729 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3730 break;
3732 case 3:
3733 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3734 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3735 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3737 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3738 && op2 == TREE_OPERAND (exp, 2))
3739 return exp;
3741 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3742 break;
3744 case 4:
3745 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3746 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3747 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3748 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3750 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3751 && op2 == TREE_OPERAND (exp, 2)
3752 && op3 == TREE_OPERAND (exp, 3))
3753 return exp;
3755 new_tree
3756 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3757 break;
3759 default:
3760 gcc_unreachable ();
3762 break;
3764 case tcc_vl_exp:
3766 int i;
3768 new_tree = NULL_TREE;
3770 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3772 tree op = TREE_OPERAND (exp, i);
3773 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
3774 if (new_op != op)
3776 if (!new_tree)
3777 new_tree = copy_node (exp);
3778 TREE_OPERAND (new_tree, i) = new_op;
3782 if (new_tree)
3784 new_tree = fold (new_tree);
3785 if (TREE_CODE (new_tree) == CALL_EXPR)
3786 process_call_operands (new_tree);
3788 else
3789 return exp;
3791 break;
3793 default:
3794 gcc_unreachable ();
3797 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3799 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3800 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3802 return new_tree;
3806 /* Subroutine of stabilize_reference; this is called for subtrees of
3807 references. Any expression with side-effects must be put in a SAVE_EXPR
3808 to ensure that it is only evaluated once.
3810 We don't put SAVE_EXPR nodes around everything, because assigning very
3811 simple expressions to temporaries causes us to miss good opportunities
3812 for optimizations. Among other things, the opportunity to fold in the
3813 addition of a constant into an addressing mode often gets lost, e.g.
3814 "y[i+1] += x;". In general, we take the approach that we should not make
3815 an assignment unless we are forced into it - i.e., that any non-side effect
3816 operator should be allowed, and that cse should take care of coalescing
3817 multiple utterances of the same expression should that prove fruitful. */
3819 static tree
3820 stabilize_reference_1 (tree e)
3822 tree result;
3823 enum tree_code code = TREE_CODE (e);
3825 /* We cannot ignore const expressions because it might be a reference
3826 to a const array but whose index contains side-effects. But we can
3827 ignore things that are actual constant or that already have been
3828 handled by this function. */
3830 if (tree_invariant_p (e))
3831 return e;
3833 switch (TREE_CODE_CLASS (code))
3835 case tcc_exceptional:
3836 case tcc_type:
3837 case tcc_declaration:
3838 case tcc_comparison:
3839 case tcc_statement:
3840 case tcc_expression:
3841 case tcc_reference:
3842 case tcc_vl_exp:
3843 /* If the expression has side-effects, then encase it in a SAVE_EXPR
3844 so that it will only be evaluated once. */
3845 /* The reference (r) and comparison (<) classes could be handled as
3846 below, but it is generally faster to only evaluate them once. */
3847 if (TREE_SIDE_EFFECTS (e))
3848 return save_expr (e);
3849 return e;
3851 case tcc_constant:
3852 /* Constants need no processing. In fact, we should never reach
3853 here. */
3854 return e;
3856 case tcc_binary:
3857 /* Division is slow and tends to be compiled with jumps,
3858 especially the division by powers of 2 that is often
3859 found inside of an array reference. So do it just once. */
3860 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3861 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3862 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3863 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3864 return save_expr (e);
3865 /* Recursively stabilize each operand. */
3866 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3867 stabilize_reference_1 (TREE_OPERAND (e, 1)));
3868 break;
3870 case tcc_unary:
3871 /* Recursively stabilize each operand. */
3872 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3873 break;
3875 default:
3876 gcc_unreachable ();
3879 TREE_TYPE (result) = TREE_TYPE (e);
3880 TREE_READONLY (result) = TREE_READONLY (e);
3881 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3882 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3884 return result;
3887 /* Stabilize a reference so that we can use it any number of times
3888 without causing its operands to be evaluated more than once.
3889 Returns the stabilized reference. This works by means of save_expr,
3890 so see the caveats in the comments about save_expr.
3892 Also allows conversion expressions whose operands are references.
3893 Any other kind of expression is returned unchanged. */
3895 tree
3896 stabilize_reference (tree ref)
3898 tree result;
3899 enum tree_code code = TREE_CODE (ref);
3901 switch (code)
3903 case VAR_DECL:
3904 case PARM_DECL:
3905 case RESULT_DECL:
3906 /* No action is needed in this case. */
3907 return ref;
3909 CASE_CONVERT:
3910 case FLOAT_EXPR:
3911 case FIX_TRUNC_EXPR:
3912 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
3913 break;
3915 case INDIRECT_REF:
3916 result = build_nt (INDIRECT_REF,
3917 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
3918 break;
3920 case COMPONENT_REF:
3921 result = build_nt (COMPONENT_REF,
3922 stabilize_reference (TREE_OPERAND (ref, 0)),
3923 TREE_OPERAND (ref, 1), NULL_TREE);
3924 break;
3926 case BIT_FIELD_REF:
3927 result = build_nt (BIT_FIELD_REF,
3928 stabilize_reference (TREE_OPERAND (ref, 0)),
3929 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
3930 break;
3932 case ARRAY_REF:
3933 result = build_nt (ARRAY_REF,
3934 stabilize_reference (TREE_OPERAND (ref, 0)),
3935 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3936 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3937 break;
3939 case ARRAY_RANGE_REF:
3940 result = build_nt (ARRAY_RANGE_REF,
3941 stabilize_reference (TREE_OPERAND (ref, 0)),
3942 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3943 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3944 break;
3946 case COMPOUND_EXPR:
3947 /* We cannot wrap the first expression in a SAVE_EXPR, as then
3948 it wouldn't be ignored. This matters when dealing with
3949 volatiles. */
3950 return stabilize_reference_1 (ref);
3952 /* If arg isn't a kind of lvalue we recognize, make no change.
3953 Caller should recognize the error for an invalid lvalue. */
3954 default:
3955 return ref;
3957 case ERROR_MARK:
3958 return error_mark_node;
3961 TREE_TYPE (result) = TREE_TYPE (ref);
3962 TREE_READONLY (result) = TREE_READONLY (ref);
3963 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
3964 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
3966 return result;
3969 /* Low-level constructors for expressions. */
3971 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
3972 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
3974 void
3975 recompute_tree_invariant_for_addr_expr (tree t)
3977 tree node;
3978 bool tc = true, se = false;
3980 /* We started out assuming this address is both invariant and constant, but
3981 does not have side effects. Now go down any handled components and see if
3982 any of them involve offsets that are either non-constant or non-invariant.
3983 Also check for side-effects.
3985 ??? Note that this code makes no attempt to deal with the case where
3986 taking the address of something causes a copy due to misalignment. */
3988 #define UPDATE_FLAGS(NODE) \
3989 do { tree _node = (NODE); \
3990 if (_node && !TREE_CONSTANT (_node)) tc = false; \
3991 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
3993 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
3994 node = TREE_OPERAND (node, 0))
3996 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
3997 array reference (probably made temporarily by the G++ front end),
3998 so ignore all the operands. */
3999 if ((TREE_CODE (node) == ARRAY_REF
4000 || TREE_CODE (node) == ARRAY_RANGE_REF)
4001 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
4003 UPDATE_FLAGS (TREE_OPERAND (node, 1));
4004 if (TREE_OPERAND (node, 2))
4005 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4006 if (TREE_OPERAND (node, 3))
4007 UPDATE_FLAGS (TREE_OPERAND (node, 3));
4009 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
4010 FIELD_DECL, apparently. The G++ front end can put something else
4011 there, at least temporarily. */
4012 else if (TREE_CODE (node) == COMPONENT_REF
4013 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
4015 if (TREE_OPERAND (node, 2))
4016 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4020 node = lang_hooks.expr_to_decl (node, &tc, &se);
4022 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4023 the address, since &(*a)->b is a form of addition. If it's a constant, the
4024 address is constant too. If it's a decl, its address is constant if the
4025 decl is static. Everything else is not constant and, furthermore,
4026 taking the address of a volatile variable is not volatile. */
4027 if (TREE_CODE (node) == INDIRECT_REF
4028 || TREE_CODE (node) == MEM_REF)
4029 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4030 else if (CONSTANT_CLASS_P (node))
4032 else if (DECL_P (node))
4033 tc &= (staticp (node) != NULL_TREE);
4034 else
4036 tc = false;
4037 se |= TREE_SIDE_EFFECTS (node);
4041 TREE_CONSTANT (t) = tc;
4042 TREE_SIDE_EFFECTS (t) = se;
4043 #undef UPDATE_FLAGS
4046 /* Build an expression of code CODE, data type TYPE, and operands as
4047 specified. Expressions and reference nodes can be created this way.
4048 Constants, decls, types and misc nodes cannot be.
4050 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4051 enough for all extant tree codes. */
4053 tree
4054 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
4056 tree t;
4058 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4060 t = make_node_stat (code PASS_MEM_STAT);
4061 TREE_TYPE (t) = tt;
4063 return t;
4066 tree
4067 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4069 int length = sizeof (struct tree_exp);
4070 tree t;
4072 record_node_allocation_statistics (code, length);
4074 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4076 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4078 memset (t, 0, sizeof (struct tree_common));
4080 TREE_SET_CODE (t, code);
4082 TREE_TYPE (t) = type;
4083 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4084 TREE_OPERAND (t, 0) = node;
4085 if (node && !TYPE_P (node))
4087 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4088 TREE_READONLY (t) = TREE_READONLY (node);
4091 if (TREE_CODE_CLASS (code) == tcc_statement)
4092 TREE_SIDE_EFFECTS (t) = 1;
4093 else switch (code)
4095 case VA_ARG_EXPR:
4096 /* All of these have side-effects, no matter what their
4097 operands are. */
4098 TREE_SIDE_EFFECTS (t) = 1;
4099 TREE_READONLY (t) = 0;
4100 break;
4102 case INDIRECT_REF:
4103 /* Whether a dereference is readonly has nothing to do with whether
4104 its operand is readonly. */
4105 TREE_READONLY (t) = 0;
4106 break;
4108 case ADDR_EXPR:
4109 if (node)
4110 recompute_tree_invariant_for_addr_expr (t);
4111 break;
4113 default:
4114 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4115 && node && !TYPE_P (node)
4116 && TREE_CONSTANT (node))
4117 TREE_CONSTANT (t) = 1;
4118 if (TREE_CODE_CLASS (code) == tcc_reference
4119 && node && TREE_THIS_VOLATILE (node))
4120 TREE_THIS_VOLATILE (t) = 1;
4121 break;
4124 return t;
4127 #define PROCESS_ARG(N) \
4128 do { \
4129 TREE_OPERAND (t, N) = arg##N; \
4130 if (arg##N &&!TYPE_P (arg##N)) \
4132 if (TREE_SIDE_EFFECTS (arg##N)) \
4133 side_effects = 1; \
4134 if (!TREE_READONLY (arg##N) \
4135 && !CONSTANT_CLASS_P (arg##N)) \
4136 (void) (read_only = 0); \
4137 if (!TREE_CONSTANT (arg##N)) \
4138 (void) (constant = 0); \
4140 } while (0)
4142 tree
4143 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
4145 bool constant, read_only, side_effects;
4146 tree t;
4148 gcc_assert (TREE_CODE_LENGTH (code) == 2);
4150 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
4151 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
4152 /* When sizetype precision doesn't match that of pointers
4153 we need to be able to build explicit extensions or truncations
4154 of the offset argument. */
4155 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
4156 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
4157 && TREE_CODE (arg1) == INTEGER_CST);
4159 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
4160 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
4161 && ptrofftype_p (TREE_TYPE (arg1)));
4163 t = make_node_stat (code PASS_MEM_STAT);
4164 TREE_TYPE (t) = tt;
4166 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
4167 result based on those same flags for the arguments. But if the
4168 arguments aren't really even `tree' expressions, we shouldn't be trying
4169 to do this. */
4171 /* Expressions without side effects may be constant if their
4172 arguments are as well. */
4173 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4174 || TREE_CODE_CLASS (code) == tcc_binary);
4175 read_only = 1;
4176 side_effects = TREE_SIDE_EFFECTS (t);
4178 PROCESS_ARG (0);
4179 PROCESS_ARG (1);
4181 TREE_READONLY (t) = read_only;
4182 TREE_CONSTANT (t) = constant;
4183 TREE_SIDE_EFFECTS (t) = side_effects;
4184 TREE_THIS_VOLATILE (t)
4185 = (TREE_CODE_CLASS (code) == tcc_reference
4186 && arg0 && TREE_THIS_VOLATILE (arg0));
4188 return t;
4192 tree
4193 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4194 tree arg2 MEM_STAT_DECL)
4196 bool constant, read_only, side_effects;
4197 tree t;
4199 gcc_assert (TREE_CODE_LENGTH (code) == 3);
4200 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4202 t = make_node_stat (code PASS_MEM_STAT);
4203 TREE_TYPE (t) = tt;
4205 read_only = 1;
4207 /* As a special exception, if COND_EXPR has NULL branches, we
4208 assume that it is a gimple statement and always consider
4209 it to have side effects. */
4210 if (code == COND_EXPR
4211 && tt == void_type_node
4212 && arg1 == NULL_TREE
4213 && arg2 == NULL_TREE)
4214 side_effects = true;
4215 else
4216 side_effects = TREE_SIDE_EFFECTS (t);
4218 PROCESS_ARG (0);
4219 PROCESS_ARG (1);
4220 PROCESS_ARG (2);
4222 if (code == COND_EXPR)
4223 TREE_READONLY (t) = read_only;
4225 TREE_SIDE_EFFECTS (t) = side_effects;
4226 TREE_THIS_VOLATILE (t)
4227 = (TREE_CODE_CLASS (code) == tcc_reference
4228 && arg0 && TREE_THIS_VOLATILE (arg0));
4230 return t;
4233 tree
4234 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4235 tree arg2, tree arg3 MEM_STAT_DECL)
4237 bool constant, read_only, side_effects;
4238 tree t;
4240 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4242 t = make_node_stat (code PASS_MEM_STAT);
4243 TREE_TYPE (t) = tt;
4245 side_effects = TREE_SIDE_EFFECTS (t);
4247 PROCESS_ARG (0);
4248 PROCESS_ARG (1);
4249 PROCESS_ARG (2);
4250 PROCESS_ARG (3);
4252 TREE_SIDE_EFFECTS (t) = side_effects;
4253 TREE_THIS_VOLATILE (t)
4254 = (TREE_CODE_CLASS (code) == tcc_reference
4255 && arg0 && TREE_THIS_VOLATILE (arg0));
4257 return t;
4260 tree
4261 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4262 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4264 bool constant, read_only, side_effects;
4265 tree t;
4267 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4269 t = make_node_stat (code PASS_MEM_STAT);
4270 TREE_TYPE (t) = tt;
4272 side_effects = TREE_SIDE_EFFECTS (t);
4274 PROCESS_ARG (0);
4275 PROCESS_ARG (1);
4276 PROCESS_ARG (2);
4277 PROCESS_ARG (3);
4278 PROCESS_ARG (4);
4280 TREE_SIDE_EFFECTS (t) = side_effects;
4281 TREE_THIS_VOLATILE (t)
4282 = (TREE_CODE_CLASS (code) == tcc_reference
4283 && arg0 && TREE_THIS_VOLATILE (arg0));
4285 return t;
4288 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4289 on the pointer PTR. */
4291 tree
4292 build_simple_mem_ref_loc (location_t loc, tree ptr)
4294 HOST_WIDE_INT offset = 0;
4295 tree ptype = TREE_TYPE (ptr);
4296 tree tem;
4297 /* For convenience allow addresses that collapse to a simple base
4298 and offset. */
4299 if (TREE_CODE (ptr) == ADDR_EXPR
4300 && (handled_component_p (TREE_OPERAND (ptr, 0))
4301 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4303 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4304 gcc_assert (ptr);
4305 ptr = build_fold_addr_expr (ptr);
4306 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4308 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4309 ptr, build_int_cst (ptype, offset));
4310 SET_EXPR_LOCATION (tem, loc);
4311 return tem;
4314 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4316 double_int
4317 mem_ref_offset (const_tree t)
4319 tree toff = TREE_OPERAND (t, 1);
4320 return tree_to_double_int (toff).sext (TYPE_PRECISION (TREE_TYPE (toff)));
4323 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4324 offsetted by OFFSET units. */
4326 tree
4327 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4329 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4330 build_fold_addr_expr (base),
4331 build_int_cst (ptr_type_node, offset));
4332 tree addr = build1 (ADDR_EXPR, type, ref);
4333 recompute_tree_invariant_for_addr_expr (addr);
4334 return addr;
4337 /* Similar except don't specify the TREE_TYPE
4338 and leave the TREE_SIDE_EFFECTS as 0.
4339 It is permissible for arguments to be null,
4340 or even garbage if their values do not matter. */
4342 tree
4343 build_nt (enum tree_code code, ...)
4345 tree t;
4346 int length;
4347 int i;
4348 va_list p;
4350 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4352 va_start (p, code);
4354 t = make_node (code);
4355 length = TREE_CODE_LENGTH (code);
4357 for (i = 0; i < length; i++)
4358 TREE_OPERAND (t, i) = va_arg (p, tree);
4360 va_end (p);
4361 return t;
4364 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4365 tree vec. */
4367 tree
4368 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4370 tree ret, t;
4371 unsigned int ix;
4373 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4374 CALL_EXPR_FN (ret) = fn;
4375 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4376 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4377 CALL_EXPR_ARG (ret, ix) = t;
4378 return ret;
4381 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4382 We do NOT enter this node in any sort of symbol table.
4384 LOC is the location of the decl.
4386 layout_decl is used to set up the decl's storage layout.
4387 Other slots are initialized to 0 or null pointers. */
4389 tree
4390 build_decl_stat (location_t loc, enum tree_code code, tree name,
4391 tree type MEM_STAT_DECL)
4393 tree t;
4395 t = make_node_stat (code PASS_MEM_STAT);
4396 DECL_SOURCE_LOCATION (t) = loc;
4398 /* if (type == error_mark_node)
4399 type = integer_type_node; */
4400 /* That is not done, deliberately, so that having error_mark_node
4401 as the type can suppress useless errors in the use of this variable. */
4403 DECL_NAME (t) = name;
4404 TREE_TYPE (t) = type;
4406 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4407 layout_decl (t, 0);
4409 return t;
4412 /* Builds and returns function declaration with NAME and TYPE. */
4414 tree
4415 build_fn_decl (const char *name, tree type)
4417 tree id = get_identifier (name);
4418 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4420 DECL_EXTERNAL (decl) = 1;
4421 TREE_PUBLIC (decl) = 1;
4422 DECL_ARTIFICIAL (decl) = 1;
4423 TREE_NOTHROW (decl) = 1;
4425 return decl;
4428 vec<tree, va_gc> *all_translation_units;
4430 /* Builds a new translation-unit decl with name NAME, queues it in the
4431 global list of translation-unit decls and returns it. */
4433 tree
4434 build_translation_unit_decl (tree name)
4436 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4437 name, NULL_TREE);
4438 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4439 vec_safe_push (all_translation_units, tu);
4440 return tu;
4444 /* BLOCK nodes are used to represent the structure of binding contours
4445 and declarations, once those contours have been exited and their contents
4446 compiled. This information is used for outputting debugging info. */
4448 tree
4449 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4451 tree block = make_node (BLOCK);
4453 BLOCK_VARS (block) = vars;
4454 BLOCK_SUBBLOCKS (block) = subblocks;
4455 BLOCK_SUPERCONTEXT (block) = supercontext;
4456 BLOCK_CHAIN (block) = chain;
4457 return block;
4461 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4463 LOC is the location to use in tree T. */
4465 void
4466 protected_set_expr_location (tree t, location_t loc)
4468 if (t && CAN_HAVE_LOCATION_P (t))
4469 SET_EXPR_LOCATION (t, loc);
4472 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4473 is ATTRIBUTE. */
4475 tree
4476 build_decl_attribute_variant (tree ddecl, tree attribute)
4478 DECL_ATTRIBUTES (ddecl) = attribute;
4479 return ddecl;
4482 /* Borrowed from hashtab.c iterative_hash implementation. */
4483 #define mix(a,b,c) \
4485 a -= b; a -= c; a ^= (c>>13); \
4486 b -= c; b -= a; b ^= (a<< 8); \
4487 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
4488 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
4489 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
4490 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
4491 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
4492 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
4493 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
4497 /* Produce good hash value combining VAL and VAL2. */
4498 hashval_t
4499 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
4501 /* the golden ratio; an arbitrary value. */
4502 hashval_t a = 0x9e3779b9;
4504 mix (a, val, val2);
4505 return val2;
4508 /* Produce good hash value combining VAL and VAL2. */
4509 hashval_t
4510 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
4512 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
4513 return iterative_hash_hashval_t (val, val2);
4514 else
4516 hashval_t a = (hashval_t) val;
4517 /* Avoid warnings about shifting of more than the width of the type on
4518 hosts that won't execute this path. */
4519 int zero = 0;
4520 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
4521 mix (a, b, val2);
4522 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
4524 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
4525 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
4526 mix (a, b, val2);
4528 return val2;
4532 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4533 is ATTRIBUTE and its qualifiers are QUALS.
4535 Record such modified types already made so we don't make duplicates. */
4537 tree
4538 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4540 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4542 hashval_t hashcode = 0;
4543 tree ntype;
4544 enum tree_code code = TREE_CODE (ttype);
4546 /* Building a distinct copy of a tagged type is inappropriate; it
4547 causes breakage in code that expects there to be a one-to-one
4548 relationship between a struct and its fields.
4549 build_duplicate_type is another solution (as used in
4550 handle_transparent_union_attribute), but that doesn't play well
4551 with the stronger C++ type identity model. */
4552 if (TREE_CODE (ttype) == RECORD_TYPE
4553 || TREE_CODE (ttype) == UNION_TYPE
4554 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4555 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4557 warning (OPT_Wattributes,
4558 "ignoring attributes applied to %qT after definition",
4559 TYPE_MAIN_VARIANT (ttype));
4560 return build_qualified_type (ttype, quals);
4563 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4564 ntype = build_distinct_type_copy (ttype);
4566 TYPE_ATTRIBUTES (ntype) = attribute;
4568 hashcode = iterative_hash_object (code, hashcode);
4569 if (TREE_TYPE (ntype))
4570 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
4571 hashcode);
4572 hashcode = attribute_hash_list (attribute, hashcode);
4574 switch (TREE_CODE (ntype))
4576 case FUNCTION_TYPE:
4577 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
4578 break;
4579 case ARRAY_TYPE:
4580 if (TYPE_DOMAIN (ntype))
4581 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
4582 hashcode);
4583 break;
4584 case INTEGER_TYPE:
4585 hashcode = iterative_hash_object
4586 (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
4587 hashcode = iterative_hash_object
4588 (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
4589 break;
4590 case REAL_TYPE:
4591 case FIXED_POINT_TYPE:
4593 unsigned int precision = TYPE_PRECISION (ntype);
4594 hashcode = iterative_hash_object (precision, hashcode);
4596 break;
4597 default:
4598 break;
4601 ntype = type_hash_canon (hashcode, ntype);
4603 /* If the target-dependent attributes make NTYPE different from
4604 its canonical type, we will need to use structural equality
4605 checks for this type. */
4606 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4607 || !comp_type_attributes (ntype, ttype))
4608 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4609 else if (TYPE_CANONICAL (ntype) == ntype)
4610 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4612 ttype = build_qualified_type (ntype, quals);
4614 else if (TYPE_QUALS (ttype) != quals)
4615 ttype = build_qualified_type (ttype, quals);
4617 return ttype;
4620 /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
4621 the same. */
4623 static bool
4624 omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
4626 tree cl1, cl2;
4627 for (cl1 = clauses1, cl2 = clauses2;
4628 cl1 && cl2;
4629 cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
4631 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
4632 return false;
4633 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
4635 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
4636 OMP_CLAUSE_DECL (cl2)) != 1)
4637 return false;
4639 switch (OMP_CLAUSE_CODE (cl1))
4641 case OMP_CLAUSE_ALIGNED:
4642 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
4643 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
4644 return false;
4645 break;
4646 case OMP_CLAUSE_LINEAR:
4647 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
4648 OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
4649 return false;
4650 break;
4651 case OMP_CLAUSE_SIMDLEN:
4652 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
4653 OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
4654 return false;
4655 default:
4656 break;
4659 return true;
4662 /* Compare two constructor-element-type constants. Return 1 if the lists
4663 are known to be equal; otherwise return 0. */
4665 static bool
4666 simple_cst_list_equal (const_tree l1, const_tree l2)
4668 while (l1 != NULL_TREE && l2 != NULL_TREE)
4670 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
4671 return false;
4673 l1 = TREE_CHAIN (l1);
4674 l2 = TREE_CHAIN (l2);
4677 return l1 == l2;
4680 /* Compare two attributes for their value identity. Return true if the
4681 attribute values are known to be equal; otherwise return false.
4684 static bool
4685 attribute_value_equal (const_tree attr1, const_tree attr2)
4687 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4688 return true;
4690 if (TREE_VALUE (attr1) != NULL_TREE
4691 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4692 && TREE_VALUE (attr2) != NULL
4693 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4694 return (simple_cst_list_equal (TREE_VALUE (attr1),
4695 TREE_VALUE (attr2)) == 1);
4697 if ((flag_openmp || flag_openmp_simd)
4698 && TREE_VALUE (attr1) && TREE_VALUE (attr2)
4699 && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
4700 && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
4701 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
4702 TREE_VALUE (attr2));
4704 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4707 /* Return 0 if the attributes for two types are incompatible, 1 if they
4708 are compatible, and 2 if they are nearly compatible (which causes a
4709 warning to be generated). */
4711 comp_type_attributes (const_tree type1, const_tree type2)
4713 const_tree a1 = TYPE_ATTRIBUTES (type1);
4714 const_tree a2 = TYPE_ATTRIBUTES (type2);
4715 const_tree a;
4717 if (a1 == a2)
4718 return 1;
4719 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4721 const struct attribute_spec *as;
4722 const_tree attr;
4724 as = lookup_attribute_spec (get_attribute_name (a));
4725 if (!as || as->affects_type_identity == false)
4726 continue;
4728 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4729 if (!attr || !attribute_value_equal (a, attr))
4730 break;
4732 if (!a)
4734 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
4736 const struct attribute_spec *as;
4738 as = lookup_attribute_spec (get_attribute_name (a));
4739 if (!as || as->affects_type_identity == false)
4740 continue;
4742 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
4743 break;
4744 /* We don't need to compare trees again, as we did this
4745 already in first loop. */
4747 /* All types - affecting identity - are equal, so
4748 there is no need to call target hook for comparison. */
4749 if (!a)
4750 return 1;
4752 /* As some type combinations - like default calling-convention - might
4753 be compatible, we have to call the target hook to get the final result. */
4754 return targetm.comp_type_attributes (type1, type2);
4757 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4758 is ATTRIBUTE.
4760 Record such modified types already made so we don't make duplicates. */
4762 tree
4763 build_type_attribute_variant (tree ttype, tree attribute)
4765 return build_type_attribute_qual_variant (ttype, attribute,
4766 TYPE_QUALS (ttype));
4770 /* Reset the expression *EXPR_P, a size or position.
4772 ??? We could reset all non-constant sizes or positions. But it's cheap
4773 enough to not do so and refrain from adding workarounds to dwarf2out.c.
4775 We need to reset self-referential sizes or positions because they cannot
4776 be gimplified and thus can contain a CALL_EXPR after the gimplification
4777 is finished, which will run afoul of LTO streaming. And they need to be
4778 reset to something essentially dummy but not constant, so as to preserve
4779 the properties of the object they are attached to. */
4781 static inline void
4782 free_lang_data_in_one_sizepos (tree *expr_p)
4784 tree expr = *expr_p;
4785 if (CONTAINS_PLACEHOLDER_P (expr))
4786 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
4790 /* Reset all the fields in a binfo node BINFO. We only keep
4791 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
4793 static void
4794 free_lang_data_in_binfo (tree binfo)
4796 unsigned i;
4797 tree t;
4799 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
4801 BINFO_VIRTUALS (binfo) = NULL_TREE;
4802 BINFO_BASE_ACCESSES (binfo) = NULL;
4803 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
4804 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
4806 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
4807 free_lang_data_in_binfo (t);
4811 /* Reset all language specific information still present in TYPE. */
4813 static void
4814 free_lang_data_in_type (tree type)
4816 gcc_assert (TYPE_P (type));
4818 /* Give the FE a chance to remove its own data first. */
4819 lang_hooks.free_lang_data (type);
4821 TREE_LANG_FLAG_0 (type) = 0;
4822 TREE_LANG_FLAG_1 (type) = 0;
4823 TREE_LANG_FLAG_2 (type) = 0;
4824 TREE_LANG_FLAG_3 (type) = 0;
4825 TREE_LANG_FLAG_4 (type) = 0;
4826 TREE_LANG_FLAG_5 (type) = 0;
4827 TREE_LANG_FLAG_6 (type) = 0;
4829 if (TREE_CODE (type) == FUNCTION_TYPE)
4831 /* Remove the const and volatile qualifiers from arguments. The
4832 C++ front end removes them, but the C front end does not,
4833 leading to false ODR violation errors when merging two
4834 instances of the same function signature compiled by
4835 different front ends. */
4836 tree p;
4838 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
4840 tree arg_type = TREE_VALUE (p);
4842 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
4844 int quals = TYPE_QUALS (arg_type)
4845 & ~TYPE_QUAL_CONST
4846 & ~TYPE_QUAL_VOLATILE;
4847 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
4848 free_lang_data_in_type (TREE_VALUE (p));
4853 /* Remove members that are not actually FIELD_DECLs from the field
4854 list of an aggregate. These occur in C++. */
4855 if (RECORD_OR_UNION_TYPE_P (type))
4857 tree prev, member;
4859 /* Note that TYPE_FIELDS can be shared across distinct
4860 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
4861 to be removed, we cannot set its TREE_CHAIN to NULL.
4862 Otherwise, we would not be able to find all the other fields
4863 in the other instances of this TREE_TYPE.
4865 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
4866 prev = NULL_TREE;
4867 member = TYPE_FIELDS (type);
4868 while (member)
4870 if (TREE_CODE (member) == FIELD_DECL
4871 || TREE_CODE (member) == TYPE_DECL)
4873 if (prev)
4874 TREE_CHAIN (prev) = member;
4875 else
4876 TYPE_FIELDS (type) = member;
4877 prev = member;
4880 member = TREE_CHAIN (member);
4883 if (prev)
4884 TREE_CHAIN (prev) = NULL_TREE;
4885 else
4886 TYPE_FIELDS (type) = NULL_TREE;
4888 TYPE_METHODS (type) = NULL_TREE;
4889 if (TYPE_BINFO (type))
4890 free_lang_data_in_binfo (TYPE_BINFO (type));
4892 else
4894 /* For non-aggregate types, clear out the language slot (which
4895 overloads TYPE_BINFO). */
4896 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
4898 if (INTEGRAL_TYPE_P (type)
4899 || SCALAR_FLOAT_TYPE_P (type)
4900 || FIXED_POINT_TYPE_P (type))
4902 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
4903 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
4907 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
4908 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
4910 if (TYPE_CONTEXT (type)
4911 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
4913 tree ctx = TYPE_CONTEXT (type);
4916 ctx = BLOCK_SUPERCONTEXT (ctx);
4918 while (ctx && TREE_CODE (ctx) == BLOCK);
4919 TYPE_CONTEXT (type) = ctx;
4924 /* Return true if DECL may need an assembler name to be set. */
4926 static inline bool
4927 need_assembler_name_p (tree decl)
4929 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
4930 if (TREE_CODE (decl) != FUNCTION_DECL
4931 && TREE_CODE (decl) != VAR_DECL)
4932 return false;
4934 /* If DECL already has its assembler name set, it does not need a
4935 new one. */
4936 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
4937 || DECL_ASSEMBLER_NAME_SET_P (decl))
4938 return false;
4940 /* Abstract decls do not need an assembler name. */
4941 if (DECL_ABSTRACT (decl))
4942 return false;
4944 /* For VAR_DECLs, only static, public and external symbols need an
4945 assembler name. */
4946 if (TREE_CODE (decl) == VAR_DECL
4947 && !TREE_STATIC (decl)
4948 && !TREE_PUBLIC (decl)
4949 && !DECL_EXTERNAL (decl))
4950 return false;
4952 if (TREE_CODE (decl) == FUNCTION_DECL)
4954 /* Do not set assembler name on builtins. Allow RTL expansion to
4955 decide whether to expand inline or via a regular call. */
4956 if (DECL_BUILT_IN (decl)
4957 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
4958 return false;
4960 /* Functions represented in the callgraph need an assembler name. */
4961 if (cgraph_get_node (decl) != NULL)
4962 return true;
4964 /* Unused and not public functions don't need an assembler name. */
4965 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
4966 return false;
4969 return true;
4973 /* Reset all language specific information still present in symbol
4974 DECL. */
4976 static void
4977 free_lang_data_in_decl (tree decl)
4979 gcc_assert (DECL_P (decl));
4981 /* Give the FE a chance to remove its own data first. */
4982 lang_hooks.free_lang_data (decl);
4984 TREE_LANG_FLAG_0 (decl) = 0;
4985 TREE_LANG_FLAG_1 (decl) = 0;
4986 TREE_LANG_FLAG_2 (decl) = 0;
4987 TREE_LANG_FLAG_3 (decl) = 0;
4988 TREE_LANG_FLAG_4 (decl) = 0;
4989 TREE_LANG_FLAG_5 (decl) = 0;
4990 TREE_LANG_FLAG_6 (decl) = 0;
4992 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
4993 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
4994 if (TREE_CODE (decl) == FIELD_DECL)
4996 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
4997 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
4998 DECL_QUALIFIER (decl) = NULL_TREE;
5001 if (TREE_CODE (decl) == FUNCTION_DECL)
5003 struct cgraph_node *node;
5004 if (!(node = cgraph_get_node (decl))
5005 || (!node->definition && !node->clones))
5007 if (node)
5008 cgraph_release_function_body (node);
5009 else
5011 release_function_body (decl);
5012 DECL_ARGUMENTS (decl) = NULL;
5013 DECL_RESULT (decl) = NULL;
5014 DECL_INITIAL (decl) = error_mark_node;
5017 if (gimple_has_body_p (decl))
5019 tree t;
5021 /* If DECL has a gimple body, then the context for its
5022 arguments must be DECL. Otherwise, it doesn't really
5023 matter, as we will not be emitting any code for DECL. In
5024 general, there may be other instances of DECL created by
5025 the front end and since PARM_DECLs are generally shared,
5026 their DECL_CONTEXT changes as the replicas of DECL are
5027 created. The only time where DECL_CONTEXT is important
5028 is for the FUNCTION_DECLs that have a gimple body (since
5029 the PARM_DECL will be used in the function's body). */
5030 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
5031 DECL_CONTEXT (t) = decl;
5034 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
5035 At this point, it is not needed anymore. */
5036 DECL_SAVED_TREE (decl) = NULL_TREE;
5038 /* Clear the abstract origin if it refers to a method. Otherwise
5039 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
5040 origin will not be output correctly. */
5041 if (DECL_ABSTRACT_ORIGIN (decl)
5042 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
5043 && RECORD_OR_UNION_TYPE_P
5044 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
5045 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
5047 /* Sometimes the C++ frontend doesn't manage to transform a temporary
5048 DECL_VINDEX referring to itself into a vtable slot number as it
5049 should. Happens with functions that are copied and then forgotten
5050 about. Just clear it, it won't matter anymore. */
5051 if (DECL_VINDEX (decl) && !tree_fits_shwi_p (DECL_VINDEX (decl)))
5052 DECL_VINDEX (decl) = NULL_TREE;
5054 else if (TREE_CODE (decl) == VAR_DECL)
5056 if ((DECL_EXTERNAL (decl)
5057 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
5058 || (decl_function_context (decl) && !TREE_STATIC (decl)))
5059 DECL_INITIAL (decl) = NULL_TREE;
5061 else if (TREE_CODE (decl) == TYPE_DECL
5062 || TREE_CODE (decl) == FIELD_DECL)
5063 DECL_INITIAL (decl) = NULL_TREE;
5064 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
5065 && DECL_INITIAL (decl)
5066 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
5068 /* Strip builtins from the translation-unit BLOCK. We still have targets
5069 without builtin_decl_explicit support and also builtins are shared
5070 nodes and thus we can't use TREE_CHAIN in multiple lists. */
5071 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
5072 while (*nextp)
5074 tree var = *nextp;
5075 if (TREE_CODE (var) == FUNCTION_DECL
5076 && DECL_BUILT_IN (var))
5077 *nextp = TREE_CHAIN (var);
5078 else
5079 nextp = &TREE_CHAIN (var);
5085 /* Data used when collecting DECLs and TYPEs for language data removal. */
5087 struct free_lang_data_d
5089 /* Worklist to avoid excessive recursion. */
5090 vec<tree> worklist;
5092 /* Set of traversed objects. Used to avoid duplicate visits. */
5093 struct pointer_set_t *pset;
5095 /* Array of symbols to process with free_lang_data_in_decl. */
5096 vec<tree> decls;
5098 /* Array of types to process with free_lang_data_in_type. */
5099 vec<tree> types;
5103 /* Save all language fields needed to generate proper debug information
5104 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
5106 static void
5107 save_debug_info_for_decl (tree t)
5109 /*struct saved_debug_info_d *sdi;*/
5111 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
5113 /* FIXME. Partial implementation for saving debug info removed. */
5117 /* Save all language fields needed to generate proper debug information
5118 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
5120 static void
5121 save_debug_info_for_type (tree t)
5123 /*struct saved_debug_info_d *sdi;*/
5125 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
5127 /* FIXME. Partial implementation for saving debug info removed. */
5131 /* Add type or decl T to one of the list of tree nodes that need their
5132 language data removed. The lists are held inside FLD. */
5134 static void
5135 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
5137 if (DECL_P (t))
5139 fld->decls.safe_push (t);
5140 if (debug_info_level > DINFO_LEVEL_TERSE)
5141 save_debug_info_for_decl (t);
5143 else if (TYPE_P (t))
5145 fld->types.safe_push (t);
5146 if (debug_info_level > DINFO_LEVEL_TERSE)
5147 save_debug_info_for_type (t);
5149 else
5150 gcc_unreachable ();
5153 /* Push tree node T into FLD->WORKLIST. */
5155 static inline void
5156 fld_worklist_push (tree t, struct free_lang_data_d *fld)
5158 if (t && !is_lang_specific (t) && !pointer_set_contains (fld->pset, t))
5159 fld->worklist.safe_push ((t));
5163 /* Operand callback helper for free_lang_data_in_node. *TP is the
5164 subtree operand being considered. */
5166 static tree
5167 find_decls_types_r (tree *tp, int *ws, void *data)
5169 tree t = *tp;
5170 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
5172 if (TREE_CODE (t) == TREE_LIST)
5173 return NULL_TREE;
5175 /* Language specific nodes will be removed, so there is no need
5176 to gather anything under them. */
5177 if (is_lang_specific (t))
5179 *ws = 0;
5180 return NULL_TREE;
5183 if (DECL_P (t))
5185 /* Note that walk_tree does not traverse every possible field in
5186 decls, so we have to do our own traversals here. */
5187 add_tree_to_fld_list (t, fld);
5189 fld_worklist_push (DECL_NAME (t), fld);
5190 fld_worklist_push (DECL_CONTEXT (t), fld);
5191 fld_worklist_push (DECL_SIZE (t), fld);
5192 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
5194 /* We are going to remove everything under DECL_INITIAL for
5195 TYPE_DECLs. No point walking them. */
5196 if (TREE_CODE (t) != TYPE_DECL)
5197 fld_worklist_push (DECL_INITIAL (t), fld);
5199 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
5200 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
5202 if (TREE_CODE (t) == FUNCTION_DECL)
5204 fld_worklist_push (DECL_ARGUMENTS (t), fld);
5205 fld_worklist_push (DECL_RESULT (t), fld);
5207 else if (TREE_CODE (t) == TYPE_DECL)
5209 fld_worklist_push (DECL_ARGUMENT_FLD (t), fld);
5210 fld_worklist_push (DECL_VINDEX (t), fld);
5211 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
5213 else if (TREE_CODE (t) == FIELD_DECL)
5215 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
5216 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
5217 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
5218 fld_worklist_push (DECL_FCONTEXT (t), fld);
5220 else if (TREE_CODE (t) == VAR_DECL)
5222 fld_worklist_push (DECL_SECTION_NAME (t), fld);
5223 fld_worklist_push (DECL_COMDAT_GROUP (t), fld);
5226 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
5227 && DECL_HAS_VALUE_EXPR_P (t))
5228 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
5230 if (TREE_CODE (t) != FIELD_DECL
5231 && TREE_CODE (t) != TYPE_DECL)
5232 fld_worklist_push (TREE_CHAIN (t), fld);
5233 *ws = 0;
5235 else if (TYPE_P (t))
5237 /* Note that walk_tree does not traverse every possible field in
5238 types, so we have to do our own traversals here. */
5239 add_tree_to_fld_list (t, fld);
5241 if (!RECORD_OR_UNION_TYPE_P (t))
5242 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
5243 fld_worklist_push (TYPE_SIZE (t), fld);
5244 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
5245 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
5246 fld_worklist_push (TYPE_POINTER_TO (t), fld);
5247 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
5248 fld_worklist_push (TYPE_NAME (t), fld);
5249 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
5250 them and thus do not and want not to reach unused pointer types
5251 this way. */
5252 if (!POINTER_TYPE_P (t))
5253 fld_worklist_push (TYPE_MINVAL (t), fld);
5254 if (!RECORD_OR_UNION_TYPE_P (t))
5255 fld_worklist_push (TYPE_MAXVAL (t), fld);
5256 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
5257 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
5258 do not and want not to reach unused variants this way. */
5259 if (TYPE_CONTEXT (t))
5261 tree ctx = TYPE_CONTEXT (t);
5262 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
5263 So push that instead. */
5264 while (ctx && TREE_CODE (ctx) == BLOCK)
5265 ctx = BLOCK_SUPERCONTEXT (ctx);
5266 fld_worklist_push (ctx, fld);
5268 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
5269 and want not to reach unused types this way. */
5271 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
5273 unsigned i;
5274 tree tem;
5275 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
5276 fld_worklist_push (TREE_TYPE (tem), fld);
5277 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
5278 if (tem
5279 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
5280 && TREE_CODE (tem) == TREE_LIST)
5283 fld_worklist_push (TREE_VALUE (tem), fld);
5284 tem = TREE_CHAIN (tem);
5286 while (tem);
5288 if (RECORD_OR_UNION_TYPE_P (t))
5290 tree tem;
5291 /* Push all TYPE_FIELDS - there can be interleaving interesting
5292 and non-interesting things. */
5293 tem = TYPE_FIELDS (t);
5294 while (tem)
5296 if (TREE_CODE (tem) == FIELD_DECL
5297 || TREE_CODE (tem) == TYPE_DECL)
5298 fld_worklist_push (tem, fld);
5299 tem = TREE_CHAIN (tem);
5303 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5304 *ws = 0;
5306 else if (TREE_CODE (t) == BLOCK)
5308 tree tem;
5309 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5310 fld_worklist_push (tem, fld);
5311 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5312 fld_worklist_push (tem, fld);
5313 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5316 if (TREE_CODE (t) != IDENTIFIER_NODE
5317 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5318 fld_worklist_push (TREE_TYPE (t), fld);
5320 return NULL_TREE;
5324 /* Find decls and types in T. */
5326 static void
5327 find_decls_types (tree t, struct free_lang_data_d *fld)
5329 while (1)
5331 if (!pointer_set_contains (fld->pset, t))
5332 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5333 if (fld->worklist.is_empty ())
5334 break;
5335 t = fld->worklist.pop ();
5339 /* Translate all the types in LIST with the corresponding runtime
5340 types. */
5342 static tree
5343 get_eh_types_for_runtime (tree list)
5345 tree head, prev;
5347 if (list == NULL_TREE)
5348 return NULL_TREE;
5350 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5351 prev = head;
5352 list = TREE_CHAIN (list);
5353 while (list)
5355 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5356 TREE_CHAIN (prev) = n;
5357 prev = TREE_CHAIN (prev);
5358 list = TREE_CHAIN (list);
5361 return head;
5365 /* Find decls and types referenced in EH region R and store them in
5366 FLD->DECLS and FLD->TYPES. */
5368 static void
5369 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5371 switch (r->type)
5373 case ERT_CLEANUP:
5374 break;
5376 case ERT_TRY:
5378 eh_catch c;
5380 /* The types referenced in each catch must first be changed to the
5381 EH types used at runtime. This removes references to FE types
5382 in the region. */
5383 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5385 c->type_list = get_eh_types_for_runtime (c->type_list);
5386 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5389 break;
5391 case ERT_ALLOWED_EXCEPTIONS:
5392 r->u.allowed.type_list
5393 = get_eh_types_for_runtime (r->u.allowed.type_list);
5394 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5395 break;
5397 case ERT_MUST_NOT_THROW:
5398 walk_tree (&r->u.must_not_throw.failure_decl,
5399 find_decls_types_r, fld, fld->pset);
5400 break;
5405 /* Find decls and types referenced in cgraph node N and store them in
5406 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5407 look for *every* kind of DECL and TYPE node reachable from N,
5408 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5409 NAMESPACE_DECLs, etc). */
5411 static void
5412 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5414 basic_block bb;
5415 struct function *fn;
5416 unsigned ix;
5417 tree t;
5419 find_decls_types (n->decl, fld);
5421 if (!gimple_has_body_p (n->decl))
5422 return;
5424 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5426 fn = DECL_STRUCT_FUNCTION (n->decl);
5428 /* Traverse locals. */
5429 FOR_EACH_LOCAL_DECL (fn, ix, t)
5430 find_decls_types (t, fld);
5432 /* Traverse EH regions in FN. */
5434 eh_region r;
5435 FOR_ALL_EH_REGION_FN (r, fn)
5436 find_decls_types_in_eh_region (r, fld);
5439 /* Traverse every statement in FN. */
5440 FOR_EACH_BB_FN (bb, fn)
5442 gimple_stmt_iterator si;
5443 unsigned i;
5445 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5447 gimple phi = gsi_stmt (si);
5449 for (i = 0; i < gimple_phi_num_args (phi); i++)
5451 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5452 find_decls_types (*arg_p, fld);
5456 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5458 gimple stmt = gsi_stmt (si);
5460 if (is_gimple_call (stmt))
5461 find_decls_types (gimple_call_fntype (stmt), fld);
5463 for (i = 0; i < gimple_num_ops (stmt); i++)
5465 tree arg = gimple_op (stmt, i);
5466 find_decls_types (arg, fld);
5473 /* Find decls and types referenced in varpool node N and store them in
5474 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5475 look for *every* kind of DECL and TYPE node reachable from N,
5476 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5477 NAMESPACE_DECLs, etc). */
5479 static void
5480 find_decls_types_in_var (varpool_node *v, struct free_lang_data_d *fld)
5482 find_decls_types (v->decl, fld);
5485 /* If T needs an assembler name, have one created for it. */
5487 void
5488 assign_assembler_name_if_neeeded (tree t)
5490 if (need_assembler_name_p (t))
5492 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5493 diagnostics that use input_location to show locus
5494 information. The problem here is that, at this point,
5495 input_location is generally anchored to the end of the file
5496 (since the parser is long gone), so we don't have a good
5497 position to pin it to.
5499 To alleviate this problem, this uses the location of T's
5500 declaration. Examples of this are
5501 testsuite/g++.dg/template/cond2.C and
5502 testsuite/g++.dg/template/pr35240.C. */
5503 location_t saved_location = input_location;
5504 input_location = DECL_SOURCE_LOCATION (t);
5506 decl_assembler_name (t);
5508 input_location = saved_location;
5513 /* Free language specific information for every operand and expression
5514 in every node of the call graph. This process operates in three stages:
5516 1- Every callgraph node and varpool node is traversed looking for
5517 decls and types embedded in them. This is a more exhaustive
5518 search than that done by find_referenced_vars, because it will
5519 also collect individual fields, decls embedded in types, etc.
5521 2- All the decls found are sent to free_lang_data_in_decl.
5523 3- All the types found are sent to free_lang_data_in_type.
5525 The ordering between decls and types is important because
5526 free_lang_data_in_decl sets assembler names, which includes
5527 mangling. So types cannot be freed up until assembler names have
5528 been set up. */
5530 static void
5531 free_lang_data_in_cgraph (void)
5533 struct cgraph_node *n;
5534 varpool_node *v;
5535 struct free_lang_data_d fld;
5536 tree t;
5537 unsigned i;
5538 alias_pair *p;
5540 /* Initialize sets and arrays to store referenced decls and types. */
5541 fld.pset = pointer_set_create ();
5542 fld.worklist.create (0);
5543 fld.decls.create (100);
5544 fld.types.create (100);
5546 /* Find decls and types in the body of every function in the callgraph. */
5547 FOR_EACH_FUNCTION (n)
5548 find_decls_types_in_node (n, &fld);
5550 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5551 find_decls_types (p->decl, &fld);
5553 /* Find decls and types in every varpool symbol. */
5554 FOR_EACH_VARIABLE (v)
5555 find_decls_types_in_var (v, &fld);
5557 /* Set the assembler name on every decl found. We need to do this
5558 now because free_lang_data_in_decl will invalidate data needed
5559 for mangling. This breaks mangling on interdependent decls. */
5560 FOR_EACH_VEC_ELT (fld.decls, i, t)
5561 assign_assembler_name_if_neeeded (t);
5563 /* Traverse every decl found freeing its language data. */
5564 FOR_EACH_VEC_ELT (fld.decls, i, t)
5565 free_lang_data_in_decl (t);
5567 /* Traverse every type found freeing its language data. */
5568 FOR_EACH_VEC_ELT (fld.types, i, t)
5569 free_lang_data_in_type (t);
5571 pointer_set_destroy (fld.pset);
5572 fld.worklist.release ();
5573 fld.decls.release ();
5574 fld.types.release ();
5578 /* Free resources that are used by FE but are not needed once they are done. */
5580 static unsigned
5581 free_lang_data (void)
5583 unsigned i;
5585 /* If we are the LTO frontend we have freed lang-specific data already. */
5586 if (in_lto_p
5587 || !flag_generate_lto)
5588 return 0;
5590 /* Allocate and assign alias sets to the standard integer types
5591 while the slots are still in the way the frontends generated them. */
5592 for (i = 0; i < itk_none; ++i)
5593 if (integer_types[i])
5594 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5596 /* Traverse the IL resetting language specific information for
5597 operands, expressions, etc. */
5598 free_lang_data_in_cgraph ();
5600 /* Create gimple variants for common types. */
5601 ptrdiff_type_node = integer_type_node;
5602 fileptr_type_node = ptr_type_node;
5604 /* Reset some langhooks. Do not reset types_compatible_p, it may
5605 still be used indirectly via the get_alias_set langhook. */
5606 lang_hooks.dwarf_name = lhd_dwarf_name;
5607 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5608 /* We do not want the default decl_assembler_name implementation,
5609 rather if we have fixed everything we want a wrapper around it
5610 asserting that all non-local symbols already got their assembler
5611 name and only produce assembler names for local symbols. Or rather
5612 make sure we never call decl_assembler_name on local symbols and
5613 devise a separate, middle-end private scheme for it. */
5615 /* Reset diagnostic machinery. */
5616 tree_diagnostics_defaults (global_dc);
5618 return 0;
5622 namespace {
5624 const pass_data pass_data_ipa_free_lang_data =
5626 SIMPLE_IPA_PASS, /* type */
5627 "*free_lang_data", /* name */
5628 OPTGROUP_NONE, /* optinfo_flags */
5629 false, /* has_gate */
5630 true, /* has_execute */
5631 TV_IPA_FREE_LANG_DATA, /* tv_id */
5632 0, /* properties_required */
5633 0, /* properties_provided */
5634 0, /* properties_destroyed */
5635 0, /* todo_flags_start */
5636 0, /* todo_flags_finish */
5639 class pass_ipa_free_lang_data : public simple_ipa_opt_pass
5641 public:
5642 pass_ipa_free_lang_data (gcc::context *ctxt)
5643 : simple_ipa_opt_pass (pass_data_ipa_free_lang_data, ctxt)
5646 /* opt_pass methods: */
5647 unsigned int execute () { return free_lang_data (); }
5649 }; // class pass_ipa_free_lang_data
5651 } // anon namespace
5653 simple_ipa_opt_pass *
5654 make_pass_ipa_free_lang_data (gcc::context *ctxt)
5656 return new pass_ipa_free_lang_data (ctxt);
5659 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
5660 ATTR_NAME. Also used internally by remove_attribute(). */
5661 bool
5662 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
5664 size_t ident_len = IDENTIFIER_LENGTH (ident);
5666 if (ident_len == attr_len)
5668 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
5669 return true;
5671 else if (ident_len == attr_len + 4)
5673 /* There is the possibility that ATTR is 'text' and IDENT is
5674 '__text__'. */
5675 const char *p = IDENTIFIER_POINTER (ident);
5676 if (p[0] == '_' && p[1] == '_'
5677 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5678 && strncmp (attr_name, p + 2, attr_len) == 0)
5679 return true;
5682 return false;
5685 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
5686 of ATTR_NAME, and LIST is not NULL_TREE. */
5687 tree
5688 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
5690 while (list)
5692 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5694 if (ident_len == attr_len)
5696 if (!strcmp (attr_name,
5697 IDENTIFIER_POINTER (get_attribute_name (list))))
5698 break;
5700 /* TODO: If we made sure that attributes were stored in the
5701 canonical form without '__...__' (ie, as in 'text' as opposed
5702 to '__text__') then we could avoid the following case. */
5703 else if (ident_len == attr_len + 4)
5705 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5706 if (p[0] == '_' && p[1] == '_'
5707 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5708 && strncmp (attr_name, p + 2, attr_len) == 0)
5709 break;
5711 list = TREE_CHAIN (list);
5714 return list;
5717 /* A variant of lookup_attribute() that can be used with an identifier
5718 as the first argument, and where the identifier can be either
5719 'text' or '__text__'.
5721 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
5722 return a pointer to the attribute's list element if the attribute
5723 is part of the list, or NULL_TREE if not found. If the attribute
5724 appears more than once, this only returns the first occurrence; the
5725 TREE_CHAIN of the return value should be passed back in if further
5726 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
5727 can be in the form 'text' or '__text__'. */
5728 static tree
5729 lookup_ident_attribute (tree attr_identifier, tree list)
5731 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
5733 while (list)
5735 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
5736 == IDENTIFIER_NODE);
5738 /* Identifiers can be compared directly for equality. */
5739 if (attr_identifier == get_attribute_name (list))
5740 break;
5742 /* If they are not equal, they may still be one in the form
5743 'text' while the other one is in the form '__text__'. TODO:
5744 If we were storing attributes in normalized 'text' form, then
5745 this could all go away and we could take full advantage of
5746 the fact that we're comparing identifiers. :-) */
5748 size_t attr_len = IDENTIFIER_LENGTH (attr_identifier);
5749 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5751 if (ident_len == attr_len + 4)
5753 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5754 const char *q = IDENTIFIER_POINTER (attr_identifier);
5755 if (p[0] == '_' && p[1] == '_'
5756 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5757 && strncmp (q, p + 2, attr_len) == 0)
5758 break;
5760 else if (ident_len + 4 == attr_len)
5762 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5763 const char *q = IDENTIFIER_POINTER (attr_identifier);
5764 if (q[0] == '_' && q[1] == '_'
5765 && q[attr_len - 2] == '_' && q[attr_len - 1] == '_'
5766 && strncmp (q + 2, p, ident_len) == 0)
5767 break;
5770 list = TREE_CHAIN (list);
5773 return list;
5776 /* Remove any instances of attribute ATTR_NAME in LIST and return the
5777 modified list. */
5779 tree
5780 remove_attribute (const char *attr_name, tree list)
5782 tree *p;
5783 size_t attr_len = strlen (attr_name);
5785 gcc_checking_assert (attr_name[0] != '_');
5787 for (p = &list; *p; )
5789 tree l = *p;
5790 /* TODO: If we were storing attributes in normalized form, here
5791 we could use a simple strcmp(). */
5792 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
5793 *p = TREE_CHAIN (l);
5794 else
5795 p = &TREE_CHAIN (l);
5798 return list;
5801 /* Return an attribute list that is the union of a1 and a2. */
5803 tree
5804 merge_attributes (tree a1, tree a2)
5806 tree attributes;
5808 /* Either one unset? Take the set one. */
5810 if ((attributes = a1) == 0)
5811 attributes = a2;
5813 /* One that completely contains the other? Take it. */
5815 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
5817 if (attribute_list_contained (a2, a1))
5818 attributes = a2;
5819 else
5821 /* Pick the longest list, and hang on the other list. */
5823 if (list_length (a1) < list_length (a2))
5824 attributes = a2, a2 = a1;
5826 for (; a2 != 0; a2 = TREE_CHAIN (a2))
5828 tree a;
5829 for (a = lookup_ident_attribute (get_attribute_name (a2),
5830 attributes);
5831 a != NULL_TREE && !attribute_value_equal (a, a2);
5832 a = lookup_ident_attribute (get_attribute_name (a2),
5833 TREE_CHAIN (a)))
5835 if (a == NULL_TREE)
5837 a1 = copy_node (a2);
5838 TREE_CHAIN (a1) = attributes;
5839 attributes = a1;
5844 return attributes;
5847 /* Given types T1 and T2, merge their attributes and return
5848 the result. */
5850 tree
5851 merge_type_attributes (tree t1, tree t2)
5853 return merge_attributes (TYPE_ATTRIBUTES (t1),
5854 TYPE_ATTRIBUTES (t2));
5857 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
5858 the result. */
5860 tree
5861 merge_decl_attributes (tree olddecl, tree newdecl)
5863 return merge_attributes (DECL_ATTRIBUTES (olddecl),
5864 DECL_ATTRIBUTES (newdecl));
5867 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
5869 /* Specialization of merge_decl_attributes for various Windows targets.
5871 This handles the following situation:
5873 __declspec (dllimport) int foo;
5874 int foo;
5876 The second instance of `foo' nullifies the dllimport. */
5878 tree
5879 merge_dllimport_decl_attributes (tree old, tree new_tree)
5881 tree a;
5882 int delete_dllimport_p = 1;
5884 /* What we need to do here is remove from `old' dllimport if it doesn't
5885 appear in `new'. dllimport behaves like extern: if a declaration is
5886 marked dllimport and a definition appears later, then the object
5887 is not dllimport'd. We also remove a `new' dllimport if the old list
5888 contains dllexport: dllexport always overrides dllimport, regardless
5889 of the order of declaration. */
5890 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
5891 delete_dllimport_p = 0;
5892 else if (DECL_DLLIMPORT_P (new_tree)
5893 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
5895 DECL_DLLIMPORT_P (new_tree) = 0;
5896 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
5897 "dllimport ignored", new_tree);
5899 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
5901 /* Warn about overriding a symbol that has already been used, e.g.:
5902 extern int __attribute__ ((dllimport)) foo;
5903 int* bar () {return &foo;}
5904 int foo;
5906 if (TREE_USED (old))
5908 warning (0, "%q+D redeclared without dllimport attribute "
5909 "after being referenced with dll linkage", new_tree);
5910 /* If we have used a variable's address with dllimport linkage,
5911 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
5912 decl may already have had TREE_CONSTANT computed.
5913 We still remove the attribute so that assembler code refers
5914 to '&foo rather than '_imp__foo'. */
5915 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
5916 DECL_DLLIMPORT_P (new_tree) = 1;
5919 /* Let an inline definition silently override the external reference,
5920 but otherwise warn about attribute inconsistency. */
5921 else if (TREE_CODE (new_tree) == VAR_DECL
5922 || !DECL_DECLARED_INLINE_P (new_tree))
5923 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
5924 "previous dllimport ignored", new_tree);
5926 else
5927 delete_dllimport_p = 0;
5929 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
5931 if (delete_dllimport_p)
5932 a = remove_attribute ("dllimport", a);
5934 return a;
5937 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
5938 struct attribute_spec.handler. */
5940 tree
5941 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
5942 bool *no_add_attrs)
5944 tree node = *pnode;
5945 bool is_dllimport;
5947 /* These attributes may apply to structure and union types being created,
5948 but otherwise should pass to the declaration involved. */
5949 if (!DECL_P (node))
5951 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
5952 | (int) ATTR_FLAG_ARRAY_NEXT))
5954 *no_add_attrs = true;
5955 return tree_cons (name, args, NULL_TREE);
5957 if (TREE_CODE (node) == RECORD_TYPE
5958 || TREE_CODE (node) == UNION_TYPE)
5960 node = TYPE_NAME (node);
5961 if (!node)
5962 return NULL_TREE;
5964 else
5966 warning (OPT_Wattributes, "%qE attribute ignored",
5967 name);
5968 *no_add_attrs = true;
5969 return NULL_TREE;
5973 if (TREE_CODE (node) != FUNCTION_DECL
5974 && TREE_CODE (node) != VAR_DECL
5975 && TREE_CODE (node) != TYPE_DECL)
5977 *no_add_attrs = true;
5978 warning (OPT_Wattributes, "%qE attribute ignored",
5979 name);
5980 return NULL_TREE;
5983 if (TREE_CODE (node) == TYPE_DECL
5984 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
5985 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
5987 *no_add_attrs = true;
5988 warning (OPT_Wattributes, "%qE attribute ignored",
5989 name);
5990 return NULL_TREE;
5993 is_dllimport = is_attribute_p ("dllimport", name);
5995 /* Report error on dllimport ambiguities seen now before they cause
5996 any damage. */
5997 if (is_dllimport)
5999 /* Honor any target-specific overrides. */
6000 if (!targetm.valid_dllimport_attribute_p (node))
6001 *no_add_attrs = true;
6003 else if (TREE_CODE (node) == FUNCTION_DECL
6004 && DECL_DECLARED_INLINE_P (node))
6006 warning (OPT_Wattributes, "inline function %q+D declared as "
6007 " dllimport: attribute ignored", node);
6008 *no_add_attrs = true;
6010 /* Like MS, treat definition of dllimported variables and
6011 non-inlined functions on declaration as syntax errors. */
6012 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
6014 error ("function %q+D definition is marked dllimport", node);
6015 *no_add_attrs = true;
6018 else if (TREE_CODE (node) == VAR_DECL)
6020 if (DECL_INITIAL (node))
6022 error ("variable %q+D definition is marked dllimport",
6023 node);
6024 *no_add_attrs = true;
6027 /* `extern' needn't be specified with dllimport.
6028 Specify `extern' now and hope for the best. Sigh. */
6029 DECL_EXTERNAL (node) = 1;
6030 /* Also, implicitly give dllimport'd variables declared within
6031 a function global scope, unless declared static. */
6032 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
6033 TREE_PUBLIC (node) = 1;
6036 if (*no_add_attrs == false)
6037 DECL_DLLIMPORT_P (node) = 1;
6039 else if (TREE_CODE (node) == FUNCTION_DECL
6040 && DECL_DECLARED_INLINE_P (node)
6041 && flag_keep_inline_dllexport)
6042 /* An exported function, even if inline, must be emitted. */
6043 DECL_EXTERNAL (node) = 0;
6045 /* Report error if symbol is not accessible at global scope. */
6046 if (!TREE_PUBLIC (node)
6047 && (TREE_CODE (node) == VAR_DECL
6048 || TREE_CODE (node) == FUNCTION_DECL))
6050 error ("external linkage required for symbol %q+D because of "
6051 "%qE attribute", node, name);
6052 *no_add_attrs = true;
6055 /* A dllexport'd entity must have default visibility so that other
6056 program units (shared libraries or the main executable) can see
6057 it. A dllimport'd entity must have default visibility so that
6058 the linker knows that undefined references within this program
6059 unit can be resolved by the dynamic linker. */
6060 if (!*no_add_attrs)
6062 if (DECL_VISIBILITY_SPECIFIED (node)
6063 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
6064 error ("%qE implies default visibility, but %qD has already "
6065 "been declared with a different visibility",
6066 name, node);
6067 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
6068 DECL_VISIBILITY_SPECIFIED (node) = 1;
6071 return NULL_TREE;
6074 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
6076 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
6077 of the various TYPE_QUAL values. */
6079 static void
6080 set_type_quals (tree type, int type_quals)
6082 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
6083 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
6084 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
6085 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
6086 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
6089 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
6091 bool
6092 check_qualified_type (const_tree cand, const_tree base, int type_quals)
6094 return (TYPE_QUALS (cand) == type_quals
6095 && TYPE_NAME (cand) == TYPE_NAME (base)
6096 /* Apparently this is needed for Objective-C. */
6097 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6098 /* Check alignment. */
6099 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
6100 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6101 TYPE_ATTRIBUTES (base)));
6104 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
6106 static bool
6107 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
6109 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
6110 && TYPE_NAME (cand) == TYPE_NAME (base)
6111 /* Apparently this is needed for Objective-C. */
6112 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6113 /* Check alignment. */
6114 && TYPE_ALIGN (cand) == align
6115 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6116 TYPE_ATTRIBUTES (base)));
6119 /* This function checks to see if TYPE matches the size one of the built-in
6120 atomic types, and returns that core atomic type. */
6122 static tree
6123 find_atomic_core_type (tree type)
6125 tree base_atomic_type;
6127 /* Only handle complete types. */
6128 if (TYPE_SIZE (type) == NULL_TREE)
6129 return NULL_TREE;
6131 HOST_WIDE_INT type_size = tree_to_uhwi (TYPE_SIZE (type));
6132 switch (type_size)
6134 case 8:
6135 base_atomic_type = atomicQI_type_node;
6136 break;
6138 case 16:
6139 base_atomic_type = atomicHI_type_node;
6140 break;
6142 case 32:
6143 base_atomic_type = atomicSI_type_node;
6144 break;
6146 case 64:
6147 base_atomic_type = atomicDI_type_node;
6148 break;
6150 case 128:
6151 base_atomic_type = atomicTI_type_node;
6152 break;
6154 default:
6155 base_atomic_type = NULL_TREE;
6158 return base_atomic_type;
6161 /* Return a version of the TYPE, qualified as indicated by the
6162 TYPE_QUALS, if one exists. If no qualified version exists yet,
6163 return NULL_TREE. */
6165 tree
6166 get_qualified_type (tree type, int type_quals)
6168 tree t;
6170 if (TYPE_QUALS (type) == type_quals)
6171 return type;
6173 /* Search the chain of variants to see if there is already one there just
6174 like the one we need to have. If so, use that existing one. We must
6175 preserve the TYPE_NAME, since there is code that depends on this. */
6176 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6177 if (check_qualified_type (t, type, type_quals))
6178 return t;
6180 return NULL_TREE;
6183 /* Like get_qualified_type, but creates the type if it does not
6184 exist. This function never returns NULL_TREE. */
6186 tree
6187 build_qualified_type (tree type, int type_quals)
6189 tree t;
6191 /* See if we already have the appropriate qualified variant. */
6192 t = get_qualified_type (type, type_quals);
6194 /* If not, build it. */
6195 if (!t)
6197 t = build_variant_type_copy (type);
6198 set_type_quals (t, type_quals);
6200 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
6202 /* See if this object can map to a basic atomic type. */
6203 tree atomic_type = find_atomic_core_type (type);
6204 if (atomic_type)
6206 /* Ensure the alignment of this type is compatible with
6207 the required alignment of the atomic type. */
6208 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
6209 TYPE_ALIGN (t) = TYPE_ALIGN (atomic_type);
6213 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6214 /* Propagate structural equality. */
6215 SET_TYPE_STRUCTURAL_EQUALITY (t);
6216 else if (TYPE_CANONICAL (type) != type)
6217 /* Build the underlying canonical type, since it is different
6218 from TYPE. */
6220 tree c = build_qualified_type (TYPE_CANONICAL (type),
6221 type_quals);
6222 TYPE_CANONICAL (t) = TYPE_CANONICAL (c);
6224 else
6225 /* T is its own canonical type. */
6226 TYPE_CANONICAL (t) = t;
6230 return t;
6233 /* Create a variant of type T with alignment ALIGN. */
6235 tree
6236 build_aligned_type (tree type, unsigned int align)
6238 tree t;
6240 if (TYPE_PACKED (type)
6241 || TYPE_ALIGN (type) == align)
6242 return type;
6244 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6245 if (check_aligned_type (t, type, align))
6246 return t;
6248 t = build_variant_type_copy (type);
6249 TYPE_ALIGN (t) = align;
6251 return t;
6254 /* Create a new distinct copy of TYPE. The new type is made its own
6255 MAIN_VARIANT. If TYPE requires structural equality checks, the
6256 resulting type requires structural equality checks; otherwise, its
6257 TYPE_CANONICAL points to itself. */
6259 tree
6260 build_distinct_type_copy (tree type)
6262 tree t = copy_node (type);
6264 TYPE_POINTER_TO (t) = 0;
6265 TYPE_REFERENCE_TO (t) = 0;
6267 /* Set the canonical type either to a new equivalence class, or
6268 propagate the need for structural equality checks. */
6269 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6270 SET_TYPE_STRUCTURAL_EQUALITY (t);
6271 else
6272 TYPE_CANONICAL (t) = t;
6274 /* Make it its own variant. */
6275 TYPE_MAIN_VARIANT (t) = t;
6276 TYPE_NEXT_VARIANT (t) = 0;
6278 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
6279 whose TREE_TYPE is not t. This can also happen in the Ada
6280 frontend when using subtypes. */
6282 return t;
6285 /* Create a new variant of TYPE, equivalent but distinct. This is so
6286 the caller can modify it. TYPE_CANONICAL for the return type will
6287 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
6288 are considered equal by the language itself (or that both types
6289 require structural equality checks). */
6291 tree
6292 build_variant_type_copy (tree type)
6294 tree t, m = TYPE_MAIN_VARIANT (type);
6296 t = build_distinct_type_copy (type);
6298 /* Since we're building a variant, assume that it is a non-semantic
6299 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
6300 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
6302 /* Add the new type to the chain of variants of TYPE. */
6303 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
6304 TYPE_NEXT_VARIANT (m) = t;
6305 TYPE_MAIN_VARIANT (t) = m;
6307 return t;
6310 /* Return true if the from tree in both tree maps are equal. */
6313 tree_map_base_eq (const void *va, const void *vb)
6315 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6316 *const b = (const struct tree_map_base *) vb;
6317 return (a->from == b->from);
6320 /* Hash a from tree in a tree_base_map. */
6322 unsigned int
6323 tree_map_base_hash (const void *item)
6325 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6328 /* Return true if this tree map structure is marked for garbage collection
6329 purposes. We simply return true if the from tree is marked, so that this
6330 structure goes away when the from tree goes away. */
6333 tree_map_base_marked_p (const void *p)
6335 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6338 /* Hash a from tree in a tree_map. */
6340 unsigned int
6341 tree_map_hash (const void *item)
6343 return (((const struct tree_map *) item)->hash);
6346 /* Hash a from tree in a tree_decl_map. */
6348 unsigned int
6349 tree_decl_map_hash (const void *item)
6351 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6354 /* Return the initialization priority for DECL. */
6356 priority_type
6357 decl_init_priority_lookup (tree decl)
6359 struct tree_priority_map *h;
6360 struct tree_map_base in;
6362 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6363 in.from = decl;
6364 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6365 return h ? h->init : DEFAULT_INIT_PRIORITY;
6368 /* Return the finalization priority for DECL. */
6370 priority_type
6371 decl_fini_priority_lookup (tree decl)
6373 struct tree_priority_map *h;
6374 struct tree_map_base in;
6376 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6377 in.from = decl;
6378 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6379 return h ? h->fini : DEFAULT_INIT_PRIORITY;
6382 /* Return the initialization and finalization priority information for
6383 DECL. If there is no previous priority information, a freshly
6384 allocated structure is returned. */
6386 static struct tree_priority_map *
6387 decl_priority_info (tree decl)
6389 struct tree_priority_map in;
6390 struct tree_priority_map *h;
6391 void **loc;
6393 in.base.from = decl;
6394 loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
6395 h = (struct tree_priority_map *) *loc;
6396 if (!h)
6398 h = ggc_alloc_cleared_tree_priority_map ();
6399 *loc = h;
6400 h->base.from = decl;
6401 h->init = DEFAULT_INIT_PRIORITY;
6402 h->fini = DEFAULT_INIT_PRIORITY;
6405 return h;
6408 /* Set the initialization priority for DECL to PRIORITY. */
6410 void
6411 decl_init_priority_insert (tree decl, priority_type priority)
6413 struct tree_priority_map *h;
6415 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6416 if (priority == DEFAULT_INIT_PRIORITY)
6417 return;
6418 h = decl_priority_info (decl);
6419 h->init = priority;
6422 /* Set the finalization priority for DECL to PRIORITY. */
6424 void
6425 decl_fini_priority_insert (tree decl, priority_type priority)
6427 struct tree_priority_map *h;
6429 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6430 if (priority == DEFAULT_INIT_PRIORITY)
6431 return;
6432 h = decl_priority_info (decl);
6433 h->fini = priority;
6436 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6438 static void
6439 print_debug_expr_statistics (void)
6441 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6442 (long) htab_size (debug_expr_for_decl),
6443 (long) htab_elements (debug_expr_for_decl),
6444 htab_collisions (debug_expr_for_decl));
6447 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6449 static void
6450 print_value_expr_statistics (void)
6452 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6453 (long) htab_size (value_expr_for_decl),
6454 (long) htab_elements (value_expr_for_decl),
6455 htab_collisions (value_expr_for_decl));
6458 /* Lookup a debug expression for FROM, and return it if we find one. */
6460 tree
6461 decl_debug_expr_lookup (tree from)
6463 struct tree_decl_map *h, in;
6464 in.base.from = from;
6466 h = (struct tree_decl_map *)
6467 htab_find_with_hash (debug_expr_for_decl, &in, DECL_UID (from));
6468 if (h)
6469 return h->to;
6470 return NULL_TREE;
6473 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6475 void
6476 decl_debug_expr_insert (tree from, tree to)
6478 struct tree_decl_map *h;
6479 void **loc;
6481 h = ggc_alloc_tree_decl_map ();
6482 h->base.from = from;
6483 h->to = to;
6484 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, DECL_UID (from),
6485 INSERT);
6486 *(struct tree_decl_map **) loc = h;
6489 /* Lookup a value expression for FROM, and return it if we find one. */
6491 tree
6492 decl_value_expr_lookup (tree from)
6494 struct tree_decl_map *h, in;
6495 in.base.from = from;
6497 h = (struct tree_decl_map *)
6498 htab_find_with_hash (value_expr_for_decl, &in, DECL_UID (from));
6499 if (h)
6500 return h->to;
6501 return NULL_TREE;
6504 /* Insert a mapping FROM->TO in the value expression hashtable. */
6506 void
6507 decl_value_expr_insert (tree from, tree to)
6509 struct tree_decl_map *h;
6510 void **loc;
6512 h = ggc_alloc_tree_decl_map ();
6513 h->base.from = from;
6514 h->to = to;
6515 loc = htab_find_slot_with_hash (value_expr_for_decl, h, DECL_UID (from),
6516 INSERT);
6517 *(struct tree_decl_map **) loc = h;
6520 /* Lookup a vector of debug arguments for FROM, and return it if we
6521 find one. */
6523 vec<tree, va_gc> **
6524 decl_debug_args_lookup (tree from)
6526 struct tree_vec_map *h, in;
6528 if (!DECL_HAS_DEBUG_ARGS_P (from))
6529 return NULL;
6530 gcc_checking_assert (debug_args_for_decl != NULL);
6531 in.base.from = from;
6532 h = (struct tree_vec_map *)
6533 htab_find_with_hash (debug_args_for_decl, &in, DECL_UID (from));
6534 if (h)
6535 return &h->to;
6536 return NULL;
6539 /* Insert a mapping FROM->empty vector of debug arguments in the value
6540 expression hashtable. */
6542 vec<tree, va_gc> **
6543 decl_debug_args_insert (tree from)
6545 struct tree_vec_map *h;
6546 void **loc;
6548 if (DECL_HAS_DEBUG_ARGS_P (from))
6549 return decl_debug_args_lookup (from);
6550 if (debug_args_for_decl == NULL)
6551 debug_args_for_decl = htab_create_ggc (64, tree_vec_map_hash,
6552 tree_vec_map_eq, 0);
6553 h = ggc_alloc_tree_vec_map ();
6554 h->base.from = from;
6555 h->to = NULL;
6556 loc = htab_find_slot_with_hash (debug_args_for_decl, h, DECL_UID (from),
6557 INSERT);
6558 *(struct tree_vec_map **) loc = h;
6559 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6560 return &h->to;
6563 /* Hashing of types so that we don't make duplicates.
6564 The entry point is `type_hash_canon'. */
6566 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6567 with types in the TREE_VALUE slots), by adding the hash codes
6568 of the individual types. */
6570 static unsigned int
6571 type_hash_list (const_tree list, hashval_t hashcode)
6573 const_tree tail;
6575 for (tail = list; tail; tail = TREE_CHAIN (tail))
6576 if (TREE_VALUE (tail) != error_mark_node)
6577 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
6578 hashcode);
6580 return hashcode;
6583 /* These are the Hashtable callback functions. */
6585 /* Returns true iff the types are equivalent. */
6587 static int
6588 type_hash_eq (const void *va, const void *vb)
6590 const struct type_hash *const a = (const struct type_hash *) va,
6591 *const b = (const struct type_hash *) vb;
6593 /* First test the things that are the same for all types. */
6594 if (a->hash != b->hash
6595 || TREE_CODE (a->type) != TREE_CODE (b->type)
6596 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6597 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6598 TYPE_ATTRIBUTES (b->type))
6599 || (TREE_CODE (a->type) != COMPLEX_TYPE
6600 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6601 return 0;
6603 /* Be careful about comparing arrays before and after the element type
6604 has been completed; don't compare TYPE_ALIGN unless both types are
6605 complete. */
6606 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6607 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6608 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6609 return 0;
6611 switch (TREE_CODE (a->type))
6613 case VOID_TYPE:
6614 case COMPLEX_TYPE:
6615 case POINTER_TYPE:
6616 case REFERENCE_TYPE:
6617 case NULLPTR_TYPE:
6618 return 1;
6620 case VECTOR_TYPE:
6621 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6623 case ENUMERAL_TYPE:
6624 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6625 && !(TYPE_VALUES (a->type)
6626 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6627 && TYPE_VALUES (b->type)
6628 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6629 && type_list_equal (TYPE_VALUES (a->type),
6630 TYPE_VALUES (b->type))))
6631 return 0;
6633 /* ... fall through ... */
6635 case INTEGER_TYPE:
6636 case REAL_TYPE:
6637 case BOOLEAN_TYPE:
6638 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6639 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6640 TYPE_MAX_VALUE (b->type)))
6641 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6642 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6643 TYPE_MIN_VALUE (b->type))));
6645 case FIXED_POINT_TYPE:
6646 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6648 case OFFSET_TYPE:
6649 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6651 case METHOD_TYPE:
6652 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6653 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6654 || (TYPE_ARG_TYPES (a->type)
6655 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6656 && TYPE_ARG_TYPES (b->type)
6657 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6658 && type_list_equal (TYPE_ARG_TYPES (a->type),
6659 TYPE_ARG_TYPES (b->type)))))
6660 break;
6661 return 0;
6662 case ARRAY_TYPE:
6663 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6665 case RECORD_TYPE:
6666 case UNION_TYPE:
6667 case QUAL_UNION_TYPE:
6668 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6669 || (TYPE_FIELDS (a->type)
6670 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6671 && TYPE_FIELDS (b->type)
6672 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6673 && type_list_equal (TYPE_FIELDS (a->type),
6674 TYPE_FIELDS (b->type))));
6676 case FUNCTION_TYPE:
6677 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6678 || (TYPE_ARG_TYPES (a->type)
6679 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6680 && TYPE_ARG_TYPES (b->type)
6681 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6682 && type_list_equal (TYPE_ARG_TYPES (a->type),
6683 TYPE_ARG_TYPES (b->type))))
6684 break;
6685 return 0;
6687 default:
6688 return 0;
6691 if (lang_hooks.types.type_hash_eq != NULL)
6692 return lang_hooks.types.type_hash_eq (a->type, b->type);
6694 return 1;
6697 /* Return the cached hash value. */
6699 static hashval_t
6700 type_hash_hash (const void *item)
6702 return ((const struct type_hash *) item)->hash;
6705 /* Look in the type hash table for a type isomorphic to TYPE.
6706 If one is found, return it. Otherwise return 0. */
6708 static tree
6709 type_hash_lookup (hashval_t hashcode, tree type)
6711 struct type_hash *h, in;
6713 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6714 must call that routine before comparing TYPE_ALIGNs. */
6715 layout_type (type);
6717 in.hash = hashcode;
6718 in.type = type;
6720 h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
6721 hashcode);
6722 if (h)
6723 return h->type;
6724 return NULL_TREE;
6727 /* Add an entry to the type-hash-table
6728 for a type TYPE whose hash code is HASHCODE. */
6730 static void
6731 type_hash_add (hashval_t hashcode, tree type)
6733 struct type_hash *h;
6734 void **loc;
6736 h = ggc_alloc_type_hash ();
6737 h->hash = hashcode;
6738 h->type = type;
6739 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
6740 *loc = (void *)h;
6743 /* Given TYPE, and HASHCODE its hash code, return the canonical
6744 object for an identical type if one already exists.
6745 Otherwise, return TYPE, and record it as the canonical object.
6747 To use this function, first create a type of the sort you want.
6748 Then compute its hash code from the fields of the type that
6749 make it different from other similar types.
6750 Then call this function and use the value. */
6752 tree
6753 type_hash_canon (unsigned int hashcode, tree type)
6755 tree t1;
6757 /* The hash table only contains main variants, so ensure that's what we're
6758 being passed. */
6759 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6761 /* See if the type is in the hash table already. If so, return it.
6762 Otherwise, add the type. */
6763 t1 = type_hash_lookup (hashcode, type);
6764 if (t1 != 0)
6766 if (GATHER_STATISTICS)
6768 tree_code_counts[(int) TREE_CODE (type)]--;
6769 tree_node_counts[(int) t_kind]--;
6770 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
6772 return t1;
6774 else
6776 type_hash_add (hashcode, type);
6777 return type;
6781 /* See if the data pointed to by the type hash table is marked. We consider
6782 it marked if the type is marked or if a debug type number or symbol
6783 table entry has been made for the type. */
6785 static int
6786 type_hash_marked_p (const void *p)
6788 const_tree const type = ((const struct type_hash *) p)->type;
6790 return ggc_marked_p (type);
6793 static void
6794 print_type_hash_statistics (void)
6796 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6797 (long) htab_size (type_hash_table),
6798 (long) htab_elements (type_hash_table),
6799 htab_collisions (type_hash_table));
6802 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
6803 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
6804 by adding the hash codes of the individual attributes. */
6806 static unsigned int
6807 attribute_hash_list (const_tree list, hashval_t hashcode)
6809 const_tree tail;
6811 for (tail = list; tail; tail = TREE_CHAIN (tail))
6812 /* ??? Do we want to add in TREE_VALUE too? */
6813 hashcode = iterative_hash_object
6814 (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)), hashcode);
6815 return hashcode;
6818 /* Given two lists of attributes, return true if list l2 is
6819 equivalent to l1. */
6822 attribute_list_equal (const_tree l1, const_tree l2)
6824 if (l1 == l2)
6825 return 1;
6827 return attribute_list_contained (l1, l2)
6828 && attribute_list_contained (l2, l1);
6831 /* Given two lists of attributes, return true if list L2 is
6832 completely contained within L1. */
6833 /* ??? This would be faster if attribute names were stored in a canonicalized
6834 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
6835 must be used to show these elements are equivalent (which they are). */
6836 /* ??? It's not clear that attributes with arguments will always be handled
6837 correctly. */
6840 attribute_list_contained (const_tree l1, const_tree l2)
6842 const_tree t1, t2;
6844 /* First check the obvious, maybe the lists are identical. */
6845 if (l1 == l2)
6846 return 1;
6848 /* Maybe the lists are similar. */
6849 for (t1 = l1, t2 = l2;
6850 t1 != 0 && t2 != 0
6851 && get_attribute_name (t1) == get_attribute_name (t2)
6852 && TREE_VALUE (t1) == TREE_VALUE (t2);
6853 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6856 /* Maybe the lists are equal. */
6857 if (t1 == 0 && t2 == 0)
6858 return 1;
6860 for (; t2 != 0; t2 = TREE_CHAIN (t2))
6862 const_tree attr;
6863 /* This CONST_CAST is okay because lookup_attribute does not
6864 modify its argument and the return value is assigned to a
6865 const_tree. */
6866 for (attr = lookup_ident_attribute (get_attribute_name (t2),
6867 CONST_CAST_TREE (l1));
6868 attr != NULL_TREE && !attribute_value_equal (t2, attr);
6869 attr = lookup_ident_attribute (get_attribute_name (t2),
6870 TREE_CHAIN (attr)))
6873 if (attr == NULL_TREE)
6874 return 0;
6877 return 1;
6880 /* Given two lists of types
6881 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6882 return 1 if the lists contain the same types in the same order.
6883 Also, the TREE_PURPOSEs must match. */
6886 type_list_equal (const_tree l1, const_tree l2)
6888 const_tree t1, t2;
6890 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6891 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6892 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6893 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6894 && (TREE_TYPE (TREE_PURPOSE (t1))
6895 == TREE_TYPE (TREE_PURPOSE (t2))))))
6896 return 0;
6898 return t1 == t2;
6901 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6902 given by TYPE. If the argument list accepts variable arguments,
6903 then this function counts only the ordinary arguments. */
6906 type_num_arguments (const_tree type)
6908 int i = 0;
6909 tree t;
6911 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6912 /* If the function does not take a variable number of arguments,
6913 the last element in the list will have type `void'. */
6914 if (VOID_TYPE_P (TREE_VALUE (t)))
6915 break;
6916 else
6917 ++i;
6919 return i;
6922 /* Nonzero if integer constants T1 and T2
6923 represent the same constant value. */
6926 tree_int_cst_equal (const_tree t1, const_tree t2)
6928 if (t1 == t2)
6929 return 1;
6931 if (t1 == 0 || t2 == 0)
6932 return 0;
6934 if (TREE_CODE (t1) == INTEGER_CST
6935 && TREE_CODE (t2) == INTEGER_CST
6936 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6937 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
6938 return 1;
6940 return 0;
6943 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
6944 The precise way of comparison depends on their data type. */
6947 tree_int_cst_lt (const_tree t1, const_tree t2)
6949 if (t1 == t2)
6950 return 0;
6952 if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
6954 int t1_sgn = tree_int_cst_sgn (t1);
6955 int t2_sgn = tree_int_cst_sgn (t2);
6957 if (t1_sgn < t2_sgn)
6958 return 1;
6959 else if (t1_sgn > t2_sgn)
6960 return 0;
6961 /* Otherwise, both are non-negative, so we compare them as
6962 unsigned just in case one of them would overflow a signed
6963 type. */
6965 else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
6966 return INT_CST_LT (t1, t2);
6968 return INT_CST_LT_UNSIGNED (t1, t2);
6971 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
6974 tree_int_cst_compare (const_tree t1, const_tree t2)
6976 if (tree_int_cst_lt (t1, t2))
6977 return -1;
6978 else if (tree_int_cst_lt (t2, t1))
6979 return 1;
6980 else
6981 return 0;
6984 /* Return true if T is an INTEGER_CST whose numerical value (extended
6985 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
6987 bool
6988 tree_fits_shwi_p (const_tree t)
6990 return (t != NULL_TREE
6991 && TREE_CODE (t) == INTEGER_CST
6992 && ((TREE_INT_CST_HIGH (t) == 0
6993 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
6994 || (TREE_INT_CST_HIGH (t) == -1
6995 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
6996 && !TYPE_UNSIGNED (TREE_TYPE (t)))));
6999 /* Return true if T is an INTEGER_CST whose numerical value (extended
7000 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
7002 bool
7003 tree_fits_uhwi_p (const_tree t)
7005 return (t != NULL_TREE
7006 && TREE_CODE (t) == INTEGER_CST
7007 && TREE_INT_CST_HIGH (t) == 0);
7010 /* T is an INTEGER_CST whose numerical value (extended according to
7011 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
7012 HOST_WIDE_INT. */
7014 HOST_WIDE_INT
7015 tree_to_shwi (const_tree t)
7017 gcc_assert (tree_fits_shwi_p (t));
7018 return TREE_INT_CST_LOW (t);
7021 /* T is an INTEGER_CST whose numerical value (extended according to
7022 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
7023 HOST_WIDE_INT. */
7025 unsigned HOST_WIDE_INT
7026 tree_to_uhwi (const_tree t)
7028 gcc_assert (tree_fits_uhwi_p (t));
7029 return TREE_INT_CST_LOW (t);
7032 /* Return the most significant (sign) bit of T. */
7035 tree_int_cst_sign_bit (const_tree t)
7037 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
7038 unsigned HOST_WIDE_INT w;
7040 if (bitno < HOST_BITS_PER_WIDE_INT)
7041 w = TREE_INT_CST_LOW (t);
7042 else
7044 w = TREE_INT_CST_HIGH (t);
7045 bitno -= HOST_BITS_PER_WIDE_INT;
7048 return (w >> bitno) & 1;
7051 /* Return an indication of the sign of the integer constant T.
7052 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
7053 Note that -1 will never be returned if T's type is unsigned. */
7056 tree_int_cst_sgn (const_tree t)
7058 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
7059 return 0;
7060 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
7061 return 1;
7062 else if (TREE_INT_CST_HIGH (t) < 0)
7063 return -1;
7064 else
7065 return 1;
7068 /* Return the minimum number of bits needed to represent VALUE in a
7069 signed or unsigned type, UNSIGNEDP says which. */
7071 unsigned int
7072 tree_int_cst_min_precision (tree value, bool unsignedp)
7074 /* If the value is negative, compute its negative minus 1. The latter
7075 adjustment is because the absolute value of the largest negative value
7076 is one larger than the largest positive value. This is equivalent to
7077 a bit-wise negation, so use that operation instead. */
7079 if (tree_int_cst_sgn (value) < 0)
7080 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
7082 /* Return the number of bits needed, taking into account the fact
7083 that we need one more bit for a signed than unsigned type.
7084 If value is 0 or -1, the minimum precision is 1 no matter
7085 whether unsignedp is true or false. */
7087 if (integer_zerop (value))
7088 return 1;
7089 else
7090 return tree_floor_log2 (value) + 1 + !unsignedp;
7093 /* Return truthvalue of whether T1 is the same tree structure as T2.
7094 Return 1 if they are the same.
7095 Return 0 if they are understandably different.
7096 Return -1 if either contains tree structure not understood by
7097 this function. */
7100 simple_cst_equal (const_tree t1, const_tree t2)
7102 enum tree_code code1, code2;
7103 int cmp;
7104 int i;
7106 if (t1 == t2)
7107 return 1;
7108 if (t1 == 0 || t2 == 0)
7109 return 0;
7111 code1 = TREE_CODE (t1);
7112 code2 = TREE_CODE (t2);
7114 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
7116 if (CONVERT_EXPR_CODE_P (code2)
7117 || code2 == NON_LVALUE_EXPR)
7118 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7119 else
7120 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
7123 else if (CONVERT_EXPR_CODE_P (code2)
7124 || code2 == NON_LVALUE_EXPR)
7125 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
7127 if (code1 != code2)
7128 return 0;
7130 switch (code1)
7132 case INTEGER_CST:
7133 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
7134 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
7136 case REAL_CST:
7137 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
7139 case FIXED_CST:
7140 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
7142 case STRING_CST:
7143 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
7144 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
7145 TREE_STRING_LENGTH (t1)));
7147 case CONSTRUCTOR:
7149 unsigned HOST_WIDE_INT idx;
7150 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
7151 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
7153 if (vec_safe_length (v1) != vec_safe_length (v2))
7154 return false;
7156 for (idx = 0; idx < vec_safe_length (v1); ++idx)
7157 /* ??? Should we handle also fields here? */
7158 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
7159 return false;
7160 return true;
7163 case SAVE_EXPR:
7164 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7166 case CALL_EXPR:
7167 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
7168 if (cmp <= 0)
7169 return cmp;
7170 if (call_expr_nargs (t1) != call_expr_nargs (t2))
7171 return 0;
7173 const_tree arg1, arg2;
7174 const_call_expr_arg_iterator iter1, iter2;
7175 for (arg1 = first_const_call_expr_arg (t1, &iter1),
7176 arg2 = first_const_call_expr_arg (t2, &iter2);
7177 arg1 && arg2;
7178 arg1 = next_const_call_expr_arg (&iter1),
7179 arg2 = next_const_call_expr_arg (&iter2))
7181 cmp = simple_cst_equal (arg1, arg2);
7182 if (cmp <= 0)
7183 return cmp;
7185 return arg1 == arg2;
7188 case TARGET_EXPR:
7189 /* Special case: if either target is an unallocated VAR_DECL,
7190 it means that it's going to be unified with whatever the
7191 TARGET_EXPR is really supposed to initialize, so treat it
7192 as being equivalent to anything. */
7193 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
7194 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
7195 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
7196 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
7197 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
7198 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
7199 cmp = 1;
7200 else
7201 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7203 if (cmp <= 0)
7204 return cmp;
7206 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
7208 case WITH_CLEANUP_EXPR:
7209 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7210 if (cmp <= 0)
7211 return cmp;
7213 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
7215 case COMPONENT_REF:
7216 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
7217 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7219 return 0;
7221 case VAR_DECL:
7222 case PARM_DECL:
7223 case CONST_DECL:
7224 case FUNCTION_DECL:
7225 return 0;
7227 default:
7228 break;
7231 /* This general rule works for most tree codes. All exceptions should be
7232 handled above. If this is a language-specific tree code, we can't
7233 trust what might be in the operand, so say we don't know
7234 the situation. */
7235 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
7236 return -1;
7238 switch (TREE_CODE_CLASS (code1))
7240 case tcc_unary:
7241 case tcc_binary:
7242 case tcc_comparison:
7243 case tcc_expression:
7244 case tcc_reference:
7245 case tcc_statement:
7246 cmp = 1;
7247 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
7249 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
7250 if (cmp <= 0)
7251 return cmp;
7254 return cmp;
7256 default:
7257 return -1;
7261 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
7262 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
7263 than U, respectively. */
7266 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
7268 if (tree_int_cst_sgn (t) < 0)
7269 return -1;
7270 else if (TREE_INT_CST_HIGH (t) != 0)
7271 return 1;
7272 else if (TREE_INT_CST_LOW (t) == u)
7273 return 0;
7274 else if (TREE_INT_CST_LOW (t) < u)
7275 return -1;
7276 else
7277 return 1;
7280 /* Return true if SIZE represents a constant size that is in bounds of
7281 what the middle-end and the backend accepts (covering not more than
7282 half of the address-space). */
7284 bool
7285 valid_constant_size_p (const_tree size)
7287 if (! tree_fits_uhwi_p (size)
7288 || TREE_OVERFLOW (size)
7289 || tree_int_cst_sign_bit (size) != 0)
7290 return false;
7291 return true;
7294 /* Return the precision of the type, or for a complex or vector type the
7295 precision of the type of its elements. */
7297 unsigned int
7298 element_precision (const_tree type)
7300 enum tree_code code = TREE_CODE (type);
7301 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
7302 type = TREE_TYPE (type);
7304 return TYPE_PRECISION (type);
7307 /* Return true if CODE represents an associative tree code. Otherwise
7308 return false. */
7309 bool
7310 associative_tree_code (enum tree_code code)
7312 switch (code)
7314 case BIT_IOR_EXPR:
7315 case BIT_AND_EXPR:
7316 case BIT_XOR_EXPR:
7317 case PLUS_EXPR:
7318 case MULT_EXPR:
7319 case MIN_EXPR:
7320 case MAX_EXPR:
7321 return true;
7323 default:
7324 break;
7326 return false;
7329 /* Return true if CODE represents a commutative tree code. Otherwise
7330 return false. */
7331 bool
7332 commutative_tree_code (enum tree_code code)
7334 switch (code)
7336 case PLUS_EXPR:
7337 case MULT_EXPR:
7338 case MULT_HIGHPART_EXPR:
7339 case MIN_EXPR:
7340 case MAX_EXPR:
7341 case BIT_IOR_EXPR:
7342 case BIT_XOR_EXPR:
7343 case BIT_AND_EXPR:
7344 case NE_EXPR:
7345 case EQ_EXPR:
7346 case UNORDERED_EXPR:
7347 case ORDERED_EXPR:
7348 case UNEQ_EXPR:
7349 case LTGT_EXPR:
7350 case TRUTH_AND_EXPR:
7351 case TRUTH_XOR_EXPR:
7352 case TRUTH_OR_EXPR:
7353 case WIDEN_MULT_EXPR:
7354 case VEC_WIDEN_MULT_HI_EXPR:
7355 case VEC_WIDEN_MULT_LO_EXPR:
7356 case VEC_WIDEN_MULT_EVEN_EXPR:
7357 case VEC_WIDEN_MULT_ODD_EXPR:
7358 return true;
7360 default:
7361 break;
7363 return false;
7366 /* Return true if CODE represents a ternary tree code for which the
7367 first two operands are commutative. Otherwise return false. */
7368 bool
7369 commutative_ternary_tree_code (enum tree_code code)
7371 switch (code)
7373 case WIDEN_MULT_PLUS_EXPR:
7374 case WIDEN_MULT_MINUS_EXPR:
7375 return true;
7377 default:
7378 break;
7380 return false;
7383 /* Generate a hash value for an expression. This can be used iteratively
7384 by passing a previous result as the VAL argument.
7386 This function is intended to produce the same hash for expressions which
7387 would compare equal using operand_equal_p. */
7389 hashval_t
7390 iterative_hash_expr (const_tree t, hashval_t val)
7392 int i;
7393 enum tree_code code;
7394 char tclass;
7396 if (t == NULL_TREE)
7397 return iterative_hash_hashval_t (0, val);
7399 code = TREE_CODE (t);
7401 switch (code)
7403 /* Alas, constants aren't shared, so we can't rely on pointer
7404 identity. */
7405 case INTEGER_CST:
7406 val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
7407 return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
7408 case REAL_CST:
7410 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7412 return iterative_hash_hashval_t (val2, val);
7414 case FIXED_CST:
7416 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7418 return iterative_hash_hashval_t (val2, val);
7420 case STRING_CST:
7421 return iterative_hash (TREE_STRING_POINTER (t),
7422 TREE_STRING_LENGTH (t), val);
7423 case COMPLEX_CST:
7424 val = iterative_hash_expr (TREE_REALPART (t), val);
7425 return iterative_hash_expr (TREE_IMAGPART (t), val);
7426 case VECTOR_CST:
7428 unsigned i;
7429 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7430 val = iterative_hash_expr (VECTOR_CST_ELT (t, i), val);
7431 return val;
7433 case SSA_NAME:
7434 /* We can just compare by pointer. */
7435 return iterative_hash_host_wide_int (SSA_NAME_VERSION (t), val);
7436 case PLACEHOLDER_EXPR:
7437 /* The node itself doesn't matter. */
7438 return val;
7439 case TREE_LIST:
7440 /* A list of expressions, for a CALL_EXPR or as the elements of a
7441 VECTOR_CST. */
7442 for (; t; t = TREE_CHAIN (t))
7443 val = iterative_hash_expr (TREE_VALUE (t), val);
7444 return val;
7445 case CONSTRUCTOR:
7447 unsigned HOST_WIDE_INT idx;
7448 tree field, value;
7449 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7451 val = iterative_hash_expr (field, val);
7452 val = iterative_hash_expr (value, val);
7454 return val;
7456 case FUNCTION_DECL:
7457 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7458 Otherwise nodes that compare equal according to operand_equal_p might
7459 get different hash codes. However, don't do this for machine specific
7460 or front end builtins, since the function code is overloaded in those
7461 cases. */
7462 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7463 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7465 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7466 code = TREE_CODE (t);
7468 /* FALL THROUGH */
7469 default:
7470 tclass = TREE_CODE_CLASS (code);
7472 if (tclass == tcc_declaration)
7474 /* DECL's have a unique ID */
7475 val = iterative_hash_host_wide_int (DECL_UID (t), val);
7477 else
7479 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7481 val = iterative_hash_object (code, val);
7483 /* Don't hash the type, that can lead to having nodes which
7484 compare equal according to operand_equal_p, but which
7485 have different hash codes. */
7486 if (CONVERT_EXPR_CODE_P (code)
7487 || code == NON_LVALUE_EXPR)
7489 /* Make sure to include signness in the hash computation. */
7490 val += TYPE_UNSIGNED (TREE_TYPE (t));
7491 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7494 else if (commutative_tree_code (code))
7496 /* It's a commutative expression. We want to hash it the same
7497 however it appears. We do this by first hashing both operands
7498 and then rehashing based on the order of their independent
7499 hashes. */
7500 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
7501 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
7502 hashval_t t;
7504 if (one > two)
7505 t = one, one = two, two = t;
7507 val = iterative_hash_hashval_t (one, val);
7508 val = iterative_hash_hashval_t (two, val);
7510 else
7511 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7512 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
7514 return val;
7518 /* Constructors for pointer, array and function types.
7519 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7520 constructed by language-dependent code, not here.) */
7522 /* Construct, lay out and return the type of pointers to TO_TYPE with
7523 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7524 reference all of memory. If such a type has already been
7525 constructed, reuse it. */
7527 tree
7528 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
7529 bool can_alias_all)
7531 tree t;
7533 if (to_type == error_mark_node)
7534 return error_mark_node;
7536 /* If the pointed-to type has the may_alias attribute set, force
7537 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7538 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7539 can_alias_all = true;
7541 /* In some cases, languages will have things that aren't a POINTER_TYPE
7542 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7543 In that case, return that type without regard to the rest of our
7544 operands.
7546 ??? This is a kludge, but consistent with the way this function has
7547 always operated and there doesn't seem to be a good way to avoid this
7548 at the moment. */
7549 if (TYPE_POINTER_TO (to_type) != 0
7550 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7551 return TYPE_POINTER_TO (to_type);
7553 /* First, if we already have a type for pointers to TO_TYPE and it's
7554 the proper mode, use it. */
7555 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7556 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7557 return t;
7559 t = make_node (POINTER_TYPE);
7561 TREE_TYPE (t) = to_type;
7562 SET_TYPE_MODE (t, mode);
7563 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7564 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7565 TYPE_POINTER_TO (to_type) = t;
7567 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7568 SET_TYPE_STRUCTURAL_EQUALITY (t);
7569 else if (TYPE_CANONICAL (to_type) != to_type)
7570 TYPE_CANONICAL (t)
7571 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7572 mode, can_alias_all);
7574 /* Lay out the type. This function has many callers that are concerned
7575 with expression-construction, and this simplifies them all. */
7576 layout_type (t);
7578 return t;
7581 /* By default build pointers in ptr_mode. */
7583 tree
7584 build_pointer_type (tree to_type)
7586 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7587 : TYPE_ADDR_SPACE (to_type);
7588 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7589 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7592 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7594 tree
7595 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
7596 bool can_alias_all)
7598 tree t;
7600 if (to_type == error_mark_node)
7601 return error_mark_node;
7603 /* If the pointed-to type has the may_alias attribute set, force
7604 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7605 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7606 can_alias_all = true;
7608 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7609 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7610 In that case, return that type without regard to the rest of our
7611 operands.
7613 ??? This is a kludge, but consistent with the way this function has
7614 always operated and there doesn't seem to be a good way to avoid this
7615 at the moment. */
7616 if (TYPE_REFERENCE_TO (to_type) != 0
7617 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7618 return TYPE_REFERENCE_TO (to_type);
7620 /* First, if we already have a type for pointers to TO_TYPE and it's
7621 the proper mode, use it. */
7622 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7623 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7624 return t;
7626 t = make_node (REFERENCE_TYPE);
7628 TREE_TYPE (t) = to_type;
7629 SET_TYPE_MODE (t, mode);
7630 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7631 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7632 TYPE_REFERENCE_TO (to_type) = t;
7634 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7635 SET_TYPE_STRUCTURAL_EQUALITY (t);
7636 else if (TYPE_CANONICAL (to_type) != to_type)
7637 TYPE_CANONICAL (t)
7638 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7639 mode, can_alias_all);
7641 layout_type (t);
7643 return t;
7647 /* Build the node for the type of references-to-TO_TYPE by default
7648 in ptr_mode. */
7650 tree
7651 build_reference_type (tree to_type)
7653 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7654 : TYPE_ADDR_SPACE (to_type);
7655 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7656 return build_reference_type_for_mode (to_type, pointer_mode, false);
7659 #define MAX_INT_CACHED_PREC \
7660 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7661 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7663 /* Builds a signed or unsigned integer type of precision PRECISION.
7664 Used for C bitfields whose precision does not match that of
7665 built-in target types. */
7666 tree
7667 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7668 int unsignedp)
7670 tree itype, ret;
7672 if (unsignedp)
7673 unsignedp = MAX_INT_CACHED_PREC + 1;
7675 if (precision <= MAX_INT_CACHED_PREC)
7677 itype = nonstandard_integer_type_cache[precision + unsignedp];
7678 if (itype)
7679 return itype;
7682 itype = make_node (INTEGER_TYPE);
7683 TYPE_PRECISION (itype) = precision;
7685 if (unsignedp)
7686 fixup_unsigned_type (itype);
7687 else
7688 fixup_signed_type (itype);
7690 ret = itype;
7691 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (itype)))
7692 ret = type_hash_canon (tree_to_uhwi (TYPE_MAX_VALUE (itype)), itype);
7693 if (precision <= MAX_INT_CACHED_PREC)
7694 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7696 return ret;
7699 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7700 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7701 is true, reuse such a type that has already been constructed. */
7703 static tree
7704 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7706 tree itype = make_node (INTEGER_TYPE);
7707 hashval_t hashcode = 0;
7709 TREE_TYPE (itype) = type;
7711 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7712 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7714 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7715 SET_TYPE_MODE (itype, TYPE_MODE (type));
7716 TYPE_SIZE (itype) = TYPE_SIZE (type);
7717 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7718 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7719 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7721 if (!shared)
7722 return itype;
7724 if ((TYPE_MIN_VALUE (itype)
7725 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7726 || (TYPE_MAX_VALUE (itype)
7727 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7729 /* Since we cannot reliably merge this type, we need to compare it using
7730 structural equality checks. */
7731 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7732 return itype;
7735 hashcode = iterative_hash_expr (TYPE_MIN_VALUE (itype), hashcode);
7736 hashcode = iterative_hash_expr (TYPE_MAX_VALUE (itype), hashcode);
7737 hashcode = iterative_hash_hashval_t (TYPE_HASH (type), hashcode);
7738 itype = type_hash_canon (hashcode, itype);
7740 return itype;
7743 /* Wrapper around build_range_type_1 with SHARED set to true. */
7745 tree
7746 build_range_type (tree type, tree lowval, tree highval)
7748 return build_range_type_1 (type, lowval, highval, true);
7751 /* Wrapper around build_range_type_1 with SHARED set to false. */
7753 tree
7754 build_nonshared_range_type (tree type, tree lowval, tree highval)
7756 return build_range_type_1 (type, lowval, highval, false);
7759 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7760 MAXVAL should be the maximum value in the domain
7761 (one less than the length of the array).
7763 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7764 We don't enforce this limit, that is up to caller (e.g. language front end).
7765 The limit exists because the result is a signed type and we don't handle
7766 sizes that use more than one HOST_WIDE_INT. */
7768 tree
7769 build_index_type (tree maxval)
7771 return build_range_type (sizetype, size_zero_node, maxval);
7774 /* Return true if the debug information for TYPE, a subtype, should be emitted
7775 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7776 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7777 debug info and doesn't reflect the source code. */
7779 bool
7780 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7782 tree base_type = TREE_TYPE (type), low, high;
7784 /* Subrange types have a base type which is an integral type. */
7785 if (!INTEGRAL_TYPE_P (base_type))
7786 return false;
7788 /* Get the real bounds of the subtype. */
7789 if (lang_hooks.types.get_subrange_bounds)
7790 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7791 else
7793 low = TYPE_MIN_VALUE (type);
7794 high = TYPE_MAX_VALUE (type);
7797 /* If the type and its base type have the same representation and the same
7798 name, then the type is not a subrange but a copy of the base type. */
7799 if ((TREE_CODE (base_type) == INTEGER_TYPE
7800 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7801 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7802 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7803 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type)))
7805 tree type_name = TYPE_NAME (type);
7806 tree base_type_name = TYPE_NAME (base_type);
7808 if (type_name && TREE_CODE (type_name) == TYPE_DECL)
7809 type_name = DECL_NAME (type_name);
7811 if (base_type_name && TREE_CODE (base_type_name) == TYPE_DECL)
7812 base_type_name = DECL_NAME (base_type_name);
7814 if (type_name == base_type_name)
7815 return false;
7818 if (lowval)
7819 *lowval = low;
7820 if (highval)
7821 *highval = high;
7822 return true;
7825 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7826 and number of elements specified by the range of values of INDEX_TYPE.
7827 If SHARED is true, reuse such a type that has already been constructed. */
7829 static tree
7830 build_array_type_1 (tree elt_type, tree index_type, bool shared)
7832 tree t;
7834 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7836 error ("arrays of functions are not meaningful");
7837 elt_type = integer_type_node;
7840 t = make_node (ARRAY_TYPE);
7841 TREE_TYPE (t) = elt_type;
7842 TYPE_DOMAIN (t) = index_type;
7843 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7844 layout_type (t);
7846 /* If the element type is incomplete at this point we get marked for
7847 structural equality. Do not record these types in the canonical
7848 type hashtable. */
7849 if (TYPE_STRUCTURAL_EQUALITY_P (t))
7850 return t;
7852 if (shared)
7854 hashval_t hashcode = iterative_hash_object (TYPE_HASH (elt_type), 0);
7855 if (index_type)
7856 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
7857 t = type_hash_canon (hashcode, t);
7860 if (TYPE_CANONICAL (t) == t)
7862 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7863 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7864 SET_TYPE_STRUCTURAL_EQUALITY (t);
7865 else if (TYPE_CANONICAL (elt_type) != elt_type
7866 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7867 TYPE_CANONICAL (t)
7868 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7869 index_type
7870 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7871 shared);
7874 return t;
7877 /* Wrapper around build_array_type_1 with SHARED set to true. */
7879 tree
7880 build_array_type (tree elt_type, tree index_type)
7882 return build_array_type_1 (elt_type, index_type, true);
7885 /* Wrapper around build_array_type_1 with SHARED set to false. */
7887 tree
7888 build_nonshared_array_type (tree elt_type, tree index_type)
7890 return build_array_type_1 (elt_type, index_type, false);
7893 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7894 sizetype. */
7896 tree
7897 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
7899 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7902 /* Recursively examines the array elements of TYPE, until a non-array
7903 element type is found. */
7905 tree
7906 strip_array_types (tree type)
7908 while (TREE_CODE (type) == ARRAY_TYPE)
7909 type = TREE_TYPE (type);
7911 return type;
7914 /* Computes the canonical argument types from the argument type list
7915 ARGTYPES.
7917 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7918 on entry to this function, or if any of the ARGTYPES are
7919 structural.
7921 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7922 true on entry to this function, or if any of the ARGTYPES are
7923 non-canonical.
7925 Returns a canonical argument list, which may be ARGTYPES when the
7926 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7927 true) or would not differ from ARGTYPES. */
7929 static tree
7930 maybe_canonicalize_argtypes (tree argtypes,
7931 bool *any_structural_p,
7932 bool *any_noncanonical_p)
7934 tree arg;
7935 bool any_noncanonical_argtypes_p = false;
7937 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7939 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7940 /* Fail gracefully by stating that the type is structural. */
7941 *any_structural_p = true;
7942 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7943 *any_structural_p = true;
7944 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7945 || TREE_PURPOSE (arg))
7946 /* If the argument has a default argument, we consider it
7947 non-canonical even though the type itself is canonical.
7948 That way, different variants of function and method types
7949 with default arguments will all point to the variant with
7950 no defaults as their canonical type. */
7951 any_noncanonical_argtypes_p = true;
7954 if (*any_structural_p)
7955 return argtypes;
7957 if (any_noncanonical_argtypes_p)
7959 /* Build the canonical list of argument types. */
7960 tree canon_argtypes = NULL_TREE;
7961 bool is_void = false;
7963 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7965 if (arg == void_list_node)
7966 is_void = true;
7967 else
7968 canon_argtypes = tree_cons (NULL_TREE,
7969 TYPE_CANONICAL (TREE_VALUE (arg)),
7970 canon_argtypes);
7973 canon_argtypes = nreverse (canon_argtypes);
7974 if (is_void)
7975 canon_argtypes = chainon (canon_argtypes, void_list_node);
7977 /* There is a non-canonical type. */
7978 *any_noncanonical_p = true;
7979 return canon_argtypes;
7982 /* The canonical argument types are the same as ARGTYPES. */
7983 return argtypes;
7986 /* Construct, lay out and return
7987 the type of functions returning type VALUE_TYPE
7988 given arguments of types ARG_TYPES.
7989 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7990 are data type nodes for the arguments of the function.
7991 If such a type has already been constructed, reuse it. */
7993 tree
7994 build_function_type (tree value_type, tree arg_types)
7996 tree t;
7997 hashval_t hashcode = 0;
7998 bool any_structural_p, any_noncanonical_p;
7999 tree canon_argtypes;
8001 if (TREE_CODE (value_type) == FUNCTION_TYPE)
8003 error ("function return type cannot be function");
8004 value_type = integer_type_node;
8007 /* Make a node of the sort we want. */
8008 t = make_node (FUNCTION_TYPE);
8009 TREE_TYPE (t) = value_type;
8010 TYPE_ARG_TYPES (t) = arg_types;
8012 /* If we already have such a type, use the old one. */
8013 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
8014 hashcode = type_hash_list (arg_types, hashcode);
8015 t = type_hash_canon (hashcode, t);
8017 /* Set up the canonical type. */
8018 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
8019 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
8020 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
8021 &any_structural_p,
8022 &any_noncanonical_p);
8023 if (any_structural_p)
8024 SET_TYPE_STRUCTURAL_EQUALITY (t);
8025 else if (any_noncanonical_p)
8026 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
8027 canon_argtypes);
8029 if (!COMPLETE_TYPE_P (t))
8030 layout_type (t);
8031 return t;
8034 /* Build a function type. The RETURN_TYPE is the type returned by the
8035 function. If VAARGS is set, no void_type_node is appended to the
8036 the list. ARGP must be always be terminated be a NULL_TREE. */
8038 static tree
8039 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
8041 tree t, args, last;
8043 t = va_arg (argp, tree);
8044 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
8045 args = tree_cons (NULL_TREE, t, args);
8047 if (vaargs)
8049 last = args;
8050 if (args != NULL_TREE)
8051 args = nreverse (args);
8052 gcc_assert (last != void_list_node);
8054 else if (args == NULL_TREE)
8055 args = void_list_node;
8056 else
8058 last = args;
8059 args = nreverse (args);
8060 TREE_CHAIN (last) = void_list_node;
8062 args = build_function_type (return_type, args);
8064 return args;
8067 /* Build a function type. The RETURN_TYPE is the type returned by the
8068 function. If additional arguments are provided, they are
8069 additional argument types. The list of argument types must always
8070 be terminated by NULL_TREE. */
8072 tree
8073 build_function_type_list (tree return_type, ...)
8075 tree args;
8076 va_list p;
8078 va_start (p, return_type);
8079 args = build_function_type_list_1 (false, return_type, p);
8080 va_end (p);
8081 return args;
8084 /* Build a variable argument function type. The RETURN_TYPE is the
8085 type returned by the function. If additional arguments are provided,
8086 they are additional argument types. The list of argument types must
8087 always be terminated by NULL_TREE. */
8089 tree
8090 build_varargs_function_type_list (tree return_type, ...)
8092 tree args;
8093 va_list p;
8095 va_start (p, return_type);
8096 args = build_function_type_list_1 (true, return_type, p);
8097 va_end (p);
8099 return args;
8102 /* Build a function type. RETURN_TYPE is the type returned by the
8103 function; VAARGS indicates whether the function takes varargs. The
8104 function takes N named arguments, the types of which are provided in
8105 ARG_TYPES. */
8107 static tree
8108 build_function_type_array_1 (bool vaargs, tree return_type, int n,
8109 tree *arg_types)
8111 int i;
8112 tree t = vaargs ? NULL_TREE : void_list_node;
8114 for (i = n - 1; i >= 0; i--)
8115 t = tree_cons (NULL_TREE, arg_types[i], t);
8117 return build_function_type (return_type, t);
8120 /* Build a function type. RETURN_TYPE is the type returned by the
8121 function. The function takes N named arguments, the types of which
8122 are provided in ARG_TYPES. */
8124 tree
8125 build_function_type_array (tree return_type, int n, tree *arg_types)
8127 return build_function_type_array_1 (false, return_type, n, arg_types);
8130 /* Build a variable argument function type. RETURN_TYPE is the type
8131 returned by the function. The function takes N named arguments, the
8132 types of which are provided in ARG_TYPES. */
8134 tree
8135 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8137 return build_function_type_array_1 (true, return_type, n, arg_types);
8140 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8141 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8142 for the method. An implicit additional parameter (of type
8143 pointer-to-BASETYPE) is added to the ARGTYPES. */
8145 tree
8146 build_method_type_directly (tree basetype,
8147 tree rettype,
8148 tree argtypes)
8150 tree t;
8151 tree ptype;
8152 int hashcode = 0;
8153 bool any_structural_p, any_noncanonical_p;
8154 tree canon_argtypes;
8156 /* Make a node of the sort we want. */
8157 t = make_node (METHOD_TYPE);
8159 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8160 TREE_TYPE (t) = rettype;
8161 ptype = build_pointer_type (basetype);
8163 /* The actual arglist for this function includes a "hidden" argument
8164 which is "this". Put it into the list of argument types. */
8165 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8166 TYPE_ARG_TYPES (t) = argtypes;
8168 /* If we already have such a type, use the old one. */
8169 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8170 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
8171 hashcode = type_hash_list (argtypes, hashcode);
8172 t = type_hash_canon (hashcode, t);
8174 /* Set up the canonical type. */
8175 any_structural_p
8176 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8177 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8178 any_noncanonical_p
8179 = (TYPE_CANONICAL (basetype) != basetype
8180 || TYPE_CANONICAL (rettype) != rettype);
8181 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8182 &any_structural_p,
8183 &any_noncanonical_p);
8184 if (any_structural_p)
8185 SET_TYPE_STRUCTURAL_EQUALITY (t);
8186 else if (any_noncanonical_p)
8187 TYPE_CANONICAL (t)
8188 = build_method_type_directly (TYPE_CANONICAL (basetype),
8189 TYPE_CANONICAL (rettype),
8190 canon_argtypes);
8191 if (!COMPLETE_TYPE_P (t))
8192 layout_type (t);
8194 return t;
8197 /* Construct, lay out and return the type of methods belonging to class
8198 BASETYPE and whose arguments and values are described by TYPE.
8199 If that type exists already, reuse it.
8200 TYPE must be a FUNCTION_TYPE node. */
8202 tree
8203 build_method_type (tree basetype, tree type)
8205 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8207 return build_method_type_directly (basetype,
8208 TREE_TYPE (type),
8209 TYPE_ARG_TYPES (type));
8212 /* Construct, lay out and return the type of offsets to a value
8213 of type TYPE, within an object of type BASETYPE.
8214 If a suitable offset type exists already, reuse it. */
8216 tree
8217 build_offset_type (tree basetype, tree type)
8219 tree t;
8220 hashval_t hashcode = 0;
8222 /* Make a node of the sort we want. */
8223 t = make_node (OFFSET_TYPE);
8225 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8226 TREE_TYPE (t) = type;
8228 /* If we already have such a type, use the old one. */
8229 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8230 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
8231 t = type_hash_canon (hashcode, t);
8233 if (!COMPLETE_TYPE_P (t))
8234 layout_type (t);
8236 if (TYPE_CANONICAL (t) == t)
8238 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8239 || TYPE_STRUCTURAL_EQUALITY_P (type))
8240 SET_TYPE_STRUCTURAL_EQUALITY (t);
8241 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8242 || TYPE_CANONICAL (type) != type)
8243 TYPE_CANONICAL (t)
8244 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8245 TYPE_CANONICAL (type));
8248 return t;
8251 /* Create a complex type whose components are COMPONENT_TYPE. */
8253 tree
8254 build_complex_type (tree component_type)
8256 tree t;
8257 hashval_t hashcode;
8259 gcc_assert (INTEGRAL_TYPE_P (component_type)
8260 || SCALAR_FLOAT_TYPE_P (component_type)
8261 || FIXED_POINT_TYPE_P (component_type));
8263 /* Make a node of the sort we want. */
8264 t = make_node (COMPLEX_TYPE);
8266 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8268 /* If we already have such a type, use the old one. */
8269 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
8270 t = type_hash_canon (hashcode, t);
8272 if (!COMPLETE_TYPE_P (t))
8273 layout_type (t);
8275 if (TYPE_CANONICAL (t) == t)
8277 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8278 SET_TYPE_STRUCTURAL_EQUALITY (t);
8279 else if (TYPE_CANONICAL (component_type) != component_type)
8280 TYPE_CANONICAL (t)
8281 = build_complex_type (TYPE_CANONICAL (component_type));
8284 /* We need to create a name, since complex is a fundamental type. */
8285 if (! TYPE_NAME (t))
8287 const char *name;
8288 if (component_type == char_type_node)
8289 name = "complex char";
8290 else if (component_type == signed_char_type_node)
8291 name = "complex signed char";
8292 else if (component_type == unsigned_char_type_node)
8293 name = "complex unsigned char";
8294 else if (component_type == short_integer_type_node)
8295 name = "complex short int";
8296 else if (component_type == short_unsigned_type_node)
8297 name = "complex short unsigned int";
8298 else if (component_type == integer_type_node)
8299 name = "complex int";
8300 else if (component_type == unsigned_type_node)
8301 name = "complex unsigned int";
8302 else if (component_type == long_integer_type_node)
8303 name = "complex long int";
8304 else if (component_type == long_unsigned_type_node)
8305 name = "complex long unsigned int";
8306 else if (component_type == long_long_integer_type_node)
8307 name = "complex long long int";
8308 else if (component_type == long_long_unsigned_type_node)
8309 name = "complex long long unsigned int";
8310 else
8311 name = 0;
8313 if (name != 0)
8314 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8315 get_identifier (name), t);
8318 return build_qualified_type (t, TYPE_QUALS (component_type));
8321 /* If TYPE is a real or complex floating-point type and the target
8322 does not directly support arithmetic on TYPE then return the wider
8323 type to be used for arithmetic on TYPE. Otherwise, return
8324 NULL_TREE. */
8326 tree
8327 excess_precision_type (tree type)
8329 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8331 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8332 switch (TREE_CODE (type))
8334 case REAL_TYPE:
8335 switch (flt_eval_method)
8337 case 1:
8338 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8339 return double_type_node;
8340 break;
8341 case 2:
8342 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8343 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8344 return long_double_type_node;
8345 break;
8346 default:
8347 gcc_unreachable ();
8349 break;
8350 case COMPLEX_TYPE:
8351 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8352 return NULL_TREE;
8353 switch (flt_eval_method)
8355 case 1:
8356 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8357 return complex_double_type_node;
8358 break;
8359 case 2:
8360 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8361 || (TYPE_MODE (TREE_TYPE (type))
8362 == TYPE_MODE (double_type_node)))
8363 return complex_long_double_type_node;
8364 break;
8365 default:
8366 gcc_unreachable ();
8368 break;
8369 default:
8370 break;
8373 return NULL_TREE;
8376 /* Return OP, stripped of any conversions to wider types as much as is safe.
8377 Converting the value back to OP's type makes a value equivalent to OP.
8379 If FOR_TYPE is nonzero, we return a value which, if converted to
8380 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8382 OP must have integer, real or enumeral type. Pointers are not allowed!
8384 There are some cases where the obvious value we could return
8385 would regenerate to OP if converted to OP's type,
8386 but would not extend like OP to wider types.
8387 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8388 For example, if OP is (unsigned short)(signed char)-1,
8389 we avoid returning (signed char)-1 if FOR_TYPE is int,
8390 even though extending that to an unsigned short would regenerate OP,
8391 since the result of extending (signed char)-1 to (int)
8392 is different from (int) OP. */
8394 tree
8395 get_unwidened (tree op, tree for_type)
8397 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8398 tree type = TREE_TYPE (op);
8399 unsigned final_prec
8400 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8401 int uns
8402 = (for_type != 0 && for_type != type
8403 && final_prec > TYPE_PRECISION (type)
8404 && TYPE_UNSIGNED (type));
8405 tree win = op;
8407 while (CONVERT_EXPR_P (op))
8409 int bitschange;
8411 /* TYPE_PRECISION on vector types has different meaning
8412 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8413 so avoid them here. */
8414 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8415 break;
8417 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8418 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8420 /* Truncations are many-one so cannot be removed.
8421 Unless we are later going to truncate down even farther. */
8422 if (bitschange < 0
8423 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8424 break;
8426 /* See what's inside this conversion. If we decide to strip it,
8427 we will set WIN. */
8428 op = TREE_OPERAND (op, 0);
8430 /* If we have not stripped any zero-extensions (uns is 0),
8431 we can strip any kind of extension.
8432 If we have previously stripped a zero-extension,
8433 only zero-extensions can safely be stripped.
8434 Any extension can be stripped if the bits it would produce
8435 are all going to be discarded later by truncating to FOR_TYPE. */
8437 if (bitschange > 0)
8439 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8440 win = op;
8441 /* TYPE_UNSIGNED says whether this is a zero-extension.
8442 Let's avoid computing it if it does not affect WIN
8443 and if UNS will not be needed again. */
8444 if ((uns
8445 || CONVERT_EXPR_P (op))
8446 && TYPE_UNSIGNED (TREE_TYPE (op)))
8448 uns = 1;
8449 win = op;
8454 /* If we finally reach a constant see if it fits in for_type and
8455 in that case convert it. */
8456 if (for_type
8457 && TREE_CODE (win) == INTEGER_CST
8458 && TREE_TYPE (win) != for_type
8459 && int_fits_type_p (win, for_type))
8460 win = fold_convert (for_type, win);
8462 return win;
8465 /* Return OP or a simpler expression for a narrower value
8466 which can be sign-extended or zero-extended to give back OP.
8467 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8468 or 0 if the value should be sign-extended. */
8470 tree
8471 get_narrower (tree op, int *unsignedp_ptr)
8473 int uns = 0;
8474 int first = 1;
8475 tree win = op;
8476 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8478 while (TREE_CODE (op) == NOP_EXPR)
8480 int bitschange
8481 = (TYPE_PRECISION (TREE_TYPE (op))
8482 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8484 /* Truncations are many-one so cannot be removed. */
8485 if (bitschange < 0)
8486 break;
8488 /* See what's inside this conversion. If we decide to strip it,
8489 we will set WIN. */
8491 if (bitschange > 0)
8493 op = TREE_OPERAND (op, 0);
8494 /* An extension: the outermost one can be stripped,
8495 but remember whether it is zero or sign extension. */
8496 if (first)
8497 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8498 /* Otherwise, if a sign extension has been stripped,
8499 only sign extensions can now be stripped;
8500 if a zero extension has been stripped, only zero-extensions. */
8501 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8502 break;
8503 first = 0;
8505 else /* bitschange == 0 */
8507 /* A change in nominal type can always be stripped, but we must
8508 preserve the unsignedness. */
8509 if (first)
8510 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8511 first = 0;
8512 op = TREE_OPERAND (op, 0);
8513 /* Keep trying to narrow, but don't assign op to win if it
8514 would turn an integral type into something else. */
8515 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8516 continue;
8519 win = op;
8522 if (TREE_CODE (op) == COMPONENT_REF
8523 /* Since type_for_size always gives an integer type. */
8524 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8525 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8526 /* Ensure field is laid out already. */
8527 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8528 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
8530 unsigned HOST_WIDE_INT innerprec
8531 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
8532 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8533 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8534 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8536 /* We can get this structure field in a narrower type that fits it,
8537 but the resulting extension to its nominal type (a fullword type)
8538 must satisfy the same conditions as for other extensions.
8540 Do this only for fields that are aligned (not bit-fields),
8541 because when bit-field insns will be used there is no
8542 advantage in doing this. */
8544 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8545 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8546 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8547 && type != 0)
8549 if (first)
8550 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8551 win = fold_convert (type, op);
8555 *unsignedp_ptr = uns;
8556 return win;
8559 /* Returns true if integer constant C has a value that is permissible
8560 for type TYPE (an INTEGER_TYPE). */
8562 bool
8563 int_fits_type_p (const_tree c, const_tree type)
8565 tree type_low_bound, type_high_bound;
8566 bool ok_for_low_bound, ok_for_high_bound, unsc;
8567 double_int dc, dd;
8569 dc = tree_to_double_int (c);
8570 unsc = TYPE_UNSIGNED (TREE_TYPE (c));
8572 retry:
8573 type_low_bound = TYPE_MIN_VALUE (type);
8574 type_high_bound = TYPE_MAX_VALUE (type);
8576 /* If at least one bound of the type is a constant integer, we can check
8577 ourselves and maybe make a decision. If no such decision is possible, but
8578 this type is a subtype, try checking against that. Otherwise, use
8579 double_int_fits_to_tree_p, which checks against the precision.
8581 Compute the status for each possibly constant bound, and return if we see
8582 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8583 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8584 for "constant known to fit". */
8586 /* Check if c >= type_low_bound. */
8587 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8589 dd = tree_to_double_int (type_low_bound);
8590 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
8592 int c_neg = (!unsc && dc.is_negative ());
8593 int t_neg = (unsc && dd.is_negative ());
8595 if (c_neg && !t_neg)
8596 return false;
8597 if ((c_neg || !t_neg) && dc.ult (dd))
8598 return false;
8600 else if (dc.cmp (dd, unsc) < 0)
8601 return false;
8602 ok_for_low_bound = true;
8604 else
8605 ok_for_low_bound = false;
8607 /* Check if c <= type_high_bound. */
8608 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8610 dd = tree_to_double_int (type_high_bound);
8611 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
8613 int c_neg = (!unsc && dc.is_negative ());
8614 int t_neg = (unsc && dd.is_negative ());
8616 if (t_neg && !c_neg)
8617 return false;
8618 if ((t_neg || !c_neg) && dc.ugt (dd))
8619 return false;
8621 else if (dc.cmp (dd, unsc) > 0)
8622 return false;
8623 ok_for_high_bound = true;
8625 else
8626 ok_for_high_bound = false;
8628 /* If the constant fits both bounds, the result is known. */
8629 if (ok_for_low_bound && ok_for_high_bound)
8630 return true;
8632 /* Perform some generic filtering which may allow making a decision
8633 even if the bounds are not constant. First, negative integers
8634 never fit in unsigned types, */
8635 if (TYPE_UNSIGNED (type) && !unsc && dc.is_negative ())
8636 return false;
8638 /* Second, narrower types always fit in wider ones. */
8639 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8640 return true;
8642 /* Third, unsigned integers with top bit set never fit signed types. */
8643 if (! TYPE_UNSIGNED (type) && unsc)
8645 int prec = GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (c))) - 1;
8646 if (prec < HOST_BITS_PER_WIDE_INT)
8648 if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
8649 return false;
8651 else if (((((unsigned HOST_WIDE_INT) 1)
8652 << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
8653 return false;
8656 /* If we haven't been able to decide at this point, there nothing more we
8657 can check ourselves here. Look at the base type if we have one and it
8658 has the same precision. */
8659 if (TREE_CODE (type) == INTEGER_TYPE
8660 && TREE_TYPE (type) != 0
8661 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8663 type = TREE_TYPE (type);
8664 goto retry;
8667 /* Or to double_int_fits_to_tree_p, if nothing else. */
8668 return double_int_fits_to_tree_p (type, dc);
8671 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8672 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8673 represented (assuming two's-complement arithmetic) within the bit
8674 precision of the type are returned instead. */
8676 void
8677 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8679 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8680 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8681 mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
8682 TYPE_UNSIGNED (type));
8683 else
8685 if (TYPE_UNSIGNED (type))
8686 mpz_set_ui (min, 0);
8687 else
8689 double_int mn;
8690 mn = double_int::mask (TYPE_PRECISION (type) - 1);
8691 mn = (mn + double_int_one).sext (TYPE_PRECISION (type));
8692 mpz_set_double_int (min, mn, false);
8696 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8697 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8698 mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
8699 TYPE_UNSIGNED (type));
8700 else
8702 if (TYPE_UNSIGNED (type))
8703 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)),
8704 true);
8705 else
8706 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type) - 1),
8707 true);
8711 /* Return true if VAR is an automatic variable defined in function FN. */
8713 bool
8714 auto_var_in_fn_p (const_tree var, const_tree fn)
8716 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8717 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8718 || TREE_CODE (var) == PARM_DECL)
8719 && ! TREE_STATIC (var))
8720 || TREE_CODE (var) == LABEL_DECL
8721 || TREE_CODE (var) == RESULT_DECL));
8724 /* Subprogram of following function. Called by walk_tree.
8726 Return *TP if it is an automatic variable or parameter of the
8727 function passed in as DATA. */
8729 static tree
8730 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8732 tree fn = (tree) data;
8734 if (TYPE_P (*tp))
8735 *walk_subtrees = 0;
8737 else if (DECL_P (*tp)
8738 && auto_var_in_fn_p (*tp, fn))
8739 return *tp;
8741 return NULL_TREE;
8744 /* Returns true if T is, contains, or refers to a type with variable
8745 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8746 arguments, but not the return type. If FN is nonzero, only return
8747 true if a modifier of the type or position of FN is a variable or
8748 parameter inside FN.
8750 This concept is more general than that of C99 'variably modified types':
8751 in C99, a struct type is never variably modified because a VLA may not
8752 appear as a structure member. However, in GNU C code like:
8754 struct S { int i[f()]; };
8756 is valid, and other languages may define similar constructs. */
8758 bool
8759 variably_modified_type_p (tree type, tree fn)
8761 tree t;
8763 /* Test if T is either variable (if FN is zero) or an expression containing
8764 a variable in FN. If TYPE isn't gimplified, return true also if
8765 gimplify_one_sizepos would gimplify the expression into a local
8766 variable. */
8767 #define RETURN_TRUE_IF_VAR(T) \
8768 do { tree _t = (T); \
8769 if (_t != NULL_TREE \
8770 && _t != error_mark_node \
8771 && TREE_CODE (_t) != INTEGER_CST \
8772 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8773 && (!fn \
8774 || (!TYPE_SIZES_GIMPLIFIED (type) \
8775 && !is_gimple_sizepos (_t)) \
8776 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8777 return true; } while (0)
8779 if (type == error_mark_node)
8780 return false;
8782 /* If TYPE itself has variable size, it is variably modified. */
8783 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8784 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8786 switch (TREE_CODE (type))
8788 case POINTER_TYPE:
8789 case REFERENCE_TYPE:
8790 case VECTOR_TYPE:
8791 if (variably_modified_type_p (TREE_TYPE (type), fn))
8792 return true;
8793 break;
8795 case FUNCTION_TYPE:
8796 case METHOD_TYPE:
8797 /* If TYPE is a function type, it is variably modified if the
8798 return type is variably modified. */
8799 if (variably_modified_type_p (TREE_TYPE (type), fn))
8800 return true;
8801 break;
8803 case INTEGER_TYPE:
8804 case REAL_TYPE:
8805 case FIXED_POINT_TYPE:
8806 case ENUMERAL_TYPE:
8807 case BOOLEAN_TYPE:
8808 /* Scalar types are variably modified if their end points
8809 aren't constant. */
8810 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8811 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8812 break;
8814 case RECORD_TYPE:
8815 case UNION_TYPE:
8816 case QUAL_UNION_TYPE:
8817 /* We can't see if any of the fields are variably-modified by the
8818 definition we normally use, since that would produce infinite
8819 recursion via pointers. */
8820 /* This is variably modified if some field's type is. */
8821 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8822 if (TREE_CODE (t) == FIELD_DECL)
8824 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8825 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8826 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8828 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8829 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8831 break;
8833 case ARRAY_TYPE:
8834 /* Do not call ourselves to avoid infinite recursion. This is
8835 variably modified if the element type is. */
8836 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8837 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8838 break;
8840 default:
8841 break;
8844 /* The current language may have other cases to check, but in general,
8845 all other types are not variably modified. */
8846 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8848 #undef RETURN_TRUE_IF_VAR
8851 /* Given a DECL or TYPE, return the scope in which it was declared, or
8852 NULL_TREE if there is no containing scope. */
8854 tree
8855 get_containing_scope (const_tree t)
8857 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8860 /* Return the innermost context enclosing DECL that is
8861 a FUNCTION_DECL, or zero if none. */
8863 tree
8864 decl_function_context (const_tree decl)
8866 tree context;
8868 if (TREE_CODE (decl) == ERROR_MARK)
8869 return 0;
8871 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8872 where we look up the function at runtime. Such functions always take
8873 a first argument of type 'pointer to real context'.
8875 C++ should really be fixed to use DECL_CONTEXT for the real context,
8876 and use something else for the "virtual context". */
8877 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8878 context
8879 = TYPE_MAIN_VARIANT
8880 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8881 else
8882 context = DECL_CONTEXT (decl);
8884 while (context && TREE_CODE (context) != FUNCTION_DECL)
8886 if (TREE_CODE (context) == BLOCK)
8887 context = BLOCK_SUPERCONTEXT (context);
8888 else
8889 context = get_containing_scope (context);
8892 return context;
8895 /* Return the innermost context enclosing DECL that is
8896 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8897 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8899 tree
8900 decl_type_context (const_tree decl)
8902 tree context = DECL_CONTEXT (decl);
8904 while (context)
8905 switch (TREE_CODE (context))
8907 case NAMESPACE_DECL:
8908 case TRANSLATION_UNIT_DECL:
8909 return NULL_TREE;
8911 case RECORD_TYPE:
8912 case UNION_TYPE:
8913 case QUAL_UNION_TYPE:
8914 return context;
8916 case TYPE_DECL:
8917 case FUNCTION_DECL:
8918 context = DECL_CONTEXT (context);
8919 break;
8921 case BLOCK:
8922 context = BLOCK_SUPERCONTEXT (context);
8923 break;
8925 default:
8926 gcc_unreachable ();
8929 return NULL_TREE;
8932 /* CALL is a CALL_EXPR. Return the declaration for the function
8933 called, or NULL_TREE if the called function cannot be
8934 determined. */
8936 tree
8937 get_callee_fndecl (const_tree call)
8939 tree addr;
8941 if (call == error_mark_node)
8942 return error_mark_node;
8944 /* It's invalid to call this function with anything but a
8945 CALL_EXPR. */
8946 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8948 /* The first operand to the CALL is the address of the function
8949 called. */
8950 addr = CALL_EXPR_FN (call);
8952 STRIP_NOPS (addr);
8954 /* If this is a readonly function pointer, extract its initial value. */
8955 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8956 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8957 && DECL_INITIAL (addr))
8958 addr = DECL_INITIAL (addr);
8960 /* If the address is just `&f' for some function `f', then we know
8961 that `f' is being called. */
8962 if (TREE_CODE (addr) == ADDR_EXPR
8963 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8964 return TREE_OPERAND (addr, 0);
8966 /* We couldn't figure out what was being called. */
8967 return NULL_TREE;
8970 /* Print debugging information about tree nodes generated during the compile,
8971 and any language-specific information. */
8973 void
8974 dump_tree_statistics (void)
8976 if (GATHER_STATISTICS)
8978 int i;
8979 int total_nodes, total_bytes;
8980 fprintf (stderr, "Kind Nodes Bytes\n");
8981 fprintf (stderr, "---------------------------------------\n");
8982 total_nodes = total_bytes = 0;
8983 for (i = 0; i < (int) all_kinds; i++)
8985 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
8986 tree_node_counts[i], tree_node_sizes[i]);
8987 total_nodes += tree_node_counts[i];
8988 total_bytes += tree_node_sizes[i];
8990 fprintf (stderr, "---------------------------------------\n");
8991 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
8992 fprintf (stderr, "---------------------------------------\n");
8993 fprintf (stderr, "Code Nodes\n");
8994 fprintf (stderr, "----------------------------\n");
8995 for (i = 0; i < (int) MAX_TREE_CODES; i++)
8996 fprintf (stderr, "%-20s %7d\n", get_tree_code_name ((enum tree_code) i),
8997 tree_code_counts[i]);
8998 fprintf (stderr, "----------------------------\n");
8999 ssanames_print_statistics ();
9000 phinodes_print_statistics ();
9002 else
9003 fprintf (stderr, "(No per-node statistics)\n");
9005 print_type_hash_statistics ();
9006 print_debug_expr_statistics ();
9007 print_value_expr_statistics ();
9008 lang_hooks.print_statistics ();
9011 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
9013 /* Generate a crc32 of a byte. */
9015 static unsigned
9016 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
9018 unsigned ix;
9020 for (ix = bits; ix--; value <<= 1)
9022 unsigned feedback;
9024 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
9025 chksum <<= 1;
9026 chksum ^= feedback;
9028 return chksum;
9031 /* Generate a crc32 of a 32-bit unsigned. */
9033 unsigned
9034 crc32_unsigned (unsigned chksum, unsigned value)
9036 return crc32_unsigned_bits (chksum, value, 32);
9039 /* Generate a crc32 of a byte. */
9041 unsigned
9042 crc32_byte (unsigned chksum, char byte)
9044 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
9047 /* Generate a crc32 of a string. */
9049 unsigned
9050 crc32_string (unsigned chksum, const char *string)
9054 chksum = crc32_byte (chksum, *string);
9056 while (*string++);
9057 return chksum;
9060 /* P is a string that will be used in a symbol. Mask out any characters
9061 that are not valid in that context. */
9063 void
9064 clean_symbol_name (char *p)
9066 for (; *p; p++)
9067 if (! (ISALNUM (*p)
9068 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
9069 || *p == '$'
9070 #endif
9071 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
9072 || *p == '.'
9073 #endif
9075 *p = '_';
9078 /* Generate a name for a special-purpose function.
9079 The generated name may need to be unique across the whole link.
9080 Changes to this function may also require corresponding changes to
9081 xstrdup_mask_random.
9082 TYPE is some string to identify the purpose of this function to the
9083 linker or collect2; it must start with an uppercase letter,
9084 one of:
9085 I - for constructors
9086 D - for destructors
9087 N - for C++ anonymous namespaces
9088 F - for DWARF unwind frame information. */
9090 tree
9091 get_file_function_name (const char *type)
9093 char *buf;
9094 const char *p;
9095 char *q;
9097 /* If we already have a name we know to be unique, just use that. */
9098 if (first_global_object_name)
9099 p = q = ASTRDUP (first_global_object_name);
9100 /* If the target is handling the constructors/destructors, they
9101 will be local to this file and the name is only necessary for
9102 debugging purposes.
9103 We also assign sub_I and sub_D sufixes to constructors called from
9104 the global static constructors. These are always local. */
9105 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9106 || (strncmp (type, "sub_", 4) == 0
9107 && (type[4] == 'I' || type[4] == 'D')))
9109 const char *file = main_input_filename;
9110 if (! file)
9111 file = LOCATION_FILE (input_location);
9112 /* Just use the file's basename, because the full pathname
9113 might be quite long. */
9114 p = q = ASTRDUP (lbasename (file));
9116 else
9118 /* Otherwise, the name must be unique across the entire link.
9119 We don't have anything that we know to be unique to this translation
9120 unit, so use what we do have and throw in some randomness. */
9121 unsigned len;
9122 const char *name = weak_global_object_name;
9123 const char *file = main_input_filename;
9125 if (! name)
9126 name = "";
9127 if (! file)
9128 file = LOCATION_FILE (input_location);
9130 len = strlen (file);
9131 q = (char *) alloca (9 + 17 + len + 1);
9132 memcpy (q, file, len + 1);
9134 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9135 crc32_string (0, name), get_random_seed (false));
9137 p = q;
9140 clean_symbol_name (q);
9141 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9142 + strlen (type));
9144 /* Set up the name of the file-level functions we may need.
9145 Use a global object (which is already required to be unique over
9146 the program) rather than the file name (which imposes extra
9147 constraints). */
9148 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9150 return get_identifier (buf);
9153 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9155 /* Complain that the tree code of NODE does not match the expected 0
9156 terminated list of trailing codes. The trailing code list can be
9157 empty, for a more vague error message. FILE, LINE, and FUNCTION
9158 are of the caller. */
9160 void
9161 tree_check_failed (const_tree node, const char *file,
9162 int line, const char *function, ...)
9164 va_list args;
9165 const char *buffer;
9166 unsigned length = 0;
9167 enum tree_code code;
9169 va_start (args, function);
9170 while ((code = (enum tree_code) va_arg (args, int)))
9171 length += 4 + strlen (get_tree_code_name (code));
9172 va_end (args);
9173 if (length)
9175 char *tmp;
9176 va_start (args, function);
9177 length += strlen ("expected ");
9178 buffer = tmp = (char *) alloca (length);
9179 length = 0;
9180 while ((code = (enum tree_code) va_arg (args, int)))
9182 const char *prefix = length ? " or " : "expected ";
9184 strcpy (tmp + length, prefix);
9185 length += strlen (prefix);
9186 strcpy (tmp + length, get_tree_code_name (code));
9187 length += strlen (get_tree_code_name (code));
9189 va_end (args);
9191 else
9192 buffer = "unexpected node";
9194 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9195 buffer, get_tree_code_name (TREE_CODE (node)),
9196 function, trim_filename (file), line);
9199 /* Complain that the tree code of NODE does match the expected 0
9200 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9201 the caller. */
9203 void
9204 tree_not_check_failed (const_tree node, const char *file,
9205 int line, const char *function, ...)
9207 va_list args;
9208 char *buffer;
9209 unsigned length = 0;
9210 enum tree_code code;
9212 va_start (args, function);
9213 while ((code = (enum tree_code) va_arg (args, int)))
9214 length += 4 + strlen (get_tree_code_name (code));
9215 va_end (args);
9216 va_start (args, function);
9217 buffer = (char *) alloca (length);
9218 length = 0;
9219 while ((code = (enum tree_code) va_arg (args, int)))
9221 if (length)
9223 strcpy (buffer + length, " or ");
9224 length += 4;
9226 strcpy (buffer + length, get_tree_code_name (code));
9227 length += strlen (get_tree_code_name (code));
9229 va_end (args);
9231 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9232 buffer, get_tree_code_name (TREE_CODE (node)),
9233 function, trim_filename (file), line);
9236 /* Similar to tree_check_failed, except that we check for a class of tree
9237 code, given in CL. */
9239 void
9240 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9241 const char *file, int line, const char *function)
9243 internal_error
9244 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9245 TREE_CODE_CLASS_STRING (cl),
9246 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9247 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9250 /* Similar to tree_check_failed, except that instead of specifying a
9251 dozen codes, use the knowledge that they're all sequential. */
9253 void
9254 tree_range_check_failed (const_tree node, const char *file, int line,
9255 const char *function, enum tree_code c1,
9256 enum tree_code c2)
9258 char *buffer;
9259 unsigned length = 0;
9260 unsigned int c;
9262 for (c = c1; c <= c2; ++c)
9263 length += 4 + strlen (get_tree_code_name ((enum tree_code) c));
9265 length += strlen ("expected ");
9266 buffer = (char *) alloca (length);
9267 length = 0;
9269 for (c = c1; c <= c2; ++c)
9271 const char *prefix = length ? " or " : "expected ";
9273 strcpy (buffer + length, prefix);
9274 length += strlen (prefix);
9275 strcpy (buffer + length, get_tree_code_name ((enum tree_code) c));
9276 length += strlen (get_tree_code_name ((enum tree_code) c));
9279 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9280 buffer, get_tree_code_name (TREE_CODE (node)),
9281 function, trim_filename (file), line);
9285 /* Similar to tree_check_failed, except that we check that a tree does
9286 not have the specified code, given in CL. */
9288 void
9289 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9290 const char *file, int line, const char *function)
9292 internal_error
9293 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9294 TREE_CODE_CLASS_STRING (cl),
9295 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9296 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9300 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9302 void
9303 omp_clause_check_failed (const_tree node, const char *file, int line,
9304 const char *function, enum omp_clause_code code)
9306 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9307 omp_clause_code_name[code], get_tree_code_name (TREE_CODE (node)),
9308 function, trim_filename (file), line);
9312 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9314 void
9315 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9316 const char *function, enum omp_clause_code c1,
9317 enum omp_clause_code c2)
9319 char *buffer;
9320 unsigned length = 0;
9321 unsigned int c;
9323 for (c = c1; c <= c2; ++c)
9324 length += 4 + strlen (omp_clause_code_name[c]);
9326 length += strlen ("expected ");
9327 buffer = (char *) alloca (length);
9328 length = 0;
9330 for (c = c1; c <= c2; ++c)
9332 const char *prefix = length ? " or " : "expected ";
9334 strcpy (buffer + length, prefix);
9335 length += strlen (prefix);
9336 strcpy (buffer + length, omp_clause_code_name[c]);
9337 length += strlen (omp_clause_code_name[c]);
9340 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9341 buffer, omp_clause_code_name[TREE_CODE (node)],
9342 function, trim_filename (file), line);
9346 #undef DEFTREESTRUCT
9347 #define DEFTREESTRUCT(VAL, NAME) NAME,
9349 static const char *ts_enum_names[] = {
9350 #include "treestruct.def"
9352 #undef DEFTREESTRUCT
9354 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9356 /* Similar to tree_class_check_failed, except that we check for
9357 whether CODE contains the tree structure identified by EN. */
9359 void
9360 tree_contains_struct_check_failed (const_tree node,
9361 const enum tree_node_structure_enum en,
9362 const char *file, int line,
9363 const char *function)
9365 internal_error
9366 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9367 TS_ENUM_NAME (en),
9368 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9372 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9373 (dynamically sized) vector. */
9375 void
9376 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9377 const char *function)
9379 internal_error
9380 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9381 idx + 1, len, function, trim_filename (file), line);
9384 /* Similar to above, except that the check is for the bounds of the operand
9385 vector of an expression node EXP. */
9387 void
9388 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9389 int line, const char *function)
9391 enum tree_code code = TREE_CODE (exp);
9392 internal_error
9393 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9394 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9395 function, trim_filename (file), line);
9398 /* Similar to above, except that the check is for the number of
9399 operands of an OMP_CLAUSE node. */
9401 void
9402 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9403 int line, const char *function)
9405 internal_error
9406 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9407 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9408 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9409 trim_filename (file), line);
9411 #endif /* ENABLE_TREE_CHECKING */
9413 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9414 and mapped to the machine mode MODE. Initialize its fields and build
9415 the information necessary for debugging output. */
9417 static tree
9418 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
9420 tree t;
9421 hashval_t hashcode = 0;
9423 t = make_node (VECTOR_TYPE);
9424 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9425 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9426 SET_TYPE_MODE (t, mode);
9428 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9429 SET_TYPE_STRUCTURAL_EQUALITY (t);
9430 else if (TYPE_CANONICAL (innertype) != innertype
9431 || mode != VOIDmode)
9432 TYPE_CANONICAL (t)
9433 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9435 layout_type (t);
9437 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
9438 hashcode = iterative_hash_host_wide_int (nunits, hashcode);
9439 hashcode = iterative_hash_host_wide_int (mode, hashcode);
9440 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (t)), hashcode);
9441 t = type_hash_canon (hashcode, t);
9443 /* We have built a main variant, based on the main variant of the
9444 inner type. Use it to build the variant we return. */
9445 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9446 && TREE_TYPE (t) != innertype)
9447 return build_type_attribute_qual_variant (t,
9448 TYPE_ATTRIBUTES (innertype),
9449 TYPE_QUALS (innertype));
9451 return t;
9454 static tree
9455 make_or_reuse_type (unsigned size, int unsignedp)
9457 if (size == INT_TYPE_SIZE)
9458 return unsignedp ? unsigned_type_node : integer_type_node;
9459 if (size == CHAR_TYPE_SIZE)
9460 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9461 if (size == SHORT_TYPE_SIZE)
9462 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9463 if (size == LONG_TYPE_SIZE)
9464 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9465 if (size == LONG_LONG_TYPE_SIZE)
9466 return (unsignedp ? long_long_unsigned_type_node
9467 : long_long_integer_type_node);
9468 if (size == 128 && int128_integer_type_node)
9469 return (unsignedp ? int128_unsigned_type_node
9470 : int128_integer_type_node);
9472 if (unsignedp)
9473 return make_unsigned_type (size);
9474 else
9475 return make_signed_type (size);
9478 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9480 static tree
9481 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9483 if (satp)
9485 if (size == SHORT_FRACT_TYPE_SIZE)
9486 return unsignedp ? sat_unsigned_short_fract_type_node
9487 : sat_short_fract_type_node;
9488 if (size == FRACT_TYPE_SIZE)
9489 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9490 if (size == LONG_FRACT_TYPE_SIZE)
9491 return unsignedp ? sat_unsigned_long_fract_type_node
9492 : sat_long_fract_type_node;
9493 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9494 return unsignedp ? sat_unsigned_long_long_fract_type_node
9495 : sat_long_long_fract_type_node;
9497 else
9499 if (size == SHORT_FRACT_TYPE_SIZE)
9500 return unsignedp ? unsigned_short_fract_type_node
9501 : short_fract_type_node;
9502 if (size == FRACT_TYPE_SIZE)
9503 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9504 if (size == LONG_FRACT_TYPE_SIZE)
9505 return unsignedp ? unsigned_long_fract_type_node
9506 : long_fract_type_node;
9507 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9508 return unsignedp ? unsigned_long_long_fract_type_node
9509 : long_long_fract_type_node;
9512 return make_fract_type (size, unsignedp, satp);
9515 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9517 static tree
9518 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9520 if (satp)
9522 if (size == SHORT_ACCUM_TYPE_SIZE)
9523 return unsignedp ? sat_unsigned_short_accum_type_node
9524 : sat_short_accum_type_node;
9525 if (size == ACCUM_TYPE_SIZE)
9526 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9527 if (size == LONG_ACCUM_TYPE_SIZE)
9528 return unsignedp ? sat_unsigned_long_accum_type_node
9529 : sat_long_accum_type_node;
9530 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9531 return unsignedp ? sat_unsigned_long_long_accum_type_node
9532 : sat_long_long_accum_type_node;
9534 else
9536 if (size == SHORT_ACCUM_TYPE_SIZE)
9537 return unsignedp ? unsigned_short_accum_type_node
9538 : short_accum_type_node;
9539 if (size == ACCUM_TYPE_SIZE)
9540 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9541 if (size == LONG_ACCUM_TYPE_SIZE)
9542 return unsignedp ? unsigned_long_accum_type_node
9543 : long_accum_type_node;
9544 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9545 return unsignedp ? unsigned_long_long_accum_type_node
9546 : long_long_accum_type_node;
9549 return make_accum_type (size, unsignedp, satp);
9553 /* Create an atomic variant node for TYPE. This routine is called
9554 during initialization of data types to create the 5 basic atomic
9555 types. The generic build_variant_type function requires these to
9556 already be set up in order to function properly, so cannot be
9557 called from there. If ALIGN is non-zero, then ensure alignment is
9558 overridden to this value. */
9560 static tree
9561 build_atomic_base (tree type, unsigned int align)
9563 tree t;
9565 /* Make sure its not already registered. */
9566 if ((t = get_qualified_type (type, TYPE_QUAL_ATOMIC)))
9567 return t;
9569 t = build_variant_type_copy (type);
9570 set_type_quals (t, TYPE_QUAL_ATOMIC);
9572 if (align)
9573 TYPE_ALIGN (t) = align;
9575 return t;
9578 /* Create nodes for all integer types (and error_mark_node) using the sizes
9579 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9580 SHORT_DOUBLE specifies whether double should be of the same precision
9581 as float. */
9583 void
9584 build_common_tree_nodes (bool signed_char, bool short_double)
9586 error_mark_node = make_node (ERROR_MARK);
9587 TREE_TYPE (error_mark_node) = error_mark_node;
9589 initialize_sizetypes ();
9591 /* Define both `signed char' and `unsigned char'. */
9592 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9593 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9594 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9595 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9597 /* Define `char', which is like either `signed char' or `unsigned char'
9598 but not the same as either. */
9599 char_type_node
9600 = (signed_char
9601 ? make_signed_type (CHAR_TYPE_SIZE)
9602 : make_unsigned_type (CHAR_TYPE_SIZE));
9603 TYPE_STRING_FLAG (char_type_node) = 1;
9605 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9606 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9607 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9608 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9609 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9610 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9611 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9612 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9613 #if HOST_BITS_PER_WIDE_INT >= 64
9614 /* TODO: This isn't correct, but as logic depends at the moment on
9615 host's instead of target's wide-integer.
9616 If there is a target not supporting TImode, but has an 128-bit
9617 integer-scalar register, this target check needs to be adjusted. */
9618 if (targetm.scalar_mode_supported_p (TImode))
9620 int128_integer_type_node = make_signed_type (128);
9621 int128_unsigned_type_node = make_unsigned_type (128);
9623 #endif
9625 /* Define a boolean type. This type only represents boolean values but
9626 may be larger than char depending on the value of BOOL_TYPE_SIZE.
9627 Front ends which want to override this size (i.e. Java) can redefine
9628 boolean_type_node before calling build_common_tree_nodes_2. */
9629 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9630 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9631 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9632 TYPE_PRECISION (boolean_type_node) = 1;
9634 /* Define what type to use for size_t. */
9635 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9636 size_type_node = unsigned_type_node;
9637 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9638 size_type_node = long_unsigned_type_node;
9639 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9640 size_type_node = long_long_unsigned_type_node;
9641 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9642 size_type_node = short_unsigned_type_node;
9643 else
9644 gcc_unreachable ();
9646 /* Fill in the rest of the sized types. Reuse existing type nodes
9647 when possible. */
9648 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9649 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9650 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9651 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9652 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9654 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9655 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9656 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9657 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9658 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9660 /* Don't call build_qualified type for atomics. That routine does
9661 special processing for atomics, and until they are initialized
9662 it's better not to make that call.
9664 Check to see if there is a target override for atomic types. */
9666 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
9667 targetm.atomic_align_for_mode (QImode));
9668 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
9669 targetm.atomic_align_for_mode (HImode));
9670 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
9671 targetm.atomic_align_for_mode (SImode));
9672 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
9673 targetm.atomic_align_for_mode (DImode));
9674 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
9675 targetm.atomic_align_for_mode (TImode));
9677 access_public_node = get_identifier ("public");
9678 access_protected_node = get_identifier ("protected");
9679 access_private_node = get_identifier ("private");
9681 /* Define these next since types below may used them. */
9682 integer_zero_node = build_int_cst (integer_type_node, 0);
9683 integer_one_node = build_int_cst (integer_type_node, 1);
9684 integer_three_node = build_int_cst (integer_type_node, 3);
9685 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9687 size_zero_node = size_int (0);
9688 size_one_node = size_int (1);
9689 bitsize_zero_node = bitsize_int (0);
9690 bitsize_one_node = bitsize_int (1);
9691 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9693 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9694 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9696 void_type_node = make_node (VOID_TYPE);
9697 layout_type (void_type_node);
9699 /* We are not going to have real types in C with less than byte alignment,
9700 so we might as well not have any types that claim to have it. */
9701 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9702 TYPE_USER_ALIGN (void_type_node) = 0;
9704 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9705 layout_type (TREE_TYPE (null_pointer_node));
9707 ptr_type_node = build_pointer_type (void_type_node);
9708 const_ptr_type_node
9709 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9710 fileptr_type_node = ptr_type_node;
9712 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
9714 float_type_node = make_node (REAL_TYPE);
9715 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9716 layout_type (float_type_node);
9718 double_type_node = make_node (REAL_TYPE);
9719 if (short_double)
9720 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9721 else
9722 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9723 layout_type (double_type_node);
9725 long_double_type_node = make_node (REAL_TYPE);
9726 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9727 layout_type (long_double_type_node);
9729 float_ptr_type_node = build_pointer_type (float_type_node);
9730 double_ptr_type_node = build_pointer_type (double_type_node);
9731 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9732 integer_ptr_type_node = build_pointer_type (integer_type_node);
9734 /* Fixed size integer types. */
9735 uint16_type_node = build_nonstandard_integer_type (16, true);
9736 uint32_type_node = build_nonstandard_integer_type (32, true);
9737 uint64_type_node = build_nonstandard_integer_type (64, true);
9739 /* Decimal float types. */
9740 dfloat32_type_node = make_node (REAL_TYPE);
9741 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9742 layout_type (dfloat32_type_node);
9743 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9744 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9746 dfloat64_type_node = make_node (REAL_TYPE);
9747 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9748 layout_type (dfloat64_type_node);
9749 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9750 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9752 dfloat128_type_node = make_node (REAL_TYPE);
9753 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9754 layout_type (dfloat128_type_node);
9755 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9756 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9758 complex_integer_type_node = build_complex_type (integer_type_node);
9759 complex_float_type_node = build_complex_type (float_type_node);
9760 complex_double_type_node = build_complex_type (double_type_node);
9761 complex_long_double_type_node = build_complex_type (long_double_type_node);
9763 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9764 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9765 sat_ ## KIND ## _type_node = \
9766 make_sat_signed_ ## KIND ## _type (SIZE); \
9767 sat_unsigned_ ## KIND ## _type_node = \
9768 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9769 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9770 unsigned_ ## KIND ## _type_node = \
9771 make_unsigned_ ## KIND ## _type (SIZE);
9773 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9774 sat_ ## WIDTH ## KIND ## _type_node = \
9775 make_sat_signed_ ## KIND ## _type (SIZE); \
9776 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9777 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9778 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9779 unsigned_ ## WIDTH ## KIND ## _type_node = \
9780 make_unsigned_ ## KIND ## _type (SIZE);
9782 /* Make fixed-point type nodes based on four different widths. */
9783 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9784 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9785 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9786 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9787 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9789 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9790 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9791 NAME ## _type_node = \
9792 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9793 u ## NAME ## _type_node = \
9794 make_or_reuse_unsigned_ ## KIND ## _type \
9795 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9796 sat_ ## NAME ## _type_node = \
9797 make_or_reuse_sat_signed_ ## KIND ## _type \
9798 (GET_MODE_BITSIZE (MODE ## mode)); \
9799 sat_u ## NAME ## _type_node = \
9800 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9801 (GET_MODE_BITSIZE (U ## MODE ## mode));
9803 /* Fixed-point type and mode nodes. */
9804 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9805 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9806 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9807 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9808 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9809 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9810 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9811 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9812 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9813 MAKE_FIXED_MODE_NODE (accum, da, DA)
9814 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9817 tree t = targetm.build_builtin_va_list ();
9819 /* Many back-ends define record types without setting TYPE_NAME.
9820 If we copied the record type here, we'd keep the original
9821 record type without a name. This breaks name mangling. So,
9822 don't copy record types and let c_common_nodes_and_builtins()
9823 declare the type to be __builtin_va_list. */
9824 if (TREE_CODE (t) != RECORD_TYPE)
9825 t = build_variant_type_copy (t);
9827 va_list_type_node = t;
9831 /* Modify DECL for given flags.
9832 TM_PURE attribute is set only on types, so the function will modify
9833 DECL's type when ECF_TM_PURE is used. */
9835 void
9836 set_call_expr_flags (tree decl, int flags)
9838 if (flags & ECF_NOTHROW)
9839 TREE_NOTHROW (decl) = 1;
9840 if (flags & ECF_CONST)
9841 TREE_READONLY (decl) = 1;
9842 if (flags & ECF_PURE)
9843 DECL_PURE_P (decl) = 1;
9844 if (flags & ECF_LOOPING_CONST_OR_PURE)
9845 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9846 if (flags & ECF_NOVOPS)
9847 DECL_IS_NOVOPS (decl) = 1;
9848 if (flags & ECF_NORETURN)
9849 TREE_THIS_VOLATILE (decl) = 1;
9850 if (flags & ECF_MALLOC)
9851 DECL_IS_MALLOC (decl) = 1;
9852 if (flags & ECF_RETURNS_TWICE)
9853 DECL_IS_RETURNS_TWICE (decl) = 1;
9854 if (flags & ECF_LEAF)
9855 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9856 NULL, DECL_ATTRIBUTES (decl));
9857 if ((flags & ECF_TM_PURE) && flag_tm)
9858 apply_tm_attr (decl, get_identifier ("transaction_pure"));
9859 /* Looping const or pure is implied by noreturn.
9860 There is currently no way to declare looping const or looping pure alone. */
9861 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9862 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9866 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9868 static void
9869 local_define_builtin (const char *name, tree type, enum built_in_function code,
9870 const char *library_name, int ecf_flags)
9872 tree decl;
9874 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9875 library_name, NULL_TREE);
9876 set_call_expr_flags (decl, ecf_flags);
9878 set_builtin_decl (code, decl, true);
9881 /* Call this function after instantiating all builtins that the language
9882 front end cares about. This will build the rest of the builtins
9883 and internal functions that are relied upon by the tree optimizers and
9884 the middle-end. */
9886 void
9887 build_common_builtin_nodes (void)
9889 tree tmp, ftype;
9890 int ecf_flags;
9892 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9894 ftype = build_function_type (void_type_node, void_list_node);
9895 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
9896 "__builtin_unreachable",
9897 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9898 | ECF_CONST | ECF_LEAF);
9901 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9902 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9904 ftype = build_function_type_list (ptr_type_node,
9905 ptr_type_node, const_ptr_type_node,
9906 size_type_node, NULL_TREE);
9908 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
9909 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9910 "memcpy", ECF_NOTHROW | ECF_LEAF);
9911 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9912 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9913 "memmove", ECF_NOTHROW | ECF_LEAF);
9916 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
9918 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9919 const_ptr_type_node, size_type_node,
9920 NULL_TREE);
9921 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9922 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9925 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
9927 ftype = build_function_type_list (ptr_type_node,
9928 ptr_type_node, integer_type_node,
9929 size_type_node, NULL_TREE);
9930 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9931 "memset", ECF_NOTHROW | ECF_LEAF);
9934 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
9936 ftype = build_function_type_list (ptr_type_node,
9937 size_type_node, NULL_TREE);
9938 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9939 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9942 ftype = build_function_type_list (ptr_type_node, size_type_node,
9943 size_type_node, NULL_TREE);
9944 local_define_builtin ("__builtin_alloca_with_align", ftype,
9945 BUILT_IN_ALLOCA_WITH_ALIGN, "alloca",
9946 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9948 /* If we're checking the stack, `alloca' can throw. */
9949 if (flag_stack_check)
9951 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
9952 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
9955 ftype = build_function_type_list (void_type_node,
9956 ptr_type_node, ptr_type_node,
9957 ptr_type_node, NULL_TREE);
9958 local_define_builtin ("__builtin_init_trampoline", ftype,
9959 BUILT_IN_INIT_TRAMPOLINE,
9960 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9961 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
9962 BUILT_IN_INIT_HEAP_TRAMPOLINE,
9963 "__builtin_init_heap_trampoline",
9964 ECF_NOTHROW | ECF_LEAF);
9966 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9967 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9968 BUILT_IN_ADJUST_TRAMPOLINE,
9969 "__builtin_adjust_trampoline",
9970 ECF_CONST | ECF_NOTHROW);
9972 ftype = build_function_type_list (void_type_node,
9973 ptr_type_node, ptr_type_node, NULL_TREE);
9974 local_define_builtin ("__builtin_nonlocal_goto", ftype,
9975 BUILT_IN_NONLOCAL_GOTO,
9976 "__builtin_nonlocal_goto",
9977 ECF_NORETURN | ECF_NOTHROW);
9979 ftype = build_function_type_list (void_type_node,
9980 ptr_type_node, ptr_type_node, NULL_TREE);
9981 local_define_builtin ("__builtin_setjmp_setup", ftype,
9982 BUILT_IN_SETJMP_SETUP,
9983 "__builtin_setjmp_setup", ECF_NOTHROW);
9985 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9986 local_define_builtin ("__builtin_setjmp_receiver", ftype,
9987 BUILT_IN_SETJMP_RECEIVER,
9988 "__builtin_setjmp_receiver", ECF_NOTHROW | ECF_LEAF);
9990 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9991 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
9992 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9994 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9995 local_define_builtin ("__builtin_stack_restore", ftype,
9996 BUILT_IN_STACK_RESTORE,
9997 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9999 /* If there's a possibility that we might use the ARM EABI, build the
10000 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
10001 if (targetm.arm_eabi_unwinder)
10003 ftype = build_function_type_list (void_type_node, NULL_TREE);
10004 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
10005 BUILT_IN_CXA_END_CLEANUP,
10006 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
10009 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10010 local_define_builtin ("__builtin_unwind_resume", ftype,
10011 BUILT_IN_UNWIND_RESUME,
10012 ((targetm_common.except_unwind_info (&global_options)
10013 == UI_SJLJ)
10014 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
10015 ECF_NORETURN);
10017 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
10019 ftype = build_function_type_list (ptr_type_node, integer_type_node,
10020 NULL_TREE);
10021 local_define_builtin ("__builtin_return_address", ftype,
10022 BUILT_IN_RETURN_ADDRESS,
10023 "__builtin_return_address",
10024 ECF_NOTHROW);
10027 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
10028 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10030 ftype = build_function_type_list (void_type_node, ptr_type_node,
10031 ptr_type_node, NULL_TREE);
10032 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
10033 local_define_builtin ("__cyg_profile_func_enter", ftype,
10034 BUILT_IN_PROFILE_FUNC_ENTER,
10035 "__cyg_profile_func_enter", 0);
10036 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10037 local_define_builtin ("__cyg_profile_func_exit", ftype,
10038 BUILT_IN_PROFILE_FUNC_EXIT,
10039 "__cyg_profile_func_exit", 0);
10042 /* The exception object and filter values from the runtime. The argument
10043 must be zero before exception lowering, i.e. from the front end. After
10044 exception lowering, it will be the region number for the exception
10045 landing pad. These functions are PURE instead of CONST to prevent
10046 them from being hoisted past the exception edge that will initialize
10047 its value in the landing pad. */
10048 ftype = build_function_type_list (ptr_type_node,
10049 integer_type_node, NULL_TREE);
10050 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
10051 /* Only use TM_PURE if we we have TM language support. */
10052 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
10053 ecf_flags |= ECF_TM_PURE;
10054 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
10055 "__builtin_eh_pointer", ecf_flags);
10057 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
10058 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
10059 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
10060 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10062 ftype = build_function_type_list (void_type_node,
10063 integer_type_node, integer_type_node,
10064 NULL_TREE);
10065 local_define_builtin ("__builtin_eh_copy_values", ftype,
10066 BUILT_IN_EH_COPY_VALUES,
10067 "__builtin_eh_copy_values", ECF_NOTHROW);
10069 /* Complex multiplication and division. These are handled as builtins
10070 rather than optabs because emit_library_call_value doesn't support
10071 complex. Further, we can do slightly better with folding these
10072 beasties if the real and complex parts of the arguments are separate. */
10074 int mode;
10076 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10078 char mode_name_buf[4], *q;
10079 const char *p;
10080 enum built_in_function mcode, dcode;
10081 tree type, inner_type;
10082 const char *prefix = "__";
10084 if (targetm.libfunc_gnu_prefix)
10085 prefix = "__gnu_";
10087 type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
10088 if (type == NULL)
10089 continue;
10090 inner_type = TREE_TYPE (type);
10092 ftype = build_function_type_list (type, inner_type, inner_type,
10093 inner_type, inner_type, NULL_TREE);
10095 mcode = ((enum built_in_function)
10096 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10097 dcode = ((enum built_in_function)
10098 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10100 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10101 *q = TOLOWER (*p);
10102 *q = '\0';
10104 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10105 NULL);
10106 local_define_builtin (built_in_names[mcode], ftype, mcode,
10107 built_in_names[mcode],
10108 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10110 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10111 NULL);
10112 local_define_builtin (built_in_names[dcode], ftype, dcode,
10113 built_in_names[dcode],
10114 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10118 init_internal_fns ();
10121 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10122 better way.
10124 If we requested a pointer to a vector, build up the pointers that
10125 we stripped off while looking for the inner type. Similarly for
10126 return values from functions.
10128 The argument TYPE is the top of the chain, and BOTTOM is the
10129 new type which we will point to. */
10131 tree
10132 reconstruct_complex_type (tree type, tree bottom)
10134 tree inner, outer;
10136 if (TREE_CODE (type) == POINTER_TYPE)
10138 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10139 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10140 TYPE_REF_CAN_ALIAS_ALL (type));
10142 else if (TREE_CODE (type) == REFERENCE_TYPE)
10144 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10145 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10146 TYPE_REF_CAN_ALIAS_ALL (type));
10148 else if (TREE_CODE (type) == ARRAY_TYPE)
10150 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10151 outer = build_array_type (inner, TYPE_DOMAIN (type));
10153 else if (TREE_CODE (type) == FUNCTION_TYPE)
10155 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10156 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
10158 else if (TREE_CODE (type) == METHOD_TYPE)
10160 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10161 /* The build_method_type_directly() routine prepends 'this' to argument list,
10162 so we must compensate by getting rid of it. */
10163 outer
10164 = build_method_type_directly
10165 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10166 inner,
10167 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10169 else if (TREE_CODE (type) == OFFSET_TYPE)
10171 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10172 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10174 else
10175 return bottom;
10177 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10178 TYPE_QUALS (type));
10181 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10182 the inner type. */
10183 tree
10184 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
10186 int nunits;
10188 switch (GET_MODE_CLASS (mode))
10190 case MODE_VECTOR_INT:
10191 case MODE_VECTOR_FLOAT:
10192 case MODE_VECTOR_FRACT:
10193 case MODE_VECTOR_UFRACT:
10194 case MODE_VECTOR_ACCUM:
10195 case MODE_VECTOR_UACCUM:
10196 nunits = GET_MODE_NUNITS (mode);
10197 break;
10199 case MODE_INT:
10200 /* Check that there are no leftover bits. */
10201 gcc_assert (GET_MODE_BITSIZE (mode)
10202 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10204 nunits = GET_MODE_BITSIZE (mode)
10205 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10206 break;
10208 default:
10209 gcc_unreachable ();
10212 return make_vector_type (innertype, nunits, mode);
10215 /* Similarly, but takes the inner type and number of units, which must be
10216 a power of two. */
10218 tree
10219 build_vector_type (tree innertype, int nunits)
10221 return make_vector_type (innertype, nunits, VOIDmode);
10224 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10226 tree
10227 build_opaque_vector_type (tree innertype, int nunits)
10229 tree t = make_vector_type (innertype, nunits, VOIDmode);
10230 tree cand;
10231 /* We always build the non-opaque variant before the opaque one,
10232 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10233 cand = TYPE_NEXT_VARIANT (t);
10234 if (cand
10235 && TYPE_VECTOR_OPAQUE (cand)
10236 && check_qualified_type (cand, t, TYPE_QUALS (t)))
10237 return cand;
10238 /* Othewise build a variant type and make sure to queue it after
10239 the non-opaque type. */
10240 cand = build_distinct_type_copy (t);
10241 TYPE_VECTOR_OPAQUE (cand) = true;
10242 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10243 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10244 TYPE_NEXT_VARIANT (t) = cand;
10245 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10246 return cand;
10250 /* Given an initializer INIT, return TRUE if INIT is zero or some
10251 aggregate of zeros. Otherwise return FALSE. */
10252 bool
10253 initializer_zerop (const_tree init)
10255 tree elt;
10257 STRIP_NOPS (init);
10259 switch (TREE_CODE (init))
10261 case INTEGER_CST:
10262 return integer_zerop (init);
10264 case REAL_CST:
10265 /* ??? Note that this is not correct for C4X float formats. There,
10266 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10267 negative exponent. */
10268 return real_zerop (init)
10269 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10271 case FIXED_CST:
10272 return fixed_zerop (init);
10274 case COMPLEX_CST:
10275 return integer_zerop (init)
10276 || (real_zerop (init)
10277 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10278 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10280 case VECTOR_CST:
10282 unsigned i;
10283 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10284 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10285 return false;
10286 return true;
10289 case CONSTRUCTOR:
10291 unsigned HOST_WIDE_INT idx;
10293 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10294 if (!initializer_zerop (elt))
10295 return false;
10296 return true;
10299 case STRING_CST:
10301 int i;
10303 /* We need to loop through all elements to handle cases like
10304 "\0" and "\0foobar". */
10305 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10306 if (TREE_STRING_POINTER (init)[i] != '\0')
10307 return false;
10309 return true;
10312 default:
10313 return false;
10317 /* Check if vector VEC consists of all the equal elements and
10318 that the number of elements corresponds to the type of VEC.
10319 The function returns first element of the vector
10320 or NULL_TREE if the vector is not uniform. */
10321 tree
10322 uniform_vector_p (const_tree vec)
10324 tree first, t;
10325 unsigned i;
10327 if (vec == NULL_TREE)
10328 return NULL_TREE;
10330 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10332 if (TREE_CODE (vec) == VECTOR_CST)
10334 first = VECTOR_CST_ELT (vec, 0);
10335 for (i = 1; i < VECTOR_CST_NELTS (vec); ++i)
10336 if (!operand_equal_p (first, VECTOR_CST_ELT (vec, i), 0))
10337 return NULL_TREE;
10339 return first;
10342 else if (TREE_CODE (vec) == CONSTRUCTOR)
10344 first = error_mark_node;
10346 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10348 if (i == 0)
10350 first = t;
10351 continue;
10353 if (!operand_equal_p (first, t, 0))
10354 return NULL_TREE;
10356 if (i != TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)))
10357 return NULL_TREE;
10359 return first;
10362 return NULL_TREE;
10365 /* Build an empty statement at location LOC. */
10367 tree
10368 build_empty_stmt (location_t loc)
10370 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10371 SET_EXPR_LOCATION (t, loc);
10372 return t;
10376 /* Build an OpenMP clause with code CODE. LOC is the location of the
10377 clause. */
10379 tree
10380 build_omp_clause (location_t loc, enum omp_clause_code code)
10382 tree t;
10383 int size, length;
10385 length = omp_clause_num_ops[code];
10386 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10388 record_node_allocation_statistics (OMP_CLAUSE, size);
10390 t = ggc_alloc_tree_node (size);
10391 memset (t, 0, size);
10392 TREE_SET_CODE (t, OMP_CLAUSE);
10393 OMP_CLAUSE_SET_CODE (t, code);
10394 OMP_CLAUSE_LOCATION (t) = loc;
10396 return t;
10399 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10400 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10401 Except for the CODE and operand count field, other storage for the
10402 object is initialized to zeros. */
10404 tree
10405 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10407 tree t;
10408 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10410 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10411 gcc_assert (len >= 1);
10413 record_node_allocation_statistics (code, length);
10415 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10417 TREE_SET_CODE (t, code);
10419 /* Can't use TREE_OPERAND to store the length because if checking is
10420 enabled, it will try to check the length before we store it. :-P */
10421 t->exp.operands[0] = build_int_cst (sizetype, len);
10423 return t;
10426 /* Helper function for build_call_* functions; build a CALL_EXPR with
10427 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10428 the argument slots. */
10430 static tree
10431 build_call_1 (tree return_type, tree fn, int nargs)
10433 tree t;
10435 t = build_vl_exp (CALL_EXPR, nargs + 3);
10436 TREE_TYPE (t) = return_type;
10437 CALL_EXPR_FN (t) = fn;
10438 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10440 return t;
10443 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10444 FN and a null static chain slot. NARGS is the number of call arguments
10445 which are specified as "..." arguments. */
10447 tree
10448 build_call_nary (tree return_type, tree fn, int nargs, ...)
10450 tree ret;
10451 va_list args;
10452 va_start (args, nargs);
10453 ret = build_call_valist (return_type, fn, nargs, args);
10454 va_end (args);
10455 return ret;
10458 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10459 FN and a null static chain slot. NARGS is the number of call arguments
10460 which are specified as a va_list ARGS. */
10462 tree
10463 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10465 tree t;
10466 int i;
10468 t = build_call_1 (return_type, fn, nargs);
10469 for (i = 0; i < nargs; i++)
10470 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10471 process_call_operands (t);
10472 return t;
10475 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10476 FN and a null static chain slot. NARGS is the number of call arguments
10477 which are specified as a tree array ARGS. */
10479 tree
10480 build_call_array_loc (location_t loc, tree return_type, tree fn,
10481 int nargs, const tree *args)
10483 tree t;
10484 int i;
10486 t = build_call_1 (return_type, fn, nargs);
10487 for (i = 0; i < nargs; i++)
10488 CALL_EXPR_ARG (t, i) = args[i];
10489 process_call_operands (t);
10490 SET_EXPR_LOCATION (t, loc);
10491 return t;
10494 /* Like build_call_array, but takes a vec. */
10496 tree
10497 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10499 tree ret, t;
10500 unsigned int ix;
10502 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10503 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10504 CALL_EXPR_ARG (ret, ix) = t;
10505 process_call_operands (ret);
10506 return ret;
10509 /* Return true if T (assumed to be a DECL) must be assigned a memory
10510 location. */
10512 bool
10513 needs_to_live_in_memory (const_tree t)
10515 return (TREE_ADDRESSABLE (t)
10516 || is_global_var (t)
10517 || (TREE_CODE (t) == RESULT_DECL
10518 && !DECL_BY_REFERENCE (t)
10519 && aggregate_value_p (t, current_function_decl)));
10522 /* Return value of a constant X and sign-extend it. */
10524 HOST_WIDE_INT
10525 int_cst_value (const_tree x)
10527 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10528 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10530 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10531 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10532 || TREE_INT_CST_HIGH (x) == -1);
10534 if (bits < HOST_BITS_PER_WIDE_INT)
10536 bool negative = ((val >> (bits - 1)) & 1) != 0;
10537 if (negative)
10538 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10539 else
10540 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10543 return val;
10546 /* Return value of a constant X and sign-extend it. */
10548 HOST_WIDEST_INT
10549 widest_int_cst_value (const_tree x)
10551 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10552 unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
10554 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
10555 gcc_assert (HOST_BITS_PER_WIDEST_INT >= HOST_BITS_PER_DOUBLE_INT);
10556 val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_HIGH (x))
10557 << HOST_BITS_PER_WIDE_INT);
10558 #else
10559 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10560 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10561 || TREE_INT_CST_HIGH (x) == -1);
10562 #endif
10564 if (bits < HOST_BITS_PER_WIDEST_INT)
10566 bool negative = ((val >> (bits - 1)) & 1) != 0;
10567 if (negative)
10568 val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
10569 else
10570 val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
10573 return val;
10576 /* If TYPE is an integral or pointer type, return an integer type with
10577 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10578 if TYPE is already an integer type of signedness UNSIGNEDP. */
10580 tree
10581 signed_or_unsigned_type_for (int unsignedp, tree type)
10583 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10584 return type;
10586 if (TREE_CODE (type) == VECTOR_TYPE)
10588 tree inner = TREE_TYPE (type);
10589 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10590 if (!inner2)
10591 return NULL_TREE;
10592 if (inner == inner2)
10593 return type;
10594 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10597 if (!INTEGRAL_TYPE_P (type)
10598 && !POINTER_TYPE_P (type)
10599 && TREE_CODE (type) != OFFSET_TYPE)
10600 return NULL_TREE;
10602 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10605 /* If TYPE is an integral or pointer type, return an integer type with
10606 the same precision which is unsigned, or itself if TYPE is already an
10607 unsigned integer type. */
10609 tree
10610 unsigned_type_for (tree type)
10612 return signed_or_unsigned_type_for (1, type);
10615 /* If TYPE is an integral or pointer type, return an integer type with
10616 the same precision which is signed, or itself if TYPE is already a
10617 signed integer type. */
10619 tree
10620 signed_type_for (tree type)
10622 return signed_or_unsigned_type_for (0, type);
10625 /* If TYPE is a vector type, return a signed integer vector type with the
10626 same width and number of subparts. Otherwise return boolean_type_node. */
10628 tree
10629 truth_type_for (tree type)
10631 if (TREE_CODE (type) == VECTOR_TYPE)
10633 tree elem = lang_hooks.types.type_for_size
10634 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10635 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10637 else
10638 return boolean_type_node;
10641 /* Returns the largest value obtainable by casting something in INNER type to
10642 OUTER type. */
10644 tree
10645 upper_bound_in_type (tree outer, tree inner)
10647 double_int high;
10648 unsigned int det = 0;
10649 unsigned oprec = TYPE_PRECISION (outer);
10650 unsigned iprec = TYPE_PRECISION (inner);
10651 unsigned prec;
10653 /* Compute a unique number for every combination. */
10654 det |= (oprec > iprec) ? 4 : 0;
10655 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10656 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10658 /* Determine the exponent to use. */
10659 switch (det)
10661 case 0:
10662 case 1:
10663 /* oprec <= iprec, outer: signed, inner: don't care. */
10664 prec = oprec - 1;
10665 break;
10666 case 2:
10667 case 3:
10668 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10669 prec = oprec;
10670 break;
10671 case 4:
10672 /* oprec > iprec, outer: signed, inner: signed. */
10673 prec = iprec - 1;
10674 break;
10675 case 5:
10676 /* oprec > iprec, outer: signed, inner: unsigned. */
10677 prec = iprec;
10678 break;
10679 case 6:
10680 /* oprec > iprec, outer: unsigned, inner: signed. */
10681 prec = oprec;
10682 break;
10683 case 7:
10684 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10685 prec = iprec;
10686 break;
10687 default:
10688 gcc_unreachable ();
10691 /* Compute 2^^prec - 1. */
10692 if (prec <= HOST_BITS_PER_WIDE_INT)
10694 high.high = 0;
10695 high.low = ((~(unsigned HOST_WIDE_INT) 0)
10696 >> (HOST_BITS_PER_WIDE_INT - prec));
10698 else
10700 high.high = ((~(unsigned HOST_WIDE_INT) 0)
10701 >> (HOST_BITS_PER_DOUBLE_INT - prec));
10702 high.low = ~(unsigned HOST_WIDE_INT) 0;
10705 return double_int_to_tree (outer, high);
10708 /* Returns the smallest value obtainable by casting something in INNER type to
10709 OUTER type. */
10711 tree
10712 lower_bound_in_type (tree outer, tree inner)
10714 double_int low;
10715 unsigned oprec = TYPE_PRECISION (outer);
10716 unsigned iprec = TYPE_PRECISION (inner);
10718 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10719 and obtain 0. */
10720 if (TYPE_UNSIGNED (outer)
10721 /* If we are widening something of an unsigned type, OUTER type
10722 contains all values of INNER type. In particular, both INNER
10723 and OUTER types have zero in common. */
10724 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10725 low.low = low.high = 0;
10726 else
10728 /* If we are widening a signed type to another signed type, we
10729 want to obtain -2^^(iprec-1). If we are keeping the
10730 precision or narrowing to a signed type, we want to obtain
10731 -2^(oprec-1). */
10732 unsigned prec = oprec > iprec ? iprec : oprec;
10734 if (prec <= HOST_BITS_PER_WIDE_INT)
10736 low.high = ~(unsigned HOST_WIDE_INT) 0;
10737 low.low = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
10739 else
10741 low.high = ((~(unsigned HOST_WIDE_INT) 0)
10742 << (prec - HOST_BITS_PER_WIDE_INT - 1));
10743 low.low = 0;
10747 return double_int_to_tree (outer, low);
10750 /* Return nonzero if two operands that are suitable for PHI nodes are
10751 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10752 SSA_NAME or invariant. Note that this is strictly an optimization.
10753 That is, callers of this function can directly call operand_equal_p
10754 and get the same result, only slower. */
10757 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10759 if (arg0 == arg1)
10760 return 1;
10761 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10762 return 0;
10763 return operand_equal_p (arg0, arg1, 0);
10766 /* Returns number of zeros at the end of binary representation of X.
10768 ??? Use ffs if available? */
10770 tree
10771 num_ending_zeros (const_tree x)
10773 unsigned HOST_WIDE_INT fr, nfr;
10774 unsigned num, abits;
10775 tree type = TREE_TYPE (x);
10777 if (TREE_INT_CST_LOW (x) == 0)
10779 num = HOST_BITS_PER_WIDE_INT;
10780 fr = TREE_INT_CST_HIGH (x);
10782 else
10784 num = 0;
10785 fr = TREE_INT_CST_LOW (x);
10788 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
10790 nfr = fr >> abits;
10791 if (nfr << abits == fr)
10793 num += abits;
10794 fr = nfr;
10798 if (num > TYPE_PRECISION (type))
10799 num = TYPE_PRECISION (type);
10801 return build_int_cst_type (type, num);
10805 #define WALK_SUBTREE(NODE) \
10806 do \
10808 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10809 if (result) \
10810 return result; \
10812 while (0)
10814 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10815 be walked whenever a type is seen in the tree. Rest of operands and return
10816 value are as for walk_tree. */
10818 static tree
10819 walk_type_fields (tree type, walk_tree_fn func, void *data,
10820 struct pointer_set_t *pset, walk_tree_lh lh)
10822 tree result = NULL_TREE;
10824 switch (TREE_CODE (type))
10826 case POINTER_TYPE:
10827 case REFERENCE_TYPE:
10828 case VECTOR_TYPE:
10829 /* We have to worry about mutually recursive pointers. These can't
10830 be written in C. They can in Ada. It's pathological, but
10831 there's an ACATS test (c38102a) that checks it. Deal with this
10832 by checking if we're pointing to another pointer, that one
10833 points to another pointer, that one does too, and we have no htab.
10834 If so, get a hash table. We check three levels deep to avoid
10835 the cost of the hash table if we don't need one. */
10836 if (POINTER_TYPE_P (TREE_TYPE (type))
10837 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10838 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10839 && !pset)
10841 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10842 func, data);
10843 if (result)
10844 return result;
10846 break;
10849 /* ... fall through ... */
10851 case COMPLEX_TYPE:
10852 WALK_SUBTREE (TREE_TYPE (type));
10853 break;
10855 case METHOD_TYPE:
10856 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10858 /* Fall through. */
10860 case FUNCTION_TYPE:
10861 WALK_SUBTREE (TREE_TYPE (type));
10863 tree arg;
10865 /* We never want to walk into default arguments. */
10866 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10867 WALK_SUBTREE (TREE_VALUE (arg));
10869 break;
10871 case ARRAY_TYPE:
10872 /* Don't follow this nodes's type if a pointer for fear that
10873 we'll have infinite recursion. If we have a PSET, then we
10874 need not fear. */
10875 if (pset
10876 || (!POINTER_TYPE_P (TREE_TYPE (type))
10877 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10878 WALK_SUBTREE (TREE_TYPE (type));
10879 WALK_SUBTREE (TYPE_DOMAIN (type));
10880 break;
10882 case OFFSET_TYPE:
10883 WALK_SUBTREE (TREE_TYPE (type));
10884 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
10885 break;
10887 default:
10888 break;
10891 return NULL_TREE;
10894 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
10895 called with the DATA and the address of each sub-tree. If FUNC returns a
10896 non-NULL value, the traversal is stopped, and the value returned by FUNC
10897 is returned. If PSET is non-NULL it is used to record the nodes visited,
10898 and to avoid visiting a node more than once. */
10900 tree
10901 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
10902 struct pointer_set_t *pset, walk_tree_lh lh)
10904 enum tree_code code;
10905 int walk_subtrees;
10906 tree result;
10908 #define WALK_SUBTREE_TAIL(NODE) \
10909 do \
10911 tp = & (NODE); \
10912 goto tail_recurse; \
10914 while (0)
10916 tail_recurse:
10917 /* Skip empty subtrees. */
10918 if (!*tp)
10919 return NULL_TREE;
10921 /* Don't walk the same tree twice, if the user has requested
10922 that we avoid doing so. */
10923 if (pset && pointer_set_insert (pset, *tp))
10924 return NULL_TREE;
10926 /* Call the function. */
10927 walk_subtrees = 1;
10928 result = (*func) (tp, &walk_subtrees, data);
10930 /* If we found something, return it. */
10931 if (result)
10932 return result;
10934 code = TREE_CODE (*tp);
10936 /* Even if we didn't, FUNC may have decided that there was nothing
10937 interesting below this point in the tree. */
10938 if (!walk_subtrees)
10940 /* But we still need to check our siblings. */
10941 if (code == TREE_LIST)
10942 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10943 else if (code == OMP_CLAUSE)
10944 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10945 else
10946 return NULL_TREE;
10949 if (lh)
10951 result = (*lh) (tp, &walk_subtrees, func, data, pset);
10952 if (result || !walk_subtrees)
10953 return result;
10956 switch (code)
10958 case ERROR_MARK:
10959 case IDENTIFIER_NODE:
10960 case INTEGER_CST:
10961 case REAL_CST:
10962 case FIXED_CST:
10963 case VECTOR_CST:
10964 case STRING_CST:
10965 case BLOCK:
10966 case PLACEHOLDER_EXPR:
10967 case SSA_NAME:
10968 case FIELD_DECL:
10969 case RESULT_DECL:
10970 /* None of these have subtrees other than those already walked
10971 above. */
10972 break;
10974 case TREE_LIST:
10975 WALK_SUBTREE (TREE_VALUE (*tp));
10976 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10977 break;
10979 case TREE_VEC:
10981 int len = TREE_VEC_LENGTH (*tp);
10983 if (len == 0)
10984 break;
10986 /* Walk all elements but the first. */
10987 while (--len)
10988 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
10990 /* Now walk the first one as a tail call. */
10991 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
10994 case COMPLEX_CST:
10995 WALK_SUBTREE (TREE_REALPART (*tp));
10996 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
10998 case CONSTRUCTOR:
11000 unsigned HOST_WIDE_INT idx;
11001 constructor_elt *ce;
11003 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*tp), idx, &ce);
11004 idx++)
11005 WALK_SUBTREE (ce->value);
11007 break;
11009 case SAVE_EXPR:
11010 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
11012 case BIND_EXPR:
11014 tree decl;
11015 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
11017 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11018 into declarations that are just mentioned, rather than
11019 declared; they don't really belong to this part of the tree.
11020 And, we can see cycles: the initializer for a declaration
11021 can refer to the declaration itself. */
11022 WALK_SUBTREE (DECL_INITIAL (decl));
11023 WALK_SUBTREE (DECL_SIZE (decl));
11024 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11026 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
11029 case STATEMENT_LIST:
11031 tree_stmt_iterator i;
11032 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
11033 WALK_SUBTREE (*tsi_stmt_ptr (i));
11035 break;
11037 case OMP_CLAUSE:
11038 switch (OMP_CLAUSE_CODE (*tp))
11040 case OMP_CLAUSE_PRIVATE:
11041 case OMP_CLAUSE_SHARED:
11042 case OMP_CLAUSE_FIRSTPRIVATE:
11043 case OMP_CLAUSE_COPYIN:
11044 case OMP_CLAUSE_COPYPRIVATE:
11045 case OMP_CLAUSE_FINAL:
11046 case OMP_CLAUSE_IF:
11047 case OMP_CLAUSE_NUM_THREADS:
11048 case OMP_CLAUSE_SCHEDULE:
11049 case OMP_CLAUSE_UNIFORM:
11050 case OMP_CLAUSE_DEPEND:
11051 case OMP_CLAUSE_NUM_TEAMS:
11052 case OMP_CLAUSE_THREAD_LIMIT:
11053 case OMP_CLAUSE_DEVICE:
11054 case OMP_CLAUSE_DIST_SCHEDULE:
11055 case OMP_CLAUSE_SAFELEN:
11056 case OMP_CLAUSE_SIMDLEN:
11057 case OMP_CLAUSE__LOOPTEMP_:
11058 case OMP_CLAUSE__SIMDUID_:
11059 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
11060 /* FALLTHRU */
11062 case OMP_CLAUSE_NOWAIT:
11063 case OMP_CLAUSE_ORDERED:
11064 case OMP_CLAUSE_DEFAULT:
11065 case OMP_CLAUSE_UNTIED:
11066 case OMP_CLAUSE_MERGEABLE:
11067 case OMP_CLAUSE_PROC_BIND:
11068 case OMP_CLAUSE_INBRANCH:
11069 case OMP_CLAUSE_NOTINBRANCH:
11070 case OMP_CLAUSE_FOR:
11071 case OMP_CLAUSE_PARALLEL:
11072 case OMP_CLAUSE_SECTIONS:
11073 case OMP_CLAUSE_TASKGROUP:
11074 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11076 case OMP_CLAUSE_LASTPRIVATE:
11077 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11078 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
11079 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11081 case OMP_CLAUSE_COLLAPSE:
11083 int i;
11084 for (i = 0; i < 3; i++)
11085 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11086 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11089 case OMP_CLAUSE_LINEAR:
11090 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11091 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STEP (*tp));
11092 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STMT (*tp));
11093 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11095 case OMP_CLAUSE_ALIGNED:
11096 case OMP_CLAUSE_FROM:
11097 case OMP_CLAUSE_TO:
11098 case OMP_CLAUSE_MAP:
11099 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11100 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11101 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11103 case OMP_CLAUSE_REDUCTION:
11105 int i;
11106 for (i = 0; i < 4; i++)
11107 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11108 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11111 default:
11112 gcc_unreachable ();
11114 break;
11116 case TARGET_EXPR:
11118 int i, len;
11120 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11121 But, we only want to walk once. */
11122 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11123 for (i = 0; i < len; ++i)
11124 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11125 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11128 case DECL_EXPR:
11129 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11130 defining. We only want to walk into these fields of a type in this
11131 case and not in the general case of a mere reference to the type.
11133 The criterion is as follows: if the field can be an expression, it
11134 must be walked only here. This should be in keeping with the fields
11135 that are directly gimplified in gimplify_type_sizes in order for the
11136 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11137 variable-sized types.
11139 Note that DECLs get walked as part of processing the BIND_EXPR. */
11140 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11142 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11143 if (TREE_CODE (*type_p) == ERROR_MARK)
11144 return NULL_TREE;
11146 /* Call the function for the type. See if it returns anything or
11147 doesn't want us to continue. If we are to continue, walk both
11148 the normal fields and those for the declaration case. */
11149 result = (*func) (type_p, &walk_subtrees, data);
11150 if (result || !walk_subtrees)
11151 return result;
11153 /* But do not walk a pointed-to type since it may itself need to
11154 be walked in the declaration case if it isn't anonymous. */
11155 if (!POINTER_TYPE_P (*type_p))
11157 result = walk_type_fields (*type_p, func, data, pset, lh);
11158 if (result)
11159 return result;
11162 /* If this is a record type, also walk the fields. */
11163 if (RECORD_OR_UNION_TYPE_P (*type_p))
11165 tree field;
11167 for (field = TYPE_FIELDS (*type_p); field;
11168 field = DECL_CHAIN (field))
11170 /* We'd like to look at the type of the field, but we can
11171 easily get infinite recursion. So assume it's pointed
11172 to elsewhere in the tree. Also, ignore things that
11173 aren't fields. */
11174 if (TREE_CODE (field) != FIELD_DECL)
11175 continue;
11177 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11178 WALK_SUBTREE (DECL_SIZE (field));
11179 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11180 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11181 WALK_SUBTREE (DECL_QUALIFIER (field));
11185 /* Same for scalar types. */
11186 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11187 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11188 || TREE_CODE (*type_p) == INTEGER_TYPE
11189 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11190 || TREE_CODE (*type_p) == REAL_TYPE)
11192 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11193 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11196 WALK_SUBTREE (TYPE_SIZE (*type_p));
11197 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11199 /* FALLTHRU */
11201 default:
11202 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11204 int i, len;
11206 /* Walk over all the sub-trees of this operand. */
11207 len = TREE_OPERAND_LENGTH (*tp);
11209 /* Go through the subtrees. We need to do this in forward order so
11210 that the scope of a FOR_EXPR is handled properly. */
11211 if (len)
11213 for (i = 0; i < len - 1; ++i)
11214 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11215 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11218 /* If this is a type, walk the needed fields in the type. */
11219 else if (TYPE_P (*tp))
11220 return walk_type_fields (*tp, func, data, pset, lh);
11221 break;
11224 /* We didn't find what we were looking for. */
11225 return NULL_TREE;
11227 #undef WALK_SUBTREE_TAIL
11229 #undef WALK_SUBTREE
11231 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11233 tree
11234 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11235 walk_tree_lh lh)
11237 tree result;
11238 struct pointer_set_t *pset;
11240 pset = pointer_set_create ();
11241 result = walk_tree_1 (tp, func, data, pset, lh);
11242 pointer_set_destroy (pset);
11243 return result;
11247 tree
11248 tree_block (tree t)
11250 char const c = TREE_CODE_CLASS (TREE_CODE (t));
11252 if (IS_EXPR_CODE_CLASS (c))
11253 return LOCATION_BLOCK (t->exp.locus);
11254 gcc_unreachable ();
11255 return NULL;
11258 void
11259 tree_set_block (tree t, tree b)
11261 char const c = TREE_CODE_CLASS (TREE_CODE (t));
11263 if (IS_EXPR_CODE_CLASS (c))
11265 if (b)
11266 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
11267 else
11268 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
11270 else
11271 gcc_unreachable ();
11274 /* Create a nameless artificial label and put it in the current
11275 function context. The label has a location of LOC. Returns the
11276 newly created label. */
11278 tree
11279 create_artificial_label (location_t loc)
11281 tree lab = build_decl (loc,
11282 LABEL_DECL, NULL_TREE, void_type_node);
11284 DECL_ARTIFICIAL (lab) = 1;
11285 DECL_IGNORED_P (lab) = 1;
11286 DECL_CONTEXT (lab) = current_function_decl;
11287 return lab;
11290 /* Given a tree, try to return a useful variable name that we can use
11291 to prefix a temporary that is being assigned the value of the tree.
11292 I.E. given <temp> = &A, return A. */
11294 const char *
11295 get_name (tree t)
11297 tree stripped_decl;
11299 stripped_decl = t;
11300 STRIP_NOPS (stripped_decl);
11301 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11302 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11303 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11305 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11306 if (!name)
11307 return NULL;
11308 return IDENTIFIER_POINTER (name);
11310 else
11312 switch (TREE_CODE (stripped_decl))
11314 case ADDR_EXPR:
11315 return get_name (TREE_OPERAND (stripped_decl, 0));
11316 default:
11317 return NULL;
11322 /* Return true if TYPE has a variable argument list. */
11324 bool
11325 stdarg_p (const_tree fntype)
11327 function_args_iterator args_iter;
11328 tree n = NULL_TREE, t;
11330 if (!fntype)
11331 return false;
11333 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
11335 n = t;
11338 return n != NULL_TREE && n != void_type_node;
11341 /* Return true if TYPE has a prototype. */
11343 bool
11344 prototype_p (tree fntype)
11346 tree t;
11348 gcc_assert (fntype != NULL_TREE);
11350 t = TYPE_ARG_TYPES (fntype);
11351 return (t != NULL_TREE);
11354 /* If BLOCK is inlined from an __attribute__((__artificial__))
11355 routine, return pointer to location from where it has been
11356 called. */
11357 location_t *
11358 block_nonartificial_location (tree block)
11360 location_t *ret = NULL;
11362 while (block && TREE_CODE (block) == BLOCK
11363 && BLOCK_ABSTRACT_ORIGIN (block))
11365 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11367 while (TREE_CODE (ao) == BLOCK
11368 && BLOCK_ABSTRACT_ORIGIN (ao)
11369 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11370 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11372 if (TREE_CODE (ao) == FUNCTION_DECL)
11374 /* If AO is an artificial inline, point RET to the
11375 call site locus at which it has been inlined and continue
11376 the loop, in case AO's caller is also an artificial
11377 inline. */
11378 if (DECL_DECLARED_INLINE_P (ao)
11379 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11380 ret = &BLOCK_SOURCE_LOCATION (block);
11381 else
11382 break;
11384 else if (TREE_CODE (ao) != BLOCK)
11385 break;
11387 block = BLOCK_SUPERCONTEXT (block);
11389 return ret;
11393 /* If EXP is inlined from an __attribute__((__artificial__))
11394 function, return the location of the original call expression. */
11396 location_t
11397 tree_nonartificial_location (tree exp)
11399 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11401 if (loc)
11402 return *loc;
11403 else
11404 return EXPR_LOCATION (exp);
11408 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11409 nodes. */
11411 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11413 static hashval_t
11414 cl_option_hash_hash (const void *x)
11416 const_tree const t = (const_tree) x;
11417 const char *p;
11418 size_t i;
11419 size_t len = 0;
11420 hashval_t hash = 0;
11422 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11424 p = (const char *)TREE_OPTIMIZATION (t);
11425 len = sizeof (struct cl_optimization);
11428 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11430 p = (const char *)TREE_TARGET_OPTION (t);
11431 len = sizeof (struct cl_target_option);
11434 else
11435 gcc_unreachable ();
11437 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11438 something else. */
11439 for (i = 0; i < len; i++)
11440 if (p[i])
11441 hash = (hash << 4) ^ ((i << 2) | p[i]);
11443 return hash;
11446 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11447 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11448 same. */
11450 static int
11451 cl_option_hash_eq (const void *x, const void *y)
11453 const_tree const xt = (const_tree) x;
11454 const_tree const yt = (const_tree) y;
11455 const char *xp;
11456 const char *yp;
11457 size_t len;
11459 if (TREE_CODE (xt) != TREE_CODE (yt))
11460 return 0;
11462 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11464 xp = (const char *)TREE_OPTIMIZATION (xt);
11465 yp = (const char *)TREE_OPTIMIZATION (yt);
11466 len = sizeof (struct cl_optimization);
11469 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11471 xp = (const char *)TREE_TARGET_OPTION (xt);
11472 yp = (const char *)TREE_TARGET_OPTION (yt);
11473 len = sizeof (struct cl_target_option);
11476 else
11477 gcc_unreachable ();
11479 return (memcmp (xp, yp, len) == 0);
11482 /* Build an OPTIMIZATION_NODE based on the options in OPTS. */
11484 tree
11485 build_optimization_node (struct gcc_options *opts)
11487 tree t;
11488 void **slot;
11490 /* Use the cache of optimization nodes. */
11492 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11493 opts);
11495 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
11496 t = (tree) *slot;
11497 if (!t)
11499 /* Insert this one into the hash table. */
11500 t = cl_optimization_node;
11501 *slot = t;
11503 /* Make a new node for next time round. */
11504 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11507 return t;
11510 /* Build a TARGET_OPTION_NODE based on the options in OPTS. */
11512 tree
11513 build_target_option_node (struct gcc_options *opts)
11515 tree t;
11516 void **slot;
11518 /* Use the cache of optimization nodes. */
11520 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11521 opts);
11523 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
11524 t = (tree) *slot;
11525 if (!t)
11527 /* Insert this one into the hash table. */
11528 t = cl_target_option_node;
11529 *slot = t;
11531 /* Make a new node for next time round. */
11532 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11535 return t;
11538 /* Reset TREE_TARGET_GLOBALS cache for TARGET_OPTION_NODE.
11539 Called through htab_traverse. */
11541 static int
11542 prepare_target_option_node_for_pch (void **slot, void *)
11544 tree node = (tree) *slot;
11545 if (TREE_CODE (node) == TARGET_OPTION_NODE)
11546 TREE_TARGET_GLOBALS (node) = NULL;
11547 return 1;
11550 /* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
11551 so that they aren't saved during PCH writing. */
11553 void
11554 prepare_target_option_nodes_for_pch (void)
11556 htab_traverse (cl_option_hash_table, prepare_target_option_node_for_pch,
11557 NULL);
11560 /* Determine the "ultimate origin" of a block. The block may be an inlined
11561 instance of an inlined instance of a block which is local to an inline
11562 function, so we have to trace all of the way back through the origin chain
11563 to find out what sort of node actually served as the original seed for the
11564 given block. */
11566 tree
11567 block_ultimate_origin (const_tree block)
11569 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11571 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
11572 nodes in the function to point to themselves; ignore that if
11573 we're trying to output the abstract instance of this function. */
11574 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11575 return NULL_TREE;
11577 if (immediate_origin == NULL_TREE)
11578 return NULL_TREE;
11579 else
11581 tree ret_val;
11582 tree lookahead = immediate_origin;
11586 ret_val = lookahead;
11587 lookahead = (TREE_CODE (ret_val) == BLOCK
11588 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11590 while (lookahead != NULL && lookahead != ret_val);
11592 /* The block's abstract origin chain may not be the *ultimate* origin of
11593 the block. It could lead to a DECL that has an abstract origin set.
11594 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11595 will give us if it has one). Note that DECL's abstract origins are
11596 supposed to be the most distant ancestor (or so decl_ultimate_origin
11597 claims), so we don't need to loop following the DECL origins. */
11598 if (DECL_P (ret_val))
11599 return DECL_ORIGIN (ret_val);
11601 return ret_val;
11605 /* Return true iff conversion in EXP generates no instruction. Mark
11606 it inline so that we fully inline into the stripping functions even
11607 though we have two uses of this function. */
11609 static inline bool
11610 tree_nop_conversion (const_tree exp)
11612 tree outer_type, inner_type;
11614 if (!CONVERT_EXPR_P (exp)
11615 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11616 return false;
11617 if (TREE_OPERAND (exp, 0) == error_mark_node)
11618 return false;
11620 outer_type = TREE_TYPE (exp);
11621 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11623 if (!inner_type)
11624 return false;
11626 /* Use precision rather then machine mode when we can, which gives
11627 the correct answer even for submode (bit-field) types. */
11628 if ((INTEGRAL_TYPE_P (outer_type)
11629 || POINTER_TYPE_P (outer_type)
11630 || TREE_CODE (outer_type) == OFFSET_TYPE)
11631 && (INTEGRAL_TYPE_P (inner_type)
11632 || POINTER_TYPE_P (inner_type)
11633 || TREE_CODE (inner_type) == OFFSET_TYPE))
11634 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11636 /* Otherwise fall back on comparing machine modes (e.g. for
11637 aggregate types, floats). */
11638 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11641 /* Return true iff conversion in EXP generates no instruction. Don't
11642 consider conversions changing the signedness. */
11644 static bool
11645 tree_sign_nop_conversion (const_tree exp)
11647 tree outer_type, inner_type;
11649 if (!tree_nop_conversion (exp))
11650 return false;
11652 outer_type = TREE_TYPE (exp);
11653 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11655 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11656 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11659 /* Strip conversions from EXP according to tree_nop_conversion and
11660 return the resulting expression. */
11662 tree
11663 tree_strip_nop_conversions (tree exp)
11665 while (tree_nop_conversion (exp))
11666 exp = TREE_OPERAND (exp, 0);
11667 return exp;
11670 /* Strip conversions from EXP according to tree_sign_nop_conversion
11671 and return the resulting expression. */
11673 tree
11674 tree_strip_sign_nop_conversions (tree exp)
11676 while (tree_sign_nop_conversion (exp))
11677 exp = TREE_OPERAND (exp, 0);
11678 return exp;
11681 /* Avoid any floating point extensions from EXP. */
11682 tree
11683 strip_float_extensions (tree exp)
11685 tree sub, expt, subt;
11687 /* For floating point constant look up the narrowest type that can hold
11688 it properly and handle it like (type)(narrowest_type)constant.
11689 This way we can optimize for instance a=a*2.0 where "a" is float
11690 but 2.0 is double constant. */
11691 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11693 REAL_VALUE_TYPE orig;
11694 tree type = NULL;
11696 orig = TREE_REAL_CST (exp);
11697 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11698 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11699 type = float_type_node;
11700 else if (TYPE_PRECISION (TREE_TYPE (exp))
11701 > TYPE_PRECISION (double_type_node)
11702 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11703 type = double_type_node;
11704 if (type)
11705 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11708 if (!CONVERT_EXPR_P (exp))
11709 return exp;
11711 sub = TREE_OPERAND (exp, 0);
11712 subt = TREE_TYPE (sub);
11713 expt = TREE_TYPE (exp);
11715 if (!FLOAT_TYPE_P (subt))
11716 return exp;
11718 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11719 return exp;
11721 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11722 return exp;
11724 return strip_float_extensions (sub);
11727 /* Strip out all handled components that produce invariant
11728 offsets. */
11730 const_tree
11731 strip_invariant_refs (const_tree op)
11733 while (handled_component_p (op))
11735 switch (TREE_CODE (op))
11737 case ARRAY_REF:
11738 case ARRAY_RANGE_REF:
11739 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11740 || TREE_OPERAND (op, 2) != NULL_TREE
11741 || TREE_OPERAND (op, 3) != NULL_TREE)
11742 return NULL;
11743 break;
11745 case COMPONENT_REF:
11746 if (TREE_OPERAND (op, 2) != NULL_TREE)
11747 return NULL;
11748 break;
11750 default:;
11752 op = TREE_OPERAND (op, 0);
11755 return op;
11758 static GTY(()) tree gcc_eh_personality_decl;
11760 /* Return the GCC personality function decl. */
11762 tree
11763 lhd_gcc_personality (void)
11765 if (!gcc_eh_personality_decl)
11766 gcc_eh_personality_decl = build_personality_function ("gcc");
11767 return gcc_eh_personality_decl;
11770 /* For languages with One Definition Rule, work out if
11771 trees are actually the same even if the tree representation
11772 differs. This handles only decls appearing in TYPE_NAME
11773 and TYPE_CONTEXT. That is NAMESPACE_DECL, TYPE_DECL,
11774 RECORD_TYPE and IDENTIFIER_NODE. */
11776 static bool
11777 same_for_odr (tree t1, tree t2)
11779 if (t1 == t2)
11780 return true;
11781 if (!t1 || !t2)
11782 return false;
11783 /* C and C++ FEs differ by using IDENTIFIER_NODE and TYPE_DECL. */
11784 if (TREE_CODE (t1) == IDENTIFIER_NODE
11785 && TREE_CODE (t2) == TYPE_DECL
11786 && DECL_FILE_SCOPE_P (t1))
11788 t2 = DECL_NAME (t2);
11789 gcc_assert (TREE_CODE (t2) == IDENTIFIER_NODE);
11791 if (TREE_CODE (t2) == IDENTIFIER_NODE
11792 && TREE_CODE (t1) == TYPE_DECL
11793 && DECL_FILE_SCOPE_P (t2))
11795 t1 = DECL_NAME (t1);
11796 gcc_assert (TREE_CODE (t1) == IDENTIFIER_NODE);
11798 if (TREE_CODE (t1) != TREE_CODE (t2))
11799 return false;
11800 if (TYPE_P (t1))
11801 return types_same_for_odr (t1, t2);
11802 if (DECL_P (t1))
11803 return decls_same_for_odr (t1, t2);
11804 return false;
11807 /* For languages with One Definition Rule, work out if
11808 decls are actually the same even if the tree representation
11809 differs. This handles only decls appearing in TYPE_NAME
11810 and TYPE_CONTEXT. That is NAMESPACE_DECL, TYPE_DECL,
11811 RECORD_TYPE and IDENTIFIER_NODE. */
11813 static bool
11814 decls_same_for_odr (tree decl1, tree decl2)
11816 if (decl1 && TREE_CODE (decl1) == TYPE_DECL
11817 && DECL_ORIGINAL_TYPE (decl1))
11818 decl1 = DECL_ORIGINAL_TYPE (decl1);
11819 if (decl2 && TREE_CODE (decl2) == TYPE_DECL
11820 && DECL_ORIGINAL_TYPE (decl2))
11821 decl2 = DECL_ORIGINAL_TYPE (decl2);
11822 if (decl1 == decl2)
11823 return true;
11824 if (!decl1 || !decl2)
11825 return false;
11826 gcc_checking_assert (DECL_P (decl1) && DECL_P (decl2));
11827 if (TREE_CODE (decl1) != TREE_CODE (decl2))
11828 return false;
11829 if (TREE_CODE (decl1) == TRANSLATION_UNIT_DECL)
11830 return true;
11831 if (TREE_CODE (decl1) != NAMESPACE_DECL
11832 && TREE_CODE (decl1) != TYPE_DECL)
11833 return false;
11834 if (!DECL_NAME (decl1))
11835 return false;
11836 gcc_checking_assert (TREE_CODE (DECL_NAME (decl1)) == IDENTIFIER_NODE);
11837 gcc_checking_assert (!DECL_NAME (decl2)
11838 || TREE_CODE (DECL_NAME (decl2)) == IDENTIFIER_NODE);
11839 if (DECL_NAME (decl1) != DECL_NAME (decl2))
11840 return false;
11841 return same_for_odr (DECL_CONTEXT (decl1),
11842 DECL_CONTEXT (decl2));
11845 /* For languages with One Definition Rule, work out if
11846 types are same even if the tree representation differs.
11847 This is non-trivial for LTO where minnor differences in
11848 the type representation may have prevented type merging
11849 to merge two copies of otherwise equivalent type. */
11851 bool
11852 types_same_for_odr (tree type1, tree type2)
11854 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
11855 type1 = TYPE_MAIN_VARIANT (type1);
11856 type2 = TYPE_MAIN_VARIANT (type2);
11857 if (type1 == type2)
11858 return true;
11860 #ifndef ENABLE_CHECKING
11861 if (!in_lto_p)
11862 return false;
11863 #endif
11865 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
11866 on the corresponding TYPE_STUB_DECL. */
11867 if (type_in_anonymous_namespace_p (type1)
11868 || type_in_anonymous_namespace_p (type2))
11869 return false;
11870 /* When assembler name of virtual table is available, it is
11871 easy to compare types for equivalence. */
11872 if (TYPE_BINFO (type1) && TYPE_BINFO (type2)
11873 && BINFO_VTABLE (TYPE_BINFO (type1))
11874 && BINFO_VTABLE (TYPE_BINFO (type2)))
11876 tree v1 = BINFO_VTABLE (TYPE_BINFO (type1));
11877 tree v2 = BINFO_VTABLE (TYPE_BINFO (type2));
11879 if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
11881 if (TREE_CODE (v2) != POINTER_PLUS_EXPR
11882 || !operand_equal_p (TREE_OPERAND (v1, 1),
11883 TREE_OPERAND (v2, 1), 0))
11884 return false;
11885 v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
11886 v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
11888 v1 = DECL_ASSEMBLER_NAME (v1);
11889 v2 = DECL_ASSEMBLER_NAME (v2);
11890 return (v1 == v2);
11893 /* FIXME: the code comparing type names consider all instantiations of the
11894 same template to have same name. This is because we have no access
11895 to template parameters. For types with no virtual method tables
11896 we thus can return false positives. At the moment we do not need
11897 to compare types in other scenarios than devirtualization. */
11899 /* If types are not structuraly same, do not bother to contnue.
11900 Match in the remainder of code would mean ODR violation. */
11901 if (!types_compatible_p (type1, type2))
11902 return false;
11903 if (!TYPE_NAME (type1))
11904 return false;
11905 if (!decls_same_for_odr (TYPE_NAME (type1), TYPE_NAME (type2)))
11906 return false;
11907 if (!same_for_odr (TYPE_CONTEXT (type1), TYPE_CONTEXT (type2)))
11908 return false;
11909 /* When not in LTO the MAIN_VARIANT check should be the same. */
11910 gcc_assert (in_lto_p || L_IPO_COMP_MODE);
11912 return true;
11915 /* TARGET is a call target of GIMPLE call statement
11916 (obtained by gimple_call_fn). Return true if it is
11917 OBJ_TYPE_REF representing an virtual call of C++ method.
11918 (As opposed to OBJ_TYPE_REF representing objc calls
11919 through a cast where middle-end devirtualization machinery
11920 can't apply.) */
11922 bool
11923 virtual_method_call_p (tree target)
11925 if (TREE_CODE (target) != OBJ_TYPE_REF)
11926 return false;
11927 target = TREE_TYPE (target);
11928 gcc_checking_assert (TREE_CODE (target) == POINTER_TYPE);
11929 target = TREE_TYPE (target);
11930 if (TREE_CODE (target) == FUNCTION_TYPE)
11931 return false;
11932 gcc_checking_assert (TREE_CODE (target) == METHOD_TYPE);
11933 return true;
11936 /* REF is OBJ_TYPE_REF, return the class the ref corresponds to. */
11938 tree
11939 obj_type_ref_class (tree ref)
11941 gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF);
11942 ref = TREE_TYPE (ref);
11943 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
11944 ref = TREE_TYPE (ref);
11945 /* We look for type THIS points to. ObjC also builds
11946 OBJ_TYPE_REF with non-method calls, Their first parameter
11947 ID however also corresponds to class type. */
11948 gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE
11949 || TREE_CODE (ref) == FUNCTION_TYPE);
11950 ref = TREE_VALUE (TYPE_ARG_TYPES (ref));
11951 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
11952 return TREE_TYPE (ref);
11955 /* Return true if T is in anonymous namespace. */
11957 bool
11958 type_in_anonymous_namespace_p (tree t)
11960 return (TYPE_STUB_DECL (t) && !TREE_PUBLIC (TYPE_STUB_DECL (t)));
11963 /* Try to find a base info of BINFO that would have its field decl at offset
11964 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11965 found, return, otherwise return NULL_TREE. */
11967 tree
11968 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
11970 tree type = BINFO_TYPE (binfo);
11972 while (true)
11974 HOST_WIDE_INT pos, size;
11975 tree fld;
11976 int i;
11978 if (types_same_for_odr (type, expected_type))
11979 return binfo;
11980 if (offset < 0)
11981 return NULL_TREE;
11983 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
11985 if (TREE_CODE (fld) != FIELD_DECL)
11986 continue;
11988 pos = int_bit_position (fld);
11989 size = tree_to_uhwi (DECL_SIZE (fld));
11990 if (pos <= offset && (pos + size) > offset)
11991 break;
11993 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11994 return NULL_TREE;
11996 if (!DECL_ARTIFICIAL (fld))
11998 binfo = TYPE_BINFO (TREE_TYPE (fld));
11999 if (!binfo)
12000 return NULL_TREE;
12002 /* Offset 0 indicates the primary base, whose vtable contents are
12003 represented in the binfo for the derived class. */
12004 else if (offset != 0)
12006 tree base_binfo, binfo2 = binfo;
12008 /* Find BINFO corresponding to FLD. This is bit harder
12009 by a fact that in virtual inheritance we may need to walk down
12010 the non-virtual inheritance chain. */
12011 while (true)
12013 tree containing_binfo = NULL, found_binfo = NULL;
12014 for (i = 0; BINFO_BASE_ITERATE (binfo2, i, base_binfo); i++)
12015 if (types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
12017 found_binfo = base_binfo;
12018 break;
12020 else
12021 if ((tree_to_shwi (BINFO_OFFSET (base_binfo))
12022 - tree_to_shwi (BINFO_OFFSET (binfo)))
12023 * BITS_PER_UNIT < pos
12024 /* Rule out types with no virtual methods or we can get confused
12025 here by zero sized bases. */
12026 && BINFO_VTABLE (TYPE_BINFO (BINFO_TYPE (base_binfo)))
12027 && (!containing_binfo
12028 || (tree_to_shwi (BINFO_OFFSET (containing_binfo))
12029 < tree_to_shwi (BINFO_OFFSET (base_binfo)))))
12030 containing_binfo = base_binfo;
12031 if (found_binfo)
12033 binfo = found_binfo;
12034 break;
12036 if (!containing_binfo)
12037 return NULL_TREE;
12038 binfo2 = containing_binfo;
12042 type = TREE_TYPE (fld);
12043 offset -= pos;
12047 /* Returns true if X is a typedef decl. */
12049 bool
12050 is_typedef_decl (tree x)
12052 return (x && TREE_CODE (x) == TYPE_DECL
12053 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
12056 /* Returns true iff TYPE is a type variant created for a typedef. */
12058 bool
12059 typedef_variant_p (tree type)
12061 return is_typedef_decl (TYPE_NAME (type));
12064 /* Warn about a use of an identifier which was marked deprecated. */
12065 void
12066 warn_deprecated_use (tree node, tree attr)
12068 const char *msg;
12070 if (node == 0 || !warn_deprecated_decl)
12071 return;
12073 if (!attr)
12075 if (DECL_P (node))
12076 attr = DECL_ATTRIBUTES (node);
12077 else if (TYPE_P (node))
12079 tree decl = TYPE_STUB_DECL (node);
12080 if (decl)
12081 attr = lookup_attribute ("deprecated",
12082 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
12086 if (attr)
12087 attr = lookup_attribute ("deprecated", attr);
12089 if (attr)
12090 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
12091 else
12092 msg = NULL;
12094 if (DECL_P (node))
12096 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
12097 if (msg)
12098 warning (OPT_Wdeprecated_declarations,
12099 "%qD is deprecated (declared at %r%s:%d%R): %s",
12100 node, "locus", xloc.file, xloc.line, msg);
12101 else
12102 warning (OPT_Wdeprecated_declarations,
12103 "%qD is deprecated (declared at %r%s:%d%R)",
12104 node, "locus", xloc.file, xloc.line);
12106 else if (TYPE_P (node))
12108 tree what = NULL_TREE;
12109 tree decl = TYPE_STUB_DECL (node);
12111 if (TYPE_NAME (node))
12113 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12114 what = TYPE_NAME (node);
12115 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12116 && DECL_NAME (TYPE_NAME (node)))
12117 what = DECL_NAME (TYPE_NAME (node));
12120 if (decl)
12122 expanded_location xloc
12123 = expand_location (DECL_SOURCE_LOCATION (decl));
12124 if (what)
12126 if (msg)
12127 warning (OPT_Wdeprecated_declarations,
12128 "%qE is deprecated (declared at %r%s:%d%R): %s",
12129 what, "locus", xloc.file, xloc.line, msg);
12130 else
12131 warning (OPT_Wdeprecated_declarations,
12132 "%qE is deprecated (declared at %r%s:%d%R)",
12133 what, "locus", xloc.file, xloc.line);
12135 else
12137 if (msg)
12138 warning (OPT_Wdeprecated_declarations,
12139 "type is deprecated (declared at %r%s:%d%R): %s",
12140 "locus", xloc.file, xloc.line, msg);
12141 else
12142 warning (OPT_Wdeprecated_declarations,
12143 "type is deprecated (declared at %r%s:%d%R)",
12144 "locus", xloc.file, xloc.line);
12147 else
12149 if (what)
12151 if (msg)
12152 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
12153 what, msg);
12154 else
12155 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
12157 else
12159 if (msg)
12160 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
12161 msg);
12162 else
12163 warning (OPT_Wdeprecated_declarations, "type is deprecated");
12169 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12170 somewhere in it. */
12172 bool
12173 contains_bitfld_component_ref_p (const_tree ref)
12175 while (handled_component_p (ref))
12177 if (TREE_CODE (ref) == COMPONENT_REF
12178 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12179 return true;
12180 ref = TREE_OPERAND (ref, 0);
12183 return false;
12186 /* Try to determine whether a TRY_CATCH expression can fall through.
12187 This is a subroutine of block_may_fallthru. */
12189 static bool
12190 try_catch_may_fallthru (const_tree stmt)
12192 tree_stmt_iterator i;
12194 /* If the TRY block can fall through, the whole TRY_CATCH can
12195 fall through. */
12196 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
12197 return true;
12199 i = tsi_start (TREE_OPERAND (stmt, 1));
12200 switch (TREE_CODE (tsi_stmt (i)))
12202 case CATCH_EXPR:
12203 /* We expect to see a sequence of CATCH_EXPR trees, each with a
12204 catch expression and a body. The whole TRY_CATCH may fall
12205 through iff any of the catch bodies falls through. */
12206 for (; !tsi_end_p (i); tsi_next (&i))
12208 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
12209 return true;
12211 return false;
12213 case EH_FILTER_EXPR:
12214 /* The exception filter expression only matters if there is an
12215 exception. If the exception does not match EH_FILTER_TYPES,
12216 we will execute EH_FILTER_FAILURE, and we will fall through
12217 if that falls through. If the exception does match
12218 EH_FILTER_TYPES, the stack unwinder will continue up the
12219 stack, so we will not fall through. We don't know whether we
12220 will throw an exception which matches EH_FILTER_TYPES or not,
12221 so we just ignore EH_FILTER_TYPES and assume that we might
12222 throw an exception which doesn't match. */
12223 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
12225 default:
12226 /* This case represents statements to be executed when an
12227 exception occurs. Those statements are implicitly followed
12228 by a RESX statement to resume execution after the exception.
12229 So in this case the TRY_CATCH never falls through. */
12230 return false;
12234 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
12235 need not be 100% accurate; simply be conservative and return true if we
12236 don't know. This is used only to avoid stupidly generating extra code.
12237 If we're wrong, we'll just delete the extra code later. */
12239 bool
12240 block_may_fallthru (const_tree block)
12242 /* This CONST_CAST is okay because expr_last returns its argument
12243 unmodified and we assign it to a const_tree. */
12244 const_tree stmt = expr_last (CONST_CAST_TREE (block));
12246 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
12248 case GOTO_EXPR:
12249 case RETURN_EXPR:
12250 /* Easy cases. If the last statement of the block implies
12251 control transfer, then we can't fall through. */
12252 return false;
12254 case SWITCH_EXPR:
12255 /* If SWITCH_LABELS is set, this is lowered, and represents a
12256 branch to a selected label and hence can not fall through.
12257 Otherwise SWITCH_BODY is set, and the switch can fall
12258 through. */
12259 return SWITCH_LABELS (stmt) == NULL_TREE;
12261 case COND_EXPR:
12262 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
12263 return true;
12264 return block_may_fallthru (COND_EXPR_ELSE (stmt));
12266 case BIND_EXPR:
12267 return block_may_fallthru (BIND_EXPR_BODY (stmt));
12269 case TRY_CATCH_EXPR:
12270 return try_catch_may_fallthru (stmt);
12272 case TRY_FINALLY_EXPR:
12273 /* The finally clause is always executed after the try clause,
12274 so if it does not fall through, then the try-finally will not
12275 fall through. Otherwise, if the try clause does not fall
12276 through, then when the finally clause falls through it will
12277 resume execution wherever the try clause was going. So the
12278 whole try-finally will only fall through if both the try
12279 clause and the finally clause fall through. */
12280 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
12281 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
12283 case MODIFY_EXPR:
12284 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
12285 stmt = TREE_OPERAND (stmt, 1);
12286 else
12287 return true;
12288 /* FALLTHRU */
12290 case CALL_EXPR:
12291 /* Functions that do not return do not fall through. */
12292 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
12294 case CLEANUP_POINT_EXPR:
12295 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12297 case TARGET_EXPR:
12298 return block_may_fallthru (TREE_OPERAND (stmt, 1));
12300 case ERROR_MARK:
12301 return true;
12303 default:
12304 return lang_hooks.block_may_fallthru (stmt);
12308 /* True if we are using EH to handle cleanups. */
12309 static bool using_eh_for_cleanups_flag = false;
12311 /* This routine is called from front ends to indicate eh should be used for
12312 cleanups. */
12313 void
12314 using_eh_for_cleanups (void)
12316 using_eh_for_cleanups_flag = true;
12319 /* Query whether EH is used for cleanups. */
12320 bool
12321 using_eh_for_cleanups_p (void)
12323 return using_eh_for_cleanups_flag;
12326 /* Wrapper for tree_code_name to ensure that tree code is valid */
12327 const char *
12328 get_tree_code_name (enum tree_code code)
12330 const char *invalid = "<invalid tree code>";
12332 if (code >= MAX_TREE_CODES)
12333 return invalid;
12335 return tree_code_name[code];
12338 /* Drops the TREE_OVERFLOW flag from T. */
12340 tree
12341 drop_tree_overflow (tree t)
12343 gcc_checking_assert (TREE_OVERFLOW (t));
12345 /* For tree codes with a sharing machinery re-build the result. */
12346 if (TREE_CODE (t) == INTEGER_CST)
12347 return build_int_cst_wide (TREE_TYPE (t),
12348 TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t));
12350 /* Otherwise, as all tcc_constants are possibly shared, copy the node
12351 and drop the flag. */
12352 t = copy_node (t);
12353 TREE_OVERFLOW (t) = 0;
12354 return t;
12357 /* Given a memory reference expression T, return its base address.
12358 The base address of a memory reference expression is the main
12359 object being referenced. For instance, the base address for
12360 'array[i].fld[j]' is 'array'. You can think of this as stripping
12361 away the offset part from a memory address.
12363 This function calls handled_component_p to strip away all the inner
12364 parts of the memory reference until it reaches the base object. */
12366 tree
12367 get_base_address (tree t)
12369 while (handled_component_p (t))
12370 t = TREE_OPERAND (t, 0);
12372 if ((TREE_CODE (t) == MEM_REF
12373 || TREE_CODE (t) == TARGET_MEM_REF)
12374 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
12375 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
12377 /* ??? Either the alias oracle or all callers need to properly deal
12378 with WITH_SIZE_EXPRs before we can look through those. */
12379 if (TREE_CODE (t) == WITH_SIZE_EXPR)
12380 return NULL_TREE;
12382 return t;
12385 #include "gt-tree.h"