1/* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2024 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
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.
26
27 It is intended to be language-independent but can occasionally
28 calls language-dependent routines. */
29
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"
76
77/* Names of tree components.
78 Used for printing out the tree and error messages. */
79#define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
80#define END_OF_BASE_TREE_CODES "@dummy",
81
82static const char *const tree_code_name[] = {
83#include "all-tree.def"
84};
85
86#undef DEFTREECODE
87#undef END_OF_BASE_TREE_CODES
88
89/* Each tree code class has an associated string representation.
90 These must correspond to the tree_code_class entries. */
91
92const char *const tree_code_class_strings[] =
93{
94 "exceptional",
95 "constant",
96 "type",
97 "declaration",
98 "reference",
99 "comparison",
100 "unary",
101 "binary",
102 "statement",
103 "vl_exp",
104 "expression"
105};
106
107/* obstack.[ch] explicitly declined to prototype this. */
108extern int _obstack_allocated_p (struct obstack *h, void *obj);
109
110/* Statistics-gathering stuff. */
111
112static uint64_t tree_code_counts[MAX_TREE_CODES];
113uint64_t tree_node_counts[(int) all_kinds];
114uint64_t tree_node_sizes[(int) all_kinds];
115
116/* Keep in sync with tree.h:enum tree_node_kind. */
117static const char * const tree_node_kind_names[] = {
118 "decls",
119 "types",
120 "blocks",
121 "stmts",
122 "refs",
123 "exprs",
124 "constants",
125 "identifiers",
126 "vecs",
127 "binfos",
128 "ssa names",
129 "constructors",
130 "random kinds",
131 "lang_decl kinds",
132 "lang_type kinds",
133 "omp clauses",
134};
135
136/* Unique id for next decl created. */
137static GTY(()) int next_decl_uid;
138/* Unique id for next type created. */
139static GTY(()) unsigned next_type_uid = 1;
140/* Unique id for next debug decl created. Use negative numbers,
141 to catch erroneous uses. */
142static GTY(()) int next_debug_decl_uid;
143
144/* Since we cannot rehash a type after it is in the table, we have to
145 keep the hash code. */
146
147struct GTY((for_user)) type_hash {
148 unsigned long hash;
149 tree type;
150};
151
152/* Initial size of the hash table (rounded to next prime). */
153#define TYPE_HASH_INITIAL_SIZE 1000
154
155struct type_cache_hasher : ggc_cache_ptr_hash<type_hash>
156{
157 static hashval_t hash (type_hash *t) { return t->hash; }
158 static bool equal (type_hash *a, type_hash *b);
159
160 static int
161 keep_cache_entry (type_hash *&t)
162 {
163 return ggc_marked_p (t->type);
164 }
165};
166
167/* Now here is the hash table. When recording a type, it is added to
168 the slot whose index is the hash code. Note that the hash table is
169 used for several kinds of types (function types, array types and
170 array index range types, for now). While all these live in the
171 same table, they are completely independent, and the hash code is
172 computed differently for each of these. */
173
174static GTY ((cache)) hash_table<type_cache_hasher> *type_hash_table;
175
176/* Hash table and temporary node for larger integer const values. */
177static GTY (()) tree int_cst_node;
178
179struct int_cst_hasher : ggc_cache_ptr_hash<tree_node>
180{
181 static hashval_t hash (tree t);
182 static bool equal (tree x, tree y);
183};
184
185static GTY ((cache)) hash_table<int_cst_hasher> *int_cst_hash_table;
186
187/* Class and variable for making sure that there is a single POLY_INT_CST
188 for a given value. */
189struct poly_int_cst_hasher : ggc_cache_ptr_hash<tree_node>
190{
191 typedef std::pair<tree, const poly_wide_int *> compare_type;
192 static hashval_t hash (tree t);
193 static bool equal (tree x, const compare_type &y);
194};
195
196static GTY ((cache)) hash_table<poly_int_cst_hasher> *poly_int_cst_hash_table;
197
198/* Hash table for optimization flags and target option flags. Use the same
199 hash table for both sets of options. Nodes for building the current
200 optimization and target option nodes. The assumption is most of the time
201 the options created will already be in the hash table, so we avoid
202 allocating and freeing up a node repeatably. */
203static GTY (()) tree cl_optimization_node;
204static GTY (()) tree cl_target_option_node;
205
206struct cl_option_hasher : ggc_cache_ptr_hash<tree_node>
207{
208 static hashval_t hash (tree t);
209 static bool equal (tree x, tree y);
210};
211
212static GTY ((cache)) hash_table<cl_option_hasher> *cl_option_hash_table;
213
214/* General tree->tree mapping structure for use in hash tables. */
215
216
217static GTY ((cache))
218 hash_table<tree_decl_map_cache_hasher> *debug_expr_for_decl;
219
220static GTY ((cache))
221 hash_table<tree_decl_map_cache_hasher> *value_expr_for_decl;
222
223static GTY ((cache))
224 hash_table<tree_vec_map_cache_hasher> *debug_args_for_decl;
225
226static void set_type_quals (tree, int);
227static void print_type_hash_statistics (void);
228static void print_debug_expr_statistics (void);
229static void print_value_expr_statistics (void);
230
231tree global_trees[TI_MAX];
232tree integer_types[itk_none];
233
234bool int_n_enabled_p[NUM_INT_N_ENTS];
235struct int_n_trees_t int_n_trees [NUM_INT_N_ENTS];
236
237bool tree_contains_struct[MAX_TREE_CODES][64];
238
239/* Number of operands for each OMP clause. */
240unsigned const char omp_clause_num_ops[] =
241{
242 0, /* OMP_CLAUSE_ERROR */
243 1, /* OMP_CLAUSE_PRIVATE */
244 1, /* OMP_CLAUSE_SHARED */
245 1, /* OMP_CLAUSE_FIRSTPRIVATE */
246 2, /* OMP_CLAUSE_LASTPRIVATE */
247 5, /* OMP_CLAUSE_REDUCTION */
248 5, /* OMP_CLAUSE_TASK_REDUCTION */
249 5, /* OMP_CLAUSE_IN_REDUCTION */
250 1, /* OMP_CLAUSE_COPYIN */
251 1, /* OMP_CLAUSE_COPYPRIVATE */
252 3, /* OMP_CLAUSE_LINEAR */
253 1, /* OMP_CLAUSE_AFFINITY */
254 2, /* OMP_CLAUSE_ALIGNED */
255 3, /* OMP_CLAUSE_ALLOCATE */
256 1, /* OMP_CLAUSE_DEPEND */
257 1, /* OMP_CLAUSE_NONTEMPORAL */
258 1, /* OMP_CLAUSE_UNIFORM */
259 1, /* OMP_CLAUSE_ENTER */
260 1, /* OMP_CLAUSE_LINK */
261 1, /* OMP_CLAUSE_DETACH */
262 1, /* OMP_CLAUSE_USE_DEVICE_PTR */
263 1, /* OMP_CLAUSE_USE_DEVICE_ADDR */
264 1, /* OMP_CLAUSE_IS_DEVICE_PTR */
265 1, /* OMP_CLAUSE_INCLUSIVE */
266 1, /* OMP_CLAUSE_EXCLUSIVE */
267 2, /* OMP_CLAUSE_FROM */
268 2, /* OMP_CLAUSE_TO */
269 2, /* OMP_CLAUSE_MAP */
270 1, /* OMP_CLAUSE_HAS_DEVICE_ADDR */
271 1, /* OMP_CLAUSE_DOACROSS */
272 2, /* OMP_CLAUSE__CACHE_ */
273 2, /* OMP_CLAUSE_GANG */
274 1, /* OMP_CLAUSE_ASYNC */
275 1, /* OMP_CLAUSE_WAIT */
276 0, /* OMP_CLAUSE_AUTO */
277 0, /* OMP_CLAUSE_SEQ */
278 1, /* OMP_CLAUSE__LOOPTEMP_ */
279 1, /* OMP_CLAUSE__REDUCTEMP_ */
280 1, /* OMP_CLAUSE__CONDTEMP_ */
281 1, /* OMP_CLAUSE__SCANTEMP_ */
282 1, /* OMP_CLAUSE_IF */
283 1, /* OMP_CLAUSE_SELF */
284 1, /* OMP_CLAUSE_NUM_THREADS */
285 1, /* OMP_CLAUSE_SCHEDULE */
286 0, /* OMP_CLAUSE_NOWAIT */
287 1, /* OMP_CLAUSE_ORDERED */
288 0, /* OMP_CLAUSE_DEFAULT */
289 3, /* OMP_CLAUSE_COLLAPSE */
290 0, /* OMP_CLAUSE_UNTIED */
291 1, /* OMP_CLAUSE_FINAL */
292 0, /* OMP_CLAUSE_MERGEABLE */
293 1, /* OMP_CLAUSE_DEVICE */
294 1, /* OMP_CLAUSE_DIST_SCHEDULE */
295 0, /* OMP_CLAUSE_INBRANCH */
296 0, /* OMP_CLAUSE_NOTINBRANCH */
297 2, /* OMP_CLAUSE_NUM_TEAMS */
298 1, /* OMP_CLAUSE_THREAD_LIMIT */
299 0, /* OMP_CLAUSE_PROC_BIND */
300 1, /* OMP_CLAUSE_SAFELEN */
301 1, /* OMP_CLAUSE_SIMDLEN */
302 0, /* OMP_CLAUSE_DEVICE_TYPE */
303 0, /* OMP_CLAUSE_FOR */
304 0, /* OMP_CLAUSE_PARALLEL */
305 0, /* OMP_CLAUSE_SECTIONS */
306 0, /* OMP_CLAUSE_TASKGROUP */
307 1, /* OMP_CLAUSE_PRIORITY */
308 1, /* OMP_CLAUSE_GRAINSIZE */
309 1, /* OMP_CLAUSE_NUM_TASKS */
310 0, /* OMP_CLAUSE_NOGROUP */
311 0, /* OMP_CLAUSE_THREADS */
312 0, /* OMP_CLAUSE_SIMD */
313 1, /* OMP_CLAUSE_HINT */
314 0, /* OMP_CLAUSE_DEFAULTMAP */
315 0, /* OMP_CLAUSE_ORDER */
316 0, /* OMP_CLAUSE_BIND */
317 1, /* OMP_CLAUSE_FILTER */
318 1, /* OMP_CLAUSE_INDIRECT */
319 1, /* OMP_CLAUSE__SIMDUID_ */
320 0, /* OMP_CLAUSE__SIMT_ */
321 0, /* OMP_CLAUSE_INDEPENDENT */
322 1, /* OMP_CLAUSE_WORKER */
323 1, /* OMP_CLAUSE_VECTOR */
324 1, /* OMP_CLAUSE_NUM_GANGS */
325 1, /* OMP_CLAUSE_NUM_WORKERS */
326 1, /* OMP_CLAUSE_VECTOR_LENGTH */
327 3, /* OMP_CLAUSE_TILE */
328 0, /* OMP_CLAUSE_IF_PRESENT */
329 0, /* OMP_CLAUSE_FINALIZE */
330 0, /* OMP_CLAUSE_NOHOST */
331};
332
333const char * const omp_clause_code_name[] =
334{
335 "error_clause",
336 "private",
337 "shared",
338 "firstprivate",
339 "lastprivate",
340 "reduction",
341 "task_reduction",
342 "in_reduction",
343 "copyin",
344 "copyprivate",
345 "linear",
346 "affinity",
347 "aligned",
348 "allocate",
349 "depend",
350 "nontemporal",
351 "uniform",
352 "enter",
353 "link",
354 "detach",
355 "use_device_ptr",
356 "use_device_addr",
357 "is_device_ptr",
358 "inclusive",
359 "exclusive",
360 "from",
361 "to",
362 "map",
363 "has_device_addr",
364 "doacross",
365 "_cache_",
366 "gang",
367 "async",
368 "wait",
369 "auto",
370 "seq",
371 "_looptemp_",
372 "_reductemp_",
373 "_condtemp_",
374 "_scantemp_",
375 "if",
376 "self",
377 "num_threads",
378 "schedule",
379 "nowait",
380 "ordered",
381 "default",
382 "collapse",
383 "untied",
384 "final",
385 "mergeable",
386 "device",
387 "dist_schedule",
388 "inbranch",
389 "notinbranch",
390 "num_teams",
391 "thread_limit",
392 "proc_bind",
393 "safelen",
394 "simdlen",
395 "device_type",
396 "for",
397 "parallel",
398 "sections",
399 "taskgroup",
400 "priority",
401 "grainsize",
402 "num_tasks",
403 "nogroup",
404 "threads",
405 "simd",
406 "hint",
407 "defaultmap",
408 "order",
409 "bind",
410 "filter",
411 "indirect",
412 "_simduid_",
413 "_simt_",
414 "independent",
415 "worker",
416 "vector",
417 "num_gangs",
418 "num_workers",
419 "vector_length",
420 "tile",
421 "if_present",
422 "finalize",
423 "nohost",
424};
425
426/* Unless specific to OpenACC, we tend to internally maintain OpenMP-centric
427 clause names, but for use in diagnostics etc. would like to use the "user"
428 clause names. */
429
430const char *
431user_omp_clause_code_name (tree clause, bool oacc)
432{
433 /* For OpenACC, the 'OMP_CLAUSE_MAP_KIND' of an 'OMP_CLAUSE_MAP' is used to
434 distinguish clauses as seen by the user. See also where front ends do
435 'build_omp_clause' with 'OMP_CLAUSE_MAP'. */
436 if (oacc && OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP)
437 switch (OMP_CLAUSE_MAP_KIND (clause))
438 {
439 case GOMP_MAP_FORCE_ALLOC:
440 case GOMP_MAP_ALLOC: return "create";
441 case GOMP_MAP_FORCE_TO:
442 case GOMP_MAP_TO: return "copyin";
443 case GOMP_MAP_FORCE_FROM:
444 case GOMP_MAP_FROM: return "copyout";
445 case GOMP_MAP_FORCE_TOFROM:
446 case GOMP_MAP_TOFROM: return "copy";
447 case GOMP_MAP_RELEASE: return "delete";
448 case GOMP_MAP_FORCE_PRESENT: return "present";
449 case GOMP_MAP_ATTACH: return "attach";
450 case GOMP_MAP_FORCE_DETACH:
451 case GOMP_MAP_DETACH: return "detach";
452 case GOMP_MAP_DEVICE_RESIDENT: return "device_resident";
453 case GOMP_MAP_LINK: return "link";
454 case GOMP_MAP_FORCE_DEVICEPTR: return "deviceptr";
455 default: break;
456 }
457
458 return omp_clause_code_name[OMP_CLAUSE_CODE (clause)];
459}
460
461
462/* Return the tree node structure used by tree code CODE. */
463
464static inline enum tree_node_structure_enum
465tree_node_structure_for_code (enum tree_code code)
466{
467 switch (TREE_CODE_CLASS (code))
468 {
469 case tcc_declaration:
470 switch (code)
471 {
472 case CONST_DECL: return TS_CONST_DECL;
473 case DEBUG_EXPR_DECL: return TS_DECL_WRTL;
474 case FIELD_DECL: return TS_FIELD_DECL;
475 case FUNCTION_DECL: return TS_FUNCTION_DECL;
476 case LABEL_DECL: return TS_LABEL_DECL;
477 case PARM_DECL: return TS_PARM_DECL;
478 case RESULT_DECL: return TS_RESULT_DECL;
479 case TRANSLATION_UNIT_DECL: return TS_TRANSLATION_UNIT_DECL;
480 case TYPE_DECL: return TS_TYPE_DECL;
481 case VAR_DECL: return TS_VAR_DECL;
482 default: return TS_DECL_NON_COMMON;
483 }
484
485 case tcc_type: return TS_TYPE_NON_COMMON;
486
487 case tcc_binary:
488 case tcc_comparison:
489 case tcc_expression:
490 case tcc_reference:
491 case tcc_statement:
492 case tcc_unary:
493 case tcc_vl_exp: return TS_EXP;
494
495 default: /* tcc_constant and tcc_exceptional */
496 break;
497 }
498
499 switch (code)
500 {
501 /* tcc_constant cases. */
502 case COMPLEX_CST: return TS_COMPLEX;
503 case FIXED_CST: return TS_FIXED_CST;
504 case INTEGER_CST: return TS_INT_CST;
505 case POLY_INT_CST: return TS_POLY_INT_CST;
506 case REAL_CST: return TS_REAL_CST;
507 case STRING_CST: return TS_STRING;
508 case VECTOR_CST: return TS_VECTOR;
509 case VOID_CST: return TS_TYPED;
510
511 /* tcc_exceptional cases. */
512 case BLOCK: return TS_BLOCK;
513 case CONSTRUCTOR: return TS_CONSTRUCTOR;
514 case ERROR_MARK: return TS_COMMON;
515 case IDENTIFIER_NODE: return TS_IDENTIFIER;
516 case OMP_CLAUSE: return TS_OMP_CLAUSE;
517 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
518 case PLACEHOLDER_EXPR: return TS_COMMON;
519 case SSA_NAME: return TS_SSA_NAME;
520 case STATEMENT_LIST: return TS_STATEMENT_LIST;
521 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
522 case TREE_BINFO: return TS_BINFO;
523 case TREE_LIST: return TS_LIST;
524 case TREE_VEC: return TS_VEC;
525
526 default:
527 gcc_unreachable ();
528 }
529}
530
531
532/* Initialize tree_contains_struct to describe the hierarchy of tree
533 nodes. */
534
535static void
536initialize_tree_contains_struct (void)
537{
538 unsigned i;
539
540 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
541 {
542 enum tree_code code;
543 enum tree_node_structure_enum ts_code;
544
545 code = (enum tree_code) i;
546 ts_code = tree_node_structure_for_code (code);
547
548 /* Mark the TS structure itself. */
549 tree_contains_struct[code][ts_code] = 1;
550
551 /* Mark all the structures that TS is derived from. */
552 switch (ts_code)
553 {
554 case TS_TYPED:
555 case TS_BLOCK:
556 case TS_OPTIMIZATION:
557 case TS_TARGET_OPTION:
558 MARK_TS_BASE (code);
559 break;
560
561 case TS_COMMON:
562 case TS_INT_CST:
563 case TS_POLY_INT_CST:
564 case TS_REAL_CST:
565 case TS_FIXED_CST:
566 case TS_VECTOR:
567 case TS_STRING:
568 case TS_COMPLEX:
569 case TS_SSA_NAME:
570 case TS_CONSTRUCTOR:
571 case TS_EXP:
572 case TS_STATEMENT_LIST:
573 MARK_TS_TYPED (code);
574 break;
575
576 case TS_IDENTIFIER:
577 case TS_DECL_MINIMAL:
578 case TS_TYPE_COMMON:
579 case TS_LIST:
580 case TS_VEC:
581 case TS_BINFO:
582 case TS_OMP_CLAUSE:
583 MARK_TS_COMMON (code);
584 break;
585
586 case TS_TYPE_WITH_LANG_SPECIFIC:
587 MARK_TS_TYPE_COMMON (code);
588 break;
589
590 case TS_TYPE_NON_COMMON:
591 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
592 break;
593
594 case TS_DECL_COMMON:
595 MARK_TS_DECL_MINIMAL (code);
596 break;
597
598 case TS_DECL_WRTL:
599 case TS_CONST_DECL:
600 MARK_TS_DECL_COMMON (code);
601 break;
602
603 case TS_DECL_NON_COMMON:
604 MARK_TS_DECL_WITH_VIS (code);
605 break;
606
607 case TS_DECL_WITH_VIS:
608 case TS_PARM_DECL:
609 case TS_LABEL_DECL:
610 case TS_RESULT_DECL:
611 MARK_TS_DECL_WRTL (code);
612 break;
613
614 case TS_FIELD_DECL:
615 MARK_TS_DECL_COMMON (code);
616 break;
617
618 case TS_VAR_DECL:
619 MARK_TS_DECL_WITH_VIS (code);
620 break;
621
622 case TS_TYPE_DECL:
623 case TS_FUNCTION_DECL:
624 MARK_TS_DECL_NON_COMMON (code);
625 break;
626
627 case TS_TRANSLATION_UNIT_DECL:
628 MARK_TS_DECL_COMMON (code);
629 break;
630
631 default:
632 gcc_unreachable ();
633 }
634 }
635
636 /* Basic consistency checks for attributes used in fold. */
637 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
638 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
639 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
640 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
641 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
642 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
643 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
644 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
645 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
646 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
647 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
648 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
649 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
650 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
651 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
652 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
653 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
654 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
655 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
656 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
657 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
658 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
659 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
660 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
661 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
662 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
663 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
664 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
665 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
666 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
667 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
668 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
669 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
670 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
671 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
672 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
673 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
674 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
675 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_MINIMAL]);
676 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_COMMON]);
677}
678
679
680/* Init tree.cc. */
681
682void
683init_ttree (void)
684{
685 /* Initialize the hash table of types. */
686 type_hash_table
687 = hash_table<type_cache_hasher>::create_ggc (TYPE_HASH_INITIAL_SIZE);
688
689 debug_expr_for_decl
690 = hash_table<tree_decl_map_cache_hasher>::create_ggc (n: 512);
691
692 value_expr_for_decl
693 = hash_table<tree_decl_map_cache_hasher>::create_ggc (n: 512);
694
695 int_cst_hash_table = hash_table<int_cst_hasher>::create_ggc (n: 1024);
696
697 poly_int_cst_hash_table = hash_table<poly_int_cst_hasher>::create_ggc (n: 64);
698
699 int_cst_node = make_int_cst (1, 1);
700
701 cl_option_hash_table = hash_table<cl_option_hasher>::create_ggc (n: 64);
702
703 cl_optimization_node = make_node (OPTIMIZATION_NODE);
704 cl_target_option_node = make_node (TARGET_OPTION_NODE);
705
706 /* Initialize the tree_contains_struct array. */
707 initialize_tree_contains_struct ();
708 lang_hooks.init_ts ();
709}
710
711
712/* The name of the object as the assembler will see it (but before any
713 translations made by ASM_OUTPUT_LABELREF). Often this is the same
714 as DECL_NAME. It is an IDENTIFIER_NODE. */
715tree
716decl_assembler_name (tree decl)
717{
718 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
719 lang_hooks.set_decl_assembler_name (decl);
720 return DECL_ASSEMBLER_NAME_RAW (decl);
721}
722
723/* The DECL_ASSEMBLER_NAME_RAW of DECL is being explicitly set to NAME
724 (either of which may be NULL). Inform the FE, if this changes the
725 name. */
726
727void
728overwrite_decl_assembler_name (tree decl, tree name)
729{
730 if (DECL_ASSEMBLER_NAME_RAW (decl) != name)
731 lang_hooks.overwrite_decl_assembler_name (decl, name);
732}
733
734/* Return true if DECL may need an assembler name to be set. */
735
736static inline bool
737need_assembler_name_p (tree decl)
738{
739 /* We use DECL_ASSEMBLER_NAME to hold mangled type names for One Definition
740 Rule merging. This makes type_odr_p to return true on those types during
741 LTO and by comparing the mangled name, we can say what types are intended
742 to be equivalent across compilation unit.
743
744 We do not store names of type_in_anonymous_namespace_p.
745
746 Record, union and enumeration type have linkage that allows use
747 to check type_in_anonymous_namespace_p. We do not mangle compound types
748 that always can be compared structurally.
749
750 Similarly for builtin types, we compare properties of their main variant.
751 A special case are integer types where mangling do make differences
752 between char/signed char/unsigned char etc. Storing name for these makes
753 e.g. -fno-signed-char/-fsigned-char mismatches to be handled well.
754 See cp/mangle.cc:write_builtin_type for details. */
755
756 if (TREE_CODE (decl) == TYPE_DECL)
757 {
758 if (DECL_NAME (decl)
759 && decl == TYPE_NAME (TREE_TYPE (decl))
760 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TREE_TYPE (decl)
761 && !TYPE_ARTIFICIAL (TREE_TYPE (decl))
762 && ((TREE_CODE (TREE_TYPE (decl)) != RECORD_TYPE
763 && TREE_CODE (TREE_TYPE (decl)) != UNION_TYPE)
764 || TYPE_CXX_ODR_P (TREE_TYPE (decl)))
765 && (type_with_linkage_p (TREE_TYPE (decl))
766 || TREE_CODE (TREE_TYPE (decl)) == INTEGER_TYPE)
767 && !variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
768 return !DECL_ASSEMBLER_NAME_SET_P (decl);
769 return false;
770 }
771 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
772 if (!VAR_OR_FUNCTION_DECL_P (decl))
773 return false;
774
775 /* If DECL already has its assembler name set, it does not need a
776 new one. */
777 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
778 || DECL_ASSEMBLER_NAME_SET_P (decl))
779 return false;
780
781 /* Abstract decls do not need an assembler name. */
782 if (DECL_ABSTRACT_P (decl))
783 return false;
784
785 /* For VAR_DECLs, only static, public and external symbols need an
786 assembler name. */
787 if (VAR_P (decl)
788 && !TREE_STATIC (decl)
789 && !TREE_PUBLIC (decl)
790 && !DECL_EXTERNAL (decl))
791 return false;
792
793 if (TREE_CODE (decl) == FUNCTION_DECL)
794 {
795 /* Do not set assembler name on builtins. Allow RTL expansion to
796 decide whether to expand inline or via a regular call. */
797 if (fndecl_built_in_p (node: decl)
798 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
799 return false;
800
801 /* Functions represented in the callgraph need an assembler name. */
802 if (cgraph_node::get (decl) != NULL)
803 return true;
804
805 /* Unused and not public functions don't need an assembler name. */
806 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
807 return false;
808 }
809
810 return true;
811}
812
813/* If T needs an assembler name, have one created for it. */
814
815void
816assign_assembler_name_if_needed (tree t)
817{
818 if (need_assembler_name_p (decl: t))
819 {
820 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
821 diagnostics that use input_location to show locus
822 information. The problem here is that, at this point,
823 input_location is generally anchored to the end of the file
824 (since the parser is long gone), so we don't have a good
825 position to pin it to.
826
827 To alleviate this problem, this uses the location of T's
828 declaration. Examples of this are
829 testsuite/g++.dg/template/cond2.C and
830 testsuite/g++.dg/template/pr35240.C. */
831 location_t saved_location = input_location;
832 input_location = DECL_SOURCE_LOCATION (t);
833
834 decl_assembler_name (decl: t);
835
836 input_location = saved_location;
837 }
838}
839
840/* When the target supports COMDAT groups, this indicates which group the
841 DECL is associated with. This can be either an IDENTIFIER_NODE or a
842 decl, in which case its DECL_ASSEMBLER_NAME identifies the group. */
843tree
844decl_comdat_group (const_tree node)
845{
846 struct symtab_node *snode = symtab_node::get (decl: node);
847 if (!snode)
848 return NULL;
849 return snode->get_comdat_group ();
850}
851
852/* Likewise, but make sure it's been reduced to an IDENTIFIER_NODE. */
853tree
854decl_comdat_group_id (const_tree node)
855{
856 struct symtab_node *snode = symtab_node::get (decl: node);
857 if (!snode)
858 return NULL;
859 return snode->get_comdat_group_id ();
860}
861
862/* When the target supports named section, return its name as IDENTIFIER_NODE
863 or NULL if it is in no section. */
864const char *
865decl_section_name (const_tree node)
866{
867 struct symtab_node *snode = symtab_node::get (decl: node);
868 if (!snode)
869 return NULL;
870 return snode->get_section ();
871}
872
873/* Set section name of NODE to VALUE (that is expected to be
874 identifier node) */
875void
876set_decl_section_name (tree node, const char *value)
877{
878 struct symtab_node *snode;
879
880 if (value == NULL)
881 {
882 snode = symtab_node::get (decl: node);
883 if (!snode)
884 return;
885 }
886 else if (VAR_P (node))
887 snode = varpool_node::get_create (decl: node);
888 else
889 snode = cgraph_node::get_create (node);
890 snode->set_section (value);
891}
892
893/* Set section name of NODE to match the section name of OTHER.
894
895 set_decl_section_name (decl, other) is equivalent to
896 set_decl_section_name (decl, DECL_SECTION_NAME (other)), but possibly more
897 efficient. */
898void
899set_decl_section_name (tree decl, const_tree other)
900{
901 struct symtab_node *other_node = symtab_node::get (decl: other);
902 if (other_node)
903 {
904 struct symtab_node *decl_node;
905 if (VAR_P (decl))
906 decl_node = varpool_node::get_create (decl);
907 else
908 decl_node = cgraph_node::get_create (decl);
909 decl_node->set_section (*other_node);
910 }
911 else
912 {
913 struct symtab_node *decl_node = symtab_node::get (decl);
914 if (!decl_node)
915 return;
916 decl_node->set_section (NULL);
917 }
918}
919
920/* Return TLS model of a variable NODE. */
921enum tls_model
922decl_tls_model (const_tree node)
923{
924 struct varpool_node *snode = varpool_node::get (decl: node);
925 if (!snode)
926 return TLS_MODEL_NONE;
927 return snode->tls_model;
928}
929
930/* Set TLS model of variable NODE to MODEL. */
931void
932set_decl_tls_model (tree node, enum tls_model model)
933{
934 struct varpool_node *vnode;
935
936 if (model == TLS_MODEL_NONE)
937 {
938 vnode = varpool_node::get (decl: node);
939 if (!vnode)
940 return;
941 }
942 else
943 vnode = varpool_node::get_create (decl: node);
944 vnode->tls_model = model;
945}
946
947/* Compute the number of bytes occupied by a tree with code CODE.
948 This function cannot be used for nodes that have variable sizes,
949 including TREE_VEC, INTEGER_CST, STRING_CST, and CALL_EXPR. */
950size_t
951tree_code_size (enum tree_code code)
952{
953 switch (TREE_CODE_CLASS (code))
954 {
955 case tcc_declaration: /* A decl node */
956 switch (code)
957 {
958 case FIELD_DECL: return sizeof (tree_field_decl);
959 case PARM_DECL: return sizeof (tree_parm_decl);
960 case VAR_DECL: return sizeof (tree_var_decl);
961 case LABEL_DECL: return sizeof (tree_label_decl);
962 case RESULT_DECL: return sizeof (tree_result_decl);
963 case CONST_DECL: return sizeof (tree_const_decl);
964 case TYPE_DECL: return sizeof (tree_type_decl);
965 case FUNCTION_DECL: return sizeof (tree_function_decl);
966 case DEBUG_EXPR_DECL: return sizeof (tree_decl_with_rtl);
967 case TRANSLATION_UNIT_DECL: return sizeof (tree_translation_unit_decl);
968 case NAMESPACE_DECL:
969 case IMPORTED_DECL:
970 case NAMELIST_DECL: return sizeof (tree_decl_non_common);
971 default:
972 gcc_checking_assert (code >= NUM_TREE_CODES);
973 return lang_hooks.tree_size (code);
974 }
975
976 case tcc_type: /* a type node */
977 switch (code)
978 {
979 case OFFSET_TYPE:
980 case ENUMERAL_TYPE:
981 case BOOLEAN_TYPE:
982 case INTEGER_TYPE:
983 case REAL_TYPE:
984 case OPAQUE_TYPE:
985 case POINTER_TYPE:
986 case REFERENCE_TYPE:
987 case NULLPTR_TYPE:
988 case FIXED_POINT_TYPE:
989 case COMPLEX_TYPE:
990 case VECTOR_TYPE:
991 case ARRAY_TYPE:
992 case RECORD_TYPE:
993 case UNION_TYPE:
994 case QUAL_UNION_TYPE:
995 case VOID_TYPE:
996 case FUNCTION_TYPE:
997 case METHOD_TYPE:
998 case BITINT_TYPE:
999 case LANG_TYPE: return sizeof (tree_type_non_common);
1000 default:
1001 gcc_checking_assert (code >= NUM_TREE_CODES);
1002 return lang_hooks.tree_size (code);
1003 }
1004
1005 case tcc_reference: /* a reference */
1006 case tcc_expression: /* an expression */
1007 case tcc_statement: /* an expression with side effects */
1008 case tcc_comparison: /* a comparison expression */
1009 case tcc_unary: /* a unary arithmetic expression */
1010 case tcc_binary: /* a binary arithmetic expression */
1011 return (sizeof (struct tree_exp)
1012 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
1013
1014 case tcc_constant: /* a constant */
1015 switch (code)
1016 {
1017 case VOID_CST: return sizeof (tree_typed);
1018 case INTEGER_CST: gcc_unreachable ();
1019 case POLY_INT_CST: return sizeof (tree_poly_int_cst);
1020 case REAL_CST: return sizeof (tree_real_cst);
1021 case FIXED_CST: return sizeof (tree_fixed_cst);
1022 case COMPLEX_CST: return sizeof (tree_complex);
1023 case VECTOR_CST: gcc_unreachable ();
1024 case STRING_CST: gcc_unreachable ();
1025 default:
1026 gcc_checking_assert (code >= NUM_TREE_CODES);
1027 return lang_hooks.tree_size (code);
1028 }
1029
1030 case tcc_exceptional: /* something random, like an identifier. */
1031 switch (code)
1032 {
1033 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
1034 case TREE_LIST: return sizeof (tree_list);
1035
1036 case ERROR_MARK:
1037 case PLACEHOLDER_EXPR: return sizeof (tree_common);
1038
1039 case TREE_VEC: gcc_unreachable ();
1040 case OMP_CLAUSE: gcc_unreachable ();
1041
1042 case SSA_NAME: return sizeof (tree_ssa_name);
1043
1044 case STATEMENT_LIST: return sizeof (tree_statement_list);
1045 case BLOCK: return sizeof (struct tree_block);
1046 case CONSTRUCTOR: return sizeof (tree_constructor);
1047 case OPTIMIZATION_NODE: return sizeof (tree_optimization_option);
1048 case TARGET_OPTION_NODE: return sizeof (tree_target_option);
1049
1050 default:
1051 gcc_checking_assert (code >= NUM_TREE_CODES);
1052 return lang_hooks.tree_size (code);
1053 }
1054
1055 default:
1056 gcc_unreachable ();
1057 }
1058}
1059
1060/* Compute the number of bytes occupied by NODE. This routine only
1061 looks at TREE_CODE, except for those nodes that have variable sizes. */
1062size_t
1063tree_size (const_tree node)
1064{
1065 const enum tree_code code = TREE_CODE (node);
1066 switch (code)
1067 {
1068 case INTEGER_CST:
1069 return (sizeof (struct tree_int_cst)
1070 + (TREE_INT_CST_EXT_NUNITS (node) - 1) * sizeof (HOST_WIDE_INT));
1071
1072 case TREE_BINFO:
1073 return (offsetof (struct tree_binfo, base_binfos)
1074 + vec<tree, va_gc>
1075 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
1076
1077 case TREE_VEC:
1078 return (sizeof (struct tree_vec)
1079 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
1080
1081 case VECTOR_CST:
1082 return (sizeof (struct tree_vector)
1083 + (vector_cst_encoded_nelts (t: node) - 1) * sizeof (tree));
1084
1085 case STRING_CST:
1086 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
1087
1088 case OMP_CLAUSE:
1089 return (sizeof (struct tree_omp_clause)
1090 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
1091 * sizeof (tree));
1092
1093 default:
1094 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
1095 return (sizeof (struct tree_exp)
1096 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
1097 else
1098 return tree_code_size (code);
1099 }
1100}
1101
1102/* Return tree node kind based on tree CODE. */
1103
1104static tree_node_kind
1105get_stats_node_kind (enum tree_code code)
1106{
1107 enum tree_code_class type = TREE_CODE_CLASS (code);
1108
1109 switch (type)
1110 {
1111 case tcc_declaration: /* A decl node */
1112 return d_kind;
1113 case tcc_type: /* a type node */
1114 return t_kind;
1115 case tcc_statement: /* an expression with side effects */
1116 return s_kind;
1117 case tcc_reference: /* a reference */
1118 return r_kind;
1119 case tcc_expression: /* an expression */
1120 case tcc_comparison: /* a comparison expression */
1121 case tcc_unary: /* a unary arithmetic expression */
1122 case tcc_binary: /* a binary arithmetic expression */
1123 return e_kind;
1124 case tcc_constant: /* a constant */
1125 return c_kind;
1126 case tcc_exceptional: /* something random, like an identifier. */
1127 switch (code)
1128 {
1129 case IDENTIFIER_NODE:
1130 return id_kind;
1131 case TREE_VEC:
1132 return vec_kind;
1133 case TREE_BINFO:
1134 return binfo_kind;
1135 case SSA_NAME:
1136 return ssa_name_kind;
1137 case BLOCK:
1138 return b_kind;
1139 case CONSTRUCTOR:
1140 return constr_kind;
1141 case OMP_CLAUSE:
1142 return omp_clause_kind;
1143 default:
1144 return x_kind;
1145 }
1146 break;
1147 case tcc_vl_exp:
1148 return e_kind;
1149 default:
1150 gcc_unreachable ();
1151 }
1152}
1153
1154/* Record interesting allocation statistics for a tree node with CODE
1155 and LENGTH. */
1156
1157static void
1158record_node_allocation_statistics (enum tree_code code, size_t length)
1159{
1160 if (!GATHER_STATISTICS)
1161 return;
1162
1163 tree_node_kind kind = get_stats_node_kind (code);
1164
1165 tree_code_counts[(int) code]++;
1166 tree_node_counts[(int) kind]++;
1167 tree_node_sizes[(int) kind] += length;
1168}
1169
1170/* Allocate and return a new UID from the DECL_UID namespace. */
1171
1172int
1173allocate_decl_uid (void)
1174{
1175 return next_decl_uid++;
1176}
1177
1178/* Return a newly allocated node of code CODE. For decl and type
1179 nodes, some other fields are initialized. The rest of the node is
1180 initialized to zero. This function cannot be used for TREE_VEC,
1181 INTEGER_CST or OMP_CLAUSE nodes, which is enforced by asserts in
1182 tree_code_size.
1183
1184 Achoo! I got a code in the node. */
1185
1186tree
1187make_node (enum tree_code code MEM_STAT_DECL)
1188{
1189 tree t;
1190 enum tree_code_class type = TREE_CODE_CLASS (code);
1191 size_t length = tree_code_size (code);
1192
1193 record_node_allocation_statistics (code, length);
1194
1195 t = ggc_alloc_cleared_tree_node_stat (s: length PASS_MEM_STAT);
1196 TREE_SET_CODE (t, code);
1197
1198 switch (type)
1199 {
1200 case tcc_statement:
1201 if (code != DEBUG_BEGIN_STMT)
1202 TREE_SIDE_EFFECTS (t) = 1;
1203 break;
1204
1205 case tcc_declaration:
1206 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1207 {
1208 if (code == FUNCTION_DECL)
1209 {
1210 SET_DECL_ALIGN (t, FUNCTION_ALIGNMENT (FUNCTION_BOUNDARY));
1211 SET_DECL_MODE (t, FUNCTION_MODE);
1212 }
1213 else
1214 SET_DECL_ALIGN (t, 1);
1215 }
1216 DECL_SOURCE_LOCATION (t) = input_location;
1217 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
1218 DECL_UID (t) = --next_debug_decl_uid;
1219 else
1220 {
1221 DECL_UID (t) = allocate_decl_uid ();
1222 SET_DECL_PT_UID (t, -1);
1223 }
1224 if (TREE_CODE (t) == LABEL_DECL)
1225 LABEL_DECL_UID (t) = -1;
1226
1227 break;
1228
1229 case tcc_type:
1230 TYPE_UID (t) = next_type_uid++;
1231 SET_TYPE_ALIGN (t, BITS_PER_UNIT);
1232 TYPE_USER_ALIGN (t) = 0;
1233 TYPE_MAIN_VARIANT (t) = t;
1234 TYPE_CANONICAL (t) = t;
1235
1236 /* Default to no attributes for type, but let target change that. */
1237 TYPE_ATTRIBUTES (t) = NULL_TREE;
1238 targetm.set_default_type_attributes (t);
1239
1240 /* We have not yet computed the alias set for this type. */
1241 TYPE_ALIAS_SET (t) = -1;
1242 break;
1243
1244 case tcc_constant:
1245 TREE_CONSTANT (t) = 1;
1246 break;
1247
1248 case tcc_expression:
1249 switch (code)
1250 {
1251 case INIT_EXPR:
1252 case MODIFY_EXPR:
1253 case VA_ARG_EXPR:
1254 case PREDECREMENT_EXPR:
1255 case PREINCREMENT_EXPR:
1256 case POSTDECREMENT_EXPR:
1257 case POSTINCREMENT_EXPR:
1258 /* All of these have side-effects, no matter what their
1259 operands are. */
1260 TREE_SIDE_EFFECTS (t) = 1;
1261 break;
1262
1263 default:
1264 break;
1265 }
1266 break;
1267
1268 case tcc_exceptional:
1269 switch (code)
1270 {
1271 case TARGET_OPTION_NODE:
1272 TREE_TARGET_OPTION(t)
1273 = ggc_cleared_alloc<struct cl_target_option> ();
1274 break;
1275
1276 case OPTIMIZATION_NODE:
1277 TREE_OPTIMIZATION (t)
1278 = ggc_cleared_alloc<struct cl_optimization> ();
1279 break;
1280
1281 default:
1282 break;
1283 }
1284 break;
1285
1286 default:
1287 /* Other classes need no special treatment. */
1288 break;
1289 }
1290
1291 return t;
1292}
1293
1294/* Free tree node. */
1295
1296void
1297free_node (tree node)
1298{
1299 enum tree_code code = TREE_CODE (node);
1300 if (GATHER_STATISTICS)
1301 {
1302 enum tree_node_kind kind = get_stats_node_kind (code);
1303
1304 gcc_checking_assert (tree_code_counts[(int) TREE_CODE (node)] != 0);
1305 gcc_checking_assert (tree_node_counts[(int) kind] != 0);
1306 gcc_checking_assert (tree_node_sizes[(int) kind] >= tree_size (node));
1307
1308 tree_code_counts[(int) TREE_CODE (node)]--;
1309 tree_node_counts[(int) kind]--;
1310 tree_node_sizes[(int) kind] -= tree_size (node);
1311 }
1312 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1313 vec_free (CONSTRUCTOR_ELTS (node));
1314 else if (code == BLOCK)
1315 vec_free (BLOCK_NONLOCALIZED_VARS (node));
1316 else if (code == TREE_BINFO)
1317 vec_free (BINFO_BASE_ACCESSES (node));
1318 else if (code == OPTIMIZATION_NODE)
1319 cl_optimization_option_free (TREE_OPTIMIZATION (node));
1320 else if (code == TARGET_OPTION_NODE)
1321 cl_target_option_free (TREE_TARGET_OPTION (node));
1322 ggc_free (node);
1323}
1324
1325/* Return a new node with the same contents as NODE except that its
1326 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
1327
1328tree
1329copy_node (tree node MEM_STAT_DECL)
1330{
1331 tree t;
1332 enum tree_code code = TREE_CODE (node);
1333 size_t length;
1334
1335 gcc_assert (code != STATEMENT_LIST);
1336
1337 length = tree_size (node);
1338 record_node_allocation_statistics (code, length);
1339 t = ggc_alloc_tree_node_stat (s: length PASS_MEM_STAT);
1340 memcpy (dest: t, src: node, n: length);
1341
1342 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
1343 TREE_CHAIN (t) = 0;
1344 TREE_ASM_WRITTEN (t) = 0;
1345 TREE_VISITED (t) = 0;
1346
1347 if (TREE_CODE_CLASS (code) == tcc_declaration)
1348 {
1349 if (code == DEBUG_EXPR_DECL)
1350 DECL_UID (t) = --next_debug_decl_uid;
1351 else
1352 {
1353 DECL_UID (t) = allocate_decl_uid ();
1354 if (DECL_PT_UID_SET_P (node))
1355 SET_DECL_PT_UID (t, DECL_PT_UID (node));
1356 }
1357 if ((TREE_CODE (node) == PARM_DECL || VAR_P (node))
1358 && DECL_HAS_VALUE_EXPR_P (node))
1359 {
1360 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
1361 DECL_HAS_VALUE_EXPR_P (t) = 1;
1362 }
1363 /* DECL_DEBUG_EXPR is copied explicitly by callers. */
1364 if (VAR_P (node))
1365 {
1366 DECL_HAS_DEBUG_EXPR_P (t) = 0;
1367 t->decl_with_vis.symtab_node = NULL;
1368 }
1369 if (VAR_P (node) && DECL_HAS_INIT_PRIORITY_P (node))
1370 {
1371 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
1372 DECL_HAS_INIT_PRIORITY_P (t) = 1;
1373 }
1374 if (TREE_CODE (node) == FUNCTION_DECL)
1375 {
1376 DECL_STRUCT_FUNCTION (t) = NULL;
1377 t->decl_with_vis.symtab_node = NULL;
1378 }
1379 }
1380 else if (TREE_CODE_CLASS (code) == tcc_type)
1381 {
1382 TYPE_UID (t) = next_type_uid++;
1383 /* The following is so that the debug code for
1384 the copy is different from the original type.
1385 The two statements usually duplicate each other
1386 (because they clear fields of the same union),
1387 but the optimizer should catch that. */
1388 TYPE_SYMTAB_ADDRESS (t) = 0;
1389 TYPE_SYMTAB_DIE (t) = 0;
1390
1391 /* Do not copy the values cache. */
1392 if (TYPE_CACHED_VALUES_P (t))
1393 {
1394 TYPE_CACHED_VALUES_P (t) = 0;
1395 TYPE_CACHED_VALUES (t) = NULL_TREE;
1396 }
1397 }
1398 else if (code == TARGET_OPTION_NODE)
1399 {
1400 TREE_TARGET_OPTION (t) = ggc_alloc<struct cl_target_option>();
1401 memcpy (TREE_TARGET_OPTION (t), TREE_TARGET_OPTION (node),
1402 n: sizeof (struct cl_target_option));
1403 }
1404 else if (code == OPTIMIZATION_NODE)
1405 {
1406 TREE_OPTIMIZATION (t) = ggc_alloc<struct cl_optimization>();
1407 memcpy (TREE_OPTIMIZATION (t), TREE_OPTIMIZATION (node),
1408 n: sizeof (struct cl_optimization));
1409 }
1410
1411 return t;
1412}
1413
1414/* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1415 For example, this can copy a list made of TREE_LIST nodes. */
1416
1417tree
1418copy_list (tree list)
1419{
1420 tree head;
1421 tree prev, next;
1422
1423 if (list == 0)
1424 return 0;
1425
1426 head = prev = copy_node (node: list);
1427 next = TREE_CHAIN (list);
1428 while (next)
1429 {
1430 TREE_CHAIN (prev) = copy_node (node: next);
1431 prev = TREE_CHAIN (prev);
1432 next = TREE_CHAIN (next);
1433 }
1434 return head;
1435}
1436
1437
1438/* Return the value that TREE_INT_CST_EXT_NUNITS should have for an
1439 INTEGER_CST with value CST and type TYPE. */
1440
1441static unsigned int
1442get_int_cst_ext_nunits (tree type, const wide_int &cst)
1443{
1444 gcc_checking_assert (cst.get_precision () == TYPE_PRECISION (type));
1445 /* We need extra HWIs if CST is an unsigned integer with its
1446 upper bit set. */
1447 if (TYPE_UNSIGNED (type) && wi::neg_p (x: cst))
1448 return cst.get_precision () / HOST_BITS_PER_WIDE_INT + 1;
1449 return cst.get_len ();
1450}
1451
1452/* Return a new INTEGER_CST with value CST and type TYPE. */
1453
1454static tree
1455build_new_int_cst (tree type, const wide_int &cst)
1456{
1457 unsigned int len = cst.get_len ();
1458 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1459 tree nt = make_int_cst (len, ext_len);
1460
1461 if (len < ext_len)
1462 {
1463 --ext_len;
1464 TREE_INT_CST_ELT (nt, ext_len)
1465 = zext_hwi (src: -1, prec: cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1466 for (unsigned int i = len; i < ext_len; ++i)
1467 TREE_INT_CST_ELT (nt, i) = -1;
1468 }
1469 else if (TYPE_UNSIGNED (type)
1470 && cst.get_precision () < len * HOST_BITS_PER_WIDE_INT)
1471 {
1472 len--;
1473 TREE_INT_CST_ELT (nt, len)
1474 = zext_hwi (src: cst.elt (i: len),
1475 prec: cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1476 }
1477
1478 for (unsigned int i = 0; i < len; i++)
1479 TREE_INT_CST_ELT (nt, i) = cst.elt (i);
1480 TREE_TYPE (nt) = type;
1481 return nt;
1482}
1483
1484/* Return a new POLY_INT_CST with coefficients COEFFS and type TYPE. */
1485
1486static tree
1487build_new_poly_int_cst (tree type, tree (&coeffs)[NUM_POLY_INT_COEFFS]
1488 CXX_MEM_STAT_INFO)
1489{
1490 size_t length = sizeof (struct tree_poly_int_cst);
1491 record_node_allocation_statistics (code: POLY_INT_CST, length);
1492
1493 tree t = ggc_alloc_cleared_tree_node_stat (s: length PASS_MEM_STAT);
1494
1495 TREE_SET_CODE (t, POLY_INT_CST);
1496 TREE_CONSTANT (t) = 1;
1497 TREE_TYPE (t) = type;
1498 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1499 POLY_INT_CST_COEFF (t, i) = coeffs[i];
1500 return t;
1501}
1502
1503/* Create a constant tree that contains CST sign-extended to TYPE. */
1504
1505tree
1506build_int_cst (tree type, poly_int64 cst)
1507{
1508 /* Support legacy code. */
1509 if (!type)
1510 type = integer_type_node;
1511
1512 return wide_int_to_tree (type, cst: wi::shwi (a: cst, TYPE_PRECISION (type)));
1513}
1514
1515/* Create a constant tree that contains CST zero-extended to TYPE. */
1516
1517tree
1518build_int_cstu (tree type, poly_uint64 cst)
1519{
1520 return wide_int_to_tree (type, cst: wi::uhwi (a: cst, TYPE_PRECISION (type)));
1521}
1522
1523/* Create a constant tree that contains CST sign-extended to TYPE. */
1524
1525tree
1526build_int_cst_type (tree type, poly_int64 cst)
1527{
1528 gcc_assert (type);
1529 return wide_int_to_tree (type, cst: wi::shwi (a: cst, TYPE_PRECISION (type)));
1530}
1531
1532/* Constructs tree in type TYPE from with value given by CST. Signedness
1533 of CST is assumed to be the same as the signedness of TYPE. */
1534
1535tree
1536double_int_to_tree (tree type, double_int cst)
1537{
1538 return wide_int_to_tree (type, cst: widest_int::from (x: cst, TYPE_SIGN (type)));
1539}
1540
1541/* We force the wide_int CST to the range of the type TYPE by sign or
1542 zero extending it. OVERFLOWABLE indicates if we are interested in
1543 overflow of the value, when >0 we are only interested in signed
1544 overflow, for <0 we are interested in any overflow. OVERFLOWED
1545 indicates whether overflow has already occurred. CONST_OVERFLOWED
1546 indicates whether constant overflow has already occurred. We force
1547 T's value to be within range of T's type (by setting to 0 or 1 all
1548 the bits outside the type's range). We set TREE_OVERFLOWED if,
1549 OVERFLOWED is nonzero,
1550 or OVERFLOWABLE is >0 and signed overflow occurs
1551 or OVERFLOWABLE is <0 and any overflow occurs
1552 We return a new tree node for the extended wide_int. The node
1553 is shared if no overflow flags are set. */
1554
1555
1556tree
1557force_fit_type (tree type, const poly_wide_int_ref &cst,
1558 int overflowable, bool overflowed)
1559{
1560 signop sign = TYPE_SIGN (type);
1561
1562 /* If we need to set overflow flags, return a new unshared node. */
1563 if (overflowed || !wi::fits_to_tree_p (x: cst, type))
1564 {
1565 if (overflowed
1566 || overflowable < 0
1567 || (overflowable > 0 && sign == SIGNED))
1568 {
1569 poly_wide_int tmp = poly_wide_int::from (a: cst, TYPE_PRECISION (type),
1570 sgn: sign);
1571 tree t;
1572 if (tmp.is_constant ())
1573 t = build_new_int_cst (type, cst: tmp.coeffs[0]);
1574 else
1575 {
1576 tree coeffs[NUM_POLY_INT_COEFFS];
1577 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1578 {
1579 coeffs[i] = build_new_int_cst (type, cst: tmp.coeffs[i]);
1580 TREE_OVERFLOW (coeffs[i]) = 1;
1581 }
1582 t = build_new_poly_int_cst (type, coeffs);
1583 }
1584 TREE_OVERFLOW (t) = 1;
1585 return t;
1586 }
1587 }
1588
1589 /* Else build a shared node. */
1590 return wide_int_to_tree (type, cst);
1591}
1592
1593/* These are the hash table functions for the hash table of INTEGER_CST
1594 nodes of a sizetype. */
1595
1596/* Return the hash code X, an INTEGER_CST. */
1597
1598hashval_t
1599int_cst_hasher::hash (tree x)
1600{
1601 const_tree const t = x;
1602 hashval_t code = TYPE_UID (TREE_TYPE (t));
1603 int i;
1604
1605 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
1606 code = iterative_hash_host_wide_int (TREE_INT_CST_ELT(t, i), val2: code);
1607
1608 return code;
1609}
1610
1611/* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1612 is the same as that given by *Y, which is the same. */
1613
1614bool
1615int_cst_hasher::equal (tree x, tree y)
1616{
1617 const_tree const xt = x;
1618 const_tree const yt = y;
1619
1620 if (TREE_TYPE (xt) != TREE_TYPE (yt)
1621 || TREE_INT_CST_NUNITS (xt) != TREE_INT_CST_NUNITS (yt)
1622 || TREE_INT_CST_EXT_NUNITS (xt) != TREE_INT_CST_EXT_NUNITS (yt))
1623 return false;
1624
1625 for (int i = 0; i < TREE_INT_CST_NUNITS (xt); i++)
1626 if (TREE_INT_CST_ELT (xt, i) != TREE_INT_CST_ELT (yt, i))
1627 return false;
1628
1629 return true;
1630}
1631
1632/* Cache wide_int CST into the TYPE_CACHED_VALUES cache for TYPE.
1633 SLOT is the slot entry to store it in, and MAX_SLOTS is the maximum
1634 number of slots that can be cached for the type. */
1635
1636static inline tree
1637cache_wide_int_in_type_cache (tree type, const wide_int &cst,
1638 int slot, int max_slots)
1639{
1640 gcc_checking_assert (slot >= 0);
1641 /* Initialize cache. */
1642 if (!TYPE_CACHED_VALUES_P (type))
1643 {
1644 TYPE_CACHED_VALUES_P (type) = 1;
1645 TYPE_CACHED_VALUES (type) = make_tree_vec (max_slots);
1646 }
1647 tree t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), slot);
1648 if (!t)
1649 {
1650 /* Create a new shared int. */
1651 t = build_new_int_cst (type, cst);
1652 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), slot) = t;
1653 }
1654 return t;
1655}
1656
1657/* Create an INT_CST node of TYPE and value CST.
1658 The returned node is always shared. For small integers we use a
1659 per-type vector cache, for larger ones we use a single hash table.
1660 The value is extended from its precision according to the sign of
1661 the type to be a multiple of HOST_BITS_PER_WIDE_INT. This defines
1662 the upper bits and ensures that hashing and value equality based
1663 upon the underlying HOST_WIDE_INTs works without masking. */
1664
1665static tree
1666wide_int_to_tree_1 (tree type, const wide_int_ref &pcst)
1667{
1668 tree t;
1669 int ix = -1;
1670 int limit = 0;
1671
1672 gcc_assert (type);
1673 unsigned int prec = TYPE_PRECISION (type);
1674 signop sgn = TYPE_SIGN (type);
1675
1676 /* Verify that everything is canonical. */
1677 int l = pcst.get_len ();
1678 if (l > 1)
1679 {
1680 if (pcst.elt (i: l - 1) == 0)
1681 gcc_checking_assert (pcst.elt (l - 2) < 0);
1682 if (pcst.elt (i: l - 1) == HOST_WIDE_INT_M1)
1683 gcc_checking_assert (pcst.elt (l - 2) >= 0);
1684 }
1685
1686 wide_int cst = wide_int::from (x: pcst, precision: prec, sgn);
1687 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1688
1689 enum tree_code code = TREE_CODE (type);
1690 if (code == POINTER_TYPE || code == REFERENCE_TYPE)
1691 {
1692 /* Cache NULL pointer and zero bounds. */
1693 if (cst == 0)
1694 ix = 0;
1695 /* Cache upper bounds of pointers. */
1696 else if (cst == wi::max_value (prec, sgn))
1697 ix = 1;
1698 /* Cache 1 which is used for a non-zero range. */
1699 else if (cst == 1)
1700 ix = 2;
1701
1702 if (ix >= 0)
1703 {
1704 t = cache_wide_int_in_type_cache (type, cst, slot: ix, max_slots: 3);
1705 /* Make sure no one is clobbering the shared constant. */
1706 gcc_checking_assert (TREE_TYPE (t) == type
1707 && cst == wi::to_wide (t));
1708 return t;
1709 }
1710 }
1711 if (ext_len == 1)
1712 {
1713 /* We just need to store a single HOST_WIDE_INT. */
1714 HOST_WIDE_INT hwi;
1715 if (TYPE_UNSIGNED (type))
1716 hwi = cst.to_uhwi ();
1717 else
1718 hwi = cst.to_shwi ();
1719
1720 switch (code)
1721 {
1722 case NULLPTR_TYPE:
1723 gcc_assert (hwi == 0);
1724 /* Fallthru. */
1725
1726 case POINTER_TYPE:
1727 case REFERENCE_TYPE:
1728 /* Ignore pointers, as they were already handled above. */
1729 break;
1730
1731 case BOOLEAN_TYPE:
1732 /* Cache false or true. */
1733 limit = 2;
1734 if (IN_RANGE (hwi, 0, 1))
1735 ix = hwi;
1736 break;
1737
1738 case INTEGER_TYPE:
1739 case OFFSET_TYPE:
1740 case BITINT_TYPE:
1741 if (TYPE_SIGN (type) == UNSIGNED)
1742 {
1743 /* Cache [0, N). */
1744 limit = param_integer_share_limit;
1745 if (IN_RANGE (hwi, 0, param_integer_share_limit - 1))
1746 ix = hwi;
1747 }
1748 else
1749 {
1750 /* Cache [-1, N). */
1751 limit = param_integer_share_limit + 1;
1752 if (IN_RANGE (hwi, -1, param_integer_share_limit - 1))
1753 ix = hwi + 1;
1754 }
1755 break;
1756
1757 case ENUMERAL_TYPE:
1758 break;
1759
1760 default:
1761 gcc_unreachable ();
1762 }
1763
1764 if (ix >= 0)
1765 {
1766 t = cache_wide_int_in_type_cache (type, cst, slot: ix, max_slots: limit);
1767 /* Make sure no one is clobbering the shared constant. */
1768 gcc_checking_assert (TREE_TYPE (t) == type
1769 && TREE_INT_CST_NUNITS (t) == 1
1770 && TREE_INT_CST_EXT_NUNITS (t) == 1
1771 && TREE_INT_CST_ELT (t, 0) == hwi);
1772 return t;
1773 }
1774 else
1775 {
1776 /* Use the cache of larger shared ints, using int_cst_node as
1777 a temporary. */
1778
1779 TREE_INT_CST_ELT (int_cst_node, 0) = hwi;
1780 TREE_TYPE (int_cst_node) = type;
1781
1782 tree *slot = int_cst_hash_table->find_slot (value: int_cst_node, insert: INSERT);
1783 t = *slot;
1784 if (!t)
1785 {
1786 /* Insert this one into the hash table. */
1787 t = int_cst_node;
1788 *slot = t;
1789 /* Make a new node for next time round. */
1790 int_cst_node = make_int_cst (1, 1);
1791 }
1792 }
1793 }
1794 else
1795 {
1796 /* The value either hashes properly or we drop it on the floor
1797 for the gc to take care of. There will not be enough of them
1798 to worry about. */
1799
1800 tree nt = build_new_int_cst (type, cst);
1801 tree *slot = int_cst_hash_table->find_slot (value: nt, insert: INSERT);
1802 t = *slot;
1803 if (!t)
1804 {
1805 /* Insert this one into the hash table. */
1806 t = nt;
1807 *slot = t;
1808 }
1809 else
1810 ggc_free (nt);
1811 }
1812
1813 return t;
1814}
1815
1816hashval_t
1817poly_int_cst_hasher::hash (tree t)
1818{
1819 inchash::hash hstate;
1820
1821 hstate.add_int (TYPE_UID (TREE_TYPE (t)));
1822 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1823 hstate.add_wide_int (x: wi::to_wide (POLY_INT_CST_COEFF (t, i)));
1824
1825 return hstate.end ();
1826}
1827
1828bool
1829poly_int_cst_hasher::equal (tree x, const compare_type &y)
1830{
1831 if (TREE_TYPE (x) != y.first)
1832 return false;
1833 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1834 if (wi::to_wide (POLY_INT_CST_COEFF (x, i)) != y.second->coeffs[i])
1835 return false;
1836 return true;
1837}
1838
1839/* Build a POLY_INT_CST node with type TYPE and with the elements in VALUES.
1840 The elements must also have type TYPE. */
1841
1842tree
1843build_poly_int_cst (tree type, const poly_wide_int_ref &values)
1844{
1845 unsigned int prec = TYPE_PRECISION (type);
1846 gcc_assert (prec <= values.coeffs[0].get_precision ());
1847 poly_wide_int c = poly_wide_int::from (a: values, bitsize: prec, sgn: SIGNED);
1848
1849 inchash::hash h;
1850 h.add_int (TYPE_UID (type));
1851 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1852 h.add_wide_int (x: c.coeffs[i]);
1853 poly_int_cst_hasher::compare_type comp (type, &c);
1854 tree *slot = poly_int_cst_hash_table->find_slot_with_hash (comparable: comp, hash: h.end (),
1855 insert: INSERT);
1856 if (*slot == NULL_TREE)
1857 {
1858 tree coeffs[NUM_POLY_INT_COEFFS];
1859 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1860 coeffs[i] = wide_int_to_tree_1 (type, pcst: c.coeffs[i]);
1861 *slot = build_new_poly_int_cst (type, coeffs);
1862 }
1863 return *slot;
1864}
1865
1866/* Create a constant tree with value VALUE in type TYPE. */
1867
1868tree
1869wide_int_to_tree (tree type, const poly_wide_int_ref &value)
1870{
1871 if (value.is_constant ())
1872 return wide_int_to_tree_1 (type, pcst: value.coeffs[0]);
1873 return build_poly_int_cst (type, values: value);
1874}
1875
1876/* Insert INTEGER_CST T into a cache of integer constants. And return
1877 the cached constant (which may or may not be T). If MIGHT_DUPLICATE
1878 is false, and T falls into the type's 'smaller values' range, there
1879 cannot be an existing entry. Otherwise, if MIGHT_DUPLICATE is true,
1880 or the value is large, should an existing entry exist, it is
1881 returned (rather than inserting T). */
1882
1883tree
1884cache_integer_cst (tree t, bool might_duplicate ATTRIBUTE_UNUSED)
1885{
1886 tree type = TREE_TYPE (t);
1887 int ix = -1;
1888 int limit = 0;
1889 int prec = TYPE_PRECISION (type);
1890
1891 gcc_assert (!TREE_OVERFLOW (t));
1892
1893 /* The caching indices here must match those in
1894 wide_int_to_type_1. */
1895 switch (TREE_CODE (type))
1896 {
1897 case NULLPTR_TYPE:
1898 gcc_checking_assert (integer_zerop (t));
1899 /* Fallthru. */
1900
1901 case POINTER_TYPE:
1902 case REFERENCE_TYPE:
1903 {
1904 if (integer_zerop (t))
1905 ix = 0;
1906 else if (integer_onep (t))
1907 ix = 2;
1908
1909 if (ix >= 0)
1910 limit = 3;
1911 }
1912 break;
1913
1914 case BOOLEAN_TYPE:
1915 /* Cache false or true. */
1916 limit = 2;
1917 if (wi::ltu_p (x: wi::to_wide (t), y: 2))
1918 ix = TREE_INT_CST_ELT (t, 0);
1919 break;
1920
1921 case INTEGER_TYPE:
1922 case OFFSET_TYPE:
1923 case BITINT_TYPE:
1924 if (TYPE_UNSIGNED (type))
1925 {
1926 /* Cache 0..N */
1927 limit = param_integer_share_limit;
1928
1929 /* This is a little hokie, but if the prec is smaller than
1930 what is necessary to hold param_integer_share_limit, then the
1931 obvious test will not get the correct answer. */
1932 if (prec < HOST_BITS_PER_WIDE_INT)
1933 {
1934 if (tree_to_uhwi (t)
1935 < (unsigned HOST_WIDE_INT) param_integer_share_limit)
1936 ix = tree_to_uhwi (t);
1937 }
1938 else if (wi::ltu_p (x: wi::to_wide (t), param_integer_share_limit))
1939 ix = tree_to_uhwi (t);
1940 }
1941 else
1942 {
1943 /* Cache -1..N */
1944 limit = param_integer_share_limit + 1;
1945
1946 if (integer_minus_onep (t))
1947 ix = 0;
1948 else if (!wi::neg_p (x: wi::to_wide (t)))
1949 {
1950 if (prec < HOST_BITS_PER_WIDE_INT)
1951 {
1952 if (tree_to_shwi (t) < param_integer_share_limit)
1953 ix = tree_to_shwi (t) + 1;
1954 }
1955 else if (wi::ltu_p (x: wi::to_wide (t), param_integer_share_limit))
1956 ix = tree_to_shwi (t) + 1;
1957 }
1958 }
1959 break;
1960
1961 case ENUMERAL_TYPE:
1962 /* The slot used by TYPE_CACHED_VALUES is used for the enum
1963 members. */
1964 break;
1965
1966 default:
1967 gcc_unreachable ();
1968 }
1969
1970 if (ix >= 0)
1971 {
1972 /* Look for it in the type's vector of small shared ints. */
1973 if (!TYPE_CACHED_VALUES_P (type))
1974 {
1975 TYPE_CACHED_VALUES_P (type) = 1;
1976 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1977 }
1978
1979 if (tree r = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix))
1980 {
1981 gcc_checking_assert (might_duplicate);
1982 t = r;
1983 }
1984 else
1985 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1986 }
1987 else
1988 {
1989 /* Use the cache of larger shared ints. */
1990 tree *slot = int_cst_hash_table->find_slot (value: t, insert: INSERT);
1991 if (tree r = *slot)
1992 {
1993 /* If there is already an entry for the number verify it's the
1994 same value. */
1995 gcc_checking_assert (wi::to_wide (tree (r)) == wi::to_wide (t));
1996 /* And return the cached value. */
1997 t = r;
1998 }
1999 else
2000 /* Otherwise insert this one into the hash table. */
2001 *slot = t;
2002 }
2003
2004 return t;
2005}
2006
2007
2008/* Builds an integer constant in TYPE such that lowest BITS bits are ones
2009 and the rest are zeros. */
2010
2011tree
2012build_low_bits_mask (tree type, unsigned bits)
2013{
2014 gcc_assert (bits <= TYPE_PRECISION (type));
2015
2016 return wide_int_to_tree (type, value: wi::mask (width: bits, negate_p: false,
2017 TYPE_PRECISION (type)));
2018}
2019
2020/* Checks that X is integer constant that can be expressed in (unsigned)
2021 HOST_WIDE_INT without loss of precision. */
2022
2023bool
2024cst_and_fits_in_hwi (const_tree x)
2025{
2026 return (TREE_CODE (x) == INTEGER_CST
2027 && (tree_fits_shwi_p (x) || tree_fits_uhwi_p (x)));
2028}
2029
2030/* Build a newly constructed VECTOR_CST with the given values of
2031 (VECTOR_CST_)LOG2_NPATTERNS and (VECTOR_CST_)NELTS_PER_PATTERN. */
2032
2033tree
2034make_vector (unsigned log2_npatterns,
2035 unsigned int nelts_per_pattern MEM_STAT_DECL)
2036{
2037 gcc_assert (IN_RANGE (nelts_per_pattern, 1, 3));
2038 tree t;
2039 unsigned npatterns = 1 << log2_npatterns;
2040 unsigned encoded_nelts = npatterns * nelts_per_pattern;
2041 unsigned length = (sizeof (struct tree_vector)
2042 + (encoded_nelts - 1) * sizeof (tree));
2043
2044 record_node_allocation_statistics (code: VECTOR_CST, length);
2045
2046 t = ggc_alloc_cleared_tree_node_stat (s: length PASS_MEM_STAT);
2047
2048 TREE_SET_CODE (t, VECTOR_CST);
2049 TREE_CONSTANT (t) = 1;
2050 VECTOR_CST_LOG2_NPATTERNS (t) = log2_npatterns;
2051 VECTOR_CST_NELTS_PER_PATTERN (t) = nelts_per_pattern;
2052
2053 return t;
2054}
2055
2056/* Return a new VECTOR_CST node whose type is TYPE and whose values
2057 are extracted from V, a vector of CONSTRUCTOR_ELT. */
2058
2059tree
2060build_vector_from_ctor (tree type, const vec<constructor_elt, va_gc> *v)
2061{
2062 if (vec_safe_length (v) == 0)
2063 return build_zero_cst (type);
2064
2065 unsigned HOST_WIDE_INT idx, nelts;
2066 tree value;
2067
2068 /* We can't construct a VECTOR_CST for a variable number of elements. */
2069 nelts = TYPE_VECTOR_SUBPARTS (node: type).to_constant ();
2070 tree_vector_builder vec (type, nelts, 1);
2071 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
2072 {
2073 if (TREE_CODE (value) == VECTOR_CST)
2074 {
2075 /* If NELTS is constant then this must be too. */
2076 unsigned int sub_nelts = VECTOR_CST_NELTS (value).to_constant ();
2077 for (unsigned i = 0; i < sub_nelts; ++i)
2078 vec.quick_push (VECTOR_CST_ELT (value, i));
2079 }
2080 else
2081 vec.quick_push (obj: value);
2082 }
2083 while (vec.length () < nelts)
2084 vec.quick_push (obj: build_zero_cst (TREE_TYPE (type)));
2085
2086 return vec.build ();
2087}
2088
2089/* Build a vector of type VECTYPE where all the elements are SCs. */
2090tree
2091build_vector_from_val (tree vectype, tree sc)
2092{
2093 unsigned HOST_WIDE_INT i, nunits;
2094
2095 if (sc == error_mark_node)
2096 return sc;
2097
2098 /* Verify that the vector type is suitable for SC. Note that there
2099 is some inconsistency in the type-system with respect to restrict
2100 qualifications of pointers. Vector types always have a main-variant
2101 element type and the qualification is applied to the vector-type.
2102 So TREE_TYPE (vector-type) does not return a properly qualified
2103 vector element-type. */
2104 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
2105 TREE_TYPE (vectype)));
2106
2107 if (CONSTANT_CLASS_P (sc))
2108 {
2109 tree_vector_builder v (vectype, 1, 1);
2110 v.quick_push (obj: sc);
2111 return v.build ();
2112 }
2113 else if (!TYPE_VECTOR_SUBPARTS (node: vectype).is_constant (const_value: &nunits))
2114 return fold_build1 (VEC_DUPLICATE_EXPR, vectype, sc);
2115 else
2116 {
2117 vec<constructor_elt, va_gc> *v;
2118 vec_alloc (v, nelems: nunits);
2119 for (i = 0; i < nunits; ++i)
2120 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
2121 return build_constructor (vectype, v);
2122 }
2123}
2124
2125/* If TYPE is not a vector type, just return SC, otherwise return
2126 build_vector_from_val (TYPE, SC). */
2127
2128tree
2129build_uniform_cst (tree type, tree sc)
2130{
2131 if (!VECTOR_TYPE_P (type))
2132 return sc;
2133
2134 return build_vector_from_val (vectype: type, sc);
2135}
2136
2137/* Build a vector series of type TYPE in which element I has the value
2138 BASE + I * STEP. The result is a constant if BASE and STEP are constant
2139 and a VEC_SERIES_EXPR otherwise. */
2140
2141tree
2142build_vec_series (tree type, tree base, tree step)
2143{
2144 if (integer_zerop (step))
2145 return build_vector_from_val (vectype: type, sc: base);
2146 if (TREE_CODE (base) == INTEGER_CST && TREE_CODE (step) == INTEGER_CST)
2147 {
2148 tree_vector_builder builder (type, 1, 3);
2149 tree elt1 = wide_int_to_tree (TREE_TYPE (base),
2150 value: wi::to_wide (t: base) + wi::to_wide (t: step));
2151 tree elt2 = wide_int_to_tree (TREE_TYPE (base),
2152 value: wi::to_wide (t: elt1) + wi::to_wide (t: step));
2153 builder.quick_push (obj: base);
2154 builder.quick_push (obj: elt1);
2155 builder.quick_push (obj: elt2);
2156 return builder.build ();
2157 }
2158 return build2 (VEC_SERIES_EXPR, type, base, step);
2159}
2160
2161/* Return a vector with the same number of units and number of bits
2162 as VEC_TYPE, but in which the elements are a linear series of unsigned
2163 integers { BASE, BASE + STEP, BASE + STEP * 2, ... }. */
2164
2165tree
2166build_index_vector (tree vec_type, poly_uint64 base, poly_uint64 step)
2167{
2168 tree index_vec_type = vec_type;
2169 tree index_elt_type = TREE_TYPE (vec_type);
2170 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (node: vec_type);
2171 if (!INTEGRAL_TYPE_P (index_elt_type) || !TYPE_UNSIGNED (index_elt_type))
2172 {
2173 index_elt_type = build_nonstandard_integer_type
2174 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (index_elt_type)), true);
2175 index_vec_type = build_vector_type (index_elt_type, nunits);
2176 }
2177
2178 tree_vector_builder v (index_vec_type, 1, 3);
2179 for (unsigned int i = 0; i < 3; ++i)
2180 v.quick_push (obj: build_int_cstu (type: index_elt_type, cst: base + i * step));
2181 return v.build ();
2182}
2183
2184/* Return a VECTOR_CST of type VEC_TYPE in which the first NUM_A
2185 elements are A and the rest are B. */
2186
2187tree
2188build_vector_a_then_b (tree vec_type, unsigned int num_a, tree a, tree b)
2189{
2190 gcc_assert (known_le (num_a, TYPE_VECTOR_SUBPARTS (vec_type)));
2191 unsigned int count = constant_lower_bound (a: TYPE_VECTOR_SUBPARTS (node: vec_type));
2192 /* Optimize the constant case. */
2193 if ((count & 1) == 0 && TYPE_VECTOR_SUBPARTS (node: vec_type).is_constant ())
2194 count /= 2;
2195 tree_vector_builder builder (vec_type, count, 2);
2196 for (unsigned int i = 0; i < count * 2; ++i)
2197 builder.quick_push (obj: i < num_a ? a : b);
2198 return builder.build ();
2199}
2200
2201/* Something has messed with the elements of CONSTRUCTOR C after it was built;
2202 calculate TREE_CONSTANT and TREE_SIDE_EFFECTS. */
2203
2204void
2205recompute_constructor_flags (tree c)
2206{
2207 unsigned int i;
2208 tree val;
2209 bool constant_p = true;
2210 bool side_effects_p = false;
2211 vec<constructor_elt, va_gc> *vals = CONSTRUCTOR_ELTS (c);
2212
2213 FOR_EACH_CONSTRUCTOR_VALUE (vals, i, val)
2214 {
2215 /* Mostly ctors will have elts that don't have side-effects, so
2216 the usual case is to scan all the elements. Hence a single
2217 loop for both const and side effects, rather than one loop
2218 each (with early outs). */
2219 if (!TREE_CONSTANT (val))
2220 constant_p = false;
2221 if (TREE_SIDE_EFFECTS (val))
2222 side_effects_p = true;
2223 }
2224
2225 TREE_SIDE_EFFECTS (c) = side_effects_p;
2226 TREE_CONSTANT (c) = constant_p;
2227}
2228
2229/* Make sure that TREE_CONSTANT and TREE_SIDE_EFFECTS are correct for
2230 CONSTRUCTOR C. */
2231
2232void
2233verify_constructor_flags (tree c)
2234{
2235 unsigned int i;
2236 tree val;
2237 bool constant_p = TREE_CONSTANT (c);
2238 bool side_effects_p = TREE_SIDE_EFFECTS (c);
2239 vec<constructor_elt, va_gc> *vals = CONSTRUCTOR_ELTS (c);
2240
2241 FOR_EACH_CONSTRUCTOR_VALUE (vals, i, val)
2242 {
2243 if (constant_p && !TREE_CONSTANT (val))
2244 internal_error ("non-constant element in constant CONSTRUCTOR");
2245 if (!side_effects_p && TREE_SIDE_EFFECTS (val))
2246 internal_error ("side-effects element in no-side-effects CONSTRUCTOR");
2247 }
2248}
2249
2250/* Return a new CONSTRUCTOR node whose type is TYPE and whose values
2251 are in the vec pointed to by VALS. */
2252tree
2253build_constructor (tree type, vec<constructor_elt, va_gc> *vals MEM_STAT_DECL)
2254{
2255 tree c = make_node (code: CONSTRUCTOR PASS_MEM_STAT);
2256
2257 TREE_TYPE (c) = type;
2258 CONSTRUCTOR_ELTS (c) = vals;
2259
2260 recompute_constructor_flags (c);
2261
2262 return c;
2263}
2264
2265/* Build a CONSTRUCTOR node made of a single initializer, with the specified
2266 INDEX and VALUE. */
2267tree
2268build_constructor_single (tree type, tree index, tree value)
2269{
2270 vec<constructor_elt, va_gc> *v;
2271 constructor_elt elt = {.index: index, .value: value};
2272
2273 vec_alloc (v, nelems: 1);
2274 v->quick_push (obj: elt);
2275
2276 return build_constructor (type, vals: v);
2277}
2278
2279
2280/* Return a new CONSTRUCTOR node whose type is TYPE and whose values
2281 are in a list pointed to by VALS. */
2282tree
2283build_constructor_from_list (tree type, tree vals)
2284{
2285 tree t;
2286 vec<constructor_elt, va_gc> *v = NULL;
2287
2288 if (vals)
2289 {
2290 vec_alloc (v, nelems: list_length (vals));
2291 for (t = vals; t; t = TREE_CHAIN (t))
2292 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
2293 }
2294
2295 return build_constructor (type, vals: v);
2296}
2297
2298/* Return a new CONSTRUCTOR node whose type is TYPE and whose values
2299 are in a vector pointed to by VALS. Note that the TREE_PURPOSE
2300 fields in the constructor remain null. */
2301
2302tree
2303build_constructor_from_vec (tree type, const vec<tree, va_gc> *vals)
2304{
2305 vec<constructor_elt, va_gc> *v = NULL;
2306
2307 for (tree t : vals)
2308 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, t);
2309
2310 return build_constructor (type, vals: v);
2311}
2312
2313/* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
2314 of elements, provided as index/value pairs. */
2315
2316tree
2317build_constructor_va (tree type, int nelts, ...)
2318{
2319 vec<constructor_elt, va_gc> *v = NULL;
2320 va_list p;
2321
2322 va_start (p, nelts);
2323 vec_alloc (v, nelems: nelts);
2324 while (nelts--)
2325 {
2326 tree index = va_arg (p, tree);
2327 tree value = va_arg (p, tree);
2328 CONSTRUCTOR_APPEND_ELT (v, index, value);
2329 }
2330 va_end (p);
2331 return build_constructor (type, vals: v);
2332}
2333
2334/* Return a node of type TYPE for which TREE_CLOBBER_P is true. */
2335
2336tree
2337build_clobber (tree type, enum clobber_kind kind)
2338{
2339 tree clobber = build_constructor (type, NULL);
2340 TREE_THIS_VOLATILE (clobber) = true;
2341 CLOBBER_KIND (clobber) = kind;
2342 return clobber;
2343}
2344
2345/* Return a new FIXED_CST node whose type is TYPE and value is F. */
2346
2347tree
2348build_fixed (tree type, FIXED_VALUE_TYPE f)
2349{
2350 tree v;
2351 FIXED_VALUE_TYPE *fp;
2352
2353 v = make_node (code: FIXED_CST);
2354 fp = ggc_alloc<fixed_value> ();
2355 memcpy (dest: fp, src: &f, n: sizeof (FIXED_VALUE_TYPE));
2356
2357 TREE_TYPE (v) = type;
2358 TREE_FIXED_CST_PTR (v) = fp;
2359 return v;
2360}
2361
2362/* Return a new REAL_CST node whose type is TYPE and value is D. */
2363
2364tree
2365build_real (tree type, REAL_VALUE_TYPE d)
2366{
2367 tree v;
2368 int overflow = 0;
2369
2370 /* dconst{0,1,2,m1,half} are used in various places in
2371 the middle-end and optimizers, allow them here
2372 even for decimal floating point types as an exception
2373 by converting them to decimal. */
2374 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type))
2375 && (d.cl == rvc_normal || d.cl == rvc_zero)
2376 && !d.decimal)
2377 {
2378 if (memcmp (s1: &d, s2: &dconst1, n: sizeof (d)) == 0)
2379 decimal_real_from_string (&d, "1");
2380 else if (memcmp (s1: &d, s2: &dconst2, n: sizeof (d)) == 0)
2381 decimal_real_from_string (&d, "2");
2382 else if (memcmp (s1: &d, s2: &dconstm1, n: sizeof (d)) == 0)
2383 decimal_real_from_string (&d, "-1");
2384 else if (memcmp (s1: &d, s2: &dconsthalf, n: sizeof (d)) == 0)
2385 decimal_real_from_string (&d, "0.5");
2386 else if (memcmp (s1: &d, s2: &dconst0, n: sizeof (d)) == 0)
2387 {
2388 /* Make sure to give zero the minimum quantum exponent for
2389 the type (which corresponds to all bits zero). */
2390 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
2391 char buf[16];
2392 sprintf (s: buf, format: "0e%d", fmt->emin - fmt->p);
2393 decimal_real_from_string (&d, buf);
2394 }
2395 else
2396 gcc_unreachable ();
2397 }
2398
2399 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
2400 Consider doing it via real_convert now. */
2401
2402 v = make_node (code: REAL_CST);
2403 TREE_TYPE (v) = type;
2404 memcpy (TREE_REAL_CST_PTR (v), src: &d, n: sizeof (REAL_VALUE_TYPE));
2405 TREE_OVERFLOW (v) = overflow;
2406 return v;
2407}
2408
2409/* Like build_real, but first truncate D to the type. */
2410
2411tree
2412build_real_truncate (tree type, REAL_VALUE_TYPE d)
2413{
2414 return build_real (type, d: real_value_truncate (TYPE_MODE (type), d));
2415}
2416
2417/* Return a new REAL_CST node whose type is TYPE
2418 and whose value is the integer value of the INTEGER_CST node I. */
2419
2420REAL_VALUE_TYPE
2421real_value_from_int_cst (const_tree type, const_tree i)
2422{
2423 REAL_VALUE_TYPE d;
2424
2425 /* Clear all bits of the real value type so that we can later do
2426 bitwise comparisons to see if two values are the same. */
2427 memset (s: &d, c: 0, n: sizeof d);
2428
2429 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode, wi::to_wide (t: i),
2430 TYPE_SIGN (TREE_TYPE (i)));
2431 return d;
2432}
2433
2434/* Given a tree representing an integer constant I, return a tree
2435 representing the same value as a floating-point constant of type TYPE. */
2436
2437tree
2438build_real_from_int_cst (tree type, const_tree i)
2439{
2440 tree v;
2441 int overflow = TREE_OVERFLOW (i);
2442
2443 v = build_real (type, d: real_value_from_int_cst (type, i));
2444
2445 TREE_OVERFLOW (v) |= overflow;
2446 return v;
2447}
2448
2449/* Return a new REAL_CST node whose type is TYPE
2450 and whose value is the integer value I which has sign SGN. */
2451
2452tree
2453build_real_from_wide (tree type, const wide_int_ref &i, signop sgn)
2454{
2455 REAL_VALUE_TYPE d;
2456
2457 /* Clear all bits of the real value type so that we can later do
2458 bitwise comparisons to see if two values are the same. */
2459 memset (s: &d, c: 0, n: sizeof d);
2460
2461 real_from_integer (&d, TYPE_MODE (type), i, sgn);
2462 return build_real (type, d);
2463}
2464
2465/* Return a newly constructed STRING_CST node whose value is the LEN
2466 characters at STR when STR is nonnull, or all zeros otherwise.
2467 Note that for a C string literal, LEN should include the trailing NUL.
2468 The TREE_TYPE is not initialized. */
2469
2470tree
2471build_string (unsigned len, const char *str /*= NULL */)
2472{
2473 /* Do not waste bytes provided by padding of struct tree_string. */
2474 unsigned size = len + offsetof (struct tree_string, str) + 1;
2475
2476 record_node_allocation_statistics (code: STRING_CST, length: size);
2477
2478 tree s = (tree) ggc_internal_alloc (s: size);
2479
2480 memset (s: s, c: 0, n: sizeof (struct tree_typed));
2481 TREE_SET_CODE (s, STRING_CST);
2482 TREE_CONSTANT (s) = 1;
2483 TREE_STRING_LENGTH (s) = len;
2484 if (str)
2485 memcpy (dest: s->string.str, src: str, n: len);
2486 else
2487 memset (s: s->string.str, c: 0, n: len);
2488 s->string.str[len] = '\0';
2489
2490 return s;
2491}
2492
2493/* Return a newly constructed COMPLEX_CST node whose value is
2494 specified by the real and imaginary parts REAL and IMAG.
2495 Both REAL and IMAG should be constant nodes. TYPE, if specified,
2496 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
2497
2498tree
2499build_complex (tree type, tree real, tree imag)
2500{
2501 gcc_assert (CONSTANT_CLASS_P (real));
2502 gcc_assert (CONSTANT_CLASS_P (imag));
2503
2504 tree t = make_node (code: COMPLEX_CST);
2505
2506 TREE_REALPART (t) = real;
2507 TREE_IMAGPART (t) = imag;
2508 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
2509 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
2510 return t;
2511}
2512
2513/* Build a complex (inf +- 0i), such as for the result of cproj.
2514 TYPE is the complex tree type of the result. If NEG is true, the
2515 imaginary zero is negative. */
2516
2517tree
2518build_complex_inf (tree type, bool neg)
2519{
2520 REAL_VALUE_TYPE rzero = dconst0;
2521
2522 rzero.sign = neg;
2523 return build_complex (type, real: build_real (TREE_TYPE (type), d: dconstinf),
2524 imag: build_real (TREE_TYPE (type), d: rzero));
2525}
2526
2527/* Return the constant 1 in type TYPE. If TYPE has several elements, each
2528 element is set to 1. In particular, this is 1 + i for complex types. */
2529
2530tree
2531build_each_one_cst (tree type)
2532{
2533 if (TREE_CODE (type) == COMPLEX_TYPE)
2534 {
2535 tree scalar = build_one_cst (TREE_TYPE (type));
2536 return build_complex (type, real: scalar, imag: scalar);
2537 }
2538 else
2539 return build_one_cst (type);
2540}
2541
2542/* Return a constant of arithmetic type TYPE which is the
2543 multiplicative identity of the set TYPE. */
2544
2545tree
2546build_one_cst (tree type)
2547{
2548 switch (TREE_CODE (type))
2549 {
2550 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2551 case POINTER_TYPE: case REFERENCE_TYPE:
2552 case OFFSET_TYPE: case BITINT_TYPE:
2553 return build_int_cst (type, cst: 1);
2554
2555 case REAL_TYPE:
2556 return build_real (type, d: dconst1);
2557
2558 case FIXED_POINT_TYPE:
2559 /* We can only generate 1 for accum types. */
2560 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2561 return build_fixed (type, FCONST1 (TYPE_MODE (type)));
2562
2563 case VECTOR_TYPE:
2564 {
2565 tree scalar = build_one_cst (TREE_TYPE (type));
2566
2567 return build_vector_from_val (vectype: type, sc: scalar);
2568 }
2569
2570 case COMPLEX_TYPE:
2571 return build_complex (type,
2572 real: build_one_cst (TREE_TYPE (type)),
2573 imag: build_zero_cst (TREE_TYPE (type)));
2574
2575 default:
2576 gcc_unreachable ();
2577 }
2578}
2579
2580/* Return an integer of type TYPE containing all 1's in as much precision as
2581 it contains, or a complex or vector whose subparts are such integers. */
2582
2583tree
2584build_all_ones_cst (tree type)
2585{
2586 if (TREE_CODE (type) == COMPLEX_TYPE)
2587 {
2588 tree scalar = build_all_ones_cst (TREE_TYPE (type));
2589 return build_complex (type, real: scalar, imag: scalar);
2590 }
2591 else
2592 return build_minus_one_cst (type);
2593}
2594
2595/* Return a constant of arithmetic type TYPE which is the
2596 opposite of the multiplicative identity of the set TYPE. */
2597
2598tree
2599build_minus_one_cst (tree type)
2600{
2601 switch (TREE_CODE (type))
2602 {
2603 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2604 case POINTER_TYPE: case REFERENCE_TYPE:
2605 case OFFSET_TYPE: case BITINT_TYPE:
2606 return build_int_cst (type, cst: -1);
2607
2608 case REAL_TYPE:
2609 return build_real (type, d: dconstm1);
2610
2611 case FIXED_POINT_TYPE:
2612 /* We can only generate 1 for accum types. */
2613 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2614 return build_fixed (type,
2615 f: fixed_from_double_int (double_int_minus_one,
2616 SCALAR_TYPE_MODE (type)));
2617
2618 case VECTOR_TYPE:
2619 {
2620 tree scalar = build_minus_one_cst (TREE_TYPE (type));
2621
2622 return build_vector_from_val (vectype: type, sc: scalar);
2623 }
2624
2625 case COMPLEX_TYPE:
2626 return build_complex (type,
2627 real: build_minus_one_cst (TREE_TYPE (type)),
2628 imag: build_zero_cst (TREE_TYPE (type)));
2629
2630 default:
2631 gcc_unreachable ();
2632 }
2633}
2634
2635/* Build 0 constant of type TYPE. This is used by constructor folding
2636 and thus the constant should be represented in memory by
2637 zero(es). */
2638
2639tree
2640build_zero_cst (tree type)
2641{
2642 switch (TREE_CODE (type))
2643 {
2644 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2645 case POINTER_TYPE: case REFERENCE_TYPE:
2646 case OFFSET_TYPE: case NULLPTR_TYPE: case BITINT_TYPE:
2647 return build_int_cst (type, cst: 0);
2648
2649 case REAL_TYPE:
2650 return build_real (type, d: dconst0);
2651
2652 case FIXED_POINT_TYPE:
2653 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
2654
2655 case VECTOR_TYPE:
2656 {
2657 tree scalar = build_zero_cst (TREE_TYPE (type));
2658
2659 return build_vector_from_val (vectype: type, sc: scalar);
2660 }
2661
2662 case COMPLEX_TYPE:
2663 {
2664 tree zero = build_zero_cst (TREE_TYPE (type));
2665
2666 return build_complex (type, real: zero, imag: zero);
2667 }
2668
2669 default:
2670 if (!AGGREGATE_TYPE_P (type))
2671 return fold_convert (type, integer_zero_node);
2672 return build_constructor (type, NULL);
2673 }
2674}
2675
2676/* Build a constant of integer type TYPE, made of VALUE's bits replicated
2677 every WIDTH bits to fit TYPE's precision. */
2678
2679tree
2680build_replicated_int_cst (tree type, unsigned int width, HOST_WIDE_INT value)
2681{
2682 int n = ((TYPE_PRECISION (type) + HOST_BITS_PER_WIDE_INT - 1)
2683 / HOST_BITS_PER_WIDE_INT);
2684 unsigned HOST_WIDE_INT low, mask;
2685 HOST_WIDE_INT a[WIDE_INT_MAX_INL_ELTS];
2686 int i;
2687
2688 gcc_assert (n && n <= WIDE_INT_MAX_INL_ELTS);
2689
2690 if (width == HOST_BITS_PER_WIDE_INT)
2691 low = value;
2692 else
2693 {
2694 mask = (HOST_WIDE_INT_1U << width) - 1;
2695 low = (unsigned HOST_WIDE_INT) ~0 / mask * (value & mask);
2696 }
2697
2698 for (i = 0; i < n; i++)
2699 a[i] = low;
2700
2701 gcc_assert (TYPE_PRECISION (type) <= MAX_BITSIZE_MODE_ANY_INT);
2702 return wide_int_to_tree (type, value: wide_int::from_array (val: a, len: n,
2703 TYPE_PRECISION (type)));
2704}
2705
2706/* If floating-point type TYPE has an IEEE-style sign bit, return an
2707 unsigned constant in which only the sign bit is set. Return null
2708 otherwise. */
2709
2710tree
2711sign_mask_for (tree type)
2712{
2713 /* Avoid having to choose between a real-only sign and a pair of signs.
2714 This could be relaxed if the choice becomes obvious later. */
2715 if (TREE_CODE (type) == COMPLEX_TYPE)
2716 return NULL_TREE;
2717
2718 auto eltmode = as_a<scalar_float_mode> (m: element_mode (type));
2719 auto bits = REAL_MODE_FORMAT (eltmode)->ieee_bits;
2720 if (!bits || !pow2p_hwi (x: bits))
2721 return NULL_TREE;
2722
2723 tree inttype = unsigned_type_for (type);
2724 if (!inttype)
2725 return NULL_TREE;
2726
2727 auto mask = wi::set_bit_in_zero (bit: bits - 1, precision: bits);
2728 if (VECTOR_TYPE_P (inttype))
2729 {
2730 tree elt = wide_int_to_tree (TREE_TYPE (inttype), value: mask);
2731 return build_vector_from_val (vectype: inttype, sc: elt);
2732 }
2733 return wide_int_to_tree (type: inttype, value: mask);
2734}
2735
2736/* Build a BINFO with LEN language slots. */
2737
2738tree
2739make_tree_binfo (unsigned base_binfos MEM_STAT_DECL)
2740{
2741 tree t;
2742 size_t length = (offsetof (struct tree_binfo, base_binfos)
2743 + vec<tree, va_gc>::embedded_size (alloc: base_binfos));
2744
2745 record_node_allocation_statistics (code: TREE_BINFO, length);
2746
2747 t = ggc_alloc_tree_node_stat (s: length PASS_MEM_STAT);
2748
2749 memset (s: t, c: 0, offsetof (struct tree_binfo, base_binfos));
2750
2751 TREE_SET_CODE (t, TREE_BINFO);
2752
2753 BINFO_BASE_BINFOS (t)->embedded_init (alloc: base_binfos);
2754
2755 return t;
2756}
2757
2758/* Create a CASE_LABEL_EXPR tree node and return it. */
2759
2760tree
2761build_case_label (tree low_value, tree high_value, tree label_decl)
2762{
2763 tree t = make_node (code: CASE_LABEL_EXPR);
2764
2765 TREE_TYPE (t) = void_type_node;
2766 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
2767
2768 CASE_LOW (t) = low_value;
2769 CASE_HIGH (t) = high_value;
2770 CASE_LABEL (t) = label_decl;
2771 CASE_CHAIN (t) = NULL_TREE;
2772
2773 return t;
2774}
2775
2776/* Build a newly constructed INTEGER_CST node. LEN and EXT_LEN are the
2777 values of TREE_INT_CST_NUNITS and TREE_INT_CST_EXT_NUNITS respectively.
2778 The latter determines the length of the HOST_WIDE_INT vector. */
2779
2780tree
2781make_int_cst (int len, int ext_len MEM_STAT_DECL)
2782{
2783 tree t;
2784 int length = ((ext_len - 1) * sizeof (HOST_WIDE_INT)
2785 + sizeof (struct tree_int_cst));
2786
2787 gcc_assert (len);
2788 record_node_allocation_statistics (code: INTEGER_CST, length);
2789
2790 t = ggc_alloc_cleared_tree_node_stat (s: length PASS_MEM_STAT);
2791
2792 TREE_SET_CODE (t, INTEGER_CST);
2793 TREE_INT_CST_NUNITS (t) = len;
2794 TREE_INT_CST_EXT_NUNITS (t) = ext_len;
2795 TREE_CONSTANT (t) = 1;
2796
2797 return t;
2798}
2799
2800/* Build a newly constructed TREE_VEC node of length LEN. */
2801
2802tree
2803make_tree_vec (int len MEM_STAT_DECL)
2804{
2805 tree t;
2806 size_t length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2807
2808 record_node_allocation_statistics (code: TREE_VEC, length);
2809
2810 t = ggc_alloc_cleared_tree_node_stat (s: length PASS_MEM_STAT);
2811
2812 TREE_SET_CODE (t, TREE_VEC);
2813 TREE_VEC_LENGTH (t) = len;
2814
2815 return t;
2816}
2817
2818/* Grow a TREE_VEC node to new length LEN. */
2819
2820tree
2821grow_tree_vec (tree v, int len MEM_STAT_DECL)
2822{
2823 gcc_assert (TREE_CODE (v) == TREE_VEC);
2824
2825 int oldlen = TREE_VEC_LENGTH (v);
2826 gcc_assert (len > oldlen);
2827
2828 size_t oldlength = (oldlen - 1) * sizeof (tree) + sizeof (struct tree_vec);
2829 size_t length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2830
2831 record_node_allocation_statistics (code: TREE_VEC, length: length - oldlength);
2832
2833 v = (tree) ggc_realloc (v, length PASS_MEM_STAT);
2834
2835 TREE_VEC_LENGTH (v) = len;
2836
2837 return v;
2838}
2839
2840/* Return true if EXPR is the constant zero, whether it is integral, float or
2841 fixed, and scalar, complex or vector. */
2842
2843bool
2844zerop (const_tree expr)
2845{
2846 return (integer_zerop (expr)
2847 || real_zerop (expr)
2848 || fixed_zerop (expr));
2849}
2850
2851/* Return true if EXPR is the integer constant zero or a complex constant
2852 of zero, or a location wrapper for such a constant. */
2853
2854bool
2855integer_zerop (const_tree expr)
2856{
2857 STRIP_ANY_LOCATION_WRAPPER (expr);
2858
2859 switch (TREE_CODE (expr))
2860 {
2861 case INTEGER_CST:
2862 return wi::to_wide (t: expr) == 0;
2863 case COMPLEX_CST:
2864 return (integer_zerop (TREE_REALPART (expr))
2865 && integer_zerop (TREE_IMAGPART (expr)));
2866 case VECTOR_CST:
2867 return (VECTOR_CST_NPATTERNS (expr) == 1
2868 && VECTOR_CST_DUPLICATE_P (expr)
2869 && integer_zerop (VECTOR_CST_ENCODED_ELT (expr, 0)));
2870 default:
2871 return false;
2872 }
2873}
2874
2875/* Return true if EXPR is the integer constant one or the corresponding
2876 complex constant, or a location wrapper for such a constant. */
2877
2878bool
2879integer_onep (const_tree expr)
2880{
2881 STRIP_ANY_LOCATION_WRAPPER (expr);
2882
2883 switch (TREE_CODE (expr))
2884 {
2885 case INTEGER_CST:
2886 return wi::eq_p (x: wi::to_widest (t: expr), y: 1);
2887 case COMPLEX_CST:
2888 return (integer_onep (TREE_REALPART (expr))
2889 && integer_zerop (TREE_IMAGPART (expr)));
2890 case VECTOR_CST:
2891 return (VECTOR_CST_NPATTERNS (expr) == 1
2892 && VECTOR_CST_DUPLICATE_P (expr)
2893 && integer_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
2894 default:
2895 return false;
2896 }
2897}
2898
2899/* Return true if EXPR is the integer constant one. For complex and vector,
2900 return true if every piece is the integer constant one.
2901 Also return true for location wrappers for such a constant. */
2902
2903bool
2904integer_each_onep (const_tree expr)
2905{
2906 STRIP_ANY_LOCATION_WRAPPER (expr);
2907
2908 if (TREE_CODE (expr) == COMPLEX_CST)
2909 return (integer_onep (TREE_REALPART (expr))
2910 && integer_onep (TREE_IMAGPART (expr)));
2911 else
2912 return integer_onep (expr);
2913}
2914
2915/* Return true if EXPR is an integer containing all 1's in as much precision
2916 as it contains, or a complex or vector whose subparts are such integers,
2917 or a location wrapper for such a constant. */
2918
2919bool
2920integer_all_onesp (const_tree expr)
2921{
2922 STRIP_ANY_LOCATION_WRAPPER (expr);
2923
2924 if (TREE_CODE (expr) == COMPLEX_CST
2925 && integer_all_onesp (TREE_REALPART (expr))
2926 && integer_all_onesp (TREE_IMAGPART (expr)))
2927 return true;
2928
2929 else if (TREE_CODE (expr) == VECTOR_CST)
2930 return (VECTOR_CST_NPATTERNS (expr) == 1
2931 && VECTOR_CST_DUPLICATE_P (expr)
2932 && integer_all_onesp (VECTOR_CST_ENCODED_ELT (expr, 0)));
2933
2934 else if (TREE_CODE (expr) != INTEGER_CST)
2935 return false;
2936
2937 return (wi::max_value (TYPE_PRECISION (TREE_TYPE (expr)), UNSIGNED)
2938 == wi::to_wide (t: expr));
2939}
2940
2941/* Return true if EXPR is the integer constant minus one, or a location
2942 wrapper for such a constant. */
2943
2944bool
2945integer_minus_onep (const_tree expr)
2946{
2947 STRIP_ANY_LOCATION_WRAPPER (expr);
2948
2949 if (TREE_CODE (expr) == COMPLEX_CST)
2950 return (integer_all_onesp (TREE_REALPART (expr))
2951 && integer_zerop (TREE_IMAGPART (expr)));
2952 else
2953 return integer_all_onesp (expr);
2954}
2955
2956/* Return true if EXPR is an integer constant that is a power of 2 (i.e., has
2957 only one bit on), or a location wrapper for such a constant. */
2958
2959bool
2960integer_pow2p (const_tree expr)
2961{
2962 STRIP_ANY_LOCATION_WRAPPER (expr);
2963
2964 if (TREE_CODE (expr) == COMPLEX_CST
2965 && integer_pow2p (TREE_REALPART (expr))
2966 && integer_zerop (TREE_IMAGPART (expr)))
2967 return true;
2968
2969 if (TREE_CODE (expr) != INTEGER_CST)
2970 return false;
2971
2972 return wi::popcount (wi::to_wide (t: expr)) == 1;
2973}
2974
2975/* Return true if EXPR is an integer constant other than zero or a
2976 complex constant other than zero, or a location wrapper for such a
2977 constant. */
2978
2979bool
2980integer_nonzerop (const_tree expr)
2981{
2982 STRIP_ANY_LOCATION_WRAPPER (expr);
2983
2984 return ((TREE_CODE (expr) == INTEGER_CST
2985 && wi::to_wide (t: expr) != 0)
2986 || (TREE_CODE (expr) == COMPLEX_CST
2987 && (integer_nonzerop (TREE_REALPART (expr))
2988 || integer_nonzerop (TREE_IMAGPART (expr)))));
2989}
2990
2991/* Return true if EXPR is the integer constant one. For vector,
2992 return true if every piece is the integer constant minus one
2993 (representing the value TRUE).
2994 Also return true for location wrappers for such a constant. */
2995
2996bool
2997integer_truep (const_tree expr)
2998{
2999 STRIP_ANY_LOCATION_WRAPPER (expr);
3000
3001 if (TREE_CODE (expr) == VECTOR_CST)
3002 return integer_all_onesp (expr);
3003 return integer_onep (expr);
3004}
3005
3006/* Return true if EXPR is the fixed-point constant zero, or a location wrapper
3007 for such a constant. */
3008
3009bool
3010fixed_zerop (const_tree expr)
3011{
3012 STRIP_ANY_LOCATION_WRAPPER (expr);
3013
3014 return (TREE_CODE (expr) == FIXED_CST
3015 && TREE_FIXED_CST (expr).data.is_zero ());
3016}
3017
3018/* Return the power of two represented by a tree node known to be a
3019 power of two. */
3020
3021int
3022tree_log2 (const_tree expr)
3023{
3024 if (TREE_CODE (expr) == COMPLEX_CST)
3025 return tree_log2 (TREE_REALPART (expr));
3026
3027 return wi::exact_log2 (wi::to_wide (t: expr));
3028}
3029
3030/* Similar, but return the largest integer Y such that 2 ** Y is less
3031 than or equal to EXPR. */
3032
3033int
3034tree_floor_log2 (const_tree expr)
3035{
3036 if (TREE_CODE (expr) == COMPLEX_CST)
3037 return tree_log2 (TREE_REALPART (expr));
3038
3039 return wi::floor_log2 (wi::to_wide (t: expr));
3040}
3041
3042/* Return number of known trailing zero bits in EXPR, or, if the value of
3043 EXPR is known to be zero, the precision of it's type. */
3044
3045unsigned int
3046tree_ctz (const_tree expr)
3047{
3048 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
3049 && !POINTER_TYPE_P (TREE_TYPE (expr)))
3050 return 0;
3051
3052 unsigned int ret1, ret2, prec = TYPE_PRECISION (TREE_TYPE (expr));
3053 switch (TREE_CODE (expr))
3054 {
3055 case INTEGER_CST:
3056 ret1 = wi::ctz (wi::to_wide (t: expr));
3057 return MIN (ret1, prec);
3058 case SSA_NAME:
3059 ret1 = wi::ctz (get_nonzero_bits (expr));
3060 return MIN (ret1, prec);
3061 case PLUS_EXPR:
3062 case MINUS_EXPR:
3063 case BIT_IOR_EXPR:
3064 case BIT_XOR_EXPR:
3065 case MIN_EXPR:
3066 case MAX_EXPR:
3067 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3068 if (ret1 == 0)
3069 return ret1;
3070 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
3071 return MIN (ret1, ret2);
3072 case POINTER_PLUS_EXPR:
3073 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3074 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
3075 /* Second operand is sizetype, which could be in theory
3076 wider than pointer's precision. Make sure we never
3077 return more than prec. */
3078 ret2 = MIN (ret2, prec);
3079 return MIN (ret1, ret2);
3080 case BIT_AND_EXPR:
3081 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3082 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
3083 return MAX (ret1, ret2);
3084 case MULT_EXPR:
3085 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3086 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
3087 return MIN (ret1 + ret2, prec);
3088 case LSHIFT_EXPR:
3089 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3090 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
3091 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
3092 {
3093 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
3094 return MIN (ret1 + ret2, prec);
3095 }
3096 return ret1;
3097 case RSHIFT_EXPR:
3098 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
3099 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
3100 {
3101 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3102 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
3103 if (ret1 > ret2)
3104 return ret1 - ret2;
3105 }
3106 return 0;
3107 case TRUNC_DIV_EXPR:
3108 case CEIL_DIV_EXPR:
3109 case FLOOR_DIV_EXPR:
3110 case ROUND_DIV_EXPR:
3111 case EXACT_DIV_EXPR:
3112 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
3113 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) == 1)
3114 {
3115 int l = tree_log2 (TREE_OPERAND (expr, 1));
3116 if (l >= 0)
3117 {
3118 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3119 ret2 = l;
3120 if (ret1 > ret2)
3121 return ret1 - ret2;
3122 }
3123 }
3124 return 0;
3125 CASE_CONVERT:
3126 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3127 if (ret1 && ret1 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
3128 ret1 = prec;
3129 return MIN (ret1, prec);
3130 case SAVE_EXPR:
3131 return tree_ctz (TREE_OPERAND (expr, 0));
3132 case COND_EXPR:
3133 ret1 = tree_ctz (TREE_OPERAND (expr, 1));
3134 if (ret1 == 0)
3135 return 0;
3136 ret2 = tree_ctz (TREE_OPERAND (expr, 2));
3137 return MIN (ret1, ret2);
3138 case COMPOUND_EXPR:
3139 return tree_ctz (TREE_OPERAND (expr, 1));
3140 case ADDR_EXPR:
3141 ret1 = get_pointer_alignment (CONST_CAST_TREE (expr));
3142 if (ret1 > BITS_PER_UNIT)
3143 {
3144 ret1 = ctz_hwi (x: ret1 / BITS_PER_UNIT);
3145 return MIN (ret1, prec);
3146 }
3147 return 0;
3148 default:
3149 return 0;
3150 }
3151}
3152
3153/* Return true if EXPR is the real constant zero. Trailing zeroes matter for
3154 decimal float constants, so don't return true for them.
3155 Also return true for location wrappers around such a constant. */
3156
3157bool
3158real_zerop (const_tree expr)
3159{
3160 STRIP_ANY_LOCATION_WRAPPER (expr);
3161
3162 switch (TREE_CODE (expr))
3163 {
3164 case REAL_CST:
3165 return real_equal (&TREE_REAL_CST (expr), &dconst0)
3166 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
3167 case COMPLEX_CST:
3168 return real_zerop (TREE_REALPART (expr))
3169 && real_zerop (TREE_IMAGPART (expr));
3170 case VECTOR_CST:
3171 {
3172 /* Don't simply check for a duplicate because the predicate
3173 accepts both +0.0 and -0.0. */
3174 unsigned count = vector_cst_encoded_nelts (t: expr);
3175 for (unsigned int i = 0; i < count; ++i)
3176 if (!real_zerop (VECTOR_CST_ENCODED_ELT (expr, i)))
3177 return false;
3178 return true;
3179 }
3180 default:
3181 return false;
3182 }
3183}
3184
3185/* Return true if EXPR is the real constant one in real or complex form.
3186 Trailing zeroes matter for decimal float constants, so don't return
3187 true for them.
3188 Also return true for location wrappers around such a constant. */
3189
3190bool
3191real_onep (const_tree expr)
3192{
3193 STRIP_ANY_LOCATION_WRAPPER (expr);
3194
3195 switch (TREE_CODE (expr))
3196 {
3197 case REAL_CST:
3198 return real_equal (&TREE_REAL_CST (expr), &dconst1)
3199 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
3200 case COMPLEX_CST:
3201 return real_onep (TREE_REALPART (expr))
3202 && real_zerop (TREE_IMAGPART (expr));
3203 case VECTOR_CST:
3204 return (VECTOR_CST_NPATTERNS (expr) == 1
3205 && VECTOR_CST_DUPLICATE_P (expr)
3206 && real_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
3207 default:
3208 return false;
3209 }
3210}
3211
3212/* Return true if EXPR is the real constant minus one. Trailing zeroes
3213 matter for decimal float constants, so don't return true for them.
3214 Also return true for location wrappers around such a constant. */
3215
3216bool
3217real_minus_onep (const_tree expr)
3218{
3219 STRIP_ANY_LOCATION_WRAPPER (expr);
3220
3221 switch (TREE_CODE (expr))
3222 {
3223 case REAL_CST:
3224 return real_equal (&TREE_REAL_CST (expr), &dconstm1)
3225 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
3226 case COMPLEX_CST:
3227 return real_minus_onep (TREE_REALPART (expr))
3228 && real_zerop (TREE_IMAGPART (expr));
3229 case VECTOR_CST:
3230 return (VECTOR_CST_NPATTERNS (expr) == 1
3231 && VECTOR_CST_DUPLICATE_P (expr)
3232 && real_minus_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
3233 default:
3234 return false;
3235 }
3236}
3237
3238/* Return true if T could be a floating point zero. */
3239
3240bool
3241real_maybe_zerop (const_tree expr)
3242{
3243 switch (TREE_CODE (expr))
3244 {
3245 case REAL_CST:
3246 /* Can't use real_zerop here, as it always returns false for decimal
3247 floats. And can't use TREE_REAL_CST (expr).cl == rvc_zero
3248 either, as decimal zeros are rvc_normal. */
3249 return real_equal (&TREE_REAL_CST (expr), &dconst0);
3250 case COMPLEX_CST:
3251 return (real_maybe_zerop (TREE_REALPART (expr))
3252 || real_maybe_zerop (TREE_IMAGPART (expr)));
3253 case VECTOR_CST:
3254 {
3255 unsigned count = vector_cst_encoded_nelts (t: expr);
3256 for (unsigned int i = 0; i < count; ++i)
3257 if (real_maybe_zerop (VECTOR_CST_ENCODED_ELT (expr, i)))
3258 return true;
3259 return false;
3260 }
3261 default:
3262 /* Perhaps for SSA_NAMEs we could query frange. */
3263 return true;
3264 }
3265}
3266
3267/* True if EXP is a constant or a cast of a constant. */
3268
3269bool
3270really_constant_p (const_tree exp)
3271{
3272 /* This is not quite the same as STRIP_NOPS. It does more. */
3273 while (CONVERT_EXPR_P (exp)
3274 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3275 exp = TREE_OPERAND (exp, 0);
3276 return TREE_CONSTANT (exp);
3277}
3278
3279/* Return true if T holds a polynomial pointer difference, storing it in
3280 *VALUE if so. A true return means that T's precision is no greater
3281 than 64 bits, which is the largest address space we support, so *VALUE
3282 never loses precision. However, the signedness of the result does
3283 not necessarily match the signedness of T: sometimes an unsigned type
3284 like sizetype is used to encode a value that is actually negative. */
3285
3286bool
3287ptrdiff_tree_p (const_tree t, poly_int64 *value)
3288{
3289 if (!t)
3290 return false;
3291 if (TREE_CODE (t) == INTEGER_CST)
3292 {
3293 if (!cst_and_fits_in_hwi (x: t))
3294 return false;
3295 *value = int_cst_value (t);
3296 return true;
3297 }
3298 if (POLY_INT_CST_P (t))
3299 {
3300 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
3301 if (!cst_and_fits_in_hwi (POLY_INT_CST_COEFF (t, i)))
3302 return false;
3303 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
3304 value->coeffs[i] = int_cst_value (POLY_INT_CST_COEFF (t, i));
3305 return true;
3306 }
3307 return false;
3308}
3309
3310poly_int64
3311tree_to_poly_int64 (const_tree t)
3312{
3313 gcc_assert (tree_fits_poly_int64_p (t));
3314 if (POLY_INT_CST_P (t))
3315 return poly_int_cst_value (x: t).force_shwi ();
3316 return TREE_INT_CST_LOW (t);
3317}
3318
3319poly_uint64
3320tree_to_poly_uint64 (const_tree t)
3321{
3322 gcc_assert (tree_fits_poly_uint64_p (t));
3323 if (POLY_INT_CST_P (t))
3324 return poly_int_cst_value (x: t).force_uhwi ();
3325 return TREE_INT_CST_LOW (t);
3326}
3327
3328/* Return first list element whose TREE_VALUE is ELEM.
3329 Return 0 if ELEM is not in LIST. */
3330
3331tree
3332value_member (tree elem, tree list)
3333{
3334 while (list)
3335 {
3336 if (elem == TREE_VALUE (list))
3337 return list;
3338 list = TREE_CHAIN (list);
3339 }
3340 return NULL_TREE;
3341}
3342
3343/* Return first list element whose TREE_PURPOSE is ELEM.
3344 Return 0 if ELEM is not in LIST. */
3345
3346tree
3347purpose_member (const_tree elem, tree list)
3348{
3349 while (list)
3350 {
3351 if (elem == TREE_PURPOSE (list))
3352 return list;
3353 list = TREE_CHAIN (list);
3354 }
3355 return NULL_TREE;
3356}
3357
3358/* Return true if ELEM is in V. */
3359
3360bool
3361vec_member (const_tree elem, vec<tree, va_gc> *v)
3362{
3363 unsigned ix;
3364 tree t;
3365 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
3366 if (elem == t)
3367 return true;
3368 return false;
3369}
3370
3371/* Returns element number IDX (zero-origin) of chain CHAIN, or
3372 NULL_TREE. */
3373
3374tree
3375chain_index (int idx, tree chain)
3376{
3377 for (; chain && idx > 0; --idx)
3378 chain = TREE_CHAIN (chain);
3379 return chain;
3380}
3381
3382/* Return true if ELEM is part of the chain CHAIN. */
3383
3384bool
3385chain_member (const_tree elem, const_tree chain)
3386{
3387 while (chain)
3388 {
3389 if (elem == chain)
3390 return true;
3391 chain = DECL_CHAIN (chain);
3392 }
3393
3394 return false;
3395}
3396
3397/* Return the length of a chain of nodes chained through TREE_CHAIN.
3398 We expect a null pointer to mark the end of the chain.
3399 This is the Lisp primitive `length'. */
3400
3401int
3402list_length (const_tree t)
3403{
3404 const_tree p = t;
3405#ifdef ENABLE_TREE_CHECKING
3406 const_tree q = t;
3407#endif
3408 int len = 0;
3409
3410 while (p)
3411 {
3412 p = TREE_CHAIN (p);
3413#ifdef ENABLE_TREE_CHECKING
3414 if (len % 2)
3415 q = TREE_CHAIN (q);
3416 gcc_assert (p != q);
3417#endif
3418 len++;
3419 }
3420
3421 return len;
3422}
3423
3424/* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
3425 UNION_TYPE TYPE, or NULL_TREE if none. */
3426
3427tree
3428first_field (const_tree type)
3429{
3430 tree t = TYPE_FIELDS (type);
3431 while (t && TREE_CODE (t) != FIELD_DECL)
3432 t = TREE_CHAIN (t);
3433 return t;
3434}
3435
3436/* Returns the last FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
3437 UNION_TYPE TYPE, or NULL_TREE if none. */
3438
3439tree
3440last_field (const_tree type)
3441{
3442 tree last = NULL_TREE;
3443
3444 for (tree fld = TYPE_FIELDS (type); fld; fld = TREE_CHAIN (fld))
3445 {
3446 if (TREE_CODE (fld) != FIELD_DECL)
3447 continue;
3448
3449 last = fld;
3450 }
3451
3452 return last;
3453}
3454
3455/* Concatenate two chains of nodes (chained through TREE_CHAIN)
3456 by modifying the last node in chain 1 to point to chain 2.
3457 This is the Lisp primitive `nconc'. */
3458
3459tree
3460chainon (tree op1, tree op2)
3461{
3462 tree t1;
3463
3464 if (!op1)
3465 return op2;
3466 if (!op2)
3467 return op1;
3468
3469 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
3470 continue;
3471 TREE_CHAIN (t1) = op2;
3472
3473#ifdef ENABLE_TREE_CHECKING
3474 {
3475 tree t2;
3476 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
3477 gcc_assert (t2 != t1);
3478 }
3479#endif
3480
3481 return op1;
3482}
3483
3484/* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
3485
3486tree
3487tree_last (tree chain)
3488{
3489 tree next;
3490 if (chain)
3491 while ((next = TREE_CHAIN (chain)))
3492 chain = next;
3493 return chain;
3494}
3495
3496/* Reverse the order of elements in the chain T,
3497 and return the new head of the chain (old last element). */
3498
3499tree
3500nreverse (tree t)
3501{
3502 tree prev = 0, decl, next;
3503 for (decl = t; decl; decl = next)
3504 {
3505 /* We shouldn't be using this function to reverse BLOCK chains; we
3506 have blocks_nreverse for that. */
3507 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
3508 next = TREE_CHAIN (decl);
3509 TREE_CHAIN (decl) = prev;
3510 prev = decl;
3511 }
3512 return prev;
3513}
3514
3515/* Return a newly created TREE_LIST node whose
3516 purpose and value fields are PARM and VALUE. */
3517
3518tree
3519build_tree_list (tree parm, tree value MEM_STAT_DECL)
3520{
3521 tree t = make_node (code: TREE_LIST PASS_MEM_STAT);
3522 TREE_PURPOSE (t) = parm;
3523 TREE_VALUE (t) = value;
3524 return t;
3525}
3526
3527/* Build a chain of TREE_LIST nodes from a vector. */
3528
3529tree
3530build_tree_list_vec (const vec<tree, va_gc> *vec MEM_STAT_DECL)
3531{
3532 tree ret = NULL_TREE;
3533 tree *pp = &ret;
3534 unsigned int i;
3535 tree t;
3536 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
3537 {
3538 *pp = build_tree_list (NULL, value: t PASS_MEM_STAT);
3539 pp = &TREE_CHAIN (*pp);
3540 }
3541 return ret;
3542}
3543
3544/* Return a newly created TREE_LIST node whose
3545 purpose and value fields are PURPOSE and VALUE
3546 and whose TREE_CHAIN is CHAIN. */
3547
3548tree
3549tree_cons (tree purpose, tree value, tree chain MEM_STAT_DECL)
3550{
3551 tree node;
3552
3553 node = ggc_alloc_tree_node_stat (s: sizeof (struct tree_list) PASS_MEM_STAT);
3554 memset (s: node, c: 0, n: sizeof (struct tree_common));
3555
3556 record_node_allocation_statistics (code: TREE_LIST, length: sizeof (struct tree_list));
3557
3558 TREE_SET_CODE (node, TREE_LIST);
3559 TREE_CHAIN (node) = chain;
3560 TREE_PURPOSE (node) = purpose;
3561 TREE_VALUE (node) = value;
3562 return node;
3563}
3564
3565/* Return the values of the elements of a CONSTRUCTOR as a vector of
3566 trees. */
3567
3568vec<tree, va_gc> *
3569ctor_to_vec (tree ctor)
3570{
3571 vec<tree, va_gc> *vec;
3572 vec_alloc (v&: vec, CONSTRUCTOR_NELTS (ctor));
3573 unsigned int ix;
3574 tree val;
3575
3576 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
3577 vec->quick_push (obj: val);
3578
3579 return vec;
3580}
3581
3582/* Return the size nominally occupied by an object of type TYPE
3583 when it resides in memory. The value is measured in units of bytes,
3584 and its data type is that normally used for type sizes
3585 (which is the first type created by make_signed_type or
3586 make_unsigned_type). */
3587
3588tree
3589size_in_bytes_loc (location_t loc, const_tree type)
3590{
3591 tree t;
3592
3593 if (type == error_mark_node)
3594 return integer_zero_node;
3595
3596 type = TYPE_MAIN_VARIANT (type);
3597 t = TYPE_SIZE_UNIT (type);
3598
3599 if (t == 0)
3600 {
3601 lang_hooks.types.incomplete_type_error (loc, NULL_TREE, type);
3602 return size_zero_node;
3603 }
3604
3605 return t;
3606}
3607
3608/* Return the size of TYPE (in bytes) as a wide integer
3609 or return -1 if the size can vary or is larger than an integer. */
3610
3611HOST_WIDE_INT
3612int_size_in_bytes (const_tree type)
3613{
3614 tree t;
3615
3616 if (type == error_mark_node)
3617 return 0;
3618
3619 type = TYPE_MAIN_VARIANT (type);
3620 t = TYPE_SIZE_UNIT (type);
3621
3622 if (t && tree_fits_uhwi_p (t))
3623 return TREE_INT_CST_LOW (t);
3624 else
3625 return -1;
3626}
3627
3628/* Return the maximum size of TYPE (in bytes) as a wide integer
3629 or return -1 if the size can vary or is larger than an integer. */
3630
3631HOST_WIDE_INT
3632max_int_size_in_bytes (const_tree type)
3633{
3634 HOST_WIDE_INT size = -1;
3635 tree size_tree;
3636
3637 /* If this is an array type, check for a possible MAX_SIZE attached. */
3638
3639 if (TREE_CODE (type) == ARRAY_TYPE)
3640 {
3641 size_tree = TYPE_ARRAY_MAX_SIZE (type);
3642
3643 if (size_tree && tree_fits_uhwi_p (size_tree))
3644 size = tree_to_uhwi (size_tree);
3645 }
3646
3647 /* If we still haven't been able to get a size, see if the language
3648 can compute a maximum size. */
3649
3650 if (size == -1)
3651 {
3652 size_tree = lang_hooks.types.max_size (type);
3653
3654 if (size_tree && tree_fits_uhwi_p (size_tree))
3655 size = tree_to_uhwi (size_tree);
3656 }
3657
3658 return size;
3659}
3660
3661/* Return the bit position of FIELD, in bits from the start of the record.
3662 This is a tree of type bitsizetype. */
3663
3664tree
3665bit_position (const_tree field)
3666{
3667 return bit_from_pos (DECL_FIELD_OFFSET (field),
3668 DECL_FIELD_BIT_OFFSET (field));
3669}
3670
3671/* Return the byte position of FIELD, in bytes from the start of the record.
3672 This is a tree of type sizetype. */
3673
3674tree
3675byte_position (const_tree field)
3676{
3677 return byte_from_pos (DECL_FIELD_OFFSET (field),
3678 DECL_FIELD_BIT_OFFSET (field));
3679}
3680
3681/* Likewise, but return as an integer. It must be representable in
3682 that way (since it could be a signed value, we don't have the
3683 option of returning -1 like int_size_in_byte can. */
3684
3685HOST_WIDE_INT
3686int_byte_position (const_tree field)
3687{
3688 return tree_to_shwi (byte_position (field));
3689}
3690
3691/* Return, as a tree node, the number of elements for TYPE (which is an
3692 ARRAY_TYPE) minus one. This counts only elements of the top array. */
3693
3694tree
3695array_type_nelts (const_tree type)
3696{
3697 tree index_type, min, max;
3698
3699 /* If they did it with unspecified bounds, then we should have already
3700 given an error about it before we got here. */
3701 if (! TYPE_DOMAIN (type))
3702 return error_mark_node;
3703
3704 index_type = TYPE_DOMAIN (type);
3705 min = TYPE_MIN_VALUE (index_type);
3706 max = TYPE_MAX_VALUE (index_type);
3707
3708 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
3709 if (!max)
3710 {
3711 /* zero sized arrays are represented from C FE as complete types with
3712 NULL TYPE_MAX_VALUE and zero TYPE_SIZE, while C++ FE represents
3713 them as min 0, max -1. */
3714 if (COMPLETE_TYPE_P (type)
3715 && integer_zerop (TYPE_SIZE (type))
3716 && integer_zerop (expr: min))
3717 return build_int_cst (TREE_TYPE (min), cst: -1);
3718
3719 return error_mark_node;
3720 }
3721
3722 return (integer_zerop (expr: min)
3723 ? max
3724 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
3725}
3726
3727/* If arg is static -- a reference to an object in static storage -- then
3728 return the object. This is not the same as the C meaning of `static'.
3729 If arg isn't static, return NULL. */
3730
3731tree
3732staticp (tree arg)
3733{
3734 switch (TREE_CODE (arg))
3735 {
3736 case FUNCTION_DECL:
3737 /* Nested functions are static, even though taking their address will
3738 involve a trampoline as we unnest the nested function and create
3739 the trampoline on the tree level. */
3740 return arg;
3741
3742 case VAR_DECL:
3743 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3744 && ! DECL_THREAD_LOCAL_P (arg)
3745 && ! DECL_DLLIMPORT_P (arg)
3746 ? arg : NULL);
3747
3748 case CONST_DECL:
3749 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3750 ? arg : NULL);
3751
3752 case CONSTRUCTOR:
3753 return TREE_STATIC (arg) ? arg : NULL;
3754
3755 case LABEL_DECL:
3756 case STRING_CST:
3757 return arg;
3758
3759 case COMPONENT_REF:
3760 /* If the thing being referenced is not a field, then it is
3761 something language specific. */
3762 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
3763
3764 /* If we are referencing a bitfield, we can't evaluate an
3765 ADDR_EXPR at compile time and so it isn't a constant. */
3766 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
3767 return NULL;
3768
3769 return staticp (TREE_OPERAND (arg, 0));
3770
3771 case BIT_FIELD_REF:
3772 return NULL;
3773
3774 case INDIRECT_REF:
3775 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
3776
3777 case ARRAY_REF:
3778 case ARRAY_RANGE_REF:
3779 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
3780 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
3781 return staticp (TREE_OPERAND (arg, 0));
3782 else
3783 return NULL;
3784
3785 case COMPOUND_LITERAL_EXPR:
3786 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
3787
3788 default:
3789 return NULL;
3790 }
3791}
3792
3793
3794
3795
3796/* Return whether OP is a DECL whose address is function-invariant. */
3797
3798bool
3799decl_address_invariant_p (const_tree op)
3800{
3801 /* The conditions below are slightly less strict than the one in
3802 staticp. */
3803
3804 switch (TREE_CODE (op))
3805 {
3806 case PARM_DECL:
3807 case RESULT_DECL:
3808 case LABEL_DECL:
3809 case FUNCTION_DECL:
3810 return true;
3811
3812 case VAR_DECL:
3813 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3814 || DECL_THREAD_LOCAL_P (op)
3815 || DECL_CONTEXT (op) == current_function_decl
3816 || decl_function_context (op) == current_function_decl)
3817 return true;
3818 break;
3819
3820 case CONST_DECL:
3821 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3822 || decl_function_context (op) == current_function_decl)
3823 return true;
3824 break;
3825
3826 default:
3827 break;
3828 }
3829
3830 return false;
3831}
3832
3833/* Return whether OP is a DECL whose address is interprocedural-invariant. */
3834
3835bool
3836decl_address_ip_invariant_p (const_tree op)
3837{
3838 /* The conditions below are slightly less strict than the one in
3839 staticp. */
3840
3841 switch (TREE_CODE (op))
3842 {
3843 case LABEL_DECL:
3844 case FUNCTION_DECL:
3845 case STRING_CST:
3846 return true;
3847
3848 case VAR_DECL:
3849 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
3850 && !DECL_DLLIMPORT_P (op))
3851 || DECL_THREAD_LOCAL_P (op))
3852 return true;
3853 break;
3854
3855 case CONST_DECL:
3856 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
3857 return true;
3858 break;
3859
3860 default:
3861 break;
3862 }
3863
3864 return false;
3865}
3866
3867
3868/* Return true if T is function-invariant (internal function, does
3869 not handle arithmetic; that's handled in skip_simple_arithmetic and
3870 tree_invariant_p). */
3871
3872static bool
3873tree_invariant_p_1 (tree t)
3874{
3875 tree op;
3876
3877 if (TREE_CONSTANT (t)
3878 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
3879 return true;
3880
3881 switch (TREE_CODE (t))
3882 {
3883 case SAVE_EXPR:
3884 return true;
3885
3886 case ADDR_EXPR:
3887 op = TREE_OPERAND (t, 0);
3888 while (handled_component_p (t: op))
3889 {
3890 switch (TREE_CODE (op))
3891 {
3892 case ARRAY_REF:
3893 case ARRAY_RANGE_REF:
3894 if (!tree_invariant_p (TREE_OPERAND (op, 1))
3895 || TREE_OPERAND (op, 2) != NULL_TREE
3896 || TREE_OPERAND (op, 3) != NULL_TREE)
3897 return false;
3898 break;
3899
3900 case COMPONENT_REF:
3901 if (TREE_OPERAND (op, 2) != NULL_TREE)
3902 return false;
3903 break;
3904
3905 default:;
3906 }
3907 op = TREE_OPERAND (op, 0);
3908 }
3909
3910 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
3911
3912 default:
3913 break;
3914 }
3915
3916 return false;
3917}
3918
3919/* Return true if T is function-invariant. */
3920
3921bool
3922tree_invariant_p (tree t)
3923{
3924 tree inner = skip_simple_arithmetic (t);
3925 return tree_invariant_p_1 (t: inner);
3926}
3927
3928/* Wrap a SAVE_EXPR around EXPR, if appropriate.
3929 Do this to any expression which may be used in more than one place,
3930 but must be evaluated only once.
3931
3932 Normally, expand_expr would reevaluate the expression each time.
3933 Calling save_expr produces something that is evaluated and recorded
3934 the first time expand_expr is called on it. Subsequent calls to
3935 expand_expr just reuse the recorded value.
3936
3937 The call to expand_expr that generates code that actually computes
3938 the value is the first call *at compile time*. Subsequent calls
3939 *at compile time* generate code to use the saved value.
3940 This produces correct result provided that *at run time* control
3941 always flows through the insns made by the first expand_expr
3942 before reaching the other places where the save_expr was evaluated.
3943 You, the caller of save_expr, must make sure this is so.
3944
3945 Constants, and certain read-only nodes, are returned with no
3946 SAVE_EXPR because that is safe. Expressions containing placeholders
3947 are not touched; see tree.def for an explanation of what these
3948 are used for. */
3949
3950tree
3951save_expr (tree expr)
3952{
3953 tree inner;
3954
3955 /* If the tree evaluates to a constant, then we don't want to hide that
3956 fact (i.e. this allows further folding, and direct checks for constants).
3957 However, a read-only object that has side effects cannot be bypassed.
3958 Since it is no problem to reevaluate literals, we just return the
3959 literal node. */
3960 inner = skip_simple_arithmetic (expr);
3961 if (TREE_CODE (inner) == ERROR_MARK)
3962 return inner;
3963
3964 if (tree_invariant_p_1 (t: inner))
3965 return expr;
3966
3967 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
3968 it means that the size or offset of some field of an object depends on
3969 the value within another field.
3970
3971 Note that it must not be the case that EXPR contains both a PLACEHOLDER_EXPR
3972 and some variable since it would then need to be both evaluated once and
3973 evaluated more than once. Front-ends must assure this case cannot
3974 happen by surrounding any such subexpressions in their own SAVE_EXPR
3975 and forcing evaluation at the proper time. */
3976 if (contains_placeholder_p (inner))
3977 return expr;
3978
3979 expr = build1_loc (EXPR_LOCATION (expr), code: SAVE_EXPR, TREE_TYPE (expr), arg1: expr);
3980
3981 /* This expression might be placed ahead of a jump to ensure that the
3982 value was computed on both sides of the jump. So make sure it isn't
3983 eliminated as dead. */
3984 TREE_SIDE_EFFECTS (expr) = 1;
3985 return expr;
3986}
3987
3988/* Look inside EXPR into any simple arithmetic operations. Return the
3989 outermost non-arithmetic or non-invariant node. */
3990
3991tree
3992skip_simple_arithmetic (tree expr)
3993{
3994 /* We don't care about whether this can be used as an lvalue in this
3995 context. */
3996 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3997 expr = TREE_OPERAND (expr, 0);
3998
3999 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
4000 a constant, it will be more efficient to not make another SAVE_EXPR since
4001 it will allow better simplification and GCSE will be able to merge the
4002 computations if they actually occur. */
4003 while (true)
4004 {
4005 if (UNARY_CLASS_P (expr))
4006 expr = TREE_OPERAND (expr, 0);
4007 else if (BINARY_CLASS_P (expr))
4008 {
4009 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
4010 expr = TREE_OPERAND (expr, 0);
4011 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
4012 expr = TREE_OPERAND (expr, 1);
4013 else
4014 break;
4015 }
4016 else
4017 break;
4018 }
4019
4020 return expr;
4021}
4022
4023/* Look inside EXPR into simple arithmetic operations involving constants.
4024 Return the outermost non-arithmetic or non-constant node. */
4025
4026tree
4027skip_simple_constant_arithmetic (tree expr)
4028{
4029 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
4030 expr = TREE_OPERAND (expr, 0);
4031
4032 while (true)
4033 {
4034 if (UNARY_CLASS_P (expr))
4035 expr = TREE_OPERAND (expr, 0);
4036 else if (BINARY_CLASS_P (expr))
4037 {
4038 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
4039 expr = TREE_OPERAND (expr, 0);
4040 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
4041 expr = TREE_OPERAND (expr, 1);
4042 else
4043 break;
4044 }
4045 else
4046 break;
4047 }
4048
4049 return expr;
4050}
4051
4052/* Return which tree structure is used by T. */
4053
4054enum tree_node_structure_enum
4055tree_node_structure (const_tree t)
4056{
4057 const enum tree_code code = TREE_CODE (t);
4058 return tree_node_structure_for_code (code);
4059}
4060
4061/* Set various status flags when building a CALL_EXPR object T. */
4062
4063static void
4064process_call_operands (tree t)
4065{
4066 bool side_effects = TREE_SIDE_EFFECTS (t);
4067 bool read_only = false;
4068 int i = call_expr_flags (t);
4069
4070 /* Calls have side-effects, except those to const or pure functions. */
4071 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
4072 side_effects = true;
4073 /* Propagate TREE_READONLY of arguments for const functions. */
4074 if (i & ECF_CONST)
4075 read_only = true;
4076
4077 if (!side_effects || read_only)
4078 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
4079 {
4080 tree op = TREE_OPERAND (t, i);
4081 if (op && TREE_SIDE_EFFECTS (op))
4082 side_effects = true;
4083 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
4084 read_only = false;
4085 }
4086
4087 TREE_SIDE_EFFECTS (t) = side_effects;
4088 TREE_READONLY (t) = read_only;
4089}
4090
4091/* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
4092 size or offset that depends on a field within a record. */
4093
4094bool
4095contains_placeholder_p (const_tree exp)
4096{
4097 enum tree_code code;
4098
4099 if (!exp)
4100 return false;
4101
4102 code = TREE_CODE (exp);
4103 if (code == PLACEHOLDER_EXPR)
4104 return true;
4105
4106 switch (TREE_CODE_CLASS (code))
4107 {
4108 case tcc_reference:
4109 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
4110 position computations since they will be converted into a
4111 WITH_RECORD_EXPR involving the reference, which will assume
4112 here will be valid. */
4113 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
4114
4115 case tcc_exceptional:
4116 if (code == TREE_LIST)
4117 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
4118 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
4119 break;
4120
4121 case tcc_unary:
4122 case tcc_binary:
4123 case tcc_comparison:
4124 case tcc_expression:
4125 switch (code)
4126 {
4127 case COMPOUND_EXPR:
4128 /* Ignoring the first operand isn't quite right, but works best. */
4129 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
4130
4131 case COND_EXPR:
4132 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
4133 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
4134 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
4135
4136 case SAVE_EXPR:
4137 /* The save_expr function never wraps anything containing
4138 a PLACEHOLDER_EXPR. */
4139 return false;
4140
4141 default:
4142 break;
4143 }
4144
4145 switch (TREE_CODE_LENGTH (code))
4146 {
4147 case 1:
4148 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
4149 case 2:
4150 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
4151 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
4152 default:
4153 return false;
4154 }
4155
4156 case tcc_vl_exp:
4157 switch (code)
4158 {
4159 case CALL_EXPR:
4160 {
4161 const_tree arg;
4162 const_call_expr_arg_iterator iter;
4163 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
4164 if (CONTAINS_PLACEHOLDER_P (arg))
4165 return true;
4166 return false;
4167 }
4168 default:
4169 return false;
4170 }
4171
4172 default:
4173 return false;
4174 }
4175 return false;
4176}
4177
4178/* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
4179 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
4180 field positions. */
4181
4182static bool
4183type_contains_placeholder_1 (const_tree type)
4184{
4185 /* If the size contains a placeholder or the parent type (component type in
4186 the case of arrays) type involves a placeholder, this type does. */
4187 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
4188 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
4189 || (!POINTER_TYPE_P (type)
4190 && TREE_TYPE (type)
4191 && type_contains_placeholder_p (TREE_TYPE (type))))
4192 return true;
4193
4194 /* Now do type-specific checks. Note that the last part of the check above
4195 greatly limits what we have to do below. */
4196 switch (TREE_CODE (type))
4197 {
4198 case VOID_TYPE:
4199 case OPAQUE_TYPE:
4200 case COMPLEX_TYPE:
4201 case ENUMERAL_TYPE:
4202 case BOOLEAN_TYPE:
4203 case POINTER_TYPE:
4204 case OFFSET_TYPE:
4205 case REFERENCE_TYPE:
4206 case METHOD_TYPE:
4207 case FUNCTION_TYPE:
4208 case VECTOR_TYPE:
4209 case NULLPTR_TYPE:
4210 return false;
4211
4212 case INTEGER_TYPE:
4213 case BITINT_TYPE:
4214 case REAL_TYPE:
4215 case FIXED_POINT_TYPE:
4216 /* Here we just check the bounds. */
4217 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
4218 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
4219
4220 case ARRAY_TYPE:
4221 /* We have already checked the component type above, so just check
4222 the domain type. Flexible array members have a null domain. */
4223 return TYPE_DOMAIN (type) ?
4224 type_contains_placeholder_p (TYPE_DOMAIN (type)) : false;
4225
4226 case RECORD_TYPE:
4227 case UNION_TYPE:
4228 case QUAL_UNION_TYPE:
4229 {
4230 tree field;
4231
4232 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4233 if (TREE_CODE (field) == FIELD_DECL
4234 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
4235 || (TREE_CODE (type) == QUAL_UNION_TYPE
4236 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
4237 || type_contains_placeholder_p (TREE_TYPE (field))))
4238 return true;
4239
4240 return false;
4241 }
4242
4243 default:
4244 gcc_unreachable ();
4245 }
4246}
4247
4248/* Wrapper around above function used to cache its result. */
4249
4250bool
4251type_contains_placeholder_p (tree type)
4252{
4253 bool result;
4254
4255 /* If the contains_placeholder_bits field has been initialized,
4256 then we know the answer. */
4257 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
4258 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
4259
4260 /* Indicate that we've seen this type node, and the answer is false.
4261 This is what we want to return if we run into recursion via fields. */
4262 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
4263
4264 /* Compute the real value. */
4265 result = type_contains_placeholder_1 (type);
4266
4267 /* Store the real value. */
4268 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
4269
4270 return result;
4271}
4272
4273/* Push tree EXP onto vector QUEUE if it is not already present. */
4274
4275static void
4276push_without_duplicates (tree exp, vec<tree> *queue)
4277{
4278 unsigned int i;
4279 tree iter;
4280
4281 FOR_EACH_VEC_ELT (*queue, i, iter)
4282 if (simple_cst_equal (iter, exp) == 1)
4283 break;
4284
4285 if (!iter)
4286 queue->safe_push (obj: exp);
4287}
4288
4289/* Given a tree EXP, find all occurrences of references to fields
4290 in a PLACEHOLDER_EXPR and place them in vector REFS without
4291 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
4292 we assume here that EXP contains only arithmetic expressions
4293 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
4294 argument list. */
4295
4296void
4297find_placeholder_in_expr (tree exp, vec<tree> *refs)
4298{
4299 enum tree_code code = TREE_CODE (exp);
4300 tree inner;
4301 int i;
4302
4303 /* We handle TREE_LIST and COMPONENT_REF separately. */
4304 if (code == TREE_LIST)
4305 {
4306 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
4307 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
4308 }
4309 else if (code == COMPONENT_REF)
4310 {
4311 for (inner = TREE_OPERAND (exp, 0);
4312 REFERENCE_CLASS_P (inner);
4313 inner = TREE_OPERAND (inner, 0))
4314 ;
4315
4316 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
4317 push_without_duplicates (exp, queue: refs);
4318 else
4319 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
4320 }
4321 else
4322 switch (TREE_CODE_CLASS (code))
4323 {
4324 case tcc_constant:
4325 break;
4326
4327 case tcc_declaration:
4328 /* Variables allocated to static storage can stay. */
4329 if (!TREE_STATIC (exp))
4330 push_without_duplicates (exp, queue: refs);
4331 break;
4332
4333 case tcc_expression:
4334 /* This is the pattern built in ada/make_aligning_type. */
4335 if (code == ADDR_EXPR
4336 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
4337 {
4338 push_without_duplicates (exp, queue: refs);
4339 break;
4340 }
4341
4342 /* Fall through. */
4343
4344 case tcc_exceptional:
4345 case tcc_unary:
4346 case tcc_binary:
4347 case tcc_comparison:
4348 case tcc_reference:
4349 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
4350 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
4351 break;
4352
4353 case tcc_vl_exp:
4354 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4355 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
4356 break;
4357
4358 default:
4359 gcc_unreachable ();
4360 }
4361}
4362
4363/* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
4364 return a tree with all occurrences of references to F in a
4365 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
4366 CONST_DECLs. Note that we assume here that EXP contains only
4367 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
4368 occurring only in their argument list. */
4369
4370tree
4371substitute_in_expr (tree exp, tree f, tree r)
4372{
4373 enum tree_code code = TREE_CODE (exp);
4374 tree op0, op1, op2, op3;
4375 tree new_tree;
4376
4377 /* We handle TREE_LIST and COMPONENT_REF separately. */
4378 if (code == TREE_LIST)
4379 {
4380 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
4381 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
4382 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
4383 return exp;
4384
4385 return tree_cons (TREE_PURPOSE (exp), value: op1, chain: op0);
4386 }
4387 else if (code == COMPONENT_REF)
4388 {
4389 tree inner;
4390
4391 /* If this expression is getting a value from a PLACEHOLDER_EXPR
4392 and it is the right field, replace it with R. */
4393 for (inner = TREE_OPERAND (exp, 0);
4394 REFERENCE_CLASS_P (inner);
4395 inner = TREE_OPERAND (inner, 0))
4396 ;
4397
4398 /* The field. */
4399 op1 = TREE_OPERAND (exp, 1);
4400
4401 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
4402 return r;
4403
4404 /* If this expression hasn't been completed let, leave it alone. */
4405 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
4406 return exp;
4407
4408 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4409 if (op0 == TREE_OPERAND (exp, 0))
4410 return exp;
4411
4412 new_tree
4413 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
4414 }
4415 else
4416 switch (TREE_CODE_CLASS (code))
4417 {
4418 case tcc_constant:
4419 return exp;
4420
4421 case tcc_declaration:
4422 if (exp == f)
4423 return r;
4424 else
4425 return exp;
4426
4427 case tcc_expression:
4428 if (exp == f)
4429 return r;
4430
4431 /* Fall through. */
4432
4433 case tcc_exceptional:
4434 case tcc_unary:
4435 case tcc_binary:
4436 case tcc_comparison:
4437 case tcc_reference:
4438 switch (TREE_CODE_LENGTH (code))
4439 {
4440 case 0:
4441 return exp;
4442
4443 case 1:
4444 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4445 if (op0 == TREE_OPERAND (exp, 0))
4446 return exp;
4447
4448 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
4449 break;
4450
4451 case 2:
4452 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4453 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4454
4455 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
4456 return exp;
4457
4458 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
4459 break;
4460
4461 case 3:
4462 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4463 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4464 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
4465
4466 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4467 && op2 == TREE_OPERAND (exp, 2))
4468 return exp;
4469
4470 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
4471 break;
4472
4473 case 4:
4474 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4475 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4476 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
4477 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
4478
4479 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4480 && op2 == TREE_OPERAND (exp, 2)
4481 && op3 == TREE_OPERAND (exp, 3))
4482 return exp;
4483
4484 new_tree
4485 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
4486 break;
4487
4488 default:
4489 gcc_unreachable ();
4490 }
4491 break;
4492
4493 case tcc_vl_exp:
4494 {
4495 int i;
4496
4497 new_tree = NULL_TREE;
4498
4499 /* If we are trying to replace F with a constant or with another
4500 instance of one of the arguments of the call, inline back
4501 functions which do nothing else than computing a value from
4502 the arguments they are passed. This makes it possible to
4503 fold partially or entirely the replacement expression. */
4504 if (code == CALL_EXPR)
4505 {
4506 bool maybe_inline = false;
4507 if (CONSTANT_CLASS_P (r))
4508 maybe_inline = true;
4509 else
4510 for (i = 3; i < TREE_OPERAND_LENGTH (exp); i++)
4511 if (operand_equal_p (TREE_OPERAND (exp, i), r, flags: 0))
4512 {
4513 maybe_inline = true;
4514 break;
4515 }
4516 if (maybe_inline)
4517 {
4518 tree t = maybe_inline_call_in_expr (exp);
4519 if (t)
4520 return SUBSTITUTE_IN_EXPR (t, f, r);
4521 }
4522 }
4523
4524 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4525 {
4526 tree op = TREE_OPERAND (exp, i);
4527 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
4528 if (new_op != op)
4529 {
4530 if (!new_tree)
4531 new_tree = copy_node (node: exp);
4532 TREE_OPERAND (new_tree, i) = new_op;
4533 }
4534 }
4535
4536 if (new_tree)
4537 {
4538 new_tree = fold (new_tree);
4539 if (TREE_CODE (new_tree) == CALL_EXPR)
4540 process_call_operands (t: new_tree);
4541 }
4542 else
4543 return exp;
4544 }
4545 break;
4546
4547 default:
4548 gcc_unreachable ();
4549 }
4550
4551 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4552
4553 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4554 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4555
4556 return new_tree;
4557}
4558
4559/* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
4560 for it within OBJ, a tree that is an object or a chain of references. */
4561
4562tree
4563substitute_placeholder_in_expr (tree exp, tree obj)
4564{
4565 enum tree_code code = TREE_CODE (exp);
4566 tree op0, op1, op2, op3;
4567 tree new_tree;
4568
4569 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
4570 in the chain of OBJ. */
4571 if (code == PLACEHOLDER_EXPR)
4572 {
4573 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
4574 tree elt;
4575
4576 for (elt = obj; elt != 0;
4577 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
4578 || TREE_CODE (elt) == COND_EXPR)
4579 ? TREE_OPERAND (elt, 1)
4580 : (REFERENCE_CLASS_P (elt)
4581 || UNARY_CLASS_P (elt)
4582 || BINARY_CLASS_P (elt)
4583 || VL_EXP_CLASS_P (elt)
4584 || EXPRESSION_CLASS_P (elt))
4585 ? TREE_OPERAND (elt, 0) : 0))
4586 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
4587 return elt;
4588
4589 for (elt = obj; elt != 0;
4590 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
4591 || TREE_CODE (elt) == COND_EXPR)
4592 ? TREE_OPERAND (elt, 1)
4593 : (REFERENCE_CLASS_P (elt)
4594 || UNARY_CLASS_P (elt)
4595 || BINARY_CLASS_P (elt)
4596 || VL_EXP_CLASS_P (elt)
4597 || EXPRESSION_CLASS_P (elt))
4598 ? TREE_OPERAND (elt, 0) : 0))
4599 if (POINTER_TYPE_P (TREE_TYPE (elt))
4600 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
4601 == need_type))
4602 return fold_build1 (INDIRECT_REF, need_type, elt);
4603
4604 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
4605 survives until RTL generation, there will be an error. */
4606 return exp;
4607 }
4608
4609 /* TREE_LIST is special because we need to look at TREE_VALUE
4610 and TREE_CHAIN, not TREE_OPERANDS. */
4611 else if (code == TREE_LIST)
4612 {
4613 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
4614 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
4615 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
4616 return exp;
4617
4618 return tree_cons (TREE_PURPOSE (exp), value: op1, chain: op0);
4619 }
4620 else
4621 switch (TREE_CODE_CLASS (code))
4622 {
4623 case tcc_constant:
4624 case tcc_declaration:
4625 return exp;
4626
4627 case tcc_exceptional:
4628 case tcc_unary:
4629 case tcc_binary:
4630 case tcc_comparison:
4631 case tcc_expression:
4632 case tcc_reference:
4633 case tcc_statement:
4634 switch (TREE_CODE_LENGTH (code))
4635 {
4636 case 0:
4637 return exp;
4638
4639 case 1:
4640 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4641 if (op0 == TREE_OPERAND (exp, 0))
4642 return exp;
4643
4644 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
4645 break;
4646
4647 case 2:
4648 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4649 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4650
4651 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
4652 return exp;
4653
4654 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
4655 break;
4656
4657 case 3:
4658 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4659 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4660 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
4661
4662 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4663 && op2 == TREE_OPERAND (exp, 2))
4664 return exp;
4665
4666 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
4667 break;
4668
4669 case 4:
4670 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4671 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4672 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
4673 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
4674
4675 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4676 && op2 == TREE_OPERAND (exp, 2)
4677 && op3 == TREE_OPERAND (exp, 3))
4678 return exp;
4679
4680 new_tree
4681 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
4682 break;
4683
4684 default:
4685 gcc_unreachable ();
4686 }
4687 break;
4688
4689 case tcc_vl_exp:
4690 {
4691 int i;
4692
4693 new_tree = NULL_TREE;
4694
4695 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4696 {
4697 tree op = TREE_OPERAND (exp, i);
4698 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
4699 if (new_op != op)
4700 {
4701 if (!new_tree)
4702 new_tree = copy_node (node: exp);
4703 TREE_OPERAND (new_tree, i) = new_op;
4704 }
4705 }
4706
4707 if (new_tree)
4708 {
4709 new_tree = fold (new_tree);
4710 if (TREE_CODE (new_tree) == CALL_EXPR)
4711 process_call_operands (t: new_tree);
4712 }
4713 else
4714 return exp;
4715 }
4716 break;
4717
4718 default:
4719 gcc_unreachable ();
4720 }
4721
4722 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4723
4724 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4725 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4726
4727 return new_tree;
4728}
4729
4730
4731/* Subroutine of stabilize_reference; this is called for subtrees of
4732 references. Any expression with side-effects must be put in a SAVE_EXPR
4733 to ensure that it is only evaluated once.
4734
4735 We don't put SAVE_EXPR nodes around everything, because assigning very
4736 simple expressions to temporaries causes us to miss good opportunities
4737 for optimizations. Among other things, the opportunity to fold in the
4738 addition of a constant into an addressing mode often gets lost, e.g.
4739 "y[i+1] += x;". In general, we take the approach that we should not make
4740 an assignment unless we are forced into it - i.e., that any non-side effect
4741 operator should be allowed, and that cse should take care of coalescing
4742 multiple utterances of the same expression should that prove fruitful. */
4743
4744static tree
4745stabilize_reference_1 (tree e)
4746{
4747 tree result;
4748 enum tree_code code = TREE_CODE (e);
4749
4750 /* We cannot ignore const expressions because it might be a reference
4751 to a const array but whose index contains side-effects. But we can
4752 ignore things that are actual constant or that already have been
4753 handled by this function. */
4754
4755 if (tree_invariant_p (t: e))
4756 return e;
4757
4758 switch (TREE_CODE_CLASS (code))
4759 {
4760 case tcc_exceptional:
4761 /* Always wrap STATEMENT_LIST into SAVE_EXPR, even if it doesn't
4762 have side-effects. */
4763 if (code == STATEMENT_LIST)
4764 return save_expr (expr: e);
4765 /* FALLTHRU */
4766 case tcc_type:
4767 case tcc_declaration:
4768 case tcc_comparison:
4769 case tcc_statement:
4770 case tcc_expression:
4771 case tcc_reference:
4772 case tcc_vl_exp:
4773 /* If the expression has side-effects, then encase it in a SAVE_EXPR
4774 so that it will only be evaluated once. */
4775 /* The reference (r) and comparison (<) classes could be handled as
4776 below, but it is generally faster to only evaluate them once. */
4777 if (TREE_SIDE_EFFECTS (e))
4778 return save_expr (expr: e);
4779 return e;
4780
4781 case tcc_constant:
4782 /* Constants need no processing. In fact, we should never reach
4783 here. */
4784 return e;
4785
4786 case tcc_binary:
4787 /* Division is slow and tends to be compiled with jumps,
4788 especially the division by powers of 2 that is often
4789 found inside of an array reference. So do it just once. */
4790 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
4791 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
4792 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
4793 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
4794 return save_expr (expr: e);
4795 /* Recursively stabilize each operand. */
4796 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
4797 stabilize_reference_1 (TREE_OPERAND (e, 1)));
4798 break;
4799
4800 case tcc_unary:
4801 /* Recursively stabilize each operand. */
4802 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
4803 break;
4804
4805 default:
4806 gcc_unreachable ();
4807 }
4808
4809 TREE_TYPE (result) = TREE_TYPE (e);
4810 TREE_READONLY (result) = TREE_READONLY (e);
4811 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
4812 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
4813
4814 return result;
4815}
4816
4817/* Stabilize a reference so that we can use it any number of times
4818 without causing its operands to be evaluated more than once.
4819 Returns the stabilized reference. This works by means of save_expr,
4820 so see the caveats in the comments about save_expr.
4821
4822 Also allows conversion expressions whose operands are references.
4823 Any other kind of expression is returned unchanged. */
4824
4825tree
4826stabilize_reference (tree ref)
4827{
4828 tree result;
4829 enum tree_code code = TREE_CODE (ref);
4830
4831 switch (code)
4832 {
4833 case VAR_DECL:
4834 case PARM_DECL:
4835 case RESULT_DECL:
4836 /* No action is needed in this case. */
4837 return ref;
4838
4839 CASE_CONVERT:
4840 case FLOAT_EXPR:
4841 case FIX_TRUNC_EXPR:
4842 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
4843 break;
4844
4845 case INDIRECT_REF:
4846 result = build_nt (INDIRECT_REF,
4847 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
4848 break;
4849
4850 case COMPONENT_REF:
4851 result = build_nt (COMPONENT_REF,
4852 stabilize_reference (TREE_OPERAND (ref, 0)),
4853 TREE_OPERAND (ref, 1), NULL_TREE);
4854 break;
4855
4856 case BIT_FIELD_REF:
4857 result = build_nt (BIT_FIELD_REF,
4858 stabilize_reference (TREE_OPERAND (ref, 0)),
4859 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
4860 REF_REVERSE_STORAGE_ORDER (result) = REF_REVERSE_STORAGE_ORDER (ref);
4861 break;
4862
4863 case ARRAY_REF:
4864 result = build_nt (ARRAY_REF,
4865 stabilize_reference (TREE_OPERAND (ref, 0)),
4866 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4867 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4868 break;
4869
4870 case ARRAY_RANGE_REF:
4871 result = build_nt (ARRAY_RANGE_REF,
4872 stabilize_reference (TREE_OPERAND (ref, 0)),
4873 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4874 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4875 break;
4876
4877 case COMPOUND_EXPR:
4878 /* We cannot wrap the first expression in a SAVE_EXPR, as then
4879 it wouldn't be ignored. This matters when dealing with
4880 volatiles. */
4881 return stabilize_reference_1 (e: ref);
4882
4883 /* If arg isn't a kind of lvalue we recognize, make no change.
4884 Caller should recognize the error for an invalid lvalue. */
4885 default:
4886 return ref;
4887
4888 case ERROR_MARK:
4889 return error_mark_node;
4890 }
4891
4892 TREE_TYPE (result) = TREE_TYPE (ref);
4893 TREE_READONLY (result) = TREE_READONLY (ref);
4894 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
4895 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
4896 protected_set_expr_location (result, EXPR_LOCATION (ref));
4897
4898 return result;
4899}
4900
4901/* Low-level constructors for expressions. */
4902
4903/* A helper function for build1 and constant folders. Set TREE_CONSTANT,
4904 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
4905
4906void
4907recompute_tree_invariant_for_addr_expr (tree t)
4908{
4909 tree node;
4910 bool tc = true, se = false;
4911
4912 gcc_assert (TREE_CODE (t) == ADDR_EXPR);
4913
4914 /* We started out assuming this address is both invariant and constant, but
4915 does not have side effects. Now go down any handled components and see if
4916 any of them involve offsets that are either non-constant or non-invariant.
4917 Also check for side-effects.
4918
4919 ??? Note that this code makes no attempt to deal with the case where
4920 taking the address of something causes a copy due to misalignment. */
4921
4922#define UPDATE_FLAGS(NODE) \
4923do { tree _node = (NODE); \
4924 if (_node && !TREE_CONSTANT (_node)) tc = false; \
4925 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
4926
4927 for (node = TREE_OPERAND (t, 0); handled_component_p (t: node);
4928 node = TREE_OPERAND (node, 0))
4929 {
4930 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
4931 array reference (probably made temporarily by the G++ front end),
4932 so ignore all the operands. */
4933 if ((TREE_CODE (node) == ARRAY_REF
4934 || TREE_CODE (node) == ARRAY_RANGE_REF)
4935 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
4936 {
4937 UPDATE_FLAGS (TREE_OPERAND (node, 1));
4938 if (TREE_OPERAND (node, 2))
4939 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4940 if (TREE_OPERAND (node, 3))
4941 UPDATE_FLAGS (TREE_OPERAND (node, 3));
4942 }
4943 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
4944 FIELD_DECL, apparently. The G++ front end can put something else
4945 there, at least temporarily. */
4946 else if (TREE_CODE (node) == COMPONENT_REF
4947 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
4948 {
4949 if (TREE_OPERAND (node, 2))
4950 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4951 }
4952 }
4953
4954 node = lang_hooks.expr_to_decl (node, &tc, &se);
4955
4956 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4957 the address, since &(*a)->b is a form of addition. If it's a constant, the
4958 address is constant too. If it's a decl, its address is constant if the
4959 decl is static. Everything else is not constant and, furthermore,
4960 taking the address of a volatile variable is not volatile. */
4961 if (INDIRECT_REF_P (node)
4962 || TREE_CODE (node) == MEM_REF)
4963 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4964 else if (CONSTANT_CLASS_P (node))
4965 ;
4966 else if (DECL_P (node))
4967 tc &= (staticp (arg: node) != NULL_TREE);
4968 else
4969 {
4970 tc = false;
4971 se |= TREE_SIDE_EFFECTS (node);
4972 }
4973
4974
4975 TREE_CONSTANT (t) = tc;
4976 TREE_SIDE_EFFECTS (t) = se;
4977#undef UPDATE_FLAGS
4978}
4979
4980/* Build an expression of code CODE, data type TYPE, and operands as
4981 specified. Expressions and reference nodes can be created this way.
4982 Constants, decls, types and misc nodes cannot be.
4983
4984 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4985 enough for all extant tree codes. */
4986
4987tree
4988build0 (enum tree_code code, tree tt MEM_STAT_DECL)
4989{
4990 tree t;
4991
4992 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4993
4994 t = make_node (code PASS_MEM_STAT);
4995 TREE_TYPE (t) = tt;
4996
4997 return t;
4998}
4999
5000tree
5001build1 (enum tree_code code, tree type, tree node MEM_STAT_DECL)
5002{
5003 int length = sizeof (struct tree_exp);
5004 tree t;
5005
5006 record_node_allocation_statistics (code, length);
5007
5008 gcc_assert (TREE_CODE_LENGTH (code) == 1);
5009
5010 t = ggc_alloc_tree_node_stat (s: length PASS_MEM_STAT);
5011
5012 memset (s: t, c: 0, n: sizeof (struct tree_common));
5013
5014 TREE_SET_CODE (t, code);
5015
5016 TREE_TYPE (t) = type;
5017 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
5018 TREE_OPERAND (t, 0) = node;
5019 if (node && !TYPE_P (node))
5020 {
5021 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
5022 TREE_READONLY (t) = TREE_READONLY (node);
5023 }
5024
5025 if (TREE_CODE_CLASS (code) == tcc_statement)
5026 {
5027 if (code != DEBUG_BEGIN_STMT)
5028 TREE_SIDE_EFFECTS (t) = 1;
5029 }
5030 else switch (code)
5031 {
5032 case VA_ARG_EXPR:
5033 /* All of these have side-effects, no matter what their
5034 operands are. */
5035 TREE_SIDE_EFFECTS (t) = 1;
5036 TREE_READONLY (t) = 0;
5037 break;
5038
5039 case INDIRECT_REF:
5040 /* Whether a dereference is readonly has nothing to do with whether
5041 its operand is readonly. */
5042 TREE_READONLY (t) = 0;
5043 break;
5044
5045 case ADDR_EXPR:
5046 if (node)
5047 recompute_tree_invariant_for_addr_expr (t);
5048 break;
5049
5050 default:
5051 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
5052 && node && !TYPE_P (node)
5053 && TREE_CONSTANT (node))
5054 TREE_CONSTANT (t) = 1;
5055 if (TREE_CODE_CLASS (code) == tcc_reference
5056 && node && TREE_THIS_VOLATILE (node))
5057 TREE_THIS_VOLATILE (t) = 1;
5058 break;
5059 }
5060
5061 return t;
5062}
5063
5064#define PROCESS_ARG(N) \
5065 do { \
5066 TREE_OPERAND (t, N) = arg##N; \
5067 if (arg##N &&!TYPE_P (arg##N)) \
5068 { \
5069 if (TREE_SIDE_EFFECTS (arg##N)) \
5070 side_effects = 1; \
5071 if (!TREE_READONLY (arg##N) \
5072 && !CONSTANT_CLASS_P (arg##N)) \
5073 (void) (read_only = 0); \
5074 if (!TREE_CONSTANT (arg##N)) \
5075 (void) (constant = 0); \
5076 } \
5077 } while (0)
5078
5079tree
5080build2 (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
5081{
5082 bool constant, read_only, side_effects, div_by_zero;
5083 tree t;
5084
5085 gcc_assert (TREE_CODE_LENGTH (code) == 2);
5086
5087 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
5088 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
5089 /* When sizetype precision doesn't match that of pointers
5090 we need to be able to build explicit extensions or truncations
5091 of the offset argument. */
5092 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
5093 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
5094 && TREE_CODE (arg1) == INTEGER_CST);
5095
5096 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
5097 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
5098 && ptrofftype_p (TREE_TYPE (arg1)));
5099
5100 t = make_node (code PASS_MEM_STAT);
5101 TREE_TYPE (t) = tt;
5102
5103 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
5104 result based on those same flags for the arguments. But if the
5105 arguments aren't really even `tree' expressions, we shouldn't be trying
5106 to do this. */
5107
5108 /* Expressions without side effects may be constant if their
5109 arguments are as well. */
5110 constant = (TREE_CODE_CLASS (code) == tcc_comparison
5111 || TREE_CODE_CLASS (code) == tcc_binary);
5112 read_only = 1;
5113 side_effects = TREE_SIDE_EFFECTS (t);
5114
5115 switch (code)
5116 {
5117 case TRUNC_DIV_EXPR:
5118 case CEIL_DIV_EXPR:
5119 case FLOOR_DIV_EXPR:
5120 case ROUND_DIV_EXPR:
5121 case EXACT_DIV_EXPR:
5122 case CEIL_MOD_EXPR:
5123 case FLOOR_MOD_EXPR:
5124 case ROUND_MOD_EXPR:
5125 case TRUNC_MOD_EXPR:
5126 div_by_zero = integer_zerop (expr: arg1);
5127 break;
5128 default:
5129 div_by_zero = false;
5130 }
5131
5132 PROCESS_ARG (0);
5133 PROCESS_ARG (1);
5134
5135 TREE_SIDE_EFFECTS (t) = side_effects;
5136 if (code == MEM_REF)
5137 {
5138 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
5139 {
5140 tree o = TREE_OPERAND (arg0, 0);
5141 TREE_READONLY (t) = TREE_READONLY (o);
5142 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
5143 }
5144 }
5145 else
5146 {
5147 TREE_READONLY (t) = read_only;
5148 /* Don't mark X / 0 as constant. */
5149 TREE_CONSTANT (t) = constant && !div_by_zero;
5150 TREE_THIS_VOLATILE (t)
5151 = (TREE_CODE_CLASS (code) == tcc_reference
5152 && arg0 && TREE_THIS_VOLATILE (arg0));
5153 }
5154
5155 return t;
5156}
5157
5158
5159tree
5160build3 (enum tree_code code, tree tt, tree arg0, tree arg1,
5161 tree arg2 MEM_STAT_DECL)
5162{
5163 bool constant, read_only, side_effects;
5164 tree t;
5165
5166 gcc_assert (TREE_CODE_LENGTH (code) == 3);
5167 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
5168
5169 t = make_node (code PASS_MEM_STAT);
5170 TREE_TYPE (t) = tt;
5171
5172 read_only = 1;
5173
5174 /* As a special exception, if COND_EXPR has NULL branches, we
5175 assume that it is a gimple statement and always consider
5176 it to have side effects. */
5177 if (code == COND_EXPR
5178 && tt == void_type_node
5179 && arg1 == NULL_TREE
5180 && arg2 == NULL_TREE)
5181 side_effects = true;
5182 else
5183 side_effects = TREE_SIDE_EFFECTS (t);
5184
5185 PROCESS_ARG (0);
5186 PROCESS_ARG (1);
5187 PROCESS_ARG (2);
5188
5189 if (code == COND_EXPR)
5190 TREE_READONLY (t) = read_only;
5191
5192 TREE_SIDE_EFFECTS (t) = side_effects;
5193 TREE_THIS_VOLATILE (t)
5194 = (TREE_CODE_CLASS (code) == tcc_reference
5195 && arg0 && TREE_THIS_VOLATILE (arg0));
5196
5197 return t;
5198}
5199
5200tree
5201build4 (enum tree_code code, tree tt, tree arg0, tree arg1,
5202 tree arg2, tree arg3 MEM_STAT_DECL)
5203{
5204 bool constant, read_only, side_effects;
5205 tree t;
5206
5207 gcc_assert (TREE_CODE_LENGTH (code) == 4);
5208
5209 t = make_node (code PASS_MEM_STAT);
5210 TREE_TYPE (t) = tt;
5211
5212 side_effects = TREE_SIDE_EFFECTS (t);
5213
5214 PROCESS_ARG (0);
5215 PROCESS_ARG (1);
5216 PROCESS_ARG (2);
5217 PROCESS_ARG (3);
5218
5219 TREE_SIDE_EFFECTS (t) = side_effects;
5220 TREE_THIS_VOLATILE (t)
5221 = (TREE_CODE_CLASS (code) == tcc_reference
5222 && arg0 && TREE_THIS_VOLATILE (arg0));
5223
5224 return t;
5225}
5226
5227tree
5228build5 (enum tree_code code, tree tt, tree arg0, tree arg1,
5229 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
5230{
5231 bool constant, read_only, side_effects;
5232 tree t;
5233
5234 gcc_assert (TREE_CODE_LENGTH (code) == 5);
5235
5236 t = make_node (code PASS_MEM_STAT);
5237 TREE_TYPE (t) = tt;
5238
5239 side_effects = TREE_SIDE_EFFECTS (t);
5240
5241 PROCESS_ARG (0);
5242 PROCESS_ARG (1);
5243 PROCESS_ARG (2);
5244 PROCESS_ARG (3);
5245 PROCESS_ARG (4);
5246
5247 TREE_SIDE_EFFECTS (t) = side_effects;
5248 if (code == TARGET_MEM_REF)
5249 {
5250 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
5251 {
5252 tree o = TREE_OPERAND (arg0, 0);
5253 TREE_READONLY (t) = TREE_READONLY (o);
5254 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
5255 }
5256 }
5257 else
5258 TREE_THIS_VOLATILE (t)
5259 = (TREE_CODE_CLASS (code) == tcc_reference
5260 && arg0 && TREE_THIS_VOLATILE (arg0));
5261
5262 return t;
5263}
5264
5265/* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
5266 on the pointer PTR. */
5267
5268tree
5269build_simple_mem_ref_loc (location_t loc, tree ptr)
5270{
5271 poly_int64 offset = 0;
5272 tree ptype = TREE_TYPE (ptr);
5273 tree tem;
5274 /* For convenience allow addresses that collapse to a simple base
5275 and offset. */
5276 if (TREE_CODE (ptr) == ADDR_EXPR
5277 && (handled_component_p (TREE_OPERAND (ptr, 0))
5278 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
5279 {
5280 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
5281 gcc_assert (ptr);
5282 if (TREE_CODE (ptr) == MEM_REF)
5283 {
5284 offset += mem_ref_offset (ptr).force_shwi ();
5285 ptr = TREE_OPERAND (ptr, 0);
5286 }
5287 else
5288 ptr = build_fold_addr_expr (ptr);
5289 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
5290 }
5291 tem = build2 (code: MEM_REF, TREE_TYPE (ptype),
5292 arg0: ptr, arg1: build_int_cst (type: ptype, cst: offset));
5293 SET_EXPR_LOCATION (tem, loc);
5294 return tem;
5295}
5296
5297/* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
5298
5299poly_offset_int
5300mem_ref_offset (const_tree t)
5301{
5302 return poly_offset_int::from (a: wi::to_poly_wide (TREE_OPERAND (t, 1)),
5303 sgn: SIGNED);
5304}
5305
5306/* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
5307 offsetted by OFFSET units. */
5308
5309tree
5310build_invariant_address (tree type, tree base, poly_int64 offset)
5311{
5312 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
5313 build_fold_addr_expr (base),
5314 build_int_cst (ptr_type_node, offset));
5315 tree addr = build1 (code: ADDR_EXPR, type, node: ref);
5316 recompute_tree_invariant_for_addr_expr (t: addr);
5317 return addr;
5318}
5319
5320/* Similar except don't specify the TREE_TYPE
5321 and leave the TREE_SIDE_EFFECTS as 0.
5322 It is permissible for arguments to be null,
5323 or even garbage if their values do not matter. */
5324
5325tree
5326build_nt (enum tree_code code, ...)
5327{
5328 tree t;
5329 int length;
5330 int i;
5331 va_list p;
5332
5333 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
5334
5335 va_start (p, code);
5336
5337 t = make_node (code);
5338 length = TREE_CODE_LENGTH (code);
5339
5340 for (i = 0; i < length; i++)
5341 TREE_OPERAND (t, i) = va_arg (p, tree);
5342
5343 va_end (p);
5344 return t;
5345}
5346
5347/* Similar to build_nt, but for creating a CALL_EXPR object with a
5348 tree vec. */
5349
5350tree
5351build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
5352{
5353 tree ret, t;
5354 unsigned int ix;
5355
5356 ret = build_vl_exp (CALL_EXPR, vec_safe_length (v: args) + 3);
5357 CALL_EXPR_FN (ret) = fn;
5358 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
5359 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
5360 CALL_EXPR_ARG (ret, ix) = t;
5361 return ret;
5362}
5363
5364/* Create a DECL_... node of code CODE, name NAME (if non-null)
5365 and data type TYPE.
5366 We do NOT enter this node in any sort of symbol table.
5367
5368 LOC is the location of the decl.
5369
5370 layout_decl is used to set up the decl's storage layout.
5371 Other slots are initialized to 0 or null pointers. */
5372
5373tree
5374build_decl (location_t loc, enum tree_code code, tree name,
5375 tree type MEM_STAT_DECL)
5376{
5377 tree t;
5378
5379 t = make_node (code PASS_MEM_STAT);
5380 DECL_SOURCE_LOCATION (t) = loc;
5381
5382/* if (type == error_mark_node)
5383 type = integer_type_node; */
5384/* That is not done, deliberately, so that having error_mark_node
5385 as the type can suppress useless errors in the use of this variable. */
5386
5387 DECL_NAME (t) = name;
5388 TREE_TYPE (t) = type;
5389
5390 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
5391 layout_decl (t, 0);
5392
5393 return t;
5394}
5395
5396/* Create and return a DEBUG_EXPR_DECL node of the given TYPE. */
5397
5398tree
5399build_debug_expr_decl (tree type)
5400{
5401 tree vexpr = make_node (code: DEBUG_EXPR_DECL);
5402 DECL_ARTIFICIAL (vexpr) = 1;
5403 TREE_TYPE (vexpr) = type;
5404 SET_DECL_MODE (vexpr, TYPE_MODE (type));
5405 return vexpr;
5406}
5407
5408/* Builds and returns function declaration with NAME and TYPE. */
5409
5410tree
5411build_fn_decl (const char *name, tree type)
5412{
5413 tree id = get_identifier (name);
5414 tree decl = build_decl (loc: input_location, code: FUNCTION_DECL, name: id, type);
5415
5416 DECL_EXTERNAL (decl) = 1;
5417 TREE_PUBLIC (decl) = 1;
5418 DECL_ARTIFICIAL (decl) = 1;
5419 TREE_NOTHROW (decl) = 1;
5420
5421 return decl;
5422}
5423
5424vec<tree, va_gc> *all_translation_units;
5425
5426/* Builds a new translation-unit decl with name NAME, queues it in the
5427 global list of translation-unit decls and returns it. */
5428
5429tree
5430build_translation_unit_decl (tree name)
5431{
5432 tree tu = build_decl (UNKNOWN_LOCATION, code: TRANSLATION_UNIT_DECL,
5433 name, NULL_TREE);
5434 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
5435 vec_safe_push (v&: all_translation_units, obj: tu);
5436 return tu;
5437}
5438
5439
5440/* BLOCK nodes are used to represent the structure of binding contours
5441 and declarations, once those contours have been exited and their contents
5442 compiled. This information is used for outputting debugging info. */
5443
5444tree
5445build_block (tree vars, tree subblocks, tree supercontext, tree chain)
5446{
5447 tree block = make_node (code: BLOCK);
5448
5449 BLOCK_VARS (block) = vars;
5450 BLOCK_SUBBLOCKS (block) = subblocks;
5451 BLOCK_SUPERCONTEXT (block) = supercontext;
5452 BLOCK_CHAIN (block) = chain;
5453 return block;
5454}
5455
5456
5457/* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
5458
5459 LOC is the location to use in tree T. */
5460
5461void
5462protected_set_expr_location (tree t, location_t loc)
5463{
5464 if (CAN_HAVE_LOCATION_P (t))
5465 SET_EXPR_LOCATION (t, loc);
5466 else if (t && TREE_CODE (t) == STATEMENT_LIST)
5467 {
5468 t = expr_single (t);
5469 if (t && CAN_HAVE_LOCATION_P (t))
5470 SET_EXPR_LOCATION (t, loc);
5471 }
5472}
5473
5474/* Like PROTECTED_SET_EXPR_LOCATION, but only do that if T has
5475 UNKNOWN_LOCATION. */
5476
5477void
5478protected_set_expr_location_if_unset (tree t, location_t loc)
5479{
5480 t = expr_single (t);
5481 if (t && !EXPR_HAS_LOCATION (t))
5482 protected_set_expr_location (t, loc);
5483}
5484
5485/* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
5486 of the various TYPE_QUAL values. */
5487
5488static void
5489set_type_quals (tree type, int type_quals)
5490{
5491 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
5492 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
5493 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
5494 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
5495 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
5496}
5497
5498/* Returns true iff CAND and BASE have equivalent language-specific
5499 qualifiers. */
5500
5501bool
5502check_lang_type (const_tree cand, const_tree base)
5503{
5504 if (lang_hooks.types.type_hash_eq == NULL)
5505 return true;
5506 /* type_hash_eq currently only applies to these types. */
5507 if (TREE_CODE (cand) != FUNCTION_TYPE
5508 && TREE_CODE (cand) != METHOD_TYPE)
5509 return true;
5510 return lang_hooks.types.type_hash_eq (cand, base);
5511}
5512
5513/* This function checks to see if TYPE matches the size one of the built-in
5514 atomic types, and returns that core atomic type. */
5515
5516static tree
5517find_atomic_core_type (const_tree type)
5518{
5519 tree base_atomic_type;
5520
5521 /* Only handle complete types. */
5522 if (!tree_fits_uhwi_p (TYPE_SIZE (type)))
5523 return NULL_TREE;
5524
5525 switch (tree_to_uhwi (TYPE_SIZE (type)))
5526 {
5527 case 8:
5528 base_atomic_type = atomicQI_type_node;
5529 break;
5530
5531 case 16:
5532 base_atomic_type = atomicHI_type_node;
5533 break;
5534
5535 case 32:
5536 base_atomic_type = atomicSI_type_node;
5537 break;
5538
5539 case 64:
5540 base_atomic_type = atomicDI_type_node;
5541 break;
5542
5543 case 128:
5544 base_atomic_type = atomicTI_type_node;
5545 break;
5546
5547 default:
5548 base_atomic_type = NULL_TREE;
5549 }
5550
5551 return base_atomic_type;
5552}
5553
5554/* Returns true iff unqualified CAND and BASE are equivalent. */
5555
5556bool
5557check_base_type (const_tree cand, const_tree base)
5558{
5559 if (TYPE_NAME (cand) != TYPE_NAME (base)
5560 /* Apparently this is needed for Objective-C. */
5561 || TYPE_CONTEXT (cand) != TYPE_CONTEXT (base)
5562 || !attribute_list_equal (TYPE_ATTRIBUTES (cand),
5563 TYPE_ATTRIBUTES (base)))
5564 return false;
5565 /* Check alignment. */
5566 if (TYPE_ALIGN (cand) == TYPE_ALIGN (base)
5567 && TYPE_USER_ALIGN (cand) == TYPE_USER_ALIGN (base))
5568 return true;
5569 /* Atomic types increase minimal alignment. We must to do so as well
5570 or we get duplicated canonical types. See PR88686. */
5571 if ((TYPE_QUALS (cand) & TYPE_QUAL_ATOMIC))
5572 {
5573 /* See if this object can map to a basic atomic type. */
5574 tree atomic_type = find_atomic_core_type (type: cand);
5575 if (atomic_type && TYPE_ALIGN (atomic_type) == TYPE_ALIGN (cand))
5576 return true;
5577 }
5578 return false;
5579}
5580
5581/* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
5582
5583bool
5584check_qualified_type (const_tree cand, const_tree base, int type_quals)
5585{
5586 return (TYPE_QUALS (cand) == type_quals
5587 && check_base_type (cand, base)
5588 && check_lang_type (cand, base));
5589}
5590
5591/* Returns true iff CAND is equivalent to BASE with ALIGN. */
5592
5593static bool
5594check_aligned_type (const_tree cand, const_tree base, unsigned int align)
5595{
5596 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
5597 && TYPE_NAME (cand) == TYPE_NAME (base)
5598 /* Apparently this is needed for Objective-C. */
5599 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5600 /* Check alignment. */
5601 && TYPE_ALIGN (cand) == align
5602 /* Check this is a user-aligned type as build_aligned_type
5603 would create. */
5604 && TYPE_USER_ALIGN (cand)
5605 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5606 TYPE_ATTRIBUTES (base))
5607 && check_lang_type (cand, base));
5608}
5609
5610/* Return a version of the TYPE, qualified as indicated by the
5611 TYPE_QUALS, if one exists. If no qualified version exists yet,
5612 return NULL_TREE. */
5613
5614tree
5615get_qualified_type (tree type, int type_quals)
5616{
5617 if (TYPE_QUALS (type) == type_quals)
5618 return type;
5619
5620 tree mv = TYPE_MAIN_VARIANT (type);
5621 if (check_qualified_type (cand: mv, base: type, type_quals))
5622 return mv;
5623
5624 /* Search the chain of variants to see if there is already one there just
5625 like the one we need to have. If so, use that existing one. We must
5626 preserve the TYPE_NAME, since there is code that depends on this. */
5627 for (tree *tp = &TYPE_NEXT_VARIANT (mv); *tp; tp = &TYPE_NEXT_VARIANT (*tp))
5628 if (check_qualified_type (cand: *tp, base: type, type_quals))
5629 {
5630 /* Put the found variant at the head of the variant list so
5631 frequently searched variants get found faster. The C++ FE
5632 benefits greatly from this. */
5633 tree t = *tp;
5634 *tp = TYPE_NEXT_VARIANT (t);
5635 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
5636 TYPE_NEXT_VARIANT (mv) = t;
5637 return t;
5638 }
5639
5640 return NULL_TREE;
5641}
5642
5643/* Like get_qualified_type, but creates the type if it does not
5644 exist. This function never returns NULL_TREE. */
5645
5646tree
5647build_qualified_type (tree type, int type_quals MEM_STAT_DECL)
5648{
5649 tree t;
5650
5651 /* See if we already have the appropriate qualified variant. */
5652 t = get_qualified_type (type, type_quals);
5653
5654 /* If not, build it. */
5655 if (!t)
5656 {
5657 t = build_variant_type_copy (type PASS_MEM_STAT);
5658 set_type_quals (type: t, type_quals);
5659
5660 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
5661 {
5662 /* See if this object can map to a basic atomic type. */
5663 tree atomic_type = find_atomic_core_type (type);
5664 if (atomic_type)
5665 {
5666 /* Ensure the alignment of this type is compatible with
5667 the required alignment of the atomic type. */
5668 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
5669 SET_TYPE_ALIGN (t, TYPE_ALIGN (atomic_type));
5670 }
5671 }
5672
5673 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5674 /* Propagate structural equality. */
5675 SET_TYPE_STRUCTURAL_EQUALITY (t);
5676 else if (TYPE_CANONICAL (type) != type)
5677 /* Build the underlying canonical type, since it is different
5678 from TYPE. */
5679 {
5680 tree c = build_qualified_type (TYPE_CANONICAL (type), type_quals);
5681 TYPE_CANONICAL (t) = TYPE_CANONICAL (c);
5682 }
5683 else
5684 /* T is its own canonical type. */
5685 TYPE_CANONICAL (t) = t;
5686
5687 }
5688
5689 return t;
5690}
5691
5692/* Create a variant of type T with alignment ALIGN which
5693 is measured in bits. */
5694
5695tree
5696build_aligned_type (tree type, unsigned int align)
5697{
5698 tree t;
5699
5700 if (TYPE_PACKED (type)
5701 || TYPE_ALIGN (type) == align)
5702 return type;
5703
5704 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5705 if (check_aligned_type (cand: t, base: type, align))
5706 return t;
5707
5708 t = build_variant_type_copy (type);
5709 SET_TYPE_ALIGN (t, align);
5710 TYPE_USER_ALIGN (t) = 1;
5711
5712 return t;
5713}
5714
5715/* Create a new distinct copy of TYPE. The new type is made its own
5716 MAIN_VARIANT. If TYPE requires structural equality checks, the
5717 resulting type requires structural equality checks; otherwise, its
5718 TYPE_CANONICAL points to itself. */
5719
5720tree
5721build_distinct_type_copy (tree type MEM_STAT_DECL)
5722{
5723 tree t = copy_node (node: type PASS_MEM_STAT);
5724
5725 TYPE_POINTER_TO (t) = 0;
5726 TYPE_REFERENCE_TO (t) = 0;
5727
5728 /* Set the canonical type either to a new equivalence class, or
5729 propagate the need for structural equality checks. */
5730 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5731 SET_TYPE_STRUCTURAL_EQUALITY (t);
5732 else
5733 TYPE_CANONICAL (t) = t;
5734
5735 /* Make it its own variant. */
5736 TYPE_MAIN_VARIANT (t) = t;
5737 TYPE_NEXT_VARIANT (t) = 0;
5738
5739 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
5740 whose TREE_TYPE is not t. This can also happen in the Ada
5741 frontend when using subtypes. */
5742
5743 return t;
5744}
5745
5746/* Create a new variant of TYPE, equivalent but distinct. This is so
5747 the caller can modify it. TYPE_CANONICAL for the return type will
5748 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
5749 are considered equal by the language itself (or that both types
5750 require structural equality checks). */
5751
5752tree
5753build_variant_type_copy (tree type MEM_STAT_DECL)
5754{
5755 tree t, m = TYPE_MAIN_VARIANT (type);
5756
5757 t = build_distinct_type_copy (type PASS_MEM_STAT);
5758
5759 /* Since we're building a variant, assume that it is a non-semantic
5760 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
5761 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
5762 /* Type variants have no alias set defined. */
5763 TYPE_ALIAS_SET (t) = -1;
5764
5765 /* Add the new type to the chain of variants of TYPE. */
5766 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
5767 TYPE_NEXT_VARIANT (m) = t;
5768 TYPE_MAIN_VARIANT (t) = m;
5769
5770 return t;
5771}
5772
5773/* Return true if the from tree in both tree maps are equal. */
5774
5775int
5776tree_map_base_eq (const void *va, const void *vb)
5777{
5778 const struct tree_map_base *const a = (const struct tree_map_base *) va,
5779 *const b = (const struct tree_map_base *) vb;
5780 return (a->from == b->from);
5781}
5782
5783/* Hash a from tree in a tree_base_map. */
5784
5785unsigned int
5786tree_map_base_hash (const void *item)
5787{
5788 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
5789}
5790
5791/* Return true if this tree map structure is marked for garbage collection
5792 purposes. We simply return true if the from tree is marked, so that this
5793 structure goes away when the from tree goes away. */
5794
5795bool
5796tree_map_base_marked_p (const void *p)
5797{
5798 return ggc_marked_p (((const struct tree_map_base *) p)->from);
5799}
5800
5801/* Hash a from tree in a tree_map. */
5802
5803unsigned int
5804tree_map_hash (const void *item)
5805{
5806 return (((const struct tree_map *) item)->hash);
5807}
5808
5809/* Hash a from tree in a tree_decl_map. */
5810
5811unsigned int
5812tree_decl_map_hash (const void *item)
5813{
5814 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
5815}
5816
5817/* Return the initialization priority for DECL. */
5818
5819priority_type
5820decl_init_priority_lookup (tree decl)
5821{
5822 symtab_node *snode = symtab_node::get (decl);
5823
5824 if (!snode)
5825 return DEFAULT_INIT_PRIORITY;
5826 return
5827 snode->get_init_priority ();
5828}
5829
5830/* Return the finalization priority for DECL. */
5831
5832priority_type
5833decl_fini_priority_lookup (tree decl)
5834{
5835 cgraph_node *node = cgraph_node::get (decl);
5836
5837 if (!node)
5838 return DEFAULT_INIT_PRIORITY;
5839 return
5840 node->get_fini_priority ();
5841}
5842
5843/* Set the initialization priority for DECL to PRIORITY. */
5844
5845void
5846decl_init_priority_insert (tree decl, priority_type priority)
5847{
5848 struct symtab_node *snode;
5849
5850 if (priority == DEFAULT_INIT_PRIORITY)
5851 {
5852 snode = symtab_node::get (decl);
5853 if (!snode)
5854 return;
5855 }
5856 else if (VAR_P (decl))
5857 snode = varpool_node::get_create (decl);
5858 else
5859 snode = cgraph_node::get_create (decl);
5860 snode->set_init_priority (priority);
5861}
5862
5863/* Set the finalization priority for DECL to PRIORITY. */
5864
5865void
5866decl_fini_priority_insert (tree decl, priority_type priority)
5867{
5868 struct cgraph_node *node;
5869
5870 if (priority == DEFAULT_INIT_PRIORITY)
5871 {
5872 node = cgraph_node::get (decl);
5873 if (!node)
5874 return;
5875 }
5876 else
5877 node = cgraph_node::get_create (decl);
5878 node->set_fini_priority (priority);
5879}
5880
5881/* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
5882
5883static void
5884print_debug_expr_statistics (void)
5885{
5886 fprintf (stderr, format: "DECL_DEBUG_EXPR hash: size " HOST_SIZE_T_PRINT_DEC ", "
5887 HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n",
5888 (fmt_size_t) debug_expr_for_decl->size (),
5889 (fmt_size_t) debug_expr_for_decl->elements (),
5890 debug_expr_for_decl->collisions ());
5891}
5892
5893/* Print out the statistics for the DECL_VALUE_EXPR hash table. */
5894
5895static void
5896print_value_expr_statistics (void)
5897{
5898 fprintf (stderr, format: "DECL_VALUE_EXPR hash: size " HOST_SIZE_T_PRINT_DEC ", "
5899 HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n",
5900 (fmt_size_t) value_expr_for_decl->size (),
5901 (fmt_size_t) value_expr_for_decl->elements (),
5902 value_expr_for_decl->collisions ());
5903}
5904
5905/* Lookup a debug expression for FROM, and return it if we find one. */
5906
5907tree
5908decl_debug_expr_lookup (tree from)
5909{
5910 struct tree_decl_map *h, in;
5911 in.base.from = from;
5912
5913 h = debug_expr_for_decl->find_with_hash (comparable: &in, DECL_UID (from));
5914 if (h)
5915 return h->to;
5916 return NULL_TREE;
5917}
5918
5919/* Insert a mapping FROM->TO in the debug expression hashtable. */
5920
5921void
5922decl_debug_expr_insert (tree from, tree to)
5923{
5924 struct tree_decl_map *h;
5925
5926 h = ggc_alloc<tree_decl_map> ();
5927 h->base.from = from;
5928 h->to = to;
5929 *debug_expr_for_decl->find_slot_with_hash (comparable: h, DECL_UID (from), insert: INSERT) = h;
5930}
5931
5932/* Lookup a value expression for FROM, and return it if we find one. */
5933
5934tree
5935decl_value_expr_lookup (tree from)
5936{
5937 struct tree_decl_map *h, in;
5938 in.base.from = from;
5939
5940 h = value_expr_for_decl->find_with_hash (comparable: &in, DECL_UID (from));
5941 if (h)
5942 return h->to;
5943 return NULL_TREE;
5944}
5945
5946/* Insert a mapping FROM->TO in the value expression hashtable. */
5947
5948void
5949decl_value_expr_insert (tree from, tree to)
5950{
5951 struct tree_decl_map *h;
5952
5953 /* Uses of FROM shouldn't look like they happen at the location of TO. */
5954 to = protected_set_expr_location_unshare (to, UNKNOWN_LOCATION);
5955
5956 h = ggc_alloc<tree_decl_map> ();
5957 h->base.from = from;
5958 h->to = to;
5959 *value_expr_for_decl->find_slot_with_hash (comparable: h, DECL_UID (from), insert: INSERT) = h;
5960}
5961
5962/* Lookup a vector of debug arguments for FROM, and return it if we
5963 find one. */
5964
5965vec<tree, va_gc> **
5966decl_debug_args_lookup (tree from)
5967{
5968 struct tree_vec_map *h, in;
5969
5970 if (!DECL_HAS_DEBUG_ARGS_P (from))
5971 return NULL;
5972 gcc_checking_assert (debug_args_for_decl != NULL);
5973 in.base.from = from;
5974 h = debug_args_for_decl->find_with_hash (comparable: &in, DECL_UID (from));
5975 if (h)
5976 return &h->to;
5977 return NULL;
5978}
5979
5980/* Insert a mapping FROM->empty vector of debug arguments in the value
5981 expression hashtable. */
5982
5983vec<tree, va_gc> **
5984decl_debug_args_insert (tree from)
5985{
5986 struct tree_vec_map *h;
5987 tree_vec_map **loc;
5988
5989 if (DECL_HAS_DEBUG_ARGS_P (from))
5990 return decl_debug_args_lookup (from);
5991 if (debug_args_for_decl == NULL)
5992 debug_args_for_decl = hash_table<tree_vec_map_cache_hasher>::create_ggc (n: 64);
5993 h = ggc_alloc<tree_vec_map> ();
5994 h->base.from = from;
5995 h->to = NULL;
5996 loc = debug_args_for_decl->find_slot_with_hash (comparable: h, DECL_UID (from), insert: INSERT);
5997 *loc = h;
5998 DECL_HAS_DEBUG_ARGS_P (from) = 1;
5999 return &h->to;
6000}
6001
6002/* Hashing of types so that we don't make duplicates.
6003 The entry point is `type_hash_canon'. */
6004
6005/* Generate the default hash code for TYPE. This is designed for
6006 speed, rather than maximum entropy. */
6007
6008hashval_t
6009type_hash_canon_hash (tree type)
6010{
6011 inchash::hash hstate;
6012
6013 hstate.add_int (TREE_CODE (type));
6014
6015 if (TREE_TYPE (type))
6016 hstate.add_object (TYPE_HASH (TREE_TYPE (type)));
6017
6018 for (tree t = TYPE_ATTRIBUTES (type); t; t = TREE_CHAIN (t))
6019 /* Just the identifier is adequate to distinguish. */
6020 hstate.add_object (IDENTIFIER_HASH_VALUE (get_attribute_name (t)));
6021
6022 switch (TREE_CODE (type))
6023 {
6024 case METHOD_TYPE:
6025 hstate.add_object (TYPE_HASH (TYPE_METHOD_BASETYPE (type)));
6026 /* FALLTHROUGH. */
6027 case FUNCTION_TYPE:
6028 for (tree t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6029 if (TREE_VALUE (t) != error_mark_node)
6030 hstate.add_object (TYPE_HASH (TREE_VALUE (t)));
6031 break;
6032
6033 case OFFSET_TYPE:
6034 hstate.add_object (TYPE_HASH (TYPE_OFFSET_BASETYPE (type)));
6035 break;
6036
6037 case ARRAY_TYPE:
6038 {
6039 if (TYPE_DOMAIN (type))
6040 hstate.add_object (TYPE_HASH (TYPE_DOMAIN (type)));
6041 if (!AGGREGATE_TYPE_P (TREE_TYPE (type)))
6042 {
6043 unsigned typeless = TYPE_TYPELESS_STORAGE (type);
6044 hstate.add_object (obj&: typeless);
6045 }
6046 }
6047 break;
6048
6049 case INTEGER_TYPE:
6050 {
6051 tree t = TYPE_MAX_VALUE (type);
6052 if (!t)
6053 t = TYPE_MIN_VALUE (type);
6054 for (int i = 0; i < TREE_INT_CST_NUNITS (t); i++)
6055 hstate.add_object (TREE_INT_CST_ELT (t, i));
6056 break;
6057 }
6058
6059 case BITINT_TYPE:
6060 {
6061 unsigned prec = TYPE_PRECISION (type);
6062 unsigned uns = TYPE_UNSIGNED (type);
6063 hstate.add_object (obj&: prec);
6064 hstate.add_int (v: uns);
6065 break;
6066 }
6067
6068 case REAL_TYPE:
6069 case FIXED_POINT_TYPE:
6070 {
6071 unsigned prec = TYPE_PRECISION (type);
6072 hstate.add_object (obj&: prec);
6073 break;
6074 }
6075
6076 case VECTOR_TYPE:
6077 hstate.add_poly_int (v: TYPE_VECTOR_SUBPARTS (node: type));
6078 break;
6079
6080 default:
6081 break;
6082 }
6083
6084 return hstate.end ();
6085}
6086
6087/* These are the Hashtable callback functions. */
6088
6089/* Returns true iff the types are equivalent. */
6090
6091bool
6092type_cache_hasher::equal (type_hash *a, type_hash *b)
6093{
6094 /* First test the things that are the same for all types. */
6095 if (a->hash != b->hash
6096 || TREE_CODE (a->type) != TREE_CODE (b->type)
6097 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6098 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6099 TYPE_ATTRIBUTES (b->type))
6100 || (TREE_CODE (a->type) != COMPLEX_TYPE
6101 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6102 return false;
6103
6104 /* Be careful about comparing arrays before and after the element type
6105 has been completed; don't compare TYPE_ALIGN unless both types are
6106 complete. */
6107 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6108 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6109 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6110 return false;
6111
6112 switch (TREE_CODE (a->type))
6113 {
6114 case VOID_TYPE:
6115 case OPAQUE_TYPE:
6116 case COMPLEX_TYPE:
6117 case POINTER_TYPE:
6118 case REFERENCE_TYPE:
6119 case NULLPTR_TYPE:
6120 return true;
6121
6122 case VECTOR_TYPE:
6123 return known_eq (TYPE_VECTOR_SUBPARTS (a->type),
6124 TYPE_VECTOR_SUBPARTS (b->type));
6125
6126 case ENUMERAL_TYPE:
6127 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6128 && !(TYPE_VALUES (a->type)
6129 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6130 && TYPE_VALUES (b->type)
6131 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6132 && type_list_equal (TYPE_VALUES (a->type),
6133 TYPE_VALUES (b->type))))
6134 return false;
6135
6136 /* fall through */
6137
6138 case INTEGER_TYPE:
6139 case REAL_TYPE:
6140 case BOOLEAN_TYPE:
6141 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
6142 return false;
6143 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6144 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6145 TYPE_MAX_VALUE (b->type)))
6146 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6147 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6148 TYPE_MIN_VALUE (b->type))));
6149
6150 case BITINT_TYPE:
6151 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
6152 return false;
6153 return TYPE_UNSIGNED (a->type) == TYPE_UNSIGNED (b->type);
6154
6155 case FIXED_POINT_TYPE:
6156 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6157
6158 case OFFSET_TYPE:
6159 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6160
6161 case METHOD_TYPE:
6162 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6163 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6164 || (TYPE_ARG_TYPES (a->type)
6165 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6166 && TYPE_ARG_TYPES (b->type)
6167 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6168 && type_list_equal (TYPE_ARG_TYPES (a->type),
6169 TYPE_ARG_TYPES (b->type)))))
6170 break;
6171 return false;
6172 case ARRAY_TYPE:
6173 /* Don't compare TYPE_TYPELESS_STORAGE flag on aggregates,
6174 where the flag should be inherited from the element type
6175 and can change after ARRAY_TYPEs are created; on non-aggregates
6176 compare it and hash it, scalars will never have that flag set
6177 and we need to differentiate between arrays created by different
6178 front-ends or middle-end created arrays. */
6179 return (TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type)
6180 && (AGGREGATE_TYPE_P (TREE_TYPE (a->type))
6181 || (TYPE_TYPELESS_STORAGE (a->type)
6182 == TYPE_TYPELESS_STORAGE (b->type))));
6183
6184 case RECORD_TYPE:
6185 case UNION_TYPE:
6186 case QUAL_UNION_TYPE:
6187 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6188 || (TYPE_FIELDS (a->type)
6189 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6190 && TYPE_FIELDS (b->type)
6191 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6192 && type_list_equal (TYPE_FIELDS (a->type),
6193 TYPE_FIELDS (b->type))));
6194
6195 case FUNCTION_TYPE:
6196 if ((TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6197 && (TYPE_NO_NAMED_ARGS_STDARG_P (a->type)
6198 == TYPE_NO_NAMED_ARGS_STDARG_P (b->type)))
6199 || (TYPE_ARG_TYPES (a->type)
6200 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6201 && TYPE_ARG_TYPES (b->type)
6202 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6203 && type_list_equal (TYPE_ARG_TYPES (a->type),
6204 TYPE_ARG_TYPES (b->type))))
6205 break;
6206 return false;
6207
6208 default:
6209 return false;
6210 }
6211
6212 if (lang_hooks.types.type_hash_eq != NULL)
6213 return lang_hooks.types.type_hash_eq (a->type, b->type);
6214
6215 return true;
6216}
6217
6218/* Given TYPE, and HASHCODE its hash code, return the canonical
6219 object for an identical type if one already exists.
6220 Otherwise, return TYPE, and record it as the canonical object.
6221
6222 To use this function, first create a type of the sort you want.
6223 Then compute its hash code from the fields of the type that
6224 make it different from other similar types.
6225 Then call this function and use the value. */
6226
6227tree
6228type_hash_canon (unsigned int hashcode, tree type)
6229{
6230 type_hash in;
6231 type_hash **loc;
6232
6233 /* The hash table only contains main variants, so ensure that's what we're
6234 being passed. */
6235 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6236
6237 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6238 must call that routine before comparing TYPE_ALIGNs. */
6239 layout_type (type);
6240
6241 in.hash = hashcode;
6242 in.type = type;
6243
6244 loc = type_hash_table->find_slot_with_hash (comparable: &in, hash: hashcode, insert: INSERT);
6245 if (*loc)
6246 {
6247 tree t1 = ((type_hash *) *loc)->type;
6248 gcc_assert (TYPE_MAIN_VARIANT (t1) == t1
6249 && t1 != type);
6250 if (TYPE_UID (type) + 1 == next_type_uid)
6251 --next_type_uid;
6252 /* Free also min/max values and the cache for integer
6253 types. This can't be done in free_node, as LTO frees
6254 those on its own. */
6255 if (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == BITINT_TYPE)
6256 {
6257 if (TYPE_MIN_VALUE (type)
6258 && TREE_TYPE (TYPE_MIN_VALUE (type)) == type)
6259 {
6260 /* Zero is always in TYPE_CACHED_VALUES. */
6261 if (! TYPE_UNSIGNED (type))
6262 int_cst_hash_table->remove_elt (TYPE_MIN_VALUE (type));
6263 ggc_free (TYPE_MIN_VALUE (type));
6264 }
6265 if (TYPE_MAX_VALUE (type)
6266 && TREE_TYPE (TYPE_MAX_VALUE (type)) == type)
6267 {
6268 int_cst_hash_table->remove_elt (TYPE_MAX_VALUE (type));
6269 ggc_free (TYPE_MAX_VALUE (type));
6270 }
6271 if (TYPE_CACHED_VALUES_P (type))
6272 ggc_free (TYPE_CACHED_VALUES (type));
6273 }
6274 free_node (node: type);
6275 return t1;
6276 }
6277 else
6278 {
6279 struct type_hash *h;
6280
6281 h = ggc_alloc<type_hash> ();
6282 h->hash = hashcode;
6283 h->type = type;
6284 *loc = h;
6285
6286 return type;
6287 }
6288}
6289
6290static void
6291print_type_hash_statistics (void)
6292{
6293 fprintf (stderr, format: "Type hash: size " HOST_SIZE_T_PRINT_DEC ", "
6294 HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n",
6295 (fmt_size_t) type_hash_table->size (),
6296 (fmt_size_t) type_hash_table->elements (),
6297 type_hash_table->collisions ());
6298}
6299
6300/* Given two lists of types
6301 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6302 return 1 if the lists contain the same types in the same order.
6303 Also, the TREE_PURPOSEs must match. */
6304
6305bool
6306type_list_equal (const_tree l1, const_tree l2)
6307{
6308 const_tree t1, t2;
6309
6310 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6311 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6312 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6313 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6314 && (TREE_TYPE (TREE_PURPOSE (t1))
6315 == TREE_TYPE (TREE_PURPOSE (t2))))))
6316 return false;
6317
6318 return t1 == t2;
6319}
6320
6321/* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6322 given by TYPE. If the argument list accepts variable arguments,
6323 then this function counts only the ordinary arguments. */
6324
6325int
6326type_num_arguments (const_tree fntype)
6327{
6328 int i = 0;
6329
6330 for (tree t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
6331 /* If the function does not take a variable number of arguments,
6332 the last element in the list will have type `void'. */
6333 if (VOID_TYPE_P (TREE_VALUE (t)))
6334 break;
6335 else
6336 ++i;
6337
6338 return i;
6339}
6340
6341/* Return the type of the function TYPE's argument ARGNO if known.
6342 For vararg function's where ARGNO refers to one of the variadic
6343 arguments return null. Otherwise, return a void_type_node for
6344 out-of-bounds ARGNO. */
6345
6346tree
6347type_argument_type (const_tree fntype, unsigned argno)
6348{
6349 /* Treat zero the same as an out-of-bounds argument number. */
6350 if (!argno)
6351 return void_type_node;
6352
6353 function_args_iterator iter;
6354
6355 tree argtype;
6356 unsigned i = 1;
6357 FOREACH_FUNCTION_ARGS (fntype, argtype, iter)
6358 {
6359 /* A vararg function's argument list ends in a null. Otherwise,
6360 an ordinary function's argument list ends with void. Return
6361 null if ARGNO refers to a vararg argument, void_type_node if
6362 it's out of bounds, and the formal argument type otherwise. */
6363 if (!argtype)
6364 break;
6365
6366 if (i == argno || VOID_TYPE_P (argtype))
6367 return argtype;
6368
6369 ++i;
6370 }
6371
6372 return NULL_TREE;
6373}
6374
6375/* True if integer constants T1 and T2
6376 represent the same constant value. */
6377
6378bool
6379tree_int_cst_equal (const_tree t1, const_tree t2)
6380{
6381 if (t1 == t2)
6382 return true;
6383
6384 if (t1 == 0 || t2 == 0)
6385 return false;
6386
6387 STRIP_ANY_LOCATION_WRAPPER (t1);
6388 STRIP_ANY_LOCATION_WRAPPER (t2);
6389
6390 if (TREE_CODE (t1) == INTEGER_CST
6391 && TREE_CODE (t2) == INTEGER_CST
6392 && wi::to_widest (t: t1) == wi::to_widest (t: t2))
6393 return true;
6394
6395 return false;
6396}
6397
6398/* Return true if T is an INTEGER_CST whose numerical value (extended
6399 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
6400
6401bool
6402tree_fits_shwi_p (const_tree t)
6403{
6404 return (t != NULL_TREE
6405 && TREE_CODE (t) == INTEGER_CST
6406 && wi::fits_shwi_p (x: wi::to_widest (t)));
6407}
6408
6409/* Return true if T is an INTEGER_CST or POLY_INT_CST whose numerical
6410 value (extended according to TYPE_UNSIGNED) fits in a poly_int64. */
6411
6412bool
6413tree_fits_poly_int64_p (const_tree t)
6414{
6415 if (t == NULL_TREE)
6416 return false;
6417 if (POLY_INT_CST_P (t))
6418 {
6419 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; i++)
6420 if (!wi::fits_shwi_p (x: wi::to_wide (POLY_INT_CST_COEFF (t, i))))
6421 return false;
6422 return true;
6423 }
6424 return (TREE_CODE (t) == INTEGER_CST
6425 && wi::fits_shwi_p (x: wi::to_widest (t)));
6426}
6427
6428/* Return true if T is an INTEGER_CST whose numerical value (extended
6429 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
6430
6431bool
6432tree_fits_uhwi_p (const_tree t)
6433{
6434 return (t != NULL_TREE
6435 && TREE_CODE (t) == INTEGER_CST
6436 && wi::fits_uhwi_p (x: wi::to_widest (t)));
6437}
6438
6439/* Return true if T is an INTEGER_CST or POLY_INT_CST whose numerical
6440 value (extended according to TYPE_UNSIGNED) fits in a poly_uint64. */
6441
6442bool
6443tree_fits_poly_uint64_p (const_tree t)
6444{
6445 if (t == NULL_TREE)
6446 return false;
6447 if (POLY_INT_CST_P (t))
6448 {
6449 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; i++)
6450 if (!wi::fits_uhwi_p (x: wi::to_widest (POLY_INT_CST_COEFF (t, i))))
6451 return false;
6452 return true;
6453 }
6454 return (TREE_CODE (t) == INTEGER_CST
6455 && wi::fits_uhwi_p (x: wi::to_widest (t)));
6456}
6457
6458/* T is an INTEGER_CST whose numerical value (extended according to
6459 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
6460 HOST_WIDE_INT. */
6461
6462HOST_WIDE_INT
6463tree_to_shwi (const_tree t)
6464{
6465 gcc_assert (tree_fits_shwi_p (t));
6466 return TREE_INT_CST_LOW (t);
6467}
6468
6469/* T is an INTEGER_CST whose numerical value (extended according to
6470 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
6471 HOST_WIDE_INT. */
6472
6473unsigned HOST_WIDE_INT
6474tree_to_uhwi (const_tree t)
6475{
6476 gcc_assert (tree_fits_uhwi_p (t));
6477 return TREE_INT_CST_LOW (t);
6478}
6479
6480/* Return the most significant (sign) bit of T. */
6481
6482int
6483tree_int_cst_sign_bit (const_tree t)
6484{
6485 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
6486
6487 return wi::extract_uhwi (x: wi::to_wide (t), bitpos: bitno, width: 1);
6488}
6489
6490/* Return an indication of the sign of the integer constant T.
6491 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
6492 Note that -1 will never be returned if T's type is unsigned. */
6493
6494int
6495tree_int_cst_sgn (const_tree t)
6496{
6497 if (wi::to_wide (t) == 0)
6498 return 0;
6499 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
6500 return 1;
6501 else if (wi::neg_p (x: wi::to_wide (t)))
6502 return -1;
6503 else
6504 return 1;
6505}
6506
6507/* Return the minimum number of bits needed to represent VALUE in a
6508 signed or unsigned type, UNSIGNEDP says which. */
6509
6510unsigned int
6511tree_int_cst_min_precision (tree value, signop sgn)
6512{
6513 /* If the value is negative, compute its negative minus 1. The latter
6514 adjustment is because the absolute value of the largest negative value
6515 is one larger than the largest positive value. This is equivalent to
6516 a bit-wise negation, so use that operation instead. */
6517
6518 if (tree_int_cst_sgn (t: value) < 0)
6519 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
6520
6521 /* Return the number of bits needed, taking into account the fact
6522 that we need one more bit for a signed than unsigned type.
6523 If value is 0 or -1, the minimum precision is 1 no matter
6524 whether unsignedp is true or false. */
6525
6526 if (integer_zerop (expr: value))
6527 return 1;
6528 else
6529 return tree_floor_log2 (expr: value) + 1 + (sgn == SIGNED ? 1 : 0) ;
6530}
6531
6532/* Return truthvalue of whether T1 is the same tree structure as T2.
6533 Return 1 if they are the same.
6534 Return 0 if they are understandably different.
6535 Return -1 if either contains tree structure not understood by
6536 this function. */
6537
6538int
6539simple_cst_equal (const_tree t1, const_tree t2)
6540{
6541 enum tree_code code1, code2;
6542 int cmp;
6543 int i;
6544
6545 if (t1 == t2)
6546 return 1;
6547 if (t1 == 0 || t2 == 0)
6548 return 0;
6549
6550 /* For location wrappers to be the same, they must be at the same
6551 source location (and wrap the same thing). */
6552 if (location_wrapper_p (exp: t1) && location_wrapper_p (exp: t2))
6553 {
6554 if (EXPR_LOCATION (t1) != EXPR_LOCATION (t2))
6555 return 0;
6556 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6557 }
6558
6559 code1 = TREE_CODE (t1);
6560 code2 = TREE_CODE (t2);
6561
6562 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
6563 {
6564 if (CONVERT_EXPR_CODE_P (code2)
6565 || code2 == NON_LVALUE_EXPR)
6566 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6567 else
6568 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
6569 }
6570
6571 else if (CONVERT_EXPR_CODE_P (code2)
6572 || code2 == NON_LVALUE_EXPR)
6573 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
6574
6575 if (code1 != code2)
6576 return 0;
6577
6578 switch (code1)
6579 {
6580 case INTEGER_CST:
6581 return wi::to_widest (t: t1) == wi::to_widest (t: t2);
6582
6583 case REAL_CST:
6584 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
6585
6586 case FIXED_CST:
6587 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
6588
6589 case STRING_CST:
6590 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
6591 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
6592 TREE_STRING_LENGTH (t1)));
6593
6594 case CONSTRUCTOR:
6595 {
6596 unsigned HOST_WIDE_INT idx;
6597 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
6598 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
6599
6600 if (vec_safe_length (v: v1) != vec_safe_length (v: v2))
6601 return false;
6602
6603 for (idx = 0; idx < vec_safe_length (v: v1); ++idx)
6604 /* ??? Should we handle also fields here? */
6605 if (!simple_cst_equal (t1: (*v1)[idx].value, t2: (*v2)[idx].value))
6606 return false;
6607 return true;
6608 }
6609
6610 case SAVE_EXPR:
6611 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6612
6613 case CALL_EXPR:
6614 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
6615 if (cmp <= 0)
6616 return cmp;
6617 if (call_expr_nargs (t1) != call_expr_nargs (t2))
6618 return 0;
6619 {
6620 const_tree arg1, arg2;
6621 const_call_expr_arg_iterator iter1, iter2;
6622 for (arg1 = first_const_call_expr_arg (exp: t1, iter: &iter1),
6623 arg2 = first_const_call_expr_arg (exp: t2, iter: &iter2);
6624 arg1 && arg2;
6625 arg1 = next_const_call_expr_arg (iter: &iter1),
6626 arg2 = next_const_call_expr_arg (iter: &iter2))
6627 {
6628 cmp = simple_cst_equal (t1: arg1, t2: arg2);
6629 if (cmp <= 0)
6630 return cmp;
6631 }
6632 return arg1 == arg2;
6633 }
6634
6635 case TARGET_EXPR:
6636 /* Special case: if either target is an unallocated VAR_DECL,
6637 it means that it's going to be unified with whatever the
6638 TARGET_EXPR is really supposed to initialize, so treat it
6639 as being equivalent to anything. */
6640 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
6641 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
6642 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
6643 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
6644 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
6645 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
6646 cmp = 1;
6647 else
6648 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6649
6650 if (cmp <= 0)
6651 return cmp;
6652
6653 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
6654
6655 case WITH_CLEANUP_EXPR:
6656 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6657 if (cmp <= 0)
6658 return cmp;
6659
6660 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
6661
6662 case COMPONENT_REF:
6663 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
6664 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6665
6666 return 0;
6667
6668 case VAR_DECL:
6669 case PARM_DECL:
6670 case CONST_DECL:
6671 case FUNCTION_DECL:
6672 return 0;
6673
6674 default:
6675 if (POLY_INT_CST_P (t1))
6676 /* A false return means maybe_ne rather than known_ne. */
6677 return known_eq (poly_widest_int::from (poly_int_cst_value (t1),
6678 TYPE_SIGN (TREE_TYPE (t1))),
6679 poly_widest_int::from (poly_int_cst_value (t2),
6680 TYPE_SIGN (TREE_TYPE (t2))));
6681 break;
6682 }
6683
6684 /* This general rule works for most tree codes. All exceptions should be
6685 handled above. If this is a language-specific tree code, we can't
6686 trust what might be in the operand, so say we don't know
6687 the situation. */
6688 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
6689 return -1;
6690
6691 switch (TREE_CODE_CLASS (code1))
6692 {
6693 case tcc_unary:
6694 case tcc_binary:
6695 case tcc_comparison:
6696 case tcc_expression:
6697 case tcc_reference:
6698 case tcc_statement:
6699 cmp = 1;
6700 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
6701 {
6702 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
6703 if (cmp <= 0)
6704 return cmp;
6705 }
6706
6707 return cmp;
6708
6709 default:
6710 return -1;
6711 }
6712}
6713
6714/* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
6715 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
6716 than U, respectively. */
6717
6718int
6719compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
6720{
6721 if (tree_int_cst_sgn (t) < 0)
6722 return -1;
6723 else if (!tree_fits_uhwi_p (t))
6724 return 1;
6725 else if (TREE_INT_CST_LOW (t) == u)
6726 return 0;
6727 else if (TREE_INT_CST_LOW (t) < u)
6728 return -1;
6729 else
6730 return 1;
6731}
6732
6733/* Return true if SIZE represents a constant size that is in bounds of
6734 what the middle-end and the backend accepts (covering not more than
6735 half of the address-space).
6736 When PERR is non-null, set *PERR on failure to the description of
6737 why SIZE is not valid. */
6738
6739bool
6740valid_constant_size_p (const_tree size, cst_size_error *perr /* = NULL */)
6741{
6742 if (POLY_INT_CST_P (size))
6743 {
6744 if (TREE_OVERFLOW (size))
6745 return false;
6746 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
6747 if (!valid_constant_size_p (POLY_INT_CST_COEFF (size, i)))
6748 return false;
6749 return true;
6750 }
6751
6752 cst_size_error error;
6753 if (!perr)
6754 perr = &error;
6755
6756 if (TREE_CODE (size) != INTEGER_CST)
6757 {
6758 *perr = cst_size_not_constant;
6759 return false;
6760 }
6761
6762 if (TREE_OVERFLOW_P (size))
6763 {
6764 *perr = cst_size_overflow;
6765 return false;
6766 }
6767
6768 if (tree_int_cst_sgn (t: size) < 0)
6769 {
6770 *perr = cst_size_negative;
6771 return false;
6772 }
6773 if (!tree_fits_uhwi_p (t: size)
6774 || (wi::to_widest (TYPE_MAX_VALUE (sizetype))
6775 < wi::to_widest (t: size) * 2))
6776 {
6777 *perr = cst_size_too_big;
6778 return false;
6779 }
6780
6781 return true;
6782}
6783
6784/* Return the precision of the type, or for a complex or vector type the
6785 precision of the type of its elements. */
6786
6787unsigned int
6788element_precision (const_tree type)
6789{
6790 if (!TYPE_P (type))
6791 type = TREE_TYPE (type);
6792 enum tree_code code = TREE_CODE (type);
6793 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
6794 type = TREE_TYPE (type);
6795
6796 return TYPE_PRECISION (type);
6797}
6798
6799/* Return true if CODE represents an associative tree code. Otherwise
6800 return false. */
6801bool
6802associative_tree_code (enum tree_code code)
6803{
6804 switch (code)
6805 {
6806 case BIT_IOR_EXPR:
6807 case BIT_AND_EXPR:
6808 case BIT_XOR_EXPR:
6809 case PLUS_EXPR:
6810 case MULT_EXPR:
6811 case MIN_EXPR:
6812 case MAX_EXPR:
6813 return true;
6814
6815 default:
6816 break;
6817 }
6818 return false;
6819}
6820
6821/* Return true if CODE represents a commutative tree code. Otherwise
6822 return false. */
6823bool
6824commutative_tree_code (enum tree_code code)
6825{
6826 switch (code)
6827 {
6828 case PLUS_EXPR:
6829 case MULT_EXPR:
6830 case MULT_HIGHPART_EXPR:
6831 case MIN_EXPR:
6832 case MAX_EXPR:
6833 case BIT_IOR_EXPR:
6834 case BIT_XOR_EXPR:
6835 case BIT_AND_EXPR:
6836 case NE_EXPR:
6837 case EQ_EXPR:
6838 case UNORDERED_EXPR:
6839 case ORDERED_EXPR:
6840 case UNEQ_EXPR:
6841 case LTGT_EXPR:
6842 case TRUTH_AND_EXPR:
6843 case TRUTH_XOR_EXPR:
6844 case TRUTH_OR_EXPR:
6845 case WIDEN_MULT_EXPR:
6846 case VEC_WIDEN_MULT_HI_EXPR:
6847 case VEC_WIDEN_MULT_LO_EXPR:
6848 case VEC_WIDEN_MULT_EVEN_EXPR:
6849 case VEC_WIDEN_MULT_ODD_EXPR:
6850 return true;
6851
6852 default:
6853 break;
6854 }
6855 return false;
6856}
6857
6858/* Return true if CODE represents a ternary tree code for which the
6859 first two operands are commutative. Otherwise return false. */
6860bool
6861commutative_ternary_tree_code (enum tree_code code)
6862{
6863 switch (code)
6864 {
6865 case WIDEN_MULT_PLUS_EXPR:
6866 case WIDEN_MULT_MINUS_EXPR:
6867 case DOT_PROD_EXPR:
6868 return true;
6869
6870 default:
6871 break;
6872 }
6873 return false;
6874}
6875
6876/* Returns true if CODE can overflow. */
6877
6878bool
6879operation_can_overflow (enum tree_code code)
6880{
6881 switch (code)
6882 {
6883 case PLUS_EXPR:
6884 case MINUS_EXPR:
6885 case MULT_EXPR:
6886 case LSHIFT_EXPR:
6887 /* Can overflow in various ways. */
6888 return true;
6889 case TRUNC_DIV_EXPR:
6890 case EXACT_DIV_EXPR:
6891 case FLOOR_DIV_EXPR:
6892 case CEIL_DIV_EXPR:
6893 /* For INT_MIN / -1. */
6894 return true;
6895 case NEGATE_EXPR:
6896 case ABS_EXPR:
6897 /* For -INT_MIN. */
6898 return true;
6899 default:
6900 /* These operators cannot overflow. */
6901 return false;
6902 }
6903}
6904
6905/* Returns true if CODE operating on operands of type TYPE doesn't overflow, or
6906 ftrapv doesn't generate trapping insns for CODE. */
6907
6908bool
6909operation_no_trapping_overflow (tree type, enum tree_code code)
6910{
6911 gcc_checking_assert (ANY_INTEGRAL_TYPE_P (type));
6912
6913 /* We don't generate instructions that trap on overflow for complex or vector
6914 types. */
6915 if (!INTEGRAL_TYPE_P (type))
6916 return true;
6917
6918 if (!TYPE_OVERFLOW_TRAPS (type))
6919 return true;
6920
6921 switch (code)
6922 {
6923 case PLUS_EXPR:
6924 case MINUS_EXPR:
6925 case MULT_EXPR:
6926 case NEGATE_EXPR:
6927 case ABS_EXPR:
6928 /* These operators can overflow, and -ftrapv generates trapping code for
6929 these. */
6930 return false;
6931 case TRUNC_DIV_EXPR:
6932 case EXACT_DIV_EXPR:
6933 case FLOOR_DIV_EXPR:
6934 case CEIL_DIV_EXPR:
6935 case LSHIFT_EXPR:
6936 /* These operators can overflow, but -ftrapv does not generate trapping
6937 code for these. */
6938 return true;
6939 default:
6940 /* These operators cannot overflow. */
6941 return true;
6942 }
6943}
6944
6945/* Constructors for pointer, array and function types.
6946 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
6947 constructed by language-dependent code, not here.) */
6948
6949/* Construct, lay out and return the type of pointers to TO_TYPE with
6950 mode MODE. If MODE is VOIDmode, a pointer mode for the address
6951 space of TO_TYPE will be picked. If CAN_ALIAS_ALL is TRUE,
6952 indicate this type can reference all of memory. If such a type has
6953 already been constructed, reuse it. */
6954
6955tree
6956build_pointer_type_for_mode (tree to_type, machine_mode mode,
6957 bool can_alias_all)
6958{
6959 tree t;
6960 bool could_alias = can_alias_all;
6961
6962 if (to_type == error_mark_node)
6963 return error_mark_node;
6964
6965 if (mode == VOIDmode)
6966 {
6967 addr_space_t as = TYPE_ADDR_SPACE (to_type);
6968 mode = targetm.addr_space.pointer_mode (as);
6969 }
6970
6971 /* If the pointed-to type has the may_alias attribute set, force
6972 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
6973 if (lookup_attribute (attr_name: "may_alias", TYPE_ATTRIBUTES (to_type)))
6974 can_alias_all = true;
6975
6976 /* In some cases, languages will have things that aren't a POINTER_TYPE
6977 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
6978 In that case, return that type without regard to the rest of our
6979 operands.
6980
6981 ??? This is a kludge, but consistent with the way this function has
6982 always operated and there doesn't seem to be a good way to avoid this
6983 at the moment. */
6984 if (TYPE_POINTER_TO (to_type) != 0
6985 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
6986 return TYPE_POINTER_TO (to_type);
6987
6988 /* First, if we already have a type for pointers to TO_TYPE and it's
6989 the proper mode, use it. */
6990 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
6991 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
6992 return t;
6993
6994 t = make_node (code: POINTER_TYPE);
6995
6996 TREE_TYPE (t) = to_type;
6997 SET_TYPE_MODE (t, mode);
6998 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
6999 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7000 TYPE_POINTER_TO (to_type) = t;
7001
7002 /* During LTO we do not set TYPE_CANONICAL of pointers and references. */
7003 if (TYPE_STRUCTURAL_EQUALITY_P (to_type) || in_lto_p)
7004 SET_TYPE_STRUCTURAL_EQUALITY (t);
7005 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
7006 TYPE_CANONICAL (t)
7007 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7008 mode, can_alias_all: false);
7009
7010 /* Lay out the type. This function has many callers that are concerned
7011 with expression-construction, and this simplifies them all. */
7012 layout_type (t);
7013
7014 return t;
7015}
7016
7017/* By default build pointers in ptr_mode. */
7018
7019tree
7020build_pointer_type (tree to_type)
7021{
7022 return build_pointer_type_for_mode (to_type, VOIDmode, can_alias_all: false);
7023}
7024
7025/* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7026
7027tree
7028build_reference_type_for_mode (tree to_type, machine_mode mode,
7029 bool can_alias_all)
7030{
7031 tree t;
7032 bool could_alias = can_alias_all;
7033
7034 if (to_type == error_mark_node)
7035 return error_mark_node;
7036
7037 if (mode == VOIDmode)
7038 {
7039 addr_space_t as = TYPE_ADDR_SPACE (to_type);
7040 mode = targetm.addr_space.pointer_mode (as);
7041 }
7042
7043 /* If the pointed-to type has the may_alias attribute set, force
7044 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7045 if (lookup_attribute (attr_name: "may_alias", TYPE_ATTRIBUTES (to_type)))
7046 can_alias_all = true;
7047
7048 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7049 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7050 In that case, return that type without regard to the rest of our
7051 operands.
7052
7053 ??? This is a kludge, but consistent with the way this function has
7054 always operated and there doesn't seem to be a good way to avoid this
7055 at the moment. */
7056 if (TYPE_REFERENCE_TO (to_type) != 0
7057 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7058 return TYPE_REFERENCE_TO (to_type);
7059
7060 /* First, if we already have a type for pointers to TO_TYPE and it's
7061 the proper mode, use it. */
7062 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7063 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7064 return t;
7065
7066 t = make_node (code: REFERENCE_TYPE);
7067
7068 TREE_TYPE (t) = to_type;
7069 SET_TYPE_MODE (t, mode);
7070 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7071 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7072 TYPE_REFERENCE_TO (to_type) = t;
7073
7074 /* During LTO we do not set TYPE_CANONICAL of pointers and references. */
7075 if (TYPE_STRUCTURAL_EQUALITY_P (to_type) || in_lto_p)
7076 SET_TYPE_STRUCTURAL_EQUALITY (t);
7077 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
7078 TYPE_CANONICAL (t)
7079 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7080 mode, can_alias_all: false);
7081
7082 layout_type (t);
7083
7084 return t;
7085}
7086
7087
7088/* Build the node for the type of references-to-TO_TYPE by default
7089 in ptr_mode. */
7090
7091tree
7092build_reference_type (tree to_type)
7093{
7094 return build_reference_type_for_mode (to_type, VOIDmode, can_alias_all: false);
7095}
7096
7097#define MAX_INT_CACHED_PREC \
7098 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7099static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7100
7101static void
7102clear_nonstandard_integer_type_cache (void)
7103{
7104 for (size_t i = 0 ; i < 2 * MAX_INT_CACHED_PREC + 2 ; i++)
7105 {
7106 nonstandard_integer_type_cache[i] = NULL;
7107 }
7108}
7109
7110/* Builds a signed or unsigned integer type of precision PRECISION.
7111 Used for C bitfields whose precision does not match that of
7112 built-in target types. */
7113tree
7114build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7115 int unsignedp)
7116{
7117 tree itype, ret;
7118
7119 if (unsignedp)
7120 unsignedp = MAX_INT_CACHED_PREC + 1;
7121
7122 if (precision <= MAX_INT_CACHED_PREC)
7123 {
7124 itype = nonstandard_integer_type_cache[precision + unsignedp];
7125 if (itype)
7126 return itype;
7127 }
7128
7129 itype = make_node (code: INTEGER_TYPE);
7130 TYPE_PRECISION (itype) = precision;
7131
7132 if (unsignedp)
7133 fixup_unsigned_type (itype);
7134 else
7135 fixup_signed_type (itype);
7136
7137 inchash::hash hstate;
7138 inchash::add_expr (TYPE_MAX_VALUE (itype), hstate);
7139 ret = type_hash_canon (hashcode: hstate.end (), type: itype);
7140 if (precision <= MAX_INT_CACHED_PREC)
7141 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7142
7143 return ret;
7144}
7145
7146#define MAX_BOOL_CACHED_PREC \
7147 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7148static GTY(()) tree nonstandard_boolean_type_cache[MAX_BOOL_CACHED_PREC + 1];
7149
7150/* Builds a boolean type of precision PRECISION.
7151 Used for boolean vectors to choose proper vector element size. */
7152tree
7153build_nonstandard_boolean_type (unsigned HOST_WIDE_INT precision)
7154{
7155 tree type;
7156
7157 if (precision <= MAX_BOOL_CACHED_PREC)
7158 {
7159 type = nonstandard_boolean_type_cache[precision];
7160 if (type)
7161 return type;
7162 }
7163
7164 type = make_node (code: BOOLEAN_TYPE);
7165 TYPE_PRECISION (type) = precision;
7166 fixup_signed_type (type);
7167
7168 if (precision <= MAX_INT_CACHED_PREC)
7169 nonstandard_boolean_type_cache[precision] = type;
7170
7171 return type;
7172}
7173
7174static GTY(()) vec<tree, va_gc> *bitint_type_cache;
7175
7176/* Builds a signed or unsigned _BitInt(PRECISION) type. */
7177tree
7178build_bitint_type (unsigned HOST_WIDE_INT precision, int unsignedp)
7179{
7180 tree itype, ret;
7181
7182 gcc_checking_assert (precision >= 1 + !unsignedp);
7183
7184 if (unsignedp)
7185 unsignedp = MAX_INT_CACHED_PREC + 1;
7186
7187 if (bitint_type_cache == NULL)
7188 vec_safe_grow_cleared (v&: bitint_type_cache, len: 2 * MAX_INT_CACHED_PREC + 2);
7189
7190 if (precision <= MAX_INT_CACHED_PREC)
7191 {
7192 itype = (*bitint_type_cache)[precision + unsignedp];
7193 if (itype)
7194 return itype;
7195 }
7196
7197 itype = make_node (code: BITINT_TYPE);
7198 TYPE_PRECISION (itype) = precision;
7199
7200 if (unsignedp)
7201 fixup_unsigned_type (itype);
7202 else
7203 fixup_signed_type (itype);
7204
7205 inchash::hash hstate;
7206 inchash::add_expr (TYPE_MAX_VALUE (itype), hstate);
7207 ret = type_hash_canon (hashcode: hstate.end (), type: itype);
7208 if (precision <= MAX_INT_CACHED_PREC)
7209 (*bitint_type_cache)[precision + unsignedp] = ret;
7210
7211 return ret;
7212}
7213
7214/* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7215 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7216 is true, reuse such a type that has already been constructed. */
7217
7218static tree
7219build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7220{
7221 tree itype = make_node (code: INTEGER_TYPE);
7222
7223 TREE_TYPE (itype) = type;
7224
7225 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7226 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7227
7228 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7229 SET_TYPE_MODE (itype, TYPE_MODE (type));
7230 TYPE_SIZE (itype) = TYPE_SIZE (type);
7231 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7232 SET_TYPE_ALIGN (itype, TYPE_ALIGN (type));
7233 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7234 SET_TYPE_WARN_IF_NOT_ALIGN (itype, TYPE_WARN_IF_NOT_ALIGN (type));
7235
7236 if (!shared)
7237 return itype;
7238
7239 if ((TYPE_MIN_VALUE (itype)
7240 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7241 || (TYPE_MAX_VALUE (itype)
7242 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7243 {
7244 /* Since we cannot reliably merge this type, we need to compare it using
7245 structural equality checks. */
7246 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7247 return itype;
7248 }
7249
7250 hashval_t hash = type_hash_canon_hash (type: itype);
7251 itype = type_hash_canon (hashcode: hash, type: itype);
7252
7253 return itype;
7254}
7255
7256/* Wrapper around build_range_type_1 with SHARED set to true. */
7257
7258tree
7259build_range_type (tree type, tree lowval, tree highval)
7260{
7261 return build_range_type_1 (type, lowval, highval, shared: true);
7262}
7263
7264/* Wrapper around build_range_type_1 with SHARED set to false. */
7265
7266tree
7267build_nonshared_range_type (tree type, tree lowval, tree highval)
7268{
7269 return build_range_type_1 (type, lowval, highval, shared: false);
7270}
7271
7272/* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7273 MAXVAL should be the maximum value in the domain
7274 (one less than the length of the array).
7275
7276 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7277 We don't enforce this limit, that is up to caller (e.g. language front end).
7278 The limit exists because the result is a signed type and we don't handle
7279 sizes that use more than one HOST_WIDE_INT. */
7280
7281tree
7282build_index_type (tree maxval)
7283{
7284 return build_range_type (sizetype, size_zero_node, highval: maxval);
7285}
7286
7287/* Return true if the debug information for TYPE, a subtype, should be emitted
7288 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7289 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7290 debug info and doesn't reflect the source code. */
7291
7292bool
7293subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7294{
7295 tree base_type = TREE_TYPE (type), low, high;
7296
7297 /* Subrange types have a base type which is an integral type. */
7298 if (!INTEGRAL_TYPE_P (base_type))
7299 return false;
7300
7301 /* Get the real bounds of the subtype. */
7302 if (lang_hooks.types.get_subrange_bounds)
7303 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7304 else
7305 {
7306 low = TYPE_MIN_VALUE (type);
7307 high = TYPE_MAX_VALUE (type);
7308 }
7309
7310 /* If the type and its base type have the same representation and the same
7311 name, then the type is not a subrange but a copy of the base type. */
7312 if ((TREE_CODE (base_type) == INTEGER_TYPE
7313 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7314 && int_size_in_bytes (type) == int_size_in_bytes (type: base_type)
7315 && tree_int_cst_equal (t1: low, TYPE_MIN_VALUE (base_type))
7316 && tree_int_cst_equal (t1: high, TYPE_MAX_VALUE (base_type))
7317 && TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (base_type))
7318 return false;
7319
7320 if (lowval)
7321 *lowval = low;
7322 if (highval)
7323 *highval = high;
7324 return true;
7325}
7326
7327/* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7328 and number of elements specified by the range of values of INDEX_TYPE.
7329 If TYPELESS_STORAGE is true, TYPE_TYPELESS_STORAGE flag is set on the type.
7330 If SHARED is true, reuse such a type that has already been constructed.
7331 If SET_CANONICAL is true, compute TYPE_CANONICAL from the element type. */
7332
7333tree
7334build_array_type_1 (tree elt_type, tree index_type, bool typeless_storage,
7335 bool shared, bool set_canonical)
7336{
7337 tree t;
7338
7339 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7340 {
7341 error ("arrays of functions are not meaningful");
7342 elt_type = integer_type_node;
7343 }
7344
7345 t = make_node (code: ARRAY_TYPE);
7346 TREE_TYPE (t) = elt_type;
7347 TYPE_DOMAIN (t) = index_type;
7348 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7349 TYPE_TYPELESS_STORAGE (t) = typeless_storage;
7350 layout_type (t);
7351
7352 if (shared)
7353 {
7354 hashval_t hash = type_hash_canon_hash (type: t);
7355 t = type_hash_canon (hashcode: hash, type: t);
7356 }
7357
7358 if (TYPE_CANONICAL (t) == t && set_canonical)
7359 {
7360 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7361 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type))
7362 || in_lto_p)
7363 SET_TYPE_STRUCTURAL_EQUALITY (t);
7364 else if (TYPE_CANONICAL (elt_type) != elt_type
7365 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7366 TYPE_CANONICAL (t)
7367 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7368 index_type: index_type
7369 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7370 typeless_storage, shared, set_canonical);
7371 }
7372
7373 return t;
7374}
7375
7376/* Wrapper around build_array_type_1 with SHARED set to true. */
7377
7378tree
7379build_array_type (tree elt_type, tree index_type, bool typeless_storage)
7380{
7381 return
7382 build_array_type_1 (elt_type, index_type, typeless_storage, shared: true, set_canonical: true);
7383}
7384
7385/* Wrapper around build_array_type_1 with SHARED set to false. */
7386
7387tree
7388build_nonshared_array_type (tree elt_type, tree index_type)
7389{
7390 return build_array_type_1 (elt_type, index_type, typeless_storage: false, shared: false, set_canonical: true);
7391}
7392
7393/* Return a representation of ELT_TYPE[NELTS], using indices of type
7394 sizetype. */
7395
7396tree
7397build_array_type_nelts (tree elt_type, poly_uint64 nelts)
7398{
7399 return build_array_type (elt_type, index_type: build_index_type (size_int (nelts - 1)));
7400}
7401
7402/* Computes the canonical argument types from the argument type list
7403 ARGTYPES.
7404
7405 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7406 on entry to this function, or if any of the ARGTYPES are
7407 structural.
7408
7409 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7410 true on entry to this function, or if any of the ARGTYPES are
7411 non-canonical.
7412
7413 Returns a canonical argument list, which may be ARGTYPES when the
7414 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7415 true) or would not differ from ARGTYPES. */
7416
7417static tree
7418maybe_canonicalize_argtypes (tree argtypes,
7419 bool *any_structural_p,
7420 bool *any_noncanonical_p)
7421{
7422 tree arg;
7423 bool any_noncanonical_argtypes_p = false;
7424
7425 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7426 {
7427 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7428 /* Fail gracefully by stating that the type is structural. */
7429 *any_structural_p = true;
7430 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7431 *any_structural_p = true;
7432 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7433 || TREE_PURPOSE (arg))
7434 /* If the argument has a default argument, we consider it
7435 non-canonical even though the type itself is canonical.
7436 That way, different variants of function and method types
7437 with default arguments will all point to the variant with
7438 no defaults as their canonical type. */
7439 any_noncanonical_argtypes_p = true;
7440 }
7441
7442 if (*any_structural_p)
7443 return argtypes;
7444
7445 if (any_noncanonical_argtypes_p)
7446 {
7447 /* Build the canonical list of argument types. */
7448 tree canon_argtypes = NULL_TREE;
7449 bool is_void = false;
7450
7451 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7452 {
7453 if (arg == void_list_node)
7454 is_void = true;
7455 else
7456 canon_argtypes = tree_cons (NULL_TREE,
7457 TYPE_CANONICAL (TREE_VALUE (arg)),
7458 chain: canon_argtypes);
7459 }
7460
7461 canon_argtypes = nreverse (t: canon_argtypes);
7462 if (is_void)
7463 canon_argtypes = chainon (op1: canon_argtypes, void_list_node);
7464
7465 /* There is a non-canonical type. */
7466 *any_noncanonical_p = true;
7467 return canon_argtypes;
7468 }
7469
7470 /* The canonical argument types are the same as ARGTYPES. */
7471 return argtypes;
7472}
7473
7474/* Construct, lay out and return
7475 the type of functions returning type VALUE_TYPE
7476 given arguments of types ARG_TYPES.
7477 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7478 are data type nodes for the arguments of the function.
7479 NO_NAMED_ARGS_STDARG_P is true if this is a prototyped
7480 variable-arguments function with (...) prototype (no named arguments).
7481 If such a type has already been constructed, reuse it. */
7482
7483tree
7484build_function_type (tree value_type, tree arg_types,
7485 bool no_named_args_stdarg_p)
7486{
7487 tree t;
7488 inchash::hash hstate;
7489 bool any_structural_p, any_noncanonical_p;
7490 tree canon_argtypes;
7491
7492 gcc_assert (arg_types != error_mark_node);
7493
7494 if (TREE_CODE (value_type) == FUNCTION_TYPE)
7495 {
7496 error ("function return type cannot be function");
7497 value_type = integer_type_node;
7498 }
7499
7500 /* Make a node of the sort we want. */
7501 t = make_node (code: FUNCTION_TYPE);
7502 TREE_TYPE (t) = value_type;
7503 TYPE_ARG_TYPES (t) = arg_types;
7504 if (no_named_args_stdarg_p)
7505 {
7506 gcc_assert (arg_types == NULL_TREE);
7507 TYPE_NO_NAMED_ARGS_STDARG_P (t) = 1;
7508 }
7509
7510 /* If we already have such a type, use the old one. */
7511 hashval_t hash = type_hash_canon_hash (type: t);
7512 t = type_hash_canon (hashcode: hash, type: t);
7513
7514 /* Set up the canonical type. */
7515 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7516 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7517 canon_argtypes = maybe_canonicalize_argtypes (argtypes: arg_types,
7518 any_structural_p: &any_structural_p,
7519 any_noncanonical_p: &any_noncanonical_p);
7520 if (any_structural_p)
7521 SET_TYPE_STRUCTURAL_EQUALITY (t);
7522 else if (any_noncanonical_p)
7523 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
7524 arg_types: canon_argtypes);
7525
7526 if (!COMPLETE_TYPE_P (t))
7527 layout_type (t);
7528 return t;
7529}
7530
7531/* Build a function type. The RETURN_TYPE is the type returned by the
7532 function. If VAARGS is set, no void_type_node is appended to the
7533 list. ARGP must be always be terminated be a NULL_TREE. */
7534
7535static tree
7536build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
7537{
7538 tree t, args, last;
7539
7540 t = va_arg (argp, tree);
7541 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
7542 args = tree_cons (NULL_TREE, value: t, chain: args);
7543
7544 if (vaargs)
7545 {
7546 last = args;
7547 if (args != NULL_TREE)
7548 args = nreverse (t: args);
7549 gcc_assert (last != void_list_node);
7550 }
7551 else if (args == NULL_TREE)
7552 args = void_list_node;
7553 else
7554 {
7555 last = args;
7556 args = nreverse (t: args);
7557 TREE_CHAIN (last) = void_list_node;
7558 }
7559 args = build_function_type (value_type: return_type, arg_types: args, no_named_args_stdarg_p: vaargs && args == NULL_TREE);
7560
7561 return args;
7562}
7563
7564/* Build a function type. The RETURN_TYPE is the type returned by the
7565 function. If additional arguments are provided, they are
7566 additional argument types. The list of argument types must always
7567 be terminated by NULL_TREE. */
7568
7569tree
7570build_function_type_list (tree return_type, ...)
7571{
7572 tree args;
7573 va_list p;
7574
7575 va_start (p, return_type);
7576 args = build_function_type_list_1 (vaargs: false, return_type, argp: p);
7577 va_end (p);
7578 return args;
7579}
7580
7581/* Build a variable argument function type. The RETURN_TYPE is the
7582 type returned by the function. If additional arguments are provided,
7583 they are additional argument types. The list of argument types must
7584 always be terminated by NULL_TREE. */
7585
7586tree
7587build_varargs_function_type_list (tree return_type, ...)
7588{
7589 tree args;
7590 va_list p;
7591
7592 va_start (p, return_type);
7593 args = build_function_type_list_1 (vaargs: true, return_type, argp: p);
7594 va_end (p);
7595
7596 return args;
7597}
7598
7599/* Build a function type. RETURN_TYPE is the type returned by the
7600 function; VAARGS indicates whether the function takes varargs. The
7601 function takes N named arguments, the types of which are provided in
7602 ARG_TYPES. */
7603
7604static tree
7605build_function_type_array_1 (bool vaargs, tree return_type, int n,
7606 tree *arg_types)
7607{
7608 int i;
7609 tree t = vaargs ? NULL_TREE : void_list_node;
7610
7611 for (i = n - 1; i >= 0; i--)
7612 t = tree_cons (NULL_TREE, value: arg_types[i], chain: t);
7613
7614 return build_function_type (value_type: return_type, arg_types: t, no_named_args_stdarg_p: vaargs && n == 0);
7615}
7616
7617/* Build a function type. RETURN_TYPE is the type returned by the
7618 function. The function takes N named arguments, the types of which
7619 are provided in ARG_TYPES. */
7620
7621tree
7622build_function_type_array (tree return_type, int n, tree *arg_types)
7623{
7624 return build_function_type_array_1 (vaargs: false, return_type, n, arg_types);
7625}
7626
7627/* Build a variable argument function type. RETURN_TYPE is the type
7628 returned by the function. The function takes N named arguments, the
7629 types of which are provided in ARG_TYPES. */
7630
7631tree
7632build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
7633{
7634 return build_function_type_array_1 (vaargs: true, return_type, n, arg_types);
7635}
7636
7637/* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
7638 and ARGTYPES (a TREE_LIST) are the return type and arguments types
7639 for the method. An implicit additional parameter (of type
7640 pointer-to-BASETYPE) is added to the ARGTYPES. */
7641
7642tree
7643build_method_type_directly (tree basetype,
7644 tree rettype,
7645 tree argtypes)
7646{
7647 tree t;
7648 tree ptype;
7649 bool any_structural_p, any_noncanonical_p;
7650 tree canon_argtypes;
7651
7652 /* Make a node of the sort we want. */
7653 t = make_node (code: METHOD_TYPE);
7654
7655 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7656 TREE_TYPE (t) = rettype;
7657 ptype = build_pointer_type (to_type: basetype);
7658
7659 /* The actual arglist for this function includes a "hidden" argument
7660 which is "this". Put it into the list of argument types. */
7661 argtypes = tree_cons (NULL_TREE, value: ptype, chain: argtypes);
7662 TYPE_ARG_TYPES (t) = argtypes;
7663
7664 /* If we already have such a type, use the old one. */
7665 hashval_t hash = type_hash_canon_hash (type: t);
7666 t = type_hash_canon (hashcode: hash, type: t);
7667
7668 /* Set up the canonical type. */
7669 any_structural_p
7670 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7671 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
7672 any_noncanonical_p
7673 = (TYPE_CANONICAL (basetype) != basetype
7674 || TYPE_CANONICAL (rettype) != rettype);
7675 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
7676 any_structural_p: &any_structural_p,
7677 any_noncanonical_p: &any_noncanonical_p);
7678 if (any_structural_p)
7679 SET_TYPE_STRUCTURAL_EQUALITY (t);
7680 else if (any_noncanonical_p)
7681 TYPE_CANONICAL (t)
7682 = build_method_type_directly (TYPE_CANONICAL (basetype),
7683 TYPE_CANONICAL (rettype),
7684 argtypes: canon_argtypes);
7685 if (!COMPLETE_TYPE_P (t))
7686 layout_type (t);
7687
7688 return t;
7689}
7690
7691/* Construct, lay out and return the type of methods belonging to class
7692 BASETYPE and whose arguments and values are described by TYPE.
7693 If that type exists already, reuse it.
7694 TYPE must be a FUNCTION_TYPE node. */
7695
7696tree
7697build_method_type (tree basetype, tree type)
7698{
7699 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
7700
7701 return build_method_type_directly (basetype,
7702 TREE_TYPE (type),
7703 TYPE_ARG_TYPES (type));
7704}
7705
7706/* Construct, lay out and return the type of offsets to a value
7707 of type TYPE, within an object of type BASETYPE.
7708 If a suitable offset type exists already, reuse it. */
7709
7710tree
7711build_offset_type (tree basetype, tree type)
7712{
7713 tree t;
7714
7715 /* Make a node of the sort we want. */
7716 t = make_node (code: OFFSET_TYPE);
7717
7718 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7719 TREE_TYPE (t) = type;
7720
7721 /* If we already have such a type, use the old one. */
7722 hashval_t hash = type_hash_canon_hash (type: t);
7723 t = type_hash_canon (hashcode: hash, type: t);
7724
7725 if (!COMPLETE_TYPE_P (t))
7726 layout_type (t);
7727
7728 if (TYPE_CANONICAL (t) == t)
7729 {
7730 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7731 || TYPE_STRUCTURAL_EQUALITY_P (type))
7732 SET_TYPE_STRUCTURAL_EQUALITY (t);
7733 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
7734 || TYPE_CANONICAL (type) != type)
7735 TYPE_CANONICAL (t)
7736 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
7737 TYPE_CANONICAL (type));
7738 }
7739
7740 return t;
7741}
7742
7743/* Create a complex type whose components are COMPONENT_TYPE.
7744
7745 If NAMED is true, the type is given a TYPE_NAME. We do not always
7746 do so because this creates a DECL node and thus make the DECL_UIDs
7747 dependent on the type canonicalization hashtable, which is GC-ed,
7748 so the DECL_UIDs would not be stable wrt garbage collection. */
7749
7750tree
7751build_complex_type (tree component_type, bool named)
7752{
7753 gcc_assert (INTEGRAL_TYPE_P (component_type)
7754 || SCALAR_FLOAT_TYPE_P (component_type)
7755 || FIXED_POINT_TYPE_P (component_type));
7756
7757 /* Make a node of the sort we want. */
7758 tree probe = make_node (code: COMPLEX_TYPE);
7759
7760 TREE_TYPE (probe) = TYPE_MAIN_VARIANT (component_type);
7761
7762 /* If we already have such a type, use the old one. */
7763 hashval_t hash = type_hash_canon_hash (type: probe);
7764 tree t = type_hash_canon (hashcode: hash, type: probe);
7765
7766 if (t == probe)
7767 {
7768 /* We created a new type. The hash insertion will have laid
7769 out the type. We need to check the canonicalization and
7770 maybe set the name. */
7771 gcc_checking_assert (COMPLETE_TYPE_P (t)
7772 && !TYPE_NAME (t)
7773 && TYPE_CANONICAL (t) == t);
7774
7775 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (t)))
7776 SET_TYPE_STRUCTURAL_EQUALITY (t);
7777 else if (TYPE_CANONICAL (TREE_TYPE (t)) != TREE_TYPE (t))
7778 TYPE_CANONICAL (t)
7779 = build_complex_type (TYPE_CANONICAL (TREE_TYPE (t)), named);
7780
7781 /* We need to create a name, since complex is a fundamental type. */
7782 if (named)
7783 {
7784 const char *name = NULL;
7785
7786 if (TREE_TYPE (t) == char_type_node)
7787 name = "complex char";
7788 else if (TREE_TYPE (t) == signed_char_type_node)
7789 name = "complex signed char";
7790 else if (TREE_TYPE (t) == unsigned_char_type_node)
7791 name = "complex unsigned char";
7792 else if (TREE_TYPE (t) == short_integer_type_node)
7793 name = "complex short int";
7794 else if (TREE_TYPE (t) == short_unsigned_type_node)
7795 name = "complex short unsigned int";
7796 else if (TREE_TYPE (t) == integer_type_node)
7797 name = "complex int";
7798 else if (TREE_TYPE (t) == unsigned_type_node)
7799 name = "complex unsigned int";
7800 else if (TREE_TYPE (t) == long_integer_type_node)
7801 name = "complex long int";
7802 else if (TREE_TYPE (t) == long_unsigned_type_node)
7803 name = "complex long unsigned int";
7804 else if (TREE_TYPE (t) == long_long_integer_type_node)
7805 name = "complex long long int";
7806 else if (TREE_TYPE (t) == long_long_unsigned_type_node)
7807 name = "complex long long unsigned int";
7808
7809 if (name != NULL)
7810 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, code: TYPE_DECL,
7811 get_identifier (name), type: t);
7812 }
7813 }
7814
7815 return build_qualified_type (type: t, TYPE_QUALS (component_type));
7816}
7817
7818/* If TYPE is a real or complex floating-point type and the target
7819 does not directly support arithmetic on TYPE then return the wider
7820 type to be used for arithmetic on TYPE. Otherwise, return
7821 NULL_TREE. */
7822
7823tree
7824excess_precision_type (tree type)
7825{
7826 /* The target can give two different responses to the question of
7827 which excess precision mode it would like depending on whether we
7828 are in -fexcess-precision=standard or -fexcess-precision=fast. */
7829
7830 enum excess_precision_type requested_type
7831 = (flag_excess_precision == EXCESS_PRECISION_FAST
7832 ? EXCESS_PRECISION_TYPE_FAST
7833 : (flag_excess_precision == EXCESS_PRECISION_FLOAT16
7834 ? EXCESS_PRECISION_TYPE_FLOAT16 : EXCESS_PRECISION_TYPE_STANDARD));
7835
7836 enum flt_eval_method target_flt_eval_method
7837 = targetm.c.excess_precision (requested_type);
7838
7839 /* The target should not ask for unpredictable float evaluation (though
7840 it might advertise that implicitly the evaluation is unpredictable,
7841 but we don't care about that here, it will have been reported
7842 elsewhere). If it does ask for unpredictable evaluation, we have
7843 nothing to do here. */
7844 gcc_assert (target_flt_eval_method != FLT_EVAL_METHOD_UNPREDICTABLE);
7845
7846 /* Nothing to do. The target has asked for all types we know about
7847 to be computed with their native precision and range. */
7848 if (target_flt_eval_method == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
7849 return NULL_TREE;
7850
7851 /* The target will promote this type in a target-dependent way, so excess
7852 precision ought to leave it alone. */
7853 if (targetm.promoted_type (type) != NULL_TREE)
7854 return NULL_TREE;
7855
7856 machine_mode float16_type_mode = (float16_type_node
7857 ? TYPE_MODE (float16_type_node)
7858 : VOIDmode);
7859 machine_mode bfloat16_type_mode = (bfloat16_type_node
7860 ? TYPE_MODE (bfloat16_type_node)
7861 : VOIDmode);
7862 machine_mode float_type_mode = TYPE_MODE (float_type_node);
7863 machine_mode double_type_mode = TYPE_MODE (double_type_node);
7864
7865 switch (TREE_CODE (type))
7866 {
7867 case REAL_TYPE:
7868 {
7869 machine_mode type_mode = TYPE_MODE (type);
7870 switch (target_flt_eval_method)
7871 {
7872 case FLT_EVAL_METHOD_PROMOTE_TO_FLOAT:
7873 if (type_mode == float16_type_mode
7874 || type_mode == bfloat16_type_mode)
7875 return float_type_node;
7876 break;
7877 case FLT_EVAL_METHOD_PROMOTE_TO_DOUBLE:
7878 if (type_mode == float16_type_mode
7879 || type_mode == bfloat16_type_mode
7880 || type_mode == float_type_mode)
7881 return double_type_node;
7882 break;
7883 case FLT_EVAL_METHOD_PROMOTE_TO_LONG_DOUBLE:
7884 if (type_mode == float16_type_mode
7885 || type_mode == bfloat16_type_mode
7886 || type_mode == float_type_mode
7887 || type_mode == double_type_mode)
7888 return long_double_type_node;
7889 break;
7890 default:
7891 gcc_unreachable ();
7892 }
7893 break;
7894 }
7895 case COMPLEX_TYPE:
7896 {
7897 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
7898 return NULL_TREE;
7899 machine_mode type_mode = TYPE_MODE (TREE_TYPE (type));
7900 switch (target_flt_eval_method)
7901 {
7902 case FLT_EVAL_METHOD_PROMOTE_TO_FLOAT:
7903 if (type_mode == float16_type_mode
7904 || type_mode == bfloat16_type_mode)
7905 return complex_float_type_node;
7906 break;
7907 case FLT_EVAL_METHOD_PROMOTE_TO_DOUBLE:
7908 if (type_mode == float16_type_mode
7909 || type_mode == bfloat16_type_mode
7910 || type_mode == float_type_mode)
7911 return complex_double_type_node;
7912 break;
7913 case FLT_EVAL_METHOD_PROMOTE_TO_LONG_DOUBLE:
7914 if (type_mode == float16_type_mode
7915 || type_mode == bfloat16_type_mode
7916 || type_mode == float_type_mode
7917 || type_mode == double_type_mode)
7918 return complex_long_double_type_node;
7919 break;
7920 default:
7921 gcc_unreachable ();
7922 }
7923 break;
7924 }
7925 default:
7926 break;
7927 }
7928
7929 return NULL_TREE;
7930}
7931
7932/* Return OP, stripped of any conversions to wider types as much as is safe.
7933 Converting the value back to OP's type makes a value equivalent to OP.
7934
7935 If FOR_TYPE is nonzero, we return a value which, if converted to
7936 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
7937
7938 OP must have integer, real or enumeral type. Pointers are not allowed!
7939
7940 There are some cases where the obvious value we could return
7941 would regenerate to OP if converted to OP's type,
7942 but would not extend like OP to wider types.
7943 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
7944 For example, if OP is (unsigned short)(signed char)-1,
7945 we avoid returning (signed char)-1 if FOR_TYPE is int,
7946 even though extending that to an unsigned short would regenerate OP,
7947 since the result of extending (signed char)-1 to (int)
7948 is different from (int) OP. */
7949
7950tree
7951get_unwidened (tree op, tree for_type)
7952{
7953 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
7954 tree type = TREE_TYPE (op);
7955 unsigned final_prec
7956 = TYPE_PRECISION (for_type != 0 ? for_type : type);
7957 int uns
7958 = (for_type != 0 && for_type != type
7959 && final_prec > TYPE_PRECISION (type)
7960 && TYPE_UNSIGNED (type));
7961 tree win = op;
7962
7963 while (CONVERT_EXPR_P (op))
7964 {
7965 int bitschange;
7966
7967 /* TYPE_PRECISION on vector types has different meaning
7968 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
7969 so avoid them here. */
7970 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
7971 break;
7972
7973 bitschange = TYPE_PRECISION (TREE_TYPE (op))
7974 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
7975
7976 /* Truncations are many-one so cannot be removed.
7977 Unless we are later going to truncate down even farther. */
7978 if (bitschange < 0
7979 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
7980 break;
7981
7982 /* See what's inside this conversion. If we decide to strip it,
7983 we will set WIN. */
7984 op = TREE_OPERAND (op, 0);
7985
7986 /* If we have not stripped any zero-extensions (uns is 0),
7987 we can strip any kind of extension.
7988 If we have previously stripped a zero-extension,
7989 only zero-extensions can safely be stripped.
7990 Any extension can be stripped if the bits it would produce
7991 are all going to be discarded later by truncating to FOR_TYPE. */
7992
7993 if (bitschange > 0)
7994 {
7995 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
7996 win = op;
7997 /* TYPE_UNSIGNED says whether this is a zero-extension.
7998 Let's avoid computing it if it does not affect WIN
7999 and if UNS will not be needed again. */
8000 if ((uns
8001 || CONVERT_EXPR_P (op))
8002 && TYPE_UNSIGNED (TREE_TYPE (op)))
8003 {
8004 uns = 1;
8005 win = op;
8006 }
8007 }
8008 }
8009
8010 /* If we finally reach a constant see if it fits in sth smaller and
8011 in that case convert it. */
8012 if (TREE_CODE (win) == INTEGER_CST)
8013 {
8014 tree wtype = TREE_TYPE (win);
8015 unsigned prec = wi::min_precision (x: wi::to_wide (t: win), TYPE_SIGN (wtype));
8016 if (for_type)
8017 prec = MAX (prec, final_prec);
8018 if (prec < TYPE_PRECISION (wtype))
8019 {
8020 tree t = lang_hooks.types.type_for_size (prec, TYPE_UNSIGNED (wtype));
8021 if (t && TYPE_PRECISION (t) < TYPE_PRECISION (wtype))
8022 win = fold_convert (t, win);
8023 }
8024 }
8025
8026 return win;
8027}
8028
8029/* Return OP or a simpler expression for a narrower value
8030 which can be sign-extended or zero-extended to give back OP.
8031 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8032 or 0 if the value should be sign-extended. */
8033
8034tree
8035get_narrower (tree op, int *unsignedp_ptr)
8036{
8037 int uns = 0;
8038 bool first = true;
8039 tree win = op;
8040 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8041
8042 if (TREE_CODE (op) == COMPOUND_EXPR)
8043 {
8044 do
8045 op = TREE_OPERAND (op, 1);
8046 while (TREE_CODE (op) == COMPOUND_EXPR);
8047 tree ret = get_narrower (op, unsignedp_ptr);
8048 if (ret == op)
8049 return win;
8050 auto_vec <tree, 16> v;
8051 unsigned int i;
8052 for (op = win; TREE_CODE (op) == COMPOUND_EXPR;
8053 op = TREE_OPERAND (op, 1))
8054 v.safe_push (obj: op);
8055 FOR_EACH_VEC_ELT_REVERSE (v, i, op)
8056 ret = build2_loc (EXPR_LOCATION (op), code: COMPOUND_EXPR,
8057 TREE_TYPE (ret), TREE_OPERAND (op, 0),
8058 arg1: ret);
8059 return ret;
8060 }
8061 while (TREE_CODE (op) == NOP_EXPR)
8062 {
8063 int bitschange
8064 = (TYPE_PRECISION (TREE_TYPE (op))
8065 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8066
8067 /* Truncations are many-one so cannot be removed. */
8068 if (bitschange < 0)
8069 break;
8070
8071 /* See what's inside this conversion. If we decide to strip it,
8072 we will set WIN. */
8073
8074 if (bitschange > 0)
8075 {
8076 op = TREE_OPERAND (op, 0);
8077 /* An extension: the outermost one can be stripped,
8078 but remember whether it is zero or sign extension. */
8079 if (first)
8080 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8081 /* Otherwise, if a sign extension has been stripped,
8082 only sign extensions can now be stripped;
8083 if a zero extension has been stripped, only zero-extensions. */
8084 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8085 break;
8086 first = false;
8087 }
8088 else /* bitschange == 0 */
8089 {
8090 /* A change in nominal type can always be stripped, but we must
8091 preserve the unsignedness. */
8092 if (first)
8093 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8094 first = false;
8095 op = TREE_OPERAND (op, 0);
8096 /* Keep trying to narrow, but don't assign op to win if it
8097 would turn an integral type into something else. */
8098 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8099 continue;
8100 }
8101
8102 win = op;
8103 }
8104
8105 if (TREE_CODE (op) == COMPONENT_REF
8106 /* Since type_for_size always gives an integer type. */
8107 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8108 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8109 /* Ensure field is laid out already. */
8110 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8111 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
8112 {
8113 unsigned HOST_WIDE_INT innerprec
8114 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
8115 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8116 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8117 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8118
8119 /* We can get this structure field in a narrower type that fits it,
8120 but the resulting extension to its nominal type (a fullword type)
8121 must satisfy the same conditions as for other extensions.
8122
8123 Do this only for fields that are aligned (not bit-fields),
8124 because when bit-field insns will be used there is no
8125 advantage in doing this. */
8126
8127 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8128 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8129 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8130 && type != 0)
8131 {
8132 if (first)
8133 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8134 win = fold_convert (type, op);
8135 }
8136 }
8137
8138 *unsignedp_ptr = uns;
8139 return win;
8140}
8141
8142/* Return true if integer constant C has a value that is permissible
8143 for TYPE, an integral type. */
8144
8145bool
8146int_fits_type_p (const_tree c, const_tree type)
8147{
8148 tree type_low_bound, type_high_bound;
8149 bool ok_for_low_bound, ok_for_high_bound;
8150 signop sgn_c = TYPE_SIGN (TREE_TYPE (c));
8151
8152 /* Non-standard boolean types can have arbitrary precision but various
8153 transformations assume that they can only take values 0 and +/-1. */
8154 if (TREE_CODE (type) == BOOLEAN_TYPE)
8155 return wi::fits_to_boolean_p (x: wi::to_wide (t: c), type);
8156
8157retry:
8158 type_low_bound = TYPE_MIN_VALUE (type);
8159 type_high_bound = TYPE_MAX_VALUE (type);
8160
8161 /* If at least one bound of the type is a constant integer, we can check
8162 ourselves and maybe make a decision. If no such decision is possible, but
8163 this type is a subtype, try checking against that. Otherwise, use
8164 fits_to_tree_p, which checks against the precision.
8165
8166 Compute the status for each possibly constant bound, and return if we see
8167 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8168 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8169 for "constant known to fit". */
8170
8171 /* Check if c >= type_low_bound. */
8172 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8173 {
8174 if (tree_int_cst_lt (t1: c, t2: type_low_bound))
8175 return false;
8176 ok_for_low_bound = true;
8177 }
8178 else
8179 ok_for_low_bound = false;
8180
8181 /* Check if c <= type_high_bound. */
8182 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8183 {
8184 if (tree_int_cst_lt (t1: type_high_bound, t2: c))
8185 return false;
8186 ok_for_high_bound = true;
8187 }
8188 else
8189 ok_for_high_bound = false;
8190
8191 /* If the constant fits both bounds, the result is known. */
8192 if (ok_for_low_bound && ok_for_high_bound)
8193 return true;
8194
8195 /* Perform some generic filtering which may allow making a decision
8196 even if the bounds are not constant. First, negative integers
8197 never fit in unsigned types, */
8198 if (TYPE_UNSIGNED (type) && sgn_c == SIGNED && wi::neg_p (x: wi::to_wide (t: c)))
8199 return false;
8200
8201 /* Second, narrower types always fit in wider ones. */
8202 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8203 return true;
8204
8205 /* Third, unsigned integers with top bit set never fit signed types. */
8206 if (!TYPE_UNSIGNED (type) && sgn_c == UNSIGNED)
8207 {
8208 int prec = GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (TREE_TYPE (c))) - 1;
8209 if (prec < TYPE_PRECISION (TREE_TYPE (c)))
8210 {
8211 /* When a tree_cst is converted to a wide-int, the precision
8212 is taken from the type. However, if the precision of the
8213 mode underneath the type is smaller than that, it is
8214 possible that the value will not fit. The test below
8215 fails if any bit is set between the sign bit of the
8216 underlying mode and the top bit of the type. */
8217 if (wi::zext (x: wi::to_wide (t: c), offset: prec - 1) != wi::to_wide (t: c))
8218 return false;
8219 }
8220 else if (wi::neg_p (x: wi::to_wide (t: c)))
8221 return false;
8222 }
8223
8224 /* If we haven't been able to decide at this point, there nothing more we
8225 can check ourselves here. Look at the base type if we have one and it
8226 has the same precision. */
8227 if (TREE_CODE (type) == INTEGER_TYPE
8228 && TREE_TYPE (type) != 0
8229 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8230 {
8231 type = TREE_TYPE (type);
8232 goto retry;
8233 }
8234
8235 /* Or to fits_to_tree_p, if nothing else. */
8236 return wi::fits_to_tree_p (x: wi::to_wide (t: c), type);
8237}
8238
8239/* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8240 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8241 represented (assuming two's-complement arithmetic) within the bit
8242 precision of the type are returned instead. */
8243
8244void
8245get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8246{
8247 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8248 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8249 wi::to_mpz (wi::to_wide (TYPE_MIN_VALUE (type)), min, TYPE_SIGN (type));
8250 else
8251 {
8252 if (TYPE_UNSIGNED (type))
8253 mpz_set_ui (min, 0);
8254 else
8255 {
8256 wide_int mn = wi::min_value (TYPE_PRECISION (type), SIGNED);
8257 wi::to_mpz (mn, min, SIGNED);
8258 }
8259 }
8260
8261 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8262 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8263 wi::to_mpz (wi::to_wide (TYPE_MAX_VALUE (type)), max, TYPE_SIGN (type));
8264 else
8265 {
8266 wide_int mn = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
8267 wi::to_mpz (mn, max, TYPE_SIGN (type));
8268 }
8269}
8270
8271/* Return true if VAR is an automatic variable. */
8272
8273bool
8274auto_var_p (const_tree var)
8275{
8276 return ((((VAR_P (var) && ! DECL_EXTERNAL (var))
8277 || TREE_CODE (var) == PARM_DECL)
8278 && ! TREE_STATIC (var))
8279 || TREE_CODE (var) == RESULT_DECL);
8280}
8281
8282/* Return true if VAR is an automatic variable defined in function FN. */
8283
8284bool
8285auto_var_in_fn_p (const_tree var, const_tree fn)
8286{
8287 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8288 && (auto_var_p (var)
8289 || TREE_CODE (var) == LABEL_DECL));
8290}
8291
8292/* Subprogram of following function. Called by walk_tree.
8293
8294 Return *TP if it is an automatic variable or parameter of the
8295 function passed in as DATA. */
8296
8297static tree
8298find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8299{
8300 tree fn = (tree) data;
8301
8302 if (TYPE_P (*tp))
8303 *walk_subtrees = 0;
8304
8305 else if (DECL_P (*tp)
8306 && auto_var_in_fn_p (var: *tp, fn))
8307 return *tp;
8308
8309 return NULL_TREE;
8310}
8311
8312/* Returns true if T is, contains, or refers to a type with variable
8313 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8314 arguments, but not the return type. If FN is nonzero, only return
8315 true if a modifier of the type or position of FN is a variable or
8316 parameter inside FN.
8317
8318 This concept is more general than that of C99 'variably modified types':
8319 in C99, a struct type is never variably modified because a VLA may not
8320 appear as a structure member. However, in GNU C code like:
8321
8322 struct S { int i[f()]; };
8323
8324 is valid, and other languages may define similar constructs. */
8325
8326bool
8327variably_modified_type_p (tree type, tree fn)
8328{
8329 tree t;
8330
8331/* Test if T is either variable (if FN is zero) or an expression containing
8332 a variable in FN. If TYPE isn't gimplified, return true also if
8333 gimplify_one_sizepos would gimplify the expression into a local
8334 variable. */
8335#define RETURN_TRUE_IF_VAR(T) \
8336 do { tree _t = (T); \
8337 if (_t != NULL_TREE \
8338 && _t != error_mark_node \
8339 && !CONSTANT_CLASS_P (_t) \
8340 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8341 && (!fn \
8342 || (!TYPE_SIZES_GIMPLIFIED (type) \
8343 && (TREE_CODE (_t) != VAR_DECL \
8344 && !CONTAINS_PLACEHOLDER_P (_t))) \
8345 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8346 return true; } while (0)
8347
8348 if (type == error_mark_node)
8349 return false;
8350
8351 /* If TYPE itself has variable size, it is variably modified. */
8352 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8353 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8354
8355 switch (TREE_CODE (type))
8356 {
8357 case POINTER_TYPE:
8358 case REFERENCE_TYPE:
8359 case VECTOR_TYPE:
8360 /* Ada can have pointer types refering to themselves indirectly. */
8361 if (TREE_VISITED (type))
8362 return false;
8363 TREE_VISITED (type) = true;
8364 if (variably_modified_type_p (TREE_TYPE (type), fn))
8365 {
8366 TREE_VISITED (type) = false;
8367 return true;
8368 }
8369 TREE_VISITED (type) = false;
8370 break;
8371
8372 case FUNCTION_TYPE:
8373 case METHOD_TYPE:
8374 /* If TYPE is a function type, it is variably modified if the
8375 return type is variably modified. */
8376 if (variably_modified_type_p (TREE_TYPE (type), fn))
8377 return true;
8378 break;
8379
8380 case INTEGER_TYPE:
8381 case REAL_TYPE:
8382 case FIXED_POINT_TYPE:
8383 case ENUMERAL_TYPE:
8384 case BOOLEAN_TYPE:
8385 /* Scalar types are variably modified if their end points
8386 aren't constant. */
8387 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8388 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8389 break;
8390
8391 case RECORD_TYPE:
8392 case UNION_TYPE:
8393 case QUAL_UNION_TYPE:
8394 /* We can't see if any of the fields are variably-modified by the
8395 definition we normally use, since that would produce infinite
8396 recursion via pointers. */
8397 /* This is variably modified if some field's type is. */
8398 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8399 if (TREE_CODE (t) == FIELD_DECL)
8400 {
8401 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8402 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8403 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8404
8405 /* If the type is a qualified union, then the DECL_QUALIFIER
8406 of fields can also be an expression containing a variable. */
8407 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8408 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8409
8410 /* If the field is a qualified union, then it's only a container
8411 for what's inside so we look into it. That's necessary in LTO
8412 mode because the sizes of the field tested above have been set
8413 to PLACEHOLDER_EXPRs by free_lang_data. */
8414 if (TREE_CODE (TREE_TYPE (t)) == QUAL_UNION_TYPE
8415 && variably_modified_type_p (TREE_TYPE (t), fn))
8416 return true;
8417 }
8418 break;
8419
8420 case ARRAY_TYPE:
8421 /* Do not call ourselves to avoid infinite recursion. This is
8422 variably modified if the element type is. */
8423 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8424 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8425 break;
8426
8427 default:
8428 break;
8429 }
8430
8431 /* The current language may have other cases to check, but in general,
8432 all other types are not variably modified. */
8433 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8434
8435#undef RETURN_TRUE_IF_VAR
8436}
8437
8438/* Given a DECL or TYPE, return the scope in which it was declared, or
8439 NULL_TREE if there is no containing scope. */
8440
8441tree
8442get_containing_scope (const_tree t)
8443{
8444 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8445}
8446
8447/* Returns the ultimate TRANSLATION_UNIT_DECL context of DECL or NULL. */
8448
8449const_tree
8450get_ultimate_context (const_tree decl)
8451{
8452 while (decl && TREE_CODE (decl) != TRANSLATION_UNIT_DECL)
8453 {
8454 if (TREE_CODE (decl) == BLOCK)
8455 decl = BLOCK_SUPERCONTEXT (decl);
8456 else
8457 decl = get_containing_scope (t: decl);
8458 }
8459 return decl;
8460}
8461
8462/* Return the innermost context enclosing DECL that is
8463 a FUNCTION_DECL, or zero if none. */
8464
8465tree
8466decl_function_context (const_tree decl)
8467{
8468 tree context;
8469
8470 if (TREE_CODE (decl) == ERROR_MARK)
8471 return 0;
8472
8473 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8474 where we look up the function at runtime. Such functions always take
8475 a first argument of type 'pointer to real context'.
8476
8477 C++ should really be fixed to use DECL_CONTEXT for the real context,
8478 and use something else for the "virtual context". */
8479 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
8480 context
8481 = TYPE_MAIN_VARIANT
8482 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8483 else
8484 context = DECL_CONTEXT (decl);
8485
8486 while (context && TREE_CODE (context) != FUNCTION_DECL)
8487 {
8488 if (TREE_CODE (context) == BLOCK)
8489 context = BLOCK_SUPERCONTEXT (context);
8490 else
8491 context = get_containing_scope (t: context);
8492 }
8493
8494 return context;
8495}
8496
8497/* Return the innermost context enclosing DECL that is
8498 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8499 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8500
8501tree
8502decl_type_context (const_tree decl)
8503{
8504 tree context = DECL_CONTEXT (decl);
8505
8506 while (context)
8507 switch (TREE_CODE (context))
8508 {
8509 case NAMESPACE_DECL:
8510 case TRANSLATION_UNIT_DECL:
8511 return NULL_TREE;
8512
8513 case RECORD_TYPE:
8514 case UNION_TYPE:
8515 case QUAL_UNION_TYPE:
8516 return context;
8517
8518 case TYPE_DECL:
8519 case FUNCTION_DECL:
8520 context = DECL_CONTEXT (context);
8521 break;
8522
8523 case BLOCK:
8524 context = BLOCK_SUPERCONTEXT (context);
8525 break;
8526
8527 default:
8528 gcc_unreachable ();
8529 }
8530
8531 return NULL_TREE;
8532}
8533
8534/* CALL is a CALL_EXPR. Return the declaration for the function
8535 called, or NULL_TREE if the called function cannot be
8536 determined. */
8537
8538tree
8539get_callee_fndecl (const_tree call)
8540{
8541 tree addr;
8542
8543 if (call == error_mark_node)
8544 return error_mark_node;
8545
8546 /* It's invalid to call this function with anything but a
8547 CALL_EXPR. */
8548 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8549
8550 /* The first operand to the CALL is the address of the function
8551 called. */
8552 addr = CALL_EXPR_FN (call);
8553
8554 /* If there is no function, return early. */
8555 if (addr == NULL_TREE)
8556 return NULL_TREE;
8557
8558 STRIP_NOPS (addr);
8559
8560 /* If this is a readonly function pointer, extract its initial value. */
8561 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8562 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8563 && DECL_INITIAL (addr))
8564 addr = DECL_INITIAL (addr);
8565
8566 /* If the address is just `&f' for some function `f', then we know
8567 that `f' is being called. */
8568 if (TREE_CODE (addr) == ADDR_EXPR
8569 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8570 return TREE_OPERAND (addr, 0);
8571
8572 /* We couldn't figure out what was being called. */
8573 return NULL_TREE;
8574}
8575
8576/* Return true when STMTs arguments and return value match those of FNDECL,
8577 a decl of a builtin function. */
8578
8579static bool
8580tree_builtin_call_types_compatible_p (const_tree call, tree fndecl)
8581{
8582 gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) != NOT_BUILT_IN);
8583
8584 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
8585 if (tree decl = builtin_decl_explicit (fncode: DECL_FUNCTION_CODE (decl: fndecl)))
8586 fndecl = decl;
8587
8588 bool gimple_form = (cfun && (cfun->curr_properties & PROP_gimple)) != 0;
8589 if (gimple_form
8590 ? !useless_type_conversion_p (TREE_TYPE (call),
8591 TREE_TYPE (TREE_TYPE (fndecl)))
8592 : (TYPE_MAIN_VARIANT (TREE_TYPE (call))
8593 != TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))))
8594 return false;
8595
8596 tree targs = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
8597 unsigned nargs = call_expr_nargs (call);
8598 for (unsigned i = 0; i < nargs; ++i, targs = TREE_CHAIN (targs))
8599 {
8600 /* Variadic args follow. */
8601 if (!targs)
8602 return true;
8603 tree arg = CALL_EXPR_ARG (call, i);
8604 tree type = TREE_VALUE (targs);
8605 if (gimple_form
8606 ? !useless_type_conversion_p (type, TREE_TYPE (arg))
8607 : TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (TREE_TYPE (arg)))
8608 {
8609 /* For pointer arguments be more forgiving, e.g. due to
8610 FILE * vs. fileptr_type_node, or say char * vs. const char *
8611 differences etc. */
8612 if (!gimple_form
8613 && POINTER_TYPE_P (type)
8614 && POINTER_TYPE_P (TREE_TYPE (arg))
8615 && tree_nop_conversion_p (type, TREE_TYPE (arg)))
8616 continue;
8617 /* char/short integral arguments are promoted to int
8618 by several frontends if targetm.calls.promote_prototypes
8619 is true. Allow such promotion too. */
8620 if (INTEGRAL_TYPE_P (type)
8621 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
8622 && INTEGRAL_TYPE_P (TREE_TYPE (arg))
8623 && !TYPE_UNSIGNED (TREE_TYPE (arg))
8624 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl))
8625 && (gimple_form
8626 ? useless_type_conversion_p (integer_type_node,
8627 TREE_TYPE (arg))
8628 : tree_nop_conversion_p (integer_type_node,
8629 TREE_TYPE (arg))))
8630 continue;
8631 return false;
8632 }
8633 }
8634 if (targs && !VOID_TYPE_P (TREE_VALUE (targs)))
8635 return false;
8636 return true;
8637}
8638
8639/* If CALL_EXPR CALL calls a normal built-in function or an internal function,
8640 return the associated function code, otherwise return CFN_LAST. */
8641
8642combined_fn
8643get_call_combined_fn (const_tree call)
8644{
8645 /* It's invalid to call this function with anything but a CALL_EXPR. */
8646 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8647
8648 if (!CALL_EXPR_FN (call))
8649 return as_combined_fn (CALL_EXPR_IFN (call));
8650
8651 tree fndecl = get_callee_fndecl (call);
8652 if (fndecl
8653 && fndecl_built_in_p (node: fndecl, klass: BUILT_IN_NORMAL)
8654 && tree_builtin_call_types_compatible_p (call, fndecl))
8655 return as_combined_fn (fn: DECL_FUNCTION_CODE (decl: fndecl));
8656
8657 return CFN_LAST;
8658}
8659
8660/* Comparator of indices based on tree_node_counts. */
8661
8662static int
8663tree_nodes_cmp (const void *p1, const void *p2)
8664{
8665 const unsigned *n1 = (const unsigned *)p1;
8666 const unsigned *n2 = (const unsigned *)p2;
8667
8668 return tree_node_counts[*n1] - tree_node_counts[*n2];
8669}
8670
8671/* Comparator of indices based on tree_code_counts. */
8672
8673static int
8674tree_codes_cmp (const void *p1, const void *p2)
8675{
8676 const unsigned *n1 = (const unsigned *)p1;
8677 const unsigned *n2 = (const unsigned *)p2;
8678
8679 return tree_code_counts[*n1] - tree_code_counts[*n2];
8680}
8681
8682#define TREE_MEM_USAGE_SPACES 40
8683
8684/* Print debugging information about tree nodes generated during the compile,
8685 and any language-specific information. */
8686
8687void
8688dump_tree_statistics (void)
8689{
8690 if (GATHER_STATISTICS)
8691 {
8692 uint64_t total_nodes, total_bytes;
8693 fprintf (stderr, format: "\nKind Nodes Bytes\n");
8694 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8695 total_nodes = total_bytes = 0;
8696
8697 {
8698 auto_vec<unsigned> indices (all_kinds);
8699 for (unsigned i = 0; i < all_kinds; i++)
8700 indices.quick_push (obj: i);
8701 indices.qsort (tree_nodes_cmp);
8702
8703 for (unsigned i = 0; i < (int) all_kinds; i++)
8704 {
8705 unsigned j = indices[i];
8706 fprintf (stderr, format: "%-20s %6" PRIu64 "%c %9" PRIu64 "%c\n",
8707 tree_node_kind_names[j], SIZE_AMOUNT (tree_node_counts[j]),
8708 SIZE_AMOUNT (tree_node_sizes[j]));
8709 total_nodes += tree_node_counts[j];
8710 total_bytes += tree_node_sizes[j];
8711 }
8712 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8713 fprintf (stderr, format: "%-20s %6" PRIu64 "%c %9" PRIu64 "%c\n", "Total",
8714 SIZE_AMOUNT (total_nodes), SIZE_AMOUNT (total_bytes));
8715 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8716 }
8717
8718 {
8719 fprintf (stderr, format: "Code Nodes\n");
8720 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8721
8722 auto_vec<unsigned> indices (MAX_TREE_CODES);
8723 for (unsigned i = 0; i < MAX_TREE_CODES; i++)
8724 indices.quick_push (obj: i);
8725 indices.qsort (tree_codes_cmp);
8726
8727 for (unsigned i = 0; i < MAX_TREE_CODES; i++)
8728 {
8729 unsigned j = indices[i];
8730 fprintf (stderr, format: "%-32s %6" PRIu64 "%c\n",
8731 get_tree_code_name ((enum tree_code) j),
8732 SIZE_AMOUNT (tree_code_counts[j]));
8733 }
8734 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8735 fprintf (stderr, format: "\n");
8736 ssanames_print_statistics ();
8737 fprintf (stderr, format: "\n");
8738 phinodes_print_statistics ();
8739 fprintf (stderr, format: "\n");
8740 }
8741 }
8742 else
8743 fprintf (stderr, format: "(No per-node statistics)\n");
8744
8745 print_type_hash_statistics ();
8746 print_debug_expr_statistics ();
8747 print_value_expr_statistics ();
8748 lang_hooks.print_statistics ();
8749}
8750
8751#define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8752
8753/* Generate a crc32 of the low BYTES bytes of VALUE. */
8754
8755unsigned
8756crc32_unsigned_n (unsigned chksum, unsigned value, unsigned bytes)
8757{
8758 /* This relies on the raw feedback's top 4 bits being zero. */
8759#define FEEDBACK(X) ((X) * 0x04c11db7)
8760#define SYNDROME(X) (FEEDBACK ((X) & 1) ^ FEEDBACK ((X) & 2) \
8761 ^ FEEDBACK ((X) & 4) ^ FEEDBACK ((X) & 8))
8762 static const unsigned syndromes[16] =
8763 {
8764 SYNDROME(0x0), SYNDROME(0x1), SYNDROME(0x2), SYNDROME(0x3),
8765 SYNDROME(0x4), SYNDROME(0x5), SYNDROME(0x6), SYNDROME(0x7),
8766 SYNDROME(0x8), SYNDROME(0x9), SYNDROME(0xa), SYNDROME(0xb),
8767 SYNDROME(0xc), SYNDROME(0xd), SYNDROME(0xe), SYNDROME(0xf),
8768 };
8769#undef FEEDBACK
8770#undef SYNDROME
8771
8772 value <<= (32 - bytes * 8);
8773 for (unsigned ix = bytes * 2; ix--; value <<= 4)
8774 {
8775 unsigned feedback = syndromes[((value ^ chksum) >> 28) & 0xf];
8776
8777 chksum = (chksum << 4) ^ feedback;
8778 }
8779
8780 return chksum;
8781}
8782
8783/* Generate a crc32 of a string. */
8784
8785unsigned
8786crc32_string (unsigned chksum, const char *string)
8787{
8788 do
8789 chksum = crc32_byte (chksum, byte: *string);
8790 while (*string++);
8791 return chksum;
8792}
8793
8794/* P is a string that will be used in a symbol. Mask out any characters
8795 that are not valid in that context. */
8796
8797void
8798clean_symbol_name (char *p)
8799{
8800 for (; *p; p++)
8801 if (! (ISALNUM (*p)
8802#ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
8803 || *p == '$'
8804#endif
8805#ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
8806 || *p == '.'
8807#endif
8808 ))
8809 *p = '_';
8810}
8811
8812static GTY(()) unsigned anon_cnt = 0; /* Saved for PCH. */
8813
8814/* Create a unique anonymous identifier. The identifier is still a
8815 valid assembly label. */
8816
8817tree
8818make_anon_name ()
8819{
8820 const char *fmt =
8821#if !defined (NO_DOT_IN_LABEL)
8822 "."
8823#elif !defined (NO_DOLLAR_IN_LABEL)
8824 "$"
8825#else
8826 "_"
8827#endif
8828 "_anon_%d";
8829
8830 char buf[24];
8831 int len = snprintf (s: buf, maxlen: sizeof (buf), format: fmt, anon_cnt++);
8832 gcc_checking_assert (len < int (sizeof (buf)));
8833
8834 tree id = get_identifier_with_length (buf, len);
8835 IDENTIFIER_ANON_P (id) = true;
8836
8837 return id;
8838}
8839
8840/* Generate a name for a special-purpose function.
8841 The generated name may need to be unique across the whole link.
8842 Changes to this function may also require corresponding changes to
8843 xstrdup_mask_random.
8844 TYPE is some string to identify the purpose of this function to the
8845 linker or collect2; it must start with an uppercase letter,
8846 one of:
8847 I - for constructors
8848 D - for destructors
8849 N - for C++ anonymous namespaces
8850 F - for DWARF unwind frame information. */
8851
8852tree
8853get_file_function_name (const char *type)
8854{
8855 char *buf;
8856 const char *p;
8857 char *q;
8858
8859 /* If we already have a name we know to be unique, just use that. */
8860 if (first_global_object_name)
8861 p = q = ASTRDUP (first_global_object_name);
8862 /* If the target is handling the constructors/destructors, they
8863 will be local to this file and the name is only necessary for
8864 debugging purposes.
8865 We also assign sub_I and sub_D sufixes to constructors called from
8866 the global static constructors. These are always local. */
8867 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
8868 || (startswith (str: type, prefix: "sub_")
8869 && (type[4] == 'I' || type[4] == 'D')))
8870 {
8871 const char *file = main_input_filename;
8872 if (! file)
8873 file = LOCATION_FILE (input_location);
8874 /* Just use the file's basename, because the full pathname
8875 might be quite long. */
8876 p = q = ASTRDUP (lbasename (file));
8877 }
8878 else
8879 {
8880 /* Otherwise, the name must be unique across the entire link.
8881 We don't have anything that we know to be unique to this translation
8882 unit, so use what we do have and throw in some randomness. */
8883 unsigned len;
8884 const char *name = weak_global_object_name;
8885 const char *file = main_input_filename;
8886
8887 if (! name)
8888 name = "";
8889 if (! file)
8890 file = LOCATION_FILE (input_location);
8891
8892 len = strlen (s: file);
8893 q = (char *) alloca (9 + 19 + len + 1);
8894 memcpy (dest: q, src: file, n: len + 1);
8895
8896 snprintf (s: q + len, maxlen: 9 + 19 + 1, format: "_%08X_" HOST_WIDE_INT_PRINT_HEX,
8897 crc32_string (chksum: 0, string: name), get_random_seed (false));
8898
8899 p = q;
8900 }
8901
8902 clean_symbol_name (p: q);
8903 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
8904 + strlen (type));
8905
8906 /* Set up the name of the file-level functions we may need.
8907 Use a global object (which is already required to be unique over
8908 the program) rather than the file name (which imposes extra
8909 constraints). */
8910 sprintf (s: buf, FILE_FUNCTION_FORMAT, type, p);
8911
8912 return get_identifier (buf);
8913}
8914
8915#if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
8916
8917/* Complain that the tree code of NODE does not match the expected 0
8918 terminated list of trailing codes. The trailing code list can be
8919 empty, for a more vague error message. FILE, LINE, and FUNCTION
8920 are of the caller. */
8921
8922void
8923tree_check_failed (const_tree node, const char *file,
8924 int line, const char *function, ...)
8925{
8926 va_list args;
8927 const char *buffer;
8928 unsigned length = 0;
8929 enum tree_code code;
8930
8931 va_start (args, function);
8932 while ((code = (enum tree_code) va_arg (args, int)))
8933 length += 4 + strlen (s: get_tree_code_name (code));
8934 va_end (args);
8935 if (length)
8936 {
8937 char *tmp;
8938 va_start (args, function);
8939 length += strlen (s: "expected ");
8940 buffer = tmp = (char *) alloca (length);
8941 length = 0;
8942 while ((code = (enum tree_code) va_arg (args, int)))
8943 {
8944 const char *prefix = length ? " or " : "expected ";
8945
8946 strcpy (dest: tmp + length, src: prefix);
8947 length += strlen (s: prefix);
8948 strcpy (dest: tmp + length, src: get_tree_code_name (code));
8949 length += strlen (s: get_tree_code_name (code));
8950 }
8951 va_end (args);
8952 }
8953 else
8954 buffer = "unexpected node";
8955
8956 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8957 buffer, get_tree_code_name (TREE_CODE (node)),
8958 function, trim_filename (file), line);
8959}
8960
8961/* Complain that the tree code of NODE does match the expected 0
8962 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
8963 the caller. */
8964
8965void
8966tree_not_check_failed (const_tree node, const char *file,
8967 int line, const char *function, ...)
8968{
8969 va_list args;
8970 char *buffer;
8971 unsigned length = 0;
8972 enum tree_code code;
8973
8974 va_start (args, function);
8975 while ((code = (enum tree_code) va_arg (args, int)))
8976 length += 4 + strlen (s: get_tree_code_name (code));
8977 va_end (args);
8978 va_start (args, function);
8979 buffer = (char *) alloca (length);
8980 length = 0;
8981 while ((code = (enum tree_code) va_arg (args, int)))
8982 {
8983 if (length)
8984 {
8985 strcpy (dest: buffer + length, src: " or ");
8986 length += 4;
8987 }
8988 strcpy (dest: buffer + length, src: get_tree_code_name (code));
8989 length += strlen (s: get_tree_code_name (code));
8990 }
8991 va_end (args);
8992
8993 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
8994 buffer, get_tree_code_name (TREE_CODE (node)),
8995 function, trim_filename (file), line);
8996}
8997
8998/* Similar to tree_check_failed, except that we check for a class of tree
8999 code, given in CL. */
9000
9001void
9002tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9003 const char *file, int line, const char *function)
9004{
9005 internal_error
9006 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9007 TREE_CODE_CLASS_STRING (cl),
9008 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9009 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9010}
9011
9012/* Similar to tree_check_failed, except that instead of specifying a
9013 dozen codes, use the knowledge that they're all sequential. */
9014
9015void
9016tree_range_check_failed (const_tree node, const char *file, int line,
9017 const char *function, enum tree_code c1,
9018 enum tree_code c2)
9019{
9020 char *buffer;
9021 unsigned length = 0;
9022 unsigned int c;
9023
9024 for (c = c1; c <= c2; ++c)
9025 length += 4 + strlen (s: get_tree_code_name ((enum tree_code) c));
9026
9027 length += strlen (s: "expected ");
9028 buffer = (char *) alloca (length);
9029 length = 0;
9030
9031 for (c = c1; c <= c2; ++c)
9032 {
9033 const char *prefix = length ? " or " : "expected ";
9034
9035 strcpy (dest: buffer + length, src: prefix);
9036 length += strlen (s: prefix);
9037 strcpy (dest: buffer + length, src: get_tree_code_name ((enum tree_code) c));
9038 length += strlen (s: get_tree_code_name ((enum tree_code) c));
9039 }
9040
9041 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9042 buffer, get_tree_code_name (TREE_CODE (node)),
9043 function, trim_filename (file), line);
9044}
9045
9046
9047/* Similar to tree_check_failed, except that we check that a tree does
9048 not have the specified code, given in CL. */
9049
9050void
9051tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9052 const char *file, int line, const char *function)
9053{
9054 internal_error
9055 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9056 TREE_CODE_CLASS_STRING (cl),
9057 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9058 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9059}
9060
9061
9062/* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9063
9064void
9065omp_clause_check_failed (const_tree node, const char *file, int line,
9066 const char *function, enum omp_clause_code code)
9067{
9068 internal_error ("tree check: expected %<omp_clause %s%>, have %qs "
9069 "in %s, at %s:%d",
9070 omp_clause_code_name[code],
9071 get_tree_code_name (TREE_CODE (node)),
9072 function, trim_filename (file), line);
9073}
9074
9075
9076/* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9077
9078void
9079omp_clause_range_check_failed (const_tree node, const char *file, int line,
9080 const char *function, enum omp_clause_code c1,
9081 enum omp_clause_code c2)
9082{
9083 char *buffer;
9084 unsigned length = 0;
9085 unsigned int c;
9086
9087 for (c = c1; c <= c2; ++c)
9088 length += 4 + strlen (s: omp_clause_code_name[c]);
9089
9090 length += strlen (s: "expected ");
9091 buffer = (char *) alloca (length);
9092 length = 0;
9093
9094 for (c = c1; c <= c2; ++c)
9095 {
9096 const char *prefix = length ? " or " : "expected ";
9097
9098 strcpy (dest: buffer + length, src: prefix);
9099 length += strlen (s: prefix);
9100 strcpy (dest: buffer + length, src: omp_clause_code_name[c]);
9101 length += strlen (s: omp_clause_code_name[c]);
9102 }
9103
9104 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9105 buffer, omp_clause_code_name[TREE_CODE (node)],
9106 function, trim_filename (file), line);
9107}
9108
9109
9110#undef DEFTREESTRUCT
9111#define DEFTREESTRUCT(VAL, NAME) NAME,
9112
9113static const char *ts_enum_names[] = {
9114#include "treestruct.def"
9115};
9116#undef DEFTREESTRUCT
9117
9118#define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9119
9120/* Similar to tree_class_check_failed, except that we check for
9121 whether CODE contains the tree structure identified by EN. */
9122
9123void
9124tree_contains_struct_check_failed (const_tree node,
9125 const enum tree_node_structure_enum en,
9126 const char *file, int line,
9127 const char *function)
9128{
9129 internal_error
9130 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9131 TS_ENUM_NAME (en),
9132 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9133}
9134
9135
9136/* Similar to above, except that the check is for the bounds of a TREE_VEC's
9137 (dynamically sized) vector. */
9138
9139void
9140tree_int_cst_elt_check_failed (int idx, int len, const char *file, int line,
9141 const char *function)
9142{
9143 internal_error
9144 ("tree check: accessed elt %d of %<tree_int_cst%> with %d elts in %s, "
9145 "at %s:%d",
9146 idx + 1, len, function, trim_filename (file), line);
9147}
9148
9149/* Similar to above, except that the check is for the bounds of a TREE_VEC's
9150 (dynamically sized) vector. */
9151
9152void
9153tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9154 const char *function)
9155{
9156 internal_error
9157 ("tree check: accessed elt %d of %<tree_vec%> with %d elts in %s, at %s:%d",
9158 idx + 1, len, function, trim_filename (file), line);
9159}
9160
9161/* Similar to above, except that the check is for the bounds of the operand
9162 vector of an expression node EXP. */
9163
9164void
9165tree_operand_check_failed (int idx, const_tree exp, const char *file,
9166 int line, const char *function)
9167{
9168 enum tree_code code = TREE_CODE (exp);
9169 internal_error
9170 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9171 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9172 function, trim_filename (file), line);
9173}
9174
9175/* Similar to above, except that the check is for the number of
9176 operands of an OMP_CLAUSE node. */
9177
9178void
9179omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9180 int line, const char *function)
9181{
9182 internal_error
9183 ("tree check: accessed operand %d of %<omp_clause %s%> with %d operands "
9184 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9185 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9186 trim_filename (file), line);
9187}
9188#endif /* ENABLE_TREE_CHECKING */
9189
9190/* Create a new vector type node holding NUNITS units of type INNERTYPE,
9191 and mapped to the machine mode MODE. Initialize its fields and build
9192 the information necessary for debugging output. */
9193
9194static tree
9195make_vector_type (tree innertype, poly_int64 nunits, machine_mode mode)
9196{
9197 tree t;
9198 tree mv_innertype = TYPE_MAIN_VARIANT (innertype);
9199
9200 t = make_node (code: VECTOR_TYPE);
9201 TREE_TYPE (t) = mv_innertype;
9202 SET_TYPE_VECTOR_SUBPARTS (node: t, subparts: nunits);
9203 SET_TYPE_MODE (t, mode);
9204
9205 if (TYPE_STRUCTURAL_EQUALITY_P (mv_innertype) || in_lto_p)
9206 SET_TYPE_STRUCTURAL_EQUALITY (t);
9207 else if ((TYPE_CANONICAL (mv_innertype) != innertype
9208 || mode != VOIDmode)
9209 && !VECTOR_BOOLEAN_TYPE_P (t))
9210 TYPE_CANONICAL (t)
9211 = make_vector_type (TYPE_CANONICAL (mv_innertype), nunits, VOIDmode);
9212
9213 layout_type (t);
9214
9215 hashval_t hash = type_hash_canon_hash (type: t);
9216 t = type_hash_canon (hashcode: hash, type: t);
9217
9218 /* We have built a main variant, based on the main variant of the
9219 inner type. Use it to build the variant we return. */
9220 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9221 && TREE_TYPE (t) != innertype)
9222 return build_type_attribute_qual_variant (t,
9223 TYPE_ATTRIBUTES (innertype),
9224 TYPE_QUALS (innertype));
9225
9226 return t;
9227}
9228
9229static tree
9230make_or_reuse_type (unsigned size, int unsignedp)
9231{
9232 int i;
9233
9234 if (size == INT_TYPE_SIZE)
9235 return unsignedp ? unsigned_type_node : integer_type_node;
9236 if (size == CHAR_TYPE_SIZE)
9237 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9238 if (size == SHORT_TYPE_SIZE)
9239 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9240 if (size == LONG_TYPE_SIZE)
9241 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9242 if (size == LONG_LONG_TYPE_SIZE)
9243 return (unsignedp ? long_long_unsigned_type_node
9244 : long_long_integer_type_node);
9245
9246 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9247 if (size == int_n_data[i].bitsize
9248 && int_n_enabled_p[i])
9249 return (unsignedp ? int_n_trees[i].unsigned_type
9250 : int_n_trees[i].signed_type);
9251
9252 if (unsignedp)
9253 return make_unsigned_type (size);
9254 else
9255 return make_signed_type (size);
9256}
9257
9258/* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9259
9260static tree
9261make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9262{
9263 if (satp)
9264 {
9265 if (size == SHORT_FRACT_TYPE_SIZE)
9266 return unsignedp ? sat_unsigned_short_fract_type_node
9267 : sat_short_fract_type_node;
9268 if (size == FRACT_TYPE_SIZE)
9269 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9270 if (size == LONG_FRACT_TYPE_SIZE)
9271 return unsignedp ? sat_unsigned_long_fract_type_node
9272 : sat_long_fract_type_node;
9273 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9274 return unsignedp ? sat_unsigned_long_long_fract_type_node
9275 : sat_long_long_fract_type_node;
9276 }
9277 else
9278 {
9279 if (size == SHORT_FRACT_TYPE_SIZE)
9280 return unsignedp ? unsigned_short_fract_type_node
9281 : short_fract_type_node;
9282 if (size == FRACT_TYPE_SIZE)
9283 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9284 if (size == LONG_FRACT_TYPE_SIZE)
9285 return unsignedp ? unsigned_long_fract_type_node
9286 : long_fract_type_node;
9287 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9288 return unsignedp ? unsigned_long_long_fract_type_node
9289 : long_long_fract_type_node;
9290 }
9291
9292 return make_fract_type (size, unsignedp, satp);
9293}
9294
9295/* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9296
9297static tree
9298make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9299{
9300 if (satp)
9301 {
9302 if (size == SHORT_ACCUM_TYPE_SIZE)
9303 return unsignedp ? sat_unsigned_short_accum_type_node
9304 : sat_short_accum_type_node;
9305 if (size == ACCUM_TYPE_SIZE)
9306 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9307 if (size == LONG_ACCUM_TYPE_SIZE)
9308 return unsignedp ? sat_unsigned_long_accum_type_node
9309 : sat_long_accum_type_node;
9310 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9311 return unsignedp ? sat_unsigned_long_long_accum_type_node
9312 : sat_long_long_accum_type_node;
9313 }
9314 else
9315 {
9316 if (size == SHORT_ACCUM_TYPE_SIZE)
9317 return unsignedp ? unsigned_short_accum_type_node
9318 : short_accum_type_node;
9319 if (size == ACCUM_TYPE_SIZE)
9320 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9321 if (size == LONG_ACCUM_TYPE_SIZE)
9322 return unsignedp ? unsigned_long_accum_type_node
9323 : long_accum_type_node;
9324 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9325 return unsignedp ? unsigned_long_long_accum_type_node
9326 : long_long_accum_type_node;
9327 }
9328
9329 return make_accum_type (size, unsignedp, satp);
9330}
9331
9332
9333/* Create an atomic variant node for TYPE. This routine is called
9334 during initialization of data types to create the 5 basic atomic
9335 types. The generic build_variant_type function requires these to
9336 already be set up in order to function properly, so cannot be
9337 called from there. If ALIGN is non-zero, then ensure alignment is
9338 overridden to this value. */
9339
9340static tree
9341build_atomic_base (tree type, unsigned int align)
9342{
9343 tree t;
9344
9345 /* Make sure its not already registered. */
9346 if ((t = get_qualified_type (type, type_quals: TYPE_QUAL_ATOMIC)))
9347 return t;
9348
9349 t = build_variant_type_copy (type);
9350 set_type_quals (type: t, type_quals: TYPE_QUAL_ATOMIC);
9351
9352 if (align)
9353 SET_TYPE_ALIGN (t, align);
9354
9355 return t;
9356}
9357
9358/* Information about the _FloatN and _FloatNx types. This must be in
9359 the same order as the corresponding TI_* enum values. */
9360const floatn_type_info floatn_nx_types[NUM_FLOATN_NX_TYPES] =
9361 {
9362 { .n: 16, .extended: false },
9363 { .n: 32, .extended: false },
9364 { .n: 64, .extended: false },
9365 { .n: 128, .extended: false },
9366 { .n: 32, .extended: true },
9367 { .n: 64, .extended: true },
9368 { .n: 128, .extended: true },
9369 };
9370
9371
9372/* Create nodes for all integer types (and error_mark_node) using the sizes
9373 of C datatypes. SIGNED_CHAR specifies whether char is signed. */
9374
9375void
9376build_common_tree_nodes (bool signed_char)
9377{
9378 int i;
9379
9380 error_mark_node = make_node (code: ERROR_MARK);
9381 TREE_TYPE (error_mark_node) = error_mark_node;
9382
9383 initialize_sizetypes ();
9384
9385 /* Define both `signed char' and `unsigned char'. */
9386 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9387 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9388 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9389 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9390
9391 /* Define `char', which is like either `signed char' or `unsigned char'
9392 but not the same as either. */
9393 char_type_node
9394 = (signed_char
9395 ? make_signed_type (CHAR_TYPE_SIZE)
9396 : make_unsigned_type (CHAR_TYPE_SIZE));
9397 TYPE_STRING_FLAG (char_type_node) = 1;
9398
9399 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9400 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9401 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9402 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9403 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9404 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9405 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9406 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9407
9408 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9409 {
9410 int_n_trees[i].signed_type = make_signed_type (int_n_data[i].bitsize);
9411 int_n_trees[i].unsigned_type = make_unsigned_type (int_n_data[i].bitsize);
9412
9413 if (int_n_enabled_p[i])
9414 {
9415 integer_types[itk_intN_0 + i * 2] = int_n_trees[i].signed_type;
9416 integer_types[itk_unsigned_intN_0 + i * 2] = int_n_trees[i].unsigned_type;
9417 }
9418 }
9419
9420 /* Define a boolean type. This type only represents boolean values but
9421 may be larger than char depending on the value of BOOL_TYPE_SIZE. */
9422 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9423 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9424 TYPE_PRECISION (boolean_type_node) = 1;
9425 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, cst: 1);
9426
9427 /* Define what type to use for size_t. */
9428 if (strcmp (SIZE_TYPE, s2: "unsigned int") == 0)
9429 size_type_node = unsigned_type_node;
9430 else if (strcmp (SIZE_TYPE, s2: "long unsigned int") == 0)
9431 size_type_node = long_unsigned_type_node;
9432 else if (strcmp (SIZE_TYPE, s2: "long long unsigned int") == 0)
9433 size_type_node = long_long_unsigned_type_node;
9434 else if (strcmp (SIZE_TYPE, s2: "short unsigned int") == 0)
9435 size_type_node = short_unsigned_type_node;
9436 else
9437 {
9438 int i;
9439
9440 size_type_node = NULL_TREE;
9441 for (i = 0; i < NUM_INT_N_ENTS; i++)
9442 if (int_n_enabled_p[i])
9443 {
9444 char name[50], altname[50];
9445 sprintf (s: name, format: "__int%d unsigned", int_n_data[i].bitsize);
9446 sprintf (s: altname, format: "__int%d__ unsigned", int_n_data[i].bitsize);
9447
9448 if (strcmp (s1: name, SIZE_TYPE) == 0
9449 || strcmp (s1: altname, SIZE_TYPE) == 0)
9450 {
9451 size_type_node = int_n_trees[i].unsigned_type;
9452 }
9453 }
9454 if (size_type_node == NULL_TREE)
9455 gcc_unreachable ();
9456 }
9457
9458 /* Define what type to use for ptrdiff_t. */
9459 if (strcmp (PTRDIFF_TYPE, s2: "int") == 0)
9460 ptrdiff_type_node = integer_type_node;
9461 else if (strcmp (PTRDIFF_TYPE, s2: "long int") == 0)
9462 ptrdiff_type_node = long_integer_type_node;
9463 else if (strcmp (PTRDIFF_TYPE, s2: "long long int") == 0)
9464 ptrdiff_type_node = long_long_integer_type_node;
9465 else if (strcmp (PTRDIFF_TYPE, s2: "short int") == 0)
9466 ptrdiff_type_node = short_integer_type_node;
9467 else
9468 {
9469 ptrdiff_type_node = NULL_TREE;
9470 for (int i = 0; i < NUM_INT_N_ENTS; i++)
9471 if (int_n_enabled_p[i])
9472 {
9473 char name[50], altname[50];
9474 sprintf (s: name, format: "__int%d", int_n_data[i].bitsize);
9475 sprintf (s: altname, format: "__int%d__", int_n_data[i].bitsize);
9476
9477 if (strcmp (s1: name, PTRDIFF_TYPE) == 0
9478 || strcmp (s1: altname, PTRDIFF_TYPE) == 0)
9479 ptrdiff_type_node = int_n_trees[i].signed_type;
9480 }
9481 if (ptrdiff_type_node == NULL_TREE)
9482 gcc_unreachable ();
9483 }
9484
9485 /* Fill in the rest of the sized types. Reuse existing type nodes
9486 when possible. */
9487 intQI_type_node = make_or_reuse_type (size: GET_MODE_BITSIZE (QImode), unsignedp: 0);
9488 intHI_type_node = make_or_reuse_type (size: GET_MODE_BITSIZE (HImode), unsignedp: 0);
9489 intSI_type_node = make_or_reuse_type (size: GET_MODE_BITSIZE (SImode), unsignedp: 0);
9490 intDI_type_node = make_or_reuse_type (size: GET_MODE_BITSIZE (DImode), unsignedp: 0);
9491 intTI_type_node = make_or_reuse_type (size: GET_MODE_BITSIZE (TImode), unsignedp: 0);
9492
9493 unsigned_intQI_type_node = make_or_reuse_type (size: GET_MODE_BITSIZE (QImode), unsignedp: 1);
9494 unsigned_intHI_type_node = make_or_reuse_type (size: GET_MODE_BITSIZE (HImode), unsignedp: 1);
9495 unsigned_intSI_type_node = make_or_reuse_type (size: GET_MODE_BITSIZE (SImode), unsignedp: 1);
9496 unsigned_intDI_type_node = make_or_reuse_type (size: GET_MODE_BITSIZE (DImode), unsignedp: 1);
9497 unsigned_intTI_type_node = make_or_reuse_type (size: GET_MODE_BITSIZE (TImode), unsignedp: 1);
9498
9499 /* Don't call build_qualified type for atomics. That routine does
9500 special processing for atomics, and until they are initialized
9501 it's better not to make that call.
9502
9503 Check to see if there is a target override for atomic types. */
9504
9505 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
9506 align: targetm.atomic_align_for_mode (QImode));
9507 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
9508 align: targetm.atomic_align_for_mode (HImode));
9509 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
9510 align: targetm.atomic_align_for_mode (SImode));
9511 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
9512 align: targetm.atomic_align_for_mode (DImode));
9513 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
9514 align: targetm.atomic_align_for_mode (TImode));
9515
9516 access_public_node = get_identifier ("public");
9517 access_protected_node = get_identifier ("protected");
9518 access_private_node = get_identifier ("private");
9519
9520 /* Define these next since types below may used them. */
9521 integer_zero_node = build_int_cst (integer_type_node, cst: 0);
9522 integer_one_node = build_int_cst (integer_type_node, cst: 1);
9523 integer_three_node = build_int_cst (integer_type_node, cst: 3);
9524 integer_minus_one_node = build_int_cst (integer_type_node, cst: -1);
9525
9526 size_zero_node = size_int (0);
9527 size_one_node = size_int (1);
9528 bitsize_zero_node = bitsize_int (0);
9529 bitsize_one_node = bitsize_int (1);
9530 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9531
9532 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9533 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9534
9535 void_type_node = make_node (code: VOID_TYPE);
9536 layout_type (void_type_node);
9537
9538 /* We are not going to have real types in C with less than byte alignment,
9539 so we might as well not have any types that claim to have it. */
9540 SET_TYPE_ALIGN (void_type_node, BITS_PER_UNIT);
9541 TYPE_USER_ALIGN (void_type_node) = 0;
9542
9543 void_node = make_node (code: VOID_CST);
9544 TREE_TYPE (void_node) = void_type_node;
9545
9546 void_list_node = build_tree_list (NULL_TREE, void_type_node);
9547
9548 null_pointer_node = build_int_cst (type: build_pointer_type (void_type_node), cst: 0);
9549 layout_type (TREE_TYPE (null_pointer_node));
9550
9551 ptr_type_node = build_pointer_type (void_type_node);
9552 const_ptr_type_node
9553 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9554 for (unsigned i = 0; i < ARRAY_SIZE (builtin_structptr_types); ++i)
9555 builtin_structptr_types[i].node = builtin_structptr_types[i].base;
9556
9557 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, unsignedp: 1);
9558
9559 float_type_node = make_node (code: REAL_TYPE);
9560 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9561 layout_type (float_type_node);
9562
9563 double_type_node = make_node (code: REAL_TYPE);
9564 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9565 layout_type (double_type_node);
9566
9567 long_double_type_node = make_node (code: REAL_TYPE);
9568 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9569 layout_type (long_double_type_node);
9570
9571 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
9572 {
9573 int n = floatn_nx_types[i].n;
9574 bool extended = floatn_nx_types[i].extended;
9575 scalar_float_mode mode;
9576 if (!targetm.floatn_mode (n, extended).exists (mode: &mode))
9577 continue;
9578 int precision = GET_MODE_PRECISION (mode);
9579 /* Work around the rs6000 KFmode having precision 113 not
9580 128. */
9581 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
9582 gcc_assert (fmt->b == 2 && fmt->emin + fmt->emax == 3);
9583 int min_precision = fmt->p + ceil_log2 (x: fmt->emax - fmt->emin);
9584 if (!extended)
9585 gcc_assert (min_precision == n);
9586 if (precision < min_precision)
9587 precision = min_precision;
9588 FLOATN_NX_TYPE_NODE (i) = make_node (code: REAL_TYPE);
9589 TYPE_PRECISION (FLOATN_NX_TYPE_NODE (i)) = precision;
9590 layout_type (FLOATN_NX_TYPE_NODE (i));
9591 SET_TYPE_MODE (FLOATN_NX_TYPE_NODE (i), mode);
9592 }
9593 float128t_type_node = float128_type_node;
9594#ifdef HAVE_BFmode
9595 if (REAL_MODE_FORMAT (BFmode) == &arm_bfloat_half_format
9596 && targetm.scalar_mode_supported_p (BFmode)
9597 && targetm.libgcc_floating_mode_supported_p (BFmode))
9598 {
9599 bfloat16_type_node = make_node (code: REAL_TYPE);
9600 TYPE_PRECISION (bfloat16_type_node) = GET_MODE_PRECISION (BFmode);
9601 layout_type (bfloat16_type_node);
9602 SET_TYPE_MODE (bfloat16_type_node, BFmode);
9603 }
9604#endif
9605
9606 float_ptr_type_node = build_pointer_type (float_type_node);
9607 double_ptr_type_node = build_pointer_type (double_type_node);
9608 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9609 integer_ptr_type_node = build_pointer_type (integer_type_node);
9610
9611 /* Fixed size integer types. */
9612 uint16_type_node = make_or_reuse_type (size: 16, unsignedp: 1);
9613 uint32_type_node = make_or_reuse_type (size: 32, unsignedp: 1);
9614 uint64_type_node = make_or_reuse_type (size: 64, unsignedp: 1);
9615 if (targetm.scalar_mode_supported_p (TImode))
9616 uint128_type_node = make_or_reuse_type (size: 128, unsignedp: 1);
9617
9618 /* Decimal float types. */
9619 if (targetm.decimal_float_supported_p ())
9620 {
9621 dfloat32_type_node = make_node (code: REAL_TYPE);
9622 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9623 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9624 layout_type (dfloat32_type_node);
9625
9626 dfloat64_type_node = make_node (code: REAL_TYPE);
9627 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9628 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9629 layout_type (dfloat64_type_node);
9630
9631 dfloat128_type_node = make_node (code: REAL_TYPE);
9632 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9633 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9634 layout_type (dfloat128_type_node);
9635 }
9636
9637 complex_integer_type_node = build_complex_type (integer_type_node, named: true);
9638 complex_float_type_node = build_complex_type (float_type_node, named: true);
9639 complex_double_type_node = build_complex_type (double_type_node, named: true);
9640 complex_long_double_type_node = build_complex_type (long_double_type_node,
9641 named: true);
9642
9643 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
9644 {
9645 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
9646 COMPLEX_FLOATN_NX_TYPE_NODE (i)
9647 = build_complex_type (FLOATN_NX_TYPE_NODE (i));
9648 }
9649
9650/* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9651#define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9652 sat_ ## KIND ## _type_node = \
9653 make_sat_signed_ ## KIND ## _type (SIZE); \
9654 sat_unsigned_ ## KIND ## _type_node = \
9655 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9656 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9657 unsigned_ ## KIND ## _type_node = \
9658 make_unsigned_ ## KIND ## _type (SIZE);
9659
9660#define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9661 sat_ ## WIDTH ## KIND ## _type_node = \
9662 make_sat_signed_ ## KIND ## _type (SIZE); \
9663 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9664 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9665 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9666 unsigned_ ## WIDTH ## KIND ## _type_node = \
9667 make_unsigned_ ## KIND ## _type (SIZE);
9668
9669/* Make fixed-point type nodes based on four different widths. */
9670#define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9671 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9672 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9673 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9674 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9675
9676/* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9677#define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9678 NAME ## _type_node = \
9679 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9680 u ## NAME ## _type_node = \
9681 make_or_reuse_unsigned_ ## KIND ## _type \
9682 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9683 sat_ ## NAME ## _type_node = \
9684 make_or_reuse_sat_signed_ ## KIND ## _type \
9685 (GET_MODE_BITSIZE (MODE ## mode)); \
9686 sat_u ## NAME ## _type_node = \
9687 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9688 (GET_MODE_BITSIZE (U ## MODE ## mode));
9689
9690 /* Fixed-point type and mode nodes. */
9691 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9692 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9693 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9694 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9695 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9696 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9697 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9698 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9699 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9700 MAKE_FIXED_MODE_NODE (accum, da, DA)
9701 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9702
9703 {
9704 tree t = targetm.build_builtin_va_list ();
9705
9706 /* Many back-ends define record types without setting TYPE_NAME.
9707 If we copied the record type here, we'd keep the original
9708 record type without a name. This breaks name mangling. So,
9709 don't copy record types and let c_common_nodes_and_builtins()
9710 declare the type to be __builtin_va_list. */
9711 if (TREE_CODE (t) != RECORD_TYPE)
9712 t = build_variant_type_copy (type: t);
9713
9714 va_list_type_node = t;
9715 }
9716
9717 /* SCEV analyzer global shared trees. */
9718 chrec_dont_know = make_node (code: SCEV_NOT_KNOWN);
9719 TREE_TYPE (chrec_dont_know) = void_type_node;
9720 chrec_known = make_node (code: SCEV_KNOWN);
9721 TREE_TYPE (chrec_known) = void_type_node;
9722}
9723
9724/* Modify DECL for given flags.
9725 TM_PURE attribute is set only on types, so the function will modify
9726 DECL's type when ECF_TM_PURE is used. */
9727
9728void
9729set_call_expr_flags (tree decl, int flags)
9730{
9731 if (flags & ECF_NOTHROW)
9732 TREE_NOTHROW (decl) = 1;
9733 if (flags & ECF_CONST)
9734 TREE_READONLY (decl) = 1;
9735 if (flags & ECF_PURE)
9736 DECL_PURE_P (decl) = 1;
9737 if (flags & ECF_LOOPING_CONST_OR_PURE)
9738 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9739 if (flags & ECF_NOVOPS)
9740 DECL_IS_NOVOPS (decl) = 1;
9741 if (flags & ECF_NORETURN)
9742 TREE_THIS_VOLATILE (decl) = 1;
9743 if (flags & ECF_MALLOC)
9744 DECL_IS_MALLOC (decl) = 1;
9745 if (flags & ECF_RETURNS_TWICE)
9746 DECL_IS_RETURNS_TWICE (decl) = 1;
9747 if (flags & ECF_LEAF)
9748 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9749 NULL, DECL_ATTRIBUTES (decl));
9750 if (flags & ECF_COLD)
9751 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("cold"),
9752 NULL, DECL_ATTRIBUTES (decl));
9753 if (flags & ECF_RET1)
9754 DECL_ATTRIBUTES (decl)
9755 = tree_cons (get_identifier ("fn spec"),
9756 value: build_tree_list (NULL_TREE, value: build_string (len: 2, str: "1 ")),
9757 DECL_ATTRIBUTES (decl));
9758 if ((flags & ECF_TM_PURE) && flag_tm)
9759 apply_tm_attr (decl, get_identifier ("transaction_pure"));
9760 if ((flags & ECF_XTHROW))
9761 DECL_ATTRIBUTES (decl)
9762 = tree_cons (get_identifier ("expected_throw"),
9763 NULL, DECL_ATTRIBUTES (decl));
9764 /* Looping const or pure is implied by noreturn.
9765 There is currently no way to declare looping const or looping pure alone. */
9766 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9767 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9768}
9769
9770
9771/* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9772
9773static void
9774local_define_builtin (const char *name, tree type, enum built_in_function code,
9775 const char *library_name, int ecf_flags)
9776{
9777 tree decl;
9778
9779 decl = add_builtin_function (name, type, function_code: code, cl: BUILT_IN_NORMAL,
9780 library_name, NULL_TREE);
9781 set_call_expr_flags (decl, flags: ecf_flags);
9782
9783 set_builtin_decl (fncode: code, decl, implicit_p: true);
9784}
9785
9786/* Call this function after instantiating all builtins that the language
9787 front end cares about. This will build the rest of the builtins
9788 and internal functions that are relied upon by the tree optimizers and
9789 the middle-end. */
9790
9791void
9792build_common_builtin_nodes (void)
9793{
9794 tree tmp, ftype;
9795 int ecf_flags;
9796
9797 if (!builtin_decl_explicit_p (fncode: BUILT_IN_CLEAR_PADDING))
9798 {
9799 ftype = build_function_type_list (void_type_node,
9800 ptr_type_node,
9801 ptr_type_node,
9802 integer_type_node,
9803 NULL_TREE);
9804 local_define_builtin (name: "__builtin_clear_padding", type: ftype,
9805 code: BUILT_IN_CLEAR_PADDING,
9806 library_name: "__builtin_clear_padding",
9807 ECF_LEAF | ECF_NOTHROW);
9808 }
9809
9810 if (!builtin_decl_explicit_p (fncode: BUILT_IN_UNREACHABLE)
9811 || !builtin_decl_explicit_p (fncode: BUILT_IN_TRAP)
9812 || !builtin_decl_explicit_p (fncode: BUILT_IN_UNREACHABLE_TRAP)
9813 || !builtin_decl_explicit_p (fncode: BUILT_IN_ABORT))
9814 {
9815 ftype = build_function_type (void_type_node, void_list_node);
9816 if (!builtin_decl_explicit_p (fncode: BUILT_IN_UNREACHABLE))
9817 local_define_builtin (name: "__builtin_unreachable", type: ftype,
9818 code: BUILT_IN_UNREACHABLE,
9819 library_name: "__builtin_unreachable",
9820 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9821 | ECF_CONST | ECF_COLD);
9822 if (!builtin_decl_explicit_p (fncode: BUILT_IN_UNREACHABLE_TRAP))
9823 local_define_builtin (name: "__builtin_unreachable trap", type: ftype,
9824 code: BUILT_IN_UNREACHABLE_TRAP,
9825 library_name: "__builtin_unreachable trap",
9826 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9827 | ECF_CONST | ECF_COLD);
9828 if (!builtin_decl_explicit_p (fncode: BUILT_IN_ABORT))
9829 local_define_builtin (name: "__builtin_abort", type: ftype, code: BUILT_IN_ABORT,
9830 library_name: "abort",
9831 ECF_LEAF | ECF_NORETURN | ECF_CONST | ECF_COLD);
9832 if (!builtin_decl_explicit_p (fncode: BUILT_IN_TRAP))
9833 local_define_builtin (name: "__builtin_trap", type: ftype, code: BUILT_IN_TRAP,
9834 library_name: "__builtin_trap",
9835 ECF_NORETURN | ECF_NOTHROW | ECF_LEAF | ECF_COLD);
9836 }
9837
9838 if (!builtin_decl_explicit_p (fncode: BUILT_IN_MEMCPY)
9839 || !builtin_decl_explicit_p (fncode: BUILT_IN_MEMMOVE))
9840 {
9841 ftype = build_function_type_list (ptr_type_node,
9842 ptr_type_node, const_ptr_type_node,
9843 size_type_node, NULL_TREE);
9844
9845 if (!builtin_decl_explicit_p (fncode: BUILT_IN_MEMCPY))
9846 local_define_builtin (name: "__builtin_memcpy", type: ftype, code: BUILT_IN_MEMCPY,
9847 library_name: "memcpy", ECF_NOTHROW | ECF_LEAF);
9848 if (!builtin_decl_explicit_p (fncode: BUILT_IN_MEMMOVE))
9849 local_define_builtin (name: "__builtin_memmove", type: ftype, code: BUILT_IN_MEMMOVE,
9850 library_name: "memmove", ECF_NOTHROW | ECF_LEAF);
9851 }
9852
9853 if (!builtin_decl_explicit_p (fncode: BUILT_IN_MEMCMP))
9854 {
9855 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9856 const_ptr_type_node, size_type_node,
9857 NULL_TREE);
9858 local_define_builtin (name: "__builtin_memcmp", type: ftype, code: BUILT_IN_MEMCMP,
9859 library_name: "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9860 }
9861
9862 if (!builtin_decl_explicit_p (fncode: BUILT_IN_MEMSET))
9863 {
9864 ftype = build_function_type_list (ptr_type_node,
9865 ptr_type_node, integer_type_node,
9866 size_type_node, NULL_TREE);
9867 local_define_builtin (name: "__builtin_memset", type: ftype, code: BUILT_IN_MEMSET,
9868 library_name: "memset", ECF_NOTHROW | ECF_LEAF);
9869 }
9870
9871 /* If we're checking the stack, `alloca' can throw. */
9872 const int alloca_flags
9873 = ECF_MALLOC | ECF_LEAF | (flag_stack_check ? 0 : ECF_NOTHROW);
9874
9875 if (!builtin_decl_explicit_p (fncode: BUILT_IN_ALLOCA))
9876 {
9877 ftype = build_function_type_list (ptr_type_node,
9878 size_type_node, NULL_TREE);
9879 local_define_builtin (name: "__builtin_alloca", type: ftype, code: BUILT_IN_ALLOCA,
9880 library_name: "alloca", ecf_flags: alloca_flags);
9881 }
9882
9883 ftype = build_function_type_list (ptr_type_node, size_type_node,
9884 size_type_node, NULL_TREE);
9885 local_define_builtin (name: "__builtin_alloca_with_align", type: ftype,
9886 code: BUILT_IN_ALLOCA_WITH_ALIGN,
9887 library_name: "__builtin_alloca_with_align",
9888 ecf_flags: alloca_flags);
9889
9890 ftype = build_function_type_list (ptr_type_node, size_type_node,
9891 size_type_node, size_type_node, NULL_TREE);
9892 local_define_builtin (name: "__builtin_alloca_with_align_and_max", type: ftype,
9893 code: BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX,
9894 library_name: "__builtin_alloca_with_align_and_max",
9895 ecf_flags: alloca_flags);
9896
9897 ftype = build_function_type_list (void_type_node,
9898 ptr_type_node, ptr_type_node,
9899 ptr_type_node, NULL_TREE);
9900 local_define_builtin (name: "__builtin_init_trampoline", type: ftype,
9901 code: BUILT_IN_INIT_TRAMPOLINE,
9902 library_name: "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9903 local_define_builtin (name: "__builtin_init_heap_trampoline", type: ftype,
9904 code: BUILT_IN_INIT_HEAP_TRAMPOLINE,
9905 library_name: "__builtin_init_heap_trampoline",
9906 ECF_NOTHROW | ECF_LEAF);
9907 local_define_builtin (name: "__builtin_init_descriptor", type: ftype,
9908 code: BUILT_IN_INIT_DESCRIPTOR,
9909 library_name: "__builtin_init_descriptor", ECF_NOTHROW | ECF_LEAF);
9910
9911 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9912 local_define_builtin (name: "__builtin_adjust_trampoline", type: ftype,
9913 code: BUILT_IN_ADJUST_TRAMPOLINE,
9914 library_name: "__builtin_adjust_trampoline",
9915 ECF_CONST | ECF_NOTHROW);
9916 local_define_builtin (name: "__builtin_adjust_descriptor", type: ftype,
9917 code: BUILT_IN_ADJUST_DESCRIPTOR,
9918 library_name: "__builtin_adjust_descriptor",
9919 ECF_CONST | ECF_NOTHROW);
9920
9921 ftype = build_function_type_list (void_type_node,
9922 ptr_type_node, ptr_type_node, NULL_TREE);
9923 if (!builtin_decl_explicit_p (fncode: BUILT_IN_CLEAR_CACHE))
9924 local_define_builtin (name: "__builtin___clear_cache", type: ftype,
9925 code: BUILT_IN_CLEAR_CACHE,
9926 library_name: "__clear_cache",
9927 ECF_NOTHROW);
9928
9929 local_define_builtin (name: "__builtin_nonlocal_goto", type: ftype,
9930 code: BUILT_IN_NONLOCAL_GOTO,
9931 library_name: "__builtin_nonlocal_goto",
9932 ECF_NORETURN | ECF_NOTHROW);
9933
9934 tree ptr_ptr_type_node = build_pointer_type (ptr_type_node);
9935
9936 if (!builtin_decl_explicit_p (fncode: BUILT_IN_GCC_NESTED_PTR_CREATED))
9937 {
9938 ftype = build_function_type_list (void_type_node,
9939 ptr_type_node, // void *chain
9940 ptr_type_node, // void *func
9941 ptr_ptr_type_node, // void **dst
9942 NULL_TREE);
9943 local_define_builtin (name: "__builtin___gcc_nested_func_ptr_created", type: ftype,
9944 code: BUILT_IN_GCC_NESTED_PTR_CREATED,
9945 library_name: "__gcc_nested_func_ptr_created", ECF_NOTHROW);
9946 }
9947
9948 if (!builtin_decl_explicit_p (fncode: BUILT_IN_GCC_NESTED_PTR_DELETED))
9949 {
9950 ftype = build_function_type_list (void_type_node, NULL_TREE);
9951 local_define_builtin (name: "__builtin___gcc_nested_func_ptr_deleted", type: ftype,
9952 code: BUILT_IN_GCC_NESTED_PTR_DELETED,
9953 library_name: "__gcc_nested_func_ptr_deleted", ECF_NOTHROW);
9954 }
9955
9956 ftype = build_function_type_list (void_type_node,
9957 ptr_type_node, ptr_type_node, NULL_TREE);
9958 local_define_builtin (name: "__builtin_setjmp_setup", type: ftype,
9959 code: BUILT_IN_SETJMP_SETUP,
9960 library_name: "__builtin_setjmp_setup", ECF_NOTHROW);
9961
9962 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9963 local_define_builtin (name: "__builtin_setjmp_receiver", type: ftype,
9964 code: BUILT_IN_SETJMP_RECEIVER,
9965 library_name: "__builtin_setjmp_receiver", ECF_NOTHROW | ECF_LEAF);
9966
9967 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9968 local_define_builtin (name: "__builtin_stack_save", type: ftype, code: BUILT_IN_STACK_SAVE,
9969 library_name: "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9970
9971 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9972 local_define_builtin (name: "__builtin_stack_restore", type: ftype,
9973 code: BUILT_IN_STACK_RESTORE,
9974 library_name: "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9975
9976 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9977 const_ptr_type_node, size_type_node,
9978 NULL_TREE);
9979 local_define_builtin (name: "__builtin_memcmp_eq", type: ftype, code: BUILT_IN_MEMCMP_EQ,
9980 library_name: "__builtin_memcmp_eq",
9981 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9982
9983 local_define_builtin (name: "__builtin_strncmp_eq", type: ftype, code: BUILT_IN_STRNCMP_EQ,
9984 library_name: "__builtin_strncmp_eq",
9985 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9986
9987 local_define_builtin (name: "__builtin_strcmp_eq", type: ftype, code: BUILT_IN_STRCMP_EQ,
9988 library_name: "__builtin_strcmp_eq",
9989 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9990
9991 /* If there's a possibility that we might use the ARM EABI, build the
9992 alternate __cxa_end_cleanup node used to resume from C++. */
9993 if (targetm.arm_eabi_unwinder)
9994 {
9995 ftype = build_function_type_list (void_type_node, NULL_TREE);
9996 local_define_builtin (name: "__builtin_cxa_end_cleanup", type: ftype,
9997 code: BUILT_IN_CXA_END_CLEANUP,
9998 library_name: "__cxa_end_cleanup",
9999 ECF_NORETURN | ECF_XTHROW | ECF_LEAF);
10000 }
10001
10002 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10003 local_define_builtin (name: "__builtin_unwind_resume", type: ftype,
10004 code: BUILT_IN_UNWIND_RESUME,
10005 library_name: ((targetm_common.except_unwind_info (&global_options)
10006 == UI_SJLJ)
10007 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
10008 ECF_NORETURN | ECF_XTHROW);
10009
10010 if (builtin_decl_explicit (fncode: BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
10011 {
10012 ftype = build_function_type_list (ptr_type_node, integer_type_node,
10013 NULL_TREE);
10014 local_define_builtin (name: "__builtin_return_address", type: ftype,
10015 code: BUILT_IN_RETURN_ADDRESS,
10016 library_name: "__builtin_return_address",
10017 ECF_NOTHROW);
10018 }
10019
10020 if (!builtin_decl_explicit_p (fncode: BUILT_IN_PROFILE_FUNC_ENTER)
10021 || !builtin_decl_explicit_p (fncode: BUILT_IN_PROFILE_FUNC_EXIT))
10022 {
10023 ftype = build_function_type_list (void_type_node, ptr_type_node,
10024 ptr_type_node, NULL_TREE);
10025 if (!builtin_decl_explicit_p (fncode: BUILT_IN_PROFILE_FUNC_ENTER))
10026 local_define_builtin (name: "__cyg_profile_func_enter", type: ftype,
10027 code: BUILT_IN_PROFILE_FUNC_ENTER,
10028 library_name: "__cyg_profile_func_enter", ecf_flags: 0);
10029 if (!builtin_decl_explicit_p (fncode: BUILT_IN_PROFILE_FUNC_EXIT))
10030 local_define_builtin (name: "__cyg_profile_func_exit", type: ftype,
10031 code: BUILT_IN_PROFILE_FUNC_EXIT,
10032 library_name: "__cyg_profile_func_exit", ecf_flags: 0);
10033 }
10034
10035 /* The exception object and filter values from the runtime. The argument
10036 must be zero before exception lowering, i.e. from the front end. After
10037 exception lowering, it will be the region number for the exception
10038 landing pad. These functions are PURE instead of CONST to prevent
10039 them from being hoisted past the exception edge that will initialize
10040 its value in the landing pad. */
10041 ftype = build_function_type_list (ptr_type_node,
10042 integer_type_node, NULL_TREE);
10043 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
10044 /* Only use TM_PURE if we have TM language support. */
10045 if (builtin_decl_explicit_p (fncode: BUILT_IN_TM_LOAD_1))
10046 ecf_flags |= ECF_TM_PURE;
10047 local_define_builtin (name: "__builtin_eh_pointer", type: ftype, code: BUILT_IN_EH_POINTER,
10048 library_name: "__builtin_eh_pointer", ecf_flags);
10049
10050 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
10051 ftype = build_function_type_list (return_type: tmp, integer_type_node, NULL_TREE);
10052 local_define_builtin (name: "__builtin_eh_filter", type: ftype, code: BUILT_IN_EH_FILTER,
10053 library_name: "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10054
10055 ftype = build_function_type_list (void_type_node,
10056 integer_type_node, integer_type_node,
10057 NULL_TREE);
10058 local_define_builtin (name: "__builtin_eh_copy_values", type: ftype,
10059 code: BUILT_IN_EH_COPY_VALUES,
10060 library_name: "__builtin_eh_copy_values", ECF_NOTHROW);
10061
10062 /* Complex multiplication and division. These are handled as builtins
10063 rather than optabs because emit_library_call_value doesn't support
10064 complex. Further, we can do slightly better with folding these
10065 beasties if the real and complex parts of the arguments are separate. */
10066 {
10067 int mode;
10068
10069 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10070 {
10071 char mode_name_buf[4], *q;
10072 const char *p;
10073 enum built_in_function mcode, dcode;
10074 tree type, inner_type;
10075 const char *prefix = "__";
10076
10077 if (targetm.libfunc_gnu_prefix)
10078 prefix = "__gnu_";
10079
10080 type = lang_hooks.types.type_for_mode ((machine_mode) mode, 0);
10081 if (type == NULL)
10082 continue;
10083 inner_type = TREE_TYPE (type);
10084
10085 ftype = build_function_type_list (return_type: type, inner_type, inner_type,
10086 inner_type, inner_type, NULL_TREE);
10087
10088 mcode = ((enum built_in_function)
10089 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10090 dcode = ((enum built_in_function)
10091 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10092
10093 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10094 *q = TOLOWER (*p);
10095 *q = '\0';
10096
10097 /* For -ftrapping-math these should throw from a former
10098 -fnon-call-exception stmt. */
10099 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10100 NULL);
10101 local_define_builtin (name: built_in_names[mcode], type: ftype, code: mcode,
10102 library_name: built_in_names[mcode],
10103 ECF_CONST | ECF_LEAF);
10104
10105 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10106 NULL);
10107 local_define_builtin (name: built_in_names[dcode], type: ftype, code: dcode,
10108 library_name: built_in_names[dcode],
10109 ECF_CONST | ECF_LEAF);
10110 }
10111 }
10112
10113 init_internal_fns ();
10114}
10115
10116/* HACK. GROSS. This is absolutely disgusting. I wish there was a
10117 better way.
10118
10119 If we requested a pointer to a vector, build up the pointers that
10120 we stripped off while looking for the inner type. Similarly for
10121 return values from functions.
10122
10123 The argument TYPE is the top of the chain, and BOTTOM is the
10124 new type which we will point to. */
10125
10126tree
10127reconstruct_complex_type (tree type, tree bottom)
10128{
10129 tree inner, outer;
10130
10131 if (TREE_CODE (type) == POINTER_TYPE)
10132 {
10133 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10134 outer = build_pointer_type_for_mode (to_type: inner, TYPE_MODE (type),
10135 TYPE_REF_CAN_ALIAS_ALL (type));
10136 }
10137 else if (TREE_CODE (type) == REFERENCE_TYPE)
10138 {
10139 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10140 outer = build_reference_type_for_mode (to_type: inner, TYPE_MODE (type),
10141 TYPE_REF_CAN_ALIAS_ALL (type));
10142 }
10143 else if (TREE_CODE (type) == ARRAY_TYPE)
10144 {
10145 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10146 outer = build_array_type (elt_type: inner, TYPE_DOMAIN (type));
10147 }
10148 else if (TREE_CODE (type) == FUNCTION_TYPE)
10149 {
10150 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10151 outer = build_function_type (value_type: inner, TYPE_ARG_TYPES (type),
10152 TYPE_NO_NAMED_ARGS_STDARG_P (type));
10153 }
10154 else if (TREE_CODE (type) == METHOD_TYPE)
10155 {
10156 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10157 /* The build_method_type_directly() routine prepends 'this' to argument list,
10158 so we must compensate by getting rid of it. */
10159 outer
10160 = build_method_type_directly
10161 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10162 rettype: inner,
10163 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10164 }
10165 else if (TREE_CODE (type) == OFFSET_TYPE)
10166 {
10167 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10168 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), type: inner);
10169 }
10170 else
10171 return bottom;
10172
10173 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10174 TYPE_QUALS (type));
10175}
10176
10177/* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10178 the inner type. */
10179tree
10180build_vector_type_for_mode (tree innertype, machine_mode mode)
10181{
10182 poly_int64 nunits;
10183 unsigned int bitsize;
10184
10185 switch (GET_MODE_CLASS (mode))
10186 {
10187 case MODE_VECTOR_BOOL:
10188 case MODE_VECTOR_INT:
10189 case MODE_VECTOR_FLOAT:
10190 case MODE_VECTOR_FRACT:
10191 case MODE_VECTOR_UFRACT:
10192 case MODE_VECTOR_ACCUM:
10193 case MODE_VECTOR_UACCUM:
10194 nunits = GET_MODE_NUNITS (mode);
10195 break;
10196
10197 case MODE_INT:
10198 /* Check that there are no leftover bits. */
10199 bitsize = GET_MODE_BITSIZE (mode: as_a <scalar_int_mode> (m: mode));
10200 gcc_assert (bitsize % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10201 nunits = bitsize / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10202 break;
10203
10204 default:
10205 gcc_unreachable ();
10206 }
10207
10208 return make_vector_type (innertype, nunits, mode);
10209}
10210
10211/* Similarly, but takes the inner type and number of units, which must be
10212 a power of two. */
10213
10214tree
10215build_vector_type (tree innertype, poly_int64 nunits)
10216{
10217 return make_vector_type (innertype, nunits, VOIDmode);
10218}
10219
10220/* Build a truth vector with NUNITS units, giving it mode MASK_MODE. */
10221
10222tree
10223build_truth_vector_type_for_mode (poly_uint64 nunits, machine_mode mask_mode)
10224{
10225 gcc_assert (mask_mode != BLKmode);
10226
10227 unsigned HOST_WIDE_INT esize;
10228 if (VECTOR_MODE_P (mask_mode))
10229 {
10230 poly_uint64 vsize = GET_MODE_PRECISION (mode: mask_mode);
10231 esize = vector_element_size (vsize, nunits);
10232 }
10233 else
10234 esize = 1;
10235
10236 tree bool_type = build_nonstandard_boolean_type (precision: esize);
10237
10238 return make_vector_type (innertype: bool_type, nunits, mode: mask_mode);
10239}
10240
10241/* Build a vector type that holds one boolean result for each element of
10242 vector type VECTYPE. The public interface for this operation is
10243 truth_type_for. */
10244
10245static tree
10246build_truth_vector_type_for (tree vectype)
10247{
10248 machine_mode vector_mode = TYPE_MODE (vectype);
10249 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (node: vectype);
10250
10251 machine_mode mask_mode;
10252 if (VECTOR_MODE_P (vector_mode)
10253 && targetm.vectorize.get_mask_mode (vector_mode).exists (mode: &mask_mode))
10254 return build_truth_vector_type_for_mode (nunits, mask_mode);
10255
10256 poly_uint64 vsize = tree_to_poly_uint64 (TYPE_SIZE (vectype));
10257 unsigned HOST_WIDE_INT esize = vector_element_size (vsize, nunits);
10258 tree bool_type = build_nonstandard_boolean_type (precision: esize);
10259
10260 return make_vector_type (innertype: bool_type, nunits, VOIDmode);
10261}
10262
10263/* Like build_vector_type, but builds a variant type with TYPE_VECTOR_OPAQUE
10264 set. */
10265
10266tree
10267build_opaque_vector_type (tree innertype, poly_int64 nunits)
10268{
10269 tree t = make_vector_type (innertype, nunits, VOIDmode);
10270 tree cand;
10271 /* We always build the non-opaque variant before the opaque one,
10272 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10273 cand = TYPE_NEXT_VARIANT (t);
10274 if (cand
10275 && TYPE_VECTOR_OPAQUE (cand)
10276 && check_qualified_type (cand, base: t, TYPE_QUALS (t)))
10277 return cand;
10278 /* Othewise build a variant type and make sure to queue it after
10279 the non-opaque type. */
10280 cand = build_distinct_type_copy (type: t);
10281 TYPE_VECTOR_OPAQUE (cand) = true;
10282 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10283 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10284 TYPE_NEXT_VARIANT (t) = cand;
10285 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10286 /* Type variants have no alias set defined. */
10287 TYPE_ALIAS_SET (cand) = -1;
10288 return cand;
10289}
10290
10291/* Return the value of element I of VECTOR_CST T as a wide_int. */
10292
10293static poly_wide_int
10294vector_cst_int_elt (const_tree t, unsigned int i)
10295{
10296 /* First handle elements that are directly encoded. */
10297 unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
10298 if (i < encoded_nelts)
10299 return wi::to_poly_wide (VECTOR_CST_ENCODED_ELT (t, i));
10300
10301 /* Identify the pattern that contains element I and work out the index of
10302 the last encoded element for that pattern. */
10303 unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
10304 unsigned int pattern = i % npatterns;
10305 unsigned int count = i / npatterns;
10306 unsigned int final_i = encoded_nelts - npatterns + pattern;
10307
10308 /* If there are no steps, the final encoded value is the right one. */
10309 if (!VECTOR_CST_STEPPED_P (t))
10310 return wi::to_poly_wide (VECTOR_CST_ENCODED_ELT (t, final_i));
10311
10312 /* Otherwise work out the value from the last two encoded elements. */
10313 tree v1 = VECTOR_CST_ENCODED_ELT (t, final_i - npatterns);
10314 tree v2 = VECTOR_CST_ENCODED_ELT (t, final_i);
10315 poly_wide_int diff = wi::to_poly_wide (t: v2) - wi::to_poly_wide (t: v1);
10316 return wi::to_poly_wide (t: v2) + (count - 2) * diff;
10317}
10318
10319/* Return the value of element I of VECTOR_CST T. */
10320
10321tree
10322vector_cst_elt (const_tree t, unsigned int i)
10323{
10324 /* First handle elements that are directly encoded. */
10325 unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
10326 if (i < encoded_nelts)
10327 return VECTOR_CST_ENCODED_ELT (t, i);
10328
10329 /* If there are no steps, the final encoded value is the right one. */
10330 if (!VECTOR_CST_STEPPED_P (t))
10331 {
10332 /* Identify the pattern that contains element I and work out the index of
10333 the last encoded element for that pattern. */
10334 unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
10335 unsigned int pattern = i % npatterns;
10336 unsigned int final_i = encoded_nelts - npatterns + pattern;
10337 return VECTOR_CST_ENCODED_ELT (t, final_i);
10338 }
10339
10340 /* Otherwise work out the value from the last two encoded elements. */
10341 return wide_int_to_tree (TREE_TYPE (TREE_TYPE (t)),
10342 value: vector_cst_int_elt (t, i));
10343}
10344
10345/* Given an initializer INIT, return TRUE if INIT is zero or some
10346 aggregate of zeros. Otherwise return FALSE. If NONZERO is not
10347 null, set *NONZERO if and only if INIT is known not to be all
10348 zeros. The combination of return value of false and *NONZERO
10349 false implies that INIT may but need not be all zeros. Other
10350 combinations indicate definitive answers. */
10351
10352bool
10353initializer_zerop (const_tree init, bool *nonzero /* = NULL */)
10354{
10355 bool dummy;
10356 if (!nonzero)
10357 nonzero = &dummy;
10358
10359 /* Conservatively clear NONZERO and set it only if INIT is definitely
10360 not all zero. */
10361 *nonzero = false;
10362
10363 STRIP_NOPS (init);
10364
10365 unsigned HOST_WIDE_INT off = 0;
10366
10367 switch (TREE_CODE (init))
10368 {
10369 case INTEGER_CST:
10370 if (integer_zerop (expr: init))
10371 return true;
10372
10373 *nonzero = true;
10374 return false;
10375
10376 case REAL_CST:
10377 /* ??? Note that this is not correct for C4X float formats. There,
10378 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10379 negative exponent. */
10380 if (real_zerop (expr: init)
10381 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init)))
10382 return true;
10383
10384 *nonzero = true;
10385 return false;
10386
10387 case FIXED_CST:
10388 if (fixed_zerop (expr: init))
10389 return true;
10390
10391 *nonzero = true;
10392 return false;
10393
10394 case COMPLEX_CST:
10395 if (integer_zerop (expr: init)
10396 || (real_zerop (expr: init)
10397 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10398 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init)))))
10399 return true;
10400
10401 *nonzero = true;
10402 return false;
10403
10404 case VECTOR_CST:
10405 if (VECTOR_CST_NPATTERNS (init) == 1
10406 && VECTOR_CST_DUPLICATE_P (init)
10407 && initializer_zerop (VECTOR_CST_ENCODED_ELT (init, 0)))
10408 return true;
10409
10410 *nonzero = true;
10411 return false;
10412
10413 case CONSTRUCTOR:
10414 {
10415 if (TREE_CLOBBER_P (init))
10416 return false;
10417
10418 unsigned HOST_WIDE_INT idx;
10419 tree elt;
10420
10421 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10422 if (!initializer_zerop (init: elt, nonzero))
10423 return false;
10424
10425 return true;
10426 }
10427
10428 case MEM_REF:
10429 {
10430 tree arg = TREE_OPERAND (init, 0);
10431 if (TREE_CODE (arg) != ADDR_EXPR)
10432 return false;
10433 tree offset = TREE_OPERAND (init, 1);
10434 if (TREE_CODE (offset) != INTEGER_CST
10435 || !tree_fits_uhwi_p (t: offset))
10436 return false;
10437 off = tree_to_uhwi (t: offset);
10438 if (INT_MAX < off)
10439 return false;
10440 arg = TREE_OPERAND (arg, 0);
10441 if (TREE_CODE (arg) != STRING_CST)
10442 return false;
10443 init = arg;
10444 }
10445 /* Fall through. */
10446
10447 case STRING_CST:
10448 {
10449 gcc_assert (off <= INT_MAX);
10450
10451 int i = off;
10452 int n = TREE_STRING_LENGTH (init);
10453 if (n <= i)
10454 return false;
10455
10456 /* We need to loop through all elements to handle cases like
10457 "\0" and "\0foobar". */
10458 for (i = 0; i < n; ++i)
10459 if (TREE_STRING_POINTER (init)[i] != '\0')
10460 {
10461 *nonzero = true;
10462 return false;
10463 }
10464
10465 return true;
10466 }
10467
10468 default:
10469 return false;
10470 }
10471}
10472
10473/* Return true if EXPR is an initializer expression in which every element
10474 is a constant that is numerically equal to 0 or 1. The elements do not
10475 need to be equal to each other. */
10476
10477bool
10478initializer_each_zero_or_onep (const_tree expr)
10479{
10480 STRIP_ANY_LOCATION_WRAPPER (expr);
10481
10482 switch (TREE_CODE (expr))
10483 {
10484 case INTEGER_CST:
10485 return integer_zerop (expr) || integer_onep (expr);
10486
10487 case REAL_CST:
10488 return real_zerop (expr) || real_onep (expr);
10489
10490 case VECTOR_CST:
10491 {
10492 unsigned HOST_WIDE_INT nelts = vector_cst_encoded_nelts (t: expr);
10493 if (VECTOR_CST_STEPPED_P (expr)
10494 && !TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr)).is_constant (const_value: &nelts))
10495 return false;
10496
10497 for (unsigned int i = 0; i < nelts; ++i)
10498 {
10499 tree elt = vector_cst_elt (t: expr, i);
10500 if (!initializer_each_zero_or_onep (expr: elt))
10501 return false;
10502 }
10503
10504 return true;
10505 }
10506
10507 default:
10508 return false;
10509 }
10510}
10511
10512/* Check if vector VEC consists of all the equal elements and
10513 that the number of elements corresponds to the type of VEC.
10514 The function returns first element of the vector
10515 or NULL_TREE if the vector is not uniform. */
10516tree
10517uniform_vector_p (const_tree vec)
10518{
10519 tree first, t;
10520 unsigned HOST_WIDE_INT i, nelts;
10521
10522 if (vec == NULL_TREE)
10523 return NULL_TREE;
10524
10525 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10526
10527 if (TREE_CODE (vec) == VEC_DUPLICATE_EXPR)
10528 return TREE_OPERAND (vec, 0);
10529
10530 else if (TREE_CODE (vec) == VECTOR_CST)
10531 {
10532 if (VECTOR_CST_NPATTERNS (vec) == 1 && VECTOR_CST_DUPLICATE_P (vec))
10533 return VECTOR_CST_ENCODED_ELT (vec, 0);
10534 return NULL_TREE;
10535 }
10536
10537 else if (TREE_CODE (vec) == CONSTRUCTOR
10538 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)).is_constant (const_value: &nelts))
10539 {
10540 first = error_mark_node;
10541
10542 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10543 {
10544 if (i == 0)
10545 {
10546 first = t;
10547 continue;
10548 }
10549 if (!operand_equal_p (first, t, flags: 0))
10550 return NULL_TREE;
10551 }
10552 if (i != nelts)
10553 return NULL_TREE;
10554
10555 if (TREE_CODE (first) == CONSTRUCTOR || TREE_CODE (first) == VECTOR_CST)
10556 return uniform_vector_p (vec: first);
10557 return first;
10558 }
10559
10560 return NULL_TREE;
10561}
10562
10563/* If the argument is INTEGER_CST, return it. If the argument is vector
10564 with all elements the same INTEGER_CST, return that INTEGER_CST. Otherwise
10565 return NULL_TREE.
10566 Look through location wrappers. */
10567
10568tree
10569uniform_integer_cst_p (tree t)
10570{
10571 STRIP_ANY_LOCATION_WRAPPER (t);
10572
10573 if (TREE_CODE (t) == INTEGER_CST)
10574 return t;
10575
10576 if (VECTOR_TYPE_P (TREE_TYPE (t)))
10577 {
10578 t = uniform_vector_p (vec: t);
10579 if (t && TREE_CODE (t) == INTEGER_CST)
10580 return t;
10581 }
10582
10583 return NULL_TREE;
10584}
10585
10586/* Checks to see if T is a constant or a constant vector and if each element E
10587 adheres to ~E + 1 == pow2 then return ~E otherwise NULL_TREE. */
10588
10589tree
10590bitmask_inv_cst_vector_p (tree t)
10591{
10592
10593 tree_code code = TREE_CODE (t);
10594 tree type = TREE_TYPE (t);
10595
10596 if (!INTEGRAL_TYPE_P (type)
10597 && !VECTOR_INTEGER_TYPE_P (type))
10598 return NULL_TREE;
10599
10600 unsigned HOST_WIDE_INT nelts = 1;
10601 tree cst;
10602 unsigned int idx = 0;
10603 bool uniform = uniform_integer_cst_p (t);
10604 tree newtype = unsigned_type_for (type);
10605 tree_vector_builder builder;
10606 if (code == INTEGER_CST)
10607 cst = t;
10608 else
10609 {
10610 if (!VECTOR_CST_NELTS (t).is_constant (const_value: &nelts))
10611 return NULL_TREE;
10612
10613 cst = vector_cst_elt (t, i: 0);
10614 builder.new_vector (type: newtype, npatterns: nelts, nelts_per_pattern: 1);
10615 }
10616
10617 tree ty = unsigned_type_for (TREE_TYPE (cst));
10618
10619 do
10620 {
10621 if (idx > 0)
10622 cst = vector_cst_elt (t, i: idx);
10623 wide_int icst = wi::to_wide (t: cst);
10624 wide_int inv = wi::bit_not (x: icst);
10625 icst = wi::add (x: 1, y: inv);
10626 if (wi::popcount (icst) != 1)
10627 return NULL_TREE;
10628
10629 tree newcst = wide_int_to_tree (type: ty, value: inv);
10630
10631 if (uniform)
10632 return build_uniform_cst (type: newtype, sc: newcst);
10633
10634 builder.quick_push (obj: newcst);
10635 }
10636 while (++idx < nelts);
10637
10638 return builder.build ();
10639}
10640
10641/* If VECTOR_CST T has a single nonzero element, return the index of that
10642 element, otherwise return -1. */
10643
10644int
10645single_nonzero_element (const_tree t)
10646{
10647 unsigned HOST_WIDE_INT nelts;
10648 unsigned int repeat_nelts;
10649 if (VECTOR_CST_NELTS (t).is_constant (const_value: &nelts))
10650 repeat_nelts = nelts;
10651 else if (VECTOR_CST_NELTS_PER_PATTERN (t) == 2)
10652 {
10653 nelts = vector_cst_encoded_nelts (t);
10654 repeat_nelts = VECTOR_CST_NPATTERNS (t);
10655 }
10656 else
10657 return -1;
10658
10659 int res = -1;
10660 for (unsigned int i = 0; i < nelts; ++i)
10661 {
10662 tree elt = vector_cst_elt (t, i);
10663 if (!integer_zerop (expr: elt) && !real_zerop (expr: elt))
10664 {
10665 if (res >= 0 || i >= repeat_nelts)
10666 return -1;
10667 res = i;
10668 }
10669 }
10670 return res;
10671}
10672
10673/* Build an empty statement at location LOC. */
10674
10675tree
10676build_empty_stmt (location_t loc)
10677{
10678 tree t = build1 (code: NOP_EXPR, void_type_node, size_zero_node);
10679 SET_EXPR_LOCATION (t, loc);
10680 return t;
10681}
10682
10683
10684/* Build an OMP clause with code CODE. LOC is the location of the
10685 clause. */
10686
10687tree
10688build_omp_clause (location_t loc, enum omp_clause_code code)
10689{
10690 tree t;
10691 int size, length;
10692
10693 length = omp_clause_num_ops[code];
10694 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10695
10696 record_node_allocation_statistics (code: OMP_CLAUSE, length: size);
10697
10698 t = (tree) ggc_internal_alloc (s: size);
10699 memset (s: t, c: 0, n: size);
10700 TREE_SET_CODE (t, OMP_CLAUSE);
10701 OMP_CLAUSE_SET_CODE (t, code);
10702 OMP_CLAUSE_LOCATION (t) = loc;
10703
10704 return t;
10705}
10706
10707/* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10708 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10709 Except for the CODE and operand count field, other storage for the
10710 object is initialized to zeros. */
10711
10712tree
10713build_vl_exp (enum tree_code code, int len MEM_STAT_DECL)
10714{
10715 tree t;
10716 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10717
10718 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10719 gcc_assert (len >= 1);
10720
10721 record_node_allocation_statistics (code, length);
10722
10723 t = ggc_alloc_cleared_tree_node_stat (s: length PASS_MEM_STAT);
10724
10725 TREE_SET_CODE (t, code);
10726
10727 /* Can't use TREE_OPERAND to store the length because if checking is
10728 enabled, it will try to check the length before we store it. :-P */
10729 t->exp.operands[0] = build_int_cst (sizetype, cst: len);
10730
10731 return t;
10732}
10733
10734/* Helper function for build_call_* functions; build a CALL_EXPR with
10735 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10736 the argument slots. */
10737
10738static tree
10739build_call_1 (tree return_type, tree fn, int nargs)
10740{
10741 tree t;
10742
10743 t = build_vl_exp (code: CALL_EXPR, len: nargs + 3);
10744 TREE_TYPE (t) = return_type;
10745 CALL_EXPR_FN (t) = fn;
10746 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10747
10748 return t;
10749}
10750
10751/* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10752 FN and a null static chain slot. NARGS is the number of call arguments
10753 which are specified as "..." arguments. */
10754
10755tree
10756build_call_nary (tree return_type, tree fn, int nargs, ...)
10757{
10758 tree ret;
10759 va_list args;
10760 va_start (args, nargs);
10761 ret = build_call_valist (return_type, fn, nargs, args);
10762 va_end (args);
10763 return ret;
10764}
10765
10766/* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10767 FN and a null static chain slot. NARGS is the number of call arguments
10768 which are specified as a va_list ARGS. */
10769
10770tree
10771build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10772{
10773 tree t;
10774 int i;
10775
10776 t = build_call_1 (return_type, fn, nargs);
10777 for (i = 0; i < nargs; i++)
10778 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10779 process_call_operands (t);
10780 return t;
10781}
10782
10783/* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10784 FN and a null static chain slot. NARGS is the number of call arguments
10785 which are specified as a tree array ARGS. */
10786
10787tree
10788build_call_array_loc (location_t loc, tree return_type, tree fn,
10789 int nargs, const tree *args)
10790{
10791 tree t;
10792 int i;
10793
10794 t = build_call_1 (return_type, fn, nargs);
10795 for (i = 0; i < nargs; i++)
10796 CALL_EXPR_ARG (t, i) = args[i];
10797 process_call_operands (t);
10798 SET_EXPR_LOCATION (t, loc);
10799 return t;
10800}
10801
10802/* Like build_call_array, but takes a vec. */
10803
10804tree
10805build_call_vec (tree return_type, tree fn, const vec<tree, va_gc> *args)
10806{
10807 tree ret, t;
10808 unsigned int ix;
10809
10810 ret = build_call_1 (return_type, fn, nargs: vec_safe_length (v: args));
10811 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10812 CALL_EXPR_ARG (ret, ix) = t;
10813 process_call_operands (t: ret);
10814 return ret;
10815}
10816
10817/* Conveniently construct a function call expression. FNDECL names the
10818 function to be called and N arguments are passed in the array
10819 ARGARRAY. */
10820
10821tree
10822build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
10823{
10824 tree fntype = TREE_TYPE (fndecl);
10825 tree fn = build1 (code: ADDR_EXPR, type: build_pointer_type (to_type: fntype), node: fndecl);
10826
10827 return fold_build_call_array_loc (loc, TREE_TYPE (fntype), fn, n, argarray);
10828}
10829
10830/* Conveniently construct a function call expression. FNDECL names the
10831 function to be called and the arguments are passed in the vector
10832 VEC. */
10833
10834tree
10835build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
10836{
10837 return build_call_expr_loc_array (loc, fndecl, n: vec_safe_length (v: vec),
10838 argarray: vec_safe_address (v: vec));
10839}
10840
10841
10842/* Conveniently construct a function call expression. FNDECL names the
10843 function to be called, N is the number of arguments, and the "..."
10844 parameters are the argument expressions. */
10845
10846tree
10847build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
10848{
10849 va_list ap;
10850 tree *argarray = XALLOCAVEC (tree, n);
10851 int i;
10852
10853 va_start (ap, n);
10854 for (i = 0; i < n; i++)
10855 argarray[i] = va_arg (ap, tree);
10856 va_end (ap);
10857 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10858}
10859
10860/* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
10861 varargs macros aren't supported by all bootstrap compilers. */
10862
10863tree
10864build_call_expr (tree fndecl, int n, ...)
10865{
10866 va_list ap;
10867 tree *argarray = XALLOCAVEC (tree, n);
10868 int i;
10869
10870 va_start (ap, n);
10871 for (i = 0; i < n; i++)
10872 argarray[i] = va_arg (ap, tree);
10873 va_end (ap);
10874 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
10875}
10876
10877/* Build an internal call to IFN, with arguments ARGS[0:N-1] and with return
10878 type TYPE. This is just like CALL_EXPR, except its CALL_EXPR_FN is NULL.
10879 It will get gimplified later into an ordinary internal function. */
10880
10881tree
10882build_call_expr_internal_loc_array (location_t loc, internal_fn ifn,
10883 tree type, int n, const tree *args)
10884{
10885 tree t = build_call_1 (return_type: type, NULL_TREE, nargs: n);
10886 for (int i = 0; i < n; ++i)
10887 CALL_EXPR_ARG (t, i) = args[i];
10888 SET_EXPR_LOCATION (t, loc);
10889 CALL_EXPR_IFN (t) = ifn;
10890 process_call_operands (t);
10891 return t;
10892}
10893
10894/* Build internal call expression. This is just like CALL_EXPR, except
10895 its CALL_EXPR_FN is NULL. It will get gimplified later into ordinary
10896 internal function. */
10897
10898tree
10899build_call_expr_internal_loc (location_t loc, enum internal_fn ifn,
10900 tree type, int n, ...)
10901{
10902 va_list ap;
10903 tree *argarray = XALLOCAVEC (tree, n);
10904 int i;
10905
10906 va_start (ap, n);
10907 for (i = 0; i < n; i++)
10908 argarray[i] = va_arg (ap, tree);
10909 va_end (ap);
10910 return build_call_expr_internal_loc_array (loc, ifn, type, n, args: argarray);
10911}
10912
10913/* Return a function call to FN, if the target is guaranteed to support it,
10914 or null otherwise.
10915
10916 N is the number of arguments, passed in the "...", and TYPE is the
10917 type of the return value. */
10918
10919tree
10920maybe_build_call_expr_loc (location_t loc, combined_fn fn, tree type,
10921 int n, ...)
10922{
10923 va_list ap;
10924 tree *argarray = XALLOCAVEC (tree, n);
10925 int i;
10926
10927 va_start (ap, n);
10928 for (i = 0; i < n; i++)
10929 argarray[i] = va_arg (ap, tree);
10930 va_end (ap);
10931 if (internal_fn_p (code: fn))
10932 {
10933 internal_fn ifn = as_internal_fn (code: fn);
10934 if (direct_internal_fn_p (fn: ifn))
10935 {
10936 tree_pair types = direct_internal_fn_types (ifn, type, argarray);
10937 if (!direct_internal_fn_supported_p (ifn, types,
10938 OPTIMIZE_FOR_BOTH))
10939 return NULL_TREE;
10940 }
10941 return build_call_expr_internal_loc_array (loc, ifn, type, n, args: argarray);
10942 }
10943 else
10944 {
10945 tree fndecl = builtin_decl_implicit (fncode: as_builtin_fn (code: fn));
10946 if (!fndecl)
10947 return NULL_TREE;
10948 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10949 }
10950}
10951
10952/* Return a function call to the appropriate builtin alloca variant.
10953
10954 SIZE is the size to be allocated. ALIGN, if non-zero, is the requested
10955 alignment of the allocated area. MAX_SIZE, if non-negative, is an upper
10956 bound for SIZE in case it is not a fixed value. */
10957
10958tree
10959build_alloca_call_expr (tree size, unsigned int align, HOST_WIDE_INT max_size)
10960{
10961 if (max_size >= 0)
10962 {
10963 tree t = builtin_decl_explicit (fncode: BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX);
10964 return
10965 build_call_expr (fndecl: t, n: 3, size, size_int (align), size_int (max_size));
10966 }
10967 else if (align > 0)
10968 {
10969 tree t = builtin_decl_explicit (fncode: BUILT_IN_ALLOCA_WITH_ALIGN);
10970 return build_call_expr (fndecl: t, n: 2, size, size_int (align));
10971 }
10972 else
10973 {
10974 tree t = builtin_decl_explicit (fncode: BUILT_IN_ALLOCA);
10975 return build_call_expr (fndecl: t, n: 1, size);
10976 }
10977}
10978
10979/* The built-in decl to use to mark code points believed to be unreachable.
10980 Typically __builtin_unreachable, but __builtin_trap if
10981 -fsanitize=unreachable -fsanitize-trap=unreachable. If only
10982 -fsanitize=unreachable, we rely on sanopt to replace calls with the
10983 appropriate ubsan function. When building a call directly, use
10984 {gimple_},build_builtin_unreachable instead. */
10985
10986tree
10987builtin_decl_unreachable ()
10988{
10989 enum built_in_function fncode = BUILT_IN_UNREACHABLE;
10990
10991 if (sanitize_flags_p (flag: SANITIZE_UNREACHABLE)
10992 ? (flag_sanitize_trap & SANITIZE_UNREACHABLE)
10993 : flag_unreachable_traps)
10994 fncode = BUILT_IN_UNREACHABLE_TRAP;
10995 /* For non-trapping sanitize, we will rewrite __builtin_unreachable () later,
10996 in the sanopt pass. */
10997
10998 return builtin_decl_explicit (fncode);
10999}
11000
11001/* Build a call to __builtin_unreachable, possibly rewritten by
11002 -fsanitize=unreachable. Use this rather than the above when practical. */
11003
11004tree
11005build_builtin_unreachable (location_t loc)
11006{
11007 tree data = NULL_TREE;
11008 tree fn = sanitize_unreachable_fn (data: &data, loc);
11009 return build_call_expr_loc (loc, fndecl: fn, n: data != NULL_TREE, data);
11010}
11011
11012/* Create a new constant string literal of type ELTYPE[SIZE] (or LEN
11013 if SIZE == -1) and return a tree node representing char* pointer to
11014 it as an ADDR_EXPR (ARRAY_REF (ELTYPE, ...)). When STR is nonnull
11015 the STRING_CST value is the LEN bytes at STR (the representation
11016 of the string, which may be wide). Otherwise it's all zeros. */
11017
11018tree
11019build_string_literal (unsigned len, const char *str /* = NULL */,
11020 tree eltype /* = char_type_node */,
11021 unsigned HOST_WIDE_INT size /* = -1 */)
11022{
11023 tree t = build_string (len, str);
11024 /* Set the maximum valid index based on the string length or SIZE. */
11025 unsigned HOST_WIDE_INT maxidx
11026 = (size == HOST_WIDE_INT_M1U ? len : size) - 1;
11027
11028 tree index = build_index_type (size_int (maxidx));
11029 eltype = build_type_variant (eltype, 1, 0);
11030 tree type = build_array_type (elt_type: eltype, index_type: index);
11031 TREE_TYPE (t) = type;
11032 TREE_CONSTANT (t) = 1;
11033 TREE_READONLY (t) = 1;
11034 TREE_STATIC (t) = 1;
11035
11036 type = build_pointer_type (to_type: eltype);
11037 t = build1 (code: ADDR_EXPR, type,
11038 node: build4 (code: ARRAY_REF, tt: eltype,
11039 arg0: t, integer_zero_node, NULL_TREE, NULL_TREE));
11040 return t;
11041}
11042
11043
11044
11045/* Return true if T (assumed to be a DECL) must be assigned a memory
11046 location. */
11047
11048bool
11049needs_to_live_in_memory (const_tree t)
11050{
11051 return (TREE_ADDRESSABLE (t)
11052 || is_global_var (t)
11053 || (TREE_CODE (t) == RESULT_DECL
11054 && !DECL_BY_REFERENCE (t)
11055 && aggregate_value_p (t, current_function_decl)));
11056}
11057
11058/* Return value of a constant X and sign-extend it. */
11059
11060HOST_WIDE_INT
11061int_cst_value (const_tree x)
11062{
11063 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
11064 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
11065
11066 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
11067 gcc_assert (cst_and_fits_in_hwi (x));
11068
11069 if (bits < HOST_BITS_PER_WIDE_INT)
11070 {
11071 bool negative = ((val >> (bits - 1)) & 1) != 0;
11072 if (negative)
11073 val |= HOST_WIDE_INT_M1U << (bits - 1) << 1;
11074 else
11075 val &= ~(HOST_WIDE_INT_M1U << (bits - 1) << 1);
11076 }
11077
11078 return val;
11079}
11080
11081/* If TYPE is an integral or pointer type, return an integer type with
11082 the same precision which is unsigned iff UNSIGNEDP is true, or itself
11083 if TYPE is already an integer type of signedness UNSIGNEDP.
11084 If TYPE is a floating-point type, return an integer type with the same
11085 bitsize and with the signedness given by UNSIGNEDP; this is useful
11086 when doing bit-level operations on a floating-point value. */
11087
11088tree
11089signed_or_unsigned_type_for (int unsignedp, tree type)
11090{
11091 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) == unsignedp)
11092 return type;
11093
11094 if (TREE_CODE (type) == VECTOR_TYPE)
11095 {
11096 tree inner = TREE_TYPE (type);
11097 tree inner2 = signed_or_unsigned_type_for (unsignedp, type: inner);
11098 if (!inner2)
11099 return NULL_TREE;
11100 if (inner == inner2)
11101 return type;
11102 machine_mode new_mode;
11103 if (VECTOR_MODE_P (TYPE_MODE (type))
11104 && related_int_vector_mode (TYPE_MODE (type)).exists (mode: &new_mode))
11105 return build_vector_type_for_mode (innertype: inner2, mode: new_mode);
11106 return build_vector_type (innertype: inner2, nunits: TYPE_VECTOR_SUBPARTS (node: type));
11107 }
11108
11109 if (TREE_CODE (type) == COMPLEX_TYPE)
11110 {
11111 tree inner = TREE_TYPE (type);
11112 tree inner2 = signed_or_unsigned_type_for (unsignedp, type: inner);
11113 if (!inner2)
11114 return NULL_TREE;
11115 if (inner == inner2)
11116 return type;
11117 return build_complex_type (component_type: inner2);
11118 }
11119
11120 unsigned int bits;
11121 if (INTEGRAL_TYPE_P (type)
11122 || POINTER_TYPE_P (type)
11123 || TREE_CODE (type) == OFFSET_TYPE)
11124 bits = TYPE_PRECISION (type);
11125 else if (TREE_CODE (type) == REAL_TYPE)
11126 bits = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (type));
11127 else
11128 return NULL_TREE;
11129
11130 if (TREE_CODE (type) == BITINT_TYPE && (unsignedp || bits > 1))
11131 return build_bitint_type (precision: bits, unsignedp);
11132 return build_nonstandard_integer_type (precision: bits, unsignedp);
11133}
11134
11135/* If TYPE is an integral or pointer type, return an integer type with
11136 the same precision which is unsigned, or itself if TYPE is already an
11137 unsigned integer type. If TYPE is a floating-point type, return an
11138 unsigned integer type with the same bitsize as TYPE. */
11139
11140tree
11141unsigned_type_for (tree type)
11142{
11143 return signed_or_unsigned_type_for (unsignedp: 1, type);
11144}
11145
11146/* If TYPE is an integral or pointer type, return an integer type with
11147 the same precision which is signed, or itself if TYPE is already a
11148 signed integer type. If TYPE is a floating-point type, return a
11149 signed integer type with the same bitsize as TYPE. */
11150
11151tree
11152signed_type_for (tree type)
11153{
11154 return signed_or_unsigned_type_for (unsignedp: 0, type);
11155}
11156
11157/* - For VECTOR_TYPEs:
11158 - The truth type must be a VECTOR_BOOLEAN_TYPE.
11159 - The number of elements must match (known_eq).
11160 - targetm.vectorize.get_mask_mode exists, and exactly
11161 the same mode as the truth type.
11162 - Otherwise, the truth type must be a BOOLEAN_TYPE
11163 or useless_type_conversion_p to BOOLEAN_TYPE. */
11164bool
11165is_truth_type_for (tree type, tree truth_type)
11166{
11167 machine_mode mask_mode = TYPE_MODE (truth_type);
11168 machine_mode vmode = TYPE_MODE (type);
11169 machine_mode tmask_mode;
11170
11171 if (TREE_CODE (type) == VECTOR_TYPE)
11172 {
11173 if (VECTOR_BOOLEAN_TYPE_P (truth_type)
11174 && known_eq (TYPE_VECTOR_SUBPARTS (type),
11175 TYPE_VECTOR_SUBPARTS (truth_type))
11176 && targetm.vectorize.get_mask_mode (vmode).exists (mode: &tmask_mode)
11177 && tmask_mode == mask_mode)
11178 return true;
11179
11180 return false;
11181 }
11182
11183 return useless_type_conversion_p (boolean_type_node, truth_type);
11184}
11185
11186/* If TYPE is a vector type, return a signed integer vector type with the
11187 same width and number of subparts. Otherwise return boolean_type_node. */
11188
11189tree
11190truth_type_for (tree type)
11191{
11192 if (TREE_CODE (type) == VECTOR_TYPE)
11193 {
11194 if (VECTOR_BOOLEAN_TYPE_P (type))
11195 return type;
11196 return build_truth_vector_type_for (vectype: type);
11197 }
11198 else
11199 return boolean_type_node;
11200}
11201
11202/* Returns the largest value obtainable by casting something in INNER type to
11203 OUTER type. */
11204
11205tree
11206upper_bound_in_type (tree outer, tree inner)
11207{
11208 unsigned int det = 0;
11209 unsigned oprec = TYPE_PRECISION (outer);
11210 unsigned iprec = TYPE_PRECISION (inner);
11211 unsigned prec;
11212
11213 /* Compute a unique number for every combination. */
11214 det |= (oprec > iprec) ? 4 : 0;
11215 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
11216 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
11217
11218 /* Determine the exponent to use. */
11219 switch (det)
11220 {
11221 case 0:
11222 case 1:
11223 /* oprec <= iprec, outer: signed, inner: don't care. */
11224 prec = oprec - 1;
11225 break;
11226 case 2:
11227 case 3:
11228 /* oprec <= iprec, outer: unsigned, inner: don't care. */
11229 prec = oprec;
11230 break;
11231 case 4:
11232 /* oprec > iprec, outer: signed, inner: signed. */
11233 prec = iprec - 1;
11234 break;
11235 case 5:
11236 /* oprec > iprec, outer: signed, inner: unsigned. */
11237 prec = iprec;
11238 break;
11239 case 6:
11240 /* oprec > iprec, outer: unsigned, inner: signed. */
11241 prec = oprec;
11242 break;
11243 case 7:
11244 /* oprec > iprec, outer: unsigned, inner: unsigned. */
11245 prec = iprec;
11246 break;
11247 default:
11248 gcc_unreachable ();
11249 }
11250
11251 return wide_int_to_tree (type: outer,
11252 value: wi::mask (width: prec, negate_p: false, TYPE_PRECISION (outer)));
11253}
11254
11255/* Returns the smallest value obtainable by casting something in INNER type to
11256 OUTER type. */
11257
11258tree
11259lower_bound_in_type (tree outer, tree inner)
11260{
11261 unsigned oprec = TYPE_PRECISION (outer);
11262 unsigned iprec = TYPE_PRECISION (inner);
11263
11264 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
11265 and obtain 0. */
11266 if (TYPE_UNSIGNED (outer)
11267 /* If we are widening something of an unsigned type, OUTER type
11268 contains all values of INNER type. In particular, both INNER
11269 and OUTER types have zero in common. */
11270 || (oprec > iprec && TYPE_UNSIGNED (inner)))
11271 return build_int_cst (type: outer, cst: 0);
11272 else
11273 {
11274 /* If we are widening a signed type to another signed type, we
11275 want to obtain -2^^(iprec-1). If we are keeping the
11276 precision or narrowing to a signed type, we want to obtain
11277 -2^(oprec-1). */
11278 unsigned prec = oprec > iprec ? iprec : oprec;
11279 return wide_int_to_tree (type: outer,
11280 value: wi::mask (width: prec - 1, negate_p: true,
11281 TYPE_PRECISION (outer)));
11282 }
11283}
11284
11285/* Return true if two operands that are suitable for PHI nodes are
11286 necessarily equal. Specifically, both ARG0 and ARG1 must be either
11287 SSA_NAME or invariant. Note that this is strictly an optimization.
11288 That is, callers of this function can directly call operand_equal_p
11289 and get the same result, only slower. */
11290
11291bool
11292operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
11293{
11294 if (arg0 == arg1)
11295 return true;
11296 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
11297 return false;
11298 return operand_equal_p (arg0, arg1, flags: 0);
11299}
11300
11301/* Returns number of zeros at the end of binary representation of X. */
11302
11303tree
11304num_ending_zeros (const_tree x)
11305{
11306 return build_int_cst (TREE_TYPE (x), cst: wi::ctz (wi::to_wide (t: x)));
11307}
11308
11309
11310#define WALK_SUBTREE(NODE) \
11311 do \
11312 { \
11313 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
11314 if (result) \
11315 return result; \
11316 } \
11317 while (0)
11318
11319/* This is a subroutine of walk_tree that walks field of TYPE that are to
11320 be walked whenever a type is seen in the tree. Rest of operands and return
11321 value are as for walk_tree. */
11322
11323static tree
11324walk_type_fields (tree type, walk_tree_fn func, void *data,
11325 hash_set<tree> *pset, walk_tree_lh lh)
11326{
11327 tree result = NULL_TREE;
11328
11329 switch (TREE_CODE (type))
11330 {
11331 case POINTER_TYPE:
11332 case REFERENCE_TYPE:
11333 case VECTOR_TYPE:
11334 /* We have to worry about mutually recursive pointers. These can't
11335 be written in C. They can in Ada. It's pathological, but
11336 there's an ACATS test (c38102a) that checks it. Deal with this
11337 by checking if we're pointing to another pointer, that one
11338 points to another pointer, that one does too, and we have no htab.
11339 If so, get a hash table. We check three levels deep to avoid
11340 the cost of the hash table if we don't need one. */
11341 if (POINTER_TYPE_P (TREE_TYPE (type))
11342 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
11343 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
11344 && !pset)
11345 {
11346 result = walk_tree_without_duplicates (&TREE_TYPE (type),
11347 func, data);
11348 if (result)
11349 return result;
11350
11351 break;
11352 }
11353
11354 /* fall through */
11355
11356 case COMPLEX_TYPE:
11357 WALK_SUBTREE (TREE_TYPE (type));
11358 break;
11359
11360 case METHOD_TYPE:
11361 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
11362
11363 /* Fall through. */
11364
11365 case FUNCTION_TYPE:
11366 WALK_SUBTREE (TREE_TYPE (type));
11367 {
11368 tree arg;
11369
11370 /* We never want to walk into default arguments. */
11371 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
11372 WALK_SUBTREE (TREE_VALUE (arg));
11373 }
11374 break;
11375
11376 case ARRAY_TYPE:
11377 /* Don't follow this nodes's type if a pointer for fear that
11378 we'll have infinite recursion. If we have a PSET, then we
11379 need not fear. */
11380 if (pset
11381 || (!POINTER_TYPE_P (TREE_TYPE (type))
11382 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
11383 WALK_SUBTREE (TREE_TYPE (type));
11384 WALK_SUBTREE (TYPE_DOMAIN (type));
11385 break;
11386
11387 case OFFSET_TYPE:
11388 WALK_SUBTREE (TREE_TYPE (type));
11389 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
11390 break;
11391
11392 default:
11393 break;
11394 }
11395
11396 return NULL_TREE;
11397}
11398
11399/* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
11400 called with the DATA and the address of each sub-tree. If FUNC returns a
11401 non-NULL value, the traversal is stopped, and the value returned by FUNC
11402 is returned. If PSET is non-NULL it is used to record the nodes visited,
11403 and to avoid visiting a node more than once. */
11404
11405tree
11406walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
11407 hash_set<tree> *pset, walk_tree_lh lh)
11408{
11409#define WALK_SUBTREE_TAIL(NODE) \
11410 do \
11411 { \
11412 tp = & (NODE); \
11413 goto tail_recurse; \
11414 } \
11415 while (0)
11416
11417 tail_recurse:
11418 /* Skip empty subtrees. */
11419 if (!*tp)
11420 return NULL_TREE;
11421
11422 /* Don't walk the same tree twice, if the user has requested
11423 that we avoid doing so. */
11424 if (pset && pset->add (k: *tp))
11425 return NULL_TREE;
11426
11427 /* Call the function. */
11428 int walk_subtrees = 1;
11429 tree result = (*func) (tp, &walk_subtrees, data);
11430
11431 /* If we found something, return it. */
11432 if (result)
11433 return result;
11434
11435 tree t = *tp;
11436 tree_code code = TREE_CODE (t);
11437
11438 /* Even if we didn't, FUNC may have decided that there was nothing
11439 interesting below this point in the tree. */
11440 if (!walk_subtrees)
11441 {
11442 /* But we still need to check our siblings. */
11443 if (code == TREE_LIST)
11444 WALK_SUBTREE_TAIL (TREE_CHAIN (t));
11445 else if (code == OMP_CLAUSE)
11446 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (t));
11447 else
11448 return NULL_TREE;
11449 }
11450
11451 if (lh)
11452 {
11453 result = (*lh) (tp, &walk_subtrees, func, data, pset);
11454 if (result || !walk_subtrees)
11455 return result;
11456 }
11457
11458 switch (code)
11459 {
11460 case ERROR_MARK:
11461 case IDENTIFIER_NODE:
11462 case INTEGER_CST:
11463 case REAL_CST:
11464 case FIXED_CST:
11465 case STRING_CST:
11466 case BLOCK:
11467 case PLACEHOLDER_EXPR:
11468 case SSA_NAME:
11469 case FIELD_DECL:
11470 case RESULT_DECL:
11471 /* None of these have subtrees other than those already walked
11472 above. */
11473 break;
11474
11475 case TREE_LIST:
11476 WALK_SUBTREE (TREE_VALUE (t));
11477 WALK_SUBTREE_TAIL (TREE_CHAIN (t));
11478
11479 case TREE_VEC:
11480 {
11481 int len = TREE_VEC_LENGTH (t);
11482
11483 if (len == 0)
11484 break;
11485
11486 /* Walk all elements but the last. */
11487 for (int i = 0; i < len - 1; ++i)
11488 WALK_SUBTREE (TREE_VEC_ELT (t, i));
11489
11490 /* Now walk the last one as a tail call. */
11491 WALK_SUBTREE_TAIL (TREE_VEC_ELT (t, len - 1));
11492 }
11493
11494 case VECTOR_CST:
11495 {
11496 unsigned len = vector_cst_encoded_nelts (t);
11497 if (len == 0)
11498 break;
11499 /* Walk all elements but the last. */
11500 for (unsigned i = 0; i < len - 1; ++i)
11501 WALK_SUBTREE (VECTOR_CST_ENCODED_ELT (t, i));
11502 /* Now walk the last one as a tail call. */
11503 WALK_SUBTREE_TAIL (VECTOR_CST_ENCODED_ELT (t, len - 1));
11504 }
11505
11506 case COMPLEX_CST:
11507 WALK_SUBTREE (TREE_REALPART (t));
11508 WALK_SUBTREE_TAIL (TREE_IMAGPART (t));
11509
11510 case CONSTRUCTOR:
11511 {
11512 unsigned HOST_WIDE_INT idx;
11513 constructor_elt *ce;
11514
11515 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), ix: idx, ptr: &ce);
11516 idx++)
11517 WALK_SUBTREE (ce->value);
11518 }
11519 break;
11520
11521 case SAVE_EXPR:
11522 WALK_SUBTREE_TAIL (TREE_OPERAND (t, 0));
11523
11524 case BIND_EXPR:
11525 {
11526 tree decl;
11527 for (decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
11528 {
11529 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11530 into declarations that are just mentioned, rather than
11531 declared; they don't really belong to this part of the tree.
11532 And, we can see cycles: the initializer for a declaration
11533 can refer to the declaration itself. */
11534 WALK_SUBTREE (DECL_INITIAL (decl));
11535 WALK_SUBTREE (DECL_SIZE (decl));
11536 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11537 }
11538 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (t));
11539 }
11540
11541 case STATEMENT_LIST:
11542 {
11543 tree_stmt_iterator i;
11544 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (i: &i))
11545 WALK_SUBTREE (*tsi_stmt_ptr (i));
11546 }
11547 break;
11548
11549 case OMP_CLAUSE:
11550 {
11551 int len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
11552 for (int i = 0; i < len; i++)
11553 WALK_SUBTREE (OMP_CLAUSE_OPERAND (t, i));
11554 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (t));
11555 }
11556
11557 case TARGET_EXPR:
11558 {
11559 int i, len;
11560
11561 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11562 But, we only want to walk once. */
11563 len = (TREE_OPERAND (t, 3) == TREE_OPERAND (t, 1)) ? 2 : 3;
11564 for (i = 0; i < len; ++i)
11565 WALK_SUBTREE (TREE_OPERAND (t, i));
11566 WALK_SUBTREE_TAIL (TREE_OPERAND (t, len));
11567 }
11568
11569 case DECL_EXPR:
11570 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11571 defining. We only want to walk into these fields of a type in this
11572 case and not in the general case of a mere reference to the type.
11573
11574 The criterion is as follows: if the field can be an expression, it
11575 must be walked only here. This should be in keeping with the fields
11576 that are directly gimplified in gimplify_type_sizes in order for the
11577 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11578 variable-sized types.
11579
11580 Note that DECLs get walked as part of processing the BIND_EXPR. */
11581 if (TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
11582 {
11583 /* Call the function for the decl so e.g. copy_tree_body_r can
11584 replace it with the remapped one. */
11585 result = (*func) (&DECL_EXPR_DECL (t), &walk_subtrees, data);
11586 if (result || !walk_subtrees)
11587 return result;
11588
11589 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (t));
11590 if (TREE_CODE (*type_p) == ERROR_MARK)
11591 return NULL_TREE;
11592
11593 /* Call the function for the type. See if it returns anything or
11594 doesn't want us to continue. If we are to continue, walk both
11595 the normal fields and those for the declaration case. */
11596 result = (*func) (type_p, &walk_subtrees, data);
11597 if (result || !walk_subtrees)
11598 return result;
11599
11600 tree type = *type_p;
11601
11602 /* But do not walk a pointed-to type since it may itself need to
11603 be walked in the declaration case if it isn't anonymous. */
11604 if (!POINTER_TYPE_P (type))
11605 {
11606 result = walk_type_fields (type, func, data, pset, lh);
11607 if (result)
11608 return result;
11609 }
11610
11611 /* If this is a record type, also walk the fields. */
11612 if (RECORD_OR_UNION_TYPE_P (type))
11613 {
11614 tree field;
11615
11616 for (field = TYPE_FIELDS (type); field;
11617 field = DECL_CHAIN (field))
11618 {
11619 /* We'd like to look at the type of the field, but we can
11620 easily get infinite recursion. So assume it's pointed
11621 to elsewhere in the tree. Also, ignore things that
11622 aren't fields. */
11623 if (TREE_CODE (field) != FIELD_DECL)
11624 continue;
11625
11626 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11627 WALK_SUBTREE (DECL_SIZE (field));
11628 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11629 if (TREE_CODE (type) == QUAL_UNION_TYPE)
11630 WALK_SUBTREE (DECL_QUALIFIER (field));
11631 }
11632 }
11633
11634 /* Same for scalar types. */
11635 else if (TREE_CODE (type) == BOOLEAN_TYPE
11636 || TREE_CODE (type) == ENUMERAL_TYPE
11637 || TREE_CODE (type) == INTEGER_TYPE
11638 || TREE_CODE (type) == FIXED_POINT_TYPE
11639 || TREE_CODE (type) == REAL_TYPE)
11640 {
11641 WALK_SUBTREE (TYPE_MIN_VALUE (type));
11642 WALK_SUBTREE (TYPE_MAX_VALUE (type));
11643 }
11644
11645 WALK_SUBTREE (TYPE_SIZE (type));
11646 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (type));
11647 }
11648 /* FALLTHRU */
11649
11650 default:
11651 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11652 {
11653 int i, len;
11654
11655 /* Walk over all the sub-trees of this operand. */
11656 len = TREE_OPERAND_LENGTH (t);
11657
11658 /* Go through the subtrees. We need to do this in forward order so
11659 that the scope of a FOR_EXPR is handled properly. */
11660 if (len)
11661 {
11662 for (i = 0; i < len - 1; ++i)
11663 WALK_SUBTREE (TREE_OPERAND (t, i));
11664 WALK_SUBTREE_TAIL (TREE_OPERAND (t, len - 1));
11665 }
11666 }
11667 /* If this is a type, walk the needed fields in the type. */
11668 else if (TYPE_P (t))
11669 return walk_type_fields (type: t, func, data, pset, lh);
11670 break;
11671 }
11672
11673 /* We didn't find what we were looking for. */
11674 return NULL_TREE;
11675
11676#undef WALK_SUBTREE_TAIL
11677}
11678#undef WALK_SUBTREE
11679
11680/* Like walk_tree, but does not walk duplicate nodes more than once. */
11681
11682tree
11683walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11684 walk_tree_lh lh)
11685{
11686 tree result;
11687
11688 hash_set<tree> pset;
11689 result = walk_tree_1 (tp, func, data, pset: &pset, lh);
11690 return result;
11691}
11692
11693
11694tree
11695tree_block (tree t)
11696{
11697 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11698
11699 if (IS_EXPR_CODE_CLASS (c))
11700 return LOCATION_BLOCK (t->exp.locus);
11701 gcc_unreachable ();
11702 return NULL;
11703}
11704
11705void
11706tree_set_block (tree t, tree b)
11707{
11708 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11709
11710 if (IS_EXPR_CODE_CLASS (c))
11711 {
11712 t->exp.locus = set_block (loc: t->exp.locus, block: b);
11713 }
11714 else
11715 gcc_unreachable ();
11716}
11717
11718/* Create a nameless artificial label and put it in the current
11719 function context. The label has a location of LOC. Returns the
11720 newly created label. */
11721
11722tree
11723create_artificial_label (location_t loc)
11724{
11725 tree lab = build_decl (loc,
11726 code: LABEL_DECL, NULL_TREE, void_type_node);
11727
11728 DECL_ARTIFICIAL (lab) = 1;
11729 DECL_IGNORED_P (lab) = 1;
11730 DECL_CONTEXT (lab) = current_function_decl;
11731 return lab;
11732}
11733
11734/* Given a tree, try to return a useful variable name that we can use
11735 to prefix a temporary that is being assigned the value of the tree.
11736 I.E. given <temp> = &A, return A. */
11737
11738const char *
11739get_name (tree t)
11740{
11741 tree stripped_decl;
11742
11743 stripped_decl = t;
11744 STRIP_NOPS (stripped_decl);
11745 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11746 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11747 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11748 {
11749 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11750 if (!name)
11751 return NULL;
11752 return IDENTIFIER_POINTER (name);
11753 }
11754 else
11755 {
11756 switch (TREE_CODE (stripped_decl))
11757 {
11758 case ADDR_EXPR:
11759 return get_name (TREE_OPERAND (stripped_decl, 0));
11760 default:
11761 return NULL;
11762 }
11763 }
11764}
11765
11766/* Return true if TYPE has a variable argument list. */
11767
11768bool
11769stdarg_p (const_tree fntype)
11770{
11771 function_args_iterator args_iter;
11772 tree n = NULL_TREE, t;
11773
11774 if (!fntype)
11775 return false;
11776
11777 if (TYPE_NO_NAMED_ARGS_STDARG_P (fntype))
11778 return true;
11779
11780 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
11781 {
11782 n = t;
11783 }
11784
11785 return n != NULL_TREE && n != void_type_node;
11786}
11787
11788/* Return true if TYPE has a prototype. */
11789
11790bool
11791prototype_p (const_tree fntype)
11792{
11793 tree t;
11794
11795 gcc_assert (fntype != NULL_TREE);
11796
11797 if (TYPE_NO_NAMED_ARGS_STDARG_P (fntype))
11798 return true;
11799
11800 t = TYPE_ARG_TYPES (fntype);
11801 return (t != NULL_TREE);
11802}
11803
11804/* If BLOCK is inlined from an __attribute__((__artificial__))
11805 routine, return pointer to location from where it has been
11806 called. */
11807location_t *
11808block_nonartificial_location (tree block)
11809{
11810 location_t *ret = NULL;
11811
11812 while (block && TREE_CODE (block) == BLOCK
11813 && BLOCK_ABSTRACT_ORIGIN (block))
11814 {
11815 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11816 if (TREE_CODE (ao) == FUNCTION_DECL)
11817 {
11818 /* If AO is an artificial inline, point RET to the
11819 call site locus at which it has been inlined and continue
11820 the loop, in case AO's caller is also an artificial
11821 inline. */
11822 if (DECL_DECLARED_INLINE_P (ao)
11823 && lookup_attribute (attr_name: "artificial", DECL_ATTRIBUTES (ao)))
11824 ret = &BLOCK_SOURCE_LOCATION (block);
11825 else
11826 break;
11827 }
11828 else if (TREE_CODE (ao) != BLOCK)
11829 break;
11830
11831 block = BLOCK_SUPERCONTEXT (block);
11832 }
11833 return ret;
11834}
11835
11836
11837/* If EXP is inlined from an __attribute__((__artificial__))
11838 function, return the location of the original call expression. */
11839
11840location_t
11841tree_nonartificial_location (tree exp)
11842{
11843 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11844
11845 if (loc)
11846 return *loc;
11847 else
11848 return EXPR_LOCATION (exp);
11849}
11850
11851/* Return the location into which EXP has been inlined. Analogous
11852 to tree_nonartificial_location() above but not limited to artificial
11853 functions declared inline. If SYSTEM_HEADER is true, return
11854 the macro expansion point of the location if it's in a system header */
11855
11856location_t
11857tree_inlined_location (tree exp, bool system_header /* = true */)
11858{
11859 location_t loc = UNKNOWN_LOCATION;
11860
11861 tree block = TREE_BLOCK (exp);
11862
11863 while (block && TREE_CODE (block) == BLOCK
11864 && BLOCK_ABSTRACT_ORIGIN (block))
11865 {
11866 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11867 if (TREE_CODE (ao) == FUNCTION_DECL)
11868 loc = BLOCK_SOURCE_LOCATION (block);
11869 else if (TREE_CODE (ao) != BLOCK)
11870 break;
11871
11872 block = BLOCK_SUPERCONTEXT (block);
11873 }
11874
11875 if (loc == UNKNOWN_LOCATION)
11876 {
11877 loc = EXPR_LOCATION (exp);
11878 if (system_header)
11879 /* Only consider macro expansion when the block traversal failed
11880 to find a location. Otherwise it's not relevant. */
11881 return expansion_point_location_if_in_system_header (loc);
11882 }
11883
11884 return loc;
11885}
11886
11887/* These are the hash table functions for the hash table of OPTIMIZATION_NODE
11888 nodes. */
11889
11890/* Return the hash code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11891
11892hashval_t
11893cl_option_hasher::hash (tree x)
11894{
11895 const_tree const t = x;
11896
11897 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11898 return cl_optimization_hash (TREE_OPTIMIZATION (t));
11899 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11900 return cl_target_option_hash (TREE_TARGET_OPTION (t));
11901 else
11902 gcc_unreachable ();
11903}
11904
11905/* Return nonzero if the value represented by *X (an OPTIMIZATION or
11906 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11907 same. */
11908
11909bool
11910cl_option_hasher::equal (tree x, tree y)
11911{
11912 const_tree const xt = x;
11913 const_tree const yt = y;
11914
11915 if (TREE_CODE (xt) != TREE_CODE (yt))
11916 return false;
11917
11918 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11919 return cl_optimization_option_eq (TREE_OPTIMIZATION (xt),
11920 TREE_OPTIMIZATION (yt));
11921 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11922 return cl_target_option_eq (TREE_TARGET_OPTION (xt),
11923 TREE_TARGET_OPTION (yt));
11924 else
11925 gcc_unreachable ();
11926}
11927
11928/* Build an OPTIMIZATION_NODE based on the options in OPTS and OPTS_SET. */
11929
11930tree
11931build_optimization_node (struct gcc_options *opts,
11932 struct gcc_options *opts_set)
11933{
11934 tree t;
11935
11936 /* Use the cache of optimization nodes. */
11937
11938 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11939 opts, opts_set);
11940
11941 tree *slot = cl_option_hash_table->find_slot (value: cl_optimization_node, insert: INSERT);
11942 t = *slot;
11943 if (!t)
11944 {
11945 /* Insert this one into the hash table. */
11946 t = cl_optimization_node;
11947 *slot = t;
11948
11949 /* Make a new node for next time round. */
11950 cl_optimization_node = make_node (code: OPTIMIZATION_NODE);
11951 }
11952
11953 return t;
11954}
11955
11956/* Build a TARGET_OPTION_NODE based on the options in OPTS and OPTS_SET. */
11957
11958tree
11959build_target_option_node (struct gcc_options *opts,
11960 struct gcc_options *opts_set)
11961{
11962 tree t;
11963
11964 /* Use the cache of optimization nodes. */
11965
11966 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11967 opts, opts_set);
11968
11969 tree *slot = cl_option_hash_table->find_slot (value: cl_target_option_node, insert: INSERT);
11970 t = *slot;
11971 if (!t)
11972 {
11973 /* Insert this one into the hash table. */
11974 t = cl_target_option_node;
11975 *slot = t;
11976
11977 /* Make a new node for next time round. */
11978 cl_target_option_node = make_node (code: TARGET_OPTION_NODE);
11979 }
11980
11981 return t;
11982}
11983
11984/* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
11985 so that they aren't saved during PCH writing. */
11986
11987void
11988prepare_target_option_nodes_for_pch (void)
11989{
11990 hash_table<cl_option_hasher>::iterator iter = cl_option_hash_table->begin ();
11991 for (; iter != cl_option_hash_table->end (); ++iter)
11992 if (TREE_CODE (*iter) == TARGET_OPTION_NODE)
11993 TREE_TARGET_GLOBALS (*iter) = NULL;
11994}
11995
11996/* Determine the "ultimate origin" of a block. */
11997
11998tree
11999block_ultimate_origin (const_tree block)
12000{
12001 tree origin = BLOCK_ABSTRACT_ORIGIN (block);
12002
12003 if (origin == NULL_TREE)
12004 return NULL_TREE;
12005 else
12006 {
12007 gcc_checking_assert ((DECL_P (origin)
12008 && DECL_ORIGIN (origin) == origin)
12009 || BLOCK_ORIGIN (origin) == origin);
12010 return origin;
12011 }
12012}
12013
12014/* Return true iff conversion from INNER_TYPE to OUTER_TYPE generates
12015 no instruction. */
12016
12017bool
12018tree_nop_conversion_p (const_tree outer_type, const_tree inner_type)
12019{
12020 /* Do not strip casts into or out of differing address spaces. */
12021 if (POINTER_TYPE_P (outer_type)
12022 && TYPE_ADDR_SPACE (TREE_TYPE (outer_type)) != ADDR_SPACE_GENERIC)
12023 {
12024 if (!POINTER_TYPE_P (inner_type)
12025 || (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
12026 != TYPE_ADDR_SPACE (TREE_TYPE (inner_type))))
12027 return false;
12028 }
12029 else if (POINTER_TYPE_P (inner_type)
12030 && TYPE_ADDR_SPACE (TREE_TYPE (inner_type)) != ADDR_SPACE_GENERIC)
12031 {
12032 /* We already know that outer_type is not a pointer with
12033 a non-generic address space. */
12034 return false;
12035 }
12036
12037 /* Use precision rather then machine mode when we can, which gives
12038 the correct answer even for submode (bit-field) types. */
12039 if ((INTEGRAL_TYPE_P (outer_type)
12040 || POINTER_TYPE_P (outer_type)
12041 || TREE_CODE (outer_type) == OFFSET_TYPE)
12042 && (INTEGRAL_TYPE_P (inner_type)
12043 || POINTER_TYPE_P (inner_type)
12044 || TREE_CODE (inner_type) == OFFSET_TYPE))
12045 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
12046
12047 /* Otherwise fall back on comparing machine modes (e.g. for
12048 aggregate types, floats). */
12049 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
12050}
12051
12052/* Return true iff conversion in EXP generates no instruction. Mark
12053 it inline so that we fully inline into the stripping functions even
12054 though we have two uses of this function. */
12055
12056static inline bool
12057tree_nop_conversion (const_tree exp)
12058{
12059 tree outer_type, inner_type;
12060
12061 if (location_wrapper_p (exp))
12062 return true;
12063 if (!CONVERT_EXPR_P (exp)
12064 && TREE_CODE (exp) != NON_LVALUE_EXPR)
12065 return false;
12066
12067 outer_type = TREE_TYPE (exp);
12068 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
12069 if (!inner_type || inner_type == error_mark_node)
12070 return false;
12071
12072 return tree_nop_conversion_p (outer_type, inner_type);
12073}
12074
12075/* Return true iff conversion in EXP generates no instruction. Don't
12076 consider conversions changing the signedness. */
12077
12078static bool
12079tree_sign_nop_conversion (const_tree exp)
12080{
12081 tree outer_type, inner_type;
12082
12083 if (!tree_nop_conversion (exp))
12084 return false;
12085
12086 outer_type = TREE_TYPE (exp);
12087 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
12088
12089 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
12090 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
12091}
12092
12093/* Strip conversions from EXP according to tree_nop_conversion and
12094 return the resulting expression. */
12095
12096tree
12097tree_strip_nop_conversions (tree exp)
12098{
12099 while (tree_nop_conversion (exp))
12100 exp = TREE_OPERAND (exp, 0);
12101 return exp;
12102}
12103
12104/* Strip conversions from EXP according to tree_sign_nop_conversion
12105 and return the resulting expression. */
12106
12107tree
12108tree_strip_sign_nop_conversions (tree exp)
12109{
12110 while (tree_sign_nop_conversion (exp))
12111 exp = TREE_OPERAND (exp, 0);
12112 return exp;
12113}
12114
12115/* Avoid any floating point extensions from EXP. */
12116tree
12117strip_float_extensions (tree exp)
12118{
12119 tree sub, expt, subt;
12120
12121 /* For floating point constant look up the narrowest type that can hold
12122 it properly and handle it like (type)(narrowest_type)constant.
12123 This way we can optimize for instance a=a*2.0 where "a" is float
12124 but 2.0 is double constant. */
12125 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
12126 {
12127 REAL_VALUE_TYPE orig;
12128 tree type = NULL;
12129
12130 orig = TREE_REAL_CST (exp);
12131 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
12132 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
12133 type = float_type_node;
12134 else if (TYPE_PRECISION (TREE_TYPE (exp))
12135 > TYPE_PRECISION (double_type_node)
12136 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
12137 type = double_type_node;
12138 if (type)
12139 return build_real_truncate (type, d: orig);
12140 }
12141
12142 if (!CONVERT_EXPR_P (exp))
12143 return exp;
12144
12145 sub = TREE_OPERAND (exp, 0);
12146 subt = TREE_TYPE (sub);
12147 expt = TREE_TYPE (exp);
12148
12149 if (!FLOAT_TYPE_P (subt))
12150 return exp;
12151
12152 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
12153 return exp;
12154
12155 if (element_precision (type: subt) > element_precision (type: expt))
12156 return exp;
12157
12158 return strip_float_extensions (exp: sub);
12159}
12160
12161/* Strip out all handled components that produce invariant
12162 offsets. */
12163
12164const_tree
12165strip_invariant_refs (const_tree op)
12166{
12167 while (handled_component_p (t: op))
12168 {
12169 switch (TREE_CODE (op))
12170 {
12171 case ARRAY_REF:
12172 case ARRAY_RANGE_REF:
12173 if (!is_gimple_constant (TREE_OPERAND (op, 1))
12174 || TREE_OPERAND (op, 2) != NULL_TREE
12175 || TREE_OPERAND (op, 3) != NULL_TREE)
12176 return NULL;
12177 break;
12178
12179 case COMPONENT_REF:
12180 if (TREE_OPERAND (op, 2) != NULL_TREE)
12181 return NULL;
12182 break;
12183
12184 default:;
12185 }
12186 op = TREE_OPERAND (op, 0);
12187 }
12188
12189 return op;
12190}
12191
12192/* Strip handled components with zero offset from OP. */
12193
12194tree
12195strip_zero_offset_components (tree op)
12196{
12197 while (TREE_CODE (op) == COMPONENT_REF
12198 && integer_zerop (DECL_FIELD_OFFSET (TREE_OPERAND (op, 1)))
12199 && integer_zerop (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (op, 1))))
12200 op = TREE_OPERAND (op, 0);
12201 return op;
12202}
12203
12204static GTY(()) tree gcc_eh_personality_decl;
12205
12206/* Return the GCC personality function decl. */
12207
12208tree
12209lhd_gcc_personality (void)
12210{
12211 if (!gcc_eh_personality_decl)
12212 gcc_eh_personality_decl = build_personality_function ("gcc");
12213 return gcc_eh_personality_decl;
12214}
12215
12216/* TARGET is a call target of GIMPLE call statement
12217 (obtained by gimple_call_fn). Return true if it is
12218 OBJ_TYPE_REF representing an virtual call of C++ method.
12219 (As opposed to OBJ_TYPE_REF representing objc calls
12220 through a cast where middle-end devirtualization machinery
12221 can't apply.) FOR_DUMP_P is true when being called from
12222 the dump routines. */
12223
12224bool
12225virtual_method_call_p (const_tree target, bool for_dump_p)
12226{
12227 if (TREE_CODE (target) != OBJ_TYPE_REF)
12228 return false;
12229 tree t = TREE_TYPE (target);
12230 gcc_checking_assert (TREE_CODE (t) == POINTER_TYPE);
12231 t = TREE_TYPE (t);
12232 if (TREE_CODE (t) == FUNCTION_TYPE)
12233 return false;
12234 gcc_checking_assert (TREE_CODE (t) == METHOD_TYPE);
12235 /* If we do not have BINFO associated, it means that type was built
12236 without devirtualization enabled. Do not consider this a virtual
12237 call. */
12238 if (!TYPE_BINFO (obj_type_ref_class (target, for_dump_p)))
12239 return false;
12240 return true;
12241}
12242
12243/* Lookup sub-BINFO of BINFO of TYPE at offset POS. */
12244
12245static tree
12246lookup_binfo_at_offset (tree binfo, tree type, HOST_WIDE_INT pos)
12247{
12248 unsigned int i;
12249 tree base_binfo, b;
12250
12251 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12252 if (pos == tree_to_shwi (BINFO_OFFSET (base_binfo))
12253 && types_same_for_odr (TREE_TYPE (base_binfo), type2: type))
12254 return base_binfo;
12255 else if ((b = lookup_binfo_at_offset (binfo: base_binfo, type, pos)) != NULL)
12256 return b;
12257 return NULL;
12258}
12259
12260/* Try to find a base info of BINFO that would have its field decl at offset
12261 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
12262 found, return, otherwise return NULL_TREE. */
12263
12264tree
12265get_binfo_at_offset (tree binfo, poly_int64 offset, tree expected_type)
12266{
12267 tree type = BINFO_TYPE (binfo);
12268
12269 while (true)
12270 {
12271 HOST_WIDE_INT pos, size;
12272 tree fld;
12273 int i;
12274
12275 if (types_same_for_odr (type1: type, type2: expected_type))
12276 return binfo;
12277 if (maybe_lt (a: offset, b: 0))
12278 return NULL_TREE;
12279
12280 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
12281 {
12282 if (TREE_CODE (fld) != FIELD_DECL || !DECL_ARTIFICIAL (fld))
12283 continue;
12284
12285 pos = int_bit_position (field: fld);
12286 size = tree_to_uhwi (DECL_SIZE (fld));
12287 if (known_in_range_p (val: offset, pos, size))
12288 break;
12289 }
12290 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
12291 return NULL_TREE;
12292
12293 /* Offset 0 indicates the primary base, whose vtable contents are
12294 represented in the binfo for the derived class. */
12295 else if (maybe_ne (a: offset, b: 0))
12296 {
12297 tree found_binfo = NULL, base_binfo;
12298 /* Offsets in BINFO are in bytes relative to the whole structure
12299 while POS is in bits relative to the containing field. */
12300 int binfo_offset = (tree_to_shwi (BINFO_OFFSET (binfo)) + pos
12301 / BITS_PER_UNIT);
12302
12303 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12304 if (tree_to_shwi (BINFO_OFFSET (base_binfo)) == binfo_offset
12305 && types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
12306 {
12307 found_binfo = base_binfo;
12308 break;
12309 }
12310 if (found_binfo)
12311 binfo = found_binfo;
12312 else
12313 binfo = lookup_binfo_at_offset (binfo, TREE_TYPE (fld),
12314 pos: binfo_offset);
12315 }
12316
12317 type = TREE_TYPE (fld);
12318 offset -= pos;
12319 }
12320}
12321
12322/* PR 84195: Replace control characters in "unescaped" with their
12323 escaped equivalents. Allow newlines if -fmessage-length has
12324 been set to a non-zero value. This is done here, rather than
12325 where the attribute is recorded as the message length can
12326 change between these two locations. */
12327
12328void
12329escaped_string::escape (const char *unescaped)
12330{
12331 char *escaped;
12332 size_t i, new_i, len;
12333
12334 if (m_owned)
12335 free (ptr: m_str);
12336
12337 m_str = const_cast<char *> (unescaped);
12338 m_owned = false;
12339
12340 if (unescaped == NULL || *unescaped == 0)
12341 return;
12342
12343 len = strlen (s: unescaped);
12344 escaped = NULL;
12345 new_i = 0;
12346
12347 for (i = 0; i < len; i++)
12348 {
12349 char c = unescaped[i];
12350
12351 if (!ISCNTRL (c))
12352 {
12353 if (escaped)
12354 escaped[new_i++] = c;
12355 continue;
12356 }
12357
12358 if (c != '\n' || !pp_is_wrapping_line (global_dc->printer))
12359 {
12360 if (escaped == NULL)
12361 {
12362 /* We only allocate space for a new string if we
12363 actually encounter a control character that
12364 needs replacing. */
12365 escaped = (char *) xmalloc (len * 2 + 1);
12366 strncpy (dest: escaped, src: unescaped, n: i);
12367 new_i = i;
12368 }
12369
12370 escaped[new_i++] = '\\';
12371
12372 switch (c)
12373 {
12374 case '\a': escaped[new_i++] = 'a'; break;
12375 case '\b': escaped[new_i++] = 'b'; break;
12376 case '\f': escaped[new_i++] = 'f'; break;
12377 case '\n': escaped[new_i++] = 'n'; break;
12378 case '\r': escaped[new_i++] = 'r'; break;
12379 case '\t': escaped[new_i++] = 't'; break;
12380 case '\v': escaped[new_i++] = 'v'; break;
12381 default: escaped[new_i++] = '?'; break;
12382 }
12383 }
12384 else if (escaped)
12385 escaped[new_i++] = c;
12386 }
12387
12388 if (escaped)
12389 {
12390 escaped[new_i] = 0;
12391 m_str = escaped;
12392 m_owned = true;
12393 }
12394}
12395
12396/* Warn about a use of an identifier which was marked deprecated. Returns
12397 whether a warning was given. */
12398
12399bool
12400warn_deprecated_use (tree node, tree attr)
12401{
12402 escaped_string msg;
12403
12404 if (node == 0 || !warn_deprecated_decl)
12405 return false;
12406
12407 if (!attr)
12408 {
12409 if (DECL_P (node))
12410 attr = DECL_ATTRIBUTES (node);
12411 else if (TYPE_P (node))
12412 {
12413 tree decl = TYPE_STUB_DECL (node);
12414 if (decl)
12415 attr = TYPE_ATTRIBUTES (TREE_TYPE (decl));
12416 else if ((decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (node)))
12417 != NULL_TREE)
12418 {
12419 node = TREE_TYPE (decl);
12420 attr = TYPE_ATTRIBUTES (node);
12421 }
12422 }
12423 }
12424
12425 if (attr)
12426 attr = lookup_attribute (attr_name: "deprecated", list: attr);
12427
12428 if (attr)
12429 msg.escape (TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
12430
12431 bool w = false;
12432 if (DECL_P (node))
12433 {
12434 auto_diagnostic_group d;
12435 if (msg)
12436 w = warning (OPT_Wdeprecated_declarations,
12437 "%qD is deprecated: %s", node, (const char *) msg);
12438 else
12439 w = warning (OPT_Wdeprecated_declarations,
12440 "%qD is deprecated", node);
12441 if (w)
12442 inform (DECL_SOURCE_LOCATION (node), "declared here");
12443 }
12444 else if (TYPE_P (node))
12445 {
12446 tree what = NULL_TREE;
12447 tree decl = TYPE_STUB_DECL (node);
12448
12449 if (TYPE_NAME (node))
12450 {
12451 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12452 what = TYPE_NAME (node);
12453 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12454 && DECL_NAME (TYPE_NAME (node)))
12455 what = DECL_NAME (TYPE_NAME (node));
12456 }
12457
12458 auto_diagnostic_group d;
12459 if (what)
12460 {
12461 if (msg)
12462 w = warning (OPT_Wdeprecated_declarations,
12463 "%qE is deprecated: %s", what, (const char *) msg);
12464 else
12465 w = warning (OPT_Wdeprecated_declarations,
12466 "%qE is deprecated", what);
12467 }
12468 else
12469 {
12470 if (msg)
12471 w = warning (OPT_Wdeprecated_declarations,
12472 "type is deprecated: %s", (const char *) msg);
12473 else
12474 w = warning (OPT_Wdeprecated_declarations,
12475 "type is deprecated");
12476 }
12477
12478 if (w && decl)
12479 inform (DECL_SOURCE_LOCATION (decl), "declared here");
12480 }
12481
12482 return w;
12483}
12484
12485/* Error out with an identifier which was marked 'unavailable'. */
12486void
12487error_unavailable_use (tree node, tree attr)
12488{
12489 escaped_string msg;
12490
12491 if (node == 0)
12492 return;
12493
12494 if (!attr)
12495 {
12496 if (DECL_P (node))
12497 attr = DECL_ATTRIBUTES (node);
12498 else if (TYPE_P (node))
12499 {
12500 tree decl = TYPE_STUB_DECL (node);
12501 if (decl)
12502 attr = lookup_attribute (attr_name: "unavailable",
12503 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
12504 }
12505 }
12506
12507 if (attr)
12508 attr = lookup_attribute (attr_name: "unavailable", list: attr);
12509
12510 if (attr)
12511 msg.escape (TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
12512
12513 if (DECL_P (node))
12514 {
12515 auto_diagnostic_group d;
12516 if (msg)
12517 error ("%qD is unavailable: %s", node, (const char *) msg);
12518 else
12519 error ("%qD is unavailable", node);
12520 inform (DECL_SOURCE_LOCATION (node), "declared here");
12521 }
12522 else if (TYPE_P (node))
12523 {
12524 tree what = NULL_TREE;
12525 tree decl = TYPE_STUB_DECL (node);
12526
12527 if (TYPE_NAME (node))
12528 {
12529 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12530 what = TYPE_NAME (node);
12531 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12532 && DECL_NAME (TYPE_NAME (node)))
12533 what = DECL_NAME (TYPE_NAME (node));
12534 }
12535
12536 auto_diagnostic_group d;
12537 if (what)
12538 {
12539 if (msg)
12540 error ("%qE is unavailable: %s", what, (const char *) msg);
12541 else
12542 error ("%qE is unavailable", what);
12543 }
12544 else
12545 {
12546 if (msg)
12547 error ("type is unavailable: %s", (const char *) msg);
12548 else
12549 error ("type is unavailable");
12550 }
12551
12552 if (decl)
12553 inform (DECL_SOURCE_LOCATION (decl), "declared here");
12554 }
12555}
12556
12557/* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12558 somewhere in it. */
12559
12560bool
12561contains_bitfld_component_ref_p (const_tree ref)
12562{
12563 while (handled_component_p (t: ref))
12564 {
12565 if (TREE_CODE (ref) == COMPONENT_REF
12566 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12567 return true;
12568 ref = TREE_OPERAND (ref, 0);
12569 }
12570
12571 return false;
12572}
12573
12574/* Try to determine whether a TRY_CATCH expression can fall through.
12575 This is a subroutine of block_may_fallthru. */
12576
12577static bool
12578try_catch_may_fallthru (const_tree stmt)
12579{
12580 tree_stmt_iterator i;
12581
12582 /* If the TRY block can fall through, the whole TRY_CATCH can
12583 fall through. */
12584 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
12585 return true;
12586
12587 switch (TREE_CODE (TREE_OPERAND (stmt, 1)))
12588 {
12589 case CATCH_EXPR:
12590 /* See below. */
12591 return block_may_fallthru (CATCH_BODY (TREE_OPERAND (stmt, 1)));
12592
12593 case EH_FILTER_EXPR:
12594 /* See below. */
12595 return block_may_fallthru (EH_FILTER_FAILURE (TREE_OPERAND (stmt, 1)));
12596
12597 case STATEMENT_LIST:
12598 break;
12599
12600 default:
12601 /* See below. */
12602 return false;
12603 }
12604
12605 i = tsi_start (TREE_OPERAND (stmt, 1));
12606 switch (TREE_CODE (tsi_stmt (i)))
12607 {
12608 case CATCH_EXPR:
12609 /* We expect to see a sequence of CATCH_EXPR trees, each with a
12610 catch expression and a body. The whole TRY_CATCH may fall
12611 through iff any of the catch bodies falls through. */
12612 for (; !tsi_end_p (i); tsi_next (i: &i))
12613 {
12614 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
12615 return true;
12616 }
12617 return false;
12618
12619 case EH_FILTER_EXPR:
12620 /* The exception filter expression only matters if there is an
12621 exception. If the exception does not match EH_FILTER_TYPES,
12622 we will execute EH_FILTER_FAILURE, and we will fall through
12623 if that falls through. If the exception does match
12624 EH_FILTER_TYPES, the stack unwinder will continue up the
12625 stack, so we will not fall through. We don't know whether we
12626 will throw an exception which matches EH_FILTER_TYPES or not,
12627 so we just ignore EH_FILTER_TYPES and assume that we might
12628 throw an exception which doesn't match. */
12629 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
12630
12631 default:
12632 /* This case represents statements to be executed when an
12633 exception occurs. Those statements are implicitly followed
12634 by a RESX statement to resume execution after the exception.
12635 So in this case the TRY_CATCH never falls through. */
12636 return false;
12637 }
12638}
12639
12640/* Try to determine if we can fall out of the bottom of BLOCK. This guess
12641 need not be 100% accurate; simply be conservative and return true if we
12642 don't know. This is used only to avoid stupidly generating extra code.
12643 If we're wrong, we'll just delete the extra code later. */
12644
12645bool
12646block_may_fallthru (const_tree block)
12647{
12648 /* This CONST_CAST is okay because expr_last returns its argument
12649 unmodified and we assign it to a const_tree. */
12650 const_tree stmt = expr_last (CONST_CAST_TREE (block));
12651
12652 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
12653 {
12654 case GOTO_EXPR:
12655 case RETURN_EXPR:
12656 /* Easy cases. If the last statement of the block implies
12657 control transfer, then we can't fall through. */
12658 return false;
12659
12660 case SWITCH_EXPR:
12661 /* If there is a default: label or case labels cover all possible
12662 SWITCH_COND values, then the SWITCH_EXPR will transfer control
12663 to some case label in all cases and all we care is whether the
12664 SWITCH_BODY falls through. */
12665 if (SWITCH_ALL_CASES_P (stmt))
12666 return block_may_fallthru (SWITCH_BODY (stmt));
12667 return true;
12668
12669 case COND_EXPR:
12670 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
12671 return true;
12672 return block_may_fallthru (COND_EXPR_ELSE (stmt));
12673
12674 case BIND_EXPR:
12675 return block_may_fallthru (BIND_EXPR_BODY (stmt));
12676
12677 case TRY_CATCH_EXPR:
12678 return try_catch_may_fallthru (stmt);
12679
12680 case TRY_FINALLY_EXPR:
12681 /* The finally clause is always executed after the try clause,
12682 so if it does not fall through, then the try-finally will not
12683 fall through. Otherwise, if the try clause does not fall
12684 through, then when the finally clause falls through it will
12685 resume execution wherever the try clause was going. So the
12686 whole try-finally will only fall through if both the try
12687 clause and the finally clause fall through. */
12688 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
12689 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
12690
12691 case EH_ELSE_EXPR:
12692 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12693
12694 case MODIFY_EXPR:
12695 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
12696 stmt = TREE_OPERAND (stmt, 1);
12697 else
12698 return true;
12699 /* FALLTHRU */
12700
12701 case CALL_EXPR:
12702 /* Functions that do not return do not fall through. */
12703 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
12704
12705 case CLEANUP_POINT_EXPR:
12706 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12707
12708 case TARGET_EXPR:
12709 return block_may_fallthru (TREE_OPERAND (stmt, 1));
12710
12711 case ERROR_MARK:
12712 return true;
12713
12714 default:
12715 return lang_hooks.block_may_fallthru (stmt);
12716 }
12717}
12718
12719/* True if we are using EH to handle cleanups. */
12720static bool using_eh_for_cleanups_flag = false;
12721
12722/* This routine is called from front ends to indicate eh should be used for
12723 cleanups. */
12724void
12725using_eh_for_cleanups (void)
12726{
12727 using_eh_for_cleanups_flag = true;
12728}
12729
12730/* Query whether EH is used for cleanups. */
12731bool
12732using_eh_for_cleanups_p (void)
12733{
12734 return using_eh_for_cleanups_flag;
12735}
12736
12737/* Wrapper for tree_code_name to ensure that tree code is valid */
12738const char *
12739get_tree_code_name (enum tree_code code)
12740{
12741 const char *invalid = "<invalid tree code>";
12742
12743 /* The tree_code enum promotes to signed, but we could be getting
12744 invalid values, so force an unsigned comparison. */
12745 if (unsigned (code) >= MAX_TREE_CODES)
12746 {
12747 if ((unsigned)code == 0xa5a5)
12748 return "ggc_freed";
12749 return invalid;
12750 }
12751
12752 return tree_code_name[code];
12753}
12754
12755/* Drops the TREE_OVERFLOW flag from T. */
12756
12757tree
12758drop_tree_overflow (tree t)
12759{
12760 gcc_checking_assert (TREE_OVERFLOW (t));
12761
12762 /* For tree codes with a sharing machinery re-build the result. */
12763 if (poly_int_tree_p (t))
12764 return wide_int_to_tree (TREE_TYPE (t), value: wi::to_poly_wide (t));
12765
12766 /* For VECTOR_CST, remove the overflow bits from the encoded elements
12767 and canonicalize the result. */
12768 if (TREE_CODE (t) == VECTOR_CST)
12769 {
12770 tree_vector_builder builder;
12771 builder.new_unary_operation (TREE_TYPE (t), vec: t, allow_stepped_p: true);
12772 unsigned int count = builder.encoded_nelts ();
12773 for (unsigned int i = 0; i < count; ++i)
12774 {
12775 tree elt = VECTOR_CST_ELT (t, i);
12776 if (TREE_OVERFLOW (elt))
12777 elt = drop_tree_overflow (t: elt);
12778 builder.quick_push (obj: elt);
12779 }
12780 return builder.build ();
12781 }
12782
12783 /* Otherwise, as all tcc_constants are possibly shared, copy the node
12784 and drop the flag. */
12785 t = copy_node (node: t);
12786 TREE_OVERFLOW (t) = 0;
12787
12788 /* For constants that contain nested constants, drop the flag
12789 from those as well. */
12790 if (TREE_CODE (t) == COMPLEX_CST)
12791 {
12792 if (TREE_OVERFLOW (TREE_REALPART (t)))
12793 TREE_REALPART (t) = drop_tree_overflow (TREE_REALPART (t));
12794 if (TREE_OVERFLOW (TREE_IMAGPART (t)))
12795 TREE_IMAGPART (t) = drop_tree_overflow (TREE_IMAGPART (t));
12796 }
12797
12798 return t;
12799}
12800
12801/* Given a memory reference expression T, return its base address.
12802 The base address of a memory reference expression is the main
12803 object being referenced. For instance, the base address for
12804 'array[i].fld[j]' is 'array'. You can think of this as stripping
12805 away the offset part from a memory address.
12806
12807 This function calls handled_component_p to strip away all the inner
12808 parts of the memory reference until it reaches the base object. */
12809
12810tree
12811get_base_address (tree t)
12812{
12813 if (TREE_CODE (t) == WITH_SIZE_EXPR)
12814 t = TREE_OPERAND (t, 0);
12815 while (handled_component_p (t))
12816 t = TREE_OPERAND (t, 0);
12817
12818 if ((TREE_CODE (t) == MEM_REF
12819 || TREE_CODE (t) == TARGET_MEM_REF)
12820 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
12821 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
12822
12823 return t;
12824}
12825
12826/* Return a tree of sizetype representing the size, in bytes, of the element
12827 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12828
12829tree
12830array_ref_element_size (tree exp)
12831{
12832 tree aligned_size = TREE_OPERAND (exp, 3);
12833 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
12834 location_t loc = EXPR_LOCATION (exp);
12835
12836 /* If a size was specified in the ARRAY_REF, it's the size measured
12837 in alignment units of the element type. So multiply by that value. */
12838 if (aligned_size)
12839 {
12840 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
12841 sizetype from another type of the same width and signedness. */
12842 if (TREE_TYPE (aligned_size) != sizetype)
12843 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
12844 return size_binop_loc (loc, MULT_EXPR, aligned_size,
12845 size_int (TYPE_ALIGN_UNIT (elmt_type)));
12846 }
12847
12848 /* Otherwise, take the size from that of the element type. Substitute
12849 any PLACEHOLDER_EXPR that we have. */
12850 else
12851 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
12852}
12853
12854/* Return a tree representing the lower bound of the array mentioned in
12855 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12856
12857tree
12858array_ref_low_bound (tree exp)
12859{
12860 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12861
12862 /* If a lower bound is specified in EXP, use it. */
12863 if (TREE_OPERAND (exp, 2))
12864 return TREE_OPERAND (exp, 2);
12865
12866 /* Otherwise, if there is a domain type and it has a lower bound, use it,
12867 substituting for a PLACEHOLDER_EXPR as needed. */
12868 if (domain_type && TYPE_MIN_VALUE (domain_type))
12869 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
12870
12871 /* Otherwise, return a zero of the appropriate type. */
12872 tree idxtype = TREE_TYPE (TREE_OPERAND (exp, 1));
12873 return (idxtype == error_mark_node
12874 ? integer_zero_node : build_int_cst (type: idxtype, cst: 0));
12875}
12876
12877/* Return a tree representing the upper bound of the array mentioned in
12878 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12879
12880tree
12881array_ref_up_bound (tree exp)
12882{
12883 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12884
12885 /* If there is a domain type and it has an upper bound, use it, substituting
12886 for a PLACEHOLDER_EXPR as needed. */
12887 if (domain_type && TYPE_MAX_VALUE (domain_type))
12888 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
12889
12890 /* Otherwise fail. */
12891 return NULL_TREE;
12892}
12893
12894/* Returns true if REF is an array reference, a component reference,
12895 or a memory reference to an array whose actual size might be larger
12896 than its upper bound implies, there are multiple cases:
12897 A. a ref to a flexible array member at the end of a structure;
12898 B. a ref to an array with a different type against the original decl;
12899 for example:
12900
12901 short a[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
12902 (*((char(*)[16])&a[0]))[i+8]
12903
12904 C. a ref to an array that was passed as a parameter;
12905 for example:
12906
12907 int test (uint8_t *p, uint32_t t[1][1], int n) {
12908 for (int i = 0; i < 4; i++, p++)
12909 t[i][0] = ...;
12910
12911 If non-null, set IS_TRAILING_ARRAY to true if the ref is the above case A.
12912*/
12913
12914bool
12915array_ref_flexible_size_p (tree ref, bool *is_trailing_array /* = NULL */)
12916{
12917 /* The TYPE for this array referece. */
12918 tree atype = NULL_TREE;
12919 /* The FIELD_DECL for the array field in the containing structure. */
12920 tree afield_decl = NULL_TREE;
12921 /* Whether this array is the trailing array of a structure. */
12922 bool is_trailing_array_tmp = false;
12923 if (!is_trailing_array)
12924 is_trailing_array = &is_trailing_array_tmp;
12925
12926 if (TREE_CODE (ref) == ARRAY_REF
12927 || TREE_CODE (ref) == ARRAY_RANGE_REF)
12928 {
12929 atype = TREE_TYPE (TREE_OPERAND (ref, 0));
12930 ref = TREE_OPERAND (ref, 0);
12931 }
12932 else if (TREE_CODE (ref) == COMPONENT_REF
12933 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 1))) == ARRAY_TYPE)
12934 {
12935 atype = TREE_TYPE (TREE_OPERAND (ref, 1));
12936 afield_decl = TREE_OPERAND (ref, 1);
12937 }
12938 else if (TREE_CODE (ref) == MEM_REF)
12939 {
12940 tree arg = TREE_OPERAND (ref, 0);
12941 if (TREE_CODE (arg) == ADDR_EXPR)
12942 arg = TREE_OPERAND (arg, 0);
12943 tree argtype = TREE_TYPE (arg);
12944 if (TREE_CODE (argtype) == RECORD_TYPE)
12945 {
12946 if (tree fld = last_field (type: argtype))
12947 {
12948 atype = TREE_TYPE (fld);
12949 afield_decl = fld;
12950 if (TREE_CODE (atype) != ARRAY_TYPE)
12951 return false;
12952 if (VAR_P (arg) && DECL_SIZE (fld))
12953 return false;
12954 }
12955 else
12956 return false;
12957 }
12958 else
12959 return false;
12960 }
12961 else
12962 return false;
12963
12964 if (TREE_CODE (ref) == STRING_CST)
12965 return false;
12966
12967 tree ref_to_array = ref;
12968 while (handled_component_p (t: ref))
12969 {
12970 /* If the reference chain contains a component reference to a
12971 non-union type and there follows another field the reference
12972 is not at the end of a structure. */
12973 if (TREE_CODE (ref) == COMPONENT_REF)
12974 {
12975 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
12976 {
12977 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
12978 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
12979 nextf = DECL_CHAIN (nextf);
12980 if (nextf)
12981 return false;
12982 }
12983 }
12984 /* If we have a multi-dimensional array we do not consider
12985 a non-innermost dimension as flex array if the whole
12986 multi-dimensional array is at struct end.
12987 Same for an array of aggregates with a trailing array
12988 member. */
12989 else if (TREE_CODE (ref) == ARRAY_REF)
12990 return false;
12991 else if (TREE_CODE (ref) == ARRAY_RANGE_REF)
12992 ;
12993 /* If we view an underlying object as sth else then what we
12994 gathered up to now is what we have to rely on. */
12995 else if (TREE_CODE (ref) == VIEW_CONVERT_EXPR)
12996 break;
12997 else
12998 gcc_unreachable ();
12999
13000 ref = TREE_OPERAND (ref, 0);
13001 }
13002
13003 gcc_assert (!afield_decl
13004 || (afield_decl && TREE_CODE (afield_decl) == FIELD_DECL));
13005
13006 /* The array now is at struct end. Treat flexible array member as
13007 always subject to extend, even into just padding constrained by
13008 an underlying decl. */
13009 if (! TYPE_SIZE (atype)
13010 || ! TYPE_DOMAIN (atype)
13011 || ! TYPE_MAX_VALUE (TYPE_DOMAIN (atype)))
13012 {
13013 *is_trailing_array = afield_decl && TREE_CODE (afield_decl) == FIELD_DECL;
13014 return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
13015 }
13016
13017 /* If the reference is based on a declared entity, the size of the array
13018 is constrained by its given domain. (Do not trust commons PR/69368). */
13019 ref = get_base_address (t: ref);
13020 if (ref
13021 && DECL_P (ref)
13022 && !(flag_unconstrained_commons
13023 && VAR_P (ref) && DECL_COMMON (ref))
13024 && DECL_SIZE_UNIT (ref)
13025 && TREE_CODE (DECL_SIZE_UNIT (ref)) == INTEGER_CST)
13026 {
13027 /* If the object itself is the array it is not at struct end. */
13028 if (DECL_P (ref_to_array))
13029 return false;
13030
13031 /* Check whether the array domain covers all of the available
13032 padding. */
13033 poly_int64 offset;
13034 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (atype))) != INTEGER_CST
13035 || TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))) != INTEGER_CST
13036 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (atype))) != INTEGER_CST)
13037 {
13038 *is_trailing_array
13039 = afield_decl && TREE_CODE (afield_decl) == FIELD_DECL;
13040 return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
13041 }
13042 if (! get_addr_base_and_unit_offset (ref_to_array, &offset))
13043 {
13044 *is_trailing_array
13045 = afield_decl && TREE_CODE (afield_decl) == FIELD_DECL;
13046 return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
13047 }
13048
13049 /* If at least one extra element fits it is a flexarray. */
13050 if (known_le ((wi::to_offset (TYPE_MAX_VALUE (TYPE_DOMAIN (atype)))
13051 - wi::to_offset (TYPE_MIN_VALUE (TYPE_DOMAIN (atype)))
13052 + 2)
13053 * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (atype))),
13054 wi::to_offset (DECL_SIZE_UNIT (ref)) - offset))
13055 {
13056 *is_trailing_array
13057 = afield_decl && TREE_CODE (afield_decl) == FIELD_DECL;
13058 return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
13059 }
13060
13061 return false;
13062 }
13063
13064 *is_trailing_array = afield_decl && TREE_CODE (afield_decl) == FIELD_DECL;
13065 return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
13066}
13067
13068
13069/* Return a tree representing the offset, in bytes, of the field referenced
13070 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
13071
13072tree
13073component_ref_field_offset (tree exp)
13074{
13075 tree aligned_offset = TREE_OPERAND (exp, 2);
13076 tree field = TREE_OPERAND (exp, 1);
13077 location_t loc = EXPR_LOCATION (exp);
13078
13079 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
13080 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
13081 value. */
13082 if (aligned_offset)
13083 {
13084 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
13085 sizetype from another type of the same width and signedness. */
13086 if (TREE_TYPE (aligned_offset) != sizetype)
13087 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
13088 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
13089 size_int (DECL_OFFSET_ALIGN (field)
13090 / BITS_PER_UNIT));
13091 }
13092
13093 /* Otherwise, take the offset from that of the field. Substitute
13094 any PLACEHOLDER_EXPR that we have. */
13095 else
13096 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
13097}
13098
13099/* Given the initializer INIT, return the initializer for the field
13100 DECL if it exists, otherwise null. Used to obtain the initializer
13101 for a flexible array member and determine its size. */
13102
13103static tree
13104get_initializer_for (tree init, tree decl)
13105{
13106 STRIP_NOPS (init);
13107
13108 tree fld, fld_init;
13109 unsigned HOST_WIDE_INT i;
13110 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), i, fld, fld_init)
13111 {
13112 if (decl == fld)
13113 return fld_init;
13114
13115 if (TREE_CODE (fld) == CONSTRUCTOR)
13116 {
13117 fld_init = get_initializer_for (init: fld_init, decl);
13118 if (fld_init)
13119 return fld_init;
13120 }
13121 }
13122
13123 return NULL_TREE;
13124}
13125
13126/* Determines the special array member type for the array reference REF. */
13127special_array_member
13128component_ref_sam_type (tree ref)
13129{
13130 special_array_member sam_type = special_array_member::none;
13131
13132 tree member = TREE_OPERAND (ref, 1);
13133 tree memsize = DECL_SIZE_UNIT (member);
13134 if (memsize)
13135 {
13136 tree memtype = TREE_TYPE (member);
13137 if (TREE_CODE (memtype) != ARRAY_TYPE)
13138 return sam_type;
13139
13140 bool trailing = false;
13141 (void) array_ref_flexible_size_p (ref, is_trailing_array: &trailing);
13142 bool zero_elts = integer_zerop (expr: memsize);
13143 if (zero_elts && integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (memtype))))
13144 {
13145 /* If array element has zero size, verify if it is a flexible
13146 array member or zero length array. Clear zero_elts if
13147 it has one or more members or is a VLA member. */
13148 if (tree dom = TYPE_DOMAIN (memtype))
13149 if (tree min = TYPE_MIN_VALUE (dom))
13150 if (tree max = TYPE_MAX_VALUE (dom))
13151 if (TREE_CODE (min) != INTEGER_CST
13152 || TREE_CODE (max) != INTEGER_CST
13153 || !((integer_zerop (expr: min) && integer_all_onesp (expr: max))
13154 || tree_int_cst_lt (t1: max, t2: min)))
13155 zero_elts = false;
13156 }
13157 if (!trailing && !zero_elts)
13158 /* MEMBER is an interior array with more than one element. */
13159 return special_array_member::int_n;
13160
13161 if (zero_elts)
13162 {
13163 if (trailing)
13164 return special_array_member::trail_0;
13165 else
13166 return special_array_member::int_0;
13167 }
13168
13169 if (!zero_elts)
13170 if (tree dom = TYPE_DOMAIN (memtype))
13171 if (tree min = TYPE_MIN_VALUE (dom))
13172 if (tree max = TYPE_MAX_VALUE (dom))
13173 if (TREE_CODE (min) == INTEGER_CST
13174 && TREE_CODE (max) == INTEGER_CST)
13175 {
13176 offset_int minidx = wi::to_offset (t: min);
13177 offset_int maxidx = wi::to_offset (t: max);
13178 offset_int neltsm1 = maxidx - minidx;
13179 if (neltsm1 > 0)
13180 /* MEMBER is a trailing array with more than
13181 one elements. */
13182 return special_array_member::trail_n;
13183
13184 if (neltsm1 == 0)
13185 return special_array_member::trail_1;
13186 }
13187 }
13188
13189 return sam_type;
13190}
13191
13192/* Determines the size of the member referenced by the COMPONENT_REF
13193 REF, using its initializer expression if necessary in order to
13194 determine the size of an initialized flexible array member.
13195 If non-null, set *SAM to the type of special array member.
13196 Returns the size as sizetype (which might be zero for an object
13197 with an uninitialized flexible array member) or null if the size
13198 cannot be determined. */
13199
13200tree
13201component_ref_size (tree ref, special_array_member *sam /* = NULL */)
13202{
13203 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
13204
13205 special_array_member sambuf;
13206 if (!sam)
13207 sam = &sambuf;
13208 *sam = component_ref_sam_type (ref);
13209
13210 /* The object/argument referenced by the COMPONENT_REF and its type. */
13211 tree arg = TREE_OPERAND (ref, 0);
13212 tree argtype = TREE_TYPE (arg);
13213 /* The referenced member. */
13214 tree member = TREE_OPERAND (ref, 1);
13215
13216 tree memsize = DECL_SIZE_UNIT (member);
13217 if (memsize)
13218 {
13219 tree memtype = TREE_TYPE (member);
13220 if (TREE_CODE (memtype) != ARRAY_TYPE)
13221 /* DECL_SIZE may be less than TYPE_SIZE in C++ when referring
13222 to the type of a class with a virtual base which doesn't
13223 reflect the size of the virtual's members (see pr97595).
13224 If that's the case fail for now and implement something
13225 more robust in the future. */
13226 return (tree_int_cst_equal (t1: memsize, TYPE_SIZE_UNIT (memtype))
13227 ? memsize : NULL_TREE);
13228
13229 /* 2-or-more elements arrays are treated as normal arrays by default. */
13230 if (*sam == special_array_member::int_n
13231 || *sam == special_array_member::trail_n)
13232 return memsize;
13233
13234 tree afield_decl = TREE_OPERAND (ref, 1);
13235 gcc_assert (TREE_CODE (afield_decl) == FIELD_DECL);
13236 /* If the trailing array is a not a flexible array member, treat it as
13237 a normal array. */
13238 if (DECL_NOT_FLEXARRAY (afield_decl)
13239 && *sam != special_array_member::int_0)
13240 return memsize;
13241
13242 if (*sam == special_array_member::int_0)
13243 memsize = NULL_TREE;
13244
13245 /* For a reference to a flexible array member of a union
13246 use the size of the union instead of the size of the member. */
13247 if (TREE_CODE (argtype) == UNION_TYPE)
13248 memsize = TYPE_SIZE_UNIT (argtype);
13249 }
13250
13251 /* MEMBER is either a bona fide flexible array member, or a zero-elements
13252 array member, or an array of length one treated as such. */
13253
13254 /* If the reference is to a declared object and the member a true
13255 flexible array, try to determine its size from its initializer. */
13256 poly_int64 baseoff = 0;
13257 tree base = get_addr_base_and_unit_offset (ref, &baseoff);
13258 if (!base || !VAR_P (base))
13259 {
13260 if (*sam != special_array_member::int_0)
13261 return NULL_TREE;
13262
13263 if (TREE_CODE (arg) != COMPONENT_REF)
13264 return NULL_TREE;
13265
13266 base = arg;
13267 while (TREE_CODE (base) == COMPONENT_REF)
13268 base = TREE_OPERAND (base, 0);
13269 baseoff = tree_to_poly_int64 (t: byte_position (TREE_OPERAND (ref, 1)));
13270 }
13271
13272 /* BASE is the declared object of which MEMBER is either a member
13273 or that is cast to ARGTYPE (e.g., a char buffer used to store
13274 an ARGTYPE object). */
13275 tree basetype = TREE_TYPE (base);
13276
13277 /* Determine the base type of the referenced object. If it's
13278 the same as ARGTYPE and MEMBER has a known size, return it. */
13279 tree bt = basetype;
13280 if (*sam != special_array_member::int_0)
13281 while (TREE_CODE (bt) == ARRAY_TYPE)
13282 bt = TREE_TYPE (bt);
13283 bool typematch = useless_type_conversion_p (argtype, bt);
13284 if (memsize && typematch)
13285 return memsize;
13286
13287 memsize = NULL_TREE;
13288
13289 if (typematch)
13290 /* MEMBER is a true flexible array member. Compute its size from
13291 the initializer of the BASE object if it has one. */
13292 if (tree init = DECL_P (base) ? DECL_INITIAL (base) : NULL_TREE)
13293 if (init != error_mark_node)
13294 {
13295 init = get_initializer_for (init, decl: member);
13296 if (init)
13297 {
13298 memsize = TYPE_SIZE_UNIT (TREE_TYPE (init));
13299 if (tree refsize = TYPE_SIZE_UNIT (argtype))
13300 {
13301 /* Use the larger of the initializer size and the tail
13302 padding in the enclosing struct. */
13303 poly_int64 rsz = tree_to_poly_int64 (t: refsize);
13304 rsz -= baseoff;
13305 if (known_lt (tree_to_poly_int64 (memsize), rsz))
13306 memsize = wide_int_to_tree (TREE_TYPE (memsize), value: rsz);
13307 }
13308
13309 baseoff = 0;
13310 }
13311 }
13312
13313 if (!memsize)
13314 {
13315 if (typematch)
13316 {
13317 if (DECL_P (base)
13318 && DECL_EXTERNAL (base)
13319 && bt == basetype
13320 && *sam != special_array_member::int_0)
13321 /* The size of a flexible array member of an extern struct
13322 with no initializer cannot be determined (it's defined
13323 in another translation unit and can have an initializer
13324 with an arbitrary number of elements). */
13325 return NULL_TREE;
13326
13327 /* Use the size of the base struct or, for interior zero-length
13328 arrays, the size of the enclosing type. */
13329 memsize = TYPE_SIZE_UNIT (bt);
13330 }
13331 else if (DECL_P (base))
13332 /* Use the size of the BASE object (possibly an array of some
13333 other type such as char used to store the struct). */
13334 memsize = DECL_SIZE_UNIT (base);
13335 else
13336 return NULL_TREE;
13337 }
13338
13339 /* If the flexible array member has a known size use the greater
13340 of it and the tail padding in the enclosing struct.
13341 Otherwise, when the size of the flexible array member is unknown
13342 and the referenced object is not a struct, use the size of its
13343 type when known. This detects sizes of array buffers when cast
13344 to struct types with flexible array members. */
13345 if (memsize)
13346 {
13347 if (!tree_fits_poly_int64_p (t: memsize))
13348 return NULL_TREE;
13349 poly_int64 memsz64 = memsize ? tree_to_poly_int64 (t: memsize) : 0;
13350 if (known_lt (baseoff, memsz64))
13351 {
13352 memsz64 -= baseoff;
13353 return wide_int_to_tree (TREE_TYPE (memsize), value: memsz64);
13354 }
13355 return size_zero_node;
13356 }
13357
13358 /* Return "don't know" for an external non-array object since its
13359 flexible array member can be initialized to have any number of
13360 elements. Otherwise, return zero because the flexible array
13361 member has no elements. */
13362 return (DECL_P (base)
13363 && DECL_EXTERNAL (base)
13364 && (!typematch
13365 || TREE_CODE (basetype) != ARRAY_TYPE)
13366 ? NULL_TREE : size_zero_node);
13367}
13368
13369/* Return the machine mode of T. For vectors, returns the mode of the
13370 inner type. The main use case is to feed the result to HONOR_NANS,
13371 avoiding the BLKmode that a direct TYPE_MODE (T) might return. */
13372
13373machine_mode
13374element_mode (const_tree t)
13375{
13376 if (!TYPE_P (t))
13377 t = TREE_TYPE (t);
13378 if (VECTOR_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
13379 t = TREE_TYPE (t);
13380 return TYPE_MODE (t);
13381}
13382
13383/* Vector types need to re-check the target flags each time we report
13384 the machine mode. We need to do this because attribute target can
13385 change the result of vector_mode_supported_p and have_regs_of_mode
13386 on a per-function basis. Thus the TYPE_MODE of a VECTOR_TYPE can
13387 change on a per-function basis. */
13388/* ??? Possibly a better solution is to run through all the types
13389 referenced by a function and re-compute the TYPE_MODE once, rather
13390 than make the TYPE_MODE macro call a function. */
13391
13392machine_mode
13393vector_type_mode (const_tree t)
13394{
13395 machine_mode mode;
13396
13397 gcc_assert (TREE_CODE (t) == VECTOR_TYPE);
13398
13399 mode = t->type_common.mode;
13400 if (VECTOR_MODE_P (mode)
13401 && (!targetm.vector_mode_supported_p (mode)
13402 || !have_regs_of_mode[mode]))
13403 {
13404 scalar_int_mode innermode;
13405
13406 /* For integers, try mapping it to a same-sized scalar mode. */
13407 if (is_int_mode (TREE_TYPE (t)->type_common.mode, int_mode: &innermode))
13408 {
13409 poly_int64 size = (TYPE_VECTOR_SUBPARTS (node: t)
13410 * GET_MODE_BITSIZE (mode: innermode));
13411 scalar_int_mode mode;
13412 if (int_mode_for_size (size, limit: 0).exists (mode: &mode)
13413 && have_regs_of_mode[mode])
13414 return mode;
13415 }
13416
13417 return BLKmode;
13418 }
13419
13420 return mode;
13421}
13422
13423/* Return the size in bits of each element of vector type TYPE. */
13424
13425unsigned int
13426vector_element_bits (const_tree type)
13427{
13428 gcc_checking_assert (VECTOR_TYPE_P (type));
13429 if (VECTOR_BOOLEAN_TYPE_P (type))
13430 return TYPE_PRECISION (TREE_TYPE (type));
13431 return tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)));
13432}
13433
13434/* Calculate the size in bits of each element of vector type TYPE
13435 and return the result as a tree of type bitsizetype. */
13436
13437tree
13438vector_element_bits_tree (const_tree type)
13439{
13440 gcc_checking_assert (VECTOR_TYPE_P (type));
13441 if (VECTOR_BOOLEAN_TYPE_P (type))
13442 return bitsize_int (vector_element_bits (type));
13443 return TYPE_SIZE (TREE_TYPE (type));
13444}
13445
13446/* Verify that basic properties of T match TV and thus T can be a variant of
13447 TV. TV should be the more specified variant (i.e. the main variant). */
13448
13449static bool
13450verify_type_variant (const_tree t, tree tv)
13451{
13452 /* Type variant can differ by:
13453
13454 - TYPE_QUALS: TYPE_READONLY, TYPE_VOLATILE, TYPE_ATOMIC, TYPE_RESTRICT,
13455 ENCODE_QUAL_ADDR_SPACE.
13456 - main variant may be TYPE_COMPLETE_P and variant types !TYPE_COMPLETE_P
13457 in this case some values may not be set in the variant types
13458 (see TYPE_COMPLETE_P checks).
13459 - it is possible to have TYPE_ARTIFICIAL variant of non-artifical type
13460 - by TYPE_NAME and attributes (i.e. when variant originate by typedef)
13461 - TYPE_CANONICAL (TYPE_ALIAS_SET is the same among variants)
13462 - by the alignment: TYPE_ALIGN and TYPE_USER_ALIGN
13463 - during LTO by TYPE_CONTEXT if type is TYPE_FILE_SCOPE_P
13464 this is necessary to make it possible to merge types form different TUs
13465 - arrays, pointers and references may have TREE_TYPE that is a variant
13466 of TREE_TYPE of their main variants.
13467 - aggregates may have new TYPE_FIELDS list that list variants of
13468 the main variant TYPE_FIELDS.
13469 - vector types may differ by TYPE_VECTOR_OPAQUE
13470 */
13471
13472 /* Convenience macro for matching individual fields. */
13473#define verify_variant_match(flag) \
13474 do { \
13475 if (flag (tv) != flag (t)) \
13476 { \
13477 error ("type variant differs by %s", #flag); \
13478 debug_tree (tv); \
13479 return false; \
13480 } \
13481 } while (false)
13482
13483 /* tree_base checks. */
13484
13485 verify_variant_match (TREE_CODE);
13486 /* FIXME: Ada builds non-artificial variants of artificial types. */
13487#if 0
13488 if (TYPE_ARTIFICIAL (tv))
13489 verify_variant_match (TYPE_ARTIFICIAL);
13490#endif
13491 if (POINTER_TYPE_P (tv))
13492 verify_variant_match (TYPE_REF_CAN_ALIAS_ALL);
13493 /* FIXME: TYPE_SIZES_GIMPLIFIED may differs for Ada build. */
13494 verify_variant_match (TYPE_UNSIGNED);
13495 verify_variant_match (TYPE_PACKED);
13496 if (TREE_CODE (t) == REFERENCE_TYPE)
13497 verify_variant_match (TYPE_REF_IS_RVALUE);
13498 if (AGGREGATE_TYPE_P (t))
13499 verify_variant_match (TYPE_REVERSE_STORAGE_ORDER);
13500 else
13501 verify_variant_match (TYPE_SATURATING);
13502 /* FIXME: This check trigger during libstdc++ build. */
13503#if 0
13504 if (RECORD_OR_UNION_TYPE_P (t) && COMPLETE_TYPE_P (t))
13505 verify_variant_match (TYPE_FINAL_P);
13506#endif
13507
13508 /* tree_type_common checks. */
13509
13510 if (COMPLETE_TYPE_P (t))
13511 {
13512 verify_variant_match (TYPE_MODE);
13513 if (TREE_CODE (TYPE_SIZE (t)) != PLACEHOLDER_EXPR
13514 && TREE_CODE (TYPE_SIZE (tv)) != PLACEHOLDER_EXPR)
13515 verify_variant_match (TYPE_SIZE);
13516 if (TREE_CODE (TYPE_SIZE_UNIT (t)) != PLACEHOLDER_EXPR
13517 && TREE_CODE (TYPE_SIZE_UNIT (tv)) != PLACEHOLDER_EXPR
13518 && TYPE_SIZE_UNIT (t) != TYPE_SIZE_UNIT (tv))
13519 {
13520 gcc_assert (!operand_equal_p (TYPE_SIZE_UNIT (t),
13521 TYPE_SIZE_UNIT (tv), 0));
13522 error ("type variant has different %<TYPE_SIZE_UNIT%>");
13523 debug_tree (tv);
13524 error ("type variant%'s %<TYPE_SIZE_UNIT%>");
13525 debug_tree (TYPE_SIZE_UNIT (tv));
13526 error ("type%'s %<TYPE_SIZE_UNIT%>");
13527 debug_tree (TYPE_SIZE_UNIT (t));
13528 return false;
13529 }
13530 verify_variant_match (TYPE_NEEDS_CONSTRUCTING);
13531 }
13532 verify_variant_match (TYPE_PRECISION_RAW);
13533 if (RECORD_OR_UNION_TYPE_P (t))
13534 verify_variant_match (TYPE_TRANSPARENT_AGGR);
13535 else if (TREE_CODE (t) == ARRAY_TYPE)
13536 verify_variant_match (TYPE_NONALIASED_COMPONENT);
13537 /* During LTO we merge variant lists from diferent translation units
13538 that may differ BY TYPE_CONTEXT that in turn may point
13539 to TRANSLATION_UNIT_DECL.
13540 Ada also builds variants of types with different TYPE_CONTEXT. */
13541#if 0
13542 if (!in_lto_p || !TYPE_FILE_SCOPE_P (t))
13543 verify_variant_match (TYPE_CONTEXT);
13544#endif
13545 if (TREE_CODE (t) == ARRAY_TYPE || TREE_CODE (t) == INTEGER_TYPE)
13546 verify_variant_match (TYPE_STRING_FLAG);
13547 if (TREE_CODE (t) == RECORD_TYPE || TREE_CODE (t) == UNION_TYPE)
13548 verify_variant_match (TYPE_CXX_ODR_P);
13549 if (TYPE_ALIAS_SET_KNOWN_P (t))
13550 {
13551 error ("type variant with %<TYPE_ALIAS_SET_KNOWN_P%>");
13552 debug_tree (tv);
13553 return false;
13554 }
13555
13556 /* tree_type_non_common checks. */
13557
13558 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
13559 and dangle the pointer from time to time. */
13560 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_VFIELD (t) != TYPE_VFIELD (tv)
13561 && (in_lto_p || !TYPE_VFIELD (tv)
13562 || TREE_CODE (TYPE_VFIELD (tv)) != TREE_LIST))
13563 {
13564 error ("type variant has different %<TYPE_VFIELD%>");
13565 debug_tree (tv);
13566 return false;
13567 }
13568 if ((TREE_CODE (t) == ENUMERAL_TYPE && COMPLETE_TYPE_P (t))
13569 || TREE_CODE (t) == INTEGER_TYPE
13570 || TREE_CODE (t) == BOOLEAN_TYPE
13571 || TREE_CODE (t) == BITINT_TYPE
13572 || SCALAR_FLOAT_TYPE_P (t)
13573 || FIXED_POINT_TYPE_P (t))
13574 {
13575 verify_variant_match (TYPE_MAX_VALUE);
13576 verify_variant_match (TYPE_MIN_VALUE);
13577 }
13578 if (TREE_CODE (t) == METHOD_TYPE)
13579 verify_variant_match (TYPE_METHOD_BASETYPE);
13580 if (TREE_CODE (t) == OFFSET_TYPE)
13581 verify_variant_match (TYPE_OFFSET_BASETYPE);
13582 if (TREE_CODE (t) == ARRAY_TYPE)
13583 verify_variant_match (TYPE_ARRAY_MAX_SIZE);
13584 /* FIXME: Be lax and allow TYPE_BINFO to be missing in variant types
13585 or even type's main variant. This is needed to make bootstrap pass
13586 and the bug seems new in GCC 5.
13587 C++ FE should be updated to make this consistent and we should check
13588 that TYPE_BINFO is always NULL for !COMPLETE_TYPE_P and otherwise there
13589 is a match with main variant.
13590
13591 Also disable the check for Java for now because of parser hack that builds
13592 first an dummy BINFO and then sometimes replace it by real BINFO in some
13593 of the copies. */
13594 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t) && TYPE_BINFO (tv)
13595 && TYPE_BINFO (t) != TYPE_BINFO (tv)
13596 /* FIXME: Java sometimes keep dump TYPE_BINFOs on variant types.
13597 Since there is no cheap way to tell C++/Java type w/o LTO, do checking
13598 at LTO time only. */
13599 && (in_lto_p && odr_type_p (t)))
13600 {
13601 error ("type variant has different %<TYPE_BINFO%>");
13602 debug_tree (tv);
13603 error ("type variant%'s %<TYPE_BINFO%>");
13604 debug_tree (TYPE_BINFO (tv));
13605 error ("type%'s %<TYPE_BINFO%>");
13606 debug_tree (TYPE_BINFO (t));
13607 return false;
13608 }
13609
13610 /* Check various uses of TYPE_VALUES_RAW. */
13611 if (TREE_CODE (t) == ENUMERAL_TYPE
13612 && TYPE_VALUES (t))
13613 verify_variant_match (TYPE_VALUES);
13614 else if (TREE_CODE (t) == ARRAY_TYPE)
13615 verify_variant_match (TYPE_DOMAIN);
13616 /* Permit incomplete variants of complete type. While FEs may complete
13617 all variants, this does not happen for C++ templates in all cases. */
13618 else if (RECORD_OR_UNION_TYPE_P (t)
13619 && COMPLETE_TYPE_P (t)
13620 && TYPE_FIELDS (t) != TYPE_FIELDS (tv))
13621 {
13622 tree f1, f2;
13623
13624 /* Fortran builds qualified variants as new records with items of
13625 qualified type. Verify that they looks same. */
13626 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (tv);
13627 f1 && f2;
13628 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
13629 if (TREE_CODE (f1) != FIELD_DECL || TREE_CODE (f2) != FIELD_DECL
13630 || (TYPE_MAIN_VARIANT (TREE_TYPE (f1))
13631 != TYPE_MAIN_VARIANT (TREE_TYPE (f2))
13632 /* FIXME: gfc_nonrestricted_type builds all types as variants
13633 with exception of pointer types. It deeply copies the type
13634 which means that we may end up with a variant type
13635 referring non-variant pointer. We may change it to
13636 produce types as variants, too, like
13637 objc_get_protocol_qualified_type does. */
13638 && !POINTER_TYPE_P (TREE_TYPE (f1)))
13639 || DECL_FIELD_OFFSET (f1) != DECL_FIELD_OFFSET (f2)
13640 || DECL_FIELD_BIT_OFFSET (f1) != DECL_FIELD_BIT_OFFSET (f2))
13641 break;
13642 if (f1 || f2)
13643 {
13644 error ("type variant has different %<TYPE_FIELDS%>");
13645 debug_tree (tv);
13646 error ("first mismatch is field");
13647 debug_tree (f1);
13648 error ("and field");
13649 debug_tree (f2);
13650 return false;
13651 }
13652 }
13653 else if (FUNC_OR_METHOD_TYPE_P (t))
13654 verify_variant_match (TYPE_ARG_TYPES);
13655 /* For C++ the qualified variant of array type is really an array type
13656 of qualified TREE_TYPE.
13657 objc builds variants of pointer where pointer to type is a variant, too
13658 in objc_get_protocol_qualified_type. */
13659 if (TREE_TYPE (t) != TREE_TYPE (tv)
13660 && ((TREE_CODE (t) != ARRAY_TYPE
13661 && !POINTER_TYPE_P (t))
13662 || TYPE_MAIN_VARIANT (TREE_TYPE (t))
13663 != TYPE_MAIN_VARIANT (TREE_TYPE (tv))))
13664 {
13665 error ("type variant has different %<TREE_TYPE%>");
13666 debug_tree (tv);
13667 error ("type variant%'s %<TREE_TYPE%>");
13668 debug_tree (TREE_TYPE (tv));
13669 error ("type%'s %<TREE_TYPE%>");
13670 debug_tree (TREE_TYPE (t));
13671 return false;
13672 }
13673 if (type_with_alias_set_p (t)
13674 && !gimple_canonical_types_compatible_p (t, tv, trust_type_canonical: false))
13675 {
13676 error ("type is not compatible with its variant");
13677 debug_tree (tv);
13678 error ("type variant%'s %<TREE_TYPE%>");
13679 debug_tree (TREE_TYPE (tv));
13680 error ("type%'s %<TREE_TYPE%>");
13681 debug_tree (TREE_TYPE (t));
13682 return false;
13683 }
13684 return true;
13685#undef verify_variant_match
13686}
13687
13688
13689/* The TYPE_CANONICAL merging machinery. It should closely resemble
13690 the middle-end types_compatible_p function. It needs to avoid
13691 claiming types are different for types that should be treated
13692 the same with respect to TBAA. Canonical types are also used
13693 for IL consistency checks via the useless_type_conversion_p
13694 predicate which does not handle all type kinds itself but falls
13695 back to pointer-comparison of TYPE_CANONICAL for aggregates
13696 for example. */
13697
13698/* Return true if TYPE_UNSIGNED of TYPE should be ignored for canonical
13699 type calculation because we need to allow inter-operability between signed
13700 and unsigned variants. */
13701
13702bool
13703type_with_interoperable_signedness (const_tree type)
13704{
13705 /* Fortran standard require C_SIGNED_CHAR to be interoperable with both
13706 signed char and unsigned char. Similarly fortran FE builds
13707 C_SIZE_T as signed type, while C defines it unsigned. */
13708
13709 return tree_code_for_canonical_type_merging (TREE_CODE (type))
13710 == INTEGER_TYPE
13711 && (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node)
13712 || TYPE_PRECISION (type) == TYPE_PRECISION (size_type_node));
13713}
13714
13715/* Return true iff T1 and T2 are structurally identical for what
13716 TBAA is concerned.
13717 This function is used both by lto.cc canonical type merging and by the
13718 verifier. If TRUST_TYPE_CANONICAL we do not look into structure of types
13719 that have TYPE_CANONICAL defined and assume them equivalent. This is useful
13720 only for LTO because only in these cases TYPE_CANONICAL equivalence
13721 correspond to one defined by gimple_canonical_types_compatible_p. */
13722
13723bool
13724gimple_canonical_types_compatible_p (const_tree t1, const_tree t2,
13725 bool trust_type_canonical)
13726{
13727 /* Type variants should be same as the main variant. When not doing sanity
13728 checking to verify this fact, go to main variants and save some work. */
13729 if (trust_type_canonical)
13730 {
13731 t1 = TYPE_MAIN_VARIANT (t1);
13732 t2 = TYPE_MAIN_VARIANT (t2);
13733 }
13734
13735 /* Check first for the obvious case of pointer identity. */
13736 if (t1 == t2)
13737 return true;
13738
13739 /* Check that we have two types to compare. */
13740 if (t1 == NULL_TREE || t2 == NULL_TREE)
13741 return false;
13742
13743 /* We consider complete types always compatible with incomplete type.
13744 This does not make sense for canonical type calculation and thus we
13745 need to ensure that we are never called on it.
13746
13747 FIXME: For more correctness the function probably should have three modes
13748 1) mode assuming that types are complete mathcing their structure
13749 2) mode allowing incomplete types but producing equivalence classes
13750 and thus ignoring all info from complete types
13751 3) mode allowing incomplete types to match complete but checking
13752 compatibility between complete types.
13753
13754 1 and 2 can be used for canonical type calculation. 3 is the real
13755 definition of type compatibility that can be used i.e. for warnings during
13756 declaration merging. */
13757
13758 gcc_assert (!trust_type_canonical
13759 || (type_with_alias_set_p (t1) && type_with_alias_set_p (t2)));
13760
13761 /* If the types have been previously registered and found equal
13762 they still are. */
13763
13764 if (TYPE_CANONICAL (t1) && TYPE_CANONICAL (t2)
13765 && trust_type_canonical)
13766 {
13767 /* Do not use TYPE_CANONICAL of pointer types. For LTO streamed types
13768 they are always NULL, but they are set to non-NULL for types
13769 constructed by build_pointer_type and variants. In this case the
13770 TYPE_CANONICAL is more fine grained than the equivalnce we test (where
13771 all pointers are considered equal. Be sure to not return false
13772 negatives. */
13773 gcc_checking_assert (canonical_type_used_p (t1)
13774 && canonical_type_used_p (t2));
13775 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
13776 }
13777
13778 /* For types where we do ODR based TBAA the canonical type is always
13779 set correctly, so we know that types are different if their
13780 canonical types does not match. */
13781 if (trust_type_canonical
13782 && (odr_type_p (t: t1) && odr_based_tbaa_p (type: t1))
13783 != (odr_type_p (t: t2) && odr_based_tbaa_p (type: t2)))
13784 return false;
13785
13786 /* Can't be the same type if the types don't have the same code. */
13787 enum tree_code code = tree_code_for_canonical_type_merging (TREE_CODE (t1));
13788 if (code != tree_code_for_canonical_type_merging (TREE_CODE (t2)))
13789 return false;
13790
13791 /* Qualifiers do not matter for canonical type comparison purposes. */
13792
13793 /* Void types and nullptr types are always the same. */
13794 if (VOID_TYPE_P (t1)
13795 || TREE_CODE (t1) == NULLPTR_TYPE)
13796 return true;
13797
13798 /* Can't be the same type if they have different mode. */
13799 if (TYPE_MODE (t1) != TYPE_MODE (t2))
13800 return false;
13801
13802 /* Non-aggregate types can be handled cheaply. */
13803 if (INTEGRAL_TYPE_P (t1)
13804 || SCALAR_FLOAT_TYPE_P (t1)
13805 || FIXED_POINT_TYPE_P (t1)
13806 || VECTOR_TYPE_P (t1)
13807 || TREE_CODE (t1) == COMPLEX_TYPE
13808 || TREE_CODE (t1) == OFFSET_TYPE
13809 || POINTER_TYPE_P (t1))
13810 {
13811 /* Can't be the same type if they have different precision. */
13812 if (TYPE_PRECISION_RAW (t1) != TYPE_PRECISION_RAW (t2))
13813 return false;
13814
13815 /* In some cases the signed and unsigned types are required to be
13816 inter-operable. */
13817 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2)
13818 && !type_with_interoperable_signedness (type: t1))
13819 return false;
13820
13821 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
13822 interoperable with "signed char". Unless all frontends are revisited
13823 to agree on these types, we must ignore the flag completely. */
13824
13825 /* Fortran standard define C_PTR type that is compatible with every
13826 C pointer. For this reason we need to glob all pointers into one.
13827 Still pointers in different address spaces are not compatible. */
13828 if (POINTER_TYPE_P (t1))
13829 {
13830 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
13831 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
13832 return false;
13833 }
13834
13835 /* Tail-recurse to components. */
13836 if (VECTOR_TYPE_P (t1)
13837 || TREE_CODE (t1) == COMPLEX_TYPE)
13838 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
13839 TREE_TYPE (t2),
13840 trust_type_canonical);
13841
13842 return true;
13843 }
13844
13845 /* Do type-specific comparisons. */
13846 switch (TREE_CODE (t1))
13847 {
13848 case ARRAY_TYPE:
13849 /* Array types are the same if the element types are the same and
13850 the number of elements are the same. */
13851 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13852 trust_type_canonical)
13853 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
13854 || TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2)
13855 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
13856 return false;
13857 else
13858 {
13859 tree i1 = TYPE_DOMAIN (t1);
13860 tree i2 = TYPE_DOMAIN (t2);
13861
13862 /* For an incomplete external array, the type domain can be
13863 NULL_TREE. Check this condition also. */
13864 if (i1 == NULL_TREE && i2 == NULL_TREE)
13865 return true;
13866 else if (i1 == NULL_TREE || i2 == NULL_TREE)
13867 return false;
13868 else
13869 {
13870 tree min1 = TYPE_MIN_VALUE (i1);
13871 tree min2 = TYPE_MIN_VALUE (i2);
13872 tree max1 = TYPE_MAX_VALUE (i1);
13873 tree max2 = TYPE_MAX_VALUE (i2);
13874
13875 /* The minimum/maximum values have to be the same. */
13876 if ((min1 == min2
13877 || (min1 && min2
13878 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
13879 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
13880 || operand_equal_p (min1, min2, flags: 0))))
13881 && (max1 == max2
13882 || (max1 && max2
13883 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
13884 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
13885 || operand_equal_p (max1, max2, flags: 0)))))
13886 return true;
13887 else
13888 return false;
13889 }
13890 }
13891
13892 case METHOD_TYPE:
13893 case FUNCTION_TYPE:
13894 /* Function types are the same if the return type and arguments types
13895 are the same. */
13896 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13897 trust_type_canonical))
13898 return false;
13899
13900 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2)
13901 && (TYPE_NO_NAMED_ARGS_STDARG_P (t1)
13902 == TYPE_NO_NAMED_ARGS_STDARG_P (t2)))
13903 return true;
13904 else
13905 {
13906 tree parms1, parms2;
13907
13908 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
13909 parms1 && parms2;
13910 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
13911 {
13912 if (!gimple_canonical_types_compatible_p
13913 (TREE_VALUE (parms1), TREE_VALUE (parms2),
13914 trust_type_canonical))
13915 return false;
13916 }
13917
13918 if (parms1 || parms2)
13919 return false;
13920
13921 return true;
13922 }
13923
13924 case RECORD_TYPE:
13925 case UNION_TYPE:
13926 case QUAL_UNION_TYPE:
13927 {
13928 tree f1, f2;
13929
13930 /* Don't try to compare variants of an incomplete type, before
13931 TYPE_FIELDS has been copied around. */
13932 if (!COMPLETE_TYPE_P (t1) && !COMPLETE_TYPE_P (t2))
13933 return true;
13934
13935
13936 if (TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2))
13937 return false;
13938
13939 /* For aggregate types, all the fields must be the same. */
13940 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
13941 f1 || f2;
13942 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
13943 {
13944 /* Skip non-fields and zero-sized fields. */
13945 while (f1 && (TREE_CODE (f1) != FIELD_DECL
13946 || (DECL_SIZE (f1)
13947 && integer_zerop (DECL_SIZE (f1)))))
13948 f1 = TREE_CHAIN (f1);
13949 while (f2 && (TREE_CODE (f2) != FIELD_DECL
13950 || (DECL_SIZE (f2)
13951 && integer_zerop (DECL_SIZE (f2)))))
13952 f2 = TREE_CHAIN (f2);
13953 if (!f1 || !f2)
13954 break;
13955 /* The fields must have the same name, offset and type. */
13956 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
13957 || !gimple_compare_field_offset (f1, f2)
13958 || !gimple_canonical_types_compatible_p
13959 (TREE_TYPE (f1), TREE_TYPE (f2),
13960 trust_type_canonical))
13961 return false;
13962 }
13963
13964 /* If one aggregate has more fields than the other, they
13965 are not the same. */
13966 if (f1 || f2)
13967 return false;
13968
13969 return true;
13970 }
13971
13972 default:
13973 /* Consider all types with language specific trees in them mutually
13974 compatible. This is executed only from verify_type and false
13975 positives can be tolerated. */
13976 gcc_assert (!in_lto_p);
13977 return true;
13978 }
13979}
13980
13981/* For OPAQUE_TYPE T, it should have only size and alignment information
13982 and its mode should be of class MODE_OPAQUE. This function verifies
13983 these properties of T match TV which is the main variant of T and TC
13984 which is the canonical of T. */
13985
13986static void
13987verify_opaque_type (const_tree t, tree tv, tree tc)
13988{
13989 gcc_assert (OPAQUE_TYPE_P (t));
13990 gcc_assert (tv && tv == TYPE_MAIN_VARIANT (tv));
13991 gcc_assert (tc && tc == TYPE_CANONICAL (tc));
13992
13993 /* For an opaque type T1, check if some of its properties match
13994 the corresponding ones of the other opaque type T2, emit some
13995 error messages for those inconsistent ones. */
13996 auto check_properties_for_opaque_type = [](const_tree t1, tree t2,
13997 const char *kind_msg)
13998 {
13999 if (!OPAQUE_TYPE_P (t2))
14000 {
14001 error ("type %s is not an opaque type", kind_msg);
14002 debug_tree (t2);
14003 return;
14004 }
14005 if (!OPAQUE_MODE_P (TYPE_MODE (t2)))
14006 {
14007 error ("type %s is not with opaque mode", kind_msg);
14008 debug_tree (t2);
14009 return;
14010 }
14011 if (TYPE_MODE (t1) != TYPE_MODE (t2))
14012 {
14013 error ("type %s differs by %<TYPE_MODE%>", kind_msg);
14014 debug_tree (t2);
14015 return;
14016 }
14017 poly_uint64 t1_size = tree_to_poly_uint64 (TYPE_SIZE (t1));
14018 poly_uint64 t2_size = tree_to_poly_uint64 (TYPE_SIZE (t2));
14019 if (maybe_ne (a: t1_size, b: t2_size))
14020 {
14021 error ("type %s differs by %<TYPE_SIZE%>", kind_msg);
14022 debug_tree (t2);
14023 return;
14024 }
14025 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
14026 {
14027 error ("type %s differs by %<TYPE_ALIGN%>", kind_msg);
14028 debug_tree (t2);
14029 return;
14030 }
14031 if (TYPE_USER_ALIGN (t1) != TYPE_USER_ALIGN (t2))
14032 {
14033 error ("type %s differs by %<TYPE_USER_ALIGN%>", kind_msg);
14034 debug_tree (t2);
14035 return;
14036 }
14037 };
14038
14039 if (t != tv)
14040 check_properties_for_opaque_type (t, tv, "variant");
14041
14042 if (t != tc)
14043 check_properties_for_opaque_type (t, tc, "canonical");
14044}
14045
14046/* Verify type T. */
14047
14048void
14049verify_type (const_tree t)
14050{
14051 bool error_found = false;
14052 tree mv = TYPE_MAIN_VARIANT (t);
14053 tree ct = TYPE_CANONICAL (t);
14054
14055 if (OPAQUE_TYPE_P (t))
14056 {
14057 verify_opaque_type (t, tv: mv, tc: ct);
14058 return;
14059 }
14060
14061 if (!mv)
14062 {
14063 error ("main variant is not defined");
14064 error_found = true;
14065 }
14066 else if (mv != TYPE_MAIN_VARIANT (mv))
14067 {
14068 error ("%<TYPE_MAIN_VARIANT%> has different %<TYPE_MAIN_VARIANT%>");
14069 debug_tree (mv);
14070 error_found = true;
14071 }
14072 else if (t != mv && !verify_type_variant (t, tv: mv))
14073 error_found = true;
14074
14075 if (!ct)
14076 ;
14077 else if (TYPE_CANONICAL (ct) != ct)
14078 {
14079 error ("%<TYPE_CANONICAL%> has different %<TYPE_CANONICAL%>");
14080 debug_tree (ct);
14081 error_found = true;
14082 }
14083 /* Method and function types cannot be used to address memory and thus
14084 TYPE_CANONICAL really matters only for determining useless conversions.
14085
14086 FIXME: C++ FE produce declarations of builtin functions that are not
14087 compatible with main variants. */
14088 else if (TREE_CODE (t) == FUNCTION_TYPE)
14089 ;
14090 else if (t != ct
14091 /* FIXME: gimple_canonical_types_compatible_p cannot compare types
14092 with variably sized arrays because their sizes possibly
14093 gimplified to different variables. */
14094 && !variably_modified_type_p (type: ct, NULL)
14095 && !gimple_canonical_types_compatible_p (t1: t, t2: ct, trust_type_canonical: false)
14096 && COMPLETE_TYPE_P (t))
14097 {
14098 error ("%<TYPE_CANONICAL%> is not compatible");
14099 debug_tree (ct);
14100 error_found = true;
14101 }
14102
14103 if (COMPLETE_TYPE_P (t) && TYPE_CANONICAL (t)
14104 && TYPE_MODE (t) != TYPE_MODE (TYPE_CANONICAL (t)))
14105 {
14106 error ("%<TYPE_MODE%> of %<TYPE_CANONICAL%> is not compatible");
14107 debug_tree (ct);
14108 error_found = true;
14109 }
14110 if (TYPE_MAIN_VARIANT (t) == t && ct && TYPE_MAIN_VARIANT (ct) != ct)
14111 {
14112 error ("%<TYPE_CANONICAL%> of main variant is not main variant");
14113 debug_tree (ct);
14114 debug_tree (TYPE_MAIN_VARIANT (ct));
14115 error_found = true;
14116 }
14117
14118
14119 /* Check various uses of TYPE_MIN_VALUE_RAW. */
14120 if (RECORD_OR_UNION_TYPE_P (t))
14121 {
14122 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
14123 and danagle the pointer from time to time. */
14124 if (TYPE_VFIELD (t)
14125 && TREE_CODE (TYPE_VFIELD (t)) != FIELD_DECL
14126 && TREE_CODE (TYPE_VFIELD (t)) != TREE_LIST)
14127 {
14128 error ("%<TYPE_VFIELD%> is not %<FIELD_DECL%> nor %<TREE_LIST%>");
14129 debug_tree (TYPE_VFIELD (t));
14130 error_found = true;
14131 }
14132 }
14133 else if (TREE_CODE (t) == POINTER_TYPE)
14134 {
14135 if (TYPE_NEXT_PTR_TO (t)
14136 && TREE_CODE (TYPE_NEXT_PTR_TO (t)) != POINTER_TYPE)
14137 {
14138 error ("%<TYPE_NEXT_PTR_TO%> is not %<POINTER_TYPE%>");
14139 debug_tree (TYPE_NEXT_PTR_TO (t));
14140 error_found = true;
14141 }
14142 }
14143 else if (TREE_CODE (t) == REFERENCE_TYPE)
14144 {
14145 if (TYPE_NEXT_REF_TO (t)
14146 && TREE_CODE (TYPE_NEXT_REF_TO (t)) != REFERENCE_TYPE)
14147 {
14148 error ("%<TYPE_NEXT_REF_TO%> is not %<REFERENCE_TYPE%>");
14149 debug_tree (TYPE_NEXT_REF_TO (t));
14150 error_found = true;
14151 }
14152 }
14153 else if (INTEGRAL_TYPE_P (t) || SCALAR_FLOAT_TYPE_P (t)
14154 || FIXED_POINT_TYPE_P (t))
14155 {
14156 /* FIXME: The following check should pass:
14157 useless_type_conversion_p (const_cast <tree> (t),
14158 TREE_TYPE (TYPE_MIN_VALUE (t))
14159 but does not for C sizetypes in LTO. */
14160 }
14161
14162 /* Check various uses of TYPE_MAXVAL_RAW. */
14163 if (RECORD_OR_UNION_TYPE_P (t))
14164 {
14165 if (!TYPE_BINFO (t))
14166 ;
14167 else if (TREE_CODE (TYPE_BINFO (t)) != TREE_BINFO)
14168 {
14169 error ("%<TYPE_BINFO%> is not %<TREE_BINFO%>");
14170 debug_tree (TYPE_BINFO (t));
14171 error_found = true;
14172 }
14173 else if (TREE_TYPE (TYPE_BINFO (t)) != TYPE_MAIN_VARIANT (t))
14174 {
14175 error ("%<TYPE_BINFO%> type is not %<TYPE_MAIN_VARIANT%>");
14176 debug_tree (TREE_TYPE (TYPE_BINFO (t)));
14177 error_found = true;
14178 }
14179 }
14180 else if (FUNC_OR_METHOD_TYPE_P (t))
14181 {
14182 if (TYPE_METHOD_BASETYPE (t)
14183 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != RECORD_TYPE
14184 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != UNION_TYPE)
14185 {
14186 error ("%<TYPE_METHOD_BASETYPE%> is not record nor union");
14187 debug_tree (TYPE_METHOD_BASETYPE (t));
14188 error_found = true;
14189 }
14190 }
14191 else if (TREE_CODE (t) == OFFSET_TYPE)
14192 {
14193 if (TYPE_OFFSET_BASETYPE (t)
14194 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != RECORD_TYPE
14195 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != UNION_TYPE)
14196 {
14197 error ("%<TYPE_OFFSET_BASETYPE%> is not record nor union");
14198 debug_tree (TYPE_OFFSET_BASETYPE (t));
14199 error_found = true;
14200 }
14201 }
14202 else if (INTEGRAL_TYPE_P (t) || SCALAR_FLOAT_TYPE_P (t)
14203 || FIXED_POINT_TYPE_P (t))
14204 {
14205 /* FIXME: The following check should pass:
14206 useless_type_conversion_p (const_cast <tree> (t),
14207 TREE_TYPE (TYPE_MAX_VALUE (t))
14208 but does not for C sizetypes in LTO. */
14209 }
14210 else if (TREE_CODE (t) == ARRAY_TYPE)
14211 {
14212 if (TYPE_ARRAY_MAX_SIZE (t)
14213 && TREE_CODE (TYPE_ARRAY_MAX_SIZE (t)) != INTEGER_CST)
14214 {
14215 error ("%<TYPE_ARRAY_MAX_SIZE%> not %<INTEGER_CST%>");
14216 debug_tree (TYPE_ARRAY_MAX_SIZE (t));
14217 error_found = true;
14218 }
14219 }
14220 else if (TYPE_MAX_VALUE_RAW (t))
14221 {
14222 error ("%<TYPE_MAX_VALUE_RAW%> non-NULL");
14223 debug_tree (TYPE_MAX_VALUE_RAW (t));
14224 error_found = true;
14225 }
14226
14227 if (TYPE_LANG_SLOT_1 (t) && in_lto_p)
14228 {
14229 error ("%<TYPE_LANG_SLOT_1 (binfo)%> field is non-NULL");
14230 debug_tree (TYPE_LANG_SLOT_1 (t));
14231 error_found = true;
14232 }
14233
14234 /* Check various uses of TYPE_VALUES_RAW. */
14235 if (TREE_CODE (t) == ENUMERAL_TYPE)
14236 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
14237 {
14238 tree value = TREE_VALUE (l);
14239 tree name = TREE_PURPOSE (l);
14240
14241 /* C FE porduce INTEGER_CST of INTEGER_TYPE, while C++ FE uses
14242 CONST_DECL of ENUMERAL TYPE. */
14243 if (TREE_CODE (value) != INTEGER_CST && TREE_CODE (value) != CONST_DECL)
14244 {
14245 error ("enum value is not %<CONST_DECL%> or %<INTEGER_CST%>");
14246 debug_tree (value);
14247 debug_tree (name);
14248 error_found = true;
14249 }
14250 if (TREE_CODE (TREE_TYPE (value)) != INTEGER_TYPE
14251 && TREE_CODE (TREE_TYPE (value)) != BOOLEAN_TYPE
14252 && !useless_type_conversion_p (const_cast <tree> (t), TREE_TYPE (value)))
14253 {
14254 error ("enum value type is not %<INTEGER_TYPE%> nor convertible "
14255 "to the enum");
14256 debug_tree (value);
14257 debug_tree (name);
14258 error_found = true;
14259 }
14260 if (TREE_CODE (name) != IDENTIFIER_NODE)
14261 {
14262 error ("enum value name is not %<IDENTIFIER_NODE%>");
14263 debug_tree (value);
14264 debug_tree (name);
14265 error_found = true;
14266 }
14267 }
14268 else if (TREE_CODE (t) == ARRAY_TYPE)
14269 {
14270 if (TYPE_DOMAIN (t) && TREE_CODE (TYPE_DOMAIN (t)) != INTEGER_TYPE)
14271 {
14272 error ("array %<TYPE_DOMAIN%> is not integer type");
14273 debug_tree (TYPE_DOMAIN (t));
14274 error_found = true;
14275 }
14276 }
14277 else if (RECORD_OR_UNION_TYPE_P (t))
14278 {
14279 if (TYPE_FIELDS (t) && !COMPLETE_TYPE_P (t) && in_lto_p)
14280 {
14281 error ("%<TYPE_FIELDS%> defined in incomplete type");
14282 error_found = true;
14283 }
14284 for (tree fld = TYPE_FIELDS (t); fld; fld = TREE_CHAIN (fld))
14285 {
14286 /* TODO: verify properties of decls. */
14287 if (TREE_CODE (fld) == FIELD_DECL)
14288 ;
14289 else if (TREE_CODE (fld) == TYPE_DECL)
14290 ;
14291 else if (TREE_CODE (fld) == CONST_DECL)
14292 ;
14293 else if (VAR_P (fld))
14294 ;
14295 else if (TREE_CODE (fld) == TEMPLATE_DECL)
14296 ;
14297 else if (TREE_CODE (fld) == USING_DECL)
14298 ;
14299 else if (TREE_CODE (fld) == FUNCTION_DECL)
14300 ;
14301 else
14302 {
14303 error ("wrong tree in %<TYPE_FIELDS%> list");
14304 debug_tree (fld);
14305 error_found = true;
14306 }
14307 }
14308 }
14309 else if (TREE_CODE (t) == INTEGER_TYPE
14310 || TREE_CODE (t) == BOOLEAN_TYPE
14311 || TREE_CODE (t) == BITINT_TYPE
14312 || TREE_CODE (t) == OFFSET_TYPE
14313 || TREE_CODE (t) == REFERENCE_TYPE
14314 || TREE_CODE (t) == NULLPTR_TYPE
14315 || TREE_CODE (t) == POINTER_TYPE)
14316 {
14317 if (TYPE_CACHED_VALUES_P (t) != (TYPE_CACHED_VALUES (t) != NULL))
14318 {
14319 error ("%<TYPE_CACHED_VALUES_P%> is %i while %<TYPE_CACHED_VALUES%> "
14320 "is %p",
14321 TYPE_CACHED_VALUES_P (t), (void *)TYPE_CACHED_VALUES (t));
14322 error_found = true;
14323 }
14324 else if (TYPE_CACHED_VALUES_P (t) && TREE_CODE (TYPE_CACHED_VALUES (t)) != TREE_VEC)
14325 {
14326 error ("%<TYPE_CACHED_VALUES%> is not %<TREE_VEC%>");
14327 debug_tree (TYPE_CACHED_VALUES (t));
14328 error_found = true;
14329 }
14330 /* Verify just enough of cache to ensure that no one copied it to new type.
14331 All copying should go by copy_node that should clear it. */
14332 else if (TYPE_CACHED_VALUES_P (t))
14333 {
14334 int i;
14335 for (i = 0; i < TREE_VEC_LENGTH (TYPE_CACHED_VALUES (t)); i++)
14336 if (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)
14337 && TREE_TYPE (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)) != t)
14338 {
14339 error ("wrong %<TYPE_CACHED_VALUES%> entry");
14340 debug_tree (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i));
14341 error_found = true;
14342 break;
14343 }
14344 }
14345 }
14346 else if (FUNC_OR_METHOD_TYPE_P (t))
14347 for (tree l = TYPE_ARG_TYPES (t); l; l = TREE_CHAIN (l))
14348 {
14349 /* C++ FE uses TREE_PURPOSE to store initial values. */
14350 if (TREE_PURPOSE (l) && in_lto_p)
14351 {
14352 error ("%<TREE_PURPOSE%> is non-NULL in %<TYPE_ARG_TYPES%> list");
14353 debug_tree (l);
14354 error_found = true;
14355 }
14356 if (!TYPE_P (TREE_VALUE (l)))
14357 {
14358 error ("wrong entry in %<TYPE_ARG_TYPES%> list");
14359 debug_tree (l);
14360 error_found = true;
14361 }
14362 }
14363 else if (!is_lang_specific (t) && TYPE_VALUES_RAW (t))
14364 {
14365 error ("%<TYPE_VALUES_RAW%> field is non-NULL");
14366 debug_tree (TYPE_VALUES_RAW (t));
14367 error_found = true;
14368 }
14369 if (TREE_CODE (t) != INTEGER_TYPE
14370 && TREE_CODE (t) != BOOLEAN_TYPE
14371 && TREE_CODE (t) != BITINT_TYPE
14372 && TREE_CODE (t) != OFFSET_TYPE
14373 && TREE_CODE (t) != REFERENCE_TYPE
14374 && TREE_CODE (t) != NULLPTR_TYPE
14375 && TREE_CODE (t) != POINTER_TYPE
14376 && TYPE_CACHED_VALUES_P (t))
14377 {
14378 error ("%<TYPE_CACHED_VALUES_P%> is set while it should not be");
14379 error_found = true;
14380 }
14381
14382 /* ipa-devirt makes an assumption that TYPE_METHOD_BASETYPE is always
14383 TYPE_MAIN_VARIANT and it would be odd to add methods only to variatns
14384 of a type. */
14385 if (TREE_CODE (t) == METHOD_TYPE
14386 && TYPE_MAIN_VARIANT (TYPE_METHOD_BASETYPE (t)) != TYPE_METHOD_BASETYPE (t))
14387 {
14388 error ("%<TYPE_METHOD_BASETYPE%> is not main variant");
14389 error_found = true;
14390 }
14391
14392 if (error_found)
14393 {
14394 debug_tree (const_cast <tree> (t));
14395 internal_error ("%qs failed", __func__);
14396 }
14397}
14398
14399
14400/* Return 1 if ARG interpreted as signed in its precision is known to be
14401 always positive or 2 if ARG is known to be always negative, or 3 if
14402 ARG may be positive or negative. */
14403
14404int
14405get_range_pos_neg (tree arg)
14406{
14407 if (arg == error_mark_node)
14408 return 3;
14409
14410 int prec = TYPE_PRECISION (TREE_TYPE (arg));
14411 int cnt = 0;
14412 if (TREE_CODE (arg) == INTEGER_CST)
14413 {
14414 wide_int w = wi::sext (x: wi::to_wide (t: arg), offset: prec);
14415 if (wi::neg_p (x: w))
14416 return 2;
14417 else
14418 return 1;
14419 }
14420 while (CONVERT_EXPR_P (arg)
14421 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
14422 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg, 0))) <= prec)
14423 {
14424 arg = TREE_OPERAND (arg, 0);
14425 /* Narrower value zero extended into wider type
14426 will always result in positive values. */
14427 if (TYPE_UNSIGNED (TREE_TYPE (arg))
14428 && TYPE_PRECISION (TREE_TYPE (arg)) < prec)
14429 return 1;
14430 prec = TYPE_PRECISION (TREE_TYPE (arg));
14431 if (++cnt > 30)
14432 return 3;
14433 }
14434
14435 if (TREE_CODE (arg) != SSA_NAME)
14436 return 3;
14437 value_range r;
14438 while (!get_global_range_query ()->range_of_expr (r, expr: arg)
14439 || r.undefined_p () || r.varying_p ())
14440 {
14441 gimple *g = SSA_NAME_DEF_STMT (arg);
14442 if (is_gimple_assign (gs: g)
14443 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (g)))
14444 {
14445 tree t = gimple_assign_rhs1 (gs: g);
14446 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
14447 && TYPE_PRECISION (TREE_TYPE (t)) <= prec)
14448 {
14449 if (TYPE_UNSIGNED (TREE_TYPE (t))
14450 && TYPE_PRECISION (TREE_TYPE (t)) < prec)
14451 return 1;
14452 prec = TYPE_PRECISION (TREE_TYPE (t));
14453 arg = t;
14454 if (++cnt > 30)
14455 return 3;
14456 continue;
14457 }
14458 }
14459 return 3;
14460 }
14461 if (TYPE_UNSIGNED (TREE_TYPE (arg)))
14462 {
14463 /* For unsigned values, the "positive" range comes
14464 below the "negative" range. */
14465 if (!wi::neg_p (x: wi::sext (x: r.upper_bound (), offset: prec), sgn: SIGNED))
14466 return 1;
14467 if (wi::neg_p (x: wi::sext (x: r.lower_bound (), offset: prec), sgn: SIGNED))
14468 return 2;
14469 }
14470 else
14471 {
14472 if (!wi::neg_p (x: wi::sext (x: r.lower_bound (), offset: prec), sgn: SIGNED))
14473 return 1;
14474 if (wi::neg_p (x: wi::sext (x: r.upper_bound (), offset: prec), sgn: SIGNED))
14475 return 2;
14476 }
14477 return 3;
14478}
14479
14480
14481
14482
14483/* Return true if ARG is marked with the nonnull attribute in the
14484 current function signature. */
14485
14486bool
14487nonnull_arg_p (const_tree arg)
14488{
14489 tree t, attrs, fntype;
14490 unsigned HOST_WIDE_INT arg_num;
14491
14492 gcc_assert (TREE_CODE (arg) == PARM_DECL
14493 && (POINTER_TYPE_P (TREE_TYPE (arg))
14494 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE));
14495
14496 /* The static chain decl is always non null. */
14497 if (arg == cfun->static_chain_decl)
14498 return true;
14499
14500 /* THIS argument of method is always non-NULL. */
14501 if (TREE_CODE (TREE_TYPE (cfun->decl)) == METHOD_TYPE
14502 && arg == DECL_ARGUMENTS (cfun->decl)
14503 && flag_delete_null_pointer_checks)
14504 return true;
14505
14506 /* Values passed by reference are always non-NULL. */
14507 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
14508 && flag_delete_null_pointer_checks)
14509 return true;
14510
14511 fntype = TREE_TYPE (cfun->decl);
14512 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
14513 {
14514 attrs = lookup_attribute (attr_name: "nonnull", list: attrs);
14515
14516 /* If "nonnull" wasn't specified, we know nothing about the argument. */
14517 if (attrs == NULL_TREE)
14518 return false;
14519
14520 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
14521 if (TREE_VALUE (attrs) == NULL_TREE)
14522 return true;
14523
14524 /* Get the position number for ARG in the function signature. */
14525 for (arg_num = 1, t = DECL_ARGUMENTS (cfun->decl);
14526 t;
14527 t = DECL_CHAIN (t), arg_num++)
14528 {
14529 if (t == arg)
14530 break;
14531 }
14532
14533 gcc_assert (t == arg);
14534
14535 /* Now see if ARG_NUM is mentioned in the nonnull list. */
14536 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
14537 {
14538 if (compare_tree_int (TREE_VALUE (t), u: arg_num) == 0)
14539 return true;
14540 }
14541 }
14542
14543 return false;
14544}
14545
14546/* Combine LOC and BLOCK to a combined adhoc loc, retaining any range
14547 information. */
14548
14549location_t
14550set_block (location_t loc, tree block)
14551{
14552 location_t pure_loc = get_pure_location (loc);
14553 source_range src_range = get_range_from_loc (set: line_table, loc);
14554 unsigned discriminator = get_discriminator_from_loc (set: line_table, loc);
14555 return line_table->get_or_create_combined_loc (locus: pure_loc, src_range, data: block,
14556 discriminator);
14557}
14558
14559location_t
14560set_source_range (tree expr, location_t start, location_t finish)
14561{
14562 source_range src_range;
14563 src_range.m_start = start;
14564 src_range.m_finish = finish;
14565 return set_source_range (expr, src_range);
14566}
14567
14568location_t
14569set_source_range (tree expr, source_range src_range)
14570{
14571 if (!EXPR_P (expr))
14572 return UNKNOWN_LOCATION;
14573
14574 location_t expr_location = EXPR_LOCATION (expr);
14575 location_t pure_loc = get_pure_location (loc: expr_location);
14576 unsigned discriminator = get_discriminator_from_loc (expr_location);
14577 location_t adhoc = line_table->get_or_create_combined_loc (locus: pure_loc,
14578 src_range,
14579 data: nullptr,
14580 discriminator);
14581 SET_EXPR_LOCATION (expr, adhoc);
14582 return adhoc;
14583}
14584
14585/* Return EXPR, potentially wrapped with a node expression LOC,
14586 if !CAN_HAVE_LOCATION_P (expr).
14587
14588 NON_LVALUE_EXPR is used for wrapping constants, apart from STRING_CST.
14589 VIEW_CONVERT_EXPR is used for wrapping non-constants and STRING_CST.
14590
14591 Wrapper nodes can be identified using location_wrapper_p. */
14592
14593tree
14594maybe_wrap_with_location (tree expr, location_t loc)
14595{
14596 if (expr == NULL)
14597 return NULL;
14598 if (loc == UNKNOWN_LOCATION)
14599 return expr;
14600 if (CAN_HAVE_LOCATION_P (expr))
14601 return expr;
14602 /* We should only be adding wrappers for constants and for decls,
14603 or for some exceptional tree nodes (e.g. BASELINK in the C++ FE). */
14604 gcc_assert (CONSTANT_CLASS_P (expr)
14605 || DECL_P (expr)
14606 || EXCEPTIONAL_CLASS_P (expr));
14607
14608 /* For now, don't add wrappers to exceptional tree nodes, to minimize
14609 any impact of the wrapper nodes. */
14610 if (EXCEPTIONAL_CLASS_P (expr) || error_operand_p (t: expr))
14611 return expr;
14612
14613 /* Compiler-generated temporary variables don't need a wrapper. */
14614 if (DECL_P (expr) && DECL_ARTIFICIAL (expr) && DECL_IGNORED_P (expr))
14615 return expr;
14616
14617 /* If any auto_suppress_location_wrappers are active, don't create
14618 wrappers. */
14619 if (suppress_location_wrappers > 0)
14620 return expr;
14621
14622 tree_code code
14623 = (((CONSTANT_CLASS_P (expr) && TREE_CODE (expr) != STRING_CST)
14624 || (TREE_CODE (expr) == CONST_DECL && !TREE_STATIC (expr)))
14625 ? NON_LVALUE_EXPR : VIEW_CONVERT_EXPR);
14626 tree wrapper = build1_loc (loc, code, TREE_TYPE (expr), arg1: expr);
14627 /* Mark this node as being a wrapper. */
14628 EXPR_LOCATION_WRAPPER_P (wrapper) = 1;
14629 return wrapper;
14630}
14631
14632int suppress_location_wrappers;
14633
14634/* Return the name of combined function FN, for debugging purposes. */
14635
14636const char *
14637combined_fn_name (combined_fn fn)
14638{
14639 if (builtin_fn_p (code: fn))
14640 {
14641 tree fndecl = builtin_decl_explicit (fncode: as_builtin_fn (code: fn));
14642 return IDENTIFIER_POINTER (DECL_NAME (fndecl));
14643 }
14644 else
14645 return internal_fn_name (fn: as_internal_fn (code: fn));
14646}
14647
14648/* Return a bitmap with a bit set corresponding to each argument in
14649 a function call type FNTYPE declared with attribute nonnull,
14650 or null if none of the function's argument are nonnull. The caller
14651 must free the bitmap. */
14652
14653bitmap
14654get_nonnull_args (const_tree fntype)
14655{
14656 if (fntype == NULL_TREE)
14657 return NULL;
14658
14659 bitmap argmap = NULL;
14660 if (TREE_CODE (fntype) == METHOD_TYPE)
14661 {
14662 /* The this pointer in C++ non-static member functions is
14663 implicitly nonnull whether or not it's declared as such. */
14664 argmap = BITMAP_ALLOC (NULL);
14665 bitmap_set_bit (argmap, 0);
14666 }
14667
14668 tree attrs = TYPE_ATTRIBUTES (fntype);
14669 if (!attrs)
14670 return argmap;
14671
14672 /* A function declaration can specify multiple attribute nonnull,
14673 each with zero or more arguments. The loop below creates a bitmap
14674 representing a union of all the arguments. An empty (but non-null)
14675 bitmap means that all arguments have been declaraed nonnull. */
14676 for ( ; attrs; attrs = TREE_CHAIN (attrs))
14677 {
14678 attrs = lookup_attribute (attr_name: "nonnull", list: attrs);
14679 if (!attrs)
14680 break;
14681
14682 if (!argmap)
14683 argmap = BITMAP_ALLOC (NULL);
14684
14685 if (!TREE_VALUE (attrs))
14686 {
14687 /* Clear the bitmap in case a previous attribute nonnull
14688 set it and this one overrides it for all arguments. */
14689 bitmap_clear (argmap);
14690 return argmap;
14691 }
14692
14693 /* Iterate over the indices of the format arguments declared nonnull
14694 and set a bit for each. */
14695 for (tree idx = TREE_VALUE (attrs); idx; idx = TREE_CHAIN (idx))
14696 {
14697 unsigned int val = TREE_INT_CST_LOW (TREE_VALUE (idx)) - 1;
14698 bitmap_set_bit (argmap, val);
14699 }
14700 }
14701
14702 return argmap;
14703}
14704
14705/* Returns true if TYPE is a type where it and all of its subobjects
14706 (recursively) are of structure, union, or array type. */
14707
14708bool
14709is_empty_type (const_tree type)
14710{
14711 if (RECORD_OR_UNION_TYPE_P (type))
14712 {
14713 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
14714 if (TREE_CODE (field) == FIELD_DECL
14715 && !DECL_PADDING_P (field)
14716 && !is_empty_type (TREE_TYPE (field)))
14717 return false;
14718 return true;
14719 }
14720 else if (TREE_CODE (type) == ARRAY_TYPE)
14721 return (integer_minus_onep (expr: array_type_nelts (type))
14722 || TYPE_DOMAIN (type) == NULL_TREE
14723 || is_empty_type (TREE_TYPE (type)));
14724 return false;
14725}
14726
14727/* Implement TARGET_EMPTY_RECORD_P. Return true if TYPE is an empty type
14728 that shouldn't be passed via stack. */
14729
14730bool
14731default_is_empty_record (const_tree type)
14732{
14733 if (!abi_version_at_least (12))
14734 return false;
14735
14736 if (type == error_mark_node)
14737 return false;
14738
14739 if (TREE_ADDRESSABLE (type))
14740 return false;
14741
14742 return is_empty_type (TYPE_MAIN_VARIANT (type));
14743}
14744
14745/* Determine whether TYPE is a structure with a flexible array member,
14746 or a union containing such a structure (possibly recursively). */
14747
14748bool
14749flexible_array_type_p (const_tree type)
14750{
14751 tree x, last;
14752 switch (TREE_CODE (type))
14753 {
14754 case RECORD_TYPE:
14755 last = NULL_TREE;
14756 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
14757 if (TREE_CODE (x) == FIELD_DECL)
14758 last = x;
14759 if (last == NULL_TREE)
14760 return false;
14761 if (TREE_CODE (TREE_TYPE (last)) == ARRAY_TYPE
14762 && TYPE_SIZE (TREE_TYPE (last)) == NULL_TREE
14763 && TYPE_DOMAIN (TREE_TYPE (last)) != NULL_TREE
14764 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (last))) == NULL_TREE)
14765 return true;
14766 return false;
14767 case UNION_TYPE:
14768 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
14769 {
14770 if (TREE_CODE (x) == FIELD_DECL
14771 && flexible_array_type_p (TREE_TYPE (x)))
14772 return true;
14773 }
14774 return false;
14775 default:
14776 return false;
14777 }
14778}
14779
14780/* Like int_size_in_bytes, but handle empty records specially. */
14781
14782HOST_WIDE_INT
14783arg_int_size_in_bytes (const_tree type)
14784{
14785 return TYPE_EMPTY_P (type) ? 0 : int_size_in_bytes (type);
14786}
14787
14788/* Like size_in_bytes, but handle empty records specially. */
14789
14790tree
14791arg_size_in_bytes (const_tree type)
14792{
14793 return TYPE_EMPTY_P (type) ? size_zero_node : size_in_bytes (t: type);
14794}
14795
14796/* Return true if an expression with CODE has to have the same result type as
14797 its first operand. */
14798
14799bool
14800expr_type_first_operand_type_p (tree_code code)
14801{
14802 switch (code)
14803 {
14804 case NEGATE_EXPR:
14805 case ABS_EXPR:
14806 case BIT_NOT_EXPR:
14807 case PAREN_EXPR:
14808 case CONJ_EXPR:
14809
14810 case PLUS_EXPR:
14811 case MINUS_EXPR:
14812 case MULT_EXPR:
14813 case TRUNC_DIV_EXPR:
14814 case CEIL_DIV_EXPR:
14815 case FLOOR_DIV_EXPR:
14816 case ROUND_DIV_EXPR:
14817 case TRUNC_MOD_EXPR:
14818 case CEIL_MOD_EXPR:
14819 case FLOOR_MOD_EXPR:
14820 case ROUND_MOD_EXPR:
14821 case RDIV_EXPR:
14822 case EXACT_DIV_EXPR:
14823 case MIN_EXPR:
14824 case MAX_EXPR:
14825 case BIT_IOR_EXPR:
14826 case BIT_XOR_EXPR:
14827 case BIT_AND_EXPR:
14828
14829 case LSHIFT_EXPR:
14830 case RSHIFT_EXPR:
14831 case LROTATE_EXPR:
14832 case RROTATE_EXPR:
14833 return true;
14834
14835 default:
14836 return false;
14837 }
14838}
14839
14840/* Return a typenode for the "standard" C type with a given name. */
14841tree
14842get_typenode_from_name (const char *name)
14843{
14844 if (name == NULL || *name == '\0')
14845 return NULL_TREE;
14846
14847 if (strcmp (s1: name, s2: "char") == 0)
14848 return char_type_node;
14849 if (strcmp (s1: name, s2: "unsigned char") == 0)
14850 return unsigned_char_type_node;
14851 if (strcmp (s1: name, s2: "signed char") == 0)
14852 return signed_char_type_node;
14853
14854 if (strcmp (s1: name, s2: "short int") == 0)
14855 return short_integer_type_node;
14856 if (strcmp (s1: name, s2: "short unsigned int") == 0)
14857 return short_unsigned_type_node;
14858
14859 if (strcmp (s1: name, s2: "int") == 0)
14860 return integer_type_node;
14861 if (strcmp (s1: name, s2: "unsigned int") == 0)
14862 return unsigned_type_node;
14863
14864 if (strcmp (s1: name, s2: "long int") == 0)
14865 return long_integer_type_node;
14866 if (strcmp (s1: name, s2: "long unsigned int") == 0)
14867 return long_unsigned_type_node;
14868
14869 if (strcmp (s1: name, s2: "long long int") == 0)
14870 return long_long_integer_type_node;
14871 if (strcmp (s1: name, s2: "long long unsigned int") == 0)
14872 return long_long_unsigned_type_node;
14873
14874 gcc_unreachable ();
14875}
14876
14877/* List of pointer types used to declare builtins before we have seen their
14878 real declaration.
14879
14880 Keep the size up to date in tree.h ! */
14881const builtin_structptr_type builtin_structptr_types[6] =
14882{
14883 { fileptr_type_node, ptr_type_node, .str: "FILE" },
14884 { const_tm_ptr_type_node, const_ptr_type_node, .str: "tm" },
14885 { fenv_t_ptr_type_node, ptr_type_node, .str: "fenv_t" },
14886 { const_fenv_t_ptr_type_node, const_ptr_type_node, .str: "fenv_t" },
14887 { fexcept_t_ptr_type_node, ptr_type_node, .str: "fexcept_t" },
14888 { const_fexcept_t_ptr_type_node, const_ptr_type_node, .str: "fexcept_t" }
14889};
14890
14891/* Return the maximum object size. */
14892
14893tree
14894max_object_size (void)
14895{
14896 /* To do: Make this a configurable parameter. */
14897 return TYPE_MAX_VALUE (ptrdiff_type_node);
14898}
14899
14900/* A wrapper around TARGET_VERIFY_TYPE_CONTEXT that makes the silent_p
14901 parameter default to false and that weeds out error_mark_node. */
14902
14903bool
14904verify_type_context (location_t loc, type_context_kind context,
14905 const_tree type, bool silent_p)
14906{
14907 if (type == error_mark_node)
14908 return true;
14909
14910 gcc_assert (TYPE_P (type));
14911 return (!targetm.verify_type_context
14912 || targetm.verify_type_context (loc, context, type, silent_p));
14913}
14914
14915/* Return true if NEW_ASM and DELETE_ASM name a valid pair of new and
14916 delete operators. Return false if they may or may not name such
14917 a pair and, when nonnull, set *PCERTAIN to true if they certainly
14918 do not. */
14919
14920bool
14921valid_new_delete_pair_p (tree new_asm, tree delete_asm,
14922 bool *pcertain /* = NULL */)
14923{
14924 bool certain;
14925 if (!pcertain)
14926 pcertain = &certain;
14927
14928 const char *new_name = IDENTIFIER_POINTER (new_asm);
14929 const char *delete_name = IDENTIFIER_POINTER (delete_asm);
14930 unsigned int new_len = IDENTIFIER_LENGTH (new_asm);
14931 unsigned int delete_len = IDENTIFIER_LENGTH (delete_asm);
14932
14933 /* The following failures are due to invalid names so they're not
14934 considered certain mismatches. */
14935 *pcertain = false;
14936
14937 if (new_len < 5 || delete_len < 6)
14938 return false;
14939 if (new_name[0] == '_')
14940 ++new_name, --new_len;
14941 if (new_name[0] == '_')
14942 ++new_name, --new_len;
14943 if (delete_name[0] == '_')
14944 ++delete_name, --delete_len;
14945 if (delete_name[0] == '_')
14946 ++delete_name, --delete_len;
14947 if (new_len < 4 || delete_len < 5)
14948 return false;
14949
14950 /* The following failures are due to names of user-defined operators
14951 so they're also not considered certain mismatches. */
14952
14953 /* *_len is now just the length after initial underscores. */
14954 if (new_name[0] != 'Z' || new_name[1] != 'n')
14955 return false;
14956 if (delete_name[0] != 'Z' || delete_name[1] != 'd')
14957 return false;
14958
14959 /* The following failures are certain mismatches. */
14960 *pcertain = true;
14961
14962 /* _Znw must match _Zdl, _Zna must match _Zda. */
14963 if ((new_name[2] != 'w' || delete_name[2] != 'l')
14964 && (new_name[2] != 'a' || delete_name[2] != 'a'))
14965 return false;
14966 /* 'j', 'm' and 'y' correspond to size_t. */
14967 if (new_name[3] != 'j' && new_name[3] != 'm' && new_name[3] != 'y')
14968 return false;
14969 if (delete_name[3] != 'P' || delete_name[4] != 'v')
14970 return false;
14971 if (new_len == 4
14972 || (new_len == 18 && !memcmp (s1: new_name + 4, s2: "RKSt9nothrow_t", n: 14)))
14973 {
14974 /* _ZnXY or _ZnXYRKSt9nothrow_t matches
14975 _ZdXPv, _ZdXPvY and _ZdXPvRKSt9nothrow_t. */
14976 if (delete_len == 5)
14977 return true;
14978 if (delete_len == 6 && delete_name[5] == new_name[3])
14979 return true;
14980 if (delete_len == 19 && !memcmp (s1: delete_name + 5, s2: "RKSt9nothrow_t", n: 14))
14981 return true;
14982 }
14983 else if ((new_len == 19 && !memcmp (s1: new_name + 4, s2: "St11align_val_t", n: 15))
14984 || (new_len == 33
14985 && !memcmp (s1: new_name + 4, s2: "St11align_val_tRKSt9nothrow_t", n: 29)))
14986 {
14987 /* _ZnXYSt11align_val_t or _ZnXYSt11align_val_tRKSt9nothrow_t matches
14988 _ZdXPvSt11align_val_t or _ZdXPvYSt11align_val_t or or
14989 _ZdXPvSt11align_val_tRKSt9nothrow_t. */
14990 if (delete_len == 20 && !memcmp (s1: delete_name + 5, s2: "St11align_val_t", n: 15))
14991 return true;
14992 if (delete_len == 21
14993 && delete_name[5] == new_name[3]
14994 && !memcmp (s1: delete_name + 6, s2: "St11align_val_t", n: 15))
14995 return true;
14996 if (delete_len == 34
14997 && !memcmp (s1: delete_name + 5, s2: "St11align_val_tRKSt9nothrow_t", n: 29))
14998 return true;
14999 }
15000
15001 /* The negative result is conservative. */
15002 *pcertain = false;
15003 return false;
15004}
15005
15006/* Return the zero-based number corresponding to the argument being
15007 deallocated if FNDECL is a deallocation function or an out-of-bounds
15008 value if it isn't. */
15009
15010unsigned
15011fndecl_dealloc_argno (tree fndecl)
15012{
15013 /* A call to operator delete isn't recognized as one to a built-in. */
15014 if (DECL_IS_OPERATOR_DELETE_P (fndecl))
15015 {
15016 if (DECL_IS_REPLACEABLE_OPERATOR (fndecl))
15017 return 0;
15018
15019 /* Avoid placement delete that's not been inlined. */
15020 tree fname = DECL_ASSEMBLER_NAME (fndecl);
15021 if (id_equal (id: fname, str: "_ZdlPvS_") // ordinary form
15022 || id_equal (id: fname, str: "_ZdaPvS_")) // array form
15023 return UINT_MAX;
15024 return 0;
15025 }
15026
15027 /* TODO: Handle user-defined functions with attribute malloc? Handle
15028 known non-built-ins like fopen? */
15029 if (fndecl_built_in_p (node: fndecl, klass: BUILT_IN_NORMAL))
15030 {
15031 switch (DECL_FUNCTION_CODE (decl: fndecl))
15032 {
15033 case BUILT_IN_FREE:
15034 case BUILT_IN_REALLOC:
15035 case BUILT_IN_GOMP_FREE:
15036 case BUILT_IN_GOMP_REALLOC:
15037 return 0;
15038 default:
15039 break;
15040 }
15041 return UINT_MAX;
15042 }
15043
15044 tree attrs = DECL_ATTRIBUTES (fndecl);
15045 if (!attrs)
15046 return UINT_MAX;
15047
15048 for (tree atfree = attrs;
15049 (atfree = lookup_attribute (attr_name: "*dealloc", list: atfree));
15050 atfree = TREE_CHAIN (atfree))
15051 {
15052 tree alloc = TREE_VALUE (atfree);
15053 if (!alloc)
15054 continue;
15055
15056 tree pos = TREE_CHAIN (alloc);
15057 if (!pos)
15058 return 0;
15059
15060 pos = TREE_VALUE (pos);
15061 return TREE_INT_CST_LOW (pos) - 1;
15062 }
15063
15064 return UINT_MAX;
15065}
15066
15067/* If EXPR refers to a character array or pointer declared attribute
15068 nonstring, return a decl for that array or pointer and set *REF
15069 to the referenced enclosing object or pointer. Otherwise return
15070 null. */
15071
15072tree
15073get_attr_nonstring_decl (tree expr, tree *ref)
15074{
15075 tree decl = expr;
15076 tree var = NULL_TREE;
15077 if (TREE_CODE (decl) == SSA_NAME)
15078 {
15079 gimple *def = SSA_NAME_DEF_STMT (decl);
15080
15081 if (is_gimple_assign (gs: def))
15082 {
15083 tree_code code = gimple_assign_rhs_code (gs: def);
15084 if (code == ADDR_EXPR
15085 || code == COMPONENT_REF
15086 || code == VAR_DECL)
15087 decl = gimple_assign_rhs1 (gs: def);
15088 }
15089 else
15090 var = SSA_NAME_VAR (decl);
15091 }
15092
15093 if (TREE_CODE (decl) == ADDR_EXPR)
15094 decl = TREE_OPERAND (decl, 0);
15095
15096 /* To simplify calling code, store the referenced DECL regardless of
15097 the attribute determined below, but avoid storing the SSA_NAME_VAR
15098 obtained above (it's not useful for dataflow purposes). */
15099 if (ref)
15100 *ref = decl;
15101
15102 /* Use the SSA_NAME_VAR that was determined above to see if it's
15103 declared nonstring. Otherwise drill down into the referenced
15104 DECL. */
15105 if (var)
15106 decl = var;
15107 else if (TREE_CODE (decl) == ARRAY_REF)
15108 decl = TREE_OPERAND (decl, 0);
15109 else if (TREE_CODE (decl) == COMPONENT_REF)
15110 decl = TREE_OPERAND (decl, 1);
15111 else if (TREE_CODE (decl) == MEM_REF)
15112 return get_attr_nonstring_decl (TREE_OPERAND (decl, 0), ref);
15113
15114 if (DECL_P (decl)
15115 && lookup_attribute (attr_name: "nonstring", DECL_ATTRIBUTES (decl)))
15116 return decl;
15117
15118 return NULL_TREE;
15119}
15120
15121/* Return length of attribute names string,
15122 if arglist chain > 1, -1 otherwise. */
15123
15124int
15125get_target_clone_attr_len (tree arglist)
15126{
15127 tree arg;
15128 int str_len_sum = 0;
15129 int argnum = 0;
15130
15131 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
15132 {
15133 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
15134 size_t len = strlen (s: str);
15135 str_len_sum += len + 1;
15136 for (const char *p = strchr (s: str, c: ','); p; p = strchr (s: p + 1, c: ','))
15137 argnum++;
15138 argnum++;
15139 }
15140 if (argnum <= 1)
15141 return -1;
15142 return str_len_sum;
15143}
15144
15145void
15146tree_cc_finalize (void)
15147{
15148 clear_nonstandard_integer_type_cache ();
15149 vec_free (v&: bitint_type_cache);
15150}
15151
15152#if CHECKING_P
15153
15154namespace selftest {
15155
15156/* Selftests for tree. */
15157
15158/* Verify that integer constants are sane. */
15159
15160static void
15161test_integer_constants ()
15162{
15163 ASSERT_TRUE (integer_type_node != NULL);
15164 ASSERT_TRUE (build_int_cst (integer_type_node, 0) != NULL);
15165
15166 tree type = integer_type_node;
15167
15168 tree zero = build_zero_cst (type);
15169 ASSERT_EQ (INTEGER_CST, TREE_CODE (zero));
15170 ASSERT_EQ (type, TREE_TYPE (zero));
15171
15172 tree one = build_int_cst (type, cst: 1);
15173 ASSERT_EQ (INTEGER_CST, TREE_CODE (one));
15174 ASSERT_EQ (type, TREE_TYPE (zero));
15175}
15176
15177/* Verify identifiers. */
15178
15179static void
15180test_identifiers ()
15181{
15182 tree identifier = get_identifier ("foo");
15183 ASSERT_EQ (3, IDENTIFIER_LENGTH (identifier));
15184 ASSERT_STREQ ("foo", IDENTIFIER_POINTER (identifier));
15185}
15186
15187/* Verify LABEL_DECL. */
15188
15189static void
15190test_labels ()
15191{
15192 tree identifier = get_identifier ("err");
15193 tree label_decl = build_decl (UNKNOWN_LOCATION, code: LABEL_DECL,
15194 name: identifier, void_type_node);
15195 ASSERT_EQ (-1, LABEL_DECL_UID (label_decl));
15196 ASSERT_FALSE (FORCED_LABEL (label_decl));
15197}
15198
15199/* Return a new VECTOR_CST node whose type is TYPE and whose values
15200 are given by VALS. */
15201
15202static tree
15203build_vector (tree type, const vec<tree> &vals MEM_STAT_DECL)
15204{
15205 gcc_assert (known_eq (vals.length (), TYPE_VECTOR_SUBPARTS (type)));
15206 tree_vector_builder builder (type, vals.length (), 1);
15207 builder.splice (src: vals);
15208 return builder.build ();
15209}
15210
15211/* Check that VECTOR_CST ACTUAL contains the elements in EXPECTED. */
15212
15213static void
15214check_vector_cst (const vec<tree> &expected, tree actual)
15215{
15216 ASSERT_KNOWN_EQ (expected.length (),
15217 TYPE_VECTOR_SUBPARTS (TREE_TYPE (actual)));
15218 for (unsigned int i = 0; i < expected.length (); ++i)
15219 ASSERT_EQ (wi::to_wide (expected[i]),
15220 wi::to_wide (vector_cst_elt (actual, i)));
15221}
15222
15223/* Check that VECTOR_CST ACTUAL contains NPATTERNS duplicated elements,
15224 and that its elements match EXPECTED. */
15225
15226static void
15227check_vector_cst_duplicate (const vec<tree> &expected, tree actual,
15228 unsigned int npatterns)
15229{
15230 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
15231 ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (actual));
15232 ASSERT_EQ (npatterns, vector_cst_encoded_nelts (actual));
15233 ASSERT_TRUE (VECTOR_CST_DUPLICATE_P (actual));
15234 ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
15235 check_vector_cst (expected, actual);
15236}
15237
15238/* Check that VECTOR_CST ACTUAL contains NPATTERNS foreground elements
15239 and NPATTERNS background elements, and that its elements match
15240 EXPECTED. */
15241
15242static void
15243check_vector_cst_fill (const vec<tree> &expected, tree actual,
15244 unsigned int npatterns)
15245{
15246 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
15247 ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (actual));
15248 ASSERT_EQ (2 * npatterns, vector_cst_encoded_nelts (actual));
15249 ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
15250 ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
15251 check_vector_cst (expected, actual);
15252}
15253
15254/* Check that VECTOR_CST ACTUAL contains NPATTERNS stepped patterns,
15255 and that its elements match EXPECTED. */
15256
15257static void
15258check_vector_cst_stepped (const vec<tree> &expected, tree actual,
15259 unsigned int npatterns)
15260{
15261 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
15262 ASSERT_EQ (3, VECTOR_CST_NELTS_PER_PATTERN (actual));
15263 ASSERT_EQ (3 * npatterns, vector_cst_encoded_nelts (actual));
15264 ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
15265 ASSERT_TRUE (VECTOR_CST_STEPPED_P (actual));
15266 check_vector_cst (expected, actual);
15267}
15268
15269/* Test the creation of VECTOR_CSTs. */
15270
15271static void
15272test_vector_cst_patterns (ALONE_CXX_MEM_STAT_INFO)
15273{
15274 auto_vec<tree, 8> elements (8);
15275 elements.quick_grow (len: 8);
15276 tree element_type = build_nonstandard_integer_type (precision: 16, unsignedp: true);
15277 tree vector_type = build_vector_type (innertype: element_type, nunits: 8);
15278
15279 /* Test a simple linear series with a base of 0 and a step of 1:
15280 { 0, 1, 2, 3, 4, 5, 6, 7 }. */
15281 for (unsigned int i = 0; i < 8; ++i)
15282 elements[i] = build_int_cst (type: element_type, cst: i);
15283 tree vector = build_vector (type: vector_type, vals: elements PASS_MEM_STAT);
15284 check_vector_cst_stepped (expected: elements, actual: vector, npatterns: 1);
15285
15286 /* Try the same with the first element replaced by 100:
15287 { 100, 1, 2, 3, 4, 5, 6, 7 }. */
15288 elements[0] = build_int_cst (type: element_type, cst: 100);
15289 vector = build_vector (type: vector_type, vals: elements PASS_MEM_STAT);
15290 check_vector_cst_stepped (expected: elements, actual: vector, npatterns: 1);
15291
15292 /* Try a series that wraps around.
15293 { 100, 65531, 65532, 65533, 65534, 65535, 0, 1 }. */
15294 for (unsigned int i = 1; i < 8; ++i)
15295 elements[i] = build_int_cst (type: element_type, cst: (65530 + i) & 0xffff);
15296 vector = build_vector (type: vector_type, vals: elements PASS_MEM_STAT);
15297 check_vector_cst_stepped (expected: elements, actual: vector, npatterns: 1);
15298
15299 /* Try a downward series:
15300 { 100, 79, 78, 77, 76, 75, 75, 73 }. */
15301 for (unsigned int i = 1; i < 8; ++i)
15302 elements[i] = build_int_cst (type: element_type, cst: 80 - i);
15303 vector = build_vector (type: vector_type, vals: elements PASS_MEM_STAT);
15304 check_vector_cst_stepped (expected: elements, actual: vector, npatterns: 1);
15305
15306 /* Try two interleaved series with different bases and steps:
15307 { 100, 53, 66, 206, 62, 212, 58, 218 }. */
15308 elements[1] = build_int_cst (type: element_type, cst: 53);
15309 for (unsigned int i = 2; i < 8; i += 2)
15310 {
15311 elements[i] = build_int_cst (type: element_type, cst: 70 - i * 2);
15312 elements[i + 1] = build_int_cst (type: element_type, cst: 200 + i * 3);
15313 }
15314 vector = build_vector (type: vector_type, vals: elements PASS_MEM_STAT);
15315 check_vector_cst_stepped (expected: elements, actual: vector, npatterns: 2);
15316
15317 /* Try a duplicated value:
15318 { 100, 100, 100, 100, 100, 100, 100, 100 }. */
15319 for (unsigned int i = 1; i < 8; ++i)
15320 elements[i] = elements[0];
15321 vector = build_vector (type: vector_type, vals: elements PASS_MEM_STAT);
15322 check_vector_cst_duplicate (expected: elements, actual: vector, npatterns: 1);
15323
15324 /* Try an interleaved duplicated value:
15325 { 100, 55, 100, 55, 100, 55, 100, 55 }. */
15326 elements[1] = build_int_cst (type: element_type, cst: 55);
15327 for (unsigned int i = 2; i < 8; ++i)
15328 elements[i] = elements[i - 2];
15329 vector = build_vector (type: vector_type, vals: elements PASS_MEM_STAT);
15330 check_vector_cst_duplicate (expected: elements, actual: vector, npatterns: 2);
15331
15332 /* Try a duplicated value with 2 exceptions
15333 { 41, 97, 100, 55, 100, 55, 100, 55 }. */
15334 elements[0] = build_int_cst (type: element_type, cst: 41);
15335 elements[1] = build_int_cst (type: element_type, cst: 97);
15336 vector = build_vector (type: vector_type, vals: elements PASS_MEM_STAT);
15337 check_vector_cst_fill (expected: elements, actual: vector, npatterns: 2);
15338
15339 /* Try with and without a step
15340 { 41, 97, 100, 21, 100, 35, 100, 49 }. */
15341 for (unsigned int i = 3; i < 8; i += 2)
15342 elements[i] = build_int_cst (type: element_type, cst: i * 7);
15343 vector = build_vector (type: vector_type, vals: elements PASS_MEM_STAT);
15344 check_vector_cst_stepped (expected: elements, actual: vector, npatterns: 2);
15345
15346 /* Try a fully-general constant:
15347 { 41, 97, 100, 21, 100, 9990, 100, 49 }. */
15348 elements[5] = build_int_cst (type: element_type, cst: 9990);
15349 vector = build_vector (type: vector_type, vals: elements PASS_MEM_STAT);
15350 check_vector_cst_fill (expected: elements, actual: vector, npatterns: 4);
15351}
15352
15353/* Verify that STRIP_NOPS (NODE) is EXPECTED.
15354 Helper function for test_location_wrappers, to deal with STRIP_NOPS
15355 modifying its argument in-place. */
15356
15357static void
15358check_strip_nops (tree node, tree expected)
15359{
15360 STRIP_NOPS (node);
15361 ASSERT_EQ (expected, node);
15362}
15363
15364/* Verify location wrappers. */
15365
15366static void
15367test_location_wrappers ()
15368{
15369 location_t loc = BUILTINS_LOCATION;
15370
15371 ASSERT_EQ (NULL_TREE, maybe_wrap_with_location (NULL_TREE, loc));
15372
15373 /* Wrapping a constant. */
15374 tree int_cst = build_int_cst (integer_type_node, cst: 42);
15375 ASSERT_FALSE (CAN_HAVE_LOCATION_P (int_cst));
15376 ASSERT_FALSE (location_wrapper_p (int_cst));
15377
15378 tree wrapped_int_cst = maybe_wrap_with_location (expr: int_cst, loc);
15379 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
15380 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_int_cst));
15381 ASSERT_EQ (int_cst, tree_strip_any_location_wrapper (wrapped_int_cst));
15382
15383 /* We shouldn't add wrapper nodes for UNKNOWN_LOCATION. */
15384 ASSERT_EQ (int_cst, maybe_wrap_with_location (int_cst, UNKNOWN_LOCATION));
15385
15386 /* We shouldn't add wrapper nodes for nodes that CAN_HAVE_LOCATION_P. */
15387 tree cast = build1 (code: NOP_EXPR, char_type_node, node: int_cst);
15388 ASSERT_TRUE (CAN_HAVE_LOCATION_P (cast));
15389 ASSERT_EQ (cast, maybe_wrap_with_location (cast, loc));
15390
15391 /* Wrapping a STRING_CST. */
15392 tree string_cst = build_string (len: 4, str: "foo");
15393 ASSERT_FALSE (CAN_HAVE_LOCATION_P (string_cst));
15394 ASSERT_FALSE (location_wrapper_p (string_cst));
15395
15396 tree wrapped_string_cst = maybe_wrap_with_location (expr: string_cst, loc);
15397 ASSERT_TRUE (location_wrapper_p (wrapped_string_cst));
15398 ASSERT_EQ (VIEW_CONVERT_EXPR, TREE_CODE (wrapped_string_cst));
15399 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_string_cst));
15400 ASSERT_EQ (string_cst, tree_strip_any_location_wrapper (wrapped_string_cst));
15401
15402
15403 /* Wrapping a variable. */
15404 tree int_var = build_decl (UNKNOWN_LOCATION, code: VAR_DECL,
15405 get_identifier ("some_int_var"),
15406 integer_type_node);
15407 ASSERT_FALSE (CAN_HAVE_LOCATION_P (int_var));
15408 ASSERT_FALSE (location_wrapper_p (int_var));
15409
15410 tree wrapped_int_var = maybe_wrap_with_location (expr: int_var, loc);
15411 ASSERT_TRUE (location_wrapper_p (wrapped_int_var));
15412 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_int_var));
15413 ASSERT_EQ (int_var, tree_strip_any_location_wrapper (wrapped_int_var));
15414
15415 /* Verify that "reinterpret_cast<int>(some_int_var)" is not a location
15416 wrapper. */
15417 tree r_cast = build1 (code: NON_LVALUE_EXPR, integer_type_node, node: int_var);
15418 ASSERT_FALSE (location_wrapper_p (r_cast));
15419 ASSERT_EQ (r_cast, tree_strip_any_location_wrapper (r_cast));
15420
15421 /* Verify that STRIP_NOPS removes wrappers. */
15422 check_strip_nops (node: wrapped_int_cst, expected: int_cst);
15423 check_strip_nops (node: wrapped_string_cst, expected: string_cst);
15424 check_strip_nops (node: wrapped_int_var, expected: int_var);
15425}
15426
15427/* Test various tree predicates. Verify that location wrappers don't
15428 affect the results. */
15429
15430static void
15431test_predicates ()
15432{
15433 /* Build various constants and wrappers around them. */
15434
15435 location_t loc = BUILTINS_LOCATION;
15436
15437 tree i_0 = build_int_cst (integer_type_node, cst: 0);
15438 tree wr_i_0 = maybe_wrap_with_location (expr: i_0, loc);
15439
15440 tree i_1 = build_int_cst (integer_type_node, cst: 1);
15441 tree wr_i_1 = maybe_wrap_with_location (expr: i_1, loc);
15442
15443 tree i_m1 = build_int_cst (integer_type_node, cst: -1);
15444 tree wr_i_m1 = maybe_wrap_with_location (expr: i_m1, loc);
15445
15446 tree f_0 = build_real_from_int_cst (float_type_node, i: i_0);
15447 tree wr_f_0 = maybe_wrap_with_location (expr: f_0, loc);
15448 tree f_1 = build_real_from_int_cst (float_type_node, i: i_1);
15449 tree wr_f_1 = maybe_wrap_with_location (expr: f_1, loc);
15450 tree f_m1 = build_real_from_int_cst (float_type_node, i: i_m1);
15451 tree wr_f_m1 = maybe_wrap_with_location (expr: f_m1, loc);
15452
15453 tree c_i_0 = build_complex (NULL_TREE, real: i_0, imag: i_0);
15454 tree c_i_1 = build_complex (NULL_TREE, real: i_1, imag: i_0);
15455 tree c_i_m1 = build_complex (NULL_TREE, real: i_m1, imag: i_0);
15456
15457 tree c_f_0 = build_complex (NULL_TREE, real: f_0, imag: f_0);
15458 tree c_f_1 = build_complex (NULL_TREE, real: f_1, imag: f_0);
15459 tree c_f_m1 = build_complex (NULL_TREE, real: f_m1, imag: f_0);
15460
15461 /* TODO: vector constants. */
15462
15463 /* Test integer_onep. */
15464 ASSERT_FALSE (integer_onep (i_0));
15465 ASSERT_FALSE (integer_onep (wr_i_0));
15466 ASSERT_TRUE (integer_onep (i_1));
15467 ASSERT_TRUE (integer_onep (wr_i_1));
15468 ASSERT_FALSE (integer_onep (i_m1));
15469 ASSERT_FALSE (integer_onep (wr_i_m1));
15470 ASSERT_FALSE (integer_onep (f_0));
15471 ASSERT_FALSE (integer_onep (wr_f_0));
15472 ASSERT_FALSE (integer_onep (f_1));
15473 ASSERT_FALSE (integer_onep (wr_f_1));
15474 ASSERT_FALSE (integer_onep (f_m1));
15475 ASSERT_FALSE (integer_onep (wr_f_m1));
15476 ASSERT_FALSE (integer_onep (c_i_0));
15477 ASSERT_TRUE (integer_onep (c_i_1));
15478 ASSERT_FALSE (integer_onep (c_i_m1));
15479 ASSERT_FALSE (integer_onep (c_f_0));
15480 ASSERT_FALSE (integer_onep (c_f_1));
15481 ASSERT_FALSE (integer_onep (c_f_m1));
15482
15483 /* Test integer_zerop. */
15484 ASSERT_TRUE (integer_zerop (i_0));
15485 ASSERT_TRUE (integer_zerop (wr_i_0));
15486 ASSERT_FALSE (integer_zerop (i_1));
15487 ASSERT_FALSE (integer_zerop (wr_i_1));
15488 ASSERT_FALSE (integer_zerop (i_m1));
15489 ASSERT_FALSE (integer_zerop (wr_i_m1));
15490 ASSERT_FALSE (integer_zerop (f_0));
15491 ASSERT_FALSE (integer_zerop (wr_f_0));
15492 ASSERT_FALSE (integer_zerop (f_1));
15493 ASSERT_FALSE (integer_zerop (wr_f_1));
15494 ASSERT_FALSE (integer_zerop (f_m1));
15495 ASSERT_FALSE (integer_zerop (wr_f_m1));
15496 ASSERT_TRUE (integer_zerop (c_i_0));
15497 ASSERT_FALSE (integer_zerop (c_i_1));
15498 ASSERT_FALSE (integer_zerop (c_i_m1));
15499 ASSERT_FALSE (integer_zerop (c_f_0));
15500 ASSERT_FALSE (integer_zerop (c_f_1));
15501 ASSERT_FALSE (integer_zerop (c_f_m1));
15502
15503 /* Test integer_all_onesp. */
15504 ASSERT_FALSE (integer_all_onesp (i_0));
15505 ASSERT_FALSE (integer_all_onesp (wr_i_0));
15506 ASSERT_FALSE (integer_all_onesp (i_1));
15507 ASSERT_FALSE (integer_all_onesp (wr_i_1));
15508 ASSERT_TRUE (integer_all_onesp (i_m1));
15509 ASSERT_TRUE (integer_all_onesp (wr_i_m1));
15510 ASSERT_FALSE (integer_all_onesp (f_0));
15511 ASSERT_FALSE (integer_all_onesp (wr_f_0));
15512 ASSERT_FALSE (integer_all_onesp (f_1));
15513 ASSERT_FALSE (integer_all_onesp (wr_f_1));
15514 ASSERT_FALSE (integer_all_onesp (f_m1));
15515 ASSERT_FALSE (integer_all_onesp (wr_f_m1));
15516 ASSERT_FALSE (integer_all_onesp (c_i_0));
15517 ASSERT_FALSE (integer_all_onesp (c_i_1));
15518 ASSERT_FALSE (integer_all_onesp (c_i_m1));
15519 ASSERT_FALSE (integer_all_onesp (c_f_0));
15520 ASSERT_FALSE (integer_all_onesp (c_f_1));
15521 ASSERT_FALSE (integer_all_onesp (c_f_m1));
15522
15523 /* Test integer_minus_onep. */
15524 ASSERT_FALSE (integer_minus_onep (i_0));
15525 ASSERT_FALSE (integer_minus_onep (wr_i_0));
15526 ASSERT_FALSE (integer_minus_onep (i_1));
15527 ASSERT_FALSE (integer_minus_onep (wr_i_1));
15528 ASSERT_TRUE (integer_minus_onep (i_m1));
15529 ASSERT_TRUE (integer_minus_onep (wr_i_m1));
15530 ASSERT_FALSE (integer_minus_onep (f_0));
15531 ASSERT_FALSE (integer_minus_onep (wr_f_0));
15532 ASSERT_FALSE (integer_minus_onep (f_1));
15533 ASSERT_FALSE (integer_minus_onep (wr_f_1));
15534 ASSERT_FALSE (integer_minus_onep (f_m1));
15535 ASSERT_FALSE (integer_minus_onep (wr_f_m1));
15536 ASSERT_FALSE (integer_minus_onep (c_i_0));
15537 ASSERT_FALSE (integer_minus_onep (c_i_1));
15538 ASSERT_TRUE (integer_minus_onep (c_i_m1));
15539 ASSERT_FALSE (integer_minus_onep (c_f_0));
15540 ASSERT_FALSE (integer_minus_onep (c_f_1));
15541 ASSERT_FALSE (integer_minus_onep (c_f_m1));
15542
15543 /* Test integer_each_onep. */
15544 ASSERT_FALSE (integer_each_onep (i_0));
15545 ASSERT_FALSE (integer_each_onep (wr_i_0));
15546 ASSERT_TRUE (integer_each_onep (i_1));
15547 ASSERT_TRUE (integer_each_onep (wr_i_1));
15548 ASSERT_FALSE (integer_each_onep (i_m1));
15549 ASSERT_FALSE (integer_each_onep (wr_i_m1));
15550 ASSERT_FALSE (integer_each_onep (f_0));
15551 ASSERT_FALSE (integer_each_onep (wr_f_0));
15552 ASSERT_FALSE (integer_each_onep (f_1));
15553 ASSERT_FALSE (integer_each_onep (wr_f_1));
15554 ASSERT_FALSE (integer_each_onep (f_m1));
15555 ASSERT_FALSE (integer_each_onep (wr_f_m1));
15556 ASSERT_FALSE (integer_each_onep (c_i_0));
15557 ASSERT_FALSE (integer_each_onep (c_i_1));
15558 ASSERT_FALSE (integer_each_onep (c_i_m1));
15559 ASSERT_FALSE (integer_each_onep (c_f_0));
15560 ASSERT_FALSE (integer_each_onep (c_f_1));
15561 ASSERT_FALSE (integer_each_onep (c_f_m1));
15562
15563 /* Test integer_truep. */
15564 ASSERT_FALSE (integer_truep (i_0));
15565 ASSERT_FALSE (integer_truep (wr_i_0));
15566 ASSERT_TRUE (integer_truep (i_1));
15567 ASSERT_TRUE (integer_truep (wr_i_1));
15568 ASSERT_FALSE (integer_truep (i_m1));
15569 ASSERT_FALSE (integer_truep (wr_i_m1));
15570 ASSERT_FALSE (integer_truep (f_0));
15571 ASSERT_FALSE (integer_truep (wr_f_0));
15572 ASSERT_FALSE (integer_truep (f_1));
15573 ASSERT_FALSE (integer_truep (wr_f_1));
15574 ASSERT_FALSE (integer_truep (f_m1));
15575 ASSERT_FALSE (integer_truep (wr_f_m1));
15576 ASSERT_FALSE (integer_truep (c_i_0));
15577 ASSERT_TRUE (integer_truep (c_i_1));
15578 ASSERT_FALSE (integer_truep (c_i_m1));
15579 ASSERT_FALSE (integer_truep (c_f_0));
15580 ASSERT_FALSE (integer_truep (c_f_1));
15581 ASSERT_FALSE (integer_truep (c_f_m1));
15582
15583 /* Test integer_nonzerop. */
15584 ASSERT_FALSE (integer_nonzerop (i_0));
15585 ASSERT_FALSE (integer_nonzerop (wr_i_0));
15586 ASSERT_TRUE (integer_nonzerop (i_1));
15587 ASSERT_TRUE (integer_nonzerop (wr_i_1));
15588 ASSERT_TRUE (integer_nonzerop (i_m1));
15589 ASSERT_TRUE (integer_nonzerop (wr_i_m1));
15590 ASSERT_FALSE (integer_nonzerop (f_0));
15591 ASSERT_FALSE (integer_nonzerop (wr_f_0));
15592 ASSERT_FALSE (integer_nonzerop (f_1));
15593 ASSERT_FALSE (integer_nonzerop (wr_f_1));
15594 ASSERT_FALSE (integer_nonzerop (f_m1));
15595 ASSERT_FALSE (integer_nonzerop (wr_f_m1));
15596 ASSERT_FALSE (integer_nonzerop (c_i_0));
15597 ASSERT_TRUE (integer_nonzerop (c_i_1));
15598 ASSERT_TRUE (integer_nonzerop (c_i_m1));
15599 ASSERT_FALSE (integer_nonzerop (c_f_0));
15600 ASSERT_FALSE (integer_nonzerop (c_f_1));
15601 ASSERT_FALSE (integer_nonzerop (c_f_m1));
15602
15603 /* Test real_zerop. */
15604 ASSERT_FALSE (real_zerop (i_0));
15605 ASSERT_FALSE (real_zerop (wr_i_0));
15606 ASSERT_FALSE (real_zerop (i_1));
15607 ASSERT_FALSE (real_zerop (wr_i_1));
15608 ASSERT_FALSE (real_zerop (i_m1));
15609 ASSERT_FALSE (real_zerop (wr_i_m1));
15610 ASSERT_TRUE (real_zerop (f_0));
15611 ASSERT_TRUE (real_zerop (wr_f_0));
15612 ASSERT_FALSE (real_zerop (f_1));
15613 ASSERT_FALSE (real_zerop (wr_f_1));
15614 ASSERT_FALSE (real_zerop (f_m1));
15615 ASSERT_FALSE (real_zerop (wr_f_m1));
15616 ASSERT_FALSE (real_zerop (c_i_0));
15617 ASSERT_FALSE (real_zerop (c_i_1));
15618 ASSERT_FALSE (real_zerop (c_i_m1));
15619 ASSERT_TRUE (real_zerop (c_f_0));
15620 ASSERT_FALSE (real_zerop (c_f_1));
15621 ASSERT_FALSE (real_zerop (c_f_m1));
15622
15623 /* Test real_onep. */
15624 ASSERT_FALSE (real_onep (i_0));
15625 ASSERT_FALSE (real_onep (wr_i_0));
15626 ASSERT_FALSE (real_onep (i_1));
15627 ASSERT_FALSE (real_onep (wr_i_1));
15628 ASSERT_FALSE (real_onep (i_m1));
15629 ASSERT_FALSE (real_onep (wr_i_m1));
15630 ASSERT_FALSE (real_onep (f_0));
15631 ASSERT_FALSE (real_onep (wr_f_0));
15632 ASSERT_TRUE (real_onep (f_1));
15633 ASSERT_TRUE (real_onep (wr_f_1));
15634 ASSERT_FALSE (real_onep (f_m1));
15635 ASSERT_FALSE (real_onep (wr_f_m1));
15636 ASSERT_FALSE (real_onep (c_i_0));
15637 ASSERT_FALSE (real_onep (c_i_1));
15638 ASSERT_FALSE (real_onep (c_i_m1));
15639 ASSERT_FALSE (real_onep (c_f_0));
15640 ASSERT_TRUE (real_onep (c_f_1));
15641 ASSERT_FALSE (real_onep (c_f_m1));
15642
15643 /* Test real_minus_onep. */
15644 ASSERT_FALSE (real_minus_onep (i_0));
15645 ASSERT_FALSE (real_minus_onep (wr_i_0));
15646 ASSERT_FALSE (real_minus_onep (i_1));
15647 ASSERT_FALSE (real_minus_onep (wr_i_1));
15648 ASSERT_FALSE (real_minus_onep (i_m1));
15649 ASSERT_FALSE (real_minus_onep (wr_i_m1));
15650 ASSERT_FALSE (real_minus_onep (f_0));
15651 ASSERT_FALSE (real_minus_onep (wr_f_0));
15652 ASSERT_FALSE (real_minus_onep (f_1));
15653 ASSERT_FALSE (real_minus_onep (wr_f_1));
15654 ASSERT_TRUE (real_minus_onep (f_m1));
15655 ASSERT_TRUE (real_minus_onep (wr_f_m1));
15656 ASSERT_FALSE (real_minus_onep (c_i_0));
15657 ASSERT_FALSE (real_minus_onep (c_i_1));
15658 ASSERT_FALSE (real_minus_onep (c_i_m1));
15659 ASSERT_FALSE (real_minus_onep (c_f_0));
15660 ASSERT_FALSE (real_minus_onep (c_f_1));
15661 ASSERT_TRUE (real_minus_onep (c_f_m1));
15662
15663 /* Test zerop. */
15664 ASSERT_TRUE (zerop (i_0));
15665 ASSERT_TRUE (zerop (wr_i_0));
15666 ASSERT_FALSE (zerop (i_1));
15667 ASSERT_FALSE (zerop (wr_i_1));
15668 ASSERT_FALSE (zerop (i_m1));
15669 ASSERT_FALSE (zerop (wr_i_m1));
15670 ASSERT_TRUE (zerop (f_0));
15671 ASSERT_TRUE (zerop (wr_f_0));
15672 ASSERT_FALSE (zerop (f_1));
15673 ASSERT_FALSE (zerop (wr_f_1));
15674 ASSERT_FALSE (zerop (f_m1));
15675 ASSERT_FALSE (zerop (wr_f_m1));
15676 ASSERT_TRUE (zerop (c_i_0));
15677 ASSERT_FALSE (zerop (c_i_1));
15678 ASSERT_FALSE (zerop (c_i_m1));
15679 ASSERT_TRUE (zerop (c_f_0));
15680 ASSERT_FALSE (zerop (c_f_1));
15681 ASSERT_FALSE (zerop (c_f_m1));
15682
15683 /* Test tree_expr_nonnegative_p. */
15684 ASSERT_TRUE (tree_expr_nonnegative_p (i_0));
15685 ASSERT_TRUE (tree_expr_nonnegative_p (wr_i_0));
15686 ASSERT_TRUE (tree_expr_nonnegative_p (i_1));
15687 ASSERT_TRUE (tree_expr_nonnegative_p (wr_i_1));
15688 ASSERT_FALSE (tree_expr_nonnegative_p (i_m1));
15689 ASSERT_FALSE (tree_expr_nonnegative_p (wr_i_m1));
15690 ASSERT_TRUE (tree_expr_nonnegative_p (f_0));
15691 ASSERT_TRUE (tree_expr_nonnegative_p (wr_f_0));
15692 ASSERT_TRUE (tree_expr_nonnegative_p (f_1));
15693 ASSERT_TRUE (tree_expr_nonnegative_p (wr_f_1));
15694 ASSERT_FALSE (tree_expr_nonnegative_p (f_m1));
15695 ASSERT_FALSE (tree_expr_nonnegative_p (wr_f_m1));
15696 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_0));
15697 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_1));
15698 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_m1));
15699 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_0));
15700 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_1));
15701 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_m1));
15702
15703 /* Test tree_expr_nonzero_p. */
15704 ASSERT_FALSE (tree_expr_nonzero_p (i_0));
15705 ASSERT_FALSE (tree_expr_nonzero_p (wr_i_0));
15706 ASSERT_TRUE (tree_expr_nonzero_p (i_1));
15707 ASSERT_TRUE (tree_expr_nonzero_p (wr_i_1));
15708 ASSERT_TRUE (tree_expr_nonzero_p (i_m1));
15709 ASSERT_TRUE (tree_expr_nonzero_p (wr_i_m1));
15710
15711 /* Test integer_valued_real_p. */
15712 ASSERT_FALSE (integer_valued_real_p (i_0));
15713 ASSERT_TRUE (integer_valued_real_p (f_0));
15714 ASSERT_TRUE (integer_valued_real_p (wr_f_0));
15715 ASSERT_TRUE (integer_valued_real_p (f_1));
15716 ASSERT_TRUE (integer_valued_real_p (wr_f_1));
15717
15718 /* Test integer_pow2p. */
15719 ASSERT_FALSE (integer_pow2p (i_0));
15720 ASSERT_TRUE (integer_pow2p (i_1));
15721 ASSERT_TRUE (integer_pow2p (wr_i_1));
15722
15723 /* Test uniform_integer_cst_p. */
15724 ASSERT_TRUE (uniform_integer_cst_p (i_0));
15725 ASSERT_TRUE (uniform_integer_cst_p (wr_i_0));
15726 ASSERT_TRUE (uniform_integer_cst_p (i_1));
15727 ASSERT_TRUE (uniform_integer_cst_p (wr_i_1));
15728 ASSERT_TRUE (uniform_integer_cst_p (i_m1));
15729 ASSERT_TRUE (uniform_integer_cst_p (wr_i_m1));
15730 ASSERT_FALSE (uniform_integer_cst_p (f_0));
15731 ASSERT_FALSE (uniform_integer_cst_p (wr_f_0));
15732 ASSERT_FALSE (uniform_integer_cst_p (f_1));
15733 ASSERT_FALSE (uniform_integer_cst_p (wr_f_1));
15734 ASSERT_FALSE (uniform_integer_cst_p (f_m1));
15735 ASSERT_FALSE (uniform_integer_cst_p (wr_f_m1));
15736 ASSERT_FALSE (uniform_integer_cst_p (c_i_0));
15737 ASSERT_FALSE (uniform_integer_cst_p (c_i_1));
15738 ASSERT_FALSE (uniform_integer_cst_p (c_i_m1));
15739 ASSERT_FALSE (uniform_integer_cst_p (c_f_0));
15740 ASSERT_FALSE (uniform_integer_cst_p (c_f_1));
15741 ASSERT_FALSE (uniform_integer_cst_p (c_f_m1));
15742}
15743
15744/* Check that string escaping works correctly. */
15745
15746static void
15747test_escaped_strings (void)
15748{
15749 int saved_cutoff;
15750 escaped_string msg;
15751
15752 msg.escape (NULL);
15753 /* ASSERT_STREQ does not accept NULL as a valid test
15754 result, so we have to use ASSERT_EQ instead. */
15755 ASSERT_EQ (NULL, (const char *) msg);
15756
15757 msg.escape (unescaped: "");
15758 ASSERT_STREQ ("", (const char *) msg);
15759
15760 msg.escape (unescaped: "foobar");
15761 ASSERT_STREQ ("foobar", (const char *) msg);
15762
15763 /* Ensure that we have -fmessage-length set to 0. */
15764 saved_cutoff = pp_line_cutoff (global_dc->printer);
15765 pp_line_cutoff (global_dc->printer) = 0;
15766
15767 msg.escape (unescaped: "foo\nbar");
15768 ASSERT_STREQ ("foo\\nbar", (const char *) msg);
15769
15770 msg.escape (unescaped: "\a\b\f\n\r\t\v");
15771 ASSERT_STREQ ("\\a\\b\\f\\n\\r\\t\\v", (const char *) msg);
15772
15773 /* Now repeat the tests with -fmessage-length set to 5. */
15774 pp_line_cutoff (global_dc->printer) = 5;
15775
15776 /* Note that the newline is not translated into an escape. */
15777 msg.escape (unescaped: "foo\nbar");
15778 ASSERT_STREQ ("foo\nbar", (const char *) msg);
15779
15780 msg.escape (unescaped: "\a\b\f\n\r\t\v");
15781 ASSERT_STREQ ("\\a\\b\\f\n\\r\\t\\v", (const char *) msg);
15782
15783 /* Restore the original message length setting. */
15784 pp_line_cutoff (global_dc->printer) = saved_cutoff;
15785}
15786
15787/* Run all of the selftests within this file. */
15788
15789void
15790tree_cc_tests ()
15791{
15792 test_integer_constants ();
15793 test_identifiers ();
15794 test_labels ();
15795 test_vector_cst_patterns ();
15796 test_location_wrappers ();
15797 test_predicates ();
15798 test_escaped_strings ();
15799}
15800
15801} // namespace selftest
15802
15803#endif /* CHECKING_P */
15804
15805#include "gt-tree.h"
15806

source code of gcc/tree.cc