2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / tree.c
blobbe01d0e19c96226578ff299bc3cbddee07c06d4a
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2015 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 can occasionally
28 calls language-dependent routines. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "flags.h"
35 #include "input.h"
36 #include "alias.h"
37 #include "symtab.h"
38 #include "tree.h"
39 #include "fold-const.h"
40 #include "stor-layout.h"
41 #include "calls.h"
42 #include "attribs.h"
43 #include "varasm.h"
44 #include "tm_p.h"
45 #include "hard-reg-set.h"
46 #include "function.h"
47 #include "obstack.h"
48 #include "toplev.h" /* get_random_seed */
49 #include "filenames.h"
50 #include "output.h"
51 #include "target.h"
52 #include "common/common-target.h"
53 #include "langhooks.h"
54 #include "tree-inline.h"
55 #include "tree-iterator.h"
56 #include "predict.h"
57 #include "dominance.h"
58 #include "cfg.h"
59 #include "basic-block.h"
60 #include "bitmap.h"
61 #include "tree-ssa-alias.h"
62 #include "internal-fn.h"
63 #include "gimple-expr.h"
64 #include "is-a.h"
65 #include "gimple.h"
66 #include "gimple-iterator.h"
67 #include "gimplify.h"
68 #include "gimple-ssa.h"
69 #include "plugin-api.h"
70 #include "ipa-ref.h"
71 #include "cgraph.h"
72 #include "tree-phinodes.h"
73 #include "stringpool.h"
74 #include "tree-ssanames.h"
75 #include "rtl.h"
76 #include "insn-config.h"
77 #include "expmed.h"
78 #include "dojump.h"
79 #include "explow.h"
80 #include "emit-rtl.h"
81 #include "stmt.h"
82 #include "expr.h"
83 #include "tree-dfa.h"
84 #include "params.h"
85 #include "tree-pass.h"
86 #include "langhooks-def.h"
87 #include "diagnostic.h"
88 #include "tree-diagnostic.h"
89 #include "tree-pretty-print.h"
90 #include "except.h"
91 #include "debug.h"
92 #include "intl.h"
93 #include "builtins.h"
94 #include "print-tree.h"
95 #include "ipa-utils.h"
97 /* Tree code classes. */
99 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
100 #define END_OF_BASE_TREE_CODES tcc_exceptional,
102 const enum tree_code_class tree_code_type[] = {
103 #include "all-tree.def"
106 #undef DEFTREECODE
107 #undef END_OF_BASE_TREE_CODES
109 /* Table indexed by tree code giving number of expression
110 operands beyond the fixed part of the node structure.
111 Not used for types or decls. */
113 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
114 #define END_OF_BASE_TREE_CODES 0,
116 const unsigned char tree_code_length[] = {
117 #include "all-tree.def"
120 #undef DEFTREECODE
121 #undef END_OF_BASE_TREE_CODES
123 /* Names of tree components.
124 Used for printing out the tree and error messages. */
125 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
126 #define END_OF_BASE_TREE_CODES "@dummy",
128 static const char *const tree_code_name[] = {
129 #include "all-tree.def"
132 #undef DEFTREECODE
133 #undef END_OF_BASE_TREE_CODES
135 /* Each tree code class has an associated string representation.
136 These must correspond to the tree_code_class entries. */
138 const char *const tree_code_class_strings[] =
140 "exceptional",
141 "constant",
142 "type",
143 "declaration",
144 "reference",
145 "comparison",
146 "unary",
147 "binary",
148 "statement",
149 "vl_exp",
150 "expression"
153 /* obstack.[ch] explicitly declined to prototype this. */
154 extern int _obstack_allocated_p (struct obstack *h, void *obj);
156 /* Statistics-gathering stuff. */
158 static int tree_code_counts[MAX_TREE_CODES];
159 int tree_node_counts[(int) all_kinds];
160 int tree_node_sizes[(int) all_kinds];
162 /* Keep in sync with tree.h:enum tree_node_kind. */
163 static const char * const tree_node_kind_names[] = {
164 "decls",
165 "types",
166 "blocks",
167 "stmts",
168 "refs",
169 "exprs",
170 "constants",
171 "identifiers",
172 "vecs",
173 "binfos",
174 "ssa names",
175 "constructors",
176 "random kinds",
177 "lang_decl kinds",
178 "lang_type kinds",
179 "omp clauses",
182 /* Unique id for next decl created. */
183 static GTY(()) int next_decl_uid;
184 /* Unique id for next type created. */
185 static GTY(()) int next_type_uid = 1;
186 /* Unique id for next debug decl created. Use negative numbers,
187 to catch erroneous uses. */
188 static GTY(()) int next_debug_decl_uid;
190 /* Since we cannot rehash a type after it is in the table, we have to
191 keep the hash code. */
193 struct GTY((for_user)) type_hash {
194 unsigned long hash;
195 tree type;
198 /* Initial size of the hash table (rounded to next prime). */
199 #define TYPE_HASH_INITIAL_SIZE 1000
201 struct type_cache_hasher : ggc_cache_hasher<type_hash *>
203 static hashval_t hash (type_hash *t) { return t->hash; }
204 static bool equal (type_hash *a, type_hash *b);
206 static void
207 handle_cache_entry (type_hash *&t)
209 extern void gt_ggc_mx (type_hash *&);
210 if (t == HTAB_DELETED_ENTRY || t == HTAB_EMPTY_ENTRY)
211 return;
212 else if (ggc_marked_p (t->type))
213 gt_ggc_mx (t);
214 else
215 t = static_cast<type_hash *> (HTAB_DELETED_ENTRY);
219 /* Now here is the hash table. When recording a type, it is added to
220 the slot whose index is the hash code. Note that the hash table is
221 used for several kinds of types (function types, array types and
222 array index range types, for now). While all these live in the
223 same table, they are completely independent, and the hash code is
224 computed differently for each of these. */
226 static GTY ((cache)) hash_table<type_cache_hasher> *type_hash_table;
228 /* Hash table and temporary node for larger integer const values. */
229 static GTY (()) tree int_cst_node;
231 struct int_cst_hasher : ggc_cache_hasher<tree>
233 static hashval_t hash (tree t);
234 static bool equal (tree x, tree y);
237 static GTY ((cache)) hash_table<int_cst_hasher> *int_cst_hash_table;
239 /* Hash table for optimization flags and target option flags. Use the same
240 hash table for both sets of options. Nodes for building the current
241 optimization and target option nodes. The assumption is most of the time
242 the options created will already be in the hash table, so we avoid
243 allocating and freeing up a node repeatably. */
244 static GTY (()) tree cl_optimization_node;
245 static GTY (()) tree cl_target_option_node;
247 struct cl_option_hasher : ggc_cache_hasher<tree>
249 static hashval_t hash (tree t);
250 static bool equal (tree x, tree y);
253 static GTY ((cache)) hash_table<cl_option_hasher> *cl_option_hash_table;
255 /* General tree->tree mapping structure for use in hash tables. */
258 static GTY ((cache))
259 hash_table<tree_decl_map_cache_hasher> *debug_expr_for_decl;
261 static GTY ((cache))
262 hash_table<tree_decl_map_cache_hasher> *value_expr_for_decl;
264 struct tree_vec_map_cache_hasher : ggc_cache_hasher<tree_vec_map *>
266 static hashval_t hash (tree_vec_map *m) { return DECL_UID (m->base.from); }
268 static bool
269 equal (tree_vec_map *a, tree_vec_map *b)
271 return a->base.from == b->base.from;
274 static void
275 handle_cache_entry (tree_vec_map *&m)
277 extern void gt_ggc_mx (tree_vec_map *&);
278 if (m == HTAB_EMPTY_ENTRY || m == HTAB_DELETED_ENTRY)
279 return;
280 else if (ggc_marked_p (m->base.from))
281 gt_ggc_mx (m);
282 else
283 m = static_cast<tree_vec_map *> (HTAB_DELETED_ENTRY);
287 static GTY ((cache))
288 hash_table<tree_vec_map_cache_hasher> *debug_args_for_decl;
290 static void set_type_quals (tree, int);
291 static void print_type_hash_statistics (void);
292 static void print_debug_expr_statistics (void);
293 static void print_value_expr_statistics (void);
294 static void type_hash_list (const_tree, inchash::hash &);
295 static void attribute_hash_list (const_tree, inchash::hash &);
297 tree global_trees[TI_MAX];
298 tree integer_types[itk_none];
300 bool int_n_enabled_p[NUM_INT_N_ENTS];
301 struct int_n_trees_t int_n_trees [NUM_INT_N_ENTS];
303 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
305 /* Number of operands for each OpenMP clause. */
306 unsigned const char omp_clause_num_ops[] =
308 0, /* OMP_CLAUSE_ERROR */
309 1, /* OMP_CLAUSE_PRIVATE */
310 1, /* OMP_CLAUSE_SHARED */
311 1, /* OMP_CLAUSE_FIRSTPRIVATE */
312 2, /* OMP_CLAUSE_LASTPRIVATE */
313 4, /* OMP_CLAUSE_REDUCTION */
314 1, /* OMP_CLAUSE_COPYIN */
315 1, /* OMP_CLAUSE_COPYPRIVATE */
316 3, /* OMP_CLAUSE_LINEAR */
317 2, /* OMP_CLAUSE_ALIGNED */
318 1, /* OMP_CLAUSE_DEPEND */
319 1, /* OMP_CLAUSE_UNIFORM */
320 2, /* OMP_CLAUSE_FROM */
321 2, /* OMP_CLAUSE_TO */
322 2, /* OMP_CLAUSE_MAP */
323 2, /* OMP_CLAUSE__CACHE_ */
324 1, /* OMP_CLAUSE_DEVICE_RESIDENT */
325 1, /* OMP_CLAUSE_USE_DEVICE */
326 2, /* OMP_CLAUSE_GANG */
327 1, /* OMP_CLAUSE_ASYNC */
328 1, /* OMP_CLAUSE_WAIT */
329 0, /* OMP_CLAUSE_AUTO */
330 0, /* OMP_CLAUSE_SEQ */
331 1, /* OMP_CLAUSE__LOOPTEMP_ */
332 1, /* OMP_CLAUSE_IF */
333 1, /* OMP_CLAUSE_NUM_THREADS */
334 1, /* OMP_CLAUSE_SCHEDULE */
335 0, /* OMP_CLAUSE_NOWAIT */
336 0, /* OMP_CLAUSE_ORDERED */
337 0, /* OMP_CLAUSE_DEFAULT */
338 3, /* OMP_CLAUSE_COLLAPSE */
339 0, /* OMP_CLAUSE_UNTIED */
340 1, /* OMP_CLAUSE_FINAL */
341 0, /* OMP_CLAUSE_MERGEABLE */
342 1, /* OMP_CLAUSE_DEVICE */
343 1, /* OMP_CLAUSE_DIST_SCHEDULE */
344 0, /* OMP_CLAUSE_INBRANCH */
345 0, /* OMP_CLAUSE_NOTINBRANCH */
346 1, /* OMP_CLAUSE_NUM_TEAMS */
347 1, /* OMP_CLAUSE_THREAD_LIMIT */
348 0, /* OMP_CLAUSE_PROC_BIND */
349 1, /* OMP_CLAUSE_SAFELEN */
350 1, /* OMP_CLAUSE_SIMDLEN */
351 0, /* OMP_CLAUSE_FOR */
352 0, /* OMP_CLAUSE_PARALLEL */
353 0, /* OMP_CLAUSE_SECTIONS */
354 0, /* OMP_CLAUSE_TASKGROUP */
355 1, /* OMP_CLAUSE__SIMDUID_ */
356 1, /* OMP_CLAUSE__CILK_FOR_COUNT_ */
357 0, /* OMP_CLAUSE_INDEPENDENT */
358 1, /* OMP_CLAUSE_WORKER */
359 1, /* OMP_CLAUSE_VECTOR */
360 1, /* OMP_CLAUSE_NUM_GANGS */
361 1, /* OMP_CLAUSE_NUM_WORKERS */
362 1, /* OMP_CLAUSE_VECTOR_LENGTH */
365 const char * const omp_clause_code_name[] =
367 "error_clause",
368 "private",
369 "shared",
370 "firstprivate",
371 "lastprivate",
372 "reduction",
373 "copyin",
374 "copyprivate",
375 "linear",
376 "aligned",
377 "depend",
378 "uniform",
379 "from",
380 "to",
381 "map",
382 "_cache_",
383 "device_resident",
384 "use_device",
385 "gang",
386 "async",
387 "wait",
388 "auto",
389 "seq",
390 "_looptemp_",
391 "if",
392 "num_threads",
393 "schedule",
394 "nowait",
395 "ordered",
396 "default",
397 "collapse",
398 "untied",
399 "final",
400 "mergeable",
401 "device",
402 "dist_schedule",
403 "inbranch",
404 "notinbranch",
405 "num_teams",
406 "thread_limit",
407 "proc_bind",
408 "safelen",
409 "simdlen",
410 "for",
411 "parallel",
412 "sections",
413 "taskgroup",
414 "_simduid_",
415 "_Cilk_for_count_",
416 "independent",
417 "worker",
418 "vector",
419 "num_gangs",
420 "num_workers",
421 "vector_length"
425 /* Return the tree node structure used by tree code CODE. */
427 static inline enum tree_node_structure_enum
428 tree_node_structure_for_code (enum tree_code code)
430 switch (TREE_CODE_CLASS (code))
432 case tcc_declaration:
434 switch (code)
436 case FIELD_DECL:
437 return TS_FIELD_DECL;
438 case PARM_DECL:
439 return TS_PARM_DECL;
440 case VAR_DECL:
441 return TS_VAR_DECL;
442 case LABEL_DECL:
443 return TS_LABEL_DECL;
444 case RESULT_DECL:
445 return TS_RESULT_DECL;
446 case DEBUG_EXPR_DECL:
447 return TS_DECL_WRTL;
448 case CONST_DECL:
449 return TS_CONST_DECL;
450 case TYPE_DECL:
451 return TS_TYPE_DECL;
452 case FUNCTION_DECL:
453 return TS_FUNCTION_DECL;
454 case TRANSLATION_UNIT_DECL:
455 return TS_TRANSLATION_UNIT_DECL;
456 default:
457 return TS_DECL_NON_COMMON;
460 case tcc_type:
461 return TS_TYPE_NON_COMMON;
462 case tcc_reference:
463 case tcc_comparison:
464 case tcc_unary:
465 case tcc_binary:
466 case tcc_expression:
467 case tcc_statement:
468 case tcc_vl_exp:
469 return TS_EXP;
470 default: /* tcc_constant and tcc_exceptional */
471 break;
473 switch (code)
475 /* tcc_constant cases. */
476 case VOID_CST: return TS_TYPED;
477 case INTEGER_CST: return TS_INT_CST;
478 case REAL_CST: return TS_REAL_CST;
479 case FIXED_CST: return TS_FIXED_CST;
480 case COMPLEX_CST: return TS_COMPLEX;
481 case VECTOR_CST: return TS_VECTOR;
482 case STRING_CST: return TS_STRING;
483 /* tcc_exceptional cases. */
484 case ERROR_MARK: return TS_COMMON;
485 case IDENTIFIER_NODE: return TS_IDENTIFIER;
486 case TREE_LIST: return TS_LIST;
487 case TREE_VEC: return TS_VEC;
488 case SSA_NAME: return TS_SSA_NAME;
489 case PLACEHOLDER_EXPR: return TS_COMMON;
490 case STATEMENT_LIST: return TS_STATEMENT_LIST;
491 case BLOCK: return TS_BLOCK;
492 case CONSTRUCTOR: return TS_CONSTRUCTOR;
493 case TREE_BINFO: return TS_BINFO;
494 case OMP_CLAUSE: return TS_OMP_CLAUSE;
495 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
496 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
498 default:
499 gcc_unreachable ();
504 /* Initialize tree_contains_struct to describe the hierarchy of tree
505 nodes. */
507 static void
508 initialize_tree_contains_struct (void)
510 unsigned i;
512 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
514 enum tree_code code;
515 enum tree_node_structure_enum ts_code;
517 code = (enum tree_code) i;
518 ts_code = tree_node_structure_for_code (code);
520 /* Mark the TS structure itself. */
521 tree_contains_struct[code][ts_code] = 1;
523 /* Mark all the structures that TS is derived from. */
524 switch (ts_code)
526 case TS_TYPED:
527 case TS_BLOCK:
528 MARK_TS_BASE (code);
529 break;
531 case TS_COMMON:
532 case TS_INT_CST:
533 case TS_REAL_CST:
534 case TS_FIXED_CST:
535 case TS_VECTOR:
536 case TS_STRING:
537 case TS_COMPLEX:
538 case TS_SSA_NAME:
539 case TS_CONSTRUCTOR:
540 case TS_EXP:
541 case TS_STATEMENT_LIST:
542 MARK_TS_TYPED (code);
543 break;
545 case TS_IDENTIFIER:
546 case TS_DECL_MINIMAL:
547 case TS_TYPE_COMMON:
548 case TS_LIST:
549 case TS_VEC:
550 case TS_BINFO:
551 case TS_OMP_CLAUSE:
552 case TS_OPTIMIZATION:
553 case TS_TARGET_OPTION:
554 MARK_TS_COMMON (code);
555 break;
557 case TS_TYPE_WITH_LANG_SPECIFIC:
558 MARK_TS_TYPE_COMMON (code);
559 break;
561 case TS_TYPE_NON_COMMON:
562 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
563 break;
565 case TS_DECL_COMMON:
566 MARK_TS_DECL_MINIMAL (code);
567 break;
569 case TS_DECL_WRTL:
570 case TS_CONST_DECL:
571 MARK_TS_DECL_COMMON (code);
572 break;
574 case TS_DECL_NON_COMMON:
575 MARK_TS_DECL_WITH_VIS (code);
576 break;
578 case TS_DECL_WITH_VIS:
579 case TS_PARM_DECL:
580 case TS_LABEL_DECL:
581 case TS_RESULT_DECL:
582 MARK_TS_DECL_WRTL (code);
583 break;
585 case TS_FIELD_DECL:
586 MARK_TS_DECL_COMMON (code);
587 break;
589 case TS_VAR_DECL:
590 MARK_TS_DECL_WITH_VIS (code);
591 break;
593 case TS_TYPE_DECL:
594 case TS_FUNCTION_DECL:
595 MARK_TS_DECL_NON_COMMON (code);
596 break;
598 case TS_TRANSLATION_UNIT_DECL:
599 MARK_TS_DECL_COMMON (code);
600 break;
602 default:
603 gcc_unreachable ();
607 /* Basic consistency checks for attributes used in fold. */
608 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
609 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
610 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
611 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
612 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
613 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
614 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
615 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
616 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
617 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
618 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
619 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
620 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
621 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
622 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
623 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
624 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
625 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
626 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
627 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
628 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
629 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
630 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
631 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
632 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
633 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
634 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
635 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
636 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
637 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
638 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
639 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
640 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
641 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
642 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
643 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
644 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
645 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
646 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_MINIMAL]);
647 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_COMMON]);
651 /* Init tree.c. */
653 void
654 init_ttree (void)
656 /* Initialize the hash table of types. */
657 type_hash_table
658 = hash_table<type_cache_hasher>::create_ggc (TYPE_HASH_INITIAL_SIZE);
660 debug_expr_for_decl
661 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
663 value_expr_for_decl
664 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
666 int_cst_hash_table = hash_table<int_cst_hasher>::create_ggc (1024);
668 int_cst_node = make_int_cst (1, 1);
670 cl_option_hash_table = hash_table<cl_option_hasher>::create_ggc (64);
672 cl_optimization_node = make_node (OPTIMIZATION_NODE);
673 cl_target_option_node = make_node (TARGET_OPTION_NODE);
675 /* Initialize the tree_contains_struct array. */
676 initialize_tree_contains_struct ();
677 lang_hooks.init_ts ();
681 /* The name of the object as the assembler will see it (but before any
682 translations made by ASM_OUTPUT_LABELREF). Often this is the same
683 as DECL_NAME. It is an IDENTIFIER_NODE. */
684 tree
685 decl_assembler_name (tree decl)
687 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
688 lang_hooks.set_decl_assembler_name (decl);
689 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
692 /* When the target supports COMDAT groups, this indicates which group the
693 DECL is associated with. This can be either an IDENTIFIER_NODE or a
694 decl, in which case its DECL_ASSEMBLER_NAME identifies the group. */
695 tree
696 decl_comdat_group (const_tree node)
698 struct symtab_node *snode = symtab_node::get (node);
699 if (!snode)
700 return NULL;
701 return snode->get_comdat_group ();
704 /* Likewise, but make sure it's been reduced to an IDENTIFIER_NODE. */
705 tree
706 decl_comdat_group_id (const_tree node)
708 struct symtab_node *snode = symtab_node::get (node);
709 if (!snode)
710 return NULL;
711 return snode->get_comdat_group_id ();
714 /* When the target supports named section, return its name as IDENTIFIER_NODE
715 or NULL if it is in no section. */
716 const char *
717 decl_section_name (const_tree node)
719 struct symtab_node *snode = symtab_node::get (node);
720 if (!snode)
721 return NULL;
722 return snode->get_section ();
725 /* Set section section name of NODE to VALUE (that is expected to
726 be identifier node) */
727 void
728 set_decl_section_name (tree node, const char *value)
730 struct symtab_node *snode;
732 if (value == NULL)
734 snode = symtab_node::get (node);
735 if (!snode)
736 return;
738 else if (TREE_CODE (node) == VAR_DECL)
739 snode = varpool_node::get_create (node);
740 else
741 snode = cgraph_node::get_create (node);
742 snode->set_section (value);
745 /* Return TLS model of a variable NODE. */
746 enum tls_model
747 decl_tls_model (const_tree node)
749 struct varpool_node *snode = varpool_node::get (node);
750 if (!snode)
751 return TLS_MODEL_NONE;
752 return snode->tls_model;
755 /* Set TLS model of variable NODE to MODEL. */
756 void
757 set_decl_tls_model (tree node, enum tls_model model)
759 struct varpool_node *vnode;
761 if (model == TLS_MODEL_NONE)
763 vnode = varpool_node::get (node);
764 if (!vnode)
765 return;
767 else
768 vnode = varpool_node::get_create (node);
769 vnode->tls_model = model;
772 /* Compute the number of bytes occupied by a tree with code CODE.
773 This function cannot be used for nodes that have variable sizes,
774 including TREE_VEC, INTEGER_CST, STRING_CST, and CALL_EXPR. */
775 size_t
776 tree_code_size (enum tree_code code)
778 switch (TREE_CODE_CLASS (code))
780 case tcc_declaration: /* A decl node */
782 switch (code)
784 case FIELD_DECL:
785 return sizeof (struct tree_field_decl);
786 case PARM_DECL:
787 return sizeof (struct tree_parm_decl);
788 case VAR_DECL:
789 return sizeof (struct tree_var_decl);
790 case LABEL_DECL:
791 return sizeof (struct tree_label_decl);
792 case RESULT_DECL:
793 return sizeof (struct tree_result_decl);
794 case CONST_DECL:
795 return sizeof (struct tree_const_decl);
796 case TYPE_DECL:
797 return sizeof (struct tree_type_decl);
798 case FUNCTION_DECL:
799 return sizeof (struct tree_function_decl);
800 case DEBUG_EXPR_DECL:
801 return sizeof (struct tree_decl_with_rtl);
802 case TRANSLATION_UNIT_DECL:
803 return sizeof (struct tree_translation_unit_decl);
804 case NAMESPACE_DECL:
805 case IMPORTED_DECL:
806 case NAMELIST_DECL:
807 return sizeof (struct tree_decl_non_common);
808 default:
809 return lang_hooks.tree_size (code);
813 case tcc_type: /* a type node */
814 return sizeof (struct tree_type_non_common);
816 case tcc_reference: /* a reference */
817 case tcc_expression: /* an expression */
818 case tcc_statement: /* an expression with side effects */
819 case tcc_comparison: /* a comparison expression */
820 case tcc_unary: /* a unary arithmetic expression */
821 case tcc_binary: /* a binary arithmetic expression */
822 return (sizeof (struct tree_exp)
823 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
825 case tcc_constant: /* a constant */
826 switch (code)
828 case VOID_CST: return sizeof (struct tree_typed);
829 case INTEGER_CST: gcc_unreachable ();
830 case REAL_CST: return sizeof (struct tree_real_cst);
831 case FIXED_CST: return sizeof (struct tree_fixed_cst);
832 case COMPLEX_CST: return sizeof (struct tree_complex);
833 case VECTOR_CST: return sizeof (struct tree_vector);
834 case STRING_CST: gcc_unreachable ();
835 default:
836 return lang_hooks.tree_size (code);
839 case tcc_exceptional: /* something random, like an identifier. */
840 switch (code)
842 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
843 case TREE_LIST: return sizeof (struct tree_list);
845 case ERROR_MARK:
846 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
848 case TREE_VEC:
849 case OMP_CLAUSE: gcc_unreachable ();
851 case SSA_NAME: return sizeof (struct tree_ssa_name);
853 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
854 case BLOCK: return sizeof (struct tree_block);
855 case CONSTRUCTOR: return sizeof (struct tree_constructor);
856 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
857 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
859 default:
860 return lang_hooks.tree_size (code);
863 default:
864 gcc_unreachable ();
868 /* Compute the number of bytes occupied by NODE. This routine only
869 looks at TREE_CODE, except for those nodes that have variable sizes. */
870 size_t
871 tree_size (const_tree node)
873 const enum tree_code code = TREE_CODE (node);
874 switch (code)
876 case INTEGER_CST:
877 return (sizeof (struct tree_int_cst)
878 + (TREE_INT_CST_EXT_NUNITS (node) - 1) * sizeof (HOST_WIDE_INT));
880 case TREE_BINFO:
881 return (offsetof (struct tree_binfo, base_binfos)
882 + vec<tree, va_gc>
883 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
885 case TREE_VEC:
886 return (sizeof (struct tree_vec)
887 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
889 case VECTOR_CST:
890 return (sizeof (struct tree_vector)
891 + (TYPE_VECTOR_SUBPARTS (TREE_TYPE (node)) - 1) * sizeof (tree));
893 case STRING_CST:
894 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
896 case OMP_CLAUSE:
897 return (sizeof (struct tree_omp_clause)
898 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
899 * sizeof (tree));
901 default:
902 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
903 return (sizeof (struct tree_exp)
904 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
905 else
906 return tree_code_size (code);
910 /* Record interesting allocation statistics for a tree node with CODE
911 and LENGTH. */
913 static void
914 record_node_allocation_statistics (enum tree_code code ATTRIBUTE_UNUSED,
915 size_t length ATTRIBUTE_UNUSED)
917 enum tree_code_class type = TREE_CODE_CLASS (code);
918 tree_node_kind kind;
920 if (!GATHER_STATISTICS)
921 return;
923 switch (type)
925 case tcc_declaration: /* A decl node */
926 kind = d_kind;
927 break;
929 case tcc_type: /* a type node */
930 kind = t_kind;
931 break;
933 case tcc_statement: /* an expression with side effects */
934 kind = s_kind;
935 break;
937 case tcc_reference: /* a reference */
938 kind = r_kind;
939 break;
941 case tcc_expression: /* an expression */
942 case tcc_comparison: /* a comparison expression */
943 case tcc_unary: /* a unary arithmetic expression */
944 case tcc_binary: /* a binary arithmetic expression */
945 kind = e_kind;
946 break;
948 case tcc_constant: /* a constant */
949 kind = c_kind;
950 break;
952 case tcc_exceptional: /* something random, like an identifier. */
953 switch (code)
955 case IDENTIFIER_NODE:
956 kind = id_kind;
957 break;
959 case TREE_VEC:
960 kind = vec_kind;
961 break;
963 case TREE_BINFO:
964 kind = binfo_kind;
965 break;
967 case SSA_NAME:
968 kind = ssa_name_kind;
969 break;
971 case BLOCK:
972 kind = b_kind;
973 break;
975 case CONSTRUCTOR:
976 kind = constr_kind;
977 break;
979 case OMP_CLAUSE:
980 kind = omp_clause_kind;
981 break;
983 default:
984 kind = x_kind;
985 break;
987 break;
989 case tcc_vl_exp:
990 kind = e_kind;
991 break;
993 default:
994 gcc_unreachable ();
997 tree_code_counts[(int) code]++;
998 tree_node_counts[(int) kind]++;
999 tree_node_sizes[(int) kind] += length;
1002 /* Allocate and return a new UID from the DECL_UID namespace. */
1005 allocate_decl_uid (void)
1007 return next_decl_uid++;
1010 /* Return a newly allocated node of code CODE. For decl and type
1011 nodes, some other fields are initialized. The rest of the node is
1012 initialized to zero. This function cannot be used for TREE_VEC,
1013 INTEGER_CST or OMP_CLAUSE nodes, which is enforced by asserts in
1014 tree_code_size.
1016 Achoo! I got a code in the node. */
1018 tree
1019 make_node_stat (enum tree_code code MEM_STAT_DECL)
1021 tree t;
1022 enum tree_code_class type = TREE_CODE_CLASS (code);
1023 size_t length = tree_code_size (code);
1025 record_node_allocation_statistics (code, length);
1027 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1028 TREE_SET_CODE (t, code);
1030 switch (type)
1032 case tcc_statement:
1033 TREE_SIDE_EFFECTS (t) = 1;
1034 break;
1036 case tcc_declaration:
1037 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1039 if (code == FUNCTION_DECL)
1041 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
1042 DECL_MODE (t) = FUNCTION_MODE;
1044 else
1045 DECL_ALIGN (t) = 1;
1047 DECL_SOURCE_LOCATION (t) = input_location;
1048 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
1049 DECL_UID (t) = --next_debug_decl_uid;
1050 else
1052 DECL_UID (t) = allocate_decl_uid ();
1053 SET_DECL_PT_UID (t, -1);
1055 if (TREE_CODE (t) == LABEL_DECL)
1056 LABEL_DECL_UID (t) = -1;
1058 break;
1060 case tcc_type:
1061 TYPE_UID (t) = next_type_uid++;
1062 TYPE_ALIGN (t) = BITS_PER_UNIT;
1063 TYPE_USER_ALIGN (t) = 0;
1064 TYPE_MAIN_VARIANT (t) = t;
1065 TYPE_CANONICAL (t) = t;
1067 /* Default to no attributes for type, but let target change that. */
1068 TYPE_ATTRIBUTES (t) = NULL_TREE;
1069 targetm.set_default_type_attributes (t);
1071 /* We have not yet computed the alias set for this type. */
1072 TYPE_ALIAS_SET (t) = -1;
1073 break;
1075 case tcc_constant:
1076 TREE_CONSTANT (t) = 1;
1077 break;
1079 case tcc_expression:
1080 switch (code)
1082 case INIT_EXPR:
1083 case MODIFY_EXPR:
1084 case VA_ARG_EXPR:
1085 case PREDECREMENT_EXPR:
1086 case PREINCREMENT_EXPR:
1087 case POSTDECREMENT_EXPR:
1088 case POSTINCREMENT_EXPR:
1089 /* All of these have side-effects, no matter what their
1090 operands are. */
1091 TREE_SIDE_EFFECTS (t) = 1;
1092 break;
1094 default:
1095 break;
1097 break;
1099 default:
1100 /* Other classes need no special treatment. */
1101 break;
1104 return t;
1107 /* Return a new node with the same contents as NODE except that its
1108 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
1110 tree
1111 copy_node_stat (tree node MEM_STAT_DECL)
1113 tree t;
1114 enum tree_code code = TREE_CODE (node);
1115 size_t length;
1117 gcc_assert (code != STATEMENT_LIST);
1119 length = tree_size (node);
1120 record_node_allocation_statistics (code, length);
1121 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1122 memcpy (t, node, length);
1124 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
1125 TREE_CHAIN (t) = 0;
1126 TREE_ASM_WRITTEN (t) = 0;
1127 TREE_VISITED (t) = 0;
1129 if (TREE_CODE_CLASS (code) == tcc_declaration)
1131 if (code == DEBUG_EXPR_DECL)
1132 DECL_UID (t) = --next_debug_decl_uid;
1133 else
1135 DECL_UID (t) = allocate_decl_uid ();
1136 if (DECL_PT_UID_SET_P (node))
1137 SET_DECL_PT_UID (t, DECL_PT_UID (node));
1139 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
1140 && DECL_HAS_VALUE_EXPR_P (node))
1142 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
1143 DECL_HAS_VALUE_EXPR_P (t) = 1;
1145 /* DECL_DEBUG_EXPR is copied explicitely by callers. */
1146 if (TREE_CODE (node) == VAR_DECL)
1148 DECL_HAS_DEBUG_EXPR_P (t) = 0;
1149 t->decl_with_vis.symtab_node = NULL;
1151 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
1153 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
1154 DECL_HAS_INIT_PRIORITY_P (t) = 1;
1156 if (TREE_CODE (node) == FUNCTION_DECL)
1158 DECL_STRUCT_FUNCTION (t) = NULL;
1159 t->decl_with_vis.symtab_node = NULL;
1162 else if (TREE_CODE_CLASS (code) == tcc_type)
1164 TYPE_UID (t) = next_type_uid++;
1165 /* The following is so that the debug code for
1166 the copy is different from the original type.
1167 The two statements usually duplicate each other
1168 (because they clear fields of the same union),
1169 but the optimizer should catch that. */
1170 TYPE_SYMTAB_POINTER (t) = 0;
1171 TYPE_SYMTAB_ADDRESS (t) = 0;
1173 /* Do not copy the values cache. */
1174 if (TYPE_CACHED_VALUES_P (t))
1176 TYPE_CACHED_VALUES_P (t) = 0;
1177 TYPE_CACHED_VALUES (t) = NULL_TREE;
1181 return t;
1184 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1185 For example, this can copy a list made of TREE_LIST nodes. */
1187 tree
1188 copy_list (tree list)
1190 tree head;
1191 tree prev, next;
1193 if (list == 0)
1194 return 0;
1196 head = prev = copy_node (list);
1197 next = TREE_CHAIN (list);
1198 while (next)
1200 TREE_CHAIN (prev) = copy_node (next);
1201 prev = TREE_CHAIN (prev);
1202 next = TREE_CHAIN (next);
1204 return head;
1208 /* Return the value that TREE_INT_CST_EXT_NUNITS should have for an
1209 INTEGER_CST with value CST and type TYPE. */
1211 static unsigned int
1212 get_int_cst_ext_nunits (tree type, const wide_int &cst)
1214 gcc_checking_assert (cst.get_precision () == TYPE_PRECISION (type));
1215 /* We need an extra zero HWI if CST is an unsigned integer with its
1216 upper bit set, and if CST occupies a whole number of HWIs. */
1217 if (TYPE_UNSIGNED (type)
1218 && wi::neg_p (cst)
1219 && (cst.get_precision () % HOST_BITS_PER_WIDE_INT) == 0)
1220 return cst.get_precision () / HOST_BITS_PER_WIDE_INT + 1;
1221 return cst.get_len ();
1224 /* Return a new INTEGER_CST with value CST and type TYPE. */
1226 static tree
1227 build_new_int_cst (tree type, const wide_int &cst)
1229 unsigned int len = cst.get_len ();
1230 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1231 tree nt = make_int_cst (len, ext_len);
1233 if (len < ext_len)
1235 --ext_len;
1236 TREE_INT_CST_ELT (nt, ext_len) = 0;
1237 for (unsigned int i = len; i < ext_len; ++i)
1238 TREE_INT_CST_ELT (nt, i) = -1;
1240 else if (TYPE_UNSIGNED (type)
1241 && cst.get_precision () < len * HOST_BITS_PER_WIDE_INT)
1243 len--;
1244 TREE_INT_CST_ELT (nt, len)
1245 = zext_hwi (cst.elt (len),
1246 cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1249 for (unsigned int i = 0; i < len; i++)
1250 TREE_INT_CST_ELT (nt, i) = cst.elt (i);
1251 TREE_TYPE (nt) = type;
1252 return nt;
1255 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1257 tree
1258 build_int_cst (tree type, HOST_WIDE_INT low)
1260 /* Support legacy code. */
1261 if (!type)
1262 type = integer_type_node;
1264 return wide_int_to_tree (type, wi::shwi (low, TYPE_PRECISION (type)));
1267 tree
1268 build_int_cstu (tree type, unsigned HOST_WIDE_INT cst)
1270 return wide_int_to_tree (type, wi::uhwi (cst, TYPE_PRECISION (type)));
1273 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1275 tree
1276 build_int_cst_type (tree type, HOST_WIDE_INT low)
1278 gcc_assert (type);
1279 return wide_int_to_tree (type, wi::shwi (low, TYPE_PRECISION (type)));
1282 /* Constructs tree in type TYPE from with value given by CST. Signedness
1283 of CST is assumed to be the same as the signedness of TYPE. */
1285 tree
1286 double_int_to_tree (tree type, double_int cst)
1288 return wide_int_to_tree (type, widest_int::from (cst, TYPE_SIGN (type)));
1291 /* We force the wide_int CST to the range of the type TYPE by sign or
1292 zero extending it. OVERFLOWABLE indicates if we are interested in
1293 overflow of the value, when >0 we are only interested in signed
1294 overflow, for <0 we are interested in any overflow. OVERFLOWED
1295 indicates whether overflow has already occurred. CONST_OVERFLOWED
1296 indicates whether constant overflow has already occurred. We force
1297 T's value to be within range of T's type (by setting to 0 or 1 all
1298 the bits outside the type's range). We set TREE_OVERFLOWED if,
1299 OVERFLOWED is nonzero,
1300 or OVERFLOWABLE is >0 and signed overflow occurs
1301 or OVERFLOWABLE is <0 and any overflow occurs
1302 We return a new tree node for the extended wide_int. The node
1303 is shared if no overflow flags are set. */
1306 tree
1307 force_fit_type (tree type, const wide_int_ref &cst,
1308 int overflowable, bool overflowed)
1310 signop sign = TYPE_SIGN (type);
1312 /* If we need to set overflow flags, return a new unshared node. */
1313 if (overflowed || !wi::fits_to_tree_p (cst, type))
1315 if (overflowed
1316 || overflowable < 0
1317 || (overflowable > 0 && sign == SIGNED))
1319 wide_int tmp = wide_int::from (cst, TYPE_PRECISION (type), sign);
1320 tree t = build_new_int_cst (type, tmp);
1321 TREE_OVERFLOW (t) = 1;
1322 return t;
1326 /* Else build a shared node. */
1327 return wide_int_to_tree (type, cst);
1330 /* These are the hash table functions for the hash table of INTEGER_CST
1331 nodes of a sizetype. */
1333 /* Return the hash code code X, an INTEGER_CST. */
1335 hashval_t
1336 int_cst_hasher::hash (tree x)
1338 const_tree const t = x;
1339 hashval_t code = TYPE_UID (TREE_TYPE (t));
1340 int i;
1342 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
1343 code ^= TREE_INT_CST_ELT (t, i);
1345 return code;
1348 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1349 is the same as that given by *Y, which is the same. */
1351 bool
1352 int_cst_hasher::equal (tree x, tree y)
1354 const_tree const xt = x;
1355 const_tree const yt = y;
1357 if (TREE_TYPE (xt) != TREE_TYPE (yt)
1358 || TREE_INT_CST_NUNITS (xt) != TREE_INT_CST_NUNITS (yt)
1359 || TREE_INT_CST_EXT_NUNITS (xt) != TREE_INT_CST_EXT_NUNITS (yt))
1360 return false;
1362 for (int i = 0; i < TREE_INT_CST_NUNITS (xt); i++)
1363 if (TREE_INT_CST_ELT (xt, i) != TREE_INT_CST_ELT (yt, i))
1364 return false;
1366 return true;
1369 /* Create an INT_CST node of TYPE and value CST.
1370 The returned node is always shared. For small integers we use a
1371 per-type vector cache, for larger ones we use a single hash table.
1372 The value is extended from its precision according to the sign of
1373 the type to be a multiple of HOST_BITS_PER_WIDE_INT. This defines
1374 the upper bits and ensures that hashing and value equality based
1375 upon the underlying HOST_WIDE_INTs works without masking. */
1377 tree
1378 wide_int_to_tree (tree type, const wide_int_ref &pcst)
1380 tree t;
1381 int ix = -1;
1382 int limit = 0;
1384 gcc_assert (type);
1385 unsigned int prec = TYPE_PRECISION (type);
1386 signop sgn = TYPE_SIGN (type);
1388 /* Verify that everything is canonical. */
1389 int l = pcst.get_len ();
1390 if (l > 1)
1392 if (pcst.elt (l - 1) == 0)
1393 gcc_checking_assert (pcst.elt (l - 2) < 0);
1394 if (pcst.elt (l - 1) == (HOST_WIDE_INT) -1)
1395 gcc_checking_assert (pcst.elt (l - 2) >= 0);
1398 wide_int cst = wide_int::from (pcst, prec, sgn);
1399 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1401 if (ext_len == 1)
1403 /* We just need to store a single HOST_WIDE_INT. */
1404 HOST_WIDE_INT hwi;
1405 if (TYPE_UNSIGNED (type))
1406 hwi = cst.to_uhwi ();
1407 else
1408 hwi = cst.to_shwi ();
1410 switch (TREE_CODE (type))
1412 case NULLPTR_TYPE:
1413 gcc_assert (hwi == 0);
1414 /* Fallthru. */
1416 case POINTER_TYPE:
1417 case REFERENCE_TYPE:
1418 case POINTER_BOUNDS_TYPE:
1419 /* Cache NULL pointer and zero bounds. */
1420 if (hwi == 0)
1422 limit = 1;
1423 ix = 0;
1425 break;
1427 case BOOLEAN_TYPE:
1428 /* Cache false or true. */
1429 limit = 2;
1430 if (hwi < 2)
1431 ix = hwi;
1432 break;
1434 case INTEGER_TYPE:
1435 case OFFSET_TYPE:
1436 if (TYPE_SIGN (type) == UNSIGNED)
1438 /* Cache [0, N). */
1439 limit = INTEGER_SHARE_LIMIT;
1440 if (IN_RANGE (hwi, 0, INTEGER_SHARE_LIMIT - 1))
1441 ix = hwi;
1443 else
1445 /* Cache [-1, N). */
1446 limit = INTEGER_SHARE_LIMIT + 1;
1447 if (IN_RANGE (hwi, -1, INTEGER_SHARE_LIMIT - 1))
1448 ix = hwi + 1;
1450 break;
1452 case ENUMERAL_TYPE:
1453 break;
1455 default:
1456 gcc_unreachable ();
1459 if (ix >= 0)
1461 /* Look for it in the type's vector of small shared ints. */
1462 if (!TYPE_CACHED_VALUES_P (type))
1464 TYPE_CACHED_VALUES_P (type) = 1;
1465 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1468 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1469 if (t)
1470 /* Make sure no one is clobbering the shared constant. */
1471 gcc_checking_assert (TREE_TYPE (t) == type
1472 && TREE_INT_CST_NUNITS (t) == 1
1473 && TREE_INT_CST_OFFSET_NUNITS (t) == 1
1474 && TREE_INT_CST_EXT_NUNITS (t) == 1
1475 && TREE_INT_CST_ELT (t, 0) == hwi);
1476 else
1478 /* Create a new shared int. */
1479 t = build_new_int_cst (type, cst);
1480 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1483 else
1485 /* Use the cache of larger shared ints, using int_cst_node as
1486 a temporary. */
1488 TREE_INT_CST_ELT (int_cst_node, 0) = hwi;
1489 TREE_TYPE (int_cst_node) = type;
1491 tree *slot = int_cst_hash_table->find_slot (int_cst_node, INSERT);
1492 t = *slot;
1493 if (!t)
1495 /* Insert this one into the hash table. */
1496 t = int_cst_node;
1497 *slot = t;
1498 /* Make a new node for next time round. */
1499 int_cst_node = make_int_cst (1, 1);
1503 else
1505 /* The value either hashes properly or we drop it on the floor
1506 for the gc to take care of. There will not be enough of them
1507 to worry about. */
1509 tree nt = build_new_int_cst (type, cst);
1510 tree *slot = int_cst_hash_table->find_slot (nt, INSERT);
1511 t = *slot;
1512 if (!t)
1514 /* Insert this one into the hash table. */
1515 t = nt;
1516 *slot = t;
1520 return t;
1523 void
1524 cache_integer_cst (tree t)
1526 tree type = TREE_TYPE (t);
1527 int ix = -1;
1528 int limit = 0;
1529 int prec = TYPE_PRECISION (type);
1531 gcc_assert (!TREE_OVERFLOW (t));
1533 switch (TREE_CODE (type))
1535 case NULLPTR_TYPE:
1536 gcc_assert (integer_zerop (t));
1537 /* Fallthru. */
1539 case POINTER_TYPE:
1540 case REFERENCE_TYPE:
1541 /* Cache NULL pointer. */
1542 if (integer_zerop (t))
1544 limit = 1;
1545 ix = 0;
1547 break;
1549 case BOOLEAN_TYPE:
1550 /* Cache false or true. */
1551 limit = 2;
1552 if (wi::ltu_p (t, 2))
1553 ix = TREE_INT_CST_ELT (t, 0);
1554 break;
1556 case INTEGER_TYPE:
1557 case OFFSET_TYPE:
1558 if (TYPE_UNSIGNED (type))
1560 /* Cache 0..N */
1561 limit = INTEGER_SHARE_LIMIT;
1563 /* This is a little hokie, but if the prec is smaller than
1564 what is necessary to hold INTEGER_SHARE_LIMIT, then the
1565 obvious test will not get the correct answer. */
1566 if (prec < HOST_BITS_PER_WIDE_INT)
1568 if (tree_to_uhwi (t) < (unsigned HOST_WIDE_INT) INTEGER_SHARE_LIMIT)
1569 ix = tree_to_uhwi (t);
1571 else if (wi::ltu_p (t, INTEGER_SHARE_LIMIT))
1572 ix = tree_to_uhwi (t);
1574 else
1576 /* Cache -1..N */
1577 limit = INTEGER_SHARE_LIMIT + 1;
1579 if (integer_minus_onep (t))
1580 ix = 0;
1581 else if (!wi::neg_p (t))
1583 if (prec < HOST_BITS_PER_WIDE_INT)
1585 if (tree_to_shwi (t) < INTEGER_SHARE_LIMIT)
1586 ix = tree_to_shwi (t) + 1;
1588 else if (wi::ltu_p (t, INTEGER_SHARE_LIMIT))
1589 ix = tree_to_shwi (t) + 1;
1592 break;
1594 case ENUMERAL_TYPE:
1595 break;
1597 default:
1598 gcc_unreachable ();
1601 if (ix >= 0)
1603 /* Look for it in the type's vector of small shared ints. */
1604 if (!TYPE_CACHED_VALUES_P (type))
1606 TYPE_CACHED_VALUES_P (type) = 1;
1607 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1610 gcc_assert (TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) == NULL_TREE);
1611 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1613 else
1615 /* Use the cache of larger shared ints. */
1616 tree *slot = int_cst_hash_table->find_slot (t, INSERT);
1617 /* If there is already an entry for the number verify it's the
1618 same. */
1619 if (*slot)
1620 gcc_assert (wi::eq_p (tree (*slot), t));
1621 else
1622 /* Otherwise insert this one into the hash table. */
1623 *slot = t;
1628 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1629 and the rest are zeros. */
1631 tree
1632 build_low_bits_mask (tree type, unsigned bits)
1634 gcc_assert (bits <= TYPE_PRECISION (type));
1636 return wide_int_to_tree (type, wi::mask (bits, false,
1637 TYPE_PRECISION (type)));
1640 /* Checks that X is integer constant that can be expressed in (unsigned)
1641 HOST_WIDE_INT without loss of precision. */
1643 bool
1644 cst_and_fits_in_hwi (const_tree x)
1646 if (TREE_CODE (x) != INTEGER_CST)
1647 return false;
1649 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1650 return false;
1652 return TREE_INT_CST_NUNITS (x) == 1;
1655 /* Build a newly constructed TREE_VEC node of length LEN. */
1657 tree
1658 make_vector_stat (unsigned len MEM_STAT_DECL)
1660 tree t;
1661 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1663 record_node_allocation_statistics (VECTOR_CST, length);
1665 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1667 TREE_SET_CODE (t, VECTOR_CST);
1668 TREE_CONSTANT (t) = 1;
1670 return t;
1673 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1674 are in a list pointed to by VALS. */
1676 tree
1677 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1679 int over = 0;
1680 unsigned cnt = 0;
1681 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1682 TREE_TYPE (v) = type;
1684 /* Iterate through elements and check for overflow. */
1685 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1687 tree value = vals[cnt];
1689 VECTOR_CST_ELT (v, cnt) = value;
1691 /* Don't crash if we get an address constant. */
1692 if (!CONSTANT_CLASS_P (value))
1693 continue;
1695 over |= TREE_OVERFLOW (value);
1698 TREE_OVERFLOW (v) = over;
1699 return v;
1702 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1703 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1705 tree
1706 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1708 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1709 unsigned HOST_WIDE_INT idx;
1710 tree value;
1712 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1713 vec[idx] = value;
1714 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1715 vec[idx] = build_zero_cst (TREE_TYPE (type));
1717 return build_vector (type, vec);
1720 /* Build a vector of type VECTYPE where all the elements are SCs. */
1721 tree
1722 build_vector_from_val (tree vectype, tree sc)
1724 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1726 if (sc == error_mark_node)
1727 return sc;
1729 /* Verify that the vector type is suitable for SC. Note that there
1730 is some inconsistency in the type-system with respect to restrict
1731 qualifications of pointers. Vector types always have a main-variant
1732 element type and the qualification is applied to the vector-type.
1733 So TREE_TYPE (vector-type) does not return a properly qualified
1734 vector element-type. */
1735 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1736 TREE_TYPE (vectype)));
1738 if (CONSTANT_CLASS_P (sc))
1740 tree *v = XALLOCAVEC (tree, nunits);
1741 for (i = 0; i < nunits; ++i)
1742 v[i] = sc;
1743 return build_vector (vectype, v);
1745 else
1747 vec<constructor_elt, va_gc> *v;
1748 vec_alloc (v, nunits);
1749 for (i = 0; i < nunits; ++i)
1750 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1751 return build_constructor (vectype, v);
1755 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1756 are in the vec pointed to by VALS. */
1757 tree
1758 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1760 tree c = make_node (CONSTRUCTOR);
1761 unsigned int i;
1762 constructor_elt *elt;
1763 bool constant_p = true;
1764 bool side_effects_p = false;
1766 TREE_TYPE (c) = type;
1767 CONSTRUCTOR_ELTS (c) = vals;
1769 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1771 /* Mostly ctors will have elts that don't have side-effects, so
1772 the usual case is to scan all the elements. Hence a single
1773 loop for both const and side effects, rather than one loop
1774 each (with early outs). */
1775 if (!TREE_CONSTANT (elt->value))
1776 constant_p = false;
1777 if (TREE_SIDE_EFFECTS (elt->value))
1778 side_effects_p = true;
1781 TREE_SIDE_EFFECTS (c) = side_effects_p;
1782 TREE_CONSTANT (c) = constant_p;
1784 return c;
1787 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1788 INDEX and VALUE. */
1789 tree
1790 build_constructor_single (tree type, tree index, tree value)
1792 vec<constructor_elt, va_gc> *v;
1793 constructor_elt elt = {index, value};
1795 vec_alloc (v, 1);
1796 v->quick_push (elt);
1798 return build_constructor (type, v);
1802 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1803 are in a list pointed to by VALS. */
1804 tree
1805 build_constructor_from_list (tree type, tree vals)
1807 tree t;
1808 vec<constructor_elt, va_gc> *v = NULL;
1810 if (vals)
1812 vec_alloc (v, list_length (vals));
1813 for (t = vals; t; t = TREE_CHAIN (t))
1814 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1817 return build_constructor (type, v);
1820 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
1821 of elements, provided as index/value pairs. */
1823 tree
1824 build_constructor_va (tree type, int nelts, ...)
1826 vec<constructor_elt, va_gc> *v = NULL;
1827 va_list p;
1829 va_start (p, nelts);
1830 vec_alloc (v, nelts);
1831 while (nelts--)
1833 tree index = va_arg (p, tree);
1834 tree value = va_arg (p, tree);
1835 CONSTRUCTOR_APPEND_ELT (v, index, value);
1837 va_end (p);
1838 return build_constructor (type, v);
1841 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1843 tree
1844 build_fixed (tree type, FIXED_VALUE_TYPE f)
1846 tree v;
1847 FIXED_VALUE_TYPE *fp;
1849 v = make_node (FIXED_CST);
1850 fp = ggc_alloc<fixed_value> ();
1851 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1853 TREE_TYPE (v) = type;
1854 TREE_FIXED_CST_PTR (v) = fp;
1855 return v;
1858 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1860 tree
1861 build_real (tree type, REAL_VALUE_TYPE d)
1863 tree v;
1864 REAL_VALUE_TYPE *dp;
1865 int overflow = 0;
1867 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1868 Consider doing it via real_convert now. */
1870 v = make_node (REAL_CST);
1871 dp = ggc_alloc<real_value> ();
1872 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1874 TREE_TYPE (v) = type;
1875 TREE_REAL_CST_PTR (v) = dp;
1876 TREE_OVERFLOW (v) = overflow;
1877 return v;
1880 /* Return a new REAL_CST node whose type is TYPE
1881 and whose value is the integer value of the INTEGER_CST node I. */
1883 REAL_VALUE_TYPE
1884 real_value_from_int_cst (const_tree type, const_tree i)
1886 REAL_VALUE_TYPE d;
1888 /* Clear all bits of the real value type so that we can later do
1889 bitwise comparisons to see if two values are the same. */
1890 memset (&d, 0, sizeof d);
1892 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode, i,
1893 TYPE_SIGN (TREE_TYPE (i)));
1894 return d;
1897 /* Given a tree representing an integer constant I, return a tree
1898 representing the same value as a floating-point constant of type TYPE. */
1900 tree
1901 build_real_from_int_cst (tree type, const_tree i)
1903 tree v;
1904 int overflow = TREE_OVERFLOW (i);
1906 v = build_real (type, real_value_from_int_cst (type, i));
1908 TREE_OVERFLOW (v) |= overflow;
1909 return v;
1912 /* Return a newly constructed STRING_CST node whose value is
1913 the LEN characters at STR.
1914 Note that for a C string literal, LEN should include the trailing NUL.
1915 The TREE_TYPE is not initialized. */
1917 tree
1918 build_string (int len, const char *str)
1920 tree s;
1921 size_t length;
1923 /* Do not waste bytes provided by padding of struct tree_string. */
1924 length = len + offsetof (struct tree_string, str) + 1;
1926 record_node_allocation_statistics (STRING_CST, length);
1928 s = (tree) ggc_internal_alloc (length);
1930 memset (s, 0, sizeof (struct tree_typed));
1931 TREE_SET_CODE (s, STRING_CST);
1932 TREE_CONSTANT (s) = 1;
1933 TREE_STRING_LENGTH (s) = len;
1934 memcpy (s->string.str, str, len);
1935 s->string.str[len] = '\0';
1937 return s;
1940 /* Return a newly constructed COMPLEX_CST node whose value is
1941 specified by the real and imaginary parts REAL and IMAG.
1942 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1943 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1945 tree
1946 build_complex (tree type, tree real, tree imag)
1948 tree t = make_node (COMPLEX_CST);
1950 TREE_REALPART (t) = real;
1951 TREE_IMAGPART (t) = imag;
1952 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1953 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1954 return t;
1957 /* Return a constant of arithmetic type TYPE which is the
1958 multiplicative identity of the set TYPE. */
1960 tree
1961 build_one_cst (tree type)
1963 switch (TREE_CODE (type))
1965 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1966 case POINTER_TYPE: case REFERENCE_TYPE:
1967 case OFFSET_TYPE:
1968 return build_int_cst (type, 1);
1970 case REAL_TYPE:
1971 return build_real (type, dconst1);
1973 case FIXED_POINT_TYPE:
1974 /* We can only generate 1 for accum types. */
1975 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1976 return build_fixed (type, FCONST1 (TYPE_MODE (type)));
1978 case VECTOR_TYPE:
1980 tree scalar = build_one_cst (TREE_TYPE (type));
1982 return build_vector_from_val (type, scalar);
1985 case COMPLEX_TYPE:
1986 return build_complex (type,
1987 build_one_cst (TREE_TYPE (type)),
1988 build_zero_cst (TREE_TYPE (type)));
1990 default:
1991 gcc_unreachable ();
1995 /* Return an integer of type TYPE containing all 1's in as much precision as
1996 it contains, or a complex or vector whose subparts are such integers. */
1998 tree
1999 build_all_ones_cst (tree type)
2001 if (TREE_CODE (type) == COMPLEX_TYPE)
2003 tree scalar = build_all_ones_cst (TREE_TYPE (type));
2004 return build_complex (type, scalar, scalar);
2006 else
2007 return build_minus_one_cst (type);
2010 /* Return a constant of arithmetic type TYPE which is the
2011 opposite of the multiplicative identity of the set TYPE. */
2013 tree
2014 build_minus_one_cst (tree type)
2016 switch (TREE_CODE (type))
2018 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2019 case POINTER_TYPE: case REFERENCE_TYPE:
2020 case OFFSET_TYPE:
2021 return build_int_cst (type, -1);
2023 case REAL_TYPE:
2024 return build_real (type, dconstm1);
2026 case FIXED_POINT_TYPE:
2027 /* We can only generate 1 for accum types. */
2028 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2029 return build_fixed (type, fixed_from_double_int (double_int_minus_one,
2030 TYPE_MODE (type)));
2032 case VECTOR_TYPE:
2034 tree scalar = build_minus_one_cst (TREE_TYPE (type));
2036 return build_vector_from_val (type, scalar);
2039 case COMPLEX_TYPE:
2040 return build_complex (type,
2041 build_minus_one_cst (TREE_TYPE (type)),
2042 build_zero_cst (TREE_TYPE (type)));
2044 default:
2045 gcc_unreachable ();
2049 /* Build 0 constant of type TYPE. This is used by constructor folding
2050 and thus the constant should be represented in memory by
2051 zero(es). */
2053 tree
2054 build_zero_cst (tree type)
2056 switch (TREE_CODE (type))
2058 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2059 case POINTER_TYPE: case REFERENCE_TYPE:
2060 case OFFSET_TYPE: case NULLPTR_TYPE:
2061 return build_int_cst (type, 0);
2063 case REAL_TYPE:
2064 return build_real (type, dconst0);
2066 case FIXED_POINT_TYPE:
2067 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
2069 case VECTOR_TYPE:
2071 tree scalar = build_zero_cst (TREE_TYPE (type));
2073 return build_vector_from_val (type, scalar);
2076 case COMPLEX_TYPE:
2078 tree zero = build_zero_cst (TREE_TYPE (type));
2080 return build_complex (type, zero, zero);
2083 default:
2084 if (!AGGREGATE_TYPE_P (type))
2085 return fold_convert (type, integer_zero_node);
2086 return build_constructor (type, NULL);
2091 /* Build a BINFO with LEN language slots. */
2093 tree
2094 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
2096 tree t;
2097 size_t length = (offsetof (struct tree_binfo, base_binfos)
2098 + vec<tree, va_gc>::embedded_size (base_binfos));
2100 record_node_allocation_statistics (TREE_BINFO, length);
2102 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
2104 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
2106 TREE_SET_CODE (t, TREE_BINFO);
2108 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
2110 return t;
2113 /* Create a CASE_LABEL_EXPR tree node and return it. */
2115 tree
2116 build_case_label (tree low_value, tree high_value, tree label_decl)
2118 tree t = make_node (CASE_LABEL_EXPR);
2120 TREE_TYPE (t) = void_type_node;
2121 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
2123 CASE_LOW (t) = low_value;
2124 CASE_HIGH (t) = high_value;
2125 CASE_LABEL (t) = label_decl;
2126 CASE_CHAIN (t) = NULL_TREE;
2128 return t;
2131 /* Build a newly constructed INTEGER_CST node. LEN and EXT_LEN are the
2132 values of TREE_INT_CST_NUNITS and TREE_INT_CST_EXT_NUNITS respectively.
2133 The latter determines the length of the HOST_WIDE_INT vector. */
2135 tree
2136 make_int_cst_stat (int len, int ext_len MEM_STAT_DECL)
2138 tree t;
2139 int length = ((ext_len - 1) * sizeof (HOST_WIDE_INT)
2140 + sizeof (struct tree_int_cst));
2142 gcc_assert (len);
2143 record_node_allocation_statistics (INTEGER_CST, length);
2145 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2147 TREE_SET_CODE (t, INTEGER_CST);
2148 TREE_INT_CST_NUNITS (t) = len;
2149 TREE_INT_CST_EXT_NUNITS (t) = ext_len;
2150 /* to_offset can only be applied to trees that are offset_int-sized
2151 or smaller. EXT_LEN is correct if it fits, otherwise the constant
2152 must be exactly the precision of offset_int and so LEN is correct. */
2153 if (ext_len <= OFFSET_INT_ELTS)
2154 TREE_INT_CST_OFFSET_NUNITS (t) = ext_len;
2155 else
2156 TREE_INT_CST_OFFSET_NUNITS (t) = len;
2158 TREE_CONSTANT (t) = 1;
2160 return t;
2163 /* Build a newly constructed TREE_VEC node of length LEN. */
2165 tree
2166 make_tree_vec_stat (int len MEM_STAT_DECL)
2168 tree t;
2169 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2171 record_node_allocation_statistics (TREE_VEC, length);
2173 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2175 TREE_SET_CODE (t, TREE_VEC);
2176 TREE_VEC_LENGTH (t) = len;
2178 return t;
2181 /* Grow a TREE_VEC node to new length LEN. */
2183 tree
2184 grow_tree_vec_stat (tree v, int len MEM_STAT_DECL)
2186 gcc_assert (TREE_CODE (v) == TREE_VEC);
2188 int oldlen = TREE_VEC_LENGTH (v);
2189 gcc_assert (len > oldlen);
2191 int oldlength = (oldlen - 1) * sizeof (tree) + sizeof (struct tree_vec);
2192 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2194 record_node_allocation_statistics (TREE_VEC, length - oldlength);
2196 v = (tree) ggc_realloc (v, length PASS_MEM_STAT);
2198 TREE_VEC_LENGTH (v) = len;
2200 return v;
2203 /* Return 1 if EXPR is the integer constant zero or a complex constant
2204 of zero. */
2207 integer_zerop (const_tree expr)
2209 STRIP_NOPS (expr);
2211 switch (TREE_CODE (expr))
2213 case INTEGER_CST:
2214 return wi::eq_p (expr, 0);
2215 case COMPLEX_CST:
2216 return (integer_zerop (TREE_REALPART (expr))
2217 && integer_zerop (TREE_IMAGPART (expr)));
2218 case VECTOR_CST:
2220 unsigned i;
2221 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2222 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
2223 return false;
2224 return true;
2226 default:
2227 return false;
2231 /* Return 1 if EXPR is the integer constant one or the corresponding
2232 complex constant. */
2235 integer_onep (const_tree expr)
2237 STRIP_NOPS (expr);
2239 switch (TREE_CODE (expr))
2241 case INTEGER_CST:
2242 return wi::eq_p (wi::to_widest (expr), 1);
2243 case COMPLEX_CST:
2244 return (integer_onep (TREE_REALPART (expr))
2245 && integer_zerop (TREE_IMAGPART (expr)));
2246 case VECTOR_CST:
2248 unsigned i;
2249 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2250 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
2251 return false;
2252 return true;
2254 default:
2255 return false;
2259 /* Return 1 if EXPR is the integer constant one. For complex and vector,
2260 return 1 if every piece is the integer constant one. */
2263 integer_each_onep (const_tree expr)
2265 STRIP_NOPS (expr);
2267 if (TREE_CODE (expr) == COMPLEX_CST)
2268 return (integer_onep (TREE_REALPART (expr))
2269 && integer_onep (TREE_IMAGPART (expr)));
2270 else
2271 return integer_onep (expr);
2274 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
2275 it contains, or a complex or vector whose subparts are such integers. */
2278 integer_all_onesp (const_tree expr)
2280 STRIP_NOPS (expr);
2282 if (TREE_CODE (expr) == COMPLEX_CST
2283 && integer_all_onesp (TREE_REALPART (expr))
2284 && integer_all_onesp (TREE_IMAGPART (expr)))
2285 return 1;
2287 else if (TREE_CODE (expr) == VECTOR_CST)
2289 unsigned i;
2290 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2291 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
2292 return 0;
2293 return 1;
2296 else if (TREE_CODE (expr) != INTEGER_CST)
2297 return 0;
2299 return wi::max_value (TYPE_PRECISION (TREE_TYPE (expr)), UNSIGNED) == expr;
2302 /* Return 1 if EXPR is the integer constant minus one. */
2305 integer_minus_onep (const_tree expr)
2307 STRIP_NOPS (expr);
2309 if (TREE_CODE (expr) == COMPLEX_CST)
2310 return (integer_all_onesp (TREE_REALPART (expr))
2311 && integer_zerop (TREE_IMAGPART (expr)));
2312 else
2313 return integer_all_onesp (expr);
2316 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
2317 one bit on). */
2320 integer_pow2p (const_tree expr)
2322 STRIP_NOPS (expr);
2324 if (TREE_CODE (expr) == COMPLEX_CST
2325 && integer_pow2p (TREE_REALPART (expr))
2326 && integer_zerop (TREE_IMAGPART (expr)))
2327 return 1;
2329 if (TREE_CODE (expr) != INTEGER_CST)
2330 return 0;
2332 return wi::popcount (expr) == 1;
2335 /* Return 1 if EXPR is an integer constant other than zero or a
2336 complex constant other than zero. */
2339 integer_nonzerop (const_tree expr)
2341 STRIP_NOPS (expr);
2343 return ((TREE_CODE (expr) == INTEGER_CST
2344 && !wi::eq_p (expr, 0))
2345 || (TREE_CODE (expr) == COMPLEX_CST
2346 && (integer_nonzerop (TREE_REALPART (expr))
2347 || integer_nonzerop (TREE_IMAGPART (expr)))));
2350 /* Return 1 if EXPR is the integer constant one. For vector,
2351 return 1 if every piece is the integer constant minus one
2352 (representing the value TRUE). */
2355 integer_truep (const_tree expr)
2357 STRIP_NOPS (expr);
2359 if (TREE_CODE (expr) == VECTOR_CST)
2360 return integer_all_onesp (expr);
2361 return integer_onep (expr);
2364 /* Return 1 if EXPR is the fixed-point constant zero. */
2367 fixed_zerop (const_tree expr)
2369 return (TREE_CODE (expr) == FIXED_CST
2370 && TREE_FIXED_CST (expr).data.is_zero ());
2373 /* Return the power of two represented by a tree node known to be a
2374 power of two. */
2377 tree_log2 (const_tree expr)
2379 STRIP_NOPS (expr);
2381 if (TREE_CODE (expr) == COMPLEX_CST)
2382 return tree_log2 (TREE_REALPART (expr));
2384 return wi::exact_log2 (expr);
2387 /* Similar, but return the largest integer Y such that 2 ** Y is less
2388 than or equal to EXPR. */
2391 tree_floor_log2 (const_tree expr)
2393 STRIP_NOPS (expr);
2395 if (TREE_CODE (expr) == COMPLEX_CST)
2396 return tree_log2 (TREE_REALPART (expr));
2398 return wi::floor_log2 (expr);
2401 /* Return number of known trailing zero bits in EXPR, or, if the value of
2402 EXPR is known to be zero, the precision of it's type. */
2404 unsigned int
2405 tree_ctz (const_tree expr)
2407 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
2408 && !POINTER_TYPE_P (TREE_TYPE (expr)))
2409 return 0;
2411 unsigned int ret1, ret2, prec = TYPE_PRECISION (TREE_TYPE (expr));
2412 switch (TREE_CODE (expr))
2414 case INTEGER_CST:
2415 ret1 = wi::ctz (expr);
2416 return MIN (ret1, prec);
2417 case SSA_NAME:
2418 ret1 = wi::ctz (get_nonzero_bits (expr));
2419 return MIN (ret1, prec);
2420 case PLUS_EXPR:
2421 case MINUS_EXPR:
2422 case BIT_IOR_EXPR:
2423 case BIT_XOR_EXPR:
2424 case MIN_EXPR:
2425 case MAX_EXPR:
2426 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2427 if (ret1 == 0)
2428 return ret1;
2429 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2430 return MIN (ret1, ret2);
2431 case POINTER_PLUS_EXPR:
2432 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2433 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2434 /* Second operand is sizetype, which could be in theory
2435 wider than pointer's precision. Make sure we never
2436 return more than prec. */
2437 ret2 = MIN (ret2, prec);
2438 return MIN (ret1, ret2);
2439 case BIT_AND_EXPR:
2440 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2441 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2442 return MAX (ret1, ret2);
2443 case MULT_EXPR:
2444 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2445 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2446 return MIN (ret1 + ret2, prec);
2447 case LSHIFT_EXPR:
2448 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2449 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2450 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2452 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2453 return MIN (ret1 + ret2, prec);
2455 return ret1;
2456 case RSHIFT_EXPR:
2457 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2458 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2460 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2461 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2462 if (ret1 > ret2)
2463 return ret1 - ret2;
2465 return 0;
2466 case TRUNC_DIV_EXPR:
2467 case CEIL_DIV_EXPR:
2468 case FLOOR_DIV_EXPR:
2469 case ROUND_DIV_EXPR:
2470 case EXACT_DIV_EXPR:
2471 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
2472 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) == 1)
2474 int l = tree_log2 (TREE_OPERAND (expr, 1));
2475 if (l >= 0)
2477 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2478 ret2 = l;
2479 if (ret1 > ret2)
2480 return ret1 - ret2;
2483 return 0;
2484 CASE_CONVERT:
2485 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2486 if (ret1 && ret1 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2487 ret1 = prec;
2488 return MIN (ret1, prec);
2489 case SAVE_EXPR:
2490 return tree_ctz (TREE_OPERAND (expr, 0));
2491 case COND_EXPR:
2492 ret1 = tree_ctz (TREE_OPERAND (expr, 1));
2493 if (ret1 == 0)
2494 return 0;
2495 ret2 = tree_ctz (TREE_OPERAND (expr, 2));
2496 return MIN (ret1, ret2);
2497 case COMPOUND_EXPR:
2498 return tree_ctz (TREE_OPERAND (expr, 1));
2499 case ADDR_EXPR:
2500 ret1 = get_pointer_alignment (CONST_CAST_TREE (expr));
2501 if (ret1 > BITS_PER_UNIT)
2503 ret1 = ctz_hwi (ret1 / BITS_PER_UNIT);
2504 return MIN (ret1, prec);
2506 return 0;
2507 default:
2508 return 0;
2512 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
2513 decimal float constants, so don't return 1 for them. */
2516 real_zerop (const_tree expr)
2518 STRIP_NOPS (expr);
2520 switch (TREE_CODE (expr))
2522 case REAL_CST:
2523 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
2524 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2525 case COMPLEX_CST:
2526 return real_zerop (TREE_REALPART (expr))
2527 && real_zerop (TREE_IMAGPART (expr));
2528 case VECTOR_CST:
2530 unsigned i;
2531 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2532 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2533 return false;
2534 return true;
2536 default:
2537 return false;
2541 /* Return 1 if EXPR is the real constant one in real or complex form.
2542 Trailing zeroes matter for decimal float constants, so don't return
2543 1 for them. */
2546 real_onep (const_tree expr)
2548 STRIP_NOPS (expr);
2550 switch (TREE_CODE (expr))
2552 case REAL_CST:
2553 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
2554 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2555 case COMPLEX_CST:
2556 return real_onep (TREE_REALPART (expr))
2557 && real_zerop (TREE_IMAGPART (expr));
2558 case VECTOR_CST:
2560 unsigned i;
2561 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2562 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2563 return false;
2564 return true;
2566 default:
2567 return false;
2571 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2572 matter for decimal float constants, so don't return 1 for them. */
2575 real_minus_onep (const_tree expr)
2577 STRIP_NOPS (expr);
2579 switch (TREE_CODE (expr))
2581 case REAL_CST:
2582 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2583 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2584 case COMPLEX_CST:
2585 return real_minus_onep (TREE_REALPART (expr))
2586 && real_zerop (TREE_IMAGPART (expr));
2587 case VECTOR_CST:
2589 unsigned i;
2590 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2591 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2592 return false;
2593 return true;
2595 default:
2596 return false;
2600 /* Nonzero if EXP is a constant or a cast of a constant. */
2603 really_constant_p (const_tree exp)
2605 /* This is not quite the same as STRIP_NOPS. It does more. */
2606 while (CONVERT_EXPR_P (exp)
2607 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2608 exp = TREE_OPERAND (exp, 0);
2609 return TREE_CONSTANT (exp);
2612 /* Return first list element whose TREE_VALUE is ELEM.
2613 Return 0 if ELEM is not in LIST. */
2615 tree
2616 value_member (tree elem, tree list)
2618 while (list)
2620 if (elem == TREE_VALUE (list))
2621 return list;
2622 list = TREE_CHAIN (list);
2624 return NULL_TREE;
2627 /* Return first list element whose TREE_PURPOSE is ELEM.
2628 Return 0 if ELEM is not in LIST. */
2630 tree
2631 purpose_member (const_tree elem, tree list)
2633 while (list)
2635 if (elem == TREE_PURPOSE (list))
2636 return list;
2637 list = TREE_CHAIN (list);
2639 return NULL_TREE;
2642 /* Return true if ELEM is in V. */
2644 bool
2645 vec_member (const_tree elem, vec<tree, va_gc> *v)
2647 unsigned ix;
2648 tree t;
2649 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2650 if (elem == t)
2651 return true;
2652 return false;
2655 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2656 NULL_TREE. */
2658 tree
2659 chain_index (int idx, tree chain)
2661 for (; chain && idx > 0; --idx)
2662 chain = TREE_CHAIN (chain);
2663 return chain;
2666 /* Return nonzero if ELEM is part of the chain CHAIN. */
2669 chain_member (const_tree elem, const_tree chain)
2671 while (chain)
2673 if (elem == chain)
2674 return 1;
2675 chain = DECL_CHAIN (chain);
2678 return 0;
2681 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2682 We expect a null pointer to mark the end of the chain.
2683 This is the Lisp primitive `length'. */
2686 list_length (const_tree t)
2688 const_tree p = t;
2689 #ifdef ENABLE_TREE_CHECKING
2690 const_tree q = t;
2691 #endif
2692 int len = 0;
2694 while (p)
2696 p = TREE_CHAIN (p);
2697 #ifdef ENABLE_TREE_CHECKING
2698 if (len % 2)
2699 q = TREE_CHAIN (q);
2700 gcc_assert (p != q);
2701 #endif
2702 len++;
2705 return len;
2708 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2709 UNION_TYPE TYPE, or NULL_TREE if none. */
2711 tree
2712 first_field (const_tree type)
2714 tree t = TYPE_FIELDS (type);
2715 while (t && TREE_CODE (t) != FIELD_DECL)
2716 t = TREE_CHAIN (t);
2717 return t;
2720 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2721 by modifying the last node in chain 1 to point to chain 2.
2722 This is the Lisp primitive `nconc'. */
2724 tree
2725 chainon (tree op1, tree op2)
2727 tree t1;
2729 if (!op1)
2730 return op2;
2731 if (!op2)
2732 return op1;
2734 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2735 continue;
2736 TREE_CHAIN (t1) = op2;
2738 #ifdef ENABLE_TREE_CHECKING
2740 tree t2;
2741 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2742 gcc_assert (t2 != t1);
2744 #endif
2746 return op1;
2749 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2751 tree
2752 tree_last (tree chain)
2754 tree next;
2755 if (chain)
2756 while ((next = TREE_CHAIN (chain)))
2757 chain = next;
2758 return chain;
2761 /* Reverse the order of elements in the chain T,
2762 and return the new head of the chain (old last element). */
2764 tree
2765 nreverse (tree t)
2767 tree prev = 0, decl, next;
2768 for (decl = t; decl; decl = next)
2770 /* We shouldn't be using this function to reverse BLOCK chains; we
2771 have blocks_nreverse for that. */
2772 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2773 next = TREE_CHAIN (decl);
2774 TREE_CHAIN (decl) = prev;
2775 prev = decl;
2777 return prev;
2780 /* Return a newly created TREE_LIST node whose
2781 purpose and value fields are PARM and VALUE. */
2783 tree
2784 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2786 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2787 TREE_PURPOSE (t) = parm;
2788 TREE_VALUE (t) = value;
2789 return t;
2792 /* Build a chain of TREE_LIST nodes from a vector. */
2794 tree
2795 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2797 tree ret = NULL_TREE;
2798 tree *pp = &ret;
2799 unsigned int i;
2800 tree t;
2801 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2803 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2804 pp = &TREE_CHAIN (*pp);
2806 return ret;
2809 /* Return a newly created TREE_LIST node whose
2810 purpose and value fields are PURPOSE and VALUE
2811 and whose TREE_CHAIN is CHAIN. */
2813 tree
2814 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2816 tree node;
2818 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
2819 memset (node, 0, sizeof (struct tree_common));
2821 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2823 TREE_SET_CODE (node, TREE_LIST);
2824 TREE_CHAIN (node) = chain;
2825 TREE_PURPOSE (node) = purpose;
2826 TREE_VALUE (node) = value;
2827 return node;
2830 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2831 trees. */
2833 vec<tree, va_gc> *
2834 ctor_to_vec (tree ctor)
2836 vec<tree, va_gc> *vec;
2837 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2838 unsigned int ix;
2839 tree val;
2841 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2842 vec->quick_push (val);
2844 return vec;
2847 /* Return the size nominally occupied by an object of type TYPE
2848 when it resides in memory. The value is measured in units of bytes,
2849 and its data type is that normally used for type sizes
2850 (which is the first type created by make_signed_type or
2851 make_unsigned_type). */
2853 tree
2854 size_in_bytes (const_tree type)
2856 tree t;
2858 if (type == error_mark_node)
2859 return integer_zero_node;
2861 type = TYPE_MAIN_VARIANT (type);
2862 t = TYPE_SIZE_UNIT (type);
2864 if (t == 0)
2866 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2867 return size_zero_node;
2870 return t;
2873 /* Return the size of TYPE (in bytes) as a wide integer
2874 or return -1 if the size can vary or is larger than an integer. */
2876 HOST_WIDE_INT
2877 int_size_in_bytes (const_tree type)
2879 tree t;
2881 if (type == error_mark_node)
2882 return 0;
2884 type = TYPE_MAIN_VARIANT (type);
2885 t = TYPE_SIZE_UNIT (type);
2887 if (t && tree_fits_uhwi_p (t))
2888 return TREE_INT_CST_LOW (t);
2889 else
2890 return -1;
2893 /* Return the maximum size of TYPE (in bytes) as a wide integer
2894 or return -1 if the size can vary or is larger than an integer. */
2896 HOST_WIDE_INT
2897 max_int_size_in_bytes (const_tree type)
2899 HOST_WIDE_INT size = -1;
2900 tree size_tree;
2902 /* If this is an array type, check for a possible MAX_SIZE attached. */
2904 if (TREE_CODE (type) == ARRAY_TYPE)
2906 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2908 if (size_tree && tree_fits_uhwi_p (size_tree))
2909 size = tree_to_uhwi (size_tree);
2912 /* If we still haven't been able to get a size, see if the language
2913 can compute a maximum size. */
2915 if (size == -1)
2917 size_tree = lang_hooks.types.max_size (type);
2919 if (size_tree && tree_fits_uhwi_p (size_tree))
2920 size = tree_to_uhwi (size_tree);
2923 return size;
2926 /* Return the bit position of FIELD, in bits from the start of the record.
2927 This is a tree of type bitsizetype. */
2929 tree
2930 bit_position (const_tree field)
2932 return bit_from_pos (DECL_FIELD_OFFSET (field),
2933 DECL_FIELD_BIT_OFFSET (field));
2936 /* Return the byte position of FIELD, in bytes from the start of the record.
2937 This is a tree of type sizetype. */
2939 tree
2940 byte_position (const_tree field)
2942 return byte_from_pos (DECL_FIELD_OFFSET (field),
2943 DECL_FIELD_BIT_OFFSET (field));
2946 /* Likewise, but return as an integer. It must be representable in
2947 that way (since it could be a signed value, we don't have the
2948 option of returning -1 like int_size_in_byte can. */
2950 HOST_WIDE_INT
2951 int_byte_position (const_tree field)
2953 return tree_to_shwi (byte_position (field));
2956 /* Return the strictest alignment, in bits, that T is known to have. */
2958 unsigned int
2959 expr_align (const_tree t)
2961 unsigned int align0, align1;
2963 switch (TREE_CODE (t))
2965 CASE_CONVERT: case NON_LVALUE_EXPR:
2966 /* If we have conversions, we know that the alignment of the
2967 object must meet each of the alignments of the types. */
2968 align0 = expr_align (TREE_OPERAND (t, 0));
2969 align1 = TYPE_ALIGN (TREE_TYPE (t));
2970 return MAX (align0, align1);
2972 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2973 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2974 case CLEANUP_POINT_EXPR:
2975 /* These don't change the alignment of an object. */
2976 return expr_align (TREE_OPERAND (t, 0));
2978 case COND_EXPR:
2979 /* The best we can do is say that the alignment is the least aligned
2980 of the two arms. */
2981 align0 = expr_align (TREE_OPERAND (t, 1));
2982 align1 = expr_align (TREE_OPERAND (t, 2));
2983 return MIN (align0, align1);
2985 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2986 meaningfully, it's always 1. */
2987 case LABEL_DECL: case CONST_DECL:
2988 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2989 case FUNCTION_DECL:
2990 gcc_assert (DECL_ALIGN (t) != 0);
2991 return DECL_ALIGN (t);
2993 default:
2994 break;
2997 /* Otherwise take the alignment from that of the type. */
2998 return TYPE_ALIGN (TREE_TYPE (t));
3001 /* Return, as a tree node, the number of elements for TYPE (which is an
3002 ARRAY_TYPE) minus one. This counts only elements of the top array. */
3004 tree
3005 array_type_nelts (const_tree type)
3007 tree index_type, min, max;
3009 /* If they did it with unspecified bounds, then we should have already
3010 given an error about it before we got here. */
3011 if (! TYPE_DOMAIN (type))
3012 return error_mark_node;
3014 index_type = TYPE_DOMAIN (type);
3015 min = TYPE_MIN_VALUE (index_type);
3016 max = TYPE_MAX_VALUE (index_type);
3018 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
3019 if (!max)
3020 return error_mark_node;
3022 return (integer_zerop (min)
3023 ? max
3024 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
3027 /* If arg is static -- a reference to an object in static storage -- then
3028 return the object. This is not the same as the C meaning of `static'.
3029 If arg isn't static, return NULL. */
3031 tree
3032 staticp (tree arg)
3034 switch (TREE_CODE (arg))
3036 case FUNCTION_DECL:
3037 /* Nested functions are static, even though taking their address will
3038 involve a trampoline as we unnest the nested function and create
3039 the trampoline on the tree level. */
3040 return arg;
3042 case VAR_DECL:
3043 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3044 && ! DECL_THREAD_LOCAL_P (arg)
3045 && ! DECL_DLLIMPORT_P (arg)
3046 ? arg : NULL);
3048 case CONST_DECL:
3049 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3050 ? arg : NULL);
3052 case CONSTRUCTOR:
3053 return TREE_STATIC (arg) ? arg : NULL;
3055 case LABEL_DECL:
3056 case STRING_CST:
3057 return arg;
3059 case COMPONENT_REF:
3060 /* If the thing being referenced is not a field, then it is
3061 something language specific. */
3062 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
3064 /* If we are referencing a bitfield, we can't evaluate an
3065 ADDR_EXPR at compile time and so it isn't a constant. */
3066 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
3067 return NULL;
3069 return staticp (TREE_OPERAND (arg, 0));
3071 case BIT_FIELD_REF:
3072 return NULL;
3074 case INDIRECT_REF:
3075 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
3077 case ARRAY_REF:
3078 case ARRAY_RANGE_REF:
3079 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
3080 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
3081 return staticp (TREE_OPERAND (arg, 0));
3082 else
3083 return NULL;
3085 case COMPOUND_LITERAL_EXPR:
3086 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
3088 default:
3089 return NULL;
3096 /* Return whether OP is a DECL whose address is function-invariant. */
3098 bool
3099 decl_address_invariant_p (const_tree op)
3101 /* The conditions below are slightly less strict than the one in
3102 staticp. */
3104 switch (TREE_CODE (op))
3106 case PARM_DECL:
3107 case RESULT_DECL:
3108 case LABEL_DECL:
3109 case FUNCTION_DECL:
3110 return true;
3112 case VAR_DECL:
3113 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3114 || DECL_THREAD_LOCAL_P (op)
3115 || DECL_CONTEXT (op) == current_function_decl
3116 || decl_function_context (op) == current_function_decl)
3117 return true;
3118 break;
3120 case CONST_DECL:
3121 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3122 || decl_function_context (op) == current_function_decl)
3123 return true;
3124 break;
3126 default:
3127 break;
3130 return false;
3133 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
3135 bool
3136 decl_address_ip_invariant_p (const_tree op)
3138 /* The conditions below are slightly less strict than the one in
3139 staticp. */
3141 switch (TREE_CODE (op))
3143 case LABEL_DECL:
3144 case FUNCTION_DECL:
3145 case STRING_CST:
3146 return true;
3148 case VAR_DECL:
3149 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
3150 && !DECL_DLLIMPORT_P (op))
3151 || DECL_THREAD_LOCAL_P (op))
3152 return true;
3153 break;
3155 case CONST_DECL:
3156 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
3157 return true;
3158 break;
3160 default:
3161 break;
3164 return false;
3168 /* Return true if T is function-invariant (internal function, does
3169 not handle arithmetic; that's handled in skip_simple_arithmetic and
3170 tree_invariant_p). */
3172 static bool tree_invariant_p (tree t);
3174 static bool
3175 tree_invariant_p_1 (tree t)
3177 tree op;
3179 if (TREE_CONSTANT (t)
3180 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
3181 return true;
3183 switch (TREE_CODE (t))
3185 case SAVE_EXPR:
3186 return true;
3188 case ADDR_EXPR:
3189 op = TREE_OPERAND (t, 0);
3190 while (handled_component_p (op))
3192 switch (TREE_CODE (op))
3194 case ARRAY_REF:
3195 case ARRAY_RANGE_REF:
3196 if (!tree_invariant_p (TREE_OPERAND (op, 1))
3197 || TREE_OPERAND (op, 2) != NULL_TREE
3198 || TREE_OPERAND (op, 3) != NULL_TREE)
3199 return false;
3200 break;
3202 case COMPONENT_REF:
3203 if (TREE_OPERAND (op, 2) != NULL_TREE)
3204 return false;
3205 break;
3207 default:;
3209 op = TREE_OPERAND (op, 0);
3212 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
3214 default:
3215 break;
3218 return false;
3221 /* Return true if T is function-invariant. */
3223 static bool
3224 tree_invariant_p (tree t)
3226 tree inner = skip_simple_arithmetic (t);
3227 return tree_invariant_p_1 (inner);
3230 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
3231 Do this to any expression which may be used in more than one place,
3232 but must be evaluated only once.
3234 Normally, expand_expr would reevaluate the expression each time.
3235 Calling save_expr produces something that is evaluated and recorded
3236 the first time expand_expr is called on it. Subsequent calls to
3237 expand_expr just reuse the recorded value.
3239 The call to expand_expr that generates code that actually computes
3240 the value is the first call *at compile time*. Subsequent calls
3241 *at compile time* generate code to use the saved value.
3242 This produces correct result provided that *at run time* control
3243 always flows through the insns made by the first expand_expr
3244 before reaching the other places where the save_expr was evaluated.
3245 You, the caller of save_expr, must make sure this is so.
3247 Constants, and certain read-only nodes, are returned with no
3248 SAVE_EXPR because that is safe. Expressions containing placeholders
3249 are not touched; see tree.def for an explanation of what these
3250 are used for. */
3252 tree
3253 save_expr (tree expr)
3255 tree t = fold (expr);
3256 tree inner;
3258 /* If the tree evaluates to a constant, then we don't want to hide that
3259 fact (i.e. this allows further folding, and direct checks for constants).
3260 However, a read-only object that has side effects cannot be bypassed.
3261 Since it is no problem to reevaluate literals, we just return the
3262 literal node. */
3263 inner = skip_simple_arithmetic (t);
3264 if (TREE_CODE (inner) == ERROR_MARK)
3265 return inner;
3267 if (tree_invariant_p_1 (inner))
3268 return t;
3270 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
3271 it means that the size or offset of some field of an object depends on
3272 the value within another field.
3274 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
3275 and some variable since it would then need to be both evaluated once and
3276 evaluated more than once. Front-ends must assure this case cannot
3277 happen by surrounding any such subexpressions in their own SAVE_EXPR
3278 and forcing evaluation at the proper time. */
3279 if (contains_placeholder_p (inner))
3280 return t;
3282 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
3283 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
3285 /* This expression might be placed ahead of a jump to ensure that the
3286 value was computed on both sides of the jump. So make sure it isn't
3287 eliminated as dead. */
3288 TREE_SIDE_EFFECTS (t) = 1;
3289 return t;
3292 /* Look inside EXPR into any simple arithmetic operations. Return the
3293 outermost non-arithmetic or non-invariant node. */
3295 tree
3296 skip_simple_arithmetic (tree expr)
3298 /* We don't care about whether this can be used as an lvalue in this
3299 context. */
3300 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3301 expr = TREE_OPERAND (expr, 0);
3303 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
3304 a constant, it will be more efficient to not make another SAVE_EXPR since
3305 it will allow better simplification and GCSE will be able to merge the
3306 computations if they actually occur. */
3307 while (true)
3309 if (UNARY_CLASS_P (expr))
3310 expr = TREE_OPERAND (expr, 0);
3311 else if (BINARY_CLASS_P (expr))
3313 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
3314 expr = TREE_OPERAND (expr, 0);
3315 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
3316 expr = TREE_OPERAND (expr, 1);
3317 else
3318 break;
3320 else
3321 break;
3324 return expr;
3327 /* Look inside EXPR into simple arithmetic operations involving constants.
3328 Return the outermost non-arithmetic or non-constant node. */
3330 tree
3331 skip_simple_constant_arithmetic (tree expr)
3333 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3334 expr = TREE_OPERAND (expr, 0);
3336 while (true)
3338 if (UNARY_CLASS_P (expr))
3339 expr = TREE_OPERAND (expr, 0);
3340 else if (BINARY_CLASS_P (expr))
3342 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3343 expr = TREE_OPERAND (expr, 0);
3344 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3345 expr = TREE_OPERAND (expr, 1);
3346 else
3347 break;
3349 else
3350 break;
3353 return expr;
3356 /* Return which tree structure is used by T. */
3358 enum tree_node_structure_enum
3359 tree_node_structure (const_tree t)
3361 const enum tree_code code = TREE_CODE (t);
3362 return tree_node_structure_for_code (code);
3365 /* Set various status flags when building a CALL_EXPR object T. */
3367 static void
3368 process_call_operands (tree t)
3370 bool side_effects = TREE_SIDE_EFFECTS (t);
3371 bool read_only = false;
3372 int i = call_expr_flags (t);
3374 /* Calls have side-effects, except those to const or pure functions. */
3375 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
3376 side_effects = true;
3377 /* Propagate TREE_READONLY of arguments for const functions. */
3378 if (i & ECF_CONST)
3379 read_only = true;
3381 if (!side_effects || read_only)
3382 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
3384 tree op = TREE_OPERAND (t, i);
3385 if (op && TREE_SIDE_EFFECTS (op))
3386 side_effects = true;
3387 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
3388 read_only = false;
3391 TREE_SIDE_EFFECTS (t) = side_effects;
3392 TREE_READONLY (t) = read_only;
3395 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
3396 size or offset that depends on a field within a record. */
3398 bool
3399 contains_placeholder_p (const_tree exp)
3401 enum tree_code code;
3403 if (!exp)
3404 return 0;
3406 code = TREE_CODE (exp);
3407 if (code == PLACEHOLDER_EXPR)
3408 return 1;
3410 switch (TREE_CODE_CLASS (code))
3412 case tcc_reference:
3413 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
3414 position computations since they will be converted into a
3415 WITH_RECORD_EXPR involving the reference, which will assume
3416 here will be valid. */
3417 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3419 case tcc_exceptional:
3420 if (code == TREE_LIST)
3421 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
3422 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
3423 break;
3425 case tcc_unary:
3426 case tcc_binary:
3427 case tcc_comparison:
3428 case tcc_expression:
3429 switch (code)
3431 case COMPOUND_EXPR:
3432 /* Ignoring the first operand isn't quite right, but works best. */
3433 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
3435 case COND_EXPR:
3436 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3437 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
3438 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
3440 case SAVE_EXPR:
3441 /* The save_expr function never wraps anything containing
3442 a PLACEHOLDER_EXPR. */
3443 return 0;
3445 default:
3446 break;
3449 switch (TREE_CODE_LENGTH (code))
3451 case 1:
3452 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3453 case 2:
3454 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3455 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
3456 default:
3457 return 0;
3460 case tcc_vl_exp:
3461 switch (code)
3463 case CALL_EXPR:
3465 const_tree arg;
3466 const_call_expr_arg_iterator iter;
3467 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
3468 if (CONTAINS_PLACEHOLDER_P (arg))
3469 return 1;
3470 return 0;
3472 default:
3473 return 0;
3476 default:
3477 return 0;
3479 return 0;
3482 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3483 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3484 field positions. */
3486 static bool
3487 type_contains_placeholder_1 (const_tree type)
3489 /* If the size contains a placeholder or the parent type (component type in
3490 the case of arrays) type involves a placeholder, this type does. */
3491 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3492 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3493 || (!POINTER_TYPE_P (type)
3494 && TREE_TYPE (type)
3495 && type_contains_placeholder_p (TREE_TYPE (type))))
3496 return true;
3498 /* Now do type-specific checks. Note that the last part of the check above
3499 greatly limits what we have to do below. */
3500 switch (TREE_CODE (type))
3502 case VOID_TYPE:
3503 case POINTER_BOUNDS_TYPE:
3504 case COMPLEX_TYPE:
3505 case ENUMERAL_TYPE:
3506 case BOOLEAN_TYPE:
3507 case POINTER_TYPE:
3508 case OFFSET_TYPE:
3509 case REFERENCE_TYPE:
3510 case METHOD_TYPE:
3511 case FUNCTION_TYPE:
3512 case VECTOR_TYPE:
3513 case NULLPTR_TYPE:
3514 return false;
3516 case INTEGER_TYPE:
3517 case REAL_TYPE:
3518 case FIXED_POINT_TYPE:
3519 /* Here we just check the bounds. */
3520 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3521 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3523 case ARRAY_TYPE:
3524 /* We have already checked the component type above, so just check the
3525 domain type. */
3526 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3528 case RECORD_TYPE:
3529 case UNION_TYPE:
3530 case QUAL_UNION_TYPE:
3532 tree field;
3534 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3535 if (TREE_CODE (field) == FIELD_DECL
3536 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3537 || (TREE_CODE (type) == QUAL_UNION_TYPE
3538 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3539 || type_contains_placeholder_p (TREE_TYPE (field))))
3540 return true;
3542 return false;
3545 default:
3546 gcc_unreachable ();
3550 /* Wrapper around above function used to cache its result. */
3552 bool
3553 type_contains_placeholder_p (tree type)
3555 bool result;
3557 /* If the contains_placeholder_bits field has been initialized,
3558 then we know the answer. */
3559 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3560 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3562 /* Indicate that we've seen this type node, and the answer is false.
3563 This is what we want to return if we run into recursion via fields. */
3564 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3566 /* Compute the real value. */
3567 result = type_contains_placeholder_1 (type);
3569 /* Store the real value. */
3570 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3572 return result;
3575 /* Push tree EXP onto vector QUEUE if it is not already present. */
3577 static void
3578 push_without_duplicates (tree exp, vec<tree> *queue)
3580 unsigned int i;
3581 tree iter;
3583 FOR_EACH_VEC_ELT (*queue, i, iter)
3584 if (simple_cst_equal (iter, exp) == 1)
3585 break;
3587 if (!iter)
3588 queue->safe_push (exp);
3591 /* Given a tree EXP, find all occurrences of references to fields
3592 in a PLACEHOLDER_EXPR and place them in vector REFS without
3593 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3594 we assume here that EXP contains only arithmetic expressions
3595 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3596 argument list. */
3598 void
3599 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3601 enum tree_code code = TREE_CODE (exp);
3602 tree inner;
3603 int i;
3605 /* We handle TREE_LIST and COMPONENT_REF separately. */
3606 if (code == TREE_LIST)
3608 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3609 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3611 else if (code == COMPONENT_REF)
3613 for (inner = TREE_OPERAND (exp, 0);
3614 REFERENCE_CLASS_P (inner);
3615 inner = TREE_OPERAND (inner, 0))
3618 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3619 push_without_duplicates (exp, refs);
3620 else
3621 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3623 else
3624 switch (TREE_CODE_CLASS (code))
3626 case tcc_constant:
3627 break;
3629 case tcc_declaration:
3630 /* Variables allocated to static storage can stay. */
3631 if (!TREE_STATIC (exp))
3632 push_without_duplicates (exp, refs);
3633 break;
3635 case tcc_expression:
3636 /* This is the pattern built in ada/make_aligning_type. */
3637 if (code == ADDR_EXPR
3638 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3640 push_without_duplicates (exp, refs);
3641 break;
3644 /* Fall through... */
3646 case tcc_exceptional:
3647 case tcc_unary:
3648 case tcc_binary:
3649 case tcc_comparison:
3650 case tcc_reference:
3651 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3652 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3653 break;
3655 case tcc_vl_exp:
3656 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3657 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3658 break;
3660 default:
3661 gcc_unreachable ();
3665 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3666 return a tree with all occurrences of references to F in a
3667 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3668 CONST_DECLs. Note that we assume here that EXP contains only
3669 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3670 occurring only in their argument list. */
3672 tree
3673 substitute_in_expr (tree exp, tree f, tree r)
3675 enum tree_code code = TREE_CODE (exp);
3676 tree op0, op1, op2, op3;
3677 tree new_tree;
3679 /* We handle TREE_LIST and COMPONENT_REF separately. */
3680 if (code == TREE_LIST)
3682 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3683 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3684 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3685 return exp;
3687 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3689 else if (code == COMPONENT_REF)
3691 tree inner;
3693 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3694 and it is the right field, replace it with R. */
3695 for (inner = TREE_OPERAND (exp, 0);
3696 REFERENCE_CLASS_P (inner);
3697 inner = TREE_OPERAND (inner, 0))
3700 /* The field. */
3701 op1 = TREE_OPERAND (exp, 1);
3703 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3704 return r;
3706 /* If this expression hasn't been completed let, leave it alone. */
3707 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3708 return exp;
3710 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3711 if (op0 == TREE_OPERAND (exp, 0))
3712 return exp;
3714 new_tree
3715 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3717 else
3718 switch (TREE_CODE_CLASS (code))
3720 case tcc_constant:
3721 return exp;
3723 case tcc_declaration:
3724 if (exp == f)
3725 return r;
3726 else
3727 return exp;
3729 case tcc_expression:
3730 if (exp == f)
3731 return r;
3733 /* Fall through... */
3735 case tcc_exceptional:
3736 case tcc_unary:
3737 case tcc_binary:
3738 case tcc_comparison:
3739 case tcc_reference:
3740 switch (TREE_CODE_LENGTH (code))
3742 case 0:
3743 return exp;
3745 case 1:
3746 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3747 if (op0 == TREE_OPERAND (exp, 0))
3748 return exp;
3750 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3751 break;
3753 case 2:
3754 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3755 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3757 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3758 return exp;
3760 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3761 break;
3763 case 3:
3764 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3765 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3766 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3768 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3769 && op2 == TREE_OPERAND (exp, 2))
3770 return exp;
3772 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3773 break;
3775 case 4:
3776 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3777 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3778 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3779 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3781 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3782 && op2 == TREE_OPERAND (exp, 2)
3783 && op3 == TREE_OPERAND (exp, 3))
3784 return exp;
3786 new_tree
3787 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3788 break;
3790 default:
3791 gcc_unreachable ();
3793 break;
3795 case tcc_vl_exp:
3797 int i;
3799 new_tree = NULL_TREE;
3801 /* If we are trying to replace F with a constant, inline back
3802 functions which do nothing else than computing a value from
3803 the arguments they are passed. This makes it possible to
3804 fold partially or entirely the replacement expression. */
3805 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3807 tree t = maybe_inline_call_in_expr (exp);
3808 if (t)
3809 return SUBSTITUTE_IN_EXPR (t, f, r);
3812 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3814 tree op = TREE_OPERAND (exp, i);
3815 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3816 if (new_op != op)
3818 if (!new_tree)
3819 new_tree = copy_node (exp);
3820 TREE_OPERAND (new_tree, i) = new_op;
3824 if (new_tree)
3826 new_tree = fold (new_tree);
3827 if (TREE_CODE (new_tree) == CALL_EXPR)
3828 process_call_operands (new_tree);
3830 else
3831 return exp;
3833 break;
3835 default:
3836 gcc_unreachable ();
3839 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3841 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3842 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3844 return new_tree;
3847 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3848 for it within OBJ, a tree that is an object or a chain of references. */
3850 tree
3851 substitute_placeholder_in_expr (tree exp, tree obj)
3853 enum tree_code code = TREE_CODE (exp);
3854 tree op0, op1, op2, op3;
3855 tree new_tree;
3857 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3858 in the chain of OBJ. */
3859 if (code == PLACEHOLDER_EXPR)
3861 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3862 tree elt;
3864 for (elt = obj; elt != 0;
3865 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3866 || TREE_CODE (elt) == COND_EXPR)
3867 ? TREE_OPERAND (elt, 1)
3868 : (REFERENCE_CLASS_P (elt)
3869 || UNARY_CLASS_P (elt)
3870 || BINARY_CLASS_P (elt)
3871 || VL_EXP_CLASS_P (elt)
3872 || EXPRESSION_CLASS_P (elt))
3873 ? TREE_OPERAND (elt, 0) : 0))
3874 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3875 return elt;
3877 for (elt = obj; elt != 0;
3878 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3879 || TREE_CODE (elt) == COND_EXPR)
3880 ? TREE_OPERAND (elt, 1)
3881 : (REFERENCE_CLASS_P (elt)
3882 || UNARY_CLASS_P (elt)
3883 || BINARY_CLASS_P (elt)
3884 || VL_EXP_CLASS_P (elt)
3885 || EXPRESSION_CLASS_P (elt))
3886 ? TREE_OPERAND (elt, 0) : 0))
3887 if (POINTER_TYPE_P (TREE_TYPE (elt))
3888 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3889 == need_type))
3890 return fold_build1 (INDIRECT_REF, need_type, elt);
3892 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3893 survives until RTL generation, there will be an error. */
3894 return exp;
3897 /* TREE_LIST is special because we need to look at TREE_VALUE
3898 and TREE_CHAIN, not TREE_OPERANDS. */
3899 else if (code == TREE_LIST)
3901 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3902 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3903 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3904 return exp;
3906 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3908 else
3909 switch (TREE_CODE_CLASS (code))
3911 case tcc_constant:
3912 case tcc_declaration:
3913 return exp;
3915 case tcc_exceptional:
3916 case tcc_unary:
3917 case tcc_binary:
3918 case tcc_comparison:
3919 case tcc_expression:
3920 case tcc_reference:
3921 case tcc_statement:
3922 switch (TREE_CODE_LENGTH (code))
3924 case 0:
3925 return exp;
3927 case 1:
3928 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3929 if (op0 == TREE_OPERAND (exp, 0))
3930 return exp;
3932 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3933 break;
3935 case 2:
3936 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3937 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3939 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3940 return exp;
3942 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3943 break;
3945 case 3:
3946 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3947 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3948 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3950 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3951 && op2 == TREE_OPERAND (exp, 2))
3952 return exp;
3954 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3955 break;
3957 case 4:
3958 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3959 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3960 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3961 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3963 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3964 && op2 == TREE_OPERAND (exp, 2)
3965 && op3 == TREE_OPERAND (exp, 3))
3966 return exp;
3968 new_tree
3969 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3970 break;
3972 default:
3973 gcc_unreachable ();
3975 break;
3977 case tcc_vl_exp:
3979 int i;
3981 new_tree = NULL_TREE;
3983 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3985 tree op = TREE_OPERAND (exp, i);
3986 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
3987 if (new_op != op)
3989 if (!new_tree)
3990 new_tree = copy_node (exp);
3991 TREE_OPERAND (new_tree, i) = new_op;
3995 if (new_tree)
3997 new_tree = fold (new_tree);
3998 if (TREE_CODE (new_tree) == CALL_EXPR)
3999 process_call_operands (new_tree);
4001 else
4002 return exp;
4004 break;
4006 default:
4007 gcc_unreachable ();
4010 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4012 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4013 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4015 return new_tree;
4019 /* Subroutine of stabilize_reference; this is called for subtrees of
4020 references. Any expression with side-effects must be put in a SAVE_EXPR
4021 to ensure that it is only evaluated once.
4023 We don't put SAVE_EXPR nodes around everything, because assigning very
4024 simple expressions to temporaries causes us to miss good opportunities
4025 for optimizations. Among other things, the opportunity to fold in the
4026 addition of a constant into an addressing mode often gets lost, e.g.
4027 "y[i+1] += x;". In general, we take the approach that we should not make
4028 an assignment unless we are forced into it - i.e., that any non-side effect
4029 operator should be allowed, and that cse should take care of coalescing
4030 multiple utterances of the same expression should that prove fruitful. */
4032 static tree
4033 stabilize_reference_1 (tree e)
4035 tree result;
4036 enum tree_code code = TREE_CODE (e);
4038 /* We cannot ignore const expressions because it might be a reference
4039 to a const array but whose index contains side-effects. But we can
4040 ignore things that are actual constant or that already have been
4041 handled by this function. */
4043 if (tree_invariant_p (e))
4044 return e;
4046 switch (TREE_CODE_CLASS (code))
4048 case tcc_exceptional:
4049 case tcc_type:
4050 case tcc_declaration:
4051 case tcc_comparison:
4052 case tcc_statement:
4053 case tcc_expression:
4054 case tcc_reference:
4055 case tcc_vl_exp:
4056 /* If the expression has side-effects, then encase it in a SAVE_EXPR
4057 so that it will only be evaluated once. */
4058 /* The reference (r) and comparison (<) classes could be handled as
4059 below, but it is generally faster to only evaluate them once. */
4060 if (TREE_SIDE_EFFECTS (e))
4061 return save_expr (e);
4062 return e;
4064 case tcc_constant:
4065 /* Constants need no processing. In fact, we should never reach
4066 here. */
4067 return e;
4069 case tcc_binary:
4070 /* Division is slow and tends to be compiled with jumps,
4071 especially the division by powers of 2 that is often
4072 found inside of an array reference. So do it just once. */
4073 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
4074 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
4075 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
4076 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
4077 return save_expr (e);
4078 /* Recursively stabilize each operand. */
4079 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
4080 stabilize_reference_1 (TREE_OPERAND (e, 1)));
4081 break;
4083 case tcc_unary:
4084 /* Recursively stabilize each operand. */
4085 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
4086 break;
4088 default:
4089 gcc_unreachable ();
4092 TREE_TYPE (result) = TREE_TYPE (e);
4093 TREE_READONLY (result) = TREE_READONLY (e);
4094 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
4095 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
4097 return result;
4100 /* Stabilize a reference so that we can use it any number of times
4101 without causing its operands to be evaluated more than once.
4102 Returns the stabilized reference. This works by means of save_expr,
4103 so see the caveats in the comments about save_expr.
4105 Also allows conversion expressions whose operands are references.
4106 Any other kind of expression is returned unchanged. */
4108 tree
4109 stabilize_reference (tree ref)
4111 tree result;
4112 enum tree_code code = TREE_CODE (ref);
4114 switch (code)
4116 case VAR_DECL:
4117 case PARM_DECL:
4118 case RESULT_DECL:
4119 /* No action is needed in this case. */
4120 return ref;
4122 CASE_CONVERT:
4123 case FLOAT_EXPR:
4124 case FIX_TRUNC_EXPR:
4125 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
4126 break;
4128 case INDIRECT_REF:
4129 result = build_nt (INDIRECT_REF,
4130 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
4131 break;
4133 case COMPONENT_REF:
4134 result = build_nt (COMPONENT_REF,
4135 stabilize_reference (TREE_OPERAND (ref, 0)),
4136 TREE_OPERAND (ref, 1), NULL_TREE);
4137 break;
4139 case BIT_FIELD_REF:
4140 result = build_nt (BIT_FIELD_REF,
4141 stabilize_reference (TREE_OPERAND (ref, 0)),
4142 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
4143 break;
4145 case ARRAY_REF:
4146 result = build_nt (ARRAY_REF,
4147 stabilize_reference (TREE_OPERAND (ref, 0)),
4148 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4149 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4150 break;
4152 case ARRAY_RANGE_REF:
4153 result = build_nt (ARRAY_RANGE_REF,
4154 stabilize_reference (TREE_OPERAND (ref, 0)),
4155 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4156 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4157 break;
4159 case COMPOUND_EXPR:
4160 /* We cannot wrap the first expression in a SAVE_EXPR, as then
4161 it wouldn't be ignored. This matters when dealing with
4162 volatiles. */
4163 return stabilize_reference_1 (ref);
4165 /* If arg isn't a kind of lvalue we recognize, make no change.
4166 Caller should recognize the error for an invalid lvalue. */
4167 default:
4168 return ref;
4170 case ERROR_MARK:
4171 return error_mark_node;
4174 TREE_TYPE (result) = TREE_TYPE (ref);
4175 TREE_READONLY (result) = TREE_READONLY (ref);
4176 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
4177 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
4179 return result;
4182 /* Low-level constructors for expressions. */
4184 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
4185 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
4187 void
4188 recompute_tree_invariant_for_addr_expr (tree t)
4190 tree node;
4191 bool tc = true, se = false;
4193 /* We started out assuming this address is both invariant and constant, but
4194 does not have side effects. Now go down any handled components and see if
4195 any of them involve offsets that are either non-constant or non-invariant.
4196 Also check for side-effects.
4198 ??? Note that this code makes no attempt to deal with the case where
4199 taking the address of something causes a copy due to misalignment. */
4201 #define UPDATE_FLAGS(NODE) \
4202 do { tree _node = (NODE); \
4203 if (_node && !TREE_CONSTANT (_node)) tc = false; \
4204 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
4206 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
4207 node = TREE_OPERAND (node, 0))
4209 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
4210 array reference (probably made temporarily by the G++ front end),
4211 so ignore all the operands. */
4212 if ((TREE_CODE (node) == ARRAY_REF
4213 || TREE_CODE (node) == ARRAY_RANGE_REF)
4214 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
4216 UPDATE_FLAGS (TREE_OPERAND (node, 1));
4217 if (TREE_OPERAND (node, 2))
4218 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4219 if (TREE_OPERAND (node, 3))
4220 UPDATE_FLAGS (TREE_OPERAND (node, 3));
4222 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
4223 FIELD_DECL, apparently. The G++ front end can put something else
4224 there, at least temporarily. */
4225 else if (TREE_CODE (node) == COMPONENT_REF
4226 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
4228 if (TREE_OPERAND (node, 2))
4229 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4233 node = lang_hooks.expr_to_decl (node, &tc, &se);
4235 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4236 the address, since &(*a)->b is a form of addition. If it's a constant, the
4237 address is constant too. If it's a decl, its address is constant if the
4238 decl is static. Everything else is not constant and, furthermore,
4239 taking the address of a volatile variable is not volatile. */
4240 if (TREE_CODE (node) == INDIRECT_REF
4241 || TREE_CODE (node) == MEM_REF)
4242 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4243 else if (CONSTANT_CLASS_P (node))
4245 else if (DECL_P (node))
4246 tc &= (staticp (node) != NULL_TREE);
4247 else
4249 tc = false;
4250 se |= TREE_SIDE_EFFECTS (node);
4254 TREE_CONSTANT (t) = tc;
4255 TREE_SIDE_EFFECTS (t) = se;
4256 #undef UPDATE_FLAGS
4259 /* Build an expression of code CODE, data type TYPE, and operands as
4260 specified. Expressions and reference nodes can be created this way.
4261 Constants, decls, types and misc nodes cannot be.
4263 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4264 enough for all extant tree codes. */
4266 tree
4267 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
4269 tree t;
4271 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4273 t = make_node_stat (code PASS_MEM_STAT);
4274 TREE_TYPE (t) = tt;
4276 return t;
4279 tree
4280 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4282 int length = sizeof (struct tree_exp);
4283 tree t;
4285 record_node_allocation_statistics (code, length);
4287 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4289 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4291 memset (t, 0, sizeof (struct tree_common));
4293 TREE_SET_CODE (t, code);
4295 TREE_TYPE (t) = type;
4296 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4297 TREE_OPERAND (t, 0) = node;
4298 if (node && !TYPE_P (node))
4300 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4301 TREE_READONLY (t) = TREE_READONLY (node);
4304 if (TREE_CODE_CLASS (code) == tcc_statement)
4305 TREE_SIDE_EFFECTS (t) = 1;
4306 else switch (code)
4308 case VA_ARG_EXPR:
4309 /* All of these have side-effects, no matter what their
4310 operands are. */
4311 TREE_SIDE_EFFECTS (t) = 1;
4312 TREE_READONLY (t) = 0;
4313 break;
4315 case INDIRECT_REF:
4316 /* Whether a dereference is readonly has nothing to do with whether
4317 its operand is readonly. */
4318 TREE_READONLY (t) = 0;
4319 break;
4321 case ADDR_EXPR:
4322 if (node)
4323 recompute_tree_invariant_for_addr_expr (t);
4324 break;
4326 default:
4327 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4328 && node && !TYPE_P (node)
4329 && TREE_CONSTANT (node))
4330 TREE_CONSTANT (t) = 1;
4331 if (TREE_CODE_CLASS (code) == tcc_reference
4332 && node && TREE_THIS_VOLATILE (node))
4333 TREE_THIS_VOLATILE (t) = 1;
4334 break;
4337 return t;
4340 #define PROCESS_ARG(N) \
4341 do { \
4342 TREE_OPERAND (t, N) = arg##N; \
4343 if (arg##N &&!TYPE_P (arg##N)) \
4345 if (TREE_SIDE_EFFECTS (arg##N)) \
4346 side_effects = 1; \
4347 if (!TREE_READONLY (arg##N) \
4348 && !CONSTANT_CLASS_P (arg##N)) \
4349 (void) (read_only = 0); \
4350 if (!TREE_CONSTANT (arg##N)) \
4351 (void) (constant = 0); \
4353 } while (0)
4355 tree
4356 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
4358 bool constant, read_only, side_effects;
4359 tree t;
4361 gcc_assert (TREE_CODE_LENGTH (code) == 2);
4363 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
4364 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
4365 /* When sizetype precision doesn't match that of pointers
4366 we need to be able to build explicit extensions or truncations
4367 of the offset argument. */
4368 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
4369 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
4370 && TREE_CODE (arg1) == INTEGER_CST);
4372 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
4373 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
4374 && ptrofftype_p (TREE_TYPE (arg1)));
4376 t = make_node_stat (code PASS_MEM_STAT);
4377 TREE_TYPE (t) = tt;
4379 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
4380 result based on those same flags for the arguments. But if the
4381 arguments aren't really even `tree' expressions, we shouldn't be trying
4382 to do this. */
4384 /* Expressions without side effects may be constant if their
4385 arguments are as well. */
4386 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4387 || TREE_CODE_CLASS (code) == tcc_binary);
4388 read_only = 1;
4389 side_effects = TREE_SIDE_EFFECTS (t);
4391 PROCESS_ARG (0);
4392 PROCESS_ARG (1);
4394 TREE_SIDE_EFFECTS (t) = side_effects;
4395 if (code == MEM_REF)
4397 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
4399 tree o = TREE_OPERAND (arg0, 0);
4400 TREE_READONLY (t) = TREE_READONLY (o);
4401 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
4404 else
4406 TREE_READONLY (t) = read_only;
4407 TREE_CONSTANT (t) = constant;
4408 TREE_THIS_VOLATILE (t)
4409 = (TREE_CODE_CLASS (code) == tcc_reference
4410 && arg0 && TREE_THIS_VOLATILE (arg0));
4413 return t;
4417 tree
4418 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4419 tree arg2 MEM_STAT_DECL)
4421 bool constant, read_only, side_effects;
4422 tree t;
4424 gcc_assert (TREE_CODE_LENGTH (code) == 3);
4425 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4427 t = make_node_stat (code PASS_MEM_STAT);
4428 TREE_TYPE (t) = tt;
4430 read_only = 1;
4432 /* As a special exception, if COND_EXPR has NULL branches, we
4433 assume that it is a gimple statement and always consider
4434 it to have side effects. */
4435 if (code == COND_EXPR
4436 && tt == void_type_node
4437 && arg1 == NULL_TREE
4438 && arg2 == NULL_TREE)
4439 side_effects = true;
4440 else
4441 side_effects = TREE_SIDE_EFFECTS (t);
4443 PROCESS_ARG (0);
4444 PROCESS_ARG (1);
4445 PROCESS_ARG (2);
4447 if (code == COND_EXPR)
4448 TREE_READONLY (t) = read_only;
4450 TREE_SIDE_EFFECTS (t) = side_effects;
4451 TREE_THIS_VOLATILE (t)
4452 = (TREE_CODE_CLASS (code) == tcc_reference
4453 && arg0 && TREE_THIS_VOLATILE (arg0));
4455 return t;
4458 tree
4459 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4460 tree arg2, tree arg3 MEM_STAT_DECL)
4462 bool constant, read_only, side_effects;
4463 tree t;
4465 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4467 t = make_node_stat (code PASS_MEM_STAT);
4468 TREE_TYPE (t) = tt;
4470 side_effects = TREE_SIDE_EFFECTS (t);
4472 PROCESS_ARG (0);
4473 PROCESS_ARG (1);
4474 PROCESS_ARG (2);
4475 PROCESS_ARG (3);
4477 TREE_SIDE_EFFECTS (t) = side_effects;
4478 TREE_THIS_VOLATILE (t)
4479 = (TREE_CODE_CLASS (code) == tcc_reference
4480 && arg0 && TREE_THIS_VOLATILE (arg0));
4482 return t;
4485 tree
4486 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4487 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4489 bool constant, read_only, side_effects;
4490 tree t;
4492 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4494 t = make_node_stat (code PASS_MEM_STAT);
4495 TREE_TYPE (t) = tt;
4497 side_effects = TREE_SIDE_EFFECTS (t);
4499 PROCESS_ARG (0);
4500 PROCESS_ARG (1);
4501 PROCESS_ARG (2);
4502 PROCESS_ARG (3);
4503 PROCESS_ARG (4);
4505 TREE_SIDE_EFFECTS (t) = side_effects;
4506 if (code == TARGET_MEM_REF)
4508 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
4510 tree o = TREE_OPERAND (arg0, 0);
4511 TREE_READONLY (t) = TREE_READONLY (o);
4512 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
4515 else
4516 TREE_THIS_VOLATILE (t)
4517 = (TREE_CODE_CLASS (code) == tcc_reference
4518 && arg0 && TREE_THIS_VOLATILE (arg0));
4520 return t;
4523 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4524 on the pointer PTR. */
4526 tree
4527 build_simple_mem_ref_loc (location_t loc, tree ptr)
4529 HOST_WIDE_INT offset = 0;
4530 tree ptype = TREE_TYPE (ptr);
4531 tree tem;
4532 /* For convenience allow addresses that collapse to a simple base
4533 and offset. */
4534 if (TREE_CODE (ptr) == ADDR_EXPR
4535 && (handled_component_p (TREE_OPERAND (ptr, 0))
4536 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4538 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4539 gcc_assert (ptr);
4540 ptr = build_fold_addr_expr (ptr);
4541 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4543 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4544 ptr, build_int_cst (ptype, offset));
4545 SET_EXPR_LOCATION (tem, loc);
4546 return tem;
4549 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4551 offset_int
4552 mem_ref_offset (const_tree t)
4554 return offset_int::from (TREE_OPERAND (t, 1), SIGNED);
4557 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4558 offsetted by OFFSET units. */
4560 tree
4561 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4563 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4564 build_fold_addr_expr (base),
4565 build_int_cst (ptr_type_node, offset));
4566 tree addr = build1 (ADDR_EXPR, type, ref);
4567 recompute_tree_invariant_for_addr_expr (addr);
4568 return addr;
4571 /* Similar except don't specify the TREE_TYPE
4572 and leave the TREE_SIDE_EFFECTS as 0.
4573 It is permissible for arguments to be null,
4574 or even garbage if their values do not matter. */
4576 tree
4577 build_nt (enum tree_code code, ...)
4579 tree t;
4580 int length;
4581 int i;
4582 va_list p;
4584 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4586 va_start (p, code);
4588 t = make_node (code);
4589 length = TREE_CODE_LENGTH (code);
4591 for (i = 0; i < length; i++)
4592 TREE_OPERAND (t, i) = va_arg (p, tree);
4594 va_end (p);
4595 return t;
4598 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4599 tree vec. */
4601 tree
4602 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4604 tree ret, t;
4605 unsigned int ix;
4607 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4608 CALL_EXPR_FN (ret) = fn;
4609 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4610 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4611 CALL_EXPR_ARG (ret, ix) = t;
4612 return ret;
4615 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4616 We do NOT enter this node in any sort of symbol table.
4618 LOC is the location of the decl.
4620 layout_decl is used to set up the decl's storage layout.
4621 Other slots are initialized to 0 or null pointers. */
4623 tree
4624 build_decl_stat (location_t loc, enum tree_code code, tree name,
4625 tree type MEM_STAT_DECL)
4627 tree t;
4629 t = make_node_stat (code PASS_MEM_STAT);
4630 DECL_SOURCE_LOCATION (t) = loc;
4632 /* if (type == error_mark_node)
4633 type = integer_type_node; */
4634 /* That is not done, deliberately, so that having error_mark_node
4635 as the type can suppress useless errors in the use of this variable. */
4637 DECL_NAME (t) = name;
4638 TREE_TYPE (t) = type;
4640 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4641 layout_decl (t, 0);
4643 return t;
4646 /* Builds and returns function declaration with NAME and TYPE. */
4648 tree
4649 build_fn_decl (const char *name, tree type)
4651 tree id = get_identifier (name);
4652 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4654 DECL_EXTERNAL (decl) = 1;
4655 TREE_PUBLIC (decl) = 1;
4656 DECL_ARTIFICIAL (decl) = 1;
4657 TREE_NOTHROW (decl) = 1;
4659 return decl;
4662 vec<tree, va_gc> *all_translation_units;
4664 /* Builds a new translation-unit decl with name NAME, queues it in the
4665 global list of translation-unit decls and returns it. */
4667 tree
4668 build_translation_unit_decl (tree name)
4670 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4671 name, NULL_TREE);
4672 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4673 vec_safe_push (all_translation_units, tu);
4674 return tu;
4678 /* BLOCK nodes are used to represent the structure of binding contours
4679 and declarations, once those contours have been exited and their contents
4680 compiled. This information is used for outputting debugging info. */
4682 tree
4683 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4685 tree block = make_node (BLOCK);
4687 BLOCK_VARS (block) = vars;
4688 BLOCK_SUBBLOCKS (block) = subblocks;
4689 BLOCK_SUPERCONTEXT (block) = supercontext;
4690 BLOCK_CHAIN (block) = chain;
4691 return block;
4695 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4697 LOC is the location to use in tree T. */
4699 void
4700 protected_set_expr_location (tree t, location_t loc)
4702 if (CAN_HAVE_LOCATION_P (t))
4703 SET_EXPR_LOCATION (t, loc);
4706 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4707 is ATTRIBUTE. */
4709 tree
4710 build_decl_attribute_variant (tree ddecl, tree attribute)
4712 DECL_ATTRIBUTES (ddecl) = attribute;
4713 return ddecl;
4716 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4717 is ATTRIBUTE and its qualifiers are QUALS.
4719 Record such modified types already made so we don't make duplicates. */
4721 tree
4722 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4724 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4726 inchash::hash hstate;
4727 tree ntype;
4728 int i;
4729 tree t;
4730 enum tree_code code = TREE_CODE (ttype);
4732 /* Building a distinct copy of a tagged type is inappropriate; it
4733 causes breakage in code that expects there to be a one-to-one
4734 relationship between a struct and its fields.
4735 build_duplicate_type is another solution (as used in
4736 handle_transparent_union_attribute), but that doesn't play well
4737 with the stronger C++ type identity model. */
4738 if (TREE_CODE (ttype) == RECORD_TYPE
4739 || TREE_CODE (ttype) == UNION_TYPE
4740 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4741 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4743 warning (OPT_Wattributes,
4744 "ignoring attributes applied to %qT after definition",
4745 TYPE_MAIN_VARIANT (ttype));
4746 return build_qualified_type (ttype, quals);
4749 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4750 ntype = build_distinct_type_copy (ttype);
4752 TYPE_ATTRIBUTES (ntype) = attribute;
4754 hstate.add_int (code);
4755 if (TREE_TYPE (ntype))
4756 hstate.add_object (TYPE_HASH (TREE_TYPE (ntype)));
4757 attribute_hash_list (attribute, hstate);
4759 switch (TREE_CODE (ntype))
4761 case FUNCTION_TYPE:
4762 type_hash_list (TYPE_ARG_TYPES (ntype), hstate);
4763 break;
4764 case ARRAY_TYPE:
4765 if (TYPE_DOMAIN (ntype))
4766 hstate.add_object (TYPE_HASH (TYPE_DOMAIN (ntype)));
4767 break;
4768 case INTEGER_TYPE:
4769 t = TYPE_MAX_VALUE (ntype);
4770 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
4771 hstate.add_object (TREE_INT_CST_ELT (t, i));
4772 break;
4773 case REAL_TYPE:
4774 case FIXED_POINT_TYPE:
4776 unsigned int precision = TYPE_PRECISION (ntype);
4777 hstate.add_object (precision);
4779 break;
4780 default:
4781 break;
4784 ntype = type_hash_canon (hstate.end(), ntype);
4786 /* If the target-dependent attributes make NTYPE different from
4787 its canonical type, we will need to use structural equality
4788 checks for this type. */
4789 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4790 || !comp_type_attributes (ntype, ttype))
4791 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4792 else if (TYPE_CANONICAL (ntype) == ntype)
4793 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4795 ttype = build_qualified_type (ntype, quals);
4797 else if (TYPE_QUALS (ttype) != quals)
4798 ttype = build_qualified_type (ttype, quals);
4800 return ttype;
4803 /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
4804 the same. */
4806 static bool
4807 omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
4809 tree cl1, cl2;
4810 for (cl1 = clauses1, cl2 = clauses2;
4811 cl1 && cl2;
4812 cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
4814 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
4815 return false;
4816 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
4818 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
4819 OMP_CLAUSE_DECL (cl2)) != 1)
4820 return false;
4822 switch (OMP_CLAUSE_CODE (cl1))
4824 case OMP_CLAUSE_ALIGNED:
4825 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
4826 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
4827 return false;
4828 break;
4829 case OMP_CLAUSE_LINEAR:
4830 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
4831 OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
4832 return false;
4833 break;
4834 case OMP_CLAUSE_SIMDLEN:
4835 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
4836 OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
4837 return false;
4838 default:
4839 break;
4842 return true;
4845 /* Compare two constructor-element-type constants. Return 1 if the lists
4846 are known to be equal; otherwise return 0. */
4848 static bool
4849 simple_cst_list_equal (const_tree l1, const_tree l2)
4851 while (l1 != NULL_TREE && l2 != NULL_TREE)
4853 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
4854 return false;
4856 l1 = TREE_CHAIN (l1);
4857 l2 = TREE_CHAIN (l2);
4860 return l1 == l2;
4863 /* Compare two identifier nodes representing attributes. Either one may
4864 be in wrapped __ATTR__ form. Return true if they are the same, false
4865 otherwise. */
4867 static bool
4868 cmp_attrib_identifiers (const_tree attr1, const_tree attr2)
4870 /* Make sure we're dealing with IDENTIFIER_NODEs. */
4871 gcc_checking_assert (TREE_CODE (attr1) == IDENTIFIER_NODE
4872 && TREE_CODE (attr2) == IDENTIFIER_NODE);
4874 /* Identifiers can be compared directly for equality. */
4875 if (attr1 == attr2)
4876 return true;
4878 /* If they are not equal, they may still be one in the form
4879 'text' while the other one is in the form '__text__'. TODO:
4880 If we were storing attributes in normalized 'text' form, then
4881 this could all go away and we could take full advantage of
4882 the fact that we're comparing identifiers. :-) */
4883 const size_t attr1_len = IDENTIFIER_LENGTH (attr1);
4884 const size_t attr2_len = IDENTIFIER_LENGTH (attr2);
4886 if (attr2_len == attr1_len + 4)
4888 const char *p = IDENTIFIER_POINTER (attr2);
4889 const char *q = IDENTIFIER_POINTER (attr1);
4890 if (p[0] == '_' && p[1] == '_'
4891 && p[attr2_len - 2] == '_' && p[attr2_len - 1] == '_'
4892 && strncmp (q, p + 2, attr1_len) == 0)
4893 return true;;
4895 else if (attr2_len + 4 == attr1_len)
4897 const char *p = IDENTIFIER_POINTER (attr2);
4898 const char *q = IDENTIFIER_POINTER (attr1);
4899 if (q[0] == '_' && q[1] == '_'
4900 && q[attr1_len - 2] == '_' && q[attr1_len - 1] == '_'
4901 && strncmp (q + 2, p, attr2_len) == 0)
4902 return true;
4905 return false;
4908 /* Compare two attributes for their value identity. Return true if the
4909 attribute values are known to be equal; otherwise return false. */
4911 bool
4912 attribute_value_equal (const_tree attr1, const_tree attr2)
4914 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4915 return true;
4917 if (TREE_VALUE (attr1) != NULL_TREE
4918 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4919 && TREE_VALUE (attr2) != NULL_TREE
4920 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4922 /* Handle attribute format. */
4923 if (is_attribute_p ("format", TREE_PURPOSE (attr1)))
4925 attr1 = TREE_VALUE (attr1);
4926 attr2 = TREE_VALUE (attr2);
4927 /* Compare the archetypes (printf/scanf/strftime/...). */
4928 if (!cmp_attrib_identifiers (TREE_VALUE (attr1),
4929 TREE_VALUE (attr2)))
4930 return false;
4931 /* Archetypes are the same. Compare the rest. */
4932 return (simple_cst_list_equal (TREE_CHAIN (attr1),
4933 TREE_CHAIN (attr2)) == 1);
4935 return (simple_cst_list_equal (TREE_VALUE (attr1),
4936 TREE_VALUE (attr2)) == 1);
4939 if ((flag_openmp || flag_openmp_simd)
4940 && TREE_VALUE (attr1) && TREE_VALUE (attr2)
4941 && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
4942 && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
4943 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
4944 TREE_VALUE (attr2));
4946 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4949 /* Return 0 if the attributes for two types are incompatible, 1 if they
4950 are compatible, and 2 if they are nearly compatible (which causes a
4951 warning to be generated). */
4953 comp_type_attributes (const_tree type1, const_tree type2)
4955 const_tree a1 = TYPE_ATTRIBUTES (type1);
4956 const_tree a2 = TYPE_ATTRIBUTES (type2);
4957 const_tree a;
4959 if (a1 == a2)
4960 return 1;
4961 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4963 const struct attribute_spec *as;
4964 const_tree attr;
4966 as = lookup_attribute_spec (get_attribute_name (a));
4967 if (!as || as->affects_type_identity == false)
4968 continue;
4970 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4971 if (!attr || !attribute_value_equal (a, attr))
4972 break;
4974 if (!a)
4976 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
4978 const struct attribute_spec *as;
4980 as = lookup_attribute_spec (get_attribute_name (a));
4981 if (!as || as->affects_type_identity == false)
4982 continue;
4984 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
4985 break;
4986 /* We don't need to compare trees again, as we did this
4987 already in first loop. */
4989 /* All types - affecting identity - are equal, so
4990 there is no need to call target hook for comparison. */
4991 if (!a)
4992 return 1;
4994 /* As some type combinations - like default calling-convention - might
4995 be compatible, we have to call the target hook to get the final result. */
4996 return targetm.comp_type_attributes (type1, type2);
4999 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
5000 is ATTRIBUTE.
5002 Record such modified types already made so we don't make duplicates. */
5004 tree
5005 build_type_attribute_variant (tree ttype, tree attribute)
5007 return build_type_attribute_qual_variant (ttype, attribute,
5008 TYPE_QUALS (ttype));
5012 /* Reset the expression *EXPR_P, a size or position.
5014 ??? We could reset all non-constant sizes or positions. But it's cheap
5015 enough to not do so and refrain from adding workarounds to dwarf2out.c.
5017 We need to reset self-referential sizes or positions because they cannot
5018 be gimplified and thus can contain a CALL_EXPR after the gimplification
5019 is finished, which will run afoul of LTO streaming. And they need to be
5020 reset to something essentially dummy but not constant, so as to preserve
5021 the properties of the object they are attached to. */
5023 static inline void
5024 free_lang_data_in_one_sizepos (tree *expr_p)
5026 tree expr = *expr_p;
5027 if (CONTAINS_PLACEHOLDER_P (expr))
5028 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
5032 /* Reset all the fields in a binfo node BINFO. We only keep
5033 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
5035 static void
5036 free_lang_data_in_binfo (tree binfo)
5038 unsigned i;
5039 tree t;
5041 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
5043 BINFO_VIRTUALS (binfo) = NULL_TREE;
5044 BINFO_BASE_ACCESSES (binfo) = NULL;
5045 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
5046 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
5048 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
5049 free_lang_data_in_binfo (t);
5053 /* Reset all language specific information still present in TYPE. */
5055 static void
5056 free_lang_data_in_type (tree type)
5058 gcc_assert (TYPE_P (type));
5060 /* Give the FE a chance to remove its own data first. */
5061 lang_hooks.free_lang_data (type);
5063 TREE_LANG_FLAG_0 (type) = 0;
5064 TREE_LANG_FLAG_1 (type) = 0;
5065 TREE_LANG_FLAG_2 (type) = 0;
5066 TREE_LANG_FLAG_3 (type) = 0;
5067 TREE_LANG_FLAG_4 (type) = 0;
5068 TREE_LANG_FLAG_5 (type) = 0;
5069 TREE_LANG_FLAG_6 (type) = 0;
5071 if (TREE_CODE (type) == FUNCTION_TYPE)
5073 /* Remove the const and volatile qualifiers from arguments. The
5074 C++ front end removes them, but the C front end does not,
5075 leading to false ODR violation errors when merging two
5076 instances of the same function signature compiled by
5077 different front ends. */
5078 tree p;
5080 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
5082 tree arg_type = TREE_VALUE (p);
5084 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
5086 int quals = TYPE_QUALS (arg_type)
5087 & ~TYPE_QUAL_CONST
5088 & ~TYPE_QUAL_VOLATILE;
5089 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
5090 free_lang_data_in_type (TREE_VALUE (p));
5092 /* C++ FE uses TREE_PURPOSE to store initial values. */
5093 TREE_PURPOSE (p) = NULL;
5095 /* Java uses TYPE_MINVAL for TYPE_ARGUMENT_SIGNATURE. */
5096 TYPE_MINVAL (type) = NULL;
5098 if (TREE_CODE (type) == METHOD_TYPE)
5100 tree p;
5102 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
5104 /* C++ FE uses TREE_PURPOSE to store initial values. */
5105 TREE_PURPOSE (p) = NULL;
5107 /* Java uses TYPE_MINVAL for TYPE_ARGUMENT_SIGNATURE. */
5108 TYPE_MINVAL (type) = NULL;
5111 /* Remove members that are not actually FIELD_DECLs from the field
5112 list of an aggregate. These occur in C++. */
5113 if (RECORD_OR_UNION_TYPE_P (type))
5115 tree prev, member;
5117 /* Note that TYPE_FIELDS can be shared across distinct
5118 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
5119 to be removed, we cannot set its TREE_CHAIN to NULL.
5120 Otherwise, we would not be able to find all the other fields
5121 in the other instances of this TREE_TYPE.
5123 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
5124 prev = NULL_TREE;
5125 member = TYPE_FIELDS (type);
5126 while (member)
5128 if (TREE_CODE (member) == FIELD_DECL
5129 || TREE_CODE (member) == TYPE_DECL)
5131 if (prev)
5132 TREE_CHAIN (prev) = member;
5133 else
5134 TYPE_FIELDS (type) = member;
5135 prev = member;
5138 member = TREE_CHAIN (member);
5141 if (prev)
5142 TREE_CHAIN (prev) = NULL_TREE;
5143 else
5144 TYPE_FIELDS (type) = NULL_TREE;
5146 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
5147 and danagle the pointer from time to time. */
5148 if (TYPE_VFIELD (type) && TREE_CODE (TYPE_VFIELD (type)) != FIELD_DECL)
5149 TYPE_VFIELD (type) = NULL_TREE;
5151 /* Remove TYPE_METHODS list. While it would be nice to keep it
5152 to enable ODR warnings about different method lists, doing so
5153 seems to impractically increase size of LTO data streamed.
5154 Keep the infrmation if TYPE_METHODS was non-NULL. This is used
5155 by function.c and pretty printers. */
5156 if (TYPE_METHODS (type))
5157 TYPE_METHODS (type) = error_mark_node;
5158 if (TYPE_BINFO (type))
5160 free_lang_data_in_binfo (TYPE_BINFO (type));
5161 /* We need to preserve link to bases and virtual table for all
5162 polymorphic types to make devirtualization machinery working.
5163 Debug output cares only about bases, but output also
5164 virtual table pointers so merging of -fdevirtualize and
5165 -fno-devirtualize units is easier. */
5166 if ((!BINFO_VTABLE (TYPE_BINFO (type))
5167 || !flag_devirtualize)
5168 && ((!BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
5169 && !BINFO_VTABLE (TYPE_BINFO (type)))
5170 || debug_info_level != DINFO_LEVEL_NONE))
5171 TYPE_BINFO (type) = NULL;
5174 else
5176 /* For non-aggregate types, clear out the language slot (which
5177 overloads TYPE_BINFO). */
5178 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
5180 if (INTEGRAL_TYPE_P (type)
5181 || SCALAR_FLOAT_TYPE_P (type)
5182 || FIXED_POINT_TYPE_P (type))
5184 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
5185 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
5189 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
5190 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
5192 if (TYPE_CONTEXT (type)
5193 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
5195 tree ctx = TYPE_CONTEXT (type);
5198 ctx = BLOCK_SUPERCONTEXT (ctx);
5200 while (ctx && TREE_CODE (ctx) == BLOCK);
5201 TYPE_CONTEXT (type) = ctx;
5206 /* Return true if DECL may need an assembler name to be set. */
5208 static inline bool
5209 need_assembler_name_p (tree decl)
5211 /* We use DECL_ASSEMBLER_NAME to hold mangled type names for One Definition
5212 Rule merging. This makes type_odr_p to return true on those types during
5213 LTO and by comparing the mangled name, we can say what types are intended
5214 to be equivalent across compilation unit.
5216 We do not store names of type_in_anonymous_namespace_p.
5218 Record, union and enumeration type have linkage that allows use
5219 to check type_in_anonymous_namespace_p. We do not mangle compound types
5220 that always can be compared structurally.
5222 Similarly for builtin types, we compare properties of their main variant.
5223 A special case are integer types where mangling do make differences
5224 between char/signed char/unsigned char etc. Storing name for these makes
5225 e.g. -fno-signed-char/-fsigned-char mismatches to be handled well.
5226 See cp/mangle.c:write_builtin_type for details. */
5228 if (flag_lto_odr_type_mering
5229 && TREE_CODE (decl) == TYPE_DECL
5230 && DECL_NAME (decl)
5231 && decl == TYPE_NAME (TREE_TYPE (decl))
5232 && !TYPE_ARTIFICIAL (TREE_TYPE (decl))
5233 && (type_with_linkage_p (TREE_TYPE (decl))
5234 || TREE_CODE (TREE_TYPE (decl)) == INTEGER_TYPE)
5235 && !variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5236 return !DECL_ASSEMBLER_NAME_SET_P (decl);
5237 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
5238 if (TREE_CODE (decl) != FUNCTION_DECL
5239 && TREE_CODE (decl) != VAR_DECL)
5240 return false;
5242 /* If DECL already has its assembler name set, it does not need a
5243 new one. */
5244 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
5245 || DECL_ASSEMBLER_NAME_SET_P (decl))
5246 return false;
5248 /* Abstract decls do not need an assembler name. */
5249 if (DECL_ABSTRACT_P (decl))
5250 return false;
5252 /* For VAR_DECLs, only static, public and external symbols need an
5253 assembler name. */
5254 if (TREE_CODE (decl) == VAR_DECL
5255 && !TREE_STATIC (decl)
5256 && !TREE_PUBLIC (decl)
5257 && !DECL_EXTERNAL (decl))
5258 return false;
5260 if (TREE_CODE (decl) == FUNCTION_DECL)
5262 /* Do not set assembler name on builtins. Allow RTL expansion to
5263 decide whether to expand inline or via a regular call. */
5264 if (DECL_BUILT_IN (decl)
5265 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
5266 return false;
5268 /* Functions represented in the callgraph need an assembler name. */
5269 if (cgraph_node::get (decl) != NULL)
5270 return true;
5272 /* Unused and not public functions don't need an assembler name. */
5273 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
5274 return false;
5277 return true;
5281 /* Reset all language specific information still present in symbol
5282 DECL. */
5284 static void
5285 free_lang_data_in_decl (tree decl)
5287 gcc_assert (DECL_P (decl));
5289 /* Give the FE a chance to remove its own data first. */
5290 lang_hooks.free_lang_data (decl);
5292 TREE_LANG_FLAG_0 (decl) = 0;
5293 TREE_LANG_FLAG_1 (decl) = 0;
5294 TREE_LANG_FLAG_2 (decl) = 0;
5295 TREE_LANG_FLAG_3 (decl) = 0;
5296 TREE_LANG_FLAG_4 (decl) = 0;
5297 TREE_LANG_FLAG_5 (decl) = 0;
5298 TREE_LANG_FLAG_6 (decl) = 0;
5300 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
5301 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
5302 if (TREE_CODE (decl) == FIELD_DECL)
5304 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
5305 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
5306 DECL_QUALIFIER (decl) = NULL_TREE;
5309 if (TREE_CODE (decl) == FUNCTION_DECL)
5311 struct cgraph_node *node;
5312 if (!(node = cgraph_node::get (decl))
5313 || (!node->definition && !node->clones))
5315 if (node)
5316 node->release_body ();
5317 else
5319 release_function_body (decl);
5320 DECL_ARGUMENTS (decl) = NULL;
5321 DECL_RESULT (decl) = NULL;
5322 DECL_INITIAL (decl) = error_mark_node;
5325 if (gimple_has_body_p (decl))
5327 tree t;
5329 /* If DECL has a gimple body, then the context for its
5330 arguments must be DECL. Otherwise, it doesn't really
5331 matter, as we will not be emitting any code for DECL. In
5332 general, there may be other instances of DECL created by
5333 the front end and since PARM_DECLs are generally shared,
5334 their DECL_CONTEXT changes as the replicas of DECL are
5335 created. The only time where DECL_CONTEXT is important
5336 is for the FUNCTION_DECLs that have a gimple body (since
5337 the PARM_DECL will be used in the function's body). */
5338 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
5339 DECL_CONTEXT (t) = decl;
5340 if (!DECL_FUNCTION_SPECIFIC_TARGET (decl))
5341 DECL_FUNCTION_SPECIFIC_TARGET (decl)
5342 = target_option_default_node;
5343 if (!DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl))
5344 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl)
5345 = optimization_default_node;
5348 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
5349 At this point, it is not needed anymore. */
5350 DECL_SAVED_TREE (decl) = NULL_TREE;
5352 /* Clear the abstract origin if it refers to a method. Otherwise
5353 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
5354 origin will not be output correctly. */
5355 if (DECL_ABSTRACT_ORIGIN (decl)
5356 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
5357 && RECORD_OR_UNION_TYPE_P
5358 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
5359 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
5361 /* Sometimes the C++ frontend doesn't manage to transform a temporary
5362 DECL_VINDEX referring to itself into a vtable slot number as it
5363 should. Happens with functions that are copied and then forgotten
5364 about. Just clear it, it won't matter anymore. */
5365 if (DECL_VINDEX (decl) && !tree_fits_shwi_p (DECL_VINDEX (decl)))
5366 DECL_VINDEX (decl) = NULL_TREE;
5368 else if (TREE_CODE (decl) == VAR_DECL)
5370 if ((DECL_EXTERNAL (decl)
5371 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
5372 || (decl_function_context (decl) && !TREE_STATIC (decl)))
5373 DECL_INITIAL (decl) = NULL_TREE;
5375 else if (TREE_CODE (decl) == TYPE_DECL
5376 || TREE_CODE (decl) == FIELD_DECL)
5377 DECL_INITIAL (decl) = NULL_TREE;
5378 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
5379 && DECL_INITIAL (decl)
5380 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
5382 /* Strip builtins from the translation-unit BLOCK. We still have targets
5383 without builtin_decl_explicit support and also builtins are shared
5384 nodes and thus we can't use TREE_CHAIN in multiple lists. */
5385 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
5386 while (*nextp)
5388 tree var = *nextp;
5389 if (TREE_CODE (var) == FUNCTION_DECL
5390 && DECL_BUILT_IN (var))
5391 *nextp = TREE_CHAIN (var);
5392 else
5393 nextp = &TREE_CHAIN (var);
5399 /* Data used when collecting DECLs and TYPEs for language data removal. */
5401 struct free_lang_data_d
5403 /* Worklist to avoid excessive recursion. */
5404 vec<tree> worklist;
5406 /* Set of traversed objects. Used to avoid duplicate visits. */
5407 hash_set<tree> *pset;
5409 /* Array of symbols to process with free_lang_data_in_decl. */
5410 vec<tree> decls;
5412 /* Array of types to process with free_lang_data_in_type. */
5413 vec<tree> types;
5417 /* Save all language fields needed to generate proper debug information
5418 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
5420 static void
5421 save_debug_info_for_decl (tree t)
5423 /*struct saved_debug_info_d *sdi;*/
5425 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
5427 /* FIXME. Partial implementation for saving debug info removed. */
5431 /* Save all language fields needed to generate proper debug information
5432 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
5434 static void
5435 save_debug_info_for_type (tree t)
5437 /*struct saved_debug_info_d *sdi;*/
5439 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
5441 /* FIXME. Partial implementation for saving debug info removed. */
5445 /* Add type or decl T to one of the list of tree nodes that need their
5446 language data removed. The lists are held inside FLD. */
5448 static void
5449 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
5451 if (DECL_P (t))
5453 fld->decls.safe_push (t);
5454 if (debug_info_level > DINFO_LEVEL_TERSE)
5455 save_debug_info_for_decl (t);
5457 else if (TYPE_P (t))
5459 fld->types.safe_push (t);
5460 if (debug_info_level > DINFO_LEVEL_TERSE)
5461 save_debug_info_for_type (t);
5463 else
5464 gcc_unreachable ();
5467 /* Push tree node T into FLD->WORKLIST. */
5469 static inline void
5470 fld_worklist_push (tree t, struct free_lang_data_d *fld)
5472 if (t && !is_lang_specific (t) && !fld->pset->contains (t))
5473 fld->worklist.safe_push ((t));
5477 /* Operand callback helper for free_lang_data_in_node. *TP is the
5478 subtree operand being considered. */
5480 static tree
5481 find_decls_types_r (tree *tp, int *ws, void *data)
5483 tree t = *tp;
5484 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
5486 if (TREE_CODE (t) == TREE_LIST)
5487 return NULL_TREE;
5489 /* Language specific nodes will be removed, so there is no need
5490 to gather anything under them. */
5491 if (is_lang_specific (t))
5493 *ws = 0;
5494 return NULL_TREE;
5497 if (DECL_P (t))
5499 /* Note that walk_tree does not traverse every possible field in
5500 decls, so we have to do our own traversals here. */
5501 add_tree_to_fld_list (t, fld);
5503 fld_worklist_push (DECL_NAME (t), fld);
5504 fld_worklist_push (DECL_CONTEXT (t), fld);
5505 fld_worklist_push (DECL_SIZE (t), fld);
5506 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
5508 /* We are going to remove everything under DECL_INITIAL for
5509 TYPE_DECLs. No point walking them. */
5510 if (TREE_CODE (t) != TYPE_DECL)
5511 fld_worklist_push (DECL_INITIAL (t), fld);
5513 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
5514 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
5516 if (TREE_CODE (t) == FUNCTION_DECL)
5518 fld_worklist_push (DECL_ARGUMENTS (t), fld);
5519 fld_worklist_push (DECL_RESULT (t), fld);
5521 else if (TREE_CODE (t) == TYPE_DECL)
5523 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
5525 else if (TREE_CODE (t) == FIELD_DECL)
5527 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
5528 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
5529 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
5530 fld_worklist_push (DECL_FCONTEXT (t), fld);
5533 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
5534 && DECL_HAS_VALUE_EXPR_P (t))
5535 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
5537 if (TREE_CODE (t) != FIELD_DECL
5538 && TREE_CODE (t) != TYPE_DECL)
5539 fld_worklist_push (TREE_CHAIN (t), fld);
5540 *ws = 0;
5542 else if (TYPE_P (t))
5544 /* Note that walk_tree does not traverse every possible field in
5545 types, so we have to do our own traversals here. */
5546 add_tree_to_fld_list (t, fld);
5548 if (!RECORD_OR_UNION_TYPE_P (t))
5549 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
5550 fld_worklist_push (TYPE_SIZE (t), fld);
5551 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
5552 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
5553 fld_worklist_push (TYPE_POINTER_TO (t), fld);
5554 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
5555 fld_worklist_push (TYPE_NAME (t), fld);
5556 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
5557 them and thus do not and want not to reach unused pointer types
5558 this way. */
5559 if (!POINTER_TYPE_P (t))
5560 fld_worklist_push (TYPE_MINVAL (t), fld);
5561 if (!RECORD_OR_UNION_TYPE_P (t))
5562 fld_worklist_push (TYPE_MAXVAL (t), fld);
5563 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
5564 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
5565 do not and want not to reach unused variants this way. */
5566 if (TYPE_CONTEXT (t))
5568 tree ctx = TYPE_CONTEXT (t);
5569 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
5570 So push that instead. */
5571 while (ctx && TREE_CODE (ctx) == BLOCK)
5572 ctx = BLOCK_SUPERCONTEXT (ctx);
5573 fld_worklist_push (ctx, fld);
5575 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
5576 and want not to reach unused types this way. */
5578 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
5580 unsigned i;
5581 tree tem;
5582 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
5583 fld_worklist_push (TREE_TYPE (tem), fld);
5584 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
5585 if (tem
5586 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
5587 && TREE_CODE (tem) == TREE_LIST)
5590 fld_worklist_push (TREE_VALUE (tem), fld);
5591 tem = TREE_CHAIN (tem);
5593 while (tem);
5595 if (RECORD_OR_UNION_TYPE_P (t))
5597 tree tem;
5598 /* Push all TYPE_FIELDS - there can be interleaving interesting
5599 and non-interesting things. */
5600 tem = TYPE_FIELDS (t);
5601 while (tem)
5603 if (TREE_CODE (tem) == FIELD_DECL
5604 || TREE_CODE (tem) == TYPE_DECL)
5605 fld_worklist_push (tem, fld);
5606 tem = TREE_CHAIN (tem);
5610 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5611 *ws = 0;
5613 else if (TREE_CODE (t) == BLOCK)
5615 tree tem;
5616 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5617 fld_worklist_push (tem, fld);
5618 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5619 fld_worklist_push (tem, fld);
5620 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5623 if (TREE_CODE (t) != IDENTIFIER_NODE
5624 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5625 fld_worklist_push (TREE_TYPE (t), fld);
5627 return NULL_TREE;
5631 /* Find decls and types in T. */
5633 static void
5634 find_decls_types (tree t, struct free_lang_data_d *fld)
5636 while (1)
5638 if (!fld->pset->contains (t))
5639 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5640 if (fld->worklist.is_empty ())
5641 break;
5642 t = fld->worklist.pop ();
5646 /* Translate all the types in LIST with the corresponding runtime
5647 types. */
5649 static tree
5650 get_eh_types_for_runtime (tree list)
5652 tree head, prev;
5654 if (list == NULL_TREE)
5655 return NULL_TREE;
5657 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5658 prev = head;
5659 list = TREE_CHAIN (list);
5660 while (list)
5662 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5663 TREE_CHAIN (prev) = n;
5664 prev = TREE_CHAIN (prev);
5665 list = TREE_CHAIN (list);
5668 return head;
5672 /* Find decls and types referenced in EH region R and store them in
5673 FLD->DECLS and FLD->TYPES. */
5675 static void
5676 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5678 switch (r->type)
5680 case ERT_CLEANUP:
5681 break;
5683 case ERT_TRY:
5685 eh_catch c;
5687 /* The types referenced in each catch must first be changed to the
5688 EH types used at runtime. This removes references to FE types
5689 in the region. */
5690 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5692 c->type_list = get_eh_types_for_runtime (c->type_list);
5693 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5696 break;
5698 case ERT_ALLOWED_EXCEPTIONS:
5699 r->u.allowed.type_list
5700 = get_eh_types_for_runtime (r->u.allowed.type_list);
5701 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5702 break;
5704 case ERT_MUST_NOT_THROW:
5705 walk_tree (&r->u.must_not_throw.failure_decl,
5706 find_decls_types_r, fld, fld->pset);
5707 break;
5712 /* Find decls and types referenced in cgraph node N and store them in
5713 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5714 look for *every* kind of DECL and TYPE node reachable from N,
5715 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5716 NAMESPACE_DECLs, etc). */
5718 static void
5719 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5721 basic_block bb;
5722 struct function *fn;
5723 unsigned ix;
5724 tree t;
5726 find_decls_types (n->decl, fld);
5728 if (!gimple_has_body_p (n->decl))
5729 return;
5731 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5733 fn = DECL_STRUCT_FUNCTION (n->decl);
5735 /* Traverse locals. */
5736 FOR_EACH_LOCAL_DECL (fn, ix, t)
5737 find_decls_types (t, fld);
5739 /* Traverse EH regions in FN. */
5741 eh_region r;
5742 FOR_ALL_EH_REGION_FN (r, fn)
5743 find_decls_types_in_eh_region (r, fld);
5746 /* Traverse every statement in FN. */
5747 FOR_EACH_BB_FN (bb, fn)
5749 gphi_iterator psi;
5750 gimple_stmt_iterator si;
5751 unsigned i;
5753 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
5755 gphi *phi = psi.phi ();
5757 for (i = 0; i < gimple_phi_num_args (phi); i++)
5759 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5760 find_decls_types (*arg_p, fld);
5764 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5766 gimple stmt = gsi_stmt (si);
5768 if (is_gimple_call (stmt))
5769 find_decls_types (gimple_call_fntype (stmt), fld);
5771 for (i = 0; i < gimple_num_ops (stmt); i++)
5773 tree arg = gimple_op (stmt, i);
5774 find_decls_types (arg, fld);
5781 /* Find decls and types referenced in varpool node N and store them in
5782 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5783 look for *every* kind of DECL and TYPE node reachable from N,
5784 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5785 NAMESPACE_DECLs, etc). */
5787 static void
5788 find_decls_types_in_var (varpool_node *v, struct free_lang_data_d *fld)
5790 find_decls_types (v->decl, fld);
5793 /* If T needs an assembler name, have one created for it. */
5795 void
5796 assign_assembler_name_if_neeeded (tree t)
5798 if (need_assembler_name_p (t))
5800 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5801 diagnostics that use input_location to show locus
5802 information. The problem here is that, at this point,
5803 input_location is generally anchored to the end of the file
5804 (since the parser is long gone), so we don't have a good
5805 position to pin it to.
5807 To alleviate this problem, this uses the location of T's
5808 declaration. Examples of this are
5809 testsuite/g++.dg/template/cond2.C and
5810 testsuite/g++.dg/template/pr35240.C. */
5811 location_t saved_location = input_location;
5812 input_location = DECL_SOURCE_LOCATION (t);
5814 decl_assembler_name (t);
5816 input_location = saved_location;
5821 /* Free language specific information for every operand and expression
5822 in every node of the call graph. This process operates in three stages:
5824 1- Every callgraph node and varpool node is traversed looking for
5825 decls and types embedded in them. This is a more exhaustive
5826 search than that done by find_referenced_vars, because it will
5827 also collect individual fields, decls embedded in types, etc.
5829 2- All the decls found are sent to free_lang_data_in_decl.
5831 3- All the types found are sent to free_lang_data_in_type.
5833 The ordering between decls and types is important because
5834 free_lang_data_in_decl sets assembler names, which includes
5835 mangling. So types cannot be freed up until assembler names have
5836 been set up. */
5838 static void
5839 free_lang_data_in_cgraph (void)
5841 struct cgraph_node *n;
5842 varpool_node *v;
5843 struct free_lang_data_d fld;
5844 tree t;
5845 unsigned i;
5846 alias_pair *p;
5848 /* Initialize sets and arrays to store referenced decls and types. */
5849 fld.pset = new hash_set<tree>;
5850 fld.worklist.create (0);
5851 fld.decls.create (100);
5852 fld.types.create (100);
5854 /* Find decls and types in the body of every function in the callgraph. */
5855 FOR_EACH_FUNCTION (n)
5856 find_decls_types_in_node (n, &fld);
5858 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5859 find_decls_types (p->decl, &fld);
5861 /* Find decls and types in every varpool symbol. */
5862 FOR_EACH_VARIABLE (v)
5863 find_decls_types_in_var (v, &fld);
5865 /* Set the assembler name on every decl found. We need to do this
5866 now because free_lang_data_in_decl will invalidate data needed
5867 for mangling. This breaks mangling on interdependent decls. */
5868 FOR_EACH_VEC_ELT (fld.decls, i, t)
5869 assign_assembler_name_if_neeeded (t);
5871 /* Traverse every decl found freeing its language data. */
5872 FOR_EACH_VEC_ELT (fld.decls, i, t)
5873 free_lang_data_in_decl (t);
5875 /* Traverse every type found freeing its language data. */
5876 FOR_EACH_VEC_ELT (fld.types, i, t)
5877 free_lang_data_in_type (t);
5878 #ifdef ENABLE_CHECKING
5879 FOR_EACH_VEC_ELT (fld.types, i, t)
5880 verify_type (t);
5881 #endif
5883 delete fld.pset;
5884 fld.worklist.release ();
5885 fld.decls.release ();
5886 fld.types.release ();
5890 /* Free resources that are used by FE but are not needed once they are done. */
5892 static unsigned
5893 free_lang_data (void)
5895 unsigned i;
5897 /* If we are the LTO frontend we have freed lang-specific data already. */
5898 if (in_lto_p
5899 || (!flag_generate_lto && !flag_generate_offload))
5900 return 0;
5902 /* Allocate and assign alias sets to the standard integer types
5903 while the slots are still in the way the frontends generated them. */
5904 for (i = 0; i < itk_none; ++i)
5905 if (integer_types[i])
5906 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5908 /* Traverse the IL resetting language specific information for
5909 operands, expressions, etc. */
5910 free_lang_data_in_cgraph ();
5912 /* Create gimple variants for common types. */
5913 ptrdiff_type_node = integer_type_node;
5914 fileptr_type_node = ptr_type_node;
5916 /* Reset some langhooks. Do not reset types_compatible_p, it may
5917 still be used indirectly via the get_alias_set langhook. */
5918 lang_hooks.dwarf_name = lhd_dwarf_name;
5919 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5920 lang_hooks.gimplify_expr = lhd_gimplify_expr;
5922 /* We do not want the default decl_assembler_name implementation,
5923 rather if we have fixed everything we want a wrapper around it
5924 asserting that all non-local symbols already got their assembler
5925 name and only produce assembler names for local symbols. Or rather
5926 make sure we never call decl_assembler_name on local symbols and
5927 devise a separate, middle-end private scheme for it. */
5929 /* Reset diagnostic machinery. */
5930 tree_diagnostics_defaults (global_dc);
5932 return 0;
5936 namespace {
5938 const pass_data pass_data_ipa_free_lang_data =
5940 SIMPLE_IPA_PASS, /* type */
5941 "*free_lang_data", /* name */
5942 OPTGROUP_NONE, /* optinfo_flags */
5943 TV_IPA_FREE_LANG_DATA, /* tv_id */
5944 0, /* properties_required */
5945 0, /* properties_provided */
5946 0, /* properties_destroyed */
5947 0, /* todo_flags_start */
5948 0, /* todo_flags_finish */
5951 class pass_ipa_free_lang_data : public simple_ipa_opt_pass
5953 public:
5954 pass_ipa_free_lang_data (gcc::context *ctxt)
5955 : simple_ipa_opt_pass (pass_data_ipa_free_lang_data, ctxt)
5958 /* opt_pass methods: */
5959 virtual unsigned int execute (function *) { return free_lang_data (); }
5961 }; // class pass_ipa_free_lang_data
5963 } // anon namespace
5965 simple_ipa_opt_pass *
5966 make_pass_ipa_free_lang_data (gcc::context *ctxt)
5968 return new pass_ipa_free_lang_data (ctxt);
5971 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
5972 ATTR_NAME. Also used internally by remove_attribute(). */
5973 bool
5974 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
5976 size_t ident_len = IDENTIFIER_LENGTH (ident);
5978 if (ident_len == attr_len)
5980 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
5981 return true;
5983 else if (ident_len == attr_len + 4)
5985 /* There is the possibility that ATTR is 'text' and IDENT is
5986 '__text__'. */
5987 const char *p = IDENTIFIER_POINTER (ident);
5988 if (p[0] == '_' && p[1] == '_'
5989 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5990 && strncmp (attr_name, p + 2, attr_len) == 0)
5991 return true;
5994 return false;
5997 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
5998 of ATTR_NAME, and LIST is not NULL_TREE. */
5999 tree
6000 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
6002 while (list)
6004 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
6006 if (ident_len == attr_len)
6008 if (!strcmp (attr_name,
6009 IDENTIFIER_POINTER (get_attribute_name (list))))
6010 break;
6012 /* TODO: If we made sure that attributes were stored in the
6013 canonical form without '__...__' (ie, as in 'text' as opposed
6014 to '__text__') then we could avoid the following case. */
6015 else if (ident_len == attr_len + 4)
6017 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
6018 if (p[0] == '_' && p[1] == '_'
6019 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
6020 && strncmp (attr_name, p + 2, attr_len) == 0)
6021 break;
6023 list = TREE_CHAIN (list);
6026 return list;
6029 /* Given an attribute name ATTR_NAME and a list of attributes LIST,
6030 return a pointer to the attribute's list first element if the attribute
6031 starts with ATTR_NAME. ATTR_NAME must be in the form 'text' (not
6032 '__text__'). */
6034 tree
6035 private_lookup_attribute_by_prefix (const char *attr_name, size_t attr_len,
6036 tree list)
6038 while (list)
6040 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
6042 if (attr_len > ident_len)
6044 list = TREE_CHAIN (list);
6045 continue;
6048 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
6050 if (strncmp (attr_name, p, attr_len) == 0)
6051 break;
6053 /* TODO: If we made sure that attributes were stored in the
6054 canonical form without '__...__' (ie, as in 'text' as opposed
6055 to '__text__') then we could avoid the following case. */
6056 if (p[0] == '_' && p[1] == '_' &&
6057 strncmp (attr_name, p + 2, attr_len) == 0)
6058 break;
6060 list = TREE_CHAIN (list);
6063 return list;
6067 /* A variant of lookup_attribute() that can be used with an identifier
6068 as the first argument, and where the identifier can be either
6069 'text' or '__text__'.
6071 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
6072 return a pointer to the attribute's list element if the attribute
6073 is part of the list, or NULL_TREE if not found. If the attribute
6074 appears more than once, this only returns the first occurrence; the
6075 TREE_CHAIN of the return value should be passed back in if further
6076 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
6077 can be in the form 'text' or '__text__'. */
6078 static tree
6079 lookup_ident_attribute (tree attr_identifier, tree list)
6081 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
6083 while (list)
6085 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
6086 == IDENTIFIER_NODE);
6088 if (cmp_attrib_identifiers (attr_identifier,
6089 get_attribute_name (list)))
6090 /* Found it. */
6091 break;
6092 list = TREE_CHAIN (list);
6095 return list;
6098 /* Remove any instances of attribute ATTR_NAME in LIST and return the
6099 modified list. */
6101 tree
6102 remove_attribute (const char *attr_name, tree list)
6104 tree *p;
6105 size_t attr_len = strlen (attr_name);
6107 gcc_checking_assert (attr_name[0] != '_');
6109 for (p = &list; *p; )
6111 tree l = *p;
6112 /* TODO: If we were storing attributes in normalized form, here
6113 we could use a simple strcmp(). */
6114 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
6115 *p = TREE_CHAIN (l);
6116 else
6117 p = &TREE_CHAIN (l);
6120 return list;
6123 /* Return an attribute list that is the union of a1 and a2. */
6125 tree
6126 merge_attributes (tree a1, tree a2)
6128 tree attributes;
6130 /* Either one unset? Take the set one. */
6132 if ((attributes = a1) == 0)
6133 attributes = a2;
6135 /* One that completely contains the other? Take it. */
6137 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
6139 if (attribute_list_contained (a2, a1))
6140 attributes = a2;
6141 else
6143 /* Pick the longest list, and hang on the other list. */
6145 if (list_length (a1) < list_length (a2))
6146 attributes = a2, a2 = a1;
6148 for (; a2 != 0; a2 = TREE_CHAIN (a2))
6150 tree a;
6151 for (a = lookup_ident_attribute (get_attribute_name (a2),
6152 attributes);
6153 a != NULL_TREE && !attribute_value_equal (a, a2);
6154 a = lookup_ident_attribute (get_attribute_name (a2),
6155 TREE_CHAIN (a)))
6157 if (a == NULL_TREE)
6159 a1 = copy_node (a2);
6160 TREE_CHAIN (a1) = attributes;
6161 attributes = a1;
6166 return attributes;
6169 /* Given types T1 and T2, merge their attributes and return
6170 the result. */
6172 tree
6173 merge_type_attributes (tree t1, tree t2)
6175 return merge_attributes (TYPE_ATTRIBUTES (t1),
6176 TYPE_ATTRIBUTES (t2));
6179 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
6180 the result. */
6182 tree
6183 merge_decl_attributes (tree olddecl, tree newdecl)
6185 return merge_attributes (DECL_ATTRIBUTES (olddecl),
6186 DECL_ATTRIBUTES (newdecl));
6189 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
6191 /* Specialization of merge_decl_attributes for various Windows targets.
6193 This handles the following situation:
6195 __declspec (dllimport) int foo;
6196 int foo;
6198 The second instance of `foo' nullifies the dllimport. */
6200 tree
6201 merge_dllimport_decl_attributes (tree old, tree new_tree)
6203 tree a;
6204 int delete_dllimport_p = 1;
6206 /* What we need to do here is remove from `old' dllimport if it doesn't
6207 appear in `new'. dllimport behaves like extern: if a declaration is
6208 marked dllimport and a definition appears later, then the object
6209 is not dllimport'd. We also remove a `new' dllimport if the old list
6210 contains dllexport: dllexport always overrides dllimport, regardless
6211 of the order of declaration. */
6212 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
6213 delete_dllimport_p = 0;
6214 else if (DECL_DLLIMPORT_P (new_tree)
6215 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
6217 DECL_DLLIMPORT_P (new_tree) = 0;
6218 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
6219 "dllimport ignored", new_tree);
6221 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
6223 /* Warn about overriding a symbol that has already been used, e.g.:
6224 extern int __attribute__ ((dllimport)) foo;
6225 int* bar () {return &foo;}
6226 int foo;
6228 if (TREE_USED (old))
6230 warning (0, "%q+D redeclared without dllimport attribute "
6231 "after being referenced with dll linkage", new_tree);
6232 /* If we have used a variable's address with dllimport linkage,
6233 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
6234 decl may already have had TREE_CONSTANT computed.
6235 We still remove the attribute so that assembler code refers
6236 to '&foo rather than '_imp__foo'. */
6237 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
6238 DECL_DLLIMPORT_P (new_tree) = 1;
6241 /* Let an inline definition silently override the external reference,
6242 but otherwise warn about attribute inconsistency. */
6243 else if (TREE_CODE (new_tree) == VAR_DECL
6244 || !DECL_DECLARED_INLINE_P (new_tree))
6245 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
6246 "previous dllimport ignored", new_tree);
6248 else
6249 delete_dllimport_p = 0;
6251 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
6253 if (delete_dllimport_p)
6254 a = remove_attribute ("dllimport", a);
6256 return a;
6259 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
6260 struct attribute_spec.handler. */
6262 tree
6263 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
6264 bool *no_add_attrs)
6266 tree node = *pnode;
6267 bool is_dllimport;
6269 /* These attributes may apply to structure and union types being created,
6270 but otherwise should pass to the declaration involved. */
6271 if (!DECL_P (node))
6273 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
6274 | (int) ATTR_FLAG_ARRAY_NEXT))
6276 *no_add_attrs = true;
6277 return tree_cons (name, args, NULL_TREE);
6279 if (TREE_CODE (node) == RECORD_TYPE
6280 || TREE_CODE (node) == UNION_TYPE)
6282 node = TYPE_NAME (node);
6283 if (!node)
6284 return NULL_TREE;
6286 else
6288 warning (OPT_Wattributes, "%qE attribute ignored",
6289 name);
6290 *no_add_attrs = true;
6291 return NULL_TREE;
6295 if (TREE_CODE (node) != FUNCTION_DECL
6296 && TREE_CODE (node) != VAR_DECL
6297 && TREE_CODE (node) != TYPE_DECL)
6299 *no_add_attrs = true;
6300 warning (OPT_Wattributes, "%qE attribute ignored",
6301 name);
6302 return NULL_TREE;
6305 if (TREE_CODE (node) == TYPE_DECL
6306 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
6307 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
6309 *no_add_attrs = true;
6310 warning (OPT_Wattributes, "%qE attribute ignored",
6311 name);
6312 return NULL_TREE;
6315 is_dllimport = is_attribute_p ("dllimport", name);
6317 /* Report error on dllimport ambiguities seen now before they cause
6318 any damage. */
6319 if (is_dllimport)
6321 /* Honor any target-specific overrides. */
6322 if (!targetm.valid_dllimport_attribute_p (node))
6323 *no_add_attrs = true;
6325 else if (TREE_CODE (node) == FUNCTION_DECL
6326 && DECL_DECLARED_INLINE_P (node))
6328 warning (OPT_Wattributes, "inline function %q+D declared as "
6329 " dllimport: attribute ignored", node);
6330 *no_add_attrs = true;
6332 /* Like MS, treat definition of dllimported variables and
6333 non-inlined functions on declaration as syntax errors. */
6334 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
6336 error ("function %q+D definition is marked dllimport", node);
6337 *no_add_attrs = true;
6340 else if (TREE_CODE (node) == VAR_DECL)
6342 if (DECL_INITIAL (node))
6344 error ("variable %q+D definition is marked dllimport",
6345 node);
6346 *no_add_attrs = true;
6349 /* `extern' needn't be specified with dllimport.
6350 Specify `extern' now and hope for the best. Sigh. */
6351 DECL_EXTERNAL (node) = 1;
6352 /* Also, implicitly give dllimport'd variables declared within
6353 a function global scope, unless declared static. */
6354 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
6355 TREE_PUBLIC (node) = 1;
6358 if (*no_add_attrs == false)
6359 DECL_DLLIMPORT_P (node) = 1;
6361 else if (TREE_CODE (node) == FUNCTION_DECL
6362 && DECL_DECLARED_INLINE_P (node)
6363 && flag_keep_inline_dllexport)
6364 /* An exported function, even if inline, must be emitted. */
6365 DECL_EXTERNAL (node) = 0;
6367 /* Report error if symbol is not accessible at global scope. */
6368 if (!TREE_PUBLIC (node)
6369 && (TREE_CODE (node) == VAR_DECL
6370 || TREE_CODE (node) == FUNCTION_DECL))
6372 error ("external linkage required for symbol %q+D because of "
6373 "%qE attribute", node, name);
6374 *no_add_attrs = true;
6377 /* A dllexport'd entity must have default visibility so that other
6378 program units (shared libraries or the main executable) can see
6379 it. A dllimport'd entity must have default visibility so that
6380 the linker knows that undefined references within this program
6381 unit can be resolved by the dynamic linker. */
6382 if (!*no_add_attrs)
6384 if (DECL_VISIBILITY_SPECIFIED (node)
6385 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
6386 error ("%qE implies default visibility, but %qD has already "
6387 "been declared with a different visibility",
6388 name, node);
6389 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
6390 DECL_VISIBILITY_SPECIFIED (node) = 1;
6393 return NULL_TREE;
6396 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
6398 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
6399 of the various TYPE_QUAL values. */
6401 static void
6402 set_type_quals (tree type, int type_quals)
6404 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
6405 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
6406 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
6407 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
6408 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
6411 /* Returns true iff unqualified CAND and BASE are equivalent. */
6413 bool
6414 check_base_type (const_tree cand, const_tree base)
6416 return (TYPE_NAME (cand) == TYPE_NAME (base)
6417 /* Apparently this is needed for Objective-C. */
6418 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6419 /* Check alignment. */
6420 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
6421 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6422 TYPE_ATTRIBUTES (base)));
6425 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
6427 bool
6428 check_qualified_type (const_tree cand, const_tree base, int type_quals)
6430 return (TYPE_QUALS (cand) == type_quals
6431 && check_base_type (cand, base));
6434 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
6436 static bool
6437 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
6439 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
6440 && TYPE_NAME (cand) == TYPE_NAME (base)
6441 /* Apparently this is needed for Objective-C. */
6442 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6443 /* Check alignment. */
6444 && TYPE_ALIGN (cand) == align
6445 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6446 TYPE_ATTRIBUTES (base)));
6449 /* This function checks to see if TYPE matches the size one of the built-in
6450 atomic types, and returns that core atomic type. */
6452 static tree
6453 find_atomic_core_type (tree type)
6455 tree base_atomic_type;
6457 /* Only handle complete types. */
6458 if (TYPE_SIZE (type) == NULL_TREE)
6459 return NULL_TREE;
6461 HOST_WIDE_INT type_size = tree_to_uhwi (TYPE_SIZE (type));
6462 switch (type_size)
6464 case 8:
6465 base_atomic_type = atomicQI_type_node;
6466 break;
6468 case 16:
6469 base_atomic_type = atomicHI_type_node;
6470 break;
6472 case 32:
6473 base_atomic_type = atomicSI_type_node;
6474 break;
6476 case 64:
6477 base_atomic_type = atomicDI_type_node;
6478 break;
6480 case 128:
6481 base_atomic_type = atomicTI_type_node;
6482 break;
6484 default:
6485 base_atomic_type = NULL_TREE;
6488 return base_atomic_type;
6491 /* Return a version of the TYPE, qualified as indicated by the
6492 TYPE_QUALS, if one exists. If no qualified version exists yet,
6493 return NULL_TREE. */
6495 tree
6496 get_qualified_type (tree type, int type_quals)
6498 tree t;
6500 if (TYPE_QUALS (type) == type_quals)
6501 return type;
6503 /* Search the chain of variants to see if there is already one there just
6504 like the one we need to have. If so, use that existing one. We must
6505 preserve the TYPE_NAME, since there is code that depends on this. */
6506 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6507 if (check_qualified_type (t, type, type_quals))
6508 return t;
6510 return NULL_TREE;
6513 /* Like get_qualified_type, but creates the type if it does not
6514 exist. This function never returns NULL_TREE. */
6516 tree
6517 build_qualified_type (tree type, int type_quals)
6519 tree t;
6521 /* See if we already have the appropriate qualified variant. */
6522 t = get_qualified_type (type, type_quals);
6524 /* If not, build it. */
6525 if (!t)
6527 t = build_variant_type_copy (type);
6528 set_type_quals (t, type_quals);
6530 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
6532 /* See if this object can map to a basic atomic type. */
6533 tree atomic_type = find_atomic_core_type (type);
6534 if (atomic_type)
6536 /* Ensure the alignment of this type is compatible with
6537 the required alignment of the atomic type. */
6538 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
6539 TYPE_ALIGN (t) = TYPE_ALIGN (atomic_type);
6543 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6544 /* Propagate structural equality. */
6545 SET_TYPE_STRUCTURAL_EQUALITY (t);
6546 else if (TYPE_CANONICAL (type) != type)
6547 /* Build the underlying canonical type, since it is different
6548 from TYPE. */
6550 tree c = build_qualified_type (TYPE_CANONICAL (type), type_quals);
6551 TYPE_CANONICAL (t) = TYPE_CANONICAL (c);
6553 else
6554 /* T is its own canonical type. */
6555 TYPE_CANONICAL (t) = t;
6559 return t;
6562 /* Create a variant of type T with alignment ALIGN. */
6564 tree
6565 build_aligned_type (tree type, unsigned int align)
6567 tree t;
6569 if (TYPE_PACKED (type)
6570 || TYPE_ALIGN (type) == align)
6571 return type;
6573 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6574 if (check_aligned_type (t, type, align))
6575 return t;
6577 t = build_variant_type_copy (type);
6578 TYPE_ALIGN (t) = align;
6580 return t;
6583 /* Create a new distinct copy of TYPE. The new type is made its own
6584 MAIN_VARIANT. If TYPE requires structural equality checks, the
6585 resulting type requires structural equality checks; otherwise, its
6586 TYPE_CANONICAL points to itself. */
6588 tree
6589 build_distinct_type_copy (tree type)
6591 tree t = copy_node (type);
6593 TYPE_POINTER_TO (t) = 0;
6594 TYPE_REFERENCE_TO (t) = 0;
6596 /* Set the canonical type either to a new equivalence class, or
6597 propagate the need for structural equality checks. */
6598 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6599 SET_TYPE_STRUCTURAL_EQUALITY (t);
6600 else
6601 TYPE_CANONICAL (t) = t;
6603 /* Make it its own variant. */
6604 TYPE_MAIN_VARIANT (t) = t;
6605 TYPE_NEXT_VARIANT (t) = 0;
6607 /* We do not record methods in type copies nor variants
6608 so we do not need to keep them up to date when new method
6609 is inserted. */
6610 if (RECORD_OR_UNION_TYPE_P (t))
6611 TYPE_METHODS (t) = NULL_TREE;
6613 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
6614 whose TREE_TYPE is not t. This can also happen in the Ada
6615 frontend when using subtypes. */
6617 return t;
6620 /* Create a new variant of TYPE, equivalent but distinct. This is so
6621 the caller can modify it. TYPE_CANONICAL for the return type will
6622 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
6623 are considered equal by the language itself (or that both types
6624 require structural equality checks). */
6626 tree
6627 build_variant_type_copy (tree type)
6629 tree t, m = TYPE_MAIN_VARIANT (type);
6631 t = build_distinct_type_copy (type);
6633 /* Since we're building a variant, assume that it is a non-semantic
6634 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
6635 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
6637 /* Add the new type to the chain of variants of TYPE. */
6638 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
6639 TYPE_NEXT_VARIANT (m) = t;
6640 TYPE_MAIN_VARIANT (t) = m;
6642 return t;
6645 /* Return true if the from tree in both tree maps are equal. */
6648 tree_map_base_eq (const void *va, const void *vb)
6650 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6651 *const b = (const struct tree_map_base *) vb;
6652 return (a->from == b->from);
6655 /* Hash a from tree in a tree_base_map. */
6657 unsigned int
6658 tree_map_base_hash (const void *item)
6660 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6663 /* Return true if this tree map structure is marked for garbage collection
6664 purposes. We simply return true if the from tree is marked, so that this
6665 structure goes away when the from tree goes away. */
6668 tree_map_base_marked_p (const void *p)
6670 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6673 /* Hash a from tree in a tree_map. */
6675 unsigned int
6676 tree_map_hash (const void *item)
6678 return (((const struct tree_map *) item)->hash);
6681 /* Hash a from tree in a tree_decl_map. */
6683 unsigned int
6684 tree_decl_map_hash (const void *item)
6686 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6689 /* Return the initialization priority for DECL. */
6691 priority_type
6692 decl_init_priority_lookup (tree decl)
6694 symtab_node *snode = symtab_node::get (decl);
6696 if (!snode)
6697 return DEFAULT_INIT_PRIORITY;
6698 return
6699 snode->get_init_priority ();
6702 /* Return the finalization priority for DECL. */
6704 priority_type
6705 decl_fini_priority_lookup (tree decl)
6707 cgraph_node *node = cgraph_node::get (decl);
6709 if (!node)
6710 return DEFAULT_INIT_PRIORITY;
6711 return
6712 node->get_fini_priority ();
6715 /* Set the initialization priority for DECL to PRIORITY. */
6717 void
6718 decl_init_priority_insert (tree decl, priority_type priority)
6720 struct symtab_node *snode;
6722 if (priority == DEFAULT_INIT_PRIORITY)
6724 snode = symtab_node::get (decl);
6725 if (!snode)
6726 return;
6728 else if (TREE_CODE (decl) == VAR_DECL)
6729 snode = varpool_node::get_create (decl);
6730 else
6731 snode = cgraph_node::get_create (decl);
6732 snode->set_init_priority (priority);
6735 /* Set the finalization priority for DECL to PRIORITY. */
6737 void
6738 decl_fini_priority_insert (tree decl, priority_type priority)
6740 struct cgraph_node *node;
6742 if (priority == DEFAULT_INIT_PRIORITY)
6744 node = cgraph_node::get (decl);
6745 if (!node)
6746 return;
6748 else
6749 node = cgraph_node::get_create (decl);
6750 node->set_fini_priority (priority);
6753 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6755 static void
6756 print_debug_expr_statistics (void)
6758 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6759 (long) debug_expr_for_decl->size (),
6760 (long) debug_expr_for_decl->elements (),
6761 debug_expr_for_decl->collisions ());
6764 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6766 static void
6767 print_value_expr_statistics (void)
6769 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6770 (long) value_expr_for_decl->size (),
6771 (long) value_expr_for_decl->elements (),
6772 value_expr_for_decl->collisions ());
6775 /* Lookup a debug expression for FROM, and return it if we find one. */
6777 tree
6778 decl_debug_expr_lookup (tree from)
6780 struct tree_decl_map *h, in;
6781 in.base.from = from;
6783 h = debug_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6784 if (h)
6785 return h->to;
6786 return NULL_TREE;
6789 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6791 void
6792 decl_debug_expr_insert (tree from, tree to)
6794 struct tree_decl_map *h;
6796 h = ggc_alloc<tree_decl_map> ();
6797 h->base.from = from;
6798 h->to = to;
6799 *debug_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6802 /* Lookup a value expression for FROM, and return it if we find one. */
6804 tree
6805 decl_value_expr_lookup (tree from)
6807 struct tree_decl_map *h, in;
6808 in.base.from = from;
6810 h = value_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6811 if (h)
6812 return h->to;
6813 return NULL_TREE;
6816 /* Insert a mapping FROM->TO in the value expression hashtable. */
6818 void
6819 decl_value_expr_insert (tree from, tree to)
6821 struct tree_decl_map *h;
6823 h = ggc_alloc<tree_decl_map> ();
6824 h->base.from = from;
6825 h->to = to;
6826 *value_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6829 /* Lookup a vector of debug arguments for FROM, and return it if we
6830 find one. */
6832 vec<tree, va_gc> **
6833 decl_debug_args_lookup (tree from)
6835 struct tree_vec_map *h, in;
6837 if (!DECL_HAS_DEBUG_ARGS_P (from))
6838 return NULL;
6839 gcc_checking_assert (debug_args_for_decl != NULL);
6840 in.base.from = from;
6841 h = debug_args_for_decl->find_with_hash (&in, DECL_UID (from));
6842 if (h)
6843 return &h->to;
6844 return NULL;
6847 /* Insert a mapping FROM->empty vector of debug arguments in the value
6848 expression hashtable. */
6850 vec<tree, va_gc> **
6851 decl_debug_args_insert (tree from)
6853 struct tree_vec_map *h;
6854 tree_vec_map **loc;
6856 if (DECL_HAS_DEBUG_ARGS_P (from))
6857 return decl_debug_args_lookup (from);
6858 if (debug_args_for_decl == NULL)
6859 debug_args_for_decl = hash_table<tree_vec_map_cache_hasher>::create_ggc (64);
6860 h = ggc_alloc<tree_vec_map> ();
6861 h->base.from = from;
6862 h->to = NULL;
6863 loc = debug_args_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT);
6864 *loc = h;
6865 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6866 return &h->to;
6869 /* Hashing of types so that we don't make duplicates.
6870 The entry point is `type_hash_canon'. */
6872 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6873 with types in the TREE_VALUE slots), by adding the hash codes
6874 of the individual types. */
6876 static void
6877 type_hash_list (const_tree list, inchash::hash &hstate)
6879 const_tree tail;
6881 for (tail = list; tail; tail = TREE_CHAIN (tail))
6882 if (TREE_VALUE (tail) != error_mark_node)
6883 hstate.add_object (TYPE_HASH (TREE_VALUE (tail)));
6886 /* These are the Hashtable callback functions. */
6888 /* Returns true iff the types are equivalent. */
6890 bool
6891 type_cache_hasher::equal (type_hash *a, type_hash *b)
6893 /* First test the things that are the same for all types. */
6894 if (a->hash != b->hash
6895 || TREE_CODE (a->type) != TREE_CODE (b->type)
6896 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6897 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6898 TYPE_ATTRIBUTES (b->type))
6899 || (TREE_CODE (a->type) != COMPLEX_TYPE
6900 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6901 return 0;
6903 /* Be careful about comparing arrays before and after the element type
6904 has been completed; don't compare TYPE_ALIGN unless both types are
6905 complete. */
6906 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6907 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6908 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6909 return 0;
6911 switch (TREE_CODE (a->type))
6913 case VOID_TYPE:
6914 case COMPLEX_TYPE:
6915 case POINTER_TYPE:
6916 case REFERENCE_TYPE:
6917 case NULLPTR_TYPE:
6918 return 1;
6920 case VECTOR_TYPE:
6921 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6923 case ENUMERAL_TYPE:
6924 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6925 && !(TYPE_VALUES (a->type)
6926 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6927 && TYPE_VALUES (b->type)
6928 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6929 && type_list_equal (TYPE_VALUES (a->type),
6930 TYPE_VALUES (b->type))))
6931 return 0;
6933 /* ... fall through ... */
6935 case INTEGER_TYPE:
6936 case REAL_TYPE:
6937 case BOOLEAN_TYPE:
6938 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
6939 return false;
6940 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6941 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6942 TYPE_MAX_VALUE (b->type)))
6943 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6944 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6945 TYPE_MIN_VALUE (b->type))));
6947 case FIXED_POINT_TYPE:
6948 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6950 case OFFSET_TYPE:
6951 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6953 case METHOD_TYPE:
6954 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6955 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6956 || (TYPE_ARG_TYPES (a->type)
6957 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6958 && TYPE_ARG_TYPES (b->type)
6959 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6960 && type_list_equal (TYPE_ARG_TYPES (a->type),
6961 TYPE_ARG_TYPES (b->type)))))
6962 break;
6963 return 0;
6964 case ARRAY_TYPE:
6965 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6967 case RECORD_TYPE:
6968 case UNION_TYPE:
6969 case QUAL_UNION_TYPE:
6970 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6971 || (TYPE_FIELDS (a->type)
6972 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6973 && TYPE_FIELDS (b->type)
6974 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6975 && type_list_equal (TYPE_FIELDS (a->type),
6976 TYPE_FIELDS (b->type))));
6978 case FUNCTION_TYPE:
6979 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6980 || (TYPE_ARG_TYPES (a->type)
6981 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6982 && TYPE_ARG_TYPES (b->type)
6983 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6984 && type_list_equal (TYPE_ARG_TYPES (a->type),
6985 TYPE_ARG_TYPES (b->type))))
6986 break;
6987 return 0;
6989 default:
6990 return 0;
6993 if (lang_hooks.types.type_hash_eq != NULL)
6994 return lang_hooks.types.type_hash_eq (a->type, b->type);
6996 return 1;
6999 /* Given TYPE, and HASHCODE its hash code, return the canonical
7000 object for an identical type if one already exists.
7001 Otherwise, return TYPE, and record it as the canonical object.
7003 To use this function, first create a type of the sort you want.
7004 Then compute its hash code from the fields of the type that
7005 make it different from other similar types.
7006 Then call this function and use the value. */
7008 tree
7009 type_hash_canon (unsigned int hashcode, tree type)
7011 type_hash in;
7012 type_hash **loc;
7014 /* The hash table only contains main variants, so ensure that's what we're
7015 being passed. */
7016 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
7018 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
7019 must call that routine before comparing TYPE_ALIGNs. */
7020 layout_type (type);
7022 in.hash = hashcode;
7023 in.type = type;
7025 loc = type_hash_table->find_slot_with_hash (&in, hashcode, INSERT);
7026 if (*loc)
7028 tree t1 = ((type_hash *) *loc)->type;
7029 gcc_assert (TYPE_MAIN_VARIANT (t1) == t1);
7030 if (GATHER_STATISTICS)
7032 tree_code_counts[(int) TREE_CODE (type)]--;
7033 tree_node_counts[(int) t_kind]--;
7034 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
7036 return t1;
7038 else
7040 struct type_hash *h;
7042 h = ggc_alloc<type_hash> ();
7043 h->hash = hashcode;
7044 h->type = type;
7045 *loc = h;
7047 return type;
7051 static void
7052 print_type_hash_statistics (void)
7054 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
7055 (long) type_hash_table->size (),
7056 (long) type_hash_table->elements (),
7057 type_hash_table->collisions ());
7060 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
7061 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
7062 by adding the hash codes of the individual attributes. */
7064 static void
7065 attribute_hash_list (const_tree list, inchash::hash &hstate)
7067 const_tree tail;
7069 for (tail = list; tail; tail = TREE_CHAIN (tail))
7070 /* ??? Do we want to add in TREE_VALUE too? */
7071 hstate.add_object (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)));
7074 /* Given two lists of attributes, return true if list l2 is
7075 equivalent to l1. */
7078 attribute_list_equal (const_tree l1, const_tree l2)
7080 if (l1 == l2)
7081 return 1;
7083 return attribute_list_contained (l1, l2)
7084 && attribute_list_contained (l2, l1);
7087 /* Given two lists of attributes, return true if list L2 is
7088 completely contained within L1. */
7089 /* ??? This would be faster if attribute names were stored in a canonicalized
7090 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
7091 must be used to show these elements are equivalent (which they are). */
7092 /* ??? It's not clear that attributes with arguments will always be handled
7093 correctly. */
7096 attribute_list_contained (const_tree l1, const_tree l2)
7098 const_tree t1, t2;
7100 /* First check the obvious, maybe the lists are identical. */
7101 if (l1 == l2)
7102 return 1;
7104 /* Maybe the lists are similar. */
7105 for (t1 = l1, t2 = l2;
7106 t1 != 0 && t2 != 0
7107 && get_attribute_name (t1) == get_attribute_name (t2)
7108 && TREE_VALUE (t1) == TREE_VALUE (t2);
7109 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7112 /* Maybe the lists are equal. */
7113 if (t1 == 0 && t2 == 0)
7114 return 1;
7116 for (; t2 != 0; t2 = TREE_CHAIN (t2))
7118 const_tree attr;
7119 /* This CONST_CAST is okay because lookup_attribute does not
7120 modify its argument and the return value is assigned to a
7121 const_tree. */
7122 for (attr = lookup_ident_attribute (get_attribute_name (t2),
7123 CONST_CAST_TREE (l1));
7124 attr != NULL_TREE && !attribute_value_equal (t2, attr);
7125 attr = lookup_ident_attribute (get_attribute_name (t2),
7126 TREE_CHAIN (attr)))
7129 if (attr == NULL_TREE)
7130 return 0;
7133 return 1;
7136 /* Given two lists of types
7137 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
7138 return 1 if the lists contain the same types in the same order.
7139 Also, the TREE_PURPOSEs must match. */
7142 type_list_equal (const_tree l1, const_tree l2)
7144 const_tree t1, t2;
7146 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7147 if (TREE_VALUE (t1) != TREE_VALUE (t2)
7148 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
7149 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
7150 && (TREE_TYPE (TREE_PURPOSE (t1))
7151 == TREE_TYPE (TREE_PURPOSE (t2))))))
7152 return 0;
7154 return t1 == t2;
7157 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
7158 given by TYPE. If the argument list accepts variable arguments,
7159 then this function counts only the ordinary arguments. */
7162 type_num_arguments (const_tree type)
7164 int i = 0;
7165 tree t;
7167 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
7168 /* If the function does not take a variable number of arguments,
7169 the last element in the list will have type `void'. */
7170 if (VOID_TYPE_P (TREE_VALUE (t)))
7171 break;
7172 else
7173 ++i;
7175 return i;
7178 /* Nonzero if integer constants T1 and T2
7179 represent the same constant value. */
7182 tree_int_cst_equal (const_tree t1, const_tree t2)
7184 if (t1 == t2)
7185 return 1;
7187 if (t1 == 0 || t2 == 0)
7188 return 0;
7190 if (TREE_CODE (t1) == INTEGER_CST
7191 && TREE_CODE (t2) == INTEGER_CST
7192 && wi::to_widest (t1) == wi::to_widest (t2))
7193 return 1;
7195 return 0;
7198 /* Return true if T is an INTEGER_CST whose numerical value (extended
7199 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
7201 bool
7202 tree_fits_shwi_p (const_tree t)
7204 return (t != NULL_TREE
7205 && TREE_CODE (t) == INTEGER_CST
7206 && wi::fits_shwi_p (wi::to_widest (t)));
7209 /* Return true if T is an INTEGER_CST whose numerical value (extended
7210 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
7212 bool
7213 tree_fits_uhwi_p (const_tree t)
7215 return (t != NULL_TREE
7216 && TREE_CODE (t) == INTEGER_CST
7217 && wi::fits_uhwi_p (wi::to_widest (t)));
7220 /* T is an INTEGER_CST whose numerical value (extended according to
7221 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
7222 HOST_WIDE_INT. */
7224 HOST_WIDE_INT
7225 tree_to_shwi (const_tree t)
7227 gcc_assert (tree_fits_shwi_p (t));
7228 return TREE_INT_CST_LOW (t);
7231 /* T is an INTEGER_CST whose numerical value (extended according to
7232 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
7233 HOST_WIDE_INT. */
7235 unsigned HOST_WIDE_INT
7236 tree_to_uhwi (const_tree t)
7238 gcc_assert (tree_fits_uhwi_p (t));
7239 return TREE_INT_CST_LOW (t);
7242 /* Return the most significant (sign) bit of T. */
7245 tree_int_cst_sign_bit (const_tree t)
7247 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
7249 return wi::extract_uhwi (t, bitno, 1);
7252 /* Return an indication of the sign of the integer constant T.
7253 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
7254 Note that -1 will never be returned if T's type is unsigned. */
7257 tree_int_cst_sgn (const_tree t)
7259 if (wi::eq_p (t, 0))
7260 return 0;
7261 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
7262 return 1;
7263 else if (wi::neg_p (t))
7264 return -1;
7265 else
7266 return 1;
7269 /* Return the minimum number of bits needed to represent VALUE in a
7270 signed or unsigned type, UNSIGNEDP says which. */
7272 unsigned int
7273 tree_int_cst_min_precision (tree value, signop sgn)
7275 /* If the value is negative, compute its negative minus 1. The latter
7276 adjustment is because the absolute value of the largest negative value
7277 is one larger than the largest positive value. This is equivalent to
7278 a bit-wise negation, so use that operation instead. */
7280 if (tree_int_cst_sgn (value) < 0)
7281 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
7283 /* Return the number of bits needed, taking into account the fact
7284 that we need one more bit for a signed than unsigned type.
7285 If value is 0 or -1, the minimum precision is 1 no matter
7286 whether unsignedp is true or false. */
7288 if (integer_zerop (value))
7289 return 1;
7290 else
7291 return tree_floor_log2 (value) + 1 + (sgn == SIGNED ? 1 : 0) ;
7294 /* Return truthvalue of whether T1 is the same tree structure as T2.
7295 Return 1 if they are the same.
7296 Return 0 if they are understandably different.
7297 Return -1 if either contains tree structure not understood by
7298 this function. */
7301 simple_cst_equal (const_tree t1, const_tree t2)
7303 enum tree_code code1, code2;
7304 int cmp;
7305 int i;
7307 if (t1 == t2)
7308 return 1;
7309 if (t1 == 0 || t2 == 0)
7310 return 0;
7312 code1 = TREE_CODE (t1);
7313 code2 = TREE_CODE (t2);
7315 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
7317 if (CONVERT_EXPR_CODE_P (code2)
7318 || code2 == NON_LVALUE_EXPR)
7319 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7320 else
7321 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
7324 else if (CONVERT_EXPR_CODE_P (code2)
7325 || code2 == NON_LVALUE_EXPR)
7326 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
7328 if (code1 != code2)
7329 return 0;
7331 switch (code1)
7333 case INTEGER_CST:
7334 return wi::to_widest (t1) == wi::to_widest (t2);
7336 case REAL_CST:
7337 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
7339 case FIXED_CST:
7340 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
7342 case STRING_CST:
7343 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
7344 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
7345 TREE_STRING_LENGTH (t1)));
7347 case CONSTRUCTOR:
7349 unsigned HOST_WIDE_INT idx;
7350 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
7351 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
7353 if (vec_safe_length (v1) != vec_safe_length (v2))
7354 return false;
7356 for (idx = 0; idx < vec_safe_length (v1); ++idx)
7357 /* ??? Should we handle also fields here? */
7358 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
7359 return false;
7360 return true;
7363 case SAVE_EXPR:
7364 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7366 case CALL_EXPR:
7367 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
7368 if (cmp <= 0)
7369 return cmp;
7370 if (call_expr_nargs (t1) != call_expr_nargs (t2))
7371 return 0;
7373 const_tree arg1, arg2;
7374 const_call_expr_arg_iterator iter1, iter2;
7375 for (arg1 = first_const_call_expr_arg (t1, &iter1),
7376 arg2 = first_const_call_expr_arg (t2, &iter2);
7377 arg1 && arg2;
7378 arg1 = next_const_call_expr_arg (&iter1),
7379 arg2 = next_const_call_expr_arg (&iter2))
7381 cmp = simple_cst_equal (arg1, arg2);
7382 if (cmp <= 0)
7383 return cmp;
7385 return arg1 == arg2;
7388 case TARGET_EXPR:
7389 /* Special case: if either target is an unallocated VAR_DECL,
7390 it means that it's going to be unified with whatever the
7391 TARGET_EXPR is really supposed to initialize, so treat it
7392 as being equivalent to anything. */
7393 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
7394 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
7395 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
7396 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
7397 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
7398 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
7399 cmp = 1;
7400 else
7401 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7403 if (cmp <= 0)
7404 return cmp;
7406 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
7408 case WITH_CLEANUP_EXPR:
7409 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7410 if (cmp <= 0)
7411 return cmp;
7413 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
7415 case COMPONENT_REF:
7416 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
7417 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7419 return 0;
7421 case VAR_DECL:
7422 case PARM_DECL:
7423 case CONST_DECL:
7424 case FUNCTION_DECL:
7425 return 0;
7427 default:
7428 break;
7431 /* This general rule works for most tree codes. All exceptions should be
7432 handled above. If this is a language-specific tree code, we can't
7433 trust what might be in the operand, so say we don't know
7434 the situation. */
7435 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
7436 return -1;
7438 switch (TREE_CODE_CLASS (code1))
7440 case tcc_unary:
7441 case tcc_binary:
7442 case tcc_comparison:
7443 case tcc_expression:
7444 case tcc_reference:
7445 case tcc_statement:
7446 cmp = 1;
7447 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
7449 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
7450 if (cmp <= 0)
7451 return cmp;
7454 return cmp;
7456 default:
7457 return -1;
7461 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
7462 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
7463 than U, respectively. */
7466 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
7468 if (tree_int_cst_sgn (t) < 0)
7469 return -1;
7470 else if (!tree_fits_uhwi_p (t))
7471 return 1;
7472 else if (TREE_INT_CST_LOW (t) == u)
7473 return 0;
7474 else if (TREE_INT_CST_LOW (t) < u)
7475 return -1;
7476 else
7477 return 1;
7480 /* Return true if SIZE represents a constant size that is in bounds of
7481 what the middle-end and the backend accepts (covering not more than
7482 half of the address-space). */
7484 bool
7485 valid_constant_size_p (const_tree size)
7487 if (! tree_fits_uhwi_p (size)
7488 || TREE_OVERFLOW (size)
7489 || tree_int_cst_sign_bit (size) != 0)
7490 return false;
7491 return true;
7494 /* Return the precision of the type, or for a complex or vector type the
7495 precision of the type of its elements. */
7497 unsigned int
7498 element_precision (const_tree type)
7500 enum tree_code code = TREE_CODE (type);
7501 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
7502 type = TREE_TYPE (type);
7504 return TYPE_PRECISION (type);
7507 /* Return true if CODE represents an associative tree code. Otherwise
7508 return false. */
7509 bool
7510 associative_tree_code (enum tree_code code)
7512 switch (code)
7514 case BIT_IOR_EXPR:
7515 case BIT_AND_EXPR:
7516 case BIT_XOR_EXPR:
7517 case PLUS_EXPR:
7518 case MULT_EXPR:
7519 case MIN_EXPR:
7520 case MAX_EXPR:
7521 return true;
7523 default:
7524 break;
7526 return false;
7529 /* Return true if CODE represents a commutative tree code. Otherwise
7530 return false. */
7531 bool
7532 commutative_tree_code (enum tree_code code)
7534 switch (code)
7536 case PLUS_EXPR:
7537 case MULT_EXPR:
7538 case MULT_HIGHPART_EXPR:
7539 case MIN_EXPR:
7540 case MAX_EXPR:
7541 case BIT_IOR_EXPR:
7542 case BIT_XOR_EXPR:
7543 case BIT_AND_EXPR:
7544 case NE_EXPR:
7545 case EQ_EXPR:
7546 case UNORDERED_EXPR:
7547 case ORDERED_EXPR:
7548 case UNEQ_EXPR:
7549 case LTGT_EXPR:
7550 case TRUTH_AND_EXPR:
7551 case TRUTH_XOR_EXPR:
7552 case TRUTH_OR_EXPR:
7553 case WIDEN_MULT_EXPR:
7554 case VEC_WIDEN_MULT_HI_EXPR:
7555 case VEC_WIDEN_MULT_LO_EXPR:
7556 case VEC_WIDEN_MULT_EVEN_EXPR:
7557 case VEC_WIDEN_MULT_ODD_EXPR:
7558 return true;
7560 default:
7561 break;
7563 return false;
7566 /* Return true if CODE represents a ternary tree code for which the
7567 first two operands are commutative. Otherwise return false. */
7568 bool
7569 commutative_ternary_tree_code (enum tree_code code)
7571 switch (code)
7573 case WIDEN_MULT_PLUS_EXPR:
7574 case WIDEN_MULT_MINUS_EXPR:
7575 case DOT_PROD_EXPR:
7576 case FMA_EXPR:
7577 return true;
7579 default:
7580 break;
7582 return false;
7585 namespace inchash
7588 /* Generate a hash value for an expression. This can be used iteratively
7589 by passing a previous result as the HSTATE argument.
7591 This function is intended to produce the same hash for expressions which
7592 would compare equal using operand_equal_p. */
7593 void
7594 add_expr (const_tree t, inchash::hash &hstate)
7596 int i;
7597 enum tree_code code;
7598 enum tree_code_class tclass;
7600 if (t == NULL_TREE)
7602 hstate.merge_hash (0);
7603 return;
7606 code = TREE_CODE (t);
7608 switch (code)
7610 /* Alas, constants aren't shared, so we can't rely on pointer
7611 identity. */
7612 case VOID_CST:
7613 hstate.merge_hash (0);
7614 return;
7615 case INTEGER_CST:
7616 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
7617 hstate.add_wide_int (TREE_INT_CST_ELT (t, i));
7618 return;
7619 case REAL_CST:
7621 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7622 hstate.merge_hash (val2);
7623 return;
7625 case FIXED_CST:
7627 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7628 hstate.merge_hash (val2);
7629 return;
7631 case STRING_CST:
7632 hstate.add ((const void *) TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
7633 return;
7634 case COMPLEX_CST:
7635 inchash::add_expr (TREE_REALPART (t), hstate);
7636 inchash::add_expr (TREE_IMAGPART (t), hstate);
7637 return;
7638 case VECTOR_CST:
7640 unsigned i;
7641 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7642 inchash::add_expr (VECTOR_CST_ELT (t, i), hstate);
7643 return;
7645 case SSA_NAME:
7646 /* We can just compare by pointer. */
7647 hstate.add_wide_int (SSA_NAME_VERSION (t));
7648 return;
7649 case PLACEHOLDER_EXPR:
7650 /* The node itself doesn't matter. */
7651 return;
7652 case TREE_LIST:
7653 /* A list of expressions, for a CALL_EXPR or as the elements of a
7654 VECTOR_CST. */
7655 for (; t; t = TREE_CHAIN (t))
7656 inchash::add_expr (TREE_VALUE (t), hstate);
7657 return;
7658 case CONSTRUCTOR:
7660 unsigned HOST_WIDE_INT idx;
7661 tree field, value;
7662 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7664 inchash::add_expr (field, hstate);
7665 inchash::add_expr (value, hstate);
7667 return;
7669 case FUNCTION_DECL:
7670 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7671 Otherwise nodes that compare equal according to operand_equal_p might
7672 get different hash codes. However, don't do this for machine specific
7673 or front end builtins, since the function code is overloaded in those
7674 cases. */
7675 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7676 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7678 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7679 code = TREE_CODE (t);
7681 /* FALL THROUGH */
7682 default:
7683 tclass = TREE_CODE_CLASS (code);
7685 if (tclass == tcc_declaration)
7687 /* DECL's have a unique ID */
7688 hstate.add_wide_int (DECL_UID (t));
7690 else
7692 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7694 hstate.add_object (code);
7696 /* Don't hash the type, that can lead to having nodes which
7697 compare equal according to operand_equal_p, but which
7698 have different hash codes. */
7699 if (CONVERT_EXPR_CODE_P (code)
7700 || code == NON_LVALUE_EXPR)
7702 /* Make sure to include signness in the hash computation. */
7703 hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
7704 inchash::add_expr (TREE_OPERAND (t, 0), hstate);
7707 else if (commutative_tree_code (code))
7709 /* It's a commutative expression. We want to hash it the same
7710 however it appears. We do this by first hashing both operands
7711 and then rehashing based on the order of their independent
7712 hashes. */
7713 inchash::hash one, two;
7714 inchash::add_expr (TREE_OPERAND (t, 0), one);
7715 inchash::add_expr (TREE_OPERAND (t, 1), two);
7716 hstate.add_commutative (one, two);
7718 else
7719 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7720 inchash::add_expr (TREE_OPERAND (t, i), hstate);
7722 return;
7728 /* Constructors for pointer, array and function types.
7729 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7730 constructed by language-dependent code, not here.) */
7732 /* Construct, lay out and return the type of pointers to TO_TYPE with
7733 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7734 reference all of memory. If such a type has already been
7735 constructed, reuse it. */
7737 tree
7738 build_pointer_type_for_mode (tree to_type, machine_mode mode,
7739 bool can_alias_all)
7741 tree t;
7742 bool could_alias = can_alias_all;
7744 if (to_type == error_mark_node)
7745 return error_mark_node;
7747 /* If the pointed-to type has the may_alias attribute set, force
7748 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7749 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7750 can_alias_all = true;
7752 /* In some cases, languages will have things that aren't a POINTER_TYPE
7753 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7754 In that case, return that type without regard to the rest of our
7755 operands.
7757 ??? This is a kludge, but consistent with the way this function has
7758 always operated and there doesn't seem to be a good way to avoid this
7759 at the moment. */
7760 if (TYPE_POINTER_TO (to_type) != 0
7761 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7762 return TYPE_POINTER_TO (to_type);
7764 /* First, if we already have a type for pointers to TO_TYPE and it's
7765 the proper mode, use it. */
7766 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7767 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7768 return t;
7770 t = make_node (POINTER_TYPE);
7772 TREE_TYPE (t) = to_type;
7773 SET_TYPE_MODE (t, mode);
7774 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7775 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7776 TYPE_POINTER_TO (to_type) = t;
7778 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7779 SET_TYPE_STRUCTURAL_EQUALITY (t);
7780 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
7781 TYPE_CANONICAL (t)
7782 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7783 mode, false);
7785 /* Lay out the type. This function has many callers that are concerned
7786 with expression-construction, and this simplifies them all. */
7787 layout_type (t);
7789 return t;
7792 /* By default build pointers in ptr_mode. */
7794 tree
7795 build_pointer_type (tree to_type)
7797 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7798 : TYPE_ADDR_SPACE (to_type);
7799 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7800 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7803 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7805 tree
7806 build_reference_type_for_mode (tree to_type, machine_mode mode,
7807 bool can_alias_all)
7809 tree t;
7810 bool could_alias = can_alias_all;
7812 if (to_type == error_mark_node)
7813 return error_mark_node;
7815 /* If the pointed-to type has the may_alias attribute set, force
7816 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7817 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7818 can_alias_all = true;
7820 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7821 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7822 In that case, return that type without regard to the rest of our
7823 operands.
7825 ??? This is a kludge, but consistent with the way this function has
7826 always operated and there doesn't seem to be a good way to avoid this
7827 at the moment. */
7828 if (TYPE_REFERENCE_TO (to_type) != 0
7829 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7830 return TYPE_REFERENCE_TO (to_type);
7832 /* First, if we already have a type for pointers to TO_TYPE and it's
7833 the proper mode, use it. */
7834 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7835 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7836 return t;
7838 t = make_node (REFERENCE_TYPE);
7840 TREE_TYPE (t) = to_type;
7841 SET_TYPE_MODE (t, mode);
7842 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7843 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7844 TYPE_REFERENCE_TO (to_type) = t;
7846 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7847 SET_TYPE_STRUCTURAL_EQUALITY (t);
7848 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
7849 TYPE_CANONICAL (t)
7850 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7851 mode, false);
7853 layout_type (t);
7855 return t;
7859 /* Build the node for the type of references-to-TO_TYPE by default
7860 in ptr_mode. */
7862 tree
7863 build_reference_type (tree to_type)
7865 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7866 : TYPE_ADDR_SPACE (to_type);
7867 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7868 return build_reference_type_for_mode (to_type, pointer_mode, false);
7871 #define MAX_INT_CACHED_PREC \
7872 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7873 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7875 /* Builds a signed or unsigned integer type of precision PRECISION.
7876 Used for C bitfields whose precision does not match that of
7877 built-in target types. */
7878 tree
7879 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7880 int unsignedp)
7882 tree itype, ret;
7884 if (unsignedp)
7885 unsignedp = MAX_INT_CACHED_PREC + 1;
7887 if (precision <= MAX_INT_CACHED_PREC)
7889 itype = nonstandard_integer_type_cache[precision + unsignedp];
7890 if (itype)
7891 return itype;
7894 itype = make_node (INTEGER_TYPE);
7895 TYPE_PRECISION (itype) = precision;
7897 if (unsignedp)
7898 fixup_unsigned_type (itype);
7899 else
7900 fixup_signed_type (itype);
7902 ret = itype;
7903 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (itype)))
7904 ret = type_hash_canon (tree_to_uhwi (TYPE_MAX_VALUE (itype)), itype);
7905 if (precision <= MAX_INT_CACHED_PREC)
7906 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7908 return ret;
7911 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7912 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7913 is true, reuse such a type that has already been constructed. */
7915 static tree
7916 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7918 tree itype = make_node (INTEGER_TYPE);
7919 inchash::hash hstate;
7921 TREE_TYPE (itype) = type;
7923 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7924 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7926 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7927 SET_TYPE_MODE (itype, TYPE_MODE (type));
7928 TYPE_SIZE (itype) = TYPE_SIZE (type);
7929 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7930 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7931 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7933 if (!shared)
7934 return itype;
7936 if ((TYPE_MIN_VALUE (itype)
7937 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7938 || (TYPE_MAX_VALUE (itype)
7939 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7941 /* Since we cannot reliably merge this type, we need to compare it using
7942 structural equality checks. */
7943 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7944 return itype;
7947 inchash::add_expr (TYPE_MIN_VALUE (itype), hstate);
7948 inchash::add_expr (TYPE_MAX_VALUE (itype), hstate);
7949 hstate.merge_hash (TYPE_HASH (type));
7950 itype = type_hash_canon (hstate.end (), itype);
7952 return itype;
7955 /* Wrapper around build_range_type_1 with SHARED set to true. */
7957 tree
7958 build_range_type (tree type, tree lowval, tree highval)
7960 return build_range_type_1 (type, lowval, highval, true);
7963 /* Wrapper around build_range_type_1 with SHARED set to false. */
7965 tree
7966 build_nonshared_range_type (tree type, tree lowval, tree highval)
7968 return build_range_type_1 (type, lowval, highval, false);
7971 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7972 MAXVAL should be the maximum value in the domain
7973 (one less than the length of the array).
7975 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7976 We don't enforce this limit, that is up to caller (e.g. language front end).
7977 The limit exists because the result is a signed type and we don't handle
7978 sizes that use more than one HOST_WIDE_INT. */
7980 tree
7981 build_index_type (tree maxval)
7983 return build_range_type (sizetype, size_zero_node, maxval);
7986 /* Return true if the debug information for TYPE, a subtype, should be emitted
7987 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7988 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7989 debug info and doesn't reflect the source code. */
7991 bool
7992 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7994 tree base_type = TREE_TYPE (type), low, high;
7996 /* Subrange types have a base type which is an integral type. */
7997 if (!INTEGRAL_TYPE_P (base_type))
7998 return false;
8000 /* Get the real bounds of the subtype. */
8001 if (lang_hooks.types.get_subrange_bounds)
8002 lang_hooks.types.get_subrange_bounds (type, &low, &high);
8003 else
8005 low = TYPE_MIN_VALUE (type);
8006 high = TYPE_MAX_VALUE (type);
8009 /* If the type and its base type have the same representation and the same
8010 name, then the type is not a subrange but a copy of the base type. */
8011 if ((TREE_CODE (base_type) == INTEGER_TYPE
8012 || TREE_CODE (base_type) == BOOLEAN_TYPE)
8013 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
8014 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
8015 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type))
8016 && TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (base_type))
8017 return false;
8019 if (lowval)
8020 *lowval = low;
8021 if (highval)
8022 *highval = high;
8023 return true;
8026 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
8027 and number of elements specified by the range of values of INDEX_TYPE.
8028 If SHARED is true, reuse such a type that has already been constructed. */
8030 static tree
8031 build_array_type_1 (tree elt_type, tree index_type, bool shared)
8033 tree t;
8035 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
8037 error ("arrays of functions are not meaningful");
8038 elt_type = integer_type_node;
8041 t = make_node (ARRAY_TYPE);
8042 TREE_TYPE (t) = elt_type;
8043 TYPE_DOMAIN (t) = index_type;
8044 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
8045 layout_type (t);
8047 /* If the element type is incomplete at this point we get marked for
8048 structural equality. Do not record these types in the canonical
8049 type hashtable. */
8050 if (TYPE_STRUCTURAL_EQUALITY_P (t))
8051 return t;
8053 if (shared)
8055 inchash::hash hstate;
8056 hstate.add_object (TYPE_HASH (elt_type));
8057 if (index_type)
8058 hstate.add_object (TYPE_HASH (index_type));
8059 t = type_hash_canon (hstate.end (), t);
8062 if (TYPE_CANONICAL (t) == t)
8064 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
8065 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
8066 SET_TYPE_STRUCTURAL_EQUALITY (t);
8067 else if (TYPE_CANONICAL (elt_type) != elt_type
8068 || (index_type && TYPE_CANONICAL (index_type) != index_type))
8069 TYPE_CANONICAL (t)
8070 = build_array_type_1 (TYPE_CANONICAL (elt_type),
8071 index_type
8072 ? TYPE_CANONICAL (index_type) : NULL_TREE,
8073 shared);
8076 return t;
8079 /* Wrapper around build_array_type_1 with SHARED set to true. */
8081 tree
8082 build_array_type (tree elt_type, tree index_type)
8084 return build_array_type_1 (elt_type, index_type, true);
8087 /* Wrapper around build_array_type_1 with SHARED set to false. */
8089 tree
8090 build_nonshared_array_type (tree elt_type, tree index_type)
8092 return build_array_type_1 (elt_type, index_type, false);
8095 /* Return a representation of ELT_TYPE[NELTS], using indices of type
8096 sizetype. */
8098 tree
8099 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
8101 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
8104 /* Recursively examines the array elements of TYPE, until a non-array
8105 element type is found. */
8107 tree
8108 strip_array_types (tree type)
8110 while (TREE_CODE (type) == ARRAY_TYPE)
8111 type = TREE_TYPE (type);
8113 return type;
8116 /* Computes the canonical argument types from the argument type list
8117 ARGTYPES.
8119 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
8120 on entry to this function, or if any of the ARGTYPES are
8121 structural.
8123 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
8124 true on entry to this function, or if any of the ARGTYPES are
8125 non-canonical.
8127 Returns a canonical argument list, which may be ARGTYPES when the
8128 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
8129 true) or would not differ from ARGTYPES. */
8131 static tree
8132 maybe_canonicalize_argtypes (tree argtypes,
8133 bool *any_structural_p,
8134 bool *any_noncanonical_p)
8136 tree arg;
8137 bool any_noncanonical_argtypes_p = false;
8139 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
8141 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
8142 /* Fail gracefully by stating that the type is structural. */
8143 *any_structural_p = true;
8144 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
8145 *any_structural_p = true;
8146 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
8147 || TREE_PURPOSE (arg))
8148 /* If the argument has a default argument, we consider it
8149 non-canonical even though the type itself is canonical.
8150 That way, different variants of function and method types
8151 with default arguments will all point to the variant with
8152 no defaults as their canonical type. */
8153 any_noncanonical_argtypes_p = true;
8156 if (*any_structural_p)
8157 return argtypes;
8159 if (any_noncanonical_argtypes_p)
8161 /* Build the canonical list of argument types. */
8162 tree canon_argtypes = NULL_TREE;
8163 bool is_void = false;
8165 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
8167 if (arg == void_list_node)
8168 is_void = true;
8169 else
8170 canon_argtypes = tree_cons (NULL_TREE,
8171 TYPE_CANONICAL (TREE_VALUE (arg)),
8172 canon_argtypes);
8175 canon_argtypes = nreverse (canon_argtypes);
8176 if (is_void)
8177 canon_argtypes = chainon (canon_argtypes, void_list_node);
8179 /* There is a non-canonical type. */
8180 *any_noncanonical_p = true;
8181 return canon_argtypes;
8184 /* The canonical argument types are the same as ARGTYPES. */
8185 return argtypes;
8188 /* Construct, lay out and return
8189 the type of functions returning type VALUE_TYPE
8190 given arguments of types ARG_TYPES.
8191 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
8192 are data type nodes for the arguments of the function.
8193 If such a type has already been constructed, reuse it. */
8195 tree
8196 build_function_type (tree value_type, tree arg_types)
8198 tree t;
8199 inchash::hash hstate;
8200 bool any_structural_p, any_noncanonical_p;
8201 tree canon_argtypes;
8203 if (TREE_CODE (value_type) == FUNCTION_TYPE)
8205 error ("function return type cannot be function");
8206 value_type = integer_type_node;
8209 /* Make a node of the sort we want. */
8210 t = make_node (FUNCTION_TYPE);
8211 TREE_TYPE (t) = value_type;
8212 TYPE_ARG_TYPES (t) = arg_types;
8214 /* If we already have such a type, use the old one. */
8215 hstate.add_object (TYPE_HASH (value_type));
8216 type_hash_list (arg_types, hstate);
8217 t = type_hash_canon (hstate.end (), t);
8219 /* Set up the canonical type. */
8220 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
8221 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
8222 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
8223 &any_structural_p,
8224 &any_noncanonical_p);
8225 if (any_structural_p)
8226 SET_TYPE_STRUCTURAL_EQUALITY (t);
8227 else if (any_noncanonical_p)
8228 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
8229 canon_argtypes);
8231 if (!COMPLETE_TYPE_P (t))
8232 layout_type (t);
8233 return t;
8236 /* Build a function type. The RETURN_TYPE is the type returned by the
8237 function. If VAARGS is set, no void_type_node is appended to the
8238 the list. ARGP must be always be terminated be a NULL_TREE. */
8240 static tree
8241 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
8243 tree t, args, last;
8245 t = va_arg (argp, tree);
8246 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
8247 args = tree_cons (NULL_TREE, t, args);
8249 if (vaargs)
8251 last = args;
8252 if (args != NULL_TREE)
8253 args = nreverse (args);
8254 gcc_assert (last != void_list_node);
8256 else if (args == NULL_TREE)
8257 args = void_list_node;
8258 else
8260 last = args;
8261 args = nreverse (args);
8262 TREE_CHAIN (last) = void_list_node;
8264 args = build_function_type (return_type, args);
8266 return args;
8269 /* Build a function type. The RETURN_TYPE is the type returned by the
8270 function. If additional arguments are provided, they are
8271 additional argument types. The list of argument types must always
8272 be terminated by NULL_TREE. */
8274 tree
8275 build_function_type_list (tree return_type, ...)
8277 tree args;
8278 va_list p;
8280 va_start (p, return_type);
8281 args = build_function_type_list_1 (false, return_type, p);
8282 va_end (p);
8283 return args;
8286 /* Build a variable argument function type. The RETURN_TYPE is the
8287 type returned by the function. If additional arguments are provided,
8288 they are additional argument types. The list of argument types must
8289 always be terminated by NULL_TREE. */
8291 tree
8292 build_varargs_function_type_list (tree return_type, ...)
8294 tree args;
8295 va_list p;
8297 va_start (p, return_type);
8298 args = build_function_type_list_1 (true, return_type, p);
8299 va_end (p);
8301 return args;
8304 /* Build a function type. RETURN_TYPE is the type returned by the
8305 function; VAARGS indicates whether the function takes varargs. The
8306 function takes N named arguments, the types of which are provided in
8307 ARG_TYPES. */
8309 static tree
8310 build_function_type_array_1 (bool vaargs, tree return_type, int n,
8311 tree *arg_types)
8313 int i;
8314 tree t = vaargs ? NULL_TREE : void_list_node;
8316 for (i = n - 1; i >= 0; i--)
8317 t = tree_cons (NULL_TREE, arg_types[i], t);
8319 return build_function_type (return_type, t);
8322 /* Build a function type. RETURN_TYPE is the type returned by the
8323 function. The function takes N named arguments, the types of which
8324 are provided in ARG_TYPES. */
8326 tree
8327 build_function_type_array (tree return_type, int n, tree *arg_types)
8329 return build_function_type_array_1 (false, return_type, n, arg_types);
8332 /* Build a variable argument function type. RETURN_TYPE is the type
8333 returned by the function. The function takes N named arguments, the
8334 types of which are provided in ARG_TYPES. */
8336 tree
8337 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8339 return build_function_type_array_1 (true, return_type, n, arg_types);
8342 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8343 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8344 for the method. An implicit additional parameter (of type
8345 pointer-to-BASETYPE) is added to the ARGTYPES. */
8347 tree
8348 build_method_type_directly (tree basetype,
8349 tree rettype,
8350 tree argtypes)
8352 tree t;
8353 tree ptype;
8354 inchash::hash hstate;
8355 bool any_structural_p, any_noncanonical_p;
8356 tree canon_argtypes;
8358 /* Make a node of the sort we want. */
8359 t = make_node (METHOD_TYPE);
8361 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8362 TREE_TYPE (t) = rettype;
8363 ptype = build_pointer_type (basetype);
8365 /* The actual arglist for this function includes a "hidden" argument
8366 which is "this". Put it into the list of argument types. */
8367 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8368 TYPE_ARG_TYPES (t) = argtypes;
8370 /* If we already have such a type, use the old one. */
8371 hstate.add_object (TYPE_HASH (basetype));
8372 hstate.add_object (TYPE_HASH (rettype));
8373 type_hash_list (argtypes, hstate);
8374 t = type_hash_canon (hstate.end (), t);
8376 /* Set up the canonical type. */
8377 any_structural_p
8378 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8379 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8380 any_noncanonical_p
8381 = (TYPE_CANONICAL (basetype) != basetype
8382 || TYPE_CANONICAL (rettype) != rettype);
8383 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8384 &any_structural_p,
8385 &any_noncanonical_p);
8386 if (any_structural_p)
8387 SET_TYPE_STRUCTURAL_EQUALITY (t);
8388 else if (any_noncanonical_p)
8389 TYPE_CANONICAL (t)
8390 = build_method_type_directly (TYPE_CANONICAL (basetype),
8391 TYPE_CANONICAL (rettype),
8392 canon_argtypes);
8393 if (!COMPLETE_TYPE_P (t))
8394 layout_type (t);
8396 return t;
8399 /* Construct, lay out and return the type of methods belonging to class
8400 BASETYPE and whose arguments and values are described by TYPE.
8401 If that type exists already, reuse it.
8402 TYPE must be a FUNCTION_TYPE node. */
8404 tree
8405 build_method_type (tree basetype, tree type)
8407 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8409 return build_method_type_directly (basetype,
8410 TREE_TYPE (type),
8411 TYPE_ARG_TYPES (type));
8414 /* Construct, lay out and return the type of offsets to a value
8415 of type TYPE, within an object of type BASETYPE.
8416 If a suitable offset type exists already, reuse it. */
8418 tree
8419 build_offset_type (tree basetype, tree type)
8421 tree t;
8422 inchash::hash hstate;
8424 /* Make a node of the sort we want. */
8425 t = make_node (OFFSET_TYPE);
8427 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8428 TREE_TYPE (t) = type;
8430 /* If we already have such a type, use the old one. */
8431 hstate.add_object (TYPE_HASH (basetype));
8432 hstate.add_object (TYPE_HASH (type));
8433 t = type_hash_canon (hstate.end (), t);
8435 if (!COMPLETE_TYPE_P (t))
8436 layout_type (t);
8438 if (TYPE_CANONICAL (t) == t)
8440 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8441 || TYPE_STRUCTURAL_EQUALITY_P (type))
8442 SET_TYPE_STRUCTURAL_EQUALITY (t);
8443 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8444 || TYPE_CANONICAL (type) != type)
8445 TYPE_CANONICAL (t)
8446 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8447 TYPE_CANONICAL (type));
8450 return t;
8453 /* Create a complex type whose components are COMPONENT_TYPE. */
8455 tree
8456 build_complex_type (tree component_type)
8458 tree t;
8459 inchash::hash hstate;
8461 gcc_assert (INTEGRAL_TYPE_P (component_type)
8462 || SCALAR_FLOAT_TYPE_P (component_type)
8463 || FIXED_POINT_TYPE_P (component_type));
8465 /* Make a node of the sort we want. */
8466 t = make_node (COMPLEX_TYPE);
8468 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8470 /* If we already have such a type, use the old one. */
8471 hstate.add_object (TYPE_HASH (component_type));
8472 t = type_hash_canon (hstate.end (), t);
8474 if (!COMPLETE_TYPE_P (t))
8475 layout_type (t);
8477 if (TYPE_CANONICAL (t) == t)
8479 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8480 SET_TYPE_STRUCTURAL_EQUALITY (t);
8481 else if (TYPE_CANONICAL (component_type) != component_type)
8482 TYPE_CANONICAL (t)
8483 = build_complex_type (TYPE_CANONICAL (component_type));
8486 /* We need to create a name, since complex is a fundamental type. */
8487 if (! TYPE_NAME (t))
8489 const char *name;
8490 if (component_type == char_type_node)
8491 name = "complex char";
8492 else if (component_type == signed_char_type_node)
8493 name = "complex signed char";
8494 else if (component_type == unsigned_char_type_node)
8495 name = "complex unsigned char";
8496 else if (component_type == short_integer_type_node)
8497 name = "complex short int";
8498 else if (component_type == short_unsigned_type_node)
8499 name = "complex short unsigned int";
8500 else if (component_type == integer_type_node)
8501 name = "complex int";
8502 else if (component_type == unsigned_type_node)
8503 name = "complex unsigned int";
8504 else if (component_type == long_integer_type_node)
8505 name = "complex long int";
8506 else if (component_type == long_unsigned_type_node)
8507 name = "complex long unsigned int";
8508 else if (component_type == long_long_integer_type_node)
8509 name = "complex long long int";
8510 else if (component_type == long_long_unsigned_type_node)
8511 name = "complex long long unsigned int";
8512 else
8513 name = 0;
8515 if (name != 0)
8516 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8517 get_identifier (name), t);
8520 return build_qualified_type (t, TYPE_QUALS (component_type));
8523 /* If TYPE is a real or complex floating-point type and the target
8524 does not directly support arithmetic on TYPE then return the wider
8525 type to be used for arithmetic on TYPE. Otherwise, return
8526 NULL_TREE. */
8528 tree
8529 excess_precision_type (tree type)
8531 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8533 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8534 switch (TREE_CODE (type))
8536 case REAL_TYPE:
8537 switch (flt_eval_method)
8539 case 1:
8540 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8541 return double_type_node;
8542 break;
8543 case 2:
8544 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8545 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8546 return long_double_type_node;
8547 break;
8548 default:
8549 gcc_unreachable ();
8551 break;
8552 case COMPLEX_TYPE:
8553 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8554 return NULL_TREE;
8555 switch (flt_eval_method)
8557 case 1:
8558 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8559 return complex_double_type_node;
8560 break;
8561 case 2:
8562 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8563 || (TYPE_MODE (TREE_TYPE (type))
8564 == TYPE_MODE (double_type_node)))
8565 return complex_long_double_type_node;
8566 break;
8567 default:
8568 gcc_unreachable ();
8570 break;
8571 default:
8572 break;
8575 return NULL_TREE;
8578 /* Return OP, stripped of any conversions to wider types as much as is safe.
8579 Converting the value back to OP's type makes a value equivalent to OP.
8581 If FOR_TYPE is nonzero, we return a value which, if converted to
8582 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8584 OP must have integer, real or enumeral type. Pointers are not allowed!
8586 There are some cases where the obvious value we could return
8587 would regenerate to OP if converted to OP's type,
8588 but would not extend like OP to wider types.
8589 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8590 For example, if OP is (unsigned short)(signed char)-1,
8591 we avoid returning (signed char)-1 if FOR_TYPE is int,
8592 even though extending that to an unsigned short would regenerate OP,
8593 since the result of extending (signed char)-1 to (int)
8594 is different from (int) OP. */
8596 tree
8597 get_unwidened (tree op, tree for_type)
8599 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8600 tree type = TREE_TYPE (op);
8601 unsigned final_prec
8602 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8603 int uns
8604 = (for_type != 0 && for_type != type
8605 && final_prec > TYPE_PRECISION (type)
8606 && TYPE_UNSIGNED (type));
8607 tree win = op;
8609 while (CONVERT_EXPR_P (op))
8611 int bitschange;
8613 /* TYPE_PRECISION on vector types has different meaning
8614 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8615 so avoid them here. */
8616 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8617 break;
8619 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8620 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8622 /* Truncations are many-one so cannot be removed.
8623 Unless we are later going to truncate down even farther. */
8624 if (bitschange < 0
8625 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8626 break;
8628 /* See what's inside this conversion. If we decide to strip it,
8629 we will set WIN. */
8630 op = TREE_OPERAND (op, 0);
8632 /* If we have not stripped any zero-extensions (uns is 0),
8633 we can strip any kind of extension.
8634 If we have previously stripped a zero-extension,
8635 only zero-extensions can safely be stripped.
8636 Any extension can be stripped if the bits it would produce
8637 are all going to be discarded later by truncating to FOR_TYPE. */
8639 if (bitschange > 0)
8641 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8642 win = op;
8643 /* TYPE_UNSIGNED says whether this is a zero-extension.
8644 Let's avoid computing it if it does not affect WIN
8645 and if UNS will not be needed again. */
8646 if ((uns
8647 || CONVERT_EXPR_P (op))
8648 && TYPE_UNSIGNED (TREE_TYPE (op)))
8650 uns = 1;
8651 win = op;
8656 /* If we finally reach a constant see if it fits in for_type and
8657 in that case convert it. */
8658 if (for_type
8659 && TREE_CODE (win) == INTEGER_CST
8660 && TREE_TYPE (win) != for_type
8661 && int_fits_type_p (win, for_type))
8662 win = fold_convert (for_type, win);
8664 return win;
8667 /* Return OP or a simpler expression for a narrower value
8668 which can be sign-extended or zero-extended to give back OP.
8669 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8670 or 0 if the value should be sign-extended. */
8672 tree
8673 get_narrower (tree op, int *unsignedp_ptr)
8675 int uns = 0;
8676 int first = 1;
8677 tree win = op;
8678 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8680 while (TREE_CODE (op) == NOP_EXPR)
8682 int bitschange
8683 = (TYPE_PRECISION (TREE_TYPE (op))
8684 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8686 /* Truncations are many-one so cannot be removed. */
8687 if (bitschange < 0)
8688 break;
8690 /* See what's inside this conversion. If we decide to strip it,
8691 we will set WIN. */
8693 if (bitschange > 0)
8695 op = TREE_OPERAND (op, 0);
8696 /* An extension: the outermost one can be stripped,
8697 but remember whether it is zero or sign extension. */
8698 if (first)
8699 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8700 /* Otherwise, if a sign extension has been stripped,
8701 only sign extensions can now be stripped;
8702 if a zero extension has been stripped, only zero-extensions. */
8703 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8704 break;
8705 first = 0;
8707 else /* bitschange == 0 */
8709 /* A change in nominal type can always be stripped, but we must
8710 preserve the unsignedness. */
8711 if (first)
8712 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8713 first = 0;
8714 op = TREE_OPERAND (op, 0);
8715 /* Keep trying to narrow, but don't assign op to win if it
8716 would turn an integral type into something else. */
8717 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8718 continue;
8721 win = op;
8724 if (TREE_CODE (op) == COMPONENT_REF
8725 /* Since type_for_size always gives an integer type. */
8726 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8727 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8728 /* Ensure field is laid out already. */
8729 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8730 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
8732 unsigned HOST_WIDE_INT innerprec
8733 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
8734 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8735 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8736 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8738 /* We can get this structure field in a narrower type that fits it,
8739 but the resulting extension to its nominal type (a fullword type)
8740 must satisfy the same conditions as for other extensions.
8742 Do this only for fields that are aligned (not bit-fields),
8743 because when bit-field insns will be used there is no
8744 advantage in doing this. */
8746 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8747 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8748 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8749 && type != 0)
8751 if (first)
8752 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8753 win = fold_convert (type, op);
8757 *unsignedp_ptr = uns;
8758 return win;
8761 /* Returns true if integer constant C has a value that is permissible
8762 for type TYPE (an INTEGER_TYPE). */
8764 bool
8765 int_fits_type_p (const_tree c, const_tree type)
8767 tree type_low_bound, type_high_bound;
8768 bool ok_for_low_bound, ok_for_high_bound;
8769 signop sgn_c = TYPE_SIGN (TREE_TYPE (c));
8771 retry:
8772 type_low_bound = TYPE_MIN_VALUE (type);
8773 type_high_bound = TYPE_MAX_VALUE (type);
8775 /* If at least one bound of the type is a constant integer, we can check
8776 ourselves and maybe make a decision. If no such decision is possible, but
8777 this type is a subtype, try checking against that. Otherwise, use
8778 fits_to_tree_p, which checks against the precision.
8780 Compute the status for each possibly constant bound, and return if we see
8781 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8782 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8783 for "constant known to fit". */
8785 /* Check if c >= type_low_bound. */
8786 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8788 if (tree_int_cst_lt (c, type_low_bound))
8789 return false;
8790 ok_for_low_bound = true;
8792 else
8793 ok_for_low_bound = false;
8795 /* Check if c <= type_high_bound. */
8796 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8798 if (tree_int_cst_lt (type_high_bound, c))
8799 return false;
8800 ok_for_high_bound = true;
8802 else
8803 ok_for_high_bound = false;
8805 /* If the constant fits both bounds, the result is known. */
8806 if (ok_for_low_bound && ok_for_high_bound)
8807 return true;
8809 /* Perform some generic filtering which may allow making a decision
8810 even if the bounds are not constant. First, negative integers
8811 never fit in unsigned types, */
8812 if (TYPE_UNSIGNED (type) && sgn_c == SIGNED && wi::neg_p (c))
8813 return false;
8815 /* Second, narrower types always fit in wider ones. */
8816 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8817 return true;
8819 /* Third, unsigned integers with top bit set never fit signed types. */
8820 if (!TYPE_UNSIGNED (type) && sgn_c == UNSIGNED)
8822 int prec = GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (c))) - 1;
8823 if (prec < TYPE_PRECISION (TREE_TYPE (c)))
8825 /* When a tree_cst is converted to a wide-int, the precision
8826 is taken from the type. However, if the precision of the
8827 mode underneath the type is smaller than that, it is
8828 possible that the value will not fit. The test below
8829 fails if any bit is set between the sign bit of the
8830 underlying mode and the top bit of the type. */
8831 if (wi::ne_p (wi::zext (c, prec - 1), c))
8832 return false;
8834 else if (wi::neg_p (c))
8835 return false;
8838 /* If we haven't been able to decide at this point, there nothing more we
8839 can check ourselves here. Look at the base type if we have one and it
8840 has the same precision. */
8841 if (TREE_CODE (type) == INTEGER_TYPE
8842 && TREE_TYPE (type) != 0
8843 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8845 type = TREE_TYPE (type);
8846 goto retry;
8849 /* Or to fits_to_tree_p, if nothing else. */
8850 return wi::fits_to_tree_p (c, type);
8853 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8854 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8855 represented (assuming two's-complement arithmetic) within the bit
8856 precision of the type are returned instead. */
8858 void
8859 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8861 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8862 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8863 wi::to_mpz (TYPE_MIN_VALUE (type), min, TYPE_SIGN (type));
8864 else
8866 if (TYPE_UNSIGNED (type))
8867 mpz_set_ui (min, 0);
8868 else
8870 wide_int mn = wi::min_value (TYPE_PRECISION (type), SIGNED);
8871 wi::to_mpz (mn, min, SIGNED);
8875 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8876 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8877 wi::to_mpz (TYPE_MAX_VALUE (type), max, TYPE_SIGN (type));
8878 else
8880 wide_int mn = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
8881 wi::to_mpz (mn, max, TYPE_SIGN (type));
8885 /* Return true if VAR is an automatic variable defined in function FN. */
8887 bool
8888 auto_var_in_fn_p (const_tree var, const_tree fn)
8890 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8891 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8892 || TREE_CODE (var) == PARM_DECL)
8893 && ! TREE_STATIC (var))
8894 || TREE_CODE (var) == LABEL_DECL
8895 || TREE_CODE (var) == RESULT_DECL));
8898 /* Subprogram of following function. Called by walk_tree.
8900 Return *TP if it is an automatic variable or parameter of the
8901 function passed in as DATA. */
8903 static tree
8904 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8906 tree fn = (tree) data;
8908 if (TYPE_P (*tp))
8909 *walk_subtrees = 0;
8911 else if (DECL_P (*tp)
8912 && auto_var_in_fn_p (*tp, fn))
8913 return *tp;
8915 return NULL_TREE;
8918 /* Returns true if T is, contains, or refers to a type with variable
8919 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8920 arguments, but not the return type. If FN is nonzero, only return
8921 true if a modifier of the type or position of FN is a variable or
8922 parameter inside FN.
8924 This concept is more general than that of C99 'variably modified types':
8925 in C99, a struct type is never variably modified because a VLA may not
8926 appear as a structure member. However, in GNU C code like:
8928 struct S { int i[f()]; };
8930 is valid, and other languages may define similar constructs. */
8932 bool
8933 variably_modified_type_p (tree type, tree fn)
8935 tree t;
8937 /* Test if T is either variable (if FN is zero) or an expression containing
8938 a variable in FN. If TYPE isn't gimplified, return true also if
8939 gimplify_one_sizepos would gimplify the expression into a local
8940 variable. */
8941 #define RETURN_TRUE_IF_VAR(T) \
8942 do { tree _t = (T); \
8943 if (_t != NULL_TREE \
8944 && _t != error_mark_node \
8945 && TREE_CODE (_t) != INTEGER_CST \
8946 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8947 && (!fn \
8948 || (!TYPE_SIZES_GIMPLIFIED (type) \
8949 && !is_gimple_sizepos (_t)) \
8950 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8951 return true; } while (0)
8953 if (type == error_mark_node)
8954 return false;
8956 /* If TYPE itself has variable size, it is variably modified. */
8957 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8958 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8960 switch (TREE_CODE (type))
8962 case POINTER_TYPE:
8963 case REFERENCE_TYPE:
8964 case VECTOR_TYPE:
8965 if (variably_modified_type_p (TREE_TYPE (type), fn))
8966 return true;
8967 break;
8969 case FUNCTION_TYPE:
8970 case METHOD_TYPE:
8971 /* If TYPE is a function type, it is variably modified if the
8972 return type is variably modified. */
8973 if (variably_modified_type_p (TREE_TYPE (type), fn))
8974 return true;
8975 break;
8977 case INTEGER_TYPE:
8978 case REAL_TYPE:
8979 case FIXED_POINT_TYPE:
8980 case ENUMERAL_TYPE:
8981 case BOOLEAN_TYPE:
8982 /* Scalar types are variably modified if their end points
8983 aren't constant. */
8984 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8985 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8986 break;
8988 case RECORD_TYPE:
8989 case UNION_TYPE:
8990 case QUAL_UNION_TYPE:
8991 /* We can't see if any of the fields are variably-modified by the
8992 definition we normally use, since that would produce infinite
8993 recursion via pointers. */
8994 /* This is variably modified if some field's type is. */
8995 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8996 if (TREE_CODE (t) == FIELD_DECL)
8998 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8999 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
9000 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
9002 if (TREE_CODE (type) == QUAL_UNION_TYPE)
9003 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
9005 break;
9007 case ARRAY_TYPE:
9008 /* Do not call ourselves to avoid infinite recursion. This is
9009 variably modified if the element type is. */
9010 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
9011 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
9012 break;
9014 default:
9015 break;
9018 /* The current language may have other cases to check, but in general,
9019 all other types are not variably modified. */
9020 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
9022 #undef RETURN_TRUE_IF_VAR
9025 /* Given a DECL or TYPE, return the scope in which it was declared, or
9026 NULL_TREE if there is no containing scope. */
9028 tree
9029 get_containing_scope (const_tree t)
9031 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
9034 /* Return the innermost context enclosing DECL that is
9035 a FUNCTION_DECL, or zero if none. */
9037 tree
9038 decl_function_context (const_tree decl)
9040 tree context;
9042 if (TREE_CODE (decl) == ERROR_MARK)
9043 return 0;
9045 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
9046 where we look up the function at runtime. Such functions always take
9047 a first argument of type 'pointer to real context'.
9049 C++ should really be fixed to use DECL_CONTEXT for the real context,
9050 and use something else for the "virtual context". */
9051 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
9052 context
9053 = TYPE_MAIN_VARIANT
9054 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
9055 else
9056 context = DECL_CONTEXT (decl);
9058 while (context && TREE_CODE (context) != FUNCTION_DECL)
9060 if (TREE_CODE (context) == BLOCK)
9061 context = BLOCK_SUPERCONTEXT (context);
9062 else
9063 context = get_containing_scope (context);
9066 return context;
9069 /* Return the innermost context enclosing DECL that is
9070 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
9071 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
9073 tree
9074 decl_type_context (const_tree decl)
9076 tree context = DECL_CONTEXT (decl);
9078 while (context)
9079 switch (TREE_CODE (context))
9081 case NAMESPACE_DECL:
9082 case TRANSLATION_UNIT_DECL:
9083 return NULL_TREE;
9085 case RECORD_TYPE:
9086 case UNION_TYPE:
9087 case QUAL_UNION_TYPE:
9088 return context;
9090 case TYPE_DECL:
9091 case FUNCTION_DECL:
9092 context = DECL_CONTEXT (context);
9093 break;
9095 case BLOCK:
9096 context = BLOCK_SUPERCONTEXT (context);
9097 break;
9099 default:
9100 gcc_unreachable ();
9103 return NULL_TREE;
9106 /* CALL is a CALL_EXPR. Return the declaration for the function
9107 called, or NULL_TREE if the called function cannot be
9108 determined. */
9110 tree
9111 get_callee_fndecl (const_tree call)
9113 tree addr;
9115 if (call == error_mark_node)
9116 return error_mark_node;
9118 /* It's invalid to call this function with anything but a
9119 CALL_EXPR. */
9120 gcc_assert (TREE_CODE (call) == CALL_EXPR);
9122 /* The first operand to the CALL is the address of the function
9123 called. */
9124 addr = CALL_EXPR_FN (call);
9126 /* If there is no function, return early. */
9127 if (addr == NULL_TREE)
9128 return NULL_TREE;
9130 STRIP_NOPS (addr);
9132 /* If this is a readonly function pointer, extract its initial value. */
9133 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
9134 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
9135 && DECL_INITIAL (addr))
9136 addr = DECL_INITIAL (addr);
9138 /* If the address is just `&f' for some function `f', then we know
9139 that `f' is being called. */
9140 if (TREE_CODE (addr) == ADDR_EXPR
9141 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
9142 return TREE_OPERAND (addr, 0);
9144 /* We couldn't figure out what was being called. */
9145 return NULL_TREE;
9148 #define TREE_MEM_USAGE_SPACES 40
9150 /* Print debugging information about tree nodes generated during the compile,
9151 and any language-specific information. */
9153 void
9154 dump_tree_statistics (void)
9156 if (GATHER_STATISTICS)
9158 int i;
9159 int total_nodes, total_bytes;
9160 fprintf (stderr, "\nKind Nodes Bytes\n");
9161 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9162 total_nodes = total_bytes = 0;
9163 for (i = 0; i < (int) all_kinds; i++)
9165 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
9166 tree_node_counts[i], tree_node_sizes[i]);
9167 total_nodes += tree_node_counts[i];
9168 total_bytes += tree_node_sizes[i];
9170 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9171 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
9172 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9173 fprintf (stderr, "Code Nodes\n");
9174 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9175 for (i = 0; i < (int) MAX_TREE_CODES; i++)
9176 fprintf (stderr, "%-32s %7d\n", get_tree_code_name ((enum tree_code) i),
9177 tree_code_counts[i]);
9178 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9179 fprintf (stderr, "\n");
9180 ssanames_print_statistics ();
9181 fprintf (stderr, "\n");
9182 phinodes_print_statistics ();
9183 fprintf (stderr, "\n");
9185 else
9186 fprintf (stderr, "(No per-node statistics)\n");
9188 print_type_hash_statistics ();
9189 print_debug_expr_statistics ();
9190 print_value_expr_statistics ();
9191 lang_hooks.print_statistics ();
9194 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
9196 /* Generate a crc32 of a byte. */
9198 static unsigned
9199 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
9201 unsigned ix;
9203 for (ix = bits; ix--; value <<= 1)
9205 unsigned feedback;
9207 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
9208 chksum <<= 1;
9209 chksum ^= feedback;
9211 return chksum;
9214 /* Generate a crc32 of a 32-bit unsigned. */
9216 unsigned
9217 crc32_unsigned (unsigned chksum, unsigned value)
9219 return crc32_unsigned_bits (chksum, value, 32);
9222 /* Generate a crc32 of a byte. */
9224 unsigned
9225 crc32_byte (unsigned chksum, char byte)
9227 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
9230 /* Generate a crc32 of a string. */
9232 unsigned
9233 crc32_string (unsigned chksum, const char *string)
9237 chksum = crc32_byte (chksum, *string);
9239 while (*string++);
9240 return chksum;
9243 /* P is a string that will be used in a symbol. Mask out any characters
9244 that are not valid in that context. */
9246 void
9247 clean_symbol_name (char *p)
9249 for (; *p; p++)
9250 if (! (ISALNUM (*p)
9251 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
9252 || *p == '$'
9253 #endif
9254 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
9255 || *p == '.'
9256 #endif
9258 *p = '_';
9261 /* Generate a name for a special-purpose function.
9262 The generated name may need to be unique across the whole link.
9263 Changes to this function may also require corresponding changes to
9264 xstrdup_mask_random.
9265 TYPE is some string to identify the purpose of this function to the
9266 linker or collect2; it must start with an uppercase letter,
9267 one of:
9268 I - for constructors
9269 D - for destructors
9270 N - for C++ anonymous namespaces
9271 F - for DWARF unwind frame information. */
9273 tree
9274 get_file_function_name (const char *type)
9276 char *buf;
9277 const char *p;
9278 char *q;
9280 /* If we already have a name we know to be unique, just use that. */
9281 if (first_global_object_name)
9282 p = q = ASTRDUP (first_global_object_name);
9283 /* If the target is handling the constructors/destructors, they
9284 will be local to this file and the name is only necessary for
9285 debugging purposes.
9286 We also assign sub_I and sub_D sufixes to constructors called from
9287 the global static constructors. These are always local. */
9288 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9289 || (strncmp (type, "sub_", 4) == 0
9290 && (type[4] == 'I' || type[4] == 'D')))
9292 const char *file = main_input_filename;
9293 if (! file)
9294 file = LOCATION_FILE (input_location);
9295 /* Just use the file's basename, because the full pathname
9296 might be quite long. */
9297 p = q = ASTRDUP (lbasename (file));
9299 else
9301 /* Otherwise, the name must be unique across the entire link.
9302 We don't have anything that we know to be unique to this translation
9303 unit, so use what we do have and throw in some randomness. */
9304 unsigned len;
9305 const char *name = weak_global_object_name;
9306 const char *file = main_input_filename;
9308 if (! name)
9309 name = "";
9310 if (! file)
9311 file = LOCATION_FILE (input_location);
9313 len = strlen (file);
9314 q = (char *) alloca (9 + 17 + len + 1);
9315 memcpy (q, file, len + 1);
9317 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9318 crc32_string (0, name), get_random_seed (false));
9320 p = q;
9323 clean_symbol_name (q);
9324 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9325 + strlen (type));
9327 /* Set up the name of the file-level functions we may need.
9328 Use a global object (which is already required to be unique over
9329 the program) rather than the file name (which imposes extra
9330 constraints). */
9331 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9333 return get_identifier (buf);
9336 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9338 /* Complain that the tree code of NODE does not match the expected 0
9339 terminated list of trailing codes. The trailing code list can be
9340 empty, for a more vague error message. FILE, LINE, and FUNCTION
9341 are of the caller. */
9343 void
9344 tree_check_failed (const_tree node, const char *file,
9345 int line, const char *function, ...)
9347 va_list args;
9348 const char *buffer;
9349 unsigned length = 0;
9350 enum tree_code code;
9352 va_start (args, function);
9353 while ((code = (enum tree_code) va_arg (args, int)))
9354 length += 4 + strlen (get_tree_code_name (code));
9355 va_end (args);
9356 if (length)
9358 char *tmp;
9359 va_start (args, function);
9360 length += strlen ("expected ");
9361 buffer = tmp = (char *) alloca (length);
9362 length = 0;
9363 while ((code = (enum tree_code) va_arg (args, int)))
9365 const char *prefix = length ? " or " : "expected ";
9367 strcpy (tmp + length, prefix);
9368 length += strlen (prefix);
9369 strcpy (tmp + length, get_tree_code_name (code));
9370 length += strlen (get_tree_code_name (code));
9372 va_end (args);
9374 else
9375 buffer = "unexpected node";
9377 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9378 buffer, get_tree_code_name (TREE_CODE (node)),
9379 function, trim_filename (file), line);
9382 /* Complain that the tree code of NODE does match the expected 0
9383 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9384 the caller. */
9386 void
9387 tree_not_check_failed (const_tree node, const char *file,
9388 int line, const char *function, ...)
9390 va_list args;
9391 char *buffer;
9392 unsigned length = 0;
9393 enum tree_code code;
9395 va_start (args, function);
9396 while ((code = (enum tree_code) va_arg (args, int)))
9397 length += 4 + strlen (get_tree_code_name (code));
9398 va_end (args);
9399 va_start (args, function);
9400 buffer = (char *) alloca (length);
9401 length = 0;
9402 while ((code = (enum tree_code) va_arg (args, int)))
9404 if (length)
9406 strcpy (buffer + length, " or ");
9407 length += 4;
9409 strcpy (buffer + length, get_tree_code_name (code));
9410 length += strlen (get_tree_code_name (code));
9412 va_end (args);
9414 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9415 buffer, get_tree_code_name (TREE_CODE (node)),
9416 function, trim_filename (file), line);
9419 /* Similar to tree_check_failed, except that we check for a class of tree
9420 code, given in CL. */
9422 void
9423 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9424 const char *file, int line, const char *function)
9426 internal_error
9427 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9428 TREE_CODE_CLASS_STRING (cl),
9429 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9430 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9433 /* Similar to tree_check_failed, except that instead of specifying a
9434 dozen codes, use the knowledge that they're all sequential. */
9436 void
9437 tree_range_check_failed (const_tree node, const char *file, int line,
9438 const char *function, enum tree_code c1,
9439 enum tree_code c2)
9441 char *buffer;
9442 unsigned length = 0;
9443 unsigned int c;
9445 for (c = c1; c <= c2; ++c)
9446 length += 4 + strlen (get_tree_code_name ((enum tree_code) c));
9448 length += strlen ("expected ");
9449 buffer = (char *) alloca (length);
9450 length = 0;
9452 for (c = c1; c <= c2; ++c)
9454 const char *prefix = length ? " or " : "expected ";
9456 strcpy (buffer + length, prefix);
9457 length += strlen (prefix);
9458 strcpy (buffer + length, get_tree_code_name ((enum tree_code) c));
9459 length += strlen (get_tree_code_name ((enum tree_code) c));
9462 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9463 buffer, get_tree_code_name (TREE_CODE (node)),
9464 function, trim_filename (file), line);
9468 /* Similar to tree_check_failed, except that we check that a tree does
9469 not have the specified code, given in CL. */
9471 void
9472 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9473 const char *file, int line, const char *function)
9475 internal_error
9476 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9477 TREE_CODE_CLASS_STRING (cl),
9478 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9479 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9483 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9485 void
9486 omp_clause_check_failed (const_tree node, const char *file, int line,
9487 const char *function, enum omp_clause_code code)
9489 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9490 omp_clause_code_name[code], get_tree_code_name (TREE_CODE (node)),
9491 function, trim_filename (file), line);
9495 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9497 void
9498 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9499 const char *function, enum omp_clause_code c1,
9500 enum omp_clause_code c2)
9502 char *buffer;
9503 unsigned length = 0;
9504 unsigned int c;
9506 for (c = c1; c <= c2; ++c)
9507 length += 4 + strlen (omp_clause_code_name[c]);
9509 length += strlen ("expected ");
9510 buffer = (char *) alloca (length);
9511 length = 0;
9513 for (c = c1; c <= c2; ++c)
9515 const char *prefix = length ? " or " : "expected ";
9517 strcpy (buffer + length, prefix);
9518 length += strlen (prefix);
9519 strcpy (buffer + length, omp_clause_code_name[c]);
9520 length += strlen (omp_clause_code_name[c]);
9523 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9524 buffer, omp_clause_code_name[TREE_CODE (node)],
9525 function, trim_filename (file), line);
9529 #undef DEFTREESTRUCT
9530 #define DEFTREESTRUCT(VAL, NAME) NAME,
9532 static const char *ts_enum_names[] = {
9533 #include "treestruct.def"
9535 #undef DEFTREESTRUCT
9537 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9539 /* Similar to tree_class_check_failed, except that we check for
9540 whether CODE contains the tree structure identified by EN. */
9542 void
9543 tree_contains_struct_check_failed (const_tree node,
9544 const enum tree_node_structure_enum en,
9545 const char *file, int line,
9546 const char *function)
9548 internal_error
9549 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9550 TS_ENUM_NAME (en),
9551 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9555 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9556 (dynamically sized) vector. */
9558 void
9559 tree_int_cst_elt_check_failed (int idx, int len, const char *file, int line,
9560 const char *function)
9562 internal_error
9563 ("tree check: accessed elt %d of tree_int_cst with %d elts in %s, at %s:%d",
9564 idx + 1, len, function, trim_filename (file), line);
9567 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9568 (dynamically sized) vector. */
9570 void
9571 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9572 const char *function)
9574 internal_error
9575 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9576 idx + 1, len, function, trim_filename (file), line);
9579 /* Similar to above, except that the check is for the bounds of the operand
9580 vector of an expression node EXP. */
9582 void
9583 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9584 int line, const char *function)
9586 enum tree_code code = TREE_CODE (exp);
9587 internal_error
9588 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9589 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9590 function, trim_filename (file), line);
9593 /* Similar to above, except that the check is for the number of
9594 operands of an OMP_CLAUSE node. */
9596 void
9597 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9598 int line, const char *function)
9600 internal_error
9601 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9602 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9603 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9604 trim_filename (file), line);
9606 #endif /* ENABLE_TREE_CHECKING */
9608 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9609 and mapped to the machine mode MODE. Initialize its fields and build
9610 the information necessary for debugging output. */
9612 static tree
9613 make_vector_type (tree innertype, int nunits, machine_mode mode)
9615 tree t;
9616 inchash::hash hstate;
9618 t = make_node (VECTOR_TYPE);
9619 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9620 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9621 SET_TYPE_MODE (t, mode);
9623 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9624 SET_TYPE_STRUCTURAL_EQUALITY (t);
9625 else if (TYPE_CANONICAL (innertype) != innertype
9626 || mode != VOIDmode)
9627 TYPE_CANONICAL (t)
9628 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9630 layout_type (t);
9632 hstate.add_wide_int (VECTOR_TYPE);
9633 hstate.add_wide_int (nunits);
9634 hstate.add_wide_int (mode);
9635 hstate.add_object (TYPE_HASH (TREE_TYPE (t)));
9636 t = type_hash_canon (hstate.end (), t);
9638 /* We have built a main variant, based on the main variant of the
9639 inner type. Use it to build the variant we return. */
9640 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9641 && TREE_TYPE (t) != innertype)
9642 return build_type_attribute_qual_variant (t,
9643 TYPE_ATTRIBUTES (innertype),
9644 TYPE_QUALS (innertype));
9646 return t;
9649 static tree
9650 make_or_reuse_type (unsigned size, int unsignedp)
9652 int i;
9654 if (size == INT_TYPE_SIZE)
9655 return unsignedp ? unsigned_type_node : integer_type_node;
9656 if (size == CHAR_TYPE_SIZE)
9657 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9658 if (size == SHORT_TYPE_SIZE)
9659 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9660 if (size == LONG_TYPE_SIZE)
9661 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9662 if (size == LONG_LONG_TYPE_SIZE)
9663 return (unsignedp ? long_long_unsigned_type_node
9664 : long_long_integer_type_node);
9666 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9667 if (size == int_n_data[i].bitsize
9668 && int_n_enabled_p[i])
9669 return (unsignedp ? int_n_trees[i].unsigned_type
9670 : int_n_trees[i].signed_type);
9672 if (unsignedp)
9673 return make_unsigned_type (size);
9674 else
9675 return make_signed_type (size);
9678 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9680 static tree
9681 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9683 if (satp)
9685 if (size == SHORT_FRACT_TYPE_SIZE)
9686 return unsignedp ? sat_unsigned_short_fract_type_node
9687 : sat_short_fract_type_node;
9688 if (size == FRACT_TYPE_SIZE)
9689 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9690 if (size == LONG_FRACT_TYPE_SIZE)
9691 return unsignedp ? sat_unsigned_long_fract_type_node
9692 : sat_long_fract_type_node;
9693 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9694 return unsignedp ? sat_unsigned_long_long_fract_type_node
9695 : sat_long_long_fract_type_node;
9697 else
9699 if (size == SHORT_FRACT_TYPE_SIZE)
9700 return unsignedp ? unsigned_short_fract_type_node
9701 : short_fract_type_node;
9702 if (size == FRACT_TYPE_SIZE)
9703 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9704 if (size == LONG_FRACT_TYPE_SIZE)
9705 return unsignedp ? unsigned_long_fract_type_node
9706 : long_fract_type_node;
9707 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9708 return unsignedp ? unsigned_long_long_fract_type_node
9709 : long_long_fract_type_node;
9712 return make_fract_type (size, unsignedp, satp);
9715 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9717 static tree
9718 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9720 if (satp)
9722 if (size == SHORT_ACCUM_TYPE_SIZE)
9723 return unsignedp ? sat_unsigned_short_accum_type_node
9724 : sat_short_accum_type_node;
9725 if (size == ACCUM_TYPE_SIZE)
9726 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9727 if (size == LONG_ACCUM_TYPE_SIZE)
9728 return unsignedp ? sat_unsigned_long_accum_type_node
9729 : sat_long_accum_type_node;
9730 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9731 return unsignedp ? sat_unsigned_long_long_accum_type_node
9732 : sat_long_long_accum_type_node;
9734 else
9736 if (size == SHORT_ACCUM_TYPE_SIZE)
9737 return unsignedp ? unsigned_short_accum_type_node
9738 : short_accum_type_node;
9739 if (size == ACCUM_TYPE_SIZE)
9740 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9741 if (size == LONG_ACCUM_TYPE_SIZE)
9742 return unsignedp ? unsigned_long_accum_type_node
9743 : long_accum_type_node;
9744 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9745 return unsignedp ? unsigned_long_long_accum_type_node
9746 : long_long_accum_type_node;
9749 return make_accum_type (size, unsignedp, satp);
9753 /* Create an atomic variant node for TYPE. This routine is called
9754 during initialization of data types to create the 5 basic atomic
9755 types. The generic build_variant_type function requires these to
9756 already be set up in order to function properly, so cannot be
9757 called from there. If ALIGN is non-zero, then ensure alignment is
9758 overridden to this value. */
9760 static tree
9761 build_atomic_base (tree type, unsigned int align)
9763 tree t;
9765 /* Make sure its not already registered. */
9766 if ((t = get_qualified_type (type, TYPE_QUAL_ATOMIC)))
9767 return t;
9769 t = build_variant_type_copy (type);
9770 set_type_quals (t, TYPE_QUAL_ATOMIC);
9772 if (align)
9773 TYPE_ALIGN (t) = align;
9775 return t;
9778 /* Create nodes for all integer types (and error_mark_node) using the sizes
9779 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9780 SHORT_DOUBLE specifies whether double should be of the same precision
9781 as float. */
9783 void
9784 build_common_tree_nodes (bool signed_char, bool short_double)
9786 int i;
9788 error_mark_node = make_node (ERROR_MARK);
9789 TREE_TYPE (error_mark_node) = error_mark_node;
9791 initialize_sizetypes ();
9793 /* Define both `signed char' and `unsigned char'. */
9794 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9795 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9796 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9797 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9799 /* Define `char', which is like either `signed char' or `unsigned char'
9800 but not the same as either. */
9801 char_type_node
9802 = (signed_char
9803 ? make_signed_type (CHAR_TYPE_SIZE)
9804 : make_unsigned_type (CHAR_TYPE_SIZE));
9805 TYPE_STRING_FLAG (char_type_node) = 1;
9807 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9808 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9809 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9810 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9811 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9812 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9813 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9814 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9816 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9818 int_n_trees[i].signed_type = make_signed_type (int_n_data[i].bitsize);
9819 int_n_trees[i].unsigned_type = make_unsigned_type (int_n_data[i].bitsize);
9820 TYPE_SIZE (int_n_trees[i].signed_type) = bitsize_int (int_n_data[i].bitsize);
9821 TYPE_SIZE (int_n_trees[i].unsigned_type) = bitsize_int (int_n_data[i].bitsize);
9823 if (int_n_data[i].bitsize > LONG_LONG_TYPE_SIZE
9824 && int_n_enabled_p[i])
9826 integer_types[itk_intN_0 + i * 2] = int_n_trees[i].signed_type;
9827 integer_types[itk_unsigned_intN_0 + i * 2] = int_n_trees[i].unsigned_type;
9831 /* Define a boolean type. This type only represents boolean values but
9832 may be larger than char depending on the value of BOOL_TYPE_SIZE. */
9833 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9834 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9835 TYPE_PRECISION (boolean_type_node) = 1;
9836 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9838 /* Define what type to use for size_t. */
9839 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9840 size_type_node = unsigned_type_node;
9841 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9842 size_type_node = long_unsigned_type_node;
9843 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9844 size_type_node = long_long_unsigned_type_node;
9845 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9846 size_type_node = short_unsigned_type_node;
9847 else
9849 int i;
9851 size_type_node = NULL_TREE;
9852 for (i = 0; i < NUM_INT_N_ENTS; i++)
9853 if (int_n_enabled_p[i])
9855 char name[50];
9856 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
9858 if (strcmp (name, SIZE_TYPE) == 0)
9860 size_type_node = int_n_trees[i].unsigned_type;
9863 if (size_type_node == NULL_TREE)
9864 gcc_unreachable ();
9867 /* Fill in the rest of the sized types. Reuse existing type nodes
9868 when possible. */
9869 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9870 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9871 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9872 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9873 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9875 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9876 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9877 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9878 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9879 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9881 /* Don't call build_qualified type for atomics. That routine does
9882 special processing for atomics, and until they are initialized
9883 it's better not to make that call.
9885 Check to see if there is a target override for atomic types. */
9887 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
9888 targetm.atomic_align_for_mode (QImode));
9889 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
9890 targetm.atomic_align_for_mode (HImode));
9891 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
9892 targetm.atomic_align_for_mode (SImode));
9893 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
9894 targetm.atomic_align_for_mode (DImode));
9895 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
9896 targetm.atomic_align_for_mode (TImode));
9898 access_public_node = get_identifier ("public");
9899 access_protected_node = get_identifier ("protected");
9900 access_private_node = get_identifier ("private");
9902 /* Define these next since types below may used them. */
9903 integer_zero_node = build_int_cst (integer_type_node, 0);
9904 integer_one_node = build_int_cst (integer_type_node, 1);
9905 integer_three_node = build_int_cst (integer_type_node, 3);
9906 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9908 size_zero_node = size_int (0);
9909 size_one_node = size_int (1);
9910 bitsize_zero_node = bitsize_int (0);
9911 bitsize_one_node = bitsize_int (1);
9912 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9914 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9915 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9917 void_type_node = make_node (VOID_TYPE);
9918 layout_type (void_type_node);
9920 pointer_bounds_type_node = targetm.chkp_bound_type ();
9922 /* We are not going to have real types in C with less than byte alignment,
9923 so we might as well not have any types that claim to have it. */
9924 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9925 TYPE_USER_ALIGN (void_type_node) = 0;
9927 void_node = make_node (VOID_CST);
9928 TREE_TYPE (void_node) = void_type_node;
9930 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9931 layout_type (TREE_TYPE (null_pointer_node));
9933 ptr_type_node = build_pointer_type (void_type_node);
9934 const_ptr_type_node
9935 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9936 fileptr_type_node = ptr_type_node;
9938 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
9940 float_type_node = make_node (REAL_TYPE);
9941 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9942 layout_type (float_type_node);
9944 double_type_node = make_node (REAL_TYPE);
9945 if (short_double)
9946 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9947 else
9948 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9949 layout_type (double_type_node);
9951 long_double_type_node = make_node (REAL_TYPE);
9952 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9953 layout_type (long_double_type_node);
9955 float_ptr_type_node = build_pointer_type (float_type_node);
9956 double_ptr_type_node = build_pointer_type (double_type_node);
9957 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9958 integer_ptr_type_node = build_pointer_type (integer_type_node);
9960 /* Fixed size integer types. */
9961 uint16_type_node = make_or_reuse_type (16, 1);
9962 uint32_type_node = make_or_reuse_type (32, 1);
9963 uint64_type_node = make_or_reuse_type (64, 1);
9965 /* Decimal float types. */
9966 dfloat32_type_node = make_node (REAL_TYPE);
9967 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9968 layout_type (dfloat32_type_node);
9969 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9970 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9972 dfloat64_type_node = make_node (REAL_TYPE);
9973 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9974 layout_type (dfloat64_type_node);
9975 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9976 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9978 dfloat128_type_node = make_node (REAL_TYPE);
9979 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9980 layout_type (dfloat128_type_node);
9981 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9982 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9984 complex_integer_type_node = build_complex_type (integer_type_node);
9985 complex_float_type_node = build_complex_type (float_type_node);
9986 complex_double_type_node = build_complex_type (double_type_node);
9987 complex_long_double_type_node = build_complex_type (long_double_type_node);
9989 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9990 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9991 sat_ ## KIND ## _type_node = \
9992 make_sat_signed_ ## KIND ## _type (SIZE); \
9993 sat_unsigned_ ## KIND ## _type_node = \
9994 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9995 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9996 unsigned_ ## KIND ## _type_node = \
9997 make_unsigned_ ## KIND ## _type (SIZE);
9999 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
10000 sat_ ## WIDTH ## KIND ## _type_node = \
10001 make_sat_signed_ ## KIND ## _type (SIZE); \
10002 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
10003 make_sat_unsigned_ ## KIND ## _type (SIZE); \
10004 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
10005 unsigned_ ## WIDTH ## KIND ## _type_node = \
10006 make_unsigned_ ## KIND ## _type (SIZE);
10008 /* Make fixed-point type nodes based on four different widths. */
10009 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
10010 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
10011 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
10012 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
10013 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
10015 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
10016 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
10017 NAME ## _type_node = \
10018 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
10019 u ## NAME ## _type_node = \
10020 make_or_reuse_unsigned_ ## KIND ## _type \
10021 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
10022 sat_ ## NAME ## _type_node = \
10023 make_or_reuse_sat_signed_ ## KIND ## _type \
10024 (GET_MODE_BITSIZE (MODE ## mode)); \
10025 sat_u ## NAME ## _type_node = \
10026 make_or_reuse_sat_unsigned_ ## KIND ## _type \
10027 (GET_MODE_BITSIZE (U ## MODE ## mode));
10029 /* Fixed-point type and mode nodes. */
10030 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
10031 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
10032 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
10033 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
10034 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
10035 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
10036 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
10037 MAKE_FIXED_MODE_NODE (accum, ha, HA)
10038 MAKE_FIXED_MODE_NODE (accum, sa, SA)
10039 MAKE_FIXED_MODE_NODE (accum, da, DA)
10040 MAKE_FIXED_MODE_NODE (accum, ta, TA)
10043 tree t = targetm.build_builtin_va_list ();
10045 /* Many back-ends define record types without setting TYPE_NAME.
10046 If we copied the record type here, we'd keep the original
10047 record type without a name. This breaks name mangling. So,
10048 don't copy record types and let c_common_nodes_and_builtins()
10049 declare the type to be __builtin_va_list. */
10050 if (TREE_CODE (t) != RECORD_TYPE)
10051 t = build_variant_type_copy (t);
10053 va_list_type_node = t;
10057 /* Modify DECL for given flags.
10058 TM_PURE attribute is set only on types, so the function will modify
10059 DECL's type when ECF_TM_PURE is used. */
10061 void
10062 set_call_expr_flags (tree decl, int flags)
10064 if (flags & ECF_NOTHROW)
10065 TREE_NOTHROW (decl) = 1;
10066 if (flags & ECF_CONST)
10067 TREE_READONLY (decl) = 1;
10068 if (flags & ECF_PURE)
10069 DECL_PURE_P (decl) = 1;
10070 if (flags & ECF_LOOPING_CONST_OR_PURE)
10071 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
10072 if (flags & ECF_NOVOPS)
10073 DECL_IS_NOVOPS (decl) = 1;
10074 if (flags & ECF_NORETURN)
10075 TREE_THIS_VOLATILE (decl) = 1;
10076 if (flags & ECF_MALLOC)
10077 DECL_IS_MALLOC (decl) = 1;
10078 if (flags & ECF_RETURNS_TWICE)
10079 DECL_IS_RETURNS_TWICE (decl) = 1;
10080 if (flags & ECF_LEAF)
10081 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
10082 NULL, DECL_ATTRIBUTES (decl));
10083 if ((flags & ECF_TM_PURE) && flag_tm)
10084 apply_tm_attr (decl, get_identifier ("transaction_pure"));
10085 /* Looping const or pure is implied by noreturn.
10086 There is currently no way to declare looping const or looping pure alone. */
10087 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
10088 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
10092 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
10094 static void
10095 local_define_builtin (const char *name, tree type, enum built_in_function code,
10096 const char *library_name, int ecf_flags)
10098 tree decl;
10100 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
10101 library_name, NULL_TREE);
10102 set_call_expr_flags (decl, ecf_flags);
10104 set_builtin_decl (code, decl, true);
10107 /* Call this function after instantiating all builtins that the language
10108 front end cares about. This will build the rest of the builtins
10109 and internal functions that are relied upon by the tree optimizers and
10110 the middle-end. */
10112 void
10113 build_common_builtin_nodes (void)
10115 tree tmp, ftype;
10116 int ecf_flags;
10118 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
10120 ftype = build_function_type (void_type_node, void_list_node);
10121 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
10122 "__builtin_unreachable",
10123 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
10124 | ECF_CONST);
10127 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
10128 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10130 ftype = build_function_type_list (ptr_type_node,
10131 ptr_type_node, const_ptr_type_node,
10132 size_type_node, NULL_TREE);
10134 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
10135 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
10136 "memcpy", ECF_NOTHROW | ECF_LEAF);
10137 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10138 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
10139 "memmove", ECF_NOTHROW | ECF_LEAF);
10142 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
10144 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
10145 const_ptr_type_node, size_type_node,
10146 NULL_TREE);
10147 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
10148 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10151 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
10153 ftype = build_function_type_list (ptr_type_node,
10154 ptr_type_node, integer_type_node,
10155 size_type_node, NULL_TREE);
10156 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
10157 "memset", ECF_NOTHROW | ECF_LEAF);
10160 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
10162 ftype = build_function_type_list (ptr_type_node,
10163 size_type_node, NULL_TREE);
10164 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
10165 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
10168 ftype = build_function_type_list (ptr_type_node, size_type_node,
10169 size_type_node, NULL_TREE);
10170 local_define_builtin ("__builtin_alloca_with_align", ftype,
10171 BUILT_IN_ALLOCA_WITH_ALIGN,
10172 "__builtin_alloca_with_align",
10173 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
10175 /* If we're checking the stack, `alloca' can throw. */
10176 if (flag_stack_check)
10178 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
10179 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
10182 ftype = build_function_type_list (void_type_node,
10183 ptr_type_node, ptr_type_node,
10184 ptr_type_node, NULL_TREE);
10185 local_define_builtin ("__builtin_init_trampoline", ftype,
10186 BUILT_IN_INIT_TRAMPOLINE,
10187 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
10188 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
10189 BUILT_IN_INIT_HEAP_TRAMPOLINE,
10190 "__builtin_init_heap_trampoline",
10191 ECF_NOTHROW | ECF_LEAF);
10193 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
10194 local_define_builtin ("__builtin_adjust_trampoline", ftype,
10195 BUILT_IN_ADJUST_TRAMPOLINE,
10196 "__builtin_adjust_trampoline",
10197 ECF_CONST | ECF_NOTHROW);
10199 ftype = build_function_type_list (void_type_node,
10200 ptr_type_node, ptr_type_node, NULL_TREE);
10201 local_define_builtin ("__builtin_nonlocal_goto", ftype,
10202 BUILT_IN_NONLOCAL_GOTO,
10203 "__builtin_nonlocal_goto",
10204 ECF_NORETURN | ECF_NOTHROW);
10206 ftype = build_function_type_list (void_type_node,
10207 ptr_type_node, ptr_type_node, NULL_TREE);
10208 local_define_builtin ("__builtin_setjmp_setup", ftype,
10209 BUILT_IN_SETJMP_SETUP,
10210 "__builtin_setjmp_setup", ECF_NOTHROW);
10212 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10213 local_define_builtin ("__builtin_setjmp_receiver", ftype,
10214 BUILT_IN_SETJMP_RECEIVER,
10215 "__builtin_setjmp_receiver", ECF_NOTHROW | ECF_LEAF);
10217 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
10218 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
10219 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
10221 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10222 local_define_builtin ("__builtin_stack_restore", ftype,
10223 BUILT_IN_STACK_RESTORE,
10224 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
10226 /* If there's a possibility that we might use the ARM EABI, build the
10227 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
10228 if (targetm.arm_eabi_unwinder)
10230 ftype = build_function_type_list (void_type_node, NULL_TREE);
10231 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
10232 BUILT_IN_CXA_END_CLEANUP,
10233 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
10236 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10237 local_define_builtin ("__builtin_unwind_resume", ftype,
10238 BUILT_IN_UNWIND_RESUME,
10239 ((targetm_common.except_unwind_info (&global_options)
10240 == UI_SJLJ)
10241 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
10242 ECF_NORETURN);
10244 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
10246 ftype = build_function_type_list (ptr_type_node, integer_type_node,
10247 NULL_TREE);
10248 local_define_builtin ("__builtin_return_address", ftype,
10249 BUILT_IN_RETURN_ADDRESS,
10250 "__builtin_return_address",
10251 ECF_NOTHROW);
10254 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
10255 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10257 ftype = build_function_type_list (void_type_node, ptr_type_node,
10258 ptr_type_node, NULL_TREE);
10259 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
10260 local_define_builtin ("__cyg_profile_func_enter", ftype,
10261 BUILT_IN_PROFILE_FUNC_ENTER,
10262 "__cyg_profile_func_enter", 0);
10263 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10264 local_define_builtin ("__cyg_profile_func_exit", ftype,
10265 BUILT_IN_PROFILE_FUNC_EXIT,
10266 "__cyg_profile_func_exit", 0);
10269 /* The exception object and filter values from the runtime. The argument
10270 must be zero before exception lowering, i.e. from the front end. After
10271 exception lowering, it will be the region number for the exception
10272 landing pad. These functions are PURE instead of CONST to prevent
10273 them from being hoisted past the exception edge that will initialize
10274 its value in the landing pad. */
10275 ftype = build_function_type_list (ptr_type_node,
10276 integer_type_node, NULL_TREE);
10277 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
10278 /* Only use TM_PURE if we we have TM language support. */
10279 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
10280 ecf_flags |= ECF_TM_PURE;
10281 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
10282 "__builtin_eh_pointer", ecf_flags);
10284 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
10285 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
10286 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
10287 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10289 ftype = build_function_type_list (void_type_node,
10290 integer_type_node, integer_type_node,
10291 NULL_TREE);
10292 local_define_builtin ("__builtin_eh_copy_values", ftype,
10293 BUILT_IN_EH_COPY_VALUES,
10294 "__builtin_eh_copy_values", ECF_NOTHROW);
10296 /* Complex multiplication and division. These are handled as builtins
10297 rather than optabs because emit_library_call_value doesn't support
10298 complex. Further, we can do slightly better with folding these
10299 beasties if the real and complex parts of the arguments are separate. */
10301 int mode;
10303 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10305 char mode_name_buf[4], *q;
10306 const char *p;
10307 enum built_in_function mcode, dcode;
10308 tree type, inner_type;
10309 const char *prefix = "__";
10311 if (targetm.libfunc_gnu_prefix)
10312 prefix = "__gnu_";
10314 type = lang_hooks.types.type_for_mode ((machine_mode) mode, 0);
10315 if (type == NULL)
10316 continue;
10317 inner_type = TREE_TYPE (type);
10319 ftype = build_function_type_list (type, inner_type, inner_type,
10320 inner_type, inner_type, NULL_TREE);
10322 mcode = ((enum built_in_function)
10323 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10324 dcode = ((enum built_in_function)
10325 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10327 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10328 *q = TOLOWER (*p);
10329 *q = '\0';
10331 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10332 NULL);
10333 local_define_builtin (built_in_names[mcode], ftype, mcode,
10334 built_in_names[mcode],
10335 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10337 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10338 NULL);
10339 local_define_builtin (built_in_names[dcode], ftype, dcode,
10340 built_in_names[dcode],
10341 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10345 init_internal_fns ();
10348 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10349 better way.
10351 If we requested a pointer to a vector, build up the pointers that
10352 we stripped off while looking for the inner type. Similarly for
10353 return values from functions.
10355 The argument TYPE is the top of the chain, and BOTTOM is the
10356 new type which we will point to. */
10358 tree
10359 reconstruct_complex_type (tree type, tree bottom)
10361 tree inner, outer;
10363 if (TREE_CODE (type) == POINTER_TYPE)
10365 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10366 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10367 TYPE_REF_CAN_ALIAS_ALL (type));
10369 else if (TREE_CODE (type) == REFERENCE_TYPE)
10371 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10372 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10373 TYPE_REF_CAN_ALIAS_ALL (type));
10375 else if (TREE_CODE (type) == ARRAY_TYPE)
10377 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10378 outer = build_array_type (inner, TYPE_DOMAIN (type));
10380 else if (TREE_CODE (type) == FUNCTION_TYPE)
10382 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10383 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
10385 else if (TREE_CODE (type) == METHOD_TYPE)
10387 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10388 /* The build_method_type_directly() routine prepends 'this' to argument list,
10389 so we must compensate by getting rid of it. */
10390 outer
10391 = build_method_type_directly
10392 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10393 inner,
10394 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10396 else if (TREE_CODE (type) == OFFSET_TYPE)
10398 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10399 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10401 else
10402 return bottom;
10404 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10405 TYPE_QUALS (type));
10408 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10409 the inner type. */
10410 tree
10411 build_vector_type_for_mode (tree innertype, machine_mode mode)
10413 int nunits;
10415 switch (GET_MODE_CLASS (mode))
10417 case MODE_VECTOR_INT:
10418 case MODE_VECTOR_FLOAT:
10419 case MODE_VECTOR_FRACT:
10420 case MODE_VECTOR_UFRACT:
10421 case MODE_VECTOR_ACCUM:
10422 case MODE_VECTOR_UACCUM:
10423 nunits = GET_MODE_NUNITS (mode);
10424 break;
10426 case MODE_INT:
10427 /* Check that there are no leftover bits. */
10428 gcc_assert (GET_MODE_BITSIZE (mode)
10429 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10431 nunits = GET_MODE_BITSIZE (mode)
10432 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10433 break;
10435 default:
10436 gcc_unreachable ();
10439 return make_vector_type (innertype, nunits, mode);
10442 /* Similarly, but takes the inner type and number of units, which must be
10443 a power of two. */
10445 tree
10446 build_vector_type (tree innertype, int nunits)
10448 return make_vector_type (innertype, nunits, VOIDmode);
10451 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10453 tree
10454 build_opaque_vector_type (tree innertype, int nunits)
10456 tree t = make_vector_type (innertype, nunits, VOIDmode);
10457 tree cand;
10458 /* We always build the non-opaque variant before the opaque one,
10459 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10460 cand = TYPE_NEXT_VARIANT (t);
10461 if (cand
10462 && TYPE_VECTOR_OPAQUE (cand)
10463 && check_qualified_type (cand, t, TYPE_QUALS (t)))
10464 return cand;
10465 /* Othewise build a variant type and make sure to queue it after
10466 the non-opaque type. */
10467 cand = build_distinct_type_copy (t);
10468 TYPE_VECTOR_OPAQUE (cand) = true;
10469 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10470 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10471 TYPE_NEXT_VARIANT (t) = cand;
10472 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10473 return cand;
10477 /* Given an initializer INIT, return TRUE if INIT is zero or some
10478 aggregate of zeros. Otherwise return FALSE. */
10479 bool
10480 initializer_zerop (const_tree init)
10482 tree elt;
10484 STRIP_NOPS (init);
10486 switch (TREE_CODE (init))
10488 case INTEGER_CST:
10489 return integer_zerop (init);
10491 case REAL_CST:
10492 /* ??? Note that this is not correct for C4X float formats. There,
10493 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10494 negative exponent. */
10495 return real_zerop (init)
10496 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10498 case FIXED_CST:
10499 return fixed_zerop (init);
10501 case COMPLEX_CST:
10502 return integer_zerop (init)
10503 || (real_zerop (init)
10504 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10505 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10507 case VECTOR_CST:
10509 unsigned i;
10510 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10511 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10512 return false;
10513 return true;
10516 case CONSTRUCTOR:
10518 unsigned HOST_WIDE_INT idx;
10520 if (TREE_CLOBBER_P (init))
10521 return false;
10522 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10523 if (!initializer_zerop (elt))
10524 return false;
10525 return true;
10528 case STRING_CST:
10530 int i;
10532 /* We need to loop through all elements to handle cases like
10533 "\0" and "\0foobar". */
10534 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10535 if (TREE_STRING_POINTER (init)[i] != '\0')
10536 return false;
10538 return true;
10541 default:
10542 return false;
10546 /* Check if vector VEC consists of all the equal elements and
10547 that the number of elements corresponds to the type of VEC.
10548 The function returns first element of the vector
10549 or NULL_TREE if the vector is not uniform. */
10550 tree
10551 uniform_vector_p (const_tree vec)
10553 tree first, t;
10554 unsigned i;
10556 if (vec == NULL_TREE)
10557 return NULL_TREE;
10559 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10561 if (TREE_CODE (vec) == VECTOR_CST)
10563 first = VECTOR_CST_ELT (vec, 0);
10564 for (i = 1; i < VECTOR_CST_NELTS (vec); ++i)
10565 if (!operand_equal_p (first, VECTOR_CST_ELT (vec, i), 0))
10566 return NULL_TREE;
10568 return first;
10571 else if (TREE_CODE (vec) == CONSTRUCTOR)
10573 first = error_mark_node;
10575 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10577 if (i == 0)
10579 first = t;
10580 continue;
10582 if (!operand_equal_p (first, t, 0))
10583 return NULL_TREE;
10585 if (i != TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)))
10586 return NULL_TREE;
10588 return first;
10591 return NULL_TREE;
10594 /* Build an empty statement at location LOC. */
10596 tree
10597 build_empty_stmt (location_t loc)
10599 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10600 SET_EXPR_LOCATION (t, loc);
10601 return t;
10605 /* Build an OpenMP clause with code CODE. LOC is the location of the
10606 clause. */
10608 tree
10609 build_omp_clause (location_t loc, enum omp_clause_code code)
10611 tree t;
10612 int size, length;
10614 length = omp_clause_num_ops[code];
10615 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10617 record_node_allocation_statistics (OMP_CLAUSE, size);
10619 t = (tree) ggc_internal_alloc (size);
10620 memset (t, 0, size);
10621 TREE_SET_CODE (t, OMP_CLAUSE);
10622 OMP_CLAUSE_SET_CODE (t, code);
10623 OMP_CLAUSE_LOCATION (t) = loc;
10625 return t;
10628 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10629 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10630 Except for the CODE and operand count field, other storage for the
10631 object is initialized to zeros. */
10633 tree
10634 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10636 tree t;
10637 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10639 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10640 gcc_assert (len >= 1);
10642 record_node_allocation_statistics (code, length);
10644 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10646 TREE_SET_CODE (t, code);
10648 /* Can't use TREE_OPERAND to store the length because if checking is
10649 enabled, it will try to check the length before we store it. :-P */
10650 t->exp.operands[0] = build_int_cst (sizetype, len);
10652 return t;
10655 /* Helper function for build_call_* functions; build a CALL_EXPR with
10656 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10657 the argument slots. */
10659 static tree
10660 build_call_1 (tree return_type, tree fn, int nargs)
10662 tree t;
10664 t = build_vl_exp (CALL_EXPR, nargs + 3);
10665 TREE_TYPE (t) = return_type;
10666 CALL_EXPR_FN (t) = fn;
10667 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10669 return t;
10672 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10673 FN and a null static chain slot. NARGS is the number of call arguments
10674 which are specified as "..." arguments. */
10676 tree
10677 build_call_nary (tree return_type, tree fn, int nargs, ...)
10679 tree ret;
10680 va_list args;
10681 va_start (args, nargs);
10682 ret = build_call_valist (return_type, fn, nargs, args);
10683 va_end (args);
10684 return ret;
10687 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10688 FN and a null static chain slot. NARGS is the number of call arguments
10689 which are specified as a va_list ARGS. */
10691 tree
10692 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10694 tree t;
10695 int i;
10697 t = build_call_1 (return_type, fn, nargs);
10698 for (i = 0; i < nargs; i++)
10699 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10700 process_call_operands (t);
10701 return t;
10704 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10705 FN and a null static chain slot. NARGS is the number of call arguments
10706 which are specified as a tree array ARGS. */
10708 tree
10709 build_call_array_loc (location_t loc, tree return_type, tree fn,
10710 int nargs, const tree *args)
10712 tree t;
10713 int i;
10715 t = build_call_1 (return_type, fn, nargs);
10716 for (i = 0; i < nargs; i++)
10717 CALL_EXPR_ARG (t, i) = args[i];
10718 process_call_operands (t);
10719 SET_EXPR_LOCATION (t, loc);
10720 return t;
10723 /* Like build_call_array, but takes a vec. */
10725 tree
10726 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10728 tree ret, t;
10729 unsigned int ix;
10731 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10732 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10733 CALL_EXPR_ARG (ret, ix) = t;
10734 process_call_operands (ret);
10735 return ret;
10738 /* Conveniently construct a function call expression. FNDECL names the
10739 function to be called and N arguments are passed in the array
10740 ARGARRAY. */
10742 tree
10743 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
10745 tree fntype = TREE_TYPE (fndecl);
10746 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10748 return fold_build_call_array_loc (loc, TREE_TYPE (fntype), fn, n, argarray);
10751 /* Conveniently construct a function call expression. FNDECL names the
10752 function to be called and the arguments are passed in the vector
10753 VEC. */
10755 tree
10756 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
10758 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
10759 vec_safe_address (vec));
10763 /* Conveniently construct a function call expression. FNDECL names the
10764 function to be called, N is the number of arguments, and the "..."
10765 parameters are the argument expressions. */
10767 tree
10768 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
10770 va_list ap;
10771 tree *argarray = XALLOCAVEC (tree, n);
10772 int i;
10774 va_start (ap, n);
10775 for (i = 0; i < n; i++)
10776 argarray[i] = va_arg (ap, tree);
10777 va_end (ap);
10778 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10781 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
10782 varargs macros aren't supported by all bootstrap compilers. */
10784 tree
10785 build_call_expr (tree fndecl, int n, ...)
10787 va_list ap;
10788 tree *argarray = XALLOCAVEC (tree, n);
10789 int i;
10791 va_start (ap, n);
10792 for (i = 0; i < n; i++)
10793 argarray[i] = va_arg (ap, tree);
10794 va_end (ap);
10795 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
10798 /* Build internal call expression. This is just like CALL_EXPR, except
10799 its CALL_EXPR_FN is NULL. It will get gimplified later into ordinary
10800 internal function. */
10802 tree
10803 build_call_expr_internal_loc (location_t loc, enum internal_fn ifn,
10804 tree type, int n, ...)
10806 va_list ap;
10807 int i;
10809 tree fn = build_call_1 (type, NULL_TREE, n);
10810 va_start (ap, n);
10811 for (i = 0; i < n; i++)
10812 CALL_EXPR_ARG (fn, i) = va_arg (ap, tree);
10813 va_end (ap);
10814 SET_EXPR_LOCATION (fn, loc);
10815 CALL_EXPR_IFN (fn) = ifn;
10816 return fn;
10819 /* Create a new constant string literal and return a char* pointer to it.
10820 The STRING_CST value is the LEN characters at STR. */
10821 tree
10822 build_string_literal (int len, const char *str)
10824 tree t, elem, index, type;
10826 t = build_string (len, str);
10827 elem = build_type_variant (char_type_node, 1, 0);
10828 index = build_index_type (size_int (len - 1));
10829 type = build_array_type (elem, index);
10830 TREE_TYPE (t) = type;
10831 TREE_CONSTANT (t) = 1;
10832 TREE_READONLY (t) = 1;
10833 TREE_STATIC (t) = 1;
10835 type = build_pointer_type (elem);
10836 t = build1 (ADDR_EXPR, type,
10837 build4 (ARRAY_REF, elem,
10838 t, integer_zero_node, NULL_TREE, NULL_TREE));
10839 return t;
10844 /* Return true if T (assumed to be a DECL) must be assigned a memory
10845 location. */
10847 bool
10848 needs_to_live_in_memory (const_tree t)
10850 return (TREE_ADDRESSABLE (t)
10851 || is_global_var (t)
10852 || (TREE_CODE (t) == RESULT_DECL
10853 && !DECL_BY_REFERENCE (t)
10854 && aggregate_value_p (t, current_function_decl)));
10857 /* Return value of a constant X and sign-extend it. */
10859 HOST_WIDE_INT
10860 int_cst_value (const_tree x)
10862 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10863 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10865 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10866 gcc_assert (cst_and_fits_in_hwi (x));
10868 if (bits < HOST_BITS_PER_WIDE_INT)
10870 bool negative = ((val >> (bits - 1)) & 1) != 0;
10871 if (negative)
10872 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10873 else
10874 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10877 return val;
10880 /* If TYPE is an integral or pointer type, return an integer type with
10881 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10882 if TYPE is already an integer type of signedness UNSIGNEDP. */
10884 tree
10885 signed_or_unsigned_type_for (int unsignedp, tree type)
10887 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10888 return type;
10890 if (TREE_CODE (type) == VECTOR_TYPE)
10892 tree inner = TREE_TYPE (type);
10893 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10894 if (!inner2)
10895 return NULL_TREE;
10896 if (inner == inner2)
10897 return type;
10898 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10901 if (!INTEGRAL_TYPE_P (type)
10902 && !POINTER_TYPE_P (type)
10903 && TREE_CODE (type) != OFFSET_TYPE)
10904 return NULL_TREE;
10906 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10909 /* If TYPE is an integral or pointer type, return an integer type with
10910 the same precision which is unsigned, or itself if TYPE is already an
10911 unsigned integer type. */
10913 tree
10914 unsigned_type_for (tree type)
10916 return signed_or_unsigned_type_for (1, type);
10919 /* If TYPE is an integral or pointer type, return an integer type with
10920 the same precision which is signed, or itself if TYPE is already a
10921 signed integer type. */
10923 tree
10924 signed_type_for (tree type)
10926 return signed_or_unsigned_type_for (0, type);
10929 /* If TYPE is a vector type, return a signed integer vector type with the
10930 same width and number of subparts. Otherwise return boolean_type_node. */
10932 tree
10933 truth_type_for (tree type)
10935 if (TREE_CODE (type) == VECTOR_TYPE)
10937 tree elem = lang_hooks.types.type_for_size
10938 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10939 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10941 else
10942 return boolean_type_node;
10945 /* Returns the largest value obtainable by casting something in INNER type to
10946 OUTER type. */
10948 tree
10949 upper_bound_in_type (tree outer, tree inner)
10951 unsigned int det = 0;
10952 unsigned oprec = TYPE_PRECISION (outer);
10953 unsigned iprec = TYPE_PRECISION (inner);
10954 unsigned prec;
10956 /* Compute a unique number for every combination. */
10957 det |= (oprec > iprec) ? 4 : 0;
10958 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10959 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10961 /* Determine the exponent to use. */
10962 switch (det)
10964 case 0:
10965 case 1:
10966 /* oprec <= iprec, outer: signed, inner: don't care. */
10967 prec = oprec - 1;
10968 break;
10969 case 2:
10970 case 3:
10971 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10972 prec = oprec;
10973 break;
10974 case 4:
10975 /* oprec > iprec, outer: signed, inner: signed. */
10976 prec = iprec - 1;
10977 break;
10978 case 5:
10979 /* oprec > iprec, outer: signed, inner: unsigned. */
10980 prec = iprec;
10981 break;
10982 case 6:
10983 /* oprec > iprec, outer: unsigned, inner: signed. */
10984 prec = oprec;
10985 break;
10986 case 7:
10987 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10988 prec = iprec;
10989 break;
10990 default:
10991 gcc_unreachable ();
10994 return wide_int_to_tree (outer,
10995 wi::mask (prec, false, TYPE_PRECISION (outer)));
10998 /* Returns the smallest value obtainable by casting something in INNER type to
10999 OUTER type. */
11001 tree
11002 lower_bound_in_type (tree outer, tree inner)
11004 unsigned oprec = TYPE_PRECISION (outer);
11005 unsigned iprec = TYPE_PRECISION (inner);
11007 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
11008 and obtain 0. */
11009 if (TYPE_UNSIGNED (outer)
11010 /* If we are widening something of an unsigned type, OUTER type
11011 contains all values of INNER type. In particular, both INNER
11012 and OUTER types have zero in common. */
11013 || (oprec > iprec && TYPE_UNSIGNED (inner)))
11014 return build_int_cst (outer, 0);
11015 else
11017 /* If we are widening a signed type to another signed type, we
11018 want to obtain -2^^(iprec-1). If we are keeping the
11019 precision or narrowing to a signed type, we want to obtain
11020 -2^(oprec-1). */
11021 unsigned prec = oprec > iprec ? iprec : oprec;
11022 return wide_int_to_tree (outer,
11023 wi::mask (prec - 1, true,
11024 TYPE_PRECISION (outer)));
11028 /* Return nonzero if two operands that are suitable for PHI nodes are
11029 necessarily equal. Specifically, both ARG0 and ARG1 must be either
11030 SSA_NAME or invariant. Note that this is strictly an optimization.
11031 That is, callers of this function can directly call operand_equal_p
11032 and get the same result, only slower. */
11035 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
11037 if (arg0 == arg1)
11038 return 1;
11039 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
11040 return 0;
11041 return operand_equal_p (arg0, arg1, 0);
11044 /* Returns number of zeros at the end of binary representation of X. */
11046 tree
11047 num_ending_zeros (const_tree x)
11049 return build_int_cst (TREE_TYPE (x), wi::ctz (x));
11053 #define WALK_SUBTREE(NODE) \
11054 do \
11056 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
11057 if (result) \
11058 return result; \
11060 while (0)
11062 /* This is a subroutine of walk_tree that walks field of TYPE that are to
11063 be walked whenever a type is seen in the tree. Rest of operands and return
11064 value are as for walk_tree. */
11066 static tree
11067 walk_type_fields (tree type, walk_tree_fn func, void *data,
11068 hash_set<tree> *pset, walk_tree_lh lh)
11070 tree result = NULL_TREE;
11072 switch (TREE_CODE (type))
11074 case POINTER_TYPE:
11075 case REFERENCE_TYPE:
11076 case VECTOR_TYPE:
11077 /* We have to worry about mutually recursive pointers. These can't
11078 be written in C. They can in Ada. It's pathological, but
11079 there's an ACATS test (c38102a) that checks it. Deal with this
11080 by checking if we're pointing to another pointer, that one
11081 points to another pointer, that one does too, and we have no htab.
11082 If so, get a hash table. We check three levels deep to avoid
11083 the cost of the hash table if we don't need one. */
11084 if (POINTER_TYPE_P (TREE_TYPE (type))
11085 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
11086 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
11087 && !pset)
11089 result = walk_tree_without_duplicates (&TREE_TYPE (type),
11090 func, data);
11091 if (result)
11092 return result;
11094 break;
11097 /* ... fall through ... */
11099 case COMPLEX_TYPE:
11100 WALK_SUBTREE (TREE_TYPE (type));
11101 break;
11103 case METHOD_TYPE:
11104 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
11106 /* Fall through. */
11108 case FUNCTION_TYPE:
11109 WALK_SUBTREE (TREE_TYPE (type));
11111 tree arg;
11113 /* We never want to walk into default arguments. */
11114 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
11115 WALK_SUBTREE (TREE_VALUE (arg));
11117 break;
11119 case ARRAY_TYPE:
11120 /* Don't follow this nodes's type if a pointer for fear that
11121 we'll have infinite recursion. If we have a PSET, then we
11122 need not fear. */
11123 if (pset
11124 || (!POINTER_TYPE_P (TREE_TYPE (type))
11125 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
11126 WALK_SUBTREE (TREE_TYPE (type));
11127 WALK_SUBTREE (TYPE_DOMAIN (type));
11128 break;
11130 case OFFSET_TYPE:
11131 WALK_SUBTREE (TREE_TYPE (type));
11132 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
11133 break;
11135 default:
11136 break;
11139 return NULL_TREE;
11142 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
11143 called with the DATA and the address of each sub-tree. If FUNC returns a
11144 non-NULL value, the traversal is stopped, and the value returned by FUNC
11145 is returned. If PSET is non-NULL it is used to record the nodes visited,
11146 and to avoid visiting a node more than once. */
11148 tree
11149 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
11150 hash_set<tree> *pset, walk_tree_lh lh)
11152 enum tree_code code;
11153 int walk_subtrees;
11154 tree result;
11156 #define WALK_SUBTREE_TAIL(NODE) \
11157 do \
11159 tp = & (NODE); \
11160 goto tail_recurse; \
11162 while (0)
11164 tail_recurse:
11165 /* Skip empty subtrees. */
11166 if (!*tp)
11167 return NULL_TREE;
11169 /* Don't walk the same tree twice, if the user has requested
11170 that we avoid doing so. */
11171 if (pset && pset->add (*tp))
11172 return NULL_TREE;
11174 /* Call the function. */
11175 walk_subtrees = 1;
11176 result = (*func) (tp, &walk_subtrees, data);
11178 /* If we found something, return it. */
11179 if (result)
11180 return result;
11182 code = TREE_CODE (*tp);
11184 /* Even if we didn't, FUNC may have decided that there was nothing
11185 interesting below this point in the tree. */
11186 if (!walk_subtrees)
11188 /* But we still need to check our siblings. */
11189 if (code == TREE_LIST)
11190 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11191 else if (code == OMP_CLAUSE)
11192 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11193 else
11194 return NULL_TREE;
11197 if (lh)
11199 result = (*lh) (tp, &walk_subtrees, func, data, pset);
11200 if (result || !walk_subtrees)
11201 return result;
11204 switch (code)
11206 case ERROR_MARK:
11207 case IDENTIFIER_NODE:
11208 case INTEGER_CST:
11209 case REAL_CST:
11210 case FIXED_CST:
11211 case VECTOR_CST:
11212 case STRING_CST:
11213 case BLOCK:
11214 case PLACEHOLDER_EXPR:
11215 case SSA_NAME:
11216 case FIELD_DECL:
11217 case RESULT_DECL:
11218 /* None of these have subtrees other than those already walked
11219 above. */
11220 break;
11222 case TREE_LIST:
11223 WALK_SUBTREE (TREE_VALUE (*tp));
11224 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11225 break;
11227 case TREE_VEC:
11229 int len = TREE_VEC_LENGTH (*tp);
11231 if (len == 0)
11232 break;
11234 /* Walk all elements but the first. */
11235 while (--len)
11236 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
11238 /* Now walk the first one as a tail call. */
11239 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
11242 case COMPLEX_CST:
11243 WALK_SUBTREE (TREE_REALPART (*tp));
11244 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
11246 case CONSTRUCTOR:
11248 unsigned HOST_WIDE_INT idx;
11249 constructor_elt *ce;
11251 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*tp), idx, &ce);
11252 idx++)
11253 WALK_SUBTREE (ce->value);
11255 break;
11257 case SAVE_EXPR:
11258 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
11260 case BIND_EXPR:
11262 tree decl;
11263 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
11265 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11266 into declarations that are just mentioned, rather than
11267 declared; they don't really belong to this part of the tree.
11268 And, we can see cycles: the initializer for a declaration
11269 can refer to the declaration itself. */
11270 WALK_SUBTREE (DECL_INITIAL (decl));
11271 WALK_SUBTREE (DECL_SIZE (decl));
11272 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11274 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
11277 case STATEMENT_LIST:
11279 tree_stmt_iterator i;
11280 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
11281 WALK_SUBTREE (*tsi_stmt_ptr (i));
11283 break;
11285 case OMP_CLAUSE:
11286 switch (OMP_CLAUSE_CODE (*tp))
11288 case OMP_CLAUSE_GANG:
11289 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11290 /* FALLTHRU */
11292 case OMP_CLAUSE_DEVICE_RESIDENT:
11293 case OMP_CLAUSE_USE_DEVICE:
11294 case OMP_CLAUSE_ASYNC:
11295 case OMP_CLAUSE_WAIT:
11296 case OMP_CLAUSE_WORKER:
11297 case OMP_CLAUSE_VECTOR:
11298 case OMP_CLAUSE_NUM_GANGS:
11299 case OMP_CLAUSE_NUM_WORKERS:
11300 case OMP_CLAUSE_VECTOR_LENGTH:
11301 case OMP_CLAUSE_PRIVATE:
11302 case OMP_CLAUSE_SHARED:
11303 case OMP_CLAUSE_FIRSTPRIVATE:
11304 case OMP_CLAUSE_COPYIN:
11305 case OMP_CLAUSE_COPYPRIVATE:
11306 case OMP_CLAUSE_FINAL:
11307 case OMP_CLAUSE_IF:
11308 case OMP_CLAUSE_NUM_THREADS:
11309 case OMP_CLAUSE_SCHEDULE:
11310 case OMP_CLAUSE_UNIFORM:
11311 case OMP_CLAUSE_DEPEND:
11312 case OMP_CLAUSE_NUM_TEAMS:
11313 case OMP_CLAUSE_THREAD_LIMIT:
11314 case OMP_CLAUSE_DEVICE:
11315 case OMP_CLAUSE_DIST_SCHEDULE:
11316 case OMP_CLAUSE_SAFELEN:
11317 case OMP_CLAUSE_SIMDLEN:
11318 case OMP_CLAUSE__LOOPTEMP_:
11319 case OMP_CLAUSE__SIMDUID_:
11320 case OMP_CLAUSE__CILK_FOR_COUNT_:
11321 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
11322 /* FALLTHRU */
11324 case OMP_CLAUSE_INDEPENDENT:
11325 case OMP_CLAUSE_NOWAIT:
11326 case OMP_CLAUSE_ORDERED:
11327 case OMP_CLAUSE_DEFAULT:
11328 case OMP_CLAUSE_UNTIED:
11329 case OMP_CLAUSE_MERGEABLE:
11330 case OMP_CLAUSE_PROC_BIND:
11331 case OMP_CLAUSE_INBRANCH:
11332 case OMP_CLAUSE_NOTINBRANCH:
11333 case OMP_CLAUSE_FOR:
11334 case OMP_CLAUSE_PARALLEL:
11335 case OMP_CLAUSE_SECTIONS:
11336 case OMP_CLAUSE_TASKGROUP:
11337 case OMP_CLAUSE_AUTO:
11338 case OMP_CLAUSE_SEQ:
11339 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11341 case OMP_CLAUSE_LASTPRIVATE:
11342 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11343 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
11344 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11346 case OMP_CLAUSE_COLLAPSE:
11348 int i;
11349 for (i = 0; i < 3; i++)
11350 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11351 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11354 case OMP_CLAUSE_LINEAR:
11355 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11356 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STEP (*tp));
11357 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STMT (*tp));
11358 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11360 case OMP_CLAUSE_ALIGNED:
11361 case OMP_CLAUSE_FROM:
11362 case OMP_CLAUSE_TO:
11363 case OMP_CLAUSE_MAP:
11364 case OMP_CLAUSE__CACHE_:
11365 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11366 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11367 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11369 case OMP_CLAUSE_REDUCTION:
11371 int i;
11372 for (i = 0; i < 4; i++)
11373 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11374 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11377 default:
11378 gcc_unreachable ();
11380 break;
11382 case TARGET_EXPR:
11384 int i, len;
11386 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11387 But, we only want to walk once. */
11388 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11389 for (i = 0; i < len; ++i)
11390 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11391 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11394 case DECL_EXPR:
11395 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11396 defining. We only want to walk into these fields of a type in this
11397 case and not in the general case of a mere reference to the type.
11399 The criterion is as follows: if the field can be an expression, it
11400 must be walked only here. This should be in keeping with the fields
11401 that are directly gimplified in gimplify_type_sizes in order for the
11402 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11403 variable-sized types.
11405 Note that DECLs get walked as part of processing the BIND_EXPR. */
11406 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11408 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11409 if (TREE_CODE (*type_p) == ERROR_MARK)
11410 return NULL_TREE;
11412 /* Call the function for the type. See if it returns anything or
11413 doesn't want us to continue. If we are to continue, walk both
11414 the normal fields and those for the declaration case. */
11415 result = (*func) (type_p, &walk_subtrees, data);
11416 if (result || !walk_subtrees)
11417 return result;
11419 /* But do not walk a pointed-to type since it may itself need to
11420 be walked in the declaration case if it isn't anonymous. */
11421 if (!POINTER_TYPE_P (*type_p))
11423 result = walk_type_fields (*type_p, func, data, pset, lh);
11424 if (result)
11425 return result;
11428 /* If this is a record type, also walk the fields. */
11429 if (RECORD_OR_UNION_TYPE_P (*type_p))
11431 tree field;
11433 for (field = TYPE_FIELDS (*type_p); field;
11434 field = DECL_CHAIN (field))
11436 /* We'd like to look at the type of the field, but we can
11437 easily get infinite recursion. So assume it's pointed
11438 to elsewhere in the tree. Also, ignore things that
11439 aren't fields. */
11440 if (TREE_CODE (field) != FIELD_DECL)
11441 continue;
11443 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11444 WALK_SUBTREE (DECL_SIZE (field));
11445 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11446 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11447 WALK_SUBTREE (DECL_QUALIFIER (field));
11451 /* Same for scalar types. */
11452 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11453 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11454 || TREE_CODE (*type_p) == INTEGER_TYPE
11455 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11456 || TREE_CODE (*type_p) == REAL_TYPE)
11458 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11459 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11462 WALK_SUBTREE (TYPE_SIZE (*type_p));
11463 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11465 /* FALLTHRU */
11467 default:
11468 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11470 int i, len;
11472 /* Walk over all the sub-trees of this operand. */
11473 len = TREE_OPERAND_LENGTH (*tp);
11475 /* Go through the subtrees. We need to do this in forward order so
11476 that the scope of a FOR_EXPR is handled properly. */
11477 if (len)
11479 for (i = 0; i < len - 1; ++i)
11480 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11481 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11484 /* If this is a type, walk the needed fields in the type. */
11485 else if (TYPE_P (*tp))
11486 return walk_type_fields (*tp, func, data, pset, lh);
11487 break;
11490 /* We didn't find what we were looking for. */
11491 return NULL_TREE;
11493 #undef WALK_SUBTREE_TAIL
11495 #undef WALK_SUBTREE
11497 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11499 tree
11500 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11501 walk_tree_lh lh)
11503 tree result;
11505 hash_set<tree> pset;
11506 result = walk_tree_1 (tp, func, data, &pset, lh);
11507 return result;
11511 tree
11512 tree_block (tree t)
11514 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11516 if (IS_EXPR_CODE_CLASS (c))
11517 return LOCATION_BLOCK (t->exp.locus);
11518 gcc_unreachable ();
11519 return NULL;
11522 void
11523 tree_set_block (tree t, tree b)
11525 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11527 if (IS_EXPR_CODE_CLASS (c))
11529 if (b)
11530 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
11531 else
11532 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
11534 else
11535 gcc_unreachable ();
11538 /* Create a nameless artificial label and put it in the current
11539 function context. The label has a location of LOC. Returns the
11540 newly created label. */
11542 tree
11543 create_artificial_label (location_t loc)
11545 tree lab = build_decl (loc,
11546 LABEL_DECL, NULL_TREE, void_type_node);
11548 DECL_ARTIFICIAL (lab) = 1;
11549 DECL_IGNORED_P (lab) = 1;
11550 DECL_CONTEXT (lab) = current_function_decl;
11551 return lab;
11554 /* Given a tree, try to return a useful variable name that we can use
11555 to prefix a temporary that is being assigned the value of the tree.
11556 I.E. given <temp> = &A, return A. */
11558 const char *
11559 get_name (tree t)
11561 tree stripped_decl;
11563 stripped_decl = t;
11564 STRIP_NOPS (stripped_decl);
11565 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11566 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11567 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11569 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11570 if (!name)
11571 return NULL;
11572 return IDENTIFIER_POINTER (name);
11574 else
11576 switch (TREE_CODE (stripped_decl))
11578 case ADDR_EXPR:
11579 return get_name (TREE_OPERAND (stripped_decl, 0));
11580 default:
11581 return NULL;
11586 /* Return true if TYPE has a variable argument list. */
11588 bool
11589 stdarg_p (const_tree fntype)
11591 function_args_iterator args_iter;
11592 tree n = NULL_TREE, t;
11594 if (!fntype)
11595 return false;
11597 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
11599 n = t;
11602 return n != NULL_TREE && n != void_type_node;
11605 /* Return true if TYPE has a prototype. */
11607 bool
11608 prototype_p (const_tree fntype)
11610 tree t;
11612 gcc_assert (fntype != NULL_TREE);
11614 t = TYPE_ARG_TYPES (fntype);
11615 return (t != NULL_TREE);
11618 /* If BLOCK is inlined from an __attribute__((__artificial__))
11619 routine, return pointer to location from where it has been
11620 called. */
11621 location_t *
11622 block_nonartificial_location (tree block)
11624 location_t *ret = NULL;
11626 while (block && TREE_CODE (block) == BLOCK
11627 && BLOCK_ABSTRACT_ORIGIN (block))
11629 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11631 while (TREE_CODE (ao) == BLOCK
11632 && BLOCK_ABSTRACT_ORIGIN (ao)
11633 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11634 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11636 if (TREE_CODE (ao) == FUNCTION_DECL)
11638 /* If AO is an artificial inline, point RET to the
11639 call site locus at which it has been inlined and continue
11640 the loop, in case AO's caller is also an artificial
11641 inline. */
11642 if (DECL_DECLARED_INLINE_P (ao)
11643 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11644 ret = &BLOCK_SOURCE_LOCATION (block);
11645 else
11646 break;
11648 else if (TREE_CODE (ao) != BLOCK)
11649 break;
11651 block = BLOCK_SUPERCONTEXT (block);
11653 return ret;
11657 /* If EXP is inlined from an __attribute__((__artificial__))
11658 function, return the location of the original call expression. */
11660 location_t
11661 tree_nonartificial_location (tree exp)
11663 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11665 if (loc)
11666 return *loc;
11667 else
11668 return EXPR_LOCATION (exp);
11672 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11673 nodes. */
11675 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11677 hashval_t
11678 cl_option_hasher::hash (tree x)
11680 const_tree const t = x;
11681 const char *p;
11682 size_t i;
11683 size_t len = 0;
11684 hashval_t hash = 0;
11686 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11688 p = (const char *)TREE_OPTIMIZATION (t);
11689 len = sizeof (struct cl_optimization);
11692 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11693 return cl_target_option_hash (TREE_TARGET_OPTION (t));
11695 else
11696 gcc_unreachable ();
11698 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11699 something else. */
11700 for (i = 0; i < len; i++)
11701 if (p[i])
11702 hash = (hash << 4) ^ ((i << 2) | p[i]);
11704 return hash;
11707 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11708 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11709 same. */
11711 bool
11712 cl_option_hasher::equal (tree x, tree y)
11714 const_tree const xt = x;
11715 const_tree const yt = y;
11716 const char *xp;
11717 const char *yp;
11718 size_t len;
11720 if (TREE_CODE (xt) != TREE_CODE (yt))
11721 return 0;
11723 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11725 xp = (const char *)TREE_OPTIMIZATION (xt);
11726 yp = (const char *)TREE_OPTIMIZATION (yt);
11727 len = sizeof (struct cl_optimization);
11730 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11732 return cl_target_option_eq (TREE_TARGET_OPTION (xt),
11733 TREE_TARGET_OPTION (yt));
11736 else
11737 gcc_unreachable ();
11739 return (memcmp (xp, yp, len) == 0);
11742 /* Build an OPTIMIZATION_NODE based on the options in OPTS. */
11744 tree
11745 build_optimization_node (struct gcc_options *opts)
11747 tree t;
11749 /* Use the cache of optimization nodes. */
11751 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11752 opts);
11754 tree *slot = cl_option_hash_table->find_slot (cl_optimization_node, INSERT);
11755 t = *slot;
11756 if (!t)
11758 /* Insert this one into the hash table. */
11759 t = cl_optimization_node;
11760 *slot = t;
11762 /* Make a new node for next time round. */
11763 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11766 return t;
11769 /* Build a TARGET_OPTION_NODE based on the options in OPTS. */
11771 tree
11772 build_target_option_node (struct gcc_options *opts)
11774 tree t;
11776 /* Use the cache of optimization nodes. */
11778 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11779 opts);
11781 tree *slot = cl_option_hash_table->find_slot (cl_target_option_node, INSERT);
11782 t = *slot;
11783 if (!t)
11785 /* Insert this one into the hash table. */
11786 t = cl_target_option_node;
11787 *slot = t;
11789 /* Make a new node for next time round. */
11790 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11793 return t;
11796 /* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
11797 so that they aren't saved during PCH writing. */
11799 void
11800 prepare_target_option_nodes_for_pch (void)
11802 hash_table<cl_option_hasher>::iterator iter = cl_option_hash_table->begin ();
11803 for (; iter != cl_option_hash_table->end (); ++iter)
11804 if (TREE_CODE (*iter) == TARGET_OPTION_NODE)
11805 TREE_TARGET_GLOBALS (*iter) = NULL;
11808 /* Determine the "ultimate origin" of a block. The block may be an inlined
11809 instance of an inlined instance of a block which is local to an inline
11810 function, so we have to trace all of the way back through the origin chain
11811 to find out what sort of node actually served as the original seed for the
11812 given block. */
11814 tree
11815 block_ultimate_origin (const_tree block)
11817 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11819 /* BLOCK_ABSTRACT_ORIGIN can point to itself; ignore that if
11820 we're trying to output the abstract instance of this function. */
11821 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11822 return NULL_TREE;
11824 if (immediate_origin == NULL_TREE)
11825 return NULL_TREE;
11826 else
11828 tree ret_val;
11829 tree lookahead = immediate_origin;
11833 ret_val = lookahead;
11834 lookahead = (TREE_CODE (ret_val) == BLOCK
11835 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11837 while (lookahead != NULL && lookahead != ret_val);
11839 /* The block's abstract origin chain may not be the *ultimate* origin of
11840 the block. It could lead to a DECL that has an abstract origin set.
11841 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11842 will give us if it has one). Note that DECL's abstract origins are
11843 supposed to be the most distant ancestor (or so decl_ultimate_origin
11844 claims), so we don't need to loop following the DECL origins. */
11845 if (DECL_P (ret_val))
11846 return DECL_ORIGIN (ret_val);
11848 return ret_val;
11852 /* Return true iff conversion from INNER_TYPE to OUTER_TYPE generates
11853 no instruction. */
11855 bool
11856 tree_nop_conversion_p (const_tree outer_type, const_tree inner_type)
11858 /* Use precision rather then machine mode when we can, which gives
11859 the correct answer even for submode (bit-field) types. */
11860 if ((INTEGRAL_TYPE_P (outer_type)
11861 || POINTER_TYPE_P (outer_type)
11862 || TREE_CODE (outer_type) == OFFSET_TYPE)
11863 && (INTEGRAL_TYPE_P (inner_type)
11864 || POINTER_TYPE_P (inner_type)
11865 || TREE_CODE (inner_type) == OFFSET_TYPE))
11866 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11868 /* Otherwise fall back on comparing machine modes (e.g. for
11869 aggregate types, floats). */
11870 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11873 /* Return true iff conversion in EXP generates no instruction. Mark
11874 it inline so that we fully inline into the stripping functions even
11875 though we have two uses of this function. */
11877 static inline bool
11878 tree_nop_conversion (const_tree exp)
11880 tree outer_type, inner_type;
11882 if (!CONVERT_EXPR_P (exp)
11883 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11884 return false;
11885 if (TREE_OPERAND (exp, 0) == error_mark_node)
11886 return false;
11888 outer_type = TREE_TYPE (exp);
11889 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11891 if (!inner_type)
11892 return false;
11894 return tree_nop_conversion_p (outer_type, inner_type);
11897 /* Return true iff conversion in EXP generates no instruction. Don't
11898 consider conversions changing the signedness. */
11900 static bool
11901 tree_sign_nop_conversion (const_tree exp)
11903 tree outer_type, inner_type;
11905 if (!tree_nop_conversion (exp))
11906 return false;
11908 outer_type = TREE_TYPE (exp);
11909 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11911 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11912 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11915 /* Strip conversions from EXP according to tree_nop_conversion and
11916 return the resulting expression. */
11918 tree
11919 tree_strip_nop_conversions (tree exp)
11921 while (tree_nop_conversion (exp))
11922 exp = TREE_OPERAND (exp, 0);
11923 return exp;
11926 /* Strip conversions from EXP according to tree_sign_nop_conversion
11927 and return the resulting expression. */
11929 tree
11930 tree_strip_sign_nop_conversions (tree exp)
11932 while (tree_sign_nop_conversion (exp))
11933 exp = TREE_OPERAND (exp, 0);
11934 return exp;
11937 /* Avoid any floating point extensions from EXP. */
11938 tree
11939 strip_float_extensions (tree exp)
11941 tree sub, expt, subt;
11943 /* For floating point constant look up the narrowest type that can hold
11944 it properly and handle it like (type)(narrowest_type)constant.
11945 This way we can optimize for instance a=a*2.0 where "a" is float
11946 but 2.0 is double constant. */
11947 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11949 REAL_VALUE_TYPE orig;
11950 tree type = NULL;
11952 orig = TREE_REAL_CST (exp);
11953 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11954 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11955 type = float_type_node;
11956 else if (TYPE_PRECISION (TREE_TYPE (exp))
11957 > TYPE_PRECISION (double_type_node)
11958 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11959 type = double_type_node;
11960 if (type)
11961 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11964 if (!CONVERT_EXPR_P (exp))
11965 return exp;
11967 sub = TREE_OPERAND (exp, 0);
11968 subt = TREE_TYPE (sub);
11969 expt = TREE_TYPE (exp);
11971 if (!FLOAT_TYPE_P (subt))
11972 return exp;
11974 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11975 return exp;
11977 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11978 return exp;
11980 return strip_float_extensions (sub);
11983 /* Strip out all handled components that produce invariant
11984 offsets. */
11986 const_tree
11987 strip_invariant_refs (const_tree op)
11989 while (handled_component_p (op))
11991 switch (TREE_CODE (op))
11993 case ARRAY_REF:
11994 case ARRAY_RANGE_REF:
11995 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11996 || TREE_OPERAND (op, 2) != NULL_TREE
11997 || TREE_OPERAND (op, 3) != NULL_TREE)
11998 return NULL;
11999 break;
12001 case COMPONENT_REF:
12002 if (TREE_OPERAND (op, 2) != NULL_TREE)
12003 return NULL;
12004 break;
12006 default:;
12008 op = TREE_OPERAND (op, 0);
12011 return op;
12014 static GTY(()) tree gcc_eh_personality_decl;
12016 /* Return the GCC personality function decl. */
12018 tree
12019 lhd_gcc_personality (void)
12021 if (!gcc_eh_personality_decl)
12022 gcc_eh_personality_decl = build_personality_function ("gcc");
12023 return gcc_eh_personality_decl;
12026 /* TARGET is a call target of GIMPLE call statement
12027 (obtained by gimple_call_fn). Return true if it is
12028 OBJ_TYPE_REF representing an virtual call of C++ method.
12029 (As opposed to OBJ_TYPE_REF representing objc calls
12030 through a cast where middle-end devirtualization machinery
12031 can't apply.) */
12033 bool
12034 virtual_method_call_p (const_tree target)
12036 if (TREE_CODE (target) != OBJ_TYPE_REF)
12037 return false;
12038 tree t = TREE_TYPE (target);
12039 gcc_checking_assert (TREE_CODE (t) == POINTER_TYPE);
12040 t = TREE_TYPE (t);
12041 if (TREE_CODE (t) == FUNCTION_TYPE)
12042 return false;
12043 gcc_checking_assert (TREE_CODE (t) == METHOD_TYPE);
12044 /* If we do not have BINFO associated, it means that type was built
12045 without devirtualization enabled. Do not consider this a virtual
12046 call. */
12047 if (!TYPE_BINFO (obj_type_ref_class (target)))
12048 return false;
12049 return true;
12052 /* REF is OBJ_TYPE_REF, return the class the ref corresponds to. */
12054 tree
12055 obj_type_ref_class (const_tree ref)
12057 gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF);
12058 ref = TREE_TYPE (ref);
12059 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
12060 ref = TREE_TYPE (ref);
12061 /* We look for type THIS points to. ObjC also builds
12062 OBJ_TYPE_REF with non-method calls, Their first parameter
12063 ID however also corresponds to class type. */
12064 gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE
12065 || TREE_CODE (ref) == FUNCTION_TYPE);
12066 ref = TREE_VALUE (TYPE_ARG_TYPES (ref));
12067 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
12068 return TREE_TYPE (ref);
12071 /* Lookup sub-BINFO of BINFO of TYPE at offset POS. */
12073 static tree
12074 lookup_binfo_at_offset (tree binfo, tree type, HOST_WIDE_INT pos)
12076 unsigned int i;
12077 tree base_binfo, b;
12079 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12080 if (pos == tree_to_shwi (BINFO_OFFSET (base_binfo))
12081 && types_same_for_odr (TREE_TYPE (base_binfo), type))
12082 return base_binfo;
12083 else if ((b = lookup_binfo_at_offset (base_binfo, type, pos)) != NULL)
12084 return b;
12085 return NULL;
12088 /* Try to find a base info of BINFO that would have its field decl at offset
12089 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
12090 found, return, otherwise return NULL_TREE. */
12092 tree
12093 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
12095 tree type = BINFO_TYPE (binfo);
12097 while (true)
12099 HOST_WIDE_INT pos, size;
12100 tree fld;
12101 int i;
12103 if (types_same_for_odr (type, expected_type))
12104 return binfo;
12105 if (offset < 0)
12106 return NULL_TREE;
12108 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
12110 if (TREE_CODE (fld) != FIELD_DECL || !DECL_ARTIFICIAL (fld))
12111 continue;
12113 pos = int_bit_position (fld);
12114 size = tree_to_uhwi (DECL_SIZE (fld));
12115 if (pos <= offset && (pos + size) > offset)
12116 break;
12118 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
12119 return NULL_TREE;
12121 /* Offset 0 indicates the primary base, whose vtable contents are
12122 represented in the binfo for the derived class. */
12123 else if (offset != 0)
12125 tree found_binfo = NULL, base_binfo;
12126 /* Offsets in BINFO are in bytes relative to the whole structure
12127 while POS is in bits relative to the containing field. */
12128 int binfo_offset = (tree_to_shwi (BINFO_OFFSET (binfo)) + pos
12129 / BITS_PER_UNIT);
12131 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12132 if (tree_to_shwi (BINFO_OFFSET (base_binfo)) == binfo_offset
12133 && types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
12135 found_binfo = base_binfo;
12136 break;
12138 if (found_binfo)
12139 binfo = found_binfo;
12140 else
12141 binfo = lookup_binfo_at_offset (binfo, TREE_TYPE (fld),
12142 binfo_offset);
12145 type = TREE_TYPE (fld);
12146 offset -= pos;
12150 /* Returns true if X is a typedef decl. */
12152 bool
12153 is_typedef_decl (const_tree x)
12155 return (x && TREE_CODE (x) == TYPE_DECL
12156 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
12159 /* Returns true iff TYPE is a type variant created for a typedef. */
12161 bool
12162 typedef_variant_p (const_tree type)
12164 return is_typedef_decl (TYPE_NAME (type));
12167 /* Warn about a use of an identifier which was marked deprecated. */
12168 void
12169 warn_deprecated_use (tree node, tree attr)
12171 const char *msg;
12173 if (node == 0 || !warn_deprecated_decl)
12174 return;
12176 if (!attr)
12178 if (DECL_P (node))
12179 attr = DECL_ATTRIBUTES (node);
12180 else if (TYPE_P (node))
12182 tree decl = TYPE_STUB_DECL (node);
12183 if (decl)
12184 attr = lookup_attribute ("deprecated",
12185 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
12189 if (attr)
12190 attr = lookup_attribute ("deprecated", attr);
12192 if (attr)
12193 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
12194 else
12195 msg = NULL;
12197 bool w;
12198 if (DECL_P (node))
12200 if (msg)
12201 w = warning (OPT_Wdeprecated_declarations,
12202 "%qD is deprecated: %s", node, msg);
12203 else
12204 w = warning (OPT_Wdeprecated_declarations,
12205 "%qD is deprecated", node);
12206 if (w)
12207 inform (DECL_SOURCE_LOCATION (node), "declared here");
12209 else if (TYPE_P (node))
12211 tree what = NULL_TREE;
12212 tree decl = TYPE_STUB_DECL (node);
12214 if (TYPE_NAME (node))
12216 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12217 what = TYPE_NAME (node);
12218 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12219 && DECL_NAME (TYPE_NAME (node)))
12220 what = DECL_NAME (TYPE_NAME (node));
12223 if (decl)
12225 if (what)
12227 if (msg)
12228 w = warning (OPT_Wdeprecated_declarations,
12229 "%qE is deprecated: %s", what, msg);
12230 else
12231 w = warning (OPT_Wdeprecated_declarations,
12232 "%qE is deprecated", what);
12234 else
12236 if (msg)
12237 w = warning (OPT_Wdeprecated_declarations,
12238 "type is deprecated: %s", msg);
12239 else
12240 w = warning (OPT_Wdeprecated_declarations,
12241 "type is deprecated");
12243 if (w)
12244 inform (DECL_SOURCE_LOCATION (decl), "declared here");
12246 else
12248 if (what)
12250 if (msg)
12251 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
12252 what, msg);
12253 else
12254 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
12256 else
12258 if (msg)
12259 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
12260 msg);
12261 else
12262 warning (OPT_Wdeprecated_declarations, "type is deprecated");
12268 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12269 somewhere in it. */
12271 bool
12272 contains_bitfld_component_ref_p (const_tree ref)
12274 while (handled_component_p (ref))
12276 if (TREE_CODE (ref) == COMPONENT_REF
12277 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12278 return true;
12279 ref = TREE_OPERAND (ref, 0);
12282 return false;
12285 /* Try to determine whether a TRY_CATCH expression can fall through.
12286 This is a subroutine of block_may_fallthru. */
12288 static bool
12289 try_catch_may_fallthru (const_tree stmt)
12291 tree_stmt_iterator i;
12293 /* If the TRY block can fall through, the whole TRY_CATCH can
12294 fall through. */
12295 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
12296 return true;
12298 i = tsi_start (TREE_OPERAND (stmt, 1));
12299 switch (TREE_CODE (tsi_stmt (i)))
12301 case CATCH_EXPR:
12302 /* We expect to see a sequence of CATCH_EXPR trees, each with a
12303 catch expression and a body. The whole TRY_CATCH may fall
12304 through iff any of the catch bodies falls through. */
12305 for (; !tsi_end_p (i); tsi_next (&i))
12307 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
12308 return true;
12310 return false;
12312 case EH_FILTER_EXPR:
12313 /* The exception filter expression only matters if there is an
12314 exception. If the exception does not match EH_FILTER_TYPES,
12315 we will execute EH_FILTER_FAILURE, and we will fall through
12316 if that falls through. If the exception does match
12317 EH_FILTER_TYPES, the stack unwinder will continue up the
12318 stack, so we will not fall through. We don't know whether we
12319 will throw an exception which matches EH_FILTER_TYPES or not,
12320 so we just ignore EH_FILTER_TYPES and assume that we might
12321 throw an exception which doesn't match. */
12322 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
12324 default:
12325 /* This case represents statements to be executed when an
12326 exception occurs. Those statements are implicitly followed
12327 by a RESX statement to resume execution after the exception.
12328 So in this case the TRY_CATCH never falls through. */
12329 return false;
12333 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
12334 need not be 100% accurate; simply be conservative and return true if we
12335 don't know. This is used only to avoid stupidly generating extra code.
12336 If we're wrong, we'll just delete the extra code later. */
12338 bool
12339 block_may_fallthru (const_tree block)
12341 /* This CONST_CAST is okay because expr_last returns its argument
12342 unmodified and we assign it to a const_tree. */
12343 const_tree stmt = expr_last (CONST_CAST_TREE (block));
12345 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
12347 case GOTO_EXPR:
12348 case RETURN_EXPR:
12349 /* Easy cases. If the last statement of the block implies
12350 control transfer, then we can't fall through. */
12351 return false;
12353 case SWITCH_EXPR:
12354 /* If SWITCH_LABELS is set, this is lowered, and represents a
12355 branch to a selected label and hence can not fall through.
12356 Otherwise SWITCH_BODY is set, and the switch can fall
12357 through. */
12358 return SWITCH_LABELS (stmt) == NULL_TREE;
12360 case COND_EXPR:
12361 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
12362 return true;
12363 return block_may_fallthru (COND_EXPR_ELSE (stmt));
12365 case BIND_EXPR:
12366 return block_may_fallthru (BIND_EXPR_BODY (stmt));
12368 case TRY_CATCH_EXPR:
12369 return try_catch_may_fallthru (stmt);
12371 case TRY_FINALLY_EXPR:
12372 /* The finally clause is always executed after the try clause,
12373 so if it does not fall through, then the try-finally will not
12374 fall through. Otherwise, if the try clause does not fall
12375 through, then when the finally clause falls through it will
12376 resume execution wherever the try clause was going. So the
12377 whole try-finally will only fall through if both the try
12378 clause and the finally clause fall through. */
12379 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
12380 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
12382 case MODIFY_EXPR:
12383 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
12384 stmt = TREE_OPERAND (stmt, 1);
12385 else
12386 return true;
12387 /* FALLTHRU */
12389 case CALL_EXPR:
12390 /* Functions that do not return do not fall through. */
12391 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
12393 case CLEANUP_POINT_EXPR:
12394 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12396 case TARGET_EXPR:
12397 return block_may_fallthru (TREE_OPERAND (stmt, 1));
12399 case ERROR_MARK:
12400 return true;
12402 default:
12403 return lang_hooks.block_may_fallthru (stmt);
12407 /* True if we are using EH to handle cleanups. */
12408 static bool using_eh_for_cleanups_flag = false;
12410 /* This routine is called from front ends to indicate eh should be used for
12411 cleanups. */
12412 void
12413 using_eh_for_cleanups (void)
12415 using_eh_for_cleanups_flag = true;
12418 /* Query whether EH is used for cleanups. */
12419 bool
12420 using_eh_for_cleanups_p (void)
12422 return using_eh_for_cleanups_flag;
12425 /* Wrapper for tree_code_name to ensure that tree code is valid */
12426 const char *
12427 get_tree_code_name (enum tree_code code)
12429 const char *invalid = "<invalid tree code>";
12431 if (code >= MAX_TREE_CODES)
12432 return invalid;
12434 return tree_code_name[code];
12437 /* Drops the TREE_OVERFLOW flag from T. */
12439 tree
12440 drop_tree_overflow (tree t)
12442 gcc_checking_assert (TREE_OVERFLOW (t));
12444 /* For tree codes with a sharing machinery re-build the result. */
12445 if (TREE_CODE (t) == INTEGER_CST)
12446 return wide_int_to_tree (TREE_TYPE (t), t);
12448 /* Otherwise, as all tcc_constants are possibly shared, copy the node
12449 and drop the flag. */
12450 t = copy_node (t);
12451 TREE_OVERFLOW (t) = 0;
12452 return t;
12455 /* Given a memory reference expression T, return its base address.
12456 The base address of a memory reference expression is the main
12457 object being referenced. For instance, the base address for
12458 'array[i].fld[j]' is 'array'. You can think of this as stripping
12459 away the offset part from a memory address.
12461 This function calls handled_component_p to strip away all the inner
12462 parts of the memory reference until it reaches the base object. */
12464 tree
12465 get_base_address (tree t)
12467 while (handled_component_p (t))
12468 t = TREE_OPERAND (t, 0);
12470 if ((TREE_CODE (t) == MEM_REF
12471 || TREE_CODE (t) == TARGET_MEM_REF)
12472 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
12473 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
12475 /* ??? Either the alias oracle or all callers need to properly deal
12476 with WITH_SIZE_EXPRs before we can look through those. */
12477 if (TREE_CODE (t) == WITH_SIZE_EXPR)
12478 return NULL_TREE;
12480 return t;
12483 /* Return a tree of sizetype representing the size, in bytes, of the element
12484 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12486 tree
12487 array_ref_element_size (tree exp)
12489 tree aligned_size = TREE_OPERAND (exp, 3);
12490 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
12491 location_t loc = EXPR_LOCATION (exp);
12493 /* If a size was specified in the ARRAY_REF, it's the size measured
12494 in alignment units of the element type. So multiply by that value. */
12495 if (aligned_size)
12497 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
12498 sizetype from another type of the same width and signedness. */
12499 if (TREE_TYPE (aligned_size) != sizetype)
12500 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
12501 return size_binop_loc (loc, MULT_EXPR, aligned_size,
12502 size_int (TYPE_ALIGN_UNIT (elmt_type)));
12505 /* Otherwise, take the size from that of the element type. Substitute
12506 any PLACEHOLDER_EXPR that we have. */
12507 else
12508 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
12511 /* Return a tree representing the lower bound of the array mentioned in
12512 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12514 tree
12515 array_ref_low_bound (tree exp)
12517 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12519 /* If a lower bound is specified in EXP, use it. */
12520 if (TREE_OPERAND (exp, 2))
12521 return TREE_OPERAND (exp, 2);
12523 /* Otherwise, if there is a domain type and it has a lower bound, use it,
12524 substituting for a PLACEHOLDER_EXPR as needed. */
12525 if (domain_type && TYPE_MIN_VALUE (domain_type))
12526 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
12528 /* Otherwise, return a zero of the appropriate type. */
12529 return build_int_cst (TREE_TYPE (TREE_OPERAND (exp, 1)), 0);
12532 /* Return a tree representing the upper bound of the array mentioned in
12533 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12535 tree
12536 array_ref_up_bound (tree exp)
12538 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12540 /* If there is a domain type and it has an upper bound, use it, substituting
12541 for a PLACEHOLDER_EXPR as needed. */
12542 if (domain_type && TYPE_MAX_VALUE (domain_type))
12543 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
12545 /* Otherwise fail. */
12546 return NULL_TREE;
12549 /* Returns true if REF is an array reference to an array at the end of
12550 a structure. If this is the case, the array may be allocated larger
12551 than its upper bound implies. */
12553 bool
12554 array_at_struct_end_p (tree ref)
12556 if (TREE_CODE (ref) != ARRAY_REF
12557 && TREE_CODE (ref) != ARRAY_RANGE_REF)
12558 return false;
12560 while (handled_component_p (ref))
12562 /* If the reference chain contains a component reference to a
12563 non-union type and there follows another field the reference
12564 is not at the end of a structure. */
12565 if (TREE_CODE (ref) == COMPONENT_REF
12566 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
12568 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
12569 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
12570 nextf = DECL_CHAIN (nextf);
12571 if (nextf)
12572 return false;
12575 ref = TREE_OPERAND (ref, 0);
12578 /* If the reference is based on a declared entity, the size of the array
12579 is constrained by its given domain. */
12580 if (DECL_P (ref))
12581 return false;
12583 return true;
12586 /* Return a tree representing the offset, in bytes, of the field referenced
12587 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
12589 tree
12590 component_ref_field_offset (tree exp)
12592 tree aligned_offset = TREE_OPERAND (exp, 2);
12593 tree field = TREE_OPERAND (exp, 1);
12594 location_t loc = EXPR_LOCATION (exp);
12596 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
12597 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
12598 value. */
12599 if (aligned_offset)
12601 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
12602 sizetype from another type of the same width and signedness. */
12603 if (TREE_TYPE (aligned_offset) != sizetype)
12604 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
12605 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
12606 size_int (DECL_OFFSET_ALIGN (field)
12607 / BITS_PER_UNIT));
12610 /* Otherwise, take the offset from that of the field. Substitute
12611 any PLACEHOLDER_EXPR that we have. */
12612 else
12613 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
12616 /* Return the machine mode of T. For vectors, returns the mode of the
12617 inner type. The main use case is to feed the result to HONOR_NANS,
12618 avoiding the BLKmode that a direct TYPE_MODE (T) might return. */
12620 machine_mode
12621 element_mode (const_tree t)
12623 if (!TYPE_P (t))
12624 t = TREE_TYPE (t);
12625 if (VECTOR_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
12626 t = TREE_TYPE (t);
12627 return TYPE_MODE (t);
12631 /* Veirfy that basic properties of T match TV and thus T can be a variant of
12632 TV. TV should be the more specified variant (i.e. the main variant). */
12634 static bool
12635 verify_type_variant (const_tree t, tree tv)
12637 /* Type variant can differ by:
12639 - TYPE_QUALS: TYPE_READONLY, TYPE_VOLATILE, TYPE_ATOMIC, TYPE_RESTRICT,
12640 ENCODE_QUAL_ADDR_SPACE.
12641 - main variant may be TYPE_COMPLETE_P and variant types !TYPE_COMPLETE_P
12642 in this case some values may not be set in the variant types
12643 (see TYPE_COMPLETE_P checks).
12644 - it is possible to have TYPE_ARTIFICIAL variant of non-artifical type
12645 - by TYPE_NAME and attributes (i.e. when variant originate by typedef)
12646 - TYPE_CANONICAL (TYPE_ALIAS_SET is the same among variants)
12647 - by the alignment: TYPE_ALIGN and TYPE_USER_ALIGN
12648 - during LTO by TYPE_CONTEXT if type is TYPE_FILE_SCOPE_P
12649 this is necessary to make it possible to merge types form different TUs
12650 - arrays, pointers and references may have TREE_TYPE that is a variant
12651 of TREE_TYPE of their main variants.
12652 - aggregates may have new TYPE_FIELDS list that list variants of
12653 the main variant TYPE_FIELDS.
12654 - vector types may differ by TYPE_VECTOR_OPAQUE
12655 - TYPE_METHODS is always NULL for vairant types and maintained for
12656 main variant only.
12659 /* Convenience macro for matching individual fields. */
12660 #define verify_variant_match(flag) \
12661 do { \
12662 if (flag (tv) != flag (t)) \
12664 error ("type variant differs by " #flag "."); \
12665 debug_tree (tv); \
12666 return false; \
12668 } while (false)
12670 /* tree_base checks. */
12672 verify_variant_match (TREE_CODE);
12673 /* FIXME: Ada builds non-artificial variants of artificial types. */
12674 if (TYPE_ARTIFICIAL (tv) && 0)
12675 verify_variant_match (TYPE_ARTIFICIAL);
12676 if (POINTER_TYPE_P (tv))
12677 verify_variant_match (TYPE_REF_CAN_ALIAS_ALL);
12678 /* FIXME: TYPE_SIZES_GIMPLIFIED may differs for Ada build. */
12679 verify_variant_match (TYPE_UNSIGNED);
12680 verify_variant_match (TYPE_ALIGN_OK);
12681 verify_variant_match (TYPE_PACKED);
12682 if (TREE_CODE (t) == REFERENCE_TYPE)
12683 verify_variant_match (TYPE_REF_IS_RVALUE);
12684 verify_variant_match (TYPE_SATURATING);
12685 /* FIXME: This check trigger during libstdc++ build. */
12686 if (RECORD_OR_UNION_TYPE_P (t) && COMPLETE_TYPE_P (t) && 0)
12687 verify_variant_match (TYPE_FINAL_P);
12689 /* tree_type_common checks. */
12691 if (COMPLETE_TYPE_P (t))
12693 verify_variant_match (TYPE_SIZE);
12694 verify_variant_match (TYPE_MODE);
12695 if (TYPE_SIZE_UNIT (t) != TYPE_SIZE_UNIT (tv)
12696 /* FIXME: ideally we should compare pointer equality, but java FE
12697 produce variants where size is INTEGER_CST of different type (int
12698 wrt size_type) during libjava biuld. */
12699 && !operand_equal_p (TYPE_SIZE_UNIT (t), TYPE_SIZE_UNIT (tv), 0))
12701 error ("type variant has different TYPE_SIZE_UNIT");
12702 debug_tree (tv);
12703 error ("type variant's TYPE_SIZE_UNIT");
12704 debug_tree (TYPE_SIZE_UNIT (tv));
12705 error ("type's TYPE_SIZE_UNIT");
12706 debug_tree (TYPE_SIZE_UNIT (t));
12707 return false;
12710 verify_variant_match (TYPE_PRECISION);
12711 verify_variant_match (TYPE_NEEDS_CONSTRUCTING);
12712 if (RECORD_OR_UNION_TYPE_P (t))
12713 verify_variant_match (TYPE_TRANSPARENT_AGGR);
12714 else if (TREE_CODE (t) == ARRAY_TYPE)
12715 verify_variant_match (TYPE_NONALIASED_COMPONENT);
12716 /* During LTO we merge variant lists from diferent translation units
12717 that may differ BY TYPE_CONTEXT that in turn may point
12718 to TRANSLATION_UNIT_DECL.
12719 Ada also builds variants of types with different TYPE_CONTEXT. */
12720 if ((!in_lto_p || !TYPE_FILE_SCOPE_P (t)) && 0)
12721 verify_variant_match (TYPE_CONTEXT);
12722 verify_variant_match (TYPE_STRING_FLAG);
12723 if (TYPE_ALIAS_SET_KNOWN_P (t) && TYPE_ALIAS_SET_KNOWN_P (tv))
12724 verify_variant_match (TYPE_ALIAS_SET);
12726 /* tree_type_non_common checks. */
12728 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
12729 and dangle the pointer from time to time. */
12730 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_VFIELD (t) != TYPE_VFIELD (tv)
12731 && (in_lto_p || !TYPE_VFIELD (tv)
12732 || TREE_CODE (TYPE_VFIELD (tv)) != TREE_LIST))
12734 error ("type variant has different TYPE_VFIELD");
12735 debug_tree (tv);
12736 return false;
12738 if ((TREE_CODE (t) == ENUMERAL_TYPE && COMPLETE_TYPE_P (t))
12739 || TREE_CODE (t) == INTEGER_TYPE
12740 || TREE_CODE (t) == BOOLEAN_TYPE
12741 || TREE_CODE (t) == REAL_TYPE
12742 || TREE_CODE (t) == FIXED_POINT_TYPE)
12744 verify_variant_match (TYPE_MAX_VALUE);
12745 verify_variant_match (TYPE_MIN_VALUE);
12747 if (TREE_CODE (t) == METHOD_TYPE)
12748 verify_variant_match (TYPE_METHOD_BASETYPE);
12749 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_METHODS (t))
12751 error ("type variant has TYPE_METHODS");
12752 debug_tree (tv);
12753 return false;
12755 if (TREE_CODE (t) == OFFSET_TYPE)
12756 verify_variant_match (TYPE_OFFSET_BASETYPE);
12757 if (TREE_CODE (t) == ARRAY_TYPE)
12758 verify_variant_match (TYPE_ARRAY_MAX_SIZE);
12759 /* FIXME: Be lax and allow TYPE_BINFO to be missing in variant types
12760 or even type's main variant. This is needed to make bootstrap pass
12761 and the bug seems new in GCC 5.
12762 C++ FE should be updated to make this consistent and we should check
12763 that TYPE_BINFO is always NULL for !COMPLETE_TYPE_P and otherwise there
12764 is a match with main variant.
12766 Also disable the check for Java for now because of parser hack that builds
12767 first an dummy BINFO and then sometimes replace it by real BINFO in some
12768 of the copies. */
12769 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t) && TYPE_BINFO (tv)
12770 && TYPE_BINFO (t) != TYPE_BINFO (tv)
12771 /* FIXME: Java sometimes keep dump TYPE_BINFOs on variant types.
12772 Since there is no cheap way to tell C++/Java type w/o LTO, do checking
12773 at LTO time only. */
12774 && (in_lto_p && odr_type_p (t)))
12776 error ("type variant has different TYPE_BINFO");
12777 debug_tree (tv);
12778 error ("type variant's TYPE_BINFO");
12779 debug_tree (TYPE_BINFO (tv));
12780 error ("type's TYPE_BINFO");
12781 debug_tree (TYPE_BINFO (t));
12782 return false;
12785 /* Check various uses of TYPE_VALUES_RAW. */
12786 if (TREE_CODE (t) == ENUMERAL_TYPE)
12787 verify_variant_match (TYPE_VALUES);
12788 else if (TREE_CODE (t) == ARRAY_TYPE)
12789 verify_variant_match (TYPE_DOMAIN);
12790 /* Permit incomplete variants of complete type. While FEs may complete
12791 all variants, this does not happen for C++ templates in all cases. */
12792 else if (RECORD_OR_UNION_TYPE_P (t)
12793 && COMPLETE_TYPE_P (t)
12794 && TYPE_FIELDS (t) != TYPE_FIELDS (tv))
12796 tree f1, f2;
12798 /* Fortran builds qualified variants as new records with items of
12799 qualified type. Verify that they looks same. */
12800 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (tv);
12801 f1 && f2;
12802 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
12803 if (TREE_CODE (f1) != FIELD_DECL || TREE_CODE (f2) != FIELD_DECL
12804 || (TYPE_MAIN_VARIANT (TREE_TYPE (f1))
12805 != TYPE_MAIN_VARIANT (TREE_TYPE (f2))
12806 /* FIXME: gfc_nonrestricted_type builds all types as variants
12807 with exception of pointer types. It deeply copies the type
12808 which means that we may end up with a variant type
12809 referring non-variant pointer. We may change it to
12810 produce types as variants, too, like
12811 objc_get_protocol_qualified_type does. */
12812 && !POINTER_TYPE_P (TREE_TYPE (f1)))
12813 || DECL_FIELD_OFFSET (f1) != DECL_FIELD_OFFSET (f2)
12814 || DECL_FIELD_BIT_OFFSET (f1) != DECL_FIELD_BIT_OFFSET (f2))
12815 break;
12816 if (f1 || f2)
12818 error ("type variant has different TYPE_FIELDS");
12819 debug_tree (tv);
12820 error ("first mismatch is field");
12821 debug_tree (f1);
12822 error ("and field");
12823 debug_tree (f2);
12824 return false;
12827 else if ((TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE))
12828 verify_variant_match (TYPE_ARG_TYPES);
12829 /* For C++ the qualified variant of array type is really an array type
12830 of qualified TREE_TYPE.
12831 objc builds variants of pointer where pointer to type is a variant, too
12832 in objc_get_protocol_qualified_type. */
12833 if (TREE_TYPE (t) != TREE_TYPE (tv)
12834 && ((TREE_CODE (t) != ARRAY_TYPE
12835 && !POINTER_TYPE_P (t))
12836 || TYPE_MAIN_VARIANT (TREE_TYPE (t))
12837 != TYPE_MAIN_VARIANT (TREE_TYPE (tv))))
12839 error ("type variant has different TREE_TYPE");
12840 debug_tree (tv);
12841 error ("type variant's TREE_TYPE");
12842 debug_tree (TREE_TYPE (tv));
12843 error ("type's TREE_TYPE");
12844 debug_tree (TREE_TYPE (t));
12845 return false;
12847 if (type_with_alias_set_p (t)
12848 && !gimple_canonical_types_compatible_p (t, tv, false))
12850 error ("type is not compatible with its vairant");
12851 debug_tree (tv);
12852 error ("type variant's TREE_TYPE");
12853 debug_tree (TREE_TYPE (tv));
12854 error ("type's TREE_TYPE");
12855 debug_tree (TREE_TYPE (t));
12856 return false;
12858 return true;
12859 #undef verify_variant_match
12863 /* The TYPE_CANONICAL merging machinery. It should closely resemble
12864 the middle-end types_compatible_p function. It needs to avoid
12865 claiming types are different for types that should be treated
12866 the same with respect to TBAA. Canonical types are also used
12867 for IL consistency checks via the useless_type_conversion_p
12868 predicate which does not handle all type kinds itself but falls
12869 back to pointer-comparison of TYPE_CANONICAL for aggregates
12870 for example. */
12872 /* Return true iff T1 and T2 are structurally identical for what
12873 TBAA is concerned.
12874 This function is used both by lto.c canonical type merging and by the
12875 verifier. If TRUST_TYPE_CANONICAL we do not look into structure of types
12876 that have TYPE_CANONICAL defined and assume them equivalent. */
12878 bool
12879 gimple_canonical_types_compatible_p (const_tree t1, const_tree t2,
12880 bool trust_type_canonical)
12882 /* Type variants should be same as the main variant. When not doing sanity
12883 checking to verify this fact, go to main variants and save some work. */
12884 if (trust_type_canonical)
12886 t1 = TYPE_MAIN_VARIANT (t1);
12887 t2 = TYPE_MAIN_VARIANT (t2);
12890 /* Check first for the obvious case of pointer identity. */
12891 if (t1 == t2)
12892 return true;
12894 /* Check that we have two types to compare. */
12895 if (t1 == NULL_TREE || t2 == NULL_TREE)
12896 return false;
12898 /* We consider complete types always compatible with incomplete type.
12899 This does not make sense for canonical type calculation and thus we
12900 need to ensure that we are never called on it.
12902 FIXME: For more correctness the function probably should have three modes
12903 1) mode assuming that types are complete mathcing their structure
12904 2) mode allowing incomplete types but producing equivalence classes
12905 and thus ignoring all info from complete types
12906 3) mode allowing incomplete types to match complete but checking
12907 compatibility between complete types.
12909 1 and 2 can be used for canonical type calculation. 3 is the real
12910 definition of type compatibility that can be used i.e. for warnings during
12911 declaration merging. */
12913 gcc_assert (!trust_type_canonical
12914 || (type_with_alias_set_p (t1) && type_with_alias_set_p (t2)));
12915 /* If the types have been previously registered and found equal
12916 they still are. */
12917 if (TYPE_CANONICAL (t1) && TYPE_CANONICAL (t2)
12918 && trust_type_canonical)
12919 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
12921 /* Can't be the same type if the types don't have the same code. */
12922 if (tree_code_for_canonical_type_merging (TREE_CODE (t1))
12923 != tree_code_for_canonical_type_merging (TREE_CODE (t2)))
12924 return false;
12926 /* Qualifiers do not matter for canonical type comparison purposes. */
12928 /* Void types and nullptr types are always the same. */
12929 if (TREE_CODE (t1) == VOID_TYPE
12930 || TREE_CODE (t1) == NULLPTR_TYPE)
12931 return true;
12933 /* Can't be the same type if they have different mode. */
12934 if (TYPE_MODE (t1) != TYPE_MODE (t2))
12935 return false;
12937 /* Non-aggregate types can be handled cheaply. */
12938 if (INTEGRAL_TYPE_P (t1)
12939 || SCALAR_FLOAT_TYPE_P (t1)
12940 || FIXED_POINT_TYPE_P (t1)
12941 || TREE_CODE (t1) == VECTOR_TYPE
12942 || TREE_CODE (t1) == COMPLEX_TYPE
12943 || TREE_CODE (t1) == OFFSET_TYPE
12944 || POINTER_TYPE_P (t1))
12946 /* Can't be the same type if they have different sign or precision. */
12947 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
12948 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
12949 return false;
12951 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
12952 interoperable with "signed char". Unless all frontends are revisited
12953 to agree on these types, we must ignore the flag completely. */
12955 /* Fortran standard define C_PTR type that is compatible with every
12956 C pointer. For this reason we need to glob all pointers into one.
12957 Still pointers in different address spaces are not compatible. */
12958 if (POINTER_TYPE_P (t1))
12960 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
12961 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
12962 return false;
12965 /* Tail-recurse to components. */
12966 if (TREE_CODE (t1) == VECTOR_TYPE
12967 || TREE_CODE (t1) == COMPLEX_TYPE)
12968 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
12969 TREE_TYPE (t2),
12970 trust_type_canonical);
12972 return true;
12975 /* Do type-specific comparisons. */
12976 switch (TREE_CODE (t1))
12978 case ARRAY_TYPE:
12979 /* Array types are the same if the element types are the same and
12980 the number of elements are the same. */
12981 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
12982 trust_type_canonical)
12983 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
12984 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
12985 return false;
12986 else
12988 tree i1 = TYPE_DOMAIN (t1);
12989 tree i2 = TYPE_DOMAIN (t2);
12991 /* For an incomplete external array, the type domain can be
12992 NULL_TREE. Check this condition also. */
12993 if (i1 == NULL_TREE && i2 == NULL_TREE)
12994 return true;
12995 else if (i1 == NULL_TREE || i2 == NULL_TREE)
12996 return false;
12997 else
12999 tree min1 = TYPE_MIN_VALUE (i1);
13000 tree min2 = TYPE_MIN_VALUE (i2);
13001 tree max1 = TYPE_MAX_VALUE (i1);
13002 tree max2 = TYPE_MAX_VALUE (i2);
13004 /* The minimum/maximum values have to be the same. */
13005 if ((min1 == min2
13006 || (min1 && min2
13007 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
13008 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
13009 || operand_equal_p (min1, min2, 0))))
13010 && (max1 == max2
13011 || (max1 && max2
13012 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
13013 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
13014 || operand_equal_p (max1, max2, 0)))))
13015 return true;
13016 else
13017 return false;
13021 case METHOD_TYPE:
13022 case FUNCTION_TYPE:
13023 /* Function types are the same if the return type and arguments types
13024 are the same. */
13025 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13026 trust_type_canonical))
13027 return false;
13029 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
13030 return true;
13031 else
13033 tree parms1, parms2;
13035 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
13036 parms1 && parms2;
13037 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
13039 if (!gimple_canonical_types_compatible_p
13040 (TREE_VALUE (parms1), TREE_VALUE (parms2),
13041 trust_type_canonical))
13042 return false;
13045 if (parms1 || parms2)
13046 return false;
13048 return true;
13051 case RECORD_TYPE:
13052 case UNION_TYPE:
13053 case QUAL_UNION_TYPE:
13055 tree f1, f2;
13057 /* For aggregate types, all the fields must be the same. */
13058 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
13059 f1 || f2;
13060 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
13062 /* Skip non-fields. */
13063 while (f1 && TREE_CODE (f1) != FIELD_DECL)
13064 f1 = TREE_CHAIN (f1);
13065 while (f2 && TREE_CODE (f2) != FIELD_DECL)
13066 f2 = TREE_CHAIN (f2);
13067 if (!f1 || !f2)
13068 break;
13069 /* The fields must have the same name, offset and type. */
13070 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
13071 || !gimple_compare_field_offset (f1, f2)
13072 || !gimple_canonical_types_compatible_p
13073 (TREE_TYPE (f1), TREE_TYPE (f2),
13074 trust_type_canonical))
13075 return false;
13078 /* If one aggregate has more fields than the other, they
13079 are not the same. */
13080 if (f1 || f2)
13081 return false;
13083 return true;
13086 default:
13087 /* Consider all types with language specific trees in them mutually
13088 compatible. This is executed only from verify_type and false
13089 positives can be tolerated. */
13090 gcc_assert (!in_lto_p);
13091 return true;
13095 /* Verify type T. */
13097 void
13098 verify_type (const_tree t)
13100 bool error_found = false;
13101 tree mv = TYPE_MAIN_VARIANT (t);
13102 if (!mv)
13104 error ("Main variant is not defined");
13105 error_found = true;
13107 else if (mv != TYPE_MAIN_VARIANT (mv))
13109 error ("TYPE_MAIN_VARIANT has different TYPE_MAIN_VARIANT");
13110 debug_tree (mv);
13111 error_found = true;
13113 else if (t != mv && !verify_type_variant (t, mv))
13114 error_found = true;
13116 tree ct = TYPE_CANONICAL (t);
13117 if (!ct)
13119 else if (TYPE_CANONICAL (t) != ct)
13121 error ("TYPE_CANONICAL has different TYPE_CANONICAL");
13122 debug_tree (ct);
13123 error_found = true;
13125 /* Method and function types can not be used to address memory and thus
13126 TYPE_CANONICAL really matters only for determining useless conversions.
13128 FIXME: C++ FE produce declarations of builtin functions that are not
13129 compatible with main variants. */
13130 else if (TREE_CODE (t) == FUNCTION_TYPE)
13132 else if (t != ct
13133 /* FIXME: gimple_canonical_types_compatible_p can not compare types
13134 with variably sized arrays because their sizes possibly
13135 gimplified to different variables. */
13136 && !variably_modified_type_p (ct, NULL)
13137 && !gimple_canonical_types_compatible_p (t, ct, false))
13139 error ("TYPE_CANONICAL is not compatible");
13140 debug_tree (ct);
13141 error_found = true;
13145 /* Check various uses of TYPE_MINVAL. */
13146 if (RECORD_OR_UNION_TYPE_P (t))
13148 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
13149 and danagle the pointer from time to time. */
13150 if (TYPE_VFIELD (t)
13151 && TREE_CODE (TYPE_VFIELD (t)) != FIELD_DECL
13152 && TREE_CODE (TYPE_VFIELD (t)) != TREE_LIST)
13154 error ("TYPE_VFIELD is not FIELD_DECL nor TREE_LIST");
13155 debug_tree (TYPE_VFIELD (t));
13156 error_found = true;
13159 else if (TREE_CODE (t) == POINTER_TYPE)
13161 if (TYPE_NEXT_PTR_TO (t)
13162 && TREE_CODE (TYPE_NEXT_PTR_TO (t)) != POINTER_TYPE)
13164 error ("TYPE_NEXT_PTR_TO is not POINTER_TYPE");
13165 debug_tree (TYPE_NEXT_PTR_TO (t));
13166 error_found = true;
13169 else if (TREE_CODE (t) == REFERENCE_TYPE)
13171 if (TYPE_NEXT_REF_TO (t)
13172 && TREE_CODE (TYPE_NEXT_REF_TO (t)) != REFERENCE_TYPE)
13174 error ("TYPE_NEXT_REF_TO is not REFERENCE_TYPE");
13175 debug_tree (TYPE_NEXT_REF_TO (t));
13176 error_found = true;
13179 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
13180 || TREE_CODE (t) == FIXED_POINT_TYPE)
13182 /* FIXME: The following check should pass:
13183 useless_type_conversion_p (const_cast <tree> (t),
13184 TREE_TYPE (TYPE_MIN_VALUE (t))
13185 but does not for C sizetypes in LTO. */
13187 /* Java uses TYPE_MINVAL for TYPE_ARGUMENT_SIGNATURE. */
13188 else if (TYPE_MINVAL (t)
13189 && ((TREE_CODE (t) != METHOD_TYPE && TREE_CODE (t) != FUNCTION_TYPE)
13190 || in_lto_p))
13192 error ("TYPE_MINVAL non-NULL");
13193 debug_tree (TYPE_MINVAL (t));
13194 error_found = true;
13197 /* Check various uses of TYPE_MAXVAL. */
13198 if (RECORD_OR_UNION_TYPE_P (t))
13200 if (TYPE_METHODS (t) && TREE_CODE (TYPE_METHODS (t)) != FUNCTION_DECL
13201 && TREE_CODE (TYPE_METHODS (t)) != TEMPLATE_DECL
13202 && TYPE_METHODS (t) != error_mark_node)
13204 error ("TYPE_METHODS is not FUNCTION_DECL, TEMPLATE_DECL nor error_mark_node");
13205 debug_tree (TYPE_METHODS (t));
13206 error_found = true;
13209 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
13211 if (TYPE_METHOD_BASETYPE (t)
13212 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != RECORD_TYPE
13213 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != UNION_TYPE)
13215 error ("TYPE_METHOD_BASETYPE is not record nor union");
13216 debug_tree (TYPE_METHOD_BASETYPE (t));
13217 error_found = true;
13220 else if (TREE_CODE (t) == OFFSET_TYPE)
13222 if (TYPE_OFFSET_BASETYPE (t)
13223 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != RECORD_TYPE
13224 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != UNION_TYPE)
13226 error ("TYPE_OFFSET_BASETYPE is not record nor union");
13227 debug_tree (TYPE_OFFSET_BASETYPE (t));
13228 error_found = true;
13231 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
13232 || TREE_CODE (t) == FIXED_POINT_TYPE)
13234 /* FIXME: The following check should pass:
13235 useless_type_conversion_p (const_cast <tree> (t),
13236 TREE_TYPE (TYPE_MAX_VALUE (t))
13237 but does not for C sizetypes in LTO. */
13239 else if (TREE_CODE (t) == ARRAY_TYPE)
13241 if (TYPE_ARRAY_MAX_SIZE (t)
13242 && TREE_CODE (TYPE_ARRAY_MAX_SIZE (t)) != INTEGER_CST)
13244 error ("TYPE_ARRAY_MAX_SIZE not INTEGER_CST");
13245 debug_tree (TYPE_ARRAY_MAX_SIZE (t));
13246 error_found = true;
13249 else if (TYPE_MAXVAL (t))
13251 error ("TYPE_MAXVAL non-NULL");
13252 debug_tree (TYPE_MAXVAL (t));
13253 error_found = true;
13256 /* Check various uses of TYPE_BINFO. */
13257 if (RECORD_OR_UNION_TYPE_P (t))
13259 if (!TYPE_BINFO (t))
13261 else if (TREE_CODE (TYPE_BINFO (t)) != TREE_BINFO)
13263 error ("TYPE_BINFO is not TREE_BINFO");
13264 debug_tree (TYPE_BINFO (t));
13265 error_found = true;
13267 /* FIXME: Java builds invalid empty binfos that do not have
13268 TREE_TYPE set. */
13269 else if (TREE_TYPE (TYPE_BINFO (t)) != TYPE_MAIN_VARIANT (t) && 0)
13271 error ("TYPE_BINFO type is not TYPE_MAIN_VARIANT");
13272 debug_tree (TREE_TYPE (TYPE_BINFO (t)));
13273 error_found = true;
13276 else if (TYPE_LANG_SLOT_1 (t) && in_lto_p)
13278 error ("TYPE_LANG_SLOT_1 (binfo) field is non-NULL");
13279 debug_tree (TYPE_LANG_SLOT_1 (t));
13280 error_found = true;
13283 /* Check various uses of TYPE_VALUES_RAW. */
13284 if (TREE_CODE (t) == ENUMERAL_TYPE)
13285 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
13287 tree value = TREE_VALUE (l);
13288 tree name = TREE_PURPOSE (l);
13290 /* C FE porduce INTEGER_CST of INTEGER_TYPE, while C++ FE uses
13291 CONST_DECL of ENUMERAL TYPE. */
13292 if (TREE_CODE (value) != INTEGER_CST && TREE_CODE (value) != CONST_DECL)
13294 error ("Enum value is not CONST_DECL or INTEGER_CST");
13295 debug_tree (value);
13296 debug_tree (name);
13297 error_found = true;
13299 if (TREE_CODE (TREE_TYPE (value)) != INTEGER_TYPE
13300 && !useless_type_conversion_p (const_cast <tree> (t), TREE_TYPE (value)))
13302 error ("Enum value type is not INTEGER_TYPE nor convertible to the enum");
13303 debug_tree (value);
13304 debug_tree (name);
13305 error_found = true;
13307 if (TREE_CODE (name) != IDENTIFIER_NODE)
13309 error ("Enum value name is not IDENTIFIER_NODE");
13310 debug_tree (value);
13311 debug_tree (name);
13312 error_found = true;
13315 else if (TREE_CODE (t) == ARRAY_TYPE)
13317 if (TYPE_DOMAIN (t) && TREE_CODE (TYPE_DOMAIN (t)) != INTEGER_TYPE)
13319 error ("Array TYPE_DOMAIN is not integer type");
13320 debug_tree (TYPE_DOMAIN (t));
13321 error_found = true;
13324 else if (RECORD_OR_UNION_TYPE_P (t))
13325 for (tree fld = TYPE_FIELDS (t); fld; fld = TREE_CHAIN (fld))
13327 /* TODO: verify properties of decls. */
13328 if (TREE_CODE (fld) == FIELD_DECL)
13330 else if (TREE_CODE (fld) == TYPE_DECL)
13332 else if (TREE_CODE (fld) == CONST_DECL)
13334 else if (TREE_CODE (fld) == VAR_DECL)
13336 else if (TREE_CODE (fld) == TEMPLATE_DECL)
13338 else if (TREE_CODE (fld) == USING_DECL)
13340 else
13342 error ("Wrong tree in TYPE_FIELDS list");
13343 debug_tree (fld);
13344 error_found = true;
13347 else if (TREE_CODE (t) == INTEGER_TYPE
13348 || TREE_CODE (t) == BOOLEAN_TYPE
13349 || TREE_CODE (t) == OFFSET_TYPE
13350 || TREE_CODE (t) == REFERENCE_TYPE
13351 || TREE_CODE (t) == NULLPTR_TYPE
13352 || TREE_CODE (t) == POINTER_TYPE)
13354 if (TYPE_CACHED_VALUES_P (t) != (TYPE_CACHED_VALUES (t) != NULL))
13356 error ("TYPE_CACHED_VALUES_P is %i while TYPE_CACHED_VALUES is %p",
13357 TYPE_CACHED_VALUES_P (t), (void *)TYPE_CACHED_VALUES (t));
13358 error_found = true;
13360 else if (TYPE_CACHED_VALUES_P (t) && TREE_CODE (TYPE_CACHED_VALUES (t)) != TREE_VEC)
13362 error ("TYPE_CACHED_VALUES is not TREE_VEC");
13363 debug_tree (TYPE_CACHED_VALUES (t));
13364 error_found = true;
13366 /* Verify just enough of cache to ensure that no one copied it to new type.
13367 All copying should go by copy_node that should clear it. */
13368 else if (TYPE_CACHED_VALUES_P (t))
13370 int i;
13371 for (i = 0; i < TREE_VEC_LENGTH (TYPE_CACHED_VALUES (t)); i++)
13372 if (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)
13373 && TREE_TYPE (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)) != t)
13375 error ("wrong TYPE_CACHED_VALUES entry");
13376 debug_tree (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i));
13377 error_found = true;
13378 break;
13382 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
13383 for (tree l = TYPE_ARG_TYPES (t); l; l = TREE_CHAIN (l))
13385 /* C++ FE uses TREE_PURPOSE to store initial values. */
13386 if (TREE_PURPOSE (l) && in_lto_p)
13388 error ("TREE_PURPOSE is non-NULL in TYPE_ARG_TYPES list");
13389 debug_tree (l);
13390 error_found = true;
13392 if (!TYPE_P (TREE_VALUE (l)))
13394 error ("Wrong entry in TYPE_ARG_TYPES list");
13395 debug_tree (l);
13396 error_found = true;
13399 else if (!is_lang_specific (t) && TYPE_VALUES_RAW (t))
13401 error ("TYPE_VALUES_RAW field is non-NULL");
13402 debug_tree (TYPE_VALUES_RAW (t));
13403 error_found = true;
13405 if (TREE_CODE (t) != INTEGER_TYPE
13406 && TREE_CODE (t) != BOOLEAN_TYPE
13407 && TREE_CODE (t) != OFFSET_TYPE
13408 && TREE_CODE (t) != REFERENCE_TYPE
13409 && TREE_CODE (t) != NULLPTR_TYPE
13410 && TREE_CODE (t) != POINTER_TYPE
13411 && TYPE_CACHED_VALUES_P (t))
13413 error ("TYPE_CACHED_VALUES_P is set while it should not");
13414 error_found = true;
13416 if (TYPE_STRING_FLAG (t)
13417 && TREE_CODE (t) != ARRAY_TYPE && TREE_CODE (t) != INTEGER_TYPE)
13419 error ("TYPE_STRING_FLAG is set on wrong type code");
13420 error_found = true;
13422 else if (TYPE_STRING_FLAG (t))
13424 const_tree b = t;
13425 if (TREE_CODE (b) == ARRAY_TYPE)
13426 b = TREE_TYPE (t);
13427 /* Java builds arrays with TYPE_STRING_FLAG of promoted_char_type
13428 that is 32bits. */
13429 if (TREE_CODE (b) != INTEGER_TYPE)
13431 error ("TYPE_STRING_FLAG is set on type that does not look like "
13432 "char nor array of chars");
13433 error_found = true;
13437 /* ipa-devirt makes an assumption that TYPE_METHOD_BASETYPE is always
13438 TYPE_MAIN_VARIANT and it would be odd to add methods only to variatns
13439 of a type. */
13440 if (TREE_CODE (t) == METHOD_TYPE
13441 && TYPE_MAIN_VARIANT (TYPE_METHOD_BASETYPE (t)) != TYPE_METHOD_BASETYPE (t))
13443 error ("TYPE_METHOD_BASETYPE is not main variant");
13444 error_found = true;
13447 if (error_found)
13449 debug_tree (const_cast <tree> (t));
13450 internal_error ("verify_type failed");
13454 #include "gt-tree.h"