1/* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2023 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 1, /* OMP_CLAUSE_INDIRECT */
273 2, /* OMP_CLAUSE__CACHE_ */
274 2, /* OMP_CLAUSE_GANG */
275 1, /* OMP_CLAUSE_ASYNC */
276 1, /* OMP_CLAUSE_WAIT */
277 0, /* OMP_CLAUSE_AUTO */
278 0, /* OMP_CLAUSE_SEQ */
279 1, /* OMP_CLAUSE__LOOPTEMP_ */
280 1, /* OMP_CLAUSE__REDUCTEMP_ */
281 1, /* OMP_CLAUSE__CONDTEMP_ */
282 1, /* OMP_CLAUSE__SCANTEMP_ */
283 1, /* OMP_CLAUSE_IF */
284 1, /* OMP_CLAUSE_SELF */
285 1, /* OMP_CLAUSE_NUM_THREADS */
286 1, /* OMP_CLAUSE_SCHEDULE */
287 0, /* OMP_CLAUSE_NOWAIT */
288 1, /* OMP_CLAUSE_ORDERED */
289 0, /* OMP_CLAUSE_DEFAULT */
290 3, /* OMP_CLAUSE_COLLAPSE */
291 0, /* OMP_CLAUSE_UNTIED */
292 1, /* OMP_CLAUSE_FINAL */
293 0, /* OMP_CLAUSE_MERGEABLE */
294 1, /* OMP_CLAUSE_DEVICE */
295 1, /* OMP_CLAUSE_DIST_SCHEDULE */
296 0, /* OMP_CLAUSE_INBRANCH */
297 0, /* OMP_CLAUSE_NOTINBRANCH */
298 2, /* OMP_CLAUSE_NUM_TEAMS */
299 1, /* OMP_CLAUSE_THREAD_LIMIT */
300 0, /* OMP_CLAUSE_PROC_BIND */
301 1, /* OMP_CLAUSE_SAFELEN */
302 1, /* OMP_CLAUSE_SIMDLEN */
303 0, /* OMP_CLAUSE_DEVICE_TYPE */
304 0, /* OMP_CLAUSE_FOR */
305 0, /* OMP_CLAUSE_PARALLEL */
306 0, /* OMP_CLAUSE_SECTIONS */
307 0, /* OMP_CLAUSE_TASKGROUP */
308 1, /* OMP_CLAUSE_PRIORITY */
309 1, /* OMP_CLAUSE_GRAINSIZE */
310 1, /* OMP_CLAUSE_NUM_TASKS */
311 0, /* OMP_CLAUSE_NOGROUP */
312 0, /* OMP_CLAUSE_THREADS */
313 0, /* OMP_CLAUSE_SIMD */
314 1, /* OMP_CLAUSE_HINT */
315 0, /* OMP_CLAUSE_DEFAULTMAP */
316 0, /* OMP_CLAUSE_ORDER */
317 0, /* OMP_CLAUSE_BIND */
318 1, /* OMP_CLAUSE_FILTER */
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 "indirect",
366 "_cache_",
367 "gang",
368 "async",
369 "wait",
370 "auto",
371 "seq",
372 "_looptemp_",
373 "_reductemp_",
374 "_condtemp_",
375 "_scantemp_",
376 "if",
377 "self",
378 "num_threads",
379 "schedule",
380 "nowait",
381 "ordered",
382 "default",
383 "collapse",
384 "untied",
385 "final",
386 "mergeable",
387 "device",
388 "dist_schedule",
389 "inbranch",
390 "notinbranch",
391 "num_teams",
392 "thread_limit",
393 "proc_bind",
394 "safelen",
395 "simdlen",
396 "device_type",
397 "for",
398 "parallel",
399 "sections",
400 "taskgroup",
401 "priority",
402 "grainsize",
403 "num_tasks",
404 "nogroup",
405 "threads",
406 "simd",
407 "hint",
408 "defaultmap",
409 "order",
410 "bind",
411 "filter",
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)1 << 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 REAL_TYPE:
4214 case FIXED_POINT_TYPE:
4215 /* Here we just check the bounds. */
4216 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
4217 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
4218
4219 case ARRAY_TYPE:
4220 /* We have already checked the component type above, so just check
4221 the domain type. Flexible array members have a null domain. */
4222 return TYPE_DOMAIN (type) ?
4223 type_contains_placeholder_p (TYPE_DOMAIN (type)) : false;
4224
4225 case RECORD_TYPE:
4226 case UNION_TYPE:
4227 case QUAL_UNION_TYPE:
4228 {
4229 tree field;
4230
4231 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4232 if (TREE_CODE (field) == FIELD_DECL
4233 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
4234 || (TREE_CODE (type) == QUAL_UNION_TYPE
4235 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
4236 || type_contains_placeholder_p (TREE_TYPE (field))))
4237 return true;
4238
4239 return false;
4240 }
4241
4242 default:
4243 gcc_unreachable ();
4244 }
4245}
4246
4247/* Wrapper around above function used to cache its result. */
4248
4249bool
4250type_contains_placeholder_p (tree type)
4251{
4252 bool result;
4253
4254 /* If the contains_placeholder_bits field has been initialized,
4255 then we know the answer. */
4256 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
4257 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
4258
4259 /* Indicate that we've seen this type node, and the answer is false.
4260 This is what we want to return if we run into recursion via fields. */
4261 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
4262
4263 /* Compute the real value. */
4264 result = type_contains_placeholder_1 (type);
4265
4266 /* Store the real value. */
4267 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
4268
4269 return result;
4270}
4271
4272/* Push tree EXP onto vector QUEUE if it is not already present. */
4273
4274static void
4275push_without_duplicates (tree exp, vec<tree> *queue)
4276{
4277 unsigned int i;
4278 tree iter;
4279
4280 FOR_EACH_VEC_ELT (*queue, i, iter)
4281 if (simple_cst_equal (iter, exp) == 1)
4282 break;
4283
4284 if (!iter)
4285 queue->safe_push (obj: exp);
4286}
4287
4288/* Given a tree EXP, find all occurrences of references to fields
4289 in a PLACEHOLDER_EXPR and place them in vector REFS without
4290 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
4291 we assume here that EXP contains only arithmetic expressions
4292 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
4293 argument list. */
4294
4295void
4296find_placeholder_in_expr (tree exp, vec<tree> *refs)
4297{
4298 enum tree_code code = TREE_CODE (exp);
4299 tree inner;
4300 int i;
4301
4302 /* We handle TREE_LIST and COMPONENT_REF separately. */
4303 if (code == TREE_LIST)
4304 {
4305 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
4306 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
4307 }
4308 else if (code == COMPONENT_REF)
4309 {
4310 for (inner = TREE_OPERAND (exp, 0);
4311 REFERENCE_CLASS_P (inner);
4312 inner = TREE_OPERAND (inner, 0))
4313 ;
4314
4315 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
4316 push_without_duplicates (exp, queue: refs);
4317 else
4318 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
4319 }
4320 else
4321 switch (TREE_CODE_CLASS (code))
4322 {
4323 case tcc_constant:
4324 break;
4325
4326 case tcc_declaration:
4327 /* Variables allocated to static storage can stay. */
4328 if (!TREE_STATIC (exp))
4329 push_without_duplicates (exp, queue: refs);
4330 break;
4331
4332 case tcc_expression:
4333 /* This is the pattern built in ada/make_aligning_type. */
4334 if (code == ADDR_EXPR
4335 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
4336 {
4337 push_without_duplicates (exp, queue: refs);
4338 break;
4339 }
4340
4341 /* Fall through. */
4342
4343 case tcc_exceptional:
4344 case tcc_unary:
4345 case tcc_binary:
4346 case tcc_comparison:
4347 case tcc_reference:
4348 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
4349 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
4350 break;
4351
4352 case tcc_vl_exp:
4353 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4354 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
4355 break;
4356
4357 default:
4358 gcc_unreachable ();
4359 }
4360}
4361
4362/* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
4363 return a tree with all occurrences of references to F in a
4364 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
4365 CONST_DECLs. Note that we assume here that EXP contains only
4366 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
4367 occurring only in their argument list. */
4368
4369tree
4370substitute_in_expr (tree exp, tree f, tree r)
4371{
4372 enum tree_code code = TREE_CODE (exp);
4373 tree op0, op1, op2, op3;
4374 tree new_tree;
4375
4376 /* We handle TREE_LIST and COMPONENT_REF separately. */
4377 if (code == TREE_LIST)
4378 {
4379 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
4380 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
4381 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
4382 return exp;
4383
4384 return tree_cons (TREE_PURPOSE (exp), value: op1, chain: op0);
4385 }
4386 else if (code == COMPONENT_REF)
4387 {
4388 tree inner;
4389
4390 /* If this expression is getting a value from a PLACEHOLDER_EXPR
4391 and it is the right field, replace it with R. */
4392 for (inner = TREE_OPERAND (exp, 0);
4393 REFERENCE_CLASS_P (inner);
4394 inner = TREE_OPERAND (inner, 0))
4395 ;
4396
4397 /* The field. */
4398 op1 = TREE_OPERAND (exp, 1);
4399
4400 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
4401 return r;
4402
4403 /* If this expression hasn't been completed let, leave it alone. */
4404 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
4405 return exp;
4406
4407 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4408 if (op0 == TREE_OPERAND (exp, 0))
4409 return exp;
4410
4411 new_tree
4412 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
4413 }
4414 else
4415 switch (TREE_CODE_CLASS (code))
4416 {
4417 case tcc_constant:
4418 return exp;
4419
4420 case tcc_declaration:
4421 if (exp == f)
4422 return r;
4423 else
4424 return exp;
4425
4426 case tcc_expression:
4427 if (exp == f)
4428 return r;
4429
4430 /* Fall through. */
4431
4432 case tcc_exceptional:
4433 case tcc_unary:
4434 case tcc_binary:
4435 case tcc_comparison:
4436 case tcc_reference:
4437 switch (TREE_CODE_LENGTH (code))
4438 {
4439 case 0:
4440 return exp;
4441
4442 case 1:
4443 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4444 if (op0 == TREE_OPERAND (exp, 0))
4445 return exp;
4446
4447 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
4448 break;
4449
4450 case 2:
4451 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4452 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4453
4454 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
4455 return exp;
4456
4457 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
4458 break;
4459
4460 case 3:
4461 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4462 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4463 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
4464
4465 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4466 && op2 == TREE_OPERAND (exp, 2))
4467 return exp;
4468
4469 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
4470 break;
4471
4472 case 4:
4473 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4474 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4475 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
4476 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
4477
4478 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4479 && op2 == TREE_OPERAND (exp, 2)
4480 && op3 == TREE_OPERAND (exp, 3))
4481 return exp;
4482
4483 new_tree
4484 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
4485 break;
4486
4487 default:
4488 gcc_unreachable ();
4489 }
4490 break;
4491
4492 case tcc_vl_exp:
4493 {
4494 int i;
4495
4496 new_tree = NULL_TREE;
4497
4498 /* If we are trying to replace F with a constant or with another
4499 instance of one of the arguments of the call, inline back
4500 functions which do nothing else than computing a value from
4501 the arguments they are passed. This makes it possible to
4502 fold partially or entirely the replacement expression. */
4503 if (code == CALL_EXPR)
4504 {
4505 bool maybe_inline = false;
4506 if (CONSTANT_CLASS_P (r))
4507 maybe_inline = true;
4508 else
4509 for (i = 3; i < TREE_OPERAND_LENGTH (exp); i++)
4510 if (operand_equal_p (TREE_OPERAND (exp, i), r, flags: 0))
4511 {
4512 maybe_inline = true;
4513 break;
4514 }
4515 if (maybe_inline)
4516 {
4517 tree t = maybe_inline_call_in_expr (exp);
4518 if (t)
4519 return SUBSTITUTE_IN_EXPR (t, f, r);
4520 }
4521 }
4522
4523 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4524 {
4525 tree op = TREE_OPERAND (exp, i);
4526 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
4527 if (new_op != op)
4528 {
4529 if (!new_tree)
4530 new_tree = copy_node (node: exp);
4531 TREE_OPERAND (new_tree, i) = new_op;
4532 }
4533 }
4534
4535 if (new_tree)
4536 {
4537 new_tree = fold (new_tree);
4538 if (TREE_CODE (new_tree) == CALL_EXPR)
4539 process_call_operands (t: new_tree);
4540 }
4541 else
4542 return exp;
4543 }
4544 break;
4545
4546 default:
4547 gcc_unreachable ();
4548 }
4549
4550 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4551
4552 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4553 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4554
4555 return new_tree;
4556}
4557
4558/* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
4559 for it within OBJ, a tree that is an object or a chain of references. */
4560
4561tree
4562substitute_placeholder_in_expr (tree exp, tree obj)
4563{
4564 enum tree_code code = TREE_CODE (exp);
4565 tree op0, op1, op2, op3;
4566 tree new_tree;
4567
4568 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
4569 in the chain of OBJ. */
4570 if (code == PLACEHOLDER_EXPR)
4571 {
4572 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
4573 tree elt;
4574
4575 for (elt = obj; elt != 0;
4576 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
4577 || TREE_CODE (elt) == COND_EXPR)
4578 ? TREE_OPERAND (elt, 1)
4579 : (REFERENCE_CLASS_P (elt)
4580 || UNARY_CLASS_P (elt)
4581 || BINARY_CLASS_P (elt)
4582 || VL_EXP_CLASS_P (elt)
4583 || EXPRESSION_CLASS_P (elt))
4584 ? TREE_OPERAND (elt, 0) : 0))
4585 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
4586 return elt;
4587
4588 for (elt = obj; elt != 0;
4589 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
4590 || TREE_CODE (elt) == COND_EXPR)
4591 ? TREE_OPERAND (elt, 1)
4592 : (REFERENCE_CLASS_P (elt)
4593 || UNARY_CLASS_P (elt)
4594 || BINARY_CLASS_P (elt)
4595 || VL_EXP_CLASS_P (elt)
4596 || EXPRESSION_CLASS_P (elt))
4597 ? TREE_OPERAND (elt, 0) : 0))
4598 if (POINTER_TYPE_P (TREE_TYPE (elt))
4599 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
4600 == need_type))
4601 return fold_build1 (INDIRECT_REF, need_type, elt);
4602
4603 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
4604 survives until RTL generation, there will be an error. */
4605 return exp;
4606 }
4607
4608 /* TREE_LIST is special because we need to look at TREE_VALUE
4609 and TREE_CHAIN, not TREE_OPERANDS. */
4610 else if (code == TREE_LIST)
4611 {
4612 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
4613 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
4614 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
4615 return exp;
4616
4617 return tree_cons (TREE_PURPOSE (exp), value: op1, chain: op0);
4618 }
4619 else
4620 switch (TREE_CODE_CLASS (code))
4621 {
4622 case tcc_constant:
4623 case tcc_declaration:
4624 return exp;
4625
4626 case tcc_exceptional:
4627 case tcc_unary:
4628 case tcc_binary:
4629 case tcc_comparison:
4630 case tcc_expression:
4631 case tcc_reference:
4632 case tcc_statement:
4633 switch (TREE_CODE_LENGTH (code))
4634 {
4635 case 0:
4636 return exp;
4637
4638 case 1:
4639 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4640 if (op0 == TREE_OPERAND (exp, 0))
4641 return exp;
4642
4643 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
4644 break;
4645
4646 case 2:
4647 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4648 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4649
4650 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
4651 return exp;
4652
4653 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
4654 break;
4655
4656 case 3:
4657 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4658 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4659 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
4660
4661 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4662 && op2 == TREE_OPERAND (exp, 2))
4663 return exp;
4664
4665 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
4666 break;
4667
4668 case 4:
4669 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4670 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4671 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
4672 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
4673
4674 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4675 && op2 == TREE_OPERAND (exp, 2)
4676 && op3 == TREE_OPERAND (exp, 3))
4677 return exp;
4678
4679 new_tree
4680 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
4681 break;
4682
4683 default:
4684 gcc_unreachable ();
4685 }
4686 break;
4687
4688 case tcc_vl_exp:
4689 {
4690 int i;
4691
4692 new_tree = NULL_TREE;
4693
4694 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4695 {
4696 tree op = TREE_OPERAND (exp, i);
4697 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
4698 if (new_op != op)
4699 {
4700 if (!new_tree)
4701 new_tree = copy_node (node: exp);
4702 TREE_OPERAND (new_tree, i) = new_op;
4703 }
4704 }
4705
4706 if (new_tree)
4707 {
4708 new_tree = fold (new_tree);
4709 if (TREE_CODE (new_tree) == CALL_EXPR)
4710 process_call_operands (t: new_tree);
4711 }
4712 else
4713 return exp;
4714 }
4715 break;
4716
4717 default:
4718 gcc_unreachable ();
4719 }
4720
4721 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4722
4723 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4724 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4725
4726 return new_tree;
4727}
4728
4729
4730/* Subroutine of stabilize_reference; this is called for subtrees of
4731 references. Any expression with side-effects must be put in a SAVE_EXPR
4732 to ensure that it is only evaluated once.
4733
4734 We don't put SAVE_EXPR nodes around everything, because assigning very
4735 simple expressions to temporaries causes us to miss good opportunities
4736 for optimizations. Among other things, the opportunity to fold in the
4737 addition of a constant into an addressing mode often gets lost, e.g.
4738 "y[i+1] += x;". In general, we take the approach that we should not make
4739 an assignment unless we are forced into it - i.e., that any non-side effect
4740 operator should be allowed, and that cse should take care of coalescing
4741 multiple utterances of the same expression should that prove fruitful. */
4742
4743static tree
4744stabilize_reference_1 (tree e)
4745{
4746 tree result;
4747 enum tree_code code = TREE_CODE (e);
4748
4749 /* We cannot ignore const expressions because it might be a reference
4750 to a const array but whose index contains side-effects. But we can
4751 ignore things that are actual constant or that already have been
4752 handled by this function. */
4753
4754 if (tree_invariant_p (t: e))
4755 return e;
4756
4757 switch (TREE_CODE_CLASS (code))
4758 {
4759 case tcc_exceptional:
4760 /* Always wrap STATEMENT_LIST into SAVE_EXPR, even if it doesn't
4761 have side-effects. */
4762 if (code == STATEMENT_LIST)
4763 return save_expr (expr: e);
4764 /* FALLTHRU */
4765 case tcc_type:
4766 case tcc_declaration:
4767 case tcc_comparison:
4768 case tcc_statement:
4769 case tcc_expression:
4770 case tcc_reference:
4771 case tcc_vl_exp:
4772 /* If the expression has side-effects, then encase it in a SAVE_EXPR
4773 so that it will only be evaluated once. */
4774 /* The reference (r) and comparison (<) classes could be handled as
4775 below, but it is generally faster to only evaluate them once. */
4776 if (TREE_SIDE_EFFECTS (e))
4777 return save_expr (expr: e);
4778 return e;
4779
4780 case tcc_constant:
4781 /* Constants need no processing. In fact, we should never reach
4782 here. */
4783 return e;
4784
4785 case tcc_binary:
4786 /* Division is slow and tends to be compiled with jumps,
4787 especially the division by powers of 2 that is often
4788 found inside of an array reference. So do it just once. */
4789 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
4790 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
4791 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
4792 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
4793 return save_expr (expr: e);
4794 /* Recursively stabilize each operand. */
4795 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
4796 stabilize_reference_1 (TREE_OPERAND (e, 1)));
4797 break;
4798
4799 case tcc_unary:
4800 /* Recursively stabilize each operand. */
4801 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
4802 break;
4803
4804 default:
4805 gcc_unreachable ();
4806 }
4807
4808 TREE_TYPE (result) = TREE_TYPE (e);
4809 TREE_READONLY (result) = TREE_READONLY (e);
4810 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
4811 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
4812
4813 return result;
4814}
4815
4816/* Stabilize a reference so that we can use it any number of times
4817 without causing its operands to be evaluated more than once.
4818 Returns the stabilized reference. This works by means of save_expr,
4819 so see the caveats in the comments about save_expr.
4820
4821 Also allows conversion expressions whose operands are references.
4822 Any other kind of expression is returned unchanged. */
4823
4824tree
4825stabilize_reference (tree ref)
4826{
4827 tree result;
4828 enum tree_code code = TREE_CODE (ref);
4829
4830 switch (code)
4831 {
4832 case VAR_DECL:
4833 case PARM_DECL:
4834 case RESULT_DECL:
4835 /* No action is needed in this case. */
4836 return ref;
4837
4838 CASE_CONVERT:
4839 case FLOAT_EXPR:
4840 case FIX_TRUNC_EXPR:
4841 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
4842 break;
4843
4844 case INDIRECT_REF:
4845 result = build_nt (INDIRECT_REF,
4846 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
4847 break;
4848
4849 case COMPONENT_REF:
4850 result = build_nt (COMPONENT_REF,
4851 stabilize_reference (TREE_OPERAND (ref, 0)),
4852 TREE_OPERAND (ref, 1), NULL_TREE);
4853 break;
4854
4855 case BIT_FIELD_REF:
4856 result = build_nt (BIT_FIELD_REF,
4857 stabilize_reference (TREE_OPERAND (ref, 0)),
4858 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
4859 REF_REVERSE_STORAGE_ORDER (result) = REF_REVERSE_STORAGE_ORDER (ref);
4860 break;
4861
4862 case ARRAY_REF:
4863 result = build_nt (ARRAY_REF,
4864 stabilize_reference (TREE_OPERAND (ref, 0)),
4865 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4866 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4867 break;
4868
4869 case ARRAY_RANGE_REF:
4870 result = build_nt (ARRAY_RANGE_REF,
4871 stabilize_reference (TREE_OPERAND (ref, 0)),
4872 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4873 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4874 break;
4875
4876 case COMPOUND_EXPR:
4877 /* We cannot wrap the first expression in a SAVE_EXPR, as then
4878 it wouldn't be ignored. This matters when dealing with
4879 volatiles. */
4880 return stabilize_reference_1 (e: ref);
4881
4882 /* If arg isn't a kind of lvalue we recognize, make no change.
4883 Caller should recognize the error for an invalid lvalue. */
4884 default:
4885 return ref;
4886
4887 case ERROR_MARK:
4888 return error_mark_node;
4889 }
4890
4891 TREE_TYPE (result) = TREE_TYPE (ref);
4892 TREE_READONLY (result) = TREE_READONLY (ref);
4893 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
4894 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
4895 protected_set_expr_location (result, EXPR_LOCATION (ref));
4896
4897 return result;
4898}
4899
4900/* Low-level constructors for expressions. */
4901
4902/* A helper function for build1 and constant folders. Set TREE_CONSTANT,
4903 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
4904
4905void
4906recompute_tree_invariant_for_addr_expr (tree t)
4907{
4908 tree node;
4909 bool tc = true, se = false;
4910
4911 gcc_assert (TREE_CODE (t) == ADDR_EXPR);
4912
4913 /* We started out assuming this address is both invariant and constant, but
4914 does not have side effects. Now go down any handled components and see if
4915 any of them involve offsets that are either non-constant or non-invariant.
4916 Also check for side-effects.
4917
4918 ??? Note that this code makes no attempt to deal with the case where
4919 taking the address of something causes a copy due to misalignment. */
4920
4921#define UPDATE_FLAGS(NODE) \
4922do { tree _node = (NODE); \
4923 if (_node && !TREE_CONSTANT (_node)) tc = false; \
4924 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
4925
4926 for (node = TREE_OPERAND (t, 0); handled_component_p (t: node);
4927 node = TREE_OPERAND (node, 0))
4928 {
4929 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
4930 array reference (probably made temporarily by the G++ front end),
4931 so ignore all the operands. */
4932 if ((TREE_CODE (node) == ARRAY_REF
4933 || TREE_CODE (node) == ARRAY_RANGE_REF)
4934 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
4935 {
4936 UPDATE_FLAGS (TREE_OPERAND (node, 1));
4937 if (TREE_OPERAND (node, 2))
4938 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4939 if (TREE_OPERAND (node, 3))
4940 UPDATE_FLAGS (TREE_OPERAND (node, 3));
4941 }
4942 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
4943 FIELD_DECL, apparently. The G++ front end can put something else
4944 there, at least temporarily. */
4945 else if (TREE_CODE (node) == COMPONENT_REF
4946 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
4947 {
4948 if (TREE_OPERAND (node, 2))
4949 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4950 }
4951 }
4952
4953 node = lang_hooks.expr_to_decl (node, &tc, &se);
4954
4955 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4956 the address, since &(*a)->b is a form of addition. If it's a constant, the
4957 address is constant too. If it's a decl, its address is constant if the
4958 decl is static. Everything else is not constant and, furthermore,
4959 taking the address of a volatile variable is not volatile. */
4960 if (INDIRECT_REF_P (node)
4961 || TREE_CODE (node) == MEM_REF)
4962 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4963 else if (CONSTANT_CLASS_P (node))
4964 ;
4965 else if (DECL_P (node))
4966 tc &= (staticp (arg: node) != NULL_TREE);
4967 else
4968 {
4969 tc = false;
4970 se |= TREE_SIDE_EFFECTS (node);
4971 }
4972
4973
4974 TREE_CONSTANT (t) = tc;
4975 TREE_SIDE_EFFECTS (t) = se;
4976#undef UPDATE_FLAGS
4977}
4978
4979/* Build an expression of code CODE, data type TYPE, and operands as
4980 specified. Expressions and reference nodes can be created this way.
4981 Constants, decls, types and misc nodes cannot be.
4982
4983 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4984 enough for all extant tree codes. */
4985
4986tree
4987build0 (enum tree_code code, tree tt MEM_STAT_DECL)
4988{
4989 tree t;
4990
4991 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4992
4993 t = make_node (code PASS_MEM_STAT);
4994 TREE_TYPE (t) = tt;
4995
4996 return t;
4997}
4998
4999tree
5000build1 (enum tree_code code, tree type, tree node MEM_STAT_DECL)
5001{
5002 int length = sizeof (struct tree_exp);
5003 tree t;
5004
5005 record_node_allocation_statistics (code, length);
5006
5007 gcc_assert (TREE_CODE_LENGTH (code) == 1);
5008
5009 t = ggc_alloc_tree_node_stat (s: length PASS_MEM_STAT);
5010
5011 memset (s: t, c: 0, n: sizeof (struct tree_common));
5012
5013 TREE_SET_CODE (t, code);
5014
5015 TREE_TYPE (t) = type;
5016 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
5017 TREE_OPERAND (t, 0) = node;
5018 if (node && !TYPE_P (node))
5019 {
5020 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
5021 TREE_READONLY (t) = TREE_READONLY (node);
5022 }
5023
5024 if (TREE_CODE_CLASS (code) == tcc_statement)
5025 {
5026 if (code != DEBUG_BEGIN_STMT)
5027 TREE_SIDE_EFFECTS (t) = 1;
5028 }
5029 else switch (code)
5030 {
5031 case VA_ARG_EXPR:
5032 /* All of these have side-effects, no matter what their
5033 operands are. */
5034 TREE_SIDE_EFFECTS (t) = 1;
5035 TREE_READONLY (t) = 0;
5036 break;
5037
5038 case INDIRECT_REF:
5039 /* Whether a dereference is readonly has nothing to do with whether
5040 its operand is readonly. */
5041 TREE_READONLY (t) = 0;
5042 break;
5043
5044 case ADDR_EXPR:
5045 if (node)
5046 recompute_tree_invariant_for_addr_expr (t);
5047 break;
5048
5049 default:
5050 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
5051 && node && !TYPE_P (node)
5052 && TREE_CONSTANT (node))
5053 TREE_CONSTANT (t) = 1;
5054 if (TREE_CODE_CLASS (code) == tcc_reference
5055 && node && TREE_THIS_VOLATILE (node))
5056 TREE_THIS_VOLATILE (t) = 1;
5057 break;
5058 }
5059
5060 return t;
5061}
5062
5063#define PROCESS_ARG(N) \
5064 do { \
5065 TREE_OPERAND (t, N) = arg##N; \
5066 if (arg##N &&!TYPE_P (arg##N)) \
5067 { \
5068 if (TREE_SIDE_EFFECTS (arg##N)) \
5069 side_effects = 1; \
5070 if (!TREE_READONLY (arg##N) \
5071 && !CONSTANT_CLASS_P (arg##N)) \
5072 (void) (read_only = 0); \
5073 if (!TREE_CONSTANT (arg##N)) \
5074 (void) (constant = 0); \
5075 } \
5076 } while (0)
5077
5078tree
5079build2 (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
5080{
5081 bool constant, read_only, side_effects, div_by_zero;
5082 tree t;
5083
5084 gcc_assert (TREE_CODE_LENGTH (code) == 2);
5085
5086 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
5087 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
5088 /* When sizetype precision doesn't match that of pointers
5089 we need to be able to build explicit extensions or truncations
5090 of the offset argument. */
5091 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
5092 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
5093 && TREE_CODE (arg1) == INTEGER_CST);
5094
5095 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
5096 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
5097 && ptrofftype_p (TREE_TYPE (arg1)));
5098
5099 t = make_node (code PASS_MEM_STAT);
5100 TREE_TYPE (t) = tt;
5101
5102 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
5103 result based on those same flags for the arguments. But if the
5104 arguments aren't really even `tree' expressions, we shouldn't be trying
5105 to do this. */
5106
5107 /* Expressions without side effects may be constant if their
5108 arguments are as well. */
5109 constant = (TREE_CODE_CLASS (code) == tcc_comparison
5110 || TREE_CODE_CLASS (code) == tcc_binary);
5111 read_only = 1;
5112 side_effects = TREE_SIDE_EFFECTS (t);
5113
5114 switch (code)
5115 {
5116 case TRUNC_DIV_EXPR:
5117 case CEIL_DIV_EXPR:
5118 case FLOOR_DIV_EXPR:
5119 case ROUND_DIV_EXPR:
5120 case EXACT_DIV_EXPR:
5121 case CEIL_MOD_EXPR:
5122 case FLOOR_MOD_EXPR:
5123 case ROUND_MOD_EXPR:
5124 case TRUNC_MOD_EXPR:
5125 div_by_zero = integer_zerop (expr: arg1);
5126 break;
5127 default:
5128 div_by_zero = false;
5129 }
5130
5131 PROCESS_ARG (0);
5132 PROCESS_ARG (1);
5133
5134 TREE_SIDE_EFFECTS (t) = side_effects;
5135 if (code == MEM_REF)
5136 {
5137 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
5138 {
5139 tree o = TREE_OPERAND (arg0, 0);
5140 TREE_READONLY (t) = TREE_READONLY (o);
5141 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
5142 }
5143 }
5144 else
5145 {
5146 TREE_READONLY (t) = read_only;
5147 /* Don't mark X / 0 as constant. */
5148 TREE_CONSTANT (t) = constant && !div_by_zero;
5149 TREE_THIS_VOLATILE (t)
5150 = (TREE_CODE_CLASS (code) == tcc_reference
5151 && arg0 && TREE_THIS_VOLATILE (arg0));
5152 }
5153
5154 return t;
5155}
5156
5157
5158tree
5159build3 (enum tree_code code, tree tt, tree arg0, tree arg1,
5160 tree arg2 MEM_STAT_DECL)
5161{
5162 bool constant, read_only, side_effects;
5163 tree t;
5164
5165 gcc_assert (TREE_CODE_LENGTH (code) == 3);
5166 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
5167
5168 t = make_node (code PASS_MEM_STAT);
5169 TREE_TYPE (t) = tt;
5170
5171 read_only = 1;
5172
5173 /* As a special exception, if COND_EXPR has NULL branches, we
5174 assume that it is a gimple statement and always consider
5175 it to have side effects. */
5176 if (code == COND_EXPR
5177 && tt == void_type_node
5178 && arg1 == NULL_TREE
5179 && arg2 == NULL_TREE)
5180 side_effects = true;
5181 else
5182 side_effects = TREE_SIDE_EFFECTS (t);
5183
5184 PROCESS_ARG (0);
5185 PROCESS_ARG (1);
5186 PROCESS_ARG (2);
5187
5188 if (code == COND_EXPR)
5189 TREE_READONLY (t) = read_only;
5190
5191 TREE_SIDE_EFFECTS (t) = side_effects;
5192 TREE_THIS_VOLATILE (t)
5193 = (TREE_CODE_CLASS (code) == tcc_reference
5194 && arg0 && TREE_THIS_VOLATILE (arg0));
5195
5196 return t;
5197}
5198
5199tree
5200build4 (enum tree_code code, tree tt, tree arg0, tree arg1,
5201 tree arg2, tree arg3 MEM_STAT_DECL)
5202{
5203 bool constant, read_only, side_effects;
5204 tree t;
5205
5206 gcc_assert (TREE_CODE_LENGTH (code) == 4);
5207
5208 t = make_node (code PASS_MEM_STAT);
5209 TREE_TYPE (t) = tt;
5210
5211 side_effects = TREE_SIDE_EFFECTS (t);
5212
5213 PROCESS_ARG (0);
5214 PROCESS_ARG (1);
5215 PROCESS_ARG (2);
5216 PROCESS_ARG (3);
5217
5218 TREE_SIDE_EFFECTS (t) = side_effects;
5219 TREE_THIS_VOLATILE (t)
5220 = (TREE_CODE_CLASS (code) == tcc_reference
5221 && arg0 && TREE_THIS_VOLATILE (arg0));
5222
5223 return t;
5224}
5225
5226tree
5227build5 (enum tree_code code, tree tt, tree arg0, tree arg1,
5228 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
5229{
5230 bool constant, read_only, side_effects;
5231 tree t;
5232
5233 gcc_assert (TREE_CODE_LENGTH (code) == 5);
5234
5235 t = make_node (code PASS_MEM_STAT);
5236 TREE_TYPE (t) = tt;
5237
5238 side_effects = TREE_SIDE_EFFECTS (t);
5239
5240 PROCESS_ARG (0);
5241 PROCESS_ARG (1);
5242 PROCESS_ARG (2);
5243 PROCESS_ARG (3);
5244 PROCESS_ARG (4);
5245
5246 TREE_SIDE_EFFECTS (t) = side_effects;
5247 if (code == TARGET_MEM_REF)
5248 {
5249 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
5250 {
5251 tree o = TREE_OPERAND (arg0, 0);
5252 TREE_READONLY (t) = TREE_READONLY (o);
5253 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
5254 }
5255 }
5256 else
5257 TREE_THIS_VOLATILE (t)
5258 = (TREE_CODE_CLASS (code) == tcc_reference
5259 && arg0 && TREE_THIS_VOLATILE (arg0));
5260
5261 return t;
5262}
5263
5264/* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
5265 on the pointer PTR. */
5266
5267tree
5268build_simple_mem_ref_loc (location_t loc, tree ptr)
5269{
5270 poly_int64 offset = 0;
5271 tree ptype = TREE_TYPE (ptr);
5272 tree tem;
5273 /* For convenience allow addresses that collapse to a simple base
5274 and offset. */
5275 if (TREE_CODE (ptr) == ADDR_EXPR
5276 && (handled_component_p (TREE_OPERAND (ptr, 0))
5277 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
5278 {
5279 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
5280 gcc_assert (ptr);
5281 if (TREE_CODE (ptr) == MEM_REF)
5282 {
5283 offset += mem_ref_offset (ptr).force_shwi ();
5284 ptr = TREE_OPERAND (ptr, 0);
5285 }
5286 else
5287 ptr = build_fold_addr_expr (ptr);
5288 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
5289 }
5290 tem = build2 (code: MEM_REF, TREE_TYPE (ptype),
5291 arg0: ptr, arg1: build_int_cst (type: ptype, cst: offset));
5292 SET_EXPR_LOCATION (tem, loc);
5293 return tem;
5294}
5295
5296/* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
5297
5298poly_offset_int
5299mem_ref_offset (const_tree t)
5300{
5301 return poly_offset_int::from (a: wi::to_poly_wide (TREE_OPERAND (t, 1)),
5302 sgn: SIGNED);
5303}
5304
5305/* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
5306 offsetted by OFFSET units. */
5307
5308tree
5309build_invariant_address (tree type, tree base, poly_int64 offset)
5310{
5311 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
5312 build_fold_addr_expr (base),
5313 build_int_cst (ptr_type_node, offset));
5314 tree addr = build1 (code: ADDR_EXPR, type, node: ref);
5315 recompute_tree_invariant_for_addr_expr (t: addr);
5316 return addr;
5317}
5318
5319/* Similar except don't specify the TREE_TYPE
5320 and leave the TREE_SIDE_EFFECTS as 0.
5321 It is permissible for arguments to be null,
5322 or even garbage if their values do not matter. */
5323
5324tree
5325build_nt (enum tree_code code, ...)
5326{
5327 tree t;
5328 int length;
5329 int i;
5330 va_list p;
5331
5332 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
5333
5334 va_start (p, code);
5335
5336 t = make_node (code);
5337 length = TREE_CODE_LENGTH (code);
5338
5339 for (i = 0; i < length; i++)
5340 TREE_OPERAND (t, i) = va_arg (p, tree);
5341
5342 va_end (p);
5343 return t;
5344}
5345
5346/* Similar to build_nt, but for creating a CALL_EXPR object with a
5347 tree vec. */
5348
5349tree
5350build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
5351{
5352 tree ret, t;
5353 unsigned int ix;
5354
5355 ret = build_vl_exp (CALL_EXPR, vec_safe_length (v: args) + 3);
5356 CALL_EXPR_FN (ret) = fn;
5357 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
5358 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
5359 CALL_EXPR_ARG (ret, ix) = t;
5360 return ret;
5361}
5362
5363/* Create a DECL_... node of code CODE, name NAME (if non-null)
5364 and data type TYPE.
5365 We do NOT enter this node in any sort of symbol table.
5366
5367 LOC is the location of the decl.
5368
5369 layout_decl is used to set up the decl's storage layout.
5370 Other slots are initialized to 0 or null pointers. */
5371
5372tree
5373build_decl (location_t loc, enum tree_code code, tree name,
5374 tree type MEM_STAT_DECL)
5375{
5376 tree t;
5377
5378 t = make_node (code PASS_MEM_STAT);
5379 DECL_SOURCE_LOCATION (t) = loc;
5380
5381/* if (type == error_mark_node)
5382 type = integer_type_node; */
5383/* That is not done, deliberately, so that having error_mark_node
5384 as the type can suppress useless errors in the use of this variable. */
5385
5386 DECL_NAME (t) = name;
5387 TREE_TYPE (t) = type;
5388
5389 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
5390 layout_decl (t, 0);
5391
5392 return t;
5393}
5394
5395/* Create and return a DEBUG_EXPR_DECL node of the given TYPE. */
5396
5397tree
5398build_debug_expr_decl (tree type)
5399{
5400 tree vexpr = make_node (code: DEBUG_EXPR_DECL);
5401 DECL_ARTIFICIAL (vexpr) = 1;
5402 TREE_TYPE (vexpr) = type;
5403 SET_DECL_MODE (vexpr, TYPE_MODE (type));
5404 return vexpr;
5405}
5406
5407/* Builds and returns function declaration with NAME and TYPE. */
5408
5409tree
5410build_fn_decl (const char *name, tree type)
5411{
5412 tree id = get_identifier (name);
5413 tree decl = build_decl (loc: input_location, code: FUNCTION_DECL, name: id, type);
5414
5415 DECL_EXTERNAL (decl) = 1;
5416 TREE_PUBLIC (decl) = 1;
5417 DECL_ARTIFICIAL (decl) = 1;
5418 TREE_NOTHROW (decl) = 1;
5419
5420 return decl;
5421}
5422
5423vec<tree, va_gc> *all_translation_units;
5424
5425/* Builds a new translation-unit decl with name NAME, queues it in the
5426 global list of translation-unit decls and returns it. */
5427
5428tree
5429build_translation_unit_decl (tree name)
5430{
5431 tree tu = build_decl (UNKNOWN_LOCATION, code: TRANSLATION_UNIT_DECL,
5432 name, NULL_TREE);
5433 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
5434 vec_safe_push (v&: all_translation_units, obj: tu);
5435 return tu;
5436}
5437
5438
5439/* BLOCK nodes are used to represent the structure of binding contours
5440 and declarations, once those contours have been exited and their contents
5441 compiled. This information is used for outputting debugging info. */
5442
5443tree
5444build_block (tree vars, tree subblocks, tree supercontext, tree chain)
5445{
5446 tree block = make_node (code: BLOCK);
5447
5448 BLOCK_VARS (block) = vars;
5449 BLOCK_SUBBLOCKS (block) = subblocks;
5450 BLOCK_SUPERCONTEXT (block) = supercontext;
5451 BLOCK_CHAIN (block) = chain;
5452 return block;
5453}
5454
5455
5456/* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
5457
5458 LOC is the location to use in tree T. */
5459
5460void
5461protected_set_expr_location (tree t, location_t loc)
5462{
5463 if (CAN_HAVE_LOCATION_P (t))
5464 SET_EXPR_LOCATION (t, loc);
5465 else if (t && TREE_CODE (t) == STATEMENT_LIST)
5466 {
5467 t = expr_single (t);
5468 if (t && CAN_HAVE_LOCATION_P (t))
5469 SET_EXPR_LOCATION (t, loc);
5470 }
5471}
5472
5473/* Like PROTECTED_SET_EXPR_LOCATION, but only do that if T has
5474 UNKNOWN_LOCATION. */
5475
5476void
5477protected_set_expr_location_if_unset (tree t, location_t loc)
5478{
5479 t = expr_single (t);
5480 if (t && !EXPR_HAS_LOCATION (t))
5481 protected_set_expr_location (t, loc);
5482}
5483
5484/* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
5485 of the various TYPE_QUAL values. */
5486
5487static void
5488set_type_quals (tree type, int type_quals)
5489{
5490 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
5491 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
5492 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
5493 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
5494 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
5495}
5496
5497/* Returns true iff CAND and BASE have equivalent language-specific
5498 qualifiers. */
5499
5500bool
5501check_lang_type (const_tree cand, const_tree base)
5502{
5503 if (lang_hooks.types.type_hash_eq == NULL)
5504 return true;
5505 /* type_hash_eq currently only applies to these types. */
5506 if (TREE_CODE (cand) != FUNCTION_TYPE
5507 && TREE_CODE (cand) != METHOD_TYPE)
5508 return true;
5509 return lang_hooks.types.type_hash_eq (cand, base);
5510}
5511
5512/* This function checks to see if TYPE matches the size one of the built-in
5513 atomic types, and returns that core atomic type. */
5514
5515static tree
5516find_atomic_core_type (const_tree type)
5517{
5518 tree base_atomic_type;
5519
5520 /* Only handle complete types. */
5521 if (!tree_fits_uhwi_p (TYPE_SIZE (type)))
5522 return NULL_TREE;
5523
5524 switch (tree_to_uhwi (TYPE_SIZE (type)))
5525 {
5526 case 8:
5527 base_atomic_type = atomicQI_type_node;
5528 break;
5529
5530 case 16:
5531 base_atomic_type = atomicHI_type_node;
5532 break;
5533
5534 case 32:
5535 base_atomic_type = atomicSI_type_node;
5536 break;
5537
5538 case 64:
5539 base_atomic_type = atomicDI_type_node;
5540 break;
5541
5542 case 128:
5543 base_atomic_type = atomicTI_type_node;
5544 break;
5545
5546 default:
5547 base_atomic_type = NULL_TREE;
5548 }
5549
5550 return base_atomic_type;
5551}
5552
5553/* Returns true iff unqualified CAND and BASE are equivalent. */
5554
5555bool
5556check_base_type (const_tree cand, const_tree base)
5557{
5558 if (TYPE_NAME (cand) != TYPE_NAME (base)
5559 /* Apparently this is needed for Objective-C. */
5560 || TYPE_CONTEXT (cand) != TYPE_CONTEXT (base)
5561 || !attribute_list_equal (TYPE_ATTRIBUTES (cand),
5562 TYPE_ATTRIBUTES (base)))
5563 return false;
5564 /* Check alignment. */
5565 if (TYPE_ALIGN (cand) == TYPE_ALIGN (base)
5566 && TYPE_USER_ALIGN (cand) == TYPE_USER_ALIGN (base))
5567 return true;
5568 /* Atomic types increase minimal alignment. We must to do so as well
5569 or we get duplicated canonical types. See PR88686. */
5570 if ((TYPE_QUALS (cand) & TYPE_QUAL_ATOMIC))
5571 {
5572 /* See if this object can map to a basic atomic type. */
5573 tree atomic_type = find_atomic_core_type (type: cand);
5574 if (atomic_type && TYPE_ALIGN (atomic_type) == TYPE_ALIGN (cand))
5575 return true;
5576 }
5577 return false;
5578}
5579
5580/* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
5581
5582bool
5583check_qualified_type (const_tree cand, const_tree base, int type_quals)
5584{
5585 return (TYPE_QUALS (cand) == type_quals
5586 && check_base_type (cand, base)
5587 && check_lang_type (cand, base));
5588}
5589
5590/* Returns true iff CAND is equivalent to BASE with ALIGN. */
5591
5592static bool
5593check_aligned_type (const_tree cand, const_tree base, unsigned int align)
5594{
5595 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
5596 && TYPE_NAME (cand) == TYPE_NAME (base)
5597 /* Apparently this is needed for Objective-C. */
5598 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5599 /* Check alignment. */
5600 && TYPE_ALIGN (cand) == align
5601 /* Check this is a user-aligned type as build_aligned_type
5602 would create. */
5603 && TYPE_USER_ALIGN (cand)
5604 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5605 TYPE_ATTRIBUTES (base))
5606 && check_lang_type (cand, base));
5607}
5608
5609/* Return a version of the TYPE, qualified as indicated by the
5610 TYPE_QUALS, if one exists. If no qualified version exists yet,
5611 return NULL_TREE. */
5612
5613tree
5614get_qualified_type (tree type, int type_quals)
5615{
5616 if (TYPE_QUALS (type) == type_quals)
5617 return type;
5618
5619 tree mv = TYPE_MAIN_VARIANT (type);
5620 if (check_qualified_type (cand: mv, base: type, type_quals))
5621 return mv;
5622
5623 /* Search the chain of variants to see if there is already one there just
5624 like the one we need to have. If so, use that existing one. We must
5625 preserve the TYPE_NAME, since there is code that depends on this. */
5626 for (tree *tp = &TYPE_NEXT_VARIANT (mv); *tp; tp = &TYPE_NEXT_VARIANT (*tp))
5627 if (check_qualified_type (cand: *tp, base: type, type_quals))
5628 {
5629 /* Put the found variant at the head of the variant list so
5630 frequently searched variants get found faster. The C++ FE
5631 benefits greatly from this. */
5632 tree t = *tp;
5633 *tp = TYPE_NEXT_VARIANT (t);
5634 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
5635 TYPE_NEXT_VARIANT (mv) = t;
5636 return t;
5637 }
5638
5639 return NULL_TREE;
5640}
5641
5642/* Like get_qualified_type, but creates the type if it does not
5643 exist. This function never returns NULL_TREE. */
5644
5645tree
5646build_qualified_type (tree type, int type_quals MEM_STAT_DECL)
5647{
5648 tree t;
5649
5650 /* See if we already have the appropriate qualified variant. */
5651 t = get_qualified_type (type, type_quals);
5652
5653 /* If not, build it. */
5654 if (!t)
5655 {
5656 t = build_variant_type_copy (type PASS_MEM_STAT);
5657 set_type_quals (type: t, type_quals);
5658
5659 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
5660 {
5661 /* See if this object can map to a basic atomic type. */
5662 tree atomic_type = find_atomic_core_type (type);
5663 if (atomic_type)
5664 {
5665 /* Ensure the alignment of this type is compatible with
5666 the required alignment of the atomic type. */
5667 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
5668 SET_TYPE_ALIGN (t, TYPE_ALIGN (atomic_type));
5669 }
5670 }
5671
5672 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5673 /* Propagate structural equality. */
5674 SET_TYPE_STRUCTURAL_EQUALITY (t);
5675 else if (TYPE_CANONICAL (type) != type)
5676 /* Build the underlying canonical type, since it is different
5677 from TYPE. */
5678 {
5679 tree c = build_qualified_type (TYPE_CANONICAL (type), type_quals);
5680 TYPE_CANONICAL (t) = TYPE_CANONICAL (c);
5681 }
5682 else
5683 /* T is its own canonical type. */
5684 TYPE_CANONICAL (t) = t;
5685
5686 }
5687
5688 return t;
5689}
5690
5691/* Create a variant of type T with alignment ALIGN. */
5692
5693tree
5694build_aligned_type (tree type, unsigned int align)
5695{
5696 tree t;
5697
5698 if (TYPE_PACKED (type)
5699 || TYPE_ALIGN (type) == align)
5700 return type;
5701
5702 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5703 if (check_aligned_type (cand: t, base: type, align))
5704 return t;
5705
5706 t = build_variant_type_copy (type);
5707 SET_TYPE_ALIGN (t, align);
5708 TYPE_USER_ALIGN (t) = 1;
5709
5710 return t;
5711}
5712
5713/* Create a new distinct copy of TYPE. The new type is made its own
5714 MAIN_VARIANT. If TYPE requires structural equality checks, the
5715 resulting type requires structural equality checks; otherwise, its
5716 TYPE_CANONICAL points to itself. */
5717
5718tree
5719build_distinct_type_copy (tree type MEM_STAT_DECL)
5720{
5721 tree t = copy_node (node: type PASS_MEM_STAT);
5722
5723 TYPE_POINTER_TO (t) = 0;
5724 TYPE_REFERENCE_TO (t) = 0;
5725
5726 /* Set the canonical type either to a new equivalence class, or
5727 propagate the need for structural equality checks. */
5728 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5729 SET_TYPE_STRUCTURAL_EQUALITY (t);
5730 else
5731 TYPE_CANONICAL (t) = t;
5732
5733 /* Make it its own variant. */
5734 TYPE_MAIN_VARIANT (t) = t;
5735 TYPE_NEXT_VARIANT (t) = 0;
5736
5737 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
5738 whose TREE_TYPE is not t. This can also happen in the Ada
5739 frontend when using subtypes. */
5740
5741 return t;
5742}
5743
5744/* Create a new variant of TYPE, equivalent but distinct. This is so
5745 the caller can modify it. TYPE_CANONICAL for the return type will
5746 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
5747 are considered equal by the language itself (or that both types
5748 require structural equality checks). */
5749
5750tree
5751build_variant_type_copy (tree type MEM_STAT_DECL)
5752{
5753 tree t, m = TYPE_MAIN_VARIANT (type);
5754
5755 t = build_distinct_type_copy (type PASS_MEM_STAT);
5756
5757 /* Since we're building a variant, assume that it is a non-semantic
5758 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
5759 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
5760 /* Type variants have no alias set defined. */
5761 TYPE_ALIAS_SET (t) = -1;
5762
5763 /* Add the new type to the chain of variants of TYPE. */
5764 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
5765 TYPE_NEXT_VARIANT (m) = t;
5766 TYPE_MAIN_VARIANT (t) = m;
5767
5768 return t;
5769}
5770
5771/* Return true if the from tree in both tree maps are equal. */
5772
5773int
5774tree_map_base_eq (const void *va, const void *vb)
5775{
5776 const struct tree_map_base *const a = (const struct tree_map_base *) va,
5777 *const b = (const struct tree_map_base *) vb;
5778 return (a->from == b->from);
5779}
5780
5781/* Hash a from tree in a tree_base_map. */
5782
5783unsigned int
5784tree_map_base_hash (const void *item)
5785{
5786 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
5787}
5788
5789/* Return true if this tree map structure is marked for garbage collection
5790 purposes. We simply return true if the from tree is marked, so that this
5791 structure goes away when the from tree goes away. */
5792
5793bool
5794tree_map_base_marked_p (const void *p)
5795{
5796 return ggc_marked_p (((const struct tree_map_base *) p)->from);
5797}
5798
5799/* Hash a from tree in a tree_map. */
5800
5801unsigned int
5802tree_map_hash (const void *item)
5803{
5804 return (((const struct tree_map *) item)->hash);
5805}
5806
5807/* Hash a from tree in a tree_decl_map. */
5808
5809unsigned int
5810tree_decl_map_hash (const void *item)
5811{
5812 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
5813}
5814
5815/* Return the initialization priority for DECL. */
5816
5817priority_type
5818decl_init_priority_lookup (tree decl)
5819{
5820 symtab_node *snode = symtab_node::get (decl);
5821
5822 if (!snode)
5823 return DEFAULT_INIT_PRIORITY;
5824 return
5825 snode->get_init_priority ();
5826}
5827
5828/* Return the finalization priority for DECL. */
5829
5830priority_type
5831decl_fini_priority_lookup (tree decl)
5832{
5833 cgraph_node *node = cgraph_node::get (decl);
5834
5835 if (!node)
5836 return DEFAULT_INIT_PRIORITY;
5837 return
5838 node->get_fini_priority ();
5839}
5840
5841/* Set the initialization priority for DECL to PRIORITY. */
5842
5843void
5844decl_init_priority_insert (tree decl, priority_type priority)
5845{
5846 struct symtab_node *snode;
5847
5848 if (priority == DEFAULT_INIT_PRIORITY)
5849 {
5850 snode = symtab_node::get (decl);
5851 if (!snode)
5852 return;
5853 }
5854 else if (VAR_P (decl))
5855 snode = varpool_node::get_create (decl);
5856 else
5857 snode = cgraph_node::get_create (decl);
5858 snode->set_init_priority (priority);
5859}
5860
5861/* Set the finalization priority for DECL to PRIORITY. */
5862
5863void
5864decl_fini_priority_insert (tree decl, priority_type priority)
5865{
5866 struct cgraph_node *node;
5867
5868 if (priority == DEFAULT_INIT_PRIORITY)
5869 {
5870 node = cgraph_node::get (decl);
5871 if (!node)
5872 return;
5873 }
5874 else
5875 node = cgraph_node::get_create (decl);
5876 node->set_fini_priority (priority);
5877}
5878
5879/* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
5880
5881static void
5882print_debug_expr_statistics (void)
5883{
5884 fprintf (stderr, format: "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
5885 (long) debug_expr_for_decl->size (),
5886 (long) debug_expr_for_decl->elements (),
5887 debug_expr_for_decl->collisions ());
5888}
5889
5890/* Print out the statistics for the DECL_VALUE_EXPR hash table. */
5891
5892static void
5893print_value_expr_statistics (void)
5894{
5895 fprintf (stderr, format: "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
5896 (long) value_expr_for_decl->size (),
5897 (long) value_expr_for_decl->elements (),
5898 value_expr_for_decl->collisions ());
5899}
5900
5901/* Lookup a debug expression for FROM, and return it if we find one. */
5902
5903tree
5904decl_debug_expr_lookup (tree from)
5905{
5906 struct tree_decl_map *h, in;
5907 in.base.from = from;
5908
5909 h = debug_expr_for_decl->find_with_hash (comparable: &in, DECL_UID (from));
5910 if (h)
5911 return h->to;
5912 return NULL_TREE;
5913}
5914
5915/* Insert a mapping FROM->TO in the debug expression hashtable. */
5916
5917void
5918decl_debug_expr_insert (tree from, tree to)
5919{
5920 struct tree_decl_map *h;
5921
5922 h = ggc_alloc<tree_decl_map> ();
5923 h->base.from = from;
5924 h->to = to;
5925 *debug_expr_for_decl->find_slot_with_hash (comparable: h, DECL_UID (from), insert: INSERT) = h;
5926}
5927
5928/* Lookup a value expression for FROM, and return it if we find one. */
5929
5930tree
5931decl_value_expr_lookup (tree from)
5932{
5933 struct tree_decl_map *h, in;
5934 in.base.from = from;
5935
5936 h = value_expr_for_decl->find_with_hash (comparable: &in, DECL_UID (from));
5937 if (h)
5938 return h->to;
5939 return NULL_TREE;
5940}
5941
5942/* Insert a mapping FROM->TO in the value expression hashtable. */
5943
5944void
5945decl_value_expr_insert (tree from, tree to)
5946{
5947 struct tree_decl_map *h;
5948
5949 /* Uses of FROM shouldn't look like they happen at the location of TO. */
5950 to = protected_set_expr_location_unshare (to, UNKNOWN_LOCATION);
5951
5952 h = ggc_alloc<tree_decl_map> ();
5953 h->base.from = from;
5954 h->to = to;
5955 *value_expr_for_decl->find_slot_with_hash (comparable: h, DECL_UID (from), insert: INSERT) = h;
5956}
5957
5958/* Lookup a vector of debug arguments for FROM, and return it if we
5959 find one. */
5960
5961vec<tree, va_gc> **
5962decl_debug_args_lookup (tree from)
5963{
5964 struct tree_vec_map *h, in;
5965
5966 if (!DECL_HAS_DEBUG_ARGS_P (from))
5967 return NULL;
5968 gcc_checking_assert (debug_args_for_decl != NULL);
5969 in.base.from = from;
5970 h = debug_args_for_decl->find_with_hash (comparable: &in, DECL_UID (from));
5971 if (h)
5972 return &h->to;
5973 return NULL;
5974}
5975
5976/* Insert a mapping FROM->empty vector of debug arguments in the value
5977 expression hashtable. */
5978
5979vec<tree, va_gc> **
5980decl_debug_args_insert (tree from)
5981{
5982 struct tree_vec_map *h;
5983 tree_vec_map **loc;
5984
5985 if (DECL_HAS_DEBUG_ARGS_P (from))
5986 return decl_debug_args_lookup (from);
5987 if (debug_args_for_decl == NULL)
5988 debug_args_for_decl = hash_table<tree_vec_map_cache_hasher>::create_ggc (n: 64);
5989 h = ggc_alloc<tree_vec_map> ();
5990 h->base.from = from;
5991 h->to = NULL;
5992 loc = debug_args_for_decl->find_slot_with_hash (comparable: h, DECL_UID (from), insert: INSERT);
5993 *loc = h;
5994 DECL_HAS_DEBUG_ARGS_P (from) = 1;
5995 return &h->to;
5996}
5997
5998/* Hashing of types so that we don't make duplicates.
5999 The entry point is `type_hash_canon'. */
6000
6001/* Generate the default hash code for TYPE. This is designed for
6002 speed, rather than maximum entropy. */
6003
6004hashval_t
6005type_hash_canon_hash (tree type)
6006{
6007 inchash::hash hstate;
6008
6009 hstate.add_int (TREE_CODE (type));
6010
6011 if (TREE_TYPE (type))
6012 hstate.add_object (TYPE_HASH (TREE_TYPE (type)));
6013
6014 for (tree t = TYPE_ATTRIBUTES (type); t; t = TREE_CHAIN (t))
6015 /* Just the identifier is adequate to distinguish. */
6016 hstate.add_object (IDENTIFIER_HASH_VALUE (get_attribute_name (t)));
6017
6018 switch (TREE_CODE (type))
6019 {
6020 case METHOD_TYPE:
6021 hstate.add_object (TYPE_HASH (TYPE_METHOD_BASETYPE (type)));
6022 /* FALLTHROUGH. */
6023 case FUNCTION_TYPE:
6024 for (tree t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6025 if (TREE_VALUE (t) != error_mark_node)
6026 hstate.add_object (TYPE_HASH (TREE_VALUE (t)));
6027 break;
6028
6029 case OFFSET_TYPE:
6030 hstate.add_object (TYPE_HASH (TYPE_OFFSET_BASETYPE (type)));
6031 break;
6032
6033 case ARRAY_TYPE:
6034 {
6035 if (TYPE_DOMAIN (type))
6036 hstate.add_object (TYPE_HASH (TYPE_DOMAIN (type)));
6037 if (!AGGREGATE_TYPE_P (TREE_TYPE (type)))
6038 {
6039 unsigned typeless = TYPE_TYPELESS_STORAGE (type);
6040 hstate.add_object (obj&: typeless);
6041 }
6042 }
6043 break;
6044
6045 case INTEGER_TYPE:
6046 {
6047 tree t = TYPE_MAX_VALUE (type);
6048 if (!t)
6049 t = TYPE_MIN_VALUE (type);
6050 for (int i = 0; i < TREE_INT_CST_NUNITS (t); i++)
6051 hstate.add_object (TREE_INT_CST_ELT (t, i));
6052 break;
6053 }
6054
6055 case BITINT_TYPE:
6056 {
6057 unsigned prec = TYPE_PRECISION (type);
6058 unsigned uns = TYPE_UNSIGNED (type);
6059 hstate.add_object (obj&: prec);
6060 hstate.add_int (v: uns);
6061 break;
6062 }
6063
6064 case REAL_TYPE:
6065 case FIXED_POINT_TYPE:
6066 {
6067 unsigned prec = TYPE_PRECISION (type);
6068 hstate.add_object (obj&: prec);
6069 break;
6070 }
6071
6072 case VECTOR_TYPE:
6073 hstate.add_poly_int (v: TYPE_VECTOR_SUBPARTS (node: type));
6074 break;
6075
6076 default:
6077 break;
6078 }
6079
6080 return hstate.end ();
6081}
6082
6083/* These are the Hashtable callback functions. */
6084
6085/* Returns true iff the types are equivalent. */
6086
6087bool
6088type_cache_hasher::equal (type_hash *a, type_hash *b)
6089{
6090 /* First test the things that are the same for all types. */
6091 if (a->hash != b->hash
6092 || TREE_CODE (a->type) != TREE_CODE (b->type)
6093 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6094 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6095 TYPE_ATTRIBUTES (b->type))
6096 || (TREE_CODE (a->type) != COMPLEX_TYPE
6097 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6098 return false;
6099
6100 /* Be careful about comparing arrays before and after the element type
6101 has been completed; don't compare TYPE_ALIGN unless both types are
6102 complete. */
6103 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6104 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6105 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6106 return false;
6107
6108 switch (TREE_CODE (a->type))
6109 {
6110 case VOID_TYPE:
6111 case OPAQUE_TYPE:
6112 case COMPLEX_TYPE:
6113 case POINTER_TYPE:
6114 case REFERENCE_TYPE:
6115 case NULLPTR_TYPE:
6116 return true;
6117
6118 case VECTOR_TYPE:
6119 return known_eq (TYPE_VECTOR_SUBPARTS (a->type),
6120 TYPE_VECTOR_SUBPARTS (b->type));
6121
6122 case ENUMERAL_TYPE:
6123 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6124 && !(TYPE_VALUES (a->type)
6125 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6126 && TYPE_VALUES (b->type)
6127 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6128 && type_list_equal (TYPE_VALUES (a->type),
6129 TYPE_VALUES (b->type))))
6130 return false;
6131
6132 /* fall through */
6133
6134 case INTEGER_TYPE:
6135 case REAL_TYPE:
6136 case BOOLEAN_TYPE:
6137 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
6138 return false;
6139 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6140 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6141 TYPE_MAX_VALUE (b->type)))
6142 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6143 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6144 TYPE_MIN_VALUE (b->type))));
6145
6146 case BITINT_TYPE:
6147 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
6148 return false;
6149 return TYPE_UNSIGNED (a->type) == TYPE_UNSIGNED (b->type);
6150
6151 case FIXED_POINT_TYPE:
6152 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6153
6154 case OFFSET_TYPE:
6155 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6156
6157 case METHOD_TYPE:
6158 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6159 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6160 || (TYPE_ARG_TYPES (a->type)
6161 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6162 && TYPE_ARG_TYPES (b->type)
6163 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6164 && type_list_equal (TYPE_ARG_TYPES (a->type),
6165 TYPE_ARG_TYPES (b->type)))))
6166 break;
6167 return false;
6168 case ARRAY_TYPE:
6169 /* Don't compare TYPE_TYPELESS_STORAGE flag on aggregates,
6170 where the flag should be inherited from the element type
6171 and can change after ARRAY_TYPEs are created; on non-aggregates
6172 compare it and hash it, scalars will never have that flag set
6173 and we need to differentiate between arrays created by different
6174 front-ends or middle-end created arrays. */
6175 return (TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type)
6176 && (AGGREGATE_TYPE_P (TREE_TYPE (a->type))
6177 || (TYPE_TYPELESS_STORAGE (a->type)
6178 == TYPE_TYPELESS_STORAGE (b->type))));
6179
6180 case RECORD_TYPE:
6181 case UNION_TYPE:
6182 case QUAL_UNION_TYPE:
6183 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6184 || (TYPE_FIELDS (a->type)
6185 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6186 && TYPE_FIELDS (b->type)
6187 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6188 && type_list_equal (TYPE_FIELDS (a->type),
6189 TYPE_FIELDS (b->type))));
6190
6191 case FUNCTION_TYPE:
6192 if ((TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6193 && (TYPE_NO_NAMED_ARGS_STDARG_P (a->type)
6194 == TYPE_NO_NAMED_ARGS_STDARG_P (b->type)))
6195 || (TYPE_ARG_TYPES (a->type)
6196 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6197 && TYPE_ARG_TYPES (b->type)
6198 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6199 && type_list_equal (TYPE_ARG_TYPES (a->type),
6200 TYPE_ARG_TYPES (b->type))))
6201 break;
6202 return false;
6203
6204 default:
6205 return false;
6206 }
6207
6208 if (lang_hooks.types.type_hash_eq != NULL)
6209 return lang_hooks.types.type_hash_eq (a->type, b->type);
6210
6211 return true;
6212}
6213
6214/* Given TYPE, and HASHCODE its hash code, return the canonical
6215 object for an identical type if one already exists.
6216 Otherwise, return TYPE, and record it as the canonical object.
6217
6218 To use this function, first create a type of the sort you want.
6219 Then compute its hash code from the fields of the type that
6220 make it different from other similar types.
6221 Then call this function and use the value. */
6222
6223tree
6224type_hash_canon (unsigned int hashcode, tree type)
6225{
6226 type_hash in;
6227 type_hash **loc;
6228
6229 /* The hash table only contains main variants, so ensure that's what we're
6230 being passed. */
6231 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6232
6233 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6234 must call that routine before comparing TYPE_ALIGNs. */
6235 layout_type (type);
6236
6237 in.hash = hashcode;
6238 in.type = type;
6239
6240 loc = type_hash_table->find_slot_with_hash (comparable: &in, hash: hashcode, insert: INSERT);
6241 if (*loc)
6242 {
6243 tree t1 = ((type_hash *) *loc)->type;
6244 gcc_assert (TYPE_MAIN_VARIANT (t1) == t1
6245 && t1 != type);
6246 if (TYPE_UID (type) + 1 == next_type_uid)
6247 --next_type_uid;
6248 /* Free also min/max values and the cache for integer
6249 types. This can't be done in free_node, as LTO frees
6250 those on its own. */
6251 if (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == BITINT_TYPE)
6252 {
6253 if (TYPE_MIN_VALUE (type)
6254 && TREE_TYPE (TYPE_MIN_VALUE (type)) == type)
6255 {
6256 /* Zero is always in TYPE_CACHED_VALUES. */
6257 if (! TYPE_UNSIGNED (type))
6258 int_cst_hash_table->remove_elt (TYPE_MIN_VALUE (type));
6259 ggc_free (TYPE_MIN_VALUE (type));
6260 }
6261 if (TYPE_MAX_VALUE (type)
6262 && TREE_TYPE (TYPE_MAX_VALUE (type)) == type)
6263 {
6264 int_cst_hash_table->remove_elt (TYPE_MAX_VALUE (type));
6265 ggc_free (TYPE_MAX_VALUE (type));
6266 }
6267 if (TYPE_CACHED_VALUES_P (type))
6268 ggc_free (TYPE_CACHED_VALUES (type));
6269 }
6270 free_node (node: type);
6271 return t1;
6272 }
6273 else
6274 {
6275 struct type_hash *h;
6276
6277 h = ggc_alloc<type_hash> ();
6278 h->hash = hashcode;
6279 h->type = type;
6280 *loc = h;
6281
6282 return type;
6283 }
6284}
6285
6286static void
6287print_type_hash_statistics (void)
6288{
6289 fprintf (stderr, format: "Type hash: size %ld, %ld elements, %f collisions\n",
6290 (long) type_hash_table->size (),
6291 (long) type_hash_table->elements (),
6292 type_hash_table->collisions ());
6293}
6294
6295/* Given two lists of types
6296 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6297 return 1 if the lists contain the same types in the same order.
6298 Also, the TREE_PURPOSEs must match. */
6299
6300bool
6301type_list_equal (const_tree l1, const_tree l2)
6302{
6303 const_tree t1, t2;
6304
6305 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6306 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6307 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6308 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6309 && (TREE_TYPE (TREE_PURPOSE (t1))
6310 == TREE_TYPE (TREE_PURPOSE (t2))))))
6311 return false;
6312
6313 return t1 == t2;
6314}
6315
6316/* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6317 given by TYPE. If the argument list accepts variable arguments,
6318 then this function counts only the ordinary arguments. */
6319
6320int
6321type_num_arguments (const_tree fntype)
6322{
6323 int i = 0;
6324
6325 for (tree t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
6326 /* If the function does not take a variable number of arguments,
6327 the last element in the list will have type `void'. */
6328 if (VOID_TYPE_P (TREE_VALUE (t)))
6329 break;
6330 else
6331 ++i;
6332
6333 return i;
6334}
6335
6336/* Return the type of the function TYPE's argument ARGNO if known.
6337 For vararg function's where ARGNO refers to one of the variadic
6338 arguments return null. Otherwise, return a void_type_node for
6339 out-of-bounds ARGNO. */
6340
6341tree
6342type_argument_type (const_tree fntype, unsigned argno)
6343{
6344 /* Treat zero the same as an out-of-bounds argument number. */
6345 if (!argno)
6346 return void_type_node;
6347
6348 function_args_iterator iter;
6349
6350 tree argtype;
6351 unsigned i = 1;
6352 FOREACH_FUNCTION_ARGS (fntype, argtype, iter)
6353 {
6354 /* A vararg function's argument list ends in a null. Otherwise,
6355 an ordinary function's argument list ends with void. Return
6356 null if ARGNO refers to a vararg argument, void_type_node if
6357 it's out of bounds, and the formal argument type otherwise. */
6358 if (!argtype)
6359 break;
6360
6361 if (i == argno || VOID_TYPE_P (argtype))
6362 return argtype;
6363
6364 ++i;
6365 }
6366
6367 return NULL_TREE;
6368}
6369
6370/* True if integer constants T1 and T2
6371 represent the same constant value. */
6372
6373bool
6374tree_int_cst_equal (const_tree t1, const_tree t2)
6375{
6376 if (t1 == t2)
6377 return true;
6378
6379 if (t1 == 0 || t2 == 0)
6380 return false;
6381
6382 STRIP_ANY_LOCATION_WRAPPER (t1);
6383 STRIP_ANY_LOCATION_WRAPPER (t2);
6384
6385 if (TREE_CODE (t1) == INTEGER_CST
6386 && TREE_CODE (t2) == INTEGER_CST
6387 && wi::to_widest (t: t1) == wi::to_widest (t: t2))
6388 return true;
6389
6390 return false;
6391}
6392
6393/* Return true if T is an INTEGER_CST whose numerical value (extended
6394 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
6395
6396bool
6397tree_fits_shwi_p (const_tree t)
6398{
6399 return (t != NULL_TREE
6400 && TREE_CODE (t) == INTEGER_CST
6401 && wi::fits_shwi_p (x: wi::to_widest (t)));
6402}
6403
6404/* Return true if T is an INTEGER_CST or POLY_INT_CST whose numerical
6405 value (extended according to TYPE_UNSIGNED) fits in a poly_int64. */
6406
6407bool
6408tree_fits_poly_int64_p (const_tree t)
6409{
6410 if (t == NULL_TREE)
6411 return false;
6412 if (POLY_INT_CST_P (t))
6413 {
6414 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; i++)
6415 if (!wi::fits_shwi_p (x: wi::to_wide (POLY_INT_CST_COEFF (t, i))))
6416 return false;
6417 return true;
6418 }
6419 return (TREE_CODE (t) == INTEGER_CST
6420 && wi::fits_shwi_p (x: wi::to_widest (t)));
6421}
6422
6423/* Return true if T is an INTEGER_CST whose numerical value (extended
6424 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
6425
6426bool
6427tree_fits_uhwi_p (const_tree t)
6428{
6429 return (t != NULL_TREE
6430 && TREE_CODE (t) == INTEGER_CST
6431 && wi::fits_uhwi_p (x: wi::to_widest (t)));
6432}
6433
6434/* Return true if T is an INTEGER_CST or POLY_INT_CST whose numerical
6435 value (extended according to TYPE_UNSIGNED) fits in a poly_uint64. */
6436
6437bool
6438tree_fits_poly_uint64_p (const_tree t)
6439{
6440 if (t == NULL_TREE)
6441 return false;
6442 if (POLY_INT_CST_P (t))
6443 {
6444 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; i++)
6445 if (!wi::fits_uhwi_p (x: wi::to_widest (POLY_INT_CST_COEFF (t, i))))
6446 return false;
6447 return true;
6448 }
6449 return (TREE_CODE (t) == INTEGER_CST
6450 && wi::fits_uhwi_p (x: wi::to_widest (t)));
6451}
6452
6453/* T is an INTEGER_CST whose numerical value (extended according to
6454 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
6455 HOST_WIDE_INT. */
6456
6457HOST_WIDE_INT
6458tree_to_shwi (const_tree t)
6459{
6460 gcc_assert (tree_fits_shwi_p (t));
6461 return TREE_INT_CST_LOW (t);
6462}
6463
6464/* T is an INTEGER_CST whose numerical value (extended according to
6465 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
6466 HOST_WIDE_INT. */
6467
6468unsigned HOST_WIDE_INT
6469tree_to_uhwi (const_tree t)
6470{
6471 gcc_assert (tree_fits_uhwi_p (t));
6472 return TREE_INT_CST_LOW (t);
6473}
6474
6475/* Return the most significant (sign) bit of T. */
6476
6477int
6478tree_int_cst_sign_bit (const_tree t)
6479{
6480 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
6481
6482 return wi::extract_uhwi (x: wi::to_wide (t), bitpos: bitno, width: 1);
6483}
6484
6485/* Return an indication of the sign of the integer constant T.
6486 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
6487 Note that -1 will never be returned if T's type is unsigned. */
6488
6489int
6490tree_int_cst_sgn (const_tree t)
6491{
6492 if (wi::to_wide (t) == 0)
6493 return 0;
6494 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
6495 return 1;
6496 else if (wi::neg_p (x: wi::to_wide (t)))
6497 return -1;
6498 else
6499 return 1;
6500}
6501
6502/* Return the minimum number of bits needed to represent VALUE in a
6503 signed or unsigned type, UNSIGNEDP says which. */
6504
6505unsigned int
6506tree_int_cst_min_precision (tree value, signop sgn)
6507{
6508 /* If the value is negative, compute its negative minus 1. The latter
6509 adjustment is because the absolute value of the largest negative value
6510 is one larger than the largest positive value. This is equivalent to
6511 a bit-wise negation, so use that operation instead. */
6512
6513 if (tree_int_cst_sgn (t: value) < 0)
6514 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
6515
6516 /* Return the number of bits needed, taking into account the fact
6517 that we need one more bit for a signed than unsigned type.
6518 If value is 0 or -1, the minimum precision is 1 no matter
6519 whether unsignedp is true or false. */
6520
6521 if (integer_zerop (expr: value))
6522 return 1;
6523 else
6524 return tree_floor_log2 (expr: value) + 1 + (sgn == SIGNED ? 1 : 0) ;
6525}
6526
6527/* Return truthvalue of whether T1 is the same tree structure as T2.
6528 Return 1 if they are the same.
6529 Return 0 if they are understandably different.
6530 Return -1 if either contains tree structure not understood by
6531 this function. */
6532
6533int
6534simple_cst_equal (const_tree t1, const_tree t2)
6535{
6536 enum tree_code code1, code2;
6537 int cmp;
6538 int i;
6539
6540 if (t1 == t2)
6541 return 1;
6542 if (t1 == 0 || t2 == 0)
6543 return 0;
6544
6545 /* For location wrappers to be the same, they must be at the same
6546 source location (and wrap the same thing). */
6547 if (location_wrapper_p (exp: t1) && location_wrapper_p (exp: t2))
6548 {
6549 if (EXPR_LOCATION (t1) != EXPR_LOCATION (t2))
6550 return 0;
6551 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6552 }
6553
6554 code1 = TREE_CODE (t1);
6555 code2 = TREE_CODE (t2);
6556
6557 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
6558 {
6559 if (CONVERT_EXPR_CODE_P (code2)
6560 || code2 == NON_LVALUE_EXPR)
6561 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6562 else
6563 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
6564 }
6565
6566 else if (CONVERT_EXPR_CODE_P (code2)
6567 || code2 == NON_LVALUE_EXPR)
6568 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
6569
6570 if (code1 != code2)
6571 return 0;
6572
6573 switch (code1)
6574 {
6575 case INTEGER_CST:
6576 return wi::to_widest (t: t1) == wi::to_widest (t: t2);
6577
6578 case REAL_CST:
6579 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
6580
6581 case FIXED_CST:
6582 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
6583
6584 case STRING_CST:
6585 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
6586 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
6587 TREE_STRING_LENGTH (t1)));
6588
6589 case CONSTRUCTOR:
6590 {
6591 unsigned HOST_WIDE_INT idx;
6592 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
6593 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
6594
6595 if (vec_safe_length (v: v1) != vec_safe_length (v: v2))
6596 return false;
6597
6598 for (idx = 0; idx < vec_safe_length (v: v1); ++idx)
6599 /* ??? Should we handle also fields here? */
6600 if (!simple_cst_equal (t1: (*v1)[idx].value, t2: (*v2)[idx].value))
6601 return false;
6602 return true;
6603 }
6604
6605 case SAVE_EXPR:
6606 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6607
6608 case CALL_EXPR:
6609 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
6610 if (cmp <= 0)
6611 return cmp;
6612 if (call_expr_nargs (t1) != call_expr_nargs (t2))
6613 return 0;
6614 {
6615 const_tree arg1, arg2;
6616 const_call_expr_arg_iterator iter1, iter2;
6617 for (arg1 = first_const_call_expr_arg (exp: t1, iter: &iter1),
6618 arg2 = first_const_call_expr_arg (exp: t2, iter: &iter2);
6619 arg1 && arg2;
6620 arg1 = next_const_call_expr_arg (iter: &iter1),
6621 arg2 = next_const_call_expr_arg (iter: &iter2))
6622 {
6623 cmp = simple_cst_equal (t1: arg1, t2: arg2);
6624 if (cmp <= 0)
6625 return cmp;
6626 }
6627 return arg1 == arg2;
6628 }
6629
6630 case TARGET_EXPR:
6631 /* Special case: if either target is an unallocated VAR_DECL,
6632 it means that it's going to be unified with whatever the
6633 TARGET_EXPR is really supposed to initialize, so treat it
6634 as being equivalent to anything. */
6635 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
6636 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
6637 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
6638 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
6639 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
6640 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
6641 cmp = 1;
6642 else
6643 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6644
6645 if (cmp <= 0)
6646 return cmp;
6647
6648 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
6649
6650 case WITH_CLEANUP_EXPR:
6651 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6652 if (cmp <= 0)
6653 return cmp;
6654
6655 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
6656
6657 case COMPONENT_REF:
6658 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
6659 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6660
6661 return 0;
6662
6663 case VAR_DECL:
6664 case PARM_DECL:
6665 case CONST_DECL:
6666 case FUNCTION_DECL:
6667 return 0;
6668
6669 default:
6670 if (POLY_INT_CST_P (t1))
6671 /* A false return means maybe_ne rather than known_ne. */
6672 return known_eq (poly_widest_int::from (poly_int_cst_value (t1),
6673 TYPE_SIGN (TREE_TYPE (t1))),
6674 poly_widest_int::from (poly_int_cst_value (t2),
6675 TYPE_SIGN (TREE_TYPE (t2))));
6676 break;
6677 }
6678
6679 /* This general rule works for most tree codes. All exceptions should be
6680 handled above. If this is a language-specific tree code, we can't
6681 trust what might be in the operand, so say we don't know
6682 the situation. */
6683 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
6684 return -1;
6685
6686 switch (TREE_CODE_CLASS (code1))
6687 {
6688 case tcc_unary:
6689 case tcc_binary:
6690 case tcc_comparison:
6691 case tcc_expression:
6692 case tcc_reference:
6693 case tcc_statement:
6694 cmp = 1;
6695 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
6696 {
6697 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
6698 if (cmp <= 0)
6699 return cmp;
6700 }
6701
6702 return cmp;
6703
6704 default:
6705 return -1;
6706 }
6707}
6708
6709/* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
6710 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
6711 than U, respectively. */
6712
6713int
6714compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
6715{
6716 if (tree_int_cst_sgn (t) < 0)
6717 return -1;
6718 else if (!tree_fits_uhwi_p (t))
6719 return 1;
6720 else if (TREE_INT_CST_LOW (t) == u)
6721 return 0;
6722 else if (TREE_INT_CST_LOW (t) < u)
6723 return -1;
6724 else
6725 return 1;
6726}
6727
6728/* Return true if SIZE represents a constant size that is in bounds of
6729 what the middle-end and the backend accepts (covering not more than
6730 half of the address-space).
6731 When PERR is non-null, set *PERR on failure to the description of
6732 why SIZE is not valid. */
6733
6734bool
6735valid_constant_size_p (const_tree size, cst_size_error *perr /* = NULL */)
6736{
6737 if (POLY_INT_CST_P (size))
6738 {
6739 if (TREE_OVERFLOW (size))
6740 return false;
6741 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
6742 if (!valid_constant_size_p (POLY_INT_CST_COEFF (size, i)))
6743 return false;
6744 return true;
6745 }
6746
6747 cst_size_error error;
6748 if (!perr)
6749 perr = &error;
6750
6751 if (TREE_CODE (size) != INTEGER_CST)
6752 {
6753 *perr = cst_size_not_constant;
6754 return false;
6755 }
6756
6757 if (TREE_OVERFLOW_P (size))
6758 {
6759 *perr = cst_size_overflow;
6760 return false;
6761 }
6762
6763 if (tree_int_cst_sgn (t: size) < 0)
6764 {
6765 *perr = cst_size_negative;
6766 return false;
6767 }
6768 if (!tree_fits_uhwi_p (t: size)
6769 || (wi::to_widest (TYPE_MAX_VALUE (sizetype))
6770 < wi::to_widest (t: size) * 2))
6771 {
6772 *perr = cst_size_too_big;
6773 return false;
6774 }
6775
6776 return true;
6777}
6778
6779/* Return the precision of the type, or for a complex or vector type the
6780 precision of the type of its elements. */
6781
6782unsigned int
6783element_precision (const_tree type)
6784{
6785 if (!TYPE_P (type))
6786 type = TREE_TYPE (type);
6787 enum tree_code code = TREE_CODE (type);
6788 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
6789 type = TREE_TYPE (type);
6790
6791 return TYPE_PRECISION (type);
6792}
6793
6794/* Return true if CODE represents an associative tree code. Otherwise
6795 return false. */
6796bool
6797associative_tree_code (enum tree_code code)
6798{
6799 switch (code)
6800 {
6801 case BIT_IOR_EXPR:
6802 case BIT_AND_EXPR:
6803 case BIT_XOR_EXPR:
6804 case PLUS_EXPR:
6805 case MULT_EXPR:
6806 case MIN_EXPR:
6807 case MAX_EXPR:
6808 return true;
6809
6810 default:
6811 break;
6812 }
6813 return false;
6814}
6815
6816/* Return true if CODE represents a commutative tree code. Otherwise
6817 return false. */
6818bool
6819commutative_tree_code (enum tree_code code)
6820{
6821 switch (code)
6822 {
6823 case PLUS_EXPR:
6824 case MULT_EXPR:
6825 case MULT_HIGHPART_EXPR:
6826 case MIN_EXPR:
6827 case MAX_EXPR:
6828 case BIT_IOR_EXPR:
6829 case BIT_XOR_EXPR:
6830 case BIT_AND_EXPR:
6831 case NE_EXPR:
6832 case EQ_EXPR:
6833 case UNORDERED_EXPR:
6834 case ORDERED_EXPR:
6835 case UNEQ_EXPR:
6836 case LTGT_EXPR:
6837 case TRUTH_AND_EXPR:
6838 case TRUTH_XOR_EXPR:
6839 case TRUTH_OR_EXPR:
6840 case WIDEN_MULT_EXPR:
6841 case VEC_WIDEN_MULT_HI_EXPR:
6842 case VEC_WIDEN_MULT_LO_EXPR:
6843 case VEC_WIDEN_MULT_EVEN_EXPR:
6844 case VEC_WIDEN_MULT_ODD_EXPR:
6845 return true;
6846
6847 default:
6848 break;
6849 }
6850 return false;
6851}
6852
6853/* Return true if CODE represents a ternary tree code for which the
6854 first two operands are commutative. Otherwise return false. */
6855bool
6856commutative_ternary_tree_code (enum tree_code code)
6857{
6858 switch (code)
6859 {
6860 case WIDEN_MULT_PLUS_EXPR:
6861 case WIDEN_MULT_MINUS_EXPR:
6862 case DOT_PROD_EXPR:
6863 return true;
6864
6865 default:
6866 break;
6867 }
6868 return false;
6869}
6870
6871/* Returns true if CODE can overflow. */
6872
6873bool
6874operation_can_overflow (enum tree_code code)
6875{
6876 switch (code)
6877 {
6878 case PLUS_EXPR:
6879 case MINUS_EXPR:
6880 case MULT_EXPR:
6881 case LSHIFT_EXPR:
6882 /* Can overflow in various ways. */
6883 return true;
6884 case TRUNC_DIV_EXPR:
6885 case EXACT_DIV_EXPR:
6886 case FLOOR_DIV_EXPR:
6887 case CEIL_DIV_EXPR:
6888 /* For INT_MIN / -1. */
6889 return true;
6890 case NEGATE_EXPR:
6891 case ABS_EXPR:
6892 /* For -INT_MIN. */
6893 return true;
6894 default:
6895 /* These operators cannot overflow. */
6896 return false;
6897 }
6898}
6899
6900/* Returns true if CODE operating on operands of type TYPE doesn't overflow, or
6901 ftrapv doesn't generate trapping insns for CODE. */
6902
6903bool
6904operation_no_trapping_overflow (tree type, enum tree_code code)
6905{
6906 gcc_checking_assert (ANY_INTEGRAL_TYPE_P (type));
6907
6908 /* We don't generate instructions that trap on overflow for complex or vector
6909 types. */
6910 if (!INTEGRAL_TYPE_P (type))
6911 return true;
6912
6913 if (!TYPE_OVERFLOW_TRAPS (type))
6914 return true;
6915
6916 switch (code)
6917 {
6918 case PLUS_EXPR:
6919 case MINUS_EXPR:
6920 case MULT_EXPR:
6921 case NEGATE_EXPR:
6922 case ABS_EXPR:
6923 /* These operators can overflow, and -ftrapv generates trapping code for
6924 these. */
6925 return false;
6926 case TRUNC_DIV_EXPR:
6927 case EXACT_DIV_EXPR:
6928 case FLOOR_DIV_EXPR:
6929 case CEIL_DIV_EXPR:
6930 case LSHIFT_EXPR:
6931 /* These operators can overflow, but -ftrapv does not generate trapping
6932 code for these. */
6933 return true;
6934 default:
6935 /* These operators cannot overflow. */
6936 return true;
6937 }
6938}
6939
6940/* Constructors for pointer, array and function types.
6941 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
6942 constructed by language-dependent code, not here.) */
6943
6944/* Construct, lay out and return the type of pointers to TO_TYPE with
6945 mode MODE. If MODE is VOIDmode, a pointer mode for the address
6946 space of TO_TYPE will be picked. If CAN_ALIAS_ALL is TRUE,
6947 indicate this type can reference all of memory. If such a type has
6948 already been constructed, reuse it. */
6949
6950tree
6951build_pointer_type_for_mode (tree to_type, machine_mode mode,
6952 bool can_alias_all)
6953{
6954 tree t;
6955 bool could_alias = can_alias_all;
6956
6957 if (to_type == error_mark_node)
6958 return error_mark_node;
6959
6960 if (mode == VOIDmode)
6961 {
6962 addr_space_t as = TYPE_ADDR_SPACE (to_type);
6963 mode = targetm.addr_space.pointer_mode (as);
6964 }
6965
6966 /* If the pointed-to type has the may_alias attribute set, force
6967 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
6968 if (lookup_attribute (attr_name: "may_alias", TYPE_ATTRIBUTES (to_type)))
6969 can_alias_all = true;
6970
6971 /* In some cases, languages will have things that aren't a POINTER_TYPE
6972 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
6973 In that case, return that type without regard to the rest of our
6974 operands.
6975
6976 ??? This is a kludge, but consistent with the way this function has
6977 always operated and there doesn't seem to be a good way to avoid this
6978 at the moment. */
6979 if (TYPE_POINTER_TO (to_type) != 0
6980 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
6981 return TYPE_POINTER_TO (to_type);
6982
6983 /* First, if we already have a type for pointers to TO_TYPE and it's
6984 the proper mode, use it. */
6985 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
6986 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
6987 return t;
6988
6989 t = make_node (code: POINTER_TYPE);
6990
6991 TREE_TYPE (t) = to_type;
6992 SET_TYPE_MODE (t, mode);
6993 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
6994 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
6995 TYPE_POINTER_TO (to_type) = t;
6996
6997 /* During LTO we do not set TYPE_CANONICAL of pointers and references. */
6998 if (TYPE_STRUCTURAL_EQUALITY_P (to_type) || in_lto_p)
6999 SET_TYPE_STRUCTURAL_EQUALITY (t);
7000 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
7001 TYPE_CANONICAL (t)
7002 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7003 mode, can_alias_all: false);
7004
7005 /* Lay out the type. This function has many callers that are concerned
7006 with expression-construction, and this simplifies them all. */
7007 layout_type (t);
7008
7009 return t;
7010}
7011
7012/* By default build pointers in ptr_mode. */
7013
7014tree
7015build_pointer_type (tree to_type)
7016{
7017 return build_pointer_type_for_mode (to_type, VOIDmode, can_alias_all: false);
7018}
7019
7020/* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7021
7022tree
7023build_reference_type_for_mode (tree to_type, machine_mode mode,
7024 bool can_alias_all)
7025{
7026 tree t;
7027 bool could_alias = can_alias_all;
7028
7029 if (to_type == error_mark_node)
7030 return error_mark_node;
7031
7032 if (mode == VOIDmode)
7033 {
7034 addr_space_t as = TYPE_ADDR_SPACE (to_type);
7035 mode = targetm.addr_space.pointer_mode (as);
7036 }
7037
7038 /* If the pointed-to type has the may_alias attribute set, force
7039 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7040 if (lookup_attribute (attr_name: "may_alias", TYPE_ATTRIBUTES (to_type)))
7041 can_alias_all = true;
7042
7043 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7044 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7045 In that case, return that type without regard to the rest of our
7046 operands.
7047
7048 ??? This is a kludge, but consistent with the way this function has
7049 always operated and there doesn't seem to be a good way to avoid this
7050 at the moment. */
7051 if (TYPE_REFERENCE_TO (to_type) != 0
7052 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7053 return TYPE_REFERENCE_TO (to_type);
7054
7055 /* First, if we already have a type for pointers to TO_TYPE and it's
7056 the proper mode, use it. */
7057 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7058 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7059 return t;
7060
7061 t = make_node (code: REFERENCE_TYPE);
7062
7063 TREE_TYPE (t) = to_type;
7064 SET_TYPE_MODE (t, mode);
7065 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7066 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7067 TYPE_REFERENCE_TO (to_type) = t;
7068
7069 /* During LTO we do not set TYPE_CANONICAL of pointers and references. */
7070 if (TYPE_STRUCTURAL_EQUALITY_P (to_type) || in_lto_p)
7071 SET_TYPE_STRUCTURAL_EQUALITY (t);
7072 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
7073 TYPE_CANONICAL (t)
7074 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7075 mode, can_alias_all: false);
7076
7077 layout_type (t);
7078
7079 return t;
7080}
7081
7082
7083/* Build the node for the type of references-to-TO_TYPE by default
7084 in ptr_mode. */
7085
7086tree
7087build_reference_type (tree to_type)
7088{
7089 return build_reference_type_for_mode (to_type, VOIDmode, can_alias_all: false);
7090}
7091
7092#define MAX_INT_CACHED_PREC \
7093 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7094static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7095
7096static void
7097clear_nonstandard_integer_type_cache (void)
7098{
7099 for (size_t i = 0 ; i < 2 * MAX_INT_CACHED_PREC + 2 ; i++)
7100 {
7101 nonstandard_integer_type_cache[i] = NULL;
7102 }
7103}
7104
7105/* Builds a signed or unsigned integer type of precision PRECISION.
7106 Used for C bitfields whose precision does not match that of
7107 built-in target types. */
7108tree
7109build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7110 int unsignedp)
7111{
7112 tree itype, ret;
7113
7114 if (unsignedp)
7115 unsignedp = MAX_INT_CACHED_PREC + 1;
7116
7117 if (precision <= MAX_INT_CACHED_PREC)
7118 {
7119 itype = nonstandard_integer_type_cache[precision + unsignedp];
7120 if (itype)
7121 return itype;
7122 }
7123
7124 itype = make_node (code: INTEGER_TYPE);
7125 TYPE_PRECISION (itype) = precision;
7126
7127 if (unsignedp)
7128 fixup_unsigned_type (itype);
7129 else
7130 fixup_signed_type (itype);
7131
7132 inchash::hash hstate;
7133 inchash::add_expr (TYPE_MAX_VALUE (itype), hstate);
7134 ret = type_hash_canon (hashcode: hstate.end (), type: itype);
7135 if (precision <= MAX_INT_CACHED_PREC)
7136 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7137
7138 return ret;
7139}
7140
7141#define MAX_BOOL_CACHED_PREC \
7142 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7143static GTY(()) tree nonstandard_boolean_type_cache[MAX_BOOL_CACHED_PREC + 1];
7144
7145/* Builds a boolean type of precision PRECISION.
7146 Used for boolean vectors to choose proper vector element size. */
7147tree
7148build_nonstandard_boolean_type (unsigned HOST_WIDE_INT precision)
7149{
7150 tree type;
7151
7152 if (precision <= MAX_BOOL_CACHED_PREC)
7153 {
7154 type = nonstandard_boolean_type_cache[precision];
7155 if (type)
7156 return type;
7157 }
7158
7159 type = make_node (code: BOOLEAN_TYPE);
7160 TYPE_PRECISION (type) = precision;
7161 fixup_signed_type (type);
7162
7163 if (precision <= MAX_INT_CACHED_PREC)
7164 nonstandard_boolean_type_cache[precision] = type;
7165
7166 return type;
7167}
7168
7169static GTY(()) vec<tree, va_gc> *bitint_type_cache;
7170
7171/* Builds a signed or unsigned _BitInt(PRECISION) type. */
7172tree
7173build_bitint_type (unsigned HOST_WIDE_INT precision, int unsignedp)
7174{
7175 tree itype, ret;
7176
7177 gcc_checking_assert (precision >= 1 + !unsignedp);
7178
7179 if (unsignedp)
7180 unsignedp = MAX_INT_CACHED_PREC + 1;
7181
7182 if (bitint_type_cache == NULL)
7183 vec_safe_grow_cleared (v&: bitint_type_cache, len: 2 * MAX_INT_CACHED_PREC + 2);
7184
7185 if (precision <= MAX_INT_CACHED_PREC)
7186 {
7187 itype = (*bitint_type_cache)[precision + unsignedp];
7188 if (itype)
7189 return itype;
7190 }
7191
7192 itype = make_node (code: BITINT_TYPE);
7193 TYPE_PRECISION (itype) = precision;
7194
7195 if (unsignedp)
7196 fixup_unsigned_type (itype);
7197 else
7198 fixup_signed_type (itype);
7199
7200 inchash::hash hstate;
7201 inchash::add_expr (TYPE_MAX_VALUE (itype), hstate);
7202 ret = type_hash_canon (hashcode: hstate.end (), type: itype);
7203 if (precision <= MAX_INT_CACHED_PREC)
7204 (*bitint_type_cache)[precision + unsignedp] = ret;
7205
7206 return ret;
7207}
7208
7209/* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7210 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7211 is true, reuse such a type that has already been constructed. */
7212
7213static tree
7214build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7215{
7216 tree itype = make_node (code: INTEGER_TYPE);
7217
7218 TREE_TYPE (itype) = type;
7219
7220 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7221 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7222
7223 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7224 SET_TYPE_MODE (itype, TYPE_MODE (type));
7225 TYPE_SIZE (itype) = TYPE_SIZE (type);
7226 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7227 SET_TYPE_ALIGN (itype, TYPE_ALIGN (type));
7228 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7229 SET_TYPE_WARN_IF_NOT_ALIGN (itype, TYPE_WARN_IF_NOT_ALIGN (type));
7230
7231 if (!shared)
7232 return itype;
7233
7234 if ((TYPE_MIN_VALUE (itype)
7235 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7236 || (TYPE_MAX_VALUE (itype)
7237 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7238 {
7239 /* Since we cannot reliably merge this type, we need to compare it using
7240 structural equality checks. */
7241 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7242 return itype;
7243 }
7244
7245 hashval_t hash = type_hash_canon_hash (type: itype);
7246 itype = type_hash_canon (hashcode: hash, type: itype);
7247
7248 return itype;
7249}
7250
7251/* Wrapper around build_range_type_1 with SHARED set to true. */
7252
7253tree
7254build_range_type (tree type, tree lowval, tree highval)
7255{
7256 return build_range_type_1 (type, lowval, highval, shared: true);
7257}
7258
7259/* Wrapper around build_range_type_1 with SHARED set to false. */
7260
7261tree
7262build_nonshared_range_type (tree type, tree lowval, tree highval)
7263{
7264 return build_range_type_1 (type, lowval, highval, shared: false);
7265}
7266
7267/* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7268 MAXVAL should be the maximum value in the domain
7269 (one less than the length of the array).
7270
7271 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7272 We don't enforce this limit, that is up to caller (e.g. language front end).
7273 The limit exists because the result is a signed type and we don't handle
7274 sizes that use more than one HOST_WIDE_INT. */
7275
7276tree
7277build_index_type (tree maxval)
7278{
7279 return build_range_type (sizetype, size_zero_node, highval: maxval);
7280}
7281
7282/* Return true if the debug information for TYPE, a subtype, should be emitted
7283 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7284 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7285 debug info and doesn't reflect the source code. */
7286
7287bool
7288subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7289{
7290 tree base_type = TREE_TYPE (type), low, high;
7291
7292 /* Subrange types have a base type which is an integral type. */
7293 if (!INTEGRAL_TYPE_P (base_type))
7294 return false;
7295
7296 /* Get the real bounds of the subtype. */
7297 if (lang_hooks.types.get_subrange_bounds)
7298 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7299 else
7300 {
7301 low = TYPE_MIN_VALUE (type);
7302 high = TYPE_MAX_VALUE (type);
7303 }
7304
7305 /* If the type and its base type have the same representation and the same
7306 name, then the type is not a subrange but a copy of the base type. */
7307 if ((TREE_CODE (base_type) == INTEGER_TYPE
7308 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7309 && int_size_in_bytes (type) == int_size_in_bytes (type: base_type)
7310 && tree_int_cst_equal (t1: low, TYPE_MIN_VALUE (base_type))
7311 && tree_int_cst_equal (t1: high, TYPE_MAX_VALUE (base_type))
7312 && TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (base_type))
7313 return false;
7314
7315 if (lowval)
7316 *lowval = low;
7317 if (highval)
7318 *highval = high;
7319 return true;
7320}
7321
7322/* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7323 and number of elements specified by the range of values of INDEX_TYPE.
7324 If TYPELESS_STORAGE is true, TYPE_TYPELESS_STORAGE flag is set on the type.
7325 If SHARED is true, reuse such a type that has already been constructed.
7326 If SET_CANONICAL is true, compute TYPE_CANONICAL from the element type. */
7327
7328tree
7329build_array_type_1 (tree elt_type, tree index_type, bool typeless_storage,
7330 bool shared, bool set_canonical)
7331{
7332 tree t;
7333
7334 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7335 {
7336 error ("arrays of functions are not meaningful");
7337 elt_type = integer_type_node;
7338 }
7339
7340 t = make_node (code: ARRAY_TYPE);
7341 TREE_TYPE (t) = elt_type;
7342 TYPE_DOMAIN (t) = index_type;
7343 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7344 TYPE_TYPELESS_STORAGE (t) = typeless_storage;
7345 layout_type (t);
7346
7347 if (shared)
7348 {
7349 hashval_t hash = type_hash_canon_hash (type: t);
7350 t = type_hash_canon (hashcode: hash, type: t);
7351 }
7352
7353 if (TYPE_CANONICAL (t) == t && set_canonical)
7354 {
7355 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7356 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type))
7357 || in_lto_p)
7358 SET_TYPE_STRUCTURAL_EQUALITY (t);
7359 else if (TYPE_CANONICAL (elt_type) != elt_type
7360 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7361 TYPE_CANONICAL (t)
7362 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7363 index_type: index_type
7364 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7365 typeless_storage, shared, set_canonical);
7366 }
7367
7368 return t;
7369}
7370
7371/* Wrapper around build_array_type_1 with SHARED set to true. */
7372
7373tree
7374build_array_type (tree elt_type, tree index_type, bool typeless_storage)
7375{
7376 return
7377 build_array_type_1 (elt_type, index_type, typeless_storage, shared: true, set_canonical: true);
7378}
7379
7380/* Wrapper around build_array_type_1 with SHARED set to false. */
7381
7382tree
7383build_nonshared_array_type (tree elt_type, tree index_type)
7384{
7385 return build_array_type_1 (elt_type, index_type, typeless_storage: false, shared: false, set_canonical: true);
7386}
7387
7388/* Return a representation of ELT_TYPE[NELTS], using indices of type
7389 sizetype. */
7390
7391tree
7392build_array_type_nelts (tree elt_type, poly_uint64 nelts)
7393{
7394 return build_array_type (elt_type, index_type: build_index_type (size_int (nelts - 1)));
7395}
7396
7397/* Computes the canonical argument types from the argument type list
7398 ARGTYPES.
7399
7400 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7401 on entry to this function, or if any of the ARGTYPES are
7402 structural.
7403
7404 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7405 true on entry to this function, or if any of the ARGTYPES are
7406 non-canonical.
7407
7408 Returns a canonical argument list, which may be ARGTYPES when the
7409 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7410 true) or would not differ from ARGTYPES. */
7411
7412static tree
7413maybe_canonicalize_argtypes (tree argtypes,
7414 bool *any_structural_p,
7415 bool *any_noncanonical_p)
7416{
7417 tree arg;
7418 bool any_noncanonical_argtypes_p = false;
7419
7420 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7421 {
7422 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7423 /* Fail gracefully by stating that the type is structural. */
7424 *any_structural_p = true;
7425 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7426 *any_structural_p = true;
7427 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7428 || TREE_PURPOSE (arg))
7429 /* If the argument has a default argument, we consider it
7430 non-canonical even though the type itself is canonical.
7431 That way, different variants of function and method types
7432 with default arguments will all point to the variant with
7433 no defaults as their canonical type. */
7434 any_noncanonical_argtypes_p = true;
7435 }
7436
7437 if (*any_structural_p)
7438 return argtypes;
7439
7440 if (any_noncanonical_argtypes_p)
7441 {
7442 /* Build the canonical list of argument types. */
7443 tree canon_argtypes = NULL_TREE;
7444 bool is_void = false;
7445
7446 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7447 {
7448 if (arg == void_list_node)
7449 is_void = true;
7450 else
7451 canon_argtypes = tree_cons (NULL_TREE,
7452 TYPE_CANONICAL (TREE_VALUE (arg)),
7453 chain: canon_argtypes);
7454 }
7455
7456 canon_argtypes = nreverse (t: canon_argtypes);
7457 if (is_void)
7458 canon_argtypes = chainon (op1: canon_argtypes, void_list_node);
7459
7460 /* There is a non-canonical type. */
7461 *any_noncanonical_p = true;
7462 return canon_argtypes;
7463 }
7464
7465 /* The canonical argument types are the same as ARGTYPES. */
7466 return argtypes;
7467}
7468
7469/* Construct, lay out and return
7470 the type of functions returning type VALUE_TYPE
7471 given arguments of types ARG_TYPES.
7472 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7473 are data type nodes for the arguments of the function.
7474 NO_NAMED_ARGS_STDARG_P is true if this is a prototyped
7475 variable-arguments function with (...) prototype (no named arguments).
7476 If such a type has already been constructed, reuse it. */
7477
7478tree
7479build_function_type (tree value_type, tree arg_types,
7480 bool no_named_args_stdarg_p)
7481{
7482 tree t;
7483 inchash::hash hstate;
7484 bool any_structural_p, any_noncanonical_p;
7485 tree canon_argtypes;
7486
7487 gcc_assert (arg_types != error_mark_node);
7488
7489 if (TREE_CODE (value_type) == FUNCTION_TYPE)
7490 {
7491 error ("function return type cannot be function");
7492 value_type = integer_type_node;
7493 }
7494
7495 /* Make a node of the sort we want. */
7496 t = make_node (code: FUNCTION_TYPE);
7497 TREE_TYPE (t) = value_type;
7498 TYPE_ARG_TYPES (t) = arg_types;
7499 if (no_named_args_stdarg_p)
7500 {
7501 gcc_assert (arg_types == NULL_TREE);
7502 TYPE_NO_NAMED_ARGS_STDARG_P (t) = 1;
7503 }
7504
7505 /* If we already have such a type, use the old one. */
7506 hashval_t hash = type_hash_canon_hash (type: t);
7507 t = type_hash_canon (hashcode: hash, type: t);
7508
7509 /* Set up the canonical type. */
7510 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7511 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7512 canon_argtypes = maybe_canonicalize_argtypes (argtypes: arg_types,
7513 any_structural_p: &any_structural_p,
7514 any_noncanonical_p: &any_noncanonical_p);
7515 if (any_structural_p)
7516 SET_TYPE_STRUCTURAL_EQUALITY (t);
7517 else if (any_noncanonical_p)
7518 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
7519 arg_types: canon_argtypes);
7520
7521 if (!COMPLETE_TYPE_P (t))
7522 layout_type (t);
7523 return t;
7524}
7525
7526/* Build a function type. The RETURN_TYPE is the type returned by the
7527 function. If VAARGS is set, no void_type_node is appended to the
7528 list. ARGP must be always be terminated be a NULL_TREE. */
7529
7530static tree
7531build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
7532{
7533 tree t, args, last;
7534
7535 t = va_arg (argp, tree);
7536 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
7537 args = tree_cons (NULL_TREE, value: t, chain: args);
7538
7539 if (vaargs)
7540 {
7541 last = args;
7542 if (args != NULL_TREE)
7543 args = nreverse (t: args);
7544 gcc_assert (last != void_list_node);
7545 }
7546 else if (args == NULL_TREE)
7547 args = void_list_node;
7548 else
7549 {
7550 last = args;
7551 args = nreverse (t: args);
7552 TREE_CHAIN (last) = void_list_node;
7553 }
7554 args = build_function_type (value_type: return_type, arg_types: args, no_named_args_stdarg_p: vaargs && args == NULL_TREE);
7555
7556 return args;
7557}
7558
7559/* Build a function type. The RETURN_TYPE is the type returned by the
7560 function. If additional arguments are provided, they are
7561 additional argument types. The list of argument types must always
7562 be terminated by NULL_TREE. */
7563
7564tree
7565build_function_type_list (tree return_type, ...)
7566{
7567 tree args;
7568 va_list p;
7569
7570 va_start (p, return_type);
7571 args = build_function_type_list_1 (vaargs: false, return_type, argp: p);
7572 va_end (p);
7573 return args;
7574}
7575
7576/* Build a variable argument function type. The RETURN_TYPE is the
7577 type returned by the function. If additional arguments are provided,
7578 they are additional argument types. The list of argument types must
7579 always be terminated by NULL_TREE. */
7580
7581tree
7582build_varargs_function_type_list (tree return_type, ...)
7583{
7584 tree args;
7585 va_list p;
7586
7587 va_start (p, return_type);
7588 args = build_function_type_list_1 (vaargs: true, return_type, argp: p);
7589 va_end (p);
7590
7591 return args;
7592}
7593
7594/* Build a function type. RETURN_TYPE is the type returned by the
7595 function; VAARGS indicates whether the function takes varargs. The
7596 function takes N named arguments, the types of which are provided in
7597 ARG_TYPES. */
7598
7599static tree
7600build_function_type_array_1 (bool vaargs, tree return_type, int n,
7601 tree *arg_types)
7602{
7603 int i;
7604 tree t = vaargs ? NULL_TREE : void_list_node;
7605
7606 for (i = n - 1; i >= 0; i--)
7607 t = tree_cons (NULL_TREE, value: arg_types[i], chain: t);
7608
7609 return build_function_type (value_type: return_type, arg_types: t, no_named_args_stdarg_p: vaargs && n == 0);
7610}
7611
7612/* Build a function type. RETURN_TYPE is the type returned by the
7613 function. The function takes N named arguments, the types of which
7614 are provided in ARG_TYPES. */
7615
7616tree
7617build_function_type_array (tree return_type, int n, tree *arg_types)
7618{
7619 return build_function_type_array_1 (vaargs: false, return_type, n, arg_types);
7620}
7621
7622/* Build a variable argument function type. RETURN_TYPE is the type
7623 returned by the function. The function takes N named arguments, the
7624 types of which are provided in ARG_TYPES. */
7625
7626tree
7627build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
7628{
7629 return build_function_type_array_1 (vaargs: true, return_type, n, arg_types);
7630}
7631
7632/* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
7633 and ARGTYPES (a TREE_LIST) are the return type and arguments types
7634 for the method. An implicit additional parameter (of type
7635 pointer-to-BASETYPE) is added to the ARGTYPES. */
7636
7637tree
7638build_method_type_directly (tree basetype,
7639 tree rettype,
7640 tree argtypes)
7641{
7642 tree t;
7643 tree ptype;
7644 bool any_structural_p, any_noncanonical_p;
7645 tree canon_argtypes;
7646
7647 /* Make a node of the sort we want. */
7648 t = make_node (code: METHOD_TYPE);
7649
7650 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7651 TREE_TYPE (t) = rettype;
7652 ptype = build_pointer_type (to_type: basetype);
7653
7654 /* The actual arglist for this function includes a "hidden" argument
7655 which is "this". Put it into the list of argument types. */
7656 argtypes = tree_cons (NULL_TREE, value: ptype, chain: argtypes);
7657 TYPE_ARG_TYPES (t) = argtypes;
7658
7659 /* If we already have such a type, use the old one. */
7660 hashval_t hash = type_hash_canon_hash (type: t);
7661 t = type_hash_canon (hashcode: hash, type: t);
7662
7663 /* Set up the canonical type. */
7664 any_structural_p
7665 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7666 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
7667 any_noncanonical_p
7668 = (TYPE_CANONICAL (basetype) != basetype
7669 || TYPE_CANONICAL (rettype) != rettype);
7670 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
7671 any_structural_p: &any_structural_p,
7672 any_noncanonical_p: &any_noncanonical_p);
7673 if (any_structural_p)
7674 SET_TYPE_STRUCTURAL_EQUALITY (t);
7675 else if (any_noncanonical_p)
7676 TYPE_CANONICAL (t)
7677 = build_method_type_directly (TYPE_CANONICAL (basetype),
7678 TYPE_CANONICAL (rettype),
7679 argtypes: canon_argtypes);
7680 if (!COMPLETE_TYPE_P (t))
7681 layout_type (t);
7682
7683 return t;
7684}
7685
7686/* Construct, lay out and return the type of methods belonging to class
7687 BASETYPE and whose arguments and values are described by TYPE.
7688 If that type exists already, reuse it.
7689 TYPE must be a FUNCTION_TYPE node. */
7690
7691tree
7692build_method_type (tree basetype, tree type)
7693{
7694 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
7695
7696 return build_method_type_directly (basetype,
7697 TREE_TYPE (type),
7698 TYPE_ARG_TYPES (type));
7699}
7700
7701/* Construct, lay out and return the type of offsets to a value
7702 of type TYPE, within an object of type BASETYPE.
7703 If a suitable offset type exists already, reuse it. */
7704
7705tree
7706build_offset_type (tree basetype, tree type)
7707{
7708 tree t;
7709
7710 /* Make a node of the sort we want. */
7711 t = make_node (code: OFFSET_TYPE);
7712
7713 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7714 TREE_TYPE (t) = type;
7715
7716 /* If we already have such a type, use the old one. */
7717 hashval_t hash = type_hash_canon_hash (type: t);
7718 t = type_hash_canon (hashcode: hash, type: t);
7719
7720 if (!COMPLETE_TYPE_P (t))
7721 layout_type (t);
7722
7723 if (TYPE_CANONICAL (t) == t)
7724 {
7725 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7726 || TYPE_STRUCTURAL_EQUALITY_P (type))
7727 SET_TYPE_STRUCTURAL_EQUALITY (t);
7728 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
7729 || TYPE_CANONICAL (type) != type)
7730 TYPE_CANONICAL (t)
7731 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
7732 TYPE_CANONICAL (type));
7733 }
7734
7735 return t;
7736}
7737
7738/* Create a complex type whose components are COMPONENT_TYPE.
7739
7740 If NAMED is true, the type is given a TYPE_NAME. We do not always
7741 do so because this creates a DECL node and thus make the DECL_UIDs
7742 dependent on the type canonicalization hashtable, which is GC-ed,
7743 so the DECL_UIDs would not be stable wrt garbage collection. */
7744
7745tree
7746build_complex_type (tree component_type, bool named)
7747{
7748 gcc_assert (INTEGRAL_TYPE_P (component_type)
7749 || SCALAR_FLOAT_TYPE_P (component_type)
7750 || FIXED_POINT_TYPE_P (component_type));
7751
7752 /* Make a node of the sort we want. */
7753 tree probe = make_node (code: COMPLEX_TYPE);
7754
7755 TREE_TYPE (probe) = TYPE_MAIN_VARIANT (component_type);
7756
7757 /* If we already have such a type, use the old one. */
7758 hashval_t hash = type_hash_canon_hash (type: probe);
7759 tree t = type_hash_canon (hashcode: hash, type: probe);
7760
7761 if (t == probe)
7762 {
7763 /* We created a new type. The hash insertion will have laid
7764 out the type. We need to check the canonicalization and
7765 maybe set the name. */
7766 gcc_checking_assert (COMPLETE_TYPE_P (t)
7767 && !TYPE_NAME (t)
7768 && TYPE_CANONICAL (t) == t);
7769
7770 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (t)))
7771 SET_TYPE_STRUCTURAL_EQUALITY (t);
7772 else if (TYPE_CANONICAL (TREE_TYPE (t)) != TREE_TYPE (t))
7773 TYPE_CANONICAL (t)
7774 = build_complex_type (TYPE_CANONICAL (TREE_TYPE (t)), named);
7775
7776 /* We need to create a name, since complex is a fundamental type. */
7777 if (named)
7778 {
7779 const char *name = NULL;
7780
7781 if (TREE_TYPE (t) == char_type_node)
7782 name = "complex char";
7783 else if (TREE_TYPE (t) == signed_char_type_node)
7784 name = "complex signed char";
7785 else if (TREE_TYPE (t) == unsigned_char_type_node)
7786 name = "complex unsigned char";
7787 else if (TREE_TYPE (t) == short_integer_type_node)
7788 name = "complex short int";
7789 else if (TREE_TYPE (t) == short_unsigned_type_node)
7790 name = "complex short unsigned int";
7791 else if (TREE_TYPE (t) == integer_type_node)
7792 name = "complex int";
7793 else if (TREE_TYPE (t) == unsigned_type_node)
7794 name = "complex unsigned int";
7795 else if (TREE_TYPE (t) == long_integer_type_node)
7796 name = "complex long int";
7797 else if (TREE_TYPE (t) == long_unsigned_type_node)
7798 name = "complex long unsigned int";
7799 else if (TREE_TYPE (t) == long_long_integer_type_node)
7800 name = "complex long long int";
7801 else if (TREE_TYPE (t) == long_long_unsigned_type_node)
7802 name = "complex long long unsigned int";
7803
7804 if (name != NULL)
7805 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, code: TYPE_DECL,
7806 get_identifier (name), type: t);
7807 }
7808 }
7809
7810 return build_qualified_type (type: t, TYPE_QUALS (component_type));
7811}
7812
7813/* If TYPE is a real or complex floating-point type and the target
7814 does not directly support arithmetic on TYPE then return the wider
7815 type to be used for arithmetic on TYPE. Otherwise, return
7816 NULL_TREE. */
7817
7818tree
7819excess_precision_type (tree type)
7820{
7821 /* The target can give two different responses to the question of
7822 which excess precision mode it would like depending on whether we
7823 are in -fexcess-precision=standard or -fexcess-precision=fast. */
7824
7825 enum excess_precision_type requested_type
7826 = (flag_excess_precision == EXCESS_PRECISION_FAST
7827 ? EXCESS_PRECISION_TYPE_FAST
7828 : (flag_excess_precision == EXCESS_PRECISION_FLOAT16
7829 ? EXCESS_PRECISION_TYPE_FLOAT16 : EXCESS_PRECISION_TYPE_STANDARD));
7830
7831 enum flt_eval_method target_flt_eval_method
7832 = targetm.c.excess_precision (requested_type);
7833
7834 /* The target should not ask for unpredictable float evaluation (though
7835 it might advertise that implicitly the evaluation is unpredictable,
7836 but we don't care about that here, it will have been reported
7837 elsewhere). If it does ask for unpredictable evaluation, we have
7838 nothing to do here. */
7839 gcc_assert (target_flt_eval_method != FLT_EVAL_METHOD_UNPREDICTABLE);
7840
7841 /* Nothing to do. The target has asked for all types we know about
7842 to be computed with their native precision and range. */
7843 if (target_flt_eval_method == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
7844 return NULL_TREE;
7845
7846 /* The target will promote this type in a target-dependent way, so excess
7847 precision ought to leave it alone. */
7848 if (targetm.promoted_type (type) != NULL_TREE)
7849 return NULL_TREE;
7850
7851 machine_mode float16_type_mode = (float16_type_node
7852 ? TYPE_MODE (float16_type_node)
7853 : VOIDmode);
7854 machine_mode bfloat16_type_mode = (bfloat16_type_node
7855 ? TYPE_MODE (bfloat16_type_node)
7856 : VOIDmode);
7857 machine_mode float_type_mode = TYPE_MODE (float_type_node);
7858 machine_mode double_type_mode = TYPE_MODE (double_type_node);
7859
7860 switch (TREE_CODE (type))
7861 {
7862 case REAL_TYPE:
7863 {
7864 machine_mode type_mode = TYPE_MODE (type);
7865 switch (target_flt_eval_method)
7866 {
7867 case FLT_EVAL_METHOD_PROMOTE_TO_FLOAT:
7868 if (type_mode == float16_type_mode
7869 || type_mode == bfloat16_type_mode)
7870 return float_type_node;
7871 break;
7872 case FLT_EVAL_METHOD_PROMOTE_TO_DOUBLE:
7873 if (type_mode == float16_type_mode
7874 || type_mode == bfloat16_type_mode
7875 || type_mode == float_type_mode)
7876 return double_type_node;
7877 break;
7878 case FLT_EVAL_METHOD_PROMOTE_TO_LONG_DOUBLE:
7879 if (type_mode == float16_type_mode
7880 || type_mode == bfloat16_type_mode
7881 || type_mode == float_type_mode
7882 || type_mode == double_type_mode)
7883 return long_double_type_node;
7884 break;
7885 default:
7886 gcc_unreachable ();
7887 }
7888 break;
7889 }
7890 case COMPLEX_TYPE:
7891 {
7892 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
7893 return NULL_TREE;
7894 machine_mode type_mode = TYPE_MODE (TREE_TYPE (type));
7895 switch (target_flt_eval_method)
7896 {
7897 case FLT_EVAL_METHOD_PROMOTE_TO_FLOAT:
7898 if (type_mode == float16_type_mode
7899 || type_mode == bfloat16_type_mode)
7900 return complex_float_type_node;
7901 break;
7902 case FLT_EVAL_METHOD_PROMOTE_TO_DOUBLE:
7903 if (type_mode == float16_type_mode
7904 || type_mode == bfloat16_type_mode
7905 || type_mode == float_type_mode)
7906 return complex_double_type_node;
7907 break;
7908 case FLT_EVAL_METHOD_PROMOTE_TO_LONG_DOUBLE:
7909 if (type_mode == float16_type_mode
7910 || type_mode == bfloat16_type_mode
7911 || type_mode == float_type_mode
7912 || type_mode == double_type_mode)
7913 return complex_long_double_type_node;
7914 break;
7915 default:
7916 gcc_unreachable ();
7917 }
7918 break;
7919 }
7920 default:
7921 break;
7922 }
7923
7924 return NULL_TREE;
7925}
7926
7927/* Return OP, stripped of any conversions to wider types as much as is safe.
7928 Converting the value back to OP's type makes a value equivalent to OP.
7929
7930 If FOR_TYPE is nonzero, we return a value which, if converted to
7931 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
7932
7933 OP must have integer, real or enumeral type. Pointers are not allowed!
7934
7935 There are some cases where the obvious value we could return
7936 would regenerate to OP if converted to OP's type,
7937 but would not extend like OP to wider types.
7938 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
7939 For example, if OP is (unsigned short)(signed char)-1,
7940 we avoid returning (signed char)-1 if FOR_TYPE is int,
7941 even though extending that to an unsigned short would regenerate OP,
7942 since the result of extending (signed char)-1 to (int)
7943 is different from (int) OP. */
7944
7945tree
7946get_unwidened (tree op, tree for_type)
7947{
7948 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
7949 tree type = TREE_TYPE (op);
7950 unsigned final_prec
7951 = TYPE_PRECISION (for_type != 0 ? for_type : type);
7952 int uns
7953 = (for_type != 0 && for_type != type
7954 && final_prec > TYPE_PRECISION (type)
7955 && TYPE_UNSIGNED (type));
7956 tree win = op;
7957
7958 while (CONVERT_EXPR_P (op))
7959 {
7960 int bitschange;
7961
7962 /* TYPE_PRECISION on vector types has different meaning
7963 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
7964 so avoid them here. */
7965 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
7966 break;
7967
7968 bitschange = TYPE_PRECISION (TREE_TYPE (op))
7969 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
7970
7971 /* Truncations are many-one so cannot be removed.
7972 Unless we are later going to truncate down even farther. */
7973 if (bitschange < 0
7974 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
7975 break;
7976
7977 /* See what's inside this conversion. If we decide to strip it,
7978 we will set WIN. */
7979 op = TREE_OPERAND (op, 0);
7980
7981 /* If we have not stripped any zero-extensions (uns is 0),
7982 we can strip any kind of extension.
7983 If we have previously stripped a zero-extension,
7984 only zero-extensions can safely be stripped.
7985 Any extension can be stripped if the bits it would produce
7986 are all going to be discarded later by truncating to FOR_TYPE. */
7987
7988 if (bitschange > 0)
7989 {
7990 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
7991 win = op;
7992 /* TYPE_UNSIGNED says whether this is a zero-extension.
7993 Let's avoid computing it if it does not affect WIN
7994 and if UNS will not be needed again. */
7995 if ((uns
7996 || CONVERT_EXPR_P (op))
7997 && TYPE_UNSIGNED (TREE_TYPE (op)))
7998 {
7999 uns = 1;
8000 win = op;
8001 }
8002 }
8003 }
8004
8005 /* If we finally reach a constant see if it fits in sth smaller and
8006 in that case convert it. */
8007 if (TREE_CODE (win) == INTEGER_CST)
8008 {
8009 tree wtype = TREE_TYPE (win);
8010 unsigned prec = wi::min_precision (x: wi::to_wide (t: win), TYPE_SIGN (wtype));
8011 if (for_type)
8012 prec = MAX (prec, final_prec);
8013 if (prec < TYPE_PRECISION (wtype))
8014 {
8015 tree t = lang_hooks.types.type_for_size (prec, TYPE_UNSIGNED (wtype));
8016 if (t && TYPE_PRECISION (t) < TYPE_PRECISION (wtype))
8017 win = fold_convert (t, win);
8018 }
8019 }
8020
8021 return win;
8022}
8023
8024/* Return OP or a simpler expression for a narrower value
8025 which can be sign-extended or zero-extended to give back OP.
8026 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8027 or 0 if the value should be sign-extended. */
8028
8029tree
8030get_narrower (tree op, int *unsignedp_ptr)
8031{
8032 int uns = 0;
8033 bool first = true;
8034 tree win = op;
8035 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8036
8037 if (TREE_CODE (op) == COMPOUND_EXPR)
8038 {
8039 do
8040 op = TREE_OPERAND (op, 1);
8041 while (TREE_CODE (op) == COMPOUND_EXPR);
8042 tree ret = get_narrower (op, unsignedp_ptr);
8043 if (ret == op)
8044 return win;
8045 auto_vec <tree, 16> v;
8046 unsigned int i;
8047 for (op = win; TREE_CODE (op) == COMPOUND_EXPR;
8048 op = TREE_OPERAND (op, 1))
8049 v.safe_push (obj: op);
8050 FOR_EACH_VEC_ELT_REVERSE (v, i, op)
8051 ret = build2_loc (EXPR_LOCATION (op), code: COMPOUND_EXPR,
8052 TREE_TYPE (ret), TREE_OPERAND (op, 0),
8053 arg1: ret);
8054 return ret;
8055 }
8056 while (TREE_CODE (op) == NOP_EXPR)
8057 {
8058 int bitschange
8059 = (TYPE_PRECISION (TREE_TYPE (op))
8060 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8061
8062 /* Truncations are many-one so cannot be removed. */
8063 if (bitschange < 0)
8064 break;
8065
8066 /* See what's inside this conversion. If we decide to strip it,
8067 we will set WIN. */
8068
8069 if (bitschange > 0)
8070 {
8071 op = TREE_OPERAND (op, 0);
8072 /* An extension: the outermost one can be stripped,
8073 but remember whether it is zero or sign extension. */
8074 if (first)
8075 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8076 /* Otherwise, if a sign extension has been stripped,
8077 only sign extensions can now be stripped;
8078 if a zero extension has been stripped, only zero-extensions. */
8079 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8080 break;
8081 first = false;
8082 }
8083 else /* bitschange == 0 */
8084 {
8085 /* A change in nominal type can always be stripped, but we must
8086 preserve the unsignedness. */
8087 if (first)
8088 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8089 first = false;
8090 op = TREE_OPERAND (op, 0);
8091 /* Keep trying to narrow, but don't assign op to win if it
8092 would turn an integral type into something else. */
8093 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8094 continue;
8095 }
8096
8097 win = op;
8098 }
8099
8100 if (TREE_CODE (op) == COMPONENT_REF
8101 /* Since type_for_size always gives an integer type. */
8102 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8103 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8104 /* Ensure field is laid out already. */
8105 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8106 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
8107 {
8108 unsigned HOST_WIDE_INT innerprec
8109 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
8110 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8111 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8112 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8113
8114 /* We can get this structure field in a narrower type that fits it,
8115 but the resulting extension to its nominal type (a fullword type)
8116 must satisfy the same conditions as for other extensions.
8117
8118 Do this only for fields that are aligned (not bit-fields),
8119 because when bit-field insns will be used there is no
8120 advantage in doing this. */
8121
8122 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8123 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8124 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8125 && type != 0)
8126 {
8127 if (first)
8128 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8129 win = fold_convert (type, op);
8130 }
8131 }
8132
8133 *unsignedp_ptr = uns;
8134 return win;
8135}
8136
8137/* Return true if integer constant C has a value that is permissible
8138 for TYPE, an integral type. */
8139
8140bool
8141int_fits_type_p (const_tree c, const_tree type)
8142{
8143 tree type_low_bound, type_high_bound;
8144 bool ok_for_low_bound, ok_for_high_bound;
8145 signop sgn_c = TYPE_SIGN (TREE_TYPE (c));
8146
8147 /* Non-standard boolean types can have arbitrary precision but various
8148 transformations assume that they can only take values 0 and +/-1. */
8149 if (TREE_CODE (type) == BOOLEAN_TYPE)
8150 return wi::fits_to_boolean_p (x: wi::to_wide (t: c), type);
8151
8152retry:
8153 type_low_bound = TYPE_MIN_VALUE (type);
8154 type_high_bound = TYPE_MAX_VALUE (type);
8155
8156 /* If at least one bound of the type is a constant integer, we can check
8157 ourselves and maybe make a decision. If no such decision is possible, but
8158 this type is a subtype, try checking against that. Otherwise, use
8159 fits_to_tree_p, which checks against the precision.
8160
8161 Compute the status for each possibly constant bound, and return if we see
8162 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8163 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8164 for "constant known to fit". */
8165
8166 /* Check if c >= type_low_bound. */
8167 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8168 {
8169 if (tree_int_cst_lt (t1: c, t2: type_low_bound))
8170 return false;
8171 ok_for_low_bound = true;
8172 }
8173 else
8174 ok_for_low_bound = false;
8175
8176 /* Check if c <= type_high_bound. */
8177 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8178 {
8179 if (tree_int_cst_lt (t1: type_high_bound, t2: c))
8180 return false;
8181 ok_for_high_bound = true;
8182 }
8183 else
8184 ok_for_high_bound = false;
8185
8186 /* If the constant fits both bounds, the result is known. */
8187 if (ok_for_low_bound && ok_for_high_bound)
8188 return true;
8189
8190 /* Perform some generic filtering which may allow making a decision
8191 even if the bounds are not constant. First, negative integers
8192 never fit in unsigned types, */
8193 if (TYPE_UNSIGNED (type) && sgn_c == SIGNED && wi::neg_p (x: wi::to_wide (t: c)))
8194 return false;
8195
8196 /* Second, narrower types always fit in wider ones. */
8197 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8198 return true;
8199
8200 /* Third, unsigned integers with top bit set never fit signed types. */
8201 if (!TYPE_UNSIGNED (type) && sgn_c == UNSIGNED)
8202 {
8203 int prec = GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (TREE_TYPE (c))) - 1;
8204 if (prec < TYPE_PRECISION (TREE_TYPE (c)))
8205 {
8206 /* When a tree_cst is converted to a wide-int, the precision
8207 is taken from the type. However, if the precision of the
8208 mode underneath the type is smaller than that, it is
8209 possible that the value will not fit. The test below
8210 fails if any bit is set between the sign bit of the
8211 underlying mode and the top bit of the type. */
8212 if (wi::zext (x: wi::to_wide (t: c), offset: prec - 1) != wi::to_wide (t: c))
8213 return false;
8214 }
8215 else if (wi::neg_p (x: wi::to_wide (t: c)))
8216 return false;
8217 }
8218
8219 /* If we haven't been able to decide at this point, there nothing more we
8220 can check ourselves here. Look at the base type if we have one and it
8221 has the same precision. */
8222 if (TREE_CODE (type) == INTEGER_TYPE
8223 && TREE_TYPE (type) != 0
8224 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8225 {
8226 type = TREE_TYPE (type);
8227 goto retry;
8228 }
8229
8230 /* Or to fits_to_tree_p, if nothing else. */
8231 return wi::fits_to_tree_p (x: wi::to_wide (t: c), type);
8232}
8233
8234/* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8235 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8236 represented (assuming two's-complement arithmetic) within the bit
8237 precision of the type are returned instead. */
8238
8239void
8240get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8241{
8242 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8243 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8244 wi::to_mpz (wi::to_wide (TYPE_MIN_VALUE (type)), min, TYPE_SIGN (type));
8245 else
8246 {
8247 if (TYPE_UNSIGNED (type))
8248 mpz_set_ui (min, 0);
8249 else
8250 {
8251 wide_int mn = wi::min_value (TYPE_PRECISION (type), SIGNED);
8252 wi::to_mpz (mn, min, SIGNED);
8253 }
8254 }
8255
8256 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8257 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8258 wi::to_mpz (wi::to_wide (TYPE_MAX_VALUE (type)), max, TYPE_SIGN (type));
8259 else
8260 {
8261 wide_int mn = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
8262 wi::to_mpz (mn, max, TYPE_SIGN (type));
8263 }
8264}
8265
8266/* Return true if VAR is an automatic variable. */
8267
8268bool
8269auto_var_p (const_tree var)
8270{
8271 return ((((VAR_P (var) && ! DECL_EXTERNAL (var))
8272 || TREE_CODE (var) == PARM_DECL)
8273 && ! TREE_STATIC (var))
8274 || TREE_CODE (var) == RESULT_DECL);
8275}
8276
8277/* Return true if VAR is an automatic variable defined in function FN. */
8278
8279bool
8280auto_var_in_fn_p (const_tree var, const_tree fn)
8281{
8282 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8283 && (auto_var_p (var)
8284 || TREE_CODE (var) == LABEL_DECL));
8285}
8286
8287/* Subprogram of following function. Called by walk_tree.
8288
8289 Return *TP if it is an automatic variable or parameter of the
8290 function passed in as DATA. */
8291
8292static tree
8293find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8294{
8295 tree fn = (tree) data;
8296
8297 if (TYPE_P (*tp))
8298 *walk_subtrees = 0;
8299
8300 else if (DECL_P (*tp)
8301 && auto_var_in_fn_p (var: *tp, fn))
8302 return *tp;
8303
8304 return NULL_TREE;
8305}
8306
8307/* Returns true if T is, contains, or refers to a type with variable
8308 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8309 arguments, but not the return type. If FN is nonzero, only return
8310 true if a modifier of the type or position of FN is a variable or
8311 parameter inside FN.
8312
8313 This concept is more general than that of C99 'variably modified types':
8314 in C99, a struct type is never variably modified because a VLA may not
8315 appear as a structure member. However, in GNU C code like:
8316
8317 struct S { int i[f()]; };
8318
8319 is valid, and other languages may define similar constructs. */
8320
8321bool
8322variably_modified_type_p (tree type, tree fn)
8323{
8324 tree t;
8325
8326/* Test if T is either variable (if FN is zero) or an expression containing
8327 a variable in FN. If TYPE isn't gimplified, return true also if
8328 gimplify_one_sizepos would gimplify the expression into a local
8329 variable. */
8330#define RETURN_TRUE_IF_VAR(T) \
8331 do { tree _t = (T); \
8332 if (_t != NULL_TREE \
8333 && _t != error_mark_node \
8334 && !CONSTANT_CLASS_P (_t) \
8335 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8336 && (!fn \
8337 || (!TYPE_SIZES_GIMPLIFIED (type) \
8338 && (TREE_CODE (_t) != VAR_DECL \
8339 && !CONTAINS_PLACEHOLDER_P (_t))) \
8340 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8341 return true; } while (0)
8342
8343 if (type == error_mark_node)
8344 return false;
8345
8346 /* If TYPE itself has variable size, it is variably modified. */
8347 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8348 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8349
8350 switch (TREE_CODE (type))
8351 {
8352 case POINTER_TYPE:
8353 case REFERENCE_TYPE:
8354 case VECTOR_TYPE:
8355 /* Ada can have pointer types refering to themselves indirectly. */
8356 if (TREE_VISITED (type))
8357 return false;
8358 TREE_VISITED (type) = true;
8359 if (variably_modified_type_p (TREE_TYPE (type), fn))
8360 {
8361 TREE_VISITED (type) = false;
8362 return true;
8363 }
8364 TREE_VISITED (type) = false;
8365 break;
8366
8367 case FUNCTION_TYPE:
8368 case METHOD_TYPE:
8369 /* If TYPE is a function type, it is variably modified if the
8370 return type is variably modified. */
8371 if (variably_modified_type_p (TREE_TYPE (type), fn))
8372 return true;
8373 break;
8374
8375 case INTEGER_TYPE:
8376 case REAL_TYPE:
8377 case FIXED_POINT_TYPE:
8378 case ENUMERAL_TYPE:
8379 case BOOLEAN_TYPE:
8380 /* Scalar types are variably modified if their end points
8381 aren't constant. */
8382 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8383 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8384 break;
8385
8386 case RECORD_TYPE:
8387 case UNION_TYPE:
8388 case QUAL_UNION_TYPE:
8389 /* We can't see if any of the fields are variably-modified by the
8390 definition we normally use, since that would produce infinite
8391 recursion via pointers. */
8392 /* This is variably modified if some field's type is. */
8393 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8394 if (TREE_CODE (t) == FIELD_DECL)
8395 {
8396 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8397 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8398 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8399
8400 /* If the type is a qualified union, then the DECL_QUALIFIER
8401 of fields can also be an expression containing a variable. */
8402 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8403 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8404
8405 /* If the field is a qualified union, then it's only a container
8406 for what's inside so we look into it. That's necessary in LTO
8407 mode because the sizes of the field tested above have been set
8408 to PLACEHOLDER_EXPRs by free_lang_data. */
8409 if (TREE_CODE (TREE_TYPE (t)) == QUAL_UNION_TYPE
8410 && variably_modified_type_p (TREE_TYPE (t), fn))
8411 return true;
8412 }
8413 break;
8414
8415 case ARRAY_TYPE:
8416 /* Do not call ourselves to avoid infinite recursion. This is
8417 variably modified if the element type is. */
8418 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8419 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8420 break;
8421
8422 default:
8423 break;
8424 }
8425
8426 /* The current language may have other cases to check, but in general,
8427 all other types are not variably modified. */
8428 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8429
8430#undef RETURN_TRUE_IF_VAR
8431}
8432
8433/* Given a DECL or TYPE, return the scope in which it was declared, or
8434 NULL_TREE if there is no containing scope. */
8435
8436tree
8437get_containing_scope (const_tree t)
8438{
8439 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8440}
8441
8442/* Returns the ultimate TRANSLATION_UNIT_DECL context of DECL or NULL. */
8443
8444const_tree
8445get_ultimate_context (const_tree decl)
8446{
8447 while (decl && TREE_CODE (decl) != TRANSLATION_UNIT_DECL)
8448 {
8449 if (TREE_CODE (decl) == BLOCK)
8450 decl = BLOCK_SUPERCONTEXT (decl);
8451 else
8452 decl = get_containing_scope (t: decl);
8453 }
8454 return decl;
8455}
8456
8457/* Return the innermost context enclosing DECL that is
8458 a FUNCTION_DECL, or zero if none. */
8459
8460tree
8461decl_function_context (const_tree decl)
8462{
8463 tree context;
8464
8465 if (TREE_CODE (decl) == ERROR_MARK)
8466 return 0;
8467
8468 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8469 where we look up the function at runtime. Such functions always take
8470 a first argument of type 'pointer to real context'.
8471
8472 C++ should really be fixed to use DECL_CONTEXT for the real context,
8473 and use something else for the "virtual context". */
8474 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
8475 context
8476 = TYPE_MAIN_VARIANT
8477 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8478 else
8479 context = DECL_CONTEXT (decl);
8480
8481 while (context && TREE_CODE (context) != FUNCTION_DECL)
8482 {
8483 if (TREE_CODE (context) == BLOCK)
8484 context = BLOCK_SUPERCONTEXT (context);
8485 else
8486 context = get_containing_scope (t: context);
8487 }
8488
8489 return context;
8490}
8491
8492/* Return the innermost context enclosing DECL that is
8493 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8494 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8495
8496tree
8497decl_type_context (const_tree decl)
8498{
8499 tree context = DECL_CONTEXT (decl);
8500
8501 while (context)
8502 switch (TREE_CODE (context))
8503 {
8504 case NAMESPACE_DECL:
8505 case TRANSLATION_UNIT_DECL:
8506 return NULL_TREE;
8507
8508 case RECORD_TYPE:
8509 case UNION_TYPE:
8510 case QUAL_UNION_TYPE:
8511 return context;
8512
8513 case TYPE_DECL:
8514 case FUNCTION_DECL:
8515 context = DECL_CONTEXT (context);
8516 break;
8517
8518 case BLOCK:
8519 context = BLOCK_SUPERCONTEXT (context);
8520 break;
8521
8522 default:
8523 gcc_unreachable ();
8524 }
8525
8526 return NULL_TREE;
8527}
8528
8529/* CALL is a CALL_EXPR. Return the declaration for the function
8530 called, or NULL_TREE if the called function cannot be
8531 determined. */
8532
8533tree
8534get_callee_fndecl (const_tree call)
8535{
8536 tree addr;
8537
8538 if (call == error_mark_node)
8539 return error_mark_node;
8540
8541 /* It's invalid to call this function with anything but a
8542 CALL_EXPR. */
8543 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8544
8545 /* The first operand to the CALL is the address of the function
8546 called. */
8547 addr = CALL_EXPR_FN (call);
8548
8549 /* If there is no function, return early. */
8550 if (addr == NULL_TREE)
8551 return NULL_TREE;
8552
8553 STRIP_NOPS (addr);
8554
8555 /* If this is a readonly function pointer, extract its initial value. */
8556 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8557 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8558 && DECL_INITIAL (addr))
8559 addr = DECL_INITIAL (addr);
8560
8561 /* If the address is just `&f' for some function `f', then we know
8562 that `f' is being called. */
8563 if (TREE_CODE (addr) == ADDR_EXPR
8564 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8565 return TREE_OPERAND (addr, 0);
8566
8567 /* We couldn't figure out what was being called. */
8568 return NULL_TREE;
8569}
8570
8571/* Return true when STMTs arguments and return value match those of FNDECL,
8572 a decl of a builtin function. */
8573
8574static bool
8575tree_builtin_call_types_compatible_p (const_tree call, tree fndecl)
8576{
8577 gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) != NOT_BUILT_IN);
8578
8579 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
8580 if (tree decl = builtin_decl_explicit (fncode: DECL_FUNCTION_CODE (decl: fndecl)))
8581 fndecl = decl;
8582
8583 bool gimple_form = (cfun && (cfun->curr_properties & PROP_gimple)) != 0;
8584 if (gimple_form
8585 ? !useless_type_conversion_p (TREE_TYPE (call),
8586 TREE_TYPE (TREE_TYPE (fndecl)))
8587 : (TYPE_MAIN_VARIANT (TREE_TYPE (call))
8588 != TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))))
8589 return false;
8590
8591 tree targs = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
8592 unsigned nargs = call_expr_nargs (call);
8593 for (unsigned i = 0; i < nargs; ++i, targs = TREE_CHAIN (targs))
8594 {
8595 /* Variadic args follow. */
8596 if (!targs)
8597 return true;
8598 tree arg = CALL_EXPR_ARG (call, i);
8599 tree type = TREE_VALUE (targs);
8600 if (gimple_form
8601 ? !useless_type_conversion_p (type, TREE_TYPE (arg))
8602 : TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (TREE_TYPE (arg)))
8603 {
8604 /* For pointer arguments be more forgiving, e.g. due to
8605 FILE * vs. fileptr_type_node, or say char * vs. const char *
8606 differences etc. */
8607 if (!gimple_form
8608 && POINTER_TYPE_P (type)
8609 && POINTER_TYPE_P (TREE_TYPE (arg))
8610 && tree_nop_conversion_p (type, TREE_TYPE (arg)))
8611 continue;
8612 /* char/short integral arguments are promoted to int
8613 by several frontends if targetm.calls.promote_prototypes
8614 is true. Allow such promotion too. */
8615 if (INTEGRAL_TYPE_P (type)
8616 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
8617 && INTEGRAL_TYPE_P (TREE_TYPE (arg))
8618 && !TYPE_UNSIGNED (TREE_TYPE (arg))
8619 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl))
8620 && (gimple_form
8621 ? useless_type_conversion_p (integer_type_node,
8622 TREE_TYPE (arg))
8623 : tree_nop_conversion_p (integer_type_node,
8624 TREE_TYPE (arg))))
8625 continue;
8626 return false;
8627 }
8628 }
8629 if (targs && !VOID_TYPE_P (TREE_VALUE (targs)))
8630 return false;
8631 return true;
8632}
8633
8634/* If CALL_EXPR CALL calls a normal built-in function or an internal function,
8635 return the associated function code, otherwise return CFN_LAST. */
8636
8637combined_fn
8638get_call_combined_fn (const_tree call)
8639{
8640 /* It's invalid to call this function with anything but a CALL_EXPR. */
8641 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8642
8643 if (!CALL_EXPR_FN (call))
8644 return as_combined_fn (CALL_EXPR_IFN (call));
8645
8646 tree fndecl = get_callee_fndecl (call);
8647 if (fndecl
8648 && fndecl_built_in_p (node: fndecl, klass: BUILT_IN_NORMAL)
8649 && tree_builtin_call_types_compatible_p (call, fndecl))
8650 return as_combined_fn (fn: DECL_FUNCTION_CODE (decl: fndecl));
8651
8652 return CFN_LAST;
8653}
8654
8655/* Comparator of indices based on tree_node_counts. */
8656
8657static int
8658tree_nodes_cmp (const void *p1, const void *p2)
8659{
8660 const unsigned *n1 = (const unsigned *)p1;
8661 const unsigned *n2 = (const unsigned *)p2;
8662
8663 return tree_node_counts[*n1] - tree_node_counts[*n2];
8664}
8665
8666/* Comparator of indices based on tree_code_counts. */
8667
8668static int
8669tree_codes_cmp (const void *p1, const void *p2)
8670{
8671 const unsigned *n1 = (const unsigned *)p1;
8672 const unsigned *n2 = (const unsigned *)p2;
8673
8674 return tree_code_counts[*n1] - tree_code_counts[*n2];
8675}
8676
8677#define TREE_MEM_USAGE_SPACES 40
8678
8679/* Print debugging information about tree nodes generated during the compile,
8680 and any language-specific information. */
8681
8682void
8683dump_tree_statistics (void)
8684{
8685 if (GATHER_STATISTICS)
8686 {
8687 uint64_t total_nodes, total_bytes;
8688 fprintf (stderr, format: "\nKind Nodes Bytes\n");
8689 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8690 total_nodes = total_bytes = 0;
8691
8692 {
8693 auto_vec<unsigned> indices (all_kinds);
8694 for (unsigned i = 0; i < all_kinds; i++)
8695 indices.quick_push (obj: i);
8696 indices.qsort (tree_nodes_cmp);
8697
8698 for (unsigned i = 0; i < (int) all_kinds; i++)
8699 {
8700 unsigned j = indices[i];
8701 fprintf (stderr, format: "%-20s %6" PRIu64 "%c %9" PRIu64 "%c\n",
8702 tree_node_kind_names[j], SIZE_AMOUNT (tree_node_counts[j]),
8703 SIZE_AMOUNT (tree_node_sizes[j]));
8704 total_nodes += tree_node_counts[j];
8705 total_bytes += tree_node_sizes[j];
8706 }
8707 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8708 fprintf (stderr, format: "%-20s %6" PRIu64 "%c %9" PRIu64 "%c\n", "Total",
8709 SIZE_AMOUNT (total_nodes), SIZE_AMOUNT (total_bytes));
8710 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8711 }
8712
8713 {
8714 fprintf (stderr, format: "Code Nodes\n");
8715 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8716
8717 auto_vec<unsigned> indices (MAX_TREE_CODES);
8718 for (unsigned i = 0; i < MAX_TREE_CODES; i++)
8719 indices.quick_push (obj: i);
8720 indices.qsort (tree_codes_cmp);
8721
8722 for (unsigned i = 0; i < MAX_TREE_CODES; i++)
8723 {
8724 unsigned j = indices[i];
8725 fprintf (stderr, format: "%-32s %6" PRIu64 "%c\n",
8726 get_tree_code_name ((enum tree_code) j),
8727 SIZE_AMOUNT (tree_code_counts[j]));
8728 }
8729 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8730 fprintf (stderr, format: "\n");
8731 ssanames_print_statistics ();
8732 fprintf (stderr, format: "\n");
8733 phinodes_print_statistics ();
8734 fprintf (stderr, format: "\n");
8735 }
8736 }
8737 else
8738 fprintf (stderr, format: "(No per-node statistics)\n");
8739
8740 print_type_hash_statistics ();
8741 print_debug_expr_statistics ();
8742 print_value_expr_statistics ();
8743 lang_hooks.print_statistics ();
8744}
8745
8746#define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8747
8748/* Generate a crc32 of the low BYTES bytes of VALUE. */
8749
8750unsigned
8751crc32_unsigned_n (unsigned chksum, unsigned value, unsigned bytes)
8752{
8753 /* This relies on the raw feedback's top 4 bits being zero. */
8754#define FEEDBACK(X) ((X) * 0x04c11db7)
8755#define SYNDROME(X) (FEEDBACK ((X) & 1) ^ FEEDBACK ((X) & 2) \
8756 ^ FEEDBACK ((X) & 4) ^ FEEDBACK ((X) & 8))
8757 static const unsigned syndromes[16] =
8758 {
8759 SYNDROME(0x0), SYNDROME(0x1), SYNDROME(0x2), SYNDROME(0x3),
8760 SYNDROME(0x4), SYNDROME(0x5), SYNDROME(0x6), SYNDROME(0x7),
8761 SYNDROME(0x8), SYNDROME(0x9), SYNDROME(0xa), SYNDROME(0xb),
8762 SYNDROME(0xc), SYNDROME(0xd), SYNDROME(0xe), SYNDROME(0xf),
8763 };
8764#undef FEEDBACK
8765#undef SYNDROME
8766
8767 value <<= (32 - bytes * 8);
8768 for (unsigned ix = bytes * 2; ix--; value <<= 4)
8769 {
8770 unsigned feedback = syndromes[((value ^ chksum) >> 28) & 0xf];
8771
8772 chksum = (chksum << 4) ^ feedback;
8773 }
8774
8775 return chksum;
8776}
8777
8778/* Generate a crc32 of a string. */
8779
8780unsigned
8781crc32_string (unsigned chksum, const char *string)
8782{
8783 do
8784 chksum = crc32_byte (chksum, byte: *string);
8785 while (*string++);
8786 return chksum;
8787}
8788
8789/* P is a string that will be used in a symbol. Mask out any characters
8790 that are not valid in that context. */
8791
8792void
8793clean_symbol_name (char *p)
8794{
8795 for (; *p; p++)
8796 if (! (ISALNUM (*p)
8797#ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
8798 || *p == '$'
8799#endif
8800#ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
8801 || *p == '.'
8802#endif
8803 ))
8804 *p = '_';
8805}
8806
8807static GTY(()) unsigned anon_cnt = 0; /* Saved for PCH. */
8808
8809/* Create a unique anonymous identifier. The identifier is still a
8810 valid assembly label. */
8811
8812tree
8813make_anon_name ()
8814{
8815 const char *fmt =
8816#if !defined (NO_DOT_IN_LABEL)
8817 "."
8818#elif !defined (NO_DOLLAR_IN_LABEL)
8819 "$"
8820#else
8821 "_"
8822#endif
8823 "_anon_%d";
8824
8825 char buf[24];
8826 int len = snprintf (s: buf, maxlen: sizeof (buf), format: fmt, anon_cnt++);
8827 gcc_checking_assert (len < int (sizeof (buf)));
8828
8829 tree id = get_identifier_with_length (buf, len);
8830 IDENTIFIER_ANON_P (id) = true;
8831
8832 return id;
8833}
8834
8835/* Generate a name for a special-purpose function.
8836 The generated name may need to be unique across the whole link.
8837 Changes to this function may also require corresponding changes to
8838 xstrdup_mask_random.
8839 TYPE is some string to identify the purpose of this function to the
8840 linker or collect2; it must start with an uppercase letter,
8841 one of:
8842 I - for constructors
8843 D - for destructors
8844 N - for C++ anonymous namespaces
8845 F - for DWARF unwind frame information. */
8846
8847tree
8848get_file_function_name (const char *type)
8849{
8850 char *buf;
8851 const char *p;
8852 char *q;
8853
8854 /* If we already have a name we know to be unique, just use that. */
8855 if (first_global_object_name)
8856 p = q = ASTRDUP (first_global_object_name);
8857 /* If the target is handling the constructors/destructors, they
8858 will be local to this file and the name is only necessary for
8859 debugging purposes.
8860 We also assign sub_I and sub_D sufixes to constructors called from
8861 the global static constructors. These are always local. */
8862 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
8863 || (startswith (str: type, prefix: "sub_")
8864 && (type[4] == 'I' || type[4] == 'D')))
8865 {
8866 const char *file = main_input_filename;
8867 if (! file)
8868 file = LOCATION_FILE (input_location);
8869 /* Just use the file's basename, because the full pathname
8870 might be quite long. */
8871 p = q = ASTRDUP (lbasename (file));
8872 }
8873 else
8874 {
8875 /* Otherwise, the name must be unique across the entire link.
8876 We don't have anything that we know to be unique to this translation
8877 unit, so use what we do have and throw in some randomness. */
8878 unsigned len;
8879 const char *name = weak_global_object_name;
8880 const char *file = main_input_filename;
8881
8882 if (! name)
8883 name = "";
8884 if (! file)
8885 file = LOCATION_FILE (input_location);
8886
8887 len = strlen (s: file);
8888 q = (char *) alloca (9 + 19 + len + 1);
8889 memcpy (dest: q, src: file, n: len + 1);
8890
8891 snprintf (s: q + len, maxlen: 9 + 19 + 1, format: "_%08X_" HOST_WIDE_INT_PRINT_HEX,
8892 crc32_string (chksum: 0, string: name), get_random_seed (false));
8893
8894 p = q;
8895 }
8896
8897 clean_symbol_name (p: q);
8898 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
8899 + strlen (type));
8900
8901 /* Set up the name of the file-level functions we may need.
8902 Use a global object (which is already required to be unique over
8903 the program) rather than the file name (which imposes extra
8904 constraints). */
8905 sprintf (s: buf, FILE_FUNCTION_FORMAT, type, p);
8906
8907 return get_identifier (buf);
8908}
8909
8910#if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
8911
8912/* Complain that the tree code of NODE does not match the expected 0
8913 terminated list of trailing codes. The trailing code list can be
8914 empty, for a more vague error message. FILE, LINE, and FUNCTION
8915 are of the caller. */
8916
8917void
8918tree_check_failed (const_tree node, const char *file,
8919 int line, const char *function, ...)
8920{
8921 va_list args;
8922 const char *buffer;
8923 unsigned length = 0;
8924 enum tree_code code;
8925
8926 va_start (args, function);
8927 while ((code = (enum tree_code) va_arg (args, int)))
8928 length += 4 + strlen (s: get_tree_code_name (code));
8929 va_end (args);
8930 if (length)
8931 {
8932 char *tmp;
8933 va_start (args, function);
8934 length += strlen (s: "expected ");
8935 buffer = tmp = (char *) alloca (length);
8936 length = 0;
8937 while ((code = (enum tree_code) va_arg (args, int)))
8938 {
8939 const char *prefix = length ? " or " : "expected ";
8940
8941 strcpy (dest: tmp + length, src: prefix);
8942 length += strlen (s: prefix);
8943 strcpy (dest: tmp + length, src: get_tree_code_name (code));
8944 length += strlen (s: get_tree_code_name (code));
8945 }
8946 va_end (args);
8947 }
8948 else
8949 buffer = "unexpected node";
8950
8951 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8952 buffer, get_tree_code_name (TREE_CODE (node)),
8953 function, trim_filename (file), line);
8954}
8955
8956/* Complain that the tree code of NODE does match the expected 0
8957 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
8958 the caller. */
8959
8960void
8961tree_not_check_failed (const_tree node, const char *file,
8962 int line, const char *function, ...)
8963{
8964 va_list args;
8965 char *buffer;
8966 unsigned length = 0;
8967 enum tree_code code;
8968
8969 va_start (args, function);
8970 while ((code = (enum tree_code) va_arg (args, int)))
8971 length += 4 + strlen (s: get_tree_code_name (code));
8972 va_end (args);
8973 va_start (args, function);
8974 buffer = (char *) alloca (length);
8975 length = 0;
8976 while ((code = (enum tree_code) va_arg (args, int)))
8977 {
8978 if (length)
8979 {
8980 strcpy (dest: buffer + length, src: " or ");
8981 length += 4;
8982 }
8983 strcpy (dest: buffer + length, src: get_tree_code_name (code));
8984 length += strlen (s: get_tree_code_name (code));
8985 }
8986 va_end (args);
8987
8988 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
8989 buffer, get_tree_code_name (TREE_CODE (node)),
8990 function, trim_filename (file), line);
8991}
8992
8993/* Similar to tree_check_failed, except that we check for a class of tree
8994 code, given in CL. */
8995
8996void
8997tree_class_check_failed (const_tree node, const enum tree_code_class cl,
8998 const char *file, int line, const char *function)
8999{
9000 internal_error
9001 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9002 TREE_CODE_CLASS_STRING (cl),
9003 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9004 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9005}
9006
9007/* Similar to tree_check_failed, except that instead of specifying a
9008 dozen codes, use the knowledge that they're all sequential. */
9009
9010void
9011tree_range_check_failed (const_tree node, const char *file, int line,
9012 const char *function, enum tree_code c1,
9013 enum tree_code c2)
9014{
9015 char *buffer;
9016 unsigned length = 0;
9017 unsigned int c;
9018
9019 for (c = c1; c <= c2; ++c)
9020 length += 4 + strlen (s: get_tree_code_name ((enum tree_code) c));
9021
9022 length += strlen (s: "expected ");
9023 buffer = (char *) alloca (length);
9024 length = 0;
9025
9026 for (c = c1; c <= c2; ++c)
9027 {
9028 const char *prefix = length ? " or " : "expected ";
9029
9030 strcpy (dest: buffer + length, src: prefix);
9031 length += strlen (s: prefix);
9032 strcpy (dest: buffer + length, src: get_tree_code_name ((enum tree_code) c));
9033 length += strlen (s: get_tree_code_name ((enum tree_code) c));
9034 }
9035
9036 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9037 buffer, get_tree_code_name (TREE_CODE (node)),
9038 function, trim_filename (file), line);
9039}
9040
9041
9042/* Similar to tree_check_failed, except that we check that a tree does
9043 not have the specified code, given in CL. */
9044
9045void
9046tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9047 const char *file, int line, const char *function)
9048{
9049 internal_error
9050 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9051 TREE_CODE_CLASS_STRING (cl),
9052 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9053 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9054}
9055
9056
9057/* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9058
9059void
9060omp_clause_check_failed (const_tree node, const char *file, int line,
9061 const char *function, enum omp_clause_code code)
9062{
9063 internal_error ("tree check: expected %<omp_clause %s%>, have %qs "
9064 "in %s, at %s:%d",
9065 omp_clause_code_name[code],
9066 get_tree_code_name (TREE_CODE (node)),
9067 function, trim_filename (file), line);
9068}
9069
9070
9071/* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9072
9073void
9074omp_clause_range_check_failed (const_tree node, const char *file, int line,
9075 const char *function, enum omp_clause_code c1,
9076 enum omp_clause_code c2)
9077{
9078 char *buffer;
9079 unsigned length = 0;
9080 unsigned int c;
9081
9082 for (c = c1; c <= c2; ++c)
9083 length += 4 + strlen (s: omp_clause_code_name[c]);
9084
9085 length += strlen (s: "expected ");
9086 buffer = (char *) alloca (length);
9087 length = 0;
9088
9089 for (c = c1; c <= c2; ++c)
9090 {
9091 const char *prefix = length ? " or " : "expected ";
9092
9093 strcpy (dest: buffer + length, src: prefix);
9094 length += strlen (s: prefix);
9095 strcpy (dest: buffer + length, src: omp_clause_code_name[c]);
9096 length += strlen (s: omp_clause_code_name[c]);
9097 }
9098
9099 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9100 buffer, omp_clause_code_name[TREE_CODE (node)],
9101 function, trim_filename (file), line);
9102}
9103
9104
9105#undef DEFTREESTRUCT
9106#define DEFTREESTRUCT(VAL, NAME) NAME,
9107
9108static const char *ts_enum_names[] = {
9109#include "treestruct.def"
9110};
9111#undef DEFTREESTRUCT
9112
9113#define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9114
9115/* Similar to tree_class_check_failed, except that we check for
9116 whether CODE contains the tree structure identified by EN. */
9117
9118void
9119tree_contains_struct_check_failed (const_tree node,
9120 const enum tree_node_structure_enum en,
9121 const char *file, int line,
9122 const char *function)
9123{
9124 internal_error
9125 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9126 TS_ENUM_NAME (en),
9127 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9128}
9129
9130
9131/* Similar to above, except that the check is for the bounds of a TREE_VEC's
9132 (dynamically sized) vector. */
9133
9134void
9135tree_int_cst_elt_check_failed (int idx, int len, const char *file, int line,
9136 const char *function)
9137{
9138 internal_error
9139 ("tree check: accessed elt %d of %<tree_int_cst%> with %d elts in %s, "
9140 "at %s:%d",
9141 idx + 1, len, function, trim_filename (file), line);
9142}
9143
9144/* Similar to above, except that the check is for the bounds of a TREE_VEC's
9145 (dynamically sized) vector. */
9146
9147void
9148tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9149 const char *function)
9150{
9151 internal_error
9152 ("tree check: accessed elt %d of %<tree_vec%> with %d elts in %s, at %s:%d",
9153 idx + 1, len, function, trim_filename (file), line);
9154}
9155
9156/* Similar to above, except that the check is for the bounds of the operand
9157 vector of an expression node EXP. */
9158
9159void
9160tree_operand_check_failed (int idx, const_tree exp, const char *file,
9161 int line, const char *function)
9162{
9163 enum tree_code code = TREE_CODE (exp);
9164 internal_error
9165 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9166 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9167 function, trim_filename (file), line);
9168}
9169
9170/* Similar to above, except that the check is for the number of
9171 operands of an OMP_CLAUSE node. */
9172
9173void
9174omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9175 int line, const char *function)
9176{
9177 internal_error
9178 ("tree check: accessed operand %d of %<omp_clause %s%> with %d operands "
9179 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9180 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9181 trim_filename (file), line);
9182}
9183#endif /* ENABLE_TREE_CHECKING */
9184
9185/* Create a new vector type node holding NUNITS units of type INNERTYPE,
9186 and mapped to the machine mode MODE. Initialize its fields and build
9187 the information necessary for debugging output. */
9188
9189static tree
9190make_vector_type (tree innertype, poly_int64 nunits, machine_mode mode)
9191{
9192 tree t;
9193 tree mv_innertype = TYPE_MAIN_VARIANT (innertype);
9194
9195 t = make_node (code: VECTOR_TYPE);
9196 TREE_TYPE (t) = mv_innertype;
9197 SET_TYPE_VECTOR_SUBPARTS (node: t, subparts: nunits);
9198 SET_TYPE_MODE (t, mode);
9199
9200 if (TYPE_STRUCTURAL_EQUALITY_P (mv_innertype) || in_lto_p)
9201 SET_TYPE_STRUCTURAL_EQUALITY (t);
9202 else if ((TYPE_CANONICAL (mv_innertype) != innertype
9203 || mode != VOIDmode)
9204 && !VECTOR_BOOLEAN_TYPE_P (t))
9205 TYPE_CANONICAL (t)
9206 = make_vector_type (TYPE_CANONICAL (mv_innertype), nunits, VOIDmode);
9207
9208 layout_type (t);
9209
9210 hashval_t hash = type_hash_canon_hash (type: t);
9211 t = type_hash_canon (hashcode: hash, type: t);
9212
9213 /* We have built a main variant, based on the main variant of the
9214 inner type. Use it to build the variant we return. */
9215 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9216 && TREE_TYPE (t) != innertype)
9217 return build_type_attribute_qual_variant (t,
9218 TYPE_ATTRIBUTES (innertype),
9219 TYPE_QUALS (innertype));
9220
9221 return t;
9222}
9223
9224static tree
9225make_or_reuse_type (unsigned size, int unsignedp)
9226{
9227 int i;
9228
9229 if (size == INT_TYPE_SIZE)
9230 return unsignedp ? unsigned_type_node : integer_type_node;
9231 if (size == CHAR_TYPE_SIZE)
9232 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9233 if (size == SHORT_TYPE_SIZE)
9234 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9235 if (size == LONG_TYPE_SIZE)
9236 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9237 if (size == LONG_LONG_TYPE_SIZE)
9238 return (unsignedp ? long_long_unsigned_type_node
9239 : long_long_integer_type_node);
9240
9241 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9242 if (size == int_n_data[i].bitsize
9243 && int_n_enabled_p[i])
9244 return (unsignedp ? int_n_trees[i].unsigned_type
9245 : int_n_trees[i].signed_type);
9246
9247 if (unsignedp)
9248 return make_unsigned_type (size);
9249 else
9250 return make_signed_type (size);
9251}
9252
9253/* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9254
9255static tree
9256make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9257{
9258 if (satp)
9259 {
9260 if (size == SHORT_FRACT_TYPE_SIZE)
9261 return unsignedp ? sat_unsigned_short_fract_type_node
9262 : sat_short_fract_type_node;
9263 if (size == FRACT_TYPE_SIZE)
9264 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9265 if (size == LONG_FRACT_TYPE_SIZE)
9266 return unsignedp ? sat_unsigned_long_fract_type_node
9267 : sat_long_fract_type_node;
9268 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9269 return unsignedp ? sat_unsigned_long_long_fract_type_node
9270 : sat_long_long_fract_type_node;
9271 }
9272 else
9273 {
9274 if (size == SHORT_FRACT_TYPE_SIZE)
9275 return unsignedp ? unsigned_short_fract_type_node
9276 : short_fract_type_node;
9277 if (size == FRACT_TYPE_SIZE)
9278 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9279 if (size == LONG_FRACT_TYPE_SIZE)
9280 return unsignedp ? unsigned_long_fract_type_node
9281 : long_fract_type_node;
9282 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9283 return unsignedp ? unsigned_long_long_fract_type_node
9284 : long_long_fract_type_node;
9285 }
9286
9287 return make_fract_type (size, unsignedp, satp);
9288}
9289
9290/* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9291
9292static tree
9293make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9294{
9295 if (satp)
9296 {
9297 if (size == SHORT_ACCUM_TYPE_SIZE)
9298 return unsignedp ? sat_unsigned_short_accum_type_node
9299 : sat_short_accum_type_node;
9300 if (size == ACCUM_TYPE_SIZE)
9301 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9302 if (size == LONG_ACCUM_TYPE_SIZE)
9303 return unsignedp ? sat_unsigned_long_accum_type_node
9304 : sat_long_accum_type_node;
9305 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9306 return unsignedp ? sat_unsigned_long_long_accum_type_node
9307 : sat_long_long_accum_type_node;
9308 }
9309 else
9310 {
9311 if (size == SHORT_ACCUM_TYPE_SIZE)
9312 return unsignedp ? unsigned_short_accum_type_node
9313 : short_accum_type_node;
9314 if (size == ACCUM_TYPE_SIZE)
9315 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9316 if (size == LONG_ACCUM_TYPE_SIZE)
9317 return unsignedp ? unsigned_long_accum_type_node
9318 : long_accum_type_node;
9319 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9320 return unsignedp ? unsigned_long_long_accum_type_node
9321 : long_long_accum_type_node;
9322 }
9323
9324 return make_accum_type (size, unsignedp, satp);
9325}
9326
9327
9328/* Create an atomic variant node for TYPE. This routine is called
9329 during initialization of data types to create the 5 basic atomic
9330 types. The generic build_variant_type function requires these to
9331 already be set up in order to function properly, so cannot be
9332 called from there. If ALIGN is non-zero, then ensure alignment is
9333 overridden to this value. */
9334
9335static tree
9336build_atomic_base (tree type, unsigned int align)
9337{
9338 tree t;
9339
9340 /* Make sure its not already registered. */
9341 if ((t = get_qualified_type (type, type_quals: TYPE_QUAL_ATOMIC)))
9342 return t;
9343
9344 t = build_variant_type_copy (type);
9345 set_type_quals (type: t, type_quals: TYPE_QUAL_ATOMIC);
9346
9347 if (align)
9348 SET_TYPE_ALIGN (t, align);
9349
9350 return t;
9351}
9352
9353/* Information about the _FloatN and _FloatNx types. This must be in
9354 the same order as the corresponding TI_* enum values. */
9355const floatn_type_info floatn_nx_types[NUM_FLOATN_NX_TYPES] =
9356 {
9357 { .n: 16, .extended: false },
9358 { .n: 32, .extended: false },
9359 { .n: 64, .extended: false },
9360 { .n: 128, .extended: false },
9361 { .n: 32, .extended: true },
9362 { .n: 64, .extended: true },
9363 { .n: 128, .extended: true },
9364 };
9365
9366
9367/* Create nodes for all integer types (and error_mark_node) using the sizes
9368 of C datatypes. SIGNED_CHAR specifies whether char is signed. */
9369
9370void
9371build_common_tree_nodes (bool signed_char)
9372{
9373 int i;
9374
9375 error_mark_node = make_node (code: ERROR_MARK);
9376 TREE_TYPE (error_mark_node) = error_mark_node;
9377
9378 initialize_sizetypes ();
9379
9380 /* Define both `signed char' and `unsigned char'. */
9381 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9382 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9383 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9384 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9385
9386 /* Define `char', which is like either `signed char' or `unsigned char'
9387 but not the same as either. */
9388 char_type_node
9389 = (signed_char
9390 ? make_signed_type (CHAR_TYPE_SIZE)
9391 : make_unsigned_type (CHAR_TYPE_SIZE));
9392 TYPE_STRING_FLAG (char_type_node) = 1;
9393
9394 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9395 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9396 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9397 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9398 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9399 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9400 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9401 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9402
9403 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9404 {
9405 int_n_trees[i].signed_type = make_signed_type (int_n_data[i].bitsize);
9406 int_n_trees[i].unsigned_type = make_unsigned_type (int_n_data[i].bitsize);
9407
9408 if (int_n_enabled_p[i])
9409 {
9410 integer_types[itk_intN_0 + i * 2] = int_n_trees[i].signed_type;
9411 integer_types[itk_unsigned_intN_0 + i * 2] = int_n_trees[i].unsigned_type;
9412 }
9413 }
9414
9415 /* Define a boolean type. This type only represents boolean values but
9416 may be larger than char depending on the value of BOOL_TYPE_SIZE. */
9417 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9418 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9419 TYPE_PRECISION (boolean_type_node) = 1;
9420 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, cst: 1);
9421
9422 /* Define what type to use for size_t. */
9423 if (strcmp (SIZE_TYPE, s2: "unsigned int") == 0)
9424 size_type_node = unsigned_type_node;
9425 else if (strcmp (SIZE_TYPE, s2: "long unsigned int") == 0)
9426 size_type_node = long_unsigned_type_node;
9427 else if (strcmp (SIZE_TYPE, s2: "long long unsigned int") == 0)
9428 size_type_node = long_long_unsigned_type_node;
9429 else if (strcmp (SIZE_TYPE, s2: "short unsigned int") == 0)
9430 size_type_node = short_unsigned_type_node;
9431 else
9432 {
9433 int i;
9434
9435 size_type_node = NULL_TREE;
9436 for (i = 0; i < NUM_INT_N_ENTS; i++)
9437 if (int_n_enabled_p[i])
9438 {
9439 char name[50], altname[50];
9440 sprintf (s: name, format: "__int%d unsigned", int_n_data[i].bitsize);
9441 sprintf (s: altname, format: "__int%d__ unsigned", int_n_data[i].bitsize);
9442
9443 if (strcmp (s1: name, SIZE_TYPE) == 0
9444 || strcmp (s1: altname, SIZE_TYPE) == 0)
9445 {
9446 size_type_node = int_n_trees[i].unsigned_type;
9447 }
9448 }
9449 if (size_type_node == NULL_TREE)
9450 gcc_unreachable ();
9451 }
9452
9453 /* Define what type to use for ptrdiff_t. */
9454 if (strcmp (PTRDIFF_TYPE, s2: "int") == 0)
9455 ptrdiff_type_node = integer_type_node;
9456 else if (strcmp (PTRDIFF_TYPE, s2: "long int") == 0)
9457 ptrdiff_type_node = long_integer_type_node;
9458 else if (strcmp (PTRDIFF_TYPE, s2: "long long int") == 0)
9459 ptrdiff_type_node = long_long_integer_type_node;
9460 else if (strcmp (PTRDIFF_TYPE, s2: "short int") == 0)
9461 ptrdiff_type_node = short_integer_type_node;
9462 else
9463 {
9464 ptrdiff_type_node = NULL_TREE;
9465 for (int i = 0; i < NUM_INT_N_ENTS; i++)
9466 if (int_n_enabled_p[i])
9467 {
9468 char name[50], altname[50];
9469 sprintf (s: name, format: "__int%d", int_n_data[i].bitsize);
9470 sprintf (s: altname, format: "__int%d__", int_n_data[i].bitsize);
9471
9472 if (strcmp (s1: name, PTRDIFF_TYPE) == 0
9473 || strcmp (s1: altname, PTRDIFF_TYPE) == 0)
9474 ptrdiff_type_node = int_n_trees[i].signed_type;
9475 }
9476 if (ptrdiff_type_node == NULL_TREE)
9477 gcc_unreachable ();
9478 }
9479
9480 /* Fill in the rest of the sized types. Reuse existing type nodes
9481 when possible. */
9482 intQI_type_node = make_or_reuse_type (size: GET_MODE_BITSIZE (QImode), unsignedp: 0);
9483 intHI_type_node = make_or_reuse_type (size: GET_MODE_BITSIZE (HImode), unsignedp: 0);
9484 intSI_type_node = make_or_reuse_type (size: GET_MODE_BITSIZE (SImode), unsignedp: 0);
9485 intDI_type_node = make_or_reuse_type (size: GET_MODE_BITSIZE (DImode), unsignedp: 0);
9486 intTI_type_node = make_or_reuse_type (size: GET_MODE_BITSIZE (TImode), unsignedp: 0);
9487
9488 unsigned_intQI_type_node = make_or_reuse_type (size: GET_MODE_BITSIZE (QImode), unsignedp: 1);
9489 unsigned_intHI_type_node = make_or_reuse_type (size: GET_MODE_BITSIZE (HImode), unsignedp: 1);
9490 unsigned_intSI_type_node = make_or_reuse_type (size: GET_MODE_BITSIZE (SImode), unsignedp: 1);
9491 unsigned_intDI_type_node = make_or_reuse_type (size: GET_MODE_BITSIZE (DImode), unsignedp: 1);
9492 unsigned_intTI_type_node = make_or_reuse_type (size: GET_MODE_BITSIZE (TImode), unsignedp: 1);
9493
9494 /* Don't call build_qualified type for atomics. That routine does
9495 special processing for atomics, and until they are initialized
9496 it's better not to make that call.
9497
9498 Check to see if there is a target override for atomic types. */
9499
9500 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
9501 align: targetm.atomic_align_for_mode (QImode));
9502 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
9503 align: targetm.atomic_align_for_mode (HImode));
9504 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
9505 align: targetm.atomic_align_for_mode (SImode));
9506 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
9507 align: targetm.atomic_align_for_mode (DImode));
9508 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
9509 align: targetm.atomic_align_for_mode (TImode));
9510
9511 access_public_node = get_identifier ("public");
9512 access_protected_node = get_identifier ("protected");
9513 access_private_node = get_identifier ("private");
9514
9515 /* Define these next since types below may used them. */
9516 integer_zero_node = build_int_cst (integer_type_node, cst: 0);
9517 integer_one_node = build_int_cst (integer_type_node, cst: 1);
9518 integer_three_node = build_int_cst (integer_type_node, cst: 3);
9519 integer_minus_one_node = build_int_cst (integer_type_node, cst: -1);
9520
9521 size_zero_node = size_int (0);
9522 size_one_node = size_int (1);
9523 bitsize_zero_node = bitsize_int (0);
9524 bitsize_one_node = bitsize_int (1);
9525 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9526
9527 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9528 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9529
9530 void_type_node = make_node (code: VOID_TYPE);
9531 layout_type (void_type_node);
9532
9533 /* We are not going to have real types in C with less than byte alignment,
9534 so we might as well not have any types that claim to have it. */
9535 SET_TYPE_ALIGN (void_type_node, BITS_PER_UNIT);
9536 TYPE_USER_ALIGN (void_type_node) = 0;
9537
9538 void_node = make_node (code: VOID_CST);
9539 TREE_TYPE (void_node) = void_type_node;
9540
9541 void_list_node = build_tree_list (NULL_TREE, void_type_node);
9542
9543 null_pointer_node = build_int_cst (type: build_pointer_type (void_type_node), cst: 0);
9544 layout_type (TREE_TYPE (null_pointer_node));
9545
9546 ptr_type_node = build_pointer_type (void_type_node);
9547 const_ptr_type_node
9548 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9549 for (unsigned i = 0; i < ARRAY_SIZE (builtin_structptr_types); ++i)
9550 builtin_structptr_types[i].node = builtin_structptr_types[i].base;
9551
9552 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, unsignedp: 1);
9553
9554 float_type_node = make_node (code: REAL_TYPE);
9555 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9556 layout_type (float_type_node);
9557
9558 double_type_node = make_node (code: REAL_TYPE);
9559 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9560 layout_type (double_type_node);
9561
9562 long_double_type_node = make_node (code: REAL_TYPE);
9563 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9564 layout_type (long_double_type_node);
9565
9566 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
9567 {
9568 int n = floatn_nx_types[i].n;
9569 bool extended = floatn_nx_types[i].extended;
9570 scalar_float_mode mode;
9571 if (!targetm.floatn_mode (n, extended).exists (mode: &mode))
9572 continue;
9573 int precision = GET_MODE_PRECISION (mode);
9574 /* Work around the rs6000 KFmode having precision 113 not
9575 128. */
9576 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
9577 gcc_assert (fmt->b == 2 && fmt->emin + fmt->emax == 3);
9578 int min_precision = fmt->p + ceil_log2 (x: fmt->emax - fmt->emin);
9579 if (!extended)
9580 gcc_assert (min_precision == n);
9581 if (precision < min_precision)
9582 precision = min_precision;
9583 FLOATN_NX_TYPE_NODE (i) = make_node (code: REAL_TYPE);
9584 TYPE_PRECISION (FLOATN_NX_TYPE_NODE (i)) = precision;
9585 layout_type (FLOATN_NX_TYPE_NODE (i));
9586 SET_TYPE_MODE (FLOATN_NX_TYPE_NODE (i), mode);
9587 }
9588 float128t_type_node = float128_type_node;
9589#ifdef HAVE_BFmode
9590 if (REAL_MODE_FORMAT (BFmode) == &arm_bfloat_half_format
9591 && targetm.scalar_mode_supported_p (BFmode)
9592 && targetm.libgcc_floating_mode_supported_p (BFmode))
9593 {
9594 bfloat16_type_node = make_node (code: REAL_TYPE);
9595 TYPE_PRECISION (bfloat16_type_node) = GET_MODE_PRECISION (BFmode);
9596 layout_type (bfloat16_type_node);
9597 SET_TYPE_MODE (bfloat16_type_node, BFmode);
9598 }
9599#endif
9600
9601 float_ptr_type_node = build_pointer_type (float_type_node);
9602 double_ptr_type_node = build_pointer_type (double_type_node);
9603 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9604 integer_ptr_type_node = build_pointer_type (integer_type_node);
9605
9606 /* Fixed size integer types. */
9607 uint16_type_node = make_or_reuse_type (size: 16, unsignedp: 1);
9608 uint32_type_node = make_or_reuse_type (size: 32, unsignedp: 1);
9609 uint64_type_node = make_or_reuse_type (size: 64, unsignedp: 1);
9610 if (targetm.scalar_mode_supported_p (TImode))
9611 uint128_type_node = make_or_reuse_type (size: 128, unsignedp: 1);
9612
9613 /* Decimal float types. */
9614 if (targetm.decimal_float_supported_p ())
9615 {
9616 dfloat32_type_node = make_node (code: REAL_TYPE);
9617 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9618 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9619 layout_type (dfloat32_type_node);
9620
9621 dfloat64_type_node = make_node (code: REAL_TYPE);
9622 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9623 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9624 layout_type (dfloat64_type_node);
9625
9626 dfloat128_type_node = make_node (code: REAL_TYPE);
9627 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9628 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9629 layout_type (dfloat128_type_node);
9630 }
9631
9632 complex_integer_type_node = build_complex_type (integer_type_node, named: true);
9633 complex_float_type_node = build_complex_type (float_type_node, named: true);
9634 complex_double_type_node = build_complex_type (double_type_node, named: true);
9635 complex_long_double_type_node = build_complex_type (long_double_type_node,
9636 named: true);
9637
9638 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
9639 {
9640 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
9641 COMPLEX_FLOATN_NX_TYPE_NODE (i)
9642 = build_complex_type (FLOATN_NX_TYPE_NODE (i));
9643 }
9644
9645/* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9646#define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9647 sat_ ## KIND ## _type_node = \
9648 make_sat_signed_ ## KIND ## _type (SIZE); \
9649 sat_unsigned_ ## KIND ## _type_node = \
9650 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9651 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9652 unsigned_ ## KIND ## _type_node = \
9653 make_unsigned_ ## KIND ## _type (SIZE);
9654
9655#define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9656 sat_ ## WIDTH ## KIND ## _type_node = \
9657 make_sat_signed_ ## KIND ## _type (SIZE); \
9658 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9659 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9660 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9661 unsigned_ ## WIDTH ## KIND ## _type_node = \
9662 make_unsigned_ ## KIND ## _type (SIZE);
9663
9664/* Make fixed-point type nodes based on four different widths. */
9665#define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9666 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9667 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9668 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9669 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9670
9671/* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9672#define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9673 NAME ## _type_node = \
9674 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9675 u ## NAME ## _type_node = \
9676 make_or_reuse_unsigned_ ## KIND ## _type \
9677 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9678 sat_ ## NAME ## _type_node = \
9679 make_or_reuse_sat_signed_ ## KIND ## _type \
9680 (GET_MODE_BITSIZE (MODE ## mode)); \
9681 sat_u ## NAME ## _type_node = \
9682 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9683 (GET_MODE_BITSIZE (U ## MODE ## mode));
9684
9685 /* Fixed-point type and mode nodes. */
9686 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9687 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9688 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9689 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9690 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9691 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9692 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9693 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9694 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9695 MAKE_FIXED_MODE_NODE (accum, da, DA)
9696 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9697
9698 {
9699 tree t = targetm.build_builtin_va_list ();
9700
9701 /* Many back-ends define record types without setting TYPE_NAME.
9702 If we copied the record type here, we'd keep the original
9703 record type without a name. This breaks name mangling. So,
9704 don't copy record types and let c_common_nodes_and_builtins()
9705 declare the type to be __builtin_va_list. */
9706 if (TREE_CODE (t) != RECORD_TYPE)
9707 t = build_variant_type_copy (type: t);
9708
9709 va_list_type_node = t;
9710 }
9711
9712 /* SCEV analyzer global shared trees. */
9713 chrec_dont_know = make_node (code: SCEV_NOT_KNOWN);
9714 TREE_TYPE (chrec_dont_know) = void_type_node;
9715 chrec_known = make_node (code: SCEV_KNOWN);
9716 TREE_TYPE (chrec_known) = void_type_node;
9717}
9718
9719/* Modify DECL for given flags.
9720 TM_PURE attribute is set only on types, so the function will modify
9721 DECL's type when ECF_TM_PURE is used. */
9722
9723void
9724set_call_expr_flags (tree decl, int flags)
9725{
9726 if (flags & ECF_NOTHROW)
9727 TREE_NOTHROW (decl) = 1;
9728 if (flags & ECF_CONST)
9729 TREE_READONLY (decl) = 1;
9730 if (flags & ECF_PURE)
9731 DECL_PURE_P (decl) = 1;
9732 if (flags & ECF_LOOPING_CONST_OR_PURE)
9733 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9734 if (flags & ECF_NOVOPS)
9735 DECL_IS_NOVOPS (decl) = 1;
9736 if (flags & ECF_NORETURN)
9737 TREE_THIS_VOLATILE (decl) = 1;
9738 if (flags & ECF_MALLOC)
9739 DECL_IS_MALLOC (decl) = 1;
9740 if (flags & ECF_RETURNS_TWICE)
9741 DECL_IS_RETURNS_TWICE (decl) = 1;
9742 if (flags & ECF_LEAF)
9743 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9744 NULL, DECL_ATTRIBUTES (decl));
9745 if (flags & ECF_COLD)
9746 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("cold"),
9747 NULL, DECL_ATTRIBUTES (decl));
9748 if (flags & ECF_RET1)
9749 DECL_ATTRIBUTES (decl)
9750 = tree_cons (get_identifier ("fn spec"),
9751 value: build_tree_list (NULL_TREE, value: build_string (len: 2, str: "1 ")),
9752 DECL_ATTRIBUTES (decl));
9753 if ((flags & ECF_TM_PURE) && flag_tm)
9754 apply_tm_attr (decl, get_identifier ("transaction_pure"));
9755 if ((flags & ECF_XTHROW))
9756 DECL_ATTRIBUTES (decl)
9757 = tree_cons (get_identifier ("expected_throw"),
9758 NULL, DECL_ATTRIBUTES (decl));
9759 /* Looping const or pure is implied by noreturn.
9760 There is currently no way to declare looping const or looping pure alone. */
9761 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9762 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9763}
9764
9765
9766/* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9767
9768static void
9769local_define_builtin (const char *name, tree type, enum built_in_function code,
9770 const char *library_name, int ecf_flags)
9771{
9772 tree decl;
9773
9774 decl = add_builtin_function (name, type, function_code: code, cl: BUILT_IN_NORMAL,
9775 library_name, NULL_TREE);
9776 set_call_expr_flags (decl, flags: ecf_flags);
9777
9778 set_builtin_decl (fncode: code, decl, implicit_p: true);
9779}
9780
9781/* Call this function after instantiating all builtins that the language
9782 front end cares about. This will build the rest of the builtins
9783 and internal functions that are relied upon by the tree optimizers and
9784 the middle-end. */
9785
9786void
9787build_common_builtin_nodes (void)
9788{
9789 tree tmp, ftype;
9790 int ecf_flags;
9791
9792 if (!builtin_decl_explicit_p (fncode: BUILT_IN_CLEAR_PADDING))
9793 {
9794 ftype = build_function_type_list (void_type_node,
9795 ptr_type_node,
9796 ptr_type_node,
9797 integer_type_node,
9798 NULL_TREE);
9799 local_define_builtin (name: "__builtin_clear_padding", type: ftype,
9800 code: BUILT_IN_CLEAR_PADDING,
9801 library_name: "__builtin_clear_padding",
9802 ECF_LEAF | ECF_NOTHROW);
9803 }
9804
9805 if (!builtin_decl_explicit_p (fncode: BUILT_IN_UNREACHABLE)
9806 || !builtin_decl_explicit_p (fncode: BUILT_IN_TRAP)
9807 || !builtin_decl_explicit_p (fncode: BUILT_IN_UNREACHABLE_TRAP)
9808 || !builtin_decl_explicit_p (fncode: BUILT_IN_ABORT))
9809 {
9810 ftype = build_function_type (void_type_node, void_list_node);
9811 if (!builtin_decl_explicit_p (fncode: BUILT_IN_UNREACHABLE))
9812 local_define_builtin (name: "__builtin_unreachable", type: ftype,
9813 code: BUILT_IN_UNREACHABLE,
9814 library_name: "__builtin_unreachable",
9815 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9816 | ECF_CONST | ECF_COLD);
9817 if (!builtin_decl_explicit_p (fncode: BUILT_IN_UNREACHABLE_TRAP))
9818 local_define_builtin (name: "__builtin_unreachable trap", type: ftype,
9819 code: BUILT_IN_UNREACHABLE_TRAP,
9820 library_name: "__builtin_unreachable trap",
9821 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9822 | ECF_CONST | ECF_COLD);
9823 if (!builtin_decl_explicit_p (fncode: BUILT_IN_ABORT))
9824 local_define_builtin (name: "__builtin_abort", type: ftype, code: BUILT_IN_ABORT,
9825 library_name: "abort",
9826 ECF_LEAF | ECF_NORETURN | ECF_CONST | ECF_COLD);
9827 if (!builtin_decl_explicit_p (fncode: BUILT_IN_TRAP))
9828 local_define_builtin (name: "__builtin_trap", type: ftype, code: BUILT_IN_TRAP,
9829 library_name: "__builtin_trap",
9830 ECF_NORETURN | ECF_NOTHROW | ECF_LEAF | ECF_COLD);
9831 }
9832
9833 if (!builtin_decl_explicit_p (fncode: BUILT_IN_MEMCPY)
9834 || !builtin_decl_explicit_p (fncode: BUILT_IN_MEMMOVE))
9835 {
9836 ftype = build_function_type_list (ptr_type_node,
9837 ptr_type_node, const_ptr_type_node,
9838 size_type_node, NULL_TREE);
9839
9840 if (!builtin_decl_explicit_p (fncode: BUILT_IN_MEMCPY))
9841 local_define_builtin (name: "__builtin_memcpy", type: ftype, code: BUILT_IN_MEMCPY,
9842 library_name: "memcpy", ECF_NOTHROW | ECF_LEAF);
9843 if (!builtin_decl_explicit_p (fncode: BUILT_IN_MEMMOVE))
9844 local_define_builtin (name: "__builtin_memmove", type: ftype, code: BUILT_IN_MEMMOVE,
9845 library_name: "memmove", ECF_NOTHROW | ECF_LEAF);
9846 }
9847
9848 if (!builtin_decl_explicit_p (fncode: BUILT_IN_MEMCMP))
9849 {
9850 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9851 const_ptr_type_node, size_type_node,
9852 NULL_TREE);
9853 local_define_builtin (name: "__builtin_memcmp", type: ftype, code: BUILT_IN_MEMCMP,
9854 library_name: "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9855 }
9856
9857 if (!builtin_decl_explicit_p (fncode: BUILT_IN_MEMSET))
9858 {
9859 ftype = build_function_type_list (ptr_type_node,
9860 ptr_type_node, integer_type_node,
9861 size_type_node, NULL_TREE);
9862 local_define_builtin (name: "__builtin_memset", type: ftype, code: BUILT_IN_MEMSET,
9863 library_name: "memset", ECF_NOTHROW | ECF_LEAF);
9864 }
9865
9866 /* If we're checking the stack, `alloca' can throw. */
9867 const int alloca_flags
9868 = ECF_MALLOC | ECF_LEAF | (flag_stack_check ? 0 : ECF_NOTHROW);
9869
9870 if (!builtin_decl_explicit_p (fncode: BUILT_IN_ALLOCA))
9871 {
9872 ftype = build_function_type_list (ptr_type_node,
9873 size_type_node, NULL_TREE);
9874 local_define_builtin (name: "__builtin_alloca", type: ftype, code: BUILT_IN_ALLOCA,
9875 library_name: "alloca", ecf_flags: alloca_flags);
9876 }
9877
9878 ftype = build_function_type_list (ptr_type_node, size_type_node,
9879 size_type_node, NULL_TREE);
9880 local_define_builtin (name: "__builtin_alloca_with_align", type: ftype,
9881 code: BUILT_IN_ALLOCA_WITH_ALIGN,
9882 library_name: "__builtin_alloca_with_align",
9883 ecf_flags: alloca_flags);
9884
9885 ftype = build_function_type_list (ptr_type_node, size_type_node,
9886 size_type_node, size_type_node, NULL_TREE);
9887 local_define_builtin (name: "__builtin_alloca_with_align_and_max", type: ftype,
9888 code: BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX,
9889 library_name: "__builtin_alloca_with_align_and_max",
9890 ecf_flags: alloca_flags);
9891
9892 ftype = build_function_type_list (void_type_node,
9893 ptr_type_node, ptr_type_node,
9894 ptr_type_node, NULL_TREE);
9895 local_define_builtin (name: "__builtin_init_trampoline", type: ftype,
9896 code: BUILT_IN_INIT_TRAMPOLINE,
9897 library_name: "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9898 local_define_builtin (name: "__builtin_init_heap_trampoline", type: ftype,
9899 code: BUILT_IN_INIT_HEAP_TRAMPOLINE,
9900 library_name: "__builtin_init_heap_trampoline",
9901 ECF_NOTHROW | ECF_LEAF);
9902 local_define_builtin (name: "__builtin_init_descriptor", type: ftype,
9903 code: BUILT_IN_INIT_DESCRIPTOR,
9904 library_name: "__builtin_init_descriptor", ECF_NOTHROW | ECF_LEAF);
9905
9906 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9907 local_define_builtin (name: "__builtin_adjust_trampoline", type: ftype,
9908 code: BUILT_IN_ADJUST_TRAMPOLINE,
9909 library_name: "__builtin_adjust_trampoline",
9910 ECF_CONST | ECF_NOTHROW);
9911 local_define_builtin (name: "__builtin_adjust_descriptor", type: ftype,
9912 code: BUILT_IN_ADJUST_DESCRIPTOR,
9913 library_name: "__builtin_adjust_descriptor",
9914 ECF_CONST | ECF_NOTHROW);
9915
9916 ftype = build_function_type_list (void_type_node,
9917 ptr_type_node, ptr_type_node, NULL_TREE);
9918 if (!builtin_decl_explicit_p (fncode: BUILT_IN_CLEAR_CACHE))
9919 local_define_builtin (name: "__builtin___clear_cache", type: ftype,
9920 code: BUILT_IN_CLEAR_CACHE,
9921 library_name: "__clear_cache",
9922 ECF_NOTHROW);
9923
9924 local_define_builtin (name: "__builtin_nonlocal_goto", type: ftype,
9925 code: BUILT_IN_NONLOCAL_GOTO,
9926 library_name: "__builtin_nonlocal_goto",
9927 ECF_NORETURN | ECF_NOTHROW);
9928
9929 tree ptr_ptr_type_node = build_pointer_type (ptr_type_node);
9930
9931 ftype = build_function_type_list (void_type_node,
9932 ptr_type_node, // void *chain
9933 ptr_type_node, // void *func
9934 ptr_ptr_type_node, // void **dst
9935 NULL_TREE);
9936 local_define_builtin (name: "__builtin_nested_func_ptr_created", type: ftype,
9937 code: BUILT_IN_NESTED_PTR_CREATED,
9938 library_name: "__builtin_nested_func_ptr_created", ECF_NOTHROW);
9939
9940 ftype = build_function_type_list (void_type_node,
9941 NULL_TREE);
9942 local_define_builtin (name: "__builtin_nested_func_ptr_deleted", type: ftype,
9943 code: BUILT_IN_NESTED_PTR_DELETED,
9944 library_name: "__builtin_nested_func_ptr_deleted", ECF_NOTHROW);
9945
9946 ftype = build_function_type_list (void_type_node,
9947 ptr_type_node, ptr_type_node, NULL_TREE);
9948 local_define_builtin (name: "__builtin_setjmp_setup", type: ftype,
9949 code: BUILT_IN_SETJMP_SETUP,
9950 library_name: "__builtin_setjmp_setup", ECF_NOTHROW);
9951
9952 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9953 local_define_builtin (name: "__builtin_setjmp_receiver", type: ftype,
9954 code: BUILT_IN_SETJMP_RECEIVER,
9955 library_name: "__builtin_setjmp_receiver", ECF_NOTHROW | ECF_LEAF);
9956
9957 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9958 local_define_builtin (name: "__builtin_stack_save", type: ftype, code: BUILT_IN_STACK_SAVE,
9959 library_name: "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9960
9961 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9962 local_define_builtin (name: "__builtin_stack_restore", type: ftype,
9963 code: BUILT_IN_STACK_RESTORE,
9964 library_name: "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9965
9966 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9967 const_ptr_type_node, size_type_node,
9968 NULL_TREE);
9969 local_define_builtin (name: "__builtin_memcmp_eq", type: ftype, code: BUILT_IN_MEMCMP_EQ,
9970 library_name: "__builtin_memcmp_eq",
9971 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9972
9973 local_define_builtin (name: "__builtin_strncmp_eq", type: ftype, code: BUILT_IN_STRNCMP_EQ,
9974 library_name: "__builtin_strncmp_eq",
9975 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9976
9977 local_define_builtin (name: "__builtin_strcmp_eq", type: ftype, code: BUILT_IN_STRCMP_EQ,
9978 library_name: "__builtin_strcmp_eq",
9979 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9980
9981 /* If there's a possibility that we might use the ARM EABI, build the
9982 alternate __cxa_end_cleanup node used to resume from C++. */
9983 if (targetm.arm_eabi_unwinder)
9984 {
9985 ftype = build_function_type_list (void_type_node, NULL_TREE);
9986 local_define_builtin (name: "__builtin_cxa_end_cleanup", type: ftype,
9987 code: BUILT_IN_CXA_END_CLEANUP,
9988 library_name: "__cxa_end_cleanup",
9989 ECF_NORETURN | ECF_XTHROW | ECF_LEAF);
9990 }
9991
9992 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9993 local_define_builtin (name: "__builtin_unwind_resume", type: ftype,
9994 code: BUILT_IN_UNWIND_RESUME,
9995 library_name: ((targetm_common.except_unwind_info (&global_options)
9996 == UI_SJLJ)
9997 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
9998 ECF_NORETURN | ECF_XTHROW);
9999
10000 if (builtin_decl_explicit (fncode: BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
10001 {
10002 ftype = build_function_type_list (ptr_type_node, integer_type_node,
10003 NULL_TREE);
10004 local_define_builtin (name: "__builtin_return_address", type: ftype,
10005 code: BUILT_IN_RETURN_ADDRESS,
10006 library_name: "__builtin_return_address",
10007 ECF_NOTHROW);
10008 }
10009
10010 if (!builtin_decl_explicit_p (fncode: BUILT_IN_PROFILE_FUNC_ENTER)
10011 || !builtin_decl_explicit_p (fncode: BUILT_IN_PROFILE_FUNC_EXIT))
10012 {
10013 ftype = build_function_type_list (void_type_node, ptr_type_node,
10014 ptr_type_node, NULL_TREE);
10015 if (!builtin_decl_explicit_p (fncode: BUILT_IN_PROFILE_FUNC_ENTER))
10016 local_define_builtin (name: "__cyg_profile_func_enter", type: ftype,
10017 code: BUILT_IN_PROFILE_FUNC_ENTER,
10018 library_name: "__cyg_profile_func_enter", ecf_flags: 0);
10019 if (!builtin_decl_explicit_p (fncode: BUILT_IN_PROFILE_FUNC_EXIT))
10020 local_define_builtin (name: "__cyg_profile_func_exit", type: ftype,
10021 code: BUILT_IN_PROFILE_FUNC_EXIT,
10022 library_name: "__cyg_profile_func_exit", ecf_flags: 0);
10023 }
10024
10025 /* The exception object and filter values from the runtime. The argument
10026 must be zero before exception lowering, i.e. from the front end. After
10027 exception lowering, it will be the region number for the exception
10028 landing pad. These functions are PURE instead of CONST to prevent
10029 them from being hoisted past the exception edge that will initialize
10030 its value in the landing pad. */
10031 ftype = build_function_type_list (ptr_type_node,
10032 integer_type_node, NULL_TREE);
10033 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
10034 /* Only use TM_PURE if we have TM language support. */
10035 if (builtin_decl_explicit_p (fncode: BUILT_IN_TM_LOAD_1))
10036 ecf_flags |= ECF_TM_PURE;
10037 local_define_builtin (name: "__builtin_eh_pointer", type: ftype, code: BUILT_IN_EH_POINTER,
10038 library_name: "__builtin_eh_pointer", ecf_flags);
10039
10040 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
10041 ftype = build_function_type_list (return_type: tmp, integer_type_node, NULL_TREE);
10042 local_define_builtin (name: "__builtin_eh_filter", type: ftype, code: BUILT_IN_EH_FILTER,
10043 library_name: "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10044
10045 ftype = build_function_type_list (void_type_node,
10046 integer_type_node, integer_type_node,
10047 NULL_TREE);
10048 local_define_builtin (name: "__builtin_eh_copy_values", type: ftype,
10049 code: BUILT_IN_EH_COPY_VALUES,
10050 library_name: "__builtin_eh_copy_values", ECF_NOTHROW);
10051
10052 /* Complex multiplication and division. These are handled as builtins
10053 rather than optabs because emit_library_call_value doesn't support
10054 complex. Further, we can do slightly better with folding these
10055 beasties if the real and complex parts of the arguments are separate. */
10056 {
10057 int mode;
10058
10059 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10060 {
10061 char mode_name_buf[4], *q;
10062 const char *p;
10063 enum built_in_function mcode, dcode;
10064 tree type, inner_type;
10065 const char *prefix = "__";
10066
10067 if (targetm.libfunc_gnu_prefix)
10068 prefix = "__gnu_";
10069
10070 type = lang_hooks.types.type_for_mode ((machine_mode) mode, 0);
10071 if (type == NULL)
10072 continue;
10073 inner_type = TREE_TYPE (type);
10074
10075 ftype = build_function_type_list (return_type: type, inner_type, inner_type,
10076 inner_type, inner_type, NULL_TREE);
10077
10078 mcode = ((enum built_in_function)
10079 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10080 dcode = ((enum built_in_function)
10081 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10082
10083 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10084 *q = TOLOWER (*p);
10085 *q = '\0';
10086
10087 /* For -ftrapping-math these should throw from a former
10088 -fnon-call-exception stmt. */
10089 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10090 NULL);
10091 local_define_builtin (name: built_in_names[mcode], type: ftype, code: mcode,
10092 library_name: built_in_names[mcode],
10093 ECF_CONST | ECF_LEAF);
10094
10095 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10096 NULL);
10097 local_define_builtin (name: built_in_names[dcode], type: ftype, code: dcode,
10098 library_name: built_in_names[dcode],
10099 ECF_CONST | ECF_LEAF);
10100 }
10101 }
10102
10103 init_internal_fns ();
10104}
10105
10106/* HACK. GROSS. This is absolutely disgusting. I wish there was a
10107 better way.
10108
10109 If we requested a pointer to a vector, build up the pointers that
10110 we stripped off while looking for the inner type. Similarly for
10111 return values from functions.
10112
10113 The argument TYPE is the top of the chain, and BOTTOM is the
10114 new type which we will point to. */
10115
10116tree
10117reconstruct_complex_type (tree type, tree bottom)
10118{
10119 tree inner, outer;
10120
10121 if (TREE_CODE (type) == POINTER_TYPE)
10122 {
10123 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10124 outer = build_pointer_type_for_mode (to_type: inner, TYPE_MODE (type),
10125 TYPE_REF_CAN_ALIAS_ALL (type));
10126 }
10127 else if (TREE_CODE (type) == REFERENCE_TYPE)
10128 {
10129 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10130 outer = build_reference_type_for_mode (to_type: inner, TYPE_MODE (type),
10131 TYPE_REF_CAN_ALIAS_ALL (type));
10132 }
10133 else if (TREE_CODE (type) == ARRAY_TYPE)
10134 {
10135 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10136 outer = build_array_type (elt_type: inner, TYPE_DOMAIN (type));
10137 }
10138 else if (TREE_CODE (type) == FUNCTION_TYPE)
10139 {
10140 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10141 outer = build_function_type (value_type: inner, TYPE_ARG_TYPES (type),
10142 TYPE_NO_NAMED_ARGS_STDARG_P (type));
10143 }
10144 else if (TREE_CODE (type) == METHOD_TYPE)
10145 {
10146 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10147 /* The build_method_type_directly() routine prepends 'this' to argument list,
10148 so we must compensate by getting rid of it. */
10149 outer
10150 = build_method_type_directly
10151 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10152 rettype: inner,
10153 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10154 }
10155 else if (TREE_CODE (type) == OFFSET_TYPE)
10156 {
10157 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10158 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), type: inner);
10159 }
10160 else
10161 return bottom;
10162
10163 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10164 TYPE_QUALS (type));
10165}
10166
10167/* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10168 the inner type. */
10169tree
10170build_vector_type_for_mode (tree innertype, machine_mode mode)
10171{
10172 poly_int64 nunits;
10173 unsigned int bitsize;
10174
10175 switch (GET_MODE_CLASS (mode))
10176 {
10177 case MODE_VECTOR_BOOL:
10178 case MODE_VECTOR_INT:
10179 case MODE_VECTOR_FLOAT:
10180 case MODE_VECTOR_FRACT:
10181 case MODE_VECTOR_UFRACT:
10182 case MODE_VECTOR_ACCUM:
10183 case MODE_VECTOR_UACCUM:
10184 nunits = GET_MODE_NUNITS (mode);
10185 break;
10186
10187 case MODE_INT:
10188 /* Check that there are no leftover bits. */
10189 bitsize = GET_MODE_BITSIZE (mode: as_a <scalar_int_mode> (m: mode));
10190 gcc_assert (bitsize % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10191 nunits = bitsize / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10192 break;
10193
10194 default:
10195 gcc_unreachable ();
10196 }
10197
10198 return make_vector_type (innertype, nunits, mode);
10199}
10200
10201/* Similarly, but takes the inner type and number of units, which must be
10202 a power of two. */
10203
10204tree
10205build_vector_type (tree innertype, poly_int64 nunits)
10206{
10207 return make_vector_type (innertype, nunits, VOIDmode);
10208}
10209
10210/* Build a truth vector with NUNITS units, giving it mode MASK_MODE. */
10211
10212tree
10213build_truth_vector_type_for_mode (poly_uint64 nunits, machine_mode mask_mode)
10214{
10215 gcc_assert (mask_mode != BLKmode);
10216
10217 unsigned HOST_WIDE_INT esize;
10218 if (VECTOR_MODE_P (mask_mode))
10219 {
10220 poly_uint64 vsize = GET_MODE_PRECISION (mode: mask_mode);
10221 esize = vector_element_size (vsize, nunits);
10222 }
10223 else
10224 esize = 1;
10225
10226 tree bool_type = build_nonstandard_boolean_type (precision: esize);
10227
10228 return make_vector_type (innertype: bool_type, nunits, mode: mask_mode);
10229}
10230
10231/* Build a vector type that holds one boolean result for each element of
10232 vector type VECTYPE. The public interface for this operation is
10233 truth_type_for. */
10234
10235static tree
10236build_truth_vector_type_for (tree vectype)
10237{
10238 machine_mode vector_mode = TYPE_MODE (vectype);
10239 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (node: vectype);
10240
10241 machine_mode mask_mode;
10242 if (VECTOR_MODE_P (vector_mode)
10243 && targetm.vectorize.get_mask_mode (vector_mode).exists (mode: &mask_mode))
10244 return build_truth_vector_type_for_mode (nunits, mask_mode);
10245
10246 poly_uint64 vsize = tree_to_poly_uint64 (TYPE_SIZE (vectype));
10247 unsigned HOST_WIDE_INT esize = vector_element_size (vsize, nunits);
10248 tree bool_type = build_nonstandard_boolean_type (precision: esize);
10249
10250 return make_vector_type (innertype: bool_type, nunits, VOIDmode);
10251}
10252
10253/* Like build_vector_type, but builds a variant type with TYPE_VECTOR_OPAQUE
10254 set. */
10255
10256tree
10257build_opaque_vector_type (tree innertype, poly_int64 nunits)
10258{
10259 tree t = make_vector_type (innertype, nunits, VOIDmode);
10260 tree cand;
10261 /* We always build the non-opaque variant before the opaque one,
10262 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10263 cand = TYPE_NEXT_VARIANT (t);
10264 if (cand
10265 && TYPE_VECTOR_OPAQUE (cand)
10266 && check_qualified_type (cand, base: t, TYPE_QUALS (t)))
10267 return cand;
10268 /* Othewise build a variant type and make sure to queue it after
10269 the non-opaque type. */
10270 cand = build_distinct_type_copy (type: t);
10271 TYPE_VECTOR_OPAQUE (cand) = true;
10272 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10273 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10274 TYPE_NEXT_VARIANT (t) = cand;
10275 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10276 return cand;
10277}
10278
10279/* Return the value of element I of VECTOR_CST T as a wide_int. */
10280
10281static poly_wide_int
10282vector_cst_int_elt (const_tree t, unsigned int i)
10283{
10284 /* First handle elements that are directly encoded. */
10285 unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
10286 if (i < encoded_nelts)
10287 return wi::to_poly_wide (VECTOR_CST_ENCODED_ELT (t, i));
10288
10289 /* Identify the pattern that contains element I and work out the index of
10290 the last encoded element for that pattern. */
10291 unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
10292 unsigned int pattern = i % npatterns;
10293 unsigned int count = i / npatterns;
10294 unsigned int final_i = encoded_nelts - npatterns + pattern;
10295
10296 /* If there are no steps, the final encoded value is the right one. */
10297 if (!VECTOR_CST_STEPPED_P (t))
10298 return wi::to_poly_wide (VECTOR_CST_ENCODED_ELT (t, final_i));
10299
10300 /* Otherwise work out the value from the last two encoded elements. */
10301 tree v1 = VECTOR_CST_ENCODED_ELT (t, final_i - npatterns);
10302 tree v2 = VECTOR_CST_ENCODED_ELT (t, final_i);
10303 poly_wide_int diff = wi::to_poly_wide (t: v2) - wi::to_poly_wide (t: v1);
10304 return wi::to_poly_wide (t: v2) + (count - 2) * diff;
10305}
10306
10307/* Return the value of element I of VECTOR_CST T. */
10308
10309tree
10310vector_cst_elt (const_tree t, unsigned int i)
10311{
10312 /* First handle elements that are directly encoded. */
10313 unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
10314 if (i < encoded_nelts)
10315 return VECTOR_CST_ENCODED_ELT (t, i);
10316
10317 /* If there are no steps, the final encoded value is the right one. */
10318 if (!VECTOR_CST_STEPPED_P (t))
10319 {
10320 /* Identify the pattern that contains element I and work out the index of
10321 the last encoded element for that pattern. */
10322 unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
10323 unsigned int pattern = i % npatterns;
10324 unsigned int final_i = encoded_nelts - npatterns + pattern;
10325 return VECTOR_CST_ENCODED_ELT (t, final_i);
10326 }
10327
10328 /* Otherwise work out the value from the last two encoded elements. */
10329 return wide_int_to_tree (TREE_TYPE (TREE_TYPE (t)),
10330 value: vector_cst_int_elt (t, i));
10331}
10332
10333/* Given an initializer INIT, return TRUE if INIT is zero or some
10334 aggregate of zeros. Otherwise return FALSE. If NONZERO is not
10335 null, set *NONZERO if and only if INIT is known not to be all
10336 zeros. The combination of return value of false and *NONZERO
10337 false implies that INIT may but need not be all zeros. Other
10338 combinations indicate definitive answers. */
10339
10340bool
10341initializer_zerop (const_tree init, bool *nonzero /* = NULL */)
10342{
10343 bool dummy;
10344 if (!nonzero)
10345 nonzero = &dummy;
10346
10347 /* Conservatively clear NONZERO and set it only if INIT is definitely
10348 not all zero. */
10349 *nonzero = false;
10350
10351 STRIP_NOPS (init);
10352
10353 unsigned HOST_WIDE_INT off = 0;
10354
10355 switch (TREE_CODE (init))
10356 {
10357 case INTEGER_CST:
10358 if (integer_zerop (expr: init))
10359 return true;
10360
10361 *nonzero = true;
10362 return false;
10363
10364 case REAL_CST:
10365 /* ??? Note that this is not correct for C4X float formats. There,
10366 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10367 negative exponent. */
10368 if (real_zerop (expr: init)
10369 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init)))
10370 return true;
10371
10372 *nonzero = true;
10373 return false;
10374
10375 case FIXED_CST:
10376 if (fixed_zerop (expr: init))
10377 return true;
10378
10379 *nonzero = true;
10380 return false;
10381
10382 case COMPLEX_CST:
10383 if (integer_zerop (expr: init)
10384 || (real_zerop (expr: init)
10385 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10386 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init)))))
10387 return true;
10388
10389 *nonzero = true;
10390 return false;
10391
10392 case VECTOR_CST:
10393 if (VECTOR_CST_NPATTERNS (init) == 1
10394 && VECTOR_CST_DUPLICATE_P (init)
10395 && initializer_zerop (VECTOR_CST_ENCODED_ELT (init, 0)))
10396 return true;
10397
10398 *nonzero = true;
10399 return false;
10400
10401 case CONSTRUCTOR:
10402 {
10403 if (TREE_CLOBBER_P (init))
10404 return false;
10405
10406 unsigned HOST_WIDE_INT idx;
10407 tree elt;
10408
10409 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10410 if (!initializer_zerop (init: elt, nonzero))
10411 return false;
10412
10413 return true;
10414 }
10415
10416 case MEM_REF:
10417 {
10418 tree arg = TREE_OPERAND (init, 0);
10419 if (TREE_CODE (arg) != ADDR_EXPR)
10420 return false;
10421 tree offset = TREE_OPERAND (init, 1);
10422 if (TREE_CODE (offset) != INTEGER_CST
10423 || !tree_fits_uhwi_p (t: offset))
10424 return false;
10425 off = tree_to_uhwi (t: offset);
10426 if (INT_MAX < off)
10427 return false;
10428 arg = TREE_OPERAND (arg, 0);
10429 if (TREE_CODE (arg) != STRING_CST)
10430 return false;
10431 init = arg;
10432 }
10433 /* Fall through. */
10434
10435 case STRING_CST:
10436 {
10437 gcc_assert (off <= INT_MAX);
10438
10439 int i = off;
10440 int n = TREE_STRING_LENGTH (init);
10441 if (n <= i)
10442 return false;
10443
10444 /* We need to loop through all elements to handle cases like
10445 "\0" and "\0foobar". */
10446 for (i = 0; i < n; ++i)
10447 if (TREE_STRING_POINTER (init)[i] != '\0')
10448 {
10449 *nonzero = true;
10450 return false;
10451 }
10452
10453 return true;
10454 }
10455
10456 default:
10457 return false;
10458 }
10459}
10460
10461/* Return true if EXPR is an initializer expression in which every element
10462 is a constant that is numerically equal to 0 or 1. The elements do not
10463 need to be equal to each other. */
10464
10465bool
10466initializer_each_zero_or_onep (const_tree expr)
10467{
10468 STRIP_ANY_LOCATION_WRAPPER (expr);
10469
10470 switch (TREE_CODE (expr))
10471 {
10472 case INTEGER_CST:
10473 return integer_zerop (expr) || integer_onep (expr);
10474
10475 case REAL_CST:
10476 return real_zerop (expr) || real_onep (expr);
10477
10478 case VECTOR_CST:
10479 {
10480 unsigned HOST_WIDE_INT nelts = vector_cst_encoded_nelts (t: expr);
10481 if (VECTOR_CST_STEPPED_P (expr)
10482 && !TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr)).is_constant (const_value: &nelts))
10483 return false;
10484
10485 for (unsigned int i = 0; i < nelts; ++i)
10486 {
10487 tree elt = vector_cst_elt (t: expr, i);
10488 if (!initializer_each_zero_or_onep (expr: elt))
10489 return false;
10490 }
10491
10492 return true;
10493 }
10494
10495 default:
10496 return false;
10497 }
10498}
10499
10500/* Check if vector VEC consists of all the equal elements and
10501 that the number of elements corresponds to the type of VEC.
10502 The function returns first element of the vector
10503 or NULL_TREE if the vector is not uniform. */
10504tree
10505uniform_vector_p (const_tree vec)
10506{
10507 tree first, t;
10508 unsigned HOST_WIDE_INT i, nelts;
10509
10510 if (vec == NULL_TREE)
10511 return NULL_TREE;
10512
10513 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10514
10515 if (TREE_CODE (vec) == VEC_DUPLICATE_EXPR)
10516 return TREE_OPERAND (vec, 0);
10517
10518 else if (TREE_CODE (vec) == VECTOR_CST)
10519 {
10520 if (VECTOR_CST_NPATTERNS (vec) == 1 && VECTOR_CST_DUPLICATE_P (vec))
10521 return VECTOR_CST_ENCODED_ELT (vec, 0);
10522 return NULL_TREE;
10523 }
10524
10525 else if (TREE_CODE (vec) == CONSTRUCTOR
10526 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)).is_constant (const_value: &nelts))
10527 {
10528 first = error_mark_node;
10529
10530 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10531 {
10532 if (i == 0)
10533 {
10534 first = t;
10535 continue;
10536 }
10537 if (!operand_equal_p (first, t, flags: 0))
10538 return NULL_TREE;
10539 }
10540 if (i != nelts)
10541 return NULL_TREE;
10542
10543 if (TREE_CODE (first) == CONSTRUCTOR || TREE_CODE (first) == VECTOR_CST)
10544 return uniform_vector_p (vec: first);
10545 return first;
10546 }
10547
10548 return NULL_TREE;
10549}
10550
10551/* If the argument is INTEGER_CST, return it. If the argument is vector
10552 with all elements the same INTEGER_CST, return that INTEGER_CST. Otherwise
10553 return NULL_TREE.
10554 Look through location wrappers. */
10555
10556tree
10557uniform_integer_cst_p (tree t)
10558{
10559 STRIP_ANY_LOCATION_WRAPPER (t);
10560
10561 if (TREE_CODE (t) == INTEGER_CST)
10562 return t;
10563
10564 if (VECTOR_TYPE_P (TREE_TYPE (t)))
10565 {
10566 t = uniform_vector_p (vec: t);
10567 if (t && TREE_CODE (t) == INTEGER_CST)
10568 return t;
10569 }
10570
10571 return NULL_TREE;
10572}
10573
10574/* Checks to see if T is a constant or a constant vector and if each element E
10575 adheres to ~E + 1 == pow2 then return ~E otherwise NULL_TREE. */
10576
10577tree
10578bitmask_inv_cst_vector_p (tree t)
10579{
10580
10581 tree_code code = TREE_CODE (t);
10582 tree type = TREE_TYPE (t);
10583
10584 if (!INTEGRAL_TYPE_P (type)
10585 && !VECTOR_INTEGER_TYPE_P (type))
10586 return NULL_TREE;
10587
10588 unsigned HOST_WIDE_INT nelts = 1;
10589 tree cst;
10590 unsigned int idx = 0;
10591 bool uniform = uniform_integer_cst_p (t);
10592 tree newtype = unsigned_type_for (type);
10593 tree_vector_builder builder;
10594 if (code == INTEGER_CST)
10595 cst = t;
10596 else
10597 {
10598 if (!VECTOR_CST_NELTS (t).is_constant (const_value: &nelts))
10599 return NULL_TREE;
10600
10601 cst = vector_cst_elt (t, i: 0);
10602 builder.new_vector (type: newtype, npatterns: nelts, nelts_per_pattern: 1);
10603 }
10604
10605 tree ty = unsigned_type_for (TREE_TYPE (cst));
10606
10607 do
10608 {
10609 if (idx > 0)
10610 cst = vector_cst_elt (t, i: idx);
10611 wide_int icst = wi::to_wide (t: cst);
10612 wide_int inv = wi::bit_not (x: icst);
10613 icst = wi::add (x: 1, y: inv);
10614 if (wi::popcount (icst) != 1)
10615 return NULL_TREE;
10616
10617 tree newcst = wide_int_to_tree (type: ty, value: inv);
10618
10619 if (uniform)
10620 return build_uniform_cst (type: newtype, sc: newcst);
10621
10622 builder.quick_push (obj: newcst);
10623 }
10624 while (++idx < nelts);
10625
10626 return builder.build ();
10627}
10628
10629/* If VECTOR_CST T has a single nonzero element, return the index of that
10630 element, otherwise return -1. */
10631
10632int
10633single_nonzero_element (const_tree t)
10634{
10635 unsigned HOST_WIDE_INT nelts;
10636 unsigned int repeat_nelts;
10637 if (VECTOR_CST_NELTS (t).is_constant (const_value: &nelts))
10638 repeat_nelts = nelts;
10639 else if (VECTOR_CST_NELTS_PER_PATTERN (t) == 2)
10640 {
10641 nelts = vector_cst_encoded_nelts (t);
10642 repeat_nelts = VECTOR_CST_NPATTERNS (t);
10643 }
10644 else
10645 return -1;
10646
10647 int res = -1;
10648 for (unsigned int i = 0; i < nelts; ++i)
10649 {
10650 tree elt = vector_cst_elt (t, i);
10651 if (!integer_zerop (expr: elt) && !real_zerop (expr: elt))
10652 {
10653 if (res >= 0 || i >= repeat_nelts)
10654 return -1;
10655 res = i;
10656 }
10657 }
10658 return res;
10659}
10660
10661/* Build an empty statement at location LOC. */
10662
10663tree
10664build_empty_stmt (location_t loc)
10665{
10666 tree t = build1 (code: NOP_EXPR, void_type_node, size_zero_node);
10667 SET_EXPR_LOCATION (t, loc);
10668 return t;
10669}
10670
10671
10672/* Build an OMP clause with code CODE. LOC is the location of the
10673 clause. */
10674
10675tree
10676build_omp_clause (location_t loc, enum omp_clause_code code)
10677{
10678 tree t;
10679 int size, length;
10680
10681 length = omp_clause_num_ops[code];
10682 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10683
10684 record_node_allocation_statistics (code: OMP_CLAUSE, length: size);
10685
10686 t = (tree) ggc_internal_alloc (s: size);
10687 memset (s: t, c: 0, n: size);
10688 TREE_SET_CODE (t, OMP_CLAUSE);
10689 OMP_CLAUSE_SET_CODE (t, code);
10690 OMP_CLAUSE_LOCATION (t) = loc;
10691
10692 return t;
10693}
10694
10695/* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10696 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10697 Except for the CODE and operand count field, other storage for the
10698 object is initialized to zeros. */
10699
10700tree
10701build_vl_exp (enum tree_code code, int len MEM_STAT_DECL)
10702{
10703 tree t;
10704 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10705
10706 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10707 gcc_assert (len >= 1);
10708
10709 record_node_allocation_statistics (code, length);
10710
10711 t = ggc_alloc_cleared_tree_node_stat (s: length PASS_MEM_STAT);
10712
10713 TREE_SET_CODE (t, code);
10714
10715 /* Can't use TREE_OPERAND to store the length because if checking is
10716 enabled, it will try to check the length before we store it. :-P */
10717 t->exp.operands[0] = build_int_cst (sizetype, cst: len);
10718
10719 return t;
10720}
10721
10722/* Helper function for build_call_* functions; build a CALL_EXPR with
10723 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10724 the argument slots. */
10725
10726static tree
10727build_call_1 (tree return_type, tree fn, int nargs)
10728{
10729 tree t;
10730
10731 t = build_vl_exp (code: CALL_EXPR, len: nargs + 3);
10732 TREE_TYPE (t) = return_type;
10733 CALL_EXPR_FN (t) = fn;
10734 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10735
10736 return t;
10737}
10738
10739/* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10740 FN and a null static chain slot. NARGS is the number of call arguments
10741 which are specified as "..." arguments. */
10742
10743tree
10744build_call_nary (tree return_type, tree fn, int nargs, ...)
10745{
10746 tree ret;
10747 va_list args;
10748 va_start (args, nargs);
10749 ret = build_call_valist (return_type, fn, nargs, args);
10750 va_end (args);
10751 return ret;
10752}
10753
10754/* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10755 FN and a null static chain slot. NARGS is the number of call arguments
10756 which are specified as a va_list ARGS. */
10757
10758tree
10759build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10760{
10761 tree t;
10762 int i;
10763
10764 t = build_call_1 (return_type, fn, nargs);
10765 for (i = 0; i < nargs; i++)
10766 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10767 process_call_operands (t);
10768 return t;
10769}
10770
10771/* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10772 FN and a null static chain slot. NARGS is the number of call arguments
10773 which are specified as a tree array ARGS. */
10774
10775tree
10776build_call_array_loc (location_t loc, tree return_type, tree fn,
10777 int nargs, const tree *args)
10778{
10779 tree t;
10780 int i;
10781
10782 t = build_call_1 (return_type, fn, nargs);
10783 for (i = 0; i < nargs; i++)
10784 CALL_EXPR_ARG (t, i) = args[i];
10785 process_call_operands (t);
10786 SET_EXPR_LOCATION (t, loc);
10787 return t;
10788}
10789
10790/* Like build_call_array, but takes a vec. */
10791
10792tree
10793build_call_vec (tree return_type, tree fn, const vec<tree, va_gc> *args)
10794{
10795 tree ret, t;
10796 unsigned int ix;
10797
10798 ret = build_call_1 (return_type, fn, nargs: vec_safe_length (v: args));
10799 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10800 CALL_EXPR_ARG (ret, ix) = t;
10801 process_call_operands (t: ret);
10802 return ret;
10803}
10804
10805/* Conveniently construct a function call expression. FNDECL names the
10806 function to be called and N arguments are passed in the array
10807 ARGARRAY. */
10808
10809tree
10810build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
10811{
10812 tree fntype = TREE_TYPE (fndecl);
10813 tree fn = build1 (code: ADDR_EXPR, type: build_pointer_type (to_type: fntype), node: fndecl);
10814
10815 return fold_build_call_array_loc (loc, TREE_TYPE (fntype), fn, n, argarray);
10816}
10817
10818/* Conveniently construct a function call expression. FNDECL names the
10819 function to be called and the arguments are passed in the vector
10820 VEC. */
10821
10822tree
10823build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
10824{
10825 return build_call_expr_loc_array (loc, fndecl, n: vec_safe_length (v: vec),
10826 argarray: vec_safe_address (v: vec));
10827}
10828
10829
10830/* Conveniently construct a function call expression. FNDECL names the
10831 function to be called, N is the number of arguments, and the "..."
10832 parameters are the argument expressions. */
10833
10834tree
10835build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
10836{
10837 va_list ap;
10838 tree *argarray = XALLOCAVEC (tree, n);
10839 int i;
10840
10841 va_start (ap, n);
10842 for (i = 0; i < n; i++)
10843 argarray[i] = va_arg (ap, tree);
10844 va_end (ap);
10845 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10846}
10847
10848/* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
10849 varargs macros aren't supported by all bootstrap compilers. */
10850
10851tree
10852build_call_expr (tree fndecl, int n, ...)
10853{
10854 va_list ap;
10855 tree *argarray = XALLOCAVEC (tree, n);
10856 int i;
10857
10858 va_start (ap, n);
10859 for (i = 0; i < n; i++)
10860 argarray[i] = va_arg (ap, tree);
10861 va_end (ap);
10862 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
10863}
10864
10865/* Build an internal call to IFN, with arguments ARGS[0:N-1] and with return
10866 type TYPE. This is just like CALL_EXPR, except its CALL_EXPR_FN is NULL.
10867 It will get gimplified later into an ordinary internal function. */
10868
10869tree
10870build_call_expr_internal_loc_array (location_t loc, internal_fn ifn,
10871 tree type, int n, const tree *args)
10872{
10873 tree t = build_call_1 (return_type: type, NULL_TREE, nargs: n);
10874 for (int i = 0; i < n; ++i)
10875 CALL_EXPR_ARG (t, i) = args[i];
10876 SET_EXPR_LOCATION (t, loc);
10877 CALL_EXPR_IFN (t) = ifn;
10878 process_call_operands (t);
10879 return t;
10880}
10881
10882/* Build internal call expression. This is just like CALL_EXPR, except
10883 its CALL_EXPR_FN is NULL. It will get gimplified later into ordinary
10884 internal function. */
10885
10886tree
10887build_call_expr_internal_loc (location_t loc, enum internal_fn ifn,
10888 tree type, int n, ...)
10889{
10890 va_list ap;
10891 tree *argarray = XALLOCAVEC (tree, n);
10892 int i;
10893
10894 va_start (ap, n);
10895 for (i = 0; i < n; i++)
10896 argarray[i] = va_arg (ap, tree);
10897 va_end (ap);
10898 return build_call_expr_internal_loc_array (loc, ifn, type, n, args: argarray);
10899}
10900
10901/* Return a function call to FN, if the target is guaranteed to support it,
10902 or null otherwise.
10903
10904 N is the number of arguments, passed in the "...", and TYPE is the
10905 type of the return value. */
10906
10907tree
10908maybe_build_call_expr_loc (location_t loc, combined_fn fn, tree type,
10909 int n, ...)
10910{
10911 va_list ap;
10912 tree *argarray = XALLOCAVEC (tree, n);
10913 int i;
10914
10915 va_start (ap, n);
10916 for (i = 0; i < n; i++)
10917 argarray[i] = va_arg (ap, tree);
10918 va_end (ap);
10919 if (internal_fn_p (code: fn))
10920 {
10921 internal_fn ifn = as_internal_fn (code: fn);
10922 if (direct_internal_fn_p (fn: ifn))
10923 {
10924 tree_pair types = direct_internal_fn_types (ifn, type, argarray);
10925 if (!direct_internal_fn_supported_p (ifn, types,
10926 OPTIMIZE_FOR_BOTH))
10927 return NULL_TREE;
10928 }
10929 return build_call_expr_internal_loc_array (loc, ifn, type, n, args: argarray);
10930 }
10931 else
10932 {
10933 tree fndecl = builtin_decl_implicit (fncode: as_builtin_fn (code: fn));
10934 if (!fndecl)
10935 return NULL_TREE;
10936 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10937 }
10938}
10939
10940/* Return a function call to the appropriate builtin alloca variant.
10941
10942 SIZE is the size to be allocated. ALIGN, if non-zero, is the requested
10943 alignment of the allocated area. MAX_SIZE, if non-negative, is an upper
10944 bound for SIZE in case it is not a fixed value. */
10945
10946tree
10947build_alloca_call_expr (tree size, unsigned int align, HOST_WIDE_INT max_size)
10948{
10949 if (max_size >= 0)
10950 {
10951 tree t = builtin_decl_explicit (fncode: BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX);
10952 return
10953 build_call_expr (fndecl: t, n: 3, size, size_int (align), size_int (max_size));
10954 }
10955 else if (align > 0)
10956 {
10957 tree t = builtin_decl_explicit (fncode: BUILT_IN_ALLOCA_WITH_ALIGN);
10958 return build_call_expr (fndecl: t, n: 2, size, size_int (align));
10959 }
10960 else
10961 {
10962 tree t = builtin_decl_explicit (fncode: BUILT_IN_ALLOCA);
10963 return build_call_expr (fndecl: t, n: 1, size);
10964 }
10965}
10966
10967/* The built-in decl to use to mark code points believed to be unreachable.
10968 Typically __builtin_unreachable, but __builtin_trap if
10969 -fsanitize=unreachable -fsanitize-trap=unreachable. If only
10970 -fsanitize=unreachable, we rely on sanopt to replace calls with the
10971 appropriate ubsan function. When building a call directly, use
10972 {gimple_},build_builtin_unreachable instead. */
10973
10974tree
10975builtin_decl_unreachable ()
10976{
10977 enum built_in_function fncode = BUILT_IN_UNREACHABLE;
10978
10979 if (sanitize_flags_p (flag: SANITIZE_UNREACHABLE)
10980 ? (flag_sanitize_trap & SANITIZE_UNREACHABLE)
10981 : flag_unreachable_traps)
10982 fncode = BUILT_IN_UNREACHABLE_TRAP;
10983 /* For non-trapping sanitize, we will rewrite __builtin_unreachable () later,
10984 in the sanopt pass. */
10985
10986 return builtin_decl_explicit (fncode);
10987}
10988
10989/* Build a call to __builtin_unreachable, possibly rewritten by
10990 -fsanitize=unreachable. Use this rather than the above when practical. */
10991
10992tree
10993build_builtin_unreachable (location_t loc)
10994{
10995 tree data = NULL_TREE;
10996 tree fn = sanitize_unreachable_fn (data: &data, loc);
10997 return build_call_expr_loc (loc, fndecl: fn, n: data != NULL_TREE, data);
10998}
10999
11000/* Create a new constant string literal of type ELTYPE[SIZE] (or LEN
11001 if SIZE == -1) and return a tree node representing char* pointer to
11002 it as an ADDR_EXPR (ARRAY_REF (ELTYPE, ...)). When STR is nonnull
11003 the STRING_CST value is the LEN bytes at STR (the representation
11004 of the string, which may be wide). Otherwise it's all zeros. */
11005
11006tree
11007build_string_literal (unsigned len, const char *str /* = NULL */,
11008 tree eltype /* = char_type_node */,
11009 unsigned HOST_WIDE_INT size /* = -1 */)
11010{
11011 tree t = build_string (len, str);
11012 /* Set the maximum valid index based on the string length or SIZE. */
11013 unsigned HOST_WIDE_INT maxidx
11014 = (size == HOST_WIDE_INT_M1U ? len : size) - 1;
11015
11016 tree index = build_index_type (size_int (maxidx));
11017 eltype = build_type_variant (eltype, 1, 0);
11018 tree type = build_array_type (elt_type: eltype, index_type: index);
11019 TREE_TYPE (t) = type;
11020 TREE_CONSTANT (t) = 1;
11021 TREE_READONLY (t) = 1;
11022 TREE_STATIC (t) = 1;
11023
11024 type = build_pointer_type (to_type: eltype);
11025 t = build1 (code: ADDR_EXPR, type,
11026 node: build4 (code: ARRAY_REF, tt: eltype,
11027 arg0: t, integer_zero_node, NULL_TREE, NULL_TREE));
11028 return t;
11029}
11030
11031
11032
11033/* Return true if T (assumed to be a DECL) must be assigned a memory
11034 location. */
11035
11036bool
11037needs_to_live_in_memory (const_tree t)
11038{
11039 return (TREE_ADDRESSABLE (t)
11040 || is_global_var (t)
11041 || (TREE_CODE (t) == RESULT_DECL
11042 && !DECL_BY_REFERENCE (t)
11043 && aggregate_value_p (t, current_function_decl)));
11044}
11045
11046/* Return value of a constant X and sign-extend it. */
11047
11048HOST_WIDE_INT
11049int_cst_value (const_tree x)
11050{
11051 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
11052 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
11053
11054 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
11055 gcc_assert (cst_and_fits_in_hwi (x));
11056
11057 if (bits < HOST_BITS_PER_WIDE_INT)
11058 {
11059 bool negative = ((val >> (bits - 1)) & 1) != 0;
11060 if (negative)
11061 val |= HOST_WIDE_INT_M1U << (bits - 1) << 1;
11062 else
11063 val &= ~(HOST_WIDE_INT_M1U << (bits - 1) << 1);
11064 }
11065
11066 return val;
11067}
11068
11069/* If TYPE is an integral or pointer type, return an integer type with
11070 the same precision which is unsigned iff UNSIGNEDP is true, or itself
11071 if TYPE is already an integer type of signedness UNSIGNEDP.
11072 If TYPE is a floating-point type, return an integer type with the same
11073 bitsize and with the signedness given by UNSIGNEDP; this is useful
11074 when doing bit-level operations on a floating-point value. */
11075
11076tree
11077signed_or_unsigned_type_for (int unsignedp, tree type)
11078{
11079 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) == unsignedp)
11080 return type;
11081
11082 if (TREE_CODE (type) == VECTOR_TYPE)
11083 {
11084 tree inner = TREE_TYPE (type);
11085 tree inner2 = signed_or_unsigned_type_for (unsignedp, type: inner);
11086 if (!inner2)
11087 return NULL_TREE;
11088 if (inner == inner2)
11089 return type;
11090 machine_mode new_mode;
11091 if (VECTOR_MODE_P (TYPE_MODE (type))
11092 && related_int_vector_mode (TYPE_MODE (type)).exists (mode: &new_mode))
11093 return build_vector_type_for_mode (innertype: inner2, mode: new_mode);
11094 return build_vector_type (innertype: inner2, nunits: TYPE_VECTOR_SUBPARTS (node: type));
11095 }
11096
11097 if (TREE_CODE (type) == COMPLEX_TYPE)
11098 {
11099 tree inner = TREE_TYPE (type);
11100 tree inner2 = signed_or_unsigned_type_for (unsignedp, type: inner);
11101 if (!inner2)
11102 return NULL_TREE;
11103 if (inner == inner2)
11104 return type;
11105 return build_complex_type (component_type: inner2);
11106 }
11107
11108 unsigned int bits;
11109 if (INTEGRAL_TYPE_P (type)
11110 || POINTER_TYPE_P (type)
11111 || TREE_CODE (type) == OFFSET_TYPE)
11112 bits = TYPE_PRECISION (type);
11113 else if (TREE_CODE (type) == REAL_TYPE)
11114 bits = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (type));
11115 else
11116 return NULL_TREE;
11117
11118 if (TREE_CODE (type) == BITINT_TYPE && (unsignedp || bits > 1))
11119 return build_bitint_type (precision: bits, unsignedp);
11120 return build_nonstandard_integer_type (precision: bits, unsignedp);
11121}
11122
11123/* If TYPE is an integral or pointer type, return an integer type with
11124 the same precision which is unsigned, or itself if TYPE is already an
11125 unsigned integer type. If TYPE is a floating-point type, return an
11126 unsigned integer type with the same bitsize as TYPE. */
11127
11128tree
11129unsigned_type_for (tree type)
11130{
11131 return signed_or_unsigned_type_for (unsignedp: 1, type);
11132}
11133
11134/* If TYPE is an integral or pointer type, return an integer type with
11135 the same precision which is signed, or itself if TYPE is already a
11136 signed integer type. If TYPE is a floating-point type, return a
11137 signed integer type with the same bitsize as TYPE. */
11138
11139tree
11140signed_type_for (tree type)
11141{
11142 return signed_or_unsigned_type_for (unsignedp: 0, type);
11143}
11144
11145/* - For VECTOR_TYPEs:
11146 - The truth type must be a VECTOR_BOOLEAN_TYPE.
11147 - The number of elements must match (known_eq).
11148 - targetm.vectorize.get_mask_mode exists, and exactly
11149 the same mode as the truth type.
11150 - Otherwise, the truth type must be a BOOLEAN_TYPE
11151 or useless_type_conversion_p to BOOLEAN_TYPE. */
11152bool
11153is_truth_type_for (tree type, tree truth_type)
11154{
11155 machine_mode mask_mode = TYPE_MODE (truth_type);
11156 machine_mode vmode = TYPE_MODE (type);
11157 machine_mode tmask_mode;
11158
11159 if (TREE_CODE (type) == VECTOR_TYPE)
11160 {
11161 if (VECTOR_BOOLEAN_TYPE_P (truth_type)
11162 && known_eq (TYPE_VECTOR_SUBPARTS (type),
11163 TYPE_VECTOR_SUBPARTS (truth_type))
11164 && targetm.vectorize.get_mask_mode (vmode).exists (mode: &tmask_mode)
11165 && tmask_mode == mask_mode)
11166 return true;
11167
11168 return false;
11169 }
11170
11171 return useless_type_conversion_p (boolean_type_node, truth_type);
11172}
11173
11174/* If TYPE is a vector type, return a signed integer vector type with the
11175 same width and number of subparts. Otherwise return boolean_type_node. */
11176
11177tree
11178truth_type_for (tree type)
11179{
11180 if (TREE_CODE (type) == VECTOR_TYPE)
11181 {
11182 if (VECTOR_BOOLEAN_TYPE_P (type))
11183 return type;
11184 return build_truth_vector_type_for (vectype: type);
11185 }
11186 else
11187 return boolean_type_node;
11188}
11189
11190/* Returns the largest value obtainable by casting something in INNER type to
11191 OUTER type. */
11192
11193tree
11194upper_bound_in_type (tree outer, tree inner)
11195{
11196 unsigned int det = 0;
11197 unsigned oprec = TYPE_PRECISION (outer);
11198 unsigned iprec = TYPE_PRECISION (inner);
11199 unsigned prec;
11200
11201 /* Compute a unique number for every combination. */
11202 det |= (oprec > iprec) ? 4 : 0;
11203 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
11204 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
11205
11206 /* Determine the exponent to use. */
11207 switch (det)
11208 {
11209 case 0:
11210 case 1:
11211 /* oprec <= iprec, outer: signed, inner: don't care. */
11212 prec = oprec - 1;
11213 break;
11214 case 2:
11215 case 3:
11216 /* oprec <= iprec, outer: unsigned, inner: don't care. */
11217 prec = oprec;
11218 break;
11219 case 4:
11220 /* oprec > iprec, outer: signed, inner: signed. */
11221 prec = iprec - 1;
11222 break;
11223 case 5:
11224 /* oprec > iprec, outer: signed, inner: unsigned. */
11225 prec = iprec;
11226 break;
11227 case 6:
11228 /* oprec > iprec, outer: unsigned, inner: signed. */
11229 prec = oprec;
11230 break;
11231 case 7:
11232 /* oprec > iprec, outer: unsigned, inner: unsigned. */
11233 prec = iprec;
11234 break;
11235 default:
11236 gcc_unreachable ();
11237 }
11238
11239 return wide_int_to_tree (type: outer,
11240 value: wi::mask (width: prec, negate_p: false, TYPE_PRECISION (outer)));
11241}
11242
11243/* Returns the smallest value obtainable by casting something in INNER type to
11244 OUTER type. */
11245
11246tree
11247lower_bound_in_type (tree outer, tree inner)
11248{
11249 unsigned oprec = TYPE_PRECISION (outer);
11250 unsigned iprec = TYPE_PRECISION (inner);
11251
11252 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
11253 and obtain 0. */
11254 if (TYPE_UNSIGNED (outer)
11255 /* If we are widening something of an unsigned type, OUTER type
11256 contains all values of INNER type. In particular, both INNER
11257 and OUTER types have zero in common. */
11258 || (oprec > iprec && TYPE_UNSIGNED (inner)))
11259 return build_int_cst (type: outer, cst: 0);
11260 else
11261 {
11262 /* If we are widening a signed type to another signed type, we
11263 want to obtain -2^^(iprec-1). If we are keeping the
11264 precision or narrowing to a signed type, we want to obtain
11265 -2^(oprec-1). */
11266 unsigned prec = oprec > iprec ? iprec : oprec;
11267 return wide_int_to_tree (type: outer,
11268 value: wi::mask (width: prec - 1, negate_p: true,
11269 TYPE_PRECISION (outer)));
11270 }
11271}
11272
11273/* Return true if two operands that are suitable for PHI nodes are
11274 necessarily equal. Specifically, both ARG0 and ARG1 must be either
11275 SSA_NAME or invariant. Note that this is strictly an optimization.
11276 That is, callers of this function can directly call operand_equal_p
11277 and get the same result, only slower. */
11278
11279bool
11280operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
11281{
11282 if (arg0 == arg1)
11283 return true;
11284 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
11285 return false;
11286 return operand_equal_p (arg0, arg1, flags: 0);
11287}
11288
11289/* Returns number of zeros at the end of binary representation of X. */
11290
11291tree
11292num_ending_zeros (const_tree x)
11293{
11294 return build_int_cst (TREE_TYPE (x), cst: wi::ctz (wi::to_wide (t: x)));
11295}
11296
11297
11298#define WALK_SUBTREE(NODE) \
11299 do \
11300 { \
11301 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
11302 if (result) \
11303 return result; \
11304 } \
11305 while (0)
11306
11307/* This is a subroutine of walk_tree that walks field of TYPE that are to
11308 be walked whenever a type is seen in the tree. Rest of operands and return
11309 value are as for walk_tree. */
11310
11311static tree
11312walk_type_fields (tree type, walk_tree_fn func, void *data,
11313 hash_set<tree> *pset, walk_tree_lh lh)
11314{
11315 tree result = NULL_TREE;
11316
11317 switch (TREE_CODE (type))
11318 {
11319 case POINTER_TYPE:
11320 case REFERENCE_TYPE:
11321 case VECTOR_TYPE:
11322 /* We have to worry about mutually recursive pointers. These can't
11323 be written in C. They can in Ada. It's pathological, but
11324 there's an ACATS test (c38102a) that checks it. Deal with this
11325 by checking if we're pointing to another pointer, that one
11326 points to another pointer, that one does too, and we have no htab.
11327 If so, get a hash table. We check three levels deep to avoid
11328 the cost of the hash table if we don't need one. */
11329 if (POINTER_TYPE_P (TREE_TYPE (type))
11330 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
11331 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
11332 && !pset)
11333 {
11334 result = walk_tree_without_duplicates (&TREE_TYPE (type),
11335 func, data);
11336 if (result)
11337 return result;
11338
11339 break;
11340 }
11341
11342 /* fall through */
11343
11344 case COMPLEX_TYPE:
11345 WALK_SUBTREE (TREE_TYPE (type));
11346 break;
11347
11348 case METHOD_TYPE:
11349 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
11350
11351 /* Fall through. */
11352
11353 case FUNCTION_TYPE:
11354 WALK_SUBTREE (TREE_TYPE (type));
11355 {
11356 tree arg;
11357
11358 /* We never want to walk into default arguments. */
11359 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
11360 WALK_SUBTREE (TREE_VALUE (arg));
11361 }
11362 break;
11363
11364 case ARRAY_TYPE:
11365 /* Don't follow this nodes's type if a pointer for fear that
11366 we'll have infinite recursion. If we have a PSET, then we
11367 need not fear. */
11368 if (pset
11369 || (!POINTER_TYPE_P (TREE_TYPE (type))
11370 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
11371 WALK_SUBTREE (TREE_TYPE (type));
11372 WALK_SUBTREE (TYPE_DOMAIN (type));
11373 break;
11374
11375 case OFFSET_TYPE:
11376 WALK_SUBTREE (TREE_TYPE (type));
11377 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
11378 break;
11379
11380 default:
11381 break;
11382 }
11383
11384 return NULL_TREE;
11385}
11386
11387/* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
11388 called with the DATA and the address of each sub-tree. If FUNC returns a
11389 non-NULL value, the traversal is stopped, and the value returned by FUNC
11390 is returned. If PSET is non-NULL it is used to record the nodes visited,
11391 and to avoid visiting a node more than once. */
11392
11393tree
11394walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
11395 hash_set<tree> *pset, walk_tree_lh lh)
11396{
11397#define WALK_SUBTREE_TAIL(NODE) \
11398 do \
11399 { \
11400 tp = & (NODE); \
11401 goto tail_recurse; \
11402 } \
11403 while (0)
11404
11405 tail_recurse:
11406 /* Skip empty subtrees. */
11407 if (!*tp)
11408 return NULL_TREE;
11409
11410 /* Don't walk the same tree twice, if the user has requested
11411 that we avoid doing so. */
11412 if (pset && pset->add (k: *tp))
11413 return NULL_TREE;
11414
11415 /* Call the function. */
11416 int walk_subtrees = 1;
11417 tree result = (*func) (tp, &walk_subtrees, data);
11418
11419 /* If we found something, return it. */
11420 if (result)
11421 return result;
11422
11423 tree t = *tp;
11424 tree_code code = TREE_CODE (t);
11425
11426 /* Even if we didn't, FUNC may have decided that there was nothing
11427 interesting below this point in the tree. */
11428 if (!walk_subtrees)
11429 {
11430 /* But we still need to check our siblings. */
11431 if (code == TREE_LIST)
11432 WALK_SUBTREE_TAIL (TREE_CHAIN (t));
11433 else if (code == OMP_CLAUSE)
11434 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (t));
11435 else
11436 return NULL_TREE;
11437 }
11438
11439 if (lh)
11440 {
11441 result = (*lh) (tp, &walk_subtrees, func, data, pset);
11442 if (result || !walk_subtrees)
11443 return result;
11444 }
11445
11446 switch (code)
11447 {
11448 case ERROR_MARK:
11449 case IDENTIFIER_NODE:
11450 case INTEGER_CST:
11451 case REAL_CST:
11452 case FIXED_CST:
11453 case STRING_CST:
11454 case BLOCK:
11455 case PLACEHOLDER_EXPR:
11456 case SSA_NAME:
11457 case FIELD_DECL:
11458 case RESULT_DECL:
11459 /* None of these have subtrees other than those already walked
11460 above. */
11461 break;
11462
11463 case TREE_LIST:
11464 WALK_SUBTREE (TREE_VALUE (t));
11465 WALK_SUBTREE_TAIL (TREE_CHAIN (t));
11466
11467 case TREE_VEC:
11468 {
11469 int len = TREE_VEC_LENGTH (t);
11470
11471 if (len == 0)
11472 break;
11473
11474 /* Walk all elements but the last. */
11475 for (int i = 0; i < len - 1; ++i)
11476 WALK_SUBTREE (TREE_VEC_ELT (t, i));
11477
11478 /* Now walk the last one as a tail call. */
11479 WALK_SUBTREE_TAIL (TREE_VEC_ELT (t, len - 1));
11480 }
11481
11482 case VECTOR_CST:
11483 {
11484 unsigned len = vector_cst_encoded_nelts (t);
11485 if (len == 0)
11486 break;
11487 /* Walk all elements but the last. */
11488 for (unsigned i = 0; i < len - 1; ++i)
11489 WALK_SUBTREE (VECTOR_CST_ENCODED_ELT (t, i));
11490 /* Now walk the last one as a tail call. */
11491 WALK_SUBTREE_TAIL (VECTOR_CST_ENCODED_ELT (t, len - 1));
11492 }
11493
11494 case COMPLEX_CST:
11495 WALK_SUBTREE (TREE_REALPART (t));
11496 WALK_SUBTREE_TAIL (TREE_IMAGPART (t));
11497
11498 case CONSTRUCTOR:
11499 {
11500 unsigned HOST_WIDE_INT idx;
11501 constructor_elt *ce;
11502
11503 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), ix: idx, ptr: &ce);
11504 idx++)
11505 WALK_SUBTREE (ce->value);
11506 }
11507 break;
11508
11509 case SAVE_EXPR:
11510 WALK_SUBTREE_TAIL (TREE_OPERAND (t, 0));
11511
11512 case BIND_EXPR:
11513 {
11514 tree decl;
11515 for (decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
11516 {
11517 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11518 into declarations that are just mentioned, rather than
11519 declared; they don't really belong to this part of the tree.
11520 And, we can see cycles: the initializer for a declaration
11521 can refer to the declaration itself. */
11522 WALK_SUBTREE (DECL_INITIAL (decl));
11523 WALK_SUBTREE (DECL_SIZE (decl));
11524 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11525 }
11526 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (t));
11527 }
11528
11529 case STATEMENT_LIST:
11530 {
11531 tree_stmt_iterator i;
11532 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (i: &i))
11533 WALK_SUBTREE (*tsi_stmt_ptr (i));
11534 }
11535 break;
11536
11537 case OMP_CLAUSE:
11538 {
11539 int len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
11540 for (int i = 0; i < len; i++)
11541 WALK_SUBTREE (OMP_CLAUSE_OPERAND (t, i));
11542 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (t));
11543 }
11544
11545 case TARGET_EXPR:
11546 {
11547 int i, len;
11548
11549 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11550 But, we only want to walk once. */
11551 len = (TREE_OPERAND (t, 3) == TREE_OPERAND (t, 1)) ? 2 : 3;
11552 for (i = 0; i < len; ++i)
11553 WALK_SUBTREE (TREE_OPERAND (t, i));
11554 WALK_SUBTREE_TAIL (TREE_OPERAND (t, len));
11555 }
11556
11557 case DECL_EXPR:
11558 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11559 defining. We only want to walk into these fields of a type in this
11560 case and not in the general case of a mere reference to the type.
11561
11562 The criterion is as follows: if the field can be an expression, it
11563 must be walked only here. This should be in keeping with the fields
11564 that are directly gimplified in gimplify_type_sizes in order for the
11565 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11566 variable-sized types.
11567
11568 Note that DECLs get walked as part of processing the BIND_EXPR. */
11569 if (TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
11570 {
11571 /* Call the function for the decl so e.g. copy_tree_body_r can
11572 replace it with the remapped one. */
11573 result = (*func) (&DECL_EXPR_DECL (t), &walk_subtrees, data);
11574 if (result || !walk_subtrees)
11575 return result;
11576
11577 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (t));
11578 if (TREE_CODE (*type_p) == ERROR_MARK)
11579 return NULL_TREE;
11580
11581 /* Call the function for the type. See if it returns anything or
11582 doesn't want us to continue. If we are to continue, walk both
11583 the normal fields and those for the declaration case. */
11584 result = (*func) (type_p, &walk_subtrees, data);
11585 if (result || !walk_subtrees)
11586 return result;
11587
11588 tree type = *type_p;
11589
11590 /* But do not walk a pointed-to type since it may itself need to
11591 be walked in the declaration case if it isn't anonymous. */
11592 if (!POINTER_TYPE_P (type))
11593 {
11594 result = walk_type_fields (type, func, data, pset, lh);
11595 if (result)
11596 return result;
11597 }
11598
11599 /* If this is a record type, also walk the fields. */
11600 if (RECORD_OR_UNION_TYPE_P (type))
11601 {
11602 tree field;
11603
11604 for (field = TYPE_FIELDS (type); field;
11605 field = DECL_CHAIN (field))
11606 {
11607 /* We'd like to look at the type of the field, but we can
11608 easily get infinite recursion. So assume it's pointed
11609 to elsewhere in the tree. Also, ignore things that
11610 aren't fields. */
11611 if (TREE_CODE (field) != FIELD_DECL)
11612 continue;
11613
11614 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11615 WALK_SUBTREE (DECL_SIZE (field));
11616 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11617 if (TREE_CODE (type) == QUAL_UNION_TYPE)
11618 WALK_SUBTREE (DECL_QUALIFIER (field));
11619 }
11620 }
11621
11622 /* Same for scalar types. */
11623 else if (TREE_CODE (type) == BOOLEAN_TYPE
11624 || TREE_CODE (type) == ENUMERAL_TYPE
11625 || TREE_CODE (type) == INTEGER_TYPE
11626 || TREE_CODE (type) == FIXED_POINT_TYPE
11627 || TREE_CODE (type) == REAL_TYPE)
11628 {
11629 WALK_SUBTREE (TYPE_MIN_VALUE (type));
11630 WALK_SUBTREE (TYPE_MAX_VALUE (type));
11631 }
11632
11633 WALK_SUBTREE (TYPE_SIZE (type));
11634 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (type));
11635 }
11636 /* FALLTHRU */
11637
11638 default:
11639 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11640 {
11641 int i, len;
11642
11643 /* Walk over all the sub-trees of this operand. */
11644 len = TREE_OPERAND_LENGTH (t);
11645
11646 /* Go through the subtrees. We need to do this in forward order so
11647 that the scope of a FOR_EXPR is handled properly. */
11648 if (len)
11649 {
11650 for (i = 0; i < len - 1; ++i)
11651 WALK_SUBTREE (TREE_OPERAND (t, i));
11652 WALK_SUBTREE_TAIL (TREE_OPERAND (t, len - 1));
11653 }
11654 }
11655 /* If this is a type, walk the needed fields in the type. */
11656 else if (TYPE_P (t))
11657 return walk_type_fields (type: t, func, data, pset, lh);
11658 break;
11659 }
11660
11661 /* We didn't find what we were looking for. */
11662 return NULL_TREE;
11663
11664#undef WALK_SUBTREE_TAIL
11665}
11666#undef WALK_SUBTREE
11667
11668/* Like walk_tree, but does not walk duplicate nodes more than once. */
11669
11670tree
11671walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11672 walk_tree_lh lh)
11673{
11674 tree result;
11675
11676 hash_set<tree> pset;
11677 result = walk_tree_1 (tp, func, data, pset: &pset, lh);
11678 return result;
11679}
11680
11681
11682tree
11683tree_block (tree t)
11684{
11685 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11686
11687 if (IS_EXPR_CODE_CLASS (c))
11688 return LOCATION_BLOCK (t->exp.locus);
11689 gcc_unreachable ();
11690 return NULL;
11691}
11692
11693void
11694tree_set_block (tree t, tree b)
11695{
11696 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11697
11698 if (IS_EXPR_CODE_CLASS (c))
11699 {
11700 t->exp.locus = set_block (loc: t->exp.locus, block: b);
11701 }
11702 else
11703 gcc_unreachable ();
11704}
11705
11706/* Create a nameless artificial label and put it in the current
11707 function context. The label has a location of LOC. Returns the
11708 newly created label. */
11709
11710tree
11711create_artificial_label (location_t loc)
11712{
11713 tree lab = build_decl (loc,
11714 code: LABEL_DECL, NULL_TREE, void_type_node);
11715
11716 DECL_ARTIFICIAL (lab) = 1;
11717 DECL_IGNORED_P (lab) = 1;
11718 DECL_CONTEXT (lab) = current_function_decl;
11719 return lab;
11720}
11721
11722/* Given a tree, try to return a useful variable name that we can use
11723 to prefix a temporary that is being assigned the value of the tree.
11724 I.E. given <temp> = &A, return A. */
11725
11726const char *
11727get_name (tree t)
11728{
11729 tree stripped_decl;
11730
11731 stripped_decl = t;
11732 STRIP_NOPS (stripped_decl);
11733 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11734 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11735 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11736 {
11737 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11738 if (!name)
11739 return NULL;
11740 return IDENTIFIER_POINTER (name);
11741 }
11742 else
11743 {
11744 switch (TREE_CODE (stripped_decl))
11745 {
11746 case ADDR_EXPR:
11747 return get_name (TREE_OPERAND (stripped_decl, 0));
11748 default:
11749 return NULL;
11750 }
11751 }
11752}
11753
11754/* Return true if TYPE has a variable argument list. */
11755
11756bool
11757stdarg_p (const_tree fntype)
11758{
11759 function_args_iterator args_iter;
11760 tree n = NULL_TREE, t;
11761
11762 if (!fntype)
11763 return false;
11764
11765 if (TYPE_NO_NAMED_ARGS_STDARG_P (fntype))
11766 return true;
11767
11768 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
11769 {
11770 n = t;
11771 }
11772
11773 return n != NULL_TREE && n != void_type_node;
11774}
11775
11776/* Return true if TYPE has a prototype. */
11777
11778bool
11779prototype_p (const_tree fntype)
11780{
11781 tree t;
11782
11783 gcc_assert (fntype != NULL_TREE);
11784
11785 if (TYPE_NO_NAMED_ARGS_STDARG_P (fntype))
11786 return true;
11787
11788 t = TYPE_ARG_TYPES (fntype);
11789 return (t != NULL_TREE);
11790}
11791
11792/* If BLOCK is inlined from an __attribute__((__artificial__))
11793 routine, return pointer to location from where it has been
11794 called. */
11795location_t *
11796block_nonartificial_location (tree block)
11797{
11798 location_t *ret = NULL;
11799
11800 while (block && TREE_CODE (block) == BLOCK
11801 && BLOCK_ABSTRACT_ORIGIN (block))
11802 {
11803 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11804 if (TREE_CODE (ao) == FUNCTION_DECL)
11805 {
11806 /* If AO is an artificial inline, point RET to the
11807 call site locus at which it has been inlined and continue
11808 the loop, in case AO's caller is also an artificial
11809 inline. */
11810 if (DECL_DECLARED_INLINE_P (ao)
11811 && lookup_attribute (attr_name: "artificial", DECL_ATTRIBUTES (ao)))
11812 ret = &BLOCK_SOURCE_LOCATION (block);
11813 else
11814 break;
11815 }
11816 else if (TREE_CODE (ao) != BLOCK)
11817 break;
11818
11819 block = BLOCK_SUPERCONTEXT (block);
11820 }
11821 return ret;
11822}
11823
11824
11825/* If EXP is inlined from an __attribute__((__artificial__))
11826 function, return the location of the original call expression. */
11827
11828location_t
11829tree_nonartificial_location (tree exp)
11830{
11831 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11832
11833 if (loc)
11834 return *loc;
11835 else
11836 return EXPR_LOCATION (exp);
11837}
11838
11839/* Return the location into which EXP has been inlined. Analogous
11840 to tree_nonartificial_location() above but not limited to artificial
11841 functions declared inline. If SYSTEM_HEADER is true, return
11842 the macro expansion point of the location if it's in a system header */
11843
11844location_t
11845tree_inlined_location (tree exp, bool system_header /* = true */)
11846{
11847 location_t loc = UNKNOWN_LOCATION;
11848
11849 tree block = TREE_BLOCK (exp);
11850
11851 while (block && TREE_CODE (block) == BLOCK
11852 && BLOCK_ABSTRACT_ORIGIN (block))
11853 {
11854 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11855 if (TREE_CODE (ao) == FUNCTION_DECL)
11856 loc = BLOCK_SOURCE_LOCATION (block);
11857 else if (TREE_CODE (ao) != BLOCK)
11858 break;
11859
11860 block = BLOCK_SUPERCONTEXT (block);
11861 }
11862
11863 if (loc == UNKNOWN_LOCATION)
11864 {
11865 loc = EXPR_LOCATION (exp);
11866 if (system_header)
11867 /* Only consider macro expansion when the block traversal failed
11868 to find a location. Otherwise it's not relevant. */
11869 return expansion_point_location_if_in_system_header (loc);
11870 }
11871
11872 return loc;
11873}
11874
11875/* These are the hash table functions for the hash table of OPTIMIZATION_NODE
11876 nodes. */
11877
11878/* Return the hash code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11879
11880hashval_t
11881cl_option_hasher::hash (tree x)
11882{
11883 const_tree const t = x;
11884
11885 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11886 return cl_optimization_hash (TREE_OPTIMIZATION (t));
11887 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11888 return cl_target_option_hash (TREE_TARGET_OPTION (t));
11889 else
11890 gcc_unreachable ();
11891}
11892
11893/* Return nonzero if the value represented by *X (an OPTIMIZATION or
11894 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11895 same. */
11896
11897bool
11898cl_option_hasher::equal (tree x, tree y)
11899{
11900 const_tree const xt = x;
11901 const_tree const yt = y;
11902
11903 if (TREE_CODE (xt) != TREE_CODE (yt))
11904 return false;
11905
11906 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11907 return cl_optimization_option_eq (TREE_OPTIMIZATION (xt),
11908 TREE_OPTIMIZATION (yt));
11909 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11910 return cl_target_option_eq (TREE_TARGET_OPTION (xt),
11911 TREE_TARGET_OPTION (yt));
11912 else
11913 gcc_unreachable ();
11914}
11915
11916/* Build an OPTIMIZATION_NODE based on the options in OPTS and OPTS_SET. */
11917
11918tree
11919build_optimization_node (struct gcc_options *opts,
11920 struct gcc_options *opts_set)
11921{
11922 tree t;
11923
11924 /* Use the cache of optimization nodes. */
11925
11926 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11927 opts, opts_set);
11928
11929 tree *slot = cl_option_hash_table->find_slot (value: cl_optimization_node, insert: INSERT);
11930 t = *slot;
11931 if (!t)
11932 {
11933 /* Insert this one into the hash table. */
11934 t = cl_optimization_node;
11935 *slot = t;
11936
11937 /* Make a new node for next time round. */
11938 cl_optimization_node = make_node (code: OPTIMIZATION_NODE);
11939 }
11940
11941 return t;
11942}
11943
11944/* Build a TARGET_OPTION_NODE based on the options in OPTS and OPTS_SET. */
11945
11946tree
11947build_target_option_node (struct gcc_options *opts,
11948 struct gcc_options *opts_set)
11949{
11950 tree t;
11951
11952 /* Use the cache of optimization nodes. */
11953
11954 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11955 opts, opts_set);
11956
11957 tree *slot = cl_option_hash_table->find_slot (value: cl_target_option_node, insert: INSERT);
11958 t = *slot;
11959 if (!t)
11960 {
11961 /* Insert this one into the hash table. */
11962 t = cl_target_option_node;
11963 *slot = t;
11964
11965 /* Make a new node for next time round. */
11966 cl_target_option_node = make_node (code: TARGET_OPTION_NODE);
11967 }
11968
11969 return t;
11970}
11971
11972/* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
11973 so that they aren't saved during PCH writing. */
11974
11975void
11976prepare_target_option_nodes_for_pch (void)
11977{
11978 hash_table<cl_option_hasher>::iterator iter = cl_option_hash_table->begin ();
11979 for (; iter != cl_option_hash_table->end (); ++iter)
11980 if (TREE_CODE (*iter) == TARGET_OPTION_NODE)
11981 TREE_TARGET_GLOBALS (*iter) = NULL;
11982}
11983
11984/* Determine the "ultimate origin" of a block. */
11985
11986tree
11987block_ultimate_origin (const_tree block)
11988{
11989 tree origin = BLOCK_ABSTRACT_ORIGIN (block);
11990
11991 if (origin == NULL_TREE)
11992 return NULL_TREE;
11993 else
11994 {
11995 gcc_checking_assert ((DECL_P (origin)
11996 && DECL_ORIGIN (origin) == origin)
11997 || BLOCK_ORIGIN (origin) == origin);
11998 return origin;
11999 }
12000}
12001
12002/* Return true iff conversion from INNER_TYPE to OUTER_TYPE generates
12003 no instruction. */
12004
12005bool
12006tree_nop_conversion_p (const_tree outer_type, const_tree inner_type)
12007{
12008 /* Do not strip casts into or out of differing address spaces. */
12009 if (POINTER_TYPE_P (outer_type)
12010 && TYPE_ADDR_SPACE (TREE_TYPE (outer_type)) != ADDR_SPACE_GENERIC)
12011 {
12012 if (!POINTER_TYPE_P (inner_type)
12013 || (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
12014 != TYPE_ADDR_SPACE (TREE_TYPE (inner_type))))
12015 return false;
12016 }
12017 else if (POINTER_TYPE_P (inner_type)
12018 && TYPE_ADDR_SPACE (TREE_TYPE (inner_type)) != ADDR_SPACE_GENERIC)
12019 {
12020 /* We already know that outer_type is not a pointer with
12021 a non-generic address space. */
12022 return false;
12023 }
12024
12025 /* Use precision rather then machine mode when we can, which gives
12026 the correct answer even for submode (bit-field) types. */
12027 if ((INTEGRAL_TYPE_P (outer_type)
12028 || POINTER_TYPE_P (outer_type)
12029 || TREE_CODE (outer_type) == OFFSET_TYPE)
12030 && (INTEGRAL_TYPE_P (inner_type)
12031 || POINTER_TYPE_P (inner_type)
12032 || TREE_CODE (inner_type) == OFFSET_TYPE))
12033 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
12034
12035 /* Otherwise fall back on comparing machine modes (e.g. for
12036 aggregate types, floats). */
12037 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
12038}
12039
12040/* Return true iff conversion in EXP generates no instruction. Mark
12041 it inline so that we fully inline into the stripping functions even
12042 though we have two uses of this function. */
12043
12044static inline bool
12045tree_nop_conversion (const_tree exp)
12046{
12047 tree outer_type, inner_type;
12048
12049 if (location_wrapper_p (exp))
12050 return true;
12051 if (!CONVERT_EXPR_P (exp)
12052 && TREE_CODE (exp) != NON_LVALUE_EXPR)
12053 return false;
12054
12055 outer_type = TREE_TYPE (exp);
12056 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
12057 if (!inner_type || inner_type == error_mark_node)
12058 return false;
12059
12060 return tree_nop_conversion_p (outer_type, inner_type);
12061}
12062
12063/* Return true iff conversion in EXP generates no instruction. Don't
12064 consider conversions changing the signedness. */
12065
12066static bool
12067tree_sign_nop_conversion (const_tree exp)
12068{
12069 tree outer_type, inner_type;
12070
12071 if (!tree_nop_conversion (exp))
12072 return false;
12073
12074 outer_type = TREE_TYPE (exp);
12075 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
12076
12077 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
12078 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
12079}
12080
12081/* Strip conversions from EXP according to tree_nop_conversion and
12082 return the resulting expression. */
12083
12084tree
12085tree_strip_nop_conversions (tree exp)
12086{
12087 while (tree_nop_conversion (exp))
12088 exp = TREE_OPERAND (exp, 0);
12089 return exp;
12090}
12091
12092/* Strip conversions from EXP according to tree_sign_nop_conversion
12093 and return the resulting expression. */
12094
12095tree
12096tree_strip_sign_nop_conversions (tree exp)
12097{
12098 while (tree_sign_nop_conversion (exp))
12099 exp = TREE_OPERAND (exp, 0);
12100 return exp;
12101}
12102
12103/* Avoid any floating point extensions from EXP. */
12104tree
12105strip_float_extensions (tree exp)
12106{
12107 tree sub, expt, subt;
12108
12109 /* For floating point constant look up the narrowest type that can hold
12110 it properly and handle it like (type)(narrowest_type)constant.
12111 This way we can optimize for instance a=a*2.0 where "a" is float
12112 but 2.0 is double constant. */
12113 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
12114 {
12115 REAL_VALUE_TYPE orig;
12116 tree type = NULL;
12117
12118 orig = TREE_REAL_CST (exp);
12119 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
12120 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
12121 type = float_type_node;
12122 else if (TYPE_PRECISION (TREE_TYPE (exp))
12123 > TYPE_PRECISION (double_type_node)
12124 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
12125 type = double_type_node;
12126 if (type)
12127 return build_real_truncate (type, d: orig);
12128 }
12129
12130 if (!CONVERT_EXPR_P (exp))
12131 return exp;
12132
12133 sub = TREE_OPERAND (exp, 0);
12134 subt = TREE_TYPE (sub);
12135 expt = TREE_TYPE (exp);
12136
12137 if (!FLOAT_TYPE_P (subt))
12138 return exp;
12139
12140 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
12141 return exp;
12142
12143 if (element_precision (type: subt) > element_precision (type: expt))
12144 return exp;
12145
12146 return strip_float_extensions (exp: sub);
12147}
12148
12149/* Strip out all handled components that produce invariant
12150 offsets. */
12151
12152const_tree
12153strip_invariant_refs (const_tree op)
12154{
12155 while (handled_component_p (t: op))
12156 {
12157 switch (TREE_CODE (op))
12158 {
12159 case ARRAY_REF:
12160 case ARRAY_RANGE_REF:
12161 if (!is_gimple_constant (TREE_OPERAND (op, 1))
12162 || TREE_OPERAND (op, 2) != NULL_TREE
12163 || TREE_OPERAND (op, 3) != NULL_TREE)
12164 return NULL;
12165 break;
12166
12167 case COMPONENT_REF:
12168 if (TREE_OPERAND (op, 2) != NULL_TREE)
12169 return NULL;
12170 break;
12171
12172 default:;
12173 }
12174 op = TREE_OPERAND (op, 0);
12175 }
12176
12177 return op;
12178}
12179
12180/* Strip handled components with zero offset from OP. */
12181
12182tree
12183strip_zero_offset_components (tree op)
12184{
12185 while (TREE_CODE (op) == COMPONENT_REF
12186 && integer_zerop (DECL_FIELD_OFFSET (TREE_OPERAND (op, 1)))
12187 && integer_zerop (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (op, 1))))
12188 op = TREE_OPERAND (op, 0);
12189 return op;
12190}
12191
12192static GTY(()) tree gcc_eh_personality_decl;
12193
12194/* Return the GCC personality function decl. */
12195
12196tree
12197lhd_gcc_personality (void)
12198{
12199 if (!gcc_eh_personality_decl)
12200 gcc_eh_personality_decl = build_personality_function ("gcc");
12201 return gcc_eh_personality_decl;
12202}
12203
12204/* TARGET is a call target of GIMPLE call statement
12205 (obtained by gimple_call_fn). Return true if it is
12206 OBJ_TYPE_REF representing an virtual call of C++ method.
12207 (As opposed to OBJ_TYPE_REF representing objc calls
12208 through a cast where middle-end devirtualization machinery
12209 can't apply.) FOR_DUMP_P is true when being called from
12210 the dump routines. */
12211
12212bool
12213virtual_method_call_p (const_tree target, bool for_dump_p)
12214{
12215 if (TREE_CODE (target) != OBJ_TYPE_REF)
12216 return false;
12217 tree t = TREE_TYPE (target);
12218 gcc_checking_assert (TREE_CODE (t) == POINTER_TYPE);
12219 t = TREE_TYPE (t);
12220 if (TREE_CODE (t) == FUNCTION_TYPE)
12221 return false;
12222 gcc_checking_assert (TREE_CODE (t) == METHOD_TYPE);
12223 /* If we do not have BINFO associated, it means that type was built
12224 without devirtualization enabled. Do not consider this a virtual
12225 call. */
12226 if (!TYPE_BINFO (obj_type_ref_class (target, for_dump_p)))
12227 return false;
12228 return true;
12229}
12230
12231/* Lookup sub-BINFO of BINFO of TYPE at offset POS. */
12232
12233static tree
12234lookup_binfo_at_offset (tree binfo, tree type, HOST_WIDE_INT pos)
12235{
12236 unsigned int i;
12237 tree base_binfo, b;
12238
12239 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12240 if (pos == tree_to_shwi (BINFO_OFFSET (base_binfo))
12241 && types_same_for_odr (TREE_TYPE (base_binfo), type2: type))
12242 return base_binfo;
12243 else if ((b = lookup_binfo_at_offset (binfo: base_binfo, type, pos)) != NULL)
12244 return b;
12245 return NULL;
12246}
12247
12248/* Try to find a base info of BINFO that would have its field decl at offset
12249 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
12250 found, return, otherwise return NULL_TREE. */
12251
12252tree
12253get_binfo_at_offset (tree binfo, poly_int64 offset, tree expected_type)
12254{
12255 tree type = BINFO_TYPE (binfo);
12256
12257 while (true)
12258 {
12259 HOST_WIDE_INT pos, size;
12260 tree fld;
12261 int i;
12262
12263 if (types_same_for_odr (type1: type, type2: expected_type))
12264 return binfo;
12265 if (maybe_lt (a: offset, b: 0))
12266 return NULL_TREE;
12267
12268 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
12269 {
12270 if (TREE_CODE (fld) != FIELD_DECL || !DECL_ARTIFICIAL (fld))
12271 continue;
12272
12273 pos = int_bit_position (field: fld);
12274 size = tree_to_uhwi (DECL_SIZE (fld));
12275 if (known_in_range_p (val: offset, pos, size))
12276 break;
12277 }
12278 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
12279 return NULL_TREE;
12280
12281 /* Offset 0 indicates the primary base, whose vtable contents are
12282 represented in the binfo for the derived class. */
12283 else if (maybe_ne (a: offset, b: 0))
12284 {
12285 tree found_binfo = NULL, base_binfo;
12286 /* Offsets in BINFO are in bytes relative to the whole structure
12287 while POS is in bits relative to the containing field. */
12288 int binfo_offset = (tree_to_shwi (BINFO_OFFSET (binfo)) + pos
12289 / BITS_PER_UNIT);
12290
12291 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12292 if (tree_to_shwi (BINFO_OFFSET (base_binfo)) == binfo_offset
12293 && types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
12294 {
12295 found_binfo = base_binfo;
12296 break;
12297 }
12298 if (found_binfo)
12299 binfo = found_binfo;
12300 else
12301 binfo = lookup_binfo_at_offset (binfo, TREE_TYPE (fld),
12302 pos: binfo_offset);
12303 }
12304
12305 type = TREE_TYPE (fld);
12306 offset -= pos;
12307 }
12308}
12309
12310/* PR 84195: Replace control characters in "unescaped" with their
12311 escaped equivalents. Allow newlines if -fmessage-length has
12312 been set to a non-zero value. This is done here, rather than
12313 where the attribute is recorded as the message length can
12314 change between these two locations. */
12315
12316void
12317escaped_string::escape (const char *unescaped)
12318{
12319 char *escaped;
12320 size_t i, new_i, len;
12321
12322 if (m_owned)
12323 free (ptr: m_str);
12324
12325 m_str = const_cast<char *> (unescaped);
12326 m_owned = false;
12327
12328 if (unescaped == NULL || *unescaped == 0)
12329 return;
12330
12331 len = strlen (s: unescaped);
12332 escaped = NULL;
12333 new_i = 0;
12334
12335 for (i = 0; i < len; i++)
12336 {
12337 char c = unescaped[i];
12338
12339 if (!ISCNTRL (c))
12340 {
12341 if (escaped)
12342 escaped[new_i++] = c;
12343 continue;
12344 }
12345
12346 if (c != '\n' || !pp_is_wrapping_line (global_dc->printer))
12347 {
12348 if (escaped == NULL)
12349 {
12350 /* We only allocate space for a new string if we
12351 actually encounter a control character that
12352 needs replacing. */
12353 escaped = (char *) xmalloc (len * 2 + 1);
12354 strncpy (dest: escaped, src: unescaped, n: i);
12355 new_i = i;
12356 }
12357
12358 escaped[new_i++] = '\\';
12359
12360 switch (c)
12361 {
12362 case '\a': escaped[new_i++] = 'a'; break;
12363 case '\b': escaped[new_i++] = 'b'; break;
12364 case '\f': escaped[new_i++] = 'f'; break;
12365 case '\n': escaped[new_i++] = 'n'; break;
12366 case '\r': escaped[new_i++] = 'r'; break;
12367 case '\t': escaped[new_i++] = 't'; break;
12368 case '\v': escaped[new_i++] = 'v'; break;
12369 default: escaped[new_i++] = '?'; break;
12370 }
12371 }
12372 else if (escaped)
12373 escaped[new_i++] = c;
12374 }
12375
12376 if (escaped)
12377 {
12378 escaped[new_i] = 0;
12379 m_str = escaped;
12380 m_owned = true;
12381 }
12382}
12383
12384/* Warn about a use of an identifier which was marked deprecated. Returns
12385 whether a warning was given. */
12386
12387bool
12388warn_deprecated_use (tree node, tree attr)
12389{
12390 escaped_string msg;
12391
12392 if (node == 0 || !warn_deprecated_decl)
12393 return false;
12394
12395 if (!attr)
12396 {
12397 if (DECL_P (node))
12398 attr = DECL_ATTRIBUTES (node);
12399 else if (TYPE_P (node))
12400 {
12401 tree decl = TYPE_STUB_DECL (node);
12402 if (decl)
12403 attr = TYPE_ATTRIBUTES (TREE_TYPE (decl));
12404 else if ((decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (node)))
12405 != NULL_TREE)
12406 {
12407 node = TREE_TYPE (decl);
12408 attr = TYPE_ATTRIBUTES (node);
12409 }
12410 }
12411 }
12412
12413 if (attr)
12414 attr = lookup_attribute (attr_name: "deprecated", list: attr);
12415
12416 if (attr)
12417 msg.escape (TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
12418
12419 bool w = false;
12420 if (DECL_P (node))
12421 {
12422 auto_diagnostic_group d;
12423 if (msg)
12424 w = warning (OPT_Wdeprecated_declarations,
12425 "%qD is deprecated: %s", node, (const char *) msg);
12426 else
12427 w = warning (OPT_Wdeprecated_declarations,
12428 "%qD is deprecated", node);
12429 if (w)
12430 inform (DECL_SOURCE_LOCATION (node), "declared here");
12431 }
12432 else if (TYPE_P (node))
12433 {
12434 tree what = NULL_TREE;
12435 tree decl = TYPE_STUB_DECL (node);
12436
12437 if (TYPE_NAME (node))
12438 {
12439 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12440 what = TYPE_NAME (node);
12441 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12442 && DECL_NAME (TYPE_NAME (node)))
12443 what = DECL_NAME (TYPE_NAME (node));
12444 }
12445
12446 auto_diagnostic_group d;
12447 if (what)
12448 {
12449 if (msg)
12450 w = warning (OPT_Wdeprecated_declarations,
12451 "%qE is deprecated: %s", what, (const char *) msg);
12452 else
12453 w = warning (OPT_Wdeprecated_declarations,
12454 "%qE is deprecated", what);
12455 }
12456 else
12457 {
12458 if (msg)
12459 w = warning (OPT_Wdeprecated_declarations,
12460 "type is deprecated: %s", (const char *) msg);
12461 else
12462 w = warning (OPT_Wdeprecated_declarations,
12463 "type is deprecated");
12464 }
12465
12466 if (w && decl)
12467 inform (DECL_SOURCE_LOCATION (decl), "declared here");
12468 }
12469
12470 return w;
12471}
12472
12473/* Error out with an identifier which was marked 'unavailable'. */
12474void
12475error_unavailable_use (tree node, tree attr)
12476{
12477 escaped_string msg;
12478
12479 if (node == 0)
12480 return;
12481
12482 if (!attr)
12483 {
12484 if (DECL_P (node))
12485 attr = DECL_ATTRIBUTES (node);
12486 else if (TYPE_P (node))
12487 {
12488 tree decl = TYPE_STUB_DECL (node);
12489 if (decl)
12490 attr = lookup_attribute (attr_name: "unavailable",
12491 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
12492 }
12493 }
12494
12495 if (attr)
12496 attr = lookup_attribute (attr_name: "unavailable", list: attr);
12497
12498 if (attr)
12499 msg.escape (TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
12500
12501 if (DECL_P (node))
12502 {
12503 auto_diagnostic_group d;
12504 if (msg)
12505 error ("%qD is unavailable: %s", node, (const char *) msg);
12506 else
12507 error ("%qD is unavailable", node);
12508 inform (DECL_SOURCE_LOCATION (node), "declared here");
12509 }
12510 else if (TYPE_P (node))
12511 {
12512 tree what = NULL_TREE;
12513 tree decl = TYPE_STUB_DECL (node);
12514
12515 if (TYPE_NAME (node))
12516 {
12517 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12518 what = TYPE_NAME (node);
12519 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12520 && DECL_NAME (TYPE_NAME (node)))
12521 what = DECL_NAME (TYPE_NAME (node));
12522 }
12523
12524 auto_diagnostic_group d;
12525 if (what)
12526 {
12527 if (msg)
12528 error ("%qE is unavailable: %s", what, (const char *) msg);
12529 else
12530 error ("%qE is unavailable", what);
12531 }
12532 else
12533 {
12534 if (msg)
12535 error ("type is unavailable: %s", (const char *) msg);
12536 else
12537 error ("type is unavailable");
12538 }
12539
12540 if (decl)
12541 inform (DECL_SOURCE_LOCATION (decl), "declared here");
12542 }
12543}
12544
12545/* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12546 somewhere in it. */
12547
12548bool
12549contains_bitfld_component_ref_p (const_tree ref)
12550{
12551 while (handled_component_p (t: ref))
12552 {
12553 if (TREE_CODE (ref) == COMPONENT_REF
12554 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12555 return true;
12556 ref = TREE_OPERAND (ref, 0);
12557 }
12558
12559 return false;
12560}
12561
12562/* Try to determine whether a TRY_CATCH expression can fall through.
12563 This is a subroutine of block_may_fallthru. */
12564
12565static bool
12566try_catch_may_fallthru (const_tree stmt)
12567{
12568 tree_stmt_iterator i;
12569
12570 /* If the TRY block can fall through, the whole TRY_CATCH can
12571 fall through. */
12572 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
12573 return true;
12574
12575 i = tsi_start (TREE_OPERAND (stmt, 1));
12576 switch (TREE_CODE (tsi_stmt (i)))
12577 {
12578 case CATCH_EXPR:
12579 /* We expect to see a sequence of CATCH_EXPR trees, each with a
12580 catch expression and a body. The whole TRY_CATCH may fall
12581 through iff any of the catch bodies falls through. */
12582 for (; !tsi_end_p (i); tsi_next (i: &i))
12583 {
12584 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
12585 return true;
12586 }
12587 return false;
12588
12589 case EH_FILTER_EXPR:
12590 /* The exception filter expression only matters if there is an
12591 exception. If the exception does not match EH_FILTER_TYPES,
12592 we will execute EH_FILTER_FAILURE, and we will fall through
12593 if that falls through. If the exception does match
12594 EH_FILTER_TYPES, the stack unwinder will continue up the
12595 stack, so we will not fall through. We don't know whether we
12596 will throw an exception which matches EH_FILTER_TYPES or not,
12597 so we just ignore EH_FILTER_TYPES and assume that we might
12598 throw an exception which doesn't match. */
12599 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
12600
12601 default:
12602 /* This case represents statements to be executed when an
12603 exception occurs. Those statements are implicitly followed
12604 by a RESX statement to resume execution after the exception.
12605 So in this case the TRY_CATCH never falls through. */
12606 return false;
12607 }
12608}
12609
12610/* Try to determine if we can fall out of the bottom of BLOCK. This guess
12611 need not be 100% accurate; simply be conservative and return true if we
12612 don't know. This is used only to avoid stupidly generating extra code.
12613 If we're wrong, we'll just delete the extra code later. */
12614
12615bool
12616block_may_fallthru (const_tree block)
12617{
12618 /* This CONST_CAST is okay because expr_last returns its argument
12619 unmodified and we assign it to a const_tree. */
12620 const_tree stmt = expr_last (CONST_CAST_TREE (block));
12621
12622 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
12623 {
12624 case GOTO_EXPR:
12625 case RETURN_EXPR:
12626 /* Easy cases. If the last statement of the block implies
12627 control transfer, then we can't fall through. */
12628 return false;
12629
12630 case SWITCH_EXPR:
12631 /* If there is a default: label or case labels cover all possible
12632 SWITCH_COND values, then the SWITCH_EXPR will transfer control
12633 to some case label in all cases and all we care is whether the
12634 SWITCH_BODY falls through. */
12635 if (SWITCH_ALL_CASES_P (stmt))
12636 return block_may_fallthru (SWITCH_BODY (stmt));
12637 return true;
12638
12639 case COND_EXPR:
12640 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
12641 return true;
12642 return block_may_fallthru (COND_EXPR_ELSE (stmt));
12643
12644 case BIND_EXPR:
12645 return block_may_fallthru (BIND_EXPR_BODY (stmt));
12646
12647 case TRY_CATCH_EXPR:
12648 return try_catch_may_fallthru (stmt);
12649
12650 case TRY_FINALLY_EXPR:
12651 /* The finally clause is always executed after the try clause,
12652 so if it does not fall through, then the try-finally will not
12653 fall through. Otherwise, if the try clause does not fall
12654 through, then when the finally clause falls through it will
12655 resume execution wherever the try clause was going. So the
12656 whole try-finally will only fall through if both the try
12657 clause and the finally clause fall through. */
12658 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
12659 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
12660
12661 case EH_ELSE_EXPR:
12662 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12663
12664 case MODIFY_EXPR:
12665 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
12666 stmt = TREE_OPERAND (stmt, 1);
12667 else
12668 return true;
12669 /* FALLTHRU */
12670
12671 case CALL_EXPR:
12672 /* Functions that do not return do not fall through. */
12673 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
12674
12675 case CLEANUP_POINT_EXPR:
12676 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12677
12678 case TARGET_EXPR:
12679 return block_may_fallthru (TREE_OPERAND (stmt, 1));
12680
12681 case ERROR_MARK:
12682 return true;
12683
12684 default:
12685 return lang_hooks.block_may_fallthru (stmt);
12686 }
12687}
12688
12689/* True if we are using EH to handle cleanups. */
12690static bool using_eh_for_cleanups_flag = false;
12691
12692/* This routine is called from front ends to indicate eh should be used for
12693 cleanups. */
12694void
12695using_eh_for_cleanups (void)
12696{
12697 using_eh_for_cleanups_flag = true;
12698}
12699
12700/* Query whether EH is used for cleanups. */
12701bool
12702using_eh_for_cleanups_p (void)
12703{
12704 return using_eh_for_cleanups_flag;
12705}
12706
12707/* Wrapper for tree_code_name to ensure that tree code is valid */
12708const char *
12709get_tree_code_name (enum tree_code code)
12710{
12711 const char *invalid = "<invalid tree code>";
12712
12713 /* The tree_code enum promotes to signed, but we could be getting
12714 invalid values, so force an unsigned comparison. */
12715 if (unsigned (code) >= MAX_TREE_CODES)
12716 {
12717 if ((unsigned)code == 0xa5a5)
12718 return "ggc_freed";
12719 return invalid;
12720 }
12721
12722 return tree_code_name[code];
12723}
12724
12725/* Drops the TREE_OVERFLOW flag from T. */
12726
12727tree
12728drop_tree_overflow (tree t)
12729{
12730 gcc_checking_assert (TREE_OVERFLOW (t));
12731
12732 /* For tree codes with a sharing machinery re-build the result. */
12733 if (poly_int_tree_p (t))
12734 return wide_int_to_tree (TREE_TYPE (t), value: wi::to_poly_wide (t));
12735
12736 /* For VECTOR_CST, remove the overflow bits from the encoded elements
12737 and canonicalize the result. */
12738 if (TREE_CODE (t) == VECTOR_CST)
12739 {
12740 tree_vector_builder builder;
12741 builder.new_unary_operation (TREE_TYPE (t), vec: t, allow_stepped_p: true);
12742 unsigned int count = builder.encoded_nelts ();
12743 for (unsigned int i = 0; i < count; ++i)
12744 {
12745 tree elt = VECTOR_CST_ELT (t, i);
12746 if (TREE_OVERFLOW (elt))
12747 elt = drop_tree_overflow (t: elt);
12748 builder.quick_push (obj: elt);
12749 }
12750 return builder.build ();
12751 }
12752
12753 /* Otherwise, as all tcc_constants are possibly shared, copy the node
12754 and drop the flag. */
12755 t = copy_node (node: t);
12756 TREE_OVERFLOW (t) = 0;
12757
12758 /* For constants that contain nested constants, drop the flag
12759 from those as well. */
12760 if (TREE_CODE (t) == COMPLEX_CST)
12761 {
12762 if (TREE_OVERFLOW (TREE_REALPART (t)))
12763 TREE_REALPART (t) = drop_tree_overflow (TREE_REALPART (t));
12764 if (TREE_OVERFLOW (TREE_IMAGPART (t)))
12765 TREE_IMAGPART (t) = drop_tree_overflow (TREE_IMAGPART (t));
12766 }
12767
12768 return t;
12769}
12770
12771/* Given a memory reference expression T, return its base address.
12772 The base address of a memory reference expression is the main
12773 object being referenced. For instance, the base address for
12774 'array[i].fld[j]' is 'array'. You can think of this as stripping
12775 away the offset part from a memory address.
12776
12777 This function calls handled_component_p to strip away all the inner
12778 parts of the memory reference until it reaches the base object. */
12779
12780tree
12781get_base_address (tree t)
12782{
12783 if (TREE_CODE (t) == WITH_SIZE_EXPR)
12784 t = TREE_OPERAND (t, 0);
12785 while (handled_component_p (t))
12786 t = TREE_OPERAND (t, 0);
12787
12788 if ((TREE_CODE (t) == MEM_REF
12789 || TREE_CODE (t) == TARGET_MEM_REF)
12790 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
12791 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
12792
12793 return t;
12794}
12795
12796/* Return a tree of sizetype representing the size, in bytes, of the element
12797 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12798
12799tree
12800array_ref_element_size (tree exp)
12801{
12802 tree aligned_size = TREE_OPERAND (exp, 3);
12803 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
12804 location_t loc = EXPR_LOCATION (exp);
12805
12806 /* If a size was specified in the ARRAY_REF, it's the size measured
12807 in alignment units of the element type. So multiply by that value. */
12808 if (aligned_size)
12809 {
12810 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
12811 sizetype from another type of the same width and signedness. */
12812 if (TREE_TYPE (aligned_size) != sizetype)
12813 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
12814 return size_binop_loc (loc, MULT_EXPR, aligned_size,
12815 size_int (TYPE_ALIGN_UNIT (elmt_type)));
12816 }
12817
12818 /* Otherwise, take the size from that of the element type. Substitute
12819 any PLACEHOLDER_EXPR that we have. */
12820 else
12821 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
12822}
12823
12824/* Return a tree representing the lower bound of the array mentioned in
12825 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12826
12827tree
12828array_ref_low_bound (tree exp)
12829{
12830 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12831
12832 /* If a lower bound is specified in EXP, use it. */
12833 if (TREE_OPERAND (exp, 2))
12834 return TREE_OPERAND (exp, 2);
12835
12836 /* Otherwise, if there is a domain type and it has a lower bound, use it,
12837 substituting for a PLACEHOLDER_EXPR as needed. */
12838 if (domain_type && TYPE_MIN_VALUE (domain_type))
12839 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
12840
12841 /* Otherwise, return a zero of the appropriate type. */
12842 tree idxtype = TREE_TYPE (TREE_OPERAND (exp, 1));
12843 return (idxtype == error_mark_node
12844 ? integer_zero_node : build_int_cst (type: idxtype, cst: 0));
12845}
12846
12847/* Return a tree representing the upper bound of the array mentioned in
12848 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12849
12850tree
12851array_ref_up_bound (tree exp)
12852{
12853 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12854
12855 /* If there is a domain type and it has an upper bound, use it, substituting
12856 for a PLACEHOLDER_EXPR as needed. */
12857 if (domain_type && TYPE_MAX_VALUE (domain_type))
12858 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
12859
12860 /* Otherwise fail. */
12861 return NULL_TREE;
12862}
12863
12864/* Returns true if REF is an array reference, a component reference,
12865 or a memory reference to an array whose actual size might be larger
12866 than its upper bound implies, there are multiple cases:
12867 A. a ref to a flexible array member at the end of a structure;
12868 B. a ref to an array with a different type against the original decl;
12869 for example:
12870
12871 short a[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
12872 (*((char(*)[16])&a[0]))[i+8]
12873
12874 C. a ref to an array that was passed as a parameter;
12875 for example:
12876
12877 int test (uint8_t *p, uint32_t t[1][1], int n) {
12878 for (int i = 0; i < 4; i++, p++)
12879 t[i][0] = ...;
12880
12881 If non-null, set IS_TRAILING_ARRAY to true if the ref is the above case A.
12882*/
12883
12884bool
12885array_ref_flexible_size_p (tree ref, bool *is_trailing_array /* = NULL */)
12886{
12887 /* The TYPE for this array referece. */
12888 tree atype = NULL_TREE;
12889 /* The FIELD_DECL for the array field in the containing structure. */
12890 tree afield_decl = NULL_TREE;
12891 /* Whether this array is the trailing array of a structure. */
12892 bool is_trailing_array_tmp = false;
12893 if (!is_trailing_array)
12894 is_trailing_array = &is_trailing_array_tmp;
12895
12896 if (TREE_CODE (ref) == ARRAY_REF
12897 || TREE_CODE (ref) == ARRAY_RANGE_REF)
12898 {
12899 atype = TREE_TYPE (TREE_OPERAND (ref, 0));
12900 ref = TREE_OPERAND (ref, 0);
12901 }
12902 else if (TREE_CODE (ref) == COMPONENT_REF
12903 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 1))) == ARRAY_TYPE)
12904 {
12905 atype = TREE_TYPE (TREE_OPERAND (ref, 1));
12906 afield_decl = TREE_OPERAND (ref, 1);
12907 }
12908 else if (TREE_CODE (ref) == MEM_REF)
12909 {
12910 tree arg = TREE_OPERAND (ref, 0);
12911 if (TREE_CODE (arg) == ADDR_EXPR)
12912 arg = TREE_OPERAND (arg, 0);
12913 tree argtype = TREE_TYPE (arg);
12914 if (TREE_CODE (argtype) == RECORD_TYPE)
12915 {
12916 if (tree fld = last_field (type: argtype))
12917 {
12918 atype = TREE_TYPE (fld);
12919 afield_decl = fld;
12920 if (TREE_CODE (atype) != ARRAY_TYPE)
12921 return false;
12922 if (VAR_P (arg) && DECL_SIZE (fld))
12923 return false;
12924 }
12925 else
12926 return false;
12927 }
12928 else
12929 return false;
12930 }
12931 else
12932 return false;
12933
12934 if (TREE_CODE (ref) == STRING_CST)
12935 return false;
12936
12937 tree ref_to_array = ref;
12938 while (handled_component_p (t: ref))
12939 {
12940 /* If the reference chain contains a component reference to a
12941 non-union type and there follows another field the reference
12942 is not at the end of a structure. */
12943 if (TREE_CODE (ref) == COMPONENT_REF)
12944 {
12945 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
12946 {
12947 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
12948 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
12949 nextf = DECL_CHAIN (nextf);
12950 if (nextf)
12951 return false;
12952 }
12953 }
12954 /* If we have a multi-dimensional array we do not consider
12955 a non-innermost dimension as flex array if the whole
12956 multi-dimensional array is at struct end.
12957 Same for an array of aggregates with a trailing array
12958 member. */
12959 else if (TREE_CODE (ref) == ARRAY_REF)
12960 return false;
12961 else if (TREE_CODE (ref) == ARRAY_RANGE_REF)
12962 ;
12963 /* If we view an underlying object as sth else then what we
12964 gathered up to now is what we have to rely on. */
12965 else if (TREE_CODE (ref) == VIEW_CONVERT_EXPR)
12966 break;
12967 else
12968 gcc_unreachable ();
12969
12970 ref = TREE_OPERAND (ref, 0);
12971 }
12972
12973 gcc_assert (!afield_decl
12974 || (afield_decl && TREE_CODE (afield_decl) == FIELD_DECL));
12975
12976 /* The array now is at struct end. Treat flexible array member as
12977 always subject to extend, even into just padding constrained by
12978 an underlying decl. */
12979 if (! TYPE_SIZE (atype)
12980 || ! TYPE_DOMAIN (atype)
12981 || ! TYPE_MAX_VALUE (TYPE_DOMAIN (atype)))
12982 {
12983 *is_trailing_array = afield_decl && TREE_CODE (afield_decl) == FIELD_DECL;
12984 return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
12985 }
12986
12987 /* If the reference is based on a declared entity, the size of the array
12988 is constrained by its given domain. (Do not trust commons PR/69368). */
12989 ref = get_base_address (t: ref);
12990 if (ref
12991 && DECL_P (ref)
12992 && !(flag_unconstrained_commons
12993 && VAR_P (ref) && DECL_COMMON (ref))
12994 && DECL_SIZE_UNIT (ref)
12995 && TREE_CODE (DECL_SIZE_UNIT (ref)) == INTEGER_CST)
12996 {
12997 /* If the object itself is the array it is not at struct end. */
12998 if (DECL_P (ref_to_array))
12999 return false;
13000
13001 /* Check whether the array domain covers all of the available
13002 padding. */
13003 poly_int64 offset;
13004 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (atype))) != INTEGER_CST
13005 || TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))) != INTEGER_CST
13006 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (atype))) != INTEGER_CST)
13007 {
13008 *is_trailing_array
13009 = afield_decl && TREE_CODE (afield_decl) == FIELD_DECL;
13010 return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
13011 }
13012 if (! get_addr_base_and_unit_offset (ref_to_array, &offset))
13013 {
13014 *is_trailing_array
13015 = afield_decl && TREE_CODE (afield_decl) == FIELD_DECL;
13016 return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
13017 }
13018
13019 /* If at least one extra element fits it is a flexarray. */
13020 if (known_le ((wi::to_offset (TYPE_MAX_VALUE (TYPE_DOMAIN (atype)))
13021 - wi::to_offset (TYPE_MIN_VALUE (TYPE_DOMAIN (atype)))
13022 + 2)
13023 * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (atype))),
13024 wi::to_offset (DECL_SIZE_UNIT (ref)) - offset))
13025 {
13026 *is_trailing_array
13027 = afield_decl && TREE_CODE (afield_decl) == FIELD_DECL;
13028 return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
13029 }
13030
13031 return false;
13032 }
13033
13034 *is_trailing_array = afield_decl && TREE_CODE (afield_decl) == FIELD_DECL;
13035 return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
13036}
13037
13038
13039/* Return a tree representing the offset, in bytes, of the field referenced
13040 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
13041
13042tree
13043component_ref_field_offset (tree exp)
13044{
13045 tree aligned_offset = TREE_OPERAND (exp, 2);
13046 tree field = TREE_OPERAND (exp, 1);
13047 location_t loc = EXPR_LOCATION (exp);
13048
13049 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
13050 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
13051 value. */
13052 if (aligned_offset)
13053 {
13054 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
13055 sizetype from another type of the same width and signedness. */
13056 if (TREE_TYPE (aligned_offset) != sizetype)
13057 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
13058 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
13059 size_int (DECL_OFFSET_ALIGN (field)
13060 / BITS_PER_UNIT));
13061 }
13062
13063 /* Otherwise, take the offset from that of the field. Substitute
13064 any PLACEHOLDER_EXPR that we have. */
13065 else
13066 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
13067}
13068
13069/* Given the initializer INIT, return the initializer for the field
13070 DECL if it exists, otherwise null. Used to obtain the initializer
13071 for a flexible array member and determine its size. */
13072
13073static tree
13074get_initializer_for (tree init, tree decl)
13075{
13076 STRIP_NOPS (init);
13077
13078 tree fld, fld_init;
13079 unsigned HOST_WIDE_INT i;
13080 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), i, fld, fld_init)
13081 {
13082 if (decl == fld)
13083 return fld_init;
13084
13085 if (TREE_CODE (fld) == CONSTRUCTOR)
13086 {
13087 fld_init = get_initializer_for (init: fld_init, decl);
13088 if (fld_init)
13089 return fld_init;
13090 }
13091 }
13092
13093 return NULL_TREE;
13094}
13095
13096/* Determines the special array member type for the array reference REF. */
13097special_array_member
13098component_ref_sam_type (tree ref)
13099{
13100 special_array_member sam_type = special_array_member::none;
13101
13102 tree member = TREE_OPERAND (ref, 1);
13103 tree memsize = DECL_SIZE_UNIT (member);
13104 if (memsize)
13105 {
13106 tree memtype = TREE_TYPE (member);
13107 if (TREE_CODE (memtype) != ARRAY_TYPE)
13108 return sam_type;
13109
13110 bool trailing = false;
13111 (void) array_ref_flexible_size_p (ref, is_trailing_array: &trailing);
13112 bool zero_elts = integer_zerop (expr: memsize);
13113 if (zero_elts && integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (memtype))))
13114 {
13115 /* If array element has zero size, verify if it is a flexible
13116 array member or zero length array. Clear zero_elts if
13117 it has one or more members or is a VLA member. */
13118 if (tree dom = TYPE_DOMAIN (memtype))
13119 if (tree min = TYPE_MIN_VALUE (dom))
13120 if (tree max = TYPE_MAX_VALUE (dom))
13121 if (TREE_CODE (min) != INTEGER_CST
13122 || TREE_CODE (max) != INTEGER_CST
13123 || !((integer_zerop (expr: min) && integer_all_onesp (expr: max))
13124 || tree_int_cst_lt (t1: max, t2: min)))
13125 zero_elts = false;
13126 }
13127 if (!trailing && !zero_elts)
13128 /* MEMBER is an interior array with more than one element. */
13129 return special_array_member::int_n;
13130
13131 if (zero_elts)
13132 {
13133 if (trailing)
13134 return special_array_member::trail_0;
13135 else
13136 return special_array_member::int_0;
13137 }
13138
13139 if (!zero_elts)
13140 if (tree dom = TYPE_DOMAIN (memtype))
13141 if (tree min = TYPE_MIN_VALUE (dom))
13142 if (tree max = TYPE_MAX_VALUE (dom))
13143 if (TREE_CODE (min) == INTEGER_CST
13144 && TREE_CODE (max) == INTEGER_CST)
13145 {
13146 offset_int minidx = wi::to_offset (t: min);
13147 offset_int maxidx = wi::to_offset (t: max);
13148 offset_int neltsm1 = maxidx - minidx;
13149 if (neltsm1 > 0)
13150 /* MEMBER is a trailing array with more than
13151 one elements. */
13152 return special_array_member::trail_n;
13153
13154 if (neltsm1 == 0)
13155 return special_array_member::trail_1;
13156 }
13157 }
13158
13159 return sam_type;
13160}
13161
13162/* Determines the size of the member referenced by the COMPONENT_REF
13163 REF, using its initializer expression if necessary in order to
13164 determine the size of an initialized flexible array member.
13165 If non-null, set *SAM to the type of special array member.
13166 Returns the size as sizetype (which might be zero for an object
13167 with an uninitialized flexible array member) or null if the size
13168 cannot be determined. */
13169
13170tree
13171component_ref_size (tree ref, special_array_member *sam /* = NULL */)
13172{
13173 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
13174
13175 special_array_member sambuf;
13176 if (!sam)
13177 sam = &sambuf;
13178 *sam = component_ref_sam_type (ref);
13179
13180 /* The object/argument referenced by the COMPONENT_REF and its type. */
13181 tree arg = TREE_OPERAND (ref, 0);
13182 tree argtype = TREE_TYPE (arg);
13183 /* The referenced member. */
13184 tree member = TREE_OPERAND (ref, 1);
13185
13186 tree memsize = DECL_SIZE_UNIT (member);
13187 if (memsize)
13188 {
13189 tree memtype = TREE_TYPE (member);
13190 if (TREE_CODE (memtype) != ARRAY_TYPE)
13191 /* DECL_SIZE may be less than TYPE_SIZE in C++ when referring
13192 to the type of a class with a virtual base which doesn't
13193 reflect the size of the virtual's members (see pr97595).
13194 If that's the case fail for now and implement something
13195 more robust in the future. */
13196 return (tree_int_cst_equal (t1: memsize, TYPE_SIZE_UNIT (memtype))
13197 ? memsize : NULL_TREE);
13198
13199 /* 2-or-more elements arrays are treated as normal arrays by default. */
13200 if (*sam == special_array_member::int_n
13201 || *sam == special_array_member::trail_n)
13202 return memsize;
13203
13204 tree afield_decl = TREE_OPERAND (ref, 1);
13205 gcc_assert (TREE_CODE (afield_decl) == FIELD_DECL);
13206 /* If the trailing array is a not a flexible array member, treat it as
13207 a normal array. */
13208 if (DECL_NOT_FLEXARRAY (afield_decl)
13209 && *sam != special_array_member::int_0)
13210 return memsize;
13211
13212 if (*sam == special_array_member::int_0)
13213 memsize = NULL_TREE;
13214
13215 /* For a reference to a flexible array member of a union
13216 use the size of the union instead of the size of the member. */
13217 if (TREE_CODE (argtype) == UNION_TYPE)
13218 memsize = TYPE_SIZE_UNIT (argtype);
13219 }
13220
13221 /* MEMBER is either a bona fide flexible array member, or a zero-elements
13222 array member, or an array of length one treated as such. */
13223
13224 /* If the reference is to a declared object and the member a true
13225 flexible array, try to determine its size from its initializer. */
13226 poly_int64 baseoff = 0;
13227 tree base = get_addr_base_and_unit_offset (ref, &baseoff);
13228 if (!base || !VAR_P (base))
13229 {
13230 if (*sam != special_array_member::int_0)
13231 return NULL_TREE;
13232
13233 if (TREE_CODE (arg) != COMPONENT_REF)
13234 return NULL_TREE;
13235
13236 base = arg;
13237 while (TREE_CODE (base) == COMPONENT_REF)
13238 base = TREE_OPERAND (base, 0);
13239 baseoff = tree_to_poly_int64 (t: byte_position (TREE_OPERAND (ref, 1)));
13240 }
13241
13242 /* BASE is the declared object of which MEMBER is either a member
13243 or that is cast to ARGTYPE (e.g., a char buffer used to store
13244 an ARGTYPE object). */
13245 tree basetype = TREE_TYPE (base);
13246
13247 /* Determine the base type of the referenced object. If it's
13248 the same as ARGTYPE and MEMBER has a known size, return it. */
13249 tree bt = basetype;
13250 if (*sam != special_array_member::int_0)
13251 while (TREE_CODE (bt) == ARRAY_TYPE)
13252 bt = TREE_TYPE (bt);
13253 bool typematch = useless_type_conversion_p (argtype, bt);
13254 if (memsize && typematch)
13255 return memsize;
13256
13257 memsize = NULL_TREE;
13258
13259 if (typematch)
13260 /* MEMBER is a true flexible array member. Compute its size from
13261 the initializer of the BASE object if it has one. */
13262 if (tree init = DECL_P (base) ? DECL_INITIAL (base) : NULL_TREE)
13263 if (init != error_mark_node)
13264 {
13265 init = get_initializer_for (init, decl: member);
13266 if (init)
13267 {
13268 memsize = TYPE_SIZE_UNIT (TREE_TYPE (init));
13269 if (tree refsize = TYPE_SIZE_UNIT (argtype))
13270 {
13271 /* Use the larger of the initializer size and the tail
13272 padding in the enclosing struct. */
13273 poly_int64 rsz = tree_to_poly_int64 (t: refsize);
13274 rsz -= baseoff;
13275 if (known_lt (tree_to_poly_int64 (memsize), rsz))
13276 memsize = wide_int_to_tree (TREE_TYPE (memsize), value: rsz);
13277 }
13278
13279 baseoff = 0;
13280 }
13281 }
13282
13283 if (!memsize)
13284 {
13285 if (typematch)
13286 {
13287 if (DECL_P (base)
13288 && DECL_EXTERNAL (base)
13289 && bt == basetype
13290 && *sam != special_array_member::int_0)
13291 /* The size of a flexible array member of an extern struct
13292 with no initializer cannot be determined (it's defined
13293 in another translation unit and can have an initializer
13294 with an arbitrary number of elements). */
13295 return NULL_TREE;
13296
13297 /* Use the size of the base struct or, for interior zero-length
13298 arrays, the size of the enclosing type. */
13299 memsize = TYPE_SIZE_UNIT (bt);
13300 }
13301 else if (DECL_P (base))
13302 /* Use the size of the BASE object (possibly an array of some
13303 other type such as char used to store the struct). */
13304 memsize = DECL_SIZE_UNIT (base);
13305 else
13306 return NULL_TREE;
13307 }
13308
13309 /* If the flexible array member has a known size use the greater
13310 of it and the tail padding in the enclosing struct.
13311 Otherwise, when the size of the flexible array member is unknown
13312 and the referenced object is not a struct, use the size of its
13313 type when known. This detects sizes of array buffers when cast
13314 to struct types with flexible array members. */
13315 if (memsize)
13316 {
13317 if (!tree_fits_poly_int64_p (t: memsize))
13318 return NULL_TREE;
13319 poly_int64 memsz64 = memsize ? tree_to_poly_int64 (t: memsize) : 0;
13320 if (known_lt (baseoff, memsz64))
13321 {
13322 memsz64 -= baseoff;
13323 return wide_int_to_tree (TREE_TYPE (memsize), value: memsz64);
13324 }
13325 return size_zero_node;
13326 }
13327
13328 /* Return "don't know" for an external non-array object since its
13329 flexible array member can be initialized to have any number of
13330 elements. Otherwise, return zero because the flexible array
13331 member has no elements. */
13332 return (DECL_P (base)
13333 && DECL_EXTERNAL (base)
13334 && (!typematch
13335 || TREE_CODE (basetype) != ARRAY_TYPE)
13336 ? NULL_TREE : size_zero_node);
13337}
13338
13339/* Return the machine mode of T. For vectors, returns the mode of the
13340 inner type. The main use case is to feed the result to HONOR_NANS,
13341 avoiding the BLKmode that a direct TYPE_MODE (T) might return. */
13342
13343machine_mode
13344element_mode (const_tree t)
13345{
13346 if (!TYPE_P (t))
13347 t = TREE_TYPE (t);
13348 if (VECTOR_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
13349 t = TREE_TYPE (t);
13350 return TYPE_MODE (t);
13351}
13352
13353/* Vector types need to re-check the target flags each time we report
13354 the machine mode. We need to do this because attribute target can
13355 change the result of vector_mode_supported_p and have_regs_of_mode
13356 on a per-function basis. Thus the TYPE_MODE of a VECTOR_TYPE can
13357 change on a per-function basis. */
13358/* ??? Possibly a better solution is to run through all the types
13359 referenced by a function and re-compute the TYPE_MODE once, rather
13360 than make the TYPE_MODE macro call a function. */
13361
13362machine_mode
13363vector_type_mode (const_tree t)
13364{
13365 machine_mode mode;
13366
13367 gcc_assert (TREE_CODE (t) == VECTOR_TYPE);
13368
13369 mode = t->type_common.mode;
13370 if (VECTOR_MODE_P (mode)
13371 && (!targetm.vector_mode_supported_p (mode)
13372 || !have_regs_of_mode[mode]))
13373 {
13374 scalar_int_mode innermode;
13375
13376 /* For integers, try mapping it to a same-sized scalar mode. */
13377 if (is_int_mode (TREE_TYPE (t)->type_common.mode, int_mode: &innermode))
13378 {
13379 poly_int64 size = (TYPE_VECTOR_SUBPARTS (node: t)
13380 * GET_MODE_BITSIZE (mode: innermode));
13381 scalar_int_mode mode;
13382 if (int_mode_for_size (size, limit: 0).exists (mode: &mode)
13383 && have_regs_of_mode[mode])
13384 return mode;
13385 }
13386
13387 return BLKmode;
13388 }
13389
13390 return mode;
13391}
13392
13393/* Return the size in bits of each element of vector type TYPE. */
13394
13395unsigned int
13396vector_element_bits (const_tree type)
13397{
13398 gcc_checking_assert (VECTOR_TYPE_P (type));
13399 if (VECTOR_BOOLEAN_TYPE_P (type))
13400 return TYPE_PRECISION (TREE_TYPE (type));
13401 return tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)));
13402}
13403
13404/* Calculate the size in bits of each element of vector type TYPE
13405 and return the result as a tree of type bitsizetype. */
13406
13407tree
13408vector_element_bits_tree (const_tree type)
13409{
13410 gcc_checking_assert (VECTOR_TYPE_P (type));
13411 if (VECTOR_BOOLEAN_TYPE_P (type))
13412 return bitsize_int (vector_element_bits (type));
13413 return TYPE_SIZE (TREE_TYPE (type));
13414}
13415
13416/* Verify that basic properties of T match TV and thus T can be a variant of
13417 TV. TV should be the more specified variant (i.e. the main variant). */
13418
13419static bool
13420verify_type_variant (const_tree t, tree tv)
13421{
13422 /* Type variant can differ by:
13423
13424 - TYPE_QUALS: TYPE_READONLY, TYPE_VOLATILE, TYPE_ATOMIC, TYPE_RESTRICT,
13425 ENCODE_QUAL_ADDR_SPACE.
13426 - main variant may be TYPE_COMPLETE_P and variant types !TYPE_COMPLETE_P
13427 in this case some values may not be set in the variant types
13428 (see TYPE_COMPLETE_P checks).
13429 - it is possible to have TYPE_ARTIFICIAL variant of non-artifical type
13430 - by TYPE_NAME and attributes (i.e. when variant originate by typedef)
13431 - TYPE_CANONICAL (TYPE_ALIAS_SET is the same among variants)
13432 - by the alignment: TYPE_ALIGN and TYPE_USER_ALIGN
13433 - during LTO by TYPE_CONTEXT if type is TYPE_FILE_SCOPE_P
13434 this is necessary to make it possible to merge types form different TUs
13435 - arrays, pointers and references may have TREE_TYPE that is a variant
13436 of TREE_TYPE of their main variants.
13437 - aggregates may have new TYPE_FIELDS list that list variants of
13438 the main variant TYPE_FIELDS.
13439 - vector types may differ by TYPE_VECTOR_OPAQUE
13440 */
13441
13442 /* Convenience macro for matching individual fields. */
13443#define verify_variant_match(flag) \
13444 do { \
13445 if (flag (tv) != flag (t)) \
13446 { \
13447 error ("type variant differs by %s", #flag); \
13448 debug_tree (tv); \
13449 return false; \
13450 } \
13451 } while (false)
13452
13453 /* tree_base checks. */
13454
13455 verify_variant_match (TREE_CODE);
13456 /* FIXME: Ada builds non-artificial variants of artificial types. */
13457#if 0
13458 if (TYPE_ARTIFICIAL (tv))
13459 verify_variant_match (TYPE_ARTIFICIAL);
13460#endif
13461 if (POINTER_TYPE_P (tv))
13462 verify_variant_match (TYPE_REF_CAN_ALIAS_ALL);
13463 /* FIXME: TYPE_SIZES_GIMPLIFIED may differs for Ada build. */
13464 verify_variant_match (TYPE_UNSIGNED);
13465 verify_variant_match (TYPE_PACKED);
13466 if (TREE_CODE (t) == REFERENCE_TYPE)
13467 verify_variant_match (TYPE_REF_IS_RVALUE);
13468 if (AGGREGATE_TYPE_P (t))
13469 verify_variant_match (TYPE_REVERSE_STORAGE_ORDER);
13470 else
13471 verify_variant_match (TYPE_SATURATING);
13472 /* FIXME: This check trigger during libstdc++ build. */
13473#if 0
13474 if (RECORD_OR_UNION_TYPE_P (t) && COMPLETE_TYPE_P (t))
13475 verify_variant_match (TYPE_FINAL_P);
13476#endif
13477
13478 /* tree_type_common checks. */
13479
13480 if (COMPLETE_TYPE_P (t))
13481 {
13482 verify_variant_match (TYPE_MODE);
13483 if (TREE_CODE (TYPE_SIZE (t)) != PLACEHOLDER_EXPR
13484 && TREE_CODE (TYPE_SIZE (tv)) != PLACEHOLDER_EXPR)
13485 verify_variant_match (TYPE_SIZE);
13486 if (TREE_CODE (TYPE_SIZE_UNIT (t)) != PLACEHOLDER_EXPR
13487 && TREE_CODE (TYPE_SIZE_UNIT (tv)) != PLACEHOLDER_EXPR
13488 && TYPE_SIZE_UNIT (t) != TYPE_SIZE_UNIT (tv))
13489 {
13490 gcc_assert (!operand_equal_p (TYPE_SIZE_UNIT (t),
13491 TYPE_SIZE_UNIT (tv), 0));
13492 error ("type variant has different %<TYPE_SIZE_UNIT%>");
13493 debug_tree (tv);
13494 error ("type variant%'s %<TYPE_SIZE_UNIT%>");
13495 debug_tree (TYPE_SIZE_UNIT (tv));
13496 error ("type%'s %<TYPE_SIZE_UNIT%>");
13497 debug_tree (TYPE_SIZE_UNIT (t));
13498 return false;
13499 }
13500 verify_variant_match (TYPE_NEEDS_CONSTRUCTING);
13501 }
13502 verify_variant_match (TYPE_PRECISION_RAW);
13503 if (RECORD_OR_UNION_TYPE_P (t))
13504 verify_variant_match (TYPE_TRANSPARENT_AGGR);
13505 else if (TREE_CODE (t) == ARRAY_TYPE)
13506 verify_variant_match (TYPE_NONALIASED_COMPONENT);
13507 /* During LTO we merge variant lists from diferent translation units
13508 that may differ BY TYPE_CONTEXT that in turn may point
13509 to TRANSLATION_UNIT_DECL.
13510 Ada also builds variants of types with different TYPE_CONTEXT. */
13511#if 0
13512 if (!in_lto_p || !TYPE_FILE_SCOPE_P (t))
13513 verify_variant_match (TYPE_CONTEXT);
13514#endif
13515 if (TREE_CODE (t) == ARRAY_TYPE || TREE_CODE (t) == INTEGER_TYPE)
13516 verify_variant_match (TYPE_STRING_FLAG);
13517 if (TREE_CODE (t) == RECORD_TYPE || TREE_CODE (t) == UNION_TYPE)
13518 verify_variant_match (TYPE_CXX_ODR_P);
13519 if (TYPE_ALIAS_SET_KNOWN_P (t))
13520 {
13521 error ("type variant with %<TYPE_ALIAS_SET_KNOWN_P%>");
13522 debug_tree (tv);
13523 return false;
13524 }
13525
13526 /* tree_type_non_common checks. */
13527
13528 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
13529 and dangle the pointer from time to time. */
13530 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_VFIELD (t) != TYPE_VFIELD (tv)
13531 && (in_lto_p || !TYPE_VFIELD (tv)
13532 || TREE_CODE (TYPE_VFIELD (tv)) != TREE_LIST))
13533 {
13534 error ("type variant has different %<TYPE_VFIELD%>");
13535 debug_tree (tv);
13536 return false;
13537 }
13538 if ((TREE_CODE (t) == ENUMERAL_TYPE && COMPLETE_TYPE_P (t))
13539 || TREE_CODE (t) == INTEGER_TYPE
13540 || TREE_CODE (t) == BOOLEAN_TYPE
13541 || TREE_CODE (t) == BITINT_TYPE
13542 || SCALAR_FLOAT_TYPE_P (t)
13543 || FIXED_POINT_TYPE_P (t))
13544 {
13545 verify_variant_match (TYPE_MAX_VALUE);
13546 verify_variant_match (TYPE_MIN_VALUE);
13547 }
13548 if (TREE_CODE (t) == METHOD_TYPE)
13549 verify_variant_match (TYPE_METHOD_BASETYPE);
13550 if (TREE_CODE (t) == OFFSET_TYPE)
13551 verify_variant_match (TYPE_OFFSET_BASETYPE);
13552 if (TREE_CODE (t) == ARRAY_TYPE)
13553 verify_variant_match (TYPE_ARRAY_MAX_SIZE);
13554 /* FIXME: Be lax and allow TYPE_BINFO to be missing in variant types
13555 or even type's main variant. This is needed to make bootstrap pass
13556 and the bug seems new in GCC 5.
13557 C++ FE should be updated to make this consistent and we should check
13558 that TYPE_BINFO is always NULL for !COMPLETE_TYPE_P and otherwise there
13559 is a match with main variant.
13560
13561 Also disable the check for Java for now because of parser hack that builds
13562 first an dummy BINFO and then sometimes replace it by real BINFO in some
13563 of the copies. */
13564 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t) && TYPE_BINFO (tv)
13565 && TYPE_BINFO (t) != TYPE_BINFO (tv)
13566 /* FIXME: Java sometimes keep dump TYPE_BINFOs on variant types.
13567 Since there is no cheap way to tell C++/Java type w/o LTO, do checking
13568 at LTO time only. */
13569 && (in_lto_p && odr_type_p (t)))
13570 {
13571 error ("type variant has different %<TYPE_BINFO%>");
13572 debug_tree (tv);
13573 error ("type variant%'s %<TYPE_BINFO%>");
13574 debug_tree (TYPE_BINFO (tv));
13575 error ("type%'s %<TYPE_BINFO%>");
13576 debug_tree (TYPE_BINFO (t));
13577 return false;
13578 }
13579
13580 /* Check various uses of TYPE_VALUES_RAW. */
13581 if (TREE_CODE (t) == ENUMERAL_TYPE
13582 && TYPE_VALUES (t))
13583 verify_variant_match (TYPE_VALUES);
13584 else if (TREE_CODE (t) == ARRAY_TYPE)
13585 verify_variant_match (TYPE_DOMAIN);
13586 /* Permit incomplete variants of complete type. While FEs may complete
13587 all variants, this does not happen for C++ templates in all cases. */
13588 else if (RECORD_OR_UNION_TYPE_P (t)
13589 && COMPLETE_TYPE_P (t)
13590 && TYPE_FIELDS (t) != TYPE_FIELDS (tv))
13591 {
13592 tree f1, f2;
13593
13594 /* Fortran builds qualified variants as new records with items of
13595 qualified type. Verify that they looks same. */
13596 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (tv);
13597 f1 && f2;
13598 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
13599 if (TREE_CODE (f1) != FIELD_DECL || TREE_CODE (f2) != FIELD_DECL
13600 || (TYPE_MAIN_VARIANT (TREE_TYPE (f1))
13601 != TYPE_MAIN_VARIANT (TREE_TYPE (f2))
13602 /* FIXME: gfc_nonrestricted_type builds all types as variants
13603 with exception of pointer types. It deeply copies the type
13604 which means that we may end up with a variant type
13605 referring non-variant pointer. We may change it to
13606 produce types as variants, too, like
13607 objc_get_protocol_qualified_type does. */
13608 && !POINTER_TYPE_P (TREE_TYPE (f1)))
13609 || DECL_FIELD_OFFSET (f1) != DECL_FIELD_OFFSET (f2)
13610 || DECL_FIELD_BIT_OFFSET (f1) != DECL_FIELD_BIT_OFFSET (f2))
13611 break;
13612 if (f1 || f2)
13613 {
13614 error ("type variant has different %<TYPE_FIELDS%>");
13615 debug_tree (tv);
13616 error ("first mismatch is field");
13617 debug_tree (f1);
13618 error ("and field");
13619 debug_tree (f2);
13620 return false;
13621 }
13622 }
13623 else if (FUNC_OR_METHOD_TYPE_P (t))
13624 verify_variant_match (TYPE_ARG_TYPES);
13625 /* For C++ the qualified variant of array type is really an array type
13626 of qualified TREE_TYPE.
13627 objc builds variants of pointer where pointer to type is a variant, too
13628 in objc_get_protocol_qualified_type. */
13629 if (TREE_TYPE (t) != TREE_TYPE (tv)
13630 && ((TREE_CODE (t) != ARRAY_TYPE
13631 && !POINTER_TYPE_P (t))
13632 || TYPE_MAIN_VARIANT (TREE_TYPE (t))
13633 != TYPE_MAIN_VARIANT (TREE_TYPE (tv))))
13634 {
13635 error ("type variant has different %<TREE_TYPE%>");
13636 debug_tree (tv);
13637 error ("type variant%'s %<TREE_TYPE%>");
13638 debug_tree (TREE_TYPE (tv));
13639 error ("type%'s %<TREE_TYPE%>");
13640 debug_tree (TREE_TYPE (t));
13641 return false;
13642 }
13643 if (type_with_alias_set_p (t)
13644 && !gimple_canonical_types_compatible_p (t, tv, trust_type_canonical: false))
13645 {
13646 error ("type is not compatible with its variant");
13647 debug_tree (tv);
13648 error ("type variant%'s %<TREE_TYPE%>");
13649 debug_tree (TREE_TYPE (tv));
13650 error ("type%'s %<TREE_TYPE%>");
13651 debug_tree (TREE_TYPE (t));
13652 return false;
13653 }
13654 return true;
13655#undef verify_variant_match
13656}
13657
13658
13659/* The TYPE_CANONICAL merging machinery. It should closely resemble
13660 the middle-end types_compatible_p function. It needs to avoid
13661 claiming types are different for types that should be treated
13662 the same with respect to TBAA. Canonical types are also used
13663 for IL consistency checks via the useless_type_conversion_p
13664 predicate which does not handle all type kinds itself but falls
13665 back to pointer-comparison of TYPE_CANONICAL for aggregates
13666 for example. */
13667
13668/* Return true if TYPE_UNSIGNED of TYPE should be ignored for canonical
13669 type calculation because we need to allow inter-operability between signed
13670 and unsigned variants. */
13671
13672bool
13673type_with_interoperable_signedness (const_tree type)
13674{
13675 /* Fortran standard require C_SIGNED_CHAR to be interoperable with both
13676 signed char and unsigned char. Similarly fortran FE builds
13677 C_SIZE_T as signed type, while C defines it unsigned. */
13678
13679 return tree_code_for_canonical_type_merging (TREE_CODE (type))
13680 == INTEGER_TYPE
13681 && (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node)
13682 || TYPE_PRECISION (type) == TYPE_PRECISION (size_type_node));
13683}
13684
13685/* Return true iff T1 and T2 are structurally identical for what
13686 TBAA is concerned.
13687 This function is used both by lto.cc canonical type merging and by the
13688 verifier. If TRUST_TYPE_CANONICAL we do not look into structure of types
13689 that have TYPE_CANONICAL defined and assume them equivalent. This is useful
13690 only for LTO because only in these cases TYPE_CANONICAL equivalence
13691 correspond to one defined by gimple_canonical_types_compatible_p. */
13692
13693bool
13694gimple_canonical_types_compatible_p (const_tree t1, const_tree t2,
13695 bool trust_type_canonical)
13696{
13697 /* Type variants should be same as the main variant. When not doing sanity
13698 checking to verify this fact, go to main variants and save some work. */
13699 if (trust_type_canonical)
13700 {
13701 t1 = TYPE_MAIN_VARIANT (t1);
13702 t2 = TYPE_MAIN_VARIANT (t2);
13703 }
13704
13705 /* Check first for the obvious case of pointer identity. */
13706 if (t1 == t2)
13707 return true;
13708
13709 /* Check that we have two types to compare. */
13710 if (t1 == NULL_TREE || t2 == NULL_TREE)
13711 return false;
13712
13713 /* We consider complete types always compatible with incomplete type.
13714 This does not make sense for canonical type calculation and thus we
13715 need to ensure that we are never called on it.
13716
13717 FIXME: For more correctness the function probably should have three modes
13718 1) mode assuming that types are complete mathcing their structure
13719 2) mode allowing incomplete types but producing equivalence classes
13720 and thus ignoring all info from complete types
13721 3) mode allowing incomplete types to match complete but checking
13722 compatibility between complete types.
13723
13724 1 and 2 can be used for canonical type calculation. 3 is the real
13725 definition of type compatibility that can be used i.e. for warnings during
13726 declaration merging. */
13727
13728 gcc_assert (!trust_type_canonical
13729 || (type_with_alias_set_p (t1) && type_with_alias_set_p (t2)));
13730
13731 /* If the types have been previously registered and found equal
13732 they still are. */
13733
13734 if (TYPE_CANONICAL (t1) && TYPE_CANONICAL (t2)
13735 && trust_type_canonical)
13736 {
13737 /* Do not use TYPE_CANONICAL of pointer types. For LTO streamed types
13738 they are always NULL, but they are set to non-NULL for types
13739 constructed by build_pointer_type and variants. In this case the
13740 TYPE_CANONICAL is more fine grained than the equivalnce we test (where
13741 all pointers are considered equal. Be sure to not return false
13742 negatives. */
13743 gcc_checking_assert (canonical_type_used_p (t1)
13744 && canonical_type_used_p (t2));
13745 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
13746 }
13747
13748 /* For types where we do ODR based TBAA the canonical type is always
13749 set correctly, so we know that types are different if their
13750 canonical types does not match. */
13751 if (trust_type_canonical
13752 && (odr_type_p (t: t1) && odr_based_tbaa_p (type: t1))
13753 != (odr_type_p (t: t2) && odr_based_tbaa_p (type: t2)))
13754 return false;
13755
13756 /* Can't be the same type if the types don't have the same code. */
13757 enum tree_code code = tree_code_for_canonical_type_merging (TREE_CODE (t1));
13758 if (code != tree_code_for_canonical_type_merging (TREE_CODE (t2)))
13759 return false;
13760
13761 /* Qualifiers do not matter for canonical type comparison purposes. */
13762
13763 /* Void types and nullptr types are always the same. */
13764 if (VOID_TYPE_P (t1)
13765 || TREE_CODE (t1) == NULLPTR_TYPE)
13766 return true;
13767
13768 /* Can't be the same type if they have different mode. */
13769 if (TYPE_MODE (t1) != TYPE_MODE (t2))
13770 return false;
13771
13772 /* Non-aggregate types can be handled cheaply. */
13773 if (INTEGRAL_TYPE_P (t1)
13774 || SCALAR_FLOAT_TYPE_P (t1)
13775 || FIXED_POINT_TYPE_P (t1)
13776 || VECTOR_TYPE_P (t1)
13777 || TREE_CODE (t1) == COMPLEX_TYPE
13778 || TREE_CODE (t1) == OFFSET_TYPE
13779 || POINTER_TYPE_P (t1))
13780 {
13781 /* Can't be the same type if they have different precision. */
13782 if (TYPE_PRECISION_RAW (t1) != TYPE_PRECISION_RAW (t2))
13783 return false;
13784
13785 /* In some cases the signed and unsigned types are required to be
13786 inter-operable. */
13787 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2)
13788 && !type_with_interoperable_signedness (type: t1))
13789 return false;
13790
13791 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
13792 interoperable with "signed char". Unless all frontends are revisited
13793 to agree on these types, we must ignore the flag completely. */
13794
13795 /* Fortran standard define C_PTR type that is compatible with every
13796 C pointer. For this reason we need to glob all pointers into one.
13797 Still pointers in different address spaces are not compatible. */
13798 if (POINTER_TYPE_P (t1))
13799 {
13800 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
13801 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
13802 return false;
13803 }
13804
13805 /* Tail-recurse to components. */
13806 if (VECTOR_TYPE_P (t1)
13807 || TREE_CODE (t1) == COMPLEX_TYPE)
13808 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
13809 TREE_TYPE (t2),
13810 trust_type_canonical);
13811
13812 return true;
13813 }
13814
13815 /* Do type-specific comparisons. */
13816 switch (TREE_CODE (t1))
13817 {
13818 case ARRAY_TYPE:
13819 /* Array types are the same if the element types are the same and
13820 the number of elements are the same. */
13821 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13822 trust_type_canonical)
13823 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
13824 || TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2)
13825 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
13826 return false;
13827 else
13828 {
13829 tree i1 = TYPE_DOMAIN (t1);
13830 tree i2 = TYPE_DOMAIN (t2);
13831
13832 /* For an incomplete external array, the type domain can be
13833 NULL_TREE. Check this condition also. */
13834 if (i1 == NULL_TREE && i2 == NULL_TREE)
13835 return true;
13836 else if (i1 == NULL_TREE || i2 == NULL_TREE)
13837 return false;
13838 else
13839 {
13840 tree min1 = TYPE_MIN_VALUE (i1);
13841 tree min2 = TYPE_MIN_VALUE (i2);
13842 tree max1 = TYPE_MAX_VALUE (i1);
13843 tree max2 = TYPE_MAX_VALUE (i2);
13844
13845 /* The minimum/maximum values have to be the same. */
13846 if ((min1 == min2
13847 || (min1 && min2
13848 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
13849 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
13850 || operand_equal_p (min1, min2, flags: 0))))
13851 && (max1 == max2
13852 || (max1 && max2
13853 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
13854 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
13855 || operand_equal_p (max1, max2, flags: 0)))))
13856 return true;
13857 else
13858 return false;
13859 }
13860 }
13861
13862 case METHOD_TYPE:
13863 case FUNCTION_TYPE:
13864 /* Function types are the same if the return type and arguments types
13865 are the same. */
13866 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13867 trust_type_canonical))
13868 return false;
13869
13870 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2)
13871 && (TYPE_NO_NAMED_ARGS_STDARG_P (t1)
13872 == TYPE_NO_NAMED_ARGS_STDARG_P (t2)))
13873 return true;
13874 else
13875 {
13876 tree parms1, parms2;
13877
13878 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
13879 parms1 && parms2;
13880 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
13881 {
13882 if (!gimple_canonical_types_compatible_p
13883 (TREE_VALUE (parms1), TREE_VALUE (parms2),
13884 trust_type_canonical))
13885 return false;
13886 }
13887
13888 if (parms1 || parms2)
13889 return false;
13890
13891 return true;
13892 }
13893
13894 case RECORD_TYPE:
13895 case UNION_TYPE:
13896 case QUAL_UNION_TYPE:
13897 {
13898 tree f1, f2;
13899
13900 /* Don't try to compare variants of an incomplete type, before
13901 TYPE_FIELDS has been copied around. */
13902 if (!COMPLETE_TYPE_P (t1) && !COMPLETE_TYPE_P (t2))
13903 return true;
13904
13905
13906 if (TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2))
13907 return false;
13908
13909 /* For aggregate types, all the fields must be the same. */
13910 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
13911 f1 || f2;
13912 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
13913 {
13914 /* Skip non-fields and zero-sized fields. */
13915 while (f1 && (TREE_CODE (f1) != FIELD_DECL
13916 || (DECL_SIZE (f1)
13917 && integer_zerop (DECL_SIZE (f1)))))
13918 f1 = TREE_CHAIN (f1);
13919 while (f2 && (TREE_CODE (f2) != FIELD_DECL
13920 || (DECL_SIZE (f2)
13921 && integer_zerop (DECL_SIZE (f2)))))
13922 f2 = TREE_CHAIN (f2);
13923 if (!f1 || !f2)
13924 break;
13925 /* The fields must have the same name, offset and type. */
13926 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
13927 || !gimple_compare_field_offset (f1, f2)
13928 || !gimple_canonical_types_compatible_p
13929 (TREE_TYPE (f1), TREE_TYPE (f2),
13930 trust_type_canonical))
13931 return false;
13932 }
13933
13934 /* If one aggregate has more fields than the other, they
13935 are not the same. */
13936 if (f1 || f2)
13937 return false;
13938
13939 return true;
13940 }
13941
13942 default:
13943 /* Consider all types with language specific trees in them mutually
13944 compatible. This is executed only from verify_type and false
13945 positives can be tolerated. */
13946 gcc_assert (!in_lto_p);
13947 return true;
13948 }
13949}
13950
13951/* For OPAQUE_TYPE T, it should have only size and alignment information
13952 and its mode should be of class MODE_OPAQUE. This function verifies
13953 these properties of T match TV which is the main variant of T and TC
13954 which is the canonical of T. */
13955
13956static void
13957verify_opaque_type (const_tree t, tree tv, tree tc)
13958{
13959 gcc_assert (OPAQUE_TYPE_P (t));
13960 gcc_assert (tv && tv == TYPE_MAIN_VARIANT (tv));
13961 gcc_assert (tc && tc == TYPE_CANONICAL (tc));
13962
13963 /* For an opaque type T1, check if some of its properties match
13964 the corresponding ones of the other opaque type T2, emit some
13965 error messages for those inconsistent ones. */
13966 auto check_properties_for_opaque_type = [](const_tree t1, tree t2,
13967 const char *kind_msg)
13968 {
13969 if (!OPAQUE_TYPE_P (t2))
13970 {
13971 error ("type %s is not an opaque type", kind_msg);
13972 debug_tree (t2);
13973 return;
13974 }
13975 if (!OPAQUE_MODE_P (TYPE_MODE (t2)))
13976 {
13977 error ("type %s is not with opaque mode", kind_msg);
13978 debug_tree (t2);
13979 return;
13980 }
13981 if (TYPE_MODE (t1) != TYPE_MODE (t2))
13982 {
13983 error ("type %s differs by %<TYPE_MODE%>", kind_msg);
13984 debug_tree (t2);
13985 return;
13986 }
13987 poly_uint64 t1_size = tree_to_poly_uint64 (TYPE_SIZE (t1));
13988 poly_uint64 t2_size = tree_to_poly_uint64 (TYPE_SIZE (t2));
13989 if (maybe_ne (a: t1_size, b: t2_size))
13990 {
13991 error ("type %s differs by %<TYPE_SIZE%>", kind_msg);
13992 debug_tree (t2);
13993 return;
13994 }
13995 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
13996 {
13997 error ("type %s differs by %<TYPE_ALIGN%>", kind_msg);
13998 debug_tree (t2);
13999 return;
14000 }
14001 if (TYPE_USER_ALIGN (t1) != TYPE_USER_ALIGN (t2))
14002 {
14003 error ("type %s differs by %<TYPE_USER_ALIGN%>", kind_msg);
14004 debug_tree (t2);
14005 return;
14006 }
14007 };
14008
14009 if (t != tv)
14010 check_properties_for_opaque_type (t, tv, "variant");
14011
14012 if (t != tc)
14013 check_properties_for_opaque_type (t, tc, "canonical");
14014}
14015
14016/* Verify type T. */
14017
14018void
14019verify_type (const_tree t)
14020{
14021 bool error_found = false;
14022 tree mv = TYPE_MAIN_VARIANT (t);
14023 tree ct = TYPE_CANONICAL (t);
14024
14025 if (OPAQUE_TYPE_P (t))
14026 {
14027 verify_opaque_type (t, tv: mv, tc: ct);
14028 return;
14029 }
14030
14031 if (!mv)
14032 {
14033 error ("main variant is not defined");
14034 error_found = true;
14035 }
14036 else if (mv != TYPE_MAIN_VARIANT (mv))
14037 {
14038 error ("%<TYPE_MAIN_VARIANT%> has different %<TYPE_MAIN_VARIANT%>");
14039 debug_tree (mv);
14040 error_found = true;
14041 }
14042 else if (t != mv && !verify_type_variant (t, tv: mv))
14043 error_found = true;
14044
14045 if (!ct)
14046 ;
14047 else if (TYPE_CANONICAL (ct) != ct)
14048 {
14049 error ("%<TYPE_CANONICAL%> has different %<TYPE_CANONICAL%>");
14050 debug_tree (ct);
14051 error_found = true;
14052 }
14053 /* Method and function types cannot be used to address memory and thus
14054 TYPE_CANONICAL really matters only for determining useless conversions.
14055
14056 FIXME: C++ FE produce declarations of builtin functions that are not
14057 compatible with main variants. */
14058 else if (TREE_CODE (t) == FUNCTION_TYPE)
14059 ;
14060 else if (t != ct
14061 /* FIXME: gimple_canonical_types_compatible_p cannot compare types
14062 with variably sized arrays because their sizes possibly
14063 gimplified to different variables. */
14064 && !variably_modified_type_p (type: ct, NULL)
14065 && !gimple_canonical_types_compatible_p (t1: t, t2: ct, trust_type_canonical: false)
14066 && COMPLETE_TYPE_P (t))
14067 {
14068 error ("%<TYPE_CANONICAL%> is not compatible");
14069 debug_tree (ct);
14070 error_found = true;
14071 }
14072
14073 if (COMPLETE_TYPE_P (t) && TYPE_CANONICAL (t)
14074 && TYPE_MODE (t) != TYPE_MODE (TYPE_CANONICAL (t)))
14075 {
14076 error ("%<TYPE_MODE%> of %<TYPE_CANONICAL%> is not compatible");
14077 debug_tree (ct);
14078 error_found = true;
14079 }
14080 if (TYPE_MAIN_VARIANT (t) == t && ct && TYPE_MAIN_VARIANT (ct) != ct)
14081 {
14082 error ("%<TYPE_CANONICAL%> of main variant is not main variant");
14083 debug_tree (ct);
14084 debug_tree (TYPE_MAIN_VARIANT (ct));
14085 error_found = true;
14086 }
14087
14088
14089 /* Check various uses of TYPE_MIN_VALUE_RAW. */
14090 if (RECORD_OR_UNION_TYPE_P (t))
14091 {
14092 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
14093 and danagle the pointer from time to time. */
14094 if (TYPE_VFIELD (t)
14095 && TREE_CODE (TYPE_VFIELD (t)) != FIELD_DECL
14096 && TREE_CODE (TYPE_VFIELD (t)) != TREE_LIST)
14097 {
14098 error ("%<TYPE_VFIELD%> is not %<FIELD_DECL%> nor %<TREE_LIST%>");
14099 debug_tree (TYPE_VFIELD (t));
14100 error_found = true;
14101 }
14102 }
14103 else if (TREE_CODE (t) == POINTER_TYPE)
14104 {
14105 if (TYPE_NEXT_PTR_TO (t)
14106 && TREE_CODE (TYPE_NEXT_PTR_TO (t)) != POINTER_TYPE)
14107 {
14108 error ("%<TYPE_NEXT_PTR_TO%> is not %<POINTER_TYPE%>");
14109 debug_tree (TYPE_NEXT_PTR_TO (t));
14110 error_found = true;
14111 }
14112 }
14113 else if (TREE_CODE (t) == REFERENCE_TYPE)
14114 {
14115 if (TYPE_NEXT_REF_TO (t)
14116 && TREE_CODE (TYPE_NEXT_REF_TO (t)) != REFERENCE_TYPE)
14117 {
14118 error ("%<TYPE_NEXT_REF_TO%> is not %<REFERENCE_TYPE%>");
14119 debug_tree (TYPE_NEXT_REF_TO (t));
14120 error_found = true;
14121 }
14122 }
14123 else if (INTEGRAL_TYPE_P (t) || SCALAR_FLOAT_TYPE_P (t)
14124 || FIXED_POINT_TYPE_P (t))
14125 {
14126 /* FIXME: The following check should pass:
14127 useless_type_conversion_p (const_cast <tree> (t),
14128 TREE_TYPE (TYPE_MIN_VALUE (t))
14129 but does not for C sizetypes in LTO. */
14130 }
14131
14132 /* Check various uses of TYPE_MAXVAL_RAW. */
14133 if (RECORD_OR_UNION_TYPE_P (t))
14134 {
14135 if (!TYPE_BINFO (t))
14136 ;
14137 else if (TREE_CODE (TYPE_BINFO (t)) != TREE_BINFO)
14138 {
14139 error ("%<TYPE_BINFO%> is not %<TREE_BINFO%>");
14140 debug_tree (TYPE_BINFO (t));
14141 error_found = true;
14142 }
14143 else if (TREE_TYPE (TYPE_BINFO (t)) != TYPE_MAIN_VARIANT (t))
14144 {
14145 error ("%<TYPE_BINFO%> type is not %<TYPE_MAIN_VARIANT%>");
14146 debug_tree (TREE_TYPE (TYPE_BINFO (t)));
14147 error_found = true;
14148 }
14149 }
14150 else if (FUNC_OR_METHOD_TYPE_P (t))
14151 {
14152 if (TYPE_METHOD_BASETYPE (t)
14153 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != RECORD_TYPE
14154 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != UNION_TYPE)
14155 {
14156 error ("%<TYPE_METHOD_BASETYPE%> is not record nor union");
14157 debug_tree (TYPE_METHOD_BASETYPE (t));
14158 error_found = true;
14159 }
14160 }
14161 else if (TREE_CODE (t) == OFFSET_TYPE)
14162 {
14163 if (TYPE_OFFSET_BASETYPE (t)
14164 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != RECORD_TYPE
14165 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != UNION_TYPE)
14166 {
14167 error ("%<TYPE_OFFSET_BASETYPE%> is not record nor union");
14168 debug_tree (TYPE_OFFSET_BASETYPE (t));
14169 error_found = true;
14170 }
14171 }
14172 else if (INTEGRAL_TYPE_P (t) || SCALAR_FLOAT_TYPE_P (t)
14173 || FIXED_POINT_TYPE_P (t))
14174 {
14175 /* FIXME: The following check should pass:
14176 useless_type_conversion_p (const_cast <tree> (t),
14177 TREE_TYPE (TYPE_MAX_VALUE (t))
14178 but does not for C sizetypes in LTO. */
14179 }
14180 else if (TREE_CODE (t) == ARRAY_TYPE)
14181 {
14182 if (TYPE_ARRAY_MAX_SIZE (t)
14183 && TREE_CODE (TYPE_ARRAY_MAX_SIZE (t)) != INTEGER_CST)
14184 {
14185 error ("%<TYPE_ARRAY_MAX_SIZE%> not %<INTEGER_CST%>");
14186 debug_tree (TYPE_ARRAY_MAX_SIZE (t));
14187 error_found = true;
14188 }
14189 }
14190 else if (TYPE_MAX_VALUE_RAW (t))
14191 {
14192 error ("%<TYPE_MAX_VALUE_RAW%> non-NULL");
14193 debug_tree (TYPE_MAX_VALUE_RAW (t));
14194 error_found = true;
14195 }
14196
14197 if (TYPE_LANG_SLOT_1 (t) && in_lto_p)
14198 {
14199 error ("%<TYPE_LANG_SLOT_1 (binfo)%> field is non-NULL");
14200 debug_tree (TYPE_LANG_SLOT_1 (t));
14201 error_found = true;
14202 }
14203
14204 /* Check various uses of TYPE_VALUES_RAW. */
14205 if (TREE_CODE (t) == ENUMERAL_TYPE)
14206 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
14207 {
14208 tree value = TREE_VALUE (l);
14209 tree name = TREE_PURPOSE (l);
14210
14211 /* C FE porduce INTEGER_CST of INTEGER_TYPE, while C++ FE uses
14212 CONST_DECL of ENUMERAL TYPE. */
14213 if (TREE_CODE (value) != INTEGER_CST && TREE_CODE (value) != CONST_DECL)
14214 {
14215 error ("enum value is not %<CONST_DECL%> or %<INTEGER_CST%>");
14216 debug_tree (value);
14217 debug_tree (name);
14218 error_found = true;
14219 }
14220 if (TREE_CODE (TREE_TYPE (value)) != INTEGER_TYPE
14221 && TREE_CODE (TREE_TYPE (value)) != BOOLEAN_TYPE
14222 && !useless_type_conversion_p (const_cast <tree> (t), TREE_TYPE (value)))
14223 {
14224 error ("enum value type is not %<INTEGER_TYPE%> nor convertible "
14225 "to the enum");
14226 debug_tree (value);
14227 debug_tree (name);
14228 error_found = true;
14229 }
14230 if (TREE_CODE (name) != IDENTIFIER_NODE)
14231 {
14232 error ("enum value name is not %<IDENTIFIER_NODE%>");
14233 debug_tree (value);
14234 debug_tree (name);
14235 error_found = true;
14236 }
14237 }
14238 else if (TREE_CODE (t) == ARRAY_TYPE)
14239 {
14240 if (TYPE_DOMAIN (t) && TREE_CODE (TYPE_DOMAIN (t)) != INTEGER_TYPE)
14241 {
14242 error ("array %<TYPE_DOMAIN%> is not integer type");
14243 debug_tree (TYPE_DOMAIN (t));
14244 error_found = true;
14245 }
14246 }
14247 else if (RECORD_OR_UNION_TYPE_P (t))
14248 {
14249 if (TYPE_FIELDS (t) && !COMPLETE_TYPE_P (t) && in_lto_p)
14250 {
14251 error ("%<TYPE_FIELDS%> defined in incomplete type");
14252 error_found = true;
14253 }
14254 for (tree fld = TYPE_FIELDS (t); fld; fld = TREE_CHAIN (fld))
14255 {
14256 /* TODO: verify properties of decls. */
14257 if (TREE_CODE (fld) == FIELD_DECL)
14258 ;
14259 else if (TREE_CODE (fld) == TYPE_DECL)
14260 ;
14261 else if (TREE_CODE (fld) == CONST_DECL)
14262 ;
14263 else if (VAR_P (fld))
14264 ;
14265 else if (TREE_CODE (fld) == TEMPLATE_DECL)
14266 ;
14267 else if (TREE_CODE (fld) == USING_DECL)
14268 ;
14269 else if (TREE_CODE (fld) == FUNCTION_DECL)
14270 ;
14271 else
14272 {
14273 error ("wrong tree in %<TYPE_FIELDS%> list");
14274 debug_tree (fld);
14275 error_found = true;
14276 }
14277 }
14278 }
14279 else if (TREE_CODE (t) == INTEGER_TYPE
14280 || TREE_CODE (t) == BOOLEAN_TYPE
14281 || TREE_CODE (t) == BITINT_TYPE
14282 || TREE_CODE (t) == OFFSET_TYPE
14283 || TREE_CODE (t) == REFERENCE_TYPE
14284 || TREE_CODE (t) == NULLPTR_TYPE
14285 || TREE_CODE (t) == POINTER_TYPE)
14286 {
14287 if (TYPE_CACHED_VALUES_P (t) != (TYPE_CACHED_VALUES (t) != NULL))
14288 {
14289 error ("%<TYPE_CACHED_VALUES_P%> is %i while %<TYPE_CACHED_VALUES%> "
14290 "is %p",
14291 TYPE_CACHED_VALUES_P (t), (void *)TYPE_CACHED_VALUES (t));
14292 error_found = true;
14293 }
14294 else if (TYPE_CACHED_VALUES_P (t) && TREE_CODE (TYPE_CACHED_VALUES (t)) != TREE_VEC)
14295 {
14296 error ("%<TYPE_CACHED_VALUES%> is not %<TREE_VEC%>");
14297 debug_tree (TYPE_CACHED_VALUES (t));
14298 error_found = true;
14299 }
14300 /* Verify just enough of cache to ensure that no one copied it to new type.
14301 All copying should go by copy_node that should clear it. */
14302 else if (TYPE_CACHED_VALUES_P (t))
14303 {
14304 int i;
14305 for (i = 0; i < TREE_VEC_LENGTH (TYPE_CACHED_VALUES (t)); i++)
14306 if (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)
14307 && TREE_TYPE (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)) != t)
14308 {
14309 error ("wrong %<TYPE_CACHED_VALUES%> entry");
14310 debug_tree (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i));
14311 error_found = true;
14312 break;
14313 }
14314 }
14315 }
14316 else if (FUNC_OR_METHOD_TYPE_P (t))
14317 for (tree l = TYPE_ARG_TYPES (t); l; l = TREE_CHAIN (l))
14318 {
14319 /* C++ FE uses TREE_PURPOSE to store initial values. */
14320 if (TREE_PURPOSE (l) && in_lto_p)
14321 {
14322 error ("%<TREE_PURPOSE%> is non-NULL in %<TYPE_ARG_TYPES%> list");
14323 debug_tree (l);
14324 error_found = true;
14325 }
14326 if (!TYPE_P (TREE_VALUE (l)))
14327 {
14328 error ("wrong entry in %<TYPE_ARG_TYPES%> list");
14329 debug_tree (l);
14330 error_found = true;
14331 }
14332 }
14333 else if (!is_lang_specific (t) && TYPE_VALUES_RAW (t))
14334 {
14335 error ("%<TYPE_VALUES_RAW%> field is non-NULL");
14336 debug_tree (TYPE_VALUES_RAW (t));
14337 error_found = true;
14338 }
14339 if (TREE_CODE (t) != INTEGER_TYPE
14340 && TREE_CODE (t) != BOOLEAN_TYPE
14341 && TREE_CODE (t) != BITINT_TYPE
14342 && TREE_CODE (t) != OFFSET_TYPE
14343 && TREE_CODE (t) != REFERENCE_TYPE
14344 && TREE_CODE (t) != NULLPTR_TYPE
14345 && TREE_CODE (t) != POINTER_TYPE
14346 && TYPE_CACHED_VALUES_P (t))
14347 {
14348 error ("%<TYPE_CACHED_VALUES_P%> is set while it should not be");
14349 error_found = true;
14350 }
14351
14352 /* ipa-devirt makes an assumption that TYPE_METHOD_BASETYPE is always
14353 TYPE_MAIN_VARIANT and it would be odd to add methods only to variatns
14354 of a type. */
14355 if (TREE_CODE (t) == METHOD_TYPE
14356 && TYPE_MAIN_VARIANT (TYPE_METHOD_BASETYPE (t)) != TYPE_METHOD_BASETYPE (t))
14357 {
14358 error ("%<TYPE_METHOD_BASETYPE%> is not main variant");
14359 error_found = true;
14360 }
14361
14362 if (error_found)
14363 {
14364 debug_tree (const_cast <tree> (t));
14365 internal_error ("%qs failed", __func__);
14366 }
14367}
14368
14369
14370/* Return 1 if ARG interpreted as signed in its precision is known to be
14371 always positive or 2 if ARG is known to be always negative, or 3 if
14372 ARG may be positive or negative. */
14373
14374int
14375get_range_pos_neg (tree arg)
14376{
14377 if (arg == error_mark_node)
14378 return 3;
14379
14380 int prec = TYPE_PRECISION (TREE_TYPE (arg));
14381 int cnt = 0;
14382 if (TREE_CODE (arg) == INTEGER_CST)
14383 {
14384 wide_int w = wi::sext (x: wi::to_wide (t: arg), offset: prec);
14385 if (wi::neg_p (x: w))
14386 return 2;
14387 else
14388 return 1;
14389 }
14390 while (CONVERT_EXPR_P (arg)
14391 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
14392 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg, 0))) <= prec)
14393 {
14394 arg = TREE_OPERAND (arg, 0);
14395 /* Narrower value zero extended into wider type
14396 will always result in positive values. */
14397 if (TYPE_UNSIGNED (TREE_TYPE (arg))
14398 && TYPE_PRECISION (TREE_TYPE (arg)) < prec)
14399 return 1;
14400 prec = TYPE_PRECISION (TREE_TYPE (arg));
14401 if (++cnt > 30)
14402 return 3;
14403 }
14404
14405 if (TREE_CODE (arg) != SSA_NAME)
14406 return 3;
14407 value_range r;
14408 while (!get_global_range_query ()->range_of_expr (r, expr: arg)
14409 || r.undefined_p () || r.varying_p ())
14410 {
14411 gimple *g = SSA_NAME_DEF_STMT (arg);
14412 if (is_gimple_assign (gs: g)
14413 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (g)))
14414 {
14415 tree t = gimple_assign_rhs1 (gs: g);
14416 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
14417 && TYPE_PRECISION (TREE_TYPE (t)) <= prec)
14418 {
14419 if (TYPE_UNSIGNED (TREE_TYPE (t))
14420 && TYPE_PRECISION (TREE_TYPE (t)) < prec)
14421 return 1;
14422 prec = TYPE_PRECISION (TREE_TYPE (t));
14423 arg = t;
14424 if (++cnt > 30)
14425 return 3;
14426 continue;
14427 }
14428 }
14429 return 3;
14430 }
14431 if (TYPE_UNSIGNED (TREE_TYPE (arg)))
14432 {
14433 /* For unsigned values, the "positive" range comes
14434 below the "negative" range. */
14435 if (!wi::neg_p (x: wi::sext (x: r.upper_bound (), offset: prec), sgn: SIGNED))
14436 return 1;
14437 if (wi::neg_p (x: wi::sext (x: r.lower_bound (), offset: prec), sgn: SIGNED))
14438 return 2;
14439 }
14440 else
14441 {
14442 if (!wi::neg_p (x: wi::sext (x: r.lower_bound (), offset: prec), sgn: SIGNED))
14443 return 1;
14444 if (wi::neg_p (x: wi::sext (x: r.upper_bound (), offset: prec), sgn: SIGNED))
14445 return 2;
14446 }
14447 return 3;
14448}
14449
14450
14451
14452
14453/* Return true if ARG is marked with the nonnull attribute in the
14454 current function signature. */
14455
14456bool
14457nonnull_arg_p (const_tree arg)
14458{
14459 tree t, attrs, fntype;
14460 unsigned HOST_WIDE_INT arg_num;
14461
14462 gcc_assert (TREE_CODE (arg) == PARM_DECL
14463 && (POINTER_TYPE_P (TREE_TYPE (arg))
14464 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE));
14465
14466 /* The static chain decl is always non null. */
14467 if (arg == cfun->static_chain_decl)
14468 return true;
14469
14470 /* THIS argument of method is always non-NULL. */
14471 if (TREE_CODE (TREE_TYPE (cfun->decl)) == METHOD_TYPE
14472 && arg == DECL_ARGUMENTS (cfun->decl)
14473 && flag_delete_null_pointer_checks)
14474 return true;
14475
14476 /* Values passed by reference are always non-NULL. */
14477 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
14478 && flag_delete_null_pointer_checks)
14479 return true;
14480
14481 fntype = TREE_TYPE (cfun->decl);
14482 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
14483 {
14484 attrs = lookup_attribute (attr_name: "nonnull", list: attrs);
14485
14486 /* If "nonnull" wasn't specified, we know nothing about the argument. */
14487 if (attrs == NULL_TREE)
14488 return false;
14489
14490 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
14491 if (TREE_VALUE (attrs) == NULL_TREE)
14492 return true;
14493
14494 /* Get the position number for ARG in the function signature. */
14495 for (arg_num = 1, t = DECL_ARGUMENTS (cfun->decl);
14496 t;
14497 t = DECL_CHAIN (t), arg_num++)
14498 {
14499 if (t == arg)
14500 break;
14501 }
14502
14503 gcc_assert (t == arg);
14504
14505 /* Now see if ARG_NUM is mentioned in the nonnull list. */
14506 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
14507 {
14508 if (compare_tree_int (TREE_VALUE (t), u: arg_num) == 0)
14509 return true;
14510 }
14511 }
14512
14513 return false;
14514}
14515
14516/* Combine LOC and BLOCK to a combined adhoc loc, retaining any range
14517 information. */
14518
14519location_t
14520set_block (location_t loc, tree block)
14521{
14522 location_t pure_loc = get_pure_location (loc);
14523 source_range src_range = get_range_from_loc (set: line_table, loc);
14524 unsigned discriminator = get_discriminator_from_loc (set: line_table, loc);
14525 return line_table->get_or_create_combined_loc (locus: pure_loc, src_range, data: block,
14526 discriminator);
14527}
14528
14529location_t
14530set_source_range (tree expr, location_t start, location_t finish)
14531{
14532 source_range src_range;
14533 src_range.m_start = start;
14534 src_range.m_finish = finish;
14535 return set_source_range (expr, src_range);
14536}
14537
14538location_t
14539set_source_range (tree expr, source_range src_range)
14540{
14541 if (!EXPR_P (expr))
14542 return UNKNOWN_LOCATION;
14543
14544 location_t expr_location = EXPR_LOCATION (expr);
14545 location_t pure_loc = get_pure_location (loc: expr_location);
14546 unsigned discriminator = get_discriminator_from_loc (expr_location);
14547 location_t adhoc = line_table->get_or_create_combined_loc (locus: pure_loc,
14548 src_range,
14549 data: nullptr,
14550 discriminator);
14551 SET_EXPR_LOCATION (expr, adhoc);
14552 return adhoc;
14553}
14554
14555/* Return EXPR, potentially wrapped with a node expression LOC,
14556 if !CAN_HAVE_LOCATION_P (expr).
14557
14558 NON_LVALUE_EXPR is used for wrapping constants, apart from STRING_CST.
14559 VIEW_CONVERT_EXPR is used for wrapping non-constants and STRING_CST.
14560
14561 Wrapper nodes can be identified using location_wrapper_p. */
14562
14563tree
14564maybe_wrap_with_location (tree expr, location_t loc)
14565{
14566 if (expr == NULL)
14567 return NULL;
14568 if (loc == UNKNOWN_LOCATION)
14569 return expr;
14570 if (CAN_HAVE_LOCATION_P (expr))
14571 return expr;
14572 /* We should only be adding wrappers for constants and for decls,
14573 or for some exceptional tree nodes (e.g. BASELINK in the C++ FE). */
14574 gcc_assert (CONSTANT_CLASS_P (expr)
14575 || DECL_P (expr)
14576 || EXCEPTIONAL_CLASS_P (expr));
14577
14578 /* For now, don't add wrappers to exceptional tree nodes, to minimize
14579 any impact of the wrapper nodes. */
14580 if (EXCEPTIONAL_CLASS_P (expr) || error_operand_p (t: expr))
14581 return expr;
14582
14583 /* Compiler-generated temporary variables don't need a wrapper. */
14584 if (DECL_P (expr) && DECL_ARTIFICIAL (expr) && DECL_IGNORED_P (expr))
14585 return expr;
14586
14587 /* If any auto_suppress_location_wrappers are active, don't create
14588 wrappers. */
14589 if (suppress_location_wrappers > 0)
14590 return expr;
14591
14592 tree_code code
14593 = (((CONSTANT_CLASS_P (expr) && TREE_CODE (expr) != STRING_CST)
14594 || (TREE_CODE (expr) == CONST_DECL && !TREE_STATIC (expr)))
14595 ? NON_LVALUE_EXPR : VIEW_CONVERT_EXPR);
14596 tree wrapper = build1_loc (loc, code, TREE_TYPE (expr), arg1: expr);
14597 /* Mark this node as being a wrapper. */
14598 EXPR_LOCATION_WRAPPER_P (wrapper) = 1;
14599 return wrapper;
14600}
14601
14602int suppress_location_wrappers;
14603
14604/* Return the name of combined function FN, for debugging purposes. */
14605
14606const char *
14607combined_fn_name (combined_fn fn)
14608{
14609 if (builtin_fn_p (code: fn))
14610 {
14611 tree fndecl = builtin_decl_explicit (fncode: as_builtin_fn (code: fn));
14612 return IDENTIFIER_POINTER (DECL_NAME (fndecl));
14613 }
14614 else
14615 return internal_fn_name (fn: as_internal_fn (code: fn));
14616}
14617
14618/* Return a bitmap with a bit set corresponding to each argument in
14619 a function call type FNTYPE declared with attribute nonnull,
14620 or null if none of the function's argument are nonnull. The caller
14621 must free the bitmap. */
14622
14623bitmap
14624get_nonnull_args (const_tree fntype)
14625{
14626 if (fntype == NULL_TREE)
14627 return NULL;
14628
14629 bitmap argmap = NULL;
14630 if (TREE_CODE (fntype) == METHOD_TYPE)
14631 {
14632 /* The this pointer in C++ non-static member functions is
14633 implicitly nonnull whether or not it's declared as such. */
14634 argmap = BITMAP_ALLOC (NULL);
14635 bitmap_set_bit (argmap, 0);
14636 }
14637
14638 tree attrs = TYPE_ATTRIBUTES (fntype);
14639 if (!attrs)
14640 return argmap;
14641
14642 /* A function declaration can specify multiple attribute nonnull,
14643 each with zero or more arguments. The loop below creates a bitmap
14644 representing a union of all the arguments. An empty (but non-null)
14645 bitmap means that all arguments have been declaraed nonnull. */
14646 for ( ; attrs; attrs = TREE_CHAIN (attrs))
14647 {
14648 attrs = lookup_attribute (attr_name: "nonnull", list: attrs);
14649 if (!attrs)
14650 break;
14651
14652 if (!argmap)
14653 argmap = BITMAP_ALLOC (NULL);
14654
14655 if (!TREE_VALUE (attrs))
14656 {
14657 /* Clear the bitmap in case a previous attribute nonnull
14658 set it and this one overrides it for all arguments. */
14659 bitmap_clear (argmap);
14660 return argmap;
14661 }
14662
14663 /* Iterate over the indices of the format arguments declared nonnull
14664 and set a bit for each. */
14665 for (tree idx = TREE_VALUE (attrs); idx; idx = TREE_CHAIN (idx))
14666 {
14667 unsigned int val = TREE_INT_CST_LOW (TREE_VALUE (idx)) - 1;
14668 bitmap_set_bit (argmap, val);
14669 }
14670 }
14671
14672 return argmap;
14673}
14674
14675/* Returns true if TYPE is a type where it and all of its subobjects
14676 (recursively) are of structure, union, or array type. */
14677
14678bool
14679is_empty_type (const_tree type)
14680{
14681 if (RECORD_OR_UNION_TYPE_P (type))
14682 {
14683 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
14684 if (TREE_CODE (field) == FIELD_DECL
14685 && !DECL_PADDING_P (field)
14686 && !is_empty_type (TREE_TYPE (field)))
14687 return false;
14688 return true;
14689 }
14690 else if (TREE_CODE (type) == ARRAY_TYPE)
14691 return (integer_minus_onep (expr: array_type_nelts (type))
14692 || TYPE_DOMAIN (type) == NULL_TREE
14693 || is_empty_type (TREE_TYPE (type)));
14694 return false;
14695}
14696
14697/* Implement TARGET_EMPTY_RECORD_P. Return true if TYPE is an empty type
14698 that shouldn't be passed via stack. */
14699
14700bool
14701default_is_empty_record (const_tree type)
14702{
14703 if (!abi_version_at_least (12))
14704 return false;
14705
14706 if (type == error_mark_node)
14707 return false;
14708
14709 if (TREE_ADDRESSABLE (type))
14710 return false;
14711
14712 return is_empty_type (TYPE_MAIN_VARIANT (type));
14713}
14714
14715/* Determine whether TYPE is a structure with a flexible array member,
14716 or a union containing such a structure (possibly recursively). */
14717
14718bool
14719flexible_array_type_p (const_tree type)
14720{
14721 tree x, last;
14722 switch (TREE_CODE (type))
14723 {
14724 case RECORD_TYPE:
14725 last = NULL_TREE;
14726 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
14727 if (TREE_CODE (x) == FIELD_DECL)
14728 last = x;
14729 if (last == NULL_TREE)
14730 return false;
14731 if (TREE_CODE (TREE_TYPE (last)) == ARRAY_TYPE
14732 && TYPE_SIZE (TREE_TYPE (last)) == NULL_TREE
14733 && TYPE_DOMAIN (TREE_TYPE (last)) != NULL_TREE
14734 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (last))) == NULL_TREE)
14735 return true;
14736 return false;
14737 case UNION_TYPE:
14738 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
14739 {
14740 if (TREE_CODE (x) == FIELD_DECL
14741 && flexible_array_type_p (TREE_TYPE (x)))
14742 return true;
14743 }
14744 return false;
14745 default:
14746 return false;
14747 }
14748}
14749
14750/* Like int_size_in_bytes, but handle empty records specially. */
14751
14752HOST_WIDE_INT
14753arg_int_size_in_bytes (const_tree type)
14754{
14755 return TYPE_EMPTY_P (type) ? 0 : int_size_in_bytes (type);
14756}
14757
14758/* Like size_in_bytes, but handle empty records specially. */
14759
14760tree
14761arg_size_in_bytes (const_tree type)
14762{
14763 return TYPE_EMPTY_P (type) ? size_zero_node : size_in_bytes (t: type);
14764}
14765
14766/* Return true if an expression with CODE has to have the same result type as
14767 its first operand. */
14768
14769bool
14770expr_type_first_operand_type_p (tree_code code)
14771{
14772 switch (code)
14773 {
14774 case NEGATE_EXPR:
14775 case ABS_EXPR:
14776 case BIT_NOT_EXPR:
14777 case PAREN_EXPR:
14778 case CONJ_EXPR:
14779
14780 case PLUS_EXPR:
14781 case MINUS_EXPR:
14782 case MULT_EXPR:
14783 case TRUNC_DIV_EXPR:
14784 case CEIL_DIV_EXPR:
14785 case FLOOR_DIV_EXPR:
14786 case ROUND_DIV_EXPR:
14787 case TRUNC_MOD_EXPR:
14788 case CEIL_MOD_EXPR:
14789 case FLOOR_MOD_EXPR:
14790 case ROUND_MOD_EXPR:
14791 case RDIV_EXPR:
14792 case EXACT_DIV_EXPR:
14793 case MIN_EXPR:
14794 case MAX_EXPR:
14795 case BIT_IOR_EXPR:
14796 case BIT_XOR_EXPR:
14797 case BIT_AND_EXPR:
14798
14799 case LSHIFT_EXPR:
14800 case RSHIFT_EXPR:
14801 case LROTATE_EXPR:
14802 case RROTATE_EXPR:
14803 return true;
14804
14805 default:
14806 return false;
14807 }
14808}
14809
14810/* Return a typenode for the "standard" C type with a given name. */
14811tree
14812get_typenode_from_name (const char *name)
14813{
14814 if (name == NULL || *name == '\0')
14815 return NULL_TREE;
14816
14817 if (strcmp (s1: name, s2: "char") == 0)
14818 return char_type_node;
14819 if (strcmp (s1: name, s2: "unsigned char") == 0)
14820 return unsigned_char_type_node;
14821 if (strcmp (s1: name, s2: "signed char") == 0)
14822 return signed_char_type_node;
14823
14824 if (strcmp (s1: name, s2: "short int") == 0)
14825 return short_integer_type_node;
14826 if (strcmp (s1: name, s2: "short unsigned int") == 0)
14827 return short_unsigned_type_node;
14828
14829 if (strcmp (s1: name, s2: "int") == 0)
14830 return integer_type_node;
14831 if (strcmp (s1: name, s2: "unsigned int") == 0)
14832 return unsigned_type_node;
14833
14834 if (strcmp (s1: name, s2: "long int") == 0)
14835 return long_integer_type_node;
14836 if (strcmp (s1: name, s2: "long unsigned int") == 0)
14837 return long_unsigned_type_node;
14838
14839 if (strcmp (s1: name, s2: "long long int") == 0)
14840 return long_long_integer_type_node;
14841 if (strcmp (s1: name, s2: "long long unsigned int") == 0)
14842 return long_long_unsigned_type_node;
14843
14844 gcc_unreachable ();
14845}
14846
14847/* List of pointer types used to declare builtins before we have seen their
14848 real declaration.
14849
14850 Keep the size up to date in tree.h ! */
14851const builtin_structptr_type builtin_structptr_types[6] =
14852{
14853 { fileptr_type_node, ptr_type_node, .str: "FILE" },
14854 { const_tm_ptr_type_node, const_ptr_type_node, .str: "tm" },
14855 { fenv_t_ptr_type_node, ptr_type_node, .str: "fenv_t" },
14856 { const_fenv_t_ptr_type_node, const_ptr_type_node, .str: "fenv_t" },
14857 { fexcept_t_ptr_type_node, ptr_type_node, .str: "fexcept_t" },
14858 { const_fexcept_t_ptr_type_node, const_ptr_type_node, .str: "fexcept_t" }
14859};
14860
14861/* Return the maximum object size. */
14862
14863tree
14864max_object_size (void)
14865{
14866 /* To do: Make this a configurable parameter. */
14867 return TYPE_MAX_VALUE (ptrdiff_type_node);
14868}
14869
14870/* A wrapper around TARGET_VERIFY_TYPE_CONTEXT that makes the silent_p
14871 parameter default to false and that weeds out error_mark_node. */
14872
14873bool
14874verify_type_context (location_t loc, type_context_kind context,
14875 const_tree type, bool silent_p)
14876{
14877 if (type == error_mark_node)
14878 return true;
14879
14880 gcc_assert (TYPE_P (type));
14881 return (!targetm.verify_type_context
14882 || targetm.verify_type_context (loc, context, type, silent_p));
14883}
14884
14885/* Return true if NEW_ASM and DELETE_ASM name a valid pair of new and
14886 delete operators. Return false if they may or may not name such
14887 a pair and, when nonnull, set *PCERTAIN to true if they certainly
14888 do not. */
14889
14890bool
14891valid_new_delete_pair_p (tree new_asm, tree delete_asm,
14892 bool *pcertain /* = NULL */)
14893{
14894 bool certain;
14895 if (!pcertain)
14896 pcertain = &certain;
14897
14898 const char *new_name = IDENTIFIER_POINTER (new_asm);
14899 const char *delete_name = IDENTIFIER_POINTER (delete_asm);
14900 unsigned int new_len = IDENTIFIER_LENGTH (new_asm);
14901 unsigned int delete_len = IDENTIFIER_LENGTH (delete_asm);
14902
14903 /* The following failures are due to invalid names so they're not
14904 considered certain mismatches. */
14905 *pcertain = false;
14906
14907 if (new_len < 5 || delete_len < 6)
14908 return false;
14909 if (new_name[0] == '_')
14910 ++new_name, --new_len;
14911 if (new_name[0] == '_')
14912 ++new_name, --new_len;
14913 if (delete_name[0] == '_')
14914 ++delete_name, --delete_len;
14915 if (delete_name[0] == '_')
14916 ++delete_name, --delete_len;
14917 if (new_len < 4 || delete_len < 5)
14918 return false;
14919
14920 /* The following failures are due to names of user-defined operators
14921 so they're also not considered certain mismatches. */
14922
14923 /* *_len is now just the length after initial underscores. */
14924 if (new_name[0] != 'Z' || new_name[1] != 'n')
14925 return false;
14926 if (delete_name[0] != 'Z' || delete_name[1] != 'd')
14927 return false;
14928
14929 /* The following failures are certain mismatches. */
14930 *pcertain = true;
14931
14932 /* _Znw must match _Zdl, _Zna must match _Zda. */
14933 if ((new_name[2] != 'w' || delete_name[2] != 'l')
14934 && (new_name[2] != 'a' || delete_name[2] != 'a'))
14935 return false;
14936 /* 'j', 'm' and 'y' correspond to size_t. */
14937 if (new_name[3] != 'j' && new_name[3] != 'm' && new_name[3] != 'y')
14938 return false;
14939 if (delete_name[3] != 'P' || delete_name[4] != 'v')
14940 return false;
14941 if (new_len == 4
14942 || (new_len == 18 && !memcmp (s1: new_name + 4, s2: "RKSt9nothrow_t", n: 14)))
14943 {
14944 /* _ZnXY or _ZnXYRKSt9nothrow_t matches
14945 _ZdXPv, _ZdXPvY and _ZdXPvRKSt9nothrow_t. */
14946 if (delete_len == 5)
14947 return true;
14948 if (delete_len == 6 && delete_name[5] == new_name[3])
14949 return true;
14950 if (delete_len == 19 && !memcmp (s1: delete_name + 5, s2: "RKSt9nothrow_t", n: 14))
14951 return true;
14952 }
14953 else if ((new_len == 19 && !memcmp (s1: new_name + 4, s2: "St11align_val_t", n: 15))
14954 || (new_len == 33
14955 && !memcmp (s1: new_name + 4, s2: "St11align_val_tRKSt9nothrow_t", n: 29)))
14956 {
14957 /* _ZnXYSt11align_val_t or _ZnXYSt11align_val_tRKSt9nothrow_t matches
14958 _ZdXPvSt11align_val_t or _ZdXPvYSt11align_val_t or or
14959 _ZdXPvSt11align_val_tRKSt9nothrow_t. */
14960 if (delete_len == 20 && !memcmp (s1: delete_name + 5, s2: "St11align_val_t", n: 15))
14961 return true;
14962 if (delete_len == 21
14963 && delete_name[5] == new_name[3]
14964 && !memcmp (s1: delete_name + 6, s2: "St11align_val_t", n: 15))
14965 return true;
14966 if (delete_len == 34
14967 && !memcmp (s1: delete_name + 5, s2: "St11align_val_tRKSt9nothrow_t", n: 29))
14968 return true;
14969 }
14970
14971 /* The negative result is conservative. */
14972 *pcertain = false;
14973 return false;
14974}
14975
14976/* Return the zero-based number corresponding to the argument being
14977 deallocated if FNDECL is a deallocation function or an out-of-bounds
14978 value if it isn't. */
14979
14980unsigned
14981fndecl_dealloc_argno (tree fndecl)
14982{
14983 /* A call to operator delete isn't recognized as one to a built-in. */
14984 if (DECL_IS_OPERATOR_DELETE_P (fndecl))
14985 {
14986 if (DECL_IS_REPLACEABLE_OPERATOR (fndecl))
14987 return 0;
14988
14989 /* Avoid placement delete that's not been inlined. */
14990 tree fname = DECL_ASSEMBLER_NAME (fndecl);
14991 if (id_equal (id: fname, str: "_ZdlPvS_") // ordinary form
14992 || id_equal (id: fname, str: "_ZdaPvS_")) // array form
14993 return UINT_MAX;
14994 return 0;
14995 }
14996
14997 /* TODO: Handle user-defined functions with attribute malloc? Handle
14998 known non-built-ins like fopen? */
14999 if (fndecl_built_in_p (node: fndecl, klass: BUILT_IN_NORMAL))
15000 {
15001 switch (DECL_FUNCTION_CODE (decl: fndecl))
15002 {
15003 case BUILT_IN_FREE:
15004 case BUILT_IN_REALLOC:
15005 return 0;
15006 default:
15007 break;
15008 }
15009 return UINT_MAX;
15010 }
15011
15012 tree attrs = DECL_ATTRIBUTES (fndecl);
15013 if (!attrs)
15014 return UINT_MAX;
15015
15016 for (tree atfree = attrs;
15017 (atfree = lookup_attribute (attr_name: "*dealloc", list: atfree));
15018 atfree = TREE_CHAIN (atfree))
15019 {
15020 tree alloc = TREE_VALUE (atfree);
15021 if (!alloc)
15022 continue;
15023
15024 tree pos = TREE_CHAIN (alloc);
15025 if (!pos)
15026 return 0;
15027
15028 pos = TREE_VALUE (pos);
15029 return TREE_INT_CST_LOW (pos) - 1;
15030 }
15031
15032 return UINT_MAX;
15033}
15034
15035/* If EXPR refers to a character array or pointer declared attribute
15036 nonstring, return a decl for that array or pointer and set *REF
15037 to the referenced enclosing object or pointer. Otherwise return
15038 null. */
15039
15040tree
15041get_attr_nonstring_decl (tree expr, tree *ref)
15042{
15043 tree decl = expr;
15044 tree var = NULL_TREE;
15045 if (TREE_CODE (decl) == SSA_NAME)
15046 {
15047 gimple *def = SSA_NAME_DEF_STMT (decl);
15048
15049 if (is_gimple_assign (gs: def))
15050 {
15051 tree_code code = gimple_assign_rhs_code (gs: def);
15052 if (code == ADDR_EXPR
15053 || code == COMPONENT_REF
15054 || code == VAR_DECL)
15055 decl = gimple_assign_rhs1 (gs: def);
15056 }
15057 else
15058 var = SSA_NAME_VAR (decl);
15059 }
15060
15061 if (TREE_CODE (decl) == ADDR_EXPR)
15062 decl = TREE_OPERAND (decl, 0);
15063
15064 /* To simplify calling code, store the referenced DECL regardless of
15065 the attribute determined below, but avoid storing the SSA_NAME_VAR
15066 obtained above (it's not useful for dataflow purposes). */
15067 if (ref)
15068 *ref = decl;
15069
15070 /* Use the SSA_NAME_VAR that was determined above to see if it's
15071 declared nonstring. Otherwise drill down into the referenced
15072 DECL. */
15073 if (var)
15074 decl = var;
15075 else if (TREE_CODE (decl) == ARRAY_REF)
15076 decl = TREE_OPERAND (decl, 0);
15077 else if (TREE_CODE (decl) == COMPONENT_REF)
15078 decl = TREE_OPERAND (decl, 1);
15079 else if (TREE_CODE (decl) == MEM_REF)
15080 return get_attr_nonstring_decl (TREE_OPERAND (decl, 0), ref);
15081
15082 if (DECL_P (decl)
15083 && lookup_attribute (attr_name: "nonstring", DECL_ATTRIBUTES (decl)))
15084 return decl;
15085
15086 return NULL_TREE;
15087}
15088
15089/* Return length of attribute names string,
15090 if arglist chain > 1, -1 otherwise. */
15091
15092int
15093get_target_clone_attr_len (tree arglist)
15094{
15095 tree arg;
15096 int str_len_sum = 0;
15097 int argnum = 0;
15098
15099 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
15100 {
15101 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
15102 size_t len = strlen (s: str);
15103 str_len_sum += len + 1;
15104 for (const char *p = strchr (s: str, c: ','); p; p = strchr (s: p + 1, c: ','))
15105 argnum++;
15106 argnum++;
15107 }
15108 if (argnum <= 1)
15109 return -1;
15110 return str_len_sum;
15111}
15112
15113void
15114tree_cc_finalize (void)
15115{
15116 clear_nonstandard_integer_type_cache ();
15117 vec_free (v&: bitint_type_cache);
15118}
15119
15120#if CHECKING_P
15121
15122namespace selftest {
15123
15124/* Selftests for tree. */
15125
15126/* Verify that integer constants are sane. */
15127
15128static void
15129test_integer_constants ()
15130{
15131 ASSERT_TRUE (integer_type_node != NULL);
15132 ASSERT_TRUE (build_int_cst (integer_type_node, 0) != NULL);
15133
15134 tree type = integer_type_node;
15135
15136 tree zero = build_zero_cst (type);
15137 ASSERT_EQ (INTEGER_CST, TREE_CODE (zero));
15138 ASSERT_EQ (type, TREE_TYPE (zero));
15139
15140 tree one = build_int_cst (type, cst: 1);
15141 ASSERT_EQ (INTEGER_CST, TREE_CODE (one));
15142 ASSERT_EQ (type, TREE_TYPE (zero));
15143}
15144
15145/* Verify identifiers. */
15146
15147static void
15148test_identifiers ()
15149{
15150 tree identifier = get_identifier ("foo");
15151 ASSERT_EQ (3, IDENTIFIER_LENGTH (identifier));
15152 ASSERT_STREQ ("foo", IDENTIFIER_POINTER (identifier));
15153}
15154
15155/* Verify LABEL_DECL. */
15156
15157static void
15158test_labels ()
15159{
15160 tree identifier = get_identifier ("err");
15161 tree label_decl = build_decl (UNKNOWN_LOCATION, code: LABEL_DECL,
15162 name: identifier, void_type_node);
15163 ASSERT_EQ (-1, LABEL_DECL_UID (label_decl));
15164 ASSERT_FALSE (FORCED_LABEL (label_decl));
15165}
15166
15167/* Return a new VECTOR_CST node whose type is TYPE and whose values
15168 are given by VALS. */
15169
15170static tree
15171build_vector (tree type, const vec<tree> &vals MEM_STAT_DECL)
15172{
15173 gcc_assert (known_eq (vals.length (), TYPE_VECTOR_SUBPARTS (type)));
15174 tree_vector_builder builder (type, vals.length (), 1);
15175 builder.splice (src: vals);
15176 return builder.build ();
15177}
15178
15179/* Check that VECTOR_CST ACTUAL contains the elements in EXPECTED. */
15180
15181static void
15182check_vector_cst (const vec<tree> &expected, tree actual)
15183{
15184 ASSERT_KNOWN_EQ (expected.length (),
15185 TYPE_VECTOR_SUBPARTS (TREE_TYPE (actual)));
15186 for (unsigned int i = 0; i < expected.length (); ++i)
15187 ASSERT_EQ (wi::to_wide (expected[i]),
15188 wi::to_wide (vector_cst_elt (actual, i)));
15189}
15190
15191/* Check that VECTOR_CST ACTUAL contains NPATTERNS duplicated elements,
15192 and that its elements match EXPECTED. */
15193
15194static void
15195check_vector_cst_duplicate (const vec<tree> &expected, tree actual,
15196 unsigned int npatterns)
15197{
15198 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
15199 ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (actual));
15200 ASSERT_EQ (npatterns, vector_cst_encoded_nelts (actual));
15201 ASSERT_TRUE (VECTOR_CST_DUPLICATE_P (actual));
15202 ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
15203 check_vector_cst (expected, actual);
15204}
15205
15206/* Check that VECTOR_CST ACTUAL contains NPATTERNS foreground elements
15207 and NPATTERNS background elements, and that its elements match
15208 EXPECTED. */
15209
15210static void
15211check_vector_cst_fill (const vec<tree> &expected, tree actual,
15212 unsigned int npatterns)
15213{
15214 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
15215 ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (actual));
15216 ASSERT_EQ (2 * npatterns, vector_cst_encoded_nelts (actual));
15217 ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
15218 ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
15219 check_vector_cst (expected, actual);
15220}
15221
15222/* Check that VECTOR_CST ACTUAL contains NPATTERNS stepped patterns,
15223 and that its elements match EXPECTED. */
15224
15225static void
15226check_vector_cst_stepped (const vec<tree> &expected, tree actual,
15227 unsigned int npatterns)
15228{
15229 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
15230 ASSERT_EQ (3, VECTOR_CST_NELTS_PER_PATTERN (actual));
15231 ASSERT_EQ (3 * npatterns, vector_cst_encoded_nelts (actual));
15232 ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
15233 ASSERT_TRUE (VECTOR_CST_STEPPED_P (actual));
15234 check_vector_cst (expected, actual);
15235}
15236
15237/* Test the creation of VECTOR_CSTs. */
15238
15239static void
15240test_vector_cst_patterns (ALONE_CXX_MEM_STAT_INFO)
15241{
15242 auto_vec<tree, 8> elements (8);
15243 elements.quick_grow (len: 8);
15244 tree element_type = build_nonstandard_integer_type (precision: 16, unsignedp: true);
15245 tree vector_type = build_vector_type (innertype: element_type, nunits: 8);
15246
15247 /* Test a simple linear series with a base of 0 and a step of 1:
15248 { 0, 1, 2, 3, 4, 5, 6, 7 }. */
15249 for (unsigned int i = 0; i < 8; ++i)
15250 elements[i] = build_int_cst (type: element_type, cst: i);
15251 tree vector = build_vector (type: vector_type, vals: elements PASS_MEM_STAT);
15252 check_vector_cst_stepped (expected: elements, actual: vector, npatterns: 1);
15253
15254 /* Try the same with the first element replaced by 100:
15255 { 100, 1, 2, 3, 4, 5, 6, 7 }. */
15256 elements[0] = build_int_cst (type: element_type, cst: 100);
15257 vector = build_vector (type: vector_type, vals: elements PASS_MEM_STAT);
15258 check_vector_cst_stepped (expected: elements, actual: vector, npatterns: 1);
15259
15260 /* Try a series that wraps around.
15261 { 100, 65531, 65532, 65533, 65534, 65535, 0, 1 }. */
15262 for (unsigned int i = 1; i < 8; ++i)
15263 elements[i] = build_int_cst (type: element_type, cst: (65530 + i) & 0xffff);
15264 vector = build_vector (type: vector_type, vals: elements PASS_MEM_STAT);
15265 check_vector_cst_stepped (expected: elements, actual: vector, npatterns: 1);
15266
15267 /* Try a downward series:
15268 { 100, 79, 78, 77, 76, 75, 75, 73 }. */
15269 for (unsigned int i = 1; i < 8; ++i)
15270 elements[i] = build_int_cst (type: element_type, cst: 80 - i);
15271 vector = build_vector (type: vector_type, vals: elements PASS_MEM_STAT);
15272 check_vector_cst_stepped (expected: elements, actual: vector, npatterns: 1);
15273
15274 /* Try two interleaved series with different bases and steps:
15275 { 100, 53, 66, 206, 62, 212, 58, 218 }. */
15276 elements[1] = build_int_cst (type: element_type, cst: 53);
15277 for (unsigned int i = 2; i < 8; i += 2)
15278 {
15279 elements[i] = build_int_cst (type: element_type, cst: 70 - i * 2);
15280 elements[i + 1] = build_int_cst (type: element_type, cst: 200 + i * 3);
15281 }
15282 vector = build_vector (type: vector_type, vals: elements PASS_MEM_STAT);
15283 check_vector_cst_stepped (expected: elements, actual: vector, npatterns: 2);
15284
15285 /* Try a duplicated value:
15286 { 100, 100, 100, 100, 100, 100, 100, 100 }. */
15287 for (unsigned int i = 1; i < 8; ++i)
15288 elements[i] = elements[0];
15289 vector = build_vector (type: vector_type, vals: elements PASS_MEM_STAT);
15290 check_vector_cst_duplicate (expected: elements, actual: vector, npatterns: 1);
15291
15292 /* Try an interleaved duplicated value:
15293 { 100, 55, 100, 55, 100, 55, 100, 55 }. */
15294 elements[1] = build_int_cst (type: element_type, cst: 55);
15295 for (unsigned int i = 2; i < 8; ++i)
15296 elements[i] = elements[i - 2];
15297 vector = build_vector (type: vector_type, vals: elements PASS_MEM_STAT);
15298 check_vector_cst_duplicate (expected: elements, actual: vector, npatterns: 2);
15299
15300 /* Try a duplicated value with 2 exceptions
15301 { 41, 97, 100, 55, 100, 55, 100, 55 }. */
15302 elements[0] = build_int_cst (type: element_type, cst: 41);
15303 elements[1] = build_int_cst (type: element_type, cst: 97);
15304 vector = build_vector (type: vector_type, vals: elements PASS_MEM_STAT);
15305 check_vector_cst_fill (expected: elements, actual: vector, npatterns: 2);
15306
15307 /* Try with and without a step
15308 { 41, 97, 100, 21, 100, 35, 100, 49 }. */
15309 for (unsigned int i = 3; i < 8; i += 2)
15310 elements[i] = build_int_cst (type: element_type, cst: i * 7);
15311 vector = build_vector (type: vector_type, vals: elements PASS_MEM_STAT);
15312 check_vector_cst_stepped (expected: elements, actual: vector, npatterns: 2);
15313
15314 /* Try a fully-general constant:
15315 { 41, 97, 100, 21, 100, 9990, 100, 49 }. */
15316 elements[5] = build_int_cst (type: element_type, cst: 9990);
15317 vector = build_vector (type: vector_type, vals: elements PASS_MEM_STAT);
15318 check_vector_cst_fill (expected: elements, actual: vector, npatterns: 4);
15319}
15320
15321/* Verify that STRIP_NOPS (NODE) is EXPECTED.
15322 Helper function for test_location_wrappers, to deal with STRIP_NOPS
15323 modifying its argument in-place. */
15324
15325static void
15326check_strip_nops (tree node, tree expected)
15327{
15328 STRIP_NOPS (node);
15329 ASSERT_EQ (expected, node);
15330}
15331
15332/* Verify location wrappers. */
15333
15334static void
15335test_location_wrappers ()
15336{
15337 location_t loc = BUILTINS_LOCATION;
15338
15339 ASSERT_EQ (NULL_TREE, maybe_wrap_with_location (NULL_TREE, loc));
15340
15341 /* Wrapping a constant. */
15342 tree int_cst = build_int_cst (integer_type_node, cst: 42);
15343 ASSERT_FALSE (CAN_HAVE_LOCATION_P (int_cst));
15344 ASSERT_FALSE (location_wrapper_p (int_cst));
15345
15346 tree wrapped_int_cst = maybe_wrap_with_location (expr: int_cst, loc);
15347 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
15348 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_int_cst));
15349 ASSERT_EQ (int_cst, tree_strip_any_location_wrapper (wrapped_int_cst));
15350
15351 /* We shouldn't add wrapper nodes for UNKNOWN_LOCATION. */
15352 ASSERT_EQ (int_cst, maybe_wrap_with_location (int_cst, UNKNOWN_LOCATION));
15353
15354 /* We shouldn't add wrapper nodes for nodes that CAN_HAVE_LOCATION_P. */
15355 tree cast = build1 (code: NOP_EXPR, char_type_node, node: int_cst);
15356 ASSERT_TRUE (CAN_HAVE_LOCATION_P (cast));
15357 ASSERT_EQ (cast, maybe_wrap_with_location (cast, loc));
15358
15359 /* Wrapping a STRING_CST. */
15360 tree string_cst = build_string (len: 4, str: "foo");
15361 ASSERT_FALSE (CAN_HAVE_LOCATION_P (string_cst));
15362 ASSERT_FALSE (location_wrapper_p (string_cst));
15363
15364 tree wrapped_string_cst = maybe_wrap_with_location (expr: string_cst, loc);
15365 ASSERT_TRUE (location_wrapper_p (wrapped_string_cst));
15366 ASSERT_EQ (VIEW_CONVERT_EXPR, TREE_CODE (wrapped_string_cst));
15367 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_string_cst));
15368 ASSERT_EQ (string_cst, tree_strip_any_location_wrapper (wrapped_string_cst));
15369
15370
15371 /* Wrapping a variable. */
15372 tree int_var = build_decl (UNKNOWN_LOCATION, code: VAR_DECL,
15373 get_identifier ("some_int_var"),
15374 integer_type_node);
15375 ASSERT_FALSE (CAN_HAVE_LOCATION_P (int_var));
15376 ASSERT_FALSE (location_wrapper_p (int_var));
15377
15378 tree wrapped_int_var = maybe_wrap_with_location (expr: int_var, loc);
15379 ASSERT_TRUE (location_wrapper_p (wrapped_int_var));
15380 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_int_var));
15381 ASSERT_EQ (int_var, tree_strip_any_location_wrapper (wrapped_int_var));
15382
15383 /* Verify that "reinterpret_cast<int>(some_int_var)" is not a location
15384 wrapper. */
15385 tree r_cast = build1 (code: NON_LVALUE_EXPR, integer_type_node, node: int_var);
15386 ASSERT_FALSE (location_wrapper_p (r_cast));
15387 ASSERT_EQ (r_cast, tree_strip_any_location_wrapper (r_cast));
15388
15389 /* Verify that STRIP_NOPS removes wrappers. */
15390 check_strip_nops (node: wrapped_int_cst, expected: int_cst);
15391 check_strip_nops (node: wrapped_string_cst, expected: string_cst);
15392 check_strip_nops (node: wrapped_int_var, expected: int_var);
15393}
15394
15395/* Test various tree predicates. Verify that location wrappers don't
15396 affect the results. */
15397
15398static void
15399test_predicates ()
15400{
15401 /* Build various constants and wrappers around them. */
15402
15403 location_t loc = BUILTINS_LOCATION;
15404
15405 tree i_0 = build_int_cst (integer_type_node, cst: 0);
15406 tree wr_i_0 = maybe_wrap_with_location (expr: i_0, loc);
15407
15408 tree i_1 = build_int_cst (integer_type_node, cst: 1);
15409 tree wr_i_1 = maybe_wrap_with_location (expr: i_1, loc);
15410
15411 tree i_m1 = build_int_cst (integer_type_node, cst: -1);
15412 tree wr_i_m1 = maybe_wrap_with_location (expr: i_m1, loc);
15413
15414 tree f_0 = build_real_from_int_cst (float_type_node, i: i_0);
15415 tree wr_f_0 = maybe_wrap_with_location (expr: f_0, loc);
15416 tree f_1 = build_real_from_int_cst (float_type_node, i: i_1);
15417 tree wr_f_1 = maybe_wrap_with_location (expr: f_1, loc);
15418 tree f_m1 = build_real_from_int_cst (float_type_node, i: i_m1);
15419 tree wr_f_m1 = maybe_wrap_with_location (expr: f_m1, loc);
15420
15421 tree c_i_0 = build_complex (NULL_TREE, real: i_0, imag: i_0);
15422 tree c_i_1 = build_complex (NULL_TREE, real: i_1, imag: i_0);
15423 tree c_i_m1 = build_complex (NULL_TREE, real: i_m1, imag: i_0);
15424
15425 tree c_f_0 = build_complex (NULL_TREE, real: f_0, imag: f_0);
15426 tree c_f_1 = build_complex (NULL_TREE, real: f_1, imag: f_0);
15427 tree c_f_m1 = build_complex (NULL_TREE, real: f_m1, imag: f_0);
15428
15429 /* TODO: vector constants. */
15430
15431 /* Test integer_onep. */
15432 ASSERT_FALSE (integer_onep (i_0));
15433 ASSERT_FALSE (integer_onep (wr_i_0));
15434 ASSERT_TRUE (integer_onep (i_1));
15435 ASSERT_TRUE (integer_onep (wr_i_1));
15436 ASSERT_FALSE (integer_onep (i_m1));
15437 ASSERT_FALSE (integer_onep (wr_i_m1));
15438 ASSERT_FALSE (integer_onep (f_0));
15439 ASSERT_FALSE (integer_onep (wr_f_0));
15440 ASSERT_FALSE (integer_onep (f_1));
15441 ASSERT_FALSE (integer_onep (wr_f_1));
15442 ASSERT_FALSE (integer_onep (f_m1));
15443 ASSERT_FALSE (integer_onep (wr_f_m1));
15444 ASSERT_FALSE (integer_onep (c_i_0));
15445 ASSERT_TRUE (integer_onep (c_i_1));
15446 ASSERT_FALSE (integer_onep (c_i_m1));
15447 ASSERT_FALSE (integer_onep (c_f_0));
15448 ASSERT_FALSE (integer_onep (c_f_1));
15449 ASSERT_FALSE (integer_onep (c_f_m1));
15450
15451 /* Test integer_zerop. */
15452 ASSERT_TRUE (integer_zerop (i_0));
15453 ASSERT_TRUE (integer_zerop (wr_i_0));
15454 ASSERT_FALSE (integer_zerop (i_1));
15455 ASSERT_FALSE (integer_zerop (wr_i_1));
15456 ASSERT_FALSE (integer_zerop (i_m1));
15457 ASSERT_FALSE (integer_zerop (wr_i_m1));
15458 ASSERT_FALSE (integer_zerop (f_0));
15459 ASSERT_FALSE (integer_zerop (wr_f_0));
15460 ASSERT_FALSE (integer_zerop (f_1));
15461 ASSERT_FALSE (integer_zerop (wr_f_1));
15462 ASSERT_FALSE (integer_zerop (f_m1));
15463 ASSERT_FALSE (integer_zerop (wr_f_m1));
15464 ASSERT_TRUE (integer_zerop (c_i_0));
15465 ASSERT_FALSE (integer_zerop (c_i_1));
15466 ASSERT_FALSE (integer_zerop (c_i_m1));
15467 ASSERT_FALSE (integer_zerop (c_f_0));
15468 ASSERT_FALSE (integer_zerop (c_f_1));
15469 ASSERT_FALSE (integer_zerop (c_f_m1));
15470
15471 /* Test integer_all_onesp. */
15472 ASSERT_FALSE (integer_all_onesp (i_0));
15473 ASSERT_FALSE (integer_all_onesp (wr_i_0));
15474 ASSERT_FALSE (integer_all_onesp (i_1));
15475 ASSERT_FALSE (integer_all_onesp (wr_i_1));
15476 ASSERT_TRUE (integer_all_onesp (i_m1));
15477 ASSERT_TRUE (integer_all_onesp (wr_i_m1));
15478 ASSERT_FALSE (integer_all_onesp (f_0));
15479 ASSERT_FALSE (integer_all_onesp (wr_f_0));
15480 ASSERT_FALSE (integer_all_onesp (f_1));
15481 ASSERT_FALSE (integer_all_onesp (wr_f_1));
15482 ASSERT_FALSE (integer_all_onesp (f_m1));
15483 ASSERT_FALSE (integer_all_onesp (wr_f_m1));
15484 ASSERT_FALSE (integer_all_onesp (c_i_0));
15485 ASSERT_FALSE (integer_all_onesp (c_i_1));
15486 ASSERT_FALSE (integer_all_onesp (c_i_m1));
15487 ASSERT_FALSE (integer_all_onesp (c_f_0));
15488 ASSERT_FALSE (integer_all_onesp (c_f_1));
15489 ASSERT_FALSE (integer_all_onesp (c_f_m1));
15490
15491 /* Test integer_minus_onep. */
15492 ASSERT_FALSE (integer_minus_onep (i_0));
15493 ASSERT_FALSE (integer_minus_onep (wr_i_0));
15494 ASSERT_FALSE (integer_minus_onep (i_1));
15495 ASSERT_FALSE (integer_minus_onep (wr_i_1));
15496 ASSERT_TRUE (integer_minus_onep (i_m1));
15497 ASSERT_TRUE (integer_minus_onep (wr_i_m1));
15498 ASSERT_FALSE (integer_minus_onep (f_0));
15499 ASSERT_FALSE (integer_minus_onep (wr_f_0));
15500 ASSERT_FALSE (integer_minus_onep (f_1));
15501 ASSERT_FALSE (integer_minus_onep (wr_f_1));
15502 ASSERT_FALSE (integer_minus_onep (f_m1));
15503 ASSERT_FALSE (integer_minus_onep (wr_f_m1));
15504 ASSERT_FALSE (integer_minus_onep (c_i_0));
15505 ASSERT_FALSE (integer_minus_onep (c_i_1));
15506 ASSERT_TRUE (integer_minus_onep (c_i_m1));
15507 ASSERT_FALSE (integer_minus_onep (c_f_0));
15508 ASSERT_FALSE (integer_minus_onep (c_f_1));
15509 ASSERT_FALSE (integer_minus_onep (c_f_m1));
15510
15511 /* Test integer_each_onep. */
15512 ASSERT_FALSE (integer_each_onep (i_0));
15513 ASSERT_FALSE (integer_each_onep (wr_i_0));
15514 ASSERT_TRUE (integer_each_onep (i_1));
15515 ASSERT_TRUE (integer_each_onep (wr_i_1));
15516 ASSERT_FALSE (integer_each_onep (i_m1));
15517 ASSERT_FALSE (integer_each_onep (wr_i_m1));
15518 ASSERT_FALSE (integer_each_onep (f_0));
15519 ASSERT_FALSE (integer_each_onep (wr_f_0));
15520 ASSERT_FALSE (integer_each_onep (f_1));
15521 ASSERT_FALSE (integer_each_onep (wr_f_1));
15522 ASSERT_FALSE (integer_each_onep (f_m1));
15523 ASSERT_FALSE (integer_each_onep (wr_f_m1));
15524 ASSERT_FALSE (integer_each_onep (c_i_0));
15525 ASSERT_FALSE (integer_each_onep (c_i_1));
15526 ASSERT_FALSE (integer_each_onep (c_i_m1));
15527 ASSERT_FALSE (integer_each_onep (c_f_0));
15528 ASSERT_FALSE (integer_each_onep (c_f_1));
15529 ASSERT_FALSE (integer_each_onep (c_f_m1));
15530
15531 /* Test integer_truep. */
15532 ASSERT_FALSE (integer_truep (i_0));
15533 ASSERT_FALSE (integer_truep (wr_i_0));
15534 ASSERT_TRUE (integer_truep (i_1));
15535 ASSERT_TRUE (integer_truep (wr_i_1));
15536 ASSERT_FALSE (integer_truep (i_m1));
15537 ASSERT_FALSE (integer_truep (wr_i_m1));
15538 ASSERT_FALSE (integer_truep (f_0));
15539 ASSERT_FALSE (integer_truep (wr_f_0));
15540 ASSERT_FALSE (integer_truep (f_1));
15541 ASSERT_FALSE (integer_truep (wr_f_1));
15542 ASSERT_FALSE (integer_truep (f_m1));
15543 ASSERT_FALSE (integer_truep (wr_f_m1));
15544 ASSERT_FALSE (integer_truep (c_i_0));
15545 ASSERT_TRUE (integer_truep (c_i_1));
15546 ASSERT_FALSE (integer_truep (c_i_m1));
15547 ASSERT_FALSE (integer_truep (c_f_0));
15548 ASSERT_FALSE (integer_truep (c_f_1));
15549 ASSERT_FALSE (integer_truep (c_f_m1));
15550
15551 /* Test integer_nonzerop. */
15552 ASSERT_FALSE (integer_nonzerop (i_0));
15553 ASSERT_FALSE (integer_nonzerop (wr_i_0));
15554 ASSERT_TRUE (integer_nonzerop (i_1));
15555 ASSERT_TRUE (integer_nonzerop (wr_i_1));
15556 ASSERT_TRUE (integer_nonzerop (i_m1));
15557 ASSERT_TRUE (integer_nonzerop (wr_i_m1));
15558 ASSERT_FALSE (integer_nonzerop (f_0));
15559 ASSERT_FALSE (integer_nonzerop (wr_f_0));
15560 ASSERT_FALSE (integer_nonzerop (f_1));
15561 ASSERT_FALSE (integer_nonzerop (wr_f_1));
15562 ASSERT_FALSE (integer_nonzerop (f_m1));
15563 ASSERT_FALSE (integer_nonzerop (wr_f_m1));
15564 ASSERT_FALSE (integer_nonzerop (c_i_0));
15565 ASSERT_TRUE (integer_nonzerop (c_i_1));
15566 ASSERT_TRUE (integer_nonzerop (c_i_m1));
15567 ASSERT_FALSE (integer_nonzerop (c_f_0));
15568 ASSERT_FALSE (integer_nonzerop (c_f_1));
15569 ASSERT_FALSE (integer_nonzerop (c_f_m1));
15570
15571 /* Test real_zerop. */
15572 ASSERT_FALSE (real_zerop (i_0));
15573 ASSERT_FALSE (real_zerop (wr_i_0));
15574 ASSERT_FALSE (real_zerop (i_1));
15575 ASSERT_FALSE (real_zerop (wr_i_1));
15576 ASSERT_FALSE (real_zerop (i_m1));
15577 ASSERT_FALSE (real_zerop (wr_i_m1));
15578 ASSERT_TRUE (real_zerop (f_0));
15579 ASSERT_TRUE (real_zerop (wr_f_0));
15580 ASSERT_FALSE (real_zerop (f_1));
15581 ASSERT_FALSE (real_zerop (wr_f_1));
15582 ASSERT_FALSE (real_zerop (f_m1));
15583 ASSERT_FALSE (real_zerop (wr_f_m1));
15584 ASSERT_FALSE (real_zerop (c_i_0));
15585 ASSERT_FALSE (real_zerop (c_i_1));
15586 ASSERT_FALSE (real_zerop (c_i_m1));
15587 ASSERT_TRUE (real_zerop (c_f_0));
15588 ASSERT_FALSE (real_zerop (c_f_1));
15589 ASSERT_FALSE (real_zerop (c_f_m1));
15590
15591 /* Test real_onep. */
15592 ASSERT_FALSE (real_onep (i_0));
15593 ASSERT_FALSE (real_onep (wr_i_0));
15594 ASSERT_FALSE (real_onep (i_1));
15595 ASSERT_FALSE (real_onep (wr_i_1));
15596 ASSERT_FALSE (real_onep (i_m1));
15597 ASSERT_FALSE (real_onep (wr_i_m1));
15598 ASSERT_FALSE (real_onep (f_0));
15599 ASSERT_FALSE (real_onep (wr_f_0));
15600 ASSERT_TRUE (real_onep (f_1));
15601 ASSERT_TRUE (real_onep (wr_f_1));
15602 ASSERT_FALSE (real_onep (f_m1));
15603 ASSERT_FALSE (real_onep (wr_f_m1));
15604 ASSERT_FALSE (real_onep (c_i_0));
15605 ASSERT_FALSE (real_onep (c_i_1));
15606 ASSERT_FALSE (real_onep (c_i_m1));
15607 ASSERT_FALSE (real_onep (c_f_0));
15608 ASSERT_TRUE (real_onep (c_f_1));
15609 ASSERT_FALSE (real_onep (c_f_m1));
15610
15611 /* Test real_minus_onep. */
15612 ASSERT_FALSE (real_minus_onep (i_0));
15613 ASSERT_FALSE (real_minus_onep (wr_i_0));
15614 ASSERT_FALSE (real_minus_onep (i_1));
15615 ASSERT_FALSE (real_minus_onep (wr_i_1));
15616 ASSERT_FALSE (real_minus_onep (i_m1));
15617 ASSERT_FALSE (real_minus_onep (wr_i_m1));
15618 ASSERT_FALSE (real_minus_onep (f_0));
15619 ASSERT_FALSE (real_minus_onep (wr_f_0));
15620 ASSERT_FALSE (real_minus_onep (f_1));
15621 ASSERT_FALSE (real_minus_onep (wr_f_1));
15622 ASSERT_TRUE (real_minus_onep (f_m1));
15623 ASSERT_TRUE (real_minus_onep (wr_f_m1));
15624 ASSERT_FALSE (real_minus_onep (c_i_0));
15625 ASSERT_FALSE (real_minus_onep (c_i_1));
15626 ASSERT_FALSE (real_minus_onep (c_i_m1));
15627 ASSERT_FALSE (real_minus_onep (c_f_0));
15628 ASSERT_FALSE (real_minus_onep (c_f_1));
15629 ASSERT_TRUE (real_minus_onep (c_f_m1));
15630
15631 /* Test zerop. */
15632 ASSERT_TRUE (zerop (i_0));
15633 ASSERT_TRUE (zerop (wr_i_0));
15634 ASSERT_FALSE (zerop (i_1));
15635 ASSERT_FALSE (zerop (wr_i_1));
15636 ASSERT_FALSE (zerop (i_m1));
15637 ASSERT_FALSE (zerop (wr_i_m1));
15638 ASSERT_TRUE (zerop (f_0));
15639 ASSERT_TRUE (zerop (wr_f_0));
15640 ASSERT_FALSE (zerop (f_1));
15641 ASSERT_FALSE (zerop (wr_f_1));
15642 ASSERT_FALSE (zerop (f_m1));
15643 ASSERT_FALSE (zerop (wr_f_m1));
15644 ASSERT_TRUE (zerop (c_i_0));
15645 ASSERT_FALSE (zerop (c_i_1));
15646 ASSERT_FALSE (zerop (c_i_m1));
15647 ASSERT_TRUE (zerop (c_f_0));
15648 ASSERT_FALSE (zerop (c_f_1));
15649 ASSERT_FALSE (zerop (c_f_m1));
15650
15651 /* Test tree_expr_nonnegative_p. */
15652 ASSERT_TRUE (tree_expr_nonnegative_p (i_0));
15653 ASSERT_TRUE (tree_expr_nonnegative_p (wr_i_0));
15654 ASSERT_TRUE (tree_expr_nonnegative_p (i_1));
15655 ASSERT_TRUE (tree_expr_nonnegative_p (wr_i_1));
15656 ASSERT_FALSE (tree_expr_nonnegative_p (i_m1));
15657 ASSERT_FALSE (tree_expr_nonnegative_p (wr_i_m1));
15658 ASSERT_TRUE (tree_expr_nonnegative_p (f_0));
15659 ASSERT_TRUE (tree_expr_nonnegative_p (wr_f_0));
15660 ASSERT_TRUE (tree_expr_nonnegative_p (f_1));
15661 ASSERT_TRUE (tree_expr_nonnegative_p (wr_f_1));
15662 ASSERT_FALSE (tree_expr_nonnegative_p (f_m1));
15663 ASSERT_FALSE (tree_expr_nonnegative_p (wr_f_m1));
15664 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_0));
15665 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_1));
15666 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_m1));
15667 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_0));
15668 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_1));
15669 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_m1));
15670
15671 /* Test tree_expr_nonzero_p. */
15672 ASSERT_FALSE (tree_expr_nonzero_p (i_0));
15673 ASSERT_FALSE (tree_expr_nonzero_p (wr_i_0));
15674 ASSERT_TRUE (tree_expr_nonzero_p (i_1));
15675 ASSERT_TRUE (tree_expr_nonzero_p (wr_i_1));
15676 ASSERT_TRUE (tree_expr_nonzero_p (i_m1));
15677 ASSERT_TRUE (tree_expr_nonzero_p (wr_i_m1));
15678
15679 /* Test integer_valued_real_p. */
15680 ASSERT_FALSE (integer_valued_real_p (i_0));
15681 ASSERT_TRUE (integer_valued_real_p (f_0));
15682 ASSERT_TRUE (integer_valued_real_p (wr_f_0));
15683 ASSERT_TRUE (integer_valued_real_p (f_1));
15684 ASSERT_TRUE (integer_valued_real_p (wr_f_1));
15685
15686 /* Test integer_pow2p. */
15687 ASSERT_FALSE (integer_pow2p (i_0));
15688 ASSERT_TRUE (integer_pow2p (i_1));
15689 ASSERT_TRUE (integer_pow2p (wr_i_1));
15690
15691 /* Test uniform_integer_cst_p. */
15692 ASSERT_TRUE (uniform_integer_cst_p (i_0));
15693 ASSERT_TRUE (uniform_integer_cst_p (wr_i_0));
15694 ASSERT_TRUE (uniform_integer_cst_p (i_1));
15695 ASSERT_TRUE (uniform_integer_cst_p (wr_i_1));
15696 ASSERT_TRUE (uniform_integer_cst_p (i_m1));
15697 ASSERT_TRUE (uniform_integer_cst_p (wr_i_m1));
15698 ASSERT_FALSE (uniform_integer_cst_p (f_0));
15699 ASSERT_FALSE (uniform_integer_cst_p (wr_f_0));
15700 ASSERT_FALSE (uniform_integer_cst_p (f_1));
15701 ASSERT_FALSE (uniform_integer_cst_p (wr_f_1));
15702 ASSERT_FALSE (uniform_integer_cst_p (f_m1));
15703 ASSERT_FALSE (uniform_integer_cst_p (wr_f_m1));
15704 ASSERT_FALSE (uniform_integer_cst_p (c_i_0));
15705 ASSERT_FALSE (uniform_integer_cst_p (c_i_1));
15706 ASSERT_FALSE (uniform_integer_cst_p (c_i_m1));
15707 ASSERT_FALSE (uniform_integer_cst_p (c_f_0));
15708 ASSERT_FALSE (uniform_integer_cst_p (c_f_1));
15709 ASSERT_FALSE (uniform_integer_cst_p (c_f_m1));
15710}
15711
15712/* Check that string escaping works correctly. */
15713
15714static void
15715test_escaped_strings (void)
15716{
15717 int saved_cutoff;
15718 escaped_string msg;
15719
15720 msg.escape (NULL);
15721 /* ASSERT_STREQ does not accept NULL as a valid test
15722 result, so we have to use ASSERT_EQ instead. */
15723 ASSERT_EQ (NULL, (const char *) msg);
15724
15725 msg.escape (unescaped: "");
15726 ASSERT_STREQ ("", (const char *) msg);
15727
15728 msg.escape (unescaped: "foobar");
15729 ASSERT_STREQ ("foobar", (const char *) msg);
15730
15731 /* Ensure that we have -fmessage-length set to 0. */
15732 saved_cutoff = pp_line_cutoff (global_dc->printer);
15733 pp_line_cutoff (global_dc->printer) = 0;
15734
15735 msg.escape (unescaped: "foo\nbar");
15736 ASSERT_STREQ ("foo\\nbar", (const char *) msg);
15737
15738 msg.escape (unescaped: "\a\b\f\n\r\t\v");
15739 ASSERT_STREQ ("\\a\\b\\f\\n\\r\\t\\v", (const char *) msg);
15740
15741 /* Now repeat the tests with -fmessage-length set to 5. */
15742 pp_line_cutoff (global_dc->printer) = 5;
15743
15744 /* Note that the newline is not translated into an escape. */
15745 msg.escape (unescaped: "foo\nbar");
15746 ASSERT_STREQ ("foo\nbar", (const char *) msg);
15747
15748 msg.escape (unescaped: "\a\b\f\n\r\t\v");
15749 ASSERT_STREQ ("\\a\\b\\f\n\\r\\t\\v", (const char *) msg);
15750
15751 /* Restore the original message length setting. */
15752 pp_line_cutoff (global_dc->printer) = saved_cutoff;
15753}
15754
15755/* Run all of the selftests within this file. */
15756
15757void
15758tree_cc_tests ()
15759{
15760 test_integer_constants ();
15761 test_identifiers ();
15762 test_labels ();
15763 test_vector_cst_patterns ();
15764 test_location_wrappers ();
15765 test_predicates ();
15766 test_escaped_strings ();
15767}
15768
15769} // namespace selftest
15770
15771#endif /* CHECKING_P */
15772
15773#include "gt-tree.h"
15774

source code of gcc/tree.cc