]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree.c
PR100281 C++: Fix SImode pointer handling
[thirdparty/gcc.git] / gcc / tree.c
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2021 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
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
72 /* Tree code classes. */
73
74 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
75 #define END_OF_BASE_TREE_CODES tcc_exceptional,
76
77 const enum tree_code_class tree_code_type[] = {
78 #include "all-tree.def"
79 };
80
81 #undef DEFTREECODE
82 #undef END_OF_BASE_TREE_CODES
83
84 /* Table indexed by tree code giving number of expression
85 operands beyond the fixed part of the node structure.
86 Not used for types or decls. */
87
88 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
89 #define END_OF_BASE_TREE_CODES 0,
90
91 const unsigned char tree_code_length[] = {
92 #include "all-tree.def"
93 };
94
95 #undef DEFTREECODE
96 #undef END_OF_BASE_TREE_CODES
97
98 /* Names of tree components.
99 Used for printing out the tree and error messages. */
100 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
101 #define END_OF_BASE_TREE_CODES "@dummy",
102
103 static const char *const tree_code_name[] = {
104 #include "all-tree.def"
105 };
106
107 #undef DEFTREECODE
108 #undef END_OF_BASE_TREE_CODES
109
110 /* Each tree code class has an associated string representation.
111 These must correspond to the tree_code_class entries. */
112
113 const char *const tree_code_class_strings[] =
114 {
115 "exceptional",
116 "constant",
117 "type",
118 "declaration",
119 "reference",
120 "comparison",
121 "unary",
122 "binary",
123 "statement",
124 "vl_exp",
125 "expression"
126 };
127
128 /* obstack.[ch] explicitly declined to prototype this. */
129 extern int _obstack_allocated_p (struct obstack *h, void *obj);
130
131 /* Statistics-gathering stuff. */
132
133 static uint64_t tree_code_counts[MAX_TREE_CODES];
134 uint64_t tree_node_counts[(int) all_kinds];
135 uint64_t tree_node_sizes[(int) all_kinds];
136
137 /* Keep in sync with tree.h:enum tree_node_kind. */
138 static const char * const tree_node_kind_names[] = {
139 "decls",
140 "types",
141 "blocks",
142 "stmts",
143 "refs",
144 "exprs",
145 "constants",
146 "identifiers",
147 "vecs",
148 "binfos",
149 "ssa names",
150 "constructors",
151 "random kinds",
152 "lang_decl kinds",
153 "lang_type kinds",
154 "omp clauses",
155 };
156
157 /* Unique id for next decl created. */
158 static GTY(()) int next_decl_uid;
159 /* Unique id for next type created. */
160 static GTY(()) unsigned next_type_uid = 1;
161 /* Unique id for next debug decl created. Use negative numbers,
162 to catch erroneous uses. */
163 static GTY(()) int next_debug_decl_uid;
164
165 /* Since we cannot rehash a type after it is in the table, we have to
166 keep the hash code. */
167
168 struct GTY((for_user)) type_hash {
169 unsigned long hash;
170 tree type;
171 };
172
173 /* Initial size of the hash table (rounded to next prime). */
174 #define TYPE_HASH_INITIAL_SIZE 1000
175
176 struct type_cache_hasher : ggc_cache_ptr_hash<type_hash>
177 {
178 static hashval_t hash (type_hash *t) { return t->hash; }
179 static bool equal (type_hash *a, type_hash *b);
180
181 static int
182 keep_cache_entry (type_hash *&t)
183 {
184 return ggc_marked_p (t->type);
185 }
186 };
187
188 /* Now here is the hash table. When recording a type, it is added to
189 the slot whose index is the hash code. Note that the hash table is
190 used for several kinds of types (function types, array types and
191 array index range types, for now). While all these live in the
192 same table, they are completely independent, and the hash code is
193 computed differently for each of these. */
194
195 static GTY ((cache)) hash_table<type_cache_hasher> *type_hash_table;
196
197 /* Hash table and temporary node for larger integer const values. */
198 static GTY (()) tree int_cst_node;
199
200 struct int_cst_hasher : ggc_cache_ptr_hash<tree_node>
201 {
202 static hashval_t hash (tree t);
203 static bool equal (tree x, tree y);
204 };
205
206 static GTY ((cache)) hash_table<int_cst_hasher> *int_cst_hash_table;
207
208 /* Class and variable for making sure that there is a single POLY_INT_CST
209 for a given value. */
210 struct poly_int_cst_hasher : ggc_cache_ptr_hash<tree_node>
211 {
212 typedef std::pair<tree, const poly_wide_int *> compare_type;
213 static hashval_t hash (tree t);
214 static bool equal (tree x, const compare_type &y);
215 };
216
217 static GTY ((cache)) hash_table<poly_int_cst_hasher> *poly_int_cst_hash_table;
218
219 /* Hash table for optimization flags and target option flags. Use the same
220 hash table for both sets of options. Nodes for building the current
221 optimization and target option nodes. The assumption is most of the time
222 the options created will already be in the hash table, so we avoid
223 allocating and freeing up a node repeatably. */
224 static GTY (()) tree cl_optimization_node;
225 static GTY (()) tree cl_target_option_node;
226
227 struct cl_option_hasher : ggc_cache_ptr_hash<tree_node>
228 {
229 static hashval_t hash (tree t);
230 static bool equal (tree x, tree y);
231 };
232
233 static GTY ((cache)) hash_table<cl_option_hasher> *cl_option_hash_table;
234
235 /* General tree->tree mapping structure for use in hash tables. */
236
237
238 static GTY ((cache))
239 hash_table<tree_decl_map_cache_hasher> *debug_expr_for_decl;
240
241 static GTY ((cache))
242 hash_table<tree_decl_map_cache_hasher> *value_expr_for_decl;
243
244 struct tree_vec_map_cache_hasher : ggc_cache_ptr_hash<tree_vec_map>
245 {
246 static hashval_t hash (tree_vec_map *m) { return DECL_UID (m->base.from); }
247
248 static bool
249 equal (tree_vec_map *a, tree_vec_map *b)
250 {
251 return a->base.from == b->base.from;
252 }
253
254 static int
255 keep_cache_entry (tree_vec_map *&m)
256 {
257 return ggc_marked_p (m->base.from);
258 }
259 };
260
261 static GTY ((cache))
262 hash_table<tree_vec_map_cache_hasher> *debug_args_for_decl;
263
264 static void set_type_quals (tree, int);
265 static void print_type_hash_statistics (void);
266 static void print_debug_expr_statistics (void);
267 static void print_value_expr_statistics (void);
268
269 tree global_trees[TI_MAX];
270 tree integer_types[itk_none];
271
272 bool int_n_enabled_p[NUM_INT_N_ENTS];
273 struct int_n_trees_t int_n_trees [NUM_INT_N_ENTS];
274
275 bool tree_contains_struct[MAX_TREE_CODES][64];
276
277 /* Number of operands for each OpenMP clause. */
278 unsigned const char omp_clause_num_ops[] =
279 {
280 0, /* OMP_CLAUSE_ERROR */
281 1, /* OMP_CLAUSE_PRIVATE */
282 1, /* OMP_CLAUSE_SHARED */
283 1, /* OMP_CLAUSE_FIRSTPRIVATE */
284 2, /* OMP_CLAUSE_LASTPRIVATE */
285 5, /* OMP_CLAUSE_REDUCTION */
286 5, /* OMP_CLAUSE_TASK_REDUCTION */
287 5, /* OMP_CLAUSE_IN_REDUCTION */
288 1, /* OMP_CLAUSE_COPYIN */
289 1, /* OMP_CLAUSE_COPYPRIVATE */
290 3, /* OMP_CLAUSE_LINEAR */
291 2, /* OMP_CLAUSE_ALIGNED */
292 2, /* OMP_CLAUSE_ALLOCATE */
293 1, /* OMP_CLAUSE_DEPEND */
294 1, /* OMP_CLAUSE_NONTEMPORAL */
295 1, /* OMP_CLAUSE_UNIFORM */
296 1, /* OMP_CLAUSE_TO_DECLARE */
297 1, /* OMP_CLAUSE_LINK */
298 1, /* OMP_CLAUSE_DETACH */
299 1, /* OMP_CLAUSE_USE_DEVICE_PTR */
300 1, /* OMP_CLAUSE_USE_DEVICE_ADDR */
301 1, /* OMP_CLAUSE_IS_DEVICE_PTR */
302 1, /* OMP_CLAUSE_INCLUSIVE */
303 1, /* OMP_CLAUSE_EXCLUSIVE */
304 2, /* OMP_CLAUSE_FROM */
305 2, /* OMP_CLAUSE_TO */
306 2, /* OMP_CLAUSE_MAP */
307 2, /* OMP_CLAUSE__CACHE_ */
308 2, /* OMP_CLAUSE_GANG */
309 1, /* OMP_CLAUSE_ASYNC */
310 1, /* OMP_CLAUSE_WAIT */
311 0, /* OMP_CLAUSE_AUTO */
312 0, /* OMP_CLAUSE_SEQ */
313 1, /* OMP_CLAUSE__LOOPTEMP_ */
314 1, /* OMP_CLAUSE__REDUCTEMP_ */
315 1, /* OMP_CLAUSE__CONDTEMP_ */
316 1, /* OMP_CLAUSE__SCANTEMP_ */
317 1, /* OMP_CLAUSE_IF */
318 1, /* OMP_CLAUSE_NUM_THREADS */
319 1, /* OMP_CLAUSE_SCHEDULE */
320 0, /* OMP_CLAUSE_NOWAIT */
321 1, /* OMP_CLAUSE_ORDERED */
322 0, /* OMP_CLAUSE_DEFAULT */
323 3, /* OMP_CLAUSE_COLLAPSE */
324 0, /* OMP_CLAUSE_UNTIED */
325 1, /* OMP_CLAUSE_FINAL */
326 0, /* OMP_CLAUSE_MERGEABLE */
327 1, /* OMP_CLAUSE_DEVICE */
328 1, /* OMP_CLAUSE_DIST_SCHEDULE */
329 0, /* OMP_CLAUSE_INBRANCH */
330 0, /* OMP_CLAUSE_NOTINBRANCH */
331 1, /* OMP_CLAUSE_NUM_TEAMS */
332 1, /* OMP_CLAUSE_THREAD_LIMIT */
333 0, /* OMP_CLAUSE_PROC_BIND */
334 1, /* OMP_CLAUSE_SAFELEN */
335 1, /* OMP_CLAUSE_SIMDLEN */
336 0, /* OMP_CLAUSE_DEVICE_TYPE */
337 0, /* OMP_CLAUSE_FOR */
338 0, /* OMP_CLAUSE_PARALLEL */
339 0, /* OMP_CLAUSE_SECTIONS */
340 0, /* OMP_CLAUSE_TASKGROUP */
341 1, /* OMP_CLAUSE_PRIORITY */
342 1, /* OMP_CLAUSE_GRAINSIZE */
343 1, /* OMP_CLAUSE_NUM_TASKS */
344 0, /* OMP_CLAUSE_NOGROUP */
345 0, /* OMP_CLAUSE_THREADS */
346 0, /* OMP_CLAUSE_SIMD */
347 1, /* OMP_CLAUSE_HINT */
348 0, /* OMP_CLAUSE_DEFAULTMAP */
349 0, /* OMP_CLAUSE_ORDER */
350 0, /* OMP_CLAUSE_BIND */
351 1, /* OMP_CLAUSE__SIMDUID_ */
352 0, /* OMP_CLAUSE__SIMT_ */
353 0, /* OMP_CLAUSE_INDEPENDENT */
354 1, /* OMP_CLAUSE_WORKER */
355 1, /* OMP_CLAUSE_VECTOR */
356 1, /* OMP_CLAUSE_NUM_GANGS */
357 1, /* OMP_CLAUSE_NUM_WORKERS */
358 1, /* OMP_CLAUSE_VECTOR_LENGTH */
359 3, /* OMP_CLAUSE_TILE */
360 0, /* OMP_CLAUSE_IF_PRESENT */
361 0, /* OMP_CLAUSE_FINALIZE */
362 };
363
364 const char * const omp_clause_code_name[] =
365 {
366 "error_clause",
367 "private",
368 "shared",
369 "firstprivate",
370 "lastprivate",
371 "reduction",
372 "task_reduction",
373 "in_reduction",
374 "copyin",
375 "copyprivate",
376 "linear",
377 "aligned",
378 "allocate",
379 "depend",
380 "nontemporal",
381 "uniform",
382 "to",
383 "link",
384 "detach",
385 "use_device_ptr",
386 "use_device_addr",
387 "is_device_ptr",
388 "inclusive",
389 "exclusive",
390 "from",
391 "to",
392 "map",
393 "_cache_",
394 "gang",
395 "async",
396 "wait",
397 "auto",
398 "seq",
399 "_looptemp_",
400 "_reductemp_",
401 "_condtemp_",
402 "_scantemp_",
403 "if",
404 "num_threads",
405 "schedule",
406 "nowait",
407 "ordered",
408 "default",
409 "collapse",
410 "untied",
411 "final",
412 "mergeable",
413 "device",
414 "dist_schedule",
415 "inbranch",
416 "notinbranch",
417 "num_teams",
418 "thread_limit",
419 "proc_bind",
420 "safelen",
421 "simdlen",
422 "device_type",
423 "for",
424 "parallel",
425 "sections",
426 "taskgroup",
427 "priority",
428 "grainsize",
429 "num_tasks",
430 "nogroup",
431 "threads",
432 "simd",
433 "hint",
434 "defaultmap",
435 "order",
436 "bind",
437 "_simduid_",
438 "_simt_",
439 "independent",
440 "worker",
441 "vector",
442 "num_gangs",
443 "num_workers",
444 "vector_length",
445 "tile",
446 "if_present",
447 "finalize",
448 };
449
450
451 /* Return the tree node structure used by tree code CODE. */
452
453 static inline enum tree_node_structure_enum
454 tree_node_structure_for_code (enum tree_code code)
455 {
456 switch (TREE_CODE_CLASS (code))
457 {
458 case tcc_declaration:
459 switch (code)
460 {
461 case CONST_DECL: return TS_CONST_DECL;
462 case DEBUG_EXPR_DECL: return TS_DECL_WRTL;
463 case FIELD_DECL: return TS_FIELD_DECL;
464 case FUNCTION_DECL: return TS_FUNCTION_DECL;
465 case LABEL_DECL: return TS_LABEL_DECL;
466 case PARM_DECL: return TS_PARM_DECL;
467 case RESULT_DECL: return TS_RESULT_DECL;
468 case TRANSLATION_UNIT_DECL: return TS_TRANSLATION_UNIT_DECL;
469 case TYPE_DECL: return TS_TYPE_DECL;
470 case VAR_DECL: return TS_VAR_DECL;
471 default: return TS_DECL_NON_COMMON;
472 }
473
474 case tcc_type: return TS_TYPE_NON_COMMON;
475
476 case tcc_binary:
477 case tcc_comparison:
478 case tcc_expression:
479 case tcc_reference:
480 case tcc_statement:
481 case tcc_unary:
482 case tcc_vl_exp: return TS_EXP;
483
484 default: /* tcc_constant and tcc_exceptional */
485 break;
486 }
487
488 switch (code)
489 {
490 /* tcc_constant cases. */
491 case COMPLEX_CST: return TS_COMPLEX;
492 case FIXED_CST: return TS_FIXED_CST;
493 case INTEGER_CST: return TS_INT_CST;
494 case POLY_INT_CST: return TS_POLY_INT_CST;
495 case REAL_CST: return TS_REAL_CST;
496 case STRING_CST: return TS_STRING;
497 case VECTOR_CST: return TS_VECTOR;
498 case VOID_CST: return TS_TYPED;
499
500 /* tcc_exceptional cases. */
501 case BLOCK: return TS_BLOCK;
502 case CONSTRUCTOR: return TS_CONSTRUCTOR;
503 case ERROR_MARK: return TS_COMMON;
504 case IDENTIFIER_NODE: return TS_IDENTIFIER;
505 case OMP_CLAUSE: return TS_OMP_CLAUSE;
506 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
507 case PLACEHOLDER_EXPR: return TS_COMMON;
508 case SSA_NAME: return TS_SSA_NAME;
509 case STATEMENT_LIST: return TS_STATEMENT_LIST;
510 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
511 case TREE_BINFO: return TS_BINFO;
512 case TREE_LIST: return TS_LIST;
513 case TREE_VEC: return TS_VEC;
514
515 default:
516 gcc_unreachable ();
517 }
518 }
519
520
521 /* Initialize tree_contains_struct to describe the hierarchy of tree
522 nodes. */
523
524 static void
525 initialize_tree_contains_struct (void)
526 {
527 unsigned i;
528
529 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
530 {
531 enum tree_code code;
532 enum tree_node_structure_enum ts_code;
533
534 code = (enum tree_code) i;
535 ts_code = tree_node_structure_for_code (code);
536
537 /* Mark the TS structure itself. */
538 tree_contains_struct[code][ts_code] = 1;
539
540 /* Mark all the structures that TS is derived from. */
541 switch (ts_code)
542 {
543 case TS_TYPED:
544 case TS_BLOCK:
545 case TS_OPTIMIZATION:
546 case TS_TARGET_OPTION:
547 MARK_TS_BASE (code);
548 break;
549
550 case TS_COMMON:
551 case TS_INT_CST:
552 case TS_POLY_INT_CST:
553 case TS_REAL_CST:
554 case TS_FIXED_CST:
555 case TS_VECTOR:
556 case TS_STRING:
557 case TS_COMPLEX:
558 case TS_SSA_NAME:
559 case TS_CONSTRUCTOR:
560 case TS_EXP:
561 case TS_STATEMENT_LIST:
562 MARK_TS_TYPED (code);
563 break;
564
565 case TS_IDENTIFIER:
566 case TS_DECL_MINIMAL:
567 case TS_TYPE_COMMON:
568 case TS_LIST:
569 case TS_VEC:
570 case TS_BINFO:
571 case TS_OMP_CLAUSE:
572 MARK_TS_COMMON (code);
573 break;
574
575 case TS_TYPE_WITH_LANG_SPECIFIC:
576 MARK_TS_TYPE_COMMON (code);
577 break;
578
579 case TS_TYPE_NON_COMMON:
580 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
581 break;
582
583 case TS_DECL_COMMON:
584 MARK_TS_DECL_MINIMAL (code);
585 break;
586
587 case TS_DECL_WRTL:
588 case TS_CONST_DECL:
589 MARK_TS_DECL_COMMON (code);
590 break;
591
592 case TS_DECL_NON_COMMON:
593 MARK_TS_DECL_WITH_VIS (code);
594 break;
595
596 case TS_DECL_WITH_VIS:
597 case TS_PARM_DECL:
598 case TS_LABEL_DECL:
599 case TS_RESULT_DECL:
600 MARK_TS_DECL_WRTL (code);
601 break;
602
603 case TS_FIELD_DECL:
604 MARK_TS_DECL_COMMON (code);
605 break;
606
607 case TS_VAR_DECL:
608 MARK_TS_DECL_WITH_VIS (code);
609 break;
610
611 case TS_TYPE_DECL:
612 case TS_FUNCTION_DECL:
613 MARK_TS_DECL_NON_COMMON (code);
614 break;
615
616 case TS_TRANSLATION_UNIT_DECL:
617 MARK_TS_DECL_COMMON (code);
618 break;
619
620 default:
621 gcc_unreachable ();
622 }
623 }
624
625 /* Basic consistency checks for attributes used in fold. */
626 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
627 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
628 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
629 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
630 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
631 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
632 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
633 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
634 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
635 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
636 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
637 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
638 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
639 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
640 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
641 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
642 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
643 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
644 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
645 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
646 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
647 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
648 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
649 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
650 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
651 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
652 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
653 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
654 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
655 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
656 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
657 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
658 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
659 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
660 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
661 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
662 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
663 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
664 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_MINIMAL]);
665 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_COMMON]);
666 }
667
668
669 /* Init tree.c. */
670
671 void
672 init_ttree (void)
673 {
674 /* Initialize the hash table of types. */
675 type_hash_table
676 = hash_table<type_cache_hasher>::create_ggc (TYPE_HASH_INITIAL_SIZE);
677
678 debug_expr_for_decl
679 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
680
681 value_expr_for_decl
682 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
683
684 int_cst_hash_table = hash_table<int_cst_hasher>::create_ggc (1024);
685
686 poly_int_cst_hash_table = hash_table<poly_int_cst_hasher>::create_ggc (64);
687
688 int_cst_node = make_int_cst (1, 1);
689
690 cl_option_hash_table = hash_table<cl_option_hasher>::create_ggc (64);
691
692 cl_optimization_node = make_node (OPTIMIZATION_NODE);
693 cl_target_option_node = make_node (TARGET_OPTION_NODE);
694
695 /* Initialize the tree_contains_struct array. */
696 initialize_tree_contains_struct ();
697 lang_hooks.init_ts ();
698 }
699
700 \f
701 /* The name of the object as the assembler will see it (but before any
702 translations made by ASM_OUTPUT_LABELREF). Often this is the same
703 as DECL_NAME. It is an IDENTIFIER_NODE. */
704 tree
705 decl_assembler_name (tree decl)
706 {
707 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
708 lang_hooks.set_decl_assembler_name (decl);
709 return DECL_ASSEMBLER_NAME_RAW (decl);
710 }
711
712 /* The DECL_ASSEMBLER_NAME_RAW of DECL is being explicitly set to NAME
713 (either of which may be NULL). Inform the FE, if this changes the
714 name. */
715
716 void
717 overwrite_decl_assembler_name (tree decl, tree name)
718 {
719 if (DECL_ASSEMBLER_NAME_RAW (decl) != name)
720 lang_hooks.overwrite_decl_assembler_name (decl, name);
721 }
722
723 /* Return true if DECL may need an assembler name to be set. */
724
725 static inline bool
726 need_assembler_name_p (tree decl)
727 {
728 /* We use DECL_ASSEMBLER_NAME to hold mangled type names for One Definition
729 Rule merging. This makes type_odr_p to return true on those types during
730 LTO and by comparing the mangled name, we can say what types are intended
731 to be equivalent across compilation unit.
732
733 We do not store names of type_in_anonymous_namespace_p.
734
735 Record, union and enumeration type have linkage that allows use
736 to check type_in_anonymous_namespace_p. We do not mangle compound types
737 that always can be compared structurally.
738
739 Similarly for builtin types, we compare properties of their main variant.
740 A special case are integer types where mangling do make differences
741 between char/signed char/unsigned char etc. Storing name for these makes
742 e.g. -fno-signed-char/-fsigned-char mismatches to be handled well.
743 See cp/mangle.c:write_builtin_type for details. */
744
745 if (TREE_CODE (decl) == TYPE_DECL)
746 {
747 if (DECL_NAME (decl)
748 && decl == TYPE_NAME (TREE_TYPE (decl))
749 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TREE_TYPE (decl)
750 && !TYPE_ARTIFICIAL (TREE_TYPE (decl))
751 && ((TREE_CODE (TREE_TYPE (decl)) != RECORD_TYPE
752 && TREE_CODE (TREE_TYPE (decl)) != UNION_TYPE)
753 || TYPE_CXX_ODR_P (TREE_TYPE (decl)))
754 && (type_with_linkage_p (TREE_TYPE (decl))
755 || TREE_CODE (TREE_TYPE (decl)) == INTEGER_TYPE)
756 && !variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
757 return !DECL_ASSEMBLER_NAME_SET_P (decl);
758 return false;
759 }
760 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
761 if (!VAR_OR_FUNCTION_DECL_P (decl))
762 return false;
763
764 /* If DECL already has its assembler name set, it does not need a
765 new one. */
766 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
767 || DECL_ASSEMBLER_NAME_SET_P (decl))
768 return false;
769
770 /* Abstract decls do not need an assembler name. */
771 if (DECL_ABSTRACT_P (decl))
772 return false;
773
774 /* For VAR_DECLs, only static, public and external symbols need an
775 assembler name. */
776 if (VAR_P (decl)
777 && !TREE_STATIC (decl)
778 && !TREE_PUBLIC (decl)
779 && !DECL_EXTERNAL (decl))
780 return false;
781
782 if (TREE_CODE (decl) == FUNCTION_DECL)
783 {
784 /* Do not set assembler name on builtins. Allow RTL expansion to
785 decide whether to expand inline or via a regular call. */
786 if (fndecl_built_in_p (decl)
787 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
788 return false;
789
790 /* Functions represented in the callgraph need an assembler name. */
791 if (cgraph_node::get (decl) != NULL)
792 return true;
793
794 /* Unused and not public functions don't need an assembler name. */
795 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
796 return false;
797 }
798
799 return true;
800 }
801
802 /* If T needs an assembler name, have one created for it. */
803
804 void
805 assign_assembler_name_if_needed (tree t)
806 {
807 if (need_assembler_name_p (t))
808 {
809 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
810 diagnostics that use input_location to show locus
811 information. The problem here is that, at this point,
812 input_location is generally anchored to the end of the file
813 (since the parser is long gone), so we don't have a good
814 position to pin it to.
815
816 To alleviate this problem, this uses the location of T's
817 declaration. Examples of this are
818 testsuite/g++.dg/template/cond2.C and
819 testsuite/g++.dg/template/pr35240.C. */
820 location_t saved_location = input_location;
821 input_location = DECL_SOURCE_LOCATION (t);
822
823 decl_assembler_name (t);
824
825 input_location = saved_location;
826 }
827 }
828
829 /* When the target supports COMDAT groups, this indicates which group the
830 DECL is associated with. This can be either an IDENTIFIER_NODE or a
831 decl, in which case its DECL_ASSEMBLER_NAME identifies the group. */
832 tree
833 decl_comdat_group (const_tree node)
834 {
835 struct symtab_node *snode = symtab_node::get (node);
836 if (!snode)
837 return NULL;
838 return snode->get_comdat_group ();
839 }
840
841 /* Likewise, but make sure it's been reduced to an IDENTIFIER_NODE. */
842 tree
843 decl_comdat_group_id (const_tree node)
844 {
845 struct symtab_node *snode = symtab_node::get (node);
846 if (!snode)
847 return NULL;
848 return snode->get_comdat_group_id ();
849 }
850
851 /* When the target supports named section, return its name as IDENTIFIER_NODE
852 or NULL if it is in no section. */
853 const char *
854 decl_section_name (const_tree node)
855 {
856 struct symtab_node *snode = symtab_node::get (node);
857 if (!snode)
858 return NULL;
859 return snode->get_section ();
860 }
861
862 /* Set section name of NODE to VALUE (that is expected to be
863 identifier node) */
864 void
865 set_decl_section_name (tree node, const char *value)
866 {
867 struct symtab_node *snode;
868
869 if (value == NULL)
870 {
871 snode = symtab_node::get (node);
872 if (!snode)
873 return;
874 }
875 else if (VAR_P (node))
876 snode = varpool_node::get_create (node);
877 else
878 snode = cgraph_node::get_create (node);
879 snode->set_section (value);
880 }
881
882 /* Set section name of NODE to match the section name of OTHER.
883
884 set_decl_section_name (decl, other) is equivalent to
885 set_decl_section_name (decl, DECL_SECTION_NAME (other)), but possibly more
886 efficient. */
887 void
888 set_decl_section_name (tree decl, const_tree other)
889 {
890 struct symtab_node *other_node = symtab_node::get (other);
891 if (other_node)
892 {
893 struct symtab_node *decl_node;
894 if (VAR_P (decl))
895 decl_node = varpool_node::get_create (decl);
896 else
897 decl_node = cgraph_node::get_create (decl);
898 decl_node->set_section (*other_node);
899 }
900 else
901 {
902 struct symtab_node *decl_node = symtab_node::get (decl);
903 if (!decl_node)
904 return;
905 decl_node->set_section (NULL);
906 }
907 }
908
909 /* Return TLS model of a variable NODE. */
910 enum tls_model
911 decl_tls_model (const_tree node)
912 {
913 struct varpool_node *snode = varpool_node::get (node);
914 if (!snode)
915 return TLS_MODEL_NONE;
916 return snode->tls_model;
917 }
918
919 /* Set TLS model of variable NODE to MODEL. */
920 void
921 set_decl_tls_model (tree node, enum tls_model model)
922 {
923 struct varpool_node *vnode;
924
925 if (model == TLS_MODEL_NONE)
926 {
927 vnode = varpool_node::get (node);
928 if (!vnode)
929 return;
930 }
931 else
932 vnode = varpool_node::get_create (node);
933 vnode->tls_model = model;
934 }
935
936 /* Compute the number of bytes occupied by a tree with code CODE.
937 This function cannot be used for nodes that have variable sizes,
938 including TREE_VEC, INTEGER_CST, STRING_CST, and CALL_EXPR. */
939 size_t
940 tree_code_size (enum tree_code code)
941 {
942 switch (TREE_CODE_CLASS (code))
943 {
944 case tcc_declaration: /* A decl node */
945 switch (code)
946 {
947 case FIELD_DECL: return sizeof (tree_field_decl);
948 case PARM_DECL: return sizeof (tree_parm_decl);
949 case VAR_DECL: return sizeof (tree_var_decl);
950 case LABEL_DECL: return sizeof (tree_label_decl);
951 case RESULT_DECL: return sizeof (tree_result_decl);
952 case CONST_DECL: return sizeof (tree_const_decl);
953 case TYPE_DECL: return sizeof (tree_type_decl);
954 case FUNCTION_DECL: return sizeof (tree_function_decl);
955 case DEBUG_EXPR_DECL: return sizeof (tree_decl_with_rtl);
956 case TRANSLATION_UNIT_DECL: return sizeof (tree_translation_unit_decl);
957 case NAMESPACE_DECL:
958 case IMPORTED_DECL:
959 case NAMELIST_DECL: return sizeof (tree_decl_non_common);
960 default:
961 gcc_checking_assert (code >= NUM_TREE_CODES);
962 return lang_hooks.tree_size (code);
963 }
964
965 case tcc_type: /* a type node */
966 switch (code)
967 {
968 case OFFSET_TYPE:
969 case ENUMERAL_TYPE:
970 case BOOLEAN_TYPE:
971 case INTEGER_TYPE:
972 case REAL_TYPE:
973 case OPAQUE_TYPE:
974 case POINTER_TYPE:
975 case REFERENCE_TYPE:
976 case NULLPTR_TYPE:
977 case FIXED_POINT_TYPE:
978 case COMPLEX_TYPE:
979 case VECTOR_TYPE:
980 case ARRAY_TYPE:
981 case RECORD_TYPE:
982 case UNION_TYPE:
983 case QUAL_UNION_TYPE:
984 case VOID_TYPE:
985 case FUNCTION_TYPE:
986 case METHOD_TYPE:
987 case LANG_TYPE: return sizeof (tree_type_non_common);
988 default:
989 gcc_checking_assert (code >= NUM_TREE_CODES);
990 return lang_hooks.tree_size (code);
991 }
992
993 case tcc_reference: /* a reference */
994 case tcc_expression: /* an expression */
995 case tcc_statement: /* an expression with side effects */
996 case tcc_comparison: /* a comparison expression */
997 case tcc_unary: /* a unary arithmetic expression */
998 case tcc_binary: /* a binary arithmetic expression */
999 return (sizeof (struct tree_exp)
1000 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
1001
1002 case tcc_constant: /* a constant */
1003 switch (code)
1004 {
1005 case VOID_CST: return sizeof (tree_typed);
1006 case INTEGER_CST: gcc_unreachable ();
1007 case POLY_INT_CST: return sizeof (tree_poly_int_cst);
1008 case REAL_CST: return sizeof (tree_real_cst);
1009 case FIXED_CST: return sizeof (tree_fixed_cst);
1010 case COMPLEX_CST: return sizeof (tree_complex);
1011 case VECTOR_CST: gcc_unreachable ();
1012 case STRING_CST: gcc_unreachable ();
1013 default:
1014 gcc_checking_assert (code >= NUM_TREE_CODES);
1015 return lang_hooks.tree_size (code);
1016 }
1017
1018 case tcc_exceptional: /* something random, like an identifier. */
1019 switch (code)
1020 {
1021 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
1022 case TREE_LIST: return sizeof (tree_list);
1023
1024 case ERROR_MARK:
1025 case PLACEHOLDER_EXPR: return sizeof (tree_common);
1026
1027 case TREE_VEC: gcc_unreachable ();
1028 case OMP_CLAUSE: gcc_unreachable ();
1029
1030 case SSA_NAME: return sizeof (tree_ssa_name);
1031
1032 case STATEMENT_LIST: return sizeof (tree_statement_list);
1033 case BLOCK: return sizeof (struct tree_block);
1034 case CONSTRUCTOR: return sizeof (tree_constructor);
1035 case OPTIMIZATION_NODE: return sizeof (tree_optimization_option);
1036 case TARGET_OPTION_NODE: return sizeof (tree_target_option);
1037
1038 default:
1039 gcc_checking_assert (code >= NUM_TREE_CODES);
1040 return lang_hooks.tree_size (code);
1041 }
1042
1043 default:
1044 gcc_unreachable ();
1045 }
1046 }
1047
1048 /* Compute the number of bytes occupied by NODE. This routine only
1049 looks at TREE_CODE, except for those nodes that have variable sizes. */
1050 size_t
1051 tree_size (const_tree node)
1052 {
1053 const enum tree_code code = TREE_CODE (node);
1054 switch (code)
1055 {
1056 case INTEGER_CST:
1057 return (sizeof (struct tree_int_cst)
1058 + (TREE_INT_CST_EXT_NUNITS (node) - 1) * sizeof (HOST_WIDE_INT));
1059
1060 case TREE_BINFO:
1061 return (offsetof (struct tree_binfo, base_binfos)
1062 + vec<tree, va_gc>
1063 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
1064
1065 case TREE_VEC:
1066 return (sizeof (struct tree_vec)
1067 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
1068
1069 case VECTOR_CST:
1070 return (sizeof (struct tree_vector)
1071 + (vector_cst_encoded_nelts (node) - 1) * sizeof (tree));
1072
1073 case STRING_CST:
1074 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
1075
1076 case OMP_CLAUSE:
1077 return (sizeof (struct tree_omp_clause)
1078 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
1079 * sizeof (tree));
1080
1081 default:
1082 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
1083 return (sizeof (struct tree_exp)
1084 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
1085 else
1086 return tree_code_size (code);
1087 }
1088 }
1089
1090 /* Return tree node kind based on tree CODE. */
1091
1092 static tree_node_kind
1093 get_stats_node_kind (enum tree_code code)
1094 {
1095 enum tree_code_class type = TREE_CODE_CLASS (code);
1096
1097 switch (type)
1098 {
1099 case tcc_declaration: /* A decl node */
1100 return d_kind;
1101 case tcc_type: /* a type node */
1102 return t_kind;
1103 case tcc_statement: /* an expression with side effects */
1104 return s_kind;
1105 case tcc_reference: /* a reference */
1106 return r_kind;
1107 case tcc_expression: /* an expression */
1108 case tcc_comparison: /* a comparison expression */
1109 case tcc_unary: /* a unary arithmetic expression */
1110 case tcc_binary: /* a binary arithmetic expression */
1111 return e_kind;
1112 case tcc_constant: /* a constant */
1113 return c_kind;
1114 case tcc_exceptional: /* something random, like an identifier. */
1115 switch (code)
1116 {
1117 case IDENTIFIER_NODE:
1118 return id_kind;
1119 case TREE_VEC:
1120 return vec_kind;
1121 case TREE_BINFO:
1122 return binfo_kind;
1123 case SSA_NAME:
1124 return ssa_name_kind;
1125 case BLOCK:
1126 return b_kind;
1127 case CONSTRUCTOR:
1128 return constr_kind;
1129 case OMP_CLAUSE:
1130 return omp_clause_kind;
1131 default:
1132 return x_kind;
1133 }
1134 break;
1135 case tcc_vl_exp:
1136 return e_kind;
1137 default:
1138 gcc_unreachable ();
1139 }
1140 }
1141
1142 /* Record interesting allocation statistics for a tree node with CODE
1143 and LENGTH. */
1144
1145 static void
1146 record_node_allocation_statistics (enum tree_code code, size_t length)
1147 {
1148 if (!GATHER_STATISTICS)
1149 return;
1150
1151 tree_node_kind kind = get_stats_node_kind (code);
1152
1153 tree_code_counts[(int) code]++;
1154 tree_node_counts[(int) kind]++;
1155 tree_node_sizes[(int) kind] += length;
1156 }
1157
1158 /* Allocate and return a new UID from the DECL_UID namespace. */
1159
1160 int
1161 allocate_decl_uid (void)
1162 {
1163 return next_decl_uid++;
1164 }
1165
1166 /* Return a newly allocated node of code CODE. For decl and type
1167 nodes, some other fields are initialized. The rest of the node is
1168 initialized to zero. This function cannot be used for TREE_VEC,
1169 INTEGER_CST or OMP_CLAUSE nodes, which is enforced by asserts in
1170 tree_code_size.
1171
1172 Achoo! I got a code in the node. */
1173
1174 tree
1175 make_node (enum tree_code code MEM_STAT_DECL)
1176 {
1177 tree t;
1178 enum tree_code_class type = TREE_CODE_CLASS (code);
1179 size_t length = tree_code_size (code);
1180
1181 record_node_allocation_statistics (code, length);
1182
1183 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1184 TREE_SET_CODE (t, code);
1185
1186 switch (type)
1187 {
1188 case tcc_statement:
1189 if (code != DEBUG_BEGIN_STMT)
1190 TREE_SIDE_EFFECTS (t) = 1;
1191 break;
1192
1193 case tcc_declaration:
1194 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1195 {
1196 if (code == FUNCTION_DECL)
1197 {
1198 SET_DECL_ALIGN (t, FUNCTION_ALIGNMENT (FUNCTION_BOUNDARY));
1199 SET_DECL_MODE (t, FUNCTION_MODE);
1200 }
1201 else
1202 SET_DECL_ALIGN (t, 1);
1203 }
1204 DECL_SOURCE_LOCATION (t) = input_location;
1205 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
1206 DECL_UID (t) = --next_debug_decl_uid;
1207 else
1208 {
1209 DECL_UID (t) = allocate_decl_uid ();
1210 SET_DECL_PT_UID (t, -1);
1211 }
1212 if (TREE_CODE (t) == LABEL_DECL)
1213 LABEL_DECL_UID (t) = -1;
1214
1215 break;
1216
1217 case tcc_type:
1218 TYPE_UID (t) = next_type_uid++;
1219 SET_TYPE_ALIGN (t, BITS_PER_UNIT);
1220 TYPE_USER_ALIGN (t) = 0;
1221 TYPE_MAIN_VARIANT (t) = t;
1222 TYPE_CANONICAL (t) = t;
1223
1224 /* Default to no attributes for type, but let target change that. */
1225 TYPE_ATTRIBUTES (t) = NULL_TREE;
1226 targetm.set_default_type_attributes (t);
1227
1228 /* We have not yet computed the alias set for this type. */
1229 TYPE_ALIAS_SET (t) = -1;
1230 break;
1231
1232 case tcc_constant:
1233 TREE_CONSTANT (t) = 1;
1234 break;
1235
1236 case tcc_expression:
1237 switch (code)
1238 {
1239 case INIT_EXPR:
1240 case MODIFY_EXPR:
1241 case VA_ARG_EXPR:
1242 case PREDECREMENT_EXPR:
1243 case PREINCREMENT_EXPR:
1244 case POSTDECREMENT_EXPR:
1245 case POSTINCREMENT_EXPR:
1246 /* All of these have side-effects, no matter what their
1247 operands are. */
1248 TREE_SIDE_EFFECTS (t) = 1;
1249 break;
1250
1251 default:
1252 break;
1253 }
1254 break;
1255
1256 case tcc_exceptional:
1257 switch (code)
1258 {
1259 case TARGET_OPTION_NODE:
1260 TREE_TARGET_OPTION(t)
1261 = ggc_cleared_alloc<struct cl_target_option> ();
1262 break;
1263
1264 case OPTIMIZATION_NODE:
1265 TREE_OPTIMIZATION (t)
1266 = ggc_cleared_alloc<struct cl_optimization> ();
1267 break;
1268
1269 default:
1270 break;
1271 }
1272 break;
1273
1274 default:
1275 /* Other classes need no special treatment. */
1276 break;
1277 }
1278
1279 return t;
1280 }
1281
1282 /* Free tree node. */
1283
1284 void
1285 free_node (tree node)
1286 {
1287 enum tree_code code = TREE_CODE (node);
1288 if (GATHER_STATISTICS)
1289 {
1290 enum tree_node_kind kind = get_stats_node_kind (code);
1291
1292 gcc_checking_assert (tree_code_counts[(int) TREE_CODE (node)] != 0);
1293 gcc_checking_assert (tree_node_counts[(int) kind] != 0);
1294 gcc_checking_assert (tree_node_sizes[(int) kind] >= tree_size (node));
1295
1296 tree_code_counts[(int) TREE_CODE (node)]--;
1297 tree_node_counts[(int) kind]--;
1298 tree_node_sizes[(int) kind] -= tree_size (node);
1299 }
1300 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1301 vec_free (CONSTRUCTOR_ELTS (node));
1302 else if (code == BLOCK)
1303 vec_free (BLOCK_NONLOCALIZED_VARS (node));
1304 else if (code == TREE_BINFO)
1305 vec_free (BINFO_BASE_ACCESSES (node));
1306 else if (code == OPTIMIZATION_NODE)
1307 cl_optimization_option_free (TREE_OPTIMIZATION (node));
1308 else if (code == TARGET_OPTION_NODE)
1309 cl_target_option_free (TREE_TARGET_OPTION (node));
1310 ggc_free (node);
1311 }
1312 \f
1313 /* Return a new node with the same contents as NODE except that its
1314 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
1315
1316 tree
1317 copy_node (tree node MEM_STAT_DECL)
1318 {
1319 tree t;
1320 enum tree_code code = TREE_CODE (node);
1321 size_t length;
1322
1323 gcc_assert (code != STATEMENT_LIST);
1324
1325 length = tree_size (node);
1326 record_node_allocation_statistics (code, length);
1327 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1328 memcpy (t, node, length);
1329
1330 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
1331 TREE_CHAIN (t) = 0;
1332 TREE_ASM_WRITTEN (t) = 0;
1333 TREE_VISITED (t) = 0;
1334
1335 if (TREE_CODE_CLASS (code) == tcc_declaration)
1336 {
1337 if (code == DEBUG_EXPR_DECL)
1338 DECL_UID (t) = --next_debug_decl_uid;
1339 else
1340 {
1341 DECL_UID (t) = allocate_decl_uid ();
1342 if (DECL_PT_UID_SET_P (node))
1343 SET_DECL_PT_UID (t, DECL_PT_UID (node));
1344 }
1345 if ((TREE_CODE (node) == PARM_DECL || VAR_P (node))
1346 && DECL_HAS_VALUE_EXPR_P (node))
1347 {
1348 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
1349 DECL_HAS_VALUE_EXPR_P (t) = 1;
1350 }
1351 /* DECL_DEBUG_EXPR is copied explicitly by callers. */
1352 if (VAR_P (node))
1353 {
1354 DECL_HAS_DEBUG_EXPR_P (t) = 0;
1355 t->decl_with_vis.symtab_node = NULL;
1356 }
1357 if (VAR_P (node) && DECL_HAS_INIT_PRIORITY_P (node))
1358 {
1359 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
1360 DECL_HAS_INIT_PRIORITY_P (t) = 1;
1361 }
1362 if (TREE_CODE (node) == FUNCTION_DECL)
1363 {
1364 DECL_STRUCT_FUNCTION (t) = NULL;
1365 t->decl_with_vis.symtab_node = NULL;
1366 }
1367 }
1368 else if (TREE_CODE_CLASS (code) == tcc_type)
1369 {
1370 TYPE_UID (t) = next_type_uid++;
1371 /* The following is so that the debug code for
1372 the copy is different from the original type.
1373 The two statements usually duplicate each other
1374 (because they clear fields of the same union),
1375 but the optimizer should catch that. */
1376 TYPE_SYMTAB_ADDRESS (t) = 0;
1377 TYPE_SYMTAB_DIE (t) = 0;
1378
1379 /* Do not copy the values cache. */
1380 if (TYPE_CACHED_VALUES_P (t))
1381 {
1382 TYPE_CACHED_VALUES_P (t) = 0;
1383 TYPE_CACHED_VALUES (t) = NULL_TREE;
1384 }
1385 }
1386 else if (code == TARGET_OPTION_NODE)
1387 {
1388 TREE_TARGET_OPTION (t) = ggc_alloc<struct cl_target_option>();
1389 memcpy (TREE_TARGET_OPTION (t), TREE_TARGET_OPTION (node),
1390 sizeof (struct cl_target_option));
1391 }
1392 else if (code == OPTIMIZATION_NODE)
1393 {
1394 TREE_OPTIMIZATION (t) = ggc_alloc<struct cl_optimization>();
1395 memcpy (TREE_OPTIMIZATION (t), TREE_OPTIMIZATION (node),
1396 sizeof (struct cl_optimization));
1397 }
1398
1399 return t;
1400 }
1401
1402 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1403 For example, this can copy a list made of TREE_LIST nodes. */
1404
1405 tree
1406 copy_list (tree list)
1407 {
1408 tree head;
1409 tree prev, next;
1410
1411 if (list == 0)
1412 return 0;
1413
1414 head = prev = copy_node (list);
1415 next = TREE_CHAIN (list);
1416 while (next)
1417 {
1418 TREE_CHAIN (prev) = copy_node (next);
1419 prev = TREE_CHAIN (prev);
1420 next = TREE_CHAIN (next);
1421 }
1422 return head;
1423 }
1424
1425 \f
1426 /* Return the value that TREE_INT_CST_EXT_NUNITS should have for an
1427 INTEGER_CST with value CST and type TYPE. */
1428
1429 static unsigned int
1430 get_int_cst_ext_nunits (tree type, const wide_int &cst)
1431 {
1432 gcc_checking_assert (cst.get_precision () == TYPE_PRECISION (type));
1433 /* We need extra HWIs if CST is an unsigned integer with its
1434 upper bit set. */
1435 if (TYPE_UNSIGNED (type) && wi::neg_p (cst))
1436 return cst.get_precision () / HOST_BITS_PER_WIDE_INT + 1;
1437 return cst.get_len ();
1438 }
1439
1440 /* Return a new INTEGER_CST with value CST and type TYPE. */
1441
1442 static tree
1443 build_new_int_cst (tree type, const wide_int &cst)
1444 {
1445 unsigned int len = cst.get_len ();
1446 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1447 tree nt = make_int_cst (len, ext_len);
1448
1449 if (len < ext_len)
1450 {
1451 --ext_len;
1452 TREE_INT_CST_ELT (nt, ext_len)
1453 = zext_hwi (-1, cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1454 for (unsigned int i = len; i < ext_len; ++i)
1455 TREE_INT_CST_ELT (nt, i) = -1;
1456 }
1457 else if (TYPE_UNSIGNED (type)
1458 && cst.get_precision () < len * HOST_BITS_PER_WIDE_INT)
1459 {
1460 len--;
1461 TREE_INT_CST_ELT (nt, len)
1462 = zext_hwi (cst.elt (len),
1463 cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1464 }
1465
1466 for (unsigned int i = 0; i < len; i++)
1467 TREE_INT_CST_ELT (nt, i) = cst.elt (i);
1468 TREE_TYPE (nt) = type;
1469 return nt;
1470 }
1471
1472 /* Return a new POLY_INT_CST with coefficients COEFFS and type TYPE. */
1473
1474 static tree
1475 build_new_poly_int_cst (tree type, tree (&coeffs)[NUM_POLY_INT_COEFFS]
1476 CXX_MEM_STAT_INFO)
1477 {
1478 size_t length = sizeof (struct tree_poly_int_cst);
1479 record_node_allocation_statistics (POLY_INT_CST, length);
1480
1481 tree t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1482
1483 TREE_SET_CODE (t, POLY_INT_CST);
1484 TREE_CONSTANT (t) = 1;
1485 TREE_TYPE (t) = type;
1486 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1487 POLY_INT_CST_COEFF (t, i) = coeffs[i];
1488 return t;
1489 }
1490
1491 /* Create a constant tree that contains CST sign-extended to TYPE. */
1492
1493 tree
1494 build_int_cst (tree type, poly_int64 cst)
1495 {
1496 /* Support legacy code. */
1497 if (!type)
1498 type = integer_type_node;
1499
1500 return wide_int_to_tree (type, wi::shwi (cst, TYPE_PRECISION (type)));
1501 }
1502
1503 /* Create a constant tree that contains CST zero-extended to TYPE. */
1504
1505 tree
1506 build_int_cstu (tree type, poly_uint64 cst)
1507 {
1508 return wide_int_to_tree (type, wi::uhwi (cst, TYPE_PRECISION (type)));
1509 }
1510
1511 /* Create a constant tree that contains CST sign-extended to TYPE. */
1512
1513 tree
1514 build_int_cst_type (tree type, poly_int64 cst)
1515 {
1516 gcc_assert (type);
1517 return wide_int_to_tree (type, wi::shwi (cst, TYPE_PRECISION (type)));
1518 }
1519
1520 /* Constructs tree in type TYPE from with value given by CST. Signedness
1521 of CST is assumed to be the same as the signedness of TYPE. */
1522
1523 tree
1524 double_int_to_tree (tree type, double_int cst)
1525 {
1526 return wide_int_to_tree (type, widest_int::from (cst, TYPE_SIGN (type)));
1527 }
1528
1529 /* We force the wide_int CST to the range of the type TYPE by sign or
1530 zero extending it. OVERFLOWABLE indicates if we are interested in
1531 overflow of the value, when >0 we are only interested in signed
1532 overflow, for <0 we are interested in any overflow. OVERFLOWED
1533 indicates whether overflow has already occurred. CONST_OVERFLOWED
1534 indicates whether constant overflow has already occurred. We force
1535 T's value to be within range of T's type (by setting to 0 or 1 all
1536 the bits outside the type's range). We set TREE_OVERFLOWED if,
1537 OVERFLOWED is nonzero,
1538 or OVERFLOWABLE is >0 and signed overflow occurs
1539 or OVERFLOWABLE is <0 and any overflow occurs
1540 We return a new tree node for the extended wide_int. The node
1541 is shared if no overflow flags are set. */
1542
1543
1544 tree
1545 force_fit_type (tree type, const poly_wide_int_ref &cst,
1546 int overflowable, bool overflowed)
1547 {
1548 signop sign = TYPE_SIGN (type);
1549
1550 /* If we need to set overflow flags, return a new unshared node. */
1551 if (overflowed || !wi::fits_to_tree_p (cst, type))
1552 {
1553 if (overflowed
1554 || overflowable < 0
1555 || (overflowable > 0 && sign == SIGNED))
1556 {
1557 poly_wide_int tmp = poly_wide_int::from (cst, TYPE_PRECISION (type),
1558 sign);
1559 tree t;
1560 if (tmp.is_constant ())
1561 t = build_new_int_cst (type, tmp.coeffs[0]);
1562 else
1563 {
1564 tree coeffs[NUM_POLY_INT_COEFFS];
1565 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1566 {
1567 coeffs[i] = build_new_int_cst (type, tmp.coeffs[i]);
1568 TREE_OVERFLOW (coeffs[i]) = 1;
1569 }
1570 t = build_new_poly_int_cst (type, coeffs);
1571 }
1572 TREE_OVERFLOW (t) = 1;
1573 return t;
1574 }
1575 }
1576
1577 /* Else build a shared node. */
1578 return wide_int_to_tree (type, cst);
1579 }
1580
1581 /* These are the hash table functions for the hash table of INTEGER_CST
1582 nodes of a sizetype. */
1583
1584 /* Return the hash code X, an INTEGER_CST. */
1585
1586 hashval_t
1587 int_cst_hasher::hash (tree x)
1588 {
1589 const_tree const t = x;
1590 hashval_t code = TYPE_UID (TREE_TYPE (t));
1591 int i;
1592
1593 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
1594 code = iterative_hash_host_wide_int (TREE_INT_CST_ELT(t, i), code);
1595
1596 return code;
1597 }
1598
1599 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1600 is the same as that given by *Y, which is the same. */
1601
1602 bool
1603 int_cst_hasher::equal (tree x, tree y)
1604 {
1605 const_tree const xt = x;
1606 const_tree const yt = y;
1607
1608 if (TREE_TYPE (xt) != TREE_TYPE (yt)
1609 || TREE_INT_CST_NUNITS (xt) != TREE_INT_CST_NUNITS (yt)
1610 || TREE_INT_CST_EXT_NUNITS (xt) != TREE_INT_CST_EXT_NUNITS (yt))
1611 return false;
1612
1613 for (int i = 0; i < TREE_INT_CST_NUNITS (xt); i++)
1614 if (TREE_INT_CST_ELT (xt, i) != TREE_INT_CST_ELT (yt, i))
1615 return false;
1616
1617 return true;
1618 }
1619
1620 /* Cache wide_int CST into the TYPE_CACHED_VALUES cache for TYPE.
1621 SLOT is the slot entry to store it in, and MAX_SLOTS is the maximum
1622 number of slots that can be cached for the type. */
1623
1624 static inline tree
1625 cache_wide_int_in_type_cache (tree type, const wide_int &cst,
1626 int slot, int max_slots)
1627 {
1628 gcc_checking_assert (slot >= 0);
1629 /* Initialize cache. */
1630 if (!TYPE_CACHED_VALUES_P (type))
1631 {
1632 TYPE_CACHED_VALUES_P (type) = 1;
1633 TYPE_CACHED_VALUES (type) = make_tree_vec (max_slots);
1634 }
1635 tree t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), slot);
1636 if (!t)
1637 {
1638 /* Create a new shared int. */
1639 t = build_new_int_cst (type, cst);
1640 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), slot) = t;
1641 }
1642 return t;
1643 }
1644
1645 /* Create an INT_CST node of TYPE and value CST.
1646 The returned node is always shared. For small integers we use a
1647 per-type vector cache, for larger ones we use a single hash table.
1648 The value is extended from its precision according to the sign of
1649 the type to be a multiple of HOST_BITS_PER_WIDE_INT. This defines
1650 the upper bits and ensures that hashing and value equality based
1651 upon the underlying HOST_WIDE_INTs works without masking. */
1652
1653 static tree
1654 wide_int_to_tree_1 (tree type, const wide_int_ref &pcst)
1655 {
1656 tree t;
1657 int ix = -1;
1658 int limit = 0;
1659
1660 gcc_assert (type);
1661 unsigned int prec = TYPE_PRECISION (type);
1662 signop sgn = TYPE_SIGN (type);
1663
1664 /* Verify that everything is canonical. */
1665 int l = pcst.get_len ();
1666 if (l > 1)
1667 {
1668 if (pcst.elt (l - 1) == 0)
1669 gcc_checking_assert (pcst.elt (l - 2) < 0);
1670 if (pcst.elt (l - 1) == HOST_WIDE_INT_M1)
1671 gcc_checking_assert (pcst.elt (l - 2) >= 0);
1672 }
1673
1674 wide_int cst = wide_int::from (pcst, prec, sgn);
1675 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1676
1677 enum tree_code code = TREE_CODE (type);
1678 if (code == POINTER_TYPE || code == REFERENCE_TYPE)
1679 {
1680 /* Cache NULL pointer and zero bounds. */
1681 if (cst == 0)
1682 ix = 0;
1683 /* Cache upper bounds of pointers. */
1684 else if (cst == wi::max_value (prec, sgn))
1685 ix = 1;
1686 /* Cache 1 which is used for a non-zero range. */
1687 else if (cst == 1)
1688 ix = 2;
1689
1690 if (ix >= 0)
1691 {
1692 t = cache_wide_int_in_type_cache (type, cst, ix, 3);
1693 /* Make sure no one is clobbering the shared constant. */
1694 gcc_checking_assert (TREE_TYPE (t) == type
1695 && cst == wi::to_wide (t));
1696 return t;
1697 }
1698 }
1699 if (ext_len == 1)
1700 {
1701 /* We just need to store a single HOST_WIDE_INT. */
1702 HOST_WIDE_INT hwi;
1703 if (TYPE_UNSIGNED (type))
1704 hwi = cst.to_uhwi ();
1705 else
1706 hwi = cst.to_shwi ();
1707
1708 switch (code)
1709 {
1710 case NULLPTR_TYPE:
1711 gcc_assert (hwi == 0);
1712 /* Fallthru. */
1713
1714 case POINTER_TYPE:
1715 case REFERENCE_TYPE:
1716 /* Ignore pointers, as they were already handled above. */
1717 break;
1718
1719 case BOOLEAN_TYPE:
1720 /* Cache false or true. */
1721 limit = 2;
1722 if (IN_RANGE (hwi, 0, 1))
1723 ix = hwi;
1724 break;
1725
1726 case INTEGER_TYPE:
1727 case OFFSET_TYPE:
1728 if (TYPE_SIGN (type) == UNSIGNED)
1729 {
1730 /* Cache [0, N). */
1731 limit = param_integer_share_limit;
1732 if (IN_RANGE (hwi, 0, param_integer_share_limit - 1))
1733 ix = hwi;
1734 }
1735 else
1736 {
1737 /* Cache [-1, N). */
1738 limit = param_integer_share_limit + 1;
1739 if (IN_RANGE (hwi, -1, param_integer_share_limit - 1))
1740 ix = hwi + 1;
1741 }
1742 break;
1743
1744 case ENUMERAL_TYPE:
1745 break;
1746
1747 default:
1748 gcc_unreachable ();
1749 }
1750
1751 if (ix >= 0)
1752 {
1753 t = cache_wide_int_in_type_cache (type, cst, ix, limit);
1754 /* Make sure no one is clobbering the shared constant. */
1755 gcc_checking_assert (TREE_TYPE (t) == type
1756 && TREE_INT_CST_NUNITS (t) == 1
1757 && TREE_INT_CST_OFFSET_NUNITS (t) == 1
1758 && TREE_INT_CST_EXT_NUNITS (t) == 1
1759 && TREE_INT_CST_ELT (t, 0) == hwi);
1760 return t;
1761 }
1762 else
1763 {
1764 /* Use the cache of larger shared ints, using int_cst_node as
1765 a temporary. */
1766
1767 TREE_INT_CST_ELT (int_cst_node, 0) = hwi;
1768 TREE_TYPE (int_cst_node) = type;
1769
1770 tree *slot = int_cst_hash_table->find_slot (int_cst_node, INSERT);
1771 t = *slot;
1772 if (!t)
1773 {
1774 /* Insert this one into the hash table. */
1775 t = int_cst_node;
1776 *slot = t;
1777 /* Make a new node for next time round. */
1778 int_cst_node = make_int_cst (1, 1);
1779 }
1780 }
1781 }
1782 else
1783 {
1784 /* The value either hashes properly or we drop it on the floor
1785 for the gc to take care of. There will not be enough of them
1786 to worry about. */
1787
1788 tree nt = build_new_int_cst (type, cst);
1789 tree *slot = int_cst_hash_table->find_slot (nt, INSERT);
1790 t = *slot;
1791 if (!t)
1792 {
1793 /* Insert this one into the hash table. */
1794 t = nt;
1795 *slot = t;
1796 }
1797 else
1798 ggc_free (nt);
1799 }
1800
1801 return t;
1802 }
1803
1804 hashval_t
1805 poly_int_cst_hasher::hash (tree t)
1806 {
1807 inchash::hash hstate;
1808
1809 hstate.add_int (TYPE_UID (TREE_TYPE (t)));
1810 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1811 hstate.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t, i)));
1812
1813 return hstate.end ();
1814 }
1815
1816 bool
1817 poly_int_cst_hasher::equal (tree x, const compare_type &y)
1818 {
1819 if (TREE_TYPE (x) != y.first)
1820 return false;
1821 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1822 if (wi::to_wide (POLY_INT_CST_COEFF (x, i)) != y.second->coeffs[i])
1823 return false;
1824 return true;
1825 }
1826
1827 /* Build a POLY_INT_CST node with type TYPE and with the elements in VALUES.
1828 The elements must also have type TYPE. */
1829
1830 tree
1831 build_poly_int_cst (tree type, const poly_wide_int_ref &values)
1832 {
1833 unsigned int prec = TYPE_PRECISION (type);
1834 gcc_assert (prec <= values.coeffs[0].get_precision ());
1835 poly_wide_int c = poly_wide_int::from (values, prec, SIGNED);
1836
1837 inchash::hash h;
1838 h.add_int (TYPE_UID (type));
1839 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1840 h.add_wide_int (c.coeffs[i]);
1841 poly_int_cst_hasher::compare_type comp (type, &c);
1842 tree *slot = poly_int_cst_hash_table->find_slot_with_hash (comp, h.end (),
1843 INSERT);
1844 if (*slot == NULL_TREE)
1845 {
1846 tree coeffs[NUM_POLY_INT_COEFFS];
1847 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1848 coeffs[i] = wide_int_to_tree_1 (type, c.coeffs[i]);
1849 *slot = build_new_poly_int_cst (type, coeffs);
1850 }
1851 return *slot;
1852 }
1853
1854 /* Create a constant tree with value VALUE in type TYPE. */
1855
1856 tree
1857 wide_int_to_tree (tree type, const poly_wide_int_ref &value)
1858 {
1859 if (value.is_constant ())
1860 return wide_int_to_tree_1 (type, value.coeffs[0]);
1861 return build_poly_int_cst (type, value);
1862 }
1863
1864 /* Insert INTEGER_CST T into a cache of integer constants. And return
1865 the cached constant (which may or may not be T). If MIGHT_DUPLICATE
1866 is false, and T falls into the type's 'smaller values' range, there
1867 cannot be an existing entry. Otherwise, if MIGHT_DUPLICATE is true,
1868 or the value is large, should an existing entry exist, it is
1869 returned (rather than inserting T). */
1870
1871 tree
1872 cache_integer_cst (tree t, bool might_duplicate ATTRIBUTE_UNUSED)
1873 {
1874 tree type = TREE_TYPE (t);
1875 int ix = -1;
1876 int limit = 0;
1877 int prec = TYPE_PRECISION (type);
1878
1879 gcc_assert (!TREE_OVERFLOW (t));
1880
1881 /* The caching indices here must match those in
1882 wide_int_to_type_1. */
1883 switch (TREE_CODE (type))
1884 {
1885 case NULLPTR_TYPE:
1886 gcc_checking_assert (integer_zerop (t));
1887 /* Fallthru. */
1888
1889 case POINTER_TYPE:
1890 case REFERENCE_TYPE:
1891 {
1892 if (integer_zerop (t))
1893 ix = 0;
1894 else if (integer_onep (t))
1895 ix = 2;
1896
1897 if (ix >= 0)
1898 limit = 3;
1899 }
1900 break;
1901
1902 case BOOLEAN_TYPE:
1903 /* Cache false or true. */
1904 limit = 2;
1905 if (wi::ltu_p (wi::to_wide (t), 2))
1906 ix = TREE_INT_CST_ELT (t, 0);
1907 break;
1908
1909 case INTEGER_TYPE:
1910 case OFFSET_TYPE:
1911 if (TYPE_UNSIGNED (type))
1912 {
1913 /* Cache 0..N */
1914 limit = param_integer_share_limit;
1915
1916 /* This is a little hokie, but if the prec is smaller than
1917 what is necessary to hold param_integer_share_limit, then the
1918 obvious test will not get the correct answer. */
1919 if (prec < HOST_BITS_PER_WIDE_INT)
1920 {
1921 if (tree_to_uhwi (t)
1922 < (unsigned HOST_WIDE_INT) param_integer_share_limit)
1923 ix = tree_to_uhwi (t);
1924 }
1925 else if (wi::ltu_p (wi::to_wide (t), param_integer_share_limit))
1926 ix = tree_to_uhwi (t);
1927 }
1928 else
1929 {
1930 /* Cache -1..N */
1931 limit = param_integer_share_limit + 1;
1932
1933 if (integer_minus_onep (t))
1934 ix = 0;
1935 else if (!wi::neg_p (wi::to_wide (t)))
1936 {
1937 if (prec < HOST_BITS_PER_WIDE_INT)
1938 {
1939 if (tree_to_shwi (t) < param_integer_share_limit)
1940 ix = tree_to_shwi (t) + 1;
1941 }
1942 else if (wi::ltu_p (wi::to_wide (t), param_integer_share_limit))
1943 ix = tree_to_shwi (t) + 1;
1944 }
1945 }
1946 break;
1947
1948 case ENUMERAL_TYPE:
1949 /* The slot used by TYPE_CACHED_VALUES is used for the enum
1950 members. */
1951 break;
1952
1953 default:
1954 gcc_unreachable ();
1955 }
1956
1957 if (ix >= 0)
1958 {
1959 /* Look for it in the type's vector of small shared ints. */
1960 if (!TYPE_CACHED_VALUES_P (type))
1961 {
1962 TYPE_CACHED_VALUES_P (type) = 1;
1963 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1964 }
1965
1966 if (tree r = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix))
1967 {
1968 gcc_checking_assert (might_duplicate);
1969 t = r;
1970 }
1971 else
1972 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1973 }
1974 else
1975 {
1976 /* Use the cache of larger shared ints. */
1977 tree *slot = int_cst_hash_table->find_slot (t, INSERT);
1978 if (tree r = *slot)
1979 {
1980 /* If there is already an entry for the number verify it's the
1981 same value. */
1982 gcc_checking_assert (wi::to_wide (tree (r)) == wi::to_wide (t));
1983 /* And return the cached value. */
1984 t = r;
1985 }
1986 else
1987 /* Otherwise insert this one into the hash table. */
1988 *slot = t;
1989 }
1990
1991 return t;
1992 }
1993
1994
1995 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1996 and the rest are zeros. */
1997
1998 tree
1999 build_low_bits_mask (tree type, unsigned bits)
2000 {
2001 gcc_assert (bits <= TYPE_PRECISION (type));
2002
2003 return wide_int_to_tree (type, wi::mask (bits, false,
2004 TYPE_PRECISION (type)));
2005 }
2006
2007 /* Checks that X is integer constant that can be expressed in (unsigned)
2008 HOST_WIDE_INT without loss of precision. */
2009
2010 bool
2011 cst_and_fits_in_hwi (const_tree x)
2012 {
2013 return (TREE_CODE (x) == INTEGER_CST
2014 && (tree_fits_shwi_p (x) || tree_fits_uhwi_p (x)));
2015 }
2016
2017 /* Build a newly constructed VECTOR_CST with the given values of
2018 (VECTOR_CST_)LOG2_NPATTERNS and (VECTOR_CST_)NELTS_PER_PATTERN. */
2019
2020 tree
2021 make_vector (unsigned log2_npatterns,
2022 unsigned int nelts_per_pattern MEM_STAT_DECL)
2023 {
2024 gcc_assert (IN_RANGE (nelts_per_pattern, 1, 3));
2025 tree t;
2026 unsigned npatterns = 1 << log2_npatterns;
2027 unsigned encoded_nelts = npatterns * nelts_per_pattern;
2028 unsigned length = (sizeof (struct tree_vector)
2029 + (encoded_nelts - 1) * sizeof (tree));
2030
2031 record_node_allocation_statistics (VECTOR_CST, length);
2032
2033 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2034
2035 TREE_SET_CODE (t, VECTOR_CST);
2036 TREE_CONSTANT (t) = 1;
2037 VECTOR_CST_LOG2_NPATTERNS (t) = log2_npatterns;
2038 VECTOR_CST_NELTS_PER_PATTERN (t) = nelts_per_pattern;
2039
2040 return t;
2041 }
2042
2043 /* Return a new VECTOR_CST node whose type is TYPE and whose values
2044 are extracted from V, a vector of CONSTRUCTOR_ELT. */
2045
2046 tree
2047 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
2048 {
2049 if (vec_safe_length (v) == 0)
2050 return build_zero_cst (type);
2051
2052 unsigned HOST_WIDE_INT idx, nelts;
2053 tree value;
2054
2055 /* We can't construct a VECTOR_CST for a variable number of elements. */
2056 nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
2057 tree_vector_builder vec (type, nelts, 1);
2058 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
2059 {
2060 if (TREE_CODE (value) == VECTOR_CST)
2061 {
2062 /* If NELTS is constant then this must be too. */
2063 unsigned int sub_nelts = VECTOR_CST_NELTS (value).to_constant ();
2064 for (unsigned i = 0; i < sub_nelts; ++i)
2065 vec.quick_push (VECTOR_CST_ELT (value, i));
2066 }
2067 else
2068 vec.quick_push (value);
2069 }
2070 while (vec.length () < nelts)
2071 vec.quick_push (build_zero_cst (TREE_TYPE (type)));
2072
2073 return vec.build ();
2074 }
2075
2076 /* Build a vector of type VECTYPE where all the elements are SCs. */
2077 tree
2078 build_vector_from_val (tree vectype, tree sc)
2079 {
2080 unsigned HOST_WIDE_INT i, nunits;
2081
2082 if (sc == error_mark_node)
2083 return sc;
2084
2085 /* Verify that the vector type is suitable for SC. Note that there
2086 is some inconsistency in the type-system with respect to restrict
2087 qualifications of pointers. Vector types always have a main-variant
2088 element type and the qualification is applied to the vector-type.
2089 So TREE_TYPE (vector-type) does not return a properly qualified
2090 vector element-type. */
2091 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
2092 TREE_TYPE (vectype)));
2093
2094 if (CONSTANT_CLASS_P (sc))
2095 {
2096 tree_vector_builder v (vectype, 1, 1);
2097 v.quick_push (sc);
2098 return v.build ();
2099 }
2100 else if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits))
2101 return fold_build1 (VEC_DUPLICATE_EXPR, vectype, sc);
2102 else
2103 {
2104 vec<constructor_elt, va_gc> *v;
2105 vec_alloc (v, nunits);
2106 for (i = 0; i < nunits; ++i)
2107 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
2108 return build_constructor (vectype, v);
2109 }
2110 }
2111
2112 /* If TYPE is not a vector type, just return SC, otherwise return
2113 build_vector_from_val (TYPE, SC). */
2114
2115 tree
2116 build_uniform_cst (tree type, tree sc)
2117 {
2118 if (!VECTOR_TYPE_P (type))
2119 return sc;
2120
2121 return build_vector_from_val (type, sc);
2122 }
2123
2124 /* Build a vector series of type TYPE in which element I has the value
2125 BASE + I * STEP. The result is a constant if BASE and STEP are constant
2126 and a VEC_SERIES_EXPR otherwise. */
2127
2128 tree
2129 build_vec_series (tree type, tree base, tree step)
2130 {
2131 if (integer_zerop (step))
2132 return build_vector_from_val (type, base);
2133 if (TREE_CODE (base) == INTEGER_CST && TREE_CODE (step) == INTEGER_CST)
2134 {
2135 tree_vector_builder builder (type, 1, 3);
2136 tree elt1 = wide_int_to_tree (TREE_TYPE (base),
2137 wi::to_wide (base) + wi::to_wide (step));
2138 tree elt2 = wide_int_to_tree (TREE_TYPE (base),
2139 wi::to_wide (elt1) + wi::to_wide (step));
2140 builder.quick_push (base);
2141 builder.quick_push (elt1);
2142 builder.quick_push (elt2);
2143 return builder.build ();
2144 }
2145 return build2 (VEC_SERIES_EXPR, type, base, step);
2146 }
2147
2148 /* Return a vector with the same number of units and number of bits
2149 as VEC_TYPE, but in which the elements are a linear series of unsigned
2150 integers { BASE, BASE + STEP, BASE + STEP * 2, ... }. */
2151
2152 tree
2153 build_index_vector (tree vec_type, poly_uint64 base, poly_uint64 step)
2154 {
2155 tree index_vec_type = vec_type;
2156 tree index_elt_type = TREE_TYPE (vec_type);
2157 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vec_type);
2158 if (!INTEGRAL_TYPE_P (index_elt_type) || !TYPE_UNSIGNED (index_elt_type))
2159 {
2160 index_elt_type = build_nonstandard_integer_type
2161 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (index_elt_type)), true);
2162 index_vec_type = build_vector_type (index_elt_type, nunits);
2163 }
2164
2165 tree_vector_builder v (index_vec_type, 1, 3);
2166 for (unsigned int i = 0; i < 3; ++i)
2167 v.quick_push (build_int_cstu (index_elt_type, base + i * step));
2168 return v.build ();
2169 }
2170
2171 /* Return a VECTOR_CST of type VEC_TYPE in which the first NUM_A
2172 elements are A and the rest are B. */
2173
2174 tree
2175 build_vector_a_then_b (tree vec_type, unsigned int num_a, tree a, tree b)
2176 {
2177 gcc_assert (known_le (num_a, TYPE_VECTOR_SUBPARTS (vec_type)));
2178 unsigned int count = constant_lower_bound (TYPE_VECTOR_SUBPARTS (vec_type));
2179 /* Optimize the constant case. */
2180 if ((count & 1) == 0 && TYPE_VECTOR_SUBPARTS (vec_type).is_constant ())
2181 count /= 2;
2182 tree_vector_builder builder (vec_type, count, 2);
2183 for (unsigned int i = 0; i < count * 2; ++i)
2184 builder.quick_push (i < num_a ? a : b);
2185 return builder.build ();
2186 }
2187
2188 /* Something has messed with the elements of CONSTRUCTOR C after it was built;
2189 calculate TREE_CONSTANT and TREE_SIDE_EFFECTS. */
2190
2191 void
2192 recompute_constructor_flags (tree c)
2193 {
2194 unsigned int i;
2195 tree val;
2196 bool constant_p = true;
2197 bool side_effects_p = false;
2198 vec<constructor_elt, va_gc> *vals = CONSTRUCTOR_ELTS (c);
2199
2200 FOR_EACH_CONSTRUCTOR_VALUE (vals, i, val)
2201 {
2202 /* Mostly ctors will have elts that don't have side-effects, so
2203 the usual case is to scan all the elements. Hence a single
2204 loop for both const and side effects, rather than one loop
2205 each (with early outs). */
2206 if (!TREE_CONSTANT (val))
2207 constant_p = false;
2208 if (TREE_SIDE_EFFECTS (val))
2209 side_effects_p = true;
2210 }
2211
2212 TREE_SIDE_EFFECTS (c) = side_effects_p;
2213 TREE_CONSTANT (c) = constant_p;
2214 }
2215
2216 /* Make sure that TREE_CONSTANT and TREE_SIDE_EFFECTS are correct for
2217 CONSTRUCTOR C. */
2218
2219 void
2220 verify_constructor_flags (tree c)
2221 {
2222 unsigned int i;
2223 tree val;
2224 bool constant_p = TREE_CONSTANT (c);
2225 bool side_effects_p = TREE_SIDE_EFFECTS (c);
2226 vec<constructor_elt, va_gc> *vals = CONSTRUCTOR_ELTS (c);
2227
2228 FOR_EACH_CONSTRUCTOR_VALUE (vals, i, val)
2229 {
2230 if (constant_p && !TREE_CONSTANT (val))
2231 internal_error ("non-constant element in constant CONSTRUCTOR");
2232 if (!side_effects_p && TREE_SIDE_EFFECTS (val))
2233 internal_error ("side-effects element in no-side-effects CONSTRUCTOR");
2234 }
2235 }
2236
2237 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
2238 are in the vec pointed to by VALS. */
2239 tree
2240 build_constructor (tree type, vec<constructor_elt, va_gc> *vals MEM_STAT_DECL)
2241 {
2242 tree c = make_node (CONSTRUCTOR PASS_MEM_STAT);
2243
2244 TREE_TYPE (c) = type;
2245 CONSTRUCTOR_ELTS (c) = vals;
2246
2247 recompute_constructor_flags (c);
2248
2249 return c;
2250 }
2251
2252 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
2253 INDEX and VALUE. */
2254 tree
2255 build_constructor_single (tree type, tree index, tree value)
2256 {
2257 vec<constructor_elt, va_gc> *v;
2258 constructor_elt elt = {index, value};
2259
2260 vec_alloc (v, 1);
2261 v->quick_push (elt);
2262
2263 return build_constructor (type, v);
2264 }
2265
2266
2267 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
2268 are in a list pointed to by VALS. */
2269 tree
2270 build_constructor_from_list (tree type, tree vals)
2271 {
2272 tree t;
2273 vec<constructor_elt, va_gc> *v = NULL;
2274
2275 if (vals)
2276 {
2277 vec_alloc (v, list_length (vals));
2278 for (t = vals; t; t = TREE_CHAIN (t))
2279 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
2280 }
2281
2282 return build_constructor (type, v);
2283 }
2284
2285 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
2286 are in a vector pointed to by VALS. Note that the TREE_PURPOSE
2287 fields in the constructor remain null. */
2288
2289 tree
2290 build_constructor_from_vec (tree type, const vec<tree, va_gc> *vals)
2291 {
2292 vec<constructor_elt, va_gc> *v = NULL;
2293
2294 for (tree t : vals)
2295 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, t);
2296
2297 return build_constructor (type, v);
2298 }
2299
2300 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
2301 of elements, provided as index/value pairs. */
2302
2303 tree
2304 build_constructor_va (tree type, int nelts, ...)
2305 {
2306 vec<constructor_elt, va_gc> *v = NULL;
2307 va_list p;
2308
2309 va_start (p, nelts);
2310 vec_alloc (v, nelts);
2311 while (nelts--)
2312 {
2313 tree index = va_arg (p, tree);
2314 tree value = va_arg (p, tree);
2315 CONSTRUCTOR_APPEND_ELT (v, index, value);
2316 }
2317 va_end (p);
2318 return build_constructor (type, v);
2319 }
2320
2321 /* Return a node of type TYPE for which TREE_CLOBBER_P is true. */
2322
2323 tree
2324 build_clobber (tree type)
2325 {
2326 tree clobber = build_constructor (type, NULL);
2327 TREE_THIS_VOLATILE (clobber) = true;
2328 return clobber;
2329 }
2330
2331 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
2332
2333 tree
2334 build_fixed (tree type, FIXED_VALUE_TYPE f)
2335 {
2336 tree v;
2337 FIXED_VALUE_TYPE *fp;
2338
2339 v = make_node (FIXED_CST);
2340 fp = ggc_alloc<fixed_value> ();
2341 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
2342
2343 TREE_TYPE (v) = type;
2344 TREE_FIXED_CST_PTR (v) = fp;
2345 return v;
2346 }
2347
2348 /* Return a new REAL_CST node whose type is TYPE and value is D. */
2349
2350 tree
2351 build_real (tree type, REAL_VALUE_TYPE d)
2352 {
2353 tree v;
2354 REAL_VALUE_TYPE *dp;
2355 int overflow = 0;
2356
2357 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
2358 Consider doing it via real_convert now. */
2359
2360 v = make_node (REAL_CST);
2361 dp = ggc_alloc<real_value> ();
2362 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
2363
2364 TREE_TYPE (v) = type;
2365 TREE_REAL_CST_PTR (v) = dp;
2366 TREE_OVERFLOW (v) = overflow;
2367 return v;
2368 }
2369
2370 /* Like build_real, but first truncate D to the type. */
2371
2372 tree
2373 build_real_truncate (tree type, REAL_VALUE_TYPE d)
2374 {
2375 return build_real (type, real_value_truncate (TYPE_MODE (type), d));
2376 }
2377
2378 /* Return a new REAL_CST node whose type is TYPE
2379 and whose value is the integer value of the INTEGER_CST node I. */
2380
2381 REAL_VALUE_TYPE
2382 real_value_from_int_cst (const_tree type, const_tree i)
2383 {
2384 REAL_VALUE_TYPE d;
2385
2386 /* Clear all bits of the real value type so that we can later do
2387 bitwise comparisons to see if two values are the same. */
2388 memset (&d, 0, sizeof d);
2389
2390 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode, wi::to_wide (i),
2391 TYPE_SIGN (TREE_TYPE (i)));
2392 return d;
2393 }
2394
2395 /* Given a tree representing an integer constant I, return a tree
2396 representing the same value as a floating-point constant of type TYPE. */
2397
2398 tree
2399 build_real_from_int_cst (tree type, const_tree i)
2400 {
2401 tree v;
2402 int overflow = TREE_OVERFLOW (i);
2403
2404 v = build_real (type, real_value_from_int_cst (type, i));
2405
2406 TREE_OVERFLOW (v) |= overflow;
2407 return v;
2408 }
2409
2410 /* Return a new REAL_CST node whose type is TYPE
2411 and whose value is the integer value I which has sign SGN. */
2412
2413 tree
2414 build_real_from_wide (tree type, const wide_int_ref &i, signop sgn)
2415 {
2416 REAL_VALUE_TYPE d;
2417
2418 /* Clear all bits of the real value type so that we can later do
2419 bitwise comparisons to see if two values are the same. */
2420 memset (&d, 0, sizeof d);
2421
2422 real_from_integer (&d, TYPE_MODE (type), i, sgn);
2423 return build_real (type, d);
2424 }
2425
2426 /* Return a newly constructed STRING_CST node whose value is the LEN
2427 characters at STR when STR is nonnull, or all zeros otherwise.
2428 Note that for a C string literal, LEN should include the trailing NUL.
2429 The TREE_TYPE is not initialized. */
2430
2431 tree
2432 build_string (unsigned len, const char *str /*= NULL */)
2433 {
2434 /* Do not waste bytes provided by padding of struct tree_string. */
2435 unsigned size = len + offsetof (struct tree_string, str) + 1;
2436
2437 record_node_allocation_statistics (STRING_CST, size);
2438
2439 tree s = (tree) ggc_internal_alloc (size);
2440
2441 memset (s, 0, sizeof (struct tree_typed));
2442 TREE_SET_CODE (s, STRING_CST);
2443 TREE_CONSTANT (s) = 1;
2444 TREE_STRING_LENGTH (s) = len;
2445 if (str)
2446 memcpy (s->string.str, str, len);
2447 else
2448 memset (s->string.str, 0, len);
2449 s->string.str[len] = '\0';
2450
2451 return s;
2452 }
2453
2454 /* Return a newly constructed COMPLEX_CST node whose value is
2455 specified by the real and imaginary parts REAL and IMAG.
2456 Both REAL and IMAG should be constant nodes. TYPE, if specified,
2457 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
2458
2459 tree
2460 build_complex (tree type, tree real, tree imag)
2461 {
2462 gcc_assert (CONSTANT_CLASS_P (real));
2463 gcc_assert (CONSTANT_CLASS_P (imag));
2464
2465 tree t = make_node (COMPLEX_CST);
2466
2467 TREE_REALPART (t) = real;
2468 TREE_IMAGPART (t) = imag;
2469 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
2470 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
2471 return t;
2472 }
2473
2474 /* Build a complex (inf +- 0i), such as for the result of cproj.
2475 TYPE is the complex tree type of the result. If NEG is true, the
2476 imaginary zero is negative. */
2477
2478 tree
2479 build_complex_inf (tree type, bool neg)
2480 {
2481 REAL_VALUE_TYPE rinf, rzero = dconst0;
2482
2483 real_inf (&rinf);
2484 rzero.sign = neg;
2485 return build_complex (type, build_real (TREE_TYPE (type), rinf),
2486 build_real (TREE_TYPE (type), rzero));
2487 }
2488
2489 /* Return the constant 1 in type TYPE. If TYPE has several elements, each
2490 element is set to 1. In particular, this is 1 + i for complex types. */
2491
2492 tree
2493 build_each_one_cst (tree type)
2494 {
2495 if (TREE_CODE (type) == COMPLEX_TYPE)
2496 {
2497 tree scalar = build_one_cst (TREE_TYPE (type));
2498 return build_complex (type, scalar, scalar);
2499 }
2500 else
2501 return build_one_cst (type);
2502 }
2503
2504 /* Return a constant of arithmetic type TYPE which is the
2505 multiplicative identity of the set TYPE. */
2506
2507 tree
2508 build_one_cst (tree type)
2509 {
2510 switch (TREE_CODE (type))
2511 {
2512 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2513 case POINTER_TYPE: case REFERENCE_TYPE:
2514 case OFFSET_TYPE:
2515 return build_int_cst (type, 1);
2516
2517 case REAL_TYPE:
2518 return build_real (type, dconst1);
2519
2520 case FIXED_POINT_TYPE:
2521 /* We can only generate 1 for accum types. */
2522 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2523 return build_fixed (type, FCONST1 (TYPE_MODE (type)));
2524
2525 case VECTOR_TYPE:
2526 {
2527 tree scalar = build_one_cst (TREE_TYPE (type));
2528
2529 return build_vector_from_val (type, scalar);
2530 }
2531
2532 case COMPLEX_TYPE:
2533 return build_complex (type,
2534 build_one_cst (TREE_TYPE (type)),
2535 build_zero_cst (TREE_TYPE (type)));
2536
2537 default:
2538 gcc_unreachable ();
2539 }
2540 }
2541
2542 /* Return an integer of type TYPE containing all 1's in as much precision as
2543 it contains, or a complex or vector whose subparts are such integers. */
2544
2545 tree
2546 build_all_ones_cst (tree type)
2547 {
2548 if (TREE_CODE (type) == COMPLEX_TYPE)
2549 {
2550 tree scalar = build_all_ones_cst (TREE_TYPE (type));
2551 return build_complex (type, scalar, scalar);
2552 }
2553 else
2554 return build_minus_one_cst (type);
2555 }
2556
2557 /* Return a constant of arithmetic type TYPE which is the
2558 opposite of the multiplicative identity of the set TYPE. */
2559
2560 tree
2561 build_minus_one_cst (tree type)
2562 {
2563 switch (TREE_CODE (type))
2564 {
2565 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2566 case POINTER_TYPE: case REFERENCE_TYPE:
2567 case OFFSET_TYPE:
2568 return build_int_cst (type, -1);
2569
2570 case REAL_TYPE:
2571 return build_real (type, dconstm1);
2572
2573 case FIXED_POINT_TYPE:
2574 /* We can only generate 1 for accum types. */
2575 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2576 return build_fixed (type,
2577 fixed_from_double_int (double_int_minus_one,
2578 SCALAR_TYPE_MODE (type)));
2579
2580 case VECTOR_TYPE:
2581 {
2582 tree scalar = build_minus_one_cst (TREE_TYPE (type));
2583
2584 return build_vector_from_val (type, scalar);
2585 }
2586
2587 case COMPLEX_TYPE:
2588 return build_complex (type,
2589 build_minus_one_cst (TREE_TYPE (type)),
2590 build_zero_cst (TREE_TYPE (type)));
2591
2592 default:
2593 gcc_unreachable ();
2594 }
2595 }
2596
2597 /* Build 0 constant of type TYPE. This is used by constructor folding
2598 and thus the constant should be represented in memory by
2599 zero(es). */
2600
2601 tree
2602 build_zero_cst (tree type)
2603 {
2604 switch (TREE_CODE (type))
2605 {
2606 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2607 case POINTER_TYPE: case REFERENCE_TYPE:
2608 case OFFSET_TYPE: case NULLPTR_TYPE:
2609 return build_int_cst (type, 0);
2610
2611 case REAL_TYPE:
2612 return build_real (type, dconst0);
2613
2614 case FIXED_POINT_TYPE:
2615 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
2616
2617 case VECTOR_TYPE:
2618 {
2619 tree scalar = build_zero_cst (TREE_TYPE (type));
2620
2621 return build_vector_from_val (type, scalar);
2622 }
2623
2624 case COMPLEX_TYPE:
2625 {
2626 tree zero = build_zero_cst (TREE_TYPE (type));
2627
2628 return build_complex (type, zero, zero);
2629 }
2630
2631 default:
2632 if (!AGGREGATE_TYPE_P (type))
2633 return fold_convert (type, integer_zero_node);
2634 return build_constructor (type, NULL);
2635 }
2636 }
2637
2638
2639 /* Build a BINFO with LEN language slots. */
2640
2641 tree
2642 make_tree_binfo (unsigned base_binfos MEM_STAT_DECL)
2643 {
2644 tree t;
2645 size_t length = (offsetof (struct tree_binfo, base_binfos)
2646 + vec<tree, va_gc>::embedded_size (base_binfos));
2647
2648 record_node_allocation_statistics (TREE_BINFO, length);
2649
2650 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
2651
2652 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
2653
2654 TREE_SET_CODE (t, TREE_BINFO);
2655
2656 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
2657
2658 return t;
2659 }
2660
2661 /* Create a CASE_LABEL_EXPR tree node and return it. */
2662
2663 tree
2664 build_case_label (tree low_value, tree high_value, tree label_decl)
2665 {
2666 tree t = make_node (CASE_LABEL_EXPR);
2667
2668 TREE_TYPE (t) = void_type_node;
2669 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
2670
2671 CASE_LOW (t) = low_value;
2672 CASE_HIGH (t) = high_value;
2673 CASE_LABEL (t) = label_decl;
2674 CASE_CHAIN (t) = NULL_TREE;
2675
2676 return t;
2677 }
2678
2679 /* Build a newly constructed INTEGER_CST node. LEN and EXT_LEN are the
2680 values of TREE_INT_CST_NUNITS and TREE_INT_CST_EXT_NUNITS respectively.
2681 The latter determines the length of the HOST_WIDE_INT vector. */
2682
2683 tree
2684 make_int_cst (int len, int ext_len MEM_STAT_DECL)
2685 {
2686 tree t;
2687 int length = ((ext_len - 1) * sizeof (HOST_WIDE_INT)
2688 + sizeof (struct tree_int_cst));
2689
2690 gcc_assert (len);
2691 record_node_allocation_statistics (INTEGER_CST, length);
2692
2693 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2694
2695 TREE_SET_CODE (t, INTEGER_CST);
2696 TREE_INT_CST_NUNITS (t) = len;
2697 TREE_INT_CST_EXT_NUNITS (t) = ext_len;
2698 /* to_offset can only be applied to trees that are offset_int-sized
2699 or smaller. EXT_LEN is correct if it fits, otherwise the constant
2700 must be exactly the precision of offset_int and so LEN is correct. */
2701 if (ext_len <= OFFSET_INT_ELTS)
2702 TREE_INT_CST_OFFSET_NUNITS (t) = ext_len;
2703 else
2704 TREE_INT_CST_OFFSET_NUNITS (t) = len;
2705
2706 TREE_CONSTANT (t) = 1;
2707
2708 return t;
2709 }
2710
2711 /* Build a newly constructed TREE_VEC node of length LEN. */
2712
2713 tree
2714 make_tree_vec (int len MEM_STAT_DECL)
2715 {
2716 tree t;
2717 size_t length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2718
2719 record_node_allocation_statistics (TREE_VEC, length);
2720
2721 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2722
2723 TREE_SET_CODE (t, TREE_VEC);
2724 TREE_VEC_LENGTH (t) = len;
2725
2726 return t;
2727 }
2728
2729 /* Grow a TREE_VEC node to new length LEN. */
2730
2731 tree
2732 grow_tree_vec (tree v, int len MEM_STAT_DECL)
2733 {
2734 gcc_assert (TREE_CODE (v) == TREE_VEC);
2735
2736 int oldlen = TREE_VEC_LENGTH (v);
2737 gcc_assert (len > oldlen);
2738
2739 size_t oldlength = (oldlen - 1) * sizeof (tree) + sizeof (struct tree_vec);
2740 size_t length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2741
2742 record_node_allocation_statistics (TREE_VEC, length - oldlength);
2743
2744 v = (tree) ggc_realloc (v, length PASS_MEM_STAT);
2745
2746 TREE_VEC_LENGTH (v) = len;
2747
2748 return v;
2749 }
2750 \f
2751 /* Return 1 if EXPR is the constant zero, whether it is integral, float or
2752 fixed, and scalar, complex or vector. */
2753
2754 bool
2755 zerop (const_tree expr)
2756 {
2757 return (integer_zerop (expr)
2758 || real_zerop (expr)
2759 || fixed_zerop (expr));
2760 }
2761
2762 /* Return 1 if EXPR is the integer constant zero or a complex constant
2763 of zero, or a location wrapper for such a constant. */
2764
2765 bool
2766 integer_zerop (const_tree expr)
2767 {
2768 STRIP_ANY_LOCATION_WRAPPER (expr);
2769
2770 switch (TREE_CODE (expr))
2771 {
2772 case INTEGER_CST:
2773 return wi::to_wide (expr) == 0;
2774 case COMPLEX_CST:
2775 return (integer_zerop (TREE_REALPART (expr))
2776 && integer_zerop (TREE_IMAGPART (expr)));
2777 case VECTOR_CST:
2778 return (VECTOR_CST_NPATTERNS (expr) == 1
2779 && VECTOR_CST_DUPLICATE_P (expr)
2780 && integer_zerop (VECTOR_CST_ENCODED_ELT (expr, 0)));
2781 default:
2782 return false;
2783 }
2784 }
2785
2786 /* Return 1 if EXPR is the integer constant one or the corresponding
2787 complex constant, or a location wrapper for such a constant. */
2788
2789 bool
2790 integer_onep (const_tree expr)
2791 {
2792 STRIP_ANY_LOCATION_WRAPPER (expr);
2793
2794 switch (TREE_CODE (expr))
2795 {
2796 case INTEGER_CST:
2797 return wi::eq_p (wi::to_widest (expr), 1);
2798 case COMPLEX_CST:
2799 return (integer_onep (TREE_REALPART (expr))
2800 && integer_zerop (TREE_IMAGPART (expr)));
2801 case VECTOR_CST:
2802 return (VECTOR_CST_NPATTERNS (expr) == 1
2803 && VECTOR_CST_DUPLICATE_P (expr)
2804 && integer_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
2805 default:
2806 return false;
2807 }
2808 }
2809
2810 /* Return 1 if EXPR is the integer constant one. For complex and vector,
2811 return 1 if every piece is the integer constant one.
2812 Also return 1 for location wrappers for such a constant. */
2813
2814 bool
2815 integer_each_onep (const_tree expr)
2816 {
2817 STRIP_ANY_LOCATION_WRAPPER (expr);
2818
2819 if (TREE_CODE (expr) == COMPLEX_CST)
2820 return (integer_onep (TREE_REALPART (expr))
2821 && integer_onep (TREE_IMAGPART (expr)));
2822 else
2823 return integer_onep (expr);
2824 }
2825
2826 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
2827 it contains, or a complex or vector whose subparts are such integers,
2828 or a location wrapper for such a constant. */
2829
2830 bool
2831 integer_all_onesp (const_tree expr)
2832 {
2833 STRIP_ANY_LOCATION_WRAPPER (expr);
2834
2835 if (TREE_CODE (expr) == COMPLEX_CST
2836 && integer_all_onesp (TREE_REALPART (expr))
2837 && integer_all_onesp (TREE_IMAGPART (expr)))
2838 return true;
2839
2840 else if (TREE_CODE (expr) == VECTOR_CST)
2841 return (VECTOR_CST_NPATTERNS (expr) == 1
2842 && VECTOR_CST_DUPLICATE_P (expr)
2843 && integer_all_onesp (VECTOR_CST_ENCODED_ELT (expr, 0)));
2844
2845 else if (TREE_CODE (expr) != INTEGER_CST)
2846 return false;
2847
2848 return (wi::max_value (TYPE_PRECISION (TREE_TYPE (expr)), UNSIGNED)
2849 == wi::to_wide (expr));
2850 }
2851
2852 /* Return 1 if EXPR is the integer constant minus one, or a location wrapper
2853 for such a constant. */
2854
2855 bool
2856 integer_minus_onep (const_tree expr)
2857 {
2858 STRIP_ANY_LOCATION_WRAPPER (expr);
2859
2860 if (TREE_CODE (expr) == COMPLEX_CST)
2861 return (integer_all_onesp (TREE_REALPART (expr))
2862 && integer_zerop (TREE_IMAGPART (expr)));
2863 else
2864 return integer_all_onesp (expr);
2865 }
2866
2867 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
2868 one bit on), or a location wrapper for such a constant. */
2869
2870 bool
2871 integer_pow2p (const_tree expr)
2872 {
2873 STRIP_ANY_LOCATION_WRAPPER (expr);
2874
2875 if (TREE_CODE (expr) == COMPLEX_CST
2876 && integer_pow2p (TREE_REALPART (expr))
2877 && integer_zerop (TREE_IMAGPART (expr)))
2878 return true;
2879
2880 if (TREE_CODE (expr) != INTEGER_CST)
2881 return false;
2882
2883 return wi::popcount (wi::to_wide (expr)) == 1;
2884 }
2885
2886 /* Return 1 if EXPR is an integer constant other than zero or a
2887 complex constant other than zero, or a location wrapper for such a
2888 constant. */
2889
2890 bool
2891 integer_nonzerop (const_tree expr)
2892 {
2893 STRIP_ANY_LOCATION_WRAPPER (expr);
2894
2895 return ((TREE_CODE (expr) == INTEGER_CST
2896 && wi::to_wide (expr) != 0)
2897 || (TREE_CODE (expr) == COMPLEX_CST
2898 && (integer_nonzerop (TREE_REALPART (expr))
2899 || integer_nonzerop (TREE_IMAGPART (expr)))));
2900 }
2901
2902 /* Return 1 if EXPR is the integer constant one. For vector,
2903 return 1 if every piece is the integer constant minus one
2904 (representing the value TRUE).
2905 Also return 1 for location wrappers for such a constant. */
2906
2907 bool
2908 integer_truep (const_tree expr)
2909 {
2910 STRIP_ANY_LOCATION_WRAPPER (expr);
2911
2912 if (TREE_CODE (expr) == VECTOR_CST)
2913 return integer_all_onesp (expr);
2914 return integer_onep (expr);
2915 }
2916
2917 /* Return 1 if EXPR is the fixed-point constant zero, or a location wrapper
2918 for such a constant. */
2919
2920 bool
2921 fixed_zerop (const_tree expr)
2922 {
2923 STRIP_ANY_LOCATION_WRAPPER (expr);
2924
2925 return (TREE_CODE (expr) == FIXED_CST
2926 && TREE_FIXED_CST (expr).data.is_zero ());
2927 }
2928
2929 /* Return the power of two represented by a tree node known to be a
2930 power of two. */
2931
2932 int
2933 tree_log2 (const_tree expr)
2934 {
2935 if (TREE_CODE (expr) == COMPLEX_CST)
2936 return tree_log2 (TREE_REALPART (expr));
2937
2938 return wi::exact_log2 (wi::to_wide (expr));
2939 }
2940
2941 /* Similar, but return the largest integer Y such that 2 ** Y is less
2942 than or equal to EXPR. */
2943
2944 int
2945 tree_floor_log2 (const_tree expr)
2946 {
2947 if (TREE_CODE (expr) == COMPLEX_CST)
2948 return tree_log2 (TREE_REALPART (expr));
2949
2950 return wi::floor_log2 (wi::to_wide (expr));
2951 }
2952
2953 /* Return number of known trailing zero bits in EXPR, or, if the value of
2954 EXPR is known to be zero, the precision of it's type. */
2955
2956 unsigned int
2957 tree_ctz (const_tree expr)
2958 {
2959 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
2960 && !POINTER_TYPE_P (TREE_TYPE (expr)))
2961 return 0;
2962
2963 unsigned int ret1, ret2, prec = TYPE_PRECISION (TREE_TYPE (expr));
2964 switch (TREE_CODE (expr))
2965 {
2966 case INTEGER_CST:
2967 ret1 = wi::ctz (wi::to_wide (expr));
2968 return MIN (ret1, prec);
2969 case SSA_NAME:
2970 ret1 = wi::ctz (get_nonzero_bits (expr));
2971 return MIN (ret1, prec);
2972 case PLUS_EXPR:
2973 case MINUS_EXPR:
2974 case BIT_IOR_EXPR:
2975 case BIT_XOR_EXPR:
2976 case MIN_EXPR:
2977 case MAX_EXPR:
2978 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2979 if (ret1 == 0)
2980 return ret1;
2981 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2982 return MIN (ret1, ret2);
2983 case POINTER_PLUS_EXPR:
2984 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2985 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2986 /* Second operand is sizetype, which could be in theory
2987 wider than pointer's precision. Make sure we never
2988 return more than prec. */
2989 ret2 = MIN (ret2, prec);
2990 return MIN (ret1, ret2);
2991 case BIT_AND_EXPR:
2992 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2993 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2994 return MAX (ret1, ret2);
2995 case MULT_EXPR:
2996 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2997 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2998 return MIN (ret1 + ret2, prec);
2999 case LSHIFT_EXPR:
3000 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3001 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
3002 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
3003 {
3004 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
3005 return MIN (ret1 + ret2, prec);
3006 }
3007 return ret1;
3008 case RSHIFT_EXPR:
3009 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
3010 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
3011 {
3012 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3013 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
3014 if (ret1 > ret2)
3015 return ret1 - ret2;
3016 }
3017 return 0;
3018 case TRUNC_DIV_EXPR:
3019 case CEIL_DIV_EXPR:
3020 case FLOOR_DIV_EXPR:
3021 case ROUND_DIV_EXPR:
3022 case EXACT_DIV_EXPR:
3023 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
3024 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) == 1)
3025 {
3026 int l = tree_log2 (TREE_OPERAND (expr, 1));
3027 if (l >= 0)
3028 {
3029 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3030 ret2 = l;
3031 if (ret1 > ret2)
3032 return ret1 - ret2;
3033 }
3034 }
3035 return 0;
3036 CASE_CONVERT:
3037 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3038 if (ret1 && ret1 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
3039 ret1 = prec;
3040 return MIN (ret1, prec);
3041 case SAVE_EXPR:
3042 return tree_ctz (TREE_OPERAND (expr, 0));
3043 case COND_EXPR:
3044 ret1 = tree_ctz (TREE_OPERAND (expr, 1));
3045 if (ret1 == 0)
3046 return 0;
3047 ret2 = tree_ctz (TREE_OPERAND (expr, 2));
3048 return MIN (ret1, ret2);
3049 case COMPOUND_EXPR:
3050 return tree_ctz (TREE_OPERAND (expr, 1));
3051 case ADDR_EXPR:
3052 ret1 = get_pointer_alignment (CONST_CAST_TREE (expr));
3053 if (ret1 > BITS_PER_UNIT)
3054 {
3055 ret1 = ctz_hwi (ret1 / BITS_PER_UNIT);
3056 return MIN (ret1, prec);
3057 }
3058 return 0;
3059 default:
3060 return 0;
3061 }
3062 }
3063
3064 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
3065 decimal float constants, so don't return 1 for them.
3066 Also return 1 for location wrappers around such a constant. */
3067
3068 bool
3069 real_zerop (const_tree expr)
3070 {
3071 STRIP_ANY_LOCATION_WRAPPER (expr);
3072
3073 switch (TREE_CODE (expr))
3074 {
3075 case REAL_CST:
3076 return real_equal (&TREE_REAL_CST (expr), &dconst0)
3077 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
3078 case COMPLEX_CST:
3079 return real_zerop (TREE_REALPART (expr))
3080 && real_zerop (TREE_IMAGPART (expr));
3081 case VECTOR_CST:
3082 {
3083 /* Don't simply check for a duplicate because the predicate
3084 accepts both +0.0 and -0.0. */
3085 unsigned count = vector_cst_encoded_nelts (expr);
3086 for (unsigned int i = 0; i < count; ++i)
3087 if (!real_zerop (VECTOR_CST_ENCODED_ELT (expr, i)))
3088 return false;
3089 return true;
3090 }
3091 default:
3092 return false;
3093 }
3094 }
3095
3096 /* Return 1 if EXPR is the real constant one in real or complex form.
3097 Trailing zeroes matter for decimal float constants, so don't return
3098 1 for them.
3099 Also return 1 for location wrappers around such a constant. */
3100
3101 bool
3102 real_onep (const_tree expr)
3103 {
3104 STRIP_ANY_LOCATION_WRAPPER (expr);
3105
3106 switch (TREE_CODE (expr))
3107 {
3108 case REAL_CST:
3109 return real_equal (&TREE_REAL_CST (expr), &dconst1)
3110 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
3111 case COMPLEX_CST:
3112 return real_onep (TREE_REALPART (expr))
3113 && real_zerop (TREE_IMAGPART (expr));
3114 case VECTOR_CST:
3115 return (VECTOR_CST_NPATTERNS (expr) == 1
3116 && VECTOR_CST_DUPLICATE_P (expr)
3117 && real_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
3118 default:
3119 return false;
3120 }
3121 }
3122
3123 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
3124 matter for decimal float constants, so don't return 1 for them.
3125 Also return 1 for location wrappers around such a constant. */
3126
3127 bool
3128 real_minus_onep (const_tree expr)
3129 {
3130 STRIP_ANY_LOCATION_WRAPPER (expr);
3131
3132 switch (TREE_CODE (expr))
3133 {
3134 case REAL_CST:
3135 return real_equal (&TREE_REAL_CST (expr), &dconstm1)
3136 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
3137 case COMPLEX_CST:
3138 return real_minus_onep (TREE_REALPART (expr))
3139 && real_zerop (TREE_IMAGPART (expr));
3140 case VECTOR_CST:
3141 return (VECTOR_CST_NPATTERNS (expr) == 1
3142 && VECTOR_CST_DUPLICATE_P (expr)
3143 && real_minus_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
3144 default:
3145 return false;
3146 }
3147 }
3148
3149 /* Nonzero if EXP is a constant or a cast of a constant. */
3150
3151 bool
3152 really_constant_p (const_tree exp)
3153 {
3154 /* This is not quite the same as STRIP_NOPS. It does more. */
3155 while (CONVERT_EXPR_P (exp)
3156 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3157 exp = TREE_OPERAND (exp, 0);
3158 return TREE_CONSTANT (exp);
3159 }
3160
3161 /* Return true if T holds a polynomial pointer difference, storing it in
3162 *VALUE if so. A true return means that T's precision is no greater
3163 than 64 bits, which is the largest address space we support, so *VALUE
3164 never loses precision. However, the signedness of the result does
3165 not necessarily match the signedness of T: sometimes an unsigned type
3166 like sizetype is used to encode a value that is actually negative. */
3167
3168 bool
3169 ptrdiff_tree_p (const_tree t, poly_int64_pod *value)
3170 {
3171 if (!t)
3172 return false;
3173 if (TREE_CODE (t) == INTEGER_CST)
3174 {
3175 if (!cst_and_fits_in_hwi (t))
3176 return false;
3177 *value = int_cst_value (t);
3178 return true;
3179 }
3180 if (POLY_INT_CST_P (t))
3181 {
3182 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
3183 if (!cst_and_fits_in_hwi (POLY_INT_CST_COEFF (t, i)))
3184 return false;
3185 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
3186 value->coeffs[i] = int_cst_value (POLY_INT_CST_COEFF (t, i));
3187 return true;
3188 }
3189 return false;
3190 }
3191
3192 poly_int64
3193 tree_to_poly_int64 (const_tree t)
3194 {
3195 gcc_assert (tree_fits_poly_int64_p (t));
3196 if (POLY_INT_CST_P (t))
3197 return poly_int_cst_value (t).force_shwi ();
3198 return TREE_INT_CST_LOW (t);
3199 }
3200
3201 poly_uint64
3202 tree_to_poly_uint64 (const_tree t)
3203 {
3204 gcc_assert (tree_fits_poly_uint64_p (t));
3205 if (POLY_INT_CST_P (t))
3206 return poly_int_cst_value (t).force_uhwi ();
3207 return TREE_INT_CST_LOW (t);
3208 }
3209 \f
3210 /* Return first list element whose TREE_VALUE is ELEM.
3211 Return 0 if ELEM is not in LIST. */
3212
3213 tree
3214 value_member (tree elem, tree list)
3215 {
3216 while (list)
3217 {
3218 if (elem == TREE_VALUE (list))
3219 return list;
3220 list = TREE_CHAIN (list);
3221 }
3222 return NULL_TREE;
3223 }
3224
3225 /* Return first list element whose TREE_PURPOSE is ELEM.
3226 Return 0 if ELEM is not in LIST. */
3227
3228 tree
3229 purpose_member (const_tree elem, tree list)
3230 {
3231 while (list)
3232 {
3233 if (elem == TREE_PURPOSE (list))
3234 return list;
3235 list = TREE_CHAIN (list);
3236 }
3237 return NULL_TREE;
3238 }
3239
3240 /* Return true if ELEM is in V. */
3241
3242 bool
3243 vec_member (const_tree elem, vec<tree, va_gc> *v)
3244 {
3245 unsigned ix;
3246 tree t;
3247 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
3248 if (elem == t)
3249 return true;
3250 return false;
3251 }
3252
3253 /* Returns element number IDX (zero-origin) of chain CHAIN, or
3254 NULL_TREE. */
3255
3256 tree
3257 chain_index (int idx, tree chain)
3258 {
3259 for (; chain && idx > 0; --idx)
3260 chain = TREE_CHAIN (chain);
3261 return chain;
3262 }
3263
3264 /* Return nonzero if ELEM is part of the chain CHAIN. */
3265
3266 bool
3267 chain_member (const_tree elem, const_tree chain)
3268 {
3269 while (chain)
3270 {
3271 if (elem == chain)
3272 return true;
3273 chain = DECL_CHAIN (chain);
3274 }
3275
3276 return false;
3277 }
3278
3279 /* Return the length of a chain of nodes chained through TREE_CHAIN.
3280 We expect a null pointer to mark the end of the chain.
3281 This is the Lisp primitive `length'. */
3282
3283 int
3284 list_length (const_tree t)
3285 {
3286 const_tree p = t;
3287 #ifdef ENABLE_TREE_CHECKING
3288 const_tree q = t;
3289 #endif
3290 int len = 0;
3291
3292 while (p)
3293 {
3294 p = TREE_CHAIN (p);
3295 #ifdef ENABLE_TREE_CHECKING
3296 if (len % 2)
3297 q = TREE_CHAIN (q);
3298 gcc_assert (p != q);
3299 #endif
3300 len++;
3301 }
3302
3303 return len;
3304 }
3305
3306 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
3307 UNION_TYPE TYPE, or NULL_TREE if none. */
3308
3309 tree
3310 first_field (const_tree type)
3311 {
3312 tree t = TYPE_FIELDS (type);
3313 while (t && TREE_CODE (t) != FIELD_DECL)
3314 t = TREE_CHAIN (t);
3315 return t;
3316 }
3317
3318 /* Returns the last FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
3319 UNION_TYPE TYPE, or NULL_TREE if none. */
3320
3321 tree
3322 last_field (const_tree type)
3323 {
3324 tree last = NULL_TREE;
3325
3326 for (tree fld = TYPE_FIELDS (type); fld; fld = TREE_CHAIN (fld))
3327 {
3328 if (TREE_CODE (fld) != FIELD_DECL)
3329 continue;
3330
3331 last = fld;
3332 }
3333
3334 return last;
3335 }
3336
3337 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
3338 by modifying the last node in chain 1 to point to chain 2.
3339 This is the Lisp primitive `nconc'. */
3340
3341 tree
3342 chainon (tree op1, tree op2)
3343 {
3344 tree t1;
3345
3346 if (!op1)
3347 return op2;
3348 if (!op2)
3349 return op1;
3350
3351 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
3352 continue;
3353 TREE_CHAIN (t1) = op2;
3354
3355 #ifdef ENABLE_TREE_CHECKING
3356 {
3357 tree t2;
3358 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
3359 gcc_assert (t2 != t1);
3360 }
3361 #endif
3362
3363 return op1;
3364 }
3365
3366 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
3367
3368 tree
3369 tree_last (tree chain)
3370 {
3371 tree next;
3372 if (chain)
3373 while ((next = TREE_CHAIN (chain)))
3374 chain = next;
3375 return chain;
3376 }
3377
3378 /* Reverse the order of elements in the chain T,
3379 and return the new head of the chain (old last element). */
3380
3381 tree
3382 nreverse (tree t)
3383 {
3384 tree prev = 0, decl, next;
3385 for (decl = t; decl; decl = next)
3386 {
3387 /* We shouldn't be using this function to reverse BLOCK chains; we
3388 have blocks_nreverse for that. */
3389 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
3390 next = TREE_CHAIN (decl);
3391 TREE_CHAIN (decl) = prev;
3392 prev = decl;
3393 }
3394 return prev;
3395 }
3396 \f
3397 /* Return a newly created TREE_LIST node whose
3398 purpose and value fields are PARM and VALUE. */
3399
3400 tree
3401 build_tree_list (tree parm, tree value MEM_STAT_DECL)
3402 {
3403 tree t = make_node (TREE_LIST PASS_MEM_STAT);
3404 TREE_PURPOSE (t) = parm;
3405 TREE_VALUE (t) = value;
3406 return t;
3407 }
3408
3409 /* Build a chain of TREE_LIST nodes from a vector. */
3410
3411 tree
3412 build_tree_list_vec (const vec<tree, va_gc> *vec MEM_STAT_DECL)
3413 {
3414 tree ret = NULL_TREE;
3415 tree *pp = &ret;
3416 unsigned int i;
3417 tree t;
3418 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
3419 {
3420 *pp = build_tree_list (NULL, t PASS_MEM_STAT);
3421 pp = &TREE_CHAIN (*pp);
3422 }
3423 return ret;
3424 }
3425
3426 /* Return a newly created TREE_LIST node whose
3427 purpose and value fields are PURPOSE and VALUE
3428 and whose TREE_CHAIN is CHAIN. */
3429
3430 tree
3431 tree_cons (tree purpose, tree value, tree chain MEM_STAT_DECL)
3432 {
3433 tree node;
3434
3435 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
3436 memset (node, 0, sizeof (struct tree_common));
3437
3438 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
3439
3440 TREE_SET_CODE (node, TREE_LIST);
3441 TREE_CHAIN (node) = chain;
3442 TREE_PURPOSE (node) = purpose;
3443 TREE_VALUE (node) = value;
3444 return node;
3445 }
3446
3447 /* Return the values of the elements of a CONSTRUCTOR as a vector of
3448 trees. */
3449
3450 vec<tree, va_gc> *
3451 ctor_to_vec (tree ctor)
3452 {
3453 vec<tree, va_gc> *vec;
3454 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
3455 unsigned int ix;
3456 tree val;
3457
3458 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
3459 vec->quick_push (val);
3460
3461 return vec;
3462 }
3463 \f
3464 /* Return the size nominally occupied by an object of type TYPE
3465 when it resides in memory. The value is measured in units of bytes,
3466 and its data type is that normally used for type sizes
3467 (which is the first type created by make_signed_type or
3468 make_unsigned_type). */
3469
3470 tree
3471 size_in_bytes_loc (location_t loc, const_tree type)
3472 {
3473 tree t;
3474
3475 if (type == error_mark_node)
3476 return integer_zero_node;
3477
3478 type = TYPE_MAIN_VARIANT (type);
3479 t = TYPE_SIZE_UNIT (type);
3480
3481 if (t == 0)
3482 {
3483 lang_hooks.types.incomplete_type_error (loc, NULL_TREE, type);
3484 return size_zero_node;
3485 }
3486
3487 return t;
3488 }
3489
3490 /* Return the size of TYPE (in bytes) as a wide integer
3491 or return -1 if the size can vary or is larger than an integer. */
3492
3493 HOST_WIDE_INT
3494 int_size_in_bytes (const_tree type)
3495 {
3496 tree t;
3497
3498 if (type == error_mark_node)
3499 return 0;
3500
3501 type = TYPE_MAIN_VARIANT (type);
3502 t = TYPE_SIZE_UNIT (type);
3503
3504 if (t && tree_fits_uhwi_p (t))
3505 return TREE_INT_CST_LOW (t);
3506 else
3507 return -1;
3508 }
3509
3510 /* Return the maximum size of TYPE (in bytes) as a wide integer
3511 or return -1 if the size can vary or is larger than an integer. */
3512
3513 HOST_WIDE_INT
3514 max_int_size_in_bytes (const_tree type)
3515 {
3516 HOST_WIDE_INT size = -1;
3517 tree size_tree;
3518
3519 /* If this is an array type, check for a possible MAX_SIZE attached. */
3520
3521 if (TREE_CODE (type) == ARRAY_TYPE)
3522 {
3523 size_tree = TYPE_ARRAY_MAX_SIZE (type);
3524
3525 if (size_tree && tree_fits_uhwi_p (size_tree))
3526 size = tree_to_uhwi (size_tree);
3527 }
3528
3529 /* If we still haven't been able to get a size, see if the language
3530 can compute a maximum size. */
3531
3532 if (size == -1)
3533 {
3534 size_tree = lang_hooks.types.max_size (type);
3535
3536 if (size_tree && tree_fits_uhwi_p (size_tree))
3537 size = tree_to_uhwi (size_tree);
3538 }
3539
3540 return size;
3541 }
3542 \f
3543 /* Return the bit position of FIELD, in bits from the start of the record.
3544 This is a tree of type bitsizetype. */
3545
3546 tree
3547 bit_position (const_tree field)
3548 {
3549 return bit_from_pos (DECL_FIELD_OFFSET (field),
3550 DECL_FIELD_BIT_OFFSET (field));
3551 }
3552 \f
3553 /* Return the byte position of FIELD, in bytes from the start of the record.
3554 This is a tree of type sizetype. */
3555
3556 tree
3557 byte_position (const_tree field)
3558 {
3559 return byte_from_pos (DECL_FIELD_OFFSET (field),
3560 DECL_FIELD_BIT_OFFSET (field));
3561 }
3562
3563 /* Likewise, but return as an integer. It must be representable in
3564 that way (since it could be a signed value, we don't have the
3565 option of returning -1 like int_size_in_byte can. */
3566
3567 HOST_WIDE_INT
3568 int_byte_position (const_tree field)
3569 {
3570 return tree_to_shwi (byte_position (field));
3571 }
3572 \f
3573 /* Return, as a tree node, the number of elements for TYPE (which is an
3574 ARRAY_TYPE) minus one. This counts only elements of the top array. */
3575
3576 tree
3577 array_type_nelts (const_tree type)
3578 {
3579 tree index_type, min, max;
3580
3581 /* If they did it with unspecified bounds, then we should have already
3582 given an error about it before we got here. */
3583 if (! TYPE_DOMAIN (type))
3584 return error_mark_node;
3585
3586 index_type = TYPE_DOMAIN (type);
3587 min = TYPE_MIN_VALUE (index_type);
3588 max = TYPE_MAX_VALUE (index_type);
3589
3590 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
3591 if (!max)
3592 {
3593 /* zero sized arrays are represented from C FE as complete types with
3594 NULL TYPE_MAX_VALUE and zero TYPE_SIZE, while C++ FE represents
3595 them as min 0, max -1. */
3596 if (COMPLETE_TYPE_P (type)
3597 && integer_zerop (TYPE_SIZE (type))
3598 && integer_zerop (min))
3599 return build_int_cst (TREE_TYPE (min), -1);
3600
3601 return error_mark_node;
3602 }
3603
3604 return (integer_zerop (min)
3605 ? max
3606 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
3607 }
3608 \f
3609 /* If arg is static -- a reference to an object in static storage -- then
3610 return the object. This is not the same as the C meaning of `static'.
3611 If arg isn't static, return NULL. */
3612
3613 tree
3614 staticp (tree arg)
3615 {
3616 switch (TREE_CODE (arg))
3617 {
3618 case FUNCTION_DECL:
3619 /* Nested functions are static, even though taking their address will
3620 involve a trampoline as we unnest the nested function and create
3621 the trampoline on the tree level. */
3622 return arg;
3623
3624 case VAR_DECL:
3625 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3626 && ! DECL_THREAD_LOCAL_P (arg)
3627 && ! DECL_DLLIMPORT_P (arg)
3628 ? arg : NULL);
3629
3630 case CONST_DECL:
3631 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3632 ? arg : NULL);
3633
3634 case CONSTRUCTOR:
3635 return TREE_STATIC (arg) ? arg : NULL;
3636
3637 case LABEL_DECL:
3638 case STRING_CST:
3639 return arg;
3640
3641 case COMPONENT_REF:
3642 /* If the thing being referenced is not a field, then it is
3643 something language specific. */
3644 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
3645
3646 /* If we are referencing a bitfield, we can't evaluate an
3647 ADDR_EXPR at compile time and so it isn't a constant. */
3648 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
3649 return NULL;
3650
3651 return staticp (TREE_OPERAND (arg, 0));
3652
3653 case BIT_FIELD_REF:
3654 return NULL;
3655
3656 case INDIRECT_REF:
3657 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
3658
3659 case ARRAY_REF:
3660 case ARRAY_RANGE_REF:
3661 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
3662 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
3663 return staticp (TREE_OPERAND (arg, 0));
3664 else
3665 return NULL;
3666
3667 case COMPOUND_LITERAL_EXPR:
3668 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
3669
3670 default:
3671 return NULL;
3672 }
3673 }
3674
3675 \f
3676
3677
3678 /* Return whether OP is a DECL whose address is function-invariant. */
3679
3680 bool
3681 decl_address_invariant_p (const_tree op)
3682 {
3683 /* The conditions below are slightly less strict than the one in
3684 staticp. */
3685
3686 switch (TREE_CODE (op))
3687 {
3688 case PARM_DECL:
3689 case RESULT_DECL:
3690 case LABEL_DECL:
3691 case FUNCTION_DECL:
3692 return true;
3693
3694 case VAR_DECL:
3695 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3696 || DECL_THREAD_LOCAL_P (op)
3697 || DECL_CONTEXT (op) == current_function_decl
3698 || decl_function_context (op) == current_function_decl)
3699 return true;
3700 break;
3701
3702 case CONST_DECL:
3703 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3704 || decl_function_context (op) == current_function_decl)
3705 return true;
3706 break;
3707
3708 default:
3709 break;
3710 }
3711
3712 return false;
3713 }
3714
3715 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
3716
3717 bool
3718 decl_address_ip_invariant_p (const_tree op)
3719 {
3720 /* The conditions below are slightly less strict than the one in
3721 staticp. */
3722
3723 switch (TREE_CODE (op))
3724 {
3725 case LABEL_DECL:
3726 case FUNCTION_DECL:
3727 case STRING_CST:
3728 return true;
3729
3730 case VAR_DECL:
3731 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
3732 && !DECL_DLLIMPORT_P (op))
3733 || DECL_THREAD_LOCAL_P (op))
3734 return true;
3735 break;
3736
3737 case CONST_DECL:
3738 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
3739 return true;
3740 break;
3741
3742 default:
3743 break;
3744 }
3745
3746 return false;
3747 }
3748
3749
3750 /* Return true if T is function-invariant (internal function, does
3751 not handle arithmetic; that's handled in skip_simple_arithmetic and
3752 tree_invariant_p). */
3753
3754 static bool
3755 tree_invariant_p_1 (tree t)
3756 {
3757 tree op;
3758
3759 if (TREE_CONSTANT (t)
3760 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
3761 return true;
3762
3763 switch (TREE_CODE (t))
3764 {
3765 case SAVE_EXPR:
3766 return true;
3767
3768 case ADDR_EXPR:
3769 op = TREE_OPERAND (t, 0);
3770 while (handled_component_p (op))
3771 {
3772 switch (TREE_CODE (op))
3773 {
3774 case ARRAY_REF:
3775 case ARRAY_RANGE_REF:
3776 if (!tree_invariant_p (TREE_OPERAND (op, 1))
3777 || TREE_OPERAND (op, 2) != NULL_TREE
3778 || TREE_OPERAND (op, 3) != NULL_TREE)
3779 return false;
3780 break;
3781
3782 case COMPONENT_REF:
3783 if (TREE_OPERAND (op, 2) != NULL_TREE)
3784 return false;
3785 break;
3786
3787 default:;
3788 }
3789 op = TREE_OPERAND (op, 0);
3790 }
3791
3792 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
3793
3794 default:
3795 break;
3796 }
3797
3798 return false;
3799 }
3800
3801 /* Return true if T is function-invariant. */
3802
3803 bool
3804 tree_invariant_p (tree t)
3805 {
3806 tree inner = skip_simple_arithmetic (t);
3807 return tree_invariant_p_1 (inner);
3808 }
3809
3810 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
3811 Do this to any expression which may be used in more than one place,
3812 but must be evaluated only once.
3813
3814 Normally, expand_expr would reevaluate the expression each time.
3815 Calling save_expr produces something that is evaluated and recorded
3816 the first time expand_expr is called on it. Subsequent calls to
3817 expand_expr just reuse the recorded value.
3818
3819 The call to expand_expr that generates code that actually computes
3820 the value is the first call *at compile time*. Subsequent calls
3821 *at compile time* generate code to use the saved value.
3822 This produces correct result provided that *at run time* control
3823 always flows through the insns made by the first expand_expr
3824 before reaching the other places where the save_expr was evaluated.
3825 You, the caller of save_expr, must make sure this is so.
3826
3827 Constants, and certain read-only nodes, are returned with no
3828 SAVE_EXPR because that is safe. Expressions containing placeholders
3829 are not touched; see tree.def for an explanation of what these
3830 are used for. */
3831
3832 tree
3833 save_expr (tree expr)
3834 {
3835 tree inner;
3836
3837 /* If the tree evaluates to a constant, then we don't want to hide that
3838 fact (i.e. this allows further folding, and direct checks for constants).
3839 However, a read-only object that has side effects cannot be bypassed.
3840 Since it is no problem to reevaluate literals, we just return the
3841 literal node. */
3842 inner = skip_simple_arithmetic (expr);
3843 if (TREE_CODE (inner) == ERROR_MARK)
3844 return inner;
3845
3846 if (tree_invariant_p_1 (inner))
3847 return expr;
3848
3849 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
3850 it means that the size or offset of some field of an object depends on
3851 the value within another field.
3852
3853 Note that it must not be the case that EXPR contains both a PLACEHOLDER_EXPR
3854 and some variable since it would then need to be both evaluated once and
3855 evaluated more than once. Front-ends must assure this case cannot
3856 happen by surrounding any such subexpressions in their own SAVE_EXPR
3857 and forcing evaluation at the proper time. */
3858 if (contains_placeholder_p (inner))
3859 return expr;
3860
3861 expr = build1_loc (EXPR_LOCATION (expr), SAVE_EXPR, TREE_TYPE (expr), expr);
3862
3863 /* This expression might be placed ahead of a jump to ensure that the
3864 value was computed on both sides of the jump. So make sure it isn't
3865 eliminated as dead. */
3866 TREE_SIDE_EFFECTS (expr) = 1;
3867 return expr;
3868 }
3869
3870 /* Look inside EXPR into any simple arithmetic operations. Return the
3871 outermost non-arithmetic or non-invariant node. */
3872
3873 tree
3874 skip_simple_arithmetic (tree expr)
3875 {
3876 /* We don't care about whether this can be used as an lvalue in this
3877 context. */
3878 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3879 expr = TREE_OPERAND (expr, 0);
3880
3881 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
3882 a constant, it will be more efficient to not make another SAVE_EXPR since
3883 it will allow better simplification and GCSE will be able to merge the
3884 computations if they actually occur. */
3885 while (true)
3886 {
3887 if (UNARY_CLASS_P (expr))
3888 expr = TREE_OPERAND (expr, 0);
3889 else if (BINARY_CLASS_P (expr))
3890 {
3891 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
3892 expr = TREE_OPERAND (expr, 0);
3893 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
3894 expr = TREE_OPERAND (expr, 1);
3895 else
3896 break;
3897 }
3898 else
3899 break;
3900 }
3901
3902 return expr;
3903 }
3904
3905 /* Look inside EXPR into simple arithmetic operations involving constants.
3906 Return the outermost non-arithmetic or non-constant node. */
3907
3908 tree
3909 skip_simple_constant_arithmetic (tree expr)
3910 {
3911 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3912 expr = TREE_OPERAND (expr, 0);
3913
3914 while (true)
3915 {
3916 if (UNARY_CLASS_P (expr))
3917 expr = TREE_OPERAND (expr, 0);
3918 else if (BINARY_CLASS_P (expr))
3919 {
3920 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3921 expr = TREE_OPERAND (expr, 0);
3922 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3923 expr = TREE_OPERAND (expr, 1);
3924 else
3925 break;
3926 }
3927 else
3928 break;
3929 }
3930
3931 return expr;
3932 }
3933
3934 /* Return which tree structure is used by T. */
3935
3936 enum tree_node_structure_enum
3937 tree_node_structure (const_tree t)
3938 {
3939 const enum tree_code code = TREE_CODE (t);
3940 return tree_node_structure_for_code (code);
3941 }
3942
3943 /* Set various status flags when building a CALL_EXPR object T. */
3944
3945 static void
3946 process_call_operands (tree t)
3947 {
3948 bool side_effects = TREE_SIDE_EFFECTS (t);
3949 bool read_only = false;
3950 int i = call_expr_flags (t);
3951
3952 /* Calls have side-effects, except those to const or pure functions. */
3953 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
3954 side_effects = true;
3955 /* Propagate TREE_READONLY of arguments for const functions. */
3956 if (i & ECF_CONST)
3957 read_only = true;
3958
3959 if (!side_effects || read_only)
3960 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
3961 {
3962 tree op = TREE_OPERAND (t, i);
3963 if (op && TREE_SIDE_EFFECTS (op))
3964 side_effects = true;
3965 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
3966 read_only = false;
3967 }
3968
3969 TREE_SIDE_EFFECTS (t) = side_effects;
3970 TREE_READONLY (t) = read_only;
3971 }
3972 \f
3973 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
3974 size or offset that depends on a field within a record. */
3975
3976 bool
3977 contains_placeholder_p (const_tree exp)
3978 {
3979 enum tree_code code;
3980
3981 if (!exp)
3982 return 0;
3983
3984 code = TREE_CODE (exp);
3985 if (code == PLACEHOLDER_EXPR)
3986 return 1;
3987
3988 switch (TREE_CODE_CLASS (code))
3989 {
3990 case tcc_reference:
3991 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
3992 position computations since they will be converted into a
3993 WITH_RECORD_EXPR involving the reference, which will assume
3994 here will be valid. */
3995 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3996
3997 case tcc_exceptional:
3998 if (code == TREE_LIST)
3999 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
4000 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
4001 break;
4002
4003 case tcc_unary:
4004 case tcc_binary:
4005 case tcc_comparison:
4006 case tcc_expression:
4007 switch (code)
4008 {
4009 case COMPOUND_EXPR:
4010 /* Ignoring the first operand isn't quite right, but works best. */
4011 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
4012
4013 case COND_EXPR:
4014 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
4015 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
4016 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
4017
4018 case SAVE_EXPR:
4019 /* The save_expr function never wraps anything containing
4020 a PLACEHOLDER_EXPR. */
4021 return 0;
4022
4023 default:
4024 break;
4025 }
4026
4027 switch (TREE_CODE_LENGTH (code))
4028 {
4029 case 1:
4030 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
4031 case 2:
4032 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
4033 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
4034 default:
4035 return 0;
4036 }
4037
4038 case tcc_vl_exp:
4039 switch (code)
4040 {
4041 case CALL_EXPR:
4042 {
4043 const_tree arg;
4044 const_call_expr_arg_iterator iter;
4045 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
4046 if (CONTAINS_PLACEHOLDER_P (arg))
4047 return 1;
4048 return 0;
4049 }
4050 default:
4051 return 0;
4052 }
4053
4054 default:
4055 return 0;
4056 }
4057 return 0;
4058 }
4059
4060 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
4061 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
4062 field positions. */
4063
4064 static bool
4065 type_contains_placeholder_1 (const_tree type)
4066 {
4067 /* If the size contains a placeholder or the parent type (component type in
4068 the case of arrays) type involves a placeholder, this type does. */
4069 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
4070 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
4071 || (!POINTER_TYPE_P (type)
4072 && TREE_TYPE (type)
4073 && type_contains_placeholder_p (TREE_TYPE (type))))
4074 return true;
4075
4076 /* Now do type-specific checks. Note that the last part of the check above
4077 greatly limits what we have to do below. */
4078 switch (TREE_CODE (type))
4079 {
4080 case VOID_TYPE:
4081 case OPAQUE_TYPE:
4082 case COMPLEX_TYPE:
4083 case ENUMERAL_TYPE:
4084 case BOOLEAN_TYPE:
4085 case POINTER_TYPE:
4086 case OFFSET_TYPE:
4087 case REFERENCE_TYPE:
4088 case METHOD_TYPE:
4089 case FUNCTION_TYPE:
4090 case VECTOR_TYPE:
4091 case NULLPTR_TYPE:
4092 return false;
4093
4094 case INTEGER_TYPE:
4095 case REAL_TYPE:
4096 case FIXED_POINT_TYPE:
4097 /* Here we just check the bounds. */
4098 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
4099 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
4100
4101 case ARRAY_TYPE:
4102 /* We have already checked the component type above, so just check
4103 the domain type. Flexible array members have a null domain. */
4104 return TYPE_DOMAIN (type) ?
4105 type_contains_placeholder_p (TYPE_DOMAIN (type)) : false;
4106
4107 case RECORD_TYPE:
4108 case UNION_TYPE:
4109 case QUAL_UNION_TYPE:
4110 {
4111 tree field;
4112
4113 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4114 if (TREE_CODE (field) == FIELD_DECL
4115 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
4116 || (TREE_CODE (type) == QUAL_UNION_TYPE
4117 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
4118 || type_contains_placeholder_p (TREE_TYPE (field))))
4119 return true;
4120
4121 return false;
4122 }
4123
4124 default:
4125 gcc_unreachable ();
4126 }
4127 }
4128
4129 /* Wrapper around above function used to cache its result. */
4130
4131 bool
4132 type_contains_placeholder_p (tree type)
4133 {
4134 bool result;
4135
4136 /* If the contains_placeholder_bits field has been initialized,
4137 then we know the answer. */
4138 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
4139 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
4140
4141 /* Indicate that we've seen this type node, and the answer is false.
4142 This is what we want to return if we run into recursion via fields. */
4143 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
4144
4145 /* Compute the real value. */
4146 result = type_contains_placeholder_1 (type);
4147
4148 /* Store the real value. */
4149 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
4150
4151 return result;
4152 }
4153 \f
4154 /* Push tree EXP onto vector QUEUE if it is not already present. */
4155
4156 static void
4157 push_without_duplicates (tree exp, vec<tree> *queue)
4158 {
4159 unsigned int i;
4160 tree iter;
4161
4162 FOR_EACH_VEC_ELT (*queue, i, iter)
4163 if (simple_cst_equal (iter, exp) == 1)
4164 break;
4165
4166 if (!iter)
4167 queue->safe_push (exp);
4168 }
4169
4170 /* Given a tree EXP, find all occurrences of references to fields
4171 in a PLACEHOLDER_EXPR and place them in vector REFS without
4172 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
4173 we assume here that EXP contains only arithmetic expressions
4174 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
4175 argument list. */
4176
4177 void
4178 find_placeholder_in_expr (tree exp, vec<tree> *refs)
4179 {
4180 enum tree_code code = TREE_CODE (exp);
4181 tree inner;
4182 int i;
4183
4184 /* We handle TREE_LIST and COMPONENT_REF separately. */
4185 if (code == TREE_LIST)
4186 {
4187 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
4188 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
4189 }
4190 else if (code == COMPONENT_REF)
4191 {
4192 for (inner = TREE_OPERAND (exp, 0);
4193 REFERENCE_CLASS_P (inner);
4194 inner = TREE_OPERAND (inner, 0))
4195 ;
4196
4197 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
4198 push_without_duplicates (exp, refs);
4199 else
4200 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
4201 }
4202 else
4203 switch (TREE_CODE_CLASS (code))
4204 {
4205 case tcc_constant:
4206 break;
4207
4208 case tcc_declaration:
4209 /* Variables allocated to static storage can stay. */
4210 if (!TREE_STATIC (exp))
4211 push_without_duplicates (exp, refs);
4212 break;
4213
4214 case tcc_expression:
4215 /* This is the pattern built in ada/make_aligning_type. */
4216 if (code == ADDR_EXPR
4217 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
4218 {
4219 push_without_duplicates (exp, refs);
4220 break;
4221 }
4222
4223 /* Fall through. */
4224
4225 case tcc_exceptional:
4226 case tcc_unary:
4227 case tcc_binary:
4228 case tcc_comparison:
4229 case tcc_reference:
4230 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
4231 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
4232 break;
4233
4234 case tcc_vl_exp:
4235 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4236 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
4237 break;
4238
4239 default:
4240 gcc_unreachable ();
4241 }
4242 }
4243
4244 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
4245 return a tree with all occurrences of references to F in a
4246 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
4247 CONST_DECLs. Note that we assume here that EXP contains only
4248 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
4249 occurring only in their argument list. */
4250
4251 tree
4252 substitute_in_expr (tree exp, tree f, tree r)
4253 {
4254 enum tree_code code = TREE_CODE (exp);
4255 tree op0, op1, op2, op3;
4256 tree new_tree;
4257
4258 /* We handle TREE_LIST and COMPONENT_REF separately. */
4259 if (code == TREE_LIST)
4260 {
4261 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
4262 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
4263 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
4264 return exp;
4265
4266 return tree_cons (TREE_PURPOSE (exp), op1, op0);
4267 }
4268 else if (code == COMPONENT_REF)
4269 {
4270 tree inner;
4271
4272 /* If this expression is getting a value from a PLACEHOLDER_EXPR
4273 and it is the right field, replace it with R. */
4274 for (inner = TREE_OPERAND (exp, 0);
4275 REFERENCE_CLASS_P (inner);
4276 inner = TREE_OPERAND (inner, 0))
4277 ;
4278
4279 /* The field. */
4280 op1 = TREE_OPERAND (exp, 1);
4281
4282 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
4283 return r;
4284
4285 /* If this expression hasn't been completed let, leave it alone. */
4286 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
4287 return exp;
4288
4289 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4290 if (op0 == TREE_OPERAND (exp, 0))
4291 return exp;
4292
4293 new_tree
4294 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
4295 }
4296 else
4297 switch (TREE_CODE_CLASS (code))
4298 {
4299 case tcc_constant:
4300 return exp;
4301
4302 case tcc_declaration:
4303 if (exp == f)
4304 return r;
4305 else
4306 return exp;
4307
4308 case tcc_expression:
4309 if (exp == f)
4310 return r;
4311
4312 /* Fall through. */
4313
4314 case tcc_exceptional:
4315 case tcc_unary:
4316 case tcc_binary:
4317 case tcc_comparison:
4318 case tcc_reference:
4319 switch (TREE_CODE_LENGTH (code))
4320 {
4321 case 0:
4322 return exp;
4323
4324 case 1:
4325 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4326 if (op0 == TREE_OPERAND (exp, 0))
4327 return exp;
4328
4329 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
4330 break;
4331
4332 case 2:
4333 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4334 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4335
4336 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
4337 return exp;
4338
4339 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
4340 break;
4341
4342 case 3:
4343 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4344 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4345 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
4346
4347 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4348 && op2 == TREE_OPERAND (exp, 2))
4349 return exp;
4350
4351 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
4352 break;
4353
4354 case 4:
4355 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4356 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4357 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
4358 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
4359
4360 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4361 && op2 == TREE_OPERAND (exp, 2)
4362 && op3 == TREE_OPERAND (exp, 3))
4363 return exp;
4364
4365 new_tree
4366 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
4367 break;
4368
4369 default:
4370 gcc_unreachable ();
4371 }
4372 break;
4373
4374 case tcc_vl_exp:
4375 {
4376 int i;
4377
4378 new_tree = NULL_TREE;
4379
4380 /* If we are trying to replace F with a constant or with another
4381 instance of one of the arguments of the call, inline back
4382 functions which do nothing else than computing a value from
4383 the arguments they are passed. This makes it possible to
4384 fold partially or entirely the replacement expression. */
4385 if (code == CALL_EXPR)
4386 {
4387 bool maybe_inline = false;
4388 if (CONSTANT_CLASS_P (r))
4389 maybe_inline = true;
4390 else
4391 for (i = 3; i < TREE_OPERAND_LENGTH (exp); i++)
4392 if (operand_equal_p (TREE_OPERAND (exp, i), r, 0))
4393 {
4394 maybe_inline = true;
4395 break;
4396 }
4397 if (maybe_inline)
4398 {
4399 tree t = maybe_inline_call_in_expr (exp);
4400 if (t)
4401 return SUBSTITUTE_IN_EXPR (t, f, r);
4402 }
4403 }
4404
4405 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4406 {
4407 tree op = TREE_OPERAND (exp, i);
4408 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
4409 if (new_op != op)
4410 {
4411 if (!new_tree)
4412 new_tree = copy_node (exp);
4413 TREE_OPERAND (new_tree, i) = new_op;
4414 }
4415 }
4416
4417 if (new_tree)
4418 {
4419 new_tree = fold (new_tree);
4420 if (TREE_CODE (new_tree) == CALL_EXPR)
4421 process_call_operands (new_tree);
4422 }
4423 else
4424 return exp;
4425 }
4426 break;
4427
4428 default:
4429 gcc_unreachable ();
4430 }
4431
4432 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4433
4434 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4435 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4436
4437 return new_tree;
4438 }
4439
4440 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
4441 for it within OBJ, a tree that is an object or a chain of references. */
4442
4443 tree
4444 substitute_placeholder_in_expr (tree exp, tree obj)
4445 {
4446 enum tree_code code = TREE_CODE (exp);
4447 tree op0, op1, op2, op3;
4448 tree new_tree;
4449
4450 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
4451 in the chain of OBJ. */
4452 if (code == PLACEHOLDER_EXPR)
4453 {
4454 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
4455 tree elt;
4456
4457 for (elt = obj; elt != 0;
4458 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
4459 || TREE_CODE (elt) == COND_EXPR)
4460 ? TREE_OPERAND (elt, 1)
4461 : (REFERENCE_CLASS_P (elt)
4462 || UNARY_CLASS_P (elt)
4463 || BINARY_CLASS_P (elt)
4464 || VL_EXP_CLASS_P (elt)
4465 || EXPRESSION_CLASS_P (elt))
4466 ? TREE_OPERAND (elt, 0) : 0))
4467 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
4468 return elt;
4469
4470 for (elt = obj; elt != 0;
4471 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
4472 || TREE_CODE (elt) == COND_EXPR)
4473 ? TREE_OPERAND (elt, 1)
4474 : (REFERENCE_CLASS_P (elt)
4475 || UNARY_CLASS_P (elt)
4476 || BINARY_CLASS_P (elt)
4477 || VL_EXP_CLASS_P (elt)
4478 || EXPRESSION_CLASS_P (elt))
4479 ? TREE_OPERAND (elt, 0) : 0))
4480 if (POINTER_TYPE_P (TREE_TYPE (elt))
4481 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
4482 == need_type))
4483 return fold_build1 (INDIRECT_REF, need_type, elt);
4484
4485 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
4486 survives until RTL generation, there will be an error. */
4487 return exp;
4488 }
4489
4490 /* TREE_LIST is special because we need to look at TREE_VALUE
4491 and TREE_CHAIN, not TREE_OPERANDS. */
4492 else if (code == TREE_LIST)
4493 {
4494 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
4495 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
4496 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
4497 return exp;
4498
4499 return tree_cons (TREE_PURPOSE (exp), op1, op0);
4500 }
4501 else
4502 switch (TREE_CODE_CLASS (code))
4503 {
4504 case tcc_constant:
4505 case tcc_declaration:
4506 return exp;
4507
4508 case tcc_exceptional:
4509 case tcc_unary:
4510 case tcc_binary:
4511 case tcc_comparison:
4512 case tcc_expression:
4513 case tcc_reference:
4514 case tcc_statement:
4515 switch (TREE_CODE_LENGTH (code))
4516 {
4517 case 0:
4518 return exp;
4519
4520 case 1:
4521 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4522 if (op0 == TREE_OPERAND (exp, 0))
4523 return exp;
4524
4525 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
4526 break;
4527
4528 case 2:
4529 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4530 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4531
4532 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
4533 return exp;
4534
4535 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
4536 break;
4537
4538 case 3:
4539 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4540 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4541 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
4542
4543 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4544 && op2 == TREE_OPERAND (exp, 2))
4545 return exp;
4546
4547 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
4548 break;
4549
4550 case 4:
4551 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4552 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4553 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
4554 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
4555
4556 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4557 && op2 == TREE_OPERAND (exp, 2)
4558 && op3 == TREE_OPERAND (exp, 3))
4559 return exp;
4560
4561 new_tree
4562 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
4563 break;
4564
4565 default:
4566 gcc_unreachable ();
4567 }
4568 break;
4569
4570 case tcc_vl_exp:
4571 {
4572 int i;
4573
4574 new_tree = NULL_TREE;
4575
4576 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4577 {
4578 tree op = TREE_OPERAND (exp, i);
4579 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
4580 if (new_op != op)
4581 {
4582 if (!new_tree)
4583 new_tree = copy_node (exp);
4584 TREE_OPERAND (new_tree, i) = new_op;
4585 }
4586 }
4587
4588 if (new_tree)
4589 {
4590 new_tree = fold (new_tree);
4591 if (TREE_CODE (new_tree) == CALL_EXPR)
4592 process_call_operands (new_tree);
4593 }
4594 else
4595 return exp;
4596 }
4597 break;
4598
4599 default:
4600 gcc_unreachable ();
4601 }
4602
4603 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4604
4605 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4606 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4607
4608 return new_tree;
4609 }
4610 \f
4611
4612 /* Subroutine of stabilize_reference; this is called for subtrees of
4613 references. Any expression with side-effects must be put in a SAVE_EXPR
4614 to ensure that it is only evaluated once.
4615
4616 We don't put SAVE_EXPR nodes around everything, because assigning very
4617 simple expressions to temporaries causes us to miss good opportunities
4618 for optimizations. Among other things, the opportunity to fold in the
4619 addition of a constant into an addressing mode often gets lost, e.g.
4620 "y[i+1] += x;". In general, we take the approach that we should not make
4621 an assignment unless we are forced into it - i.e., that any non-side effect
4622 operator should be allowed, and that cse should take care of coalescing
4623 multiple utterances of the same expression should that prove fruitful. */
4624
4625 static tree
4626 stabilize_reference_1 (tree e)
4627 {
4628 tree result;
4629 enum tree_code code = TREE_CODE (e);
4630
4631 /* We cannot ignore const expressions because it might be a reference
4632 to a const array but whose index contains side-effects. But we can
4633 ignore things that are actual constant or that already have been
4634 handled by this function. */
4635
4636 if (tree_invariant_p (e))
4637 return e;
4638
4639 switch (TREE_CODE_CLASS (code))
4640 {
4641 case tcc_exceptional:
4642 /* Always wrap STATEMENT_LIST into SAVE_EXPR, even if it doesn't
4643 have side-effects. */
4644 if (code == STATEMENT_LIST)
4645 return save_expr (e);
4646 /* FALLTHRU */
4647 case tcc_type:
4648 case tcc_declaration:
4649 case tcc_comparison:
4650 case tcc_statement:
4651 case tcc_expression:
4652 case tcc_reference:
4653 case tcc_vl_exp:
4654 /* If the expression has side-effects, then encase it in a SAVE_EXPR
4655 so that it will only be evaluated once. */
4656 /* The reference (r) and comparison (<) classes could be handled as
4657 below, but it is generally faster to only evaluate them once. */
4658 if (TREE_SIDE_EFFECTS (e))
4659 return save_expr (e);
4660 return e;
4661
4662 case tcc_constant:
4663 /* Constants need no processing. In fact, we should never reach
4664 here. */
4665 return e;
4666
4667 case tcc_binary:
4668 /* Division is slow and tends to be compiled with jumps,
4669 especially the division by powers of 2 that is often
4670 found inside of an array reference. So do it just once. */
4671 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
4672 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
4673 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
4674 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
4675 return save_expr (e);
4676 /* Recursively stabilize each operand. */
4677 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
4678 stabilize_reference_1 (TREE_OPERAND (e, 1)));
4679 break;
4680
4681 case tcc_unary:
4682 /* Recursively stabilize each operand. */
4683 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
4684 break;
4685
4686 default:
4687 gcc_unreachable ();
4688 }
4689
4690 TREE_TYPE (result) = TREE_TYPE (e);
4691 TREE_READONLY (result) = TREE_READONLY (e);
4692 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
4693 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
4694
4695 return result;
4696 }
4697
4698 /* Stabilize a reference so that we can use it any number of times
4699 without causing its operands to be evaluated more than once.
4700 Returns the stabilized reference. This works by means of save_expr,
4701 so see the caveats in the comments about save_expr.
4702
4703 Also allows conversion expressions whose operands are references.
4704 Any other kind of expression is returned unchanged. */
4705
4706 tree
4707 stabilize_reference (tree ref)
4708 {
4709 tree result;
4710 enum tree_code code = TREE_CODE (ref);
4711
4712 switch (code)
4713 {
4714 case VAR_DECL:
4715 case PARM_DECL:
4716 case RESULT_DECL:
4717 /* No action is needed in this case. */
4718 return ref;
4719
4720 CASE_CONVERT:
4721 case FLOAT_EXPR:
4722 case FIX_TRUNC_EXPR:
4723 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
4724 break;
4725
4726 case INDIRECT_REF:
4727 result = build_nt (INDIRECT_REF,
4728 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
4729 break;
4730
4731 case COMPONENT_REF:
4732 result = build_nt (COMPONENT_REF,
4733 stabilize_reference (TREE_OPERAND (ref, 0)),
4734 TREE_OPERAND (ref, 1), NULL_TREE);
4735 break;
4736
4737 case BIT_FIELD_REF:
4738 result = build_nt (BIT_FIELD_REF,
4739 stabilize_reference (TREE_OPERAND (ref, 0)),
4740 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
4741 REF_REVERSE_STORAGE_ORDER (result) = REF_REVERSE_STORAGE_ORDER (ref);
4742 break;
4743
4744 case ARRAY_REF:
4745 result = build_nt (ARRAY_REF,
4746 stabilize_reference (TREE_OPERAND (ref, 0)),
4747 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4748 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4749 break;
4750
4751 case ARRAY_RANGE_REF:
4752 result = build_nt (ARRAY_RANGE_REF,
4753 stabilize_reference (TREE_OPERAND (ref, 0)),
4754 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4755 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4756 break;
4757
4758 case COMPOUND_EXPR:
4759 /* We cannot wrap the first expression in a SAVE_EXPR, as then
4760 it wouldn't be ignored. This matters when dealing with
4761 volatiles. */
4762 return stabilize_reference_1 (ref);
4763
4764 /* If arg isn't a kind of lvalue we recognize, make no change.
4765 Caller should recognize the error for an invalid lvalue. */
4766 default:
4767 return ref;
4768
4769 case ERROR_MARK:
4770 return error_mark_node;
4771 }
4772
4773 TREE_TYPE (result) = TREE_TYPE (ref);
4774 TREE_READONLY (result) = TREE_READONLY (ref);
4775 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
4776 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
4777
4778 return result;
4779 }
4780 \f
4781 /* Low-level constructors for expressions. */
4782
4783 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
4784 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
4785
4786 void
4787 recompute_tree_invariant_for_addr_expr (tree t)
4788 {
4789 tree node;
4790 bool tc = true, se = false;
4791
4792 gcc_assert (TREE_CODE (t) == ADDR_EXPR);
4793
4794 /* We started out assuming this address is both invariant and constant, but
4795 does not have side effects. Now go down any handled components and see if
4796 any of them involve offsets that are either non-constant or non-invariant.
4797 Also check for side-effects.
4798
4799 ??? Note that this code makes no attempt to deal with the case where
4800 taking the address of something causes a copy due to misalignment. */
4801
4802 #define UPDATE_FLAGS(NODE) \
4803 do { tree _node = (NODE); \
4804 if (_node && !TREE_CONSTANT (_node)) tc = false; \
4805 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
4806
4807 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
4808 node = TREE_OPERAND (node, 0))
4809 {
4810 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
4811 array reference (probably made temporarily by the G++ front end),
4812 so ignore all the operands. */
4813 if ((TREE_CODE (node) == ARRAY_REF
4814 || TREE_CODE (node) == ARRAY_RANGE_REF)
4815 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
4816 {
4817 UPDATE_FLAGS (TREE_OPERAND (node, 1));
4818 if (TREE_OPERAND (node, 2))
4819 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4820 if (TREE_OPERAND (node, 3))
4821 UPDATE_FLAGS (TREE_OPERAND (node, 3));
4822 }
4823 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
4824 FIELD_DECL, apparently. The G++ front end can put something else
4825 there, at least temporarily. */
4826 else if (TREE_CODE (node) == COMPONENT_REF
4827 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
4828 {
4829 if (TREE_OPERAND (node, 2))
4830 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4831 }
4832 }
4833
4834 node = lang_hooks.expr_to_decl (node, &tc, &se);
4835
4836 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4837 the address, since &(*a)->b is a form of addition. If it's a constant, the
4838 address is constant too. If it's a decl, its address is constant if the
4839 decl is static. Everything else is not constant and, furthermore,
4840 taking the address of a volatile variable is not volatile. */
4841 if (TREE_CODE (node) == INDIRECT_REF
4842 || TREE_CODE (node) == MEM_REF)
4843 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4844 else if (CONSTANT_CLASS_P (node))
4845 ;
4846 else if (DECL_P (node))
4847 tc &= (staticp (node) != NULL_TREE);
4848 else
4849 {
4850 tc = false;
4851 se |= TREE_SIDE_EFFECTS (node);
4852 }
4853
4854
4855 TREE_CONSTANT (t) = tc;
4856 TREE_SIDE_EFFECTS (t) = se;
4857 #undef UPDATE_FLAGS
4858 }
4859
4860 /* Build an expression of code CODE, data type TYPE, and operands as
4861 specified. Expressions and reference nodes can be created this way.
4862 Constants, decls, types and misc nodes cannot be.
4863
4864 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4865 enough for all extant tree codes. */
4866
4867 tree
4868 build0 (enum tree_code code, tree tt MEM_STAT_DECL)
4869 {
4870 tree t;
4871
4872 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4873
4874 t = make_node (code PASS_MEM_STAT);
4875 TREE_TYPE (t) = tt;
4876
4877 return t;
4878 }
4879
4880 tree
4881 build1 (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4882 {
4883 int length = sizeof (struct tree_exp);
4884 tree t;
4885
4886 record_node_allocation_statistics (code, length);
4887
4888 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4889
4890 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4891
4892 memset (t, 0, sizeof (struct tree_common));
4893
4894 TREE_SET_CODE (t, code);
4895
4896 TREE_TYPE (t) = type;
4897 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4898 TREE_OPERAND (t, 0) = node;
4899 if (node && !TYPE_P (node))
4900 {
4901 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4902 TREE_READONLY (t) = TREE_READONLY (node);
4903 }
4904
4905 if (TREE_CODE_CLASS (code) == tcc_statement)
4906 {
4907 if (code != DEBUG_BEGIN_STMT)
4908 TREE_SIDE_EFFECTS (t) = 1;
4909 }
4910 else switch (code)
4911 {
4912 case VA_ARG_EXPR:
4913 /* All of these have side-effects, no matter what their
4914 operands are. */
4915 TREE_SIDE_EFFECTS (t) = 1;
4916 TREE_READONLY (t) = 0;
4917 break;
4918
4919 case INDIRECT_REF:
4920 /* Whether a dereference is readonly has nothing to do with whether
4921 its operand is readonly. */
4922 TREE_READONLY (t) = 0;
4923 break;
4924
4925 case ADDR_EXPR:
4926 if (node)
4927 recompute_tree_invariant_for_addr_expr (t);
4928 break;
4929
4930 default:
4931 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4932 && node && !TYPE_P (node)
4933 && TREE_CONSTANT (node))
4934 TREE_CONSTANT (t) = 1;
4935 if (TREE_CODE_CLASS (code) == tcc_reference
4936 && node && TREE_THIS_VOLATILE (node))
4937 TREE_THIS_VOLATILE (t) = 1;
4938 break;
4939 }
4940
4941 return t;
4942 }
4943
4944 #define PROCESS_ARG(N) \
4945 do { \
4946 TREE_OPERAND (t, N) = arg##N; \
4947 if (arg##N &&!TYPE_P (arg##N)) \
4948 { \
4949 if (TREE_SIDE_EFFECTS (arg##N)) \
4950 side_effects = 1; \
4951 if (!TREE_READONLY (arg##N) \
4952 && !CONSTANT_CLASS_P (arg##N)) \
4953 (void) (read_only = 0); \
4954 if (!TREE_CONSTANT (arg##N)) \
4955 (void) (constant = 0); \
4956 } \
4957 } while (0)
4958
4959 tree
4960 build2 (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
4961 {
4962 bool constant, read_only, side_effects, div_by_zero;
4963 tree t;
4964
4965 gcc_assert (TREE_CODE_LENGTH (code) == 2);
4966
4967 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
4968 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
4969 /* When sizetype precision doesn't match that of pointers
4970 we need to be able to build explicit extensions or truncations
4971 of the offset argument. */
4972 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
4973 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
4974 && TREE_CODE (arg1) == INTEGER_CST);
4975
4976 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
4977 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
4978 && ptrofftype_p (TREE_TYPE (arg1)));
4979
4980 t = make_node (code PASS_MEM_STAT);
4981 TREE_TYPE (t) = tt;
4982
4983 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
4984 result based on those same flags for the arguments. But if the
4985 arguments aren't really even `tree' expressions, we shouldn't be trying
4986 to do this. */
4987
4988 /* Expressions without side effects may be constant if their
4989 arguments are as well. */
4990 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4991 || TREE_CODE_CLASS (code) == tcc_binary);
4992 read_only = 1;
4993 side_effects = TREE_SIDE_EFFECTS (t);
4994
4995 switch (code)
4996 {
4997 case TRUNC_DIV_EXPR:
4998 case CEIL_DIV_EXPR:
4999 case FLOOR_DIV_EXPR:
5000 case ROUND_DIV_EXPR:
5001 case EXACT_DIV_EXPR:
5002 case CEIL_MOD_EXPR:
5003 case FLOOR_MOD_EXPR:
5004 case ROUND_MOD_EXPR:
5005 case TRUNC_MOD_EXPR:
5006 div_by_zero = integer_zerop (arg1);
5007 break;
5008 default:
5009 div_by_zero = false;
5010 }
5011
5012 PROCESS_ARG (0);
5013 PROCESS_ARG (1);
5014
5015 TREE_SIDE_EFFECTS (t) = side_effects;
5016 if (code == MEM_REF)
5017 {
5018 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
5019 {
5020 tree o = TREE_OPERAND (arg0, 0);
5021 TREE_READONLY (t) = TREE_READONLY (o);
5022 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
5023 }
5024 }
5025 else
5026 {
5027 TREE_READONLY (t) = read_only;
5028 /* Don't mark X / 0 as constant. */
5029 TREE_CONSTANT (t) = constant && !div_by_zero;
5030 TREE_THIS_VOLATILE (t)
5031 = (TREE_CODE_CLASS (code) == tcc_reference
5032 && arg0 && TREE_THIS_VOLATILE (arg0));
5033 }
5034
5035 return t;
5036 }
5037
5038
5039 tree
5040 build3 (enum tree_code code, tree tt, tree arg0, tree arg1,
5041 tree arg2 MEM_STAT_DECL)
5042 {
5043 bool constant, read_only, side_effects;
5044 tree t;
5045
5046 gcc_assert (TREE_CODE_LENGTH (code) == 3);
5047 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
5048
5049 t = make_node (code PASS_MEM_STAT);
5050 TREE_TYPE (t) = tt;
5051
5052 read_only = 1;
5053
5054 /* As a special exception, if COND_EXPR has NULL branches, we
5055 assume that it is a gimple statement and always consider
5056 it to have side effects. */
5057 if (code == COND_EXPR
5058 && tt == void_type_node
5059 && arg1 == NULL_TREE
5060 && arg2 == NULL_TREE)
5061 side_effects = true;
5062 else
5063 side_effects = TREE_SIDE_EFFECTS (t);
5064
5065 PROCESS_ARG (0);
5066 PROCESS_ARG (1);
5067 PROCESS_ARG (2);
5068
5069 if (code == COND_EXPR)
5070 TREE_READONLY (t) = read_only;
5071
5072 TREE_SIDE_EFFECTS (t) = side_effects;
5073 TREE_THIS_VOLATILE (t)
5074 = (TREE_CODE_CLASS (code) == tcc_reference
5075 && arg0 && TREE_THIS_VOLATILE (arg0));
5076
5077 return t;
5078 }
5079
5080 tree
5081 build4 (enum tree_code code, tree tt, tree arg0, tree arg1,
5082 tree arg2, tree arg3 MEM_STAT_DECL)
5083 {
5084 bool constant, read_only, side_effects;
5085 tree t;
5086
5087 gcc_assert (TREE_CODE_LENGTH (code) == 4);
5088
5089 t = make_node (code PASS_MEM_STAT);
5090 TREE_TYPE (t) = tt;
5091
5092 side_effects = TREE_SIDE_EFFECTS (t);
5093
5094 PROCESS_ARG (0);
5095 PROCESS_ARG (1);
5096 PROCESS_ARG (2);
5097 PROCESS_ARG (3);
5098
5099 TREE_SIDE_EFFECTS (t) = side_effects;
5100 TREE_THIS_VOLATILE (t)
5101 = (TREE_CODE_CLASS (code) == tcc_reference
5102 && arg0 && TREE_THIS_VOLATILE (arg0));
5103
5104 return t;
5105 }
5106
5107 tree
5108 build5 (enum tree_code code, tree tt, tree arg0, tree arg1,
5109 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
5110 {
5111 bool constant, read_only, side_effects;
5112 tree t;
5113
5114 gcc_assert (TREE_CODE_LENGTH (code) == 5);
5115
5116 t = make_node (code PASS_MEM_STAT);
5117 TREE_TYPE (t) = tt;
5118
5119 side_effects = TREE_SIDE_EFFECTS (t);
5120
5121 PROCESS_ARG (0);
5122 PROCESS_ARG (1);
5123 PROCESS_ARG (2);
5124 PROCESS_ARG (3);
5125 PROCESS_ARG (4);
5126
5127 TREE_SIDE_EFFECTS (t) = side_effects;
5128 if (code == TARGET_MEM_REF)
5129 {
5130 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
5131 {
5132 tree o = TREE_OPERAND (arg0, 0);
5133 TREE_READONLY (t) = TREE_READONLY (o);
5134 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
5135 }
5136 }
5137 else
5138 TREE_THIS_VOLATILE (t)
5139 = (TREE_CODE_CLASS (code) == tcc_reference
5140 && arg0 && TREE_THIS_VOLATILE (arg0));
5141
5142 return t;
5143 }
5144
5145 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
5146 on the pointer PTR. */
5147
5148 tree
5149 build_simple_mem_ref_loc (location_t loc, tree ptr)
5150 {
5151 poly_int64 offset = 0;
5152 tree ptype = TREE_TYPE (ptr);
5153 tree tem;
5154 /* For convenience allow addresses that collapse to a simple base
5155 and offset. */
5156 if (TREE_CODE (ptr) == ADDR_EXPR
5157 && (handled_component_p (TREE_OPERAND (ptr, 0))
5158 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
5159 {
5160 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
5161 gcc_assert (ptr);
5162 if (TREE_CODE (ptr) == MEM_REF)
5163 {
5164 offset += mem_ref_offset (ptr).force_shwi ();
5165 ptr = TREE_OPERAND (ptr, 0);
5166 }
5167 else
5168 ptr = build_fold_addr_expr (ptr);
5169 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
5170 }
5171 tem = build2 (MEM_REF, TREE_TYPE (ptype),
5172 ptr, build_int_cst (ptype, offset));
5173 SET_EXPR_LOCATION (tem, loc);
5174 return tem;
5175 }
5176
5177 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
5178
5179 poly_offset_int
5180 mem_ref_offset (const_tree t)
5181 {
5182 return poly_offset_int::from (wi::to_poly_wide (TREE_OPERAND (t, 1)),
5183 SIGNED);
5184 }
5185
5186 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
5187 offsetted by OFFSET units. */
5188
5189 tree
5190 build_invariant_address (tree type, tree base, poly_int64 offset)
5191 {
5192 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
5193 build_fold_addr_expr (base),
5194 build_int_cst (ptr_type_node, offset));
5195 tree addr = build1 (ADDR_EXPR, type, ref);
5196 recompute_tree_invariant_for_addr_expr (addr);
5197 return addr;
5198 }
5199
5200 /* Similar except don't specify the TREE_TYPE
5201 and leave the TREE_SIDE_EFFECTS as 0.
5202 It is permissible for arguments to be null,
5203 or even garbage if their values do not matter. */
5204
5205 tree
5206 build_nt (enum tree_code code, ...)
5207 {
5208 tree t;
5209 int length;
5210 int i;
5211 va_list p;
5212
5213 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
5214
5215 va_start (p, code);
5216
5217 t = make_node (code);
5218 length = TREE_CODE_LENGTH (code);
5219
5220 for (i = 0; i < length; i++)
5221 TREE_OPERAND (t, i) = va_arg (p, tree);
5222
5223 va_end (p);
5224 return t;
5225 }
5226
5227 /* Similar to build_nt, but for creating a CALL_EXPR object with a
5228 tree vec. */
5229
5230 tree
5231 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
5232 {
5233 tree ret, t;
5234 unsigned int ix;
5235
5236 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
5237 CALL_EXPR_FN (ret) = fn;
5238 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
5239 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
5240 CALL_EXPR_ARG (ret, ix) = t;
5241 return ret;
5242 }
5243 \f
5244 /* Create a DECL_... node of code CODE, name NAME (if non-null)
5245 and data type TYPE.
5246 We do NOT enter this node in any sort of symbol table.
5247
5248 LOC is the location of the decl.
5249
5250 layout_decl is used to set up the decl's storage layout.
5251 Other slots are initialized to 0 or null pointers. */
5252
5253 tree
5254 build_decl (location_t loc, enum tree_code code, tree name,
5255 tree type MEM_STAT_DECL)
5256 {
5257 tree t;
5258
5259 t = make_node (code PASS_MEM_STAT);
5260 DECL_SOURCE_LOCATION (t) = loc;
5261
5262 /* if (type == error_mark_node)
5263 type = integer_type_node; */
5264 /* That is not done, deliberately, so that having error_mark_node
5265 as the type can suppress useless errors in the use of this variable. */
5266
5267 DECL_NAME (t) = name;
5268 TREE_TYPE (t) = type;
5269
5270 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
5271 layout_decl (t, 0);
5272
5273 return t;
5274 }
5275
5276 /* Builds and returns function declaration with NAME and TYPE. */
5277
5278 tree
5279 build_fn_decl (const char *name, tree type)
5280 {
5281 tree id = get_identifier (name);
5282 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
5283
5284 DECL_EXTERNAL (decl) = 1;
5285 TREE_PUBLIC (decl) = 1;
5286 DECL_ARTIFICIAL (decl) = 1;
5287 TREE_NOTHROW (decl) = 1;
5288
5289 return decl;
5290 }
5291
5292 vec<tree, va_gc> *all_translation_units;
5293
5294 /* Builds a new translation-unit decl with name NAME, queues it in the
5295 global list of translation-unit decls and returns it. */
5296
5297 tree
5298 build_translation_unit_decl (tree name)
5299 {
5300 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
5301 name, NULL_TREE);
5302 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
5303 vec_safe_push (all_translation_units, tu);
5304 return tu;
5305 }
5306
5307 \f
5308 /* BLOCK nodes are used to represent the structure of binding contours
5309 and declarations, once those contours have been exited and their contents
5310 compiled. This information is used for outputting debugging info. */
5311
5312 tree
5313 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
5314 {
5315 tree block = make_node (BLOCK);
5316
5317 BLOCK_VARS (block) = vars;
5318 BLOCK_SUBBLOCKS (block) = subblocks;
5319 BLOCK_SUPERCONTEXT (block) = supercontext;
5320 BLOCK_CHAIN (block) = chain;
5321 return block;
5322 }
5323
5324 \f
5325 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
5326
5327 LOC is the location to use in tree T. */
5328
5329 void
5330 protected_set_expr_location (tree t, location_t loc)
5331 {
5332 if (CAN_HAVE_LOCATION_P (t))
5333 SET_EXPR_LOCATION (t, loc);
5334 else if (t && TREE_CODE (t) == STATEMENT_LIST)
5335 {
5336 t = expr_single (t);
5337 if (t && CAN_HAVE_LOCATION_P (t))
5338 SET_EXPR_LOCATION (t, loc);
5339 }
5340 }
5341
5342 /* Like PROTECTED_SET_EXPR_LOCATION, but only do that if T has
5343 UNKNOWN_LOCATION. */
5344
5345 void
5346 protected_set_expr_location_if_unset (tree t, location_t loc)
5347 {
5348 t = expr_single (t);
5349 if (t && !EXPR_HAS_LOCATION (t))
5350 protected_set_expr_location (t, loc);
5351 }
5352 \f
5353 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
5354 of the various TYPE_QUAL values. */
5355
5356 static void
5357 set_type_quals (tree type, int type_quals)
5358 {
5359 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
5360 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
5361 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
5362 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
5363 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
5364 }
5365
5366 /* Returns true iff CAND and BASE have equivalent language-specific
5367 qualifiers. */
5368
5369 bool
5370 check_lang_type (const_tree cand, const_tree base)
5371 {
5372 if (lang_hooks.types.type_hash_eq == NULL)
5373 return true;
5374 /* type_hash_eq currently only applies to these types. */
5375 if (TREE_CODE (cand) != FUNCTION_TYPE
5376 && TREE_CODE (cand) != METHOD_TYPE)
5377 return true;
5378 return lang_hooks.types.type_hash_eq (cand, base);
5379 }
5380
5381 /* This function checks to see if TYPE matches the size one of the built-in
5382 atomic types, and returns that core atomic type. */
5383
5384 static tree
5385 find_atomic_core_type (const_tree type)
5386 {
5387 tree base_atomic_type;
5388
5389 /* Only handle complete types. */
5390 if (!tree_fits_uhwi_p (TYPE_SIZE (type)))
5391 return NULL_TREE;
5392
5393 switch (tree_to_uhwi (TYPE_SIZE (type)))
5394 {
5395 case 8:
5396 base_atomic_type = atomicQI_type_node;
5397 break;
5398
5399 case 16:
5400 base_atomic_type = atomicHI_type_node;
5401 break;
5402
5403 case 32:
5404 base_atomic_type = atomicSI_type_node;
5405 break;
5406
5407 case 64:
5408 base_atomic_type = atomicDI_type_node;
5409 break;
5410
5411 case 128:
5412 base_atomic_type = atomicTI_type_node;
5413 break;
5414
5415 default:
5416 base_atomic_type = NULL_TREE;
5417 }
5418
5419 return base_atomic_type;
5420 }
5421
5422 /* Returns true iff unqualified CAND and BASE are equivalent. */
5423
5424 bool
5425 check_base_type (const_tree cand, const_tree base)
5426 {
5427 if (TYPE_NAME (cand) != TYPE_NAME (base)
5428 /* Apparently this is needed for Objective-C. */
5429 || TYPE_CONTEXT (cand) != TYPE_CONTEXT (base)
5430 || !attribute_list_equal (TYPE_ATTRIBUTES (cand),
5431 TYPE_ATTRIBUTES (base)))
5432 return false;
5433 /* Check alignment. */
5434 if (TYPE_ALIGN (cand) == TYPE_ALIGN (base)
5435 && TYPE_USER_ALIGN (cand) == TYPE_USER_ALIGN (base))
5436 return true;
5437 /* Atomic types increase minimal alignment. We must to do so as well
5438 or we get duplicated canonical types. See PR88686. */
5439 if ((TYPE_QUALS (cand) & TYPE_QUAL_ATOMIC))
5440 {
5441 /* See if this object can map to a basic atomic type. */
5442 tree atomic_type = find_atomic_core_type (cand);
5443 if (atomic_type && TYPE_ALIGN (atomic_type) == TYPE_ALIGN (cand))
5444 return true;
5445 }
5446 return false;
5447 }
5448
5449 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
5450
5451 bool
5452 check_qualified_type (const_tree cand, const_tree base, int type_quals)
5453 {
5454 return (TYPE_QUALS (cand) == type_quals
5455 && check_base_type (cand, base)
5456 && check_lang_type (cand, base));
5457 }
5458
5459 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
5460
5461 static bool
5462 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
5463 {
5464 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
5465 && TYPE_NAME (cand) == TYPE_NAME (base)
5466 /* Apparently this is needed for Objective-C. */
5467 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5468 /* Check alignment. */
5469 && TYPE_ALIGN (cand) == align
5470 /* Check this is a user-aligned type as build_aligned_type
5471 would create. */
5472 && TYPE_USER_ALIGN (cand)
5473 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5474 TYPE_ATTRIBUTES (base))
5475 && check_lang_type (cand, base));
5476 }
5477
5478 /* Return a version of the TYPE, qualified as indicated by the
5479 TYPE_QUALS, if one exists. If no qualified version exists yet,
5480 return NULL_TREE. */
5481
5482 tree
5483 get_qualified_type (tree type, int type_quals)
5484 {
5485 if (TYPE_QUALS (type) == type_quals)
5486 return type;
5487
5488 tree mv = TYPE_MAIN_VARIANT (type);
5489 if (check_qualified_type (mv, type, type_quals))
5490 return mv;
5491
5492 /* Search the chain of variants to see if there is already one there just
5493 like the one we need to have. If so, use that existing one. We must
5494 preserve the TYPE_NAME, since there is code that depends on this. */
5495 for (tree *tp = &TYPE_NEXT_VARIANT (mv); *tp; tp = &TYPE_NEXT_VARIANT (*tp))
5496 if (check_qualified_type (*tp, type, type_quals))
5497 {
5498 /* Put the found variant at the head of the variant list so
5499 frequently searched variants get found faster. The C++ FE
5500 benefits greatly from this. */
5501 tree t = *tp;
5502 *tp = TYPE_NEXT_VARIANT (t);
5503 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
5504 TYPE_NEXT_VARIANT (mv) = t;
5505 return t;
5506 }
5507
5508 return NULL_TREE;
5509 }
5510
5511 /* Like get_qualified_type, but creates the type if it does not
5512 exist. This function never returns NULL_TREE. */
5513
5514 tree
5515 build_qualified_type (tree type, int type_quals MEM_STAT_DECL)
5516 {
5517 tree t;
5518
5519 /* See if we already have the appropriate qualified variant. */
5520 t = get_qualified_type (type, type_quals);
5521
5522 /* If not, build it. */
5523 if (!t)
5524 {
5525 t = build_variant_type_copy (type PASS_MEM_STAT);
5526 set_type_quals (t, type_quals);
5527
5528 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
5529 {
5530 /* See if this object can map to a basic atomic type. */
5531 tree atomic_type = find_atomic_core_type (type);
5532 if (atomic_type)
5533 {
5534 /* Ensure the alignment of this type is compatible with
5535 the required alignment of the atomic type. */
5536 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
5537 SET_TYPE_ALIGN (t, TYPE_ALIGN (atomic_type));
5538 }
5539 }
5540
5541 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5542 /* Propagate structural equality. */
5543 SET_TYPE_STRUCTURAL_EQUALITY (t);
5544 else if (TYPE_CANONICAL (type) != type)
5545 /* Build the underlying canonical type, since it is different
5546 from TYPE. */
5547 {
5548 tree c = build_qualified_type (TYPE_CANONICAL (type), type_quals);
5549 TYPE_CANONICAL (t) = TYPE_CANONICAL (c);
5550 }
5551 else
5552 /* T is its own canonical type. */
5553 TYPE_CANONICAL (t) = t;
5554
5555 }
5556
5557 return t;
5558 }
5559
5560 /* Create a variant of type T with alignment ALIGN. */
5561
5562 tree
5563 build_aligned_type (tree type, unsigned int align)
5564 {
5565 tree t;
5566
5567 if (TYPE_PACKED (type)
5568 || TYPE_ALIGN (type) == align)
5569 return type;
5570
5571 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5572 if (check_aligned_type (t, type, align))
5573 return t;
5574
5575 t = build_variant_type_copy (type);
5576 SET_TYPE_ALIGN (t, align);
5577 TYPE_USER_ALIGN (t) = 1;
5578
5579 return t;
5580 }
5581
5582 /* Create a new distinct copy of TYPE. The new type is made its own
5583 MAIN_VARIANT. If TYPE requires structural equality checks, the
5584 resulting type requires structural equality checks; otherwise, its
5585 TYPE_CANONICAL points to itself. */
5586
5587 tree
5588 build_distinct_type_copy (tree type MEM_STAT_DECL)
5589 {
5590 tree t = copy_node (type PASS_MEM_STAT);
5591
5592 TYPE_POINTER_TO (t) = 0;
5593 TYPE_REFERENCE_TO (t) = 0;
5594
5595 /* Set the canonical type either to a new equivalence class, or
5596 propagate the need for structural equality checks. */
5597 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5598 SET_TYPE_STRUCTURAL_EQUALITY (t);
5599 else
5600 TYPE_CANONICAL (t) = t;
5601
5602 /* Make it its own variant. */
5603 TYPE_MAIN_VARIANT (t) = t;
5604 TYPE_NEXT_VARIANT (t) = 0;
5605
5606 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
5607 whose TREE_TYPE is not t. This can also happen in the Ada
5608 frontend when using subtypes. */
5609
5610 return t;
5611 }
5612
5613 /* Create a new variant of TYPE, equivalent but distinct. This is so
5614 the caller can modify it. TYPE_CANONICAL for the return type will
5615 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
5616 are considered equal by the language itself (or that both types
5617 require structural equality checks). */
5618
5619 tree
5620 build_variant_type_copy (tree type MEM_STAT_DECL)
5621 {
5622 tree t, m = TYPE_MAIN_VARIANT (type);
5623
5624 t = build_distinct_type_copy (type PASS_MEM_STAT);
5625
5626 /* Since we're building a variant, assume that it is a non-semantic
5627 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
5628 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
5629 /* Type variants have no alias set defined. */
5630 TYPE_ALIAS_SET (t) = -1;
5631
5632 /* Add the new type to the chain of variants of TYPE. */
5633 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
5634 TYPE_NEXT_VARIANT (m) = t;
5635 TYPE_MAIN_VARIANT (t) = m;
5636
5637 return t;
5638 }
5639 \f
5640 /* Return true if the from tree in both tree maps are equal. */
5641
5642 int
5643 tree_map_base_eq (const void *va, const void *vb)
5644 {
5645 const struct tree_map_base *const a = (const struct tree_map_base *) va,
5646 *const b = (const struct tree_map_base *) vb;
5647 return (a->from == b->from);
5648 }
5649
5650 /* Hash a from tree in a tree_base_map. */
5651
5652 unsigned int
5653 tree_map_base_hash (const void *item)
5654 {
5655 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
5656 }
5657
5658 /* Return true if this tree map structure is marked for garbage collection
5659 purposes. We simply return true if the from tree is marked, so that this
5660 structure goes away when the from tree goes away. */
5661
5662 int
5663 tree_map_base_marked_p (const void *p)
5664 {
5665 return ggc_marked_p (((const struct tree_map_base *) p)->from);
5666 }
5667
5668 /* Hash a from tree in a tree_map. */
5669
5670 unsigned int
5671 tree_map_hash (const void *item)
5672 {
5673 return (((const struct tree_map *) item)->hash);
5674 }
5675
5676 /* Hash a from tree in a tree_decl_map. */
5677
5678 unsigned int
5679 tree_decl_map_hash (const void *item)
5680 {
5681 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
5682 }
5683
5684 /* Return the initialization priority for DECL. */
5685
5686 priority_type
5687 decl_init_priority_lookup (tree decl)
5688 {
5689 symtab_node *snode = symtab_node::get (decl);
5690
5691 if (!snode)
5692 return DEFAULT_INIT_PRIORITY;
5693 return
5694 snode->get_init_priority ();
5695 }
5696
5697 /* Return the finalization priority for DECL. */
5698
5699 priority_type
5700 decl_fini_priority_lookup (tree decl)
5701 {
5702 cgraph_node *node = cgraph_node::get (decl);
5703
5704 if (!node)
5705 return DEFAULT_INIT_PRIORITY;
5706 return
5707 node->get_fini_priority ();
5708 }
5709
5710 /* Set the initialization priority for DECL to PRIORITY. */
5711
5712 void
5713 decl_init_priority_insert (tree decl, priority_type priority)
5714 {
5715 struct symtab_node *snode;
5716
5717 if (priority == DEFAULT_INIT_PRIORITY)
5718 {
5719 snode = symtab_node::get (decl);
5720 if (!snode)
5721 return;
5722 }
5723 else if (VAR_P (decl))
5724 snode = varpool_node::get_create (decl);
5725 else
5726 snode = cgraph_node::get_create (decl);
5727 snode->set_init_priority (priority);
5728 }
5729
5730 /* Set the finalization priority for DECL to PRIORITY. */
5731
5732 void
5733 decl_fini_priority_insert (tree decl, priority_type priority)
5734 {
5735 struct cgraph_node *node;
5736
5737 if (priority == DEFAULT_INIT_PRIORITY)
5738 {
5739 node = cgraph_node::get (decl);
5740 if (!node)
5741 return;
5742 }
5743 else
5744 node = cgraph_node::get_create (decl);
5745 node->set_fini_priority (priority);
5746 }
5747
5748 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
5749
5750 static void
5751 print_debug_expr_statistics (void)
5752 {
5753 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
5754 (long) debug_expr_for_decl->size (),
5755 (long) debug_expr_for_decl->elements (),
5756 debug_expr_for_decl->collisions ());
5757 }
5758
5759 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
5760
5761 static void
5762 print_value_expr_statistics (void)
5763 {
5764 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
5765 (long) value_expr_for_decl->size (),
5766 (long) value_expr_for_decl->elements (),
5767 value_expr_for_decl->collisions ());
5768 }
5769
5770 /* Lookup a debug expression for FROM, and return it if we find one. */
5771
5772 tree
5773 decl_debug_expr_lookup (tree from)
5774 {
5775 struct tree_decl_map *h, in;
5776 in.base.from = from;
5777
5778 h = debug_expr_for_decl->find_with_hash (&in, DECL_UID (from));
5779 if (h)
5780 return h->to;
5781 return NULL_TREE;
5782 }
5783
5784 /* Insert a mapping FROM->TO in the debug expression hashtable. */
5785
5786 void
5787 decl_debug_expr_insert (tree from, tree to)
5788 {
5789 struct tree_decl_map *h;
5790
5791 h = ggc_alloc<tree_decl_map> ();
5792 h->base.from = from;
5793 h->to = to;
5794 *debug_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
5795 }
5796
5797 /* Lookup a value expression for FROM, and return it if we find one. */
5798
5799 tree
5800 decl_value_expr_lookup (tree from)
5801 {
5802 struct tree_decl_map *h, in;
5803 in.base.from = from;
5804
5805 h = value_expr_for_decl->find_with_hash (&in, DECL_UID (from));
5806 if (h)
5807 return h->to;
5808 return NULL_TREE;
5809 }
5810
5811 /* Insert a mapping FROM->TO in the value expression hashtable. */
5812
5813 void
5814 decl_value_expr_insert (tree from, tree to)
5815 {
5816 struct tree_decl_map *h;
5817
5818 h = ggc_alloc<tree_decl_map> ();
5819 h->base.from = from;
5820 h->to = to;
5821 *value_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
5822 }
5823
5824 /* Lookup a vector of debug arguments for FROM, and return it if we
5825 find one. */
5826
5827 vec<tree, va_gc> **
5828 decl_debug_args_lookup (tree from)
5829 {
5830 struct tree_vec_map *h, in;
5831
5832 if (!DECL_HAS_DEBUG_ARGS_P (from))
5833 return NULL;
5834 gcc_checking_assert (debug_args_for_decl != NULL);
5835 in.base.from = from;
5836 h = debug_args_for_decl->find_with_hash (&in, DECL_UID (from));
5837 if (h)
5838 return &h->to;
5839 return NULL;
5840 }
5841
5842 /* Insert a mapping FROM->empty vector of debug arguments in the value
5843 expression hashtable. */
5844
5845 vec<tree, va_gc> **
5846 decl_debug_args_insert (tree from)
5847 {
5848 struct tree_vec_map *h;
5849 tree_vec_map **loc;
5850
5851 if (DECL_HAS_DEBUG_ARGS_P (from))
5852 return decl_debug_args_lookup (from);
5853 if (debug_args_for_decl == NULL)
5854 debug_args_for_decl = hash_table<tree_vec_map_cache_hasher>::create_ggc (64);
5855 h = ggc_alloc<tree_vec_map> ();
5856 h->base.from = from;
5857 h->to = NULL;
5858 loc = debug_args_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT);
5859 *loc = h;
5860 DECL_HAS_DEBUG_ARGS_P (from) = 1;
5861 return &h->to;
5862 }
5863
5864 /* Hashing of types so that we don't make duplicates.
5865 The entry point is `type_hash_canon'. */
5866
5867 /* Generate the default hash code for TYPE. This is designed for
5868 speed, rather than maximum entropy. */
5869
5870 hashval_t
5871 type_hash_canon_hash (tree type)
5872 {
5873 inchash::hash hstate;
5874
5875 hstate.add_int (TREE_CODE (type));
5876
5877 if (TREE_TYPE (type))
5878 hstate.add_object (TYPE_HASH (TREE_TYPE (type)));
5879
5880 for (tree t = TYPE_ATTRIBUTES (type); t; t = TREE_CHAIN (t))
5881 /* Just the identifier is adequate to distinguish. */
5882 hstate.add_object (IDENTIFIER_HASH_VALUE (get_attribute_name (t)));
5883
5884 switch (TREE_CODE (type))
5885 {
5886 case METHOD_TYPE:
5887 hstate.add_object (TYPE_HASH (TYPE_METHOD_BASETYPE (type)));
5888 /* FALLTHROUGH. */
5889 case FUNCTION_TYPE:
5890 for (tree t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
5891 if (TREE_VALUE (t) != error_mark_node)
5892 hstate.add_object (TYPE_HASH (TREE_VALUE (t)));
5893 break;
5894
5895 case OFFSET_TYPE:
5896 hstate.add_object (TYPE_HASH (TYPE_OFFSET_BASETYPE (type)));
5897 break;
5898
5899 case ARRAY_TYPE:
5900 {
5901 if (TYPE_DOMAIN (type))
5902 hstate.add_object (TYPE_HASH (TYPE_DOMAIN (type)));
5903 if (!AGGREGATE_TYPE_P (TREE_TYPE (type)))
5904 {
5905 unsigned typeless = TYPE_TYPELESS_STORAGE (type);
5906 hstate.add_object (typeless);
5907 }
5908 }
5909 break;
5910
5911 case INTEGER_TYPE:
5912 {
5913 tree t = TYPE_MAX_VALUE (type);
5914 if (!t)
5915 t = TYPE_MIN_VALUE (type);
5916 for (int i = 0; i < TREE_INT_CST_NUNITS (t); i++)
5917 hstate.add_object (TREE_INT_CST_ELT (t, i));
5918 break;
5919 }
5920
5921 case REAL_TYPE:
5922 case FIXED_POINT_TYPE:
5923 {
5924 unsigned prec = TYPE_PRECISION (type);
5925 hstate.add_object (prec);
5926 break;
5927 }
5928
5929 case VECTOR_TYPE:
5930 hstate.add_poly_int (TYPE_VECTOR_SUBPARTS (type));
5931 break;
5932
5933 default:
5934 break;
5935 }
5936
5937 return hstate.end ();
5938 }
5939
5940 /* These are the Hashtable callback functions. */
5941
5942 /* Returns true iff the types are equivalent. */
5943
5944 bool
5945 type_cache_hasher::equal (type_hash *a, type_hash *b)
5946 {
5947 /* First test the things that are the same for all types. */
5948 if (a->hash != b->hash
5949 || TREE_CODE (a->type) != TREE_CODE (b->type)
5950 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
5951 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
5952 TYPE_ATTRIBUTES (b->type))
5953 || (TREE_CODE (a->type) != COMPLEX_TYPE
5954 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
5955 return 0;
5956
5957 /* Be careful about comparing arrays before and after the element type
5958 has been completed; don't compare TYPE_ALIGN unless both types are
5959 complete. */
5960 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
5961 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
5962 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
5963 return 0;
5964
5965 switch (TREE_CODE (a->type))
5966 {
5967 case VOID_TYPE:
5968 case OPAQUE_TYPE:
5969 case COMPLEX_TYPE:
5970 case POINTER_TYPE:
5971 case REFERENCE_TYPE:
5972 case NULLPTR_TYPE:
5973 return 1;
5974
5975 case VECTOR_TYPE:
5976 return known_eq (TYPE_VECTOR_SUBPARTS (a->type),
5977 TYPE_VECTOR_SUBPARTS (b->type));
5978
5979 case ENUMERAL_TYPE:
5980 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
5981 && !(TYPE_VALUES (a->type)
5982 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
5983 && TYPE_VALUES (b->type)
5984 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
5985 && type_list_equal (TYPE_VALUES (a->type),
5986 TYPE_VALUES (b->type))))
5987 return 0;
5988
5989 /* fall through */
5990
5991 case INTEGER_TYPE:
5992 case REAL_TYPE:
5993 case BOOLEAN_TYPE:
5994 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
5995 return false;
5996 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
5997 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
5998 TYPE_MAX_VALUE (b->type)))
5999 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6000 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6001 TYPE_MIN_VALUE (b->type))));
6002
6003 case FIXED_POINT_TYPE:
6004 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6005
6006 case OFFSET_TYPE:
6007 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6008
6009 case METHOD_TYPE:
6010 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6011 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6012 || (TYPE_ARG_TYPES (a->type)
6013 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6014 && TYPE_ARG_TYPES (b->type)
6015 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6016 && type_list_equal (TYPE_ARG_TYPES (a->type),
6017 TYPE_ARG_TYPES (b->type)))))
6018 break;
6019 return 0;
6020 case ARRAY_TYPE:
6021 /* Don't compare TYPE_TYPELESS_STORAGE flag on aggregates,
6022 where the flag should be inherited from the element type
6023 and can change after ARRAY_TYPEs are created; on non-aggregates
6024 compare it and hash it, scalars will never have that flag set
6025 and we need to differentiate between arrays created by different
6026 front-ends or middle-end created arrays. */
6027 return (TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type)
6028 && (AGGREGATE_TYPE_P (TREE_TYPE (a->type))
6029 || (TYPE_TYPELESS_STORAGE (a->type)
6030 == TYPE_TYPELESS_STORAGE (b->type))));
6031
6032 case RECORD_TYPE:
6033 case UNION_TYPE:
6034 case QUAL_UNION_TYPE:
6035 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6036 || (TYPE_FIELDS (a->type)
6037 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6038 && TYPE_FIELDS (b->type)
6039 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6040 && type_list_equal (TYPE_FIELDS (a->type),
6041 TYPE_FIELDS (b->type))));
6042
6043 case FUNCTION_TYPE:
6044 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6045 || (TYPE_ARG_TYPES (a->type)
6046 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6047 && TYPE_ARG_TYPES (b->type)
6048 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6049 && type_list_equal (TYPE_ARG_TYPES (a->type),
6050 TYPE_ARG_TYPES (b->type))))
6051 break;
6052 return 0;
6053
6054 default:
6055 return 0;
6056 }
6057
6058 if (lang_hooks.types.type_hash_eq != NULL)
6059 return lang_hooks.types.type_hash_eq (a->type, b->type);
6060
6061 return 1;
6062 }
6063
6064 /* Given TYPE, and HASHCODE its hash code, return the canonical
6065 object for an identical type if one already exists.
6066 Otherwise, return TYPE, and record it as the canonical object.
6067
6068 To use this function, first create a type of the sort you want.
6069 Then compute its hash code from the fields of the type that
6070 make it different from other similar types.
6071 Then call this function and use the value. */
6072
6073 tree
6074 type_hash_canon (unsigned int hashcode, tree type)
6075 {
6076 type_hash in;
6077 type_hash **loc;
6078
6079 /* The hash table only contains main variants, so ensure that's what we're
6080 being passed. */
6081 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6082
6083 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6084 must call that routine before comparing TYPE_ALIGNs. */
6085 layout_type (type);
6086
6087 in.hash = hashcode;
6088 in.type = type;
6089
6090 loc = type_hash_table->find_slot_with_hash (&in, hashcode, INSERT);
6091 if (*loc)
6092 {
6093 tree t1 = ((type_hash *) *loc)->type;
6094 gcc_assert (TYPE_MAIN_VARIANT (t1) == t1
6095 && t1 != type);
6096 if (TYPE_UID (type) + 1 == next_type_uid)
6097 --next_type_uid;
6098 /* Free also min/max values and the cache for integer
6099 types. This can't be done in free_node, as LTO frees
6100 those on its own. */
6101 if (TREE_CODE (type) == INTEGER_TYPE)
6102 {
6103 if (TYPE_MIN_VALUE (type)
6104 && TREE_TYPE (TYPE_MIN_VALUE (type)) == type)
6105 {
6106 /* Zero is always in TYPE_CACHED_VALUES. */
6107 if (! TYPE_UNSIGNED (type))
6108 int_cst_hash_table->remove_elt (TYPE_MIN_VALUE (type));
6109 ggc_free (TYPE_MIN_VALUE (type));
6110 }
6111 if (TYPE_MAX_VALUE (type)
6112 && TREE_TYPE (TYPE_MAX_VALUE (type)) == type)
6113 {
6114 int_cst_hash_table->remove_elt (TYPE_MAX_VALUE (type));
6115 ggc_free (TYPE_MAX_VALUE (type));
6116 }
6117 if (TYPE_CACHED_VALUES_P (type))
6118 ggc_free (TYPE_CACHED_VALUES (type));
6119 }
6120 free_node (type);
6121 return t1;
6122 }
6123 else
6124 {
6125 struct type_hash *h;
6126
6127 h = ggc_alloc<type_hash> ();
6128 h->hash = hashcode;
6129 h->type = type;
6130 *loc = h;
6131
6132 return type;
6133 }
6134 }
6135
6136 static void
6137 print_type_hash_statistics (void)
6138 {
6139 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6140 (long) type_hash_table->size (),
6141 (long) type_hash_table->elements (),
6142 type_hash_table->collisions ());
6143 }
6144
6145 /* Given two lists of types
6146 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6147 return 1 if the lists contain the same types in the same order.
6148 Also, the TREE_PURPOSEs must match. */
6149
6150 bool
6151 type_list_equal (const_tree l1, const_tree l2)
6152 {
6153 const_tree t1, t2;
6154
6155 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6156 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6157 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6158 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6159 && (TREE_TYPE (TREE_PURPOSE (t1))
6160 == TREE_TYPE (TREE_PURPOSE (t2))))))
6161 return false;
6162
6163 return t1 == t2;
6164 }
6165
6166 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6167 given by TYPE. If the argument list accepts variable arguments,
6168 then this function counts only the ordinary arguments. */
6169
6170 int
6171 type_num_arguments (const_tree fntype)
6172 {
6173 int i = 0;
6174
6175 for (tree t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
6176 /* If the function does not take a variable number of arguments,
6177 the last element in the list will have type `void'. */
6178 if (VOID_TYPE_P (TREE_VALUE (t)))
6179 break;
6180 else
6181 ++i;
6182
6183 return i;
6184 }
6185
6186 /* Return the type of the function TYPE's argument ARGNO if known.
6187 For vararg function's where ARGNO refers to one of the variadic
6188 arguments return null. Otherwise, return a void_type_node for
6189 out-of-bounds ARGNO. */
6190
6191 tree
6192 type_argument_type (const_tree fntype, unsigned argno)
6193 {
6194 /* Treat zero the same as an out-of-bounds argument number. */
6195 if (!argno)
6196 return void_type_node;
6197
6198 function_args_iterator iter;
6199
6200 tree argtype;
6201 unsigned i = 1;
6202 FOREACH_FUNCTION_ARGS (fntype, argtype, iter)
6203 {
6204 /* A vararg function's argument list ends in a null. Otherwise,
6205 an ordinary function's argument list ends with void. Return
6206 null if ARGNO refers to a vararg argument, void_type_node if
6207 it's out of bounds, and the formal argument type otherwise. */
6208 if (!argtype)
6209 break;
6210
6211 if (i == argno || VOID_TYPE_P (argtype))
6212 return argtype;
6213
6214 ++i;
6215 }
6216
6217 return NULL_TREE;
6218 }
6219
6220 /* Nonzero if integer constants T1 and T2
6221 represent the same constant value. */
6222
6223 int
6224 tree_int_cst_equal (const_tree t1, const_tree t2)
6225 {
6226 if (t1 == t2)
6227 return 1;
6228
6229 if (t1 == 0 || t2 == 0)
6230 return 0;
6231
6232 STRIP_ANY_LOCATION_WRAPPER (t1);
6233 STRIP_ANY_LOCATION_WRAPPER (t2);
6234
6235 if (TREE_CODE (t1) == INTEGER_CST
6236 && TREE_CODE (t2) == INTEGER_CST
6237 && wi::to_widest (t1) == wi::to_widest (t2))
6238 return 1;
6239
6240 return 0;
6241 }
6242
6243 /* Return true if T is an INTEGER_CST whose numerical value (extended
6244 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
6245
6246 bool
6247 tree_fits_shwi_p (const_tree t)
6248 {
6249 return (t != NULL_TREE
6250 && TREE_CODE (t) == INTEGER_CST
6251 && wi::fits_shwi_p (wi::to_widest (t)));
6252 }
6253
6254 /* Return true if T is an INTEGER_CST or POLY_INT_CST whose numerical
6255 value (extended according to TYPE_UNSIGNED) fits in a poly_int64. */
6256
6257 bool
6258 tree_fits_poly_int64_p (const_tree t)
6259 {
6260 if (t == NULL_TREE)
6261 return false;
6262 if (POLY_INT_CST_P (t))
6263 {
6264 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; i++)
6265 if (!wi::fits_shwi_p (wi::to_wide (POLY_INT_CST_COEFF (t, i))))
6266 return false;
6267 return true;
6268 }
6269 return (TREE_CODE (t) == INTEGER_CST
6270 && wi::fits_shwi_p (wi::to_widest (t)));
6271 }
6272
6273 /* Return true if T is an INTEGER_CST whose numerical value (extended
6274 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
6275
6276 bool
6277 tree_fits_uhwi_p (const_tree t)
6278 {
6279 return (t != NULL_TREE
6280 && TREE_CODE (t) == INTEGER_CST
6281 && wi::fits_uhwi_p (wi::to_widest (t)));
6282 }
6283
6284 /* Return true if T is an INTEGER_CST or POLY_INT_CST whose numerical
6285 value (extended according to TYPE_UNSIGNED) fits in a poly_uint64. */
6286
6287 bool
6288 tree_fits_poly_uint64_p (const_tree t)
6289 {
6290 if (t == NULL_TREE)
6291 return false;
6292 if (POLY_INT_CST_P (t))
6293 {
6294 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; i++)
6295 if (!wi::fits_uhwi_p (wi::to_widest (POLY_INT_CST_COEFF (t, i))))
6296 return false;
6297 return true;
6298 }
6299 return (TREE_CODE (t) == INTEGER_CST
6300 && wi::fits_uhwi_p (wi::to_widest (t)));
6301 }
6302
6303 /* T is an INTEGER_CST whose numerical value (extended according to
6304 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
6305 HOST_WIDE_INT. */
6306
6307 HOST_WIDE_INT
6308 tree_to_shwi (const_tree t)
6309 {
6310 gcc_assert (tree_fits_shwi_p (t));
6311 return TREE_INT_CST_LOW (t);
6312 }
6313
6314 /* T is an INTEGER_CST whose numerical value (extended according to
6315 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
6316 HOST_WIDE_INT. */
6317
6318 unsigned HOST_WIDE_INT
6319 tree_to_uhwi (const_tree t)
6320 {
6321 gcc_assert (tree_fits_uhwi_p (t));
6322 return TREE_INT_CST_LOW (t);
6323 }
6324
6325 /* Return the most significant (sign) bit of T. */
6326
6327 int
6328 tree_int_cst_sign_bit (const_tree t)
6329 {
6330 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
6331
6332 return wi::extract_uhwi (wi::to_wide (t), bitno, 1);
6333 }
6334
6335 /* Return an indication of the sign of the integer constant T.
6336 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
6337 Note that -1 will never be returned if T's type is unsigned. */
6338
6339 int
6340 tree_int_cst_sgn (const_tree t)
6341 {
6342 if (wi::to_wide (t) == 0)
6343 return 0;
6344 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
6345 return 1;
6346 else if (wi::neg_p (wi::to_wide (t)))
6347 return -1;
6348 else
6349 return 1;
6350 }
6351
6352 /* Return the minimum number of bits needed to represent VALUE in a
6353 signed or unsigned type, UNSIGNEDP says which. */
6354
6355 unsigned int
6356 tree_int_cst_min_precision (tree value, signop sgn)
6357 {
6358 /* If the value is negative, compute its negative minus 1. The latter
6359 adjustment is because the absolute value of the largest negative value
6360 is one larger than the largest positive value. This is equivalent to
6361 a bit-wise negation, so use that operation instead. */
6362
6363 if (tree_int_cst_sgn (value) < 0)
6364 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
6365
6366 /* Return the number of bits needed, taking into account the fact
6367 that we need one more bit for a signed than unsigned type.
6368 If value is 0 or -1, the minimum precision is 1 no matter
6369 whether unsignedp is true or false. */
6370
6371 if (integer_zerop (value))
6372 return 1;
6373 else
6374 return tree_floor_log2 (value) + 1 + (sgn == SIGNED ? 1 : 0) ;
6375 }
6376
6377 /* Return truthvalue of whether T1 is the same tree structure as T2.
6378 Return 1 if they are the same.
6379 Return 0 if they are understandably different.
6380 Return -1 if either contains tree structure not understood by
6381 this function. */
6382
6383 int
6384 simple_cst_equal (const_tree t1, const_tree t2)
6385 {
6386 enum tree_code code1, code2;
6387 int cmp;
6388 int i;
6389
6390 if (t1 == t2)
6391 return 1;
6392 if (t1 == 0 || t2 == 0)
6393 return 0;
6394
6395 /* For location wrappers to be the same, they must be at the same
6396 source location (and wrap the same thing). */
6397 if (location_wrapper_p (t1) && location_wrapper_p (t2))
6398 {
6399 if (EXPR_LOCATION (t1) != EXPR_LOCATION (t2))
6400 return 0;
6401 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6402 }
6403
6404 code1 = TREE_CODE (t1);
6405 code2 = TREE_CODE (t2);
6406
6407 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
6408 {
6409 if (CONVERT_EXPR_CODE_P (code2)
6410 || code2 == NON_LVALUE_EXPR)
6411 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6412 else
6413 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
6414 }
6415
6416 else if (CONVERT_EXPR_CODE_P (code2)
6417 || code2 == NON_LVALUE_EXPR)
6418 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
6419
6420 if (code1 != code2)
6421 return 0;
6422
6423 switch (code1)
6424 {
6425 case INTEGER_CST:
6426 return wi::to_widest (t1) == wi::to_widest (t2);
6427
6428 case REAL_CST:
6429 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
6430
6431 case FIXED_CST:
6432 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
6433
6434 case STRING_CST:
6435 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
6436 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
6437 TREE_STRING_LENGTH (t1)));
6438
6439 case CONSTRUCTOR:
6440 {
6441 unsigned HOST_WIDE_INT idx;
6442 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
6443 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
6444
6445 if (vec_safe_length (v1) != vec_safe_length (v2))
6446 return false;
6447
6448 for (idx = 0; idx < vec_safe_length (v1); ++idx)
6449 /* ??? Should we handle also fields here? */
6450 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
6451 return false;
6452 return true;
6453 }
6454
6455 case SAVE_EXPR:
6456 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6457
6458 case CALL_EXPR:
6459 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
6460 if (cmp <= 0)
6461 return cmp;
6462 if (call_expr_nargs (t1) != call_expr_nargs (t2))
6463 return 0;
6464 {
6465 const_tree arg1, arg2;
6466 const_call_expr_arg_iterator iter1, iter2;
6467 for (arg1 = first_const_call_expr_arg (t1, &iter1),
6468 arg2 = first_const_call_expr_arg (t2, &iter2);
6469 arg1 && arg2;
6470 arg1 = next_const_call_expr_arg (&iter1),
6471 arg2 = next_const_call_expr_arg (&iter2))
6472 {
6473 cmp = simple_cst_equal (arg1, arg2);
6474 if (cmp <= 0)
6475 return cmp;
6476 }
6477 return arg1 == arg2;
6478 }
6479
6480 case TARGET_EXPR:
6481 /* Special case: if either target is an unallocated VAR_DECL,
6482 it means that it's going to be unified with whatever the
6483 TARGET_EXPR is really supposed to initialize, so treat it
6484 as being equivalent to anything. */
6485 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
6486 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
6487 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
6488 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
6489 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
6490 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
6491 cmp = 1;
6492 else
6493 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6494
6495 if (cmp <= 0)
6496 return cmp;
6497
6498 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
6499
6500 case WITH_CLEANUP_EXPR:
6501 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6502 if (cmp <= 0)
6503 return cmp;
6504
6505 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
6506
6507 case COMPONENT_REF:
6508 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
6509 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6510
6511 return 0;
6512
6513 case VAR_DECL:
6514 case PARM_DECL:
6515 case CONST_DECL:
6516 case FUNCTION_DECL:
6517 return 0;
6518
6519 default:
6520 if (POLY_INT_CST_P (t1))
6521 /* A false return means maybe_ne rather than known_ne. */
6522 return known_eq (poly_widest_int::from (poly_int_cst_value (t1),
6523 TYPE_SIGN (TREE_TYPE (t1))),
6524 poly_widest_int::from (poly_int_cst_value (t2),
6525 TYPE_SIGN (TREE_TYPE (t2))));
6526 break;
6527 }
6528
6529 /* This general rule works for most tree codes. All exceptions should be
6530 handled above. If this is a language-specific tree code, we can't
6531 trust what might be in the operand, so say we don't know
6532 the situation. */
6533 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
6534 return -1;
6535
6536 switch (TREE_CODE_CLASS (code1))
6537 {
6538 case tcc_unary:
6539 case tcc_binary:
6540 case tcc_comparison:
6541 case tcc_expression:
6542 case tcc_reference:
6543 case tcc_statement:
6544 cmp = 1;
6545 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
6546 {
6547 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
6548 if (cmp <= 0)
6549 return cmp;
6550 }
6551
6552 return cmp;
6553
6554 default:
6555 return -1;
6556 }
6557 }
6558
6559 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
6560 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
6561 than U, respectively. */
6562
6563 int
6564 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
6565 {
6566 if (tree_int_cst_sgn (t) < 0)
6567 return -1;
6568 else if (!tree_fits_uhwi_p (t))
6569 return 1;
6570 else if (TREE_INT_CST_LOW (t) == u)
6571 return 0;
6572 else if (TREE_INT_CST_LOW (t) < u)
6573 return -1;
6574 else
6575 return 1;
6576 }
6577
6578 /* Return true if SIZE represents a constant size that is in bounds of
6579 what the middle-end and the backend accepts (covering not more than
6580 half of the address-space).
6581 When PERR is non-null, set *PERR on failure to the description of
6582 why SIZE is not valid. */
6583
6584 bool
6585 valid_constant_size_p (const_tree size, cst_size_error *perr /* = NULL */)
6586 {
6587 if (POLY_INT_CST_P (size))
6588 {
6589 if (TREE_OVERFLOW (size))
6590 return false;
6591 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
6592 if (!valid_constant_size_p (POLY_INT_CST_COEFF (size, i)))
6593 return false;
6594 return true;
6595 }
6596
6597 cst_size_error error;
6598 if (!perr)
6599 perr = &error;
6600
6601 if (TREE_CODE (size) != INTEGER_CST)
6602 {
6603 *perr = cst_size_not_constant;
6604 return false;
6605 }
6606
6607 if (TREE_OVERFLOW_P (size))
6608 {
6609 *perr = cst_size_overflow;
6610 return false;
6611 }
6612
6613 if (tree_int_cst_sgn (size) < 0)
6614 {
6615 *perr = cst_size_negative;
6616 return false;
6617 }
6618 if (!tree_fits_uhwi_p (size)
6619 || (wi::to_widest (TYPE_MAX_VALUE (sizetype))
6620 < wi::to_widest (size) * 2))
6621 {
6622 *perr = cst_size_too_big;
6623 return false;
6624 }
6625
6626 return true;
6627 }
6628
6629 /* Return the precision of the type, or for a complex or vector type the
6630 precision of the type of its elements. */
6631
6632 unsigned int
6633 element_precision (const_tree type)
6634 {
6635 if (!TYPE_P (type))
6636 type = TREE_TYPE (type);
6637 enum tree_code code = TREE_CODE (type);
6638 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
6639 type = TREE_TYPE (type);
6640
6641 return TYPE_PRECISION (type);
6642 }
6643
6644 /* Return true if CODE represents an associative tree code. Otherwise
6645 return false. */
6646 bool
6647 associative_tree_code (enum tree_code code)
6648 {
6649 switch (code)
6650 {
6651 case BIT_IOR_EXPR:
6652 case BIT_AND_EXPR:
6653 case BIT_XOR_EXPR:
6654 case PLUS_EXPR:
6655 case MULT_EXPR:
6656 case MIN_EXPR:
6657 case MAX_EXPR:
6658 return true;
6659
6660 default:
6661 break;
6662 }
6663 return false;
6664 }
6665
6666 /* Return true if CODE represents a commutative tree code. Otherwise
6667 return false. */
6668 bool
6669 commutative_tree_code (enum tree_code code)
6670 {
6671 switch (code)
6672 {
6673 case PLUS_EXPR:
6674 case MULT_EXPR:
6675 case MULT_HIGHPART_EXPR:
6676 case MIN_EXPR:
6677 case MAX_EXPR:
6678 case BIT_IOR_EXPR:
6679 case BIT_XOR_EXPR:
6680 case BIT_AND_EXPR:
6681 case NE_EXPR:
6682 case EQ_EXPR:
6683 case UNORDERED_EXPR:
6684 case ORDERED_EXPR:
6685 case UNEQ_EXPR:
6686 case LTGT_EXPR:
6687 case TRUTH_AND_EXPR:
6688 case TRUTH_XOR_EXPR:
6689 case TRUTH_OR_EXPR:
6690 case WIDEN_MULT_EXPR:
6691 case VEC_WIDEN_MULT_HI_EXPR:
6692 case VEC_WIDEN_MULT_LO_EXPR:
6693 case VEC_WIDEN_MULT_EVEN_EXPR:
6694 case VEC_WIDEN_MULT_ODD_EXPR:
6695 return true;
6696
6697 default:
6698 break;
6699 }
6700 return false;
6701 }
6702
6703 /* Return true if CODE represents a ternary tree code for which the
6704 first two operands are commutative. Otherwise return false. */
6705 bool
6706 commutative_ternary_tree_code (enum tree_code code)
6707 {
6708 switch (code)
6709 {
6710 case WIDEN_MULT_PLUS_EXPR:
6711 case WIDEN_MULT_MINUS_EXPR:
6712 case DOT_PROD_EXPR:
6713 return true;
6714
6715 default:
6716 break;
6717 }
6718 return false;
6719 }
6720
6721 /* Returns true if CODE can overflow. */
6722
6723 bool
6724 operation_can_overflow (enum tree_code code)
6725 {
6726 switch (code)
6727 {
6728 case PLUS_EXPR:
6729 case MINUS_EXPR:
6730 case MULT_EXPR:
6731 case LSHIFT_EXPR:
6732 /* Can overflow in various ways. */
6733 return true;
6734 case TRUNC_DIV_EXPR:
6735 case EXACT_DIV_EXPR:
6736 case FLOOR_DIV_EXPR:
6737 case CEIL_DIV_EXPR:
6738 /* For INT_MIN / -1. */
6739 return true;
6740 case NEGATE_EXPR:
6741 case ABS_EXPR:
6742 /* For -INT_MIN. */
6743 return true;
6744 default:
6745 /* These operators cannot overflow. */
6746 return false;
6747 }
6748 }
6749
6750 /* Returns true if CODE operating on operands of type TYPE doesn't overflow, or
6751 ftrapv doesn't generate trapping insns for CODE. */
6752
6753 bool
6754 operation_no_trapping_overflow (tree type, enum tree_code code)
6755 {
6756 gcc_checking_assert (ANY_INTEGRAL_TYPE_P (type));
6757
6758 /* We don't generate instructions that trap on overflow for complex or vector
6759 types. */
6760 if (!INTEGRAL_TYPE_P (type))
6761 return true;
6762
6763 if (!TYPE_OVERFLOW_TRAPS (type))
6764 return true;
6765
6766 switch (code)
6767 {
6768 case PLUS_EXPR:
6769 case MINUS_EXPR:
6770 case MULT_EXPR:
6771 case NEGATE_EXPR:
6772 case ABS_EXPR:
6773 /* These operators can overflow, and -ftrapv generates trapping code for
6774 these. */
6775 return false;
6776 case TRUNC_DIV_EXPR:
6777 case EXACT_DIV_EXPR:
6778 case FLOOR_DIV_EXPR:
6779 case CEIL_DIV_EXPR:
6780 case LSHIFT_EXPR:
6781 /* These operators can overflow, but -ftrapv does not generate trapping
6782 code for these. */
6783 return true;
6784 default:
6785 /* These operators cannot overflow. */
6786 return true;
6787 }
6788 }
6789
6790 /* Constructors for pointer, array and function types.
6791 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
6792 constructed by language-dependent code, not here.) */
6793
6794 /* Construct, lay out and return the type of pointers to TO_TYPE with
6795 mode MODE. If MODE is VOIDmode, a pointer mode for the address
6796 space of TO_TYPE will be picked. If CAN_ALIAS_ALL is TRUE,
6797 indicate this type can reference all of memory. If such a type has
6798 already been constructed, reuse it. */
6799
6800 tree
6801 build_pointer_type_for_mode (tree to_type, machine_mode mode,
6802 bool can_alias_all)
6803 {
6804 tree t;
6805 bool could_alias = can_alias_all;
6806
6807 if (to_type == error_mark_node)
6808 return error_mark_node;
6809
6810 if (mode == VOIDmode)
6811 {
6812 addr_space_t as = TYPE_ADDR_SPACE (to_type);
6813 mode = targetm.addr_space.pointer_mode (as);
6814 }
6815
6816 /* If the pointed-to type has the may_alias attribute set, force
6817 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
6818 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
6819 can_alias_all = true;
6820
6821 /* In some cases, languages will have things that aren't a POINTER_TYPE
6822 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
6823 In that case, return that type without regard to the rest of our
6824 operands.
6825
6826 ??? This is a kludge, but consistent with the way this function has
6827 always operated and there doesn't seem to be a good way to avoid this
6828 at the moment. */
6829 if (TYPE_POINTER_TO (to_type) != 0
6830 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
6831 return TYPE_POINTER_TO (to_type);
6832
6833 /* First, if we already have a type for pointers to TO_TYPE and it's
6834 the proper mode, use it. */
6835 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
6836 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
6837 return t;
6838
6839 t = make_node (POINTER_TYPE);
6840
6841 TREE_TYPE (t) = to_type;
6842 SET_TYPE_MODE (t, mode);
6843 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
6844 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
6845 TYPE_POINTER_TO (to_type) = t;
6846
6847 /* During LTO we do not set TYPE_CANONICAL of pointers and references. */
6848 if (TYPE_STRUCTURAL_EQUALITY_P (to_type) || in_lto_p)
6849 SET_TYPE_STRUCTURAL_EQUALITY (t);
6850 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
6851 TYPE_CANONICAL (t)
6852 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
6853 mode, false);
6854
6855 /* Lay out the type. This function has many callers that are concerned
6856 with expression-construction, and this simplifies them all. */
6857 layout_type (t);
6858
6859 return t;
6860 }
6861
6862 /* By default build pointers in ptr_mode. */
6863
6864 tree
6865 build_pointer_type (tree to_type)
6866 {
6867 return build_pointer_type_for_mode (to_type, VOIDmode, false);
6868 }
6869
6870 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
6871
6872 tree
6873 build_reference_type_for_mode (tree to_type, machine_mode mode,
6874 bool can_alias_all)
6875 {
6876 tree t;
6877 bool could_alias = can_alias_all;
6878
6879 if (to_type == error_mark_node)
6880 return error_mark_node;
6881
6882 if (mode == VOIDmode)
6883 {
6884 addr_space_t as = TYPE_ADDR_SPACE (to_type);
6885 mode = targetm.addr_space.pointer_mode (as);
6886 }
6887
6888 /* If the pointed-to type has the may_alias attribute set, force
6889 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
6890 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
6891 can_alias_all = true;
6892
6893 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
6894 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
6895 In that case, return that type without regard to the rest of our
6896 operands.
6897
6898 ??? This is a kludge, but consistent with the way this function has
6899 always operated and there doesn't seem to be a good way to avoid this
6900 at the moment. */
6901 if (TYPE_REFERENCE_TO (to_type) != 0
6902 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
6903 return TYPE_REFERENCE_TO (to_type);
6904
6905 /* First, if we already have a type for pointers to TO_TYPE and it's
6906 the proper mode, use it. */
6907 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
6908 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
6909 return t;
6910
6911 t = make_node (REFERENCE_TYPE);
6912
6913 TREE_TYPE (t) = to_type;
6914 SET_TYPE_MODE (t, mode);
6915 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
6916 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
6917 TYPE_REFERENCE_TO (to_type) = t;
6918
6919 /* During LTO we do not set TYPE_CANONICAL of pointers and references. */
6920 if (TYPE_STRUCTURAL_EQUALITY_P (to_type) || in_lto_p)
6921 SET_TYPE_STRUCTURAL_EQUALITY (t);
6922 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
6923 TYPE_CANONICAL (t)
6924 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
6925 mode, false);
6926
6927 layout_type (t);
6928
6929 return t;
6930 }
6931
6932
6933 /* Build the node for the type of references-to-TO_TYPE by default
6934 in ptr_mode. */
6935
6936 tree
6937 build_reference_type (tree to_type)
6938 {
6939 return build_reference_type_for_mode (to_type, VOIDmode, false);
6940 }
6941
6942 #define MAX_INT_CACHED_PREC \
6943 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
6944 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
6945
6946 /* Builds a signed or unsigned integer type of precision PRECISION.
6947 Used for C bitfields whose precision does not match that of
6948 built-in target types. */
6949 tree
6950 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
6951 int unsignedp)
6952 {
6953 tree itype, ret;
6954
6955 if (unsignedp)
6956 unsignedp = MAX_INT_CACHED_PREC + 1;
6957
6958 if (precision <= MAX_INT_CACHED_PREC)
6959 {
6960 itype = nonstandard_integer_type_cache[precision + unsignedp];
6961 if (itype)
6962 return itype;
6963 }
6964
6965 itype = make_node (INTEGER_TYPE);
6966 TYPE_PRECISION (itype) = precision;
6967
6968 if (unsignedp)
6969 fixup_unsigned_type (itype);
6970 else
6971 fixup_signed_type (itype);
6972
6973 inchash::hash hstate;
6974 inchash::add_expr (TYPE_MAX_VALUE (itype), hstate);
6975 ret = type_hash_canon (hstate.end (), itype);
6976 if (precision <= MAX_INT_CACHED_PREC)
6977 nonstandard_integer_type_cache[precision + unsignedp] = ret;
6978
6979 return ret;
6980 }
6981
6982 #define MAX_BOOL_CACHED_PREC \
6983 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
6984 static GTY(()) tree nonstandard_boolean_type_cache[MAX_BOOL_CACHED_PREC + 1];
6985
6986 /* Builds a boolean type of precision PRECISION.
6987 Used for boolean vectors to choose proper vector element size. */
6988 tree
6989 build_nonstandard_boolean_type (unsigned HOST_WIDE_INT precision)
6990 {
6991 tree type;
6992
6993 if (precision <= MAX_BOOL_CACHED_PREC)
6994 {
6995 type = nonstandard_boolean_type_cache[precision];
6996 if (type)
6997 return type;
6998 }
6999
7000 type = make_node (BOOLEAN_TYPE);
7001 TYPE_PRECISION (type) = precision;
7002 fixup_signed_type (type);
7003
7004 if (precision <= MAX_INT_CACHED_PREC)
7005 nonstandard_boolean_type_cache[precision] = type;
7006
7007 return type;
7008 }
7009
7010 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7011 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7012 is true, reuse such a type that has already been constructed. */
7013
7014 static tree
7015 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7016 {
7017 tree itype = make_node (INTEGER_TYPE);
7018
7019 TREE_TYPE (itype) = type;
7020
7021 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7022 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7023
7024 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7025 SET_TYPE_MODE (itype, TYPE_MODE (type));
7026 TYPE_SIZE (itype) = TYPE_SIZE (type);
7027 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7028 SET_TYPE_ALIGN (itype, TYPE_ALIGN (type));
7029 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7030 SET_TYPE_WARN_IF_NOT_ALIGN (itype, TYPE_WARN_IF_NOT_ALIGN (type));
7031
7032 if (!shared)
7033 return itype;
7034
7035 if ((TYPE_MIN_VALUE (itype)
7036 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7037 || (TYPE_MAX_VALUE (itype)
7038 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7039 {
7040 /* Since we cannot reliably merge this type, we need to compare it using
7041 structural equality checks. */
7042 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7043 return itype;
7044 }
7045
7046 hashval_t hash = type_hash_canon_hash (itype);
7047 itype = type_hash_canon (hash, itype);
7048
7049 return itype;
7050 }
7051
7052 /* Wrapper around build_range_type_1 with SHARED set to true. */
7053
7054 tree
7055 build_range_type (tree type, tree lowval, tree highval)
7056 {
7057 return build_range_type_1 (type, lowval, highval, true);
7058 }
7059
7060 /* Wrapper around build_range_type_1 with SHARED set to false. */
7061
7062 tree
7063 build_nonshared_range_type (tree type, tree lowval, tree highval)
7064 {
7065 return build_range_type_1 (type, lowval, highval, false);
7066 }
7067
7068 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7069 MAXVAL should be the maximum value in the domain
7070 (one less than the length of the array).
7071
7072 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7073 We don't enforce this limit, that is up to caller (e.g. language front end).
7074 The limit exists because the result is a signed type and we don't handle
7075 sizes that use more than one HOST_WIDE_INT. */
7076
7077 tree
7078 build_index_type (tree maxval)
7079 {
7080 return build_range_type (sizetype, size_zero_node, maxval);
7081 }
7082
7083 /* Return true if the debug information for TYPE, a subtype, should be emitted
7084 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7085 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7086 debug info and doesn't reflect the source code. */
7087
7088 bool
7089 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7090 {
7091 tree base_type = TREE_TYPE (type), low, high;
7092
7093 /* Subrange types have a base type which is an integral type. */
7094 if (!INTEGRAL_TYPE_P (base_type))
7095 return false;
7096
7097 /* Get the real bounds of the subtype. */
7098 if (lang_hooks.types.get_subrange_bounds)
7099 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7100 else
7101 {
7102 low = TYPE_MIN_VALUE (type);
7103 high = TYPE_MAX_VALUE (type);
7104 }
7105
7106 /* If the type and its base type have the same representation and the same
7107 name, then the type is not a subrange but a copy of the base type. */
7108 if ((TREE_CODE (base_type) == INTEGER_TYPE
7109 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7110 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7111 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7112 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type))
7113 && TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (base_type))
7114 return false;
7115
7116 if (lowval)
7117 *lowval = low;
7118 if (highval)
7119 *highval = high;
7120 return true;
7121 }
7122
7123 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7124 and number of elements specified by the range of values of INDEX_TYPE.
7125 If TYPELESS_STORAGE is true, TYPE_TYPELESS_STORAGE flag is set on the type.
7126 If SHARED is true, reuse such a type that has already been constructed.
7127 If SET_CANONICAL is true, compute TYPE_CANONICAL from the element type. */
7128
7129 tree
7130 build_array_type_1 (tree elt_type, tree index_type, bool typeless_storage,
7131 bool shared, bool set_canonical)
7132 {
7133 tree t;
7134
7135 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7136 {
7137 error ("arrays of functions are not meaningful");
7138 elt_type = integer_type_node;
7139 }
7140
7141 t = make_node (ARRAY_TYPE);
7142 TREE_TYPE (t) = elt_type;
7143 TYPE_DOMAIN (t) = index_type;
7144 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7145 TYPE_TYPELESS_STORAGE (t) = typeless_storage;
7146 layout_type (t);
7147
7148 if (shared)
7149 {
7150 hashval_t hash = type_hash_canon_hash (t);
7151 t = type_hash_canon (hash, t);
7152 }
7153
7154 if (TYPE_CANONICAL (t) == t && set_canonical)
7155 {
7156 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7157 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type))
7158 || in_lto_p)
7159 SET_TYPE_STRUCTURAL_EQUALITY (t);
7160 else if (TYPE_CANONICAL (elt_type) != elt_type
7161 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7162 TYPE_CANONICAL (t)
7163 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7164 index_type
7165 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7166 typeless_storage, shared, set_canonical);
7167 }
7168
7169 return t;
7170 }
7171
7172 /* Wrapper around build_array_type_1 with SHARED set to true. */
7173
7174 tree
7175 build_array_type (tree elt_type, tree index_type, bool typeless_storage)
7176 {
7177 return
7178 build_array_type_1 (elt_type, index_type, typeless_storage, true, true);
7179 }
7180
7181 /* Wrapper around build_array_type_1 with SHARED set to false. */
7182
7183 tree
7184 build_nonshared_array_type (tree elt_type, tree index_type)
7185 {
7186 return build_array_type_1 (elt_type, index_type, false, false, true);
7187 }
7188
7189 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7190 sizetype. */
7191
7192 tree
7193 build_array_type_nelts (tree elt_type, poly_uint64 nelts)
7194 {
7195 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7196 }
7197
7198 /* Recursively examines the array elements of TYPE, until a non-array
7199 element type is found. */
7200
7201 tree
7202 strip_array_types (tree type)
7203 {
7204 while (TREE_CODE (type) == ARRAY_TYPE)
7205 type = TREE_TYPE (type);
7206
7207 return type;
7208 }
7209
7210 /* Computes the canonical argument types from the argument type list
7211 ARGTYPES.
7212
7213 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7214 on entry to this function, or if any of the ARGTYPES are
7215 structural.
7216
7217 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7218 true on entry to this function, or if any of the ARGTYPES are
7219 non-canonical.
7220
7221 Returns a canonical argument list, which may be ARGTYPES when the
7222 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7223 true) or would not differ from ARGTYPES. */
7224
7225 static tree
7226 maybe_canonicalize_argtypes (tree argtypes,
7227 bool *any_structural_p,
7228 bool *any_noncanonical_p)
7229 {
7230 tree arg;
7231 bool any_noncanonical_argtypes_p = false;
7232
7233 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7234 {
7235 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7236 /* Fail gracefully by stating that the type is structural. */
7237 *any_structural_p = true;
7238 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7239 *any_structural_p = true;
7240 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7241 || TREE_PURPOSE (arg))
7242 /* If the argument has a default argument, we consider it
7243 non-canonical even though the type itself is canonical.
7244 That way, different variants of function and method types
7245 with default arguments will all point to the variant with
7246 no defaults as their canonical type. */
7247 any_noncanonical_argtypes_p = true;
7248 }
7249
7250 if (*any_structural_p)
7251 return argtypes;
7252
7253 if (any_noncanonical_argtypes_p)
7254 {
7255 /* Build the canonical list of argument types. */
7256 tree canon_argtypes = NULL_TREE;
7257 bool is_void = false;
7258
7259 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7260 {
7261 if (arg == void_list_node)
7262 is_void = true;
7263 else
7264 canon_argtypes = tree_cons (NULL_TREE,
7265 TYPE_CANONICAL (TREE_VALUE (arg)),
7266 canon_argtypes);
7267 }
7268
7269 canon_argtypes = nreverse (canon_argtypes);
7270 if (is_void)
7271 canon_argtypes = chainon (canon_argtypes, void_list_node);
7272
7273 /* There is a non-canonical type. */
7274 *any_noncanonical_p = true;
7275 return canon_argtypes;
7276 }
7277
7278 /* The canonical argument types are the same as ARGTYPES. */
7279 return argtypes;
7280 }
7281
7282 /* Construct, lay out and return
7283 the type of functions returning type VALUE_TYPE
7284 given arguments of types ARG_TYPES.
7285 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7286 are data type nodes for the arguments of the function.
7287 If such a type has already been constructed, reuse it. */
7288
7289 tree
7290 build_function_type (tree value_type, tree arg_types)
7291 {
7292 tree t;
7293 inchash::hash hstate;
7294 bool any_structural_p, any_noncanonical_p;
7295 tree canon_argtypes;
7296
7297 gcc_assert (arg_types != error_mark_node);
7298
7299 if (TREE_CODE (value_type) == FUNCTION_TYPE)
7300 {
7301 error ("function return type cannot be function");
7302 value_type = integer_type_node;
7303 }
7304
7305 /* Make a node of the sort we want. */
7306 t = make_node (FUNCTION_TYPE);
7307 TREE_TYPE (t) = value_type;
7308 TYPE_ARG_TYPES (t) = arg_types;
7309
7310 /* If we already have such a type, use the old one. */
7311 hashval_t hash = type_hash_canon_hash (t);
7312 t = type_hash_canon (hash, t);
7313
7314 /* Set up the canonical type. */
7315 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7316 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7317 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
7318 &any_structural_p,
7319 &any_noncanonical_p);
7320 if (any_structural_p)
7321 SET_TYPE_STRUCTURAL_EQUALITY (t);
7322 else if (any_noncanonical_p)
7323 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
7324 canon_argtypes);
7325
7326 if (!COMPLETE_TYPE_P (t))
7327 layout_type (t);
7328 return t;
7329 }
7330
7331 /* Build a function type. The RETURN_TYPE is the type returned by the
7332 function. If VAARGS is set, no void_type_node is appended to the
7333 list. ARGP must be always be terminated be a NULL_TREE. */
7334
7335 static tree
7336 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
7337 {
7338 tree t, args, last;
7339
7340 t = va_arg (argp, tree);
7341 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
7342 args = tree_cons (NULL_TREE, t, args);
7343
7344 if (vaargs)
7345 {
7346 last = args;
7347 if (args != NULL_TREE)
7348 args = nreverse (args);
7349 gcc_assert (last != void_list_node);
7350 }
7351 else if (args == NULL_TREE)
7352 args = void_list_node;
7353 else
7354 {
7355 last = args;
7356 args = nreverse (args);
7357 TREE_CHAIN (last) = void_list_node;
7358 }
7359 args = build_function_type (return_type, args);
7360
7361 return args;
7362 }
7363
7364 /* Build a function type. The RETURN_TYPE is the type returned by the
7365 function. If additional arguments are provided, they are
7366 additional argument types. The list of argument types must always
7367 be terminated by NULL_TREE. */
7368
7369 tree
7370 build_function_type_list (tree return_type, ...)
7371 {
7372 tree args;
7373 va_list p;
7374
7375 va_start (p, return_type);
7376 args = build_function_type_list_1 (false, return_type, p);
7377 va_end (p);
7378 return args;
7379 }
7380
7381 /* Build a variable argument function type. The RETURN_TYPE is the
7382 type returned by the function. If additional arguments are provided,
7383 they are additional argument types. The list of argument types must
7384 always be terminated by NULL_TREE. */
7385
7386 tree
7387 build_varargs_function_type_list (tree return_type, ...)
7388 {
7389 tree args;
7390 va_list p;
7391
7392 va_start (p, return_type);
7393 args = build_function_type_list_1 (true, return_type, p);
7394 va_end (p);
7395
7396 return args;
7397 }
7398
7399 /* Build a function type. RETURN_TYPE is the type returned by the
7400 function; VAARGS indicates whether the function takes varargs. The
7401 function takes N named arguments, the types of which are provided in
7402 ARG_TYPES. */
7403
7404 static tree
7405 build_function_type_array_1 (bool vaargs, tree return_type, int n,
7406 tree *arg_types)
7407 {
7408 int i;
7409 tree t = vaargs ? NULL_TREE : void_list_node;
7410
7411 for (i = n - 1; i >= 0; i--)
7412 t = tree_cons (NULL_TREE, arg_types[i], t);
7413
7414 return build_function_type (return_type, t);
7415 }
7416
7417 /* Build a function type. RETURN_TYPE is the type returned by the
7418 function. The function takes N named arguments, the types of which
7419 are provided in ARG_TYPES. */
7420
7421 tree
7422 build_function_type_array (tree return_type, int n, tree *arg_types)
7423 {
7424 return build_function_type_array_1 (false, return_type, n, arg_types);
7425 }
7426
7427 /* Build a variable argument function type. RETURN_TYPE is the type
7428 returned by the function. The function takes N named arguments, the
7429 types of which are provided in ARG_TYPES. */
7430
7431 tree
7432 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
7433 {
7434 return build_function_type_array_1 (true, return_type, n, arg_types);
7435 }
7436
7437 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
7438 and ARGTYPES (a TREE_LIST) are the return type and arguments types
7439 for the method. An implicit additional parameter (of type
7440 pointer-to-BASETYPE) is added to the ARGTYPES. */
7441
7442 tree
7443 build_method_type_directly (tree basetype,
7444 tree rettype,
7445 tree argtypes)
7446 {
7447 tree t;
7448 tree ptype;
7449 bool any_structural_p, any_noncanonical_p;
7450 tree canon_argtypes;
7451
7452 /* Make a node of the sort we want. */
7453 t = make_node (METHOD_TYPE);
7454
7455 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7456 TREE_TYPE (t) = rettype;
7457 ptype = build_pointer_type (basetype);
7458
7459 /* The actual arglist for this function includes a "hidden" argument
7460 which is "this". Put it into the list of argument types. */
7461 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
7462 TYPE_ARG_TYPES (t) = argtypes;
7463
7464 /* If we already have such a type, use the old one. */
7465 hashval_t hash = type_hash_canon_hash (t);
7466 t = type_hash_canon (hash, t);
7467
7468 /* Set up the canonical type. */
7469 any_structural_p
7470 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7471 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
7472 any_noncanonical_p
7473 = (TYPE_CANONICAL (basetype) != basetype
7474 || TYPE_CANONICAL (rettype) != rettype);
7475 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
7476 &any_structural_p,
7477 &any_noncanonical_p);
7478 if (any_structural_p)
7479 SET_TYPE_STRUCTURAL_EQUALITY (t);
7480 else if (any_noncanonical_p)
7481 TYPE_CANONICAL (t)
7482 = build_method_type_directly (TYPE_CANONICAL (basetype),
7483 TYPE_CANONICAL (rettype),
7484 canon_argtypes);
7485 if (!COMPLETE_TYPE_P (t))
7486 layout_type (t);
7487
7488 return t;
7489 }
7490
7491 /* Construct, lay out and return the type of methods belonging to class
7492 BASETYPE and whose arguments and values are described by TYPE.
7493 If that type exists already, reuse it.
7494 TYPE must be a FUNCTION_TYPE node. */
7495
7496 tree
7497 build_method_type (tree basetype, tree type)
7498 {
7499 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
7500
7501 return build_method_type_directly (basetype,
7502 TREE_TYPE (type),
7503 TYPE_ARG_TYPES (type));
7504 }
7505
7506 /* Construct, lay out and return the type of offsets to a value
7507 of type TYPE, within an object of type BASETYPE.
7508 If a suitable offset type exists already, reuse it. */
7509
7510 tree
7511 build_offset_type (tree basetype, tree type)
7512 {
7513 tree t;
7514
7515 /* Make a node of the sort we want. */
7516 t = make_node (OFFSET_TYPE);
7517
7518 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7519 TREE_TYPE (t) = type;
7520
7521 /* If we already have such a type, use the old one. */
7522 hashval_t hash = type_hash_canon_hash (t);
7523 t = type_hash_canon (hash, t);
7524
7525 if (!COMPLETE_TYPE_P (t))
7526 layout_type (t);
7527
7528 if (TYPE_CANONICAL (t) == t)
7529 {
7530 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7531 || TYPE_STRUCTURAL_EQUALITY_P (type))
7532 SET_TYPE_STRUCTURAL_EQUALITY (t);
7533 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
7534 || TYPE_CANONICAL (type) != type)
7535 TYPE_CANONICAL (t)
7536 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
7537 TYPE_CANONICAL (type));
7538 }
7539
7540 return t;
7541 }
7542
7543 /* Create a complex type whose components are COMPONENT_TYPE.
7544
7545 If NAMED is true, the type is given a TYPE_NAME. We do not always
7546 do so because this creates a DECL node and thus make the DECL_UIDs
7547 dependent on the type canonicalization hashtable, which is GC-ed,
7548 so the DECL_UIDs would not be stable wrt garbage collection. */
7549
7550 tree
7551 build_complex_type (tree component_type, bool named)
7552 {
7553 gcc_assert (INTEGRAL_TYPE_P (component_type)
7554 || SCALAR_FLOAT_TYPE_P (component_type)
7555 || FIXED_POINT_TYPE_P (component_type));
7556
7557 /* Make a node of the sort we want. */
7558 tree probe = make_node (COMPLEX_TYPE);
7559
7560 TREE_TYPE (probe) = TYPE_MAIN_VARIANT (component_type);
7561
7562 /* If we already have such a type, use the old one. */
7563 hashval_t hash = type_hash_canon_hash (probe);
7564 tree t = type_hash_canon (hash, probe);
7565
7566 if (t == probe)
7567 {
7568 /* We created a new type. The hash insertion will have laid
7569 out the type. We need to check the canonicalization and
7570 maybe set the name. */
7571 gcc_checking_assert (COMPLETE_TYPE_P (t)
7572 && !TYPE_NAME (t)
7573 && TYPE_CANONICAL (t) == t);
7574
7575 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (t)))
7576 SET_TYPE_STRUCTURAL_EQUALITY (t);
7577 else if (TYPE_CANONICAL (TREE_TYPE (t)) != TREE_TYPE (t))
7578 TYPE_CANONICAL (t)
7579 = build_complex_type (TYPE_CANONICAL (TREE_TYPE (t)), named);
7580
7581 /* We need to create a name, since complex is a fundamental type. */
7582 if (named)
7583 {
7584 const char *name = NULL;
7585
7586 if (TREE_TYPE (t) == char_type_node)
7587 name = "complex char";
7588 else if (TREE_TYPE (t) == signed_char_type_node)
7589 name = "complex signed char";
7590 else if (TREE_TYPE (t) == unsigned_char_type_node)
7591 name = "complex unsigned char";
7592 else if (TREE_TYPE (t) == short_integer_type_node)
7593 name = "complex short int";
7594 else if (TREE_TYPE (t) == short_unsigned_type_node)
7595 name = "complex short unsigned int";
7596 else if (TREE_TYPE (t) == integer_type_node)
7597 name = "complex int";
7598 else if (TREE_TYPE (t) == unsigned_type_node)
7599 name = "complex unsigned int";
7600 else if (TREE_TYPE (t) == long_integer_type_node)
7601 name = "complex long int";
7602 else if (TREE_TYPE (t) == long_unsigned_type_node)
7603 name = "complex long unsigned int";
7604 else if (TREE_TYPE (t) == long_long_integer_type_node)
7605 name = "complex long long int";
7606 else if (TREE_TYPE (t) == long_long_unsigned_type_node)
7607 name = "complex long long unsigned int";
7608
7609 if (name != NULL)
7610 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
7611 get_identifier (name), t);
7612 }
7613 }
7614
7615 return build_qualified_type (t, TYPE_QUALS (component_type));
7616 }
7617
7618 /* If TYPE is a real or complex floating-point type and the target
7619 does not directly support arithmetic on TYPE then return the wider
7620 type to be used for arithmetic on TYPE. Otherwise, return
7621 NULL_TREE. */
7622
7623 tree
7624 excess_precision_type (tree type)
7625 {
7626 /* The target can give two different responses to the question of
7627 which excess precision mode it would like depending on whether we
7628 are in -fexcess-precision=standard or -fexcess-precision=fast. */
7629
7630 enum excess_precision_type requested_type
7631 = (flag_excess_precision == EXCESS_PRECISION_FAST
7632 ? EXCESS_PRECISION_TYPE_FAST
7633 : EXCESS_PRECISION_TYPE_STANDARD);
7634
7635 enum flt_eval_method target_flt_eval_method
7636 = targetm.c.excess_precision (requested_type);
7637
7638 /* The target should not ask for unpredictable float evaluation (though
7639 it might advertise that implicitly the evaluation is unpredictable,
7640 but we don't care about that here, it will have been reported
7641 elsewhere). If it does ask for unpredictable evaluation, we have
7642 nothing to do here. */
7643 gcc_assert (target_flt_eval_method != FLT_EVAL_METHOD_UNPREDICTABLE);
7644
7645 /* Nothing to do. The target has asked for all types we know about
7646 to be computed with their native precision and range. */
7647 if (target_flt_eval_method == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
7648 return NULL_TREE;
7649
7650 /* The target will promote this type in a target-dependent way, so excess
7651 precision ought to leave it alone. */
7652 if (targetm.promoted_type (type) != NULL_TREE)
7653 return NULL_TREE;
7654
7655 machine_mode float16_type_mode = (float16_type_node
7656 ? TYPE_MODE (float16_type_node)
7657 : VOIDmode);
7658 machine_mode float_type_mode = TYPE_MODE (float_type_node);
7659 machine_mode double_type_mode = TYPE_MODE (double_type_node);
7660
7661 switch (TREE_CODE (type))
7662 {
7663 case REAL_TYPE:
7664 {
7665 machine_mode type_mode = TYPE_MODE (type);
7666 switch (target_flt_eval_method)
7667 {
7668 case FLT_EVAL_METHOD_PROMOTE_TO_FLOAT:
7669 if (type_mode == float16_type_mode)
7670 return float_type_node;
7671 break;
7672 case FLT_EVAL_METHOD_PROMOTE_TO_DOUBLE:
7673 if (type_mode == float16_type_mode
7674 || type_mode == float_type_mode)
7675 return double_type_node;
7676 break;
7677 case FLT_EVAL_METHOD_PROMOTE_TO_LONG_DOUBLE:
7678 if (type_mode == float16_type_mode
7679 || type_mode == float_type_mode
7680 || type_mode == double_type_mode)
7681 return long_double_type_node;
7682 break;
7683 default:
7684 gcc_unreachable ();
7685 }
7686 break;
7687 }
7688 case COMPLEX_TYPE:
7689 {
7690 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
7691 return NULL_TREE;
7692 machine_mode type_mode = TYPE_MODE (TREE_TYPE (type));
7693 switch (target_flt_eval_method)
7694 {
7695 case FLT_EVAL_METHOD_PROMOTE_TO_FLOAT:
7696 if (type_mode == float16_type_mode)
7697 return complex_float_type_node;
7698 break;
7699 case FLT_EVAL_METHOD_PROMOTE_TO_DOUBLE:
7700 if (type_mode == float16_type_mode
7701 || type_mode == float_type_mode)
7702 return complex_double_type_node;
7703 break;
7704 case FLT_EVAL_METHOD_PROMOTE_TO_LONG_DOUBLE:
7705 if (type_mode == float16_type_mode
7706 || type_mode == float_type_mode
7707 || type_mode == double_type_mode)
7708 return complex_long_double_type_node;
7709 break;
7710 default:
7711 gcc_unreachable ();
7712 }
7713 break;
7714 }
7715 default:
7716 break;
7717 }
7718
7719 return NULL_TREE;
7720 }
7721 \f
7722 /* Return OP, stripped of any conversions to wider types as much as is safe.
7723 Converting the value back to OP's type makes a value equivalent to OP.
7724
7725 If FOR_TYPE is nonzero, we return a value which, if converted to
7726 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
7727
7728 OP must have integer, real or enumeral type. Pointers are not allowed!
7729
7730 There are some cases where the obvious value we could return
7731 would regenerate to OP if converted to OP's type,
7732 but would not extend like OP to wider types.
7733 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
7734 For example, if OP is (unsigned short)(signed char)-1,
7735 we avoid returning (signed char)-1 if FOR_TYPE is int,
7736 even though extending that to an unsigned short would regenerate OP,
7737 since the result of extending (signed char)-1 to (int)
7738 is different from (int) OP. */
7739
7740 tree
7741 get_unwidened (tree op, tree for_type)
7742 {
7743 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
7744 tree type = TREE_TYPE (op);
7745 unsigned final_prec
7746 = TYPE_PRECISION (for_type != 0 ? for_type : type);
7747 int uns
7748 = (for_type != 0 && for_type != type
7749 && final_prec > TYPE_PRECISION (type)
7750 && TYPE_UNSIGNED (type));
7751 tree win = op;
7752
7753 while (CONVERT_EXPR_P (op))
7754 {
7755 int bitschange;
7756
7757 /* TYPE_PRECISION on vector types has different meaning
7758 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
7759 so avoid them here. */
7760 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
7761 break;
7762
7763 bitschange = TYPE_PRECISION (TREE_TYPE (op))
7764 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
7765
7766 /* Truncations are many-one so cannot be removed.
7767 Unless we are later going to truncate down even farther. */
7768 if (bitschange < 0
7769 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
7770 break;
7771
7772 /* See what's inside this conversion. If we decide to strip it,
7773 we will set WIN. */
7774 op = TREE_OPERAND (op, 0);
7775
7776 /* If we have not stripped any zero-extensions (uns is 0),
7777 we can strip any kind of extension.
7778 If we have previously stripped a zero-extension,
7779 only zero-extensions can safely be stripped.
7780 Any extension can be stripped if the bits it would produce
7781 are all going to be discarded later by truncating to FOR_TYPE. */
7782
7783 if (bitschange > 0)
7784 {
7785 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
7786 win = op;
7787 /* TYPE_UNSIGNED says whether this is a zero-extension.
7788 Let's avoid computing it if it does not affect WIN
7789 and if UNS will not be needed again. */
7790 if ((uns
7791 || CONVERT_EXPR_P (op))
7792 && TYPE_UNSIGNED (TREE_TYPE (op)))
7793 {
7794 uns = 1;
7795 win = op;
7796 }
7797 }
7798 }
7799
7800 /* If we finally reach a constant see if it fits in sth smaller and
7801 in that case convert it. */
7802 if (TREE_CODE (win) == INTEGER_CST)
7803 {
7804 tree wtype = TREE_TYPE (win);
7805 unsigned prec = wi::min_precision (wi::to_wide (win), TYPE_SIGN (wtype));
7806 if (for_type)
7807 prec = MAX (prec, final_prec);
7808 if (prec < TYPE_PRECISION (wtype))
7809 {
7810 tree t = lang_hooks.types.type_for_size (prec, TYPE_UNSIGNED (wtype));
7811 if (t && TYPE_PRECISION (t) < TYPE_PRECISION (wtype))
7812 win = fold_convert (t, win);
7813 }
7814 }
7815
7816 return win;
7817 }
7818 \f
7819 /* Return OP or a simpler expression for a narrower value
7820 which can be sign-extended or zero-extended to give back OP.
7821 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
7822 or 0 if the value should be sign-extended. */
7823
7824 tree
7825 get_narrower (tree op, int *unsignedp_ptr)
7826 {
7827 int uns = 0;
7828 int first = 1;
7829 tree win = op;
7830 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
7831
7832 if (TREE_CODE (op) == COMPOUND_EXPR)
7833 {
7834 do
7835 op = TREE_OPERAND (op, 1);
7836 while (TREE_CODE (op) == COMPOUND_EXPR);
7837 tree ret = get_narrower (op, unsignedp_ptr);
7838 if (ret == op)
7839 return win;
7840 auto_vec <tree, 16> v;
7841 unsigned int i;
7842 for (op = win; TREE_CODE (op) == COMPOUND_EXPR;
7843 op = TREE_OPERAND (op, 1))
7844 v.safe_push (op);
7845 FOR_EACH_VEC_ELT_REVERSE (v, i, op)
7846 ret = build2_loc (EXPR_LOCATION (op), COMPOUND_EXPR,
7847 TREE_TYPE (ret), TREE_OPERAND (op, 0),
7848 ret);
7849 return ret;
7850 }
7851 while (TREE_CODE (op) == NOP_EXPR)
7852 {
7853 int bitschange
7854 = (TYPE_PRECISION (TREE_TYPE (op))
7855 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
7856
7857 /* Truncations are many-one so cannot be removed. */
7858 if (bitschange < 0)
7859 break;
7860
7861 /* See what's inside this conversion. If we decide to strip it,
7862 we will set WIN. */
7863
7864 if (bitschange > 0)
7865 {
7866 op = TREE_OPERAND (op, 0);
7867 /* An extension: the outermost one can be stripped,
7868 but remember whether it is zero or sign extension. */
7869 if (first)
7870 uns = TYPE_UNSIGNED (TREE_TYPE (op));
7871 /* Otherwise, if a sign extension has been stripped,
7872 only sign extensions can now be stripped;
7873 if a zero extension has been stripped, only zero-extensions. */
7874 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
7875 break;
7876 first = 0;
7877 }
7878 else /* bitschange == 0 */
7879 {
7880 /* A change in nominal type can always be stripped, but we must
7881 preserve the unsignedness. */
7882 if (first)
7883 uns = TYPE_UNSIGNED (TREE_TYPE (op));
7884 first = 0;
7885 op = TREE_OPERAND (op, 0);
7886 /* Keep trying to narrow, but don't assign op to win if it
7887 would turn an integral type into something else. */
7888 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
7889 continue;
7890 }
7891
7892 win = op;
7893 }
7894
7895 if (TREE_CODE (op) == COMPONENT_REF
7896 /* Since type_for_size always gives an integer type. */
7897 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
7898 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
7899 /* Ensure field is laid out already. */
7900 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
7901 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
7902 {
7903 unsigned HOST_WIDE_INT innerprec
7904 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
7905 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
7906 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
7907 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
7908
7909 /* We can get this structure field in a narrower type that fits it,
7910 but the resulting extension to its nominal type (a fullword type)
7911 must satisfy the same conditions as for other extensions.
7912
7913 Do this only for fields that are aligned (not bit-fields),
7914 because when bit-field insns will be used there is no
7915 advantage in doing this. */
7916
7917 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
7918 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
7919 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
7920 && type != 0)
7921 {
7922 if (first)
7923 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
7924 win = fold_convert (type, op);
7925 }
7926 }
7927
7928 *unsignedp_ptr = uns;
7929 return win;
7930 }
7931 \f
7932 /* Return true if integer constant C has a value that is permissible
7933 for TYPE, an integral type. */
7934
7935 bool
7936 int_fits_type_p (const_tree c, const_tree type)
7937 {
7938 tree type_low_bound, type_high_bound;
7939 bool ok_for_low_bound, ok_for_high_bound;
7940 signop sgn_c = TYPE_SIGN (TREE_TYPE (c));
7941
7942 /* Non-standard boolean types can have arbitrary precision but various
7943 transformations assume that they can only take values 0 and +/-1. */
7944 if (TREE_CODE (type) == BOOLEAN_TYPE)
7945 return wi::fits_to_boolean_p (wi::to_wide (c), type);
7946
7947 retry:
7948 type_low_bound = TYPE_MIN_VALUE (type);
7949 type_high_bound = TYPE_MAX_VALUE (type);
7950
7951 /* If at least one bound of the type is a constant integer, we can check
7952 ourselves and maybe make a decision. If no such decision is possible, but
7953 this type is a subtype, try checking against that. Otherwise, use
7954 fits_to_tree_p, which checks against the precision.
7955
7956 Compute the status for each possibly constant bound, and return if we see
7957 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
7958 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
7959 for "constant known to fit". */
7960
7961 /* Check if c >= type_low_bound. */
7962 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
7963 {
7964 if (tree_int_cst_lt (c, type_low_bound))
7965 return false;
7966 ok_for_low_bound = true;
7967 }
7968 else
7969 ok_for_low_bound = false;
7970
7971 /* Check if c <= type_high_bound. */
7972 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
7973 {
7974 if (tree_int_cst_lt (type_high_bound, c))
7975 return false;
7976 ok_for_high_bound = true;
7977 }
7978 else
7979 ok_for_high_bound = false;
7980
7981 /* If the constant fits both bounds, the result is known. */
7982 if (ok_for_low_bound && ok_for_high_bound)
7983 return true;
7984
7985 /* Perform some generic filtering which may allow making a decision
7986 even if the bounds are not constant. First, negative integers
7987 never fit in unsigned types, */
7988 if (TYPE_UNSIGNED (type) && sgn_c == SIGNED && wi::neg_p (wi::to_wide (c)))
7989 return false;
7990
7991 /* Second, narrower types always fit in wider ones. */
7992 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
7993 return true;
7994
7995 /* Third, unsigned integers with top bit set never fit signed types. */
7996 if (!TYPE_UNSIGNED (type) && sgn_c == UNSIGNED)
7997 {
7998 int prec = GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (TREE_TYPE (c))) - 1;
7999 if (prec < TYPE_PRECISION (TREE_TYPE (c)))
8000 {
8001 /* When a tree_cst is converted to a wide-int, the precision
8002 is taken from the type. However, if the precision of the
8003 mode underneath the type is smaller than that, it is
8004 possible that the value will not fit. The test below
8005 fails if any bit is set between the sign bit of the
8006 underlying mode and the top bit of the type. */
8007 if (wi::zext (wi::to_wide (c), prec - 1) != wi::to_wide (c))
8008 return false;
8009 }
8010 else if (wi::neg_p (wi::to_wide (c)))
8011 return false;
8012 }
8013
8014 /* If we haven't been able to decide at this point, there nothing more we
8015 can check ourselves here. Look at the base type if we have one and it
8016 has the same precision. */
8017 if (TREE_CODE (type) == INTEGER_TYPE
8018 && TREE_TYPE (type) != 0
8019 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8020 {
8021 type = TREE_TYPE (type);
8022 goto retry;
8023 }
8024
8025 /* Or to fits_to_tree_p, if nothing else. */
8026 return wi::fits_to_tree_p (wi::to_wide (c), type);
8027 }
8028
8029 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8030 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8031 represented (assuming two's-complement arithmetic) within the bit
8032 precision of the type are returned instead. */
8033
8034 void
8035 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8036 {
8037 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8038 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8039 wi::to_mpz (wi::to_wide (TYPE_MIN_VALUE (type)), min, TYPE_SIGN (type));
8040 else
8041 {
8042 if (TYPE_UNSIGNED (type))
8043 mpz_set_ui (min, 0);
8044 else
8045 {
8046 wide_int mn = wi::min_value (TYPE_PRECISION (type), SIGNED);
8047 wi::to_mpz (mn, min, SIGNED);
8048 }
8049 }
8050
8051 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8052 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8053 wi::to_mpz (wi::to_wide (TYPE_MAX_VALUE (type)), max, TYPE_SIGN (type));
8054 else
8055 {
8056 wide_int mn = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
8057 wi::to_mpz (mn, max, TYPE_SIGN (type));
8058 }
8059 }
8060
8061 /* Return true if VAR is an automatic variable. */
8062
8063 bool
8064 auto_var_p (const_tree var)
8065 {
8066 return ((((VAR_P (var) && ! DECL_EXTERNAL (var))
8067 || TREE_CODE (var) == PARM_DECL)
8068 && ! TREE_STATIC (var))
8069 || TREE_CODE (var) == RESULT_DECL);
8070 }
8071
8072 /* Return true if VAR is an automatic variable defined in function FN. */
8073
8074 bool
8075 auto_var_in_fn_p (const_tree var, const_tree fn)
8076 {
8077 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8078 && (auto_var_p (var)
8079 || TREE_CODE (var) == LABEL_DECL));
8080 }
8081
8082 /* Subprogram of following function. Called by walk_tree.
8083
8084 Return *TP if it is an automatic variable or parameter of the
8085 function passed in as DATA. */
8086
8087 static tree
8088 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8089 {
8090 tree fn = (tree) data;
8091
8092 if (TYPE_P (*tp))
8093 *walk_subtrees = 0;
8094
8095 else if (DECL_P (*tp)
8096 && auto_var_in_fn_p (*tp, fn))
8097 return *tp;
8098
8099 return NULL_TREE;
8100 }
8101
8102 /* Returns true if T is, contains, or refers to a type with variable
8103 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8104 arguments, but not the return type. If FN is nonzero, only return
8105 true if a modifier of the type or position of FN is a variable or
8106 parameter inside FN.
8107
8108 This concept is more general than that of C99 'variably modified types':
8109 in C99, a struct type is never variably modified because a VLA may not
8110 appear as a structure member. However, in GNU C code like:
8111
8112 struct S { int i[f()]; };
8113
8114 is valid, and other languages may define similar constructs. */
8115
8116 bool
8117 variably_modified_type_p (tree type, tree fn)
8118 {
8119 tree t;
8120
8121 /* Test if T is either variable (if FN is zero) or an expression containing
8122 a variable in FN. If TYPE isn't gimplified, return true also if
8123 gimplify_one_sizepos would gimplify the expression into a local
8124 variable. */
8125 #define RETURN_TRUE_IF_VAR(T) \
8126 do { tree _t = (T); \
8127 if (_t != NULL_TREE \
8128 && _t != error_mark_node \
8129 && !CONSTANT_CLASS_P (_t) \
8130 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8131 && (!fn \
8132 || (!TYPE_SIZES_GIMPLIFIED (type) \
8133 && (TREE_CODE (_t) != VAR_DECL \
8134 && !CONTAINS_PLACEHOLDER_P (_t))) \
8135 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8136 return true; } while (0)
8137
8138 if (type == error_mark_node)
8139 return false;
8140
8141 /* If TYPE itself has variable size, it is variably modified. */
8142 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8143 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8144
8145 switch (TREE_CODE (type))
8146 {
8147 case POINTER_TYPE:
8148 case REFERENCE_TYPE:
8149 case VECTOR_TYPE:
8150 /* Ada can have pointer types refering to themselves indirectly. */
8151 if (TREE_VISITED (type))
8152 return false;
8153 TREE_VISITED (type) = true;
8154 if (variably_modified_type_p (TREE_TYPE (type), fn))
8155 {
8156 TREE_VISITED (type) = false;
8157 return true;
8158 }
8159 TREE_VISITED (type) = false;
8160 break;
8161
8162 case FUNCTION_TYPE:
8163 case METHOD_TYPE:
8164 /* If TYPE is a function type, it is variably modified if the
8165 return type is variably modified. */
8166 if (variably_modified_type_p (TREE_TYPE (type), fn))
8167 return true;
8168 break;
8169
8170 case INTEGER_TYPE:
8171 case REAL_TYPE:
8172 case FIXED_POINT_TYPE:
8173 case ENUMERAL_TYPE:
8174 case BOOLEAN_TYPE:
8175 /* Scalar types are variably modified if their end points
8176 aren't constant. */
8177 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8178 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8179 break;
8180
8181 case RECORD_TYPE:
8182 case UNION_TYPE:
8183 case QUAL_UNION_TYPE:
8184 /* We can't see if any of the fields are variably-modified by the
8185 definition we normally use, since that would produce infinite
8186 recursion via pointers. */
8187 /* This is variably modified if some field's type is. */
8188 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8189 if (TREE_CODE (t) == FIELD_DECL)
8190 {
8191 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8192 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8193 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8194
8195 /* If the type is a qualified union, then the DECL_QUALIFIER
8196 of fields can also be an expression containing a variable. */
8197 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8198 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8199
8200 /* If the field is a qualified union, then it's only a container
8201 for what's inside so we look into it. That's necessary in LTO
8202 mode because the sizes of the field tested above have been set
8203 to PLACEHOLDER_EXPRs by free_lang_data. */
8204 if (TREE_CODE (TREE_TYPE (t)) == QUAL_UNION_TYPE
8205 && variably_modified_type_p (TREE_TYPE (t), fn))
8206 return true;
8207 }
8208 break;
8209
8210 case ARRAY_TYPE:
8211 /* Do not call ourselves to avoid infinite recursion. This is
8212 variably modified if the element type is. */
8213 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8214 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8215 break;
8216
8217 default:
8218 break;
8219 }
8220
8221 /* The current language may have other cases to check, but in general,
8222 all other types are not variably modified. */
8223 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8224
8225 #undef RETURN_TRUE_IF_VAR
8226 }
8227
8228 /* Given a DECL or TYPE, return the scope in which it was declared, or
8229 NULL_TREE if there is no containing scope. */
8230
8231 tree
8232 get_containing_scope (const_tree t)
8233 {
8234 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8235 }
8236
8237 /* Returns the ultimate TRANSLATION_UNIT_DECL context of DECL or NULL. */
8238
8239 const_tree
8240 get_ultimate_context (const_tree decl)
8241 {
8242 while (decl && TREE_CODE (decl) != TRANSLATION_UNIT_DECL)
8243 {
8244 if (TREE_CODE (decl) == BLOCK)
8245 decl = BLOCK_SUPERCONTEXT (decl);
8246 else
8247 decl = get_containing_scope (decl);
8248 }
8249 return decl;
8250 }
8251
8252 /* Return the innermost context enclosing DECL that is
8253 a FUNCTION_DECL, or zero if none. */
8254
8255 tree
8256 decl_function_context (const_tree decl)
8257 {
8258 tree context;
8259
8260 if (TREE_CODE (decl) == ERROR_MARK)
8261 return 0;
8262
8263 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8264 where we look up the function at runtime. Such functions always take
8265 a first argument of type 'pointer to real context'.
8266
8267 C++ should really be fixed to use DECL_CONTEXT for the real context,
8268 and use something else for the "virtual context". */
8269 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
8270 context
8271 = TYPE_MAIN_VARIANT
8272 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8273 else
8274 context = DECL_CONTEXT (decl);
8275
8276 while (context && TREE_CODE (context) != FUNCTION_DECL)
8277 {
8278 if (TREE_CODE (context) == BLOCK)
8279 context = BLOCK_SUPERCONTEXT (context);
8280 else
8281 context = get_containing_scope (context);
8282 }
8283
8284 return context;
8285 }
8286
8287 /* Return the innermost context enclosing DECL that is
8288 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8289 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8290
8291 tree
8292 decl_type_context (const_tree decl)
8293 {
8294 tree context = DECL_CONTEXT (decl);
8295
8296 while (context)
8297 switch (TREE_CODE (context))
8298 {
8299 case NAMESPACE_DECL:
8300 case TRANSLATION_UNIT_DECL:
8301 return NULL_TREE;
8302
8303 case RECORD_TYPE:
8304 case UNION_TYPE:
8305 case QUAL_UNION_TYPE:
8306 return context;
8307
8308 case TYPE_DECL:
8309 case FUNCTION_DECL:
8310 context = DECL_CONTEXT (context);
8311 break;
8312
8313 case BLOCK:
8314 context = BLOCK_SUPERCONTEXT (context);
8315 break;
8316
8317 default:
8318 gcc_unreachable ();
8319 }
8320
8321 return NULL_TREE;
8322 }
8323
8324 /* CALL is a CALL_EXPR. Return the declaration for the function
8325 called, or NULL_TREE if the called function cannot be
8326 determined. */
8327
8328 tree
8329 get_callee_fndecl (const_tree call)
8330 {
8331 tree addr;
8332
8333 if (call == error_mark_node)
8334 return error_mark_node;
8335
8336 /* It's invalid to call this function with anything but a
8337 CALL_EXPR. */
8338 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8339
8340 /* The first operand to the CALL is the address of the function
8341 called. */
8342 addr = CALL_EXPR_FN (call);
8343
8344 /* If there is no function, return early. */
8345 if (addr == NULL_TREE)
8346 return NULL_TREE;
8347
8348 STRIP_NOPS (addr);
8349
8350 /* If this is a readonly function pointer, extract its initial value. */
8351 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8352 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8353 && DECL_INITIAL (addr))
8354 addr = DECL_INITIAL (addr);
8355
8356 /* If the address is just `&f' for some function `f', then we know
8357 that `f' is being called. */
8358 if (TREE_CODE (addr) == ADDR_EXPR
8359 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8360 return TREE_OPERAND (addr, 0);
8361
8362 /* We couldn't figure out what was being called. */
8363 return NULL_TREE;
8364 }
8365
8366 /* If CALL_EXPR CALL calls a normal built-in function or an internal function,
8367 return the associated function code, otherwise return CFN_LAST. */
8368
8369 combined_fn
8370 get_call_combined_fn (const_tree call)
8371 {
8372 /* It's invalid to call this function with anything but a CALL_EXPR. */
8373 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8374
8375 if (!CALL_EXPR_FN (call))
8376 return as_combined_fn (CALL_EXPR_IFN (call));
8377
8378 tree fndecl = get_callee_fndecl (call);
8379 if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
8380 return as_combined_fn (DECL_FUNCTION_CODE (fndecl));
8381
8382 return CFN_LAST;
8383 }
8384
8385 /* Comparator of indices based on tree_node_counts. */
8386
8387 static int
8388 tree_nodes_cmp (const void *p1, const void *p2)
8389 {
8390 const unsigned *n1 = (const unsigned *)p1;
8391 const unsigned *n2 = (const unsigned *)p2;
8392
8393 return tree_node_counts[*n1] - tree_node_counts[*n2];
8394 }
8395
8396 /* Comparator of indices based on tree_code_counts. */
8397
8398 static int
8399 tree_codes_cmp (const void *p1, const void *p2)
8400 {
8401 const unsigned *n1 = (const unsigned *)p1;
8402 const unsigned *n2 = (const unsigned *)p2;
8403
8404 return tree_code_counts[*n1] - tree_code_counts[*n2];
8405 }
8406
8407 #define TREE_MEM_USAGE_SPACES 40
8408
8409 /* Print debugging information about tree nodes generated during the compile,
8410 and any language-specific information. */
8411
8412 void
8413 dump_tree_statistics (void)
8414 {
8415 if (GATHER_STATISTICS)
8416 {
8417 uint64_t total_nodes, total_bytes;
8418 fprintf (stderr, "\nKind Nodes Bytes\n");
8419 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8420 total_nodes = total_bytes = 0;
8421
8422 {
8423 auto_vec<unsigned> indices (all_kinds);
8424 for (unsigned i = 0; i < all_kinds; i++)
8425 indices.quick_push (i);
8426 indices.qsort (tree_nodes_cmp);
8427
8428 for (unsigned i = 0; i < (int) all_kinds; i++)
8429 {
8430 unsigned j = indices[i];
8431 fprintf (stderr, "%-20s %6" PRIu64 "%c %9" PRIu64 "%c\n",
8432 tree_node_kind_names[j], SIZE_AMOUNT (tree_node_counts[j]),
8433 SIZE_AMOUNT (tree_node_sizes[j]));
8434 total_nodes += tree_node_counts[j];
8435 total_bytes += tree_node_sizes[j];
8436 }
8437 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8438 fprintf (stderr, "%-20s %6" PRIu64 "%c %9" PRIu64 "%c\n", "Total",
8439 SIZE_AMOUNT (total_nodes), SIZE_AMOUNT (total_bytes));
8440 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8441 }
8442
8443 {
8444 fprintf (stderr, "Code Nodes\n");
8445 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8446
8447 auto_vec<unsigned> indices (MAX_TREE_CODES);
8448 for (unsigned i = 0; i < MAX_TREE_CODES; i++)
8449 indices.quick_push (i);
8450 indices.qsort (tree_codes_cmp);
8451
8452 for (unsigned i = 0; i < MAX_TREE_CODES; i++)
8453 {
8454 unsigned j = indices[i];
8455 fprintf (stderr, "%-32s %6" PRIu64 "%c\n",
8456 get_tree_code_name ((enum tree_code) j),
8457 SIZE_AMOUNT (tree_code_counts[j]));
8458 }
8459 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8460 fprintf (stderr, "\n");
8461 ssanames_print_statistics ();
8462 fprintf (stderr, "\n");
8463 phinodes_print_statistics ();
8464 fprintf (stderr, "\n");
8465 }
8466 }
8467 else
8468 fprintf (stderr, "(No per-node statistics)\n");
8469
8470 print_type_hash_statistics ();
8471 print_debug_expr_statistics ();
8472 print_value_expr_statistics ();
8473 lang_hooks.print_statistics ();
8474 }
8475 \f
8476 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8477
8478 /* Generate a crc32 of the low BYTES bytes of VALUE. */
8479
8480 unsigned
8481 crc32_unsigned_n (unsigned chksum, unsigned value, unsigned bytes)
8482 {
8483 /* This relies on the raw feedback's top 4 bits being zero. */
8484 #define FEEDBACK(X) ((X) * 0x04c11db7)
8485 #define SYNDROME(X) (FEEDBACK ((X) & 1) ^ FEEDBACK ((X) & 2) \
8486 ^ FEEDBACK ((X) & 4) ^ FEEDBACK ((X) & 8))
8487 static const unsigned syndromes[16] =
8488 {
8489 SYNDROME(0x0), SYNDROME(0x1), SYNDROME(0x2), SYNDROME(0x3),
8490 SYNDROME(0x4), SYNDROME(0x5), SYNDROME(0x6), SYNDROME(0x7),
8491 SYNDROME(0x8), SYNDROME(0x9), SYNDROME(0xa), SYNDROME(0xb),
8492 SYNDROME(0xc), SYNDROME(0xd), SYNDROME(0xe), SYNDROME(0xf),
8493 };
8494 #undef FEEDBACK
8495 #undef SYNDROME
8496
8497 value <<= (32 - bytes * 8);
8498 for (unsigned ix = bytes * 2; ix--; value <<= 4)
8499 {
8500 unsigned feedback = syndromes[((value ^ chksum) >> 28) & 0xf];
8501
8502 chksum = (chksum << 4) ^ feedback;
8503 }
8504
8505 return chksum;
8506 }
8507
8508 /* Generate a crc32 of a string. */
8509
8510 unsigned
8511 crc32_string (unsigned chksum, const char *string)
8512 {
8513 do
8514 chksum = crc32_byte (chksum, *string);
8515 while (*string++);
8516 return chksum;
8517 }
8518
8519 /* P is a string that will be used in a symbol. Mask out any characters
8520 that are not valid in that context. */
8521
8522 void
8523 clean_symbol_name (char *p)
8524 {
8525 for (; *p; p++)
8526 if (! (ISALNUM (*p)
8527 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
8528 || *p == '$'
8529 #endif
8530 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
8531 || *p == '.'
8532 #endif
8533 ))
8534 *p = '_';
8535 }
8536
8537 static GTY(()) unsigned anon_cnt = 0; /* Saved for PCH. */
8538
8539 /* Create a unique anonymous identifier. The identifier is still a
8540 valid assembly label. */
8541
8542 tree
8543 make_anon_name ()
8544 {
8545 const char *fmt =
8546 #if !defined (NO_DOT_IN_LABEL)
8547 "."
8548 #elif !defined (NO_DOLLAR_IN_LABEL)
8549 "$"
8550 #else
8551 "_"
8552 #endif
8553 "_anon_%d";
8554
8555 char buf[24];
8556 int len = snprintf (buf, sizeof (buf), fmt, anon_cnt++);
8557 gcc_checking_assert (len < int (sizeof (buf)));
8558
8559 tree id = get_identifier_with_length (buf, len);
8560 IDENTIFIER_ANON_P (id) = true;
8561
8562 return id;
8563 }
8564
8565 /* Generate a name for a special-purpose function.
8566 The generated name may need to be unique across the whole link.
8567 Changes to this function may also require corresponding changes to
8568 xstrdup_mask_random.
8569 TYPE is some string to identify the purpose of this function to the
8570 linker or collect2; it must start with an uppercase letter,
8571 one of:
8572 I - for constructors
8573 D - for destructors
8574 N - for C++ anonymous namespaces
8575 F - for DWARF unwind frame information. */
8576
8577 tree
8578 get_file_function_name (const char *type)
8579 {
8580 char *buf;
8581 const char *p;
8582 char *q;
8583
8584 /* If we already have a name we know to be unique, just use that. */
8585 if (first_global_object_name)
8586 p = q = ASTRDUP (first_global_object_name);
8587 /* If the target is handling the constructors/destructors, they
8588 will be local to this file and the name is only necessary for
8589 debugging purposes.
8590 We also assign sub_I and sub_D sufixes to constructors called from
8591 the global static constructors. These are always local. */
8592 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
8593 || (startswith (type, "sub_")
8594 && (type[4] == 'I' || type[4] == 'D')))
8595 {
8596 const char *file = main_input_filename;
8597 if (! file)
8598 file = LOCATION_FILE (input_location);
8599 /* Just use the file's basename, because the full pathname
8600 might be quite long. */
8601 p = q = ASTRDUP (lbasename (file));
8602 }
8603 else
8604 {
8605 /* Otherwise, the name must be unique across the entire link.
8606 We don't have anything that we know to be unique to this translation
8607 unit, so use what we do have and throw in some randomness. */
8608 unsigned len;
8609 const char *name = weak_global_object_name;
8610 const char *file = main_input_filename;
8611
8612 if (! name)
8613 name = "";
8614 if (! file)
8615 file = LOCATION_FILE (input_location);
8616
8617 len = strlen (file);
8618 q = (char *) alloca (9 + 19 + len + 1);
8619 memcpy (q, file, len + 1);
8620
8621 snprintf (q + len, 9 + 19 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
8622 crc32_string (0, name), get_random_seed (false));
8623
8624 p = q;
8625 }
8626
8627 clean_symbol_name (q);
8628 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
8629 + strlen (type));
8630
8631 /* Set up the name of the file-level functions we may need.
8632 Use a global object (which is already required to be unique over
8633 the program) rather than the file name (which imposes extra
8634 constraints). */
8635 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
8636
8637 return get_identifier (buf);
8638 }
8639 \f
8640 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
8641
8642 /* Complain that the tree code of NODE does not match the expected 0
8643 terminated list of trailing codes. The trailing code list can be
8644 empty, for a more vague error message. FILE, LINE, and FUNCTION
8645 are of the caller. */
8646
8647 void
8648 tree_check_failed (const_tree node, const char *file,
8649 int line, const char *function, ...)
8650 {
8651 va_list args;
8652 const char *buffer;
8653 unsigned length = 0;
8654 enum tree_code code;
8655
8656 va_start (args, function);
8657 while ((code = (enum tree_code) va_arg (args, int)))
8658 length += 4 + strlen (get_tree_code_name (code));
8659 va_end (args);
8660 if (length)
8661 {
8662 char *tmp;
8663 va_start (args, function);
8664 length += strlen ("expected ");
8665 buffer = tmp = (char *) alloca (length);
8666 length = 0;
8667 while ((code = (enum tree_code) va_arg (args, int)))
8668 {
8669 const char *prefix = length ? " or " : "expected ";
8670
8671 strcpy (tmp + length, prefix);
8672 length += strlen (prefix);
8673 strcpy (tmp + length, get_tree_code_name (code));
8674 length += strlen (get_tree_code_name (code));
8675 }
8676 va_end (args);
8677 }
8678 else
8679 buffer = "unexpected node";
8680
8681 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8682 buffer, get_tree_code_name (TREE_CODE (node)),
8683 function, trim_filename (file), line);
8684 }
8685
8686 /* Complain that the tree code of NODE does match the expected 0
8687 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
8688 the caller. */
8689
8690 void
8691 tree_not_check_failed (const_tree node, const char *file,
8692 int line, const char *function, ...)
8693 {
8694 va_list args;
8695 char *buffer;
8696 unsigned length = 0;
8697 enum tree_code code;
8698
8699 va_start (args, function);
8700 while ((code = (enum tree_code) va_arg (args, int)))
8701 length += 4 + strlen (get_tree_code_name (code));
8702 va_end (args);
8703 va_start (args, function);
8704 buffer = (char *) alloca (length);
8705 length = 0;
8706 while ((code = (enum tree_code) va_arg (args, int)))
8707 {
8708 if (length)
8709 {
8710 strcpy (buffer + length, " or ");
8711 length += 4;
8712 }
8713 strcpy (buffer + length, get_tree_code_name (code));
8714 length += strlen (get_tree_code_name (code));
8715 }
8716 va_end (args);
8717
8718 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
8719 buffer, get_tree_code_name (TREE_CODE (node)),
8720 function, trim_filename (file), line);
8721 }
8722
8723 /* Similar to tree_check_failed, except that we check for a class of tree
8724 code, given in CL. */
8725
8726 void
8727 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
8728 const char *file, int line, const char *function)
8729 {
8730 internal_error
8731 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
8732 TREE_CODE_CLASS_STRING (cl),
8733 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
8734 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
8735 }
8736
8737 /* Similar to tree_check_failed, except that instead of specifying a
8738 dozen codes, use the knowledge that they're all sequential. */
8739
8740 void
8741 tree_range_check_failed (const_tree node, const char *file, int line,
8742 const char *function, enum tree_code c1,
8743 enum tree_code c2)
8744 {
8745 char *buffer;
8746 unsigned length = 0;
8747 unsigned int c;
8748
8749 for (c = c1; c <= c2; ++c)
8750 length += 4 + strlen (get_tree_code_name ((enum tree_code) c));
8751
8752 length += strlen ("expected ");
8753 buffer = (char *) alloca (length);
8754 length = 0;
8755
8756 for (c = c1; c <= c2; ++c)
8757 {
8758 const char *prefix = length ? " or " : "expected ";
8759
8760 strcpy (buffer + length, prefix);
8761 length += strlen (prefix);
8762 strcpy (buffer + length, get_tree_code_name ((enum tree_code) c));
8763 length += strlen (get_tree_code_name ((enum tree_code) c));
8764 }
8765
8766 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8767 buffer, get_tree_code_name (TREE_CODE (node)),
8768 function, trim_filename (file), line);
8769 }
8770
8771
8772 /* Similar to tree_check_failed, except that we check that a tree does
8773 not have the specified code, given in CL. */
8774
8775 void
8776 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
8777 const char *file, int line, const char *function)
8778 {
8779 internal_error
8780 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
8781 TREE_CODE_CLASS_STRING (cl),
8782 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
8783 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
8784 }
8785
8786
8787 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
8788
8789 void
8790 omp_clause_check_failed (const_tree node, const char *file, int line,
8791 const char *function, enum omp_clause_code code)
8792 {
8793 internal_error ("tree check: expected %<omp_clause %s%>, have %qs "
8794 "in %s, at %s:%d",
8795 omp_clause_code_name[code],
8796 get_tree_code_name (TREE_CODE (node)),
8797 function, trim_filename (file), line);
8798 }
8799
8800
8801 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
8802
8803 void
8804 omp_clause_range_check_failed (const_tree node, const char *file, int line,
8805 const char *function, enum omp_clause_code c1,
8806 enum omp_clause_code c2)
8807 {
8808 char *buffer;
8809 unsigned length = 0;
8810 unsigned int c;
8811
8812 for (c = c1; c <= c2; ++c)
8813 length += 4 + strlen (omp_clause_code_name[c]);
8814
8815 length += strlen ("expected ");
8816 buffer = (char *) alloca (length);
8817 length = 0;
8818
8819 for (c = c1; c <= c2; ++c)
8820 {
8821 const char *prefix = length ? " or " : "expected ";
8822
8823 strcpy (buffer + length, prefix);
8824 length += strlen (prefix);
8825 strcpy (buffer + length, omp_clause_code_name[c]);
8826 length += strlen (omp_clause_code_name[c]);
8827 }
8828
8829 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8830 buffer, omp_clause_code_name[TREE_CODE (node)],
8831 function, trim_filename (file), line);
8832 }
8833
8834
8835 #undef DEFTREESTRUCT
8836 #define DEFTREESTRUCT(VAL, NAME) NAME,
8837
8838 static const char *ts_enum_names[] = {
8839 #include "treestruct.def"
8840 };
8841 #undef DEFTREESTRUCT
8842
8843 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
8844
8845 /* Similar to tree_class_check_failed, except that we check for
8846 whether CODE contains the tree structure identified by EN. */
8847
8848 void
8849 tree_contains_struct_check_failed (const_tree node,
8850 const enum tree_node_structure_enum en,
8851 const char *file, int line,
8852 const char *function)
8853 {
8854 internal_error
8855 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
8856 TS_ENUM_NAME (en),
8857 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
8858 }
8859
8860
8861 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
8862 (dynamically sized) vector. */
8863
8864 void
8865 tree_int_cst_elt_check_failed (int idx, int len, const char *file, int line,
8866 const char *function)
8867 {
8868 internal_error
8869 ("tree check: accessed elt %d of %<tree_int_cst%> with %d elts in %s, "
8870 "at %s:%d",
8871 idx + 1, len, function, trim_filename (file), line);
8872 }
8873
8874 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
8875 (dynamically sized) vector. */
8876
8877 void
8878 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
8879 const char *function)
8880 {
8881 internal_error
8882 ("tree check: accessed elt %d of %<tree_vec%> with %d elts in %s, at %s:%d",
8883 idx + 1, len, function, trim_filename (file), line);
8884 }
8885
8886 /* Similar to above, except that the check is for the bounds of the operand
8887 vector of an expression node EXP. */
8888
8889 void
8890 tree_operand_check_failed (int idx, const_tree exp, const char *file,
8891 int line, const char *function)
8892 {
8893 enum tree_code code = TREE_CODE (exp);
8894 internal_error
8895 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
8896 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
8897 function, trim_filename (file), line);
8898 }
8899
8900 /* Similar to above, except that the check is for the number of
8901 operands of an OMP_CLAUSE node. */
8902
8903 void
8904 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
8905 int line, const char *function)
8906 {
8907 internal_error
8908 ("tree check: accessed operand %d of %<omp_clause %s%> with %d operands "
8909 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
8910 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
8911 trim_filename (file), line);
8912 }
8913 #endif /* ENABLE_TREE_CHECKING */
8914 \f
8915 /* Create a new vector type node holding NUNITS units of type INNERTYPE,
8916 and mapped to the machine mode MODE. Initialize its fields and build
8917 the information necessary for debugging output. */
8918
8919 static tree
8920 make_vector_type (tree innertype, poly_int64 nunits, machine_mode mode)
8921 {
8922 tree t;
8923 tree mv_innertype = TYPE_MAIN_VARIANT (innertype);
8924
8925 t = make_node (VECTOR_TYPE);
8926 TREE_TYPE (t) = mv_innertype;
8927 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
8928 SET_TYPE_MODE (t, mode);
8929
8930 if (TYPE_STRUCTURAL_EQUALITY_P (mv_innertype) || in_lto_p)
8931 SET_TYPE_STRUCTURAL_EQUALITY (t);
8932 else if ((TYPE_CANONICAL (mv_innertype) != innertype
8933 || mode != VOIDmode)
8934 && !VECTOR_BOOLEAN_TYPE_P (t))
8935 TYPE_CANONICAL (t)
8936 = make_vector_type (TYPE_CANONICAL (mv_innertype), nunits, VOIDmode);
8937
8938 layout_type (t);
8939
8940 hashval_t hash = type_hash_canon_hash (t);
8941 t = type_hash_canon (hash, t);
8942
8943 /* We have built a main variant, based on the main variant of the
8944 inner type. Use it to build the variant we return. */
8945 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
8946 && TREE_TYPE (t) != innertype)
8947 return build_type_attribute_qual_variant (t,
8948 TYPE_ATTRIBUTES (innertype),
8949 TYPE_QUALS (innertype));
8950
8951 return t;
8952 }
8953
8954 static tree
8955 make_or_reuse_type (unsigned size, int unsignedp)
8956 {
8957 int i;
8958
8959 if (size == INT_TYPE_SIZE)
8960 return unsignedp ? unsigned_type_node : integer_type_node;
8961 if (size == CHAR_TYPE_SIZE)
8962 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
8963 if (size == SHORT_TYPE_SIZE)
8964 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
8965 if (size == LONG_TYPE_SIZE)
8966 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
8967 if (size == LONG_LONG_TYPE_SIZE)
8968 return (unsignedp ? long_long_unsigned_type_node
8969 : long_long_integer_type_node);
8970
8971 for (i = 0; i < NUM_INT_N_ENTS; i ++)
8972 if (size == int_n_data[i].bitsize
8973 && int_n_enabled_p[i])
8974 return (unsignedp ? int_n_trees[i].unsigned_type
8975 : int_n_trees[i].signed_type);
8976
8977 if (unsignedp)
8978 return make_unsigned_type (size);
8979 else
8980 return make_signed_type (size);
8981 }
8982
8983 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
8984
8985 static tree
8986 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
8987 {
8988 if (satp)
8989 {
8990 if (size == SHORT_FRACT_TYPE_SIZE)
8991 return unsignedp ? sat_unsigned_short_fract_type_node
8992 : sat_short_fract_type_node;
8993 if (size == FRACT_TYPE_SIZE)
8994 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
8995 if (size == LONG_FRACT_TYPE_SIZE)
8996 return unsignedp ? sat_unsigned_long_fract_type_node
8997 : sat_long_fract_type_node;
8998 if (size == LONG_LONG_FRACT_TYPE_SIZE)
8999 return unsignedp ? sat_unsigned_long_long_fract_type_node
9000 : sat_long_long_fract_type_node;
9001 }
9002 else
9003 {
9004 if (size == SHORT_FRACT_TYPE_SIZE)
9005 return unsignedp ? unsigned_short_fract_type_node
9006 : short_fract_type_node;
9007 if (size == FRACT_TYPE_SIZE)
9008 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9009 if (size == LONG_FRACT_TYPE_SIZE)
9010 return unsignedp ? unsigned_long_fract_type_node
9011 : long_fract_type_node;
9012 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9013 return unsignedp ? unsigned_long_long_fract_type_node
9014 : long_long_fract_type_node;
9015 }
9016
9017 return make_fract_type (size, unsignedp, satp);
9018 }
9019
9020 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9021
9022 static tree
9023 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9024 {
9025 if (satp)
9026 {
9027 if (size == SHORT_ACCUM_TYPE_SIZE)
9028 return unsignedp ? sat_unsigned_short_accum_type_node
9029 : sat_short_accum_type_node;
9030 if (size == ACCUM_TYPE_SIZE)
9031 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9032 if (size == LONG_ACCUM_TYPE_SIZE)
9033 return unsignedp ? sat_unsigned_long_accum_type_node
9034 : sat_long_accum_type_node;
9035 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9036 return unsignedp ? sat_unsigned_long_long_accum_type_node
9037 : sat_long_long_accum_type_node;
9038 }
9039 else
9040 {
9041 if (size == SHORT_ACCUM_TYPE_SIZE)
9042 return unsignedp ? unsigned_short_accum_type_node
9043 : short_accum_type_node;
9044 if (size == ACCUM_TYPE_SIZE)
9045 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9046 if (size == LONG_ACCUM_TYPE_SIZE)
9047 return unsignedp ? unsigned_long_accum_type_node
9048 : long_accum_type_node;
9049 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9050 return unsignedp ? unsigned_long_long_accum_type_node
9051 : long_long_accum_type_node;
9052 }
9053
9054 return make_accum_type (size, unsignedp, satp);
9055 }
9056
9057
9058 /* Create an atomic variant node for TYPE. This routine is called
9059 during initialization of data types to create the 5 basic atomic
9060 types. The generic build_variant_type function requires these to
9061 already be set up in order to function properly, so cannot be
9062 called from there. If ALIGN is non-zero, then ensure alignment is
9063 overridden to this value. */
9064
9065 static tree
9066 build_atomic_base (tree type, unsigned int align)
9067 {
9068 tree t;
9069
9070 /* Make sure its not already registered. */
9071 if ((t = get_qualified_type (type, TYPE_QUAL_ATOMIC)))
9072 return t;
9073
9074 t = build_variant_type_copy (type);
9075 set_type_quals (t, TYPE_QUAL_ATOMIC);
9076
9077 if (align)
9078 SET_TYPE_ALIGN (t, align);
9079
9080 return t;
9081 }
9082
9083 /* Information about the _FloatN and _FloatNx types. This must be in
9084 the same order as the corresponding TI_* enum values. */
9085 const floatn_type_info floatn_nx_types[NUM_FLOATN_NX_TYPES] =
9086 {
9087 { 16, false },
9088 { 32, false },
9089 { 64, false },
9090 { 128, false },
9091 { 32, true },
9092 { 64, true },
9093 { 128, true },
9094 };
9095
9096
9097 /* Create nodes for all integer types (and error_mark_node) using the sizes
9098 of C datatypes. SIGNED_CHAR specifies whether char is signed. */
9099
9100 void
9101 build_common_tree_nodes (bool signed_char)
9102 {
9103 int i;
9104
9105 error_mark_node = make_node (ERROR_MARK);
9106 TREE_TYPE (error_mark_node) = error_mark_node;
9107
9108 initialize_sizetypes ();
9109
9110 /* Define both `signed char' and `unsigned char'. */
9111 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9112 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9113 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9114 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9115
9116 /* Define `char', which is like either `signed char' or `unsigned char'
9117 but not the same as either. */
9118 char_type_node
9119 = (signed_char
9120 ? make_signed_type (CHAR_TYPE_SIZE)
9121 : make_unsigned_type (CHAR_TYPE_SIZE));
9122 TYPE_STRING_FLAG (char_type_node) = 1;
9123
9124 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9125 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9126 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9127 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9128 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9129 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9130 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9131 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9132
9133 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9134 {
9135 int_n_trees[i].signed_type = make_signed_type (int_n_data[i].bitsize);
9136 int_n_trees[i].unsigned_type = make_unsigned_type (int_n_data[i].bitsize);
9137
9138 if (int_n_enabled_p[i])
9139 {
9140 integer_types[itk_intN_0 + i * 2] = int_n_trees[i].signed_type;
9141 integer_types[itk_unsigned_intN_0 + i * 2] = int_n_trees[i].unsigned_type;
9142 }
9143 }
9144
9145 /* Define a boolean type. This type only represents boolean values but
9146 may be larger than char depending on the value of BOOL_TYPE_SIZE. */
9147 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9148 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9149 TYPE_PRECISION (boolean_type_node) = 1;
9150 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9151
9152 /* Define what type to use for size_t. */
9153 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9154 size_type_node = unsigned_type_node;
9155 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9156 size_type_node = long_unsigned_type_node;
9157 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9158 size_type_node = long_long_unsigned_type_node;
9159 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9160 size_type_node = short_unsigned_type_node;
9161 else
9162 {
9163 int i;
9164
9165 size_type_node = NULL_TREE;
9166 for (i = 0; i < NUM_INT_N_ENTS; i++)
9167 if (int_n_enabled_p[i])
9168 {
9169 char name[50], altname[50];
9170 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
9171 sprintf (altname, "__int%d__ unsigned", int_n_data[i].bitsize);
9172
9173 if (strcmp (name, SIZE_TYPE) == 0
9174 || strcmp (altname, SIZE_TYPE) == 0)
9175 {
9176 size_type_node = int_n_trees[i].unsigned_type;
9177 }
9178 }
9179 if (size_type_node == NULL_TREE)
9180 gcc_unreachable ();
9181 }
9182
9183 /* Define what type to use for ptrdiff_t. */
9184 if (strcmp (PTRDIFF_TYPE, "int") == 0)
9185 ptrdiff_type_node = integer_type_node;
9186 else if (strcmp (PTRDIFF_TYPE, "long int") == 0)
9187 ptrdiff_type_node = long_integer_type_node;
9188 else if (strcmp (PTRDIFF_TYPE, "long long int") == 0)
9189 ptrdiff_type_node = long_long_integer_type_node;
9190 else if (strcmp (PTRDIFF_TYPE, "short int") == 0)
9191 ptrdiff_type_node = short_integer_type_node;
9192 else
9193 {
9194 ptrdiff_type_node = NULL_TREE;
9195 for (int i = 0; i < NUM_INT_N_ENTS; i++)
9196 if (int_n_enabled_p[i])
9197 {
9198 char name[50], altname[50];
9199 sprintf (name, "__int%d", int_n_data[i].bitsize);
9200 sprintf (altname, "__int%d__", int_n_data[i].bitsize);
9201
9202 if (strcmp (name, PTRDIFF_TYPE) == 0
9203 || strcmp (altname, PTRDIFF_TYPE) == 0)
9204 ptrdiff_type_node = int_n_trees[i].signed_type;
9205 }
9206 if (ptrdiff_type_node == NULL_TREE)
9207 gcc_unreachable ();
9208 }
9209
9210 /* Fill in the rest of the sized types. Reuse existing type nodes
9211 when possible. */
9212 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9213 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9214 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9215 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9216 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9217
9218 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9219 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9220 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9221 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9222 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9223
9224 /* Don't call build_qualified type for atomics. That routine does
9225 special processing for atomics, and until they are initialized
9226 it's better not to make that call.
9227
9228 Check to see if there is a target override for atomic types. */
9229
9230 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
9231 targetm.atomic_align_for_mode (QImode));
9232 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
9233 targetm.atomic_align_for_mode (HImode));
9234 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
9235 targetm.atomic_align_for_mode (SImode));
9236 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
9237 targetm.atomic_align_for_mode (DImode));
9238 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
9239 targetm.atomic_align_for_mode (TImode));
9240
9241 access_public_node = get_identifier ("public");
9242 access_protected_node = get_identifier ("protected");
9243 access_private_node = get_identifier ("private");
9244
9245 /* Define these next since types below may used them. */
9246 integer_zero_node = build_int_cst (integer_type_node, 0);
9247 integer_one_node = build_int_cst (integer_type_node, 1);
9248 integer_three_node = build_int_cst (integer_type_node, 3);
9249 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9250
9251 size_zero_node = size_int (0);
9252 size_one_node = size_int (1);
9253 bitsize_zero_node = bitsize_int (0);
9254 bitsize_one_node = bitsize_int (1);
9255 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9256
9257 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9258 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9259
9260 void_type_node = make_node (VOID_TYPE);
9261 layout_type (void_type_node);
9262
9263 /* We are not going to have real types in C with less than byte alignment,
9264 so we might as well not have any types that claim to have it. */
9265 SET_TYPE_ALIGN (void_type_node, BITS_PER_UNIT);
9266 TYPE_USER_ALIGN (void_type_node) = 0;
9267
9268 void_node = make_node (VOID_CST);
9269 TREE_TYPE (void_node) = void_type_node;
9270
9271 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9272 layout_type (TREE_TYPE (null_pointer_node));
9273
9274 ptr_type_node = build_pointer_type (void_type_node);
9275 const_ptr_type_node
9276 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9277 for (unsigned i = 0;
9278 i < sizeof (builtin_structptr_types) / sizeof (builtin_structptr_type);
9279 ++i)
9280 builtin_structptr_types[i].node = builtin_structptr_types[i].base;
9281
9282 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
9283
9284 float_type_node = make_node (REAL_TYPE);
9285 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9286 layout_type (float_type_node);
9287
9288 double_type_node = make_node (REAL_TYPE);
9289 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9290 layout_type (double_type_node);
9291
9292 long_double_type_node = make_node (REAL_TYPE);
9293 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9294 layout_type (long_double_type_node);
9295
9296 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
9297 {
9298 int n = floatn_nx_types[i].n;
9299 bool extended = floatn_nx_types[i].extended;
9300 scalar_float_mode mode;
9301 if (!targetm.floatn_mode (n, extended).exists (&mode))
9302 continue;
9303 int precision = GET_MODE_PRECISION (mode);
9304 /* Work around the rs6000 KFmode having precision 113 not
9305 128. */
9306 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
9307 gcc_assert (fmt->b == 2 && fmt->emin + fmt->emax == 3);
9308 int min_precision = fmt->p + ceil_log2 (fmt->emax - fmt->emin);
9309 if (!extended)
9310 gcc_assert (min_precision == n);
9311 if (precision < min_precision)
9312 precision = min_precision;
9313 FLOATN_NX_TYPE_NODE (i) = make_node (REAL_TYPE);
9314 TYPE_PRECISION (FLOATN_NX_TYPE_NODE (i)) = precision;
9315 layout_type (FLOATN_NX_TYPE_NODE (i));
9316 SET_TYPE_MODE (FLOATN_NX_TYPE_NODE (i), mode);
9317 }
9318
9319 float_ptr_type_node = build_pointer_type (float_type_node);
9320 double_ptr_type_node = build_pointer_type (double_type_node);
9321 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9322 integer_ptr_type_node = build_pointer_type (integer_type_node);
9323
9324 /* Fixed size integer types. */
9325 uint16_type_node = make_or_reuse_type (16, 1);
9326 uint32_type_node = make_or_reuse_type (32, 1);
9327 uint64_type_node = make_or_reuse_type (64, 1);
9328 if (targetm.scalar_mode_supported_p (TImode))
9329 uint128_type_node = make_or_reuse_type (128, 1);
9330
9331 /* Decimal float types. */
9332 if (targetm.decimal_float_supported_p ())
9333 {
9334 dfloat32_type_node = make_node (REAL_TYPE);
9335 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9336 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9337 layout_type (dfloat32_type_node);
9338
9339 dfloat64_type_node = make_node (REAL_TYPE);
9340 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9341 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9342 layout_type (dfloat64_type_node);
9343
9344 dfloat128_type_node = make_node (REAL_TYPE);
9345 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9346 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9347 layout_type (dfloat128_type_node);
9348 }
9349
9350 complex_integer_type_node = build_complex_type (integer_type_node, true);
9351 complex_float_type_node = build_complex_type (float_type_node, true);
9352 complex_double_type_node = build_complex_type (double_type_node, true);
9353 complex_long_double_type_node = build_complex_type (long_double_type_node,
9354 true);
9355
9356 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
9357 {
9358 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
9359 COMPLEX_FLOATN_NX_TYPE_NODE (i)
9360 = build_complex_type (FLOATN_NX_TYPE_NODE (i));
9361 }
9362
9363 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9364 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9365 sat_ ## KIND ## _type_node = \
9366 make_sat_signed_ ## KIND ## _type (SIZE); \
9367 sat_unsigned_ ## KIND ## _type_node = \
9368 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9369 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9370 unsigned_ ## KIND ## _type_node = \
9371 make_unsigned_ ## KIND ## _type (SIZE);
9372
9373 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9374 sat_ ## WIDTH ## KIND ## _type_node = \
9375 make_sat_signed_ ## KIND ## _type (SIZE); \
9376 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9377 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9378 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9379 unsigned_ ## WIDTH ## KIND ## _type_node = \
9380 make_unsigned_ ## KIND ## _type (SIZE);
9381
9382 /* Make fixed-point type nodes based on four different widths. */
9383 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9384 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9385 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9386 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9387 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9388
9389 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9390 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9391 NAME ## _type_node = \
9392 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9393 u ## NAME ## _type_node = \
9394 make_or_reuse_unsigned_ ## KIND ## _type \
9395 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9396 sat_ ## NAME ## _type_node = \
9397 make_or_reuse_sat_signed_ ## KIND ## _type \
9398 (GET_MODE_BITSIZE (MODE ## mode)); \
9399 sat_u ## NAME ## _type_node = \
9400 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9401 (GET_MODE_BITSIZE (U ## MODE ## mode));
9402
9403 /* Fixed-point type and mode nodes. */
9404 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9405 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9406 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9407 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9408 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9409 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9410 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9411 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9412 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9413 MAKE_FIXED_MODE_NODE (accum, da, DA)
9414 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9415
9416 {
9417 tree t = targetm.build_builtin_va_list ();
9418
9419 /* Many back-ends define record types without setting TYPE_NAME.
9420 If we copied the record type here, we'd keep the original
9421 record type without a name. This breaks name mangling. So,
9422 don't copy record types and let c_common_nodes_and_builtins()
9423 declare the type to be __builtin_va_list. */
9424 if (TREE_CODE (t) != RECORD_TYPE)
9425 t = build_variant_type_copy (t);
9426
9427 va_list_type_node = t;
9428 }
9429
9430 /* SCEV analyzer global shared trees. */
9431 chrec_dont_know = make_node (SCEV_NOT_KNOWN);
9432 TREE_TYPE (chrec_dont_know) = void_type_node;
9433 chrec_known = make_node (SCEV_KNOWN);
9434 TREE_TYPE (chrec_known) = void_type_node;
9435 }
9436
9437 /* Modify DECL for given flags.
9438 TM_PURE attribute is set only on types, so the function will modify
9439 DECL's type when ECF_TM_PURE is used. */
9440
9441 void
9442 set_call_expr_flags (tree decl, int flags)
9443 {
9444 if (flags & ECF_NOTHROW)
9445 TREE_NOTHROW (decl) = 1;
9446 if (flags & ECF_CONST)
9447 TREE_READONLY (decl) = 1;
9448 if (flags & ECF_PURE)
9449 DECL_PURE_P (decl) = 1;
9450 if (flags & ECF_LOOPING_CONST_OR_PURE)
9451 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9452 if (flags & ECF_NOVOPS)
9453 DECL_IS_NOVOPS (decl) = 1;
9454 if (flags & ECF_NORETURN)
9455 TREE_THIS_VOLATILE (decl) = 1;
9456 if (flags & ECF_MALLOC)
9457 DECL_IS_MALLOC (decl) = 1;
9458 if (flags & ECF_RETURNS_TWICE)
9459 DECL_IS_RETURNS_TWICE (decl) = 1;
9460 if (flags & ECF_LEAF)
9461 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9462 NULL, DECL_ATTRIBUTES (decl));
9463 if (flags & ECF_COLD)
9464 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("cold"),
9465 NULL, DECL_ATTRIBUTES (decl));
9466 if (flags & ECF_RET1)
9467 DECL_ATTRIBUTES (decl)
9468 = tree_cons (get_identifier ("fn spec"),
9469 build_tree_list (NULL_TREE, build_string (2, "1 ")),
9470 DECL_ATTRIBUTES (decl));
9471 if ((flags & ECF_TM_PURE) && flag_tm)
9472 apply_tm_attr (decl, get_identifier ("transaction_pure"));
9473 /* Looping const or pure is implied by noreturn.
9474 There is currently no way to declare looping const or looping pure alone. */
9475 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9476 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9477 }
9478
9479
9480 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9481
9482 static void
9483 local_define_builtin (const char *name, tree type, enum built_in_function code,
9484 const char *library_name, int ecf_flags)
9485 {
9486 tree decl;
9487
9488 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9489 library_name, NULL_TREE);
9490 set_call_expr_flags (decl, ecf_flags);
9491
9492 set_builtin_decl (code, decl, true);
9493 }
9494
9495 /* Call this function after instantiating all builtins that the language
9496 front end cares about. This will build the rest of the builtins
9497 and internal functions that are relied upon by the tree optimizers and
9498 the middle-end. */
9499
9500 void
9501 build_common_builtin_nodes (void)
9502 {
9503 tree tmp, ftype;
9504 int ecf_flags;
9505
9506 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE)
9507 || !builtin_decl_explicit_p (BUILT_IN_ABORT))
9508 {
9509 ftype = build_function_type (void_type_node, void_list_node);
9510 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9511 local_define_builtin ("__builtin_unreachable", ftype,
9512 BUILT_IN_UNREACHABLE,
9513 "__builtin_unreachable",
9514 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9515 | ECF_CONST | ECF_COLD);
9516 if (!builtin_decl_explicit_p (BUILT_IN_ABORT))
9517 local_define_builtin ("__builtin_abort", ftype, BUILT_IN_ABORT,
9518 "abort",
9519 ECF_LEAF | ECF_NORETURN | ECF_CONST | ECF_COLD);
9520 }
9521
9522 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9523 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9524 {
9525 ftype = build_function_type_list (ptr_type_node,
9526 ptr_type_node, const_ptr_type_node,
9527 size_type_node, NULL_TREE);
9528
9529 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
9530 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9531 "memcpy", ECF_NOTHROW | ECF_LEAF);
9532 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9533 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9534 "memmove", ECF_NOTHROW | ECF_LEAF);
9535 }
9536
9537 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
9538 {
9539 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9540 const_ptr_type_node, size_type_node,
9541 NULL_TREE);
9542 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9543 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9544 }
9545
9546 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
9547 {
9548 ftype = build_function_type_list (ptr_type_node,
9549 ptr_type_node, integer_type_node,
9550 size_type_node, NULL_TREE);
9551 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9552 "memset", ECF_NOTHROW | ECF_LEAF);
9553 }
9554
9555 /* If we're checking the stack, `alloca' can throw. */
9556 const int alloca_flags
9557 = ECF_MALLOC | ECF_LEAF | (flag_stack_check ? 0 : ECF_NOTHROW);
9558
9559 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
9560 {
9561 ftype = build_function_type_list (ptr_type_node,
9562 size_type_node, NULL_TREE);
9563 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9564 "alloca", alloca_flags);
9565 }
9566
9567 ftype = build_function_type_list (ptr_type_node, size_type_node,
9568 size_type_node, NULL_TREE);
9569 local_define_builtin ("__builtin_alloca_with_align", ftype,
9570 BUILT_IN_ALLOCA_WITH_ALIGN,
9571 "__builtin_alloca_with_align",
9572 alloca_flags);
9573
9574 ftype = build_function_type_list (ptr_type_node, size_type_node,
9575 size_type_node, size_type_node, NULL_TREE);
9576 local_define_builtin ("__builtin_alloca_with_align_and_max", ftype,
9577 BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX,
9578 "__builtin_alloca_with_align_and_max",
9579 alloca_flags);
9580
9581 ftype = build_function_type_list (void_type_node,
9582 ptr_type_node, ptr_type_node,
9583 ptr_type_node, NULL_TREE);
9584 local_define_builtin ("__builtin_init_trampoline", ftype,
9585 BUILT_IN_INIT_TRAMPOLINE,
9586 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9587 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
9588 BUILT_IN_INIT_HEAP_TRAMPOLINE,
9589 "__builtin_init_heap_trampoline",
9590 ECF_NOTHROW | ECF_LEAF);
9591 local_define_builtin ("__builtin_init_descriptor", ftype,
9592 BUILT_IN_INIT_DESCRIPTOR,
9593 "__builtin_init_descriptor", ECF_NOTHROW | ECF_LEAF);
9594
9595 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9596 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9597 BUILT_IN_ADJUST_TRAMPOLINE,
9598 "__builtin_adjust_trampoline",
9599 ECF_CONST | ECF_NOTHROW);
9600 local_define_builtin ("__builtin_adjust_descriptor", ftype,
9601 BUILT_IN_ADJUST_DESCRIPTOR,
9602 "__builtin_adjust_descriptor",
9603 ECF_CONST | ECF_NOTHROW);
9604
9605 ftype = build_function_type_list (void_type_node,
9606 ptr_type_node, ptr_type_node, NULL_TREE);
9607 if (!builtin_decl_explicit_p (BUILT_IN_CLEAR_CACHE))
9608 local_define_builtin ("__builtin___clear_cache", ftype,
9609 BUILT_IN_CLEAR_CACHE,
9610 "__clear_cache",
9611 ECF_NOTHROW);
9612
9613 local_define_builtin ("__builtin_nonlocal_goto", ftype,
9614 BUILT_IN_NONLOCAL_GOTO,
9615 "__builtin_nonlocal_goto",
9616 ECF_NORETURN | ECF_NOTHROW);
9617
9618 ftype = build_function_type_list (void_type_node,
9619 ptr_type_node, ptr_type_node, NULL_TREE);
9620 local_define_builtin ("__builtin_setjmp_setup", ftype,
9621 BUILT_IN_SETJMP_SETUP,
9622 "__builtin_setjmp_setup", ECF_NOTHROW);
9623
9624 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9625 local_define_builtin ("__builtin_setjmp_receiver", ftype,
9626 BUILT_IN_SETJMP_RECEIVER,
9627 "__builtin_setjmp_receiver", ECF_NOTHROW | ECF_LEAF);
9628
9629 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9630 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
9631 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9632
9633 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9634 local_define_builtin ("__builtin_stack_restore", ftype,
9635 BUILT_IN_STACK_RESTORE,
9636 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9637
9638 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9639 const_ptr_type_node, size_type_node,
9640 NULL_TREE);
9641 local_define_builtin ("__builtin_memcmp_eq", ftype, BUILT_IN_MEMCMP_EQ,
9642 "__builtin_memcmp_eq",
9643 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9644
9645 local_define_builtin ("__builtin_strncmp_eq", ftype, BUILT_IN_STRNCMP_EQ,
9646 "__builtin_strncmp_eq",
9647 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9648
9649 local_define_builtin ("__builtin_strcmp_eq", ftype, BUILT_IN_STRCMP_EQ,
9650 "__builtin_strcmp_eq",
9651 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9652
9653 /* If there's a possibility that we might use the ARM EABI, build the
9654 alternate __cxa_end_cleanup node used to resume from C++. */
9655 if (targetm.arm_eabi_unwinder)
9656 {
9657 ftype = build_function_type_list (void_type_node, NULL_TREE);
9658 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
9659 BUILT_IN_CXA_END_CLEANUP,
9660 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
9661 }
9662
9663 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9664 local_define_builtin ("__builtin_unwind_resume", ftype,
9665 BUILT_IN_UNWIND_RESUME,
9666 ((targetm_common.except_unwind_info (&global_options)
9667 == UI_SJLJ)
9668 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
9669 ECF_NORETURN);
9670
9671 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
9672 {
9673 ftype = build_function_type_list (ptr_type_node, integer_type_node,
9674 NULL_TREE);
9675 local_define_builtin ("__builtin_return_address", ftype,
9676 BUILT_IN_RETURN_ADDRESS,
9677 "__builtin_return_address",
9678 ECF_NOTHROW);
9679 }
9680
9681 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
9682 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9683 {
9684 ftype = build_function_type_list (void_type_node, ptr_type_node,
9685 ptr_type_node, NULL_TREE);
9686 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
9687 local_define_builtin ("__cyg_profile_func_enter", ftype,
9688 BUILT_IN_PROFILE_FUNC_ENTER,
9689 "__cyg_profile_func_enter", 0);
9690 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9691 local_define_builtin ("__cyg_profile_func_exit", ftype,
9692 BUILT_IN_PROFILE_FUNC_EXIT,
9693 "__cyg_profile_func_exit", 0);
9694 }
9695
9696 /* The exception object and filter values from the runtime. The argument
9697 must be zero before exception lowering, i.e. from the front end. After
9698 exception lowering, it will be the region number for the exception
9699 landing pad. These functions are PURE instead of CONST to prevent
9700 them from being hoisted past the exception edge that will initialize
9701 its value in the landing pad. */
9702 ftype = build_function_type_list (ptr_type_node,
9703 integer_type_node, NULL_TREE);
9704 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
9705 /* Only use TM_PURE if we have TM language support. */
9706 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
9707 ecf_flags |= ECF_TM_PURE;
9708 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
9709 "__builtin_eh_pointer", ecf_flags);
9710
9711 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
9712 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
9713 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
9714 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9715
9716 ftype = build_function_type_list (void_type_node,
9717 integer_type_node, integer_type_node,
9718 NULL_TREE);
9719 local_define_builtin ("__builtin_eh_copy_values", ftype,
9720 BUILT_IN_EH_COPY_VALUES,
9721 "__builtin_eh_copy_values", ECF_NOTHROW);
9722
9723 /* Complex multiplication and division. These are handled as builtins
9724 rather than optabs because emit_library_call_value doesn't support
9725 complex. Further, we can do slightly better with folding these
9726 beasties if the real and complex parts of the arguments are separate. */
9727 {
9728 int mode;
9729
9730 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
9731 {
9732 char mode_name_buf[4], *q;
9733 const char *p;
9734 enum built_in_function mcode, dcode;
9735 tree type, inner_type;
9736 const char *prefix = "__";
9737
9738 if (targetm.libfunc_gnu_prefix)
9739 prefix = "__gnu_";
9740
9741 type = lang_hooks.types.type_for_mode ((machine_mode) mode, 0);
9742 if (type == NULL)
9743 continue;
9744 inner_type = TREE_TYPE (type);
9745
9746 ftype = build_function_type_list (type, inner_type, inner_type,
9747 inner_type, inner_type, NULL_TREE);
9748
9749 mcode = ((enum built_in_function)
9750 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9751 dcode = ((enum built_in_function)
9752 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9753
9754 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
9755 *q = TOLOWER (*p);
9756 *q = '\0';
9757
9758 /* For -ftrapping-math these should throw from a former
9759 -fnon-call-exception stmt. */
9760 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
9761 NULL);
9762 local_define_builtin (built_in_names[mcode], ftype, mcode,
9763 built_in_names[mcode],
9764 ECF_CONST | ECF_LEAF);
9765
9766 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
9767 NULL);
9768 local_define_builtin (built_in_names[dcode], ftype, dcode,
9769 built_in_names[dcode],
9770 ECF_CONST | ECF_LEAF);
9771 }
9772 }
9773
9774 init_internal_fns ();
9775 }
9776
9777 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
9778 better way.
9779
9780 If we requested a pointer to a vector, build up the pointers that
9781 we stripped off while looking for the inner type. Similarly for
9782 return values from functions.
9783
9784 The argument TYPE is the top of the chain, and BOTTOM is the
9785 new type which we will point to. */
9786
9787 tree
9788 reconstruct_complex_type (tree type, tree bottom)
9789 {
9790 tree inner, outer;
9791
9792 if (TREE_CODE (type) == POINTER_TYPE)
9793 {
9794 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9795 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
9796 TYPE_REF_CAN_ALIAS_ALL (type));
9797 }
9798 else if (TREE_CODE (type) == REFERENCE_TYPE)
9799 {
9800 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9801 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
9802 TYPE_REF_CAN_ALIAS_ALL (type));
9803 }
9804 else if (TREE_CODE (type) == ARRAY_TYPE)
9805 {
9806 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9807 outer = build_array_type (inner, TYPE_DOMAIN (type));
9808 }
9809 else if (TREE_CODE (type) == FUNCTION_TYPE)
9810 {
9811 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9812 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
9813 }
9814 else if (TREE_CODE (type) == METHOD_TYPE)
9815 {
9816 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9817 /* The build_method_type_directly() routine prepends 'this' to argument list,
9818 so we must compensate by getting rid of it. */
9819 outer
9820 = build_method_type_directly
9821 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
9822 inner,
9823 TREE_CHAIN (TYPE_ARG_TYPES (type)));
9824 }
9825 else if (TREE_CODE (type) == OFFSET_TYPE)
9826 {
9827 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9828 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
9829 }
9830 else
9831 return bottom;
9832
9833 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
9834 TYPE_QUALS (type));
9835 }
9836
9837 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
9838 the inner type. */
9839 tree
9840 build_vector_type_for_mode (tree innertype, machine_mode mode)
9841 {
9842 poly_int64 nunits;
9843 unsigned int bitsize;
9844
9845 switch (GET_MODE_CLASS (mode))
9846 {
9847 case MODE_VECTOR_BOOL:
9848 case MODE_VECTOR_INT:
9849 case MODE_VECTOR_FLOAT:
9850 case MODE_VECTOR_FRACT:
9851 case MODE_VECTOR_UFRACT:
9852 case MODE_VECTOR_ACCUM:
9853 case MODE_VECTOR_UACCUM:
9854 nunits = GET_MODE_NUNITS (mode);
9855 break;
9856
9857 case MODE_INT:
9858 /* Check that there are no leftover bits. */
9859 bitsize = GET_MODE_BITSIZE (as_a <scalar_int_mode> (mode));
9860 gcc_assert (bitsize % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
9861 nunits = bitsize / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
9862 break;
9863
9864 default:
9865 gcc_unreachable ();
9866 }
9867
9868 return make_vector_type (innertype, nunits, mode);
9869 }
9870
9871 /* Similarly, but takes the inner type and number of units, which must be
9872 a power of two. */
9873
9874 tree
9875 build_vector_type (tree innertype, poly_int64 nunits)
9876 {
9877 return make_vector_type (innertype, nunits, VOIDmode);
9878 }
9879
9880 /* Build a truth vector with NUNITS units, giving it mode MASK_MODE. */
9881
9882 tree
9883 build_truth_vector_type_for_mode (poly_uint64 nunits, machine_mode mask_mode)
9884 {
9885 gcc_assert (mask_mode != BLKmode);
9886
9887 unsigned HOST_WIDE_INT esize;
9888 if (VECTOR_MODE_P (mask_mode))
9889 {
9890 poly_uint64 vsize = GET_MODE_BITSIZE (mask_mode);
9891 esize = vector_element_size (vsize, nunits);
9892 }
9893 else
9894 esize = 1;
9895
9896 tree bool_type = build_nonstandard_boolean_type (esize);
9897
9898 return make_vector_type (bool_type, nunits, mask_mode);
9899 }
9900
9901 /* Build a vector type that holds one boolean result for each element of
9902 vector type VECTYPE. The public interface for this operation is
9903 truth_type_for. */
9904
9905 static tree
9906 build_truth_vector_type_for (tree vectype)
9907 {
9908 machine_mode vector_mode = TYPE_MODE (vectype);
9909 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
9910
9911 machine_mode mask_mode;
9912 if (VECTOR_MODE_P (vector_mode)
9913 && targetm.vectorize.get_mask_mode (vector_mode).exists (&mask_mode))
9914 return build_truth_vector_type_for_mode (nunits, mask_mode);
9915
9916 poly_uint64 vsize = tree_to_poly_uint64 (TYPE_SIZE (vectype));
9917 unsigned HOST_WIDE_INT esize = vector_element_size (vsize, nunits);
9918 tree bool_type = build_nonstandard_boolean_type (esize);
9919
9920 return make_vector_type (bool_type, nunits, VOIDmode);
9921 }
9922
9923 /* Like build_vector_type, but builds a variant type with TYPE_VECTOR_OPAQUE
9924 set. */
9925
9926 tree
9927 build_opaque_vector_type (tree innertype, poly_int64 nunits)
9928 {
9929 tree t = make_vector_type (innertype, nunits, VOIDmode);
9930 tree cand;
9931 /* We always build the non-opaque variant before the opaque one,
9932 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
9933 cand = TYPE_NEXT_VARIANT (t);
9934 if (cand
9935 && TYPE_VECTOR_OPAQUE (cand)
9936 && check_qualified_type (cand, t, TYPE_QUALS (t)))
9937 return cand;
9938 /* Othewise build a variant type and make sure to queue it after
9939 the non-opaque type. */
9940 cand = build_distinct_type_copy (t);
9941 TYPE_VECTOR_OPAQUE (cand) = true;
9942 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
9943 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
9944 TYPE_NEXT_VARIANT (t) = cand;
9945 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
9946 return cand;
9947 }
9948
9949 /* Return the value of element I of VECTOR_CST T as a wide_int. */
9950
9951 static poly_wide_int
9952 vector_cst_int_elt (const_tree t, unsigned int i)
9953 {
9954 /* First handle elements that are directly encoded. */
9955 unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
9956 if (i < encoded_nelts)
9957 return wi::to_poly_wide (VECTOR_CST_ENCODED_ELT (t, i));
9958
9959 /* Identify the pattern that contains element I and work out the index of
9960 the last encoded element for that pattern. */
9961 unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
9962 unsigned int pattern = i % npatterns;
9963 unsigned int count = i / npatterns;
9964 unsigned int final_i = encoded_nelts - npatterns + pattern;
9965
9966 /* If there are no steps, the final encoded value is the right one. */
9967 if (!VECTOR_CST_STEPPED_P (t))
9968 return wi::to_poly_wide (VECTOR_CST_ENCODED_ELT (t, final_i));
9969
9970 /* Otherwise work out the value from the last two encoded elements. */
9971 tree v1 = VECTOR_CST_ENCODED_ELT (t, final_i - npatterns);
9972 tree v2 = VECTOR_CST_ENCODED_ELT (t, final_i);
9973 poly_wide_int diff = wi::to_poly_wide (v2) - wi::to_poly_wide (v1);
9974 return wi::to_poly_wide (v2) + (count - 2) * diff;
9975 }
9976
9977 /* Return the value of element I of VECTOR_CST T. */
9978
9979 tree
9980 vector_cst_elt (const_tree t, unsigned int i)
9981 {
9982 /* First handle elements that are directly encoded. */
9983 unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
9984 if (i < encoded_nelts)
9985 return VECTOR_CST_ENCODED_ELT (t, i);
9986
9987 /* If there are no steps, the final encoded value is the right one. */
9988 if (!VECTOR_CST_STEPPED_P (t))
9989 {
9990 /* Identify the pattern that contains element I and work out the index of
9991 the last encoded element for that pattern. */
9992 unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
9993 unsigned int pattern = i % npatterns;
9994 unsigned int final_i = encoded_nelts - npatterns + pattern;
9995 return VECTOR_CST_ENCODED_ELT (t, final_i);
9996 }
9997
9998 /* Otherwise work out the value from the last two encoded elements. */
9999 return wide_int_to_tree (TREE_TYPE (TREE_TYPE (t)),
10000 vector_cst_int_elt (t, i));
10001 }
10002
10003 /* Given an initializer INIT, return TRUE if INIT is zero or some
10004 aggregate of zeros. Otherwise return FALSE. If NONZERO is not
10005 null, set *NONZERO if and only if INIT is known not to be all
10006 zeros. The combination of return value of false and *NONZERO
10007 false implies that INIT may but need not be all zeros. Other
10008 combinations indicate definitive answers. */
10009
10010 bool
10011 initializer_zerop (const_tree init, bool *nonzero /* = NULL */)
10012 {
10013 bool dummy;
10014 if (!nonzero)
10015 nonzero = &dummy;
10016
10017 /* Conservatively clear NONZERO and set it only if INIT is definitely
10018 not all zero. */
10019 *nonzero = false;
10020
10021 STRIP_NOPS (init);
10022
10023 unsigned HOST_WIDE_INT off = 0;
10024
10025 switch (TREE_CODE (init))
10026 {
10027 case INTEGER_CST:
10028 if (integer_zerop (init))
10029 return true;
10030
10031 *nonzero = true;
10032 return false;
10033
10034 case REAL_CST:
10035 /* ??? Note that this is not correct for C4X float formats. There,
10036 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10037 negative exponent. */
10038 if (real_zerop (init)
10039 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init)))
10040 return true;
10041
10042 *nonzero = true;
10043 return false;
10044
10045 case FIXED_CST:
10046 if (fixed_zerop (init))
10047 return true;
10048
10049 *nonzero = true;
10050 return false;
10051
10052 case COMPLEX_CST:
10053 if (integer_zerop (init)
10054 || (real_zerop (init)
10055 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10056 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init)))))
10057 return true;
10058
10059 *nonzero = true;
10060 return false;
10061
10062 case VECTOR_CST:
10063 if (VECTOR_CST_NPATTERNS (init) == 1
10064 && VECTOR_CST_DUPLICATE_P (init)
10065 && initializer_zerop (VECTOR_CST_ENCODED_ELT (init, 0)))
10066 return true;
10067
10068 *nonzero = true;
10069 return false;
10070
10071 case CONSTRUCTOR:
10072 {
10073 if (TREE_CLOBBER_P (init))
10074 return false;
10075
10076 unsigned HOST_WIDE_INT idx;
10077 tree elt;
10078
10079 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10080 if (!initializer_zerop (elt, nonzero))
10081 return false;
10082
10083 return true;
10084 }
10085
10086 case MEM_REF:
10087 {
10088 tree arg = TREE_OPERAND (init, 0);
10089 if (TREE_CODE (arg) != ADDR_EXPR)
10090 return false;
10091 tree offset = TREE_OPERAND (init, 1);
10092 if (TREE_CODE (offset) != INTEGER_CST
10093 || !tree_fits_uhwi_p (offset))
10094 return false;
10095 off = tree_to_uhwi (offset);
10096 if (INT_MAX < off)
10097 return false;
10098 arg = TREE_OPERAND (arg, 0);
10099 if (TREE_CODE (arg) != STRING_CST)
10100 return false;
10101 init = arg;
10102 }
10103 /* Fall through. */
10104
10105 case STRING_CST:
10106 {
10107 gcc_assert (off <= INT_MAX);
10108
10109 int i = off;
10110 int n = TREE_STRING_LENGTH (init);
10111 if (n <= i)
10112 return false;
10113
10114 /* We need to loop through all elements to handle cases like
10115 "\0" and "\0foobar". */
10116 for (i = 0; i < n; ++i)
10117 if (TREE_STRING_POINTER (init)[i] != '\0')
10118 {
10119 *nonzero = true;
10120 return false;
10121 }
10122
10123 return true;
10124 }
10125
10126 default:
10127 return false;
10128 }
10129 }
10130
10131 /* Return true if EXPR is an initializer expression in which every element
10132 is a constant that is numerically equal to 0 or 1. The elements do not
10133 need to be equal to each other. */
10134
10135 bool
10136 initializer_each_zero_or_onep (const_tree expr)
10137 {
10138 STRIP_ANY_LOCATION_WRAPPER (expr);
10139
10140 switch (TREE_CODE (expr))
10141 {
10142 case INTEGER_CST:
10143 return integer_zerop (expr) || integer_onep (expr);
10144
10145 case REAL_CST:
10146 return real_zerop (expr) || real_onep (expr);
10147
10148 case VECTOR_CST:
10149 {
10150 unsigned HOST_WIDE_INT nelts = vector_cst_encoded_nelts (expr);
10151 if (VECTOR_CST_STEPPED_P (expr)
10152 && !TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr)).is_constant (&nelts))
10153 return false;
10154
10155 for (unsigned int i = 0; i < nelts; ++i)
10156 {
10157 tree elt = vector_cst_elt (expr, i);
10158 if (!initializer_each_zero_or_onep (elt))
10159 return false;
10160 }
10161
10162 return true;
10163 }
10164
10165 default:
10166 return false;
10167 }
10168 }
10169
10170 /* Check if vector VEC consists of all the equal elements and
10171 that the number of elements corresponds to the type of VEC.
10172 The function returns first element of the vector
10173 or NULL_TREE if the vector is not uniform. */
10174 tree
10175 uniform_vector_p (const_tree vec)
10176 {
10177 tree first, t;
10178 unsigned HOST_WIDE_INT i, nelts;
10179
10180 if (vec == NULL_TREE)
10181 return NULL_TREE;
10182
10183 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10184
10185 if (TREE_CODE (vec) == VEC_DUPLICATE_EXPR)
10186 return TREE_OPERAND (vec, 0);
10187
10188 else if (TREE_CODE (vec) == VECTOR_CST)
10189 {
10190 if (VECTOR_CST_NPATTERNS (vec) == 1 && VECTOR_CST_DUPLICATE_P (vec))
10191 return VECTOR_CST_ENCODED_ELT (vec, 0);
10192 return NULL_TREE;
10193 }
10194
10195 else if (TREE_CODE (vec) == CONSTRUCTOR
10196 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)).is_constant (&nelts))
10197 {
10198 first = error_mark_node;
10199
10200 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10201 {
10202 if (i == 0)
10203 {
10204 first = t;
10205 continue;
10206 }
10207 if (!operand_equal_p (first, t, 0))
10208 return NULL_TREE;
10209 }
10210 if (i != nelts)
10211 return NULL_TREE;
10212
10213 return first;
10214 }
10215
10216 return NULL_TREE;
10217 }
10218
10219 /* If the argument is INTEGER_CST, return it. If the argument is vector
10220 with all elements the same INTEGER_CST, return that INTEGER_CST. Otherwise
10221 return NULL_TREE.
10222 Look through location wrappers. */
10223
10224 tree
10225 uniform_integer_cst_p (tree t)
10226 {
10227 STRIP_ANY_LOCATION_WRAPPER (t);
10228
10229 if (TREE_CODE (t) == INTEGER_CST)
10230 return t;
10231
10232 if (VECTOR_TYPE_P (TREE_TYPE (t)))
10233 {
10234 t = uniform_vector_p (t);
10235 if (t && TREE_CODE (t) == INTEGER_CST)
10236 return t;
10237 }
10238
10239 return NULL_TREE;
10240 }
10241
10242 /* If VECTOR_CST T has a single nonzero element, return the index of that
10243 element, otherwise return -1. */
10244
10245 int
10246 single_nonzero_element (const_tree t)
10247 {
10248 unsigned HOST_WIDE_INT nelts;
10249 unsigned int repeat_nelts;
10250 if (VECTOR_CST_NELTS (t).is_constant (&nelts))
10251 repeat_nelts = nelts;
10252 else if (VECTOR_CST_NELTS_PER_PATTERN (t) == 2)
10253 {
10254 nelts = vector_cst_encoded_nelts (t);
10255 repeat_nelts = VECTOR_CST_NPATTERNS (t);
10256 }
10257 else
10258 return -1;
10259
10260 int res = -1;
10261 for (unsigned int i = 0; i < nelts; ++i)
10262 {
10263 tree elt = vector_cst_elt (t, i);
10264 if (!integer_zerop (elt) && !real_zerop (elt))
10265 {
10266 if (res >= 0 || i >= repeat_nelts)
10267 return -1;
10268 res = i;
10269 }
10270 }
10271 return res;
10272 }
10273
10274 /* Build an empty statement at location LOC. */
10275
10276 tree
10277 build_empty_stmt (location_t loc)
10278 {
10279 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10280 SET_EXPR_LOCATION (t, loc);
10281 return t;
10282 }
10283
10284
10285 /* Build an OpenMP clause with code CODE. LOC is the location of the
10286 clause. */
10287
10288 tree
10289 build_omp_clause (location_t loc, enum omp_clause_code code)
10290 {
10291 tree t;
10292 int size, length;
10293
10294 length = omp_clause_num_ops[code];
10295 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10296
10297 record_node_allocation_statistics (OMP_CLAUSE, size);
10298
10299 t = (tree) ggc_internal_alloc (size);
10300 memset (t, 0, size);
10301 TREE_SET_CODE (t, OMP_CLAUSE);
10302 OMP_CLAUSE_SET_CODE (t, code);
10303 OMP_CLAUSE_LOCATION (t) = loc;
10304
10305 return t;
10306 }
10307
10308 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10309 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10310 Except for the CODE and operand count field, other storage for the
10311 object is initialized to zeros. */
10312
10313 tree
10314 build_vl_exp (enum tree_code code, int len MEM_STAT_DECL)
10315 {
10316 tree t;
10317 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10318
10319 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10320 gcc_assert (len >= 1);
10321
10322 record_node_allocation_statistics (code, length);
10323
10324 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10325
10326 TREE_SET_CODE (t, code);
10327
10328 /* Can't use TREE_OPERAND to store the length because if checking is
10329 enabled, it will try to check the length before we store it. :-P */
10330 t->exp.operands[0] = build_int_cst (sizetype, len);
10331
10332 return t;
10333 }
10334
10335 /* Helper function for build_call_* functions; build a CALL_EXPR with
10336 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10337 the argument slots. */
10338
10339 static tree
10340 build_call_1 (tree return_type, tree fn, int nargs)
10341 {
10342 tree t;
10343
10344 t = build_vl_exp (CALL_EXPR, nargs + 3);
10345 TREE_TYPE (t) = return_type;
10346 CALL_EXPR_FN (t) = fn;
10347 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10348
10349 return t;
10350 }
10351
10352 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10353 FN and a null static chain slot. NARGS is the number of call arguments
10354 which are specified as "..." arguments. */
10355
10356 tree
10357 build_call_nary (tree return_type, tree fn, int nargs, ...)
10358 {
10359 tree ret;
10360 va_list args;
10361 va_start (args, nargs);
10362 ret = build_call_valist (return_type, fn, nargs, args);
10363 va_end (args);
10364 return ret;
10365 }
10366
10367 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10368 FN and a null static chain slot. NARGS is the number of call arguments
10369 which are specified as a va_list ARGS. */
10370
10371 tree
10372 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10373 {
10374 tree t;
10375 int i;
10376
10377 t = build_call_1 (return_type, fn, nargs);
10378 for (i = 0; i < nargs; i++)
10379 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10380 process_call_operands (t);
10381 return t;
10382 }
10383
10384 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10385 FN and a null static chain slot. NARGS is the number of call arguments
10386 which are specified as a tree array ARGS. */
10387
10388 tree
10389 build_call_array_loc (location_t loc, tree return_type, tree fn,
10390 int nargs, const tree *args)
10391 {
10392 tree t;
10393 int i;
10394
10395 t = build_call_1 (return_type, fn, nargs);
10396 for (i = 0; i < nargs; i++)
10397 CALL_EXPR_ARG (t, i) = args[i];
10398 process_call_operands (t);
10399 SET_EXPR_LOCATION (t, loc);
10400 return t;
10401 }
10402
10403 /* Like build_call_array, but takes a vec. */
10404
10405 tree
10406 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10407 {
10408 tree ret, t;
10409 unsigned int ix;
10410
10411 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10412 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10413 CALL_EXPR_ARG (ret, ix) = t;
10414 process_call_operands (ret);
10415 return ret;
10416 }
10417
10418 /* Conveniently construct a function call expression. FNDECL names the
10419 function to be called and N arguments are passed in the array
10420 ARGARRAY. */
10421
10422 tree
10423 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
10424 {
10425 tree fntype = TREE_TYPE (fndecl);
10426 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10427
10428 return fold_build_call_array_loc (loc, TREE_TYPE (fntype), fn, n, argarray);
10429 }
10430
10431 /* Conveniently construct a function call expression. FNDECL names the
10432 function to be called and the arguments are passed in the vector
10433 VEC. */
10434
10435 tree
10436 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
10437 {
10438 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
10439 vec_safe_address (vec));
10440 }
10441
10442
10443 /* Conveniently construct a function call expression. FNDECL names the
10444 function to be called, N is the number of arguments, and the "..."
10445 parameters are the argument expressions. */
10446
10447 tree
10448 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
10449 {
10450 va_list ap;
10451 tree *argarray = XALLOCAVEC (tree, n);
10452 int i;
10453
10454 va_start (ap, n);
10455 for (i = 0; i < n; i++)
10456 argarray[i] = va_arg (ap, tree);
10457 va_end (ap);
10458 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10459 }
10460
10461 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
10462 varargs macros aren't supported by all bootstrap compilers. */
10463
10464 tree
10465 build_call_expr (tree fndecl, int n, ...)
10466 {
10467 va_list ap;
10468 tree *argarray = XALLOCAVEC (tree, n);
10469 int i;
10470
10471 va_start (ap, n);
10472 for (i = 0; i < n; i++)
10473 argarray[i] = va_arg (ap, tree);
10474 va_end (ap);
10475 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
10476 }
10477
10478 /* Build an internal call to IFN, with arguments ARGS[0:N-1] and with return
10479 type TYPE. This is just like CALL_EXPR, except its CALL_EXPR_FN is NULL.
10480 It will get gimplified later into an ordinary internal function. */
10481
10482 tree
10483 build_call_expr_internal_loc_array (location_t loc, internal_fn ifn,
10484 tree type, int n, const tree *args)
10485 {
10486 tree t = build_call_1 (type, NULL_TREE, n);
10487 for (int i = 0; i < n; ++i)
10488 CALL_EXPR_ARG (t, i) = args[i];
10489 SET_EXPR_LOCATION (t, loc);
10490 CALL_EXPR_IFN (t) = ifn;
10491 process_call_operands (t);
10492 return t;
10493 }
10494
10495 /* Build internal call expression. This is just like CALL_EXPR, except
10496 its CALL_EXPR_FN is NULL. It will get gimplified later into ordinary
10497 internal function. */
10498
10499 tree
10500 build_call_expr_internal_loc (location_t loc, enum internal_fn ifn,
10501 tree type, int n, ...)
10502 {
10503 va_list ap;
10504 tree *argarray = XALLOCAVEC (tree, n);
10505 int i;
10506
10507 va_start (ap, n);
10508 for (i = 0; i < n; i++)
10509 argarray[i] = va_arg (ap, tree);
10510 va_end (ap);
10511 return build_call_expr_internal_loc_array (loc, ifn, type, n, argarray);
10512 }
10513
10514 /* Return a function call to FN, if the target is guaranteed to support it,
10515 or null otherwise.
10516
10517 N is the number of arguments, passed in the "...", and TYPE is the
10518 type of the return value. */
10519
10520 tree
10521 maybe_build_call_expr_loc (location_t loc, combined_fn fn, tree type,
10522 int n, ...)
10523 {
10524 va_list ap;
10525 tree *argarray = XALLOCAVEC (tree, n);
10526 int i;
10527
10528 va_start (ap, n);
10529 for (i = 0; i < n; i++)
10530 argarray[i] = va_arg (ap, tree);
10531 va_end (ap);
10532 if (internal_fn_p (fn))
10533 {
10534 internal_fn ifn = as_internal_fn (fn);
10535 if (direct_internal_fn_p (ifn))
10536 {
10537 tree_pair types = direct_internal_fn_types (ifn, type, argarray);
10538 if (!direct_internal_fn_supported_p (ifn, types,
10539 OPTIMIZE_FOR_BOTH))
10540 return NULL_TREE;
10541 }
10542 return build_call_expr_internal_loc_array (loc, ifn, type, n, argarray);
10543 }
10544 else
10545 {
10546 tree fndecl = builtin_decl_implicit (as_builtin_fn (fn));
10547 if (!fndecl)
10548 return NULL_TREE;
10549 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10550 }
10551 }
10552
10553 /* Return a function call to the appropriate builtin alloca variant.
10554
10555 SIZE is the size to be allocated. ALIGN, if non-zero, is the requested
10556 alignment of the allocated area. MAX_SIZE, if non-negative, is an upper
10557 bound for SIZE in case it is not a fixed value. */
10558
10559 tree
10560 build_alloca_call_expr (tree size, unsigned int align, HOST_WIDE_INT max_size)
10561 {
10562 if (max_size >= 0)
10563 {
10564 tree t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX);
10565 return
10566 build_call_expr (t, 3, size, size_int (align), size_int (max_size));
10567 }
10568 else if (align > 0)
10569 {
10570 tree t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
10571 return build_call_expr (t, 2, size, size_int (align));
10572 }
10573 else
10574 {
10575 tree t = builtin_decl_explicit (BUILT_IN_ALLOCA);
10576 return build_call_expr (t, 1, size);
10577 }
10578 }
10579
10580 /* Create a new constant string literal of type ELTYPE[SIZE] (or LEN
10581 if SIZE == -1) and return a tree node representing char* pointer to
10582 it as an ADDR_EXPR (ARRAY_REF (ELTYPE, ...)). When STR is nonnull
10583 the STRING_CST value is the LEN bytes at STR (the representation
10584 of the string, which may be wide). Otherwise it's all zeros. */
10585
10586 tree
10587 build_string_literal (unsigned len, const char *str /* = NULL */,
10588 tree eltype /* = char_type_node */,
10589 unsigned HOST_WIDE_INT size /* = -1 */)
10590 {
10591 tree t = build_string (len, str);
10592 /* Set the maximum valid index based on the string length or SIZE. */
10593 unsigned HOST_WIDE_INT maxidx
10594 = (size == HOST_WIDE_INT_M1U ? len : size) - 1;
10595
10596 tree index = build_index_type (size_int (maxidx));
10597 eltype = build_type_variant (eltype, 1, 0);
10598 tree type = build_array_type (eltype, index);
10599 TREE_TYPE (t) = type;
10600 TREE_CONSTANT (t) = 1;
10601 TREE_READONLY (t) = 1;
10602 TREE_STATIC (t) = 1;
10603
10604 type = build_pointer_type (eltype);
10605 t = build1 (ADDR_EXPR, type,
10606 build4 (ARRAY_REF, eltype,
10607 t, integer_zero_node, NULL_TREE, NULL_TREE));
10608 return t;
10609 }
10610
10611
10612
10613 /* Return true if T (assumed to be a DECL) must be assigned a memory
10614 location. */
10615
10616 bool
10617 needs_to_live_in_memory (const_tree t)
10618 {
10619 return (TREE_ADDRESSABLE (t)
10620 || is_global_var (t)
10621 || (TREE_CODE (t) == RESULT_DECL
10622 && !DECL_BY_REFERENCE (t)
10623 && aggregate_value_p (t, current_function_decl)));
10624 }
10625
10626 /* Return value of a constant X and sign-extend it. */
10627
10628 HOST_WIDE_INT
10629 int_cst_value (const_tree x)
10630 {
10631 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10632 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10633
10634 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10635 gcc_assert (cst_and_fits_in_hwi (x));
10636
10637 if (bits < HOST_BITS_PER_WIDE_INT)
10638 {
10639 bool negative = ((val >> (bits - 1)) & 1) != 0;
10640 if (negative)
10641 val |= HOST_WIDE_INT_M1U << (bits - 1) << 1;
10642 else
10643 val &= ~(HOST_WIDE_INT_M1U << (bits - 1) << 1);
10644 }
10645
10646 return val;
10647 }
10648
10649 /* If TYPE is an integral or pointer type, return an integer type with
10650 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10651 if TYPE is already an integer type of signedness UNSIGNEDP.
10652 If TYPE is a floating-point type, return an integer type with the same
10653 bitsize and with the signedness given by UNSIGNEDP; this is useful
10654 when doing bit-level operations on a floating-point value. */
10655
10656 tree
10657 signed_or_unsigned_type_for (int unsignedp, tree type)
10658 {
10659 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) == unsignedp)
10660 return type;
10661
10662 if (TREE_CODE (type) == VECTOR_TYPE)
10663 {
10664 tree inner = TREE_TYPE (type);
10665 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10666 if (!inner2)
10667 return NULL_TREE;
10668 if (inner == inner2)
10669 return type;
10670 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10671 }
10672
10673 if (TREE_CODE (type) == COMPLEX_TYPE)
10674 {
10675 tree inner = TREE_TYPE (type);
10676 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10677 if (!inner2)
10678 return NULL_TREE;
10679 if (inner == inner2)
10680 return type;
10681 return build_complex_type (inner2);
10682 }
10683
10684 unsigned int bits;
10685 if (INTEGRAL_TYPE_P (type)
10686 || POINTER_TYPE_P (type)
10687 || TREE_CODE (type) == OFFSET_TYPE)
10688 bits = TYPE_PRECISION (type);
10689 else if (TREE_CODE (type) == REAL_TYPE)
10690 bits = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (type));
10691 else
10692 return NULL_TREE;
10693
10694 return build_nonstandard_integer_type (bits, unsignedp);
10695 }
10696
10697 /* If TYPE is an integral or pointer type, return an integer type with
10698 the same precision which is unsigned, or itself if TYPE is already an
10699 unsigned integer type. If TYPE is a floating-point type, return an
10700 unsigned integer type with the same bitsize as TYPE. */
10701
10702 tree
10703 unsigned_type_for (tree type)
10704 {
10705 return signed_or_unsigned_type_for (1, type);
10706 }
10707
10708 /* If TYPE is an integral or pointer type, return an integer type with
10709 the same precision which is signed, or itself if TYPE is already a
10710 signed integer type. If TYPE is a floating-point type, return a
10711 signed integer type with the same bitsize as TYPE. */
10712
10713 tree
10714 signed_type_for (tree type)
10715 {
10716 return signed_or_unsigned_type_for (0, type);
10717 }
10718
10719 /* If TYPE is a vector type, return a signed integer vector type with the
10720 same width and number of subparts. Otherwise return boolean_type_node. */
10721
10722 tree
10723 truth_type_for (tree type)
10724 {
10725 if (TREE_CODE (type) == VECTOR_TYPE)
10726 {
10727 if (VECTOR_BOOLEAN_TYPE_P (type))
10728 return type;
10729 return build_truth_vector_type_for (type);
10730 }
10731 else
10732 return boolean_type_node;
10733 }
10734
10735 /* Returns the largest value obtainable by casting something in INNER type to
10736 OUTER type. */
10737
10738 tree
10739 upper_bound_in_type (tree outer, tree inner)
10740 {
10741 unsigned int det = 0;
10742 unsigned oprec = TYPE_PRECISION (outer);
10743 unsigned iprec = TYPE_PRECISION (inner);
10744 unsigned prec;
10745
10746 /* Compute a unique number for every combination. */
10747 det |= (oprec > iprec) ? 4 : 0;
10748 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10749 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10750
10751 /* Determine the exponent to use. */
10752 switch (det)
10753 {
10754 case 0:
10755 case 1:
10756 /* oprec <= iprec, outer: signed, inner: don't care. */
10757 prec = oprec - 1;
10758 break;
10759 case 2:
10760 case 3:
10761 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10762 prec = oprec;
10763 break;
10764 case 4:
10765 /* oprec > iprec, outer: signed, inner: signed. */
10766 prec = iprec - 1;
10767 break;
10768 case 5:
10769 /* oprec > iprec, outer: signed, inner: unsigned. */
10770 prec = iprec;
10771 break;
10772 case 6:
10773 /* oprec > iprec, outer: unsigned, inner: signed. */
10774 prec = oprec;
10775 break;
10776 case 7:
10777 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10778 prec = iprec;
10779 break;
10780 default:
10781 gcc_unreachable ();
10782 }
10783
10784 return wide_int_to_tree (outer,
10785 wi::mask (prec, false, TYPE_PRECISION (outer)));
10786 }
10787
10788 /* Returns the smallest value obtainable by casting something in INNER type to
10789 OUTER type. */
10790
10791 tree
10792 lower_bound_in_type (tree outer, tree inner)
10793 {
10794 unsigned oprec = TYPE_PRECISION (outer);
10795 unsigned iprec = TYPE_PRECISION (inner);
10796
10797 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10798 and obtain 0. */
10799 if (TYPE_UNSIGNED (outer)
10800 /* If we are widening something of an unsigned type, OUTER type
10801 contains all values of INNER type. In particular, both INNER
10802 and OUTER types have zero in common. */
10803 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10804 return build_int_cst (outer, 0);
10805 else
10806 {
10807 /* If we are widening a signed type to another signed type, we
10808 want to obtain -2^^(iprec-1). If we are keeping the
10809 precision or narrowing to a signed type, we want to obtain
10810 -2^(oprec-1). */
10811 unsigned prec = oprec > iprec ? iprec : oprec;
10812 return wide_int_to_tree (outer,
10813 wi::mask (prec - 1, true,
10814 TYPE_PRECISION (outer)));
10815 }
10816 }
10817
10818 /* Return nonzero if two operands that are suitable for PHI nodes are
10819 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10820 SSA_NAME or invariant. Note that this is strictly an optimization.
10821 That is, callers of this function can directly call operand_equal_p
10822 and get the same result, only slower. */
10823
10824 int
10825 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10826 {
10827 if (arg0 == arg1)
10828 return 1;
10829 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10830 return 0;
10831 return operand_equal_p (arg0, arg1, 0);
10832 }
10833
10834 /* Returns number of zeros at the end of binary representation of X. */
10835
10836 tree
10837 num_ending_zeros (const_tree x)
10838 {
10839 return build_int_cst (TREE_TYPE (x), wi::ctz (wi::to_wide (x)));
10840 }
10841
10842
10843 #define WALK_SUBTREE(NODE) \
10844 do \
10845 { \
10846 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10847 if (result) \
10848 return result; \
10849 } \
10850 while (0)
10851
10852 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10853 be walked whenever a type is seen in the tree. Rest of operands and return
10854 value are as for walk_tree. */
10855
10856 static tree
10857 walk_type_fields (tree type, walk_tree_fn func, void *data,
10858 hash_set<tree> *pset, walk_tree_lh lh)
10859 {
10860 tree result = NULL_TREE;
10861
10862 switch (TREE_CODE (type))
10863 {
10864 case POINTER_TYPE:
10865 case REFERENCE_TYPE:
10866 case VECTOR_TYPE:
10867 /* We have to worry about mutually recursive pointers. These can't
10868 be written in C. They can in Ada. It's pathological, but
10869 there's an ACATS test (c38102a) that checks it. Deal with this
10870 by checking if we're pointing to another pointer, that one
10871 points to another pointer, that one does too, and we have no htab.
10872 If so, get a hash table. We check three levels deep to avoid
10873 the cost of the hash table if we don't need one. */
10874 if (POINTER_TYPE_P (TREE_TYPE (type))
10875 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10876 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10877 && !pset)
10878 {
10879 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10880 func, data);
10881 if (result)
10882 return result;
10883
10884 break;
10885 }
10886
10887 /* fall through */
10888
10889 case COMPLEX_TYPE:
10890 WALK_SUBTREE (TREE_TYPE (type));
10891 break;
10892
10893 case METHOD_TYPE:
10894 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10895
10896 /* Fall through. */
10897
10898 case FUNCTION_TYPE:
10899 WALK_SUBTREE (TREE_TYPE (type));
10900 {
10901 tree arg;
10902
10903 /* We never want to walk into default arguments. */
10904 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10905 WALK_SUBTREE (TREE_VALUE (arg));
10906 }
10907 break;
10908
10909 case ARRAY_TYPE:
10910 /* Don't follow this nodes's type if a pointer for fear that
10911 we'll have infinite recursion. If we have a PSET, then we
10912 need not fear. */
10913 if (pset
10914 || (!POINTER_TYPE_P (TREE_TYPE (type))
10915 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10916 WALK_SUBTREE (TREE_TYPE (type));
10917 WALK_SUBTREE (TYPE_DOMAIN (type));
10918 break;
10919
10920 case OFFSET_TYPE:
10921 WALK_SUBTREE (TREE_TYPE (type));
10922 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
10923 break;
10924
10925 default:
10926 break;
10927 }
10928
10929 return NULL_TREE;
10930 }
10931
10932 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
10933 called with the DATA and the address of each sub-tree. If FUNC returns a
10934 non-NULL value, the traversal is stopped, and the value returned by FUNC
10935 is returned. If PSET is non-NULL it is used to record the nodes visited,
10936 and to avoid visiting a node more than once. */
10937
10938 tree
10939 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
10940 hash_set<tree> *pset, walk_tree_lh lh)
10941 {
10942 enum tree_code code;
10943 int walk_subtrees;
10944 tree result;
10945
10946 #define WALK_SUBTREE_TAIL(NODE) \
10947 do \
10948 { \
10949 tp = & (NODE); \
10950 goto tail_recurse; \
10951 } \
10952 while (0)
10953
10954 tail_recurse:
10955 /* Skip empty subtrees. */
10956 if (!*tp)
10957 return NULL_TREE;
10958
10959 /* Don't walk the same tree twice, if the user has requested
10960 that we avoid doing so. */
10961 if (pset && pset->add (*tp))
10962 return NULL_TREE;
10963
10964 /* Call the function. */
10965 walk_subtrees = 1;
10966 result = (*func) (tp, &walk_subtrees, data);
10967
10968 /* If we found something, return it. */
10969 if (result)
10970 return result;
10971
10972 code = TREE_CODE (*tp);
10973
10974 /* Even if we didn't, FUNC may have decided that there was nothing
10975 interesting below this point in the tree. */
10976 if (!walk_subtrees)
10977 {
10978 /* But we still need to check our siblings. */
10979 if (code == TREE_LIST)
10980 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10981 else if (code == OMP_CLAUSE)
10982 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10983 else
10984 return NULL_TREE;
10985 }
10986
10987 if (lh)
10988 {
10989 result = (*lh) (tp, &walk_subtrees, func, data, pset);
10990 if (result || !walk_subtrees)
10991 return result;
10992 }
10993
10994 switch (code)
10995 {
10996 case ERROR_MARK:
10997 case IDENTIFIER_NODE:
10998 case INTEGER_CST:
10999 case REAL_CST:
11000 case FIXED_CST:
11001 case STRING_CST:
11002 case BLOCK:
11003 case PLACEHOLDER_EXPR:
11004 case SSA_NAME:
11005 case FIELD_DECL:
11006 case RESULT_DECL:
11007 /* None of these have subtrees other than those already walked
11008 above. */
11009 break;
11010
11011 case TREE_LIST:
11012 WALK_SUBTREE (TREE_VALUE (*tp));
11013 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11014 break;
11015
11016 case TREE_VEC:
11017 {
11018 int len = TREE_VEC_LENGTH (*tp);
11019
11020 if (len == 0)
11021 break;
11022
11023 /* Walk all elements but the first. */
11024 while (--len)
11025 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
11026
11027 /* Now walk the first one as a tail call. */
11028 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
11029 }
11030
11031 case VECTOR_CST:
11032 {
11033 unsigned len = vector_cst_encoded_nelts (*tp);
11034 if (len == 0)
11035 break;
11036 /* Walk all elements but the first. */
11037 while (--len)
11038 WALK_SUBTREE (VECTOR_CST_ENCODED_ELT (*tp, len));
11039 /* Now walk the first one as a tail call. */
11040 WALK_SUBTREE_TAIL (VECTOR_CST_ENCODED_ELT (*tp, 0));
11041 }
11042
11043 case COMPLEX_CST:
11044 WALK_SUBTREE (TREE_REALPART (*tp));
11045 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
11046
11047 case CONSTRUCTOR:
11048 {
11049 unsigned HOST_WIDE_INT idx;
11050 constructor_elt *ce;
11051
11052 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*tp), idx, &ce);
11053 idx++)
11054 WALK_SUBTREE (ce->value);
11055 }
11056 break;
11057
11058 case SAVE_EXPR:
11059 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
11060
11061 case BIND_EXPR:
11062 {
11063 tree decl;
11064 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
11065 {
11066 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11067 into declarations that are just mentioned, rather than
11068 declared; they don't really belong to this part of the tree.
11069 And, we can see cycles: the initializer for a declaration
11070 can refer to the declaration itself. */
11071 WALK_SUBTREE (DECL_INITIAL (decl));
11072 WALK_SUBTREE (DECL_SIZE (decl));
11073 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11074 }
11075 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
11076 }
11077
11078 case STATEMENT_LIST:
11079 {
11080 tree_stmt_iterator i;
11081 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
11082 WALK_SUBTREE (*tsi_stmt_ptr (i));
11083 }
11084 break;
11085
11086 case OMP_CLAUSE:
11087 switch (OMP_CLAUSE_CODE (*tp))
11088 {
11089 case OMP_CLAUSE_GANG:
11090 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11091 /* FALLTHRU */
11092
11093 case OMP_CLAUSE_ASYNC:
11094 case OMP_CLAUSE_WAIT:
11095 case OMP_CLAUSE_WORKER:
11096 case OMP_CLAUSE_VECTOR:
11097 case OMP_CLAUSE_NUM_GANGS:
11098 case OMP_CLAUSE_NUM_WORKERS:
11099 case OMP_CLAUSE_VECTOR_LENGTH:
11100 case OMP_CLAUSE_PRIVATE:
11101 case OMP_CLAUSE_SHARED:
11102 case OMP_CLAUSE_FIRSTPRIVATE:
11103 case OMP_CLAUSE_COPYIN:
11104 case OMP_CLAUSE_COPYPRIVATE:
11105 case OMP_CLAUSE_FINAL:
11106 case OMP_CLAUSE_IF:
11107 case OMP_CLAUSE_NUM_THREADS:
11108 case OMP_CLAUSE_SCHEDULE:
11109 case OMP_CLAUSE_UNIFORM:
11110 case OMP_CLAUSE_DEPEND:
11111 case OMP_CLAUSE_NONTEMPORAL:
11112 case OMP_CLAUSE_NUM_TEAMS:
11113 case OMP_CLAUSE_THREAD_LIMIT:
11114 case OMP_CLAUSE_DEVICE:
11115 case OMP_CLAUSE_DIST_SCHEDULE:
11116 case OMP_CLAUSE_SAFELEN:
11117 case OMP_CLAUSE_SIMDLEN:
11118 case OMP_CLAUSE_ORDERED:
11119 case OMP_CLAUSE_PRIORITY:
11120 case OMP_CLAUSE_GRAINSIZE:
11121 case OMP_CLAUSE_NUM_TASKS:
11122 case OMP_CLAUSE_HINT:
11123 case OMP_CLAUSE_TO_DECLARE:
11124 case OMP_CLAUSE_LINK:
11125 case OMP_CLAUSE_DETACH:
11126 case OMP_CLAUSE_USE_DEVICE_PTR:
11127 case OMP_CLAUSE_USE_DEVICE_ADDR:
11128 case OMP_CLAUSE_IS_DEVICE_PTR:
11129 case OMP_CLAUSE_INCLUSIVE:
11130 case OMP_CLAUSE_EXCLUSIVE:
11131 case OMP_CLAUSE__LOOPTEMP_:
11132 case OMP_CLAUSE__REDUCTEMP_:
11133 case OMP_CLAUSE__CONDTEMP_:
11134 case OMP_CLAUSE__SCANTEMP_:
11135 case OMP_CLAUSE__SIMDUID_:
11136 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
11137 /* FALLTHRU */
11138
11139 case OMP_CLAUSE_INDEPENDENT:
11140 case OMP_CLAUSE_NOWAIT:
11141 case OMP_CLAUSE_DEFAULT:
11142 case OMP_CLAUSE_UNTIED:
11143 case OMP_CLAUSE_MERGEABLE:
11144 case OMP_CLAUSE_PROC_BIND:
11145 case OMP_CLAUSE_DEVICE_TYPE:
11146 case OMP_CLAUSE_INBRANCH:
11147 case OMP_CLAUSE_NOTINBRANCH:
11148 case OMP_CLAUSE_FOR:
11149 case OMP_CLAUSE_PARALLEL:
11150 case OMP_CLAUSE_SECTIONS:
11151 case OMP_CLAUSE_TASKGROUP:
11152 case OMP_CLAUSE_NOGROUP:
11153 case OMP_CLAUSE_THREADS:
11154 case OMP_CLAUSE_SIMD:
11155 case OMP_CLAUSE_DEFAULTMAP:
11156 case OMP_CLAUSE_ORDER:
11157 case OMP_CLAUSE_BIND:
11158 case OMP_CLAUSE_AUTO:
11159 case OMP_CLAUSE_SEQ:
11160 case OMP_CLAUSE_TILE:
11161 case OMP_CLAUSE__SIMT_:
11162 case OMP_CLAUSE_IF_PRESENT:
11163 case OMP_CLAUSE_FINALIZE:
11164 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11165
11166 case OMP_CLAUSE_LASTPRIVATE:
11167 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11168 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
11169 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11170
11171 case OMP_CLAUSE_COLLAPSE:
11172 {
11173 int i;
11174 for (i = 0; i < 3; i++)
11175 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11176 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11177 }
11178
11179 case OMP_CLAUSE_LINEAR:
11180 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11181 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STEP (*tp));
11182 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STMT (*tp));
11183 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11184
11185 case OMP_CLAUSE_ALIGNED:
11186 case OMP_CLAUSE_ALLOCATE:
11187 case OMP_CLAUSE_FROM:
11188 case OMP_CLAUSE_TO:
11189 case OMP_CLAUSE_MAP:
11190 case OMP_CLAUSE__CACHE_:
11191 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11192 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11193 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11194
11195 case OMP_CLAUSE_REDUCTION:
11196 case OMP_CLAUSE_TASK_REDUCTION:
11197 case OMP_CLAUSE_IN_REDUCTION:
11198 {
11199 int i;
11200 for (i = 0; i < 5; i++)
11201 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11202 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11203 }
11204
11205 default:
11206 gcc_unreachable ();
11207 }
11208 break;
11209
11210 case TARGET_EXPR:
11211 {
11212 int i, len;
11213
11214 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11215 But, we only want to walk once. */
11216 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11217 for (i = 0; i < len; ++i)
11218 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11219 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11220 }
11221
11222 case DECL_EXPR:
11223 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11224 defining. We only want to walk into these fields of a type in this
11225 case and not in the general case of a mere reference to the type.
11226
11227 The criterion is as follows: if the field can be an expression, it
11228 must be walked only here. This should be in keeping with the fields
11229 that are directly gimplified in gimplify_type_sizes in order for the
11230 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11231 variable-sized types.
11232
11233 Note that DECLs get walked as part of processing the BIND_EXPR. */
11234 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11235 {
11236 /* Call the function for the decl so e.g. copy_tree_body_r can
11237 replace it with the remapped one. */
11238 result = (*func) (&DECL_EXPR_DECL (*tp), &walk_subtrees, data);
11239 if (result || !walk_subtrees)
11240 return result;
11241
11242 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11243 if (TREE_CODE (*type_p) == ERROR_MARK)
11244 return NULL_TREE;
11245
11246 /* Call the function for the type. See if it returns anything or
11247 doesn't want us to continue. If we are to continue, walk both
11248 the normal fields and those for the declaration case. */
11249 result = (*func) (type_p, &walk_subtrees, data);
11250 if (result || !walk_subtrees)
11251 return result;
11252
11253 /* But do not walk a pointed-to type since it may itself need to
11254 be walked in the declaration case if it isn't anonymous. */
11255 if (!POINTER_TYPE_P (*type_p))
11256 {
11257 result = walk_type_fields (*type_p, func, data, pset, lh);
11258 if (result)
11259 return result;
11260 }
11261
11262 /* If this is a record type, also walk the fields. */
11263 if (RECORD_OR_UNION_TYPE_P (*type_p))
11264 {
11265 tree field;
11266
11267 for (field = TYPE_FIELDS (*type_p); field;
11268 field = DECL_CHAIN (field))
11269 {
11270 /* We'd like to look at the type of the field, but we can
11271 easily get infinite recursion. So assume it's pointed
11272 to elsewhere in the tree. Also, ignore things that
11273 aren't fields. */
11274 if (TREE_CODE (field) != FIELD_DECL)
11275 continue;
11276
11277 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11278 WALK_SUBTREE (DECL_SIZE (field));
11279 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11280 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11281 WALK_SUBTREE (DECL_QUALIFIER (field));
11282 }
11283 }
11284
11285 /* Same for scalar types. */
11286 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11287 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11288 || TREE_CODE (*type_p) == INTEGER_TYPE
11289 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11290 || TREE_CODE (*type_p) == REAL_TYPE)
11291 {
11292 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11293 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11294 }
11295
11296 WALK_SUBTREE (TYPE_SIZE (*type_p));
11297 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11298 }
11299 /* FALLTHRU */
11300
11301 default:
11302 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11303 {
11304 int i, len;
11305
11306 /* Walk over all the sub-trees of this operand. */
11307 len = TREE_OPERAND_LENGTH (*tp);
11308
11309 /* Go through the subtrees. We need to do this in forward order so
11310 that the scope of a FOR_EXPR is handled properly. */
11311 if (len)
11312 {
11313 for (i = 0; i < len - 1; ++i)
11314 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11315 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11316 }
11317 }
11318 /* If this is a type, walk the needed fields in the type. */
11319 else if (TYPE_P (*tp))
11320 return walk_type_fields (*tp, func, data, pset, lh);
11321 break;
11322 }
11323
11324 /* We didn't find what we were looking for. */
11325 return NULL_TREE;
11326
11327 #undef WALK_SUBTREE_TAIL
11328 }
11329 #undef WALK_SUBTREE
11330
11331 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11332
11333 tree
11334 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11335 walk_tree_lh lh)
11336 {
11337 tree result;
11338
11339 hash_set<tree> pset;
11340 result = walk_tree_1 (tp, func, data, &pset, lh);
11341 return result;
11342 }
11343
11344
11345 tree
11346 tree_block (tree t)
11347 {
11348 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11349
11350 if (IS_EXPR_CODE_CLASS (c))
11351 return LOCATION_BLOCK (t->exp.locus);
11352 gcc_unreachable ();
11353 return NULL;
11354 }
11355
11356 void
11357 tree_set_block (tree t, tree b)
11358 {
11359 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11360
11361 if (IS_EXPR_CODE_CLASS (c))
11362 {
11363 t->exp.locus = set_block (t->exp.locus, b);
11364 }
11365 else
11366 gcc_unreachable ();
11367 }
11368
11369 /* Create a nameless artificial label and put it in the current
11370 function context. The label has a location of LOC. Returns the
11371 newly created label. */
11372
11373 tree
11374 create_artificial_label (location_t loc)
11375 {
11376 tree lab = build_decl (loc,
11377 LABEL_DECL, NULL_TREE, void_type_node);
11378
11379 DECL_ARTIFICIAL (lab) = 1;
11380 DECL_IGNORED_P (lab) = 1;
11381 DECL_CONTEXT (lab) = current_function_decl;
11382 return lab;
11383 }
11384
11385 /* Given a tree, try to return a useful variable name that we can use
11386 to prefix a temporary that is being assigned the value of the tree.
11387 I.E. given <temp> = &A, return A. */
11388
11389 const char *
11390 get_name (tree t)
11391 {
11392 tree stripped_decl;
11393
11394 stripped_decl = t;
11395 STRIP_NOPS (stripped_decl);
11396 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11397 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11398 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11399 {
11400 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11401 if (!name)
11402 return NULL;
11403 return IDENTIFIER_POINTER (name);
11404 }
11405 else
11406 {
11407 switch (TREE_CODE (stripped_decl))
11408 {
11409 case ADDR_EXPR:
11410 return get_name (TREE_OPERAND (stripped_decl, 0));
11411 default:
11412 return NULL;
11413 }
11414 }
11415 }
11416
11417 /* Return true if TYPE has a variable argument list. */
11418
11419 bool
11420 stdarg_p (const_tree fntype)
11421 {
11422 function_args_iterator args_iter;
11423 tree n = NULL_TREE, t;
11424
11425 if (!fntype)
11426 return false;
11427
11428 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
11429 {
11430 n = t;
11431 }
11432
11433 return n != NULL_TREE && n != void_type_node;
11434 }
11435
11436 /* Return true if TYPE has a prototype. */
11437
11438 bool
11439 prototype_p (const_tree fntype)
11440 {
11441 tree t;
11442
11443 gcc_assert (fntype != NULL_TREE);
11444
11445 t = TYPE_ARG_TYPES (fntype);
11446 return (t != NULL_TREE);
11447 }
11448
11449 /* If BLOCK is inlined from an __attribute__((__artificial__))
11450 routine, return pointer to location from where it has been
11451 called. */
11452 location_t *
11453 block_nonartificial_location (tree block)
11454 {
11455 location_t *ret = NULL;
11456
11457 while (block && TREE_CODE (block) == BLOCK
11458 && BLOCK_ABSTRACT_ORIGIN (block))
11459 {
11460 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11461 if (TREE_CODE (ao) == FUNCTION_DECL)
11462 {
11463 /* If AO is an artificial inline, point RET to the
11464 call site locus at which it has been inlined and continue
11465 the loop, in case AO's caller is also an artificial
11466 inline. */
11467 if (DECL_DECLARED_INLINE_P (ao)
11468 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11469 ret = &BLOCK_SOURCE_LOCATION (block);
11470 else
11471 break;
11472 }
11473 else if (TREE_CODE (ao) != BLOCK)
11474 break;
11475
11476 block = BLOCK_SUPERCONTEXT (block);
11477 }
11478 return ret;
11479 }
11480
11481
11482 /* If EXP is inlined from an __attribute__((__artificial__))
11483 function, return the location of the original call expression. */
11484
11485 location_t
11486 tree_nonartificial_location (tree exp)
11487 {
11488 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11489
11490 if (loc)
11491 return *loc;
11492 else
11493 return EXPR_LOCATION (exp);
11494 }
11495
11496 /* Return the location into which EXP has been inlined. Analogous
11497 to tree_nonartificial_location() above but not limited to artificial
11498 functions declared inline. If SYSTEM_HEADER is true, return
11499 the macro expansion point of the location if it's in a system header */
11500
11501 location_t
11502 tree_inlined_location (tree exp, bool system_header /* = true */)
11503 {
11504 location_t loc = UNKNOWN_LOCATION;
11505
11506 tree block = TREE_BLOCK (exp);
11507
11508 while (block && TREE_CODE (block) == BLOCK
11509 && BLOCK_ABSTRACT_ORIGIN (block))
11510 {
11511 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11512 if (TREE_CODE (ao) == FUNCTION_DECL)
11513 loc = BLOCK_SOURCE_LOCATION (block);
11514 else if (TREE_CODE (ao) != BLOCK)
11515 break;
11516
11517 block = BLOCK_SUPERCONTEXT (block);
11518 }
11519
11520 if (loc == UNKNOWN_LOCATION)
11521 {
11522 loc = EXPR_LOCATION (exp);
11523 if (system_header)
11524 /* Only consider macro expansion when the block traversal failed
11525 to find a location. Otherwise it's not relevant. */
11526 return expansion_point_location_if_in_system_header (loc);
11527 }
11528
11529 return loc;
11530 }
11531
11532 /* These are the hash table functions for the hash table of OPTIMIZATION_NODE
11533 nodes. */
11534
11535 /* Return the hash code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11536
11537 hashval_t
11538 cl_option_hasher::hash (tree x)
11539 {
11540 const_tree const t = x;
11541 const char *p;
11542 size_t i;
11543 size_t len = 0;
11544 hashval_t hash = 0;
11545
11546 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11547 {
11548 p = (const char *)TREE_OPTIMIZATION (t);
11549 len = sizeof (struct cl_optimization);
11550 }
11551
11552 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11553 return cl_target_option_hash (TREE_TARGET_OPTION (t));
11554
11555 else
11556 gcc_unreachable ();
11557
11558 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11559 something else. */
11560 for (i = 0; i < len; i++)
11561 if (p[i])
11562 hash = (hash << 4) ^ ((i << 2) | p[i]);
11563
11564 return hash;
11565 }
11566
11567 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11568 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11569 same. */
11570
11571 bool
11572 cl_option_hasher::equal (tree x, tree y)
11573 {
11574 const_tree const xt = x;
11575 const_tree const yt = y;
11576
11577 if (TREE_CODE (xt) != TREE_CODE (yt))
11578 return 0;
11579
11580 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11581 return cl_optimization_option_eq (TREE_OPTIMIZATION (xt),
11582 TREE_OPTIMIZATION (yt));
11583 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11584 return cl_target_option_eq (TREE_TARGET_OPTION (xt),
11585 TREE_TARGET_OPTION (yt));
11586 else
11587 gcc_unreachable ();
11588 }
11589
11590 /* Build an OPTIMIZATION_NODE based on the options in OPTS and OPTS_SET. */
11591
11592 tree
11593 build_optimization_node (struct gcc_options *opts,
11594 struct gcc_options *opts_set)
11595 {
11596 tree t;
11597
11598 /* Use the cache of optimization nodes. */
11599
11600 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11601 opts, opts_set);
11602
11603 tree *slot = cl_option_hash_table->find_slot (cl_optimization_node, INSERT);
11604 t = *slot;
11605 if (!t)
11606 {
11607 /* Insert this one into the hash table. */
11608 t = cl_optimization_node;
11609 *slot = t;
11610
11611 /* Make a new node for next time round. */
11612 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11613 }
11614
11615 return t;
11616 }
11617
11618 /* Build a TARGET_OPTION_NODE based on the options in OPTS and OPTS_SET. */
11619
11620 tree
11621 build_target_option_node (struct gcc_options *opts,
11622 struct gcc_options *opts_set)
11623 {
11624 tree t;
11625
11626 /* Use the cache of optimization nodes. */
11627
11628 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11629 opts, opts_set);
11630
11631 tree *slot = cl_option_hash_table->find_slot (cl_target_option_node, INSERT);
11632 t = *slot;
11633 if (!t)
11634 {
11635 /* Insert this one into the hash table. */
11636 t = cl_target_option_node;
11637 *slot = t;
11638
11639 /* Make a new node for next time round. */
11640 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11641 }
11642
11643 return t;
11644 }
11645
11646 /* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
11647 so that they aren't saved during PCH writing. */
11648
11649 void
11650 prepare_target_option_nodes_for_pch (void)
11651 {
11652 hash_table<cl_option_hasher>::iterator iter = cl_option_hash_table->begin ();
11653 for (; iter != cl_option_hash_table->end (); ++iter)
11654 if (TREE_CODE (*iter) == TARGET_OPTION_NODE)
11655 TREE_TARGET_GLOBALS (*iter) = NULL;
11656 }
11657
11658 /* Determine the "ultimate origin" of a block. */
11659
11660 tree
11661 block_ultimate_origin (const_tree block)
11662 {
11663 tree origin = BLOCK_ABSTRACT_ORIGIN (block);
11664
11665 if (origin == NULL_TREE)
11666 return NULL_TREE;
11667 else
11668 {
11669 gcc_checking_assert ((DECL_P (origin)
11670 && DECL_ORIGIN (origin) == origin)
11671 || BLOCK_ORIGIN (origin) == origin);
11672 return origin;
11673 }
11674 }
11675
11676 /* Return true iff conversion from INNER_TYPE to OUTER_TYPE generates
11677 no instruction. */
11678
11679 bool
11680 tree_nop_conversion_p (const_tree outer_type, const_tree inner_type)
11681 {
11682 /* Do not strip casts into or out of differing address spaces. */
11683 if (POINTER_TYPE_P (outer_type)
11684 && TYPE_ADDR_SPACE (TREE_TYPE (outer_type)) != ADDR_SPACE_GENERIC)
11685 {
11686 if (!POINTER_TYPE_P (inner_type)
11687 || (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
11688 != TYPE_ADDR_SPACE (TREE_TYPE (inner_type))))
11689 return false;
11690 }
11691 else if (POINTER_TYPE_P (inner_type)
11692 && TYPE_ADDR_SPACE (TREE_TYPE (inner_type)) != ADDR_SPACE_GENERIC)
11693 {
11694 /* We already know that outer_type is not a pointer with
11695 a non-generic address space. */
11696 return false;
11697 }
11698
11699 /* Use precision rather then machine mode when we can, which gives
11700 the correct answer even for submode (bit-field) types. */
11701 if ((INTEGRAL_TYPE_P (outer_type)
11702 || POINTER_TYPE_P (outer_type)
11703 || TREE_CODE (outer_type) == OFFSET_TYPE)
11704 && (INTEGRAL_TYPE_P (inner_type)
11705 || POINTER_TYPE_P (inner_type)
11706 || TREE_CODE (inner_type) == OFFSET_TYPE))
11707 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11708
11709 /* Otherwise fall back on comparing machine modes (e.g. for
11710 aggregate types, floats). */
11711 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11712 }
11713
11714 /* Return true iff conversion in EXP generates no instruction. Mark
11715 it inline so that we fully inline into the stripping functions even
11716 though we have two uses of this function. */
11717
11718 static inline bool
11719 tree_nop_conversion (const_tree exp)
11720 {
11721 tree outer_type, inner_type;
11722
11723 if (location_wrapper_p (exp))
11724 return true;
11725 if (!CONVERT_EXPR_P (exp)
11726 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11727 return false;
11728
11729 outer_type = TREE_TYPE (exp);
11730 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11731 if (!inner_type || inner_type == error_mark_node)
11732 return false;
11733
11734 return tree_nop_conversion_p (outer_type, inner_type);
11735 }
11736
11737 /* Return true iff conversion in EXP generates no instruction. Don't
11738 consider conversions changing the signedness. */
11739
11740 static bool
11741 tree_sign_nop_conversion (const_tree exp)
11742 {
11743 tree outer_type, inner_type;
11744
11745 if (!tree_nop_conversion (exp))
11746 return false;
11747
11748 outer_type = TREE_TYPE (exp);
11749 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11750
11751 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11752 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11753 }
11754
11755 /* Strip conversions from EXP according to tree_nop_conversion and
11756 return the resulting expression. */
11757
11758 tree
11759 tree_strip_nop_conversions (tree exp)
11760 {
11761 while (tree_nop_conversion (exp))
11762 exp = TREE_OPERAND (exp, 0);
11763 return exp;
11764 }
11765
11766 /* Strip conversions from EXP according to tree_sign_nop_conversion
11767 and return the resulting expression. */
11768
11769 tree
11770 tree_strip_sign_nop_conversions (tree exp)
11771 {
11772 while (tree_sign_nop_conversion (exp))
11773 exp = TREE_OPERAND (exp, 0);
11774 return exp;
11775 }
11776
11777 /* Avoid any floating point extensions from EXP. */
11778 tree
11779 strip_float_extensions (tree exp)
11780 {
11781 tree sub, expt, subt;
11782
11783 /* For floating point constant look up the narrowest type that can hold
11784 it properly and handle it like (type)(narrowest_type)constant.
11785 This way we can optimize for instance a=a*2.0 where "a" is float
11786 but 2.0 is double constant. */
11787 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11788 {
11789 REAL_VALUE_TYPE orig;
11790 tree type = NULL;
11791
11792 orig = TREE_REAL_CST (exp);
11793 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11794 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11795 type = float_type_node;
11796 else if (TYPE_PRECISION (TREE_TYPE (exp))
11797 > TYPE_PRECISION (double_type_node)
11798 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11799 type = double_type_node;
11800 if (type)
11801 return build_real_truncate (type, orig);
11802 }
11803
11804 if (!CONVERT_EXPR_P (exp))
11805 return exp;
11806
11807 sub = TREE_OPERAND (exp, 0);
11808 subt = TREE_TYPE (sub);
11809 expt = TREE_TYPE (exp);
11810
11811 if (!FLOAT_TYPE_P (subt))
11812 return exp;
11813
11814 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11815 return exp;
11816
11817 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11818 return exp;
11819
11820 return strip_float_extensions (sub);
11821 }
11822
11823 /* Strip out all handled components that produce invariant
11824 offsets. */
11825
11826 const_tree
11827 strip_invariant_refs (const_tree op)
11828 {
11829 while (handled_component_p (op))
11830 {
11831 switch (TREE_CODE (op))
11832 {
11833 case ARRAY_REF:
11834 case ARRAY_RANGE_REF:
11835 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11836 || TREE_OPERAND (op, 2) != NULL_TREE
11837 || TREE_OPERAND (op, 3) != NULL_TREE)
11838 return NULL;
11839 break;
11840
11841 case COMPONENT_REF:
11842 if (TREE_OPERAND (op, 2) != NULL_TREE)
11843 return NULL;
11844 break;
11845
11846 default:;
11847 }
11848 op = TREE_OPERAND (op, 0);
11849 }
11850
11851 return op;
11852 }
11853
11854 static GTY(()) tree gcc_eh_personality_decl;
11855
11856 /* Return the GCC personality function decl. */
11857
11858 tree
11859 lhd_gcc_personality (void)
11860 {
11861 if (!gcc_eh_personality_decl)
11862 gcc_eh_personality_decl = build_personality_function ("gcc");
11863 return gcc_eh_personality_decl;
11864 }
11865
11866 /* TARGET is a call target of GIMPLE call statement
11867 (obtained by gimple_call_fn). Return true if it is
11868 OBJ_TYPE_REF representing an virtual call of C++ method.
11869 (As opposed to OBJ_TYPE_REF representing objc calls
11870 through a cast where middle-end devirtualization machinery
11871 can't apply.) FOR_DUMP_P is true when being called from
11872 the dump routines. */
11873
11874 bool
11875 virtual_method_call_p (const_tree target, bool for_dump_p)
11876 {
11877 if (TREE_CODE (target) != OBJ_TYPE_REF)
11878 return false;
11879 tree t = TREE_TYPE (target);
11880 gcc_checking_assert (TREE_CODE (t) == POINTER_TYPE);
11881 t = TREE_TYPE (t);
11882 if (TREE_CODE (t) == FUNCTION_TYPE)
11883 return false;
11884 gcc_checking_assert (TREE_CODE (t) == METHOD_TYPE);
11885 /* If we do not have BINFO associated, it means that type was built
11886 without devirtualization enabled. Do not consider this a virtual
11887 call. */
11888 if (!TYPE_BINFO (obj_type_ref_class (target, for_dump_p)))
11889 return false;
11890 return true;
11891 }
11892
11893 /* Lookup sub-BINFO of BINFO of TYPE at offset POS. */
11894
11895 static tree
11896 lookup_binfo_at_offset (tree binfo, tree type, HOST_WIDE_INT pos)
11897 {
11898 unsigned int i;
11899 tree base_binfo, b;
11900
11901 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11902 if (pos == tree_to_shwi (BINFO_OFFSET (base_binfo))
11903 && types_same_for_odr (TREE_TYPE (base_binfo), type))
11904 return base_binfo;
11905 else if ((b = lookup_binfo_at_offset (base_binfo, type, pos)) != NULL)
11906 return b;
11907 return NULL;
11908 }
11909
11910 /* Try to find a base info of BINFO that would have its field decl at offset
11911 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11912 found, return, otherwise return NULL_TREE. */
11913
11914 tree
11915 get_binfo_at_offset (tree binfo, poly_int64 offset, tree expected_type)
11916 {
11917 tree type = BINFO_TYPE (binfo);
11918
11919 while (true)
11920 {
11921 HOST_WIDE_INT pos, size;
11922 tree fld;
11923 int i;
11924
11925 if (types_same_for_odr (type, expected_type))
11926 return binfo;
11927 if (maybe_lt (offset, 0))
11928 return NULL_TREE;
11929
11930 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
11931 {
11932 if (TREE_CODE (fld) != FIELD_DECL || !DECL_ARTIFICIAL (fld))
11933 continue;
11934
11935 pos = int_bit_position (fld);
11936 size = tree_to_uhwi (DECL_SIZE (fld));
11937 if (known_in_range_p (offset, pos, size))
11938 break;
11939 }
11940 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11941 return NULL_TREE;
11942
11943 /* Offset 0 indicates the primary base, whose vtable contents are
11944 represented in the binfo for the derived class. */
11945 else if (maybe_ne (offset, 0))
11946 {
11947 tree found_binfo = NULL, base_binfo;
11948 /* Offsets in BINFO are in bytes relative to the whole structure
11949 while POS is in bits relative to the containing field. */
11950 int binfo_offset = (tree_to_shwi (BINFO_OFFSET (binfo)) + pos
11951 / BITS_PER_UNIT);
11952
11953 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11954 if (tree_to_shwi (BINFO_OFFSET (base_binfo)) == binfo_offset
11955 && types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
11956 {
11957 found_binfo = base_binfo;
11958 break;
11959 }
11960 if (found_binfo)
11961 binfo = found_binfo;
11962 else
11963 binfo = lookup_binfo_at_offset (binfo, TREE_TYPE (fld),
11964 binfo_offset);
11965 }
11966
11967 type = TREE_TYPE (fld);
11968 offset -= pos;
11969 }
11970 }
11971
11972 /* Returns true if X is a typedef decl. */
11973
11974 bool
11975 is_typedef_decl (const_tree x)
11976 {
11977 return (x && TREE_CODE (x) == TYPE_DECL
11978 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
11979 }
11980
11981 /* Returns true iff TYPE is a type variant created for a typedef. */
11982
11983 bool
11984 typedef_variant_p (const_tree type)
11985 {
11986 return is_typedef_decl (TYPE_NAME (type));
11987 }
11988
11989 /* PR 84195: Replace control characters in "unescaped" with their
11990 escaped equivalents. Allow newlines if -fmessage-length has
11991 been set to a non-zero value. This is done here, rather than
11992 where the attribute is recorded as the message length can
11993 change between these two locations. */
11994
11995 void
11996 escaped_string::escape (const char *unescaped)
11997 {
11998 char *escaped;
11999 size_t i, new_i, len;
12000
12001 if (m_owned)
12002 free (m_str);
12003
12004 m_str = const_cast<char *> (unescaped);
12005 m_owned = false;
12006
12007 if (unescaped == NULL || *unescaped == 0)
12008 return;
12009
12010 len = strlen (unescaped);
12011 escaped = NULL;
12012 new_i = 0;
12013
12014 for (i = 0; i < len; i++)
12015 {
12016 char c = unescaped[i];
12017
12018 if (!ISCNTRL (c))
12019 {
12020 if (escaped)
12021 escaped[new_i++] = c;
12022 continue;
12023 }
12024
12025 if (c != '\n' || !pp_is_wrapping_line (global_dc->printer))
12026 {
12027 if (escaped == NULL)
12028 {
12029 /* We only allocate space for a new string if we
12030 actually encounter a control character that
12031 needs replacing. */
12032 escaped = (char *) xmalloc (len * 2 + 1);
12033 strncpy (escaped, unescaped, i);
12034 new_i = i;
12035 }
12036
12037 escaped[new_i++] = '\\';
12038
12039 switch (c)
12040 {
12041 case '\a': escaped[new_i++] = 'a'; break;
12042 case '\b': escaped[new_i++] = 'b'; break;
12043 case '\f': escaped[new_i++] = 'f'; break;
12044 case '\n': escaped[new_i++] = 'n'; break;
12045 case '\r': escaped[new_i++] = 'r'; break;
12046 case '\t': escaped[new_i++] = 't'; break;
12047 case '\v': escaped[new_i++] = 'v'; break;
12048 default: escaped[new_i++] = '?'; break;
12049 }
12050 }
12051 else if (escaped)
12052 escaped[new_i++] = c;
12053 }
12054
12055 if (escaped)
12056 {
12057 escaped[new_i] = 0;
12058 m_str = escaped;
12059 m_owned = true;
12060 }
12061 }
12062
12063 /* Warn about a use of an identifier which was marked deprecated. Returns
12064 whether a warning was given. */
12065
12066 bool
12067 warn_deprecated_use (tree node, tree attr)
12068 {
12069 escaped_string msg;
12070
12071 if (node == 0 || !warn_deprecated_decl)
12072 return false;
12073
12074 if (!attr)
12075 {
12076 if (DECL_P (node))
12077 attr = DECL_ATTRIBUTES (node);
12078 else if (TYPE_P (node))
12079 {
12080 tree decl = TYPE_STUB_DECL (node);
12081 if (decl)
12082 attr = lookup_attribute ("deprecated",
12083 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
12084 }
12085 }
12086
12087 if (attr)
12088 attr = lookup_attribute ("deprecated", attr);
12089
12090 if (attr)
12091 msg.escape (TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
12092
12093 bool w = false;
12094 if (DECL_P (node))
12095 {
12096 auto_diagnostic_group d;
12097 if (msg)
12098 w = warning (OPT_Wdeprecated_declarations,
12099 "%qD is deprecated: %s", node, (const char *) msg);
12100 else
12101 w = warning (OPT_Wdeprecated_declarations,
12102 "%qD is deprecated", node);
12103 if (w)
12104 inform (DECL_SOURCE_LOCATION (node), "declared here");
12105 }
12106 else if (TYPE_P (node))
12107 {
12108 tree what = NULL_TREE;
12109 tree decl = TYPE_STUB_DECL (node);
12110
12111 if (TYPE_NAME (node))
12112 {
12113 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12114 what = TYPE_NAME (node);
12115 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12116 && DECL_NAME (TYPE_NAME (node)))
12117 what = DECL_NAME (TYPE_NAME (node));
12118 }
12119
12120 auto_diagnostic_group d;
12121 if (what)
12122 {
12123 if (msg)
12124 w = warning (OPT_Wdeprecated_declarations,
12125 "%qE is deprecated: %s", what, (const char *) msg);
12126 else
12127 w = warning (OPT_Wdeprecated_declarations,
12128 "%qE is deprecated", what);
12129 }
12130 else
12131 {
12132 if (msg)
12133 w = warning (OPT_Wdeprecated_declarations,
12134 "type is deprecated: %s", (const char *) msg);
12135 else
12136 w = warning (OPT_Wdeprecated_declarations,
12137 "type is deprecated");
12138 }
12139
12140 if (w && decl)
12141 inform (DECL_SOURCE_LOCATION (decl), "declared here");
12142 }
12143
12144 return w;
12145 }
12146
12147 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12148 somewhere in it. */
12149
12150 bool
12151 contains_bitfld_component_ref_p (const_tree ref)
12152 {
12153 while (handled_component_p (ref))
12154 {
12155 if (TREE_CODE (ref) == COMPONENT_REF
12156 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12157 return true;
12158 ref = TREE_OPERAND (ref, 0);
12159 }
12160
12161 return false;
12162 }
12163
12164 /* Try to determine whether a TRY_CATCH expression can fall through.
12165 This is a subroutine of block_may_fallthru. */
12166
12167 static bool
12168 try_catch_may_fallthru (const_tree stmt)
12169 {
12170 tree_stmt_iterator i;
12171
12172 /* If the TRY block can fall through, the whole TRY_CATCH can
12173 fall through. */
12174 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
12175 return true;
12176
12177 i = tsi_start (TREE_OPERAND (stmt, 1));
12178 switch (TREE_CODE (tsi_stmt (i)))
12179 {
12180 case CATCH_EXPR:
12181 /* We expect to see a sequence of CATCH_EXPR trees, each with a
12182 catch expression and a body. The whole TRY_CATCH may fall
12183 through iff any of the catch bodies falls through. */
12184 for (; !tsi_end_p (i); tsi_next (&i))
12185 {
12186 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
12187 return true;
12188 }
12189 return false;
12190
12191 case EH_FILTER_EXPR:
12192 /* The exception filter expression only matters if there is an
12193 exception. If the exception does not match EH_FILTER_TYPES,
12194 we will execute EH_FILTER_FAILURE, and we will fall through
12195 if that falls through. If the exception does match
12196 EH_FILTER_TYPES, the stack unwinder will continue up the
12197 stack, so we will not fall through. We don't know whether we
12198 will throw an exception which matches EH_FILTER_TYPES or not,
12199 so we just ignore EH_FILTER_TYPES and assume that we might
12200 throw an exception which doesn't match. */
12201 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
12202
12203 default:
12204 /* This case represents statements to be executed when an
12205 exception occurs. Those statements are implicitly followed
12206 by a RESX statement to resume execution after the exception.
12207 So in this case the TRY_CATCH never falls through. */
12208 return false;
12209 }
12210 }
12211
12212 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
12213 need not be 100% accurate; simply be conservative and return true if we
12214 don't know. This is used only to avoid stupidly generating extra code.
12215 If we're wrong, we'll just delete the extra code later. */
12216
12217 bool
12218 block_may_fallthru (const_tree block)
12219 {
12220 /* This CONST_CAST is okay because expr_last returns its argument
12221 unmodified and we assign it to a const_tree. */
12222 const_tree stmt = expr_last (CONST_CAST_TREE (block));
12223
12224 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
12225 {
12226 case GOTO_EXPR:
12227 case RETURN_EXPR:
12228 /* Easy cases. If the last statement of the block implies
12229 control transfer, then we can't fall through. */
12230 return false;
12231
12232 case SWITCH_EXPR:
12233 /* If there is a default: label or case labels cover all possible
12234 SWITCH_COND values, then the SWITCH_EXPR will transfer control
12235 to some case label in all cases and all we care is whether the
12236 SWITCH_BODY falls through. */
12237 if (SWITCH_ALL_CASES_P (stmt))
12238 return block_may_fallthru (SWITCH_BODY (stmt));
12239 return true;
12240
12241 case COND_EXPR:
12242 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
12243 return true;
12244 return block_may_fallthru (COND_EXPR_ELSE (stmt));
12245
12246 case BIND_EXPR:
12247 return block_may_fallthru (BIND_EXPR_BODY (stmt));
12248
12249 case TRY_CATCH_EXPR:
12250 return try_catch_may_fallthru (stmt);
12251
12252 case TRY_FINALLY_EXPR:
12253 /* The finally clause is always executed after the try clause,
12254 so if it does not fall through, then the try-finally will not
12255 fall through. Otherwise, if the try clause does not fall
12256 through, then when the finally clause falls through it will
12257 resume execution wherever the try clause was going. So the
12258 whole try-finally will only fall through if both the try
12259 clause and the finally clause fall through. */
12260 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
12261 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
12262
12263 case EH_ELSE_EXPR:
12264 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12265
12266 case MODIFY_EXPR:
12267 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
12268 stmt = TREE_OPERAND (stmt, 1);
12269 else
12270 return true;
12271 /* FALLTHRU */
12272
12273 case CALL_EXPR:
12274 /* Functions that do not return do not fall through. */
12275 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
12276
12277 case CLEANUP_POINT_EXPR:
12278 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12279
12280 case TARGET_EXPR:
12281 return block_may_fallthru (TREE_OPERAND (stmt, 1));
12282
12283 case ERROR_MARK:
12284 return true;
12285
12286 default:
12287 return lang_hooks.block_may_fallthru (stmt);
12288 }
12289 }
12290
12291 /* True if we are using EH to handle cleanups. */
12292 static bool using_eh_for_cleanups_flag = false;
12293
12294 /* This routine is called from front ends to indicate eh should be used for
12295 cleanups. */
12296 void
12297 using_eh_for_cleanups (void)
12298 {
12299 using_eh_for_cleanups_flag = true;
12300 }
12301
12302 /* Query whether EH is used for cleanups. */
12303 bool
12304 using_eh_for_cleanups_p (void)
12305 {
12306 return using_eh_for_cleanups_flag;
12307 }
12308
12309 /* Wrapper for tree_code_name to ensure that tree code is valid */
12310 const char *
12311 get_tree_code_name (enum tree_code code)
12312 {
12313 const char *invalid = "<invalid tree code>";
12314
12315 /* The tree_code enum promotes to signed, but we could be getting
12316 invalid values, so force an unsigned comparison. */
12317 if (unsigned (code) >= MAX_TREE_CODES)
12318 {
12319 if ((unsigned)code == 0xa5a5)
12320 return "ggc_freed";
12321 return invalid;
12322 }
12323
12324 return tree_code_name[code];
12325 }
12326
12327 /* Drops the TREE_OVERFLOW flag from T. */
12328
12329 tree
12330 drop_tree_overflow (tree t)
12331 {
12332 gcc_checking_assert (TREE_OVERFLOW (t));
12333
12334 /* For tree codes with a sharing machinery re-build the result. */
12335 if (poly_int_tree_p (t))
12336 return wide_int_to_tree (TREE_TYPE (t), wi::to_poly_wide (t));
12337
12338 /* For VECTOR_CST, remove the overflow bits from the encoded elements
12339 and canonicalize the result. */
12340 if (TREE_CODE (t) == VECTOR_CST)
12341 {
12342 tree_vector_builder builder;
12343 builder.new_unary_operation (TREE_TYPE (t), t, true);
12344 unsigned int count = builder.encoded_nelts ();
12345 for (unsigned int i = 0; i < count; ++i)
12346 {
12347 tree elt = VECTOR_CST_ELT (t, i);
12348 if (TREE_OVERFLOW (elt))
12349 elt = drop_tree_overflow (elt);
12350 builder.quick_push (elt);
12351 }
12352 return builder.build ();
12353 }
12354
12355 /* Otherwise, as all tcc_constants are possibly shared, copy the node
12356 and drop the flag. */
12357 t = copy_node (t);
12358 TREE_OVERFLOW (t) = 0;
12359
12360 /* For constants that contain nested constants, drop the flag
12361 from those as well. */
12362 if (TREE_CODE (t) == COMPLEX_CST)
12363 {
12364 if (TREE_OVERFLOW (TREE_REALPART (t)))
12365 TREE_REALPART (t) = drop_tree_overflow (TREE_REALPART (t));
12366 if (TREE_OVERFLOW (TREE_IMAGPART (t)))
12367 TREE_IMAGPART (t) = drop_tree_overflow (TREE_IMAGPART (t));
12368 }
12369
12370 return t;
12371 }
12372
12373 /* Given a memory reference expression T, return its base address.
12374 The base address of a memory reference expression is the main
12375 object being referenced. For instance, the base address for
12376 'array[i].fld[j]' is 'array'. You can think of this as stripping
12377 away the offset part from a memory address.
12378
12379 This function calls handled_component_p to strip away all the inner
12380 parts of the memory reference until it reaches the base object. */
12381
12382 tree
12383 get_base_address (tree t)
12384 {
12385 while (handled_component_p (t))
12386 t = TREE_OPERAND (t, 0);
12387
12388 if ((TREE_CODE (t) == MEM_REF
12389 || TREE_CODE (t) == TARGET_MEM_REF)
12390 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
12391 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
12392
12393 /* ??? Either the alias oracle or all callers need to properly deal
12394 with WITH_SIZE_EXPRs before we can look through those. */
12395 if (TREE_CODE (t) == WITH_SIZE_EXPR)
12396 return NULL_TREE;
12397
12398 return t;
12399 }
12400
12401 /* Return a tree of sizetype representing the size, in bytes, of the element
12402 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12403
12404 tree
12405 array_ref_element_size (tree exp)
12406 {
12407 tree aligned_size = TREE_OPERAND (exp, 3);
12408 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
12409 location_t loc = EXPR_LOCATION (exp);
12410
12411 /* If a size was specified in the ARRAY_REF, it's the size measured
12412 in alignment units of the element type. So multiply by that value. */
12413 if (aligned_size)
12414 {
12415 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
12416 sizetype from another type of the same width and signedness. */
12417 if (TREE_TYPE (aligned_size) != sizetype)
12418 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
12419 return size_binop_loc (loc, MULT_EXPR, aligned_size,
12420 size_int (TYPE_ALIGN_UNIT (elmt_type)));
12421 }
12422
12423 /* Otherwise, take the size from that of the element type. Substitute
12424 any PLACEHOLDER_EXPR that we have. */
12425 else
12426 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
12427 }
12428
12429 /* Return a tree representing the lower bound of the array mentioned in
12430 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12431
12432 tree
12433 array_ref_low_bound (tree exp)
12434 {
12435 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12436
12437 /* If a lower bound is specified in EXP, use it. */
12438 if (TREE_OPERAND (exp, 2))
12439 return TREE_OPERAND (exp, 2);
12440
12441 /* Otherwise, if there is a domain type and it has a lower bound, use it,
12442 substituting for a PLACEHOLDER_EXPR as needed. */
12443 if (domain_type && TYPE_MIN_VALUE (domain_type))
12444 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
12445
12446 /* Otherwise, return a zero of the appropriate type. */
12447 tree idxtype = TREE_TYPE (TREE_OPERAND (exp, 1));
12448 return (idxtype == error_mark_node
12449 ? integer_zero_node : build_int_cst (idxtype, 0));
12450 }
12451
12452 /* Return a tree representing the upper bound of the array mentioned in
12453 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12454
12455 tree
12456 array_ref_up_bound (tree exp)
12457 {
12458 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12459
12460 /* If there is a domain type and it has an upper bound, use it, substituting
12461 for a PLACEHOLDER_EXPR as needed. */
12462 if (domain_type && TYPE_MAX_VALUE (domain_type))
12463 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
12464
12465 /* Otherwise fail. */
12466 return NULL_TREE;
12467 }
12468
12469 /* Returns true if REF is an array reference, component reference,
12470 or memory reference to an array at the end of a structure.
12471 If this is the case, the array may be allocated larger
12472 than its upper bound implies. */
12473
12474 bool
12475 array_at_struct_end_p (tree ref)
12476 {
12477 tree atype;
12478
12479 if (TREE_CODE (ref) == ARRAY_REF
12480 || TREE_CODE (ref) == ARRAY_RANGE_REF)
12481 {
12482 atype = TREE_TYPE (TREE_OPERAND (ref, 0));
12483 ref = TREE_OPERAND (ref, 0);
12484 }
12485 else if (TREE_CODE (ref) == COMPONENT_REF
12486 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 1))) == ARRAY_TYPE)
12487 atype = TREE_TYPE (TREE_OPERAND (ref, 1));
12488 else if (TREE_CODE (ref) == MEM_REF)
12489 {
12490 tree arg = TREE_OPERAND (ref, 0);
12491 if (TREE_CODE (arg) == ADDR_EXPR)
12492 arg = TREE_OPERAND (arg, 0);
12493 tree argtype = TREE_TYPE (arg);
12494 if (TREE_CODE (argtype) == RECORD_TYPE)
12495 {
12496 if (tree fld = last_field (argtype))
12497 {
12498 atype = TREE_TYPE (fld);
12499 if (TREE_CODE (atype) != ARRAY_TYPE)
12500 return false;
12501 if (VAR_P (arg) && DECL_SIZE (fld))
12502 return false;
12503 }
12504 else
12505 return false;
12506 }
12507 else
12508 return false;
12509 }
12510 else
12511 return false;
12512
12513 if (TREE_CODE (ref) == STRING_CST)
12514 return false;
12515
12516 tree ref_to_array = ref;
12517 while (handled_component_p (ref))
12518 {
12519 /* If the reference chain contains a component reference to a
12520 non-union type and there follows another field the reference
12521 is not at the end of a structure. */
12522 if (TREE_CODE (ref) == COMPONENT_REF)
12523 {
12524 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
12525 {
12526 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
12527 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
12528 nextf = DECL_CHAIN (nextf);
12529 if (nextf)
12530 return false;
12531 }
12532 }
12533 /* If we have a multi-dimensional array we do not consider
12534 a non-innermost dimension as flex array if the whole
12535 multi-dimensional array is at struct end.
12536 Same for an array of aggregates with a trailing array
12537 member. */
12538 else if (TREE_CODE (ref) == ARRAY_REF)
12539 return false;
12540 else if (TREE_CODE (ref) == ARRAY_RANGE_REF)
12541 ;
12542 /* If we view an underlying object as sth else then what we
12543 gathered up to now is what we have to rely on. */
12544 else if (TREE_CODE (ref) == VIEW_CONVERT_EXPR)
12545 break;
12546 else
12547 gcc_unreachable ();
12548
12549 ref = TREE_OPERAND (ref, 0);
12550 }
12551
12552 /* The array now is at struct end. Treat flexible arrays as
12553 always subject to extend, even into just padding constrained by
12554 an underlying decl. */
12555 if (! TYPE_SIZE (atype)
12556 || ! TYPE_DOMAIN (atype)
12557 || ! TYPE_MAX_VALUE (TYPE_DOMAIN (atype)))
12558 return true;
12559
12560 /* If the reference is based on a declared entity, the size of the array
12561 is constrained by its given domain. (Do not trust commons PR/69368). */
12562 ref = get_base_address (ref);
12563 if (ref
12564 && DECL_P (ref)
12565 && !(flag_unconstrained_commons
12566 && VAR_P (ref) && DECL_COMMON (ref))
12567 && DECL_SIZE_UNIT (ref)
12568 && TREE_CODE (DECL_SIZE_UNIT (ref)) == INTEGER_CST)
12569 {
12570 /* Check whether the array domain covers all of the available
12571 padding. */
12572 poly_int64 offset;
12573 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (atype))) != INTEGER_CST
12574 || TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))) != INTEGER_CST
12575 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (atype))) != INTEGER_CST)
12576 return true;
12577 if (! get_addr_base_and_unit_offset (ref_to_array, &offset))
12578 return true;
12579
12580 /* If at least one extra element fits it is a flexarray. */
12581 if (known_le ((wi::to_offset (TYPE_MAX_VALUE (TYPE_DOMAIN (atype)))
12582 - wi::to_offset (TYPE_MIN_VALUE (TYPE_DOMAIN (atype)))
12583 + 2)
12584 * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (atype))),
12585 wi::to_offset (DECL_SIZE_UNIT (ref)) - offset))
12586 return true;
12587
12588 return false;
12589 }
12590
12591 return true;
12592 }
12593
12594 /* Return a tree representing the offset, in bytes, of the field referenced
12595 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
12596
12597 tree
12598 component_ref_field_offset (tree exp)
12599 {
12600 tree aligned_offset = TREE_OPERAND (exp, 2);
12601 tree field = TREE_OPERAND (exp, 1);
12602 location_t loc = EXPR_LOCATION (exp);
12603
12604 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
12605 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
12606 value. */
12607 if (aligned_offset)
12608 {
12609 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
12610 sizetype from another type of the same width and signedness. */
12611 if (TREE_TYPE (aligned_offset) != sizetype)
12612 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
12613 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
12614 size_int (DECL_OFFSET_ALIGN (field)
12615 / BITS_PER_UNIT));
12616 }
12617
12618 /* Otherwise, take the offset from that of the field. Substitute
12619 any PLACEHOLDER_EXPR that we have. */
12620 else
12621 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
12622 }
12623
12624 /* Given the initializer INIT, return the initializer for the field
12625 DECL if it exists, otherwise null. Used to obtain the initializer
12626 for a flexible array member and determine its size. */
12627
12628 static tree
12629 get_initializer_for (tree init, tree decl)
12630 {
12631 STRIP_NOPS (init);
12632
12633 tree fld, fld_init;
12634 unsigned HOST_WIDE_INT i;
12635 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), i, fld, fld_init)
12636 {
12637 if (decl == fld)
12638 return fld_init;
12639
12640 if (TREE_CODE (fld) == CONSTRUCTOR)
12641 {
12642 fld_init = get_initializer_for (fld_init, decl);
12643 if (fld_init)
12644 return fld_init;
12645 }
12646 }
12647
12648 return NULL_TREE;
12649 }
12650
12651 /* Determines the size of the member referenced by the COMPONENT_REF
12652 REF, using its initializer expression if necessary in order to
12653 determine the size of an initialized flexible array member.
12654 If non-null, set *ARK when REF refers to an interior zero-length
12655 array or a trailing one-element array.
12656 Returns the size as sizetype (which might be zero for an object
12657 with an uninitialized flexible array member) or null if the size
12658 cannot be determined. */
12659
12660 tree
12661 component_ref_size (tree ref, special_array_member *sam /* = NULL */)
12662 {
12663 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
12664
12665 special_array_member sambuf;
12666 if (!sam)
12667 sam = &sambuf;
12668 *sam = special_array_member::none;
12669
12670 /* The object/argument referenced by the COMPONENT_REF and its type. */
12671 tree arg = TREE_OPERAND (ref, 0);
12672 tree argtype = TREE_TYPE (arg);
12673 /* The referenced member. */
12674 tree member = TREE_OPERAND (ref, 1);
12675
12676 tree memsize = DECL_SIZE_UNIT (member);
12677 if (memsize)
12678 {
12679 tree memtype = TREE_TYPE (member);
12680 if (TREE_CODE (memtype) != ARRAY_TYPE)
12681 /* DECL_SIZE may be less than TYPE_SIZE in C++ when referring
12682 to the type of a class with a virtual base which doesn't
12683 reflect the size of the virtual's members (see pr97595).
12684 If that's the case fail for now and implement something
12685 more robust in the future. */
12686 return (tree_int_cst_equal (memsize, TYPE_SIZE_UNIT (memtype))
12687 ? memsize : NULL_TREE);
12688
12689 bool trailing = array_at_struct_end_p (ref);
12690 bool zero_length = integer_zerop (memsize);
12691 if (!trailing && !zero_length)
12692 /* MEMBER is either an interior array or is an array with
12693 more than one element. */
12694 return memsize;
12695
12696 if (zero_length)
12697 {
12698 if (trailing)
12699 *sam = special_array_member::trail_0;
12700 else
12701 {
12702 *sam = special_array_member::int_0;
12703 memsize = NULL_TREE;
12704 }
12705 }
12706
12707 if (!zero_length)
12708 if (tree dom = TYPE_DOMAIN (memtype))
12709 if (tree min = TYPE_MIN_VALUE (dom))
12710 if (tree max = TYPE_MAX_VALUE (dom))
12711 if (TREE_CODE (min) == INTEGER_CST
12712 && TREE_CODE (max) == INTEGER_CST)
12713 {
12714 offset_int minidx = wi::to_offset (min);
12715 offset_int maxidx = wi::to_offset (max);
12716 offset_int neltsm1 = maxidx - minidx;
12717 if (neltsm1 > 0)
12718 /* MEMBER is an array with more than one element. */
12719 return memsize;
12720
12721 if (neltsm1 == 0)
12722 *sam = special_array_member::trail_1;
12723 }
12724
12725 /* For a reference to a zero- or one-element array member of a union
12726 use the size of the union instead of the size of the member. */
12727 if (TREE_CODE (argtype) == UNION_TYPE)
12728 memsize = TYPE_SIZE_UNIT (argtype);
12729 }
12730
12731 /* MEMBER is either a bona fide flexible array member, or a zero-length
12732 array member, or an array of length one treated as such. */
12733
12734 /* If the reference is to a declared object and the member a true
12735 flexible array, try to determine its size from its initializer. */
12736 poly_int64 baseoff = 0;
12737 tree base = get_addr_base_and_unit_offset (ref, &baseoff);
12738 if (!base || !VAR_P (base))
12739 {
12740 if (*sam != special_array_member::int_0)
12741 return NULL_TREE;
12742
12743 if (TREE_CODE (arg) != COMPONENT_REF)
12744 return NULL_TREE;
12745
12746 base = arg;
12747 while (TREE_CODE (base) == COMPONENT_REF)
12748 base = TREE_OPERAND (base, 0);
12749 baseoff = tree_to_poly_int64 (byte_position (TREE_OPERAND (ref, 1)));
12750 }
12751
12752 /* BASE is the declared object of which MEMBER is either a member
12753 or that is cast to ARGTYPE (e.g., a char buffer used to store
12754 an ARGTYPE object). */
12755 tree basetype = TREE_TYPE (base);
12756
12757 /* Determine the base type of the referenced object. If it's
12758 the same as ARGTYPE and MEMBER has a known size, return it. */
12759 tree bt = basetype;
12760 if (*sam != special_array_member::int_0)
12761 while (TREE_CODE (bt) == ARRAY_TYPE)
12762 bt = TREE_TYPE (bt);
12763 bool typematch = useless_type_conversion_p (argtype, bt);
12764 if (memsize && typematch)
12765 return memsize;
12766
12767 memsize = NULL_TREE;
12768
12769 if (typematch)
12770 /* MEMBER is a true flexible array member. Compute its size from
12771 the initializer of the BASE object if it has one. */
12772 if (tree init = DECL_P (base) ? DECL_INITIAL (base) : NULL_TREE)
12773 if (init != error_mark_node)
12774 {
12775 init = get_initializer_for (init, member);
12776 if (init)
12777 {
12778 memsize = TYPE_SIZE_UNIT (TREE_TYPE (init));
12779 if (tree refsize = TYPE_SIZE_UNIT (argtype))
12780 {
12781 /* Use the larger of the initializer size and the tail
12782 padding in the enclosing struct. */
12783 poly_int64 rsz = tree_to_poly_int64 (refsize);
12784 rsz -= baseoff;
12785 if (known_lt (tree_to_poly_int64 (memsize), rsz))
12786 memsize = wide_int_to_tree (TREE_TYPE (memsize), rsz);
12787 }
12788
12789 baseoff = 0;
12790 }
12791 }
12792
12793 if (!memsize)
12794 {
12795 if (typematch)
12796 {
12797 if (DECL_P (base)
12798 && DECL_EXTERNAL (base)
12799 && bt == basetype
12800 && *sam != special_array_member::int_0)
12801 /* The size of a flexible array member of an extern struct
12802 with no initializer cannot be determined (it's defined
12803 in another translation unit and can have an initializer
12804 with an arbitrary number of elements). */
12805 return NULL_TREE;
12806
12807 /* Use the size of the base struct or, for interior zero-length
12808 arrays, the size of the enclosing type. */
12809 memsize = TYPE_SIZE_UNIT (bt);
12810 }
12811 else if (DECL_P (base))
12812 /* Use the size of the BASE object (possibly an array of some
12813 other type such as char used to store the struct). */
12814 memsize = DECL_SIZE_UNIT (base);
12815 else
12816 return NULL_TREE;
12817 }
12818
12819 /* If the flexible array member has a known size use the greater
12820 of it and the tail padding in the enclosing struct.
12821 Otherwise, when the size of the flexible array member is unknown
12822 and the referenced object is not a struct, use the size of its
12823 type when known. This detects sizes of array buffers when cast
12824 to struct types with flexible array members. */
12825 if (memsize)
12826 {
12827 poly_int64 memsz64 = memsize ? tree_to_poly_int64 (memsize) : 0;
12828 if (known_lt (baseoff, memsz64))
12829 {
12830 memsz64 -= baseoff;
12831 return wide_int_to_tree (TREE_TYPE (memsize), memsz64);
12832 }
12833 return size_zero_node;
12834 }
12835
12836 /* Return "don't know" for an external non-array object since its
12837 flexible array member can be initialized to have any number of
12838 elements. Otherwise, return zero because the flexible array
12839 member has no elements. */
12840 return (DECL_P (base)
12841 && DECL_EXTERNAL (base)
12842 && (!typematch
12843 || TREE_CODE (basetype) != ARRAY_TYPE)
12844 ? NULL_TREE : size_zero_node);
12845 }
12846
12847 /* Return the machine mode of T. For vectors, returns the mode of the
12848 inner type. The main use case is to feed the result to HONOR_NANS,
12849 avoiding the BLKmode that a direct TYPE_MODE (T) might return. */
12850
12851 machine_mode
12852 element_mode (const_tree t)
12853 {
12854 if (!TYPE_P (t))
12855 t = TREE_TYPE (t);
12856 if (VECTOR_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
12857 t = TREE_TYPE (t);
12858 return TYPE_MODE (t);
12859 }
12860
12861 /* Vector types need to re-check the target flags each time we report
12862 the machine mode. We need to do this because attribute target can
12863 change the result of vector_mode_supported_p and have_regs_of_mode
12864 on a per-function basis. Thus the TYPE_MODE of a VECTOR_TYPE can
12865 change on a per-function basis. */
12866 /* ??? Possibly a better solution is to run through all the types
12867 referenced by a function and re-compute the TYPE_MODE once, rather
12868 than make the TYPE_MODE macro call a function. */
12869
12870 machine_mode
12871 vector_type_mode (const_tree t)
12872 {
12873 machine_mode mode;
12874
12875 gcc_assert (TREE_CODE (t) == VECTOR_TYPE);
12876
12877 mode = t->type_common.mode;
12878 if (VECTOR_MODE_P (mode)
12879 && (!targetm.vector_mode_supported_p (mode)
12880 || !have_regs_of_mode[mode]))
12881 {
12882 scalar_int_mode innermode;
12883
12884 /* For integers, try mapping it to a same-sized scalar mode. */
12885 if (is_int_mode (TREE_TYPE (t)->type_common.mode, &innermode))
12886 {
12887 poly_int64 size = (TYPE_VECTOR_SUBPARTS (t)
12888 * GET_MODE_BITSIZE (innermode));
12889 scalar_int_mode mode;
12890 if (int_mode_for_size (size, 0).exists (&mode)
12891 && have_regs_of_mode[mode])
12892 return mode;
12893 }
12894
12895 return BLKmode;
12896 }
12897
12898 return mode;
12899 }
12900
12901 /* Return the size in bits of each element of vector type TYPE. */
12902
12903 unsigned int
12904 vector_element_bits (const_tree type)
12905 {
12906 gcc_checking_assert (VECTOR_TYPE_P (type));
12907 if (VECTOR_BOOLEAN_TYPE_P (type))
12908 return TYPE_PRECISION (TREE_TYPE (type));
12909 return tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)));
12910 }
12911
12912 /* Calculate the size in bits of each element of vector type TYPE
12913 and return the result as a tree of type bitsizetype. */
12914
12915 tree
12916 vector_element_bits_tree (const_tree type)
12917 {
12918 gcc_checking_assert (VECTOR_TYPE_P (type));
12919 if (VECTOR_BOOLEAN_TYPE_P (type))
12920 return bitsize_int (vector_element_bits (type));
12921 return TYPE_SIZE (TREE_TYPE (type));
12922 }
12923
12924 /* Verify that basic properties of T match TV and thus T can be a variant of
12925 TV. TV should be the more specified variant (i.e. the main variant). */
12926
12927 static bool
12928 verify_type_variant (const_tree t, tree tv)
12929 {
12930 /* Type variant can differ by:
12931
12932 - TYPE_QUALS: TYPE_READONLY, TYPE_VOLATILE, TYPE_ATOMIC, TYPE_RESTRICT,
12933 ENCODE_QUAL_ADDR_SPACE.
12934 - main variant may be TYPE_COMPLETE_P and variant types !TYPE_COMPLETE_P
12935 in this case some values may not be set in the variant types
12936 (see TYPE_COMPLETE_P checks).
12937 - it is possible to have TYPE_ARTIFICIAL variant of non-artifical type
12938 - by TYPE_NAME and attributes (i.e. when variant originate by typedef)
12939 - TYPE_CANONICAL (TYPE_ALIAS_SET is the same among variants)
12940 - by the alignment: TYPE_ALIGN and TYPE_USER_ALIGN
12941 - during LTO by TYPE_CONTEXT if type is TYPE_FILE_SCOPE_P
12942 this is necessary to make it possible to merge types form different TUs
12943 - arrays, pointers and references may have TREE_TYPE that is a variant
12944 of TREE_TYPE of their main variants.
12945 - aggregates may have new TYPE_FIELDS list that list variants of
12946 the main variant TYPE_FIELDS.
12947 - vector types may differ by TYPE_VECTOR_OPAQUE
12948 */
12949
12950 /* Convenience macro for matching individual fields. */
12951 #define verify_variant_match(flag) \
12952 do { \
12953 if (flag (tv) != flag (t)) \
12954 { \
12955 error ("type variant differs by %s", #flag); \
12956 debug_tree (tv); \
12957 return false; \
12958 } \
12959 } while (false)
12960
12961 /* tree_base checks. */
12962
12963 verify_variant_match (TREE_CODE);
12964 /* FIXME: Ada builds non-artificial variants of artificial types. */
12965 #if 0
12966 if (TYPE_ARTIFICIAL (tv))
12967 verify_variant_match (TYPE_ARTIFICIAL);
12968 #endif
12969 if (POINTER_TYPE_P (tv))
12970 verify_variant_match (TYPE_REF_CAN_ALIAS_ALL);
12971 /* FIXME: TYPE_SIZES_GIMPLIFIED may differs for Ada build. */
12972 verify_variant_match (TYPE_UNSIGNED);
12973 verify_variant_match (TYPE_PACKED);
12974 if (TREE_CODE (t) == REFERENCE_TYPE)
12975 verify_variant_match (TYPE_REF_IS_RVALUE);
12976 if (AGGREGATE_TYPE_P (t))
12977 verify_variant_match (TYPE_REVERSE_STORAGE_ORDER);
12978 else
12979 verify_variant_match (TYPE_SATURATING);
12980 /* FIXME: This check trigger during libstdc++ build. */
12981 #if 0
12982 if (RECORD_OR_UNION_TYPE_P (t) && COMPLETE_TYPE_P (t))
12983 verify_variant_match (TYPE_FINAL_P);
12984 #endif
12985
12986 /* tree_type_common checks. */
12987
12988 if (COMPLETE_TYPE_P (t))
12989 {
12990 verify_variant_match (TYPE_MODE);
12991 if (TREE_CODE (TYPE_SIZE (t)) != PLACEHOLDER_EXPR
12992 && TREE_CODE (TYPE_SIZE (tv)) != PLACEHOLDER_EXPR)
12993 verify_variant_match (TYPE_SIZE);
12994 if (TREE_CODE (TYPE_SIZE_UNIT (t)) != PLACEHOLDER_EXPR
12995 && TREE_CODE (TYPE_SIZE_UNIT (tv)) != PLACEHOLDER_EXPR
12996 && TYPE_SIZE_UNIT (t) != TYPE_SIZE_UNIT (tv))
12997 {
12998 gcc_assert (!operand_equal_p (TYPE_SIZE_UNIT (t),
12999 TYPE_SIZE_UNIT (tv), 0));
13000 error ("type variant has different %<TYPE_SIZE_UNIT%>");
13001 debug_tree (tv);
13002 error ("type variant%'s %<TYPE_SIZE_UNIT%>");
13003 debug_tree (TYPE_SIZE_UNIT (tv));
13004 error ("type%'s %<TYPE_SIZE_UNIT%>");
13005 debug_tree (TYPE_SIZE_UNIT (t));
13006 return false;
13007 }
13008 verify_variant_match (TYPE_NEEDS_CONSTRUCTING);
13009 }
13010 verify_variant_match (TYPE_PRECISION);
13011 if (RECORD_OR_UNION_TYPE_P (t))
13012 verify_variant_match (TYPE_TRANSPARENT_AGGR);
13013 else if (TREE_CODE (t) == ARRAY_TYPE)
13014 verify_variant_match (TYPE_NONALIASED_COMPONENT);
13015 /* During LTO we merge variant lists from diferent translation units
13016 that may differ BY TYPE_CONTEXT that in turn may point
13017 to TRANSLATION_UNIT_DECL.
13018 Ada also builds variants of types with different TYPE_CONTEXT. */
13019 #if 0
13020 if (!in_lto_p || !TYPE_FILE_SCOPE_P (t))
13021 verify_variant_match (TYPE_CONTEXT);
13022 #endif
13023 if (TREE_CODE (t) == ARRAY_TYPE || TREE_CODE (t) == INTEGER_TYPE)
13024 verify_variant_match (TYPE_STRING_FLAG);
13025 if (TREE_CODE (t) == RECORD_TYPE || TREE_CODE (t) == UNION_TYPE)
13026 verify_variant_match (TYPE_CXX_ODR_P);
13027 if (TYPE_ALIAS_SET_KNOWN_P (t))
13028 {
13029 error ("type variant with %<TYPE_ALIAS_SET_KNOWN_P%>");
13030 debug_tree (tv);
13031 return false;
13032 }
13033
13034 /* tree_type_non_common checks. */
13035
13036 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
13037 and dangle the pointer from time to time. */
13038 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_VFIELD (t) != TYPE_VFIELD (tv)
13039 && (in_lto_p || !TYPE_VFIELD (tv)
13040 || TREE_CODE (TYPE_VFIELD (tv)) != TREE_LIST))
13041 {
13042 error ("type variant has different %<TYPE_VFIELD%>");
13043 debug_tree (tv);
13044 return false;
13045 }
13046 if ((TREE_CODE (t) == ENUMERAL_TYPE && COMPLETE_TYPE_P (t))
13047 || TREE_CODE (t) == INTEGER_TYPE
13048 || TREE_CODE (t) == BOOLEAN_TYPE
13049 || TREE_CODE (t) == REAL_TYPE
13050 || TREE_CODE (t) == FIXED_POINT_TYPE)
13051 {
13052 verify_variant_match (TYPE_MAX_VALUE);
13053 verify_variant_match (TYPE_MIN_VALUE);
13054 }
13055 if (TREE_CODE (t) == METHOD_TYPE)
13056 verify_variant_match (TYPE_METHOD_BASETYPE);
13057 if (TREE_CODE (t) == OFFSET_TYPE)
13058 verify_variant_match (TYPE_OFFSET_BASETYPE);
13059 if (TREE_CODE (t) == ARRAY_TYPE)
13060 verify_variant_match (TYPE_ARRAY_MAX_SIZE);
13061 /* FIXME: Be lax and allow TYPE_BINFO to be missing in variant types
13062 or even type's main variant. This is needed to make bootstrap pass
13063 and the bug seems new in GCC 5.
13064 C++ FE should be updated to make this consistent and we should check
13065 that TYPE_BINFO is always NULL for !COMPLETE_TYPE_P and otherwise there
13066 is a match with main variant.
13067
13068 Also disable the check for Java for now because of parser hack that builds
13069 first an dummy BINFO and then sometimes replace it by real BINFO in some
13070 of the copies. */
13071 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t) && TYPE_BINFO (tv)
13072 && TYPE_BINFO (t) != TYPE_BINFO (tv)
13073 /* FIXME: Java sometimes keep dump TYPE_BINFOs on variant types.
13074 Since there is no cheap way to tell C++/Java type w/o LTO, do checking
13075 at LTO time only. */
13076 && (in_lto_p && odr_type_p (t)))
13077 {
13078 error ("type variant has different %<TYPE_BINFO%>");
13079 debug_tree (tv);
13080 error ("type variant%'s %<TYPE_BINFO%>");
13081 debug_tree (TYPE_BINFO (tv));
13082 error ("type%'s %<TYPE_BINFO%>");
13083 debug_tree (TYPE_BINFO (t));
13084 return false;
13085 }
13086
13087 /* Check various uses of TYPE_VALUES_RAW. */
13088 if (TREE_CODE (t) == ENUMERAL_TYPE
13089 && TYPE_VALUES (t))
13090 verify_variant_match (TYPE_VALUES);
13091 else if (TREE_CODE (t) == ARRAY_TYPE)
13092 verify_variant_match (TYPE_DOMAIN);
13093 /* Permit incomplete variants of complete type. While FEs may complete
13094 all variants, this does not happen for C++ templates in all cases. */
13095 else if (RECORD_OR_UNION_TYPE_P (t)
13096 && COMPLETE_TYPE_P (t)
13097 && TYPE_FIELDS (t) != TYPE_FIELDS (tv))
13098 {
13099 tree f1, f2;
13100
13101 /* Fortran builds qualified variants as new records with items of
13102 qualified type. Verify that they looks same. */
13103 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (tv);
13104 f1 && f2;
13105 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
13106 if (TREE_CODE (f1) != FIELD_DECL || TREE_CODE (f2) != FIELD_DECL
13107 || (TYPE_MAIN_VARIANT (TREE_TYPE (f1))
13108 != TYPE_MAIN_VARIANT (TREE_TYPE (f2))
13109 /* FIXME: gfc_nonrestricted_type builds all types as variants
13110 with exception of pointer types. It deeply copies the type
13111 which means that we may end up with a variant type
13112 referring non-variant pointer. We may change it to
13113 produce types as variants, too, like
13114 objc_get_protocol_qualified_type does. */
13115 && !POINTER_TYPE_P (TREE_TYPE (f1)))
13116 || DECL_FIELD_OFFSET (f1) != DECL_FIELD_OFFSET (f2)
13117 || DECL_FIELD_BIT_OFFSET (f1) != DECL_FIELD_BIT_OFFSET (f2))
13118 break;
13119 if (f1 || f2)
13120 {
13121 error ("type variant has different %<TYPE_FIELDS%>");
13122 debug_tree (tv);
13123 error ("first mismatch is field");
13124 debug_tree (f1);
13125 error ("and field");
13126 debug_tree (f2);
13127 return false;
13128 }
13129 }
13130 else if ((TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE))
13131 verify_variant_match (TYPE_ARG_TYPES);
13132 /* For C++ the qualified variant of array type is really an array type
13133 of qualified TREE_TYPE.
13134 objc builds variants of pointer where pointer to type is a variant, too
13135 in objc_get_protocol_qualified_type. */
13136 if (TREE_TYPE (t) != TREE_TYPE (tv)
13137 && ((TREE_CODE (t) != ARRAY_TYPE
13138 && !POINTER_TYPE_P (t))
13139 || TYPE_MAIN_VARIANT (TREE_TYPE (t))
13140 != TYPE_MAIN_VARIANT (TREE_TYPE (tv))))
13141 {
13142 error ("type variant has different %<TREE_TYPE%>");
13143 debug_tree (tv);
13144 error ("type variant%'s %<TREE_TYPE%>");
13145 debug_tree (TREE_TYPE (tv));
13146 error ("type%'s %<TREE_TYPE%>");
13147 debug_tree (TREE_TYPE (t));
13148 return false;
13149 }
13150 if (type_with_alias_set_p (t)
13151 && !gimple_canonical_types_compatible_p (t, tv, false))
13152 {
13153 error ("type is not compatible with its variant");
13154 debug_tree (tv);
13155 error ("type variant%'s %<TREE_TYPE%>");
13156 debug_tree (TREE_TYPE (tv));
13157 error ("type%'s %<TREE_TYPE%>");
13158 debug_tree (TREE_TYPE (t));
13159 return false;
13160 }
13161 return true;
13162 #undef verify_variant_match
13163 }
13164
13165
13166 /* The TYPE_CANONICAL merging machinery. It should closely resemble
13167 the middle-end types_compatible_p function. It needs to avoid
13168 claiming types are different for types that should be treated
13169 the same with respect to TBAA. Canonical types are also used
13170 for IL consistency checks via the useless_type_conversion_p
13171 predicate which does not handle all type kinds itself but falls
13172 back to pointer-comparison of TYPE_CANONICAL for aggregates
13173 for example. */
13174
13175 /* Return true if TYPE_UNSIGNED of TYPE should be ignored for canonical
13176 type calculation because we need to allow inter-operability between signed
13177 and unsigned variants. */
13178
13179 bool
13180 type_with_interoperable_signedness (const_tree type)
13181 {
13182 /* Fortran standard require C_SIGNED_CHAR to be interoperable with both
13183 signed char and unsigned char. Similarly fortran FE builds
13184 C_SIZE_T as signed type, while C defines it unsigned. */
13185
13186 return tree_code_for_canonical_type_merging (TREE_CODE (type))
13187 == INTEGER_TYPE
13188 && (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node)
13189 || TYPE_PRECISION (type) == TYPE_PRECISION (size_type_node));
13190 }
13191
13192 /* Return true iff T1 and T2 are structurally identical for what
13193 TBAA is concerned.
13194 This function is used both by lto.c canonical type merging and by the
13195 verifier. If TRUST_TYPE_CANONICAL we do not look into structure of types
13196 that have TYPE_CANONICAL defined and assume them equivalent. This is useful
13197 only for LTO because only in these cases TYPE_CANONICAL equivalence
13198 correspond to one defined by gimple_canonical_types_compatible_p. */
13199
13200 bool
13201 gimple_canonical_types_compatible_p (const_tree t1, const_tree t2,
13202 bool trust_type_canonical)
13203 {
13204 /* Type variants should be same as the main variant. When not doing sanity
13205 checking to verify this fact, go to main variants and save some work. */
13206 if (trust_type_canonical)
13207 {
13208 t1 = TYPE_MAIN_VARIANT (t1);
13209 t2 = TYPE_MAIN_VARIANT (t2);
13210 }
13211
13212 /* Check first for the obvious case of pointer identity. */
13213 if (t1 == t2)
13214 return true;
13215
13216 /* Check that we have two types to compare. */
13217 if (t1 == NULL_TREE || t2 == NULL_TREE)
13218 return false;
13219
13220 /* We consider complete types always compatible with incomplete type.
13221 This does not make sense for canonical type calculation and thus we
13222 need to ensure that we are never called on it.
13223
13224 FIXME: For more correctness the function probably should have three modes
13225 1) mode assuming that types are complete mathcing their structure
13226 2) mode allowing incomplete types but producing equivalence classes
13227 and thus ignoring all info from complete types
13228 3) mode allowing incomplete types to match complete but checking
13229 compatibility between complete types.
13230
13231 1 and 2 can be used for canonical type calculation. 3 is the real
13232 definition of type compatibility that can be used i.e. for warnings during
13233 declaration merging. */
13234
13235 gcc_assert (!trust_type_canonical
13236 || (type_with_alias_set_p (t1) && type_with_alias_set_p (t2)));
13237
13238 /* If the types have been previously registered and found equal
13239 they still are. */
13240
13241 if (TYPE_CANONICAL (t1) && TYPE_CANONICAL (t2)
13242 && trust_type_canonical)
13243 {
13244 /* Do not use TYPE_CANONICAL of pointer types. For LTO streamed types
13245 they are always NULL, but they are set to non-NULL for types
13246 constructed by build_pointer_type and variants. In this case the
13247 TYPE_CANONICAL is more fine grained than the equivalnce we test (where
13248 all pointers are considered equal. Be sure to not return false
13249 negatives. */
13250 gcc_checking_assert (canonical_type_used_p (t1)
13251 && canonical_type_used_p (t2));
13252 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
13253 }
13254
13255 /* For types where we do ODR based TBAA the canonical type is always
13256 set correctly, so we know that types are different if their
13257 canonical types does not match. */
13258 if (trust_type_canonical
13259 && (odr_type_p (t1) && odr_based_tbaa_p (t1))
13260 != (odr_type_p (t2) && odr_based_tbaa_p (t2)))
13261 return false;
13262
13263 /* Can't be the same type if the types don't have the same code. */
13264 enum tree_code code = tree_code_for_canonical_type_merging (TREE_CODE (t1));
13265 if (code != tree_code_for_canonical_type_merging (TREE_CODE (t2)))
13266 return false;
13267
13268 /* Qualifiers do not matter for canonical type comparison purposes. */
13269
13270 /* Void types and nullptr types are always the same. */
13271 if (TREE_CODE (t1) == VOID_TYPE
13272 || TREE_CODE (t1) == NULLPTR_TYPE)
13273 return true;
13274
13275 /* Can't be the same type if they have different mode. */
13276 if (TYPE_MODE (t1) != TYPE_MODE (t2))
13277 return false;
13278
13279 /* Non-aggregate types can be handled cheaply. */
13280 if (INTEGRAL_TYPE_P (t1)
13281 || SCALAR_FLOAT_TYPE_P (t1)
13282 || FIXED_POINT_TYPE_P (t1)
13283 || TREE_CODE (t1) == VECTOR_TYPE
13284 || TREE_CODE (t1) == COMPLEX_TYPE
13285 || TREE_CODE (t1) == OFFSET_TYPE
13286 || POINTER_TYPE_P (t1))
13287 {
13288 /* Can't be the same type if they have different recision. */
13289 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
13290 return false;
13291
13292 /* In some cases the signed and unsigned types are required to be
13293 inter-operable. */
13294 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2)
13295 && !type_with_interoperable_signedness (t1))
13296 return false;
13297
13298 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
13299 interoperable with "signed char". Unless all frontends are revisited
13300 to agree on these types, we must ignore the flag completely. */
13301
13302 /* Fortran standard define C_PTR type that is compatible with every
13303 C pointer. For this reason we need to glob all pointers into one.
13304 Still pointers in different address spaces are not compatible. */
13305 if (POINTER_TYPE_P (t1))
13306 {
13307 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
13308 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
13309 return false;
13310 }
13311
13312 /* Tail-recurse to components. */
13313 if (TREE_CODE (t1) == VECTOR_TYPE
13314 || TREE_CODE (t1) == COMPLEX_TYPE)
13315 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
13316 TREE_TYPE (t2),
13317 trust_type_canonical);
13318
13319 return true;
13320 }
13321
13322 /* Do type-specific comparisons. */
13323 switch (TREE_CODE (t1))
13324 {
13325 case ARRAY_TYPE:
13326 /* Array types are the same if the element types are the same and
13327 the number of elements are the same. */
13328 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13329 trust_type_canonical)
13330 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
13331 || TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2)
13332 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
13333 return false;
13334 else
13335 {
13336 tree i1 = TYPE_DOMAIN (t1);
13337 tree i2 = TYPE_DOMAIN (t2);
13338
13339 /* For an incomplete external array, the type domain can be
13340 NULL_TREE. Check this condition also. */
13341 if (i1 == NULL_TREE && i2 == NULL_TREE)
13342 return true;
13343 else if (i1 == NULL_TREE || i2 == NULL_TREE)
13344 return false;
13345 else
13346 {
13347 tree min1 = TYPE_MIN_VALUE (i1);
13348 tree min2 = TYPE_MIN_VALUE (i2);
13349 tree max1 = TYPE_MAX_VALUE (i1);
13350 tree max2 = TYPE_MAX_VALUE (i2);
13351
13352 /* The minimum/maximum values have to be the same. */
13353 if ((min1 == min2
13354 || (min1 && min2
13355 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
13356 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
13357 || operand_equal_p (min1, min2, 0))))
13358 && (max1 == max2
13359 || (max1 && max2
13360 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
13361 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
13362 || operand_equal_p (max1, max2, 0)))))
13363 return true;
13364 else
13365 return false;
13366 }
13367 }
13368
13369 case METHOD_TYPE:
13370 case FUNCTION_TYPE:
13371 /* Function types are the same if the return type and arguments types
13372 are the same. */
13373 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13374 trust_type_canonical))
13375 return false;
13376
13377 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
13378 return true;
13379 else
13380 {
13381 tree parms1, parms2;
13382
13383 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
13384 parms1 && parms2;
13385 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
13386 {
13387 if (!gimple_canonical_types_compatible_p
13388 (TREE_VALUE (parms1), TREE_VALUE (parms2),
13389 trust_type_canonical))
13390 return false;
13391 }
13392
13393 if (parms1 || parms2)
13394 return false;
13395
13396 return true;
13397 }
13398
13399 case RECORD_TYPE:
13400 case UNION_TYPE:
13401 case QUAL_UNION_TYPE:
13402 {
13403 tree f1, f2;
13404
13405 /* Don't try to compare variants of an incomplete type, before
13406 TYPE_FIELDS has been copied around. */
13407 if (!COMPLETE_TYPE_P (t1) && !COMPLETE_TYPE_P (t2))
13408 return true;
13409
13410
13411 if (TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2))
13412 return false;
13413
13414 /* For aggregate types, all the fields must be the same. */
13415 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
13416 f1 || f2;
13417 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
13418 {
13419 /* Skip non-fields and zero-sized fields. */
13420 while (f1 && (TREE_CODE (f1) != FIELD_DECL
13421 || (DECL_SIZE (f1)
13422 && integer_zerop (DECL_SIZE (f1)))))
13423 f1 = TREE_CHAIN (f1);
13424 while (f2 && (TREE_CODE (f2) != FIELD_DECL
13425 || (DECL_SIZE (f2)
13426 && integer_zerop (DECL_SIZE (f2)))))
13427 f2 = TREE_CHAIN (f2);
13428 if (!f1 || !f2)
13429 break;
13430 /* The fields must have the same name, offset and type. */
13431 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
13432 || !gimple_compare_field_offset (f1, f2)
13433 || !gimple_canonical_types_compatible_p
13434 (TREE_TYPE (f1), TREE_TYPE (f2),
13435 trust_type_canonical))
13436 return false;
13437 }
13438
13439 /* If one aggregate has more fields than the other, they
13440 are not the same. */
13441 if (f1 || f2)
13442 return false;
13443
13444 return true;
13445 }
13446
13447 default:
13448 /* Consider all types with language specific trees in them mutually
13449 compatible. This is executed only from verify_type and false
13450 positives can be tolerated. */
13451 gcc_assert (!in_lto_p);
13452 return true;
13453 }
13454 }
13455
13456 /* Verify type T. */
13457
13458 void
13459 verify_type (const_tree t)
13460 {
13461 bool error_found = false;
13462 tree mv = TYPE_MAIN_VARIANT (t);
13463 if (!mv)
13464 {
13465 error ("main variant is not defined");
13466 error_found = true;
13467 }
13468 else if (mv != TYPE_MAIN_VARIANT (mv))
13469 {
13470 error ("%<TYPE_MAIN_VARIANT%> has different %<TYPE_MAIN_VARIANT%>");
13471 debug_tree (mv);
13472 error_found = true;
13473 }
13474 else if (t != mv && !verify_type_variant (t, mv))
13475 error_found = true;
13476
13477 tree ct = TYPE_CANONICAL (t);
13478 if (!ct)
13479 ;
13480 else if (TYPE_CANONICAL (t) != ct)
13481 {
13482 error ("%<TYPE_CANONICAL%> has different %<TYPE_CANONICAL%>");
13483 debug_tree (ct);
13484 error_found = true;
13485 }
13486 /* Method and function types cannot be used to address memory and thus
13487 TYPE_CANONICAL really matters only for determining useless conversions.
13488
13489 FIXME: C++ FE produce declarations of builtin functions that are not
13490 compatible with main variants. */
13491 else if (TREE_CODE (t) == FUNCTION_TYPE)
13492 ;
13493 else if (t != ct
13494 /* FIXME: gimple_canonical_types_compatible_p cannot compare types
13495 with variably sized arrays because their sizes possibly
13496 gimplified to different variables. */
13497 && !variably_modified_type_p (ct, NULL)
13498 && !gimple_canonical_types_compatible_p (t, ct, false)
13499 && COMPLETE_TYPE_P (t))
13500 {
13501 error ("%<TYPE_CANONICAL%> is not compatible");
13502 debug_tree (ct);
13503 error_found = true;
13504 }
13505
13506 if (COMPLETE_TYPE_P (t) && TYPE_CANONICAL (t)
13507 && TYPE_MODE (t) != TYPE_MODE (TYPE_CANONICAL (t)))
13508 {
13509 error ("%<TYPE_MODE%> of %<TYPE_CANONICAL%> is not compatible");
13510 debug_tree (ct);
13511 error_found = true;
13512 }
13513 if (TYPE_MAIN_VARIANT (t) == t && ct && TYPE_MAIN_VARIANT (ct) != ct)
13514 {
13515 error ("%<TYPE_CANONICAL%> of main variant is not main variant");
13516 debug_tree (ct);
13517 debug_tree (TYPE_MAIN_VARIANT (ct));
13518 error_found = true;
13519 }
13520
13521
13522 /* Check various uses of TYPE_MIN_VALUE_RAW. */
13523 if (RECORD_OR_UNION_TYPE_P (t))
13524 {
13525 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
13526 and danagle the pointer from time to time. */
13527 if (TYPE_VFIELD (t)
13528 && TREE_CODE (TYPE_VFIELD (t)) != FIELD_DECL
13529 && TREE_CODE (TYPE_VFIELD (t)) != TREE_LIST)
13530 {
13531 error ("%<TYPE_VFIELD%> is not %<FIELD_DECL%> nor %<TREE_LIST%>");
13532 debug_tree (TYPE_VFIELD (t));
13533 error_found = true;
13534 }
13535 }
13536 else if (TREE_CODE (t) == POINTER_TYPE)
13537 {
13538 if (TYPE_NEXT_PTR_TO (t)
13539 && TREE_CODE (TYPE_NEXT_PTR_TO (t)) != POINTER_TYPE)
13540 {
13541 error ("%<TYPE_NEXT_PTR_TO%> is not %<POINTER_TYPE%>");
13542 debug_tree (TYPE_NEXT_PTR_TO (t));
13543 error_found = true;
13544 }
13545 }
13546 else if (TREE_CODE (t) == REFERENCE_TYPE)
13547 {
13548 if (TYPE_NEXT_REF_TO (t)
13549 && TREE_CODE (TYPE_NEXT_REF_TO (t)) != REFERENCE_TYPE)
13550 {
13551 error ("%<TYPE_NEXT_REF_TO%> is not %<REFERENCE_TYPE%>");
13552 debug_tree (TYPE_NEXT_REF_TO (t));
13553 error_found = true;
13554 }
13555 }
13556 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
13557 || TREE_CODE (t) == FIXED_POINT_TYPE)
13558 {
13559 /* FIXME: The following check should pass:
13560 useless_type_conversion_p (const_cast <tree> (t),
13561 TREE_TYPE (TYPE_MIN_VALUE (t))
13562 but does not for C sizetypes in LTO. */
13563 }
13564
13565 /* Check various uses of TYPE_MAXVAL_RAW. */
13566 if (RECORD_OR_UNION_TYPE_P (t))
13567 {
13568 if (!TYPE_BINFO (t))
13569 ;
13570 else if (TREE_CODE (TYPE_BINFO (t)) != TREE_BINFO)
13571 {
13572 error ("%<TYPE_BINFO%> is not %<TREE_BINFO%>");
13573 debug_tree (TYPE_BINFO (t));
13574 error_found = true;
13575 }
13576 else if (TREE_TYPE (TYPE_BINFO (t)) != TYPE_MAIN_VARIANT (t))
13577 {
13578 error ("%<TYPE_BINFO%> type is not %<TYPE_MAIN_VARIANT%>");
13579 debug_tree (TREE_TYPE (TYPE_BINFO (t)));
13580 error_found = true;
13581 }
13582 }
13583 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
13584 {
13585 if (TYPE_METHOD_BASETYPE (t)
13586 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != RECORD_TYPE
13587 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != UNION_TYPE)
13588 {
13589 error ("%<TYPE_METHOD_BASETYPE%> is not record nor union");
13590 debug_tree (TYPE_METHOD_BASETYPE (t));
13591 error_found = true;
13592 }
13593 }
13594 else if (TREE_CODE (t) == OFFSET_TYPE)
13595 {
13596 if (TYPE_OFFSET_BASETYPE (t)
13597 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != RECORD_TYPE
13598 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != UNION_TYPE)
13599 {
13600 error ("%<TYPE_OFFSET_BASETYPE%> is not record nor union");
13601 debug_tree (TYPE_OFFSET_BASETYPE (t));
13602 error_found = true;
13603 }
13604 }
13605 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
13606 || TREE_CODE (t) == FIXED_POINT_TYPE)
13607 {
13608 /* FIXME: The following check should pass:
13609 useless_type_conversion_p (const_cast <tree> (t),
13610 TREE_TYPE (TYPE_MAX_VALUE (t))
13611 but does not for C sizetypes in LTO. */
13612 }
13613 else if (TREE_CODE (t) == ARRAY_TYPE)
13614 {
13615 if (TYPE_ARRAY_MAX_SIZE (t)
13616 && TREE_CODE (TYPE_ARRAY_MAX_SIZE (t)) != INTEGER_CST)
13617 {
13618 error ("%<TYPE_ARRAY_MAX_SIZE%> not %<INTEGER_CST%>");
13619 debug_tree (TYPE_ARRAY_MAX_SIZE (t));
13620 error_found = true;
13621 }
13622 }
13623 else if (TYPE_MAX_VALUE_RAW (t))
13624 {
13625 error ("%<TYPE_MAX_VALUE_RAW%> non-NULL");
13626 debug_tree (TYPE_MAX_VALUE_RAW (t));
13627 error_found = true;
13628 }
13629
13630 if (TYPE_LANG_SLOT_1 (t) && in_lto_p)
13631 {
13632 error ("%<TYPE_LANG_SLOT_1 (binfo)%> field is non-NULL");
13633 debug_tree (TYPE_LANG_SLOT_1 (t));
13634 error_found = true;
13635 }
13636
13637 /* Check various uses of TYPE_VALUES_RAW. */
13638 if (TREE_CODE (t) == ENUMERAL_TYPE)
13639 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
13640 {
13641 tree value = TREE_VALUE (l);
13642 tree name = TREE_PURPOSE (l);
13643
13644 /* C FE porduce INTEGER_CST of INTEGER_TYPE, while C++ FE uses
13645 CONST_DECL of ENUMERAL TYPE. */
13646 if (TREE_CODE (value) != INTEGER_CST && TREE_CODE (value) != CONST_DECL)
13647 {
13648 error ("enum value is not %<CONST_DECL%> or %<INTEGER_CST%>");
13649 debug_tree (value);
13650 debug_tree (name);
13651 error_found = true;
13652 }
13653 if (TREE_CODE (TREE_TYPE (value)) != INTEGER_TYPE
13654 && !useless_type_conversion_p (const_cast <tree> (t), TREE_TYPE (value)))
13655 {
13656 error ("enum value type is not %<INTEGER_TYPE%> nor convertible "
13657 "to the enum");
13658 debug_tree (value);
13659 debug_tree (name);
13660 error_found = true;
13661 }
13662 if (TREE_CODE (name) != IDENTIFIER_NODE)
13663 {
13664 error ("enum value name is not %<IDENTIFIER_NODE%>");
13665 debug_tree (value);
13666 debug_tree (name);
13667 error_found = true;
13668 }
13669 }
13670 else if (TREE_CODE (t) == ARRAY_TYPE)
13671 {
13672 if (TYPE_DOMAIN (t) && TREE_CODE (TYPE_DOMAIN (t)) != INTEGER_TYPE)
13673 {
13674 error ("array %<TYPE_DOMAIN%> is not integer type");
13675 debug_tree (TYPE_DOMAIN (t));
13676 error_found = true;
13677 }
13678 }
13679 else if (RECORD_OR_UNION_TYPE_P (t))
13680 {
13681 if (TYPE_FIELDS (t) && !COMPLETE_TYPE_P (t) && in_lto_p)
13682 {
13683 error ("%<TYPE_FIELDS%> defined in incomplete type");
13684 error_found = true;
13685 }
13686 for (tree fld = TYPE_FIELDS (t); fld; fld = TREE_CHAIN (fld))
13687 {
13688 /* TODO: verify properties of decls. */
13689 if (TREE_CODE (fld) == FIELD_DECL)
13690 ;
13691 else if (TREE_CODE (fld) == TYPE_DECL)
13692 ;
13693 else if (TREE_CODE (fld) == CONST_DECL)
13694 ;
13695 else if (VAR_P (fld))
13696 ;
13697 else if (TREE_CODE (fld) == TEMPLATE_DECL)
13698 ;
13699 else if (TREE_CODE (fld) == USING_DECL)
13700 ;
13701 else if (TREE_CODE (fld) == FUNCTION_DECL)
13702 ;
13703 else
13704 {
13705 error ("wrong tree in %<TYPE_FIELDS%> list");
13706 debug_tree (fld);
13707 error_found = true;
13708 }
13709 }
13710 }
13711 else if (TREE_CODE (t) == INTEGER_TYPE
13712 || TREE_CODE (t) == BOOLEAN_TYPE
13713 || TREE_CODE (t) == OFFSET_TYPE
13714 || TREE_CODE (t) == REFERENCE_TYPE
13715 || TREE_CODE (t) == NULLPTR_TYPE
13716 || TREE_CODE (t) == POINTER_TYPE)
13717 {
13718 if (TYPE_CACHED_VALUES_P (t) != (TYPE_CACHED_VALUES (t) != NULL))
13719 {
13720 error ("%<TYPE_CACHED_VALUES_P%> is %i while %<TYPE_CACHED_VALUES%> "
13721 "is %p",
13722 TYPE_CACHED_VALUES_P (t), (void *)TYPE_CACHED_VALUES (t));
13723 error_found = true;
13724 }
13725 else if (TYPE_CACHED_VALUES_P (t) && TREE_CODE (TYPE_CACHED_VALUES (t)) != TREE_VEC)
13726 {
13727 error ("%<TYPE_CACHED_VALUES%> is not %<TREE_VEC%>");
13728 debug_tree (TYPE_CACHED_VALUES (t));
13729 error_found = true;
13730 }
13731 /* Verify just enough of cache to ensure that no one copied it to new type.
13732 All copying should go by copy_node that should clear it. */
13733 else if (TYPE_CACHED_VALUES_P (t))
13734 {
13735 int i;
13736 for (i = 0; i < TREE_VEC_LENGTH (TYPE_CACHED_VALUES (t)); i++)
13737 if (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)
13738 && TREE_TYPE (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)) != t)
13739 {
13740 error ("wrong %<TYPE_CACHED_VALUES%> entry");
13741 debug_tree (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i));
13742 error_found = true;
13743 break;
13744 }
13745 }
13746 }
13747 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
13748 for (tree l = TYPE_ARG_TYPES (t); l; l = TREE_CHAIN (l))
13749 {
13750 /* C++ FE uses TREE_PURPOSE to store initial values. */
13751 if (TREE_PURPOSE (l) && in_lto_p)
13752 {
13753 error ("%<TREE_PURPOSE%> is non-NULL in %<TYPE_ARG_TYPES%> list");
13754 debug_tree (l);
13755 error_found = true;
13756 }
13757 if (!TYPE_P (TREE_VALUE (l)))
13758 {
13759 error ("wrong entry in %<TYPE_ARG_TYPES%> list");
13760 debug_tree (l);
13761 error_found = true;
13762 }
13763 }
13764 else if (!is_lang_specific (t) && TYPE_VALUES_RAW (t))
13765 {
13766 error ("%<TYPE_VALUES_RAW%> field is non-NULL");
13767 debug_tree (TYPE_VALUES_RAW (t));
13768 error_found = true;
13769 }
13770 if (TREE_CODE (t) != INTEGER_TYPE
13771 && TREE_CODE (t) != BOOLEAN_TYPE
13772 && TREE_CODE (t) != OFFSET_TYPE
13773 && TREE_CODE (t) != REFERENCE_TYPE
13774 && TREE_CODE (t) != NULLPTR_TYPE
13775 && TREE_CODE (t) != POINTER_TYPE
13776 && TYPE_CACHED_VALUES_P (t))
13777 {
13778 error ("%<TYPE_CACHED_VALUES_P%> is set while it should not be");
13779 error_found = true;
13780 }
13781
13782 /* ipa-devirt makes an assumption that TYPE_METHOD_BASETYPE is always
13783 TYPE_MAIN_VARIANT and it would be odd to add methods only to variatns
13784 of a type. */
13785 if (TREE_CODE (t) == METHOD_TYPE
13786 && TYPE_MAIN_VARIANT (TYPE_METHOD_BASETYPE (t)) != TYPE_METHOD_BASETYPE (t))
13787 {
13788 error ("%<TYPE_METHOD_BASETYPE%> is not main variant");
13789 error_found = true;
13790 }
13791
13792 if (error_found)
13793 {
13794 debug_tree (const_cast <tree> (t));
13795 internal_error ("%qs failed", __func__);
13796 }
13797 }
13798
13799
13800 /* Return 1 if ARG interpreted as signed in its precision is known to be
13801 always positive or 2 if ARG is known to be always negative, or 3 if
13802 ARG may be positive or negative. */
13803
13804 int
13805 get_range_pos_neg (tree arg)
13806 {
13807 if (arg == error_mark_node)
13808 return 3;
13809
13810 int prec = TYPE_PRECISION (TREE_TYPE (arg));
13811 int cnt = 0;
13812 if (TREE_CODE (arg) == INTEGER_CST)
13813 {
13814 wide_int w = wi::sext (wi::to_wide (arg), prec);
13815 if (wi::neg_p (w))
13816 return 2;
13817 else
13818 return 1;
13819 }
13820 while (CONVERT_EXPR_P (arg)
13821 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
13822 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg, 0))) <= prec)
13823 {
13824 arg = TREE_OPERAND (arg, 0);
13825 /* Narrower value zero extended into wider type
13826 will always result in positive values. */
13827 if (TYPE_UNSIGNED (TREE_TYPE (arg))
13828 && TYPE_PRECISION (TREE_TYPE (arg)) < prec)
13829 return 1;
13830 prec = TYPE_PRECISION (TREE_TYPE (arg));
13831 if (++cnt > 30)
13832 return 3;
13833 }
13834
13835 if (TREE_CODE (arg) != SSA_NAME)
13836 return 3;
13837 wide_int arg_min, arg_max;
13838 while (get_range_info (arg, &arg_min, &arg_max) != VR_RANGE)
13839 {
13840 gimple *g = SSA_NAME_DEF_STMT (arg);
13841 if (is_gimple_assign (g)
13842 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (g)))
13843 {
13844 tree t = gimple_assign_rhs1 (g);
13845 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
13846 && TYPE_PRECISION (TREE_TYPE (t)) <= prec)
13847 {
13848 if (TYPE_UNSIGNED (TREE_TYPE (t))
13849 && TYPE_PRECISION (TREE_TYPE (t)) < prec)
13850 return 1;
13851 prec = TYPE_PRECISION (TREE_TYPE (t));
13852 arg = t;
13853 if (++cnt > 30)
13854 return 3;
13855 continue;
13856 }
13857 }
13858 return 3;
13859 }
13860 if (TYPE_UNSIGNED (TREE_TYPE (arg)))
13861 {
13862 /* For unsigned values, the "positive" range comes
13863 below the "negative" range. */
13864 if (!wi::neg_p (wi::sext (arg_max, prec), SIGNED))
13865 return 1;
13866 if (wi::neg_p (wi::sext (arg_min, prec), SIGNED))
13867 return 2;
13868 }
13869 else
13870 {
13871 if (!wi::neg_p (wi::sext (arg_min, prec), SIGNED))
13872 return 1;
13873 if (wi::neg_p (wi::sext (arg_max, prec), SIGNED))
13874 return 2;
13875 }
13876 return 3;
13877 }
13878
13879
13880
13881
13882 /* Return true if ARG is marked with the nonnull attribute in the
13883 current function signature. */
13884
13885 bool
13886 nonnull_arg_p (const_tree arg)
13887 {
13888 tree t, attrs, fntype;
13889 unsigned HOST_WIDE_INT arg_num;
13890
13891 gcc_assert (TREE_CODE (arg) == PARM_DECL
13892 && (POINTER_TYPE_P (TREE_TYPE (arg))
13893 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE));
13894
13895 /* The static chain decl is always non null. */
13896 if (arg == cfun->static_chain_decl)
13897 return true;
13898
13899 /* THIS argument of method is always non-NULL. */
13900 if (TREE_CODE (TREE_TYPE (cfun->decl)) == METHOD_TYPE
13901 && arg == DECL_ARGUMENTS (cfun->decl)
13902 && flag_delete_null_pointer_checks)
13903 return true;
13904
13905 /* Values passed by reference are always non-NULL. */
13906 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
13907 && flag_delete_null_pointer_checks)
13908 return true;
13909
13910 fntype = TREE_TYPE (cfun->decl);
13911 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
13912 {
13913 attrs = lookup_attribute ("nonnull", attrs);
13914
13915 /* If "nonnull" wasn't specified, we know nothing about the argument. */
13916 if (attrs == NULL_TREE)
13917 return false;
13918
13919 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
13920 if (TREE_VALUE (attrs) == NULL_TREE)
13921 return true;
13922
13923 /* Get the position number for ARG in the function signature. */
13924 for (arg_num = 1, t = DECL_ARGUMENTS (cfun->decl);
13925 t;
13926 t = DECL_CHAIN (t), arg_num++)
13927 {
13928 if (t == arg)
13929 break;
13930 }
13931
13932 gcc_assert (t == arg);
13933
13934 /* Now see if ARG_NUM is mentioned in the nonnull list. */
13935 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
13936 {
13937 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
13938 return true;
13939 }
13940 }
13941
13942 return false;
13943 }
13944
13945 /* Combine LOC and BLOCK to a combined adhoc loc, retaining any range
13946 information. */
13947
13948 location_t
13949 set_block (location_t loc, tree block)
13950 {
13951 location_t pure_loc = get_pure_location (loc);
13952 source_range src_range = get_range_from_loc (line_table, loc);
13953 return COMBINE_LOCATION_DATA (line_table, pure_loc, src_range, block);
13954 }
13955
13956 location_t
13957 set_source_range (tree expr, location_t start, location_t finish)
13958 {
13959 source_range src_range;
13960 src_range.m_start = start;
13961 src_range.m_finish = finish;
13962 return set_source_range (expr, src_range);
13963 }
13964
13965 location_t
13966 set_source_range (tree expr, source_range src_range)
13967 {
13968 if (!EXPR_P (expr))
13969 return UNKNOWN_LOCATION;
13970
13971 location_t pure_loc = get_pure_location (EXPR_LOCATION (expr));
13972 location_t adhoc = COMBINE_LOCATION_DATA (line_table,
13973 pure_loc,
13974 src_range,
13975 NULL);
13976 SET_EXPR_LOCATION (expr, adhoc);
13977 return adhoc;
13978 }
13979
13980 /* Return EXPR, potentially wrapped with a node expression LOC,
13981 if !CAN_HAVE_LOCATION_P (expr).
13982
13983 NON_LVALUE_EXPR is used for wrapping constants, apart from STRING_CST.
13984 VIEW_CONVERT_EXPR is used for wrapping non-constants and STRING_CST.
13985
13986 Wrapper nodes can be identified using location_wrapper_p. */
13987
13988 tree
13989 maybe_wrap_with_location (tree expr, location_t loc)
13990 {
13991 if (expr == NULL)
13992 return NULL;
13993 if (loc == UNKNOWN_LOCATION)
13994 return expr;
13995 if (CAN_HAVE_LOCATION_P (expr))
13996 return expr;
13997 /* We should only be adding wrappers for constants and for decls,
13998 or for some exceptional tree nodes (e.g. BASELINK in the C++ FE). */
13999 gcc_assert (CONSTANT_CLASS_P (expr)
14000 || DECL_P (expr)
14001 || EXCEPTIONAL_CLASS_P (expr));
14002
14003 /* For now, don't add wrappers to exceptional tree nodes, to minimize
14004 any impact of the wrapper nodes. */
14005 if (EXCEPTIONAL_CLASS_P (expr))
14006 return expr;
14007
14008 /* Compiler-generated temporary variables don't need a wrapper. */
14009 if (DECL_P (expr) && DECL_ARTIFICIAL (expr) && DECL_IGNORED_P (expr))
14010 return expr;
14011
14012 /* If any auto_suppress_location_wrappers are active, don't create
14013 wrappers. */
14014 if (suppress_location_wrappers > 0)
14015 return expr;
14016
14017 tree_code code
14018 = (((CONSTANT_CLASS_P (expr) && TREE_CODE (expr) != STRING_CST)
14019 || (TREE_CODE (expr) == CONST_DECL && !TREE_STATIC (expr)))
14020 ? NON_LVALUE_EXPR : VIEW_CONVERT_EXPR);
14021 tree wrapper = build1_loc (loc, code, TREE_TYPE (expr), expr);
14022 /* Mark this node as being a wrapper. */
14023 EXPR_LOCATION_WRAPPER_P (wrapper) = 1;
14024 return wrapper;
14025 }
14026
14027 int suppress_location_wrappers;
14028
14029 /* Return the name of combined function FN, for debugging purposes. */
14030
14031 const char *
14032 combined_fn_name (combined_fn fn)
14033 {
14034 if (builtin_fn_p (fn))
14035 {
14036 tree fndecl = builtin_decl_explicit (as_builtin_fn (fn));
14037 return IDENTIFIER_POINTER (DECL_NAME (fndecl));
14038 }
14039 else
14040 return internal_fn_name (as_internal_fn (fn));
14041 }
14042
14043 /* Return a bitmap with a bit set corresponding to each argument in
14044 a function call type FNTYPE declared with attribute nonnull,
14045 or null if none of the function's argument are nonnull. The caller
14046 must free the bitmap. */
14047
14048 bitmap
14049 get_nonnull_args (const_tree fntype)
14050 {
14051 if (fntype == NULL_TREE)
14052 return NULL;
14053
14054 bitmap argmap = NULL;
14055 if (TREE_CODE (fntype) == METHOD_TYPE)
14056 {
14057 /* The this pointer in C++ non-static member functions is
14058 implicitly nonnull whether or not it's declared as such. */
14059 argmap = BITMAP_ALLOC (NULL);
14060 bitmap_set_bit (argmap, 0);
14061 }
14062
14063 tree attrs = TYPE_ATTRIBUTES (fntype);
14064 if (!attrs)
14065 return argmap;
14066
14067 /* A function declaration can specify multiple attribute nonnull,
14068 each with zero or more arguments. The loop below creates a bitmap
14069 representing a union of all the arguments. An empty (but non-null)
14070 bitmap means that all arguments have been declaraed nonnull. */
14071 for ( ; attrs; attrs = TREE_CHAIN (attrs))
14072 {
14073 attrs = lookup_attribute ("nonnull", attrs);
14074 if (!attrs)
14075 break;
14076
14077 if (!argmap)
14078 argmap = BITMAP_ALLOC (NULL);
14079
14080 if (!TREE_VALUE (attrs))
14081 {
14082 /* Clear the bitmap in case a previous attribute nonnull
14083 set it and this one overrides it for all arguments. */
14084 bitmap_clear (argmap);
14085 return argmap;
14086 }
14087
14088 /* Iterate over the indices of the format arguments declared nonnull
14089 and set a bit for each. */
14090 for (tree idx = TREE_VALUE (attrs); idx; idx = TREE_CHAIN (idx))
14091 {
14092 unsigned int val = TREE_INT_CST_LOW (TREE_VALUE (idx)) - 1;
14093 bitmap_set_bit (argmap, val);
14094 }
14095 }
14096
14097 return argmap;
14098 }
14099
14100 /* Returns true if TYPE is a type where it and all of its subobjects
14101 (recursively) are of structure, union, or array type. */
14102
14103 bool
14104 is_empty_type (const_tree type)
14105 {
14106 if (RECORD_OR_UNION_TYPE_P (type))
14107 {
14108 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
14109 if (TREE_CODE (field) == FIELD_DECL
14110 && !DECL_PADDING_P (field)
14111 && !is_empty_type (TREE_TYPE (field)))
14112 return false;
14113 return true;
14114 }
14115 else if (TREE_CODE (type) == ARRAY_TYPE)
14116 return (integer_minus_onep (array_type_nelts (type))
14117 || TYPE_DOMAIN (type) == NULL_TREE
14118 || is_empty_type (TREE_TYPE (type)));
14119 return false;
14120 }
14121
14122 /* Implement TARGET_EMPTY_RECORD_P. Return true if TYPE is an empty type
14123 that shouldn't be passed via stack. */
14124
14125 bool
14126 default_is_empty_record (const_tree type)
14127 {
14128 if (!abi_version_at_least (12))
14129 return false;
14130
14131 if (type == error_mark_node)
14132 return false;
14133
14134 if (TREE_ADDRESSABLE (type))
14135 return false;
14136
14137 return is_empty_type (TYPE_MAIN_VARIANT (type));
14138 }
14139
14140 /* Determine whether TYPE is a structure with a flexible array member,
14141 or a union containing such a structure (possibly recursively). */
14142
14143 bool
14144 flexible_array_type_p (const_tree type)
14145 {
14146 tree x, last;
14147 switch (TREE_CODE (type))
14148 {
14149 case RECORD_TYPE:
14150 last = NULL_TREE;
14151 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
14152 if (TREE_CODE (x) == FIELD_DECL)
14153 last = x;
14154 if (last == NULL_TREE)
14155 return false;
14156 if (TREE_CODE (TREE_TYPE (last)) == ARRAY_TYPE
14157 && TYPE_SIZE (TREE_TYPE (last)) == NULL_TREE
14158 && TYPE_DOMAIN (TREE_TYPE (last)) != NULL_TREE
14159 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (last))) == NULL_TREE)
14160 return true;
14161 return false;
14162 case UNION_TYPE:
14163 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
14164 {
14165 if (TREE_CODE (x) == FIELD_DECL
14166 && flexible_array_type_p (TREE_TYPE (x)))
14167 return true;
14168 }
14169 return false;
14170 default:
14171 return false;
14172 }
14173 }
14174
14175 /* Like int_size_in_bytes, but handle empty records specially. */
14176
14177 HOST_WIDE_INT
14178 arg_int_size_in_bytes (const_tree type)
14179 {
14180 return TYPE_EMPTY_P (type) ? 0 : int_size_in_bytes (type);
14181 }
14182
14183 /* Like size_in_bytes, but handle empty records specially. */
14184
14185 tree
14186 arg_size_in_bytes (const_tree type)
14187 {
14188 return TYPE_EMPTY_P (type) ? size_zero_node : size_in_bytes (type);
14189 }
14190
14191 /* Return true if an expression with CODE has to have the same result type as
14192 its first operand. */
14193
14194 bool
14195 expr_type_first_operand_type_p (tree_code code)
14196 {
14197 switch (code)
14198 {
14199 case NEGATE_EXPR:
14200 case ABS_EXPR:
14201 case BIT_NOT_EXPR:
14202 case PAREN_EXPR:
14203 case CONJ_EXPR:
14204
14205 case PLUS_EXPR:
14206 case MINUS_EXPR:
14207 case MULT_EXPR:
14208 case TRUNC_DIV_EXPR:
14209 case CEIL_DIV_EXPR:
14210 case FLOOR_DIV_EXPR:
14211 case ROUND_DIV_EXPR:
14212 case TRUNC_MOD_EXPR:
14213 case CEIL_MOD_EXPR:
14214 case FLOOR_MOD_EXPR:
14215 case ROUND_MOD_EXPR:
14216 case RDIV_EXPR:
14217 case EXACT_DIV_EXPR:
14218 case MIN_EXPR:
14219 case MAX_EXPR:
14220 case BIT_IOR_EXPR:
14221 case BIT_XOR_EXPR:
14222 case BIT_AND_EXPR:
14223
14224 case LSHIFT_EXPR:
14225 case RSHIFT_EXPR:
14226 case LROTATE_EXPR:
14227 case RROTATE_EXPR:
14228 return true;
14229
14230 default:
14231 return false;
14232 }
14233 }
14234
14235 /* Return a typenode for the "standard" C type with a given name. */
14236 tree
14237 get_typenode_from_name (const char *name)
14238 {
14239 if (name == NULL || *name == '\0')
14240 return NULL_TREE;
14241
14242 if (strcmp (name, "char") == 0)
14243 return char_type_node;
14244 if (strcmp (name, "unsigned char") == 0)
14245 return unsigned_char_type_node;
14246 if (strcmp (name, "signed char") == 0)
14247 return signed_char_type_node;
14248
14249 if (strcmp (name, "short int") == 0)
14250 return short_integer_type_node;
14251 if (strcmp (name, "short unsigned int") == 0)
14252 return short_unsigned_type_node;
14253
14254 if (strcmp (name, "int") == 0)
14255 return integer_type_node;
14256 if (strcmp (name, "unsigned int") == 0)
14257 return unsigned_type_node;
14258
14259 if (strcmp (name, "long int") == 0)
14260 return long_integer_type_node;
14261 if (strcmp (name, "long unsigned int") == 0)
14262 return long_unsigned_type_node;
14263
14264 if (strcmp (name, "long long int") == 0)
14265 return long_long_integer_type_node;
14266 if (strcmp (name, "long long unsigned int") == 0)
14267 return long_long_unsigned_type_node;
14268
14269 gcc_unreachable ();
14270 }
14271
14272 /* List of pointer types used to declare builtins before we have seen their
14273 real declaration.
14274
14275 Keep the size up to date in tree.h ! */
14276 const builtin_structptr_type builtin_structptr_types[6] =
14277 {
14278 { fileptr_type_node, ptr_type_node, "FILE" },
14279 { const_tm_ptr_type_node, const_ptr_type_node, "tm" },
14280 { fenv_t_ptr_type_node, ptr_type_node, "fenv_t" },
14281 { const_fenv_t_ptr_type_node, const_ptr_type_node, "fenv_t" },
14282 { fexcept_t_ptr_type_node, ptr_type_node, "fexcept_t" },
14283 { const_fexcept_t_ptr_type_node, const_ptr_type_node, "fexcept_t" }
14284 };
14285
14286 /* Return the maximum object size. */
14287
14288 tree
14289 max_object_size (void)
14290 {
14291 /* To do: Make this a configurable parameter. */
14292 return TYPE_MAX_VALUE (ptrdiff_type_node);
14293 }
14294
14295 /* A wrapper around TARGET_VERIFY_TYPE_CONTEXT that makes the silent_p
14296 parameter default to false and that weeds out error_mark_node. */
14297
14298 bool
14299 verify_type_context (location_t loc, type_context_kind context,
14300 const_tree type, bool silent_p)
14301 {
14302 if (type == error_mark_node)
14303 return true;
14304
14305 gcc_assert (TYPE_P (type));
14306 return (!targetm.verify_type_context
14307 || targetm.verify_type_context (loc, context, type, silent_p));
14308 }
14309
14310 /* Return that NEW_ASM and DELETE_ASM name a valid pair of new and
14311 delete operators. */
14312
14313 bool
14314 valid_new_delete_pair_p (tree new_asm, tree delete_asm)
14315 {
14316 const char *new_name = IDENTIFIER_POINTER (new_asm);
14317 const char *delete_name = IDENTIFIER_POINTER (delete_asm);
14318 unsigned int new_len = IDENTIFIER_LENGTH (new_asm);
14319 unsigned int delete_len = IDENTIFIER_LENGTH (delete_asm);
14320
14321 if (new_len < 5 || delete_len < 6)
14322 return false;
14323 if (new_name[0] == '_')
14324 ++new_name, --new_len;
14325 if (new_name[0] == '_')
14326 ++new_name, --new_len;
14327 if (delete_name[0] == '_')
14328 ++delete_name, --delete_len;
14329 if (delete_name[0] == '_')
14330 ++delete_name, --delete_len;
14331 if (new_len < 4 || delete_len < 5)
14332 return false;
14333 /* *_len is now just the length after initial underscores. */
14334 if (new_name[0] != 'Z' || new_name[1] != 'n')
14335 return false;
14336 if (delete_name[0] != 'Z' || delete_name[1] != 'd')
14337 return false;
14338 /* _Znw must match _Zdl, _Zna must match _Zda. */
14339 if ((new_name[2] != 'w' || delete_name[2] != 'l')
14340 && (new_name[2] != 'a' || delete_name[2] != 'a'))
14341 return false;
14342 /* 'j', 'm' and 'y' correspond to size_t. */
14343 if (new_name[3] != 'j' && new_name[3] != 'm' && new_name[3] != 'y')
14344 return false;
14345 if (delete_name[3] != 'P' || delete_name[4] != 'v')
14346 return false;
14347 if (new_len == 4
14348 || (new_len == 18 && !memcmp (new_name + 4, "RKSt9nothrow_t", 14)))
14349 {
14350 /* _ZnXY or _ZnXYRKSt9nothrow_t matches
14351 _ZdXPv, _ZdXPvY and _ZdXPvRKSt9nothrow_t. */
14352 if (delete_len == 5)
14353 return true;
14354 if (delete_len == 6 && delete_name[5] == new_name[3])
14355 return true;
14356 if (delete_len == 19 && !memcmp (delete_name + 5, "RKSt9nothrow_t", 14))
14357 return true;
14358 }
14359 else if ((new_len == 19 && !memcmp (new_name + 4, "St11align_val_t", 15))
14360 || (new_len == 33
14361 && !memcmp (new_name + 4, "St11align_val_tRKSt9nothrow_t", 29)))
14362 {
14363 /* _ZnXYSt11align_val_t or _ZnXYSt11align_val_tRKSt9nothrow_t matches
14364 _ZdXPvSt11align_val_t or _ZdXPvYSt11align_val_t or or
14365 _ZdXPvSt11align_val_tRKSt9nothrow_t. */
14366 if (delete_len == 20 && !memcmp (delete_name + 5, "St11align_val_t", 15))
14367 return true;
14368 if (delete_len == 21
14369 && delete_name[5] == new_name[3]
14370 && !memcmp (delete_name + 6, "St11align_val_t", 15))
14371 return true;
14372 if (delete_len == 34
14373 && !memcmp (delete_name + 5, "St11align_val_tRKSt9nothrow_t", 29))
14374 return true;
14375 }
14376 return false;
14377 }
14378
14379 #if CHECKING_P
14380
14381 namespace selftest {
14382
14383 /* Selftests for tree. */
14384
14385 /* Verify that integer constants are sane. */
14386
14387 static void
14388 test_integer_constants ()
14389 {
14390 ASSERT_TRUE (integer_type_node != NULL);
14391 ASSERT_TRUE (build_int_cst (integer_type_node, 0) != NULL);
14392
14393 tree type = integer_type_node;
14394
14395 tree zero = build_zero_cst (type);
14396 ASSERT_EQ (INTEGER_CST, TREE_CODE (zero));
14397 ASSERT_EQ (type, TREE_TYPE (zero));
14398
14399 tree one = build_int_cst (type, 1);
14400 ASSERT_EQ (INTEGER_CST, TREE_CODE (one));
14401 ASSERT_EQ (type, TREE_TYPE (zero));
14402 }
14403
14404 /* Verify identifiers. */
14405
14406 static void
14407 test_identifiers ()
14408 {
14409 tree identifier = get_identifier ("foo");
14410 ASSERT_EQ (3, IDENTIFIER_LENGTH (identifier));
14411 ASSERT_STREQ ("foo", IDENTIFIER_POINTER (identifier));
14412 }
14413
14414 /* Verify LABEL_DECL. */
14415
14416 static void
14417 test_labels ()
14418 {
14419 tree identifier = get_identifier ("err");
14420 tree label_decl = build_decl (UNKNOWN_LOCATION, LABEL_DECL,
14421 identifier, void_type_node);
14422 ASSERT_EQ (-1, LABEL_DECL_UID (label_decl));
14423 ASSERT_FALSE (FORCED_LABEL (label_decl));
14424 }
14425
14426 /* Return a new VECTOR_CST node whose type is TYPE and whose values
14427 are given by VALS. */
14428
14429 static tree
14430 build_vector (tree type, vec<tree> vals MEM_STAT_DECL)
14431 {
14432 gcc_assert (known_eq (vals.length (), TYPE_VECTOR_SUBPARTS (type)));
14433 tree_vector_builder builder (type, vals.length (), 1);
14434 builder.splice (vals);
14435 return builder.build ();
14436 }
14437
14438 /* Check that VECTOR_CST ACTUAL contains the elements in EXPECTED. */
14439
14440 static void
14441 check_vector_cst (vec<tree> expected, tree actual)
14442 {
14443 ASSERT_KNOWN_EQ (expected.length (),
14444 TYPE_VECTOR_SUBPARTS (TREE_TYPE (actual)));
14445 for (unsigned int i = 0; i < expected.length (); ++i)
14446 ASSERT_EQ (wi::to_wide (expected[i]),
14447 wi::to_wide (vector_cst_elt (actual, i)));
14448 }
14449
14450 /* Check that VECTOR_CST ACTUAL contains NPATTERNS duplicated elements,
14451 and that its elements match EXPECTED. */
14452
14453 static void
14454 check_vector_cst_duplicate (vec<tree> expected, tree actual,
14455 unsigned int npatterns)
14456 {
14457 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
14458 ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (actual));
14459 ASSERT_EQ (npatterns, vector_cst_encoded_nelts (actual));
14460 ASSERT_TRUE (VECTOR_CST_DUPLICATE_P (actual));
14461 ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
14462 check_vector_cst (expected, actual);
14463 }
14464
14465 /* Check that VECTOR_CST ACTUAL contains NPATTERNS foreground elements
14466 and NPATTERNS background elements, and that its elements match
14467 EXPECTED. */
14468
14469 static void
14470 check_vector_cst_fill (vec<tree> expected, tree actual,
14471 unsigned int npatterns)
14472 {
14473 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
14474 ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (actual));
14475 ASSERT_EQ (2 * npatterns, vector_cst_encoded_nelts (actual));
14476 ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
14477 ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
14478 check_vector_cst (expected, actual);
14479 }
14480
14481 /* Check that VECTOR_CST ACTUAL contains NPATTERNS stepped patterns,
14482 and that its elements match EXPECTED. */
14483
14484 static void
14485 check_vector_cst_stepped (vec<tree> expected, tree actual,
14486 unsigned int npatterns)
14487 {
14488 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
14489 ASSERT_EQ (3, VECTOR_CST_NELTS_PER_PATTERN (actual));
14490 ASSERT_EQ (3 * npatterns, vector_cst_encoded_nelts (actual));
14491 ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
14492 ASSERT_TRUE (VECTOR_CST_STEPPED_P (actual));
14493 check_vector_cst (expected, actual);
14494 }
14495
14496 /* Test the creation of VECTOR_CSTs. */
14497
14498 static void
14499 test_vector_cst_patterns (ALONE_CXX_MEM_STAT_INFO)
14500 {
14501 auto_vec<tree, 8> elements (8);
14502 elements.quick_grow (8);
14503 tree element_type = build_nonstandard_integer_type (16, true);
14504 tree vector_type = build_vector_type (element_type, 8);
14505
14506 /* Test a simple linear series with a base of 0 and a step of 1:
14507 { 0, 1, 2, 3, 4, 5, 6, 7 }. */
14508 for (unsigned int i = 0; i < 8; ++i)
14509 elements[i] = build_int_cst (element_type, i);
14510 tree vector = build_vector (vector_type, elements PASS_MEM_STAT);
14511 check_vector_cst_stepped (elements, vector, 1);
14512
14513 /* Try the same with the first element replaced by 100:
14514 { 100, 1, 2, 3, 4, 5, 6, 7 }. */
14515 elements[0] = build_int_cst (element_type, 100);
14516 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14517 check_vector_cst_stepped (elements, vector, 1);
14518
14519 /* Try a series that wraps around.
14520 { 100, 65531, 65532, 65533, 65534, 65535, 0, 1 }. */
14521 for (unsigned int i = 1; i < 8; ++i)
14522 elements[i] = build_int_cst (element_type, (65530 + i) & 0xffff);
14523 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14524 check_vector_cst_stepped (elements, vector, 1);
14525
14526 /* Try a downward series:
14527 { 100, 79, 78, 77, 76, 75, 75, 73 }. */
14528 for (unsigned int i = 1; i < 8; ++i)
14529 elements[i] = build_int_cst (element_type, 80 - i);
14530 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14531 check_vector_cst_stepped (elements, vector, 1);
14532
14533 /* Try two interleaved series with different bases and steps:
14534 { 100, 53, 66, 206, 62, 212, 58, 218 }. */
14535 elements[1] = build_int_cst (element_type, 53);
14536 for (unsigned int i = 2; i < 8; i += 2)
14537 {
14538 elements[i] = build_int_cst (element_type, 70 - i * 2);
14539 elements[i + 1] = build_int_cst (element_type, 200 + i * 3);
14540 }
14541 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14542 check_vector_cst_stepped (elements, vector, 2);
14543
14544 /* Try a duplicated value:
14545 { 100, 100, 100, 100, 100, 100, 100, 100 }. */
14546 for (unsigned int i = 1; i < 8; ++i)
14547 elements[i] = elements[0];
14548 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14549 check_vector_cst_duplicate (elements, vector, 1);
14550
14551 /* Try an interleaved duplicated value:
14552 { 100, 55, 100, 55, 100, 55, 100, 55 }. */
14553 elements[1] = build_int_cst (element_type, 55);
14554 for (unsigned int i = 2; i < 8; ++i)
14555 elements[i] = elements[i - 2];
14556 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14557 check_vector_cst_duplicate (elements, vector, 2);
14558
14559 /* Try a duplicated value with 2 exceptions
14560 { 41, 97, 100, 55, 100, 55, 100, 55 }. */
14561 elements[0] = build_int_cst (element_type, 41);
14562 elements[1] = build_int_cst (element_type, 97);
14563 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14564 check_vector_cst_fill (elements, vector, 2);
14565
14566 /* Try with and without a step
14567 { 41, 97, 100, 21, 100, 35, 100, 49 }. */
14568 for (unsigned int i = 3; i < 8; i += 2)
14569 elements[i] = build_int_cst (element_type, i * 7);
14570 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14571 check_vector_cst_stepped (elements, vector, 2);
14572
14573 /* Try a fully-general constant:
14574 { 41, 97, 100, 21, 100, 9990, 100, 49 }. */
14575 elements[5] = build_int_cst (element_type, 9990);
14576 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14577 check_vector_cst_fill (elements, vector, 4);
14578 }
14579
14580 /* Verify that STRIP_NOPS (NODE) is EXPECTED.
14581 Helper function for test_location_wrappers, to deal with STRIP_NOPS
14582 modifying its argument in-place. */
14583
14584 static void
14585 check_strip_nops (tree node, tree expected)
14586 {
14587 STRIP_NOPS (node);
14588 ASSERT_EQ (expected, node);
14589 }
14590
14591 /* Verify location wrappers. */
14592
14593 static void
14594 test_location_wrappers ()
14595 {
14596 location_t loc = BUILTINS_LOCATION;
14597
14598 ASSERT_EQ (NULL_TREE, maybe_wrap_with_location (NULL_TREE, loc));
14599
14600 /* Wrapping a constant. */
14601 tree int_cst = build_int_cst (integer_type_node, 42);
14602 ASSERT_FALSE (CAN_HAVE_LOCATION_P (int_cst));
14603 ASSERT_FALSE (location_wrapper_p (int_cst));
14604
14605 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
14606 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
14607 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_int_cst));
14608 ASSERT_EQ (int_cst, tree_strip_any_location_wrapper (wrapped_int_cst));
14609
14610 /* We shouldn't add wrapper nodes for UNKNOWN_LOCATION. */
14611 ASSERT_EQ (int_cst, maybe_wrap_with_location (int_cst, UNKNOWN_LOCATION));
14612
14613 /* We shouldn't add wrapper nodes for nodes that CAN_HAVE_LOCATION_P. */
14614 tree cast = build1 (NOP_EXPR, char_type_node, int_cst);
14615 ASSERT_TRUE (CAN_HAVE_LOCATION_P (cast));
14616 ASSERT_EQ (cast, maybe_wrap_with_location (cast, loc));
14617
14618 /* Wrapping a STRING_CST. */
14619 tree string_cst = build_string (4, "foo");
14620 ASSERT_FALSE (CAN_HAVE_LOCATION_P (string_cst));
14621 ASSERT_FALSE (location_wrapper_p (string_cst));
14622
14623 tree wrapped_string_cst = maybe_wrap_with_location (string_cst, loc);
14624 ASSERT_TRUE (location_wrapper_p (wrapped_string_cst));
14625 ASSERT_EQ (VIEW_CONVERT_EXPR, TREE_CODE (wrapped_string_cst));
14626 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_string_cst));
14627 ASSERT_EQ (string_cst, tree_strip_any_location_wrapper (wrapped_string_cst));
14628
14629
14630 /* Wrapping a variable. */
14631 tree int_var = build_decl (UNKNOWN_LOCATION, VAR_DECL,
14632 get_identifier ("some_int_var"),
14633 integer_type_node);
14634 ASSERT_FALSE (CAN_HAVE_LOCATION_P (int_var));
14635 ASSERT_FALSE (location_wrapper_p (int_var));
14636
14637 tree wrapped_int_var = maybe_wrap_with_location (int_var, loc);
14638 ASSERT_TRUE (location_wrapper_p (wrapped_int_var));
14639 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_int_var));
14640 ASSERT_EQ (int_var, tree_strip_any_location_wrapper (wrapped_int_var));
14641
14642 /* Verify that "reinterpret_cast<int>(some_int_var)" is not a location
14643 wrapper. */
14644 tree r_cast = build1 (NON_LVALUE_EXPR, integer_type_node, int_var);
14645 ASSERT_FALSE (location_wrapper_p (r_cast));
14646 ASSERT_EQ (r_cast, tree_strip_any_location_wrapper (r_cast));
14647
14648 /* Verify that STRIP_NOPS removes wrappers. */
14649 check_strip_nops (wrapped_int_cst, int_cst);
14650 check_strip_nops (wrapped_string_cst, string_cst);
14651 check_strip_nops (wrapped_int_var, int_var);
14652 }
14653
14654 /* Test various tree predicates. Verify that location wrappers don't
14655 affect the results. */
14656
14657 static void
14658 test_predicates ()
14659 {
14660 /* Build various constants and wrappers around them. */
14661
14662 location_t loc = BUILTINS_LOCATION;
14663
14664 tree i_0 = build_int_cst (integer_type_node, 0);
14665 tree wr_i_0 = maybe_wrap_with_location (i_0, loc);
14666
14667 tree i_1 = build_int_cst (integer_type_node, 1);
14668 tree wr_i_1 = maybe_wrap_with_location (i_1, loc);
14669
14670 tree i_m1 = build_int_cst (integer_type_node, -1);
14671 tree wr_i_m1 = maybe_wrap_with_location (i_m1, loc);
14672
14673 tree f_0 = build_real_from_int_cst (float_type_node, i_0);
14674 tree wr_f_0 = maybe_wrap_with_location (f_0, loc);
14675 tree f_1 = build_real_from_int_cst (float_type_node, i_1);
14676 tree wr_f_1 = maybe_wrap_with_location (f_1, loc);
14677 tree f_m1 = build_real_from_int_cst (float_type_node, i_m1);
14678 tree wr_f_m1 = maybe_wrap_with_location (f_m1, loc);
14679
14680 tree c_i_0 = build_complex (NULL_TREE, i_0, i_0);
14681 tree c_i_1 = build_complex (NULL_TREE, i_1, i_0);
14682 tree c_i_m1 = build_complex (NULL_TREE, i_m1, i_0);
14683
14684 tree c_f_0 = build_complex (NULL_TREE, f_0, f_0);
14685 tree c_f_1 = build_complex (NULL_TREE, f_1, f_0);
14686 tree c_f_m1 = build_complex (NULL_TREE, f_m1, f_0);
14687
14688 /* TODO: vector constants. */
14689
14690 /* Test integer_onep. */
14691 ASSERT_FALSE (integer_onep (i_0));
14692 ASSERT_FALSE (integer_onep (wr_i_0));
14693 ASSERT_TRUE (integer_onep (i_1));
14694 ASSERT_TRUE (integer_onep (wr_i_1));
14695 ASSERT_FALSE (integer_onep (i_m1));
14696 ASSERT_FALSE (integer_onep (wr_i_m1));
14697 ASSERT_FALSE (integer_onep (f_0));
14698 ASSERT_FALSE (integer_onep (wr_f_0));
14699 ASSERT_FALSE (integer_onep (f_1));
14700 ASSERT_FALSE (integer_onep (wr_f_1));
14701 ASSERT_FALSE (integer_onep (f_m1));
14702 ASSERT_FALSE (integer_onep (wr_f_m1));
14703 ASSERT_FALSE (integer_onep (c_i_0));
14704 ASSERT_TRUE (integer_onep (c_i_1));
14705 ASSERT_FALSE (integer_onep (c_i_m1));
14706 ASSERT_FALSE (integer_onep (c_f_0));
14707 ASSERT_FALSE (integer_onep (c_f_1));
14708 ASSERT_FALSE (integer_onep (c_f_m1));
14709
14710 /* Test integer_zerop. */
14711 ASSERT_TRUE (integer_zerop (i_0));
14712 ASSERT_TRUE (integer_zerop (wr_i_0));
14713 ASSERT_FALSE (integer_zerop (i_1));
14714 ASSERT_FALSE (integer_zerop (wr_i_1));
14715 ASSERT_FALSE (integer_zerop (i_m1));
14716 ASSERT_FALSE (integer_zerop (wr_i_m1));
14717 ASSERT_FALSE (integer_zerop (f_0));
14718 ASSERT_FALSE (integer_zerop (wr_f_0));
14719 ASSERT_FALSE (integer_zerop (f_1));
14720 ASSERT_FALSE (integer_zerop (wr_f_1));
14721 ASSERT_FALSE (integer_zerop (f_m1));
14722 ASSERT_FALSE (integer_zerop (wr_f_m1));
14723 ASSERT_TRUE (integer_zerop (c_i_0));
14724 ASSERT_FALSE (integer_zerop (c_i_1));
14725 ASSERT_FALSE (integer_zerop (c_i_m1));
14726 ASSERT_FALSE (integer_zerop (c_f_0));
14727 ASSERT_FALSE (integer_zerop (c_f_1));
14728 ASSERT_FALSE (integer_zerop (c_f_m1));
14729
14730 /* Test integer_all_onesp. */
14731 ASSERT_FALSE (integer_all_onesp (i_0));
14732 ASSERT_FALSE (integer_all_onesp (wr_i_0));
14733 ASSERT_FALSE (integer_all_onesp (i_1));
14734 ASSERT_FALSE (integer_all_onesp (wr_i_1));
14735 ASSERT_TRUE (integer_all_onesp (i_m1));
14736 ASSERT_TRUE (integer_all_onesp (wr_i_m1));
14737 ASSERT_FALSE (integer_all_onesp (f_0));
14738 ASSERT_FALSE (integer_all_onesp (wr_f_0));
14739 ASSERT_FALSE (integer_all_onesp (f_1));
14740 ASSERT_FALSE (integer_all_onesp (wr_f_1));
14741 ASSERT_FALSE (integer_all_onesp (f_m1));
14742 ASSERT_FALSE (integer_all_onesp (wr_f_m1));
14743 ASSERT_FALSE (integer_all_onesp (c_i_0));
14744 ASSERT_FALSE (integer_all_onesp (c_i_1));
14745 ASSERT_FALSE (integer_all_onesp (c_i_m1));
14746 ASSERT_FALSE (integer_all_onesp (c_f_0));
14747 ASSERT_FALSE (integer_all_onesp (c_f_1));
14748 ASSERT_FALSE (integer_all_onesp (c_f_m1));
14749
14750 /* Test integer_minus_onep. */
14751 ASSERT_FALSE (integer_minus_onep (i_0));
14752 ASSERT_FALSE (integer_minus_onep (wr_i_0));
14753 ASSERT_FALSE (integer_minus_onep (i_1));
14754 ASSERT_FALSE (integer_minus_onep (wr_i_1));
14755 ASSERT_TRUE (integer_minus_onep (i_m1));
14756 ASSERT_TRUE (integer_minus_onep (wr_i_m1));
14757 ASSERT_FALSE (integer_minus_onep (f_0));
14758 ASSERT_FALSE (integer_minus_onep (wr_f_0));
14759 ASSERT_FALSE (integer_minus_onep (f_1));
14760 ASSERT_FALSE (integer_minus_onep (wr_f_1));
14761 ASSERT_FALSE (integer_minus_onep (f_m1));
14762 ASSERT_FALSE (integer_minus_onep (wr_f_m1));
14763 ASSERT_FALSE (integer_minus_onep (c_i_0));
14764 ASSERT_FALSE (integer_minus_onep (c_i_1));
14765 ASSERT_TRUE (integer_minus_onep (c_i_m1));
14766 ASSERT_FALSE (integer_minus_onep (c_f_0));
14767 ASSERT_FALSE (integer_minus_onep (c_f_1));
14768 ASSERT_FALSE (integer_minus_onep (c_f_m1));
14769
14770 /* Test integer_each_onep. */
14771 ASSERT_FALSE (integer_each_onep (i_0));
14772 ASSERT_FALSE (integer_each_onep (wr_i_0));
14773 ASSERT_TRUE (integer_each_onep (i_1));
14774 ASSERT_TRUE (integer_each_onep (wr_i_1));
14775 ASSERT_FALSE (integer_each_onep (i_m1));
14776 ASSERT_FALSE (integer_each_onep (wr_i_m1));
14777 ASSERT_FALSE (integer_each_onep (f_0));
14778 ASSERT_FALSE (integer_each_onep (wr_f_0));
14779 ASSERT_FALSE (integer_each_onep (f_1));
14780 ASSERT_FALSE (integer_each_onep (wr_f_1));
14781 ASSERT_FALSE (integer_each_onep (f_m1));
14782 ASSERT_FALSE (integer_each_onep (wr_f_m1));
14783 ASSERT_FALSE (integer_each_onep (c_i_0));
14784 ASSERT_FALSE (integer_each_onep (c_i_1));
14785 ASSERT_FALSE (integer_each_onep (c_i_m1));
14786 ASSERT_FALSE (integer_each_onep (c_f_0));
14787 ASSERT_FALSE (integer_each_onep (c_f_1));
14788 ASSERT_FALSE (integer_each_onep (c_f_m1));
14789
14790 /* Test integer_truep. */
14791 ASSERT_FALSE (integer_truep (i_0));
14792 ASSERT_FALSE (integer_truep (wr_i_0));
14793 ASSERT_TRUE (integer_truep (i_1));
14794 ASSERT_TRUE (integer_truep (wr_i_1));
14795 ASSERT_FALSE (integer_truep (i_m1));
14796 ASSERT_FALSE (integer_truep (wr_i_m1));
14797 ASSERT_FALSE (integer_truep (f_0));
14798 ASSERT_FALSE (integer_truep (wr_f_0));
14799 ASSERT_FALSE (integer_truep (f_1));
14800 ASSERT_FALSE (integer_truep (wr_f_1));
14801 ASSERT_FALSE (integer_truep (f_m1));
14802 ASSERT_FALSE (integer_truep (wr_f_m1));
14803 ASSERT_FALSE (integer_truep (c_i_0));
14804 ASSERT_TRUE (integer_truep (c_i_1));
14805 ASSERT_FALSE (integer_truep (c_i_m1));
14806 ASSERT_FALSE (integer_truep (c_f_0));
14807 ASSERT_FALSE (integer_truep (c_f_1));
14808 ASSERT_FALSE (integer_truep (c_f_m1));
14809
14810 /* Test integer_nonzerop. */
14811 ASSERT_FALSE (integer_nonzerop (i_0));
14812 ASSERT_FALSE (integer_nonzerop (wr_i_0));
14813 ASSERT_TRUE (integer_nonzerop (i_1));
14814 ASSERT_TRUE (integer_nonzerop (wr_i_1));
14815 ASSERT_TRUE (integer_nonzerop (i_m1));
14816 ASSERT_TRUE (integer_nonzerop (wr_i_m1));
14817 ASSERT_FALSE (integer_nonzerop (f_0));
14818 ASSERT_FALSE (integer_nonzerop (wr_f_0));
14819 ASSERT_FALSE (integer_nonzerop (f_1));
14820 ASSERT_FALSE (integer_nonzerop (wr_f_1));
14821 ASSERT_FALSE (integer_nonzerop (f_m1));
14822 ASSERT_FALSE (integer_nonzerop (wr_f_m1));
14823 ASSERT_FALSE (integer_nonzerop (c_i_0));
14824 ASSERT_TRUE (integer_nonzerop (c_i_1));
14825 ASSERT_TRUE (integer_nonzerop (c_i_m1));
14826 ASSERT_FALSE (integer_nonzerop (c_f_0));
14827 ASSERT_FALSE (integer_nonzerop (c_f_1));
14828 ASSERT_FALSE (integer_nonzerop (c_f_m1));
14829
14830 /* Test real_zerop. */
14831 ASSERT_FALSE (real_zerop (i_0));
14832 ASSERT_FALSE (real_zerop (wr_i_0));
14833 ASSERT_FALSE (real_zerop (i_1));
14834 ASSERT_FALSE (real_zerop (wr_i_1));
14835 ASSERT_FALSE (real_zerop (i_m1));
14836 ASSERT_FALSE (real_zerop (wr_i_m1));
14837 ASSERT_TRUE (real_zerop (f_0));
14838 ASSERT_TRUE (real_zerop (wr_f_0));
14839 ASSERT_FALSE (real_zerop (f_1));
14840 ASSERT_FALSE (real_zerop (wr_f_1));
14841 ASSERT_FALSE (real_zerop (f_m1));
14842 ASSERT_FALSE (real_zerop (wr_f_m1));
14843 ASSERT_FALSE (real_zerop (c_i_0));
14844 ASSERT_FALSE (real_zerop (c_i_1));
14845 ASSERT_FALSE (real_zerop (c_i_m1));
14846 ASSERT_TRUE (real_zerop (c_f_0));
14847 ASSERT_FALSE (real_zerop (c_f_1));
14848 ASSERT_FALSE (real_zerop (c_f_m1));
14849
14850 /* Test real_onep. */
14851 ASSERT_FALSE (real_onep (i_0));
14852 ASSERT_FALSE (real_onep (wr_i_0));
14853 ASSERT_FALSE (real_onep (i_1));
14854 ASSERT_FALSE (real_onep (wr_i_1));
14855 ASSERT_FALSE (real_onep (i_m1));
14856 ASSERT_FALSE (real_onep (wr_i_m1));
14857 ASSERT_FALSE (real_onep (f_0));
14858 ASSERT_FALSE (real_onep (wr_f_0));
14859 ASSERT_TRUE (real_onep (f_1));
14860 ASSERT_TRUE (real_onep (wr_f_1));
14861 ASSERT_FALSE (real_onep (f_m1));
14862 ASSERT_FALSE (real_onep (wr_f_m1));
14863 ASSERT_FALSE (real_onep (c_i_0));
14864 ASSERT_FALSE (real_onep (c_i_1));
14865 ASSERT_FALSE (real_onep (c_i_m1));
14866 ASSERT_FALSE (real_onep (c_f_0));
14867 ASSERT_TRUE (real_onep (c_f_1));
14868 ASSERT_FALSE (real_onep (c_f_m1));
14869
14870 /* Test real_minus_onep. */
14871 ASSERT_FALSE (real_minus_onep (i_0));
14872 ASSERT_FALSE (real_minus_onep (wr_i_0));
14873 ASSERT_FALSE (real_minus_onep (i_1));
14874 ASSERT_FALSE (real_minus_onep (wr_i_1));
14875 ASSERT_FALSE (real_minus_onep (i_m1));
14876 ASSERT_FALSE (real_minus_onep (wr_i_m1));
14877 ASSERT_FALSE (real_minus_onep (f_0));
14878 ASSERT_FALSE (real_minus_onep (wr_f_0));
14879 ASSERT_FALSE (real_minus_onep (f_1));
14880 ASSERT_FALSE (real_minus_onep (wr_f_1));
14881 ASSERT_TRUE (real_minus_onep (f_m1));
14882 ASSERT_TRUE (real_minus_onep (wr_f_m1));
14883 ASSERT_FALSE (real_minus_onep (c_i_0));
14884 ASSERT_FALSE (real_minus_onep (c_i_1));
14885 ASSERT_FALSE (real_minus_onep (c_i_m1));
14886 ASSERT_FALSE (real_minus_onep (c_f_0));
14887 ASSERT_FALSE (real_minus_onep (c_f_1));
14888 ASSERT_TRUE (real_minus_onep (c_f_m1));
14889
14890 /* Test zerop. */
14891 ASSERT_TRUE (zerop (i_0));
14892 ASSERT_TRUE (zerop (wr_i_0));
14893 ASSERT_FALSE (zerop (i_1));
14894 ASSERT_FALSE (zerop (wr_i_1));
14895 ASSERT_FALSE (zerop (i_m1));
14896 ASSERT_FALSE (zerop (wr_i_m1));
14897 ASSERT_TRUE (zerop (f_0));
14898 ASSERT_TRUE (zerop (wr_f_0));
14899 ASSERT_FALSE (zerop (f_1));
14900 ASSERT_FALSE (zerop (wr_f_1));
14901 ASSERT_FALSE (zerop (f_m1));
14902 ASSERT_FALSE (zerop (wr_f_m1));
14903 ASSERT_TRUE (zerop (c_i_0));
14904 ASSERT_FALSE (zerop (c_i_1));
14905 ASSERT_FALSE (zerop (c_i_m1));
14906 ASSERT_TRUE (zerop (c_f_0));
14907 ASSERT_FALSE (zerop (c_f_1));
14908 ASSERT_FALSE (zerop (c_f_m1));
14909
14910 /* Test tree_expr_nonnegative_p. */
14911 ASSERT_TRUE (tree_expr_nonnegative_p (i_0));
14912 ASSERT_TRUE (tree_expr_nonnegative_p (wr_i_0));
14913 ASSERT_TRUE (tree_expr_nonnegative_p (i_1));
14914 ASSERT_TRUE (tree_expr_nonnegative_p (wr_i_1));
14915 ASSERT_FALSE (tree_expr_nonnegative_p (i_m1));
14916 ASSERT_FALSE (tree_expr_nonnegative_p (wr_i_m1));
14917 ASSERT_TRUE (tree_expr_nonnegative_p (f_0));
14918 ASSERT_TRUE (tree_expr_nonnegative_p (wr_f_0));
14919 ASSERT_TRUE (tree_expr_nonnegative_p (f_1));
14920 ASSERT_TRUE (tree_expr_nonnegative_p (wr_f_1));
14921 ASSERT_FALSE (tree_expr_nonnegative_p (f_m1));
14922 ASSERT_FALSE (tree_expr_nonnegative_p (wr_f_m1));
14923 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_0));
14924 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_1));
14925 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_m1));
14926 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_0));
14927 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_1));
14928 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_m1));
14929
14930 /* Test tree_expr_nonzero_p. */
14931 ASSERT_FALSE (tree_expr_nonzero_p (i_0));
14932 ASSERT_FALSE (tree_expr_nonzero_p (wr_i_0));
14933 ASSERT_TRUE (tree_expr_nonzero_p (i_1));
14934 ASSERT_TRUE (tree_expr_nonzero_p (wr_i_1));
14935 ASSERT_TRUE (tree_expr_nonzero_p (i_m1));
14936 ASSERT_TRUE (tree_expr_nonzero_p (wr_i_m1));
14937
14938 /* Test integer_valued_real_p. */
14939 ASSERT_FALSE (integer_valued_real_p (i_0));
14940 ASSERT_TRUE (integer_valued_real_p (f_0));
14941 ASSERT_TRUE (integer_valued_real_p (wr_f_0));
14942 ASSERT_TRUE (integer_valued_real_p (f_1));
14943 ASSERT_TRUE (integer_valued_real_p (wr_f_1));
14944
14945 /* Test integer_pow2p. */
14946 ASSERT_FALSE (integer_pow2p (i_0));
14947 ASSERT_TRUE (integer_pow2p (i_1));
14948 ASSERT_TRUE (integer_pow2p (wr_i_1));
14949
14950 /* Test uniform_integer_cst_p. */
14951 ASSERT_TRUE (uniform_integer_cst_p (i_0));
14952 ASSERT_TRUE (uniform_integer_cst_p (wr_i_0));
14953 ASSERT_TRUE (uniform_integer_cst_p (i_1));
14954 ASSERT_TRUE (uniform_integer_cst_p (wr_i_1));
14955 ASSERT_TRUE (uniform_integer_cst_p (i_m1));
14956 ASSERT_TRUE (uniform_integer_cst_p (wr_i_m1));
14957 ASSERT_FALSE (uniform_integer_cst_p (f_0));
14958 ASSERT_FALSE (uniform_integer_cst_p (wr_f_0));
14959 ASSERT_FALSE (uniform_integer_cst_p (f_1));
14960 ASSERT_FALSE (uniform_integer_cst_p (wr_f_1));
14961 ASSERT_FALSE (uniform_integer_cst_p (f_m1));
14962 ASSERT_FALSE (uniform_integer_cst_p (wr_f_m1));
14963 ASSERT_FALSE (uniform_integer_cst_p (c_i_0));
14964 ASSERT_FALSE (uniform_integer_cst_p (c_i_1));
14965 ASSERT_FALSE (uniform_integer_cst_p (c_i_m1));
14966 ASSERT_FALSE (uniform_integer_cst_p (c_f_0));
14967 ASSERT_FALSE (uniform_integer_cst_p (c_f_1));
14968 ASSERT_FALSE (uniform_integer_cst_p (c_f_m1));
14969 }
14970
14971 /* Check that string escaping works correctly. */
14972
14973 static void
14974 test_escaped_strings (void)
14975 {
14976 int saved_cutoff;
14977 escaped_string msg;
14978
14979 msg.escape (NULL);
14980 /* ASSERT_STREQ does not accept NULL as a valid test
14981 result, so we have to use ASSERT_EQ instead. */
14982 ASSERT_EQ (NULL, (const char *) msg);
14983
14984 msg.escape ("");
14985 ASSERT_STREQ ("", (const char *) msg);
14986
14987 msg.escape ("foobar");
14988 ASSERT_STREQ ("foobar", (const char *) msg);
14989
14990 /* Ensure that we have -fmessage-length set to 0. */
14991 saved_cutoff = pp_line_cutoff (global_dc->printer);
14992 pp_line_cutoff (global_dc->printer) = 0;
14993
14994 msg.escape ("foo\nbar");
14995 ASSERT_STREQ ("foo\\nbar", (const char *) msg);
14996
14997 msg.escape ("\a\b\f\n\r\t\v");
14998 ASSERT_STREQ ("\\a\\b\\f\\n\\r\\t\\v", (const char *) msg);
14999
15000 /* Now repeat the tests with -fmessage-length set to 5. */
15001 pp_line_cutoff (global_dc->printer) = 5;
15002
15003 /* Note that the newline is not translated into an escape. */
15004 msg.escape ("foo\nbar");
15005 ASSERT_STREQ ("foo\nbar", (const char *) msg);
15006
15007 msg.escape ("\a\b\f\n\r\t\v");
15008 ASSERT_STREQ ("\\a\\b\\f\n\\r\\t\\v", (const char *) msg);
15009
15010 /* Restore the original message length setting. */
15011 pp_line_cutoff (global_dc->printer) = saved_cutoff;
15012 }
15013
15014 /* Run all of the selftests within this file. */
15015
15016 void
15017 tree_c_tests ()
15018 {
15019 test_integer_constants ();
15020 test_identifiers ();
15021 test_labels ();
15022 test_vector_cst_patterns ();
15023 test_location_wrappers ();
15024 test_predicates ();
15025 test_escaped_strings ();
15026 }
15027
15028 } // namespace selftest
15029
15030 #endif /* CHECKING_P */
15031
15032 #include "gt-tree.h"