c++: remove some xfails
[official-gcc.git] / gcc / tree.cc
blobfed1434d141d8297075cece345a5509c3b650b1d
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2022 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"
71 #include "gimple-range.h"
72 #include "gomp-constants.h"
73 #include "dfp.h"
74 #include "asan.h"
75 #include "ubsan.h"
77 /* Tree code classes. */
79 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
80 #define END_OF_BASE_TREE_CODES tcc_exceptional,
82 const enum tree_code_class tree_code_type[] = {
83 #include "all-tree.def"
86 #undef DEFTREECODE
87 #undef END_OF_BASE_TREE_CODES
89 /* Table indexed by tree code giving number of expression
90 operands beyond the fixed part of the node structure.
91 Not used for types or decls. */
93 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
94 #define END_OF_BASE_TREE_CODES 0,
96 const unsigned char tree_code_length[] = {
97 #include "all-tree.def"
100 #undef DEFTREECODE
101 #undef END_OF_BASE_TREE_CODES
103 /* Names of tree components.
104 Used for printing out the tree and error messages. */
105 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
106 #define END_OF_BASE_TREE_CODES "@dummy",
108 static const char *const tree_code_name[] = {
109 #include "all-tree.def"
112 #undef DEFTREECODE
113 #undef END_OF_BASE_TREE_CODES
115 /* Each tree code class has an associated string representation.
116 These must correspond to the tree_code_class entries. */
118 const char *const tree_code_class_strings[] =
120 "exceptional",
121 "constant",
122 "type",
123 "declaration",
124 "reference",
125 "comparison",
126 "unary",
127 "binary",
128 "statement",
129 "vl_exp",
130 "expression"
133 /* obstack.[ch] explicitly declined to prototype this. */
134 extern int _obstack_allocated_p (struct obstack *h, void *obj);
136 /* Statistics-gathering stuff. */
138 static uint64_t tree_code_counts[MAX_TREE_CODES];
139 uint64_t tree_node_counts[(int) all_kinds];
140 uint64_t tree_node_sizes[(int) all_kinds];
142 /* Keep in sync with tree.h:enum tree_node_kind. */
143 static const char * const tree_node_kind_names[] = {
144 "decls",
145 "types",
146 "blocks",
147 "stmts",
148 "refs",
149 "exprs",
150 "constants",
151 "identifiers",
152 "vecs",
153 "binfos",
154 "ssa names",
155 "constructors",
156 "random kinds",
157 "lang_decl kinds",
158 "lang_type kinds",
159 "omp clauses",
162 /* Unique id for next decl created. */
163 static GTY(()) int next_decl_uid;
164 /* Unique id for next type created. */
165 static GTY(()) unsigned next_type_uid = 1;
166 /* Unique id for next debug decl created. Use negative numbers,
167 to catch erroneous uses. */
168 static GTY(()) int next_debug_decl_uid;
170 /* Since we cannot rehash a type after it is in the table, we have to
171 keep the hash code. */
173 struct GTY((for_user)) type_hash {
174 unsigned long hash;
175 tree type;
178 /* Initial size of the hash table (rounded to next prime). */
179 #define TYPE_HASH_INITIAL_SIZE 1000
181 struct type_cache_hasher : ggc_cache_ptr_hash<type_hash>
183 static hashval_t hash (type_hash *t) { return t->hash; }
184 static bool equal (type_hash *a, type_hash *b);
186 static int
187 keep_cache_entry (type_hash *&t)
189 return ggc_marked_p (t->type);
193 /* Now here is the hash table. When recording a type, it is added to
194 the slot whose index is the hash code. Note that the hash table is
195 used for several kinds of types (function types, array types and
196 array index range types, for now). While all these live in the
197 same table, they are completely independent, and the hash code is
198 computed differently for each of these. */
200 static GTY ((cache)) hash_table<type_cache_hasher> *type_hash_table;
202 /* Hash table and temporary node for larger integer const values. */
203 static GTY (()) tree int_cst_node;
205 struct int_cst_hasher : ggc_cache_ptr_hash<tree_node>
207 static hashval_t hash (tree t);
208 static bool equal (tree x, tree y);
211 static GTY ((cache)) hash_table<int_cst_hasher> *int_cst_hash_table;
213 /* Class and variable for making sure that there is a single POLY_INT_CST
214 for a given value. */
215 struct poly_int_cst_hasher : ggc_cache_ptr_hash<tree_node>
217 typedef std::pair<tree, const poly_wide_int *> compare_type;
218 static hashval_t hash (tree t);
219 static bool equal (tree x, const compare_type &y);
222 static GTY ((cache)) hash_table<poly_int_cst_hasher> *poly_int_cst_hash_table;
224 /* Hash table for optimization flags and target option flags. Use the same
225 hash table for both sets of options. Nodes for building the current
226 optimization and target option nodes. The assumption is most of the time
227 the options created will already be in the hash table, so we avoid
228 allocating and freeing up a node repeatably. */
229 static GTY (()) tree cl_optimization_node;
230 static GTY (()) tree cl_target_option_node;
232 struct cl_option_hasher : ggc_cache_ptr_hash<tree_node>
234 static hashval_t hash (tree t);
235 static bool equal (tree x, tree y);
238 static GTY ((cache)) hash_table<cl_option_hasher> *cl_option_hash_table;
240 /* General tree->tree mapping structure for use in hash tables. */
243 static GTY ((cache))
244 hash_table<tree_decl_map_cache_hasher> *debug_expr_for_decl;
246 static GTY ((cache))
247 hash_table<tree_decl_map_cache_hasher> *value_expr_for_decl;
249 static GTY ((cache))
250 hash_table<tree_vec_map_cache_hasher> *debug_args_for_decl;
252 static void set_type_quals (tree, int);
253 static void print_type_hash_statistics (void);
254 static void print_debug_expr_statistics (void);
255 static void print_value_expr_statistics (void);
257 tree global_trees[TI_MAX];
258 tree integer_types[itk_none];
260 bool int_n_enabled_p[NUM_INT_N_ENTS];
261 struct int_n_trees_t int_n_trees [NUM_INT_N_ENTS];
263 bool tree_contains_struct[MAX_TREE_CODES][64];
265 /* Number of operands for each OMP clause. */
266 unsigned const char omp_clause_num_ops[] =
268 0, /* OMP_CLAUSE_ERROR */
269 1, /* OMP_CLAUSE_PRIVATE */
270 1, /* OMP_CLAUSE_SHARED */
271 1, /* OMP_CLAUSE_FIRSTPRIVATE */
272 2, /* OMP_CLAUSE_LASTPRIVATE */
273 5, /* OMP_CLAUSE_REDUCTION */
274 5, /* OMP_CLAUSE_TASK_REDUCTION */
275 5, /* OMP_CLAUSE_IN_REDUCTION */
276 1, /* OMP_CLAUSE_COPYIN */
277 1, /* OMP_CLAUSE_COPYPRIVATE */
278 3, /* OMP_CLAUSE_LINEAR */
279 1, /* OMP_CLAUSE_AFFINITY */
280 2, /* OMP_CLAUSE_ALIGNED */
281 3, /* OMP_CLAUSE_ALLOCATE */
282 1, /* OMP_CLAUSE_DEPEND */
283 1, /* OMP_CLAUSE_NONTEMPORAL */
284 1, /* OMP_CLAUSE_UNIFORM */
285 1, /* OMP_CLAUSE_ENTER */
286 1, /* OMP_CLAUSE_LINK */
287 1, /* OMP_CLAUSE_DETACH */
288 1, /* OMP_CLAUSE_USE_DEVICE_PTR */
289 1, /* OMP_CLAUSE_USE_DEVICE_ADDR */
290 1, /* OMP_CLAUSE_IS_DEVICE_PTR */
291 1, /* OMP_CLAUSE_INCLUSIVE */
292 1, /* OMP_CLAUSE_EXCLUSIVE */
293 2, /* OMP_CLAUSE_FROM */
294 2, /* OMP_CLAUSE_TO */
295 2, /* OMP_CLAUSE_MAP */
296 1, /* OMP_CLAUSE_HAS_DEVICE_ADDR */
297 2, /* OMP_CLAUSE__CACHE_ */
298 2, /* OMP_CLAUSE_GANG */
299 1, /* OMP_CLAUSE_ASYNC */
300 1, /* OMP_CLAUSE_WAIT */
301 0, /* OMP_CLAUSE_AUTO */
302 0, /* OMP_CLAUSE_SEQ */
303 1, /* OMP_CLAUSE__LOOPTEMP_ */
304 1, /* OMP_CLAUSE__REDUCTEMP_ */
305 1, /* OMP_CLAUSE__CONDTEMP_ */
306 1, /* OMP_CLAUSE__SCANTEMP_ */
307 1, /* OMP_CLAUSE_IF */
308 1, /* OMP_CLAUSE_NUM_THREADS */
309 1, /* OMP_CLAUSE_SCHEDULE */
310 0, /* OMP_CLAUSE_NOWAIT */
311 1, /* OMP_CLAUSE_ORDERED */
312 0, /* OMP_CLAUSE_DEFAULT */
313 3, /* OMP_CLAUSE_COLLAPSE */
314 0, /* OMP_CLAUSE_UNTIED */
315 1, /* OMP_CLAUSE_FINAL */
316 0, /* OMP_CLAUSE_MERGEABLE */
317 1, /* OMP_CLAUSE_DEVICE */
318 1, /* OMP_CLAUSE_DIST_SCHEDULE */
319 0, /* OMP_CLAUSE_INBRANCH */
320 0, /* OMP_CLAUSE_NOTINBRANCH */
321 2, /* OMP_CLAUSE_NUM_TEAMS */
322 1, /* OMP_CLAUSE_THREAD_LIMIT */
323 0, /* OMP_CLAUSE_PROC_BIND */
324 1, /* OMP_CLAUSE_SAFELEN */
325 1, /* OMP_CLAUSE_SIMDLEN */
326 0, /* OMP_CLAUSE_DEVICE_TYPE */
327 0, /* OMP_CLAUSE_FOR */
328 0, /* OMP_CLAUSE_PARALLEL */
329 0, /* OMP_CLAUSE_SECTIONS */
330 0, /* OMP_CLAUSE_TASKGROUP */
331 1, /* OMP_CLAUSE_PRIORITY */
332 1, /* OMP_CLAUSE_GRAINSIZE */
333 1, /* OMP_CLAUSE_NUM_TASKS */
334 0, /* OMP_CLAUSE_NOGROUP */
335 0, /* OMP_CLAUSE_THREADS */
336 0, /* OMP_CLAUSE_SIMD */
337 1, /* OMP_CLAUSE_HINT */
338 0, /* OMP_CLAUSE_DEFAULTMAP */
339 0, /* OMP_CLAUSE_ORDER */
340 0, /* OMP_CLAUSE_BIND */
341 1, /* OMP_CLAUSE_FILTER */
342 1, /* OMP_CLAUSE__SIMDUID_ */
343 0, /* OMP_CLAUSE__SIMT_ */
344 0, /* OMP_CLAUSE_INDEPENDENT */
345 1, /* OMP_CLAUSE_WORKER */
346 1, /* OMP_CLAUSE_VECTOR */
347 1, /* OMP_CLAUSE_NUM_GANGS */
348 1, /* OMP_CLAUSE_NUM_WORKERS */
349 1, /* OMP_CLAUSE_VECTOR_LENGTH */
350 3, /* OMP_CLAUSE_TILE */
351 0, /* OMP_CLAUSE_IF_PRESENT */
352 0, /* OMP_CLAUSE_FINALIZE */
353 0, /* OMP_CLAUSE_NOHOST */
356 const char * const omp_clause_code_name[] =
358 "error_clause",
359 "private",
360 "shared",
361 "firstprivate",
362 "lastprivate",
363 "reduction",
364 "task_reduction",
365 "in_reduction",
366 "copyin",
367 "copyprivate",
368 "linear",
369 "affinity",
370 "aligned",
371 "allocate",
372 "depend",
373 "nontemporal",
374 "uniform",
375 "enter",
376 "link",
377 "detach",
378 "use_device_ptr",
379 "use_device_addr",
380 "is_device_ptr",
381 "inclusive",
382 "exclusive",
383 "from",
384 "to",
385 "map",
386 "has_device_addr",
387 "_cache_",
388 "gang",
389 "async",
390 "wait",
391 "auto",
392 "seq",
393 "_looptemp_",
394 "_reductemp_",
395 "_condtemp_",
396 "_scantemp_",
397 "if",
398 "num_threads",
399 "schedule",
400 "nowait",
401 "ordered",
402 "default",
403 "collapse",
404 "untied",
405 "final",
406 "mergeable",
407 "device",
408 "dist_schedule",
409 "inbranch",
410 "notinbranch",
411 "num_teams",
412 "thread_limit",
413 "proc_bind",
414 "safelen",
415 "simdlen",
416 "device_type",
417 "for",
418 "parallel",
419 "sections",
420 "taskgroup",
421 "priority",
422 "grainsize",
423 "num_tasks",
424 "nogroup",
425 "threads",
426 "simd",
427 "hint",
428 "defaultmap",
429 "order",
430 "bind",
431 "filter",
432 "_simduid_",
433 "_simt_",
434 "independent",
435 "worker",
436 "vector",
437 "num_gangs",
438 "num_workers",
439 "vector_length",
440 "tile",
441 "if_present",
442 "finalize",
443 "nohost",
446 /* Unless specific to OpenACC, we tend to internally maintain OpenMP-centric
447 clause names, but for use in diagnostics etc. would like to use the "user"
448 clause names. */
450 const char *
451 user_omp_clause_code_name (tree clause, bool oacc)
453 /* For OpenACC, the 'OMP_CLAUSE_MAP_KIND' of an 'OMP_CLAUSE_MAP' is used to
454 distinguish clauses as seen by the user. See also where front ends do
455 'build_omp_clause' with 'OMP_CLAUSE_MAP'. */
456 if (oacc && OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP)
457 switch (OMP_CLAUSE_MAP_KIND (clause))
459 case GOMP_MAP_FORCE_ALLOC:
460 case GOMP_MAP_ALLOC: return "create";
461 case GOMP_MAP_FORCE_TO:
462 case GOMP_MAP_TO: return "copyin";
463 case GOMP_MAP_FORCE_FROM:
464 case GOMP_MAP_FROM: return "copyout";
465 case GOMP_MAP_FORCE_TOFROM:
466 case GOMP_MAP_TOFROM: return "copy";
467 case GOMP_MAP_RELEASE: return "delete";
468 case GOMP_MAP_FORCE_PRESENT: return "present";
469 case GOMP_MAP_ATTACH: return "attach";
470 case GOMP_MAP_FORCE_DETACH:
471 case GOMP_MAP_DETACH: return "detach";
472 case GOMP_MAP_DEVICE_RESIDENT: return "device_resident";
473 case GOMP_MAP_LINK: return "link";
474 case GOMP_MAP_FORCE_DEVICEPTR: return "deviceptr";
475 default: break;
478 return omp_clause_code_name[OMP_CLAUSE_CODE (clause)];
482 /* Return the tree node structure used by tree code CODE. */
484 static inline enum tree_node_structure_enum
485 tree_node_structure_for_code (enum tree_code code)
487 switch (TREE_CODE_CLASS (code))
489 case tcc_declaration:
490 switch (code)
492 case CONST_DECL: return TS_CONST_DECL;
493 case DEBUG_EXPR_DECL: return TS_DECL_WRTL;
494 case FIELD_DECL: return TS_FIELD_DECL;
495 case FUNCTION_DECL: return TS_FUNCTION_DECL;
496 case LABEL_DECL: return TS_LABEL_DECL;
497 case PARM_DECL: return TS_PARM_DECL;
498 case RESULT_DECL: return TS_RESULT_DECL;
499 case TRANSLATION_UNIT_DECL: return TS_TRANSLATION_UNIT_DECL;
500 case TYPE_DECL: return TS_TYPE_DECL;
501 case VAR_DECL: return TS_VAR_DECL;
502 default: return TS_DECL_NON_COMMON;
505 case tcc_type: return TS_TYPE_NON_COMMON;
507 case tcc_binary:
508 case tcc_comparison:
509 case tcc_expression:
510 case tcc_reference:
511 case tcc_statement:
512 case tcc_unary:
513 case tcc_vl_exp: return TS_EXP;
515 default: /* tcc_constant and tcc_exceptional */
516 break;
519 switch (code)
521 /* tcc_constant cases. */
522 case COMPLEX_CST: return TS_COMPLEX;
523 case FIXED_CST: return TS_FIXED_CST;
524 case INTEGER_CST: return TS_INT_CST;
525 case POLY_INT_CST: return TS_POLY_INT_CST;
526 case REAL_CST: return TS_REAL_CST;
527 case STRING_CST: return TS_STRING;
528 case VECTOR_CST: return TS_VECTOR;
529 case VOID_CST: return TS_TYPED;
531 /* tcc_exceptional cases. */
532 case BLOCK: return TS_BLOCK;
533 case CONSTRUCTOR: return TS_CONSTRUCTOR;
534 case ERROR_MARK: return TS_COMMON;
535 case IDENTIFIER_NODE: return TS_IDENTIFIER;
536 case OMP_CLAUSE: return TS_OMP_CLAUSE;
537 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
538 case PLACEHOLDER_EXPR: return TS_COMMON;
539 case SSA_NAME: return TS_SSA_NAME;
540 case STATEMENT_LIST: return TS_STATEMENT_LIST;
541 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
542 case TREE_BINFO: return TS_BINFO;
543 case TREE_LIST: return TS_LIST;
544 case TREE_VEC: return TS_VEC;
546 default:
547 gcc_unreachable ();
552 /* Initialize tree_contains_struct to describe the hierarchy of tree
553 nodes. */
555 static void
556 initialize_tree_contains_struct (void)
558 unsigned i;
560 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
562 enum tree_code code;
563 enum tree_node_structure_enum ts_code;
565 code = (enum tree_code) i;
566 ts_code = tree_node_structure_for_code (code);
568 /* Mark the TS structure itself. */
569 tree_contains_struct[code][ts_code] = 1;
571 /* Mark all the structures that TS is derived from. */
572 switch (ts_code)
574 case TS_TYPED:
575 case TS_BLOCK:
576 case TS_OPTIMIZATION:
577 case TS_TARGET_OPTION:
578 MARK_TS_BASE (code);
579 break;
581 case TS_COMMON:
582 case TS_INT_CST:
583 case TS_POLY_INT_CST:
584 case TS_REAL_CST:
585 case TS_FIXED_CST:
586 case TS_VECTOR:
587 case TS_STRING:
588 case TS_COMPLEX:
589 case TS_SSA_NAME:
590 case TS_CONSTRUCTOR:
591 case TS_EXP:
592 case TS_STATEMENT_LIST:
593 MARK_TS_TYPED (code);
594 break;
596 case TS_IDENTIFIER:
597 case TS_DECL_MINIMAL:
598 case TS_TYPE_COMMON:
599 case TS_LIST:
600 case TS_VEC:
601 case TS_BINFO:
602 case TS_OMP_CLAUSE:
603 MARK_TS_COMMON (code);
604 break;
606 case TS_TYPE_WITH_LANG_SPECIFIC:
607 MARK_TS_TYPE_COMMON (code);
608 break;
610 case TS_TYPE_NON_COMMON:
611 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
612 break;
614 case TS_DECL_COMMON:
615 MARK_TS_DECL_MINIMAL (code);
616 break;
618 case TS_DECL_WRTL:
619 case TS_CONST_DECL:
620 MARK_TS_DECL_COMMON (code);
621 break;
623 case TS_DECL_NON_COMMON:
624 MARK_TS_DECL_WITH_VIS (code);
625 break;
627 case TS_DECL_WITH_VIS:
628 case TS_PARM_DECL:
629 case TS_LABEL_DECL:
630 case TS_RESULT_DECL:
631 MARK_TS_DECL_WRTL (code);
632 break;
634 case TS_FIELD_DECL:
635 MARK_TS_DECL_COMMON (code);
636 break;
638 case TS_VAR_DECL:
639 MARK_TS_DECL_WITH_VIS (code);
640 break;
642 case TS_TYPE_DECL:
643 case TS_FUNCTION_DECL:
644 MARK_TS_DECL_NON_COMMON (code);
645 break;
647 case TS_TRANSLATION_UNIT_DECL:
648 MARK_TS_DECL_COMMON (code);
649 break;
651 default:
652 gcc_unreachable ();
656 /* Basic consistency checks for attributes used in fold. */
657 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
658 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
659 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
660 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
661 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
662 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
663 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
664 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
665 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
666 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
667 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
668 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
669 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
670 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
671 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
672 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
673 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
674 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
675 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
676 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
677 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
678 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
679 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
680 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
681 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
682 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
683 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
684 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
685 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
686 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
687 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
688 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
689 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
690 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
691 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
692 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
693 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
694 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
695 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_MINIMAL]);
696 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_COMMON]);
700 /* Init tree.cc. */
702 void
703 init_ttree (void)
705 /* Initialize the hash table of types. */
706 type_hash_table
707 = hash_table<type_cache_hasher>::create_ggc (TYPE_HASH_INITIAL_SIZE);
709 debug_expr_for_decl
710 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
712 value_expr_for_decl
713 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
715 int_cst_hash_table = hash_table<int_cst_hasher>::create_ggc (1024);
717 poly_int_cst_hash_table = hash_table<poly_int_cst_hasher>::create_ggc (64);
719 int_cst_node = make_int_cst (1, 1);
721 cl_option_hash_table = hash_table<cl_option_hasher>::create_ggc (64);
723 cl_optimization_node = make_node (OPTIMIZATION_NODE);
724 cl_target_option_node = make_node (TARGET_OPTION_NODE);
726 /* Initialize the tree_contains_struct array. */
727 initialize_tree_contains_struct ();
728 lang_hooks.init_ts ();
732 /* The name of the object as the assembler will see it (but before any
733 translations made by ASM_OUTPUT_LABELREF). Often this is the same
734 as DECL_NAME. It is an IDENTIFIER_NODE. */
735 tree
736 decl_assembler_name (tree decl)
738 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
739 lang_hooks.set_decl_assembler_name (decl);
740 return DECL_ASSEMBLER_NAME_RAW (decl);
743 /* The DECL_ASSEMBLER_NAME_RAW of DECL is being explicitly set to NAME
744 (either of which may be NULL). Inform the FE, if this changes the
745 name. */
747 void
748 overwrite_decl_assembler_name (tree decl, tree name)
750 if (DECL_ASSEMBLER_NAME_RAW (decl) != name)
751 lang_hooks.overwrite_decl_assembler_name (decl, name);
754 /* Return true if DECL may need an assembler name to be set. */
756 static inline bool
757 need_assembler_name_p (tree decl)
759 /* We use DECL_ASSEMBLER_NAME to hold mangled type names for One Definition
760 Rule merging. This makes type_odr_p to return true on those types during
761 LTO and by comparing the mangled name, we can say what types are intended
762 to be equivalent across compilation unit.
764 We do not store names of type_in_anonymous_namespace_p.
766 Record, union and enumeration type have linkage that allows use
767 to check type_in_anonymous_namespace_p. We do not mangle compound types
768 that always can be compared structurally.
770 Similarly for builtin types, we compare properties of their main variant.
771 A special case are integer types where mangling do make differences
772 between char/signed char/unsigned char etc. Storing name for these makes
773 e.g. -fno-signed-char/-fsigned-char mismatches to be handled well.
774 See cp/mangle.cc:write_builtin_type for details. */
776 if (TREE_CODE (decl) == TYPE_DECL)
778 if (DECL_NAME (decl)
779 && decl == TYPE_NAME (TREE_TYPE (decl))
780 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TREE_TYPE (decl)
781 && !TYPE_ARTIFICIAL (TREE_TYPE (decl))
782 && ((TREE_CODE (TREE_TYPE (decl)) != RECORD_TYPE
783 && TREE_CODE (TREE_TYPE (decl)) != UNION_TYPE)
784 || TYPE_CXX_ODR_P (TREE_TYPE (decl)))
785 && (type_with_linkage_p (TREE_TYPE (decl))
786 || TREE_CODE (TREE_TYPE (decl)) == INTEGER_TYPE)
787 && !variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
788 return !DECL_ASSEMBLER_NAME_SET_P (decl);
789 return false;
791 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
792 if (!VAR_OR_FUNCTION_DECL_P (decl))
793 return false;
795 /* If DECL already has its assembler name set, it does not need a
796 new one. */
797 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
798 || DECL_ASSEMBLER_NAME_SET_P (decl))
799 return false;
801 /* Abstract decls do not need an assembler name. */
802 if (DECL_ABSTRACT_P (decl))
803 return false;
805 /* For VAR_DECLs, only static, public and external symbols need an
806 assembler name. */
807 if (VAR_P (decl)
808 && !TREE_STATIC (decl)
809 && !TREE_PUBLIC (decl)
810 && !DECL_EXTERNAL (decl))
811 return false;
813 if (TREE_CODE (decl) == FUNCTION_DECL)
815 /* Do not set assembler name on builtins. Allow RTL expansion to
816 decide whether to expand inline or via a regular call. */
817 if (fndecl_built_in_p (decl)
818 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
819 return false;
821 /* Functions represented in the callgraph need an assembler name. */
822 if (cgraph_node::get (decl) != NULL)
823 return true;
825 /* Unused and not public functions don't need an assembler name. */
826 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
827 return false;
830 return true;
833 /* If T needs an assembler name, have one created for it. */
835 void
836 assign_assembler_name_if_needed (tree t)
838 if (need_assembler_name_p (t))
840 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
841 diagnostics that use input_location to show locus
842 information. The problem here is that, at this point,
843 input_location is generally anchored to the end of the file
844 (since the parser is long gone), so we don't have a good
845 position to pin it to.
847 To alleviate this problem, this uses the location of T's
848 declaration. Examples of this are
849 testsuite/g++.dg/template/cond2.C and
850 testsuite/g++.dg/template/pr35240.C. */
851 location_t saved_location = input_location;
852 input_location = DECL_SOURCE_LOCATION (t);
854 decl_assembler_name (t);
856 input_location = saved_location;
860 /* When the target supports COMDAT groups, this indicates which group the
861 DECL is associated with. This can be either an IDENTIFIER_NODE or a
862 decl, in which case its DECL_ASSEMBLER_NAME identifies the group. */
863 tree
864 decl_comdat_group (const_tree node)
866 struct symtab_node *snode = symtab_node::get (node);
867 if (!snode)
868 return NULL;
869 return snode->get_comdat_group ();
872 /* Likewise, but make sure it's been reduced to an IDENTIFIER_NODE. */
873 tree
874 decl_comdat_group_id (const_tree node)
876 struct symtab_node *snode = symtab_node::get (node);
877 if (!snode)
878 return NULL;
879 return snode->get_comdat_group_id ();
882 /* When the target supports named section, return its name as IDENTIFIER_NODE
883 or NULL if it is in no section. */
884 const char *
885 decl_section_name (const_tree node)
887 struct symtab_node *snode = symtab_node::get (node);
888 if (!snode)
889 return NULL;
890 return snode->get_section ();
893 /* Set section name of NODE to VALUE (that is expected to be
894 identifier node) */
895 void
896 set_decl_section_name (tree node, const char *value)
898 struct symtab_node *snode;
900 if (value == NULL)
902 snode = symtab_node::get (node);
903 if (!snode)
904 return;
906 else if (VAR_P (node))
907 snode = varpool_node::get_create (node);
908 else
909 snode = cgraph_node::get_create (node);
910 snode->set_section (value);
913 /* Set section name of NODE to match the section name of OTHER.
915 set_decl_section_name (decl, other) is equivalent to
916 set_decl_section_name (decl, DECL_SECTION_NAME (other)), but possibly more
917 efficient. */
918 void
919 set_decl_section_name (tree decl, const_tree other)
921 struct symtab_node *other_node = symtab_node::get (other);
922 if (other_node)
924 struct symtab_node *decl_node;
925 if (VAR_P (decl))
926 decl_node = varpool_node::get_create (decl);
927 else
928 decl_node = cgraph_node::get_create (decl);
929 decl_node->set_section (*other_node);
931 else
933 struct symtab_node *decl_node = symtab_node::get (decl);
934 if (!decl_node)
935 return;
936 decl_node->set_section (NULL);
940 /* Return TLS model of a variable NODE. */
941 enum tls_model
942 decl_tls_model (const_tree node)
944 struct varpool_node *snode = varpool_node::get (node);
945 if (!snode)
946 return TLS_MODEL_NONE;
947 return snode->tls_model;
950 /* Set TLS model of variable NODE to MODEL. */
951 void
952 set_decl_tls_model (tree node, enum tls_model model)
954 struct varpool_node *vnode;
956 if (model == TLS_MODEL_NONE)
958 vnode = varpool_node::get (node);
959 if (!vnode)
960 return;
962 else
963 vnode = varpool_node::get_create (node);
964 vnode->tls_model = model;
967 /* Compute the number of bytes occupied by a tree with code CODE.
968 This function cannot be used for nodes that have variable sizes,
969 including TREE_VEC, INTEGER_CST, STRING_CST, and CALL_EXPR. */
970 size_t
971 tree_code_size (enum tree_code code)
973 switch (TREE_CODE_CLASS (code))
975 case tcc_declaration: /* A decl node */
976 switch (code)
978 case FIELD_DECL: return sizeof (tree_field_decl);
979 case PARM_DECL: return sizeof (tree_parm_decl);
980 case VAR_DECL: return sizeof (tree_var_decl);
981 case LABEL_DECL: return sizeof (tree_label_decl);
982 case RESULT_DECL: return sizeof (tree_result_decl);
983 case CONST_DECL: return sizeof (tree_const_decl);
984 case TYPE_DECL: return sizeof (tree_type_decl);
985 case FUNCTION_DECL: return sizeof (tree_function_decl);
986 case DEBUG_EXPR_DECL: return sizeof (tree_decl_with_rtl);
987 case TRANSLATION_UNIT_DECL: return sizeof (tree_translation_unit_decl);
988 case NAMESPACE_DECL:
989 case IMPORTED_DECL:
990 case NAMELIST_DECL: return sizeof (tree_decl_non_common);
991 default:
992 gcc_checking_assert (code >= NUM_TREE_CODES);
993 return lang_hooks.tree_size (code);
996 case tcc_type: /* a type node */
997 switch (code)
999 case OFFSET_TYPE:
1000 case ENUMERAL_TYPE:
1001 case BOOLEAN_TYPE:
1002 case INTEGER_TYPE:
1003 case REAL_TYPE:
1004 case OPAQUE_TYPE:
1005 case POINTER_TYPE:
1006 case REFERENCE_TYPE:
1007 case NULLPTR_TYPE:
1008 case FIXED_POINT_TYPE:
1009 case COMPLEX_TYPE:
1010 case VECTOR_TYPE:
1011 case ARRAY_TYPE:
1012 case RECORD_TYPE:
1013 case UNION_TYPE:
1014 case QUAL_UNION_TYPE:
1015 case VOID_TYPE:
1016 case FUNCTION_TYPE:
1017 case METHOD_TYPE:
1018 case LANG_TYPE: return sizeof (tree_type_non_common);
1019 default:
1020 gcc_checking_assert (code >= NUM_TREE_CODES);
1021 return lang_hooks.tree_size (code);
1024 case tcc_reference: /* a reference */
1025 case tcc_expression: /* an expression */
1026 case tcc_statement: /* an expression with side effects */
1027 case tcc_comparison: /* a comparison expression */
1028 case tcc_unary: /* a unary arithmetic expression */
1029 case tcc_binary: /* a binary arithmetic expression */
1030 return (sizeof (struct tree_exp)
1031 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
1033 case tcc_constant: /* a constant */
1034 switch (code)
1036 case VOID_CST: return sizeof (tree_typed);
1037 case INTEGER_CST: gcc_unreachable ();
1038 case POLY_INT_CST: return sizeof (tree_poly_int_cst);
1039 case REAL_CST: return sizeof (tree_real_cst);
1040 case FIXED_CST: return sizeof (tree_fixed_cst);
1041 case COMPLEX_CST: return sizeof (tree_complex);
1042 case VECTOR_CST: gcc_unreachable ();
1043 case STRING_CST: gcc_unreachable ();
1044 default:
1045 gcc_checking_assert (code >= NUM_TREE_CODES);
1046 return lang_hooks.tree_size (code);
1049 case tcc_exceptional: /* something random, like an identifier. */
1050 switch (code)
1052 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
1053 case TREE_LIST: return sizeof (tree_list);
1055 case ERROR_MARK:
1056 case PLACEHOLDER_EXPR: return sizeof (tree_common);
1058 case TREE_VEC: gcc_unreachable ();
1059 case OMP_CLAUSE: gcc_unreachable ();
1061 case SSA_NAME: return sizeof (tree_ssa_name);
1063 case STATEMENT_LIST: return sizeof (tree_statement_list);
1064 case BLOCK: return sizeof (struct tree_block);
1065 case CONSTRUCTOR: return sizeof (tree_constructor);
1066 case OPTIMIZATION_NODE: return sizeof (tree_optimization_option);
1067 case TARGET_OPTION_NODE: return sizeof (tree_target_option);
1069 default:
1070 gcc_checking_assert (code >= NUM_TREE_CODES);
1071 return lang_hooks.tree_size (code);
1074 default:
1075 gcc_unreachable ();
1079 /* Compute the number of bytes occupied by NODE. This routine only
1080 looks at TREE_CODE, except for those nodes that have variable sizes. */
1081 size_t
1082 tree_size (const_tree node)
1084 const enum tree_code code = TREE_CODE (node);
1085 switch (code)
1087 case INTEGER_CST:
1088 return (sizeof (struct tree_int_cst)
1089 + (TREE_INT_CST_EXT_NUNITS (node) - 1) * sizeof (HOST_WIDE_INT));
1091 case TREE_BINFO:
1092 return (offsetof (struct tree_binfo, base_binfos)
1093 + vec<tree, va_gc>
1094 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
1096 case TREE_VEC:
1097 return (sizeof (struct tree_vec)
1098 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
1100 case VECTOR_CST:
1101 return (sizeof (struct tree_vector)
1102 + (vector_cst_encoded_nelts (node) - 1) * sizeof (tree));
1104 case STRING_CST:
1105 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
1107 case OMP_CLAUSE:
1108 return (sizeof (struct tree_omp_clause)
1109 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
1110 * sizeof (tree));
1112 default:
1113 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
1114 return (sizeof (struct tree_exp)
1115 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
1116 else
1117 return tree_code_size (code);
1121 /* Return tree node kind based on tree CODE. */
1123 static tree_node_kind
1124 get_stats_node_kind (enum tree_code code)
1126 enum tree_code_class type = TREE_CODE_CLASS (code);
1128 switch (type)
1130 case tcc_declaration: /* A decl node */
1131 return d_kind;
1132 case tcc_type: /* a type node */
1133 return t_kind;
1134 case tcc_statement: /* an expression with side effects */
1135 return s_kind;
1136 case tcc_reference: /* a reference */
1137 return r_kind;
1138 case tcc_expression: /* an expression */
1139 case tcc_comparison: /* a comparison expression */
1140 case tcc_unary: /* a unary arithmetic expression */
1141 case tcc_binary: /* a binary arithmetic expression */
1142 return e_kind;
1143 case tcc_constant: /* a constant */
1144 return c_kind;
1145 case tcc_exceptional: /* something random, like an identifier. */
1146 switch (code)
1148 case IDENTIFIER_NODE:
1149 return id_kind;
1150 case TREE_VEC:
1151 return vec_kind;
1152 case TREE_BINFO:
1153 return binfo_kind;
1154 case SSA_NAME:
1155 return ssa_name_kind;
1156 case BLOCK:
1157 return b_kind;
1158 case CONSTRUCTOR:
1159 return constr_kind;
1160 case OMP_CLAUSE:
1161 return omp_clause_kind;
1162 default:
1163 return x_kind;
1165 break;
1166 case tcc_vl_exp:
1167 return e_kind;
1168 default:
1169 gcc_unreachable ();
1173 /* Record interesting allocation statistics for a tree node with CODE
1174 and LENGTH. */
1176 static void
1177 record_node_allocation_statistics (enum tree_code code, size_t length)
1179 if (!GATHER_STATISTICS)
1180 return;
1182 tree_node_kind kind = get_stats_node_kind (code);
1184 tree_code_counts[(int) code]++;
1185 tree_node_counts[(int) kind]++;
1186 tree_node_sizes[(int) kind] += length;
1189 /* Allocate and return a new UID from the DECL_UID namespace. */
1192 allocate_decl_uid (void)
1194 return next_decl_uid++;
1197 /* Return a newly allocated node of code CODE. For decl and type
1198 nodes, some other fields are initialized. The rest of the node is
1199 initialized to zero. This function cannot be used for TREE_VEC,
1200 INTEGER_CST or OMP_CLAUSE nodes, which is enforced by asserts in
1201 tree_code_size.
1203 Achoo! I got a code in the node. */
1205 tree
1206 make_node (enum tree_code code MEM_STAT_DECL)
1208 tree t;
1209 enum tree_code_class type = TREE_CODE_CLASS (code);
1210 size_t length = tree_code_size (code);
1212 record_node_allocation_statistics (code, length);
1214 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1215 TREE_SET_CODE (t, code);
1217 switch (type)
1219 case tcc_statement:
1220 if (code != DEBUG_BEGIN_STMT)
1221 TREE_SIDE_EFFECTS (t) = 1;
1222 break;
1224 case tcc_declaration:
1225 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1227 if (code == FUNCTION_DECL)
1229 SET_DECL_ALIGN (t, FUNCTION_ALIGNMENT (FUNCTION_BOUNDARY));
1230 SET_DECL_MODE (t, FUNCTION_MODE);
1232 else
1233 SET_DECL_ALIGN (t, 1);
1235 DECL_SOURCE_LOCATION (t) = input_location;
1236 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
1237 DECL_UID (t) = --next_debug_decl_uid;
1238 else
1240 DECL_UID (t) = allocate_decl_uid ();
1241 SET_DECL_PT_UID (t, -1);
1243 if (TREE_CODE (t) == LABEL_DECL)
1244 LABEL_DECL_UID (t) = -1;
1246 break;
1248 case tcc_type:
1249 TYPE_UID (t) = next_type_uid++;
1250 SET_TYPE_ALIGN (t, BITS_PER_UNIT);
1251 TYPE_USER_ALIGN (t) = 0;
1252 TYPE_MAIN_VARIANT (t) = t;
1253 TYPE_CANONICAL (t) = t;
1255 /* Default to no attributes for type, but let target change that. */
1256 TYPE_ATTRIBUTES (t) = NULL_TREE;
1257 targetm.set_default_type_attributes (t);
1259 /* We have not yet computed the alias set for this type. */
1260 TYPE_ALIAS_SET (t) = -1;
1261 break;
1263 case tcc_constant:
1264 TREE_CONSTANT (t) = 1;
1265 break;
1267 case tcc_expression:
1268 switch (code)
1270 case INIT_EXPR:
1271 case MODIFY_EXPR:
1272 case VA_ARG_EXPR:
1273 case PREDECREMENT_EXPR:
1274 case PREINCREMENT_EXPR:
1275 case POSTDECREMENT_EXPR:
1276 case POSTINCREMENT_EXPR:
1277 /* All of these have side-effects, no matter what their
1278 operands are. */
1279 TREE_SIDE_EFFECTS (t) = 1;
1280 break;
1282 default:
1283 break;
1285 break;
1287 case tcc_exceptional:
1288 switch (code)
1290 case TARGET_OPTION_NODE:
1291 TREE_TARGET_OPTION(t)
1292 = ggc_cleared_alloc<struct cl_target_option> ();
1293 break;
1295 case OPTIMIZATION_NODE:
1296 TREE_OPTIMIZATION (t)
1297 = ggc_cleared_alloc<struct cl_optimization> ();
1298 break;
1300 default:
1301 break;
1303 break;
1305 default:
1306 /* Other classes need no special treatment. */
1307 break;
1310 return t;
1313 /* Free tree node. */
1315 void
1316 free_node (tree node)
1318 enum tree_code code = TREE_CODE (node);
1319 if (GATHER_STATISTICS)
1321 enum tree_node_kind kind = get_stats_node_kind (code);
1323 gcc_checking_assert (tree_code_counts[(int) TREE_CODE (node)] != 0);
1324 gcc_checking_assert (tree_node_counts[(int) kind] != 0);
1325 gcc_checking_assert (tree_node_sizes[(int) kind] >= tree_size (node));
1327 tree_code_counts[(int) TREE_CODE (node)]--;
1328 tree_node_counts[(int) kind]--;
1329 tree_node_sizes[(int) kind] -= tree_size (node);
1331 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1332 vec_free (CONSTRUCTOR_ELTS (node));
1333 else if (code == BLOCK)
1334 vec_free (BLOCK_NONLOCALIZED_VARS (node));
1335 else if (code == TREE_BINFO)
1336 vec_free (BINFO_BASE_ACCESSES (node));
1337 else if (code == OPTIMIZATION_NODE)
1338 cl_optimization_option_free (TREE_OPTIMIZATION (node));
1339 else if (code == TARGET_OPTION_NODE)
1340 cl_target_option_free (TREE_TARGET_OPTION (node));
1341 ggc_free (node);
1344 /* Return a new node with the same contents as NODE except that its
1345 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
1347 tree
1348 copy_node (tree node MEM_STAT_DECL)
1350 tree t;
1351 enum tree_code code = TREE_CODE (node);
1352 size_t length;
1354 gcc_assert (code != STATEMENT_LIST);
1356 length = tree_size (node);
1357 record_node_allocation_statistics (code, length);
1358 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1359 memcpy (t, node, length);
1361 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
1362 TREE_CHAIN (t) = 0;
1363 TREE_ASM_WRITTEN (t) = 0;
1364 TREE_VISITED (t) = 0;
1366 if (TREE_CODE_CLASS (code) == tcc_declaration)
1368 if (code == DEBUG_EXPR_DECL)
1369 DECL_UID (t) = --next_debug_decl_uid;
1370 else
1372 DECL_UID (t) = allocate_decl_uid ();
1373 if (DECL_PT_UID_SET_P (node))
1374 SET_DECL_PT_UID (t, DECL_PT_UID (node));
1376 if ((TREE_CODE (node) == PARM_DECL || VAR_P (node))
1377 && DECL_HAS_VALUE_EXPR_P (node))
1379 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
1380 DECL_HAS_VALUE_EXPR_P (t) = 1;
1382 /* DECL_DEBUG_EXPR is copied explicitly by callers. */
1383 if (VAR_P (node))
1385 DECL_HAS_DEBUG_EXPR_P (t) = 0;
1386 t->decl_with_vis.symtab_node = NULL;
1388 if (VAR_P (node) && DECL_HAS_INIT_PRIORITY_P (node))
1390 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
1391 DECL_HAS_INIT_PRIORITY_P (t) = 1;
1393 if (TREE_CODE (node) == FUNCTION_DECL)
1395 DECL_STRUCT_FUNCTION (t) = NULL;
1396 t->decl_with_vis.symtab_node = NULL;
1399 else if (TREE_CODE_CLASS (code) == tcc_type)
1401 TYPE_UID (t) = next_type_uid++;
1402 /* The following is so that the debug code for
1403 the copy is different from the original type.
1404 The two statements usually duplicate each other
1405 (because they clear fields of the same union),
1406 but the optimizer should catch that. */
1407 TYPE_SYMTAB_ADDRESS (t) = 0;
1408 TYPE_SYMTAB_DIE (t) = 0;
1410 /* Do not copy the values cache. */
1411 if (TYPE_CACHED_VALUES_P (t))
1413 TYPE_CACHED_VALUES_P (t) = 0;
1414 TYPE_CACHED_VALUES (t) = NULL_TREE;
1417 else if (code == TARGET_OPTION_NODE)
1419 TREE_TARGET_OPTION (t) = ggc_alloc<struct cl_target_option>();
1420 memcpy (TREE_TARGET_OPTION (t), TREE_TARGET_OPTION (node),
1421 sizeof (struct cl_target_option));
1423 else if (code == OPTIMIZATION_NODE)
1425 TREE_OPTIMIZATION (t) = ggc_alloc<struct cl_optimization>();
1426 memcpy (TREE_OPTIMIZATION (t), TREE_OPTIMIZATION (node),
1427 sizeof (struct cl_optimization));
1430 return t;
1433 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1434 For example, this can copy a list made of TREE_LIST nodes. */
1436 tree
1437 copy_list (tree list)
1439 tree head;
1440 tree prev, next;
1442 if (list == 0)
1443 return 0;
1445 head = prev = copy_node (list);
1446 next = TREE_CHAIN (list);
1447 while (next)
1449 TREE_CHAIN (prev) = copy_node (next);
1450 prev = TREE_CHAIN (prev);
1451 next = TREE_CHAIN (next);
1453 return head;
1457 /* Return the value that TREE_INT_CST_EXT_NUNITS should have for an
1458 INTEGER_CST with value CST and type TYPE. */
1460 static unsigned int
1461 get_int_cst_ext_nunits (tree type, const wide_int &cst)
1463 gcc_checking_assert (cst.get_precision () == TYPE_PRECISION (type));
1464 /* We need extra HWIs if CST is an unsigned integer with its
1465 upper bit set. */
1466 if (TYPE_UNSIGNED (type) && wi::neg_p (cst))
1467 return cst.get_precision () / HOST_BITS_PER_WIDE_INT + 1;
1468 return cst.get_len ();
1471 /* Return a new INTEGER_CST with value CST and type TYPE. */
1473 static tree
1474 build_new_int_cst (tree type, const wide_int &cst)
1476 unsigned int len = cst.get_len ();
1477 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1478 tree nt = make_int_cst (len, ext_len);
1480 if (len < ext_len)
1482 --ext_len;
1483 TREE_INT_CST_ELT (nt, ext_len)
1484 = zext_hwi (-1, cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1485 for (unsigned int i = len; i < ext_len; ++i)
1486 TREE_INT_CST_ELT (nt, i) = -1;
1488 else if (TYPE_UNSIGNED (type)
1489 && cst.get_precision () < len * HOST_BITS_PER_WIDE_INT)
1491 len--;
1492 TREE_INT_CST_ELT (nt, len)
1493 = zext_hwi (cst.elt (len),
1494 cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1497 for (unsigned int i = 0; i < len; i++)
1498 TREE_INT_CST_ELT (nt, i) = cst.elt (i);
1499 TREE_TYPE (nt) = type;
1500 return nt;
1503 /* Return a new POLY_INT_CST with coefficients COEFFS and type TYPE. */
1505 static tree
1506 build_new_poly_int_cst (tree type, tree (&coeffs)[NUM_POLY_INT_COEFFS]
1507 CXX_MEM_STAT_INFO)
1509 size_t length = sizeof (struct tree_poly_int_cst);
1510 record_node_allocation_statistics (POLY_INT_CST, length);
1512 tree t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1514 TREE_SET_CODE (t, POLY_INT_CST);
1515 TREE_CONSTANT (t) = 1;
1516 TREE_TYPE (t) = type;
1517 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1518 POLY_INT_CST_COEFF (t, i) = coeffs[i];
1519 return t;
1522 /* Create a constant tree that contains CST sign-extended to TYPE. */
1524 tree
1525 build_int_cst (tree type, poly_int64 cst)
1527 /* Support legacy code. */
1528 if (!type)
1529 type = integer_type_node;
1531 return wide_int_to_tree (type, wi::shwi (cst, TYPE_PRECISION (type)));
1534 /* Create a constant tree that contains CST zero-extended to TYPE. */
1536 tree
1537 build_int_cstu (tree type, poly_uint64 cst)
1539 return wide_int_to_tree (type, wi::uhwi (cst, TYPE_PRECISION (type)));
1542 /* Create a constant tree that contains CST sign-extended to TYPE. */
1544 tree
1545 build_int_cst_type (tree type, poly_int64 cst)
1547 gcc_assert (type);
1548 return wide_int_to_tree (type, wi::shwi (cst, TYPE_PRECISION (type)));
1551 /* Constructs tree in type TYPE from with value given by CST. Signedness
1552 of CST is assumed to be the same as the signedness of TYPE. */
1554 tree
1555 double_int_to_tree (tree type, double_int cst)
1557 return wide_int_to_tree (type, widest_int::from (cst, TYPE_SIGN (type)));
1560 /* We force the wide_int CST to the range of the type TYPE by sign or
1561 zero extending it. OVERFLOWABLE indicates if we are interested in
1562 overflow of the value, when >0 we are only interested in signed
1563 overflow, for <0 we are interested in any overflow. OVERFLOWED
1564 indicates whether overflow has already occurred. CONST_OVERFLOWED
1565 indicates whether constant overflow has already occurred. We force
1566 T's value to be within range of T's type (by setting to 0 or 1 all
1567 the bits outside the type's range). We set TREE_OVERFLOWED if,
1568 OVERFLOWED is nonzero,
1569 or OVERFLOWABLE is >0 and signed overflow occurs
1570 or OVERFLOWABLE is <0 and any overflow occurs
1571 We return a new tree node for the extended wide_int. The node
1572 is shared if no overflow flags are set. */
1575 tree
1576 force_fit_type (tree type, const poly_wide_int_ref &cst,
1577 int overflowable, bool overflowed)
1579 signop sign = TYPE_SIGN (type);
1581 /* If we need to set overflow flags, return a new unshared node. */
1582 if (overflowed || !wi::fits_to_tree_p (cst, type))
1584 if (overflowed
1585 || overflowable < 0
1586 || (overflowable > 0 && sign == SIGNED))
1588 poly_wide_int tmp = poly_wide_int::from (cst, TYPE_PRECISION (type),
1589 sign);
1590 tree t;
1591 if (tmp.is_constant ())
1592 t = build_new_int_cst (type, tmp.coeffs[0]);
1593 else
1595 tree coeffs[NUM_POLY_INT_COEFFS];
1596 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1598 coeffs[i] = build_new_int_cst (type, tmp.coeffs[i]);
1599 TREE_OVERFLOW (coeffs[i]) = 1;
1601 t = build_new_poly_int_cst (type, coeffs);
1603 TREE_OVERFLOW (t) = 1;
1604 return t;
1608 /* Else build a shared node. */
1609 return wide_int_to_tree (type, cst);
1612 /* These are the hash table functions for the hash table of INTEGER_CST
1613 nodes of a sizetype. */
1615 /* Return the hash code X, an INTEGER_CST. */
1617 hashval_t
1618 int_cst_hasher::hash (tree x)
1620 const_tree const t = x;
1621 hashval_t code = TYPE_UID (TREE_TYPE (t));
1622 int i;
1624 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
1625 code = iterative_hash_host_wide_int (TREE_INT_CST_ELT(t, i), code);
1627 return code;
1630 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1631 is the same as that given by *Y, which is the same. */
1633 bool
1634 int_cst_hasher::equal (tree x, tree y)
1636 const_tree const xt = x;
1637 const_tree const yt = y;
1639 if (TREE_TYPE (xt) != TREE_TYPE (yt)
1640 || TREE_INT_CST_NUNITS (xt) != TREE_INT_CST_NUNITS (yt)
1641 || TREE_INT_CST_EXT_NUNITS (xt) != TREE_INT_CST_EXT_NUNITS (yt))
1642 return false;
1644 for (int i = 0; i < TREE_INT_CST_NUNITS (xt); i++)
1645 if (TREE_INT_CST_ELT (xt, i) != TREE_INT_CST_ELT (yt, i))
1646 return false;
1648 return true;
1651 /* Cache wide_int CST into the TYPE_CACHED_VALUES cache for TYPE.
1652 SLOT is the slot entry to store it in, and MAX_SLOTS is the maximum
1653 number of slots that can be cached for the type. */
1655 static inline tree
1656 cache_wide_int_in_type_cache (tree type, const wide_int &cst,
1657 int slot, int max_slots)
1659 gcc_checking_assert (slot >= 0);
1660 /* Initialize cache. */
1661 if (!TYPE_CACHED_VALUES_P (type))
1663 TYPE_CACHED_VALUES_P (type) = 1;
1664 TYPE_CACHED_VALUES (type) = make_tree_vec (max_slots);
1666 tree t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), slot);
1667 if (!t)
1669 /* Create a new shared int. */
1670 t = build_new_int_cst (type, cst);
1671 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), slot) = t;
1673 return t;
1676 /* Create an INT_CST node of TYPE and value CST.
1677 The returned node is always shared. For small integers we use a
1678 per-type vector cache, for larger ones we use a single hash table.
1679 The value is extended from its precision according to the sign of
1680 the type to be a multiple of HOST_BITS_PER_WIDE_INT. This defines
1681 the upper bits and ensures that hashing and value equality based
1682 upon the underlying HOST_WIDE_INTs works without masking. */
1684 static tree
1685 wide_int_to_tree_1 (tree type, const wide_int_ref &pcst)
1687 tree t;
1688 int ix = -1;
1689 int limit = 0;
1691 gcc_assert (type);
1692 unsigned int prec = TYPE_PRECISION (type);
1693 signop sgn = TYPE_SIGN (type);
1695 /* Verify that everything is canonical. */
1696 int l = pcst.get_len ();
1697 if (l > 1)
1699 if (pcst.elt (l - 1) == 0)
1700 gcc_checking_assert (pcst.elt (l - 2) < 0);
1701 if (pcst.elt (l - 1) == HOST_WIDE_INT_M1)
1702 gcc_checking_assert (pcst.elt (l - 2) >= 0);
1705 wide_int cst = wide_int::from (pcst, prec, sgn);
1706 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1708 enum tree_code code = TREE_CODE (type);
1709 if (code == POINTER_TYPE || code == REFERENCE_TYPE)
1711 /* Cache NULL pointer and zero bounds. */
1712 if (cst == 0)
1713 ix = 0;
1714 /* Cache upper bounds of pointers. */
1715 else if (cst == wi::max_value (prec, sgn))
1716 ix = 1;
1717 /* Cache 1 which is used for a non-zero range. */
1718 else if (cst == 1)
1719 ix = 2;
1721 if (ix >= 0)
1723 t = cache_wide_int_in_type_cache (type, cst, ix, 3);
1724 /* Make sure no one is clobbering the shared constant. */
1725 gcc_checking_assert (TREE_TYPE (t) == type
1726 && cst == wi::to_wide (t));
1727 return t;
1730 if (ext_len == 1)
1732 /* We just need to store a single HOST_WIDE_INT. */
1733 HOST_WIDE_INT hwi;
1734 if (TYPE_UNSIGNED (type))
1735 hwi = cst.to_uhwi ();
1736 else
1737 hwi = cst.to_shwi ();
1739 switch (code)
1741 case NULLPTR_TYPE:
1742 gcc_assert (hwi == 0);
1743 /* Fallthru. */
1745 case POINTER_TYPE:
1746 case REFERENCE_TYPE:
1747 /* Ignore pointers, as they were already handled above. */
1748 break;
1750 case BOOLEAN_TYPE:
1751 /* Cache false or true. */
1752 limit = 2;
1753 if (IN_RANGE (hwi, 0, 1))
1754 ix = hwi;
1755 break;
1757 case INTEGER_TYPE:
1758 case OFFSET_TYPE:
1759 if (TYPE_SIGN (type) == UNSIGNED)
1761 /* Cache [0, N). */
1762 limit = param_integer_share_limit;
1763 if (IN_RANGE (hwi, 0, param_integer_share_limit - 1))
1764 ix = hwi;
1766 else
1768 /* Cache [-1, N). */
1769 limit = param_integer_share_limit + 1;
1770 if (IN_RANGE (hwi, -1, param_integer_share_limit - 1))
1771 ix = hwi + 1;
1773 break;
1775 case ENUMERAL_TYPE:
1776 break;
1778 default:
1779 gcc_unreachable ();
1782 if (ix >= 0)
1784 t = cache_wide_int_in_type_cache (type, cst, ix, limit);
1785 /* Make sure no one is clobbering the shared constant. */
1786 gcc_checking_assert (TREE_TYPE (t) == type
1787 && TREE_INT_CST_NUNITS (t) == 1
1788 && TREE_INT_CST_OFFSET_NUNITS (t) == 1
1789 && TREE_INT_CST_EXT_NUNITS (t) == 1
1790 && TREE_INT_CST_ELT (t, 0) == hwi);
1791 return t;
1793 else
1795 /* Use the cache of larger shared ints, using int_cst_node as
1796 a temporary. */
1798 TREE_INT_CST_ELT (int_cst_node, 0) = hwi;
1799 TREE_TYPE (int_cst_node) = type;
1801 tree *slot = int_cst_hash_table->find_slot (int_cst_node, INSERT);
1802 t = *slot;
1803 if (!t)
1805 /* Insert this one into the hash table. */
1806 t = int_cst_node;
1807 *slot = t;
1808 /* Make a new node for next time round. */
1809 int_cst_node = make_int_cst (1, 1);
1813 else
1815 /* The value either hashes properly or we drop it on the floor
1816 for the gc to take care of. There will not be enough of them
1817 to worry about. */
1819 tree nt = build_new_int_cst (type, cst);
1820 tree *slot = int_cst_hash_table->find_slot (nt, INSERT);
1821 t = *slot;
1822 if (!t)
1824 /* Insert this one into the hash table. */
1825 t = nt;
1826 *slot = t;
1828 else
1829 ggc_free (nt);
1832 return t;
1835 hashval_t
1836 poly_int_cst_hasher::hash (tree t)
1838 inchash::hash hstate;
1840 hstate.add_int (TYPE_UID (TREE_TYPE (t)));
1841 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1842 hstate.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t, i)));
1844 return hstate.end ();
1847 bool
1848 poly_int_cst_hasher::equal (tree x, const compare_type &y)
1850 if (TREE_TYPE (x) != y.first)
1851 return false;
1852 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1853 if (wi::to_wide (POLY_INT_CST_COEFF (x, i)) != y.second->coeffs[i])
1854 return false;
1855 return true;
1858 /* Build a POLY_INT_CST node with type TYPE and with the elements in VALUES.
1859 The elements must also have type TYPE. */
1861 tree
1862 build_poly_int_cst (tree type, const poly_wide_int_ref &values)
1864 unsigned int prec = TYPE_PRECISION (type);
1865 gcc_assert (prec <= values.coeffs[0].get_precision ());
1866 poly_wide_int c = poly_wide_int::from (values, prec, SIGNED);
1868 inchash::hash h;
1869 h.add_int (TYPE_UID (type));
1870 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1871 h.add_wide_int (c.coeffs[i]);
1872 poly_int_cst_hasher::compare_type comp (type, &c);
1873 tree *slot = poly_int_cst_hash_table->find_slot_with_hash (comp, h.end (),
1874 INSERT);
1875 if (*slot == NULL_TREE)
1877 tree coeffs[NUM_POLY_INT_COEFFS];
1878 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1879 coeffs[i] = wide_int_to_tree_1 (type, c.coeffs[i]);
1880 *slot = build_new_poly_int_cst (type, coeffs);
1882 return *slot;
1885 /* Create a constant tree with value VALUE in type TYPE. */
1887 tree
1888 wide_int_to_tree (tree type, const poly_wide_int_ref &value)
1890 if (value.is_constant ())
1891 return wide_int_to_tree_1 (type, value.coeffs[0]);
1892 return build_poly_int_cst (type, value);
1895 /* Insert INTEGER_CST T into a cache of integer constants. And return
1896 the cached constant (which may or may not be T). If MIGHT_DUPLICATE
1897 is false, and T falls into the type's 'smaller values' range, there
1898 cannot be an existing entry. Otherwise, if MIGHT_DUPLICATE is true,
1899 or the value is large, should an existing entry exist, it is
1900 returned (rather than inserting T). */
1902 tree
1903 cache_integer_cst (tree t, bool might_duplicate ATTRIBUTE_UNUSED)
1905 tree type = TREE_TYPE (t);
1906 int ix = -1;
1907 int limit = 0;
1908 int prec = TYPE_PRECISION (type);
1910 gcc_assert (!TREE_OVERFLOW (t));
1912 /* The caching indices here must match those in
1913 wide_int_to_type_1. */
1914 switch (TREE_CODE (type))
1916 case NULLPTR_TYPE:
1917 gcc_checking_assert (integer_zerop (t));
1918 /* Fallthru. */
1920 case POINTER_TYPE:
1921 case REFERENCE_TYPE:
1923 if (integer_zerop (t))
1924 ix = 0;
1925 else if (integer_onep (t))
1926 ix = 2;
1928 if (ix >= 0)
1929 limit = 3;
1931 break;
1933 case BOOLEAN_TYPE:
1934 /* Cache false or true. */
1935 limit = 2;
1936 if (wi::ltu_p (wi::to_wide (t), 2))
1937 ix = TREE_INT_CST_ELT (t, 0);
1938 break;
1940 case INTEGER_TYPE:
1941 case OFFSET_TYPE:
1942 if (TYPE_UNSIGNED (type))
1944 /* Cache 0..N */
1945 limit = param_integer_share_limit;
1947 /* This is a little hokie, but if the prec is smaller than
1948 what is necessary to hold param_integer_share_limit, then the
1949 obvious test will not get the correct answer. */
1950 if (prec < HOST_BITS_PER_WIDE_INT)
1952 if (tree_to_uhwi (t)
1953 < (unsigned HOST_WIDE_INT) param_integer_share_limit)
1954 ix = tree_to_uhwi (t);
1956 else if (wi::ltu_p (wi::to_wide (t), param_integer_share_limit))
1957 ix = tree_to_uhwi (t);
1959 else
1961 /* Cache -1..N */
1962 limit = param_integer_share_limit + 1;
1964 if (integer_minus_onep (t))
1965 ix = 0;
1966 else if (!wi::neg_p (wi::to_wide (t)))
1968 if (prec < HOST_BITS_PER_WIDE_INT)
1970 if (tree_to_shwi (t) < param_integer_share_limit)
1971 ix = tree_to_shwi (t) + 1;
1973 else if (wi::ltu_p (wi::to_wide (t), param_integer_share_limit))
1974 ix = tree_to_shwi (t) + 1;
1977 break;
1979 case ENUMERAL_TYPE:
1980 /* The slot used by TYPE_CACHED_VALUES is used for the enum
1981 members. */
1982 break;
1984 default:
1985 gcc_unreachable ();
1988 if (ix >= 0)
1990 /* Look for it in the type's vector of small shared ints. */
1991 if (!TYPE_CACHED_VALUES_P (type))
1993 TYPE_CACHED_VALUES_P (type) = 1;
1994 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1997 if (tree r = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix))
1999 gcc_checking_assert (might_duplicate);
2000 t = r;
2002 else
2003 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
2005 else
2007 /* Use the cache of larger shared ints. */
2008 tree *slot = int_cst_hash_table->find_slot (t, INSERT);
2009 if (tree r = *slot)
2011 /* If there is already an entry for the number verify it's the
2012 same value. */
2013 gcc_checking_assert (wi::to_wide (tree (r)) == wi::to_wide (t));
2014 /* And return the cached value. */
2015 t = r;
2017 else
2018 /* Otherwise insert this one into the hash table. */
2019 *slot = t;
2022 return t;
2026 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
2027 and the rest are zeros. */
2029 tree
2030 build_low_bits_mask (tree type, unsigned bits)
2032 gcc_assert (bits <= TYPE_PRECISION (type));
2034 return wide_int_to_tree (type, wi::mask (bits, false,
2035 TYPE_PRECISION (type)));
2038 /* Checks that X is integer constant that can be expressed in (unsigned)
2039 HOST_WIDE_INT without loss of precision. */
2041 bool
2042 cst_and_fits_in_hwi (const_tree x)
2044 return (TREE_CODE (x) == INTEGER_CST
2045 && (tree_fits_shwi_p (x) || tree_fits_uhwi_p (x)));
2048 /* Build a newly constructed VECTOR_CST with the given values of
2049 (VECTOR_CST_)LOG2_NPATTERNS and (VECTOR_CST_)NELTS_PER_PATTERN. */
2051 tree
2052 make_vector (unsigned log2_npatterns,
2053 unsigned int nelts_per_pattern MEM_STAT_DECL)
2055 gcc_assert (IN_RANGE (nelts_per_pattern, 1, 3));
2056 tree t;
2057 unsigned npatterns = 1 << log2_npatterns;
2058 unsigned encoded_nelts = npatterns * nelts_per_pattern;
2059 unsigned length = (sizeof (struct tree_vector)
2060 + (encoded_nelts - 1) * sizeof (tree));
2062 record_node_allocation_statistics (VECTOR_CST, length);
2064 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2066 TREE_SET_CODE (t, VECTOR_CST);
2067 TREE_CONSTANT (t) = 1;
2068 VECTOR_CST_LOG2_NPATTERNS (t) = log2_npatterns;
2069 VECTOR_CST_NELTS_PER_PATTERN (t) = nelts_per_pattern;
2071 return t;
2074 /* Return a new VECTOR_CST node whose type is TYPE and whose values
2075 are extracted from V, a vector of CONSTRUCTOR_ELT. */
2077 tree
2078 build_vector_from_ctor (tree type, const vec<constructor_elt, va_gc> *v)
2080 if (vec_safe_length (v) == 0)
2081 return build_zero_cst (type);
2083 unsigned HOST_WIDE_INT idx, nelts;
2084 tree value;
2086 /* We can't construct a VECTOR_CST for a variable number of elements. */
2087 nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
2088 tree_vector_builder vec (type, nelts, 1);
2089 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
2091 if (TREE_CODE (value) == VECTOR_CST)
2093 /* If NELTS is constant then this must be too. */
2094 unsigned int sub_nelts = VECTOR_CST_NELTS (value).to_constant ();
2095 for (unsigned i = 0; i < sub_nelts; ++i)
2096 vec.quick_push (VECTOR_CST_ELT (value, i));
2098 else
2099 vec.quick_push (value);
2101 while (vec.length () < nelts)
2102 vec.quick_push (build_zero_cst (TREE_TYPE (type)));
2104 return vec.build ();
2107 /* Build a vector of type VECTYPE where all the elements are SCs. */
2108 tree
2109 build_vector_from_val (tree vectype, tree sc)
2111 unsigned HOST_WIDE_INT i, nunits;
2113 if (sc == error_mark_node)
2114 return sc;
2116 /* Verify that the vector type is suitable for SC. Note that there
2117 is some inconsistency in the type-system with respect to restrict
2118 qualifications of pointers. Vector types always have a main-variant
2119 element type and the qualification is applied to the vector-type.
2120 So TREE_TYPE (vector-type) does not return a properly qualified
2121 vector element-type. */
2122 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
2123 TREE_TYPE (vectype)));
2125 if (CONSTANT_CLASS_P (sc))
2127 tree_vector_builder v (vectype, 1, 1);
2128 v.quick_push (sc);
2129 return v.build ();
2131 else if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits))
2132 return fold_build1 (VEC_DUPLICATE_EXPR, vectype, sc);
2133 else
2135 vec<constructor_elt, va_gc> *v;
2136 vec_alloc (v, nunits);
2137 for (i = 0; i < nunits; ++i)
2138 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
2139 return build_constructor (vectype, v);
2143 /* If TYPE is not a vector type, just return SC, otherwise return
2144 build_vector_from_val (TYPE, SC). */
2146 tree
2147 build_uniform_cst (tree type, tree sc)
2149 if (!VECTOR_TYPE_P (type))
2150 return sc;
2152 return build_vector_from_val (type, sc);
2155 /* Build a vector series of type TYPE in which element I has the value
2156 BASE + I * STEP. The result is a constant if BASE and STEP are constant
2157 and a VEC_SERIES_EXPR otherwise. */
2159 tree
2160 build_vec_series (tree type, tree base, tree step)
2162 if (integer_zerop (step))
2163 return build_vector_from_val (type, base);
2164 if (TREE_CODE (base) == INTEGER_CST && TREE_CODE (step) == INTEGER_CST)
2166 tree_vector_builder builder (type, 1, 3);
2167 tree elt1 = wide_int_to_tree (TREE_TYPE (base),
2168 wi::to_wide (base) + wi::to_wide (step));
2169 tree elt2 = wide_int_to_tree (TREE_TYPE (base),
2170 wi::to_wide (elt1) + wi::to_wide (step));
2171 builder.quick_push (base);
2172 builder.quick_push (elt1);
2173 builder.quick_push (elt2);
2174 return builder.build ();
2176 return build2 (VEC_SERIES_EXPR, type, base, step);
2179 /* Return a vector with the same number of units and number of bits
2180 as VEC_TYPE, but in which the elements are a linear series of unsigned
2181 integers { BASE, BASE + STEP, BASE + STEP * 2, ... }. */
2183 tree
2184 build_index_vector (tree vec_type, poly_uint64 base, poly_uint64 step)
2186 tree index_vec_type = vec_type;
2187 tree index_elt_type = TREE_TYPE (vec_type);
2188 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vec_type);
2189 if (!INTEGRAL_TYPE_P (index_elt_type) || !TYPE_UNSIGNED (index_elt_type))
2191 index_elt_type = build_nonstandard_integer_type
2192 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (index_elt_type)), true);
2193 index_vec_type = build_vector_type (index_elt_type, nunits);
2196 tree_vector_builder v (index_vec_type, 1, 3);
2197 for (unsigned int i = 0; i < 3; ++i)
2198 v.quick_push (build_int_cstu (index_elt_type, base + i * step));
2199 return v.build ();
2202 /* Return a VECTOR_CST of type VEC_TYPE in which the first NUM_A
2203 elements are A and the rest are B. */
2205 tree
2206 build_vector_a_then_b (tree vec_type, unsigned int num_a, tree a, tree b)
2208 gcc_assert (known_le (num_a, TYPE_VECTOR_SUBPARTS (vec_type)));
2209 unsigned int count = constant_lower_bound (TYPE_VECTOR_SUBPARTS (vec_type));
2210 /* Optimize the constant case. */
2211 if ((count & 1) == 0 && TYPE_VECTOR_SUBPARTS (vec_type).is_constant ())
2212 count /= 2;
2213 tree_vector_builder builder (vec_type, count, 2);
2214 for (unsigned int i = 0; i < count * 2; ++i)
2215 builder.quick_push (i < num_a ? a : b);
2216 return builder.build ();
2219 /* Something has messed with the elements of CONSTRUCTOR C after it was built;
2220 calculate TREE_CONSTANT and TREE_SIDE_EFFECTS. */
2222 void
2223 recompute_constructor_flags (tree c)
2225 unsigned int i;
2226 tree val;
2227 bool constant_p = true;
2228 bool side_effects_p = false;
2229 vec<constructor_elt, va_gc> *vals = CONSTRUCTOR_ELTS (c);
2231 FOR_EACH_CONSTRUCTOR_VALUE (vals, i, val)
2233 /* Mostly ctors will have elts that don't have side-effects, so
2234 the usual case is to scan all the elements. Hence a single
2235 loop for both const and side effects, rather than one loop
2236 each (with early outs). */
2237 if (!TREE_CONSTANT (val))
2238 constant_p = false;
2239 if (TREE_SIDE_EFFECTS (val))
2240 side_effects_p = true;
2243 TREE_SIDE_EFFECTS (c) = side_effects_p;
2244 TREE_CONSTANT (c) = constant_p;
2247 /* Make sure that TREE_CONSTANT and TREE_SIDE_EFFECTS are correct for
2248 CONSTRUCTOR C. */
2250 void
2251 verify_constructor_flags (tree c)
2253 unsigned int i;
2254 tree val;
2255 bool constant_p = TREE_CONSTANT (c);
2256 bool side_effects_p = TREE_SIDE_EFFECTS (c);
2257 vec<constructor_elt, va_gc> *vals = CONSTRUCTOR_ELTS (c);
2259 FOR_EACH_CONSTRUCTOR_VALUE (vals, i, val)
2261 if (constant_p && !TREE_CONSTANT (val))
2262 internal_error ("non-constant element in constant CONSTRUCTOR");
2263 if (!side_effects_p && TREE_SIDE_EFFECTS (val))
2264 internal_error ("side-effects element in no-side-effects CONSTRUCTOR");
2268 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
2269 are in the vec pointed to by VALS. */
2270 tree
2271 build_constructor (tree type, vec<constructor_elt, va_gc> *vals MEM_STAT_DECL)
2273 tree c = make_node (CONSTRUCTOR PASS_MEM_STAT);
2275 TREE_TYPE (c) = type;
2276 CONSTRUCTOR_ELTS (c) = vals;
2278 recompute_constructor_flags (c);
2280 return c;
2283 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
2284 INDEX and VALUE. */
2285 tree
2286 build_constructor_single (tree type, tree index, tree value)
2288 vec<constructor_elt, va_gc> *v;
2289 constructor_elt elt = {index, value};
2291 vec_alloc (v, 1);
2292 v->quick_push (elt);
2294 return build_constructor (type, v);
2298 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
2299 are in a list pointed to by VALS. */
2300 tree
2301 build_constructor_from_list (tree type, tree vals)
2303 tree t;
2304 vec<constructor_elt, va_gc> *v = NULL;
2306 if (vals)
2308 vec_alloc (v, list_length (vals));
2309 for (t = vals; t; t = TREE_CHAIN (t))
2310 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
2313 return build_constructor (type, v);
2316 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
2317 are in a vector pointed to by VALS. Note that the TREE_PURPOSE
2318 fields in the constructor remain null. */
2320 tree
2321 build_constructor_from_vec (tree type, const vec<tree, va_gc> *vals)
2323 vec<constructor_elt, va_gc> *v = NULL;
2325 for (tree t : vals)
2326 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, t);
2328 return build_constructor (type, v);
2331 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
2332 of elements, provided as index/value pairs. */
2334 tree
2335 build_constructor_va (tree type, int nelts, ...)
2337 vec<constructor_elt, va_gc> *v = NULL;
2338 va_list p;
2340 va_start (p, nelts);
2341 vec_alloc (v, nelts);
2342 while (nelts--)
2344 tree index = va_arg (p, tree);
2345 tree value = va_arg (p, tree);
2346 CONSTRUCTOR_APPEND_ELT (v, index, value);
2348 va_end (p);
2349 return build_constructor (type, v);
2352 /* Return a node of type TYPE for which TREE_CLOBBER_P is true. */
2354 tree
2355 build_clobber (tree type, enum clobber_kind kind)
2357 tree clobber = build_constructor (type, NULL);
2358 TREE_THIS_VOLATILE (clobber) = true;
2359 CLOBBER_KIND (clobber) = kind;
2360 return clobber;
2363 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
2365 tree
2366 build_fixed (tree type, FIXED_VALUE_TYPE f)
2368 tree v;
2369 FIXED_VALUE_TYPE *fp;
2371 v = make_node (FIXED_CST);
2372 fp = ggc_alloc<fixed_value> ();
2373 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
2375 TREE_TYPE (v) = type;
2376 TREE_FIXED_CST_PTR (v) = fp;
2377 return v;
2380 /* Return a new REAL_CST node whose type is TYPE and value is D. */
2382 tree
2383 build_real (tree type, REAL_VALUE_TYPE d)
2385 tree v;
2386 int overflow = 0;
2388 /* dconst{1,2,m1,half} are used in various places in
2389 the middle-end and optimizers, allow them here
2390 even for decimal floating point types as an exception
2391 by converting them to decimal. */
2392 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type))
2393 && d.cl == rvc_normal
2394 && !d.decimal)
2396 if (memcmp (&d, &dconst1, sizeof (d)) == 0)
2397 decimal_real_from_string (&d, "1");
2398 else if (memcmp (&d, &dconst2, sizeof (d)) == 0)
2399 decimal_real_from_string (&d, "2");
2400 else if (memcmp (&d, &dconstm1, sizeof (d)) == 0)
2401 decimal_real_from_string (&d, "-1");
2402 else if (memcmp (&d, &dconsthalf, sizeof (d)) == 0)
2403 decimal_real_from_string (&d, "0.5");
2404 else
2405 gcc_unreachable ();
2408 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
2409 Consider doing it via real_convert now. */
2411 v = make_node (REAL_CST);
2412 TREE_TYPE (v) = type;
2413 memcpy (TREE_REAL_CST_PTR (v), &d, sizeof (REAL_VALUE_TYPE));
2414 TREE_OVERFLOW (v) = overflow;
2415 return v;
2418 /* Like build_real, but first truncate D to the type. */
2420 tree
2421 build_real_truncate (tree type, REAL_VALUE_TYPE d)
2423 return build_real (type, real_value_truncate (TYPE_MODE (type), d));
2426 /* Return a new REAL_CST node whose type is TYPE
2427 and whose value is the integer value of the INTEGER_CST node I. */
2429 REAL_VALUE_TYPE
2430 real_value_from_int_cst (const_tree type, const_tree i)
2432 REAL_VALUE_TYPE d;
2434 /* Clear all bits of the real value type so that we can later do
2435 bitwise comparisons to see if two values are the same. */
2436 memset (&d, 0, sizeof d);
2438 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode, wi::to_wide (i),
2439 TYPE_SIGN (TREE_TYPE (i)));
2440 return d;
2443 /* Given a tree representing an integer constant I, return a tree
2444 representing the same value as a floating-point constant of type TYPE. */
2446 tree
2447 build_real_from_int_cst (tree type, const_tree i)
2449 tree v;
2450 int overflow = TREE_OVERFLOW (i);
2452 v = build_real (type, real_value_from_int_cst (type, i));
2454 TREE_OVERFLOW (v) |= overflow;
2455 return v;
2458 /* Return a new REAL_CST node whose type is TYPE
2459 and whose value is the integer value I which has sign SGN. */
2461 tree
2462 build_real_from_wide (tree type, const wide_int_ref &i, signop sgn)
2464 REAL_VALUE_TYPE d;
2466 /* Clear all bits of the real value type so that we can later do
2467 bitwise comparisons to see if two values are the same. */
2468 memset (&d, 0, sizeof d);
2470 real_from_integer (&d, TYPE_MODE (type), i, sgn);
2471 return build_real (type, d);
2474 /* Return a newly constructed STRING_CST node whose value is the LEN
2475 characters at STR when STR is nonnull, or all zeros otherwise.
2476 Note that for a C string literal, LEN should include the trailing NUL.
2477 The TREE_TYPE is not initialized. */
2479 tree
2480 build_string (unsigned len, const char *str /*= NULL */)
2482 /* Do not waste bytes provided by padding of struct tree_string. */
2483 unsigned size = len + offsetof (struct tree_string, str) + 1;
2485 record_node_allocation_statistics (STRING_CST, size);
2487 tree s = (tree) ggc_internal_alloc (size);
2489 memset (s, 0, sizeof (struct tree_typed));
2490 TREE_SET_CODE (s, STRING_CST);
2491 TREE_CONSTANT (s) = 1;
2492 TREE_STRING_LENGTH (s) = len;
2493 if (str)
2494 memcpy (s->string.str, str, len);
2495 else
2496 memset (s->string.str, 0, len);
2497 s->string.str[len] = '\0';
2499 return s;
2502 /* Return a newly constructed COMPLEX_CST node whose value is
2503 specified by the real and imaginary parts REAL and IMAG.
2504 Both REAL and IMAG should be constant nodes. TYPE, if specified,
2505 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
2507 tree
2508 build_complex (tree type, tree real, tree imag)
2510 gcc_assert (CONSTANT_CLASS_P (real));
2511 gcc_assert (CONSTANT_CLASS_P (imag));
2513 tree t = make_node (COMPLEX_CST);
2515 TREE_REALPART (t) = real;
2516 TREE_IMAGPART (t) = imag;
2517 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
2518 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
2519 return t;
2522 /* Build a complex (inf +- 0i), such as for the result of cproj.
2523 TYPE is the complex tree type of the result. If NEG is true, the
2524 imaginary zero is negative. */
2526 tree
2527 build_complex_inf (tree type, bool neg)
2529 REAL_VALUE_TYPE rinf, rzero = dconst0;
2531 real_inf (&rinf);
2532 rzero.sign = neg;
2533 return build_complex (type, build_real (TREE_TYPE (type), rinf),
2534 build_real (TREE_TYPE (type), rzero));
2537 /* Return the constant 1 in type TYPE. If TYPE has several elements, each
2538 element is set to 1. In particular, this is 1 + i for complex types. */
2540 tree
2541 build_each_one_cst (tree type)
2543 if (TREE_CODE (type) == COMPLEX_TYPE)
2545 tree scalar = build_one_cst (TREE_TYPE (type));
2546 return build_complex (type, scalar, scalar);
2548 else
2549 return build_one_cst (type);
2552 /* Return a constant of arithmetic type TYPE which is the
2553 multiplicative identity of the set TYPE. */
2555 tree
2556 build_one_cst (tree type)
2558 switch (TREE_CODE (type))
2560 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2561 case POINTER_TYPE: case REFERENCE_TYPE:
2562 case OFFSET_TYPE:
2563 return build_int_cst (type, 1);
2565 case REAL_TYPE:
2566 return build_real (type, dconst1);
2568 case FIXED_POINT_TYPE:
2569 /* We can only generate 1 for accum types. */
2570 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2571 return build_fixed (type, FCONST1 (TYPE_MODE (type)));
2573 case VECTOR_TYPE:
2575 tree scalar = build_one_cst (TREE_TYPE (type));
2577 return build_vector_from_val (type, scalar);
2580 case COMPLEX_TYPE:
2581 return build_complex (type,
2582 build_one_cst (TREE_TYPE (type)),
2583 build_zero_cst (TREE_TYPE (type)));
2585 default:
2586 gcc_unreachable ();
2590 /* Return an integer of type TYPE containing all 1's in as much precision as
2591 it contains, or a complex or vector whose subparts are such integers. */
2593 tree
2594 build_all_ones_cst (tree type)
2596 if (TREE_CODE (type) == COMPLEX_TYPE)
2598 tree scalar = build_all_ones_cst (TREE_TYPE (type));
2599 return build_complex (type, scalar, scalar);
2601 else
2602 return build_minus_one_cst (type);
2605 /* Return a constant of arithmetic type TYPE which is the
2606 opposite of the multiplicative identity of the set TYPE. */
2608 tree
2609 build_minus_one_cst (tree type)
2611 switch (TREE_CODE (type))
2613 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2614 case POINTER_TYPE: case REFERENCE_TYPE:
2615 case OFFSET_TYPE:
2616 return build_int_cst (type, -1);
2618 case REAL_TYPE:
2619 return build_real (type, dconstm1);
2621 case FIXED_POINT_TYPE:
2622 /* We can only generate 1 for accum types. */
2623 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2624 return build_fixed (type,
2625 fixed_from_double_int (double_int_minus_one,
2626 SCALAR_TYPE_MODE (type)));
2628 case VECTOR_TYPE:
2630 tree scalar = build_minus_one_cst (TREE_TYPE (type));
2632 return build_vector_from_val (type, scalar);
2635 case COMPLEX_TYPE:
2636 return build_complex (type,
2637 build_minus_one_cst (TREE_TYPE (type)),
2638 build_zero_cst (TREE_TYPE (type)));
2640 default:
2641 gcc_unreachable ();
2645 /* Build 0 constant of type TYPE. This is used by constructor folding
2646 and thus the constant should be represented in memory by
2647 zero(es). */
2649 tree
2650 build_zero_cst (tree type)
2652 switch (TREE_CODE (type))
2654 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2655 case POINTER_TYPE: case REFERENCE_TYPE:
2656 case OFFSET_TYPE: case NULLPTR_TYPE:
2657 return build_int_cst (type, 0);
2659 case REAL_TYPE:
2660 return build_real (type, dconst0);
2662 case FIXED_POINT_TYPE:
2663 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
2665 case VECTOR_TYPE:
2667 tree scalar = build_zero_cst (TREE_TYPE (type));
2669 return build_vector_from_val (type, scalar);
2672 case COMPLEX_TYPE:
2674 tree zero = build_zero_cst (TREE_TYPE (type));
2676 return build_complex (type, zero, zero);
2679 default:
2680 if (!AGGREGATE_TYPE_P (type))
2681 return fold_convert (type, integer_zero_node);
2682 return build_constructor (type, NULL);
2687 /* Build a BINFO with LEN language slots. */
2689 tree
2690 make_tree_binfo (unsigned base_binfos MEM_STAT_DECL)
2692 tree t;
2693 size_t length = (offsetof (struct tree_binfo, base_binfos)
2694 + vec<tree, va_gc>::embedded_size (base_binfos));
2696 record_node_allocation_statistics (TREE_BINFO, length);
2698 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
2700 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
2702 TREE_SET_CODE (t, TREE_BINFO);
2704 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
2706 return t;
2709 /* Create a CASE_LABEL_EXPR tree node and return it. */
2711 tree
2712 build_case_label (tree low_value, tree high_value, tree label_decl)
2714 tree t = make_node (CASE_LABEL_EXPR);
2716 TREE_TYPE (t) = void_type_node;
2717 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
2719 CASE_LOW (t) = low_value;
2720 CASE_HIGH (t) = high_value;
2721 CASE_LABEL (t) = label_decl;
2722 CASE_CHAIN (t) = NULL_TREE;
2724 return t;
2727 /* Build a newly constructed INTEGER_CST node. LEN and EXT_LEN are the
2728 values of TREE_INT_CST_NUNITS and TREE_INT_CST_EXT_NUNITS respectively.
2729 The latter determines the length of the HOST_WIDE_INT vector. */
2731 tree
2732 make_int_cst (int len, int ext_len MEM_STAT_DECL)
2734 tree t;
2735 int length = ((ext_len - 1) * sizeof (HOST_WIDE_INT)
2736 + sizeof (struct tree_int_cst));
2738 gcc_assert (len);
2739 record_node_allocation_statistics (INTEGER_CST, length);
2741 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2743 TREE_SET_CODE (t, INTEGER_CST);
2744 TREE_INT_CST_NUNITS (t) = len;
2745 TREE_INT_CST_EXT_NUNITS (t) = ext_len;
2746 /* to_offset can only be applied to trees that are offset_int-sized
2747 or smaller. EXT_LEN is correct if it fits, otherwise the constant
2748 must be exactly the precision of offset_int and so LEN is correct. */
2749 if (ext_len <= OFFSET_INT_ELTS)
2750 TREE_INT_CST_OFFSET_NUNITS (t) = ext_len;
2751 else
2752 TREE_INT_CST_OFFSET_NUNITS (t) = len;
2754 TREE_CONSTANT (t) = 1;
2756 return t;
2759 /* Build a newly constructed TREE_VEC node of length LEN. */
2761 tree
2762 make_tree_vec (int len MEM_STAT_DECL)
2764 tree t;
2765 size_t length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2767 record_node_allocation_statistics (TREE_VEC, length);
2769 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2771 TREE_SET_CODE (t, TREE_VEC);
2772 TREE_VEC_LENGTH (t) = len;
2774 return t;
2777 /* Grow a TREE_VEC node to new length LEN. */
2779 tree
2780 grow_tree_vec (tree v, int len MEM_STAT_DECL)
2782 gcc_assert (TREE_CODE (v) == TREE_VEC);
2784 int oldlen = TREE_VEC_LENGTH (v);
2785 gcc_assert (len > oldlen);
2787 size_t oldlength = (oldlen - 1) * sizeof (tree) + sizeof (struct tree_vec);
2788 size_t length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2790 record_node_allocation_statistics (TREE_VEC, length - oldlength);
2792 v = (tree) ggc_realloc (v, length PASS_MEM_STAT);
2794 TREE_VEC_LENGTH (v) = len;
2796 return v;
2799 /* Return 1 if EXPR is the constant zero, whether it is integral, float or
2800 fixed, and scalar, complex or vector. */
2802 bool
2803 zerop (const_tree expr)
2805 return (integer_zerop (expr)
2806 || real_zerop (expr)
2807 || fixed_zerop (expr));
2810 /* Return 1 if EXPR is the integer constant zero or a complex constant
2811 of zero, or a location wrapper for such a constant. */
2813 bool
2814 integer_zerop (const_tree expr)
2816 STRIP_ANY_LOCATION_WRAPPER (expr);
2818 switch (TREE_CODE (expr))
2820 case INTEGER_CST:
2821 return wi::to_wide (expr) == 0;
2822 case COMPLEX_CST:
2823 return (integer_zerop (TREE_REALPART (expr))
2824 && integer_zerop (TREE_IMAGPART (expr)));
2825 case VECTOR_CST:
2826 return (VECTOR_CST_NPATTERNS (expr) == 1
2827 && VECTOR_CST_DUPLICATE_P (expr)
2828 && integer_zerop (VECTOR_CST_ENCODED_ELT (expr, 0)));
2829 default:
2830 return false;
2834 /* Return 1 if EXPR is the integer constant one or the corresponding
2835 complex constant, or a location wrapper for such a constant. */
2837 bool
2838 integer_onep (const_tree expr)
2840 STRIP_ANY_LOCATION_WRAPPER (expr);
2842 switch (TREE_CODE (expr))
2844 case INTEGER_CST:
2845 return wi::eq_p (wi::to_widest (expr), 1);
2846 case COMPLEX_CST:
2847 return (integer_onep (TREE_REALPART (expr))
2848 && integer_zerop (TREE_IMAGPART (expr)));
2849 case VECTOR_CST:
2850 return (VECTOR_CST_NPATTERNS (expr) == 1
2851 && VECTOR_CST_DUPLICATE_P (expr)
2852 && integer_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
2853 default:
2854 return false;
2858 /* Return 1 if EXPR is the integer constant one. For complex and vector,
2859 return 1 if every piece is the integer constant one.
2860 Also return 1 for location wrappers for such a constant. */
2862 bool
2863 integer_each_onep (const_tree expr)
2865 STRIP_ANY_LOCATION_WRAPPER (expr);
2867 if (TREE_CODE (expr) == COMPLEX_CST)
2868 return (integer_onep (TREE_REALPART (expr))
2869 && integer_onep (TREE_IMAGPART (expr)));
2870 else
2871 return integer_onep (expr);
2874 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
2875 it contains, or a complex or vector whose subparts are such integers,
2876 or a location wrapper for such a constant. */
2878 bool
2879 integer_all_onesp (const_tree expr)
2881 STRIP_ANY_LOCATION_WRAPPER (expr);
2883 if (TREE_CODE (expr) == COMPLEX_CST
2884 && integer_all_onesp (TREE_REALPART (expr))
2885 && integer_all_onesp (TREE_IMAGPART (expr)))
2886 return true;
2888 else if (TREE_CODE (expr) == VECTOR_CST)
2889 return (VECTOR_CST_NPATTERNS (expr) == 1
2890 && VECTOR_CST_DUPLICATE_P (expr)
2891 && integer_all_onesp (VECTOR_CST_ENCODED_ELT (expr, 0)));
2893 else if (TREE_CODE (expr) != INTEGER_CST)
2894 return false;
2896 return (wi::max_value (TYPE_PRECISION (TREE_TYPE (expr)), UNSIGNED)
2897 == wi::to_wide (expr));
2900 /* Return 1 if EXPR is the integer constant minus one, or a location wrapper
2901 for such a constant. */
2903 bool
2904 integer_minus_onep (const_tree expr)
2906 STRIP_ANY_LOCATION_WRAPPER (expr);
2908 if (TREE_CODE (expr) == COMPLEX_CST)
2909 return (integer_all_onesp (TREE_REALPART (expr))
2910 && integer_zerop (TREE_IMAGPART (expr)));
2911 else
2912 return integer_all_onesp (expr);
2915 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
2916 one bit on), or a location wrapper for such a constant. */
2918 bool
2919 integer_pow2p (const_tree expr)
2921 STRIP_ANY_LOCATION_WRAPPER (expr);
2923 if (TREE_CODE (expr) == COMPLEX_CST
2924 && integer_pow2p (TREE_REALPART (expr))
2925 && integer_zerop (TREE_IMAGPART (expr)))
2926 return true;
2928 if (TREE_CODE (expr) != INTEGER_CST)
2929 return false;
2931 return wi::popcount (wi::to_wide (expr)) == 1;
2934 /* Return 1 if EXPR is an integer constant other than zero or a
2935 complex constant other than zero, or a location wrapper for such a
2936 constant. */
2938 bool
2939 integer_nonzerop (const_tree expr)
2941 STRIP_ANY_LOCATION_WRAPPER (expr);
2943 return ((TREE_CODE (expr) == INTEGER_CST
2944 && wi::to_wide (expr) != 0)
2945 || (TREE_CODE (expr) == COMPLEX_CST
2946 && (integer_nonzerop (TREE_REALPART (expr))
2947 || integer_nonzerop (TREE_IMAGPART (expr)))));
2950 /* Return 1 if EXPR is the integer constant one. For vector,
2951 return 1 if every piece is the integer constant minus one
2952 (representing the value TRUE).
2953 Also return 1 for location wrappers for such a constant. */
2955 bool
2956 integer_truep (const_tree expr)
2958 STRIP_ANY_LOCATION_WRAPPER (expr);
2960 if (TREE_CODE (expr) == VECTOR_CST)
2961 return integer_all_onesp (expr);
2962 return integer_onep (expr);
2965 /* Return 1 if EXPR is the fixed-point constant zero, or a location wrapper
2966 for such a constant. */
2968 bool
2969 fixed_zerop (const_tree expr)
2971 STRIP_ANY_LOCATION_WRAPPER (expr);
2973 return (TREE_CODE (expr) == FIXED_CST
2974 && TREE_FIXED_CST (expr).data.is_zero ());
2977 /* Return the power of two represented by a tree node known to be a
2978 power of two. */
2981 tree_log2 (const_tree expr)
2983 if (TREE_CODE (expr) == COMPLEX_CST)
2984 return tree_log2 (TREE_REALPART (expr));
2986 return wi::exact_log2 (wi::to_wide (expr));
2989 /* Similar, but return the largest integer Y such that 2 ** Y is less
2990 than or equal to EXPR. */
2993 tree_floor_log2 (const_tree expr)
2995 if (TREE_CODE (expr) == COMPLEX_CST)
2996 return tree_log2 (TREE_REALPART (expr));
2998 return wi::floor_log2 (wi::to_wide (expr));
3001 /* Return number of known trailing zero bits in EXPR, or, if the value of
3002 EXPR is known to be zero, the precision of it's type. */
3004 unsigned int
3005 tree_ctz (const_tree expr)
3007 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
3008 && !POINTER_TYPE_P (TREE_TYPE (expr)))
3009 return 0;
3011 unsigned int ret1, ret2, prec = TYPE_PRECISION (TREE_TYPE (expr));
3012 switch (TREE_CODE (expr))
3014 case INTEGER_CST:
3015 ret1 = wi::ctz (wi::to_wide (expr));
3016 return MIN (ret1, prec);
3017 case SSA_NAME:
3018 ret1 = wi::ctz (get_nonzero_bits (expr));
3019 return MIN (ret1, prec);
3020 case PLUS_EXPR:
3021 case MINUS_EXPR:
3022 case BIT_IOR_EXPR:
3023 case BIT_XOR_EXPR:
3024 case MIN_EXPR:
3025 case MAX_EXPR:
3026 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3027 if (ret1 == 0)
3028 return ret1;
3029 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
3030 return MIN (ret1, ret2);
3031 case POINTER_PLUS_EXPR:
3032 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3033 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
3034 /* Second operand is sizetype, which could be in theory
3035 wider than pointer's precision. Make sure we never
3036 return more than prec. */
3037 ret2 = MIN (ret2, prec);
3038 return MIN (ret1, ret2);
3039 case BIT_AND_EXPR:
3040 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3041 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
3042 return MAX (ret1, ret2);
3043 case MULT_EXPR:
3044 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3045 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
3046 return MIN (ret1 + ret2, prec);
3047 case LSHIFT_EXPR:
3048 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3049 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
3050 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
3052 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
3053 return MIN (ret1 + ret2, prec);
3055 return ret1;
3056 case RSHIFT_EXPR:
3057 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
3058 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
3060 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3061 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
3062 if (ret1 > ret2)
3063 return ret1 - ret2;
3065 return 0;
3066 case TRUNC_DIV_EXPR:
3067 case CEIL_DIV_EXPR:
3068 case FLOOR_DIV_EXPR:
3069 case ROUND_DIV_EXPR:
3070 case EXACT_DIV_EXPR:
3071 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
3072 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) == 1)
3074 int l = tree_log2 (TREE_OPERAND (expr, 1));
3075 if (l >= 0)
3077 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3078 ret2 = l;
3079 if (ret1 > ret2)
3080 return ret1 - ret2;
3083 return 0;
3084 CASE_CONVERT:
3085 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3086 if (ret1 && ret1 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
3087 ret1 = prec;
3088 return MIN (ret1, prec);
3089 case SAVE_EXPR:
3090 return tree_ctz (TREE_OPERAND (expr, 0));
3091 case COND_EXPR:
3092 ret1 = tree_ctz (TREE_OPERAND (expr, 1));
3093 if (ret1 == 0)
3094 return 0;
3095 ret2 = tree_ctz (TREE_OPERAND (expr, 2));
3096 return MIN (ret1, ret2);
3097 case COMPOUND_EXPR:
3098 return tree_ctz (TREE_OPERAND (expr, 1));
3099 case ADDR_EXPR:
3100 ret1 = get_pointer_alignment (CONST_CAST_TREE (expr));
3101 if (ret1 > BITS_PER_UNIT)
3103 ret1 = ctz_hwi (ret1 / BITS_PER_UNIT);
3104 return MIN (ret1, prec);
3106 return 0;
3107 default:
3108 return 0;
3112 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
3113 decimal float constants, so don't return 1 for them.
3114 Also return 1 for location wrappers around such a constant. */
3116 bool
3117 real_zerop (const_tree expr)
3119 STRIP_ANY_LOCATION_WRAPPER (expr);
3121 switch (TREE_CODE (expr))
3123 case REAL_CST:
3124 return real_equal (&TREE_REAL_CST (expr), &dconst0)
3125 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
3126 case COMPLEX_CST:
3127 return real_zerop (TREE_REALPART (expr))
3128 && real_zerop (TREE_IMAGPART (expr));
3129 case VECTOR_CST:
3131 /* Don't simply check for a duplicate because the predicate
3132 accepts both +0.0 and -0.0. */
3133 unsigned count = vector_cst_encoded_nelts (expr);
3134 for (unsigned int i = 0; i < count; ++i)
3135 if (!real_zerop (VECTOR_CST_ENCODED_ELT (expr, i)))
3136 return false;
3137 return true;
3139 default:
3140 return false;
3144 /* Return 1 if EXPR is the real constant one in real or complex form.
3145 Trailing zeroes matter for decimal float constants, so don't return
3146 1 for them.
3147 Also return 1 for location wrappers around such a constant. */
3149 bool
3150 real_onep (const_tree expr)
3152 STRIP_ANY_LOCATION_WRAPPER (expr);
3154 switch (TREE_CODE (expr))
3156 case REAL_CST:
3157 return real_equal (&TREE_REAL_CST (expr), &dconst1)
3158 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
3159 case COMPLEX_CST:
3160 return real_onep (TREE_REALPART (expr))
3161 && real_zerop (TREE_IMAGPART (expr));
3162 case VECTOR_CST:
3163 return (VECTOR_CST_NPATTERNS (expr) == 1
3164 && VECTOR_CST_DUPLICATE_P (expr)
3165 && real_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
3166 default:
3167 return false;
3171 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
3172 matter for decimal float constants, so don't return 1 for them.
3173 Also return 1 for location wrappers around such a constant. */
3175 bool
3176 real_minus_onep (const_tree expr)
3178 STRIP_ANY_LOCATION_WRAPPER (expr);
3180 switch (TREE_CODE (expr))
3182 case REAL_CST:
3183 return real_equal (&TREE_REAL_CST (expr), &dconstm1)
3184 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
3185 case COMPLEX_CST:
3186 return real_minus_onep (TREE_REALPART (expr))
3187 && real_zerop (TREE_IMAGPART (expr));
3188 case VECTOR_CST:
3189 return (VECTOR_CST_NPATTERNS (expr) == 1
3190 && VECTOR_CST_DUPLICATE_P (expr)
3191 && real_minus_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
3192 default:
3193 return false;
3197 /* Nonzero if EXP is a constant or a cast of a constant. */
3199 bool
3200 really_constant_p (const_tree exp)
3202 /* This is not quite the same as STRIP_NOPS. It does more. */
3203 while (CONVERT_EXPR_P (exp)
3204 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3205 exp = TREE_OPERAND (exp, 0);
3206 return TREE_CONSTANT (exp);
3209 /* Return true if T holds a polynomial pointer difference, storing it in
3210 *VALUE if so. A true return means that T's precision is no greater
3211 than 64 bits, which is the largest address space we support, so *VALUE
3212 never loses precision. However, the signedness of the result does
3213 not necessarily match the signedness of T: sometimes an unsigned type
3214 like sizetype is used to encode a value that is actually negative. */
3216 bool
3217 ptrdiff_tree_p (const_tree t, poly_int64_pod *value)
3219 if (!t)
3220 return false;
3221 if (TREE_CODE (t) == INTEGER_CST)
3223 if (!cst_and_fits_in_hwi (t))
3224 return false;
3225 *value = int_cst_value (t);
3226 return true;
3228 if (POLY_INT_CST_P (t))
3230 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
3231 if (!cst_and_fits_in_hwi (POLY_INT_CST_COEFF (t, i)))
3232 return false;
3233 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
3234 value->coeffs[i] = int_cst_value (POLY_INT_CST_COEFF (t, i));
3235 return true;
3237 return false;
3240 poly_int64
3241 tree_to_poly_int64 (const_tree t)
3243 gcc_assert (tree_fits_poly_int64_p (t));
3244 if (POLY_INT_CST_P (t))
3245 return poly_int_cst_value (t).force_shwi ();
3246 return TREE_INT_CST_LOW (t);
3249 poly_uint64
3250 tree_to_poly_uint64 (const_tree t)
3252 gcc_assert (tree_fits_poly_uint64_p (t));
3253 if (POLY_INT_CST_P (t))
3254 return poly_int_cst_value (t).force_uhwi ();
3255 return TREE_INT_CST_LOW (t);
3258 /* Return first list element whose TREE_VALUE is ELEM.
3259 Return 0 if ELEM is not in LIST. */
3261 tree
3262 value_member (tree elem, tree list)
3264 while (list)
3266 if (elem == TREE_VALUE (list))
3267 return list;
3268 list = TREE_CHAIN (list);
3270 return NULL_TREE;
3273 /* Return first list element whose TREE_PURPOSE is ELEM.
3274 Return 0 if ELEM is not in LIST. */
3276 tree
3277 purpose_member (const_tree elem, tree list)
3279 while (list)
3281 if (elem == TREE_PURPOSE (list))
3282 return list;
3283 list = TREE_CHAIN (list);
3285 return NULL_TREE;
3288 /* Return true if ELEM is in V. */
3290 bool
3291 vec_member (const_tree elem, vec<tree, va_gc> *v)
3293 unsigned ix;
3294 tree t;
3295 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
3296 if (elem == t)
3297 return true;
3298 return false;
3301 /* Returns element number IDX (zero-origin) of chain CHAIN, or
3302 NULL_TREE. */
3304 tree
3305 chain_index (int idx, tree chain)
3307 for (; chain && idx > 0; --idx)
3308 chain = TREE_CHAIN (chain);
3309 return chain;
3312 /* Return nonzero if ELEM is part of the chain CHAIN. */
3314 bool
3315 chain_member (const_tree elem, const_tree chain)
3317 while (chain)
3319 if (elem == chain)
3320 return true;
3321 chain = DECL_CHAIN (chain);
3324 return false;
3327 /* Return the length of a chain of nodes chained through TREE_CHAIN.
3328 We expect a null pointer to mark the end of the chain.
3329 This is the Lisp primitive `length'. */
3332 list_length (const_tree t)
3334 const_tree p = t;
3335 #ifdef ENABLE_TREE_CHECKING
3336 const_tree q = t;
3337 #endif
3338 int len = 0;
3340 while (p)
3342 p = TREE_CHAIN (p);
3343 #ifdef ENABLE_TREE_CHECKING
3344 if (len % 2)
3345 q = TREE_CHAIN (q);
3346 gcc_assert (p != q);
3347 #endif
3348 len++;
3351 return len;
3354 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
3355 UNION_TYPE TYPE, or NULL_TREE if none. */
3357 tree
3358 first_field (const_tree type)
3360 tree t = TYPE_FIELDS (type);
3361 while (t && TREE_CODE (t) != FIELD_DECL)
3362 t = TREE_CHAIN (t);
3363 return t;
3366 /* Returns the last FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
3367 UNION_TYPE TYPE, or NULL_TREE if none. */
3369 tree
3370 last_field (const_tree type)
3372 tree last = NULL_TREE;
3374 for (tree fld = TYPE_FIELDS (type); fld; fld = TREE_CHAIN (fld))
3376 if (TREE_CODE (fld) != FIELD_DECL)
3377 continue;
3379 last = fld;
3382 return last;
3385 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
3386 by modifying the last node in chain 1 to point to chain 2.
3387 This is the Lisp primitive `nconc'. */
3389 tree
3390 chainon (tree op1, tree op2)
3392 tree t1;
3394 if (!op1)
3395 return op2;
3396 if (!op2)
3397 return op1;
3399 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
3400 continue;
3401 TREE_CHAIN (t1) = op2;
3403 #ifdef ENABLE_TREE_CHECKING
3405 tree t2;
3406 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
3407 gcc_assert (t2 != t1);
3409 #endif
3411 return op1;
3414 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
3416 tree
3417 tree_last (tree chain)
3419 tree next;
3420 if (chain)
3421 while ((next = TREE_CHAIN (chain)))
3422 chain = next;
3423 return chain;
3426 /* Reverse the order of elements in the chain T,
3427 and return the new head of the chain (old last element). */
3429 tree
3430 nreverse (tree t)
3432 tree prev = 0, decl, next;
3433 for (decl = t; decl; decl = next)
3435 /* We shouldn't be using this function to reverse BLOCK chains; we
3436 have blocks_nreverse for that. */
3437 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
3438 next = TREE_CHAIN (decl);
3439 TREE_CHAIN (decl) = prev;
3440 prev = decl;
3442 return prev;
3445 /* Return a newly created TREE_LIST node whose
3446 purpose and value fields are PARM and VALUE. */
3448 tree
3449 build_tree_list (tree parm, tree value MEM_STAT_DECL)
3451 tree t = make_node (TREE_LIST PASS_MEM_STAT);
3452 TREE_PURPOSE (t) = parm;
3453 TREE_VALUE (t) = value;
3454 return t;
3457 /* Build a chain of TREE_LIST nodes from a vector. */
3459 tree
3460 build_tree_list_vec (const vec<tree, va_gc> *vec MEM_STAT_DECL)
3462 tree ret = NULL_TREE;
3463 tree *pp = &ret;
3464 unsigned int i;
3465 tree t;
3466 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
3468 *pp = build_tree_list (NULL, t PASS_MEM_STAT);
3469 pp = &TREE_CHAIN (*pp);
3471 return ret;
3474 /* Return a newly created TREE_LIST node whose
3475 purpose and value fields are PURPOSE and VALUE
3476 and whose TREE_CHAIN is CHAIN. */
3478 tree
3479 tree_cons (tree purpose, tree value, tree chain MEM_STAT_DECL)
3481 tree node;
3483 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
3484 memset (node, 0, sizeof (struct tree_common));
3486 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
3488 TREE_SET_CODE (node, TREE_LIST);
3489 TREE_CHAIN (node) = chain;
3490 TREE_PURPOSE (node) = purpose;
3491 TREE_VALUE (node) = value;
3492 return node;
3495 /* Return the values of the elements of a CONSTRUCTOR as a vector of
3496 trees. */
3498 vec<tree, va_gc> *
3499 ctor_to_vec (tree ctor)
3501 vec<tree, va_gc> *vec;
3502 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
3503 unsigned int ix;
3504 tree val;
3506 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
3507 vec->quick_push (val);
3509 return vec;
3512 /* Return the size nominally occupied by an object of type TYPE
3513 when it resides in memory. The value is measured in units of bytes,
3514 and its data type is that normally used for type sizes
3515 (which is the first type created by make_signed_type or
3516 make_unsigned_type). */
3518 tree
3519 size_in_bytes_loc (location_t loc, const_tree type)
3521 tree t;
3523 if (type == error_mark_node)
3524 return integer_zero_node;
3526 type = TYPE_MAIN_VARIANT (type);
3527 t = TYPE_SIZE_UNIT (type);
3529 if (t == 0)
3531 lang_hooks.types.incomplete_type_error (loc, NULL_TREE, type);
3532 return size_zero_node;
3535 return t;
3538 /* Return the size of TYPE (in bytes) as a wide integer
3539 or return -1 if the size can vary or is larger than an integer. */
3541 HOST_WIDE_INT
3542 int_size_in_bytes (const_tree type)
3544 tree t;
3546 if (type == error_mark_node)
3547 return 0;
3549 type = TYPE_MAIN_VARIANT (type);
3550 t = TYPE_SIZE_UNIT (type);
3552 if (t && tree_fits_uhwi_p (t))
3553 return TREE_INT_CST_LOW (t);
3554 else
3555 return -1;
3558 /* Return the maximum size of TYPE (in bytes) as a wide integer
3559 or return -1 if the size can vary or is larger than an integer. */
3561 HOST_WIDE_INT
3562 max_int_size_in_bytes (const_tree type)
3564 HOST_WIDE_INT size = -1;
3565 tree size_tree;
3567 /* If this is an array type, check for a possible MAX_SIZE attached. */
3569 if (TREE_CODE (type) == ARRAY_TYPE)
3571 size_tree = TYPE_ARRAY_MAX_SIZE (type);
3573 if (size_tree && tree_fits_uhwi_p (size_tree))
3574 size = tree_to_uhwi (size_tree);
3577 /* If we still haven't been able to get a size, see if the language
3578 can compute a maximum size. */
3580 if (size == -1)
3582 size_tree = lang_hooks.types.max_size (type);
3584 if (size_tree && tree_fits_uhwi_p (size_tree))
3585 size = tree_to_uhwi (size_tree);
3588 return size;
3591 /* Return the bit position of FIELD, in bits from the start of the record.
3592 This is a tree of type bitsizetype. */
3594 tree
3595 bit_position (const_tree field)
3597 return bit_from_pos (DECL_FIELD_OFFSET (field),
3598 DECL_FIELD_BIT_OFFSET (field));
3601 /* Return the byte position of FIELD, in bytes from the start of the record.
3602 This is a tree of type sizetype. */
3604 tree
3605 byte_position (const_tree field)
3607 return byte_from_pos (DECL_FIELD_OFFSET (field),
3608 DECL_FIELD_BIT_OFFSET (field));
3611 /* Likewise, but return as an integer. It must be representable in
3612 that way (since it could be a signed value, we don't have the
3613 option of returning -1 like int_size_in_byte can. */
3615 HOST_WIDE_INT
3616 int_byte_position (const_tree field)
3618 return tree_to_shwi (byte_position (field));
3621 /* Return, as a tree node, the number of elements for TYPE (which is an
3622 ARRAY_TYPE) minus one. This counts only elements of the top array. */
3624 tree
3625 array_type_nelts (const_tree type)
3627 tree index_type, min, max;
3629 /* If they did it with unspecified bounds, then we should have already
3630 given an error about it before we got here. */
3631 if (! TYPE_DOMAIN (type))
3632 return error_mark_node;
3634 index_type = TYPE_DOMAIN (type);
3635 min = TYPE_MIN_VALUE (index_type);
3636 max = TYPE_MAX_VALUE (index_type);
3638 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
3639 if (!max)
3641 /* zero sized arrays are represented from C FE as complete types with
3642 NULL TYPE_MAX_VALUE and zero TYPE_SIZE, while C++ FE represents
3643 them as min 0, max -1. */
3644 if (COMPLETE_TYPE_P (type)
3645 && integer_zerop (TYPE_SIZE (type))
3646 && integer_zerop (min))
3647 return build_int_cst (TREE_TYPE (min), -1);
3649 return error_mark_node;
3652 return (integer_zerop (min)
3653 ? max
3654 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
3657 /* If arg is static -- a reference to an object in static storage -- then
3658 return the object. This is not the same as the C meaning of `static'.
3659 If arg isn't static, return NULL. */
3661 tree
3662 staticp (tree arg)
3664 switch (TREE_CODE (arg))
3666 case FUNCTION_DECL:
3667 /* Nested functions are static, even though taking their address will
3668 involve a trampoline as we unnest the nested function and create
3669 the trampoline on the tree level. */
3670 return arg;
3672 case VAR_DECL:
3673 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3674 && ! DECL_THREAD_LOCAL_P (arg)
3675 && ! DECL_DLLIMPORT_P (arg)
3676 ? arg : NULL);
3678 case CONST_DECL:
3679 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3680 ? arg : NULL);
3682 case CONSTRUCTOR:
3683 return TREE_STATIC (arg) ? arg : NULL;
3685 case LABEL_DECL:
3686 case STRING_CST:
3687 return arg;
3689 case COMPONENT_REF:
3690 /* If the thing being referenced is not a field, then it is
3691 something language specific. */
3692 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
3694 /* If we are referencing a bitfield, we can't evaluate an
3695 ADDR_EXPR at compile time and so it isn't a constant. */
3696 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
3697 return NULL;
3699 return staticp (TREE_OPERAND (arg, 0));
3701 case BIT_FIELD_REF:
3702 return NULL;
3704 case INDIRECT_REF:
3705 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
3707 case ARRAY_REF:
3708 case ARRAY_RANGE_REF:
3709 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
3710 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
3711 return staticp (TREE_OPERAND (arg, 0));
3712 else
3713 return NULL;
3715 case COMPOUND_LITERAL_EXPR:
3716 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
3718 default:
3719 return NULL;
3726 /* Return whether OP is a DECL whose address is function-invariant. */
3728 bool
3729 decl_address_invariant_p (const_tree op)
3731 /* The conditions below are slightly less strict than the one in
3732 staticp. */
3734 switch (TREE_CODE (op))
3736 case PARM_DECL:
3737 case RESULT_DECL:
3738 case LABEL_DECL:
3739 case FUNCTION_DECL:
3740 return true;
3742 case VAR_DECL:
3743 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3744 || DECL_THREAD_LOCAL_P (op)
3745 || DECL_CONTEXT (op) == current_function_decl
3746 || decl_function_context (op) == current_function_decl)
3747 return true;
3748 break;
3750 case CONST_DECL:
3751 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3752 || decl_function_context (op) == current_function_decl)
3753 return true;
3754 break;
3756 default:
3757 break;
3760 return false;
3763 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
3765 bool
3766 decl_address_ip_invariant_p (const_tree op)
3768 /* The conditions below are slightly less strict than the one in
3769 staticp. */
3771 switch (TREE_CODE (op))
3773 case LABEL_DECL:
3774 case FUNCTION_DECL:
3775 case STRING_CST:
3776 return true;
3778 case VAR_DECL:
3779 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
3780 && !DECL_DLLIMPORT_P (op))
3781 || DECL_THREAD_LOCAL_P (op))
3782 return true;
3783 break;
3785 case CONST_DECL:
3786 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
3787 return true;
3788 break;
3790 default:
3791 break;
3794 return false;
3798 /* Return true if T is function-invariant (internal function, does
3799 not handle arithmetic; that's handled in skip_simple_arithmetic and
3800 tree_invariant_p). */
3802 static bool
3803 tree_invariant_p_1 (tree t)
3805 tree op;
3807 if (TREE_CONSTANT (t)
3808 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
3809 return true;
3811 switch (TREE_CODE (t))
3813 case SAVE_EXPR:
3814 return true;
3816 case ADDR_EXPR:
3817 op = TREE_OPERAND (t, 0);
3818 while (handled_component_p (op))
3820 switch (TREE_CODE (op))
3822 case ARRAY_REF:
3823 case ARRAY_RANGE_REF:
3824 if (!tree_invariant_p (TREE_OPERAND (op, 1))
3825 || TREE_OPERAND (op, 2) != NULL_TREE
3826 || TREE_OPERAND (op, 3) != NULL_TREE)
3827 return false;
3828 break;
3830 case COMPONENT_REF:
3831 if (TREE_OPERAND (op, 2) != NULL_TREE)
3832 return false;
3833 break;
3835 default:;
3837 op = TREE_OPERAND (op, 0);
3840 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
3842 default:
3843 break;
3846 return false;
3849 /* Return true if T is function-invariant. */
3851 bool
3852 tree_invariant_p (tree t)
3854 tree inner = skip_simple_arithmetic (t);
3855 return tree_invariant_p_1 (inner);
3858 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
3859 Do this to any expression which may be used in more than one place,
3860 but must be evaluated only once.
3862 Normally, expand_expr would reevaluate the expression each time.
3863 Calling save_expr produces something that is evaluated and recorded
3864 the first time expand_expr is called on it. Subsequent calls to
3865 expand_expr just reuse the recorded value.
3867 The call to expand_expr that generates code that actually computes
3868 the value is the first call *at compile time*. Subsequent calls
3869 *at compile time* generate code to use the saved value.
3870 This produces correct result provided that *at run time* control
3871 always flows through the insns made by the first expand_expr
3872 before reaching the other places where the save_expr was evaluated.
3873 You, the caller of save_expr, must make sure this is so.
3875 Constants, and certain read-only nodes, are returned with no
3876 SAVE_EXPR because that is safe. Expressions containing placeholders
3877 are not touched; see tree.def for an explanation of what these
3878 are used for. */
3880 tree
3881 save_expr (tree expr)
3883 tree inner;
3885 /* If the tree evaluates to a constant, then we don't want to hide that
3886 fact (i.e. this allows further folding, and direct checks for constants).
3887 However, a read-only object that has side effects cannot be bypassed.
3888 Since it is no problem to reevaluate literals, we just return the
3889 literal node. */
3890 inner = skip_simple_arithmetic (expr);
3891 if (TREE_CODE (inner) == ERROR_MARK)
3892 return inner;
3894 if (tree_invariant_p_1 (inner))
3895 return expr;
3897 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
3898 it means that the size or offset of some field of an object depends on
3899 the value within another field.
3901 Note that it must not be the case that EXPR contains both a PLACEHOLDER_EXPR
3902 and some variable since it would then need to be both evaluated once and
3903 evaluated more than once. Front-ends must assure this case cannot
3904 happen by surrounding any such subexpressions in their own SAVE_EXPR
3905 and forcing evaluation at the proper time. */
3906 if (contains_placeholder_p (inner))
3907 return expr;
3909 expr = build1_loc (EXPR_LOCATION (expr), SAVE_EXPR, TREE_TYPE (expr), expr);
3911 /* This expression might be placed ahead of a jump to ensure that the
3912 value was computed on both sides of the jump. So make sure it isn't
3913 eliminated as dead. */
3914 TREE_SIDE_EFFECTS (expr) = 1;
3915 return expr;
3918 /* Look inside EXPR into any simple arithmetic operations. Return the
3919 outermost non-arithmetic or non-invariant node. */
3921 tree
3922 skip_simple_arithmetic (tree expr)
3924 /* We don't care about whether this can be used as an lvalue in this
3925 context. */
3926 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3927 expr = TREE_OPERAND (expr, 0);
3929 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
3930 a constant, it will be more efficient to not make another SAVE_EXPR since
3931 it will allow better simplification and GCSE will be able to merge the
3932 computations if they actually occur. */
3933 while (true)
3935 if (UNARY_CLASS_P (expr))
3936 expr = TREE_OPERAND (expr, 0);
3937 else if (BINARY_CLASS_P (expr))
3939 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
3940 expr = TREE_OPERAND (expr, 0);
3941 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
3942 expr = TREE_OPERAND (expr, 1);
3943 else
3944 break;
3946 else
3947 break;
3950 return expr;
3953 /* Look inside EXPR into simple arithmetic operations involving constants.
3954 Return the outermost non-arithmetic or non-constant node. */
3956 tree
3957 skip_simple_constant_arithmetic (tree expr)
3959 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3960 expr = TREE_OPERAND (expr, 0);
3962 while (true)
3964 if (UNARY_CLASS_P (expr))
3965 expr = TREE_OPERAND (expr, 0);
3966 else if (BINARY_CLASS_P (expr))
3968 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3969 expr = TREE_OPERAND (expr, 0);
3970 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3971 expr = TREE_OPERAND (expr, 1);
3972 else
3973 break;
3975 else
3976 break;
3979 return expr;
3982 /* Return which tree structure is used by T. */
3984 enum tree_node_structure_enum
3985 tree_node_structure (const_tree t)
3987 const enum tree_code code = TREE_CODE (t);
3988 return tree_node_structure_for_code (code);
3991 /* Set various status flags when building a CALL_EXPR object T. */
3993 static void
3994 process_call_operands (tree t)
3996 bool side_effects = TREE_SIDE_EFFECTS (t);
3997 bool read_only = false;
3998 int i = call_expr_flags (t);
4000 /* Calls have side-effects, except those to const or pure functions. */
4001 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
4002 side_effects = true;
4003 /* Propagate TREE_READONLY of arguments for const functions. */
4004 if (i & ECF_CONST)
4005 read_only = true;
4007 if (!side_effects || read_only)
4008 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
4010 tree op = TREE_OPERAND (t, i);
4011 if (op && TREE_SIDE_EFFECTS (op))
4012 side_effects = true;
4013 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
4014 read_only = false;
4017 TREE_SIDE_EFFECTS (t) = side_effects;
4018 TREE_READONLY (t) = read_only;
4021 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
4022 size or offset that depends on a field within a record. */
4024 bool
4025 contains_placeholder_p (const_tree exp)
4027 enum tree_code code;
4029 if (!exp)
4030 return 0;
4032 code = TREE_CODE (exp);
4033 if (code == PLACEHOLDER_EXPR)
4034 return 1;
4036 switch (TREE_CODE_CLASS (code))
4038 case tcc_reference:
4039 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
4040 position computations since they will be converted into a
4041 WITH_RECORD_EXPR involving the reference, which will assume
4042 here will be valid. */
4043 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
4045 case tcc_exceptional:
4046 if (code == TREE_LIST)
4047 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
4048 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
4049 break;
4051 case tcc_unary:
4052 case tcc_binary:
4053 case tcc_comparison:
4054 case tcc_expression:
4055 switch (code)
4057 case COMPOUND_EXPR:
4058 /* Ignoring the first operand isn't quite right, but works best. */
4059 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
4061 case COND_EXPR:
4062 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
4063 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
4064 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
4066 case SAVE_EXPR:
4067 /* The save_expr function never wraps anything containing
4068 a PLACEHOLDER_EXPR. */
4069 return 0;
4071 default:
4072 break;
4075 switch (TREE_CODE_LENGTH (code))
4077 case 1:
4078 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
4079 case 2:
4080 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
4081 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
4082 default:
4083 return 0;
4086 case tcc_vl_exp:
4087 switch (code)
4089 case CALL_EXPR:
4091 const_tree arg;
4092 const_call_expr_arg_iterator iter;
4093 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
4094 if (CONTAINS_PLACEHOLDER_P (arg))
4095 return 1;
4096 return 0;
4098 default:
4099 return 0;
4102 default:
4103 return 0;
4105 return 0;
4108 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
4109 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
4110 field positions. */
4112 static bool
4113 type_contains_placeholder_1 (const_tree type)
4115 /* If the size contains a placeholder or the parent type (component type in
4116 the case of arrays) type involves a placeholder, this type does. */
4117 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
4118 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
4119 || (!POINTER_TYPE_P (type)
4120 && TREE_TYPE (type)
4121 && type_contains_placeholder_p (TREE_TYPE (type))))
4122 return true;
4124 /* Now do type-specific checks. Note that the last part of the check above
4125 greatly limits what we have to do below. */
4126 switch (TREE_CODE (type))
4128 case VOID_TYPE:
4129 case OPAQUE_TYPE:
4130 case COMPLEX_TYPE:
4131 case ENUMERAL_TYPE:
4132 case BOOLEAN_TYPE:
4133 case POINTER_TYPE:
4134 case OFFSET_TYPE:
4135 case REFERENCE_TYPE:
4136 case METHOD_TYPE:
4137 case FUNCTION_TYPE:
4138 case VECTOR_TYPE:
4139 case NULLPTR_TYPE:
4140 return false;
4142 case INTEGER_TYPE:
4143 case REAL_TYPE:
4144 case FIXED_POINT_TYPE:
4145 /* Here we just check the bounds. */
4146 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
4147 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
4149 case ARRAY_TYPE:
4150 /* We have already checked the component type above, so just check
4151 the domain type. Flexible array members have a null domain. */
4152 return TYPE_DOMAIN (type) ?
4153 type_contains_placeholder_p (TYPE_DOMAIN (type)) : false;
4155 case RECORD_TYPE:
4156 case UNION_TYPE:
4157 case QUAL_UNION_TYPE:
4159 tree field;
4161 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4162 if (TREE_CODE (field) == FIELD_DECL
4163 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
4164 || (TREE_CODE (type) == QUAL_UNION_TYPE
4165 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
4166 || type_contains_placeholder_p (TREE_TYPE (field))))
4167 return true;
4169 return false;
4172 default:
4173 gcc_unreachable ();
4177 /* Wrapper around above function used to cache its result. */
4179 bool
4180 type_contains_placeholder_p (tree type)
4182 bool result;
4184 /* If the contains_placeholder_bits field has been initialized,
4185 then we know the answer. */
4186 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
4187 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
4189 /* Indicate that we've seen this type node, and the answer is false.
4190 This is what we want to return if we run into recursion via fields. */
4191 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
4193 /* Compute the real value. */
4194 result = type_contains_placeholder_1 (type);
4196 /* Store the real value. */
4197 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
4199 return result;
4202 /* Push tree EXP onto vector QUEUE if it is not already present. */
4204 static void
4205 push_without_duplicates (tree exp, vec<tree> *queue)
4207 unsigned int i;
4208 tree iter;
4210 FOR_EACH_VEC_ELT (*queue, i, iter)
4211 if (simple_cst_equal (iter, exp) == 1)
4212 break;
4214 if (!iter)
4215 queue->safe_push (exp);
4218 /* Given a tree EXP, find all occurrences of references to fields
4219 in a PLACEHOLDER_EXPR and place them in vector REFS without
4220 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
4221 we assume here that EXP contains only arithmetic expressions
4222 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
4223 argument list. */
4225 void
4226 find_placeholder_in_expr (tree exp, vec<tree> *refs)
4228 enum tree_code code = TREE_CODE (exp);
4229 tree inner;
4230 int i;
4232 /* We handle TREE_LIST and COMPONENT_REF separately. */
4233 if (code == TREE_LIST)
4235 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
4236 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
4238 else if (code == COMPONENT_REF)
4240 for (inner = TREE_OPERAND (exp, 0);
4241 REFERENCE_CLASS_P (inner);
4242 inner = TREE_OPERAND (inner, 0))
4245 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
4246 push_without_duplicates (exp, refs);
4247 else
4248 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
4250 else
4251 switch (TREE_CODE_CLASS (code))
4253 case tcc_constant:
4254 break;
4256 case tcc_declaration:
4257 /* Variables allocated to static storage can stay. */
4258 if (!TREE_STATIC (exp))
4259 push_without_duplicates (exp, refs);
4260 break;
4262 case tcc_expression:
4263 /* This is the pattern built in ada/make_aligning_type. */
4264 if (code == ADDR_EXPR
4265 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
4267 push_without_duplicates (exp, refs);
4268 break;
4271 /* Fall through. */
4273 case tcc_exceptional:
4274 case tcc_unary:
4275 case tcc_binary:
4276 case tcc_comparison:
4277 case tcc_reference:
4278 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
4279 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
4280 break;
4282 case tcc_vl_exp:
4283 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4284 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
4285 break;
4287 default:
4288 gcc_unreachable ();
4292 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
4293 return a tree with all occurrences of references to F in a
4294 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
4295 CONST_DECLs. Note that we assume here that EXP contains only
4296 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
4297 occurring only in their argument list. */
4299 tree
4300 substitute_in_expr (tree exp, tree f, tree r)
4302 enum tree_code code = TREE_CODE (exp);
4303 tree op0, op1, op2, op3;
4304 tree new_tree;
4306 /* We handle TREE_LIST and COMPONENT_REF separately. */
4307 if (code == TREE_LIST)
4309 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
4310 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
4311 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
4312 return exp;
4314 return tree_cons (TREE_PURPOSE (exp), op1, op0);
4316 else if (code == COMPONENT_REF)
4318 tree inner;
4320 /* If this expression is getting a value from a PLACEHOLDER_EXPR
4321 and it is the right field, replace it with R. */
4322 for (inner = TREE_OPERAND (exp, 0);
4323 REFERENCE_CLASS_P (inner);
4324 inner = TREE_OPERAND (inner, 0))
4327 /* The field. */
4328 op1 = TREE_OPERAND (exp, 1);
4330 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
4331 return r;
4333 /* If this expression hasn't been completed let, leave it alone. */
4334 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
4335 return exp;
4337 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4338 if (op0 == TREE_OPERAND (exp, 0))
4339 return exp;
4341 new_tree
4342 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
4344 else
4345 switch (TREE_CODE_CLASS (code))
4347 case tcc_constant:
4348 return exp;
4350 case tcc_declaration:
4351 if (exp == f)
4352 return r;
4353 else
4354 return exp;
4356 case tcc_expression:
4357 if (exp == f)
4358 return r;
4360 /* Fall through. */
4362 case tcc_exceptional:
4363 case tcc_unary:
4364 case tcc_binary:
4365 case tcc_comparison:
4366 case tcc_reference:
4367 switch (TREE_CODE_LENGTH (code))
4369 case 0:
4370 return exp;
4372 case 1:
4373 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4374 if (op0 == TREE_OPERAND (exp, 0))
4375 return exp;
4377 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
4378 break;
4380 case 2:
4381 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4382 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4384 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
4385 return exp;
4387 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
4388 break;
4390 case 3:
4391 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4392 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4393 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
4395 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4396 && op2 == TREE_OPERAND (exp, 2))
4397 return exp;
4399 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
4400 break;
4402 case 4:
4403 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4404 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4405 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
4406 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
4408 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4409 && op2 == TREE_OPERAND (exp, 2)
4410 && op3 == TREE_OPERAND (exp, 3))
4411 return exp;
4413 new_tree
4414 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
4415 break;
4417 default:
4418 gcc_unreachable ();
4420 break;
4422 case tcc_vl_exp:
4424 int i;
4426 new_tree = NULL_TREE;
4428 /* If we are trying to replace F with a constant or with another
4429 instance of one of the arguments of the call, inline back
4430 functions which do nothing else than computing a value from
4431 the arguments they are passed. This makes it possible to
4432 fold partially or entirely the replacement expression. */
4433 if (code == CALL_EXPR)
4435 bool maybe_inline = false;
4436 if (CONSTANT_CLASS_P (r))
4437 maybe_inline = true;
4438 else
4439 for (i = 3; i < TREE_OPERAND_LENGTH (exp); i++)
4440 if (operand_equal_p (TREE_OPERAND (exp, i), r, 0))
4442 maybe_inline = true;
4443 break;
4445 if (maybe_inline)
4447 tree t = maybe_inline_call_in_expr (exp);
4448 if (t)
4449 return SUBSTITUTE_IN_EXPR (t, f, r);
4453 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4455 tree op = TREE_OPERAND (exp, i);
4456 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
4457 if (new_op != op)
4459 if (!new_tree)
4460 new_tree = copy_node (exp);
4461 TREE_OPERAND (new_tree, i) = new_op;
4465 if (new_tree)
4467 new_tree = fold (new_tree);
4468 if (TREE_CODE (new_tree) == CALL_EXPR)
4469 process_call_operands (new_tree);
4471 else
4472 return exp;
4474 break;
4476 default:
4477 gcc_unreachable ();
4480 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4482 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4483 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4485 return new_tree;
4488 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
4489 for it within OBJ, a tree that is an object or a chain of references. */
4491 tree
4492 substitute_placeholder_in_expr (tree exp, tree obj)
4494 enum tree_code code = TREE_CODE (exp);
4495 tree op0, op1, op2, op3;
4496 tree new_tree;
4498 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
4499 in the chain of OBJ. */
4500 if (code == PLACEHOLDER_EXPR)
4502 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
4503 tree elt;
4505 for (elt = obj; elt != 0;
4506 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
4507 || TREE_CODE (elt) == COND_EXPR)
4508 ? TREE_OPERAND (elt, 1)
4509 : (REFERENCE_CLASS_P (elt)
4510 || UNARY_CLASS_P (elt)
4511 || BINARY_CLASS_P (elt)
4512 || VL_EXP_CLASS_P (elt)
4513 || EXPRESSION_CLASS_P (elt))
4514 ? TREE_OPERAND (elt, 0) : 0))
4515 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
4516 return elt;
4518 for (elt = obj; elt != 0;
4519 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
4520 || TREE_CODE (elt) == COND_EXPR)
4521 ? TREE_OPERAND (elt, 1)
4522 : (REFERENCE_CLASS_P (elt)
4523 || UNARY_CLASS_P (elt)
4524 || BINARY_CLASS_P (elt)
4525 || VL_EXP_CLASS_P (elt)
4526 || EXPRESSION_CLASS_P (elt))
4527 ? TREE_OPERAND (elt, 0) : 0))
4528 if (POINTER_TYPE_P (TREE_TYPE (elt))
4529 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
4530 == need_type))
4531 return fold_build1 (INDIRECT_REF, need_type, elt);
4533 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
4534 survives until RTL generation, there will be an error. */
4535 return exp;
4538 /* TREE_LIST is special because we need to look at TREE_VALUE
4539 and TREE_CHAIN, not TREE_OPERANDS. */
4540 else if (code == TREE_LIST)
4542 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
4543 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
4544 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
4545 return exp;
4547 return tree_cons (TREE_PURPOSE (exp), op1, op0);
4549 else
4550 switch (TREE_CODE_CLASS (code))
4552 case tcc_constant:
4553 case tcc_declaration:
4554 return exp;
4556 case tcc_exceptional:
4557 case tcc_unary:
4558 case tcc_binary:
4559 case tcc_comparison:
4560 case tcc_expression:
4561 case tcc_reference:
4562 case tcc_statement:
4563 switch (TREE_CODE_LENGTH (code))
4565 case 0:
4566 return exp;
4568 case 1:
4569 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4570 if (op0 == TREE_OPERAND (exp, 0))
4571 return exp;
4573 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
4574 break;
4576 case 2:
4577 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4578 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4580 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
4581 return exp;
4583 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
4584 break;
4586 case 3:
4587 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4588 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4589 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
4591 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4592 && op2 == TREE_OPERAND (exp, 2))
4593 return exp;
4595 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
4596 break;
4598 case 4:
4599 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4600 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4601 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
4602 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
4604 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4605 && op2 == TREE_OPERAND (exp, 2)
4606 && op3 == TREE_OPERAND (exp, 3))
4607 return exp;
4609 new_tree
4610 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
4611 break;
4613 default:
4614 gcc_unreachable ();
4616 break;
4618 case tcc_vl_exp:
4620 int i;
4622 new_tree = NULL_TREE;
4624 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4626 tree op = TREE_OPERAND (exp, i);
4627 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
4628 if (new_op != op)
4630 if (!new_tree)
4631 new_tree = copy_node (exp);
4632 TREE_OPERAND (new_tree, i) = new_op;
4636 if (new_tree)
4638 new_tree = fold (new_tree);
4639 if (TREE_CODE (new_tree) == CALL_EXPR)
4640 process_call_operands (new_tree);
4642 else
4643 return exp;
4645 break;
4647 default:
4648 gcc_unreachable ();
4651 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4653 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4654 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4656 return new_tree;
4660 /* Subroutine of stabilize_reference; this is called for subtrees of
4661 references. Any expression with side-effects must be put in a SAVE_EXPR
4662 to ensure that it is only evaluated once.
4664 We don't put SAVE_EXPR nodes around everything, because assigning very
4665 simple expressions to temporaries causes us to miss good opportunities
4666 for optimizations. Among other things, the opportunity to fold in the
4667 addition of a constant into an addressing mode often gets lost, e.g.
4668 "y[i+1] += x;". In general, we take the approach that we should not make
4669 an assignment unless we are forced into it - i.e., that any non-side effect
4670 operator should be allowed, and that cse should take care of coalescing
4671 multiple utterances of the same expression should that prove fruitful. */
4673 static tree
4674 stabilize_reference_1 (tree e)
4676 tree result;
4677 enum tree_code code = TREE_CODE (e);
4679 /* We cannot ignore const expressions because it might be a reference
4680 to a const array but whose index contains side-effects. But we can
4681 ignore things that are actual constant or that already have been
4682 handled by this function. */
4684 if (tree_invariant_p (e))
4685 return e;
4687 switch (TREE_CODE_CLASS (code))
4689 case tcc_exceptional:
4690 /* Always wrap STATEMENT_LIST into SAVE_EXPR, even if it doesn't
4691 have side-effects. */
4692 if (code == STATEMENT_LIST)
4693 return save_expr (e);
4694 /* FALLTHRU */
4695 case tcc_type:
4696 case tcc_declaration:
4697 case tcc_comparison:
4698 case tcc_statement:
4699 case tcc_expression:
4700 case tcc_reference:
4701 case tcc_vl_exp:
4702 /* If the expression has side-effects, then encase it in a SAVE_EXPR
4703 so that it will only be evaluated once. */
4704 /* The reference (r) and comparison (<) classes could be handled as
4705 below, but it is generally faster to only evaluate them once. */
4706 if (TREE_SIDE_EFFECTS (e))
4707 return save_expr (e);
4708 return e;
4710 case tcc_constant:
4711 /* Constants need no processing. In fact, we should never reach
4712 here. */
4713 return e;
4715 case tcc_binary:
4716 /* Division is slow and tends to be compiled with jumps,
4717 especially the division by powers of 2 that is often
4718 found inside of an array reference. So do it just once. */
4719 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
4720 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
4721 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
4722 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
4723 return save_expr (e);
4724 /* Recursively stabilize each operand. */
4725 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
4726 stabilize_reference_1 (TREE_OPERAND (e, 1)));
4727 break;
4729 case tcc_unary:
4730 /* Recursively stabilize each operand. */
4731 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
4732 break;
4734 default:
4735 gcc_unreachable ();
4738 TREE_TYPE (result) = TREE_TYPE (e);
4739 TREE_READONLY (result) = TREE_READONLY (e);
4740 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
4741 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
4743 return result;
4746 /* Stabilize a reference so that we can use it any number of times
4747 without causing its operands to be evaluated more than once.
4748 Returns the stabilized reference. This works by means of save_expr,
4749 so see the caveats in the comments about save_expr.
4751 Also allows conversion expressions whose operands are references.
4752 Any other kind of expression is returned unchanged. */
4754 tree
4755 stabilize_reference (tree ref)
4757 tree result;
4758 enum tree_code code = TREE_CODE (ref);
4760 switch (code)
4762 case VAR_DECL:
4763 case PARM_DECL:
4764 case RESULT_DECL:
4765 /* No action is needed in this case. */
4766 return ref;
4768 CASE_CONVERT:
4769 case FLOAT_EXPR:
4770 case FIX_TRUNC_EXPR:
4771 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
4772 break;
4774 case INDIRECT_REF:
4775 result = build_nt (INDIRECT_REF,
4776 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
4777 break;
4779 case COMPONENT_REF:
4780 result = build_nt (COMPONENT_REF,
4781 stabilize_reference (TREE_OPERAND (ref, 0)),
4782 TREE_OPERAND (ref, 1), NULL_TREE);
4783 break;
4785 case BIT_FIELD_REF:
4786 result = build_nt (BIT_FIELD_REF,
4787 stabilize_reference (TREE_OPERAND (ref, 0)),
4788 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
4789 REF_REVERSE_STORAGE_ORDER (result) = REF_REVERSE_STORAGE_ORDER (ref);
4790 break;
4792 case ARRAY_REF:
4793 result = build_nt (ARRAY_REF,
4794 stabilize_reference (TREE_OPERAND (ref, 0)),
4795 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4796 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4797 break;
4799 case ARRAY_RANGE_REF:
4800 result = build_nt (ARRAY_RANGE_REF,
4801 stabilize_reference (TREE_OPERAND (ref, 0)),
4802 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4803 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4804 break;
4806 case COMPOUND_EXPR:
4807 /* We cannot wrap the first expression in a SAVE_EXPR, as then
4808 it wouldn't be ignored. This matters when dealing with
4809 volatiles. */
4810 return stabilize_reference_1 (ref);
4812 /* If arg isn't a kind of lvalue we recognize, make no change.
4813 Caller should recognize the error for an invalid lvalue. */
4814 default:
4815 return ref;
4817 case ERROR_MARK:
4818 return error_mark_node;
4821 TREE_TYPE (result) = TREE_TYPE (ref);
4822 TREE_READONLY (result) = TREE_READONLY (ref);
4823 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
4824 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
4825 protected_set_expr_location (result, EXPR_LOCATION (ref));
4827 return result;
4830 /* Low-level constructors for expressions. */
4832 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
4833 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
4835 void
4836 recompute_tree_invariant_for_addr_expr (tree t)
4838 tree node;
4839 bool tc = true, se = false;
4841 gcc_assert (TREE_CODE (t) == ADDR_EXPR);
4843 /* We started out assuming this address is both invariant and constant, but
4844 does not have side effects. Now go down any handled components and see if
4845 any of them involve offsets that are either non-constant or non-invariant.
4846 Also check for side-effects.
4848 ??? Note that this code makes no attempt to deal with the case where
4849 taking the address of something causes a copy due to misalignment. */
4851 #define UPDATE_FLAGS(NODE) \
4852 do { tree _node = (NODE); \
4853 if (_node && !TREE_CONSTANT (_node)) tc = false; \
4854 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
4856 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
4857 node = TREE_OPERAND (node, 0))
4859 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
4860 array reference (probably made temporarily by the G++ front end),
4861 so ignore all the operands. */
4862 if ((TREE_CODE (node) == ARRAY_REF
4863 || TREE_CODE (node) == ARRAY_RANGE_REF)
4864 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
4866 UPDATE_FLAGS (TREE_OPERAND (node, 1));
4867 if (TREE_OPERAND (node, 2))
4868 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4869 if (TREE_OPERAND (node, 3))
4870 UPDATE_FLAGS (TREE_OPERAND (node, 3));
4872 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
4873 FIELD_DECL, apparently. The G++ front end can put something else
4874 there, at least temporarily. */
4875 else if (TREE_CODE (node) == COMPONENT_REF
4876 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
4878 if (TREE_OPERAND (node, 2))
4879 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4883 node = lang_hooks.expr_to_decl (node, &tc, &se);
4885 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4886 the address, since &(*a)->b is a form of addition. If it's a constant, the
4887 address is constant too. If it's a decl, its address is constant if the
4888 decl is static. Everything else is not constant and, furthermore,
4889 taking the address of a volatile variable is not volatile. */
4890 if (TREE_CODE (node) == INDIRECT_REF
4891 || TREE_CODE (node) == MEM_REF)
4892 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4893 else if (CONSTANT_CLASS_P (node))
4895 else if (DECL_P (node))
4896 tc &= (staticp (node) != NULL_TREE);
4897 else
4899 tc = false;
4900 se |= TREE_SIDE_EFFECTS (node);
4904 TREE_CONSTANT (t) = tc;
4905 TREE_SIDE_EFFECTS (t) = se;
4906 #undef UPDATE_FLAGS
4909 /* Build an expression of code CODE, data type TYPE, and operands as
4910 specified. Expressions and reference nodes can be created this way.
4911 Constants, decls, types and misc nodes cannot be.
4913 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4914 enough for all extant tree codes. */
4916 tree
4917 build0 (enum tree_code code, tree tt MEM_STAT_DECL)
4919 tree t;
4921 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4923 t = make_node (code PASS_MEM_STAT);
4924 TREE_TYPE (t) = tt;
4926 return t;
4929 tree
4930 build1 (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4932 int length = sizeof (struct tree_exp);
4933 tree t;
4935 record_node_allocation_statistics (code, length);
4937 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4939 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4941 memset (t, 0, sizeof (struct tree_common));
4943 TREE_SET_CODE (t, code);
4945 TREE_TYPE (t) = type;
4946 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4947 TREE_OPERAND (t, 0) = node;
4948 if (node && !TYPE_P (node))
4950 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4951 TREE_READONLY (t) = TREE_READONLY (node);
4954 if (TREE_CODE_CLASS (code) == tcc_statement)
4956 if (code != DEBUG_BEGIN_STMT)
4957 TREE_SIDE_EFFECTS (t) = 1;
4959 else switch (code)
4961 case VA_ARG_EXPR:
4962 /* All of these have side-effects, no matter what their
4963 operands are. */
4964 TREE_SIDE_EFFECTS (t) = 1;
4965 TREE_READONLY (t) = 0;
4966 break;
4968 case INDIRECT_REF:
4969 /* Whether a dereference is readonly has nothing to do with whether
4970 its operand is readonly. */
4971 TREE_READONLY (t) = 0;
4972 break;
4974 case ADDR_EXPR:
4975 if (node)
4976 recompute_tree_invariant_for_addr_expr (t);
4977 break;
4979 default:
4980 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4981 && node && !TYPE_P (node)
4982 && TREE_CONSTANT (node))
4983 TREE_CONSTANT (t) = 1;
4984 if (TREE_CODE_CLASS (code) == tcc_reference
4985 && node && TREE_THIS_VOLATILE (node))
4986 TREE_THIS_VOLATILE (t) = 1;
4987 break;
4990 return t;
4993 #define PROCESS_ARG(N) \
4994 do { \
4995 TREE_OPERAND (t, N) = arg##N; \
4996 if (arg##N &&!TYPE_P (arg##N)) \
4998 if (TREE_SIDE_EFFECTS (arg##N)) \
4999 side_effects = 1; \
5000 if (!TREE_READONLY (arg##N) \
5001 && !CONSTANT_CLASS_P (arg##N)) \
5002 (void) (read_only = 0); \
5003 if (!TREE_CONSTANT (arg##N)) \
5004 (void) (constant = 0); \
5006 } while (0)
5008 tree
5009 build2 (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
5011 bool constant, read_only, side_effects, div_by_zero;
5012 tree t;
5014 gcc_assert (TREE_CODE_LENGTH (code) == 2);
5016 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
5017 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
5018 /* When sizetype precision doesn't match that of pointers
5019 we need to be able to build explicit extensions or truncations
5020 of the offset argument. */
5021 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
5022 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
5023 && TREE_CODE (arg1) == INTEGER_CST);
5025 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
5026 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
5027 && ptrofftype_p (TREE_TYPE (arg1)));
5029 t = make_node (code PASS_MEM_STAT);
5030 TREE_TYPE (t) = tt;
5032 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
5033 result based on those same flags for the arguments. But if the
5034 arguments aren't really even `tree' expressions, we shouldn't be trying
5035 to do this. */
5037 /* Expressions without side effects may be constant if their
5038 arguments are as well. */
5039 constant = (TREE_CODE_CLASS (code) == tcc_comparison
5040 || TREE_CODE_CLASS (code) == tcc_binary);
5041 read_only = 1;
5042 side_effects = TREE_SIDE_EFFECTS (t);
5044 switch (code)
5046 case TRUNC_DIV_EXPR:
5047 case CEIL_DIV_EXPR:
5048 case FLOOR_DIV_EXPR:
5049 case ROUND_DIV_EXPR:
5050 case EXACT_DIV_EXPR:
5051 case CEIL_MOD_EXPR:
5052 case FLOOR_MOD_EXPR:
5053 case ROUND_MOD_EXPR:
5054 case TRUNC_MOD_EXPR:
5055 div_by_zero = integer_zerop (arg1);
5056 break;
5057 default:
5058 div_by_zero = false;
5061 PROCESS_ARG (0);
5062 PROCESS_ARG (1);
5064 TREE_SIDE_EFFECTS (t) = side_effects;
5065 if (code == MEM_REF)
5067 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
5069 tree o = TREE_OPERAND (arg0, 0);
5070 TREE_READONLY (t) = TREE_READONLY (o);
5071 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
5074 else
5076 TREE_READONLY (t) = read_only;
5077 /* Don't mark X / 0 as constant. */
5078 TREE_CONSTANT (t) = constant && !div_by_zero;
5079 TREE_THIS_VOLATILE (t)
5080 = (TREE_CODE_CLASS (code) == tcc_reference
5081 && arg0 && TREE_THIS_VOLATILE (arg0));
5084 return t;
5088 tree
5089 build3 (enum tree_code code, tree tt, tree arg0, tree arg1,
5090 tree arg2 MEM_STAT_DECL)
5092 bool constant, read_only, side_effects;
5093 tree t;
5095 gcc_assert (TREE_CODE_LENGTH (code) == 3);
5096 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
5098 t = make_node (code PASS_MEM_STAT);
5099 TREE_TYPE (t) = tt;
5101 read_only = 1;
5103 /* As a special exception, if COND_EXPR has NULL branches, we
5104 assume that it is a gimple statement and always consider
5105 it to have side effects. */
5106 if (code == COND_EXPR
5107 && tt == void_type_node
5108 && arg1 == NULL_TREE
5109 && arg2 == NULL_TREE)
5110 side_effects = true;
5111 else
5112 side_effects = TREE_SIDE_EFFECTS (t);
5114 PROCESS_ARG (0);
5115 PROCESS_ARG (1);
5116 PROCESS_ARG (2);
5118 if (code == COND_EXPR)
5119 TREE_READONLY (t) = read_only;
5121 TREE_SIDE_EFFECTS (t) = side_effects;
5122 TREE_THIS_VOLATILE (t)
5123 = (TREE_CODE_CLASS (code) == tcc_reference
5124 && arg0 && TREE_THIS_VOLATILE (arg0));
5126 return t;
5129 tree
5130 build4 (enum tree_code code, tree tt, tree arg0, tree arg1,
5131 tree arg2, tree arg3 MEM_STAT_DECL)
5133 bool constant, read_only, side_effects;
5134 tree t;
5136 gcc_assert (TREE_CODE_LENGTH (code) == 4);
5138 t = make_node (code PASS_MEM_STAT);
5139 TREE_TYPE (t) = tt;
5141 side_effects = TREE_SIDE_EFFECTS (t);
5143 PROCESS_ARG (0);
5144 PROCESS_ARG (1);
5145 PROCESS_ARG (2);
5146 PROCESS_ARG (3);
5148 TREE_SIDE_EFFECTS (t) = side_effects;
5149 TREE_THIS_VOLATILE (t)
5150 = (TREE_CODE_CLASS (code) == tcc_reference
5151 && arg0 && TREE_THIS_VOLATILE (arg0));
5153 return t;
5156 tree
5157 build5 (enum tree_code code, tree tt, tree arg0, tree arg1,
5158 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
5160 bool constant, read_only, side_effects;
5161 tree t;
5163 gcc_assert (TREE_CODE_LENGTH (code) == 5);
5165 t = make_node (code PASS_MEM_STAT);
5166 TREE_TYPE (t) = tt;
5168 side_effects = TREE_SIDE_EFFECTS (t);
5170 PROCESS_ARG (0);
5171 PROCESS_ARG (1);
5172 PROCESS_ARG (2);
5173 PROCESS_ARG (3);
5174 PROCESS_ARG (4);
5176 TREE_SIDE_EFFECTS (t) = side_effects;
5177 if (code == TARGET_MEM_REF)
5179 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
5181 tree o = TREE_OPERAND (arg0, 0);
5182 TREE_READONLY (t) = TREE_READONLY (o);
5183 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
5186 else
5187 TREE_THIS_VOLATILE (t)
5188 = (TREE_CODE_CLASS (code) == tcc_reference
5189 && arg0 && TREE_THIS_VOLATILE (arg0));
5191 return t;
5194 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
5195 on the pointer PTR. */
5197 tree
5198 build_simple_mem_ref_loc (location_t loc, tree ptr)
5200 poly_int64 offset = 0;
5201 tree ptype = TREE_TYPE (ptr);
5202 tree tem;
5203 /* For convenience allow addresses that collapse to a simple base
5204 and offset. */
5205 if (TREE_CODE (ptr) == ADDR_EXPR
5206 && (handled_component_p (TREE_OPERAND (ptr, 0))
5207 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
5209 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
5210 gcc_assert (ptr);
5211 if (TREE_CODE (ptr) == MEM_REF)
5213 offset += mem_ref_offset (ptr).force_shwi ();
5214 ptr = TREE_OPERAND (ptr, 0);
5216 else
5217 ptr = build_fold_addr_expr (ptr);
5218 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
5220 tem = build2 (MEM_REF, TREE_TYPE (ptype),
5221 ptr, build_int_cst (ptype, offset));
5222 SET_EXPR_LOCATION (tem, loc);
5223 return tem;
5226 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
5228 poly_offset_int
5229 mem_ref_offset (const_tree t)
5231 return poly_offset_int::from (wi::to_poly_wide (TREE_OPERAND (t, 1)),
5232 SIGNED);
5235 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
5236 offsetted by OFFSET units. */
5238 tree
5239 build_invariant_address (tree type, tree base, poly_int64 offset)
5241 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
5242 build_fold_addr_expr (base),
5243 build_int_cst (ptr_type_node, offset));
5244 tree addr = build1 (ADDR_EXPR, type, ref);
5245 recompute_tree_invariant_for_addr_expr (addr);
5246 return addr;
5249 /* Similar except don't specify the TREE_TYPE
5250 and leave the TREE_SIDE_EFFECTS as 0.
5251 It is permissible for arguments to be null,
5252 or even garbage if their values do not matter. */
5254 tree
5255 build_nt (enum tree_code code, ...)
5257 tree t;
5258 int length;
5259 int i;
5260 va_list p;
5262 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
5264 va_start (p, code);
5266 t = make_node (code);
5267 length = TREE_CODE_LENGTH (code);
5269 for (i = 0; i < length; i++)
5270 TREE_OPERAND (t, i) = va_arg (p, tree);
5272 va_end (p);
5273 return t;
5276 /* Similar to build_nt, but for creating a CALL_EXPR object with a
5277 tree vec. */
5279 tree
5280 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
5282 tree ret, t;
5283 unsigned int ix;
5285 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
5286 CALL_EXPR_FN (ret) = fn;
5287 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
5288 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
5289 CALL_EXPR_ARG (ret, ix) = t;
5290 return ret;
5293 /* Create a DECL_... node of code CODE, name NAME (if non-null)
5294 and data type TYPE.
5295 We do NOT enter this node in any sort of symbol table.
5297 LOC is the location of the decl.
5299 layout_decl is used to set up the decl's storage layout.
5300 Other slots are initialized to 0 or null pointers. */
5302 tree
5303 build_decl (location_t loc, enum tree_code code, tree name,
5304 tree type MEM_STAT_DECL)
5306 tree t;
5308 t = make_node (code PASS_MEM_STAT);
5309 DECL_SOURCE_LOCATION (t) = loc;
5311 /* if (type == error_mark_node)
5312 type = integer_type_node; */
5313 /* That is not done, deliberately, so that having error_mark_node
5314 as the type can suppress useless errors in the use of this variable. */
5316 DECL_NAME (t) = name;
5317 TREE_TYPE (t) = type;
5319 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
5320 layout_decl (t, 0);
5322 return t;
5325 /* Create and return a DEBUG_EXPR_DECL node of the given TYPE. */
5327 tree
5328 build_debug_expr_decl (tree type)
5330 tree vexpr = make_node (DEBUG_EXPR_DECL);
5331 DECL_ARTIFICIAL (vexpr) = 1;
5332 TREE_TYPE (vexpr) = type;
5333 SET_DECL_MODE (vexpr, TYPE_MODE (type));
5334 return vexpr;
5337 /* Builds and returns function declaration with NAME and TYPE. */
5339 tree
5340 build_fn_decl (const char *name, tree type)
5342 tree id = get_identifier (name);
5343 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
5345 DECL_EXTERNAL (decl) = 1;
5346 TREE_PUBLIC (decl) = 1;
5347 DECL_ARTIFICIAL (decl) = 1;
5348 TREE_NOTHROW (decl) = 1;
5350 return decl;
5353 vec<tree, va_gc> *all_translation_units;
5355 /* Builds a new translation-unit decl with name NAME, queues it in the
5356 global list of translation-unit decls and returns it. */
5358 tree
5359 build_translation_unit_decl (tree name)
5361 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
5362 name, NULL_TREE);
5363 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
5364 vec_safe_push (all_translation_units, tu);
5365 return tu;
5369 /* BLOCK nodes are used to represent the structure of binding contours
5370 and declarations, once those contours have been exited and their contents
5371 compiled. This information is used for outputting debugging info. */
5373 tree
5374 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
5376 tree block = make_node (BLOCK);
5378 BLOCK_VARS (block) = vars;
5379 BLOCK_SUBBLOCKS (block) = subblocks;
5380 BLOCK_SUPERCONTEXT (block) = supercontext;
5381 BLOCK_CHAIN (block) = chain;
5382 return block;
5386 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
5388 LOC is the location to use in tree T. */
5390 void
5391 protected_set_expr_location (tree t, location_t loc)
5393 if (CAN_HAVE_LOCATION_P (t))
5394 SET_EXPR_LOCATION (t, loc);
5395 else if (t && TREE_CODE (t) == STATEMENT_LIST)
5397 t = expr_single (t);
5398 if (t && CAN_HAVE_LOCATION_P (t))
5399 SET_EXPR_LOCATION (t, loc);
5403 /* Like PROTECTED_SET_EXPR_LOCATION, but only do that if T has
5404 UNKNOWN_LOCATION. */
5406 void
5407 protected_set_expr_location_if_unset (tree t, location_t loc)
5409 t = expr_single (t);
5410 if (t && !EXPR_HAS_LOCATION (t))
5411 protected_set_expr_location (t, loc);
5414 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
5415 of the various TYPE_QUAL values. */
5417 static void
5418 set_type_quals (tree type, int type_quals)
5420 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
5421 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
5422 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
5423 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
5424 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
5427 /* Returns true iff CAND and BASE have equivalent language-specific
5428 qualifiers. */
5430 bool
5431 check_lang_type (const_tree cand, const_tree base)
5433 if (lang_hooks.types.type_hash_eq == NULL)
5434 return true;
5435 /* type_hash_eq currently only applies to these types. */
5436 if (TREE_CODE (cand) != FUNCTION_TYPE
5437 && TREE_CODE (cand) != METHOD_TYPE)
5438 return true;
5439 return lang_hooks.types.type_hash_eq (cand, base);
5442 /* This function checks to see if TYPE matches the size one of the built-in
5443 atomic types, and returns that core atomic type. */
5445 static tree
5446 find_atomic_core_type (const_tree type)
5448 tree base_atomic_type;
5450 /* Only handle complete types. */
5451 if (!tree_fits_uhwi_p (TYPE_SIZE (type)))
5452 return NULL_TREE;
5454 switch (tree_to_uhwi (TYPE_SIZE (type)))
5456 case 8:
5457 base_atomic_type = atomicQI_type_node;
5458 break;
5460 case 16:
5461 base_atomic_type = atomicHI_type_node;
5462 break;
5464 case 32:
5465 base_atomic_type = atomicSI_type_node;
5466 break;
5468 case 64:
5469 base_atomic_type = atomicDI_type_node;
5470 break;
5472 case 128:
5473 base_atomic_type = atomicTI_type_node;
5474 break;
5476 default:
5477 base_atomic_type = NULL_TREE;
5480 return base_atomic_type;
5483 /* Returns true iff unqualified CAND and BASE are equivalent. */
5485 bool
5486 check_base_type (const_tree cand, const_tree base)
5488 if (TYPE_NAME (cand) != TYPE_NAME (base)
5489 /* Apparently this is needed for Objective-C. */
5490 || TYPE_CONTEXT (cand) != TYPE_CONTEXT (base)
5491 || !attribute_list_equal (TYPE_ATTRIBUTES (cand),
5492 TYPE_ATTRIBUTES (base)))
5493 return false;
5494 /* Check alignment. */
5495 if (TYPE_ALIGN (cand) == TYPE_ALIGN (base)
5496 && TYPE_USER_ALIGN (cand) == TYPE_USER_ALIGN (base))
5497 return true;
5498 /* Atomic types increase minimal alignment. We must to do so as well
5499 or we get duplicated canonical types. See PR88686. */
5500 if ((TYPE_QUALS (cand) & TYPE_QUAL_ATOMIC))
5502 /* See if this object can map to a basic atomic type. */
5503 tree atomic_type = find_atomic_core_type (cand);
5504 if (atomic_type && TYPE_ALIGN (atomic_type) == TYPE_ALIGN (cand))
5505 return true;
5507 return false;
5510 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
5512 bool
5513 check_qualified_type (const_tree cand, const_tree base, int type_quals)
5515 return (TYPE_QUALS (cand) == type_quals
5516 && check_base_type (cand, base)
5517 && check_lang_type (cand, base));
5520 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
5522 static bool
5523 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
5525 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
5526 && TYPE_NAME (cand) == TYPE_NAME (base)
5527 /* Apparently this is needed for Objective-C. */
5528 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5529 /* Check alignment. */
5530 && TYPE_ALIGN (cand) == align
5531 /* Check this is a user-aligned type as build_aligned_type
5532 would create. */
5533 && TYPE_USER_ALIGN (cand)
5534 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5535 TYPE_ATTRIBUTES (base))
5536 && check_lang_type (cand, base));
5539 /* Return a version of the TYPE, qualified as indicated by the
5540 TYPE_QUALS, if one exists. If no qualified version exists yet,
5541 return NULL_TREE. */
5543 tree
5544 get_qualified_type (tree type, int type_quals)
5546 if (TYPE_QUALS (type) == type_quals)
5547 return type;
5549 tree mv = TYPE_MAIN_VARIANT (type);
5550 if (check_qualified_type (mv, type, type_quals))
5551 return mv;
5553 /* Search the chain of variants to see if there is already one there just
5554 like the one we need to have. If so, use that existing one. We must
5555 preserve the TYPE_NAME, since there is code that depends on this. */
5556 for (tree *tp = &TYPE_NEXT_VARIANT (mv); *tp; tp = &TYPE_NEXT_VARIANT (*tp))
5557 if (check_qualified_type (*tp, type, type_quals))
5559 /* Put the found variant at the head of the variant list so
5560 frequently searched variants get found faster. The C++ FE
5561 benefits greatly from this. */
5562 tree t = *tp;
5563 *tp = TYPE_NEXT_VARIANT (t);
5564 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
5565 TYPE_NEXT_VARIANT (mv) = t;
5566 return t;
5569 return NULL_TREE;
5572 /* Like get_qualified_type, but creates the type if it does not
5573 exist. This function never returns NULL_TREE. */
5575 tree
5576 build_qualified_type (tree type, int type_quals MEM_STAT_DECL)
5578 tree t;
5580 /* See if we already have the appropriate qualified variant. */
5581 t = get_qualified_type (type, type_quals);
5583 /* If not, build it. */
5584 if (!t)
5586 t = build_variant_type_copy (type PASS_MEM_STAT);
5587 set_type_quals (t, type_quals);
5589 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
5591 /* See if this object can map to a basic atomic type. */
5592 tree atomic_type = find_atomic_core_type (type);
5593 if (atomic_type)
5595 /* Ensure the alignment of this type is compatible with
5596 the required alignment of the atomic type. */
5597 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
5598 SET_TYPE_ALIGN (t, TYPE_ALIGN (atomic_type));
5602 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5603 /* Propagate structural equality. */
5604 SET_TYPE_STRUCTURAL_EQUALITY (t);
5605 else if (TYPE_CANONICAL (type) != type)
5606 /* Build the underlying canonical type, since it is different
5607 from TYPE. */
5609 tree c = build_qualified_type (TYPE_CANONICAL (type), type_quals);
5610 TYPE_CANONICAL (t) = TYPE_CANONICAL (c);
5612 else
5613 /* T is its own canonical type. */
5614 TYPE_CANONICAL (t) = t;
5618 return t;
5621 /* Create a variant of type T with alignment ALIGN. */
5623 tree
5624 build_aligned_type (tree type, unsigned int align)
5626 tree t;
5628 if (TYPE_PACKED (type)
5629 || TYPE_ALIGN (type) == align)
5630 return type;
5632 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5633 if (check_aligned_type (t, type, align))
5634 return t;
5636 t = build_variant_type_copy (type);
5637 SET_TYPE_ALIGN (t, align);
5638 TYPE_USER_ALIGN (t) = 1;
5640 return t;
5643 /* Create a new distinct copy of TYPE. The new type is made its own
5644 MAIN_VARIANT. If TYPE requires structural equality checks, the
5645 resulting type requires structural equality checks; otherwise, its
5646 TYPE_CANONICAL points to itself. */
5648 tree
5649 build_distinct_type_copy (tree type MEM_STAT_DECL)
5651 tree t = copy_node (type PASS_MEM_STAT);
5653 TYPE_POINTER_TO (t) = 0;
5654 TYPE_REFERENCE_TO (t) = 0;
5656 /* Set the canonical type either to a new equivalence class, or
5657 propagate the need for structural equality checks. */
5658 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5659 SET_TYPE_STRUCTURAL_EQUALITY (t);
5660 else
5661 TYPE_CANONICAL (t) = t;
5663 /* Make it its own variant. */
5664 TYPE_MAIN_VARIANT (t) = t;
5665 TYPE_NEXT_VARIANT (t) = 0;
5667 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
5668 whose TREE_TYPE is not t. This can also happen in the Ada
5669 frontend when using subtypes. */
5671 return t;
5674 /* Create a new variant of TYPE, equivalent but distinct. This is so
5675 the caller can modify it. TYPE_CANONICAL for the return type will
5676 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
5677 are considered equal by the language itself (or that both types
5678 require structural equality checks). */
5680 tree
5681 build_variant_type_copy (tree type MEM_STAT_DECL)
5683 tree t, m = TYPE_MAIN_VARIANT (type);
5685 t = build_distinct_type_copy (type PASS_MEM_STAT);
5687 /* Since we're building a variant, assume that it is a non-semantic
5688 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
5689 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
5690 /* Type variants have no alias set defined. */
5691 TYPE_ALIAS_SET (t) = -1;
5693 /* Add the new type to the chain of variants of TYPE. */
5694 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
5695 TYPE_NEXT_VARIANT (m) = t;
5696 TYPE_MAIN_VARIANT (t) = m;
5698 return t;
5701 /* Return true if the from tree in both tree maps are equal. */
5704 tree_map_base_eq (const void *va, const void *vb)
5706 const struct tree_map_base *const a = (const struct tree_map_base *) va,
5707 *const b = (const struct tree_map_base *) vb;
5708 return (a->from == b->from);
5711 /* Hash a from tree in a tree_base_map. */
5713 unsigned int
5714 tree_map_base_hash (const void *item)
5716 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
5719 /* Return true if this tree map structure is marked for garbage collection
5720 purposes. We simply return true if the from tree is marked, so that this
5721 structure goes away when the from tree goes away. */
5724 tree_map_base_marked_p (const void *p)
5726 return ggc_marked_p (((const struct tree_map_base *) p)->from);
5729 /* Hash a from tree in a tree_map. */
5731 unsigned int
5732 tree_map_hash (const void *item)
5734 return (((const struct tree_map *) item)->hash);
5737 /* Hash a from tree in a tree_decl_map. */
5739 unsigned int
5740 tree_decl_map_hash (const void *item)
5742 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
5745 /* Return the initialization priority for DECL. */
5747 priority_type
5748 decl_init_priority_lookup (tree decl)
5750 symtab_node *snode = symtab_node::get (decl);
5752 if (!snode)
5753 return DEFAULT_INIT_PRIORITY;
5754 return
5755 snode->get_init_priority ();
5758 /* Return the finalization priority for DECL. */
5760 priority_type
5761 decl_fini_priority_lookup (tree decl)
5763 cgraph_node *node = cgraph_node::get (decl);
5765 if (!node)
5766 return DEFAULT_INIT_PRIORITY;
5767 return
5768 node->get_fini_priority ();
5771 /* Set the initialization priority for DECL to PRIORITY. */
5773 void
5774 decl_init_priority_insert (tree decl, priority_type priority)
5776 struct symtab_node *snode;
5778 if (priority == DEFAULT_INIT_PRIORITY)
5780 snode = symtab_node::get (decl);
5781 if (!snode)
5782 return;
5784 else if (VAR_P (decl))
5785 snode = varpool_node::get_create (decl);
5786 else
5787 snode = cgraph_node::get_create (decl);
5788 snode->set_init_priority (priority);
5791 /* Set the finalization priority for DECL to PRIORITY. */
5793 void
5794 decl_fini_priority_insert (tree decl, priority_type priority)
5796 struct cgraph_node *node;
5798 if (priority == DEFAULT_INIT_PRIORITY)
5800 node = cgraph_node::get (decl);
5801 if (!node)
5802 return;
5804 else
5805 node = cgraph_node::get_create (decl);
5806 node->set_fini_priority (priority);
5809 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
5811 static void
5812 print_debug_expr_statistics (void)
5814 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
5815 (long) debug_expr_for_decl->size (),
5816 (long) debug_expr_for_decl->elements (),
5817 debug_expr_for_decl->collisions ());
5820 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
5822 static void
5823 print_value_expr_statistics (void)
5825 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
5826 (long) value_expr_for_decl->size (),
5827 (long) value_expr_for_decl->elements (),
5828 value_expr_for_decl->collisions ());
5831 /* Lookup a debug expression for FROM, and return it if we find one. */
5833 tree
5834 decl_debug_expr_lookup (tree from)
5836 struct tree_decl_map *h, in;
5837 in.base.from = from;
5839 h = debug_expr_for_decl->find_with_hash (&in, DECL_UID (from));
5840 if (h)
5841 return h->to;
5842 return NULL_TREE;
5845 /* Insert a mapping FROM->TO in the debug expression hashtable. */
5847 void
5848 decl_debug_expr_insert (tree from, tree to)
5850 struct tree_decl_map *h;
5852 h = ggc_alloc<tree_decl_map> ();
5853 h->base.from = from;
5854 h->to = to;
5855 *debug_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
5858 /* Lookup a value expression for FROM, and return it if we find one. */
5860 tree
5861 decl_value_expr_lookup (tree from)
5863 struct tree_decl_map *h, in;
5864 in.base.from = from;
5866 h = value_expr_for_decl->find_with_hash (&in, DECL_UID (from));
5867 if (h)
5868 return h->to;
5869 return NULL_TREE;
5872 /* Insert a mapping FROM->TO in the value expression hashtable. */
5874 void
5875 decl_value_expr_insert (tree from, tree to)
5877 struct tree_decl_map *h;
5879 h = ggc_alloc<tree_decl_map> ();
5880 h->base.from = from;
5881 h->to = to;
5882 *value_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
5885 /* Lookup a vector of debug arguments for FROM, and return it if we
5886 find one. */
5888 vec<tree, va_gc> **
5889 decl_debug_args_lookup (tree from)
5891 struct tree_vec_map *h, in;
5893 if (!DECL_HAS_DEBUG_ARGS_P (from))
5894 return NULL;
5895 gcc_checking_assert (debug_args_for_decl != NULL);
5896 in.base.from = from;
5897 h = debug_args_for_decl->find_with_hash (&in, DECL_UID (from));
5898 if (h)
5899 return &h->to;
5900 return NULL;
5903 /* Insert a mapping FROM->empty vector of debug arguments in the value
5904 expression hashtable. */
5906 vec<tree, va_gc> **
5907 decl_debug_args_insert (tree from)
5909 struct tree_vec_map *h;
5910 tree_vec_map **loc;
5912 if (DECL_HAS_DEBUG_ARGS_P (from))
5913 return decl_debug_args_lookup (from);
5914 if (debug_args_for_decl == NULL)
5915 debug_args_for_decl = hash_table<tree_vec_map_cache_hasher>::create_ggc (64);
5916 h = ggc_alloc<tree_vec_map> ();
5917 h->base.from = from;
5918 h->to = NULL;
5919 loc = debug_args_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT);
5920 *loc = h;
5921 DECL_HAS_DEBUG_ARGS_P (from) = 1;
5922 return &h->to;
5925 /* Hashing of types so that we don't make duplicates.
5926 The entry point is `type_hash_canon'. */
5928 /* Generate the default hash code for TYPE. This is designed for
5929 speed, rather than maximum entropy. */
5931 hashval_t
5932 type_hash_canon_hash (tree type)
5934 inchash::hash hstate;
5936 hstate.add_int (TREE_CODE (type));
5938 if (TREE_TYPE (type))
5939 hstate.add_object (TYPE_HASH (TREE_TYPE (type)));
5941 for (tree t = TYPE_ATTRIBUTES (type); t; t = TREE_CHAIN (t))
5942 /* Just the identifier is adequate to distinguish. */
5943 hstate.add_object (IDENTIFIER_HASH_VALUE (get_attribute_name (t)));
5945 switch (TREE_CODE (type))
5947 case METHOD_TYPE:
5948 hstate.add_object (TYPE_HASH (TYPE_METHOD_BASETYPE (type)));
5949 /* FALLTHROUGH. */
5950 case FUNCTION_TYPE:
5951 for (tree t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
5952 if (TREE_VALUE (t) != error_mark_node)
5953 hstate.add_object (TYPE_HASH (TREE_VALUE (t)));
5954 break;
5956 case OFFSET_TYPE:
5957 hstate.add_object (TYPE_HASH (TYPE_OFFSET_BASETYPE (type)));
5958 break;
5960 case ARRAY_TYPE:
5962 if (TYPE_DOMAIN (type))
5963 hstate.add_object (TYPE_HASH (TYPE_DOMAIN (type)));
5964 if (!AGGREGATE_TYPE_P (TREE_TYPE (type)))
5966 unsigned typeless = TYPE_TYPELESS_STORAGE (type);
5967 hstate.add_object (typeless);
5970 break;
5972 case INTEGER_TYPE:
5974 tree t = TYPE_MAX_VALUE (type);
5975 if (!t)
5976 t = TYPE_MIN_VALUE (type);
5977 for (int i = 0; i < TREE_INT_CST_NUNITS (t); i++)
5978 hstate.add_object (TREE_INT_CST_ELT (t, i));
5979 break;
5982 case REAL_TYPE:
5983 case FIXED_POINT_TYPE:
5985 unsigned prec = TYPE_PRECISION (type);
5986 hstate.add_object (prec);
5987 break;
5990 case VECTOR_TYPE:
5991 hstate.add_poly_int (TYPE_VECTOR_SUBPARTS (type));
5992 break;
5994 default:
5995 break;
5998 return hstate.end ();
6001 /* These are the Hashtable callback functions. */
6003 /* Returns true iff the types are equivalent. */
6005 bool
6006 type_cache_hasher::equal (type_hash *a, type_hash *b)
6008 /* First test the things that are the same for all types. */
6009 if (a->hash != b->hash
6010 || TREE_CODE (a->type) != TREE_CODE (b->type)
6011 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6012 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6013 TYPE_ATTRIBUTES (b->type))
6014 || (TREE_CODE (a->type) != COMPLEX_TYPE
6015 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6016 return 0;
6018 /* Be careful about comparing arrays before and after the element type
6019 has been completed; don't compare TYPE_ALIGN unless both types are
6020 complete. */
6021 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6022 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6023 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6024 return 0;
6026 switch (TREE_CODE (a->type))
6028 case VOID_TYPE:
6029 case OPAQUE_TYPE:
6030 case COMPLEX_TYPE:
6031 case POINTER_TYPE:
6032 case REFERENCE_TYPE:
6033 case NULLPTR_TYPE:
6034 return 1;
6036 case VECTOR_TYPE:
6037 return known_eq (TYPE_VECTOR_SUBPARTS (a->type),
6038 TYPE_VECTOR_SUBPARTS (b->type));
6040 case ENUMERAL_TYPE:
6041 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6042 && !(TYPE_VALUES (a->type)
6043 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6044 && TYPE_VALUES (b->type)
6045 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6046 && type_list_equal (TYPE_VALUES (a->type),
6047 TYPE_VALUES (b->type))))
6048 return 0;
6050 /* fall through */
6052 case INTEGER_TYPE:
6053 case REAL_TYPE:
6054 case BOOLEAN_TYPE:
6055 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
6056 return false;
6057 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6058 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6059 TYPE_MAX_VALUE (b->type)))
6060 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6061 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6062 TYPE_MIN_VALUE (b->type))));
6064 case FIXED_POINT_TYPE:
6065 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6067 case OFFSET_TYPE:
6068 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6070 case METHOD_TYPE:
6071 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6072 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6073 || (TYPE_ARG_TYPES (a->type)
6074 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6075 && TYPE_ARG_TYPES (b->type)
6076 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6077 && type_list_equal (TYPE_ARG_TYPES (a->type),
6078 TYPE_ARG_TYPES (b->type)))))
6079 break;
6080 return 0;
6081 case ARRAY_TYPE:
6082 /* Don't compare TYPE_TYPELESS_STORAGE flag on aggregates,
6083 where the flag should be inherited from the element type
6084 and can change after ARRAY_TYPEs are created; on non-aggregates
6085 compare it and hash it, scalars will never have that flag set
6086 and we need to differentiate between arrays created by different
6087 front-ends or middle-end created arrays. */
6088 return (TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type)
6089 && (AGGREGATE_TYPE_P (TREE_TYPE (a->type))
6090 || (TYPE_TYPELESS_STORAGE (a->type)
6091 == TYPE_TYPELESS_STORAGE (b->type))));
6093 case RECORD_TYPE:
6094 case UNION_TYPE:
6095 case QUAL_UNION_TYPE:
6096 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6097 || (TYPE_FIELDS (a->type)
6098 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6099 && TYPE_FIELDS (b->type)
6100 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6101 && type_list_equal (TYPE_FIELDS (a->type),
6102 TYPE_FIELDS (b->type))));
6104 case FUNCTION_TYPE:
6105 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6106 || (TYPE_ARG_TYPES (a->type)
6107 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6108 && TYPE_ARG_TYPES (b->type)
6109 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6110 && type_list_equal (TYPE_ARG_TYPES (a->type),
6111 TYPE_ARG_TYPES (b->type))))
6112 break;
6113 return 0;
6115 default:
6116 return 0;
6119 if (lang_hooks.types.type_hash_eq != NULL)
6120 return lang_hooks.types.type_hash_eq (a->type, b->type);
6122 return 1;
6125 /* Given TYPE, and HASHCODE its hash code, return the canonical
6126 object for an identical type if one already exists.
6127 Otherwise, return TYPE, and record it as the canonical object.
6129 To use this function, first create a type of the sort you want.
6130 Then compute its hash code from the fields of the type that
6131 make it different from other similar types.
6132 Then call this function and use the value. */
6134 tree
6135 type_hash_canon (unsigned int hashcode, tree type)
6137 type_hash in;
6138 type_hash **loc;
6140 /* The hash table only contains main variants, so ensure that's what we're
6141 being passed. */
6142 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6144 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6145 must call that routine before comparing TYPE_ALIGNs. */
6146 layout_type (type);
6148 in.hash = hashcode;
6149 in.type = type;
6151 loc = type_hash_table->find_slot_with_hash (&in, hashcode, INSERT);
6152 if (*loc)
6154 tree t1 = ((type_hash *) *loc)->type;
6155 gcc_assert (TYPE_MAIN_VARIANT (t1) == t1
6156 && t1 != type);
6157 if (TYPE_UID (type) + 1 == next_type_uid)
6158 --next_type_uid;
6159 /* Free also min/max values and the cache for integer
6160 types. This can't be done in free_node, as LTO frees
6161 those on its own. */
6162 if (TREE_CODE (type) == INTEGER_TYPE)
6164 if (TYPE_MIN_VALUE (type)
6165 && TREE_TYPE (TYPE_MIN_VALUE (type)) == type)
6167 /* Zero is always in TYPE_CACHED_VALUES. */
6168 if (! TYPE_UNSIGNED (type))
6169 int_cst_hash_table->remove_elt (TYPE_MIN_VALUE (type));
6170 ggc_free (TYPE_MIN_VALUE (type));
6172 if (TYPE_MAX_VALUE (type)
6173 && TREE_TYPE (TYPE_MAX_VALUE (type)) == type)
6175 int_cst_hash_table->remove_elt (TYPE_MAX_VALUE (type));
6176 ggc_free (TYPE_MAX_VALUE (type));
6178 if (TYPE_CACHED_VALUES_P (type))
6179 ggc_free (TYPE_CACHED_VALUES (type));
6181 free_node (type);
6182 return t1;
6184 else
6186 struct type_hash *h;
6188 h = ggc_alloc<type_hash> ();
6189 h->hash = hashcode;
6190 h->type = type;
6191 *loc = h;
6193 return type;
6197 static void
6198 print_type_hash_statistics (void)
6200 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6201 (long) type_hash_table->size (),
6202 (long) type_hash_table->elements (),
6203 type_hash_table->collisions ());
6206 /* Given two lists of types
6207 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6208 return 1 if the lists contain the same types in the same order.
6209 Also, the TREE_PURPOSEs must match. */
6211 bool
6212 type_list_equal (const_tree l1, const_tree l2)
6214 const_tree t1, t2;
6216 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6217 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6218 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6219 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6220 && (TREE_TYPE (TREE_PURPOSE (t1))
6221 == TREE_TYPE (TREE_PURPOSE (t2))))))
6222 return false;
6224 return t1 == t2;
6227 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6228 given by TYPE. If the argument list accepts variable arguments,
6229 then this function counts only the ordinary arguments. */
6232 type_num_arguments (const_tree fntype)
6234 int i = 0;
6236 for (tree t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
6237 /* If the function does not take a variable number of arguments,
6238 the last element in the list will have type `void'. */
6239 if (VOID_TYPE_P (TREE_VALUE (t)))
6240 break;
6241 else
6242 ++i;
6244 return i;
6247 /* Return the type of the function TYPE's argument ARGNO if known.
6248 For vararg function's where ARGNO refers to one of the variadic
6249 arguments return null. Otherwise, return a void_type_node for
6250 out-of-bounds ARGNO. */
6252 tree
6253 type_argument_type (const_tree fntype, unsigned argno)
6255 /* Treat zero the same as an out-of-bounds argument number. */
6256 if (!argno)
6257 return void_type_node;
6259 function_args_iterator iter;
6261 tree argtype;
6262 unsigned i = 1;
6263 FOREACH_FUNCTION_ARGS (fntype, argtype, iter)
6265 /* A vararg function's argument list ends in a null. Otherwise,
6266 an ordinary function's argument list ends with void. Return
6267 null if ARGNO refers to a vararg argument, void_type_node if
6268 it's out of bounds, and the formal argument type otherwise. */
6269 if (!argtype)
6270 break;
6272 if (i == argno || VOID_TYPE_P (argtype))
6273 return argtype;
6275 ++i;
6278 return NULL_TREE;
6281 /* Nonzero if integer constants T1 and T2
6282 represent the same constant value. */
6285 tree_int_cst_equal (const_tree t1, const_tree t2)
6287 if (t1 == t2)
6288 return 1;
6290 if (t1 == 0 || t2 == 0)
6291 return 0;
6293 STRIP_ANY_LOCATION_WRAPPER (t1);
6294 STRIP_ANY_LOCATION_WRAPPER (t2);
6296 if (TREE_CODE (t1) == INTEGER_CST
6297 && TREE_CODE (t2) == INTEGER_CST
6298 && wi::to_widest (t1) == wi::to_widest (t2))
6299 return 1;
6301 return 0;
6304 /* Return true if T is an INTEGER_CST whose numerical value (extended
6305 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
6307 bool
6308 tree_fits_shwi_p (const_tree t)
6310 return (t != NULL_TREE
6311 && TREE_CODE (t) == INTEGER_CST
6312 && wi::fits_shwi_p (wi::to_widest (t)));
6315 /* Return true if T is an INTEGER_CST or POLY_INT_CST whose numerical
6316 value (extended according to TYPE_UNSIGNED) fits in a poly_int64. */
6318 bool
6319 tree_fits_poly_int64_p (const_tree t)
6321 if (t == NULL_TREE)
6322 return false;
6323 if (POLY_INT_CST_P (t))
6325 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; i++)
6326 if (!wi::fits_shwi_p (wi::to_wide (POLY_INT_CST_COEFF (t, i))))
6327 return false;
6328 return true;
6330 return (TREE_CODE (t) == INTEGER_CST
6331 && wi::fits_shwi_p (wi::to_widest (t)));
6334 /* Return true if T is an INTEGER_CST whose numerical value (extended
6335 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
6337 bool
6338 tree_fits_uhwi_p (const_tree t)
6340 return (t != NULL_TREE
6341 && TREE_CODE (t) == INTEGER_CST
6342 && wi::fits_uhwi_p (wi::to_widest (t)));
6345 /* Return true if T is an INTEGER_CST or POLY_INT_CST whose numerical
6346 value (extended according to TYPE_UNSIGNED) fits in a poly_uint64. */
6348 bool
6349 tree_fits_poly_uint64_p (const_tree t)
6351 if (t == NULL_TREE)
6352 return false;
6353 if (POLY_INT_CST_P (t))
6355 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; i++)
6356 if (!wi::fits_uhwi_p (wi::to_widest (POLY_INT_CST_COEFF (t, i))))
6357 return false;
6358 return true;
6360 return (TREE_CODE (t) == INTEGER_CST
6361 && wi::fits_uhwi_p (wi::to_widest (t)));
6364 /* T is an INTEGER_CST whose numerical value (extended according to
6365 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
6366 HOST_WIDE_INT. */
6368 HOST_WIDE_INT
6369 tree_to_shwi (const_tree t)
6371 gcc_assert (tree_fits_shwi_p (t));
6372 return TREE_INT_CST_LOW (t);
6375 /* T is an INTEGER_CST whose numerical value (extended according to
6376 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
6377 HOST_WIDE_INT. */
6379 unsigned HOST_WIDE_INT
6380 tree_to_uhwi (const_tree t)
6382 gcc_assert (tree_fits_uhwi_p (t));
6383 return TREE_INT_CST_LOW (t);
6386 /* Return the most significant (sign) bit of T. */
6389 tree_int_cst_sign_bit (const_tree t)
6391 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
6393 return wi::extract_uhwi (wi::to_wide (t), bitno, 1);
6396 /* Return an indication of the sign of the integer constant T.
6397 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
6398 Note that -1 will never be returned if T's type is unsigned. */
6401 tree_int_cst_sgn (const_tree t)
6403 if (wi::to_wide (t) == 0)
6404 return 0;
6405 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
6406 return 1;
6407 else if (wi::neg_p (wi::to_wide (t)))
6408 return -1;
6409 else
6410 return 1;
6413 /* Return the minimum number of bits needed to represent VALUE in a
6414 signed or unsigned type, UNSIGNEDP says which. */
6416 unsigned int
6417 tree_int_cst_min_precision (tree value, signop sgn)
6419 /* If the value is negative, compute its negative minus 1. The latter
6420 adjustment is because the absolute value of the largest negative value
6421 is one larger than the largest positive value. This is equivalent to
6422 a bit-wise negation, so use that operation instead. */
6424 if (tree_int_cst_sgn (value) < 0)
6425 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
6427 /* Return the number of bits needed, taking into account the fact
6428 that we need one more bit for a signed than unsigned type.
6429 If value is 0 or -1, the minimum precision is 1 no matter
6430 whether unsignedp is true or false. */
6432 if (integer_zerop (value))
6433 return 1;
6434 else
6435 return tree_floor_log2 (value) + 1 + (sgn == SIGNED ? 1 : 0) ;
6438 /* Return truthvalue of whether T1 is the same tree structure as T2.
6439 Return 1 if they are the same.
6440 Return 0 if they are understandably different.
6441 Return -1 if either contains tree structure not understood by
6442 this function. */
6445 simple_cst_equal (const_tree t1, const_tree t2)
6447 enum tree_code code1, code2;
6448 int cmp;
6449 int i;
6451 if (t1 == t2)
6452 return 1;
6453 if (t1 == 0 || t2 == 0)
6454 return 0;
6456 /* For location wrappers to be the same, they must be at the same
6457 source location (and wrap the same thing). */
6458 if (location_wrapper_p (t1) && location_wrapper_p (t2))
6460 if (EXPR_LOCATION (t1) != EXPR_LOCATION (t2))
6461 return 0;
6462 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6465 code1 = TREE_CODE (t1);
6466 code2 = TREE_CODE (t2);
6468 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
6470 if (CONVERT_EXPR_CODE_P (code2)
6471 || code2 == NON_LVALUE_EXPR)
6472 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6473 else
6474 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
6477 else if (CONVERT_EXPR_CODE_P (code2)
6478 || code2 == NON_LVALUE_EXPR)
6479 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
6481 if (code1 != code2)
6482 return 0;
6484 switch (code1)
6486 case INTEGER_CST:
6487 return wi::to_widest (t1) == wi::to_widest (t2);
6489 case REAL_CST:
6490 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
6492 case FIXED_CST:
6493 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
6495 case STRING_CST:
6496 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
6497 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
6498 TREE_STRING_LENGTH (t1)));
6500 case CONSTRUCTOR:
6502 unsigned HOST_WIDE_INT idx;
6503 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
6504 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
6506 if (vec_safe_length (v1) != vec_safe_length (v2))
6507 return false;
6509 for (idx = 0; idx < vec_safe_length (v1); ++idx)
6510 /* ??? Should we handle also fields here? */
6511 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
6512 return false;
6513 return true;
6516 case SAVE_EXPR:
6517 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6519 case CALL_EXPR:
6520 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
6521 if (cmp <= 0)
6522 return cmp;
6523 if (call_expr_nargs (t1) != call_expr_nargs (t2))
6524 return 0;
6526 const_tree arg1, arg2;
6527 const_call_expr_arg_iterator iter1, iter2;
6528 for (arg1 = first_const_call_expr_arg (t1, &iter1),
6529 arg2 = first_const_call_expr_arg (t2, &iter2);
6530 arg1 && arg2;
6531 arg1 = next_const_call_expr_arg (&iter1),
6532 arg2 = next_const_call_expr_arg (&iter2))
6534 cmp = simple_cst_equal (arg1, arg2);
6535 if (cmp <= 0)
6536 return cmp;
6538 return arg1 == arg2;
6541 case TARGET_EXPR:
6542 /* Special case: if either target is an unallocated VAR_DECL,
6543 it means that it's going to be unified with whatever the
6544 TARGET_EXPR is really supposed to initialize, so treat it
6545 as being equivalent to anything. */
6546 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
6547 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
6548 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
6549 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
6550 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
6551 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
6552 cmp = 1;
6553 else
6554 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6556 if (cmp <= 0)
6557 return cmp;
6559 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
6561 case WITH_CLEANUP_EXPR:
6562 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6563 if (cmp <= 0)
6564 return cmp;
6566 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
6568 case COMPONENT_REF:
6569 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
6570 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6572 return 0;
6574 case VAR_DECL:
6575 case PARM_DECL:
6576 case CONST_DECL:
6577 case FUNCTION_DECL:
6578 return 0;
6580 default:
6581 if (POLY_INT_CST_P (t1))
6582 /* A false return means maybe_ne rather than known_ne. */
6583 return known_eq (poly_widest_int::from (poly_int_cst_value (t1),
6584 TYPE_SIGN (TREE_TYPE (t1))),
6585 poly_widest_int::from (poly_int_cst_value (t2),
6586 TYPE_SIGN (TREE_TYPE (t2))));
6587 break;
6590 /* This general rule works for most tree codes. All exceptions should be
6591 handled above. If this is a language-specific tree code, we can't
6592 trust what might be in the operand, so say we don't know
6593 the situation. */
6594 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
6595 return -1;
6597 switch (TREE_CODE_CLASS (code1))
6599 case tcc_unary:
6600 case tcc_binary:
6601 case tcc_comparison:
6602 case tcc_expression:
6603 case tcc_reference:
6604 case tcc_statement:
6605 cmp = 1;
6606 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
6608 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
6609 if (cmp <= 0)
6610 return cmp;
6613 return cmp;
6615 default:
6616 return -1;
6620 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
6621 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
6622 than U, respectively. */
6625 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
6627 if (tree_int_cst_sgn (t) < 0)
6628 return -1;
6629 else if (!tree_fits_uhwi_p (t))
6630 return 1;
6631 else if (TREE_INT_CST_LOW (t) == u)
6632 return 0;
6633 else if (TREE_INT_CST_LOW (t) < u)
6634 return -1;
6635 else
6636 return 1;
6639 /* Return true if SIZE represents a constant size that is in bounds of
6640 what the middle-end and the backend accepts (covering not more than
6641 half of the address-space).
6642 When PERR is non-null, set *PERR on failure to the description of
6643 why SIZE is not valid. */
6645 bool
6646 valid_constant_size_p (const_tree size, cst_size_error *perr /* = NULL */)
6648 if (POLY_INT_CST_P (size))
6650 if (TREE_OVERFLOW (size))
6651 return false;
6652 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
6653 if (!valid_constant_size_p (POLY_INT_CST_COEFF (size, i)))
6654 return false;
6655 return true;
6658 cst_size_error error;
6659 if (!perr)
6660 perr = &error;
6662 if (TREE_CODE (size) != INTEGER_CST)
6664 *perr = cst_size_not_constant;
6665 return false;
6668 if (TREE_OVERFLOW_P (size))
6670 *perr = cst_size_overflow;
6671 return false;
6674 if (tree_int_cst_sgn (size) < 0)
6676 *perr = cst_size_negative;
6677 return false;
6679 if (!tree_fits_uhwi_p (size)
6680 || (wi::to_widest (TYPE_MAX_VALUE (sizetype))
6681 < wi::to_widest (size) * 2))
6683 *perr = cst_size_too_big;
6684 return false;
6687 return true;
6690 /* Return the precision of the type, or for a complex or vector type the
6691 precision of the type of its elements. */
6693 unsigned int
6694 element_precision (const_tree type)
6696 if (!TYPE_P (type))
6697 type = TREE_TYPE (type);
6698 enum tree_code code = TREE_CODE (type);
6699 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
6700 type = TREE_TYPE (type);
6702 return TYPE_PRECISION (type);
6705 /* Return true if CODE represents an associative tree code. Otherwise
6706 return false. */
6707 bool
6708 associative_tree_code (enum tree_code code)
6710 switch (code)
6712 case BIT_IOR_EXPR:
6713 case BIT_AND_EXPR:
6714 case BIT_XOR_EXPR:
6715 case PLUS_EXPR:
6716 case MULT_EXPR:
6717 case MIN_EXPR:
6718 case MAX_EXPR:
6719 return true;
6721 default:
6722 break;
6724 return false;
6727 /* Return true if CODE represents a commutative tree code. Otherwise
6728 return false. */
6729 bool
6730 commutative_tree_code (enum tree_code code)
6732 switch (code)
6734 case PLUS_EXPR:
6735 case MULT_EXPR:
6736 case MULT_HIGHPART_EXPR:
6737 case MIN_EXPR:
6738 case MAX_EXPR:
6739 case BIT_IOR_EXPR:
6740 case BIT_XOR_EXPR:
6741 case BIT_AND_EXPR:
6742 case NE_EXPR:
6743 case EQ_EXPR:
6744 case UNORDERED_EXPR:
6745 case ORDERED_EXPR:
6746 case UNEQ_EXPR:
6747 case LTGT_EXPR:
6748 case TRUTH_AND_EXPR:
6749 case TRUTH_XOR_EXPR:
6750 case TRUTH_OR_EXPR:
6751 case WIDEN_MULT_EXPR:
6752 case VEC_WIDEN_MULT_HI_EXPR:
6753 case VEC_WIDEN_MULT_LO_EXPR:
6754 case VEC_WIDEN_MULT_EVEN_EXPR:
6755 case VEC_WIDEN_MULT_ODD_EXPR:
6756 return true;
6758 default:
6759 break;
6761 return false;
6764 /* Return true if CODE represents a ternary tree code for which the
6765 first two operands are commutative. Otherwise return false. */
6766 bool
6767 commutative_ternary_tree_code (enum tree_code code)
6769 switch (code)
6771 case WIDEN_MULT_PLUS_EXPR:
6772 case WIDEN_MULT_MINUS_EXPR:
6773 case DOT_PROD_EXPR:
6774 return true;
6776 default:
6777 break;
6779 return false;
6782 /* Returns true if CODE can overflow. */
6784 bool
6785 operation_can_overflow (enum tree_code code)
6787 switch (code)
6789 case PLUS_EXPR:
6790 case MINUS_EXPR:
6791 case MULT_EXPR:
6792 case LSHIFT_EXPR:
6793 /* Can overflow in various ways. */
6794 return true;
6795 case TRUNC_DIV_EXPR:
6796 case EXACT_DIV_EXPR:
6797 case FLOOR_DIV_EXPR:
6798 case CEIL_DIV_EXPR:
6799 /* For INT_MIN / -1. */
6800 return true;
6801 case NEGATE_EXPR:
6802 case ABS_EXPR:
6803 /* For -INT_MIN. */
6804 return true;
6805 default:
6806 /* These operators cannot overflow. */
6807 return false;
6811 /* Returns true if CODE operating on operands of type TYPE doesn't overflow, or
6812 ftrapv doesn't generate trapping insns for CODE. */
6814 bool
6815 operation_no_trapping_overflow (tree type, enum tree_code code)
6817 gcc_checking_assert (ANY_INTEGRAL_TYPE_P (type));
6819 /* We don't generate instructions that trap on overflow for complex or vector
6820 types. */
6821 if (!INTEGRAL_TYPE_P (type))
6822 return true;
6824 if (!TYPE_OVERFLOW_TRAPS (type))
6825 return true;
6827 switch (code)
6829 case PLUS_EXPR:
6830 case MINUS_EXPR:
6831 case MULT_EXPR:
6832 case NEGATE_EXPR:
6833 case ABS_EXPR:
6834 /* These operators can overflow, and -ftrapv generates trapping code for
6835 these. */
6836 return false;
6837 case TRUNC_DIV_EXPR:
6838 case EXACT_DIV_EXPR:
6839 case FLOOR_DIV_EXPR:
6840 case CEIL_DIV_EXPR:
6841 case LSHIFT_EXPR:
6842 /* These operators can overflow, but -ftrapv does not generate trapping
6843 code for these. */
6844 return true;
6845 default:
6846 /* These operators cannot overflow. */
6847 return true;
6851 /* Constructors for pointer, array and function types.
6852 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
6853 constructed by language-dependent code, not here.) */
6855 /* Construct, lay out and return the type of pointers to TO_TYPE with
6856 mode MODE. If MODE is VOIDmode, a pointer mode for the address
6857 space of TO_TYPE will be picked. If CAN_ALIAS_ALL is TRUE,
6858 indicate this type can reference all of memory. If such a type has
6859 already been constructed, reuse it. */
6861 tree
6862 build_pointer_type_for_mode (tree to_type, machine_mode mode,
6863 bool can_alias_all)
6865 tree t;
6866 bool could_alias = can_alias_all;
6868 if (to_type == error_mark_node)
6869 return error_mark_node;
6871 if (mode == VOIDmode)
6873 addr_space_t as = TYPE_ADDR_SPACE (to_type);
6874 mode = targetm.addr_space.pointer_mode (as);
6877 /* If the pointed-to type has the may_alias attribute set, force
6878 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
6879 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
6880 can_alias_all = true;
6882 /* In some cases, languages will have things that aren't a POINTER_TYPE
6883 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
6884 In that case, return that type without regard to the rest of our
6885 operands.
6887 ??? This is a kludge, but consistent with the way this function has
6888 always operated and there doesn't seem to be a good way to avoid this
6889 at the moment. */
6890 if (TYPE_POINTER_TO (to_type) != 0
6891 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
6892 return TYPE_POINTER_TO (to_type);
6894 /* First, if we already have a type for pointers to TO_TYPE and it's
6895 the proper mode, use it. */
6896 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
6897 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
6898 return t;
6900 t = make_node (POINTER_TYPE);
6902 TREE_TYPE (t) = to_type;
6903 SET_TYPE_MODE (t, mode);
6904 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
6905 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
6906 TYPE_POINTER_TO (to_type) = t;
6908 /* During LTO we do not set TYPE_CANONICAL of pointers and references. */
6909 if (TYPE_STRUCTURAL_EQUALITY_P (to_type) || in_lto_p)
6910 SET_TYPE_STRUCTURAL_EQUALITY (t);
6911 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
6912 TYPE_CANONICAL (t)
6913 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
6914 mode, false);
6916 /* Lay out the type. This function has many callers that are concerned
6917 with expression-construction, and this simplifies them all. */
6918 layout_type (t);
6920 return t;
6923 /* By default build pointers in ptr_mode. */
6925 tree
6926 build_pointer_type (tree to_type)
6928 return build_pointer_type_for_mode (to_type, VOIDmode, false);
6931 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
6933 tree
6934 build_reference_type_for_mode (tree to_type, machine_mode mode,
6935 bool can_alias_all)
6937 tree t;
6938 bool could_alias = can_alias_all;
6940 if (to_type == error_mark_node)
6941 return error_mark_node;
6943 if (mode == VOIDmode)
6945 addr_space_t as = TYPE_ADDR_SPACE (to_type);
6946 mode = targetm.addr_space.pointer_mode (as);
6949 /* If the pointed-to type has the may_alias attribute set, force
6950 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
6951 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
6952 can_alias_all = true;
6954 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
6955 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
6956 In that case, return that type without regard to the rest of our
6957 operands.
6959 ??? This is a kludge, but consistent with the way this function has
6960 always operated and there doesn't seem to be a good way to avoid this
6961 at the moment. */
6962 if (TYPE_REFERENCE_TO (to_type) != 0
6963 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
6964 return TYPE_REFERENCE_TO (to_type);
6966 /* First, if we already have a type for pointers to TO_TYPE and it's
6967 the proper mode, use it. */
6968 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
6969 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
6970 return t;
6972 t = make_node (REFERENCE_TYPE);
6974 TREE_TYPE (t) = to_type;
6975 SET_TYPE_MODE (t, mode);
6976 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
6977 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
6978 TYPE_REFERENCE_TO (to_type) = t;
6980 /* During LTO we do not set TYPE_CANONICAL of pointers and references. */
6981 if (TYPE_STRUCTURAL_EQUALITY_P (to_type) || in_lto_p)
6982 SET_TYPE_STRUCTURAL_EQUALITY (t);
6983 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
6984 TYPE_CANONICAL (t)
6985 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
6986 mode, false);
6988 layout_type (t);
6990 return t;
6994 /* Build the node for the type of references-to-TO_TYPE by default
6995 in ptr_mode. */
6997 tree
6998 build_reference_type (tree to_type)
7000 return build_reference_type_for_mode (to_type, VOIDmode, false);
7003 #define MAX_INT_CACHED_PREC \
7004 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7005 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7007 static void
7008 clear_nonstandard_integer_type_cache (void)
7010 for (size_t i = 0 ; i < 2 * MAX_INT_CACHED_PREC + 2 ; i++)
7012 nonstandard_integer_type_cache[i] = NULL;
7016 /* Builds a signed or unsigned integer type of precision PRECISION.
7017 Used for C bitfields whose precision does not match that of
7018 built-in target types. */
7019 tree
7020 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7021 int unsignedp)
7023 tree itype, ret;
7025 if (unsignedp)
7026 unsignedp = MAX_INT_CACHED_PREC + 1;
7028 if (precision <= MAX_INT_CACHED_PREC)
7030 itype = nonstandard_integer_type_cache[precision + unsignedp];
7031 if (itype)
7032 return itype;
7035 itype = make_node (INTEGER_TYPE);
7036 TYPE_PRECISION (itype) = precision;
7038 if (unsignedp)
7039 fixup_unsigned_type (itype);
7040 else
7041 fixup_signed_type (itype);
7043 inchash::hash hstate;
7044 inchash::add_expr (TYPE_MAX_VALUE (itype), hstate);
7045 ret = type_hash_canon (hstate.end (), itype);
7046 if (precision <= MAX_INT_CACHED_PREC)
7047 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7049 return ret;
7052 #define MAX_BOOL_CACHED_PREC \
7053 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7054 static GTY(()) tree nonstandard_boolean_type_cache[MAX_BOOL_CACHED_PREC + 1];
7056 /* Builds a boolean type of precision PRECISION.
7057 Used for boolean vectors to choose proper vector element size. */
7058 tree
7059 build_nonstandard_boolean_type (unsigned HOST_WIDE_INT precision)
7061 tree type;
7063 if (precision <= MAX_BOOL_CACHED_PREC)
7065 type = nonstandard_boolean_type_cache[precision];
7066 if (type)
7067 return type;
7070 type = make_node (BOOLEAN_TYPE);
7071 TYPE_PRECISION (type) = precision;
7072 fixup_signed_type (type);
7074 if (precision <= MAX_INT_CACHED_PREC)
7075 nonstandard_boolean_type_cache[precision] = type;
7077 return type;
7080 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7081 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7082 is true, reuse such a type that has already been constructed. */
7084 static tree
7085 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7087 tree itype = make_node (INTEGER_TYPE);
7089 TREE_TYPE (itype) = type;
7091 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7092 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7094 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7095 SET_TYPE_MODE (itype, TYPE_MODE (type));
7096 TYPE_SIZE (itype) = TYPE_SIZE (type);
7097 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7098 SET_TYPE_ALIGN (itype, TYPE_ALIGN (type));
7099 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7100 SET_TYPE_WARN_IF_NOT_ALIGN (itype, TYPE_WARN_IF_NOT_ALIGN (type));
7102 if (!shared)
7103 return itype;
7105 if ((TYPE_MIN_VALUE (itype)
7106 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7107 || (TYPE_MAX_VALUE (itype)
7108 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7110 /* Since we cannot reliably merge this type, we need to compare it using
7111 structural equality checks. */
7112 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7113 return itype;
7116 hashval_t hash = type_hash_canon_hash (itype);
7117 itype = type_hash_canon (hash, itype);
7119 return itype;
7122 /* Wrapper around build_range_type_1 with SHARED set to true. */
7124 tree
7125 build_range_type (tree type, tree lowval, tree highval)
7127 return build_range_type_1 (type, lowval, highval, true);
7130 /* Wrapper around build_range_type_1 with SHARED set to false. */
7132 tree
7133 build_nonshared_range_type (tree type, tree lowval, tree highval)
7135 return build_range_type_1 (type, lowval, highval, false);
7138 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7139 MAXVAL should be the maximum value in the domain
7140 (one less than the length of the array).
7142 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7143 We don't enforce this limit, that is up to caller (e.g. language front end).
7144 The limit exists because the result is a signed type and we don't handle
7145 sizes that use more than one HOST_WIDE_INT. */
7147 tree
7148 build_index_type (tree maxval)
7150 return build_range_type (sizetype, size_zero_node, maxval);
7153 /* Return true if the debug information for TYPE, a subtype, should be emitted
7154 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7155 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7156 debug info and doesn't reflect the source code. */
7158 bool
7159 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7161 tree base_type = TREE_TYPE (type), low, high;
7163 /* Subrange types have a base type which is an integral type. */
7164 if (!INTEGRAL_TYPE_P (base_type))
7165 return false;
7167 /* Get the real bounds of the subtype. */
7168 if (lang_hooks.types.get_subrange_bounds)
7169 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7170 else
7172 low = TYPE_MIN_VALUE (type);
7173 high = TYPE_MAX_VALUE (type);
7176 /* If the type and its base type have the same representation and the same
7177 name, then the type is not a subrange but a copy of the base type. */
7178 if ((TREE_CODE (base_type) == INTEGER_TYPE
7179 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7180 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7181 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7182 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type))
7183 && TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (base_type))
7184 return false;
7186 if (lowval)
7187 *lowval = low;
7188 if (highval)
7189 *highval = high;
7190 return true;
7193 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7194 and number of elements specified by the range of values of INDEX_TYPE.
7195 If TYPELESS_STORAGE is true, TYPE_TYPELESS_STORAGE flag is set on the type.
7196 If SHARED is true, reuse such a type that has already been constructed.
7197 If SET_CANONICAL is true, compute TYPE_CANONICAL from the element type. */
7199 tree
7200 build_array_type_1 (tree elt_type, tree index_type, bool typeless_storage,
7201 bool shared, bool set_canonical)
7203 tree t;
7205 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7207 error ("arrays of functions are not meaningful");
7208 elt_type = integer_type_node;
7211 t = make_node (ARRAY_TYPE);
7212 TREE_TYPE (t) = elt_type;
7213 TYPE_DOMAIN (t) = index_type;
7214 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7215 TYPE_TYPELESS_STORAGE (t) = typeless_storage;
7216 layout_type (t);
7218 if (shared)
7220 hashval_t hash = type_hash_canon_hash (t);
7221 t = type_hash_canon (hash, t);
7224 if (TYPE_CANONICAL (t) == t && set_canonical)
7226 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7227 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type))
7228 || in_lto_p)
7229 SET_TYPE_STRUCTURAL_EQUALITY (t);
7230 else if (TYPE_CANONICAL (elt_type) != elt_type
7231 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7232 TYPE_CANONICAL (t)
7233 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7234 index_type
7235 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7236 typeless_storage, shared, set_canonical);
7239 return t;
7242 /* Wrapper around build_array_type_1 with SHARED set to true. */
7244 tree
7245 build_array_type (tree elt_type, tree index_type, bool typeless_storage)
7247 return
7248 build_array_type_1 (elt_type, index_type, typeless_storage, true, true);
7251 /* Wrapper around build_array_type_1 with SHARED set to false. */
7253 tree
7254 build_nonshared_array_type (tree elt_type, tree index_type)
7256 return build_array_type_1 (elt_type, index_type, false, false, true);
7259 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7260 sizetype. */
7262 tree
7263 build_array_type_nelts (tree elt_type, poly_uint64 nelts)
7265 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7268 /* Recursively examines the array elements of TYPE, until a non-array
7269 element type is found. */
7271 tree
7272 strip_array_types (tree type)
7274 while (TREE_CODE (type) == ARRAY_TYPE)
7275 type = TREE_TYPE (type);
7277 return type;
7280 /* Computes the canonical argument types from the argument type list
7281 ARGTYPES.
7283 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7284 on entry to this function, or if any of the ARGTYPES are
7285 structural.
7287 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7288 true on entry to this function, or if any of the ARGTYPES are
7289 non-canonical.
7291 Returns a canonical argument list, which may be ARGTYPES when the
7292 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7293 true) or would not differ from ARGTYPES. */
7295 static tree
7296 maybe_canonicalize_argtypes (tree argtypes,
7297 bool *any_structural_p,
7298 bool *any_noncanonical_p)
7300 tree arg;
7301 bool any_noncanonical_argtypes_p = false;
7303 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7305 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7306 /* Fail gracefully by stating that the type is structural. */
7307 *any_structural_p = true;
7308 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7309 *any_structural_p = true;
7310 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7311 || TREE_PURPOSE (arg))
7312 /* If the argument has a default argument, we consider it
7313 non-canonical even though the type itself is canonical.
7314 That way, different variants of function and method types
7315 with default arguments will all point to the variant with
7316 no defaults as their canonical type. */
7317 any_noncanonical_argtypes_p = true;
7320 if (*any_structural_p)
7321 return argtypes;
7323 if (any_noncanonical_argtypes_p)
7325 /* Build the canonical list of argument types. */
7326 tree canon_argtypes = NULL_TREE;
7327 bool is_void = false;
7329 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7331 if (arg == void_list_node)
7332 is_void = true;
7333 else
7334 canon_argtypes = tree_cons (NULL_TREE,
7335 TYPE_CANONICAL (TREE_VALUE (arg)),
7336 canon_argtypes);
7339 canon_argtypes = nreverse (canon_argtypes);
7340 if (is_void)
7341 canon_argtypes = chainon (canon_argtypes, void_list_node);
7343 /* There is a non-canonical type. */
7344 *any_noncanonical_p = true;
7345 return canon_argtypes;
7348 /* The canonical argument types are the same as ARGTYPES. */
7349 return argtypes;
7352 /* Construct, lay out and return
7353 the type of functions returning type VALUE_TYPE
7354 given arguments of types ARG_TYPES.
7355 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7356 are data type nodes for the arguments of the function.
7357 If such a type has already been constructed, reuse it. */
7359 tree
7360 build_function_type (tree value_type, tree arg_types)
7362 tree t;
7363 inchash::hash hstate;
7364 bool any_structural_p, any_noncanonical_p;
7365 tree canon_argtypes;
7367 gcc_assert (arg_types != error_mark_node);
7369 if (TREE_CODE (value_type) == FUNCTION_TYPE)
7371 error ("function return type cannot be function");
7372 value_type = integer_type_node;
7375 /* Make a node of the sort we want. */
7376 t = make_node (FUNCTION_TYPE);
7377 TREE_TYPE (t) = value_type;
7378 TYPE_ARG_TYPES (t) = arg_types;
7380 /* If we already have such a type, use the old one. */
7381 hashval_t hash = type_hash_canon_hash (t);
7382 t = type_hash_canon (hash, t);
7384 /* Set up the canonical type. */
7385 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7386 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7387 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
7388 &any_structural_p,
7389 &any_noncanonical_p);
7390 if (any_structural_p)
7391 SET_TYPE_STRUCTURAL_EQUALITY (t);
7392 else if (any_noncanonical_p)
7393 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
7394 canon_argtypes);
7396 if (!COMPLETE_TYPE_P (t))
7397 layout_type (t);
7398 return t;
7401 /* Build a function type. The RETURN_TYPE is the type returned by the
7402 function. If VAARGS is set, no void_type_node is appended to the
7403 list. ARGP must be always be terminated be a NULL_TREE. */
7405 static tree
7406 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
7408 tree t, args, last;
7410 t = va_arg (argp, tree);
7411 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
7412 args = tree_cons (NULL_TREE, t, args);
7414 if (vaargs)
7416 last = args;
7417 if (args != NULL_TREE)
7418 args = nreverse (args);
7419 gcc_assert (last != void_list_node);
7421 else if (args == NULL_TREE)
7422 args = void_list_node;
7423 else
7425 last = args;
7426 args = nreverse (args);
7427 TREE_CHAIN (last) = void_list_node;
7429 args = build_function_type (return_type, args);
7431 return args;
7434 /* Build a function type. The RETURN_TYPE is the type returned by the
7435 function. If additional arguments are provided, they are
7436 additional argument types. The list of argument types must always
7437 be terminated by NULL_TREE. */
7439 tree
7440 build_function_type_list (tree return_type, ...)
7442 tree args;
7443 va_list p;
7445 va_start (p, return_type);
7446 args = build_function_type_list_1 (false, return_type, p);
7447 va_end (p);
7448 return args;
7451 /* Build a variable argument function type. The RETURN_TYPE is the
7452 type returned by the function. If additional arguments are provided,
7453 they are additional argument types. The list of argument types must
7454 always be terminated by NULL_TREE. */
7456 tree
7457 build_varargs_function_type_list (tree return_type, ...)
7459 tree args;
7460 va_list p;
7462 va_start (p, return_type);
7463 args = build_function_type_list_1 (true, return_type, p);
7464 va_end (p);
7466 return args;
7469 /* Build a function type. RETURN_TYPE is the type returned by the
7470 function; VAARGS indicates whether the function takes varargs. The
7471 function takes N named arguments, the types of which are provided in
7472 ARG_TYPES. */
7474 static tree
7475 build_function_type_array_1 (bool vaargs, tree return_type, int n,
7476 tree *arg_types)
7478 int i;
7479 tree t = vaargs ? NULL_TREE : void_list_node;
7481 for (i = n - 1; i >= 0; i--)
7482 t = tree_cons (NULL_TREE, arg_types[i], t);
7484 return build_function_type (return_type, t);
7487 /* Build a function type. RETURN_TYPE is the type returned by the
7488 function. The function takes N named arguments, the types of which
7489 are provided in ARG_TYPES. */
7491 tree
7492 build_function_type_array (tree return_type, int n, tree *arg_types)
7494 return build_function_type_array_1 (false, return_type, n, arg_types);
7497 /* Build a variable argument function type. RETURN_TYPE is the type
7498 returned by the function. The function takes N named arguments, the
7499 types of which are provided in ARG_TYPES. */
7501 tree
7502 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
7504 return build_function_type_array_1 (true, return_type, n, arg_types);
7507 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
7508 and ARGTYPES (a TREE_LIST) are the return type and arguments types
7509 for the method. An implicit additional parameter (of type
7510 pointer-to-BASETYPE) is added to the ARGTYPES. */
7512 tree
7513 build_method_type_directly (tree basetype,
7514 tree rettype,
7515 tree argtypes)
7517 tree t;
7518 tree ptype;
7519 bool any_structural_p, any_noncanonical_p;
7520 tree canon_argtypes;
7522 /* Make a node of the sort we want. */
7523 t = make_node (METHOD_TYPE);
7525 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7526 TREE_TYPE (t) = rettype;
7527 ptype = build_pointer_type (basetype);
7529 /* The actual arglist for this function includes a "hidden" argument
7530 which is "this". Put it into the list of argument types. */
7531 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
7532 TYPE_ARG_TYPES (t) = argtypes;
7534 /* If we already have such a type, use the old one. */
7535 hashval_t hash = type_hash_canon_hash (t);
7536 t = type_hash_canon (hash, t);
7538 /* Set up the canonical type. */
7539 any_structural_p
7540 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7541 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
7542 any_noncanonical_p
7543 = (TYPE_CANONICAL (basetype) != basetype
7544 || TYPE_CANONICAL (rettype) != rettype);
7545 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
7546 &any_structural_p,
7547 &any_noncanonical_p);
7548 if (any_structural_p)
7549 SET_TYPE_STRUCTURAL_EQUALITY (t);
7550 else if (any_noncanonical_p)
7551 TYPE_CANONICAL (t)
7552 = build_method_type_directly (TYPE_CANONICAL (basetype),
7553 TYPE_CANONICAL (rettype),
7554 canon_argtypes);
7555 if (!COMPLETE_TYPE_P (t))
7556 layout_type (t);
7558 return t;
7561 /* Construct, lay out and return the type of methods belonging to class
7562 BASETYPE and whose arguments and values are described by TYPE.
7563 If that type exists already, reuse it.
7564 TYPE must be a FUNCTION_TYPE node. */
7566 tree
7567 build_method_type (tree basetype, tree type)
7569 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
7571 return build_method_type_directly (basetype,
7572 TREE_TYPE (type),
7573 TYPE_ARG_TYPES (type));
7576 /* Construct, lay out and return the type of offsets to a value
7577 of type TYPE, within an object of type BASETYPE.
7578 If a suitable offset type exists already, reuse it. */
7580 tree
7581 build_offset_type (tree basetype, tree type)
7583 tree t;
7585 /* Make a node of the sort we want. */
7586 t = make_node (OFFSET_TYPE);
7588 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7589 TREE_TYPE (t) = type;
7591 /* If we already have such a type, use the old one. */
7592 hashval_t hash = type_hash_canon_hash (t);
7593 t = type_hash_canon (hash, t);
7595 if (!COMPLETE_TYPE_P (t))
7596 layout_type (t);
7598 if (TYPE_CANONICAL (t) == t)
7600 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7601 || TYPE_STRUCTURAL_EQUALITY_P (type))
7602 SET_TYPE_STRUCTURAL_EQUALITY (t);
7603 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
7604 || TYPE_CANONICAL (type) != type)
7605 TYPE_CANONICAL (t)
7606 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
7607 TYPE_CANONICAL (type));
7610 return t;
7613 /* Create a complex type whose components are COMPONENT_TYPE.
7615 If NAMED is true, the type is given a TYPE_NAME. We do not always
7616 do so because this creates a DECL node and thus make the DECL_UIDs
7617 dependent on the type canonicalization hashtable, which is GC-ed,
7618 so the DECL_UIDs would not be stable wrt garbage collection. */
7620 tree
7621 build_complex_type (tree component_type, bool named)
7623 gcc_assert (INTEGRAL_TYPE_P (component_type)
7624 || SCALAR_FLOAT_TYPE_P (component_type)
7625 || FIXED_POINT_TYPE_P (component_type));
7627 /* Make a node of the sort we want. */
7628 tree probe = make_node (COMPLEX_TYPE);
7630 TREE_TYPE (probe) = TYPE_MAIN_VARIANT (component_type);
7632 /* If we already have such a type, use the old one. */
7633 hashval_t hash = type_hash_canon_hash (probe);
7634 tree t = type_hash_canon (hash, probe);
7636 if (t == probe)
7638 /* We created a new type. The hash insertion will have laid
7639 out the type. We need to check the canonicalization and
7640 maybe set the name. */
7641 gcc_checking_assert (COMPLETE_TYPE_P (t)
7642 && !TYPE_NAME (t)
7643 && TYPE_CANONICAL (t) == t);
7645 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (t)))
7646 SET_TYPE_STRUCTURAL_EQUALITY (t);
7647 else if (TYPE_CANONICAL (TREE_TYPE (t)) != TREE_TYPE (t))
7648 TYPE_CANONICAL (t)
7649 = build_complex_type (TYPE_CANONICAL (TREE_TYPE (t)), named);
7651 /* We need to create a name, since complex is a fundamental type. */
7652 if (named)
7654 const char *name = NULL;
7656 if (TREE_TYPE (t) == char_type_node)
7657 name = "complex char";
7658 else if (TREE_TYPE (t) == signed_char_type_node)
7659 name = "complex signed char";
7660 else if (TREE_TYPE (t) == unsigned_char_type_node)
7661 name = "complex unsigned char";
7662 else if (TREE_TYPE (t) == short_integer_type_node)
7663 name = "complex short int";
7664 else if (TREE_TYPE (t) == short_unsigned_type_node)
7665 name = "complex short unsigned int";
7666 else if (TREE_TYPE (t) == integer_type_node)
7667 name = "complex int";
7668 else if (TREE_TYPE (t) == unsigned_type_node)
7669 name = "complex unsigned int";
7670 else if (TREE_TYPE (t) == long_integer_type_node)
7671 name = "complex long int";
7672 else if (TREE_TYPE (t) == long_unsigned_type_node)
7673 name = "complex long unsigned int";
7674 else if (TREE_TYPE (t) == long_long_integer_type_node)
7675 name = "complex long long int";
7676 else if (TREE_TYPE (t) == long_long_unsigned_type_node)
7677 name = "complex long long unsigned int";
7679 if (name != NULL)
7680 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
7681 get_identifier (name), t);
7685 return build_qualified_type (t, TYPE_QUALS (component_type));
7688 /* If TYPE is a real or complex floating-point type and the target
7689 does not directly support arithmetic on TYPE then return the wider
7690 type to be used for arithmetic on TYPE. Otherwise, return
7691 NULL_TREE. */
7693 tree
7694 excess_precision_type (tree type)
7696 /* The target can give two different responses to the question of
7697 which excess precision mode it would like depending on whether we
7698 are in -fexcess-precision=standard or -fexcess-precision=fast. */
7700 enum excess_precision_type requested_type
7701 = (flag_excess_precision == EXCESS_PRECISION_FAST
7702 ? EXCESS_PRECISION_TYPE_FAST
7703 : (flag_excess_precision == EXCESS_PRECISION_FLOAT16
7704 ? EXCESS_PRECISION_TYPE_FLOAT16 :EXCESS_PRECISION_TYPE_STANDARD));
7706 enum flt_eval_method target_flt_eval_method
7707 = targetm.c.excess_precision (requested_type);
7709 /* The target should not ask for unpredictable float evaluation (though
7710 it might advertise that implicitly the evaluation is unpredictable,
7711 but we don't care about that here, it will have been reported
7712 elsewhere). If it does ask for unpredictable evaluation, we have
7713 nothing to do here. */
7714 gcc_assert (target_flt_eval_method != FLT_EVAL_METHOD_UNPREDICTABLE);
7716 /* Nothing to do. The target has asked for all types we know about
7717 to be computed with their native precision and range. */
7718 if (target_flt_eval_method == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
7719 return NULL_TREE;
7721 /* The target will promote this type in a target-dependent way, so excess
7722 precision ought to leave it alone. */
7723 if (targetm.promoted_type (type) != NULL_TREE)
7724 return NULL_TREE;
7726 machine_mode float16_type_mode = (float16_type_node
7727 ? TYPE_MODE (float16_type_node)
7728 : VOIDmode);
7729 machine_mode float_type_mode = TYPE_MODE (float_type_node);
7730 machine_mode double_type_mode = TYPE_MODE (double_type_node);
7732 switch (TREE_CODE (type))
7734 case REAL_TYPE:
7736 machine_mode type_mode = TYPE_MODE (type);
7737 switch (target_flt_eval_method)
7739 case FLT_EVAL_METHOD_PROMOTE_TO_FLOAT:
7740 if (type_mode == float16_type_mode)
7741 return float_type_node;
7742 break;
7743 case FLT_EVAL_METHOD_PROMOTE_TO_DOUBLE:
7744 if (type_mode == float16_type_mode
7745 || type_mode == float_type_mode)
7746 return double_type_node;
7747 break;
7748 case FLT_EVAL_METHOD_PROMOTE_TO_LONG_DOUBLE:
7749 if (type_mode == float16_type_mode
7750 || type_mode == float_type_mode
7751 || type_mode == double_type_mode)
7752 return long_double_type_node;
7753 break;
7754 default:
7755 gcc_unreachable ();
7757 break;
7759 case COMPLEX_TYPE:
7761 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
7762 return NULL_TREE;
7763 machine_mode type_mode = TYPE_MODE (TREE_TYPE (type));
7764 switch (target_flt_eval_method)
7766 case FLT_EVAL_METHOD_PROMOTE_TO_FLOAT:
7767 if (type_mode == float16_type_mode)
7768 return complex_float_type_node;
7769 break;
7770 case FLT_EVAL_METHOD_PROMOTE_TO_DOUBLE:
7771 if (type_mode == float16_type_mode
7772 || type_mode == float_type_mode)
7773 return complex_double_type_node;
7774 break;
7775 case FLT_EVAL_METHOD_PROMOTE_TO_LONG_DOUBLE:
7776 if (type_mode == float16_type_mode
7777 || type_mode == float_type_mode
7778 || type_mode == double_type_mode)
7779 return complex_long_double_type_node;
7780 break;
7781 default:
7782 gcc_unreachable ();
7784 break;
7786 default:
7787 break;
7790 return NULL_TREE;
7793 /* Return OP, stripped of any conversions to wider types as much as is safe.
7794 Converting the value back to OP's type makes a value equivalent to OP.
7796 If FOR_TYPE is nonzero, we return a value which, if converted to
7797 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
7799 OP must have integer, real or enumeral type. Pointers are not allowed!
7801 There are some cases where the obvious value we could return
7802 would regenerate to OP if converted to OP's type,
7803 but would not extend like OP to wider types.
7804 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
7805 For example, if OP is (unsigned short)(signed char)-1,
7806 we avoid returning (signed char)-1 if FOR_TYPE is int,
7807 even though extending that to an unsigned short would regenerate OP,
7808 since the result of extending (signed char)-1 to (int)
7809 is different from (int) OP. */
7811 tree
7812 get_unwidened (tree op, tree for_type)
7814 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
7815 tree type = TREE_TYPE (op);
7816 unsigned final_prec
7817 = TYPE_PRECISION (for_type != 0 ? for_type : type);
7818 int uns
7819 = (for_type != 0 && for_type != type
7820 && final_prec > TYPE_PRECISION (type)
7821 && TYPE_UNSIGNED (type));
7822 tree win = op;
7824 while (CONVERT_EXPR_P (op))
7826 int bitschange;
7828 /* TYPE_PRECISION on vector types has different meaning
7829 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
7830 so avoid them here. */
7831 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
7832 break;
7834 bitschange = TYPE_PRECISION (TREE_TYPE (op))
7835 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
7837 /* Truncations are many-one so cannot be removed.
7838 Unless we are later going to truncate down even farther. */
7839 if (bitschange < 0
7840 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
7841 break;
7843 /* See what's inside this conversion. If we decide to strip it,
7844 we will set WIN. */
7845 op = TREE_OPERAND (op, 0);
7847 /* If we have not stripped any zero-extensions (uns is 0),
7848 we can strip any kind of extension.
7849 If we have previously stripped a zero-extension,
7850 only zero-extensions can safely be stripped.
7851 Any extension can be stripped if the bits it would produce
7852 are all going to be discarded later by truncating to FOR_TYPE. */
7854 if (bitschange > 0)
7856 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
7857 win = op;
7858 /* TYPE_UNSIGNED says whether this is a zero-extension.
7859 Let's avoid computing it if it does not affect WIN
7860 and if UNS will not be needed again. */
7861 if ((uns
7862 || CONVERT_EXPR_P (op))
7863 && TYPE_UNSIGNED (TREE_TYPE (op)))
7865 uns = 1;
7866 win = op;
7871 /* If we finally reach a constant see if it fits in sth smaller and
7872 in that case convert it. */
7873 if (TREE_CODE (win) == INTEGER_CST)
7875 tree wtype = TREE_TYPE (win);
7876 unsigned prec = wi::min_precision (wi::to_wide (win), TYPE_SIGN (wtype));
7877 if (for_type)
7878 prec = MAX (prec, final_prec);
7879 if (prec < TYPE_PRECISION (wtype))
7881 tree t = lang_hooks.types.type_for_size (prec, TYPE_UNSIGNED (wtype));
7882 if (t && TYPE_PRECISION (t) < TYPE_PRECISION (wtype))
7883 win = fold_convert (t, win);
7887 return win;
7890 /* Return OP or a simpler expression for a narrower value
7891 which can be sign-extended or zero-extended to give back OP.
7892 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
7893 or 0 if the value should be sign-extended. */
7895 tree
7896 get_narrower (tree op, int *unsignedp_ptr)
7898 int uns = 0;
7899 int first = 1;
7900 tree win = op;
7901 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
7903 if (TREE_CODE (op) == COMPOUND_EXPR)
7906 op = TREE_OPERAND (op, 1);
7907 while (TREE_CODE (op) == COMPOUND_EXPR);
7908 tree ret = get_narrower (op, unsignedp_ptr);
7909 if (ret == op)
7910 return win;
7911 auto_vec <tree, 16> v;
7912 unsigned int i;
7913 for (op = win; TREE_CODE (op) == COMPOUND_EXPR;
7914 op = TREE_OPERAND (op, 1))
7915 v.safe_push (op);
7916 FOR_EACH_VEC_ELT_REVERSE (v, i, op)
7917 ret = build2_loc (EXPR_LOCATION (op), COMPOUND_EXPR,
7918 TREE_TYPE (ret), TREE_OPERAND (op, 0),
7919 ret);
7920 return ret;
7922 while (TREE_CODE (op) == NOP_EXPR)
7924 int bitschange
7925 = (TYPE_PRECISION (TREE_TYPE (op))
7926 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
7928 /* Truncations are many-one so cannot be removed. */
7929 if (bitschange < 0)
7930 break;
7932 /* See what's inside this conversion. If we decide to strip it,
7933 we will set WIN. */
7935 if (bitschange > 0)
7937 op = TREE_OPERAND (op, 0);
7938 /* An extension: the outermost one can be stripped,
7939 but remember whether it is zero or sign extension. */
7940 if (first)
7941 uns = TYPE_UNSIGNED (TREE_TYPE (op));
7942 /* Otherwise, if a sign extension has been stripped,
7943 only sign extensions can now be stripped;
7944 if a zero extension has been stripped, only zero-extensions. */
7945 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
7946 break;
7947 first = 0;
7949 else /* bitschange == 0 */
7951 /* A change in nominal type can always be stripped, but we must
7952 preserve the unsignedness. */
7953 if (first)
7954 uns = TYPE_UNSIGNED (TREE_TYPE (op));
7955 first = 0;
7956 op = TREE_OPERAND (op, 0);
7957 /* Keep trying to narrow, but don't assign op to win if it
7958 would turn an integral type into something else. */
7959 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
7960 continue;
7963 win = op;
7966 if (TREE_CODE (op) == COMPONENT_REF
7967 /* Since type_for_size always gives an integer type. */
7968 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
7969 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
7970 /* Ensure field is laid out already. */
7971 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
7972 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
7974 unsigned HOST_WIDE_INT innerprec
7975 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
7976 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
7977 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
7978 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
7980 /* We can get this structure field in a narrower type that fits it,
7981 but the resulting extension to its nominal type (a fullword type)
7982 must satisfy the same conditions as for other extensions.
7984 Do this only for fields that are aligned (not bit-fields),
7985 because when bit-field insns will be used there is no
7986 advantage in doing this. */
7988 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
7989 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
7990 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
7991 && type != 0)
7993 if (first)
7994 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
7995 win = fold_convert (type, op);
7999 *unsignedp_ptr = uns;
8000 return win;
8003 /* Return true if integer constant C has a value that is permissible
8004 for TYPE, an integral type. */
8006 bool
8007 int_fits_type_p (const_tree c, const_tree type)
8009 tree type_low_bound, type_high_bound;
8010 bool ok_for_low_bound, ok_for_high_bound;
8011 signop sgn_c = TYPE_SIGN (TREE_TYPE (c));
8013 /* Non-standard boolean types can have arbitrary precision but various
8014 transformations assume that they can only take values 0 and +/-1. */
8015 if (TREE_CODE (type) == BOOLEAN_TYPE)
8016 return wi::fits_to_boolean_p (wi::to_wide (c), type);
8018 retry:
8019 type_low_bound = TYPE_MIN_VALUE (type);
8020 type_high_bound = TYPE_MAX_VALUE (type);
8022 /* If at least one bound of the type is a constant integer, we can check
8023 ourselves and maybe make a decision. If no such decision is possible, but
8024 this type is a subtype, try checking against that. Otherwise, use
8025 fits_to_tree_p, which checks against the precision.
8027 Compute the status for each possibly constant bound, and return if we see
8028 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8029 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8030 for "constant known to fit". */
8032 /* Check if c >= type_low_bound. */
8033 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8035 if (tree_int_cst_lt (c, type_low_bound))
8036 return false;
8037 ok_for_low_bound = true;
8039 else
8040 ok_for_low_bound = false;
8042 /* Check if c <= type_high_bound. */
8043 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8045 if (tree_int_cst_lt (type_high_bound, c))
8046 return false;
8047 ok_for_high_bound = true;
8049 else
8050 ok_for_high_bound = false;
8052 /* If the constant fits both bounds, the result is known. */
8053 if (ok_for_low_bound && ok_for_high_bound)
8054 return true;
8056 /* Perform some generic filtering which may allow making a decision
8057 even if the bounds are not constant. First, negative integers
8058 never fit in unsigned types, */
8059 if (TYPE_UNSIGNED (type) && sgn_c == SIGNED && wi::neg_p (wi::to_wide (c)))
8060 return false;
8062 /* Second, narrower types always fit in wider ones. */
8063 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8064 return true;
8066 /* Third, unsigned integers with top bit set never fit signed types. */
8067 if (!TYPE_UNSIGNED (type) && sgn_c == UNSIGNED)
8069 int prec = GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (TREE_TYPE (c))) - 1;
8070 if (prec < TYPE_PRECISION (TREE_TYPE (c)))
8072 /* When a tree_cst is converted to a wide-int, the precision
8073 is taken from the type. However, if the precision of the
8074 mode underneath the type is smaller than that, it is
8075 possible that the value will not fit. The test below
8076 fails if any bit is set between the sign bit of the
8077 underlying mode and the top bit of the type. */
8078 if (wi::zext (wi::to_wide (c), prec - 1) != wi::to_wide (c))
8079 return false;
8081 else if (wi::neg_p (wi::to_wide (c)))
8082 return false;
8085 /* If we haven't been able to decide at this point, there nothing more we
8086 can check ourselves here. Look at the base type if we have one and it
8087 has the same precision. */
8088 if (TREE_CODE (type) == INTEGER_TYPE
8089 && TREE_TYPE (type) != 0
8090 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8092 type = TREE_TYPE (type);
8093 goto retry;
8096 /* Or to fits_to_tree_p, if nothing else. */
8097 return wi::fits_to_tree_p (wi::to_wide (c), type);
8100 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8101 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8102 represented (assuming two's-complement arithmetic) within the bit
8103 precision of the type are returned instead. */
8105 void
8106 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8108 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8109 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8110 wi::to_mpz (wi::to_wide (TYPE_MIN_VALUE (type)), min, TYPE_SIGN (type));
8111 else
8113 if (TYPE_UNSIGNED (type))
8114 mpz_set_ui (min, 0);
8115 else
8117 wide_int mn = wi::min_value (TYPE_PRECISION (type), SIGNED);
8118 wi::to_mpz (mn, min, SIGNED);
8122 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8123 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8124 wi::to_mpz (wi::to_wide (TYPE_MAX_VALUE (type)), max, TYPE_SIGN (type));
8125 else
8127 wide_int mn = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
8128 wi::to_mpz (mn, max, TYPE_SIGN (type));
8132 /* Return true if VAR is an automatic variable. */
8134 bool
8135 auto_var_p (const_tree var)
8137 return ((((VAR_P (var) && ! DECL_EXTERNAL (var))
8138 || TREE_CODE (var) == PARM_DECL)
8139 && ! TREE_STATIC (var))
8140 || TREE_CODE (var) == RESULT_DECL);
8143 /* Return true if VAR is an automatic variable defined in function FN. */
8145 bool
8146 auto_var_in_fn_p (const_tree var, const_tree fn)
8148 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8149 && (auto_var_p (var)
8150 || TREE_CODE (var) == LABEL_DECL));
8153 /* Subprogram of following function. Called by walk_tree.
8155 Return *TP if it is an automatic variable or parameter of the
8156 function passed in as DATA. */
8158 static tree
8159 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8161 tree fn = (tree) data;
8163 if (TYPE_P (*tp))
8164 *walk_subtrees = 0;
8166 else if (DECL_P (*tp)
8167 && auto_var_in_fn_p (*tp, fn))
8168 return *tp;
8170 return NULL_TREE;
8173 /* Returns true if T is, contains, or refers to a type with variable
8174 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8175 arguments, but not the return type. If FN is nonzero, only return
8176 true if a modifier of the type or position of FN is a variable or
8177 parameter inside FN.
8179 This concept is more general than that of C99 'variably modified types':
8180 in C99, a struct type is never variably modified because a VLA may not
8181 appear as a structure member. However, in GNU C code like:
8183 struct S { int i[f()]; };
8185 is valid, and other languages may define similar constructs. */
8187 bool
8188 variably_modified_type_p (tree type, tree fn)
8190 tree t;
8192 /* Test if T is either variable (if FN is zero) or an expression containing
8193 a variable in FN. If TYPE isn't gimplified, return true also if
8194 gimplify_one_sizepos would gimplify the expression into a local
8195 variable. */
8196 #define RETURN_TRUE_IF_VAR(T) \
8197 do { tree _t = (T); \
8198 if (_t != NULL_TREE \
8199 && _t != error_mark_node \
8200 && !CONSTANT_CLASS_P (_t) \
8201 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8202 && (!fn \
8203 || (!TYPE_SIZES_GIMPLIFIED (type) \
8204 && (TREE_CODE (_t) != VAR_DECL \
8205 && !CONTAINS_PLACEHOLDER_P (_t))) \
8206 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8207 return true; } while (0)
8209 if (type == error_mark_node)
8210 return false;
8212 /* If TYPE itself has variable size, it is variably modified. */
8213 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8214 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8216 switch (TREE_CODE (type))
8218 case POINTER_TYPE:
8219 case REFERENCE_TYPE:
8220 case VECTOR_TYPE:
8221 /* Ada can have pointer types refering to themselves indirectly. */
8222 if (TREE_VISITED (type))
8223 return false;
8224 TREE_VISITED (type) = true;
8225 if (variably_modified_type_p (TREE_TYPE (type), fn))
8227 TREE_VISITED (type) = false;
8228 return true;
8230 TREE_VISITED (type) = false;
8231 break;
8233 case FUNCTION_TYPE:
8234 case METHOD_TYPE:
8235 /* If TYPE is a function type, it is variably modified if the
8236 return type is variably modified. */
8237 if (variably_modified_type_p (TREE_TYPE (type), fn))
8238 return true;
8239 break;
8241 case INTEGER_TYPE:
8242 case REAL_TYPE:
8243 case FIXED_POINT_TYPE:
8244 case ENUMERAL_TYPE:
8245 case BOOLEAN_TYPE:
8246 /* Scalar types are variably modified if their end points
8247 aren't constant. */
8248 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8249 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8250 break;
8252 case RECORD_TYPE:
8253 case UNION_TYPE:
8254 case QUAL_UNION_TYPE:
8255 /* We can't see if any of the fields are variably-modified by the
8256 definition we normally use, since that would produce infinite
8257 recursion via pointers. */
8258 /* This is variably modified if some field's type is. */
8259 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8260 if (TREE_CODE (t) == FIELD_DECL)
8262 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8263 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8264 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8266 /* If the type is a qualified union, then the DECL_QUALIFIER
8267 of fields can also be an expression containing a variable. */
8268 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8269 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8271 /* If the field is a qualified union, then it's only a container
8272 for what's inside so we look into it. That's necessary in LTO
8273 mode because the sizes of the field tested above have been set
8274 to PLACEHOLDER_EXPRs by free_lang_data. */
8275 if (TREE_CODE (TREE_TYPE (t)) == QUAL_UNION_TYPE
8276 && variably_modified_type_p (TREE_TYPE (t), fn))
8277 return true;
8279 break;
8281 case ARRAY_TYPE:
8282 /* Do not call ourselves to avoid infinite recursion. This is
8283 variably modified if the element type is. */
8284 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8285 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8286 break;
8288 default:
8289 break;
8292 /* The current language may have other cases to check, but in general,
8293 all other types are not variably modified. */
8294 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8296 #undef RETURN_TRUE_IF_VAR
8299 /* Given a DECL or TYPE, return the scope in which it was declared, or
8300 NULL_TREE if there is no containing scope. */
8302 tree
8303 get_containing_scope (const_tree t)
8305 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8308 /* Returns the ultimate TRANSLATION_UNIT_DECL context of DECL or NULL. */
8310 const_tree
8311 get_ultimate_context (const_tree decl)
8313 while (decl && TREE_CODE (decl) != TRANSLATION_UNIT_DECL)
8315 if (TREE_CODE (decl) == BLOCK)
8316 decl = BLOCK_SUPERCONTEXT (decl);
8317 else
8318 decl = get_containing_scope (decl);
8320 return decl;
8323 /* Return the innermost context enclosing DECL that is
8324 a FUNCTION_DECL, or zero if none. */
8326 tree
8327 decl_function_context (const_tree decl)
8329 tree context;
8331 if (TREE_CODE (decl) == ERROR_MARK)
8332 return 0;
8334 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8335 where we look up the function at runtime. Such functions always take
8336 a first argument of type 'pointer to real context'.
8338 C++ should really be fixed to use DECL_CONTEXT for the real context,
8339 and use something else for the "virtual context". */
8340 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
8341 context
8342 = TYPE_MAIN_VARIANT
8343 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8344 else
8345 context = DECL_CONTEXT (decl);
8347 while (context && TREE_CODE (context) != FUNCTION_DECL)
8349 if (TREE_CODE (context) == BLOCK)
8350 context = BLOCK_SUPERCONTEXT (context);
8351 else
8352 context = get_containing_scope (context);
8355 return context;
8358 /* Return the innermost context enclosing DECL that is
8359 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8360 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8362 tree
8363 decl_type_context (const_tree decl)
8365 tree context = DECL_CONTEXT (decl);
8367 while (context)
8368 switch (TREE_CODE (context))
8370 case NAMESPACE_DECL:
8371 case TRANSLATION_UNIT_DECL:
8372 return NULL_TREE;
8374 case RECORD_TYPE:
8375 case UNION_TYPE:
8376 case QUAL_UNION_TYPE:
8377 return context;
8379 case TYPE_DECL:
8380 case FUNCTION_DECL:
8381 context = DECL_CONTEXT (context);
8382 break;
8384 case BLOCK:
8385 context = BLOCK_SUPERCONTEXT (context);
8386 break;
8388 default:
8389 gcc_unreachable ();
8392 return NULL_TREE;
8395 /* CALL is a CALL_EXPR. Return the declaration for the function
8396 called, or NULL_TREE if the called function cannot be
8397 determined. */
8399 tree
8400 get_callee_fndecl (const_tree call)
8402 tree addr;
8404 if (call == error_mark_node)
8405 return error_mark_node;
8407 /* It's invalid to call this function with anything but a
8408 CALL_EXPR. */
8409 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8411 /* The first operand to the CALL is the address of the function
8412 called. */
8413 addr = CALL_EXPR_FN (call);
8415 /* If there is no function, return early. */
8416 if (addr == NULL_TREE)
8417 return NULL_TREE;
8419 STRIP_NOPS (addr);
8421 /* If this is a readonly function pointer, extract its initial value. */
8422 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8423 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8424 && DECL_INITIAL (addr))
8425 addr = DECL_INITIAL (addr);
8427 /* If the address is just `&f' for some function `f', then we know
8428 that `f' is being called. */
8429 if (TREE_CODE (addr) == ADDR_EXPR
8430 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8431 return TREE_OPERAND (addr, 0);
8433 /* We couldn't figure out what was being called. */
8434 return NULL_TREE;
8437 /* Return true when STMTs arguments and return value match those of FNDECL,
8438 a decl of a builtin function. */
8440 static bool
8441 tree_builtin_call_types_compatible_p (const_tree call, tree fndecl)
8443 gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) != NOT_BUILT_IN);
8445 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
8446 if (tree decl = builtin_decl_explicit (DECL_FUNCTION_CODE (fndecl)))
8447 fndecl = decl;
8449 bool gimple_form = (cfun && (cfun->curr_properties & PROP_gimple)) != 0;
8450 if (gimple_form
8451 ? !useless_type_conversion_p (TREE_TYPE (call),
8452 TREE_TYPE (TREE_TYPE (fndecl)))
8453 : (TYPE_MAIN_VARIANT (TREE_TYPE (call))
8454 != TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))))
8455 return false;
8457 tree targs = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
8458 unsigned nargs = call_expr_nargs (call);
8459 for (unsigned i = 0; i < nargs; ++i, targs = TREE_CHAIN (targs))
8461 /* Variadic args follow. */
8462 if (!targs)
8463 return true;
8464 tree arg = CALL_EXPR_ARG (call, i);
8465 tree type = TREE_VALUE (targs);
8466 if (gimple_form
8467 ? !useless_type_conversion_p (type, TREE_TYPE (arg))
8468 : TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (TREE_TYPE (arg)))
8470 /* For pointer arguments be more forgiving, e.g. due to
8471 FILE * vs. fileptr_type_node, or say char * vs. const char *
8472 differences etc. */
8473 if (!gimple_form
8474 && POINTER_TYPE_P (type)
8475 && POINTER_TYPE_P (TREE_TYPE (arg))
8476 && tree_nop_conversion_p (type, TREE_TYPE (arg)))
8477 continue;
8478 /* char/short integral arguments are promoted to int
8479 by several frontends if targetm.calls.promote_prototypes
8480 is true. Allow such promotion too. */
8481 if (INTEGRAL_TYPE_P (type)
8482 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
8483 && INTEGRAL_TYPE_P (TREE_TYPE (arg))
8484 && !TYPE_UNSIGNED (TREE_TYPE (arg))
8485 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl))
8486 && (gimple_form
8487 ? useless_type_conversion_p (integer_type_node,
8488 TREE_TYPE (arg))
8489 : tree_nop_conversion_p (integer_type_node,
8490 TREE_TYPE (arg))))
8491 continue;
8492 return false;
8495 if (targs && !VOID_TYPE_P (TREE_VALUE (targs)))
8496 return false;
8497 return true;
8500 /* If CALL_EXPR CALL calls a normal built-in function or an internal function,
8501 return the associated function code, otherwise return CFN_LAST. */
8503 combined_fn
8504 get_call_combined_fn (const_tree call)
8506 /* It's invalid to call this function with anything but a CALL_EXPR. */
8507 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8509 if (!CALL_EXPR_FN (call))
8510 return as_combined_fn (CALL_EXPR_IFN (call));
8512 tree fndecl = get_callee_fndecl (call);
8513 if (fndecl
8514 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
8515 && tree_builtin_call_types_compatible_p (call, fndecl))
8516 return as_combined_fn (DECL_FUNCTION_CODE (fndecl));
8518 return CFN_LAST;
8521 /* Comparator of indices based on tree_node_counts. */
8523 static int
8524 tree_nodes_cmp (const void *p1, const void *p2)
8526 const unsigned *n1 = (const unsigned *)p1;
8527 const unsigned *n2 = (const unsigned *)p2;
8529 return tree_node_counts[*n1] - tree_node_counts[*n2];
8532 /* Comparator of indices based on tree_code_counts. */
8534 static int
8535 tree_codes_cmp (const void *p1, const void *p2)
8537 const unsigned *n1 = (const unsigned *)p1;
8538 const unsigned *n2 = (const unsigned *)p2;
8540 return tree_code_counts[*n1] - tree_code_counts[*n2];
8543 #define TREE_MEM_USAGE_SPACES 40
8545 /* Print debugging information about tree nodes generated during the compile,
8546 and any language-specific information. */
8548 void
8549 dump_tree_statistics (void)
8551 if (GATHER_STATISTICS)
8553 uint64_t total_nodes, total_bytes;
8554 fprintf (stderr, "\nKind Nodes Bytes\n");
8555 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8556 total_nodes = total_bytes = 0;
8559 auto_vec<unsigned> indices (all_kinds);
8560 for (unsigned i = 0; i < all_kinds; i++)
8561 indices.quick_push (i);
8562 indices.qsort (tree_nodes_cmp);
8564 for (unsigned i = 0; i < (int) all_kinds; i++)
8566 unsigned j = indices[i];
8567 fprintf (stderr, "%-20s %6" PRIu64 "%c %9" PRIu64 "%c\n",
8568 tree_node_kind_names[j], SIZE_AMOUNT (tree_node_counts[j]),
8569 SIZE_AMOUNT (tree_node_sizes[j]));
8570 total_nodes += tree_node_counts[j];
8571 total_bytes += tree_node_sizes[j];
8573 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8574 fprintf (stderr, "%-20s %6" PRIu64 "%c %9" PRIu64 "%c\n", "Total",
8575 SIZE_AMOUNT (total_nodes), SIZE_AMOUNT (total_bytes));
8576 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8580 fprintf (stderr, "Code Nodes\n");
8581 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8583 auto_vec<unsigned> indices (MAX_TREE_CODES);
8584 for (unsigned i = 0; i < MAX_TREE_CODES; i++)
8585 indices.quick_push (i);
8586 indices.qsort (tree_codes_cmp);
8588 for (unsigned i = 0; i < MAX_TREE_CODES; i++)
8590 unsigned j = indices[i];
8591 fprintf (stderr, "%-32s %6" PRIu64 "%c\n",
8592 get_tree_code_name ((enum tree_code) j),
8593 SIZE_AMOUNT (tree_code_counts[j]));
8595 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8596 fprintf (stderr, "\n");
8597 ssanames_print_statistics ();
8598 fprintf (stderr, "\n");
8599 phinodes_print_statistics ();
8600 fprintf (stderr, "\n");
8603 else
8604 fprintf (stderr, "(No per-node statistics)\n");
8606 print_type_hash_statistics ();
8607 print_debug_expr_statistics ();
8608 print_value_expr_statistics ();
8609 lang_hooks.print_statistics ();
8612 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8614 /* Generate a crc32 of the low BYTES bytes of VALUE. */
8616 unsigned
8617 crc32_unsigned_n (unsigned chksum, unsigned value, unsigned bytes)
8619 /* This relies on the raw feedback's top 4 bits being zero. */
8620 #define FEEDBACK(X) ((X) * 0x04c11db7)
8621 #define SYNDROME(X) (FEEDBACK ((X) & 1) ^ FEEDBACK ((X) & 2) \
8622 ^ FEEDBACK ((X) & 4) ^ FEEDBACK ((X) & 8))
8623 static const unsigned syndromes[16] =
8625 SYNDROME(0x0), SYNDROME(0x1), SYNDROME(0x2), SYNDROME(0x3),
8626 SYNDROME(0x4), SYNDROME(0x5), SYNDROME(0x6), SYNDROME(0x7),
8627 SYNDROME(0x8), SYNDROME(0x9), SYNDROME(0xa), SYNDROME(0xb),
8628 SYNDROME(0xc), SYNDROME(0xd), SYNDROME(0xe), SYNDROME(0xf),
8630 #undef FEEDBACK
8631 #undef SYNDROME
8633 value <<= (32 - bytes * 8);
8634 for (unsigned ix = bytes * 2; ix--; value <<= 4)
8636 unsigned feedback = syndromes[((value ^ chksum) >> 28) & 0xf];
8638 chksum = (chksum << 4) ^ feedback;
8641 return chksum;
8644 /* Generate a crc32 of a string. */
8646 unsigned
8647 crc32_string (unsigned chksum, const char *string)
8650 chksum = crc32_byte (chksum, *string);
8651 while (*string++);
8652 return chksum;
8655 /* P is a string that will be used in a symbol. Mask out any characters
8656 that are not valid in that context. */
8658 void
8659 clean_symbol_name (char *p)
8661 for (; *p; p++)
8662 if (! (ISALNUM (*p)
8663 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
8664 || *p == '$'
8665 #endif
8666 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
8667 || *p == '.'
8668 #endif
8670 *p = '_';
8673 static GTY(()) unsigned anon_cnt = 0; /* Saved for PCH. */
8675 /* Create a unique anonymous identifier. The identifier is still a
8676 valid assembly label. */
8678 tree
8679 make_anon_name ()
8681 const char *fmt =
8682 #if !defined (NO_DOT_IN_LABEL)
8684 #elif !defined (NO_DOLLAR_IN_LABEL)
8686 #else
8688 #endif
8689 "_anon_%d";
8691 char buf[24];
8692 int len = snprintf (buf, sizeof (buf), fmt, anon_cnt++);
8693 gcc_checking_assert (len < int (sizeof (buf)));
8695 tree id = get_identifier_with_length (buf, len);
8696 IDENTIFIER_ANON_P (id) = true;
8698 return id;
8701 /* Generate a name for a special-purpose function.
8702 The generated name may need to be unique across the whole link.
8703 Changes to this function may also require corresponding changes to
8704 xstrdup_mask_random.
8705 TYPE is some string to identify the purpose of this function to the
8706 linker or collect2; it must start with an uppercase letter,
8707 one of:
8708 I - for constructors
8709 D - for destructors
8710 N - for C++ anonymous namespaces
8711 F - for DWARF unwind frame information. */
8713 tree
8714 get_file_function_name (const char *type)
8716 char *buf;
8717 const char *p;
8718 char *q;
8720 /* If we already have a name we know to be unique, just use that. */
8721 if (first_global_object_name)
8722 p = q = ASTRDUP (first_global_object_name);
8723 /* If the target is handling the constructors/destructors, they
8724 will be local to this file and the name is only necessary for
8725 debugging purposes.
8726 We also assign sub_I and sub_D sufixes to constructors called from
8727 the global static constructors. These are always local. */
8728 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
8729 || (startswith (type, "sub_")
8730 && (type[4] == 'I' || type[4] == 'D')))
8732 const char *file = main_input_filename;
8733 if (! file)
8734 file = LOCATION_FILE (input_location);
8735 /* Just use the file's basename, because the full pathname
8736 might be quite long. */
8737 p = q = ASTRDUP (lbasename (file));
8739 else
8741 /* Otherwise, the name must be unique across the entire link.
8742 We don't have anything that we know to be unique to this translation
8743 unit, so use what we do have and throw in some randomness. */
8744 unsigned len;
8745 const char *name = weak_global_object_name;
8746 const char *file = main_input_filename;
8748 if (! name)
8749 name = "";
8750 if (! file)
8751 file = LOCATION_FILE (input_location);
8753 len = strlen (file);
8754 q = (char *) alloca (9 + 19 + len + 1);
8755 memcpy (q, file, len + 1);
8757 snprintf (q + len, 9 + 19 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
8758 crc32_string (0, name), get_random_seed (false));
8760 p = q;
8763 clean_symbol_name (q);
8764 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
8765 + strlen (type));
8767 /* Set up the name of the file-level functions we may need.
8768 Use a global object (which is already required to be unique over
8769 the program) rather than the file name (which imposes extra
8770 constraints). */
8771 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
8773 return get_identifier (buf);
8776 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
8778 /* Complain that the tree code of NODE does not match the expected 0
8779 terminated list of trailing codes. The trailing code list can be
8780 empty, for a more vague error message. FILE, LINE, and FUNCTION
8781 are of the caller. */
8783 void
8784 tree_check_failed (const_tree node, const char *file,
8785 int line, const char *function, ...)
8787 va_list args;
8788 const char *buffer;
8789 unsigned length = 0;
8790 enum tree_code code;
8792 va_start (args, function);
8793 while ((code = (enum tree_code) va_arg (args, int)))
8794 length += 4 + strlen (get_tree_code_name (code));
8795 va_end (args);
8796 if (length)
8798 char *tmp;
8799 va_start (args, function);
8800 length += strlen ("expected ");
8801 buffer = tmp = (char *) alloca (length);
8802 length = 0;
8803 while ((code = (enum tree_code) va_arg (args, int)))
8805 const char *prefix = length ? " or " : "expected ";
8807 strcpy (tmp + length, prefix);
8808 length += strlen (prefix);
8809 strcpy (tmp + length, get_tree_code_name (code));
8810 length += strlen (get_tree_code_name (code));
8812 va_end (args);
8814 else
8815 buffer = "unexpected node";
8817 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8818 buffer, get_tree_code_name (TREE_CODE (node)),
8819 function, trim_filename (file), line);
8822 /* Complain that the tree code of NODE does match the expected 0
8823 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
8824 the caller. */
8826 void
8827 tree_not_check_failed (const_tree node, const char *file,
8828 int line, const char *function, ...)
8830 va_list args;
8831 char *buffer;
8832 unsigned length = 0;
8833 enum tree_code code;
8835 va_start (args, function);
8836 while ((code = (enum tree_code) va_arg (args, int)))
8837 length += 4 + strlen (get_tree_code_name (code));
8838 va_end (args);
8839 va_start (args, function);
8840 buffer = (char *) alloca (length);
8841 length = 0;
8842 while ((code = (enum tree_code) va_arg (args, int)))
8844 if (length)
8846 strcpy (buffer + length, " or ");
8847 length += 4;
8849 strcpy (buffer + length, get_tree_code_name (code));
8850 length += strlen (get_tree_code_name (code));
8852 va_end (args);
8854 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
8855 buffer, get_tree_code_name (TREE_CODE (node)),
8856 function, trim_filename (file), line);
8859 /* Similar to tree_check_failed, except that we check for a class of tree
8860 code, given in CL. */
8862 void
8863 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
8864 const char *file, int line, const char *function)
8866 internal_error
8867 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
8868 TREE_CODE_CLASS_STRING (cl),
8869 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
8870 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
8873 /* Similar to tree_check_failed, except that instead of specifying a
8874 dozen codes, use the knowledge that they're all sequential. */
8876 void
8877 tree_range_check_failed (const_tree node, const char *file, int line,
8878 const char *function, enum tree_code c1,
8879 enum tree_code c2)
8881 char *buffer;
8882 unsigned length = 0;
8883 unsigned int c;
8885 for (c = c1; c <= c2; ++c)
8886 length += 4 + strlen (get_tree_code_name ((enum tree_code) c));
8888 length += strlen ("expected ");
8889 buffer = (char *) alloca (length);
8890 length = 0;
8892 for (c = c1; c <= c2; ++c)
8894 const char *prefix = length ? " or " : "expected ";
8896 strcpy (buffer + length, prefix);
8897 length += strlen (prefix);
8898 strcpy (buffer + length, get_tree_code_name ((enum tree_code) c));
8899 length += strlen (get_tree_code_name ((enum tree_code) c));
8902 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8903 buffer, get_tree_code_name (TREE_CODE (node)),
8904 function, trim_filename (file), line);
8908 /* Similar to tree_check_failed, except that we check that a tree does
8909 not have the specified code, given in CL. */
8911 void
8912 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
8913 const char *file, int line, const char *function)
8915 internal_error
8916 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
8917 TREE_CODE_CLASS_STRING (cl),
8918 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
8919 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
8923 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
8925 void
8926 omp_clause_check_failed (const_tree node, const char *file, int line,
8927 const char *function, enum omp_clause_code code)
8929 internal_error ("tree check: expected %<omp_clause %s%>, have %qs "
8930 "in %s, at %s:%d",
8931 omp_clause_code_name[code],
8932 get_tree_code_name (TREE_CODE (node)),
8933 function, trim_filename (file), line);
8937 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
8939 void
8940 omp_clause_range_check_failed (const_tree node, const char *file, int line,
8941 const char *function, enum omp_clause_code c1,
8942 enum omp_clause_code c2)
8944 char *buffer;
8945 unsigned length = 0;
8946 unsigned int c;
8948 for (c = c1; c <= c2; ++c)
8949 length += 4 + strlen (omp_clause_code_name[c]);
8951 length += strlen ("expected ");
8952 buffer = (char *) alloca (length);
8953 length = 0;
8955 for (c = c1; c <= c2; ++c)
8957 const char *prefix = length ? " or " : "expected ";
8959 strcpy (buffer + length, prefix);
8960 length += strlen (prefix);
8961 strcpy (buffer + length, omp_clause_code_name[c]);
8962 length += strlen (omp_clause_code_name[c]);
8965 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8966 buffer, omp_clause_code_name[TREE_CODE (node)],
8967 function, trim_filename (file), line);
8971 #undef DEFTREESTRUCT
8972 #define DEFTREESTRUCT(VAL, NAME) NAME,
8974 static const char *ts_enum_names[] = {
8975 #include "treestruct.def"
8977 #undef DEFTREESTRUCT
8979 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
8981 /* Similar to tree_class_check_failed, except that we check for
8982 whether CODE contains the tree structure identified by EN. */
8984 void
8985 tree_contains_struct_check_failed (const_tree node,
8986 const enum tree_node_structure_enum en,
8987 const char *file, int line,
8988 const char *function)
8990 internal_error
8991 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
8992 TS_ENUM_NAME (en),
8993 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
8997 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
8998 (dynamically sized) vector. */
9000 void
9001 tree_int_cst_elt_check_failed (int idx, int len, const char *file, int line,
9002 const char *function)
9004 internal_error
9005 ("tree check: accessed elt %d of %<tree_int_cst%> with %d elts in %s, "
9006 "at %s:%d",
9007 idx + 1, len, function, trim_filename (file), line);
9010 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9011 (dynamically sized) vector. */
9013 void
9014 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9015 const char *function)
9017 internal_error
9018 ("tree check: accessed elt %d of %<tree_vec%> with %d elts in %s, at %s:%d",
9019 idx + 1, len, function, trim_filename (file), line);
9022 /* Similar to above, except that the check is for the bounds of the operand
9023 vector of an expression node EXP. */
9025 void
9026 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9027 int line, const char *function)
9029 enum tree_code code = TREE_CODE (exp);
9030 internal_error
9031 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9032 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9033 function, trim_filename (file), line);
9036 /* Similar to above, except that the check is for the number of
9037 operands of an OMP_CLAUSE node. */
9039 void
9040 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9041 int line, const char *function)
9043 internal_error
9044 ("tree check: accessed operand %d of %<omp_clause %s%> with %d operands "
9045 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9046 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9047 trim_filename (file), line);
9049 #endif /* ENABLE_TREE_CHECKING */
9051 /* Create a new vector type node holding NUNITS units of type INNERTYPE,
9052 and mapped to the machine mode MODE. Initialize its fields and build
9053 the information necessary for debugging output. */
9055 static tree
9056 make_vector_type (tree innertype, poly_int64 nunits, machine_mode mode)
9058 tree t;
9059 tree mv_innertype = TYPE_MAIN_VARIANT (innertype);
9061 t = make_node (VECTOR_TYPE);
9062 TREE_TYPE (t) = mv_innertype;
9063 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9064 SET_TYPE_MODE (t, mode);
9066 if (TYPE_STRUCTURAL_EQUALITY_P (mv_innertype) || in_lto_p)
9067 SET_TYPE_STRUCTURAL_EQUALITY (t);
9068 else if ((TYPE_CANONICAL (mv_innertype) != innertype
9069 || mode != VOIDmode)
9070 && !VECTOR_BOOLEAN_TYPE_P (t))
9071 TYPE_CANONICAL (t)
9072 = make_vector_type (TYPE_CANONICAL (mv_innertype), nunits, VOIDmode);
9074 layout_type (t);
9076 hashval_t hash = type_hash_canon_hash (t);
9077 t = type_hash_canon (hash, t);
9079 /* We have built a main variant, based on the main variant of the
9080 inner type. Use it to build the variant we return. */
9081 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9082 && TREE_TYPE (t) != innertype)
9083 return build_type_attribute_qual_variant (t,
9084 TYPE_ATTRIBUTES (innertype),
9085 TYPE_QUALS (innertype));
9087 return t;
9090 static tree
9091 make_or_reuse_type (unsigned size, int unsignedp)
9093 int i;
9095 if (size == INT_TYPE_SIZE)
9096 return unsignedp ? unsigned_type_node : integer_type_node;
9097 if (size == CHAR_TYPE_SIZE)
9098 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9099 if (size == SHORT_TYPE_SIZE)
9100 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9101 if (size == LONG_TYPE_SIZE)
9102 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9103 if (size == LONG_LONG_TYPE_SIZE)
9104 return (unsignedp ? long_long_unsigned_type_node
9105 : long_long_integer_type_node);
9107 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9108 if (size == int_n_data[i].bitsize
9109 && int_n_enabled_p[i])
9110 return (unsignedp ? int_n_trees[i].unsigned_type
9111 : int_n_trees[i].signed_type);
9113 if (unsignedp)
9114 return make_unsigned_type (size);
9115 else
9116 return make_signed_type (size);
9119 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9121 static tree
9122 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9124 if (satp)
9126 if (size == SHORT_FRACT_TYPE_SIZE)
9127 return unsignedp ? sat_unsigned_short_fract_type_node
9128 : sat_short_fract_type_node;
9129 if (size == FRACT_TYPE_SIZE)
9130 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9131 if (size == LONG_FRACT_TYPE_SIZE)
9132 return unsignedp ? sat_unsigned_long_fract_type_node
9133 : sat_long_fract_type_node;
9134 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9135 return unsignedp ? sat_unsigned_long_long_fract_type_node
9136 : sat_long_long_fract_type_node;
9138 else
9140 if (size == SHORT_FRACT_TYPE_SIZE)
9141 return unsignedp ? unsigned_short_fract_type_node
9142 : short_fract_type_node;
9143 if (size == FRACT_TYPE_SIZE)
9144 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9145 if (size == LONG_FRACT_TYPE_SIZE)
9146 return unsignedp ? unsigned_long_fract_type_node
9147 : long_fract_type_node;
9148 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9149 return unsignedp ? unsigned_long_long_fract_type_node
9150 : long_long_fract_type_node;
9153 return make_fract_type (size, unsignedp, satp);
9156 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9158 static tree
9159 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9161 if (satp)
9163 if (size == SHORT_ACCUM_TYPE_SIZE)
9164 return unsignedp ? sat_unsigned_short_accum_type_node
9165 : sat_short_accum_type_node;
9166 if (size == ACCUM_TYPE_SIZE)
9167 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9168 if (size == LONG_ACCUM_TYPE_SIZE)
9169 return unsignedp ? sat_unsigned_long_accum_type_node
9170 : sat_long_accum_type_node;
9171 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9172 return unsignedp ? sat_unsigned_long_long_accum_type_node
9173 : sat_long_long_accum_type_node;
9175 else
9177 if (size == SHORT_ACCUM_TYPE_SIZE)
9178 return unsignedp ? unsigned_short_accum_type_node
9179 : short_accum_type_node;
9180 if (size == ACCUM_TYPE_SIZE)
9181 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9182 if (size == LONG_ACCUM_TYPE_SIZE)
9183 return unsignedp ? unsigned_long_accum_type_node
9184 : long_accum_type_node;
9185 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9186 return unsignedp ? unsigned_long_long_accum_type_node
9187 : long_long_accum_type_node;
9190 return make_accum_type (size, unsignedp, satp);
9194 /* Create an atomic variant node for TYPE. This routine is called
9195 during initialization of data types to create the 5 basic atomic
9196 types. The generic build_variant_type function requires these to
9197 already be set up in order to function properly, so cannot be
9198 called from there. If ALIGN is non-zero, then ensure alignment is
9199 overridden to this value. */
9201 static tree
9202 build_atomic_base (tree type, unsigned int align)
9204 tree t;
9206 /* Make sure its not already registered. */
9207 if ((t = get_qualified_type (type, TYPE_QUAL_ATOMIC)))
9208 return t;
9210 t = build_variant_type_copy (type);
9211 set_type_quals (t, TYPE_QUAL_ATOMIC);
9213 if (align)
9214 SET_TYPE_ALIGN (t, align);
9216 return t;
9219 /* Information about the _FloatN and _FloatNx types. This must be in
9220 the same order as the corresponding TI_* enum values. */
9221 const floatn_type_info floatn_nx_types[NUM_FLOATN_NX_TYPES] =
9223 { 16, false },
9224 { 32, false },
9225 { 64, false },
9226 { 128, false },
9227 { 32, true },
9228 { 64, true },
9229 { 128, true },
9233 /* Create nodes for all integer types (and error_mark_node) using the sizes
9234 of C datatypes. SIGNED_CHAR specifies whether char is signed. */
9236 void
9237 build_common_tree_nodes (bool signed_char)
9239 int i;
9241 error_mark_node = make_node (ERROR_MARK);
9242 TREE_TYPE (error_mark_node) = error_mark_node;
9244 initialize_sizetypes ();
9246 /* Define both `signed char' and `unsigned char'. */
9247 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9248 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9249 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9250 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9252 /* Define `char', which is like either `signed char' or `unsigned char'
9253 but not the same as either. */
9254 char_type_node
9255 = (signed_char
9256 ? make_signed_type (CHAR_TYPE_SIZE)
9257 : make_unsigned_type (CHAR_TYPE_SIZE));
9258 TYPE_STRING_FLAG (char_type_node) = 1;
9260 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9261 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9262 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9263 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9264 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9265 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9266 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9267 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9269 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9271 int_n_trees[i].signed_type = make_signed_type (int_n_data[i].bitsize);
9272 int_n_trees[i].unsigned_type = make_unsigned_type (int_n_data[i].bitsize);
9274 if (int_n_enabled_p[i])
9276 integer_types[itk_intN_0 + i * 2] = int_n_trees[i].signed_type;
9277 integer_types[itk_unsigned_intN_0 + i * 2] = int_n_trees[i].unsigned_type;
9281 /* Define a boolean type. This type only represents boolean values but
9282 may be larger than char depending on the value of BOOL_TYPE_SIZE. */
9283 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9284 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9285 TYPE_PRECISION (boolean_type_node) = 1;
9286 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9288 /* Define what type to use for size_t. */
9289 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9290 size_type_node = unsigned_type_node;
9291 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9292 size_type_node = long_unsigned_type_node;
9293 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9294 size_type_node = long_long_unsigned_type_node;
9295 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9296 size_type_node = short_unsigned_type_node;
9297 else
9299 int i;
9301 size_type_node = NULL_TREE;
9302 for (i = 0; i < NUM_INT_N_ENTS; i++)
9303 if (int_n_enabled_p[i])
9305 char name[50], altname[50];
9306 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
9307 sprintf (altname, "__int%d__ unsigned", int_n_data[i].bitsize);
9309 if (strcmp (name, SIZE_TYPE) == 0
9310 || strcmp (altname, SIZE_TYPE) == 0)
9312 size_type_node = int_n_trees[i].unsigned_type;
9315 if (size_type_node == NULL_TREE)
9316 gcc_unreachable ();
9319 /* Define what type to use for ptrdiff_t. */
9320 if (strcmp (PTRDIFF_TYPE, "int") == 0)
9321 ptrdiff_type_node = integer_type_node;
9322 else if (strcmp (PTRDIFF_TYPE, "long int") == 0)
9323 ptrdiff_type_node = long_integer_type_node;
9324 else if (strcmp (PTRDIFF_TYPE, "long long int") == 0)
9325 ptrdiff_type_node = long_long_integer_type_node;
9326 else if (strcmp (PTRDIFF_TYPE, "short int") == 0)
9327 ptrdiff_type_node = short_integer_type_node;
9328 else
9330 ptrdiff_type_node = NULL_TREE;
9331 for (int i = 0; i < NUM_INT_N_ENTS; i++)
9332 if (int_n_enabled_p[i])
9334 char name[50], altname[50];
9335 sprintf (name, "__int%d", int_n_data[i].bitsize);
9336 sprintf (altname, "__int%d__", int_n_data[i].bitsize);
9338 if (strcmp (name, PTRDIFF_TYPE) == 0
9339 || strcmp (altname, PTRDIFF_TYPE) == 0)
9340 ptrdiff_type_node = int_n_trees[i].signed_type;
9342 if (ptrdiff_type_node == NULL_TREE)
9343 gcc_unreachable ();
9346 /* Fill in the rest of the sized types. Reuse existing type nodes
9347 when possible. */
9348 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9349 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9350 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9351 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9352 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9354 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9355 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9356 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9357 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9358 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9360 /* Don't call build_qualified type for atomics. That routine does
9361 special processing for atomics, and until they are initialized
9362 it's better not to make that call.
9364 Check to see if there is a target override for atomic types. */
9366 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
9367 targetm.atomic_align_for_mode (QImode));
9368 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
9369 targetm.atomic_align_for_mode (HImode));
9370 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
9371 targetm.atomic_align_for_mode (SImode));
9372 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
9373 targetm.atomic_align_for_mode (DImode));
9374 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
9375 targetm.atomic_align_for_mode (TImode));
9377 access_public_node = get_identifier ("public");
9378 access_protected_node = get_identifier ("protected");
9379 access_private_node = get_identifier ("private");
9381 /* Define these next since types below may used them. */
9382 integer_zero_node = build_int_cst (integer_type_node, 0);
9383 integer_one_node = build_int_cst (integer_type_node, 1);
9384 integer_three_node = build_int_cst (integer_type_node, 3);
9385 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9387 size_zero_node = size_int (0);
9388 size_one_node = size_int (1);
9389 bitsize_zero_node = bitsize_int (0);
9390 bitsize_one_node = bitsize_int (1);
9391 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9393 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9394 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9396 void_type_node = make_node (VOID_TYPE);
9397 layout_type (void_type_node);
9399 /* We are not going to have real types in C with less than byte alignment,
9400 so we might as well not have any types that claim to have it. */
9401 SET_TYPE_ALIGN (void_type_node, BITS_PER_UNIT);
9402 TYPE_USER_ALIGN (void_type_node) = 0;
9404 void_node = make_node (VOID_CST);
9405 TREE_TYPE (void_node) = void_type_node;
9407 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9408 layout_type (TREE_TYPE (null_pointer_node));
9410 ptr_type_node = build_pointer_type (void_type_node);
9411 const_ptr_type_node
9412 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9413 for (unsigned i = 0; i < ARRAY_SIZE (builtin_structptr_types); ++i)
9414 builtin_structptr_types[i].node = builtin_structptr_types[i].base;
9416 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
9418 float_type_node = make_node (REAL_TYPE);
9419 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9420 layout_type (float_type_node);
9422 double_type_node = make_node (REAL_TYPE);
9423 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9424 layout_type (double_type_node);
9426 long_double_type_node = make_node (REAL_TYPE);
9427 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9428 layout_type (long_double_type_node);
9430 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
9432 int n = floatn_nx_types[i].n;
9433 bool extended = floatn_nx_types[i].extended;
9434 scalar_float_mode mode;
9435 if (!targetm.floatn_mode (n, extended).exists (&mode))
9436 continue;
9437 int precision = GET_MODE_PRECISION (mode);
9438 /* Work around the rs6000 KFmode having precision 113 not
9439 128. */
9440 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
9441 gcc_assert (fmt->b == 2 && fmt->emin + fmt->emax == 3);
9442 int min_precision = fmt->p + ceil_log2 (fmt->emax - fmt->emin);
9443 if (!extended)
9444 gcc_assert (min_precision == n);
9445 if (precision < min_precision)
9446 precision = min_precision;
9447 FLOATN_NX_TYPE_NODE (i) = make_node (REAL_TYPE);
9448 TYPE_PRECISION (FLOATN_NX_TYPE_NODE (i)) = precision;
9449 layout_type (FLOATN_NX_TYPE_NODE (i));
9450 SET_TYPE_MODE (FLOATN_NX_TYPE_NODE (i), mode);
9453 float_ptr_type_node = build_pointer_type (float_type_node);
9454 double_ptr_type_node = build_pointer_type (double_type_node);
9455 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9456 integer_ptr_type_node = build_pointer_type (integer_type_node);
9458 /* Fixed size integer types. */
9459 uint16_type_node = make_or_reuse_type (16, 1);
9460 uint32_type_node = make_or_reuse_type (32, 1);
9461 uint64_type_node = make_or_reuse_type (64, 1);
9462 if (targetm.scalar_mode_supported_p (TImode))
9463 uint128_type_node = make_or_reuse_type (128, 1);
9465 /* Decimal float types. */
9466 if (targetm.decimal_float_supported_p ())
9468 dfloat32_type_node = make_node (REAL_TYPE);
9469 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9470 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9471 layout_type (dfloat32_type_node);
9473 dfloat64_type_node = make_node (REAL_TYPE);
9474 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9475 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9476 layout_type (dfloat64_type_node);
9478 dfloat128_type_node = make_node (REAL_TYPE);
9479 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9480 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9481 layout_type (dfloat128_type_node);
9484 complex_integer_type_node = build_complex_type (integer_type_node, true);
9485 complex_float_type_node = build_complex_type (float_type_node, true);
9486 complex_double_type_node = build_complex_type (double_type_node, true);
9487 complex_long_double_type_node = build_complex_type (long_double_type_node,
9488 true);
9490 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
9492 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
9493 COMPLEX_FLOATN_NX_TYPE_NODE (i)
9494 = build_complex_type (FLOATN_NX_TYPE_NODE (i));
9497 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9498 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9499 sat_ ## KIND ## _type_node = \
9500 make_sat_signed_ ## KIND ## _type (SIZE); \
9501 sat_unsigned_ ## KIND ## _type_node = \
9502 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9503 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9504 unsigned_ ## KIND ## _type_node = \
9505 make_unsigned_ ## KIND ## _type (SIZE);
9507 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9508 sat_ ## WIDTH ## KIND ## _type_node = \
9509 make_sat_signed_ ## KIND ## _type (SIZE); \
9510 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9511 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9512 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9513 unsigned_ ## WIDTH ## KIND ## _type_node = \
9514 make_unsigned_ ## KIND ## _type (SIZE);
9516 /* Make fixed-point type nodes based on four different widths. */
9517 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9518 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9519 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9520 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9521 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9523 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9524 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9525 NAME ## _type_node = \
9526 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9527 u ## NAME ## _type_node = \
9528 make_or_reuse_unsigned_ ## KIND ## _type \
9529 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9530 sat_ ## NAME ## _type_node = \
9531 make_or_reuse_sat_signed_ ## KIND ## _type \
9532 (GET_MODE_BITSIZE (MODE ## mode)); \
9533 sat_u ## NAME ## _type_node = \
9534 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9535 (GET_MODE_BITSIZE (U ## MODE ## mode));
9537 /* Fixed-point type and mode nodes. */
9538 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9539 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9540 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9541 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9542 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9543 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9544 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9545 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9546 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9547 MAKE_FIXED_MODE_NODE (accum, da, DA)
9548 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9551 tree t = targetm.build_builtin_va_list ();
9553 /* Many back-ends define record types without setting TYPE_NAME.
9554 If we copied the record type here, we'd keep the original
9555 record type without a name. This breaks name mangling. So,
9556 don't copy record types and let c_common_nodes_and_builtins()
9557 declare the type to be __builtin_va_list. */
9558 if (TREE_CODE (t) != RECORD_TYPE)
9559 t = build_variant_type_copy (t);
9561 va_list_type_node = t;
9564 /* SCEV analyzer global shared trees. */
9565 chrec_dont_know = make_node (SCEV_NOT_KNOWN);
9566 TREE_TYPE (chrec_dont_know) = void_type_node;
9567 chrec_known = make_node (SCEV_KNOWN);
9568 TREE_TYPE (chrec_known) = void_type_node;
9571 /* Modify DECL for given flags.
9572 TM_PURE attribute is set only on types, so the function will modify
9573 DECL's type when ECF_TM_PURE is used. */
9575 void
9576 set_call_expr_flags (tree decl, int flags)
9578 if (flags & ECF_NOTHROW)
9579 TREE_NOTHROW (decl) = 1;
9580 if (flags & ECF_CONST)
9581 TREE_READONLY (decl) = 1;
9582 if (flags & ECF_PURE)
9583 DECL_PURE_P (decl) = 1;
9584 if (flags & ECF_LOOPING_CONST_OR_PURE)
9585 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9586 if (flags & ECF_NOVOPS)
9587 DECL_IS_NOVOPS (decl) = 1;
9588 if (flags & ECF_NORETURN)
9589 TREE_THIS_VOLATILE (decl) = 1;
9590 if (flags & ECF_MALLOC)
9591 DECL_IS_MALLOC (decl) = 1;
9592 if (flags & ECF_RETURNS_TWICE)
9593 DECL_IS_RETURNS_TWICE (decl) = 1;
9594 if (flags & ECF_LEAF)
9595 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9596 NULL, DECL_ATTRIBUTES (decl));
9597 if (flags & ECF_COLD)
9598 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("cold"),
9599 NULL, DECL_ATTRIBUTES (decl));
9600 if (flags & ECF_RET1)
9601 DECL_ATTRIBUTES (decl)
9602 = tree_cons (get_identifier ("fn spec"),
9603 build_tree_list (NULL_TREE, build_string (2, "1 ")),
9604 DECL_ATTRIBUTES (decl));
9605 if ((flags & ECF_TM_PURE) && flag_tm)
9606 apply_tm_attr (decl, get_identifier ("transaction_pure"));
9607 /* Looping const or pure is implied by noreturn.
9608 There is currently no way to declare looping const or looping pure alone. */
9609 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9610 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9614 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9616 static void
9617 local_define_builtin (const char *name, tree type, enum built_in_function code,
9618 const char *library_name, int ecf_flags)
9620 tree decl;
9622 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9623 library_name, NULL_TREE);
9624 set_call_expr_flags (decl, ecf_flags);
9626 set_builtin_decl (code, decl, true);
9629 /* Call this function after instantiating all builtins that the language
9630 front end cares about. This will build the rest of the builtins
9631 and internal functions that are relied upon by the tree optimizers and
9632 the middle-end. */
9634 void
9635 build_common_builtin_nodes (void)
9637 tree tmp, ftype;
9638 int ecf_flags;
9640 if (!builtin_decl_explicit_p (BUILT_IN_CLEAR_PADDING))
9642 ftype = build_function_type_list (void_type_node,
9643 ptr_type_node,
9644 ptr_type_node,
9645 integer_type_node,
9646 NULL_TREE);
9647 local_define_builtin ("__builtin_clear_padding", ftype,
9648 BUILT_IN_CLEAR_PADDING,
9649 "__builtin_clear_padding",
9650 ECF_LEAF | ECF_NOTHROW);
9653 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE)
9654 || !builtin_decl_explicit_p (BUILT_IN_TRAP)
9655 || !builtin_decl_explicit_p (BUILT_IN_ABORT))
9657 ftype = build_function_type (void_type_node, void_list_node);
9658 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9659 local_define_builtin ("__builtin_unreachable", ftype,
9660 BUILT_IN_UNREACHABLE,
9661 "__builtin_unreachable",
9662 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9663 | ECF_CONST | ECF_COLD);
9664 if (!builtin_decl_explicit_p (BUILT_IN_ABORT))
9665 local_define_builtin ("__builtin_abort", ftype, BUILT_IN_ABORT,
9666 "abort",
9667 ECF_LEAF | ECF_NORETURN | ECF_CONST | ECF_COLD);
9668 if (!builtin_decl_explicit_p (BUILT_IN_TRAP))
9669 local_define_builtin ("__builtin_trap", ftype, BUILT_IN_TRAP,
9670 "__builtin_trap",
9671 ECF_NORETURN | ECF_NOTHROW | ECF_LEAF | ECF_COLD);
9674 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9675 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9677 ftype = build_function_type_list (ptr_type_node,
9678 ptr_type_node, const_ptr_type_node,
9679 size_type_node, NULL_TREE);
9681 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
9682 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9683 "memcpy", ECF_NOTHROW | ECF_LEAF);
9684 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9685 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9686 "memmove", ECF_NOTHROW | ECF_LEAF);
9689 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
9691 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9692 const_ptr_type_node, size_type_node,
9693 NULL_TREE);
9694 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9695 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9698 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
9700 ftype = build_function_type_list (ptr_type_node,
9701 ptr_type_node, integer_type_node,
9702 size_type_node, NULL_TREE);
9703 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9704 "memset", ECF_NOTHROW | ECF_LEAF);
9707 /* If we're checking the stack, `alloca' can throw. */
9708 const int alloca_flags
9709 = ECF_MALLOC | ECF_LEAF | (flag_stack_check ? 0 : ECF_NOTHROW);
9711 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
9713 ftype = build_function_type_list (ptr_type_node,
9714 size_type_node, NULL_TREE);
9715 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9716 "alloca", alloca_flags);
9719 ftype = build_function_type_list (ptr_type_node, size_type_node,
9720 size_type_node, NULL_TREE);
9721 local_define_builtin ("__builtin_alloca_with_align", ftype,
9722 BUILT_IN_ALLOCA_WITH_ALIGN,
9723 "__builtin_alloca_with_align",
9724 alloca_flags);
9726 ftype = build_function_type_list (ptr_type_node, size_type_node,
9727 size_type_node, size_type_node, NULL_TREE);
9728 local_define_builtin ("__builtin_alloca_with_align_and_max", ftype,
9729 BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX,
9730 "__builtin_alloca_with_align_and_max",
9731 alloca_flags);
9733 ftype = build_function_type_list (void_type_node,
9734 ptr_type_node, ptr_type_node,
9735 ptr_type_node, NULL_TREE);
9736 local_define_builtin ("__builtin_init_trampoline", ftype,
9737 BUILT_IN_INIT_TRAMPOLINE,
9738 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9739 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
9740 BUILT_IN_INIT_HEAP_TRAMPOLINE,
9741 "__builtin_init_heap_trampoline",
9742 ECF_NOTHROW | ECF_LEAF);
9743 local_define_builtin ("__builtin_init_descriptor", ftype,
9744 BUILT_IN_INIT_DESCRIPTOR,
9745 "__builtin_init_descriptor", ECF_NOTHROW | ECF_LEAF);
9747 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9748 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9749 BUILT_IN_ADJUST_TRAMPOLINE,
9750 "__builtin_adjust_trampoline",
9751 ECF_CONST | ECF_NOTHROW);
9752 local_define_builtin ("__builtin_adjust_descriptor", ftype,
9753 BUILT_IN_ADJUST_DESCRIPTOR,
9754 "__builtin_adjust_descriptor",
9755 ECF_CONST | ECF_NOTHROW);
9757 ftype = build_function_type_list (void_type_node,
9758 ptr_type_node, ptr_type_node, NULL_TREE);
9759 if (!builtin_decl_explicit_p (BUILT_IN_CLEAR_CACHE))
9760 local_define_builtin ("__builtin___clear_cache", ftype,
9761 BUILT_IN_CLEAR_CACHE,
9762 "__clear_cache",
9763 ECF_NOTHROW);
9765 local_define_builtin ("__builtin_nonlocal_goto", ftype,
9766 BUILT_IN_NONLOCAL_GOTO,
9767 "__builtin_nonlocal_goto",
9768 ECF_NORETURN | ECF_NOTHROW);
9770 ftype = build_function_type_list (void_type_node,
9771 ptr_type_node, ptr_type_node, NULL_TREE);
9772 local_define_builtin ("__builtin_setjmp_setup", ftype,
9773 BUILT_IN_SETJMP_SETUP,
9774 "__builtin_setjmp_setup", ECF_NOTHROW);
9776 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9777 local_define_builtin ("__builtin_setjmp_receiver", ftype,
9778 BUILT_IN_SETJMP_RECEIVER,
9779 "__builtin_setjmp_receiver", ECF_NOTHROW | ECF_LEAF);
9781 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9782 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
9783 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9785 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9786 local_define_builtin ("__builtin_stack_restore", ftype,
9787 BUILT_IN_STACK_RESTORE,
9788 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9790 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9791 const_ptr_type_node, size_type_node,
9792 NULL_TREE);
9793 local_define_builtin ("__builtin_memcmp_eq", ftype, BUILT_IN_MEMCMP_EQ,
9794 "__builtin_memcmp_eq",
9795 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9797 local_define_builtin ("__builtin_strncmp_eq", ftype, BUILT_IN_STRNCMP_EQ,
9798 "__builtin_strncmp_eq",
9799 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9801 local_define_builtin ("__builtin_strcmp_eq", ftype, BUILT_IN_STRCMP_EQ,
9802 "__builtin_strcmp_eq",
9803 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9805 /* If there's a possibility that we might use the ARM EABI, build the
9806 alternate __cxa_end_cleanup node used to resume from C++. */
9807 if (targetm.arm_eabi_unwinder)
9809 ftype = build_function_type_list (void_type_node, NULL_TREE);
9810 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
9811 BUILT_IN_CXA_END_CLEANUP,
9812 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
9815 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9816 local_define_builtin ("__builtin_unwind_resume", ftype,
9817 BUILT_IN_UNWIND_RESUME,
9818 ((targetm_common.except_unwind_info (&global_options)
9819 == UI_SJLJ)
9820 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
9821 ECF_NORETURN);
9823 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
9825 ftype = build_function_type_list (ptr_type_node, integer_type_node,
9826 NULL_TREE);
9827 local_define_builtin ("__builtin_return_address", ftype,
9828 BUILT_IN_RETURN_ADDRESS,
9829 "__builtin_return_address",
9830 ECF_NOTHROW);
9833 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
9834 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9836 ftype = build_function_type_list (void_type_node, ptr_type_node,
9837 ptr_type_node, NULL_TREE);
9838 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
9839 local_define_builtin ("__cyg_profile_func_enter", ftype,
9840 BUILT_IN_PROFILE_FUNC_ENTER,
9841 "__cyg_profile_func_enter", 0);
9842 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9843 local_define_builtin ("__cyg_profile_func_exit", ftype,
9844 BUILT_IN_PROFILE_FUNC_EXIT,
9845 "__cyg_profile_func_exit", 0);
9848 /* The exception object and filter values from the runtime. The argument
9849 must be zero before exception lowering, i.e. from the front end. After
9850 exception lowering, it will be the region number for the exception
9851 landing pad. These functions are PURE instead of CONST to prevent
9852 them from being hoisted past the exception edge that will initialize
9853 its value in the landing pad. */
9854 ftype = build_function_type_list (ptr_type_node,
9855 integer_type_node, NULL_TREE);
9856 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
9857 /* Only use TM_PURE if we have TM language support. */
9858 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
9859 ecf_flags |= ECF_TM_PURE;
9860 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
9861 "__builtin_eh_pointer", ecf_flags);
9863 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
9864 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
9865 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
9866 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9868 ftype = build_function_type_list (void_type_node,
9869 integer_type_node, integer_type_node,
9870 NULL_TREE);
9871 local_define_builtin ("__builtin_eh_copy_values", ftype,
9872 BUILT_IN_EH_COPY_VALUES,
9873 "__builtin_eh_copy_values", ECF_NOTHROW);
9875 /* Complex multiplication and division. These are handled as builtins
9876 rather than optabs because emit_library_call_value doesn't support
9877 complex. Further, we can do slightly better with folding these
9878 beasties if the real and complex parts of the arguments are separate. */
9880 int mode;
9882 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
9884 char mode_name_buf[4], *q;
9885 const char *p;
9886 enum built_in_function mcode, dcode;
9887 tree type, inner_type;
9888 const char *prefix = "__";
9890 if (targetm.libfunc_gnu_prefix)
9891 prefix = "__gnu_";
9893 type = lang_hooks.types.type_for_mode ((machine_mode) mode, 0);
9894 if (type == NULL)
9895 continue;
9896 inner_type = TREE_TYPE (type);
9898 ftype = build_function_type_list (type, inner_type, inner_type,
9899 inner_type, inner_type, NULL_TREE);
9901 mcode = ((enum built_in_function)
9902 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9903 dcode = ((enum built_in_function)
9904 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9906 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
9907 *q = TOLOWER (*p);
9908 *q = '\0';
9910 /* For -ftrapping-math these should throw from a former
9911 -fnon-call-exception stmt. */
9912 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
9913 NULL);
9914 local_define_builtin (built_in_names[mcode], ftype, mcode,
9915 built_in_names[mcode],
9916 ECF_CONST | ECF_LEAF);
9918 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
9919 NULL);
9920 local_define_builtin (built_in_names[dcode], ftype, dcode,
9921 built_in_names[dcode],
9922 ECF_CONST | ECF_LEAF);
9926 init_internal_fns ();
9929 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
9930 better way.
9932 If we requested a pointer to a vector, build up the pointers that
9933 we stripped off while looking for the inner type. Similarly for
9934 return values from functions.
9936 The argument TYPE is the top of the chain, and BOTTOM is the
9937 new type which we will point to. */
9939 tree
9940 reconstruct_complex_type (tree type, tree bottom)
9942 tree inner, outer;
9944 if (TREE_CODE (type) == POINTER_TYPE)
9946 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9947 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
9948 TYPE_REF_CAN_ALIAS_ALL (type));
9950 else if (TREE_CODE (type) == REFERENCE_TYPE)
9952 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9953 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
9954 TYPE_REF_CAN_ALIAS_ALL (type));
9956 else if (TREE_CODE (type) == ARRAY_TYPE)
9958 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9959 outer = build_array_type (inner, TYPE_DOMAIN (type));
9961 else if (TREE_CODE (type) == FUNCTION_TYPE)
9963 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9964 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
9966 else if (TREE_CODE (type) == METHOD_TYPE)
9968 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9969 /* The build_method_type_directly() routine prepends 'this' to argument list,
9970 so we must compensate by getting rid of it. */
9971 outer
9972 = build_method_type_directly
9973 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
9974 inner,
9975 TREE_CHAIN (TYPE_ARG_TYPES (type)));
9977 else if (TREE_CODE (type) == OFFSET_TYPE)
9979 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9980 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
9982 else
9983 return bottom;
9985 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
9986 TYPE_QUALS (type));
9989 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
9990 the inner type. */
9991 tree
9992 build_vector_type_for_mode (tree innertype, machine_mode mode)
9994 poly_int64 nunits;
9995 unsigned int bitsize;
9997 switch (GET_MODE_CLASS (mode))
9999 case MODE_VECTOR_BOOL:
10000 case MODE_VECTOR_INT:
10001 case MODE_VECTOR_FLOAT:
10002 case MODE_VECTOR_FRACT:
10003 case MODE_VECTOR_UFRACT:
10004 case MODE_VECTOR_ACCUM:
10005 case MODE_VECTOR_UACCUM:
10006 nunits = GET_MODE_NUNITS (mode);
10007 break;
10009 case MODE_INT:
10010 /* Check that there are no leftover bits. */
10011 bitsize = GET_MODE_BITSIZE (as_a <scalar_int_mode> (mode));
10012 gcc_assert (bitsize % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10013 nunits = bitsize / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10014 break;
10016 default:
10017 gcc_unreachable ();
10020 return make_vector_type (innertype, nunits, mode);
10023 /* Similarly, but takes the inner type and number of units, which must be
10024 a power of two. */
10026 tree
10027 build_vector_type (tree innertype, poly_int64 nunits)
10029 return make_vector_type (innertype, nunits, VOIDmode);
10032 /* Build a truth vector with NUNITS units, giving it mode MASK_MODE. */
10034 tree
10035 build_truth_vector_type_for_mode (poly_uint64 nunits, machine_mode mask_mode)
10037 gcc_assert (mask_mode != BLKmode);
10039 unsigned HOST_WIDE_INT esize;
10040 if (VECTOR_MODE_P (mask_mode))
10042 poly_uint64 vsize = GET_MODE_BITSIZE (mask_mode);
10043 esize = vector_element_size (vsize, nunits);
10045 else
10046 esize = 1;
10048 tree bool_type = build_nonstandard_boolean_type (esize);
10050 return make_vector_type (bool_type, nunits, mask_mode);
10053 /* Build a vector type that holds one boolean result for each element of
10054 vector type VECTYPE. The public interface for this operation is
10055 truth_type_for. */
10057 static tree
10058 build_truth_vector_type_for (tree vectype)
10060 machine_mode vector_mode = TYPE_MODE (vectype);
10061 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
10063 machine_mode mask_mode;
10064 if (VECTOR_MODE_P (vector_mode)
10065 && targetm.vectorize.get_mask_mode (vector_mode).exists (&mask_mode))
10066 return build_truth_vector_type_for_mode (nunits, mask_mode);
10068 poly_uint64 vsize = tree_to_poly_uint64 (TYPE_SIZE (vectype));
10069 unsigned HOST_WIDE_INT esize = vector_element_size (vsize, nunits);
10070 tree bool_type = build_nonstandard_boolean_type (esize);
10072 return make_vector_type (bool_type, nunits, VOIDmode);
10075 /* Like build_vector_type, but builds a variant type with TYPE_VECTOR_OPAQUE
10076 set. */
10078 tree
10079 build_opaque_vector_type (tree innertype, poly_int64 nunits)
10081 tree t = make_vector_type (innertype, nunits, VOIDmode);
10082 tree cand;
10083 /* We always build the non-opaque variant before the opaque one,
10084 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10085 cand = TYPE_NEXT_VARIANT (t);
10086 if (cand
10087 && TYPE_VECTOR_OPAQUE (cand)
10088 && check_qualified_type (cand, t, TYPE_QUALS (t)))
10089 return cand;
10090 /* Othewise build a variant type and make sure to queue it after
10091 the non-opaque type. */
10092 cand = build_distinct_type_copy (t);
10093 TYPE_VECTOR_OPAQUE (cand) = true;
10094 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10095 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10096 TYPE_NEXT_VARIANT (t) = cand;
10097 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10098 return cand;
10101 /* Return the value of element I of VECTOR_CST T as a wide_int. */
10103 static poly_wide_int
10104 vector_cst_int_elt (const_tree t, unsigned int i)
10106 /* First handle elements that are directly encoded. */
10107 unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
10108 if (i < encoded_nelts)
10109 return wi::to_poly_wide (VECTOR_CST_ENCODED_ELT (t, i));
10111 /* Identify the pattern that contains element I and work out the index of
10112 the last encoded element for that pattern. */
10113 unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
10114 unsigned int pattern = i % npatterns;
10115 unsigned int count = i / npatterns;
10116 unsigned int final_i = encoded_nelts - npatterns + pattern;
10118 /* If there are no steps, the final encoded value is the right one. */
10119 if (!VECTOR_CST_STEPPED_P (t))
10120 return wi::to_poly_wide (VECTOR_CST_ENCODED_ELT (t, final_i));
10122 /* Otherwise work out the value from the last two encoded elements. */
10123 tree v1 = VECTOR_CST_ENCODED_ELT (t, final_i - npatterns);
10124 tree v2 = VECTOR_CST_ENCODED_ELT (t, final_i);
10125 poly_wide_int diff = wi::to_poly_wide (v2) - wi::to_poly_wide (v1);
10126 return wi::to_poly_wide (v2) + (count - 2) * diff;
10129 /* Return the value of element I of VECTOR_CST T. */
10131 tree
10132 vector_cst_elt (const_tree t, unsigned int i)
10134 /* First handle elements that are directly encoded. */
10135 unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
10136 if (i < encoded_nelts)
10137 return VECTOR_CST_ENCODED_ELT (t, i);
10139 /* If there are no steps, the final encoded value is the right one. */
10140 if (!VECTOR_CST_STEPPED_P (t))
10142 /* Identify the pattern that contains element I and work out the index of
10143 the last encoded element for that pattern. */
10144 unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
10145 unsigned int pattern = i % npatterns;
10146 unsigned int final_i = encoded_nelts - npatterns + pattern;
10147 return VECTOR_CST_ENCODED_ELT (t, final_i);
10150 /* Otherwise work out the value from the last two encoded elements. */
10151 return wide_int_to_tree (TREE_TYPE (TREE_TYPE (t)),
10152 vector_cst_int_elt (t, i));
10155 /* Given an initializer INIT, return TRUE if INIT is zero or some
10156 aggregate of zeros. Otherwise return FALSE. If NONZERO is not
10157 null, set *NONZERO if and only if INIT is known not to be all
10158 zeros. The combination of return value of false and *NONZERO
10159 false implies that INIT may but need not be all zeros. Other
10160 combinations indicate definitive answers. */
10162 bool
10163 initializer_zerop (const_tree init, bool *nonzero /* = NULL */)
10165 bool dummy;
10166 if (!nonzero)
10167 nonzero = &dummy;
10169 /* Conservatively clear NONZERO and set it only if INIT is definitely
10170 not all zero. */
10171 *nonzero = false;
10173 STRIP_NOPS (init);
10175 unsigned HOST_WIDE_INT off = 0;
10177 switch (TREE_CODE (init))
10179 case INTEGER_CST:
10180 if (integer_zerop (init))
10181 return true;
10183 *nonzero = true;
10184 return false;
10186 case REAL_CST:
10187 /* ??? Note that this is not correct for C4X float formats. There,
10188 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10189 negative exponent. */
10190 if (real_zerop (init)
10191 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init)))
10192 return true;
10194 *nonzero = true;
10195 return false;
10197 case FIXED_CST:
10198 if (fixed_zerop (init))
10199 return true;
10201 *nonzero = true;
10202 return false;
10204 case COMPLEX_CST:
10205 if (integer_zerop (init)
10206 || (real_zerop (init)
10207 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10208 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init)))))
10209 return true;
10211 *nonzero = true;
10212 return false;
10214 case VECTOR_CST:
10215 if (VECTOR_CST_NPATTERNS (init) == 1
10216 && VECTOR_CST_DUPLICATE_P (init)
10217 && initializer_zerop (VECTOR_CST_ENCODED_ELT (init, 0)))
10218 return true;
10220 *nonzero = true;
10221 return false;
10223 case CONSTRUCTOR:
10225 if (TREE_CLOBBER_P (init))
10226 return false;
10228 unsigned HOST_WIDE_INT idx;
10229 tree elt;
10231 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10232 if (!initializer_zerop (elt, nonzero))
10233 return false;
10235 return true;
10238 case MEM_REF:
10240 tree arg = TREE_OPERAND (init, 0);
10241 if (TREE_CODE (arg) != ADDR_EXPR)
10242 return false;
10243 tree offset = TREE_OPERAND (init, 1);
10244 if (TREE_CODE (offset) != INTEGER_CST
10245 || !tree_fits_uhwi_p (offset))
10246 return false;
10247 off = tree_to_uhwi (offset);
10248 if (INT_MAX < off)
10249 return false;
10250 arg = TREE_OPERAND (arg, 0);
10251 if (TREE_CODE (arg) != STRING_CST)
10252 return false;
10253 init = arg;
10255 /* Fall through. */
10257 case STRING_CST:
10259 gcc_assert (off <= INT_MAX);
10261 int i = off;
10262 int n = TREE_STRING_LENGTH (init);
10263 if (n <= i)
10264 return false;
10266 /* We need to loop through all elements to handle cases like
10267 "\0" and "\0foobar". */
10268 for (i = 0; i < n; ++i)
10269 if (TREE_STRING_POINTER (init)[i] != '\0')
10271 *nonzero = true;
10272 return false;
10275 return true;
10278 default:
10279 return false;
10283 /* Return true if EXPR is an initializer expression in which every element
10284 is a constant that is numerically equal to 0 or 1. The elements do not
10285 need to be equal to each other. */
10287 bool
10288 initializer_each_zero_or_onep (const_tree expr)
10290 STRIP_ANY_LOCATION_WRAPPER (expr);
10292 switch (TREE_CODE (expr))
10294 case INTEGER_CST:
10295 return integer_zerop (expr) || integer_onep (expr);
10297 case REAL_CST:
10298 return real_zerop (expr) || real_onep (expr);
10300 case VECTOR_CST:
10302 unsigned HOST_WIDE_INT nelts = vector_cst_encoded_nelts (expr);
10303 if (VECTOR_CST_STEPPED_P (expr)
10304 && !TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr)).is_constant (&nelts))
10305 return false;
10307 for (unsigned int i = 0; i < nelts; ++i)
10309 tree elt = vector_cst_elt (expr, i);
10310 if (!initializer_each_zero_or_onep (elt))
10311 return false;
10314 return true;
10317 default:
10318 return false;
10322 /* Check if vector VEC consists of all the equal elements and
10323 that the number of elements corresponds to the type of VEC.
10324 The function returns first element of the vector
10325 or NULL_TREE if the vector is not uniform. */
10326 tree
10327 uniform_vector_p (const_tree vec)
10329 tree first, t;
10330 unsigned HOST_WIDE_INT i, nelts;
10332 if (vec == NULL_TREE)
10333 return NULL_TREE;
10335 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10337 if (TREE_CODE (vec) == VEC_DUPLICATE_EXPR)
10338 return TREE_OPERAND (vec, 0);
10340 else if (TREE_CODE (vec) == VECTOR_CST)
10342 if (VECTOR_CST_NPATTERNS (vec) == 1 && VECTOR_CST_DUPLICATE_P (vec))
10343 return VECTOR_CST_ENCODED_ELT (vec, 0);
10344 return NULL_TREE;
10347 else if (TREE_CODE (vec) == CONSTRUCTOR
10348 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)).is_constant (&nelts))
10350 first = error_mark_node;
10352 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10354 if (i == 0)
10356 first = t;
10357 continue;
10359 if (!operand_equal_p (first, t, 0))
10360 return NULL_TREE;
10362 if (i != nelts)
10363 return NULL_TREE;
10365 if (TREE_CODE (first) == CONSTRUCTOR || TREE_CODE (first) == VECTOR_CST)
10366 return uniform_vector_p (first);
10367 return first;
10370 return NULL_TREE;
10373 /* If the argument is INTEGER_CST, return it. If the argument is vector
10374 with all elements the same INTEGER_CST, return that INTEGER_CST. Otherwise
10375 return NULL_TREE.
10376 Look through location wrappers. */
10378 tree
10379 uniform_integer_cst_p (tree t)
10381 STRIP_ANY_LOCATION_WRAPPER (t);
10383 if (TREE_CODE (t) == INTEGER_CST)
10384 return t;
10386 if (VECTOR_TYPE_P (TREE_TYPE (t)))
10388 t = uniform_vector_p (t);
10389 if (t && TREE_CODE (t) == INTEGER_CST)
10390 return t;
10393 return NULL_TREE;
10396 /* Checks to see if T is a constant or a constant vector and if each element E
10397 adheres to ~E + 1 == pow2 then return ~E otherwise NULL_TREE. */
10399 tree
10400 bitmask_inv_cst_vector_p (tree t)
10403 tree_code code = TREE_CODE (t);
10404 tree type = TREE_TYPE (t);
10406 if (!INTEGRAL_TYPE_P (type)
10407 && !VECTOR_INTEGER_TYPE_P (type))
10408 return NULL_TREE;
10410 unsigned HOST_WIDE_INT nelts = 1;
10411 tree cst;
10412 unsigned int idx = 0;
10413 bool uniform = uniform_integer_cst_p (t);
10414 tree newtype = unsigned_type_for (type);
10415 tree_vector_builder builder;
10416 if (code == INTEGER_CST)
10417 cst = t;
10418 else
10420 if (!VECTOR_CST_NELTS (t).is_constant (&nelts))
10421 return NULL_TREE;
10423 cst = vector_cst_elt (t, 0);
10424 builder.new_vector (newtype, nelts, 1);
10427 tree ty = unsigned_type_for (TREE_TYPE (cst));
10431 if (idx > 0)
10432 cst = vector_cst_elt (t, idx);
10433 wide_int icst = wi::to_wide (cst);
10434 wide_int inv = wi::bit_not (icst);
10435 icst = wi::add (1, inv);
10436 if (wi::popcount (icst) != 1)
10437 return NULL_TREE;
10439 tree newcst = wide_int_to_tree (ty, inv);
10441 if (uniform)
10442 return build_uniform_cst (newtype, newcst);
10444 builder.quick_push (newcst);
10446 while (++idx < nelts);
10448 return builder.build ();
10451 /* If VECTOR_CST T has a single nonzero element, return the index of that
10452 element, otherwise return -1. */
10455 single_nonzero_element (const_tree t)
10457 unsigned HOST_WIDE_INT nelts;
10458 unsigned int repeat_nelts;
10459 if (VECTOR_CST_NELTS (t).is_constant (&nelts))
10460 repeat_nelts = nelts;
10461 else if (VECTOR_CST_NELTS_PER_PATTERN (t) == 2)
10463 nelts = vector_cst_encoded_nelts (t);
10464 repeat_nelts = VECTOR_CST_NPATTERNS (t);
10466 else
10467 return -1;
10469 int res = -1;
10470 for (unsigned int i = 0; i < nelts; ++i)
10472 tree elt = vector_cst_elt (t, i);
10473 if (!integer_zerop (elt) && !real_zerop (elt))
10475 if (res >= 0 || i >= repeat_nelts)
10476 return -1;
10477 res = i;
10480 return res;
10483 /* Build an empty statement at location LOC. */
10485 tree
10486 build_empty_stmt (location_t loc)
10488 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10489 SET_EXPR_LOCATION (t, loc);
10490 return t;
10494 /* Build an OMP clause with code CODE. LOC is the location of the
10495 clause. */
10497 tree
10498 build_omp_clause (location_t loc, enum omp_clause_code code)
10500 tree t;
10501 int size, length;
10503 length = omp_clause_num_ops[code];
10504 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10506 record_node_allocation_statistics (OMP_CLAUSE, size);
10508 t = (tree) ggc_internal_alloc (size);
10509 memset (t, 0, size);
10510 TREE_SET_CODE (t, OMP_CLAUSE);
10511 OMP_CLAUSE_SET_CODE (t, code);
10512 OMP_CLAUSE_LOCATION (t) = loc;
10514 return t;
10517 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10518 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10519 Except for the CODE and operand count field, other storage for the
10520 object is initialized to zeros. */
10522 tree
10523 build_vl_exp (enum tree_code code, int len MEM_STAT_DECL)
10525 tree t;
10526 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10528 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10529 gcc_assert (len >= 1);
10531 record_node_allocation_statistics (code, length);
10533 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10535 TREE_SET_CODE (t, code);
10537 /* Can't use TREE_OPERAND to store the length because if checking is
10538 enabled, it will try to check the length before we store it. :-P */
10539 t->exp.operands[0] = build_int_cst (sizetype, len);
10541 return t;
10544 /* Helper function for build_call_* functions; build a CALL_EXPR with
10545 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10546 the argument slots. */
10548 static tree
10549 build_call_1 (tree return_type, tree fn, int nargs)
10551 tree t;
10553 t = build_vl_exp (CALL_EXPR, nargs + 3);
10554 TREE_TYPE (t) = return_type;
10555 CALL_EXPR_FN (t) = fn;
10556 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10558 return t;
10561 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10562 FN and a null static chain slot. NARGS is the number of call arguments
10563 which are specified as "..." arguments. */
10565 tree
10566 build_call_nary (tree return_type, tree fn, int nargs, ...)
10568 tree ret;
10569 va_list args;
10570 va_start (args, nargs);
10571 ret = build_call_valist (return_type, fn, nargs, args);
10572 va_end (args);
10573 return ret;
10576 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10577 FN and a null static chain slot. NARGS is the number of call arguments
10578 which are specified as a va_list ARGS. */
10580 tree
10581 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10583 tree t;
10584 int i;
10586 t = build_call_1 (return_type, fn, nargs);
10587 for (i = 0; i < nargs; i++)
10588 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10589 process_call_operands (t);
10590 return t;
10593 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10594 FN and a null static chain slot. NARGS is the number of call arguments
10595 which are specified as a tree array ARGS. */
10597 tree
10598 build_call_array_loc (location_t loc, tree return_type, tree fn,
10599 int nargs, const tree *args)
10601 tree t;
10602 int i;
10604 t = build_call_1 (return_type, fn, nargs);
10605 for (i = 0; i < nargs; i++)
10606 CALL_EXPR_ARG (t, i) = args[i];
10607 process_call_operands (t);
10608 SET_EXPR_LOCATION (t, loc);
10609 return t;
10612 /* Like build_call_array, but takes a vec. */
10614 tree
10615 build_call_vec (tree return_type, tree fn, const vec<tree, va_gc> *args)
10617 tree ret, t;
10618 unsigned int ix;
10620 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10621 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10622 CALL_EXPR_ARG (ret, ix) = t;
10623 process_call_operands (ret);
10624 return ret;
10627 /* Conveniently construct a function call expression. FNDECL names the
10628 function to be called and N arguments are passed in the array
10629 ARGARRAY. */
10631 tree
10632 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
10634 tree fntype = TREE_TYPE (fndecl);
10635 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10637 return fold_build_call_array_loc (loc, TREE_TYPE (fntype), fn, n, argarray);
10640 /* Conveniently construct a function call expression. FNDECL names the
10641 function to be called and the arguments are passed in the vector
10642 VEC. */
10644 tree
10645 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
10647 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
10648 vec_safe_address (vec));
10652 /* Conveniently construct a function call expression. FNDECL names the
10653 function to be called, N is the number of arguments, and the "..."
10654 parameters are the argument expressions. */
10656 tree
10657 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
10659 va_list ap;
10660 tree *argarray = XALLOCAVEC (tree, n);
10661 int i;
10663 va_start (ap, n);
10664 for (i = 0; i < n; i++)
10665 argarray[i] = va_arg (ap, tree);
10666 va_end (ap);
10667 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10670 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
10671 varargs macros aren't supported by all bootstrap compilers. */
10673 tree
10674 build_call_expr (tree fndecl, int n, ...)
10676 va_list ap;
10677 tree *argarray = XALLOCAVEC (tree, n);
10678 int i;
10680 va_start (ap, n);
10681 for (i = 0; i < n; i++)
10682 argarray[i] = va_arg (ap, tree);
10683 va_end (ap);
10684 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
10687 /* Build an internal call to IFN, with arguments ARGS[0:N-1] and with return
10688 type TYPE. This is just like CALL_EXPR, except its CALL_EXPR_FN is NULL.
10689 It will get gimplified later into an ordinary internal function. */
10691 tree
10692 build_call_expr_internal_loc_array (location_t loc, internal_fn ifn,
10693 tree type, int n, const tree *args)
10695 tree t = build_call_1 (type, NULL_TREE, n);
10696 for (int i = 0; i < n; ++i)
10697 CALL_EXPR_ARG (t, i) = args[i];
10698 SET_EXPR_LOCATION (t, loc);
10699 CALL_EXPR_IFN (t) = ifn;
10700 process_call_operands (t);
10701 return t;
10704 /* Build internal call expression. This is just like CALL_EXPR, except
10705 its CALL_EXPR_FN is NULL. It will get gimplified later into ordinary
10706 internal function. */
10708 tree
10709 build_call_expr_internal_loc (location_t loc, enum internal_fn ifn,
10710 tree type, int n, ...)
10712 va_list ap;
10713 tree *argarray = XALLOCAVEC (tree, n);
10714 int i;
10716 va_start (ap, n);
10717 for (i = 0; i < n; i++)
10718 argarray[i] = va_arg (ap, tree);
10719 va_end (ap);
10720 return build_call_expr_internal_loc_array (loc, ifn, type, n, argarray);
10723 /* Return a function call to FN, if the target is guaranteed to support it,
10724 or null otherwise.
10726 N is the number of arguments, passed in the "...", and TYPE is the
10727 type of the return value. */
10729 tree
10730 maybe_build_call_expr_loc (location_t loc, combined_fn fn, tree type,
10731 int n, ...)
10733 va_list ap;
10734 tree *argarray = XALLOCAVEC (tree, n);
10735 int i;
10737 va_start (ap, n);
10738 for (i = 0; i < n; i++)
10739 argarray[i] = va_arg (ap, tree);
10740 va_end (ap);
10741 if (internal_fn_p (fn))
10743 internal_fn ifn = as_internal_fn (fn);
10744 if (direct_internal_fn_p (ifn))
10746 tree_pair types = direct_internal_fn_types (ifn, type, argarray);
10747 if (!direct_internal_fn_supported_p (ifn, types,
10748 OPTIMIZE_FOR_BOTH))
10749 return NULL_TREE;
10751 return build_call_expr_internal_loc_array (loc, ifn, type, n, argarray);
10753 else
10755 tree fndecl = builtin_decl_implicit (as_builtin_fn (fn));
10756 if (!fndecl)
10757 return NULL_TREE;
10758 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10762 /* Return a function call to the appropriate builtin alloca variant.
10764 SIZE is the size to be allocated. ALIGN, if non-zero, is the requested
10765 alignment of the allocated area. MAX_SIZE, if non-negative, is an upper
10766 bound for SIZE in case it is not a fixed value. */
10768 tree
10769 build_alloca_call_expr (tree size, unsigned int align, HOST_WIDE_INT max_size)
10771 if (max_size >= 0)
10773 tree t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX);
10774 return
10775 build_call_expr (t, 3, size, size_int (align), size_int (max_size));
10777 else if (align > 0)
10779 tree t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
10780 return build_call_expr (t, 2, size, size_int (align));
10782 else
10784 tree t = builtin_decl_explicit (BUILT_IN_ALLOCA);
10785 return build_call_expr (t, 1, size);
10789 /* The built-in decl to use to mark code points believed to be unreachable.
10790 Typically __builtin_unreachable, but __builtin_trap if
10791 -fsanitize=unreachable -fsanitize-trap=unreachable. If only
10792 -fsanitize=unreachable, we rely on sanopt to replace calls with the
10793 appropriate ubsan function. When building a call directly, use
10794 {gimple_},build_builtin_unreachable instead. */
10796 tree
10797 builtin_decl_unreachable ()
10799 enum built_in_function fncode = BUILT_IN_UNREACHABLE;
10801 if (sanitize_flags_p (SANITIZE_UNREACHABLE)
10802 ? (flag_sanitize_trap & SANITIZE_UNREACHABLE)
10803 : flag_unreachable_traps)
10804 fncode = BUILT_IN_TRAP;
10805 /* For non-trapping sanitize, we will rewrite __builtin_unreachable () later,
10806 in the sanopt pass. */
10808 return builtin_decl_explicit (fncode);
10811 /* Build a call to __builtin_unreachable, possibly rewritten by
10812 -fsanitize=unreachable. Use this rather than the above when practical. */
10814 tree
10815 build_builtin_unreachable (location_t loc)
10817 tree data = NULL_TREE;
10818 tree fn = sanitize_unreachable_fn (&data, loc);
10819 return build_call_expr_loc (loc, fn, data != NULL_TREE, data);
10822 /* Create a new constant string literal of type ELTYPE[SIZE] (or LEN
10823 if SIZE == -1) and return a tree node representing char* pointer to
10824 it as an ADDR_EXPR (ARRAY_REF (ELTYPE, ...)). When STR is nonnull
10825 the STRING_CST value is the LEN bytes at STR (the representation
10826 of the string, which may be wide). Otherwise it's all zeros. */
10828 tree
10829 build_string_literal (unsigned len, const char *str /* = NULL */,
10830 tree eltype /* = char_type_node */,
10831 unsigned HOST_WIDE_INT size /* = -1 */)
10833 tree t = build_string (len, str);
10834 /* Set the maximum valid index based on the string length or SIZE. */
10835 unsigned HOST_WIDE_INT maxidx
10836 = (size == HOST_WIDE_INT_M1U ? len : size) - 1;
10838 tree index = build_index_type (size_int (maxidx));
10839 eltype = build_type_variant (eltype, 1, 0);
10840 tree type = build_array_type (eltype, index);
10841 TREE_TYPE (t) = type;
10842 TREE_CONSTANT (t) = 1;
10843 TREE_READONLY (t) = 1;
10844 TREE_STATIC (t) = 1;
10846 type = build_pointer_type (eltype);
10847 t = build1 (ADDR_EXPR, type,
10848 build4 (ARRAY_REF, eltype,
10849 t, integer_zero_node, NULL_TREE, NULL_TREE));
10850 return t;
10855 /* Return true if T (assumed to be a DECL) must be assigned a memory
10856 location. */
10858 bool
10859 needs_to_live_in_memory (const_tree t)
10861 return (TREE_ADDRESSABLE (t)
10862 || is_global_var (t)
10863 || (TREE_CODE (t) == RESULT_DECL
10864 && !DECL_BY_REFERENCE (t)
10865 && aggregate_value_p (t, current_function_decl)));
10868 /* Return value of a constant X and sign-extend it. */
10870 HOST_WIDE_INT
10871 int_cst_value (const_tree x)
10873 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10874 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10876 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10877 gcc_assert (cst_and_fits_in_hwi (x));
10879 if (bits < HOST_BITS_PER_WIDE_INT)
10881 bool negative = ((val >> (bits - 1)) & 1) != 0;
10882 if (negative)
10883 val |= HOST_WIDE_INT_M1U << (bits - 1) << 1;
10884 else
10885 val &= ~(HOST_WIDE_INT_M1U << (bits - 1) << 1);
10888 return val;
10891 /* If TYPE is an integral or pointer type, return an integer type with
10892 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10893 if TYPE is already an integer type of signedness UNSIGNEDP.
10894 If TYPE is a floating-point type, return an integer type with the same
10895 bitsize and with the signedness given by UNSIGNEDP; this is useful
10896 when doing bit-level operations on a floating-point value. */
10898 tree
10899 signed_or_unsigned_type_for (int unsignedp, tree type)
10901 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) == unsignedp)
10902 return type;
10904 if (TREE_CODE (type) == VECTOR_TYPE)
10906 tree inner = TREE_TYPE (type);
10907 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10908 if (!inner2)
10909 return NULL_TREE;
10910 if (inner == inner2)
10911 return type;
10912 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10915 if (TREE_CODE (type) == COMPLEX_TYPE)
10917 tree inner = TREE_TYPE (type);
10918 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10919 if (!inner2)
10920 return NULL_TREE;
10921 if (inner == inner2)
10922 return type;
10923 return build_complex_type (inner2);
10926 unsigned int bits;
10927 if (INTEGRAL_TYPE_P (type)
10928 || POINTER_TYPE_P (type)
10929 || TREE_CODE (type) == OFFSET_TYPE)
10930 bits = TYPE_PRECISION (type);
10931 else if (TREE_CODE (type) == REAL_TYPE)
10932 bits = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (type));
10933 else
10934 return NULL_TREE;
10936 return build_nonstandard_integer_type (bits, unsignedp);
10939 /* If TYPE is an integral or pointer type, return an integer type with
10940 the same precision which is unsigned, or itself if TYPE is already an
10941 unsigned integer type. If TYPE is a floating-point type, return an
10942 unsigned integer type with the same bitsize as TYPE. */
10944 tree
10945 unsigned_type_for (tree type)
10947 return signed_or_unsigned_type_for (1, type);
10950 /* If TYPE is an integral or pointer type, return an integer type with
10951 the same precision which is signed, or itself if TYPE is already a
10952 signed integer type. If TYPE is a floating-point type, return a
10953 signed integer type with the same bitsize as TYPE. */
10955 tree
10956 signed_type_for (tree type)
10958 return signed_or_unsigned_type_for (0, type);
10961 /* - For VECTOR_TYPEs:
10962 - The truth type must be a VECTOR_BOOLEAN_TYPE.
10963 - The number of elements must match (known_eq).
10964 - targetm.vectorize.get_mask_mode exists, and exactly
10965 the same mode as the truth type.
10966 - Otherwise, the truth type must be a BOOLEAN_TYPE
10967 or useless_type_conversion_p to BOOLEAN_TYPE. */
10968 bool
10969 is_truth_type_for (tree type, tree truth_type)
10971 machine_mode mask_mode = TYPE_MODE (truth_type);
10972 machine_mode vmode = TYPE_MODE (type);
10973 machine_mode tmask_mode;
10975 if (TREE_CODE (type) == VECTOR_TYPE)
10977 if (VECTOR_BOOLEAN_TYPE_P (truth_type)
10978 && known_eq (TYPE_VECTOR_SUBPARTS (type),
10979 TYPE_VECTOR_SUBPARTS (truth_type))
10980 && targetm.vectorize.get_mask_mode (vmode).exists (&tmask_mode)
10981 && tmask_mode == mask_mode)
10982 return true;
10984 return false;
10987 return useless_type_conversion_p (boolean_type_node, truth_type);
10990 /* If TYPE is a vector type, return a signed integer vector type with the
10991 same width and number of subparts. Otherwise return boolean_type_node. */
10993 tree
10994 truth_type_for (tree type)
10996 if (TREE_CODE (type) == VECTOR_TYPE)
10998 if (VECTOR_BOOLEAN_TYPE_P (type))
10999 return type;
11000 return build_truth_vector_type_for (type);
11002 else
11003 return boolean_type_node;
11006 /* Returns the largest value obtainable by casting something in INNER type to
11007 OUTER type. */
11009 tree
11010 upper_bound_in_type (tree outer, tree inner)
11012 unsigned int det = 0;
11013 unsigned oprec = TYPE_PRECISION (outer);
11014 unsigned iprec = TYPE_PRECISION (inner);
11015 unsigned prec;
11017 /* Compute a unique number for every combination. */
11018 det |= (oprec > iprec) ? 4 : 0;
11019 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
11020 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
11022 /* Determine the exponent to use. */
11023 switch (det)
11025 case 0:
11026 case 1:
11027 /* oprec <= iprec, outer: signed, inner: don't care. */
11028 prec = oprec - 1;
11029 break;
11030 case 2:
11031 case 3:
11032 /* oprec <= iprec, outer: unsigned, inner: don't care. */
11033 prec = oprec;
11034 break;
11035 case 4:
11036 /* oprec > iprec, outer: signed, inner: signed. */
11037 prec = iprec - 1;
11038 break;
11039 case 5:
11040 /* oprec > iprec, outer: signed, inner: unsigned. */
11041 prec = iprec;
11042 break;
11043 case 6:
11044 /* oprec > iprec, outer: unsigned, inner: signed. */
11045 prec = oprec;
11046 break;
11047 case 7:
11048 /* oprec > iprec, outer: unsigned, inner: unsigned. */
11049 prec = iprec;
11050 break;
11051 default:
11052 gcc_unreachable ();
11055 return wide_int_to_tree (outer,
11056 wi::mask (prec, false, TYPE_PRECISION (outer)));
11059 /* Returns the smallest value obtainable by casting something in INNER type to
11060 OUTER type. */
11062 tree
11063 lower_bound_in_type (tree outer, tree inner)
11065 unsigned oprec = TYPE_PRECISION (outer);
11066 unsigned iprec = TYPE_PRECISION (inner);
11068 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
11069 and obtain 0. */
11070 if (TYPE_UNSIGNED (outer)
11071 /* If we are widening something of an unsigned type, OUTER type
11072 contains all values of INNER type. In particular, both INNER
11073 and OUTER types have zero in common. */
11074 || (oprec > iprec && TYPE_UNSIGNED (inner)))
11075 return build_int_cst (outer, 0);
11076 else
11078 /* If we are widening a signed type to another signed type, we
11079 want to obtain -2^^(iprec-1). If we are keeping the
11080 precision or narrowing to a signed type, we want to obtain
11081 -2^(oprec-1). */
11082 unsigned prec = oprec > iprec ? iprec : oprec;
11083 return wide_int_to_tree (outer,
11084 wi::mask (prec - 1, true,
11085 TYPE_PRECISION (outer)));
11089 /* Return nonzero if two operands that are suitable for PHI nodes are
11090 necessarily equal. Specifically, both ARG0 and ARG1 must be either
11091 SSA_NAME or invariant. Note that this is strictly an optimization.
11092 That is, callers of this function can directly call operand_equal_p
11093 and get the same result, only slower. */
11096 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
11098 if (arg0 == arg1)
11099 return 1;
11100 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
11101 return 0;
11102 return operand_equal_p (arg0, arg1, 0);
11105 /* Returns number of zeros at the end of binary representation of X. */
11107 tree
11108 num_ending_zeros (const_tree x)
11110 return build_int_cst (TREE_TYPE (x), wi::ctz (wi::to_wide (x)));
11114 #define WALK_SUBTREE(NODE) \
11115 do \
11117 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
11118 if (result) \
11119 return result; \
11121 while (0)
11123 /* This is a subroutine of walk_tree that walks field of TYPE that are to
11124 be walked whenever a type is seen in the tree. Rest of operands and return
11125 value are as for walk_tree. */
11127 static tree
11128 walk_type_fields (tree type, walk_tree_fn func, void *data,
11129 hash_set<tree> *pset, walk_tree_lh lh)
11131 tree result = NULL_TREE;
11133 switch (TREE_CODE (type))
11135 case POINTER_TYPE:
11136 case REFERENCE_TYPE:
11137 case VECTOR_TYPE:
11138 /* We have to worry about mutually recursive pointers. These can't
11139 be written in C. They can in Ada. It's pathological, but
11140 there's an ACATS test (c38102a) that checks it. Deal with this
11141 by checking if we're pointing to another pointer, that one
11142 points to another pointer, that one does too, and we have no htab.
11143 If so, get a hash table. We check three levels deep to avoid
11144 the cost of the hash table if we don't need one. */
11145 if (POINTER_TYPE_P (TREE_TYPE (type))
11146 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
11147 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
11148 && !pset)
11150 result = walk_tree_without_duplicates (&TREE_TYPE (type),
11151 func, data);
11152 if (result)
11153 return result;
11155 break;
11158 /* fall through */
11160 case COMPLEX_TYPE:
11161 WALK_SUBTREE (TREE_TYPE (type));
11162 break;
11164 case METHOD_TYPE:
11165 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
11167 /* Fall through. */
11169 case FUNCTION_TYPE:
11170 WALK_SUBTREE (TREE_TYPE (type));
11172 tree arg;
11174 /* We never want to walk into default arguments. */
11175 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
11176 WALK_SUBTREE (TREE_VALUE (arg));
11178 break;
11180 case ARRAY_TYPE:
11181 /* Don't follow this nodes's type if a pointer for fear that
11182 we'll have infinite recursion. If we have a PSET, then we
11183 need not fear. */
11184 if (pset
11185 || (!POINTER_TYPE_P (TREE_TYPE (type))
11186 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
11187 WALK_SUBTREE (TREE_TYPE (type));
11188 WALK_SUBTREE (TYPE_DOMAIN (type));
11189 break;
11191 case OFFSET_TYPE:
11192 WALK_SUBTREE (TREE_TYPE (type));
11193 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
11194 break;
11196 default:
11197 break;
11200 return NULL_TREE;
11203 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
11204 called with the DATA and the address of each sub-tree. If FUNC returns a
11205 non-NULL value, the traversal is stopped, and the value returned by FUNC
11206 is returned. If PSET is non-NULL it is used to record the nodes visited,
11207 and to avoid visiting a node more than once. */
11209 tree
11210 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
11211 hash_set<tree> *pset, walk_tree_lh lh)
11213 enum tree_code code;
11214 int walk_subtrees;
11215 tree result;
11217 #define WALK_SUBTREE_TAIL(NODE) \
11218 do \
11220 tp = & (NODE); \
11221 goto tail_recurse; \
11223 while (0)
11225 tail_recurse:
11226 /* Skip empty subtrees. */
11227 if (!*tp)
11228 return NULL_TREE;
11230 /* Don't walk the same tree twice, if the user has requested
11231 that we avoid doing so. */
11232 if (pset && pset->add (*tp))
11233 return NULL_TREE;
11235 /* Call the function. */
11236 walk_subtrees = 1;
11237 result = (*func) (tp, &walk_subtrees, data);
11239 /* If we found something, return it. */
11240 if (result)
11241 return result;
11243 code = TREE_CODE (*tp);
11245 /* Even if we didn't, FUNC may have decided that there was nothing
11246 interesting below this point in the tree. */
11247 if (!walk_subtrees)
11249 /* But we still need to check our siblings. */
11250 if (code == TREE_LIST)
11251 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11252 else if (code == OMP_CLAUSE)
11253 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11254 else
11255 return NULL_TREE;
11258 if (lh)
11260 result = (*lh) (tp, &walk_subtrees, func, data, pset);
11261 if (result || !walk_subtrees)
11262 return result;
11265 switch (code)
11267 case ERROR_MARK:
11268 case IDENTIFIER_NODE:
11269 case INTEGER_CST:
11270 case REAL_CST:
11271 case FIXED_CST:
11272 case STRING_CST:
11273 case BLOCK:
11274 case PLACEHOLDER_EXPR:
11275 case SSA_NAME:
11276 case FIELD_DECL:
11277 case RESULT_DECL:
11278 /* None of these have subtrees other than those already walked
11279 above. */
11280 break;
11282 case TREE_LIST:
11283 WALK_SUBTREE (TREE_VALUE (*tp));
11284 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11286 case TREE_VEC:
11288 int len = TREE_VEC_LENGTH (*tp);
11290 if (len == 0)
11291 break;
11293 /* Walk all elements but the first. */
11294 while (--len)
11295 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
11297 /* Now walk the first one as a tail call. */
11298 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
11301 case VECTOR_CST:
11303 unsigned len = vector_cst_encoded_nelts (*tp);
11304 if (len == 0)
11305 break;
11306 /* Walk all elements but the first. */
11307 while (--len)
11308 WALK_SUBTREE (VECTOR_CST_ENCODED_ELT (*tp, len));
11309 /* Now walk the first one as a tail call. */
11310 WALK_SUBTREE_TAIL (VECTOR_CST_ENCODED_ELT (*tp, 0));
11313 case COMPLEX_CST:
11314 WALK_SUBTREE (TREE_REALPART (*tp));
11315 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
11317 case CONSTRUCTOR:
11319 unsigned HOST_WIDE_INT idx;
11320 constructor_elt *ce;
11322 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*tp), idx, &ce);
11323 idx++)
11324 WALK_SUBTREE (ce->value);
11326 break;
11328 case SAVE_EXPR:
11329 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
11331 case BIND_EXPR:
11333 tree decl;
11334 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
11336 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11337 into declarations that are just mentioned, rather than
11338 declared; they don't really belong to this part of the tree.
11339 And, we can see cycles: the initializer for a declaration
11340 can refer to the declaration itself. */
11341 WALK_SUBTREE (DECL_INITIAL (decl));
11342 WALK_SUBTREE (DECL_SIZE (decl));
11343 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11345 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
11348 case STATEMENT_LIST:
11350 tree_stmt_iterator i;
11351 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
11352 WALK_SUBTREE (*tsi_stmt_ptr (i));
11354 break;
11356 case OMP_CLAUSE:
11358 int len = omp_clause_num_ops[OMP_CLAUSE_CODE (*tp)];
11359 for (int i = 0; i < len; i++)
11360 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11361 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11364 case TARGET_EXPR:
11366 int i, len;
11368 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11369 But, we only want to walk once. */
11370 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11371 for (i = 0; i < len; ++i)
11372 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11373 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11376 case DECL_EXPR:
11377 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11378 defining. We only want to walk into these fields of a type in this
11379 case and not in the general case of a mere reference to the type.
11381 The criterion is as follows: if the field can be an expression, it
11382 must be walked only here. This should be in keeping with the fields
11383 that are directly gimplified in gimplify_type_sizes in order for the
11384 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11385 variable-sized types.
11387 Note that DECLs get walked as part of processing the BIND_EXPR. */
11388 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11390 /* Call the function for the decl so e.g. copy_tree_body_r can
11391 replace it with the remapped one. */
11392 result = (*func) (&DECL_EXPR_DECL (*tp), &walk_subtrees, data);
11393 if (result || !walk_subtrees)
11394 return result;
11396 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11397 if (TREE_CODE (*type_p) == ERROR_MARK)
11398 return NULL_TREE;
11400 /* Call the function for the type. See if it returns anything or
11401 doesn't want us to continue. If we are to continue, walk both
11402 the normal fields and those for the declaration case. */
11403 result = (*func) (type_p, &walk_subtrees, data);
11404 if (result || !walk_subtrees)
11405 return result;
11407 /* But do not walk a pointed-to type since it may itself need to
11408 be walked in the declaration case if it isn't anonymous. */
11409 if (!POINTER_TYPE_P (*type_p))
11411 result = walk_type_fields (*type_p, func, data, pset, lh);
11412 if (result)
11413 return result;
11416 /* If this is a record type, also walk the fields. */
11417 if (RECORD_OR_UNION_TYPE_P (*type_p))
11419 tree field;
11421 for (field = TYPE_FIELDS (*type_p); field;
11422 field = DECL_CHAIN (field))
11424 /* We'd like to look at the type of the field, but we can
11425 easily get infinite recursion. So assume it's pointed
11426 to elsewhere in the tree. Also, ignore things that
11427 aren't fields. */
11428 if (TREE_CODE (field) != FIELD_DECL)
11429 continue;
11431 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11432 WALK_SUBTREE (DECL_SIZE (field));
11433 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11434 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11435 WALK_SUBTREE (DECL_QUALIFIER (field));
11439 /* Same for scalar types. */
11440 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11441 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11442 || TREE_CODE (*type_p) == INTEGER_TYPE
11443 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11444 || TREE_CODE (*type_p) == REAL_TYPE)
11446 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11447 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11450 WALK_SUBTREE (TYPE_SIZE (*type_p));
11451 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11453 /* FALLTHRU */
11455 default:
11456 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11458 int i, len;
11460 /* Walk over all the sub-trees of this operand. */
11461 len = TREE_OPERAND_LENGTH (*tp);
11463 /* Go through the subtrees. We need to do this in forward order so
11464 that the scope of a FOR_EXPR is handled properly. */
11465 if (len)
11467 for (i = 0; i < len - 1; ++i)
11468 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11469 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11472 /* If this is a type, walk the needed fields in the type. */
11473 else if (TYPE_P (*tp))
11474 return walk_type_fields (*tp, func, data, pset, lh);
11475 break;
11478 /* We didn't find what we were looking for. */
11479 return NULL_TREE;
11481 #undef WALK_SUBTREE_TAIL
11483 #undef WALK_SUBTREE
11485 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11487 tree
11488 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11489 walk_tree_lh lh)
11491 tree result;
11493 hash_set<tree> pset;
11494 result = walk_tree_1 (tp, func, data, &pset, lh);
11495 return result;
11499 tree
11500 tree_block (tree t)
11502 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11504 if (IS_EXPR_CODE_CLASS (c))
11505 return LOCATION_BLOCK (t->exp.locus);
11506 gcc_unreachable ();
11507 return NULL;
11510 void
11511 tree_set_block (tree t, tree b)
11513 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11515 if (IS_EXPR_CODE_CLASS (c))
11517 t->exp.locus = set_block (t->exp.locus, b);
11519 else
11520 gcc_unreachable ();
11523 /* Create a nameless artificial label and put it in the current
11524 function context. The label has a location of LOC. Returns the
11525 newly created label. */
11527 tree
11528 create_artificial_label (location_t loc)
11530 tree lab = build_decl (loc,
11531 LABEL_DECL, NULL_TREE, void_type_node);
11533 DECL_ARTIFICIAL (lab) = 1;
11534 DECL_IGNORED_P (lab) = 1;
11535 DECL_CONTEXT (lab) = current_function_decl;
11536 return lab;
11539 /* Given a tree, try to return a useful variable name that we can use
11540 to prefix a temporary that is being assigned the value of the tree.
11541 I.E. given <temp> = &A, return A. */
11543 const char *
11544 get_name (tree t)
11546 tree stripped_decl;
11548 stripped_decl = t;
11549 STRIP_NOPS (stripped_decl);
11550 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11551 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11552 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11554 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11555 if (!name)
11556 return NULL;
11557 return IDENTIFIER_POINTER (name);
11559 else
11561 switch (TREE_CODE (stripped_decl))
11563 case ADDR_EXPR:
11564 return get_name (TREE_OPERAND (stripped_decl, 0));
11565 default:
11566 return NULL;
11571 /* Return true if TYPE has a variable argument list. */
11573 bool
11574 stdarg_p (const_tree fntype)
11576 function_args_iterator args_iter;
11577 tree n = NULL_TREE, t;
11579 if (!fntype)
11580 return false;
11582 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
11584 n = t;
11587 return n != NULL_TREE && n != void_type_node;
11590 /* Return true if TYPE has a prototype. */
11592 bool
11593 prototype_p (const_tree fntype)
11595 tree t;
11597 gcc_assert (fntype != NULL_TREE);
11599 t = TYPE_ARG_TYPES (fntype);
11600 return (t != NULL_TREE);
11603 /* If BLOCK is inlined from an __attribute__((__artificial__))
11604 routine, return pointer to location from where it has been
11605 called. */
11606 location_t *
11607 block_nonartificial_location (tree block)
11609 location_t *ret = NULL;
11611 while (block && TREE_CODE (block) == BLOCK
11612 && BLOCK_ABSTRACT_ORIGIN (block))
11614 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11615 if (TREE_CODE (ao) == FUNCTION_DECL)
11617 /* If AO is an artificial inline, point RET to the
11618 call site locus at which it has been inlined and continue
11619 the loop, in case AO's caller is also an artificial
11620 inline. */
11621 if (DECL_DECLARED_INLINE_P (ao)
11622 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11623 ret = &BLOCK_SOURCE_LOCATION (block);
11624 else
11625 break;
11627 else if (TREE_CODE (ao) != BLOCK)
11628 break;
11630 block = BLOCK_SUPERCONTEXT (block);
11632 return ret;
11636 /* If EXP is inlined from an __attribute__((__artificial__))
11637 function, return the location of the original call expression. */
11639 location_t
11640 tree_nonartificial_location (tree exp)
11642 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11644 if (loc)
11645 return *loc;
11646 else
11647 return EXPR_LOCATION (exp);
11650 /* Return the location into which EXP has been inlined. Analogous
11651 to tree_nonartificial_location() above but not limited to artificial
11652 functions declared inline. If SYSTEM_HEADER is true, return
11653 the macro expansion point of the location if it's in a system header */
11655 location_t
11656 tree_inlined_location (tree exp, bool system_header /* = true */)
11658 location_t loc = UNKNOWN_LOCATION;
11660 tree block = TREE_BLOCK (exp);
11662 while (block && TREE_CODE (block) == BLOCK
11663 && BLOCK_ABSTRACT_ORIGIN (block))
11665 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11666 if (TREE_CODE (ao) == FUNCTION_DECL)
11667 loc = BLOCK_SOURCE_LOCATION (block);
11668 else if (TREE_CODE (ao) != BLOCK)
11669 break;
11671 block = BLOCK_SUPERCONTEXT (block);
11674 if (loc == UNKNOWN_LOCATION)
11676 loc = EXPR_LOCATION (exp);
11677 if (system_header)
11678 /* Only consider macro expansion when the block traversal failed
11679 to find a location. Otherwise it's not relevant. */
11680 return expansion_point_location_if_in_system_header (loc);
11683 return loc;
11686 /* These are the hash table functions for the hash table of OPTIMIZATION_NODE
11687 nodes. */
11689 /* Return the hash code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11691 hashval_t
11692 cl_option_hasher::hash (tree x)
11694 const_tree const t = x;
11696 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11697 return cl_optimization_hash (TREE_OPTIMIZATION (t));
11698 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11699 return cl_target_option_hash (TREE_TARGET_OPTION (t));
11700 else
11701 gcc_unreachable ();
11704 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11705 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11706 same. */
11708 bool
11709 cl_option_hasher::equal (tree x, tree y)
11711 const_tree const xt = x;
11712 const_tree const yt = y;
11714 if (TREE_CODE (xt) != TREE_CODE (yt))
11715 return 0;
11717 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11718 return cl_optimization_option_eq (TREE_OPTIMIZATION (xt),
11719 TREE_OPTIMIZATION (yt));
11720 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11721 return cl_target_option_eq (TREE_TARGET_OPTION (xt),
11722 TREE_TARGET_OPTION (yt));
11723 else
11724 gcc_unreachable ();
11727 /* Build an OPTIMIZATION_NODE based on the options in OPTS and OPTS_SET. */
11729 tree
11730 build_optimization_node (struct gcc_options *opts,
11731 struct gcc_options *opts_set)
11733 tree t;
11735 /* Use the cache of optimization nodes. */
11737 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11738 opts, opts_set);
11740 tree *slot = cl_option_hash_table->find_slot (cl_optimization_node, INSERT);
11741 t = *slot;
11742 if (!t)
11744 /* Insert this one into the hash table. */
11745 t = cl_optimization_node;
11746 *slot = t;
11748 /* Make a new node for next time round. */
11749 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11752 return t;
11755 /* Build a TARGET_OPTION_NODE based on the options in OPTS and OPTS_SET. */
11757 tree
11758 build_target_option_node (struct gcc_options *opts,
11759 struct gcc_options *opts_set)
11761 tree t;
11763 /* Use the cache of optimization nodes. */
11765 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11766 opts, opts_set);
11768 tree *slot = cl_option_hash_table->find_slot (cl_target_option_node, INSERT);
11769 t = *slot;
11770 if (!t)
11772 /* Insert this one into the hash table. */
11773 t = cl_target_option_node;
11774 *slot = t;
11776 /* Make a new node for next time round. */
11777 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11780 return t;
11783 /* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
11784 so that they aren't saved during PCH writing. */
11786 void
11787 prepare_target_option_nodes_for_pch (void)
11789 hash_table<cl_option_hasher>::iterator iter = cl_option_hash_table->begin ();
11790 for (; iter != cl_option_hash_table->end (); ++iter)
11791 if (TREE_CODE (*iter) == TARGET_OPTION_NODE)
11792 TREE_TARGET_GLOBALS (*iter) = NULL;
11795 /* Determine the "ultimate origin" of a block. */
11797 tree
11798 block_ultimate_origin (const_tree block)
11800 tree origin = BLOCK_ABSTRACT_ORIGIN (block);
11802 if (origin == NULL_TREE)
11803 return NULL_TREE;
11804 else
11806 gcc_checking_assert ((DECL_P (origin)
11807 && DECL_ORIGIN (origin) == origin)
11808 || BLOCK_ORIGIN (origin) == origin);
11809 return origin;
11813 /* Return true iff conversion from INNER_TYPE to OUTER_TYPE generates
11814 no instruction. */
11816 bool
11817 tree_nop_conversion_p (const_tree outer_type, const_tree inner_type)
11819 /* Do not strip casts into or out of differing address spaces. */
11820 if (POINTER_TYPE_P (outer_type)
11821 && TYPE_ADDR_SPACE (TREE_TYPE (outer_type)) != ADDR_SPACE_GENERIC)
11823 if (!POINTER_TYPE_P (inner_type)
11824 || (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
11825 != TYPE_ADDR_SPACE (TREE_TYPE (inner_type))))
11826 return false;
11828 else if (POINTER_TYPE_P (inner_type)
11829 && TYPE_ADDR_SPACE (TREE_TYPE (inner_type)) != ADDR_SPACE_GENERIC)
11831 /* We already know that outer_type is not a pointer with
11832 a non-generic address space. */
11833 return false;
11836 /* Use precision rather then machine mode when we can, which gives
11837 the correct answer even for submode (bit-field) types. */
11838 if ((INTEGRAL_TYPE_P (outer_type)
11839 || POINTER_TYPE_P (outer_type)
11840 || TREE_CODE (outer_type) == OFFSET_TYPE)
11841 && (INTEGRAL_TYPE_P (inner_type)
11842 || POINTER_TYPE_P (inner_type)
11843 || TREE_CODE (inner_type) == OFFSET_TYPE))
11844 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11846 /* Otherwise fall back on comparing machine modes (e.g. for
11847 aggregate types, floats). */
11848 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11851 /* Return true iff conversion in EXP generates no instruction. Mark
11852 it inline so that we fully inline into the stripping functions even
11853 though we have two uses of this function. */
11855 static inline bool
11856 tree_nop_conversion (const_tree exp)
11858 tree outer_type, inner_type;
11860 if (location_wrapper_p (exp))
11861 return true;
11862 if (!CONVERT_EXPR_P (exp)
11863 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11864 return false;
11866 outer_type = TREE_TYPE (exp);
11867 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11868 if (!inner_type || inner_type == error_mark_node)
11869 return false;
11871 return tree_nop_conversion_p (outer_type, inner_type);
11874 /* Return true iff conversion in EXP generates no instruction. Don't
11875 consider conversions changing the signedness. */
11877 static bool
11878 tree_sign_nop_conversion (const_tree exp)
11880 tree outer_type, inner_type;
11882 if (!tree_nop_conversion (exp))
11883 return false;
11885 outer_type = TREE_TYPE (exp);
11886 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11888 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11889 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11892 /* Strip conversions from EXP according to tree_nop_conversion and
11893 return the resulting expression. */
11895 tree
11896 tree_strip_nop_conversions (tree exp)
11898 while (tree_nop_conversion (exp))
11899 exp = TREE_OPERAND (exp, 0);
11900 return exp;
11903 /* Strip conversions from EXP according to tree_sign_nop_conversion
11904 and return the resulting expression. */
11906 tree
11907 tree_strip_sign_nop_conversions (tree exp)
11909 while (tree_sign_nop_conversion (exp))
11910 exp = TREE_OPERAND (exp, 0);
11911 return exp;
11914 /* Avoid any floating point extensions from EXP. */
11915 tree
11916 strip_float_extensions (tree exp)
11918 tree sub, expt, subt;
11920 /* For floating point constant look up the narrowest type that can hold
11921 it properly and handle it like (type)(narrowest_type)constant.
11922 This way we can optimize for instance a=a*2.0 where "a" is float
11923 but 2.0 is double constant. */
11924 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11926 REAL_VALUE_TYPE orig;
11927 tree type = NULL;
11929 orig = TREE_REAL_CST (exp);
11930 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11931 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11932 type = float_type_node;
11933 else if (TYPE_PRECISION (TREE_TYPE (exp))
11934 > TYPE_PRECISION (double_type_node)
11935 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11936 type = double_type_node;
11937 if (type)
11938 return build_real_truncate (type, orig);
11941 if (!CONVERT_EXPR_P (exp))
11942 return exp;
11944 sub = TREE_OPERAND (exp, 0);
11945 subt = TREE_TYPE (sub);
11946 expt = TREE_TYPE (exp);
11948 if (!FLOAT_TYPE_P (subt))
11949 return exp;
11951 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11952 return exp;
11954 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11955 return exp;
11957 return strip_float_extensions (sub);
11960 /* Strip out all handled components that produce invariant
11961 offsets. */
11963 const_tree
11964 strip_invariant_refs (const_tree op)
11966 while (handled_component_p (op))
11968 switch (TREE_CODE (op))
11970 case ARRAY_REF:
11971 case ARRAY_RANGE_REF:
11972 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11973 || TREE_OPERAND (op, 2) != NULL_TREE
11974 || TREE_OPERAND (op, 3) != NULL_TREE)
11975 return NULL;
11976 break;
11978 case COMPONENT_REF:
11979 if (TREE_OPERAND (op, 2) != NULL_TREE)
11980 return NULL;
11981 break;
11983 default:;
11985 op = TREE_OPERAND (op, 0);
11988 return op;
11991 static GTY(()) tree gcc_eh_personality_decl;
11993 /* Return the GCC personality function decl. */
11995 tree
11996 lhd_gcc_personality (void)
11998 if (!gcc_eh_personality_decl)
11999 gcc_eh_personality_decl = build_personality_function ("gcc");
12000 return gcc_eh_personality_decl;
12003 /* TARGET is a call target of GIMPLE call statement
12004 (obtained by gimple_call_fn). Return true if it is
12005 OBJ_TYPE_REF representing an virtual call of C++ method.
12006 (As opposed to OBJ_TYPE_REF representing objc calls
12007 through a cast where middle-end devirtualization machinery
12008 can't apply.) FOR_DUMP_P is true when being called from
12009 the dump routines. */
12011 bool
12012 virtual_method_call_p (const_tree target, bool for_dump_p)
12014 if (TREE_CODE (target) != OBJ_TYPE_REF)
12015 return false;
12016 tree t = TREE_TYPE (target);
12017 gcc_checking_assert (TREE_CODE (t) == POINTER_TYPE);
12018 t = TREE_TYPE (t);
12019 if (TREE_CODE (t) == FUNCTION_TYPE)
12020 return false;
12021 gcc_checking_assert (TREE_CODE (t) == METHOD_TYPE);
12022 /* If we do not have BINFO associated, it means that type was built
12023 without devirtualization enabled. Do not consider this a virtual
12024 call. */
12025 if (!TYPE_BINFO (obj_type_ref_class (target, for_dump_p)))
12026 return false;
12027 return true;
12030 /* Lookup sub-BINFO of BINFO of TYPE at offset POS. */
12032 static tree
12033 lookup_binfo_at_offset (tree binfo, tree type, HOST_WIDE_INT pos)
12035 unsigned int i;
12036 tree base_binfo, b;
12038 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12039 if (pos == tree_to_shwi (BINFO_OFFSET (base_binfo))
12040 && types_same_for_odr (TREE_TYPE (base_binfo), type))
12041 return base_binfo;
12042 else if ((b = lookup_binfo_at_offset (base_binfo, type, pos)) != NULL)
12043 return b;
12044 return NULL;
12047 /* Try to find a base info of BINFO that would have its field decl at offset
12048 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
12049 found, return, otherwise return NULL_TREE. */
12051 tree
12052 get_binfo_at_offset (tree binfo, poly_int64 offset, tree expected_type)
12054 tree type = BINFO_TYPE (binfo);
12056 while (true)
12058 HOST_WIDE_INT pos, size;
12059 tree fld;
12060 int i;
12062 if (types_same_for_odr (type, expected_type))
12063 return binfo;
12064 if (maybe_lt (offset, 0))
12065 return NULL_TREE;
12067 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
12069 if (TREE_CODE (fld) != FIELD_DECL || !DECL_ARTIFICIAL (fld))
12070 continue;
12072 pos = int_bit_position (fld);
12073 size = tree_to_uhwi (DECL_SIZE (fld));
12074 if (known_in_range_p (offset, pos, size))
12075 break;
12077 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
12078 return NULL_TREE;
12080 /* Offset 0 indicates the primary base, whose vtable contents are
12081 represented in the binfo for the derived class. */
12082 else if (maybe_ne (offset, 0))
12084 tree found_binfo = NULL, base_binfo;
12085 /* Offsets in BINFO are in bytes relative to the whole structure
12086 while POS is in bits relative to the containing field. */
12087 int binfo_offset = (tree_to_shwi (BINFO_OFFSET (binfo)) + pos
12088 / BITS_PER_UNIT);
12090 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12091 if (tree_to_shwi (BINFO_OFFSET (base_binfo)) == binfo_offset
12092 && types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
12094 found_binfo = base_binfo;
12095 break;
12097 if (found_binfo)
12098 binfo = found_binfo;
12099 else
12100 binfo = lookup_binfo_at_offset (binfo, TREE_TYPE (fld),
12101 binfo_offset);
12104 type = TREE_TYPE (fld);
12105 offset -= pos;
12109 /* Returns true if X is a typedef decl. */
12111 bool
12112 is_typedef_decl (const_tree x)
12114 return (x && TREE_CODE (x) == TYPE_DECL
12115 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
12118 /* Returns true iff TYPE is a type variant created for a typedef. */
12120 bool
12121 typedef_variant_p (const_tree type)
12123 return is_typedef_decl (TYPE_NAME (type));
12126 /* PR 84195: Replace control characters in "unescaped" with their
12127 escaped equivalents. Allow newlines if -fmessage-length has
12128 been set to a non-zero value. This is done here, rather than
12129 where the attribute is recorded as the message length can
12130 change between these two locations. */
12132 void
12133 escaped_string::escape (const char *unescaped)
12135 char *escaped;
12136 size_t i, new_i, len;
12138 if (m_owned)
12139 free (m_str);
12141 m_str = const_cast<char *> (unescaped);
12142 m_owned = false;
12144 if (unescaped == NULL || *unescaped == 0)
12145 return;
12147 len = strlen (unescaped);
12148 escaped = NULL;
12149 new_i = 0;
12151 for (i = 0; i < len; i++)
12153 char c = unescaped[i];
12155 if (!ISCNTRL (c))
12157 if (escaped)
12158 escaped[new_i++] = c;
12159 continue;
12162 if (c != '\n' || !pp_is_wrapping_line (global_dc->printer))
12164 if (escaped == NULL)
12166 /* We only allocate space for a new string if we
12167 actually encounter a control character that
12168 needs replacing. */
12169 escaped = (char *) xmalloc (len * 2 + 1);
12170 strncpy (escaped, unescaped, i);
12171 new_i = i;
12174 escaped[new_i++] = '\\';
12176 switch (c)
12178 case '\a': escaped[new_i++] = 'a'; break;
12179 case '\b': escaped[new_i++] = 'b'; break;
12180 case '\f': escaped[new_i++] = 'f'; break;
12181 case '\n': escaped[new_i++] = 'n'; break;
12182 case '\r': escaped[new_i++] = 'r'; break;
12183 case '\t': escaped[new_i++] = 't'; break;
12184 case '\v': escaped[new_i++] = 'v'; break;
12185 default: escaped[new_i++] = '?'; break;
12188 else if (escaped)
12189 escaped[new_i++] = c;
12192 if (escaped)
12194 escaped[new_i] = 0;
12195 m_str = escaped;
12196 m_owned = true;
12200 /* Warn about a use of an identifier which was marked deprecated. Returns
12201 whether a warning was given. */
12203 bool
12204 warn_deprecated_use (tree node, tree attr)
12206 escaped_string msg;
12208 if (node == 0 || !warn_deprecated_decl)
12209 return false;
12211 if (!attr)
12213 if (DECL_P (node))
12214 attr = DECL_ATTRIBUTES (node);
12215 else if (TYPE_P (node))
12217 tree decl = TYPE_STUB_DECL (node);
12218 if (decl)
12219 attr = TYPE_ATTRIBUTES (TREE_TYPE (decl));
12220 else if ((decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (node)))
12221 != NULL_TREE)
12223 node = TREE_TYPE (decl);
12224 attr = TYPE_ATTRIBUTES (node);
12229 if (attr)
12230 attr = lookup_attribute ("deprecated", attr);
12232 if (attr)
12233 msg.escape (TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
12235 bool w = false;
12236 if (DECL_P (node))
12238 auto_diagnostic_group d;
12239 if (msg)
12240 w = warning (OPT_Wdeprecated_declarations,
12241 "%qD is deprecated: %s", node, (const char *) msg);
12242 else
12243 w = warning (OPT_Wdeprecated_declarations,
12244 "%qD is deprecated", node);
12245 if (w)
12246 inform (DECL_SOURCE_LOCATION (node), "declared here");
12248 else if (TYPE_P (node))
12250 tree what = NULL_TREE;
12251 tree decl = TYPE_STUB_DECL (node);
12253 if (TYPE_NAME (node))
12255 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12256 what = TYPE_NAME (node);
12257 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12258 && DECL_NAME (TYPE_NAME (node)))
12259 what = DECL_NAME (TYPE_NAME (node));
12262 auto_diagnostic_group d;
12263 if (what)
12265 if (msg)
12266 w = warning (OPT_Wdeprecated_declarations,
12267 "%qE is deprecated: %s", what, (const char *) msg);
12268 else
12269 w = warning (OPT_Wdeprecated_declarations,
12270 "%qE is deprecated", what);
12272 else
12274 if (msg)
12275 w = warning (OPT_Wdeprecated_declarations,
12276 "type is deprecated: %s", (const char *) msg);
12277 else
12278 w = warning (OPT_Wdeprecated_declarations,
12279 "type is deprecated");
12282 if (w && decl)
12283 inform (DECL_SOURCE_LOCATION (decl), "declared here");
12286 return w;
12289 /* Error out with an identifier which was marked 'unavailable'. */
12290 void
12291 error_unavailable_use (tree node, tree attr)
12293 escaped_string msg;
12295 if (node == 0)
12296 return;
12298 if (!attr)
12300 if (DECL_P (node))
12301 attr = DECL_ATTRIBUTES (node);
12302 else if (TYPE_P (node))
12304 tree decl = TYPE_STUB_DECL (node);
12305 if (decl)
12306 attr = lookup_attribute ("unavailable",
12307 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
12311 if (attr)
12312 attr = lookup_attribute ("unavailable", attr);
12314 if (attr)
12315 msg.escape (TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
12317 if (DECL_P (node))
12319 auto_diagnostic_group d;
12320 if (msg)
12321 error ("%qD is unavailable: %s", node, (const char *) msg);
12322 else
12323 error ("%qD is unavailable", node);
12324 inform (DECL_SOURCE_LOCATION (node), "declared here");
12326 else if (TYPE_P (node))
12328 tree what = NULL_TREE;
12329 tree decl = TYPE_STUB_DECL (node);
12331 if (TYPE_NAME (node))
12333 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12334 what = TYPE_NAME (node);
12335 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12336 && DECL_NAME (TYPE_NAME (node)))
12337 what = DECL_NAME (TYPE_NAME (node));
12340 auto_diagnostic_group d;
12341 if (what)
12343 if (msg)
12344 error ("%qE is unavailable: %s", what, (const char *) msg);
12345 else
12346 error ("%qE is unavailable", what);
12348 else
12350 if (msg)
12351 error ("type is unavailable: %s", (const char *) msg);
12352 else
12353 error ("type is unavailable");
12356 if (decl)
12357 inform (DECL_SOURCE_LOCATION (decl), "declared here");
12361 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12362 somewhere in it. */
12364 bool
12365 contains_bitfld_component_ref_p (const_tree ref)
12367 while (handled_component_p (ref))
12369 if (TREE_CODE (ref) == COMPONENT_REF
12370 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12371 return true;
12372 ref = TREE_OPERAND (ref, 0);
12375 return false;
12378 /* Try to determine whether a TRY_CATCH expression can fall through.
12379 This is a subroutine of block_may_fallthru. */
12381 static bool
12382 try_catch_may_fallthru (const_tree stmt)
12384 tree_stmt_iterator i;
12386 /* If the TRY block can fall through, the whole TRY_CATCH can
12387 fall through. */
12388 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
12389 return true;
12391 i = tsi_start (TREE_OPERAND (stmt, 1));
12392 switch (TREE_CODE (tsi_stmt (i)))
12394 case CATCH_EXPR:
12395 /* We expect to see a sequence of CATCH_EXPR trees, each with a
12396 catch expression and a body. The whole TRY_CATCH may fall
12397 through iff any of the catch bodies falls through. */
12398 for (; !tsi_end_p (i); tsi_next (&i))
12400 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
12401 return true;
12403 return false;
12405 case EH_FILTER_EXPR:
12406 /* The exception filter expression only matters if there is an
12407 exception. If the exception does not match EH_FILTER_TYPES,
12408 we will execute EH_FILTER_FAILURE, and we will fall through
12409 if that falls through. If the exception does match
12410 EH_FILTER_TYPES, the stack unwinder will continue up the
12411 stack, so we will not fall through. We don't know whether we
12412 will throw an exception which matches EH_FILTER_TYPES or not,
12413 so we just ignore EH_FILTER_TYPES and assume that we might
12414 throw an exception which doesn't match. */
12415 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
12417 default:
12418 /* This case represents statements to be executed when an
12419 exception occurs. Those statements are implicitly followed
12420 by a RESX statement to resume execution after the exception.
12421 So in this case the TRY_CATCH never falls through. */
12422 return false;
12426 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
12427 need not be 100% accurate; simply be conservative and return true if we
12428 don't know. This is used only to avoid stupidly generating extra code.
12429 If we're wrong, we'll just delete the extra code later. */
12431 bool
12432 block_may_fallthru (const_tree block)
12434 /* This CONST_CAST is okay because expr_last returns its argument
12435 unmodified and we assign it to a const_tree. */
12436 const_tree stmt = expr_last (CONST_CAST_TREE (block));
12438 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
12440 case GOTO_EXPR:
12441 case RETURN_EXPR:
12442 /* Easy cases. If the last statement of the block implies
12443 control transfer, then we can't fall through. */
12444 return false;
12446 case SWITCH_EXPR:
12447 /* If there is a default: label or case labels cover all possible
12448 SWITCH_COND values, then the SWITCH_EXPR will transfer control
12449 to some case label in all cases and all we care is whether the
12450 SWITCH_BODY falls through. */
12451 if (SWITCH_ALL_CASES_P (stmt))
12452 return block_may_fallthru (SWITCH_BODY (stmt));
12453 return true;
12455 case COND_EXPR:
12456 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
12457 return true;
12458 return block_may_fallthru (COND_EXPR_ELSE (stmt));
12460 case BIND_EXPR:
12461 return block_may_fallthru (BIND_EXPR_BODY (stmt));
12463 case TRY_CATCH_EXPR:
12464 return try_catch_may_fallthru (stmt);
12466 case TRY_FINALLY_EXPR:
12467 /* The finally clause is always executed after the try clause,
12468 so if it does not fall through, then the try-finally will not
12469 fall through. Otherwise, if the try clause does not fall
12470 through, then when the finally clause falls through it will
12471 resume execution wherever the try clause was going. So the
12472 whole try-finally will only fall through if both the try
12473 clause and the finally clause fall through. */
12474 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
12475 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
12477 case EH_ELSE_EXPR:
12478 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12480 case MODIFY_EXPR:
12481 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
12482 stmt = TREE_OPERAND (stmt, 1);
12483 else
12484 return true;
12485 /* FALLTHRU */
12487 case CALL_EXPR:
12488 /* Functions that do not return do not fall through. */
12489 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
12491 case CLEANUP_POINT_EXPR:
12492 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12494 case TARGET_EXPR:
12495 return block_may_fallthru (TREE_OPERAND (stmt, 1));
12497 case ERROR_MARK:
12498 return true;
12500 default:
12501 return lang_hooks.block_may_fallthru (stmt);
12505 /* True if we are using EH to handle cleanups. */
12506 static bool using_eh_for_cleanups_flag = false;
12508 /* This routine is called from front ends to indicate eh should be used for
12509 cleanups. */
12510 void
12511 using_eh_for_cleanups (void)
12513 using_eh_for_cleanups_flag = true;
12516 /* Query whether EH is used for cleanups. */
12517 bool
12518 using_eh_for_cleanups_p (void)
12520 return using_eh_for_cleanups_flag;
12523 /* Wrapper for tree_code_name to ensure that tree code is valid */
12524 const char *
12525 get_tree_code_name (enum tree_code code)
12527 const char *invalid = "<invalid tree code>";
12529 /* The tree_code enum promotes to signed, but we could be getting
12530 invalid values, so force an unsigned comparison. */
12531 if (unsigned (code) >= MAX_TREE_CODES)
12533 if ((unsigned)code == 0xa5a5)
12534 return "ggc_freed";
12535 return invalid;
12538 return tree_code_name[code];
12541 /* Drops the TREE_OVERFLOW flag from T. */
12543 tree
12544 drop_tree_overflow (tree t)
12546 gcc_checking_assert (TREE_OVERFLOW (t));
12548 /* For tree codes with a sharing machinery re-build the result. */
12549 if (poly_int_tree_p (t))
12550 return wide_int_to_tree (TREE_TYPE (t), wi::to_poly_wide (t));
12552 /* For VECTOR_CST, remove the overflow bits from the encoded elements
12553 and canonicalize the result. */
12554 if (TREE_CODE (t) == VECTOR_CST)
12556 tree_vector_builder builder;
12557 builder.new_unary_operation (TREE_TYPE (t), t, true);
12558 unsigned int count = builder.encoded_nelts ();
12559 for (unsigned int i = 0; i < count; ++i)
12561 tree elt = VECTOR_CST_ELT (t, i);
12562 if (TREE_OVERFLOW (elt))
12563 elt = drop_tree_overflow (elt);
12564 builder.quick_push (elt);
12566 return builder.build ();
12569 /* Otherwise, as all tcc_constants are possibly shared, copy the node
12570 and drop the flag. */
12571 t = copy_node (t);
12572 TREE_OVERFLOW (t) = 0;
12574 /* For constants that contain nested constants, drop the flag
12575 from those as well. */
12576 if (TREE_CODE (t) == COMPLEX_CST)
12578 if (TREE_OVERFLOW (TREE_REALPART (t)))
12579 TREE_REALPART (t) = drop_tree_overflow (TREE_REALPART (t));
12580 if (TREE_OVERFLOW (TREE_IMAGPART (t)))
12581 TREE_IMAGPART (t) = drop_tree_overflow (TREE_IMAGPART (t));
12584 return t;
12587 /* Given a memory reference expression T, return its base address.
12588 The base address of a memory reference expression is the main
12589 object being referenced. For instance, the base address for
12590 'array[i].fld[j]' is 'array'. You can think of this as stripping
12591 away the offset part from a memory address.
12593 This function calls handled_component_p to strip away all the inner
12594 parts of the memory reference until it reaches the base object. */
12596 tree
12597 get_base_address (tree t)
12599 if (TREE_CODE (t) == WITH_SIZE_EXPR)
12600 t = TREE_OPERAND (t, 0);
12601 while (handled_component_p (t))
12602 t = TREE_OPERAND (t, 0);
12604 if ((TREE_CODE (t) == MEM_REF
12605 || TREE_CODE (t) == TARGET_MEM_REF)
12606 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
12607 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
12609 return t;
12612 /* Return a tree of sizetype representing the size, in bytes, of the element
12613 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12615 tree
12616 array_ref_element_size (tree exp)
12618 tree aligned_size = TREE_OPERAND (exp, 3);
12619 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
12620 location_t loc = EXPR_LOCATION (exp);
12622 /* If a size was specified in the ARRAY_REF, it's the size measured
12623 in alignment units of the element type. So multiply by that value. */
12624 if (aligned_size)
12626 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
12627 sizetype from another type of the same width and signedness. */
12628 if (TREE_TYPE (aligned_size) != sizetype)
12629 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
12630 return size_binop_loc (loc, MULT_EXPR, aligned_size,
12631 size_int (TYPE_ALIGN_UNIT (elmt_type)));
12634 /* Otherwise, take the size from that of the element type. Substitute
12635 any PLACEHOLDER_EXPR that we have. */
12636 else
12637 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
12640 /* Return a tree representing the lower bound of the array mentioned in
12641 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12643 tree
12644 array_ref_low_bound (tree exp)
12646 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12648 /* If a lower bound is specified in EXP, use it. */
12649 if (TREE_OPERAND (exp, 2))
12650 return TREE_OPERAND (exp, 2);
12652 /* Otherwise, if there is a domain type and it has a lower bound, use it,
12653 substituting for a PLACEHOLDER_EXPR as needed. */
12654 if (domain_type && TYPE_MIN_VALUE (domain_type))
12655 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
12657 /* Otherwise, return a zero of the appropriate type. */
12658 tree idxtype = TREE_TYPE (TREE_OPERAND (exp, 1));
12659 return (idxtype == error_mark_node
12660 ? integer_zero_node : build_int_cst (idxtype, 0));
12663 /* Return a tree representing the upper bound of the array mentioned in
12664 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12666 tree
12667 array_ref_up_bound (tree exp)
12669 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12671 /* If there is a domain type and it has an upper bound, use it, substituting
12672 for a PLACEHOLDER_EXPR as needed. */
12673 if (domain_type && TYPE_MAX_VALUE (domain_type))
12674 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
12676 /* Otherwise fail. */
12677 return NULL_TREE;
12680 /* Returns true if REF is an array reference, component reference,
12681 or memory reference to an array at the end of a structure.
12682 If this is the case, the array may be allocated larger
12683 than its upper bound implies. */
12685 bool
12686 array_at_struct_end_p (tree ref)
12688 tree atype;
12690 if (TREE_CODE (ref) == ARRAY_REF
12691 || TREE_CODE (ref) == ARRAY_RANGE_REF)
12693 atype = TREE_TYPE (TREE_OPERAND (ref, 0));
12694 ref = TREE_OPERAND (ref, 0);
12696 else if (TREE_CODE (ref) == COMPONENT_REF
12697 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 1))) == ARRAY_TYPE)
12698 atype = TREE_TYPE (TREE_OPERAND (ref, 1));
12699 else if (TREE_CODE (ref) == MEM_REF)
12701 tree arg = TREE_OPERAND (ref, 0);
12702 if (TREE_CODE (arg) == ADDR_EXPR)
12703 arg = TREE_OPERAND (arg, 0);
12704 tree argtype = TREE_TYPE (arg);
12705 if (TREE_CODE (argtype) == RECORD_TYPE)
12707 if (tree fld = last_field (argtype))
12709 atype = TREE_TYPE (fld);
12710 if (TREE_CODE (atype) != ARRAY_TYPE)
12711 return false;
12712 if (VAR_P (arg) && DECL_SIZE (fld))
12713 return false;
12715 else
12716 return false;
12718 else
12719 return false;
12721 else
12722 return false;
12724 if (TREE_CODE (ref) == STRING_CST)
12725 return false;
12727 tree ref_to_array = ref;
12728 while (handled_component_p (ref))
12730 /* If the reference chain contains a component reference to a
12731 non-union type and there follows another field the reference
12732 is not at the end of a structure. */
12733 if (TREE_CODE (ref) == COMPONENT_REF)
12735 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
12737 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
12738 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
12739 nextf = DECL_CHAIN (nextf);
12740 if (nextf)
12741 return false;
12744 /* If we have a multi-dimensional array we do not consider
12745 a non-innermost dimension as flex array if the whole
12746 multi-dimensional array is at struct end.
12747 Same for an array of aggregates with a trailing array
12748 member. */
12749 else if (TREE_CODE (ref) == ARRAY_REF)
12750 return false;
12751 else if (TREE_CODE (ref) == ARRAY_RANGE_REF)
12753 /* If we view an underlying object as sth else then what we
12754 gathered up to now is what we have to rely on. */
12755 else if (TREE_CODE (ref) == VIEW_CONVERT_EXPR)
12756 break;
12757 else
12758 gcc_unreachable ();
12760 ref = TREE_OPERAND (ref, 0);
12763 /* The array now is at struct end. Treat flexible arrays as
12764 always subject to extend, even into just padding constrained by
12765 an underlying decl. */
12766 if (! TYPE_SIZE (atype)
12767 || ! TYPE_DOMAIN (atype)
12768 || ! TYPE_MAX_VALUE (TYPE_DOMAIN (atype)))
12769 return true;
12771 /* If the reference is based on a declared entity, the size of the array
12772 is constrained by its given domain. (Do not trust commons PR/69368). */
12773 ref = get_base_address (ref);
12774 if (ref
12775 && DECL_P (ref)
12776 && !(flag_unconstrained_commons
12777 && VAR_P (ref) && DECL_COMMON (ref))
12778 && DECL_SIZE_UNIT (ref)
12779 && TREE_CODE (DECL_SIZE_UNIT (ref)) == INTEGER_CST)
12781 /* If the object itself is the array it is not at struct end. */
12782 if (DECL_P (ref_to_array))
12783 return false;
12785 /* Check whether the array domain covers all of the available
12786 padding. */
12787 poly_int64 offset;
12788 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (atype))) != INTEGER_CST
12789 || TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))) != INTEGER_CST
12790 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (atype))) != INTEGER_CST)
12791 return true;
12792 if (! get_addr_base_and_unit_offset (ref_to_array, &offset))
12793 return true;
12795 /* If at least one extra element fits it is a flexarray. */
12796 if (known_le ((wi::to_offset (TYPE_MAX_VALUE (TYPE_DOMAIN (atype)))
12797 - wi::to_offset (TYPE_MIN_VALUE (TYPE_DOMAIN (atype)))
12798 + 2)
12799 * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (atype))),
12800 wi::to_offset (DECL_SIZE_UNIT (ref)) - offset))
12801 return true;
12803 return false;
12806 return true;
12809 /* Return a tree representing the offset, in bytes, of the field referenced
12810 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
12812 tree
12813 component_ref_field_offset (tree exp)
12815 tree aligned_offset = TREE_OPERAND (exp, 2);
12816 tree field = TREE_OPERAND (exp, 1);
12817 location_t loc = EXPR_LOCATION (exp);
12819 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
12820 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
12821 value. */
12822 if (aligned_offset)
12824 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
12825 sizetype from another type of the same width and signedness. */
12826 if (TREE_TYPE (aligned_offset) != sizetype)
12827 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
12828 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
12829 size_int (DECL_OFFSET_ALIGN (field)
12830 / BITS_PER_UNIT));
12833 /* Otherwise, take the offset from that of the field. Substitute
12834 any PLACEHOLDER_EXPR that we have. */
12835 else
12836 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
12839 /* Given the initializer INIT, return the initializer for the field
12840 DECL if it exists, otherwise null. Used to obtain the initializer
12841 for a flexible array member and determine its size. */
12843 static tree
12844 get_initializer_for (tree init, tree decl)
12846 STRIP_NOPS (init);
12848 tree fld, fld_init;
12849 unsigned HOST_WIDE_INT i;
12850 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), i, fld, fld_init)
12852 if (decl == fld)
12853 return fld_init;
12855 if (TREE_CODE (fld) == CONSTRUCTOR)
12857 fld_init = get_initializer_for (fld_init, decl);
12858 if (fld_init)
12859 return fld_init;
12863 return NULL_TREE;
12866 /* Determines the size of the member referenced by the COMPONENT_REF
12867 REF, using its initializer expression if necessary in order to
12868 determine the size of an initialized flexible array member.
12869 If non-null, set *ARK when REF refers to an interior zero-length
12870 array or a trailing one-element array.
12871 Returns the size as sizetype (which might be zero for an object
12872 with an uninitialized flexible array member) or null if the size
12873 cannot be determined. */
12875 tree
12876 component_ref_size (tree ref, special_array_member *sam /* = NULL */)
12878 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
12880 special_array_member sambuf;
12881 if (!sam)
12882 sam = &sambuf;
12883 *sam = special_array_member::none;
12885 /* The object/argument referenced by the COMPONENT_REF and its type. */
12886 tree arg = TREE_OPERAND (ref, 0);
12887 tree argtype = TREE_TYPE (arg);
12888 /* The referenced member. */
12889 tree member = TREE_OPERAND (ref, 1);
12891 tree memsize = DECL_SIZE_UNIT (member);
12892 if (memsize)
12894 tree memtype = TREE_TYPE (member);
12895 if (TREE_CODE (memtype) != ARRAY_TYPE)
12896 /* DECL_SIZE may be less than TYPE_SIZE in C++ when referring
12897 to the type of a class with a virtual base which doesn't
12898 reflect the size of the virtual's members (see pr97595).
12899 If that's the case fail for now and implement something
12900 more robust in the future. */
12901 return (tree_int_cst_equal (memsize, TYPE_SIZE_UNIT (memtype))
12902 ? memsize : NULL_TREE);
12904 bool trailing = array_at_struct_end_p (ref);
12905 bool zero_length = integer_zerop (memsize);
12906 if (!trailing && !zero_length)
12907 /* MEMBER is either an interior array or is an array with
12908 more than one element. */
12909 return memsize;
12911 if (zero_length)
12913 if (trailing)
12914 *sam = special_array_member::trail_0;
12915 else
12917 *sam = special_array_member::int_0;
12918 memsize = NULL_TREE;
12922 if (!zero_length)
12923 if (tree dom = TYPE_DOMAIN (memtype))
12924 if (tree min = TYPE_MIN_VALUE (dom))
12925 if (tree max = TYPE_MAX_VALUE (dom))
12926 if (TREE_CODE (min) == INTEGER_CST
12927 && TREE_CODE (max) == INTEGER_CST)
12929 offset_int minidx = wi::to_offset (min);
12930 offset_int maxidx = wi::to_offset (max);
12931 offset_int neltsm1 = maxidx - minidx;
12932 if (neltsm1 > 0)
12933 /* MEMBER is an array with more than one element. */
12934 return memsize;
12936 if (neltsm1 == 0)
12937 *sam = special_array_member::trail_1;
12940 /* For a reference to a zero- or one-element array member of a union
12941 use the size of the union instead of the size of the member. */
12942 if (TREE_CODE (argtype) == UNION_TYPE)
12943 memsize = TYPE_SIZE_UNIT (argtype);
12946 /* MEMBER is either a bona fide flexible array member, or a zero-length
12947 array member, or an array of length one treated as such. */
12949 /* If the reference is to a declared object and the member a true
12950 flexible array, try to determine its size from its initializer. */
12951 poly_int64 baseoff = 0;
12952 tree base = get_addr_base_and_unit_offset (ref, &baseoff);
12953 if (!base || !VAR_P (base))
12955 if (*sam != special_array_member::int_0)
12956 return NULL_TREE;
12958 if (TREE_CODE (arg) != COMPONENT_REF)
12959 return NULL_TREE;
12961 base = arg;
12962 while (TREE_CODE (base) == COMPONENT_REF)
12963 base = TREE_OPERAND (base, 0);
12964 baseoff = tree_to_poly_int64 (byte_position (TREE_OPERAND (ref, 1)));
12967 /* BASE is the declared object of which MEMBER is either a member
12968 or that is cast to ARGTYPE (e.g., a char buffer used to store
12969 an ARGTYPE object). */
12970 tree basetype = TREE_TYPE (base);
12972 /* Determine the base type of the referenced object. If it's
12973 the same as ARGTYPE and MEMBER has a known size, return it. */
12974 tree bt = basetype;
12975 if (*sam != special_array_member::int_0)
12976 while (TREE_CODE (bt) == ARRAY_TYPE)
12977 bt = TREE_TYPE (bt);
12978 bool typematch = useless_type_conversion_p (argtype, bt);
12979 if (memsize && typematch)
12980 return memsize;
12982 memsize = NULL_TREE;
12984 if (typematch)
12985 /* MEMBER is a true flexible array member. Compute its size from
12986 the initializer of the BASE object if it has one. */
12987 if (tree init = DECL_P (base) ? DECL_INITIAL (base) : NULL_TREE)
12988 if (init != error_mark_node)
12990 init = get_initializer_for (init, member);
12991 if (init)
12993 memsize = TYPE_SIZE_UNIT (TREE_TYPE (init));
12994 if (tree refsize = TYPE_SIZE_UNIT (argtype))
12996 /* Use the larger of the initializer size and the tail
12997 padding in the enclosing struct. */
12998 poly_int64 rsz = tree_to_poly_int64 (refsize);
12999 rsz -= baseoff;
13000 if (known_lt (tree_to_poly_int64 (memsize), rsz))
13001 memsize = wide_int_to_tree (TREE_TYPE (memsize), rsz);
13004 baseoff = 0;
13008 if (!memsize)
13010 if (typematch)
13012 if (DECL_P (base)
13013 && DECL_EXTERNAL (base)
13014 && bt == basetype
13015 && *sam != special_array_member::int_0)
13016 /* The size of a flexible array member of an extern struct
13017 with no initializer cannot be determined (it's defined
13018 in another translation unit and can have an initializer
13019 with an arbitrary number of elements). */
13020 return NULL_TREE;
13022 /* Use the size of the base struct or, for interior zero-length
13023 arrays, the size of the enclosing type. */
13024 memsize = TYPE_SIZE_UNIT (bt);
13026 else if (DECL_P (base))
13027 /* Use the size of the BASE object (possibly an array of some
13028 other type such as char used to store the struct). */
13029 memsize = DECL_SIZE_UNIT (base);
13030 else
13031 return NULL_TREE;
13034 /* If the flexible array member has a known size use the greater
13035 of it and the tail padding in the enclosing struct.
13036 Otherwise, when the size of the flexible array member is unknown
13037 and the referenced object is not a struct, use the size of its
13038 type when known. This detects sizes of array buffers when cast
13039 to struct types with flexible array members. */
13040 if (memsize)
13042 if (!tree_fits_poly_int64_p (memsize))
13043 return NULL_TREE;
13044 poly_int64 memsz64 = memsize ? tree_to_poly_int64 (memsize) : 0;
13045 if (known_lt (baseoff, memsz64))
13047 memsz64 -= baseoff;
13048 return wide_int_to_tree (TREE_TYPE (memsize), memsz64);
13050 return size_zero_node;
13053 /* Return "don't know" for an external non-array object since its
13054 flexible array member can be initialized to have any number of
13055 elements. Otherwise, return zero because the flexible array
13056 member has no elements. */
13057 return (DECL_P (base)
13058 && DECL_EXTERNAL (base)
13059 && (!typematch
13060 || TREE_CODE (basetype) != ARRAY_TYPE)
13061 ? NULL_TREE : size_zero_node);
13064 /* Return the machine mode of T. For vectors, returns the mode of the
13065 inner type. The main use case is to feed the result to HONOR_NANS,
13066 avoiding the BLKmode that a direct TYPE_MODE (T) might return. */
13068 machine_mode
13069 element_mode (const_tree t)
13071 if (!TYPE_P (t))
13072 t = TREE_TYPE (t);
13073 if (VECTOR_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
13074 t = TREE_TYPE (t);
13075 return TYPE_MODE (t);
13078 /* Vector types need to re-check the target flags each time we report
13079 the machine mode. We need to do this because attribute target can
13080 change the result of vector_mode_supported_p and have_regs_of_mode
13081 on a per-function basis. Thus the TYPE_MODE of a VECTOR_TYPE can
13082 change on a per-function basis. */
13083 /* ??? Possibly a better solution is to run through all the types
13084 referenced by a function and re-compute the TYPE_MODE once, rather
13085 than make the TYPE_MODE macro call a function. */
13087 machine_mode
13088 vector_type_mode (const_tree t)
13090 machine_mode mode;
13092 gcc_assert (TREE_CODE (t) == VECTOR_TYPE);
13094 mode = t->type_common.mode;
13095 if (VECTOR_MODE_P (mode)
13096 && (!targetm.vector_mode_supported_p (mode)
13097 || !have_regs_of_mode[mode]))
13099 scalar_int_mode innermode;
13101 /* For integers, try mapping it to a same-sized scalar mode. */
13102 if (is_int_mode (TREE_TYPE (t)->type_common.mode, &innermode))
13104 poly_int64 size = (TYPE_VECTOR_SUBPARTS (t)
13105 * GET_MODE_BITSIZE (innermode));
13106 scalar_int_mode mode;
13107 if (int_mode_for_size (size, 0).exists (&mode)
13108 && have_regs_of_mode[mode])
13109 return mode;
13112 return BLKmode;
13115 return mode;
13118 /* Return the size in bits of each element of vector type TYPE. */
13120 unsigned int
13121 vector_element_bits (const_tree type)
13123 gcc_checking_assert (VECTOR_TYPE_P (type));
13124 if (VECTOR_BOOLEAN_TYPE_P (type))
13125 return TYPE_PRECISION (TREE_TYPE (type));
13126 return tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)));
13129 /* Calculate the size in bits of each element of vector type TYPE
13130 and return the result as a tree of type bitsizetype. */
13132 tree
13133 vector_element_bits_tree (const_tree type)
13135 gcc_checking_assert (VECTOR_TYPE_P (type));
13136 if (VECTOR_BOOLEAN_TYPE_P (type))
13137 return bitsize_int (vector_element_bits (type));
13138 return TYPE_SIZE (TREE_TYPE (type));
13141 /* Verify that basic properties of T match TV and thus T can be a variant of
13142 TV. TV should be the more specified variant (i.e. the main variant). */
13144 static bool
13145 verify_type_variant (const_tree t, tree tv)
13147 /* Type variant can differ by:
13149 - TYPE_QUALS: TYPE_READONLY, TYPE_VOLATILE, TYPE_ATOMIC, TYPE_RESTRICT,
13150 ENCODE_QUAL_ADDR_SPACE.
13151 - main variant may be TYPE_COMPLETE_P and variant types !TYPE_COMPLETE_P
13152 in this case some values may not be set in the variant types
13153 (see TYPE_COMPLETE_P checks).
13154 - it is possible to have TYPE_ARTIFICIAL variant of non-artifical type
13155 - by TYPE_NAME and attributes (i.e. when variant originate by typedef)
13156 - TYPE_CANONICAL (TYPE_ALIAS_SET is the same among variants)
13157 - by the alignment: TYPE_ALIGN and TYPE_USER_ALIGN
13158 - during LTO by TYPE_CONTEXT if type is TYPE_FILE_SCOPE_P
13159 this is necessary to make it possible to merge types form different TUs
13160 - arrays, pointers and references may have TREE_TYPE that is a variant
13161 of TREE_TYPE of their main variants.
13162 - aggregates may have new TYPE_FIELDS list that list variants of
13163 the main variant TYPE_FIELDS.
13164 - vector types may differ by TYPE_VECTOR_OPAQUE
13167 /* Convenience macro for matching individual fields. */
13168 #define verify_variant_match(flag) \
13169 do { \
13170 if (flag (tv) != flag (t)) \
13172 error ("type variant differs by %s", #flag); \
13173 debug_tree (tv); \
13174 return false; \
13176 } while (false)
13178 /* tree_base checks. */
13180 verify_variant_match (TREE_CODE);
13181 /* FIXME: Ada builds non-artificial variants of artificial types. */
13182 #if 0
13183 if (TYPE_ARTIFICIAL (tv))
13184 verify_variant_match (TYPE_ARTIFICIAL);
13185 #endif
13186 if (POINTER_TYPE_P (tv))
13187 verify_variant_match (TYPE_REF_CAN_ALIAS_ALL);
13188 /* FIXME: TYPE_SIZES_GIMPLIFIED may differs for Ada build. */
13189 verify_variant_match (TYPE_UNSIGNED);
13190 verify_variant_match (TYPE_PACKED);
13191 if (TREE_CODE (t) == REFERENCE_TYPE)
13192 verify_variant_match (TYPE_REF_IS_RVALUE);
13193 if (AGGREGATE_TYPE_P (t))
13194 verify_variant_match (TYPE_REVERSE_STORAGE_ORDER);
13195 else
13196 verify_variant_match (TYPE_SATURATING);
13197 /* FIXME: This check trigger during libstdc++ build. */
13198 #if 0
13199 if (RECORD_OR_UNION_TYPE_P (t) && COMPLETE_TYPE_P (t))
13200 verify_variant_match (TYPE_FINAL_P);
13201 #endif
13203 /* tree_type_common checks. */
13205 if (COMPLETE_TYPE_P (t))
13207 verify_variant_match (TYPE_MODE);
13208 if (TREE_CODE (TYPE_SIZE (t)) != PLACEHOLDER_EXPR
13209 && TREE_CODE (TYPE_SIZE (tv)) != PLACEHOLDER_EXPR)
13210 verify_variant_match (TYPE_SIZE);
13211 if (TREE_CODE (TYPE_SIZE_UNIT (t)) != PLACEHOLDER_EXPR
13212 && TREE_CODE (TYPE_SIZE_UNIT (tv)) != PLACEHOLDER_EXPR
13213 && TYPE_SIZE_UNIT (t) != TYPE_SIZE_UNIT (tv))
13215 gcc_assert (!operand_equal_p (TYPE_SIZE_UNIT (t),
13216 TYPE_SIZE_UNIT (tv), 0));
13217 error ("type variant has different %<TYPE_SIZE_UNIT%>");
13218 debug_tree (tv);
13219 error ("type variant%'s %<TYPE_SIZE_UNIT%>");
13220 debug_tree (TYPE_SIZE_UNIT (tv));
13221 error ("type%'s %<TYPE_SIZE_UNIT%>");
13222 debug_tree (TYPE_SIZE_UNIT (t));
13223 return false;
13225 verify_variant_match (TYPE_NEEDS_CONSTRUCTING);
13227 verify_variant_match (TYPE_PRECISION);
13228 if (RECORD_OR_UNION_TYPE_P (t))
13229 verify_variant_match (TYPE_TRANSPARENT_AGGR);
13230 else if (TREE_CODE (t) == ARRAY_TYPE)
13231 verify_variant_match (TYPE_NONALIASED_COMPONENT);
13232 /* During LTO we merge variant lists from diferent translation units
13233 that may differ BY TYPE_CONTEXT that in turn may point
13234 to TRANSLATION_UNIT_DECL.
13235 Ada also builds variants of types with different TYPE_CONTEXT. */
13236 #if 0
13237 if (!in_lto_p || !TYPE_FILE_SCOPE_P (t))
13238 verify_variant_match (TYPE_CONTEXT);
13239 #endif
13240 if (TREE_CODE (t) == ARRAY_TYPE || TREE_CODE (t) == INTEGER_TYPE)
13241 verify_variant_match (TYPE_STRING_FLAG);
13242 if (TREE_CODE (t) == RECORD_TYPE || TREE_CODE (t) == UNION_TYPE)
13243 verify_variant_match (TYPE_CXX_ODR_P);
13244 if (TYPE_ALIAS_SET_KNOWN_P (t))
13246 error ("type variant with %<TYPE_ALIAS_SET_KNOWN_P%>");
13247 debug_tree (tv);
13248 return false;
13251 /* tree_type_non_common checks. */
13253 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
13254 and dangle the pointer from time to time. */
13255 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_VFIELD (t) != TYPE_VFIELD (tv)
13256 && (in_lto_p || !TYPE_VFIELD (tv)
13257 || TREE_CODE (TYPE_VFIELD (tv)) != TREE_LIST))
13259 error ("type variant has different %<TYPE_VFIELD%>");
13260 debug_tree (tv);
13261 return false;
13263 if ((TREE_CODE (t) == ENUMERAL_TYPE && COMPLETE_TYPE_P (t))
13264 || TREE_CODE (t) == INTEGER_TYPE
13265 || TREE_CODE (t) == BOOLEAN_TYPE
13266 || TREE_CODE (t) == REAL_TYPE
13267 || TREE_CODE (t) == FIXED_POINT_TYPE)
13269 verify_variant_match (TYPE_MAX_VALUE);
13270 verify_variant_match (TYPE_MIN_VALUE);
13272 if (TREE_CODE (t) == METHOD_TYPE)
13273 verify_variant_match (TYPE_METHOD_BASETYPE);
13274 if (TREE_CODE (t) == OFFSET_TYPE)
13275 verify_variant_match (TYPE_OFFSET_BASETYPE);
13276 if (TREE_CODE (t) == ARRAY_TYPE)
13277 verify_variant_match (TYPE_ARRAY_MAX_SIZE);
13278 /* FIXME: Be lax and allow TYPE_BINFO to be missing in variant types
13279 or even type's main variant. This is needed to make bootstrap pass
13280 and the bug seems new in GCC 5.
13281 C++ FE should be updated to make this consistent and we should check
13282 that TYPE_BINFO is always NULL for !COMPLETE_TYPE_P and otherwise there
13283 is a match with main variant.
13285 Also disable the check for Java for now because of parser hack that builds
13286 first an dummy BINFO and then sometimes replace it by real BINFO in some
13287 of the copies. */
13288 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t) && TYPE_BINFO (tv)
13289 && TYPE_BINFO (t) != TYPE_BINFO (tv)
13290 /* FIXME: Java sometimes keep dump TYPE_BINFOs on variant types.
13291 Since there is no cheap way to tell C++/Java type w/o LTO, do checking
13292 at LTO time only. */
13293 && (in_lto_p && odr_type_p (t)))
13295 error ("type variant has different %<TYPE_BINFO%>");
13296 debug_tree (tv);
13297 error ("type variant%'s %<TYPE_BINFO%>");
13298 debug_tree (TYPE_BINFO (tv));
13299 error ("type%'s %<TYPE_BINFO%>");
13300 debug_tree (TYPE_BINFO (t));
13301 return false;
13304 /* Check various uses of TYPE_VALUES_RAW. */
13305 if (TREE_CODE (t) == ENUMERAL_TYPE
13306 && TYPE_VALUES (t))
13307 verify_variant_match (TYPE_VALUES);
13308 else if (TREE_CODE (t) == ARRAY_TYPE)
13309 verify_variant_match (TYPE_DOMAIN);
13310 /* Permit incomplete variants of complete type. While FEs may complete
13311 all variants, this does not happen for C++ templates in all cases. */
13312 else if (RECORD_OR_UNION_TYPE_P (t)
13313 && COMPLETE_TYPE_P (t)
13314 && TYPE_FIELDS (t) != TYPE_FIELDS (tv))
13316 tree f1, f2;
13318 /* Fortran builds qualified variants as new records with items of
13319 qualified type. Verify that they looks same. */
13320 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (tv);
13321 f1 && f2;
13322 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
13323 if (TREE_CODE (f1) != FIELD_DECL || TREE_CODE (f2) != FIELD_DECL
13324 || (TYPE_MAIN_VARIANT (TREE_TYPE (f1))
13325 != TYPE_MAIN_VARIANT (TREE_TYPE (f2))
13326 /* FIXME: gfc_nonrestricted_type builds all types as variants
13327 with exception of pointer types. It deeply copies the type
13328 which means that we may end up with a variant type
13329 referring non-variant pointer. We may change it to
13330 produce types as variants, too, like
13331 objc_get_protocol_qualified_type does. */
13332 && !POINTER_TYPE_P (TREE_TYPE (f1)))
13333 || DECL_FIELD_OFFSET (f1) != DECL_FIELD_OFFSET (f2)
13334 || DECL_FIELD_BIT_OFFSET (f1) != DECL_FIELD_BIT_OFFSET (f2))
13335 break;
13336 if (f1 || f2)
13338 error ("type variant has different %<TYPE_FIELDS%>");
13339 debug_tree (tv);
13340 error ("first mismatch is field");
13341 debug_tree (f1);
13342 error ("and field");
13343 debug_tree (f2);
13344 return false;
13347 else if ((TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE))
13348 verify_variant_match (TYPE_ARG_TYPES);
13349 /* For C++ the qualified variant of array type is really an array type
13350 of qualified TREE_TYPE.
13351 objc builds variants of pointer where pointer to type is a variant, too
13352 in objc_get_protocol_qualified_type. */
13353 if (TREE_TYPE (t) != TREE_TYPE (tv)
13354 && ((TREE_CODE (t) != ARRAY_TYPE
13355 && !POINTER_TYPE_P (t))
13356 || TYPE_MAIN_VARIANT (TREE_TYPE (t))
13357 != TYPE_MAIN_VARIANT (TREE_TYPE (tv))))
13359 error ("type variant has different %<TREE_TYPE%>");
13360 debug_tree (tv);
13361 error ("type variant%'s %<TREE_TYPE%>");
13362 debug_tree (TREE_TYPE (tv));
13363 error ("type%'s %<TREE_TYPE%>");
13364 debug_tree (TREE_TYPE (t));
13365 return false;
13367 if (type_with_alias_set_p (t)
13368 && !gimple_canonical_types_compatible_p (t, tv, false))
13370 error ("type is not compatible with its variant");
13371 debug_tree (tv);
13372 error ("type variant%'s %<TREE_TYPE%>");
13373 debug_tree (TREE_TYPE (tv));
13374 error ("type%'s %<TREE_TYPE%>");
13375 debug_tree (TREE_TYPE (t));
13376 return false;
13378 return true;
13379 #undef verify_variant_match
13383 /* The TYPE_CANONICAL merging machinery. It should closely resemble
13384 the middle-end types_compatible_p function. It needs to avoid
13385 claiming types are different for types that should be treated
13386 the same with respect to TBAA. Canonical types are also used
13387 for IL consistency checks via the useless_type_conversion_p
13388 predicate which does not handle all type kinds itself but falls
13389 back to pointer-comparison of TYPE_CANONICAL for aggregates
13390 for example. */
13392 /* Return true if TYPE_UNSIGNED of TYPE should be ignored for canonical
13393 type calculation because we need to allow inter-operability between signed
13394 and unsigned variants. */
13396 bool
13397 type_with_interoperable_signedness (const_tree type)
13399 /* Fortran standard require C_SIGNED_CHAR to be interoperable with both
13400 signed char and unsigned char. Similarly fortran FE builds
13401 C_SIZE_T as signed type, while C defines it unsigned. */
13403 return tree_code_for_canonical_type_merging (TREE_CODE (type))
13404 == INTEGER_TYPE
13405 && (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node)
13406 || TYPE_PRECISION (type) == TYPE_PRECISION (size_type_node));
13409 /* Return true iff T1 and T2 are structurally identical for what
13410 TBAA is concerned.
13411 This function is used both by lto.cc canonical type merging and by the
13412 verifier. If TRUST_TYPE_CANONICAL we do not look into structure of types
13413 that have TYPE_CANONICAL defined and assume them equivalent. This is useful
13414 only for LTO because only in these cases TYPE_CANONICAL equivalence
13415 correspond to one defined by gimple_canonical_types_compatible_p. */
13417 bool
13418 gimple_canonical_types_compatible_p (const_tree t1, const_tree t2,
13419 bool trust_type_canonical)
13421 /* Type variants should be same as the main variant. When not doing sanity
13422 checking to verify this fact, go to main variants and save some work. */
13423 if (trust_type_canonical)
13425 t1 = TYPE_MAIN_VARIANT (t1);
13426 t2 = TYPE_MAIN_VARIANT (t2);
13429 /* Check first for the obvious case of pointer identity. */
13430 if (t1 == t2)
13431 return true;
13433 /* Check that we have two types to compare. */
13434 if (t1 == NULL_TREE || t2 == NULL_TREE)
13435 return false;
13437 /* We consider complete types always compatible with incomplete type.
13438 This does not make sense for canonical type calculation and thus we
13439 need to ensure that we are never called on it.
13441 FIXME: For more correctness the function probably should have three modes
13442 1) mode assuming that types are complete mathcing their structure
13443 2) mode allowing incomplete types but producing equivalence classes
13444 and thus ignoring all info from complete types
13445 3) mode allowing incomplete types to match complete but checking
13446 compatibility between complete types.
13448 1 and 2 can be used for canonical type calculation. 3 is the real
13449 definition of type compatibility that can be used i.e. for warnings during
13450 declaration merging. */
13452 gcc_assert (!trust_type_canonical
13453 || (type_with_alias_set_p (t1) && type_with_alias_set_p (t2)));
13455 /* If the types have been previously registered and found equal
13456 they still are. */
13458 if (TYPE_CANONICAL (t1) && TYPE_CANONICAL (t2)
13459 && trust_type_canonical)
13461 /* Do not use TYPE_CANONICAL of pointer types. For LTO streamed types
13462 they are always NULL, but they are set to non-NULL for types
13463 constructed by build_pointer_type and variants. In this case the
13464 TYPE_CANONICAL is more fine grained than the equivalnce we test (where
13465 all pointers are considered equal. Be sure to not return false
13466 negatives. */
13467 gcc_checking_assert (canonical_type_used_p (t1)
13468 && canonical_type_used_p (t2));
13469 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
13472 /* For types where we do ODR based TBAA the canonical type is always
13473 set correctly, so we know that types are different if their
13474 canonical types does not match. */
13475 if (trust_type_canonical
13476 && (odr_type_p (t1) && odr_based_tbaa_p (t1))
13477 != (odr_type_p (t2) && odr_based_tbaa_p (t2)))
13478 return false;
13480 /* Can't be the same type if the types don't have the same code. */
13481 enum tree_code code = tree_code_for_canonical_type_merging (TREE_CODE (t1));
13482 if (code != tree_code_for_canonical_type_merging (TREE_CODE (t2)))
13483 return false;
13485 /* Qualifiers do not matter for canonical type comparison purposes. */
13487 /* Void types and nullptr types are always the same. */
13488 if (TREE_CODE (t1) == VOID_TYPE
13489 || TREE_CODE (t1) == NULLPTR_TYPE)
13490 return true;
13492 /* Can't be the same type if they have different mode. */
13493 if (TYPE_MODE (t1) != TYPE_MODE (t2))
13494 return false;
13496 /* Non-aggregate types can be handled cheaply. */
13497 if (INTEGRAL_TYPE_P (t1)
13498 || SCALAR_FLOAT_TYPE_P (t1)
13499 || FIXED_POINT_TYPE_P (t1)
13500 || TREE_CODE (t1) == VECTOR_TYPE
13501 || TREE_CODE (t1) == COMPLEX_TYPE
13502 || TREE_CODE (t1) == OFFSET_TYPE
13503 || POINTER_TYPE_P (t1))
13505 /* Can't be the same type if they have different recision. */
13506 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
13507 return false;
13509 /* In some cases the signed and unsigned types are required to be
13510 inter-operable. */
13511 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2)
13512 && !type_with_interoperable_signedness (t1))
13513 return false;
13515 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
13516 interoperable with "signed char". Unless all frontends are revisited
13517 to agree on these types, we must ignore the flag completely. */
13519 /* Fortran standard define C_PTR type that is compatible with every
13520 C pointer. For this reason we need to glob all pointers into one.
13521 Still pointers in different address spaces are not compatible. */
13522 if (POINTER_TYPE_P (t1))
13524 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
13525 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
13526 return false;
13529 /* Tail-recurse to components. */
13530 if (TREE_CODE (t1) == VECTOR_TYPE
13531 || TREE_CODE (t1) == COMPLEX_TYPE)
13532 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
13533 TREE_TYPE (t2),
13534 trust_type_canonical);
13536 return true;
13539 /* Do type-specific comparisons. */
13540 switch (TREE_CODE (t1))
13542 case ARRAY_TYPE:
13543 /* Array types are the same if the element types are the same and
13544 the number of elements are the same. */
13545 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13546 trust_type_canonical)
13547 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
13548 || TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2)
13549 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
13550 return false;
13551 else
13553 tree i1 = TYPE_DOMAIN (t1);
13554 tree i2 = TYPE_DOMAIN (t2);
13556 /* For an incomplete external array, the type domain can be
13557 NULL_TREE. Check this condition also. */
13558 if (i1 == NULL_TREE && i2 == NULL_TREE)
13559 return true;
13560 else if (i1 == NULL_TREE || i2 == NULL_TREE)
13561 return false;
13562 else
13564 tree min1 = TYPE_MIN_VALUE (i1);
13565 tree min2 = TYPE_MIN_VALUE (i2);
13566 tree max1 = TYPE_MAX_VALUE (i1);
13567 tree max2 = TYPE_MAX_VALUE (i2);
13569 /* The minimum/maximum values have to be the same. */
13570 if ((min1 == min2
13571 || (min1 && min2
13572 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
13573 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
13574 || operand_equal_p (min1, min2, 0))))
13575 && (max1 == max2
13576 || (max1 && max2
13577 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
13578 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
13579 || operand_equal_p (max1, max2, 0)))))
13580 return true;
13581 else
13582 return false;
13586 case METHOD_TYPE:
13587 case FUNCTION_TYPE:
13588 /* Function types are the same if the return type and arguments types
13589 are the same. */
13590 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13591 trust_type_canonical))
13592 return false;
13594 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
13595 return true;
13596 else
13598 tree parms1, parms2;
13600 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
13601 parms1 && parms2;
13602 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
13604 if (!gimple_canonical_types_compatible_p
13605 (TREE_VALUE (parms1), TREE_VALUE (parms2),
13606 trust_type_canonical))
13607 return false;
13610 if (parms1 || parms2)
13611 return false;
13613 return true;
13616 case RECORD_TYPE:
13617 case UNION_TYPE:
13618 case QUAL_UNION_TYPE:
13620 tree f1, f2;
13622 /* Don't try to compare variants of an incomplete type, before
13623 TYPE_FIELDS has been copied around. */
13624 if (!COMPLETE_TYPE_P (t1) && !COMPLETE_TYPE_P (t2))
13625 return true;
13628 if (TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2))
13629 return false;
13631 /* For aggregate types, all the fields must be the same. */
13632 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
13633 f1 || f2;
13634 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
13636 /* Skip non-fields and zero-sized fields. */
13637 while (f1 && (TREE_CODE (f1) != FIELD_DECL
13638 || (DECL_SIZE (f1)
13639 && integer_zerop (DECL_SIZE (f1)))))
13640 f1 = TREE_CHAIN (f1);
13641 while (f2 && (TREE_CODE (f2) != FIELD_DECL
13642 || (DECL_SIZE (f2)
13643 && integer_zerop (DECL_SIZE (f2)))))
13644 f2 = TREE_CHAIN (f2);
13645 if (!f1 || !f2)
13646 break;
13647 /* The fields must have the same name, offset and type. */
13648 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
13649 || !gimple_compare_field_offset (f1, f2)
13650 || !gimple_canonical_types_compatible_p
13651 (TREE_TYPE (f1), TREE_TYPE (f2),
13652 trust_type_canonical))
13653 return false;
13656 /* If one aggregate has more fields than the other, they
13657 are not the same. */
13658 if (f1 || f2)
13659 return false;
13661 return true;
13664 default:
13665 /* Consider all types with language specific trees in them mutually
13666 compatible. This is executed only from verify_type and false
13667 positives can be tolerated. */
13668 gcc_assert (!in_lto_p);
13669 return true;
13673 /* Verify type T. */
13675 void
13676 verify_type (const_tree t)
13678 bool error_found = false;
13679 tree mv = TYPE_MAIN_VARIANT (t);
13680 if (!mv)
13682 error ("main variant is not defined");
13683 error_found = true;
13685 else if (mv != TYPE_MAIN_VARIANT (mv))
13687 error ("%<TYPE_MAIN_VARIANT%> has different %<TYPE_MAIN_VARIANT%>");
13688 debug_tree (mv);
13689 error_found = true;
13691 else if (t != mv && !verify_type_variant (t, mv))
13692 error_found = true;
13694 tree ct = TYPE_CANONICAL (t);
13695 if (!ct)
13697 else if (TYPE_CANONICAL (ct) != ct)
13699 error ("%<TYPE_CANONICAL%> has different %<TYPE_CANONICAL%>");
13700 debug_tree (ct);
13701 error_found = true;
13703 /* Method and function types cannot be used to address memory and thus
13704 TYPE_CANONICAL really matters only for determining useless conversions.
13706 FIXME: C++ FE produce declarations of builtin functions that are not
13707 compatible with main variants. */
13708 else if (TREE_CODE (t) == FUNCTION_TYPE)
13710 else if (t != ct
13711 /* FIXME: gimple_canonical_types_compatible_p cannot compare types
13712 with variably sized arrays because their sizes possibly
13713 gimplified to different variables. */
13714 && !variably_modified_type_p (ct, NULL)
13715 && !gimple_canonical_types_compatible_p (t, ct, false)
13716 && COMPLETE_TYPE_P (t))
13718 error ("%<TYPE_CANONICAL%> is not compatible");
13719 debug_tree (ct);
13720 error_found = true;
13723 if (COMPLETE_TYPE_P (t) && TYPE_CANONICAL (t)
13724 && TYPE_MODE (t) != TYPE_MODE (TYPE_CANONICAL (t)))
13726 error ("%<TYPE_MODE%> of %<TYPE_CANONICAL%> is not compatible");
13727 debug_tree (ct);
13728 error_found = true;
13730 if (TYPE_MAIN_VARIANT (t) == t && ct && TYPE_MAIN_VARIANT (ct) != ct)
13732 error ("%<TYPE_CANONICAL%> of main variant is not main variant");
13733 debug_tree (ct);
13734 debug_tree (TYPE_MAIN_VARIANT (ct));
13735 error_found = true;
13739 /* Check various uses of TYPE_MIN_VALUE_RAW. */
13740 if (RECORD_OR_UNION_TYPE_P (t))
13742 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
13743 and danagle the pointer from time to time. */
13744 if (TYPE_VFIELD (t)
13745 && TREE_CODE (TYPE_VFIELD (t)) != FIELD_DECL
13746 && TREE_CODE (TYPE_VFIELD (t)) != TREE_LIST)
13748 error ("%<TYPE_VFIELD%> is not %<FIELD_DECL%> nor %<TREE_LIST%>");
13749 debug_tree (TYPE_VFIELD (t));
13750 error_found = true;
13753 else if (TREE_CODE (t) == POINTER_TYPE)
13755 if (TYPE_NEXT_PTR_TO (t)
13756 && TREE_CODE (TYPE_NEXT_PTR_TO (t)) != POINTER_TYPE)
13758 error ("%<TYPE_NEXT_PTR_TO%> is not %<POINTER_TYPE%>");
13759 debug_tree (TYPE_NEXT_PTR_TO (t));
13760 error_found = true;
13763 else if (TREE_CODE (t) == REFERENCE_TYPE)
13765 if (TYPE_NEXT_REF_TO (t)
13766 && TREE_CODE (TYPE_NEXT_REF_TO (t)) != REFERENCE_TYPE)
13768 error ("%<TYPE_NEXT_REF_TO%> is not %<REFERENCE_TYPE%>");
13769 debug_tree (TYPE_NEXT_REF_TO (t));
13770 error_found = true;
13773 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
13774 || TREE_CODE (t) == FIXED_POINT_TYPE)
13776 /* FIXME: The following check should pass:
13777 useless_type_conversion_p (const_cast <tree> (t),
13778 TREE_TYPE (TYPE_MIN_VALUE (t))
13779 but does not for C sizetypes in LTO. */
13782 /* Check various uses of TYPE_MAXVAL_RAW. */
13783 if (RECORD_OR_UNION_TYPE_P (t))
13785 if (!TYPE_BINFO (t))
13787 else if (TREE_CODE (TYPE_BINFO (t)) != TREE_BINFO)
13789 error ("%<TYPE_BINFO%> is not %<TREE_BINFO%>");
13790 debug_tree (TYPE_BINFO (t));
13791 error_found = true;
13793 else if (TREE_TYPE (TYPE_BINFO (t)) != TYPE_MAIN_VARIANT (t))
13795 error ("%<TYPE_BINFO%> type is not %<TYPE_MAIN_VARIANT%>");
13796 debug_tree (TREE_TYPE (TYPE_BINFO (t)));
13797 error_found = true;
13800 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
13802 if (TYPE_METHOD_BASETYPE (t)
13803 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != RECORD_TYPE
13804 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != UNION_TYPE)
13806 error ("%<TYPE_METHOD_BASETYPE%> is not record nor union");
13807 debug_tree (TYPE_METHOD_BASETYPE (t));
13808 error_found = true;
13811 else if (TREE_CODE (t) == OFFSET_TYPE)
13813 if (TYPE_OFFSET_BASETYPE (t)
13814 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != RECORD_TYPE
13815 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != UNION_TYPE)
13817 error ("%<TYPE_OFFSET_BASETYPE%> is not record nor union");
13818 debug_tree (TYPE_OFFSET_BASETYPE (t));
13819 error_found = true;
13822 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
13823 || TREE_CODE (t) == FIXED_POINT_TYPE)
13825 /* FIXME: The following check should pass:
13826 useless_type_conversion_p (const_cast <tree> (t),
13827 TREE_TYPE (TYPE_MAX_VALUE (t))
13828 but does not for C sizetypes in LTO. */
13830 else if (TREE_CODE (t) == ARRAY_TYPE)
13832 if (TYPE_ARRAY_MAX_SIZE (t)
13833 && TREE_CODE (TYPE_ARRAY_MAX_SIZE (t)) != INTEGER_CST)
13835 error ("%<TYPE_ARRAY_MAX_SIZE%> not %<INTEGER_CST%>");
13836 debug_tree (TYPE_ARRAY_MAX_SIZE (t));
13837 error_found = true;
13840 else if (TYPE_MAX_VALUE_RAW (t))
13842 error ("%<TYPE_MAX_VALUE_RAW%> non-NULL");
13843 debug_tree (TYPE_MAX_VALUE_RAW (t));
13844 error_found = true;
13847 if (TYPE_LANG_SLOT_1 (t) && in_lto_p)
13849 error ("%<TYPE_LANG_SLOT_1 (binfo)%> field is non-NULL");
13850 debug_tree (TYPE_LANG_SLOT_1 (t));
13851 error_found = true;
13854 /* Check various uses of TYPE_VALUES_RAW. */
13855 if (TREE_CODE (t) == ENUMERAL_TYPE)
13856 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
13858 tree value = TREE_VALUE (l);
13859 tree name = TREE_PURPOSE (l);
13861 /* C FE porduce INTEGER_CST of INTEGER_TYPE, while C++ FE uses
13862 CONST_DECL of ENUMERAL TYPE. */
13863 if (TREE_CODE (value) != INTEGER_CST && TREE_CODE (value) != CONST_DECL)
13865 error ("enum value is not %<CONST_DECL%> or %<INTEGER_CST%>");
13866 debug_tree (value);
13867 debug_tree (name);
13868 error_found = true;
13870 if (TREE_CODE (TREE_TYPE (value)) != INTEGER_TYPE
13871 && TREE_CODE (TREE_TYPE (value)) != BOOLEAN_TYPE
13872 && !useless_type_conversion_p (const_cast <tree> (t), TREE_TYPE (value)))
13874 error ("enum value type is not %<INTEGER_TYPE%> nor convertible "
13875 "to the enum");
13876 debug_tree (value);
13877 debug_tree (name);
13878 error_found = true;
13880 if (TREE_CODE (name) != IDENTIFIER_NODE)
13882 error ("enum value name is not %<IDENTIFIER_NODE%>");
13883 debug_tree (value);
13884 debug_tree (name);
13885 error_found = true;
13888 else if (TREE_CODE (t) == ARRAY_TYPE)
13890 if (TYPE_DOMAIN (t) && TREE_CODE (TYPE_DOMAIN (t)) != INTEGER_TYPE)
13892 error ("array %<TYPE_DOMAIN%> is not integer type");
13893 debug_tree (TYPE_DOMAIN (t));
13894 error_found = true;
13897 else if (RECORD_OR_UNION_TYPE_P (t))
13899 if (TYPE_FIELDS (t) && !COMPLETE_TYPE_P (t) && in_lto_p)
13901 error ("%<TYPE_FIELDS%> defined in incomplete type");
13902 error_found = true;
13904 for (tree fld = TYPE_FIELDS (t); fld; fld = TREE_CHAIN (fld))
13906 /* TODO: verify properties of decls. */
13907 if (TREE_CODE (fld) == FIELD_DECL)
13909 else if (TREE_CODE (fld) == TYPE_DECL)
13911 else if (TREE_CODE (fld) == CONST_DECL)
13913 else if (VAR_P (fld))
13915 else if (TREE_CODE (fld) == TEMPLATE_DECL)
13917 else if (TREE_CODE (fld) == USING_DECL)
13919 else if (TREE_CODE (fld) == FUNCTION_DECL)
13921 else
13923 error ("wrong tree in %<TYPE_FIELDS%> list");
13924 debug_tree (fld);
13925 error_found = true;
13929 else if (TREE_CODE (t) == INTEGER_TYPE
13930 || TREE_CODE (t) == BOOLEAN_TYPE
13931 || TREE_CODE (t) == OFFSET_TYPE
13932 || TREE_CODE (t) == REFERENCE_TYPE
13933 || TREE_CODE (t) == NULLPTR_TYPE
13934 || TREE_CODE (t) == POINTER_TYPE)
13936 if (TYPE_CACHED_VALUES_P (t) != (TYPE_CACHED_VALUES (t) != NULL))
13938 error ("%<TYPE_CACHED_VALUES_P%> is %i while %<TYPE_CACHED_VALUES%> "
13939 "is %p",
13940 TYPE_CACHED_VALUES_P (t), (void *)TYPE_CACHED_VALUES (t));
13941 error_found = true;
13943 else if (TYPE_CACHED_VALUES_P (t) && TREE_CODE (TYPE_CACHED_VALUES (t)) != TREE_VEC)
13945 error ("%<TYPE_CACHED_VALUES%> is not %<TREE_VEC%>");
13946 debug_tree (TYPE_CACHED_VALUES (t));
13947 error_found = true;
13949 /* Verify just enough of cache to ensure that no one copied it to new type.
13950 All copying should go by copy_node that should clear it. */
13951 else if (TYPE_CACHED_VALUES_P (t))
13953 int i;
13954 for (i = 0; i < TREE_VEC_LENGTH (TYPE_CACHED_VALUES (t)); i++)
13955 if (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)
13956 && TREE_TYPE (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)) != t)
13958 error ("wrong %<TYPE_CACHED_VALUES%> entry");
13959 debug_tree (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i));
13960 error_found = true;
13961 break;
13965 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
13966 for (tree l = TYPE_ARG_TYPES (t); l; l = TREE_CHAIN (l))
13968 /* C++ FE uses TREE_PURPOSE to store initial values. */
13969 if (TREE_PURPOSE (l) && in_lto_p)
13971 error ("%<TREE_PURPOSE%> is non-NULL in %<TYPE_ARG_TYPES%> list");
13972 debug_tree (l);
13973 error_found = true;
13975 if (!TYPE_P (TREE_VALUE (l)))
13977 error ("wrong entry in %<TYPE_ARG_TYPES%> list");
13978 debug_tree (l);
13979 error_found = true;
13982 else if (!is_lang_specific (t) && TYPE_VALUES_RAW (t))
13984 error ("%<TYPE_VALUES_RAW%> field is non-NULL");
13985 debug_tree (TYPE_VALUES_RAW (t));
13986 error_found = true;
13988 if (TREE_CODE (t) != INTEGER_TYPE
13989 && TREE_CODE (t) != BOOLEAN_TYPE
13990 && TREE_CODE (t) != OFFSET_TYPE
13991 && TREE_CODE (t) != REFERENCE_TYPE
13992 && TREE_CODE (t) != NULLPTR_TYPE
13993 && TREE_CODE (t) != POINTER_TYPE
13994 && TYPE_CACHED_VALUES_P (t))
13996 error ("%<TYPE_CACHED_VALUES_P%> is set while it should not be");
13997 error_found = true;
14000 /* ipa-devirt makes an assumption that TYPE_METHOD_BASETYPE is always
14001 TYPE_MAIN_VARIANT and it would be odd to add methods only to variatns
14002 of a type. */
14003 if (TREE_CODE (t) == METHOD_TYPE
14004 && TYPE_MAIN_VARIANT (TYPE_METHOD_BASETYPE (t)) != TYPE_METHOD_BASETYPE (t))
14006 error ("%<TYPE_METHOD_BASETYPE%> is not main variant");
14007 error_found = true;
14010 if (error_found)
14012 debug_tree (const_cast <tree> (t));
14013 internal_error ("%qs failed", __func__);
14018 /* Return 1 if ARG interpreted as signed in its precision is known to be
14019 always positive or 2 if ARG is known to be always negative, or 3 if
14020 ARG may be positive or negative. */
14023 get_range_pos_neg (tree arg)
14025 if (arg == error_mark_node)
14026 return 3;
14028 int prec = TYPE_PRECISION (TREE_TYPE (arg));
14029 int cnt = 0;
14030 if (TREE_CODE (arg) == INTEGER_CST)
14032 wide_int w = wi::sext (wi::to_wide (arg), prec);
14033 if (wi::neg_p (w))
14034 return 2;
14035 else
14036 return 1;
14038 while (CONVERT_EXPR_P (arg)
14039 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
14040 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg, 0))) <= prec)
14042 arg = TREE_OPERAND (arg, 0);
14043 /* Narrower value zero extended into wider type
14044 will always result in positive values. */
14045 if (TYPE_UNSIGNED (TREE_TYPE (arg))
14046 && TYPE_PRECISION (TREE_TYPE (arg)) < prec)
14047 return 1;
14048 prec = TYPE_PRECISION (TREE_TYPE (arg));
14049 if (++cnt > 30)
14050 return 3;
14053 if (TREE_CODE (arg) != SSA_NAME)
14054 return 3;
14055 value_range r;
14056 while (!get_global_range_query ()->range_of_expr (r, arg) || r.kind () != VR_RANGE)
14058 gimple *g = SSA_NAME_DEF_STMT (arg);
14059 if (is_gimple_assign (g)
14060 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (g)))
14062 tree t = gimple_assign_rhs1 (g);
14063 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
14064 && TYPE_PRECISION (TREE_TYPE (t)) <= prec)
14066 if (TYPE_UNSIGNED (TREE_TYPE (t))
14067 && TYPE_PRECISION (TREE_TYPE (t)) < prec)
14068 return 1;
14069 prec = TYPE_PRECISION (TREE_TYPE (t));
14070 arg = t;
14071 if (++cnt > 30)
14072 return 3;
14073 continue;
14076 return 3;
14078 if (TYPE_UNSIGNED (TREE_TYPE (arg)))
14080 /* For unsigned values, the "positive" range comes
14081 below the "negative" range. */
14082 if (!wi::neg_p (wi::sext (r.upper_bound (), prec), SIGNED))
14083 return 1;
14084 if (wi::neg_p (wi::sext (r.lower_bound (), prec), SIGNED))
14085 return 2;
14087 else
14089 if (!wi::neg_p (wi::sext (r.lower_bound (), prec), SIGNED))
14090 return 1;
14091 if (wi::neg_p (wi::sext (r.upper_bound (), prec), SIGNED))
14092 return 2;
14094 return 3;
14100 /* Return true if ARG is marked with the nonnull attribute in the
14101 current function signature. */
14103 bool
14104 nonnull_arg_p (const_tree arg)
14106 tree t, attrs, fntype;
14107 unsigned HOST_WIDE_INT arg_num;
14109 gcc_assert (TREE_CODE (arg) == PARM_DECL
14110 && (POINTER_TYPE_P (TREE_TYPE (arg))
14111 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE));
14113 /* The static chain decl is always non null. */
14114 if (arg == cfun->static_chain_decl)
14115 return true;
14117 /* THIS argument of method is always non-NULL. */
14118 if (TREE_CODE (TREE_TYPE (cfun->decl)) == METHOD_TYPE
14119 && arg == DECL_ARGUMENTS (cfun->decl)
14120 && flag_delete_null_pointer_checks)
14121 return true;
14123 /* Values passed by reference are always non-NULL. */
14124 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
14125 && flag_delete_null_pointer_checks)
14126 return true;
14128 fntype = TREE_TYPE (cfun->decl);
14129 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
14131 attrs = lookup_attribute ("nonnull", attrs);
14133 /* If "nonnull" wasn't specified, we know nothing about the argument. */
14134 if (attrs == NULL_TREE)
14135 return false;
14137 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
14138 if (TREE_VALUE (attrs) == NULL_TREE)
14139 return true;
14141 /* Get the position number for ARG in the function signature. */
14142 for (arg_num = 1, t = DECL_ARGUMENTS (cfun->decl);
14144 t = DECL_CHAIN (t), arg_num++)
14146 if (t == arg)
14147 break;
14150 gcc_assert (t == arg);
14152 /* Now see if ARG_NUM is mentioned in the nonnull list. */
14153 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
14155 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
14156 return true;
14160 return false;
14163 /* Combine LOC and BLOCK to a combined adhoc loc, retaining any range
14164 information. */
14166 location_t
14167 set_block (location_t loc, tree block)
14169 location_t pure_loc = get_pure_location (loc);
14170 source_range src_range = get_range_from_loc (line_table, loc);
14171 return COMBINE_LOCATION_DATA (line_table, pure_loc, src_range, block);
14174 location_t
14175 set_source_range (tree expr, location_t start, location_t finish)
14177 source_range src_range;
14178 src_range.m_start = start;
14179 src_range.m_finish = finish;
14180 return set_source_range (expr, src_range);
14183 location_t
14184 set_source_range (tree expr, source_range src_range)
14186 if (!EXPR_P (expr))
14187 return UNKNOWN_LOCATION;
14189 location_t pure_loc = get_pure_location (EXPR_LOCATION (expr));
14190 location_t adhoc = COMBINE_LOCATION_DATA (line_table,
14191 pure_loc,
14192 src_range,
14193 NULL);
14194 SET_EXPR_LOCATION (expr, adhoc);
14195 return adhoc;
14198 /* Return EXPR, potentially wrapped with a node expression LOC,
14199 if !CAN_HAVE_LOCATION_P (expr).
14201 NON_LVALUE_EXPR is used for wrapping constants, apart from STRING_CST.
14202 VIEW_CONVERT_EXPR is used for wrapping non-constants and STRING_CST.
14204 Wrapper nodes can be identified using location_wrapper_p. */
14206 tree
14207 maybe_wrap_with_location (tree expr, location_t loc)
14209 if (expr == NULL)
14210 return NULL;
14211 if (loc == UNKNOWN_LOCATION)
14212 return expr;
14213 if (CAN_HAVE_LOCATION_P (expr))
14214 return expr;
14215 /* We should only be adding wrappers for constants and for decls,
14216 or for some exceptional tree nodes (e.g. BASELINK in the C++ FE). */
14217 gcc_assert (CONSTANT_CLASS_P (expr)
14218 || DECL_P (expr)
14219 || EXCEPTIONAL_CLASS_P (expr));
14221 /* For now, don't add wrappers to exceptional tree nodes, to minimize
14222 any impact of the wrapper nodes. */
14223 if (EXCEPTIONAL_CLASS_P (expr))
14224 return expr;
14226 /* Compiler-generated temporary variables don't need a wrapper. */
14227 if (DECL_P (expr) && DECL_ARTIFICIAL (expr) && DECL_IGNORED_P (expr))
14228 return expr;
14230 /* If any auto_suppress_location_wrappers are active, don't create
14231 wrappers. */
14232 if (suppress_location_wrappers > 0)
14233 return expr;
14235 tree_code code
14236 = (((CONSTANT_CLASS_P (expr) && TREE_CODE (expr) != STRING_CST)
14237 || (TREE_CODE (expr) == CONST_DECL && !TREE_STATIC (expr)))
14238 ? NON_LVALUE_EXPR : VIEW_CONVERT_EXPR);
14239 tree wrapper = build1_loc (loc, code, TREE_TYPE (expr), expr);
14240 /* Mark this node as being a wrapper. */
14241 EXPR_LOCATION_WRAPPER_P (wrapper) = 1;
14242 return wrapper;
14245 int suppress_location_wrappers;
14247 /* Return the name of combined function FN, for debugging purposes. */
14249 const char *
14250 combined_fn_name (combined_fn fn)
14252 if (builtin_fn_p (fn))
14254 tree fndecl = builtin_decl_explicit (as_builtin_fn (fn));
14255 return IDENTIFIER_POINTER (DECL_NAME (fndecl));
14257 else
14258 return internal_fn_name (as_internal_fn (fn));
14261 /* Return a bitmap with a bit set corresponding to each argument in
14262 a function call type FNTYPE declared with attribute nonnull,
14263 or null if none of the function's argument are nonnull. The caller
14264 must free the bitmap. */
14266 bitmap
14267 get_nonnull_args (const_tree fntype)
14269 if (fntype == NULL_TREE)
14270 return NULL;
14272 bitmap argmap = NULL;
14273 if (TREE_CODE (fntype) == METHOD_TYPE)
14275 /* The this pointer in C++ non-static member functions is
14276 implicitly nonnull whether or not it's declared as such. */
14277 argmap = BITMAP_ALLOC (NULL);
14278 bitmap_set_bit (argmap, 0);
14281 tree attrs = TYPE_ATTRIBUTES (fntype);
14282 if (!attrs)
14283 return argmap;
14285 /* A function declaration can specify multiple attribute nonnull,
14286 each with zero or more arguments. The loop below creates a bitmap
14287 representing a union of all the arguments. An empty (but non-null)
14288 bitmap means that all arguments have been declaraed nonnull. */
14289 for ( ; attrs; attrs = TREE_CHAIN (attrs))
14291 attrs = lookup_attribute ("nonnull", attrs);
14292 if (!attrs)
14293 break;
14295 if (!argmap)
14296 argmap = BITMAP_ALLOC (NULL);
14298 if (!TREE_VALUE (attrs))
14300 /* Clear the bitmap in case a previous attribute nonnull
14301 set it and this one overrides it for all arguments. */
14302 bitmap_clear (argmap);
14303 return argmap;
14306 /* Iterate over the indices of the format arguments declared nonnull
14307 and set a bit for each. */
14308 for (tree idx = TREE_VALUE (attrs); idx; idx = TREE_CHAIN (idx))
14310 unsigned int val = TREE_INT_CST_LOW (TREE_VALUE (idx)) - 1;
14311 bitmap_set_bit (argmap, val);
14315 return argmap;
14318 /* Returns true if TYPE is a type where it and all of its subobjects
14319 (recursively) are of structure, union, or array type. */
14321 bool
14322 is_empty_type (const_tree type)
14324 if (RECORD_OR_UNION_TYPE_P (type))
14326 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
14327 if (TREE_CODE (field) == FIELD_DECL
14328 && !DECL_PADDING_P (field)
14329 && !is_empty_type (TREE_TYPE (field)))
14330 return false;
14331 return true;
14333 else if (TREE_CODE (type) == ARRAY_TYPE)
14334 return (integer_minus_onep (array_type_nelts (type))
14335 || TYPE_DOMAIN (type) == NULL_TREE
14336 || is_empty_type (TREE_TYPE (type)));
14337 return false;
14340 /* Implement TARGET_EMPTY_RECORD_P. Return true if TYPE is an empty type
14341 that shouldn't be passed via stack. */
14343 bool
14344 default_is_empty_record (const_tree type)
14346 if (!abi_version_at_least (12))
14347 return false;
14349 if (type == error_mark_node)
14350 return false;
14352 if (TREE_ADDRESSABLE (type))
14353 return false;
14355 return is_empty_type (TYPE_MAIN_VARIANT (type));
14358 /* Determine whether TYPE is a structure with a flexible array member,
14359 or a union containing such a structure (possibly recursively). */
14361 bool
14362 flexible_array_type_p (const_tree type)
14364 tree x, last;
14365 switch (TREE_CODE (type))
14367 case RECORD_TYPE:
14368 last = NULL_TREE;
14369 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
14370 if (TREE_CODE (x) == FIELD_DECL)
14371 last = x;
14372 if (last == NULL_TREE)
14373 return false;
14374 if (TREE_CODE (TREE_TYPE (last)) == ARRAY_TYPE
14375 && TYPE_SIZE (TREE_TYPE (last)) == NULL_TREE
14376 && TYPE_DOMAIN (TREE_TYPE (last)) != NULL_TREE
14377 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (last))) == NULL_TREE)
14378 return true;
14379 return false;
14380 case UNION_TYPE:
14381 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
14383 if (TREE_CODE (x) == FIELD_DECL
14384 && flexible_array_type_p (TREE_TYPE (x)))
14385 return true;
14387 return false;
14388 default:
14389 return false;
14393 /* Like int_size_in_bytes, but handle empty records specially. */
14395 HOST_WIDE_INT
14396 arg_int_size_in_bytes (const_tree type)
14398 return TYPE_EMPTY_P (type) ? 0 : int_size_in_bytes (type);
14401 /* Like size_in_bytes, but handle empty records specially. */
14403 tree
14404 arg_size_in_bytes (const_tree type)
14406 return TYPE_EMPTY_P (type) ? size_zero_node : size_in_bytes (type);
14409 /* Return true if an expression with CODE has to have the same result type as
14410 its first operand. */
14412 bool
14413 expr_type_first_operand_type_p (tree_code code)
14415 switch (code)
14417 case NEGATE_EXPR:
14418 case ABS_EXPR:
14419 case BIT_NOT_EXPR:
14420 case PAREN_EXPR:
14421 case CONJ_EXPR:
14423 case PLUS_EXPR:
14424 case MINUS_EXPR:
14425 case MULT_EXPR:
14426 case TRUNC_DIV_EXPR:
14427 case CEIL_DIV_EXPR:
14428 case FLOOR_DIV_EXPR:
14429 case ROUND_DIV_EXPR:
14430 case TRUNC_MOD_EXPR:
14431 case CEIL_MOD_EXPR:
14432 case FLOOR_MOD_EXPR:
14433 case ROUND_MOD_EXPR:
14434 case RDIV_EXPR:
14435 case EXACT_DIV_EXPR:
14436 case MIN_EXPR:
14437 case MAX_EXPR:
14438 case BIT_IOR_EXPR:
14439 case BIT_XOR_EXPR:
14440 case BIT_AND_EXPR:
14442 case LSHIFT_EXPR:
14443 case RSHIFT_EXPR:
14444 case LROTATE_EXPR:
14445 case RROTATE_EXPR:
14446 return true;
14448 default:
14449 return false;
14453 /* Return a typenode for the "standard" C type with a given name. */
14454 tree
14455 get_typenode_from_name (const char *name)
14457 if (name == NULL || *name == '\0')
14458 return NULL_TREE;
14460 if (strcmp (name, "char") == 0)
14461 return char_type_node;
14462 if (strcmp (name, "unsigned char") == 0)
14463 return unsigned_char_type_node;
14464 if (strcmp (name, "signed char") == 0)
14465 return signed_char_type_node;
14467 if (strcmp (name, "short int") == 0)
14468 return short_integer_type_node;
14469 if (strcmp (name, "short unsigned int") == 0)
14470 return short_unsigned_type_node;
14472 if (strcmp (name, "int") == 0)
14473 return integer_type_node;
14474 if (strcmp (name, "unsigned int") == 0)
14475 return unsigned_type_node;
14477 if (strcmp (name, "long int") == 0)
14478 return long_integer_type_node;
14479 if (strcmp (name, "long unsigned int") == 0)
14480 return long_unsigned_type_node;
14482 if (strcmp (name, "long long int") == 0)
14483 return long_long_integer_type_node;
14484 if (strcmp (name, "long long unsigned int") == 0)
14485 return long_long_unsigned_type_node;
14487 gcc_unreachable ();
14490 /* List of pointer types used to declare builtins before we have seen their
14491 real declaration.
14493 Keep the size up to date in tree.h ! */
14494 const builtin_structptr_type builtin_structptr_types[6] =
14496 { fileptr_type_node, ptr_type_node, "FILE" },
14497 { const_tm_ptr_type_node, const_ptr_type_node, "tm" },
14498 { fenv_t_ptr_type_node, ptr_type_node, "fenv_t" },
14499 { const_fenv_t_ptr_type_node, const_ptr_type_node, "fenv_t" },
14500 { fexcept_t_ptr_type_node, ptr_type_node, "fexcept_t" },
14501 { const_fexcept_t_ptr_type_node, const_ptr_type_node, "fexcept_t" }
14504 /* Return the maximum object size. */
14506 tree
14507 max_object_size (void)
14509 /* To do: Make this a configurable parameter. */
14510 return TYPE_MAX_VALUE (ptrdiff_type_node);
14513 /* A wrapper around TARGET_VERIFY_TYPE_CONTEXT that makes the silent_p
14514 parameter default to false and that weeds out error_mark_node. */
14516 bool
14517 verify_type_context (location_t loc, type_context_kind context,
14518 const_tree type, bool silent_p)
14520 if (type == error_mark_node)
14521 return true;
14523 gcc_assert (TYPE_P (type));
14524 return (!targetm.verify_type_context
14525 || targetm.verify_type_context (loc, context, type, silent_p));
14528 /* Return true if NEW_ASM and DELETE_ASM name a valid pair of new and
14529 delete operators. Return false if they may or may not name such
14530 a pair and, when nonnull, set *PCERTAIN to true if they certainly
14531 do not. */
14533 bool
14534 valid_new_delete_pair_p (tree new_asm, tree delete_asm,
14535 bool *pcertain /* = NULL */)
14537 bool certain;
14538 if (!pcertain)
14539 pcertain = &certain;
14541 const char *new_name = IDENTIFIER_POINTER (new_asm);
14542 const char *delete_name = IDENTIFIER_POINTER (delete_asm);
14543 unsigned int new_len = IDENTIFIER_LENGTH (new_asm);
14544 unsigned int delete_len = IDENTIFIER_LENGTH (delete_asm);
14546 /* The following failures are due to invalid names so they're not
14547 considered certain mismatches. */
14548 *pcertain = false;
14550 if (new_len < 5 || delete_len < 6)
14551 return false;
14552 if (new_name[0] == '_')
14553 ++new_name, --new_len;
14554 if (new_name[0] == '_')
14555 ++new_name, --new_len;
14556 if (delete_name[0] == '_')
14557 ++delete_name, --delete_len;
14558 if (delete_name[0] == '_')
14559 ++delete_name, --delete_len;
14560 if (new_len < 4 || delete_len < 5)
14561 return false;
14563 /* The following failures are due to names of user-defined operators
14564 so they're also not considered certain mismatches. */
14566 /* *_len is now just the length after initial underscores. */
14567 if (new_name[0] != 'Z' || new_name[1] != 'n')
14568 return false;
14569 if (delete_name[0] != 'Z' || delete_name[1] != 'd')
14570 return false;
14572 /* The following failures are certain mismatches. */
14573 *pcertain = true;
14575 /* _Znw must match _Zdl, _Zna must match _Zda. */
14576 if ((new_name[2] != 'w' || delete_name[2] != 'l')
14577 && (new_name[2] != 'a' || delete_name[2] != 'a'))
14578 return false;
14579 /* 'j', 'm' and 'y' correspond to size_t. */
14580 if (new_name[3] != 'j' && new_name[3] != 'm' && new_name[3] != 'y')
14581 return false;
14582 if (delete_name[3] != 'P' || delete_name[4] != 'v')
14583 return false;
14584 if (new_len == 4
14585 || (new_len == 18 && !memcmp (new_name + 4, "RKSt9nothrow_t", 14)))
14587 /* _ZnXY or _ZnXYRKSt9nothrow_t matches
14588 _ZdXPv, _ZdXPvY and _ZdXPvRKSt9nothrow_t. */
14589 if (delete_len == 5)
14590 return true;
14591 if (delete_len == 6 && delete_name[5] == new_name[3])
14592 return true;
14593 if (delete_len == 19 && !memcmp (delete_name + 5, "RKSt9nothrow_t", 14))
14594 return true;
14596 else if ((new_len == 19 && !memcmp (new_name + 4, "St11align_val_t", 15))
14597 || (new_len == 33
14598 && !memcmp (new_name + 4, "St11align_val_tRKSt9nothrow_t", 29)))
14600 /* _ZnXYSt11align_val_t or _ZnXYSt11align_val_tRKSt9nothrow_t matches
14601 _ZdXPvSt11align_val_t or _ZdXPvYSt11align_val_t or or
14602 _ZdXPvSt11align_val_tRKSt9nothrow_t. */
14603 if (delete_len == 20 && !memcmp (delete_name + 5, "St11align_val_t", 15))
14604 return true;
14605 if (delete_len == 21
14606 && delete_name[5] == new_name[3]
14607 && !memcmp (delete_name + 6, "St11align_val_t", 15))
14608 return true;
14609 if (delete_len == 34
14610 && !memcmp (delete_name + 5, "St11align_val_tRKSt9nothrow_t", 29))
14611 return true;
14614 /* The negative result is conservative. */
14615 *pcertain = false;
14616 return false;
14619 /* Return the zero-based number corresponding to the argument being
14620 deallocated if FNDECL is a deallocation function or an out-of-bounds
14621 value if it isn't. */
14623 unsigned
14624 fndecl_dealloc_argno (tree fndecl)
14626 /* A call to operator delete isn't recognized as one to a built-in. */
14627 if (DECL_IS_OPERATOR_DELETE_P (fndecl))
14629 if (DECL_IS_REPLACEABLE_OPERATOR (fndecl))
14630 return 0;
14632 /* Avoid placement delete that's not been inlined. */
14633 tree fname = DECL_ASSEMBLER_NAME (fndecl);
14634 if (id_equal (fname, "_ZdlPvS_") // ordinary form
14635 || id_equal (fname, "_ZdaPvS_")) // array form
14636 return UINT_MAX;
14637 return 0;
14640 /* TODO: Handle user-defined functions with attribute malloc? Handle
14641 known non-built-ins like fopen? */
14642 if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
14644 switch (DECL_FUNCTION_CODE (fndecl))
14646 case BUILT_IN_FREE:
14647 case BUILT_IN_REALLOC:
14648 return 0;
14649 default:
14650 break;
14652 return UINT_MAX;
14655 tree attrs = DECL_ATTRIBUTES (fndecl);
14656 if (!attrs)
14657 return UINT_MAX;
14659 for (tree atfree = attrs;
14660 (atfree = lookup_attribute ("*dealloc", atfree));
14661 atfree = TREE_CHAIN (atfree))
14663 tree alloc = TREE_VALUE (atfree);
14664 if (!alloc)
14665 continue;
14667 tree pos = TREE_CHAIN (alloc);
14668 if (!pos)
14669 return 0;
14671 pos = TREE_VALUE (pos);
14672 return TREE_INT_CST_LOW (pos) - 1;
14675 return UINT_MAX;
14678 /* If EXPR refers to a character array or pointer declared attribute
14679 nonstring, return a decl for that array or pointer and set *REF
14680 to the referenced enclosing object or pointer. Otherwise return
14681 null. */
14683 tree
14684 get_attr_nonstring_decl (tree expr, tree *ref)
14686 tree decl = expr;
14687 tree var = NULL_TREE;
14688 if (TREE_CODE (decl) == SSA_NAME)
14690 gimple *def = SSA_NAME_DEF_STMT (decl);
14692 if (is_gimple_assign (def))
14694 tree_code code = gimple_assign_rhs_code (def);
14695 if (code == ADDR_EXPR
14696 || code == COMPONENT_REF
14697 || code == VAR_DECL)
14698 decl = gimple_assign_rhs1 (def);
14700 else
14701 var = SSA_NAME_VAR (decl);
14704 if (TREE_CODE (decl) == ADDR_EXPR)
14705 decl = TREE_OPERAND (decl, 0);
14707 /* To simplify calling code, store the referenced DECL regardless of
14708 the attribute determined below, but avoid storing the SSA_NAME_VAR
14709 obtained above (it's not useful for dataflow purposes). */
14710 if (ref)
14711 *ref = decl;
14713 /* Use the SSA_NAME_VAR that was determined above to see if it's
14714 declared nonstring. Otherwise drill down into the referenced
14715 DECL. */
14716 if (var)
14717 decl = var;
14718 else if (TREE_CODE (decl) == ARRAY_REF)
14719 decl = TREE_OPERAND (decl, 0);
14720 else if (TREE_CODE (decl) == COMPONENT_REF)
14721 decl = TREE_OPERAND (decl, 1);
14722 else if (TREE_CODE (decl) == MEM_REF)
14723 return get_attr_nonstring_decl (TREE_OPERAND (decl, 0), ref);
14725 if (DECL_P (decl)
14726 && lookup_attribute ("nonstring", DECL_ATTRIBUTES (decl)))
14727 return decl;
14729 return NULL_TREE;
14732 /* Return length of attribute names string,
14733 if arglist chain > 1, -1 otherwise. */
14736 get_target_clone_attr_len (tree arglist)
14738 tree arg;
14739 int str_len_sum = 0;
14740 int argnum = 0;
14742 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
14744 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
14745 size_t len = strlen (str);
14746 str_len_sum += len + 1;
14747 for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
14748 argnum++;
14749 argnum++;
14751 if (argnum <= 1)
14752 return -1;
14753 return str_len_sum;
14756 void
14757 tree_cc_finalize (void)
14759 clear_nonstandard_integer_type_cache ();
14762 #if CHECKING_P
14764 namespace selftest {
14766 /* Selftests for tree. */
14768 /* Verify that integer constants are sane. */
14770 static void
14771 test_integer_constants ()
14773 ASSERT_TRUE (integer_type_node != NULL);
14774 ASSERT_TRUE (build_int_cst (integer_type_node, 0) != NULL);
14776 tree type = integer_type_node;
14778 tree zero = build_zero_cst (type);
14779 ASSERT_EQ (INTEGER_CST, TREE_CODE (zero));
14780 ASSERT_EQ (type, TREE_TYPE (zero));
14782 tree one = build_int_cst (type, 1);
14783 ASSERT_EQ (INTEGER_CST, TREE_CODE (one));
14784 ASSERT_EQ (type, TREE_TYPE (zero));
14787 /* Verify identifiers. */
14789 static void
14790 test_identifiers ()
14792 tree identifier = get_identifier ("foo");
14793 ASSERT_EQ (3, IDENTIFIER_LENGTH (identifier));
14794 ASSERT_STREQ ("foo", IDENTIFIER_POINTER (identifier));
14797 /* Verify LABEL_DECL. */
14799 static void
14800 test_labels ()
14802 tree identifier = get_identifier ("err");
14803 tree label_decl = build_decl (UNKNOWN_LOCATION, LABEL_DECL,
14804 identifier, void_type_node);
14805 ASSERT_EQ (-1, LABEL_DECL_UID (label_decl));
14806 ASSERT_FALSE (FORCED_LABEL (label_decl));
14809 /* Return a new VECTOR_CST node whose type is TYPE and whose values
14810 are given by VALS. */
14812 static tree
14813 build_vector (tree type, const vec<tree> &vals MEM_STAT_DECL)
14815 gcc_assert (known_eq (vals.length (), TYPE_VECTOR_SUBPARTS (type)));
14816 tree_vector_builder builder (type, vals.length (), 1);
14817 builder.splice (vals);
14818 return builder.build ();
14821 /* Check that VECTOR_CST ACTUAL contains the elements in EXPECTED. */
14823 static void
14824 check_vector_cst (const vec<tree> &expected, tree actual)
14826 ASSERT_KNOWN_EQ (expected.length (),
14827 TYPE_VECTOR_SUBPARTS (TREE_TYPE (actual)));
14828 for (unsigned int i = 0; i < expected.length (); ++i)
14829 ASSERT_EQ (wi::to_wide (expected[i]),
14830 wi::to_wide (vector_cst_elt (actual, i)));
14833 /* Check that VECTOR_CST ACTUAL contains NPATTERNS duplicated elements,
14834 and that its elements match EXPECTED. */
14836 static void
14837 check_vector_cst_duplicate (const vec<tree> &expected, tree actual,
14838 unsigned int npatterns)
14840 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
14841 ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (actual));
14842 ASSERT_EQ (npatterns, vector_cst_encoded_nelts (actual));
14843 ASSERT_TRUE (VECTOR_CST_DUPLICATE_P (actual));
14844 ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
14845 check_vector_cst (expected, actual);
14848 /* Check that VECTOR_CST ACTUAL contains NPATTERNS foreground elements
14849 and NPATTERNS background elements, and that its elements match
14850 EXPECTED. */
14852 static void
14853 check_vector_cst_fill (const vec<tree> &expected, tree actual,
14854 unsigned int npatterns)
14856 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
14857 ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (actual));
14858 ASSERT_EQ (2 * npatterns, vector_cst_encoded_nelts (actual));
14859 ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
14860 ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
14861 check_vector_cst (expected, actual);
14864 /* Check that VECTOR_CST ACTUAL contains NPATTERNS stepped patterns,
14865 and that its elements match EXPECTED. */
14867 static void
14868 check_vector_cst_stepped (const vec<tree> &expected, tree actual,
14869 unsigned int npatterns)
14871 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
14872 ASSERT_EQ (3, VECTOR_CST_NELTS_PER_PATTERN (actual));
14873 ASSERT_EQ (3 * npatterns, vector_cst_encoded_nelts (actual));
14874 ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
14875 ASSERT_TRUE (VECTOR_CST_STEPPED_P (actual));
14876 check_vector_cst (expected, actual);
14879 /* Test the creation of VECTOR_CSTs. */
14881 static void
14882 test_vector_cst_patterns (ALONE_CXX_MEM_STAT_INFO)
14884 auto_vec<tree, 8> elements (8);
14885 elements.quick_grow (8);
14886 tree element_type = build_nonstandard_integer_type (16, true);
14887 tree vector_type = build_vector_type (element_type, 8);
14889 /* Test a simple linear series with a base of 0 and a step of 1:
14890 { 0, 1, 2, 3, 4, 5, 6, 7 }. */
14891 for (unsigned int i = 0; i < 8; ++i)
14892 elements[i] = build_int_cst (element_type, i);
14893 tree vector = build_vector (vector_type, elements PASS_MEM_STAT);
14894 check_vector_cst_stepped (elements, vector, 1);
14896 /* Try the same with the first element replaced by 100:
14897 { 100, 1, 2, 3, 4, 5, 6, 7 }. */
14898 elements[0] = build_int_cst (element_type, 100);
14899 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14900 check_vector_cst_stepped (elements, vector, 1);
14902 /* Try a series that wraps around.
14903 { 100, 65531, 65532, 65533, 65534, 65535, 0, 1 }. */
14904 for (unsigned int i = 1; i < 8; ++i)
14905 elements[i] = build_int_cst (element_type, (65530 + i) & 0xffff);
14906 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14907 check_vector_cst_stepped (elements, vector, 1);
14909 /* Try a downward series:
14910 { 100, 79, 78, 77, 76, 75, 75, 73 }. */
14911 for (unsigned int i = 1; i < 8; ++i)
14912 elements[i] = build_int_cst (element_type, 80 - i);
14913 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14914 check_vector_cst_stepped (elements, vector, 1);
14916 /* Try two interleaved series with different bases and steps:
14917 { 100, 53, 66, 206, 62, 212, 58, 218 }. */
14918 elements[1] = build_int_cst (element_type, 53);
14919 for (unsigned int i = 2; i < 8; i += 2)
14921 elements[i] = build_int_cst (element_type, 70 - i * 2);
14922 elements[i + 1] = build_int_cst (element_type, 200 + i * 3);
14924 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14925 check_vector_cst_stepped (elements, vector, 2);
14927 /* Try a duplicated value:
14928 { 100, 100, 100, 100, 100, 100, 100, 100 }. */
14929 for (unsigned int i = 1; i < 8; ++i)
14930 elements[i] = elements[0];
14931 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14932 check_vector_cst_duplicate (elements, vector, 1);
14934 /* Try an interleaved duplicated value:
14935 { 100, 55, 100, 55, 100, 55, 100, 55 }. */
14936 elements[1] = build_int_cst (element_type, 55);
14937 for (unsigned int i = 2; i < 8; ++i)
14938 elements[i] = elements[i - 2];
14939 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14940 check_vector_cst_duplicate (elements, vector, 2);
14942 /* Try a duplicated value with 2 exceptions
14943 { 41, 97, 100, 55, 100, 55, 100, 55 }. */
14944 elements[0] = build_int_cst (element_type, 41);
14945 elements[1] = build_int_cst (element_type, 97);
14946 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14947 check_vector_cst_fill (elements, vector, 2);
14949 /* Try with and without a step
14950 { 41, 97, 100, 21, 100, 35, 100, 49 }. */
14951 for (unsigned int i = 3; i < 8; i += 2)
14952 elements[i] = build_int_cst (element_type, i * 7);
14953 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14954 check_vector_cst_stepped (elements, vector, 2);
14956 /* Try a fully-general constant:
14957 { 41, 97, 100, 21, 100, 9990, 100, 49 }. */
14958 elements[5] = build_int_cst (element_type, 9990);
14959 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14960 check_vector_cst_fill (elements, vector, 4);
14963 /* Verify that STRIP_NOPS (NODE) is EXPECTED.
14964 Helper function for test_location_wrappers, to deal with STRIP_NOPS
14965 modifying its argument in-place. */
14967 static void
14968 check_strip_nops (tree node, tree expected)
14970 STRIP_NOPS (node);
14971 ASSERT_EQ (expected, node);
14974 /* Verify location wrappers. */
14976 static void
14977 test_location_wrappers ()
14979 location_t loc = BUILTINS_LOCATION;
14981 ASSERT_EQ (NULL_TREE, maybe_wrap_with_location (NULL_TREE, loc));
14983 /* Wrapping a constant. */
14984 tree int_cst = build_int_cst (integer_type_node, 42);
14985 ASSERT_FALSE (CAN_HAVE_LOCATION_P (int_cst));
14986 ASSERT_FALSE (location_wrapper_p (int_cst));
14988 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
14989 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
14990 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_int_cst));
14991 ASSERT_EQ (int_cst, tree_strip_any_location_wrapper (wrapped_int_cst));
14993 /* We shouldn't add wrapper nodes for UNKNOWN_LOCATION. */
14994 ASSERT_EQ (int_cst, maybe_wrap_with_location (int_cst, UNKNOWN_LOCATION));
14996 /* We shouldn't add wrapper nodes for nodes that CAN_HAVE_LOCATION_P. */
14997 tree cast = build1 (NOP_EXPR, char_type_node, int_cst);
14998 ASSERT_TRUE (CAN_HAVE_LOCATION_P (cast));
14999 ASSERT_EQ (cast, maybe_wrap_with_location (cast, loc));
15001 /* Wrapping a STRING_CST. */
15002 tree string_cst = build_string (4, "foo");
15003 ASSERT_FALSE (CAN_HAVE_LOCATION_P (string_cst));
15004 ASSERT_FALSE (location_wrapper_p (string_cst));
15006 tree wrapped_string_cst = maybe_wrap_with_location (string_cst, loc);
15007 ASSERT_TRUE (location_wrapper_p (wrapped_string_cst));
15008 ASSERT_EQ (VIEW_CONVERT_EXPR, TREE_CODE (wrapped_string_cst));
15009 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_string_cst));
15010 ASSERT_EQ (string_cst, tree_strip_any_location_wrapper (wrapped_string_cst));
15013 /* Wrapping a variable. */
15014 tree int_var = build_decl (UNKNOWN_LOCATION, VAR_DECL,
15015 get_identifier ("some_int_var"),
15016 integer_type_node);
15017 ASSERT_FALSE (CAN_HAVE_LOCATION_P (int_var));
15018 ASSERT_FALSE (location_wrapper_p (int_var));
15020 tree wrapped_int_var = maybe_wrap_with_location (int_var, loc);
15021 ASSERT_TRUE (location_wrapper_p (wrapped_int_var));
15022 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_int_var));
15023 ASSERT_EQ (int_var, tree_strip_any_location_wrapper (wrapped_int_var));
15025 /* Verify that "reinterpret_cast<int>(some_int_var)" is not a location
15026 wrapper. */
15027 tree r_cast = build1 (NON_LVALUE_EXPR, integer_type_node, int_var);
15028 ASSERT_FALSE (location_wrapper_p (r_cast));
15029 ASSERT_EQ (r_cast, tree_strip_any_location_wrapper (r_cast));
15031 /* Verify that STRIP_NOPS removes wrappers. */
15032 check_strip_nops (wrapped_int_cst, int_cst);
15033 check_strip_nops (wrapped_string_cst, string_cst);
15034 check_strip_nops (wrapped_int_var, int_var);
15037 /* Test various tree predicates. Verify that location wrappers don't
15038 affect the results. */
15040 static void
15041 test_predicates ()
15043 /* Build various constants and wrappers around them. */
15045 location_t loc = BUILTINS_LOCATION;
15047 tree i_0 = build_int_cst (integer_type_node, 0);
15048 tree wr_i_0 = maybe_wrap_with_location (i_0, loc);
15050 tree i_1 = build_int_cst (integer_type_node, 1);
15051 tree wr_i_1 = maybe_wrap_with_location (i_1, loc);
15053 tree i_m1 = build_int_cst (integer_type_node, -1);
15054 tree wr_i_m1 = maybe_wrap_with_location (i_m1, loc);
15056 tree f_0 = build_real_from_int_cst (float_type_node, i_0);
15057 tree wr_f_0 = maybe_wrap_with_location (f_0, loc);
15058 tree f_1 = build_real_from_int_cst (float_type_node, i_1);
15059 tree wr_f_1 = maybe_wrap_with_location (f_1, loc);
15060 tree f_m1 = build_real_from_int_cst (float_type_node, i_m1);
15061 tree wr_f_m1 = maybe_wrap_with_location (f_m1, loc);
15063 tree c_i_0 = build_complex (NULL_TREE, i_0, i_0);
15064 tree c_i_1 = build_complex (NULL_TREE, i_1, i_0);
15065 tree c_i_m1 = build_complex (NULL_TREE, i_m1, i_0);
15067 tree c_f_0 = build_complex (NULL_TREE, f_0, f_0);
15068 tree c_f_1 = build_complex (NULL_TREE, f_1, f_0);
15069 tree c_f_m1 = build_complex (NULL_TREE, f_m1, f_0);
15071 /* TODO: vector constants. */
15073 /* Test integer_onep. */
15074 ASSERT_FALSE (integer_onep (i_0));
15075 ASSERT_FALSE (integer_onep (wr_i_0));
15076 ASSERT_TRUE (integer_onep (i_1));
15077 ASSERT_TRUE (integer_onep (wr_i_1));
15078 ASSERT_FALSE (integer_onep (i_m1));
15079 ASSERT_FALSE (integer_onep (wr_i_m1));
15080 ASSERT_FALSE (integer_onep (f_0));
15081 ASSERT_FALSE (integer_onep (wr_f_0));
15082 ASSERT_FALSE (integer_onep (f_1));
15083 ASSERT_FALSE (integer_onep (wr_f_1));
15084 ASSERT_FALSE (integer_onep (f_m1));
15085 ASSERT_FALSE (integer_onep (wr_f_m1));
15086 ASSERT_FALSE (integer_onep (c_i_0));
15087 ASSERT_TRUE (integer_onep (c_i_1));
15088 ASSERT_FALSE (integer_onep (c_i_m1));
15089 ASSERT_FALSE (integer_onep (c_f_0));
15090 ASSERT_FALSE (integer_onep (c_f_1));
15091 ASSERT_FALSE (integer_onep (c_f_m1));
15093 /* Test integer_zerop. */
15094 ASSERT_TRUE (integer_zerop (i_0));
15095 ASSERT_TRUE (integer_zerop (wr_i_0));
15096 ASSERT_FALSE (integer_zerop (i_1));
15097 ASSERT_FALSE (integer_zerop (wr_i_1));
15098 ASSERT_FALSE (integer_zerop (i_m1));
15099 ASSERT_FALSE (integer_zerop (wr_i_m1));
15100 ASSERT_FALSE (integer_zerop (f_0));
15101 ASSERT_FALSE (integer_zerop (wr_f_0));
15102 ASSERT_FALSE (integer_zerop (f_1));
15103 ASSERT_FALSE (integer_zerop (wr_f_1));
15104 ASSERT_FALSE (integer_zerop (f_m1));
15105 ASSERT_FALSE (integer_zerop (wr_f_m1));
15106 ASSERT_TRUE (integer_zerop (c_i_0));
15107 ASSERT_FALSE (integer_zerop (c_i_1));
15108 ASSERT_FALSE (integer_zerop (c_i_m1));
15109 ASSERT_FALSE (integer_zerop (c_f_0));
15110 ASSERT_FALSE (integer_zerop (c_f_1));
15111 ASSERT_FALSE (integer_zerop (c_f_m1));
15113 /* Test integer_all_onesp. */
15114 ASSERT_FALSE (integer_all_onesp (i_0));
15115 ASSERT_FALSE (integer_all_onesp (wr_i_0));
15116 ASSERT_FALSE (integer_all_onesp (i_1));
15117 ASSERT_FALSE (integer_all_onesp (wr_i_1));
15118 ASSERT_TRUE (integer_all_onesp (i_m1));
15119 ASSERT_TRUE (integer_all_onesp (wr_i_m1));
15120 ASSERT_FALSE (integer_all_onesp (f_0));
15121 ASSERT_FALSE (integer_all_onesp (wr_f_0));
15122 ASSERT_FALSE (integer_all_onesp (f_1));
15123 ASSERT_FALSE (integer_all_onesp (wr_f_1));
15124 ASSERT_FALSE (integer_all_onesp (f_m1));
15125 ASSERT_FALSE (integer_all_onesp (wr_f_m1));
15126 ASSERT_FALSE (integer_all_onesp (c_i_0));
15127 ASSERT_FALSE (integer_all_onesp (c_i_1));
15128 ASSERT_FALSE (integer_all_onesp (c_i_m1));
15129 ASSERT_FALSE (integer_all_onesp (c_f_0));
15130 ASSERT_FALSE (integer_all_onesp (c_f_1));
15131 ASSERT_FALSE (integer_all_onesp (c_f_m1));
15133 /* Test integer_minus_onep. */
15134 ASSERT_FALSE (integer_minus_onep (i_0));
15135 ASSERT_FALSE (integer_minus_onep (wr_i_0));
15136 ASSERT_FALSE (integer_minus_onep (i_1));
15137 ASSERT_FALSE (integer_minus_onep (wr_i_1));
15138 ASSERT_TRUE (integer_minus_onep (i_m1));
15139 ASSERT_TRUE (integer_minus_onep (wr_i_m1));
15140 ASSERT_FALSE (integer_minus_onep (f_0));
15141 ASSERT_FALSE (integer_minus_onep (wr_f_0));
15142 ASSERT_FALSE (integer_minus_onep (f_1));
15143 ASSERT_FALSE (integer_minus_onep (wr_f_1));
15144 ASSERT_FALSE (integer_minus_onep (f_m1));
15145 ASSERT_FALSE (integer_minus_onep (wr_f_m1));
15146 ASSERT_FALSE (integer_minus_onep (c_i_0));
15147 ASSERT_FALSE (integer_minus_onep (c_i_1));
15148 ASSERT_TRUE (integer_minus_onep (c_i_m1));
15149 ASSERT_FALSE (integer_minus_onep (c_f_0));
15150 ASSERT_FALSE (integer_minus_onep (c_f_1));
15151 ASSERT_FALSE (integer_minus_onep (c_f_m1));
15153 /* Test integer_each_onep. */
15154 ASSERT_FALSE (integer_each_onep (i_0));
15155 ASSERT_FALSE (integer_each_onep (wr_i_0));
15156 ASSERT_TRUE (integer_each_onep (i_1));
15157 ASSERT_TRUE (integer_each_onep (wr_i_1));
15158 ASSERT_FALSE (integer_each_onep (i_m1));
15159 ASSERT_FALSE (integer_each_onep (wr_i_m1));
15160 ASSERT_FALSE (integer_each_onep (f_0));
15161 ASSERT_FALSE (integer_each_onep (wr_f_0));
15162 ASSERT_FALSE (integer_each_onep (f_1));
15163 ASSERT_FALSE (integer_each_onep (wr_f_1));
15164 ASSERT_FALSE (integer_each_onep (f_m1));
15165 ASSERT_FALSE (integer_each_onep (wr_f_m1));
15166 ASSERT_FALSE (integer_each_onep (c_i_0));
15167 ASSERT_FALSE (integer_each_onep (c_i_1));
15168 ASSERT_FALSE (integer_each_onep (c_i_m1));
15169 ASSERT_FALSE (integer_each_onep (c_f_0));
15170 ASSERT_FALSE (integer_each_onep (c_f_1));
15171 ASSERT_FALSE (integer_each_onep (c_f_m1));
15173 /* Test integer_truep. */
15174 ASSERT_FALSE (integer_truep (i_0));
15175 ASSERT_FALSE (integer_truep (wr_i_0));
15176 ASSERT_TRUE (integer_truep (i_1));
15177 ASSERT_TRUE (integer_truep (wr_i_1));
15178 ASSERT_FALSE (integer_truep (i_m1));
15179 ASSERT_FALSE (integer_truep (wr_i_m1));
15180 ASSERT_FALSE (integer_truep (f_0));
15181 ASSERT_FALSE (integer_truep (wr_f_0));
15182 ASSERT_FALSE (integer_truep (f_1));
15183 ASSERT_FALSE (integer_truep (wr_f_1));
15184 ASSERT_FALSE (integer_truep (f_m1));
15185 ASSERT_FALSE (integer_truep (wr_f_m1));
15186 ASSERT_FALSE (integer_truep (c_i_0));
15187 ASSERT_TRUE (integer_truep (c_i_1));
15188 ASSERT_FALSE (integer_truep (c_i_m1));
15189 ASSERT_FALSE (integer_truep (c_f_0));
15190 ASSERT_FALSE (integer_truep (c_f_1));
15191 ASSERT_FALSE (integer_truep (c_f_m1));
15193 /* Test integer_nonzerop. */
15194 ASSERT_FALSE (integer_nonzerop (i_0));
15195 ASSERT_FALSE (integer_nonzerop (wr_i_0));
15196 ASSERT_TRUE (integer_nonzerop (i_1));
15197 ASSERT_TRUE (integer_nonzerop (wr_i_1));
15198 ASSERT_TRUE (integer_nonzerop (i_m1));
15199 ASSERT_TRUE (integer_nonzerop (wr_i_m1));
15200 ASSERT_FALSE (integer_nonzerop (f_0));
15201 ASSERT_FALSE (integer_nonzerop (wr_f_0));
15202 ASSERT_FALSE (integer_nonzerop (f_1));
15203 ASSERT_FALSE (integer_nonzerop (wr_f_1));
15204 ASSERT_FALSE (integer_nonzerop (f_m1));
15205 ASSERT_FALSE (integer_nonzerop (wr_f_m1));
15206 ASSERT_FALSE (integer_nonzerop (c_i_0));
15207 ASSERT_TRUE (integer_nonzerop (c_i_1));
15208 ASSERT_TRUE (integer_nonzerop (c_i_m1));
15209 ASSERT_FALSE (integer_nonzerop (c_f_0));
15210 ASSERT_FALSE (integer_nonzerop (c_f_1));
15211 ASSERT_FALSE (integer_nonzerop (c_f_m1));
15213 /* Test real_zerop. */
15214 ASSERT_FALSE (real_zerop (i_0));
15215 ASSERT_FALSE (real_zerop (wr_i_0));
15216 ASSERT_FALSE (real_zerop (i_1));
15217 ASSERT_FALSE (real_zerop (wr_i_1));
15218 ASSERT_FALSE (real_zerop (i_m1));
15219 ASSERT_FALSE (real_zerop (wr_i_m1));
15220 ASSERT_TRUE (real_zerop (f_0));
15221 ASSERT_TRUE (real_zerop (wr_f_0));
15222 ASSERT_FALSE (real_zerop (f_1));
15223 ASSERT_FALSE (real_zerop (wr_f_1));
15224 ASSERT_FALSE (real_zerop (f_m1));
15225 ASSERT_FALSE (real_zerop (wr_f_m1));
15226 ASSERT_FALSE (real_zerop (c_i_0));
15227 ASSERT_FALSE (real_zerop (c_i_1));
15228 ASSERT_FALSE (real_zerop (c_i_m1));
15229 ASSERT_TRUE (real_zerop (c_f_0));
15230 ASSERT_FALSE (real_zerop (c_f_1));
15231 ASSERT_FALSE (real_zerop (c_f_m1));
15233 /* Test real_onep. */
15234 ASSERT_FALSE (real_onep (i_0));
15235 ASSERT_FALSE (real_onep (wr_i_0));
15236 ASSERT_FALSE (real_onep (i_1));
15237 ASSERT_FALSE (real_onep (wr_i_1));
15238 ASSERT_FALSE (real_onep (i_m1));
15239 ASSERT_FALSE (real_onep (wr_i_m1));
15240 ASSERT_FALSE (real_onep (f_0));
15241 ASSERT_FALSE (real_onep (wr_f_0));
15242 ASSERT_TRUE (real_onep (f_1));
15243 ASSERT_TRUE (real_onep (wr_f_1));
15244 ASSERT_FALSE (real_onep (f_m1));
15245 ASSERT_FALSE (real_onep (wr_f_m1));
15246 ASSERT_FALSE (real_onep (c_i_0));
15247 ASSERT_FALSE (real_onep (c_i_1));
15248 ASSERT_FALSE (real_onep (c_i_m1));
15249 ASSERT_FALSE (real_onep (c_f_0));
15250 ASSERT_TRUE (real_onep (c_f_1));
15251 ASSERT_FALSE (real_onep (c_f_m1));
15253 /* Test real_minus_onep. */
15254 ASSERT_FALSE (real_minus_onep (i_0));
15255 ASSERT_FALSE (real_minus_onep (wr_i_0));
15256 ASSERT_FALSE (real_minus_onep (i_1));
15257 ASSERT_FALSE (real_minus_onep (wr_i_1));
15258 ASSERT_FALSE (real_minus_onep (i_m1));
15259 ASSERT_FALSE (real_minus_onep (wr_i_m1));
15260 ASSERT_FALSE (real_minus_onep (f_0));
15261 ASSERT_FALSE (real_minus_onep (wr_f_0));
15262 ASSERT_FALSE (real_minus_onep (f_1));
15263 ASSERT_FALSE (real_minus_onep (wr_f_1));
15264 ASSERT_TRUE (real_minus_onep (f_m1));
15265 ASSERT_TRUE (real_minus_onep (wr_f_m1));
15266 ASSERT_FALSE (real_minus_onep (c_i_0));
15267 ASSERT_FALSE (real_minus_onep (c_i_1));
15268 ASSERT_FALSE (real_minus_onep (c_i_m1));
15269 ASSERT_FALSE (real_minus_onep (c_f_0));
15270 ASSERT_FALSE (real_minus_onep (c_f_1));
15271 ASSERT_TRUE (real_minus_onep (c_f_m1));
15273 /* Test zerop. */
15274 ASSERT_TRUE (zerop (i_0));
15275 ASSERT_TRUE (zerop (wr_i_0));
15276 ASSERT_FALSE (zerop (i_1));
15277 ASSERT_FALSE (zerop (wr_i_1));
15278 ASSERT_FALSE (zerop (i_m1));
15279 ASSERT_FALSE (zerop (wr_i_m1));
15280 ASSERT_TRUE (zerop (f_0));
15281 ASSERT_TRUE (zerop (wr_f_0));
15282 ASSERT_FALSE (zerop (f_1));
15283 ASSERT_FALSE (zerop (wr_f_1));
15284 ASSERT_FALSE (zerop (f_m1));
15285 ASSERT_FALSE (zerop (wr_f_m1));
15286 ASSERT_TRUE (zerop (c_i_0));
15287 ASSERT_FALSE (zerop (c_i_1));
15288 ASSERT_FALSE (zerop (c_i_m1));
15289 ASSERT_TRUE (zerop (c_f_0));
15290 ASSERT_FALSE (zerop (c_f_1));
15291 ASSERT_FALSE (zerop (c_f_m1));
15293 /* Test tree_expr_nonnegative_p. */
15294 ASSERT_TRUE (tree_expr_nonnegative_p (i_0));
15295 ASSERT_TRUE (tree_expr_nonnegative_p (wr_i_0));
15296 ASSERT_TRUE (tree_expr_nonnegative_p (i_1));
15297 ASSERT_TRUE (tree_expr_nonnegative_p (wr_i_1));
15298 ASSERT_FALSE (tree_expr_nonnegative_p (i_m1));
15299 ASSERT_FALSE (tree_expr_nonnegative_p (wr_i_m1));
15300 ASSERT_TRUE (tree_expr_nonnegative_p (f_0));
15301 ASSERT_TRUE (tree_expr_nonnegative_p (wr_f_0));
15302 ASSERT_TRUE (tree_expr_nonnegative_p (f_1));
15303 ASSERT_TRUE (tree_expr_nonnegative_p (wr_f_1));
15304 ASSERT_FALSE (tree_expr_nonnegative_p (f_m1));
15305 ASSERT_FALSE (tree_expr_nonnegative_p (wr_f_m1));
15306 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_0));
15307 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_1));
15308 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_m1));
15309 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_0));
15310 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_1));
15311 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_m1));
15313 /* Test tree_expr_nonzero_p. */
15314 ASSERT_FALSE (tree_expr_nonzero_p (i_0));
15315 ASSERT_FALSE (tree_expr_nonzero_p (wr_i_0));
15316 ASSERT_TRUE (tree_expr_nonzero_p (i_1));
15317 ASSERT_TRUE (tree_expr_nonzero_p (wr_i_1));
15318 ASSERT_TRUE (tree_expr_nonzero_p (i_m1));
15319 ASSERT_TRUE (tree_expr_nonzero_p (wr_i_m1));
15321 /* Test integer_valued_real_p. */
15322 ASSERT_FALSE (integer_valued_real_p (i_0));
15323 ASSERT_TRUE (integer_valued_real_p (f_0));
15324 ASSERT_TRUE (integer_valued_real_p (wr_f_0));
15325 ASSERT_TRUE (integer_valued_real_p (f_1));
15326 ASSERT_TRUE (integer_valued_real_p (wr_f_1));
15328 /* Test integer_pow2p. */
15329 ASSERT_FALSE (integer_pow2p (i_0));
15330 ASSERT_TRUE (integer_pow2p (i_1));
15331 ASSERT_TRUE (integer_pow2p (wr_i_1));
15333 /* Test uniform_integer_cst_p. */
15334 ASSERT_TRUE (uniform_integer_cst_p (i_0));
15335 ASSERT_TRUE (uniform_integer_cst_p (wr_i_0));
15336 ASSERT_TRUE (uniform_integer_cst_p (i_1));
15337 ASSERT_TRUE (uniform_integer_cst_p (wr_i_1));
15338 ASSERT_TRUE (uniform_integer_cst_p (i_m1));
15339 ASSERT_TRUE (uniform_integer_cst_p (wr_i_m1));
15340 ASSERT_FALSE (uniform_integer_cst_p (f_0));
15341 ASSERT_FALSE (uniform_integer_cst_p (wr_f_0));
15342 ASSERT_FALSE (uniform_integer_cst_p (f_1));
15343 ASSERT_FALSE (uniform_integer_cst_p (wr_f_1));
15344 ASSERT_FALSE (uniform_integer_cst_p (f_m1));
15345 ASSERT_FALSE (uniform_integer_cst_p (wr_f_m1));
15346 ASSERT_FALSE (uniform_integer_cst_p (c_i_0));
15347 ASSERT_FALSE (uniform_integer_cst_p (c_i_1));
15348 ASSERT_FALSE (uniform_integer_cst_p (c_i_m1));
15349 ASSERT_FALSE (uniform_integer_cst_p (c_f_0));
15350 ASSERT_FALSE (uniform_integer_cst_p (c_f_1));
15351 ASSERT_FALSE (uniform_integer_cst_p (c_f_m1));
15354 /* Check that string escaping works correctly. */
15356 static void
15357 test_escaped_strings (void)
15359 int saved_cutoff;
15360 escaped_string msg;
15362 msg.escape (NULL);
15363 /* ASSERT_STREQ does not accept NULL as a valid test
15364 result, so we have to use ASSERT_EQ instead. */
15365 ASSERT_EQ (NULL, (const char *) msg);
15367 msg.escape ("");
15368 ASSERT_STREQ ("", (const char *) msg);
15370 msg.escape ("foobar");
15371 ASSERT_STREQ ("foobar", (const char *) msg);
15373 /* Ensure that we have -fmessage-length set to 0. */
15374 saved_cutoff = pp_line_cutoff (global_dc->printer);
15375 pp_line_cutoff (global_dc->printer) = 0;
15377 msg.escape ("foo\nbar");
15378 ASSERT_STREQ ("foo\\nbar", (const char *) msg);
15380 msg.escape ("\a\b\f\n\r\t\v");
15381 ASSERT_STREQ ("\\a\\b\\f\\n\\r\\t\\v", (const char *) msg);
15383 /* Now repeat the tests with -fmessage-length set to 5. */
15384 pp_line_cutoff (global_dc->printer) = 5;
15386 /* Note that the newline is not translated into an escape. */
15387 msg.escape ("foo\nbar");
15388 ASSERT_STREQ ("foo\nbar", (const char *) msg);
15390 msg.escape ("\a\b\f\n\r\t\v");
15391 ASSERT_STREQ ("\\a\\b\\f\n\\r\\t\\v", (const char *) msg);
15393 /* Restore the original message length setting. */
15394 pp_line_cutoff (global_dc->printer) = saved_cutoff;
15397 /* Run all of the selftests within this file. */
15399 void
15400 tree_cc_tests ()
15402 test_integer_constants ();
15403 test_identifiers ();
15404 test_labels ();
15405 test_vector_cst_patterns ();
15406 test_location_wrappers ();
15407 test_predicates ();
15408 test_escaped_strings ();
15411 } // namespace selftest
15413 #endif /* CHECKING_P */
15415 #include "gt-tree.h"