svn merge -r 217500:218679 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / gcc / tree.c
blobcbb7d9096769dde37ef1790ff31bf5eab8a6a38c
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 "hashtab.h"
42 #include "hash-set.h"
43 #include "vec.h"
44 #include "machmode.h"
45 #include "hard-reg-set.h"
46 #include "input.h"
47 #include "function.h"
48 #include "obstack.h"
49 #include "toplev.h" /* get_random_seed */
50 #include "inchash.h"
51 #include "filenames.h"
52 #include "output.h"
53 #include "target.h"
54 #include "common/common-target.h"
55 #include "langhooks.h"
56 #include "tree-inline.h"
57 #include "tree-iterator.h"
58 #include "predict.h"
59 #include "dominance.h"
60 #include "cfg.h"
61 #include "basic-block.h"
62 #include "bitmap.h"
63 #include "tree-ssa-alias.h"
64 #include "internal-fn.h"
65 #include "gimple-expr.h"
66 #include "is-a.h"
67 #include "gimple.h"
68 #include "gimple-iterator.h"
69 #include "gimplify.h"
70 #include "gimple-ssa.h"
71 #include "hash-map.h"
72 #include "plugin-api.h"
73 #include "ipa-ref.h"
74 #include "cgraph.h"
75 #include "tree-phinodes.h"
76 #include "stringpool.h"
77 #include "tree-ssanames.h"
78 #include "expr.h"
79 #include "tree-dfa.h"
80 #include "params.h"
81 #include "tree-pass.h"
82 #include "langhooks-def.h"
83 #include "diagnostic.h"
84 #include "tree-diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "except.h"
87 #include "debug.h"
88 #include "intl.h"
89 #include "wide-int.h"
90 #include "builtins.h"
92 /* Tree code classes. */
94 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
95 #define END_OF_BASE_TREE_CODES tcc_exceptional,
97 const enum tree_code_class tree_code_type[] = {
98 #include "all-tree.def"
101 #undef DEFTREECODE
102 #undef END_OF_BASE_TREE_CODES
104 /* Table indexed by tree code giving number of expression
105 operands beyond the fixed part of the node structure.
106 Not used for types or decls. */
108 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
109 #define END_OF_BASE_TREE_CODES 0,
111 const unsigned char tree_code_length[] = {
112 #include "all-tree.def"
115 #undef DEFTREECODE
116 #undef END_OF_BASE_TREE_CODES
118 /* Names of tree components.
119 Used for printing out the tree and error messages. */
120 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
121 #define END_OF_BASE_TREE_CODES "@dummy",
123 static const char *const tree_code_name[] = {
124 #include "all-tree.def"
127 #undef DEFTREECODE
128 #undef END_OF_BASE_TREE_CODES
130 /* Each tree code class has an associated string representation.
131 These must correspond to the tree_code_class entries. */
133 const char *const tree_code_class_strings[] =
135 "exceptional",
136 "constant",
137 "type",
138 "declaration",
139 "reference",
140 "comparison",
141 "unary",
142 "binary",
143 "statement",
144 "vl_exp",
145 "expression"
148 /* obstack.[ch] explicitly declined to prototype this. */
149 extern int _obstack_allocated_p (struct obstack *h, void *obj);
151 /* Statistics-gathering stuff. */
153 static int tree_code_counts[MAX_TREE_CODES];
154 int tree_node_counts[(int) all_kinds];
155 int tree_node_sizes[(int) all_kinds];
157 /* Keep in sync with tree.h:enum tree_node_kind. */
158 static const char * const tree_node_kind_names[] = {
159 "decls",
160 "types",
161 "blocks",
162 "stmts",
163 "refs",
164 "exprs",
165 "constants",
166 "identifiers",
167 "vecs",
168 "binfos",
169 "ssa names",
170 "constructors",
171 "random kinds",
172 "lang_decl kinds",
173 "lang_type kinds",
174 "omp clauses",
177 /* Unique id for next decl created. */
178 static GTY(()) int next_decl_uid;
179 /* Unique id for next type created. */
180 static GTY(()) int next_type_uid = 1;
181 /* Unique id for next debug decl created. Use negative numbers,
182 to catch erroneous uses. */
183 static GTY(()) int next_debug_decl_uid;
185 /* Since we cannot rehash a type after it is in the table, we have to
186 keep the hash code. */
188 struct GTY((for_user)) type_hash {
189 unsigned long hash;
190 tree type;
193 /* Initial size of the hash table (rounded to next prime). */
194 #define TYPE_HASH_INITIAL_SIZE 1000
196 struct type_cache_hasher : ggc_cache_hasher<type_hash *>
198 static hashval_t hash (type_hash *t) { return t->hash; }
199 static bool equal (type_hash *a, type_hash *b);
201 static void
202 handle_cache_entry (type_hash *&t)
204 extern void gt_ggc_mx (type_hash *&);
205 if (t == HTAB_DELETED_ENTRY || t == HTAB_EMPTY_ENTRY)
206 return;
207 else if (ggc_marked_p (t->type))
208 gt_ggc_mx (t);
209 else
210 t = static_cast<type_hash *> (HTAB_DELETED_ENTRY);
214 /* Now here is the hash table. When recording a type, it is added to
215 the slot whose index is the hash code. Note that the hash table is
216 used for several kinds of types (function types, array types and
217 array index range types, for now). While all these live in the
218 same table, they are completely independent, and the hash code is
219 computed differently for each of these. */
221 static GTY ((cache)) hash_table<type_cache_hasher> *type_hash_table;
223 /* Hash table and temporary node for larger integer const values. */
224 static GTY (()) tree int_cst_node;
226 struct int_cst_hasher : ggc_cache_hasher<tree>
228 static hashval_t hash (tree t);
229 static bool equal (tree x, tree y);
232 static GTY ((cache)) hash_table<int_cst_hasher> *int_cst_hash_table;
234 /* Hash table for optimization flags and target option flags. Use the same
235 hash table for both sets of options. Nodes for building the current
236 optimization and target option nodes. The assumption is most of the time
237 the options created will already be in the hash table, so we avoid
238 allocating and freeing up a node repeatably. */
239 static GTY (()) tree cl_optimization_node;
240 static GTY (()) tree cl_target_option_node;
242 struct cl_option_hasher : ggc_cache_hasher<tree>
244 static hashval_t hash (tree t);
245 static bool equal (tree x, tree y);
248 static GTY ((cache)) hash_table<cl_option_hasher> *cl_option_hash_table;
250 /* General tree->tree mapping structure for use in hash tables. */
253 static GTY ((cache))
254 hash_table<tree_decl_map_cache_hasher> *debug_expr_for_decl;
256 static GTY ((cache))
257 hash_table<tree_decl_map_cache_hasher> *value_expr_for_decl;
259 struct tree_vec_map_cache_hasher : ggc_cache_hasher<tree_vec_map *>
261 static hashval_t hash (tree_vec_map *m) { return DECL_UID (m->base.from); }
263 static bool
264 equal (tree_vec_map *a, tree_vec_map *b)
266 return a->base.from == b->base.from;
269 static void
270 handle_cache_entry (tree_vec_map *&m)
272 extern void gt_ggc_mx (tree_vec_map *&);
273 if (m == HTAB_EMPTY_ENTRY || m == HTAB_DELETED_ENTRY)
274 return;
275 else if (ggc_marked_p (m->base.from))
276 gt_ggc_mx (m);
277 else
278 m = static_cast<tree_vec_map *> (HTAB_DELETED_ENTRY);
282 static GTY ((cache))
283 hash_table<tree_vec_map_cache_hasher> *debug_args_for_decl;
285 static void set_type_quals (tree, int);
286 static void print_type_hash_statistics (void);
287 static void print_debug_expr_statistics (void);
288 static void print_value_expr_statistics (void);
289 static void type_hash_list (const_tree, inchash::hash &);
290 static void attribute_hash_list (const_tree, inchash::hash &);
292 tree global_trees[TI_MAX];
293 tree integer_types[itk_none];
295 bool int_n_enabled_p[NUM_INT_N_ENTS];
296 struct int_n_trees_t int_n_trees [NUM_INT_N_ENTS];
298 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
300 /* Number of operands for each OpenMP clause. */
301 unsigned const char omp_clause_num_ops[] =
303 0, /* OMP_CLAUSE_ERROR */
304 1, /* OMP_CLAUSE_PRIVATE */
305 1, /* OMP_CLAUSE_SHARED */
306 1, /* OMP_CLAUSE_FIRSTPRIVATE */
307 2, /* OMP_CLAUSE_LASTPRIVATE */
308 4, /* OMP_CLAUSE_REDUCTION */
309 1, /* OMP_CLAUSE_COPYIN */
310 1, /* OMP_CLAUSE_COPYPRIVATE */
311 3, /* OMP_CLAUSE_LINEAR */
312 2, /* OMP_CLAUSE_ALIGNED */
313 1, /* OMP_CLAUSE_DEPEND */
314 1, /* OMP_CLAUSE_UNIFORM */
315 2, /* OMP_CLAUSE_FROM */
316 2, /* OMP_CLAUSE_TO */
317 2, /* OMP_CLAUSE_MAP */
318 2, /* OMP_CLAUSE__CACHE_ */
319 1, /* OMP_CLAUSE_DEVICE_RESIDENT */
320 1, /* OMP_CLAUSE_USE_DEVICE */
321 1, /* OMP_CLAUSE_GANG */
322 1, /* OMP_CLAUSE_ASYNC */
323 1, /* OMP_CLAUSE_WAIT */
324 1, /* OMP_CLAUSE__LOOPTEMP_ */
325 1, /* OMP_CLAUSE_IF */
326 1, /* OMP_CLAUSE_NUM_THREADS */
327 1, /* OMP_CLAUSE_SCHEDULE */
328 0, /* OMP_CLAUSE_NOWAIT */
329 0, /* OMP_CLAUSE_ORDERED */
330 0, /* OMP_CLAUSE_DEFAULT */
331 3, /* OMP_CLAUSE_COLLAPSE */
332 0, /* OMP_CLAUSE_UNTIED */
333 1, /* OMP_CLAUSE_FINAL */
334 0, /* OMP_CLAUSE_MERGEABLE */
335 1, /* OMP_CLAUSE_DEVICE */
336 1, /* OMP_CLAUSE_DIST_SCHEDULE */
337 0, /* OMP_CLAUSE_INBRANCH */
338 0, /* OMP_CLAUSE_NOTINBRANCH */
339 1, /* OMP_CLAUSE_NUM_TEAMS */
340 1, /* OMP_CLAUSE_THREAD_LIMIT */
341 0, /* OMP_CLAUSE_PROC_BIND */
342 1, /* OMP_CLAUSE_SAFELEN */
343 1, /* OMP_CLAUSE_SIMDLEN */
344 0, /* OMP_CLAUSE_FOR */
345 0, /* OMP_CLAUSE_PARALLEL */
346 0, /* OMP_CLAUSE_SECTIONS */
347 0, /* OMP_CLAUSE_TASKGROUP */
348 1, /* OMP_CLAUSE__SIMDUID_ */
349 1, /* OMP_CLAUSE__CILK_FOR_COUNT_ */
350 0, /* OMP_CLAUSE_INDEPENDENT */
351 1, /* OMP_CLAUSE_WORKER */
352 1, /* OMP_CLAUSE_VECTOR */
353 1, /* OMP_CLAUSE_NUM_GANGS */
354 1, /* OMP_CLAUSE_NUM_WORKERS */
355 1, /* OMP_CLAUSE_VECTOR_LENGTH */
358 const char * const omp_clause_code_name[] =
360 "error_clause",
361 "private",
362 "shared",
363 "firstprivate",
364 "lastprivate",
365 "reduction",
366 "copyin",
367 "copyprivate",
368 "linear",
369 "aligned",
370 "depend",
371 "uniform",
372 "from",
373 "to",
374 "map",
375 "_cache_",
376 "device_resident",
377 "use_device",
378 "gang",
379 "async",
380 "wait",
381 "_looptemp_",
382 "if",
383 "num_threads",
384 "schedule",
385 "nowait",
386 "ordered",
387 "default",
388 "collapse",
389 "untied",
390 "final",
391 "mergeable",
392 "device",
393 "dist_schedule",
394 "inbranch",
395 "notinbranch",
396 "num_teams",
397 "thread_limit",
398 "proc_bind",
399 "safelen",
400 "simdlen",
401 "for",
402 "parallel",
403 "sections",
404 "taskgroup",
405 "_simduid_",
406 "_Cilk_for_count_",
407 "independent",
408 "worker",
409 "vector",
410 "num_gangs",
411 "num_workers",
412 "vector_length"
416 /* Return the tree node structure used by tree code CODE. */
418 static inline enum tree_node_structure_enum
419 tree_node_structure_for_code (enum tree_code code)
421 switch (TREE_CODE_CLASS (code))
423 case tcc_declaration:
425 switch (code)
427 case FIELD_DECL:
428 return TS_FIELD_DECL;
429 case PARM_DECL:
430 return TS_PARM_DECL;
431 case VAR_DECL:
432 return TS_VAR_DECL;
433 case LABEL_DECL:
434 return TS_LABEL_DECL;
435 case RESULT_DECL:
436 return TS_RESULT_DECL;
437 case DEBUG_EXPR_DECL:
438 return TS_DECL_WRTL;
439 case CONST_DECL:
440 return TS_CONST_DECL;
441 case TYPE_DECL:
442 return TS_TYPE_DECL;
443 case FUNCTION_DECL:
444 return TS_FUNCTION_DECL;
445 case TRANSLATION_UNIT_DECL:
446 return TS_TRANSLATION_UNIT_DECL;
447 default:
448 return TS_DECL_NON_COMMON;
451 case tcc_type:
452 return TS_TYPE_NON_COMMON;
453 case tcc_reference:
454 case tcc_comparison:
455 case tcc_unary:
456 case tcc_binary:
457 case tcc_expression:
458 case tcc_statement:
459 case tcc_vl_exp:
460 return TS_EXP;
461 default: /* tcc_constant and tcc_exceptional */
462 break;
464 switch (code)
466 /* tcc_constant cases. */
467 case VOID_CST: return TS_TYPED;
468 case INTEGER_CST: return TS_INT_CST;
469 case REAL_CST: return TS_REAL_CST;
470 case FIXED_CST: return TS_FIXED_CST;
471 case COMPLEX_CST: return TS_COMPLEX;
472 case VECTOR_CST: return TS_VECTOR;
473 case STRING_CST: return TS_STRING;
474 /* tcc_exceptional cases. */
475 case ERROR_MARK: return TS_COMMON;
476 case IDENTIFIER_NODE: return TS_IDENTIFIER;
477 case TREE_LIST: return TS_LIST;
478 case TREE_VEC: return TS_VEC;
479 case SSA_NAME: return TS_SSA_NAME;
480 case PLACEHOLDER_EXPR: return TS_COMMON;
481 case STATEMENT_LIST: return TS_STATEMENT_LIST;
482 case BLOCK: return TS_BLOCK;
483 case CONSTRUCTOR: return TS_CONSTRUCTOR;
484 case TREE_BINFO: return TS_BINFO;
485 case OMP_CLAUSE: return TS_OMP_CLAUSE;
486 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
487 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
489 default:
490 gcc_unreachable ();
495 /* Initialize tree_contains_struct to describe the hierarchy of tree
496 nodes. */
498 static void
499 initialize_tree_contains_struct (void)
501 unsigned i;
503 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
505 enum tree_code code;
506 enum tree_node_structure_enum ts_code;
508 code = (enum tree_code) i;
509 ts_code = tree_node_structure_for_code (code);
511 /* Mark the TS structure itself. */
512 tree_contains_struct[code][ts_code] = 1;
514 /* Mark all the structures that TS is derived from. */
515 switch (ts_code)
517 case TS_TYPED:
518 case TS_BLOCK:
519 MARK_TS_BASE (code);
520 break;
522 case TS_COMMON:
523 case TS_INT_CST:
524 case TS_REAL_CST:
525 case TS_FIXED_CST:
526 case TS_VECTOR:
527 case TS_STRING:
528 case TS_COMPLEX:
529 case TS_SSA_NAME:
530 case TS_CONSTRUCTOR:
531 case TS_EXP:
532 case TS_STATEMENT_LIST:
533 MARK_TS_TYPED (code);
534 break;
536 case TS_IDENTIFIER:
537 case TS_DECL_MINIMAL:
538 case TS_TYPE_COMMON:
539 case TS_LIST:
540 case TS_VEC:
541 case TS_BINFO:
542 case TS_OMP_CLAUSE:
543 case TS_OPTIMIZATION:
544 case TS_TARGET_OPTION:
545 MARK_TS_COMMON (code);
546 break;
548 case TS_TYPE_WITH_LANG_SPECIFIC:
549 MARK_TS_TYPE_COMMON (code);
550 break;
552 case TS_TYPE_NON_COMMON:
553 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
554 break;
556 case TS_DECL_COMMON:
557 MARK_TS_DECL_MINIMAL (code);
558 break;
560 case TS_DECL_WRTL:
561 case TS_CONST_DECL:
562 MARK_TS_DECL_COMMON (code);
563 break;
565 case TS_DECL_NON_COMMON:
566 MARK_TS_DECL_WITH_VIS (code);
567 break;
569 case TS_DECL_WITH_VIS:
570 case TS_PARM_DECL:
571 case TS_LABEL_DECL:
572 case TS_RESULT_DECL:
573 MARK_TS_DECL_WRTL (code);
574 break;
576 case TS_FIELD_DECL:
577 MARK_TS_DECL_COMMON (code);
578 break;
580 case TS_VAR_DECL:
581 MARK_TS_DECL_WITH_VIS (code);
582 break;
584 case TS_TYPE_DECL:
585 case TS_FUNCTION_DECL:
586 MARK_TS_DECL_NON_COMMON (code);
587 break;
589 case TS_TRANSLATION_UNIT_DECL:
590 MARK_TS_DECL_COMMON (code);
591 break;
593 default:
594 gcc_unreachable ();
598 /* Basic consistency checks for attributes used in fold. */
599 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
600 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
601 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
602 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
603 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
604 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
605 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
606 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
607 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
608 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
609 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
610 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
611 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
612 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
613 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
614 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
615 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
616 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
617 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
618 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
619 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
620 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
621 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
622 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
623 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
624 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
625 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
626 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
627 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
628 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
629 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
630 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
631 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
632 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
633 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
634 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
635 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
636 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
637 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_MINIMAL]);
638 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_COMMON]);
642 /* Init tree.c. */
644 void
645 init_ttree (void)
647 /* Initialize the hash table of types. */
648 type_hash_table
649 = hash_table<type_cache_hasher>::create_ggc (TYPE_HASH_INITIAL_SIZE);
651 debug_expr_for_decl
652 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
654 value_expr_for_decl
655 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
657 int_cst_hash_table = hash_table<int_cst_hasher>::create_ggc (1024);
659 int_cst_node = make_int_cst (1, 1);
661 cl_option_hash_table = hash_table<cl_option_hasher>::create_ggc (64);
663 cl_optimization_node = make_node (OPTIMIZATION_NODE);
664 cl_target_option_node = make_node (TARGET_OPTION_NODE);
666 /* Initialize the tree_contains_struct array. */
667 initialize_tree_contains_struct ();
668 lang_hooks.init_ts ();
672 /* The name of the object as the assembler will see it (but before any
673 translations made by ASM_OUTPUT_LABELREF). Often this is the same
674 as DECL_NAME. It is an IDENTIFIER_NODE. */
675 tree
676 decl_assembler_name (tree decl)
678 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
679 lang_hooks.set_decl_assembler_name (decl);
680 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
683 /* When the target supports COMDAT groups, this indicates which group the
684 DECL is associated with. This can be either an IDENTIFIER_NODE or a
685 decl, in which case its DECL_ASSEMBLER_NAME identifies the group. */
686 tree
687 decl_comdat_group (const_tree node)
689 struct symtab_node *snode = symtab_node::get (node);
690 if (!snode)
691 return NULL;
692 return snode->get_comdat_group ();
695 /* Likewise, but make sure it's been reduced to an IDENTIFIER_NODE. */
696 tree
697 decl_comdat_group_id (const_tree node)
699 struct symtab_node *snode = symtab_node::get (node);
700 if (!snode)
701 return NULL;
702 return snode->get_comdat_group_id ();
705 /* When the target supports named section, return its name as IDENTIFIER_NODE
706 or NULL if it is in no section. */
707 const char *
708 decl_section_name (const_tree node)
710 struct symtab_node *snode = symtab_node::get (node);
711 if (!snode)
712 return NULL;
713 return snode->get_section ();
716 /* Set section section name of NODE to VALUE (that is expected to
717 be identifier node) */
718 void
719 set_decl_section_name (tree node, const char *value)
721 struct symtab_node *snode;
723 if (value == NULL)
725 snode = symtab_node::get (node);
726 if (!snode)
727 return;
729 else if (TREE_CODE (node) == VAR_DECL)
730 snode = varpool_node::get_create (node);
731 else
732 snode = cgraph_node::get_create (node);
733 snode->set_section (value);
736 /* Return TLS model of a variable NODE. */
737 enum tls_model
738 decl_tls_model (const_tree node)
740 struct varpool_node *snode = varpool_node::get (node);
741 if (!snode)
742 return TLS_MODEL_NONE;
743 return snode->tls_model;
746 /* Set TLS model of variable NODE to MODEL. */
747 void
748 set_decl_tls_model (tree node, enum tls_model model)
750 struct varpool_node *vnode;
752 if (model == TLS_MODEL_NONE)
754 vnode = varpool_node::get (node);
755 if (!vnode)
756 return;
758 else
759 vnode = varpool_node::get_create (node);
760 vnode->tls_model = model;
763 /* Compute the number of bytes occupied by a tree with code CODE.
764 This function cannot be used for nodes that have variable sizes,
765 including TREE_VEC, INTEGER_CST, STRING_CST, and CALL_EXPR. */
766 size_t
767 tree_code_size (enum tree_code code)
769 switch (TREE_CODE_CLASS (code))
771 case tcc_declaration: /* A decl node */
773 switch (code)
775 case FIELD_DECL:
776 return sizeof (struct tree_field_decl);
777 case PARM_DECL:
778 return sizeof (struct tree_parm_decl);
779 case VAR_DECL:
780 return sizeof (struct tree_var_decl);
781 case LABEL_DECL:
782 return sizeof (struct tree_label_decl);
783 case RESULT_DECL:
784 return sizeof (struct tree_result_decl);
785 case CONST_DECL:
786 return sizeof (struct tree_const_decl);
787 case TYPE_DECL:
788 return sizeof (struct tree_type_decl);
789 case FUNCTION_DECL:
790 return sizeof (struct tree_function_decl);
791 case DEBUG_EXPR_DECL:
792 return sizeof (struct tree_decl_with_rtl);
793 case TRANSLATION_UNIT_DECL:
794 return sizeof (struct tree_translation_unit_decl);
795 case NAMESPACE_DECL:
796 case IMPORTED_DECL:
797 case NAMELIST_DECL:
798 return sizeof (struct tree_decl_non_common);
799 default:
800 return lang_hooks.tree_size (code);
804 case tcc_type: /* a type node */
805 return sizeof (struct tree_type_non_common);
807 case tcc_reference: /* a reference */
808 case tcc_expression: /* an expression */
809 case tcc_statement: /* an expression with side effects */
810 case tcc_comparison: /* a comparison expression */
811 case tcc_unary: /* a unary arithmetic expression */
812 case tcc_binary: /* a binary arithmetic expression */
813 return (sizeof (struct tree_exp)
814 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
816 case tcc_constant: /* a constant */
817 switch (code)
819 case VOID_CST: return sizeof (struct tree_typed);
820 case INTEGER_CST: gcc_unreachable ();
821 case REAL_CST: return sizeof (struct tree_real_cst);
822 case FIXED_CST: return sizeof (struct tree_fixed_cst);
823 case COMPLEX_CST: return sizeof (struct tree_complex);
824 case VECTOR_CST: return sizeof (struct tree_vector);
825 case STRING_CST: gcc_unreachable ();
826 default:
827 return lang_hooks.tree_size (code);
830 case tcc_exceptional: /* something random, like an identifier. */
831 switch (code)
833 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
834 case TREE_LIST: return sizeof (struct tree_list);
836 case ERROR_MARK:
837 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
839 case TREE_VEC:
840 case OMP_CLAUSE: gcc_unreachable ();
842 case SSA_NAME: return sizeof (struct tree_ssa_name);
844 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
845 case BLOCK: return sizeof (struct tree_block);
846 case CONSTRUCTOR: return sizeof (struct tree_constructor);
847 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
848 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
850 default:
851 return lang_hooks.tree_size (code);
854 default:
855 gcc_unreachable ();
859 /* Compute the number of bytes occupied by NODE. This routine only
860 looks at TREE_CODE, except for those nodes that have variable sizes. */
861 size_t
862 tree_size (const_tree node)
864 const enum tree_code code = TREE_CODE (node);
865 switch (code)
867 case INTEGER_CST:
868 return (sizeof (struct tree_int_cst)
869 + (TREE_INT_CST_EXT_NUNITS (node) - 1) * sizeof (HOST_WIDE_INT));
871 case TREE_BINFO:
872 return (offsetof (struct tree_binfo, base_binfos)
873 + vec<tree, va_gc>
874 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
876 case TREE_VEC:
877 return (sizeof (struct tree_vec)
878 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
880 case VECTOR_CST:
881 return (sizeof (struct tree_vector)
882 + (TYPE_VECTOR_SUBPARTS (TREE_TYPE (node)) - 1) * sizeof (tree));
884 case STRING_CST:
885 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
887 case OMP_CLAUSE:
888 return (sizeof (struct tree_omp_clause)
889 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
890 * sizeof (tree));
892 default:
893 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
894 return (sizeof (struct tree_exp)
895 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
896 else
897 return tree_code_size (code);
901 /* Record interesting allocation statistics for a tree node with CODE
902 and LENGTH. */
904 static void
905 record_node_allocation_statistics (enum tree_code code ATTRIBUTE_UNUSED,
906 size_t length ATTRIBUTE_UNUSED)
908 enum tree_code_class type = TREE_CODE_CLASS (code);
909 tree_node_kind kind;
911 if (!GATHER_STATISTICS)
912 return;
914 switch (type)
916 case tcc_declaration: /* A decl node */
917 kind = d_kind;
918 break;
920 case tcc_type: /* a type node */
921 kind = t_kind;
922 break;
924 case tcc_statement: /* an expression with side effects */
925 kind = s_kind;
926 break;
928 case tcc_reference: /* a reference */
929 kind = r_kind;
930 break;
932 case tcc_expression: /* an expression */
933 case tcc_comparison: /* a comparison expression */
934 case tcc_unary: /* a unary arithmetic expression */
935 case tcc_binary: /* a binary arithmetic expression */
936 kind = e_kind;
937 break;
939 case tcc_constant: /* a constant */
940 kind = c_kind;
941 break;
943 case tcc_exceptional: /* something random, like an identifier. */
944 switch (code)
946 case IDENTIFIER_NODE:
947 kind = id_kind;
948 break;
950 case TREE_VEC:
951 kind = vec_kind;
952 break;
954 case TREE_BINFO:
955 kind = binfo_kind;
956 break;
958 case SSA_NAME:
959 kind = ssa_name_kind;
960 break;
962 case BLOCK:
963 kind = b_kind;
964 break;
966 case CONSTRUCTOR:
967 kind = constr_kind;
968 break;
970 case OMP_CLAUSE:
971 kind = omp_clause_kind;
972 break;
974 default:
975 kind = x_kind;
976 break;
978 break;
980 case tcc_vl_exp:
981 kind = e_kind;
982 break;
984 default:
985 gcc_unreachable ();
988 tree_code_counts[(int) code]++;
989 tree_node_counts[(int) kind]++;
990 tree_node_sizes[(int) kind] += length;
993 /* Allocate and return a new UID from the DECL_UID namespace. */
996 allocate_decl_uid (void)
998 return next_decl_uid++;
1001 /* Return a newly allocated node of code CODE. For decl and type
1002 nodes, some other fields are initialized. The rest of the node is
1003 initialized to zero. This function cannot be used for TREE_VEC,
1004 INTEGER_CST or OMP_CLAUSE nodes, which is enforced by asserts in
1005 tree_code_size.
1007 Achoo! I got a code in the node. */
1009 tree
1010 make_node_stat (enum tree_code code MEM_STAT_DECL)
1012 tree t;
1013 enum tree_code_class type = TREE_CODE_CLASS (code);
1014 size_t length = tree_code_size (code);
1016 record_node_allocation_statistics (code, length);
1018 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1019 TREE_SET_CODE (t, code);
1021 switch (type)
1023 case tcc_statement:
1024 TREE_SIDE_EFFECTS (t) = 1;
1025 break;
1027 case tcc_declaration:
1028 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1030 if (code == FUNCTION_DECL)
1032 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
1033 DECL_MODE (t) = FUNCTION_MODE;
1035 else
1036 DECL_ALIGN (t) = 1;
1038 DECL_SOURCE_LOCATION (t) = input_location;
1039 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
1040 DECL_UID (t) = --next_debug_decl_uid;
1041 else
1043 DECL_UID (t) = allocate_decl_uid ();
1044 SET_DECL_PT_UID (t, -1);
1046 if (TREE_CODE (t) == LABEL_DECL)
1047 LABEL_DECL_UID (t) = -1;
1049 break;
1051 case tcc_type:
1052 TYPE_UID (t) = next_type_uid++;
1053 TYPE_ALIGN (t) = BITS_PER_UNIT;
1054 TYPE_USER_ALIGN (t) = 0;
1055 TYPE_MAIN_VARIANT (t) = t;
1056 TYPE_CANONICAL (t) = t;
1058 /* Default to no attributes for type, but let target change that. */
1059 TYPE_ATTRIBUTES (t) = NULL_TREE;
1060 targetm.set_default_type_attributes (t);
1062 /* We have not yet computed the alias set for this type. */
1063 TYPE_ALIAS_SET (t) = -1;
1064 break;
1066 case tcc_constant:
1067 TREE_CONSTANT (t) = 1;
1068 break;
1070 case tcc_expression:
1071 switch (code)
1073 case INIT_EXPR:
1074 case MODIFY_EXPR:
1075 case VA_ARG_EXPR:
1076 case PREDECREMENT_EXPR:
1077 case PREINCREMENT_EXPR:
1078 case POSTDECREMENT_EXPR:
1079 case POSTINCREMENT_EXPR:
1080 /* All of these have side-effects, no matter what their
1081 operands are. */
1082 TREE_SIDE_EFFECTS (t) = 1;
1083 break;
1085 default:
1086 break;
1088 break;
1090 default:
1091 /* Other classes need no special treatment. */
1092 break;
1095 return t;
1098 /* Return a new node with the same contents as NODE except that its
1099 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
1101 tree
1102 copy_node_stat (tree node MEM_STAT_DECL)
1104 tree t;
1105 enum tree_code code = TREE_CODE (node);
1106 size_t length;
1108 gcc_assert (code != STATEMENT_LIST);
1110 length = tree_size (node);
1111 record_node_allocation_statistics (code, length);
1112 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1113 memcpy (t, node, length);
1115 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
1116 TREE_CHAIN (t) = 0;
1117 TREE_ASM_WRITTEN (t) = 0;
1118 TREE_VISITED (t) = 0;
1120 if (TREE_CODE_CLASS (code) == tcc_declaration)
1122 if (code == DEBUG_EXPR_DECL)
1123 DECL_UID (t) = --next_debug_decl_uid;
1124 else
1126 DECL_UID (t) = allocate_decl_uid ();
1127 if (DECL_PT_UID_SET_P (node))
1128 SET_DECL_PT_UID (t, DECL_PT_UID (node));
1130 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
1131 && DECL_HAS_VALUE_EXPR_P (node))
1133 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
1134 DECL_HAS_VALUE_EXPR_P (t) = 1;
1136 /* DECL_DEBUG_EXPR is copied explicitely by callers. */
1137 if (TREE_CODE (node) == VAR_DECL)
1139 DECL_HAS_DEBUG_EXPR_P (t) = 0;
1140 t->decl_with_vis.symtab_node = NULL;
1142 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
1144 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
1145 DECL_HAS_INIT_PRIORITY_P (t) = 1;
1147 if (TREE_CODE (node) == FUNCTION_DECL)
1149 DECL_STRUCT_FUNCTION (t) = NULL;
1150 t->decl_with_vis.symtab_node = NULL;
1153 else if (TREE_CODE_CLASS (code) == tcc_type)
1155 TYPE_UID (t) = next_type_uid++;
1156 /* The following is so that the debug code for
1157 the copy is different from the original type.
1158 The two statements usually duplicate each other
1159 (because they clear fields of the same union),
1160 but the optimizer should catch that. */
1161 TYPE_SYMTAB_POINTER (t) = 0;
1162 TYPE_SYMTAB_ADDRESS (t) = 0;
1164 /* Do not copy the values cache. */
1165 if (TYPE_CACHED_VALUES_P (t))
1167 TYPE_CACHED_VALUES_P (t) = 0;
1168 TYPE_CACHED_VALUES (t) = NULL_TREE;
1172 return t;
1175 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1176 For example, this can copy a list made of TREE_LIST nodes. */
1178 tree
1179 copy_list (tree list)
1181 tree head;
1182 tree prev, next;
1184 if (list == 0)
1185 return 0;
1187 head = prev = copy_node (list);
1188 next = TREE_CHAIN (list);
1189 while (next)
1191 TREE_CHAIN (prev) = copy_node (next);
1192 prev = TREE_CHAIN (prev);
1193 next = TREE_CHAIN (next);
1195 return head;
1199 /* Return the value that TREE_INT_CST_EXT_NUNITS should have for an
1200 INTEGER_CST with value CST and type TYPE. */
1202 static unsigned int
1203 get_int_cst_ext_nunits (tree type, const wide_int &cst)
1205 gcc_checking_assert (cst.get_precision () == TYPE_PRECISION (type));
1206 /* We need an extra zero HWI if CST is an unsigned integer with its
1207 upper bit set, and if CST occupies a whole number of HWIs. */
1208 if (TYPE_UNSIGNED (type)
1209 && wi::neg_p (cst)
1210 && (cst.get_precision () % HOST_BITS_PER_WIDE_INT) == 0)
1211 return cst.get_precision () / HOST_BITS_PER_WIDE_INT + 1;
1212 return cst.get_len ();
1215 /* Return a new INTEGER_CST with value CST and type TYPE. */
1217 static tree
1218 build_new_int_cst (tree type, const wide_int &cst)
1220 unsigned int len = cst.get_len ();
1221 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1222 tree nt = make_int_cst (len, ext_len);
1224 if (len < ext_len)
1226 --ext_len;
1227 TREE_INT_CST_ELT (nt, ext_len) = 0;
1228 for (unsigned int i = len; i < ext_len; ++i)
1229 TREE_INT_CST_ELT (nt, i) = -1;
1231 else if (TYPE_UNSIGNED (type)
1232 && cst.get_precision () < len * HOST_BITS_PER_WIDE_INT)
1234 len--;
1235 TREE_INT_CST_ELT (nt, len)
1236 = zext_hwi (cst.elt (len),
1237 cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1240 for (unsigned int i = 0; i < len; i++)
1241 TREE_INT_CST_ELT (nt, i) = cst.elt (i);
1242 TREE_TYPE (nt) = type;
1243 return nt;
1246 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1248 tree
1249 build_int_cst (tree type, HOST_WIDE_INT low)
1251 /* Support legacy code. */
1252 if (!type)
1253 type = integer_type_node;
1255 return wide_int_to_tree (type, wi::shwi (low, TYPE_PRECISION (type)));
1258 tree
1259 build_int_cstu (tree type, unsigned HOST_WIDE_INT cst)
1261 return wide_int_to_tree (type, wi::uhwi (cst, TYPE_PRECISION (type)));
1264 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1266 tree
1267 build_int_cst_type (tree type, HOST_WIDE_INT low)
1269 gcc_assert (type);
1270 return wide_int_to_tree (type, wi::shwi (low, TYPE_PRECISION (type)));
1273 /* Constructs tree in type TYPE from with value given by CST. Signedness
1274 of CST is assumed to be the same as the signedness of TYPE. */
1276 tree
1277 double_int_to_tree (tree type, double_int cst)
1279 return wide_int_to_tree (type, widest_int::from (cst, TYPE_SIGN (type)));
1282 /* We force the wide_int CST to the range of the type TYPE by sign or
1283 zero extending it. OVERFLOWABLE indicates if we are interested in
1284 overflow of the value, when >0 we are only interested in signed
1285 overflow, for <0 we are interested in any overflow. OVERFLOWED
1286 indicates whether overflow has already occurred. CONST_OVERFLOWED
1287 indicates whether constant overflow has already occurred. We force
1288 T's value to be within range of T's type (by setting to 0 or 1 all
1289 the bits outside the type's range). We set TREE_OVERFLOWED if,
1290 OVERFLOWED is nonzero,
1291 or OVERFLOWABLE is >0 and signed overflow occurs
1292 or OVERFLOWABLE is <0 and any overflow occurs
1293 We return a new tree node for the extended wide_int. The node
1294 is shared if no overflow flags are set. */
1297 tree
1298 force_fit_type (tree type, const wide_int_ref &cst,
1299 int overflowable, bool overflowed)
1301 signop sign = TYPE_SIGN (type);
1303 /* If we need to set overflow flags, return a new unshared node. */
1304 if (overflowed || !wi::fits_to_tree_p (cst, type))
1306 if (overflowed
1307 || overflowable < 0
1308 || (overflowable > 0 && sign == SIGNED))
1310 wide_int tmp = wide_int::from (cst, TYPE_PRECISION (type), sign);
1311 tree t = build_new_int_cst (type, tmp);
1312 TREE_OVERFLOW (t) = 1;
1313 return t;
1317 /* Else build a shared node. */
1318 return wide_int_to_tree (type, cst);
1321 /* These are the hash table functions for the hash table of INTEGER_CST
1322 nodes of a sizetype. */
1324 /* Return the hash code code X, an INTEGER_CST. */
1326 hashval_t
1327 int_cst_hasher::hash (tree x)
1329 const_tree const t = x;
1330 hashval_t code = TYPE_UID (TREE_TYPE (t));
1331 int i;
1333 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
1334 code ^= TREE_INT_CST_ELT (t, i);
1336 return code;
1339 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1340 is the same as that given by *Y, which is the same. */
1342 bool
1343 int_cst_hasher::equal (tree x, tree y)
1345 const_tree const xt = x;
1346 const_tree const yt = y;
1348 if (TREE_TYPE (xt) != TREE_TYPE (yt)
1349 || TREE_INT_CST_NUNITS (xt) != TREE_INT_CST_NUNITS (yt)
1350 || TREE_INT_CST_EXT_NUNITS (xt) != TREE_INT_CST_EXT_NUNITS (yt))
1351 return false;
1353 for (int i = 0; i < TREE_INT_CST_NUNITS (xt); i++)
1354 if (TREE_INT_CST_ELT (xt, i) != TREE_INT_CST_ELT (yt, i))
1355 return false;
1357 return true;
1360 /* Create an INT_CST node of TYPE and value CST.
1361 The returned node is always shared. For small integers we use a
1362 per-type vector cache, for larger ones we use a single hash table.
1363 The value is extended from its precision according to the sign of
1364 the type to be a multiple of HOST_BITS_PER_WIDE_INT. This defines
1365 the upper bits and ensures that hashing and value equality based
1366 upon the underlying HOST_WIDE_INTs works without masking. */
1368 tree
1369 wide_int_to_tree (tree type, const wide_int_ref &pcst)
1371 tree t;
1372 int ix = -1;
1373 int limit = 0;
1375 gcc_assert (type);
1376 unsigned int prec = TYPE_PRECISION (type);
1377 signop sgn = TYPE_SIGN (type);
1379 /* Verify that everything is canonical. */
1380 int l = pcst.get_len ();
1381 if (l > 1)
1383 if (pcst.elt (l - 1) == 0)
1384 gcc_checking_assert (pcst.elt (l - 2) < 0);
1385 if (pcst.elt (l - 1) == (HOST_WIDE_INT) -1)
1386 gcc_checking_assert (pcst.elt (l - 2) >= 0);
1389 wide_int cst = wide_int::from (pcst, prec, sgn);
1390 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1392 if (ext_len == 1)
1394 /* We just need to store a single HOST_WIDE_INT. */
1395 HOST_WIDE_INT hwi;
1396 if (TYPE_UNSIGNED (type))
1397 hwi = cst.to_uhwi ();
1398 else
1399 hwi = cst.to_shwi ();
1401 switch (TREE_CODE (type))
1403 case NULLPTR_TYPE:
1404 gcc_assert (hwi == 0);
1405 /* Fallthru. */
1407 case POINTER_TYPE:
1408 case REFERENCE_TYPE:
1409 case POINTER_BOUNDS_TYPE:
1410 /* Cache NULL pointer and zero bounds. */
1411 if (hwi == 0)
1413 limit = 1;
1414 ix = 0;
1416 break;
1418 case BOOLEAN_TYPE:
1419 /* Cache false or true. */
1420 limit = 2;
1421 if (hwi < 2)
1422 ix = hwi;
1423 break;
1425 case INTEGER_TYPE:
1426 case OFFSET_TYPE:
1427 if (TYPE_SIGN (type) == UNSIGNED)
1429 /* Cache [0, N). */
1430 limit = INTEGER_SHARE_LIMIT;
1431 if (IN_RANGE (hwi, 0, INTEGER_SHARE_LIMIT - 1))
1432 ix = hwi;
1434 else
1436 /* Cache [-1, N). */
1437 limit = INTEGER_SHARE_LIMIT + 1;
1438 if (IN_RANGE (hwi, -1, INTEGER_SHARE_LIMIT - 1))
1439 ix = hwi + 1;
1441 break;
1443 case ENUMERAL_TYPE:
1444 break;
1446 default:
1447 gcc_unreachable ();
1450 if (ix >= 0)
1452 /* Look for it in the type's vector of small shared ints. */
1453 if (!TYPE_CACHED_VALUES_P (type))
1455 TYPE_CACHED_VALUES_P (type) = 1;
1456 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1459 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1460 if (t)
1461 /* Make sure no one is clobbering the shared constant. */
1462 gcc_checking_assert (TREE_TYPE (t) == type
1463 && TREE_INT_CST_NUNITS (t) == 1
1464 && TREE_INT_CST_OFFSET_NUNITS (t) == 1
1465 && TREE_INT_CST_EXT_NUNITS (t) == 1
1466 && TREE_INT_CST_ELT (t, 0) == hwi);
1467 else
1469 /* Create a new shared int. */
1470 t = build_new_int_cst (type, cst);
1471 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1474 else
1476 /* Use the cache of larger shared ints, using int_cst_node as
1477 a temporary. */
1479 TREE_INT_CST_ELT (int_cst_node, 0) = hwi;
1480 TREE_TYPE (int_cst_node) = type;
1482 tree *slot = int_cst_hash_table->find_slot (int_cst_node, INSERT);
1483 t = *slot;
1484 if (!t)
1486 /* Insert this one into the hash table. */
1487 t = int_cst_node;
1488 *slot = t;
1489 /* Make a new node for next time round. */
1490 int_cst_node = make_int_cst (1, 1);
1494 else
1496 /* The value either hashes properly or we drop it on the floor
1497 for the gc to take care of. There will not be enough of them
1498 to worry about. */
1500 tree nt = build_new_int_cst (type, cst);
1501 tree *slot = int_cst_hash_table->find_slot (nt, INSERT);
1502 t = *slot;
1503 if (!t)
1505 /* Insert this one into the hash table. */
1506 t = nt;
1507 *slot = t;
1511 return t;
1514 void
1515 cache_integer_cst (tree t)
1517 tree type = TREE_TYPE (t);
1518 int ix = -1;
1519 int limit = 0;
1520 int prec = TYPE_PRECISION (type);
1522 gcc_assert (!TREE_OVERFLOW (t));
1524 switch (TREE_CODE (type))
1526 case NULLPTR_TYPE:
1527 gcc_assert (integer_zerop (t));
1528 /* Fallthru. */
1530 case POINTER_TYPE:
1531 case REFERENCE_TYPE:
1532 /* Cache NULL pointer. */
1533 if (integer_zerop (t))
1535 limit = 1;
1536 ix = 0;
1538 break;
1540 case BOOLEAN_TYPE:
1541 /* Cache false or true. */
1542 limit = 2;
1543 if (wi::ltu_p (t, 2))
1544 ix = TREE_INT_CST_ELT (t, 0);
1545 break;
1547 case INTEGER_TYPE:
1548 case OFFSET_TYPE:
1549 if (TYPE_UNSIGNED (type))
1551 /* Cache 0..N */
1552 limit = INTEGER_SHARE_LIMIT;
1554 /* This is a little hokie, but if the prec is smaller than
1555 what is necessary to hold INTEGER_SHARE_LIMIT, then the
1556 obvious test will not get the correct answer. */
1557 if (prec < HOST_BITS_PER_WIDE_INT)
1559 if (tree_to_uhwi (t) < (unsigned HOST_WIDE_INT) INTEGER_SHARE_LIMIT)
1560 ix = tree_to_uhwi (t);
1562 else if (wi::ltu_p (t, INTEGER_SHARE_LIMIT))
1563 ix = tree_to_uhwi (t);
1565 else
1567 /* Cache -1..N */
1568 limit = INTEGER_SHARE_LIMIT + 1;
1570 if (integer_minus_onep (t))
1571 ix = 0;
1572 else if (!wi::neg_p (t))
1574 if (prec < HOST_BITS_PER_WIDE_INT)
1576 if (tree_to_shwi (t) < INTEGER_SHARE_LIMIT)
1577 ix = tree_to_shwi (t) + 1;
1579 else if (wi::ltu_p (t, INTEGER_SHARE_LIMIT))
1580 ix = tree_to_shwi (t) + 1;
1583 break;
1585 case ENUMERAL_TYPE:
1586 break;
1588 default:
1589 gcc_unreachable ();
1592 if (ix >= 0)
1594 /* Look for it in the type's vector of small shared ints. */
1595 if (!TYPE_CACHED_VALUES_P (type))
1597 TYPE_CACHED_VALUES_P (type) = 1;
1598 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1601 gcc_assert (TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) == NULL_TREE);
1602 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1604 else
1606 /* Use the cache of larger shared ints. */
1607 tree *slot = int_cst_hash_table->find_slot (t, INSERT);
1608 /* If there is already an entry for the number verify it's the
1609 same. */
1610 if (*slot)
1611 gcc_assert (wi::eq_p (tree (*slot), t));
1612 else
1613 /* Otherwise insert this one into the hash table. */
1614 *slot = t;
1619 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1620 and the rest are zeros. */
1622 tree
1623 build_low_bits_mask (tree type, unsigned bits)
1625 gcc_assert (bits <= TYPE_PRECISION (type));
1627 return wide_int_to_tree (type, wi::mask (bits, false,
1628 TYPE_PRECISION (type)));
1631 /* Checks that X is integer constant that can be expressed in (unsigned)
1632 HOST_WIDE_INT without loss of precision. */
1634 bool
1635 cst_and_fits_in_hwi (const_tree x)
1637 if (TREE_CODE (x) != INTEGER_CST)
1638 return false;
1640 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1641 return false;
1643 return TREE_INT_CST_NUNITS (x) == 1;
1646 /* Build a newly constructed TREE_VEC node of length LEN. */
1648 tree
1649 make_vector_stat (unsigned len MEM_STAT_DECL)
1651 tree t;
1652 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1654 record_node_allocation_statistics (VECTOR_CST, length);
1656 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1658 TREE_SET_CODE (t, VECTOR_CST);
1659 TREE_CONSTANT (t) = 1;
1661 return t;
1664 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1665 are in a list pointed to by VALS. */
1667 tree
1668 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1670 int over = 0;
1671 unsigned cnt = 0;
1672 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1673 TREE_TYPE (v) = type;
1675 /* Iterate through elements and check for overflow. */
1676 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1678 tree value = vals[cnt];
1680 VECTOR_CST_ELT (v, cnt) = value;
1682 /* Don't crash if we get an address constant. */
1683 if (!CONSTANT_CLASS_P (value))
1684 continue;
1686 over |= TREE_OVERFLOW (value);
1689 TREE_OVERFLOW (v) = over;
1690 return v;
1693 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1694 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1696 tree
1697 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1699 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1700 unsigned HOST_WIDE_INT idx;
1701 tree value;
1703 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1704 vec[idx] = value;
1705 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1706 vec[idx] = build_zero_cst (TREE_TYPE (type));
1708 return build_vector (type, vec);
1711 /* Build a vector of type VECTYPE where all the elements are SCs. */
1712 tree
1713 build_vector_from_val (tree vectype, tree sc)
1715 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1717 if (sc == error_mark_node)
1718 return sc;
1720 /* Verify that the vector type is suitable for SC. Note that there
1721 is some inconsistency in the type-system with respect to restrict
1722 qualifications of pointers. Vector types always have a main-variant
1723 element type and the qualification is applied to the vector-type.
1724 So TREE_TYPE (vector-type) does not return a properly qualified
1725 vector element-type. */
1726 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1727 TREE_TYPE (vectype)));
1729 if (CONSTANT_CLASS_P (sc))
1731 tree *v = XALLOCAVEC (tree, nunits);
1732 for (i = 0; i < nunits; ++i)
1733 v[i] = sc;
1734 return build_vector (vectype, v);
1736 else
1738 vec<constructor_elt, va_gc> *v;
1739 vec_alloc (v, nunits);
1740 for (i = 0; i < nunits; ++i)
1741 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1742 return build_constructor (vectype, v);
1746 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1747 are in the vec pointed to by VALS. */
1748 tree
1749 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1751 tree c = make_node (CONSTRUCTOR);
1752 unsigned int i;
1753 constructor_elt *elt;
1754 bool constant_p = true;
1755 bool side_effects_p = false;
1757 TREE_TYPE (c) = type;
1758 CONSTRUCTOR_ELTS (c) = vals;
1760 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1762 /* Mostly ctors will have elts that don't have side-effects, so
1763 the usual case is to scan all the elements. Hence a single
1764 loop for both const and side effects, rather than one loop
1765 each (with early outs). */
1766 if (!TREE_CONSTANT (elt->value))
1767 constant_p = false;
1768 if (TREE_SIDE_EFFECTS (elt->value))
1769 side_effects_p = true;
1772 TREE_SIDE_EFFECTS (c) = side_effects_p;
1773 TREE_CONSTANT (c) = constant_p;
1775 return c;
1778 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1779 INDEX and VALUE. */
1780 tree
1781 build_constructor_single (tree type, tree index, tree value)
1783 vec<constructor_elt, va_gc> *v;
1784 constructor_elt elt = {index, value};
1786 vec_alloc (v, 1);
1787 v->quick_push (elt);
1789 return build_constructor (type, v);
1793 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1794 are in a list pointed to by VALS. */
1795 tree
1796 build_constructor_from_list (tree type, tree vals)
1798 tree t;
1799 vec<constructor_elt, va_gc> *v = NULL;
1801 if (vals)
1803 vec_alloc (v, list_length (vals));
1804 for (t = vals; t; t = TREE_CHAIN (t))
1805 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1808 return build_constructor (type, v);
1811 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
1812 of elements, provided as index/value pairs. */
1814 tree
1815 build_constructor_va (tree type, int nelts, ...)
1817 vec<constructor_elt, va_gc> *v = NULL;
1818 va_list p;
1820 va_start (p, nelts);
1821 vec_alloc (v, nelts);
1822 while (nelts--)
1824 tree index = va_arg (p, tree);
1825 tree value = va_arg (p, tree);
1826 CONSTRUCTOR_APPEND_ELT (v, index, value);
1828 va_end (p);
1829 return build_constructor (type, v);
1832 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1834 tree
1835 build_fixed (tree type, FIXED_VALUE_TYPE f)
1837 tree v;
1838 FIXED_VALUE_TYPE *fp;
1840 v = make_node (FIXED_CST);
1841 fp = ggc_alloc<fixed_value> ();
1842 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1844 TREE_TYPE (v) = type;
1845 TREE_FIXED_CST_PTR (v) = fp;
1846 return v;
1849 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1851 tree
1852 build_real (tree type, REAL_VALUE_TYPE d)
1854 tree v;
1855 REAL_VALUE_TYPE *dp;
1856 int overflow = 0;
1858 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1859 Consider doing it via real_convert now. */
1861 v = make_node (REAL_CST);
1862 dp = ggc_alloc<real_value> ();
1863 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1865 TREE_TYPE (v) = type;
1866 TREE_REAL_CST_PTR (v) = dp;
1867 TREE_OVERFLOW (v) = overflow;
1868 return v;
1871 /* Return a new REAL_CST node whose type is TYPE
1872 and whose value is the integer value of the INTEGER_CST node I. */
1874 REAL_VALUE_TYPE
1875 real_value_from_int_cst (const_tree type, const_tree i)
1877 REAL_VALUE_TYPE d;
1879 /* Clear all bits of the real value type so that we can later do
1880 bitwise comparisons to see if two values are the same. */
1881 memset (&d, 0, sizeof d);
1883 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode, i,
1884 TYPE_SIGN (TREE_TYPE (i)));
1885 return d;
1888 /* Given a tree representing an integer constant I, return a tree
1889 representing the same value as a floating-point constant of type TYPE. */
1891 tree
1892 build_real_from_int_cst (tree type, const_tree i)
1894 tree v;
1895 int overflow = TREE_OVERFLOW (i);
1897 v = build_real (type, real_value_from_int_cst (type, i));
1899 TREE_OVERFLOW (v) |= overflow;
1900 return v;
1903 /* Return a newly constructed STRING_CST node whose value is
1904 the LEN characters at STR.
1905 Note that for a C string literal, LEN should include the trailing NUL.
1906 The TREE_TYPE is not initialized. */
1908 tree
1909 build_string (int len, const char *str)
1911 tree s;
1912 size_t length;
1914 /* Do not waste bytes provided by padding of struct tree_string. */
1915 length = len + offsetof (struct tree_string, str) + 1;
1917 record_node_allocation_statistics (STRING_CST, length);
1919 s = (tree) ggc_internal_alloc (length);
1921 memset (s, 0, sizeof (struct tree_typed));
1922 TREE_SET_CODE (s, STRING_CST);
1923 TREE_CONSTANT (s) = 1;
1924 TREE_STRING_LENGTH (s) = len;
1925 memcpy (s->string.str, str, len);
1926 s->string.str[len] = '\0';
1928 return s;
1931 /* Return a newly constructed COMPLEX_CST node whose value is
1932 specified by the real and imaginary parts REAL and IMAG.
1933 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1934 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1936 tree
1937 build_complex (tree type, tree real, tree imag)
1939 tree t = make_node (COMPLEX_CST);
1941 TREE_REALPART (t) = real;
1942 TREE_IMAGPART (t) = imag;
1943 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1944 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1945 return t;
1948 /* Return a constant of arithmetic type TYPE which is the
1949 multiplicative identity of the set TYPE. */
1951 tree
1952 build_one_cst (tree type)
1954 switch (TREE_CODE (type))
1956 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1957 case POINTER_TYPE: case REFERENCE_TYPE:
1958 case OFFSET_TYPE:
1959 return build_int_cst (type, 1);
1961 case REAL_TYPE:
1962 return build_real (type, dconst1);
1964 case FIXED_POINT_TYPE:
1965 /* We can only generate 1 for accum types. */
1966 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1967 return build_fixed (type, FCONST1 (TYPE_MODE (type)));
1969 case VECTOR_TYPE:
1971 tree scalar = build_one_cst (TREE_TYPE (type));
1973 return build_vector_from_val (type, scalar);
1976 case COMPLEX_TYPE:
1977 return build_complex (type,
1978 build_one_cst (TREE_TYPE (type)),
1979 build_zero_cst (TREE_TYPE (type)));
1981 default:
1982 gcc_unreachable ();
1986 /* Return an integer of type TYPE containing all 1's in as much precision as
1987 it contains, or a complex or vector whose subparts are such integers. */
1989 tree
1990 build_all_ones_cst (tree type)
1992 if (TREE_CODE (type) == COMPLEX_TYPE)
1994 tree scalar = build_all_ones_cst (TREE_TYPE (type));
1995 return build_complex (type, scalar, scalar);
1997 else
1998 return build_minus_one_cst (type);
2001 /* Return a constant of arithmetic type TYPE which is the
2002 opposite of the multiplicative identity of the set TYPE. */
2004 tree
2005 build_minus_one_cst (tree type)
2007 switch (TREE_CODE (type))
2009 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2010 case POINTER_TYPE: case REFERENCE_TYPE:
2011 case OFFSET_TYPE:
2012 return build_int_cst (type, -1);
2014 case REAL_TYPE:
2015 return build_real (type, dconstm1);
2017 case FIXED_POINT_TYPE:
2018 /* We can only generate 1 for accum types. */
2019 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2020 return build_fixed (type, fixed_from_double_int (double_int_minus_one,
2021 TYPE_MODE (type)));
2023 case VECTOR_TYPE:
2025 tree scalar = build_minus_one_cst (TREE_TYPE (type));
2027 return build_vector_from_val (type, scalar);
2030 case COMPLEX_TYPE:
2031 return build_complex (type,
2032 build_minus_one_cst (TREE_TYPE (type)),
2033 build_zero_cst (TREE_TYPE (type)));
2035 default:
2036 gcc_unreachable ();
2040 /* Build 0 constant of type TYPE. This is used by constructor folding
2041 and thus the constant should be represented in memory by
2042 zero(es). */
2044 tree
2045 build_zero_cst (tree type)
2047 switch (TREE_CODE (type))
2049 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2050 case POINTER_TYPE: case REFERENCE_TYPE:
2051 case OFFSET_TYPE: case NULLPTR_TYPE:
2052 return build_int_cst (type, 0);
2054 case REAL_TYPE:
2055 return build_real (type, dconst0);
2057 case FIXED_POINT_TYPE:
2058 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
2060 case VECTOR_TYPE:
2062 tree scalar = build_zero_cst (TREE_TYPE (type));
2064 return build_vector_from_val (type, scalar);
2067 case COMPLEX_TYPE:
2069 tree zero = build_zero_cst (TREE_TYPE (type));
2071 return build_complex (type, zero, zero);
2074 default:
2075 if (!AGGREGATE_TYPE_P (type))
2076 return fold_convert (type, integer_zero_node);
2077 return build_constructor (type, NULL);
2082 /* Build a BINFO with LEN language slots. */
2084 tree
2085 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
2087 tree t;
2088 size_t length = (offsetof (struct tree_binfo, base_binfos)
2089 + vec<tree, va_gc>::embedded_size (base_binfos));
2091 record_node_allocation_statistics (TREE_BINFO, length);
2093 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
2095 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
2097 TREE_SET_CODE (t, TREE_BINFO);
2099 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
2101 return t;
2104 /* Create a CASE_LABEL_EXPR tree node and return it. */
2106 tree
2107 build_case_label (tree low_value, tree high_value, tree label_decl)
2109 tree t = make_node (CASE_LABEL_EXPR);
2111 TREE_TYPE (t) = void_type_node;
2112 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
2114 CASE_LOW (t) = low_value;
2115 CASE_HIGH (t) = high_value;
2116 CASE_LABEL (t) = label_decl;
2117 CASE_CHAIN (t) = NULL_TREE;
2119 return t;
2122 /* Build a newly constructed INTEGER_CST node. LEN and EXT_LEN are the
2123 values of TREE_INT_CST_NUNITS and TREE_INT_CST_EXT_NUNITS respectively.
2124 The latter determines the length of the HOST_WIDE_INT vector. */
2126 tree
2127 make_int_cst_stat (int len, int ext_len MEM_STAT_DECL)
2129 tree t;
2130 int length = ((ext_len - 1) * sizeof (HOST_WIDE_INT)
2131 + sizeof (struct tree_int_cst));
2133 gcc_assert (len);
2134 record_node_allocation_statistics (INTEGER_CST, length);
2136 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2138 TREE_SET_CODE (t, INTEGER_CST);
2139 TREE_INT_CST_NUNITS (t) = len;
2140 TREE_INT_CST_EXT_NUNITS (t) = ext_len;
2141 /* to_offset can only be applied to trees that are offset_int-sized
2142 or smaller. EXT_LEN is correct if it fits, otherwise the constant
2143 must be exactly the precision of offset_int and so LEN is correct. */
2144 if (ext_len <= OFFSET_INT_ELTS)
2145 TREE_INT_CST_OFFSET_NUNITS (t) = ext_len;
2146 else
2147 TREE_INT_CST_OFFSET_NUNITS (t) = len;
2149 TREE_CONSTANT (t) = 1;
2151 return t;
2154 /* Build a newly constructed TREE_VEC node of length LEN. */
2156 tree
2157 make_tree_vec_stat (int len MEM_STAT_DECL)
2159 tree t;
2160 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2162 record_node_allocation_statistics (TREE_VEC, length);
2164 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2166 TREE_SET_CODE (t, TREE_VEC);
2167 TREE_VEC_LENGTH (t) = len;
2169 return t;
2172 /* Grow a TREE_VEC node to new length LEN. */
2174 tree
2175 grow_tree_vec_stat (tree v, int len MEM_STAT_DECL)
2177 gcc_assert (TREE_CODE (v) == TREE_VEC);
2179 int oldlen = TREE_VEC_LENGTH (v);
2180 gcc_assert (len > oldlen);
2182 int oldlength = (oldlen - 1) * sizeof (tree) + sizeof (struct tree_vec);
2183 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2185 record_node_allocation_statistics (TREE_VEC, length - oldlength);
2187 v = (tree) ggc_realloc (v, length PASS_MEM_STAT);
2189 TREE_VEC_LENGTH (v) = len;
2191 return v;
2194 /* Return 1 if EXPR is the integer constant zero or a complex constant
2195 of zero. */
2198 integer_zerop (const_tree expr)
2200 STRIP_NOPS (expr);
2202 switch (TREE_CODE (expr))
2204 case INTEGER_CST:
2205 return wi::eq_p (expr, 0);
2206 case COMPLEX_CST:
2207 return (integer_zerop (TREE_REALPART (expr))
2208 && integer_zerop (TREE_IMAGPART (expr)));
2209 case VECTOR_CST:
2211 unsigned i;
2212 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2213 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
2214 return false;
2215 return true;
2217 default:
2218 return false;
2222 /* Return 1 if EXPR is the integer constant one or the corresponding
2223 complex constant. */
2226 integer_onep (const_tree expr)
2228 STRIP_NOPS (expr);
2230 switch (TREE_CODE (expr))
2232 case INTEGER_CST:
2233 return wi::eq_p (wi::to_widest (expr), 1);
2234 case COMPLEX_CST:
2235 return (integer_onep (TREE_REALPART (expr))
2236 && integer_zerop (TREE_IMAGPART (expr)));
2237 case VECTOR_CST:
2239 unsigned i;
2240 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2241 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
2242 return false;
2243 return true;
2245 default:
2246 return false;
2250 /* Return 1 if EXPR is the integer constant one. For complex and vector,
2251 return 1 if every piece is the integer constant one. */
2254 integer_each_onep (const_tree expr)
2256 STRIP_NOPS (expr);
2258 if (TREE_CODE (expr) == COMPLEX_CST)
2259 return (integer_onep (TREE_REALPART (expr))
2260 && integer_onep (TREE_IMAGPART (expr)));
2261 else
2262 return integer_onep (expr);
2265 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
2266 it contains, or a complex or vector whose subparts are such integers. */
2269 integer_all_onesp (const_tree expr)
2271 STRIP_NOPS (expr);
2273 if (TREE_CODE (expr) == COMPLEX_CST
2274 && integer_all_onesp (TREE_REALPART (expr))
2275 && integer_all_onesp (TREE_IMAGPART (expr)))
2276 return 1;
2278 else if (TREE_CODE (expr) == VECTOR_CST)
2280 unsigned i;
2281 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2282 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
2283 return 0;
2284 return 1;
2287 else if (TREE_CODE (expr) != INTEGER_CST)
2288 return 0;
2290 return wi::max_value (TYPE_PRECISION (TREE_TYPE (expr)), UNSIGNED) == expr;
2293 /* Return 1 if EXPR is the integer constant minus one. */
2296 integer_minus_onep (const_tree expr)
2298 STRIP_NOPS (expr);
2300 if (TREE_CODE (expr) == COMPLEX_CST)
2301 return (integer_all_onesp (TREE_REALPART (expr))
2302 && integer_zerop (TREE_IMAGPART (expr)));
2303 else
2304 return integer_all_onesp (expr);
2307 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
2308 one bit on). */
2311 integer_pow2p (const_tree expr)
2313 STRIP_NOPS (expr);
2315 if (TREE_CODE (expr) == COMPLEX_CST
2316 && integer_pow2p (TREE_REALPART (expr))
2317 && integer_zerop (TREE_IMAGPART (expr)))
2318 return 1;
2320 if (TREE_CODE (expr) != INTEGER_CST)
2321 return 0;
2323 return wi::popcount (expr) == 1;
2326 /* Return 1 if EXPR is an integer constant other than zero or a
2327 complex constant other than zero. */
2330 integer_nonzerop (const_tree expr)
2332 STRIP_NOPS (expr);
2334 return ((TREE_CODE (expr) == INTEGER_CST
2335 && !wi::eq_p (expr, 0))
2336 || (TREE_CODE (expr) == COMPLEX_CST
2337 && (integer_nonzerop (TREE_REALPART (expr))
2338 || integer_nonzerop (TREE_IMAGPART (expr)))));
2341 /* Return 1 if EXPR is the integer constant one. For vector,
2342 return 1 if every piece is the integer constant minus one
2343 (representing the value TRUE). */
2346 integer_truep (const_tree expr)
2348 STRIP_NOPS (expr);
2350 if (TREE_CODE (expr) == VECTOR_CST)
2351 return integer_all_onesp (expr);
2352 return integer_onep (expr);
2355 /* Return 1 if EXPR is the fixed-point constant zero. */
2358 fixed_zerop (const_tree expr)
2360 return (TREE_CODE (expr) == FIXED_CST
2361 && TREE_FIXED_CST (expr).data.is_zero ());
2364 /* Return the power of two represented by a tree node known to be a
2365 power of two. */
2368 tree_log2 (const_tree expr)
2370 STRIP_NOPS (expr);
2372 if (TREE_CODE (expr) == COMPLEX_CST)
2373 return tree_log2 (TREE_REALPART (expr));
2375 return wi::exact_log2 (expr);
2378 /* Similar, but return the largest integer Y such that 2 ** Y is less
2379 than or equal to EXPR. */
2382 tree_floor_log2 (const_tree expr)
2384 STRIP_NOPS (expr);
2386 if (TREE_CODE (expr) == COMPLEX_CST)
2387 return tree_log2 (TREE_REALPART (expr));
2389 return wi::floor_log2 (expr);
2392 /* Return number of known trailing zero bits in EXPR, or, if the value of
2393 EXPR is known to be zero, the precision of it's type. */
2395 unsigned int
2396 tree_ctz (const_tree expr)
2398 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
2399 && !POINTER_TYPE_P (TREE_TYPE (expr)))
2400 return 0;
2402 unsigned int ret1, ret2, prec = TYPE_PRECISION (TREE_TYPE (expr));
2403 switch (TREE_CODE (expr))
2405 case INTEGER_CST:
2406 ret1 = wi::ctz (expr);
2407 return MIN (ret1, prec);
2408 case SSA_NAME:
2409 ret1 = wi::ctz (get_nonzero_bits (expr));
2410 return MIN (ret1, prec);
2411 case PLUS_EXPR:
2412 case MINUS_EXPR:
2413 case BIT_IOR_EXPR:
2414 case BIT_XOR_EXPR:
2415 case MIN_EXPR:
2416 case MAX_EXPR:
2417 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2418 if (ret1 == 0)
2419 return ret1;
2420 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2421 return MIN (ret1, ret2);
2422 case POINTER_PLUS_EXPR:
2423 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2424 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2425 /* Second operand is sizetype, which could be in theory
2426 wider than pointer's precision. Make sure we never
2427 return more than prec. */
2428 ret2 = MIN (ret2, prec);
2429 return MIN (ret1, ret2);
2430 case BIT_AND_EXPR:
2431 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2432 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2433 return MAX (ret1, ret2);
2434 case MULT_EXPR:
2435 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2436 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2437 return MIN (ret1 + ret2, prec);
2438 case LSHIFT_EXPR:
2439 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2440 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2441 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2443 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2444 return MIN (ret1 + ret2, prec);
2446 return ret1;
2447 case RSHIFT_EXPR:
2448 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2449 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2451 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2452 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2453 if (ret1 > ret2)
2454 return ret1 - ret2;
2456 return 0;
2457 case TRUNC_DIV_EXPR:
2458 case CEIL_DIV_EXPR:
2459 case FLOOR_DIV_EXPR:
2460 case ROUND_DIV_EXPR:
2461 case EXACT_DIV_EXPR:
2462 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
2463 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) == 1)
2465 int l = tree_log2 (TREE_OPERAND (expr, 1));
2466 if (l >= 0)
2468 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2469 ret2 = l;
2470 if (ret1 > ret2)
2471 return ret1 - ret2;
2474 return 0;
2475 CASE_CONVERT:
2476 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2477 if (ret1 && ret1 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2478 ret1 = prec;
2479 return MIN (ret1, prec);
2480 case SAVE_EXPR:
2481 return tree_ctz (TREE_OPERAND (expr, 0));
2482 case COND_EXPR:
2483 ret1 = tree_ctz (TREE_OPERAND (expr, 1));
2484 if (ret1 == 0)
2485 return 0;
2486 ret2 = tree_ctz (TREE_OPERAND (expr, 2));
2487 return MIN (ret1, ret2);
2488 case COMPOUND_EXPR:
2489 return tree_ctz (TREE_OPERAND (expr, 1));
2490 case ADDR_EXPR:
2491 ret1 = get_pointer_alignment (CONST_CAST_TREE (expr));
2492 if (ret1 > BITS_PER_UNIT)
2494 ret1 = ctz_hwi (ret1 / BITS_PER_UNIT);
2495 return MIN (ret1, prec);
2497 return 0;
2498 default:
2499 return 0;
2503 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
2504 decimal float constants, so don't return 1 for them. */
2507 real_zerop (const_tree expr)
2509 STRIP_NOPS (expr);
2511 switch (TREE_CODE (expr))
2513 case REAL_CST:
2514 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
2515 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2516 case COMPLEX_CST:
2517 return real_zerop (TREE_REALPART (expr))
2518 && real_zerop (TREE_IMAGPART (expr));
2519 case VECTOR_CST:
2521 unsigned i;
2522 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2523 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2524 return false;
2525 return true;
2527 default:
2528 return false;
2532 /* Return 1 if EXPR is the real constant one in real or complex form.
2533 Trailing zeroes matter for decimal float constants, so don't return
2534 1 for them. */
2537 real_onep (const_tree expr)
2539 STRIP_NOPS (expr);
2541 switch (TREE_CODE (expr))
2543 case REAL_CST:
2544 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
2545 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2546 case COMPLEX_CST:
2547 return real_onep (TREE_REALPART (expr))
2548 && real_zerop (TREE_IMAGPART (expr));
2549 case VECTOR_CST:
2551 unsigned i;
2552 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2553 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2554 return false;
2555 return true;
2557 default:
2558 return false;
2562 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2563 matter for decimal float constants, so don't return 1 for them. */
2566 real_minus_onep (const_tree expr)
2568 STRIP_NOPS (expr);
2570 switch (TREE_CODE (expr))
2572 case REAL_CST:
2573 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2574 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2575 case COMPLEX_CST:
2576 return real_minus_onep (TREE_REALPART (expr))
2577 && real_zerop (TREE_IMAGPART (expr));
2578 case VECTOR_CST:
2580 unsigned i;
2581 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2582 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2583 return false;
2584 return true;
2586 default:
2587 return false;
2591 /* Nonzero if EXP is a constant or a cast of a constant. */
2594 really_constant_p (const_tree exp)
2596 /* This is not quite the same as STRIP_NOPS. It does more. */
2597 while (CONVERT_EXPR_P (exp)
2598 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2599 exp = TREE_OPERAND (exp, 0);
2600 return TREE_CONSTANT (exp);
2603 /* Return first list element whose TREE_VALUE is ELEM.
2604 Return 0 if ELEM is not in LIST. */
2606 tree
2607 value_member (tree elem, tree list)
2609 while (list)
2611 if (elem == TREE_VALUE (list))
2612 return list;
2613 list = TREE_CHAIN (list);
2615 return NULL_TREE;
2618 /* Return first list element whose TREE_PURPOSE is ELEM.
2619 Return 0 if ELEM is not in LIST. */
2621 tree
2622 purpose_member (const_tree elem, tree list)
2624 while (list)
2626 if (elem == TREE_PURPOSE (list))
2627 return list;
2628 list = TREE_CHAIN (list);
2630 return NULL_TREE;
2633 /* Return true if ELEM is in V. */
2635 bool
2636 vec_member (const_tree elem, vec<tree, va_gc> *v)
2638 unsigned ix;
2639 tree t;
2640 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2641 if (elem == t)
2642 return true;
2643 return false;
2646 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2647 NULL_TREE. */
2649 tree
2650 chain_index (int idx, tree chain)
2652 for (; chain && idx > 0; --idx)
2653 chain = TREE_CHAIN (chain);
2654 return chain;
2657 /* Return nonzero if ELEM is part of the chain CHAIN. */
2660 chain_member (const_tree elem, const_tree chain)
2662 while (chain)
2664 if (elem == chain)
2665 return 1;
2666 chain = DECL_CHAIN (chain);
2669 return 0;
2672 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2673 We expect a null pointer to mark the end of the chain.
2674 This is the Lisp primitive `length'. */
2677 list_length (const_tree t)
2679 const_tree p = t;
2680 #ifdef ENABLE_TREE_CHECKING
2681 const_tree q = t;
2682 #endif
2683 int len = 0;
2685 while (p)
2687 p = TREE_CHAIN (p);
2688 #ifdef ENABLE_TREE_CHECKING
2689 if (len % 2)
2690 q = TREE_CHAIN (q);
2691 gcc_assert (p != q);
2692 #endif
2693 len++;
2696 return len;
2699 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2700 UNION_TYPE TYPE, or NULL_TREE if none. */
2702 tree
2703 first_field (const_tree type)
2705 tree t = TYPE_FIELDS (type);
2706 while (t && TREE_CODE (t) != FIELD_DECL)
2707 t = TREE_CHAIN (t);
2708 return t;
2711 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2712 by modifying the last node in chain 1 to point to chain 2.
2713 This is the Lisp primitive `nconc'. */
2715 tree
2716 chainon (tree op1, tree op2)
2718 tree t1;
2720 if (!op1)
2721 return op2;
2722 if (!op2)
2723 return op1;
2725 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2726 continue;
2727 TREE_CHAIN (t1) = op2;
2729 #ifdef ENABLE_TREE_CHECKING
2731 tree t2;
2732 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2733 gcc_assert (t2 != t1);
2735 #endif
2737 return op1;
2740 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2742 tree
2743 tree_last (tree chain)
2745 tree next;
2746 if (chain)
2747 while ((next = TREE_CHAIN (chain)))
2748 chain = next;
2749 return chain;
2752 /* Reverse the order of elements in the chain T,
2753 and return the new head of the chain (old last element). */
2755 tree
2756 nreverse (tree t)
2758 tree prev = 0, decl, next;
2759 for (decl = t; decl; decl = next)
2761 /* We shouldn't be using this function to reverse BLOCK chains; we
2762 have blocks_nreverse for that. */
2763 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2764 next = TREE_CHAIN (decl);
2765 TREE_CHAIN (decl) = prev;
2766 prev = decl;
2768 return prev;
2771 /* Return a newly created TREE_LIST node whose
2772 purpose and value fields are PARM and VALUE. */
2774 tree
2775 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2777 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2778 TREE_PURPOSE (t) = parm;
2779 TREE_VALUE (t) = value;
2780 return t;
2783 /* Build a chain of TREE_LIST nodes from a vector. */
2785 tree
2786 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2788 tree ret = NULL_TREE;
2789 tree *pp = &ret;
2790 unsigned int i;
2791 tree t;
2792 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2794 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2795 pp = &TREE_CHAIN (*pp);
2797 return ret;
2800 /* Return a newly created TREE_LIST node whose
2801 purpose and value fields are PURPOSE and VALUE
2802 and whose TREE_CHAIN is CHAIN. */
2804 tree
2805 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2807 tree node;
2809 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
2810 memset (node, 0, sizeof (struct tree_common));
2812 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2814 TREE_SET_CODE (node, TREE_LIST);
2815 TREE_CHAIN (node) = chain;
2816 TREE_PURPOSE (node) = purpose;
2817 TREE_VALUE (node) = value;
2818 return node;
2821 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2822 trees. */
2824 vec<tree, va_gc> *
2825 ctor_to_vec (tree ctor)
2827 vec<tree, va_gc> *vec;
2828 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2829 unsigned int ix;
2830 tree val;
2832 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2833 vec->quick_push (val);
2835 return vec;
2838 /* Return the size nominally occupied by an object of type TYPE
2839 when it resides in memory. The value is measured in units of bytes,
2840 and its data type is that normally used for type sizes
2841 (which is the first type created by make_signed_type or
2842 make_unsigned_type). */
2844 tree
2845 size_in_bytes (const_tree type)
2847 tree t;
2849 if (type == error_mark_node)
2850 return integer_zero_node;
2852 type = TYPE_MAIN_VARIANT (type);
2853 t = TYPE_SIZE_UNIT (type);
2855 if (t == 0)
2857 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2858 return size_zero_node;
2861 return t;
2864 /* Return the size of TYPE (in bytes) as a wide integer
2865 or return -1 if the size can vary or is larger than an integer. */
2867 HOST_WIDE_INT
2868 int_size_in_bytes (const_tree type)
2870 tree t;
2872 if (type == error_mark_node)
2873 return 0;
2875 type = TYPE_MAIN_VARIANT (type);
2876 t = TYPE_SIZE_UNIT (type);
2878 if (t && tree_fits_uhwi_p (t))
2879 return TREE_INT_CST_LOW (t);
2880 else
2881 return -1;
2884 /* Return the maximum size of TYPE (in bytes) as a wide integer
2885 or return -1 if the size can vary or is larger than an integer. */
2887 HOST_WIDE_INT
2888 max_int_size_in_bytes (const_tree type)
2890 HOST_WIDE_INT size = -1;
2891 tree size_tree;
2893 /* If this is an array type, check for a possible MAX_SIZE attached. */
2895 if (TREE_CODE (type) == ARRAY_TYPE)
2897 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2899 if (size_tree && tree_fits_uhwi_p (size_tree))
2900 size = tree_to_uhwi (size_tree);
2903 /* If we still haven't been able to get a size, see if the language
2904 can compute a maximum size. */
2906 if (size == -1)
2908 size_tree = lang_hooks.types.max_size (type);
2910 if (size_tree && tree_fits_uhwi_p (size_tree))
2911 size = tree_to_uhwi (size_tree);
2914 return size;
2917 /* Return the bit position of FIELD, in bits from the start of the record.
2918 This is a tree of type bitsizetype. */
2920 tree
2921 bit_position (const_tree field)
2923 return bit_from_pos (DECL_FIELD_OFFSET (field),
2924 DECL_FIELD_BIT_OFFSET (field));
2927 /* Return the byte position of FIELD, in bytes from the start of the record.
2928 This is a tree of type sizetype. */
2930 tree
2931 byte_position (const_tree field)
2933 return byte_from_pos (DECL_FIELD_OFFSET (field),
2934 DECL_FIELD_BIT_OFFSET (field));
2937 /* Likewise, but return as an integer. It must be representable in
2938 that way (since it could be a signed value, we don't have the
2939 option of returning -1 like int_size_in_byte can. */
2941 HOST_WIDE_INT
2942 int_byte_position (const_tree field)
2944 return tree_to_shwi (byte_position (field));
2947 /* Return the strictest alignment, in bits, that T is known to have. */
2949 unsigned int
2950 expr_align (const_tree t)
2952 unsigned int align0, align1;
2954 switch (TREE_CODE (t))
2956 CASE_CONVERT: case NON_LVALUE_EXPR:
2957 /* If we have conversions, we know that the alignment of the
2958 object must meet each of the alignments of the types. */
2959 align0 = expr_align (TREE_OPERAND (t, 0));
2960 align1 = TYPE_ALIGN (TREE_TYPE (t));
2961 return MAX (align0, align1);
2963 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2964 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2965 case CLEANUP_POINT_EXPR:
2966 /* These don't change the alignment of an object. */
2967 return expr_align (TREE_OPERAND (t, 0));
2969 case COND_EXPR:
2970 /* The best we can do is say that the alignment is the least aligned
2971 of the two arms. */
2972 align0 = expr_align (TREE_OPERAND (t, 1));
2973 align1 = expr_align (TREE_OPERAND (t, 2));
2974 return MIN (align0, align1);
2976 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2977 meaningfully, it's always 1. */
2978 case LABEL_DECL: case CONST_DECL:
2979 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2980 case FUNCTION_DECL:
2981 gcc_assert (DECL_ALIGN (t) != 0);
2982 return DECL_ALIGN (t);
2984 default:
2985 break;
2988 /* Otherwise take the alignment from that of the type. */
2989 return TYPE_ALIGN (TREE_TYPE (t));
2992 /* Return, as a tree node, the number of elements for TYPE (which is an
2993 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2995 tree
2996 array_type_nelts (const_tree type)
2998 tree index_type, min, max;
3000 /* If they did it with unspecified bounds, then we should have already
3001 given an error about it before we got here. */
3002 if (! TYPE_DOMAIN (type))
3003 return error_mark_node;
3005 index_type = TYPE_DOMAIN (type);
3006 min = TYPE_MIN_VALUE (index_type);
3007 max = TYPE_MAX_VALUE (index_type);
3009 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
3010 if (!max)
3011 return error_mark_node;
3013 return (integer_zerop (min)
3014 ? max
3015 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
3018 /* If arg is static -- a reference to an object in static storage -- then
3019 return the object. This is not the same as the C meaning of `static'.
3020 If arg isn't static, return NULL. */
3022 tree
3023 staticp (tree arg)
3025 switch (TREE_CODE (arg))
3027 case FUNCTION_DECL:
3028 /* Nested functions are static, even though taking their address will
3029 involve a trampoline as we unnest the nested function and create
3030 the trampoline on the tree level. */
3031 return arg;
3033 case VAR_DECL:
3034 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3035 && ! DECL_THREAD_LOCAL_P (arg)
3036 && ! DECL_DLLIMPORT_P (arg)
3037 ? arg : NULL);
3039 case CONST_DECL:
3040 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3041 ? arg : NULL);
3043 case CONSTRUCTOR:
3044 return TREE_STATIC (arg) ? arg : NULL;
3046 case LABEL_DECL:
3047 case STRING_CST:
3048 return arg;
3050 case COMPONENT_REF:
3051 /* If the thing being referenced is not a field, then it is
3052 something language specific. */
3053 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
3055 /* If we are referencing a bitfield, we can't evaluate an
3056 ADDR_EXPR at compile time and so it isn't a constant. */
3057 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
3058 return NULL;
3060 return staticp (TREE_OPERAND (arg, 0));
3062 case BIT_FIELD_REF:
3063 return NULL;
3065 case INDIRECT_REF:
3066 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
3068 case ARRAY_REF:
3069 case ARRAY_RANGE_REF:
3070 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
3071 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
3072 return staticp (TREE_OPERAND (arg, 0));
3073 else
3074 return NULL;
3076 case COMPOUND_LITERAL_EXPR:
3077 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
3079 default:
3080 return NULL;
3087 /* Return whether OP is a DECL whose address is function-invariant. */
3089 bool
3090 decl_address_invariant_p (const_tree op)
3092 /* The conditions below are slightly less strict than the one in
3093 staticp. */
3095 switch (TREE_CODE (op))
3097 case PARM_DECL:
3098 case RESULT_DECL:
3099 case LABEL_DECL:
3100 case FUNCTION_DECL:
3101 return true;
3103 case VAR_DECL:
3104 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3105 || DECL_THREAD_LOCAL_P (op)
3106 || DECL_CONTEXT (op) == current_function_decl
3107 || decl_function_context (op) == current_function_decl)
3108 return true;
3109 break;
3111 case CONST_DECL:
3112 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3113 || decl_function_context (op) == current_function_decl)
3114 return true;
3115 break;
3117 default:
3118 break;
3121 return false;
3124 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
3126 bool
3127 decl_address_ip_invariant_p (const_tree op)
3129 /* The conditions below are slightly less strict than the one in
3130 staticp. */
3132 switch (TREE_CODE (op))
3134 case LABEL_DECL:
3135 case FUNCTION_DECL:
3136 case STRING_CST:
3137 return true;
3139 case VAR_DECL:
3140 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
3141 && !DECL_DLLIMPORT_P (op))
3142 || DECL_THREAD_LOCAL_P (op))
3143 return true;
3144 break;
3146 case CONST_DECL:
3147 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
3148 return true;
3149 break;
3151 default:
3152 break;
3155 return false;
3159 /* Return true if T is function-invariant (internal function, does
3160 not handle arithmetic; that's handled in skip_simple_arithmetic and
3161 tree_invariant_p). */
3163 static bool tree_invariant_p (tree t);
3165 static bool
3166 tree_invariant_p_1 (tree t)
3168 tree op;
3170 if (TREE_CONSTANT (t)
3171 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
3172 return true;
3174 switch (TREE_CODE (t))
3176 case SAVE_EXPR:
3177 return true;
3179 case ADDR_EXPR:
3180 op = TREE_OPERAND (t, 0);
3181 while (handled_component_p (op))
3183 switch (TREE_CODE (op))
3185 case ARRAY_REF:
3186 case ARRAY_RANGE_REF:
3187 if (!tree_invariant_p (TREE_OPERAND (op, 1))
3188 || TREE_OPERAND (op, 2) != NULL_TREE
3189 || TREE_OPERAND (op, 3) != NULL_TREE)
3190 return false;
3191 break;
3193 case COMPONENT_REF:
3194 if (TREE_OPERAND (op, 2) != NULL_TREE)
3195 return false;
3196 break;
3198 default:;
3200 op = TREE_OPERAND (op, 0);
3203 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
3205 default:
3206 break;
3209 return false;
3212 /* Return true if T is function-invariant. */
3214 static bool
3215 tree_invariant_p (tree t)
3217 tree inner = skip_simple_arithmetic (t);
3218 return tree_invariant_p_1 (inner);
3221 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
3222 Do this to any expression which may be used in more than one place,
3223 but must be evaluated only once.
3225 Normally, expand_expr would reevaluate the expression each time.
3226 Calling save_expr produces something that is evaluated and recorded
3227 the first time expand_expr is called on it. Subsequent calls to
3228 expand_expr just reuse the recorded value.
3230 The call to expand_expr that generates code that actually computes
3231 the value is the first call *at compile time*. Subsequent calls
3232 *at compile time* generate code to use the saved value.
3233 This produces correct result provided that *at run time* control
3234 always flows through the insns made by the first expand_expr
3235 before reaching the other places where the save_expr was evaluated.
3236 You, the caller of save_expr, must make sure this is so.
3238 Constants, and certain read-only nodes, are returned with no
3239 SAVE_EXPR because that is safe. Expressions containing placeholders
3240 are not touched; see tree.def for an explanation of what these
3241 are used for. */
3243 tree
3244 save_expr (tree expr)
3246 tree t = fold (expr);
3247 tree inner;
3249 /* If the tree evaluates to a constant, then we don't want to hide that
3250 fact (i.e. this allows further folding, and direct checks for constants).
3251 However, a read-only object that has side effects cannot be bypassed.
3252 Since it is no problem to reevaluate literals, we just return the
3253 literal node. */
3254 inner = skip_simple_arithmetic (t);
3255 if (TREE_CODE (inner) == ERROR_MARK)
3256 return inner;
3258 if (tree_invariant_p_1 (inner))
3259 return t;
3261 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
3262 it means that the size or offset of some field of an object depends on
3263 the value within another field.
3265 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
3266 and some variable since it would then need to be both evaluated once and
3267 evaluated more than once. Front-ends must assure this case cannot
3268 happen by surrounding any such subexpressions in their own SAVE_EXPR
3269 and forcing evaluation at the proper time. */
3270 if (contains_placeholder_p (inner))
3271 return t;
3273 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
3274 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
3276 /* This expression might be placed ahead of a jump to ensure that the
3277 value was computed on both sides of the jump. So make sure it isn't
3278 eliminated as dead. */
3279 TREE_SIDE_EFFECTS (t) = 1;
3280 return t;
3283 /* Look inside EXPR into any simple arithmetic operations. Return the
3284 outermost non-arithmetic or non-invariant node. */
3286 tree
3287 skip_simple_arithmetic (tree expr)
3289 /* We don't care about whether this can be used as an lvalue in this
3290 context. */
3291 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3292 expr = TREE_OPERAND (expr, 0);
3294 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
3295 a constant, it will be more efficient to not make another SAVE_EXPR since
3296 it will allow better simplification and GCSE will be able to merge the
3297 computations if they actually occur. */
3298 while (true)
3300 if (UNARY_CLASS_P (expr))
3301 expr = TREE_OPERAND (expr, 0);
3302 else if (BINARY_CLASS_P (expr))
3304 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
3305 expr = TREE_OPERAND (expr, 0);
3306 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
3307 expr = TREE_OPERAND (expr, 1);
3308 else
3309 break;
3311 else
3312 break;
3315 return expr;
3318 /* Look inside EXPR into simple arithmetic operations involving constants.
3319 Return the outermost non-arithmetic or non-constant node. */
3321 tree
3322 skip_simple_constant_arithmetic (tree expr)
3324 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3325 expr = TREE_OPERAND (expr, 0);
3327 while (true)
3329 if (UNARY_CLASS_P (expr))
3330 expr = TREE_OPERAND (expr, 0);
3331 else if (BINARY_CLASS_P (expr))
3333 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3334 expr = TREE_OPERAND (expr, 0);
3335 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3336 expr = TREE_OPERAND (expr, 1);
3337 else
3338 break;
3340 else
3341 break;
3344 return expr;
3347 /* Return which tree structure is used by T. */
3349 enum tree_node_structure_enum
3350 tree_node_structure (const_tree t)
3352 const enum tree_code code = TREE_CODE (t);
3353 return tree_node_structure_for_code (code);
3356 /* Set various status flags when building a CALL_EXPR object T. */
3358 static void
3359 process_call_operands (tree t)
3361 bool side_effects = TREE_SIDE_EFFECTS (t);
3362 bool read_only = false;
3363 int i = call_expr_flags (t);
3365 /* Calls have side-effects, except those to const or pure functions. */
3366 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
3367 side_effects = true;
3368 /* Propagate TREE_READONLY of arguments for const functions. */
3369 if (i & ECF_CONST)
3370 read_only = true;
3372 if (!side_effects || read_only)
3373 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
3375 tree op = TREE_OPERAND (t, i);
3376 if (op && TREE_SIDE_EFFECTS (op))
3377 side_effects = true;
3378 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
3379 read_only = false;
3382 TREE_SIDE_EFFECTS (t) = side_effects;
3383 TREE_READONLY (t) = read_only;
3386 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
3387 size or offset that depends on a field within a record. */
3389 bool
3390 contains_placeholder_p (const_tree exp)
3392 enum tree_code code;
3394 if (!exp)
3395 return 0;
3397 code = TREE_CODE (exp);
3398 if (code == PLACEHOLDER_EXPR)
3399 return 1;
3401 switch (TREE_CODE_CLASS (code))
3403 case tcc_reference:
3404 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
3405 position computations since they will be converted into a
3406 WITH_RECORD_EXPR involving the reference, which will assume
3407 here will be valid. */
3408 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3410 case tcc_exceptional:
3411 if (code == TREE_LIST)
3412 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
3413 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
3414 break;
3416 case tcc_unary:
3417 case tcc_binary:
3418 case tcc_comparison:
3419 case tcc_expression:
3420 switch (code)
3422 case COMPOUND_EXPR:
3423 /* Ignoring the first operand isn't quite right, but works best. */
3424 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
3426 case COND_EXPR:
3427 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3428 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
3429 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
3431 case SAVE_EXPR:
3432 /* The save_expr function never wraps anything containing
3433 a PLACEHOLDER_EXPR. */
3434 return 0;
3436 default:
3437 break;
3440 switch (TREE_CODE_LENGTH (code))
3442 case 1:
3443 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3444 case 2:
3445 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3446 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
3447 default:
3448 return 0;
3451 case tcc_vl_exp:
3452 switch (code)
3454 case CALL_EXPR:
3456 const_tree arg;
3457 const_call_expr_arg_iterator iter;
3458 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
3459 if (CONTAINS_PLACEHOLDER_P (arg))
3460 return 1;
3461 return 0;
3463 default:
3464 return 0;
3467 default:
3468 return 0;
3470 return 0;
3473 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3474 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3475 field positions. */
3477 static bool
3478 type_contains_placeholder_1 (const_tree type)
3480 /* If the size contains a placeholder or the parent type (component type in
3481 the case of arrays) type involves a placeholder, this type does. */
3482 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3483 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3484 || (!POINTER_TYPE_P (type)
3485 && TREE_TYPE (type)
3486 && type_contains_placeholder_p (TREE_TYPE (type))))
3487 return true;
3489 /* Now do type-specific checks. Note that the last part of the check above
3490 greatly limits what we have to do below. */
3491 switch (TREE_CODE (type))
3493 case VOID_TYPE:
3494 case POINTER_BOUNDS_TYPE:
3495 case COMPLEX_TYPE:
3496 case ENUMERAL_TYPE:
3497 case BOOLEAN_TYPE:
3498 case POINTER_TYPE:
3499 case OFFSET_TYPE:
3500 case REFERENCE_TYPE:
3501 case METHOD_TYPE:
3502 case FUNCTION_TYPE:
3503 case VECTOR_TYPE:
3504 case NULLPTR_TYPE:
3505 return false;
3507 case INTEGER_TYPE:
3508 case REAL_TYPE:
3509 case FIXED_POINT_TYPE:
3510 /* Here we just check the bounds. */
3511 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3512 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3514 case ARRAY_TYPE:
3515 /* We have already checked the component type above, so just check the
3516 domain type. */
3517 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3519 case RECORD_TYPE:
3520 case UNION_TYPE:
3521 case QUAL_UNION_TYPE:
3523 tree field;
3525 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3526 if (TREE_CODE (field) == FIELD_DECL
3527 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3528 || (TREE_CODE (type) == QUAL_UNION_TYPE
3529 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3530 || type_contains_placeholder_p (TREE_TYPE (field))))
3531 return true;
3533 return false;
3536 default:
3537 gcc_unreachable ();
3541 /* Wrapper around above function used to cache its result. */
3543 bool
3544 type_contains_placeholder_p (tree type)
3546 bool result;
3548 /* If the contains_placeholder_bits field has been initialized,
3549 then we know the answer. */
3550 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3551 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3553 /* Indicate that we've seen this type node, and the answer is false.
3554 This is what we want to return if we run into recursion via fields. */
3555 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3557 /* Compute the real value. */
3558 result = type_contains_placeholder_1 (type);
3560 /* Store the real value. */
3561 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3563 return result;
3566 /* Push tree EXP onto vector QUEUE if it is not already present. */
3568 static void
3569 push_without_duplicates (tree exp, vec<tree> *queue)
3571 unsigned int i;
3572 tree iter;
3574 FOR_EACH_VEC_ELT (*queue, i, iter)
3575 if (simple_cst_equal (iter, exp) == 1)
3576 break;
3578 if (!iter)
3579 queue->safe_push (exp);
3582 /* Given a tree EXP, find all occurrences of references to fields
3583 in a PLACEHOLDER_EXPR and place them in vector REFS without
3584 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3585 we assume here that EXP contains only arithmetic expressions
3586 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3587 argument list. */
3589 void
3590 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3592 enum tree_code code = TREE_CODE (exp);
3593 tree inner;
3594 int i;
3596 /* We handle TREE_LIST and COMPONENT_REF separately. */
3597 if (code == TREE_LIST)
3599 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3600 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3602 else if (code == COMPONENT_REF)
3604 for (inner = TREE_OPERAND (exp, 0);
3605 REFERENCE_CLASS_P (inner);
3606 inner = TREE_OPERAND (inner, 0))
3609 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3610 push_without_duplicates (exp, refs);
3611 else
3612 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3614 else
3615 switch (TREE_CODE_CLASS (code))
3617 case tcc_constant:
3618 break;
3620 case tcc_declaration:
3621 /* Variables allocated to static storage can stay. */
3622 if (!TREE_STATIC (exp))
3623 push_without_duplicates (exp, refs);
3624 break;
3626 case tcc_expression:
3627 /* This is the pattern built in ada/make_aligning_type. */
3628 if (code == ADDR_EXPR
3629 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3631 push_without_duplicates (exp, refs);
3632 break;
3635 /* Fall through... */
3637 case tcc_exceptional:
3638 case tcc_unary:
3639 case tcc_binary:
3640 case tcc_comparison:
3641 case tcc_reference:
3642 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3643 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3644 break;
3646 case tcc_vl_exp:
3647 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3648 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3649 break;
3651 default:
3652 gcc_unreachable ();
3656 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3657 return a tree with all occurrences of references to F in a
3658 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3659 CONST_DECLs. Note that we assume here that EXP contains only
3660 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3661 occurring only in their argument list. */
3663 tree
3664 substitute_in_expr (tree exp, tree f, tree r)
3666 enum tree_code code = TREE_CODE (exp);
3667 tree op0, op1, op2, op3;
3668 tree new_tree;
3670 /* We handle TREE_LIST and COMPONENT_REF separately. */
3671 if (code == TREE_LIST)
3673 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3674 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3675 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3676 return exp;
3678 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3680 else if (code == COMPONENT_REF)
3682 tree inner;
3684 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3685 and it is the right field, replace it with R. */
3686 for (inner = TREE_OPERAND (exp, 0);
3687 REFERENCE_CLASS_P (inner);
3688 inner = TREE_OPERAND (inner, 0))
3691 /* The field. */
3692 op1 = TREE_OPERAND (exp, 1);
3694 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3695 return r;
3697 /* If this expression hasn't been completed let, leave it alone. */
3698 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3699 return exp;
3701 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3702 if (op0 == TREE_OPERAND (exp, 0))
3703 return exp;
3705 new_tree
3706 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3708 else
3709 switch (TREE_CODE_CLASS (code))
3711 case tcc_constant:
3712 return exp;
3714 case tcc_declaration:
3715 if (exp == f)
3716 return r;
3717 else
3718 return exp;
3720 case tcc_expression:
3721 if (exp == f)
3722 return r;
3724 /* Fall through... */
3726 case tcc_exceptional:
3727 case tcc_unary:
3728 case tcc_binary:
3729 case tcc_comparison:
3730 case tcc_reference:
3731 switch (TREE_CODE_LENGTH (code))
3733 case 0:
3734 return exp;
3736 case 1:
3737 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3738 if (op0 == TREE_OPERAND (exp, 0))
3739 return exp;
3741 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3742 break;
3744 case 2:
3745 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3746 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3748 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3749 return exp;
3751 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3752 break;
3754 case 3:
3755 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3756 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3757 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3759 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3760 && op2 == TREE_OPERAND (exp, 2))
3761 return exp;
3763 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3764 break;
3766 case 4:
3767 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3768 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3769 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3770 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3772 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3773 && op2 == TREE_OPERAND (exp, 2)
3774 && op3 == TREE_OPERAND (exp, 3))
3775 return exp;
3777 new_tree
3778 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3779 break;
3781 default:
3782 gcc_unreachable ();
3784 break;
3786 case tcc_vl_exp:
3788 int i;
3790 new_tree = NULL_TREE;
3792 /* If we are trying to replace F with a constant, inline back
3793 functions which do nothing else than computing a value from
3794 the arguments they are passed. This makes it possible to
3795 fold partially or entirely the replacement expression. */
3796 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3798 tree t = maybe_inline_call_in_expr (exp);
3799 if (t)
3800 return SUBSTITUTE_IN_EXPR (t, f, r);
3803 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3805 tree op = TREE_OPERAND (exp, i);
3806 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3807 if (new_op != op)
3809 if (!new_tree)
3810 new_tree = copy_node (exp);
3811 TREE_OPERAND (new_tree, i) = new_op;
3815 if (new_tree)
3817 new_tree = fold (new_tree);
3818 if (TREE_CODE (new_tree) == CALL_EXPR)
3819 process_call_operands (new_tree);
3821 else
3822 return exp;
3824 break;
3826 default:
3827 gcc_unreachable ();
3830 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3832 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3833 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3835 return new_tree;
3838 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3839 for it within OBJ, a tree that is an object or a chain of references. */
3841 tree
3842 substitute_placeholder_in_expr (tree exp, tree obj)
3844 enum tree_code code = TREE_CODE (exp);
3845 tree op0, op1, op2, op3;
3846 tree new_tree;
3848 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3849 in the chain of OBJ. */
3850 if (code == PLACEHOLDER_EXPR)
3852 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3853 tree elt;
3855 for (elt = obj; elt != 0;
3856 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3857 || TREE_CODE (elt) == COND_EXPR)
3858 ? TREE_OPERAND (elt, 1)
3859 : (REFERENCE_CLASS_P (elt)
3860 || UNARY_CLASS_P (elt)
3861 || BINARY_CLASS_P (elt)
3862 || VL_EXP_CLASS_P (elt)
3863 || EXPRESSION_CLASS_P (elt))
3864 ? TREE_OPERAND (elt, 0) : 0))
3865 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3866 return elt;
3868 for (elt = obj; elt != 0;
3869 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3870 || TREE_CODE (elt) == COND_EXPR)
3871 ? TREE_OPERAND (elt, 1)
3872 : (REFERENCE_CLASS_P (elt)
3873 || UNARY_CLASS_P (elt)
3874 || BINARY_CLASS_P (elt)
3875 || VL_EXP_CLASS_P (elt)
3876 || EXPRESSION_CLASS_P (elt))
3877 ? TREE_OPERAND (elt, 0) : 0))
3878 if (POINTER_TYPE_P (TREE_TYPE (elt))
3879 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3880 == need_type))
3881 return fold_build1 (INDIRECT_REF, need_type, elt);
3883 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3884 survives until RTL generation, there will be an error. */
3885 return exp;
3888 /* TREE_LIST is special because we need to look at TREE_VALUE
3889 and TREE_CHAIN, not TREE_OPERANDS. */
3890 else if (code == TREE_LIST)
3892 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3893 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3894 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3895 return exp;
3897 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3899 else
3900 switch (TREE_CODE_CLASS (code))
3902 case tcc_constant:
3903 case tcc_declaration:
3904 return exp;
3906 case tcc_exceptional:
3907 case tcc_unary:
3908 case tcc_binary:
3909 case tcc_comparison:
3910 case tcc_expression:
3911 case tcc_reference:
3912 case tcc_statement:
3913 switch (TREE_CODE_LENGTH (code))
3915 case 0:
3916 return exp;
3918 case 1:
3919 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3920 if (op0 == TREE_OPERAND (exp, 0))
3921 return exp;
3923 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3924 break;
3926 case 2:
3927 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3928 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3930 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3931 return exp;
3933 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3934 break;
3936 case 3:
3937 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3938 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3939 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3941 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3942 && op2 == TREE_OPERAND (exp, 2))
3943 return exp;
3945 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3946 break;
3948 case 4:
3949 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3950 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3951 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3952 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3954 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3955 && op2 == TREE_OPERAND (exp, 2)
3956 && op3 == TREE_OPERAND (exp, 3))
3957 return exp;
3959 new_tree
3960 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3961 break;
3963 default:
3964 gcc_unreachable ();
3966 break;
3968 case tcc_vl_exp:
3970 int i;
3972 new_tree = NULL_TREE;
3974 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3976 tree op = TREE_OPERAND (exp, i);
3977 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
3978 if (new_op != op)
3980 if (!new_tree)
3981 new_tree = copy_node (exp);
3982 TREE_OPERAND (new_tree, i) = new_op;
3986 if (new_tree)
3988 new_tree = fold (new_tree);
3989 if (TREE_CODE (new_tree) == CALL_EXPR)
3990 process_call_operands (new_tree);
3992 else
3993 return exp;
3995 break;
3997 default:
3998 gcc_unreachable ();
4001 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4003 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4004 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4006 return new_tree;
4010 /* Subroutine of stabilize_reference; this is called for subtrees of
4011 references. Any expression with side-effects must be put in a SAVE_EXPR
4012 to ensure that it is only evaluated once.
4014 We don't put SAVE_EXPR nodes around everything, because assigning very
4015 simple expressions to temporaries causes us to miss good opportunities
4016 for optimizations. Among other things, the opportunity to fold in the
4017 addition of a constant into an addressing mode often gets lost, e.g.
4018 "y[i+1] += x;". In general, we take the approach that we should not make
4019 an assignment unless we are forced into it - i.e., that any non-side effect
4020 operator should be allowed, and that cse should take care of coalescing
4021 multiple utterances of the same expression should that prove fruitful. */
4023 static tree
4024 stabilize_reference_1 (tree e)
4026 tree result;
4027 enum tree_code code = TREE_CODE (e);
4029 /* We cannot ignore const expressions because it might be a reference
4030 to a const array but whose index contains side-effects. But we can
4031 ignore things that are actual constant or that already have been
4032 handled by this function. */
4034 if (tree_invariant_p (e))
4035 return e;
4037 switch (TREE_CODE_CLASS (code))
4039 case tcc_exceptional:
4040 case tcc_type:
4041 case tcc_declaration:
4042 case tcc_comparison:
4043 case tcc_statement:
4044 case tcc_expression:
4045 case tcc_reference:
4046 case tcc_vl_exp:
4047 /* If the expression has side-effects, then encase it in a SAVE_EXPR
4048 so that it will only be evaluated once. */
4049 /* The reference (r) and comparison (<) classes could be handled as
4050 below, but it is generally faster to only evaluate them once. */
4051 if (TREE_SIDE_EFFECTS (e))
4052 return save_expr (e);
4053 return e;
4055 case tcc_constant:
4056 /* Constants need no processing. In fact, we should never reach
4057 here. */
4058 return e;
4060 case tcc_binary:
4061 /* Division is slow and tends to be compiled with jumps,
4062 especially the division by powers of 2 that is often
4063 found inside of an array reference. So do it just once. */
4064 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
4065 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
4066 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
4067 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
4068 return save_expr (e);
4069 /* Recursively stabilize each operand. */
4070 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
4071 stabilize_reference_1 (TREE_OPERAND (e, 1)));
4072 break;
4074 case tcc_unary:
4075 /* Recursively stabilize each operand. */
4076 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
4077 break;
4079 default:
4080 gcc_unreachable ();
4083 TREE_TYPE (result) = TREE_TYPE (e);
4084 TREE_READONLY (result) = TREE_READONLY (e);
4085 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
4086 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
4088 return result;
4091 /* Stabilize a reference so that we can use it any number of times
4092 without causing its operands to be evaluated more than once.
4093 Returns the stabilized reference. This works by means of save_expr,
4094 so see the caveats in the comments about save_expr.
4096 Also allows conversion expressions whose operands are references.
4097 Any other kind of expression is returned unchanged. */
4099 tree
4100 stabilize_reference (tree ref)
4102 tree result;
4103 enum tree_code code = TREE_CODE (ref);
4105 switch (code)
4107 case VAR_DECL:
4108 case PARM_DECL:
4109 case RESULT_DECL:
4110 /* No action is needed in this case. */
4111 return ref;
4113 CASE_CONVERT:
4114 case FLOAT_EXPR:
4115 case FIX_TRUNC_EXPR:
4116 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
4117 break;
4119 case INDIRECT_REF:
4120 result = build_nt (INDIRECT_REF,
4121 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
4122 break;
4124 case COMPONENT_REF:
4125 result = build_nt (COMPONENT_REF,
4126 stabilize_reference (TREE_OPERAND (ref, 0)),
4127 TREE_OPERAND (ref, 1), NULL_TREE);
4128 break;
4130 case BIT_FIELD_REF:
4131 result = build_nt (BIT_FIELD_REF,
4132 stabilize_reference (TREE_OPERAND (ref, 0)),
4133 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
4134 break;
4136 case ARRAY_REF:
4137 result = build_nt (ARRAY_REF,
4138 stabilize_reference (TREE_OPERAND (ref, 0)),
4139 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4140 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4141 break;
4143 case ARRAY_RANGE_REF:
4144 result = build_nt (ARRAY_RANGE_REF,
4145 stabilize_reference (TREE_OPERAND (ref, 0)),
4146 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4147 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4148 break;
4150 case COMPOUND_EXPR:
4151 /* We cannot wrap the first expression in a SAVE_EXPR, as then
4152 it wouldn't be ignored. This matters when dealing with
4153 volatiles. */
4154 return stabilize_reference_1 (ref);
4156 /* If arg isn't a kind of lvalue we recognize, make no change.
4157 Caller should recognize the error for an invalid lvalue. */
4158 default:
4159 return ref;
4161 case ERROR_MARK:
4162 return error_mark_node;
4165 TREE_TYPE (result) = TREE_TYPE (ref);
4166 TREE_READONLY (result) = TREE_READONLY (ref);
4167 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
4168 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
4170 return result;
4173 /* Low-level constructors for expressions. */
4175 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
4176 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
4178 void
4179 recompute_tree_invariant_for_addr_expr (tree t)
4181 tree node;
4182 bool tc = true, se = false;
4184 /* We started out assuming this address is both invariant and constant, but
4185 does not have side effects. Now go down any handled components and see if
4186 any of them involve offsets that are either non-constant or non-invariant.
4187 Also check for side-effects.
4189 ??? Note that this code makes no attempt to deal with the case where
4190 taking the address of something causes a copy due to misalignment. */
4192 #define UPDATE_FLAGS(NODE) \
4193 do { tree _node = (NODE); \
4194 if (_node && !TREE_CONSTANT (_node)) tc = false; \
4195 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
4197 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
4198 node = TREE_OPERAND (node, 0))
4200 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
4201 array reference (probably made temporarily by the G++ front end),
4202 so ignore all the operands. */
4203 if ((TREE_CODE (node) == ARRAY_REF
4204 || TREE_CODE (node) == ARRAY_RANGE_REF)
4205 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
4207 UPDATE_FLAGS (TREE_OPERAND (node, 1));
4208 if (TREE_OPERAND (node, 2))
4209 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4210 if (TREE_OPERAND (node, 3))
4211 UPDATE_FLAGS (TREE_OPERAND (node, 3));
4213 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
4214 FIELD_DECL, apparently. The G++ front end can put something else
4215 there, at least temporarily. */
4216 else if (TREE_CODE (node) == COMPONENT_REF
4217 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
4219 if (TREE_OPERAND (node, 2))
4220 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4224 node = lang_hooks.expr_to_decl (node, &tc, &se);
4226 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4227 the address, since &(*a)->b is a form of addition. If it's a constant, the
4228 address is constant too. If it's a decl, its address is constant if the
4229 decl is static. Everything else is not constant and, furthermore,
4230 taking the address of a volatile variable is not volatile. */
4231 if (TREE_CODE (node) == INDIRECT_REF
4232 || TREE_CODE (node) == MEM_REF)
4233 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4234 else if (CONSTANT_CLASS_P (node))
4236 else if (DECL_P (node))
4237 tc &= (staticp (node) != NULL_TREE);
4238 else
4240 tc = false;
4241 se |= TREE_SIDE_EFFECTS (node);
4245 TREE_CONSTANT (t) = tc;
4246 TREE_SIDE_EFFECTS (t) = se;
4247 #undef UPDATE_FLAGS
4250 /* Build an expression of code CODE, data type TYPE, and operands as
4251 specified. Expressions and reference nodes can be created this way.
4252 Constants, decls, types and misc nodes cannot be.
4254 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4255 enough for all extant tree codes. */
4257 tree
4258 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
4260 tree t;
4262 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4264 t = make_node_stat (code PASS_MEM_STAT);
4265 TREE_TYPE (t) = tt;
4267 return t;
4270 tree
4271 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4273 int length = sizeof (struct tree_exp);
4274 tree t;
4276 record_node_allocation_statistics (code, length);
4278 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4280 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4282 memset (t, 0, sizeof (struct tree_common));
4284 TREE_SET_CODE (t, code);
4286 TREE_TYPE (t) = type;
4287 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4288 TREE_OPERAND (t, 0) = node;
4289 if (node && !TYPE_P (node))
4291 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4292 TREE_READONLY (t) = TREE_READONLY (node);
4295 if (TREE_CODE_CLASS (code) == tcc_statement)
4296 TREE_SIDE_EFFECTS (t) = 1;
4297 else switch (code)
4299 case VA_ARG_EXPR:
4300 /* All of these have side-effects, no matter what their
4301 operands are. */
4302 TREE_SIDE_EFFECTS (t) = 1;
4303 TREE_READONLY (t) = 0;
4304 break;
4306 case INDIRECT_REF:
4307 /* Whether a dereference is readonly has nothing to do with whether
4308 its operand is readonly. */
4309 TREE_READONLY (t) = 0;
4310 break;
4312 case ADDR_EXPR:
4313 if (node)
4314 recompute_tree_invariant_for_addr_expr (t);
4315 break;
4317 default:
4318 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4319 && node && !TYPE_P (node)
4320 && TREE_CONSTANT (node))
4321 TREE_CONSTANT (t) = 1;
4322 if (TREE_CODE_CLASS (code) == tcc_reference
4323 && node && TREE_THIS_VOLATILE (node))
4324 TREE_THIS_VOLATILE (t) = 1;
4325 break;
4328 return t;
4331 #define PROCESS_ARG(N) \
4332 do { \
4333 TREE_OPERAND (t, N) = arg##N; \
4334 if (arg##N &&!TYPE_P (arg##N)) \
4336 if (TREE_SIDE_EFFECTS (arg##N)) \
4337 side_effects = 1; \
4338 if (!TREE_READONLY (arg##N) \
4339 && !CONSTANT_CLASS_P (arg##N)) \
4340 (void) (read_only = 0); \
4341 if (!TREE_CONSTANT (arg##N)) \
4342 (void) (constant = 0); \
4344 } while (0)
4346 tree
4347 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
4349 bool constant, read_only, side_effects;
4350 tree t;
4352 gcc_assert (TREE_CODE_LENGTH (code) == 2);
4354 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
4355 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
4356 /* When sizetype precision doesn't match that of pointers
4357 we need to be able to build explicit extensions or truncations
4358 of the offset argument. */
4359 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
4360 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
4361 && TREE_CODE (arg1) == INTEGER_CST);
4363 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
4364 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
4365 && ptrofftype_p (TREE_TYPE (arg1)));
4367 t = make_node_stat (code PASS_MEM_STAT);
4368 TREE_TYPE (t) = tt;
4370 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
4371 result based on those same flags for the arguments. But if the
4372 arguments aren't really even `tree' expressions, we shouldn't be trying
4373 to do this. */
4375 /* Expressions without side effects may be constant if their
4376 arguments are as well. */
4377 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4378 || TREE_CODE_CLASS (code) == tcc_binary);
4379 read_only = 1;
4380 side_effects = TREE_SIDE_EFFECTS (t);
4382 PROCESS_ARG (0);
4383 PROCESS_ARG (1);
4385 TREE_READONLY (t) = read_only;
4386 TREE_CONSTANT (t) = constant;
4387 TREE_SIDE_EFFECTS (t) = side_effects;
4388 TREE_THIS_VOLATILE (t)
4389 = (TREE_CODE_CLASS (code) == tcc_reference
4390 && arg0 && TREE_THIS_VOLATILE (arg0));
4392 return t;
4396 tree
4397 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4398 tree arg2 MEM_STAT_DECL)
4400 bool constant, read_only, side_effects;
4401 tree t;
4403 gcc_assert (TREE_CODE_LENGTH (code) == 3);
4404 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4406 t = make_node_stat (code PASS_MEM_STAT);
4407 TREE_TYPE (t) = tt;
4409 read_only = 1;
4411 /* As a special exception, if COND_EXPR has NULL branches, we
4412 assume that it is a gimple statement and always consider
4413 it to have side effects. */
4414 if (code == COND_EXPR
4415 && tt == void_type_node
4416 && arg1 == NULL_TREE
4417 && arg2 == NULL_TREE)
4418 side_effects = true;
4419 else
4420 side_effects = TREE_SIDE_EFFECTS (t);
4422 PROCESS_ARG (0);
4423 PROCESS_ARG (1);
4424 PROCESS_ARG (2);
4426 if (code == COND_EXPR)
4427 TREE_READONLY (t) = read_only;
4429 TREE_SIDE_EFFECTS (t) = side_effects;
4430 TREE_THIS_VOLATILE (t)
4431 = (TREE_CODE_CLASS (code) == tcc_reference
4432 && arg0 && TREE_THIS_VOLATILE (arg0));
4434 return t;
4437 tree
4438 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4439 tree arg2, tree arg3 MEM_STAT_DECL)
4441 bool constant, read_only, side_effects;
4442 tree t;
4444 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4446 t = make_node_stat (code PASS_MEM_STAT);
4447 TREE_TYPE (t) = tt;
4449 side_effects = TREE_SIDE_EFFECTS (t);
4451 PROCESS_ARG (0);
4452 PROCESS_ARG (1);
4453 PROCESS_ARG (2);
4454 PROCESS_ARG (3);
4456 TREE_SIDE_EFFECTS (t) = side_effects;
4457 TREE_THIS_VOLATILE (t)
4458 = (TREE_CODE_CLASS (code) == tcc_reference
4459 && arg0 && TREE_THIS_VOLATILE (arg0));
4461 return t;
4464 tree
4465 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4466 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4468 bool constant, read_only, side_effects;
4469 tree t;
4471 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4473 t = make_node_stat (code PASS_MEM_STAT);
4474 TREE_TYPE (t) = tt;
4476 side_effects = TREE_SIDE_EFFECTS (t);
4478 PROCESS_ARG (0);
4479 PROCESS_ARG (1);
4480 PROCESS_ARG (2);
4481 PROCESS_ARG (3);
4482 PROCESS_ARG (4);
4484 TREE_SIDE_EFFECTS (t) = side_effects;
4485 TREE_THIS_VOLATILE (t)
4486 = (TREE_CODE_CLASS (code) == tcc_reference
4487 && arg0 && TREE_THIS_VOLATILE (arg0));
4489 return t;
4492 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4493 on the pointer PTR. */
4495 tree
4496 build_simple_mem_ref_loc (location_t loc, tree ptr)
4498 HOST_WIDE_INT offset = 0;
4499 tree ptype = TREE_TYPE (ptr);
4500 tree tem;
4501 /* For convenience allow addresses that collapse to a simple base
4502 and offset. */
4503 if (TREE_CODE (ptr) == ADDR_EXPR
4504 && (handled_component_p (TREE_OPERAND (ptr, 0))
4505 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4507 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4508 gcc_assert (ptr);
4509 ptr = build_fold_addr_expr (ptr);
4510 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4512 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4513 ptr, build_int_cst (ptype, offset));
4514 SET_EXPR_LOCATION (tem, loc);
4515 return tem;
4518 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4520 offset_int
4521 mem_ref_offset (const_tree t)
4523 return offset_int::from (TREE_OPERAND (t, 1), SIGNED);
4526 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4527 offsetted by OFFSET units. */
4529 tree
4530 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4532 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4533 build_fold_addr_expr (base),
4534 build_int_cst (ptr_type_node, offset));
4535 tree addr = build1 (ADDR_EXPR, type, ref);
4536 recompute_tree_invariant_for_addr_expr (addr);
4537 return addr;
4540 /* Similar except don't specify the TREE_TYPE
4541 and leave the TREE_SIDE_EFFECTS as 0.
4542 It is permissible for arguments to be null,
4543 or even garbage if their values do not matter. */
4545 tree
4546 build_nt (enum tree_code code, ...)
4548 tree t;
4549 int length;
4550 int i;
4551 va_list p;
4553 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4555 va_start (p, code);
4557 t = make_node (code);
4558 length = TREE_CODE_LENGTH (code);
4560 for (i = 0; i < length; i++)
4561 TREE_OPERAND (t, i) = va_arg (p, tree);
4563 va_end (p);
4564 return t;
4567 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4568 tree vec. */
4570 tree
4571 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4573 tree ret, t;
4574 unsigned int ix;
4576 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4577 CALL_EXPR_FN (ret) = fn;
4578 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4579 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4580 CALL_EXPR_ARG (ret, ix) = t;
4581 return ret;
4584 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4585 We do NOT enter this node in any sort of symbol table.
4587 LOC is the location of the decl.
4589 layout_decl is used to set up the decl's storage layout.
4590 Other slots are initialized to 0 or null pointers. */
4592 tree
4593 build_decl_stat (location_t loc, enum tree_code code, tree name,
4594 tree type MEM_STAT_DECL)
4596 tree t;
4598 t = make_node_stat (code PASS_MEM_STAT);
4599 DECL_SOURCE_LOCATION (t) = loc;
4601 /* if (type == error_mark_node)
4602 type = integer_type_node; */
4603 /* That is not done, deliberately, so that having error_mark_node
4604 as the type can suppress useless errors in the use of this variable. */
4606 DECL_NAME (t) = name;
4607 TREE_TYPE (t) = type;
4609 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4610 layout_decl (t, 0);
4612 return t;
4615 /* Builds and returns function declaration with NAME and TYPE. */
4617 tree
4618 build_fn_decl (const char *name, tree type)
4620 tree id = get_identifier (name);
4621 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4623 DECL_EXTERNAL (decl) = 1;
4624 TREE_PUBLIC (decl) = 1;
4625 DECL_ARTIFICIAL (decl) = 1;
4626 TREE_NOTHROW (decl) = 1;
4628 return decl;
4631 vec<tree, va_gc> *all_translation_units;
4633 /* Builds a new translation-unit decl with name NAME, queues it in the
4634 global list of translation-unit decls and returns it. */
4636 tree
4637 build_translation_unit_decl (tree name)
4639 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4640 name, NULL_TREE);
4641 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4642 vec_safe_push (all_translation_units, tu);
4643 return tu;
4647 /* BLOCK nodes are used to represent the structure of binding contours
4648 and declarations, once those contours have been exited and their contents
4649 compiled. This information is used for outputting debugging info. */
4651 tree
4652 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4654 tree block = make_node (BLOCK);
4656 BLOCK_VARS (block) = vars;
4657 BLOCK_SUBBLOCKS (block) = subblocks;
4658 BLOCK_SUPERCONTEXT (block) = supercontext;
4659 BLOCK_CHAIN (block) = chain;
4660 return block;
4664 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4666 LOC is the location to use in tree T. */
4668 void
4669 protected_set_expr_location (tree t, location_t loc)
4671 if (CAN_HAVE_LOCATION_P (t))
4672 SET_EXPR_LOCATION (t, loc);
4675 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4676 is ATTRIBUTE. */
4678 tree
4679 build_decl_attribute_variant (tree ddecl, tree attribute)
4681 DECL_ATTRIBUTES (ddecl) = attribute;
4682 return ddecl;
4685 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4686 is ATTRIBUTE and its qualifiers are QUALS.
4688 Record such modified types already made so we don't make duplicates. */
4690 tree
4691 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4693 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4695 inchash::hash hstate;
4696 tree ntype;
4697 int i;
4698 tree t;
4699 enum tree_code code = TREE_CODE (ttype);
4701 /* Building a distinct copy of a tagged type is inappropriate; it
4702 causes breakage in code that expects there to be a one-to-one
4703 relationship between a struct and its fields.
4704 build_duplicate_type is another solution (as used in
4705 handle_transparent_union_attribute), but that doesn't play well
4706 with the stronger C++ type identity model. */
4707 if (TREE_CODE (ttype) == RECORD_TYPE
4708 || TREE_CODE (ttype) == UNION_TYPE
4709 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4710 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4712 warning (OPT_Wattributes,
4713 "ignoring attributes applied to %qT after definition",
4714 TYPE_MAIN_VARIANT (ttype));
4715 return build_qualified_type (ttype, quals);
4718 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4719 ntype = build_distinct_type_copy (ttype);
4721 TYPE_ATTRIBUTES (ntype) = attribute;
4723 hstate.add_int (code);
4724 if (TREE_TYPE (ntype))
4725 hstate.add_object (TYPE_HASH (TREE_TYPE (ntype)));
4726 attribute_hash_list (attribute, hstate);
4728 switch (TREE_CODE (ntype))
4730 case FUNCTION_TYPE:
4731 type_hash_list (TYPE_ARG_TYPES (ntype), hstate);
4732 break;
4733 case ARRAY_TYPE:
4734 if (TYPE_DOMAIN (ntype))
4735 hstate.add_object (TYPE_HASH (TYPE_DOMAIN (ntype)));
4736 break;
4737 case INTEGER_TYPE:
4738 t = TYPE_MAX_VALUE (ntype);
4739 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
4740 hstate.add_object (TREE_INT_CST_ELT (t, i));
4741 break;
4742 case REAL_TYPE:
4743 case FIXED_POINT_TYPE:
4745 unsigned int precision = TYPE_PRECISION (ntype);
4746 hstate.add_object (precision);
4748 break;
4749 default:
4750 break;
4753 ntype = type_hash_canon (hstate.end(), ntype);
4755 /* If the target-dependent attributes make NTYPE different from
4756 its canonical type, we will need to use structural equality
4757 checks for this type. */
4758 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4759 || !comp_type_attributes (ntype, ttype))
4760 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4761 else if (TYPE_CANONICAL (ntype) == ntype)
4762 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4764 ttype = build_qualified_type (ntype, quals);
4766 else if (TYPE_QUALS (ttype) != quals)
4767 ttype = build_qualified_type (ttype, quals);
4769 return ttype;
4772 /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
4773 the same. */
4775 static bool
4776 omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
4778 tree cl1, cl2;
4779 for (cl1 = clauses1, cl2 = clauses2;
4780 cl1 && cl2;
4781 cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
4783 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
4784 return false;
4785 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
4787 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
4788 OMP_CLAUSE_DECL (cl2)) != 1)
4789 return false;
4791 switch (OMP_CLAUSE_CODE (cl1))
4793 case OMP_CLAUSE_ALIGNED:
4794 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
4795 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
4796 return false;
4797 break;
4798 case OMP_CLAUSE_LINEAR:
4799 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
4800 OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
4801 return false;
4802 break;
4803 case OMP_CLAUSE_SIMDLEN:
4804 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
4805 OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
4806 return false;
4807 default:
4808 break;
4811 return true;
4814 /* Compare two constructor-element-type constants. Return 1 if the lists
4815 are known to be equal; otherwise return 0. */
4817 static bool
4818 simple_cst_list_equal (const_tree l1, const_tree l2)
4820 while (l1 != NULL_TREE && l2 != NULL_TREE)
4822 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
4823 return false;
4825 l1 = TREE_CHAIN (l1);
4826 l2 = TREE_CHAIN (l2);
4829 return l1 == l2;
4832 /* Compare two attributes for their value identity. Return true if the
4833 attribute values are known to be equal; otherwise return false.
4836 static bool
4837 attribute_value_equal (const_tree attr1, const_tree attr2)
4839 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4840 return true;
4842 if (TREE_VALUE (attr1) != NULL_TREE
4843 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4844 && TREE_VALUE (attr2) != NULL
4845 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4846 return (simple_cst_list_equal (TREE_VALUE (attr1),
4847 TREE_VALUE (attr2)) == 1);
4849 if ((flag_openmp || flag_openmp_simd)
4850 && TREE_VALUE (attr1) && TREE_VALUE (attr2)
4851 && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
4852 && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
4853 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
4854 TREE_VALUE (attr2));
4856 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4859 /* Return 0 if the attributes for two types are incompatible, 1 if they
4860 are compatible, and 2 if they are nearly compatible (which causes a
4861 warning to be generated). */
4863 comp_type_attributes (const_tree type1, const_tree type2)
4865 const_tree a1 = TYPE_ATTRIBUTES (type1);
4866 const_tree a2 = TYPE_ATTRIBUTES (type2);
4867 const_tree a;
4869 if (a1 == a2)
4870 return 1;
4871 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4873 const struct attribute_spec *as;
4874 const_tree attr;
4876 as = lookup_attribute_spec (get_attribute_name (a));
4877 if (!as || as->affects_type_identity == false)
4878 continue;
4880 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4881 if (!attr || !attribute_value_equal (a, attr))
4882 break;
4884 if (!a)
4886 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
4888 const struct attribute_spec *as;
4890 as = lookup_attribute_spec (get_attribute_name (a));
4891 if (!as || as->affects_type_identity == false)
4892 continue;
4894 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
4895 break;
4896 /* We don't need to compare trees again, as we did this
4897 already in first loop. */
4899 /* All types - affecting identity - are equal, so
4900 there is no need to call target hook for comparison. */
4901 if (!a)
4902 return 1;
4904 /* As some type combinations - like default calling-convention - might
4905 be compatible, we have to call the target hook to get the final result. */
4906 return targetm.comp_type_attributes (type1, type2);
4909 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4910 is ATTRIBUTE.
4912 Record such modified types already made so we don't make duplicates. */
4914 tree
4915 build_type_attribute_variant (tree ttype, tree attribute)
4917 return build_type_attribute_qual_variant (ttype, attribute,
4918 TYPE_QUALS (ttype));
4922 /* Reset the expression *EXPR_P, a size or position.
4924 ??? We could reset all non-constant sizes or positions. But it's cheap
4925 enough to not do so and refrain from adding workarounds to dwarf2out.c.
4927 We need to reset self-referential sizes or positions because they cannot
4928 be gimplified and thus can contain a CALL_EXPR after the gimplification
4929 is finished, which will run afoul of LTO streaming. And they need to be
4930 reset to something essentially dummy but not constant, so as to preserve
4931 the properties of the object they are attached to. */
4933 static inline void
4934 free_lang_data_in_one_sizepos (tree *expr_p)
4936 tree expr = *expr_p;
4937 if (CONTAINS_PLACEHOLDER_P (expr))
4938 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
4942 /* Reset all the fields in a binfo node BINFO. We only keep
4943 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
4945 static void
4946 free_lang_data_in_binfo (tree binfo)
4948 unsigned i;
4949 tree t;
4951 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
4953 BINFO_VIRTUALS (binfo) = NULL_TREE;
4954 BINFO_BASE_ACCESSES (binfo) = NULL;
4955 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
4956 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
4958 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
4959 free_lang_data_in_binfo (t);
4963 /* Reset all language specific information still present in TYPE. */
4965 static void
4966 free_lang_data_in_type (tree type)
4968 gcc_assert (TYPE_P (type));
4970 /* Give the FE a chance to remove its own data first. */
4971 lang_hooks.free_lang_data (type);
4973 TREE_LANG_FLAG_0 (type) = 0;
4974 TREE_LANG_FLAG_1 (type) = 0;
4975 TREE_LANG_FLAG_2 (type) = 0;
4976 TREE_LANG_FLAG_3 (type) = 0;
4977 TREE_LANG_FLAG_4 (type) = 0;
4978 TREE_LANG_FLAG_5 (type) = 0;
4979 TREE_LANG_FLAG_6 (type) = 0;
4981 if (TREE_CODE (type) == FUNCTION_TYPE)
4983 /* Remove the const and volatile qualifiers from arguments. The
4984 C++ front end removes them, but the C front end does not,
4985 leading to false ODR violation errors when merging two
4986 instances of the same function signature compiled by
4987 different front ends. */
4988 tree p;
4990 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
4992 tree arg_type = TREE_VALUE (p);
4994 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
4996 int quals = TYPE_QUALS (arg_type)
4997 & ~TYPE_QUAL_CONST
4998 & ~TYPE_QUAL_VOLATILE;
4999 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
5000 free_lang_data_in_type (TREE_VALUE (p));
5005 /* Remove members that are not actually FIELD_DECLs from the field
5006 list of an aggregate. These occur in C++. */
5007 if (RECORD_OR_UNION_TYPE_P (type))
5009 tree prev, member;
5011 /* Note that TYPE_FIELDS can be shared across distinct
5012 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
5013 to be removed, we cannot set its TREE_CHAIN to NULL.
5014 Otherwise, we would not be able to find all the other fields
5015 in the other instances of this TREE_TYPE.
5017 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
5018 prev = NULL_TREE;
5019 member = TYPE_FIELDS (type);
5020 while (member)
5022 if (TREE_CODE (member) == FIELD_DECL
5023 || TREE_CODE (member) == TYPE_DECL)
5025 if (prev)
5026 TREE_CHAIN (prev) = member;
5027 else
5028 TYPE_FIELDS (type) = member;
5029 prev = member;
5032 member = TREE_CHAIN (member);
5035 if (prev)
5036 TREE_CHAIN (prev) = NULL_TREE;
5037 else
5038 TYPE_FIELDS (type) = NULL_TREE;
5040 TYPE_METHODS (type) = NULL_TREE;
5041 if (TYPE_BINFO (type))
5043 free_lang_data_in_binfo (TYPE_BINFO (type));
5044 if ((!BINFO_VTABLE (TYPE_BINFO (type))
5045 || !flag_devirtualize)
5046 && (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
5047 || debug_info_level != DINFO_LEVEL_NONE))
5048 TYPE_BINFO (type) = NULL;
5051 else
5053 /* For non-aggregate types, clear out the language slot (which
5054 overloads TYPE_BINFO). */
5055 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
5057 if (INTEGRAL_TYPE_P (type)
5058 || SCALAR_FLOAT_TYPE_P (type)
5059 || FIXED_POINT_TYPE_P (type))
5061 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
5062 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
5066 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
5067 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
5069 if (TYPE_CONTEXT (type)
5070 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
5072 tree ctx = TYPE_CONTEXT (type);
5075 ctx = BLOCK_SUPERCONTEXT (ctx);
5077 while (ctx && TREE_CODE (ctx) == BLOCK);
5078 TYPE_CONTEXT (type) = ctx;
5083 /* Return true if DECL may need an assembler name to be set. */
5085 static inline bool
5086 need_assembler_name_p (tree decl)
5088 /* We use DECL_ASSEMBLER_NAME to hold mangled type names for One Definition Rule
5089 merging. */
5090 if (flag_lto_odr_type_mering
5091 && TREE_CODE (decl) == TYPE_DECL
5092 && DECL_NAME (decl)
5093 && decl == TYPE_NAME (TREE_TYPE (decl))
5094 && !is_lang_specific (TREE_TYPE (decl))
5095 && AGGREGATE_TYPE_P (TREE_TYPE (decl))
5096 && !variably_modified_type_p (TREE_TYPE (decl), NULL_TREE)
5097 && !type_in_anonymous_namespace_p (TREE_TYPE (decl)))
5098 return !DECL_ASSEMBLER_NAME_SET_P (decl);
5099 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
5100 if (TREE_CODE (decl) != FUNCTION_DECL
5101 && TREE_CODE (decl) != VAR_DECL)
5102 return false;
5104 /* If DECL already has its assembler name set, it does not need a
5105 new one. */
5106 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
5107 || DECL_ASSEMBLER_NAME_SET_P (decl))
5108 return false;
5110 /* Abstract decls do not need an assembler name. */
5111 if (DECL_ABSTRACT_P (decl))
5112 return false;
5114 /* For VAR_DECLs, only static, public and external symbols need an
5115 assembler name. */
5116 if (TREE_CODE (decl) == VAR_DECL
5117 && !TREE_STATIC (decl)
5118 && !TREE_PUBLIC (decl)
5119 && !DECL_EXTERNAL (decl))
5120 return false;
5122 if (TREE_CODE (decl) == FUNCTION_DECL)
5124 /* Do not set assembler name on builtins. Allow RTL expansion to
5125 decide whether to expand inline or via a regular call. */
5126 if (DECL_BUILT_IN (decl)
5127 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
5128 return false;
5130 /* Functions represented in the callgraph need an assembler name. */
5131 if (cgraph_node::get (decl) != NULL)
5132 return true;
5134 /* Unused and not public functions don't need an assembler name. */
5135 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
5136 return false;
5139 return true;
5143 /* Reset all language specific information still present in symbol
5144 DECL. */
5146 static void
5147 free_lang_data_in_decl (tree decl)
5149 gcc_assert (DECL_P (decl));
5151 /* Give the FE a chance to remove its own data first. */
5152 lang_hooks.free_lang_data (decl);
5154 TREE_LANG_FLAG_0 (decl) = 0;
5155 TREE_LANG_FLAG_1 (decl) = 0;
5156 TREE_LANG_FLAG_2 (decl) = 0;
5157 TREE_LANG_FLAG_3 (decl) = 0;
5158 TREE_LANG_FLAG_4 (decl) = 0;
5159 TREE_LANG_FLAG_5 (decl) = 0;
5160 TREE_LANG_FLAG_6 (decl) = 0;
5162 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
5163 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
5164 if (TREE_CODE (decl) == FIELD_DECL)
5166 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
5167 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
5168 DECL_QUALIFIER (decl) = NULL_TREE;
5171 if (TREE_CODE (decl) == FUNCTION_DECL)
5173 struct cgraph_node *node;
5174 if (!(node = cgraph_node::get (decl))
5175 || (!node->definition && !node->clones))
5177 if (node)
5178 node->release_body ();
5179 else
5181 release_function_body (decl);
5182 DECL_ARGUMENTS (decl) = NULL;
5183 DECL_RESULT (decl) = NULL;
5184 DECL_INITIAL (decl) = error_mark_node;
5187 if (gimple_has_body_p (decl))
5189 tree t;
5191 /* If DECL has a gimple body, then the context for its
5192 arguments must be DECL. Otherwise, it doesn't really
5193 matter, as we will not be emitting any code for DECL. In
5194 general, there may be other instances of DECL created by
5195 the front end and since PARM_DECLs are generally shared,
5196 their DECL_CONTEXT changes as the replicas of DECL are
5197 created. The only time where DECL_CONTEXT is important
5198 is for the FUNCTION_DECLs that have a gimple body (since
5199 the PARM_DECL will be used in the function's body). */
5200 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
5201 DECL_CONTEXT (t) = decl;
5202 if (!DECL_FUNCTION_SPECIFIC_TARGET (decl))
5203 DECL_FUNCTION_SPECIFIC_TARGET (decl)
5204 = target_option_default_node;
5205 if (!DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl))
5206 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl)
5207 = optimization_default_node;
5210 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
5211 At this point, it is not needed anymore. */
5212 DECL_SAVED_TREE (decl) = NULL_TREE;
5214 /* Clear the abstract origin if it refers to a method. Otherwise
5215 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
5216 origin will not be output correctly. */
5217 if (DECL_ABSTRACT_ORIGIN (decl)
5218 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
5219 && RECORD_OR_UNION_TYPE_P
5220 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
5221 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
5223 /* Sometimes the C++ frontend doesn't manage to transform a temporary
5224 DECL_VINDEX referring to itself into a vtable slot number as it
5225 should. Happens with functions that are copied and then forgotten
5226 about. Just clear it, it won't matter anymore. */
5227 if (DECL_VINDEX (decl) && !tree_fits_shwi_p (DECL_VINDEX (decl)))
5228 DECL_VINDEX (decl) = NULL_TREE;
5230 else if (TREE_CODE (decl) == VAR_DECL)
5232 if ((DECL_EXTERNAL (decl)
5233 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
5234 || (decl_function_context (decl) && !TREE_STATIC (decl)))
5235 DECL_INITIAL (decl) = NULL_TREE;
5237 else if (TREE_CODE (decl) == TYPE_DECL
5238 || TREE_CODE (decl) == FIELD_DECL)
5239 DECL_INITIAL (decl) = NULL_TREE;
5240 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
5241 && DECL_INITIAL (decl)
5242 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
5244 /* Strip builtins from the translation-unit BLOCK. We still have targets
5245 without builtin_decl_explicit support and also builtins are shared
5246 nodes and thus we can't use TREE_CHAIN in multiple lists. */
5247 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
5248 while (*nextp)
5250 tree var = *nextp;
5251 if (TREE_CODE (var) == FUNCTION_DECL
5252 && DECL_BUILT_IN (var))
5253 *nextp = TREE_CHAIN (var);
5254 else
5255 nextp = &TREE_CHAIN (var);
5261 /* Data used when collecting DECLs and TYPEs for language data removal. */
5263 struct free_lang_data_d
5265 /* Worklist to avoid excessive recursion. */
5266 vec<tree> worklist;
5268 /* Set of traversed objects. Used to avoid duplicate visits. */
5269 hash_set<tree> *pset;
5271 /* Array of symbols to process with free_lang_data_in_decl. */
5272 vec<tree> decls;
5274 /* Array of types to process with free_lang_data_in_type. */
5275 vec<tree> types;
5279 /* Save all language fields needed to generate proper debug information
5280 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
5282 static void
5283 save_debug_info_for_decl (tree t)
5285 /*struct saved_debug_info_d *sdi;*/
5287 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
5289 /* FIXME. Partial implementation for saving debug info removed. */
5293 /* Save all language fields needed to generate proper debug information
5294 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
5296 static void
5297 save_debug_info_for_type (tree t)
5299 /*struct saved_debug_info_d *sdi;*/
5301 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
5303 /* FIXME. Partial implementation for saving debug info removed. */
5307 /* Add type or decl T to one of the list of tree nodes that need their
5308 language data removed. The lists are held inside FLD. */
5310 static void
5311 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
5313 if (DECL_P (t))
5315 fld->decls.safe_push (t);
5316 if (debug_info_level > DINFO_LEVEL_TERSE)
5317 save_debug_info_for_decl (t);
5319 else if (TYPE_P (t))
5321 fld->types.safe_push (t);
5322 if (debug_info_level > DINFO_LEVEL_TERSE)
5323 save_debug_info_for_type (t);
5325 else
5326 gcc_unreachable ();
5329 /* Push tree node T into FLD->WORKLIST. */
5331 static inline void
5332 fld_worklist_push (tree t, struct free_lang_data_d *fld)
5334 if (t && !is_lang_specific (t) && !fld->pset->contains (t))
5335 fld->worklist.safe_push ((t));
5339 /* Operand callback helper for free_lang_data_in_node. *TP is the
5340 subtree operand being considered. */
5342 static tree
5343 find_decls_types_r (tree *tp, int *ws, void *data)
5345 tree t = *tp;
5346 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
5348 if (TREE_CODE (t) == TREE_LIST)
5349 return NULL_TREE;
5351 /* Language specific nodes will be removed, so there is no need
5352 to gather anything under them. */
5353 if (is_lang_specific (t))
5355 *ws = 0;
5356 return NULL_TREE;
5359 if (DECL_P (t))
5361 /* Note that walk_tree does not traverse every possible field in
5362 decls, so we have to do our own traversals here. */
5363 add_tree_to_fld_list (t, fld);
5365 fld_worklist_push (DECL_NAME (t), fld);
5366 fld_worklist_push (DECL_CONTEXT (t), fld);
5367 fld_worklist_push (DECL_SIZE (t), fld);
5368 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
5370 /* We are going to remove everything under DECL_INITIAL for
5371 TYPE_DECLs. No point walking them. */
5372 if (TREE_CODE (t) != TYPE_DECL)
5373 fld_worklist_push (DECL_INITIAL (t), fld);
5375 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
5376 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
5378 if (TREE_CODE (t) == FUNCTION_DECL)
5380 fld_worklist_push (DECL_ARGUMENTS (t), fld);
5381 fld_worklist_push (DECL_RESULT (t), fld);
5383 else if (TREE_CODE (t) == TYPE_DECL)
5385 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
5387 else if (TREE_CODE (t) == FIELD_DECL)
5389 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
5390 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
5391 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
5392 fld_worklist_push (DECL_FCONTEXT (t), fld);
5395 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
5396 && DECL_HAS_VALUE_EXPR_P (t))
5397 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
5399 if (TREE_CODE (t) != FIELD_DECL
5400 && TREE_CODE (t) != TYPE_DECL)
5401 fld_worklist_push (TREE_CHAIN (t), fld);
5402 *ws = 0;
5404 else if (TYPE_P (t))
5406 /* Note that walk_tree does not traverse every possible field in
5407 types, so we have to do our own traversals here. */
5408 add_tree_to_fld_list (t, fld);
5410 if (!RECORD_OR_UNION_TYPE_P (t))
5411 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
5412 fld_worklist_push (TYPE_SIZE (t), fld);
5413 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
5414 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
5415 fld_worklist_push (TYPE_POINTER_TO (t), fld);
5416 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
5417 fld_worklist_push (TYPE_NAME (t), fld);
5418 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
5419 them and thus do not and want not to reach unused pointer types
5420 this way. */
5421 if (!POINTER_TYPE_P (t))
5422 fld_worklist_push (TYPE_MINVAL (t), fld);
5423 if (!RECORD_OR_UNION_TYPE_P (t))
5424 fld_worklist_push (TYPE_MAXVAL (t), fld);
5425 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
5426 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
5427 do not and want not to reach unused variants this way. */
5428 if (TYPE_CONTEXT (t))
5430 tree ctx = TYPE_CONTEXT (t);
5431 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
5432 So push that instead. */
5433 while (ctx && TREE_CODE (ctx) == BLOCK)
5434 ctx = BLOCK_SUPERCONTEXT (ctx);
5435 fld_worklist_push (ctx, fld);
5437 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
5438 and want not to reach unused types this way. */
5440 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
5442 unsigned i;
5443 tree tem;
5444 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
5445 fld_worklist_push (TREE_TYPE (tem), fld);
5446 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
5447 if (tem
5448 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
5449 && TREE_CODE (tem) == TREE_LIST)
5452 fld_worklist_push (TREE_VALUE (tem), fld);
5453 tem = TREE_CHAIN (tem);
5455 while (tem);
5457 if (RECORD_OR_UNION_TYPE_P (t))
5459 tree tem;
5460 /* Push all TYPE_FIELDS - there can be interleaving interesting
5461 and non-interesting things. */
5462 tem = TYPE_FIELDS (t);
5463 while (tem)
5465 if (TREE_CODE (tem) == FIELD_DECL
5466 || TREE_CODE (tem) == TYPE_DECL)
5467 fld_worklist_push (tem, fld);
5468 tem = TREE_CHAIN (tem);
5472 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5473 *ws = 0;
5475 else if (TREE_CODE (t) == BLOCK)
5477 tree tem;
5478 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5479 fld_worklist_push (tem, fld);
5480 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5481 fld_worklist_push (tem, fld);
5482 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5485 if (TREE_CODE (t) != IDENTIFIER_NODE
5486 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5487 fld_worklist_push (TREE_TYPE (t), fld);
5489 return NULL_TREE;
5493 /* Find decls and types in T. */
5495 static void
5496 find_decls_types (tree t, struct free_lang_data_d *fld)
5498 while (1)
5500 if (!fld->pset->contains (t))
5501 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5502 if (fld->worklist.is_empty ())
5503 break;
5504 t = fld->worklist.pop ();
5508 /* Translate all the types in LIST with the corresponding runtime
5509 types. */
5511 static tree
5512 get_eh_types_for_runtime (tree list)
5514 tree head, prev;
5516 if (list == NULL_TREE)
5517 return NULL_TREE;
5519 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5520 prev = head;
5521 list = TREE_CHAIN (list);
5522 while (list)
5524 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5525 TREE_CHAIN (prev) = n;
5526 prev = TREE_CHAIN (prev);
5527 list = TREE_CHAIN (list);
5530 return head;
5534 /* Find decls and types referenced in EH region R and store them in
5535 FLD->DECLS and FLD->TYPES. */
5537 static void
5538 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5540 switch (r->type)
5542 case ERT_CLEANUP:
5543 break;
5545 case ERT_TRY:
5547 eh_catch c;
5549 /* The types referenced in each catch must first be changed to the
5550 EH types used at runtime. This removes references to FE types
5551 in the region. */
5552 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5554 c->type_list = get_eh_types_for_runtime (c->type_list);
5555 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5558 break;
5560 case ERT_ALLOWED_EXCEPTIONS:
5561 r->u.allowed.type_list
5562 = get_eh_types_for_runtime (r->u.allowed.type_list);
5563 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5564 break;
5566 case ERT_MUST_NOT_THROW:
5567 walk_tree (&r->u.must_not_throw.failure_decl,
5568 find_decls_types_r, fld, fld->pset);
5569 break;
5574 /* Find decls and types referenced in cgraph node N and store them in
5575 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5576 look for *every* kind of DECL and TYPE node reachable from N,
5577 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5578 NAMESPACE_DECLs, etc). */
5580 static void
5581 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5583 basic_block bb;
5584 struct function *fn;
5585 unsigned ix;
5586 tree t;
5588 find_decls_types (n->decl, fld);
5590 if (!gimple_has_body_p (n->decl))
5591 return;
5593 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5595 fn = DECL_STRUCT_FUNCTION (n->decl);
5597 /* Traverse locals. */
5598 FOR_EACH_LOCAL_DECL (fn, ix, t)
5599 find_decls_types (t, fld);
5601 /* Traverse EH regions in FN. */
5603 eh_region r;
5604 FOR_ALL_EH_REGION_FN (r, fn)
5605 find_decls_types_in_eh_region (r, fld);
5608 /* Traverse every statement in FN. */
5609 FOR_EACH_BB_FN (bb, fn)
5611 gphi_iterator psi;
5612 gimple_stmt_iterator si;
5613 unsigned i;
5615 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
5617 gphi *phi = psi.phi ();
5619 for (i = 0; i < gimple_phi_num_args (phi); i++)
5621 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5622 find_decls_types (*arg_p, fld);
5626 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5628 gimple stmt = gsi_stmt (si);
5630 if (is_gimple_call (stmt))
5631 find_decls_types (gimple_call_fntype (stmt), fld);
5633 for (i = 0; i < gimple_num_ops (stmt); i++)
5635 tree arg = gimple_op (stmt, i);
5636 find_decls_types (arg, fld);
5643 /* Find decls and types referenced in varpool node N and store them in
5644 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5645 look for *every* kind of DECL and TYPE node reachable from N,
5646 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5647 NAMESPACE_DECLs, etc). */
5649 static void
5650 find_decls_types_in_var (varpool_node *v, struct free_lang_data_d *fld)
5652 find_decls_types (v->decl, fld);
5655 /* If T needs an assembler name, have one created for it. */
5657 void
5658 assign_assembler_name_if_neeeded (tree t)
5660 if (need_assembler_name_p (t))
5662 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5663 diagnostics that use input_location to show locus
5664 information. The problem here is that, at this point,
5665 input_location is generally anchored to the end of the file
5666 (since the parser is long gone), so we don't have a good
5667 position to pin it to.
5669 To alleviate this problem, this uses the location of T's
5670 declaration. Examples of this are
5671 testsuite/g++.dg/template/cond2.C and
5672 testsuite/g++.dg/template/pr35240.C. */
5673 location_t saved_location = input_location;
5674 input_location = DECL_SOURCE_LOCATION (t);
5676 decl_assembler_name (t);
5678 input_location = saved_location;
5683 /* Free language specific information for every operand and expression
5684 in every node of the call graph. This process operates in three stages:
5686 1- Every callgraph node and varpool node is traversed looking for
5687 decls and types embedded in them. This is a more exhaustive
5688 search than that done by find_referenced_vars, because it will
5689 also collect individual fields, decls embedded in types, etc.
5691 2- All the decls found are sent to free_lang_data_in_decl.
5693 3- All the types found are sent to free_lang_data_in_type.
5695 The ordering between decls and types is important because
5696 free_lang_data_in_decl sets assembler names, which includes
5697 mangling. So types cannot be freed up until assembler names have
5698 been set up. */
5700 static void
5701 free_lang_data_in_cgraph (void)
5703 struct cgraph_node *n;
5704 varpool_node *v;
5705 struct free_lang_data_d fld;
5706 tree t;
5707 unsigned i;
5708 alias_pair *p;
5710 /* Initialize sets and arrays to store referenced decls and types. */
5711 fld.pset = new hash_set<tree>;
5712 fld.worklist.create (0);
5713 fld.decls.create (100);
5714 fld.types.create (100);
5716 /* Find decls and types in the body of every function in the callgraph. */
5717 FOR_EACH_FUNCTION (n)
5718 find_decls_types_in_node (n, &fld);
5720 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5721 find_decls_types (p->decl, &fld);
5723 /* Find decls and types in every varpool symbol. */
5724 FOR_EACH_VARIABLE (v)
5725 find_decls_types_in_var (v, &fld);
5727 /* Set the assembler name on every decl found. We need to do this
5728 now because free_lang_data_in_decl will invalidate data needed
5729 for mangling. This breaks mangling on interdependent decls. */
5730 FOR_EACH_VEC_ELT (fld.decls, i, t)
5731 assign_assembler_name_if_neeeded (t);
5733 /* Traverse every decl found freeing its language data. */
5734 FOR_EACH_VEC_ELT (fld.decls, i, t)
5735 free_lang_data_in_decl (t);
5737 /* Traverse every type found freeing its language data. */
5738 FOR_EACH_VEC_ELT (fld.types, i, t)
5739 free_lang_data_in_type (t);
5741 delete fld.pset;
5742 fld.worklist.release ();
5743 fld.decls.release ();
5744 fld.types.release ();
5748 /* Free resources that are used by FE but are not needed once they are done. */
5750 static unsigned
5751 free_lang_data (void)
5753 unsigned i;
5755 /* If we are the LTO frontend we have freed lang-specific data already. */
5756 if (in_lto_p
5757 || (!flag_generate_lto && !flag_generate_offload))
5758 return 0;
5760 /* Allocate and assign alias sets to the standard integer types
5761 while the slots are still in the way the frontends generated them. */
5762 for (i = 0; i < itk_none; ++i)
5763 if (integer_types[i])
5764 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5766 /* Traverse the IL resetting language specific information for
5767 operands, expressions, etc. */
5768 free_lang_data_in_cgraph ();
5770 /* Create gimple variants for common types. */
5771 ptrdiff_type_node = integer_type_node;
5772 fileptr_type_node = ptr_type_node;
5774 /* Reset some langhooks. Do not reset types_compatible_p, it may
5775 still be used indirectly via the get_alias_set langhook. */
5776 lang_hooks.dwarf_name = lhd_dwarf_name;
5777 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5778 /* We do not want the default decl_assembler_name implementation,
5779 rather if we have fixed everything we want a wrapper around it
5780 asserting that all non-local symbols already got their assembler
5781 name and only produce assembler names for local symbols. Or rather
5782 make sure we never call decl_assembler_name on local symbols and
5783 devise a separate, middle-end private scheme for it. */
5785 /* Reset diagnostic machinery. */
5786 tree_diagnostics_defaults (global_dc);
5788 return 0;
5792 namespace {
5794 const pass_data pass_data_ipa_free_lang_data =
5796 SIMPLE_IPA_PASS, /* type */
5797 "*free_lang_data", /* name */
5798 OPTGROUP_NONE, /* optinfo_flags */
5799 TV_IPA_FREE_LANG_DATA, /* tv_id */
5800 0, /* properties_required */
5801 0, /* properties_provided */
5802 0, /* properties_destroyed */
5803 0, /* todo_flags_start */
5804 0, /* todo_flags_finish */
5807 class pass_ipa_free_lang_data : public simple_ipa_opt_pass
5809 public:
5810 pass_ipa_free_lang_data (gcc::context *ctxt)
5811 : simple_ipa_opt_pass (pass_data_ipa_free_lang_data, ctxt)
5814 /* opt_pass methods: */
5815 virtual unsigned int execute (function *) { return free_lang_data (); }
5817 }; // class pass_ipa_free_lang_data
5819 } // anon namespace
5821 simple_ipa_opt_pass *
5822 make_pass_ipa_free_lang_data (gcc::context *ctxt)
5824 return new pass_ipa_free_lang_data (ctxt);
5827 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
5828 ATTR_NAME. Also used internally by remove_attribute(). */
5829 bool
5830 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
5832 size_t ident_len = IDENTIFIER_LENGTH (ident);
5834 if (ident_len == attr_len)
5836 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
5837 return true;
5839 else if (ident_len == attr_len + 4)
5841 /* There is the possibility that ATTR is 'text' and IDENT is
5842 '__text__'. */
5843 const char *p = IDENTIFIER_POINTER (ident);
5844 if (p[0] == '_' && p[1] == '_'
5845 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5846 && strncmp (attr_name, p + 2, attr_len) == 0)
5847 return true;
5850 return false;
5853 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
5854 of ATTR_NAME, and LIST is not NULL_TREE. */
5855 tree
5856 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
5858 while (list)
5860 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5862 if (ident_len == attr_len)
5864 if (!strcmp (attr_name,
5865 IDENTIFIER_POINTER (get_attribute_name (list))))
5866 break;
5868 /* TODO: If we made sure that attributes were stored in the
5869 canonical form without '__...__' (ie, as in 'text' as opposed
5870 to '__text__') then we could avoid the following case. */
5871 else if (ident_len == attr_len + 4)
5873 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5874 if (p[0] == '_' && p[1] == '_'
5875 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5876 && strncmp (attr_name, p + 2, attr_len) == 0)
5877 break;
5879 list = TREE_CHAIN (list);
5882 return list;
5885 /* Given an attribute name ATTR_NAME and a list of attributes LIST,
5886 return a pointer to the attribute's list first element if the attribute
5887 starts with ATTR_NAME. ATTR_NAME must be in the form 'text' (not
5888 '__text__'). */
5890 tree
5891 private_lookup_attribute_by_prefix (const char *attr_name, size_t attr_len,
5892 tree list)
5894 while (list)
5896 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5898 if (attr_len > ident_len)
5900 list = TREE_CHAIN (list);
5901 continue;
5904 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5906 if (strncmp (attr_name, p, attr_len) == 0)
5907 break;
5909 /* TODO: If we made sure that attributes were stored in the
5910 canonical form without '__...__' (ie, as in 'text' as opposed
5911 to '__text__') then we could avoid the following case. */
5912 if (p[0] == '_' && p[1] == '_' &&
5913 strncmp (attr_name, p + 2, attr_len) == 0)
5914 break;
5916 list = TREE_CHAIN (list);
5919 return list;
5923 /* A variant of lookup_attribute() that can be used with an identifier
5924 as the first argument, and where the identifier can be either
5925 'text' or '__text__'.
5927 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
5928 return a pointer to the attribute's list element if the attribute
5929 is part of the list, or NULL_TREE if not found. If the attribute
5930 appears more than once, this only returns the first occurrence; the
5931 TREE_CHAIN of the return value should be passed back in if further
5932 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
5933 can be in the form 'text' or '__text__'. */
5934 static tree
5935 lookup_ident_attribute (tree attr_identifier, tree list)
5937 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
5939 while (list)
5941 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
5942 == IDENTIFIER_NODE);
5944 /* Identifiers can be compared directly for equality. */
5945 if (attr_identifier == get_attribute_name (list))
5946 break;
5948 /* If they are not equal, they may still be one in the form
5949 'text' while the other one is in the form '__text__'. TODO:
5950 If we were storing attributes in normalized 'text' form, then
5951 this could all go away and we could take full advantage of
5952 the fact that we're comparing identifiers. :-) */
5954 size_t attr_len = IDENTIFIER_LENGTH (attr_identifier);
5955 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5957 if (ident_len == attr_len + 4)
5959 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5960 const char *q = IDENTIFIER_POINTER (attr_identifier);
5961 if (p[0] == '_' && p[1] == '_'
5962 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5963 && strncmp (q, p + 2, attr_len) == 0)
5964 break;
5966 else if (ident_len + 4 == attr_len)
5968 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5969 const char *q = IDENTIFIER_POINTER (attr_identifier);
5970 if (q[0] == '_' && q[1] == '_'
5971 && q[attr_len - 2] == '_' && q[attr_len - 1] == '_'
5972 && strncmp (q + 2, p, ident_len) == 0)
5973 break;
5976 list = TREE_CHAIN (list);
5979 return list;
5982 /* Remove any instances of attribute ATTR_NAME in LIST and return the
5983 modified list. */
5985 tree
5986 remove_attribute (const char *attr_name, tree list)
5988 tree *p;
5989 size_t attr_len = strlen (attr_name);
5991 gcc_checking_assert (attr_name[0] != '_');
5993 for (p = &list; *p; )
5995 tree l = *p;
5996 /* TODO: If we were storing attributes in normalized form, here
5997 we could use a simple strcmp(). */
5998 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
5999 *p = TREE_CHAIN (l);
6000 else
6001 p = &TREE_CHAIN (l);
6004 return list;
6007 /* Return an attribute list that is the union of a1 and a2. */
6009 tree
6010 merge_attributes (tree a1, tree a2)
6012 tree attributes;
6014 /* Either one unset? Take the set one. */
6016 if ((attributes = a1) == 0)
6017 attributes = a2;
6019 /* One that completely contains the other? Take it. */
6021 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
6023 if (attribute_list_contained (a2, a1))
6024 attributes = a2;
6025 else
6027 /* Pick the longest list, and hang on the other list. */
6029 if (list_length (a1) < list_length (a2))
6030 attributes = a2, a2 = a1;
6032 for (; a2 != 0; a2 = TREE_CHAIN (a2))
6034 tree a;
6035 for (a = lookup_ident_attribute (get_attribute_name (a2),
6036 attributes);
6037 a != NULL_TREE && !attribute_value_equal (a, a2);
6038 a = lookup_ident_attribute (get_attribute_name (a2),
6039 TREE_CHAIN (a)))
6041 if (a == NULL_TREE)
6043 a1 = copy_node (a2);
6044 TREE_CHAIN (a1) = attributes;
6045 attributes = a1;
6050 return attributes;
6053 /* Given types T1 and T2, merge their attributes and return
6054 the result. */
6056 tree
6057 merge_type_attributes (tree t1, tree t2)
6059 return merge_attributes (TYPE_ATTRIBUTES (t1),
6060 TYPE_ATTRIBUTES (t2));
6063 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
6064 the result. */
6066 tree
6067 merge_decl_attributes (tree olddecl, tree newdecl)
6069 return merge_attributes (DECL_ATTRIBUTES (olddecl),
6070 DECL_ATTRIBUTES (newdecl));
6073 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
6075 /* Specialization of merge_decl_attributes for various Windows targets.
6077 This handles the following situation:
6079 __declspec (dllimport) int foo;
6080 int foo;
6082 The second instance of `foo' nullifies the dllimport. */
6084 tree
6085 merge_dllimport_decl_attributes (tree old, tree new_tree)
6087 tree a;
6088 int delete_dllimport_p = 1;
6090 /* What we need to do here is remove from `old' dllimport if it doesn't
6091 appear in `new'. dllimport behaves like extern: if a declaration is
6092 marked dllimport and a definition appears later, then the object
6093 is not dllimport'd. We also remove a `new' dllimport if the old list
6094 contains dllexport: dllexport always overrides dllimport, regardless
6095 of the order of declaration. */
6096 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
6097 delete_dllimport_p = 0;
6098 else if (DECL_DLLIMPORT_P (new_tree)
6099 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
6101 DECL_DLLIMPORT_P (new_tree) = 0;
6102 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
6103 "dllimport ignored", new_tree);
6105 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
6107 /* Warn about overriding a symbol that has already been used, e.g.:
6108 extern int __attribute__ ((dllimport)) foo;
6109 int* bar () {return &foo;}
6110 int foo;
6112 if (TREE_USED (old))
6114 warning (0, "%q+D redeclared without dllimport attribute "
6115 "after being referenced with dll linkage", new_tree);
6116 /* If we have used a variable's address with dllimport linkage,
6117 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
6118 decl may already have had TREE_CONSTANT computed.
6119 We still remove the attribute so that assembler code refers
6120 to '&foo rather than '_imp__foo'. */
6121 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
6122 DECL_DLLIMPORT_P (new_tree) = 1;
6125 /* Let an inline definition silently override the external reference,
6126 but otherwise warn about attribute inconsistency. */
6127 else if (TREE_CODE (new_tree) == VAR_DECL
6128 || !DECL_DECLARED_INLINE_P (new_tree))
6129 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
6130 "previous dllimport ignored", new_tree);
6132 else
6133 delete_dllimport_p = 0;
6135 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
6137 if (delete_dllimport_p)
6138 a = remove_attribute ("dllimport", a);
6140 return a;
6143 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
6144 struct attribute_spec.handler. */
6146 tree
6147 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
6148 bool *no_add_attrs)
6150 tree node = *pnode;
6151 bool is_dllimport;
6153 /* These attributes may apply to structure and union types being created,
6154 but otherwise should pass to the declaration involved. */
6155 if (!DECL_P (node))
6157 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
6158 | (int) ATTR_FLAG_ARRAY_NEXT))
6160 *no_add_attrs = true;
6161 return tree_cons (name, args, NULL_TREE);
6163 if (TREE_CODE (node) == RECORD_TYPE
6164 || TREE_CODE (node) == UNION_TYPE)
6166 node = TYPE_NAME (node);
6167 if (!node)
6168 return NULL_TREE;
6170 else
6172 warning (OPT_Wattributes, "%qE attribute ignored",
6173 name);
6174 *no_add_attrs = true;
6175 return NULL_TREE;
6179 if (TREE_CODE (node) != FUNCTION_DECL
6180 && TREE_CODE (node) != VAR_DECL
6181 && TREE_CODE (node) != TYPE_DECL)
6183 *no_add_attrs = true;
6184 warning (OPT_Wattributes, "%qE attribute ignored",
6185 name);
6186 return NULL_TREE;
6189 if (TREE_CODE (node) == TYPE_DECL
6190 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
6191 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
6193 *no_add_attrs = true;
6194 warning (OPT_Wattributes, "%qE attribute ignored",
6195 name);
6196 return NULL_TREE;
6199 is_dllimport = is_attribute_p ("dllimport", name);
6201 /* Report error on dllimport ambiguities seen now before they cause
6202 any damage. */
6203 if (is_dllimport)
6205 /* Honor any target-specific overrides. */
6206 if (!targetm.valid_dllimport_attribute_p (node))
6207 *no_add_attrs = true;
6209 else if (TREE_CODE (node) == FUNCTION_DECL
6210 && DECL_DECLARED_INLINE_P (node))
6212 warning (OPT_Wattributes, "inline function %q+D declared as "
6213 " dllimport: attribute ignored", node);
6214 *no_add_attrs = true;
6216 /* Like MS, treat definition of dllimported variables and
6217 non-inlined functions on declaration as syntax errors. */
6218 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
6220 error ("function %q+D definition is marked dllimport", node);
6221 *no_add_attrs = true;
6224 else if (TREE_CODE (node) == VAR_DECL)
6226 if (DECL_INITIAL (node))
6228 error ("variable %q+D definition is marked dllimport",
6229 node);
6230 *no_add_attrs = true;
6233 /* `extern' needn't be specified with dllimport.
6234 Specify `extern' now and hope for the best. Sigh. */
6235 DECL_EXTERNAL (node) = 1;
6236 /* Also, implicitly give dllimport'd variables declared within
6237 a function global scope, unless declared static. */
6238 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
6239 TREE_PUBLIC (node) = 1;
6242 if (*no_add_attrs == false)
6243 DECL_DLLIMPORT_P (node) = 1;
6245 else if (TREE_CODE (node) == FUNCTION_DECL
6246 && DECL_DECLARED_INLINE_P (node)
6247 && flag_keep_inline_dllexport)
6248 /* An exported function, even if inline, must be emitted. */
6249 DECL_EXTERNAL (node) = 0;
6251 /* Report error if symbol is not accessible at global scope. */
6252 if (!TREE_PUBLIC (node)
6253 && (TREE_CODE (node) == VAR_DECL
6254 || TREE_CODE (node) == FUNCTION_DECL))
6256 error ("external linkage required for symbol %q+D because of "
6257 "%qE attribute", node, name);
6258 *no_add_attrs = true;
6261 /* A dllexport'd entity must have default visibility so that other
6262 program units (shared libraries or the main executable) can see
6263 it. A dllimport'd entity must have default visibility so that
6264 the linker knows that undefined references within this program
6265 unit can be resolved by the dynamic linker. */
6266 if (!*no_add_attrs)
6268 if (DECL_VISIBILITY_SPECIFIED (node)
6269 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
6270 error ("%qE implies default visibility, but %qD has already "
6271 "been declared with a different visibility",
6272 name, node);
6273 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
6274 DECL_VISIBILITY_SPECIFIED (node) = 1;
6277 return NULL_TREE;
6280 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
6282 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
6283 of the various TYPE_QUAL values. */
6285 static void
6286 set_type_quals (tree type, int type_quals)
6288 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
6289 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
6290 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
6291 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
6292 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
6295 /* Returns true iff unqualified CAND and BASE are equivalent. */
6297 bool
6298 check_base_type (const_tree cand, const_tree base)
6300 return (TYPE_NAME (cand) == TYPE_NAME (base)
6301 /* Apparently this is needed for Objective-C. */
6302 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6303 /* Check alignment. */
6304 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
6305 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6306 TYPE_ATTRIBUTES (base)));
6309 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
6311 bool
6312 check_qualified_type (const_tree cand, const_tree base, int type_quals)
6314 return (TYPE_QUALS (cand) == type_quals
6315 && check_base_type (cand, base));
6318 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
6320 static bool
6321 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
6323 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
6324 && TYPE_NAME (cand) == TYPE_NAME (base)
6325 /* Apparently this is needed for Objective-C. */
6326 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6327 /* Check alignment. */
6328 && TYPE_ALIGN (cand) == align
6329 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6330 TYPE_ATTRIBUTES (base)));
6333 /* This function checks to see if TYPE matches the size one of the built-in
6334 atomic types, and returns that core atomic type. */
6336 static tree
6337 find_atomic_core_type (tree type)
6339 tree base_atomic_type;
6341 /* Only handle complete types. */
6342 if (TYPE_SIZE (type) == NULL_TREE)
6343 return NULL_TREE;
6345 HOST_WIDE_INT type_size = tree_to_uhwi (TYPE_SIZE (type));
6346 switch (type_size)
6348 case 8:
6349 base_atomic_type = atomicQI_type_node;
6350 break;
6352 case 16:
6353 base_atomic_type = atomicHI_type_node;
6354 break;
6356 case 32:
6357 base_atomic_type = atomicSI_type_node;
6358 break;
6360 case 64:
6361 base_atomic_type = atomicDI_type_node;
6362 break;
6364 case 128:
6365 base_atomic_type = atomicTI_type_node;
6366 break;
6368 default:
6369 base_atomic_type = NULL_TREE;
6372 return base_atomic_type;
6375 /* Return a version of the TYPE, qualified as indicated by the
6376 TYPE_QUALS, if one exists. If no qualified version exists yet,
6377 return NULL_TREE. */
6379 tree
6380 get_qualified_type (tree type, int type_quals)
6382 tree t;
6384 if (TYPE_QUALS (type) == type_quals)
6385 return type;
6387 /* Search the chain of variants to see if there is already one there just
6388 like the one we need to have. If so, use that existing one. We must
6389 preserve the TYPE_NAME, since there is code that depends on this. */
6390 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6391 if (check_qualified_type (t, type, type_quals))
6392 return t;
6394 return NULL_TREE;
6397 /* Like get_qualified_type, but creates the type if it does not
6398 exist. This function never returns NULL_TREE. */
6400 tree
6401 build_qualified_type (tree type, int type_quals)
6403 tree t;
6405 /* See if we already have the appropriate qualified variant. */
6406 t = get_qualified_type (type, type_quals);
6408 /* If not, build it. */
6409 if (!t)
6411 t = build_variant_type_copy (type);
6412 set_type_quals (t, type_quals);
6414 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
6416 /* See if this object can map to a basic atomic type. */
6417 tree atomic_type = find_atomic_core_type (type);
6418 if (atomic_type)
6420 /* Ensure the alignment of this type is compatible with
6421 the required alignment of the atomic type. */
6422 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
6423 TYPE_ALIGN (t) = TYPE_ALIGN (atomic_type);
6427 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6428 /* Propagate structural equality. */
6429 SET_TYPE_STRUCTURAL_EQUALITY (t);
6430 else if (TYPE_CANONICAL (type) != type)
6431 /* Build the underlying canonical type, since it is different
6432 from TYPE. */
6434 tree c = build_qualified_type (TYPE_CANONICAL (type), type_quals);
6435 TYPE_CANONICAL (t) = TYPE_CANONICAL (c);
6437 else
6438 /* T is its own canonical type. */
6439 TYPE_CANONICAL (t) = t;
6443 return t;
6446 /* Create a variant of type T with alignment ALIGN. */
6448 tree
6449 build_aligned_type (tree type, unsigned int align)
6451 tree t;
6453 if (TYPE_PACKED (type)
6454 || TYPE_ALIGN (type) == align)
6455 return type;
6457 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6458 if (check_aligned_type (t, type, align))
6459 return t;
6461 t = build_variant_type_copy (type);
6462 TYPE_ALIGN (t) = align;
6464 return t;
6467 /* Create a new distinct copy of TYPE. The new type is made its own
6468 MAIN_VARIANT. If TYPE requires structural equality checks, the
6469 resulting type requires structural equality checks; otherwise, its
6470 TYPE_CANONICAL points to itself. */
6472 tree
6473 build_distinct_type_copy (tree type)
6475 tree t = copy_node (type);
6477 TYPE_POINTER_TO (t) = 0;
6478 TYPE_REFERENCE_TO (t) = 0;
6480 /* Set the canonical type either to a new equivalence class, or
6481 propagate the need for structural equality checks. */
6482 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6483 SET_TYPE_STRUCTURAL_EQUALITY (t);
6484 else
6485 TYPE_CANONICAL (t) = t;
6487 /* Make it its own variant. */
6488 TYPE_MAIN_VARIANT (t) = t;
6489 TYPE_NEXT_VARIANT (t) = 0;
6491 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
6492 whose TREE_TYPE is not t. This can also happen in the Ada
6493 frontend when using subtypes. */
6495 return t;
6498 /* Create a new variant of TYPE, equivalent but distinct. This is so
6499 the caller can modify it. TYPE_CANONICAL for the return type will
6500 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
6501 are considered equal by the language itself (or that both types
6502 require structural equality checks). */
6504 tree
6505 build_variant_type_copy (tree type)
6507 tree t, m = TYPE_MAIN_VARIANT (type);
6509 t = build_distinct_type_copy (type);
6511 /* Since we're building a variant, assume that it is a non-semantic
6512 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
6513 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
6515 /* Add the new type to the chain of variants of TYPE. */
6516 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
6517 TYPE_NEXT_VARIANT (m) = t;
6518 TYPE_MAIN_VARIANT (t) = m;
6520 return t;
6523 /* Return true if the from tree in both tree maps are equal. */
6526 tree_map_base_eq (const void *va, const void *vb)
6528 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6529 *const b = (const struct tree_map_base *) vb;
6530 return (a->from == b->from);
6533 /* Hash a from tree in a tree_base_map. */
6535 unsigned int
6536 tree_map_base_hash (const void *item)
6538 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6541 /* Return true if this tree map structure is marked for garbage collection
6542 purposes. We simply return true if the from tree is marked, so that this
6543 structure goes away when the from tree goes away. */
6546 tree_map_base_marked_p (const void *p)
6548 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6551 /* Hash a from tree in a tree_map. */
6553 unsigned int
6554 tree_map_hash (const void *item)
6556 return (((const struct tree_map *) item)->hash);
6559 /* Hash a from tree in a tree_decl_map. */
6561 unsigned int
6562 tree_decl_map_hash (const void *item)
6564 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6567 /* Return the initialization priority for DECL. */
6569 priority_type
6570 decl_init_priority_lookup (tree decl)
6572 symtab_node *snode = symtab_node::get (decl);
6574 if (!snode)
6575 return DEFAULT_INIT_PRIORITY;
6576 return
6577 snode->get_init_priority ();
6580 /* Return the finalization priority for DECL. */
6582 priority_type
6583 decl_fini_priority_lookup (tree decl)
6585 cgraph_node *node = cgraph_node::get (decl);
6587 if (!node)
6588 return DEFAULT_INIT_PRIORITY;
6589 return
6590 node->get_fini_priority ();
6593 /* Set the initialization priority for DECL to PRIORITY. */
6595 void
6596 decl_init_priority_insert (tree decl, priority_type priority)
6598 struct symtab_node *snode;
6600 if (priority == DEFAULT_INIT_PRIORITY)
6602 snode = symtab_node::get (decl);
6603 if (!snode)
6604 return;
6606 else if (TREE_CODE (decl) == VAR_DECL)
6607 snode = varpool_node::get_create (decl);
6608 else
6609 snode = cgraph_node::get_create (decl);
6610 snode->set_init_priority (priority);
6613 /* Set the finalization priority for DECL to PRIORITY. */
6615 void
6616 decl_fini_priority_insert (tree decl, priority_type priority)
6618 struct cgraph_node *node;
6620 if (priority == DEFAULT_INIT_PRIORITY)
6622 node = cgraph_node::get (decl);
6623 if (!node)
6624 return;
6626 else
6627 node = cgraph_node::get_create (decl);
6628 node->set_fini_priority (priority);
6631 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6633 static void
6634 print_debug_expr_statistics (void)
6636 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6637 (long) debug_expr_for_decl->size (),
6638 (long) debug_expr_for_decl->elements (),
6639 debug_expr_for_decl->collisions ());
6642 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6644 static void
6645 print_value_expr_statistics (void)
6647 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6648 (long) value_expr_for_decl->size (),
6649 (long) value_expr_for_decl->elements (),
6650 value_expr_for_decl->collisions ());
6653 /* Lookup a debug expression for FROM, and return it if we find one. */
6655 tree
6656 decl_debug_expr_lookup (tree from)
6658 struct tree_decl_map *h, in;
6659 in.base.from = from;
6661 h = debug_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6662 if (h)
6663 return h->to;
6664 return NULL_TREE;
6667 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6669 void
6670 decl_debug_expr_insert (tree from, tree to)
6672 struct tree_decl_map *h;
6674 h = ggc_alloc<tree_decl_map> ();
6675 h->base.from = from;
6676 h->to = to;
6677 *debug_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6680 /* Lookup a value expression for FROM, and return it if we find one. */
6682 tree
6683 decl_value_expr_lookup (tree from)
6685 struct tree_decl_map *h, in;
6686 in.base.from = from;
6688 h = value_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6689 if (h)
6690 return h->to;
6691 return NULL_TREE;
6694 /* Insert a mapping FROM->TO in the value expression hashtable. */
6696 void
6697 decl_value_expr_insert (tree from, tree to)
6699 struct tree_decl_map *h;
6701 h = ggc_alloc<tree_decl_map> ();
6702 h->base.from = from;
6703 h->to = to;
6704 *value_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6707 /* Lookup a vector of debug arguments for FROM, and return it if we
6708 find one. */
6710 vec<tree, va_gc> **
6711 decl_debug_args_lookup (tree from)
6713 struct tree_vec_map *h, in;
6715 if (!DECL_HAS_DEBUG_ARGS_P (from))
6716 return NULL;
6717 gcc_checking_assert (debug_args_for_decl != NULL);
6718 in.base.from = from;
6719 h = debug_args_for_decl->find_with_hash (&in, DECL_UID (from));
6720 if (h)
6721 return &h->to;
6722 return NULL;
6725 /* Insert a mapping FROM->empty vector of debug arguments in the value
6726 expression hashtable. */
6728 vec<tree, va_gc> **
6729 decl_debug_args_insert (tree from)
6731 struct tree_vec_map *h;
6732 tree_vec_map **loc;
6734 if (DECL_HAS_DEBUG_ARGS_P (from))
6735 return decl_debug_args_lookup (from);
6736 if (debug_args_for_decl == NULL)
6737 debug_args_for_decl = hash_table<tree_vec_map_cache_hasher>::create_ggc (64);
6738 h = ggc_alloc<tree_vec_map> ();
6739 h->base.from = from;
6740 h->to = NULL;
6741 loc = debug_args_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT);
6742 *loc = h;
6743 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6744 return &h->to;
6747 /* Hashing of types so that we don't make duplicates.
6748 The entry point is `type_hash_canon'. */
6750 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6751 with types in the TREE_VALUE slots), by adding the hash codes
6752 of the individual types. */
6754 static void
6755 type_hash_list (const_tree list, inchash::hash &hstate)
6757 const_tree tail;
6759 for (tail = list; tail; tail = TREE_CHAIN (tail))
6760 if (TREE_VALUE (tail) != error_mark_node)
6761 hstate.add_object (TYPE_HASH (TREE_VALUE (tail)));
6764 /* These are the Hashtable callback functions. */
6766 /* Returns true iff the types are equivalent. */
6768 bool
6769 type_cache_hasher::equal (type_hash *a, type_hash *b)
6771 /* First test the things that are the same for all types. */
6772 if (a->hash != b->hash
6773 || TREE_CODE (a->type) != TREE_CODE (b->type)
6774 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6775 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6776 TYPE_ATTRIBUTES (b->type))
6777 || (TREE_CODE (a->type) != COMPLEX_TYPE
6778 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6779 return 0;
6781 /* Be careful about comparing arrays before and after the element type
6782 has been completed; don't compare TYPE_ALIGN unless both types are
6783 complete. */
6784 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6785 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6786 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6787 return 0;
6789 switch (TREE_CODE (a->type))
6791 case VOID_TYPE:
6792 case COMPLEX_TYPE:
6793 case POINTER_TYPE:
6794 case REFERENCE_TYPE:
6795 case NULLPTR_TYPE:
6796 return 1;
6798 case VECTOR_TYPE:
6799 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6801 case ENUMERAL_TYPE:
6802 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6803 && !(TYPE_VALUES (a->type)
6804 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6805 && TYPE_VALUES (b->type)
6806 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6807 && type_list_equal (TYPE_VALUES (a->type),
6808 TYPE_VALUES (b->type))))
6809 return 0;
6811 /* ... fall through ... */
6813 case INTEGER_TYPE:
6814 case REAL_TYPE:
6815 case BOOLEAN_TYPE:
6816 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
6817 return false;
6818 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6819 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6820 TYPE_MAX_VALUE (b->type)))
6821 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6822 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6823 TYPE_MIN_VALUE (b->type))));
6825 case FIXED_POINT_TYPE:
6826 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6828 case OFFSET_TYPE:
6829 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6831 case METHOD_TYPE:
6832 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6833 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6834 || (TYPE_ARG_TYPES (a->type)
6835 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6836 && TYPE_ARG_TYPES (b->type)
6837 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6838 && type_list_equal (TYPE_ARG_TYPES (a->type),
6839 TYPE_ARG_TYPES (b->type)))))
6840 break;
6841 return 0;
6842 case ARRAY_TYPE:
6843 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6845 case RECORD_TYPE:
6846 case UNION_TYPE:
6847 case QUAL_UNION_TYPE:
6848 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6849 || (TYPE_FIELDS (a->type)
6850 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6851 && TYPE_FIELDS (b->type)
6852 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6853 && type_list_equal (TYPE_FIELDS (a->type),
6854 TYPE_FIELDS (b->type))));
6856 case FUNCTION_TYPE:
6857 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6858 || (TYPE_ARG_TYPES (a->type)
6859 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6860 && TYPE_ARG_TYPES (b->type)
6861 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6862 && type_list_equal (TYPE_ARG_TYPES (a->type),
6863 TYPE_ARG_TYPES (b->type))))
6864 break;
6865 return 0;
6867 default:
6868 return 0;
6871 if (lang_hooks.types.type_hash_eq != NULL)
6872 return lang_hooks.types.type_hash_eq (a->type, b->type);
6874 return 1;
6877 /* Given TYPE, and HASHCODE its hash code, return the canonical
6878 object for an identical type if one already exists.
6879 Otherwise, return TYPE, and record it as the canonical object.
6881 To use this function, first create a type of the sort you want.
6882 Then compute its hash code from the fields of the type that
6883 make it different from other similar types.
6884 Then call this function and use the value. */
6886 tree
6887 type_hash_canon (unsigned int hashcode, tree type)
6889 type_hash in;
6890 type_hash **loc;
6892 /* The hash table only contains main variants, so ensure that's what we're
6893 being passed. */
6894 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6896 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6897 must call that routine before comparing TYPE_ALIGNs. */
6898 layout_type (type);
6900 in.hash = hashcode;
6901 in.type = type;
6903 loc = type_hash_table->find_slot_with_hash (&in, hashcode, INSERT);
6904 if (*loc)
6906 tree t1 = ((type_hash *) *loc)->type;
6907 gcc_assert (TYPE_MAIN_VARIANT (t1) == t1);
6908 if (GATHER_STATISTICS)
6910 tree_code_counts[(int) TREE_CODE (type)]--;
6911 tree_node_counts[(int) t_kind]--;
6912 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
6914 return t1;
6916 else
6918 struct type_hash *h;
6920 h = ggc_alloc<type_hash> ();
6921 h->hash = hashcode;
6922 h->type = type;
6923 *loc = h;
6925 return type;
6929 static void
6930 print_type_hash_statistics (void)
6932 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6933 (long) type_hash_table->size (),
6934 (long) type_hash_table->elements (),
6935 type_hash_table->collisions ());
6938 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
6939 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
6940 by adding the hash codes of the individual attributes. */
6942 static void
6943 attribute_hash_list (const_tree list, inchash::hash &hstate)
6945 const_tree tail;
6947 for (tail = list; tail; tail = TREE_CHAIN (tail))
6948 /* ??? Do we want to add in TREE_VALUE too? */
6949 hstate.add_object (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)));
6952 /* Given two lists of attributes, return true if list l2 is
6953 equivalent to l1. */
6956 attribute_list_equal (const_tree l1, const_tree l2)
6958 if (l1 == l2)
6959 return 1;
6961 return attribute_list_contained (l1, l2)
6962 && attribute_list_contained (l2, l1);
6965 /* Given two lists of attributes, return true if list L2 is
6966 completely contained within L1. */
6967 /* ??? This would be faster if attribute names were stored in a canonicalized
6968 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
6969 must be used to show these elements are equivalent (which they are). */
6970 /* ??? It's not clear that attributes with arguments will always be handled
6971 correctly. */
6974 attribute_list_contained (const_tree l1, const_tree l2)
6976 const_tree t1, t2;
6978 /* First check the obvious, maybe the lists are identical. */
6979 if (l1 == l2)
6980 return 1;
6982 /* Maybe the lists are similar. */
6983 for (t1 = l1, t2 = l2;
6984 t1 != 0 && t2 != 0
6985 && get_attribute_name (t1) == get_attribute_name (t2)
6986 && TREE_VALUE (t1) == TREE_VALUE (t2);
6987 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6990 /* Maybe the lists are equal. */
6991 if (t1 == 0 && t2 == 0)
6992 return 1;
6994 for (; t2 != 0; t2 = TREE_CHAIN (t2))
6996 const_tree attr;
6997 /* This CONST_CAST is okay because lookup_attribute does not
6998 modify its argument and the return value is assigned to a
6999 const_tree. */
7000 for (attr = lookup_ident_attribute (get_attribute_name (t2),
7001 CONST_CAST_TREE (l1));
7002 attr != NULL_TREE && !attribute_value_equal (t2, attr);
7003 attr = lookup_ident_attribute (get_attribute_name (t2),
7004 TREE_CHAIN (attr)))
7007 if (attr == NULL_TREE)
7008 return 0;
7011 return 1;
7014 /* Given two lists of types
7015 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
7016 return 1 if the lists contain the same types in the same order.
7017 Also, the TREE_PURPOSEs must match. */
7020 type_list_equal (const_tree l1, const_tree l2)
7022 const_tree t1, t2;
7024 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7025 if (TREE_VALUE (t1) != TREE_VALUE (t2)
7026 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
7027 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
7028 && (TREE_TYPE (TREE_PURPOSE (t1))
7029 == TREE_TYPE (TREE_PURPOSE (t2))))))
7030 return 0;
7032 return t1 == t2;
7035 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
7036 given by TYPE. If the argument list accepts variable arguments,
7037 then this function counts only the ordinary arguments. */
7040 type_num_arguments (const_tree type)
7042 int i = 0;
7043 tree t;
7045 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
7046 /* If the function does not take a variable number of arguments,
7047 the last element in the list will have type `void'. */
7048 if (VOID_TYPE_P (TREE_VALUE (t)))
7049 break;
7050 else
7051 ++i;
7053 return i;
7056 /* Nonzero if integer constants T1 and T2
7057 represent the same constant value. */
7060 tree_int_cst_equal (const_tree t1, const_tree t2)
7062 if (t1 == t2)
7063 return 1;
7065 if (t1 == 0 || t2 == 0)
7066 return 0;
7068 if (TREE_CODE (t1) == INTEGER_CST
7069 && TREE_CODE (t2) == INTEGER_CST
7070 && wi::to_widest (t1) == wi::to_widest (t2))
7071 return 1;
7073 return 0;
7076 /* Return true if T is an INTEGER_CST whose numerical value (extended
7077 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
7079 bool
7080 tree_fits_shwi_p (const_tree t)
7082 return (t != NULL_TREE
7083 && TREE_CODE (t) == INTEGER_CST
7084 && wi::fits_shwi_p (wi::to_widest (t)));
7087 /* Return true if T is an INTEGER_CST whose numerical value (extended
7088 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
7090 bool
7091 tree_fits_uhwi_p (const_tree t)
7093 return (t != NULL_TREE
7094 && TREE_CODE (t) == INTEGER_CST
7095 && wi::fits_uhwi_p (wi::to_widest (t)));
7098 /* T is an INTEGER_CST whose numerical value (extended according to
7099 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
7100 HOST_WIDE_INT. */
7102 HOST_WIDE_INT
7103 tree_to_shwi (const_tree t)
7105 gcc_assert (tree_fits_shwi_p (t));
7106 return TREE_INT_CST_LOW (t);
7109 /* T is an INTEGER_CST whose numerical value (extended according to
7110 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
7111 HOST_WIDE_INT. */
7113 unsigned HOST_WIDE_INT
7114 tree_to_uhwi (const_tree t)
7116 gcc_assert (tree_fits_uhwi_p (t));
7117 return TREE_INT_CST_LOW (t);
7120 /* Return the most significant (sign) bit of T. */
7123 tree_int_cst_sign_bit (const_tree t)
7125 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
7127 return wi::extract_uhwi (t, bitno, 1);
7130 /* Return an indication of the sign of the integer constant T.
7131 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
7132 Note that -1 will never be returned if T's type is unsigned. */
7135 tree_int_cst_sgn (const_tree t)
7137 if (wi::eq_p (t, 0))
7138 return 0;
7139 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
7140 return 1;
7141 else if (wi::neg_p (t))
7142 return -1;
7143 else
7144 return 1;
7147 /* Return the minimum number of bits needed to represent VALUE in a
7148 signed or unsigned type, UNSIGNEDP says which. */
7150 unsigned int
7151 tree_int_cst_min_precision (tree value, signop sgn)
7153 /* If the value is negative, compute its negative minus 1. The latter
7154 adjustment is because the absolute value of the largest negative value
7155 is one larger than the largest positive value. This is equivalent to
7156 a bit-wise negation, so use that operation instead. */
7158 if (tree_int_cst_sgn (value) < 0)
7159 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
7161 /* Return the number of bits needed, taking into account the fact
7162 that we need one more bit for a signed than unsigned type.
7163 If value is 0 or -1, the minimum precision is 1 no matter
7164 whether unsignedp is true or false. */
7166 if (integer_zerop (value))
7167 return 1;
7168 else
7169 return tree_floor_log2 (value) + 1 + (sgn == SIGNED ? 1 : 0) ;
7172 /* Return truthvalue of whether T1 is the same tree structure as T2.
7173 Return 1 if they are the same.
7174 Return 0 if they are understandably different.
7175 Return -1 if either contains tree structure not understood by
7176 this function. */
7179 simple_cst_equal (const_tree t1, const_tree t2)
7181 enum tree_code code1, code2;
7182 int cmp;
7183 int i;
7185 if (t1 == t2)
7186 return 1;
7187 if (t1 == 0 || t2 == 0)
7188 return 0;
7190 code1 = TREE_CODE (t1);
7191 code2 = TREE_CODE (t2);
7193 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
7195 if (CONVERT_EXPR_CODE_P (code2)
7196 || code2 == NON_LVALUE_EXPR)
7197 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7198 else
7199 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
7202 else if (CONVERT_EXPR_CODE_P (code2)
7203 || code2 == NON_LVALUE_EXPR)
7204 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
7206 if (code1 != code2)
7207 return 0;
7209 switch (code1)
7211 case INTEGER_CST:
7212 return wi::to_widest (t1) == wi::to_widest (t2);
7214 case REAL_CST:
7215 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
7217 case FIXED_CST:
7218 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
7220 case STRING_CST:
7221 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
7222 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
7223 TREE_STRING_LENGTH (t1)));
7225 case CONSTRUCTOR:
7227 unsigned HOST_WIDE_INT idx;
7228 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
7229 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
7231 if (vec_safe_length (v1) != vec_safe_length (v2))
7232 return false;
7234 for (idx = 0; idx < vec_safe_length (v1); ++idx)
7235 /* ??? Should we handle also fields here? */
7236 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
7237 return false;
7238 return true;
7241 case SAVE_EXPR:
7242 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7244 case CALL_EXPR:
7245 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
7246 if (cmp <= 0)
7247 return cmp;
7248 if (call_expr_nargs (t1) != call_expr_nargs (t2))
7249 return 0;
7251 const_tree arg1, arg2;
7252 const_call_expr_arg_iterator iter1, iter2;
7253 for (arg1 = first_const_call_expr_arg (t1, &iter1),
7254 arg2 = first_const_call_expr_arg (t2, &iter2);
7255 arg1 && arg2;
7256 arg1 = next_const_call_expr_arg (&iter1),
7257 arg2 = next_const_call_expr_arg (&iter2))
7259 cmp = simple_cst_equal (arg1, arg2);
7260 if (cmp <= 0)
7261 return cmp;
7263 return arg1 == arg2;
7266 case TARGET_EXPR:
7267 /* Special case: if either target is an unallocated VAR_DECL,
7268 it means that it's going to be unified with whatever the
7269 TARGET_EXPR is really supposed to initialize, so treat it
7270 as being equivalent to anything. */
7271 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
7272 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
7273 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
7274 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
7275 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
7276 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
7277 cmp = 1;
7278 else
7279 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7281 if (cmp <= 0)
7282 return cmp;
7284 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
7286 case WITH_CLEANUP_EXPR:
7287 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7288 if (cmp <= 0)
7289 return cmp;
7291 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
7293 case COMPONENT_REF:
7294 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
7295 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7297 return 0;
7299 case VAR_DECL:
7300 case PARM_DECL:
7301 case CONST_DECL:
7302 case FUNCTION_DECL:
7303 return 0;
7305 default:
7306 break;
7309 /* This general rule works for most tree codes. All exceptions should be
7310 handled above. If this is a language-specific tree code, we can't
7311 trust what might be in the operand, so say we don't know
7312 the situation. */
7313 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
7314 return -1;
7316 switch (TREE_CODE_CLASS (code1))
7318 case tcc_unary:
7319 case tcc_binary:
7320 case tcc_comparison:
7321 case tcc_expression:
7322 case tcc_reference:
7323 case tcc_statement:
7324 cmp = 1;
7325 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
7327 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
7328 if (cmp <= 0)
7329 return cmp;
7332 return cmp;
7334 default:
7335 return -1;
7339 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
7340 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
7341 than U, respectively. */
7344 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
7346 if (tree_int_cst_sgn (t) < 0)
7347 return -1;
7348 else if (!tree_fits_uhwi_p (t))
7349 return 1;
7350 else if (TREE_INT_CST_LOW (t) == u)
7351 return 0;
7352 else if (TREE_INT_CST_LOW (t) < u)
7353 return -1;
7354 else
7355 return 1;
7358 /* Return true if SIZE represents a constant size that is in bounds of
7359 what the middle-end and the backend accepts (covering not more than
7360 half of the address-space). */
7362 bool
7363 valid_constant_size_p (const_tree size)
7365 if (! tree_fits_uhwi_p (size)
7366 || TREE_OVERFLOW (size)
7367 || tree_int_cst_sign_bit (size) != 0)
7368 return false;
7369 return true;
7372 /* Return the precision of the type, or for a complex or vector type the
7373 precision of the type of its elements. */
7375 unsigned int
7376 element_precision (const_tree type)
7378 enum tree_code code = TREE_CODE (type);
7379 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
7380 type = TREE_TYPE (type);
7382 return TYPE_PRECISION (type);
7385 /* Return true if CODE represents an associative tree code. Otherwise
7386 return false. */
7387 bool
7388 associative_tree_code (enum tree_code code)
7390 switch (code)
7392 case BIT_IOR_EXPR:
7393 case BIT_AND_EXPR:
7394 case BIT_XOR_EXPR:
7395 case PLUS_EXPR:
7396 case MULT_EXPR:
7397 case MIN_EXPR:
7398 case MAX_EXPR:
7399 return true;
7401 default:
7402 break;
7404 return false;
7407 /* Return true if CODE represents a commutative tree code. Otherwise
7408 return false. */
7409 bool
7410 commutative_tree_code (enum tree_code code)
7412 switch (code)
7414 case PLUS_EXPR:
7415 case MULT_EXPR:
7416 case MULT_HIGHPART_EXPR:
7417 case MIN_EXPR:
7418 case MAX_EXPR:
7419 case BIT_IOR_EXPR:
7420 case BIT_XOR_EXPR:
7421 case BIT_AND_EXPR:
7422 case NE_EXPR:
7423 case EQ_EXPR:
7424 case UNORDERED_EXPR:
7425 case ORDERED_EXPR:
7426 case UNEQ_EXPR:
7427 case LTGT_EXPR:
7428 case TRUTH_AND_EXPR:
7429 case TRUTH_XOR_EXPR:
7430 case TRUTH_OR_EXPR:
7431 case WIDEN_MULT_EXPR:
7432 case VEC_WIDEN_MULT_HI_EXPR:
7433 case VEC_WIDEN_MULT_LO_EXPR:
7434 case VEC_WIDEN_MULT_EVEN_EXPR:
7435 case VEC_WIDEN_MULT_ODD_EXPR:
7436 return true;
7438 default:
7439 break;
7441 return false;
7444 /* Return true if CODE represents a ternary tree code for which the
7445 first two operands are commutative. Otherwise return false. */
7446 bool
7447 commutative_ternary_tree_code (enum tree_code code)
7449 switch (code)
7451 case WIDEN_MULT_PLUS_EXPR:
7452 case WIDEN_MULT_MINUS_EXPR:
7453 case DOT_PROD_EXPR:
7454 case FMA_EXPR:
7455 return true;
7457 default:
7458 break;
7460 return false;
7463 namespace inchash
7466 /* Generate a hash value for an expression. This can be used iteratively
7467 by passing a previous result as the HSTATE argument.
7469 This function is intended to produce the same hash for expressions which
7470 would compare equal using operand_equal_p. */
7471 void
7472 add_expr (const_tree t, inchash::hash &hstate)
7474 int i;
7475 enum tree_code code;
7476 enum tree_code_class tclass;
7478 if (t == NULL_TREE)
7480 hstate.merge_hash (0);
7481 return;
7484 code = TREE_CODE (t);
7486 switch (code)
7488 /* Alas, constants aren't shared, so we can't rely on pointer
7489 identity. */
7490 case VOID_CST:
7491 hstate.merge_hash (0);
7492 return;
7493 case INTEGER_CST:
7494 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
7495 hstate.add_wide_int (TREE_INT_CST_ELT (t, i));
7496 return;
7497 case REAL_CST:
7499 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7500 hstate.merge_hash (val2);
7501 return;
7503 case FIXED_CST:
7505 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7506 hstate.merge_hash (val2);
7507 return;
7509 case STRING_CST:
7510 hstate.add ((const void *) TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
7511 return;
7512 case COMPLEX_CST:
7513 inchash::add_expr (TREE_REALPART (t), hstate);
7514 inchash::add_expr (TREE_IMAGPART (t), hstate);
7515 return;
7516 case VECTOR_CST:
7518 unsigned i;
7519 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7520 inchash::add_expr (VECTOR_CST_ELT (t, i), hstate);
7521 return;
7523 case SSA_NAME:
7524 /* We can just compare by pointer. */
7525 hstate.add_wide_int (SSA_NAME_VERSION (t));
7526 return;
7527 case PLACEHOLDER_EXPR:
7528 /* The node itself doesn't matter. */
7529 return;
7530 case TREE_LIST:
7531 /* A list of expressions, for a CALL_EXPR or as the elements of a
7532 VECTOR_CST. */
7533 for (; t; t = TREE_CHAIN (t))
7534 inchash::add_expr (TREE_VALUE (t), hstate);
7535 return;
7536 case CONSTRUCTOR:
7538 unsigned HOST_WIDE_INT idx;
7539 tree field, value;
7540 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7542 inchash::add_expr (field, hstate);
7543 inchash::add_expr (value, hstate);
7545 return;
7547 case FUNCTION_DECL:
7548 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7549 Otherwise nodes that compare equal according to operand_equal_p might
7550 get different hash codes. However, don't do this for machine specific
7551 or front end builtins, since the function code is overloaded in those
7552 cases. */
7553 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7554 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7556 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7557 code = TREE_CODE (t);
7559 /* FALL THROUGH */
7560 default:
7561 tclass = TREE_CODE_CLASS (code);
7563 if (tclass == tcc_declaration)
7565 /* DECL's have a unique ID */
7566 hstate.add_wide_int (DECL_UID (t));
7568 else
7570 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7572 hstate.add_object (code);
7574 /* Don't hash the type, that can lead to having nodes which
7575 compare equal according to operand_equal_p, but which
7576 have different hash codes. */
7577 if (CONVERT_EXPR_CODE_P (code)
7578 || code == NON_LVALUE_EXPR)
7580 /* Make sure to include signness in the hash computation. */
7581 hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
7582 inchash::add_expr (TREE_OPERAND (t, 0), hstate);
7585 else if (commutative_tree_code (code))
7587 /* It's a commutative expression. We want to hash it the same
7588 however it appears. We do this by first hashing both operands
7589 and then rehashing based on the order of their independent
7590 hashes. */
7591 inchash::hash one, two;
7592 inchash::add_expr (TREE_OPERAND (t, 0), one);
7593 inchash::add_expr (TREE_OPERAND (t, 1), two);
7594 hstate.add_commutative (one, two);
7596 else
7597 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7598 inchash::add_expr (TREE_OPERAND (t, i), hstate);
7600 return;
7606 /* Constructors for pointer, array and function types.
7607 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7608 constructed by language-dependent code, not here.) */
7610 /* Construct, lay out and return the type of pointers to TO_TYPE with
7611 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7612 reference all of memory. If such a type has already been
7613 constructed, reuse it. */
7615 tree
7616 build_pointer_type_for_mode (tree to_type, machine_mode mode,
7617 bool can_alias_all)
7619 tree t;
7621 if (to_type == error_mark_node)
7622 return error_mark_node;
7624 /* If the pointed-to type has the may_alias attribute set, force
7625 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7626 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7627 can_alias_all = true;
7629 /* In some cases, languages will have things that aren't a POINTER_TYPE
7630 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7631 In that case, return that type without regard to the rest of our
7632 operands.
7634 ??? This is a kludge, but consistent with the way this function has
7635 always operated and there doesn't seem to be a good way to avoid this
7636 at the moment. */
7637 if (TYPE_POINTER_TO (to_type) != 0
7638 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7639 return TYPE_POINTER_TO (to_type);
7641 /* First, if we already have a type for pointers to TO_TYPE and it's
7642 the proper mode, use it. */
7643 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7644 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7645 return t;
7647 t = make_node (POINTER_TYPE);
7649 TREE_TYPE (t) = to_type;
7650 SET_TYPE_MODE (t, mode);
7651 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7652 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7653 TYPE_POINTER_TO (to_type) = t;
7655 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7656 SET_TYPE_STRUCTURAL_EQUALITY (t);
7657 else if (TYPE_CANONICAL (to_type) != to_type)
7658 TYPE_CANONICAL (t)
7659 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7660 mode, can_alias_all);
7662 /* Lay out the type. This function has many callers that are concerned
7663 with expression-construction, and this simplifies them all. */
7664 layout_type (t);
7666 return t;
7669 /* By default build pointers in ptr_mode. */
7671 tree
7672 build_pointer_type (tree to_type)
7674 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7675 : TYPE_ADDR_SPACE (to_type);
7676 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7677 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7680 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7682 tree
7683 build_reference_type_for_mode (tree to_type, machine_mode mode,
7684 bool can_alias_all)
7686 tree t;
7688 if (to_type == error_mark_node)
7689 return error_mark_node;
7691 /* If the pointed-to type has the may_alias attribute set, force
7692 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7693 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7694 can_alias_all = true;
7696 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7697 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7698 In that case, return that type without regard to the rest of our
7699 operands.
7701 ??? This is a kludge, but consistent with the way this function has
7702 always operated and there doesn't seem to be a good way to avoid this
7703 at the moment. */
7704 if (TYPE_REFERENCE_TO (to_type) != 0
7705 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7706 return TYPE_REFERENCE_TO (to_type);
7708 /* First, if we already have a type for pointers to TO_TYPE and it's
7709 the proper mode, use it. */
7710 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7711 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7712 return t;
7714 t = make_node (REFERENCE_TYPE);
7716 TREE_TYPE (t) = to_type;
7717 SET_TYPE_MODE (t, mode);
7718 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7719 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7720 TYPE_REFERENCE_TO (to_type) = t;
7722 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7723 SET_TYPE_STRUCTURAL_EQUALITY (t);
7724 else if (TYPE_CANONICAL (to_type) != to_type)
7725 TYPE_CANONICAL (t)
7726 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7727 mode, can_alias_all);
7729 layout_type (t);
7731 return t;
7735 /* Build the node for the type of references-to-TO_TYPE by default
7736 in ptr_mode. */
7738 tree
7739 build_reference_type (tree to_type)
7741 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7742 : TYPE_ADDR_SPACE (to_type);
7743 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7744 return build_reference_type_for_mode (to_type, pointer_mode, false);
7747 #define MAX_INT_CACHED_PREC \
7748 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7749 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7751 /* Builds a signed or unsigned integer type of precision PRECISION.
7752 Used for C bitfields whose precision does not match that of
7753 built-in target types. */
7754 tree
7755 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7756 int unsignedp)
7758 tree itype, ret;
7760 if (unsignedp)
7761 unsignedp = MAX_INT_CACHED_PREC + 1;
7763 if (precision <= MAX_INT_CACHED_PREC)
7765 itype = nonstandard_integer_type_cache[precision + unsignedp];
7766 if (itype)
7767 return itype;
7770 itype = make_node (INTEGER_TYPE);
7771 TYPE_PRECISION (itype) = precision;
7773 if (unsignedp)
7774 fixup_unsigned_type (itype);
7775 else
7776 fixup_signed_type (itype);
7778 ret = itype;
7779 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (itype)))
7780 ret = type_hash_canon (tree_to_uhwi (TYPE_MAX_VALUE (itype)), itype);
7781 if (precision <= MAX_INT_CACHED_PREC)
7782 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7784 return ret;
7787 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7788 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7789 is true, reuse such a type that has already been constructed. */
7791 static tree
7792 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7794 tree itype = make_node (INTEGER_TYPE);
7795 inchash::hash hstate;
7797 TREE_TYPE (itype) = type;
7799 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7800 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7802 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7803 SET_TYPE_MODE (itype, TYPE_MODE (type));
7804 TYPE_SIZE (itype) = TYPE_SIZE (type);
7805 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7806 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7807 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7809 if (!shared)
7810 return itype;
7812 if ((TYPE_MIN_VALUE (itype)
7813 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7814 || (TYPE_MAX_VALUE (itype)
7815 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7817 /* Since we cannot reliably merge this type, we need to compare it using
7818 structural equality checks. */
7819 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7820 return itype;
7823 inchash::add_expr (TYPE_MIN_VALUE (itype), hstate);
7824 inchash::add_expr (TYPE_MAX_VALUE (itype), hstate);
7825 hstate.merge_hash (TYPE_HASH (type));
7826 itype = type_hash_canon (hstate.end (), itype);
7828 return itype;
7831 /* Wrapper around build_range_type_1 with SHARED set to true. */
7833 tree
7834 build_range_type (tree type, tree lowval, tree highval)
7836 return build_range_type_1 (type, lowval, highval, true);
7839 /* Wrapper around build_range_type_1 with SHARED set to false. */
7841 tree
7842 build_nonshared_range_type (tree type, tree lowval, tree highval)
7844 return build_range_type_1 (type, lowval, highval, false);
7847 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7848 MAXVAL should be the maximum value in the domain
7849 (one less than the length of the array).
7851 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7852 We don't enforce this limit, that is up to caller (e.g. language front end).
7853 The limit exists because the result is a signed type and we don't handle
7854 sizes that use more than one HOST_WIDE_INT. */
7856 tree
7857 build_index_type (tree maxval)
7859 return build_range_type (sizetype, size_zero_node, maxval);
7862 /* Return true if the debug information for TYPE, a subtype, should be emitted
7863 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7864 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7865 debug info and doesn't reflect the source code. */
7867 bool
7868 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7870 tree base_type = TREE_TYPE (type), low, high;
7872 /* Subrange types have a base type which is an integral type. */
7873 if (!INTEGRAL_TYPE_P (base_type))
7874 return false;
7876 /* Get the real bounds of the subtype. */
7877 if (lang_hooks.types.get_subrange_bounds)
7878 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7879 else
7881 low = TYPE_MIN_VALUE (type);
7882 high = TYPE_MAX_VALUE (type);
7885 /* If the type and its base type have the same representation and the same
7886 name, then the type is not a subrange but a copy of the base type. */
7887 if ((TREE_CODE (base_type) == INTEGER_TYPE
7888 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7889 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7890 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7891 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type))
7892 && TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (base_type))
7893 return false;
7895 if (lowval)
7896 *lowval = low;
7897 if (highval)
7898 *highval = high;
7899 return true;
7902 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7903 and number of elements specified by the range of values of INDEX_TYPE.
7904 If SHARED is true, reuse such a type that has already been constructed. */
7906 static tree
7907 build_array_type_1 (tree elt_type, tree index_type, bool shared)
7909 tree t;
7911 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7913 error ("arrays of functions are not meaningful");
7914 elt_type = integer_type_node;
7917 t = make_node (ARRAY_TYPE);
7918 TREE_TYPE (t) = elt_type;
7919 TYPE_DOMAIN (t) = index_type;
7920 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7921 layout_type (t);
7923 /* If the element type is incomplete at this point we get marked for
7924 structural equality. Do not record these types in the canonical
7925 type hashtable. */
7926 if (TYPE_STRUCTURAL_EQUALITY_P (t))
7927 return t;
7929 if (shared)
7931 inchash::hash hstate;
7932 hstate.add_object (TYPE_HASH (elt_type));
7933 if (index_type)
7934 hstate.add_object (TYPE_HASH (index_type));
7935 t = type_hash_canon (hstate.end (), t);
7938 if (TYPE_CANONICAL (t) == t)
7940 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7941 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7942 SET_TYPE_STRUCTURAL_EQUALITY (t);
7943 else if (TYPE_CANONICAL (elt_type) != elt_type
7944 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7945 TYPE_CANONICAL (t)
7946 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7947 index_type
7948 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7949 shared);
7952 return t;
7955 /* Wrapper around build_array_type_1 with SHARED set to true. */
7957 tree
7958 build_array_type (tree elt_type, tree index_type)
7960 return build_array_type_1 (elt_type, index_type, true);
7963 /* Wrapper around build_array_type_1 with SHARED set to false. */
7965 tree
7966 build_nonshared_array_type (tree elt_type, tree index_type)
7968 return build_array_type_1 (elt_type, index_type, false);
7971 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7972 sizetype. */
7974 tree
7975 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
7977 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7980 /* Recursively examines the array elements of TYPE, until a non-array
7981 element type is found. */
7983 tree
7984 strip_array_types (tree type)
7986 while (TREE_CODE (type) == ARRAY_TYPE)
7987 type = TREE_TYPE (type);
7989 return type;
7992 /* Computes the canonical argument types from the argument type list
7993 ARGTYPES.
7995 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7996 on entry to this function, or if any of the ARGTYPES are
7997 structural.
7999 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
8000 true on entry to this function, or if any of the ARGTYPES are
8001 non-canonical.
8003 Returns a canonical argument list, which may be ARGTYPES when the
8004 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
8005 true) or would not differ from ARGTYPES. */
8007 static tree
8008 maybe_canonicalize_argtypes (tree argtypes,
8009 bool *any_structural_p,
8010 bool *any_noncanonical_p)
8012 tree arg;
8013 bool any_noncanonical_argtypes_p = false;
8015 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
8017 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
8018 /* Fail gracefully by stating that the type is structural. */
8019 *any_structural_p = true;
8020 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
8021 *any_structural_p = true;
8022 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
8023 || TREE_PURPOSE (arg))
8024 /* If the argument has a default argument, we consider it
8025 non-canonical even though the type itself is canonical.
8026 That way, different variants of function and method types
8027 with default arguments will all point to the variant with
8028 no defaults as their canonical type. */
8029 any_noncanonical_argtypes_p = true;
8032 if (*any_structural_p)
8033 return argtypes;
8035 if (any_noncanonical_argtypes_p)
8037 /* Build the canonical list of argument types. */
8038 tree canon_argtypes = NULL_TREE;
8039 bool is_void = false;
8041 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
8043 if (arg == void_list_node)
8044 is_void = true;
8045 else
8046 canon_argtypes = tree_cons (NULL_TREE,
8047 TYPE_CANONICAL (TREE_VALUE (arg)),
8048 canon_argtypes);
8051 canon_argtypes = nreverse (canon_argtypes);
8052 if (is_void)
8053 canon_argtypes = chainon (canon_argtypes, void_list_node);
8055 /* There is a non-canonical type. */
8056 *any_noncanonical_p = true;
8057 return canon_argtypes;
8060 /* The canonical argument types are the same as ARGTYPES. */
8061 return argtypes;
8064 /* Construct, lay out and return
8065 the type of functions returning type VALUE_TYPE
8066 given arguments of types ARG_TYPES.
8067 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
8068 are data type nodes for the arguments of the function.
8069 If such a type has already been constructed, reuse it. */
8071 tree
8072 build_function_type (tree value_type, tree arg_types)
8074 tree t;
8075 inchash::hash hstate;
8076 bool any_structural_p, any_noncanonical_p;
8077 tree canon_argtypes;
8079 if (TREE_CODE (value_type) == FUNCTION_TYPE)
8081 error ("function return type cannot be function");
8082 value_type = integer_type_node;
8085 /* Make a node of the sort we want. */
8086 t = make_node (FUNCTION_TYPE);
8087 TREE_TYPE (t) = value_type;
8088 TYPE_ARG_TYPES (t) = arg_types;
8090 /* If we already have such a type, use the old one. */
8091 hstate.add_object (TYPE_HASH (value_type));
8092 type_hash_list (arg_types, hstate);
8093 t = type_hash_canon (hstate.end (), t);
8095 /* Set up the canonical type. */
8096 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
8097 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
8098 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
8099 &any_structural_p,
8100 &any_noncanonical_p);
8101 if (any_structural_p)
8102 SET_TYPE_STRUCTURAL_EQUALITY (t);
8103 else if (any_noncanonical_p)
8104 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
8105 canon_argtypes);
8107 if (!COMPLETE_TYPE_P (t))
8108 layout_type (t);
8109 return t;
8112 /* Build a function type. The RETURN_TYPE is the type returned by the
8113 function. If VAARGS is set, no void_type_node is appended to the
8114 the list. ARGP must be always be terminated be a NULL_TREE. */
8116 static tree
8117 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
8119 tree t, args, last;
8121 t = va_arg (argp, tree);
8122 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
8123 args = tree_cons (NULL_TREE, t, args);
8125 if (vaargs)
8127 last = args;
8128 if (args != NULL_TREE)
8129 args = nreverse (args);
8130 gcc_assert (last != void_list_node);
8132 else if (args == NULL_TREE)
8133 args = void_list_node;
8134 else
8136 last = args;
8137 args = nreverse (args);
8138 TREE_CHAIN (last) = void_list_node;
8140 args = build_function_type (return_type, args);
8142 return args;
8145 /* Build a function type. The RETURN_TYPE is the type returned by the
8146 function. If additional arguments are provided, they are
8147 additional argument types. The list of argument types must always
8148 be terminated by NULL_TREE. */
8150 tree
8151 build_function_type_list (tree return_type, ...)
8153 tree args;
8154 va_list p;
8156 va_start (p, return_type);
8157 args = build_function_type_list_1 (false, return_type, p);
8158 va_end (p);
8159 return args;
8162 /* Build a variable argument function type. The RETURN_TYPE is the
8163 type returned by the function. If additional arguments are provided,
8164 they are additional argument types. The list of argument types must
8165 always be terminated by NULL_TREE. */
8167 tree
8168 build_varargs_function_type_list (tree return_type, ...)
8170 tree args;
8171 va_list p;
8173 va_start (p, return_type);
8174 args = build_function_type_list_1 (true, return_type, p);
8175 va_end (p);
8177 return args;
8180 /* Build a function type. RETURN_TYPE is the type returned by the
8181 function; VAARGS indicates whether the function takes varargs. The
8182 function takes N named arguments, the types of which are provided in
8183 ARG_TYPES. */
8185 static tree
8186 build_function_type_array_1 (bool vaargs, tree return_type, int n,
8187 tree *arg_types)
8189 int i;
8190 tree t = vaargs ? NULL_TREE : void_list_node;
8192 for (i = n - 1; i >= 0; i--)
8193 t = tree_cons (NULL_TREE, arg_types[i], t);
8195 return build_function_type (return_type, t);
8198 /* Build a function type. RETURN_TYPE is the type returned by the
8199 function. The function takes N named arguments, the types of which
8200 are provided in ARG_TYPES. */
8202 tree
8203 build_function_type_array (tree return_type, int n, tree *arg_types)
8205 return build_function_type_array_1 (false, return_type, n, arg_types);
8208 /* Build a variable argument function type. RETURN_TYPE is the type
8209 returned by the function. The function takes N named arguments, the
8210 types of which are provided in ARG_TYPES. */
8212 tree
8213 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8215 return build_function_type_array_1 (true, return_type, n, arg_types);
8218 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8219 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8220 for the method. An implicit additional parameter (of type
8221 pointer-to-BASETYPE) is added to the ARGTYPES. */
8223 tree
8224 build_method_type_directly (tree basetype,
8225 tree rettype,
8226 tree argtypes)
8228 tree t;
8229 tree ptype;
8230 inchash::hash hstate;
8231 bool any_structural_p, any_noncanonical_p;
8232 tree canon_argtypes;
8234 /* Make a node of the sort we want. */
8235 t = make_node (METHOD_TYPE);
8237 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8238 TREE_TYPE (t) = rettype;
8239 ptype = build_pointer_type (basetype);
8241 /* The actual arglist for this function includes a "hidden" argument
8242 which is "this". Put it into the list of argument types. */
8243 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8244 TYPE_ARG_TYPES (t) = argtypes;
8246 /* If we already have such a type, use the old one. */
8247 hstate.add_object (TYPE_HASH (basetype));
8248 hstate.add_object (TYPE_HASH (rettype));
8249 type_hash_list (argtypes, hstate);
8250 t = type_hash_canon (hstate.end (), t);
8252 /* Set up the canonical type. */
8253 any_structural_p
8254 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8255 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8256 any_noncanonical_p
8257 = (TYPE_CANONICAL (basetype) != basetype
8258 || TYPE_CANONICAL (rettype) != rettype);
8259 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8260 &any_structural_p,
8261 &any_noncanonical_p);
8262 if (any_structural_p)
8263 SET_TYPE_STRUCTURAL_EQUALITY (t);
8264 else if (any_noncanonical_p)
8265 TYPE_CANONICAL (t)
8266 = build_method_type_directly (TYPE_CANONICAL (basetype),
8267 TYPE_CANONICAL (rettype),
8268 canon_argtypes);
8269 if (!COMPLETE_TYPE_P (t))
8270 layout_type (t);
8272 return t;
8275 /* Construct, lay out and return the type of methods belonging to class
8276 BASETYPE and whose arguments and values are described by TYPE.
8277 If that type exists already, reuse it.
8278 TYPE must be a FUNCTION_TYPE node. */
8280 tree
8281 build_method_type (tree basetype, tree type)
8283 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8285 return build_method_type_directly (basetype,
8286 TREE_TYPE (type),
8287 TYPE_ARG_TYPES (type));
8290 /* Construct, lay out and return the type of offsets to a value
8291 of type TYPE, within an object of type BASETYPE.
8292 If a suitable offset type exists already, reuse it. */
8294 tree
8295 build_offset_type (tree basetype, tree type)
8297 tree t;
8298 inchash::hash hstate;
8300 /* Make a node of the sort we want. */
8301 t = make_node (OFFSET_TYPE);
8303 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8304 TREE_TYPE (t) = type;
8306 /* If we already have such a type, use the old one. */
8307 hstate.add_object (TYPE_HASH (basetype));
8308 hstate.add_object (TYPE_HASH (type));
8309 t = type_hash_canon (hstate.end (), t);
8311 if (!COMPLETE_TYPE_P (t))
8312 layout_type (t);
8314 if (TYPE_CANONICAL (t) == t)
8316 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8317 || TYPE_STRUCTURAL_EQUALITY_P (type))
8318 SET_TYPE_STRUCTURAL_EQUALITY (t);
8319 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8320 || TYPE_CANONICAL (type) != type)
8321 TYPE_CANONICAL (t)
8322 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8323 TYPE_CANONICAL (type));
8326 return t;
8329 /* Create a complex type whose components are COMPONENT_TYPE. */
8331 tree
8332 build_complex_type (tree component_type)
8334 tree t;
8335 inchash::hash hstate;
8337 gcc_assert (INTEGRAL_TYPE_P (component_type)
8338 || SCALAR_FLOAT_TYPE_P (component_type)
8339 || FIXED_POINT_TYPE_P (component_type));
8341 /* Make a node of the sort we want. */
8342 t = make_node (COMPLEX_TYPE);
8344 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8346 /* If we already have such a type, use the old one. */
8347 hstate.add_object (TYPE_HASH (component_type));
8348 t = type_hash_canon (hstate.end (), t);
8350 if (!COMPLETE_TYPE_P (t))
8351 layout_type (t);
8353 if (TYPE_CANONICAL (t) == t)
8355 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8356 SET_TYPE_STRUCTURAL_EQUALITY (t);
8357 else if (TYPE_CANONICAL (component_type) != component_type)
8358 TYPE_CANONICAL (t)
8359 = build_complex_type (TYPE_CANONICAL (component_type));
8362 /* We need to create a name, since complex is a fundamental type. */
8363 if (! TYPE_NAME (t))
8365 const char *name;
8366 if (component_type == char_type_node)
8367 name = "complex char";
8368 else if (component_type == signed_char_type_node)
8369 name = "complex signed char";
8370 else if (component_type == unsigned_char_type_node)
8371 name = "complex unsigned char";
8372 else if (component_type == short_integer_type_node)
8373 name = "complex short int";
8374 else if (component_type == short_unsigned_type_node)
8375 name = "complex short unsigned int";
8376 else if (component_type == integer_type_node)
8377 name = "complex int";
8378 else if (component_type == unsigned_type_node)
8379 name = "complex unsigned int";
8380 else if (component_type == long_integer_type_node)
8381 name = "complex long int";
8382 else if (component_type == long_unsigned_type_node)
8383 name = "complex long unsigned int";
8384 else if (component_type == long_long_integer_type_node)
8385 name = "complex long long int";
8386 else if (component_type == long_long_unsigned_type_node)
8387 name = "complex long long unsigned int";
8388 else
8389 name = 0;
8391 if (name != 0)
8392 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8393 get_identifier (name), t);
8396 return build_qualified_type (t, TYPE_QUALS (component_type));
8399 /* If TYPE is a real or complex floating-point type and the target
8400 does not directly support arithmetic on TYPE then return the wider
8401 type to be used for arithmetic on TYPE. Otherwise, return
8402 NULL_TREE. */
8404 tree
8405 excess_precision_type (tree type)
8407 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8409 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8410 switch (TREE_CODE (type))
8412 case REAL_TYPE:
8413 switch (flt_eval_method)
8415 case 1:
8416 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8417 return double_type_node;
8418 break;
8419 case 2:
8420 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8421 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8422 return long_double_type_node;
8423 break;
8424 default:
8425 gcc_unreachable ();
8427 break;
8428 case COMPLEX_TYPE:
8429 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8430 return NULL_TREE;
8431 switch (flt_eval_method)
8433 case 1:
8434 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8435 return complex_double_type_node;
8436 break;
8437 case 2:
8438 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8439 || (TYPE_MODE (TREE_TYPE (type))
8440 == TYPE_MODE (double_type_node)))
8441 return complex_long_double_type_node;
8442 break;
8443 default:
8444 gcc_unreachable ();
8446 break;
8447 default:
8448 break;
8451 return NULL_TREE;
8454 /* Return OP, stripped of any conversions to wider types as much as is safe.
8455 Converting the value back to OP's type makes a value equivalent to OP.
8457 If FOR_TYPE is nonzero, we return a value which, if converted to
8458 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8460 OP must have integer, real or enumeral type. Pointers are not allowed!
8462 There are some cases where the obvious value we could return
8463 would regenerate to OP if converted to OP's type,
8464 but would not extend like OP to wider types.
8465 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8466 For example, if OP is (unsigned short)(signed char)-1,
8467 we avoid returning (signed char)-1 if FOR_TYPE is int,
8468 even though extending that to an unsigned short would regenerate OP,
8469 since the result of extending (signed char)-1 to (int)
8470 is different from (int) OP. */
8472 tree
8473 get_unwidened (tree op, tree for_type)
8475 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8476 tree type = TREE_TYPE (op);
8477 unsigned final_prec
8478 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8479 int uns
8480 = (for_type != 0 && for_type != type
8481 && final_prec > TYPE_PRECISION (type)
8482 && TYPE_UNSIGNED (type));
8483 tree win = op;
8485 while (CONVERT_EXPR_P (op))
8487 int bitschange;
8489 /* TYPE_PRECISION on vector types has different meaning
8490 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8491 so avoid them here. */
8492 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8493 break;
8495 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8496 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8498 /* Truncations are many-one so cannot be removed.
8499 Unless we are later going to truncate down even farther. */
8500 if (bitschange < 0
8501 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8502 break;
8504 /* See what's inside this conversion. If we decide to strip it,
8505 we will set WIN. */
8506 op = TREE_OPERAND (op, 0);
8508 /* If we have not stripped any zero-extensions (uns is 0),
8509 we can strip any kind of extension.
8510 If we have previously stripped a zero-extension,
8511 only zero-extensions can safely be stripped.
8512 Any extension can be stripped if the bits it would produce
8513 are all going to be discarded later by truncating to FOR_TYPE. */
8515 if (bitschange > 0)
8517 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8518 win = op;
8519 /* TYPE_UNSIGNED says whether this is a zero-extension.
8520 Let's avoid computing it if it does not affect WIN
8521 and if UNS will not be needed again. */
8522 if ((uns
8523 || CONVERT_EXPR_P (op))
8524 && TYPE_UNSIGNED (TREE_TYPE (op)))
8526 uns = 1;
8527 win = op;
8532 /* If we finally reach a constant see if it fits in for_type and
8533 in that case convert it. */
8534 if (for_type
8535 && TREE_CODE (win) == INTEGER_CST
8536 && TREE_TYPE (win) != for_type
8537 && int_fits_type_p (win, for_type))
8538 win = fold_convert (for_type, win);
8540 return win;
8543 /* Return OP or a simpler expression for a narrower value
8544 which can be sign-extended or zero-extended to give back OP.
8545 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8546 or 0 if the value should be sign-extended. */
8548 tree
8549 get_narrower (tree op, int *unsignedp_ptr)
8551 int uns = 0;
8552 int first = 1;
8553 tree win = op;
8554 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8556 while (TREE_CODE (op) == NOP_EXPR)
8558 int bitschange
8559 = (TYPE_PRECISION (TREE_TYPE (op))
8560 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8562 /* Truncations are many-one so cannot be removed. */
8563 if (bitschange < 0)
8564 break;
8566 /* See what's inside this conversion. If we decide to strip it,
8567 we will set WIN. */
8569 if (bitschange > 0)
8571 op = TREE_OPERAND (op, 0);
8572 /* An extension: the outermost one can be stripped,
8573 but remember whether it is zero or sign extension. */
8574 if (first)
8575 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8576 /* Otherwise, if a sign extension has been stripped,
8577 only sign extensions can now be stripped;
8578 if a zero extension has been stripped, only zero-extensions. */
8579 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8580 break;
8581 first = 0;
8583 else /* bitschange == 0 */
8585 /* A change in nominal type can always be stripped, but we must
8586 preserve the unsignedness. */
8587 if (first)
8588 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8589 first = 0;
8590 op = TREE_OPERAND (op, 0);
8591 /* Keep trying to narrow, but don't assign op to win if it
8592 would turn an integral type into something else. */
8593 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8594 continue;
8597 win = op;
8600 if (TREE_CODE (op) == COMPONENT_REF
8601 /* Since type_for_size always gives an integer type. */
8602 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8603 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8604 /* Ensure field is laid out already. */
8605 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8606 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
8608 unsigned HOST_WIDE_INT innerprec
8609 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
8610 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8611 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8612 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8614 /* We can get this structure field in a narrower type that fits it,
8615 but the resulting extension to its nominal type (a fullword type)
8616 must satisfy the same conditions as for other extensions.
8618 Do this only for fields that are aligned (not bit-fields),
8619 because when bit-field insns will be used there is no
8620 advantage in doing this. */
8622 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8623 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8624 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8625 && type != 0)
8627 if (first)
8628 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8629 win = fold_convert (type, op);
8633 *unsignedp_ptr = uns;
8634 return win;
8637 /* Returns true if integer constant C has a value that is permissible
8638 for type TYPE (an INTEGER_TYPE). */
8640 bool
8641 int_fits_type_p (const_tree c, const_tree type)
8643 tree type_low_bound, type_high_bound;
8644 bool ok_for_low_bound, ok_for_high_bound;
8645 signop sgn_c = TYPE_SIGN (TREE_TYPE (c));
8647 retry:
8648 type_low_bound = TYPE_MIN_VALUE (type);
8649 type_high_bound = TYPE_MAX_VALUE (type);
8651 /* If at least one bound of the type is a constant integer, we can check
8652 ourselves and maybe make a decision. If no such decision is possible, but
8653 this type is a subtype, try checking against that. Otherwise, use
8654 fits_to_tree_p, which checks against the precision.
8656 Compute the status for each possibly constant bound, and return if we see
8657 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8658 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8659 for "constant known to fit". */
8661 /* Check if c >= type_low_bound. */
8662 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8664 if (tree_int_cst_lt (c, type_low_bound))
8665 return false;
8666 ok_for_low_bound = true;
8668 else
8669 ok_for_low_bound = false;
8671 /* Check if c <= type_high_bound. */
8672 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8674 if (tree_int_cst_lt (type_high_bound, c))
8675 return false;
8676 ok_for_high_bound = true;
8678 else
8679 ok_for_high_bound = false;
8681 /* If the constant fits both bounds, the result is known. */
8682 if (ok_for_low_bound && ok_for_high_bound)
8683 return true;
8685 /* Perform some generic filtering which may allow making a decision
8686 even if the bounds are not constant. First, negative integers
8687 never fit in unsigned types, */
8688 if (TYPE_UNSIGNED (type) && sgn_c == SIGNED && wi::neg_p (c))
8689 return false;
8691 /* Second, narrower types always fit in wider ones. */
8692 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8693 return true;
8695 /* Third, unsigned integers with top bit set never fit signed types. */
8696 if (!TYPE_UNSIGNED (type) && sgn_c == UNSIGNED)
8698 int prec = GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (c))) - 1;
8699 if (prec < TYPE_PRECISION (TREE_TYPE (c)))
8701 /* When a tree_cst is converted to a wide-int, the precision
8702 is taken from the type. However, if the precision of the
8703 mode underneath the type is smaller than that, it is
8704 possible that the value will not fit. The test below
8705 fails if any bit is set between the sign bit of the
8706 underlying mode and the top bit of the type. */
8707 if (wi::ne_p (wi::zext (c, prec - 1), c))
8708 return false;
8710 else if (wi::neg_p (c))
8711 return false;
8714 /* If we haven't been able to decide at this point, there nothing more we
8715 can check ourselves here. Look at the base type if we have one and it
8716 has the same precision. */
8717 if (TREE_CODE (type) == INTEGER_TYPE
8718 && TREE_TYPE (type) != 0
8719 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8721 type = TREE_TYPE (type);
8722 goto retry;
8725 /* Or to fits_to_tree_p, if nothing else. */
8726 return wi::fits_to_tree_p (c, type);
8729 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8730 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8731 represented (assuming two's-complement arithmetic) within the bit
8732 precision of the type are returned instead. */
8734 void
8735 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8737 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8738 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8739 wi::to_mpz (TYPE_MIN_VALUE (type), min, TYPE_SIGN (type));
8740 else
8742 if (TYPE_UNSIGNED (type))
8743 mpz_set_ui (min, 0);
8744 else
8746 wide_int mn = wi::min_value (TYPE_PRECISION (type), SIGNED);
8747 wi::to_mpz (mn, min, SIGNED);
8751 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8752 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8753 wi::to_mpz (TYPE_MAX_VALUE (type), max, TYPE_SIGN (type));
8754 else
8756 wide_int mn = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
8757 wi::to_mpz (mn, max, TYPE_SIGN (type));
8761 /* Return true if VAR is an automatic variable defined in function FN. */
8763 bool
8764 auto_var_in_fn_p (const_tree var, const_tree fn)
8766 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8767 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8768 || TREE_CODE (var) == PARM_DECL)
8769 && ! TREE_STATIC (var))
8770 || TREE_CODE (var) == LABEL_DECL
8771 || TREE_CODE (var) == RESULT_DECL));
8774 /* Subprogram of following function. Called by walk_tree.
8776 Return *TP if it is an automatic variable or parameter of the
8777 function passed in as DATA. */
8779 static tree
8780 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8782 tree fn = (tree) data;
8784 if (TYPE_P (*tp))
8785 *walk_subtrees = 0;
8787 else if (DECL_P (*tp)
8788 && auto_var_in_fn_p (*tp, fn))
8789 return *tp;
8791 return NULL_TREE;
8794 /* Returns true if T is, contains, or refers to a type with variable
8795 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8796 arguments, but not the return type. If FN is nonzero, only return
8797 true if a modifier of the type or position of FN is a variable or
8798 parameter inside FN.
8800 This concept is more general than that of C99 'variably modified types':
8801 in C99, a struct type is never variably modified because a VLA may not
8802 appear as a structure member. However, in GNU C code like:
8804 struct S { int i[f()]; };
8806 is valid, and other languages may define similar constructs. */
8808 bool
8809 variably_modified_type_p (tree type, tree fn)
8811 tree t;
8813 /* Test if T is either variable (if FN is zero) or an expression containing
8814 a variable in FN. If TYPE isn't gimplified, return true also if
8815 gimplify_one_sizepos would gimplify the expression into a local
8816 variable. */
8817 #define RETURN_TRUE_IF_VAR(T) \
8818 do { tree _t = (T); \
8819 if (_t != NULL_TREE \
8820 && _t != error_mark_node \
8821 && TREE_CODE (_t) != INTEGER_CST \
8822 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8823 && (!fn \
8824 || (!TYPE_SIZES_GIMPLIFIED (type) \
8825 && !is_gimple_sizepos (_t)) \
8826 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8827 return true; } while (0)
8829 if (type == error_mark_node)
8830 return false;
8832 /* If TYPE itself has variable size, it is variably modified. */
8833 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8834 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8836 switch (TREE_CODE (type))
8838 case POINTER_TYPE:
8839 case REFERENCE_TYPE:
8840 case VECTOR_TYPE:
8841 if (variably_modified_type_p (TREE_TYPE (type), fn))
8842 return true;
8843 break;
8845 case FUNCTION_TYPE:
8846 case METHOD_TYPE:
8847 /* If TYPE is a function type, it is variably modified if the
8848 return type is variably modified. */
8849 if (variably_modified_type_p (TREE_TYPE (type), fn))
8850 return true;
8851 break;
8853 case INTEGER_TYPE:
8854 case REAL_TYPE:
8855 case FIXED_POINT_TYPE:
8856 case ENUMERAL_TYPE:
8857 case BOOLEAN_TYPE:
8858 /* Scalar types are variably modified if their end points
8859 aren't constant. */
8860 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8861 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8862 break;
8864 case RECORD_TYPE:
8865 case UNION_TYPE:
8866 case QUAL_UNION_TYPE:
8867 /* We can't see if any of the fields are variably-modified by the
8868 definition we normally use, since that would produce infinite
8869 recursion via pointers. */
8870 /* This is variably modified if some field's type is. */
8871 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8872 if (TREE_CODE (t) == FIELD_DECL)
8874 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8875 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8876 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8878 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8879 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8881 break;
8883 case ARRAY_TYPE:
8884 /* Do not call ourselves to avoid infinite recursion. This is
8885 variably modified if the element type is. */
8886 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8887 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8888 break;
8890 default:
8891 break;
8894 /* The current language may have other cases to check, but in general,
8895 all other types are not variably modified. */
8896 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8898 #undef RETURN_TRUE_IF_VAR
8901 /* Given a DECL or TYPE, return the scope in which it was declared, or
8902 NULL_TREE if there is no containing scope. */
8904 tree
8905 get_containing_scope (const_tree t)
8907 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8910 /* Return the innermost context enclosing DECL that is
8911 a FUNCTION_DECL, or zero if none. */
8913 tree
8914 decl_function_context (const_tree decl)
8916 tree context;
8918 if (TREE_CODE (decl) == ERROR_MARK)
8919 return 0;
8921 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8922 where we look up the function at runtime. Such functions always take
8923 a first argument of type 'pointer to real context'.
8925 C++ should really be fixed to use DECL_CONTEXT for the real context,
8926 and use something else for the "virtual context". */
8927 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8928 context
8929 = TYPE_MAIN_VARIANT
8930 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8931 else
8932 context = DECL_CONTEXT (decl);
8934 while (context && TREE_CODE (context) != FUNCTION_DECL)
8936 if (TREE_CODE (context) == BLOCK)
8937 context = BLOCK_SUPERCONTEXT (context);
8938 else
8939 context = get_containing_scope (context);
8942 return context;
8945 /* Return the innermost context enclosing DECL that is
8946 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8947 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8949 tree
8950 decl_type_context (const_tree decl)
8952 tree context = DECL_CONTEXT (decl);
8954 while (context)
8955 switch (TREE_CODE (context))
8957 case NAMESPACE_DECL:
8958 case TRANSLATION_UNIT_DECL:
8959 return NULL_TREE;
8961 case RECORD_TYPE:
8962 case UNION_TYPE:
8963 case QUAL_UNION_TYPE:
8964 return context;
8966 case TYPE_DECL:
8967 case FUNCTION_DECL:
8968 context = DECL_CONTEXT (context);
8969 break;
8971 case BLOCK:
8972 context = BLOCK_SUPERCONTEXT (context);
8973 break;
8975 default:
8976 gcc_unreachable ();
8979 return NULL_TREE;
8982 /* CALL is a CALL_EXPR. Return the declaration for the function
8983 called, or NULL_TREE if the called function cannot be
8984 determined. */
8986 tree
8987 get_callee_fndecl (const_tree call)
8989 tree addr;
8991 if (call == error_mark_node)
8992 return error_mark_node;
8994 /* It's invalid to call this function with anything but a
8995 CALL_EXPR. */
8996 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8998 /* The first operand to the CALL is the address of the function
8999 called. */
9000 addr = CALL_EXPR_FN (call);
9002 /* If there is no function, return early. */
9003 if (addr == NULL_TREE)
9004 return NULL_TREE;
9006 STRIP_NOPS (addr);
9008 /* If this is a readonly function pointer, extract its initial value. */
9009 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
9010 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
9011 && DECL_INITIAL (addr))
9012 addr = DECL_INITIAL (addr);
9014 /* If the address is just `&f' for some function `f', then we know
9015 that `f' is being called. */
9016 if (TREE_CODE (addr) == ADDR_EXPR
9017 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
9018 return TREE_OPERAND (addr, 0);
9020 /* We couldn't figure out what was being called. */
9021 return NULL_TREE;
9024 /* Print debugging information about tree nodes generated during the compile,
9025 and any language-specific information. */
9027 void
9028 dump_tree_statistics (void)
9030 if (GATHER_STATISTICS)
9032 int i;
9033 int total_nodes, total_bytes;
9034 fprintf (stderr, "Kind Nodes Bytes\n");
9035 fprintf (stderr, "---------------------------------------\n");
9036 total_nodes = total_bytes = 0;
9037 for (i = 0; i < (int) all_kinds; i++)
9039 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
9040 tree_node_counts[i], tree_node_sizes[i]);
9041 total_nodes += tree_node_counts[i];
9042 total_bytes += tree_node_sizes[i];
9044 fprintf (stderr, "---------------------------------------\n");
9045 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
9046 fprintf (stderr, "---------------------------------------\n");
9047 fprintf (stderr, "Code Nodes\n");
9048 fprintf (stderr, "----------------------------\n");
9049 for (i = 0; i < (int) MAX_TREE_CODES; i++)
9050 fprintf (stderr, "%-20s %7d\n", get_tree_code_name ((enum tree_code) i),
9051 tree_code_counts[i]);
9052 fprintf (stderr, "----------------------------\n");
9053 ssanames_print_statistics ();
9054 phinodes_print_statistics ();
9056 else
9057 fprintf (stderr, "(No per-node statistics)\n");
9059 print_type_hash_statistics ();
9060 print_debug_expr_statistics ();
9061 print_value_expr_statistics ();
9062 lang_hooks.print_statistics ();
9065 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
9067 /* Generate a crc32 of a byte. */
9069 static unsigned
9070 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
9072 unsigned ix;
9074 for (ix = bits; ix--; value <<= 1)
9076 unsigned feedback;
9078 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
9079 chksum <<= 1;
9080 chksum ^= feedback;
9082 return chksum;
9085 /* Generate a crc32 of a 32-bit unsigned. */
9087 unsigned
9088 crc32_unsigned (unsigned chksum, unsigned value)
9090 return crc32_unsigned_bits (chksum, value, 32);
9093 /* Generate a crc32 of a byte. */
9095 unsigned
9096 crc32_byte (unsigned chksum, char byte)
9098 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
9101 /* Generate a crc32 of a string. */
9103 unsigned
9104 crc32_string (unsigned chksum, const char *string)
9108 chksum = crc32_byte (chksum, *string);
9110 while (*string++);
9111 return chksum;
9114 /* P is a string that will be used in a symbol. Mask out any characters
9115 that are not valid in that context. */
9117 void
9118 clean_symbol_name (char *p)
9120 for (; *p; p++)
9121 if (! (ISALNUM (*p)
9122 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
9123 || *p == '$'
9124 #endif
9125 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
9126 || *p == '.'
9127 #endif
9129 *p = '_';
9132 /* Generate a name for a special-purpose function.
9133 The generated name may need to be unique across the whole link.
9134 Changes to this function may also require corresponding changes to
9135 xstrdup_mask_random.
9136 TYPE is some string to identify the purpose of this function to the
9137 linker or collect2; it must start with an uppercase letter,
9138 one of:
9139 I - for constructors
9140 D - for destructors
9141 N - for C++ anonymous namespaces
9142 F - for DWARF unwind frame information. */
9144 tree
9145 get_file_function_name (const char *type)
9147 char *buf;
9148 const char *p;
9149 char *q;
9151 /* If we already have a name we know to be unique, just use that. */
9152 if (first_global_object_name)
9153 p = q = ASTRDUP (first_global_object_name);
9154 /* If the target is handling the constructors/destructors, they
9155 will be local to this file and the name is only necessary for
9156 debugging purposes.
9157 We also assign sub_I and sub_D sufixes to constructors called from
9158 the global static constructors. These are always local. */
9159 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9160 || (strncmp (type, "sub_", 4) == 0
9161 && (type[4] == 'I' || type[4] == 'D')))
9163 const char *file = main_input_filename;
9164 if (! file)
9165 file = LOCATION_FILE (input_location);
9166 /* Just use the file's basename, because the full pathname
9167 might be quite long. */
9168 p = q = ASTRDUP (lbasename (file));
9170 else
9172 /* Otherwise, the name must be unique across the entire link.
9173 We don't have anything that we know to be unique to this translation
9174 unit, so use what we do have and throw in some randomness. */
9175 unsigned len;
9176 const char *name = weak_global_object_name;
9177 const char *file = main_input_filename;
9179 if (! name)
9180 name = "";
9181 if (! file)
9182 file = LOCATION_FILE (input_location);
9184 len = strlen (file);
9185 q = (char *) alloca (9 + 17 + len + 1);
9186 memcpy (q, file, len + 1);
9188 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9189 crc32_string (0, name), get_random_seed (false));
9191 p = q;
9194 clean_symbol_name (q);
9195 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9196 + strlen (type));
9198 /* Set up the name of the file-level functions we may need.
9199 Use a global object (which is already required to be unique over
9200 the program) rather than the file name (which imposes extra
9201 constraints). */
9202 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9204 return get_identifier (buf);
9207 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9209 /* Complain that the tree code of NODE does not match the expected 0
9210 terminated list of trailing codes. The trailing code list can be
9211 empty, for a more vague error message. FILE, LINE, and FUNCTION
9212 are of the caller. */
9214 void
9215 tree_check_failed (const_tree node, const char *file,
9216 int line, const char *function, ...)
9218 va_list args;
9219 const char *buffer;
9220 unsigned length = 0;
9221 enum tree_code code;
9223 va_start (args, function);
9224 while ((code = (enum tree_code) va_arg (args, int)))
9225 length += 4 + strlen (get_tree_code_name (code));
9226 va_end (args);
9227 if (length)
9229 char *tmp;
9230 va_start (args, function);
9231 length += strlen ("expected ");
9232 buffer = tmp = (char *) alloca (length);
9233 length = 0;
9234 while ((code = (enum tree_code) va_arg (args, int)))
9236 const char *prefix = length ? " or " : "expected ";
9238 strcpy (tmp + length, prefix);
9239 length += strlen (prefix);
9240 strcpy (tmp + length, get_tree_code_name (code));
9241 length += strlen (get_tree_code_name (code));
9243 va_end (args);
9245 else
9246 buffer = "unexpected node";
9248 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9249 buffer, get_tree_code_name (TREE_CODE (node)),
9250 function, trim_filename (file), line);
9253 /* Complain that the tree code of NODE does match the expected 0
9254 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9255 the caller. */
9257 void
9258 tree_not_check_failed (const_tree node, const char *file,
9259 int line, const char *function, ...)
9261 va_list args;
9262 char *buffer;
9263 unsigned length = 0;
9264 enum tree_code code;
9266 va_start (args, function);
9267 while ((code = (enum tree_code) va_arg (args, int)))
9268 length += 4 + strlen (get_tree_code_name (code));
9269 va_end (args);
9270 va_start (args, function);
9271 buffer = (char *) alloca (length);
9272 length = 0;
9273 while ((code = (enum tree_code) va_arg (args, int)))
9275 if (length)
9277 strcpy (buffer + length, " or ");
9278 length += 4;
9280 strcpy (buffer + length, get_tree_code_name (code));
9281 length += strlen (get_tree_code_name (code));
9283 va_end (args);
9285 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9286 buffer, get_tree_code_name (TREE_CODE (node)),
9287 function, trim_filename (file), line);
9290 /* Similar to tree_check_failed, except that we check for a class of tree
9291 code, given in CL. */
9293 void
9294 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9295 const char *file, int line, const char *function)
9297 internal_error
9298 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9299 TREE_CODE_CLASS_STRING (cl),
9300 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9301 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9304 /* Similar to tree_check_failed, except that instead of specifying a
9305 dozen codes, use the knowledge that they're all sequential. */
9307 void
9308 tree_range_check_failed (const_tree node, const char *file, int line,
9309 const char *function, enum tree_code c1,
9310 enum tree_code c2)
9312 char *buffer;
9313 unsigned length = 0;
9314 unsigned int c;
9316 for (c = c1; c <= c2; ++c)
9317 length += 4 + strlen (get_tree_code_name ((enum tree_code) c));
9319 length += strlen ("expected ");
9320 buffer = (char *) alloca (length);
9321 length = 0;
9323 for (c = c1; c <= c2; ++c)
9325 const char *prefix = length ? " or " : "expected ";
9327 strcpy (buffer + length, prefix);
9328 length += strlen (prefix);
9329 strcpy (buffer + length, get_tree_code_name ((enum tree_code) c));
9330 length += strlen (get_tree_code_name ((enum tree_code) c));
9333 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9334 buffer, get_tree_code_name (TREE_CODE (node)),
9335 function, trim_filename (file), line);
9339 /* Similar to tree_check_failed, except that we check that a tree does
9340 not have the specified code, given in CL. */
9342 void
9343 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9344 const char *file, int line, const char *function)
9346 internal_error
9347 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9348 TREE_CODE_CLASS_STRING (cl),
9349 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9350 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9354 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9356 void
9357 omp_clause_check_failed (const_tree node, const char *file, int line,
9358 const char *function, enum omp_clause_code code)
9360 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9361 omp_clause_code_name[code], get_tree_code_name (TREE_CODE (node)),
9362 function, trim_filename (file), line);
9366 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9368 void
9369 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9370 const char *function, enum omp_clause_code c1,
9371 enum omp_clause_code c2)
9373 char *buffer;
9374 unsigned length = 0;
9375 unsigned int c;
9377 for (c = c1; c <= c2; ++c)
9378 length += 4 + strlen (omp_clause_code_name[c]);
9380 length += strlen ("expected ");
9381 buffer = (char *) alloca (length);
9382 length = 0;
9384 for (c = c1; c <= c2; ++c)
9386 const char *prefix = length ? " or " : "expected ";
9388 strcpy (buffer + length, prefix);
9389 length += strlen (prefix);
9390 strcpy (buffer + length, omp_clause_code_name[c]);
9391 length += strlen (omp_clause_code_name[c]);
9394 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9395 buffer, omp_clause_code_name[TREE_CODE (node)],
9396 function, trim_filename (file), line);
9400 #undef DEFTREESTRUCT
9401 #define DEFTREESTRUCT(VAL, NAME) NAME,
9403 static const char *ts_enum_names[] = {
9404 #include "treestruct.def"
9406 #undef DEFTREESTRUCT
9408 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9410 /* Similar to tree_class_check_failed, except that we check for
9411 whether CODE contains the tree structure identified by EN. */
9413 void
9414 tree_contains_struct_check_failed (const_tree node,
9415 const enum tree_node_structure_enum en,
9416 const char *file, int line,
9417 const char *function)
9419 internal_error
9420 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9421 TS_ENUM_NAME (en),
9422 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9426 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9427 (dynamically sized) vector. */
9429 void
9430 tree_int_cst_elt_check_failed (int idx, int len, const char *file, int line,
9431 const char *function)
9433 internal_error
9434 ("tree check: accessed elt %d of tree_int_cst with %d elts in %s, at %s:%d",
9435 idx + 1, len, function, trim_filename (file), line);
9438 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9439 (dynamically sized) vector. */
9441 void
9442 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9443 const char *function)
9445 internal_error
9446 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9447 idx + 1, len, function, trim_filename (file), line);
9450 /* Similar to above, except that the check is for the bounds of the operand
9451 vector of an expression node EXP. */
9453 void
9454 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9455 int line, const char *function)
9457 enum tree_code code = TREE_CODE (exp);
9458 internal_error
9459 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9460 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9461 function, trim_filename (file), line);
9464 /* Similar to above, except that the check is for the number of
9465 operands of an OMP_CLAUSE node. */
9467 void
9468 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9469 int line, const char *function)
9471 internal_error
9472 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9473 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9474 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9475 trim_filename (file), line);
9477 #endif /* ENABLE_TREE_CHECKING */
9479 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9480 and mapped to the machine mode MODE. Initialize its fields and build
9481 the information necessary for debugging output. */
9483 static tree
9484 make_vector_type (tree innertype, int nunits, machine_mode mode)
9486 tree t;
9487 inchash::hash hstate;
9489 t = make_node (VECTOR_TYPE);
9490 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9491 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9492 SET_TYPE_MODE (t, mode);
9494 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9495 SET_TYPE_STRUCTURAL_EQUALITY (t);
9496 else if (TYPE_CANONICAL (innertype) != innertype
9497 || mode != VOIDmode)
9498 TYPE_CANONICAL (t)
9499 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9501 layout_type (t);
9503 hstate.add_wide_int (VECTOR_TYPE);
9504 hstate.add_wide_int (nunits);
9505 hstate.add_wide_int (mode);
9506 hstate.add_object (TYPE_HASH (TREE_TYPE (t)));
9507 t = type_hash_canon (hstate.end (), t);
9509 /* We have built a main variant, based on the main variant of the
9510 inner type. Use it to build the variant we return. */
9511 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9512 && TREE_TYPE (t) != innertype)
9513 return build_type_attribute_qual_variant (t,
9514 TYPE_ATTRIBUTES (innertype),
9515 TYPE_QUALS (innertype));
9517 return t;
9520 static tree
9521 make_or_reuse_type (unsigned size, int unsignedp)
9523 int i;
9525 if (size == INT_TYPE_SIZE)
9526 return unsignedp ? unsigned_type_node : integer_type_node;
9527 if (size == CHAR_TYPE_SIZE)
9528 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9529 if (size == SHORT_TYPE_SIZE)
9530 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9531 if (size == LONG_TYPE_SIZE)
9532 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9533 if (size == LONG_LONG_TYPE_SIZE)
9534 return (unsignedp ? long_long_unsigned_type_node
9535 : long_long_integer_type_node);
9537 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9538 if (size == int_n_data[i].bitsize
9539 && int_n_enabled_p[i])
9540 return (unsignedp ? int_n_trees[i].unsigned_type
9541 : int_n_trees[i].signed_type);
9543 if (unsignedp)
9544 return make_unsigned_type (size);
9545 else
9546 return make_signed_type (size);
9549 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9551 static tree
9552 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9554 if (satp)
9556 if (size == SHORT_FRACT_TYPE_SIZE)
9557 return unsignedp ? sat_unsigned_short_fract_type_node
9558 : sat_short_fract_type_node;
9559 if (size == FRACT_TYPE_SIZE)
9560 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9561 if (size == LONG_FRACT_TYPE_SIZE)
9562 return unsignedp ? sat_unsigned_long_fract_type_node
9563 : sat_long_fract_type_node;
9564 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9565 return unsignedp ? sat_unsigned_long_long_fract_type_node
9566 : sat_long_long_fract_type_node;
9568 else
9570 if (size == SHORT_FRACT_TYPE_SIZE)
9571 return unsignedp ? unsigned_short_fract_type_node
9572 : short_fract_type_node;
9573 if (size == FRACT_TYPE_SIZE)
9574 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9575 if (size == LONG_FRACT_TYPE_SIZE)
9576 return unsignedp ? unsigned_long_fract_type_node
9577 : long_fract_type_node;
9578 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9579 return unsignedp ? unsigned_long_long_fract_type_node
9580 : long_long_fract_type_node;
9583 return make_fract_type (size, unsignedp, satp);
9586 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9588 static tree
9589 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9591 if (satp)
9593 if (size == SHORT_ACCUM_TYPE_SIZE)
9594 return unsignedp ? sat_unsigned_short_accum_type_node
9595 : sat_short_accum_type_node;
9596 if (size == ACCUM_TYPE_SIZE)
9597 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9598 if (size == LONG_ACCUM_TYPE_SIZE)
9599 return unsignedp ? sat_unsigned_long_accum_type_node
9600 : sat_long_accum_type_node;
9601 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9602 return unsignedp ? sat_unsigned_long_long_accum_type_node
9603 : sat_long_long_accum_type_node;
9605 else
9607 if (size == SHORT_ACCUM_TYPE_SIZE)
9608 return unsignedp ? unsigned_short_accum_type_node
9609 : short_accum_type_node;
9610 if (size == ACCUM_TYPE_SIZE)
9611 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9612 if (size == LONG_ACCUM_TYPE_SIZE)
9613 return unsignedp ? unsigned_long_accum_type_node
9614 : long_accum_type_node;
9615 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9616 return unsignedp ? unsigned_long_long_accum_type_node
9617 : long_long_accum_type_node;
9620 return make_accum_type (size, unsignedp, satp);
9624 /* Create an atomic variant node for TYPE. This routine is called
9625 during initialization of data types to create the 5 basic atomic
9626 types. The generic build_variant_type function requires these to
9627 already be set up in order to function properly, so cannot be
9628 called from there. If ALIGN is non-zero, then ensure alignment is
9629 overridden to this value. */
9631 static tree
9632 build_atomic_base (tree type, unsigned int align)
9634 tree t;
9636 /* Make sure its not already registered. */
9637 if ((t = get_qualified_type (type, TYPE_QUAL_ATOMIC)))
9638 return t;
9640 t = build_variant_type_copy (type);
9641 set_type_quals (t, TYPE_QUAL_ATOMIC);
9643 if (align)
9644 TYPE_ALIGN (t) = align;
9646 return t;
9649 /* Create nodes for all integer types (and error_mark_node) using the sizes
9650 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9651 SHORT_DOUBLE specifies whether double should be of the same precision
9652 as float. */
9654 void
9655 build_common_tree_nodes (bool signed_char, bool short_double)
9657 int i;
9659 error_mark_node = make_node (ERROR_MARK);
9660 TREE_TYPE (error_mark_node) = error_mark_node;
9662 initialize_sizetypes ();
9664 /* Define both `signed char' and `unsigned char'. */
9665 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9666 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9667 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9668 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9670 /* Define `char', which is like either `signed char' or `unsigned char'
9671 but not the same as either. */
9672 char_type_node
9673 = (signed_char
9674 ? make_signed_type (CHAR_TYPE_SIZE)
9675 : make_unsigned_type (CHAR_TYPE_SIZE));
9676 TYPE_STRING_FLAG (char_type_node) = 1;
9678 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9679 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9680 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9681 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9682 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9683 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9684 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9685 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9687 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9689 int_n_trees[i].signed_type = make_signed_type (int_n_data[i].bitsize);
9690 int_n_trees[i].unsigned_type = make_unsigned_type (int_n_data[i].bitsize);
9691 TYPE_SIZE (int_n_trees[i].signed_type) = bitsize_int (int_n_data[i].bitsize);
9692 TYPE_SIZE (int_n_trees[i].unsigned_type) = bitsize_int (int_n_data[i].bitsize);
9694 if (int_n_data[i].bitsize > LONG_LONG_TYPE_SIZE
9695 && int_n_enabled_p[i])
9697 integer_types[itk_intN_0 + i * 2] = int_n_trees[i].signed_type;
9698 integer_types[itk_unsigned_intN_0 + i * 2] = int_n_trees[i].unsigned_type;
9702 /* Define a boolean type. This type only represents boolean values but
9703 may be larger than char depending on the value of BOOL_TYPE_SIZE. */
9704 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9705 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9706 TYPE_PRECISION (boolean_type_node) = 1;
9707 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9709 /* Define what type to use for size_t. */
9710 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9711 size_type_node = unsigned_type_node;
9712 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9713 size_type_node = long_unsigned_type_node;
9714 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9715 size_type_node = long_long_unsigned_type_node;
9716 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9717 size_type_node = short_unsigned_type_node;
9718 else
9720 int i;
9722 size_type_node = NULL_TREE;
9723 for (i = 0; i < NUM_INT_N_ENTS; i++)
9724 if (int_n_enabled_p[i])
9726 char name[50];
9727 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
9729 if (strcmp (name, SIZE_TYPE) == 0)
9731 size_type_node = int_n_trees[i].unsigned_type;
9734 if (size_type_node == NULL_TREE)
9735 gcc_unreachable ();
9738 /* Fill in the rest of the sized types. Reuse existing type nodes
9739 when possible. */
9740 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9741 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9742 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9743 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9744 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9746 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9747 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9748 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9749 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9750 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9752 /* Don't call build_qualified type for atomics. That routine does
9753 special processing for atomics, and until they are initialized
9754 it's better not to make that call.
9756 Check to see if there is a target override for atomic types. */
9758 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
9759 targetm.atomic_align_for_mode (QImode));
9760 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
9761 targetm.atomic_align_for_mode (HImode));
9762 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
9763 targetm.atomic_align_for_mode (SImode));
9764 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
9765 targetm.atomic_align_for_mode (DImode));
9766 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
9767 targetm.atomic_align_for_mode (TImode));
9769 access_public_node = get_identifier ("public");
9770 access_protected_node = get_identifier ("protected");
9771 access_private_node = get_identifier ("private");
9773 /* Define these next since types below may used them. */
9774 integer_zero_node = build_int_cst (integer_type_node, 0);
9775 integer_one_node = build_int_cst (integer_type_node, 1);
9776 integer_three_node = build_int_cst (integer_type_node, 3);
9777 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9779 size_zero_node = size_int (0);
9780 size_one_node = size_int (1);
9781 bitsize_zero_node = bitsize_int (0);
9782 bitsize_one_node = bitsize_int (1);
9783 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9785 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9786 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9788 void_type_node = make_node (VOID_TYPE);
9789 layout_type (void_type_node);
9791 pointer_bounds_type_node = targetm.chkp_bound_type ();
9793 /* We are not going to have real types in C with less than byte alignment,
9794 so we might as well not have any types that claim to have it. */
9795 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9796 TYPE_USER_ALIGN (void_type_node) = 0;
9798 void_node = make_node (VOID_CST);
9799 TREE_TYPE (void_node) = void_type_node;
9801 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9802 layout_type (TREE_TYPE (null_pointer_node));
9804 ptr_type_node = build_pointer_type (void_type_node);
9805 const_ptr_type_node
9806 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9807 fileptr_type_node = ptr_type_node;
9809 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
9811 float_type_node = make_node (REAL_TYPE);
9812 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9813 layout_type (float_type_node);
9815 double_type_node = make_node (REAL_TYPE);
9816 if (short_double)
9817 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9818 else
9819 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9820 layout_type (double_type_node);
9822 long_double_type_node = make_node (REAL_TYPE);
9823 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9824 layout_type (long_double_type_node);
9826 float_ptr_type_node = build_pointer_type (float_type_node);
9827 double_ptr_type_node = build_pointer_type (double_type_node);
9828 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9829 integer_ptr_type_node = build_pointer_type (integer_type_node);
9831 /* Fixed size integer types. */
9832 uint16_type_node = make_or_reuse_type (16, 1);
9833 uint32_type_node = make_or_reuse_type (32, 1);
9834 uint64_type_node = make_or_reuse_type (64, 1);
9836 /* Decimal float types. */
9837 dfloat32_type_node = make_node (REAL_TYPE);
9838 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9839 layout_type (dfloat32_type_node);
9840 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9841 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9843 dfloat64_type_node = make_node (REAL_TYPE);
9844 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9845 layout_type (dfloat64_type_node);
9846 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9847 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9849 dfloat128_type_node = make_node (REAL_TYPE);
9850 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9851 layout_type (dfloat128_type_node);
9852 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9853 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9855 complex_integer_type_node = build_complex_type (integer_type_node);
9856 complex_float_type_node = build_complex_type (float_type_node);
9857 complex_double_type_node = build_complex_type (double_type_node);
9858 complex_long_double_type_node = build_complex_type (long_double_type_node);
9860 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9861 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9862 sat_ ## KIND ## _type_node = \
9863 make_sat_signed_ ## KIND ## _type (SIZE); \
9864 sat_unsigned_ ## KIND ## _type_node = \
9865 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9866 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9867 unsigned_ ## KIND ## _type_node = \
9868 make_unsigned_ ## KIND ## _type (SIZE);
9870 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9871 sat_ ## WIDTH ## KIND ## _type_node = \
9872 make_sat_signed_ ## KIND ## _type (SIZE); \
9873 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9874 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9875 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9876 unsigned_ ## WIDTH ## KIND ## _type_node = \
9877 make_unsigned_ ## KIND ## _type (SIZE);
9879 /* Make fixed-point type nodes based on four different widths. */
9880 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9881 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9882 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9883 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9884 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9886 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9887 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9888 NAME ## _type_node = \
9889 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9890 u ## NAME ## _type_node = \
9891 make_or_reuse_unsigned_ ## KIND ## _type \
9892 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9893 sat_ ## NAME ## _type_node = \
9894 make_or_reuse_sat_signed_ ## KIND ## _type \
9895 (GET_MODE_BITSIZE (MODE ## mode)); \
9896 sat_u ## NAME ## _type_node = \
9897 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9898 (GET_MODE_BITSIZE (U ## MODE ## mode));
9900 /* Fixed-point type and mode nodes. */
9901 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9902 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9903 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9904 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9905 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9906 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9907 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9908 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9909 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9910 MAKE_FIXED_MODE_NODE (accum, da, DA)
9911 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9914 tree t = targetm.build_builtin_va_list ();
9916 /* Many back-ends define record types without setting TYPE_NAME.
9917 If we copied the record type here, we'd keep the original
9918 record type without a name. This breaks name mangling. So,
9919 don't copy record types and let c_common_nodes_and_builtins()
9920 declare the type to be __builtin_va_list. */
9921 if (TREE_CODE (t) != RECORD_TYPE)
9922 t = build_variant_type_copy (t);
9924 va_list_type_node = t;
9928 /* Modify DECL for given flags.
9929 TM_PURE attribute is set only on types, so the function will modify
9930 DECL's type when ECF_TM_PURE is used. */
9932 void
9933 set_call_expr_flags (tree decl, int flags)
9935 if (flags & ECF_NOTHROW)
9936 TREE_NOTHROW (decl) = 1;
9937 if (flags & ECF_CONST)
9938 TREE_READONLY (decl) = 1;
9939 if (flags & ECF_PURE)
9940 DECL_PURE_P (decl) = 1;
9941 if (flags & ECF_LOOPING_CONST_OR_PURE)
9942 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9943 if (flags & ECF_NOVOPS)
9944 DECL_IS_NOVOPS (decl) = 1;
9945 if (flags & ECF_NORETURN)
9946 TREE_THIS_VOLATILE (decl) = 1;
9947 if (flags & ECF_MALLOC)
9948 DECL_IS_MALLOC (decl) = 1;
9949 if (flags & ECF_RETURNS_TWICE)
9950 DECL_IS_RETURNS_TWICE (decl) = 1;
9951 if (flags & ECF_LEAF)
9952 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9953 NULL, DECL_ATTRIBUTES (decl));
9954 if ((flags & ECF_TM_PURE) && flag_tm)
9955 apply_tm_attr (decl, get_identifier ("transaction_pure"));
9956 /* Looping const or pure is implied by noreturn.
9957 There is currently no way to declare looping const or looping pure alone. */
9958 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9959 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9963 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9965 static void
9966 local_define_builtin (const char *name, tree type, enum built_in_function code,
9967 const char *library_name, int ecf_flags)
9969 tree decl;
9971 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9972 library_name, NULL_TREE);
9973 set_call_expr_flags (decl, ecf_flags);
9975 set_builtin_decl (code, decl, true);
9978 /* Call this function after instantiating all builtins that the language
9979 front end cares about. This will build the rest of the builtins
9980 and internal functions that are relied upon by the tree optimizers and
9981 the middle-end. */
9983 void
9984 build_common_builtin_nodes (void)
9986 tree tmp, ftype;
9987 int ecf_flags;
9989 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9991 ftype = build_function_type (void_type_node, void_list_node);
9992 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
9993 "__builtin_unreachable",
9994 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9995 | ECF_CONST);
9998 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9999 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10001 ftype = build_function_type_list (ptr_type_node,
10002 ptr_type_node, const_ptr_type_node,
10003 size_type_node, NULL_TREE);
10005 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
10006 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
10007 "memcpy", ECF_NOTHROW | ECF_LEAF);
10008 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10009 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
10010 "memmove", ECF_NOTHROW | ECF_LEAF);
10013 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
10015 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
10016 const_ptr_type_node, size_type_node,
10017 NULL_TREE);
10018 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
10019 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10022 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
10024 ftype = build_function_type_list (ptr_type_node,
10025 ptr_type_node, integer_type_node,
10026 size_type_node, NULL_TREE);
10027 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
10028 "memset", ECF_NOTHROW | ECF_LEAF);
10031 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
10033 ftype = build_function_type_list (ptr_type_node,
10034 size_type_node, NULL_TREE);
10035 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
10036 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
10039 ftype = build_function_type_list (ptr_type_node, size_type_node,
10040 size_type_node, NULL_TREE);
10041 local_define_builtin ("__builtin_alloca_with_align", ftype,
10042 BUILT_IN_ALLOCA_WITH_ALIGN, "alloca",
10043 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
10045 /* If we're checking the stack, `alloca' can throw. */
10046 if (flag_stack_check)
10048 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
10049 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
10052 ftype = build_function_type_list (void_type_node,
10053 ptr_type_node, ptr_type_node,
10054 ptr_type_node, NULL_TREE);
10055 local_define_builtin ("__builtin_init_trampoline", ftype,
10056 BUILT_IN_INIT_TRAMPOLINE,
10057 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
10058 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
10059 BUILT_IN_INIT_HEAP_TRAMPOLINE,
10060 "__builtin_init_heap_trampoline",
10061 ECF_NOTHROW | ECF_LEAF);
10063 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
10064 local_define_builtin ("__builtin_adjust_trampoline", ftype,
10065 BUILT_IN_ADJUST_TRAMPOLINE,
10066 "__builtin_adjust_trampoline",
10067 ECF_CONST | ECF_NOTHROW);
10069 ftype = build_function_type_list (void_type_node,
10070 ptr_type_node, ptr_type_node, NULL_TREE);
10071 local_define_builtin ("__builtin_nonlocal_goto", ftype,
10072 BUILT_IN_NONLOCAL_GOTO,
10073 "__builtin_nonlocal_goto",
10074 ECF_NORETURN | ECF_NOTHROW);
10076 ftype = build_function_type_list (void_type_node,
10077 ptr_type_node, ptr_type_node, NULL_TREE);
10078 local_define_builtin ("__builtin_setjmp_setup", ftype,
10079 BUILT_IN_SETJMP_SETUP,
10080 "__builtin_setjmp_setup", ECF_NOTHROW);
10082 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10083 local_define_builtin ("__builtin_setjmp_receiver", ftype,
10084 BUILT_IN_SETJMP_RECEIVER,
10085 "__builtin_setjmp_receiver", ECF_NOTHROW | ECF_LEAF);
10087 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
10088 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
10089 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
10091 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10092 local_define_builtin ("__builtin_stack_restore", ftype,
10093 BUILT_IN_STACK_RESTORE,
10094 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
10096 /* If there's a possibility that we might use the ARM EABI, build the
10097 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
10098 if (targetm.arm_eabi_unwinder)
10100 ftype = build_function_type_list (void_type_node, NULL_TREE);
10101 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
10102 BUILT_IN_CXA_END_CLEANUP,
10103 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
10106 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10107 local_define_builtin ("__builtin_unwind_resume", ftype,
10108 BUILT_IN_UNWIND_RESUME,
10109 ((targetm_common.except_unwind_info (&global_options)
10110 == UI_SJLJ)
10111 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
10112 ECF_NORETURN);
10114 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
10116 ftype = build_function_type_list (ptr_type_node, integer_type_node,
10117 NULL_TREE);
10118 local_define_builtin ("__builtin_return_address", ftype,
10119 BUILT_IN_RETURN_ADDRESS,
10120 "__builtin_return_address",
10121 ECF_NOTHROW);
10124 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
10125 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10127 ftype = build_function_type_list (void_type_node, ptr_type_node,
10128 ptr_type_node, NULL_TREE);
10129 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
10130 local_define_builtin ("__cyg_profile_func_enter", ftype,
10131 BUILT_IN_PROFILE_FUNC_ENTER,
10132 "__cyg_profile_func_enter", 0);
10133 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10134 local_define_builtin ("__cyg_profile_func_exit", ftype,
10135 BUILT_IN_PROFILE_FUNC_EXIT,
10136 "__cyg_profile_func_exit", 0);
10139 /* The exception object and filter values from the runtime. The argument
10140 must be zero before exception lowering, i.e. from the front end. After
10141 exception lowering, it will be the region number for the exception
10142 landing pad. These functions are PURE instead of CONST to prevent
10143 them from being hoisted past the exception edge that will initialize
10144 its value in the landing pad. */
10145 ftype = build_function_type_list (ptr_type_node,
10146 integer_type_node, NULL_TREE);
10147 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
10148 /* Only use TM_PURE if we we have TM language support. */
10149 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
10150 ecf_flags |= ECF_TM_PURE;
10151 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
10152 "__builtin_eh_pointer", ecf_flags);
10154 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
10155 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
10156 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
10157 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10159 ftype = build_function_type_list (void_type_node,
10160 integer_type_node, integer_type_node,
10161 NULL_TREE);
10162 local_define_builtin ("__builtin_eh_copy_values", ftype,
10163 BUILT_IN_EH_COPY_VALUES,
10164 "__builtin_eh_copy_values", ECF_NOTHROW);
10166 /* Complex multiplication and division. These are handled as builtins
10167 rather than optabs because emit_library_call_value doesn't support
10168 complex. Further, we can do slightly better with folding these
10169 beasties if the real and complex parts of the arguments are separate. */
10171 int mode;
10173 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10175 char mode_name_buf[4], *q;
10176 const char *p;
10177 enum built_in_function mcode, dcode;
10178 tree type, inner_type;
10179 const char *prefix = "__";
10181 if (targetm.libfunc_gnu_prefix)
10182 prefix = "__gnu_";
10184 type = lang_hooks.types.type_for_mode ((machine_mode) mode, 0);
10185 if (type == NULL)
10186 continue;
10187 inner_type = TREE_TYPE (type);
10189 ftype = build_function_type_list (type, inner_type, inner_type,
10190 inner_type, inner_type, NULL_TREE);
10192 mcode = ((enum built_in_function)
10193 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10194 dcode = ((enum built_in_function)
10195 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10197 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10198 *q = TOLOWER (*p);
10199 *q = '\0';
10201 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10202 NULL);
10203 local_define_builtin (built_in_names[mcode], ftype, mcode,
10204 built_in_names[mcode],
10205 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10207 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10208 NULL);
10209 local_define_builtin (built_in_names[dcode], ftype, dcode,
10210 built_in_names[dcode],
10211 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10215 init_internal_fns ();
10218 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10219 better way.
10221 If we requested a pointer to a vector, build up the pointers that
10222 we stripped off while looking for the inner type. Similarly for
10223 return values from functions.
10225 The argument TYPE is the top of the chain, and BOTTOM is the
10226 new type which we will point to. */
10228 tree
10229 reconstruct_complex_type (tree type, tree bottom)
10231 tree inner, outer;
10233 if (TREE_CODE (type) == POINTER_TYPE)
10235 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10236 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10237 TYPE_REF_CAN_ALIAS_ALL (type));
10239 else if (TREE_CODE (type) == REFERENCE_TYPE)
10241 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10242 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10243 TYPE_REF_CAN_ALIAS_ALL (type));
10245 else if (TREE_CODE (type) == ARRAY_TYPE)
10247 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10248 outer = build_array_type (inner, TYPE_DOMAIN (type));
10250 else if (TREE_CODE (type) == FUNCTION_TYPE)
10252 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10253 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
10255 else if (TREE_CODE (type) == METHOD_TYPE)
10257 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10258 /* The build_method_type_directly() routine prepends 'this' to argument list,
10259 so we must compensate by getting rid of it. */
10260 outer
10261 = build_method_type_directly
10262 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10263 inner,
10264 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10266 else if (TREE_CODE (type) == OFFSET_TYPE)
10268 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10269 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10271 else
10272 return bottom;
10274 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10275 TYPE_QUALS (type));
10278 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10279 the inner type. */
10280 tree
10281 build_vector_type_for_mode (tree innertype, machine_mode mode)
10283 int nunits;
10285 switch (GET_MODE_CLASS (mode))
10287 case MODE_VECTOR_INT:
10288 case MODE_VECTOR_FLOAT:
10289 case MODE_VECTOR_FRACT:
10290 case MODE_VECTOR_UFRACT:
10291 case MODE_VECTOR_ACCUM:
10292 case MODE_VECTOR_UACCUM:
10293 nunits = GET_MODE_NUNITS (mode);
10294 break;
10296 case MODE_INT:
10297 /* Check that there are no leftover bits. */
10298 gcc_assert (GET_MODE_BITSIZE (mode)
10299 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10301 nunits = GET_MODE_BITSIZE (mode)
10302 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10303 break;
10305 default:
10306 gcc_unreachable ();
10309 return make_vector_type (innertype, nunits, mode);
10312 /* Similarly, but takes the inner type and number of units, which must be
10313 a power of two. */
10315 tree
10316 build_vector_type (tree innertype, int nunits)
10318 return make_vector_type (innertype, nunits, VOIDmode);
10321 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10323 tree
10324 build_opaque_vector_type (tree innertype, int nunits)
10326 tree t = make_vector_type (innertype, nunits, VOIDmode);
10327 tree cand;
10328 /* We always build the non-opaque variant before the opaque one,
10329 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10330 cand = TYPE_NEXT_VARIANT (t);
10331 if (cand
10332 && TYPE_VECTOR_OPAQUE (cand)
10333 && check_qualified_type (cand, t, TYPE_QUALS (t)))
10334 return cand;
10335 /* Othewise build a variant type and make sure to queue it after
10336 the non-opaque type. */
10337 cand = build_distinct_type_copy (t);
10338 TYPE_VECTOR_OPAQUE (cand) = true;
10339 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10340 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10341 TYPE_NEXT_VARIANT (t) = cand;
10342 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10343 return cand;
10347 /* Given an initializer INIT, return TRUE if INIT is zero or some
10348 aggregate of zeros. Otherwise return FALSE. */
10349 bool
10350 initializer_zerop (const_tree init)
10352 tree elt;
10354 STRIP_NOPS (init);
10356 switch (TREE_CODE (init))
10358 case INTEGER_CST:
10359 return integer_zerop (init);
10361 case REAL_CST:
10362 /* ??? Note that this is not correct for C4X float formats. There,
10363 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10364 negative exponent. */
10365 return real_zerop (init)
10366 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10368 case FIXED_CST:
10369 return fixed_zerop (init);
10371 case COMPLEX_CST:
10372 return integer_zerop (init)
10373 || (real_zerop (init)
10374 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10375 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10377 case VECTOR_CST:
10379 unsigned i;
10380 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10381 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10382 return false;
10383 return true;
10386 case CONSTRUCTOR:
10388 unsigned HOST_WIDE_INT idx;
10390 if (TREE_CLOBBER_P (init))
10391 return false;
10392 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10393 if (!initializer_zerop (elt))
10394 return false;
10395 return true;
10398 case STRING_CST:
10400 int i;
10402 /* We need to loop through all elements to handle cases like
10403 "\0" and "\0foobar". */
10404 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10405 if (TREE_STRING_POINTER (init)[i] != '\0')
10406 return false;
10408 return true;
10411 default:
10412 return false;
10416 /* Check if vector VEC consists of all the equal elements and
10417 that the number of elements corresponds to the type of VEC.
10418 The function returns first element of the vector
10419 or NULL_TREE if the vector is not uniform. */
10420 tree
10421 uniform_vector_p (const_tree vec)
10423 tree first, t;
10424 unsigned i;
10426 if (vec == NULL_TREE)
10427 return NULL_TREE;
10429 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10431 if (TREE_CODE (vec) == VECTOR_CST)
10433 first = VECTOR_CST_ELT (vec, 0);
10434 for (i = 1; i < VECTOR_CST_NELTS (vec); ++i)
10435 if (!operand_equal_p (first, VECTOR_CST_ELT (vec, i), 0))
10436 return NULL_TREE;
10438 return first;
10441 else if (TREE_CODE (vec) == CONSTRUCTOR)
10443 first = error_mark_node;
10445 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10447 if (i == 0)
10449 first = t;
10450 continue;
10452 if (!operand_equal_p (first, t, 0))
10453 return NULL_TREE;
10455 if (i != TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)))
10456 return NULL_TREE;
10458 return first;
10461 return NULL_TREE;
10464 /* Build an empty statement at location LOC. */
10466 tree
10467 build_empty_stmt (location_t loc)
10469 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10470 SET_EXPR_LOCATION (t, loc);
10471 return t;
10475 /* Build an OpenMP clause with code CODE. LOC is the location of the
10476 clause. */
10478 tree
10479 build_omp_clause (location_t loc, enum omp_clause_code code)
10481 tree t;
10482 int size, length;
10484 length = omp_clause_num_ops[code];
10485 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10487 record_node_allocation_statistics (OMP_CLAUSE, size);
10489 t = (tree) ggc_internal_alloc (size);
10490 memset (t, 0, size);
10491 TREE_SET_CODE (t, OMP_CLAUSE);
10492 OMP_CLAUSE_SET_CODE (t, code);
10493 OMP_CLAUSE_LOCATION (t) = loc;
10495 return t;
10498 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10499 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10500 Except for the CODE and operand count field, other storage for the
10501 object is initialized to zeros. */
10503 tree
10504 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10506 tree t;
10507 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10509 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10510 gcc_assert (len >= 1);
10512 record_node_allocation_statistics (code, length);
10514 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10516 TREE_SET_CODE (t, code);
10518 /* Can't use TREE_OPERAND to store the length because if checking is
10519 enabled, it will try to check the length before we store it. :-P */
10520 t->exp.operands[0] = build_int_cst (sizetype, len);
10522 return t;
10525 /* Helper function for build_call_* functions; build a CALL_EXPR with
10526 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10527 the argument slots. */
10529 static tree
10530 build_call_1 (tree return_type, tree fn, int nargs)
10532 tree t;
10534 t = build_vl_exp (CALL_EXPR, nargs + 3);
10535 TREE_TYPE (t) = return_type;
10536 CALL_EXPR_FN (t) = fn;
10537 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10539 return t;
10542 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10543 FN and a null static chain slot. NARGS is the number of call arguments
10544 which are specified as "..." arguments. */
10546 tree
10547 build_call_nary (tree return_type, tree fn, int nargs, ...)
10549 tree ret;
10550 va_list args;
10551 va_start (args, nargs);
10552 ret = build_call_valist (return_type, fn, nargs, args);
10553 va_end (args);
10554 return ret;
10557 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10558 FN and a null static chain slot. NARGS is the number of call arguments
10559 which are specified as a va_list ARGS. */
10561 tree
10562 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10564 tree t;
10565 int i;
10567 t = build_call_1 (return_type, fn, nargs);
10568 for (i = 0; i < nargs; i++)
10569 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10570 process_call_operands (t);
10571 return t;
10574 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10575 FN and a null static chain slot. NARGS is the number of call arguments
10576 which are specified as a tree array ARGS. */
10578 tree
10579 build_call_array_loc (location_t loc, tree return_type, tree fn,
10580 int nargs, const tree *args)
10582 tree t;
10583 int i;
10585 t = build_call_1 (return_type, fn, nargs);
10586 for (i = 0; i < nargs; i++)
10587 CALL_EXPR_ARG (t, i) = args[i];
10588 process_call_operands (t);
10589 SET_EXPR_LOCATION (t, loc);
10590 return t;
10593 /* Like build_call_array, but takes a vec. */
10595 tree
10596 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10598 tree ret, t;
10599 unsigned int ix;
10601 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10602 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10603 CALL_EXPR_ARG (ret, ix) = t;
10604 process_call_operands (ret);
10605 return ret;
10608 /* Conveniently construct a function call expression. FNDECL names the
10609 function to be called and N arguments are passed in the array
10610 ARGARRAY. */
10612 tree
10613 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
10615 tree fntype = TREE_TYPE (fndecl);
10616 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10618 return fold_build_call_array_loc (loc, TREE_TYPE (fntype), fn, n, argarray);
10621 /* Conveniently construct a function call expression. FNDECL names the
10622 function to be called and the arguments are passed in the vector
10623 VEC. */
10625 tree
10626 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
10628 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
10629 vec_safe_address (vec));
10633 /* Conveniently construct a function call expression. FNDECL names the
10634 function to be called, N is the number of arguments, and the "..."
10635 parameters are the argument expressions. */
10637 tree
10638 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
10640 va_list ap;
10641 tree *argarray = XALLOCAVEC (tree, n);
10642 int i;
10644 va_start (ap, n);
10645 for (i = 0; i < n; i++)
10646 argarray[i] = va_arg (ap, tree);
10647 va_end (ap);
10648 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10651 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
10652 varargs macros aren't supported by all bootstrap compilers. */
10654 tree
10655 build_call_expr (tree fndecl, int n, ...)
10657 va_list ap;
10658 tree *argarray = XALLOCAVEC (tree, n);
10659 int i;
10661 va_start (ap, n);
10662 for (i = 0; i < n; i++)
10663 argarray[i] = va_arg (ap, tree);
10664 va_end (ap);
10665 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
10668 /* Build internal call expression. This is just like CALL_EXPR, except
10669 its CALL_EXPR_FN is NULL. It will get gimplified later into ordinary
10670 internal function. */
10672 tree
10673 build_call_expr_internal_loc (location_t loc, enum internal_fn ifn,
10674 tree type, int n, ...)
10676 va_list ap;
10677 int i;
10679 tree fn = build_call_1 (type, NULL_TREE, n);
10680 va_start (ap, n);
10681 for (i = 0; i < n; i++)
10682 CALL_EXPR_ARG (fn, i) = va_arg (ap, tree);
10683 va_end (ap);
10684 SET_EXPR_LOCATION (fn, loc);
10685 CALL_EXPR_IFN (fn) = ifn;
10686 return fn;
10689 /* Create a new constant string literal and return a char* pointer to it.
10690 The STRING_CST value is the LEN characters at STR. */
10691 tree
10692 build_string_literal (int len, const char *str)
10694 tree t, elem, index, type;
10696 t = build_string (len, str);
10697 elem = build_type_variant (char_type_node, 1, 0);
10698 index = build_index_type (size_int (len - 1));
10699 type = build_array_type (elem, index);
10700 TREE_TYPE (t) = type;
10701 TREE_CONSTANT (t) = 1;
10702 TREE_READONLY (t) = 1;
10703 TREE_STATIC (t) = 1;
10705 type = build_pointer_type (elem);
10706 t = build1 (ADDR_EXPR, type,
10707 build4 (ARRAY_REF, elem,
10708 t, integer_zero_node, NULL_TREE, NULL_TREE));
10709 return t;
10714 /* Return true if T (assumed to be a DECL) must be assigned a memory
10715 location. */
10717 bool
10718 needs_to_live_in_memory (const_tree t)
10720 return (TREE_ADDRESSABLE (t)
10721 || is_global_var (t)
10722 || (TREE_CODE (t) == RESULT_DECL
10723 && !DECL_BY_REFERENCE (t)
10724 && aggregate_value_p (t, current_function_decl)));
10727 /* Return value of a constant X and sign-extend it. */
10729 HOST_WIDE_INT
10730 int_cst_value (const_tree x)
10732 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10733 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10735 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10736 gcc_assert (cst_and_fits_in_hwi (x));
10738 if (bits < HOST_BITS_PER_WIDE_INT)
10740 bool negative = ((val >> (bits - 1)) & 1) != 0;
10741 if (negative)
10742 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10743 else
10744 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10747 return val;
10750 /* If TYPE is an integral or pointer type, return an integer type with
10751 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10752 if TYPE is already an integer type of signedness UNSIGNEDP. */
10754 tree
10755 signed_or_unsigned_type_for (int unsignedp, tree type)
10757 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10758 return type;
10760 if (TREE_CODE (type) == VECTOR_TYPE)
10762 tree inner = TREE_TYPE (type);
10763 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10764 if (!inner2)
10765 return NULL_TREE;
10766 if (inner == inner2)
10767 return type;
10768 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10771 if (!INTEGRAL_TYPE_P (type)
10772 && !POINTER_TYPE_P (type)
10773 && TREE_CODE (type) != OFFSET_TYPE)
10774 return NULL_TREE;
10776 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10779 /* If TYPE is an integral or pointer type, return an integer type with
10780 the same precision which is unsigned, or itself if TYPE is already an
10781 unsigned integer type. */
10783 tree
10784 unsigned_type_for (tree type)
10786 return signed_or_unsigned_type_for (1, type);
10789 /* If TYPE is an integral or pointer type, return an integer type with
10790 the same precision which is signed, or itself if TYPE is already a
10791 signed integer type. */
10793 tree
10794 signed_type_for (tree type)
10796 return signed_or_unsigned_type_for (0, type);
10799 /* If TYPE is a vector type, return a signed integer vector type with the
10800 same width and number of subparts. Otherwise return boolean_type_node. */
10802 tree
10803 truth_type_for (tree type)
10805 if (TREE_CODE (type) == VECTOR_TYPE)
10807 tree elem = lang_hooks.types.type_for_size
10808 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10809 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10811 else
10812 return boolean_type_node;
10815 /* Returns the largest value obtainable by casting something in INNER type to
10816 OUTER type. */
10818 tree
10819 upper_bound_in_type (tree outer, tree inner)
10821 unsigned int det = 0;
10822 unsigned oprec = TYPE_PRECISION (outer);
10823 unsigned iprec = TYPE_PRECISION (inner);
10824 unsigned prec;
10826 /* Compute a unique number for every combination. */
10827 det |= (oprec > iprec) ? 4 : 0;
10828 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10829 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10831 /* Determine the exponent to use. */
10832 switch (det)
10834 case 0:
10835 case 1:
10836 /* oprec <= iprec, outer: signed, inner: don't care. */
10837 prec = oprec - 1;
10838 break;
10839 case 2:
10840 case 3:
10841 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10842 prec = oprec;
10843 break;
10844 case 4:
10845 /* oprec > iprec, outer: signed, inner: signed. */
10846 prec = iprec - 1;
10847 break;
10848 case 5:
10849 /* oprec > iprec, outer: signed, inner: unsigned. */
10850 prec = iprec;
10851 break;
10852 case 6:
10853 /* oprec > iprec, outer: unsigned, inner: signed. */
10854 prec = oprec;
10855 break;
10856 case 7:
10857 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10858 prec = iprec;
10859 break;
10860 default:
10861 gcc_unreachable ();
10864 return wide_int_to_tree (outer,
10865 wi::mask (prec, false, TYPE_PRECISION (outer)));
10868 /* Returns the smallest value obtainable by casting something in INNER type to
10869 OUTER type. */
10871 tree
10872 lower_bound_in_type (tree outer, tree inner)
10874 unsigned oprec = TYPE_PRECISION (outer);
10875 unsigned iprec = TYPE_PRECISION (inner);
10877 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10878 and obtain 0. */
10879 if (TYPE_UNSIGNED (outer)
10880 /* If we are widening something of an unsigned type, OUTER type
10881 contains all values of INNER type. In particular, both INNER
10882 and OUTER types have zero in common. */
10883 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10884 return build_int_cst (outer, 0);
10885 else
10887 /* If we are widening a signed type to another signed type, we
10888 want to obtain -2^^(iprec-1). If we are keeping the
10889 precision or narrowing to a signed type, we want to obtain
10890 -2^(oprec-1). */
10891 unsigned prec = oprec > iprec ? iprec : oprec;
10892 return wide_int_to_tree (outer,
10893 wi::mask (prec - 1, true,
10894 TYPE_PRECISION (outer)));
10898 /* Return nonzero if two operands that are suitable for PHI nodes are
10899 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10900 SSA_NAME or invariant. Note that this is strictly an optimization.
10901 That is, callers of this function can directly call operand_equal_p
10902 and get the same result, only slower. */
10905 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10907 if (arg0 == arg1)
10908 return 1;
10909 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10910 return 0;
10911 return operand_equal_p (arg0, arg1, 0);
10914 /* Returns number of zeros at the end of binary representation of X. */
10916 tree
10917 num_ending_zeros (const_tree x)
10919 return build_int_cst (TREE_TYPE (x), wi::ctz (x));
10923 #define WALK_SUBTREE(NODE) \
10924 do \
10926 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10927 if (result) \
10928 return result; \
10930 while (0)
10932 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10933 be walked whenever a type is seen in the tree. Rest of operands and return
10934 value are as for walk_tree. */
10936 static tree
10937 walk_type_fields (tree type, walk_tree_fn func, void *data,
10938 hash_set<tree> *pset, walk_tree_lh lh)
10940 tree result = NULL_TREE;
10942 switch (TREE_CODE (type))
10944 case POINTER_TYPE:
10945 case REFERENCE_TYPE:
10946 case VECTOR_TYPE:
10947 /* We have to worry about mutually recursive pointers. These can't
10948 be written in C. They can in Ada. It's pathological, but
10949 there's an ACATS test (c38102a) that checks it. Deal with this
10950 by checking if we're pointing to another pointer, that one
10951 points to another pointer, that one does too, and we have no htab.
10952 If so, get a hash table. We check three levels deep to avoid
10953 the cost of the hash table if we don't need one. */
10954 if (POINTER_TYPE_P (TREE_TYPE (type))
10955 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10956 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10957 && !pset)
10959 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10960 func, data);
10961 if (result)
10962 return result;
10964 break;
10967 /* ... fall through ... */
10969 case COMPLEX_TYPE:
10970 WALK_SUBTREE (TREE_TYPE (type));
10971 break;
10973 case METHOD_TYPE:
10974 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10976 /* Fall through. */
10978 case FUNCTION_TYPE:
10979 WALK_SUBTREE (TREE_TYPE (type));
10981 tree arg;
10983 /* We never want to walk into default arguments. */
10984 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10985 WALK_SUBTREE (TREE_VALUE (arg));
10987 break;
10989 case ARRAY_TYPE:
10990 /* Don't follow this nodes's type if a pointer for fear that
10991 we'll have infinite recursion. If we have a PSET, then we
10992 need not fear. */
10993 if (pset
10994 || (!POINTER_TYPE_P (TREE_TYPE (type))
10995 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10996 WALK_SUBTREE (TREE_TYPE (type));
10997 WALK_SUBTREE (TYPE_DOMAIN (type));
10998 break;
11000 case OFFSET_TYPE:
11001 WALK_SUBTREE (TREE_TYPE (type));
11002 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
11003 break;
11005 default:
11006 break;
11009 return NULL_TREE;
11012 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
11013 called with the DATA and the address of each sub-tree. If FUNC returns a
11014 non-NULL value, the traversal is stopped, and the value returned by FUNC
11015 is returned. If PSET is non-NULL it is used to record the nodes visited,
11016 and to avoid visiting a node more than once. */
11018 tree
11019 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
11020 hash_set<tree> *pset, walk_tree_lh lh)
11022 enum tree_code code;
11023 int walk_subtrees;
11024 tree result;
11026 #define WALK_SUBTREE_TAIL(NODE) \
11027 do \
11029 tp = & (NODE); \
11030 goto tail_recurse; \
11032 while (0)
11034 tail_recurse:
11035 /* Skip empty subtrees. */
11036 if (!*tp)
11037 return NULL_TREE;
11039 /* Don't walk the same tree twice, if the user has requested
11040 that we avoid doing so. */
11041 if (pset && pset->add (*tp))
11042 return NULL_TREE;
11044 /* Call the function. */
11045 walk_subtrees = 1;
11046 result = (*func) (tp, &walk_subtrees, data);
11048 /* If we found something, return it. */
11049 if (result)
11050 return result;
11052 code = TREE_CODE (*tp);
11054 /* Even if we didn't, FUNC may have decided that there was nothing
11055 interesting below this point in the tree. */
11056 if (!walk_subtrees)
11058 /* But we still need to check our siblings. */
11059 if (code == TREE_LIST)
11060 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11061 else if (code == OMP_CLAUSE)
11062 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11063 else
11064 return NULL_TREE;
11067 if (lh)
11069 result = (*lh) (tp, &walk_subtrees, func, data, pset);
11070 if (result || !walk_subtrees)
11071 return result;
11074 switch (code)
11076 case ERROR_MARK:
11077 case IDENTIFIER_NODE:
11078 case INTEGER_CST:
11079 case REAL_CST:
11080 case FIXED_CST:
11081 case VECTOR_CST:
11082 case STRING_CST:
11083 case BLOCK:
11084 case PLACEHOLDER_EXPR:
11085 case SSA_NAME:
11086 case FIELD_DECL:
11087 case RESULT_DECL:
11088 /* None of these have subtrees other than those already walked
11089 above. */
11090 break;
11092 case TREE_LIST:
11093 WALK_SUBTREE (TREE_VALUE (*tp));
11094 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11095 break;
11097 case TREE_VEC:
11099 int len = TREE_VEC_LENGTH (*tp);
11101 if (len == 0)
11102 break;
11104 /* Walk all elements but the first. */
11105 while (--len)
11106 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
11108 /* Now walk the first one as a tail call. */
11109 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
11112 case COMPLEX_CST:
11113 WALK_SUBTREE (TREE_REALPART (*tp));
11114 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
11116 case CONSTRUCTOR:
11118 unsigned HOST_WIDE_INT idx;
11119 constructor_elt *ce;
11121 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*tp), idx, &ce);
11122 idx++)
11123 WALK_SUBTREE (ce->value);
11125 break;
11127 case SAVE_EXPR:
11128 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
11130 case BIND_EXPR:
11132 tree decl;
11133 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
11135 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11136 into declarations that are just mentioned, rather than
11137 declared; they don't really belong to this part of the tree.
11138 And, we can see cycles: the initializer for a declaration
11139 can refer to the declaration itself. */
11140 WALK_SUBTREE (DECL_INITIAL (decl));
11141 WALK_SUBTREE (DECL_SIZE (decl));
11142 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11144 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
11147 case STATEMENT_LIST:
11149 tree_stmt_iterator i;
11150 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
11151 WALK_SUBTREE (*tsi_stmt_ptr (i));
11153 break;
11155 case OMP_CLAUSE:
11156 switch (OMP_CLAUSE_CODE (*tp))
11158 case OMP_CLAUSE_DEVICE_RESIDENT:
11159 case OMP_CLAUSE_USE_DEVICE:
11160 case OMP_CLAUSE_GANG:
11161 case OMP_CLAUSE_ASYNC:
11162 case OMP_CLAUSE_WAIT:
11163 case OMP_CLAUSE_WORKER:
11164 case OMP_CLAUSE_VECTOR:
11165 case OMP_CLAUSE_NUM_GANGS:
11166 case OMP_CLAUSE_NUM_WORKERS:
11167 case OMP_CLAUSE_VECTOR_LENGTH:
11168 case OMP_CLAUSE_PRIVATE:
11169 case OMP_CLAUSE_SHARED:
11170 case OMP_CLAUSE_FIRSTPRIVATE:
11171 case OMP_CLAUSE_COPYIN:
11172 case OMP_CLAUSE_COPYPRIVATE:
11173 case OMP_CLAUSE_FINAL:
11174 case OMP_CLAUSE_IF:
11175 case OMP_CLAUSE_NUM_THREADS:
11176 case OMP_CLAUSE_SCHEDULE:
11177 case OMP_CLAUSE_UNIFORM:
11178 case OMP_CLAUSE_DEPEND:
11179 case OMP_CLAUSE_NUM_TEAMS:
11180 case OMP_CLAUSE_THREAD_LIMIT:
11181 case OMP_CLAUSE_DEVICE:
11182 case OMP_CLAUSE_DIST_SCHEDULE:
11183 case OMP_CLAUSE_SAFELEN:
11184 case OMP_CLAUSE_SIMDLEN:
11185 case OMP_CLAUSE__LOOPTEMP_:
11186 case OMP_CLAUSE__SIMDUID_:
11187 case OMP_CLAUSE__CILK_FOR_COUNT_:
11188 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
11189 /* FALLTHRU */
11191 case OMP_CLAUSE_INDEPENDENT:
11192 case OMP_CLAUSE_NOWAIT:
11193 case OMP_CLAUSE_ORDERED:
11194 case OMP_CLAUSE_DEFAULT:
11195 case OMP_CLAUSE_UNTIED:
11196 case OMP_CLAUSE_MERGEABLE:
11197 case OMP_CLAUSE_PROC_BIND:
11198 case OMP_CLAUSE_INBRANCH:
11199 case OMP_CLAUSE_NOTINBRANCH:
11200 case OMP_CLAUSE_FOR:
11201 case OMP_CLAUSE_PARALLEL:
11202 case OMP_CLAUSE_SECTIONS:
11203 case OMP_CLAUSE_TASKGROUP:
11204 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11206 case OMP_CLAUSE_LASTPRIVATE:
11207 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11208 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
11209 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11211 case OMP_CLAUSE_COLLAPSE:
11213 int i;
11214 for (i = 0; i < 3; i++)
11215 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11216 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11219 case OMP_CLAUSE_LINEAR:
11220 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11221 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STEP (*tp));
11222 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STMT (*tp));
11223 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11225 case OMP_CLAUSE_ALIGNED:
11226 case OMP_CLAUSE_FROM:
11227 case OMP_CLAUSE_TO:
11228 case OMP_CLAUSE_MAP:
11229 case OMP_CLAUSE__CACHE_:
11230 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11231 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11232 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11234 case OMP_CLAUSE_REDUCTION:
11236 int i;
11237 for (i = 0; i < 4; i++)
11238 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11239 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11242 default:
11243 gcc_unreachable ();
11245 break;
11247 case TARGET_EXPR:
11249 int i, len;
11251 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11252 But, we only want to walk once. */
11253 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11254 for (i = 0; i < len; ++i)
11255 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11256 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11259 case DECL_EXPR:
11260 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11261 defining. We only want to walk into these fields of a type in this
11262 case and not in the general case of a mere reference to the type.
11264 The criterion is as follows: if the field can be an expression, it
11265 must be walked only here. This should be in keeping with the fields
11266 that are directly gimplified in gimplify_type_sizes in order for the
11267 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11268 variable-sized types.
11270 Note that DECLs get walked as part of processing the BIND_EXPR. */
11271 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11273 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11274 if (TREE_CODE (*type_p) == ERROR_MARK)
11275 return NULL_TREE;
11277 /* Call the function for the type. See if it returns anything or
11278 doesn't want us to continue. If we are to continue, walk both
11279 the normal fields and those for the declaration case. */
11280 result = (*func) (type_p, &walk_subtrees, data);
11281 if (result || !walk_subtrees)
11282 return result;
11284 /* But do not walk a pointed-to type since it may itself need to
11285 be walked in the declaration case if it isn't anonymous. */
11286 if (!POINTER_TYPE_P (*type_p))
11288 result = walk_type_fields (*type_p, func, data, pset, lh);
11289 if (result)
11290 return result;
11293 /* If this is a record type, also walk the fields. */
11294 if (RECORD_OR_UNION_TYPE_P (*type_p))
11296 tree field;
11298 for (field = TYPE_FIELDS (*type_p); field;
11299 field = DECL_CHAIN (field))
11301 /* We'd like to look at the type of the field, but we can
11302 easily get infinite recursion. So assume it's pointed
11303 to elsewhere in the tree. Also, ignore things that
11304 aren't fields. */
11305 if (TREE_CODE (field) != FIELD_DECL)
11306 continue;
11308 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11309 WALK_SUBTREE (DECL_SIZE (field));
11310 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11311 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11312 WALK_SUBTREE (DECL_QUALIFIER (field));
11316 /* Same for scalar types. */
11317 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11318 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11319 || TREE_CODE (*type_p) == INTEGER_TYPE
11320 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11321 || TREE_CODE (*type_p) == REAL_TYPE)
11323 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11324 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11327 WALK_SUBTREE (TYPE_SIZE (*type_p));
11328 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11330 /* FALLTHRU */
11332 default:
11333 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11335 int i, len;
11337 /* Walk over all the sub-trees of this operand. */
11338 len = TREE_OPERAND_LENGTH (*tp);
11340 /* Go through the subtrees. We need to do this in forward order so
11341 that the scope of a FOR_EXPR is handled properly. */
11342 if (len)
11344 for (i = 0; i < len - 1; ++i)
11345 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11346 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11349 /* If this is a type, walk the needed fields in the type. */
11350 else if (TYPE_P (*tp))
11351 return walk_type_fields (*tp, func, data, pset, lh);
11352 break;
11355 /* We didn't find what we were looking for. */
11356 return NULL_TREE;
11358 #undef WALK_SUBTREE_TAIL
11360 #undef WALK_SUBTREE
11362 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11364 tree
11365 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11366 walk_tree_lh lh)
11368 tree result;
11370 hash_set<tree> pset;
11371 result = walk_tree_1 (tp, func, data, &pset, lh);
11372 return result;
11376 tree
11377 tree_block (tree t)
11379 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11381 if (IS_EXPR_CODE_CLASS (c))
11382 return LOCATION_BLOCK (t->exp.locus);
11383 gcc_unreachable ();
11384 return NULL;
11387 void
11388 tree_set_block (tree t, tree b)
11390 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11392 if (IS_EXPR_CODE_CLASS (c))
11394 if (b)
11395 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
11396 else
11397 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
11399 else
11400 gcc_unreachable ();
11403 /* Create a nameless artificial label and put it in the current
11404 function context. The label has a location of LOC. Returns the
11405 newly created label. */
11407 tree
11408 create_artificial_label (location_t loc)
11410 tree lab = build_decl (loc,
11411 LABEL_DECL, NULL_TREE, void_type_node);
11413 DECL_ARTIFICIAL (lab) = 1;
11414 DECL_IGNORED_P (lab) = 1;
11415 DECL_CONTEXT (lab) = current_function_decl;
11416 return lab;
11419 /* Given a tree, try to return a useful variable name that we can use
11420 to prefix a temporary that is being assigned the value of the tree.
11421 I.E. given <temp> = &A, return A. */
11423 const char *
11424 get_name (tree t)
11426 tree stripped_decl;
11428 stripped_decl = t;
11429 STRIP_NOPS (stripped_decl);
11430 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11431 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11432 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11434 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11435 if (!name)
11436 return NULL;
11437 return IDENTIFIER_POINTER (name);
11439 else
11441 switch (TREE_CODE (stripped_decl))
11443 case ADDR_EXPR:
11444 return get_name (TREE_OPERAND (stripped_decl, 0));
11445 default:
11446 return NULL;
11451 /* Return true if TYPE has a variable argument list. */
11453 bool
11454 stdarg_p (const_tree fntype)
11456 function_args_iterator args_iter;
11457 tree n = NULL_TREE, t;
11459 if (!fntype)
11460 return false;
11462 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
11464 n = t;
11467 return n != NULL_TREE && n != void_type_node;
11470 /* Return true if TYPE has a prototype. */
11472 bool
11473 prototype_p (tree fntype)
11475 tree t;
11477 gcc_assert (fntype != NULL_TREE);
11479 t = TYPE_ARG_TYPES (fntype);
11480 return (t != NULL_TREE);
11483 /* If BLOCK is inlined from an __attribute__((__artificial__))
11484 routine, return pointer to location from where it has been
11485 called. */
11486 location_t *
11487 block_nonartificial_location (tree block)
11489 location_t *ret = NULL;
11491 while (block && TREE_CODE (block) == BLOCK
11492 && BLOCK_ABSTRACT_ORIGIN (block))
11494 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11496 while (TREE_CODE (ao) == BLOCK
11497 && BLOCK_ABSTRACT_ORIGIN (ao)
11498 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11499 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11501 if (TREE_CODE (ao) == FUNCTION_DECL)
11503 /* If AO is an artificial inline, point RET to the
11504 call site locus at which it has been inlined and continue
11505 the loop, in case AO's caller is also an artificial
11506 inline. */
11507 if (DECL_DECLARED_INLINE_P (ao)
11508 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11509 ret = &BLOCK_SOURCE_LOCATION (block);
11510 else
11511 break;
11513 else if (TREE_CODE (ao) != BLOCK)
11514 break;
11516 block = BLOCK_SUPERCONTEXT (block);
11518 return ret;
11522 /* If EXP is inlined from an __attribute__((__artificial__))
11523 function, return the location of the original call expression. */
11525 location_t
11526 tree_nonartificial_location (tree exp)
11528 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11530 if (loc)
11531 return *loc;
11532 else
11533 return EXPR_LOCATION (exp);
11537 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11538 nodes. */
11540 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11542 hashval_t
11543 cl_option_hasher::hash (tree x)
11545 const_tree const t = x;
11546 const char *p;
11547 size_t i;
11548 size_t len = 0;
11549 hashval_t hash = 0;
11551 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11553 p = (const char *)TREE_OPTIMIZATION (t);
11554 len = sizeof (struct cl_optimization);
11557 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11558 return cl_target_option_hash (TREE_TARGET_OPTION (t));
11560 else
11561 gcc_unreachable ();
11563 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11564 something else. */
11565 for (i = 0; i < len; i++)
11566 if (p[i])
11567 hash = (hash << 4) ^ ((i << 2) | p[i]);
11569 return hash;
11572 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11573 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11574 same. */
11576 bool
11577 cl_option_hasher::equal (tree x, tree y)
11579 const_tree const xt = x;
11580 const_tree const yt = y;
11581 const char *xp;
11582 const char *yp;
11583 size_t len;
11585 if (TREE_CODE (xt) != TREE_CODE (yt))
11586 return 0;
11588 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11590 xp = (const char *)TREE_OPTIMIZATION (xt);
11591 yp = (const char *)TREE_OPTIMIZATION (yt);
11592 len = sizeof (struct cl_optimization);
11595 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11597 return cl_target_option_eq (TREE_TARGET_OPTION (xt),
11598 TREE_TARGET_OPTION (yt));
11601 else
11602 gcc_unreachable ();
11604 return (memcmp (xp, yp, len) == 0);
11607 /* Build an OPTIMIZATION_NODE based on the options in OPTS. */
11609 tree
11610 build_optimization_node (struct gcc_options *opts)
11612 tree t;
11614 /* Use the cache of optimization nodes. */
11616 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11617 opts);
11619 tree *slot = cl_option_hash_table->find_slot (cl_optimization_node, INSERT);
11620 t = *slot;
11621 if (!t)
11623 /* Insert this one into the hash table. */
11624 t = cl_optimization_node;
11625 *slot = t;
11627 /* Make a new node for next time round. */
11628 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11631 return t;
11634 /* Build a TARGET_OPTION_NODE based on the options in OPTS. */
11636 tree
11637 build_target_option_node (struct gcc_options *opts)
11639 tree t;
11641 /* Use the cache of optimization nodes. */
11643 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11644 opts);
11646 tree *slot = cl_option_hash_table->find_slot (cl_target_option_node, INSERT);
11647 t = *slot;
11648 if (!t)
11650 /* Insert this one into the hash table. */
11651 t = cl_target_option_node;
11652 *slot = t;
11654 /* Make a new node for next time round. */
11655 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11658 return t;
11661 /* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
11662 so that they aren't saved during PCH writing. */
11664 void
11665 prepare_target_option_nodes_for_pch (void)
11667 hash_table<cl_option_hasher>::iterator iter = cl_option_hash_table->begin ();
11668 for (; iter != cl_option_hash_table->end (); ++iter)
11669 if (TREE_CODE (*iter) == TARGET_OPTION_NODE)
11670 TREE_TARGET_GLOBALS (*iter) = NULL;
11673 /* Determine the "ultimate origin" of a block. The block may be an inlined
11674 instance of an inlined instance of a block which is local to an inline
11675 function, so we have to trace all of the way back through the origin chain
11676 to find out what sort of node actually served as the original seed for the
11677 given block. */
11679 tree
11680 block_ultimate_origin (const_tree block)
11682 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11684 /* BLOCK_ABSTRACT_ORIGIN can point to itself; ignore that if
11685 we're trying to output the abstract instance of this function. */
11686 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11687 return NULL_TREE;
11689 if (immediate_origin == NULL_TREE)
11690 return NULL_TREE;
11691 else
11693 tree ret_val;
11694 tree lookahead = immediate_origin;
11698 ret_val = lookahead;
11699 lookahead = (TREE_CODE (ret_val) == BLOCK
11700 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11702 while (lookahead != NULL && lookahead != ret_val);
11704 /* The block's abstract origin chain may not be the *ultimate* origin of
11705 the block. It could lead to a DECL that has an abstract origin set.
11706 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11707 will give us if it has one). Note that DECL's abstract origins are
11708 supposed to be the most distant ancestor (or so decl_ultimate_origin
11709 claims), so we don't need to loop following the DECL origins. */
11710 if (DECL_P (ret_val))
11711 return DECL_ORIGIN (ret_val);
11713 return ret_val;
11717 /* Return true iff conversion from INNER_TYPE to OUTER_TYPE generates
11718 no instruction. */
11720 bool
11721 tree_nop_conversion_p (const_tree outer_type, const_tree inner_type)
11723 /* Use precision rather then machine mode when we can, which gives
11724 the correct answer even for submode (bit-field) types. */
11725 if ((INTEGRAL_TYPE_P (outer_type)
11726 || POINTER_TYPE_P (outer_type)
11727 || TREE_CODE (outer_type) == OFFSET_TYPE)
11728 && (INTEGRAL_TYPE_P (inner_type)
11729 || POINTER_TYPE_P (inner_type)
11730 || TREE_CODE (inner_type) == OFFSET_TYPE))
11731 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11733 /* Otherwise fall back on comparing machine modes (e.g. for
11734 aggregate types, floats). */
11735 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11738 /* Return true iff conversion in EXP generates no instruction. Mark
11739 it inline so that we fully inline into the stripping functions even
11740 though we have two uses of this function. */
11742 static inline bool
11743 tree_nop_conversion (const_tree exp)
11745 tree outer_type, inner_type;
11747 if (!CONVERT_EXPR_P (exp)
11748 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11749 return false;
11750 if (TREE_OPERAND (exp, 0) == error_mark_node)
11751 return false;
11753 outer_type = TREE_TYPE (exp);
11754 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11756 if (!inner_type)
11757 return false;
11759 return tree_nop_conversion_p (outer_type, inner_type);
11762 /* Return true iff conversion in EXP generates no instruction. Don't
11763 consider conversions changing the signedness. */
11765 static bool
11766 tree_sign_nop_conversion (const_tree exp)
11768 tree outer_type, inner_type;
11770 if (!tree_nop_conversion (exp))
11771 return false;
11773 outer_type = TREE_TYPE (exp);
11774 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11776 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11777 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11780 /* Strip conversions from EXP according to tree_nop_conversion and
11781 return the resulting expression. */
11783 tree
11784 tree_strip_nop_conversions (tree exp)
11786 while (tree_nop_conversion (exp))
11787 exp = TREE_OPERAND (exp, 0);
11788 return exp;
11791 /* Strip conversions from EXP according to tree_sign_nop_conversion
11792 and return the resulting expression. */
11794 tree
11795 tree_strip_sign_nop_conversions (tree exp)
11797 while (tree_sign_nop_conversion (exp))
11798 exp = TREE_OPERAND (exp, 0);
11799 return exp;
11802 /* Avoid any floating point extensions from EXP. */
11803 tree
11804 strip_float_extensions (tree exp)
11806 tree sub, expt, subt;
11808 /* For floating point constant look up the narrowest type that can hold
11809 it properly and handle it like (type)(narrowest_type)constant.
11810 This way we can optimize for instance a=a*2.0 where "a" is float
11811 but 2.0 is double constant. */
11812 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11814 REAL_VALUE_TYPE orig;
11815 tree type = NULL;
11817 orig = TREE_REAL_CST (exp);
11818 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11819 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11820 type = float_type_node;
11821 else if (TYPE_PRECISION (TREE_TYPE (exp))
11822 > TYPE_PRECISION (double_type_node)
11823 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11824 type = double_type_node;
11825 if (type)
11826 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11829 if (!CONVERT_EXPR_P (exp))
11830 return exp;
11832 sub = TREE_OPERAND (exp, 0);
11833 subt = TREE_TYPE (sub);
11834 expt = TREE_TYPE (exp);
11836 if (!FLOAT_TYPE_P (subt))
11837 return exp;
11839 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11840 return exp;
11842 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11843 return exp;
11845 return strip_float_extensions (sub);
11848 /* Strip out all handled components that produce invariant
11849 offsets. */
11851 const_tree
11852 strip_invariant_refs (const_tree op)
11854 while (handled_component_p (op))
11856 switch (TREE_CODE (op))
11858 case ARRAY_REF:
11859 case ARRAY_RANGE_REF:
11860 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11861 || TREE_OPERAND (op, 2) != NULL_TREE
11862 || TREE_OPERAND (op, 3) != NULL_TREE)
11863 return NULL;
11864 break;
11866 case COMPONENT_REF:
11867 if (TREE_OPERAND (op, 2) != NULL_TREE)
11868 return NULL;
11869 break;
11871 default:;
11873 op = TREE_OPERAND (op, 0);
11876 return op;
11879 static GTY(()) tree gcc_eh_personality_decl;
11881 /* Return the GCC personality function decl. */
11883 tree
11884 lhd_gcc_personality (void)
11886 if (!gcc_eh_personality_decl)
11887 gcc_eh_personality_decl = build_personality_function ("gcc");
11888 return gcc_eh_personality_decl;
11891 /* TARGET is a call target of GIMPLE call statement
11892 (obtained by gimple_call_fn). Return true if it is
11893 OBJ_TYPE_REF representing an virtual call of C++ method.
11894 (As opposed to OBJ_TYPE_REF representing objc calls
11895 through a cast where middle-end devirtualization machinery
11896 can't apply.) */
11898 bool
11899 virtual_method_call_p (tree target)
11901 if (TREE_CODE (target) != OBJ_TYPE_REF)
11902 return false;
11903 target = TREE_TYPE (target);
11904 gcc_checking_assert (TREE_CODE (target) == POINTER_TYPE);
11905 target = TREE_TYPE (target);
11906 if (TREE_CODE (target) == FUNCTION_TYPE)
11907 return false;
11908 gcc_checking_assert (TREE_CODE (target) == METHOD_TYPE);
11909 return true;
11912 /* REF is OBJ_TYPE_REF, return the class the ref corresponds to. */
11914 tree
11915 obj_type_ref_class (tree ref)
11917 gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF);
11918 ref = TREE_TYPE (ref);
11919 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
11920 ref = TREE_TYPE (ref);
11921 /* We look for type THIS points to. ObjC also builds
11922 OBJ_TYPE_REF with non-method calls, Their first parameter
11923 ID however also corresponds to class type. */
11924 gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE
11925 || TREE_CODE (ref) == FUNCTION_TYPE);
11926 ref = TREE_VALUE (TYPE_ARG_TYPES (ref));
11927 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
11928 return TREE_TYPE (ref);
11931 /* Return true if T is in anonymous namespace. */
11933 bool
11934 type_in_anonymous_namespace_p (const_tree t)
11936 /* TREE_PUBLIC of TYPE_STUB_DECL may not be properly set for
11937 bulitin types; those have CONTEXT NULL. */
11938 if (!TYPE_CONTEXT (t))
11939 return false;
11940 return (TYPE_STUB_DECL (t) && !TREE_PUBLIC (TYPE_STUB_DECL (t)));
11943 /* Try to find a base info of BINFO that would have its field decl at offset
11944 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11945 found, return, otherwise return NULL_TREE. */
11947 tree
11948 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
11950 tree type = BINFO_TYPE (binfo);
11952 while (true)
11954 HOST_WIDE_INT pos, size;
11955 tree fld;
11956 int i;
11958 if (types_same_for_odr (type, expected_type))
11959 return binfo;
11960 if (offset < 0)
11961 return NULL_TREE;
11963 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
11965 if (TREE_CODE (fld) != FIELD_DECL || !DECL_ARTIFICIAL (fld))
11966 continue;
11968 pos = int_bit_position (fld);
11969 size = tree_to_uhwi (DECL_SIZE (fld));
11970 if (pos <= offset && (pos + size) > offset)
11971 break;
11973 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11974 return NULL_TREE;
11976 /* Offset 0 indicates the primary base, whose vtable contents are
11977 represented in the binfo for the derived class. */
11978 else if (offset != 0)
11980 tree base_binfo, binfo2 = binfo;
11982 /* Find BINFO corresponding to FLD. This is bit harder
11983 by a fact that in virtual inheritance we may need to walk down
11984 the non-virtual inheritance chain. */
11985 while (true)
11987 tree containing_binfo = NULL, found_binfo = NULL;
11988 for (i = 0; BINFO_BASE_ITERATE (binfo2, i, base_binfo); i++)
11989 if (types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
11991 found_binfo = base_binfo;
11992 break;
11994 else
11995 if ((tree_to_shwi (BINFO_OFFSET (base_binfo))
11996 - tree_to_shwi (BINFO_OFFSET (binfo)))
11997 * BITS_PER_UNIT < pos
11998 /* Rule out types with no virtual methods or we can get confused
11999 here by zero sized bases. */
12000 && TYPE_BINFO (BINFO_TYPE (base_binfo))
12001 && BINFO_VTABLE (TYPE_BINFO (BINFO_TYPE (base_binfo)))
12002 && (!containing_binfo
12003 || (tree_to_shwi (BINFO_OFFSET (containing_binfo))
12004 < tree_to_shwi (BINFO_OFFSET (base_binfo)))))
12005 containing_binfo = base_binfo;
12006 if (found_binfo)
12008 binfo = found_binfo;
12009 break;
12011 if (!containing_binfo)
12012 return NULL_TREE;
12013 binfo2 = containing_binfo;
12017 type = TREE_TYPE (fld);
12018 offset -= pos;
12022 /* Returns true if X is a typedef decl. */
12024 bool
12025 is_typedef_decl (tree x)
12027 return (x && TREE_CODE (x) == TYPE_DECL
12028 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
12031 /* Returns true iff TYPE is a type variant created for a typedef. */
12033 bool
12034 typedef_variant_p (tree type)
12036 return is_typedef_decl (TYPE_NAME (type));
12039 /* Warn about a use of an identifier which was marked deprecated. */
12040 void
12041 warn_deprecated_use (tree node, tree attr)
12043 const char *msg;
12045 if (node == 0 || !warn_deprecated_decl)
12046 return;
12048 if (!attr)
12050 if (DECL_P (node))
12051 attr = DECL_ATTRIBUTES (node);
12052 else if (TYPE_P (node))
12054 tree decl = TYPE_STUB_DECL (node);
12055 if (decl)
12056 attr = lookup_attribute ("deprecated",
12057 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
12061 if (attr)
12062 attr = lookup_attribute ("deprecated", attr);
12064 if (attr)
12065 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
12066 else
12067 msg = NULL;
12069 bool w;
12070 if (DECL_P (node))
12072 if (msg)
12073 w = warning (OPT_Wdeprecated_declarations,
12074 "%qD is deprecated: %s", node, msg);
12075 else
12076 w = warning (OPT_Wdeprecated_declarations,
12077 "%qD is deprecated", node);
12078 if (w)
12079 inform (DECL_SOURCE_LOCATION (node), "declared here");
12081 else if (TYPE_P (node))
12083 tree what = NULL_TREE;
12084 tree decl = TYPE_STUB_DECL (node);
12086 if (TYPE_NAME (node))
12088 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12089 what = TYPE_NAME (node);
12090 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12091 && DECL_NAME (TYPE_NAME (node)))
12092 what = DECL_NAME (TYPE_NAME (node));
12095 if (decl)
12097 if (what)
12099 if (msg)
12100 w = warning (OPT_Wdeprecated_declarations,
12101 "%qE is deprecated: %s", what, msg);
12102 else
12103 w = warning (OPT_Wdeprecated_declarations,
12104 "%qE is deprecated", what);
12106 else
12108 if (msg)
12109 w = warning (OPT_Wdeprecated_declarations,
12110 "type is deprecated: %s", msg);
12111 else
12112 w = warning (OPT_Wdeprecated_declarations,
12113 "type is deprecated");
12115 if (w)
12116 inform (DECL_SOURCE_LOCATION (decl), "declared here");
12118 else
12120 if (what)
12122 if (msg)
12123 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
12124 what, msg);
12125 else
12126 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
12128 else
12130 if (msg)
12131 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
12132 msg);
12133 else
12134 warning (OPT_Wdeprecated_declarations, "type is deprecated");
12140 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12141 somewhere in it. */
12143 bool
12144 contains_bitfld_component_ref_p (const_tree ref)
12146 while (handled_component_p (ref))
12148 if (TREE_CODE (ref) == COMPONENT_REF
12149 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12150 return true;
12151 ref = TREE_OPERAND (ref, 0);
12154 return false;
12157 /* Try to determine whether a TRY_CATCH expression can fall through.
12158 This is a subroutine of block_may_fallthru. */
12160 static bool
12161 try_catch_may_fallthru (const_tree stmt)
12163 tree_stmt_iterator i;
12165 /* If the TRY block can fall through, the whole TRY_CATCH can
12166 fall through. */
12167 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
12168 return true;
12170 i = tsi_start (TREE_OPERAND (stmt, 1));
12171 switch (TREE_CODE (tsi_stmt (i)))
12173 case CATCH_EXPR:
12174 /* We expect to see a sequence of CATCH_EXPR trees, each with a
12175 catch expression and a body. The whole TRY_CATCH may fall
12176 through iff any of the catch bodies falls through. */
12177 for (; !tsi_end_p (i); tsi_next (&i))
12179 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
12180 return true;
12182 return false;
12184 case EH_FILTER_EXPR:
12185 /* The exception filter expression only matters if there is an
12186 exception. If the exception does not match EH_FILTER_TYPES,
12187 we will execute EH_FILTER_FAILURE, and we will fall through
12188 if that falls through. If the exception does match
12189 EH_FILTER_TYPES, the stack unwinder will continue up the
12190 stack, so we will not fall through. We don't know whether we
12191 will throw an exception which matches EH_FILTER_TYPES or not,
12192 so we just ignore EH_FILTER_TYPES and assume that we might
12193 throw an exception which doesn't match. */
12194 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
12196 default:
12197 /* This case represents statements to be executed when an
12198 exception occurs. Those statements are implicitly followed
12199 by a RESX statement to resume execution after the exception.
12200 So in this case the TRY_CATCH never falls through. */
12201 return false;
12205 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
12206 need not be 100% accurate; simply be conservative and return true if we
12207 don't know. This is used only to avoid stupidly generating extra code.
12208 If we're wrong, we'll just delete the extra code later. */
12210 bool
12211 block_may_fallthru (const_tree block)
12213 /* This CONST_CAST is okay because expr_last returns its argument
12214 unmodified and we assign it to a const_tree. */
12215 const_tree stmt = expr_last (CONST_CAST_TREE (block));
12217 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
12219 case GOTO_EXPR:
12220 case RETURN_EXPR:
12221 /* Easy cases. If the last statement of the block implies
12222 control transfer, then we can't fall through. */
12223 return false;
12225 case SWITCH_EXPR:
12226 /* If SWITCH_LABELS is set, this is lowered, and represents a
12227 branch to a selected label and hence can not fall through.
12228 Otherwise SWITCH_BODY is set, and the switch can fall
12229 through. */
12230 return SWITCH_LABELS (stmt) == NULL_TREE;
12232 case COND_EXPR:
12233 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
12234 return true;
12235 return block_may_fallthru (COND_EXPR_ELSE (stmt));
12237 case BIND_EXPR:
12238 return block_may_fallthru (BIND_EXPR_BODY (stmt));
12240 case TRY_CATCH_EXPR:
12241 return try_catch_may_fallthru (stmt);
12243 case TRY_FINALLY_EXPR:
12244 /* The finally clause is always executed after the try clause,
12245 so if it does not fall through, then the try-finally will not
12246 fall through. Otherwise, if the try clause does not fall
12247 through, then when the finally clause falls through it will
12248 resume execution wherever the try clause was going. So the
12249 whole try-finally will only fall through if both the try
12250 clause and the finally clause fall through. */
12251 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
12252 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
12254 case MODIFY_EXPR:
12255 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
12256 stmt = TREE_OPERAND (stmt, 1);
12257 else
12258 return true;
12259 /* FALLTHRU */
12261 case CALL_EXPR:
12262 /* Functions that do not return do not fall through. */
12263 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
12265 case CLEANUP_POINT_EXPR:
12266 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12268 case TARGET_EXPR:
12269 return block_may_fallthru (TREE_OPERAND (stmt, 1));
12271 case ERROR_MARK:
12272 return true;
12274 default:
12275 return lang_hooks.block_may_fallthru (stmt);
12279 /* True if we are using EH to handle cleanups. */
12280 static bool using_eh_for_cleanups_flag = false;
12282 /* This routine is called from front ends to indicate eh should be used for
12283 cleanups. */
12284 void
12285 using_eh_for_cleanups (void)
12287 using_eh_for_cleanups_flag = true;
12290 /* Query whether EH is used for cleanups. */
12291 bool
12292 using_eh_for_cleanups_p (void)
12294 return using_eh_for_cleanups_flag;
12297 /* Wrapper for tree_code_name to ensure that tree code is valid */
12298 const char *
12299 get_tree_code_name (enum tree_code code)
12301 const char *invalid = "<invalid tree code>";
12303 if (code >= MAX_TREE_CODES)
12304 return invalid;
12306 return tree_code_name[code];
12309 /* Drops the TREE_OVERFLOW flag from T. */
12311 tree
12312 drop_tree_overflow (tree t)
12314 gcc_checking_assert (TREE_OVERFLOW (t));
12316 /* For tree codes with a sharing machinery re-build the result. */
12317 if (TREE_CODE (t) == INTEGER_CST)
12318 return wide_int_to_tree (TREE_TYPE (t), t);
12320 /* Otherwise, as all tcc_constants are possibly shared, copy the node
12321 and drop the flag. */
12322 t = copy_node (t);
12323 TREE_OVERFLOW (t) = 0;
12324 return t;
12327 /* Given a memory reference expression T, return its base address.
12328 The base address of a memory reference expression is the main
12329 object being referenced. For instance, the base address for
12330 'array[i].fld[j]' is 'array'. You can think of this as stripping
12331 away the offset part from a memory address.
12333 This function calls handled_component_p to strip away all the inner
12334 parts of the memory reference until it reaches the base object. */
12336 tree
12337 get_base_address (tree t)
12339 while (handled_component_p (t))
12340 t = TREE_OPERAND (t, 0);
12342 if ((TREE_CODE (t) == MEM_REF
12343 || TREE_CODE (t) == TARGET_MEM_REF)
12344 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
12345 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
12347 /* ??? Either the alias oracle or all callers need to properly deal
12348 with WITH_SIZE_EXPRs before we can look through those. */
12349 if (TREE_CODE (t) == WITH_SIZE_EXPR)
12350 return NULL_TREE;
12352 return t;
12355 /* Return the machine mode of T. For vectors, returns the mode of the
12356 inner type. The main use case is to feed the result to HONOR_NANS,
12357 avoiding the BLKmode that a direct TYPE_MODE (T) might return. */
12359 machine_mode
12360 element_mode (const_tree t)
12362 if (!TYPE_P (t))
12363 t = TREE_TYPE (t);
12364 if (VECTOR_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
12365 t = TREE_TYPE (t);
12366 return TYPE_MODE (t);
12369 #include "gt-tree.h"