testsuite: Update scanning symbol sections to support AIX.
[official-gcc.git] / gcc / tree.c
blob1ad4ad5a5f7403cbff656e4f596f7333e73e57bf
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2020 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 "backend.h"
34 #include "target.h"
35 #include "tree.h"
36 #include "gimple.h"
37 #include "tree-pass.h"
38 #include "ssa.h"
39 #include "cgraph.h"
40 #include "diagnostic.h"
41 #include "flags.h"
42 #include "alias.h"
43 #include "fold-const.h"
44 #include "stor-layout.h"
45 #include "calls.h"
46 #include "attribs.h"
47 #include "toplev.h" /* get_random_seed */
48 #include "output.h"
49 #include "common/common-target.h"
50 #include "langhooks.h"
51 #include "tree-inline.h"
52 #include "tree-iterator.h"
53 #include "internal-fn.h"
54 #include "gimple-iterator.h"
55 #include "gimplify.h"
56 #include "tree-dfa.h"
57 #include "langhooks-def.h"
58 #include "tree-diagnostic.h"
59 #include "except.h"
60 #include "builtins.h"
61 #include "print-tree.h"
62 #include "ipa-utils.h"
63 #include "selftest.h"
64 #include "stringpool.h"
65 #include "attribs.h"
66 #include "rtl.h"
67 #include "regs.h"
68 #include "tree-vector-builder.h"
69 #include "gimple-fold.h"
70 #include "escaped_string.h"
72 /* Tree code classes. */
74 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
75 #define END_OF_BASE_TREE_CODES tcc_exceptional,
77 const enum tree_code_class tree_code_type[] = {
78 #include "all-tree.def"
81 #undef DEFTREECODE
82 #undef END_OF_BASE_TREE_CODES
84 /* Table indexed by tree code giving number of expression
85 operands beyond the fixed part of the node structure.
86 Not used for types or decls. */
88 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
89 #define END_OF_BASE_TREE_CODES 0,
91 const unsigned char tree_code_length[] = {
92 #include "all-tree.def"
95 #undef DEFTREECODE
96 #undef END_OF_BASE_TREE_CODES
98 /* Names of tree components.
99 Used for printing out the tree and error messages. */
100 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
101 #define END_OF_BASE_TREE_CODES "@dummy",
103 static const char *const tree_code_name[] = {
104 #include "all-tree.def"
107 #undef DEFTREECODE
108 #undef END_OF_BASE_TREE_CODES
110 /* Each tree code class has an associated string representation.
111 These must correspond to the tree_code_class entries. */
113 const char *const tree_code_class_strings[] =
115 "exceptional",
116 "constant",
117 "type",
118 "declaration",
119 "reference",
120 "comparison",
121 "unary",
122 "binary",
123 "statement",
124 "vl_exp",
125 "expression"
128 /* obstack.[ch] explicitly declined to prototype this. */
129 extern int _obstack_allocated_p (struct obstack *h, void *obj);
131 /* Statistics-gathering stuff. */
133 static uint64_t tree_code_counts[MAX_TREE_CODES];
134 uint64_t tree_node_counts[(int) all_kinds];
135 uint64_t tree_node_sizes[(int) all_kinds];
137 /* Keep in sync with tree.h:enum tree_node_kind. */
138 static const char * const tree_node_kind_names[] = {
139 "decls",
140 "types",
141 "blocks",
142 "stmts",
143 "refs",
144 "exprs",
145 "constants",
146 "identifiers",
147 "vecs",
148 "binfos",
149 "ssa names",
150 "constructors",
151 "random kinds",
152 "lang_decl kinds",
153 "lang_type kinds",
154 "omp clauses",
157 /* Unique id for next decl created. */
158 static GTY(()) int next_decl_uid;
159 /* Unique id for next type created. */
160 static GTY(()) unsigned next_type_uid = 1;
161 /* Unique id for next debug decl created. Use negative numbers,
162 to catch erroneous uses. */
163 static GTY(()) int next_debug_decl_uid;
165 /* Since we cannot rehash a type after it is in the table, we have to
166 keep the hash code. */
168 struct GTY((for_user)) type_hash {
169 unsigned long hash;
170 tree type;
173 /* Initial size of the hash table (rounded to next prime). */
174 #define TYPE_HASH_INITIAL_SIZE 1000
176 struct type_cache_hasher : ggc_cache_ptr_hash<type_hash>
178 static hashval_t hash (type_hash *t) { return t->hash; }
179 static bool equal (type_hash *a, type_hash *b);
181 static int
182 keep_cache_entry (type_hash *&t)
184 return ggc_marked_p (t->type);
188 /* Now here is the hash table. When recording a type, it is added to
189 the slot whose index is the hash code. Note that the hash table is
190 used for several kinds of types (function types, array types and
191 array index range types, for now). While all these live in the
192 same table, they are completely independent, and the hash code is
193 computed differently for each of these. */
195 static GTY ((cache)) hash_table<type_cache_hasher> *type_hash_table;
197 /* Hash table and temporary node for larger integer const values. */
198 static GTY (()) tree int_cst_node;
200 struct int_cst_hasher : ggc_cache_ptr_hash<tree_node>
202 static hashval_t hash (tree t);
203 static bool equal (tree x, tree y);
206 static GTY ((cache)) hash_table<int_cst_hasher> *int_cst_hash_table;
208 /* Class and variable for making sure that there is a single POLY_INT_CST
209 for a given value. */
210 struct poly_int_cst_hasher : ggc_cache_ptr_hash<tree_node>
212 typedef std::pair<tree, const poly_wide_int *> compare_type;
213 static hashval_t hash (tree t);
214 static bool equal (tree x, const compare_type &y);
217 static GTY ((cache)) hash_table<poly_int_cst_hasher> *poly_int_cst_hash_table;
219 /* Hash table for optimization flags and target option flags. Use the same
220 hash table for both sets of options. Nodes for building the current
221 optimization and target option nodes. The assumption is most of the time
222 the options created will already be in the hash table, so we avoid
223 allocating and freeing up a node repeatably. */
224 static GTY (()) tree cl_optimization_node;
225 static GTY (()) tree cl_target_option_node;
227 struct cl_option_hasher : ggc_cache_ptr_hash<tree_node>
229 static hashval_t hash (tree t);
230 static bool equal (tree x, tree y);
233 static GTY ((cache)) hash_table<cl_option_hasher> *cl_option_hash_table;
235 /* General tree->tree mapping structure for use in hash tables. */
238 static GTY ((cache))
239 hash_table<tree_decl_map_cache_hasher> *debug_expr_for_decl;
241 static GTY ((cache))
242 hash_table<tree_decl_map_cache_hasher> *value_expr_for_decl;
244 struct tree_vec_map_cache_hasher : ggc_cache_ptr_hash<tree_vec_map>
246 static hashval_t hash (tree_vec_map *m) { return DECL_UID (m->base.from); }
248 static bool
249 equal (tree_vec_map *a, tree_vec_map *b)
251 return a->base.from == b->base.from;
254 static int
255 keep_cache_entry (tree_vec_map *&m)
257 return ggc_marked_p (m->base.from);
261 static GTY ((cache))
262 hash_table<tree_vec_map_cache_hasher> *debug_args_for_decl;
264 static void set_type_quals (tree, int);
265 static void print_type_hash_statistics (void);
266 static void print_debug_expr_statistics (void);
267 static void print_value_expr_statistics (void);
269 static tree build_array_type_1 (tree, tree, bool, bool, bool);
271 tree global_trees[TI_MAX];
272 tree integer_types[itk_none];
274 bool int_n_enabled_p[NUM_INT_N_ENTS];
275 struct int_n_trees_t int_n_trees [NUM_INT_N_ENTS];
277 bool tree_contains_struct[MAX_TREE_CODES][64];
279 /* Number of operands for each OpenMP clause. */
280 unsigned const char omp_clause_num_ops[] =
282 0, /* OMP_CLAUSE_ERROR */
283 1, /* OMP_CLAUSE_PRIVATE */
284 1, /* OMP_CLAUSE_SHARED */
285 1, /* OMP_CLAUSE_FIRSTPRIVATE */
286 2, /* OMP_CLAUSE_LASTPRIVATE */
287 5, /* OMP_CLAUSE_REDUCTION */
288 5, /* OMP_CLAUSE_TASK_REDUCTION */
289 5, /* OMP_CLAUSE_IN_REDUCTION */
290 1, /* OMP_CLAUSE_COPYIN */
291 1, /* OMP_CLAUSE_COPYPRIVATE */
292 3, /* OMP_CLAUSE_LINEAR */
293 2, /* OMP_CLAUSE_ALIGNED */
294 2, /* OMP_CLAUSE_ALLOCATE */
295 1, /* OMP_CLAUSE_DEPEND */
296 1, /* OMP_CLAUSE_NONTEMPORAL */
297 1, /* OMP_CLAUSE_UNIFORM */
298 1, /* OMP_CLAUSE_TO_DECLARE */
299 1, /* OMP_CLAUSE_LINK */
300 2, /* OMP_CLAUSE_FROM */
301 2, /* OMP_CLAUSE_TO */
302 2, /* OMP_CLAUSE_MAP */
303 1, /* OMP_CLAUSE_USE_DEVICE_PTR */
304 1, /* OMP_CLAUSE_USE_DEVICE_ADDR */
305 1, /* OMP_CLAUSE_IS_DEVICE_PTR */
306 1, /* OMP_CLAUSE_INCLUSIVE */
307 1, /* OMP_CLAUSE_EXCLUSIVE */
308 2, /* OMP_CLAUSE__CACHE_ */
309 2, /* OMP_CLAUSE_GANG */
310 1, /* OMP_CLAUSE_ASYNC */
311 1, /* OMP_CLAUSE_WAIT */
312 0, /* OMP_CLAUSE_AUTO */
313 0, /* OMP_CLAUSE_SEQ */
314 1, /* OMP_CLAUSE__LOOPTEMP_ */
315 1, /* OMP_CLAUSE__REDUCTEMP_ */
316 1, /* OMP_CLAUSE__CONDTEMP_ */
317 1, /* OMP_CLAUSE__SCANTEMP_ */
318 1, /* OMP_CLAUSE_IF */
319 1, /* OMP_CLAUSE_NUM_THREADS */
320 1, /* OMP_CLAUSE_SCHEDULE */
321 0, /* OMP_CLAUSE_NOWAIT */
322 1, /* OMP_CLAUSE_ORDERED */
323 0, /* OMP_CLAUSE_DEFAULT */
324 3, /* OMP_CLAUSE_COLLAPSE */
325 0, /* OMP_CLAUSE_UNTIED */
326 1, /* OMP_CLAUSE_FINAL */
327 0, /* OMP_CLAUSE_MERGEABLE */
328 1, /* OMP_CLAUSE_DEVICE */
329 1, /* OMP_CLAUSE_DIST_SCHEDULE */
330 0, /* OMP_CLAUSE_INBRANCH */
331 0, /* OMP_CLAUSE_NOTINBRANCH */
332 1, /* OMP_CLAUSE_NUM_TEAMS */
333 1, /* OMP_CLAUSE_THREAD_LIMIT */
334 0, /* OMP_CLAUSE_PROC_BIND */
335 1, /* OMP_CLAUSE_SAFELEN */
336 1, /* OMP_CLAUSE_SIMDLEN */
337 0, /* OMP_CLAUSE_DEVICE_TYPE */
338 0, /* OMP_CLAUSE_FOR */
339 0, /* OMP_CLAUSE_PARALLEL */
340 0, /* OMP_CLAUSE_SECTIONS */
341 0, /* OMP_CLAUSE_TASKGROUP */
342 1, /* OMP_CLAUSE_PRIORITY */
343 1, /* OMP_CLAUSE_GRAINSIZE */
344 1, /* OMP_CLAUSE_NUM_TASKS */
345 0, /* OMP_CLAUSE_NOGROUP */
346 0, /* OMP_CLAUSE_THREADS */
347 0, /* OMP_CLAUSE_SIMD */
348 1, /* OMP_CLAUSE_HINT */
349 0, /* OMP_CLAUSE_DEFAULTMAP */
350 0, /* OMP_CLAUSE_ORDER */
351 0, /* OMP_CLAUSE_BIND */
352 1, /* OMP_CLAUSE__SIMDUID_ */
353 0, /* OMP_CLAUSE__SIMT_ */
354 0, /* OMP_CLAUSE_INDEPENDENT */
355 1, /* OMP_CLAUSE_WORKER */
356 1, /* OMP_CLAUSE_VECTOR */
357 1, /* OMP_CLAUSE_NUM_GANGS */
358 1, /* OMP_CLAUSE_NUM_WORKERS */
359 1, /* OMP_CLAUSE_VECTOR_LENGTH */
360 3, /* OMP_CLAUSE_TILE */
361 0, /* OMP_CLAUSE_IF_PRESENT */
362 0, /* OMP_CLAUSE_FINALIZE */
365 const char * const omp_clause_code_name[] =
367 "error_clause",
368 "private",
369 "shared",
370 "firstprivate",
371 "lastprivate",
372 "reduction",
373 "task_reduction",
374 "in_reduction",
375 "copyin",
376 "copyprivate",
377 "linear",
378 "aligned",
379 "allocate",
380 "depend",
381 "nontemporal",
382 "uniform",
383 "to",
384 "link",
385 "from",
386 "to",
387 "map",
388 "use_device_ptr",
389 "use_device_addr",
390 "is_device_ptr",
391 "inclusive",
392 "exclusive",
393 "_cache_",
394 "gang",
395 "async",
396 "wait",
397 "auto",
398 "seq",
399 "_looptemp_",
400 "_reductemp_",
401 "_condtemp_",
402 "_scantemp_",
403 "if",
404 "num_threads",
405 "schedule",
406 "nowait",
407 "ordered",
408 "default",
409 "collapse",
410 "untied",
411 "final",
412 "mergeable",
413 "device",
414 "dist_schedule",
415 "inbranch",
416 "notinbranch",
417 "num_teams",
418 "thread_limit",
419 "proc_bind",
420 "safelen",
421 "simdlen",
422 "device_type",
423 "for",
424 "parallel",
425 "sections",
426 "taskgroup",
427 "priority",
428 "grainsize",
429 "num_tasks",
430 "nogroup",
431 "threads",
432 "simd",
433 "hint",
434 "defaultmap",
435 "order",
436 "bind",
437 "_simduid_",
438 "_simt_",
439 "independent",
440 "worker",
441 "vector",
442 "num_gangs",
443 "num_workers",
444 "vector_length",
445 "tile",
446 "if_present",
447 "finalize",
451 /* Return the tree node structure used by tree code CODE. */
453 static inline enum tree_node_structure_enum
454 tree_node_structure_for_code (enum tree_code code)
456 switch (TREE_CODE_CLASS (code))
458 case tcc_declaration:
459 switch (code)
461 case CONST_DECL: return TS_CONST_DECL;
462 case DEBUG_EXPR_DECL: return TS_DECL_WRTL;
463 case FIELD_DECL: return TS_FIELD_DECL;
464 case FUNCTION_DECL: return TS_FUNCTION_DECL;
465 case LABEL_DECL: return TS_LABEL_DECL;
466 case PARM_DECL: return TS_PARM_DECL;
467 case RESULT_DECL: return TS_RESULT_DECL;
468 case TRANSLATION_UNIT_DECL: return TS_TRANSLATION_UNIT_DECL;
469 case TYPE_DECL: return TS_TYPE_DECL;
470 case VAR_DECL: return TS_VAR_DECL;
471 default: return TS_DECL_NON_COMMON;
474 case tcc_type: return TS_TYPE_NON_COMMON;
476 case tcc_binary:
477 case tcc_comparison:
478 case tcc_expression:
479 case tcc_reference:
480 case tcc_statement:
481 case tcc_unary:
482 case tcc_vl_exp: return TS_EXP;
484 default: /* tcc_constant and tcc_exceptional */
485 break;
488 switch (code)
490 /* tcc_constant cases. */
491 case COMPLEX_CST: return TS_COMPLEX;
492 case FIXED_CST: return TS_FIXED_CST;
493 case INTEGER_CST: return TS_INT_CST;
494 case POLY_INT_CST: return TS_POLY_INT_CST;
495 case REAL_CST: return TS_REAL_CST;
496 case STRING_CST: return TS_STRING;
497 case VECTOR_CST: return TS_VECTOR;
498 case VOID_CST: return TS_TYPED;
500 /* tcc_exceptional cases. */
501 case BLOCK: return TS_BLOCK;
502 case CONSTRUCTOR: return TS_CONSTRUCTOR;
503 case ERROR_MARK: return TS_COMMON;
504 case IDENTIFIER_NODE: return TS_IDENTIFIER;
505 case OMP_CLAUSE: return TS_OMP_CLAUSE;
506 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
507 case PLACEHOLDER_EXPR: return TS_COMMON;
508 case SSA_NAME: return TS_SSA_NAME;
509 case STATEMENT_LIST: return TS_STATEMENT_LIST;
510 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
511 case TREE_BINFO: return TS_BINFO;
512 case TREE_LIST: return TS_LIST;
513 case TREE_VEC: return TS_VEC;
515 default:
516 gcc_unreachable ();
521 /* Initialize tree_contains_struct to describe the hierarchy of tree
522 nodes. */
524 static void
525 initialize_tree_contains_struct (void)
527 unsigned i;
529 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
531 enum tree_code code;
532 enum tree_node_structure_enum ts_code;
534 code = (enum tree_code) i;
535 ts_code = tree_node_structure_for_code (code);
537 /* Mark the TS structure itself. */
538 tree_contains_struct[code][ts_code] = 1;
540 /* Mark all the structures that TS is derived from. */
541 switch (ts_code)
543 case TS_TYPED:
544 case TS_BLOCK:
545 case TS_OPTIMIZATION:
546 case TS_TARGET_OPTION:
547 MARK_TS_BASE (code);
548 break;
550 case TS_COMMON:
551 case TS_INT_CST:
552 case TS_POLY_INT_CST:
553 case TS_REAL_CST:
554 case TS_FIXED_CST:
555 case TS_VECTOR:
556 case TS_STRING:
557 case TS_COMPLEX:
558 case TS_SSA_NAME:
559 case TS_CONSTRUCTOR:
560 case TS_EXP:
561 case TS_STATEMENT_LIST:
562 MARK_TS_TYPED (code);
563 break;
565 case TS_IDENTIFIER:
566 case TS_DECL_MINIMAL:
567 case TS_TYPE_COMMON:
568 case TS_LIST:
569 case TS_VEC:
570 case TS_BINFO:
571 case TS_OMP_CLAUSE:
572 MARK_TS_COMMON (code);
573 break;
575 case TS_TYPE_WITH_LANG_SPECIFIC:
576 MARK_TS_TYPE_COMMON (code);
577 break;
579 case TS_TYPE_NON_COMMON:
580 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
581 break;
583 case TS_DECL_COMMON:
584 MARK_TS_DECL_MINIMAL (code);
585 break;
587 case TS_DECL_WRTL:
588 case TS_CONST_DECL:
589 MARK_TS_DECL_COMMON (code);
590 break;
592 case TS_DECL_NON_COMMON:
593 MARK_TS_DECL_WITH_VIS (code);
594 break;
596 case TS_DECL_WITH_VIS:
597 case TS_PARM_DECL:
598 case TS_LABEL_DECL:
599 case TS_RESULT_DECL:
600 MARK_TS_DECL_WRTL (code);
601 break;
603 case TS_FIELD_DECL:
604 MARK_TS_DECL_COMMON (code);
605 break;
607 case TS_VAR_DECL:
608 MARK_TS_DECL_WITH_VIS (code);
609 break;
611 case TS_TYPE_DECL:
612 case TS_FUNCTION_DECL:
613 MARK_TS_DECL_NON_COMMON (code);
614 break;
616 case TS_TRANSLATION_UNIT_DECL:
617 MARK_TS_DECL_COMMON (code);
618 break;
620 default:
621 gcc_unreachable ();
625 /* Basic consistency checks for attributes used in fold. */
626 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
627 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
628 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
629 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
630 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
631 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
632 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
633 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
634 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
635 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
636 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
637 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
638 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
639 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
640 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
641 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
642 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
643 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
644 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
645 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
646 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
647 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
648 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
649 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
650 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
651 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
652 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
653 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
654 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
655 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
656 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
657 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
658 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
659 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
660 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
661 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
662 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
663 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
664 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_MINIMAL]);
665 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_COMMON]);
669 /* Init tree.c. */
671 void
672 init_ttree (void)
674 /* Initialize the hash table of types. */
675 type_hash_table
676 = hash_table<type_cache_hasher>::create_ggc (TYPE_HASH_INITIAL_SIZE);
678 debug_expr_for_decl
679 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
681 value_expr_for_decl
682 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
684 int_cst_hash_table = hash_table<int_cst_hasher>::create_ggc (1024);
686 poly_int_cst_hash_table = hash_table<poly_int_cst_hasher>::create_ggc (64);
688 int_cst_node = make_int_cst (1, 1);
690 cl_option_hash_table = hash_table<cl_option_hasher>::create_ggc (64);
692 cl_optimization_node = make_node (OPTIMIZATION_NODE);
693 cl_target_option_node = make_node (TARGET_OPTION_NODE);
695 /* Initialize the tree_contains_struct array. */
696 initialize_tree_contains_struct ();
697 lang_hooks.init_ts ();
701 /* The name of the object as the assembler will see it (but before any
702 translations made by ASM_OUTPUT_LABELREF). Often this is the same
703 as DECL_NAME. It is an IDENTIFIER_NODE. */
704 tree
705 decl_assembler_name (tree decl)
707 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
708 lang_hooks.set_decl_assembler_name (decl);
709 return DECL_ASSEMBLER_NAME_RAW (decl);
712 /* The DECL_ASSEMBLER_NAME_RAW of DECL is being explicitly set to NAME
713 (either of which may be NULL). Inform the FE, if this changes the
714 name. */
716 void
717 overwrite_decl_assembler_name (tree decl, tree name)
719 if (DECL_ASSEMBLER_NAME_RAW (decl) != name)
720 lang_hooks.overwrite_decl_assembler_name (decl, name);
723 /* When the target supports COMDAT groups, this indicates which group the
724 DECL is associated with. This can be either an IDENTIFIER_NODE or a
725 decl, in which case its DECL_ASSEMBLER_NAME identifies the group. */
726 tree
727 decl_comdat_group (const_tree node)
729 struct symtab_node *snode = symtab_node::get (node);
730 if (!snode)
731 return NULL;
732 return snode->get_comdat_group ();
735 /* Likewise, but make sure it's been reduced to an IDENTIFIER_NODE. */
736 tree
737 decl_comdat_group_id (const_tree node)
739 struct symtab_node *snode = symtab_node::get (node);
740 if (!snode)
741 return NULL;
742 return snode->get_comdat_group_id ();
745 /* When the target supports named section, return its name as IDENTIFIER_NODE
746 or NULL if it is in no section. */
747 const char *
748 decl_section_name (const_tree node)
750 struct symtab_node *snode = symtab_node::get (node);
751 if (!snode)
752 return NULL;
753 return snode->get_section ();
756 /* Set section name of NODE to VALUE (that is expected to be
757 identifier node) */
758 void
759 set_decl_section_name (tree node, const char *value)
761 struct symtab_node *snode;
763 if (value == NULL)
765 snode = symtab_node::get (node);
766 if (!snode)
767 return;
769 else if (VAR_P (node))
770 snode = varpool_node::get_create (node);
771 else
772 snode = cgraph_node::get_create (node);
773 snode->set_section (value);
776 /* Set section name of NODE to match the section name of OTHER.
778 set_decl_section_name (decl, other) is equivalent to
779 set_decl_section_name (decl, DECL_SECTION_NAME (other)), but possibly more
780 efficient. */
781 void
782 set_decl_section_name (tree decl, const_tree other)
784 struct symtab_node *other_node = symtab_node::get (other);
785 if (other_node)
787 struct symtab_node *decl_node;
788 if (VAR_P (decl))
789 decl_node = varpool_node::get_create (decl);
790 else
791 decl_node = cgraph_node::get_create (decl);
792 decl_node->set_section (*other_node);
794 else
796 struct symtab_node *decl_node = symtab_node::get (decl);
797 if (!decl_node)
798 return;
799 decl_node->set_section (NULL);
803 /* Return TLS model of a variable NODE. */
804 enum tls_model
805 decl_tls_model (const_tree node)
807 struct varpool_node *snode = varpool_node::get (node);
808 if (!snode)
809 return TLS_MODEL_NONE;
810 return snode->tls_model;
813 /* Set TLS model of variable NODE to MODEL. */
814 void
815 set_decl_tls_model (tree node, enum tls_model model)
817 struct varpool_node *vnode;
819 if (model == TLS_MODEL_NONE)
821 vnode = varpool_node::get (node);
822 if (!vnode)
823 return;
825 else
826 vnode = varpool_node::get_create (node);
827 vnode->tls_model = model;
830 /* Compute the number of bytes occupied by a tree with code CODE.
831 This function cannot be used for nodes that have variable sizes,
832 including TREE_VEC, INTEGER_CST, STRING_CST, and CALL_EXPR. */
833 size_t
834 tree_code_size (enum tree_code code)
836 switch (TREE_CODE_CLASS (code))
838 case tcc_declaration: /* A decl node */
839 switch (code)
841 case FIELD_DECL: return sizeof (tree_field_decl);
842 case PARM_DECL: return sizeof (tree_parm_decl);
843 case VAR_DECL: return sizeof (tree_var_decl);
844 case LABEL_DECL: return sizeof (tree_label_decl);
845 case RESULT_DECL: return sizeof (tree_result_decl);
846 case CONST_DECL: return sizeof (tree_const_decl);
847 case TYPE_DECL: return sizeof (tree_type_decl);
848 case FUNCTION_DECL: return sizeof (tree_function_decl);
849 case DEBUG_EXPR_DECL: return sizeof (tree_decl_with_rtl);
850 case TRANSLATION_UNIT_DECL: return sizeof (tree_translation_unit_decl);
851 case NAMESPACE_DECL:
852 case IMPORTED_DECL:
853 case NAMELIST_DECL: return sizeof (tree_decl_non_common);
854 default:
855 gcc_checking_assert (code >= NUM_TREE_CODES);
856 return lang_hooks.tree_size (code);
859 case tcc_type: /* a type node */
860 switch (code)
862 case OFFSET_TYPE:
863 case ENUMERAL_TYPE:
864 case BOOLEAN_TYPE:
865 case INTEGER_TYPE:
866 case REAL_TYPE:
867 case POINTER_TYPE:
868 case REFERENCE_TYPE:
869 case NULLPTR_TYPE:
870 case FIXED_POINT_TYPE:
871 case COMPLEX_TYPE:
872 case VECTOR_TYPE:
873 case ARRAY_TYPE:
874 case RECORD_TYPE:
875 case UNION_TYPE:
876 case QUAL_UNION_TYPE:
877 case VOID_TYPE:
878 case FUNCTION_TYPE:
879 case METHOD_TYPE:
880 case LANG_TYPE: return sizeof (tree_type_non_common);
881 default:
882 gcc_checking_assert (code >= NUM_TREE_CODES);
883 return lang_hooks.tree_size (code);
886 case tcc_reference: /* a reference */
887 case tcc_expression: /* an expression */
888 case tcc_statement: /* an expression with side effects */
889 case tcc_comparison: /* a comparison expression */
890 case tcc_unary: /* a unary arithmetic expression */
891 case tcc_binary: /* a binary arithmetic expression */
892 return (sizeof (struct tree_exp)
893 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
895 case tcc_constant: /* a constant */
896 switch (code)
898 case VOID_CST: return sizeof (tree_typed);
899 case INTEGER_CST: gcc_unreachable ();
900 case POLY_INT_CST: return sizeof (tree_poly_int_cst);
901 case REAL_CST: return sizeof (tree_real_cst);
902 case FIXED_CST: return sizeof (tree_fixed_cst);
903 case COMPLEX_CST: return sizeof (tree_complex);
904 case VECTOR_CST: gcc_unreachable ();
905 case STRING_CST: gcc_unreachable ();
906 default:
907 gcc_checking_assert (code >= NUM_TREE_CODES);
908 return lang_hooks.tree_size (code);
911 case tcc_exceptional: /* something random, like an identifier. */
912 switch (code)
914 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
915 case TREE_LIST: return sizeof (tree_list);
917 case ERROR_MARK:
918 case PLACEHOLDER_EXPR: return sizeof (tree_common);
920 case TREE_VEC: gcc_unreachable ();
921 case OMP_CLAUSE: gcc_unreachable ();
923 case SSA_NAME: return sizeof (tree_ssa_name);
925 case STATEMENT_LIST: return sizeof (tree_statement_list);
926 case BLOCK: return sizeof (struct tree_block);
927 case CONSTRUCTOR: return sizeof (tree_constructor);
928 case OPTIMIZATION_NODE: return sizeof (tree_optimization_option);
929 case TARGET_OPTION_NODE: return sizeof (tree_target_option);
931 default:
932 gcc_checking_assert (code >= NUM_TREE_CODES);
933 return lang_hooks.tree_size (code);
936 default:
937 gcc_unreachable ();
941 /* Compute the number of bytes occupied by NODE. This routine only
942 looks at TREE_CODE, except for those nodes that have variable sizes. */
943 size_t
944 tree_size (const_tree node)
946 const enum tree_code code = TREE_CODE (node);
947 switch (code)
949 case INTEGER_CST:
950 return (sizeof (struct tree_int_cst)
951 + (TREE_INT_CST_EXT_NUNITS (node) - 1) * sizeof (HOST_WIDE_INT));
953 case TREE_BINFO:
954 return (offsetof (struct tree_binfo, base_binfos)
955 + vec<tree, va_gc>
956 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
958 case TREE_VEC:
959 return (sizeof (struct tree_vec)
960 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
962 case VECTOR_CST:
963 return (sizeof (struct tree_vector)
964 + (vector_cst_encoded_nelts (node) - 1) * sizeof (tree));
966 case STRING_CST:
967 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
969 case OMP_CLAUSE:
970 return (sizeof (struct tree_omp_clause)
971 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
972 * sizeof (tree));
974 default:
975 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
976 return (sizeof (struct tree_exp)
977 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
978 else
979 return tree_code_size (code);
983 /* Return tree node kind based on tree CODE. */
985 static tree_node_kind
986 get_stats_node_kind (enum tree_code code)
988 enum tree_code_class type = TREE_CODE_CLASS (code);
990 switch (type)
992 case tcc_declaration: /* A decl node */
993 return d_kind;
994 case tcc_type: /* a type node */
995 return t_kind;
996 case tcc_statement: /* an expression with side effects */
997 return s_kind;
998 case tcc_reference: /* a reference */
999 return r_kind;
1000 case tcc_expression: /* an expression */
1001 case tcc_comparison: /* a comparison expression */
1002 case tcc_unary: /* a unary arithmetic expression */
1003 case tcc_binary: /* a binary arithmetic expression */
1004 return e_kind;
1005 case tcc_constant: /* a constant */
1006 return c_kind;
1007 case tcc_exceptional: /* something random, like an identifier. */
1008 switch (code)
1010 case IDENTIFIER_NODE:
1011 return id_kind;
1012 case TREE_VEC:
1013 return vec_kind;
1014 case TREE_BINFO:
1015 return binfo_kind;
1016 case SSA_NAME:
1017 return ssa_name_kind;
1018 case BLOCK:
1019 return b_kind;
1020 case CONSTRUCTOR:
1021 return constr_kind;
1022 case OMP_CLAUSE:
1023 return omp_clause_kind;
1024 default:
1025 return x_kind;
1027 break;
1028 case tcc_vl_exp:
1029 return e_kind;
1030 default:
1031 gcc_unreachable ();
1035 /* Record interesting allocation statistics for a tree node with CODE
1036 and LENGTH. */
1038 static void
1039 record_node_allocation_statistics (enum tree_code code, size_t length)
1041 if (!GATHER_STATISTICS)
1042 return;
1044 tree_node_kind kind = get_stats_node_kind (code);
1046 tree_code_counts[(int) code]++;
1047 tree_node_counts[(int) kind]++;
1048 tree_node_sizes[(int) kind] += length;
1051 /* Allocate and return a new UID from the DECL_UID namespace. */
1054 allocate_decl_uid (void)
1056 return next_decl_uid++;
1059 /* Return a newly allocated node of code CODE. For decl and type
1060 nodes, some other fields are initialized. The rest of the node is
1061 initialized to zero. This function cannot be used for TREE_VEC,
1062 INTEGER_CST or OMP_CLAUSE nodes, which is enforced by asserts in
1063 tree_code_size.
1065 Achoo! I got a code in the node. */
1067 tree
1068 make_node (enum tree_code code MEM_STAT_DECL)
1070 tree t;
1071 enum tree_code_class type = TREE_CODE_CLASS (code);
1072 size_t length = tree_code_size (code);
1074 record_node_allocation_statistics (code, length);
1076 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1077 TREE_SET_CODE (t, code);
1079 switch (type)
1081 case tcc_statement:
1082 if (code != DEBUG_BEGIN_STMT)
1083 TREE_SIDE_EFFECTS (t) = 1;
1084 break;
1086 case tcc_declaration:
1087 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1089 if (code == FUNCTION_DECL)
1091 SET_DECL_ALIGN (t, FUNCTION_ALIGNMENT (FUNCTION_BOUNDARY));
1092 SET_DECL_MODE (t, FUNCTION_MODE);
1094 else
1095 SET_DECL_ALIGN (t, 1);
1097 DECL_SOURCE_LOCATION (t) = input_location;
1098 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
1099 DECL_UID (t) = --next_debug_decl_uid;
1100 else
1102 DECL_UID (t) = allocate_decl_uid ();
1103 SET_DECL_PT_UID (t, -1);
1105 if (TREE_CODE (t) == LABEL_DECL)
1106 LABEL_DECL_UID (t) = -1;
1108 break;
1110 case tcc_type:
1111 TYPE_UID (t) = next_type_uid++;
1112 SET_TYPE_ALIGN (t, BITS_PER_UNIT);
1113 TYPE_USER_ALIGN (t) = 0;
1114 TYPE_MAIN_VARIANT (t) = t;
1115 TYPE_CANONICAL (t) = t;
1117 /* Default to no attributes for type, but let target change that. */
1118 TYPE_ATTRIBUTES (t) = NULL_TREE;
1119 targetm.set_default_type_attributes (t);
1121 /* We have not yet computed the alias set for this type. */
1122 TYPE_ALIAS_SET (t) = -1;
1123 break;
1125 case tcc_constant:
1126 TREE_CONSTANT (t) = 1;
1127 break;
1129 case tcc_expression:
1130 switch (code)
1132 case INIT_EXPR:
1133 case MODIFY_EXPR:
1134 case VA_ARG_EXPR:
1135 case PREDECREMENT_EXPR:
1136 case PREINCREMENT_EXPR:
1137 case POSTDECREMENT_EXPR:
1138 case POSTINCREMENT_EXPR:
1139 /* All of these have side-effects, no matter what their
1140 operands are. */
1141 TREE_SIDE_EFFECTS (t) = 1;
1142 break;
1144 default:
1145 break;
1147 break;
1149 case tcc_exceptional:
1150 switch (code)
1152 case TARGET_OPTION_NODE:
1153 TREE_TARGET_OPTION(t)
1154 = ggc_cleared_alloc<struct cl_target_option> ();
1155 break;
1157 case OPTIMIZATION_NODE:
1158 TREE_OPTIMIZATION (t)
1159 = ggc_cleared_alloc<struct cl_optimization> ();
1160 break;
1162 default:
1163 break;
1165 break;
1167 default:
1168 /* Other classes need no special treatment. */
1169 break;
1172 return t;
1175 /* Free tree node. */
1177 void
1178 free_node (tree node)
1180 enum tree_code code = TREE_CODE (node);
1181 if (GATHER_STATISTICS)
1183 enum tree_node_kind kind = get_stats_node_kind (code);
1185 gcc_checking_assert (tree_code_counts[(int) TREE_CODE (node)] != 0);
1186 gcc_checking_assert (tree_node_counts[(int) kind] != 0);
1187 gcc_checking_assert (tree_node_sizes[(int) kind] >= tree_size (node));
1189 tree_code_counts[(int) TREE_CODE (node)]--;
1190 tree_node_counts[(int) kind]--;
1191 tree_node_sizes[(int) kind] -= tree_size (node);
1193 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1194 vec_free (CONSTRUCTOR_ELTS (node));
1195 else if (code == BLOCK)
1196 vec_free (BLOCK_NONLOCALIZED_VARS (node));
1197 else if (code == TREE_BINFO)
1198 vec_free (BINFO_BASE_ACCESSES (node));
1199 else if (code == OPTIMIZATION_NODE)
1200 cl_optimization_option_free (TREE_OPTIMIZATION (node));
1201 else if (code == TARGET_OPTION_NODE)
1202 cl_target_option_free (TREE_TARGET_OPTION (node));
1203 ggc_free (node);
1206 /* Return a new node with the same contents as NODE except that its
1207 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
1209 tree
1210 copy_node (tree node MEM_STAT_DECL)
1212 tree t;
1213 enum tree_code code = TREE_CODE (node);
1214 size_t length;
1216 gcc_assert (code != STATEMENT_LIST);
1218 length = tree_size (node);
1219 record_node_allocation_statistics (code, length);
1220 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1221 memcpy (t, node, length);
1223 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
1224 TREE_CHAIN (t) = 0;
1225 TREE_ASM_WRITTEN (t) = 0;
1226 TREE_VISITED (t) = 0;
1228 if (TREE_CODE_CLASS (code) == tcc_declaration)
1230 if (code == DEBUG_EXPR_DECL)
1231 DECL_UID (t) = --next_debug_decl_uid;
1232 else
1234 DECL_UID (t) = allocate_decl_uid ();
1235 if (DECL_PT_UID_SET_P (node))
1236 SET_DECL_PT_UID (t, DECL_PT_UID (node));
1238 if ((TREE_CODE (node) == PARM_DECL || VAR_P (node))
1239 && DECL_HAS_VALUE_EXPR_P (node))
1241 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
1242 DECL_HAS_VALUE_EXPR_P (t) = 1;
1244 /* DECL_DEBUG_EXPR is copied explicitly by callers. */
1245 if (VAR_P (node))
1247 DECL_HAS_DEBUG_EXPR_P (t) = 0;
1248 t->decl_with_vis.symtab_node = NULL;
1250 if (VAR_P (node) && DECL_HAS_INIT_PRIORITY_P (node))
1252 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
1253 DECL_HAS_INIT_PRIORITY_P (t) = 1;
1255 if (TREE_CODE (node) == FUNCTION_DECL)
1257 DECL_STRUCT_FUNCTION (t) = NULL;
1258 t->decl_with_vis.symtab_node = NULL;
1261 else if (TREE_CODE_CLASS (code) == tcc_type)
1263 TYPE_UID (t) = next_type_uid++;
1264 /* The following is so that the debug code for
1265 the copy is different from the original type.
1266 The two statements usually duplicate each other
1267 (because they clear fields of the same union),
1268 but the optimizer should catch that. */
1269 TYPE_SYMTAB_ADDRESS (t) = 0;
1270 TYPE_SYMTAB_DIE (t) = 0;
1272 /* Do not copy the values cache. */
1273 if (TYPE_CACHED_VALUES_P (t))
1275 TYPE_CACHED_VALUES_P (t) = 0;
1276 TYPE_CACHED_VALUES (t) = NULL_TREE;
1279 else if (code == TARGET_OPTION_NODE)
1281 TREE_TARGET_OPTION (t) = ggc_alloc<struct cl_target_option>();
1282 memcpy (TREE_TARGET_OPTION (t), TREE_TARGET_OPTION (node),
1283 sizeof (struct cl_target_option));
1285 else if (code == OPTIMIZATION_NODE)
1287 TREE_OPTIMIZATION (t) = ggc_alloc<struct cl_optimization>();
1288 memcpy (TREE_OPTIMIZATION (t), TREE_OPTIMIZATION (node),
1289 sizeof (struct cl_optimization));
1292 return t;
1295 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1296 For example, this can copy a list made of TREE_LIST nodes. */
1298 tree
1299 copy_list (tree list)
1301 tree head;
1302 tree prev, next;
1304 if (list == 0)
1305 return 0;
1307 head = prev = copy_node (list);
1308 next = TREE_CHAIN (list);
1309 while (next)
1311 TREE_CHAIN (prev) = copy_node (next);
1312 prev = TREE_CHAIN (prev);
1313 next = TREE_CHAIN (next);
1315 return head;
1319 /* Return the value that TREE_INT_CST_EXT_NUNITS should have for an
1320 INTEGER_CST with value CST and type TYPE. */
1322 static unsigned int
1323 get_int_cst_ext_nunits (tree type, const wide_int &cst)
1325 gcc_checking_assert (cst.get_precision () == TYPE_PRECISION (type));
1326 /* We need extra HWIs if CST is an unsigned integer with its
1327 upper bit set. */
1328 if (TYPE_UNSIGNED (type) && wi::neg_p (cst))
1329 return cst.get_precision () / HOST_BITS_PER_WIDE_INT + 1;
1330 return cst.get_len ();
1333 /* Return a new INTEGER_CST with value CST and type TYPE. */
1335 static tree
1336 build_new_int_cst (tree type, const wide_int &cst)
1338 unsigned int len = cst.get_len ();
1339 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1340 tree nt = make_int_cst (len, ext_len);
1342 if (len < ext_len)
1344 --ext_len;
1345 TREE_INT_CST_ELT (nt, ext_len)
1346 = zext_hwi (-1, cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1347 for (unsigned int i = len; i < ext_len; ++i)
1348 TREE_INT_CST_ELT (nt, i) = -1;
1350 else if (TYPE_UNSIGNED (type)
1351 && cst.get_precision () < len * HOST_BITS_PER_WIDE_INT)
1353 len--;
1354 TREE_INT_CST_ELT (nt, len)
1355 = zext_hwi (cst.elt (len),
1356 cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1359 for (unsigned int i = 0; i < len; i++)
1360 TREE_INT_CST_ELT (nt, i) = cst.elt (i);
1361 TREE_TYPE (nt) = type;
1362 return nt;
1365 /* Return a new POLY_INT_CST with coefficients COEFFS and type TYPE. */
1367 static tree
1368 build_new_poly_int_cst (tree type, tree (&coeffs)[NUM_POLY_INT_COEFFS]
1369 CXX_MEM_STAT_INFO)
1371 size_t length = sizeof (struct tree_poly_int_cst);
1372 record_node_allocation_statistics (POLY_INT_CST, length);
1374 tree t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1376 TREE_SET_CODE (t, POLY_INT_CST);
1377 TREE_CONSTANT (t) = 1;
1378 TREE_TYPE (t) = type;
1379 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1380 POLY_INT_CST_COEFF (t, i) = coeffs[i];
1381 return t;
1384 /* Create a constant tree that contains CST sign-extended to TYPE. */
1386 tree
1387 build_int_cst (tree type, poly_int64 cst)
1389 /* Support legacy code. */
1390 if (!type)
1391 type = integer_type_node;
1393 return wide_int_to_tree (type, wi::shwi (cst, TYPE_PRECISION (type)));
1396 /* Create a constant tree that contains CST zero-extended to TYPE. */
1398 tree
1399 build_int_cstu (tree type, poly_uint64 cst)
1401 return wide_int_to_tree (type, wi::uhwi (cst, TYPE_PRECISION (type)));
1404 /* Create a constant tree that contains CST sign-extended to TYPE. */
1406 tree
1407 build_int_cst_type (tree type, poly_int64 cst)
1409 gcc_assert (type);
1410 return wide_int_to_tree (type, wi::shwi (cst, TYPE_PRECISION (type)));
1413 /* Constructs tree in type TYPE from with value given by CST. Signedness
1414 of CST is assumed to be the same as the signedness of TYPE. */
1416 tree
1417 double_int_to_tree (tree type, double_int cst)
1419 return wide_int_to_tree (type, widest_int::from (cst, TYPE_SIGN (type)));
1422 /* We force the wide_int CST to the range of the type TYPE by sign or
1423 zero extending it. OVERFLOWABLE indicates if we are interested in
1424 overflow of the value, when >0 we are only interested in signed
1425 overflow, for <0 we are interested in any overflow. OVERFLOWED
1426 indicates whether overflow has already occurred. CONST_OVERFLOWED
1427 indicates whether constant overflow has already occurred. We force
1428 T's value to be within range of T's type (by setting to 0 or 1 all
1429 the bits outside the type's range). We set TREE_OVERFLOWED if,
1430 OVERFLOWED is nonzero,
1431 or OVERFLOWABLE is >0 and signed overflow occurs
1432 or OVERFLOWABLE is <0 and any overflow occurs
1433 We return a new tree node for the extended wide_int. The node
1434 is shared if no overflow flags are set. */
1437 tree
1438 force_fit_type (tree type, const poly_wide_int_ref &cst,
1439 int overflowable, bool overflowed)
1441 signop sign = TYPE_SIGN (type);
1443 /* If we need to set overflow flags, return a new unshared node. */
1444 if (overflowed || !wi::fits_to_tree_p (cst, type))
1446 if (overflowed
1447 || overflowable < 0
1448 || (overflowable > 0 && sign == SIGNED))
1450 poly_wide_int tmp = poly_wide_int::from (cst, TYPE_PRECISION (type),
1451 sign);
1452 tree t;
1453 if (tmp.is_constant ())
1454 t = build_new_int_cst (type, tmp.coeffs[0]);
1455 else
1457 tree coeffs[NUM_POLY_INT_COEFFS];
1458 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1460 coeffs[i] = build_new_int_cst (type, tmp.coeffs[i]);
1461 TREE_OVERFLOW (coeffs[i]) = 1;
1463 t = build_new_poly_int_cst (type, coeffs);
1465 TREE_OVERFLOW (t) = 1;
1466 return t;
1470 /* Else build a shared node. */
1471 return wide_int_to_tree (type, cst);
1474 /* These are the hash table functions for the hash table of INTEGER_CST
1475 nodes of a sizetype. */
1477 /* Return the hash code X, an INTEGER_CST. */
1479 hashval_t
1480 int_cst_hasher::hash (tree x)
1482 const_tree const t = x;
1483 hashval_t code = TYPE_UID (TREE_TYPE (t));
1484 int i;
1486 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
1487 code = iterative_hash_host_wide_int (TREE_INT_CST_ELT(t, i), code);
1489 return code;
1492 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1493 is the same as that given by *Y, which is the same. */
1495 bool
1496 int_cst_hasher::equal (tree x, tree y)
1498 const_tree const xt = x;
1499 const_tree const yt = y;
1501 if (TREE_TYPE (xt) != TREE_TYPE (yt)
1502 || TREE_INT_CST_NUNITS (xt) != TREE_INT_CST_NUNITS (yt)
1503 || TREE_INT_CST_EXT_NUNITS (xt) != TREE_INT_CST_EXT_NUNITS (yt))
1504 return false;
1506 for (int i = 0; i < TREE_INT_CST_NUNITS (xt); i++)
1507 if (TREE_INT_CST_ELT (xt, i) != TREE_INT_CST_ELT (yt, i))
1508 return false;
1510 return true;
1513 /* Cache wide_int CST into the TYPE_CACHED_VALUES cache for TYPE.
1514 SLOT is the slot entry to store it in, and MAX_SLOTS is the maximum
1515 number of slots that can be cached for the type. */
1517 static inline tree
1518 cache_wide_int_in_type_cache (tree type, const wide_int &cst,
1519 int slot, int max_slots)
1521 gcc_checking_assert (slot >= 0);
1522 /* Initialize cache. */
1523 if (!TYPE_CACHED_VALUES_P (type))
1525 TYPE_CACHED_VALUES_P (type) = 1;
1526 TYPE_CACHED_VALUES (type) = make_tree_vec (max_slots);
1528 tree t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), slot);
1529 if (!t)
1531 /* Create a new shared int. */
1532 t = build_new_int_cst (type, cst);
1533 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), slot) = t;
1535 return t;
1538 /* Create an INT_CST node of TYPE and value CST.
1539 The returned node is always shared. For small integers we use a
1540 per-type vector cache, for larger ones we use a single hash table.
1541 The value is extended from its precision according to the sign of
1542 the type to be a multiple of HOST_BITS_PER_WIDE_INT. This defines
1543 the upper bits and ensures that hashing and value equality based
1544 upon the underlying HOST_WIDE_INTs works without masking. */
1546 static tree
1547 wide_int_to_tree_1 (tree type, const wide_int_ref &pcst)
1549 tree t;
1550 int ix = -1;
1551 int limit = 0;
1553 gcc_assert (type);
1554 unsigned int prec = TYPE_PRECISION (type);
1555 signop sgn = TYPE_SIGN (type);
1557 /* Verify that everything is canonical. */
1558 int l = pcst.get_len ();
1559 if (l > 1)
1561 if (pcst.elt (l - 1) == 0)
1562 gcc_checking_assert (pcst.elt (l - 2) < 0);
1563 if (pcst.elt (l - 1) == HOST_WIDE_INT_M1)
1564 gcc_checking_assert (pcst.elt (l - 2) >= 0);
1567 wide_int cst = wide_int::from (pcst, prec, sgn);
1568 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1570 enum tree_code code = TREE_CODE (type);
1571 if (code == POINTER_TYPE || code == REFERENCE_TYPE)
1573 /* Cache NULL pointer and zero bounds. */
1574 if (cst == 0)
1575 ix = 0;
1576 /* Cache upper bounds of pointers. */
1577 else if (cst == wi::max_value (prec, sgn))
1578 ix = 1;
1579 /* Cache 1 which is used for a non-zero range. */
1580 else if (cst == 1)
1581 ix = 2;
1583 if (ix >= 0)
1585 t = cache_wide_int_in_type_cache (type, cst, ix, 3);
1586 /* Make sure no one is clobbering the shared constant. */
1587 gcc_checking_assert (TREE_TYPE (t) == type
1588 && cst == wi::to_wide (t));
1589 return t;
1592 if (ext_len == 1)
1594 /* We just need to store a single HOST_WIDE_INT. */
1595 HOST_WIDE_INT hwi;
1596 if (TYPE_UNSIGNED (type))
1597 hwi = cst.to_uhwi ();
1598 else
1599 hwi = cst.to_shwi ();
1601 switch (code)
1603 case NULLPTR_TYPE:
1604 gcc_assert (hwi == 0);
1605 /* Fallthru. */
1607 case POINTER_TYPE:
1608 case REFERENCE_TYPE:
1609 /* Ignore pointers, as they were already handled above. */
1610 break;
1612 case BOOLEAN_TYPE:
1613 /* Cache false or true. */
1614 limit = 2;
1615 if (IN_RANGE (hwi, 0, 1))
1616 ix = hwi;
1617 break;
1619 case INTEGER_TYPE:
1620 case OFFSET_TYPE:
1621 if (TYPE_SIGN (type) == UNSIGNED)
1623 /* Cache [0, N). */
1624 limit = param_integer_share_limit;
1625 if (IN_RANGE (hwi, 0, param_integer_share_limit - 1))
1626 ix = hwi;
1628 else
1630 /* Cache [-1, N). */
1631 limit = param_integer_share_limit + 1;
1632 if (IN_RANGE (hwi, -1, param_integer_share_limit - 1))
1633 ix = hwi + 1;
1635 break;
1637 case ENUMERAL_TYPE:
1638 break;
1640 default:
1641 gcc_unreachable ();
1644 if (ix >= 0)
1646 t = cache_wide_int_in_type_cache (type, cst, ix, limit);
1647 /* Make sure no one is clobbering the shared constant. */
1648 gcc_checking_assert (TREE_TYPE (t) == type
1649 && TREE_INT_CST_NUNITS (t) == 1
1650 && TREE_INT_CST_OFFSET_NUNITS (t) == 1
1651 && TREE_INT_CST_EXT_NUNITS (t) == 1
1652 && TREE_INT_CST_ELT (t, 0) == hwi);
1653 return t;
1655 else
1657 /* Use the cache of larger shared ints, using int_cst_node as
1658 a temporary. */
1660 TREE_INT_CST_ELT (int_cst_node, 0) = hwi;
1661 TREE_TYPE (int_cst_node) = type;
1663 tree *slot = int_cst_hash_table->find_slot (int_cst_node, INSERT);
1664 t = *slot;
1665 if (!t)
1667 /* Insert this one into the hash table. */
1668 t = int_cst_node;
1669 *slot = t;
1670 /* Make a new node for next time round. */
1671 int_cst_node = make_int_cst (1, 1);
1675 else
1677 /* The value either hashes properly or we drop it on the floor
1678 for the gc to take care of. There will not be enough of them
1679 to worry about. */
1681 tree nt = build_new_int_cst (type, cst);
1682 tree *slot = int_cst_hash_table->find_slot (nt, INSERT);
1683 t = *slot;
1684 if (!t)
1686 /* Insert this one into the hash table. */
1687 t = nt;
1688 *slot = t;
1690 else
1691 ggc_free (nt);
1694 return t;
1697 hashval_t
1698 poly_int_cst_hasher::hash (tree t)
1700 inchash::hash hstate;
1702 hstate.add_int (TYPE_UID (TREE_TYPE (t)));
1703 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1704 hstate.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t, i)));
1706 return hstate.end ();
1709 bool
1710 poly_int_cst_hasher::equal (tree x, const compare_type &y)
1712 if (TREE_TYPE (x) != y.first)
1713 return false;
1714 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1715 if (wi::to_wide (POLY_INT_CST_COEFF (x, i)) != y.second->coeffs[i])
1716 return false;
1717 return true;
1720 /* Build a POLY_INT_CST node with type TYPE and with the elements in VALUES.
1721 The elements must also have type TYPE. */
1723 tree
1724 build_poly_int_cst (tree type, const poly_wide_int_ref &values)
1726 unsigned int prec = TYPE_PRECISION (type);
1727 gcc_assert (prec <= values.coeffs[0].get_precision ());
1728 poly_wide_int c = poly_wide_int::from (values, prec, SIGNED);
1730 inchash::hash h;
1731 h.add_int (TYPE_UID (type));
1732 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1733 h.add_wide_int (c.coeffs[i]);
1734 poly_int_cst_hasher::compare_type comp (type, &c);
1735 tree *slot = poly_int_cst_hash_table->find_slot_with_hash (comp, h.end (),
1736 INSERT);
1737 if (*slot == NULL_TREE)
1739 tree coeffs[NUM_POLY_INT_COEFFS];
1740 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1741 coeffs[i] = wide_int_to_tree_1 (type, c.coeffs[i]);
1742 *slot = build_new_poly_int_cst (type, coeffs);
1744 return *slot;
1747 /* Create a constant tree with value VALUE in type TYPE. */
1749 tree
1750 wide_int_to_tree (tree type, const poly_wide_int_ref &value)
1752 if (value.is_constant ())
1753 return wide_int_to_tree_1 (type, value.coeffs[0]);
1754 return build_poly_int_cst (type, value);
1757 void
1758 cache_integer_cst (tree t)
1760 tree type = TREE_TYPE (t);
1761 int ix = -1;
1762 int limit = 0;
1763 int prec = TYPE_PRECISION (type);
1765 gcc_assert (!TREE_OVERFLOW (t));
1767 /* The caching indices here must match those in
1768 wide_int_to_type_1. */
1769 switch (TREE_CODE (type))
1771 case NULLPTR_TYPE:
1772 gcc_assert (integer_zerop (t));
1773 /* Fallthru. */
1775 case POINTER_TYPE:
1776 case REFERENCE_TYPE:
1778 if (integer_zerop (t))
1779 ix = 0;
1780 else if (integer_onep (t))
1781 ix = 2;
1783 if (ix >= 0)
1784 limit = 3;
1786 break;
1788 case BOOLEAN_TYPE:
1789 /* Cache false or true. */
1790 limit = 2;
1791 if (wi::ltu_p (wi::to_wide (t), 2))
1792 ix = TREE_INT_CST_ELT (t, 0);
1793 break;
1795 case INTEGER_TYPE:
1796 case OFFSET_TYPE:
1797 if (TYPE_UNSIGNED (type))
1799 /* Cache 0..N */
1800 limit = param_integer_share_limit;
1802 /* This is a little hokie, but if the prec is smaller than
1803 what is necessary to hold param_integer_share_limit, then the
1804 obvious test will not get the correct answer. */
1805 if (prec < HOST_BITS_PER_WIDE_INT)
1807 if (tree_to_uhwi (t)
1808 < (unsigned HOST_WIDE_INT) param_integer_share_limit)
1809 ix = tree_to_uhwi (t);
1811 else if (wi::ltu_p (wi::to_wide (t), param_integer_share_limit))
1812 ix = tree_to_uhwi (t);
1814 else
1816 /* Cache -1..N */
1817 limit = param_integer_share_limit + 1;
1819 if (integer_minus_onep (t))
1820 ix = 0;
1821 else if (!wi::neg_p (wi::to_wide (t)))
1823 if (prec < HOST_BITS_PER_WIDE_INT)
1825 if (tree_to_shwi (t) < param_integer_share_limit)
1826 ix = tree_to_shwi (t) + 1;
1828 else if (wi::ltu_p (wi::to_wide (t), param_integer_share_limit))
1829 ix = tree_to_shwi (t) + 1;
1832 break;
1834 case ENUMERAL_TYPE:
1835 /* The slot used by TYPE_CACHED_VALUES is used for the enum
1836 members. */
1837 break;
1839 default:
1840 gcc_unreachable ();
1843 if (ix >= 0)
1845 /* Look for it in the type's vector of small shared ints. */
1846 if (!TYPE_CACHED_VALUES_P (type))
1848 TYPE_CACHED_VALUES_P (type) = 1;
1849 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1852 gcc_assert (TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) == NULL_TREE);
1853 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1855 else
1857 /* Use the cache of larger shared ints. */
1858 tree *slot = int_cst_hash_table->find_slot (t, INSERT);
1859 /* If there is already an entry for the number verify it's the
1860 same. */
1861 if (*slot)
1862 gcc_assert (wi::to_wide (tree (*slot)) == wi::to_wide (t));
1863 else
1864 /* Otherwise insert this one into the hash table. */
1865 *slot = t;
1870 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1871 and the rest are zeros. */
1873 tree
1874 build_low_bits_mask (tree type, unsigned bits)
1876 gcc_assert (bits <= TYPE_PRECISION (type));
1878 return wide_int_to_tree (type, wi::mask (bits, false,
1879 TYPE_PRECISION (type)));
1882 /* Checks that X is integer constant that can be expressed in (unsigned)
1883 HOST_WIDE_INT without loss of precision. */
1885 bool
1886 cst_and_fits_in_hwi (const_tree x)
1888 return (TREE_CODE (x) == INTEGER_CST
1889 && (tree_fits_shwi_p (x) || tree_fits_uhwi_p (x)));
1892 /* Build a newly constructed VECTOR_CST with the given values of
1893 (VECTOR_CST_)LOG2_NPATTERNS and (VECTOR_CST_)NELTS_PER_PATTERN. */
1895 tree
1896 make_vector (unsigned log2_npatterns,
1897 unsigned int nelts_per_pattern MEM_STAT_DECL)
1899 gcc_assert (IN_RANGE (nelts_per_pattern, 1, 3));
1900 tree t;
1901 unsigned npatterns = 1 << log2_npatterns;
1902 unsigned encoded_nelts = npatterns * nelts_per_pattern;
1903 unsigned length = (sizeof (struct tree_vector)
1904 + (encoded_nelts - 1) * sizeof (tree));
1906 record_node_allocation_statistics (VECTOR_CST, length);
1908 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1910 TREE_SET_CODE (t, VECTOR_CST);
1911 TREE_CONSTANT (t) = 1;
1912 VECTOR_CST_LOG2_NPATTERNS (t) = log2_npatterns;
1913 VECTOR_CST_NELTS_PER_PATTERN (t) = nelts_per_pattern;
1915 return t;
1918 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1919 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1921 tree
1922 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1924 if (vec_safe_length (v) == 0)
1925 return build_zero_cst (type);
1927 unsigned HOST_WIDE_INT idx, nelts;
1928 tree value;
1930 /* We can't construct a VECTOR_CST for a variable number of elements. */
1931 nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
1932 tree_vector_builder vec (type, nelts, 1);
1933 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1935 if (TREE_CODE (value) == VECTOR_CST)
1937 /* If NELTS is constant then this must be too. */
1938 unsigned int sub_nelts = VECTOR_CST_NELTS (value).to_constant ();
1939 for (unsigned i = 0; i < sub_nelts; ++i)
1940 vec.quick_push (VECTOR_CST_ELT (value, i));
1942 else
1943 vec.quick_push (value);
1945 while (vec.length () < nelts)
1946 vec.quick_push (build_zero_cst (TREE_TYPE (type)));
1948 return vec.build ();
1951 /* Build a vector of type VECTYPE where all the elements are SCs. */
1952 tree
1953 build_vector_from_val (tree vectype, tree sc)
1955 unsigned HOST_WIDE_INT i, nunits;
1957 if (sc == error_mark_node)
1958 return sc;
1960 /* Verify that the vector type is suitable for SC. Note that there
1961 is some inconsistency in the type-system with respect to restrict
1962 qualifications of pointers. Vector types always have a main-variant
1963 element type and the qualification is applied to the vector-type.
1964 So TREE_TYPE (vector-type) does not return a properly qualified
1965 vector element-type. */
1966 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1967 TREE_TYPE (vectype)));
1969 if (CONSTANT_CLASS_P (sc))
1971 tree_vector_builder v (vectype, 1, 1);
1972 v.quick_push (sc);
1973 return v.build ();
1975 else if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits))
1976 return fold_build1 (VEC_DUPLICATE_EXPR, vectype, sc);
1977 else
1979 vec<constructor_elt, va_gc> *v;
1980 vec_alloc (v, nunits);
1981 for (i = 0; i < nunits; ++i)
1982 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1983 return build_constructor (vectype, v);
1987 /* If TYPE is not a vector type, just return SC, otherwise return
1988 build_vector_from_val (TYPE, SC). */
1990 tree
1991 build_uniform_cst (tree type, tree sc)
1993 if (!VECTOR_TYPE_P (type))
1994 return sc;
1996 return build_vector_from_val (type, sc);
1999 /* Build a vector series of type TYPE in which element I has the value
2000 BASE + I * STEP. The result is a constant if BASE and STEP are constant
2001 and a VEC_SERIES_EXPR otherwise. */
2003 tree
2004 build_vec_series (tree type, tree base, tree step)
2006 if (integer_zerop (step))
2007 return build_vector_from_val (type, base);
2008 if (TREE_CODE (base) == INTEGER_CST && TREE_CODE (step) == INTEGER_CST)
2010 tree_vector_builder builder (type, 1, 3);
2011 tree elt1 = wide_int_to_tree (TREE_TYPE (base),
2012 wi::to_wide (base) + wi::to_wide (step));
2013 tree elt2 = wide_int_to_tree (TREE_TYPE (base),
2014 wi::to_wide (elt1) + wi::to_wide (step));
2015 builder.quick_push (base);
2016 builder.quick_push (elt1);
2017 builder.quick_push (elt2);
2018 return builder.build ();
2020 return build2 (VEC_SERIES_EXPR, type, base, step);
2023 /* Return a vector with the same number of units and number of bits
2024 as VEC_TYPE, but in which the elements are a linear series of unsigned
2025 integers { BASE, BASE + STEP, BASE + STEP * 2, ... }. */
2027 tree
2028 build_index_vector (tree vec_type, poly_uint64 base, poly_uint64 step)
2030 tree index_vec_type = vec_type;
2031 tree index_elt_type = TREE_TYPE (vec_type);
2032 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vec_type);
2033 if (!INTEGRAL_TYPE_P (index_elt_type) || !TYPE_UNSIGNED (index_elt_type))
2035 index_elt_type = build_nonstandard_integer_type
2036 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (index_elt_type)), true);
2037 index_vec_type = build_vector_type (index_elt_type, nunits);
2040 tree_vector_builder v (index_vec_type, 1, 3);
2041 for (unsigned int i = 0; i < 3; ++i)
2042 v.quick_push (build_int_cstu (index_elt_type, base + i * step));
2043 return v.build ();
2046 /* Return a VECTOR_CST of type VEC_TYPE in which the first NUM_A
2047 elements are A and the rest are B. */
2049 tree
2050 build_vector_a_then_b (tree vec_type, unsigned int num_a, tree a, tree b)
2052 gcc_assert (known_le (num_a, TYPE_VECTOR_SUBPARTS (vec_type)));
2053 unsigned int count = constant_lower_bound (TYPE_VECTOR_SUBPARTS (vec_type));
2054 /* Optimize the constant case. */
2055 if ((count & 1) == 0 && TYPE_VECTOR_SUBPARTS (vec_type).is_constant ())
2056 count /= 2;
2057 tree_vector_builder builder (vec_type, count, 2);
2058 for (unsigned int i = 0; i < count * 2; ++i)
2059 builder.quick_push (i < num_a ? a : b);
2060 return builder.build ();
2063 /* Something has messed with the elements of CONSTRUCTOR C after it was built;
2064 calculate TREE_CONSTANT and TREE_SIDE_EFFECTS. */
2066 void
2067 recompute_constructor_flags (tree c)
2069 unsigned int i;
2070 tree val;
2071 bool constant_p = true;
2072 bool side_effects_p = false;
2073 vec<constructor_elt, va_gc> *vals = CONSTRUCTOR_ELTS (c);
2075 FOR_EACH_CONSTRUCTOR_VALUE (vals, i, val)
2077 /* Mostly ctors will have elts that don't have side-effects, so
2078 the usual case is to scan all the elements. Hence a single
2079 loop for both const and side effects, rather than one loop
2080 each (with early outs). */
2081 if (!TREE_CONSTANT (val))
2082 constant_p = false;
2083 if (TREE_SIDE_EFFECTS (val))
2084 side_effects_p = true;
2087 TREE_SIDE_EFFECTS (c) = side_effects_p;
2088 TREE_CONSTANT (c) = constant_p;
2091 /* Make sure that TREE_CONSTANT and TREE_SIDE_EFFECTS are correct for
2092 CONSTRUCTOR C. */
2094 void
2095 verify_constructor_flags (tree c)
2097 unsigned int i;
2098 tree val;
2099 bool constant_p = TREE_CONSTANT (c);
2100 bool side_effects_p = TREE_SIDE_EFFECTS (c);
2101 vec<constructor_elt, va_gc> *vals = CONSTRUCTOR_ELTS (c);
2103 FOR_EACH_CONSTRUCTOR_VALUE (vals, i, val)
2105 if (constant_p && !TREE_CONSTANT (val))
2106 internal_error ("non-constant element in constant CONSTRUCTOR");
2107 if (!side_effects_p && TREE_SIDE_EFFECTS (val))
2108 internal_error ("side-effects element in no-side-effects CONSTRUCTOR");
2112 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
2113 are in the vec pointed to by VALS. */
2114 tree
2115 build_constructor (tree type, vec<constructor_elt, va_gc> *vals MEM_STAT_DECL)
2117 tree c = make_node (CONSTRUCTOR PASS_MEM_STAT);
2119 TREE_TYPE (c) = type;
2120 CONSTRUCTOR_ELTS (c) = vals;
2122 recompute_constructor_flags (c);
2124 return c;
2127 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
2128 INDEX and VALUE. */
2129 tree
2130 build_constructor_single (tree type, tree index, tree value)
2132 vec<constructor_elt, va_gc> *v;
2133 constructor_elt elt = {index, value};
2135 vec_alloc (v, 1);
2136 v->quick_push (elt);
2138 return build_constructor (type, v);
2142 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
2143 are in a list pointed to by VALS. */
2144 tree
2145 build_constructor_from_list (tree type, tree vals)
2147 tree t;
2148 vec<constructor_elt, va_gc> *v = NULL;
2150 if (vals)
2152 vec_alloc (v, list_length (vals));
2153 for (t = vals; t; t = TREE_CHAIN (t))
2154 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
2157 return build_constructor (type, v);
2160 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
2161 are in a vector pointed to by VALS. Note that the TREE_PURPOSE
2162 fields in the constructor remain null. */
2164 tree
2165 build_constructor_from_vec (tree type, const vec<tree, va_gc> *vals)
2167 vec<constructor_elt, va_gc> *v = NULL;
2169 for (tree t : *vals)
2170 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, t);
2172 return build_constructor (type, v);
2175 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
2176 of elements, provided as index/value pairs. */
2178 tree
2179 build_constructor_va (tree type, int nelts, ...)
2181 vec<constructor_elt, va_gc> *v = NULL;
2182 va_list p;
2184 va_start (p, nelts);
2185 vec_alloc (v, nelts);
2186 while (nelts--)
2188 tree index = va_arg (p, tree);
2189 tree value = va_arg (p, tree);
2190 CONSTRUCTOR_APPEND_ELT (v, index, value);
2192 va_end (p);
2193 return build_constructor (type, v);
2196 /* Return a node of type TYPE for which TREE_CLOBBER_P is true. */
2198 tree
2199 build_clobber (tree type)
2201 tree clobber = build_constructor (type, NULL);
2202 TREE_THIS_VOLATILE (clobber) = true;
2203 return clobber;
2206 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
2208 tree
2209 build_fixed (tree type, FIXED_VALUE_TYPE f)
2211 tree v;
2212 FIXED_VALUE_TYPE *fp;
2214 v = make_node (FIXED_CST);
2215 fp = ggc_alloc<fixed_value> ();
2216 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
2218 TREE_TYPE (v) = type;
2219 TREE_FIXED_CST_PTR (v) = fp;
2220 return v;
2223 /* Return a new REAL_CST node whose type is TYPE and value is D. */
2225 tree
2226 build_real (tree type, REAL_VALUE_TYPE d)
2228 tree v;
2229 REAL_VALUE_TYPE *dp;
2230 int overflow = 0;
2232 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
2233 Consider doing it via real_convert now. */
2235 v = make_node (REAL_CST);
2236 dp = ggc_alloc<real_value> ();
2237 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
2239 TREE_TYPE (v) = type;
2240 TREE_REAL_CST_PTR (v) = dp;
2241 TREE_OVERFLOW (v) = overflow;
2242 return v;
2245 /* Like build_real, but first truncate D to the type. */
2247 tree
2248 build_real_truncate (tree type, REAL_VALUE_TYPE d)
2250 return build_real (type, real_value_truncate (TYPE_MODE (type), d));
2253 /* Return a new REAL_CST node whose type is TYPE
2254 and whose value is the integer value of the INTEGER_CST node I. */
2256 REAL_VALUE_TYPE
2257 real_value_from_int_cst (const_tree type, const_tree i)
2259 REAL_VALUE_TYPE d;
2261 /* Clear all bits of the real value type so that we can later do
2262 bitwise comparisons to see if two values are the same. */
2263 memset (&d, 0, sizeof d);
2265 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode, wi::to_wide (i),
2266 TYPE_SIGN (TREE_TYPE (i)));
2267 return d;
2270 /* Given a tree representing an integer constant I, return a tree
2271 representing the same value as a floating-point constant of type TYPE. */
2273 tree
2274 build_real_from_int_cst (tree type, const_tree i)
2276 tree v;
2277 int overflow = TREE_OVERFLOW (i);
2279 v = build_real (type, real_value_from_int_cst (type, i));
2281 TREE_OVERFLOW (v) |= overflow;
2282 return v;
2285 /* Return a new REAL_CST node whose type is TYPE
2286 and whose value is the integer value I which has sign SGN. */
2288 tree
2289 build_real_from_wide (tree type, const wide_int_ref &i, signop sgn)
2291 REAL_VALUE_TYPE d;
2293 /* Clear all bits of the real value type so that we can later do
2294 bitwise comparisons to see if two values are the same. */
2295 memset (&d, 0, sizeof d);
2297 real_from_integer (&d, TYPE_MODE (type), i, sgn);
2298 return build_real (type, d);
2301 /* Return a newly constructed STRING_CST node whose value is the LEN
2302 characters at STR when STR is nonnull, or all zeros otherwise.
2303 Note that for a C string literal, LEN should include the trailing NUL.
2304 The TREE_TYPE is not initialized. */
2306 tree
2307 build_string (unsigned len, const char *str /*= NULL */)
2309 /* Do not waste bytes provided by padding of struct tree_string. */
2310 unsigned size = len + offsetof (struct tree_string, str) + 1;
2312 record_node_allocation_statistics (STRING_CST, size);
2314 tree s = (tree) ggc_internal_alloc (size);
2316 memset (s, 0, sizeof (struct tree_typed));
2317 TREE_SET_CODE (s, STRING_CST);
2318 TREE_CONSTANT (s) = 1;
2319 TREE_STRING_LENGTH (s) = len;
2320 if (str)
2321 memcpy (s->string.str, str, len);
2322 else
2323 memset (s->string.str, 0, len);
2324 s->string.str[len] = '\0';
2326 return s;
2329 /* Return a newly constructed COMPLEX_CST node whose value is
2330 specified by the real and imaginary parts REAL and IMAG.
2331 Both REAL and IMAG should be constant nodes. TYPE, if specified,
2332 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
2334 tree
2335 build_complex (tree type, tree real, tree imag)
2337 gcc_assert (CONSTANT_CLASS_P (real));
2338 gcc_assert (CONSTANT_CLASS_P (imag));
2340 tree t = make_node (COMPLEX_CST);
2342 TREE_REALPART (t) = real;
2343 TREE_IMAGPART (t) = imag;
2344 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
2345 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
2346 return t;
2349 /* Build a complex (inf +- 0i), such as for the result of cproj.
2350 TYPE is the complex tree type of the result. If NEG is true, the
2351 imaginary zero is negative. */
2353 tree
2354 build_complex_inf (tree type, bool neg)
2356 REAL_VALUE_TYPE rinf, rzero = dconst0;
2358 real_inf (&rinf);
2359 rzero.sign = neg;
2360 return build_complex (type, build_real (TREE_TYPE (type), rinf),
2361 build_real (TREE_TYPE (type), rzero));
2364 /* Return the constant 1 in type TYPE. If TYPE has several elements, each
2365 element is set to 1. In particular, this is 1 + i for complex types. */
2367 tree
2368 build_each_one_cst (tree type)
2370 if (TREE_CODE (type) == COMPLEX_TYPE)
2372 tree scalar = build_one_cst (TREE_TYPE (type));
2373 return build_complex (type, scalar, scalar);
2375 else
2376 return build_one_cst (type);
2379 /* Return a constant of arithmetic type TYPE which is the
2380 multiplicative identity of the set TYPE. */
2382 tree
2383 build_one_cst (tree type)
2385 switch (TREE_CODE (type))
2387 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2388 case POINTER_TYPE: case REFERENCE_TYPE:
2389 case OFFSET_TYPE:
2390 return build_int_cst (type, 1);
2392 case REAL_TYPE:
2393 return build_real (type, dconst1);
2395 case FIXED_POINT_TYPE:
2396 /* We can only generate 1 for accum types. */
2397 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2398 return build_fixed (type, FCONST1 (TYPE_MODE (type)));
2400 case VECTOR_TYPE:
2402 tree scalar = build_one_cst (TREE_TYPE (type));
2404 return build_vector_from_val (type, scalar);
2407 case COMPLEX_TYPE:
2408 return build_complex (type,
2409 build_one_cst (TREE_TYPE (type)),
2410 build_zero_cst (TREE_TYPE (type)));
2412 default:
2413 gcc_unreachable ();
2417 /* Return an integer of type TYPE containing all 1's in as much precision as
2418 it contains, or a complex or vector whose subparts are such integers. */
2420 tree
2421 build_all_ones_cst (tree type)
2423 if (TREE_CODE (type) == COMPLEX_TYPE)
2425 tree scalar = build_all_ones_cst (TREE_TYPE (type));
2426 return build_complex (type, scalar, scalar);
2428 else
2429 return build_minus_one_cst (type);
2432 /* Return a constant of arithmetic type TYPE which is the
2433 opposite of the multiplicative identity of the set TYPE. */
2435 tree
2436 build_minus_one_cst (tree type)
2438 switch (TREE_CODE (type))
2440 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2441 case POINTER_TYPE: case REFERENCE_TYPE:
2442 case OFFSET_TYPE:
2443 return build_int_cst (type, -1);
2445 case REAL_TYPE:
2446 return build_real (type, dconstm1);
2448 case FIXED_POINT_TYPE:
2449 /* We can only generate 1 for accum types. */
2450 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2451 return build_fixed (type,
2452 fixed_from_double_int (double_int_minus_one,
2453 SCALAR_TYPE_MODE (type)));
2455 case VECTOR_TYPE:
2457 tree scalar = build_minus_one_cst (TREE_TYPE (type));
2459 return build_vector_from_val (type, scalar);
2462 case COMPLEX_TYPE:
2463 return build_complex (type,
2464 build_minus_one_cst (TREE_TYPE (type)),
2465 build_zero_cst (TREE_TYPE (type)));
2467 default:
2468 gcc_unreachable ();
2472 /* Build 0 constant of type TYPE. This is used by constructor folding
2473 and thus the constant should be represented in memory by
2474 zero(es). */
2476 tree
2477 build_zero_cst (tree type)
2479 switch (TREE_CODE (type))
2481 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2482 case POINTER_TYPE: case REFERENCE_TYPE:
2483 case OFFSET_TYPE: case NULLPTR_TYPE:
2484 return build_int_cst (type, 0);
2486 case REAL_TYPE:
2487 return build_real (type, dconst0);
2489 case FIXED_POINT_TYPE:
2490 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
2492 case VECTOR_TYPE:
2494 tree scalar = build_zero_cst (TREE_TYPE (type));
2496 return build_vector_from_val (type, scalar);
2499 case COMPLEX_TYPE:
2501 tree zero = build_zero_cst (TREE_TYPE (type));
2503 return build_complex (type, zero, zero);
2506 default:
2507 if (!AGGREGATE_TYPE_P (type))
2508 return fold_convert (type, integer_zero_node);
2509 return build_constructor (type, NULL);
2514 /* Build a BINFO with LEN language slots. */
2516 tree
2517 make_tree_binfo (unsigned base_binfos MEM_STAT_DECL)
2519 tree t;
2520 size_t length = (offsetof (struct tree_binfo, base_binfos)
2521 + vec<tree, va_gc>::embedded_size (base_binfos));
2523 record_node_allocation_statistics (TREE_BINFO, length);
2525 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
2527 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
2529 TREE_SET_CODE (t, TREE_BINFO);
2531 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
2533 return t;
2536 /* Create a CASE_LABEL_EXPR tree node and return it. */
2538 tree
2539 build_case_label (tree low_value, tree high_value, tree label_decl)
2541 tree t = make_node (CASE_LABEL_EXPR);
2543 TREE_TYPE (t) = void_type_node;
2544 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
2546 CASE_LOW (t) = low_value;
2547 CASE_HIGH (t) = high_value;
2548 CASE_LABEL (t) = label_decl;
2549 CASE_CHAIN (t) = NULL_TREE;
2551 return t;
2554 /* Build a newly constructed INTEGER_CST node. LEN and EXT_LEN are the
2555 values of TREE_INT_CST_NUNITS and TREE_INT_CST_EXT_NUNITS respectively.
2556 The latter determines the length of the HOST_WIDE_INT vector. */
2558 tree
2559 make_int_cst (int len, int ext_len MEM_STAT_DECL)
2561 tree t;
2562 int length = ((ext_len - 1) * sizeof (HOST_WIDE_INT)
2563 + sizeof (struct tree_int_cst));
2565 gcc_assert (len);
2566 record_node_allocation_statistics (INTEGER_CST, length);
2568 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2570 TREE_SET_CODE (t, INTEGER_CST);
2571 TREE_INT_CST_NUNITS (t) = len;
2572 TREE_INT_CST_EXT_NUNITS (t) = ext_len;
2573 /* to_offset can only be applied to trees that are offset_int-sized
2574 or smaller. EXT_LEN is correct if it fits, otherwise the constant
2575 must be exactly the precision of offset_int and so LEN is correct. */
2576 if (ext_len <= OFFSET_INT_ELTS)
2577 TREE_INT_CST_OFFSET_NUNITS (t) = ext_len;
2578 else
2579 TREE_INT_CST_OFFSET_NUNITS (t) = len;
2581 TREE_CONSTANT (t) = 1;
2583 return t;
2586 /* Build a newly constructed TREE_VEC node of length LEN. */
2588 tree
2589 make_tree_vec (int len MEM_STAT_DECL)
2591 tree t;
2592 size_t length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2594 record_node_allocation_statistics (TREE_VEC, length);
2596 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2598 TREE_SET_CODE (t, TREE_VEC);
2599 TREE_VEC_LENGTH (t) = len;
2601 return t;
2604 /* Grow a TREE_VEC node to new length LEN. */
2606 tree
2607 grow_tree_vec (tree v, int len MEM_STAT_DECL)
2609 gcc_assert (TREE_CODE (v) == TREE_VEC);
2611 int oldlen = TREE_VEC_LENGTH (v);
2612 gcc_assert (len > oldlen);
2614 size_t oldlength = (oldlen - 1) * sizeof (tree) + sizeof (struct tree_vec);
2615 size_t length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2617 record_node_allocation_statistics (TREE_VEC, length - oldlength);
2619 v = (tree) ggc_realloc (v, length PASS_MEM_STAT);
2621 TREE_VEC_LENGTH (v) = len;
2623 return v;
2626 /* Return 1 if EXPR is the constant zero, whether it is integral, float or
2627 fixed, and scalar, complex or vector. */
2629 bool
2630 zerop (const_tree expr)
2632 return (integer_zerop (expr)
2633 || real_zerop (expr)
2634 || fixed_zerop (expr));
2637 /* Return 1 if EXPR is the integer constant zero or a complex constant
2638 of zero, or a location wrapper for such a constant. */
2640 bool
2641 integer_zerop (const_tree expr)
2643 STRIP_ANY_LOCATION_WRAPPER (expr);
2645 switch (TREE_CODE (expr))
2647 case INTEGER_CST:
2648 return wi::to_wide (expr) == 0;
2649 case COMPLEX_CST:
2650 return (integer_zerop (TREE_REALPART (expr))
2651 && integer_zerop (TREE_IMAGPART (expr)));
2652 case VECTOR_CST:
2653 return (VECTOR_CST_NPATTERNS (expr) == 1
2654 && VECTOR_CST_DUPLICATE_P (expr)
2655 && integer_zerop (VECTOR_CST_ENCODED_ELT (expr, 0)));
2656 default:
2657 return false;
2661 /* Return 1 if EXPR is the integer constant one or the corresponding
2662 complex constant, or a location wrapper for such a constant. */
2664 bool
2665 integer_onep (const_tree expr)
2667 STRIP_ANY_LOCATION_WRAPPER (expr);
2669 switch (TREE_CODE (expr))
2671 case INTEGER_CST:
2672 return wi::eq_p (wi::to_widest (expr), 1);
2673 case COMPLEX_CST:
2674 return (integer_onep (TREE_REALPART (expr))
2675 && integer_zerop (TREE_IMAGPART (expr)));
2676 case VECTOR_CST:
2677 return (VECTOR_CST_NPATTERNS (expr) == 1
2678 && VECTOR_CST_DUPLICATE_P (expr)
2679 && integer_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
2680 default:
2681 return false;
2685 /* Return 1 if EXPR is the integer constant one. For complex and vector,
2686 return 1 if every piece is the integer constant one.
2687 Also return 1 for location wrappers for such a constant. */
2689 bool
2690 integer_each_onep (const_tree expr)
2692 STRIP_ANY_LOCATION_WRAPPER (expr);
2694 if (TREE_CODE (expr) == COMPLEX_CST)
2695 return (integer_onep (TREE_REALPART (expr))
2696 && integer_onep (TREE_IMAGPART (expr)));
2697 else
2698 return integer_onep (expr);
2701 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
2702 it contains, or a complex or vector whose subparts are such integers,
2703 or a location wrapper for such a constant. */
2705 bool
2706 integer_all_onesp (const_tree expr)
2708 STRIP_ANY_LOCATION_WRAPPER (expr);
2710 if (TREE_CODE (expr) == COMPLEX_CST
2711 && integer_all_onesp (TREE_REALPART (expr))
2712 && integer_all_onesp (TREE_IMAGPART (expr)))
2713 return true;
2715 else if (TREE_CODE (expr) == VECTOR_CST)
2716 return (VECTOR_CST_NPATTERNS (expr) == 1
2717 && VECTOR_CST_DUPLICATE_P (expr)
2718 && integer_all_onesp (VECTOR_CST_ENCODED_ELT (expr, 0)));
2720 else if (TREE_CODE (expr) != INTEGER_CST)
2721 return false;
2723 return (wi::max_value (TYPE_PRECISION (TREE_TYPE (expr)), UNSIGNED)
2724 == wi::to_wide (expr));
2727 /* Return 1 if EXPR is the integer constant minus one, or a location wrapper
2728 for such a constant. */
2730 bool
2731 integer_minus_onep (const_tree expr)
2733 STRIP_ANY_LOCATION_WRAPPER (expr);
2735 if (TREE_CODE (expr) == COMPLEX_CST)
2736 return (integer_all_onesp (TREE_REALPART (expr))
2737 && integer_zerop (TREE_IMAGPART (expr)));
2738 else
2739 return integer_all_onesp (expr);
2742 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
2743 one bit on), or a location wrapper for such a constant. */
2745 bool
2746 integer_pow2p (const_tree expr)
2748 STRIP_ANY_LOCATION_WRAPPER (expr);
2750 if (TREE_CODE (expr) == COMPLEX_CST
2751 && integer_pow2p (TREE_REALPART (expr))
2752 && integer_zerop (TREE_IMAGPART (expr)))
2753 return true;
2755 if (TREE_CODE (expr) != INTEGER_CST)
2756 return false;
2758 return wi::popcount (wi::to_wide (expr)) == 1;
2761 /* Return 1 if EXPR is an integer constant other than zero or a
2762 complex constant other than zero, or a location wrapper for such a
2763 constant. */
2765 bool
2766 integer_nonzerop (const_tree expr)
2768 STRIP_ANY_LOCATION_WRAPPER (expr);
2770 return ((TREE_CODE (expr) == INTEGER_CST
2771 && wi::to_wide (expr) != 0)
2772 || (TREE_CODE (expr) == COMPLEX_CST
2773 && (integer_nonzerop (TREE_REALPART (expr))
2774 || integer_nonzerop (TREE_IMAGPART (expr)))));
2777 /* Return 1 if EXPR is the integer constant one. For vector,
2778 return 1 if every piece is the integer constant minus one
2779 (representing the value TRUE).
2780 Also return 1 for location wrappers for such a constant. */
2782 bool
2783 integer_truep (const_tree expr)
2785 STRIP_ANY_LOCATION_WRAPPER (expr);
2787 if (TREE_CODE (expr) == VECTOR_CST)
2788 return integer_all_onesp (expr);
2789 return integer_onep (expr);
2792 /* Return 1 if EXPR is the fixed-point constant zero, or a location wrapper
2793 for such a constant. */
2795 bool
2796 fixed_zerop (const_tree expr)
2798 STRIP_ANY_LOCATION_WRAPPER (expr);
2800 return (TREE_CODE (expr) == FIXED_CST
2801 && TREE_FIXED_CST (expr).data.is_zero ());
2804 /* Return the power of two represented by a tree node known to be a
2805 power of two. */
2808 tree_log2 (const_tree expr)
2810 if (TREE_CODE (expr) == COMPLEX_CST)
2811 return tree_log2 (TREE_REALPART (expr));
2813 return wi::exact_log2 (wi::to_wide (expr));
2816 /* Similar, but return the largest integer Y such that 2 ** Y is less
2817 than or equal to EXPR. */
2820 tree_floor_log2 (const_tree expr)
2822 if (TREE_CODE (expr) == COMPLEX_CST)
2823 return tree_log2 (TREE_REALPART (expr));
2825 return wi::floor_log2 (wi::to_wide (expr));
2828 /* Return number of known trailing zero bits in EXPR, or, if the value of
2829 EXPR is known to be zero, the precision of it's type. */
2831 unsigned int
2832 tree_ctz (const_tree expr)
2834 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
2835 && !POINTER_TYPE_P (TREE_TYPE (expr)))
2836 return 0;
2838 unsigned int ret1, ret2, prec = TYPE_PRECISION (TREE_TYPE (expr));
2839 switch (TREE_CODE (expr))
2841 case INTEGER_CST:
2842 ret1 = wi::ctz (wi::to_wide (expr));
2843 return MIN (ret1, prec);
2844 case SSA_NAME:
2845 ret1 = wi::ctz (get_nonzero_bits (expr));
2846 return MIN (ret1, prec);
2847 case PLUS_EXPR:
2848 case MINUS_EXPR:
2849 case BIT_IOR_EXPR:
2850 case BIT_XOR_EXPR:
2851 case MIN_EXPR:
2852 case MAX_EXPR:
2853 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2854 if (ret1 == 0)
2855 return ret1;
2856 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2857 return MIN (ret1, ret2);
2858 case POINTER_PLUS_EXPR:
2859 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2860 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2861 /* Second operand is sizetype, which could be in theory
2862 wider than pointer's precision. Make sure we never
2863 return more than prec. */
2864 ret2 = MIN (ret2, prec);
2865 return MIN (ret1, ret2);
2866 case BIT_AND_EXPR:
2867 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2868 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2869 return MAX (ret1, ret2);
2870 case MULT_EXPR:
2871 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2872 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2873 return MIN (ret1 + ret2, prec);
2874 case LSHIFT_EXPR:
2875 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2876 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2877 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2879 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2880 return MIN (ret1 + ret2, prec);
2882 return ret1;
2883 case RSHIFT_EXPR:
2884 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2885 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2887 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2888 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2889 if (ret1 > ret2)
2890 return ret1 - ret2;
2892 return 0;
2893 case TRUNC_DIV_EXPR:
2894 case CEIL_DIV_EXPR:
2895 case FLOOR_DIV_EXPR:
2896 case ROUND_DIV_EXPR:
2897 case EXACT_DIV_EXPR:
2898 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
2899 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) == 1)
2901 int l = tree_log2 (TREE_OPERAND (expr, 1));
2902 if (l >= 0)
2904 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2905 ret2 = l;
2906 if (ret1 > ret2)
2907 return ret1 - ret2;
2910 return 0;
2911 CASE_CONVERT:
2912 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2913 if (ret1 && ret1 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2914 ret1 = prec;
2915 return MIN (ret1, prec);
2916 case SAVE_EXPR:
2917 return tree_ctz (TREE_OPERAND (expr, 0));
2918 case COND_EXPR:
2919 ret1 = tree_ctz (TREE_OPERAND (expr, 1));
2920 if (ret1 == 0)
2921 return 0;
2922 ret2 = tree_ctz (TREE_OPERAND (expr, 2));
2923 return MIN (ret1, ret2);
2924 case COMPOUND_EXPR:
2925 return tree_ctz (TREE_OPERAND (expr, 1));
2926 case ADDR_EXPR:
2927 ret1 = get_pointer_alignment (CONST_CAST_TREE (expr));
2928 if (ret1 > BITS_PER_UNIT)
2930 ret1 = ctz_hwi (ret1 / BITS_PER_UNIT);
2931 return MIN (ret1, prec);
2933 return 0;
2934 default:
2935 return 0;
2939 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
2940 decimal float constants, so don't return 1 for them.
2941 Also return 1 for location wrappers around such a constant. */
2943 bool
2944 real_zerop (const_tree expr)
2946 STRIP_ANY_LOCATION_WRAPPER (expr);
2948 switch (TREE_CODE (expr))
2950 case REAL_CST:
2951 return real_equal (&TREE_REAL_CST (expr), &dconst0)
2952 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2953 case COMPLEX_CST:
2954 return real_zerop (TREE_REALPART (expr))
2955 && real_zerop (TREE_IMAGPART (expr));
2956 case VECTOR_CST:
2958 /* Don't simply check for a duplicate because the predicate
2959 accepts both +0.0 and -0.0. */
2960 unsigned count = vector_cst_encoded_nelts (expr);
2961 for (unsigned int i = 0; i < count; ++i)
2962 if (!real_zerop (VECTOR_CST_ENCODED_ELT (expr, i)))
2963 return false;
2964 return true;
2966 default:
2967 return false;
2971 /* Return 1 if EXPR is the real constant one in real or complex form.
2972 Trailing zeroes matter for decimal float constants, so don't return
2973 1 for them.
2974 Also return 1 for location wrappers around such a constant. */
2976 bool
2977 real_onep (const_tree expr)
2979 STRIP_ANY_LOCATION_WRAPPER (expr);
2981 switch (TREE_CODE (expr))
2983 case REAL_CST:
2984 return real_equal (&TREE_REAL_CST (expr), &dconst1)
2985 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2986 case COMPLEX_CST:
2987 return real_onep (TREE_REALPART (expr))
2988 && real_zerop (TREE_IMAGPART (expr));
2989 case VECTOR_CST:
2990 return (VECTOR_CST_NPATTERNS (expr) == 1
2991 && VECTOR_CST_DUPLICATE_P (expr)
2992 && real_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
2993 default:
2994 return false;
2998 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2999 matter for decimal float constants, so don't return 1 for them.
3000 Also return 1 for location wrappers around such a constant. */
3002 bool
3003 real_minus_onep (const_tree expr)
3005 STRIP_ANY_LOCATION_WRAPPER (expr);
3007 switch (TREE_CODE (expr))
3009 case REAL_CST:
3010 return real_equal (&TREE_REAL_CST (expr), &dconstm1)
3011 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
3012 case COMPLEX_CST:
3013 return real_minus_onep (TREE_REALPART (expr))
3014 && real_zerop (TREE_IMAGPART (expr));
3015 case VECTOR_CST:
3016 return (VECTOR_CST_NPATTERNS (expr) == 1
3017 && VECTOR_CST_DUPLICATE_P (expr)
3018 && real_minus_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
3019 default:
3020 return false;
3024 /* Nonzero if EXP is a constant or a cast of a constant. */
3026 bool
3027 really_constant_p (const_tree exp)
3029 /* This is not quite the same as STRIP_NOPS. It does more. */
3030 while (CONVERT_EXPR_P (exp)
3031 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3032 exp = TREE_OPERAND (exp, 0);
3033 return TREE_CONSTANT (exp);
3036 /* Return true if T holds a polynomial pointer difference, storing it in
3037 *VALUE if so. A true return means that T's precision is no greater
3038 than 64 bits, which is the largest address space we support, so *VALUE
3039 never loses precision. However, the signedness of the result does
3040 not necessarily match the signedness of T: sometimes an unsigned type
3041 like sizetype is used to encode a value that is actually negative. */
3043 bool
3044 ptrdiff_tree_p (const_tree t, poly_int64_pod *value)
3046 if (!t)
3047 return false;
3048 if (TREE_CODE (t) == INTEGER_CST)
3050 if (!cst_and_fits_in_hwi (t))
3051 return false;
3052 *value = int_cst_value (t);
3053 return true;
3055 if (POLY_INT_CST_P (t))
3057 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
3058 if (!cst_and_fits_in_hwi (POLY_INT_CST_COEFF (t, i)))
3059 return false;
3060 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
3061 value->coeffs[i] = int_cst_value (POLY_INT_CST_COEFF (t, i));
3062 return true;
3064 return false;
3067 poly_int64
3068 tree_to_poly_int64 (const_tree t)
3070 gcc_assert (tree_fits_poly_int64_p (t));
3071 if (POLY_INT_CST_P (t))
3072 return poly_int_cst_value (t).force_shwi ();
3073 return TREE_INT_CST_LOW (t);
3076 poly_uint64
3077 tree_to_poly_uint64 (const_tree t)
3079 gcc_assert (tree_fits_poly_uint64_p (t));
3080 if (POLY_INT_CST_P (t))
3081 return poly_int_cst_value (t).force_uhwi ();
3082 return TREE_INT_CST_LOW (t);
3085 /* Return first list element whose TREE_VALUE is ELEM.
3086 Return 0 if ELEM is not in LIST. */
3088 tree
3089 value_member (tree elem, tree list)
3091 while (list)
3093 if (elem == TREE_VALUE (list))
3094 return list;
3095 list = TREE_CHAIN (list);
3097 return NULL_TREE;
3100 /* Return first list element whose TREE_PURPOSE is ELEM.
3101 Return 0 if ELEM is not in LIST. */
3103 tree
3104 purpose_member (const_tree elem, tree list)
3106 while (list)
3108 if (elem == TREE_PURPOSE (list))
3109 return list;
3110 list = TREE_CHAIN (list);
3112 return NULL_TREE;
3115 /* Return true if ELEM is in V. */
3117 bool
3118 vec_member (const_tree elem, vec<tree, va_gc> *v)
3120 unsigned ix;
3121 tree t;
3122 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
3123 if (elem == t)
3124 return true;
3125 return false;
3128 /* Returns element number IDX (zero-origin) of chain CHAIN, or
3129 NULL_TREE. */
3131 tree
3132 chain_index (int idx, tree chain)
3134 for (; chain && idx > 0; --idx)
3135 chain = TREE_CHAIN (chain);
3136 return chain;
3139 /* Return nonzero if ELEM is part of the chain CHAIN. */
3141 bool
3142 chain_member (const_tree elem, const_tree chain)
3144 while (chain)
3146 if (elem == chain)
3147 return true;
3148 chain = DECL_CHAIN (chain);
3151 return false;
3154 /* Return the length of a chain of nodes chained through TREE_CHAIN.
3155 We expect a null pointer to mark the end of the chain.
3156 This is the Lisp primitive `length'. */
3159 list_length (const_tree t)
3161 const_tree p = t;
3162 #ifdef ENABLE_TREE_CHECKING
3163 const_tree q = t;
3164 #endif
3165 int len = 0;
3167 while (p)
3169 p = TREE_CHAIN (p);
3170 #ifdef ENABLE_TREE_CHECKING
3171 if (len % 2)
3172 q = TREE_CHAIN (q);
3173 gcc_assert (p != q);
3174 #endif
3175 len++;
3178 return len;
3181 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
3182 UNION_TYPE TYPE, or NULL_TREE if none. */
3184 tree
3185 first_field (const_tree type)
3187 tree t = TYPE_FIELDS (type);
3188 while (t && TREE_CODE (t) != FIELD_DECL)
3189 t = TREE_CHAIN (t);
3190 return t;
3193 /* Returns the last FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
3194 UNION_TYPE TYPE, or NULL_TREE if none. */
3196 tree
3197 last_field (const_tree type)
3199 tree last = NULL_TREE;
3201 for (tree fld = TYPE_FIELDS (type); fld; fld = TREE_CHAIN (fld))
3203 if (TREE_CODE (fld) != FIELD_DECL)
3204 continue;
3206 last = fld;
3209 return last;
3212 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
3213 by modifying the last node in chain 1 to point to chain 2.
3214 This is the Lisp primitive `nconc'. */
3216 tree
3217 chainon (tree op1, tree op2)
3219 tree t1;
3221 if (!op1)
3222 return op2;
3223 if (!op2)
3224 return op1;
3226 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
3227 continue;
3228 TREE_CHAIN (t1) = op2;
3230 #ifdef ENABLE_TREE_CHECKING
3232 tree t2;
3233 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
3234 gcc_assert (t2 != t1);
3236 #endif
3238 return op1;
3241 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
3243 tree
3244 tree_last (tree chain)
3246 tree next;
3247 if (chain)
3248 while ((next = TREE_CHAIN (chain)))
3249 chain = next;
3250 return chain;
3253 /* Reverse the order of elements in the chain T,
3254 and return the new head of the chain (old last element). */
3256 tree
3257 nreverse (tree t)
3259 tree prev = 0, decl, next;
3260 for (decl = t; decl; decl = next)
3262 /* We shouldn't be using this function to reverse BLOCK chains; we
3263 have blocks_nreverse for that. */
3264 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
3265 next = TREE_CHAIN (decl);
3266 TREE_CHAIN (decl) = prev;
3267 prev = decl;
3269 return prev;
3272 /* Return a newly created TREE_LIST node whose
3273 purpose and value fields are PARM and VALUE. */
3275 tree
3276 build_tree_list (tree parm, tree value MEM_STAT_DECL)
3278 tree t = make_node (TREE_LIST PASS_MEM_STAT);
3279 TREE_PURPOSE (t) = parm;
3280 TREE_VALUE (t) = value;
3281 return t;
3284 /* Build a chain of TREE_LIST nodes from a vector. */
3286 tree
3287 build_tree_list_vec (const vec<tree, va_gc> *vec MEM_STAT_DECL)
3289 tree ret = NULL_TREE;
3290 tree *pp = &ret;
3291 unsigned int i;
3292 tree t;
3293 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
3295 *pp = build_tree_list (NULL, t PASS_MEM_STAT);
3296 pp = &TREE_CHAIN (*pp);
3298 return ret;
3301 /* Return a newly created TREE_LIST node whose
3302 purpose and value fields are PURPOSE and VALUE
3303 and whose TREE_CHAIN is CHAIN. */
3305 tree
3306 tree_cons (tree purpose, tree value, tree chain MEM_STAT_DECL)
3308 tree node;
3310 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
3311 memset (node, 0, sizeof (struct tree_common));
3313 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
3315 TREE_SET_CODE (node, TREE_LIST);
3316 TREE_CHAIN (node) = chain;
3317 TREE_PURPOSE (node) = purpose;
3318 TREE_VALUE (node) = value;
3319 return node;
3322 /* Return the values of the elements of a CONSTRUCTOR as a vector of
3323 trees. */
3325 vec<tree, va_gc> *
3326 ctor_to_vec (tree ctor)
3328 vec<tree, va_gc> *vec;
3329 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
3330 unsigned int ix;
3331 tree val;
3333 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
3334 vec->quick_push (val);
3336 return vec;
3339 /* Return the size nominally occupied by an object of type TYPE
3340 when it resides in memory. The value is measured in units of bytes,
3341 and its data type is that normally used for type sizes
3342 (which is the first type created by make_signed_type or
3343 make_unsigned_type). */
3345 tree
3346 size_in_bytes_loc (location_t loc, const_tree type)
3348 tree t;
3350 if (type == error_mark_node)
3351 return integer_zero_node;
3353 type = TYPE_MAIN_VARIANT (type);
3354 t = TYPE_SIZE_UNIT (type);
3356 if (t == 0)
3358 lang_hooks.types.incomplete_type_error (loc, NULL_TREE, type);
3359 return size_zero_node;
3362 return t;
3365 /* Return the size of TYPE (in bytes) as a wide integer
3366 or return -1 if the size can vary or is larger than an integer. */
3368 HOST_WIDE_INT
3369 int_size_in_bytes (const_tree type)
3371 tree t;
3373 if (type == error_mark_node)
3374 return 0;
3376 type = TYPE_MAIN_VARIANT (type);
3377 t = TYPE_SIZE_UNIT (type);
3379 if (t && tree_fits_uhwi_p (t))
3380 return TREE_INT_CST_LOW (t);
3381 else
3382 return -1;
3385 /* Return the maximum size of TYPE (in bytes) as a wide integer
3386 or return -1 if the size can vary or is larger than an integer. */
3388 HOST_WIDE_INT
3389 max_int_size_in_bytes (const_tree type)
3391 HOST_WIDE_INT size = -1;
3392 tree size_tree;
3394 /* If this is an array type, check for a possible MAX_SIZE attached. */
3396 if (TREE_CODE (type) == ARRAY_TYPE)
3398 size_tree = TYPE_ARRAY_MAX_SIZE (type);
3400 if (size_tree && tree_fits_uhwi_p (size_tree))
3401 size = tree_to_uhwi (size_tree);
3404 /* If we still haven't been able to get a size, see if the language
3405 can compute a maximum size. */
3407 if (size == -1)
3409 size_tree = lang_hooks.types.max_size (type);
3411 if (size_tree && tree_fits_uhwi_p (size_tree))
3412 size = tree_to_uhwi (size_tree);
3415 return size;
3418 /* Return the bit position of FIELD, in bits from the start of the record.
3419 This is a tree of type bitsizetype. */
3421 tree
3422 bit_position (const_tree field)
3424 return bit_from_pos (DECL_FIELD_OFFSET (field),
3425 DECL_FIELD_BIT_OFFSET (field));
3428 /* Return the byte position of FIELD, in bytes from the start of the record.
3429 This is a tree of type sizetype. */
3431 tree
3432 byte_position (const_tree field)
3434 return byte_from_pos (DECL_FIELD_OFFSET (field),
3435 DECL_FIELD_BIT_OFFSET (field));
3438 /* Likewise, but return as an integer. It must be representable in
3439 that way (since it could be a signed value, we don't have the
3440 option of returning -1 like int_size_in_byte can. */
3442 HOST_WIDE_INT
3443 int_byte_position (const_tree field)
3445 return tree_to_shwi (byte_position (field));
3448 /* Return, as a tree node, the number of elements for TYPE (which is an
3449 ARRAY_TYPE) minus one. This counts only elements of the top array. */
3451 tree
3452 array_type_nelts (const_tree type)
3454 tree index_type, min, max;
3456 /* If they did it with unspecified bounds, then we should have already
3457 given an error about it before we got here. */
3458 if (! TYPE_DOMAIN (type))
3459 return error_mark_node;
3461 index_type = TYPE_DOMAIN (type);
3462 min = TYPE_MIN_VALUE (index_type);
3463 max = TYPE_MAX_VALUE (index_type);
3465 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
3466 if (!max)
3467 return error_mark_node;
3469 return (integer_zerop (min)
3470 ? max
3471 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
3474 /* If arg is static -- a reference to an object in static storage -- then
3475 return the object. This is not the same as the C meaning of `static'.
3476 If arg isn't static, return NULL. */
3478 tree
3479 staticp (tree arg)
3481 switch (TREE_CODE (arg))
3483 case FUNCTION_DECL:
3484 /* Nested functions are static, even though taking their address will
3485 involve a trampoline as we unnest the nested function and create
3486 the trampoline on the tree level. */
3487 return arg;
3489 case VAR_DECL:
3490 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3491 && ! DECL_THREAD_LOCAL_P (arg)
3492 && ! DECL_DLLIMPORT_P (arg)
3493 ? arg : NULL);
3495 case CONST_DECL:
3496 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3497 ? arg : NULL);
3499 case CONSTRUCTOR:
3500 return TREE_STATIC (arg) ? arg : NULL;
3502 case LABEL_DECL:
3503 case STRING_CST:
3504 return arg;
3506 case COMPONENT_REF:
3507 /* If the thing being referenced is not a field, then it is
3508 something language specific. */
3509 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
3511 /* If we are referencing a bitfield, we can't evaluate an
3512 ADDR_EXPR at compile time and so it isn't a constant. */
3513 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
3514 return NULL;
3516 return staticp (TREE_OPERAND (arg, 0));
3518 case BIT_FIELD_REF:
3519 return NULL;
3521 case INDIRECT_REF:
3522 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
3524 case ARRAY_REF:
3525 case ARRAY_RANGE_REF:
3526 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
3527 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
3528 return staticp (TREE_OPERAND (arg, 0));
3529 else
3530 return NULL;
3532 case COMPOUND_LITERAL_EXPR:
3533 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
3535 default:
3536 return NULL;
3543 /* Return whether OP is a DECL whose address is function-invariant. */
3545 bool
3546 decl_address_invariant_p (const_tree op)
3548 /* The conditions below are slightly less strict than the one in
3549 staticp. */
3551 switch (TREE_CODE (op))
3553 case PARM_DECL:
3554 case RESULT_DECL:
3555 case LABEL_DECL:
3556 case FUNCTION_DECL:
3557 return true;
3559 case VAR_DECL:
3560 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3561 || DECL_THREAD_LOCAL_P (op)
3562 || DECL_CONTEXT (op) == current_function_decl
3563 || decl_function_context (op) == current_function_decl)
3564 return true;
3565 break;
3567 case CONST_DECL:
3568 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3569 || decl_function_context (op) == current_function_decl)
3570 return true;
3571 break;
3573 default:
3574 break;
3577 return false;
3580 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
3582 bool
3583 decl_address_ip_invariant_p (const_tree op)
3585 /* The conditions below are slightly less strict than the one in
3586 staticp. */
3588 switch (TREE_CODE (op))
3590 case LABEL_DECL:
3591 case FUNCTION_DECL:
3592 case STRING_CST:
3593 return true;
3595 case VAR_DECL:
3596 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
3597 && !DECL_DLLIMPORT_P (op))
3598 || DECL_THREAD_LOCAL_P (op))
3599 return true;
3600 break;
3602 case CONST_DECL:
3603 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
3604 return true;
3605 break;
3607 default:
3608 break;
3611 return false;
3615 /* Return true if T is function-invariant (internal function, does
3616 not handle arithmetic; that's handled in skip_simple_arithmetic and
3617 tree_invariant_p). */
3619 static bool
3620 tree_invariant_p_1 (tree t)
3622 tree op;
3624 if (TREE_CONSTANT (t)
3625 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
3626 return true;
3628 switch (TREE_CODE (t))
3630 case SAVE_EXPR:
3631 return true;
3633 case ADDR_EXPR:
3634 op = TREE_OPERAND (t, 0);
3635 while (handled_component_p (op))
3637 switch (TREE_CODE (op))
3639 case ARRAY_REF:
3640 case ARRAY_RANGE_REF:
3641 if (!tree_invariant_p (TREE_OPERAND (op, 1))
3642 || TREE_OPERAND (op, 2) != NULL_TREE
3643 || TREE_OPERAND (op, 3) != NULL_TREE)
3644 return false;
3645 break;
3647 case COMPONENT_REF:
3648 if (TREE_OPERAND (op, 2) != NULL_TREE)
3649 return false;
3650 break;
3652 default:;
3654 op = TREE_OPERAND (op, 0);
3657 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
3659 default:
3660 break;
3663 return false;
3666 /* Return true if T is function-invariant. */
3668 bool
3669 tree_invariant_p (tree t)
3671 tree inner = skip_simple_arithmetic (t);
3672 return tree_invariant_p_1 (inner);
3675 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
3676 Do this to any expression which may be used in more than one place,
3677 but must be evaluated only once.
3679 Normally, expand_expr would reevaluate the expression each time.
3680 Calling save_expr produces something that is evaluated and recorded
3681 the first time expand_expr is called on it. Subsequent calls to
3682 expand_expr just reuse the recorded value.
3684 The call to expand_expr that generates code that actually computes
3685 the value is the first call *at compile time*. Subsequent calls
3686 *at compile time* generate code to use the saved value.
3687 This produces correct result provided that *at run time* control
3688 always flows through the insns made by the first expand_expr
3689 before reaching the other places where the save_expr was evaluated.
3690 You, the caller of save_expr, must make sure this is so.
3692 Constants, and certain read-only nodes, are returned with no
3693 SAVE_EXPR because that is safe. Expressions containing placeholders
3694 are not touched; see tree.def for an explanation of what these
3695 are used for. */
3697 tree
3698 save_expr (tree expr)
3700 tree inner;
3702 /* If the tree evaluates to a constant, then we don't want to hide that
3703 fact (i.e. this allows further folding, and direct checks for constants).
3704 However, a read-only object that has side effects cannot be bypassed.
3705 Since it is no problem to reevaluate literals, we just return the
3706 literal node. */
3707 inner = skip_simple_arithmetic (expr);
3708 if (TREE_CODE (inner) == ERROR_MARK)
3709 return inner;
3711 if (tree_invariant_p_1 (inner))
3712 return expr;
3714 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
3715 it means that the size or offset of some field of an object depends on
3716 the value within another field.
3718 Note that it must not be the case that EXPR contains both a PLACEHOLDER_EXPR
3719 and some variable since it would then need to be both evaluated once and
3720 evaluated more than once. Front-ends must assure this case cannot
3721 happen by surrounding any such subexpressions in their own SAVE_EXPR
3722 and forcing evaluation at the proper time. */
3723 if (contains_placeholder_p (inner))
3724 return expr;
3726 expr = build1_loc (EXPR_LOCATION (expr), SAVE_EXPR, TREE_TYPE (expr), expr);
3728 /* This expression might be placed ahead of a jump to ensure that the
3729 value was computed on both sides of the jump. So make sure it isn't
3730 eliminated as dead. */
3731 TREE_SIDE_EFFECTS (expr) = 1;
3732 return expr;
3735 /* Look inside EXPR into any simple arithmetic operations. Return the
3736 outermost non-arithmetic or non-invariant node. */
3738 tree
3739 skip_simple_arithmetic (tree expr)
3741 /* We don't care about whether this can be used as an lvalue in this
3742 context. */
3743 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3744 expr = TREE_OPERAND (expr, 0);
3746 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
3747 a constant, it will be more efficient to not make another SAVE_EXPR since
3748 it will allow better simplification and GCSE will be able to merge the
3749 computations if they actually occur. */
3750 while (true)
3752 if (UNARY_CLASS_P (expr))
3753 expr = TREE_OPERAND (expr, 0);
3754 else if (BINARY_CLASS_P (expr))
3756 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
3757 expr = TREE_OPERAND (expr, 0);
3758 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
3759 expr = TREE_OPERAND (expr, 1);
3760 else
3761 break;
3763 else
3764 break;
3767 return expr;
3770 /* Look inside EXPR into simple arithmetic operations involving constants.
3771 Return the outermost non-arithmetic or non-constant node. */
3773 tree
3774 skip_simple_constant_arithmetic (tree expr)
3776 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3777 expr = TREE_OPERAND (expr, 0);
3779 while (true)
3781 if (UNARY_CLASS_P (expr))
3782 expr = TREE_OPERAND (expr, 0);
3783 else if (BINARY_CLASS_P (expr))
3785 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3786 expr = TREE_OPERAND (expr, 0);
3787 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3788 expr = TREE_OPERAND (expr, 1);
3789 else
3790 break;
3792 else
3793 break;
3796 return expr;
3799 /* Return which tree structure is used by T. */
3801 enum tree_node_structure_enum
3802 tree_node_structure (const_tree t)
3804 const enum tree_code code = TREE_CODE (t);
3805 return tree_node_structure_for_code (code);
3808 /* Set various status flags when building a CALL_EXPR object T. */
3810 static void
3811 process_call_operands (tree t)
3813 bool side_effects = TREE_SIDE_EFFECTS (t);
3814 bool read_only = false;
3815 int i = call_expr_flags (t);
3817 /* Calls have side-effects, except those to const or pure functions. */
3818 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
3819 side_effects = true;
3820 /* Propagate TREE_READONLY of arguments for const functions. */
3821 if (i & ECF_CONST)
3822 read_only = true;
3824 if (!side_effects || read_only)
3825 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
3827 tree op = TREE_OPERAND (t, i);
3828 if (op && TREE_SIDE_EFFECTS (op))
3829 side_effects = true;
3830 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
3831 read_only = false;
3834 TREE_SIDE_EFFECTS (t) = side_effects;
3835 TREE_READONLY (t) = read_only;
3838 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
3839 size or offset that depends on a field within a record. */
3841 bool
3842 contains_placeholder_p (const_tree exp)
3844 enum tree_code code;
3846 if (!exp)
3847 return 0;
3849 code = TREE_CODE (exp);
3850 if (code == PLACEHOLDER_EXPR)
3851 return 1;
3853 switch (TREE_CODE_CLASS (code))
3855 case tcc_reference:
3856 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
3857 position computations since they will be converted into a
3858 WITH_RECORD_EXPR involving the reference, which will assume
3859 here will be valid. */
3860 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3862 case tcc_exceptional:
3863 if (code == TREE_LIST)
3864 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
3865 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
3866 break;
3868 case tcc_unary:
3869 case tcc_binary:
3870 case tcc_comparison:
3871 case tcc_expression:
3872 switch (code)
3874 case COMPOUND_EXPR:
3875 /* Ignoring the first operand isn't quite right, but works best. */
3876 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
3878 case COND_EXPR:
3879 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3880 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
3881 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
3883 case SAVE_EXPR:
3884 /* The save_expr function never wraps anything containing
3885 a PLACEHOLDER_EXPR. */
3886 return 0;
3888 default:
3889 break;
3892 switch (TREE_CODE_LENGTH (code))
3894 case 1:
3895 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3896 case 2:
3897 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3898 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
3899 default:
3900 return 0;
3903 case tcc_vl_exp:
3904 switch (code)
3906 case CALL_EXPR:
3908 const_tree arg;
3909 const_call_expr_arg_iterator iter;
3910 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
3911 if (CONTAINS_PLACEHOLDER_P (arg))
3912 return 1;
3913 return 0;
3915 default:
3916 return 0;
3919 default:
3920 return 0;
3922 return 0;
3925 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3926 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3927 field positions. */
3929 static bool
3930 type_contains_placeholder_1 (const_tree type)
3932 /* If the size contains a placeholder or the parent type (component type in
3933 the case of arrays) type involves a placeholder, this type does. */
3934 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3935 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3936 || (!POINTER_TYPE_P (type)
3937 && TREE_TYPE (type)
3938 && type_contains_placeholder_p (TREE_TYPE (type))))
3939 return true;
3941 /* Now do type-specific checks. Note that the last part of the check above
3942 greatly limits what we have to do below. */
3943 switch (TREE_CODE (type))
3945 case VOID_TYPE:
3946 case COMPLEX_TYPE:
3947 case ENUMERAL_TYPE:
3948 case BOOLEAN_TYPE:
3949 case POINTER_TYPE:
3950 case OFFSET_TYPE:
3951 case REFERENCE_TYPE:
3952 case METHOD_TYPE:
3953 case FUNCTION_TYPE:
3954 case VECTOR_TYPE:
3955 case NULLPTR_TYPE:
3956 return false;
3958 case INTEGER_TYPE:
3959 case REAL_TYPE:
3960 case FIXED_POINT_TYPE:
3961 /* Here we just check the bounds. */
3962 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3963 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3965 case ARRAY_TYPE:
3966 /* We have already checked the component type above, so just check
3967 the domain type. Flexible array members have a null domain. */
3968 return TYPE_DOMAIN (type) ?
3969 type_contains_placeholder_p (TYPE_DOMAIN (type)) : false;
3971 case RECORD_TYPE:
3972 case UNION_TYPE:
3973 case QUAL_UNION_TYPE:
3975 tree field;
3977 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3978 if (TREE_CODE (field) == FIELD_DECL
3979 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3980 || (TREE_CODE (type) == QUAL_UNION_TYPE
3981 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3982 || type_contains_placeholder_p (TREE_TYPE (field))))
3983 return true;
3985 return false;
3988 default:
3989 gcc_unreachable ();
3993 /* Wrapper around above function used to cache its result. */
3995 bool
3996 type_contains_placeholder_p (tree type)
3998 bool result;
4000 /* If the contains_placeholder_bits field has been initialized,
4001 then we know the answer. */
4002 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
4003 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
4005 /* Indicate that we've seen this type node, and the answer is false.
4006 This is what we want to return if we run into recursion via fields. */
4007 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
4009 /* Compute the real value. */
4010 result = type_contains_placeholder_1 (type);
4012 /* Store the real value. */
4013 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
4015 return result;
4018 /* Push tree EXP onto vector QUEUE if it is not already present. */
4020 static void
4021 push_without_duplicates (tree exp, vec<tree> *queue)
4023 unsigned int i;
4024 tree iter;
4026 FOR_EACH_VEC_ELT (*queue, i, iter)
4027 if (simple_cst_equal (iter, exp) == 1)
4028 break;
4030 if (!iter)
4031 queue->safe_push (exp);
4034 /* Given a tree EXP, find all occurrences of references to fields
4035 in a PLACEHOLDER_EXPR and place them in vector REFS without
4036 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
4037 we assume here that EXP contains only arithmetic expressions
4038 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
4039 argument list. */
4041 void
4042 find_placeholder_in_expr (tree exp, vec<tree> *refs)
4044 enum tree_code code = TREE_CODE (exp);
4045 tree inner;
4046 int i;
4048 /* We handle TREE_LIST and COMPONENT_REF separately. */
4049 if (code == TREE_LIST)
4051 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
4052 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
4054 else if (code == COMPONENT_REF)
4056 for (inner = TREE_OPERAND (exp, 0);
4057 REFERENCE_CLASS_P (inner);
4058 inner = TREE_OPERAND (inner, 0))
4061 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
4062 push_without_duplicates (exp, refs);
4063 else
4064 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
4066 else
4067 switch (TREE_CODE_CLASS (code))
4069 case tcc_constant:
4070 break;
4072 case tcc_declaration:
4073 /* Variables allocated to static storage can stay. */
4074 if (!TREE_STATIC (exp))
4075 push_without_duplicates (exp, refs);
4076 break;
4078 case tcc_expression:
4079 /* This is the pattern built in ada/make_aligning_type. */
4080 if (code == ADDR_EXPR
4081 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
4083 push_without_duplicates (exp, refs);
4084 break;
4087 /* Fall through. */
4089 case tcc_exceptional:
4090 case tcc_unary:
4091 case tcc_binary:
4092 case tcc_comparison:
4093 case tcc_reference:
4094 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
4095 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
4096 break;
4098 case tcc_vl_exp:
4099 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4100 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
4101 break;
4103 default:
4104 gcc_unreachable ();
4108 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
4109 return a tree with all occurrences of references to F in a
4110 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
4111 CONST_DECLs. Note that we assume here that EXP contains only
4112 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
4113 occurring only in their argument list. */
4115 tree
4116 substitute_in_expr (tree exp, tree f, tree r)
4118 enum tree_code code = TREE_CODE (exp);
4119 tree op0, op1, op2, op3;
4120 tree new_tree;
4122 /* We handle TREE_LIST and COMPONENT_REF separately. */
4123 if (code == TREE_LIST)
4125 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
4126 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
4127 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
4128 return exp;
4130 return tree_cons (TREE_PURPOSE (exp), op1, op0);
4132 else if (code == COMPONENT_REF)
4134 tree inner;
4136 /* If this expression is getting a value from a PLACEHOLDER_EXPR
4137 and it is the right field, replace it with R. */
4138 for (inner = TREE_OPERAND (exp, 0);
4139 REFERENCE_CLASS_P (inner);
4140 inner = TREE_OPERAND (inner, 0))
4143 /* The field. */
4144 op1 = TREE_OPERAND (exp, 1);
4146 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
4147 return r;
4149 /* If this expression hasn't been completed let, leave it alone. */
4150 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
4151 return exp;
4153 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4154 if (op0 == TREE_OPERAND (exp, 0))
4155 return exp;
4157 new_tree
4158 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
4160 else
4161 switch (TREE_CODE_CLASS (code))
4163 case tcc_constant:
4164 return exp;
4166 case tcc_declaration:
4167 if (exp == f)
4168 return r;
4169 else
4170 return exp;
4172 case tcc_expression:
4173 if (exp == f)
4174 return r;
4176 /* Fall through. */
4178 case tcc_exceptional:
4179 case tcc_unary:
4180 case tcc_binary:
4181 case tcc_comparison:
4182 case tcc_reference:
4183 switch (TREE_CODE_LENGTH (code))
4185 case 0:
4186 return exp;
4188 case 1:
4189 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4190 if (op0 == TREE_OPERAND (exp, 0))
4191 return exp;
4193 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
4194 break;
4196 case 2:
4197 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4198 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4200 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
4201 return exp;
4203 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
4204 break;
4206 case 3:
4207 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4208 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4209 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
4211 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4212 && op2 == TREE_OPERAND (exp, 2))
4213 return exp;
4215 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
4216 break;
4218 case 4:
4219 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4220 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4221 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
4222 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
4224 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4225 && op2 == TREE_OPERAND (exp, 2)
4226 && op3 == TREE_OPERAND (exp, 3))
4227 return exp;
4229 new_tree
4230 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
4231 break;
4233 default:
4234 gcc_unreachable ();
4236 break;
4238 case tcc_vl_exp:
4240 int i;
4242 new_tree = NULL_TREE;
4244 /* If we are trying to replace F with a constant or with another
4245 instance of one of the arguments of the call, inline back
4246 functions which do nothing else than computing a value from
4247 the arguments they are passed. This makes it possible to
4248 fold partially or entirely the replacement expression. */
4249 if (code == CALL_EXPR)
4251 bool maybe_inline = false;
4252 if (CONSTANT_CLASS_P (r))
4253 maybe_inline = true;
4254 else
4255 for (i = 3; i < TREE_OPERAND_LENGTH (exp); i++)
4256 if (operand_equal_p (TREE_OPERAND (exp, i), r, 0))
4258 maybe_inline = true;
4259 break;
4261 if (maybe_inline)
4263 tree t = maybe_inline_call_in_expr (exp);
4264 if (t)
4265 return SUBSTITUTE_IN_EXPR (t, f, r);
4269 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4271 tree op = TREE_OPERAND (exp, i);
4272 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
4273 if (new_op != op)
4275 if (!new_tree)
4276 new_tree = copy_node (exp);
4277 TREE_OPERAND (new_tree, i) = new_op;
4281 if (new_tree)
4283 new_tree = fold (new_tree);
4284 if (TREE_CODE (new_tree) == CALL_EXPR)
4285 process_call_operands (new_tree);
4287 else
4288 return exp;
4290 break;
4292 default:
4293 gcc_unreachable ();
4296 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4298 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4299 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4301 return new_tree;
4304 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
4305 for it within OBJ, a tree that is an object or a chain of references. */
4307 tree
4308 substitute_placeholder_in_expr (tree exp, tree obj)
4310 enum tree_code code = TREE_CODE (exp);
4311 tree op0, op1, op2, op3;
4312 tree new_tree;
4314 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
4315 in the chain of OBJ. */
4316 if (code == PLACEHOLDER_EXPR)
4318 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
4319 tree elt;
4321 for (elt = obj; elt != 0;
4322 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
4323 || TREE_CODE (elt) == COND_EXPR)
4324 ? TREE_OPERAND (elt, 1)
4325 : (REFERENCE_CLASS_P (elt)
4326 || UNARY_CLASS_P (elt)
4327 || BINARY_CLASS_P (elt)
4328 || VL_EXP_CLASS_P (elt)
4329 || EXPRESSION_CLASS_P (elt))
4330 ? TREE_OPERAND (elt, 0) : 0))
4331 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
4332 return elt;
4334 for (elt = obj; elt != 0;
4335 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
4336 || TREE_CODE (elt) == COND_EXPR)
4337 ? TREE_OPERAND (elt, 1)
4338 : (REFERENCE_CLASS_P (elt)
4339 || UNARY_CLASS_P (elt)
4340 || BINARY_CLASS_P (elt)
4341 || VL_EXP_CLASS_P (elt)
4342 || EXPRESSION_CLASS_P (elt))
4343 ? TREE_OPERAND (elt, 0) : 0))
4344 if (POINTER_TYPE_P (TREE_TYPE (elt))
4345 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
4346 == need_type))
4347 return fold_build1 (INDIRECT_REF, need_type, elt);
4349 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
4350 survives until RTL generation, there will be an error. */
4351 return exp;
4354 /* TREE_LIST is special because we need to look at TREE_VALUE
4355 and TREE_CHAIN, not TREE_OPERANDS. */
4356 else if (code == TREE_LIST)
4358 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
4359 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
4360 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
4361 return exp;
4363 return tree_cons (TREE_PURPOSE (exp), op1, op0);
4365 else
4366 switch (TREE_CODE_CLASS (code))
4368 case tcc_constant:
4369 case tcc_declaration:
4370 return exp;
4372 case tcc_exceptional:
4373 case tcc_unary:
4374 case tcc_binary:
4375 case tcc_comparison:
4376 case tcc_expression:
4377 case tcc_reference:
4378 case tcc_statement:
4379 switch (TREE_CODE_LENGTH (code))
4381 case 0:
4382 return exp;
4384 case 1:
4385 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4386 if (op0 == TREE_OPERAND (exp, 0))
4387 return exp;
4389 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
4390 break;
4392 case 2:
4393 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4394 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4396 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
4397 return exp;
4399 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
4400 break;
4402 case 3:
4403 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4404 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4405 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
4407 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4408 && op2 == TREE_OPERAND (exp, 2))
4409 return exp;
4411 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
4412 break;
4414 case 4:
4415 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4416 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4417 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
4418 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
4420 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4421 && op2 == TREE_OPERAND (exp, 2)
4422 && op3 == TREE_OPERAND (exp, 3))
4423 return exp;
4425 new_tree
4426 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
4427 break;
4429 default:
4430 gcc_unreachable ();
4432 break;
4434 case tcc_vl_exp:
4436 int i;
4438 new_tree = NULL_TREE;
4440 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4442 tree op = TREE_OPERAND (exp, i);
4443 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
4444 if (new_op != op)
4446 if (!new_tree)
4447 new_tree = copy_node (exp);
4448 TREE_OPERAND (new_tree, i) = new_op;
4452 if (new_tree)
4454 new_tree = fold (new_tree);
4455 if (TREE_CODE (new_tree) == CALL_EXPR)
4456 process_call_operands (new_tree);
4458 else
4459 return exp;
4461 break;
4463 default:
4464 gcc_unreachable ();
4467 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4469 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4470 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4472 return new_tree;
4476 /* Subroutine of stabilize_reference; this is called for subtrees of
4477 references. Any expression with side-effects must be put in a SAVE_EXPR
4478 to ensure that it is only evaluated once.
4480 We don't put SAVE_EXPR nodes around everything, because assigning very
4481 simple expressions to temporaries causes us to miss good opportunities
4482 for optimizations. Among other things, the opportunity to fold in the
4483 addition of a constant into an addressing mode often gets lost, e.g.
4484 "y[i+1] += x;". In general, we take the approach that we should not make
4485 an assignment unless we are forced into it - i.e., that any non-side effect
4486 operator should be allowed, and that cse should take care of coalescing
4487 multiple utterances of the same expression should that prove fruitful. */
4489 static tree
4490 stabilize_reference_1 (tree e)
4492 tree result;
4493 enum tree_code code = TREE_CODE (e);
4495 /* We cannot ignore const expressions because it might be a reference
4496 to a const array but whose index contains side-effects. But we can
4497 ignore things that are actual constant or that already have been
4498 handled by this function. */
4500 if (tree_invariant_p (e))
4501 return e;
4503 switch (TREE_CODE_CLASS (code))
4505 case tcc_exceptional:
4506 /* Always wrap STATEMENT_LIST into SAVE_EXPR, even if it doesn't
4507 have side-effects. */
4508 if (code == STATEMENT_LIST)
4509 return save_expr (e);
4510 /* FALLTHRU */
4511 case tcc_type:
4512 case tcc_declaration:
4513 case tcc_comparison:
4514 case tcc_statement:
4515 case tcc_expression:
4516 case tcc_reference:
4517 case tcc_vl_exp:
4518 /* If the expression has side-effects, then encase it in a SAVE_EXPR
4519 so that it will only be evaluated once. */
4520 /* The reference (r) and comparison (<) classes could be handled as
4521 below, but it is generally faster to only evaluate them once. */
4522 if (TREE_SIDE_EFFECTS (e))
4523 return save_expr (e);
4524 return e;
4526 case tcc_constant:
4527 /* Constants need no processing. In fact, we should never reach
4528 here. */
4529 return e;
4531 case tcc_binary:
4532 /* Division is slow and tends to be compiled with jumps,
4533 especially the division by powers of 2 that is often
4534 found inside of an array reference. So do it just once. */
4535 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
4536 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
4537 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
4538 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
4539 return save_expr (e);
4540 /* Recursively stabilize each operand. */
4541 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
4542 stabilize_reference_1 (TREE_OPERAND (e, 1)));
4543 break;
4545 case tcc_unary:
4546 /* Recursively stabilize each operand. */
4547 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
4548 break;
4550 default:
4551 gcc_unreachable ();
4554 TREE_TYPE (result) = TREE_TYPE (e);
4555 TREE_READONLY (result) = TREE_READONLY (e);
4556 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
4557 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
4559 return result;
4562 /* Stabilize a reference so that we can use it any number of times
4563 without causing its operands to be evaluated more than once.
4564 Returns the stabilized reference. This works by means of save_expr,
4565 so see the caveats in the comments about save_expr.
4567 Also allows conversion expressions whose operands are references.
4568 Any other kind of expression is returned unchanged. */
4570 tree
4571 stabilize_reference (tree ref)
4573 tree result;
4574 enum tree_code code = TREE_CODE (ref);
4576 switch (code)
4578 case VAR_DECL:
4579 case PARM_DECL:
4580 case RESULT_DECL:
4581 /* No action is needed in this case. */
4582 return ref;
4584 CASE_CONVERT:
4585 case FLOAT_EXPR:
4586 case FIX_TRUNC_EXPR:
4587 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
4588 break;
4590 case INDIRECT_REF:
4591 result = build_nt (INDIRECT_REF,
4592 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
4593 break;
4595 case COMPONENT_REF:
4596 result = build_nt (COMPONENT_REF,
4597 stabilize_reference (TREE_OPERAND (ref, 0)),
4598 TREE_OPERAND (ref, 1), NULL_TREE);
4599 break;
4601 case BIT_FIELD_REF:
4602 result = build_nt (BIT_FIELD_REF,
4603 stabilize_reference (TREE_OPERAND (ref, 0)),
4604 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
4605 REF_REVERSE_STORAGE_ORDER (result) = REF_REVERSE_STORAGE_ORDER (ref);
4606 break;
4608 case ARRAY_REF:
4609 result = build_nt (ARRAY_REF,
4610 stabilize_reference (TREE_OPERAND (ref, 0)),
4611 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4612 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4613 break;
4615 case ARRAY_RANGE_REF:
4616 result = build_nt (ARRAY_RANGE_REF,
4617 stabilize_reference (TREE_OPERAND (ref, 0)),
4618 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4619 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4620 break;
4622 case COMPOUND_EXPR:
4623 /* We cannot wrap the first expression in a SAVE_EXPR, as then
4624 it wouldn't be ignored. This matters when dealing with
4625 volatiles. */
4626 return stabilize_reference_1 (ref);
4628 /* If arg isn't a kind of lvalue we recognize, make no change.
4629 Caller should recognize the error for an invalid lvalue. */
4630 default:
4631 return ref;
4633 case ERROR_MARK:
4634 return error_mark_node;
4637 TREE_TYPE (result) = TREE_TYPE (ref);
4638 TREE_READONLY (result) = TREE_READONLY (ref);
4639 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
4640 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
4642 return result;
4645 /* Low-level constructors for expressions. */
4647 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
4648 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
4650 void
4651 recompute_tree_invariant_for_addr_expr (tree t)
4653 tree node;
4654 bool tc = true, se = false;
4656 gcc_assert (TREE_CODE (t) == ADDR_EXPR);
4658 /* We started out assuming this address is both invariant and constant, but
4659 does not have side effects. Now go down any handled components and see if
4660 any of them involve offsets that are either non-constant or non-invariant.
4661 Also check for side-effects.
4663 ??? Note that this code makes no attempt to deal with the case where
4664 taking the address of something causes a copy due to misalignment. */
4666 #define UPDATE_FLAGS(NODE) \
4667 do { tree _node = (NODE); \
4668 if (_node && !TREE_CONSTANT (_node)) tc = false; \
4669 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
4671 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
4672 node = TREE_OPERAND (node, 0))
4674 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
4675 array reference (probably made temporarily by the G++ front end),
4676 so ignore all the operands. */
4677 if ((TREE_CODE (node) == ARRAY_REF
4678 || TREE_CODE (node) == ARRAY_RANGE_REF)
4679 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
4681 UPDATE_FLAGS (TREE_OPERAND (node, 1));
4682 if (TREE_OPERAND (node, 2))
4683 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4684 if (TREE_OPERAND (node, 3))
4685 UPDATE_FLAGS (TREE_OPERAND (node, 3));
4687 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
4688 FIELD_DECL, apparently. The G++ front end can put something else
4689 there, at least temporarily. */
4690 else if (TREE_CODE (node) == COMPONENT_REF
4691 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
4693 if (TREE_OPERAND (node, 2))
4694 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4698 node = lang_hooks.expr_to_decl (node, &tc, &se);
4700 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4701 the address, since &(*a)->b is a form of addition. If it's a constant, the
4702 address is constant too. If it's a decl, its address is constant if the
4703 decl is static. Everything else is not constant and, furthermore,
4704 taking the address of a volatile variable is not volatile. */
4705 if (TREE_CODE (node) == INDIRECT_REF
4706 || TREE_CODE (node) == MEM_REF)
4707 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4708 else if (CONSTANT_CLASS_P (node))
4710 else if (DECL_P (node))
4711 tc &= (staticp (node) != NULL_TREE);
4712 else
4714 tc = false;
4715 se |= TREE_SIDE_EFFECTS (node);
4719 TREE_CONSTANT (t) = tc;
4720 TREE_SIDE_EFFECTS (t) = se;
4721 #undef UPDATE_FLAGS
4724 /* Build an expression of code CODE, data type TYPE, and operands as
4725 specified. Expressions and reference nodes can be created this way.
4726 Constants, decls, types and misc nodes cannot be.
4728 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4729 enough for all extant tree codes. */
4731 tree
4732 build0 (enum tree_code code, tree tt MEM_STAT_DECL)
4734 tree t;
4736 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4738 t = make_node (code PASS_MEM_STAT);
4739 TREE_TYPE (t) = tt;
4741 return t;
4744 tree
4745 build1 (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4747 int length = sizeof (struct tree_exp);
4748 tree t;
4750 record_node_allocation_statistics (code, length);
4752 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4754 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4756 memset (t, 0, sizeof (struct tree_common));
4758 TREE_SET_CODE (t, code);
4760 TREE_TYPE (t) = type;
4761 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4762 TREE_OPERAND (t, 0) = node;
4763 if (node && !TYPE_P (node))
4765 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4766 TREE_READONLY (t) = TREE_READONLY (node);
4769 if (TREE_CODE_CLASS (code) == tcc_statement)
4771 if (code != DEBUG_BEGIN_STMT)
4772 TREE_SIDE_EFFECTS (t) = 1;
4774 else switch (code)
4776 case VA_ARG_EXPR:
4777 /* All of these have side-effects, no matter what their
4778 operands are. */
4779 TREE_SIDE_EFFECTS (t) = 1;
4780 TREE_READONLY (t) = 0;
4781 break;
4783 case INDIRECT_REF:
4784 /* Whether a dereference is readonly has nothing to do with whether
4785 its operand is readonly. */
4786 TREE_READONLY (t) = 0;
4787 break;
4789 case ADDR_EXPR:
4790 if (node)
4791 recompute_tree_invariant_for_addr_expr (t);
4792 break;
4794 default:
4795 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4796 && node && !TYPE_P (node)
4797 && TREE_CONSTANT (node))
4798 TREE_CONSTANT (t) = 1;
4799 if (TREE_CODE_CLASS (code) == tcc_reference
4800 && node && TREE_THIS_VOLATILE (node))
4801 TREE_THIS_VOLATILE (t) = 1;
4802 break;
4805 return t;
4808 #define PROCESS_ARG(N) \
4809 do { \
4810 TREE_OPERAND (t, N) = arg##N; \
4811 if (arg##N &&!TYPE_P (arg##N)) \
4813 if (TREE_SIDE_EFFECTS (arg##N)) \
4814 side_effects = 1; \
4815 if (!TREE_READONLY (arg##N) \
4816 && !CONSTANT_CLASS_P (arg##N)) \
4817 (void) (read_only = 0); \
4818 if (!TREE_CONSTANT (arg##N)) \
4819 (void) (constant = 0); \
4821 } while (0)
4823 tree
4824 build2 (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
4826 bool constant, read_only, side_effects, div_by_zero;
4827 tree t;
4829 gcc_assert (TREE_CODE_LENGTH (code) == 2);
4831 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
4832 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
4833 /* When sizetype precision doesn't match that of pointers
4834 we need to be able to build explicit extensions or truncations
4835 of the offset argument. */
4836 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
4837 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
4838 && TREE_CODE (arg1) == INTEGER_CST);
4840 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
4841 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
4842 && ptrofftype_p (TREE_TYPE (arg1)));
4844 t = make_node (code PASS_MEM_STAT);
4845 TREE_TYPE (t) = tt;
4847 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
4848 result based on those same flags for the arguments. But if the
4849 arguments aren't really even `tree' expressions, we shouldn't be trying
4850 to do this. */
4852 /* Expressions without side effects may be constant if their
4853 arguments are as well. */
4854 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4855 || TREE_CODE_CLASS (code) == tcc_binary);
4856 read_only = 1;
4857 side_effects = TREE_SIDE_EFFECTS (t);
4859 switch (code)
4861 case TRUNC_DIV_EXPR:
4862 case CEIL_DIV_EXPR:
4863 case FLOOR_DIV_EXPR:
4864 case ROUND_DIV_EXPR:
4865 case EXACT_DIV_EXPR:
4866 case CEIL_MOD_EXPR:
4867 case FLOOR_MOD_EXPR:
4868 case ROUND_MOD_EXPR:
4869 case TRUNC_MOD_EXPR:
4870 div_by_zero = integer_zerop (arg1);
4871 break;
4872 default:
4873 div_by_zero = false;
4876 PROCESS_ARG (0);
4877 PROCESS_ARG (1);
4879 TREE_SIDE_EFFECTS (t) = side_effects;
4880 if (code == MEM_REF)
4882 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
4884 tree o = TREE_OPERAND (arg0, 0);
4885 TREE_READONLY (t) = TREE_READONLY (o);
4886 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
4889 else
4891 TREE_READONLY (t) = read_only;
4892 /* Don't mark X / 0 as constant. */
4893 TREE_CONSTANT (t) = constant && !div_by_zero;
4894 TREE_THIS_VOLATILE (t)
4895 = (TREE_CODE_CLASS (code) == tcc_reference
4896 && arg0 && TREE_THIS_VOLATILE (arg0));
4899 return t;
4903 tree
4904 build3 (enum tree_code code, tree tt, tree arg0, tree arg1,
4905 tree arg2 MEM_STAT_DECL)
4907 bool constant, read_only, side_effects;
4908 tree t;
4910 gcc_assert (TREE_CODE_LENGTH (code) == 3);
4911 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4913 t = make_node (code PASS_MEM_STAT);
4914 TREE_TYPE (t) = tt;
4916 read_only = 1;
4918 /* As a special exception, if COND_EXPR has NULL branches, we
4919 assume that it is a gimple statement and always consider
4920 it to have side effects. */
4921 if (code == COND_EXPR
4922 && tt == void_type_node
4923 && arg1 == NULL_TREE
4924 && arg2 == NULL_TREE)
4925 side_effects = true;
4926 else
4927 side_effects = TREE_SIDE_EFFECTS (t);
4929 PROCESS_ARG (0);
4930 PROCESS_ARG (1);
4931 PROCESS_ARG (2);
4933 if (code == COND_EXPR)
4934 TREE_READONLY (t) = read_only;
4936 TREE_SIDE_EFFECTS (t) = side_effects;
4937 TREE_THIS_VOLATILE (t)
4938 = (TREE_CODE_CLASS (code) == tcc_reference
4939 && arg0 && TREE_THIS_VOLATILE (arg0));
4941 return t;
4944 tree
4945 build4 (enum tree_code code, tree tt, tree arg0, tree arg1,
4946 tree arg2, tree arg3 MEM_STAT_DECL)
4948 bool constant, read_only, side_effects;
4949 tree t;
4951 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4953 t = make_node (code PASS_MEM_STAT);
4954 TREE_TYPE (t) = tt;
4956 side_effects = TREE_SIDE_EFFECTS (t);
4958 PROCESS_ARG (0);
4959 PROCESS_ARG (1);
4960 PROCESS_ARG (2);
4961 PROCESS_ARG (3);
4963 TREE_SIDE_EFFECTS (t) = side_effects;
4964 TREE_THIS_VOLATILE (t)
4965 = (TREE_CODE_CLASS (code) == tcc_reference
4966 && arg0 && TREE_THIS_VOLATILE (arg0));
4968 return t;
4971 tree
4972 build5 (enum tree_code code, tree tt, tree arg0, tree arg1,
4973 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4975 bool constant, read_only, side_effects;
4976 tree t;
4978 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4980 t = make_node (code PASS_MEM_STAT);
4981 TREE_TYPE (t) = tt;
4983 side_effects = TREE_SIDE_EFFECTS (t);
4985 PROCESS_ARG (0);
4986 PROCESS_ARG (1);
4987 PROCESS_ARG (2);
4988 PROCESS_ARG (3);
4989 PROCESS_ARG (4);
4991 TREE_SIDE_EFFECTS (t) = side_effects;
4992 if (code == TARGET_MEM_REF)
4994 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
4996 tree o = TREE_OPERAND (arg0, 0);
4997 TREE_READONLY (t) = TREE_READONLY (o);
4998 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
5001 else
5002 TREE_THIS_VOLATILE (t)
5003 = (TREE_CODE_CLASS (code) == tcc_reference
5004 && arg0 && TREE_THIS_VOLATILE (arg0));
5006 return t;
5009 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
5010 on the pointer PTR. */
5012 tree
5013 build_simple_mem_ref_loc (location_t loc, tree ptr)
5015 poly_int64 offset = 0;
5016 tree ptype = TREE_TYPE (ptr);
5017 tree tem;
5018 /* For convenience allow addresses that collapse to a simple base
5019 and offset. */
5020 if (TREE_CODE (ptr) == ADDR_EXPR
5021 && (handled_component_p (TREE_OPERAND (ptr, 0))
5022 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
5024 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
5025 gcc_assert (ptr);
5026 if (TREE_CODE (ptr) == MEM_REF)
5028 offset += mem_ref_offset (ptr).force_shwi ();
5029 ptr = TREE_OPERAND (ptr, 0);
5031 else
5032 ptr = build_fold_addr_expr (ptr);
5033 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
5035 tem = build2 (MEM_REF, TREE_TYPE (ptype),
5036 ptr, build_int_cst (ptype, offset));
5037 SET_EXPR_LOCATION (tem, loc);
5038 return tem;
5041 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
5043 poly_offset_int
5044 mem_ref_offset (const_tree t)
5046 return poly_offset_int::from (wi::to_poly_wide (TREE_OPERAND (t, 1)),
5047 SIGNED);
5050 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
5051 offsetted by OFFSET units. */
5053 tree
5054 build_invariant_address (tree type, tree base, poly_int64 offset)
5056 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
5057 build_fold_addr_expr (base),
5058 build_int_cst (ptr_type_node, offset));
5059 tree addr = build1 (ADDR_EXPR, type, ref);
5060 recompute_tree_invariant_for_addr_expr (addr);
5061 return addr;
5064 /* Similar except don't specify the TREE_TYPE
5065 and leave the TREE_SIDE_EFFECTS as 0.
5066 It is permissible for arguments to be null,
5067 or even garbage if their values do not matter. */
5069 tree
5070 build_nt (enum tree_code code, ...)
5072 tree t;
5073 int length;
5074 int i;
5075 va_list p;
5077 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
5079 va_start (p, code);
5081 t = make_node (code);
5082 length = TREE_CODE_LENGTH (code);
5084 for (i = 0; i < length; i++)
5085 TREE_OPERAND (t, i) = va_arg (p, tree);
5087 va_end (p);
5088 return t;
5091 /* Similar to build_nt, but for creating a CALL_EXPR object with a
5092 tree vec. */
5094 tree
5095 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
5097 tree ret, t;
5098 unsigned int ix;
5100 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
5101 CALL_EXPR_FN (ret) = fn;
5102 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
5103 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
5104 CALL_EXPR_ARG (ret, ix) = t;
5105 return ret;
5108 /* Create a DECL_... node of code CODE, name NAME (if non-null)
5109 and data type TYPE.
5110 We do NOT enter this node in any sort of symbol table.
5112 LOC is the location of the decl.
5114 layout_decl is used to set up the decl's storage layout.
5115 Other slots are initialized to 0 or null pointers. */
5117 tree
5118 build_decl (location_t loc, enum tree_code code, tree name,
5119 tree type MEM_STAT_DECL)
5121 tree t;
5123 t = make_node (code PASS_MEM_STAT);
5124 DECL_SOURCE_LOCATION (t) = loc;
5126 /* if (type == error_mark_node)
5127 type = integer_type_node; */
5128 /* That is not done, deliberately, so that having error_mark_node
5129 as the type can suppress useless errors in the use of this variable. */
5131 DECL_NAME (t) = name;
5132 TREE_TYPE (t) = type;
5134 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
5135 layout_decl (t, 0);
5137 return t;
5140 /* Builds and returns function declaration with NAME and TYPE. */
5142 tree
5143 build_fn_decl (const char *name, tree type)
5145 tree id = get_identifier (name);
5146 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
5148 DECL_EXTERNAL (decl) = 1;
5149 TREE_PUBLIC (decl) = 1;
5150 DECL_ARTIFICIAL (decl) = 1;
5151 TREE_NOTHROW (decl) = 1;
5153 return decl;
5156 vec<tree, va_gc> *all_translation_units;
5158 /* Builds a new translation-unit decl with name NAME, queues it in the
5159 global list of translation-unit decls and returns it. */
5161 tree
5162 build_translation_unit_decl (tree name)
5164 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
5165 name, NULL_TREE);
5166 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
5167 vec_safe_push (all_translation_units, tu);
5168 return tu;
5172 /* BLOCK nodes are used to represent the structure of binding contours
5173 and declarations, once those contours have been exited and their contents
5174 compiled. This information is used for outputting debugging info. */
5176 tree
5177 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
5179 tree block = make_node (BLOCK);
5181 BLOCK_VARS (block) = vars;
5182 BLOCK_SUBBLOCKS (block) = subblocks;
5183 BLOCK_SUPERCONTEXT (block) = supercontext;
5184 BLOCK_CHAIN (block) = chain;
5185 return block;
5189 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
5191 LOC is the location to use in tree T. */
5193 void
5194 protected_set_expr_location (tree t, location_t loc)
5196 if (CAN_HAVE_LOCATION_P (t))
5197 SET_EXPR_LOCATION (t, loc);
5198 else if (t && TREE_CODE (t) == STATEMENT_LIST)
5200 t = expr_single (t);
5201 if (t && CAN_HAVE_LOCATION_P (t))
5202 SET_EXPR_LOCATION (t, loc);
5206 /* Like PROTECTED_SET_EXPR_LOCATION, but only do that if T has
5207 UNKNOWN_LOCATION. */
5209 void
5210 protected_set_expr_location_if_unset (tree t, location_t loc)
5212 t = expr_single (t);
5213 if (t && !EXPR_HAS_LOCATION (t))
5214 protected_set_expr_location (t, loc);
5217 /* Data used when collecting DECLs and TYPEs for language data removal. */
5219 class free_lang_data_d
5221 public:
5222 free_lang_data_d () : decls (100), types (100) {}
5224 /* Worklist to avoid excessive recursion. */
5225 auto_vec<tree> worklist;
5227 /* Set of traversed objects. Used to avoid duplicate visits. */
5228 hash_set<tree> pset;
5230 /* Array of symbols to process with free_lang_data_in_decl. */
5231 auto_vec<tree> decls;
5233 /* Array of types to process with free_lang_data_in_type. */
5234 auto_vec<tree> types;
5238 /* Add type or decl T to one of the list of tree nodes that need their
5239 language data removed. The lists are held inside FLD. */
5241 static void
5242 add_tree_to_fld_list (tree t, class free_lang_data_d *fld)
5244 if (DECL_P (t))
5245 fld->decls.safe_push (t);
5246 else if (TYPE_P (t))
5247 fld->types.safe_push (t);
5248 else
5249 gcc_unreachable ();
5252 /* Push tree node T into FLD->WORKLIST. */
5254 static inline void
5255 fld_worklist_push (tree t, class free_lang_data_d *fld)
5257 if (t && !is_lang_specific (t) && !fld->pset.contains (t))
5258 fld->worklist.safe_push ((t));
5263 /* Return simplified TYPE_NAME of TYPE. */
5265 static tree
5266 fld_simplified_type_name (tree type)
5268 if (!TYPE_NAME (type) || TREE_CODE (TYPE_NAME (type)) != TYPE_DECL)
5269 return TYPE_NAME (type);
5270 /* Drop TYPE_DECLs in TYPE_NAME in favor of the identifier in the
5271 TYPE_DECL if the type doesn't have linkage.
5272 this must match fld_ */
5273 if (type != TYPE_MAIN_VARIANT (type)
5274 || (!DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type))
5275 && (TREE_CODE (type) != RECORD_TYPE
5276 || !TYPE_BINFO (type)
5277 || !BINFO_VTABLE (TYPE_BINFO (type)))))
5278 return DECL_NAME (TYPE_NAME (type));
5279 return TYPE_NAME (type);
5282 /* Do same comparsion as check_qualified_type skipping lang part of type
5283 and be more permissive about type names: we only care that names are
5284 same (for diagnostics) and that ODR names are the same.
5285 If INNER_TYPE is non-NULL, be sure that TREE_TYPE match it. */
5287 static bool
5288 fld_type_variant_equal_p (tree t, tree v, tree inner_type)
5290 if (TYPE_QUALS (t) != TYPE_QUALS (v)
5291 /* We want to match incomplete variants with complete types.
5292 In this case we need to ignore alignment. */
5293 || ((!RECORD_OR_UNION_TYPE_P (t) || COMPLETE_TYPE_P (v))
5294 && (TYPE_ALIGN (t) != TYPE_ALIGN (v)
5295 || TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (v)))
5296 || fld_simplified_type_name (t) != fld_simplified_type_name (v)
5297 || !attribute_list_equal (TYPE_ATTRIBUTES (t),
5298 TYPE_ATTRIBUTES (v))
5299 || (inner_type && TREE_TYPE (v) != inner_type))
5300 return false;
5302 return true;
5305 /* Find variant of FIRST that match T and create new one if necessary.
5306 Set TREE_TYPE to INNER_TYPE if non-NULL. */
5308 static tree
5309 fld_type_variant (tree first, tree t, class free_lang_data_d *fld,
5310 tree inner_type = NULL)
5312 if (first == TYPE_MAIN_VARIANT (t))
5313 return t;
5314 for (tree v = first; v; v = TYPE_NEXT_VARIANT (v))
5315 if (fld_type_variant_equal_p (t, v, inner_type))
5316 return v;
5317 tree v = build_variant_type_copy (first);
5318 TYPE_READONLY (v) = TYPE_READONLY (t);
5319 TYPE_VOLATILE (v) = TYPE_VOLATILE (t);
5320 TYPE_ATOMIC (v) = TYPE_ATOMIC (t);
5321 TYPE_RESTRICT (v) = TYPE_RESTRICT (t);
5322 TYPE_ADDR_SPACE (v) = TYPE_ADDR_SPACE (t);
5323 TYPE_NAME (v) = TYPE_NAME (t);
5324 TYPE_ATTRIBUTES (v) = TYPE_ATTRIBUTES (t);
5325 TYPE_CANONICAL (v) = TYPE_CANONICAL (t);
5326 /* Variants of incomplete types should have alignment
5327 set to BITS_PER_UNIT. Do not copy the actual alignment. */
5328 if (!RECORD_OR_UNION_TYPE_P (v) || COMPLETE_TYPE_P (v))
5330 SET_TYPE_ALIGN (v, TYPE_ALIGN (t));
5331 TYPE_USER_ALIGN (v) = TYPE_USER_ALIGN (t);
5333 if (inner_type)
5334 TREE_TYPE (v) = inner_type;
5335 gcc_checking_assert (fld_type_variant_equal_p (t,v, inner_type));
5336 if (!fld->pset.add (v))
5337 add_tree_to_fld_list (v, fld);
5338 return v;
5341 /* Map complete types to incomplete types. */
5343 static hash_map<tree, tree> *fld_incomplete_types;
5345 /* Map types to simplified types. */
5347 static hash_map<tree, tree> *fld_simplified_types;
5349 /* Produce variant of T whose TREE_TYPE is T2. If it is main variant,
5350 use MAP to prevent duplicates. */
5352 static tree
5353 fld_process_array_type (tree t, tree t2, hash_map<tree, tree> *map,
5354 class free_lang_data_d *fld)
5356 if (TREE_TYPE (t) == t2)
5357 return t;
5359 if (TYPE_MAIN_VARIANT (t) != t)
5361 return fld_type_variant
5362 (fld_process_array_type (TYPE_MAIN_VARIANT (t),
5363 TYPE_MAIN_VARIANT (t2), map, fld),
5364 t, fld, t2);
5367 bool existed;
5368 tree &array
5369 = map->get_or_insert (t, &existed);
5370 if (!existed)
5372 array
5373 = build_array_type_1 (t2, TYPE_DOMAIN (t), TYPE_TYPELESS_STORAGE (t),
5374 false, false);
5375 TYPE_CANONICAL (array) = TYPE_CANONICAL (t);
5376 if (!fld->pset.add (array))
5377 add_tree_to_fld_list (array, fld);
5379 return array;
5382 /* Return CTX after removal of contexts that are not relevant */
5384 static tree
5385 fld_decl_context (tree ctx)
5387 /* Variably modified types are needed for tree_is_indexable to decide
5388 whether the type needs to go to local or global section.
5389 This code is semi-broken but for now it is easiest to keep contexts
5390 as expected. */
5391 if (ctx && TYPE_P (ctx)
5392 && !variably_modified_type_p (ctx, NULL_TREE))
5394 while (ctx && TYPE_P (ctx))
5395 ctx = TYPE_CONTEXT (ctx);
5397 return ctx;
5400 /* For T being aggregate type try to turn it into a incomplete variant.
5401 Return T if no simplification is possible. */
5403 static tree
5404 fld_incomplete_type_of (tree t, class free_lang_data_d *fld)
5406 if (!t)
5407 return NULL;
5408 if (POINTER_TYPE_P (t))
5410 tree t2 = fld_incomplete_type_of (TREE_TYPE (t), fld);
5411 if (t2 != TREE_TYPE (t))
5413 tree first;
5414 if (TREE_CODE (t) == POINTER_TYPE)
5415 first = build_pointer_type_for_mode (t2, TYPE_MODE (t),
5416 TYPE_REF_CAN_ALIAS_ALL (t));
5417 else
5418 first = build_reference_type_for_mode (t2, TYPE_MODE (t),
5419 TYPE_REF_CAN_ALIAS_ALL (t));
5420 gcc_assert (TYPE_CANONICAL (t2) != t2
5421 && TYPE_CANONICAL (t2) == TYPE_CANONICAL (TREE_TYPE (t)));
5422 if (!fld->pset.add (first))
5423 add_tree_to_fld_list (first, fld);
5424 return fld_type_variant (first, t, fld);
5426 return t;
5428 if (TREE_CODE (t) == ARRAY_TYPE)
5429 return fld_process_array_type (t,
5430 fld_incomplete_type_of (TREE_TYPE (t), fld),
5431 fld_incomplete_types, fld);
5432 if ((!RECORD_OR_UNION_TYPE_P (t) && TREE_CODE (t) != ENUMERAL_TYPE)
5433 || !COMPLETE_TYPE_P (t))
5434 return t;
5435 if (TYPE_MAIN_VARIANT (t) == t)
5437 bool existed;
5438 tree &copy
5439 = fld_incomplete_types->get_or_insert (t, &existed);
5441 if (!existed)
5443 copy = build_distinct_type_copy (t);
5445 /* It is possible that type was not seen by free_lang_data yet. */
5446 if (!fld->pset.add (copy))
5447 add_tree_to_fld_list (copy, fld);
5448 TYPE_SIZE (copy) = NULL;
5449 TYPE_USER_ALIGN (copy) = 0;
5450 TYPE_SIZE_UNIT (copy) = NULL;
5451 TYPE_CANONICAL (copy) = TYPE_CANONICAL (t);
5452 TREE_ADDRESSABLE (copy) = 0;
5453 if (AGGREGATE_TYPE_P (t))
5455 SET_TYPE_MODE (copy, VOIDmode);
5456 SET_TYPE_ALIGN (copy, BITS_PER_UNIT);
5457 TYPE_TYPELESS_STORAGE (copy) = 0;
5458 TYPE_FIELDS (copy) = NULL;
5459 TYPE_BINFO (copy) = NULL;
5460 TYPE_FINAL_P (copy) = 0;
5461 TYPE_EMPTY_P (copy) = 0;
5463 else
5465 TYPE_VALUES (copy) = NULL;
5466 ENUM_IS_OPAQUE (copy) = 0;
5467 ENUM_IS_SCOPED (copy) = 0;
5470 /* Build copy of TYPE_DECL in TYPE_NAME if necessary.
5471 This is needed for ODR violation warnings to come out right (we
5472 want duplicate TYPE_DECLs whenever the type is duplicated because
5473 of ODR violation. Because lang data in the TYPE_DECL may not
5474 have been freed yet, rebuild it from scratch and copy relevant
5475 fields. */
5476 TYPE_NAME (copy) = fld_simplified_type_name (copy);
5477 tree name = TYPE_NAME (copy);
5479 if (name && TREE_CODE (name) == TYPE_DECL)
5481 gcc_checking_assert (TREE_TYPE (name) == t);
5482 tree name2 = build_decl (DECL_SOURCE_LOCATION (name), TYPE_DECL,
5483 DECL_NAME (name), copy);
5484 if (DECL_ASSEMBLER_NAME_SET_P (name))
5485 SET_DECL_ASSEMBLER_NAME (name2, DECL_ASSEMBLER_NAME (name));
5486 SET_DECL_ALIGN (name2, 0);
5487 DECL_CONTEXT (name2) = fld_decl_context
5488 (DECL_CONTEXT (name));
5489 TYPE_NAME (copy) = name2;
5492 return copy;
5494 return (fld_type_variant
5495 (fld_incomplete_type_of (TYPE_MAIN_VARIANT (t), fld), t, fld));
5498 /* Simplify type T for scenarios where we do not need complete pointer
5499 types. */
5501 static tree
5502 fld_simplified_type (tree t, class free_lang_data_d *fld)
5504 if (!t)
5505 return t;
5506 if (POINTER_TYPE_P (t))
5507 return fld_incomplete_type_of (t, fld);
5508 /* FIXME: This triggers verification error, see PR88140. */
5509 if (TREE_CODE (t) == ARRAY_TYPE && 0)
5510 return fld_process_array_type (t, fld_simplified_type (TREE_TYPE (t), fld),
5511 fld_simplified_types, fld);
5512 return t;
5515 /* Reset the expression *EXPR_P, a size or position.
5517 ??? We could reset all non-constant sizes or positions. But it's cheap
5518 enough to not do so and refrain from adding workarounds to dwarf2out.c.
5520 We need to reset self-referential sizes or positions because they cannot
5521 be gimplified and thus can contain a CALL_EXPR after the gimplification
5522 is finished, which will run afoul of LTO streaming. And they need to be
5523 reset to something essentially dummy but not constant, so as to preserve
5524 the properties of the object they are attached to. */
5526 static inline void
5527 free_lang_data_in_one_sizepos (tree *expr_p)
5529 tree expr = *expr_p;
5530 if (CONTAINS_PLACEHOLDER_P (expr))
5531 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
5535 /* Reset all the fields in a binfo node BINFO. We only keep
5536 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
5538 static void
5539 free_lang_data_in_binfo (tree binfo)
5541 unsigned i;
5542 tree t;
5544 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
5546 BINFO_VIRTUALS (binfo) = NULL_TREE;
5547 BINFO_BASE_ACCESSES (binfo) = NULL;
5548 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
5549 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
5550 BINFO_VPTR_FIELD (binfo) = NULL_TREE;
5551 TREE_PUBLIC (binfo) = 0;
5553 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
5554 free_lang_data_in_binfo (t);
5558 /* Reset all language specific information still present in TYPE. */
5560 static void
5561 free_lang_data_in_type (tree type, class free_lang_data_d *fld)
5563 gcc_assert (TYPE_P (type));
5565 /* Give the FE a chance to remove its own data first. */
5566 lang_hooks.free_lang_data (type);
5568 TREE_LANG_FLAG_0 (type) = 0;
5569 TREE_LANG_FLAG_1 (type) = 0;
5570 TREE_LANG_FLAG_2 (type) = 0;
5571 TREE_LANG_FLAG_3 (type) = 0;
5572 TREE_LANG_FLAG_4 (type) = 0;
5573 TREE_LANG_FLAG_5 (type) = 0;
5574 TREE_LANG_FLAG_6 (type) = 0;
5576 TYPE_NEEDS_CONSTRUCTING (type) = 0;
5578 /* Purge non-marked variants from the variants chain, so that they
5579 don't reappear in the IL after free_lang_data. */
5580 while (TYPE_NEXT_VARIANT (type)
5581 && !fld->pset.contains (TYPE_NEXT_VARIANT (type)))
5583 tree t = TYPE_NEXT_VARIANT (type);
5584 TYPE_NEXT_VARIANT (type) = TYPE_NEXT_VARIANT (t);
5585 /* Turn the removed types into distinct types. */
5586 TYPE_MAIN_VARIANT (t) = t;
5587 TYPE_NEXT_VARIANT (t) = NULL_TREE;
5590 if (TREE_CODE (type) == FUNCTION_TYPE)
5592 TREE_TYPE (type) = fld_simplified_type (TREE_TYPE (type), fld);
5593 /* Remove the const and volatile qualifiers from arguments. The
5594 C++ front end removes them, but the C front end does not,
5595 leading to false ODR violation errors when merging two
5596 instances of the same function signature compiled by
5597 different front ends. */
5598 for (tree p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
5600 TREE_VALUE (p) = fld_simplified_type (TREE_VALUE (p), fld);
5601 tree arg_type = TREE_VALUE (p);
5603 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
5605 int quals = TYPE_QUALS (arg_type)
5606 & ~TYPE_QUAL_CONST
5607 & ~TYPE_QUAL_VOLATILE;
5608 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
5609 if (!fld->pset.add (TREE_VALUE (p)))
5610 free_lang_data_in_type (TREE_VALUE (p), fld);
5612 /* C++ FE uses TREE_PURPOSE to store initial values. */
5613 TREE_PURPOSE (p) = NULL;
5616 else if (TREE_CODE (type) == METHOD_TYPE)
5618 TREE_TYPE (type) = fld_simplified_type (TREE_TYPE (type), fld);
5619 for (tree p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
5621 /* C++ FE uses TREE_PURPOSE to store initial values. */
5622 TREE_VALUE (p) = fld_simplified_type (TREE_VALUE (p), fld);
5623 TREE_PURPOSE (p) = NULL;
5626 else if (RECORD_OR_UNION_TYPE_P (type))
5628 /* Remove members that are not FIELD_DECLs from the field list
5629 of an aggregate. These occur in C++. */
5630 for (tree *prev = &TYPE_FIELDS (type), member; (member = *prev);)
5631 if (TREE_CODE (member) == FIELD_DECL)
5632 prev = &DECL_CHAIN (member);
5633 else
5634 *prev = DECL_CHAIN (member);
5636 TYPE_VFIELD (type) = NULL_TREE;
5638 if (TYPE_BINFO (type))
5640 free_lang_data_in_binfo (TYPE_BINFO (type));
5641 /* We need to preserve link to bases and virtual table for all
5642 polymorphic types to make devirtualization machinery working. */
5643 if (!BINFO_VTABLE (TYPE_BINFO (type)))
5644 TYPE_BINFO (type) = NULL;
5647 else if (INTEGRAL_TYPE_P (type)
5648 || SCALAR_FLOAT_TYPE_P (type)
5649 || FIXED_POINT_TYPE_P (type))
5651 if (TREE_CODE (type) == ENUMERAL_TYPE)
5653 ENUM_IS_OPAQUE (type) = 0;
5654 ENUM_IS_SCOPED (type) = 0;
5655 /* Type values are used only for C++ ODR checking. Drop them
5656 for all type variants and non-ODR types.
5657 For ODR types the data is freed in free_odr_warning_data. */
5658 if (!TYPE_VALUES (type))
5660 else if (TYPE_MAIN_VARIANT (type) != type
5661 || !type_with_linkage_p (type)
5662 || type_in_anonymous_namespace_p (type))
5663 TYPE_VALUES (type) = NULL;
5664 else
5665 register_odr_enum (type);
5667 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
5668 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
5671 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
5673 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
5674 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
5676 if (TYPE_CONTEXT (type)
5677 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
5679 tree ctx = TYPE_CONTEXT (type);
5682 ctx = BLOCK_SUPERCONTEXT (ctx);
5684 while (ctx && TREE_CODE (ctx) == BLOCK);
5685 TYPE_CONTEXT (type) = ctx;
5688 TYPE_STUB_DECL (type) = NULL;
5689 TYPE_NAME (type) = fld_simplified_type_name (type);
5693 /* Return true if DECL may need an assembler name to be set. */
5695 static inline bool
5696 need_assembler_name_p (tree decl)
5698 /* We use DECL_ASSEMBLER_NAME to hold mangled type names for One Definition
5699 Rule merging. This makes type_odr_p to return true on those types during
5700 LTO and by comparing the mangled name, we can say what types are intended
5701 to be equivalent across compilation unit.
5703 We do not store names of type_in_anonymous_namespace_p.
5705 Record, union and enumeration type have linkage that allows use
5706 to check type_in_anonymous_namespace_p. We do not mangle compound types
5707 that always can be compared structurally.
5709 Similarly for builtin types, we compare properties of their main variant.
5710 A special case are integer types where mangling do make differences
5711 between char/signed char/unsigned char etc. Storing name for these makes
5712 e.g. -fno-signed-char/-fsigned-char mismatches to be handled well.
5713 See cp/mangle.c:write_builtin_type for details. */
5715 if (TREE_CODE (decl) == TYPE_DECL)
5717 if (DECL_NAME (decl)
5718 && decl == TYPE_NAME (TREE_TYPE (decl))
5719 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TREE_TYPE (decl)
5720 && !TYPE_ARTIFICIAL (TREE_TYPE (decl))
5721 && ((TREE_CODE (TREE_TYPE (decl)) != RECORD_TYPE
5722 && TREE_CODE (TREE_TYPE (decl)) != UNION_TYPE)
5723 || TYPE_CXX_ODR_P (TREE_TYPE (decl)))
5724 && (type_with_linkage_p (TREE_TYPE (decl))
5725 || TREE_CODE (TREE_TYPE (decl)) == INTEGER_TYPE)
5726 && !variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5727 return !DECL_ASSEMBLER_NAME_SET_P (decl);
5728 return false;
5730 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
5731 if (!VAR_OR_FUNCTION_DECL_P (decl))
5732 return false;
5734 /* If DECL already has its assembler name set, it does not need a
5735 new one. */
5736 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
5737 || DECL_ASSEMBLER_NAME_SET_P (decl))
5738 return false;
5740 /* Abstract decls do not need an assembler name. */
5741 if (DECL_ABSTRACT_P (decl))
5742 return false;
5744 /* For VAR_DECLs, only static, public and external symbols need an
5745 assembler name. */
5746 if (VAR_P (decl)
5747 && !TREE_STATIC (decl)
5748 && !TREE_PUBLIC (decl)
5749 && !DECL_EXTERNAL (decl))
5750 return false;
5752 if (TREE_CODE (decl) == FUNCTION_DECL)
5754 /* Do not set assembler name on builtins. Allow RTL expansion to
5755 decide whether to expand inline or via a regular call. */
5756 if (fndecl_built_in_p (decl)
5757 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
5758 return false;
5760 /* Functions represented in the callgraph need an assembler name. */
5761 if (cgraph_node::get (decl) != NULL)
5762 return true;
5764 /* Unused and not public functions don't need an assembler name. */
5765 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
5766 return false;
5769 return true;
5773 /* Reset all language specific information still present in symbol
5774 DECL. */
5776 static void
5777 free_lang_data_in_decl (tree decl, class free_lang_data_d *fld)
5779 gcc_assert (DECL_P (decl));
5781 /* Give the FE a chance to remove its own data first. */
5782 lang_hooks.free_lang_data (decl);
5784 TREE_LANG_FLAG_0 (decl) = 0;
5785 TREE_LANG_FLAG_1 (decl) = 0;
5786 TREE_LANG_FLAG_2 (decl) = 0;
5787 TREE_LANG_FLAG_3 (decl) = 0;
5788 TREE_LANG_FLAG_4 (decl) = 0;
5789 TREE_LANG_FLAG_5 (decl) = 0;
5790 TREE_LANG_FLAG_6 (decl) = 0;
5792 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
5793 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
5794 if (TREE_CODE (decl) == FIELD_DECL)
5796 DECL_FCONTEXT (decl) = NULL;
5797 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
5798 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
5799 DECL_QUALIFIER (decl) = NULL_TREE;
5802 if (TREE_CODE (decl) == FUNCTION_DECL)
5804 struct cgraph_node *node;
5805 /* Frontends do not set TREE_ADDRESSABLE on public variables even though
5806 the address may be taken in other unit, so this flag has no practical
5807 use for middle-end.
5809 It would make more sense if frontends set TREE_ADDRESSABLE to 0 only
5810 for public objects that indeed cannot be adressed, but it is not
5811 the case. Set the flag to true so we do not get merge failures for
5812 i.e. virtual tables between units that take address of it and
5813 units that don't. */
5814 if (TREE_PUBLIC (decl))
5815 TREE_ADDRESSABLE (decl) = true;
5816 TREE_TYPE (decl) = fld_simplified_type (TREE_TYPE (decl), fld);
5817 if (!(node = cgraph_node::get (decl))
5818 || (!node->definition && !node->clones))
5820 if (node)
5821 node->release_body ();
5822 else
5824 release_function_body (decl);
5825 DECL_ARGUMENTS (decl) = NULL;
5826 DECL_RESULT (decl) = NULL;
5827 DECL_INITIAL (decl) = error_mark_node;
5830 if (gimple_has_body_p (decl) || (node && node->thunk))
5832 tree t;
5834 /* If DECL has a gimple body, then the context for its
5835 arguments must be DECL. Otherwise, it doesn't really
5836 matter, as we will not be emitting any code for DECL. In
5837 general, there may be other instances of DECL created by
5838 the front end and since PARM_DECLs are generally shared,
5839 their DECL_CONTEXT changes as the replicas of DECL are
5840 created. The only time where DECL_CONTEXT is important
5841 is for the FUNCTION_DECLs that have a gimple body (since
5842 the PARM_DECL will be used in the function's body). */
5843 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
5844 DECL_CONTEXT (t) = decl;
5845 if (!DECL_FUNCTION_SPECIFIC_TARGET (decl))
5846 DECL_FUNCTION_SPECIFIC_TARGET (decl)
5847 = target_option_default_node;
5848 if (!DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl))
5849 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl)
5850 = optimization_default_node;
5853 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
5854 At this point, it is not needed anymore. */
5855 DECL_SAVED_TREE (decl) = NULL_TREE;
5857 /* Clear the abstract origin if it refers to a method.
5858 Otherwise dwarf2out.c will ICE as we splice functions out of
5859 TYPE_FIELDS and thus the origin will not be output
5860 correctly. */
5861 if (DECL_ABSTRACT_ORIGIN (decl)
5862 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
5863 && RECORD_OR_UNION_TYPE_P
5864 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
5865 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
5867 DECL_VINDEX (decl) = NULL_TREE;
5869 else if (VAR_P (decl))
5871 /* See comment above why we set the flag for functions. */
5872 if (TREE_PUBLIC (decl))
5873 TREE_ADDRESSABLE (decl) = true;
5874 if ((DECL_EXTERNAL (decl)
5875 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
5876 || (decl_function_context (decl) && !TREE_STATIC (decl)))
5877 DECL_INITIAL (decl) = NULL_TREE;
5879 else if (TREE_CODE (decl) == TYPE_DECL)
5881 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5882 DECL_VISIBILITY_SPECIFIED (decl) = 0;
5883 TREE_PUBLIC (decl) = 0;
5884 TREE_PRIVATE (decl) = 0;
5885 DECL_ARTIFICIAL (decl) = 0;
5886 TYPE_DECL_SUPPRESS_DEBUG (decl) = 0;
5887 DECL_INITIAL (decl) = NULL_TREE;
5888 DECL_ORIGINAL_TYPE (decl) = NULL_TREE;
5889 DECL_MODE (decl) = VOIDmode;
5890 SET_DECL_ALIGN (decl, 0);
5891 /* TREE_TYPE is cleared at WPA time in free_odr_warning_data. */
5893 else if (TREE_CODE (decl) == FIELD_DECL)
5895 TREE_TYPE (decl) = fld_simplified_type (TREE_TYPE (decl), fld);
5896 DECL_INITIAL (decl) = NULL_TREE;
5898 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
5899 && DECL_INITIAL (decl)
5900 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
5902 /* Strip builtins from the translation-unit BLOCK. We still have targets
5903 without builtin_decl_explicit support and also builtins are shared
5904 nodes and thus we can't use TREE_CHAIN in multiple lists. */
5905 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
5906 while (*nextp)
5908 tree var = *nextp;
5909 if (TREE_CODE (var) == FUNCTION_DECL
5910 && fndecl_built_in_p (var))
5911 *nextp = TREE_CHAIN (var);
5912 else
5913 nextp = &TREE_CHAIN (var);
5916 /* We need to keep field decls associated with their trees. Otherwise tree
5917 merging may merge some fileds and keep others disjoint wich in turn will
5918 not do well with TREE_CHAIN pointers linking them.
5920 Also do not drop containing types for virtual methods and tables because
5921 these are needed by devirtualization.
5922 C++ destructors are special because C++ frontends sometimes produces
5923 virtual destructor as an alias of non-virtual destructor. In
5924 devirutalization code we always walk through aliases and we need
5925 context to be preserved too. See PR89335 */
5926 if (TREE_CODE (decl) != FIELD_DECL
5927 && ((TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
5928 || (!DECL_VIRTUAL_P (decl)
5929 && (TREE_CODE (decl) != FUNCTION_DECL
5930 || !DECL_CXX_DESTRUCTOR_P (decl)))))
5931 DECL_CONTEXT (decl) = fld_decl_context (DECL_CONTEXT (decl));
5935 /* Operand callback helper for free_lang_data_in_node. *TP is the
5936 subtree operand being considered. */
5938 static tree
5939 find_decls_types_r (tree *tp, int *ws, void *data)
5941 tree t = *tp;
5942 class free_lang_data_d *fld = (class free_lang_data_d *) data;
5944 if (TREE_CODE (t) == TREE_LIST)
5945 return NULL_TREE;
5947 /* Language specific nodes will be removed, so there is no need
5948 to gather anything under them. */
5949 if (is_lang_specific (t))
5951 *ws = 0;
5952 return NULL_TREE;
5955 if (DECL_P (t))
5957 /* Note that walk_tree does not traverse every possible field in
5958 decls, so we have to do our own traversals here. */
5959 add_tree_to_fld_list (t, fld);
5961 fld_worklist_push (DECL_NAME (t), fld);
5962 fld_worklist_push (DECL_CONTEXT (t), fld);
5963 fld_worklist_push (DECL_SIZE (t), fld);
5964 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
5966 /* We are going to remove everything under DECL_INITIAL for
5967 TYPE_DECLs. No point walking them. */
5968 if (TREE_CODE (t) != TYPE_DECL)
5969 fld_worklist_push (DECL_INITIAL (t), fld);
5971 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
5972 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
5974 if (TREE_CODE (t) == FUNCTION_DECL)
5976 fld_worklist_push (DECL_ARGUMENTS (t), fld);
5977 fld_worklist_push (DECL_RESULT (t), fld);
5979 else if (TREE_CODE (t) == FIELD_DECL)
5981 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
5982 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
5983 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
5984 fld_worklist_push (DECL_FCONTEXT (t), fld);
5987 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
5988 && DECL_HAS_VALUE_EXPR_P (t))
5989 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
5991 if (TREE_CODE (t) != FIELD_DECL
5992 && TREE_CODE (t) != TYPE_DECL)
5993 fld_worklist_push (TREE_CHAIN (t), fld);
5994 *ws = 0;
5996 else if (TYPE_P (t))
5998 /* Note that walk_tree does not traverse every possible field in
5999 types, so we have to do our own traversals here. */
6000 add_tree_to_fld_list (t, fld);
6002 if (!RECORD_OR_UNION_TYPE_P (t))
6003 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
6004 fld_worklist_push (TYPE_SIZE (t), fld);
6005 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
6006 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
6007 fld_worklist_push (TYPE_POINTER_TO (t), fld);
6008 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
6009 fld_worklist_push (TYPE_NAME (t), fld);
6010 /* While we do not stream TYPE_POINTER_TO and TYPE_REFERENCE_TO
6011 lists, we may look types up in these lists and use them while
6012 optimizing the function body. Thus we need to free lang data
6013 in them. */
6014 if (TREE_CODE (t) == POINTER_TYPE)
6015 fld_worklist_push (TYPE_NEXT_PTR_TO (t), fld);
6016 if (TREE_CODE (t) == REFERENCE_TYPE)
6017 fld_worklist_push (TYPE_NEXT_REF_TO (t), fld);
6018 if (!POINTER_TYPE_P (t))
6019 fld_worklist_push (TYPE_MIN_VALUE_RAW (t), fld);
6020 /* TYPE_MAX_VALUE_RAW is TYPE_BINFO for record types. */
6021 if (!RECORD_OR_UNION_TYPE_P (t))
6022 fld_worklist_push (TYPE_MAX_VALUE_RAW (t), fld);
6023 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
6024 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
6025 do not and want not to reach unused variants this way. */
6026 if (TYPE_CONTEXT (t))
6028 tree ctx = TYPE_CONTEXT (t);
6029 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
6030 So push that instead. */
6031 while (ctx && TREE_CODE (ctx) == BLOCK)
6032 ctx = BLOCK_SUPERCONTEXT (ctx);
6033 fld_worklist_push (ctx, fld);
6035 fld_worklist_push (TYPE_CANONICAL (t), fld);
6037 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
6039 unsigned i;
6040 tree tem;
6041 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
6042 fld_worklist_push (TREE_TYPE (tem), fld);
6043 fld_worklist_push (BINFO_TYPE (TYPE_BINFO (t)), fld);
6044 fld_worklist_push (BINFO_VTABLE (TYPE_BINFO (t)), fld);
6046 if (RECORD_OR_UNION_TYPE_P (t))
6048 tree tem;
6049 /* Push all TYPE_FIELDS - there can be interleaving interesting
6050 and non-interesting things. */
6051 tem = TYPE_FIELDS (t);
6052 while (tem)
6054 if (TREE_CODE (tem) == FIELD_DECL)
6055 fld_worklist_push (tem, fld);
6056 tem = TREE_CHAIN (tem);
6059 if (FUNC_OR_METHOD_TYPE_P (t))
6060 fld_worklist_push (TYPE_METHOD_BASETYPE (t), fld);
6062 fld_worklist_push (TYPE_STUB_DECL (t), fld);
6063 *ws = 0;
6065 else if (TREE_CODE (t) == BLOCK)
6067 for (tree *tem = &BLOCK_VARS (t); *tem; )
6069 if (TREE_CODE (*tem) != LABEL_DECL
6070 && (TREE_CODE (*tem) != VAR_DECL
6071 || !auto_var_in_fn_p (*tem, DECL_CONTEXT (*tem))))
6073 gcc_assert (TREE_CODE (*tem) != RESULT_DECL
6074 && TREE_CODE (*tem) != PARM_DECL);
6075 *tem = TREE_CHAIN (*tem);
6077 else
6079 fld_worklist_push (*tem, fld);
6080 tem = &TREE_CHAIN (*tem);
6083 for (tree tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
6084 fld_worklist_push (tem, fld);
6085 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
6088 if (TREE_CODE (t) != IDENTIFIER_NODE
6089 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
6090 fld_worklist_push (TREE_TYPE (t), fld);
6092 return NULL_TREE;
6096 /* Find decls and types in T. */
6098 static void
6099 find_decls_types (tree t, class free_lang_data_d *fld)
6101 while (1)
6103 if (!fld->pset.contains (t))
6104 walk_tree (&t, find_decls_types_r, fld, &fld->pset);
6105 if (fld->worklist.is_empty ())
6106 break;
6107 t = fld->worklist.pop ();
6111 /* Translate all the types in LIST with the corresponding runtime
6112 types. */
6114 static tree
6115 get_eh_types_for_runtime (tree list)
6117 tree head, prev;
6119 if (list == NULL_TREE)
6120 return NULL_TREE;
6122 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
6123 prev = head;
6124 list = TREE_CHAIN (list);
6125 while (list)
6127 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
6128 TREE_CHAIN (prev) = n;
6129 prev = TREE_CHAIN (prev);
6130 list = TREE_CHAIN (list);
6133 return head;
6137 /* Find decls and types referenced in EH region R and store them in
6138 FLD->DECLS and FLD->TYPES. */
6140 static void
6141 find_decls_types_in_eh_region (eh_region r, class free_lang_data_d *fld)
6143 switch (r->type)
6145 case ERT_CLEANUP:
6146 break;
6148 case ERT_TRY:
6150 eh_catch c;
6152 /* The types referenced in each catch must first be changed to the
6153 EH types used at runtime. This removes references to FE types
6154 in the region. */
6155 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
6157 c->type_list = get_eh_types_for_runtime (c->type_list);
6158 walk_tree (&c->type_list, find_decls_types_r, fld, &fld->pset);
6161 break;
6163 case ERT_ALLOWED_EXCEPTIONS:
6164 r->u.allowed.type_list
6165 = get_eh_types_for_runtime (r->u.allowed.type_list);
6166 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, &fld->pset);
6167 break;
6169 case ERT_MUST_NOT_THROW:
6170 walk_tree (&r->u.must_not_throw.failure_decl,
6171 find_decls_types_r, fld, &fld->pset);
6172 break;
6177 /* Find decls and types referenced in cgraph node N and store them in
6178 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
6179 look for *every* kind of DECL and TYPE node reachable from N,
6180 including those embedded inside types and decls (i.e,, TYPE_DECLs,
6181 NAMESPACE_DECLs, etc). */
6183 static void
6184 find_decls_types_in_node (struct cgraph_node *n, class free_lang_data_d *fld)
6186 basic_block bb;
6187 struct function *fn;
6188 unsigned ix;
6189 tree t;
6191 find_decls_types (n->decl, fld);
6193 if (!gimple_has_body_p (n->decl))
6194 return;
6196 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
6198 fn = DECL_STRUCT_FUNCTION (n->decl);
6200 /* Traverse locals. */
6201 FOR_EACH_LOCAL_DECL (fn, ix, t)
6202 find_decls_types (t, fld);
6204 /* Traverse EH regions in FN. */
6206 eh_region r;
6207 FOR_ALL_EH_REGION_FN (r, fn)
6208 find_decls_types_in_eh_region (r, fld);
6211 /* Traverse every statement in FN. */
6212 FOR_EACH_BB_FN (bb, fn)
6214 gphi_iterator psi;
6215 gimple_stmt_iterator si;
6216 unsigned i;
6218 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
6220 gphi *phi = psi.phi ();
6222 for (i = 0; i < gimple_phi_num_args (phi); i++)
6224 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
6225 find_decls_types (*arg_p, fld);
6229 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6231 gimple *stmt = gsi_stmt (si);
6233 if (is_gimple_call (stmt))
6234 find_decls_types (gimple_call_fntype (stmt), fld);
6236 for (i = 0; i < gimple_num_ops (stmt); i++)
6238 tree arg = gimple_op (stmt, i);
6239 find_decls_types (arg, fld);
6240 /* find_decls_types doesn't walk TREE_PURPOSE of TREE_LISTs,
6241 which we need for asm stmts. */
6242 if (arg
6243 && TREE_CODE (arg) == TREE_LIST
6244 && TREE_PURPOSE (arg)
6245 && gimple_code (stmt) == GIMPLE_ASM)
6246 find_decls_types (TREE_PURPOSE (arg), fld);
6253 /* Find decls and types referenced in varpool node N and store them in
6254 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
6255 look for *every* kind of DECL and TYPE node reachable from N,
6256 including those embedded inside types and decls (i.e,, TYPE_DECLs,
6257 NAMESPACE_DECLs, etc). */
6259 static void
6260 find_decls_types_in_var (varpool_node *v, class free_lang_data_d *fld)
6262 find_decls_types (v->decl, fld);
6265 /* If T needs an assembler name, have one created for it. */
6267 void
6268 assign_assembler_name_if_needed (tree t)
6270 if (need_assembler_name_p (t))
6272 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
6273 diagnostics that use input_location to show locus
6274 information. The problem here is that, at this point,
6275 input_location is generally anchored to the end of the file
6276 (since the parser is long gone), so we don't have a good
6277 position to pin it to.
6279 To alleviate this problem, this uses the location of T's
6280 declaration. Examples of this are
6281 testsuite/g++.dg/template/cond2.C and
6282 testsuite/g++.dg/template/pr35240.C. */
6283 location_t saved_location = input_location;
6284 input_location = DECL_SOURCE_LOCATION (t);
6286 decl_assembler_name (t);
6288 input_location = saved_location;
6293 /* Free language specific information for every operand and expression
6294 in every node of the call graph. This process operates in three stages:
6296 1- Every callgraph node and varpool node is traversed looking for
6297 decls and types embedded in them. This is a more exhaustive
6298 search than that done by find_referenced_vars, because it will
6299 also collect individual fields, decls embedded in types, etc.
6301 2- All the decls found are sent to free_lang_data_in_decl.
6303 3- All the types found are sent to free_lang_data_in_type.
6305 The ordering between decls and types is important because
6306 free_lang_data_in_decl sets assembler names, which includes
6307 mangling. So types cannot be freed up until assembler names have
6308 been set up. */
6310 static void
6311 free_lang_data_in_cgraph (class free_lang_data_d *fld)
6313 struct cgraph_node *n;
6314 varpool_node *v;
6315 tree t;
6316 unsigned i;
6317 alias_pair *p;
6319 /* Find decls and types in the body of every function in the callgraph. */
6320 FOR_EACH_FUNCTION (n)
6321 find_decls_types_in_node (n, fld);
6323 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
6324 find_decls_types (p->decl, fld);
6326 /* Find decls and types in every varpool symbol. */
6327 FOR_EACH_VARIABLE (v)
6328 find_decls_types_in_var (v, fld);
6330 /* Set the assembler name on every decl found. We need to do this
6331 now because free_lang_data_in_decl will invalidate data needed
6332 for mangling. This breaks mangling on interdependent decls. */
6333 FOR_EACH_VEC_ELT (fld->decls, i, t)
6334 assign_assembler_name_if_needed (t);
6336 /* Traverse every decl found freeing its language data. */
6337 FOR_EACH_VEC_ELT (fld->decls, i, t)
6338 free_lang_data_in_decl (t, fld);
6340 /* Traverse every type found freeing its language data. */
6341 FOR_EACH_VEC_ELT (fld->types, i, t)
6342 free_lang_data_in_type (t, fld);
6346 /* Free resources that are used by FE but are not needed once they are done. */
6348 static unsigned
6349 free_lang_data (void)
6351 unsigned i;
6352 class free_lang_data_d fld;
6354 /* If we are the LTO frontend we have freed lang-specific data already. */
6355 if (in_lto_p
6356 || (!flag_generate_lto && !flag_generate_offload))
6358 /* Rebuild type inheritance graph even when not doing LTO to get
6359 consistent profile data. */
6360 rebuild_type_inheritance_graph ();
6361 return 0;
6364 fld_incomplete_types = new hash_map<tree, tree>;
6365 fld_simplified_types = new hash_map<tree, tree>;
6367 /* Provide a dummy TRANSLATION_UNIT_DECL if the FE failed to provide one. */
6368 if (vec_safe_is_empty (all_translation_units))
6369 build_translation_unit_decl (NULL_TREE);
6371 /* Allocate and assign alias sets to the standard integer types
6372 while the slots are still in the way the frontends generated them. */
6373 for (i = 0; i < itk_none; ++i)
6374 if (integer_types[i])
6375 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
6377 /* Traverse the IL resetting language specific information for
6378 operands, expressions, etc. */
6379 free_lang_data_in_cgraph (&fld);
6381 /* Create gimple variants for common types. */
6382 for (unsigned i = 0;
6383 i < sizeof (builtin_structptr_types) / sizeof (builtin_structptr_type);
6384 ++i)
6385 builtin_structptr_types[i].node = builtin_structptr_types[i].base;
6387 /* Reset some langhooks. Do not reset types_compatible_p, it may
6388 still be used indirectly via the get_alias_set langhook. */
6389 lang_hooks.dwarf_name = lhd_dwarf_name;
6390 lang_hooks.decl_printable_name = gimple_decl_printable_name;
6391 lang_hooks.gimplify_expr = lhd_gimplify_expr;
6392 lang_hooks.overwrite_decl_assembler_name = lhd_overwrite_decl_assembler_name;
6393 lang_hooks.print_xnode = lhd_print_tree_nothing;
6394 lang_hooks.print_decl = lhd_print_tree_nothing;
6395 lang_hooks.print_type = lhd_print_tree_nothing;
6396 lang_hooks.print_identifier = lhd_print_tree_nothing;
6398 lang_hooks.tree_inlining.var_mod_type_p = hook_bool_tree_tree_false;
6400 if (flag_checking)
6402 int i;
6403 tree t;
6405 FOR_EACH_VEC_ELT (fld.types, i, t)
6406 verify_type (t);
6409 /* We do not want the default decl_assembler_name implementation,
6410 rather if we have fixed everything we want a wrapper around it
6411 asserting that all non-local symbols already got their assembler
6412 name and only produce assembler names for local symbols. Or rather
6413 make sure we never call decl_assembler_name on local symbols and
6414 devise a separate, middle-end private scheme for it. */
6416 /* Reset diagnostic machinery. */
6417 tree_diagnostics_defaults (global_dc);
6419 rebuild_type_inheritance_graph ();
6421 delete fld_incomplete_types;
6422 delete fld_simplified_types;
6424 return 0;
6428 namespace {
6430 const pass_data pass_data_ipa_free_lang_data =
6432 SIMPLE_IPA_PASS, /* type */
6433 "*free_lang_data", /* name */
6434 OPTGROUP_NONE, /* optinfo_flags */
6435 TV_IPA_FREE_LANG_DATA, /* tv_id */
6436 0, /* properties_required */
6437 0, /* properties_provided */
6438 0, /* properties_destroyed */
6439 0, /* todo_flags_start */
6440 0, /* todo_flags_finish */
6443 class pass_ipa_free_lang_data : public simple_ipa_opt_pass
6445 public:
6446 pass_ipa_free_lang_data (gcc::context *ctxt)
6447 : simple_ipa_opt_pass (pass_data_ipa_free_lang_data, ctxt)
6450 /* opt_pass methods: */
6451 virtual unsigned int execute (function *) { return free_lang_data (); }
6453 }; // class pass_ipa_free_lang_data
6455 } // anon namespace
6457 simple_ipa_opt_pass *
6458 make_pass_ipa_free_lang_data (gcc::context *ctxt)
6460 return new pass_ipa_free_lang_data (ctxt);
6463 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
6464 of the various TYPE_QUAL values. */
6466 static void
6467 set_type_quals (tree type, int type_quals)
6469 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
6470 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
6471 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
6472 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
6473 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
6476 /* Returns true iff CAND and BASE have equivalent language-specific
6477 qualifiers. */
6479 bool
6480 check_lang_type (const_tree cand, const_tree base)
6482 if (lang_hooks.types.type_hash_eq == NULL)
6483 return true;
6484 /* type_hash_eq currently only applies to these types. */
6485 if (TREE_CODE (cand) != FUNCTION_TYPE
6486 && TREE_CODE (cand) != METHOD_TYPE)
6487 return true;
6488 return lang_hooks.types.type_hash_eq (cand, base);
6491 /* This function checks to see if TYPE matches the size one of the built-in
6492 atomic types, and returns that core atomic type. */
6494 static tree
6495 find_atomic_core_type (const_tree type)
6497 tree base_atomic_type;
6499 /* Only handle complete types. */
6500 if (!tree_fits_uhwi_p (TYPE_SIZE (type)))
6501 return NULL_TREE;
6503 switch (tree_to_uhwi (TYPE_SIZE (type)))
6505 case 8:
6506 base_atomic_type = atomicQI_type_node;
6507 break;
6509 case 16:
6510 base_atomic_type = atomicHI_type_node;
6511 break;
6513 case 32:
6514 base_atomic_type = atomicSI_type_node;
6515 break;
6517 case 64:
6518 base_atomic_type = atomicDI_type_node;
6519 break;
6521 case 128:
6522 base_atomic_type = atomicTI_type_node;
6523 break;
6525 default:
6526 base_atomic_type = NULL_TREE;
6529 return base_atomic_type;
6532 /* Returns true iff unqualified CAND and BASE are equivalent. */
6534 bool
6535 check_base_type (const_tree cand, const_tree base)
6537 if (TYPE_NAME (cand) != TYPE_NAME (base)
6538 /* Apparently this is needed for Objective-C. */
6539 || TYPE_CONTEXT (cand) != TYPE_CONTEXT (base)
6540 || !attribute_list_equal (TYPE_ATTRIBUTES (cand),
6541 TYPE_ATTRIBUTES (base)))
6542 return false;
6543 /* Check alignment. */
6544 if (TYPE_ALIGN (cand) == TYPE_ALIGN (base))
6545 return true;
6546 /* Atomic types increase minimal alignment. We must to do so as well
6547 or we get duplicated canonical types. See PR88686. */
6548 if ((TYPE_QUALS (cand) & TYPE_QUAL_ATOMIC))
6550 /* See if this object can map to a basic atomic type. */
6551 tree atomic_type = find_atomic_core_type (cand);
6552 if (atomic_type && TYPE_ALIGN (atomic_type) == TYPE_ALIGN (cand))
6553 return true;
6555 return false;
6558 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
6560 bool
6561 check_qualified_type (const_tree cand, const_tree base, int type_quals)
6563 return (TYPE_QUALS (cand) == type_quals
6564 && check_base_type (cand, base)
6565 && check_lang_type (cand, base));
6568 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
6570 static bool
6571 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
6573 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
6574 && TYPE_NAME (cand) == TYPE_NAME (base)
6575 /* Apparently this is needed for Objective-C. */
6576 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6577 /* Check alignment. */
6578 && TYPE_ALIGN (cand) == align
6579 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6580 TYPE_ATTRIBUTES (base))
6581 && check_lang_type (cand, base));
6584 /* Return a version of the TYPE, qualified as indicated by the
6585 TYPE_QUALS, if one exists. If no qualified version exists yet,
6586 return NULL_TREE. */
6588 tree
6589 get_qualified_type (tree type, int type_quals)
6591 if (TYPE_QUALS (type) == type_quals)
6592 return type;
6594 tree mv = TYPE_MAIN_VARIANT (type);
6595 if (check_qualified_type (mv, type, type_quals))
6596 return mv;
6598 /* Search the chain of variants to see if there is already one there just
6599 like the one we need to have. If so, use that existing one. We must
6600 preserve the TYPE_NAME, since there is code that depends on this. */
6601 for (tree *tp = &TYPE_NEXT_VARIANT (mv); *tp; tp = &TYPE_NEXT_VARIANT (*tp))
6602 if (check_qualified_type (*tp, type, type_quals))
6604 /* Put the found variant at the head of the variant list so
6605 frequently searched variants get found faster. The C++ FE
6606 benefits greatly from this. */
6607 tree t = *tp;
6608 *tp = TYPE_NEXT_VARIANT (t);
6609 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
6610 TYPE_NEXT_VARIANT (mv) = t;
6611 return t;
6614 return NULL_TREE;
6617 /* Like get_qualified_type, but creates the type if it does not
6618 exist. This function never returns NULL_TREE. */
6620 tree
6621 build_qualified_type (tree type, int type_quals MEM_STAT_DECL)
6623 tree t;
6625 /* See if we already have the appropriate qualified variant. */
6626 t = get_qualified_type (type, type_quals);
6628 /* If not, build it. */
6629 if (!t)
6631 t = build_variant_type_copy (type PASS_MEM_STAT);
6632 set_type_quals (t, type_quals);
6634 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
6636 /* See if this object can map to a basic atomic type. */
6637 tree atomic_type = find_atomic_core_type (type);
6638 if (atomic_type)
6640 /* Ensure the alignment of this type is compatible with
6641 the required alignment of the atomic type. */
6642 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
6643 SET_TYPE_ALIGN (t, TYPE_ALIGN (atomic_type));
6647 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6648 /* Propagate structural equality. */
6649 SET_TYPE_STRUCTURAL_EQUALITY (t);
6650 else if (TYPE_CANONICAL (type) != type)
6651 /* Build the underlying canonical type, since it is different
6652 from TYPE. */
6654 tree c = build_qualified_type (TYPE_CANONICAL (type), type_quals);
6655 TYPE_CANONICAL (t) = TYPE_CANONICAL (c);
6657 else
6658 /* T is its own canonical type. */
6659 TYPE_CANONICAL (t) = t;
6663 return t;
6666 /* Create a variant of type T with alignment ALIGN. */
6668 tree
6669 build_aligned_type (tree type, unsigned int align)
6671 tree t;
6673 if (TYPE_PACKED (type)
6674 || TYPE_ALIGN (type) == align)
6675 return type;
6677 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6678 if (check_aligned_type (t, type, align))
6679 return t;
6681 t = build_variant_type_copy (type);
6682 SET_TYPE_ALIGN (t, align);
6683 TYPE_USER_ALIGN (t) = 1;
6685 return t;
6688 /* Create a new distinct copy of TYPE. The new type is made its own
6689 MAIN_VARIANT. If TYPE requires structural equality checks, the
6690 resulting type requires structural equality checks; otherwise, its
6691 TYPE_CANONICAL points to itself. */
6693 tree
6694 build_distinct_type_copy (tree type MEM_STAT_DECL)
6696 tree t = copy_node (type PASS_MEM_STAT);
6698 TYPE_POINTER_TO (t) = 0;
6699 TYPE_REFERENCE_TO (t) = 0;
6701 /* Set the canonical type either to a new equivalence class, or
6702 propagate the need for structural equality checks. */
6703 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6704 SET_TYPE_STRUCTURAL_EQUALITY (t);
6705 else
6706 TYPE_CANONICAL (t) = t;
6708 /* Make it its own variant. */
6709 TYPE_MAIN_VARIANT (t) = t;
6710 TYPE_NEXT_VARIANT (t) = 0;
6712 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
6713 whose TREE_TYPE is not t. This can also happen in the Ada
6714 frontend when using subtypes. */
6716 return t;
6719 /* Create a new variant of TYPE, equivalent but distinct. This is so
6720 the caller can modify it. TYPE_CANONICAL for the return type will
6721 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
6722 are considered equal by the language itself (or that both types
6723 require structural equality checks). */
6725 tree
6726 build_variant_type_copy (tree type MEM_STAT_DECL)
6728 tree t, m = TYPE_MAIN_VARIANT (type);
6730 t = build_distinct_type_copy (type PASS_MEM_STAT);
6732 /* Since we're building a variant, assume that it is a non-semantic
6733 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
6734 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
6735 /* Type variants have no alias set defined. */
6736 TYPE_ALIAS_SET (t) = -1;
6738 /* Add the new type to the chain of variants of TYPE. */
6739 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
6740 TYPE_NEXT_VARIANT (m) = t;
6741 TYPE_MAIN_VARIANT (t) = m;
6743 return t;
6746 /* Return true if the from tree in both tree maps are equal. */
6749 tree_map_base_eq (const void *va, const void *vb)
6751 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6752 *const b = (const struct tree_map_base *) vb;
6753 return (a->from == b->from);
6756 /* Hash a from tree in a tree_base_map. */
6758 unsigned int
6759 tree_map_base_hash (const void *item)
6761 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6764 /* Return true if this tree map structure is marked for garbage collection
6765 purposes. We simply return true if the from tree is marked, so that this
6766 structure goes away when the from tree goes away. */
6769 tree_map_base_marked_p (const void *p)
6771 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6774 /* Hash a from tree in a tree_map. */
6776 unsigned int
6777 tree_map_hash (const void *item)
6779 return (((const struct tree_map *) item)->hash);
6782 /* Hash a from tree in a tree_decl_map. */
6784 unsigned int
6785 tree_decl_map_hash (const void *item)
6787 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6790 /* Return the initialization priority for DECL. */
6792 priority_type
6793 decl_init_priority_lookup (tree decl)
6795 symtab_node *snode = symtab_node::get (decl);
6797 if (!snode)
6798 return DEFAULT_INIT_PRIORITY;
6799 return
6800 snode->get_init_priority ();
6803 /* Return the finalization priority for DECL. */
6805 priority_type
6806 decl_fini_priority_lookup (tree decl)
6808 cgraph_node *node = cgraph_node::get (decl);
6810 if (!node)
6811 return DEFAULT_INIT_PRIORITY;
6812 return
6813 node->get_fini_priority ();
6816 /* Set the initialization priority for DECL to PRIORITY. */
6818 void
6819 decl_init_priority_insert (tree decl, priority_type priority)
6821 struct symtab_node *snode;
6823 if (priority == DEFAULT_INIT_PRIORITY)
6825 snode = symtab_node::get (decl);
6826 if (!snode)
6827 return;
6829 else if (VAR_P (decl))
6830 snode = varpool_node::get_create (decl);
6831 else
6832 snode = cgraph_node::get_create (decl);
6833 snode->set_init_priority (priority);
6836 /* Set the finalization priority for DECL to PRIORITY. */
6838 void
6839 decl_fini_priority_insert (tree decl, priority_type priority)
6841 struct cgraph_node *node;
6843 if (priority == DEFAULT_INIT_PRIORITY)
6845 node = cgraph_node::get (decl);
6846 if (!node)
6847 return;
6849 else
6850 node = cgraph_node::get_create (decl);
6851 node->set_fini_priority (priority);
6854 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6856 static void
6857 print_debug_expr_statistics (void)
6859 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6860 (long) debug_expr_for_decl->size (),
6861 (long) debug_expr_for_decl->elements (),
6862 debug_expr_for_decl->collisions ());
6865 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6867 static void
6868 print_value_expr_statistics (void)
6870 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6871 (long) value_expr_for_decl->size (),
6872 (long) value_expr_for_decl->elements (),
6873 value_expr_for_decl->collisions ());
6876 /* Lookup a debug expression for FROM, and return it if we find one. */
6878 tree
6879 decl_debug_expr_lookup (tree from)
6881 struct tree_decl_map *h, in;
6882 in.base.from = from;
6884 h = debug_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6885 if (h)
6886 return h->to;
6887 return NULL_TREE;
6890 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6892 void
6893 decl_debug_expr_insert (tree from, tree to)
6895 struct tree_decl_map *h;
6897 h = ggc_alloc<tree_decl_map> ();
6898 h->base.from = from;
6899 h->to = to;
6900 *debug_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6903 /* Lookup a value expression for FROM, and return it if we find one. */
6905 tree
6906 decl_value_expr_lookup (tree from)
6908 struct tree_decl_map *h, in;
6909 in.base.from = from;
6911 h = value_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6912 if (h)
6913 return h->to;
6914 return NULL_TREE;
6917 /* Insert a mapping FROM->TO in the value expression hashtable. */
6919 void
6920 decl_value_expr_insert (tree from, tree to)
6922 struct tree_decl_map *h;
6924 h = ggc_alloc<tree_decl_map> ();
6925 h->base.from = from;
6926 h->to = to;
6927 *value_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6930 /* Lookup a vector of debug arguments for FROM, and return it if we
6931 find one. */
6933 vec<tree, va_gc> **
6934 decl_debug_args_lookup (tree from)
6936 struct tree_vec_map *h, in;
6938 if (!DECL_HAS_DEBUG_ARGS_P (from))
6939 return NULL;
6940 gcc_checking_assert (debug_args_for_decl != NULL);
6941 in.base.from = from;
6942 h = debug_args_for_decl->find_with_hash (&in, DECL_UID (from));
6943 if (h)
6944 return &h->to;
6945 return NULL;
6948 /* Insert a mapping FROM->empty vector of debug arguments in the value
6949 expression hashtable. */
6951 vec<tree, va_gc> **
6952 decl_debug_args_insert (tree from)
6954 struct tree_vec_map *h;
6955 tree_vec_map **loc;
6957 if (DECL_HAS_DEBUG_ARGS_P (from))
6958 return decl_debug_args_lookup (from);
6959 if (debug_args_for_decl == NULL)
6960 debug_args_for_decl = hash_table<tree_vec_map_cache_hasher>::create_ggc (64);
6961 h = ggc_alloc<tree_vec_map> ();
6962 h->base.from = from;
6963 h->to = NULL;
6964 loc = debug_args_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT);
6965 *loc = h;
6966 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6967 return &h->to;
6970 /* Hashing of types so that we don't make duplicates.
6971 The entry point is `type_hash_canon'. */
6973 /* Generate the default hash code for TYPE. This is designed for
6974 speed, rather than maximum entropy. */
6976 hashval_t
6977 type_hash_canon_hash (tree type)
6979 inchash::hash hstate;
6981 hstate.add_int (TREE_CODE (type));
6983 if (TREE_TYPE (type))
6984 hstate.add_object (TYPE_HASH (TREE_TYPE (type)));
6986 for (tree t = TYPE_ATTRIBUTES (type); t; t = TREE_CHAIN (t))
6987 /* Just the identifier is adequate to distinguish. */
6988 hstate.add_object (IDENTIFIER_HASH_VALUE (get_attribute_name (t)));
6990 switch (TREE_CODE (type))
6992 case METHOD_TYPE:
6993 hstate.add_object (TYPE_HASH (TYPE_METHOD_BASETYPE (type)));
6994 /* FALLTHROUGH. */
6995 case FUNCTION_TYPE:
6996 for (tree t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6997 if (TREE_VALUE (t) != error_mark_node)
6998 hstate.add_object (TYPE_HASH (TREE_VALUE (t)));
6999 break;
7001 case OFFSET_TYPE:
7002 hstate.add_object (TYPE_HASH (TYPE_OFFSET_BASETYPE (type)));
7003 break;
7005 case ARRAY_TYPE:
7007 if (TYPE_DOMAIN (type))
7008 hstate.add_object (TYPE_HASH (TYPE_DOMAIN (type)));
7009 if (!AGGREGATE_TYPE_P (TREE_TYPE (type)))
7011 unsigned typeless = TYPE_TYPELESS_STORAGE (type);
7012 hstate.add_object (typeless);
7015 break;
7017 case INTEGER_TYPE:
7019 tree t = TYPE_MAX_VALUE (type);
7020 if (!t)
7021 t = TYPE_MIN_VALUE (type);
7022 for (int i = 0; i < TREE_INT_CST_NUNITS (t); i++)
7023 hstate.add_object (TREE_INT_CST_ELT (t, i));
7024 break;
7027 case REAL_TYPE:
7028 case FIXED_POINT_TYPE:
7030 unsigned prec = TYPE_PRECISION (type);
7031 hstate.add_object (prec);
7032 break;
7035 case VECTOR_TYPE:
7036 hstate.add_poly_int (TYPE_VECTOR_SUBPARTS (type));
7037 break;
7039 default:
7040 break;
7043 return hstate.end ();
7046 /* These are the Hashtable callback functions. */
7048 /* Returns true iff the types are equivalent. */
7050 bool
7051 type_cache_hasher::equal (type_hash *a, type_hash *b)
7053 /* First test the things that are the same for all types. */
7054 if (a->hash != b->hash
7055 || TREE_CODE (a->type) != TREE_CODE (b->type)
7056 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
7057 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
7058 TYPE_ATTRIBUTES (b->type))
7059 || (TREE_CODE (a->type) != COMPLEX_TYPE
7060 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
7061 return 0;
7063 /* Be careful about comparing arrays before and after the element type
7064 has been completed; don't compare TYPE_ALIGN unless both types are
7065 complete. */
7066 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
7067 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
7068 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
7069 return 0;
7071 switch (TREE_CODE (a->type))
7073 case VOID_TYPE:
7074 case COMPLEX_TYPE:
7075 case POINTER_TYPE:
7076 case REFERENCE_TYPE:
7077 case NULLPTR_TYPE:
7078 return 1;
7080 case VECTOR_TYPE:
7081 return known_eq (TYPE_VECTOR_SUBPARTS (a->type),
7082 TYPE_VECTOR_SUBPARTS (b->type));
7084 case ENUMERAL_TYPE:
7085 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
7086 && !(TYPE_VALUES (a->type)
7087 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
7088 && TYPE_VALUES (b->type)
7089 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
7090 && type_list_equal (TYPE_VALUES (a->type),
7091 TYPE_VALUES (b->type))))
7092 return 0;
7094 /* fall through */
7096 case INTEGER_TYPE:
7097 case REAL_TYPE:
7098 case BOOLEAN_TYPE:
7099 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
7100 return false;
7101 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
7102 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
7103 TYPE_MAX_VALUE (b->type)))
7104 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
7105 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
7106 TYPE_MIN_VALUE (b->type))));
7108 case FIXED_POINT_TYPE:
7109 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
7111 case OFFSET_TYPE:
7112 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
7114 case METHOD_TYPE:
7115 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
7116 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
7117 || (TYPE_ARG_TYPES (a->type)
7118 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
7119 && TYPE_ARG_TYPES (b->type)
7120 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
7121 && type_list_equal (TYPE_ARG_TYPES (a->type),
7122 TYPE_ARG_TYPES (b->type)))))
7123 break;
7124 return 0;
7125 case ARRAY_TYPE:
7126 /* Don't compare TYPE_TYPELESS_STORAGE flag on aggregates,
7127 where the flag should be inherited from the element type
7128 and can change after ARRAY_TYPEs are created; on non-aggregates
7129 compare it and hash it, scalars will never have that flag set
7130 and we need to differentiate between arrays created by different
7131 front-ends or middle-end created arrays. */
7132 return (TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type)
7133 && (AGGREGATE_TYPE_P (TREE_TYPE (a->type))
7134 || (TYPE_TYPELESS_STORAGE (a->type)
7135 == TYPE_TYPELESS_STORAGE (b->type))));
7137 case RECORD_TYPE:
7138 case UNION_TYPE:
7139 case QUAL_UNION_TYPE:
7140 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
7141 || (TYPE_FIELDS (a->type)
7142 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
7143 && TYPE_FIELDS (b->type)
7144 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
7145 && type_list_equal (TYPE_FIELDS (a->type),
7146 TYPE_FIELDS (b->type))));
7148 case FUNCTION_TYPE:
7149 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
7150 || (TYPE_ARG_TYPES (a->type)
7151 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
7152 && TYPE_ARG_TYPES (b->type)
7153 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
7154 && type_list_equal (TYPE_ARG_TYPES (a->type),
7155 TYPE_ARG_TYPES (b->type))))
7156 break;
7157 return 0;
7159 default:
7160 return 0;
7163 if (lang_hooks.types.type_hash_eq != NULL)
7164 return lang_hooks.types.type_hash_eq (a->type, b->type);
7166 return 1;
7169 /* Given TYPE, and HASHCODE its hash code, return the canonical
7170 object for an identical type if one already exists.
7171 Otherwise, return TYPE, and record it as the canonical object.
7173 To use this function, first create a type of the sort you want.
7174 Then compute its hash code from the fields of the type that
7175 make it different from other similar types.
7176 Then call this function and use the value. */
7178 tree
7179 type_hash_canon (unsigned int hashcode, tree type)
7181 type_hash in;
7182 type_hash **loc;
7184 /* The hash table only contains main variants, so ensure that's what we're
7185 being passed. */
7186 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
7188 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
7189 must call that routine before comparing TYPE_ALIGNs. */
7190 layout_type (type);
7192 in.hash = hashcode;
7193 in.type = type;
7195 loc = type_hash_table->find_slot_with_hash (&in, hashcode, INSERT);
7196 if (*loc)
7198 tree t1 = ((type_hash *) *loc)->type;
7199 gcc_assert (TYPE_MAIN_VARIANT (t1) == t1
7200 && t1 != type);
7201 if (TYPE_UID (type) + 1 == next_type_uid)
7202 --next_type_uid;
7203 /* Free also min/max values and the cache for integer
7204 types. This can't be done in free_node, as LTO frees
7205 those on its own. */
7206 if (TREE_CODE (type) == INTEGER_TYPE)
7208 if (TYPE_MIN_VALUE (type)
7209 && TREE_TYPE (TYPE_MIN_VALUE (type)) == type)
7211 /* Zero is always in TYPE_CACHED_VALUES. */
7212 if (! TYPE_UNSIGNED (type))
7213 int_cst_hash_table->remove_elt (TYPE_MIN_VALUE (type));
7214 ggc_free (TYPE_MIN_VALUE (type));
7216 if (TYPE_MAX_VALUE (type)
7217 && TREE_TYPE (TYPE_MAX_VALUE (type)) == type)
7219 int_cst_hash_table->remove_elt (TYPE_MAX_VALUE (type));
7220 ggc_free (TYPE_MAX_VALUE (type));
7222 if (TYPE_CACHED_VALUES_P (type))
7223 ggc_free (TYPE_CACHED_VALUES (type));
7225 free_node (type);
7226 return t1;
7228 else
7230 struct type_hash *h;
7232 h = ggc_alloc<type_hash> ();
7233 h->hash = hashcode;
7234 h->type = type;
7235 *loc = h;
7237 return type;
7241 static void
7242 print_type_hash_statistics (void)
7244 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
7245 (long) type_hash_table->size (),
7246 (long) type_hash_table->elements (),
7247 type_hash_table->collisions ());
7250 /* Given two lists of types
7251 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
7252 return 1 if the lists contain the same types in the same order.
7253 Also, the TREE_PURPOSEs must match. */
7255 bool
7256 type_list_equal (const_tree l1, const_tree l2)
7258 const_tree t1, t2;
7260 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7261 if (TREE_VALUE (t1) != TREE_VALUE (t2)
7262 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
7263 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
7264 && (TREE_TYPE (TREE_PURPOSE (t1))
7265 == TREE_TYPE (TREE_PURPOSE (t2))))))
7266 return false;
7268 return t1 == t2;
7271 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
7272 given by TYPE. If the argument list accepts variable arguments,
7273 then this function counts only the ordinary arguments. */
7276 type_num_arguments (const_tree fntype)
7278 int i = 0;
7280 for (tree t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
7281 /* If the function does not take a variable number of arguments,
7282 the last element in the list will have type `void'. */
7283 if (VOID_TYPE_P (TREE_VALUE (t)))
7284 break;
7285 else
7286 ++i;
7288 return i;
7291 /* Return the type of the function TYPE's argument ARGNO if known.
7292 For vararg function's where ARGNO refers to one of the variadic
7293 arguments return null. Otherwise, return a void_type_node for
7294 out-of-bounds ARGNO. */
7296 tree
7297 type_argument_type (const_tree fntype, unsigned argno)
7299 /* Treat zero the same as an out-of-bounds argument number. */
7300 if (!argno)
7301 return void_type_node;
7303 function_args_iterator iter;
7305 tree argtype;
7306 unsigned i = 1;
7307 FOREACH_FUNCTION_ARGS (fntype, argtype, iter)
7309 /* A vararg function's argument list ends in a null. Otherwise,
7310 an ordinary function's argument list ends with void. Return
7311 null if ARGNO refers to a vararg argument, void_type_node if
7312 it's out of bounds, and the formal argument type otherwise. */
7313 if (!argtype)
7314 break;
7316 if (i == argno || VOID_TYPE_P (argtype))
7317 return argtype;
7319 ++i;
7322 return NULL_TREE;
7325 /* Nonzero if integer constants T1 and T2
7326 represent the same constant value. */
7329 tree_int_cst_equal (const_tree t1, const_tree t2)
7331 if (t1 == t2)
7332 return 1;
7334 if (t1 == 0 || t2 == 0)
7335 return 0;
7337 STRIP_ANY_LOCATION_WRAPPER (t1);
7338 STRIP_ANY_LOCATION_WRAPPER (t2);
7340 if (TREE_CODE (t1) == INTEGER_CST
7341 && TREE_CODE (t2) == INTEGER_CST
7342 && wi::to_widest (t1) == wi::to_widest (t2))
7343 return 1;
7345 return 0;
7348 /* Return true if T is an INTEGER_CST whose numerical value (extended
7349 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
7351 bool
7352 tree_fits_shwi_p (const_tree t)
7354 return (t != NULL_TREE
7355 && TREE_CODE (t) == INTEGER_CST
7356 && wi::fits_shwi_p (wi::to_widest (t)));
7359 /* Return true if T is an INTEGER_CST or POLY_INT_CST whose numerical
7360 value (extended according to TYPE_UNSIGNED) fits in a poly_int64. */
7362 bool
7363 tree_fits_poly_int64_p (const_tree t)
7365 if (t == NULL_TREE)
7366 return false;
7367 if (POLY_INT_CST_P (t))
7369 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; i++)
7370 if (!wi::fits_shwi_p (wi::to_wide (POLY_INT_CST_COEFF (t, i))))
7371 return false;
7372 return true;
7374 return (TREE_CODE (t) == INTEGER_CST
7375 && wi::fits_shwi_p (wi::to_widest (t)));
7378 /* Return true if T is an INTEGER_CST whose numerical value (extended
7379 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
7381 bool
7382 tree_fits_uhwi_p (const_tree t)
7384 return (t != NULL_TREE
7385 && TREE_CODE (t) == INTEGER_CST
7386 && wi::fits_uhwi_p (wi::to_widest (t)));
7389 /* Return true if T is an INTEGER_CST or POLY_INT_CST whose numerical
7390 value (extended according to TYPE_UNSIGNED) fits in a poly_uint64. */
7392 bool
7393 tree_fits_poly_uint64_p (const_tree t)
7395 if (t == NULL_TREE)
7396 return false;
7397 if (POLY_INT_CST_P (t))
7399 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; i++)
7400 if (!wi::fits_uhwi_p (wi::to_widest (POLY_INT_CST_COEFF (t, i))))
7401 return false;
7402 return true;
7404 return (TREE_CODE (t) == INTEGER_CST
7405 && wi::fits_uhwi_p (wi::to_widest (t)));
7408 /* T is an INTEGER_CST whose numerical value (extended according to
7409 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
7410 HOST_WIDE_INT. */
7412 HOST_WIDE_INT
7413 tree_to_shwi (const_tree t)
7415 gcc_assert (tree_fits_shwi_p (t));
7416 return TREE_INT_CST_LOW (t);
7419 /* T is an INTEGER_CST whose numerical value (extended according to
7420 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
7421 HOST_WIDE_INT. */
7423 unsigned HOST_WIDE_INT
7424 tree_to_uhwi (const_tree t)
7426 gcc_assert (tree_fits_uhwi_p (t));
7427 return TREE_INT_CST_LOW (t);
7430 /* Return the most significant (sign) bit of T. */
7433 tree_int_cst_sign_bit (const_tree t)
7435 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
7437 return wi::extract_uhwi (wi::to_wide (t), bitno, 1);
7440 /* Return an indication of the sign of the integer constant T.
7441 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
7442 Note that -1 will never be returned if T's type is unsigned. */
7445 tree_int_cst_sgn (const_tree t)
7447 if (wi::to_wide (t) == 0)
7448 return 0;
7449 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
7450 return 1;
7451 else if (wi::neg_p (wi::to_wide (t)))
7452 return -1;
7453 else
7454 return 1;
7457 /* Return the minimum number of bits needed to represent VALUE in a
7458 signed or unsigned type, UNSIGNEDP says which. */
7460 unsigned int
7461 tree_int_cst_min_precision (tree value, signop sgn)
7463 /* If the value is negative, compute its negative minus 1. The latter
7464 adjustment is because the absolute value of the largest negative value
7465 is one larger than the largest positive value. This is equivalent to
7466 a bit-wise negation, so use that operation instead. */
7468 if (tree_int_cst_sgn (value) < 0)
7469 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
7471 /* Return the number of bits needed, taking into account the fact
7472 that we need one more bit for a signed than unsigned type.
7473 If value is 0 or -1, the minimum precision is 1 no matter
7474 whether unsignedp is true or false. */
7476 if (integer_zerop (value))
7477 return 1;
7478 else
7479 return tree_floor_log2 (value) + 1 + (sgn == SIGNED ? 1 : 0) ;
7482 /* Return truthvalue of whether T1 is the same tree structure as T2.
7483 Return 1 if they are the same.
7484 Return 0 if they are understandably different.
7485 Return -1 if either contains tree structure not understood by
7486 this function. */
7489 simple_cst_equal (const_tree t1, const_tree t2)
7491 enum tree_code code1, code2;
7492 int cmp;
7493 int i;
7495 if (t1 == t2)
7496 return 1;
7497 if (t1 == 0 || t2 == 0)
7498 return 0;
7500 /* For location wrappers to be the same, they must be at the same
7501 source location (and wrap the same thing). */
7502 if (location_wrapper_p (t1) && location_wrapper_p (t2))
7504 if (EXPR_LOCATION (t1) != EXPR_LOCATION (t2))
7505 return 0;
7506 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7509 code1 = TREE_CODE (t1);
7510 code2 = TREE_CODE (t2);
7512 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
7514 if (CONVERT_EXPR_CODE_P (code2)
7515 || code2 == NON_LVALUE_EXPR)
7516 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7517 else
7518 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
7521 else if (CONVERT_EXPR_CODE_P (code2)
7522 || code2 == NON_LVALUE_EXPR)
7523 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
7525 if (code1 != code2)
7526 return 0;
7528 switch (code1)
7530 case INTEGER_CST:
7531 return wi::to_widest (t1) == wi::to_widest (t2);
7533 case REAL_CST:
7534 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
7536 case FIXED_CST:
7537 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
7539 case STRING_CST:
7540 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
7541 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
7542 TREE_STRING_LENGTH (t1)));
7544 case CONSTRUCTOR:
7546 unsigned HOST_WIDE_INT idx;
7547 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
7548 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
7550 if (vec_safe_length (v1) != vec_safe_length (v2))
7551 return false;
7553 for (idx = 0; idx < vec_safe_length (v1); ++idx)
7554 /* ??? Should we handle also fields here? */
7555 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
7556 return false;
7557 return true;
7560 case SAVE_EXPR:
7561 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7563 case CALL_EXPR:
7564 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
7565 if (cmp <= 0)
7566 return cmp;
7567 if (call_expr_nargs (t1) != call_expr_nargs (t2))
7568 return 0;
7570 const_tree arg1, arg2;
7571 const_call_expr_arg_iterator iter1, iter2;
7572 for (arg1 = first_const_call_expr_arg (t1, &iter1),
7573 arg2 = first_const_call_expr_arg (t2, &iter2);
7574 arg1 && arg2;
7575 arg1 = next_const_call_expr_arg (&iter1),
7576 arg2 = next_const_call_expr_arg (&iter2))
7578 cmp = simple_cst_equal (arg1, arg2);
7579 if (cmp <= 0)
7580 return cmp;
7582 return arg1 == arg2;
7585 case TARGET_EXPR:
7586 /* Special case: if either target is an unallocated VAR_DECL,
7587 it means that it's going to be unified with whatever the
7588 TARGET_EXPR is really supposed to initialize, so treat it
7589 as being equivalent to anything. */
7590 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
7591 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
7592 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
7593 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
7594 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
7595 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
7596 cmp = 1;
7597 else
7598 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7600 if (cmp <= 0)
7601 return cmp;
7603 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
7605 case WITH_CLEANUP_EXPR:
7606 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7607 if (cmp <= 0)
7608 return cmp;
7610 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
7612 case COMPONENT_REF:
7613 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
7614 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7616 return 0;
7618 case VAR_DECL:
7619 case PARM_DECL:
7620 case CONST_DECL:
7621 case FUNCTION_DECL:
7622 return 0;
7624 default:
7625 if (POLY_INT_CST_P (t1))
7626 /* A false return means maybe_ne rather than known_ne. */
7627 return known_eq (poly_widest_int::from (poly_int_cst_value (t1),
7628 TYPE_SIGN (TREE_TYPE (t1))),
7629 poly_widest_int::from (poly_int_cst_value (t2),
7630 TYPE_SIGN (TREE_TYPE (t2))));
7631 break;
7634 /* This general rule works for most tree codes. All exceptions should be
7635 handled above. If this is a language-specific tree code, we can't
7636 trust what might be in the operand, so say we don't know
7637 the situation. */
7638 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
7639 return -1;
7641 switch (TREE_CODE_CLASS (code1))
7643 case tcc_unary:
7644 case tcc_binary:
7645 case tcc_comparison:
7646 case tcc_expression:
7647 case tcc_reference:
7648 case tcc_statement:
7649 cmp = 1;
7650 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
7652 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
7653 if (cmp <= 0)
7654 return cmp;
7657 return cmp;
7659 default:
7660 return -1;
7664 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
7665 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
7666 than U, respectively. */
7669 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
7671 if (tree_int_cst_sgn (t) < 0)
7672 return -1;
7673 else if (!tree_fits_uhwi_p (t))
7674 return 1;
7675 else if (TREE_INT_CST_LOW (t) == u)
7676 return 0;
7677 else if (TREE_INT_CST_LOW (t) < u)
7678 return -1;
7679 else
7680 return 1;
7683 /* Return true if SIZE represents a constant size that is in bounds of
7684 what the middle-end and the backend accepts (covering not more than
7685 half of the address-space).
7686 When PERR is non-null, set *PERR on failure to the description of
7687 why SIZE is not valid. */
7689 bool
7690 valid_constant_size_p (const_tree size, cst_size_error *perr /* = NULL */)
7692 if (POLY_INT_CST_P (size))
7694 if (TREE_OVERFLOW (size))
7695 return false;
7696 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
7697 if (!valid_constant_size_p (POLY_INT_CST_COEFF (size, i)))
7698 return false;
7699 return true;
7702 cst_size_error error;
7703 if (!perr)
7704 perr = &error;
7706 if (TREE_CODE (size) != INTEGER_CST)
7708 *perr = cst_size_not_constant;
7709 return false;
7712 if (TREE_OVERFLOW_P (size))
7714 *perr = cst_size_overflow;
7715 return false;
7718 if (tree_int_cst_sgn (size) < 0)
7720 *perr = cst_size_negative;
7721 return false;
7723 if (!tree_fits_uhwi_p (size)
7724 || (wi::to_widest (TYPE_MAX_VALUE (sizetype))
7725 < wi::to_widest (size) * 2))
7727 *perr = cst_size_too_big;
7728 return false;
7731 return true;
7734 /* Return the precision of the type, or for a complex or vector type the
7735 precision of the type of its elements. */
7737 unsigned int
7738 element_precision (const_tree type)
7740 if (!TYPE_P (type))
7741 type = TREE_TYPE (type);
7742 enum tree_code code = TREE_CODE (type);
7743 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
7744 type = TREE_TYPE (type);
7746 return TYPE_PRECISION (type);
7749 /* Return true if CODE represents an associative tree code. Otherwise
7750 return false. */
7751 bool
7752 associative_tree_code (enum tree_code code)
7754 switch (code)
7756 case BIT_IOR_EXPR:
7757 case BIT_AND_EXPR:
7758 case BIT_XOR_EXPR:
7759 case PLUS_EXPR:
7760 case MULT_EXPR:
7761 case MIN_EXPR:
7762 case MAX_EXPR:
7763 return true;
7765 default:
7766 break;
7768 return false;
7771 /* Return true if CODE represents a commutative tree code. Otherwise
7772 return false. */
7773 bool
7774 commutative_tree_code (enum tree_code code)
7776 switch (code)
7778 case PLUS_EXPR:
7779 case MULT_EXPR:
7780 case MULT_HIGHPART_EXPR:
7781 case MIN_EXPR:
7782 case MAX_EXPR:
7783 case BIT_IOR_EXPR:
7784 case BIT_XOR_EXPR:
7785 case BIT_AND_EXPR:
7786 case NE_EXPR:
7787 case EQ_EXPR:
7788 case UNORDERED_EXPR:
7789 case ORDERED_EXPR:
7790 case UNEQ_EXPR:
7791 case LTGT_EXPR:
7792 case TRUTH_AND_EXPR:
7793 case TRUTH_XOR_EXPR:
7794 case TRUTH_OR_EXPR:
7795 case WIDEN_MULT_EXPR:
7796 case VEC_WIDEN_MULT_HI_EXPR:
7797 case VEC_WIDEN_MULT_LO_EXPR:
7798 case VEC_WIDEN_MULT_EVEN_EXPR:
7799 case VEC_WIDEN_MULT_ODD_EXPR:
7800 return true;
7802 default:
7803 break;
7805 return false;
7808 /* Return true if CODE represents a ternary tree code for which the
7809 first two operands are commutative. Otherwise return false. */
7810 bool
7811 commutative_ternary_tree_code (enum tree_code code)
7813 switch (code)
7815 case WIDEN_MULT_PLUS_EXPR:
7816 case WIDEN_MULT_MINUS_EXPR:
7817 case DOT_PROD_EXPR:
7818 return true;
7820 default:
7821 break;
7823 return false;
7826 /* Returns true if CODE can overflow. */
7828 bool
7829 operation_can_overflow (enum tree_code code)
7831 switch (code)
7833 case PLUS_EXPR:
7834 case MINUS_EXPR:
7835 case MULT_EXPR:
7836 case LSHIFT_EXPR:
7837 /* Can overflow in various ways. */
7838 return true;
7839 case TRUNC_DIV_EXPR:
7840 case EXACT_DIV_EXPR:
7841 case FLOOR_DIV_EXPR:
7842 case CEIL_DIV_EXPR:
7843 /* For INT_MIN / -1. */
7844 return true;
7845 case NEGATE_EXPR:
7846 case ABS_EXPR:
7847 /* For -INT_MIN. */
7848 return true;
7849 default:
7850 /* These operators cannot overflow. */
7851 return false;
7855 /* Returns true if CODE operating on operands of type TYPE doesn't overflow, or
7856 ftrapv doesn't generate trapping insns for CODE. */
7858 bool
7859 operation_no_trapping_overflow (tree type, enum tree_code code)
7861 gcc_checking_assert (ANY_INTEGRAL_TYPE_P (type));
7863 /* We don't generate instructions that trap on overflow for complex or vector
7864 types. */
7865 if (!INTEGRAL_TYPE_P (type))
7866 return true;
7868 if (!TYPE_OVERFLOW_TRAPS (type))
7869 return true;
7871 switch (code)
7873 case PLUS_EXPR:
7874 case MINUS_EXPR:
7875 case MULT_EXPR:
7876 case NEGATE_EXPR:
7877 case ABS_EXPR:
7878 /* These operators can overflow, and -ftrapv generates trapping code for
7879 these. */
7880 return false;
7881 case TRUNC_DIV_EXPR:
7882 case EXACT_DIV_EXPR:
7883 case FLOOR_DIV_EXPR:
7884 case CEIL_DIV_EXPR:
7885 case LSHIFT_EXPR:
7886 /* These operators can overflow, but -ftrapv does not generate trapping
7887 code for these. */
7888 return true;
7889 default:
7890 /* These operators cannot overflow. */
7891 return true;
7895 /* Constructors for pointer, array and function types.
7896 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7897 constructed by language-dependent code, not here.) */
7899 /* Construct, lay out and return the type of pointers to TO_TYPE with
7900 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7901 reference all of memory. If such a type has already been
7902 constructed, reuse it. */
7904 tree
7905 build_pointer_type_for_mode (tree to_type, machine_mode mode,
7906 bool can_alias_all)
7908 tree t;
7909 bool could_alias = can_alias_all;
7911 if (to_type == error_mark_node)
7912 return error_mark_node;
7914 /* If the pointed-to type has the may_alias attribute set, force
7915 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7916 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7917 can_alias_all = true;
7919 /* In some cases, languages will have things that aren't a POINTER_TYPE
7920 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7921 In that case, return that type without regard to the rest of our
7922 operands.
7924 ??? This is a kludge, but consistent with the way this function has
7925 always operated and there doesn't seem to be a good way to avoid this
7926 at the moment. */
7927 if (TYPE_POINTER_TO (to_type) != 0
7928 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7929 return TYPE_POINTER_TO (to_type);
7931 /* First, if we already have a type for pointers to TO_TYPE and it's
7932 the proper mode, use it. */
7933 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7934 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7935 return t;
7937 t = make_node (POINTER_TYPE);
7939 TREE_TYPE (t) = to_type;
7940 SET_TYPE_MODE (t, mode);
7941 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7942 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7943 TYPE_POINTER_TO (to_type) = t;
7945 /* During LTO we do not set TYPE_CANONICAL of pointers and references. */
7946 if (TYPE_STRUCTURAL_EQUALITY_P (to_type) || in_lto_p)
7947 SET_TYPE_STRUCTURAL_EQUALITY (t);
7948 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
7949 TYPE_CANONICAL (t)
7950 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7951 mode, false);
7953 /* Lay out the type. This function has many callers that are concerned
7954 with expression-construction, and this simplifies them all. */
7955 layout_type (t);
7957 return t;
7960 /* By default build pointers in ptr_mode. */
7962 tree
7963 build_pointer_type (tree to_type)
7965 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7966 : TYPE_ADDR_SPACE (to_type);
7967 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7968 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7971 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7973 tree
7974 build_reference_type_for_mode (tree to_type, machine_mode mode,
7975 bool can_alias_all)
7977 tree t;
7978 bool could_alias = can_alias_all;
7980 if (to_type == error_mark_node)
7981 return error_mark_node;
7983 /* If the pointed-to type has the may_alias attribute set, force
7984 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7985 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7986 can_alias_all = true;
7988 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7989 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7990 In that case, return that type without regard to the rest of our
7991 operands.
7993 ??? This is a kludge, but consistent with the way this function has
7994 always operated and there doesn't seem to be a good way to avoid this
7995 at the moment. */
7996 if (TYPE_REFERENCE_TO (to_type) != 0
7997 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7998 return TYPE_REFERENCE_TO (to_type);
8000 /* First, if we already have a type for pointers to TO_TYPE and it's
8001 the proper mode, use it. */
8002 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
8003 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
8004 return t;
8006 t = make_node (REFERENCE_TYPE);
8008 TREE_TYPE (t) = to_type;
8009 SET_TYPE_MODE (t, mode);
8010 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
8011 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
8012 TYPE_REFERENCE_TO (to_type) = t;
8014 /* During LTO we do not set TYPE_CANONICAL of pointers and references. */
8015 if (TYPE_STRUCTURAL_EQUALITY_P (to_type) || in_lto_p)
8016 SET_TYPE_STRUCTURAL_EQUALITY (t);
8017 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
8018 TYPE_CANONICAL (t)
8019 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
8020 mode, false);
8022 layout_type (t);
8024 return t;
8028 /* Build the node for the type of references-to-TO_TYPE by default
8029 in ptr_mode. */
8031 tree
8032 build_reference_type (tree to_type)
8034 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
8035 : TYPE_ADDR_SPACE (to_type);
8036 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
8037 return build_reference_type_for_mode (to_type, pointer_mode, false);
8040 #define MAX_INT_CACHED_PREC \
8041 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
8042 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
8044 /* Builds a signed or unsigned integer type of precision PRECISION.
8045 Used for C bitfields whose precision does not match that of
8046 built-in target types. */
8047 tree
8048 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
8049 int unsignedp)
8051 tree itype, ret;
8053 if (unsignedp)
8054 unsignedp = MAX_INT_CACHED_PREC + 1;
8056 if (precision <= MAX_INT_CACHED_PREC)
8058 itype = nonstandard_integer_type_cache[precision + unsignedp];
8059 if (itype)
8060 return itype;
8063 itype = make_node (INTEGER_TYPE);
8064 TYPE_PRECISION (itype) = precision;
8066 if (unsignedp)
8067 fixup_unsigned_type (itype);
8068 else
8069 fixup_signed_type (itype);
8071 inchash::hash hstate;
8072 inchash::add_expr (TYPE_MAX_VALUE (itype), hstate);
8073 ret = type_hash_canon (hstate.end (), itype);
8074 if (precision <= MAX_INT_CACHED_PREC)
8075 nonstandard_integer_type_cache[precision + unsignedp] = ret;
8077 return ret;
8080 #define MAX_BOOL_CACHED_PREC \
8081 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
8082 static GTY(()) tree nonstandard_boolean_type_cache[MAX_BOOL_CACHED_PREC + 1];
8084 /* Builds a boolean type of precision PRECISION.
8085 Used for boolean vectors to choose proper vector element size. */
8086 tree
8087 build_nonstandard_boolean_type (unsigned HOST_WIDE_INT precision)
8089 tree type;
8091 if (precision <= MAX_BOOL_CACHED_PREC)
8093 type = nonstandard_boolean_type_cache[precision];
8094 if (type)
8095 return type;
8098 type = make_node (BOOLEAN_TYPE);
8099 TYPE_PRECISION (type) = precision;
8100 fixup_signed_type (type);
8102 if (precision <= MAX_INT_CACHED_PREC)
8103 nonstandard_boolean_type_cache[precision] = type;
8105 return type;
8108 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
8109 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
8110 is true, reuse such a type that has already been constructed. */
8112 static tree
8113 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
8115 tree itype = make_node (INTEGER_TYPE);
8117 TREE_TYPE (itype) = type;
8119 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
8120 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
8122 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
8123 SET_TYPE_MODE (itype, TYPE_MODE (type));
8124 TYPE_SIZE (itype) = TYPE_SIZE (type);
8125 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
8126 SET_TYPE_ALIGN (itype, TYPE_ALIGN (type));
8127 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
8128 SET_TYPE_WARN_IF_NOT_ALIGN (itype, TYPE_WARN_IF_NOT_ALIGN (type));
8130 if (!shared)
8131 return itype;
8133 if ((TYPE_MIN_VALUE (itype)
8134 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
8135 || (TYPE_MAX_VALUE (itype)
8136 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
8138 /* Since we cannot reliably merge this type, we need to compare it using
8139 structural equality checks. */
8140 SET_TYPE_STRUCTURAL_EQUALITY (itype);
8141 return itype;
8144 hashval_t hash = type_hash_canon_hash (itype);
8145 itype = type_hash_canon (hash, itype);
8147 return itype;
8150 /* Wrapper around build_range_type_1 with SHARED set to true. */
8152 tree
8153 build_range_type (tree type, tree lowval, tree highval)
8155 return build_range_type_1 (type, lowval, highval, true);
8158 /* Wrapper around build_range_type_1 with SHARED set to false. */
8160 tree
8161 build_nonshared_range_type (tree type, tree lowval, tree highval)
8163 return build_range_type_1 (type, lowval, highval, false);
8166 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
8167 MAXVAL should be the maximum value in the domain
8168 (one less than the length of the array).
8170 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
8171 We don't enforce this limit, that is up to caller (e.g. language front end).
8172 The limit exists because the result is a signed type and we don't handle
8173 sizes that use more than one HOST_WIDE_INT. */
8175 tree
8176 build_index_type (tree maxval)
8178 return build_range_type (sizetype, size_zero_node, maxval);
8181 /* Return true if the debug information for TYPE, a subtype, should be emitted
8182 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
8183 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
8184 debug info and doesn't reflect the source code. */
8186 bool
8187 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
8189 tree base_type = TREE_TYPE (type), low, high;
8191 /* Subrange types have a base type which is an integral type. */
8192 if (!INTEGRAL_TYPE_P (base_type))
8193 return false;
8195 /* Get the real bounds of the subtype. */
8196 if (lang_hooks.types.get_subrange_bounds)
8197 lang_hooks.types.get_subrange_bounds (type, &low, &high);
8198 else
8200 low = TYPE_MIN_VALUE (type);
8201 high = TYPE_MAX_VALUE (type);
8204 /* If the type and its base type have the same representation and the same
8205 name, then the type is not a subrange but a copy of the base type. */
8206 if ((TREE_CODE (base_type) == INTEGER_TYPE
8207 || TREE_CODE (base_type) == BOOLEAN_TYPE)
8208 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
8209 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
8210 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type))
8211 && TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (base_type))
8212 return false;
8214 if (lowval)
8215 *lowval = low;
8216 if (highval)
8217 *highval = high;
8218 return true;
8221 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
8222 and number of elements specified by the range of values of INDEX_TYPE.
8223 If TYPELESS_STORAGE is true, TYPE_TYPELESS_STORAGE flag is set on the type.
8224 If SHARED is true, reuse such a type that has already been constructed.
8225 If SET_CANONICAL is true, compute TYPE_CANONICAL from the element type. */
8227 static tree
8228 build_array_type_1 (tree elt_type, tree index_type, bool typeless_storage,
8229 bool shared, bool set_canonical)
8231 tree t;
8233 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
8235 error ("arrays of functions are not meaningful");
8236 elt_type = integer_type_node;
8239 t = make_node (ARRAY_TYPE);
8240 TREE_TYPE (t) = elt_type;
8241 TYPE_DOMAIN (t) = index_type;
8242 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
8243 TYPE_TYPELESS_STORAGE (t) = typeless_storage;
8244 layout_type (t);
8246 if (shared)
8248 hashval_t hash = type_hash_canon_hash (t);
8249 t = type_hash_canon (hash, t);
8252 if (TYPE_CANONICAL (t) == t && set_canonical)
8254 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
8255 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type))
8256 || in_lto_p)
8257 SET_TYPE_STRUCTURAL_EQUALITY (t);
8258 else if (TYPE_CANONICAL (elt_type) != elt_type
8259 || (index_type && TYPE_CANONICAL (index_type) != index_type))
8260 TYPE_CANONICAL (t)
8261 = build_array_type_1 (TYPE_CANONICAL (elt_type),
8262 index_type
8263 ? TYPE_CANONICAL (index_type) : NULL_TREE,
8264 typeless_storage, shared, set_canonical);
8267 return t;
8270 /* Wrapper around build_array_type_1 with SHARED set to true. */
8272 tree
8273 build_array_type (tree elt_type, tree index_type, bool typeless_storage)
8275 return
8276 build_array_type_1 (elt_type, index_type, typeless_storage, true, true);
8279 /* Wrapper around build_array_type_1 with SHARED set to false. */
8281 tree
8282 build_nonshared_array_type (tree elt_type, tree index_type)
8284 return build_array_type_1 (elt_type, index_type, false, false, true);
8287 /* Return a representation of ELT_TYPE[NELTS], using indices of type
8288 sizetype. */
8290 tree
8291 build_array_type_nelts (tree elt_type, poly_uint64 nelts)
8293 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
8296 /* Recursively examines the array elements of TYPE, until a non-array
8297 element type is found. */
8299 tree
8300 strip_array_types (tree type)
8302 while (TREE_CODE (type) == ARRAY_TYPE)
8303 type = TREE_TYPE (type);
8305 return type;
8308 /* Computes the canonical argument types from the argument type list
8309 ARGTYPES.
8311 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
8312 on entry to this function, or if any of the ARGTYPES are
8313 structural.
8315 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
8316 true on entry to this function, or if any of the ARGTYPES are
8317 non-canonical.
8319 Returns a canonical argument list, which may be ARGTYPES when the
8320 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
8321 true) or would not differ from ARGTYPES. */
8323 static tree
8324 maybe_canonicalize_argtypes (tree argtypes,
8325 bool *any_structural_p,
8326 bool *any_noncanonical_p)
8328 tree arg;
8329 bool any_noncanonical_argtypes_p = false;
8331 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
8333 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
8334 /* Fail gracefully by stating that the type is structural. */
8335 *any_structural_p = true;
8336 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
8337 *any_structural_p = true;
8338 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
8339 || TREE_PURPOSE (arg))
8340 /* If the argument has a default argument, we consider it
8341 non-canonical even though the type itself is canonical.
8342 That way, different variants of function and method types
8343 with default arguments will all point to the variant with
8344 no defaults as their canonical type. */
8345 any_noncanonical_argtypes_p = true;
8348 if (*any_structural_p)
8349 return argtypes;
8351 if (any_noncanonical_argtypes_p)
8353 /* Build the canonical list of argument types. */
8354 tree canon_argtypes = NULL_TREE;
8355 bool is_void = false;
8357 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
8359 if (arg == void_list_node)
8360 is_void = true;
8361 else
8362 canon_argtypes = tree_cons (NULL_TREE,
8363 TYPE_CANONICAL (TREE_VALUE (arg)),
8364 canon_argtypes);
8367 canon_argtypes = nreverse (canon_argtypes);
8368 if (is_void)
8369 canon_argtypes = chainon (canon_argtypes, void_list_node);
8371 /* There is a non-canonical type. */
8372 *any_noncanonical_p = true;
8373 return canon_argtypes;
8376 /* The canonical argument types are the same as ARGTYPES. */
8377 return argtypes;
8380 /* Construct, lay out and return
8381 the type of functions returning type VALUE_TYPE
8382 given arguments of types ARG_TYPES.
8383 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
8384 are data type nodes for the arguments of the function.
8385 If such a type has already been constructed, reuse it. */
8387 tree
8388 build_function_type (tree value_type, tree arg_types)
8390 tree t;
8391 inchash::hash hstate;
8392 bool any_structural_p, any_noncanonical_p;
8393 tree canon_argtypes;
8395 gcc_assert (arg_types != error_mark_node);
8397 if (TREE_CODE (value_type) == FUNCTION_TYPE)
8399 error ("function return type cannot be function");
8400 value_type = integer_type_node;
8403 /* Make a node of the sort we want. */
8404 t = make_node (FUNCTION_TYPE);
8405 TREE_TYPE (t) = value_type;
8406 TYPE_ARG_TYPES (t) = arg_types;
8408 /* If we already have such a type, use the old one. */
8409 hashval_t hash = type_hash_canon_hash (t);
8410 t = type_hash_canon (hash, t);
8412 /* Set up the canonical type. */
8413 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
8414 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
8415 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
8416 &any_structural_p,
8417 &any_noncanonical_p);
8418 if (any_structural_p)
8419 SET_TYPE_STRUCTURAL_EQUALITY (t);
8420 else if (any_noncanonical_p)
8421 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
8422 canon_argtypes);
8424 if (!COMPLETE_TYPE_P (t))
8425 layout_type (t);
8426 return t;
8429 /* Build a function type. The RETURN_TYPE is the type returned by the
8430 function. If VAARGS is set, no void_type_node is appended to the
8431 list. ARGP must be always be terminated be a NULL_TREE. */
8433 static tree
8434 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
8436 tree t, args, last;
8438 t = va_arg (argp, tree);
8439 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
8440 args = tree_cons (NULL_TREE, t, args);
8442 if (vaargs)
8444 last = args;
8445 if (args != NULL_TREE)
8446 args = nreverse (args);
8447 gcc_assert (last != void_list_node);
8449 else if (args == NULL_TREE)
8450 args = void_list_node;
8451 else
8453 last = args;
8454 args = nreverse (args);
8455 TREE_CHAIN (last) = void_list_node;
8457 args = build_function_type (return_type, args);
8459 return args;
8462 /* Build a function type. The RETURN_TYPE is the type returned by the
8463 function. If additional arguments are provided, they are
8464 additional argument types. The list of argument types must always
8465 be terminated by NULL_TREE. */
8467 tree
8468 build_function_type_list (tree return_type, ...)
8470 tree args;
8471 va_list p;
8473 va_start (p, return_type);
8474 args = build_function_type_list_1 (false, return_type, p);
8475 va_end (p);
8476 return args;
8479 /* Build a variable argument function type. The RETURN_TYPE is the
8480 type returned by the function. If additional arguments are provided,
8481 they are additional argument types. The list of argument types must
8482 always be terminated by NULL_TREE. */
8484 tree
8485 build_varargs_function_type_list (tree return_type, ...)
8487 tree args;
8488 va_list p;
8490 va_start (p, return_type);
8491 args = build_function_type_list_1 (true, return_type, p);
8492 va_end (p);
8494 return args;
8497 /* Build a function type. RETURN_TYPE is the type returned by the
8498 function; VAARGS indicates whether the function takes varargs. The
8499 function takes N named arguments, the types of which are provided in
8500 ARG_TYPES. */
8502 static tree
8503 build_function_type_array_1 (bool vaargs, tree return_type, int n,
8504 tree *arg_types)
8506 int i;
8507 tree t = vaargs ? NULL_TREE : void_list_node;
8509 for (i = n - 1; i >= 0; i--)
8510 t = tree_cons (NULL_TREE, arg_types[i], t);
8512 return build_function_type (return_type, t);
8515 /* Build a function type. RETURN_TYPE is the type returned by the
8516 function. The function takes N named arguments, the types of which
8517 are provided in ARG_TYPES. */
8519 tree
8520 build_function_type_array (tree return_type, int n, tree *arg_types)
8522 return build_function_type_array_1 (false, return_type, n, arg_types);
8525 /* Build a variable argument function type. RETURN_TYPE is the type
8526 returned by the function. The function takes N named arguments, the
8527 types of which are provided in ARG_TYPES. */
8529 tree
8530 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8532 return build_function_type_array_1 (true, return_type, n, arg_types);
8535 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8536 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8537 for the method. An implicit additional parameter (of type
8538 pointer-to-BASETYPE) is added to the ARGTYPES. */
8540 tree
8541 build_method_type_directly (tree basetype,
8542 tree rettype,
8543 tree argtypes)
8545 tree t;
8546 tree ptype;
8547 bool any_structural_p, any_noncanonical_p;
8548 tree canon_argtypes;
8550 /* Make a node of the sort we want. */
8551 t = make_node (METHOD_TYPE);
8553 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8554 TREE_TYPE (t) = rettype;
8555 ptype = build_pointer_type (basetype);
8557 /* The actual arglist for this function includes a "hidden" argument
8558 which is "this". Put it into the list of argument types. */
8559 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8560 TYPE_ARG_TYPES (t) = argtypes;
8562 /* If we already have such a type, use the old one. */
8563 hashval_t hash = type_hash_canon_hash (t);
8564 t = type_hash_canon (hash, t);
8566 /* Set up the canonical type. */
8567 any_structural_p
8568 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8569 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8570 any_noncanonical_p
8571 = (TYPE_CANONICAL (basetype) != basetype
8572 || TYPE_CANONICAL (rettype) != rettype);
8573 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8574 &any_structural_p,
8575 &any_noncanonical_p);
8576 if (any_structural_p)
8577 SET_TYPE_STRUCTURAL_EQUALITY (t);
8578 else if (any_noncanonical_p)
8579 TYPE_CANONICAL (t)
8580 = build_method_type_directly (TYPE_CANONICAL (basetype),
8581 TYPE_CANONICAL (rettype),
8582 canon_argtypes);
8583 if (!COMPLETE_TYPE_P (t))
8584 layout_type (t);
8586 return t;
8589 /* Construct, lay out and return the type of methods belonging to class
8590 BASETYPE and whose arguments and values are described by TYPE.
8591 If that type exists already, reuse it.
8592 TYPE must be a FUNCTION_TYPE node. */
8594 tree
8595 build_method_type (tree basetype, tree type)
8597 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8599 return build_method_type_directly (basetype,
8600 TREE_TYPE (type),
8601 TYPE_ARG_TYPES (type));
8604 /* Construct, lay out and return the type of offsets to a value
8605 of type TYPE, within an object of type BASETYPE.
8606 If a suitable offset type exists already, reuse it. */
8608 tree
8609 build_offset_type (tree basetype, tree type)
8611 tree t;
8613 /* Make a node of the sort we want. */
8614 t = make_node (OFFSET_TYPE);
8616 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8617 TREE_TYPE (t) = type;
8619 /* If we already have such a type, use the old one. */
8620 hashval_t hash = type_hash_canon_hash (t);
8621 t = type_hash_canon (hash, t);
8623 if (!COMPLETE_TYPE_P (t))
8624 layout_type (t);
8626 if (TYPE_CANONICAL (t) == t)
8628 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8629 || TYPE_STRUCTURAL_EQUALITY_P (type))
8630 SET_TYPE_STRUCTURAL_EQUALITY (t);
8631 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8632 || TYPE_CANONICAL (type) != type)
8633 TYPE_CANONICAL (t)
8634 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8635 TYPE_CANONICAL (type));
8638 return t;
8641 /* Create a complex type whose components are COMPONENT_TYPE.
8643 If NAMED is true, the type is given a TYPE_NAME. We do not always
8644 do so because this creates a DECL node and thus make the DECL_UIDs
8645 dependent on the type canonicalization hashtable, which is GC-ed,
8646 so the DECL_UIDs would not be stable wrt garbage collection. */
8648 tree
8649 build_complex_type (tree component_type, bool named)
8651 gcc_assert (INTEGRAL_TYPE_P (component_type)
8652 || SCALAR_FLOAT_TYPE_P (component_type)
8653 || FIXED_POINT_TYPE_P (component_type));
8655 /* Make a node of the sort we want. */
8656 tree probe = make_node (COMPLEX_TYPE);
8658 TREE_TYPE (probe) = TYPE_MAIN_VARIANT (component_type);
8660 /* If we already have such a type, use the old one. */
8661 hashval_t hash = type_hash_canon_hash (probe);
8662 tree t = type_hash_canon (hash, probe);
8664 if (t == probe)
8666 /* We created a new type. The hash insertion will have laid
8667 out the type. We need to check the canonicalization and
8668 maybe set the name. */
8669 gcc_checking_assert (COMPLETE_TYPE_P (t)
8670 && !TYPE_NAME (t)
8671 && TYPE_CANONICAL (t) == t);
8673 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (t)))
8674 SET_TYPE_STRUCTURAL_EQUALITY (t);
8675 else if (TYPE_CANONICAL (TREE_TYPE (t)) != TREE_TYPE (t))
8676 TYPE_CANONICAL (t)
8677 = build_complex_type (TYPE_CANONICAL (TREE_TYPE (t)), named);
8679 /* We need to create a name, since complex is a fundamental type. */
8680 if (named)
8682 const char *name = NULL;
8684 if (TREE_TYPE (t) == char_type_node)
8685 name = "complex char";
8686 else if (TREE_TYPE (t) == signed_char_type_node)
8687 name = "complex signed char";
8688 else if (TREE_TYPE (t) == unsigned_char_type_node)
8689 name = "complex unsigned char";
8690 else if (TREE_TYPE (t) == short_integer_type_node)
8691 name = "complex short int";
8692 else if (TREE_TYPE (t) == short_unsigned_type_node)
8693 name = "complex short unsigned int";
8694 else if (TREE_TYPE (t) == integer_type_node)
8695 name = "complex int";
8696 else if (TREE_TYPE (t) == unsigned_type_node)
8697 name = "complex unsigned int";
8698 else if (TREE_TYPE (t) == long_integer_type_node)
8699 name = "complex long int";
8700 else if (TREE_TYPE (t) == long_unsigned_type_node)
8701 name = "complex long unsigned int";
8702 else if (TREE_TYPE (t) == long_long_integer_type_node)
8703 name = "complex long long int";
8704 else if (TREE_TYPE (t) == long_long_unsigned_type_node)
8705 name = "complex long long unsigned int";
8707 if (name != NULL)
8708 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8709 get_identifier (name), t);
8713 return build_qualified_type (t, TYPE_QUALS (component_type));
8716 /* If TYPE is a real or complex floating-point type and the target
8717 does not directly support arithmetic on TYPE then return the wider
8718 type to be used for arithmetic on TYPE. Otherwise, return
8719 NULL_TREE. */
8721 tree
8722 excess_precision_type (tree type)
8724 /* The target can give two different responses to the question of
8725 which excess precision mode it would like depending on whether we
8726 are in -fexcess-precision=standard or -fexcess-precision=fast. */
8728 enum excess_precision_type requested_type
8729 = (flag_excess_precision == EXCESS_PRECISION_FAST
8730 ? EXCESS_PRECISION_TYPE_FAST
8731 : EXCESS_PRECISION_TYPE_STANDARD);
8733 enum flt_eval_method target_flt_eval_method
8734 = targetm.c.excess_precision (requested_type);
8736 /* The target should not ask for unpredictable float evaluation (though
8737 it might advertise that implicitly the evaluation is unpredictable,
8738 but we don't care about that here, it will have been reported
8739 elsewhere). If it does ask for unpredictable evaluation, we have
8740 nothing to do here. */
8741 gcc_assert (target_flt_eval_method != FLT_EVAL_METHOD_UNPREDICTABLE);
8743 /* Nothing to do. The target has asked for all types we know about
8744 to be computed with their native precision and range. */
8745 if (target_flt_eval_method == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
8746 return NULL_TREE;
8748 /* The target will promote this type in a target-dependent way, so excess
8749 precision ought to leave it alone. */
8750 if (targetm.promoted_type (type) != NULL_TREE)
8751 return NULL_TREE;
8753 machine_mode float16_type_mode = (float16_type_node
8754 ? TYPE_MODE (float16_type_node)
8755 : VOIDmode);
8756 machine_mode float_type_mode = TYPE_MODE (float_type_node);
8757 machine_mode double_type_mode = TYPE_MODE (double_type_node);
8759 switch (TREE_CODE (type))
8761 case REAL_TYPE:
8763 machine_mode type_mode = TYPE_MODE (type);
8764 switch (target_flt_eval_method)
8766 case FLT_EVAL_METHOD_PROMOTE_TO_FLOAT:
8767 if (type_mode == float16_type_mode)
8768 return float_type_node;
8769 break;
8770 case FLT_EVAL_METHOD_PROMOTE_TO_DOUBLE:
8771 if (type_mode == float16_type_mode
8772 || type_mode == float_type_mode)
8773 return double_type_node;
8774 break;
8775 case FLT_EVAL_METHOD_PROMOTE_TO_LONG_DOUBLE:
8776 if (type_mode == float16_type_mode
8777 || type_mode == float_type_mode
8778 || type_mode == double_type_mode)
8779 return long_double_type_node;
8780 break;
8781 default:
8782 gcc_unreachable ();
8784 break;
8786 case COMPLEX_TYPE:
8788 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8789 return NULL_TREE;
8790 machine_mode type_mode = TYPE_MODE (TREE_TYPE (type));
8791 switch (target_flt_eval_method)
8793 case FLT_EVAL_METHOD_PROMOTE_TO_FLOAT:
8794 if (type_mode == float16_type_mode)
8795 return complex_float_type_node;
8796 break;
8797 case FLT_EVAL_METHOD_PROMOTE_TO_DOUBLE:
8798 if (type_mode == float16_type_mode
8799 || type_mode == float_type_mode)
8800 return complex_double_type_node;
8801 break;
8802 case FLT_EVAL_METHOD_PROMOTE_TO_LONG_DOUBLE:
8803 if (type_mode == float16_type_mode
8804 || type_mode == float_type_mode
8805 || type_mode == double_type_mode)
8806 return complex_long_double_type_node;
8807 break;
8808 default:
8809 gcc_unreachable ();
8811 break;
8813 default:
8814 break;
8817 return NULL_TREE;
8820 /* Return OP, stripped of any conversions to wider types as much as is safe.
8821 Converting the value back to OP's type makes a value equivalent to OP.
8823 If FOR_TYPE is nonzero, we return a value which, if converted to
8824 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8826 OP must have integer, real or enumeral type. Pointers are not allowed!
8828 There are some cases where the obvious value we could return
8829 would regenerate to OP if converted to OP's type,
8830 but would not extend like OP to wider types.
8831 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8832 For example, if OP is (unsigned short)(signed char)-1,
8833 we avoid returning (signed char)-1 if FOR_TYPE is int,
8834 even though extending that to an unsigned short would regenerate OP,
8835 since the result of extending (signed char)-1 to (int)
8836 is different from (int) OP. */
8838 tree
8839 get_unwidened (tree op, tree for_type)
8841 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8842 tree type = TREE_TYPE (op);
8843 unsigned final_prec
8844 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8845 int uns
8846 = (for_type != 0 && for_type != type
8847 && final_prec > TYPE_PRECISION (type)
8848 && TYPE_UNSIGNED (type));
8849 tree win = op;
8851 while (CONVERT_EXPR_P (op))
8853 int bitschange;
8855 /* TYPE_PRECISION on vector types has different meaning
8856 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8857 so avoid them here. */
8858 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8859 break;
8861 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8862 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8864 /* Truncations are many-one so cannot be removed.
8865 Unless we are later going to truncate down even farther. */
8866 if (bitschange < 0
8867 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8868 break;
8870 /* See what's inside this conversion. If we decide to strip it,
8871 we will set WIN. */
8872 op = TREE_OPERAND (op, 0);
8874 /* If we have not stripped any zero-extensions (uns is 0),
8875 we can strip any kind of extension.
8876 If we have previously stripped a zero-extension,
8877 only zero-extensions can safely be stripped.
8878 Any extension can be stripped if the bits it would produce
8879 are all going to be discarded later by truncating to FOR_TYPE. */
8881 if (bitschange > 0)
8883 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8884 win = op;
8885 /* TYPE_UNSIGNED says whether this is a zero-extension.
8886 Let's avoid computing it if it does not affect WIN
8887 and if UNS will not be needed again. */
8888 if ((uns
8889 || CONVERT_EXPR_P (op))
8890 && TYPE_UNSIGNED (TREE_TYPE (op)))
8892 uns = 1;
8893 win = op;
8898 /* If we finally reach a constant see if it fits in sth smaller and
8899 in that case convert it. */
8900 if (TREE_CODE (win) == INTEGER_CST)
8902 tree wtype = TREE_TYPE (win);
8903 unsigned prec = wi::min_precision (wi::to_wide (win), TYPE_SIGN (wtype));
8904 if (for_type)
8905 prec = MAX (prec, final_prec);
8906 if (prec < TYPE_PRECISION (wtype))
8908 tree t = lang_hooks.types.type_for_size (prec, TYPE_UNSIGNED (wtype));
8909 if (t && TYPE_PRECISION (t) < TYPE_PRECISION (wtype))
8910 win = fold_convert (t, win);
8914 return win;
8917 /* Return OP or a simpler expression for a narrower value
8918 which can be sign-extended or zero-extended to give back OP.
8919 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8920 or 0 if the value should be sign-extended. */
8922 tree
8923 get_narrower (tree op, int *unsignedp_ptr)
8925 int uns = 0;
8926 int first = 1;
8927 tree win = op;
8928 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8930 if (TREE_CODE (op) == COMPOUND_EXPR)
8933 op = TREE_OPERAND (op, 1);
8934 while (TREE_CODE (op) == COMPOUND_EXPR);
8935 tree ret = get_narrower (op, unsignedp_ptr);
8936 if (ret == op)
8937 return win;
8938 auto_vec <tree, 16> v;
8939 unsigned int i;
8940 for (op = win; TREE_CODE (op) == COMPOUND_EXPR;
8941 op = TREE_OPERAND (op, 1))
8942 v.safe_push (op);
8943 FOR_EACH_VEC_ELT_REVERSE (v, i, op)
8944 ret = build2_loc (EXPR_LOCATION (op), COMPOUND_EXPR,
8945 TREE_TYPE (ret), TREE_OPERAND (op, 0),
8946 ret);
8947 return ret;
8949 while (TREE_CODE (op) == NOP_EXPR)
8951 int bitschange
8952 = (TYPE_PRECISION (TREE_TYPE (op))
8953 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8955 /* Truncations are many-one so cannot be removed. */
8956 if (bitschange < 0)
8957 break;
8959 /* See what's inside this conversion. If we decide to strip it,
8960 we will set WIN. */
8962 if (bitschange > 0)
8964 op = TREE_OPERAND (op, 0);
8965 /* An extension: the outermost one can be stripped,
8966 but remember whether it is zero or sign extension. */
8967 if (first)
8968 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8969 /* Otherwise, if a sign extension has been stripped,
8970 only sign extensions can now be stripped;
8971 if a zero extension has been stripped, only zero-extensions. */
8972 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8973 break;
8974 first = 0;
8976 else /* bitschange == 0 */
8978 /* A change in nominal type can always be stripped, but we must
8979 preserve the unsignedness. */
8980 if (first)
8981 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8982 first = 0;
8983 op = TREE_OPERAND (op, 0);
8984 /* Keep trying to narrow, but don't assign op to win if it
8985 would turn an integral type into something else. */
8986 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8987 continue;
8990 win = op;
8993 if (TREE_CODE (op) == COMPONENT_REF
8994 /* Since type_for_size always gives an integer type. */
8995 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8996 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8997 /* Ensure field is laid out already. */
8998 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8999 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
9001 unsigned HOST_WIDE_INT innerprec
9002 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
9003 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
9004 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
9005 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
9007 /* We can get this structure field in a narrower type that fits it,
9008 but the resulting extension to its nominal type (a fullword type)
9009 must satisfy the same conditions as for other extensions.
9011 Do this only for fields that are aligned (not bit-fields),
9012 because when bit-field insns will be used there is no
9013 advantage in doing this. */
9015 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
9016 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
9017 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
9018 && type != 0)
9020 if (first)
9021 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
9022 win = fold_convert (type, op);
9026 *unsignedp_ptr = uns;
9027 return win;
9030 /* Return true if integer constant C has a value that is permissible
9031 for TYPE, an integral type. */
9033 bool
9034 int_fits_type_p (const_tree c, const_tree type)
9036 tree type_low_bound, type_high_bound;
9037 bool ok_for_low_bound, ok_for_high_bound;
9038 signop sgn_c = TYPE_SIGN (TREE_TYPE (c));
9040 /* Non-standard boolean types can have arbitrary precision but various
9041 transformations assume that they can only take values 0 and +/-1. */
9042 if (TREE_CODE (type) == BOOLEAN_TYPE)
9043 return wi::fits_to_boolean_p (wi::to_wide (c), type);
9045 retry:
9046 type_low_bound = TYPE_MIN_VALUE (type);
9047 type_high_bound = TYPE_MAX_VALUE (type);
9049 /* If at least one bound of the type is a constant integer, we can check
9050 ourselves and maybe make a decision. If no such decision is possible, but
9051 this type is a subtype, try checking against that. Otherwise, use
9052 fits_to_tree_p, which checks against the precision.
9054 Compute the status for each possibly constant bound, and return if we see
9055 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
9056 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
9057 for "constant known to fit". */
9059 /* Check if c >= type_low_bound. */
9060 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
9062 if (tree_int_cst_lt (c, type_low_bound))
9063 return false;
9064 ok_for_low_bound = true;
9066 else
9067 ok_for_low_bound = false;
9069 /* Check if c <= type_high_bound. */
9070 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
9072 if (tree_int_cst_lt (type_high_bound, c))
9073 return false;
9074 ok_for_high_bound = true;
9076 else
9077 ok_for_high_bound = false;
9079 /* If the constant fits both bounds, the result is known. */
9080 if (ok_for_low_bound && ok_for_high_bound)
9081 return true;
9083 /* Perform some generic filtering which may allow making a decision
9084 even if the bounds are not constant. First, negative integers
9085 never fit in unsigned types, */
9086 if (TYPE_UNSIGNED (type) && sgn_c == SIGNED && wi::neg_p (wi::to_wide (c)))
9087 return false;
9089 /* Second, narrower types always fit in wider ones. */
9090 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
9091 return true;
9093 /* Third, unsigned integers with top bit set never fit signed types. */
9094 if (!TYPE_UNSIGNED (type) && sgn_c == UNSIGNED)
9096 int prec = GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (TREE_TYPE (c))) - 1;
9097 if (prec < TYPE_PRECISION (TREE_TYPE (c)))
9099 /* When a tree_cst is converted to a wide-int, the precision
9100 is taken from the type. However, if the precision of the
9101 mode underneath the type is smaller than that, it is
9102 possible that the value will not fit. The test below
9103 fails if any bit is set between the sign bit of the
9104 underlying mode and the top bit of the type. */
9105 if (wi::zext (wi::to_wide (c), prec - 1) != wi::to_wide (c))
9106 return false;
9108 else if (wi::neg_p (wi::to_wide (c)))
9109 return false;
9112 /* If we haven't been able to decide at this point, there nothing more we
9113 can check ourselves here. Look at the base type if we have one and it
9114 has the same precision. */
9115 if (TREE_CODE (type) == INTEGER_TYPE
9116 && TREE_TYPE (type) != 0
9117 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
9119 type = TREE_TYPE (type);
9120 goto retry;
9123 /* Or to fits_to_tree_p, if nothing else. */
9124 return wi::fits_to_tree_p (wi::to_wide (c), type);
9127 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
9128 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
9129 represented (assuming two's-complement arithmetic) within the bit
9130 precision of the type are returned instead. */
9132 void
9133 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
9135 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
9136 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
9137 wi::to_mpz (wi::to_wide (TYPE_MIN_VALUE (type)), min, TYPE_SIGN (type));
9138 else
9140 if (TYPE_UNSIGNED (type))
9141 mpz_set_ui (min, 0);
9142 else
9144 wide_int mn = wi::min_value (TYPE_PRECISION (type), SIGNED);
9145 wi::to_mpz (mn, min, SIGNED);
9149 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
9150 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
9151 wi::to_mpz (wi::to_wide (TYPE_MAX_VALUE (type)), max, TYPE_SIGN (type));
9152 else
9154 wide_int mn = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
9155 wi::to_mpz (mn, max, TYPE_SIGN (type));
9159 /* Return true if VAR is an automatic variable. */
9161 bool
9162 auto_var_p (const_tree var)
9164 return ((((VAR_P (var) && ! DECL_EXTERNAL (var))
9165 || TREE_CODE (var) == PARM_DECL)
9166 && ! TREE_STATIC (var))
9167 || TREE_CODE (var) == RESULT_DECL);
9170 /* Return true if VAR is an automatic variable defined in function FN. */
9172 bool
9173 auto_var_in_fn_p (const_tree var, const_tree fn)
9175 return (DECL_P (var) && DECL_CONTEXT (var) == fn
9176 && (auto_var_p (var)
9177 || TREE_CODE (var) == LABEL_DECL));
9180 /* Subprogram of following function. Called by walk_tree.
9182 Return *TP if it is an automatic variable or parameter of the
9183 function passed in as DATA. */
9185 static tree
9186 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
9188 tree fn = (tree) data;
9190 if (TYPE_P (*tp))
9191 *walk_subtrees = 0;
9193 else if (DECL_P (*tp)
9194 && auto_var_in_fn_p (*tp, fn))
9195 return *tp;
9197 return NULL_TREE;
9200 /* Returns true if T is, contains, or refers to a type with variable
9201 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
9202 arguments, but not the return type. If FN is nonzero, only return
9203 true if a modifier of the type or position of FN is a variable or
9204 parameter inside FN.
9206 This concept is more general than that of C99 'variably modified types':
9207 in C99, a struct type is never variably modified because a VLA may not
9208 appear as a structure member. However, in GNU C code like:
9210 struct S { int i[f()]; };
9212 is valid, and other languages may define similar constructs. */
9214 bool
9215 variably_modified_type_p (tree type, tree fn)
9217 tree t;
9219 /* Test if T is either variable (if FN is zero) or an expression containing
9220 a variable in FN. If TYPE isn't gimplified, return true also if
9221 gimplify_one_sizepos would gimplify the expression into a local
9222 variable. */
9223 #define RETURN_TRUE_IF_VAR(T) \
9224 do { tree _t = (T); \
9225 if (_t != NULL_TREE \
9226 && _t != error_mark_node \
9227 && !CONSTANT_CLASS_P (_t) \
9228 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
9229 && (!fn \
9230 || (!TYPE_SIZES_GIMPLIFIED (type) \
9231 && (TREE_CODE (_t) != VAR_DECL \
9232 && !CONTAINS_PLACEHOLDER_P (_t))) \
9233 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
9234 return true; } while (0)
9236 if (type == error_mark_node)
9237 return false;
9239 /* If TYPE itself has variable size, it is variably modified. */
9240 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
9241 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
9243 switch (TREE_CODE (type))
9245 case POINTER_TYPE:
9246 case REFERENCE_TYPE:
9247 case VECTOR_TYPE:
9248 /* Ada can have pointer types refering to themselves indirectly. */
9249 if (TREE_VISITED (type))
9250 return false;
9251 TREE_VISITED (type) = true;
9252 if (variably_modified_type_p (TREE_TYPE (type), fn))
9254 TREE_VISITED (type) = false;
9255 return true;
9257 TREE_VISITED (type) = false;
9258 break;
9260 case FUNCTION_TYPE:
9261 case METHOD_TYPE:
9262 /* If TYPE is a function type, it is variably modified if the
9263 return type is variably modified. */
9264 if (variably_modified_type_p (TREE_TYPE (type), fn))
9265 return true;
9266 break;
9268 case INTEGER_TYPE:
9269 case REAL_TYPE:
9270 case FIXED_POINT_TYPE:
9271 case ENUMERAL_TYPE:
9272 case BOOLEAN_TYPE:
9273 /* Scalar types are variably modified if their end points
9274 aren't constant. */
9275 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
9276 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
9277 break;
9279 case RECORD_TYPE:
9280 case UNION_TYPE:
9281 case QUAL_UNION_TYPE:
9282 /* We can't see if any of the fields are variably-modified by the
9283 definition we normally use, since that would produce infinite
9284 recursion via pointers. */
9285 /* This is variably modified if some field's type is. */
9286 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
9287 if (TREE_CODE (t) == FIELD_DECL)
9289 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
9290 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
9291 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
9293 /* If the type is a qualified union, then the DECL_QUALIFIER
9294 of fields can also be an expression containing a variable. */
9295 if (TREE_CODE (type) == QUAL_UNION_TYPE)
9296 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
9298 /* If the field is a qualified union, then it's only a container
9299 for what's inside so we look into it. That's necessary in LTO
9300 mode because the sizes of the field tested above have been set
9301 to PLACEHOLDER_EXPRs by free_lang_data. */
9302 if (TREE_CODE (TREE_TYPE (t)) == QUAL_UNION_TYPE
9303 && variably_modified_type_p (TREE_TYPE (t), fn))
9304 return true;
9306 break;
9308 case ARRAY_TYPE:
9309 /* Do not call ourselves to avoid infinite recursion. This is
9310 variably modified if the element type is. */
9311 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
9312 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
9313 break;
9315 default:
9316 break;
9319 /* The current language may have other cases to check, but in general,
9320 all other types are not variably modified. */
9321 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
9323 #undef RETURN_TRUE_IF_VAR
9326 /* Given a DECL or TYPE, return the scope in which it was declared, or
9327 NULL_TREE if there is no containing scope. */
9329 tree
9330 get_containing_scope (const_tree t)
9332 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
9335 /* Returns the ultimate TRANSLATION_UNIT_DECL context of DECL or NULL. */
9337 const_tree
9338 get_ultimate_context (const_tree decl)
9340 while (decl && TREE_CODE (decl) != TRANSLATION_UNIT_DECL)
9342 if (TREE_CODE (decl) == BLOCK)
9343 decl = BLOCK_SUPERCONTEXT (decl);
9344 else
9345 decl = get_containing_scope (decl);
9347 return decl;
9350 /* Return the innermost context enclosing DECL that is
9351 a FUNCTION_DECL, or zero if none. */
9353 tree
9354 decl_function_context (const_tree decl)
9356 tree context;
9358 if (TREE_CODE (decl) == ERROR_MARK)
9359 return 0;
9361 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
9362 where we look up the function at runtime. Such functions always take
9363 a first argument of type 'pointer to real context'.
9365 C++ should really be fixed to use DECL_CONTEXT for the real context,
9366 and use something else for the "virtual context". */
9367 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
9368 context
9369 = TYPE_MAIN_VARIANT
9370 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
9371 else
9372 context = DECL_CONTEXT (decl);
9374 while (context && TREE_CODE (context) != FUNCTION_DECL)
9376 if (TREE_CODE (context) == BLOCK)
9377 context = BLOCK_SUPERCONTEXT (context);
9378 else
9379 context = get_containing_scope (context);
9382 return context;
9385 /* Return the innermost context enclosing DECL that is
9386 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
9387 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
9389 tree
9390 decl_type_context (const_tree decl)
9392 tree context = DECL_CONTEXT (decl);
9394 while (context)
9395 switch (TREE_CODE (context))
9397 case NAMESPACE_DECL:
9398 case TRANSLATION_UNIT_DECL:
9399 return NULL_TREE;
9401 case RECORD_TYPE:
9402 case UNION_TYPE:
9403 case QUAL_UNION_TYPE:
9404 return context;
9406 case TYPE_DECL:
9407 case FUNCTION_DECL:
9408 context = DECL_CONTEXT (context);
9409 break;
9411 case BLOCK:
9412 context = BLOCK_SUPERCONTEXT (context);
9413 break;
9415 default:
9416 gcc_unreachable ();
9419 return NULL_TREE;
9422 /* CALL is a CALL_EXPR. Return the declaration for the function
9423 called, or NULL_TREE if the called function cannot be
9424 determined. */
9426 tree
9427 get_callee_fndecl (const_tree call)
9429 tree addr;
9431 if (call == error_mark_node)
9432 return error_mark_node;
9434 /* It's invalid to call this function with anything but a
9435 CALL_EXPR. */
9436 gcc_assert (TREE_CODE (call) == CALL_EXPR);
9438 /* The first operand to the CALL is the address of the function
9439 called. */
9440 addr = CALL_EXPR_FN (call);
9442 /* If there is no function, return early. */
9443 if (addr == NULL_TREE)
9444 return NULL_TREE;
9446 STRIP_NOPS (addr);
9448 /* If this is a readonly function pointer, extract its initial value. */
9449 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
9450 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
9451 && DECL_INITIAL (addr))
9452 addr = DECL_INITIAL (addr);
9454 /* If the address is just `&f' for some function `f', then we know
9455 that `f' is being called. */
9456 if (TREE_CODE (addr) == ADDR_EXPR
9457 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
9458 return TREE_OPERAND (addr, 0);
9460 /* We couldn't figure out what was being called. */
9461 return NULL_TREE;
9464 /* If CALL_EXPR CALL calls a normal built-in function or an internal function,
9465 return the associated function code, otherwise return CFN_LAST. */
9467 combined_fn
9468 get_call_combined_fn (const_tree call)
9470 /* It's invalid to call this function with anything but a CALL_EXPR. */
9471 gcc_assert (TREE_CODE (call) == CALL_EXPR);
9473 if (!CALL_EXPR_FN (call))
9474 return as_combined_fn (CALL_EXPR_IFN (call));
9476 tree fndecl = get_callee_fndecl (call);
9477 if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
9478 return as_combined_fn (DECL_FUNCTION_CODE (fndecl));
9480 return CFN_LAST;
9483 /* Comparator of indices based on tree_node_counts. */
9485 static int
9486 tree_nodes_cmp (const void *p1, const void *p2)
9488 const unsigned *n1 = (const unsigned *)p1;
9489 const unsigned *n2 = (const unsigned *)p2;
9491 return tree_node_counts[*n1] - tree_node_counts[*n2];
9494 /* Comparator of indices based on tree_code_counts. */
9496 static int
9497 tree_codes_cmp (const void *p1, const void *p2)
9499 const unsigned *n1 = (const unsigned *)p1;
9500 const unsigned *n2 = (const unsigned *)p2;
9502 return tree_code_counts[*n1] - tree_code_counts[*n2];
9505 #define TREE_MEM_USAGE_SPACES 40
9507 /* Print debugging information about tree nodes generated during the compile,
9508 and any language-specific information. */
9510 void
9511 dump_tree_statistics (void)
9513 if (GATHER_STATISTICS)
9515 uint64_t total_nodes, total_bytes;
9516 fprintf (stderr, "\nKind Nodes Bytes\n");
9517 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9518 total_nodes = total_bytes = 0;
9521 auto_vec<unsigned> indices (all_kinds);
9522 for (unsigned i = 0; i < all_kinds; i++)
9523 indices.quick_push (i);
9524 indices.qsort (tree_nodes_cmp);
9526 for (unsigned i = 0; i < (int) all_kinds; i++)
9528 unsigned j = indices[i];
9529 fprintf (stderr, "%-20s %6" PRIu64 "%c %9" PRIu64 "%c\n",
9530 tree_node_kind_names[j], SIZE_AMOUNT (tree_node_counts[j]),
9531 SIZE_AMOUNT (tree_node_sizes[j]));
9532 total_nodes += tree_node_counts[j];
9533 total_bytes += tree_node_sizes[j];
9535 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9536 fprintf (stderr, "%-20s %6" PRIu64 "%c %9" PRIu64 "%c\n", "Total",
9537 SIZE_AMOUNT (total_nodes), SIZE_AMOUNT (total_bytes));
9538 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9542 fprintf (stderr, "Code Nodes\n");
9543 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9545 auto_vec<unsigned> indices (MAX_TREE_CODES);
9546 for (unsigned i = 0; i < MAX_TREE_CODES; i++)
9547 indices.quick_push (i);
9548 indices.qsort (tree_codes_cmp);
9550 for (unsigned i = 0; i < MAX_TREE_CODES; i++)
9552 unsigned j = indices[i];
9553 fprintf (stderr, "%-32s %6" PRIu64 "%c\n",
9554 get_tree_code_name ((enum tree_code) j),
9555 SIZE_AMOUNT (tree_code_counts[j]));
9557 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9558 fprintf (stderr, "\n");
9559 ssanames_print_statistics ();
9560 fprintf (stderr, "\n");
9561 phinodes_print_statistics ();
9562 fprintf (stderr, "\n");
9565 else
9566 fprintf (stderr, "(No per-node statistics)\n");
9568 print_type_hash_statistics ();
9569 print_debug_expr_statistics ();
9570 print_value_expr_statistics ();
9571 lang_hooks.print_statistics ();
9574 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
9576 /* Generate a crc32 of the low BYTES bytes of VALUE. */
9578 unsigned
9579 crc32_unsigned_n (unsigned chksum, unsigned value, unsigned bytes)
9581 /* This relies on the raw feedback's top 4 bits being zero. */
9582 #define FEEDBACK(X) ((X) * 0x04c11db7)
9583 #define SYNDROME(X) (FEEDBACK ((X) & 1) ^ FEEDBACK ((X) & 2) \
9584 ^ FEEDBACK ((X) & 4) ^ FEEDBACK ((X) & 8))
9585 static const unsigned syndromes[16] =
9587 SYNDROME(0x0), SYNDROME(0x1), SYNDROME(0x2), SYNDROME(0x3),
9588 SYNDROME(0x4), SYNDROME(0x5), SYNDROME(0x6), SYNDROME(0x7),
9589 SYNDROME(0x8), SYNDROME(0x9), SYNDROME(0xa), SYNDROME(0xb),
9590 SYNDROME(0xc), SYNDROME(0xd), SYNDROME(0xe), SYNDROME(0xf),
9592 #undef FEEDBACK
9593 #undef SYNDROME
9595 value <<= (32 - bytes * 8);
9596 for (unsigned ix = bytes * 2; ix--; value <<= 4)
9598 unsigned feedback = syndromes[((value ^ chksum) >> 28) & 0xf];
9600 chksum = (chksum << 4) ^ feedback;
9603 return chksum;
9606 /* Generate a crc32 of a string. */
9608 unsigned
9609 crc32_string (unsigned chksum, const char *string)
9612 chksum = crc32_byte (chksum, *string);
9613 while (*string++);
9614 return chksum;
9617 /* P is a string that will be used in a symbol. Mask out any characters
9618 that are not valid in that context. */
9620 void
9621 clean_symbol_name (char *p)
9623 for (; *p; p++)
9624 if (! (ISALNUM (*p)
9625 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
9626 || *p == '$'
9627 #endif
9628 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
9629 || *p == '.'
9630 #endif
9632 *p = '_';
9635 static GTY(()) unsigned anon_cnt = 0; /* Saved for PCH. */
9637 /* Create a unique anonymous identifier. The identifier is still a
9638 valid assembly label. */
9640 tree
9641 make_anon_name ()
9643 const char *fmt =
9644 #if !defined (NO_DOT_IN_LABEL)
9646 #elif !defined (NO_DOLLAR_IN_LABEL)
9648 #else
9650 #endif
9651 "_anon_%d";
9653 char buf[24];
9654 int len = snprintf (buf, sizeof (buf), fmt, anon_cnt++);
9655 gcc_checking_assert (len < int (sizeof (buf)));
9657 tree id = get_identifier_with_length (buf, len);
9658 IDENTIFIER_ANON_P (id) = true;
9660 return id;
9663 /* Generate a name for a special-purpose function.
9664 The generated name may need to be unique across the whole link.
9665 Changes to this function may also require corresponding changes to
9666 xstrdup_mask_random.
9667 TYPE is some string to identify the purpose of this function to the
9668 linker or collect2; it must start with an uppercase letter,
9669 one of:
9670 I - for constructors
9671 D - for destructors
9672 N - for C++ anonymous namespaces
9673 F - for DWARF unwind frame information. */
9675 tree
9676 get_file_function_name (const char *type)
9678 char *buf;
9679 const char *p;
9680 char *q;
9682 /* If we already have a name we know to be unique, just use that. */
9683 if (first_global_object_name)
9684 p = q = ASTRDUP (first_global_object_name);
9685 /* If the target is handling the constructors/destructors, they
9686 will be local to this file and the name is only necessary for
9687 debugging purposes.
9688 We also assign sub_I and sub_D sufixes to constructors called from
9689 the global static constructors. These are always local. */
9690 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9691 || (strncmp (type, "sub_", 4) == 0
9692 && (type[4] == 'I' || type[4] == 'D')))
9694 const char *file = main_input_filename;
9695 if (! file)
9696 file = LOCATION_FILE (input_location);
9697 /* Just use the file's basename, because the full pathname
9698 might be quite long. */
9699 p = q = ASTRDUP (lbasename (file));
9701 else
9703 /* Otherwise, the name must be unique across the entire link.
9704 We don't have anything that we know to be unique to this translation
9705 unit, so use what we do have and throw in some randomness. */
9706 unsigned len;
9707 const char *name = weak_global_object_name;
9708 const char *file = main_input_filename;
9710 if (! name)
9711 name = "";
9712 if (! file)
9713 file = LOCATION_FILE (input_location);
9715 len = strlen (file);
9716 q = (char *) alloca (9 + 19 + len + 1);
9717 memcpy (q, file, len + 1);
9719 snprintf (q + len, 9 + 19 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9720 crc32_string (0, name), get_random_seed (false));
9722 p = q;
9725 clean_symbol_name (q);
9726 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9727 + strlen (type));
9729 /* Set up the name of the file-level functions we may need.
9730 Use a global object (which is already required to be unique over
9731 the program) rather than the file name (which imposes extra
9732 constraints). */
9733 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9735 return get_identifier (buf);
9738 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9740 /* Complain that the tree code of NODE does not match the expected 0
9741 terminated list of trailing codes. The trailing code list can be
9742 empty, for a more vague error message. FILE, LINE, and FUNCTION
9743 are of the caller. */
9745 void
9746 tree_check_failed (const_tree node, const char *file,
9747 int line, const char *function, ...)
9749 va_list args;
9750 const char *buffer;
9751 unsigned length = 0;
9752 enum tree_code code;
9754 va_start (args, function);
9755 while ((code = (enum tree_code) va_arg (args, int)))
9756 length += 4 + strlen (get_tree_code_name (code));
9757 va_end (args);
9758 if (length)
9760 char *tmp;
9761 va_start (args, function);
9762 length += strlen ("expected ");
9763 buffer = tmp = (char *) alloca (length);
9764 length = 0;
9765 while ((code = (enum tree_code) va_arg (args, int)))
9767 const char *prefix = length ? " or " : "expected ";
9769 strcpy (tmp + length, prefix);
9770 length += strlen (prefix);
9771 strcpy (tmp + length, get_tree_code_name (code));
9772 length += strlen (get_tree_code_name (code));
9774 va_end (args);
9776 else
9777 buffer = "unexpected node";
9779 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9780 buffer, get_tree_code_name (TREE_CODE (node)),
9781 function, trim_filename (file), line);
9784 /* Complain that the tree code of NODE does match the expected 0
9785 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9786 the caller. */
9788 void
9789 tree_not_check_failed (const_tree node, const char *file,
9790 int line, const char *function, ...)
9792 va_list args;
9793 char *buffer;
9794 unsigned length = 0;
9795 enum tree_code code;
9797 va_start (args, function);
9798 while ((code = (enum tree_code) va_arg (args, int)))
9799 length += 4 + strlen (get_tree_code_name (code));
9800 va_end (args);
9801 va_start (args, function);
9802 buffer = (char *) alloca (length);
9803 length = 0;
9804 while ((code = (enum tree_code) va_arg (args, int)))
9806 if (length)
9808 strcpy (buffer + length, " or ");
9809 length += 4;
9811 strcpy (buffer + length, get_tree_code_name (code));
9812 length += strlen (get_tree_code_name (code));
9814 va_end (args);
9816 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9817 buffer, get_tree_code_name (TREE_CODE (node)),
9818 function, trim_filename (file), line);
9821 /* Similar to tree_check_failed, except that we check for a class of tree
9822 code, given in CL. */
9824 void
9825 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9826 const char *file, int line, const char *function)
9828 internal_error
9829 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9830 TREE_CODE_CLASS_STRING (cl),
9831 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9832 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9835 /* Similar to tree_check_failed, except that instead of specifying a
9836 dozen codes, use the knowledge that they're all sequential. */
9838 void
9839 tree_range_check_failed (const_tree node, const char *file, int line,
9840 const char *function, enum tree_code c1,
9841 enum tree_code c2)
9843 char *buffer;
9844 unsigned length = 0;
9845 unsigned int c;
9847 for (c = c1; c <= c2; ++c)
9848 length += 4 + strlen (get_tree_code_name ((enum tree_code) c));
9850 length += strlen ("expected ");
9851 buffer = (char *) alloca (length);
9852 length = 0;
9854 for (c = c1; c <= c2; ++c)
9856 const char *prefix = length ? " or " : "expected ";
9858 strcpy (buffer + length, prefix);
9859 length += strlen (prefix);
9860 strcpy (buffer + length, get_tree_code_name ((enum tree_code) c));
9861 length += strlen (get_tree_code_name ((enum tree_code) c));
9864 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9865 buffer, get_tree_code_name (TREE_CODE (node)),
9866 function, trim_filename (file), line);
9870 /* Similar to tree_check_failed, except that we check that a tree does
9871 not have the specified code, given in CL. */
9873 void
9874 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9875 const char *file, int line, const char *function)
9877 internal_error
9878 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9879 TREE_CODE_CLASS_STRING (cl),
9880 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9881 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9885 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9887 void
9888 omp_clause_check_failed (const_tree node, const char *file, int line,
9889 const char *function, enum omp_clause_code code)
9891 internal_error ("tree check: expected %<omp_clause %s%>, have %qs "
9892 "in %s, at %s:%d",
9893 omp_clause_code_name[code],
9894 get_tree_code_name (TREE_CODE (node)),
9895 function, trim_filename (file), line);
9899 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9901 void
9902 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9903 const char *function, enum omp_clause_code c1,
9904 enum omp_clause_code c2)
9906 char *buffer;
9907 unsigned length = 0;
9908 unsigned int c;
9910 for (c = c1; c <= c2; ++c)
9911 length += 4 + strlen (omp_clause_code_name[c]);
9913 length += strlen ("expected ");
9914 buffer = (char *) alloca (length);
9915 length = 0;
9917 for (c = c1; c <= c2; ++c)
9919 const char *prefix = length ? " or " : "expected ";
9921 strcpy (buffer + length, prefix);
9922 length += strlen (prefix);
9923 strcpy (buffer + length, omp_clause_code_name[c]);
9924 length += strlen (omp_clause_code_name[c]);
9927 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9928 buffer, omp_clause_code_name[TREE_CODE (node)],
9929 function, trim_filename (file), line);
9933 #undef DEFTREESTRUCT
9934 #define DEFTREESTRUCT(VAL, NAME) NAME,
9936 static const char *ts_enum_names[] = {
9937 #include "treestruct.def"
9939 #undef DEFTREESTRUCT
9941 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9943 /* Similar to tree_class_check_failed, except that we check for
9944 whether CODE contains the tree structure identified by EN. */
9946 void
9947 tree_contains_struct_check_failed (const_tree node,
9948 const enum tree_node_structure_enum en,
9949 const char *file, int line,
9950 const char *function)
9952 internal_error
9953 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9954 TS_ENUM_NAME (en),
9955 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9959 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9960 (dynamically sized) vector. */
9962 void
9963 tree_int_cst_elt_check_failed (int idx, int len, const char *file, int line,
9964 const char *function)
9966 internal_error
9967 ("tree check: accessed elt %d of %<tree_int_cst%> with %d elts in %s, "
9968 "at %s:%d",
9969 idx + 1, len, function, trim_filename (file), line);
9972 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9973 (dynamically sized) vector. */
9975 void
9976 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9977 const char *function)
9979 internal_error
9980 ("tree check: accessed elt %d of %<tree_vec%> with %d elts in %s, at %s:%d",
9981 idx + 1, len, function, trim_filename (file), line);
9984 /* Similar to above, except that the check is for the bounds of the operand
9985 vector of an expression node EXP. */
9987 void
9988 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9989 int line, const char *function)
9991 enum tree_code code = TREE_CODE (exp);
9992 internal_error
9993 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9994 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9995 function, trim_filename (file), line);
9998 /* Similar to above, except that the check is for the number of
9999 operands of an OMP_CLAUSE node. */
10001 void
10002 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
10003 int line, const char *function)
10005 internal_error
10006 ("tree check: accessed operand %d of %<omp_clause %s%> with %d operands "
10007 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
10008 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
10009 trim_filename (file), line);
10011 #endif /* ENABLE_TREE_CHECKING */
10013 /* Create a new vector type node holding NUNITS units of type INNERTYPE,
10014 and mapped to the machine mode MODE. Initialize its fields and build
10015 the information necessary for debugging output. */
10017 static tree
10018 make_vector_type (tree innertype, poly_int64 nunits, machine_mode mode)
10020 tree t;
10021 tree mv_innertype = TYPE_MAIN_VARIANT (innertype);
10023 t = make_node (VECTOR_TYPE);
10024 TREE_TYPE (t) = mv_innertype;
10025 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
10026 SET_TYPE_MODE (t, mode);
10028 if (TYPE_STRUCTURAL_EQUALITY_P (mv_innertype) || in_lto_p)
10029 SET_TYPE_STRUCTURAL_EQUALITY (t);
10030 else if ((TYPE_CANONICAL (mv_innertype) != innertype
10031 || mode != VOIDmode)
10032 && !VECTOR_BOOLEAN_TYPE_P (t))
10033 TYPE_CANONICAL (t)
10034 = make_vector_type (TYPE_CANONICAL (mv_innertype), nunits, VOIDmode);
10036 layout_type (t);
10038 hashval_t hash = type_hash_canon_hash (t);
10039 t = type_hash_canon (hash, t);
10041 /* We have built a main variant, based on the main variant of the
10042 inner type. Use it to build the variant we return. */
10043 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
10044 && TREE_TYPE (t) != innertype)
10045 return build_type_attribute_qual_variant (t,
10046 TYPE_ATTRIBUTES (innertype),
10047 TYPE_QUALS (innertype));
10049 return t;
10052 static tree
10053 make_or_reuse_type (unsigned size, int unsignedp)
10055 int i;
10057 if (size == INT_TYPE_SIZE)
10058 return unsignedp ? unsigned_type_node : integer_type_node;
10059 if (size == CHAR_TYPE_SIZE)
10060 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
10061 if (size == SHORT_TYPE_SIZE)
10062 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
10063 if (size == LONG_TYPE_SIZE)
10064 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
10065 if (size == LONG_LONG_TYPE_SIZE)
10066 return (unsignedp ? long_long_unsigned_type_node
10067 : long_long_integer_type_node);
10069 for (i = 0; i < NUM_INT_N_ENTS; i ++)
10070 if (size == int_n_data[i].bitsize
10071 && int_n_enabled_p[i])
10072 return (unsignedp ? int_n_trees[i].unsigned_type
10073 : int_n_trees[i].signed_type);
10075 if (unsignedp)
10076 return make_unsigned_type (size);
10077 else
10078 return make_signed_type (size);
10081 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
10083 static tree
10084 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
10086 if (satp)
10088 if (size == SHORT_FRACT_TYPE_SIZE)
10089 return unsignedp ? sat_unsigned_short_fract_type_node
10090 : sat_short_fract_type_node;
10091 if (size == FRACT_TYPE_SIZE)
10092 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
10093 if (size == LONG_FRACT_TYPE_SIZE)
10094 return unsignedp ? sat_unsigned_long_fract_type_node
10095 : sat_long_fract_type_node;
10096 if (size == LONG_LONG_FRACT_TYPE_SIZE)
10097 return unsignedp ? sat_unsigned_long_long_fract_type_node
10098 : sat_long_long_fract_type_node;
10100 else
10102 if (size == SHORT_FRACT_TYPE_SIZE)
10103 return unsignedp ? unsigned_short_fract_type_node
10104 : short_fract_type_node;
10105 if (size == FRACT_TYPE_SIZE)
10106 return unsignedp ? unsigned_fract_type_node : fract_type_node;
10107 if (size == LONG_FRACT_TYPE_SIZE)
10108 return unsignedp ? unsigned_long_fract_type_node
10109 : long_fract_type_node;
10110 if (size == LONG_LONG_FRACT_TYPE_SIZE)
10111 return unsignedp ? unsigned_long_long_fract_type_node
10112 : long_long_fract_type_node;
10115 return make_fract_type (size, unsignedp, satp);
10118 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
10120 static tree
10121 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
10123 if (satp)
10125 if (size == SHORT_ACCUM_TYPE_SIZE)
10126 return unsignedp ? sat_unsigned_short_accum_type_node
10127 : sat_short_accum_type_node;
10128 if (size == ACCUM_TYPE_SIZE)
10129 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
10130 if (size == LONG_ACCUM_TYPE_SIZE)
10131 return unsignedp ? sat_unsigned_long_accum_type_node
10132 : sat_long_accum_type_node;
10133 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
10134 return unsignedp ? sat_unsigned_long_long_accum_type_node
10135 : sat_long_long_accum_type_node;
10137 else
10139 if (size == SHORT_ACCUM_TYPE_SIZE)
10140 return unsignedp ? unsigned_short_accum_type_node
10141 : short_accum_type_node;
10142 if (size == ACCUM_TYPE_SIZE)
10143 return unsignedp ? unsigned_accum_type_node : accum_type_node;
10144 if (size == LONG_ACCUM_TYPE_SIZE)
10145 return unsignedp ? unsigned_long_accum_type_node
10146 : long_accum_type_node;
10147 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
10148 return unsignedp ? unsigned_long_long_accum_type_node
10149 : long_long_accum_type_node;
10152 return make_accum_type (size, unsignedp, satp);
10156 /* Create an atomic variant node for TYPE. This routine is called
10157 during initialization of data types to create the 5 basic atomic
10158 types. The generic build_variant_type function requires these to
10159 already be set up in order to function properly, so cannot be
10160 called from there. If ALIGN is non-zero, then ensure alignment is
10161 overridden to this value. */
10163 static tree
10164 build_atomic_base (tree type, unsigned int align)
10166 tree t;
10168 /* Make sure its not already registered. */
10169 if ((t = get_qualified_type (type, TYPE_QUAL_ATOMIC)))
10170 return t;
10172 t = build_variant_type_copy (type);
10173 set_type_quals (t, TYPE_QUAL_ATOMIC);
10175 if (align)
10176 SET_TYPE_ALIGN (t, align);
10178 return t;
10181 /* Information about the _FloatN and _FloatNx types. This must be in
10182 the same order as the corresponding TI_* enum values. */
10183 const floatn_type_info floatn_nx_types[NUM_FLOATN_NX_TYPES] =
10185 { 16, false },
10186 { 32, false },
10187 { 64, false },
10188 { 128, false },
10189 { 32, true },
10190 { 64, true },
10191 { 128, true },
10195 /* Create nodes for all integer types (and error_mark_node) using the sizes
10196 of C datatypes. SIGNED_CHAR specifies whether char is signed. */
10198 void
10199 build_common_tree_nodes (bool signed_char)
10201 int i;
10203 error_mark_node = make_node (ERROR_MARK);
10204 TREE_TYPE (error_mark_node) = error_mark_node;
10206 initialize_sizetypes ();
10208 /* Define both `signed char' and `unsigned char'. */
10209 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
10210 TYPE_STRING_FLAG (signed_char_type_node) = 1;
10211 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
10212 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
10214 /* Define `char', which is like either `signed char' or `unsigned char'
10215 but not the same as either. */
10216 char_type_node
10217 = (signed_char
10218 ? make_signed_type (CHAR_TYPE_SIZE)
10219 : make_unsigned_type (CHAR_TYPE_SIZE));
10220 TYPE_STRING_FLAG (char_type_node) = 1;
10222 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
10223 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
10224 integer_type_node = make_signed_type (INT_TYPE_SIZE);
10225 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
10226 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
10227 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
10228 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
10229 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
10231 for (i = 0; i < NUM_INT_N_ENTS; i ++)
10233 int_n_trees[i].signed_type = make_signed_type (int_n_data[i].bitsize);
10234 int_n_trees[i].unsigned_type = make_unsigned_type (int_n_data[i].bitsize);
10236 if (int_n_enabled_p[i])
10238 integer_types[itk_intN_0 + i * 2] = int_n_trees[i].signed_type;
10239 integer_types[itk_unsigned_intN_0 + i * 2] = int_n_trees[i].unsigned_type;
10243 /* Define a boolean type. This type only represents boolean values but
10244 may be larger than char depending on the value of BOOL_TYPE_SIZE. */
10245 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
10246 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
10247 TYPE_PRECISION (boolean_type_node) = 1;
10248 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
10250 /* Define what type to use for size_t. */
10251 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
10252 size_type_node = unsigned_type_node;
10253 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
10254 size_type_node = long_unsigned_type_node;
10255 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
10256 size_type_node = long_long_unsigned_type_node;
10257 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
10258 size_type_node = short_unsigned_type_node;
10259 else
10261 int i;
10263 size_type_node = NULL_TREE;
10264 for (i = 0; i < NUM_INT_N_ENTS; i++)
10265 if (int_n_enabled_p[i])
10267 char name[50], altname[50];
10268 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
10269 sprintf (altname, "__int%d__ unsigned", int_n_data[i].bitsize);
10271 if (strcmp (name, SIZE_TYPE) == 0
10272 || strcmp (altname, SIZE_TYPE) == 0)
10274 size_type_node = int_n_trees[i].unsigned_type;
10277 if (size_type_node == NULL_TREE)
10278 gcc_unreachable ();
10281 /* Define what type to use for ptrdiff_t. */
10282 if (strcmp (PTRDIFF_TYPE, "int") == 0)
10283 ptrdiff_type_node = integer_type_node;
10284 else if (strcmp (PTRDIFF_TYPE, "long int") == 0)
10285 ptrdiff_type_node = long_integer_type_node;
10286 else if (strcmp (PTRDIFF_TYPE, "long long int") == 0)
10287 ptrdiff_type_node = long_long_integer_type_node;
10288 else if (strcmp (PTRDIFF_TYPE, "short int") == 0)
10289 ptrdiff_type_node = short_integer_type_node;
10290 else
10292 ptrdiff_type_node = NULL_TREE;
10293 for (int i = 0; i < NUM_INT_N_ENTS; i++)
10294 if (int_n_enabled_p[i])
10296 char name[50], altname[50];
10297 sprintf (name, "__int%d", int_n_data[i].bitsize);
10298 sprintf (altname, "__int%d__", int_n_data[i].bitsize);
10300 if (strcmp (name, PTRDIFF_TYPE) == 0
10301 || strcmp (altname, PTRDIFF_TYPE) == 0)
10302 ptrdiff_type_node = int_n_trees[i].signed_type;
10304 if (ptrdiff_type_node == NULL_TREE)
10305 gcc_unreachable ();
10308 /* Fill in the rest of the sized types. Reuse existing type nodes
10309 when possible. */
10310 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
10311 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
10312 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
10313 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
10314 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
10316 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
10317 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
10318 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
10319 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
10320 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
10322 /* Don't call build_qualified type for atomics. That routine does
10323 special processing for atomics, and until they are initialized
10324 it's better not to make that call.
10326 Check to see if there is a target override for atomic types. */
10328 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
10329 targetm.atomic_align_for_mode (QImode));
10330 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
10331 targetm.atomic_align_for_mode (HImode));
10332 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
10333 targetm.atomic_align_for_mode (SImode));
10334 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
10335 targetm.atomic_align_for_mode (DImode));
10336 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
10337 targetm.atomic_align_for_mode (TImode));
10339 access_public_node = get_identifier ("public");
10340 access_protected_node = get_identifier ("protected");
10341 access_private_node = get_identifier ("private");
10343 /* Define these next since types below may used them. */
10344 integer_zero_node = build_int_cst (integer_type_node, 0);
10345 integer_one_node = build_int_cst (integer_type_node, 1);
10346 integer_three_node = build_int_cst (integer_type_node, 3);
10347 integer_minus_one_node = build_int_cst (integer_type_node, -1);
10349 size_zero_node = size_int (0);
10350 size_one_node = size_int (1);
10351 bitsize_zero_node = bitsize_int (0);
10352 bitsize_one_node = bitsize_int (1);
10353 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
10355 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
10356 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
10358 void_type_node = make_node (VOID_TYPE);
10359 layout_type (void_type_node);
10361 /* We are not going to have real types in C with less than byte alignment,
10362 so we might as well not have any types that claim to have it. */
10363 SET_TYPE_ALIGN (void_type_node, BITS_PER_UNIT);
10364 TYPE_USER_ALIGN (void_type_node) = 0;
10366 void_node = make_node (VOID_CST);
10367 TREE_TYPE (void_node) = void_type_node;
10369 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
10370 layout_type (TREE_TYPE (null_pointer_node));
10372 ptr_type_node = build_pointer_type (void_type_node);
10373 const_ptr_type_node
10374 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
10375 for (unsigned i = 0;
10376 i < sizeof (builtin_structptr_types) / sizeof (builtin_structptr_type);
10377 ++i)
10378 builtin_structptr_types[i].node = builtin_structptr_types[i].base;
10380 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
10382 float_type_node = make_node (REAL_TYPE);
10383 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
10384 layout_type (float_type_node);
10386 double_type_node = make_node (REAL_TYPE);
10387 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
10388 layout_type (double_type_node);
10390 long_double_type_node = make_node (REAL_TYPE);
10391 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
10392 layout_type (long_double_type_node);
10394 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
10396 int n = floatn_nx_types[i].n;
10397 bool extended = floatn_nx_types[i].extended;
10398 scalar_float_mode mode;
10399 if (!targetm.floatn_mode (n, extended).exists (&mode))
10400 continue;
10401 int precision = GET_MODE_PRECISION (mode);
10402 /* Work around the rs6000 KFmode having precision 113 not
10403 128. */
10404 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
10405 gcc_assert (fmt->b == 2 && fmt->emin + fmt->emax == 3);
10406 int min_precision = fmt->p + ceil_log2 (fmt->emax - fmt->emin);
10407 if (!extended)
10408 gcc_assert (min_precision == n);
10409 if (precision < min_precision)
10410 precision = min_precision;
10411 FLOATN_NX_TYPE_NODE (i) = make_node (REAL_TYPE);
10412 TYPE_PRECISION (FLOATN_NX_TYPE_NODE (i)) = precision;
10413 layout_type (FLOATN_NX_TYPE_NODE (i));
10414 SET_TYPE_MODE (FLOATN_NX_TYPE_NODE (i), mode);
10417 float_ptr_type_node = build_pointer_type (float_type_node);
10418 double_ptr_type_node = build_pointer_type (double_type_node);
10419 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
10420 integer_ptr_type_node = build_pointer_type (integer_type_node);
10422 /* Fixed size integer types. */
10423 uint16_type_node = make_or_reuse_type (16, 1);
10424 uint32_type_node = make_or_reuse_type (32, 1);
10425 uint64_type_node = make_or_reuse_type (64, 1);
10426 if (targetm.scalar_mode_supported_p (TImode))
10427 uint128_type_node = make_or_reuse_type (128, 1);
10429 /* Decimal float types. */
10430 if (targetm.decimal_float_supported_p ())
10432 dfloat32_type_node = make_node (REAL_TYPE);
10433 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
10434 SET_TYPE_MODE (dfloat32_type_node, SDmode);
10435 layout_type (dfloat32_type_node);
10437 dfloat64_type_node = make_node (REAL_TYPE);
10438 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
10439 SET_TYPE_MODE (dfloat64_type_node, DDmode);
10440 layout_type (dfloat64_type_node);
10442 dfloat128_type_node = make_node (REAL_TYPE);
10443 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
10444 SET_TYPE_MODE (dfloat128_type_node, TDmode);
10445 layout_type (dfloat128_type_node);
10448 complex_integer_type_node = build_complex_type (integer_type_node, true);
10449 complex_float_type_node = build_complex_type (float_type_node, true);
10450 complex_double_type_node = build_complex_type (double_type_node, true);
10451 complex_long_double_type_node = build_complex_type (long_double_type_node,
10452 true);
10454 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
10456 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
10457 COMPLEX_FLOATN_NX_TYPE_NODE (i)
10458 = build_complex_type (FLOATN_NX_TYPE_NODE (i));
10461 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
10462 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
10463 sat_ ## KIND ## _type_node = \
10464 make_sat_signed_ ## KIND ## _type (SIZE); \
10465 sat_unsigned_ ## KIND ## _type_node = \
10466 make_sat_unsigned_ ## KIND ## _type (SIZE); \
10467 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
10468 unsigned_ ## KIND ## _type_node = \
10469 make_unsigned_ ## KIND ## _type (SIZE);
10471 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
10472 sat_ ## WIDTH ## KIND ## _type_node = \
10473 make_sat_signed_ ## KIND ## _type (SIZE); \
10474 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
10475 make_sat_unsigned_ ## KIND ## _type (SIZE); \
10476 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
10477 unsigned_ ## WIDTH ## KIND ## _type_node = \
10478 make_unsigned_ ## KIND ## _type (SIZE);
10480 /* Make fixed-point type nodes based on four different widths. */
10481 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
10482 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
10483 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
10484 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
10485 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
10487 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
10488 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
10489 NAME ## _type_node = \
10490 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
10491 u ## NAME ## _type_node = \
10492 make_or_reuse_unsigned_ ## KIND ## _type \
10493 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
10494 sat_ ## NAME ## _type_node = \
10495 make_or_reuse_sat_signed_ ## KIND ## _type \
10496 (GET_MODE_BITSIZE (MODE ## mode)); \
10497 sat_u ## NAME ## _type_node = \
10498 make_or_reuse_sat_unsigned_ ## KIND ## _type \
10499 (GET_MODE_BITSIZE (U ## MODE ## mode));
10501 /* Fixed-point type and mode nodes. */
10502 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
10503 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
10504 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
10505 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
10506 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
10507 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
10508 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
10509 MAKE_FIXED_MODE_NODE (accum, ha, HA)
10510 MAKE_FIXED_MODE_NODE (accum, sa, SA)
10511 MAKE_FIXED_MODE_NODE (accum, da, DA)
10512 MAKE_FIXED_MODE_NODE (accum, ta, TA)
10515 tree t = targetm.build_builtin_va_list ();
10517 /* Many back-ends define record types without setting TYPE_NAME.
10518 If we copied the record type here, we'd keep the original
10519 record type without a name. This breaks name mangling. So,
10520 don't copy record types and let c_common_nodes_and_builtins()
10521 declare the type to be __builtin_va_list. */
10522 if (TREE_CODE (t) != RECORD_TYPE)
10523 t = build_variant_type_copy (t);
10525 va_list_type_node = t;
10528 /* SCEV analyzer global shared trees. */
10529 chrec_dont_know = make_node (SCEV_NOT_KNOWN);
10530 TREE_TYPE (chrec_dont_know) = void_type_node;
10531 chrec_known = make_node (SCEV_KNOWN);
10532 TREE_TYPE (chrec_known) = void_type_node;
10535 /* Modify DECL for given flags.
10536 TM_PURE attribute is set only on types, so the function will modify
10537 DECL's type when ECF_TM_PURE is used. */
10539 void
10540 set_call_expr_flags (tree decl, int flags)
10542 if (flags & ECF_NOTHROW)
10543 TREE_NOTHROW (decl) = 1;
10544 if (flags & ECF_CONST)
10545 TREE_READONLY (decl) = 1;
10546 if (flags & ECF_PURE)
10547 DECL_PURE_P (decl) = 1;
10548 if (flags & ECF_LOOPING_CONST_OR_PURE)
10549 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
10550 if (flags & ECF_NOVOPS)
10551 DECL_IS_NOVOPS (decl) = 1;
10552 if (flags & ECF_NORETURN)
10553 TREE_THIS_VOLATILE (decl) = 1;
10554 if (flags & ECF_MALLOC)
10555 DECL_IS_MALLOC (decl) = 1;
10556 if (flags & ECF_RETURNS_TWICE)
10557 DECL_IS_RETURNS_TWICE (decl) = 1;
10558 if (flags & ECF_LEAF)
10559 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
10560 NULL, DECL_ATTRIBUTES (decl));
10561 if (flags & ECF_COLD)
10562 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("cold"),
10563 NULL, DECL_ATTRIBUTES (decl));
10564 if (flags & ECF_RET1)
10565 DECL_ATTRIBUTES (decl)
10566 = tree_cons (get_identifier ("fn spec"),
10567 build_tree_list (NULL_TREE, build_string (2, "1 ")),
10568 DECL_ATTRIBUTES (decl));
10569 if ((flags & ECF_TM_PURE) && flag_tm)
10570 apply_tm_attr (decl, get_identifier ("transaction_pure"));
10571 /* Looping const or pure is implied by noreturn.
10572 There is currently no way to declare looping const or looping pure alone. */
10573 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
10574 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
10578 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
10580 static void
10581 local_define_builtin (const char *name, tree type, enum built_in_function code,
10582 const char *library_name, int ecf_flags)
10584 tree decl;
10586 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
10587 library_name, NULL_TREE);
10588 set_call_expr_flags (decl, ecf_flags);
10590 set_builtin_decl (code, decl, true);
10593 /* Call this function after instantiating all builtins that the language
10594 front end cares about. This will build the rest of the builtins
10595 and internal functions that are relied upon by the tree optimizers and
10596 the middle-end. */
10598 void
10599 build_common_builtin_nodes (void)
10601 tree tmp, ftype;
10602 int ecf_flags;
10604 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE)
10605 || !builtin_decl_explicit_p (BUILT_IN_ABORT))
10607 ftype = build_function_type (void_type_node, void_list_node);
10608 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
10609 local_define_builtin ("__builtin_unreachable", ftype,
10610 BUILT_IN_UNREACHABLE,
10611 "__builtin_unreachable",
10612 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
10613 | ECF_CONST | ECF_COLD);
10614 if (!builtin_decl_explicit_p (BUILT_IN_ABORT))
10615 local_define_builtin ("__builtin_abort", ftype, BUILT_IN_ABORT,
10616 "abort",
10617 ECF_LEAF | ECF_NORETURN | ECF_CONST | ECF_COLD);
10620 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
10621 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10623 ftype = build_function_type_list (ptr_type_node,
10624 ptr_type_node, const_ptr_type_node,
10625 size_type_node, NULL_TREE);
10627 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
10628 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
10629 "memcpy", ECF_NOTHROW | ECF_LEAF);
10630 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10631 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
10632 "memmove", ECF_NOTHROW | ECF_LEAF);
10635 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
10637 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
10638 const_ptr_type_node, size_type_node,
10639 NULL_TREE);
10640 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
10641 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10644 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
10646 ftype = build_function_type_list (ptr_type_node,
10647 ptr_type_node, integer_type_node,
10648 size_type_node, NULL_TREE);
10649 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
10650 "memset", ECF_NOTHROW | ECF_LEAF);
10653 /* If we're checking the stack, `alloca' can throw. */
10654 const int alloca_flags
10655 = ECF_MALLOC | ECF_LEAF | (flag_stack_check ? 0 : ECF_NOTHROW);
10657 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
10659 ftype = build_function_type_list (ptr_type_node,
10660 size_type_node, NULL_TREE);
10661 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
10662 "alloca", alloca_flags);
10665 ftype = build_function_type_list (ptr_type_node, size_type_node,
10666 size_type_node, NULL_TREE);
10667 local_define_builtin ("__builtin_alloca_with_align", ftype,
10668 BUILT_IN_ALLOCA_WITH_ALIGN,
10669 "__builtin_alloca_with_align",
10670 alloca_flags);
10672 ftype = build_function_type_list (ptr_type_node, size_type_node,
10673 size_type_node, size_type_node, NULL_TREE);
10674 local_define_builtin ("__builtin_alloca_with_align_and_max", ftype,
10675 BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX,
10676 "__builtin_alloca_with_align_and_max",
10677 alloca_flags);
10679 ftype = build_function_type_list (void_type_node,
10680 ptr_type_node, ptr_type_node,
10681 ptr_type_node, NULL_TREE);
10682 local_define_builtin ("__builtin_init_trampoline", ftype,
10683 BUILT_IN_INIT_TRAMPOLINE,
10684 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
10685 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
10686 BUILT_IN_INIT_HEAP_TRAMPOLINE,
10687 "__builtin_init_heap_trampoline",
10688 ECF_NOTHROW | ECF_LEAF);
10689 local_define_builtin ("__builtin_init_descriptor", ftype,
10690 BUILT_IN_INIT_DESCRIPTOR,
10691 "__builtin_init_descriptor", ECF_NOTHROW | ECF_LEAF);
10693 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
10694 local_define_builtin ("__builtin_adjust_trampoline", ftype,
10695 BUILT_IN_ADJUST_TRAMPOLINE,
10696 "__builtin_adjust_trampoline",
10697 ECF_CONST | ECF_NOTHROW);
10698 local_define_builtin ("__builtin_adjust_descriptor", ftype,
10699 BUILT_IN_ADJUST_DESCRIPTOR,
10700 "__builtin_adjust_descriptor",
10701 ECF_CONST | ECF_NOTHROW);
10703 ftype = build_function_type_list (void_type_node,
10704 ptr_type_node, ptr_type_node, NULL_TREE);
10705 local_define_builtin ("__builtin_nonlocal_goto", ftype,
10706 BUILT_IN_NONLOCAL_GOTO,
10707 "__builtin_nonlocal_goto",
10708 ECF_NORETURN | ECF_NOTHROW);
10710 ftype = build_function_type_list (void_type_node,
10711 ptr_type_node, ptr_type_node, NULL_TREE);
10712 local_define_builtin ("__builtin_setjmp_setup", ftype,
10713 BUILT_IN_SETJMP_SETUP,
10714 "__builtin_setjmp_setup", ECF_NOTHROW);
10716 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10717 local_define_builtin ("__builtin_setjmp_receiver", ftype,
10718 BUILT_IN_SETJMP_RECEIVER,
10719 "__builtin_setjmp_receiver", ECF_NOTHROW | ECF_LEAF);
10721 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
10722 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
10723 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
10725 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10726 local_define_builtin ("__builtin_stack_restore", ftype,
10727 BUILT_IN_STACK_RESTORE,
10728 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
10730 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
10731 const_ptr_type_node, size_type_node,
10732 NULL_TREE);
10733 local_define_builtin ("__builtin_memcmp_eq", ftype, BUILT_IN_MEMCMP_EQ,
10734 "__builtin_memcmp_eq",
10735 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10737 local_define_builtin ("__builtin_strncmp_eq", ftype, BUILT_IN_STRNCMP_EQ,
10738 "__builtin_strncmp_eq",
10739 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10741 local_define_builtin ("__builtin_strcmp_eq", ftype, BUILT_IN_STRCMP_EQ,
10742 "__builtin_strcmp_eq",
10743 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10745 /* If there's a possibility that we might use the ARM EABI, build the
10746 alternate __cxa_end_cleanup node used to resume from C++. */
10747 if (targetm.arm_eabi_unwinder)
10749 ftype = build_function_type_list (void_type_node, NULL_TREE);
10750 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
10751 BUILT_IN_CXA_END_CLEANUP,
10752 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
10755 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10756 local_define_builtin ("__builtin_unwind_resume", ftype,
10757 BUILT_IN_UNWIND_RESUME,
10758 ((targetm_common.except_unwind_info (&global_options)
10759 == UI_SJLJ)
10760 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
10761 ECF_NORETURN);
10763 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
10765 ftype = build_function_type_list (ptr_type_node, integer_type_node,
10766 NULL_TREE);
10767 local_define_builtin ("__builtin_return_address", ftype,
10768 BUILT_IN_RETURN_ADDRESS,
10769 "__builtin_return_address",
10770 ECF_NOTHROW);
10773 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
10774 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10776 ftype = build_function_type_list (void_type_node, ptr_type_node,
10777 ptr_type_node, NULL_TREE);
10778 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
10779 local_define_builtin ("__cyg_profile_func_enter", ftype,
10780 BUILT_IN_PROFILE_FUNC_ENTER,
10781 "__cyg_profile_func_enter", 0);
10782 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10783 local_define_builtin ("__cyg_profile_func_exit", ftype,
10784 BUILT_IN_PROFILE_FUNC_EXIT,
10785 "__cyg_profile_func_exit", 0);
10788 /* The exception object and filter values from the runtime. The argument
10789 must be zero before exception lowering, i.e. from the front end. After
10790 exception lowering, it will be the region number for the exception
10791 landing pad. These functions are PURE instead of CONST to prevent
10792 them from being hoisted past the exception edge that will initialize
10793 its value in the landing pad. */
10794 ftype = build_function_type_list (ptr_type_node,
10795 integer_type_node, NULL_TREE);
10796 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
10797 /* Only use TM_PURE if we have TM language support. */
10798 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
10799 ecf_flags |= ECF_TM_PURE;
10800 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
10801 "__builtin_eh_pointer", ecf_flags);
10803 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
10804 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
10805 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
10806 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10808 ftype = build_function_type_list (void_type_node,
10809 integer_type_node, integer_type_node,
10810 NULL_TREE);
10811 local_define_builtin ("__builtin_eh_copy_values", ftype,
10812 BUILT_IN_EH_COPY_VALUES,
10813 "__builtin_eh_copy_values", ECF_NOTHROW);
10815 /* Complex multiplication and division. These are handled as builtins
10816 rather than optabs because emit_library_call_value doesn't support
10817 complex. Further, we can do slightly better with folding these
10818 beasties if the real and complex parts of the arguments are separate. */
10820 int mode;
10822 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10824 char mode_name_buf[4], *q;
10825 const char *p;
10826 enum built_in_function mcode, dcode;
10827 tree type, inner_type;
10828 const char *prefix = "__";
10830 if (targetm.libfunc_gnu_prefix)
10831 prefix = "__gnu_";
10833 type = lang_hooks.types.type_for_mode ((machine_mode) mode, 0);
10834 if (type == NULL)
10835 continue;
10836 inner_type = TREE_TYPE (type);
10838 ftype = build_function_type_list (type, inner_type, inner_type,
10839 inner_type, inner_type, NULL_TREE);
10841 mcode = ((enum built_in_function)
10842 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10843 dcode = ((enum built_in_function)
10844 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10846 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10847 *q = TOLOWER (*p);
10848 *q = '\0';
10850 /* For -ftrapping-math these should throw from a former
10851 -fnon-call-exception stmt. */
10852 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10853 NULL);
10854 local_define_builtin (built_in_names[mcode], ftype, mcode,
10855 built_in_names[mcode],
10856 ECF_CONST | ECF_LEAF);
10858 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10859 NULL);
10860 local_define_builtin (built_in_names[dcode], ftype, dcode,
10861 built_in_names[dcode],
10862 ECF_CONST | ECF_LEAF);
10866 init_internal_fns ();
10869 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10870 better way.
10872 If we requested a pointer to a vector, build up the pointers that
10873 we stripped off while looking for the inner type. Similarly for
10874 return values from functions.
10876 The argument TYPE is the top of the chain, and BOTTOM is the
10877 new type which we will point to. */
10879 tree
10880 reconstruct_complex_type (tree type, tree bottom)
10882 tree inner, outer;
10884 if (TREE_CODE (type) == POINTER_TYPE)
10886 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10887 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10888 TYPE_REF_CAN_ALIAS_ALL (type));
10890 else if (TREE_CODE (type) == REFERENCE_TYPE)
10892 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10893 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10894 TYPE_REF_CAN_ALIAS_ALL (type));
10896 else if (TREE_CODE (type) == ARRAY_TYPE)
10898 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10899 outer = build_array_type (inner, TYPE_DOMAIN (type));
10901 else if (TREE_CODE (type) == FUNCTION_TYPE)
10903 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10904 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
10906 else if (TREE_CODE (type) == METHOD_TYPE)
10908 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10909 /* The build_method_type_directly() routine prepends 'this' to argument list,
10910 so we must compensate by getting rid of it. */
10911 outer
10912 = build_method_type_directly
10913 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10914 inner,
10915 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10917 else if (TREE_CODE (type) == OFFSET_TYPE)
10919 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10920 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10922 else
10923 return bottom;
10925 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10926 TYPE_QUALS (type));
10929 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10930 the inner type. */
10931 tree
10932 build_vector_type_for_mode (tree innertype, machine_mode mode)
10934 poly_int64 nunits;
10935 unsigned int bitsize;
10937 switch (GET_MODE_CLASS (mode))
10939 case MODE_VECTOR_BOOL:
10940 case MODE_VECTOR_INT:
10941 case MODE_VECTOR_FLOAT:
10942 case MODE_VECTOR_FRACT:
10943 case MODE_VECTOR_UFRACT:
10944 case MODE_VECTOR_ACCUM:
10945 case MODE_VECTOR_UACCUM:
10946 nunits = GET_MODE_NUNITS (mode);
10947 break;
10949 case MODE_INT:
10950 /* Check that there are no leftover bits. */
10951 bitsize = GET_MODE_BITSIZE (as_a <scalar_int_mode> (mode));
10952 gcc_assert (bitsize % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10953 nunits = bitsize / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10954 break;
10956 default:
10957 gcc_unreachable ();
10960 return make_vector_type (innertype, nunits, mode);
10963 /* Similarly, but takes the inner type and number of units, which must be
10964 a power of two. */
10966 tree
10967 build_vector_type (tree innertype, poly_int64 nunits)
10969 return make_vector_type (innertype, nunits, VOIDmode);
10972 /* Build a truth vector with NUNITS units, giving it mode MASK_MODE. */
10974 tree
10975 build_truth_vector_type_for_mode (poly_uint64 nunits, machine_mode mask_mode)
10977 gcc_assert (mask_mode != BLKmode);
10979 unsigned HOST_WIDE_INT esize;
10980 if (VECTOR_MODE_P (mask_mode))
10982 poly_uint64 vsize = GET_MODE_BITSIZE (mask_mode);
10983 esize = vector_element_size (vsize, nunits);
10985 else
10986 esize = 1;
10988 tree bool_type = build_nonstandard_boolean_type (esize);
10990 return make_vector_type (bool_type, nunits, mask_mode);
10993 /* Build a vector type that holds one boolean result for each element of
10994 vector type VECTYPE. The public interface for this operation is
10995 truth_type_for. */
10997 static tree
10998 build_truth_vector_type_for (tree vectype)
11000 machine_mode vector_mode = TYPE_MODE (vectype);
11001 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
11003 machine_mode mask_mode;
11004 if (VECTOR_MODE_P (vector_mode)
11005 && targetm.vectorize.get_mask_mode (vector_mode).exists (&mask_mode))
11006 return build_truth_vector_type_for_mode (nunits, mask_mode);
11008 poly_uint64 vsize = tree_to_poly_uint64 (TYPE_SIZE (vectype));
11009 unsigned HOST_WIDE_INT esize = vector_element_size (vsize, nunits);
11010 tree bool_type = build_nonstandard_boolean_type (esize);
11012 return make_vector_type (bool_type, nunits, VOIDmode);
11015 /* Like build_vector_type, but builds a variant type with TYPE_VECTOR_OPAQUE
11016 set. */
11018 tree
11019 build_opaque_vector_type (tree innertype, poly_int64 nunits)
11021 tree t = make_vector_type (innertype, nunits, VOIDmode);
11022 tree cand;
11023 /* We always build the non-opaque variant before the opaque one,
11024 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
11025 cand = TYPE_NEXT_VARIANT (t);
11026 if (cand
11027 && TYPE_VECTOR_OPAQUE (cand)
11028 && check_qualified_type (cand, t, TYPE_QUALS (t)))
11029 return cand;
11030 /* Othewise build a variant type and make sure to queue it after
11031 the non-opaque type. */
11032 cand = build_distinct_type_copy (t);
11033 TYPE_VECTOR_OPAQUE (cand) = true;
11034 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
11035 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
11036 TYPE_NEXT_VARIANT (t) = cand;
11037 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
11038 return cand;
11041 /* Return the value of element I of VECTOR_CST T as a wide_int. */
11043 wide_int
11044 vector_cst_int_elt (const_tree t, unsigned int i)
11046 /* First handle elements that are directly encoded. */
11047 unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
11048 if (i < encoded_nelts)
11049 return wi::to_wide (VECTOR_CST_ENCODED_ELT (t, i));
11051 /* Identify the pattern that contains element I and work out the index of
11052 the last encoded element for that pattern. */
11053 unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
11054 unsigned int pattern = i % npatterns;
11055 unsigned int count = i / npatterns;
11056 unsigned int final_i = encoded_nelts - npatterns + pattern;
11058 /* If there are no steps, the final encoded value is the right one. */
11059 if (!VECTOR_CST_STEPPED_P (t))
11060 return wi::to_wide (VECTOR_CST_ENCODED_ELT (t, final_i));
11062 /* Otherwise work out the value from the last two encoded elements. */
11063 tree v1 = VECTOR_CST_ENCODED_ELT (t, final_i - npatterns);
11064 tree v2 = VECTOR_CST_ENCODED_ELT (t, final_i);
11065 wide_int diff = wi::to_wide (v2) - wi::to_wide (v1);
11066 return wi::to_wide (v2) + (count - 2) * diff;
11069 /* Return the value of element I of VECTOR_CST T. */
11071 tree
11072 vector_cst_elt (const_tree t, unsigned int i)
11074 /* First handle elements that are directly encoded. */
11075 unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
11076 if (i < encoded_nelts)
11077 return VECTOR_CST_ENCODED_ELT (t, i);
11079 /* If there are no steps, the final encoded value is the right one. */
11080 if (!VECTOR_CST_STEPPED_P (t))
11082 /* Identify the pattern that contains element I and work out the index of
11083 the last encoded element for that pattern. */
11084 unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
11085 unsigned int pattern = i % npatterns;
11086 unsigned int final_i = encoded_nelts - npatterns + pattern;
11087 return VECTOR_CST_ENCODED_ELT (t, final_i);
11090 /* Otherwise work out the value from the last two encoded elements. */
11091 return wide_int_to_tree (TREE_TYPE (TREE_TYPE (t)),
11092 vector_cst_int_elt (t, i));
11095 /* Given an initializer INIT, return TRUE if INIT is zero or some
11096 aggregate of zeros. Otherwise return FALSE. If NONZERO is not
11097 null, set *NONZERO if and only if INIT is known not to be all
11098 zeros. The combination of return value of false and *NONZERO
11099 false implies that INIT may but need not be all zeros. Other
11100 combinations indicate definitive answers. */
11102 bool
11103 initializer_zerop (const_tree init, bool *nonzero /* = NULL */)
11105 bool dummy;
11106 if (!nonzero)
11107 nonzero = &dummy;
11109 /* Conservatively clear NONZERO and set it only if INIT is definitely
11110 not all zero. */
11111 *nonzero = false;
11113 STRIP_NOPS (init);
11115 unsigned HOST_WIDE_INT off = 0;
11117 switch (TREE_CODE (init))
11119 case INTEGER_CST:
11120 if (integer_zerop (init))
11121 return true;
11123 *nonzero = true;
11124 return false;
11126 case REAL_CST:
11127 /* ??? Note that this is not correct for C4X float formats. There,
11128 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
11129 negative exponent. */
11130 if (real_zerop (init)
11131 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init)))
11132 return true;
11134 *nonzero = true;
11135 return false;
11137 case FIXED_CST:
11138 if (fixed_zerop (init))
11139 return true;
11141 *nonzero = true;
11142 return false;
11144 case COMPLEX_CST:
11145 if (integer_zerop (init)
11146 || (real_zerop (init)
11147 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
11148 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init)))))
11149 return true;
11151 *nonzero = true;
11152 return false;
11154 case VECTOR_CST:
11155 if (VECTOR_CST_NPATTERNS (init) == 1
11156 && VECTOR_CST_DUPLICATE_P (init)
11157 && initializer_zerop (VECTOR_CST_ENCODED_ELT (init, 0)))
11158 return true;
11160 *nonzero = true;
11161 return false;
11163 case CONSTRUCTOR:
11165 if (TREE_CLOBBER_P (init))
11166 return false;
11168 unsigned HOST_WIDE_INT idx;
11169 tree elt;
11171 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
11172 if (!initializer_zerop (elt, nonzero))
11173 return false;
11175 return true;
11178 case MEM_REF:
11180 tree arg = TREE_OPERAND (init, 0);
11181 if (TREE_CODE (arg) != ADDR_EXPR)
11182 return false;
11183 tree offset = TREE_OPERAND (init, 1);
11184 if (TREE_CODE (offset) != INTEGER_CST
11185 || !tree_fits_uhwi_p (offset))
11186 return false;
11187 off = tree_to_uhwi (offset);
11188 if (INT_MAX < off)
11189 return false;
11190 arg = TREE_OPERAND (arg, 0);
11191 if (TREE_CODE (arg) != STRING_CST)
11192 return false;
11193 init = arg;
11195 /* Fall through. */
11197 case STRING_CST:
11199 gcc_assert (off <= INT_MAX);
11201 int i = off;
11202 int n = TREE_STRING_LENGTH (init);
11203 if (n <= i)
11204 return false;
11206 /* We need to loop through all elements to handle cases like
11207 "\0" and "\0foobar". */
11208 for (i = 0; i < n; ++i)
11209 if (TREE_STRING_POINTER (init)[i] != '\0')
11211 *nonzero = true;
11212 return false;
11215 return true;
11218 default:
11219 return false;
11223 /* Return true if EXPR is an initializer expression in which every element
11224 is a constant that is numerically equal to 0 or 1. The elements do not
11225 need to be equal to each other. */
11227 bool
11228 initializer_each_zero_or_onep (const_tree expr)
11230 STRIP_ANY_LOCATION_WRAPPER (expr);
11232 switch (TREE_CODE (expr))
11234 case INTEGER_CST:
11235 return integer_zerop (expr) || integer_onep (expr);
11237 case REAL_CST:
11238 return real_zerop (expr) || real_onep (expr);
11240 case VECTOR_CST:
11242 unsigned HOST_WIDE_INT nelts = vector_cst_encoded_nelts (expr);
11243 if (VECTOR_CST_STEPPED_P (expr)
11244 && !TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr)).is_constant (&nelts))
11245 return false;
11247 for (unsigned int i = 0; i < nelts; ++i)
11249 tree elt = vector_cst_elt (expr, i);
11250 if (!initializer_each_zero_or_onep (elt))
11251 return false;
11254 return true;
11257 default:
11258 return false;
11262 /* Check if vector VEC consists of all the equal elements and
11263 that the number of elements corresponds to the type of VEC.
11264 The function returns first element of the vector
11265 or NULL_TREE if the vector is not uniform. */
11266 tree
11267 uniform_vector_p (const_tree vec)
11269 tree first, t;
11270 unsigned HOST_WIDE_INT i, nelts;
11272 if (vec == NULL_TREE)
11273 return NULL_TREE;
11275 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
11277 if (TREE_CODE (vec) == VEC_DUPLICATE_EXPR)
11278 return TREE_OPERAND (vec, 0);
11280 else if (TREE_CODE (vec) == VECTOR_CST)
11282 if (VECTOR_CST_NPATTERNS (vec) == 1 && VECTOR_CST_DUPLICATE_P (vec))
11283 return VECTOR_CST_ENCODED_ELT (vec, 0);
11284 return NULL_TREE;
11287 else if (TREE_CODE (vec) == CONSTRUCTOR
11288 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)).is_constant (&nelts))
11290 first = error_mark_node;
11292 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
11294 if (i == 0)
11296 first = t;
11297 continue;
11299 if (!operand_equal_p (first, t, 0))
11300 return NULL_TREE;
11302 if (i != nelts)
11303 return NULL_TREE;
11305 return first;
11308 return NULL_TREE;
11311 /* If the argument is INTEGER_CST, return it. If the argument is vector
11312 with all elements the same INTEGER_CST, return that INTEGER_CST. Otherwise
11313 return NULL_TREE.
11314 Look through location wrappers. */
11316 tree
11317 uniform_integer_cst_p (tree t)
11319 STRIP_ANY_LOCATION_WRAPPER (t);
11321 if (TREE_CODE (t) == INTEGER_CST)
11322 return t;
11324 if (VECTOR_TYPE_P (TREE_TYPE (t)))
11326 t = uniform_vector_p (t);
11327 if (t && TREE_CODE (t) == INTEGER_CST)
11328 return t;
11331 return NULL_TREE;
11334 /* If VECTOR_CST T has a single nonzero element, return the index of that
11335 element, otherwise return -1. */
11338 single_nonzero_element (const_tree t)
11340 unsigned HOST_WIDE_INT nelts;
11341 unsigned int repeat_nelts;
11342 if (VECTOR_CST_NELTS (t).is_constant (&nelts))
11343 repeat_nelts = nelts;
11344 else if (VECTOR_CST_NELTS_PER_PATTERN (t) == 2)
11346 nelts = vector_cst_encoded_nelts (t);
11347 repeat_nelts = VECTOR_CST_NPATTERNS (t);
11349 else
11350 return -1;
11352 int res = -1;
11353 for (unsigned int i = 0; i < nelts; ++i)
11355 tree elt = vector_cst_elt (t, i);
11356 if (!integer_zerop (elt) && !real_zerop (elt))
11358 if (res >= 0 || i >= repeat_nelts)
11359 return -1;
11360 res = i;
11363 return res;
11366 /* Build an empty statement at location LOC. */
11368 tree
11369 build_empty_stmt (location_t loc)
11371 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
11372 SET_EXPR_LOCATION (t, loc);
11373 return t;
11377 /* Build an OpenMP clause with code CODE. LOC is the location of the
11378 clause. */
11380 tree
11381 build_omp_clause (location_t loc, enum omp_clause_code code)
11383 tree t;
11384 int size, length;
11386 length = omp_clause_num_ops[code];
11387 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
11389 record_node_allocation_statistics (OMP_CLAUSE, size);
11391 t = (tree) ggc_internal_alloc (size);
11392 memset (t, 0, size);
11393 TREE_SET_CODE (t, OMP_CLAUSE);
11394 OMP_CLAUSE_SET_CODE (t, code);
11395 OMP_CLAUSE_LOCATION (t) = loc;
11397 return t;
11400 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
11401 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
11402 Except for the CODE and operand count field, other storage for the
11403 object is initialized to zeros. */
11405 tree
11406 build_vl_exp (enum tree_code code, int len MEM_STAT_DECL)
11408 tree t;
11409 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
11411 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
11412 gcc_assert (len >= 1);
11414 record_node_allocation_statistics (code, length);
11416 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
11418 TREE_SET_CODE (t, code);
11420 /* Can't use TREE_OPERAND to store the length because if checking is
11421 enabled, it will try to check the length before we store it. :-P */
11422 t->exp.operands[0] = build_int_cst (sizetype, len);
11424 return t;
11427 /* Helper function for build_call_* functions; build a CALL_EXPR with
11428 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
11429 the argument slots. */
11431 static tree
11432 build_call_1 (tree return_type, tree fn, int nargs)
11434 tree t;
11436 t = build_vl_exp (CALL_EXPR, nargs + 3);
11437 TREE_TYPE (t) = return_type;
11438 CALL_EXPR_FN (t) = fn;
11439 CALL_EXPR_STATIC_CHAIN (t) = NULL;
11441 return t;
11444 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
11445 FN and a null static chain slot. NARGS is the number of call arguments
11446 which are specified as "..." arguments. */
11448 tree
11449 build_call_nary (tree return_type, tree fn, int nargs, ...)
11451 tree ret;
11452 va_list args;
11453 va_start (args, nargs);
11454 ret = build_call_valist (return_type, fn, nargs, args);
11455 va_end (args);
11456 return ret;
11459 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
11460 FN and a null static chain slot. NARGS is the number of call arguments
11461 which are specified as a va_list ARGS. */
11463 tree
11464 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
11466 tree t;
11467 int i;
11469 t = build_call_1 (return_type, fn, nargs);
11470 for (i = 0; i < nargs; i++)
11471 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
11472 process_call_operands (t);
11473 return t;
11476 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
11477 FN and a null static chain slot. NARGS is the number of call arguments
11478 which are specified as a tree array ARGS. */
11480 tree
11481 build_call_array_loc (location_t loc, tree return_type, tree fn,
11482 int nargs, const tree *args)
11484 tree t;
11485 int i;
11487 t = build_call_1 (return_type, fn, nargs);
11488 for (i = 0; i < nargs; i++)
11489 CALL_EXPR_ARG (t, i) = args[i];
11490 process_call_operands (t);
11491 SET_EXPR_LOCATION (t, loc);
11492 return t;
11495 /* Like build_call_array, but takes a vec. */
11497 tree
11498 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
11500 tree ret, t;
11501 unsigned int ix;
11503 ret = build_call_1 (return_type, fn, vec_safe_length (args));
11504 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
11505 CALL_EXPR_ARG (ret, ix) = t;
11506 process_call_operands (ret);
11507 return ret;
11510 /* Conveniently construct a function call expression. FNDECL names the
11511 function to be called and N arguments are passed in the array
11512 ARGARRAY. */
11514 tree
11515 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
11517 tree fntype = TREE_TYPE (fndecl);
11518 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11520 return fold_build_call_array_loc (loc, TREE_TYPE (fntype), fn, n, argarray);
11523 /* Conveniently construct a function call expression. FNDECL names the
11524 function to be called and the arguments are passed in the vector
11525 VEC. */
11527 tree
11528 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
11530 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
11531 vec_safe_address (vec));
11535 /* Conveniently construct a function call expression. FNDECL names the
11536 function to be called, N is the number of arguments, and the "..."
11537 parameters are the argument expressions. */
11539 tree
11540 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
11542 va_list ap;
11543 tree *argarray = XALLOCAVEC (tree, n);
11544 int i;
11546 va_start (ap, n);
11547 for (i = 0; i < n; i++)
11548 argarray[i] = va_arg (ap, tree);
11549 va_end (ap);
11550 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11553 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11554 varargs macros aren't supported by all bootstrap compilers. */
11556 tree
11557 build_call_expr (tree fndecl, int n, ...)
11559 va_list ap;
11560 tree *argarray = XALLOCAVEC (tree, n);
11561 int i;
11563 va_start (ap, n);
11564 for (i = 0; i < n; i++)
11565 argarray[i] = va_arg (ap, tree);
11566 va_end (ap);
11567 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
11570 /* Build an internal call to IFN, with arguments ARGS[0:N-1] and with return
11571 type TYPE. This is just like CALL_EXPR, except its CALL_EXPR_FN is NULL.
11572 It will get gimplified later into an ordinary internal function. */
11574 tree
11575 build_call_expr_internal_loc_array (location_t loc, internal_fn ifn,
11576 tree type, int n, const tree *args)
11578 tree t = build_call_1 (type, NULL_TREE, n);
11579 for (int i = 0; i < n; ++i)
11580 CALL_EXPR_ARG (t, i) = args[i];
11581 SET_EXPR_LOCATION (t, loc);
11582 CALL_EXPR_IFN (t) = ifn;
11583 process_call_operands (t);
11584 return t;
11587 /* Build internal call expression. This is just like CALL_EXPR, except
11588 its CALL_EXPR_FN is NULL. It will get gimplified later into ordinary
11589 internal function. */
11591 tree
11592 build_call_expr_internal_loc (location_t loc, enum internal_fn ifn,
11593 tree type, int n, ...)
11595 va_list ap;
11596 tree *argarray = XALLOCAVEC (tree, n);
11597 int i;
11599 va_start (ap, n);
11600 for (i = 0; i < n; i++)
11601 argarray[i] = va_arg (ap, tree);
11602 va_end (ap);
11603 return build_call_expr_internal_loc_array (loc, ifn, type, n, argarray);
11606 /* Return a function call to FN, if the target is guaranteed to support it,
11607 or null otherwise.
11609 N is the number of arguments, passed in the "...", and TYPE is the
11610 type of the return value. */
11612 tree
11613 maybe_build_call_expr_loc (location_t loc, combined_fn fn, tree type,
11614 int n, ...)
11616 va_list ap;
11617 tree *argarray = XALLOCAVEC (tree, n);
11618 int i;
11620 va_start (ap, n);
11621 for (i = 0; i < n; i++)
11622 argarray[i] = va_arg (ap, tree);
11623 va_end (ap);
11624 if (internal_fn_p (fn))
11626 internal_fn ifn = as_internal_fn (fn);
11627 if (direct_internal_fn_p (ifn))
11629 tree_pair types = direct_internal_fn_types (ifn, type, argarray);
11630 if (!direct_internal_fn_supported_p (ifn, types,
11631 OPTIMIZE_FOR_BOTH))
11632 return NULL_TREE;
11634 return build_call_expr_internal_loc_array (loc, ifn, type, n, argarray);
11636 else
11638 tree fndecl = builtin_decl_implicit (as_builtin_fn (fn));
11639 if (!fndecl)
11640 return NULL_TREE;
11641 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11645 /* Return a function call to the appropriate builtin alloca variant.
11647 SIZE is the size to be allocated. ALIGN, if non-zero, is the requested
11648 alignment of the allocated area. MAX_SIZE, if non-negative, is an upper
11649 bound for SIZE in case it is not a fixed value. */
11651 tree
11652 build_alloca_call_expr (tree size, unsigned int align, HOST_WIDE_INT max_size)
11654 if (max_size >= 0)
11656 tree t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX);
11657 return
11658 build_call_expr (t, 3, size, size_int (align), size_int (max_size));
11660 else if (align > 0)
11662 tree t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
11663 return build_call_expr (t, 2, size, size_int (align));
11665 else
11667 tree t = builtin_decl_explicit (BUILT_IN_ALLOCA);
11668 return build_call_expr (t, 1, size);
11672 /* Create a new constant string literal of type ELTYPE[SIZE] (or LEN
11673 if SIZE == -1) and return a tree node representing char* pointer to
11674 it as an ADDR_EXPR (ARRAY_REF (ELTYPE, ...)). When STR is nonnull
11675 the STRING_CST value is the LEN bytes at STR (the representation
11676 of the string, which may be wide). Otherwise it's all zeros. */
11678 tree
11679 build_string_literal (unsigned len, const char *str /* = NULL */,
11680 tree eltype /* = char_type_node */,
11681 unsigned HOST_WIDE_INT size /* = -1 */)
11683 tree t = build_string (len, str);
11684 /* Set the maximum valid index based on the string length or SIZE. */
11685 unsigned HOST_WIDE_INT maxidx
11686 = (size == HOST_WIDE_INT_M1U ? len : size) - 1;
11688 tree index = build_index_type (size_int (maxidx));
11689 eltype = build_type_variant (eltype, 1, 0);
11690 tree type = build_array_type (eltype, index);
11691 TREE_TYPE (t) = type;
11692 TREE_CONSTANT (t) = 1;
11693 TREE_READONLY (t) = 1;
11694 TREE_STATIC (t) = 1;
11696 type = build_pointer_type (eltype);
11697 t = build1 (ADDR_EXPR, type,
11698 build4 (ARRAY_REF, eltype,
11699 t, integer_zero_node, NULL_TREE, NULL_TREE));
11700 return t;
11705 /* Return true if T (assumed to be a DECL) must be assigned a memory
11706 location. */
11708 bool
11709 needs_to_live_in_memory (const_tree t)
11711 return (TREE_ADDRESSABLE (t)
11712 || is_global_var (t)
11713 || (TREE_CODE (t) == RESULT_DECL
11714 && !DECL_BY_REFERENCE (t)
11715 && aggregate_value_p (t, current_function_decl)));
11718 /* Return value of a constant X and sign-extend it. */
11720 HOST_WIDE_INT
11721 int_cst_value (const_tree x)
11723 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
11724 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
11726 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
11727 gcc_assert (cst_and_fits_in_hwi (x));
11729 if (bits < HOST_BITS_PER_WIDE_INT)
11731 bool negative = ((val >> (bits - 1)) & 1) != 0;
11732 if (negative)
11733 val |= HOST_WIDE_INT_M1U << (bits - 1) << 1;
11734 else
11735 val &= ~(HOST_WIDE_INT_M1U << (bits - 1) << 1);
11738 return val;
11741 /* If TYPE is an integral or pointer type, return an integer type with
11742 the same precision which is unsigned iff UNSIGNEDP is true, or itself
11743 if TYPE is already an integer type of signedness UNSIGNEDP.
11744 If TYPE is a floating-point type, return an integer type with the same
11745 bitsize and with the signedness given by UNSIGNEDP; this is useful
11746 when doing bit-level operations on a floating-point value. */
11748 tree
11749 signed_or_unsigned_type_for (int unsignedp, tree type)
11751 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) == unsignedp)
11752 return type;
11754 if (TREE_CODE (type) == VECTOR_TYPE)
11756 tree inner = TREE_TYPE (type);
11757 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
11758 if (!inner2)
11759 return NULL_TREE;
11760 if (inner == inner2)
11761 return type;
11762 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
11765 if (TREE_CODE (type) == COMPLEX_TYPE)
11767 tree inner = TREE_TYPE (type);
11768 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
11769 if (!inner2)
11770 return NULL_TREE;
11771 if (inner == inner2)
11772 return type;
11773 return build_complex_type (inner2);
11776 unsigned int bits;
11777 if (INTEGRAL_TYPE_P (type)
11778 || POINTER_TYPE_P (type)
11779 || TREE_CODE (type) == OFFSET_TYPE)
11780 bits = TYPE_PRECISION (type);
11781 else if (TREE_CODE (type) == REAL_TYPE)
11782 bits = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (type));
11783 else
11784 return NULL_TREE;
11786 return build_nonstandard_integer_type (bits, unsignedp);
11789 /* If TYPE is an integral or pointer type, return an integer type with
11790 the same precision which is unsigned, or itself if TYPE is already an
11791 unsigned integer type. If TYPE is a floating-point type, return an
11792 unsigned integer type with the same bitsize as TYPE. */
11794 tree
11795 unsigned_type_for (tree type)
11797 return signed_or_unsigned_type_for (1, type);
11800 /* If TYPE is an integral or pointer type, return an integer type with
11801 the same precision which is signed, or itself if TYPE is already a
11802 signed integer type. If TYPE is a floating-point type, return a
11803 signed integer type with the same bitsize as TYPE. */
11805 tree
11806 signed_type_for (tree type)
11808 return signed_or_unsigned_type_for (0, type);
11811 /* If TYPE is a vector type, return a signed integer vector type with the
11812 same width and number of subparts. Otherwise return boolean_type_node. */
11814 tree
11815 truth_type_for (tree type)
11817 if (TREE_CODE (type) == VECTOR_TYPE)
11819 if (VECTOR_BOOLEAN_TYPE_P (type))
11820 return type;
11821 return build_truth_vector_type_for (type);
11823 else
11824 return boolean_type_node;
11827 /* Returns the largest value obtainable by casting something in INNER type to
11828 OUTER type. */
11830 tree
11831 upper_bound_in_type (tree outer, tree inner)
11833 unsigned int det = 0;
11834 unsigned oprec = TYPE_PRECISION (outer);
11835 unsigned iprec = TYPE_PRECISION (inner);
11836 unsigned prec;
11838 /* Compute a unique number for every combination. */
11839 det |= (oprec > iprec) ? 4 : 0;
11840 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
11841 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
11843 /* Determine the exponent to use. */
11844 switch (det)
11846 case 0:
11847 case 1:
11848 /* oprec <= iprec, outer: signed, inner: don't care. */
11849 prec = oprec - 1;
11850 break;
11851 case 2:
11852 case 3:
11853 /* oprec <= iprec, outer: unsigned, inner: don't care. */
11854 prec = oprec;
11855 break;
11856 case 4:
11857 /* oprec > iprec, outer: signed, inner: signed. */
11858 prec = iprec - 1;
11859 break;
11860 case 5:
11861 /* oprec > iprec, outer: signed, inner: unsigned. */
11862 prec = iprec;
11863 break;
11864 case 6:
11865 /* oprec > iprec, outer: unsigned, inner: signed. */
11866 prec = oprec;
11867 break;
11868 case 7:
11869 /* oprec > iprec, outer: unsigned, inner: unsigned. */
11870 prec = iprec;
11871 break;
11872 default:
11873 gcc_unreachable ();
11876 return wide_int_to_tree (outer,
11877 wi::mask (prec, false, TYPE_PRECISION (outer)));
11880 /* Returns the smallest value obtainable by casting something in INNER type to
11881 OUTER type. */
11883 tree
11884 lower_bound_in_type (tree outer, tree inner)
11886 unsigned oprec = TYPE_PRECISION (outer);
11887 unsigned iprec = TYPE_PRECISION (inner);
11889 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
11890 and obtain 0. */
11891 if (TYPE_UNSIGNED (outer)
11892 /* If we are widening something of an unsigned type, OUTER type
11893 contains all values of INNER type. In particular, both INNER
11894 and OUTER types have zero in common. */
11895 || (oprec > iprec && TYPE_UNSIGNED (inner)))
11896 return build_int_cst (outer, 0);
11897 else
11899 /* If we are widening a signed type to another signed type, we
11900 want to obtain -2^^(iprec-1). If we are keeping the
11901 precision or narrowing to a signed type, we want to obtain
11902 -2^(oprec-1). */
11903 unsigned prec = oprec > iprec ? iprec : oprec;
11904 return wide_int_to_tree (outer,
11905 wi::mask (prec - 1, true,
11906 TYPE_PRECISION (outer)));
11910 /* Return nonzero if two operands that are suitable for PHI nodes are
11911 necessarily equal. Specifically, both ARG0 and ARG1 must be either
11912 SSA_NAME or invariant. Note that this is strictly an optimization.
11913 That is, callers of this function can directly call operand_equal_p
11914 and get the same result, only slower. */
11917 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
11919 if (arg0 == arg1)
11920 return 1;
11921 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
11922 return 0;
11923 return operand_equal_p (arg0, arg1, 0);
11926 /* Returns number of zeros at the end of binary representation of X. */
11928 tree
11929 num_ending_zeros (const_tree x)
11931 return build_int_cst (TREE_TYPE (x), wi::ctz (wi::to_wide (x)));
11935 #define WALK_SUBTREE(NODE) \
11936 do \
11938 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
11939 if (result) \
11940 return result; \
11942 while (0)
11944 /* This is a subroutine of walk_tree that walks field of TYPE that are to
11945 be walked whenever a type is seen in the tree. Rest of operands and return
11946 value are as for walk_tree. */
11948 static tree
11949 walk_type_fields (tree type, walk_tree_fn func, void *data,
11950 hash_set<tree> *pset, walk_tree_lh lh)
11952 tree result = NULL_TREE;
11954 switch (TREE_CODE (type))
11956 case POINTER_TYPE:
11957 case REFERENCE_TYPE:
11958 case VECTOR_TYPE:
11959 /* We have to worry about mutually recursive pointers. These can't
11960 be written in C. They can in Ada. It's pathological, but
11961 there's an ACATS test (c38102a) that checks it. Deal with this
11962 by checking if we're pointing to another pointer, that one
11963 points to another pointer, that one does too, and we have no htab.
11964 If so, get a hash table. We check three levels deep to avoid
11965 the cost of the hash table if we don't need one. */
11966 if (POINTER_TYPE_P (TREE_TYPE (type))
11967 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
11968 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
11969 && !pset)
11971 result = walk_tree_without_duplicates (&TREE_TYPE (type),
11972 func, data);
11973 if (result)
11974 return result;
11976 break;
11979 /* fall through */
11981 case COMPLEX_TYPE:
11982 WALK_SUBTREE (TREE_TYPE (type));
11983 break;
11985 case METHOD_TYPE:
11986 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
11988 /* Fall through. */
11990 case FUNCTION_TYPE:
11991 WALK_SUBTREE (TREE_TYPE (type));
11993 tree arg;
11995 /* We never want to walk into default arguments. */
11996 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
11997 WALK_SUBTREE (TREE_VALUE (arg));
11999 break;
12001 case ARRAY_TYPE:
12002 /* Don't follow this nodes's type if a pointer for fear that
12003 we'll have infinite recursion. If we have a PSET, then we
12004 need not fear. */
12005 if (pset
12006 || (!POINTER_TYPE_P (TREE_TYPE (type))
12007 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
12008 WALK_SUBTREE (TREE_TYPE (type));
12009 WALK_SUBTREE (TYPE_DOMAIN (type));
12010 break;
12012 case OFFSET_TYPE:
12013 WALK_SUBTREE (TREE_TYPE (type));
12014 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
12015 break;
12017 default:
12018 break;
12021 return NULL_TREE;
12024 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
12025 called with the DATA and the address of each sub-tree. If FUNC returns a
12026 non-NULL value, the traversal is stopped, and the value returned by FUNC
12027 is returned. If PSET is non-NULL it is used to record the nodes visited,
12028 and to avoid visiting a node more than once. */
12030 tree
12031 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
12032 hash_set<tree> *pset, walk_tree_lh lh)
12034 enum tree_code code;
12035 int walk_subtrees;
12036 tree result;
12038 #define WALK_SUBTREE_TAIL(NODE) \
12039 do \
12041 tp = & (NODE); \
12042 goto tail_recurse; \
12044 while (0)
12046 tail_recurse:
12047 /* Skip empty subtrees. */
12048 if (!*tp)
12049 return NULL_TREE;
12051 /* Don't walk the same tree twice, if the user has requested
12052 that we avoid doing so. */
12053 if (pset && pset->add (*tp))
12054 return NULL_TREE;
12056 /* Call the function. */
12057 walk_subtrees = 1;
12058 result = (*func) (tp, &walk_subtrees, data);
12060 /* If we found something, return it. */
12061 if (result)
12062 return result;
12064 code = TREE_CODE (*tp);
12066 /* Even if we didn't, FUNC may have decided that there was nothing
12067 interesting below this point in the tree. */
12068 if (!walk_subtrees)
12070 /* But we still need to check our siblings. */
12071 if (code == TREE_LIST)
12072 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
12073 else if (code == OMP_CLAUSE)
12074 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
12075 else
12076 return NULL_TREE;
12079 if (lh)
12081 result = (*lh) (tp, &walk_subtrees, func, data, pset);
12082 if (result || !walk_subtrees)
12083 return result;
12086 switch (code)
12088 case ERROR_MARK:
12089 case IDENTIFIER_NODE:
12090 case INTEGER_CST:
12091 case REAL_CST:
12092 case FIXED_CST:
12093 case VECTOR_CST:
12094 case STRING_CST:
12095 case BLOCK:
12096 case PLACEHOLDER_EXPR:
12097 case SSA_NAME:
12098 case FIELD_DECL:
12099 case RESULT_DECL:
12100 /* None of these have subtrees other than those already walked
12101 above. */
12102 break;
12104 case TREE_LIST:
12105 WALK_SUBTREE (TREE_VALUE (*tp));
12106 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
12107 break;
12109 case TREE_VEC:
12111 int len = TREE_VEC_LENGTH (*tp);
12113 if (len == 0)
12114 break;
12116 /* Walk all elements but the first. */
12117 while (--len)
12118 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
12120 /* Now walk the first one as a tail call. */
12121 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
12124 case COMPLEX_CST:
12125 WALK_SUBTREE (TREE_REALPART (*tp));
12126 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
12128 case CONSTRUCTOR:
12130 unsigned HOST_WIDE_INT idx;
12131 constructor_elt *ce;
12133 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*tp), idx, &ce);
12134 idx++)
12135 WALK_SUBTREE (ce->value);
12137 break;
12139 case SAVE_EXPR:
12140 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
12142 case BIND_EXPR:
12144 tree decl;
12145 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
12147 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
12148 into declarations that are just mentioned, rather than
12149 declared; they don't really belong to this part of the tree.
12150 And, we can see cycles: the initializer for a declaration
12151 can refer to the declaration itself. */
12152 WALK_SUBTREE (DECL_INITIAL (decl));
12153 WALK_SUBTREE (DECL_SIZE (decl));
12154 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
12156 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
12159 case STATEMENT_LIST:
12161 tree_stmt_iterator i;
12162 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
12163 WALK_SUBTREE (*tsi_stmt_ptr (i));
12165 break;
12167 case OMP_CLAUSE:
12168 switch (OMP_CLAUSE_CODE (*tp))
12170 case OMP_CLAUSE_GANG:
12171 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
12172 /* FALLTHRU */
12174 case OMP_CLAUSE_ASYNC:
12175 case OMP_CLAUSE_WAIT:
12176 case OMP_CLAUSE_WORKER:
12177 case OMP_CLAUSE_VECTOR:
12178 case OMP_CLAUSE_NUM_GANGS:
12179 case OMP_CLAUSE_NUM_WORKERS:
12180 case OMP_CLAUSE_VECTOR_LENGTH:
12181 case OMP_CLAUSE_PRIVATE:
12182 case OMP_CLAUSE_SHARED:
12183 case OMP_CLAUSE_FIRSTPRIVATE:
12184 case OMP_CLAUSE_COPYIN:
12185 case OMP_CLAUSE_COPYPRIVATE:
12186 case OMP_CLAUSE_FINAL:
12187 case OMP_CLAUSE_IF:
12188 case OMP_CLAUSE_NUM_THREADS:
12189 case OMP_CLAUSE_SCHEDULE:
12190 case OMP_CLAUSE_UNIFORM:
12191 case OMP_CLAUSE_DEPEND:
12192 case OMP_CLAUSE_NONTEMPORAL:
12193 case OMP_CLAUSE_NUM_TEAMS:
12194 case OMP_CLAUSE_THREAD_LIMIT:
12195 case OMP_CLAUSE_DEVICE:
12196 case OMP_CLAUSE_DIST_SCHEDULE:
12197 case OMP_CLAUSE_SAFELEN:
12198 case OMP_CLAUSE_SIMDLEN:
12199 case OMP_CLAUSE_ORDERED:
12200 case OMP_CLAUSE_PRIORITY:
12201 case OMP_CLAUSE_GRAINSIZE:
12202 case OMP_CLAUSE_NUM_TASKS:
12203 case OMP_CLAUSE_HINT:
12204 case OMP_CLAUSE_TO_DECLARE:
12205 case OMP_CLAUSE_LINK:
12206 case OMP_CLAUSE_USE_DEVICE_PTR:
12207 case OMP_CLAUSE_USE_DEVICE_ADDR:
12208 case OMP_CLAUSE_IS_DEVICE_PTR:
12209 case OMP_CLAUSE_INCLUSIVE:
12210 case OMP_CLAUSE_EXCLUSIVE:
12211 case OMP_CLAUSE__LOOPTEMP_:
12212 case OMP_CLAUSE__REDUCTEMP_:
12213 case OMP_CLAUSE__CONDTEMP_:
12214 case OMP_CLAUSE__SCANTEMP_:
12215 case OMP_CLAUSE__SIMDUID_:
12216 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
12217 /* FALLTHRU */
12219 case OMP_CLAUSE_INDEPENDENT:
12220 case OMP_CLAUSE_NOWAIT:
12221 case OMP_CLAUSE_DEFAULT:
12222 case OMP_CLAUSE_UNTIED:
12223 case OMP_CLAUSE_MERGEABLE:
12224 case OMP_CLAUSE_PROC_BIND:
12225 case OMP_CLAUSE_DEVICE_TYPE:
12226 case OMP_CLAUSE_INBRANCH:
12227 case OMP_CLAUSE_NOTINBRANCH:
12228 case OMP_CLAUSE_FOR:
12229 case OMP_CLAUSE_PARALLEL:
12230 case OMP_CLAUSE_SECTIONS:
12231 case OMP_CLAUSE_TASKGROUP:
12232 case OMP_CLAUSE_NOGROUP:
12233 case OMP_CLAUSE_THREADS:
12234 case OMP_CLAUSE_SIMD:
12235 case OMP_CLAUSE_DEFAULTMAP:
12236 case OMP_CLAUSE_ORDER:
12237 case OMP_CLAUSE_BIND:
12238 case OMP_CLAUSE_AUTO:
12239 case OMP_CLAUSE_SEQ:
12240 case OMP_CLAUSE_TILE:
12241 case OMP_CLAUSE__SIMT_:
12242 case OMP_CLAUSE_IF_PRESENT:
12243 case OMP_CLAUSE_FINALIZE:
12244 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
12246 case OMP_CLAUSE_LASTPRIVATE:
12247 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
12248 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
12249 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
12251 case OMP_CLAUSE_COLLAPSE:
12253 int i;
12254 for (i = 0; i < 3; i++)
12255 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
12256 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
12259 case OMP_CLAUSE_LINEAR:
12260 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
12261 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STEP (*tp));
12262 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STMT (*tp));
12263 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
12265 case OMP_CLAUSE_ALIGNED:
12266 case OMP_CLAUSE_ALLOCATE:
12267 case OMP_CLAUSE_FROM:
12268 case OMP_CLAUSE_TO:
12269 case OMP_CLAUSE_MAP:
12270 case OMP_CLAUSE__CACHE_:
12271 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
12272 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
12273 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
12275 case OMP_CLAUSE_REDUCTION:
12276 case OMP_CLAUSE_TASK_REDUCTION:
12277 case OMP_CLAUSE_IN_REDUCTION:
12279 int i;
12280 for (i = 0; i < 5; i++)
12281 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
12282 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
12285 default:
12286 gcc_unreachable ();
12288 break;
12290 case TARGET_EXPR:
12292 int i, len;
12294 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
12295 But, we only want to walk once. */
12296 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
12297 for (i = 0; i < len; ++i)
12298 WALK_SUBTREE (TREE_OPERAND (*tp, i));
12299 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
12302 case DECL_EXPR:
12303 /* If this is a TYPE_DECL, walk into the fields of the type that it's
12304 defining. We only want to walk into these fields of a type in this
12305 case and not in the general case of a mere reference to the type.
12307 The criterion is as follows: if the field can be an expression, it
12308 must be walked only here. This should be in keeping with the fields
12309 that are directly gimplified in gimplify_type_sizes in order for the
12310 mark/copy-if-shared/unmark machinery of the gimplifier to work with
12311 variable-sized types.
12313 Note that DECLs get walked as part of processing the BIND_EXPR. */
12314 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
12316 /* Call the function for the decl so e.g. copy_tree_body_r can
12317 replace it with the remapped one. */
12318 result = (*func) (&DECL_EXPR_DECL (*tp), &walk_subtrees, data);
12319 if (result || !walk_subtrees)
12320 return result;
12322 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
12323 if (TREE_CODE (*type_p) == ERROR_MARK)
12324 return NULL_TREE;
12326 /* Call the function for the type. See if it returns anything or
12327 doesn't want us to continue. If we are to continue, walk both
12328 the normal fields and those for the declaration case. */
12329 result = (*func) (type_p, &walk_subtrees, data);
12330 if (result || !walk_subtrees)
12331 return result;
12333 /* But do not walk a pointed-to type since it may itself need to
12334 be walked in the declaration case if it isn't anonymous. */
12335 if (!POINTER_TYPE_P (*type_p))
12337 result = walk_type_fields (*type_p, func, data, pset, lh);
12338 if (result)
12339 return result;
12342 /* If this is a record type, also walk the fields. */
12343 if (RECORD_OR_UNION_TYPE_P (*type_p))
12345 tree field;
12347 for (field = TYPE_FIELDS (*type_p); field;
12348 field = DECL_CHAIN (field))
12350 /* We'd like to look at the type of the field, but we can
12351 easily get infinite recursion. So assume it's pointed
12352 to elsewhere in the tree. Also, ignore things that
12353 aren't fields. */
12354 if (TREE_CODE (field) != FIELD_DECL)
12355 continue;
12357 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
12358 WALK_SUBTREE (DECL_SIZE (field));
12359 WALK_SUBTREE (DECL_SIZE_UNIT (field));
12360 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
12361 WALK_SUBTREE (DECL_QUALIFIER (field));
12365 /* Same for scalar types. */
12366 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
12367 || TREE_CODE (*type_p) == ENUMERAL_TYPE
12368 || TREE_CODE (*type_p) == INTEGER_TYPE
12369 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
12370 || TREE_CODE (*type_p) == REAL_TYPE)
12372 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
12373 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
12376 WALK_SUBTREE (TYPE_SIZE (*type_p));
12377 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
12379 /* FALLTHRU */
12381 default:
12382 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
12384 int i, len;
12386 /* Walk over all the sub-trees of this operand. */
12387 len = TREE_OPERAND_LENGTH (*tp);
12389 /* Go through the subtrees. We need to do this in forward order so
12390 that the scope of a FOR_EXPR is handled properly. */
12391 if (len)
12393 for (i = 0; i < len - 1; ++i)
12394 WALK_SUBTREE (TREE_OPERAND (*tp, i));
12395 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
12398 /* If this is a type, walk the needed fields in the type. */
12399 else if (TYPE_P (*tp))
12400 return walk_type_fields (*tp, func, data, pset, lh);
12401 break;
12404 /* We didn't find what we were looking for. */
12405 return NULL_TREE;
12407 #undef WALK_SUBTREE_TAIL
12409 #undef WALK_SUBTREE
12411 /* Like walk_tree, but does not walk duplicate nodes more than once. */
12413 tree
12414 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
12415 walk_tree_lh lh)
12417 tree result;
12419 hash_set<tree> pset;
12420 result = walk_tree_1 (tp, func, data, &pset, lh);
12421 return result;
12425 tree
12426 tree_block (tree t)
12428 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
12430 if (IS_EXPR_CODE_CLASS (c))
12431 return LOCATION_BLOCK (t->exp.locus);
12432 gcc_unreachable ();
12433 return NULL;
12436 void
12437 tree_set_block (tree t, tree b)
12439 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
12441 if (IS_EXPR_CODE_CLASS (c))
12443 t->exp.locus = set_block (t->exp.locus, b);
12445 else
12446 gcc_unreachable ();
12449 /* Create a nameless artificial label and put it in the current
12450 function context. The label has a location of LOC. Returns the
12451 newly created label. */
12453 tree
12454 create_artificial_label (location_t loc)
12456 tree lab = build_decl (loc,
12457 LABEL_DECL, NULL_TREE, void_type_node);
12459 DECL_ARTIFICIAL (lab) = 1;
12460 DECL_IGNORED_P (lab) = 1;
12461 DECL_CONTEXT (lab) = current_function_decl;
12462 return lab;
12465 /* Given a tree, try to return a useful variable name that we can use
12466 to prefix a temporary that is being assigned the value of the tree.
12467 I.E. given <temp> = &A, return A. */
12469 const char *
12470 get_name (tree t)
12472 tree stripped_decl;
12474 stripped_decl = t;
12475 STRIP_NOPS (stripped_decl);
12476 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
12477 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
12478 else if (TREE_CODE (stripped_decl) == SSA_NAME)
12480 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
12481 if (!name)
12482 return NULL;
12483 return IDENTIFIER_POINTER (name);
12485 else
12487 switch (TREE_CODE (stripped_decl))
12489 case ADDR_EXPR:
12490 return get_name (TREE_OPERAND (stripped_decl, 0));
12491 default:
12492 return NULL;
12497 /* Return true if TYPE has a variable argument list. */
12499 bool
12500 stdarg_p (const_tree fntype)
12502 function_args_iterator args_iter;
12503 tree n = NULL_TREE, t;
12505 if (!fntype)
12506 return false;
12508 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
12510 n = t;
12513 return n != NULL_TREE && n != void_type_node;
12516 /* Return true if TYPE has a prototype. */
12518 bool
12519 prototype_p (const_tree fntype)
12521 tree t;
12523 gcc_assert (fntype != NULL_TREE);
12525 t = TYPE_ARG_TYPES (fntype);
12526 return (t != NULL_TREE);
12529 /* If BLOCK is inlined from an __attribute__((__artificial__))
12530 routine, return pointer to location from where it has been
12531 called. */
12532 location_t *
12533 block_nonartificial_location (tree block)
12535 location_t *ret = NULL;
12537 while (block && TREE_CODE (block) == BLOCK
12538 && BLOCK_ABSTRACT_ORIGIN (block))
12540 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
12541 if (TREE_CODE (ao) == FUNCTION_DECL)
12543 /* If AO is an artificial inline, point RET to the
12544 call site locus at which it has been inlined and continue
12545 the loop, in case AO's caller is also an artificial
12546 inline. */
12547 if (DECL_DECLARED_INLINE_P (ao)
12548 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
12549 ret = &BLOCK_SOURCE_LOCATION (block);
12550 else
12551 break;
12553 else if (TREE_CODE (ao) != BLOCK)
12554 break;
12556 block = BLOCK_SUPERCONTEXT (block);
12558 return ret;
12562 /* If EXP is inlined from an __attribute__((__artificial__))
12563 function, return the location of the original call expression. */
12565 location_t
12566 tree_nonartificial_location (tree exp)
12568 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
12570 if (loc)
12571 return *loc;
12572 else
12573 return EXPR_LOCATION (exp);
12577 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
12578 nodes. */
12580 /* Return the hash code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
12582 hashval_t
12583 cl_option_hasher::hash (tree x)
12585 const_tree const t = x;
12586 const char *p;
12587 size_t i;
12588 size_t len = 0;
12589 hashval_t hash = 0;
12591 if (TREE_CODE (t) == OPTIMIZATION_NODE)
12593 p = (const char *)TREE_OPTIMIZATION (t);
12594 len = sizeof (struct cl_optimization);
12597 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
12598 return cl_target_option_hash (TREE_TARGET_OPTION (t));
12600 else
12601 gcc_unreachable ();
12603 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
12604 something else. */
12605 for (i = 0; i < len; i++)
12606 if (p[i])
12607 hash = (hash << 4) ^ ((i << 2) | p[i]);
12609 return hash;
12612 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
12613 TARGET_OPTION tree node) is the same as that given by *Y, which is the
12614 same. */
12616 bool
12617 cl_option_hasher::equal (tree x, tree y)
12619 const_tree const xt = x;
12620 const_tree const yt = y;
12622 if (TREE_CODE (xt) != TREE_CODE (yt))
12623 return 0;
12625 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
12626 return cl_optimization_option_eq (TREE_OPTIMIZATION (xt),
12627 TREE_OPTIMIZATION (yt));
12628 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
12629 return cl_target_option_eq (TREE_TARGET_OPTION (xt),
12630 TREE_TARGET_OPTION (yt));
12631 else
12632 gcc_unreachable ();
12635 /* Build an OPTIMIZATION_NODE based on the options in OPTS and OPTS_SET. */
12637 tree
12638 build_optimization_node (struct gcc_options *opts,
12639 struct gcc_options *opts_set)
12641 tree t;
12643 /* Use the cache of optimization nodes. */
12645 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
12646 opts, opts_set);
12648 tree *slot = cl_option_hash_table->find_slot (cl_optimization_node, INSERT);
12649 t = *slot;
12650 if (!t)
12652 /* Insert this one into the hash table. */
12653 t = cl_optimization_node;
12654 *slot = t;
12656 /* Make a new node for next time round. */
12657 cl_optimization_node = make_node (OPTIMIZATION_NODE);
12660 return t;
12663 /* Build a TARGET_OPTION_NODE based on the options in OPTS and OPTS_SET. */
12665 tree
12666 build_target_option_node (struct gcc_options *opts,
12667 struct gcc_options *opts_set)
12669 tree t;
12671 /* Use the cache of optimization nodes. */
12673 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
12674 opts, opts_set);
12676 tree *slot = cl_option_hash_table->find_slot (cl_target_option_node, INSERT);
12677 t = *slot;
12678 if (!t)
12680 /* Insert this one into the hash table. */
12681 t = cl_target_option_node;
12682 *slot = t;
12684 /* Make a new node for next time round. */
12685 cl_target_option_node = make_node (TARGET_OPTION_NODE);
12688 return t;
12691 /* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
12692 so that they aren't saved during PCH writing. */
12694 void
12695 prepare_target_option_nodes_for_pch (void)
12697 hash_table<cl_option_hasher>::iterator iter = cl_option_hash_table->begin ();
12698 for (; iter != cl_option_hash_table->end (); ++iter)
12699 if (TREE_CODE (*iter) == TARGET_OPTION_NODE)
12700 TREE_TARGET_GLOBALS (*iter) = NULL;
12703 /* Determine the "ultimate origin" of a block. */
12705 tree
12706 block_ultimate_origin (const_tree block)
12708 tree origin = BLOCK_ABSTRACT_ORIGIN (block);
12710 if (origin == NULL_TREE)
12711 return NULL_TREE;
12712 else
12714 gcc_checking_assert ((DECL_P (origin)
12715 && DECL_ORIGIN (origin) == origin)
12716 || BLOCK_ORIGIN (origin) == origin);
12717 return origin;
12721 /* Return true iff conversion from INNER_TYPE to OUTER_TYPE generates
12722 no instruction. */
12724 bool
12725 tree_nop_conversion_p (const_tree outer_type, const_tree inner_type)
12727 /* Do not strip casts into or out of differing address spaces. */
12728 if (POINTER_TYPE_P (outer_type)
12729 && TYPE_ADDR_SPACE (TREE_TYPE (outer_type)) != ADDR_SPACE_GENERIC)
12731 if (!POINTER_TYPE_P (inner_type)
12732 || (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
12733 != TYPE_ADDR_SPACE (TREE_TYPE (inner_type))))
12734 return false;
12736 else if (POINTER_TYPE_P (inner_type)
12737 && TYPE_ADDR_SPACE (TREE_TYPE (inner_type)) != ADDR_SPACE_GENERIC)
12739 /* We already know that outer_type is not a pointer with
12740 a non-generic address space. */
12741 return false;
12744 /* Use precision rather then machine mode when we can, which gives
12745 the correct answer even for submode (bit-field) types. */
12746 if ((INTEGRAL_TYPE_P (outer_type)
12747 || POINTER_TYPE_P (outer_type)
12748 || TREE_CODE (outer_type) == OFFSET_TYPE)
12749 && (INTEGRAL_TYPE_P (inner_type)
12750 || POINTER_TYPE_P (inner_type)
12751 || TREE_CODE (inner_type) == OFFSET_TYPE))
12752 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
12754 /* Otherwise fall back on comparing machine modes (e.g. for
12755 aggregate types, floats). */
12756 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
12759 /* Return true iff conversion in EXP generates no instruction. Mark
12760 it inline so that we fully inline into the stripping functions even
12761 though we have two uses of this function. */
12763 static inline bool
12764 tree_nop_conversion (const_tree exp)
12766 tree outer_type, inner_type;
12768 if (location_wrapper_p (exp))
12769 return true;
12770 if (!CONVERT_EXPR_P (exp)
12771 && TREE_CODE (exp) != NON_LVALUE_EXPR)
12772 return false;
12774 outer_type = TREE_TYPE (exp);
12775 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
12776 if (!inner_type || inner_type == error_mark_node)
12777 return false;
12779 return tree_nop_conversion_p (outer_type, inner_type);
12782 /* Return true iff conversion in EXP generates no instruction. Don't
12783 consider conversions changing the signedness. */
12785 static bool
12786 tree_sign_nop_conversion (const_tree exp)
12788 tree outer_type, inner_type;
12790 if (!tree_nop_conversion (exp))
12791 return false;
12793 outer_type = TREE_TYPE (exp);
12794 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
12796 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
12797 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
12800 /* Strip conversions from EXP according to tree_nop_conversion and
12801 return the resulting expression. */
12803 tree
12804 tree_strip_nop_conversions (tree exp)
12806 while (tree_nop_conversion (exp))
12807 exp = TREE_OPERAND (exp, 0);
12808 return exp;
12811 /* Strip conversions from EXP according to tree_sign_nop_conversion
12812 and return the resulting expression. */
12814 tree
12815 tree_strip_sign_nop_conversions (tree exp)
12817 while (tree_sign_nop_conversion (exp))
12818 exp = TREE_OPERAND (exp, 0);
12819 return exp;
12822 /* Avoid any floating point extensions from EXP. */
12823 tree
12824 strip_float_extensions (tree exp)
12826 tree sub, expt, subt;
12828 /* For floating point constant look up the narrowest type that can hold
12829 it properly and handle it like (type)(narrowest_type)constant.
12830 This way we can optimize for instance a=a*2.0 where "a" is float
12831 but 2.0 is double constant. */
12832 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
12834 REAL_VALUE_TYPE orig;
12835 tree type = NULL;
12837 orig = TREE_REAL_CST (exp);
12838 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
12839 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
12840 type = float_type_node;
12841 else if (TYPE_PRECISION (TREE_TYPE (exp))
12842 > TYPE_PRECISION (double_type_node)
12843 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
12844 type = double_type_node;
12845 if (type)
12846 return build_real_truncate (type, orig);
12849 if (!CONVERT_EXPR_P (exp))
12850 return exp;
12852 sub = TREE_OPERAND (exp, 0);
12853 subt = TREE_TYPE (sub);
12854 expt = TREE_TYPE (exp);
12856 if (!FLOAT_TYPE_P (subt))
12857 return exp;
12859 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
12860 return exp;
12862 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
12863 return exp;
12865 return strip_float_extensions (sub);
12868 /* Strip out all handled components that produce invariant
12869 offsets. */
12871 const_tree
12872 strip_invariant_refs (const_tree op)
12874 while (handled_component_p (op))
12876 switch (TREE_CODE (op))
12878 case ARRAY_REF:
12879 case ARRAY_RANGE_REF:
12880 if (!is_gimple_constant (TREE_OPERAND (op, 1))
12881 || TREE_OPERAND (op, 2) != NULL_TREE
12882 || TREE_OPERAND (op, 3) != NULL_TREE)
12883 return NULL;
12884 break;
12886 case COMPONENT_REF:
12887 if (TREE_OPERAND (op, 2) != NULL_TREE)
12888 return NULL;
12889 break;
12891 default:;
12893 op = TREE_OPERAND (op, 0);
12896 return op;
12899 static GTY(()) tree gcc_eh_personality_decl;
12901 /* Return the GCC personality function decl. */
12903 tree
12904 lhd_gcc_personality (void)
12906 if (!gcc_eh_personality_decl)
12907 gcc_eh_personality_decl = build_personality_function ("gcc");
12908 return gcc_eh_personality_decl;
12911 /* TARGET is a call target of GIMPLE call statement
12912 (obtained by gimple_call_fn). Return true if it is
12913 OBJ_TYPE_REF representing an virtual call of C++ method.
12914 (As opposed to OBJ_TYPE_REF representing objc calls
12915 through a cast where middle-end devirtualization machinery
12916 can't apply.) FOR_DUMP_P is true when being called from
12917 the dump routines. */
12919 bool
12920 virtual_method_call_p (const_tree target, bool for_dump_p)
12922 if (TREE_CODE (target) != OBJ_TYPE_REF)
12923 return false;
12924 tree t = TREE_TYPE (target);
12925 gcc_checking_assert (TREE_CODE (t) == POINTER_TYPE);
12926 t = TREE_TYPE (t);
12927 if (TREE_CODE (t) == FUNCTION_TYPE)
12928 return false;
12929 gcc_checking_assert (TREE_CODE (t) == METHOD_TYPE);
12930 /* If we do not have BINFO associated, it means that type was built
12931 without devirtualization enabled. Do not consider this a virtual
12932 call. */
12933 if (!TYPE_BINFO (obj_type_ref_class (target, for_dump_p)))
12934 return false;
12935 return true;
12938 /* Lookup sub-BINFO of BINFO of TYPE at offset POS. */
12940 static tree
12941 lookup_binfo_at_offset (tree binfo, tree type, HOST_WIDE_INT pos)
12943 unsigned int i;
12944 tree base_binfo, b;
12946 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12947 if (pos == tree_to_shwi (BINFO_OFFSET (base_binfo))
12948 && types_same_for_odr (TREE_TYPE (base_binfo), type))
12949 return base_binfo;
12950 else if ((b = lookup_binfo_at_offset (base_binfo, type, pos)) != NULL)
12951 return b;
12952 return NULL;
12955 /* Try to find a base info of BINFO that would have its field decl at offset
12956 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
12957 found, return, otherwise return NULL_TREE. */
12959 tree
12960 get_binfo_at_offset (tree binfo, poly_int64 offset, tree expected_type)
12962 tree type = BINFO_TYPE (binfo);
12964 while (true)
12966 HOST_WIDE_INT pos, size;
12967 tree fld;
12968 int i;
12970 if (types_same_for_odr (type, expected_type))
12971 return binfo;
12972 if (maybe_lt (offset, 0))
12973 return NULL_TREE;
12975 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
12977 if (TREE_CODE (fld) != FIELD_DECL || !DECL_ARTIFICIAL (fld))
12978 continue;
12980 pos = int_bit_position (fld);
12981 size = tree_to_uhwi (DECL_SIZE (fld));
12982 if (known_in_range_p (offset, pos, size))
12983 break;
12985 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
12986 return NULL_TREE;
12988 /* Offset 0 indicates the primary base, whose vtable contents are
12989 represented in the binfo for the derived class. */
12990 else if (maybe_ne (offset, 0))
12992 tree found_binfo = NULL, base_binfo;
12993 /* Offsets in BINFO are in bytes relative to the whole structure
12994 while POS is in bits relative to the containing field. */
12995 int binfo_offset = (tree_to_shwi (BINFO_OFFSET (binfo)) + pos
12996 / BITS_PER_UNIT);
12998 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12999 if (tree_to_shwi (BINFO_OFFSET (base_binfo)) == binfo_offset
13000 && types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
13002 found_binfo = base_binfo;
13003 break;
13005 if (found_binfo)
13006 binfo = found_binfo;
13007 else
13008 binfo = lookup_binfo_at_offset (binfo, TREE_TYPE (fld),
13009 binfo_offset);
13012 type = TREE_TYPE (fld);
13013 offset -= pos;
13017 /* Returns true if X is a typedef decl. */
13019 bool
13020 is_typedef_decl (const_tree x)
13022 return (x && TREE_CODE (x) == TYPE_DECL
13023 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
13026 /* Returns true iff TYPE is a type variant created for a typedef. */
13028 bool
13029 typedef_variant_p (const_tree type)
13031 return is_typedef_decl (TYPE_NAME (type));
13034 /* PR 84195: Replace control characters in "unescaped" with their
13035 escaped equivalents. Allow newlines if -fmessage-length has
13036 been set to a non-zero value. This is done here, rather than
13037 where the attribute is recorded as the message length can
13038 change between these two locations. */
13040 void
13041 escaped_string::escape (const char *unescaped)
13043 char *escaped;
13044 size_t i, new_i, len;
13046 if (m_owned)
13047 free (m_str);
13049 m_str = const_cast<char *> (unescaped);
13050 m_owned = false;
13052 if (unescaped == NULL || *unescaped == 0)
13053 return;
13055 len = strlen (unescaped);
13056 escaped = NULL;
13057 new_i = 0;
13059 for (i = 0; i < len; i++)
13061 char c = unescaped[i];
13063 if (!ISCNTRL (c))
13065 if (escaped)
13066 escaped[new_i++] = c;
13067 continue;
13070 if (c != '\n' || !pp_is_wrapping_line (global_dc->printer))
13072 if (escaped == NULL)
13074 /* We only allocate space for a new string if we
13075 actually encounter a control character that
13076 needs replacing. */
13077 escaped = (char *) xmalloc (len * 2 + 1);
13078 strncpy (escaped, unescaped, i);
13079 new_i = i;
13082 escaped[new_i++] = '\\';
13084 switch (c)
13086 case '\a': escaped[new_i++] = 'a'; break;
13087 case '\b': escaped[new_i++] = 'b'; break;
13088 case '\f': escaped[new_i++] = 'f'; break;
13089 case '\n': escaped[new_i++] = 'n'; break;
13090 case '\r': escaped[new_i++] = 'r'; break;
13091 case '\t': escaped[new_i++] = 't'; break;
13092 case '\v': escaped[new_i++] = 'v'; break;
13093 default: escaped[new_i++] = '?'; break;
13096 else if (escaped)
13097 escaped[new_i++] = c;
13100 if (escaped)
13102 escaped[new_i] = 0;
13103 m_str = escaped;
13104 m_owned = true;
13108 /* Warn about a use of an identifier which was marked deprecated. Returns
13109 whether a warning was given. */
13111 bool
13112 warn_deprecated_use (tree node, tree attr)
13114 escaped_string msg;
13116 if (node == 0 || !warn_deprecated_decl)
13117 return false;
13119 if (!attr)
13121 if (DECL_P (node))
13122 attr = DECL_ATTRIBUTES (node);
13123 else if (TYPE_P (node))
13125 tree decl = TYPE_STUB_DECL (node);
13126 if (decl)
13127 attr = lookup_attribute ("deprecated",
13128 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
13132 if (attr)
13133 attr = lookup_attribute ("deprecated", attr);
13135 if (attr)
13136 msg.escape (TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
13138 bool w = false;
13139 if (DECL_P (node))
13141 auto_diagnostic_group d;
13142 if (msg)
13143 w = warning (OPT_Wdeprecated_declarations,
13144 "%qD is deprecated: %s", node, (const char *) msg);
13145 else
13146 w = warning (OPT_Wdeprecated_declarations,
13147 "%qD is deprecated", node);
13148 if (w)
13149 inform (DECL_SOURCE_LOCATION (node), "declared here");
13151 else if (TYPE_P (node))
13153 tree what = NULL_TREE;
13154 tree decl = TYPE_STUB_DECL (node);
13156 if (TYPE_NAME (node))
13158 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
13159 what = TYPE_NAME (node);
13160 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
13161 && DECL_NAME (TYPE_NAME (node)))
13162 what = DECL_NAME (TYPE_NAME (node));
13165 auto_diagnostic_group d;
13166 if (what)
13168 if (msg)
13169 w = warning (OPT_Wdeprecated_declarations,
13170 "%qE is deprecated: %s", what, (const char *) msg);
13171 else
13172 w = warning (OPT_Wdeprecated_declarations,
13173 "%qE is deprecated", what);
13175 else
13177 if (msg)
13178 w = warning (OPT_Wdeprecated_declarations,
13179 "type is deprecated: %s", (const char *) msg);
13180 else
13181 w = warning (OPT_Wdeprecated_declarations,
13182 "type is deprecated");
13185 if (w && decl)
13186 inform (DECL_SOURCE_LOCATION (decl), "declared here");
13189 return w;
13192 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
13193 somewhere in it. */
13195 bool
13196 contains_bitfld_component_ref_p (const_tree ref)
13198 while (handled_component_p (ref))
13200 if (TREE_CODE (ref) == COMPONENT_REF
13201 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
13202 return true;
13203 ref = TREE_OPERAND (ref, 0);
13206 return false;
13209 /* Try to determine whether a TRY_CATCH expression can fall through.
13210 This is a subroutine of block_may_fallthru. */
13212 static bool
13213 try_catch_may_fallthru (const_tree stmt)
13215 tree_stmt_iterator i;
13217 /* If the TRY block can fall through, the whole TRY_CATCH can
13218 fall through. */
13219 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
13220 return true;
13222 i = tsi_start (TREE_OPERAND (stmt, 1));
13223 switch (TREE_CODE (tsi_stmt (i)))
13225 case CATCH_EXPR:
13226 /* We expect to see a sequence of CATCH_EXPR trees, each with a
13227 catch expression and a body. The whole TRY_CATCH may fall
13228 through iff any of the catch bodies falls through. */
13229 for (; !tsi_end_p (i); tsi_next (&i))
13231 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
13232 return true;
13234 return false;
13236 case EH_FILTER_EXPR:
13237 /* The exception filter expression only matters if there is an
13238 exception. If the exception does not match EH_FILTER_TYPES,
13239 we will execute EH_FILTER_FAILURE, and we will fall through
13240 if that falls through. If the exception does match
13241 EH_FILTER_TYPES, the stack unwinder will continue up the
13242 stack, so we will not fall through. We don't know whether we
13243 will throw an exception which matches EH_FILTER_TYPES or not,
13244 so we just ignore EH_FILTER_TYPES and assume that we might
13245 throw an exception which doesn't match. */
13246 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
13248 default:
13249 /* This case represents statements to be executed when an
13250 exception occurs. Those statements are implicitly followed
13251 by a RESX statement to resume execution after the exception.
13252 So in this case the TRY_CATCH never falls through. */
13253 return false;
13257 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
13258 need not be 100% accurate; simply be conservative and return true if we
13259 don't know. This is used only to avoid stupidly generating extra code.
13260 If we're wrong, we'll just delete the extra code later. */
13262 bool
13263 block_may_fallthru (const_tree block)
13265 /* This CONST_CAST is okay because expr_last returns its argument
13266 unmodified and we assign it to a const_tree. */
13267 const_tree stmt = expr_last (CONST_CAST_TREE (block));
13269 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
13271 case GOTO_EXPR:
13272 case RETURN_EXPR:
13273 /* Easy cases. If the last statement of the block implies
13274 control transfer, then we can't fall through. */
13275 return false;
13277 case SWITCH_EXPR:
13278 /* If there is a default: label or case labels cover all possible
13279 SWITCH_COND values, then the SWITCH_EXPR will transfer control
13280 to some case label in all cases and all we care is whether the
13281 SWITCH_BODY falls through. */
13282 if (SWITCH_ALL_CASES_P (stmt))
13283 return block_may_fallthru (SWITCH_BODY (stmt));
13284 return true;
13286 case COND_EXPR:
13287 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
13288 return true;
13289 return block_may_fallthru (COND_EXPR_ELSE (stmt));
13291 case BIND_EXPR:
13292 return block_may_fallthru (BIND_EXPR_BODY (stmt));
13294 case TRY_CATCH_EXPR:
13295 return try_catch_may_fallthru (stmt);
13297 case TRY_FINALLY_EXPR:
13298 /* The finally clause is always executed after the try clause,
13299 so if it does not fall through, then the try-finally will not
13300 fall through. Otherwise, if the try clause does not fall
13301 through, then when the finally clause falls through it will
13302 resume execution wherever the try clause was going. So the
13303 whole try-finally will only fall through if both the try
13304 clause and the finally clause fall through. */
13305 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
13306 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
13308 case EH_ELSE_EXPR:
13309 return block_may_fallthru (TREE_OPERAND (stmt, 0));
13311 case MODIFY_EXPR:
13312 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
13313 stmt = TREE_OPERAND (stmt, 1);
13314 else
13315 return true;
13316 /* FALLTHRU */
13318 case CALL_EXPR:
13319 /* Functions that do not return do not fall through. */
13320 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
13322 case CLEANUP_POINT_EXPR:
13323 return block_may_fallthru (TREE_OPERAND (stmt, 0));
13325 case TARGET_EXPR:
13326 return block_may_fallthru (TREE_OPERAND (stmt, 1));
13328 case ERROR_MARK:
13329 return true;
13331 default:
13332 return lang_hooks.block_may_fallthru (stmt);
13336 /* True if we are using EH to handle cleanups. */
13337 static bool using_eh_for_cleanups_flag = false;
13339 /* This routine is called from front ends to indicate eh should be used for
13340 cleanups. */
13341 void
13342 using_eh_for_cleanups (void)
13344 using_eh_for_cleanups_flag = true;
13347 /* Query whether EH is used for cleanups. */
13348 bool
13349 using_eh_for_cleanups_p (void)
13351 return using_eh_for_cleanups_flag;
13354 /* Wrapper for tree_code_name to ensure that tree code is valid */
13355 const char *
13356 get_tree_code_name (enum tree_code code)
13358 const char *invalid = "<invalid tree code>";
13360 /* The tree_code enum promotes to signed, but we could be getting
13361 invalid values, so force an unsigned comparison. */
13362 if (unsigned (code) >= MAX_TREE_CODES)
13364 if (code == 0xa5a5)
13365 return "ggc_freed";
13366 return invalid;
13369 return tree_code_name[code];
13372 /* Drops the TREE_OVERFLOW flag from T. */
13374 tree
13375 drop_tree_overflow (tree t)
13377 gcc_checking_assert (TREE_OVERFLOW (t));
13379 /* For tree codes with a sharing machinery re-build the result. */
13380 if (poly_int_tree_p (t))
13381 return wide_int_to_tree (TREE_TYPE (t), wi::to_poly_wide (t));
13383 /* For VECTOR_CST, remove the overflow bits from the encoded elements
13384 and canonicalize the result. */
13385 if (TREE_CODE (t) == VECTOR_CST)
13387 tree_vector_builder builder;
13388 builder.new_unary_operation (TREE_TYPE (t), t, true);
13389 unsigned int count = builder.encoded_nelts ();
13390 for (unsigned int i = 0; i < count; ++i)
13392 tree elt = VECTOR_CST_ELT (t, i);
13393 if (TREE_OVERFLOW (elt))
13394 elt = drop_tree_overflow (elt);
13395 builder.quick_push (elt);
13397 return builder.build ();
13400 /* Otherwise, as all tcc_constants are possibly shared, copy the node
13401 and drop the flag. */
13402 t = copy_node (t);
13403 TREE_OVERFLOW (t) = 0;
13405 /* For constants that contain nested constants, drop the flag
13406 from those as well. */
13407 if (TREE_CODE (t) == COMPLEX_CST)
13409 if (TREE_OVERFLOW (TREE_REALPART (t)))
13410 TREE_REALPART (t) = drop_tree_overflow (TREE_REALPART (t));
13411 if (TREE_OVERFLOW (TREE_IMAGPART (t)))
13412 TREE_IMAGPART (t) = drop_tree_overflow (TREE_IMAGPART (t));
13415 return t;
13418 /* Given a memory reference expression T, return its base address.
13419 The base address of a memory reference expression is the main
13420 object being referenced. For instance, the base address for
13421 'array[i].fld[j]' is 'array'. You can think of this as stripping
13422 away the offset part from a memory address.
13424 This function calls handled_component_p to strip away all the inner
13425 parts of the memory reference until it reaches the base object. */
13427 tree
13428 get_base_address (tree t)
13430 while (handled_component_p (t))
13431 t = TREE_OPERAND (t, 0);
13433 if ((TREE_CODE (t) == MEM_REF
13434 || TREE_CODE (t) == TARGET_MEM_REF)
13435 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
13436 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
13438 /* ??? Either the alias oracle or all callers need to properly deal
13439 with WITH_SIZE_EXPRs before we can look through those. */
13440 if (TREE_CODE (t) == WITH_SIZE_EXPR)
13441 return NULL_TREE;
13443 return t;
13446 /* Return a tree of sizetype representing the size, in bytes, of the element
13447 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
13449 tree
13450 array_ref_element_size (tree exp)
13452 tree aligned_size = TREE_OPERAND (exp, 3);
13453 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
13454 location_t loc = EXPR_LOCATION (exp);
13456 /* If a size was specified in the ARRAY_REF, it's the size measured
13457 in alignment units of the element type. So multiply by that value. */
13458 if (aligned_size)
13460 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
13461 sizetype from another type of the same width and signedness. */
13462 if (TREE_TYPE (aligned_size) != sizetype)
13463 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
13464 return size_binop_loc (loc, MULT_EXPR, aligned_size,
13465 size_int (TYPE_ALIGN_UNIT (elmt_type)));
13468 /* Otherwise, take the size from that of the element type. Substitute
13469 any PLACEHOLDER_EXPR that we have. */
13470 else
13471 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
13474 /* Return a tree representing the lower bound of the array mentioned in
13475 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
13477 tree
13478 array_ref_low_bound (tree exp)
13480 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
13482 /* If a lower bound is specified in EXP, use it. */
13483 if (TREE_OPERAND (exp, 2))
13484 return TREE_OPERAND (exp, 2);
13486 /* Otherwise, if there is a domain type and it has a lower bound, use it,
13487 substituting for a PLACEHOLDER_EXPR as needed. */
13488 if (domain_type && TYPE_MIN_VALUE (domain_type))
13489 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
13491 /* Otherwise, return a zero of the appropriate type. */
13492 tree idxtype = TREE_TYPE (TREE_OPERAND (exp, 1));
13493 return (idxtype == error_mark_node
13494 ? integer_zero_node : build_int_cst (idxtype, 0));
13497 /* Return a tree representing the upper bound of the array mentioned in
13498 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
13500 tree
13501 array_ref_up_bound (tree exp)
13503 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
13505 /* If there is a domain type and it has an upper bound, use it, substituting
13506 for a PLACEHOLDER_EXPR as needed. */
13507 if (domain_type && TYPE_MAX_VALUE (domain_type))
13508 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
13510 /* Otherwise fail. */
13511 return NULL_TREE;
13514 /* Returns true if REF is an array reference, component reference,
13515 or memory reference to an array at the end of a structure.
13516 If this is the case, the array may be allocated larger
13517 than its upper bound implies. */
13519 bool
13520 array_at_struct_end_p (tree ref)
13522 tree atype;
13524 if (TREE_CODE (ref) == ARRAY_REF
13525 || TREE_CODE (ref) == ARRAY_RANGE_REF)
13527 atype = TREE_TYPE (TREE_OPERAND (ref, 0));
13528 ref = TREE_OPERAND (ref, 0);
13530 else if (TREE_CODE (ref) == COMPONENT_REF
13531 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 1))) == ARRAY_TYPE)
13532 atype = TREE_TYPE (TREE_OPERAND (ref, 1));
13533 else if (TREE_CODE (ref) == MEM_REF)
13535 tree arg = TREE_OPERAND (ref, 0);
13536 if (TREE_CODE (arg) == ADDR_EXPR)
13537 arg = TREE_OPERAND (arg, 0);
13538 tree argtype = TREE_TYPE (arg);
13539 if (TREE_CODE (argtype) == RECORD_TYPE)
13541 if (tree fld = last_field (argtype))
13543 atype = TREE_TYPE (fld);
13544 if (TREE_CODE (atype) != ARRAY_TYPE)
13545 return false;
13546 if (VAR_P (arg) && DECL_SIZE (fld))
13547 return false;
13549 else
13550 return false;
13552 else
13553 return false;
13555 else
13556 return false;
13558 if (TREE_CODE (ref) == STRING_CST)
13559 return false;
13561 tree ref_to_array = ref;
13562 while (handled_component_p (ref))
13564 /* If the reference chain contains a component reference to a
13565 non-union type and there follows another field the reference
13566 is not at the end of a structure. */
13567 if (TREE_CODE (ref) == COMPONENT_REF)
13569 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
13571 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
13572 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
13573 nextf = DECL_CHAIN (nextf);
13574 if (nextf)
13575 return false;
13578 /* If we have a multi-dimensional array we do not consider
13579 a non-innermost dimension as flex array if the whole
13580 multi-dimensional array is at struct end.
13581 Same for an array of aggregates with a trailing array
13582 member. */
13583 else if (TREE_CODE (ref) == ARRAY_REF)
13584 return false;
13585 else if (TREE_CODE (ref) == ARRAY_RANGE_REF)
13587 /* If we view an underlying object as sth else then what we
13588 gathered up to now is what we have to rely on. */
13589 else if (TREE_CODE (ref) == VIEW_CONVERT_EXPR)
13590 break;
13591 else
13592 gcc_unreachable ();
13594 ref = TREE_OPERAND (ref, 0);
13597 /* The array now is at struct end. Treat flexible arrays as
13598 always subject to extend, even into just padding constrained by
13599 an underlying decl. */
13600 if (! TYPE_SIZE (atype)
13601 || ! TYPE_DOMAIN (atype)
13602 || ! TYPE_MAX_VALUE (TYPE_DOMAIN (atype)))
13603 return true;
13605 if (TREE_CODE (ref) == MEM_REF
13606 && TREE_CODE (TREE_OPERAND (ref, 0)) == ADDR_EXPR)
13607 ref = TREE_OPERAND (TREE_OPERAND (ref, 0), 0);
13609 /* If the reference is based on a declared entity, the size of the array
13610 is constrained by its given domain. (Do not trust commons PR/69368). */
13611 if (DECL_P (ref)
13612 && !(flag_unconstrained_commons
13613 && VAR_P (ref) && DECL_COMMON (ref))
13614 && DECL_SIZE_UNIT (ref)
13615 && TREE_CODE (DECL_SIZE_UNIT (ref)) == INTEGER_CST)
13617 /* Check whether the array domain covers all of the available
13618 padding. */
13619 poly_int64 offset;
13620 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (atype))) != INTEGER_CST
13621 || TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))) != INTEGER_CST
13622 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (atype))) != INTEGER_CST)
13623 return true;
13624 if (! get_addr_base_and_unit_offset (ref_to_array, &offset))
13625 return true;
13627 /* If at least one extra element fits it is a flexarray. */
13628 if (known_le ((wi::to_offset (TYPE_MAX_VALUE (TYPE_DOMAIN (atype)))
13629 - wi::to_offset (TYPE_MIN_VALUE (TYPE_DOMAIN (atype)))
13630 + 2)
13631 * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (atype))),
13632 wi::to_offset (DECL_SIZE_UNIT (ref)) - offset))
13633 return true;
13635 return false;
13638 return true;
13641 /* Return a tree representing the offset, in bytes, of the field referenced
13642 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
13644 tree
13645 component_ref_field_offset (tree exp)
13647 tree aligned_offset = TREE_OPERAND (exp, 2);
13648 tree field = TREE_OPERAND (exp, 1);
13649 location_t loc = EXPR_LOCATION (exp);
13651 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
13652 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
13653 value. */
13654 if (aligned_offset)
13656 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
13657 sizetype from another type of the same width and signedness. */
13658 if (TREE_TYPE (aligned_offset) != sizetype)
13659 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
13660 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
13661 size_int (DECL_OFFSET_ALIGN (field)
13662 / BITS_PER_UNIT));
13665 /* Otherwise, take the offset from that of the field. Substitute
13666 any PLACEHOLDER_EXPR that we have. */
13667 else
13668 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
13671 /* Given the initializer INIT, return the initializer for the field
13672 DECL if it exists, otherwise null. Used to obtain the initializer
13673 for a flexible array member and determine its size. */
13675 static tree
13676 get_initializer_for (tree init, tree decl)
13678 STRIP_NOPS (init);
13680 tree fld, fld_init;
13681 unsigned HOST_WIDE_INT i;
13682 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), i, fld, fld_init)
13684 if (decl == fld)
13685 return fld_init;
13687 if (TREE_CODE (fld) == CONSTRUCTOR)
13689 fld_init = get_initializer_for (fld_init, decl);
13690 if (fld_init)
13691 return fld_init;
13695 return NULL_TREE;
13698 /* Determines the size of the member referenced by the COMPONENT_REF
13699 REF, using its initializer expression if necessary in order to
13700 determine the size of an initialized flexible array member.
13701 If non-null, set *ARK when REF refers to an interior zero-length
13702 array or a trailing one-element array.
13703 Returns the size as sizetype (which might be zero for an object
13704 with an uninitialized flexible array member) or null if the size
13705 cannot be determined. */
13707 tree
13708 component_ref_size (tree ref, special_array_member *sam /* = NULL */)
13710 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
13712 special_array_member arkbuf;
13713 if (!sam)
13714 sam = &arkbuf;
13715 *sam = special_array_member::none;
13717 /* The object/argument referenced by the COMPONENT_REF and its type. */
13718 tree arg = TREE_OPERAND (ref, 0);
13719 tree argtype = TREE_TYPE (arg);
13720 /* The referenced member. */
13721 tree member = TREE_OPERAND (ref, 1);
13723 tree memsize = DECL_SIZE_UNIT (member);
13724 if (memsize)
13726 tree memtype = TREE_TYPE (member);
13727 if (TREE_CODE (memtype) != ARRAY_TYPE)
13728 return memsize;
13730 bool trailing = array_at_struct_end_p (ref);
13731 bool zero_length = integer_zerop (memsize);
13732 if (!trailing && !zero_length)
13733 /* MEMBER is either an interior array or is an array with
13734 more than one element. */
13735 return memsize;
13737 if (zero_length)
13739 if (trailing)
13740 *sam = special_array_member::trail_0;
13741 else
13743 *sam = special_array_member::int_0;
13744 memsize = NULL_TREE;
13748 if (!zero_length)
13749 if (tree dom = TYPE_DOMAIN (memtype))
13750 if (tree min = TYPE_MIN_VALUE (dom))
13751 if (tree max = TYPE_MAX_VALUE (dom))
13752 if (TREE_CODE (min) == INTEGER_CST
13753 && TREE_CODE (max) == INTEGER_CST)
13755 offset_int minidx = wi::to_offset (min);
13756 offset_int maxidx = wi::to_offset (max);
13757 offset_int neltsm1 = maxidx - minidx;
13758 if (neltsm1 > 0)
13759 /* MEMBER is an array with more than one element. */
13760 return memsize;
13762 if (neltsm1 == 0)
13763 *sam = special_array_member::trail_1;
13766 /* For a refernce to a zero- or one-element array member of a union
13767 use the size of the union instead of the size of the member. */
13768 if (TREE_CODE (argtype) == UNION_TYPE)
13769 memsize = TYPE_SIZE_UNIT (argtype);
13772 /* MEMBER is either a bona fide flexible array member, or a zero-length
13773 array member, or an array of length one treated as such. */
13775 /* If the reference is to a declared object and the member a true
13776 flexible array, try to determine its size from its initializer. */
13777 poly_int64 baseoff = 0;
13778 tree base = get_addr_base_and_unit_offset (ref, &baseoff);
13779 if (!base || !VAR_P (base))
13781 if (*sam != special_array_member::int_0)
13782 return NULL_TREE;
13784 if (TREE_CODE (arg) != COMPONENT_REF)
13785 return NULL_TREE;
13787 base = arg;
13788 while (TREE_CODE (base) == COMPONENT_REF)
13789 base = TREE_OPERAND (base, 0);
13790 baseoff = tree_to_poly_int64 (byte_position (TREE_OPERAND (ref, 1)));
13793 /* BASE is the declared object of which MEMBER is either a member
13794 or that is cast to ARGTYPE (e.g., a char buffer used to store
13795 an ARGTYPE object). */
13796 tree basetype = TREE_TYPE (base);
13798 /* Determine the base type of the referenced object. If it's
13799 the same as ARGTYPE and MEMBER has a known size, return it. */
13800 tree bt = basetype;
13801 if (*sam != special_array_member::int_0)
13802 while (TREE_CODE (bt) == ARRAY_TYPE)
13803 bt = TREE_TYPE (bt);
13804 bool typematch = useless_type_conversion_p (argtype, bt);
13805 if (memsize && typematch)
13806 return memsize;
13808 memsize = NULL_TREE;
13810 if (typematch)
13811 /* MEMBER is a true flexible array member. Compute its size from
13812 the initializer of the BASE object if it has one. */
13813 if (tree init = DECL_P (base) ? DECL_INITIAL (base) : NULL_TREE)
13814 if (init != error_mark_node)
13816 init = get_initializer_for (init, member);
13817 if (init)
13819 memsize = TYPE_SIZE_UNIT (TREE_TYPE (init));
13820 if (tree refsize = TYPE_SIZE_UNIT (argtype))
13822 /* Use the larger of the initializer size and the tail
13823 padding in the enclosing struct. */
13824 poly_int64 rsz = tree_to_poly_int64 (refsize);
13825 rsz -= baseoff;
13826 if (known_lt (tree_to_poly_int64 (memsize), rsz))
13827 memsize = wide_int_to_tree (TREE_TYPE (memsize), rsz);
13830 baseoff = 0;
13834 if (!memsize)
13836 if (typematch)
13838 if (DECL_P (base)
13839 && DECL_EXTERNAL (base)
13840 && bt == basetype
13841 && *sam != special_array_member::int_0)
13842 /* The size of a flexible array member of an extern struct
13843 with no initializer cannot be determined (it's defined
13844 in another translation unit and can have an initializer
13845 with an arbitrary number of elements). */
13846 return NULL_TREE;
13848 /* Use the size of the base struct or, for interior zero-length
13849 arrays, the size of the enclosing type. */
13850 memsize = TYPE_SIZE_UNIT (bt);
13852 else if (DECL_P (base))
13853 /* Use the size of the BASE object (possibly an array of some
13854 other type such as char used to store the struct). */
13855 memsize = DECL_SIZE_UNIT (base);
13856 else
13857 return NULL_TREE;
13860 /* If the flexible array member has a known size use the greater
13861 of it and the tail padding in the enclosing struct.
13862 Otherwise, when the size of the flexible array member is unknown
13863 and the referenced object is not a struct, use the size of its
13864 type when known. This detects sizes of array buffers when cast
13865 to struct types with flexible array members. */
13866 if (memsize)
13868 poly_int64 memsz64 = memsize ? tree_to_poly_int64 (memsize) : 0;
13869 if (known_lt (baseoff, memsz64))
13871 memsz64 -= baseoff;
13872 return wide_int_to_tree (TREE_TYPE (memsize), memsz64);
13874 return size_zero_node;
13877 /* Return "don't know" for an external non-array object since its
13878 flexible array member can be initialized to have any number of
13879 elements. Otherwise, return zero because the flexible array
13880 member has no elements. */
13881 return (DECL_P (base)
13882 && DECL_EXTERNAL (base)
13883 && (!typematch
13884 || TREE_CODE (basetype) != ARRAY_TYPE)
13885 ? NULL_TREE : size_zero_node);
13888 /* Return the machine mode of T. For vectors, returns the mode of the
13889 inner type. The main use case is to feed the result to HONOR_NANS,
13890 avoiding the BLKmode that a direct TYPE_MODE (T) might return. */
13892 machine_mode
13893 element_mode (const_tree t)
13895 if (!TYPE_P (t))
13896 t = TREE_TYPE (t);
13897 if (VECTOR_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
13898 t = TREE_TYPE (t);
13899 return TYPE_MODE (t);
13902 /* Vector types need to re-check the target flags each time we report
13903 the machine mode. We need to do this because attribute target can
13904 change the result of vector_mode_supported_p and have_regs_of_mode
13905 on a per-function basis. Thus the TYPE_MODE of a VECTOR_TYPE can
13906 change on a per-function basis. */
13907 /* ??? Possibly a better solution is to run through all the types
13908 referenced by a function and re-compute the TYPE_MODE once, rather
13909 than make the TYPE_MODE macro call a function. */
13911 machine_mode
13912 vector_type_mode (const_tree t)
13914 machine_mode mode;
13916 gcc_assert (TREE_CODE (t) == VECTOR_TYPE);
13918 mode = t->type_common.mode;
13919 if (VECTOR_MODE_P (mode)
13920 && (!targetm.vector_mode_supported_p (mode)
13921 || !have_regs_of_mode[mode]))
13923 scalar_int_mode innermode;
13925 /* For integers, try mapping it to a same-sized scalar mode. */
13926 if (is_int_mode (TREE_TYPE (t)->type_common.mode, &innermode))
13928 poly_int64 size = (TYPE_VECTOR_SUBPARTS (t)
13929 * GET_MODE_BITSIZE (innermode));
13930 scalar_int_mode mode;
13931 if (int_mode_for_size (size, 0).exists (&mode)
13932 && have_regs_of_mode[mode])
13933 return mode;
13936 return BLKmode;
13939 return mode;
13942 /* Return the size in bits of each element of vector type TYPE. */
13944 unsigned int
13945 vector_element_bits (const_tree type)
13947 gcc_checking_assert (VECTOR_TYPE_P (type));
13948 if (VECTOR_BOOLEAN_TYPE_P (type))
13949 return vector_element_size (tree_to_poly_uint64 (TYPE_SIZE (type)),
13950 TYPE_VECTOR_SUBPARTS (type));
13951 return tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)));
13954 /* Calculate the size in bits of each element of vector type TYPE
13955 and return the result as a tree of type bitsizetype. */
13957 tree
13958 vector_element_bits_tree (const_tree type)
13960 gcc_checking_assert (VECTOR_TYPE_P (type));
13961 if (VECTOR_BOOLEAN_TYPE_P (type))
13962 return bitsize_int (vector_element_bits (type));
13963 return TYPE_SIZE (TREE_TYPE (type));
13966 /* Verify that basic properties of T match TV and thus T can be a variant of
13967 TV. TV should be the more specified variant (i.e. the main variant). */
13969 static bool
13970 verify_type_variant (const_tree t, tree tv)
13972 /* Type variant can differ by:
13974 - TYPE_QUALS: TYPE_READONLY, TYPE_VOLATILE, TYPE_ATOMIC, TYPE_RESTRICT,
13975 ENCODE_QUAL_ADDR_SPACE.
13976 - main variant may be TYPE_COMPLETE_P and variant types !TYPE_COMPLETE_P
13977 in this case some values may not be set in the variant types
13978 (see TYPE_COMPLETE_P checks).
13979 - it is possible to have TYPE_ARTIFICIAL variant of non-artifical type
13980 - by TYPE_NAME and attributes (i.e. when variant originate by typedef)
13981 - TYPE_CANONICAL (TYPE_ALIAS_SET is the same among variants)
13982 - by the alignment: TYPE_ALIGN and TYPE_USER_ALIGN
13983 - during LTO by TYPE_CONTEXT if type is TYPE_FILE_SCOPE_P
13984 this is necessary to make it possible to merge types form different TUs
13985 - arrays, pointers and references may have TREE_TYPE that is a variant
13986 of TREE_TYPE of their main variants.
13987 - aggregates may have new TYPE_FIELDS list that list variants of
13988 the main variant TYPE_FIELDS.
13989 - vector types may differ by TYPE_VECTOR_OPAQUE
13992 /* Convenience macro for matching individual fields. */
13993 #define verify_variant_match(flag) \
13994 do { \
13995 if (flag (tv) != flag (t)) \
13997 error ("type variant differs by %s", #flag); \
13998 debug_tree (tv); \
13999 return false; \
14001 } while (false)
14003 /* tree_base checks. */
14005 verify_variant_match (TREE_CODE);
14006 /* FIXME: Ada builds non-artificial variants of artificial types. */
14007 if (TYPE_ARTIFICIAL (tv) && 0)
14008 verify_variant_match (TYPE_ARTIFICIAL);
14009 if (POINTER_TYPE_P (tv))
14010 verify_variant_match (TYPE_REF_CAN_ALIAS_ALL);
14011 /* FIXME: TYPE_SIZES_GIMPLIFIED may differs for Ada build. */
14012 verify_variant_match (TYPE_UNSIGNED);
14013 verify_variant_match (TYPE_PACKED);
14014 if (TREE_CODE (t) == REFERENCE_TYPE)
14015 verify_variant_match (TYPE_REF_IS_RVALUE);
14016 if (AGGREGATE_TYPE_P (t))
14017 verify_variant_match (TYPE_REVERSE_STORAGE_ORDER);
14018 else
14019 verify_variant_match (TYPE_SATURATING);
14020 /* FIXME: This check trigger during libstdc++ build. */
14021 if (RECORD_OR_UNION_TYPE_P (t) && COMPLETE_TYPE_P (t) && 0)
14022 verify_variant_match (TYPE_FINAL_P);
14024 /* tree_type_common checks. */
14026 if (COMPLETE_TYPE_P (t))
14028 verify_variant_match (TYPE_MODE);
14029 if (TREE_CODE (TYPE_SIZE (t)) != PLACEHOLDER_EXPR
14030 && TREE_CODE (TYPE_SIZE (tv)) != PLACEHOLDER_EXPR)
14031 verify_variant_match (TYPE_SIZE);
14032 if (TREE_CODE (TYPE_SIZE_UNIT (t)) != PLACEHOLDER_EXPR
14033 && TREE_CODE (TYPE_SIZE_UNIT (tv)) != PLACEHOLDER_EXPR
14034 && TYPE_SIZE_UNIT (t) != TYPE_SIZE_UNIT (tv))
14036 gcc_assert (!operand_equal_p (TYPE_SIZE_UNIT (t),
14037 TYPE_SIZE_UNIT (tv), 0));
14038 error ("type variant has different %<TYPE_SIZE_UNIT%>");
14039 debug_tree (tv);
14040 error ("type variant%'s %<TYPE_SIZE_UNIT%>");
14041 debug_tree (TYPE_SIZE_UNIT (tv));
14042 error ("type%'s %<TYPE_SIZE_UNIT%>");
14043 debug_tree (TYPE_SIZE_UNIT (t));
14044 return false;
14046 verify_variant_match (TYPE_NEEDS_CONSTRUCTING);
14048 verify_variant_match (TYPE_PRECISION);
14049 if (RECORD_OR_UNION_TYPE_P (t))
14050 verify_variant_match (TYPE_TRANSPARENT_AGGR);
14051 else if (TREE_CODE (t) == ARRAY_TYPE)
14052 verify_variant_match (TYPE_NONALIASED_COMPONENT);
14053 /* During LTO we merge variant lists from diferent translation units
14054 that may differ BY TYPE_CONTEXT that in turn may point
14055 to TRANSLATION_UNIT_DECL.
14056 Ada also builds variants of types with different TYPE_CONTEXT. */
14057 if ((!in_lto_p || !TYPE_FILE_SCOPE_P (t)) && 0)
14058 verify_variant_match (TYPE_CONTEXT);
14059 if (TREE_CODE (t) == ARRAY_TYPE || TREE_CODE (t) == INTEGER_TYPE)
14060 verify_variant_match (TYPE_STRING_FLAG);
14061 if (TREE_CODE (t) == RECORD_TYPE || TREE_CODE (t) == UNION_TYPE)
14062 verify_variant_match (TYPE_CXX_ODR_P);
14063 if (TYPE_ALIAS_SET_KNOWN_P (t))
14065 error ("type variant with %<TYPE_ALIAS_SET_KNOWN_P%>");
14066 debug_tree (tv);
14067 return false;
14070 /* tree_type_non_common checks. */
14072 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
14073 and dangle the pointer from time to time. */
14074 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_VFIELD (t) != TYPE_VFIELD (tv)
14075 && (in_lto_p || !TYPE_VFIELD (tv)
14076 || TREE_CODE (TYPE_VFIELD (tv)) != TREE_LIST))
14078 error ("type variant has different %<TYPE_VFIELD%>");
14079 debug_tree (tv);
14080 return false;
14082 if ((TREE_CODE (t) == ENUMERAL_TYPE && COMPLETE_TYPE_P (t))
14083 || TREE_CODE (t) == INTEGER_TYPE
14084 || TREE_CODE (t) == BOOLEAN_TYPE
14085 || TREE_CODE (t) == REAL_TYPE
14086 || TREE_CODE (t) == FIXED_POINT_TYPE)
14088 verify_variant_match (TYPE_MAX_VALUE);
14089 verify_variant_match (TYPE_MIN_VALUE);
14091 if (TREE_CODE (t) == METHOD_TYPE)
14092 verify_variant_match (TYPE_METHOD_BASETYPE);
14093 if (TREE_CODE (t) == OFFSET_TYPE)
14094 verify_variant_match (TYPE_OFFSET_BASETYPE);
14095 if (TREE_CODE (t) == ARRAY_TYPE)
14096 verify_variant_match (TYPE_ARRAY_MAX_SIZE);
14097 /* FIXME: Be lax and allow TYPE_BINFO to be missing in variant types
14098 or even type's main variant. This is needed to make bootstrap pass
14099 and the bug seems new in GCC 5.
14100 C++ FE should be updated to make this consistent and we should check
14101 that TYPE_BINFO is always NULL for !COMPLETE_TYPE_P and otherwise there
14102 is a match with main variant.
14104 Also disable the check for Java for now because of parser hack that builds
14105 first an dummy BINFO and then sometimes replace it by real BINFO in some
14106 of the copies. */
14107 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t) && TYPE_BINFO (tv)
14108 && TYPE_BINFO (t) != TYPE_BINFO (tv)
14109 /* FIXME: Java sometimes keep dump TYPE_BINFOs on variant types.
14110 Since there is no cheap way to tell C++/Java type w/o LTO, do checking
14111 at LTO time only. */
14112 && (in_lto_p && odr_type_p (t)))
14114 error ("type variant has different %<TYPE_BINFO%>");
14115 debug_tree (tv);
14116 error ("type variant%'s %<TYPE_BINFO%>");
14117 debug_tree (TYPE_BINFO (tv));
14118 error ("type%'s %<TYPE_BINFO%>");
14119 debug_tree (TYPE_BINFO (t));
14120 return false;
14123 /* Check various uses of TYPE_VALUES_RAW. */
14124 if (TREE_CODE (t) == ENUMERAL_TYPE
14125 && TYPE_VALUES (t))
14126 verify_variant_match (TYPE_VALUES);
14127 else if (TREE_CODE (t) == ARRAY_TYPE)
14128 verify_variant_match (TYPE_DOMAIN);
14129 /* Permit incomplete variants of complete type. While FEs may complete
14130 all variants, this does not happen for C++ templates in all cases. */
14131 else if (RECORD_OR_UNION_TYPE_P (t)
14132 && COMPLETE_TYPE_P (t)
14133 && TYPE_FIELDS (t) != TYPE_FIELDS (tv))
14135 tree f1, f2;
14137 /* Fortran builds qualified variants as new records with items of
14138 qualified type. Verify that they looks same. */
14139 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (tv);
14140 f1 && f2;
14141 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
14142 if (TREE_CODE (f1) != FIELD_DECL || TREE_CODE (f2) != FIELD_DECL
14143 || (TYPE_MAIN_VARIANT (TREE_TYPE (f1))
14144 != TYPE_MAIN_VARIANT (TREE_TYPE (f2))
14145 /* FIXME: gfc_nonrestricted_type builds all types as variants
14146 with exception of pointer types. It deeply copies the type
14147 which means that we may end up with a variant type
14148 referring non-variant pointer. We may change it to
14149 produce types as variants, too, like
14150 objc_get_protocol_qualified_type does. */
14151 && !POINTER_TYPE_P (TREE_TYPE (f1)))
14152 || DECL_FIELD_OFFSET (f1) != DECL_FIELD_OFFSET (f2)
14153 || DECL_FIELD_BIT_OFFSET (f1) != DECL_FIELD_BIT_OFFSET (f2))
14154 break;
14155 if (f1 || f2)
14157 error ("type variant has different %<TYPE_FIELDS%>");
14158 debug_tree (tv);
14159 error ("first mismatch is field");
14160 debug_tree (f1);
14161 error ("and field");
14162 debug_tree (f2);
14163 return false;
14166 else if ((TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE))
14167 verify_variant_match (TYPE_ARG_TYPES);
14168 /* For C++ the qualified variant of array type is really an array type
14169 of qualified TREE_TYPE.
14170 objc builds variants of pointer where pointer to type is a variant, too
14171 in objc_get_protocol_qualified_type. */
14172 if (TREE_TYPE (t) != TREE_TYPE (tv)
14173 && ((TREE_CODE (t) != ARRAY_TYPE
14174 && !POINTER_TYPE_P (t))
14175 || TYPE_MAIN_VARIANT (TREE_TYPE (t))
14176 != TYPE_MAIN_VARIANT (TREE_TYPE (tv))))
14178 error ("type variant has different %<TREE_TYPE%>");
14179 debug_tree (tv);
14180 error ("type variant%'s %<TREE_TYPE%>");
14181 debug_tree (TREE_TYPE (tv));
14182 error ("type%'s %<TREE_TYPE%>");
14183 debug_tree (TREE_TYPE (t));
14184 return false;
14186 if (type_with_alias_set_p (t)
14187 && !gimple_canonical_types_compatible_p (t, tv, false))
14189 error ("type is not compatible with its variant");
14190 debug_tree (tv);
14191 error ("type variant%'s %<TREE_TYPE%>");
14192 debug_tree (TREE_TYPE (tv));
14193 error ("type%'s %<TREE_TYPE%>");
14194 debug_tree (TREE_TYPE (t));
14195 return false;
14197 return true;
14198 #undef verify_variant_match
14202 /* The TYPE_CANONICAL merging machinery. It should closely resemble
14203 the middle-end types_compatible_p function. It needs to avoid
14204 claiming types are different for types that should be treated
14205 the same with respect to TBAA. Canonical types are also used
14206 for IL consistency checks via the useless_type_conversion_p
14207 predicate which does not handle all type kinds itself but falls
14208 back to pointer-comparison of TYPE_CANONICAL for aggregates
14209 for example. */
14211 /* Return true if TYPE_UNSIGNED of TYPE should be ignored for canonical
14212 type calculation because we need to allow inter-operability between signed
14213 and unsigned variants. */
14215 bool
14216 type_with_interoperable_signedness (const_tree type)
14218 /* Fortran standard require C_SIGNED_CHAR to be interoperable with both
14219 signed char and unsigned char. Similarly fortran FE builds
14220 C_SIZE_T as signed type, while C defines it unsigned. */
14222 return tree_code_for_canonical_type_merging (TREE_CODE (type))
14223 == INTEGER_TYPE
14224 && (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node)
14225 || TYPE_PRECISION (type) == TYPE_PRECISION (size_type_node));
14228 /* Return true iff T1 and T2 are structurally identical for what
14229 TBAA is concerned.
14230 This function is used both by lto.c canonical type merging and by the
14231 verifier. If TRUST_TYPE_CANONICAL we do not look into structure of types
14232 that have TYPE_CANONICAL defined and assume them equivalent. This is useful
14233 only for LTO because only in these cases TYPE_CANONICAL equivalence
14234 correspond to one defined by gimple_canonical_types_compatible_p. */
14236 bool
14237 gimple_canonical_types_compatible_p (const_tree t1, const_tree t2,
14238 bool trust_type_canonical)
14240 /* Type variants should be same as the main variant. When not doing sanity
14241 checking to verify this fact, go to main variants and save some work. */
14242 if (trust_type_canonical)
14244 t1 = TYPE_MAIN_VARIANT (t1);
14245 t2 = TYPE_MAIN_VARIANT (t2);
14248 /* Check first for the obvious case of pointer identity. */
14249 if (t1 == t2)
14250 return true;
14252 /* Check that we have two types to compare. */
14253 if (t1 == NULL_TREE || t2 == NULL_TREE)
14254 return false;
14256 /* We consider complete types always compatible with incomplete type.
14257 This does not make sense for canonical type calculation and thus we
14258 need to ensure that we are never called on it.
14260 FIXME: For more correctness the function probably should have three modes
14261 1) mode assuming that types are complete mathcing their structure
14262 2) mode allowing incomplete types but producing equivalence classes
14263 and thus ignoring all info from complete types
14264 3) mode allowing incomplete types to match complete but checking
14265 compatibility between complete types.
14267 1 and 2 can be used for canonical type calculation. 3 is the real
14268 definition of type compatibility that can be used i.e. for warnings during
14269 declaration merging. */
14271 gcc_assert (!trust_type_canonical
14272 || (type_with_alias_set_p (t1) && type_with_alias_set_p (t2)));
14274 /* If the types have been previously registered and found equal
14275 they still are. */
14277 if (TYPE_CANONICAL (t1) && TYPE_CANONICAL (t2)
14278 && trust_type_canonical)
14280 /* Do not use TYPE_CANONICAL of pointer types. For LTO streamed types
14281 they are always NULL, but they are set to non-NULL for types
14282 constructed by build_pointer_type and variants. In this case the
14283 TYPE_CANONICAL is more fine grained than the equivalnce we test (where
14284 all pointers are considered equal. Be sure to not return false
14285 negatives. */
14286 gcc_checking_assert (canonical_type_used_p (t1)
14287 && canonical_type_used_p (t2));
14288 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
14291 /* For types where we do ODR based TBAA the canonical type is always
14292 set correctly, so we know that types are different if their
14293 canonical types does not match. */
14294 if (trust_type_canonical
14295 && (odr_type_p (t1) && odr_based_tbaa_p (t1))
14296 != (odr_type_p (t2) && odr_based_tbaa_p (t2)))
14297 return false;
14299 /* Can't be the same type if the types don't have the same code. */
14300 enum tree_code code = tree_code_for_canonical_type_merging (TREE_CODE (t1));
14301 if (code != tree_code_for_canonical_type_merging (TREE_CODE (t2)))
14302 return false;
14304 /* Qualifiers do not matter for canonical type comparison purposes. */
14306 /* Void types and nullptr types are always the same. */
14307 if (TREE_CODE (t1) == VOID_TYPE
14308 || TREE_CODE (t1) == NULLPTR_TYPE)
14309 return true;
14311 /* Can't be the same type if they have different mode. */
14312 if (TYPE_MODE (t1) != TYPE_MODE (t2))
14313 return false;
14315 /* Non-aggregate types can be handled cheaply. */
14316 if (INTEGRAL_TYPE_P (t1)
14317 || SCALAR_FLOAT_TYPE_P (t1)
14318 || FIXED_POINT_TYPE_P (t1)
14319 || TREE_CODE (t1) == VECTOR_TYPE
14320 || TREE_CODE (t1) == COMPLEX_TYPE
14321 || TREE_CODE (t1) == OFFSET_TYPE
14322 || POINTER_TYPE_P (t1))
14324 /* Can't be the same type if they have different recision. */
14325 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
14326 return false;
14328 /* In some cases the signed and unsigned types are required to be
14329 inter-operable. */
14330 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2)
14331 && !type_with_interoperable_signedness (t1))
14332 return false;
14334 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
14335 interoperable with "signed char". Unless all frontends are revisited
14336 to agree on these types, we must ignore the flag completely. */
14338 /* Fortran standard define C_PTR type that is compatible with every
14339 C pointer. For this reason we need to glob all pointers into one.
14340 Still pointers in different address spaces are not compatible. */
14341 if (POINTER_TYPE_P (t1))
14343 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
14344 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
14345 return false;
14348 /* Tail-recurse to components. */
14349 if (TREE_CODE (t1) == VECTOR_TYPE
14350 || TREE_CODE (t1) == COMPLEX_TYPE)
14351 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
14352 TREE_TYPE (t2),
14353 trust_type_canonical);
14355 return true;
14358 /* Do type-specific comparisons. */
14359 switch (TREE_CODE (t1))
14361 case ARRAY_TYPE:
14362 /* Array types are the same if the element types are the same and
14363 the number of elements are the same. */
14364 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
14365 trust_type_canonical)
14366 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
14367 || TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2)
14368 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
14369 return false;
14370 else
14372 tree i1 = TYPE_DOMAIN (t1);
14373 tree i2 = TYPE_DOMAIN (t2);
14375 /* For an incomplete external array, the type domain can be
14376 NULL_TREE. Check this condition also. */
14377 if (i1 == NULL_TREE && i2 == NULL_TREE)
14378 return true;
14379 else if (i1 == NULL_TREE || i2 == NULL_TREE)
14380 return false;
14381 else
14383 tree min1 = TYPE_MIN_VALUE (i1);
14384 tree min2 = TYPE_MIN_VALUE (i2);
14385 tree max1 = TYPE_MAX_VALUE (i1);
14386 tree max2 = TYPE_MAX_VALUE (i2);
14388 /* The minimum/maximum values have to be the same. */
14389 if ((min1 == min2
14390 || (min1 && min2
14391 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
14392 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
14393 || operand_equal_p (min1, min2, 0))))
14394 && (max1 == max2
14395 || (max1 && max2
14396 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
14397 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
14398 || operand_equal_p (max1, max2, 0)))))
14399 return true;
14400 else
14401 return false;
14405 case METHOD_TYPE:
14406 case FUNCTION_TYPE:
14407 /* Function types are the same if the return type and arguments types
14408 are the same. */
14409 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
14410 trust_type_canonical))
14411 return false;
14413 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
14414 return true;
14415 else
14417 tree parms1, parms2;
14419 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
14420 parms1 && parms2;
14421 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
14423 if (!gimple_canonical_types_compatible_p
14424 (TREE_VALUE (parms1), TREE_VALUE (parms2),
14425 trust_type_canonical))
14426 return false;
14429 if (parms1 || parms2)
14430 return false;
14432 return true;
14435 case RECORD_TYPE:
14436 case UNION_TYPE:
14437 case QUAL_UNION_TYPE:
14439 tree f1, f2;
14441 /* Don't try to compare variants of an incomplete type, before
14442 TYPE_FIELDS has been copied around. */
14443 if (!COMPLETE_TYPE_P (t1) && !COMPLETE_TYPE_P (t2))
14444 return true;
14447 if (TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2))
14448 return false;
14450 /* For aggregate types, all the fields must be the same. */
14451 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
14452 f1 || f2;
14453 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
14455 /* Skip non-fields and zero-sized fields. */
14456 while (f1 && (TREE_CODE (f1) != FIELD_DECL
14457 || (DECL_SIZE (f1)
14458 && integer_zerop (DECL_SIZE (f1)))))
14459 f1 = TREE_CHAIN (f1);
14460 while (f2 && (TREE_CODE (f2) != FIELD_DECL
14461 || (DECL_SIZE (f2)
14462 && integer_zerop (DECL_SIZE (f2)))))
14463 f2 = TREE_CHAIN (f2);
14464 if (!f1 || !f2)
14465 break;
14466 /* The fields must have the same name, offset and type. */
14467 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
14468 || !gimple_compare_field_offset (f1, f2)
14469 || !gimple_canonical_types_compatible_p
14470 (TREE_TYPE (f1), TREE_TYPE (f2),
14471 trust_type_canonical))
14472 return false;
14475 /* If one aggregate has more fields than the other, they
14476 are not the same. */
14477 if (f1 || f2)
14478 return false;
14480 return true;
14483 default:
14484 /* Consider all types with language specific trees in them mutually
14485 compatible. This is executed only from verify_type and false
14486 positives can be tolerated. */
14487 gcc_assert (!in_lto_p);
14488 return true;
14492 /* Verify type T. */
14494 void
14495 verify_type (const_tree t)
14497 bool error_found = false;
14498 tree mv = TYPE_MAIN_VARIANT (t);
14499 if (!mv)
14501 error ("main variant is not defined");
14502 error_found = true;
14504 else if (mv != TYPE_MAIN_VARIANT (mv))
14506 error ("%<TYPE_MAIN_VARIANT%> has different %<TYPE_MAIN_VARIANT%>");
14507 debug_tree (mv);
14508 error_found = true;
14510 else if (t != mv && !verify_type_variant (t, mv))
14511 error_found = true;
14513 tree ct = TYPE_CANONICAL (t);
14514 if (!ct)
14516 else if (TYPE_CANONICAL (t) != ct)
14518 error ("%<TYPE_CANONICAL%> has different %<TYPE_CANONICAL%>");
14519 debug_tree (ct);
14520 error_found = true;
14522 /* Method and function types cannot be used to address memory and thus
14523 TYPE_CANONICAL really matters only for determining useless conversions.
14525 FIXME: C++ FE produce declarations of builtin functions that are not
14526 compatible with main variants. */
14527 else if (TREE_CODE (t) == FUNCTION_TYPE)
14529 else if (t != ct
14530 /* FIXME: gimple_canonical_types_compatible_p cannot compare types
14531 with variably sized arrays because their sizes possibly
14532 gimplified to different variables. */
14533 && !variably_modified_type_p (ct, NULL)
14534 && !gimple_canonical_types_compatible_p (t, ct, false)
14535 && COMPLETE_TYPE_P (t))
14537 error ("%<TYPE_CANONICAL%> is not compatible");
14538 debug_tree (ct);
14539 error_found = true;
14542 if (COMPLETE_TYPE_P (t) && TYPE_CANONICAL (t)
14543 && TYPE_MODE (t) != TYPE_MODE (TYPE_CANONICAL (t)))
14545 error ("%<TYPE_MODE%> of %<TYPE_CANONICAL%> is not compatible");
14546 debug_tree (ct);
14547 error_found = true;
14549 if (TYPE_MAIN_VARIANT (t) == t && ct && TYPE_MAIN_VARIANT (ct) != ct)
14551 error ("%<TYPE_CANONICAL%> of main variant is not main variant");
14552 debug_tree (ct);
14553 debug_tree (TYPE_MAIN_VARIANT (ct));
14554 error_found = true;
14558 /* Check various uses of TYPE_MIN_VALUE_RAW. */
14559 if (RECORD_OR_UNION_TYPE_P (t))
14561 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
14562 and danagle the pointer from time to time. */
14563 if (TYPE_VFIELD (t)
14564 && TREE_CODE (TYPE_VFIELD (t)) != FIELD_DECL
14565 && TREE_CODE (TYPE_VFIELD (t)) != TREE_LIST)
14567 error ("%<TYPE_VFIELD%> is not %<FIELD_DECL%> nor %<TREE_LIST%>");
14568 debug_tree (TYPE_VFIELD (t));
14569 error_found = true;
14572 else if (TREE_CODE (t) == POINTER_TYPE)
14574 if (TYPE_NEXT_PTR_TO (t)
14575 && TREE_CODE (TYPE_NEXT_PTR_TO (t)) != POINTER_TYPE)
14577 error ("%<TYPE_NEXT_PTR_TO%> is not %<POINTER_TYPE%>");
14578 debug_tree (TYPE_NEXT_PTR_TO (t));
14579 error_found = true;
14582 else if (TREE_CODE (t) == REFERENCE_TYPE)
14584 if (TYPE_NEXT_REF_TO (t)
14585 && TREE_CODE (TYPE_NEXT_REF_TO (t)) != REFERENCE_TYPE)
14587 error ("%<TYPE_NEXT_REF_TO%> is not %<REFERENCE_TYPE%>");
14588 debug_tree (TYPE_NEXT_REF_TO (t));
14589 error_found = true;
14592 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
14593 || TREE_CODE (t) == FIXED_POINT_TYPE)
14595 /* FIXME: The following check should pass:
14596 useless_type_conversion_p (const_cast <tree> (t),
14597 TREE_TYPE (TYPE_MIN_VALUE (t))
14598 but does not for C sizetypes in LTO. */
14601 /* Check various uses of TYPE_MAXVAL_RAW. */
14602 if (RECORD_OR_UNION_TYPE_P (t))
14604 if (!TYPE_BINFO (t))
14606 else if (TREE_CODE (TYPE_BINFO (t)) != TREE_BINFO)
14608 error ("%<TYPE_BINFO%> is not %<TREE_BINFO%>");
14609 debug_tree (TYPE_BINFO (t));
14610 error_found = true;
14612 else if (TREE_TYPE (TYPE_BINFO (t)) != TYPE_MAIN_VARIANT (t))
14614 error ("%<TYPE_BINFO%> type is not %<TYPE_MAIN_VARIANT%>");
14615 debug_tree (TREE_TYPE (TYPE_BINFO (t)));
14616 error_found = true;
14619 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
14621 if (TYPE_METHOD_BASETYPE (t)
14622 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != RECORD_TYPE
14623 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != UNION_TYPE)
14625 error ("%<TYPE_METHOD_BASETYPE%> is not record nor union");
14626 debug_tree (TYPE_METHOD_BASETYPE (t));
14627 error_found = true;
14630 else if (TREE_CODE (t) == OFFSET_TYPE)
14632 if (TYPE_OFFSET_BASETYPE (t)
14633 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != RECORD_TYPE
14634 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != UNION_TYPE)
14636 error ("%<TYPE_OFFSET_BASETYPE%> is not record nor union");
14637 debug_tree (TYPE_OFFSET_BASETYPE (t));
14638 error_found = true;
14641 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
14642 || TREE_CODE (t) == FIXED_POINT_TYPE)
14644 /* FIXME: The following check should pass:
14645 useless_type_conversion_p (const_cast <tree> (t),
14646 TREE_TYPE (TYPE_MAX_VALUE (t))
14647 but does not for C sizetypes in LTO. */
14649 else if (TREE_CODE (t) == ARRAY_TYPE)
14651 if (TYPE_ARRAY_MAX_SIZE (t)
14652 && TREE_CODE (TYPE_ARRAY_MAX_SIZE (t)) != INTEGER_CST)
14654 error ("%<TYPE_ARRAY_MAX_SIZE%> not %<INTEGER_CST%>");
14655 debug_tree (TYPE_ARRAY_MAX_SIZE (t));
14656 error_found = true;
14659 else if (TYPE_MAX_VALUE_RAW (t))
14661 error ("%<TYPE_MAX_VALUE_RAW%> non-NULL");
14662 debug_tree (TYPE_MAX_VALUE_RAW (t));
14663 error_found = true;
14666 if (TYPE_LANG_SLOT_1 (t) && in_lto_p)
14668 error ("%<TYPE_LANG_SLOT_1 (binfo)%> field is non-NULL");
14669 debug_tree (TYPE_LANG_SLOT_1 (t));
14670 error_found = true;
14673 /* Check various uses of TYPE_VALUES_RAW. */
14674 if (TREE_CODE (t) == ENUMERAL_TYPE)
14675 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
14677 tree value = TREE_VALUE (l);
14678 tree name = TREE_PURPOSE (l);
14680 /* C FE porduce INTEGER_CST of INTEGER_TYPE, while C++ FE uses
14681 CONST_DECL of ENUMERAL TYPE. */
14682 if (TREE_CODE (value) != INTEGER_CST && TREE_CODE (value) != CONST_DECL)
14684 error ("enum value is not %<CONST_DECL%> or %<INTEGER_CST%>");
14685 debug_tree (value);
14686 debug_tree (name);
14687 error_found = true;
14689 if (TREE_CODE (TREE_TYPE (value)) != INTEGER_TYPE
14690 && !useless_type_conversion_p (const_cast <tree> (t), TREE_TYPE (value)))
14692 error ("enum value type is not %<INTEGER_TYPE%> nor convertible "
14693 "to the enum");
14694 debug_tree (value);
14695 debug_tree (name);
14696 error_found = true;
14698 if (TREE_CODE (name) != IDENTIFIER_NODE)
14700 error ("enum value name is not %<IDENTIFIER_NODE%>");
14701 debug_tree (value);
14702 debug_tree (name);
14703 error_found = true;
14706 else if (TREE_CODE (t) == ARRAY_TYPE)
14708 if (TYPE_DOMAIN (t) && TREE_CODE (TYPE_DOMAIN (t)) != INTEGER_TYPE)
14710 error ("array %<TYPE_DOMAIN%> is not integer type");
14711 debug_tree (TYPE_DOMAIN (t));
14712 error_found = true;
14715 else if (RECORD_OR_UNION_TYPE_P (t))
14717 if (TYPE_FIELDS (t) && !COMPLETE_TYPE_P (t) && in_lto_p)
14719 error ("%<TYPE_FIELDS%> defined in incomplete type");
14720 error_found = true;
14722 for (tree fld = TYPE_FIELDS (t); fld; fld = TREE_CHAIN (fld))
14724 /* TODO: verify properties of decls. */
14725 if (TREE_CODE (fld) == FIELD_DECL)
14727 else if (TREE_CODE (fld) == TYPE_DECL)
14729 else if (TREE_CODE (fld) == CONST_DECL)
14731 else if (VAR_P (fld))
14733 else if (TREE_CODE (fld) == TEMPLATE_DECL)
14735 else if (TREE_CODE (fld) == USING_DECL)
14737 else if (TREE_CODE (fld) == FUNCTION_DECL)
14739 else
14741 error ("wrong tree in %<TYPE_FIELDS%> list");
14742 debug_tree (fld);
14743 error_found = true;
14747 else if (TREE_CODE (t) == INTEGER_TYPE
14748 || TREE_CODE (t) == BOOLEAN_TYPE
14749 || TREE_CODE (t) == OFFSET_TYPE
14750 || TREE_CODE (t) == REFERENCE_TYPE
14751 || TREE_CODE (t) == NULLPTR_TYPE
14752 || TREE_CODE (t) == POINTER_TYPE)
14754 if (TYPE_CACHED_VALUES_P (t) != (TYPE_CACHED_VALUES (t) != NULL))
14756 error ("%<TYPE_CACHED_VALUES_P%> is %i while %<TYPE_CACHED_VALUES%> "
14757 "is %p",
14758 TYPE_CACHED_VALUES_P (t), (void *)TYPE_CACHED_VALUES (t));
14759 error_found = true;
14761 else if (TYPE_CACHED_VALUES_P (t) && TREE_CODE (TYPE_CACHED_VALUES (t)) != TREE_VEC)
14763 error ("%<TYPE_CACHED_VALUES%> is not %<TREE_VEC%>");
14764 debug_tree (TYPE_CACHED_VALUES (t));
14765 error_found = true;
14767 /* Verify just enough of cache to ensure that no one copied it to new type.
14768 All copying should go by copy_node that should clear it. */
14769 else if (TYPE_CACHED_VALUES_P (t))
14771 int i;
14772 for (i = 0; i < TREE_VEC_LENGTH (TYPE_CACHED_VALUES (t)); i++)
14773 if (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)
14774 && TREE_TYPE (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)) != t)
14776 error ("wrong %<TYPE_CACHED_VALUES%> entry");
14777 debug_tree (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i));
14778 error_found = true;
14779 break;
14783 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
14784 for (tree l = TYPE_ARG_TYPES (t); l; l = TREE_CHAIN (l))
14786 /* C++ FE uses TREE_PURPOSE to store initial values. */
14787 if (TREE_PURPOSE (l) && in_lto_p)
14789 error ("%<TREE_PURPOSE%> is non-NULL in %<TYPE_ARG_TYPES%> list");
14790 debug_tree (l);
14791 error_found = true;
14793 if (!TYPE_P (TREE_VALUE (l)))
14795 error ("wrong entry in %<TYPE_ARG_TYPES%> list");
14796 debug_tree (l);
14797 error_found = true;
14800 else if (!is_lang_specific (t) && TYPE_VALUES_RAW (t))
14802 error ("%<TYPE_VALUES_RAW%> field is non-NULL");
14803 debug_tree (TYPE_VALUES_RAW (t));
14804 error_found = true;
14806 if (TREE_CODE (t) != INTEGER_TYPE
14807 && TREE_CODE (t) != BOOLEAN_TYPE
14808 && TREE_CODE (t) != OFFSET_TYPE
14809 && TREE_CODE (t) != REFERENCE_TYPE
14810 && TREE_CODE (t) != NULLPTR_TYPE
14811 && TREE_CODE (t) != POINTER_TYPE
14812 && TYPE_CACHED_VALUES_P (t))
14814 error ("%<TYPE_CACHED_VALUES_P%> is set while it should not be");
14815 error_found = true;
14818 /* ipa-devirt makes an assumption that TYPE_METHOD_BASETYPE is always
14819 TYPE_MAIN_VARIANT and it would be odd to add methods only to variatns
14820 of a type. */
14821 if (TREE_CODE (t) == METHOD_TYPE
14822 && TYPE_MAIN_VARIANT (TYPE_METHOD_BASETYPE (t)) != TYPE_METHOD_BASETYPE (t))
14824 error ("%<TYPE_METHOD_BASETYPE%> is not main variant");
14825 error_found = true;
14828 if (error_found)
14830 debug_tree (const_cast <tree> (t));
14831 internal_error ("%qs failed", __func__);
14836 /* Return 1 if ARG interpreted as signed in its precision is known to be
14837 always positive or 2 if ARG is known to be always negative, or 3 if
14838 ARG may be positive or negative. */
14841 get_range_pos_neg (tree arg)
14843 if (arg == error_mark_node)
14844 return 3;
14846 int prec = TYPE_PRECISION (TREE_TYPE (arg));
14847 int cnt = 0;
14848 if (TREE_CODE (arg) == INTEGER_CST)
14850 wide_int w = wi::sext (wi::to_wide (arg), prec);
14851 if (wi::neg_p (w))
14852 return 2;
14853 else
14854 return 1;
14856 while (CONVERT_EXPR_P (arg)
14857 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
14858 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg, 0))) <= prec)
14860 arg = TREE_OPERAND (arg, 0);
14861 /* Narrower value zero extended into wider type
14862 will always result in positive values. */
14863 if (TYPE_UNSIGNED (TREE_TYPE (arg))
14864 && TYPE_PRECISION (TREE_TYPE (arg)) < prec)
14865 return 1;
14866 prec = TYPE_PRECISION (TREE_TYPE (arg));
14867 if (++cnt > 30)
14868 return 3;
14871 if (TREE_CODE (arg) != SSA_NAME)
14872 return 3;
14873 wide_int arg_min, arg_max;
14874 while (get_range_info (arg, &arg_min, &arg_max) != VR_RANGE)
14876 gimple *g = SSA_NAME_DEF_STMT (arg);
14877 if (is_gimple_assign (g)
14878 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (g)))
14880 tree t = gimple_assign_rhs1 (g);
14881 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
14882 && TYPE_PRECISION (TREE_TYPE (t)) <= prec)
14884 if (TYPE_UNSIGNED (TREE_TYPE (t))
14885 && TYPE_PRECISION (TREE_TYPE (t)) < prec)
14886 return 1;
14887 prec = TYPE_PRECISION (TREE_TYPE (t));
14888 arg = t;
14889 if (++cnt > 30)
14890 return 3;
14891 continue;
14894 return 3;
14896 if (TYPE_UNSIGNED (TREE_TYPE (arg)))
14898 /* For unsigned values, the "positive" range comes
14899 below the "negative" range. */
14900 if (!wi::neg_p (wi::sext (arg_max, prec), SIGNED))
14901 return 1;
14902 if (wi::neg_p (wi::sext (arg_min, prec), SIGNED))
14903 return 2;
14905 else
14907 if (!wi::neg_p (wi::sext (arg_min, prec), SIGNED))
14908 return 1;
14909 if (wi::neg_p (wi::sext (arg_max, prec), SIGNED))
14910 return 2;
14912 return 3;
14918 /* Return true if ARG is marked with the nonnull attribute in the
14919 current function signature. */
14921 bool
14922 nonnull_arg_p (const_tree arg)
14924 tree t, attrs, fntype;
14925 unsigned HOST_WIDE_INT arg_num;
14927 gcc_assert (TREE_CODE (arg) == PARM_DECL
14928 && (POINTER_TYPE_P (TREE_TYPE (arg))
14929 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE));
14931 /* The static chain decl is always non null. */
14932 if (arg == cfun->static_chain_decl)
14933 return true;
14935 /* THIS argument of method is always non-NULL. */
14936 if (TREE_CODE (TREE_TYPE (cfun->decl)) == METHOD_TYPE
14937 && arg == DECL_ARGUMENTS (cfun->decl)
14938 && flag_delete_null_pointer_checks)
14939 return true;
14941 /* Values passed by reference are always non-NULL. */
14942 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
14943 && flag_delete_null_pointer_checks)
14944 return true;
14946 fntype = TREE_TYPE (cfun->decl);
14947 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
14949 attrs = lookup_attribute ("nonnull", attrs);
14951 /* If "nonnull" wasn't specified, we know nothing about the argument. */
14952 if (attrs == NULL_TREE)
14953 return false;
14955 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
14956 if (TREE_VALUE (attrs) == NULL_TREE)
14957 return true;
14959 /* Get the position number for ARG in the function signature. */
14960 for (arg_num = 1, t = DECL_ARGUMENTS (cfun->decl);
14962 t = DECL_CHAIN (t), arg_num++)
14964 if (t == arg)
14965 break;
14968 gcc_assert (t == arg);
14970 /* Now see if ARG_NUM is mentioned in the nonnull list. */
14971 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
14973 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
14974 return true;
14978 return false;
14981 /* Combine LOC and BLOCK to a combined adhoc loc, retaining any range
14982 information. */
14984 location_t
14985 set_block (location_t loc, tree block)
14987 location_t pure_loc = get_pure_location (loc);
14988 source_range src_range = get_range_from_loc (line_table, loc);
14989 return COMBINE_LOCATION_DATA (line_table, pure_loc, src_range, block);
14992 location_t
14993 set_source_range (tree expr, location_t start, location_t finish)
14995 source_range src_range;
14996 src_range.m_start = start;
14997 src_range.m_finish = finish;
14998 return set_source_range (expr, src_range);
15001 location_t
15002 set_source_range (tree expr, source_range src_range)
15004 if (!EXPR_P (expr))
15005 return UNKNOWN_LOCATION;
15007 location_t pure_loc = get_pure_location (EXPR_LOCATION (expr));
15008 location_t adhoc = COMBINE_LOCATION_DATA (line_table,
15009 pure_loc,
15010 src_range,
15011 NULL);
15012 SET_EXPR_LOCATION (expr, adhoc);
15013 return adhoc;
15016 /* Return EXPR, potentially wrapped with a node expression LOC,
15017 if !CAN_HAVE_LOCATION_P (expr).
15019 NON_LVALUE_EXPR is used for wrapping constants, apart from STRING_CST.
15020 VIEW_CONVERT_EXPR is used for wrapping non-constants and STRING_CST.
15022 Wrapper nodes can be identified using location_wrapper_p. */
15024 tree
15025 maybe_wrap_with_location (tree expr, location_t loc)
15027 if (expr == NULL)
15028 return NULL;
15029 if (loc == UNKNOWN_LOCATION)
15030 return expr;
15031 if (CAN_HAVE_LOCATION_P (expr))
15032 return expr;
15033 /* We should only be adding wrappers for constants and for decls,
15034 or for some exceptional tree nodes (e.g. BASELINK in the C++ FE). */
15035 gcc_assert (CONSTANT_CLASS_P (expr)
15036 || DECL_P (expr)
15037 || EXCEPTIONAL_CLASS_P (expr));
15039 /* For now, don't add wrappers to exceptional tree nodes, to minimize
15040 any impact of the wrapper nodes. */
15041 if (EXCEPTIONAL_CLASS_P (expr))
15042 return expr;
15044 /* Compiler-generated temporary variables don't need a wrapper. */
15045 if (DECL_P (expr) && DECL_ARTIFICIAL (expr) && DECL_IGNORED_P (expr))
15046 return expr;
15048 /* If any auto_suppress_location_wrappers are active, don't create
15049 wrappers. */
15050 if (suppress_location_wrappers > 0)
15051 return expr;
15053 tree_code code
15054 = (((CONSTANT_CLASS_P (expr) && TREE_CODE (expr) != STRING_CST)
15055 || (TREE_CODE (expr) == CONST_DECL && !TREE_STATIC (expr)))
15056 ? NON_LVALUE_EXPR : VIEW_CONVERT_EXPR);
15057 tree wrapper = build1_loc (loc, code, TREE_TYPE (expr), expr);
15058 /* Mark this node as being a wrapper. */
15059 EXPR_LOCATION_WRAPPER_P (wrapper) = 1;
15060 return wrapper;
15063 int suppress_location_wrappers;
15065 /* Return the name of combined function FN, for debugging purposes. */
15067 const char *
15068 combined_fn_name (combined_fn fn)
15070 if (builtin_fn_p (fn))
15072 tree fndecl = builtin_decl_explicit (as_builtin_fn (fn));
15073 return IDENTIFIER_POINTER (DECL_NAME (fndecl));
15075 else
15076 return internal_fn_name (as_internal_fn (fn));
15079 /* Return a bitmap with a bit set corresponding to each argument in
15080 a function call type FNTYPE declared with attribute nonnull,
15081 or null if none of the function's argument are nonnull. The caller
15082 must free the bitmap. */
15084 bitmap
15085 get_nonnull_args (const_tree fntype)
15087 if (fntype == NULL_TREE)
15088 return NULL;
15090 bitmap argmap = NULL;
15091 if (TREE_CODE (fntype) == METHOD_TYPE)
15093 /* The this pointer in C++ non-static member functions is
15094 implicitly nonnull whether or not it's declared as such. */
15095 argmap = BITMAP_ALLOC (NULL);
15096 bitmap_set_bit (argmap, 0);
15099 tree attrs = TYPE_ATTRIBUTES (fntype);
15100 if (!attrs)
15101 return argmap;
15103 /* A function declaration can specify multiple attribute nonnull,
15104 each with zero or more arguments. The loop below creates a bitmap
15105 representing a union of all the arguments. An empty (but non-null)
15106 bitmap means that all arguments have been declaraed nonnull. */
15107 for ( ; attrs; attrs = TREE_CHAIN (attrs))
15109 attrs = lookup_attribute ("nonnull", attrs);
15110 if (!attrs)
15111 break;
15113 if (!argmap)
15114 argmap = BITMAP_ALLOC (NULL);
15116 if (!TREE_VALUE (attrs))
15118 /* Clear the bitmap in case a previous attribute nonnull
15119 set it and this one overrides it for all arguments. */
15120 bitmap_clear (argmap);
15121 return argmap;
15124 /* Iterate over the indices of the format arguments declared nonnull
15125 and set a bit for each. */
15126 for (tree idx = TREE_VALUE (attrs); idx; idx = TREE_CHAIN (idx))
15128 unsigned int val = TREE_INT_CST_LOW (TREE_VALUE (idx)) - 1;
15129 bitmap_set_bit (argmap, val);
15133 return argmap;
15136 /* Returns true if TYPE is a type where it and all of its subobjects
15137 (recursively) are of structure, union, or array type. */
15139 static bool
15140 default_is_empty_type (tree type)
15142 if (RECORD_OR_UNION_TYPE_P (type))
15144 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
15145 if (TREE_CODE (field) == FIELD_DECL
15146 && !DECL_PADDING_P (field)
15147 && !default_is_empty_type (TREE_TYPE (field)))
15148 return false;
15149 return true;
15151 else if (TREE_CODE (type) == ARRAY_TYPE)
15152 return (integer_minus_onep (array_type_nelts (type))
15153 || TYPE_DOMAIN (type) == NULL_TREE
15154 || default_is_empty_type (TREE_TYPE (type)));
15155 return false;
15158 /* Implement TARGET_EMPTY_RECORD_P. Return true if TYPE is an empty type
15159 that shouldn't be passed via stack. */
15161 bool
15162 default_is_empty_record (const_tree type)
15164 if (!abi_version_at_least (12))
15165 return false;
15167 if (type == error_mark_node)
15168 return false;
15170 if (TREE_ADDRESSABLE (type))
15171 return false;
15173 return default_is_empty_type (TYPE_MAIN_VARIANT (type));
15176 /* Determine whether TYPE is a structure with a flexible array member,
15177 or a union containing such a structure (possibly recursively). */
15179 bool
15180 flexible_array_type_p (const_tree type)
15182 tree x, last;
15183 switch (TREE_CODE (type))
15185 case RECORD_TYPE:
15186 last = NULL_TREE;
15187 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
15188 if (TREE_CODE (x) == FIELD_DECL)
15189 last = x;
15190 if (last == NULL_TREE)
15191 return false;
15192 if (TREE_CODE (TREE_TYPE (last)) == ARRAY_TYPE
15193 && TYPE_SIZE (TREE_TYPE (last)) == NULL_TREE
15194 && TYPE_DOMAIN (TREE_TYPE (last)) != NULL_TREE
15195 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (last))) == NULL_TREE)
15196 return true;
15197 return false;
15198 case UNION_TYPE:
15199 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
15201 if (TREE_CODE (x) == FIELD_DECL
15202 && flexible_array_type_p (TREE_TYPE (x)))
15203 return true;
15205 return false;
15206 default:
15207 return false;
15211 /* Like int_size_in_bytes, but handle empty records specially. */
15213 HOST_WIDE_INT
15214 arg_int_size_in_bytes (const_tree type)
15216 return TYPE_EMPTY_P (type) ? 0 : int_size_in_bytes (type);
15219 /* Like size_in_bytes, but handle empty records specially. */
15221 tree
15222 arg_size_in_bytes (const_tree type)
15224 return TYPE_EMPTY_P (type) ? size_zero_node : size_in_bytes (type);
15227 /* Return true if an expression with CODE has to have the same result type as
15228 its first operand. */
15230 bool
15231 expr_type_first_operand_type_p (tree_code code)
15233 switch (code)
15235 case NEGATE_EXPR:
15236 case ABS_EXPR:
15237 case BIT_NOT_EXPR:
15238 case PAREN_EXPR:
15239 case CONJ_EXPR:
15241 case PLUS_EXPR:
15242 case MINUS_EXPR:
15243 case MULT_EXPR:
15244 case TRUNC_DIV_EXPR:
15245 case CEIL_DIV_EXPR:
15246 case FLOOR_DIV_EXPR:
15247 case ROUND_DIV_EXPR:
15248 case TRUNC_MOD_EXPR:
15249 case CEIL_MOD_EXPR:
15250 case FLOOR_MOD_EXPR:
15251 case ROUND_MOD_EXPR:
15252 case RDIV_EXPR:
15253 case EXACT_DIV_EXPR:
15254 case MIN_EXPR:
15255 case MAX_EXPR:
15256 case BIT_IOR_EXPR:
15257 case BIT_XOR_EXPR:
15258 case BIT_AND_EXPR:
15260 case LSHIFT_EXPR:
15261 case RSHIFT_EXPR:
15262 case LROTATE_EXPR:
15263 case RROTATE_EXPR:
15264 return true;
15266 default:
15267 return false;
15271 /* Return a typenode for the "standard" C type with a given name. */
15272 tree
15273 get_typenode_from_name (const char *name)
15275 if (name == NULL || *name == '\0')
15276 return NULL_TREE;
15278 if (strcmp (name, "char") == 0)
15279 return char_type_node;
15280 if (strcmp (name, "unsigned char") == 0)
15281 return unsigned_char_type_node;
15282 if (strcmp (name, "signed char") == 0)
15283 return signed_char_type_node;
15285 if (strcmp (name, "short int") == 0)
15286 return short_integer_type_node;
15287 if (strcmp (name, "short unsigned int") == 0)
15288 return short_unsigned_type_node;
15290 if (strcmp (name, "int") == 0)
15291 return integer_type_node;
15292 if (strcmp (name, "unsigned int") == 0)
15293 return unsigned_type_node;
15295 if (strcmp (name, "long int") == 0)
15296 return long_integer_type_node;
15297 if (strcmp (name, "long unsigned int") == 0)
15298 return long_unsigned_type_node;
15300 if (strcmp (name, "long long int") == 0)
15301 return long_long_integer_type_node;
15302 if (strcmp (name, "long long unsigned int") == 0)
15303 return long_long_unsigned_type_node;
15305 gcc_unreachable ();
15308 /* List of pointer types used to declare builtins before we have seen their
15309 real declaration.
15311 Keep the size up to date in tree.h ! */
15312 const builtin_structptr_type builtin_structptr_types[6] =
15314 { fileptr_type_node, ptr_type_node, "FILE" },
15315 { const_tm_ptr_type_node, const_ptr_type_node, "tm" },
15316 { fenv_t_ptr_type_node, ptr_type_node, "fenv_t" },
15317 { const_fenv_t_ptr_type_node, const_ptr_type_node, "fenv_t" },
15318 { fexcept_t_ptr_type_node, ptr_type_node, "fexcept_t" },
15319 { const_fexcept_t_ptr_type_node, const_ptr_type_node, "fexcept_t" }
15322 /* Return the maximum object size. */
15324 tree
15325 max_object_size (void)
15327 /* To do: Make this a configurable parameter. */
15328 return TYPE_MAX_VALUE (ptrdiff_type_node);
15331 /* A wrapper around TARGET_VERIFY_TYPE_CONTEXT that makes the silent_p
15332 parameter default to false and that weeds out error_mark_node. */
15334 bool
15335 verify_type_context (location_t loc, type_context_kind context,
15336 const_tree type, bool silent_p)
15338 if (type == error_mark_node)
15339 return true;
15341 gcc_assert (TYPE_P (type));
15342 return (!targetm.verify_type_context
15343 || targetm.verify_type_context (loc, context, type, silent_p));
15346 #if CHECKING_P
15348 namespace selftest {
15350 /* Selftests for tree. */
15352 /* Verify that integer constants are sane. */
15354 static void
15355 test_integer_constants ()
15357 ASSERT_TRUE (integer_type_node != NULL);
15358 ASSERT_TRUE (build_int_cst (integer_type_node, 0) != NULL);
15360 tree type = integer_type_node;
15362 tree zero = build_zero_cst (type);
15363 ASSERT_EQ (INTEGER_CST, TREE_CODE (zero));
15364 ASSERT_EQ (type, TREE_TYPE (zero));
15366 tree one = build_int_cst (type, 1);
15367 ASSERT_EQ (INTEGER_CST, TREE_CODE (one));
15368 ASSERT_EQ (type, TREE_TYPE (zero));
15371 /* Verify identifiers. */
15373 static void
15374 test_identifiers ()
15376 tree identifier = get_identifier ("foo");
15377 ASSERT_EQ (3, IDENTIFIER_LENGTH (identifier));
15378 ASSERT_STREQ ("foo", IDENTIFIER_POINTER (identifier));
15381 /* Verify LABEL_DECL. */
15383 static void
15384 test_labels ()
15386 tree identifier = get_identifier ("err");
15387 tree label_decl = build_decl (UNKNOWN_LOCATION, LABEL_DECL,
15388 identifier, void_type_node);
15389 ASSERT_EQ (-1, LABEL_DECL_UID (label_decl));
15390 ASSERT_FALSE (FORCED_LABEL (label_decl));
15393 /* Return a new VECTOR_CST node whose type is TYPE and whose values
15394 are given by VALS. */
15396 static tree
15397 build_vector (tree type, vec<tree> vals MEM_STAT_DECL)
15399 gcc_assert (known_eq (vals.length (), TYPE_VECTOR_SUBPARTS (type)));
15400 tree_vector_builder builder (type, vals.length (), 1);
15401 builder.splice (vals);
15402 return builder.build ();
15405 /* Check that VECTOR_CST ACTUAL contains the elements in EXPECTED. */
15407 static void
15408 check_vector_cst (vec<tree> expected, tree actual)
15410 ASSERT_KNOWN_EQ (expected.length (),
15411 TYPE_VECTOR_SUBPARTS (TREE_TYPE (actual)));
15412 for (unsigned int i = 0; i < expected.length (); ++i)
15413 ASSERT_EQ (wi::to_wide (expected[i]),
15414 wi::to_wide (vector_cst_elt (actual, i)));
15417 /* Check that VECTOR_CST ACTUAL contains NPATTERNS duplicated elements,
15418 and that its elements match EXPECTED. */
15420 static void
15421 check_vector_cst_duplicate (vec<tree> expected, tree actual,
15422 unsigned int npatterns)
15424 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
15425 ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (actual));
15426 ASSERT_EQ (npatterns, vector_cst_encoded_nelts (actual));
15427 ASSERT_TRUE (VECTOR_CST_DUPLICATE_P (actual));
15428 ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
15429 check_vector_cst (expected, actual);
15432 /* Check that VECTOR_CST ACTUAL contains NPATTERNS foreground elements
15433 and NPATTERNS background elements, and that its elements match
15434 EXPECTED. */
15436 static void
15437 check_vector_cst_fill (vec<tree> expected, tree actual,
15438 unsigned int npatterns)
15440 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
15441 ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (actual));
15442 ASSERT_EQ (2 * npatterns, vector_cst_encoded_nelts (actual));
15443 ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
15444 ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
15445 check_vector_cst (expected, actual);
15448 /* Check that VECTOR_CST ACTUAL contains NPATTERNS stepped patterns,
15449 and that its elements match EXPECTED. */
15451 static void
15452 check_vector_cst_stepped (vec<tree> expected, tree actual,
15453 unsigned int npatterns)
15455 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
15456 ASSERT_EQ (3, VECTOR_CST_NELTS_PER_PATTERN (actual));
15457 ASSERT_EQ (3 * npatterns, vector_cst_encoded_nelts (actual));
15458 ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
15459 ASSERT_TRUE (VECTOR_CST_STEPPED_P (actual));
15460 check_vector_cst (expected, actual);
15463 /* Test the creation of VECTOR_CSTs. */
15465 static void
15466 test_vector_cst_patterns (ALONE_CXX_MEM_STAT_INFO)
15468 auto_vec<tree, 8> elements (8);
15469 elements.quick_grow (8);
15470 tree element_type = build_nonstandard_integer_type (16, true);
15471 tree vector_type = build_vector_type (element_type, 8);
15473 /* Test a simple linear series with a base of 0 and a step of 1:
15474 { 0, 1, 2, 3, 4, 5, 6, 7 }. */
15475 for (unsigned int i = 0; i < 8; ++i)
15476 elements[i] = build_int_cst (element_type, i);
15477 tree vector = build_vector (vector_type, elements PASS_MEM_STAT);
15478 check_vector_cst_stepped (elements, vector, 1);
15480 /* Try the same with the first element replaced by 100:
15481 { 100, 1, 2, 3, 4, 5, 6, 7 }. */
15482 elements[0] = build_int_cst (element_type, 100);
15483 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15484 check_vector_cst_stepped (elements, vector, 1);
15486 /* Try a series that wraps around.
15487 { 100, 65531, 65532, 65533, 65534, 65535, 0, 1 }. */
15488 for (unsigned int i = 1; i < 8; ++i)
15489 elements[i] = build_int_cst (element_type, (65530 + i) & 0xffff);
15490 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15491 check_vector_cst_stepped (elements, vector, 1);
15493 /* Try a downward series:
15494 { 100, 79, 78, 77, 76, 75, 75, 73 }. */
15495 for (unsigned int i = 1; i < 8; ++i)
15496 elements[i] = build_int_cst (element_type, 80 - i);
15497 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15498 check_vector_cst_stepped (elements, vector, 1);
15500 /* Try two interleaved series with different bases and steps:
15501 { 100, 53, 66, 206, 62, 212, 58, 218 }. */
15502 elements[1] = build_int_cst (element_type, 53);
15503 for (unsigned int i = 2; i < 8; i += 2)
15505 elements[i] = build_int_cst (element_type, 70 - i * 2);
15506 elements[i + 1] = build_int_cst (element_type, 200 + i * 3);
15508 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15509 check_vector_cst_stepped (elements, vector, 2);
15511 /* Try a duplicated value:
15512 { 100, 100, 100, 100, 100, 100, 100, 100 }. */
15513 for (unsigned int i = 1; i < 8; ++i)
15514 elements[i] = elements[0];
15515 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15516 check_vector_cst_duplicate (elements, vector, 1);
15518 /* Try an interleaved duplicated value:
15519 { 100, 55, 100, 55, 100, 55, 100, 55 }. */
15520 elements[1] = build_int_cst (element_type, 55);
15521 for (unsigned int i = 2; i < 8; ++i)
15522 elements[i] = elements[i - 2];
15523 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15524 check_vector_cst_duplicate (elements, vector, 2);
15526 /* Try a duplicated value with 2 exceptions
15527 { 41, 97, 100, 55, 100, 55, 100, 55 }. */
15528 elements[0] = build_int_cst (element_type, 41);
15529 elements[1] = build_int_cst (element_type, 97);
15530 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15531 check_vector_cst_fill (elements, vector, 2);
15533 /* Try with and without a step
15534 { 41, 97, 100, 21, 100, 35, 100, 49 }. */
15535 for (unsigned int i = 3; i < 8; i += 2)
15536 elements[i] = build_int_cst (element_type, i * 7);
15537 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15538 check_vector_cst_stepped (elements, vector, 2);
15540 /* Try a fully-general constant:
15541 { 41, 97, 100, 21, 100, 9990, 100, 49 }. */
15542 elements[5] = build_int_cst (element_type, 9990);
15543 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15544 check_vector_cst_fill (elements, vector, 4);
15547 /* Verify that STRIP_NOPS (NODE) is EXPECTED.
15548 Helper function for test_location_wrappers, to deal with STRIP_NOPS
15549 modifying its argument in-place. */
15551 static void
15552 check_strip_nops (tree node, tree expected)
15554 STRIP_NOPS (node);
15555 ASSERT_EQ (expected, node);
15558 /* Verify location wrappers. */
15560 static void
15561 test_location_wrappers ()
15563 location_t loc = BUILTINS_LOCATION;
15565 ASSERT_EQ (NULL_TREE, maybe_wrap_with_location (NULL_TREE, loc));
15567 /* Wrapping a constant. */
15568 tree int_cst = build_int_cst (integer_type_node, 42);
15569 ASSERT_FALSE (CAN_HAVE_LOCATION_P (int_cst));
15570 ASSERT_FALSE (location_wrapper_p (int_cst));
15572 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
15573 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
15574 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_int_cst));
15575 ASSERT_EQ (int_cst, tree_strip_any_location_wrapper (wrapped_int_cst));
15577 /* We shouldn't add wrapper nodes for UNKNOWN_LOCATION. */
15578 ASSERT_EQ (int_cst, maybe_wrap_with_location (int_cst, UNKNOWN_LOCATION));
15580 /* We shouldn't add wrapper nodes for nodes that CAN_HAVE_LOCATION_P. */
15581 tree cast = build1 (NOP_EXPR, char_type_node, int_cst);
15582 ASSERT_TRUE (CAN_HAVE_LOCATION_P (cast));
15583 ASSERT_EQ (cast, maybe_wrap_with_location (cast, loc));
15585 /* Wrapping a STRING_CST. */
15586 tree string_cst = build_string (4, "foo");
15587 ASSERT_FALSE (CAN_HAVE_LOCATION_P (string_cst));
15588 ASSERT_FALSE (location_wrapper_p (string_cst));
15590 tree wrapped_string_cst = maybe_wrap_with_location (string_cst, loc);
15591 ASSERT_TRUE (location_wrapper_p (wrapped_string_cst));
15592 ASSERT_EQ (VIEW_CONVERT_EXPR, TREE_CODE (wrapped_string_cst));
15593 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_string_cst));
15594 ASSERT_EQ (string_cst, tree_strip_any_location_wrapper (wrapped_string_cst));
15597 /* Wrapping a variable. */
15598 tree int_var = build_decl (UNKNOWN_LOCATION, VAR_DECL,
15599 get_identifier ("some_int_var"),
15600 integer_type_node);
15601 ASSERT_FALSE (CAN_HAVE_LOCATION_P (int_var));
15602 ASSERT_FALSE (location_wrapper_p (int_var));
15604 tree wrapped_int_var = maybe_wrap_with_location (int_var, loc);
15605 ASSERT_TRUE (location_wrapper_p (wrapped_int_var));
15606 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_int_var));
15607 ASSERT_EQ (int_var, tree_strip_any_location_wrapper (wrapped_int_var));
15609 /* Verify that "reinterpret_cast<int>(some_int_var)" is not a location
15610 wrapper. */
15611 tree r_cast = build1 (NON_LVALUE_EXPR, integer_type_node, int_var);
15612 ASSERT_FALSE (location_wrapper_p (r_cast));
15613 ASSERT_EQ (r_cast, tree_strip_any_location_wrapper (r_cast));
15615 /* Verify that STRIP_NOPS removes wrappers. */
15616 check_strip_nops (wrapped_int_cst, int_cst);
15617 check_strip_nops (wrapped_string_cst, string_cst);
15618 check_strip_nops (wrapped_int_var, int_var);
15621 /* Test various tree predicates. Verify that location wrappers don't
15622 affect the results. */
15624 static void
15625 test_predicates ()
15627 /* Build various constants and wrappers around them. */
15629 location_t loc = BUILTINS_LOCATION;
15631 tree i_0 = build_int_cst (integer_type_node, 0);
15632 tree wr_i_0 = maybe_wrap_with_location (i_0, loc);
15634 tree i_1 = build_int_cst (integer_type_node, 1);
15635 tree wr_i_1 = maybe_wrap_with_location (i_1, loc);
15637 tree i_m1 = build_int_cst (integer_type_node, -1);
15638 tree wr_i_m1 = maybe_wrap_with_location (i_m1, loc);
15640 tree f_0 = build_real_from_int_cst (float_type_node, i_0);
15641 tree wr_f_0 = maybe_wrap_with_location (f_0, loc);
15642 tree f_1 = build_real_from_int_cst (float_type_node, i_1);
15643 tree wr_f_1 = maybe_wrap_with_location (f_1, loc);
15644 tree f_m1 = build_real_from_int_cst (float_type_node, i_m1);
15645 tree wr_f_m1 = maybe_wrap_with_location (f_m1, loc);
15647 tree c_i_0 = build_complex (NULL_TREE, i_0, i_0);
15648 tree c_i_1 = build_complex (NULL_TREE, i_1, i_0);
15649 tree c_i_m1 = build_complex (NULL_TREE, i_m1, i_0);
15651 tree c_f_0 = build_complex (NULL_TREE, f_0, f_0);
15652 tree c_f_1 = build_complex (NULL_TREE, f_1, f_0);
15653 tree c_f_m1 = build_complex (NULL_TREE, f_m1, f_0);
15655 /* TODO: vector constants. */
15657 /* Test integer_onep. */
15658 ASSERT_FALSE (integer_onep (i_0));
15659 ASSERT_FALSE (integer_onep (wr_i_0));
15660 ASSERT_TRUE (integer_onep (i_1));
15661 ASSERT_TRUE (integer_onep (wr_i_1));
15662 ASSERT_FALSE (integer_onep (i_m1));
15663 ASSERT_FALSE (integer_onep (wr_i_m1));
15664 ASSERT_FALSE (integer_onep (f_0));
15665 ASSERT_FALSE (integer_onep (wr_f_0));
15666 ASSERT_FALSE (integer_onep (f_1));
15667 ASSERT_FALSE (integer_onep (wr_f_1));
15668 ASSERT_FALSE (integer_onep (f_m1));
15669 ASSERT_FALSE (integer_onep (wr_f_m1));
15670 ASSERT_FALSE (integer_onep (c_i_0));
15671 ASSERT_TRUE (integer_onep (c_i_1));
15672 ASSERT_FALSE (integer_onep (c_i_m1));
15673 ASSERT_FALSE (integer_onep (c_f_0));
15674 ASSERT_FALSE (integer_onep (c_f_1));
15675 ASSERT_FALSE (integer_onep (c_f_m1));
15677 /* Test integer_zerop. */
15678 ASSERT_TRUE (integer_zerop (i_0));
15679 ASSERT_TRUE (integer_zerop (wr_i_0));
15680 ASSERT_FALSE (integer_zerop (i_1));
15681 ASSERT_FALSE (integer_zerop (wr_i_1));
15682 ASSERT_FALSE (integer_zerop (i_m1));
15683 ASSERT_FALSE (integer_zerop (wr_i_m1));
15684 ASSERT_FALSE (integer_zerop (f_0));
15685 ASSERT_FALSE (integer_zerop (wr_f_0));
15686 ASSERT_FALSE (integer_zerop (f_1));
15687 ASSERT_FALSE (integer_zerop (wr_f_1));
15688 ASSERT_FALSE (integer_zerop (f_m1));
15689 ASSERT_FALSE (integer_zerop (wr_f_m1));
15690 ASSERT_TRUE (integer_zerop (c_i_0));
15691 ASSERT_FALSE (integer_zerop (c_i_1));
15692 ASSERT_FALSE (integer_zerop (c_i_m1));
15693 ASSERT_FALSE (integer_zerop (c_f_0));
15694 ASSERT_FALSE (integer_zerop (c_f_1));
15695 ASSERT_FALSE (integer_zerop (c_f_m1));
15697 /* Test integer_all_onesp. */
15698 ASSERT_FALSE (integer_all_onesp (i_0));
15699 ASSERT_FALSE (integer_all_onesp (wr_i_0));
15700 ASSERT_FALSE (integer_all_onesp (i_1));
15701 ASSERT_FALSE (integer_all_onesp (wr_i_1));
15702 ASSERT_TRUE (integer_all_onesp (i_m1));
15703 ASSERT_TRUE (integer_all_onesp (wr_i_m1));
15704 ASSERT_FALSE (integer_all_onesp (f_0));
15705 ASSERT_FALSE (integer_all_onesp (wr_f_0));
15706 ASSERT_FALSE (integer_all_onesp (f_1));
15707 ASSERT_FALSE (integer_all_onesp (wr_f_1));
15708 ASSERT_FALSE (integer_all_onesp (f_m1));
15709 ASSERT_FALSE (integer_all_onesp (wr_f_m1));
15710 ASSERT_FALSE (integer_all_onesp (c_i_0));
15711 ASSERT_FALSE (integer_all_onesp (c_i_1));
15712 ASSERT_FALSE (integer_all_onesp (c_i_m1));
15713 ASSERT_FALSE (integer_all_onesp (c_f_0));
15714 ASSERT_FALSE (integer_all_onesp (c_f_1));
15715 ASSERT_FALSE (integer_all_onesp (c_f_m1));
15717 /* Test integer_minus_onep. */
15718 ASSERT_FALSE (integer_minus_onep (i_0));
15719 ASSERT_FALSE (integer_minus_onep (wr_i_0));
15720 ASSERT_FALSE (integer_minus_onep (i_1));
15721 ASSERT_FALSE (integer_minus_onep (wr_i_1));
15722 ASSERT_TRUE (integer_minus_onep (i_m1));
15723 ASSERT_TRUE (integer_minus_onep (wr_i_m1));
15724 ASSERT_FALSE (integer_minus_onep (f_0));
15725 ASSERT_FALSE (integer_minus_onep (wr_f_0));
15726 ASSERT_FALSE (integer_minus_onep (f_1));
15727 ASSERT_FALSE (integer_minus_onep (wr_f_1));
15728 ASSERT_FALSE (integer_minus_onep (f_m1));
15729 ASSERT_FALSE (integer_minus_onep (wr_f_m1));
15730 ASSERT_FALSE (integer_minus_onep (c_i_0));
15731 ASSERT_FALSE (integer_minus_onep (c_i_1));
15732 ASSERT_TRUE (integer_minus_onep (c_i_m1));
15733 ASSERT_FALSE (integer_minus_onep (c_f_0));
15734 ASSERT_FALSE (integer_minus_onep (c_f_1));
15735 ASSERT_FALSE (integer_minus_onep (c_f_m1));
15737 /* Test integer_each_onep. */
15738 ASSERT_FALSE (integer_each_onep (i_0));
15739 ASSERT_FALSE (integer_each_onep (wr_i_0));
15740 ASSERT_TRUE (integer_each_onep (i_1));
15741 ASSERT_TRUE (integer_each_onep (wr_i_1));
15742 ASSERT_FALSE (integer_each_onep (i_m1));
15743 ASSERT_FALSE (integer_each_onep (wr_i_m1));
15744 ASSERT_FALSE (integer_each_onep (f_0));
15745 ASSERT_FALSE (integer_each_onep (wr_f_0));
15746 ASSERT_FALSE (integer_each_onep (f_1));
15747 ASSERT_FALSE (integer_each_onep (wr_f_1));
15748 ASSERT_FALSE (integer_each_onep (f_m1));
15749 ASSERT_FALSE (integer_each_onep (wr_f_m1));
15750 ASSERT_FALSE (integer_each_onep (c_i_0));
15751 ASSERT_FALSE (integer_each_onep (c_i_1));
15752 ASSERT_FALSE (integer_each_onep (c_i_m1));
15753 ASSERT_FALSE (integer_each_onep (c_f_0));
15754 ASSERT_FALSE (integer_each_onep (c_f_1));
15755 ASSERT_FALSE (integer_each_onep (c_f_m1));
15757 /* Test integer_truep. */
15758 ASSERT_FALSE (integer_truep (i_0));
15759 ASSERT_FALSE (integer_truep (wr_i_0));
15760 ASSERT_TRUE (integer_truep (i_1));
15761 ASSERT_TRUE (integer_truep (wr_i_1));
15762 ASSERT_FALSE (integer_truep (i_m1));
15763 ASSERT_FALSE (integer_truep (wr_i_m1));
15764 ASSERT_FALSE (integer_truep (f_0));
15765 ASSERT_FALSE (integer_truep (wr_f_0));
15766 ASSERT_FALSE (integer_truep (f_1));
15767 ASSERT_FALSE (integer_truep (wr_f_1));
15768 ASSERT_FALSE (integer_truep (f_m1));
15769 ASSERT_FALSE (integer_truep (wr_f_m1));
15770 ASSERT_FALSE (integer_truep (c_i_0));
15771 ASSERT_TRUE (integer_truep (c_i_1));
15772 ASSERT_FALSE (integer_truep (c_i_m1));
15773 ASSERT_FALSE (integer_truep (c_f_0));
15774 ASSERT_FALSE (integer_truep (c_f_1));
15775 ASSERT_FALSE (integer_truep (c_f_m1));
15777 /* Test integer_nonzerop. */
15778 ASSERT_FALSE (integer_nonzerop (i_0));
15779 ASSERT_FALSE (integer_nonzerop (wr_i_0));
15780 ASSERT_TRUE (integer_nonzerop (i_1));
15781 ASSERT_TRUE (integer_nonzerop (wr_i_1));
15782 ASSERT_TRUE (integer_nonzerop (i_m1));
15783 ASSERT_TRUE (integer_nonzerop (wr_i_m1));
15784 ASSERT_FALSE (integer_nonzerop (f_0));
15785 ASSERT_FALSE (integer_nonzerop (wr_f_0));
15786 ASSERT_FALSE (integer_nonzerop (f_1));
15787 ASSERT_FALSE (integer_nonzerop (wr_f_1));
15788 ASSERT_FALSE (integer_nonzerop (f_m1));
15789 ASSERT_FALSE (integer_nonzerop (wr_f_m1));
15790 ASSERT_FALSE (integer_nonzerop (c_i_0));
15791 ASSERT_TRUE (integer_nonzerop (c_i_1));
15792 ASSERT_TRUE (integer_nonzerop (c_i_m1));
15793 ASSERT_FALSE (integer_nonzerop (c_f_0));
15794 ASSERT_FALSE (integer_nonzerop (c_f_1));
15795 ASSERT_FALSE (integer_nonzerop (c_f_m1));
15797 /* Test real_zerop. */
15798 ASSERT_FALSE (real_zerop (i_0));
15799 ASSERT_FALSE (real_zerop (wr_i_0));
15800 ASSERT_FALSE (real_zerop (i_1));
15801 ASSERT_FALSE (real_zerop (wr_i_1));
15802 ASSERT_FALSE (real_zerop (i_m1));
15803 ASSERT_FALSE (real_zerop (wr_i_m1));
15804 ASSERT_TRUE (real_zerop (f_0));
15805 ASSERT_TRUE (real_zerop (wr_f_0));
15806 ASSERT_FALSE (real_zerop (f_1));
15807 ASSERT_FALSE (real_zerop (wr_f_1));
15808 ASSERT_FALSE (real_zerop (f_m1));
15809 ASSERT_FALSE (real_zerop (wr_f_m1));
15810 ASSERT_FALSE (real_zerop (c_i_0));
15811 ASSERT_FALSE (real_zerop (c_i_1));
15812 ASSERT_FALSE (real_zerop (c_i_m1));
15813 ASSERT_TRUE (real_zerop (c_f_0));
15814 ASSERT_FALSE (real_zerop (c_f_1));
15815 ASSERT_FALSE (real_zerop (c_f_m1));
15817 /* Test real_onep. */
15818 ASSERT_FALSE (real_onep (i_0));
15819 ASSERT_FALSE (real_onep (wr_i_0));
15820 ASSERT_FALSE (real_onep (i_1));
15821 ASSERT_FALSE (real_onep (wr_i_1));
15822 ASSERT_FALSE (real_onep (i_m1));
15823 ASSERT_FALSE (real_onep (wr_i_m1));
15824 ASSERT_FALSE (real_onep (f_0));
15825 ASSERT_FALSE (real_onep (wr_f_0));
15826 ASSERT_TRUE (real_onep (f_1));
15827 ASSERT_TRUE (real_onep (wr_f_1));
15828 ASSERT_FALSE (real_onep (f_m1));
15829 ASSERT_FALSE (real_onep (wr_f_m1));
15830 ASSERT_FALSE (real_onep (c_i_0));
15831 ASSERT_FALSE (real_onep (c_i_1));
15832 ASSERT_FALSE (real_onep (c_i_m1));
15833 ASSERT_FALSE (real_onep (c_f_0));
15834 ASSERT_TRUE (real_onep (c_f_1));
15835 ASSERT_FALSE (real_onep (c_f_m1));
15837 /* Test real_minus_onep. */
15838 ASSERT_FALSE (real_minus_onep (i_0));
15839 ASSERT_FALSE (real_minus_onep (wr_i_0));
15840 ASSERT_FALSE (real_minus_onep (i_1));
15841 ASSERT_FALSE (real_minus_onep (wr_i_1));
15842 ASSERT_FALSE (real_minus_onep (i_m1));
15843 ASSERT_FALSE (real_minus_onep (wr_i_m1));
15844 ASSERT_FALSE (real_minus_onep (f_0));
15845 ASSERT_FALSE (real_minus_onep (wr_f_0));
15846 ASSERT_FALSE (real_minus_onep (f_1));
15847 ASSERT_FALSE (real_minus_onep (wr_f_1));
15848 ASSERT_TRUE (real_minus_onep (f_m1));
15849 ASSERT_TRUE (real_minus_onep (wr_f_m1));
15850 ASSERT_FALSE (real_minus_onep (c_i_0));
15851 ASSERT_FALSE (real_minus_onep (c_i_1));
15852 ASSERT_FALSE (real_minus_onep (c_i_m1));
15853 ASSERT_FALSE (real_minus_onep (c_f_0));
15854 ASSERT_FALSE (real_minus_onep (c_f_1));
15855 ASSERT_TRUE (real_minus_onep (c_f_m1));
15857 /* Test zerop. */
15858 ASSERT_TRUE (zerop (i_0));
15859 ASSERT_TRUE (zerop (wr_i_0));
15860 ASSERT_FALSE (zerop (i_1));
15861 ASSERT_FALSE (zerop (wr_i_1));
15862 ASSERT_FALSE (zerop (i_m1));
15863 ASSERT_FALSE (zerop (wr_i_m1));
15864 ASSERT_TRUE (zerop (f_0));
15865 ASSERT_TRUE (zerop (wr_f_0));
15866 ASSERT_FALSE (zerop (f_1));
15867 ASSERT_FALSE (zerop (wr_f_1));
15868 ASSERT_FALSE (zerop (f_m1));
15869 ASSERT_FALSE (zerop (wr_f_m1));
15870 ASSERT_TRUE (zerop (c_i_0));
15871 ASSERT_FALSE (zerop (c_i_1));
15872 ASSERT_FALSE (zerop (c_i_m1));
15873 ASSERT_TRUE (zerop (c_f_0));
15874 ASSERT_FALSE (zerop (c_f_1));
15875 ASSERT_FALSE (zerop (c_f_m1));
15877 /* Test tree_expr_nonnegative_p. */
15878 ASSERT_TRUE (tree_expr_nonnegative_p (i_0));
15879 ASSERT_TRUE (tree_expr_nonnegative_p (wr_i_0));
15880 ASSERT_TRUE (tree_expr_nonnegative_p (i_1));
15881 ASSERT_TRUE (tree_expr_nonnegative_p (wr_i_1));
15882 ASSERT_FALSE (tree_expr_nonnegative_p (i_m1));
15883 ASSERT_FALSE (tree_expr_nonnegative_p (wr_i_m1));
15884 ASSERT_TRUE (tree_expr_nonnegative_p (f_0));
15885 ASSERT_TRUE (tree_expr_nonnegative_p (wr_f_0));
15886 ASSERT_TRUE (tree_expr_nonnegative_p (f_1));
15887 ASSERT_TRUE (tree_expr_nonnegative_p (wr_f_1));
15888 ASSERT_FALSE (tree_expr_nonnegative_p (f_m1));
15889 ASSERT_FALSE (tree_expr_nonnegative_p (wr_f_m1));
15890 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_0));
15891 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_1));
15892 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_m1));
15893 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_0));
15894 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_1));
15895 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_m1));
15897 /* Test tree_expr_nonzero_p. */
15898 ASSERT_FALSE (tree_expr_nonzero_p (i_0));
15899 ASSERT_FALSE (tree_expr_nonzero_p (wr_i_0));
15900 ASSERT_TRUE (tree_expr_nonzero_p (i_1));
15901 ASSERT_TRUE (tree_expr_nonzero_p (wr_i_1));
15902 ASSERT_TRUE (tree_expr_nonzero_p (i_m1));
15903 ASSERT_TRUE (tree_expr_nonzero_p (wr_i_m1));
15905 /* Test integer_valued_real_p. */
15906 ASSERT_FALSE (integer_valued_real_p (i_0));
15907 ASSERT_TRUE (integer_valued_real_p (f_0));
15908 ASSERT_TRUE (integer_valued_real_p (wr_f_0));
15909 ASSERT_TRUE (integer_valued_real_p (f_1));
15910 ASSERT_TRUE (integer_valued_real_p (wr_f_1));
15912 /* Test integer_pow2p. */
15913 ASSERT_FALSE (integer_pow2p (i_0));
15914 ASSERT_TRUE (integer_pow2p (i_1));
15915 ASSERT_TRUE (integer_pow2p (wr_i_1));
15917 /* Test uniform_integer_cst_p. */
15918 ASSERT_TRUE (uniform_integer_cst_p (i_0));
15919 ASSERT_TRUE (uniform_integer_cst_p (wr_i_0));
15920 ASSERT_TRUE (uniform_integer_cst_p (i_1));
15921 ASSERT_TRUE (uniform_integer_cst_p (wr_i_1));
15922 ASSERT_TRUE (uniform_integer_cst_p (i_m1));
15923 ASSERT_TRUE (uniform_integer_cst_p (wr_i_m1));
15924 ASSERT_FALSE (uniform_integer_cst_p (f_0));
15925 ASSERT_FALSE (uniform_integer_cst_p (wr_f_0));
15926 ASSERT_FALSE (uniform_integer_cst_p (f_1));
15927 ASSERT_FALSE (uniform_integer_cst_p (wr_f_1));
15928 ASSERT_FALSE (uniform_integer_cst_p (f_m1));
15929 ASSERT_FALSE (uniform_integer_cst_p (wr_f_m1));
15930 ASSERT_FALSE (uniform_integer_cst_p (c_i_0));
15931 ASSERT_FALSE (uniform_integer_cst_p (c_i_1));
15932 ASSERT_FALSE (uniform_integer_cst_p (c_i_m1));
15933 ASSERT_FALSE (uniform_integer_cst_p (c_f_0));
15934 ASSERT_FALSE (uniform_integer_cst_p (c_f_1));
15935 ASSERT_FALSE (uniform_integer_cst_p (c_f_m1));
15938 /* Check that string escaping works correctly. */
15940 static void
15941 test_escaped_strings (void)
15943 int saved_cutoff;
15944 escaped_string msg;
15946 msg.escape (NULL);
15947 /* ASSERT_STREQ does not accept NULL as a valid test
15948 result, so we have to use ASSERT_EQ instead. */
15949 ASSERT_EQ (NULL, (const char *) msg);
15951 msg.escape ("");
15952 ASSERT_STREQ ("", (const char *) msg);
15954 msg.escape ("foobar");
15955 ASSERT_STREQ ("foobar", (const char *) msg);
15957 /* Ensure that we have -fmessage-length set to 0. */
15958 saved_cutoff = pp_line_cutoff (global_dc->printer);
15959 pp_line_cutoff (global_dc->printer) = 0;
15961 msg.escape ("foo\nbar");
15962 ASSERT_STREQ ("foo\\nbar", (const char *) msg);
15964 msg.escape ("\a\b\f\n\r\t\v");
15965 ASSERT_STREQ ("\\a\\b\\f\\n\\r\\t\\v", (const char *) msg);
15967 /* Now repeat the tests with -fmessage-length set to 5. */
15968 pp_line_cutoff (global_dc->printer) = 5;
15970 /* Note that the newline is not translated into an escape. */
15971 msg.escape ("foo\nbar");
15972 ASSERT_STREQ ("foo\nbar", (const char *) msg);
15974 msg.escape ("\a\b\f\n\r\t\v");
15975 ASSERT_STREQ ("\\a\\b\\f\n\\r\\t\\v", (const char *) msg);
15977 /* Restore the original message length setting. */
15978 pp_line_cutoff (global_dc->printer) = saved_cutoff;
15981 /* Run all of the selftests within this file. */
15983 void
15984 tree_c_tests ()
15986 test_integer_constants ();
15987 test_identifiers ();
15988 test_labels ();
15989 test_vector_cst_patterns ();
15990 test_location_wrappers ();
15991 test_predicates ();
15992 test_escaped_strings ();
15995 } // namespace selftest
15997 #endif /* CHECKING_P */
15999 #include "gt-tree.h"