]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree.cc
Don't build readline/libreadline.a, when --with-system-readline is supplied
[thirdparty/gcc.git] / gcc / tree.cc
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2022 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 #include "gimple-range.h"
72 #include "gomp-constants.h"
73 #include "dfp.h"
74 #include "asan.h"
75 #include "ubsan.h"
76
77 /* Tree code classes. */
78
79 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
80 #define END_OF_BASE_TREE_CODES tcc_exceptional,
81
82 const enum tree_code_class tree_code_type[] = {
83 #include "all-tree.def"
84 };
85
86 #undef DEFTREECODE
87 #undef END_OF_BASE_TREE_CODES
88
89 /* Table indexed by tree code giving number of expression
90 operands beyond the fixed part of the node structure.
91 Not used for types or decls. */
92
93 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
94 #define END_OF_BASE_TREE_CODES 0,
95
96 const unsigned char tree_code_length[] = {
97 #include "all-tree.def"
98 };
99
100 #undef DEFTREECODE
101 #undef END_OF_BASE_TREE_CODES
102
103 /* Names of tree components.
104 Used for printing out the tree and error messages. */
105 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
106 #define END_OF_BASE_TREE_CODES "@dummy",
107
108 static const char *const tree_code_name[] = {
109 #include "all-tree.def"
110 };
111
112 #undef DEFTREECODE
113 #undef END_OF_BASE_TREE_CODES
114
115 /* Each tree code class has an associated string representation.
116 These must correspond to the tree_code_class entries. */
117
118 const char *const tree_code_class_strings[] =
119 {
120 "exceptional",
121 "constant",
122 "type",
123 "declaration",
124 "reference",
125 "comparison",
126 "unary",
127 "binary",
128 "statement",
129 "vl_exp",
130 "expression"
131 };
132
133 /* obstack.[ch] explicitly declined to prototype this. */
134 extern int _obstack_allocated_p (struct obstack *h, void *obj);
135
136 /* Statistics-gathering stuff. */
137
138 static uint64_t tree_code_counts[MAX_TREE_CODES];
139 uint64_t tree_node_counts[(int) all_kinds];
140 uint64_t tree_node_sizes[(int) all_kinds];
141
142 /* Keep in sync with tree.h:enum tree_node_kind. */
143 static const char * const tree_node_kind_names[] = {
144 "decls",
145 "types",
146 "blocks",
147 "stmts",
148 "refs",
149 "exprs",
150 "constants",
151 "identifiers",
152 "vecs",
153 "binfos",
154 "ssa names",
155 "constructors",
156 "random kinds",
157 "lang_decl kinds",
158 "lang_type kinds",
159 "omp clauses",
160 };
161
162 /* Unique id for next decl created. */
163 static GTY(()) int next_decl_uid;
164 /* Unique id for next type created. */
165 static GTY(()) unsigned next_type_uid = 1;
166 /* Unique id for next debug decl created. Use negative numbers,
167 to catch erroneous uses. */
168 static GTY(()) int next_debug_decl_uid;
169
170 /* Since we cannot rehash a type after it is in the table, we have to
171 keep the hash code. */
172
173 struct GTY((for_user)) type_hash {
174 unsigned long hash;
175 tree type;
176 };
177
178 /* Initial size of the hash table (rounded to next prime). */
179 #define TYPE_HASH_INITIAL_SIZE 1000
180
181 struct type_cache_hasher : ggc_cache_ptr_hash<type_hash>
182 {
183 static hashval_t hash (type_hash *t) { return t->hash; }
184 static bool equal (type_hash *a, type_hash *b);
185
186 static int
187 keep_cache_entry (type_hash *&t)
188 {
189 return ggc_marked_p (t->type);
190 }
191 };
192
193 /* Now here is the hash table. When recording a type, it is added to
194 the slot whose index is the hash code. Note that the hash table is
195 used for several kinds of types (function types, array types and
196 array index range types, for now). While all these live in the
197 same table, they are completely independent, and the hash code is
198 computed differently for each of these. */
199
200 static GTY ((cache)) hash_table<type_cache_hasher> *type_hash_table;
201
202 /* Hash table and temporary node for larger integer const values. */
203 static GTY (()) tree int_cst_node;
204
205 struct int_cst_hasher : ggc_cache_ptr_hash<tree_node>
206 {
207 static hashval_t hash (tree t);
208 static bool equal (tree x, tree y);
209 };
210
211 static GTY ((cache)) hash_table<int_cst_hasher> *int_cst_hash_table;
212
213 /* Class and variable for making sure that there is a single POLY_INT_CST
214 for a given value. */
215 struct poly_int_cst_hasher : ggc_cache_ptr_hash<tree_node>
216 {
217 typedef std::pair<tree, const poly_wide_int *> compare_type;
218 static hashval_t hash (tree t);
219 static bool equal (tree x, const compare_type &y);
220 };
221
222 static GTY ((cache)) hash_table<poly_int_cst_hasher> *poly_int_cst_hash_table;
223
224 /* Hash table for optimization flags and target option flags. Use the same
225 hash table for both sets of options. Nodes for building the current
226 optimization and target option nodes. The assumption is most of the time
227 the options created will already be in the hash table, so we avoid
228 allocating and freeing up a node repeatably. */
229 static GTY (()) tree cl_optimization_node;
230 static GTY (()) tree cl_target_option_node;
231
232 struct cl_option_hasher : ggc_cache_ptr_hash<tree_node>
233 {
234 static hashval_t hash (tree t);
235 static bool equal (tree x, tree y);
236 };
237
238 static GTY ((cache)) hash_table<cl_option_hasher> *cl_option_hash_table;
239
240 /* General tree->tree mapping structure for use in hash tables. */
241
242
243 static GTY ((cache))
244 hash_table<tree_decl_map_cache_hasher> *debug_expr_for_decl;
245
246 static GTY ((cache))
247 hash_table<tree_decl_map_cache_hasher> *value_expr_for_decl;
248
249 static GTY ((cache))
250 hash_table<tree_vec_map_cache_hasher> *debug_args_for_decl;
251
252 static void set_type_quals (tree, int);
253 static void print_type_hash_statistics (void);
254 static void print_debug_expr_statistics (void);
255 static void print_value_expr_statistics (void);
256
257 tree global_trees[TI_MAX];
258 tree integer_types[itk_none];
259
260 bool int_n_enabled_p[NUM_INT_N_ENTS];
261 struct int_n_trees_t int_n_trees [NUM_INT_N_ENTS];
262
263 bool tree_contains_struct[MAX_TREE_CODES][64];
264
265 /* Number of operands for each OMP clause. */
266 unsigned const char omp_clause_num_ops[] =
267 {
268 0, /* OMP_CLAUSE_ERROR */
269 1, /* OMP_CLAUSE_PRIVATE */
270 1, /* OMP_CLAUSE_SHARED */
271 1, /* OMP_CLAUSE_FIRSTPRIVATE */
272 2, /* OMP_CLAUSE_LASTPRIVATE */
273 5, /* OMP_CLAUSE_REDUCTION */
274 5, /* OMP_CLAUSE_TASK_REDUCTION */
275 5, /* OMP_CLAUSE_IN_REDUCTION */
276 1, /* OMP_CLAUSE_COPYIN */
277 1, /* OMP_CLAUSE_COPYPRIVATE */
278 3, /* OMP_CLAUSE_LINEAR */
279 1, /* OMP_CLAUSE_AFFINITY */
280 2, /* OMP_CLAUSE_ALIGNED */
281 3, /* OMP_CLAUSE_ALLOCATE */
282 1, /* OMP_CLAUSE_DEPEND */
283 1, /* OMP_CLAUSE_NONTEMPORAL */
284 1, /* OMP_CLAUSE_UNIFORM */
285 1, /* OMP_CLAUSE_ENTER */
286 1, /* OMP_CLAUSE_LINK */
287 1, /* OMP_CLAUSE_DETACH */
288 1, /* OMP_CLAUSE_USE_DEVICE_PTR */
289 1, /* OMP_CLAUSE_USE_DEVICE_ADDR */
290 1, /* OMP_CLAUSE_IS_DEVICE_PTR */
291 1, /* OMP_CLAUSE_INCLUSIVE */
292 1, /* OMP_CLAUSE_EXCLUSIVE */
293 2, /* OMP_CLAUSE_FROM */
294 2, /* OMP_CLAUSE_TO */
295 2, /* OMP_CLAUSE_MAP */
296 1, /* OMP_CLAUSE_HAS_DEVICE_ADDR */
297 1, /* OMP_CLAUSE_DOACROSS */
298 2, /* OMP_CLAUSE__CACHE_ */
299 2, /* OMP_CLAUSE_GANG */
300 1, /* OMP_CLAUSE_ASYNC */
301 1, /* OMP_CLAUSE_WAIT */
302 0, /* OMP_CLAUSE_AUTO */
303 0, /* OMP_CLAUSE_SEQ */
304 1, /* OMP_CLAUSE__LOOPTEMP_ */
305 1, /* OMP_CLAUSE__REDUCTEMP_ */
306 1, /* OMP_CLAUSE__CONDTEMP_ */
307 1, /* OMP_CLAUSE__SCANTEMP_ */
308 1, /* OMP_CLAUSE_IF */
309 1, /* OMP_CLAUSE_NUM_THREADS */
310 1, /* OMP_CLAUSE_SCHEDULE */
311 0, /* OMP_CLAUSE_NOWAIT */
312 1, /* OMP_CLAUSE_ORDERED */
313 0, /* OMP_CLAUSE_DEFAULT */
314 3, /* OMP_CLAUSE_COLLAPSE */
315 0, /* OMP_CLAUSE_UNTIED */
316 1, /* OMP_CLAUSE_FINAL */
317 0, /* OMP_CLAUSE_MERGEABLE */
318 1, /* OMP_CLAUSE_DEVICE */
319 1, /* OMP_CLAUSE_DIST_SCHEDULE */
320 0, /* OMP_CLAUSE_INBRANCH */
321 0, /* OMP_CLAUSE_NOTINBRANCH */
322 2, /* OMP_CLAUSE_NUM_TEAMS */
323 1, /* OMP_CLAUSE_THREAD_LIMIT */
324 0, /* OMP_CLAUSE_PROC_BIND */
325 1, /* OMP_CLAUSE_SAFELEN */
326 1, /* OMP_CLAUSE_SIMDLEN */
327 0, /* OMP_CLAUSE_DEVICE_TYPE */
328 0, /* OMP_CLAUSE_FOR */
329 0, /* OMP_CLAUSE_PARALLEL */
330 0, /* OMP_CLAUSE_SECTIONS */
331 0, /* OMP_CLAUSE_TASKGROUP */
332 1, /* OMP_CLAUSE_PRIORITY */
333 1, /* OMP_CLAUSE_GRAINSIZE */
334 1, /* OMP_CLAUSE_NUM_TASKS */
335 0, /* OMP_CLAUSE_NOGROUP */
336 0, /* OMP_CLAUSE_THREADS */
337 0, /* OMP_CLAUSE_SIMD */
338 1, /* OMP_CLAUSE_HINT */
339 0, /* OMP_CLAUSE_DEFAULTMAP */
340 0, /* OMP_CLAUSE_ORDER */
341 0, /* OMP_CLAUSE_BIND */
342 1, /* OMP_CLAUSE_FILTER */
343 1, /* OMP_CLAUSE__SIMDUID_ */
344 0, /* OMP_CLAUSE__SIMT_ */
345 0, /* OMP_CLAUSE_INDEPENDENT */
346 1, /* OMP_CLAUSE_WORKER */
347 1, /* OMP_CLAUSE_VECTOR */
348 1, /* OMP_CLAUSE_NUM_GANGS */
349 1, /* OMP_CLAUSE_NUM_WORKERS */
350 1, /* OMP_CLAUSE_VECTOR_LENGTH */
351 3, /* OMP_CLAUSE_TILE */
352 0, /* OMP_CLAUSE_IF_PRESENT */
353 0, /* OMP_CLAUSE_FINALIZE */
354 0, /* OMP_CLAUSE_NOHOST */
355 };
356
357 const char * const omp_clause_code_name[] =
358 {
359 "error_clause",
360 "private",
361 "shared",
362 "firstprivate",
363 "lastprivate",
364 "reduction",
365 "task_reduction",
366 "in_reduction",
367 "copyin",
368 "copyprivate",
369 "linear",
370 "affinity",
371 "aligned",
372 "allocate",
373 "depend",
374 "nontemporal",
375 "uniform",
376 "enter",
377 "link",
378 "detach",
379 "use_device_ptr",
380 "use_device_addr",
381 "is_device_ptr",
382 "inclusive",
383 "exclusive",
384 "from",
385 "to",
386 "map",
387 "has_device_addr",
388 "doacross",
389 "_cache_",
390 "gang",
391 "async",
392 "wait",
393 "auto",
394 "seq",
395 "_looptemp_",
396 "_reductemp_",
397 "_condtemp_",
398 "_scantemp_",
399 "if",
400 "num_threads",
401 "schedule",
402 "nowait",
403 "ordered",
404 "default",
405 "collapse",
406 "untied",
407 "final",
408 "mergeable",
409 "device",
410 "dist_schedule",
411 "inbranch",
412 "notinbranch",
413 "num_teams",
414 "thread_limit",
415 "proc_bind",
416 "safelen",
417 "simdlen",
418 "device_type",
419 "for",
420 "parallel",
421 "sections",
422 "taskgroup",
423 "priority",
424 "grainsize",
425 "num_tasks",
426 "nogroup",
427 "threads",
428 "simd",
429 "hint",
430 "defaultmap",
431 "order",
432 "bind",
433 "filter",
434 "_simduid_",
435 "_simt_",
436 "independent",
437 "worker",
438 "vector",
439 "num_gangs",
440 "num_workers",
441 "vector_length",
442 "tile",
443 "if_present",
444 "finalize",
445 "nohost",
446 };
447
448 /* Unless specific to OpenACC, we tend to internally maintain OpenMP-centric
449 clause names, but for use in diagnostics etc. would like to use the "user"
450 clause names. */
451
452 const char *
453 user_omp_clause_code_name (tree clause, bool oacc)
454 {
455 /* For OpenACC, the 'OMP_CLAUSE_MAP_KIND' of an 'OMP_CLAUSE_MAP' is used to
456 distinguish clauses as seen by the user. See also where front ends do
457 'build_omp_clause' with 'OMP_CLAUSE_MAP'. */
458 if (oacc && OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP)
459 switch (OMP_CLAUSE_MAP_KIND (clause))
460 {
461 case GOMP_MAP_FORCE_ALLOC:
462 case GOMP_MAP_ALLOC: return "create";
463 case GOMP_MAP_FORCE_TO:
464 case GOMP_MAP_TO: return "copyin";
465 case GOMP_MAP_FORCE_FROM:
466 case GOMP_MAP_FROM: return "copyout";
467 case GOMP_MAP_FORCE_TOFROM:
468 case GOMP_MAP_TOFROM: return "copy";
469 case GOMP_MAP_RELEASE: return "delete";
470 case GOMP_MAP_FORCE_PRESENT: return "present";
471 case GOMP_MAP_ATTACH: return "attach";
472 case GOMP_MAP_FORCE_DETACH:
473 case GOMP_MAP_DETACH: return "detach";
474 case GOMP_MAP_DEVICE_RESIDENT: return "device_resident";
475 case GOMP_MAP_LINK: return "link";
476 case GOMP_MAP_FORCE_DEVICEPTR: return "deviceptr";
477 default: break;
478 }
479
480 return omp_clause_code_name[OMP_CLAUSE_CODE (clause)];
481 }
482
483
484 /* Return the tree node structure used by tree code CODE. */
485
486 static inline enum tree_node_structure_enum
487 tree_node_structure_for_code (enum tree_code code)
488 {
489 switch (TREE_CODE_CLASS (code))
490 {
491 case tcc_declaration:
492 switch (code)
493 {
494 case CONST_DECL: return TS_CONST_DECL;
495 case DEBUG_EXPR_DECL: return TS_DECL_WRTL;
496 case FIELD_DECL: return TS_FIELD_DECL;
497 case FUNCTION_DECL: return TS_FUNCTION_DECL;
498 case LABEL_DECL: return TS_LABEL_DECL;
499 case PARM_DECL: return TS_PARM_DECL;
500 case RESULT_DECL: return TS_RESULT_DECL;
501 case TRANSLATION_UNIT_DECL: return TS_TRANSLATION_UNIT_DECL;
502 case TYPE_DECL: return TS_TYPE_DECL;
503 case VAR_DECL: return TS_VAR_DECL;
504 default: return TS_DECL_NON_COMMON;
505 }
506
507 case tcc_type: return TS_TYPE_NON_COMMON;
508
509 case tcc_binary:
510 case tcc_comparison:
511 case tcc_expression:
512 case tcc_reference:
513 case tcc_statement:
514 case tcc_unary:
515 case tcc_vl_exp: return TS_EXP;
516
517 default: /* tcc_constant and tcc_exceptional */
518 break;
519 }
520
521 switch (code)
522 {
523 /* tcc_constant cases. */
524 case COMPLEX_CST: return TS_COMPLEX;
525 case FIXED_CST: return TS_FIXED_CST;
526 case INTEGER_CST: return TS_INT_CST;
527 case POLY_INT_CST: return TS_POLY_INT_CST;
528 case REAL_CST: return TS_REAL_CST;
529 case STRING_CST: return TS_STRING;
530 case VECTOR_CST: return TS_VECTOR;
531 case VOID_CST: return TS_TYPED;
532
533 /* tcc_exceptional cases. */
534 case BLOCK: return TS_BLOCK;
535 case CONSTRUCTOR: return TS_CONSTRUCTOR;
536 case ERROR_MARK: return TS_COMMON;
537 case IDENTIFIER_NODE: return TS_IDENTIFIER;
538 case OMP_CLAUSE: return TS_OMP_CLAUSE;
539 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
540 case PLACEHOLDER_EXPR: return TS_COMMON;
541 case SSA_NAME: return TS_SSA_NAME;
542 case STATEMENT_LIST: return TS_STATEMENT_LIST;
543 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
544 case TREE_BINFO: return TS_BINFO;
545 case TREE_LIST: return TS_LIST;
546 case TREE_VEC: return TS_VEC;
547
548 default:
549 gcc_unreachable ();
550 }
551 }
552
553
554 /* Initialize tree_contains_struct to describe the hierarchy of tree
555 nodes. */
556
557 static void
558 initialize_tree_contains_struct (void)
559 {
560 unsigned i;
561
562 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
563 {
564 enum tree_code code;
565 enum tree_node_structure_enum ts_code;
566
567 code = (enum tree_code) i;
568 ts_code = tree_node_structure_for_code (code);
569
570 /* Mark the TS structure itself. */
571 tree_contains_struct[code][ts_code] = 1;
572
573 /* Mark all the structures that TS is derived from. */
574 switch (ts_code)
575 {
576 case TS_TYPED:
577 case TS_BLOCK:
578 case TS_OPTIMIZATION:
579 case TS_TARGET_OPTION:
580 MARK_TS_BASE (code);
581 break;
582
583 case TS_COMMON:
584 case TS_INT_CST:
585 case TS_POLY_INT_CST:
586 case TS_REAL_CST:
587 case TS_FIXED_CST:
588 case TS_VECTOR:
589 case TS_STRING:
590 case TS_COMPLEX:
591 case TS_SSA_NAME:
592 case TS_CONSTRUCTOR:
593 case TS_EXP:
594 case TS_STATEMENT_LIST:
595 MARK_TS_TYPED (code);
596 break;
597
598 case TS_IDENTIFIER:
599 case TS_DECL_MINIMAL:
600 case TS_TYPE_COMMON:
601 case TS_LIST:
602 case TS_VEC:
603 case TS_BINFO:
604 case TS_OMP_CLAUSE:
605 MARK_TS_COMMON (code);
606 break;
607
608 case TS_TYPE_WITH_LANG_SPECIFIC:
609 MARK_TS_TYPE_COMMON (code);
610 break;
611
612 case TS_TYPE_NON_COMMON:
613 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
614 break;
615
616 case TS_DECL_COMMON:
617 MARK_TS_DECL_MINIMAL (code);
618 break;
619
620 case TS_DECL_WRTL:
621 case TS_CONST_DECL:
622 MARK_TS_DECL_COMMON (code);
623 break;
624
625 case TS_DECL_NON_COMMON:
626 MARK_TS_DECL_WITH_VIS (code);
627 break;
628
629 case TS_DECL_WITH_VIS:
630 case TS_PARM_DECL:
631 case TS_LABEL_DECL:
632 case TS_RESULT_DECL:
633 MARK_TS_DECL_WRTL (code);
634 break;
635
636 case TS_FIELD_DECL:
637 MARK_TS_DECL_COMMON (code);
638 break;
639
640 case TS_VAR_DECL:
641 MARK_TS_DECL_WITH_VIS (code);
642 break;
643
644 case TS_TYPE_DECL:
645 case TS_FUNCTION_DECL:
646 MARK_TS_DECL_NON_COMMON (code);
647 break;
648
649 case TS_TRANSLATION_UNIT_DECL:
650 MARK_TS_DECL_COMMON (code);
651 break;
652
653 default:
654 gcc_unreachable ();
655 }
656 }
657
658 /* Basic consistency checks for attributes used in fold. */
659 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
660 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
661 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
662 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
663 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
664 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
665 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
666 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
667 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
668 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
669 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
670 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
671 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
672 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
673 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
674 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
675 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
676 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
677 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
678 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
679 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
680 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
681 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
682 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
683 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
684 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
685 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
686 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
687 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
688 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
689 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
690 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
691 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
692 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
693 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
694 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
695 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
696 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
697 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_MINIMAL]);
698 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_COMMON]);
699 }
700
701
702 /* Init tree.cc. */
703
704 void
705 init_ttree (void)
706 {
707 /* Initialize the hash table of types. */
708 type_hash_table
709 = hash_table<type_cache_hasher>::create_ggc (TYPE_HASH_INITIAL_SIZE);
710
711 debug_expr_for_decl
712 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
713
714 value_expr_for_decl
715 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
716
717 int_cst_hash_table = hash_table<int_cst_hasher>::create_ggc (1024);
718
719 poly_int_cst_hash_table = hash_table<poly_int_cst_hasher>::create_ggc (64);
720
721 int_cst_node = make_int_cst (1, 1);
722
723 cl_option_hash_table = hash_table<cl_option_hasher>::create_ggc (64);
724
725 cl_optimization_node = make_node (OPTIMIZATION_NODE);
726 cl_target_option_node = make_node (TARGET_OPTION_NODE);
727
728 /* Initialize the tree_contains_struct array. */
729 initialize_tree_contains_struct ();
730 lang_hooks.init_ts ();
731 }
732
733 \f
734 /* The name of the object as the assembler will see it (but before any
735 translations made by ASM_OUTPUT_LABELREF). Often this is the same
736 as DECL_NAME. It is an IDENTIFIER_NODE. */
737 tree
738 decl_assembler_name (tree decl)
739 {
740 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
741 lang_hooks.set_decl_assembler_name (decl);
742 return DECL_ASSEMBLER_NAME_RAW (decl);
743 }
744
745 /* The DECL_ASSEMBLER_NAME_RAW of DECL is being explicitly set to NAME
746 (either of which may be NULL). Inform the FE, if this changes the
747 name. */
748
749 void
750 overwrite_decl_assembler_name (tree decl, tree name)
751 {
752 if (DECL_ASSEMBLER_NAME_RAW (decl) != name)
753 lang_hooks.overwrite_decl_assembler_name (decl, name);
754 }
755
756 /* Return true if DECL may need an assembler name to be set. */
757
758 static inline bool
759 need_assembler_name_p (tree decl)
760 {
761 /* We use DECL_ASSEMBLER_NAME to hold mangled type names for One Definition
762 Rule merging. This makes type_odr_p to return true on those types during
763 LTO and by comparing the mangled name, we can say what types are intended
764 to be equivalent across compilation unit.
765
766 We do not store names of type_in_anonymous_namespace_p.
767
768 Record, union and enumeration type have linkage that allows use
769 to check type_in_anonymous_namespace_p. We do not mangle compound types
770 that always can be compared structurally.
771
772 Similarly for builtin types, we compare properties of their main variant.
773 A special case are integer types where mangling do make differences
774 between char/signed char/unsigned char etc. Storing name for these makes
775 e.g. -fno-signed-char/-fsigned-char mismatches to be handled well.
776 See cp/mangle.cc:write_builtin_type for details. */
777
778 if (TREE_CODE (decl) == TYPE_DECL)
779 {
780 if (DECL_NAME (decl)
781 && decl == TYPE_NAME (TREE_TYPE (decl))
782 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TREE_TYPE (decl)
783 && !TYPE_ARTIFICIAL (TREE_TYPE (decl))
784 && ((TREE_CODE (TREE_TYPE (decl)) != RECORD_TYPE
785 && TREE_CODE (TREE_TYPE (decl)) != UNION_TYPE)
786 || TYPE_CXX_ODR_P (TREE_TYPE (decl)))
787 && (type_with_linkage_p (TREE_TYPE (decl))
788 || TREE_CODE (TREE_TYPE (decl)) == INTEGER_TYPE)
789 && !variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
790 return !DECL_ASSEMBLER_NAME_SET_P (decl);
791 return false;
792 }
793 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
794 if (!VAR_OR_FUNCTION_DECL_P (decl))
795 return false;
796
797 /* If DECL already has its assembler name set, it does not need a
798 new one. */
799 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
800 || DECL_ASSEMBLER_NAME_SET_P (decl))
801 return false;
802
803 /* Abstract decls do not need an assembler name. */
804 if (DECL_ABSTRACT_P (decl))
805 return false;
806
807 /* For VAR_DECLs, only static, public and external symbols need an
808 assembler name. */
809 if (VAR_P (decl)
810 && !TREE_STATIC (decl)
811 && !TREE_PUBLIC (decl)
812 && !DECL_EXTERNAL (decl))
813 return false;
814
815 if (TREE_CODE (decl) == FUNCTION_DECL)
816 {
817 /* Do not set assembler name on builtins. Allow RTL expansion to
818 decide whether to expand inline or via a regular call. */
819 if (fndecl_built_in_p (decl)
820 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
821 return false;
822
823 /* Functions represented in the callgraph need an assembler name. */
824 if (cgraph_node::get (decl) != NULL)
825 return true;
826
827 /* Unused and not public functions don't need an assembler name. */
828 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
829 return false;
830 }
831
832 return true;
833 }
834
835 /* If T needs an assembler name, have one created for it. */
836
837 void
838 assign_assembler_name_if_needed (tree t)
839 {
840 if (need_assembler_name_p (t))
841 {
842 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
843 diagnostics that use input_location to show locus
844 information. The problem here is that, at this point,
845 input_location is generally anchored to the end of the file
846 (since the parser is long gone), so we don't have a good
847 position to pin it to.
848
849 To alleviate this problem, this uses the location of T's
850 declaration. Examples of this are
851 testsuite/g++.dg/template/cond2.C and
852 testsuite/g++.dg/template/pr35240.C. */
853 location_t saved_location = input_location;
854 input_location = DECL_SOURCE_LOCATION (t);
855
856 decl_assembler_name (t);
857
858 input_location = saved_location;
859 }
860 }
861
862 /* When the target supports COMDAT groups, this indicates which group the
863 DECL is associated with. This can be either an IDENTIFIER_NODE or a
864 decl, in which case its DECL_ASSEMBLER_NAME identifies the group. */
865 tree
866 decl_comdat_group (const_tree node)
867 {
868 struct symtab_node *snode = symtab_node::get (node);
869 if (!snode)
870 return NULL;
871 return snode->get_comdat_group ();
872 }
873
874 /* Likewise, but make sure it's been reduced to an IDENTIFIER_NODE. */
875 tree
876 decl_comdat_group_id (const_tree node)
877 {
878 struct symtab_node *snode = symtab_node::get (node);
879 if (!snode)
880 return NULL;
881 return snode->get_comdat_group_id ();
882 }
883
884 /* When the target supports named section, return its name as IDENTIFIER_NODE
885 or NULL if it is in no section. */
886 const char *
887 decl_section_name (const_tree node)
888 {
889 struct symtab_node *snode = symtab_node::get (node);
890 if (!snode)
891 return NULL;
892 return snode->get_section ();
893 }
894
895 /* Set section name of NODE to VALUE (that is expected to be
896 identifier node) */
897 void
898 set_decl_section_name (tree node, const char *value)
899 {
900 struct symtab_node *snode;
901
902 if (value == NULL)
903 {
904 snode = symtab_node::get (node);
905 if (!snode)
906 return;
907 }
908 else if (VAR_P (node))
909 snode = varpool_node::get_create (node);
910 else
911 snode = cgraph_node::get_create (node);
912 snode->set_section (value);
913 }
914
915 /* Set section name of NODE to match the section name of OTHER.
916
917 set_decl_section_name (decl, other) is equivalent to
918 set_decl_section_name (decl, DECL_SECTION_NAME (other)), but possibly more
919 efficient. */
920 void
921 set_decl_section_name (tree decl, const_tree other)
922 {
923 struct symtab_node *other_node = symtab_node::get (other);
924 if (other_node)
925 {
926 struct symtab_node *decl_node;
927 if (VAR_P (decl))
928 decl_node = varpool_node::get_create (decl);
929 else
930 decl_node = cgraph_node::get_create (decl);
931 decl_node->set_section (*other_node);
932 }
933 else
934 {
935 struct symtab_node *decl_node = symtab_node::get (decl);
936 if (!decl_node)
937 return;
938 decl_node->set_section (NULL);
939 }
940 }
941
942 /* Return TLS model of a variable NODE. */
943 enum tls_model
944 decl_tls_model (const_tree node)
945 {
946 struct varpool_node *snode = varpool_node::get (node);
947 if (!snode)
948 return TLS_MODEL_NONE;
949 return snode->tls_model;
950 }
951
952 /* Set TLS model of variable NODE to MODEL. */
953 void
954 set_decl_tls_model (tree node, enum tls_model model)
955 {
956 struct varpool_node *vnode;
957
958 if (model == TLS_MODEL_NONE)
959 {
960 vnode = varpool_node::get (node);
961 if (!vnode)
962 return;
963 }
964 else
965 vnode = varpool_node::get_create (node);
966 vnode->tls_model = model;
967 }
968
969 /* Compute the number of bytes occupied by a tree with code CODE.
970 This function cannot be used for nodes that have variable sizes,
971 including TREE_VEC, INTEGER_CST, STRING_CST, and CALL_EXPR. */
972 size_t
973 tree_code_size (enum tree_code code)
974 {
975 switch (TREE_CODE_CLASS (code))
976 {
977 case tcc_declaration: /* A decl node */
978 switch (code)
979 {
980 case FIELD_DECL: return sizeof (tree_field_decl);
981 case PARM_DECL: return sizeof (tree_parm_decl);
982 case VAR_DECL: return sizeof (tree_var_decl);
983 case LABEL_DECL: return sizeof (tree_label_decl);
984 case RESULT_DECL: return sizeof (tree_result_decl);
985 case CONST_DECL: return sizeof (tree_const_decl);
986 case TYPE_DECL: return sizeof (tree_type_decl);
987 case FUNCTION_DECL: return sizeof (tree_function_decl);
988 case DEBUG_EXPR_DECL: return sizeof (tree_decl_with_rtl);
989 case TRANSLATION_UNIT_DECL: return sizeof (tree_translation_unit_decl);
990 case NAMESPACE_DECL:
991 case IMPORTED_DECL:
992 case NAMELIST_DECL: return sizeof (tree_decl_non_common);
993 default:
994 gcc_checking_assert (code >= NUM_TREE_CODES);
995 return lang_hooks.tree_size (code);
996 }
997
998 case tcc_type: /* a type node */
999 switch (code)
1000 {
1001 case OFFSET_TYPE:
1002 case ENUMERAL_TYPE:
1003 case BOOLEAN_TYPE:
1004 case INTEGER_TYPE:
1005 case REAL_TYPE:
1006 case OPAQUE_TYPE:
1007 case POINTER_TYPE:
1008 case REFERENCE_TYPE:
1009 case NULLPTR_TYPE:
1010 case FIXED_POINT_TYPE:
1011 case COMPLEX_TYPE:
1012 case VECTOR_TYPE:
1013 case ARRAY_TYPE:
1014 case RECORD_TYPE:
1015 case UNION_TYPE:
1016 case QUAL_UNION_TYPE:
1017 case VOID_TYPE:
1018 case FUNCTION_TYPE:
1019 case METHOD_TYPE:
1020 case LANG_TYPE: return sizeof (tree_type_non_common);
1021 default:
1022 gcc_checking_assert (code >= NUM_TREE_CODES);
1023 return lang_hooks.tree_size (code);
1024 }
1025
1026 case tcc_reference: /* a reference */
1027 case tcc_expression: /* an expression */
1028 case tcc_statement: /* an expression with side effects */
1029 case tcc_comparison: /* a comparison expression */
1030 case tcc_unary: /* a unary arithmetic expression */
1031 case tcc_binary: /* a binary arithmetic expression */
1032 return (sizeof (struct tree_exp)
1033 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
1034
1035 case tcc_constant: /* a constant */
1036 switch (code)
1037 {
1038 case VOID_CST: return sizeof (tree_typed);
1039 case INTEGER_CST: gcc_unreachable ();
1040 case POLY_INT_CST: return sizeof (tree_poly_int_cst);
1041 case REAL_CST: return sizeof (tree_real_cst);
1042 case FIXED_CST: return sizeof (tree_fixed_cst);
1043 case COMPLEX_CST: return sizeof (tree_complex);
1044 case VECTOR_CST: gcc_unreachable ();
1045 case STRING_CST: gcc_unreachable ();
1046 default:
1047 gcc_checking_assert (code >= NUM_TREE_CODES);
1048 return lang_hooks.tree_size (code);
1049 }
1050
1051 case tcc_exceptional: /* something random, like an identifier. */
1052 switch (code)
1053 {
1054 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
1055 case TREE_LIST: return sizeof (tree_list);
1056
1057 case ERROR_MARK:
1058 case PLACEHOLDER_EXPR: return sizeof (tree_common);
1059
1060 case TREE_VEC: gcc_unreachable ();
1061 case OMP_CLAUSE: gcc_unreachable ();
1062
1063 case SSA_NAME: return sizeof (tree_ssa_name);
1064
1065 case STATEMENT_LIST: return sizeof (tree_statement_list);
1066 case BLOCK: return sizeof (struct tree_block);
1067 case CONSTRUCTOR: return sizeof (tree_constructor);
1068 case OPTIMIZATION_NODE: return sizeof (tree_optimization_option);
1069 case TARGET_OPTION_NODE: return sizeof (tree_target_option);
1070
1071 default:
1072 gcc_checking_assert (code >= NUM_TREE_CODES);
1073 return lang_hooks.tree_size (code);
1074 }
1075
1076 default:
1077 gcc_unreachable ();
1078 }
1079 }
1080
1081 /* Compute the number of bytes occupied by NODE. This routine only
1082 looks at TREE_CODE, except for those nodes that have variable sizes. */
1083 size_t
1084 tree_size (const_tree node)
1085 {
1086 const enum tree_code code = TREE_CODE (node);
1087 switch (code)
1088 {
1089 case INTEGER_CST:
1090 return (sizeof (struct tree_int_cst)
1091 + (TREE_INT_CST_EXT_NUNITS (node) - 1) * sizeof (HOST_WIDE_INT));
1092
1093 case TREE_BINFO:
1094 return (offsetof (struct tree_binfo, base_binfos)
1095 + vec<tree, va_gc>
1096 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
1097
1098 case TREE_VEC:
1099 return (sizeof (struct tree_vec)
1100 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
1101
1102 case VECTOR_CST:
1103 return (sizeof (struct tree_vector)
1104 + (vector_cst_encoded_nelts (node) - 1) * sizeof (tree));
1105
1106 case STRING_CST:
1107 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
1108
1109 case OMP_CLAUSE:
1110 return (sizeof (struct tree_omp_clause)
1111 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
1112 * sizeof (tree));
1113
1114 default:
1115 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
1116 return (sizeof (struct tree_exp)
1117 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
1118 else
1119 return tree_code_size (code);
1120 }
1121 }
1122
1123 /* Return tree node kind based on tree CODE. */
1124
1125 static tree_node_kind
1126 get_stats_node_kind (enum tree_code code)
1127 {
1128 enum tree_code_class type = TREE_CODE_CLASS (code);
1129
1130 switch (type)
1131 {
1132 case tcc_declaration: /* A decl node */
1133 return d_kind;
1134 case tcc_type: /* a type node */
1135 return t_kind;
1136 case tcc_statement: /* an expression with side effects */
1137 return s_kind;
1138 case tcc_reference: /* a reference */
1139 return r_kind;
1140 case tcc_expression: /* an expression */
1141 case tcc_comparison: /* a comparison expression */
1142 case tcc_unary: /* a unary arithmetic expression */
1143 case tcc_binary: /* a binary arithmetic expression */
1144 return e_kind;
1145 case tcc_constant: /* a constant */
1146 return c_kind;
1147 case tcc_exceptional: /* something random, like an identifier. */
1148 switch (code)
1149 {
1150 case IDENTIFIER_NODE:
1151 return id_kind;
1152 case TREE_VEC:
1153 return vec_kind;
1154 case TREE_BINFO:
1155 return binfo_kind;
1156 case SSA_NAME:
1157 return ssa_name_kind;
1158 case BLOCK:
1159 return b_kind;
1160 case CONSTRUCTOR:
1161 return constr_kind;
1162 case OMP_CLAUSE:
1163 return omp_clause_kind;
1164 default:
1165 return x_kind;
1166 }
1167 break;
1168 case tcc_vl_exp:
1169 return e_kind;
1170 default:
1171 gcc_unreachable ();
1172 }
1173 }
1174
1175 /* Record interesting allocation statistics for a tree node with CODE
1176 and LENGTH. */
1177
1178 static void
1179 record_node_allocation_statistics (enum tree_code code, size_t length)
1180 {
1181 if (!GATHER_STATISTICS)
1182 return;
1183
1184 tree_node_kind kind = get_stats_node_kind (code);
1185
1186 tree_code_counts[(int) code]++;
1187 tree_node_counts[(int) kind]++;
1188 tree_node_sizes[(int) kind] += length;
1189 }
1190
1191 /* Allocate and return a new UID from the DECL_UID namespace. */
1192
1193 int
1194 allocate_decl_uid (void)
1195 {
1196 return next_decl_uid++;
1197 }
1198
1199 /* Return a newly allocated node of code CODE. For decl and type
1200 nodes, some other fields are initialized. The rest of the node is
1201 initialized to zero. This function cannot be used for TREE_VEC,
1202 INTEGER_CST or OMP_CLAUSE nodes, which is enforced by asserts in
1203 tree_code_size.
1204
1205 Achoo! I got a code in the node. */
1206
1207 tree
1208 make_node (enum tree_code code MEM_STAT_DECL)
1209 {
1210 tree t;
1211 enum tree_code_class type = TREE_CODE_CLASS (code);
1212 size_t length = tree_code_size (code);
1213
1214 record_node_allocation_statistics (code, length);
1215
1216 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1217 TREE_SET_CODE (t, code);
1218
1219 switch (type)
1220 {
1221 case tcc_statement:
1222 if (code != DEBUG_BEGIN_STMT)
1223 TREE_SIDE_EFFECTS (t) = 1;
1224 break;
1225
1226 case tcc_declaration:
1227 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1228 {
1229 if (code == FUNCTION_DECL)
1230 {
1231 SET_DECL_ALIGN (t, FUNCTION_ALIGNMENT (FUNCTION_BOUNDARY));
1232 SET_DECL_MODE (t, FUNCTION_MODE);
1233 }
1234 else
1235 SET_DECL_ALIGN (t, 1);
1236 }
1237 DECL_SOURCE_LOCATION (t) = input_location;
1238 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
1239 DECL_UID (t) = --next_debug_decl_uid;
1240 else
1241 {
1242 DECL_UID (t) = allocate_decl_uid ();
1243 SET_DECL_PT_UID (t, -1);
1244 }
1245 if (TREE_CODE (t) == LABEL_DECL)
1246 LABEL_DECL_UID (t) = -1;
1247
1248 break;
1249
1250 case tcc_type:
1251 TYPE_UID (t) = next_type_uid++;
1252 SET_TYPE_ALIGN (t, BITS_PER_UNIT);
1253 TYPE_USER_ALIGN (t) = 0;
1254 TYPE_MAIN_VARIANT (t) = t;
1255 TYPE_CANONICAL (t) = t;
1256
1257 /* Default to no attributes for type, but let target change that. */
1258 TYPE_ATTRIBUTES (t) = NULL_TREE;
1259 targetm.set_default_type_attributes (t);
1260
1261 /* We have not yet computed the alias set for this type. */
1262 TYPE_ALIAS_SET (t) = -1;
1263 break;
1264
1265 case tcc_constant:
1266 TREE_CONSTANT (t) = 1;
1267 break;
1268
1269 case tcc_expression:
1270 switch (code)
1271 {
1272 case INIT_EXPR:
1273 case MODIFY_EXPR:
1274 case VA_ARG_EXPR:
1275 case PREDECREMENT_EXPR:
1276 case PREINCREMENT_EXPR:
1277 case POSTDECREMENT_EXPR:
1278 case POSTINCREMENT_EXPR:
1279 /* All of these have side-effects, no matter what their
1280 operands are. */
1281 TREE_SIDE_EFFECTS (t) = 1;
1282 break;
1283
1284 default:
1285 break;
1286 }
1287 break;
1288
1289 case tcc_exceptional:
1290 switch (code)
1291 {
1292 case TARGET_OPTION_NODE:
1293 TREE_TARGET_OPTION(t)
1294 = ggc_cleared_alloc<struct cl_target_option> ();
1295 break;
1296
1297 case OPTIMIZATION_NODE:
1298 TREE_OPTIMIZATION (t)
1299 = ggc_cleared_alloc<struct cl_optimization> ();
1300 break;
1301
1302 default:
1303 break;
1304 }
1305 break;
1306
1307 default:
1308 /* Other classes need no special treatment. */
1309 break;
1310 }
1311
1312 return t;
1313 }
1314
1315 /* Free tree node. */
1316
1317 void
1318 free_node (tree node)
1319 {
1320 enum tree_code code = TREE_CODE (node);
1321 if (GATHER_STATISTICS)
1322 {
1323 enum tree_node_kind kind = get_stats_node_kind (code);
1324
1325 gcc_checking_assert (tree_code_counts[(int) TREE_CODE (node)] != 0);
1326 gcc_checking_assert (tree_node_counts[(int) kind] != 0);
1327 gcc_checking_assert (tree_node_sizes[(int) kind] >= tree_size (node));
1328
1329 tree_code_counts[(int) TREE_CODE (node)]--;
1330 tree_node_counts[(int) kind]--;
1331 tree_node_sizes[(int) kind] -= tree_size (node);
1332 }
1333 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1334 vec_free (CONSTRUCTOR_ELTS (node));
1335 else if (code == BLOCK)
1336 vec_free (BLOCK_NONLOCALIZED_VARS (node));
1337 else if (code == TREE_BINFO)
1338 vec_free (BINFO_BASE_ACCESSES (node));
1339 else if (code == OPTIMIZATION_NODE)
1340 cl_optimization_option_free (TREE_OPTIMIZATION (node));
1341 else if (code == TARGET_OPTION_NODE)
1342 cl_target_option_free (TREE_TARGET_OPTION (node));
1343 ggc_free (node);
1344 }
1345 \f
1346 /* Return a new node with the same contents as NODE except that its
1347 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
1348
1349 tree
1350 copy_node (tree node MEM_STAT_DECL)
1351 {
1352 tree t;
1353 enum tree_code code = TREE_CODE (node);
1354 size_t length;
1355
1356 gcc_assert (code != STATEMENT_LIST);
1357
1358 length = tree_size (node);
1359 record_node_allocation_statistics (code, length);
1360 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1361 memcpy (t, node, length);
1362
1363 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
1364 TREE_CHAIN (t) = 0;
1365 TREE_ASM_WRITTEN (t) = 0;
1366 TREE_VISITED (t) = 0;
1367
1368 if (TREE_CODE_CLASS (code) == tcc_declaration)
1369 {
1370 if (code == DEBUG_EXPR_DECL)
1371 DECL_UID (t) = --next_debug_decl_uid;
1372 else
1373 {
1374 DECL_UID (t) = allocate_decl_uid ();
1375 if (DECL_PT_UID_SET_P (node))
1376 SET_DECL_PT_UID (t, DECL_PT_UID (node));
1377 }
1378 if ((TREE_CODE (node) == PARM_DECL || VAR_P (node))
1379 && DECL_HAS_VALUE_EXPR_P (node))
1380 {
1381 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
1382 DECL_HAS_VALUE_EXPR_P (t) = 1;
1383 }
1384 /* DECL_DEBUG_EXPR is copied explicitly by callers. */
1385 if (VAR_P (node))
1386 {
1387 DECL_HAS_DEBUG_EXPR_P (t) = 0;
1388 t->decl_with_vis.symtab_node = NULL;
1389 }
1390 if (VAR_P (node) && DECL_HAS_INIT_PRIORITY_P (node))
1391 {
1392 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
1393 DECL_HAS_INIT_PRIORITY_P (t) = 1;
1394 }
1395 if (TREE_CODE (node) == FUNCTION_DECL)
1396 {
1397 DECL_STRUCT_FUNCTION (t) = NULL;
1398 t->decl_with_vis.symtab_node = NULL;
1399 }
1400 }
1401 else if (TREE_CODE_CLASS (code) == tcc_type)
1402 {
1403 TYPE_UID (t) = next_type_uid++;
1404 /* The following is so that the debug code for
1405 the copy is different from the original type.
1406 The two statements usually duplicate each other
1407 (because they clear fields of the same union),
1408 but the optimizer should catch that. */
1409 TYPE_SYMTAB_ADDRESS (t) = 0;
1410 TYPE_SYMTAB_DIE (t) = 0;
1411
1412 /* Do not copy the values cache. */
1413 if (TYPE_CACHED_VALUES_P (t))
1414 {
1415 TYPE_CACHED_VALUES_P (t) = 0;
1416 TYPE_CACHED_VALUES (t) = NULL_TREE;
1417 }
1418 }
1419 else if (code == TARGET_OPTION_NODE)
1420 {
1421 TREE_TARGET_OPTION (t) = ggc_alloc<struct cl_target_option>();
1422 memcpy (TREE_TARGET_OPTION (t), TREE_TARGET_OPTION (node),
1423 sizeof (struct cl_target_option));
1424 }
1425 else if (code == OPTIMIZATION_NODE)
1426 {
1427 TREE_OPTIMIZATION (t) = ggc_alloc<struct cl_optimization>();
1428 memcpy (TREE_OPTIMIZATION (t), TREE_OPTIMIZATION (node),
1429 sizeof (struct cl_optimization));
1430 }
1431
1432 return t;
1433 }
1434
1435 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1436 For example, this can copy a list made of TREE_LIST nodes. */
1437
1438 tree
1439 copy_list (tree list)
1440 {
1441 tree head;
1442 tree prev, next;
1443
1444 if (list == 0)
1445 return 0;
1446
1447 head = prev = copy_node (list);
1448 next = TREE_CHAIN (list);
1449 while (next)
1450 {
1451 TREE_CHAIN (prev) = copy_node (next);
1452 prev = TREE_CHAIN (prev);
1453 next = TREE_CHAIN (next);
1454 }
1455 return head;
1456 }
1457
1458 \f
1459 /* Return the value that TREE_INT_CST_EXT_NUNITS should have for an
1460 INTEGER_CST with value CST and type TYPE. */
1461
1462 static unsigned int
1463 get_int_cst_ext_nunits (tree type, const wide_int &cst)
1464 {
1465 gcc_checking_assert (cst.get_precision () == TYPE_PRECISION (type));
1466 /* We need extra HWIs if CST is an unsigned integer with its
1467 upper bit set. */
1468 if (TYPE_UNSIGNED (type) && wi::neg_p (cst))
1469 return cst.get_precision () / HOST_BITS_PER_WIDE_INT + 1;
1470 return cst.get_len ();
1471 }
1472
1473 /* Return a new INTEGER_CST with value CST and type TYPE. */
1474
1475 static tree
1476 build_new_int_cst (tree type, const wide_int &cst)
1477 {
1478 unsigned int len = cst.get_len ();
1479 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1480 tree nt = make_int_cst (len, ext_len);
1481
1482 if (len < ext_len)
1483 {
1484 --ext_len;
1485 TREE_INT_CST_ELT (nt, ext_len)
1486 = zext_hwi (-1, cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1487 for (unsigned int i = len; i < ext_len; ++i)
1488 TREE_INT_CST_ELT (nt, i) = -1;
1489 }
1490 else if (TYPE_UNSIGNED (type)
1491 && cst.get_precision () < len * HOST_BITS_PER_WIDE_INT)
1492 {
1493 len--;
1494 TREE_INT_CST_ELT (nt, len)
1495 = zext_hwi (cst.elt (len),
1496 cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1497 }
1498
1499 for (unsigned int i = 0; i < len; i++)
1500 TREE_INT_CST_ELT (nt, i) = cst.elt (i);
1501 TREE_TYPE (nt) = type;
1502 return nt;
1503 }
1504
1505 /* Return a new POLY_INT_CST with coefficients COEFFS and type TYPE. */
1506
1507 static tree
1508 build_new_poly_int_cst (tree type, tree (&coeffs)[NUM_POLY_INT_COEFFS]
1509 CXX_MEM_STAT_INFO)
1510 {
1511 size_t length = sizeof (struct tree_poly_int_cst);
1512 record_node_allocation_statistics (POLY_INT_CST, length);
1513
1514 tree t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1515
1516 TREE_SET_CODE (t, POLY_INT_CST);
1517 TREE_CONSTANT (t) = 1;
1518 TREE_TYPE (t) = type;
1519 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1520 POLY_INT_CST_COEFF (t, i) = coeffs[i];
1521 return t;
1522 }
1523
1524 /* Create a constant tree that contains CST sign-extended to TYPE. */
1525
1526 tree
1527 build_int_cst (tree type, poly_int64 cst)
1528 {
1529 /* Support legacy code. */
1530 if (!type)
1531 type = integer_type_node;
1532
1533 return wide_int_to_tree (type, wi::shwi (cst, TYPE_PRECISION (type)));
1534 }
1535
1536 /* Create a constant tree that contains CST zero-extended to TYPE. */
1537
1538 tree
1539 build_int_cstu (tree type, poly_uint64 cst)
1540 {
1541 return wide_int_to_tree (type, wi::uhwi (cst, TYPE_PRECISION (type)));
1542 }
1543
1544 /* Create a constant tree that contains CST sign-extended to TYPE. */
1545
1546 tree
1547 build_int_cst_type (tree type, poly_int64 cst)
1548 {
1549 gcc_assert (type);
1550 return wide_int_to_tree (type, wi::shwi (cst, TYPE_PRECISION (type)));
1551 }
1552
1553 /* Constructs tree in type TYPE from with value given by CST. Signedness
1554 of CST is assumed to be the same as the signedness of TYPE. */
1555
1556 tree
1557 double_int_to_tree (tree type, double_int cst)
1558 {
1559 return wide_int_to_tree (type, widest_int::from (cst, TYPE_SIGN (type)));
1560 }
1561
1562 /* We force the wide_int CST to the range of the type TYPE by sign or
1563 zero extending it. OVERFLOWABLE indicates if we are interested in
1564 overflow of the value, when >0 we are only interested in signed
1565 overflow, for <0 we are interested in any overflow. OVERFLOWED
1566 indicates whether overflow has already occurred. CONST_OVERFLOWED
1567 indicates whether constant overflow has already occurred. We force
1568 T's value to be within range of T's type (by setting to 0 or 1 all
1569 the bits outside the type's range). We set TREE_OVERFLOWED if,
1570 OVERFLOWED is nonzero,
1571 or OVERFLOWABLE is >0 and signed overflow occurs
1572 or OVERFLOWABLE is <0 and any overflow occurs
1573 We return a new tree node for the extended wide_int. The node
1574 is shared if no overflow flags are set. */
1575
1576
1577 tree
1578 force_fit_type (tree type, const poly_wide_int_ref &cst,
1579 int overflowable, bool overflowed)
1580 {
1581 signop sign = TYPE_SIGN (type);
1582
1583 /* If we need to set overflow flags, return a new unshared node. */
1584 if (overflowed || !wi::fits_to_tree_p (cst, type))
1585 {
1586 if (overflowed
1587 || overflowable < 0
1588 || (overflowable > 0 && sign == SIGNED))
1589 {
1590 poly_wide_int tmp = poly_wide_int::from (cst, TYPE_PRECISION (type),
1591 sign);
1592 tree t;
1593 if (tmp.is_constant ())
1594 t = build_new_int_cst (type, tmp.coeffs[0]);
1595 else
1596 {
1597 tree coeffs[NUM_POLY_INT_COEFFS];
1598 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1599 {
1600 coeffs[i] = build_new_int_cst (type, tmp.coeffs[i]);
1601 TREE_OVERFLOW (coeffs[i]) = 1;
1602 }
1603 t = build_new_poly_int_cst (type, coeffs);
1604 }
1605 TREE_OVERFLOW (t) = 1;
1606 return t;
1607 }
1608 }
1609
1610 /* Else build a shared node. */
1611 return wide_int_to_tree (type, cst);
1612 }
1613
1614 /* These are the hash table functions for the hash table of INTEGER_CST
1615 nodes of a sizetype. */
1616
1617 /* Return the hash code X, an INTEGER_CST. */
1618
1619 hashval_t
1620 int_cst_hasher::hash (tree x)
1621 {
1622 const_tree const t = x;
1623 hashval_t code = TYPE_UID (TREE_TYPE (t));
1624 int i;
1625
1626 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
1627 code = iterative_hash_host_wide_int (TREE_INT_CST_ELT(t, i), code);
1628
1629 return code;
1630 }
1631
1632 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1633 is the same as that given by *Y, which is the same. */
1634
1635 bool
1636 int_cst_hasher::equal (tree x, tree y)
1637 {
1638 const_tree const xt = x;
1639 const_tree const yt = y;
1640
1641 if (TREE_TYPE (xt) != TREE_TYPE (yt)
1642 || TREE_INT_CST_NUNITS (xt) != TREE_INT_CST_NUNITS (yt)
1643 || TREE_INT_CST_EXT_NUNITS (xt) != TREE_INT_CST_EXT_NUNITS (yt))
1644 return false;
1645
1646 for (int i = 0; i < TREE_INT_CST_NUNITS (xt); i++)
1647 if (TREE_INT_CST_ELT (xt, i) != TREE_INT_CST_ELT (yt, i))
1648 return false;
1649
1650 return true;
1651 }
1652
1653 /* Cache wide_int CST into the TYPE_CACHED_VALUES cache for TYPE.
1654 SLOT is the slot entry to store it in, and MAX_SLOTS is the maximum
1655 number of slots that can be cached for the type. */
1656
1657 static inline tree
1658 cache_wide_int_in_type_cache (tree type, const wide_int &cst,
1659 int slot, int max_slots)
1660 {
1661 gcc_checking_assert (slot >= 0);
1662 /* Initialize cache. */
1663 if (!TYPE_CACHED_VALUES_P (type))
1664 {
1665 TYPE_CACHED_VALUES_P (type) = 1;
1666 TYPE_CACHED_VALUES (type) = make_tree_vec (max_slots);
1667 }
1668 tree t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), slot);
1669 if (!t)
1670 {
1671 /* Create a new shared int. */
1672 t = build_new_int_cst (type, cst);
1673 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), slot) = t;
1674 }
1675 return t;
1676 }
1677
1678 /* Create an INT_CST node of TYPE and value CST.
1679 The returned node is always shared. For small integers we use a
1680 per-type vector cache, for larger ones we use a single hash table.
1681 The value is extended from its precision according to the sign of
1682 the type to be a multiple of HOST_BITS_PER_WIDE_INT. This defines
1683 the upper bits and ensures that hashing and value equality based
1684 upon the underlying HOST_WIDE_INTs works without masking. */
1685
1686 static tree
1687 wide_int_to_tree_1 (tree type, const wide_int_ref &pcst)
1688 {
1689 tree t;
1690 int ix = -1;
1691 int limit = 0;
1692
1693 gcc_assert (type);
1694 unsigned int prec = TYPE_PRECISION (type);
1695 signop sgn = TYPE_SIGN (type);
1696
1697 /* Verify that everything is canonical. */
1698 int l = pcst.get_len ();
1699 if (l > 1)
1700 {
1701 if (pcst.elt (l - 1) == 0)
1702 gcc_checking_assert (pcst.elt (l - 2) < 0);
1703 if (pcst.elt (l - 1) == HOST_WIDE_INT_M1)
1704 gcc_checking_assert (pcst.elt (l - 2) >= 0);
1705 }
1706
1707 wide_int cst = wide_int::from (pcst, prec, sgn);
1708 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1709
1710 enum tree_code code = TREE_CODE (type);
1711 if (code == POINTER_TYPE || code == REFERENCE_TYPE)
1712 {
1713 /* Cache NULL pointer and zero bounds. */
1714 if (cst == 0)
1715 ix = 0;
1716 /* Cache upper bounds of pointers. */
1717 else if (cst == wi::max_value (prec, sgn))
1718 ix = 1;
1719 /* Cache 1 which is used for a non-zero range. */
1720 else if (cst == 1)
1721 ix = 2;
1722
1723 if (ix >= 0)
1724 {
1725 t = cache_wide_int_in_type_cache (type, cst, ix, 3);
1726 /* Make sure no one is clobbering the shared constant. */
1727 gcc_checking_assert (TREE_TYPE (t) == type
1728 && cst == wi::to_wide (t));
1729 return t;
1730 }
1731 }
1732 if (ext_len == 1)
1733 {
1734 /* We just need to store a single HOST_WIDE_INT. */
1735 HOST_WIDE_INT hwi;
1736 if (TYPE_UNSIGNED (type))
1737 hwi = cst.to_uhwi ();
1738 else
1739 hwi = cst.to_shwi ();
1740
1741 switch (code)
1742 {
1743 case NULLPTR_TYPE:
1744 gcc_assert (hwi == 0);
1745 /* Fallthru. */
1746
1747 case POINTER_TYPE:
1748 case REFERENCE_TYPE:
1749 /* Ignore pointers, as they were already handled above. */
1750 break;
1751
1752 case BOOLEAN_TYPE:
1753 /* Cache false or true. */
1754 limit = 2;
1755 if (IN_RANGE (hwi, 0, 1))
1756 ix = hwi;
1757 break;
1758
1759 case INTEGER_TYPE:
1760 case OFFSET_TYPE:
1761 if (TYPE_SIGN (type) == UNSIGNED)
1762 {
1763 /* Cache [0, N). */
1764 limit = param_integer_share_limit;
1765 if (IN_RANGE (hwi, 0, param_integer_share_limit - 1))
1766 ix = hwi;
1767 }
1768 else
1769 {
1770 /* Cache [-1, N). */
1771 limit = param_integer_share_limit + 1;
1772 if (IN_RANGE (hwi, -1, param_integer_share_limit - 1))
1773 ix = hwi + 1;
1774 }
1775 break;
1776
1777 case ENUMERAL_TYPE:
1778 break;
1779
1780 default:
1781 gcc_unreachable ();
1782 }
1783
1784 if (ix >= 0)
1785 {
1786 t = cache_wide_int_in_type_cache (type, cst, ix, limit);
1787 /* Make sure no one is clobbering the shared constant. */
1788 gcc_checking_assert (TREE_TYPE (t) == type
1789 && TREE_INT_CST_NUNITS (t) == 1
1790 && TREE_INT_CST_OFFSET_NUNITS (t) == 1
1791 && TREE_INT_CST_EXT_NUNITS (t) == 1
1792 && TREE_INT_CST_ELT (t, 0) == hwi);
1793 return t;
1794 }
1795 else
1796 {
1797 /* Use the cache of larger shared ints, using int_cst_node as
1798 a temporary. */
1799
1800 TREE_INT_CST_ELT (int_cst_node, 0) = hwi;
1801 TREE_TYPE (int_cst_node) = type;
1802
1803 tree *slot = int_cst_hash_table->find_slot (int_cst_node, INSERT);
1804 t = *slot;
1805 if (!t)
1806 {
1807 /* Insert this one into the hash table. */
1808 t = int_cst_node;
1809 *slot = t;
1810 /* Make a new node for next time round. */
1811 int_cst_node = make_int_cst (1, 1);
1812 }
1813 }
1814 }
1815 else
1816 {
1817 /* The value either hashes properly or we drop it on the floor
1818 for the gc to take care of. There will not be enough of them
1819 to worry about. */
1820
1821 tree nt = build_new_int_cst (type, cst);
1822 tree *slot = int_cst_hash_table->find_slot (nt, INSERT);
1823 t = *slot;
1824 if (!t)
1825 {
1826 /* Insert this one into the hash table. */
1827 t = nt;
1828 *slot = t;
1829 }
1830 else
1831 ggc_free (nt);
1832 }
1833
1834 return t;
1835 }
1836
1837 hashval_t
1838 poly_int_cst_hasher::hash (tree t)
1839 {
1840 inchash::hash hstate;
1841
1842 hstate.add_int (TYPE_UID (TREE_TYPE (t)));
1843 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1844 hstate.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t, i)));
1845
1846 return hstate.end ();
1847 }
1848
1849 bool
1850 poly_int_cst_hasher::equal (tree x, const compare_type &y)
1851 {
1852 if (TREE_TYPE (x) != y.first)
1853 return false;
1854 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1855 if (wi::to_wide (POLY_INT_CST_COEFF (x, i)) != y.second->coeffs[i])
1856 return false;
1857 return true;
1858 }
1859
1860 /* Build a POLY_INT_CST node with type TYPE and with the elements in VALUES.
1861 The elements must also have type TYPE. */
1862
1863 tree
1864 build_poly_int_cst (tree type, const poly_wide_int_ref &values)
1865 {
1866 unsigned int prec = TYPE_PRECISION (type);
1867 gcc_assert (prec <= values.coeffs[0].get_precision ());
1868 poly_wide_int c = poly_wide_int::from (values, prec, SIGNED);
1869
1870 inchash::hash h;
1871 h.add_int (TYPE_UID (type));
1872 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1873 h.add_wide_int (c.coeffs[i]);
1874 poly_int_cst_hasher::compare_type comp (type, &c);
1875 tree *slot = poly_int_cst_hash_table->find_slot_with_hash (comp, h.end (),
1876 INSERT);
1877 if (*slot == NULL_TREE)
1878 {
1879 tree coeffs[NUM_POLY_INT_COEFFS];
1880 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1881 coeffs[i] = wide_int_to_tree_1 (type, c.coeffs[i]);
1882 *slot = build_new_poly_int_cst (type, coeffs);
1883 }
1884 return *slot;
1885 }
1886
1887 /* Create a constant tree with value VALUE in type TYPE. */
1888
1889 tree
1890 wide_int_to_tree (tree type, const poly_wide_int_ref &value)
1891 {
1892 if (value.is_constant ())
1893 return wide_int_to_tree_1 (type, value.coeffs[0]);
1894 return build_poly_int_cst (type, value);
1895 }
1896
1897 /* Insert INTEGER_CST T into a cache of integer constants. And return
1898 the cached constant (which may or may not be T). If MIGHT_DUPLICATE
1899 is false, and T falls into the type's 'smaller values' range, there
1900 cannot be an existing entry. Otherwise, if MIGHT_DUPLICATE is true,
1901 or the value is large, should an existing entry exist, it is
1902 returned (rather than inserting T). */
1903
1904 tree
1905 cache_integer_cst (tree t, bool might_duplicate ATTRIBUTE_UNUSED)
1906 {
1907 tree type = TREE_TYPE (t);
1908 int ix = -1;
1909 int limit = 0;
1910 int prec = TYPE_PRECISION (type);
1911
1912 gcc_assert (!TREE_OVERFLOW (t));
1913
1914 /* The caching indices here must match those in
1915 wide_int_to_type_1. */
1916 switch (TREE_CODE (type))
1917 {
1918 case NULLPTR_TYPE:
1919 gcc_checking_assert (integer_zerop (t));
1920 /* Fallthru. */
1921
1922 case POINTER_TYPE:
1923 case REFERENCE_TYPE:
1924 {
1925 if (integer_zerop (t))
1926 ix = 0;
1927 else if (integer_onep (t))
1928 ix = 2;
1929
1930 if (ix >= 0)
1931 limit = 3;
1932 }
1933 break;
1934
1935 case BOOLEAN_TYPE:
1936 /* Cache false or true. */
1937 limit = 2;
1938 if (wi::ltu_p (wi::to_wide (t), 2))
1939 ix = TREE_INT_CST_ELT (t, 0);
1940 break;
1941
1942 case INTEGER_TYPE:
1943 case OFFSET_TYPE:
1944 if (TYPE_UNSIGNED (type))
1945 {
1946 /* Cache 0..N */
1947 limit = param_integer_share_limit;
1948
1949 /* This is a little hokie, but if the prec is smaller than
1950 what is necessary to hold param_integer_share_limit, then the
1951 obvious test will not get the correct answer. */
1952 if (prec < HOST_BITS_PER_WIDE_INT)
1953 {
1954 if (tree_to_uhwi (t)
1955 < (unsigned HOST_WIDE_INT) param_integer_share_limit)
1956 ix = tree_to_uhwi (t);
1957 }
1958 else if (wi::ltu_p (wi::to_wide (t), param_integer_share_limit))
1959 ix = tree_to_uhwi (t);
1960 }
1961 else
1962 {
1963 /* Cache -1..N */
1964 limit = param_integer_share_limit + 1;
1965
1966 if (integer_minus_onep (t))
1967 ix = 0;
1968 else if (!wi::neg_p (wi::to_wide (t)))
1969 {
1970 if (prec < HOST_BITS_PER_WIDE_INT)
1971 {
1972 if (tree_to_shwi (t) < param_integer_share_limit)
1973 ix = tree_to_shwi (t) + 1;
1974 }
1975 else if (wi::ltu_p (wi::to_wide (t), param_integer_share_limit))
1976 ix = tree_to_shwi (t) + 1;
1977 }
1978 }
1979 break;
1980
1981 case ENUMERAL_TYPE:
1982 /* The slot used by TYPE_CACHED_VALUES is used for the enum
1983 members. */
1984 break;
1985
1986 default:
1987 gcc_unreachable ();
1988 }
1989
1990 if (ix >= 0)
1991 {
1992 /* Look for it in the type's vector of small shared ints. */
1993 if (!TYPE_CACHED_VALUES_P (type))
1994 {
1995 TYPE_CACHED_VALUES_P (type) = 1;
1996 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1997 }
1998
1999 if (tree r = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix))
2000 {
2001 gcc_checking_assert (might_duplicate);
2002 t = r;
2003 }
2004 else
2005 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
2006 }
2007 else
2008 {
2009 /* Use the cache of larger shared ints. */
2010 tree *slot = int_cst_hash_table->find_slot (t, INSERT);
2011 if (tree r = *slot)
2012 {
2013 /* If there is already an entry for the number verify it's the
2014 same value. */
2015 gcc_checking_assert (wi::to_wide (tree (r)) == wi::to_wide (t));
2016 /* And return the cached value. */
2017 t = r;
2018 }
2019 else
2020 /* Otherwise insert this one into the hash table. */
2021 *slot = t;
2022 }
2023
2024 return t;
2025 }
2026
2027
2028 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
2029 and the rest are zeros. */
2030
2031 tree
2032 build_low_bits_mask (tree type, unsigned bits)
2033 {
2034 gcc_assert (bits <= TYPE_PRECISION (type));
2035
2036 return wide_int_to_tree (type, wi::mask (bits, false,
2037 TYPE_PRECISION (type)));
2038 }
2039
2040 /* Checks that X is integer constant that can be expressed in (unsigned)
2041 HOST_WIDE_INT without loss of precision. */
2042
2043 bool
2044 cst_and_fits_in_hwi (const_tree x)
2045 {
2046 return (TREE_CODE (x) == INTEGER_CST
2047 && (tree_fits_shwi_p (x) || tree_fits_uhwi_p (x)));
2048 }
2049
2050 /* Build a newly constructed VECTOR_CST with the given values of
2051 (VECTOR_CST_)LOG2_NPATTERNS and (VECTOR_CST_)NELTS_PER_PATTERN. */
2052
2053 tree
2054 make_vector (unsigned log2_npatterns,
2055 unsigned int nelts_per_pattern MEM_STAT_DECL)
2056 {
2057 gcc_assert (IN_RANGE (nelts_per_pattern, 1, 3));
2058 tree t;
2059 unsigned npatterns = 1 << log2_npatterns;
2060 unsigned encoded_nelts = npatterns * nelts_per_pattern;
2061 unsigned length = (sizeof (struct tree_vector)
2062 + (encoded_nelts - 1) * sizeof (tree));
2063
2064 record_node_allocation_statistics (VECTOR_CST, length);
2065
2066 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2067
2068 TREE_SET_CODE (t, VECTOR_CST);
2069 TREE_CONSTANT (t) = 1;
2070 VECTOR_CST_LOG2_NPATTERNS (t) = log2_npatterns;
2071 VECTOR_CST_NELTS_PER_PATTERN (t) = nelts_per_pattern;
2072
2073 return t;
2074 }
2075
2076 /* Return a new VECTOR_CST node whose type is TYPE and whose values
2077 are extracted from V, a vector of CONSTRUCTOR_ELT. */
2078
2079 tree
2080 build_vector_from_ctor (tree type, const vec<constructor_elt, va_gc> *v)
2081 {
2082 if (vec_safe_length (v) == 0)
2083 return build_zero_cst (type);
2084
2085 unsigned HOST_WIDE_INT idx, nelts;
2086 tree value;
2087
2088 /* We can't construct a VECTOR_CST for a variable number of elements. */
2089 nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
2090 tree_vector_builder vec (type, nelts, 1);
2091 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
2092 {
2093 if (TREE_CODE (value) == VECTOR_CST)
2094 {
2095 /* If NELTS is constant then this must be too. */
2096 unsigned int sub_nelts = VECTOR_CST_NELTS (value).to_constant ();
2097 for (unsigned i = 0; i < sub_nelts; ++i)
2098 vec.quick_push (VECTOR_CST_ELT (value, i));
2099 }
2100 else
2101 vec.quick_push (value);
2102 }
2103 while (vec.length () < nelts)
2104 vec.quick_push (build_zero_cst (TREE_TYPE (type)));
2105
2106 return vec.build ();
2107 }
2108
2109 /* Build a vector of type VECTYPE where all the elements are SCs. */
2110 tree
2111 build_vector_from_val (tree vectype, tree sc)
2112 {
2113 unsigned HOST_WIDE_INT i, nunits;
2114
2115 if (sc == error_mark_node)
2116 return sc;
2117
2118 /* Verify that the vector type is suitable for SC. Note that there
2119 is some inconsistency in the type-system with respect to restrict
2120 qualifications of pointers. Vector types always have a main-variant
2121 element type and the qualification is applied to the vector-type.
2122 So TREE_TYPE (vector-type) does not return a properly qualified
2123 vector element-type. */
2124 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
2125 TREE_TYPE (vectype)));
2126
2127 if (CONSTANT_CLASS_P (sc))
2128 {
2129 tree_vector_builder v (vectype, 1, 1);
2130 v.quick_push (sc);
2131 return v.build ();
2132 }
2133 else if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits))
2134 return fold_build1 (VEC_DUPLICATE_EXPR, vectype, sc);
2135 else
2136 {
2137 vec<constructor_elt, va_gc> *v;
2138 vec_alloc (v, nunits);
2139 for (i = 0; i < nunits; ++i)
2140 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
2141 return build_constructor (vectype, v);
2142 }
2143 }
2144
2145 /* If TYPE is not a vector type, just return SC, otherwise return
2146 build_vector_from_val (TYPE, SC). */
2147
2148 tree
2149 build_uniform_cst (tree type, tree sc)
2150 {
2151 if (!VECTOR_TYPE_P (type))
2152 return sc;
2153
2154 return build_vector_from_val (type, sc);
2155 }
2156
2157 /* Build a vector series of type TYPE in which element I has the value
2158 BASE + I * STEP. The result is a constant if BASE and STEP are constant
2159 and a VEC_SERIES_EXPR otherwise. */
2160
2161 tree
2162 build_vec_series (tree type, tree base, tree step)
2163 {
2164 if (integer_zerop (step))
2165 return build_vector_from_val (type, base);
2166 if (TREE_CODE (base) == INTEGER_CST && TREE_CODE (step) == INTEGER_CST)
2167 {
2168 tree_vector_builder builder (type, 1, 3);
2169 tree elt1 = wide_int_to_tree (TREE_TYPE (base),
2170 wi::to_wide (base) + wi::to_wide (step));
2171 tree elt2 = wide_int_to_tree (TREE_TYPE (base),
2172 wi::to_wide (elt1) + wi::to_wide (step));
2173 builder.quick_push (base);
2174 builder.quick_push (elt1);
2175 builder.quick_push (elt2);
2176 return builder.build ();
2177 }
2178 return build2 (VEC_SERIES_EXPR, type, base, step);
2179 }
2180
2181 /* Return a vector with the same number of units and number of bits
2182 as VEC_TYPE, but in which the elements are a linear series of unsigned
2183 integers { BASE, BASE + STEP, BASE + STEP * 2, ... }. */
2184
2185 tree
2186 build_index_vector (tree vec_type, poly_uint64 base, poly_uint64 step)
2187 {
2188 tree index_vec_type = vec_type;
2189 tree index_elt_type = TREE_TYPE (vec_type);
2190 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vec_type);
2191 if (!INTEGRAL_TYPE_P (index_elt_type) || !TYPE_UNSIGNED (index_elt_type))
2192 {
2193 index_elt_type = build_nonstandard_integer_type
2194 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (index_elt_type)), true);
2195 index_vec_type = build_vector_type (index_elt_type, nunits);
2196 }
2197
2198 tree_vector_builder v (index_vec_type, 1, 3);
2199 for (unsigned int i = 0; i < 3; ++i)
2200 v.quick_push (build_int_cstu (index_elt_type, base + i * step));
2201 return v.build ();
2202 }
2203
2204 /* Return a VECTOR_CST of type VEC_TYPE in which the first NUM_A
2205 elements are A and the rest are B. */
2206
2207 tree
2208 build_vector_a_then_b (tree vec_type, unsigned int num_a, tree a, tree b)
2209 {
2210 gcc_assert (known_le (num_a, TYPE_VECTOR_SUBPARTS (vec_type)));
2211 unsigned int count = constant_lower_bound (TYPE_VECTOR_SUBPARTS (vec_type));
2212 /* Optimize the constant case. */
2213 if ((count & 1) == 0 && TYPE_VECTOR_SUBPARTS (vec_type).is_constant ())
2214 count /= 2;
2215 tree_vector_builder builder (vec_type, count, 2);
2216 for (unsigned int i = 0; i < count * 2; ++i)
2217 builder.quick_push (i < num_a ? a : b);
2218 return builder.build ();
2219 }
2220
2221 /* Something has messed with the elements of CONSTRUCTOR C after it was built;
2222 calculate TREE_CONSTANT and TREE_SIDE_EFFECTS. */
2223
2224 void
2225 recompute_constructor_flags (tree c)
2226 {
2227 unsigned int i;
2228 tree val;
2229 bool constant_p = true;
2230 bool side_effects_p = false;
2231 vec<constructor_elt, va_gc> *vals = CONSTRUCTOR_ELTS (c);
2232
2233 FOR_EACH_CONSTRUCTOR_VALUE (vals, i, val)
2234 {
2235 /* Mostly ctors will have elts that don't have side-effects, so
2236 the usual case is to scan all the elements. Hence a single
2237 loop for both const and side effects, rather than one loop
2238 each (with early outs). */
2239 if (!TREE_CONSTANT (val))
2240 constant_p = false;
2241 if (TREE_SIDE_EFFECTS (val))
2242 side_effects_p = true;
2243 }
2244
2245 TREE_SIDE_EFFECTS (c) = side_effects_p;
2246 TREE_CONSTANT (c) = constant_p;
2247 }
2248
2249 /* Make sure that TREE_CONSTANT and TREE_SIDE_EFFECTS are correct for
2250 CONSTRUCTOR C. */
2251
2252 void
2253 verify_constructor_flags (tree c)
2254 {
2255 unsigned int i;
2256 tree val;
2257 bool constant_p = TREE_CONSTANT (c);
2258 bool side_effects_p = TREE_SIDE_EFFECTS (c);
2259 vec<constructor_elt, va_gc> *vals = CONSTRUCTOR_ELTS (c);
2260
2261 FOR_EACH_CONSTRUCTOR_VALUE (vals, i, val)
2262 {
2263 if (constant_p && !TREE_CONSTANT (val))
2264 internal_error ("non-constant element in constant CONSTRUCTOR");
2265 if (!side_effects_p && TREE_SIDE_EFFECTS (val))
2266 internal_error ("side-effects element in no-side-effects CONSTRUCTOR");
2267 }
2268 }
2269
2270 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
2271 are in the vec pointed to by VALS. */
2272 tree
2273 build_constructor (tree type, vec<constructor_elt, va_gc> *vals MEM_STAT_DECL)
2274 {
2275 tree c = make_node (CONSTRUCTOR PASS_MEM_STAT);
2276
2277 TREE_TYPE (c) = type;
2278 CONSTRUCTOR_ELTS (c) = vals;
2279
2280 recompute_constructor_flags (c);
2281
2282 return c;
2283 }
2284
2285 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
2286 INDEX and VALUE. */
2287 tree
2288 build_constructor_single (tree type, tree index, tree value)
2289 {
2290 vec<constructor_elt, va_gc> *v;
2291 constructor_elt elt = {index, value};
2292
2293 vec_alloc (v, 1);
2294 v->quick_push (elt);
2295
2296 return build_constructor (type, v);
2297 }
2298
2299
2300 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
2301 are in a list pointed to by VALS. */
2302 tree
2303 build_constructor_from_list (tree type, tree vals)
2304 {
2305 tree t;
2306 vec<constructor_elt, va_gc> *v = NULL;
2307
2308 if (vals)
2309 {
2310 vec_alloc (v, list_length (vals));
2311 for (t = vals; t; t = TREE_CHAIN (t))
2312 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
2313 }
2314
2315 return build_constructor (type, v);
2316 }
2317
2318 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
2319 are in a vector pointed to by VALS. Note that the TREE_PURPOSE
2320 fields in the constructor remain null. */
2321
2322 tree
2323 build_constructor_from_vec (tree type, const vec<tree, va_gc> *vals)
2324 {
2325 vec<constructor_elt, va_gc> *v = NULL;
2326
2327 for (tree t : vals)
2328 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, t);
2329
2330 return build_constructor (type, v);
2331 }
2332
2333 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
2334 of elements, provided as index/value pairs. */
2335
2336 tree
2337 build_constructor_va (tree type, int nelts, ...)
2338 {
2339 vec<constructor_elt, va_gc> *v = NULL;
2340 va_list p;
2341
2342 va_start (p, nelts);
2343 vec_alloc (v, nelts);
2344 while (nelts--)
2345 {
2346 tree index = va_arg (p, tree);
2347 tree value = va_arg (p, tree);
2348 CONSTRUCTOR_APPEND_ELT (v, index, value);
2349 }
2350 va_end (p);
2351 return build_constructor (type, v);
2352 }
2353
2354 /* Return a node of type TYPE for which TREE_CLOBBER_P is true. */
2355
2356 tree
2357 build_clobber (tree type, enum clobber_kind kind)
2358 {
2359 tree clobber = build_constructor (type, NULL);
2360 TREE_THIS_VOLATILE (clobber) = true;
2361 CLOBBER_KIND (clobber) = kind;
2362 return clobber;
2363 }
2364
2365 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
2366
2367 tree
2368 build_fixed (tree type, FIXED_VALUE_TYPE f)
2369 {
2370 tree v;
2371 FIXED_VALUE_TYPE *fp;
2372
2373 v = make_node (FIXED_CST);
2374 fp = ggc_alloc<fixed_value> ();
2375 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
2376
2377 TREE_TYPE (v) = type;
2378 TREE_FIXED_CST_PTR (v) = fp;
2379 return v;
2380 }
2381
2382 /* Return a new REAL_CST node whose type is TYPE and value is D. */
2383
2384 tree
2385 build_real (tree type, REAL_VALUE_TYPE d)
2386 {
2387 tree v;
2388 int overflow = 0;
2389
2390 /* dconst{0,1,2,m1,half} are used in various places in
2391 the middle-end and optimizers, allow them here
2392 even for decimal floating point types as an exception
2393 by converting them to decimal. */
2394 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type))
2395 && (d.cl == rvc_normal || d.cl == rvc_zero)
2396 && !d.decimal)
2397 {
2398 if (memcmp (&d, &dconst1, sizeof (d)) == 0)
2399 decimal_real_from_string (&d, "1");
2400 else if (memcmp (&d, &dconst2, sizeof (d)) == 0)
2401 decimal_real_from_string (&d, "2");
2402 else if (memcmp (&d, &dconstm1, sizeof (d)) == 0)
2403 decimal_real_from_string (&d, "-1");
2404 else if (memcmp (&d, &dconsthalf, sizeof (d)) == 0)
2405 decimal_real_from_string (&d, "0.5");
2406 else if (memcmp (&d, &dconst0, sizeof (d)) == 0)
2407 {
2408 /* Make sure to give zero the minimum quantum exponent for
2409 the type (which corresponds to all bits zero). */
2410 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
2411 char buf[16];
2412 sprintf (buf, "0e%d", fmt->emin - fmt->p);
2413 decimal_real_from_string (&d, buf);
2414 }
2415 else
2416 gcc_unreachable ();
2417 }
2418
2419 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
2420 Consider doing it via real_convert now. */
2421
2422 v = make_node (REAL_CST);
2423 TREE_TYPE (v) = type;
2424 memcpy (TREE_REAL_CST_PTR (v), &d, sizeof (REAL_VALUE_TYPE));
2425 TREE_OVERFLOW (v) = overflow;
2426 return v;
2427 }
2428
2429 /* Like build_real, but first truncate D to the type. */
2430
2431 tree
2432 build_real_truncate (tree type, REAL_VALUE_TYPE d)
2433 {
2434 return build_real (type, real_value_truncate (TYPE_MODE (type), d));
2435 }
2436
2437 /* Return a new REAL_CST node whose type is TYPE
2438 and whose value is the integer value of the INTEGER_CST node I. */
2439
2440 REAL_VALUE_TYPE
2441 real_value_from_int_cst (const_tree type, const_tree i)
2442 {
2443 REAL_VALUE_TYPE d;
2444
2445 /* Clear all bits of the real value type so that we can later do
2446 bitwise comparisons to see if two values are the same. */
2447 memset (&d, 0, sizeof d);
2448
2449 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode, wi::to_wide (i),
2450 TYPE_SIGN (TREE_TYPE (i)));
2451 return d;
2452 }
2453
2454 /* Given a tree representing an integer constant I, return a tree
2455 representing the same value as a floating-point constant of type TYPE. */
2456
2457 tree
2458 build_real_from_int_cst (tree type, const_tree i)
2459 {
2460 tree v;
2461 int overflow = TREE_OVERFLOW (i);
2462
2463 v = build_real (type, real_value_from_int_cst (type, i));
2464
2465 TREE_OVERFLOW (v) |= overflow;
2466 return v;
2467 }
2468
2469 /* Return a new REAL_CST node whose type is TYPE
2470 and whose value is the integer value I which has sign SGN. */
2471
2472 tree
2473 build_real_from_wide (tree type, const wide_int_ref &i, signop sgn)
2474 {
2475 REAL_VALUE_TYPE d;
2476
2477 /* Clear all bits of the real value type so that we can later do
2478 bitwise comparisons to see if two values are the same. */
2479 memset (&d, 0, sizeof d);
2480
2481 real_from_integer (&d, TYPE_MODE (type), i, sgn);
2482 return build_real (type, d);
2483 }
2484
2485 /* Return a newly constructed STRING_CST node whose value is the LEN
2486 characters at STR when STR is nonnull, or all zeros otherwise.
2487 Note that for a C string literal, LEN should include the trailing NUL.
2488 The TREE_TYPE is not initialized. */
2489
2490 tree
2491 build_string (unsigned len, const char *str /*= NULL */)
2492 {
2493 /* Do not waste bytes provided by padding of struct tree_string. */
2494 unsigned size = len + offsetof (struct tree_string, str) + 1;
2495
2496 record_node_allocation_statistics (STRING_CST, size);
2497
2498 tree s = (tree) ggc_internal_alloc (size);
2499
2500 memset (s, 0, sizeof (struct tree_typed));
2501 TREE_SET_CODE (s, STRING_CST);
2502 TREE_CONSTANT (s) = 1;
2503 TREE_STRING_LENGTH (s) = len;
2504 if (str)
2505 memcpy (s->string.str, str, len);
2506 else
2507 memset (s->string.str, 0, len);
2508 s->string.str[len] = '\0';
2509
2510 return s;
2511 }
2512
2513 /* Return a newly constructed COMPLEX_CST node whose value is
2514 specified by the real and imaginary parts REAL and IMAG.
2515 Both REAL and IMAG should be constant nodes. TYPE, if specified,
2516 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
2517
2518 tree
2519 build_complex (tree type, tree real, tree imag)
2520 {
2521 gcc_assert (CONSTANT_CLASS_P (real));
2522 gcc_assert (CONSTANT_CLASS_P (imag));
2523
2524 tree t = make_node (COMPLEX_CST);
2525
2526 TREE_REALPART (t) = real;
2527 TREE_IMAGPART (t) = imag;
2528 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
2529 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
2530 return t;
2531 }
2532
2533 /* Build a complex (inf +- 0i), such as for the result of cproj.
2534 TYPE is the complex tree type of the result. If NEG is true, the
2535 imaginary zero is negative. */
2536
2537 tree
2538 build_complex_inf (tree type, bool neg)
2539 {
2540 REAL_VALUE_TYPE rzero = dconst0;
2541
2542 rzero.sign = neg;
2543 return build_complex (type, build_real (TREE_TYPE (type), dconstinf),
2544 build_real (TREE_TYPE (type), rzero));
2545 }
2546
2547 /* Return the constant 1 in type TYPE. If TYPE has several elements, each
2548 element is set to 1. In particular, this is 1 + i for complex types. */
2549
2550 tree
2551 build_each_one_cst (tree type)
2552 {
2553 if (TREE_CODE (type) == COMPLEX_TYPE)
2554 {
2555 tree scalar = build_one_cst (TREE_TYPE (type));
2556 return build_complex (type, scalar, scalar);
2557 }
2558 else
2559 return build_one_cst (type);
2560 }
2561
2562 /* Return a constant of arithmetic type TYPE which is the
2563 multiplicative identity of the set TYPE. */
2564
2565 tree
2566 build_one_cst (tree type)
2567 {
2568 switch (TREE_CODE (type))
2569 {
2570 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2571 case POINTER_TYPE: case REFERENCE_TYPE:
2572 case OFFSET_TYPE:
2573 return build_int_cst (type, 1);
2574
2575 case REAL_TYPE:
2576 return build_real (type, dconst1);
2577
2578 case FIXED_POINT_TYPE:
2579 /* We can only generate 1 for accum types. */
2580 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2581 return build_fixed (type, FCONST1 (TYPE_MODE (type)));
2582
2583 case VECTOR_TYPE:
2584 {
2585 tree scalar = build_one_cst (TREE_TYPE (type));
2586
2587 return build_vector_from_val (type, scalar);
2588 }
2589
2590 case COMPLEX_TYPE:
2591 return build_complex (type,
2592 build_one_cst (TREE_TYPE (type)),
2593 build_zero_cst (TREE_TYPE (type)));
2594
2595 default:
2596 gcc_unreachable ();
2597 }
2598 }
2599
2600 /* Return an integer of type TYPE containing all 1's in as much precision as
2601 it contains, or a complex or vector whose subparts are such integers. */
2602
2603 tree
2604 build_all_ones_cst (tree type)
2605 {
2606 if (TREE_CODE (type) == COMPLEX_TYPE)
2607 {
2608 tree scalar = build_all_ones_cst (TREE_TYPE (type));
2609 return build_complex (type, scalar, scalar);
2610 }
2611 else
2612 return build_minus_one_cst (type);
2613 }
2614
2615 /* Return a constant of arithmetic type TYPE which is the
2616 opposite of the multiplicative identity of the set TYPE. */
2617
2618 tree
2619 build_minus_one_cst (tree type)
2620 {
2621 switch (TREE_CODE (type))
2622 {
2623 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2624 case POINTER_TYPE: case REFERENCE_TYPE:
2625 case OFFSET_TYPE:
2626 return build_int_cst (type, -1);
2627
2628 case REAL_TYPE:
2629 return build_real (type, dconstm1);
2630
2631 case FIXED_POINT_TYPE:
2632 /* We can only generate 1 for accum types. */
2633 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2634 return build_fixed (type,
2635 fixed_from_double_int (double_int_minus_one,
2636 SCALAR_TYPE_MODE (type)));
2637
2638 case VECTOR_TYPE:
2639 {
2640 tree scalar = build_minus_one_cst (TREE_TYPE (type));
2641
2642 return build_vector_from_val (type, scalar);
2643 }
2644
2645 case COMPLEX_TYPE:
2646 return build_complex (type,
2647 build_minus_one_cst (TREE_TYPE (type)),
2648 build_zero_cst (TREE_TYPE (type)));
2649
2650 default:
2651 gcc_unreachable ();
2652 }
2653 }
2654
2655 /* Build 0 constant of type TYPE. This is used by constructor folding
2656 and thus the constant should be represented in memory by
2657 zero(es). */
2658
2659 tree
2660 build_zero_cst (tree type)
2661 {
2662 switch (TREE_CODE (type))
2663 {
2664 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2665 case POINTER_TYPE: case REFERENCE_TYPE:
2666 case OFFSET_TYPE: case NULLPTR_TYPE:
2667 return build_int_cst (type, 0);
2668
2669 case REAL_TYPE:
2670 return build_real (type, dconst0);
2671
2672 case FIXED_POINT_TYPE:
2673 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
2674
2675 case VECTOR_TYPE:
2676 {
2677 tree scalar = build_zero_cst (TREE_TYPE (type));
2678
2679 return build_vector_from_val (type, scalar);
2680 }
2681
2682 case COMPLEX_TYPE:
2683 {
2684 tree zero = build_zero_cst (TREE_TYPE (type));
2685
2686 return build_complex (type, zero, zero);
2687 }
2688
2689 default:
2690 if (!AGGREGATE_TYPE_P (type))
2691 return fold_convert (type, integer_zero_node);
2692 return build_constructor (type, NULL);
2693 }
2694 }
2695
2696
2697 /* Build a BINFO with LEN language slots. */
2698
2699 tree
2700 make_tree_binfo (unsigned base_binfos MEM_STAT_DECL)
2701 {
2702 tree t;
2703 size_t length = (offsetof (struct tree_binfo, base_binfos)
2704 + vec<tree, va_gc>::embedded_size (base_binfos));
2705
2706 record_node_allocation_statistics (TREE_BINFO, length);
2707
2708 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
2709
2710 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
2711
2712 TREE_SET_CODE (t, TREE_BINFO);
2713
2714 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
2715
2716 return t;
2717 }
2718
2719 /* Create a CASE_LABEL_EXPR tree node and return it. */
2720
2721 tree
2722 build_case_label (tree low_value, tree high_value, tree label_decl)
2723 {
2724 tree t = make_node (CASE_LABEL_EXPR);
2725
2726 TREE_TYPE (t) = void_type_node;
2727 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
2728
2729 CASE_LOW (t) = low_value;
2730 CASE_HIGH (t) = high_value;
2731 CASE_LABEL (t) = label_decl;
2732 CASE_CHAIN (t) = NULL_TREE;
2733
2734 return t;
2735 }
2736
2737 /* Build a newly constructed INTEGER_CST node. LEN and EXT_LEN are the
2738 values of TREE_INT_CST_NUNITS and TREE_INT_CST_EXT_NUNITS respectively.
2739 The latter determines the length of the HOST_WIDE_INT vector. */
2740
2741 tree
2742 make_int_cst (int len, int ext_len MEM_STAT_DECL)
2743 {
2744 tree t;
2745 int length = ((ext_len - 1) * sizeof (HOST_WIDE_INT)
2746 + sizeof (struct tree_int_cst));
2747
2748 gcc_assert (len);
2749 record_node_allocation_statistics (INTEGER_CST, length);
2750
2751 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2752
2753 TREE_SET_CODE (t, INTEGER_CST);
2754 TREE_INT_CST_NUNITS (t) = len;
2755 TREE_INT_CST_EXT_NUNITS (t) = ext_len;
2756 /* to_offset can only be applied to trees that are offset_int-sized
2757 or smaller. EXT_LEN is correct if it fits, otherwise the constant
2758 must be exactly the precision of offset_int and so LEN is correct. */
2759 if (ext_len <= OFFSET_INT_ELTS)
2760 TREE_INT_CST_OFFSET_NUNITS (t) = ext_len;
2761 else
2762 TREE_INT_CST_OFFSET_NUNITS (t) = len;
2763
2764 TREE_CONSTANT (t) = 1;
2765
2766 return t;
2767 }
2768
2769 /* Build a newly constructed TREE_VEC node of length LEN. */
2770
2771 tree
2772 make_tree_vec (int len MEM_STAT_DECL)
2773 {
2774 tree t;
2775 size_t length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2776
2777 record_node_allocation_statistics (TREE_VEC, length);
2778
2779 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2780
2781 TREE_SET_CODE (t, TREE_VEC);
2782 TREE_VEC_LENGTH (t) = len;
2783
2784 return t;
2785 }
2786
2787 /* Grow a TREE_VEC node to new length LEN. */
2788
2789 tree
2790 grow_tree_vec (tree v, int len MEM_STAT_DECL)
2791 {
2792 gcc_assert (TREE_CODE (v) == TREE_VEC);
2793
2794 int oldlen = TREE_VEC_LENGTH (v);
2795 gcc_assert (len > oldlen);
2796
2797 size_t oldlength = (oldlen - 1) * sizeof (tree) + sizeof (struct tree_vec);
2798 size_t length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2799
2800 record_node_allocation_statistics (TREE_VEC, length - oldlength);
2801
2802 v = (tree) ggc_realloc (v, length PASS_MEM_STAT);
2803
2804 TREE_VEC_LENGTH (v) = len;
2805
2806 return v;
2807 }
2808 \f
2809 /* Return 1 if EXPR is the constant zero, whether it is integral, float or
2810 fixed, and scalar, complex or vector. */
2811
2812 bool
2813 zerop (const_tree expr)
2814 {
2815 return (integer_zerop (expr)
2816 || real_zerop (expr)
2817 || fixed_zerop (expr));
2818 }
2819
2820 /* Return 1 if EXPR is the integer constant zero or a complex constant
2821 of zero, or a location wrapper for such a constant. */
2822
2823 bool
2824 integer_zerop (const_tree expr)
2825 {
2826 STRIP_ANY_LOCATION_WRAPPER (expr);
2827
2828 switch (TREE_CODE (expr))
2829 {
2830 case INTEGER_CST:
2831 return wi::to_wide (expr) == 0;
2832 case COMPLEX_CST:
2833 return (integer_zerop (TREE_REALPART (expr))
2834 && integer_zerop (TREE_IMAGPART (expr)));
2835 case VECTOR_CST:
2836 return (VECTOR_CST_NPATTERNS (expr) == 1
2837 && VECTOR_CST_DUPLICATE_P (expr)
2838 && integer_zerop (VECTOR_CST_ENCODED_ELT (expr, 0)));
2839 default:
2840 return false;
2841 }
2842 }
2843
2844 /* Return 1 if EXPR is the integer constant one or the corresponding
2845 complex constant, or a location wrapper for such a constant. */
2846
2847 bool
2848 integer_onep (const_tree expr)
2849 {
2850 STRIP_ANY_LOCATION_WRAPPER (expr);
2851
2852 switch (TREE_CODE (expr))
2853 {
2854 case INTEGER_CST:
2855 return wi::eq_p (wi::to_widest (expr), 1);
2856 case COMPLEX_CST:
2857 return (integer_onep (TREE_REALPART (expr))
2858 && integer_zerop (TREE_IMAGPART (expr)));
2859 case VECTOR_CST:
2860 return (VECTOR_CST_NPATTERNS (expr) == 1
2861 && VECTOR_CST_DUPLICATE_P (expr)
2862 && integer_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
2863 default:
2864 return false;
2865 }
2866 }
2867
2868 /* Return 1 if EXPR is the integer constant one. For complex and vector,
2869 return 1 if every piece is the integer constant one.
2870 Also return 1 for location wrappers for such a constant. */
2871
2872 bool
2873 integer_each_onep (const_tree expr)
2874 {
2875 STRIP_ANY_LOCATION_WRAPPER (expr);
2876
2877 if (TREE_CODE (expr) == COMPLEX_CST)
2878 return (integer_onep (TREE_REALPART (expr))
2879 && integer_onep (TREE_IMAGPART (expr)));
2880 else
2881 return integer_onep (expr);
2882 }
2883
2884 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
2885 it contains, or a complex or vector whose subparts are such integers,
2886 or a location wrapper for such a constant. */
2887
2888 bool
2889 integer_all_onesp (const_tree expr)
2890 {
2891 STRIP_ANY_LOCATION_WRAPPER (expr);
2892
2893 if (TREE_CODE (expr) == COMPLEX_CST
2894 && integer_all_onesp (TREE_REALPART (expr))
2895 && integer_all_onesp (TREE_IMAGPART (expr)))
2896 return true;
2897
2898 else if (TREE_CODE (expr) == VECTOR_CST)
2899 return (VECTOR_CST_NPATTERNS (expr) == 1
2900 && VECTOR_CST_DUPLICATE_P (expr)
2901 && integer_all_onesp (VECTOR_CST_ENCODED_ELT (expr, 0)));
2902
2903 else if (TREE_CODE (expr) != INTEGER_CST)
2904 return false;
2905
2906 return (wi::max_value (TYPE_PRECISION (TREE_TYPE (expr)), UNSIGNED)
2907 == wi::to_wide (expr));
2908 }
2909
2910 /* Return 1 if EXPR is the integer constant minus one, or a location wrapper
2911 for such a constant. */
2912
2913 bool
2914 integer_minus_onep (const_tree expr)
2915 {
2916 STRIP_ANY_LOCATION_WRAPPER (expr);
2917
2918 if (TREE_CODE (expr) == COMPLEX_CST)
2919 return (integer_all_onesp (TREE_REALPART (expr))
2920 && integer_zerop (TREE_IMAGPART (expr)));
2921 else
2922 return integer_all_onesp (expr);
2923 }
2924
2925 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
2926 one bit on), or a location wrapper for such a constant. */
2927
2928 bool
2929 integer_pow2p (const_tree expr)
2930 {
2931 STRIP_ANY_LOCATION_WRAPPER (expr);
2932
2933 if (TREE_CODE (expr) == COMPLEX_CST
2934 && integer_pow2p (TREE_REALPART (expr))
2935 && integer_zerop (TREE_IMAGPART (expr)))
2936 return true;
2937
2938 if (TREE_CODE (expr) != INTEGER_CST)
2939 return false;
2940
2941 return wi::popcount (wi::to_wide (expr)) == 1;
2942 }
2943
2944 /* Return 1 if EXPR is an integer constant other than zero or a
2945 complex constant other than zero, or a location wrapper for such a
2946 constant. */
2947
2948 bool
2949 integer_nonzerop (const_tree expr)
2950 {
2951 STRIP_ANY_LOCATION_WRAPPER (expr);
2952
2953 return ((TREE_CODE (expr) == INTEGER_CST
2954 && wi::to_wide (expr) != 0)
2955 || (TREE_CODE (expr) == COMPLEX_CST
2956 && (integer_nonzerop (TREE_REALPART (expr))
2957 || integer_nonzerop (TREE_IMAGPART (expr)))));
2958 }
2959
2960 /* Return 1 if EXPR is the integer constant one. For vector,
2961 return 1 if every piece is the integer constant minus one
2962 (representing the value TRUE).
2963 Also return 1 for location wrappers for such a constant. */
2964
2965 bool
2966 integer_truep (const_tree expr)
2967 {
2968 STRIP_ANY_LOCATION_WRAPPER (expr);
2969
2970 if (TREE_CODE (expr) == VECTOR_CST)
2971 return integer_all_onesp (expr);
2972 return integer_onep (expr);
2973 }
2974
2975 /* Return 1 if EXPR is the fixed-point constant zero, or a location wrapper
2976 for such a constant. */
2977
2978 bool
2979 fixed_zerop (const_tree expr)
2980 {
2981 STRIP_ANY_LOCATION_WRAPPER (expr);
2982
2983 return (TREE_CODE (expr) == FIXED_CST
2984 && TREE_FIXED_CST (expr).data.is_zero ());
2985 }
2986
2987 /* Return the power of two represented by a tree node known to be a
2988 power of two. */
2989
2990 int
2991 tree_log2 (const_tree expr)
2992 {
2993 if (TREE_CODE (expr) == COMPLEX_CST)
2994 return tree_log2 (TREE_REALPART (expr));
2995
2996 return wi::exact_log2 (wi::to_wide (expr));
2997 }
2998
2999 /* Similar, but return the largest integer Y such that 2 ** Y is less
3000 than or equal to EXPR. */
3001
3002 int
3003 tree_floor_log2 (const_tree expr)
3004 {
3005 if (TREE_CODE (expr) == COMPLEX_CST)
3006 return tree_log2 (TREE_REALPART (expr));
3007
3008 return wi::floor_log2 (wi::to_wide (expr));
3009 }
3010
3011 /* Return number of known trailing zero bits in EXPR, or, if the value of
3012 EXPR is known to be zero, the precision of it's type. */
3013
3014 unsigned int
3015 tree_ctz (const_tree expr)
3016 {
3017 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
3018 && !POINTER_TYPE_P (TREE_TYPE (expr)))
3019 return 0;
3020
3021 unsigned int ret1, ret2, prec = TYPE_PRECISION (TREE_TYPE (expr));
3022 switch (TREE_CODE (expr))
3023 {
3024 case INTEGER_CST:
3025 ret1 = wi::ctz (wi::to_wide (expr));
3026 return MIN (ret1, prec);
3027 case SSA_NAME:
3028 ret1 = wi::ctz (get_nonzero_bits (expr));
3029 return MIN (ret1, prec);
3030 case PLUS_EXPR:
3031 case MINUS_EXPR:
3032 case BIT_IOR_EXPR:
3033 case BIT_XOR_EXPR:
3034 case MIN_EXPR:
3035 case MAX_EXPR:
3036 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3037 if (ret1 == 0)
3038 return ret1;
3039 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
3040 return MIN (ret1, ret2);
3041 case POINTER_PLUS_EXPR:
3042 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3043 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
3044 /* Second operand is sizetype, which could be in theory
3045 wider than pointer's precision. Make sure we never
3046 return more than prec. */
3047 ret2 = MIN (ret2, prec);
3048 return MIN (ret1, ret2);
3049 case BIT_AND_EXPR:
3050 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3051 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
3052 return MAX (ret1, ret2);
3053 case MULT_EXPR:
3054 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3055 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
3056 return MIN (ret1 + ret2, prec);
3057 case LSHIFT_EXPR:
3058 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3059 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
3060 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
3061 {
3062 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
3063 return MIN (ret1 + ret2, prec);
3064 }
3065 return ret1;
3066 case RSHIFT_EXPR:
3067 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
3068 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
3069 {
3070 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3071 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
3072 if (ret1 > ret2)
3073 return ret1 - ret2;
3074 }
3075 return 0;
3076 case TRUNC_DIV_EXPR:
3077 case CEIL_DIV_EXPR:
3078 case FLOOR_DIV_EXPR:
3079 case ROUND_DIV_EXPR:
3080 case EXACT_DIV_EXPR:
3081 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
3082 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) == 1)
3083 {
3084 int l = tree_log2 (TREE_OPERAND (expr, 1));
3085 if (l >= 0)
3086 {
3087 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3088 ret2 = l;
3089 if (ret1 > ret2)
3090 return ret1 - ret2;
3091 }
3092 }
3093 return 0;
3094 CASE_CONVERT:
3095 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3096 if (ret1 && ret1 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
3097 ret1 = prec;
3098 return MIN (ret1, prec);
3099 case SAVE_EXPR:
3100 return tree_ctz (TREE_OPERAND (expr, 0));
3101 case COND_EXPR:
3102 ret1 = tree_ctz (TREE_OPERAND (expr, 1));
3103 if (ret1 == 0)
3104 return 0;
3105 ret2 = tree_ctz (TREE_OPERAND (expr, 2));
3106 return MIN (ret1, ret2);
3107 case COMPOUND_EXPR:
3108 return tree_ctz (TREE_OPERAND (expr, 1));
3109 case ADDR_EXPR:
3110 ret1 = get_pointer_alignment (CONST_CAST_TREE (expr));
3111 if (ret1 > BITS_PER_UNIT)
3112 {
3113 ret1 = ctz_hwi (ret1 / BITS_PER_UNIT);
3114 return MIN (ret1, prec);
3115 }
3116 return 0;
3117 default:
3118 return 0;
3119 }
3120 }
3121
3122 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
3123 decimal float constants, so don't return 1 for them.
3124 Also return 1 for location wrappers around such a constant. */
3125
3126 bool
3127 real_zerop (const_tree expr)
3128 {
3129 STRIP_ANY_LOCATION_WRAPPER (expr);
3130
3131 switch (TREE_CODE (expr))
3132 {
3133 case REAL_CST:
3134 return real_equal (&TREE_REAL_CST (expr), &dconst0)
3135 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
3136 case COMPLEX_CST:
3137 return real_zerop (TREE_REALPART (expr))
3138 && real_zerop (TREE_IMAGPART (expr));
3139 case VECTOR_CST:
3140 {
3141 /* Don't simply check for a duplicate because the predicate
3142 accepts both +0.0 and -0.0. */
3143 unsigned count = vector_cst_encoded_nelts (expr);
3144 for (unsigned int i = 0; i < count; ++i)
3145 if (!real_zerop (VECTOR_CST_ENCODED_ELT (expr, i)))
3146 return false;
3147 return true;
3148 }
3149 default:
3150 return false;
3151 }
3152 }
3153
3154 /* Return 1 if EXPR is the real constant one in real or complex form.
3155 Trailing zeroes matter for decimal float constants, so don't return
3156 1 for them.
3157 Also return 1 for location wrappers around such a constant. */
3158
3159 bool
3160 real_onep (const_tree expr)
3161 {
3162 STRIP_ANY_LOCATION_WRAPPER (expr);
3163
3164 switch (TREE_CODE (expr))
3165 {
3166 case REAL_CST:
3167 return real_equal (&TREE_REAL_CST (expr), &dconst1)
3168 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
3169 case COMPLEX_CST:
3170 return real_onep (TREE_REALPART (expr))
3171 && real_zerop (TREE_IMAGPART (expr));
3172 case VECTOR_CST:
3173 return (VECTOR_CST_NPATTERNS (expr) == 1
3174 && VECTOR_CST_DUPLICATE_P (expr)
3175 && real_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
3176 default:
3177 return false;
3178 }
3179 }
3180
3181 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
3182 matter for decimal float constants, so don't return 1 for them.
3183 Also return 1 for location wrappers around such a constant. */
3184
3185 bool
3186 real_minus_onep (const_tree expr)
3187 {
3188 STRIP_ANY_LOCATION_WRAPPER (expr);
3189
3190 switch (TREE_CODE (expr))
3191 {
3192 case REAL_CST:
3193 return real_equal (&TREE_REAL_CST (expr), &dconstm1)
3194 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
3195 case COMPLEX_CST:
3196 return real_minus_onep (TREE_REALPART (expr))
3197 && real_zerop (TREE_IMAGPART (expr));
3198 case VECTOR_CST:
3199 return (VECTOR_CST_NPATTERNS (expr) == 1
3200 && VECTOR_CST_DUPLICATE_P (expr)
3201 && real_minus_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
3202 default:
3203 return false;
3204 }
3205 }
3206
3207 /* Nonzero if EXP is a constant or a cast of a constant. */
3208
3209 bool
3210 really_constant_p (const_tree exp)
3211 {
3212 /* This is not quite the same as STRIP_NOPS. It does more. */
3213 while (CONVERT_EXPR_P (exp)
3214 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3215 exp = TREE_OPERAND (exp, 0);
3216 return TREE_CONSTANT (exp);
3217 }
3218
3219 /* Return true if T holds a polynomial pointer difference, storing it in
3220 *VALUE if so. A true return means that T's precision is no greater
3221 than 64 bits, which is the largest address space we support, so *VALUE
3222 never loses precision. However, the signedness of the result does
3223 not necessarily match the signedness of T: sometimes an unsigned type
3224 like sizetype is used to encode a value that is actually negative. */
3225
3226 bool
3227 ptrdiff_tree_p (const_tree t, poly_int64_pod *value)
3228 {
3229 if (!t)
3230 return false;
3231 if (TREE_CODE (t) == INTEGER_CST)
3232 {
3233 if (!cst_and_fits_in_hwi (t))
3234 return false;
3235 *value = int_cst_value (t);
3236 return true;
3237 }
3238 if (POLY_INT_CST_P (t))
3239 {
3240 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
3241 if (!cst_and_fits_in_hwi (POLY_INT_CST_COEFF (t, i)))
3242 return false;
3243 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
3244 value->coeffs[i] = int_cst_value (POLY_INT_CST_COEFF (t, i));
3245 return true;
3246 }
3247 return false;
3248 }
3249
3250 poly_int64
3251 tree_to_poly_int64 (const_tree t)
3252 {
3253 gcc_assert (tree_fits_poly_int64_p (t));
3254 if (POLY_INT_CST_P (t))
3255 return poly_int_cst_value (t).force_shwi ();
3256 return TREE_INT_CST_LOW (t);
3257 }
3258
3259 poly_uint64
3260 tree_to_poly_uint64 (const_tree t)
3261 {
3262 gcc_assert (tree_fits_poly_uint64_p (t));
3263 if (POLY_INT_CST_P (t))
3264 return poly_int_cst_value (t).force_uhwi ();
3265 return TREE_INT_CST_LOW (t);
3266 }
3267 \f
3268 /* Return first list element whose TREE_VALUE is ELEM.
3269 Return 0 if ELEM is not in LIST. */
3270
3271 tree
3272 value_member (tree elem, tree list)
3273 {
3274 while (list)
3275 {
3276 if (elem == TREE_VALUE (list))
3277 return list;
3278 list = TREE_CHAIN (list);
3279 }
3280 return NULL_TREE;
3281 }
3282
3283 /* Return first list element whose TREE_PURPOSE is ELEM.
3284 Return 0 if ELEM is not in LIST. */
3285
3286 tree
3287 purpose_member (const_tree elem, tree list)
3288 {
3289 while (list)
3290 {
3291 if (elem == TREE_PURPOSE (list))
3292 return list;
3293 list = TREE_CHAIN (list);
3294 }
3295 return NULL_TREE;
3296 }
3297
3298 /* Return true if ELEM is in V. */
3299
3300 bool
3301 vec_member (const_tree elem, vec<tree, va_gc> *v)
3302 {
3303 unsigned ix;
3304 tree t;
3305 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
3306 if (elem == t)
3307 return true;
3308 return false;
3309 }
3310
3311 /* Returns element number IDX (zero-origin) of chain CHAIN, or
3312 NULL_TREE. */
3313
3314 tree
3315 chain_index (int idx, tree chain)
3316 {
3317 for (; chain && idx > 0; --idx)
3318 chain = TREE_CHAIN (chain);
3319 return chain;
3320 }
3321
3322 /* Return nonzero if ELEM is part of the chain CHAIN. */
3323
3324 bool
3325 chain_member (const_tree elem, const_tree chain)
3326 {
3327 while (chain)
3328 {
3329 if (elem == chain)
3330 return true;
3331 chain = DECL_CHAIN (chain);
3332 }
3333
3334 return false;
3335 }
3336
3337 /* Return the length of a chain of nodes chained through TREE_CHAIN.
3338 We expect a null pointer to mark the end of the chain.
3339 This is the Lisp primitive `length'. */
3340
3341 int
3342 list_length (const_tree t)
3343 {
3344 const_tree p = t;
3345 #ifdef ENABLE_TREE_CHECKING
3346 const_tree q = t;
3347 #endif
3348 int len = 0;
3349
3350 while (p)
3351 {
3352 p = TREE_CHAIN (p);
3353 #ifdef ENABLE_TREE_CHECKING
3354 if (len % 2)
3355 q = TREE_CHAIN (q);
3356 gcc_assert (p != q);
3357 #endif
3358 len++;
3359 }
3360
3361 return len;
3362 }
3363
3364 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
3365 UNION_TYPE TYPE, or NULL_TREE if none. */
3366
3367 tree
3368 first_field (const_tree type)
3369 {
3370 tree t = TYPE_FIELDS (type);
3371 while (t && TREE_CODE (t) != FIELD_DECL)
3372 t = TREE_CHAIN (t);
3373 return t;
3374 }
3375
3376 /* Returns the last FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
3377 UNION_TYPE TYPE, or NULL_TREE if none. */
3378
3379 tree
3380 last_field (const_tree type)
3381 {
3382 tree last = NULL_TREE;
3383
3384 for (tree fld = TYPE_FIELDS (type); fld; fld = TREE_CHAIN (fld))
3385 {
3386 if (TREE_CODE (fld) != FIELD_DECL)
3387 continue;
3388
3389 last = fld;
3390 }
3391
3392 return last;
3393 }
3394
3395 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
3396 by modifying the last node in chain 1 to point to chain 2.
3397 This is the Lisp primitive `nconc'. */
3398
3399 tree
3400 chainon (tree op1, tree op2)
3401 {
3402 tree t1;
3403
3404 if (!op1)
3405 return op2;
3406 if (!op2)
3407 return op1;
3408
3409 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
3410 continue;
3411 TREE_CHAIN (t1) = op2;
3412
3413 #ifdef ENABLE_TREE_CHECKING
3414 {
3415 tree t2;
3416 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
3417 gcc_assert (t2 != t1);
3418 }
3419 #endif
3420
3421 return op1;
3422 }
3423
3424 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
3425
3426 tree
3427 tree_last (tree chain)
3428 {
3429 tree next;
3430 if (chain)
3431 while ((next = TREE_CHAIN (chain)))
3432 chain = next;
3433 return chain;
3434 }
3435
3436 /* Reverse the order of elements in the chain T,
3437 and return the new head of the chain (old last element). */
3438
3439 tree
3440 nreverse (tree t)
3441 {
3442 tree prev = 0, decl, next;
3443 for (decl = t; decl; decl = next)
3444 {
3445 /* We shouldn't be using this function to reverse BLOCK chains; we
3446 have blocks_nreverse for that. */
3447 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
3448 next = TREE_CHAIN (decl);
3449 TREE_CHAIN (decl) = prev;
3450 prev = decl;
3451 }
3452 return prev;
3453 }
3454 \f
3455 /* Return a newly created TREE_LIST node whose
3456 purpose and value fields are PARM and VALUE. */
3457
3458 tree
3459 build_tree_list (tree parm, tree value MEM_STAT_DECL)
3460 {
3461 tree t = make_node (TREE_LIST PASS_MEM_STAT);
3462 TREE_PURPOSE (t) = parm;
3463 TREE_VALUE (t) = value;
3464 return t;
3465 }
3466
3467 /* Build a chain of TREE_LIST nodes from a vector. */
3468
3469 tree
3470 build_tree_list_vec (const vec<tree, va_gc> *vec MEM_STAT_DECL)
3471 {
3472 tree ret = NULL_TREE;
3473 tree *pp = &ret;
3474 unsigned int i;
3475 tree t;
3476 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
3477 {
3478 *pp = build_tree_list (NULL, t PASS_MEM_STAT);
3479 pp = &TREE_CHAIN (*pp);
3480 }
3481 return ret;
3482 }
3483
3484 /* Return a newly created TREE_LIST node whose
3485 purpose and value fields are PURPOSE and VALUE
3486 and whose TREE_CHAIN is CHAIN. */
3487
3488 tree
3489 tree_cons (tree purpose, tree value, tree chain MEM_STAT_DECL)
3490 {
3491 tree node;
3492
3493 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
3494 memset (node, 0, sizeof (struct tree_common));
3495
3496 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
3497
3498 TREE_SET_CODE (node, TREE_LIST);
3499 TREE_CHAIN (node) = chain;
3500 TREE_PURPOSE (node) = purpose;
3501 TREE_VALUE (node) = value;
3502 return node;
3503 }
3504
3505 /* Return the values of the elements of a CONSTRUCTOR as a vector of
3506 trees. */
3507
3508 vec<tree, va_gc> *
3509 ctor_to_vec (tree ctor)
3510 {
3511 vec<tree, va_gc> *vec;
3512 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
3513 unsigned int ix;
3514 tree val;
3515
3516 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
3517 vec->quick_push (val);
3518
3519 return vec;
3520 }
3521 \f
3522 /* Return the size nominally occupied by an object of type TYPE
3523 when it resides in memory. The value is measured in units of bytes,
3524 and its data type is that normally used for type sizes
3525 (which is the first type created by make_signed_type or
3526 make_unsigned_type). */
3527
3528 tree
3529 size_in_bytes_loc (location_t loc, const_tree type)
3530 {
3531 tree t;
3532
3533 if (type == error_mark_node)
3534 return integer_zero_node;
3535
3536 type = TYPE_MAIN_VARIANT (type);
3537 t = TYPE_SIZE_UNIT (type);
3538
3539 if (t == 0)
3540 {
3541 lang_hooks.types.incomplete_type_error (loc, NULL_TREE, type);
3542 return size_zero_node;
3543 }
3544
3545 return t;
3546 }
3547
3548 /* Return the size of TYPE (in bytes) as a wide integer
3549 or return -1 if the size can vary or is larger than an integer. */
3550
3551 HOST_WIDE_INT
3552 int_size_in_bytes (const_tree type)
3553 {
3554 tree t;
3555
3556 if (type == error_mark_node)
3557 return 0;
3558
3559 type = TYPE_MAIN_VARIANT (type);
3560 t = TYPE_SIZE_UNIT (type);
3561
3562 if (t && tree_fits_uhwi_p (t))
3563 return TREE_INT_CST_LOW (t);
3564 else
3565 return -1;
3566 }
3567
3568 /* Return the maximum size of TYPE (in bytes) as a wide integer
3569 or return -1 if the size can vary or is larger than an integer. */
3570
3571 HOST_WIDE_INT
3572 max_int_size_in_bytes (const_tree type)
3573 {
3574 HOST_WIDE_INT size = -1;
3575 tree size_tree;
3576
3577 /* If this is an array type, check for a possible MAX_SIZE attached. */
3578
3579 if (TREE_CODE (type) == ARRAY_TYPE)
3580 {
3581 size_tree = TYPE_ARRAY_MAX_SIZE (type);
3582
3583 if (size_tree && tree_fits_uhwi_p (size_tree))
3584 size = tree_to_uhwi (size_tree);
3585 }
3586
3587 /* If we still haven't been able to get a size, see if the language
3588 can compute a maximum size. */
3589
3590 if (size == -1)
3591 {
3592 size_tree = lang_hooks.types.max_size (type);
3593
3594 if (size_tree && tree_fits_uhwi_p (size_tree))
3595 size = tree_to_uhwi (size_tree);
3596 }
3597
3598 return size;
3599 }
3600 \f
3601 /* Return the bit position of FIELD, in bits from the start of the record.
3602 This is a tree of type bitsizetype. */
3603
3604 tree
3605 bit_position (const_tree field)
3606 {
3607 return bit_from_pos (DECL_FIELD_OFFSET (field),
3608 DECL_FIELD_BIT_OFFSET (field));
3609 }
3610 \f
3611 /* Return the byte position of FIELD, in bytes from the start of the record.
3612 This is a tree of type sizetype. */
3613
3614 tree
3615 byte_position (const_tree field)
3616 {
3617 return byte_from_pos (DECL_FIELD_OFFSET (field),
3618 DECL_FIELD_BIT_OFFSET (field));
3619 }
3620
3621 /* Likewise, but return as an integer. It must be representable in
3622 that way (since it could be a signed value, we don't have the
3623 option of returning -1 like int_size_in_byte can. */
3624
3625 HOST_WIDE_INT
3626 int_byte_position (const_tree field)
3627 {
3628 return tree_to_shwi (byte_position (field));
3629 }
3630 \f
3631 /* Return, as a tree node, the number of elements for TYPE (which is an
3632 ARRAY_TYPE) minus one. This counts only elements of the top array. */
3633
3634 tree
3635 array_type_nelts (const_tree type)
3636 {
3637 tree index_type, min, max;
3638
3639 /* If they did it with unspecified bounds, then we should have already
3640 given an error about it before we got here. */
3641 if (! TYPE_DOMAIN (type))
3642 return error_mark_node;
3643
3644 index_type = TYPE_DOMAIN (type);
3645 min = TYPE_MIN_VALUE (index_type);
3646 max = TYPE_MAX_VALUE (index_type);
3647
3648 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
3649 if (!max)
3650 {
3651 /* zero sized arrays are represented from C FE as complete types with
3652 NULL TYPE_MAX_VALUE and zero TYPE_SIZE, while C++ FE represents
3653 them as min 0, max -1. */
3654 if (COMPLETE_TYPE_P (type)
3655 && integer_zerop (TYPE_SIZE (type))
3656 && integer_zerop (min))
3657 return build_int_cst (TREE_TYPE (min), -1);
3658
3659 return error_mark_node;
3660 }
3661
3662 return (integer_zerop (min)
3663 ? max
3664 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
3665 }
3666 \f
3667 /* If arg is static -- a reference to an object in static storage -- then
3668 return the object. This is not the same as the C meaning of `static'.
3669 If arg isn't static, return NULL. */
3670
3671 tree
3672 staticp (tree arg)
3673 {
3674 switch (TREE_CODE (arg))
3675 {
3676 case FUNCTION_DECL:
3677 /* Nested functions are static, even though taking their address will
3678 involve a trampoline as we unnest the nested function and create
3679 the trampoline on the tree level. */
3680 return arg;
3681
3682 case VAR_DECL:
3683 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3684 && ! DECL_THREAD_LOCAL_P (arg)
3685 && ! DECL_DLLIMPORT_P (arg)
3686 ? arg : NULL);
3687
3688 case CONST_DECL:
3689 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3690 ? arg : NULL);
3691
3692 case CONSTRUCTOR:
3693 return TREE_STATIC (arg) ? arg : NULL;
3694
3695 case LABEL_DECL:
3696 case STRING_CST:
3697 return arg;
3698
3699 case COMPONENT_REF:
3700 /* If the thing being referenced is not a field, then it is
3701 something language specific. */
3702 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
3703
3704 /* If we are referencing a bitfield, we can't evaluate an
3705 ADDR_EXPR at compile time and so it isn't a constant. */
3706 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
3707 return NULL;
3708
3709 return staticp (TREE_OPERAND (arg, 0));
3710
3711 case BIT_FIELD_REF:
3712 return NULL;
3713
3714 case INDIRECT_REF:
3715 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
3716
3717 case ARRAY_REF:
3718 case ARRAY_RANGE_REF:
3719 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
3720 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
3721 return staticp (TREE_OPERAND (arg, 0));
3722 else
3723 return NULL;
3724
3725 case COMPOUND_LITERAL_EXPR:
3726 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
3727
3728 default:
3729 return NULL;
3730 }
3731 }
3732
3733 \f
3734
3735
3736 /* Return whether OP is a DECL whose address is function-invariant. */
3737
3738 bool
3739 decl_address_invariant_p (const_tree op)
3740 {
3741 /* The conditions below are slightly less strict than the one in
3742 staticp. */
3743
3744 switch (TREE_CODE (op))
3745 {
3746 case PARM_DECL:
3747 case RESULT_DECL:
3748 case LABEL_DECL:
3749 case FUNCTION_DECL:
3750 return true;
3751
3752 case VAR_DECL:
3753 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3754 || DECL_THREAD_LOCAL_P (op)
3755 || DECL_CONTEXT (op) == current_function_decl
3756 || decl_function_context (op) == current_function_decl)
3757 return true;
3758 break;
3759
3760 case CONST_DECL:
3761 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3762 || decl_function_context (op) == current_function_decl)
3763 return true;
3764 break;
3765
3766 default:
3767 break;
3768 }
3769
3770 return false;
3771 }
3772
3773 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
3774
3775 bool
3776 decl_address_ip_invariant_p (const_tree op)
3777 {
3778 /* The conditions below are slightly less strict than the one in
3779 staticp. */
3780
3781 switch (TREE_CODE (op))
3782 {
3783 case LABEL_DECL:
3784 case FUNCTION_DECL:
3785 case STRING_CST:
3786 return true;
3787
3788 case VAR_DECL:
3789 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
3790 && !DECL_DLLIMPORT_P (op))
3791 || DECL_THREAD_LOCAL_P (op))
3792 return true;
3793 break;
3794
3795 case CONST_DECL:
3796 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
3797 return true;
3798 break;
3799
3800 default:
3801 break;
3802 }
3803
3804 return false;
3805 }
3806
3807
3808 /* Return true if T is function-invariant (internal function, does
3809 not handle arithmetic; that's handled in skip_simple_arithmetic and
3810 tree_invariant_p). */
3811
3812 static bool
3813 tree_invariant_p_1 (tree t)
3814 {
3815 tree op;
3816
3817 if (TREE_CONSTANT (t)
3818 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
3819 return true;
3820
3821 switch (TREE_CODE (t))
3822 {
3823 case SAVE_EXPR:
3824 return true;
3825
3826 case ADDR_EXPR:
3827 op = TREE_OPERAND (t, 0);
3828 while (handled_component_p (op))
3829 {
3830 switch (TREE_CODE (op))
3831 {
3832 case ARRAY_REF:
3833 case ARRAY_RANGE_REF:
3834 if (!tree_invariant_p (TREE_OPERAND (op, 1))
3835 || TREE_OPERAND (op, 2) != NULL_TREE
3836 || TREE_OPERAND (op, 3) != NULL_TREE)
3837 return false;
3838 break;
3839
3840 case COMPONENT_REF:
3841 if (TREE_OPERAND (op, 2) != NULL_TREE)
3842 return false;
3843 break;
3844
3845 default:;
3846 }
3847 op = TREE_OPERAND (op, 0);
3848 }
3849
3850 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
3851
3852 default:
3853 break;
3854 }
3855
3856 return false;
3857 }
3858
3859 /* Return true if T is function-invariant. */
3860
3861 bool
3862 tree_invariant_p (tree t)
3863 {
3864 tree inner = skip_simple_arithmetic (t);
3865 return tree_invariant_p_1 (inner);
3866 }
3867
3868 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
3869 Do this to any expression which may be used in more than one place,
3870 but must be evaluated only once.
3871
3872 Normally, expand_expr would reevaluate the expression each time.
3873 Calling save_expr produces something that is evaluated and recorded
3874 the first time expand_expr is called on it. Subsequent calls to
3875 expand_expr just reuse the recorded value.
3876
3877 The call to expand_expr that generates code that actually computes
3878 the value is the first call *at compile time*. Subsequent calls
3879 *at compile time* generate code to use the saved value.
3880 This produces correct result provided that *at run time* control
3881 always flows through the insns made by the first expand_expr
3882 before reaching the other places where the save_expr was evaluated.
3883 You, the caller of save_expr, must make sure this is so.
3884
3885 Constants, and certain read-only nodes, are returned with no
3886 SAVE_EXPR because that is safe. Expressions containing placeholders
3887 are not touched; see tree.def for an explanation of what these
3888 are used for. */
3889
3890 tree
3891 save_expr (tree expr)
3892 {
3893 tree inner;
3894
3895 /* If the tree evaluates to a constant, then we don't want to hide that
3896 fact (i.e. this allows further folding, and direct checks for constants).
3897 However, a read-only object that has side effects cannot be bypassed.
3898 Since it is no problem to reevaluate literals, we just return the
3899 literal node. */
3900 inner = skip_simple_arithmetic (expr);
3901 if (TREE_CODE (inner) == ERROR_MARK)
3902 return inner;
3903
3904 if (tree_invariant_p_1 (inner))
3905 return expr;
3906
3907 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
3908 it means that the size or offset of some field of an object depends on
3909 the value within another field.
3910
3911 Note that it must not be the case that EXPR contains both a PLACEHOLDER_EXPR
3912 and some variable since it would then need to be both evaluated once and
3913 evaluated more than once. Front-ends must assure this case cannot
3914 happen by surrounding any such subexpressions in their own SAVE_EXPR
3915 and forcing evaluation at the proper time. */
3916 if (contains_placeholder_p (inner))
3917 return expr;
3918
3919 expr = build1_loc (EXPR_LOCATION (expr), SAVE_EXPR, TREE_TYPE (expr), expr);
3920
3921 /* This expression might be placed ahead of a jump to ensure that the
3922 value was computed on both sides of the jump. So make sure it isn't
3923 eliminated as dead. */
3924 TREE_SIDE_EFFECTS (expr) = 1;
3925 return expr;
3926 }
3927
3928 /* Look inside EXPR into any simple arithmetic operations. Return the
3929 outermost non-arithmetic or non-invariant node. */
3930
3931 tree
3932 skip_simple_arithmetic (tree expr)
3933 {
3934 /* We don't care about whether this can be used as an lvalue in this
3935 context. */
3936 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3937 expr = TREE_OPERAND (expr, 0);
3938
3939 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
3940 a constant, it will be more efficient to not make another SAVE_EXPR since
3941 it will allow better simplification and GCSE will be able to merge the
3942 computations if they actually occur. */
3943 while (true)
3944 {
3945 if (UNARY_CLASS_P (expr))
3946 expr = TREE_OPERAND (expr, 0);
3947 else if (BINARY_CLASS_P (expr))
3948 {
3949 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
3950 expr = TREE_OPERAND (expr, 0);
3951 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
3952 expr = TREE_OPERAND (expr, 1);
3953 else
3954 break;
3955 }
3956 else
3957 break;
3958 }
3959
3960 return expr;
3961 }
3962
3963 /* Look inside EXPR into simple arithmetic operations involving constants.
3964 Return the outermost non-arithmetic or non-constant node. */
3965
3966 tree
3967 skip_simple_constant_arithmetic (tree expr)
3968 {
3969 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3970 expr = TREE_OPERAND (expr, 0);
3971
3972 while (true)
3973 {
3974 if (UNARY_CLASS_P (expr))
3975 expr = TREE_OPERAND (expr, 0);
3976 else if (BINARY_CLASS_P (expr))
3977 {
3978 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3979 expr = TREE_OPERAND (expr, 0);
3980 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3981 expr = TREE_OPERAND (expr, 1);
3982 else
3983 break;
3984 }
3985 else
3986 break;
3987 }
3988
3989 return expr;
3990 }
3991
3992 /* Return which tree structure is used by T. */
3993
3994 enum tree_node_structure_enum
3995 tree_node_structure (const_tree t)
3996 {
3997 const enum tree_code code = TREE_CODE (t);
3998 return tree_node_structure_for_code (code);
3999 }
4000
4001 /* Set various status flags when building a CALL_EXPR object T. */
4002
4003 static void
4004 process_call_operands (tree t)
4005 {
4006 bool side_effects = TREE_SIDE_EFFECTS (t);
4007 bool read_only = false;
4008 int i = call_expr_flags (t);
4009
4010 /* Calls have side-effects, except those to const or pure functions. */
4011 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
4012 side_effects = true;
4013 /* Propagate TREE_READONLY of arguments for const functions. */
4014 if (i & ECF_CONST)
4015 read_only = true;
4016
4017 if (!side_effects || read_only)
4018 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
4019 {
4020 tree op = TREE_OPERAND (t, i);
4021 if (op && TREE_SIDE_EFFECTS (op))
4022 side_effects = true;
4023 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
4024 read_only = false;
4025 }
4026
4027 TREE_SIDE_EFFECTS (t) = side_effects;
4028 TREE_READONLY (t) = read_only;
4029 }
4030 \f
4031 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
4032 size or offset that depends on a field within a record. */
4033
4034 bool
4035 contains_placeholder_p (const_tree exp)
4036 {
4037 enum tree_code code;
4038
4039 if (!exp)
4040 return 0;
4041
4042 code = TREE_CODE (exp);
4043 if (code == PLACEHOLDER_EXPR)
4044 return 1;
4045
4046 switch (TREE_CODE_CLASS (code))
4047 {
4048 case tcc_reference:
4049 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
4050 position computations since they will be converted into a
4051 WITH_RECORD_EXPR involving the reference, which will assume
4052 here will be valid. */
4053 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
4054
4055 case tcc_exceptional:
4056 if (code == TREE_LIST)
4057 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
4058 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
4059 break;
4060
4061 case tcc_unary:
4062 case tcc_binary:
4063 case tcc_comparison:
4064 case tcc_expression:
4065 switch (code)
4066 {
4067 case COMPOUND_EXPR:
4068 /* Ignoring the first operand isn't quite right, but works best. */
4069 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
4070
4071 case COND_EXPR:
4072 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
4073 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
4074 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
4075
4076 case SAVE_EXPR:
4077 /* The save_expr function never wraps anything containing
4078 a PLACEHOLDER_EXPR. */
4079 return 0;
4080
4081 default:
4082 break;
4083 }
4084
4085 switch (TREE_CODE_LENGTH (code))
4086 {
4087 case 1:
4088 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
4089 case 2:
4090 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
4091 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
4092 default:
4093 return 0;
4094 }
4095
4096 case tcc_vl_exp:
4097 switch (code)
4098 {
4099 case CALL_EXPR:
4100 {
4101 const_tree arg;
4102 const_call_expr_arg_iterator iter;
4103 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
4104 if (CONTAINS_PLACEHOLDER_P (arg))
4105 return 1;
4106 return 0;
4107 }
4108 default:
4109 return 0;
4110 }
4111
4112 default:
4113 return 0;
4114 }
4115 return 0;
4116 }
4117
4118 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
4119 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
4120 field positions. */
4121
4122 static bool
4123 type_contains_placeholder_1 (const_tree type)
4124 {
4125 /* If the size contains a placeholder or the parent type (component type in
4126 the case of arrays) type involves a placeholder, this type does. */
4127 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
4128 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
4129 || (!POINTER_TYPE_P (type)
4130 && TREE_TYPE (type)
4131 && type_contains_placeholder_p (TREE_TYPE (type))))
4132 return true;
4133
4134 /* Now do type-specific checks. Note that the last part of the check above
4135 greatly limits what we have to do below. */
4136 switch (TREE_CODE (type))
4137 {
4138 case VOID_TYPE:
4139 case OPAQUE_TYPE:
4140 case COMPLEX_TYPE:
4141 case ENUMERAL_TYPE:
4142 case BOOLEAN_TYPE:
4143 case POINTER_TYPE:
4144 case OFFSET_TYPE:
4145 case REFERENCE_TYPE:
4146 case METHOD_TYPE:
4147 case FUNCTION_TYPE:
4148 case VECTOR_TYPE:
4149 case NULLPTR_TYPE:
4150 return false;
4151
4152 case INTEGER_TYPE:
4153 case REAL_TYPE:
4154 case FIXED_POINT_TYPE:
4155 /* Here we just check the bounds. */
4156 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
4157 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
4158
4159 case ARRAY_TYPE:
4160 /* We have already checked the component type above, so just check
4161 the domain type. Flexible array members have a null domain. */
4162 return TYPE_DOMAIN (type) ?
4163 type_contains_placeholder_p (TYPE_DOMAIN (type)) : false;
4164
4165 case RECORD_TYPE:
4166 case UNION_TYPE:
4167 case QUAL_UNION_TYPE:
4168 {
4169 tree field;
4170
4171 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4172 if (TREE_CODE (field) == FIELD_DECL
4173 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
4174 || (TREE_CODE (type) == QUAL_UNION_TYPE
4175 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
4176 || type_contains_placeholder_p (TREE_TYPE (field))))
4177 return true;
4178
4179 return false;
4180 }
4181
4182 default:
4183 gcc_unreachable ();
4184 }
4185 }
4186
4187 /* Wrapper around above function used to cache its result. */
4188
4189 bool
4190 type_contains_placeholder_p (tree type)
4191 {
4192 bool result;
4193
4194 /* If the contains_placeholder_bits field has been initialized,
4195 then we know the answer. */
4196 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
4197 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
4198
4199 /* Indicate that we've seen this type node, and the answer is false.
4200 This is what we want to return if we run into recursion via fields. */
4201 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
4202
4203 /* Compute the real value. */
4204 result = type_contains_placeholder_1 (type);
4205
4206 /* Store the real value. */
4207 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
4208
4209 return result;
4210 }
4211 \f
4212 /* Push tree EXP onto vector QUEUE if it is not already present. */
4213
4214 static void
4215 push_without_duplicates (tree exp, vec<tree> *queue)
4216 {
4217 unsigned int i;
4218 tree iter;
4219
4220 FOR_EACH_VEC_ELT (*queue, i, iter)
4221 if (simple_cst_equal (iter, exp) == 1)
4222 break;
4223
4224 if (!iter)
4225 queue->safe_push (exp);
4226 }
4227
4228 /* Given a tree EXP, find all occurrences of references to fields
4229 in a PLACEHOLDER_EXPR and place them in vector REFS without
4230 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
4231 we assume here that EXP contains only arithmetic expressions
4232 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
4233 argument list. */
4234
4235 void
4236 find_placeholder_in_expr (tree exp, vec<tree> *refs)
4237 {
4238 enum tree_code code = TREE_CODE (exp);
4239 tree inner;
4240 int i;
4241
4242 /* We handle TREE_LIST and COMPONENT_REF separately. */
4243 if (code == TREE_LIST)
4244 {
4245 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
4246 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
4247 }
4248 else if (code == COMPONENT_REF)
4249 {
4250 for (inner = TREE_OPERAND (exp, 0);
4251 REFERENCE_CLASS_P (inner);
4252 inner = TREE_OPERAND (inner, 0))
4253 ;
4254
4255 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
4256 push_without_duplicates (exp, refs);
4257 else
4258 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
4259 }
4260 else
4261 switch (TREE_CODE_CLASS (code))
4262 {
4263 case tcc_constant:
4264 break;
4265
4266 case tcc_declaration:
4267 /* Variables allocated to static storage can stay. */
4268 if (!TREE_STATIC (exp))
4269 push_without_duplicates (exp, refs);
4270 break;
4271
4272 case tcc_expression:
4273 /* This is the pattern built in ada/make_aligning_type. */
4274 if (code == ADDR_EXPR
4275 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
4276 {
4277 push_without_duplicates (exp, refs);
4278 break;
4279 }
4280
4281 /* Fall through. */
4282
4283 case tcc_exceptional:
4284 case tcc_unary:
4285 case tcc_binary:
4286 case tcc_comparison:
4287 case tcc_reference:
4288 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
4289 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
4290 break;
4291
4292 case tcc_vl_exp:
4293 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4294 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
4295 break;
4296
4297 default:
4298 gcc_unreachable ();
4299 }
4300 }
4301
4302 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
4303 return a tree with all occurrences of references to F in a
4304 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
4305 CONST_DECLs. Note that we assume here that EXP contains only
4306 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
4307 occurring only in their argument list. */
4308
4309 tree
4310 substitute_in_expr (tree exp, tree f, tree r)
4311 {
4312 enum tree_code code = TREE_CODE (exp);
4313 tree op0, op1, op2, op3;
4314 tree new_tree;
4315
4316 /* We handle TREE_LIST and COMPONENT_REF separately. */
4317 if (code == TREE_LIST)
4318 {
4319 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
4320 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
4321 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
4322 return exp;
4323
4324 return tree_cons (TREE_PURPOSE (exp), op1, op0);
4325 }
4326 else if (code == COMPONENT_REF)
4327 {
4328 tree inner;
4329
4330 /* If this expression is getting a value from a PLACEHOLDER_EXPR
4331 and it is the right field, replace it with R. */
4332 for (inner = TREE_OPERAND (exp, 0);
4333 REFERENCE_CLASS_P (inner);
4334 inner = TREE_OPERAND (inner, 0))
4335 ;
4336
4337 /* The field. */
4338 op1 = TREE_OPERAND (exp, 1);
4339
4340 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
4341 return r;
4342
4343 /* If this expression hasn't been completed let, leave it alone. */
4344 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
4345 return exp;
4346
4347 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4348 if (op0 == TREE_OPERAND (exp, 0))
4349 return exp;
4350
4351 new_tree
4352 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
4353 }
4354 else
4355 switch (TREE_CODE_CLASS (code))
4356 {
4357 case tcc_constant:
4358 return exp;
4359
4360 case tcc_declaration:
4361 if (exp == f)
4362 return r;
4363 else
4364 return exp;
4365
4366 case tcc_expression:
4367 if (exp == f)
4368 return r;
4369
4370 /* Fall through. */
4371
4372 case tcc_exceptional:
4373 case tcc_unary:
4374 case tcc_binary:
4375 case tcc_comparison:
4376 case tcc_reference:
4377 switch (TREE_CODE_LENGTH (code))
4378 {
4379 case 0:
4380 return exp;
4381
4382 case 1:
4383 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4384 if (op0 == TREE_OPERAND (exp, 0))
4385 return exp;
4386
4387 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
4388 break;
4389
4390 case 2:
4391 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4392 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4393
4394 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
4395 return exp;
4396
4397 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
4398 break;
4399
4400 case 3:
4401 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4402 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4403 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
4404
4405 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4406 && op2 == TREE_OPERAND (exp, 2))
4407 return exp;
4408
4409 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
4410 break;
4411
4412 case 4:
4413 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4414 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4415 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
4416 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
4417
4418 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4419 && op2 == TREE_OPERAND (exp, 2)
4420 && op3 == TREE_OPERAND (exp, 3))
4421 return exp;
4422
4423 new_tree
4424 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
4425 break;
4426
4427 default:
4428 gcc_unreachable ();
4429 }
4430 break;
4431
4432 case tcc_vl_exp:
4433 {
4434 int i;
4435
4436 new_tree = NULL_TREE;
4437
4438 /* If we are trying to replace F with a constant or with another
4439 instance of one of the arguments of the call, inline back
4440 functions which do nothing else than computing a value from
4441 the arguments they are passed. This makes it possible to
4442 fold partially or entirely the replacement expression. */
4443 if (code == CALL_EXPR)
4444 {
4445 bool maybe_inline = false;
4446 if (CONSTANT_CLASS_P (r))
4447 maybe_inline = true;
4448 else
4449 for (i = 3; i < TREE_OPERAND_LENGTH (exp); i++)
4450 if (operand_equal_p (TREE_OPERAND (exp, i), r, 0))
4451 {
4452 maybe_inline = true;
4453 break;
4454 }
4455 if (maybe_inline)
4456 {
4457 tree t = maybe_inline_call_in_expr (exp);
4458 if (t)
4459 return SUBSTITUTE_IN_EXPR (t, f, r);
4460 }
4461 }
4462
4463 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4464 {
4465 tree op = TREE_OPERAND (exp, i);
4466 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
4467 if (new_op != op)
4468 {
4469 if (!new_tree)
4470 new_tree = copy_node (exp);
4471 TREE_OPERAND (new_tree, i) = new_op;
4472 }
4473 }
4474
4475 if (new_tree)
4476 {
4477 new_tree = fold (new_tree);
4478 if (TREE_CODE (new_tree) == CALL_EXPR)
4479 process_call_operands (new_tree);
4480 }
4481 else
4482 return exp;
4483 }
4484 break;
4485
4486 default:
4487 gcc_unreachable ();
4488 }
4489
4490 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4491
4492 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4493 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4494
4495 return new_tree;
4496 }
4497
4498 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
4499 for it within OBJ, a tree that is an object or a chain of references. */
4500
4501 tree
4502 substitute_placeholder_in_expr (tree exp, tree obj)
4503 {
4504 enum tree_code code = TREE_CODE (exp);
4505 tree op0, op1, op2, op3;
4506 tree new_tree;
4507
4508 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
4509 in the chain of OBJ. */
4510 if (code == PLACEHOLDER_EXPR)
4511 {
4512 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
4513 tree elt;
4514
4515 for (elt = obj; elt != 0;
4516 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
4517 || TREE_CODE (elt) == COND_EXPR)
4518 ? TREE_OPERAND (elt, 1)
4519 : (REFERENCE_CLASS_P (elt)
4520 || UNARY_CLASS_P (elt)
4521 || BINARY_CLASS_P (elt)
4522 || VL_EXP_CLASS_P (elt)
4523 || EXPRESSION_CLASS_P (elt))
4524 ? TREE_OPERAND (elt, 0) : 0))
4525 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
4526 return elt;
4527
4528 for (elt = obj; elt != 0;
4529 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
4530 || TREE_CODE (elt) == COND_EXPR)
4531 ? TREE_OPERAND (elt, 1)
4532 : (REFERENCE_CLASS_P (elt)
4533 || UNARY_CLASS_P (elt)
4534 || BINARY_CLASS_P (elt)
4535 || VL_EXP_CLASS_P (elt)
4536 || EXPRESSION_CLASS_P (elt))
4537 ? TREE_OPERAND (elt, 0) : 0))
4538 if (POINTER_TYPE_P (TREE_TYPE (elt))
4539 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
4540 == need_type))
4541 return fold_build1 (INDIRECT_REF, need_type, elt);
4542
4543 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
4544 survives until RTL generation, there will be an error. */
4545 return exp;
4546 }
4547
4548 /* TREE_LIST is special because we need to look at TREE_VALUE
4549 and TREE_CHAIN, not TREE_OPERANDS. */
4550 else if (code == TREE_LIST)
4551 {
4552 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
4553 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
4554 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
4555 return exp;
4556
4557 return tree_cons (TREE_PURPOSE (exp), op1, op0);
4558 }
4559 else
4560 switch (TREE_CODE_CLASS (code))
4561 {
4562 case tcc_constant:
4563 case tcc_declaration:
4564 return exp;
4565
4566 case tcc_exceptional:
4567 case tcc_unary:
4568 case tcc_binary:
4569 case tcc_comparison:
4570 case tcc_expression:
4571 case tcc_reference:
4572 case tcc_statement:
4573 switch (TREE_CODE_LENGTH (code))
4574 {
4575 case 0:
4576 return exp;
4577
4578 case 1:
4579 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4580 if (op0 == TREE_OPERAND (exp, 0))
4581 return exp;
4582
4583 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
4584 break;
4585
4586 case 2:
4587 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4588 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4589
4590 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
4591 return exp;
4592
4593 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
4594 break;
4595
4596 case 3:
4597 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4598 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4599 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
4600
4601 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4602 && op2 == TREE_OPERAND (exp, 2))
4603 return exp;
4604
4605 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
4606 break;
4607
4608 case 4:
4609 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4610 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4611 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
4612 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
4613
4614 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4615 && op2 == TREE_OPERAND (exp, 2)
4616 && op3 == TREE_OPERAND (exp, 3))
4617 return exp;
4618
4619 new_tree
4620 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
4621 break;
4622
4623 default:
4624 gcc_unreachable ();
4625 }
4626 break;
4627
4628 case tcc_vl_exp:
4629 {
4630 int i;
4631
4632 new_tree = NULL_TREE;
4633
4634 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4635 {
4636 tree op = TREE_OPERAND (exp, i);
4637 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
4638 if (new_op != op)
4639 {
4640 if (!new_tree)
4641 new_tree = copy_node (exp);
4642 TREE_OPERAND (new_tree, i) = new_op;
4643 }
4644 }
4645
4646 if (new_tree)
4647 {
4648 new_tree = fold (new_tree);
4649 if (TREE_CODE (new_tree) == CALL_EXPR)
4650 process_call_operands (new_tree);
4651 }
4652 else
4653 return exp;
4654 }
4655 break;
4656
4657 default:
4658 gcc_unreachable ();
4659 }
4660
4661 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4662
4663 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4664 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4665
4666 return new_tree;
4667 }
4668 \f
4669
4670 /* Subroutine of stabilize_reference; this is called for subtrees of
4671 references. Any expression with side-effects must be put in a SAVE_EXPR
4672 to ensure that it is only evaluated once.
4673
4674 We don't put SAVE_EXPR nodes around everything, because assigning very
4675 simple expressions to temporaries causes us to miss good opportunities
4676 for optimizations. Among other things, the opportunity to fold in the
4677 addition of a constant into an addressing mode often gets lost, e.g.
4678 "y[i+1] += x;". In general, we take the approach that we should not make
4679 an assignment unless we are forced into it - i.e., that any non-side effect
4680 operator should be allowed, and that cse should take care of coalescing
4681 multiple utterances of the same expression should that prove fruitful. */
4682
4683 static tree
4684 stabilize_reference_1 (tree e)
4685 {
4686 tree result;
4687 enum tree_code code = TREE_CODE (e);
4688
4689 /* We cannot ignore const expressions because it might be a reference
4690 to a const array but whose index contains side-effects. But we can
4691 ignore things that are actual constant or that already have been
4692 handled by this function. */
4693
4694 if (tree_invariant_p (e))
4695 return e;
4696
4697 switch (TREE_CODE_CLASS (code))
4698 {
4699 case tcc_exceptional:
4700 /* Always wrap STATEMENT_LIST into SAVE_EXPR, even if it doesn't
4701 have side-effects. */
4702 if (code == STATEMENT_LIST)
4703 return save_expr (e);
4704 /* FALLTHRU */
4705 case tcc_type:
4706 case tcc_declaration:
4707 case tcc_comparison:
4708 case tcc_statement:
4709 case tcc_expression:
4710 case tcc_reference:
4711 case tcc_vl_exp:
4712 /* If the expression has side-effects, then encase it in a SAVE_EXPR
4713 so that it will only be evaluated once. */
4714 /* The reference (r) and comparison (<) classes could be handled as
4715 below, but it is generally faster to only evaluate them once. */
4716 if (TREE_SIDE_EFFECTS (e))
4717 return save_expr (e);
4718 return e;
4719
4720 case tcc_constant:
4721 /* Constants need no processing. In fact, we should never reach
4722 here. */
4723 return e;
4724
4725 case tcc_binary:
4726 /* Division is slow and tends to be compiled with jumps,
4727 especially the division by powers of 2 that is often
4728 found inside of an array reference. So do it just once. */
4729 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
4730 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
4731 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
4732 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
4733 return save_expr (e);
4734 /* Recursively stabilize each operand. */
4735 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
4736 stabilize_reference_1 (TREE_OPERAND (e, 1)));
4737 break;
4738
4739 case tcc_unary:
4740 /* Recursively stabilize each operand. */
4741 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
4742 break;
4743
4744 default:
4745 gcc_unreachable ();
4746 }
4747
4748 TREE_TYPE (result) = TREE_TYPE (e);
4749 TREE_READONLY (result) = TREE_READONLY (e);
4750 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
4751 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
4752
4753 return result;
4754 }
4755
4756 /* Stabilize a reference so that we can use it any number of times
4757 without causing its operands to be evaluated more than once.
4758 Returns the stabilized reference. This works by means of save_expr,
4759 so see the caveats in the comments about save_expr.
4760
4761 Also allows conversion expressions whose operands are references.
4762 Any other kind of expression is returned unchanged. */
4763
4764 tree
4765 stabilize_reference (tree ref)
4766 {
4767 tree result;
4768 enum tree_code code = TREE_CODE (ref);
4769
4770 switch (code)
4771 {
4772 case VAR_DECL:
4773 case PARM_DECL:
4774 case RESULT_DECL:
4775 /* No action is needed in this case. */
4776 return ref;
4777
4778 CASE_CONVERT:
4779 case FLOAT_EXPR:
4780 case FIX_TRUNC_EXPR:
4781 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
4782 break;
4783
4784 case INDIRECT_REF:
4785 result = build_nt (INDIRECT_REF,
4786 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
4787 break;
4788
4789 case COMPONENT_REF:
4790 result = build_nt (COMPONENT_REF,
4791 stabilize_reference (TREE_OPERAND (ref, 0)),
4792 TREE_OPERAND (ref, 1), NULL_TREE);
4793 break;
4794
4795 case BIT_FIELD_REF:
4796 result = build_nt (BIT_FIELD_REF,
4797 stabilize_reference (TREE_OPERAND (ref, 0)),
4798 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
4799 REF_REVERSE_STORAGE_ORDER (result) = REF_REVERSE_STORAGE_ORDER (ref);
4800 break;
4801
4802 case ARRAY_REF:
4803 result = build_nt (ARRAY_REF,
4804 stabilize_reference (TREE_OPERAND (ref, 0)),
4805 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4806 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4807 break;
4808
4809 case ARRAY_RANGE_REF:
4810 result = build_nt (ARRAY_RANGE_REF,
4811 stabilize_reference (TREE_OPERAND (ref, 0)),
4812 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4813 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4814 break;
4815
4816 case COMPOUND_EXPR:
4817 /* We cannot wrap the first expression in a SAVE_EXPR, as then
4818 it wouldn't be ignored. This matters when dealing with
4819 volatiles. */
4820 return stabilize_reference_1 (ref);
4821
4822 /* If arg isn't a kind of lvalue we recognize, make no change.
4823 Caller should recognize the error for an invalid lvalue. */
4824 default:
4825 return ref;
4826
4827 case ERROR_MARK:
4828 return error_mark_node;
4829 }
4830
4831 TREE_TYPE (result) = TREE_TYPE (ref);
4832 TREE_READONLY (result) = TREE_READONLY (ref);
4833 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
4834 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
4835 protected_set_expr_location (result, EXPR_LOCATION (ref));
4836
4837 return result;
4838 }
4839 \f
4840 /* Low-level constructors for expressions. */
4841
4842 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
4843 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
4844
4845 void
4846 recompute_tree_invariant_for_addr_expr (tree t)
4847 {
4848 tree node;
4849 bool tc = true, se = false;
4850
4851 gcc_assert (TREE_CODE (t) == ADDR_EXPR);
4852
4853 /* We started out assuming this address is both invariant and constant, but
4854 does not have side effects. Now go down any handled components and see if
4855 any of them involve offsets that are either non-constant or non-invariant.
4856 Also check for side-effects.
4857
4858 ??? Note that this code makes no attempt to deal with the case where
4859 taking the address of something causes a copy due to misalignment. */
4860
4861 #define UPDATE_FLAGS(NODE) \
4862 do { tree _node = (NODE); \
4863 if (_node && !TREE_CONSTANT (_node)) tc = false; \
4864 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
4865
4866 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
4867 node = TREE_OPERAND (node, 0))
4868 {
4869 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
4870 array reference (probably made temporarily by the G++ front end),
4871 so ignore all the operands. */
4872 if ((TREE_CODE (node) == ARRAY_REF
4873 || TREE_CODE (node) == ARRAY_RANGE_REF)
4874 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
4875 {
4876 UPDATE_FLAGS (TREE_OPERAND (node, 1));
4877 if (TREE_OPERAND (node, 2))
4878 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4879 if (TREE_OPERAND (node, 3))
4880 UPDATE_FLAGS (TREE_OPERAND (node, 3));
4881 }
4882 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
4883 FIELD_DECL, apparently. The G++ front end can put something else
4884 there, at least temporarily. */
4885 else if (TREE_CODE (node) == COMPONENT_REF
4886 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
4887 {
4888 if (TREE_OPERAND (node, 2))
4889 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4890 }
4891 }
4892
4893 node = lang_hooks.expr_to_decl (node, &tc, &se);
4894
4895 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4896 the address, since &(*a)->b is a form of addition. If it's a constant, the
4897 address is constant too. If it's a decl, its address is constant if the
4898 decl is static. Everything else is not constant and, furthermore,
4899 taking the address of a volatile variable is not volatile. */
4900 if (TREE_CODE (node) == INDIRECT_REF
4901 || TREE_CODE (node) == MEM_REF)
4902 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4903 else if (CONSTANT_CLASS_P (node))
4904 ;
4905 else if (DECL_P (node))
4906 tc &= (staticp (node) != NULL_TREE);
4907 else
4908 {
4909 tc = false;
4910 se |= TREE_SIDE_EFFECTS (node);
4911 }
4912
4913
4914 TREE_CONSTANT (t) = tc;
4915 TREE_SIDE_EFFECTS (t) = se;
4916 #undef UPDATE_FLAGS
4917 }
4918
4919 /* Build an expression of code CODE, data type TYPE, and operands as
4920 specified. Expressions and reference nodes can be created this way.
4921 Constants, decls, types and misc nodes cannot be.
4922
4923 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4924 enough for all extant tree codes. */
4925
4926 tree
4927 build0 (enum tree_code code, tree tt MEM_STAT_DECL)
4928 {
4929 tree t;
4930
4931 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4932
4933 t = make_node (code PASS_MEM_STAT);
4934 TREE_TYPE (t) = tt;
4935
4936 return t;
4937 }
4938
4939 tree
4940 build1 (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4941 {
4942 int length = sizeof (struct tree_exp);
4943 tree t;
4944
4945 record_node_allocation_statistics (code, length);
4946
4947 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4948
4949 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4950
4951 memset (t, 0, sizeof (struct tree_common));
4952
4953 TREE_SET_CODE (t, code);
4954
4955 TREE_TYPE (t) = type;
4956 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4957 TREE_OPERAND (t, 0) = node;
4958 if (node && !TYPE_P (node))
4959 {
4960 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4961 TREE_READONLY (t) = TREE_READONLY (node);
4962 }
4963
4964 if (TREE_CODE_CLASS (code) == tcc_statement)
4965 {
4966 if (code != DEBUG_BEGIN_STMT)
4967 TREE_SIDE_EFFECTS (t) = 1;
4968 }
4969 else switch (code)
4970 {
4971 case VA_ARG_EXPR:
4972 /* All of these have side-effects, no matter what their
4973 operands are. */
4974 TREE_SIDE_EFFECTS (t) = 1;
4975 TREE_READONLY (t) = 0;
4976 break;
4977
4978 case INDIRECT_REF:
4979 /* Whether a dereference is readonly has nothing to do with whether
4980 its operand is readonly. */
4981 TREE_READONLY (t) = 0;
4982 break;
4983
4984 case ADDR_EXPR:
4985 if (node)
4986 recompute_tree_invariant_for_addr_expr (t);
4987 break;
4988
4989 default:
4990 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4991 && node && !TYPE_P (node)
4992 && TREE_CONSTANT (node))
4993 TREE_CONSTANT (t) = 1;
4994 if (TREE_CODE_CLASS (code) == tcc_reference
4995 && node && TREE_THIS_VOLATILE (node))
4996 TREE_THIS_VOLATILE (t) = 1;
4997 break;
4998 }
4999
5000 return t;
5001 }
5002
5003 #define PROCESS_ARG(N) \
5004 do { \
5005 TREE_OPERAND (t, N) = arg##N; \
5006 if (arg##N &&!TYPE_P (arg##N)) \
5007 { \
5008 if (TREE_SIDE_EFFECTS (arg##N)) \
5009 side_effects = 1; \
5010 if (!TREE_READONLY (arg##N) \
5011 && !CONSTANT_CLASS_P (arg##N)) \
5012 (void) (read_only = 0); \
5013 if (!TREE_CONSTANT (arg##N)) \
5014 (void) (constant = 0); \
5015 } \
5016 } while (0)
5017
5018 tree
5019 build2 (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
5020 {
5021 bool constant, read_only, side_effects, div_by_zero;
5022 tree t;
5023
5024 gcc_assert (TREE_CODE_LENGTH (code) == 2);
5025
5026 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
5027 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
5028 /* When sizetype precision doesn't match that of pointers
5029 we need to be able to build explicit extensions or truncations
5030 of the offset argument. */
5031 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
5032 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
5033 && TREE_CODE (arg1) == INTEGER_CST);
5034
5035 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
5036 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
5037 && ptrofftype_p (TREE_TYPE (arg1)));
5038
5039 t = make_node (code PASS_MEM_STAT);
5040 TREE_TYPE (t) = tt;
5041
5042 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
5043 result based on those same flags for the arguments. But if the
5044 arguments aren't really even `tree' expressions, we shouldn't be trying
5045 to do this. */
5046
5047 /* Expressions without side effects may be constant if their
5048 arguments are as well. */
5049 constant = (TREE_CODE_CLASS (code) == tcc_comparison
5050 || TREE_CODE_CLASS (code) == tcc_binary);
5051 read_only = 1;
5052 side_effects = TREE_SIDE_EFFECTS (t);
5053
5054 switch (code)
5055 {
5056 case TRUNC_DIV_EXPR:
5057 case CEIL_DIV_EXPR:
5058 case FLOOR_DIV_EXPR:
5059 case ROUND_DIV_EXPR:
5060 case EXACT_DIV_EXPR:
5061 case CEIL_MOD_EXPR:
5062 case FLOOR_MOD_EXPR:
5063 case ROUND_MOD_EXPR:
5064 case TRUNC_MOD_EXPR:
5065 div_by_zero = integer_zerop (arg1);
5066 break;
5067 default:
5068 div_by_zero = false;
5069 }
5070
5071 PROCESS_ARG (0);
5072 PROCESS_ARG (1);
5073
5074 TREE_SIDE_EFFECTS (t) = side_effects;
5075 if (code == MEM_REF)
5076 {
5077 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
5078 {
5079 tree o = TREE_OPERAND (arg0, 0);
5080 TREE_READONLY (t) = TREE_READONLY (o);
5081 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
5082 }
5083 }
5084 else
5085 {
5086 TREE_READONLY (t) = read_only;
5087 /* Don't mark X / 0 as constant. */
5088 TREE_CONSTANT (t) = constant && !div_by_zero;
5089 TREE_THIS_VOLATILE (t)
5090 = (TREE_CODE_CLASS (code) == tcc_reference
5091 && arg0 && TREE_THIS_VOLATILE (arg0));
5092 }
5093
5094 return t;
5095 }
5096
5097
5098 tree
5099 build3 (enum tree_code code, tree tt, tree arg0, tree arg1,
5100 tree arg2 MEM_STAT_DECL)
5101 {
5102 bool constant, read_only, side_effects;
5103 tree t;
5104
5105 gcc_assert (TREE_CODE_LENGTH (code) == 3);
5106 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
5107
5108 t = make_node (code PASS_MEM_STAT);
5109 TREE_TYPE (t) = tt;
5110
5111 read_only = 1;
5112
5113 /* As a special exception, if COND_EXPR has NULL branches, we
5114 assume that it is a gimple statement and always consider
5115 it to have side effects. */
5116 if (code == COND_EXPR
5117 && tt == void_type_node
5118 && arg1 == NULL_TREE
5119 && arg2 == NULL_TREE)
5120 side_effects = true;
5121 else
5122 side_effects = TREE_SIDE_EFFECTS (t);
5123
5124 PROCESS_ARG (0);
5125 PROCESS_ARG (1);
5126 PROCESS_ARG (2);
5127
5128 if (code == COND_EXPR)
5129 TREE_READONLY (t) = read_only;
5130
5131 TREE_SIDE_EFFECTS (t) = side_effects;
5132 TREE_THIS_VOLATILE (t)
5133 = (TREE_CODE_CLASS (code) == tcc_reference
5134 && arg0 && TREE_THIS_VOLATILE (arg0));
5135
5136 return t;
5137 }
5138
5139 tree
5140 build4 (enum tree_code code, tree tt, tree arg0, tree arg1,
5141 tree arg2, tree arg3 MEM_STAT_DECL)
5142 {
5143 bool constant, read_only, side_effects;
5144 tree t;
5145
5146 gcc_assert (TREE_CODE_LENGTH (code) == 4);
5147
5148 t = make_node (code PASS_MEM_STAT);
5149 TREE_TYPE (t) = tt;
5150
5151 side_effects = TREE_SIDE_EFFECTS (t);
5152
5153 PROCESS_ARG (0);
5154 PROCESS_ARG (1);
5155 PROCESS_ARG (2);
5156 PROCESS_ARG (3);
5157
5158 TREE_SIDE_EFFECTS (t) = side_effects;
5159 TREE_THIS_VOLATILE (t)
5160 = (TREE_CODE_CLASS (code) == tcc_reference
5161 && arg0 && TREE_THIS_VOLATILE (arg0));
5162
5163 return t;
5164 }
5165
5166 tree
5167 build5 (enum tree_code code, tree tt, tree arg0, tree arg1,
5168 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
5169 {
5170 bool constant, read_only, side_effects;
5171 tree t;
5172
5173 gcc_assert (TREE_CODE_LENGTH (code) == 5);
5174
5175 t = make_node (code PASS_MEM_STAT);
5176 TREE_TYPE (t) = tt;
5177
5178 side_effects = TREE_SIDE_EFFECTS (t);
5179
5180 PROCESS_ARG (0);
5181 PROCESS_ARG (1);
5182 PROCESS_ARG (2);
5183 PROCESS_ARG (3);
5184 PROCESS_ARG (4);
5185
5186 TREE_SIDE_EFFECTS (t) = side_effects;
5187 if (code == TARGET_MEM_REF)
5188 {
5189 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
5190 {
5191 tree o = TREE_OPERAND (arg0, 0);
5192 TREE_READONLY (t) = TREE_READONLY (o);
5193 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
5194 }
5195 }
5196 else
5197 TREE_THIS_VOLATILE (t)
5198 = (TREE_CODE_CLASS (code) == tcc_reference
5199 && arg0 && TREE_THIS_VOLATILE (arg0));
5200
5201 return t;
5202 }
5203
5204 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
5205 on the pointer PTR. */
5206
5207 tree
5208 build_simple_mem_ref_loc (location_t loc, tree ptr)
5209 {
5210 poly_int64 offset = 0;
5211 tree ptype = TREE_TYPE (ptr);
5212 tree tem;
5213 /* For convenience allow addresses that collapse to a simple base
5214 and offset. */
5215 if (TREE_CODE (ptr) == ADDR_EXPR
5216 && (handled_component_p (TREE_OPERAND (ptr, 0))
5217 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
5218 {
5219 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
5220 gcc_assert (ptr);
5221 if (TREE_CODE (ptr) == MEM_REF)
5222 {
5223 offset += mem_ref_offset (ptr).force_shwi ();
5224 ptr = TREE_OPERAND (ptr, 0);
5225 }
5226 else
5227 ptr = build_fold_addr_expr (ptr);
5228 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
5229 }
5230 tem = build2 (MEM_REF, TREE_TYPE (ptype),
5231 ptr, build_int_cst (ptype, offset));
5232 SET_EXPR_LOCATION (tem, loc);
5233 return tem;
5234 }
5235
5236 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
5237
5238 poly_offset_int
5239 mem_ref_offset (const_tree t)
5240 {
5241 return poly_offset_int::from (wi::to_poly_wide (TREE_OPERAND (t, 1)),
5242 SIGNED);
5243 }
5244
5245 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
5246 offsetted by OFFSET units. */
5247
5248 tree
5249 build_invariant_address (tree type, tree base, poly_int64 offset)
5250 {
5251 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
5252 build_fold_addr_expr (base),
5253 build_int_cst (ptr_type_node, offset));
5254 tree addr = build1 (ADDR_EXPR, type, ref);
5255 recompute_tree_invariant_for_addr_expr (addr);
5256 return addr;
5257 }
5258
5259 /* Similar except don't specify the TREE_TYPE
5260 and leave the TREE_SIDE_EFFECTS as 0.
5261 It is permissible for arguments to be null,
5262 or even garbage if their values do not matter. */
5263
5264 tree
5265 build_nt (enum tree_code code, ...)
5266 {
5267 tree t;
5268 int length;
5269 int i;
5270 va_list p;
5271
5272 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
5273
5274 va_start (p, code);
5275
5276 t = make_node (code);
5277 length = TREE_CODE_LENGTH (code);
5278
5279 for (i = 0; i < length; i++)
5280 TREE_OPERAND (t, i) = va_arg (p, tree);
5281
5282 va_end (p);
5283 return t;
5284 }
5285
5286 /* Similar to build_nt, but for creating a CALL_EXPR object with a
5287 tree vec. */
5288
5289 tree
5290 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
5291 {
5292 tree ret, t;
5293 unsigned int ix;
5294
5295 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
5296 CALL_EXPR_FN (ret) = fn;
5297 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
5298 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
5299 CALL_EXPR_ARG (ret, ix) = t;
5300 return ret;
5301 }
5302 \f
5303 /* Create a DECL_... node of code CODE, name NAME (if non-null)
5304 and data type TYPE.
5305 We do NOT enter this node in any sort of symbol table.
5306
5307 LOC is the location of the decl.
5308
5309 layout_decl is used to set up the decl's storage layout.
5310 Other slots are initialized to 0 or null pointers. */
5311
5312 tree
5313 build_decl (location_t loc, enum tree_code code, tree name,
5314 tree type MEM_STAT_DECL)
5315 {
5316 tree t;
5317
5318 t = make_node (code PASS_MEM_STAT);
5319 DECL_SOURCE_LOCATION (t) = loc;
5320
5321 /* if (type == error_mark_node)
5322 type = integer_type_node; */
5323 /* That is not done, deliberately, so that having error_mark_node
5324 as the type can suppress useless errors in the use of this variable. */
5325
5326 DECL_NAME (t) = name;
5327 TREE_TYPE (t) = type;
5328
5329 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
5330 layout_decl (t, 0);
5331
5332 return t;
5333 }
5334
5335 /* Create and return a DEBUG_EXPR_DECL node of the given TYPE. */
5336
5337 tree
5338 build_debug_expr_decl (tree type)
5339 {
5340 tree vexpr = make_node (DEBUG_EXPR_DECL);
5341 DECL_ARTIFICIAL (vexpr) = 1;
5342 TREE_TYPE (vexpr) = type;
5343 SET_DECL_MODE (vexpr, TYPE_MODE (type));
5344 return vexpr;
5345 }
5346
5347 /* Builds and returns function declaration with NAME and TYPE. */
5348
5349 tree
5350 build_fn_decl (const char *name, tree type)
5351 {
5352 tree id = get_identifier (name);
5353 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
5354
5355 DECL_EXTERNAL (decl) = 1;
5356 TREE_PUBLIC (decl) = 1;
5357 DECL_ARTIFICIAL (decl) = 1;
5358 TREE_NOTHROW (decl) = 1;
5359
5360 return decl;
5361 }
5362
5363 vec<tree, va_gc> *all_translation_units;
5364
5365 /* Builds a new translation-unit decl with name NAME, queues it in the
5366 global list of translation-unit decls and returns it. */
5367
5368 tree
5369 build_translation_unit_decl (tree name)
5370 {
5371 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
5372 name, NULL_TREE);
5373 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
5374 vec_safe_push (all_translation_units, tu);
5375 return tu;
5376 }
5377
5378 \f
5379 /* BLOCK nodes are used to represent the structure of binding contours
5380 and declarations, once those contours have been exited and their contents
5381 compiled. This information is used for outputting debugging info. */
5382
5383 tree
5384 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
5385 {
5386 tree block = make_node (BLOCK);
5387
5388 BLOCK_VARS (block) = vars;
5389 BLOCK_SUBBLOCKS (block) = subblocks;
5390 BLOCK_SUPERCONTEXT (block) = supercontext;
5391 BLOCK_CHAIN (block) = chain;
5392 return block;
5393 }
5394
5395 \f
5396 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
5397
5398 LOC is the location to use in tree T. */
5399
5400 void
5401 protected_set_expr_location (tree t, location_t loc)
5402 {
5403 if (CAN_HAVE_LOCATION_P (t))
5404 SET_EXPR_LOCATION (t, loc);
5405 else if (t && TREE_CODE (t) == STATEMENT_LIST)
5406 {
5407 t = expr_single (t);
5408 if (t && CAN_HAVE_LOCATION_P (t))
5409 SET_EXPR_LOCATION (t, loc);
5410 }
5411 }
5412
5413 /* Like PROTECTED_SET_EXPR_LOCATION, but only do that if T has
5414 UNKNOWN_LOCATION. */
5415
5416 void
5417 protected_set_expr_location_if_unset (tree t, location_t loc)
5418 {
5419 t = expr_single (t);
5420 if (t && !EXPR_HAS_LOCATION (t))
5421 protected_set_expr_location (t, loc);
5422 }
5423 \f
5424 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
5425 of the various TYPE_QUAL values. */
5426
5427 static void
5428 set_type_quals (tree type, int type_quals)
5429 {
5430 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
5431 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
5432 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
5433 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
5434 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
5435 }
5436
5437 /* Returns true iff CAND and BASE have equivalent language-specific
5438 qualifiers. */
5439
5440 bool
5441 check_lang_type (const_tree cand, const_tree base)
5442 {
5443 if (lang_hooks.types.type_hash_eq == NULL)
5444 return true;
5445 /* type_hash_eq currently only applies to these types. */
5446 if (TREE_CODE (cand) != FUNCTION_TYPE
5447 && TREE_CODE (cand) != METHOD_TYPE)
5448 return true;
5449 return lang_hooks.types.type_hash_eq (cand, base);
5450 }
5451
5452 /* This function checks to see if TYPE matches the size one of the built-in
5453 atomic types, and returns that core atomic type. */
5454
5455 static tree
5456 find_atomic_core_type (const_tree type)
5457 {
5458 tree base_atomic_type;
5459
5460 /* Only handle complete types. */
5461 if (!tree_fits_uhwi_p (TYPE_SIZE (type)))
5462 return NULL_TREE;
5463
5464 switch (tree_to_uhwi (TYPE_SIZE (type)))
5465 {
5466 case 8:
5467 base_atomic_type = atomicQI_type_node;
5468 break;
5469
5470 case 16:
5471 base_atomic_type = atomicHI_type_node;
5472 break;
5473
5474 case 32:
5475 base_atomic_type = atomicSI_type_node;
5476 break;
5477
5478 case 64:
5479 base_atomic_type = atomicDI_type_node;
5480 break;
5481
5482 case 128:
5483 base_atomic_type = atomicTI_type_node;
5484 break;
5485
5486 default:
5487 base_atomic_type = NULL_TREE;
5488 }
5489
5490 return base_atomic_type;
5491 }
5492
5493 /* Returns true iff unqualified CAND and BASE are equivalent. */
5494
5495 bool
5496 check_base_type (const_tree cand, const_tree base)
5497 {
5498 if (TYPE_NAME (cand) != TYPE_NAME (base)
5499 /* Apparently this is needed for Objective-C. */
5500 || TYPE_CONTEXT (cand) != TYPE_CONTEXT (base)
5501 || !attribute_list_equal (TYPE_ATTRIBUTES (cand),
5502 TYPE_ATTRIBUTES (base)))
5503 return false;
5504 /* Check alignment. */
5505 if (TYPE_ALIGN (cand) == TYPE_ALIGN (base)
5506 && TYPE_USER_ALIGN (cand) == TYPE_USER_ALIGN (base))
5507 return true;
5508 /* Atomic types increase minimal alignment. We must to do so as well
5509 or we get duplicated canonical types. See PR88686. */
5510 if ((TYPE_QUALS (cand) & TYPE_QUAL_ATOMIC))
5511 {
5512 /* See if this object can map to a basic atomic type. */
5513 tree atomic_type = find_atomic_core_type (cand);
5514 if (atomic_type && TYPE_ALIGN (atomic_type) == TYPE_ALIGN (cand))
5515 return true;
5516 }
5517 return false;
5518 }
5519
5520 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
5521
5522 bool
5523 check_qualified_type (const_tree cand, const_tree base, int type_quals)
5524 {
5525 return (TYPE_QUALS (cand) == type_quals
5526 && check_base_type (cand, base)
5527 && check_lang_type (cand, base));
5528 }
5529
5530 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
5531
5532 static bool
5533 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
5534 {
5535 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
5536 && TYPE_NAME (cand) == TYPE_NAME (base)
5537 /* Apparently this is needed for Objective-C. */
5538 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5539 /* Check alignment. */
5540 && TYPE_ALIGN (cand) == align
5541 /* Check this is a user-aligned type as build_aligned_type
5542 would create. */
5543 && TYPE_USER_ALIGN (cand)
5544 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5545 TYPE_ATTRIBUTES (base))
5546 && check_lang_type (cand, base));
5547 }
5548
5549 /* Return a version of the TYPE, qualified as indicated by the
5550 TYPE_QUALS, if one exists. If no qualified version exists yet,
5551 return NULL_TREE. */
5552
5553 tree
5554 get_qualified_type (tree type, int type_quals)
5555 {
5556 if (TYPE_QUALS (type) == type_quals)
5557 return type;
5558
5559 tree mv = TYPE_MAIN_VARIANT (type);
5560 if (check_qualified_type (mv, type, type_quals))
5561 return mv;
5562
5563 /* Search the chain of variants to see if there is already one there just
5564 like the one we need to have. If so, use that existing one. We must
5565 preserve the TYPE_NAME, since there is code that depends on this. */
5566 for (tree *tp = &TYPE_NEXT_VARIANT (mv); *tp; tp = &TYPE_NEXT_VARIANT (*tp))
5567 if (check_qualified_type (*tp, type, type_quals))
5568 {
5569 /* Put the found variant at the head of the variant list so
5570 frequently searched variants get found faster. The C++ FE
5571 benefits greatly from this. */
5572 tree t = *tp;
5573 *tp = TYPE_NEXT_VARIANT (t);
5574 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
5575 TYPE_NEXT_VARIANT (mv) = t;
5576 return t;
5577 }
5578
5579 return NULL_TREE;
5580 }
5581
5582 /* Like get_qualified_type, but creates the type if it does not
5583 exist. This function never returns NULL_TREE. */
5584
5585 tree
5586 build_qualified_type (tree type, int type_quals MEM_STAT_DECL)
5587 {
5588 tree t;
5589
5590 /* See if we already have the appropriate qualified variant. */
5591 t = get_qualified_type (type, type_quals);
5592
5593 /* If not, build it. */
5594 if (!t)
5595 {
5596 t = build_variant_type_copy (type PASS_MEM_STAT);
5597 set_type_quals (t, type_quals);
5598
5599 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
5600 {
5601 /* See if this object can map to a basic atomic type. */
5602 tree atomic_type = find_atomic_core_type (type);
5603 if (atomic_type)
5604 {
5605 /* Ensure the alignment of this type is compatible with
5606 the required alignment of the atomic type. */
5607 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
5608 SET_TYPE_ALIGN (t, TYPE_ALIGN (atomic_type));
5609 }
5610 }
5611
5612 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5613 /* Propagate structural equality. */
5614 SET_TYPE_STRUCTURAL_EQUALITY (t);
5615 else if (TYPE_CANONICAL (type) != type)
5616 /* Build the underlying canonical type, since it is different
5617 from TYPE. */
5618 {
5619 tree c = build_qualified_type (TYPE_CANONICAL (type), type_quals);
5620 TYPE_CANONICAL (t) = TYPE_CANONICAL (c);
5621 }
5622 else
5623 /* T is its own canonical type. */
5624 TYPE_CANONICAL (t) = t;
5625
5626 }
5627
5628 return t;
5629 }
5630
5631 /* Create a variant of type T with alignment ALIGN. */
5632
5633 tree
5634 build_aligned_type (tree type, unsigned int align)
5635 {
5636 tree t;
5637
5638 if (TYPE_PACKED (type)
5639 || TYPE_ALIGN (type) == align)
5640 return type;
5641
5642 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5643 if (check_aligned_type (t, type, align))
5644 return t;
5645
5646 t = build_variant_type_copy (type);
5647 SET_TYPE_ALIGN (t, align);
5648 TYPE_USER_ALIGN (t) = 1;
5649
5650 return t;
5651 }
5652
5653 /* Create a new distinct copy of TYPE. The new type is made its own
5654 MAIN_VARIANT. If TYPE requires structural equality checks, the
5655 resulting type requires structural equality checks; otherwise, its
5656 TYPE_CANONICAL points to itself. */
5657
5658 tree
5659 build_distinct_type_copy (tree type MEM_STAT_DECL)
5660 {
5661 tree t = copy_node (type PASS_MEM_STAT);
5662
5663 TYPE_POINTER_TO (t) = 0;
5664 TYPE_REFERENCE_TO (t) = 0;
5665
5666 /* Set the canonical type either to a new equivalence class, or
5667 propagate the need for structural equality checks. */
5668 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5669 SET_TYPE_STRUCTURAL_EQUALITY (t);
5670 else
5671 TYPE_CANONICAL (t) = t;
5672
5673 /* Make it its own variant. */
5674 TYPE_MAIN_VARIANT (t) = t;
5675 TYPE_NEXT_VARIANT (t) = 0;
5676
5677 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
5678 whose TREE_TYPE is not t. This can also happen in the Ada
5679 frontend when using subtypes. */
5680
5681 return t;
5682 }
5683
5684 /* Create a new variant of TYPE, equivalent but distinct. This is so
5685 the caller can modify it. TYPE_CANONICAL for the return type will
5686 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
5687 are considered equal by the language itself (or that both types
5688 require structural equality checks). */
5689
5690 tree
5691 build_variant_type_copy (tree type MEM_STAT_DECL)
5692 {
5693 tree t, m = TYPE_MAIN_VARIANT (type);
5694
5695 t = build_distinct_type_copy (type PASS_MEM_STAT);
5696
5697 /* Since we're building a variant, assume that it is a non-semantic
5698 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
5699 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
5700 /* Type variants have no alias set defined. */
5701 TYPE_ALIAS_SET (t) = -1;
5702
5703 /* Add the new type to the chain of variants of TYPE. */
5704 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
5705 TYPE_NEXT_VARIANT (m) = t;
5706 TYPE_MAIN_VARIANT (t) = m;
5707
5708 return t;
5709 }
5710 \f
5711 /* Return true if the from tree in both tree maps are equal. */
5712
5713 int
5714 tree_map_base_eq (const void *va, const void *vb)
5715 {
5716 const struct tree_map_base *const a = (const struct tree_map_base *) va,
5717 *const b = (const struct tree_map_base *) vb;
5718 return (a->from == b->from);
5719 }
5720
5721 /* Hash a from tree in a tree_base_map. */
5722
5723 unsigned int
5724 tree_map_base_hash (const void *item)
5725 {
5726 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
5727 }
5728
5729 /* Return true if this tree map structure is marked for garbage collection
5730 purposes. We simply return true if the from tree is marked, so that this
5731 structure goes away when the from tree goes away. */
5732
5733 int
5734 tree_map_base_marked_p (const void *p)
5735 {
5736 return ggc_marked_p (((const struct tree_map_base *) p)->from);
5737 }
5738
5739 /* Hash a from tree in a tree_map. */
5740
5741 unsigned int
5742 tree_map_hash (const void *item)
5743 {
5744 return (((const struct tree_map *) item)->hash);
5745 }
5746
5747 /* Hash a from tree in a tree_decl_map. */
5748
5749 unsigned int
5750 tree_decl_map_hash (const void *item)
5751 {
5752 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
5753 }
5754
5755 /* Return the initialization priority for DECL. */
5756
5757 priority_type
5758 decl_init_priority_lookup (tree decl)
5759 {
5760 symtab_node *snode = symtab_node::get (decl);
5761
5762 if (!snode)
5763 return DEFAULT_INIT_PRIORITY;
5764 return
5765 snode->get_init_priority ();
5766 }
5767
5768 /* Return the finalization priority for DECL. */
5769
5770 priority_type
5771 decl_fini_priority_lookup (tree decl)
5772 {
5773 cgraph_node *node = cgraph_node::get (decl);
5774
5775 if (!node)
5776 return DEFAULT_INIT_PRIORITY;
5777 return
5778 node->get_fini_priority ();
5779 }
5780
5781 /* Set the initialization priority for DECL to PRIORITY. */
5782
5783 void
5784 decl_init_priority_insert (tree decl, priority_type priority)
5785 {
5786 struct symtab_node *snode;
5787
5788 if (priority == DEFAULT_INIT_PRIORITY)
5789 {
5790 snode = symtab_node::get (decl);
5791 if (!snode)
5792 return;
5793 }
5794 else if (VAR_P (decl))
5795 snode = varpool_node::get_create (decl);
5796 else
5797 snode = cgraph_node::get_create (decl);
5798 snode->set_init_priority (priority);
5799 }
5800
5801 /* Set the finalization priority for DECL to PRIORITY. */
5802
5803 void
5804 decl_fini_priority_insert (tree decl, priority_type priority)
5805 {
5806 struct cgraph_node *node;
5807
5808 if (priority == DEFAULT_INIT_PRIORITY)
5809 {
5810 node = cgraph_node::get (decl);
5811 if (!node)
5812 return;
5813 }
5814 else
5815 node = cgraph_node::get_create (decl);
5816 node->set_fini_priority (priority);
5817 }
5818
5819 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
5820
5821 static void
5822 print_debug_expr_statistics (void)
5823 {
5824 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
5825 (long) debug_expr_for_decl->size (),
5826 (long) debug_expr_for_decl->elements (),
5827 debug_expr_for_decl->collisions ());
5828 }
5829
5830 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
5831
5832 static void
5833 print_value_expr_statistics (void)
5834 {
5835 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
5836 (long) value_expr_for_decl->size (),
5837 (long) value_expr_for_decl->elements (),
5838 value_expr_for_decl->collisions ());
5839 }
5840
5841 /* Lookup a debug expression for FROM, and return it if we find one. */
5842
5843 tree
5844 decl_debug_expr_lookup (tree from)
5845 {
5846 struct tree_decl_map *h, in;
5847 in.base.from = from;
5848
5849 h = debug_expr_for_decl->find_with_hash (&in, DECL_UID (from));
5850 if (h)
5851 return h->to;
5852 return NULL_TREE;
5853 }
5854
5855 /* Insert a mapping FROM->TO in the debug expression hashtable. */
5856
5857 void
5858 decl_debug_expr_insert (tree from, tree to)
5859 {
5860 struct tree_decl_map *h;
5861
5862 h = ggc_alloc<tree_decl_map> ();
5863 h->base.from = from;
5864 h->to = to;
5865 *debug_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
5866 }
5867
5868 /* Lookup a value expression for FROM, and return it if we find one. */
5869
5870 tree
5871 decl_value_expr_lookup (tree from)
5872 {
5873 struct tree_decl_map *h, in;
5874 in.base.from = from;
5875
5876 h = value_expr_for_decl->find_with_hash (&in, DECL_UID (from));
5877 if (h)
5878 return h->to;
5879 return NULL_TREE;
5880 }
5881
5882 /* Insert a mapping FROM->TO in the value expression hashtable. */
5883
5884 void
5885 decl_value_expr_insert (tree from, tree to)
5886 {
5887 struct tree_decl_map *h;
5888
5889 h = ggc_alloc<tree_decl_map> ();
5890 h->base.from = from;
5891 h->to = to;
5892 *value_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
5893 }
5894
5895 /* Lookup a vector of debug arguments for FROM, and return it if we
5896 find one. */
5897
5898 vec<tree, va_gc> **
5899 decl_debug_args_lookup (tree from)
5900 {
5901 struct tree_vec_map *h, in;
5902
5903 if (!DECL_HAS_DEBUG_ARGS_P (from))
5904 return NULL;
5905 gcc_checking_assert (debug_args_for_decl != NULL);
5906 in.base.from = from;
5907 h = debug_args_for_decl->find_with_hash (&in, DECL_UID (from));
5908 if (h)
5909 return &h->to;
5910 return NULL;
5911 }
5912
5913 /* Insert a mapping FROM->empty vector of debug arguments in the value
5914 expression hashtable. */
5915
5916 vec<tree, va_gc> **
5917 decl_debug_args_insert (tree from)
5918 {
5919 struct tree_vec_map *h;
5920 tree_vec_map **loc;
5921
5922 if (DECL_HAS_DEBUG_ARGS_P (from))
5923 return decl_debug_args_lookup (from);
5924 if (debug_args_for_decl == NULL)
5925 debug_args_for_decl = hash_table<tree_vec_map_cache_hasher>::create_ggc (64);
5926 h = ggc_alloc<tree_vec_map> ();
5927 h->base.from = from;
5928 h->to = NULL;
5929 loc = debug_args_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT);
5930 *loc = h;
5931 DECL_HAS_DEBUG_ARGS_P (from) = 1;
5932 return &h->to;
5933 }
5934
5935 /* Hashing of types so that we don't make duplicates.
5936 The entry point is `type_hash_canon'. */
5937
5938 /* Generate the default hash code for TYPE. This is designed for
5939 speed, rather than maximum entropy. */
5940
5941 hashval_t
5942 type_hash_canon_hash (tree type)
5943 {
5944 inchash::hash hstate;
5945
5946 hstate.add_int (TREE_CODE (type));
5947
5948 if (TREE_TYPE (type))
5949 hstate.add_object (TYPE_HASH (TREE_TYPE (type)));
5950
5951 for (tree t = TYPE_ATTRIBUTES (type); t; t = TREE_CHAIN (t))
5952 /* Just the identifier is adequate to distinguish. */
5953 hstate.add_object (IDENTIFIER_HASH_VALUE (get_attribute_name (t)));
5954
5955 switch (TREE_CODE (type))
5956 {
5957 case METHOD_TYPE:
5958 hstate.add_object (TYPE_HASH (TYPE_METHOD_BASETYPE (type)));
5959 /* FALLTHROUGH. */
5960 case FUNCTION_TYPE:
5961 for (tree t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
5962 if (TREE_VALUE (t) != error_mark_node)
5963 hstate.add_object (TYPE_HASH (TREE_VALUE (t)));
5964 break;
5965
5966 case OFFSET_TYPE:
5967 hstate.add_object (TYPE_HASH (TYPE_OFFSET_BASETYPE (type)));
5968 break;
5969
5970 case ARRAY_TYPE:
5971 {
5972 if (TYPE_DOMAIN (type))
5973 hstate.add_object (TYPE_HASH (TYPE_DOMAIN (type)));
5974 if (!AGGREGATE_TYPE_P (TREE_TYPE (type)))
5975 {
5976 unsigned typeless = TYPE_TYPELESS_STORAGE (type);
5977 hstate.add_object (typeless);
5978 }
5979 }
5980 break;
5981
5982 case INTEGER_TYPE:
5983 {
5984 tree t = TYPE_MAX_VALUE (type);
5985 if (!t)
5986 t = TYPE_MIN_VALUE (type);
5987 for (int i = 0; i < TREE_INT_CST_NUNITS (t); i++)
5988 hstate.add_object (TREE_INT_CST_ELT (t, i));
5989 break;
5990 }
5991
5992 case REAL_TYPE:
5993 case FIXED_POINT_TYPE:
5994 {
5995 unsigned prec = TYPE_PRECISION (type);
5996 hstate.add_object (prec);
5997 break;
5998 }
5999
6000 case VECTOR_TYPE:
6001 hstate.add_poly_int (TYPE_VECTOR_SUBPARTS (type));
6002 break;
6003
6004 default:
6005 break;
6006 }
6007
6008 return hstate.end ();
6009 }
6010
6011 /* These are the Hashtable callback functions. */
6012
6013 /* Returns true iff the types are equivalent. */
6014
6015 bool
6016 type_cache_hasher::equal (type_hash *a, type_hash *b)
6017 {
6018 /* First test the things that are the same for all types. */
6019 if (a->hash != b->hash
6020 || TREE_CODE (a->type) != TREE_CODE (b->type)
6021 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6022 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6023 TYPE_ATTRIBUTES (b->type))
6024 || (TREE_CODE (a->type) != COMPLEX_TYPE
6025 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6026 return 0;
6027
6028 /* Be careful about comparing arrays before and after the element type
6029 has been completed; don't compare TYPE_ALIGN unless both types are
6030 complete. */
6031 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6032 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6033 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6034 return 0;
6035
6036 switch (TREE_CODE (a->type))
6037 {
6038 case VOID_TYPE:
6039 case OPAQUE_TYPE:
6040 case COMPLEX_TYPE:
6041 case POINTER_TYPE:
6042 case REFERENCE_TYPE:
6043 case NULLPTR_TYPE:
6044 return 1;
6045
6046 case VECTOR_TYPE:
6047 return known_eq (TYPE_VECTOR_SUBPARTS (a->type),
6048 TYPE_VECTOR_SUBPARTS (b->type));
6049
6050 case ENUMERAL_TYPE:
6051 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6052 && !(TYPE_VALUES (a->type)
6053 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6054 && TYPE_VALUES (b->type)
6055 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6056 && type_list_equal (TYPE_VALUES (a->type),
6057 TYPE_VALUES (b->type))))
6058 return 0;
6059
6060 /* fall through */
6061
6062 case INTEGER_TYPE:
6063 case REAL_TYPE:
6064 case BOOLEAN_TYPE:
6065 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
6066 return false;
6067 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6068 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6069 TYPE_MAX_VALUE (b->type)))
6070 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6071 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6072 TYPE_MIN_VALUE (b->type))));
6073
6074 case FIXED_POINT_TYPE:
6075 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6076
6077 case OFFSET_TYPE:
6078 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6079
6080 case METHOD_TYPE:
6081 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6082 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6083 || (TYPE_ARG_TYPES (a->type)
6084 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6085 && TYPE_ARG_TYPES (b->type)
6086 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6087 && type_list_equal (TYPE_ARG_TYPES (a->type),
6088 TYPE_ARG_TYPES (b->type)))))
6089 break;
6090 return 0;
6091 case ARRAY_TYPE:
6092 /* Don't compare TYPE_TYPELESS_STORAGE flag on aggregates,
6093 where the flag should be inherited from the element type
6094 and can change after ARRAY_TYPEs are created; on non-aggregates
6095 compare it and hash it, scalars will never have that flag set
6096 and we need to differentiate between arrays created by different
6097 front-ends or middle-end created arrays. */
6098 return (TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type)
6099 && (AGGREGATE_TYPE_P (TREE_TYPE (a->type))
6100 || (TYPE_TYPELESS_STORAGE (a->type)
6101 == TYPE_TYPELESS_STORAGE (b->type))));
6102
6103 case RECORD_TYPE:
6104 case UNION_TYPE:
6105 case QUAL_UNION_TYPE:
6106 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6107 || (TYPE_FIELDS (a->type)
6108 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6109 && TYPE_FIELDS (b->type)
6110 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6111 && type_list_equal (TYPE_FIELDS (a->type),
6112 TYPE_FIELDS (b->type))));
6113
6114 case FUNCTION_TYPE:
6115 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6116 || (TYPE_ARG_TYPES (a->type)
6117 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6118 && TYPE_ARG_TYPES (b->type)
6119 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6120 && type_list_equal (TYPE_ARG_TYPES (a->type),
6121 TYPE_ARG_TYPES (b->type))))
6122 break;
6123 return 0;
6124
6125 default:
6126 return 0;
6127 }
6128
6129 if (lang_hooks.types.type_hash_eq != NULL)
6130 return lang_hooks.types.type_hash_eq (a->type, b->type);
6131
6132 return 1;
6133 }
6134
6135 /* Given TYPE, and HASHCODE its hash code, return the canonical
6136 object for an identical type if one already exists.
6137 Otherwise, return TYPE, and record it as the canonical object.
6138
6139 To use this function, first create a type of the sort you want.
6140 Then compute its hash code from the fields of the type that
6141 make it different from other similar types.
6142 Then call this function and use the value. */
6143
6144 tree
6145 type_hash_canon (unsigned int hashcode, tree type)
6146 {
6147 type_hash in;
6148 type_hash **loc;
6149
6150 /* The hash table only contains main variants, so ensure that's what we're
6151 being passed. */
6152 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6153
6154 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6155 must call that routine before comparing TYPE_ALIGNs. */
6156 layout_type (type);
6157
6158 in.hash = hashcode;
6159 in.type = type;
6160
6161 loc = type_hash_table->find_slot_with_hash (&in, hashcode, INSERT);
6162 if (*loc)
6163 {
6164 tree t1 = ((type_hash *) *loc)->type;
6165 gcc_assert (TYPE_MAIN_VARIANT (t1) == t1
6166 && t1 != type);
6167 if (TYPE_UID (type) + 1 == next_type_uid)
6168 --next_type_uid;
6169 /* Free also min/max values and the cache for integer
6170 types. This can't be done in free_node, as LTO frees
6171 those on its own. */
6172 if (TREE_CODE (type) == INTEGER_TYPE)
6173 {
6174 if (TYPE_MIN_VALUE (type)
6175 && TREE_TYPE (TYPE_MIN_VALUE (type)) == type)
6176 {
6177 /* Zero is always in TYPE_CACHED_VALUES. */
6178 if (! TYPE_UNSIGNED (type))
6179 int_cst_hash_table->remove_elt (TYPE_MIN_VALUE (type));
6180 ggc_free (TYPE_MIN_VALUE (type));
6181 }
6182 if (TYPE_MAX_VALUE (type)
6183 && TREE_TYPE (TYPE_MAX_VALUE (type)) == type)
6184 {
6185 int_cst_hash_table->remove_elt (TYPE_MAX_VALUE (type));
6186 ggc_free (TYPE_MAX_VALUE (type));
6187 }
6188 if (TYPE_CACHED_VALUES_P (type))
6189 ggc_free (TYPE_CACHED_VALUES (type));
6190 }
6191 free_node (type);
6192 return t1;
6193 }
6194 else
6195 {
6196 struct type_hash *h;
6197
6198 h = ggc_alloc<type_hash> ();
6199 h->hash = hashcode;
6200 h->type = type;
6201 *loc = h;
6202
6203 return type;
6204 }
6205 }
6206
6207 static void
6208 print_type_hash_statistics (void)
6209 {
6210 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6211 (long) type_hash_table->size (),
6212 (long) type_hash_table->elements (),
6213 type_hash_table->collisions ());
6214 }
6215
6216 /* Given two lists of types
6217 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6218 return 1 if the lists contain the same types in the same order.
6219 Also, the TREE_PURPOSEs must match. */
6220
6221 bool
6222 type_list_equal (const_tree l1, const_tree l2)
6223 {
6224 const_tree t1, t2;
6225
6226 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6227 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6228 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6229 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6230 && (TREE_TYPE (TREE_PURPOSE (t1))
6231 == TREE_TYPE (TREE_PURPOSE (t2))))))
6232 return false;
6233
6234 return t1 == t2;
6235 }
6236
6237 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6238 given by TYPE. If the argument list accepts variable arguments,
6239 then this function counts only the ordinary arguments. */
6240
6241 int
6242 type_num_arguments (const_tree fntype)
6243 {
6244 int i = 0;
6245
6246 for (tree t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
6247 /* If the function does not take a variable number of arguments,
6248 the last element in the list will have type `void'. */
6249 if (VOID_TYPE_P (TREE_VALUE (t)))
6250 break;
6251 else
6252 ++i;
6253
6254 return i;
6255 }
6256
6257 /* Return the type of the function TYPE's argument ARGNO if known.
6258 For vararg function's where ARGNO refers to one of the variadic
6259 arguments return null. Otherwise, return a void_type_node for
6260 out-of-bounds ARGNO. */
6261
6262 tree
6263 type_argument_type (const_tree fntype, unsigned argno)
6264 {
6265 /* Treat zero the same as an out-of-bounds argument number. */
6266 if (!argno)
6267 return void_type_node;
6268
6269 function_args_iterator iter;
6270
6271 tree argtype;
6272 unsigned i = 1;
6273 FOREACH_FUNCTION_ARGS (fntype, argtype, iter)
6274 {
6275 /* A vararg function's argument list ends in a null. Otherwise,
6276 an ordinary function's argument list ends with void. Return
6277 null if ARGNO refers to a vararg argument, void_type_node if
6278 it's out of bounds, and the formal argument type otherwise. */
6279 if (!argtype)
6280 break;
6281
6282 if (i == argno || VOID_TYPE_P (argtype))
6283 return argtype;
6284
6285 ++i;
6286 }
6287
6288 return NULL_TREE;
6289 }
6290
6291 /* Nonzero if integer constants T1 and T2
6292 represent the same constant value. */
6293
6294 int
6295 tree_int_cst_equal (const_tree t1, const_tree t2)
6296 {
6297 if (t1 == t2)
6298 return 1;
6299
6300 if (t1 == 0 || t2 == 0)
6301 return 0;
6302
6303 STRIP_ANY_LOCATION_WRAPPER (t1);
6304 STRIP_ANY_LOCATION_WRAPPER (t2);
6305
6306 if (TREE_CODE (t1) == INTEGER_CST
6307 && TREE_CODE (t2) == INTEGER_CST
6308 && wi::to_widest (t1) == wi::to_widest (t2))
6309 return 1;
6310
6311 return 0;
6312 }
6313
6314 /* Return true if T is an INTEGER_CST whose numerical value (extended
6315 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
6316
6317 bool
6318 tree_fits_shwi_p (const_tree t)
6319 {
6320 return (t != NULL_TREE
6321 && TREE_CODE (t) == INTEGER_CST
6322 && wi::fits_shwi_p (wi::to_widest (t)));
6323 }
6324
6325 /* Return true if T is an INTEGER_CST or POLY_INT_CST whose numerical
6326 value (extended according to TYPE_UNSIGNED) fits in a poly_int64. */
6327
6328 bool
6329 tree_fits_poly_int64_p (const_tree t)
6330 {
6331 if (t == NULL_TREE)
6332 return false;
6333 if (POLY_INT_CST_P (t))
6334 {
6335 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; i++)
6336 if (!wi::fits_shwi_p (wi::to_wide (POLY_INT_CST_COEFF (t, i))))
6337 return false;
6338 return true;
6339 }
6340 return (TREE_CODE (t) == INTEGER_CST
6341 && wi::fits_shwi_p (wi::to_widest (t)));
6342 }
6343
6344 /* Return true if T is an INTEGER_CST whose numerical value (extended
6345 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
6346
6347 bool
6348 tree_fits_uhwi_p (const_tree t)
6349 {
6350 return (t != NULL_TREE
6351 && TREE_CODE (t) == INTEGER_CST
6352 && wi::fits_uhwi_p (wi::to_widest (t)));
6353 }
6354
6355 /* Return true if T is an INTEGER_CST or POLY_INT_CST whose numerical
6356 value (extended according to TYPE_UNSIGNED) fits in a poly_uint64. */
6357
6358 bool
6359 tree_fits_poly_uint64_p (const_tree t)
6360 {
6361 if (t == NULL_TREE)
6362 return false;
6363 if (POLY_INT_CST_P (t))
6364 {
6365 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; i++)
6366 if (!wi::fits_uhwi_p (wi::to_widest (POLY_INT_CST_COEFF (t, i))))
6367 return false;
6368 return true;
6369 }
6370 return (TREE_CODE (t) == INTEGER_CST
6371 && wi::fits_uhwi_p (wi::to_widest (t)));
6372 }
6373
6374 /* T is an INTEGER_CST whose numerical value (extended according to
6375 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
6376 HOST_WIDE_INT. */
6377
6378 HOST_WIDE_INT
6379 tree_to_shwi (const_tree t)
6380 {
6381 gcc_assert (tree_fits_shwi_p (t));
6382 return TREE_INT_CST_LOW (t);
6383 }
6384
6385 /* T is an INTEGER_CST whose numerical value (extended according to
6386 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
6387 HOST_WIDE_INT. */
6388
6389 unsigned HOST_WIDE_INT
6390 tree_to_uhwi (const_tree t)
6391 {
6392 gcc_assert (tree_fits_uhwi_p (t));
6393 return TREE_INT_CST_LOW (t);
6394 }
6395
6396 /* Return the most significant (sign) bit of T. */
6397
6398 int
6399 tree_int_cst_sign_bit (const_tree t)
6400 {
6401 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
6402
6403 return wi::extract_uhwi (wi::to_wide (t), bitno, 1);
6404 }
6405
6406 /* Return an indication of the sign of the integer constant T.
6407 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
6408 Note that -1 will never be returned if T's type is unsigned. */
6409
6410 int
6411 tree_int_cst_sgn (const_tree t)
6412 {
6413 if (wi::to_wide (t) == 0)
6414 return 0;
6415 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
6416 return 1;
6417 else if (wi::neg_p (wi::to_wide (t)))
6418 return -1;
6419 else
6420 return 1;
6421 }
6422
6423 /* Return the minimum number of bits needed to represent VALUE in a
6424 signed or unsigned type, UNSIGNEDP says which. */
6425
6426 unsigned int
6427 tree_int_cst_min_precision (tree value, signop sgn)
6428 {
6429 /* If the value is negative, compute its negative minus 1. The latter
6430 adjustment is because the absolute value of the largest negative value
6431 is one larger than the largest positive value. This is equivalent to
6432 a bit-wise negation, so use that operation instead. */
6433
6434 if (tree_int_cst_sgn (value) < 0)
6435 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
6436
6437 /* Return the number of bits needed, taking into account the fact
6438 that we need one more bit for a signed than unsigned type.
6439 If value is 0 or -1, the minimum precision is 1 no matter
6440 whether unsignedp is true or false. */
6441
6442 if (integer_zerop (value))
6443 return 1;
6444 else
6445 return tree_floor_log2 (value) + 1 + (sgn == SIGNED ? 1 : 0) ;
6446 }
6447
6448 /* Return truthvalue of whether T1 is the same tree structure as T2.
6449 Return 1 if they are the same.
6450 Return 0 if they are understandably different.
6451 Return -1 if either contains tree structure not understood by
6452 this function. */
6453
6454 int
6455 simple_cst_equal (const_tree t1, const_tree t2)
6456 {
6457 enum tree_code code1, code2;
6458 int cmp;
6459 int i;
6460
6461 if (t1 == t2)
6462 return 1;
6463 if (t1 == 0 || t2 == 0)
6464 return 0;
6465
6466 /* For location wrappers to be the same, they must be at the same
6467 source location (and wrap the same thing). */
6468 if (location_wrapper_p (t1) && location_wrapper_p (t2))
6469 {
6470 if (EXPR_LOCATION (t1) != EXPR_LOCATION (t2))
6471 return 0;
6472 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6473 }
6474
6475 code1 = TREE_CODE (t1);
6476 code2 = TREE_CODE (t2);
6477
6478 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
6479 {
6480 if (CONVERT_EXPR_CODE_P (code2)
6481 || code2 == NON_LVALUE_EXPR)
6482 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6483 else
6484 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
6485 }
6486
6487 else if (CONVERT_EXPR_CODE_P (code2)
6488 || code2 == NON_LVALUE_EXPR)
6489 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
6490
6491 if (code1 != code2)
6492 return 0;
6493
6494 switch (code1)
6495 {
6496 case INTEGER_CST:
6497 return wi::to_widest (t1) == wi::to_widest (t2);
6498
6499 case REAL_CST:
6500 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
6501
6502 case FIXED_CST:
6503 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
6504
6505 case STRING_CST:
6506 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
6507 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
6508 TREE_STRING_LENGTH (t1)));
6509
6510 case CONSTRUCTOR:
6511 {
6512 unsigned HOST_WIDE_INT idx;
6513 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
6514 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
6515
6516 if (vec_safe_length (v1) != vec_safe_length (v2))
6517 return false;
6518
6519 for (idx = 0; idx < vec_safe_length (v1); ++idx)
6520 /* ??? Should we handle also fields here? */
6521 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
6522 return false;
6523 return true;
6524 }
6525
6526 case SAVE_EXPR:
6527 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6528
6529 case CALL_EXPR:
6530 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
6531 if (cmp <= 0)
6532 return cmp;
6533 if (call_expr_nargs (t1) != call_expr_nargs (t2))
6534 return 0;
6535 {
6536 const_tree arg1, arg2;
6537 const_call_expr_arg_iterator iter1, iter2;
6538 for (arg1 = first_const_call_expr_arg (t1, &iter1),
6539 arg2 = first_const_call_expr_arg (t2, &iter2);
6540 arg1 && arg2;
6541 arg1 = next_const_call_expr_arg (&iter1),
6542 arg2 = next_const_call_expr_arg (&iter2))
6543 {
6544 cmp = simple_cst_equal (arg1, arg2);
6545 if (cmp <= 0)
6546 return cmp;
6547 }
6548 return arg1 == arg2;
6549 }
6550
6551 case TARGET_EXPR:
6552 /* Special case: if either target is an unallocated VAR_DECL,
6553 it means that it's going to be unified with whatever the
6554 TARGET_EXPR is really supposed to initialize, so treat it
6555 as being equivalent to anything. */
6556 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
6557 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
6558 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
6559 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
6560 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
6561 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
6562 cmp = 1;
6563 else
6564 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6565
6566 if (cmp <= 0)
6567 return cmp;
6568
6569 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
6570
6571 case WITH_CLEANUP_EXPR:
6572 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6573 if (cmp <= 0)
6574 return cmp;
6575
6576 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
6577
6578 case COMPONENT_REF:
6579 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
6580 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6581
6582 return 0;
6583
6584 case VAR_DECL:
6585 case PARM_DECL:
6586 case CONST_DECL:
6587 case FUNCTION_DECL:
6588 return 0;
6589
6590 default:
6591 if (POLY_INT_CST_P (t1))
6592 /* A false return means maybe_ne rather than known_ne. */
6593 return known_eq (poly_widest_int::from (poly_int_cst_value (t1),
6594 TYPE_SIGN (TREE_TYPE (t1))),
6595 poly_widest_int::from (poly_int_cst_value (t2),
6596 TYPE_SIGN (TREE_TYPE (t2))));
6597 break;
6598 }
6599
6600 /* This general rule works for most tree codes. All exceptions should be
6601 handled above. If this is a language-specific tree code, we can't
6602 trust what might be in the operand, so say we don't know
6603 the situation. */
6604 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
6605 return -1;
6606
6607 switch (TREE_CODE_CLASS (code1))
6608 {
6609 case tcc_unary:
6610 case tcc_binary:
6611 case tcc_comparison:
6612 case tcc_expression:
6613 case tcc_reference:
6614 case tcc_statement:
6615 cmp = 1;
6616 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
6617 {
6618 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
6619 if (cmp <= 0)
6620 return cmp;
6621 }
6622
6623 return cmp;
6624
6625 default:
6626 return -1;
6627 }
6628 }
6629
6630 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
6631 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
6632 than U, respectively. */
6633
6634 int
6635 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
6636 {
6637 if (tree_int_cst_sgn (t) < 0)
6638 return -1;
6639 else if (!tree_fits_uhwi_p (t))
6640 return 1;
6641 else if (TREE_INT_CST_LOW (t) == u)
6642 return 0;
6643 else if (TREE_INT_CST_LOW (t) < u)
6644 return -1;
6645 else
6646 return 1;
6647 }
6648
6649 /* Return true if SIZE represents a constant size that is in bounds of
6650 what the middle-end and the backend accepts (covering not more than
6651 half of the address-space).
6652 When PERR is non-null, set *PERR on failure to the description of
6653 why SIZE is not valid. */
6654
6655 bool
6656 valid_constant_size_p (const_tree size, cst_size_error *perr /* = NULL */)
6657 {
6658 if (POLY_INT_CST_P (size))
6659 {
6660 if (TREE_OVERFLOW (size))
6661 return false;
6662 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
6663 if (!valid_constant_size_p (POLY_INT_CST_COEFF (size, i)))
6664 return false;
6665 return true;
6666 }
6667
6668 cst_size_error error;
6669 if (!perr)
6670 perr = &error;
6671
6672 if (TREE_CODE (size) != INTEGER_CST)
6673 {
6674 *perr = cst_size_not_constant;
6675 return false;
6676 }
6677
6678 if (TREE_OVERFLOW_P (size))
6679 {
6680 *perr = cst_size_overflow;
6681 return false;
6682 }
6683
6684 if (tree_int_cst_sgn (size) < 0)
6685 {
6686 *perr = cst_size_negative;
6687 return false;
6688 }
6689 if (!tree_fits_uhwi_p (size)
6690 || (wi::to_widest (TYPE_MAX_VALUE (sizetype))
6691 < wi::to_widest (size) * 2))
6692 {
6693 *perr = cst_size_too_big;
6694 return false;
6695 }
6696
6697 return true;
6698 }
6699
6700 /* Return the precision of the type, or for a complex or vector type the
6701 precision of the type of its elements. */
6702
6703 unsigned int
6704 element_precision (const_tree type)
6705 {
6706 if (!TYPE_P (type))
6707 type = TREE_TYPE (type);
6708 enum tree_code code = TREE_CODE (type);
6709 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
6710 type = TREE_TYPE (type);
6711
6712 return TYPE_PRECISION (type);
6713 }
6714
6715 /* Return true if CODE represents an associative tree code. Otherwise
6716 return false. */
6717 bool
6718 associative_tree_code (enum tree_code code)
6719 {
6720 switch (code)
6721 {
6722 case BIT_IOR_EXPR:
6723 case BIT_AND_EXPR:
6724 case BIT_XOR_EXPR:
6725 case PLUS_EXPR:
6726 case MULT_EXPR:
6727 case MIN_EXPR:
6728 case MAX_EXPR:
6729 return true;
6730
6731 default:
6732 break;
6733 }
6734 return false;
6735 }
6736
6737 /* Return true if CODE represents a commutative tree code. Otherwise
6738 return false. */
6739 bool
6740 commutative_tree_code (enum tree_code code)
6741 {
6742 switch (code)
6743 {
6744 case PLUS_EXPR:
6745 case MULT_EXPR:
6746 case MULT_HIGHPART_EXPR:
6747 case MIN_EXPR:
6748 case MAX_EXPR:
6749 case BIT_IOR_EXPR:
6750 case BIT_XOR_EXPR:
6751 case BIT_AND_EXPR:
6752 case NE_EXPR:
6753 case EQ_EXPR:
6754 case UNORDERED_EXPR:
6755 case ORDERED_EXPR:
6756 case UNEQ_EXPR:
6757 case LTGT_EXPR:
6758 case TRUTH_AND_EXPR:
6759 case TRUTH_XOR_EXPR:
6760 case TRUTH_OR_EXPR:
6761 case WIDEN_MULT_EXPR:
6762 case VEC_WIDEN_MULT_HI_EXPR:
6763 case VEC_WIDEN_MULT_LO_EXPR:
6764 case VEC_WIDEN_MULT_EVEN_EXPR:
6765 case VEC_WIDEN_MULT_ODD_EXPR:
6766 return true;
6767
6768 default:
6769 break;
6770 }
6771 return false;
6772 }
6773
6774 /* Return true if CODE represents a ternary tree code for which the
6775 first two operands are commutative. Otherwise return false. */
6776 bool
6777 commutative_ternary_tree_code (enum tree_code code)
6778 {
6779 switch (code)
6780 {
6781 case WIDEN_MULT_PLUS_EXPR:
6782 case WIDEN_MULT_MINUS_EXPR:
6783 case DOT_PROD_EXPR:
6784 return true;
6785
6786 default:
6787 break;
6788 }
6789 return false;
6790 }
6791
6792 /* Returns true if CODE can overflow. */
6793
6794 bool
6795 operation_can_overflow (enum tree_code code)
6796 {
6797 switch (code)
6798 {
6799 case PLUS_EXPR:
6800 case MINUS_EXPR:
6801 case MULT_EXPR:
6802 case LSHIFT_EXPR:
6803 /* Can overflow in various ways. */
6804 return true;
6805 case TRUNC_DIV_EXPR:
6806 case EXACT_DIV_EXPR:
6807 case FLOOR_DIV_EXPR:
6808 case CEIL_DIV_EXPR:
6809 /* For INT_MIN / -1. */
6810 return true;
6811 case NEGATE_EXPR:
6812 case ABS_EXPR:
6813 /* For -INT_MIN. */
6814 return true;
6815 default:
6816 /* These operators cannot overflow. */
6817 return false;
6818 }
6819 }
6820
6821 /* Returns true if CODE operating on operands of type TYPE doesn't overflow, or
6822 ftrapv doesn't generate trapping insns for CODE. */
6823
6824 bool
6825 operation_no_trapping_overflow (tree type, enum tree_code code)
6826 {
6827 gcc_checking_assert (ANY_INTEGRAL_TYPE_P (type));
6828
6829 /* We don't generate instructions that trap on overflow for complex or vector
6830 types. */
6831 if (!INTEGRAL_TYPE_P (type))
6832 return true;
6833
6834 if (!TYPE_OVERFLOW_TRAPS (type))
6835 return true;
6836
6837 switch (code)
6838 {
6839 case PLUS_EXPR:
6840 case MINUS_EXPR:
6841 case MULT_EXPR:
6842 case NEGATE_EXPR:
6843 case ABS_EXPR:
6844 /* These operators can overflow, and -ftrapv generates trapping code for
6845 these. */
6846 return false;
6847 case TRUNC_DIV_EXPR:
6848 case EXACT_DIV_EXPR:
6849 case FLOOR_DIV_EXPR:
6850 case CEIL_DIV_EXPR:
6851 case LSHIFT_EXPR:
6852 /* These operators can overflow, but -ftrapv does not generate trapping
6853 code for these. */
6854 return true;
6855 default:
6856 /* These operators cannot overflow. */
6857 return true;
6858 }
6859 }
6860
6861 /* Constructors for pointer, array and function types.
6862 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
6863 constructed by language-dependent code, not here.) */
6864
6865 /* Construct, lay out and return the type of pointers to TO_TYPE with
6866 mode MODE. If MODE is VOIDmode, a pointer mode for the address
6867 space of TO_TYPE will be picked. If CAN_ALIAS_ALL is TRUE,
6868 indicate this type can reference all of memory. If such a type has
6869 already been constructed, reuse it. */
6870
6871 tree
6872 build_pointer_type_for_mode (tree to_type, machine_mode mode,
6873 bool can_alias_all)
6874 {
6875 tree t;
6876 bool could_alias = can_alias_all;
6877
6878 if (to_type == error_mark_node)
6879 return error_mark_node;
6880
6881 if (mode == VOIDmode)
6882 {
6883 addr_space_t as = TYPE_ADDR_SPACE (to_type);
6884 mode = targetm.addr_space.pointer_mode (as);
6885 }
6886
6887 /* If the pointed-to type has the may_alias attribute set, force
6888 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
6889 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
6890 can_alias_all = true;
6891
6892 /* In some cases, languages will have things that aren't a POINTER_TYPE
6893 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
6894 In that case, return that type without regard to the rest of our
6895 operands.
6896
6897 ??? This is a kludge, but consistent with the way this function has
6898 always operated and there doesn't seem to be a good way to avoid this
6899 at the moment. */
6900 if (TYPE_POINTER_TO (to_type) != 0
6901 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
6902 return TYPE_POINTER_TO (to_type);
6903
6904 /* First, if we already have a type for pointers to TO_TYPE and it's
6905 the proper mode, use it. */
6906 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
6907 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
6908 return t;
6909
6910 t = make_node (POINTER_TYPE);
6911
6912 TREE_TYPE (t) = to_type;
6913 SET_TYPE_MODE (t, mode);
6914 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
6915 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
6916 TYPE_POINTER_TO (to_type) = t;
6917
6918 /* During LTO we do not set TYPE_CANONICAL of pointers and references. */
6919 if (TYPE_STRUCTURAL_EQUALITY_P (to_type) || in_lto_p)
6920 SET_TYPE_STRUCTURAL_EQUALITY (t);
6921 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
6922 TYPE_CANONICAL (t)
6923 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
6924 mode, false);
6925
6926 /* Lay out the type. This function has many callers that are concerned
6927 with expression-construction, and this simplifies them all. */
6928 layout_type (t);
6929
6930 return t;
6931 }
6932
6933 /* By default build pointers in ptr_mode. */
6934
6935 tree
6936 build_pointer_type (tree to_type)
6937 {
6938 return build_pointer_type_for_mode (to_type, VOIDmode, false);
6939 }
6940
6941 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
6942
6943 tree
6944 build_reference_type_for_mode (tree to_type, machine_mode mode,
6945 bool can_alias_all)
6946 {
6947 tree t;
6948 bool could_alias = can_alias_all;
6949
6950 if (to_type == error_mark_node)
6951 return error_mark_node;
6952
6953 if (mode == VOIDmode)
6954 {
6955 addr_space_t as = TYPE_ADDR_SPACE (to_type);
6956 mode = targetm.addr_space.pointer_mode (as);
6957 }
6958
6959 /* If the pointed-to type has the may_alias attribute set, force
6960 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
6961 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
6962 can_alias_all = true;
6963
6964 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
6965 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
6966 In that case, return that type without regard to the rest of our
6967 operands.
6968
6969 ??? This is a kludge, but consistent with the way this function has
6970 always operated and there doesn't seem to be a good way to avoid this
6971 at the moment. */
6972 if (TYPE_REFERENCE_TO (to_type) != 0
6973 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
6974 return TYPE_REFERENCE_TO (to_type);
6975
6976 /* First, if we already have a type for pointers to TO_TYPE and it's
6977 the proper mode, use it. */
6978 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
6979 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
6980 return t;
6981
6982 t = make_node (REFERENCE_TYPE);
6983
6984 TREE_TYPE (t) = to_type;
6985 SET_TYPE_MODE (t, mode);
6986 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
6987 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
6988 TYPE_REFERENCE_TO (to_type) = t;
6989
6990 /* During LTO we do not set TYPE_CANONICAL of pointers and references. */
6991 if (TYPE_STRUCTURAL_EQUALITY_P (to_type) || in_lto_p)
6992 SET_TYPE_STRUCTURAL_EQUALITY (t);
6993 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
6994 TYPE_CANONICAL (t)
6995 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
6996 mode, false);
6997
6998 layout_type (t);
6999
7000 return t;
7001 }
7002
7003
7004 /* Build the node for the type of references-to-TO_TYPE by default
7005 in ptr_mode. */
7006
7007 tree
7008 build_reference_type (tree to_type)
7009 {
7010 return build_reference_type_for_mode (to_type, VOIDmode, false);
7011 }
7012
7013 #define MAX_INT_CACHED_PREC \
7014 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7015 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7016
7017 static void
7018 clear_nonstandard_integer_type_cache (void)
7019 {
7020 for (size_t i = 0 ; i < 2 * MAX_INT_CACHED_PREC + 2 ; i++)
7021 {
7022 nonstandard_integer_type_cache[i] = NULL;
7023 }
7024 }
7025
7026 /* Builds a signed or unsigned integer type of precision PRECISION.
7027 Used for C bitfields whose precision does not match that of
7028 built-in target types. */
7029 tree
7030 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7031 int unsignedp)
7032 {
7033 tree itype, ret;
7034
7035 if (unsignedp)
7036 unsignedp = MAX_INT_CACHED_PREC + 1;
7037
7038 if (precision <= MAX_INT_CACHED_PREC)
7039 {
7040 itype = nonstandard_integer_type_cache[precision + unsignedp];
7041 if (itype)
7042 return itype;
7043 }
7044
7045 itype = make_node (INTEGER_TYPE);
7046 TYPE_PRECISION (itype) = precision;
7047
7048 if (unsignedp)
7049 fixup_unsigned_type (itype);
7050 else
7051 fixup_signed_type (itype);
7052
7053 inchash::hash hstate;
7054 inchash::add_expr (TYPE_MAX_VALUE (itype), hstate);
7055 ret = type_hash_canon (hstate.end (), itype);
7056 if (precision <= MAX_INT_CACHED_PREC)
7057 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7058
7059 return ret;
7060 }
7061
7062 #define MAX_BOOL_CACHED_PREC \
7063 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7064 static GTY(()) tree nonstandard_boolean_type_cache[MAX_BOOL_CACHED_PREC + 1];
7065
7066 /* Builds a boolean type of precision PRECISION.
7067 Used for boolean vectors to choose proper vector element size. */
7068 tree
7069 build_nonstandard_boolean_type (unsigned HOST_WIDE_INT precision)
7070 {
7071 tree type;
7072
7073 if (precision <= MAX_BOOL_CACHED_PREC)
7074 {
7075 type = nonstandard_boolean_type_cache[precision];
7076 if (type)
7077 return type;
7078 }
7079
7080 type = make_node (BOOLEAN_TYPE);
7081 TYPE_PRECISION (type) = precision;
7082 fixup_signed_type (type);
7083
7084 if (precision <= MAX_INT_CACHED_PREC)
7085 nonstandard_boolean_type_cache[precision] = type;
7086
7087 return type;
7088 }
7089
7090 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7091 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7092 is true, reuse such a type that has already been constructed. */
7093
7094 static tree
7095 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7096 {
7097 tree itype = make_node (INTEGER_TYPE);
7098
7099 TREE_TYPE (itype) = type;
7100
7101 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7102 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7103
7104 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7105 SET_TYPE_MODE (itype, TYPE_MODE (type));
7106 TYPE_SIZE (itype) = TYPE_SIZE (type);
7107 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7108 SET_TYPE_ALIGN (itype, TYPE_ALIGN (type));
7109 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7110 SET_TYPE_WARN_IF_NOT_ALIGN (itype, TYPE_WARN_IF_NOT_ALIGN (type));
7111
7112 if (!shared)
7113 return itype;
7114
7115 if ((TYPE_MIN_VALUE (itype)
7116 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7117 || (TYPE_MAX_VALUE (itype)
7118 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7119 {
7120 /* Since we cannot reliably merge this type, we need to compare it using
7121 structural equality checks. */
7122 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7123 return itype;
7124 }
7125
7126 hashval_t hash = type_hash_canon_hash (itype);
7127 itype = type_hash_canon (hash, itype);
7128
7129 return itype;
7130 }
7131
7132 /* Wrapper around build_range_type_1 with SHARED set to true. */
7133
7134 tree
7135 build_range_type (tree type, tree lowval, tree highval)
7136 {
7137 return build_range_type_1 (type, lowval, highval, true);
7138 }
7139
7140 /* Wrapper around build_range_type_1 with SHARED set to false. */
7141
7142 tree
7143 build_nonshared_range_type (tree type, tree lowval, tree highval)
7144 {
7145 return build_range_type_1 (type, lowval, highval, false);
7146 }
7147
7148 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7149 MAXVAL should be the maximum value in the domain
7150 (one less than the length of the array).
7151
7152 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7153 We don't enforce this limit, that is up to caller (e.g. language front end).
7154 The limit exists because the result is a signed type and we don't handle
7155 sizes that use more than one HOST_WIDE_INT. */
7156
7157 tree
7158 build_index_type (tree maxval)
7159 {
7160 return build_range_type (sizetype, size_zero_node, maxval);
7161 }
7162
7163 /* Return true if the debug information for TYPE, a subtype, should be emitted
7164 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7165 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7166 debug info and doesn't reflect the source code. */
7167
7168 bool
7169 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7170 {
7171 tree base_type = TREE_TYPE (type), low, high;
7172
7173 /* Subrange types have a base type which is an integral type. */
7174 if (!INTEGRAL_TYPE_P (base_type))
7175 return false;
7176
7177 /* Get the real bounds of the subtype. */
7178 if (lang_hooks.types.get_subrange_bounds)
7179 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7180 else
7181 {
7182 low = TYPE_MIN_VALUE (type);
7183 high = TYPE_MAX_VALUE (type);
7184 }
7185
7186 /* If the type and its base type have the same representation and the same
7187 name, then the type is not a subrange but a copy of the base type. */
7188 if ((TREE_CODE (base_type) == INTEGER_TYPE
7189 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7190 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7191 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7192 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type))
7193 && TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (base_type))
7194 return false;
7195
7196 if (lowval)
7197 *lowval = low;
7198 if (highval)
7199 *highval = high;
7200 return true;
7201 }
7202
7203 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7204 and number of elements specified by the range of values of INDEX_TYPE.
7205 If TYPELESS_STORAGE is true, TYPE_TYPELESS_STORAGE flag is set on the type.
7206 If SHARED is true, reuse such a type that has already been constructed.
7207 If SET_CANONICAL is true, compute TYPE_CANONICAL from the element type. */
7208
7209 tree
7210 build_array_type_1 (tree elt_type, tree index_type, bool typeless_storage,
7211 bool shared, bool set_canonical)
7212 {
7213 tree t;
7214
7215 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7216 {
7217 error ("arrays of functions are not meaningful");
7218 elt_type = integer_type_node;
7219 }
7220
7221 t = make_node (ARRAY_TYPE);
7222 TREE_TYPE (t) = elt_type;
7223 TYPE_DOMAIN (t) = index_type;
7224 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7225 TYPE_TYPELESS_STORAGE (t) = typeless_storage;
7226 layout_type (t);
7227
7228 if (shared)
7229 {
7230 hashval_t hash = type_hash_canon_hash (t);
7231 t = type_hash_canon (hash, t);
7232 }
7233
7234 if (TYPE_CANONICAL (t) == t && set_canonical)
7235 {
7236 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7237 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type))
7238 || in_lto_p)
7239 SET_TYPE_STRUCTURAL_EQUALITY (t);
7240 else if (TYPE_CANONICAL (elt_type) != elt_type
7241 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7242 TYPE_CANONICAL (t)
7243 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7244 index_type
7245 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7246 typeless_storage, shared, set_canonical);
7247 }
7248
7249 return t;
7250 }
7251
7252 /* Wrapper around build_array_type_1 with SHARED set to true. */
7253
7254 tree
7255 build_array_type (tree elt_type, tree index_type, bool typeless_storage)
7256 {
7257 return
7258 build_array_type_1 (elt_type, index_type, typeless_storage, true, true);
7259 }
7260
7261 /* Wrapper around build_array_type_1 with SHARED set to false. */
7262
7263 tree
7264 build_nonshared_array_type (tree elt_type, tree index_type)
7265 {
7266 return build_array_type_1 (elt_type, index_type, false, false, true);
7267 }
7268
7269 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7270 sizetype. */
7271
7272 tree
7273 build_array_type_nelts (tree elt_type, poly_uint64 nelts)
7274 {
7275 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7276 }
7277
7278 /* Recursively examines the array elements of TYPE, until a non-array
7279 element type is found. */
7280
7281 tree
7282 strip_array_types (tree type)
7283 {
7284 while (TREE_CODE (type) == ARRAY_TYPE)
7285 type = TREE_TYPE (type);
7286
7287 return type;
7288 }
7289
7290 /* Computes the canonical argument types from the argument type list
7291 ARGTYPES.
7292
7293 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7294 on entry to this function, or if any of the ARGTYPES are
7295 structural.
7296
7297 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7298 true on entry to this function, or if any of the ARGTYPES are
7299 non-canonical.
7300
7301 Returns a canonical argument list, which may be ARGTYPES when the
7302 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7303 true) or would not differ from ARGTYPES. */
7304
7305 static tree
7306 maybe_canonicalize_argtypes (tree argtypes,
7307 bool *any_structural_p,
7308 bool *any_noncanonical_p)
7309 {
7310 tree arg;
7311 bool any_noncanonical_argtypes_p = false;
7312
7313 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7314 {
7315 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7316 /* Fail gracefully by stating that the type is structural. */
7317 *any_structural_p = true;
7318 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7319 *any_structural_p = true;
7320 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7321 || TREE_PURPOSE (arg))
7322 /* If the argument has a default argument, we consider it
7323 non-canonical even though the type itself is canonical.
7324 That way, different variants of function and method types
7325 with default arguments will all point to the variant with
7326 no defaults as their canonical type. */
7327 any_noncanonical_argtypes_p = true;
7328 }
7329
7330 if (*any_structural_p)
7331 return argtypes;
7332
7333 if (any_noncanonical_argtypes_p)
7334 {
7335 /* Build the canonical list of argument types. */
7336 tree canon_argtypes = NULL_TREE;
7337 bool is_void = false;
7338
7339 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7340 {
7341 if (arg == void_list_node)
7342 is_void = true;
7343 else
7344 canon_argtypes = tree_cons (NULL_TREE,
7345 TYPE_CANONICAL (TREE_VALUE (arg)),
7346 canon_argtypes);
7347 }
7348
7349 canon_argtypes = nreverse (canon_argtypes);
7350 if (is_void)
7351 canon_argtypes = chainon (canon_argtypes, void_list_node);
7352
7353 /* There is a non-canonical type. */
7354 *any_noncanonical_p = true;
7355 return canon_argtypes;
7356 }
7357
7358 /* The canonical argument types are the same as ARGTYPES. */
7359 return argtypes;
7360 }
7361
7362 /* Construct, lay out and return
7363 the type of functions returning type VALUE_TYPE
7364 given arguments of types ARG_TYPES.
7365 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7366 are data type nodes for the arguments of the function.
7367 If such a type has already been constructed, reuse it. */
7368
7369 tree
7370 build_function_type (tree value_type, tree arg_types)
7371 {
7372 tree t;
7373 inchash::hash hstate;
7374 bool any_structural_p, any_noncanonical_p;
7375 tree canon_argtypes;
7376
7377 gcc_assert (arg_types != error_mark_node);
7378
7379 if (TREE_CODE (value_type) == FUNCTION_TYPE)
7380 {
7381 error ("function return type cannot be function");
7382 value_type = integer_type_node;
7383 }
7384
7385 /* Make a node of the sort we want. */
7386 t = make_node (FUNCTION_TYPE);
7387 TREE_TYPE (t) = value_type;
7388 TYPE_ARG_TYPES (t) = arg_types;
7389
7390 /* If we already have such a type, use the old one. */
7391 hashval_t hash = type_hash_canon_hash (t);
7392 t = type_hash_canon (hash, t);
7393
7394 /* Set up the canonical type. */
7395 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7396 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7397 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
7398 &any_structural_p,
7399 &any_noncanonical_p);
7400 if (any_structural_p)
7401 SET_TYPE_STRUCTURAL_EQUALITY (t);
7402 else if (any_noncanonical_p)
7403 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
7404 canon_argtypes);
7405
7406 if (!COMPLETE_TYPE_P (t))
7407 layout_type (t);
7408 return t;
7409 }
7410
7411 /* Build a function type. The RETURN_TYPE is the type returned by the
7412 function. If VAARGS is set, no void_type_node is appended to the
7413 list. ARGP must be always be terminated be a NULL_TREE. */
7414
7415 static tree
7416 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
7417 {
7418 tree t, args, last;
7419
7420 t = va_arg (argp, tree);
7421 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
7422 args = tree_cons (NULL_TREE, t, args);
7423
7424 if (vaargs)
7425 {
7426 last = args;
7427 if (args != NULL_TREE)
7428 args = nreverse (args);
7429 gcc_assert (last != void_list_node);
7430 }
7431 else if (args == NULL_TREE)
7432 args = void_list_node;
7433 else
7434 {
7435 last = args;
7436 args = nreverse (args);
7437 TREE_CHAIN (last) = void_list_node;
7438 }
7439 args = build_function_type (return_type, args);
7440
7441 return args;
7442 }
7443
7444 /* Build a function type. The RETURN_TYPE is the type returned by the
7445 function. If additional arguments are provided, they are
7446 additional argument types. The list of argument types must always
7447 be terminated by NULL_TREE. */
7448
7449 tree
7450 build_function_type_list (tree return_type, ...)
7451 {
7452 tree args;
7453 va_list p;
7454
7455 va_start (p, return_type);
7456 args = build_function_type_list_1 (false, return_type, p);
7457 va_end (p);
7458 return args;
7459 }
7460
7461 /* Build a variable argument function type. The RETURN_TYPE is the
7462 type returned by the function. If additional arguments are provided,
7463 they are additional argument types. The list of argument types must
7464 always be terminated by NULL_TREE. */
7465
7466 tree
7467 build_varargs_function_type_list (tree return_type, ...)
7468 {
7469 tree args;
7470 va_list p;
7471
7472 va_start (p, return_type);
7473 args = build_function_type_list_1 (true, return_type, p);
7474 va_end (p);
7475
7476 return args;
7477 }
7478
7479 /* Build a function type. RETURN_TYPE is the type returned by the
7480 function; VAARGS indicates whether the function takes varargs. The
7481 function takes N named arguments, the types of which are provided in
7482 ARG_TYPES. */
7483
7484 static tree
7485 build_function_type_array_1 (bool vaargs, tree return_type, int n,
7486 tree *arg_types)
7487 {
7488 int i;
7489 tree t = vaargs ? NULL_TREE : void_list_node;
7490
7491 for (i = n - 1; i >= 0; i--)
7492 t = tree_cons (NULL_TREE, arg_types[i], t);
7493
7494 return build_function_type (return_type, t);
7495 }
7496
7497 /* Build a function type. RETURN_TYPE is the type returned by the
7498 function. The function takes N named arguments, the types of which
7499 are provided in ARG_TYPES. */
7500
7501 tree
7502 build_function_type_array (tree return_type, int n, tree *arg_types)
7503 {
7504 return build_function_type_array_1 (false, return_type, n, arg_types);
7505 }
7506
7507 /* Build a variable argument function type. RETURN_TYPE is the type
7508 returned by the function. The function takes N named arguments, the
7509 types of which are provided in ARG_TYPES. */
7510
7511 tree
7512 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
7513 {
7514 return build_function_type_array_1 (true, return_type, n, arg_types);
7515 }
7516
7517 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
7518 and ARGTYPES (a TREE_LIST) are the return type and arguments types
7519 for the method. An implicit additional parameter (of type
7520 pointer-to-BASETYPE) is added to the ARGTYPES. */
7521
7522 tree
7523 build_method_type_directly (tree basetype,
7524 tree rettype,
7525 tree argtypes)
7526 {
7527 tree t;
7528 tree ptype;
7529 bool any_structural_p, any_noncanonical_p;
7530 tree canon_argtypes;
7531
7532 /* Make a node of the sort we want. */
7533 t = make_node (METHOD_TYPE);
7534
7535 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7536 TREE_TYPE (t) = rettype;
7537 ptype = build_pointer_type (basetype);
7538
7539 /* The actual arglist for this function includes a "hidden" argument
7540 which is "this". Put it into the list of argument types. */
7541 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
7542 TYPE_ARG_TYPES (t) = argtypes;
7543
7544 /* If we already have such a type, use the old one. */
7545 hashval_t hash = type_hash_canon_hash (t);
7546 t = type_hash_canon (hash, t);
7547
7548 /* Set up the canonical type. */
7549 any_structural_p
7550 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7551 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
7552 any_noncanonical_p
7553 = (TYPE_CANONICAL (basetype) != basetype
7554 || TYPE_CANONICAL (rettype) != rettype);
7555 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
7556 &any_structural_p,
7557 &any_noncanonical_p);
7558 if (any_structural_p)
7559 SET_TYPE_STRUCTURAL_EQUALITY (t);
7560 else if (any_noncanonical_p)
7561 TYPE_CANONICAL (t)
7562 = build_method_type_directly (TYPE_CANONICAL (basetype),
7563 TYPE_CANONICAL (rettype),
7564 canon_argtypes);
7565 if (!COMPLETE_TYPE_P (t))
7566 layout_type (t);
7567
7568 return t;
7569 }
7570
7571 /* Construct, lay out and return the type of methods belonging to class
7572 BASETYPE and whose arguments and values are described by TYPE.
7573 If that type exists already, reuse it.
7574 TYPE must be a FUNCTION_TYPE node. */
7575
7576 tree
7577 build_method_type (tree basetype, tree type)
7578 {
7579 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
7580
7581 return build_method_type_directly (basetype,
7582 TREE_TYPE (type),
7583 TYPE_ARG_TYPES (type));
7584 }
7585
7586 /* Construct, lay out and return the type of offsets to a value
7587 of type TYPE, within an object of type BASETYPE.
7588 If a suitable offset type exists already, reuse it. */
7589
7590 tree
7591 build_offset_type (tree basetype, tree type)
7592 {
7593 tree t;
7594
7595 /* Make a node of the sort we want. */
7596 t = make_node (OFFSET_TYPE);
7597
7598 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7599 TREE_TYPE (t) = type;
7600
7601 /* If we already have such a type, use the old one. */
7602 hashval_t hash = type_hash_canon_hash (t);
7603 t = type_hash_canon (hash, t);
7604
7605 if (!COMPLETE_TYPE_P (t))
7606 layout_type (t);
7607
7608 if (TYPE_CANONICAL (t) == t)
7609 {
7610 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7611 || TYPE_STRUCTURAL_EQUALITY_P (type))
7612 SET_TYPE_STRUCTURAL_EQUALITY (t);
7613 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
7614 || TYPE_CANONICAL (type) != type)
7615 TYPE_CANONICAL (t)
7616 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
7617 TYPE_CANONICAL (type));
7618 }
7619
7620 return t;
7621 }
7622
7623 /* Create a complex type whose components are COMPONENT_TYPE.
7624
7625 If NAMED is true, the type is given a TYPE_NAME. We do not always
7626 do so because this creates a DECL node and thus make the DECL_UIDs
7627 dependent on the type canonicalization hashtable, which is GC-ed,
7628 so the DECL_UIDs would not be stable wrt garbage collection. */
7629
7630 tree
7631 build_complex_type (tree component_type, bool named)
7632 {
7633 gcc_assert (INTEGRAL_TYPE_P (component_type)
7634 || SCALAR_FLOAT_TYPE_P (component_type)
7635 || FIXED_POINT_TYPE_P (component_type));
7636
7637 /* Make a node of the sort we want. */
7638 tree probe = make_node (COMPLEX_TYPE);
7639
7640 TREE_TYPE (probe) = TYPE_MAIN_VARIANT (component_type);
7641
7642 /* If we already have such a type, use the old one. */
7643 hashval_t hash = type_hash_canon_hash (probe);
7644 tree t = type_hash_canon (hash, probe);
7645
7646 if (t == probe)
7647 {
7648 /* We created a new type. The hash insertion will have laid
7649 out the type. We need to check the canonicalization and
7650 maybe set the name. */
7651 gcc_checking_assert (COMPLETE_TYPE_P (t)
7652 && !TYPE_NAME (t)
7653 && TYPE_CANONICAL (t) == t);
7654
7655 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (t)))
7656 SET_TYPE_STRUCTURAL_EQUALITY (t);
7657 else if (TYPE_CANONICAL (TREE_TYPE (t)) != TREE_TYPE (t))
7658 TYPE_CANONICAL (t)
7659 = build_complex_type (TYPE_CANONICAL (TREE_TYPE (t)), named);
7660
7661 /* We need to create a name, since complex is a fundamental type. */
7662 if (named)
7663 {
7664 const char *name = NULL;
7665
7666 if (TREE_TYPE (t) == char_type_node)
7667 name = "complex char";
7668 else if (TREE_TYPE (t) == signed_char_type_node)
7669 name = "complex signed char";
7670 else if (TREE_TYPE (t) == unsigned_char_type_node)
7671 name = "complex unsigned char";
7672 else if (TREE_TYPE (t) == short_integer_type_node)
7673 name = "complex short int";
7674 else if (TREE_TYPE (t) == short_unsigned_type_node)
7675 name = "complex short unsigned int";
7676 else if (TREE_TYPE (t) == integer_type_node)
7677 name = "complex int";
7678 else if (TREE_TYPE (t) == unsigned_type_node)
7679 name = "complex unsigned int";
7680 else if (TREE_TYPE (t) == long_integer_type_node)
7681 name = "complex long int";
7682 else if (TREE_TYPE (t) == long_unsigned_type_node)
7683 name = "complex long unsigned int";
7684 else if (TREE_TYPE (t) == long_long_integer_type_node)
7685 name = "complex long long int";
7686 else if (TREE_TYPE (t) == long_long_unsigned_type_node)
7687 name = "complex long long unsigned int";
7688
7689 if (name != NULL)
7690 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
7691 get_identifier (name), t);
7692 }
7693 }
7694
7695 return build_qualified_type (t, TYPE_QUALS (component_type));
7696 }
7697
7698 /* If TYPE is a real or complex floating-point type and the target
7699 does not directly support arithmetic on TYPE then return the wider
7700 type to be used for arithmetic on TYPE. Otherwise, return
7701 NULL_TREE. */
7702
7703 tree
7704 excess_precision_type (tree type)
7705 {
7706 /* The target can give two different responses to the question of
7707 which excess precision mode it would like depending on whether we
7708 are in -fexcess-precision=standard or -fexcess-precision=fast. */
7709
7710 enum excess_precision_type requested_type
7711 = (flag_excess_precision == EXCESS_PRECISION_FAST
7712 ? EXCESS_PRECISION_TYPE_FAST
7713 : (flag_excess_precision == EXCESS_PRECISION_FLOAT16
7714 ? EXCESS_PRECISION_TYPE_FLOAT16 : EXCESS_PRECISION_TYPE_STANDARD));
7715
7716 enum flt_eval_method target_flt_eval_method
7717 = targetm.c.excess_precision (requested_type);
7718
7719 /* The target should not ask for unpredictable float evaluation (though
7720 it might advertise that implicitly the evaluation is unpredictable,
7721 but we don't care about that here, it will have been reported
7722 elsewhere). If it does ask for unpredictable evaluation, we have
7723 nothing to do here. */
7724 gcc_assert (target_flt_eval_method != FLT_EVAL_METHOD_UNPREDICTABLE);
7725
7726 /* Nothing to do. The target has asked for all types we know about
7727 to be computed with their native precision and range. */
7728 if (target_flt_eval_method == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
7729 return NULL_TREE;
7730
7731 /* The target will promote this type in a target-dependent way, so excess
7732 precision ought to leave it alone. */
7733 if (targetm.promoted_type (type) != NULL_TREE)
7734 return NULL_TREE;
7735
7736 machine_mode float16_type_mode = (float16_type_node
7737 ? TYPE_MODE (float16_type_node)
7738 : VOIDmode);
7739 machine_mode bfloat16_type_mode = (bfloat16_type_node
7740 ? TYPE_MODE (bfloat16_type_node)
7741 : VOIDmode);
7742 machine_mode float_type_mode = TYPE_MODE (float_type_node);
7743 machine_mode double_type_mode = TYPE_MODE (double_type_node);
7744
7745 switch (TREE_CODE (type))
7746 {
7747 case REAL_TYPE:
7748 {
7749 machine_mode type_mode = TYPE_MODE (type);
7750 switch (target_flt_eval_method)
7751 {
7752 case FLT_EVAL_METHOD_PROMOTE_TO_FLOAT:
7753 if (type_mode == float16_type_mode
7754 || type_mode == bfloat16_type_mode)
7755 return float_type_node;
7756 break;
7757 case FLT_EVAL_METHOD_PROMOTE_TO_DOUBLE:
7758 if (type_mode == float16_type_mode
7759 || type_mode == bfloat16_type_mode
7760 || type_mode == float_type_mode)
7761 return double_type_node;
7762 break;
7763 case FLT_EVAL_METHOD_PROMOTE_TO_LONG_DOUBLE:
7764 if (type_mode == float16_type_mode
7765 || type_mode == bfloat16_type_mode
7766 || type_mode == float_type_mode
7767 || type_mode == double_type_mode)
7768 return long_double_type_node;
7769 break;
7770 default:
7771 gcc_unreachable ();
7772 }
7773 break;
7774 }
7775 case COMPLEX_TYPE:
7776 {
7777 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
7778 return NULL_TREE;
7779 machine_mode type_mode = TYPE_MODE (TREE_TYPE (type));
7780 switch (target_flt_eval_method)
7781 {
7782 case FLT_EVAL_METHOD_PROMOTE_TO_FLOAT:
7783 if (type_mode == float16_type_mode
7784 || type_mode == bfloat16_type_mode)
7785 return complex_float_type_node;
7786 break;
7787 case FLT_EVAL_METHOD_PROMOTE_TO_DOUBLE:
7788 if (type_mode == float16_type_mode
7789 || type_mode == bfloat16_type_mode
7790 || type_mode == float_type_mode)
7791 return complex_double_type_node;
7792 break;
7793 case FLT_EVAL_METHOD_PROMOTE_TO_LONG_DOUBLE:
7794 if (type_mode == float16_type_mode
7795 || type_mode == bfloat16_type_mode
7796 || type_mode == float_type_mode
7797 || type_mode == double_type_mode)
7798 return complex_long_double_type_node;
7799 break;
7800 default:
7801 gcc_unreachable ();
7802 }
7803 break;
7804 }
7805 default:
7806 break;
7807 }
7808
7809 return NULL_TREE;
7810 }
7811 \f
7812 /* Return OP, stripped of any conversions to wider types as much as is safe.
7813 Converting the value back to OP's type makes a value equivalent to OP.
7814
7815 If FOR_TYPE is nonzero, we return a value which, if converted to
7816 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
7817
7818 OP must have integer, real or enumeral type. Pointers are not allowed!
7819
7820 There are some cases where the obvious value we could return
7821 would regenerate to OP if converted to OP's type,
7822 but would not extend like OP to wider types.
7823 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
7824 For example, if OP is (unsigned short)(signed char)-1,
7825 we avoid returning (signed char)-1 if FOR_TYPE is int,
7826 even though extending that to an unsigned short would regenerate OP,
7827 since the result of extending (signed char)-1 to (int)
7828 is different from (int) OP. */
7829
7830 tree
7831 get_unwidened (tree op, tree for_type)
7832 {
7833 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
7834 tree type = TREE_TYPE (op);
7835 unsigned final_prec
7836 = TYPE_PRECISION (for_type != 0 ? for_type : type);
7837 int uns
7838 = (for_type != 0 && for_type != type
7839 && final_prec > TYPE_PRECISION (type)
7840 && TYPE_UNSIGNED (type));
7841 tree win = op;
7842
7843 while (CONVERT_EXPR_P (op))
7844 {
7845 int bitschange;
7846
7847 /* TYPE_PRECISION on vector types has different meaning
7848 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
7849 so avoid them here. */
7850 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
7851 break;
7852
7853 bitschange = TYPE_PRECISION (TREE_TYPE (op))
7854 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
7855
7856 /* Truncations are many-one so cannot be removed.
7857 Unless we are later going to truncate down even farther. */
7858 if (bitschange < 0
7859 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
7860 break;
7861
7862 /* See what's inside this conversion. If we decide to strip it,
7863 we will set WIN. */
7864 op = TREE_OPERAND (op, 0);
7865
7866 /* If we have not stripped any zero-extensions (uns is 0),
7867 we can strip any kind of extension.
7868 If we have previously stripped a zero-extension,
7869 only zero-extensions can safely be stripped.
7870 Any extension can be stripped if the bits it would produce
7871 are all going to be discarded later by truncating to FOR_TYPE. */
7872
7873 if (bitschange > 0)
7874 {
7875 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
7876 win = op;
7877 /* TYPE_UNSIGNED says whether this is a zero-extension.
7878 Let's avoid computing it if it does not affect WIN
7879 and if UNS will not be needed again. */
7880 if ((uns
7881 || CONVERT_EXPR_P (op))
7882 && TYPE_UNSIGNED (TREE_TYPE (op)))
7883 {
7884 uns = 1;
7885 win = op;
7886 }
7887 }
7888 }
7889
7890 /* If we finally reach a constant see if it fits in sth smaller and
7891 in that case convert it. */
7892 if (TREE_CODE (win) == INTEGER_CST)
7893 {
7894 tree wtype = TREE_TYPE (win);
7895 unsigned prec = wi::min_precision (wi::to_wide (win), TYPE_SIGN (wtype));
7896 if (for_type)
7897 prec = MAX (prec, final_prec);
7898 if (prec < TYPE_PRECISION (wtype))
7899 {
7900 tree t = lang_hooks.types.type_for_size (prec, TYPE_UNSIGNED (wtype));
7901 if (t && TYPE_PRECISION (t) < TYPE_PRECISION (wtype))
7902 win = fold_convert (t, win);
7903 }
7904 }
7905
7906 return win;
7907 }
7908 \f
7909 /* Return OP or a simpler expression for a narrower value
7910 which can be sign-extended or zero-extended to give back OP.
7911 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
7912 or 0 if the value should be sign-extended. */
7913
7914 tree
7915 get_narrower (tree op, int *unsignedp_ptr)
7916 {
7917 int uns = 0;
7918 int first = 1;
7919 tree win = op;
7920 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
7921
7922 if (TREE_CODE (op) == COMPOUND_EXPR)
7923 {
7924 do
7925 op = TREE_OPERAND (op, 1);
7926 while (TREE_CODE (op) == COMPOUND_EXPR);
7927 tree ret = get_narrower (op, unsignedp_ptr);
7928 if (ret == op)
7929 return win;
7930 auto_vec <tree, 16> v;
7931 unsigned int i;
7932 for (op = win; TREE_CODE (op) == COMPOUND_EXPR;
7933 op = TREE_OPERAND (op, 1))
7934 v.safe_push (op);
7935 FOR_EACH_VEC_ELT_REVERSE (v, i, op)
7936 ret = build2_loc (EXPR_LOCATION (op), COMPOUND_EXPR,
7937 TREE_TYPE (ret), TREE_OPERAND (op, 0),
7938 ret);
7939 return ret;
7940 }
7941 while (TREE_CODE (op) == NOP_EXPR)
7942 {
7943 int bitschange
7944 = (TYPE_PRECISION (TREE_TYPE (op))
7945 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
7946
7947 /* Truncations are many-one so cannot be removed. */
7948 if (bitschange < 0)
7949 break;
7950
7951 /* See what's inside this conversion. If we decide to strip it,
7952 we will set WIN. */
7953
7954 if (bitschange > 0)
7955 {
7956 op = TREE_OPERAND (op, 0);
7957 /* An extension: the outermost one can be stripped,
7958 but remember whether it is zero or sign extension. */
7959 if (first)
7960 uns = TYPE_UNSIGNED (TREE_TYPE (op));
7961 /* Otherwise, if a sign extension has been stripped,
7962 only sign extensions can now be stripped;
7963 if a zero extension has been stripped, only zero-extensions. */
7964 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
7965 break;
7966 first = 0;
7967 }
7968 else /* bitschange == 0 */
7969 {
7970 /* A change in nominal type can always be stripped, but we must
7971 preserve the unsignedness. */
7972 if (first)
7973 uns = TYPE_UNSIGNED (TREE_TYPE (op));
7974 first = 0;
7975 op = TREE_OPERAND (op, 0);
7976 /* Keep trying to narrow, but don't assign op to win if it
7977 would turn an integral type into something else. */
7978 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
7979 continue;
7980 }
7981
7982 win = op;
7983 }
7984
7985 if (TREE_CODE (op) == COMPONENT_REF
7986 /* Since type_for_size always gives an integer type. */
7987 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
7988 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
7989 /* Ensure field is laid out already. */
7990 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
7991 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
7992 {
7993 unsigned HOST_WIDE_INT innerprec
7994 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
7995 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
7996 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
7997 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
7998
7999 /* We can get this structure field in a narrower type that fits it,
8000 but the resulting extension to its nominal type (a fullword type)
8001 must satisfy the same conditions as for other extensions.
8002
8003 Do this only for fields that are aligned (not bit-fields),
8004 because when bit-field insns will be used there is no
8005 advantage in doing this. */
8006
8007 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8008 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8009 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8010 && type != 0)
8011 {
8012 if (first)
8013 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8014 win = fold_convert (type, op);
8015 }
8016 }
8017
8018 *unsignedp_ptr = uns;
8019 return win;
8020 }
8021 \f
8022 /* Return true if integer constant C has a value that is permissible
8023 for TYPE, an integral type. */
8024
8025 bool
8026 int_fits_type_p (const_tree c, const_tree type)
8027 {
8028 tree type_low_bound, type_high_bound;
8029 bool ok_for_low_bound, ok_for_high_bound;
8030 signop sgn_c = TYPE_SIGN (TREE_TYPE (c));
8031
8032 /* Non-standard boolean types can have arbitrary precision but various
8033 transformations assume that they can only take values 0 and +/-1. */
8034 if (TREE_CODE (type) == BOOLEAN_TYPE)
8035 return wi::fits_to_boolean_p (wi::to_wide (c), type);
8036
8037 retry:
8038 type_low_bound = TYPE_MIN_VALUE (type);
8039 type_high_bound = TYPE_MAX_VALUE (type);
8040
8041 /* If at least one bound of the type is a constant integer, we can check
8042 ourselves and maybe make a decision. If no such decision is possible, but
8043 this type is a subtype, try checking against that. Otherwise, use
8044 fits_to_tree_p, which checks against the precision.
8045
8046 Compute the status for each possibly constant bound, and return if we see
8047 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8048 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8049 for "constant known to fit". */
8050
8051 /* Check if c >= type_low_bound. */
8052 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8053 {
8054 if (tree_int_cst_lt (c, type_low_bound))
8055 return false;
8056 ok_for_low_bound = true;
8057 }
8058 else
8059 ok_for_low_bound = false;
8060
8061 /* Check if c <= type_high_bound. */
8062 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8063 {
8064 if (tree_int_cst_lt (type_high_bound, c))
8065 return false;
8066 ok_for_high_bound = true;
8067 }
8068 else
8069 ok_for_high_bound = false;
8070
8071 /* If the constant fits both bounds, the result is known. */
8072 if (ok_for_low_bound && ok_for_high_bound)
8073 return true;
8074
8075 /* Perform some generic filtering which may allow making a decision
8076 even if the bounds are not constant. First, negative integers
8077 never fit in unsigned types, */
8078 if (TYPE_UNSIGNED (type) && sgn_c == SIGNED && wi::neg_p (wi::to_wide (c)))
8079 return false;
8080
8081 /* Second, narrower types always fit in wider ones. */
8082 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8083 return true;
8084
8085 /* Third, unsigned integers with top bit set never fit signed types. */
8086 if (!TYPE_UNSIGNED (type) && sgn_c == UNSIGNED)
8087 {
8088 int prec = GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (TREE_TYPE (c))) - 1;
8089 if (prec < TYPE_PRECISION (TREE_TYPE (c)))
8090 {
8091 /* When a tree_cst is converted to a wide-int, the precision
8092 is taken from the type. However, if the precision of the
8093 mode underneath the type is smaller than that, it is
8094 possible that the value will not fit. The test below
8095 fails if any bit is set between the sign bit of the
8096 underlying mode and the top bit of the type. */
8097 if (wi::zext (wi::to_wide (c), prec - 1) != wi::to_wide (c))
8098 return false;
8099 }
8100 else if (wi::neg_p (wi::to_wide (c)))
8101 return false;
8102 }
8103
8104 /* If we haven't been able to decide at this point, there nothing more we
8105 can check ourselves here. Look at the base type if we have one and it
8106 has the same precision. */
8107 if (TREE_CODE (type) == INTEGER_TYPE
8108 && TREE_TYPE (type) != 0
8109 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8110 {
8111 type = TREE_TYPE (type);
8112 goto retry;
8113 }
8114
8115 /* Or to fits_to_tree_p, if nothing else. */
8116 return wi::fits_to_tree_p (wi::to_wide (c), type);
8117 }
8118
8119 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8120 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8121 represented (assuming two's-complement arithmetic) within the bit
8122 precision of the type are returned instead. */
8123
8124 void
8125 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8126 {
8127 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8128 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8129 wi::to_mpz (wi::to_wide (TYPE_MIN_VALUE (type)), min, TYPE_SIGN (type));
8130 else
8131 {
8132 if (TYPE_UNSIGNED (type))
8133 mpz_set_ui (min, 0);
8134 else
8135 {
8136 wide_int mn = wi::min_value (TYPE_PRECISION (type), SIGNED);
8137 wi::to_mpz (mn, min, SIGNED);
8138 }
8139 }
8140
8141 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8142 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8143 wi::to_mpz (wi::to_wide (TYPE_MAX_VALUE (type)), max, TYPE_SIGN (type));
8144 else
8145 {
8146 wide_int mn = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
8147 wi::to_mpz (mn, max, TYPE_SIGN (type));
8148 }
8149 }
8150
8151 /* Return true if VAR is an automatic variable. */
8152
8153 bool
8154 auto_var_p (const_tree var)
8155 {
8156 return ((((VAR_P (var) && ! DECL_EXTERNAL (var))
8157 || TREE_CODE (var) == PARM_DECL)
8158 && ! TREE_STATIC (var))
8159 || TREE_CODE (var) == RESULT_DECL);
8160 }
8161
8162 /* Return true if VAR is an automatic variable defined in function FN. */
8163
8164 bool
8165 auto_var_in_fn_p (const_tree var, const_tree fn)
8166 {
8167 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8168 && (auto_var_p (var)
8169 || TREE_CODE (var) == LABEL_DECL));
8170 }
8171
8172 /* Subprogram of following function. Called by walk_tree.
8173
8174 Return *TP if it is an automatic variable or parameter of the
8175 function passed in as DATA. */
8176
8177 static tree
8178 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8179 {
8180 tree fn = (tree) data;
8181
8182 if (TYPE_P (*tp))
8183 *walk_subtrees = 0;
8184
8185 else if (DECL_P (*tp)
8186 && auto_var_in_fn_p (*tp, fn))
8187 return *tp;
8188
8189 return NULL_TREE;
8190 }
8191
8192 /* Returns true if T is, contains, or refers to a type with variable
8193 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8194 arguments, but not the return type. If FN is nonzero, only return
8195 true if a modifier of the type or position of FN is a variable or
8196 parameter inside FN.
8197
8198 This concept is more general than that of C99 'variably modified types':
8199 in C99, a struct type is never variably modified because a VLA may not
8200 appear as a structure member. However, in GNU C code like:
8201
8202 struct S { int i[f()]; };
8203
8204 is valid, and other languages may define similar constructs. */
8205
8206 bool
8207 variably_modified_type_p (tree type, tree fn)
8208 {
8209 tree t;
8210
8211 /* Test if T is either variable (if FN is zero) or an expression containing
8212 a variable in FN. If TYPE isn't gimplified, return true also if
8213 gimplify_one_sizepos would gimplify the expression into a local
8214 variable. */
8215 #define RETURN_TRUE_IF_VAR(T) \
8216 do { tree _t = (T); \
8217 if (_t != NULL_TREE \
8218 && _t != error_mark_node \
8219 && !CONSTANT_CLASS_P (_t) \
8220 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8221 && (!fn \
8222 || (!TYPE_SIZES_GIMPLIFIED (type) \
8223 && (TREE_CODE (_t) != VAR_DECL \
8224 && !CONTAINS_PLACEHOLDER_P (_t))) \
8225 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8226 return true; } while (0)
8227
8228 if (type == error_mark_node)
8229 return false;
8230
8231 /* If TYPE itself has variable size, it is variably modified. */
8232 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8233 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8234
8235 switch (TREE_CODE (type))
8236 {
8237 case POINTER_TYPE:
8238 case REFERENCE_TYPE:
8239 case VECTOR_TYPE:
8240 /* Ada can have pointer types refering to themselves indirectly. */
8241 if (TREE_VISITED (type))
8242 return false;
8243 TREE_VISITED (type) = true;
8244 if (variably_modified_type_p (TREE_TYPE (type), fn))
8245 {
8246 TREE_VISITED (type) = false;
8247 return true;
8248 }
8249 TREE_VISITED (type) = false;
8250 break;
8251
8252 case FUNCTION_TYPE:
8253 case METHOD_TYPE:
8254 /* If TYPE is a function type, it is variably modified if the
8255 return type is variably modified. */
8256 if (variably_modified_type_p (TREE_TYPE (type), fn))
8257 return true;
8258 break;
8259
8260 case INTEGER_TYPE:
8261 case REAL_TYPE:
8262 case FIXED_POINT_TYPE:
8263 case ENUMERAL_TYPE:
8264 case BOOLEAN_TYPE:
8265 /* Scalar types are variably modified if their end points
8266 aren't constant. */
8267 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8268 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8269 break;
8270
8271 case RECORD_TYPE:
8272 case UNION_TYPE:
8273 case QUAL_UNION_TYPE:
8274 /* We can't see if any of the fields are variably-modified by the
8275 definition we normally use, since that would produce infinite
8276 recursion via pointers. */
8277 /* This is variably modified if some field's type is. */
8278 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8279 if (TREE_CODE (t) == FIELD_DECL)
8280 {
8281 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8282 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8283 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8284
8285 /* If the type is a qualified union, then the DECL_QUALIFIER
8286 of fields can also be an expression containing a variable. */
8287 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8288 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8289
8290 /* If the field is a qualified union, then it's only a container
8291 for what's inside so we look into it. That's necessary in LTO
8292 mode because the sizes of the field tested above have been set
8293 to PLACEHOLDER_EXPRs by free_lang_data. */
8294 if (TREE_CODE (TREE_TYPE (t)) == QUAL_UNION_TYPE
8295 && variably_modified_type_p (TREE_TYPE (t), fn))
8296 return true;
8297 }
8298 break;
8299
8300 case ARRAY_TYPE:
8301 /* Do not call ourselves to avoid infinite recursion. This is
8302 variably modified if the element type is. */
8303 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8304 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8305 break;
8306
8307 default:
8308 break;
8309 }
8310
8311 /* The current language may have other cases to check, but in general,
8312 all other types are not variably modified. */
8313 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8314
8315 #undef RETURN_TRUE_IF_VAR
8316 }
8317
8318 /* Given a DECL or TYPE, return the scope in which it was declared, or
8319 NULL_TREE if there is no containing scope. */
8320
8321 tree
8322 get_containing_scope (const_tree t)
8323 {
8324 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8325 }
8326
8327 /* Returns the ultimate TRANSLATION_UNIT_DECL context of DECL or NULL. */
8328
8329 const_tree
8330 get_ultimate_context (const_tree decl)
8331 {
8332 while (decl && TREE_CODE (decl) != TRANSLATION_UNIT_DECL)
8333 {
8334 if (TREE_CODE (decl) == BLOCK)
8335 decl = BLOCK_SUPERCONTEXT (decl);
8336 else
8337 decl = get_containing_scope (decl);
8338 }
8339 return decl;
8340 }
8341
8342 /* Return the innermost context enclosing DECL that is
8343 a FUNCTION_DECL, or zero if none. */
8344
8345 tree
8346 decl_function_context (const_tree decl)
8347 {
8348 tree context;
8349
8350 if (TREE_CODE (decl) == ERROR_MARK)
8351 return 0;
8352
8353 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8354 where we look up the function at runtime. Such functions always take
8355 a first argument of type 'pointer to real context'.
8356
8357 C++ should really be fixed to use DECL_CONTEXT for the real context,
8358 and use something else for the "virtual context". */
8359 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
8360 context
8361 = TYPE_MAIN_VARIANT
8362 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8363 else
8364 context = DECL_CONTEXT (decl);
8365
8366 while (context && TREE_CODE (context) != FUNCTION_DECL)
8367 {
8368 if (TREE_CODE (context) == BLOCK)
8369 context = BLOCK_SUPERCONTEXT (context);
8370 else
8371 context = get_containing_scope (context);
8372 }
8373
8374 return context;
8375 }
8376
8377 /* Return the innermost context enclosing DECL that is
8378 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8379 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8380
8381 tree
8382 decl_type_context (const_tree decl)
8383 {
8384 tree context = DECL_CONTEXT (decl);
8385
8386 while (context)
8387 switch (TREE_CODE (context))
8388 {
8389 case NAMESPACE_DECL:
8390 case TRANSLATION_UNIT_DECL:
8391 return NULL_TREE;
8392
8393 case RECORD_TYPE:
8394 case UNION_TYPE:
8395 case QUAL_UNION_TYPE:
8396 return context;
8397
8398 case TYPE_DECL:
8399 case FUNCTION_DECL:
8400 context = DECL_CONTEXT (context);
8401 break;
8402
8403 case BLOCK:
8404 context = BLOCK_SUPERCONTEXT (context);
8405 break;
8406
8407 default:
8408 gcc_unreachable ();
8409 }
8410
8411 return NULL_TREE;
8412 }
8413
8414 /* CALL is a CALL_EXPR. Return the declaration for the function
8415 called, or NULL_TREE if the called function cannot be
8416 determined. */
8417
8418 tree
8419 get_callee_fndecl (const_tree call)
8420 {
8421 tree addr;
8422
8423 if (call == error_mark_node)
8424 return error_mark_node;
8425
8426 /* It's invalid to call this function with anything but a
8427 CALL_EXPR. */
8428 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8429
8430 /* The first operand to the CALL is the address of the function
8431 called. */
8432 addr = CALL_EXPR_FN (call);
8433
8434 /* If there is no function, return early. */
8435 if (addr == NULL_TREE)
8436 return NULL_TREE;
8437
8438 STRIP_NOPS (addr);
8439
8440 /* If this is a readonly function pointer, extract its initial value. */
8441 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8442 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8443 && DECL_INITIAL (addr))
8444 addr = DECL_INITIAL (addr);
8445
8446 /* If the address is just `&f' for some function `f', then we know
8447 that `f' is being called. */
8448 if (TREE_CODE (addr) == ADDR_EXPR
8449 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8450 return TREE_OPERAND (addr, 0);
8451
8452 /* We couldn't figure out what was being called. */
8453 return NULL_TREE;
8454 }
8455
8456 /* Return true when STMTs arguments and return value match those of FNDECL,
8457 a decl of a builtin function. */
8458
8459 static bool
8460 tree_builtin_call_types_compatible_p (const_tree call, tree fndecl)
8461 {
8462 gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) != NOT_BUILT_IN);
8463
8464 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
8465 if (tree decl = builtin_decl_explicit (DECL_FUNCTION_CODE (fndecl)))
8466 fndecl = decl;
8467
8468 bool gimple_form = (cfun && (cfun->curr_properties & PROP_gimple)) != 0;
8469 if (gimple_form
8470 ? !useless_type_conversion_p (TREE_TYPE (call),
8471 TREE_TYPE (TREE_TYPE (fndecl)))
8472 : (TYPE_MAIN_VARIANT (TREE_TYPE (call))
8473 != TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))))
8474 return false;
8475
8476 tree targs = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
8477 unsigned nargs = call_expr_nargs (call);
8478 for (unsigned i = 0; i < nargs; ++i, targs = TREE_CHAIN (targs))
8479 {
8480 /* Variadic args follow. */
8481 if (!targs)
8482 return true;
8483 tree arg = CALL_EXPR_ARG (call, i);
8484 tree type = TREE_VALUE (targs);
8485 if (gimple_form
8486 ? !useless_type_conversion_p (type, TREE_TYPE (arg))
8487 : TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (TREE_TYPE (arg)))
8488 {
8489 /* For pointer arguments be more forgiving, e.g. due to
8490 FILE * vs. fileptr_type_node, or say char * vs. const char *
8491 differences etc. */
8492 if (!gimple_form
8493 && POINTER_TYPE_P (type)
8494 && POINTER_TYPE_P (TREE_TYPE (arg))
8495 && tree_nop_conversion_p (type, TREE_TYPE (arg)))
8496 continue;
8497 /* char/short integral arguments are promoted to int
8498 by several frontends if targetm.calls.promote_prototypes
8499 is true. Allow such promotion too. */
8500 if (INTEGRAL_TYPE_P (type)
8501 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
8502 && INTEGRAL_TYPE_P (TREE_TYPE (arg))
8503 && !TYPE_UNSIGNED (TREE_TYPE (arg))
8504 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl))
8505 && (gimple_form
8506 ? useless_type_conversion_p (integer_type_node,
8507 TREE_TYPE (arg))
8508 : tree_nop_conversion_p (integer_type_node,
8509 TREE_TYPE (arg))))
8510 continue;
8511 return false;
8512 }
8513 }
8514 if (targs && !VOID_TYPE_P (TREE_VALUE (targs)))
8515 return false;
8516 return true;
8517 }
8518
8519 /* If CALL_EXPR CALL calls a normal built-in function or an internal function,
8520 return the associated function code, otherwise return CFN_LAST. */
8521
8522 combined_fn
8523 get_call_combined_fn (const_tree call)
8524 {
8525 /* It's invalid to call this function with anything but a CALL_EXPR. */
8526 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8527
8528 if (!CALL_EXPR_FN (call))
8529 return as_combined_fn (CALL_EXPR_IFN (call));
8530
8531 tree fndecl = get_callee_fndecl (call);
8532 if (fndecl
8533 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
8534 && tree_builtin_call_types_compatible_p (call, fndecl))
8535 return as_combined_fn (DECL_FUNCTION_CODE (fndecl));
8536
8537 return CFN_LAST;
8538 }
8539
8540 /* Comparator of indices based on tree_node_counts. */
8541
8542 static int
8543 tree_nodes_cmp (const void *p1, const void *p2)
8544 {
8545 const unsigned *n1 = (const unsigned *)p1;
8546 const unsigned *n2 = (const unsigned *)p2;
8547
8548 return tree_node_counts[*n1] - tree_node_counts[*n2];
8549 }
8550
8551 /* Comparator of indices based on tree_code_counts. */
8552
8553 static int
8554 tree_codes_cmp (const void *p1, const void *p2)
8555 {
8556 const unsigned *n1 = (const unsigned *)p1;
8557 const unsigned *n2 = (const unsigned *)p2;
8558
8559 return tree_code_counts[*n1] - tree_code_counts[*n2];
8560 }
8561
8562 #define TREE_MEM_USAGE_SPACES 40
8563
8564 /* Print debugging information about tree nodes generated during the compile,
8565 and any language-specific information. */
8566
8567 void
8568 dump_tree_statistics (void)
8569 {
8570 if (GATHER_STATISTICS)
8571 {
8572 uint64_t total_nodes, total_bytes;
8573 fprintf (stderr, "\nKind Nodes Bytes\n");
8574 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8575 total_nodes = total_bytes = 0;
8576
8577 {
8578 auto_vec<unsigned> indices (all_kinds);
8579 for (unsigned i = 0; i < all_kinds; i++)
8580 indices.quick_push (i);
8581 indices.qsort (tree_nodes_cmp);
8582
8583 for (unsigned i = 0; i < (int) all_kinds; i++)
8584 {
8585 unsigned j = indices[i];
8586 fprintf (stderr, "%-20s %6" PRIu64 "%c %9" PRIu64 "%c\n",
8587 tree_node_kind_names[j], SIZE_AMOUNT (tree_node_counts[j]),
8588 SIZE_AMOUNT (tree_node_sizes[j]));
8589 total_nodes += tree_node_counts[j];
8590 total_bytes += tree_node_sizes[j];
8591 }
8592 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8593 fprintf (stderr, "%-20s %6" PRIu64 "%c %9" PRIu64 "%c\n", "Total",
8594 SIZE_AMOUNT (total_nodes), SIZE_AMOUNT (total_bytes));
8595 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8596 }
8597
8598 {
8599 fprintf (stderr, "Code Nodes\n");
8600 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8601
8602 auto_vec<unsigned> indices (MAX_TREE_CODES);
8603 for (unsigned i = 0; i < MAX_TREE_CODES; i++)
8604 indices.quick_push (i);
8605 indices.qsort (tree_codes_cmp);
8606
8607 for (unsigned i = 0; i < MAX_TREE_CODES; i++)
8608 {
8609 unsigned j = indices[i];
8610 fprintf (stderr, "%-32s %6" PRIu64 "%c\n",
8611 get_tree_code_name ((enum tree_code) j),
8612 SIZE_AMOUNT (tree_code_counts[j]));
8613 }
8614 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8615 fprintf (stderr, "\n");
8616 ssanames_print_statistics ();
8617 fprintf (stderr, "\n");
8618 phinodes_print_statistics ();
8619 fprintf (stderr, "\n");
8620 }
8621 }
8622 else
8623 fprintf (stderr, "(No per-node statistics)\n");
8624
8625 print_type_hash_statistics ();
8626 print_debug_expr_statistics ();
8627 print_value_expr_statistics ();
8628 lang_hooks.print_statistics ();
8629 }
8630 \f
8631 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8632
8633 /* Generate a crc32 of the low BYTES bytes of VALUE. */
8634
8635 unsigned
8636 crc32_unsigned_n (unsigned chksum, unsigned value, unsigned bytes)
8637 {
8638 /* This relies on the raw feedback's top 4 bits being zero. */
8639 #define FEEDBACK(X) ((X) * 0x04c11db7)
8640 #define SYNDROME(X) (FEEDBACK ((X) & 1) ^ FEEDBACK ((X) & 2) \
8641 ^ FEEDBACK ((X) & 4) ^ FEEDBACK ((X) & 8))
8642 static const unsigned syndromes[16] =
8643 {
8644 SYNDROME(0x0), SYNDROME(0x1), SYNDROME(0x2), SYNDROME(0x3),
8645 SYNDROME(0x4), SYNDROME(0x5), SYNDROME(0x6), SYNDROME(0x7),
8646 SYNDROME(0x8), SYNDROME(0x9), SYNDROME(0xa), SYNDROME(0xb),
8647 SYNDROME(0xc), SYNDROME(0xd), SYNDROME(0xe), SYNDROME(0xf),
8648 };
8649 #undef FEEDBACK
8650 #undef SYNDROME
8651
8652 value <<= (32 - bytes * 8);
8653 for (unsigned ix = bytes * 2; ix--; value <<= 4)
8654 {
8655 unsigned feedback = syndromes[((value ^ chksum) >> 28) & 0xf];
8656
8657 chksum = (chksum << 4) ^ feedback;
8658 }
8659
8660 return chksum;
8661 }
8662
8663 /* Generate a crc32 of a string. */
8664
8665 unsigned
8666 crc32_string (unsigned chksum, const char *string)
8667 {
8668 do
8669 chksum = crc32_byte (chksum, *string);
8670 while (*string++);
8671 return chksum;
8672 }
8673
8674 /* P is a string that will be used in a symbol. Mask out any characters
8675 that are not valid in that context. */
8676
8677 void
8678 clean_symbol_name (char *p)
8679 {
8680 for (; *p; p++)
8681 if (! (ISALNUM (*p)
8682 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
8683 || *p == '$'
8684 #endif
8685 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
8686 || *p == '.'
8687 #endif
8688 ))
8689 *p = '_';
8690 }
8691
8692 static GTY(()) unsigned anon_cnt = 0; /* Saved for PCH. */
8693
8694 /* Create a unique anonymous identifier. The identifier is still a
8695 valid assembly label. */
8696
8697 tree
8698 make_anon_name ()
8699 {
8700 const char *fmt =
8701 #if !defined (NO_DOT_IN_LABEL)
8702 "."
8703 #elif !defined (NO_DOLLAR_IN_LABEL)
8704 "$"
8705 #else
8706 "_"
8707 #endif
8708 "_anon_%d";
8709
8710 char buf[24];
8711 int len = snprintf (buf, sizeof (buf), fmt, anon_cnt++);
8712 gcc_checking_assert (len < int (sizeof (buf)));
8713
8714 tree id = get_identifier_with_length (buf, len);
8715 IDENTIFIER_ANON_P (id) = true;
8716
8717 return id;
8718 }
8719
8720 /* Generate a name for a special-purpose function.
8721 The generated name may need to be unique across the whole link.
8722 Changes to this function may also require corresponding changes to
8723 xstrdup_mask_random.
8724 TYPE is some string to identify the purpose of this function to the
8725 linker or collect2; it must start with an uppercase letter,
8726 one of:
8727 I - for constructors
8728 D - for destructors
8729 N - for C++ anonymous namespaces
8730 F - for DWARF unwind frame information. */
8731
8732 tree
8733 get_file_function_name (const char *type)
8734 {
8735 char *buf;
8736 const char *p;
8737 char *q;
8738
8739 /* If we already have a name we know to be unique, just use that. */
8740 if (first_global_object_name)
8741 p = q = ASTRDUP (first_global_object_name);
8742 /* If the target is handling the constructors/destructors, they
8743 will be local to this file and the name is only necessary for
8744 debugging purposes.
8745 We also assign sub_I and sub_D sufixes to constructors called from
8746 the global static constructors. These are always local. */
8747 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
8748 || (startswith (type, "sub_")
8749 && (type[4] == 'I' || type[4] == 'D')))
8750 {
8751 const char *file = main_input_filename;
8752 if (! file)
8753 file = LOCATION_FILE (input_location);
8754 /* Just use the file's basename, because the full pathname
8755 might be quite long. */
8756 p = q = ASTRDUP (lbasename (file));
8757 }
8758 else
8759 {
8760 /* Otherwise, the name must be unique across the entire link.
8761 We don't have anything that we know to be unique to this translation
8762 unit, so use what we do have and throw in some randomness. */
8763 unsigned len;
8764 const char *name = weak_global_object_name;
8765 const char *file = main_input_filename;
8766
8767 if (! name)
8768 name = "";
8769 if (! file)
8770 file = LOCATION_FILE (input_location);
8771
8772 len = strlen (file);
8773 q = (char *) alloca (9 + 19 + len + 1);
8774 memcpy (q, file, len + 1);
8775
8776 snprintf (q + len, 9 + 19 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
8777 crc32_string (0, name), get_random_seed (false));
8778
8779 p = q;
8780 }
8781
8782 clean_symbol_name (q);
8783 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
8784 + strlen (type));
8785
8786 /* Set up the name of the file-level functions we may need.
8787 Use a global object (which is already required to be unique over
8788 the program) rather than the file name (which imposes extra
8789 constraints). */
8790 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
8791
8792 return get_identifier (buf);
8793 }
8794 \f
8795 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
8796
8797 /* Complain that the tree code of NODE does not match the expected 0
8798 terminated list of trailing codes. The trailing code list can be
8799 empty, for a more vague error message. FILE, LINE, and FUNCTION
8800 are of the caller. */
8801
8802 void
8803 tree_check_failed (const_tree node, const char *file,
8804 int line, const char *function, ...)
8805 {
8806 va_list args;
8807 const char *buffer;
8808 unsigned length = 0;
8809 enum tree_code code;
8810
8811 va_start (args, function);
8812 while ((code = (enum tree_code) va_arg (args, int)))
8813 length += 4 + strlen (get_tree_code_name (code));
8814 va_end (args);
8815 if (length)
8816 {
8817 char *tmp;
8818 va_start (args, function);
8819 length += strlen ("expected ");
8820 buffer = tmp = (char *) alloca (length);
8821 length = 0;
8822 while ((code = (enum tree_code) va_arg (args, int)))
8823 {
8824 const char *prefix = length ? " or " : "expected ";
8825
8826 strcpy (tmp + length, prefix);
8827 length += strlen (prefix);
8828 strcpy (tmp + length, get_tree_code_name (code));
8829 length += strlen (get_tree_code_name (code));
8830 }
8831 va_end (args);
8832 }
8833 else
8834 buffer = "unexpected node";
8835
8836 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8837 buffer, get_tree_code_name (TREE_CODE (node)),
8838 function, trim_filename (file), line);
8839 }
8840
8841 /* Complain that the tree code of NODE does match the expected 0
8842 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
8843 the caller. */
8844
8845 void
8846 tree_not_check_failed (const_tree node, const char *file,
8847 int line, const char *function, ...)
8848 {
8849 va_list args;
8850 char *buffer;
8851 unsigned length = 0;
8852 enum tree_code code;
8853
8854 va_start (args, function);
8855 while ((code = (enum tree_code) va_arg (args, int)))
8856 length += 4 + strlen (get_tree_code_name (code));
8857 va_end (args);
8858 va_start (args, function);
8859 buffer = (char *) alloca (length);
8860 length = 0;
8861 while ((code = (enum tree_code) va_arg (args, int)))
8862 {
8863 if (length)
8864 {
8865 strcpy (buffer + length, " or ");
8866 length += 4;
8867 }
8868 strcpy (buffer + length, get_tree_code_name (code));
8869 length += strlen (get_tree_code_name (code));
8870 }
8871 va_end (args);
8872
8873 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
8874 buffer, get_tree_code_name (TREE_CODE (node)),
8875 function, trim_filename (file), line);
8876 }
8877
8878 /* Similar to tree_check_failed, except that we check for a class of tree
8879 code, given in CL. */
8880
8881 void
8882 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
8883 const char *file, int line, const char *function)
8884 {
8885 internal_error
8886 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
8887 TREE_CODE_CLASS_STRING (cl),
8888 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
8889 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
8890 }
8891
8892 /* Similar to tree_check_failed, except that instead of specifying a
8893 dozen codes, use the knowledge that they're all sequential. */
8894
8895 void
8896 tree_range_check_failed (const_tree node, const char *file, int line,
8897 const char *function, enum tree_code c1,
8898 enum tree_code c2)
8899 {
8900 char *buffer;
8901 unsigned length = 0;
8902 unsigned int c;
8903
8904 for (c = c1; c <= c2; ++c)
8905 length += 4 + strlen (get_tree_code_name ((enum tree_code) c));
8906
8907 length += strlen ("expected ");
8908 buffer = (char *) alloca (length);
8909 length = 0;
8910
8911 for (c = c1; c <= c2; ++c)
8912 {
8913 const char *prefix = length ? " or " : "expected ";
8914
8915 strcpy (buffer + length, prefix);
8916 length += strlen (prefix);
8917 strcpy (buffer + length, get_tree_code_name ((enum tree_code) c));
8918 length += strlen (get_tree_code_name ((enum tree_code) c));
8919 }
8920
8921 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8922 buffer, get_tree_code_name (TREE_CODE (node)),
8923 function, trim_filename (file), line);
8924 }
8925
8926
8927 /* Similar to tree_check_failed, except that we check that a tree does
8928 not have the specified code, given in CL. */
8929
8930 void
8931 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
8932 const char *file, int line, const char *function)
8933 {
8934 internal_error
8935 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
8936 TREE_CODE_CLASS_STRING (cl),
8937 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
8938 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
8939 }
8940
8941
8942 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
8943
8944 void
8945 omp_clause_check_failed (const_tree node, const char *file, int line,
8946 const char *function, enum omp_clause_code code)
8947 {
8948 internal_error ("tree check: expected %<omp_clause %s%>, have %qs "
8949 "in %s, at %s:%d",
8950 omp_clause_code_name[code],
8951 get_tree_code_name (TREE_CODE (node)),
8952 function, trim_filename (file), line);
8953 }
8954
8955
8956 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
8957
8958 void
8959 omp_clause_range_check_failed (const_tree node, const char *file, int line,
8960 const char *function, enum omp_clause_code c1,
8961 enum omp_clause_code c2)
8962 {
8963 char *buffer;
8964 unsigned length = 0;
8965 unsigned int c;
8966
8967 for (c = c1; c <= c2; ++c)
8968 length += 4 + strlen (omp_clause_code_name[c]);
8969
8970 length += strlen ("expected ");
8971 buffer = (char *) alloca (length);
8972 length = 0;
8973
8974 for (c = c1; c <= c2; ++c)
8975 {
8976 const char *prefix = length ? " or " : "expected ";
8977
8978 strcpy (buffer + length, prefix);
8979 length += strlen (prefix);
8980 strcpy (buffer + length, omp_clause_code_name[c]);
8981 length += strlen (omp_clause_code_name[c]);
8982 }
8983
8984 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8985 buffer, omp_clause_code_name[TREE_CODE (node)],
8986 function, trim_filename (file), line);
8987 }
8988
8989
8990 #undef DEFTREESTRUCT
8991 #define DEFTREESTRUCT(VAL, NAME) NAME,
8992
8993 static const char *ts_enum_names[] = {
8994 #include "treestruct.def"
8995 };
8996 #undef DEFTREESTRUCT
8997
8998 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
8999
9000 /* Similar to tree_class_check_failed, except that we check for
9001 whether CODE contains the tree structure identified by EN. */
9002
9003 void
9004 tree_contains_struct_check_failed (const_tree node,
9005 const enum tree_node_structure_enum en,
9006 const char *file, int line,
9007 const char *function)
9008 {
9009 internal_error
9010 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9011 TS_ENUM_NAME (en),
9012 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9013 }
9014
9015
9016 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9017 (dynamically sized) vector. */
9018
9019 void
9020 tree_int_cst_elt_check_failed (int idx, int len, const char *file, int line,
9021 const char *function)
9022 {
9023 internal_error
9024 ("tree check: accessed elt %d of %<tree_int_cst%> with %d elts in %s, "
9025 "at %s:%d",
9026 idx + 1, len, function, trim_filename (file), line);
9027 }
9028
9029 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9030 (dynamically sized) vector. */
9031
9032 void
9033 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9034 const char *function)
9035 {
9036 internal_error
9037 ("tree check: accessed elt %d of %<tree_vec%> with %d elts in %s, at %s:%d",
9038 idx + 1, len, function, trim_filename (file), line);
9039 }
9040
9041 /* Similar to above, except that the check is for the bounds of the operand
9042 vector of an expression node EXP. */
9043
9044 void
9045 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9046 int line, const char *function)
9047 {
9048 enum tree_code code = TREE_CODE (exp);
9049 internal_error
9050 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9051 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9052 function, trim_filename (file), line);
9053 }
9054
9055 /* Similar to above, except that the check is for the number of
9056 operands of an OMP_CLAUSE node. */
9057
9058 void
9059 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9060 int line, const char *function)
9061 {
9062 internal_error
9063 ("tree check: accessed operand %d of %<omp_clause %s%> with %d operands "
9064 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9065 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9066 trim_filename (file), line);
9067 }
9068 #endif /* ENABLE_TREE_CHECKING */
9069 \f
9070 /* Create a new vector type node holding NUNITS units of type INNERTYPE,
9071 and mapped to the machine mode MODE. Initialize its fields and build
9072 the information necessary for debugging output. */
9073
9074 static tree
9075 make_vector_type (tree innertype, poly_int64 nunits, machine_mode mode)
9076 {
9077 tree t;
9078 tree mv_innertype = TYPE_MAIN_VARIANT (innertype);
9079
9080 t = make_node (VECTOR_TYPE);
9081 TREE_TYPE (t) = mv_innertype;
9082 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9083 SET_TYPE_MODE (t, mode);
9084
9085 if (TYPE_STRUCTURAL_EQUALITY_P (mv_innertype) || in_lto_p)
9086 SET_TYPE_STRUCTURAL_EQUALITY (t);
9087 else if ((TYPE_CANONICAL (mv_innertype) != innertype
9088 || mode != VOIDmode)
9089 && !VECTOR_BOOLEAN_TYPE_P (t))
9090 TYPE_CANONICAL (t)
9091 = make_vector_type (TYPE_CANONICAL (mv_innertype), nunits, VOIDmode);
9092
9093 layout_type (t);
9094
9095 hashval_t hash = type_hash_canon_hash (t);
9096 t = type_hash_canon (hash, t);
9097
9098 /* We have built a main variant, based on the main variant of the
9099 inner type. Use it to build the variant we return. */
9100 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9101 && TREE_TYPE (t) != innertype)
9102 return build_type_attribute_qual_variant (t,
9103 TYPE_ATTRIBUTES (innertype),
9104 TYPE_QUALS (innertype));
9105
9106 return t;
9107 }
9108
9109 static tree
9110 make_or_reuse_type (unsigned size, int unsignedp)
9111 {
9112 int i;
9113
9114 if (size == INT_TYPE_SIZE)
9115 return unsignedp ? unsigned_type_node : integer_type_node;
9116 if (size == CHAR_TYPE_SIZE)
9117 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9118 if (size == SHORT_TYPE_SIZE)
9119 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9120 if (size == LONG_TYPE_SIZE)
9121 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9122 if (size == LONG_LONG_TYPE_SIZE)
9123 return (unsignedp ? long_long_unsigned_type_node
9124 : long_long_integer_type_node);
9125
9126 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9127 if (size == int_n_data[i].bitsize
9128 && int_n_enabled_p[i])
9129 return (unsignedp ? int_n_trees[i].unsigned_type
9130 : int_n_trees[i].signed_type);
9131
9132 if (unsignedp)
9133 return make_unsigned_type (size);
9134 else
9135 return make_signed_type (size);
9136 }
9137
9138 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9139
9140 static tree
9141 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9142 {
9143 if (satp)
9144 {
9145 if (size == SHORT_FRACT_TYPE_SIZE)
9146 return unsignedp ? sat_unsigned_short_fract_type_node
9147 : sat_short_fract_type_node;
9148 if (size == FRACT_TYPE_SIZE)
9149 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9150 if (size == LONG_FRACT_TYPE_SIZE)
9151 return unsignedp ? sat_unsigned_long_fract_type_node
9152 : sat_long_fract_type_node;
9153 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9154 return unsignedp ? sat_unsigned_long_long_fract_type_node
9155 : sat_long_long_fract_type_node;
9156 }
9157 else
9158 {
9159 if (size == SHORT_FRACT_TYPE_SIZE)
9160 return unsignedp ? unsigned_short_fract_type_node
9161 : short_fract_type_node;
9162 if (size == FRACT_TYPE_SIZE)
9163 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9164 if (size == LONG_FRACT_TYPE_SIZE)
9165 return unsignedp ? unsigned_long_fract_type_node
9166 : long_fract_type_node;
9167 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9168 return unsignedp ? unsigned_long_long_fract_type_node
9169 : long_long_fract_type_node;
9170 }
9171
9172 return make_fract_type (size, unsignedp, satp);
9173 }
9174
9175 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9176
9177 static tree
9178 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9179 {
9180 if (satp)
9181 {
9182 if (size == SHORT_ACCUM_TYPE_SIZE)
9183 return unsignedp ? sat_unsigned_short_accum_type_node
9184 : sat_short_accum_type_node;
9185 if (size == ACCUM_TYPE_SIZE)
9186 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9187 if (size == LONG_ACCUM_TYPE_SIZE)
9188 return unsignedp ? sat_unsigned_long_accum_type_node
9189 : sat_long_accum_type_node;
9190 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9191 return unsignedp ? sat_unsigned_long_long_accum_type_node
9192 : sat_long_long_accum_type_node;
9193 }
9194 else
9195 {
9196 if (size == SHORT_ACCUM_TYPE_SIZE)
9197 return unsignedp ? unsigned_short_accum_type_node
9198 : short_accum_type_node;
9199 if (size == ACCUM_TYPE_SIZE)
9200 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9201 if (size == LONG_ACCUM_TYPE_SIZE)
9202 return unsignedp ? unsigned_long_accum_type_node
9203 : long_accum_type_node;
9204 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9205 return unsignedp ? unsigned_long_long_accum_type_node
9206 : long_long_accum_type_node;
9207 }
9208
9209 return make_accum_type (size, unsignedp, satp);
9210 }
9211
9212
9213 /* Create an atomic variant node for TYPE. This routine is called
9214 during initialization of data types to create the 5 basic atomic
9215 types. The generic build_variant_type function requires these to
9216 already be set up in order to function properly, so cannot be
9217 called from there. If ALIGN is non-zero, then ensure alignment is
9218 overridden to this value. */
9219
9220 static tree
9221 build_atomic_base (tree type, unsigned int align)
9222 {
9223 tree t;
9224
9225 /* Make sure its not already registered. */
9226 if ((t = get_qualified_type (type, TYPE_QUAL_ATOMIC)))
9227 return t;
9228
9229 t = build_variant_type_copy (type);
9230 set_type_quals (t, TYPE_QUAL_ATOMIC);
9231
9232 if (align)
9233 SET_TYPE_ALIGN (t, align);
9234
9235 return t;
9236 }
9237
9238 /* Information about the _FloatN and _FloatNx types. This must be in
9239 the same order as the corresponding TI_* enum values. */
9240 const floatn_type_info floatn_nx_types[NUM_FLOATN_NX_TYPES] =
9241 {
9242 { 16, false },
9243 { 32, false },
9244 { 64, false },
9245 { 128, false },
9246 { 32, true },
9247 { 64, true },
9248 { 128, true },
9249 };
9250
9251
9252 /* Create nodes for all integer types (and error_mark_node) using the sizes
9253 of C datatypes. SIGNED_CHAR specifies whether char is signed. */
9254
9255 void
9256 build_common_tree_nodes (bool signed_char)
9257 {
9258 int i;
9259
9260 error_mark_node = make_node (ERROR_MARK);
9261 TREE_TYPE (error_mark_node) = error_mark_node;
9262
9263 initialize_sizetypes ();
9264
9265 /* Define both `signed char' and `unsigned char'. */
9266 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9267 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9268 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9269 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9270
9271 /* Define `char', which is like either `signed char' or `unsigned char'
9272 but not the same as either. */
9273 char_type_node
9274 = (signed_char
9275 ? make_signed_type (CHAR_TYPE_SIZE)
9276 : make_unsigned_type (CHAR_TYPE_SIZE));
9277 TYPE_STRING_FLAG (char_type_node) = 1;
9278
9279 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9280 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9281 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9282 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9283 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9284 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9285 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9286 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9287
9288 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9289 {
9290 int_n_trees[i].signed_type = make_signed_type (int_n_data[i].bitsize);
9291 int_n_trees[i].unsigned_type = make_unsigned_type (int_n_data[i].bitsize);
9292
9293 if (int_n_enabled_p[i])
9294 {
9295 integer_types[itk_intN_0 + i * 2] = int_n_trees[i].signed_type;
9296 integer_types[itk_unsigned_intN_0 + i * 2] = int_n_trees[i].unsigned_type;
9297 }
9298 }
9299
9300 /* Define a boolean type. This type only represents boolean values but
9301 may be larger than char depending on the value of BOOL_TYPE_SIZE. */
9302 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9303 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9304 TYPE_PRECISION (boolean_type_node) = 1;
9305 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9306
9307 /* Define what type to use for size_t. */
9308 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9309 size_type_node = unsigned_type_node;
9310 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9311 size_type_node = long_unsigned_type_node;
9312 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9313 size_type_node = long_long_unsigned_type_node;
9314 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9315 size_type_node = short_unsigned_type_node;
9316 else
9317 {
9318 int i;
9319
9320 size_type_node = NULL_TREE;
9321 for (i = 0; i < NUM_INT_N_ENTS; i++)
9322 if (int_n_enabled_p[i])
9323 {
9324 char name[50], altname[50];
9325 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
9326 sprintf (altname, "__int%d__ unsigned", int_n_data[i].bitsize);
9327
9328 if (strcmp (name, SIZE_TYPE) == 0
9329 || strcmp (altname, SIZE_TYPE) == 0)
9330 {
9331 size_type_node = int_n_trees[i].unsigned_type;
9332 }
9333 }
9334 if (size_type_node == NULL_TREE)
9335 gcc_unreachable ();
9336 }
9337
9338 /* Define what type to use for ptrdiff_t. */
9339 if (strcmp (PTRDIFF_TYPE, "int") == 0)
9340 ptrdiff_type_node = integer_type_node;
9341 else if (strcmp (PTRDIFF_TYPE, "long int") == 0)
9342 ptrdiff_type_node = long_integer_type_node;
9343 else if (strcmp (PTRDIFF_TYPE, "long long int") == 0)
9344 ptrdiff_type_node = long_long_integer_type_node;
9345 else if (strcmp (PTRDIFF_TYPE, "short int") == 0)
9346 ptrdiff_type_node = short_integer_type_node;
9347 else
9348 {
9349 ptrdiff_type_node = NULL_TREE;
9350 for (int i = 0; i < NUM_INT_N_ENTS; i++)
9351 if (int_n_enabled_p[i])
9352 {
9353 char name[50], altname[50];
9354 sprintf (name, "__int%d", int_n_data[i].bitsize);
9355 sprintf (altname, "__int%d__", int_n_data[i].bitsize);
9356
9357 if (strcmp (name, PTRDIFF_TYPE) == 0
9358 || strcmp (altname, PTRDIFF_TYPE) == 0)
9359 ptrdiff_type_node = int_n_trees[i].signed_type;
9360 }
9361 if (ptrdiff_type_node == NULL_TREE)
9362 gcc_unreachable ();
9363 }
9364
9365 /* Fill in the rest of the sized types. Reuse existing type nodes
9366 when possible. */
9367 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9368 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9369 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9370 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9371 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9372
9373 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9374 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9375 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9376 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9377 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9378
9379 /* Don't call build_qualified type for atomics. That routine does
9380 special processing for atomics, and until they are initialized
9381 it's better not to make that call.
9382
9383 Check to see if there is a target override for atomic types. */
9384
9385 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
9386 targetm.atomic_align_for_mode (QImode));
9387 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
9388 targetm.atomic_align_for_mode (HImode));
9389 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
9390 targetm.atomic_align_for_mode (SImode));
9391 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
9392 targetm.atomic_align_for_mode (DImode));
9393 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
9394 targetm.atomic_align_for_mode (TImode));
9395
9396 access_public_node = get_identifier ("public");
9397 access_protected_node = get_identifier ("protected");
9398 access_private_node = get_identifier ("private");
9399
9400 /* Define these next since types below may used them. */
9401 integer_zero_node = build_int_cst (integer_type_node, 0);
9402 integer_one_node = build_int_cst (integer_type_node, 1);
9403 integer_three_node = build_int_cst (integer_type_node, 3);
9404 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9405
9406 size_zero_node = size_int (0);
9407 size_one_node = size_int (1);
9408 bitsize_zero_node = bitsize_int (0);
9409 bitsize_one_node = bitsize_int (1);
9410 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9411
9412 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9413 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9414
9415 void_type_node = make_node (VOID_TYPE);
9416 layout_type (void_type_node);
9417
9418 /* We are not going to have real types in C with less than byte alignment,
9419 so we might as well not have any types that claim to have it. */
9420 SET_TYPE_ALIGN (void_type_node, BITS_PER_UNIT);
9421 TYPE_USER_ALIGN (void_type_node) = 0;
9422
9423 void_node = make_node (VOID_CST);
9424 TREE_TYPE (void_node) = void_type_node;
9425
9426 void_list_node = build_tree_list (NULL_TREE, void_type_node);
9427
9428 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9429 layout_type (TREE_TYPE (null_pointer_node));
9430
9431 ptr_type_node = build_pointer_type (void_type_node);
9432 const_ptr_type_node
9433 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9434 for (unsigned i = 0; i < ARRAY_SIZE (builtin_structptr_types); ++i)
9435 builtin_structptr_types[i].node = builtin_structptr_types[i].base;
9436
9437 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
9438
9439 float_type_node = make_node (REAL_TYPE);
9440 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9441 layout_type (float_type_node);
9442
9443 double_type_node = make_node (REAL_TYPE);
9444 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9445 layout_type (double_type_node);
9446
9447 long_double_type_node = make_node (REAL_TYPE);
9448 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9449 layout_type (long_double_type_node);
9450
9451 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
9452 {
9453 int n = floatn_nx_types[i].n;
9454 bool extended = floatn_nx_types[i].extended;
9455 scalar_float_mode mode;
9456 if (!targetm.floatn_mode (n, extended).exists (&mode))
9457 continue;
9458 int precision = GET_MODE_PRECISION (mode);
9459 /* Work around the rs6000 KFmode having precision 113 not
9460 128. */
9461 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
9462 gcc_assert (fmt->b == 2 && fmt->emin + fmt->emax == 3);
9463 int min_precision = fmt->p + ceil_log2 (fmt->emax - fmt->emin);
9464 if (!extended)
9465 gcc_assert (min_precision == n);
9466 if (precision < min_precision)
9467 precision = min_precision;
9468 FLOATN_NX_TYPE_NODE (i) = make_node (REAL_TYPE);
9469 TYPE_PRECISION (FLOATN_NX_TYPE_NODE (i)) = precision;
9470 layout_type (FLOATN_NX_TYPE_NODE (i));
9471 SET_TYPE_MODE (FLOATN_NX_TYPE_NODE (i), mode);
9472 }
9473 float128t_type_node = float128_type_node;
9474 #ifdef HAVE_BFmode
9475 if (REAL_MODE_FORMAT (BFmode) == &arm_bfloat_half_format
9476 && targetm.scalar_mode_supported_p (BFmode)
9477 && targetm.libgcc_floating_mode_supported_p (BFmode))
9478 {
9479 bfloat16_type_node = make_node (REAL_TYPE);
9480 TYPE_PRECISION (bfloat16_type_node) = GET_MODE_PRECISION (BFmode);
9481 layout_type (bfloat16_type_node);
9482 SET_TYPE_MODE (bfloat16_type_node, BFmode);
9483 }
9484 #endif
9485
9486 float_ptr_type_node = build_pointer_type (float_type_node);
9487 double_ptr_type_node = build_pointer_type (double_type_node);
9488 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9489 integer_ptr_type_node = build_pointer_type (integer_type_node);
9490
9491 /* Fixed size integer types. */
9492 uint16_type_node = make_or_reuse_type (16, 1);
9493 uint32_type_node = make_or_reuse_type (32, 1);
9494 uint64_type_node = make_or_reuse_type (64, 1);
9495 if (targetm.scalar_mode_supported_p (TImode))
9496 uint128_type_node = make_or_reuse_type (128, 1);
9497
9498 /* Decimal float types. */
9499 if (targetm.decimal_float_supported_p ())
9500 {
9501 dfloat32_type_node = make_node (REAL_TYPE);
9502 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9503 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9504 layout_type (dfloat32_type_node);
9505
9506 dfloat64_type_node = make_node (REAL_TYPE);
9507 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9508 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9509 layout_type (dfloat64_type_node);
9510
9511 dfloat128_type_node = make_node (REAL_TYPE);
9512 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9513 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9514 layout_type (dfloat128_type_node);
9515 }
9516
9517 complex_integer_type_node = build_complex_type (integer_type_node, true);
9518 complex_float_type_node = build_complex_type (float_type_node, true);
9519 complex_double_type_node = build_complex_type (double_type_node, true);
9520 complex_long_double_type_node = build_complex_type (long_double_type_node,
9521 true);
9522
9523 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
9524 {
9525 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
9526 COMPLEX_FLOATN_NX_TYPE_NODE (i)
9527 = build_complex_type (FLOATN_NX_TYPE_NODE (i));
9528 }
9529
9530 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9531 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9532 sat_ ## KIND ## _type_node = \
9533 make_sat_signed_ ## KIND ## _type (SIZE); \
9534 sat_unsigned_ ## KIND ## _type_node = \
9535 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9536 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9537 unsigned_ ## KIND ## _type_node = \
9538 make_unsigned_ ## KIND ## _type (SIZE);
9539
9540 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9541 sat_ ## WIDTH ## KIND ## _type_node = \
9542 make_sat_signed_ ## KIND ## _type (SIZE); \
9543 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9544 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9545 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9546 unsigned_ ## WIDTH ## KIND ## _type_node = \
9547 make_unsigned_ ## KIND ## _type (SIZE);
9548
9549 /* Make fixed-point type nodes based on four different widths. */
9550 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9551 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9552 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9553 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9554 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9555
9556 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9557 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9558 NAME ## _type_node = \
9559 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9560 u ## NAME ## _type_node = \
9561 make_or_reuse_unsigned_ ## KIND ## _type \
9562 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9563 sat_ ## NAME ## _type_node = \
9564 make_or_reuse_sat_signed_ ## KIND ## _type \
9565 (GET_MODE_BITSIZE (MODE ## mode)); \
9566 sat_u ## NAME ## _type_node = \
9567 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9568 (GET_MODE_BITSIZE (U ## MODE ## mode));
9569
9570 /* Fixed-point type and mode nodes. */
9571 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9572 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9573 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9574 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9575 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9576 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9577 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9578 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9579 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9580 MAKE_FIXED_MODE_NODE (accum, da, DA)
9581 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9582
9583 {
9584 tree t = targetm.build_builtin_va_list ();
9585
9586 /* Many back-ends define record types without setting TYPE_NAME.
9587 If we copied the record type here, we'd keep the original
9588 record type without a name. This breaks name mangling. So,
9589 don't copy record types and let c_common_nodes_and_builtins()
9590 declare the type to be __builtin_va_list. */
9591 if (TREE_CODE (t) != RECORD_TYPE)
9592 t = build_variant_type_copy (t);
9593
9594 va_list_type_node = t;
9595 }
9596
9597 /* SCEV analyzer global shared trees. */
9598 chrec_dont_know = make_node (SCEV_NOT_KNOWN);
9599 TREE_TYPE (chrec_dont_know) = void_type_node;
9600 chrec_known = make_node (SCEV_KNOWN);
9601 TREE_TYPE (chrec_known) = void_type_node;
9602 }
9603
9604 /* Modify DECL for given flags.
9605 TM_PURE attribute is set only on types, so the function will modify
9606 DECL's type when ECF_TM_PURE is used. */
9607
9608 void
9609 set_call_expr_flags (tree decl, int flags)
9610 {
9611 if (flags & ECF_NOTHROW)
9612 TREE_NOTHROW (decl) = 1;
9613 if (flags & ECF_CONST)
9614 TREE_READONLY (decl) = 1;
9615 if (flags & ECF_PURE)
9616 DECL_PURE_P (decl) = 1;
9617 if (flags & ECF_LOOPING_CONST_OR_PURE)
9618 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9619 if (flags & ECF_NOVOPS)
9620 DECL_IS_NOVOPS (decl) = 1;
9621 if (flags & ECF_NORETURN)
9622 TREE_THIS_VOLATILE (decl) = 1;
9623 if (flags & ECF_MALLOC)
9624 DECL_IS_MALLOC (decl) = 1;
9625 if (flags & ECF_RETURNS_TWICE)
9626 DECL_IS_RETURNS_TWICE (decl) = 1;
9627 if (flags & ECF_LEAF)
9628 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9629 NULL, DECL_ATTRIBUTES (decl));
9630 if (flags & ECF_COLD)
9631 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("cold"),
9632 NULL, DECL_ATTRIBUTES (decl));
9633 if (flags & ECF_RET1)
9634 DECL_ATTRIBUTES (decl)
9635 = tree_cons (get_identifier ("fn spec"),
9636 build_tree_list (NULL_TREE, build_string (2, "1 ")),
9637 DECL_ATTRIBUTES (decl));
9638 if ((flags & ECF_TM_PURE) && flag_tm)
9639 apply_tm_attr (decl, get_identifier ("transaction_pure"));
9640 /* Looping const or pure is implied by noreturn.
9641 There is currently no way to declare looping const or looping pure alone. */
9642 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9643 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9644 }
9645
9646
9647 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9648
9649 static void
9650 local_define_builtin (const char *name, tree type, enum built_in_function code,
9651 const char *library_name, int ecf_flags)
9652 {
9653 tree decl;
9654
9655 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9656 library_name, NULL_TREE);
9657 set_call_expr_flags (decl, ecf_flags);
9658
9659 set_builtin_decl (code, decl, true);
9660 }
9661
9662 /* Call this function after instantiating all builtins that the language
9663 front end cares about. This will build the rest of the builtins
9664 and internal functions that are relied upon by the tree optimizers and
9665 the middle-end. */
9666
9667 void
9668 build_common_builtin_nodes (void)
9669 {
9670 tree tmp, ftype;
9671 int ecf_flags;
9672
9673 if (!builtin_decl_explicit_p (BUILT_IN_CLEAR_PADDING))
9674 {
9675 ftype = build_function_type_list (void_type_node,
9676 ptr_type_node,
9677 ptr_type_node,
9678 integer_type_node,
9679 NULL_TREE);
9680 local_define_builtin ("__builtin_clear_padding", ftype,
9681 BUILT_IN_CLEAR_PADDING,
9682 "__builtin_clear_padding",
9683 ECF_LEAF | ECF_NOTHROW);
9684 }
9685
9686 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE)
9687 || !builtin_decl_explicit_p (BUILT_IN_TRAP)
9688 || !builtin_decl_explicit_p (BUILT_IN_ABORT))
9689 {
9690 ftype = build_function_type (void_type_node, void_list_node);
9691 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9692 local_define_builtin ("__builtin_unreachable", ftype,
9693 BUILT_IN_UNREACHABLE,
9694 "__builtin_unreachable",
9695 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9696 | ECF_CONST | ECF_COLD);
9697 if (!builtin_decl_explicit_p (BUILT_IN_ABORT))
9698 local_define_builtin ("__builtin_abort", ftype, BUILT_IN_ABORT,
9699 "abort",
9700 ECF_LEAF | ECF_NORETURN | ECF_CONST | ECF_COLD);
9701 if (!builtin_decl_explicit_p (BUILT_IN_TRAP))
9702 local_define_builtin ("__builtin_trap", ftype, BUILT_IN_TRAP,
9703 "__builtin_trap",
9704 ECF_NORETURN | ECF_NOTHROW | ECF_LEAF | ECF_COLD);
9705 }
9706
9707 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9708 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9709 {
9710 ftype = build_function_type_list (ptr_type_node,
9711 ptr_type_node, const_ptr_type_node,
9712 size_type_node, NULL_TREE);
9713
9714 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
9715 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9716 "memcpy", ECF_NOTHROW | ECF_LEAF);
9717 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9718 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9719 "memmove", ECF_NOTHROW | ECF_LEAF);
9720 }
9721
9722 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
9723 {
9724 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9725 const_ptr_type_node, size_type_node,
9726 NULL_TREE);
9727 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9728 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9729 }
9730
9731 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
9732 {
9733 ftype = build_function_type_list (ptr_type_node,
9734 ptr_type_node, integer_type_node,
9735 size_type_node, NULL_TREE);
9736 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9737 "memset", ECF_NOTHROW | ECF_LEAF);
9738 }
9739
9740 /* If we're checking the stack, `alloca' can throw. */
9741 const int alloca_flags
9742 = ECF_MALLOC | ECF_LEAF | (flag_stack_check ? 0 : ECF_NOTHROW);
9743
9744 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
9745 {
9746 ftype = build_function_type_list (ptr_type_node,
9747 size_type_node, NULL_TREE);
9748 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9749 "alloca", alloca_flags);
9750 }
9751
9752 ftype = build_function_type_list (ptr_type_node, size_type_node,
9753 size_type_node, NULL_TREE);
9754 local_define_builtin ("__builtin_alloca_with_align", ftype,
9755 BUILT_IN_ALLOCA_WITH_ALIGN,
9756 "__builtin_alloca_with_align",
9757 alloca_flags);
9758
9759 ftype = build_function_type_list (ptr_type_node, size_type_node,
9760 size_type_node, size_type_node, NULL_TREE);
9761 local_define_builtin ("__builtin_alloca_with_align_and_max", ftype,
9762 BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX,
9763 "__builtin_alloca_with_align_and_max",
9764 alloca_flags);
9765
9766 ftype = build_function_type_list (void_type_node,
9767 ptr_type_node, ptr_type_node,
9768 ptr_type_node, NULL_TREE);
9769 local_define_builtin ("__builtin_init_trampoline", ftype,
9770 BUILT_IN_INIT_TRAMPOLINE,
9771 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9772 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
9773 BUILT_IN_INIT_HEAP_TRAMPOLINE,
9774 "__builtin_init_heap_trampoline",
9775 ECF_NOTHROW | ECF_LEAF);
9776 local_define_builtin ("__builtin_init_descriptor", ftype,
9777 BUILT_IN_INIT_DESCRIPTOR,
9778 "__builtin_init_descriptor", ECF_NOTHROW | ECF_LEAF);
9779
9780 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9781 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9782 BUILT_IN_ADJUST_TRAMPOLINE,
9783 "__builtin_adjust_trampoline",
9784 ECF_CONST | ECF_NOTHROW);
9785 local_define_builtin ("__builtin_adjust_descriptor", ftype,
9786 BUILT_IN_ADJUST_DESCRIPTOR,
9787 "__builtin_adjust_descriptor",
9788 ECF_CONST | ECF_NOTHROW);
9789
9790 ftype = build_function_type_list (void_type_node,
9791 ptr_type_node, ptr_type_node, NULL_TREE);
9792 if (!builtin_decl_explicit_p (BUILT_IN_CLEAR_CACHE))
9793 local_define_builtin ("__builtin___clear_cache", ftype,
9794 BUILT_IN_CLEAR_CACHE,
9795 "__clear_cache",
9796 ECF_NOTHROW);
9797
9798 local_define_builtin ("__builtin_nonlocal_goto", ftype,
9799 BUILT_IN_NONLOCAL_GOTO,
9800 "__builtin_nonlocal_goto",
9801 ECF_NORETURN | ECF_NOTHROW);
9802
9803 ftype = build_function_type_list (void_type_node,
9804 ptr_type_node, ptr_type_node, NULL_TREE);
9805 local_define_builtin ("__builtin_setjmp_setup", ftype,
9806 BUILT_IN_SETJMP_SETUP,
9807 "__builtin_setjmp_setup", ECF_NOTHROW);
9808
9809 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9810 local_define_builtin ("__builtin_setjmp_receiver", ftype,
9811 BUILT_IN_SETJMP_RECEIVER,
9812 "__builtin_setjmp_receiver", ECF_NOTHROW | ECF_LEAF);
9813
9814 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9815 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
9816 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9817
9818 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9819 local_define_builtin ("__builtin_stack_restore", ftype,
9820 BUILT_IN_STACK_RESTORE,
9821 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9822
9823 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9824 const_ptr_type_node, size_type_node,
9825 NULL_TREE);
9826 local_define_builtin ("__builtin_memcmp_eq", ftype, BUILT_IN_MEMCMP_EQ,
9827 "__builtin_memcmp_eq",
9828 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9829
9830 local_define_builtin ("__builtin_strncmp_eq", ftype, BUILT_IN_STRNCMP_EQ,
9831 "__builtin_strncmp_eq",
9832 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9833
9834 local_define_builtin ("__builtin_strcmp_eq", ftype, BUILT_IN_STRCMP_EQ,
9835 "__builtin_strcmp_eq",
9836 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9837
9838 /* If there's a possibility that we might use the ARM EABI, build the
9839 alternate __cxa_end_cleanup node used to resume from C++. */
9840 if (targetm.arm_eabi_unwinder)
9841 {
9842 ftype = build_function_type_list (void_type_node, NULL_TREE);
9843 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
9844 BUILT_IN_CXA_END_CLEANUP,
9845 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
9846 }
9847
9848 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9849 local_define_builtin ("__builtin_unwind_resume", ftype,
9850 BUILT_IN_UNWIND_RESUME,
9851 ((targetm_common.except_unwind_info (&global_options)
9852 == UI_SJLJ)
9853 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
9854 ECF_NORETURN);
9855
9856 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
9857 {
9858 ftype = build_function_type_list (ptr_type_node, integer_type_node,
9859 NULL_TREE);
9860 local_define_builtin ("__builtin_return_address", ftype,
9861 BUILT_IN_RETURN_ADDRESS,
9862 "__builtin_return_address",
9863 ECF_NOTHROW);
9864 }
9865
9866 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
9867 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9868 {
9869 ftype = build_function_type_list (void_type_node, ptr_type_node,
9870 ptr_type_node, NULL_TREE);
9871 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
9872 local_define_builtin ("__cyg_profile_func_enter", ftype,
9873 BUILT_IN_PROFILE_FUNC_ENTER,
9874 "__cyg_profile_func_enter", 0);
9875 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9876 local_define_builtin ("__cyg_profile_func_exit", ftype,
9877 BUILT_IN_PROFILE_FUNC_EXIT,
9878 "__cyg_profile_func_exit", 0);
9879 }
9880
9881 /* The exception object and filter values from the runtime. The argument
9882 must be zero before exception lowering, i.e. from the front end. After
9883 exception lowering, it will be the region number for the exception
9884 landing pad. These functions are PURE instead of CONST to prevent
9885 them from being hoisted past the exception edge that will initialize
9886 its value in the landing pad. */
9887 ftype = build_function_type_list (ptr_type_node,
9888 integer_type_node, NULL_TREE);
9889 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
9890 /* Only use TM_PURE if we have TM language support. */
9891 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
9892 ecf_flags |= ECF_TM_PURE;
9893 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
9894 "__builtin_eh_pointer", ecf_flags);
9895
9896 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
9897 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
9898 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
9899 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9900
9901 ftype = build_function_type_list (void_type_node,
9902 integer_type_node, integer_type_node,
9903 NULL_TREE);
9904 local_define_builtin ("__builtin_eh_copy_values", ftype,
9905 BUILT_IN_EH_COPY_VALUES,
9906 "__builtin_eh_copy_values", ECF_NOTHROW);
9907
9908 /* Complex multiplication and division. These are handled as builtins
9909 rather than optabs because emit_library_call_value doesn't support
9910 complex. Further, we can do slightly better with folding these
9911 beasties if the real and complex parts of the arguments are separate. */
9912 {
9913 int mode;
9914
9915 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
9916 {
9917 char mode_name_buf[4], *q;
9918 const char *p;
9919 enum built_in_function mcode, dcode;
9920 tree type, inner_type;
9921 const char *prefix = "__";
9922
9923 if (targetm.libfunc_gnu_prefix)
9924 prefix = "__gnu_";
9925
9926 type = lang_hooks.types.type_for_mode ((machine_mode) mode, 0);
9927 if (type == NULL)
9928 continue;
9929 inner_type = TREE_TYPE (type);
9930
9931 ftype = build_function_type_list (type, inner_type, inner_type,
9932 inner_type, inner_type, NULL_TREE);
9933
9934 mcode = ((enum built_in_function)
9935 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9936 dcode = ((enum built_in_function)
9937 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9938
9939 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
9940 *q = TOLOWER (*p);
9941 *q = '\0';
9942
9943 /* For -ftrapping-math these should throw from a former
9944 -fnon-call-exception stmt. */
9945 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
9946 NULL);
9947 local_define_builtin (built_in_names[mcode], ftype, mcode,
9948 built_in_names[mcode],
9949 ECF_CONST | ECF_LEAF);
9950
9951 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
9952 NULL);
9953 local_define_builtin (built_in_names[dcode], ftype, dcode,
9954 built_in_names[dcode],
9955 ECF_CONST | ECF_LEAF);
9956 }
9957 }
9958
9959 init_internal_fns ();
9960 }
9961
9962 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
9963 better way.
9964
9965 If we requested a pointer to a vector, build up the pointers that
9966 we stripped off while looking for the inner type. Similarly for
9967 return values from functions.
9968
9969 The argument TYPE is the top of the chain, and BOTTOM is the
9970 new type which we will point to. */
9971
9972 tree
9973 reconstruct_complex_type (tree type, tree bottom)
9974 {
9975 tree inner, outer;
9976
9977 if (TREE_CODE (type) == POINTER_TYPE)
9978 {
9979 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9980 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
9981 TYPE_REF_CAN_ALIAS_ALL (type));
9982 }
9983 else if (TREE_CODE (type) == REFERENCE_TYPE)
9984 {
9985 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9986 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
9987 TYPE_REF_CAN_ALIAS_ALL (type));
9988 }
9989 else if (TREE_CODE (type) == ARRAY_TYPE)
9990 {
9991 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9992 outer = build_array_type (inner, TYPE_DOMAIN (type));
9993 }
9994 else if (TREE_CODE (type) == FUNCTION_TYPE)
9995 {
9996 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9997 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
9998 }
9999 else if (TREE_CODE (type) == METHOD_TYPE)
10000 {
10001 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10002 /* The build_method_type_directly() routine prepends 'this' to argument list,
10003 so we must compensate by getting rid of it. */
10004 outer
10005 = build_method_type_directly
10006 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10007 inner,
10008 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10009 }
10010 else if (TREE_CODE (type) == OFFSET_TYPE)
10011 {
10012 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10013 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10014 }
10015 else
10016 return bottom;
10017
10018 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10019 TYPE_QUALS (type));
10020 }
10021
10022 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10023 the inner type. */
10024 tree
10025 build_vector_type_for_mode (tree innertype, machine_mode mode)
10026 {
10027 poly_int64 nunits;
10028 unsigned int bitsize;
10029
10030 switch (GET_MODE_CLASS (mode))
10031 {
10032 case MODE_VECTOR_BOOL:
10033 case MODE_VECTOR_INT:
10034 case MODE_VECTOR_FLOAT:
10035 case MODE_VECTOR_FRACT:
10036 case MODE_VECTOR_UFRACT:
10037 case MODE_VECTOR_ACCUM:
10038 case MODE_VECTOR_UACCUM:
10039 nunits = GET_MODE_NUNITS (mode);
10040 break;
10041
10042 case MODE_INT:
10043 /* Check that there are no leftover bits. */
10044 bitsize = GET_MODE_BITSIZE (as_a <scalar_int_mode> (mode));
10045 gcc_assert (bitsize % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10046 nunits = bitsize / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10047 break;
10048
10049 default:
10050 gcc_unreachable ();
10051 }
10052
10053 return make_vector_type (innertype, nunits, mode);
10054 }
10055
10056 /* Similarly, but takes the inner type and number of units, which must be
10057 a power of two. */
10058
10059 tree
10060 build_vector_type (tree innertype, poly_int64 nunits)
10061 {
10062 return make_vector_type (innertype, nunits, VOIDmode);
10063 }
10064
10065 /* Build a truth vector with NUNITS units, giving it mode MASK_MODE. */
10066
10067 tree
10068 build_truth_vector_type_for_mode (poly_uint64 nunits, machine_mode mask_mode)
10069 {
10070 gcc_assert (mask_mode != BLKmode);
10071
10072 unsigned HOST_WIDE_INT esize;
10073 if (VECTOR_MODE_P (mask_mode))
10074 {
10075 poly_uint64 vsize = GET_MODE_BITSIZE (mask_mode);
10076 esize = vector_element_size (vsize, nunits);
10077 }
10078 else
10079 esize = 1;
10080
10081 tree bool_type = build_nonstandard_boolean_type (esize);
10082
10083 return make_vector_type (bool_type, nunits, mask_mode);
10084 }
10085
10086 /* Build a vector type that holds one boolean result for each element of
10087 vector type VECTYPE. The public interface for this operation is
10088 truth_type_for. */
10089
10090 static tree
10091 build_truth_vector_type_for (tree vectype)
10092 {
10093 machine_mode vector_mode = TYPE_MODE (vectype);
10094 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
10095
10096 machine_mode mask_mode;
10097 if (VECTOR_MODE_P (vector_mode)
10098 && targetm.vectorize.get_mask_mode (vector_mode).exists (&mask_mode))
10099 return build_truth_vector_type_for_mode (nunits, mask_mode);
10100
10101 poly_uint64 vsize = tree_to_poly_uint64 (TYPE_SIZE (vectype));
10102 unsigned HOST_WIDE_INT esize = vector_element_size (vsize, nunits);
10103 tree bool_type = build_nonstandard_boolean_type (esize);
10104
10105 return make_vector_type (bool_type, nunits, VOIDmode);
10106 }
10107
10108 /* Like build_vector_type, but builds a variant type with TYPE_VECTOR_OPAQUE
10109 set. */
10110
10111 tree
10112 build_opaque_vector_type (tree innertype, poly_int64 nunits)
10113 {
10114 tree t = make_vector_type (innertype, nunits, VOIDmode);
10115 tree cand;
10116 /* We always build the non-opaque variant before the opaque one,
10117 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10118 cand = TYPE_NEXT_VARIANT (t);
10119 if (cand
10120 && TYPE_VECTOR_OPAQUE (cand)
10121 && check_qualified_type (cand, t, TYPE_QUALS (t)))
10122 return cand;
10123 /* Othewise build a variant type and make sure to queue it after
10124 the non-opaque type. */
10125 cand = build_distinct_type_copy (t);
10126 TYPE_VECTOR_OPAQUE (cand) = true;
10127 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10128 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10129 TYPE_NEXT_VARIANT (t) = cand;
10130 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10131 return cand;
10132 }
10133
10134 /* Return the value of element I of VECTOR_CST T as a wide_int. */
10135
10136 static poly_wide_int
10137 vector_cst_int_elt (const_tree t, unsigned int i)
10138 {
10139 /* First handle elements that are directly encoded. */
10140 unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
10141 if (i < encoded_nelts)
10142 return wi::to_poly_wide (VECTOR_CST_ENCODED_ELT (t, i));
10143
10144 /* Identify the pattern that contains element I and work out the index of
10145 the last encoded element for that pattern. */
10146 unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
10147 unsigned int pattern = i % npatterns;
10148 unsigned int count = i / npatterns;
10149 unsigned int final_i = encoded_nelts - npatterns + pattern;
10150
10151 /* If there are no steps, the final encoded value is the right one. */
10152 if (!VECTOR_CST_STEPPED_P (t))
10153 return wi::to_poly_wide (VECTOR_CST_ENCODED_ELT (t, final_i));
10154
10155 /* Otherwise work out the value from the last two encoded elements. */
10156 tree v1 = VECTOR_CST_ENCODED_ELT (t, final_i - npatterns);
10157 tree v2 = VECTOR_CST_ENCODED_ELT (t, final_i);
10158 poly_wide_int diff = wi::to_poly_wide (v2) - wi::to_poly_wide (v1);
10159 return wi::to_poly_wide (v2) + (count - 2) * diff;
10160 }
10161
10162 /* Return the value of element I of VECTOR_CST T. */
10163
10164 tree
10165 vector_cst_elt (const_tree t, unsigned int i)
10166 {
10167 /* First handle elements that are directly encoded. */
10168 unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
10169 if (i < encoded_nelts)
10170 return VECTOR_CST_ENCODED_ELT (t, i);
10171
10172 /* If there are no steps, the final encoded value is the right one. */
10173 if (!VECTOR_CST_STEPPED_P (t))
10174 {
10175 /* Identify the pattern that contains element I and work out the index of
10176 the last encoded element for that pattern. */
10177 unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
10178 unsigned int pattern = i % npatterns;
10179 unsigned int final_i = encoded_nelts - npatterns + pattern;
10180 return VECTOR_CST_ENCODED_ELT (t, final_i);
10181 }
10182
10183 /* Otherwise work out the value from the last two encoded elements. */
10184 return wide_int_to_tree (TREE_TYPE (TREE_TYPE (t)),
10185 vector_cst_int_elt (t, i));
10186 }
10187
10188 /* Given an initializer INIT, return TRUE if INIT is zero or some
10189 aggregate of zeros. Otherwise return FALSE. If NONZERO is not
10190 null, set *NONZERO if and only if INIT is known not to be all
10191 zeros. The combination of return value of false and *NONZERO
10192 false implies that INIT may but need not be all zeros. Other
10193 combinations indicate definitive answers. */
10194
10195 bool
10196 initializer_zerop (const_tree init, bool *nonzero /* = NULL */)
10197 {
10198 bool dummy;
10199 if (!nonzero)
10200 nonzero = &dummy;
10201
10202 /* Conservatively clear NONZERO and set it only if INIT is definitely
10203 not all zero. */
10204 *nonzero = false;
10205
10206 STRIP_NOPS (init);
10207
10208 unsigned HOST_WIDE_INT off = 0;
10209
10210 switch (TREE_CODE (init))
10211 {
10212 case INTEGER_CST:
10213 if (integer_zerop (init))
10214 return true;
10215
10216 *nonzero = true;
10217 return false;
10218
10219 case REAL_CST:
10220 /* ??? Note that this is not correct for C4X float formats. There,
10221 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10222 negative exponent. */
10223 if (real_zerop (init)
10224 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init)))
10225 return true;
10226
10227 *nonzero = true;
10228 return false;
10229
10230 case FIXED_CST:
10231 if (fixed_zerop (init))
10232 return true;
10233
10234 *nonzero = true;
10235 return false;
10236
10237 case COMPLEX_CST:
10238 if (integer_zerop (init)
10239 || (real_zerop (init)
10240 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10241 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init)))))
10242 return true;
10243
10244 *nonzero = true;
10245 return false;
10246
10247 case VECTOR_CST:
10248 if (VECTOR_CST_NPATTERNS (init) == 1
10249 && VECTOR_CST_DUPLICATE_P (init)
10250 && initializer_zerop (VECTOR_CST_ENCODED_ELT (init, 0)))
10251 return true;
10252
10253 *nonzero = true;
10254 return false;
10255
10256 case CONSTRUCTOR:
10257 {
10258 if (TREE_CLOBBER_P (init))
10259 return false;
10260
10261 unsigned HOST_WIDE_INT idx;
10262 tree elt;
10263
10264 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10265 if (!initializer_zerop (elt, nonzero))
10266 return false;
10267
10268 return true;
10269 }
10270
10271 case MEM_REF:
10272 {
10273 tree arg = TREE_OPERAND (init, 0);
10274 if (TREE_CODE (arg) != ADDR_EXPR)
10275 return false;
10276 tree offset = TREE_OPERAND (init, 1);
10277 if (TREE_CODE (offset) != INTEGER_CST
10278 || !tree_fits_uhwi_p (offset))
10279 return false;
10280 off = tree_to_uhwi (offset);
10281 if (INT_MAX < off)
10282 return false;
10283 arg = TREE_OPERAND (arg, 0);
10284 if (TREE_CODE (arg) != STRING_CST)
10285 return false;
10286 init = arg;
10287 }
10288 /* Fall through. */
10289
10290 case STRING_CST:
10291 {
10292 gcc_assert (off <= INT_MAX);
10293
10294 int i = off;
10295 int n = TREE_STRING_LENGTH (init);
10296 if (n <= i)
10297 return false;
10298
10299 /* We need to loop through all elements to handle cases like
10300 "\0" and "\0foobar". */
10301 for (i = 0; i < n; ++i)
10302 if (TREE_STRING_POINTER (init)[i] != '\0')
10303 {
10304 *nonzero = true;
10305 return false;
10306 }
10307
10308 return true;
10309 }
10310
10311 default:
10312 return false;
10313 }
10314 }
10315
10316 /* Return true if EXPR is an initializer expression in which every element
10317 is a constant that is numerically equal to 0 or 1. The elements do not
10318 need to be equal to each other. */
10319
10320 bool
10321 initializer_each_zero_or_onep (const_tree expr)
10322 {
10323 STRIP_ANY_LOCATION_WRAPPER (expr);
10324
10325 switch (TREE_CODE (expr))
10326 {
10327 case INTEGER_CST:
10328 return integer_zerop (expr) || integer_onep (expr);
10329
10330 case REAL_CST:
10331 return real_zerop (expr) || real_onep (expr);
10332
10333 case VECTOR_CST:
10334 {
10335 unsigned HOST_WIDE_INT nelts = vector_cst_encoded_nelts (expr);
10336 if (VECTOR_CST_STEPPED_P (expr)
10337 && !TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr)).is_constant (&nelts))
10338 return false;
10339
10340 for (unsigned int i = 0; i < nelts; ++i)
10341 {
10342 tree elt = vector_cst_elt (expr, i);
10343 if (!initializer_each_zero_or_onep (elt))
10344 return false;
10345 }
10346
10347 return true;
10348 }
10349
10350 default:
10351 return false;
10352 }
10353 }
10354
10355 /* Check if vector VEC consists of all the equal elements and
10356 that the number of elements corresponds to the type of VEC.
10357 The function returns first element of the vector
10358 or NULL_TREE if the vector is not uniform. */
10359 tree
10360 uniform_vector_p (const_tree vec)
10361 {
10362 tree first, t;
10363 unsigned HOST_WIDE_INT i, nelts;
10364
10365 if (vec == NULL_TREE)
10366 return NULL_TREE;
10367
10368 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10369
10370 if (TREE_CODE (vec) == VEC_DUPLICATE_EXPR)
10371 return TREE_OPERAND (vec, 0);
10372
10373 else if (TREE_CODE (vec) == VECTOR_CST)
10374 {
10375 if (VECTOR_CST_NPATTERNS (vec) == 1 && VECTOR_CST_DUPLICATE_P (vec))
10376 return VECTOR_CST_ENCODED_ELT (vec, 0);
10377 return NULL_TREE;
10378 }
10379
10380 else if (TREE_CODE (vec) == CONSTRUCTOR
10381 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)).is_constant (&nelts))
10382 {
10383 first = error_mark_node;
10384
10385 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10386 {
10387 if (i == 0)
10388 {
10389 first = t;
10390 continue;
10391 }
10392 if (!operand_equal_p (first, t, 0))
10393 return NULL_TREE;
10394 }
10395 if (i != nelts)
10396 return NULL_TREE;
10397
10398 if (TREE_CODE (first) == CONSTRUCTOR || TREE_CODE (first) == VECTOR_CST)
10399 return uniform_vector_p (first);
10400 return first;
10401 }
10402
10403 return NULL_TREE;
10404 }
10405
10406 /* If the argument is INTEGER_CST, return it. If the argument is vector
10407 with all elements the same INTEGER_CST, return that INTEGER_CST. Otherwise
10408 return NULL_TREE.
10409 Look through location wrappers. */
10410
10411 tree
10412 uniform_integer_cst_p (tree t)
10413 {
10414 STRIP_ANY_LOCATION_WRAPPER (t);
10415
10416 if (TREE_CODE (t) == INTEGER_CST)
10417 return t;
10418
10419 if (VECTOR_TYPE_P (TREE_TYPE (t)))
10420 {
10421 t = uniform_vector_p (t);
10422 if (t && TREE_CODE (t) == INTEGER_CST)
10423 return t;
10424 }
10425
10426 return NULL_TREE;
10427 }
10428
10429 /* Checks to see if T is a constant or a constant vector and if each element E
10430 adheres to ~E + 1 == pow2 then return ~E otherwise NULL_TREE. */
10431
10432 tree
10433 bitmask_inv_cst_vector_p (tree t)
10434 {
10435
10436 tree_code code = TREE_CODE (t);
10437 tree type = TREE_TYPE (t);
10438
10439 if (!INTEGRAL_TYPE_P (type)
10440 && !VECTOR_INTEGER_TYPE_P (type))
10441 return NULL_TREE;
10442
10443 unsigned HOST_WIDE_INT nelts = 1;
10444 tree cst;
10445 unsigned int idx = 0;
10446 bool uniform = uniform_integer_cst_p (t);
10447 tree newtype = unsigned_type_for (type);
10448 tree_vector_builder builder;
10449 if (code == INTEGER_CST)
10450 cst = t;
10451 else
10452 {
10453 if (!VECTOR_CST_NELTS (t).is_constant (&nelts))
10454 return NULL_TREE;
10455
10456 cst = vector_cst_elt (t, 0);
10457 builder.new_vector (newtype, nelts, 1);
10458 }
10459
10460 tree ty = unsigned_type_for (TREE_TYPE (cst));
10461
10462 do
10463 {
10464 if (idx > 0)
10465 cst = vector_cst_elt (t, idx);
10466 wide_int icst = wi::to_wide (cst);
10467 wide_int inv = wi::bit_not (icst);
10468 icst = wi::add (1, inv);
10469 if (wi::popcount (icst) != 1)
10470 return NULL_TREE;
10471
10472 tree newcst = wide_int_to_tree (ty, inv);
10473
10474 if (uniform)
10475 return build_uniform_cst (newtype, newcst);
10476
10477 builder.quick_push (newcst);
10478 }
10479 while (++idx < nelts);
10480
10481 return builder.build ();
10482 }
10483
10484 /* If VECTOR_CST T has a single nonzero element, return the index of that
10485 element, otherwise return -1. */
10486
10487 int
10488 single_nonzero_element (const_tree t)
10489 {
10490 unsigned HOST_WIDE_INT nelts;
10491 unsigned int repeat_nelts;
10492 if (VECTOR_CST_NELTS (t).is_constant (&nelts))
10493 repeat_nelts = nelts;
10494 else if (VECTOR_CST_NELTS_PER_PATTERN (t) == 2)
10495 {
10496 nelts = vector_cst_encoded_nelts (t);
10497 repeat_nelts = VECTOR_CST_NPATTERNS (t);
10498 }
10499 else
10500 return -1;
10501
10502 int res = -1;
10503 for (unsigned int i = 0; i < nelts; ++i)
10504 {
10505 tree elt = vector_cst_elt (t, i);
10506 if (!integer_zerop (elt) && !real_zerop (elt))
10507 {
10508 if (res >= 0 || i >= repeat_nelts)
10509 return -1;
10510 res = i;
10511 }
10512 }
10513 return res;
10514 }
10515
10516 /* Build an empty statement at location LOC. */
10517
10518 tree
10519 build_empty_stmt (location_t loc)
10520 {
10521 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10522 SET_EXPR_LOCATION (t, loc);
10523 return t;
10524 }
10525
10526
10527 /* Build an OMP clause with code CODE. LOC is the location of the
10528 clause. */
10529
10530 tree
10531 build_omp_clause (location_t loc, enum omp_clause_code code)
10532 {
10533 tree t;
10534 int size, length;
10535
10536 length = omp_clause_num_ops[code];
10537 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10538
10539 record_node_allocation_statistics (OMP_CLAUSE, size);
10540
10541 t = (tree) ggc_internal_alloc (size);
10542 memset (t, 0, size);
10543 TREE_SET_CODE (t, OMP_CLAUSE);
10544 OMP_CLAUSE_SET_CODE (t, code);
10545 OMP_CLAUSE_LOCATION (t) = loc;
10546
10547 return t;
10548 }
10549
10550 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10551 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10552 Except for the CODE and operand count field, other storage for the
10553 object is initialized to zeros. */
10554
10555 tree
10556 build_vl_exp (enum tree_code code, int len MEM_STAT_DECL)
10557 {
10558 tree t;
10559 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10560
10561 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10562 gcc_assert (len >= 1);
10563
10564 record_node_allocation_statistics (code, length);
10565
10566 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10567
10568 TREE_SET_CODE (t, code);
10569
10570 /* Can't use TREE_OPERAND to store the length because if checking is
10571 enabled, it will try to check the length before we store it. :-P */
10572 t->exp.operands[0] = build_int_cst (sizetype, len);
10573
10574 return t;
10575 }
10576
10577 /* Helper function for build_call_* functions; build a CALL_EXPR with
10578 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10579 the argument slots. */
10580
10581 static tree
10582 build_call_1 (tree return_type, tree fn, int nargs)
10583 {
10584 tree t;
10585
10586 t = build_vl_exp (CALL_EXPR, nargs + 3);
10587 TREE_TYPE (t) = return_type;
10588 CALL_EXPR_FN (t) = fn;
10589 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10590
10591 return t;
10592 }
10593
10594 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10595 FN and a null static chain slot. NARGS is the number of call arguments
10596 which are specified as "..." arguments. */
10597
10598 tree
10599 build_call_nary (tree return_type, tree fn, int nargs, ...)
10600 {
10601 tree ret;
10602 va_list args;
10603 va_start (args, nargs);
10604 ret = build_call_valist (return_type, fn, nargs, args);
10605 va_end (args);
10606 return ret;
10607 }
10608
10609 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10610 FN and a null static chain slot. NARGS is the number of call arguments
10611 which are specified as a va_list ARGS. */
10612
10613 tree
10614 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10615 {
10616 tree t;
10617 int i;
10618
10619 t = build_call_1 (return_type, fn, nargs);
10620 for (i = 0; i < nargs; i++)
10621 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10622 process_call_operands (t);
10623 return t;
10624 }
10625
10626 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10627 FN and a null static chain slot. NARGS is the number of call arguments
10628 which are specified as a tree array ARGS. */
10629
10630 tree
10631 build_call_array_loc (location_t loc, tree return_type, tree fn,
10632 int nargs, const tree *args)
10633 {
10634 tree t;
10635 int i;
10636
10637 t = build_call_1 (return_type, fn, nargs);
10638 for (i = 0; i < nargs; i++)
10639 CALL_EXPR_ARG (t, i) = args[i];
10640 process_call_operands (t);
10641 SET_EXPR_LOCATION (t, loc);
10642 return t;
10643 }
10644
10645 /* Like build_call_array, but takes a vec. */
10646
10647 tree
10648 build_call_vec (tree return_type, tree fn, const vec<tree, va_gc> *args)
10649 {
10650 tree ret, t;
10651 unsigned int ix;
10652
10653 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10654 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10655 CALL_EXPR_ARG (ret, ix) = t;
10656 process_call_operands (ret);
10657 return ret;
10658 }
10659
10660 /* Conveniently construct a function call expression. FNDECL names the
10661 function to be called and N arguments are passed in the array
10662 ARGARRAY. */
10663
10664 tree
10665 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
10666 {
10667 tree fntype = TREE_TYPE (fndecl);
10668 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10669
10670 return fold_build_call_array_loc (loc, TREE_TYPE (fntype), fn, n, argarray);
10671 }
10672
10673 /* Conveniently construct a function call expression. FNDECL names the
10674 function to be called and the arguments are passed in the vector
10675 VEC. */
10676
10677 tree
10678 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
10679 {
10680 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
10681 vec_safe_address (vec));
10682 }
10683
10684
10685 /* Conveniently construct a function call expression. FNDECL names the
10686 function to be called, N is the number of arguments, and the "..."
10687 parameters are the argument expressions. */
10688
10689 tree
10690 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
10691 {
10692 va_list ap;
10693 tree *argarray = XALLOCAVEC (tree, n);
10694 int i;
10695
10696 va_start (ap, n);
10697 for (i = 0; i < n; i++)
10698 argarray[i] = va_arg (ap, tree);
10699 va_end (ap);
10700 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10701 }
10702
10703 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
10704 varargs macros aren't supported by all bootstrap compilers. */
10705
10706 tree
10707 build_call_expr (tree fndecl, int n, ...)
10708 {
10709 va_list ap;
10710 tree *argarray = XALLOCAVEC (tree, n);
10711 int i;
10712
10713 va_start (ap, n);
10714 for (i = 0; i < n; i++)
10715 argarray[i] = va_arg (ap, tree);
10716 va_end (ap);
10717 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
10718 }
10719
10720 /* Build an internal call to IFN, with arguments ARGS[0:N-1] and with return
10721 type TYPE. This is just like CALL_EXPR, except its CALL_EXPR_FN is NULL.
10722 It will get gimplified later into an ordinary internal function. */
10723
10724 tree
10725 build_call_expr_internal_loc_array (location_t loc, internal_fn ifn,
10726 tree type, int n, const tree *args)
10727 {
10728 tree t = build_call_1 (type, NULL_TREE, n);
10729 for (int i = 0; i < n; ++i)
10730 CALL_EXPR_ARG (t, i) = args[i];
10731 SET_EXPR_LOCATION (t, loc);
10732 CALL_EXPR_IFN (t) = ifn;
10733 process_call_operands (t);
10734 return t;
10735 }
10736
10737 /* Build internal call expression. This is just like CALL_EXPR, except
10738 its CALL_EXPR_FN is NULL. It will get gimplified later into ordinary
10739 internal function. */
10740
10741 tree
10742 build_call_expr_internal_loc (location_t loc, enum internal_fn ifn,
10743 tree type, int n, ...)
10744 {
10745 va_list ap;
10746 tree *argarray = XALLOCAVEC (tree, n);
10747 int i;
10748
10749 va_start (ap, n);
10750 for (i = 0; i < n; i++)
10751 argarray[i] = va_arg (ap, tree);
10752 va_end (ap);
10753 return build_call_expr_internal_loc_array (loc, ifn, type, n, argarray);
10754 }
10755
10756 /* Return a function call to FN, if the target is guaranteed to support it,
10757 or null otherwise.
10758
10759 N is the number of arguments, passed in the "...", and TYPE is the
10760 type of the return value. */
10761
10762 tree
10763 maybe_build_call_expr_loc (location_t loc, combined_fn fn, tree type,
10764 int n, ...)
10765 {
10766 va_list ap;
10767 tree *argarray = XALLOCAVEC (tree, n);
10768 int i;
10769
10770 va_start (ap, n);
10771 for (i = 0; i < n; i++)
10772 argarray[i] = va_arg (ap, tree);
10773 va_end (ap);
10774 if (internal_fn_p (fn))
10775 {
10776 internal_fn ifn = as_internal_fn (fn);
10777 if (direct_internal_fn_p (ifn))
10778 {
10779 tree_pair types = direct_internal_fn_types (ifn, type, argarray);
10780 if (!direct_internal_fn_supported_p (ifn, types,
10781 OPTIMIZE_FOR_BOTH))
10782 return NULL_TREE;
10783 }
10784 return build_call_expr_internal_loc_array (loc, ifn, type, n, argarray);
10785 }
10786 else
10787 {
10788 tree fndecl = builtin_decl_implicit (as_builtin_fn (fn));
10789 if (!fndecl)
10790 return NULL_TREE;
10791 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10792 }
10793 }
10794
10795 /* Return a function call to the appropriate builtin alloca variant.
10796
10797 SIZE is the size to be allocated. ALIGN, if non-zero, is the requested
10798 alignment of the allocated area. MAX_SIZE, if non-negative, is an upper
10799 bound for SIZE in case it is not a fixed value. */
10800
10801 tree
10802 build_alloca_call_expr (tree size, unsigned int align, HOST_WIDE_INT max_size)
10803 {
10804 if (max_size >= 0)
10805 {
10806 tree t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX);
10807 return
10808 build_call_expr (t, 3, size, size_int (align), size_int (max_size));
10809 }
10810 else if (align > 0)
10811 {
10812 tree t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
10813 return build_call_expr (t, 2, size, size_int (align));
10814 }
10815 else
10816 {
10817 tree t = builtin_decl_explicit (BUILT_IN_ALLOCA);
10818 return build_call_expr (t, 1, size);
10819 }
10820 }
10821
10822 /* The built-in decl to use to mark code points believed to be unreachable.
10823 Typically __builtin_unreachable, but __builtin_trap if
10824 -fsanitize=unreachable -fsanitize-trap=unreachable. If only
10825 -fsanitize=unreachable, we rely on sanopt to replace calls with the
10826 appropriate ubsan function. When building a call directly, use
10827 {gimple_},build_builtin_unreachable instead. */
10828
10829 tree
10830 builtin_decl_unreachable ()
10831 {
10832 enum built_in_function fncode = BUILT_IN_UNREACHABLE;
10833
10834 if (sanitize_flags_p (SANITIZE_UNREACHABLE)
10835 ? (flag_sanitize_trap & SANITIZE_UNREACHABLE)
10836 : flag_unreachable_traps)
10837 fncode = BUILT_IN_TRAP;
10838 /* For non-trapping sanitize, we will rewrite __builtin_unreachable () later,
10839 in the sanopt pass. */
10840
10841 return builtin_decl_explicit (fncode);
10842 }
10843
10844 /* Build a call to __builtin_unreachable, possibly rewritten by
10845 -fsanitize=unreachable. Use this rather than the above when practical. */
10846
10847 tree
10848 build_builtin_unreachable (location_t loc)
10849 {
10850 tree data = NULL_TREE;
10851 tree fn = sanitize_unreachable_fn (&data, loc);
10852 return build_call_expr_loc (loc, fn, data != NULL_TREE, data);
10853 }
10854
10855 /* Create a new constant string literal of type ELTYPE[SIZE] (or LEN
10856 if SIZE == -1) and return a tree node representing char* pointer to
10857 it as an ADDR_EXPR (ARRAY_REF (ELTYPE, ...)). When STR is nonnull
10858 the STRING_CST value is the LEN bytes at STR (the representation
10859 of the string, which may be wide). Otherwise it's all zeros. */
10860
10861 tree
10862 build_string_literal (unsigned len, const char *str /* = NULL */,
10863 tree eltype /* = char_type_node */,
10864 unsigned HOST_WIDE_INT size /* = -1 */)
10865 {
10866 tree t = build_string (len, str);
10867 /* Set the maximum valid index based on the string length or SIZE. */
10868 unsigned HOST_WIDE_INT maxidx
10869 = (size == HOST_WIDE_INT_M1U ? len : size) - 1;
10870
10871 tree index = build_index_type (size_int (maxidx));
10872 eltype = build_type_variant (eltype, 1, 0);
10873 tree type = build_array_type (eltype, index);
10874 TREE_TYPE (t) = type;
10875 TREE_CONSTANT (t) = 1;
10876 TREE_READONLY (t) = 1;
10877 TREE_STATIC (t) = 1;
10878
10879 type = build_pointer_type (eltype);
10880 t = build1 (ADDR_EXPR, type,
10881 build4 (ARRAY_REF, eltype,
10882 t, integer_zero_node, NULL_TREE, NULL_TREE));
10883 return t;
10884 }
10885
10886
10887
10888 /* Return true if T (assumed to be a DECL) must be assigned a memory
10889 location. */
10890
10891 bool
10892 needs_to_live_in_memory (const_tree t)
10893 {
10894 return (TREE_ADDRESSABLE (t)
10895 || is_global_var (t)
10896 || (TREE_CODE (t) == RESULT_DECL
10897 && !DECL_BY_REFERENCE (t)
10898 && aggregate_value_p (t, current_function_decl)));
10899 }
10900
10901 /* Return value of a constant X and sign-extend it. */
10902
10903 HOST_WIDE_INT
10904 int_cst_value (const_tree x)
10905 {
10906 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10907 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10908
10909 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10910 gcc_assert (cst_and_fits_in_hwi (x));
10911
10912 if (bits < HOST_BITS_PER_WIDE_INT)
10913 {
10914 bool negative = ((val >> (bits - 1)) & 1) != 0;
10915 if (negative)
10916 val |= HOST_WIDE_INT_M1U << (bits - 1) << 1;
10917 else
10918 val &= ~(HOST_WIDE_INT_M1U << (bits - 1) << 1);
10919 }
10920
10921 return val;
10922 }
10923
10924 /* If TYPE is an integral or pointer type, return an integer type with
10925 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10926 if TYPE is already an integer type of signedness UNSIGNEDP.
10927 If TYPE is a floating-point type, return an integer type with the same
10928 bitsize and with the signedness given by UNSIGNEDP; this is useful
10929 when doing bit-level operations on a floating-point value. */
10930
10931 tree
10932 signed_or_unsigned_type_for (int unsignedp, tree type)
10933 {
10934 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) == unsignedp)
10935 return type;
10936
10937 if (TREE_CODE (type) == VECTOR_TYPE)
10938 {
10939 tree inner = TREE_TYPE (type);
10940 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10941 if (!inner2)
10942 return NULL_TREE;
10943 if (inner == inner2)
10944 return type;
10945 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10946 }
10947
10948 if (TREE_CODE (type) == COMPLEX_TYPE)
10949 {
10950 tree inner = TREE_TYPE (type);
10951 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10952 if (!inner2)
10953 return NULL_TREE;
10954 if (inner == inner2)
10955 return type;
10956 return build_complex_type (inner2);
10957 }
10958
10959 unsigned int bits;
10960 if (INTEGRAL_TYPE_P (type)
10961 || POINTER_TYPE_P (type)
10962 || TREE_CODE (type) == OFFSET_TYPE)
10963 bits = TYPE_PRECISION (type);
10964 else if (TREE_CODE (type) == REAL_TYPE)
10965 bits = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (type));
10966 else
10967 return NULL_TREE;
10968
10969 return build_nonstandard_integer_type (bits, unsignedp);
10970 }
10971
10972 /* If TYPE is an integral or pointer type, return an integer type with
10973 the same precision which is unsigned, or itself if TYPE is already an
10974 unsigned integer type. If TYPE is a floating-point type, return an
10975 unsigned integer type with the same bitsize as TYPE. */
10976
10977 tree
10978 unsigned_type_for (tree type)
10979 {
10980 return signed_or_unsigned_type_for (1, type);
10981 }
10982
10983 /* If TYPE is an integral or pointer type, return an integer type with
10984 the same precision which is signed, or itself if TYPE is already a
10985 signed integer type. If TYPE is a floating-point type, return a
10986 signed integer type with the same bitsize as TYPE. */
10987
10988 tree
10989 signed_type_for (tree type)
10990 {
10991 return signed_or_unsigned_type_for (0, type);
10992 }
10993
10994 /* - For VECTOR_TYPEs:
10995 - The truth type must be a VECTOR_BOOLEAN_TYPE.
10996 - The number of elements must match (known_eq).
10997 - targetm.vectorize.get_mask_mode exists, and exactly
10998 the same mode as the truth type.
10999 - Otherwise, the truth type must be a BOOLEAN_TYPE
11000 or useless_type_conversion_p to BOOLEAN_TYPE. */
11001 bool
11002 is_truth_type_for (tree type, tree truth_type)
11003 {
11004 machine_mode mask_mode = TYPE_MODE (truth_type);
11005 machine_mode vmode = TYPE_MODE (type);
11006 machine_mode tmask_mode;
11007
11008 if (TREE_CODE (type) == VECTOR_TYPE)
11009 {
11010 if (VECTOR_BOOLEAN_TYPE_P (truth_type)
11011 && known_eq (TYPE_VECTOR_SUBPARTS (type),
11012 TYPE_VECTOR_SUBPARTS (truth_type))
11013 && targetm.vectorize.get_mask_mode (vmode).exists (&tmask_mode)
11014 && tmask_mode == mask_mode)
11015 return true;
11016
11017 return false;
11018 }
11019
11020 return useless_type_conversion_p (boolean_type_node, truth_type);
11021 }
11022
11023 /* If TYPE is a vector type, return a signed integer vector type with the
11024 same width and number of subparts. Otherwise return boolean_type_node. */
11025
11026 tree
11027 truth_type_for (tree type)
11028 {
11029 if (TREE_CODE (type) == VECTOR_TYPE)
11030 {
11031 if (VECTOR_BOOLEAN_TYPE_P (type))
11032 return type;
11033 return build_truth_vector_type_for (type);
11034 }
11035 else
11036 return boolean_type_node;
11037 }
11038
11039 /* Returns the largest value obtainable by casting something in INNER type to
11040 OUTER type. */
11041
11042 tree
11043 upper_bound_in_type (tree outer, tree inner)
11044 {
11045 unsigned int det = 0;
11046 unsigned oprec = TYPE_PRECISION (outer);
11047 unsigned iprec = TYPE_PRECISION (inner);
11048 unsigned prec;
11049
11050 /* Compute a unique number for every combination. */
11051 det |= (oprec > iprec) ? 4 : 0;
11052 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
11053 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
11054
11055 /* Determine the exponent to use. */
11056 switch (det)
11057 {
11058 case 0:
11059 case 1:
11060 /* oprec <= iprec, outer: signed, inner: don't care. */
11061 prec = oprec - 1;
11062 break;
11063 case 2:
11064 case 3:
11065 /* oprec <= iprec, outer: unsigned, inner: don't care. */
11066 prec = oprec;
11067 break;
11068 case 4:
11069 /* oprec > iprec, outer: signed, inner: signed. */
11070 prec = iprec - 1;
11071 break;
11072 case 5:
11073 /* oprec > iprec, outer: signed, inner: unsigned. */
11074 prec = iprec;
11075 break;
11076 case 6:
11077 /* oprec > iprec, outer: unsigned, inner: signed. */
11078 prec = oprec;
11079 break;
11080 case 7:
11081 /* oprec > iprec, outer: unsigned, inner: unsigned. */
11082 prec = iprec;
11083 break;
11084 default:
11085 gcc_unreachable ();
11086 }
11087
11088 return wide_int_to_tree (outer,
11089 wi::mask (prec, false, TYPE_PRECISION (outer)));
11090 }
11091
11092 /* Returns the smallest value obtainable by casting something in INNER type to
11093 OUTER type. */
11094
11095 tree
11096 lower_bound_in_type (tree outer, tree inner)
11097 {
11098 unsigned oprec = TYPE_PRECISION (outer);
11099 unsigned iprec = TYPE_PRECISION (inner);
11100
11101 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
11102 and obtain 0. */
11103 if (TYPE_UNSIGNED (outer)
11104 /* If we are widening something of an unsigned type, OUTER type
11105 contains all values of INNER type. In particular, both INNER
11106 and OUTER types have zero in common. */
11107 || (oprec > iprec && TYPE_UNSIGNED (inner)))
11108 return build_int_cst (outer, 0);
11109 else
11110 {
11111 /* If we are widening a signed type to another signed type, we
11112 want to obtain -2^^(iprec-1). If we are keeping the
11113 precision or narrowing to a signed type, we want to obtain
11114 -2^(oprec-1). */
11115 unsigned prec = oprec > iprec ? iprec : oprec;
11116 return wide_int_to_tree (outer,
11117 wi::mask (prec - 1, true,
11118 TYPE_PRECISION (outer)));
11119 }
11120 }
11121
11122 /* Return nonzero if two operands that are suitable for PHI nodes are
11123 necessarily equal. Specifically, both ARG0 and ARG1 must be either
11124 SSA_NAME or invariant. Note that this is strictly an optimization.
11125 That is, callers of this function can directly call operand_equal_p
11126 and get the same result, only slower. */
11127
11128 int
11129 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
11130 {
11131 if (arg0 == arg1)
11132 return 1;
11133 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
11134 return 0;
11135 return operand_equal_p (arg0, arg1, 0);
11136 }
11137
11138 /* Returns number of zeros at the end of binary representation of X. */
11139
11140 tree
11141 num_ending_zeros (const_tree x)
11142 {
11143 return build_int_cst (TREE_TYPE (x), wi::ctz (wi::to_wide (x)));
11144 }
11145
11146
11147 #define WALK_SUBTREE(NODE) \
11148 do \
11149 { \
11150 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
11151 if (result) \
11152 return result; \
11153 } \
11154 while (0)
11155
11156 /* This is a subroutine of walk_tree that walks field of TYPE that are to
11157 be walked whenever a type is seen in the tree. Rest of operands and return
11158 value are as for walk_tree. */
11159
11160 static tree
11161 walk_type_fields (tree type, walk_tree_fn func, void *data,
11162 hash_set<tree> *pset, walk_tree_lh lh)
11163 {
11164 tree result = NULL_TREE;
11165
11166 switch (TREE_CODE (type))
11167 {
11168 case POINTER_TYPE:
11169 case REFERENCE_TYPE:
11170 case VECTOR_TYPE:
11171 /* We have to worry about mutually recursive pointers. These can't
11172 be written in C. They can in Ada. It's pathological, but
11173 there's an ACATS test (c38102a) that checks it. Deal with this
11174 by checking if we're pointing to another pointer, that one
11175 points to another pointer, that one does too, and we have no htab.
11176 If so, get a hash table. We check three levels deep to avoid
11177 the cost of the hash table if we don't need one. */
11178 if (POINTER_TYPE_P (TREE_TYPE (type))
11179 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
11180 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
11181 && !pset)
11182 {
11183 result = walk_tree_without_duplicates (&TREE_TYPE (type),
11184 func, data);
11185 if (result)
11186 return result;
11187
11188 break;
11189 }
11190
11191 /* fall through */
11192
11193 case COMPLEX_TYPE:
11194 WALK_SUBTREE (TREE_TYPE (type));
11195 break;
11196
11197 case METHOD_TYPE:
11198 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
11199
11200 /* Fall through. */
11201
11202 case FUNCTION_TYPE:
11203 WALK_SUBTREE (TREE_TYPE (type));
11204 {
11205 tree arg;
11206
11207 /* We never want to walk into default arguments. */
11208 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
11209 WALK_SUBTREE (TREE_VALUE (arg));
11210 }
11211 break;
11212
11213 case ARRAY_TYPE:
11214 /* Don't follow this nodes's type if a pointer for fear that
11215 we'll have infinite recursion. If we have a PSET, then we
11216 need not fear. */
11217 if (pset
11218 || (!POINTER_TYPE_P (TREE_TYPE (type))
11219 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
11220 WALK_SUBTREE (TREE_TYPE (type));
11221 WALK_SUBTREE (TYPE_DOMAIN (type));
11222 break;
11223
11224 case OFFSET_TYPE:
11225 WALK_SUBTREE (TREE_TYPE (type));
11226 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
11227 break;
11228
11229 default:
11230 break;
11231 }
11232
11233 return NULL_TREE;
11234 }
11235
11236 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
11237 called with the DATA and the address of each sub-tree. If FUNC returns a
11238 non-NULL value, the traversal is stopped, and the value returned by FUNC
11239 is returned. If PSET is non-NULL it is used to record the nodes visited,
11240 and to avoid visiting a node more than once. */
11241
11242 tree
11243 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
11244 hash_set<tree> *pset, walk_tree_lh lh)
11245 {
11246 enum tree_code code;
11247 int walk_subtrees;
11248 tree result;
11249
11250 #define WALK_SUBTREE_TAIL(NODE) \
11251 do \
11252 { \
11253 tp = & (NODE); \
11254 goto tail_recurse; \
11255 } \
11256 while (0)
11257
11258 tail_recurse:
11259 /* Skip empty subtrees. */
11260 if (!*tp)
11261 return NULL_TREE;
11262
11263 /* Don't walk the same tree twice, if the user has requested
11264 that we avoid doing so. */
11265 if (pset && pset->add (*tp))
11266 return NULL_TREE;
11267
11268 /* Call the function. */
11269 walk_subtrees = 1;
11270 result = (*func) (tp, &walk_subtrees, data);
11271
11272 /* If we found something, return it. */
11273 if (result)
11274 return result;
11275
11276 code = TREE_CODE (*tp);
11277
11278 /* Even if we didn't, FUNC may have decided that there was nothing
11279 interesting below this point in the tree. */
11280 if (!walk_subtrees)
11281 {
11282 /* But we still need to check our siblings. */
11283 if (code == TREE_LIST)
11284 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11285 else if (code == OMP_CLAUSE)
11286 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11287 else
11288 return NULL_TREE;
11289 }
11290
11291 if (lh)
11292 {
11293 result = (*lh) (tp, &walk_subtrees, func, data, pset);
11294 if (result || !walk_subtrees)
11295 return result;
11296 }
11297
11298 switch (code)
11299 {
11300 case ERROR_MARK:
11301 case IDENTIFIER_NODE:
11302 case INTEGER_CST:
11303 case REAL_CST:
11304 case FIXED_CST:
11305 case STRING_CST:
11306 case BLOCK:
11307 case PLACEHOLDER_EXPR:
11308 case SSA_NAME:
11309 case FIELD_DECL:
11310 case RESULT_DECL:
11311 /* None of these have subtrees other than those already walked
11312 above. */
11313 break;
11314
11315 case TREE_LIST:
11316 WALK_SUBTREE (TREE_VALUE (*tp));
11317 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11318
11319 case TREE_VEC:
11320 {
11321 int len = TREE_VEC_LENGTH (*tp);
11322
11323 if (len == 0)
11324 break;
11325
11326 /* Walk all elements but the first. */
11327 while (--len)
11328 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
11329
11330 /* Now walk the first one as a tail call. */
11331 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
11332 }
11333
11334 case VECTOR_CST:
11335 {
11336 unsigned len = vector_cst_encoded_nelts (*tp);
11337 if (len == 0)
11338 break;
11339 /* Walk all elements but the first. */
11340 while (--len)
11341 WALK_SUBTREE (VECTOR_CST_ENCODED_ELT (*tp, len));
11342 /* Now walk the first one as a tail call. */
11343 WALK_SUBTREE_TAIL (VECTOR_CST_ENCODED_ELT (*tp, 0));
11344 }
11345
11346 case COMPLEX_CST:
11347 WALK_SUBTREE (TREE_REALPART (*tp));
11348 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
11349
11350 case CONSTRUCTOR:
11351 {
11352 unsigned HOST_WIDE_INT idx;
11353 constructor_elt *ce;
11354
11355 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*tp), idx, &ce);
11356 idx++)
11357 WALK_SUBTREE (ce->value);
11358 }
11359 break;
11360
11361 case SAVE_EXPR:
11362 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
11363
11364 case BIND_EXPR:
11365 {
11366 tree decl;
11367 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
11368 {
11369 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11370 into declarations that are just mentioned, rather than
11371 declared; they don't really belong to this part of the tree.
11372 And, we can see cycles: the initializer for a declaration
11373 can refer to the declaration itself. */
11374 WALK_SUBTREE (DECL_INITIAL (decl));
11375 WALK_SUBTREE (DECL_SIZE (decl));
11376 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11377 }
11378 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
11379 }
11380
11381 case STATEMENT_LIST:
11382 {
11383 tree_stmt_iterator i;
11384 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
11385 WALK_SUBTREE (*tsi_stmt_ptr (i));
11386 }
11387 break;
11388
11389 case OMP_CLAUSE:
11390 {
11391 int len = omp_clause_num_ops[OMP_CLAUSE_CODE (*tp)];
11392 for (int i = 0; i < len; i++)
11393 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11394 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11395 }
11396
11397 case TARGET_EXPR:
11398 {
11399 int i, len;
11400
11401 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11402 But, we only want to walk once. */
11403 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11404 for (i = 0; i < len; ++i)
11405 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11406 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11407 }
11408
11409 case DECL_EXPR:
11410 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11411 defining. We only want to walk into these fields of a type in this
11412 case and not in the general case of a mere reference to the type.
11413
11414 The criterion is as follows: if the field can be an expression, it
11415 must be walked only here. This should be in keeping with the fields
11416 that are directly gimplified in gimplify_type_sizes in order for the
11417 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11418 variable-sized types.
11419
11420 Note that DECLs get walked as part of processing the BIND_EXPR. */
11421 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11422 {
11423 /* Call the function for the decl so e.g. copy_tree_body_r can
11424 replace it with the remapped one. */
11425 result = (*func) (&DECL_EXPR_DECL (*tp), &walk_subtrees, data);
11426 if (result || !walk_subtrees)
11427 return result;
11428
11429 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11430 if (TREE_CODE (*type_p) == ERROR_MARK)
11431 return NULL_TREE;
11432
11433 /* Call the function for the type. See if it returns anything or
11434 doesn't want us to continue. If we are to continue, walk both
11435 the normal fields and those for the declaration case. */
11436 result = (*func) (type_p, &walk_subtrees, data);
11437 if (result || !walk_subtrees)
11438 return result;
11439
11440 /* But do not walk a pointed-to type since it may itself need to
11441 be walked in the declaration case if it isn't anonymous. */
11442 if (!POINTER_TYPE_P (*type_p))
11443 {
11444 result = walk_type_fields (*type_p, func, data, pset, lh);
11445 if (result)
11446 return result;
11447 }
11448
11449 /* If this is a record type, also walk the fields. */
11450 if (RECORD_OR_UNION_TYPE_P (*type_p))
11451 {
11452 tree field;
11453
11454 for (field = TYPE_FIELDS (*type_p); field;
11455 field = DECL_CHAIN (field))
11456 {
11457 /* We'd like to look at the type of the field, but we can
11458 easily get infinite recursion. So assume it's pointed
11459 to elsewhere in the tree. Also, ignore things that
11460 aren't fields. */
11461 if (TREE_CODE (field) != FIELD_DECL)
11462 continue;
11463
11464 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11465 WALK_SUBTREE (DECL_SIZE (field));
11466 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11467 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11468 WALK_SUBTREE (DECL_QUALIFIER (field));
11469 }
11470 }
11471
11472 /* Same for scalar types. */
11473 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11474 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11475 || TREE_CODE (*type_p) == INTEGER_TYPE
11476 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11477 || TREE_CODE (*type_p) == REAL_TYPE)
11478 {
11479 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11480 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11481 }
11482
11483 WALK_SUBTREE (TYPE_SIZE (*type_p));
11484 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11485 }
11486 /* FALLTHRU */
11487
11488 default:
11489 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11490 {
11491 int i, len;
11492
11493 /* Walk over all the sub-trees of this operand. */
11494 len = TREE_OPERAND_LENGTH (*tp);
11495
11496 /* Go through the subtrees. We need to do this in forward order so
11497 that the scope of a FOR_EXPR is handled properly. */
11498 if (len)
11499 {
11500 for (i = 0; i < len - 1; ++i)
11501 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11502 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11503 }
11504 }
11505 /* If this is a type, walk the needed fields in the type. */
11506 else if (TYPE_P (*tp))
11507 return walk_type_fields (*tp, func, data, pset, lh);
11508 break;
11509 }
11510
11511 /* We didn't find what we were looking for. */
11512 return NULL_TREE;
11513
11514 #undef WALK_SUBTREE_TAIL
11515 }
11516 #undef WALK_SUBTREE
11517
11518 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11519
11520 tree
11521 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11522 walk_tree_lh lh)
11523 {
11524 tree result;
11525
11526 hash_set<tree> pset;
11527 result = walk_tree_1 (tp, func, data, &pset, lh);
11528 return result;
11529 }
11530
11531
11532 tree
11533 tree_block (tree t)
11534 {
11535 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11536
11537 if (IS_EXPR_CODE_CLASS (c))
11538 return LOCATION_BLOCK (t->exp.locus);
11539 gcc_unreachable ();
11540 return NULL;
11541 }
11542
11543 void
11544 tree_set_block (tree t, tree b)
11545 {
11546 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11547
11548 if (IS_EXPR_CODE_CLASS (c))
11549 {
11550 t->exp.locus = set_block (t->exp.locus, b);
11551 }
11552 else
11553 gcc_unreachable ();
11554 }
11555
11556 /* Create a nameless artificial label and put it in the current
11557 function context. The label has a location of LOC. Returns the
11558 newly created label. */
11559
11560 tree
11561 create_artificial_label (location_t loc)
11562 {
11563 tree lab = build_decl (loc,
11564 LABEL_DECL, NULL_TREE, void_type_node);
11565
11566 DECL_ARTIFICIAL (lab) = 1;
11567 DECL_IGNORED_P (lab) = 1;
11568 DECL_CONTEXT (lab) = current_function_decl;
11569 return lab;
11570 }
11571
11572 /* Given a tree, try to return a useful variable name that we can use
11573 to prefix a temporary that is being assigned the value of the tree.
11574 I.E. given <temp> = &A, return A. */
11575
11576 const char *
11577 get_name (tree t)
11578 {
11579 tree stripped_decl;
11580
11581 stripped_decl = t;
11582 STRIP_NOPS (stripped_decl);
11583 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11584 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11585 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11586 {
11587 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11588 if (!name)
11589 return NULL;
11590 return IDENTIFIER_POINTER (name);
11591 }
11592 else
11593 {
11594 switch (TREE_CODE (stripped_decl))
11595 {
11596 case ADDR_EXPR:
11597 return get_name (TREE_OPERAND (stripped_decl, 0));
11598 default:
11599 return NULL;
11600 }
11601 }
11602 }
11603
11604 /* Return true if TYPE has a variable argument list. */
11605
11606 bool
11607 stdarg_p (const_tree fntype)
11608 {
11609 function_args_iterator args_iter;
11610 tree n = NULL_TREE, t;
11611
11612 if (!fntype)
11613 return false;
11614
11615 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
11616 {
11617 n = t;
11618 }
11619
11620 return n != NULL_TREE && n != void_type_node;
11621 }
11622
11623 /* Return true if TYPE has a prototype. */
11624
11625 bool
11626 prototype_p (const_tree fntype)
11627 {
11628 tree t;
11629
11630 gcc_assert (fntype != NULL_TREE);
11631
11632 t = TYPE_ARG_TYPES (fntype);
11633 return (t != NULL_TREE);
11634 }
11635
11636 /* If BLOCK is inlined from an __attribute__((__artificial__))
11637 routine, return pointer to location from where it has been
11638 called. */
11639 location_t *
11640 block_nonartificial_location (tree block)
11641 {
11642 location_t *ret = NULL;
11643
11644 while (block && TREE_CODE (block) == BLOCK
11645 && BLOCK_ABSTRACT_ORIGIN (block))
11646 {
11647 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11648 if (TREE_CODE (ao) == FUNCTION_DECL)
11649 {
11650 /* If AO is an artificial inline, point RET to the
11651 call site locus at which it has been inlined and continue
11652 the loop, in case AO's caller is also an artificial
11653 inline. */
11654 if (DECL_DECLARED_INLINE_P (ao)
11655 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11656 ret = &BLOCK_SOURCE_LOCATION (block);
11657 else
11658 break;
11659 }
11660 else if (TREE_CODE (ao) != BLOCK)
11661 break;
11662
11663 block = BLOCK_SUPERCONTEXT (block);
11664 }
11665 return ret;
11666 }
11667
11668
11669 /* If EXP is inlined from an __attribute__((__artificial__))
11670 function, return the location of the original call expression. */
11671
11672 location_t
11673 tree_nonartificial_location (tree exp)
11674 {
11675 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11676
11677 if (loc)
11678 return *loc;
11679 else
11680 return EXPR_LOCATION (exp);
11681 }
11682
11683 /* Return the location into which EXP has been inlined. Analogous
11684 to tree_nonartificial_location() above but not limited to artificial
11685 functions declared inline. If SYSTEM_HEADER is true, return
11686 the macro expansion point of the location if it's in a system header */
11687
11688 location_t
11689 tree_inlined_location (tree exp, bool system_header /* = true */)
11690 {
11691 location_t loc = UNKNOWN_LOCATION;
11692
11693 tree block = TREE_BLOCK (exp);
11694
11695 while (block && TREE_CODE (block) == BLOCK
11696 && BLOCK_ABSTRACT_ORIGIN (block))
11697 {
11698 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11699 if (TREE_CODE (ao) == FUNCTION_DECL)
11700 loc = BLOCK_SOURCE_LOCATION (block);
11701 else if (TREE_CODE (ao) != BLOCK)
11702 break;
11703
11704 block = BLOCK_SUPERCONTEXT (block);
11705 }
11706
11707 if (loc == UNKNOWN_LOCATION)
11708 {
11709 loc = EXPR_LOCATION (exp);
11710 if (system_header)
11711 /* Only consider macro expansion when the block traversal failed
11712 to find a location. Otherwise it's not relevant. */
11713 return expansion_point_location_if_in_system_header (loc);
11714 }
11715
11716 return loc;
11717 }
11718
11719 /* These are the hash table functions for the hash table of OPTIMIZATION_NODE
11720 nodes. */
11721
11722 /* Return the hash code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11723
11724 hashval_t
11725 cl_option_hasher::hash (tree x)
11726 {
11727 const_tree const t = x;
11728
11729 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11730 return cl_optimization_hash (TREE_OPTIMIZATION (t));
11731 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11732 return cl_target_option_hash (TREE_TARGET_OPTION (t));
11733 else
11734 gcc_unreachable ();
11735 }
11736
11737 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11738 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11739 same. */
11740
11741 bool
11742 cl_option_hasher::equal (tree x, tree y)
11743 {
11744 const_tree const xt = x;
11745 const_tree const yt = y;
11746
11747 if (TREE_CODE (xt) != TREE_CODE (yt))
11748 return 0;
11749
11750 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11751 return cl_optimization_option_eq (TREE_OPTIMIZATION (xt),
11752 TREE_OPTIMIZATION (yt));
11753 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11754 return cl_target_option_eq (TREE_TARGET_OPTION (xt),
11755 TREE_TARGET_OPTION (yt));
11756 else
11757 gcc_unreachable ();
11758 }
11759
11760 /* Build an OPTIMIZATION_NODE based on the options in OPTS and OPTS_SET. */
11761
11762 tree
11763 build_optimization_node (struct gcc_options *opts,
11764 struct gcc_options *opts_set)
11765 {
11766 tree t;
11767
11768 /* Use the cache of optimization nodes. */
11769
11770 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11771 opts, opts_set);
11772
11773 tree *slot = cl_option_hash_table->find_slot (cl_optimization_node, INSERT);
11774 t = *slot;
11775 if (!t)
11776 {
11777 /* Insert this one into the hash table. */
11778 t = cl_optimization_node;
11779 *slot = t;
11780
11781 /* Make a new node for next time round. */
11782 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11783 }
11784
11785 return t;
11786 }
11787
11788 /* Build a TARGET_OPTION_NODE based on the options in OPTS and OPTS_SET. */
11789
11790 tree
11791 build_target_option_node (struct gcc_options *opts,
11792 struct gcc_options *opts_set)
11793 {
11794 tree t;
11795
11796 /* Use the cache of optimization nodes. */
11797
11798 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11799 opts, opts_set);
11800
11801 tree *slot = cl_option_hash_table->find_slot (cl_target_option_node, INSERT);
11802 t = *slot;
11803 if (!t)
11804 {
11805 /* Insert this one into the hash table. */
11806 t = cl_target_option_node;
11807 *slot = t;
11808
11809 /* Make a new node for next time round. */
11810 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11811 }
11812
11813 return t;
11814 }
11815
11816 /* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
11817 so that they aren't saved during PCH writing. */
11818
11819 void
11820 prepare_target_option_nodes_for_pch (void)
11821 {
11822 hash_table<cl_option_hasher>::iterator iter = cl_option_hash_table->begin ();
11823 for (; iter != cl_option_hash_table->end (); ++iter)
11824 if (TREE_CODE (*iter) == TARGET_OPTION_NODE)
11825 TREE_TARGET_GLOBALS (*iter) = NULL;
11826 }
11827
11828 /* Determine the "ultimate origin" of a block. */
11829
11830 tree
11831 block_ultimate_origin (const_tree block)
11832 {
11833 tree origin = BLOCK_ABSTRACT_ORIGIN (block);
11834
11835 if (origin == NULL_TREE)
11836 return NULL_TREE;
11837 else
11838 {
11839 gcc_checking_assert ((DECL_P (origin)
11840 && DECL_ORIGIN (origin) == origin)
11841 || BLOCK_ORIGIN (origin) == origin);
11842 return origin;
11843 }
11844 }
11845
11846 /* Return true iff conversion from INNER_TYPE to OUTER_TYPE generates
11847 no instruction. */
11848
11849 bool
11850 tree_nop_conversion_p (const_tree outer_type, const_tree inner_type)
11851 {
11852 /* Do not strip casts into or out of differing address spaces. */
11853 if (POINTER_TYPE_P (outer_type)
11854 && TYPE_ADDR_SPACE (TREE_TYPE (outer_type)) != ADDR_SPACE_GENERIC)
11855 {
11856 if (!POINTER_TYPE_P (inner_type)
11857 || (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
11858 != TYPE_ADDR_SPACE (TREE_TYPE (inner_type))))
11859 return false;
11860 }
11861 else if (POINTER_TYPE_P (inner_type)
11862 && TYPE_ADDR_SPACE (TREE_TYPE (inner_type)) != ADDR_SPACE_GENERIC)
11863 {
11864 /* We already know that outer_type is not a pointer with
11865 a non-generic address space. */
11866 return false;
11867 }
11868
11869 /* Use precision rather then machine mode when we can, which gives
11870 the correct answer even for submode (bit-field) types. */
11871 if ((INTEGRAL_TYPE_P (outer_type)
11872 || POINTER_TYPE_P (outer_type)
11873 || TREE_CODE (outer_type) == OFFSET_TYPE)
11874 && (INTEGRAL_TYPE_P (inner_type)
11875 || POINTER_TYPE_P (inner_type)
11876 || TREE_CODE (inner_type) == OFFSET_TYPE))
11877 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11878
11879 /* Otherwise fall back on comparing machine modes (e.g. for
11880 aggregate types, floats). */
11881 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11882 }
11883
11884 /* Return true iff conversion in EXP generates no instruction. Mark
11885 it inline so that we fully inline into the stripping functions even
11886 though we have two uses of this function. */
11887
11888 static inline bool
11889 tree_nop_conversion (const_tree exp)
11890 {
11891 tree outer_type, inner_type;
11892
11893 if (location_wrapper_p (exp))
11894 return true;
11895 if (!CONVERT_EXPR_P (exp)
11896 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11897 return false;
11898
11899 outer_type = TREE_TYPE (exp);
11900 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11901 if (!inner_type || inner_type == error_mark_node)
11902 return false;
11903
11904 return tree_nop_conversion_p (outer_type, inner_type);
11905 }
11906
11907 /* Return true iff conversion in EXP generates no instruction. Don't
11908 consider conversions changing the signedness. */
11909
11910 static bool
11911 tree_sign_nop_conversion (const_tree exp)
11912 {
11913 tree outer_type, inner_type;
11914
11915 if (!tree_nop_conversion (exp))
11916 return false;
11917
11918 outer_type = TREE_TYPE (exp);
11919 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11920
11921 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11922 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11923 }
11924
11925 /* Strip conversions from EXP according to tree_nop_conversion and
11926 return the resulting expression. */
11927
11928 tree
11929 tree_strip_nop_conversions (tree exp)
11930 {
11931 while (tree_nop_conversion (exp))
11932 exp = TREE_OPERAND (exp, 0);
11933 return exp;
11934 }
11935
11936 /* Strip conversions from EXP according to tree_sign_nop_conversion
11937 and return the resulting expression. */
11938
11939 tree
11940 tree_strip_sign_nop_conversions (tree exp)
11941 {
11942 while (tree_sign_nop_conversion (exp))
11943 exp = TREE_OPERAND (exp, 0);
11944 return exp;
11945 }
11946
11947 /* Avoid any floating point extensions from EXP. */
11948 tree
11949 strip_float_extensions (tree exp)
11950 {
11951 tree sub, expt, subt;
11952
11953 /* For floating point constant look up the narrowest type that can hold
11954 it properly and handle it like (type)(narrowest_type)constant.
11955 This way we can optimize for instance a=a*2.0 where "a" is float
11956 but 2.0 is double constant. */
11957 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11958 {
11959 REAL_VALUE_TYPE orig;
11960 tree type = NULL;
11961
11962 orig = TREE_REAL_CST (exp);
11963 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11964 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11965 type = float_type_node;
11966 else if (TYPE_PRECISION (TREE_TYPE (exp))
11967 > TYPE_PRECISION (double_type_node)
11968 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11969 type = double_type_node;
11970 if (type)
11971 return build_real_truncate (type, orig);
11972 }
11973
11974 if (!CONVERT_EXPR_P (exp))
11975 return exp;
11976
11977 sub = TREE_OPERAND (exp, 0);
11978 subt = TREE_TYPE (sub);
11979 expt = TREE_TYPE (exp);
11980
11981 if (!FLOAT_TYPE_P (subt))
11982 return exp;
11983
11984 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11985 return exp;
11986
11987 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11988 return exp;
11989
11990 return strip_float_extensions (sub);
11991 }
11992
11993 /* Strip out all handled components that produce invariant
11994 offsets. */
11995
11996 const_tree
11997 strip_invariant_refs (const_tree op)
11998 {
11999 while (handled_component_p (op))
12000 {
12001 switch (TREE_CODE (op))
12002 {
12003 case ARRAY_REF:
12004 case ARRAY_RANGE_REF:
12005 if (!is_gimple_constant (TREE_OPERAND (op, 1))
12006 || TREE_OPERAND (op, 2) != NULL_TREE
12007 || TREE_OPERAND (op, 3) != NULL_TREE)
12008 return NULL;
12009 break;
12010
12011 case COMPONENT_REF:
12012 if (TREE_OPERAND (op, 2) != NULL_TREE)
12013 return NULL;
12014 break;
12015
12016 default:;
12017 }
12018 op = TREE_OPERAND (op, 0);
12019 }
12020
12021 return op;
12022 }
12023
12024 static GTY(()) tree gcc_eh_personality_decl;
12025
12026 /* Return the GCC personality function decl. */
12027
12028 tree
12029 lhd_gcc_personality (void)
12030 {
12031 if (!gcc_eh_personality_decl)
12032 gcc_eh_personality_decl = build_personality_function ("gcc");
12033 return gcc_eh_personality_decl;
12034 }
12035
12036 /* TARGET is a call target of GIMPLE call statement
12037 (obtained by gimple_call_fn). Return true if it is
12038 OBJ_TYPE_REF representing an virtual call of C++ method.
12039 (As opposed to OBJ_TYPE_REF representing objc calls
12040 through a cast where middle-end devirtualization machinery
12041 can't apply.) FOR_DUMP_P is true when being called from
12042 the dump routines. */
12043
12044 bool
12045 virtual_method_call_p (const_tree target, bool for_dump_p)
12046 {
12047 if (TREE_CODE (target) != OBJ_TYPE_REF)
12048 return false;
12049 tree t = TREE_TYPE (target);
12050 gcc_checking_assert (TREE_CODE (t) == POINTER_TYPE);
12051 t = TREE_TYPE (t);
12052 if (TREE_CODE (t) == FUNCTION_TYPE)
12053 return false;
12054 gcc_checking_assert (TREE_CODE (t) == METHOD_TYPE);
12055 /* If we do not have BINFO associated, it means that type was built
12056 without devirtualization enabled. Do not consider this a virtual
12057 call. */
12058 if (!TYPE_BINFO (obj_type_ref_class (target, for_dump_p)))
12059 return false;
12060 return true;
12061 }
12062
12063 /* Lookup sub-BINFO of BINFO of TYPE at offset POS. */
12064
12065 static tree
12066 lookup_binfo_at_offset (tree binfo, tree type, HOST_WIDE_INT pos)
12067 {
12068 unsigned int i;
12069 tree base_binfo, b;
12070
12071 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12072 if (pos == tree_to_shwi (BINFO_OFFSET (base_binfo))
12073 && types_same_for_odr (TREE_TYPE (base_binfo), type))
12074 return base_binfo;
12075 else if ((b = lookup_binfo_at_offset (base_binfo, type, pos)) != NULL)
12076 return b;
12077 return NULL;
12078 }
12079
12080 /* Try to find a base info of BINFO that would have its field decl at offset
12081 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
12082 found, return, otherwise return NULL_TREE. */
12083
12084 tree
12085 get_binfo_at_offset (tree binfo, poly_int64 offset, tree expected_type)
12086 {
12087 tree type = BINFO_TYPE (binfo);
12088
12089 while (true)
12090 {
12091 HOST_WIDE_INT pos, size;
12092 tree fld;
12093 int i;
12094
12095 if (types_same_for_odr (type, expected_type))
12096 return binfo;
12097 if (maybe_lt (offset, 0))
12098 return NULL_TREE;
12099
12100 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
12101 {
12102 if (TREE_CODE (fld) != FIELD_DECL || !DECL_ARTIFICIAL (fld))
12103 continue;
12104
12105 pos = int_bit_position (fld);
12106 size = tree_to_uhwi (DECL_SIZE (fld));
12107 if (known_in_range_p (offset, pos, size))
12108 break;
12109 }
12110 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
12111 return NULL_TREE;
12112
12113 /* Offset 0 indicates the primary base, whose vtable contents are
12114 represented in the binfo for the derived class. */
12115 else if (maybe_ne (offset, 0))
12116 {
12117 tree found_binfo = NULL, base_binfo;
12118 /* Offsets in BINFO are in bytes relative to the whole structure
12119 while POS is in bits relative to the containing field. */
12120 int binfo_offset = (tree_to_shwi (BINFO_OFFSET (binfo)) + pos
12121 / BITS_PER_UNIT);
12122
12123 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12124 if (tree_to_shwi (BINFO_OFFSET (base_binfo)) == binfo_offset
12125 && types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
12126 {
12127 found_binfo = base_binfo;
12128 break;
12129 }
12130 if (found_binfo)
12131 binfo = found_binfo;
12132 else
12133 binfo = lookup_binfo_at_offset (binfo, TREE_TYPE (fld),
12134 binfo_offset);
12135 }
12136
12137 type = TREE_TYPE (fld);
12138 offset -= pos;
12139 }
12140 }
12141
12142 /* Returns true if X is a typedef decl. */
12143
12144 bool
12145 is_typedef_decl (const_tree x)
12146 {
12147 return (x && TREE_CODE (x) == TYPE_DECL
12148 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
12149 }
12150
12151 /* Returns true iff TYPE is a type variant created for a typedef. */
12152
12153 bool
12154 typedef_variant_p (const_tree type)
12155 {
12156 return is_typedef_decl (TYPE_NAME (type));
12157 }
12158
12159 /* PR 84195: Replace control characters in "unescaped" with their
12160 escaped equivalents. Allow newlines if -fmessage-length has
12161 been set to a non-zero value. This is done here, rather than
12162 where the attribute is recorded as the message length can
12163 change between these two locations. */
12164
12165 void
12166 escaped_string::escape (const char *unescaped)
12167 {
12168 char *escaped;
12169 size_t i, new_i, len;
12170
12171 if (m_owned)
12172 free (m_str);
12173
12174 m_str = const_cast<char *> (unescaped);
12175 m_owned = false;
12176
12177 if (unescaped == NULL || *unescaped == 0)
12178 return;
12179
12180 len = strlen (unescaped);
12181 escaped = NULL;
12182 new_i = 0;
12183
12184 for (i = 0; i < len; i++)
12185 {
12186 char c = unescaped[i];
12187
12188 if (!ISCNTRL (c))
12189 {
12190 if (escaped)
12191 escaped[new_i++] = c;
12192 continue;
12193 }
12194
12195 if (c != '\n' || !pp_is_wrapping_line (global_dc->printer))
12196 {
12197 if (escaped == NULL)
12198 {
12199 /* We only allocate space for a new string if we
12200 actually encounter a control character that
12201 needs replacing. */
12202 escaped = (char *) xmalloc (len * 2 + 1);
12203 strncpy (escaped, unescaped, i);
12204 new_i = i;
12205 }
12206
12207 escaped[new_i++] = '\\';
12208
12209 switch (c)
12210 {
12211 case '\a': escaped[new_i++] = 'a'; break;
12212 case '\b': escaped[new_i++] = 'b'; break;
12213 case '\f': escaped[new_i++] = 'f'; break;
12214 case '\n': escaped[new_i++] = 'n'; break;
12215 case '\r': escaped[new_i++] = 'r'; break;
12216 case '\t': escaped[new_i++] = 't'; break;
12217 case '\v': escaped[new_i++] = 'v'; break;
12218 default: escaped[new_i++] = '?'; break;
12219 }
12220 }
12221 else if (escaped)
12222 escaped[new_i++] = c;
12223 }
12224
12225 if (escaped)
12226 {
12227 escaped[new_i] = 0;
12228 m_str = escaped;
12229 m_owned = true;
12230 }
12231 }
12232
12233 /* Warn about a use of an identifier which was marked deprecated. Returns
12234 whether a warning was given. */
12235
12236 bool
12237 warn_deprecated_use (tree node, tree attr)
12238 {
12239 escaped_string msg;
12240
12241 if (node == 0 || !warn_deprecated_decl)
12242 return false;
12243
12244 if (!attr)
12245 {
12246 if (DECL_P (node))
12247 attr = DECL_ATTRIBUTES (node);
12248 else if (TYPE_P (node))
12249 {
12250 tree decl = TYPE_STUB_DECL (node);
12251 if (decl)
12252 attr = TYPE_ATTRIBUTES (TREE_TYPE (decl));
12253 else if ((decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (node)))
12254 != NULL_TREE)
12255 {
12256 node = TREE_TYPE (decl);
12257 attr = TYPE_ATTRIBUTES (node);
12258 }
12259 }
12260 }
12261
12262 if (attr)
12263 attr = lookup_attribute ("deprecated", attr);
12264
12265 if (attr)
12266 msg.escape (TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
12267
12268 bool w = false;
12269 if (DECL_P (node))
12270 {
12271 auto_diagnostic_group d;
12272 if (msg)
12273 w = warning (OPT_Wdeprecated_declarations,
12274 "%qD is deprecated: %s", node, (const char *) msg);
12275 else
12276 w = warning (OPT_Wdeprecated_declarations,
12277 "%qD is deprecated", node);
12278 if (w)
12279 inform (DECL_SOURCE_LOCATION (node), "declared here");
12280 }
12281 else if (TYPE_P (node))
12282 {
12283 tree what = NULL_TREE;
12284 tree decl = TYPE_STUB_DECL (node);
12285
12286 if (TYPE_NAME (node))
12287 {
12288 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12289 what = TYPE_NAME (node);
12290 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12291 && DECL_NAME (TYPE_NAME (node)))
12292 what = DECL_NAME (TYPE_NAME (node));
12293 }
12294
12295 auto_diagnostic_group d;
12296 if (what)
12297 {
12298 if (msg)
12299 w = warning (OPT_Wdeprecated_declarations,
12300 "%qE is deprecated: %s", what, (const char *) msg);
12301 else
12302 w = warning (OPT_Wdeprecated_declarations,
12303 "%qE is deprecated", what);
12304 }
12305 else
12306 {
12307 if (msg)
12308 w = warning (OPT_Wdeprecated_declarations,
12309 "type is deprecated: %s", (const char *) msg);
12310 else
12311 w = warning (OPT_Wdeprecated_declarations,
12312 "type is deprecated");
12313 }
12314
12315 if (w && decl)
12316 inform (DECL_SOURCE_LOCATION (decl), "declared here");
12317 }
12318
12319 return w;
12320 }
12321
12322 /* Error out with an identifier which was marked 'unavailable'. */
12323 void
12324 error_unavailable_use (tree node, tree attr)
12325 {
12326 escaped_string msg;
12327
12328 if (node == 0)
12329 return;
12330
12331 if (!attr)
12332 {
12333 if (DECL_P (node))
12334 attr = DECL_ATTRIBUTES (node);
12335 else if (TYPE_P (node))
12336 {
12337 tree decl = TYPE_STUB_DECL (node);
12338 if (decl)
12339 attr = lookup_attribute ("unavailable",
12340 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
12341 }
12342 }
12343
12344 if (attr)
12345 attr = lookup_attribute ("unavailable", attr);
12346
12347 if (attr)
12348 msg.escape (TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
12349
12350 if (DECL_P (node))
12351 {
12352 auto_diagnostic_group d;
12353 if (msg)
12354 error ("%qD is unavailable: %s", node, (const char *) msg);
12355 else
12356 error ("%qD is unavailable", node);
12357 inform (DECL_SOURCE_LOCATION (node), "declared here");
12358 }
12359 else if (TYPE_P (node))
12360 {
12361 tree what = NULL_TREE;
12362 tree decl = TYPE_STUB_DECL (node);
12363
12364 if (TYPE_NAME (node))
12365 {
12366 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12367 what = TYPE_NAME (node);
12368 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12369 && DECL_NAME (TYPE_NAME (node)))
12370 what = DECL_NAME (TYPE_NAME (node));
12371 }
12372
12373 auto_diagnostic_group d;
12374 if (what)
12375 {
12376 if (msg)
12377 error ("%qE is unavailable: %s", what, (const char *) msg);
12378 else
12379 error ("%qE is unavailable", what);
12380 }
12381 else
12382 {
12383 if (msg)
12384 error ("type is unavailable: %s", (const char *) msg);
12385 else
12386 error ("type is unavailable");
12387 }
12388
12389 if (decl)
12390 inform (DECL_SOURCE_LOCATION (decl), "declared here");
12391 }
12392 }
12393
12394 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12395 somewhere in it. */
12396
12397 bool
12398 contains_bitfld_component_ref_p (const_tree ref)
12399 {
12400 while (handled_component_p (ref))
12401 {
12402 if (TREE_CODE (ref) == COMPONENT_REF
12403 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12404 return true;
12405 ref = TREE_OPERAND (ref, 0);
12406 }
12407
12408 return false;
12409 }
12410
12411 /* Try to determine whether a TRY_CATCH expression can fall through.
12412 This is a subroutine of block_may_fallthru. */
12413
12414 static bool
12415 try_catch_may_fallthru (const_tree stmt)
12416 {
12417 tree_stmt_iterator i;
12418
12419 /* If the TRY block can fall through, the whole TRY_CATCH can
12420 fall through. */
12421 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
12422 return true;
12423
12424 i = tsi_start (TREE_OPERAND (stmt, 1));
12425 switch (TREE_CODE (tsi_stmt (i)))
12426 {
12427 case CATCH_EXPR:
12428 /* We expect to see a sequence of CATCH_EXPR trees, each with a
12429 catch expression and a body. The whole TRY_CATCH may fall
12430 through iff any of the catch bodies falls through. */
12431 for (; !tsi_end_p (i); tsi_next (&i))
12432 {
12433 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
12434 return true;
12435 }
12436 return false;
12437
12438 case EH_FILTER_EXPR:
12439 /* The exception filter expression only matters if there is an
12440 exception. If the exception does not match EH_FILTER_TYPES,
12441 we will execute EH_FILTER_FAILURE, and we will fall through
12442 if that falls through. If the exception does match
12443 EH_FILTER_TYPES, the stack unwinder will continue up the
12444 stack, so we will not fall through. We don't know whether we
12445 will throw an exception which matches EH_FILTER_TYPES or not,
12446 so we just ignore EH_FILTER_TYPES and assume that we might
12447 throw an exception which doesn't match. */
12448 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
12449
12450 default:
12451 /* This case represents statements to be executed when an
12452 exception occurs. Those statements are implicitly followed
12453 by a RESX statement to resume execution after the exception.
12454 So in this case the TRY_CATCH never falls through. */
12455 return false;
12456 }
12457 }
12458
12459 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
12460 need not be 100% accurate; simply be conservative and return true if we
12461 don't know. This is used only to avoid stupidly generating extra code.
12462 If we're wrong, we'll just delete the extra code later. */
12463
12464 bool
12465 block_may_fallthru (const_tree block)
12466 {
12467 /* This CONST_CAST is okay because expr_last returns its argument
12468 unmodified and we assign it to a const_tree. */
12469 const_tree stmt = expr_last (CONST_CAST_TREE (block));
12470
12471 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
12472 {
12473 case GOTO_EXPR:
12474 case RETURN_EXPR:
12475 /* Easy cases. If the last statement of the block implies
12476 control transfer, then we can't fall through. */
12477 return false;
12478
12479 case SWITCH_EXPR:
12480 /* If there is a default: label or case labels cover all possible
12481 SWITCH_COND values, then the SWITCH_EXPR will transfer control
12482 to some case label in all cases and all we care is whether the
12483 SWITCH_BODY falls through. */
12484 if (SWITCH_ALL_CASES_P (stmt))
12485 return block_may_fallthru (SWITCH_BODY (stmt));
12486 return true;
12487
12488 case COND_EXPR:
12489 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
12490 return true;
12491 return block_may_fallthru (COND_EXPR_ELSE (stmt));
12492
12493 case BIND_EXPR:
12494 return block_may_fallthru (BIND_EXPR_BODY (stmt));
12495
12496 case TRY_CATCH_EXPR:
12497 return try_catch_may_fallthru (stmt);
12498
12499 case TRY_FINALLY_EXPR:
12500 /* The finally clause is always executed after the try clause,
12501 so if it does not fall through, then the try-finally will not
12502 fall through. Otherwise, if the try clause does not fall
12503 through, then when the finally clause falls through it will
12504 resume execution wherever the try clause was going. So the
12505 whole try-finally will only fall through if both the try
12506 clause and the finally clause fall through. */
12507 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
12508 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
12509
12510 case EH_ELSE_EXPR:
12511 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12512
12513 case MODIFY_EXPR:
12514 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
12515 stmt = TREE_OPERAND (stmt, 1);
12516 else
12517 return true;
12518 /* FALLTHRU */
12519
12520 case CALL_EXPR:
12521 /* Functions that do not return do not fall through. */
12522 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
12523
12524 case CLEANUP_POINT_EXPR:
12525 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12526
12527 case TARGET_EXPR:
12528 return block_may_fallthru (TREE_OPERAND (stmt, 1));
12529
12530 case ERROR_MARK:
12531 return true;
12532
12533 default:
12534 return lang_hooks.block_may_fallthru (stmt);
12535 }
12536 }
12537
12538 /* True if we are using EH to handle cleanups. */
12539 static bool using_eh_for_cleanups_flag = false;
12540
12541 /* This routine is called from front ends to indicate eh should be used for
12542 cleanups. */
12543 void
12544 using_eh_for_cleanups (void)
12545 {
12546 using_eh_for_cleanups_flag = true;
12547 }
12548
12549 /* Query whether EH is used for cleanups. */
12550 bool
12551 using_eh_for_cleanups_p (void)
12552 {
12553 return using_eh_for_cleanups_flag;
12554 }
12555
12556 /* Wrapper for tree_code_name to ensure that tree code is valid */
12557 const char *
12558 get_tree_code_name (enum tree_code code)
12559 {
12560 const char *invalid = "<invalid tree code>";
12561
12562 /* The tree_code enum promotes to signed, but we could be getting
12563 invalid values, so force an unsigned comparison. */
12564 if (unsigned (code) >= MAX_TREE_CODES)
12565 {
12566 if ((unsigned)code == 0xa5a5)
12567 return "ggc_freed";
12568 return invalid;
12569 }
12570
12571 return tree_code_name[code];
12572 }
12573
12574 /* Drops the TREE_OVERFLOW flag from T. */
12575
12576 tree
12577 drop_tree_overflow (tree t)
12578 {
12579 gcc_checking_assert (TREE_OVERFLOW (t));
12580
12581 /* For tree codes with a sharing machinery re-build the result. */
12582 if (poly_int_tree_p (t))
12583 return wide_int_to_tree (TREE_TYPE (t), wi::to_poly_wide (t));
12584
12585 /* For VECTOR_CST, remove the overflow bits from the encoded elements
12586 and canonicalize the result. */
12587 if (TREE_CODE (t) == VECTOR_CST)
12588 {
12589 tree_vector_builder builder;
12590 builder.new_unary_operation (TREE_TYPE (t), t, true);
12591 unsigned int count = builder.encoded_nelts ();
12592 for (unsigned int i = 0; i < count; ++i)
12593 {
12594 tree elt = VECTOR_CST_ELT (t, i);
12595 if (TREE_OVERFLOW (elt))
12596 elt = drop_tree_overflow (elt);
12597 builder.quick_push (elt);
12598 }
12599 return builder.build ();
12600 }
12601
12602 /* Otherwise, as all tcc_constants are possibly shared, copy the node
12603 and drop the flag. */
12604 t = copy_node (t);
12605 TREE_OVERFLOW (t) = 0;
12606
12607 /* For constants that contain nested constants, drop the flag
12608 from those as well. */
12609 if (TREE_CODE (t) == COMPLEX_CST)
12610 {
12611 if (TREE_OVERFLOW (TREE_REALPART (t)))
12612 TREE_REALPART (t) = drop_tree_overflow (TREE_REALPART (t));
12613 if (TREE_OVERFLOW (TREE_IMAGPART (t)))
12614 TREE_IMAGPART (t) = drop_tree_overflow (TREE_IMAGPART (t));
12615 }
12616
12617 return t;
12618 }
12619
12620 /* Given a memory reference expression T, return its base address.
12621 The base address of a memory reference expression is the main
12622 object being referenced. For instance, the base address for
12623 'array[i].fld[j]' is 'array'. You can think of this as stripping
12624 away the offset part from a memory address.
12625
12626 This function calls handled_component_p to strip away all the inner
12627 parts of the memory reference until it reaches the base object. */
12628
12629 tree
12630 get_base_address (tree t)
12631 {
12632 if (TREE_CODE (t) == WITH_SIZE_EXPR)
12633 t = TREE_OPERAND (t, 0);
12634 while (handled_component_p (t))
12635 t = TREE_OPERAND (t, 0);
12636
12637 if ((TREE_CODE (t) == MEM_REF
12638 || TREE_CODE (t) == TARGET_MEM_REF)
12639 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
12640 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
12641
12642 return t;
12643 }
12644
12645 /* Return a tree of sizetype representing the size, in bytes, of the element
12646 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12647
12648 tree
12649 array_ref_element_size (tree exp)
12650 {
12651 tree aligned_size = TREE_OPERAND (exp, 3);
12652 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
12653 location_t loc = EXPR_LOCATION (exp);
12654
12655 /* If a size was specified in the ARRAY_REF, it's the size measured
12656 in alignment units of the element type. So multiply by that value. */
12657 if (aligned_size)
12658 {
12659 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
12660 sizetype from another type of the same width and signedness. */
12661 if (TREE_TYPE (aligned_size) != sizetype)
12662 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
12663 return size_binop_loc (loc, MULT_EXPR, aligned_size,
12664 size_int (TYPE_ALIGN_UNIT (elmt_type)));
12665 }
12666
12667 /* Otherwise, take the size from that of the element type. Substitute
12668 any PLACEHOLDER_EXPR that we have. */
12669 else
12670 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
12671 }
12672
12673 /* Return a tree representing the lower bound of the array mentioned in
12674 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12675
12676 tree
12677 array_ref_low_bound (tree exp)
12678 {
12679 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12680
12681 /* If a lower bound is specified in EXP, use it. */
12682 if (TREE_OPERAND (exp, 2))
12683 return TREE_OPERAND (exp, 2);
12684
12685 /* Otherwise, if there is a domain type and it has a lower bound, use it,
12686 substituting for a PLACEHOLDER_EXPR as needed. */
12687 if (domain_type && TYPE_MIN_VALUE (domain_type))
12688 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
12689
12690 /* Otherwise, return a zero of the appropriate type. */
12691 tree idxtype = TREE_TYPE (TREE_OPERAND (exp, 1));
12692 return (idxtype == error_mark_node
12693 ? integer_zero_node : build_int_cst (idxtype, 0));
12694 }
12695
12696 /* Return a tree representing the upper bound of the array mentioned in
12697 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12698
12699 tree
12700 array_ref_up_bound (tree exp)
12701 {
12702 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12703
12704 /* If there is a domain type and it has an upper bound, use it, substituting
12705 for a PLACEHOLDER_EXPR as needed. */
12706 if (domain_type && TYPE_MAX_VALUE (domain_type))
12707 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
12708
12709 /* Otherwise fail. */
12710 return NULL_TREE;
12711 }
12712
12713 /* Returns true if REF is an array reference, component reference,
12714 or memory reference to an array whose actual size might be larger
12715 than its upper bound implies, there are multiple cases:
12716 A. a ref to a flexible array member at the end of a structure;
12717 B. a ref to an array with a different type against the original decl;
12718 for example:
12719
12720 short a[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
12721 (*((char(*)[16])&a[0]))[i+8]
12722
12723 C. a ref to an array that was passed as a parameter;
12724 for example:
12725
12726 int test (uint8_t *p, uint32_t t[1][1], int n) {
12727 for (int i = 0; i < 4; i++, p++)
12728 t[i][0] = ...;
12729
12730 FIXME, the name of this routine need to be changed to be more accurate. */
12731 bool
12732 array_at_struct_end_p (tree ref)
12733 {
12734 /* the TYPE for this array referece. */
12735 tree atype = NULL_TREE;
12736 /* the FIELD_DECL for the array field in the containing structure. */
12737 tree afield_decl = NULL_TREE;
12738
12739 if (TREE_CODE (ref) == ARRAY_REF
12740 || TREE_CODE (ref) == ARRAY_RANGE_REF)
12741 {
12742 atype = TREE_TYPE (TREE_OPERAND (ref, 0));
12743 ref = TREE_OPERAND (ref, 0);
12744 }
12745 else if (TREE_CODE (ref) == COMPONENT_REF
12746 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 1))) == ARRAY_TYPE)
12747 {
12748 atype = TREE_TYPE (TREE_OPERAND (ref, 1));
12749 afield_decl = TREE_OPERAND (ref, 1);
12750 }
12751 else if (TREE_CODE (ref) == MEM_REF)
12752 {
12753 tree arg = TREE_OPERAND (ref, 0);
12754 if (TREE_CODE (arg) == ADDR_EXPR)
12755 arg = TREE_OPERAND (arg, 0);
12756 tree argtype = TREE_TYPE (arg);
12757 if (TREE_CODE (argtype) == RECORD_TYPE)
12758 {
12759 if (tree fld = last_field (argtype))
12760 {
12761 atype = TREE_TYPE (fld);
12762 afield_decl = fld;
12763 if (TREE_CODE (atype) != ARRAY_TYPE)
12764 return false;
12765 if (VAR_P (arg) && DECL_SIZE (fld))
12766 return false;
12767 }
12768 else
12769 return false;
12770 }
12771 else
12772 return false;
12773 }
12774 else
12775 return false;
12776
12777 if (TREE_CODE (ref) == STRING_CST)
12778 return false;
12779
12780 tree ref_to_array = ref;
12781 while (handled_component_p (ref))
12782 {
12783 /* If the reference chain contains a component reference to a
12784 non-union type and there follows another field the reference
12785 is not at the end of a structure. */
12786 if (TREE_CODE (ref) == COMPONENT_REF)
12787 {
12788 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
12789 {
12790 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
12791 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
12792 nextf = DECL_CHAIN (nextf);
12793 if (nextf)
12794 return false;
12795 }
12796 }
12797 /* If we have a multi-dimensional array we do not consider
12798 a non-innermost dimension as flex array if the whole
12799 multi-dimensional array is at struct end.
12800 Same for an array of aggregates with a trailing array
12801 member. */
12802 else if (TREE_CODE (ref) == ARRAY_REF)
12803 return false;
12804 else if (TREE_CODE (ref) == ARRAY_RANGE_REF)
12805 ;
12806 /* If we view an underlying object as sth else then what we
12807 gathered up to now is what we have to rely on. */
12808 else if (TREE_CODE (ref) == VIEW_CONVERT_EXPR)
12809 break;
12810 else
12811 gcc_unreachable ();
12812
12813 ref = TREE_OPERAND (ref, 0);
12814 }
12815
12816 gcc_assert (!afield_decl
12817 || (afield_decl && TREE_CODE (afield_decl) == FIELD_DECL));
12818
12819 /* The array now is at struct end. Treat flexible array member as
12820 always subject to extend, even into just padding constrained by
12821 an underlying decl. */
12822 if (! TYPE_SIZE (atype)
12823 || ! TYPE_DOMAIN (atype)
12824 || ! TYPE_MAX_VALUE (TYPE_DOMAIN (atype)))
12825 return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
12826
12827 /* If the reference is based on a declared entity, the size of the array
12828 is constrained by its given domain. (Do not trust commons PR/69368). */
12829 ref = get_base_address (ref);
12830 if (ref
12831 && DECL_P (ref)
12832 && !(flag_unconstrained_commons
12833 && VAR_P (ref) && DECL_COMMON (ref))
12834 && DECL_SIZE_UNIT (ref)
12835 && TREE_CODE (DECL_SIZE_UNIT (ref)) == INTEGER_CST)
12836 {
12837 /* If the object itself is the array it is not at struct end. */
12838 if (DECL_P (ref_to_array))
12839 return false;
12840
12841 /* Check whether the array domain covers all of the available
12842 padding. */
12843 poly_int64 offset;
12844 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (atype))) != INTEGER_CST
12845 || TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))) != INTEGER_CST
12846 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (atype))) != INTEGER_CST)
12847 return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
12848 if (! get_addr_base_and_unit_offset (ref_to_array, &offset))
12849 return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
12850
12851 /* If at least one extra element fits it is a flexarray. */
12852 if (known_le ((wi::to_offset (TYPE_MAX_VALUE (TYPE_DOMAIN (atype)))
12853 - wi::to_offset (TYPE_MIN_VALUE (TYPE_DOMAIN (atype)))
12854 + 2)
12855 * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (atype))),
12856 wi::to_offset (DECL_SIZE_UNIT (ref)) - offset))
12857 return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
12858
12859 return false;
12860 }
12861
12862 return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
12863 }
12864
12865 /* Return a tree representing the offset, in bytes, of the field referenced
12866 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
12867
12868 tree
12869 component_ref_field_offset (tree exp)
12870 {
12871 tree aligned_offset = TREE_OPERAND (exp, 2);
12872 tree field = TREE_OPERAND (exp, 1);
12873 location_t loc = EXPR_LOCATION (exp);
12874
12875 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
12876 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
12877 value. */
12878 if (aligned_offset)
12879 {
12880 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
12881 sizetype from another type of the same width and signedness. */
12882 if (TREE_TYPE (aligned_offset) != sizetype)
12883 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
12884 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
12885 size_int (DECL_OFFSET_ALIGN (field)
12886 / BITS_PER_UNIT));
12887 }
12888
12889 /* Otherwise, take the offset from that of the field. Substitute
12890 any PLACEHOLDER_EXPR that we have. */
12891 else
12892 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
12893 }
12894
12895 /* Given the initializer INIT, return the initializer for the field
12896 DECL if it exists, otherwise null. Used to obtain the initializer
12897 for a flexible array member and determine its size. */
12898
12899 static tree
12900 get_initializer_for (tree init, tree decl)
12901 {
12902 STRIP_NOPS (init);
12903
12904 tree fld, fld_init;
12905 unsigned HOST_WIDE_INT i;
12906 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), i, fld, fld_init)
12907 {
12908 if (decl == fld)
12909 return fld_init;
12910
12911 if (TREE_CODE (fld) == CONSTRUCTOR)
12912 {
12913 fld_init = get_initializer_for (fld_init, decl);
12914 if (fld_init)
12915 return fld_init;
12916 }
12917 }
12918
12919 return NULL_TREE;
12920 }
12921
12922 /* Determines the size of the member referenced by the COMPONENT_REF
12923 REF, using its initializer expression if necessary in order to
12924 determine the size of an initialized flexible array member.
12925 If non-null, set *ARK when REF refers to an interior zero-length
12926 array or a trailing one-element array.
12927 Returns the size as sizetype (which might be zero for an object
12928 with an uninitialized flexible array member) or null if the size
12929 cannot be determined. */
12930
12931 tree
12932 component_ref_size (tree ref, special_array_member *sam /* = NULL */)
12933 {
12934 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
12935
12936 special_array_member sambuf;
12937 if (!sam)
12938 sam = &sambuf;
12939 *sam = special_array_member::none;
12940
12941 /* The object/argument referenced by the COMPONENT_REF and its type. */
12942 tree arg = TREE_OPERAND (ref, 0);
12943 tree argtype = TREE_TYPE (arg);
12944 /* The referenced member. */
12945 tree member = TREE_OPERAND (ref, 1);
12946
12947 tree memsize = DECL_SIZE_UNIT (member);
12948 if (memsize)
12949 {
12950 tree memtype = TREE_TYPE (member);
12951 if (TREE_CODE (memtype) != ARRAY_TYPE)
12952 /* DECL_SIZE may be less than TYPE_SIZE in C++ when referring
12953 to the type of a class with a virtual base which doesn't
12954 reflect the size of the virtual's members (see pr97595).
12955 If that's the case fail for now and implement something
12956 more robust in the future. */
12957 return (tree_int_cst_equal (memsize, TYPE_SIZE_UNIT (memtype))
12958 ? memsize : NULL_TREE);
12959
12960 bool trailing = array_at_struct_end_p (ref);
12961 bool zero_length = integer_zerop (memsize);
12962 if (!trailing && !zero_length)
12963 /* MEMBER is either an interior array or is an array with
12964 more than one element. */
12965 return memsize;
12966
12967 if (zero_length)
12968 {
12969 if (trailing)
12970 *sam = special_array_member::trail_0;
12971 else
12972 {
12973 *sam = special_array_member::int_0;
12974 memsize = NULL_TREE;
12975 }
12976 }
12977
12978 if (!zero_length)
12979 if (tree dom = TYPE_DOMAIN (memtype))
12980 if (tree min = TYPE_MIN_VALUE (dom))
12981 if (tree max = TYPE_MAX_VALUE (dom))
12982 if (TREE_CODE (min) == INTEGER_CST
12983 && TREE_CODE (max) == INTEGER_CST)
12984 {
12985 offset_int minidx = wi::to_offset (min);
12986 offset_int maxidx = wi::to_offset (max);
12987 offset_int neltsm1 = maxidx - minidx;
12988 if (neltsm1 > 0)
12989 /* MEMBER is an array with more than one element. */
12990 return memsize;
12991
12992 if (neltsm1 == 0)
12993 *sam = special_array_member::trail_1;
12994 }
12995
12996 /* For a reference to a zero- or one-element array member of a union
12997 use the size of the union instead of the size of the member. */
12998 if (TREE_CODE (argtype) == UNION_TYPE)
12999 memsize = TYPE_SIZE_UNIT (argtype);
13000 }
13001
13002 /* MEMBER is either a bona fide flexible array member, or a zero-length
13003 array member, or an array of length one treated as such. */
13004
13005 /* If the reference is to a declared object and the member a true
13006 flexible array, try to determine its size from its initializer. */
13007 poly_int64 baseoff = 0;
13008 tree base = get_addr_base_and_unit_offset (ref, &baseoff);
13009 if (!base || !VAR_P (base))
13010 {
13011 if (*sam != special_array_member::int_0)
13012 return NULL_TREE;
13013
13014 if (TREE_CODE (arg) != COMPONENT_REF)
13015 return NULL_TREE;
13016
13017 base = arg;
13018 while (TREE_CODE (base) == COMPONENT_REF)
13019 base = TREE_OPERAND (base, 0);
13020 baseoff = tree_to_poly_int64 (byte_position (TREE_OPERAND (ref, 1)));
13021 }
13022
13023 /* BASE is the declared object of which MEMBER is either a member
13024 or that is cast to ARGTYPE (e.g., a char buffer used to store
13025 an ARGTYPE object). */
13026 tree basetype = TREE_TYPE (base);
13027
13028 /* Determine the base type of the referenced object. If it's
13029 the same as ARGTYPE and MEMBER has a known size, return it. */
13030 tree bt = basetype;
13031 if (*sam != special_array_member::int_0)
13032 while (TREE_CODE (bt) == ARRAY_TYPE)
13033 bt = TREE_TYPE (bt);
13034 bool typematch = useless_type_conversion_p (argtype, bt);
13035 if (memsize && typematch)
13036 return memsize;
13037
13038 memsize = NULL_TREE;
13039
13040 if (typematch)
13041 /* MEMBER is a true flexible array member. Compute its size from
13042 the initializer of the BASE object if it has one. */
13043 if (tree init = DECL_P (base) ? DECL_INITIAL (base) : NULL_TREE)
13044 if (init != error_mark_node)
13045 {
13046 init = get_initializer_for (init, member);
13047 if (init)
13048 {
13049 memsize = TYPE_SIZE_UNIT (TREE_TYPE (init));
13050 if (tree refsize = TYPE_SIZE_UNIT (argtype))
13051 {
13052 /* Use the larger of the initializer size and the tail
13053 padding in the enclosing struct. */
13054 poly_int64 rsz = tree_to_poly_int64 (refsize);
13055 rsz -= baseoff;
13056 if (known_lt (tree_to_poly_int64 (memsize), rsz))
13057 memsize = wide_int_to_tree (TREE_TYPE (memsize), rsz);
13058 }
13059
13060 baseoff = 0;
13061 }
13062 }
13063
13064 if (!memsize)
13065 {
13066 if (typematch)
13067 {
13068 if (DECL_P (base)
13069 && DECL_EXTERNAL (base)
13070 && bt == basetype
13071 && *sam != special_array_member::int_0)
13072 /* The size of a flexible array member of an extern struct
13073 with no initializer cannot be determined (it's defined
13074 in another translation unit and can have an initializer
13075 with an arbitrary number of elements). */
13076 return NULL_TREE;
13077
13078 /* Use the size of the base struct or, for interior zero-length
13079 arrays, the size of the enclosing type. */
13080 memsize = TYPE_SIZE_UNIT (bt);
13081 }
13082 else if (DECL_P (base))
13083 /* Use the size of the BASE object (possibly an array of some
13084 other type such as char used to store the struct). */
13085 memsize = DECL_SIZE_UNIT (base);
13086 else
13087 return NULL_TREE;
13088 }
13089
13090 /* If the flexible array member has a known size use the greater
13091 of it and the tail padding in the enclosing struct.
13092 Otherwise, when the size of the flexible array member is unknown
13093 and the referenced object is not a struct, use the size of its
13094 type when known. This detects sizes of array buffers when cast
13095 to struct types with flexible array members. */
13096 if (memsize)
13097 {
13098 if (!tree_fits_poly_int64_p (memsize))
13099 return NULL_TREE;
13100 poly_int64 memsz64 = memsize ? tree_to_poly_int64 (memsize) : 0;
13101 if (known_lt (baseoff, memsz64))
13102 {
13103 memsz64 -= baseoff;
13104 return wide_int_to_tree (TREE_TYPE (memsize), memsz64);
13105 }
13106 return size_zero_node;
13107 }
13108
13109 /* Return "don't know" for an external non-array object since its
13110 flexible array member can be initialized to have any number of
13111 elements. Otherwise, return zero because the flexible array
13112 member has no elements. */
13113 return (DECL_P (base)
13114 && DECL_EXTERNAL (base)
13115 && (!typematch
13116 || TREE_CODE (basetype) != ARRAY_TYPE)
13117 ? NULL_TREE : size_zero_node);
13118 }
13119
13120 /* Return the machine mode of T. For vectors, returns the mode of the
13121 inner type. The main use case is to feed the result to HONOR_NANS,
13122 avoiding the BLKmode that a direct TYPE_MODE (T) might return. */
13123
13124 machine_mode
13125 element_mode (const_tree t)
13126 {
13127 if (!TYPE_P (t))
13128 t = TREE_TYPE (t);
13129 if (VECTOR_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
13130 t = TREE_TYPE (t);
13131 return TYPE_MODE (t);
13132 }
13133
13134 /* Vector types need to re-check the target flags each time we report
13135 the machine mode. We need to do this because attribute target can
13136 change the result of vector_mode_supported_p and have_regs_of_mode
13137 on a per-function basis. Thus the TYPE_MODE of a VECTOR_TYPE can
13138 change on a per-function basis. */
13139 /* ??? Possibly a better solution is to run through all the types
13140 referenced by a function and re-compute the TYPE_MODE once, rather
13141 than make the TYPE_MODE macro call a function. */
13142
13143 machine_mode
13144 vector_type_mode (const_tree t)
13145 {
13146 machine_mode mode;
13147
13148 gcc_assert (TREE_CODE (t) == VECTOR_TYPE);
13149
13150 mode = t->type_common.mode;
13151 if (VECTOR_MODE_P (mode)
13152 && (!targetm.vector_mode_supported_p (mode)
13153 || !have_regs_of_mode[mode]))
13154 {
13155 scalar_int_mode innermode;
13156
13157 /* For integers, try mapping it to a same-sized scalar mode. */
13158 if (is_int_mode (TREE_TYPE (t)->type_common.mode, &innermode))
13159 {
13160 poly_int64 size = (TYPE_VECTOR_SUBPARTS (t)
13161 * GET_MODE_BITSIZE (innermode));
13162 scalar_int_mode mode;
13163 if (int_mode_for_size (size, 0).exists (&mode)
13164 && have_regs_of_mode[mode])
13165 return mode;
13166 }
13167
13168 return BLKmode;
13169 }
13170
13171 return mode;
13172 }
13173
13174 /* Return the size in bits of each element of vector type TYPE. */
13175
13176 unsigned int
13177 vector_element_bits (const_tree type)
13178 {
13179 gcc_checking_assert (VECTOR_TYPE_P (type));
13180 if (VECTOR_BOOLEAN_TYPE_P (type))
13181 return TYPE_PRECISION (TREE_TYPE (type));
13182 return tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)));
13183 }
13184
13185 /* Calculate the size in bits of each element of vector type TYPE
13186 and return the result as a tree of type bitsizetype. */
13187
13188 tree
13189 vector_element_bits_tree (const_tree type)
13190 {
13191 gcc_checking_assert (VECTOR_TYPE_P (type));
13192 if (VECTOR_BOOLEAN_TYPE_P (type))
13193 return bitsize_int (vector_element_bits (type));
13194 return TYPE_SIZE (TREE_TYPE (type));
13195 }
13196
13197 /* Verify that basic properties of T match TV and thus T can be a variant of
13198 TV. TV should be the more specified variant (i.e. the main variant). */
13199
13200 static bool
13201 verify_type_variant (const_tree t, tree tv)
13202 {
13203 /* Type variant can differ by:
13204
13205 - TYPE_QUALS: TYPE_READONLY, TYPE_VOLATILE, TYPE_ATOMIC, TYPE_RESTRICT,
13206 ENCODE_QUAL_ADDR_SPACE.
13207 - main variant may be TYPE_COMPLETE_P and variant types !TYPE_COMPLETE_P
13208 in this case some values may not be set in the variant types
13209 (see TYPE_COMPLETE_P checks).
13210 - it is possible to have TYPE_ARTIFICIAL variant of non-artifical type
13211 - by TYPE_NAME and attributes (i.e. when variant originate by typedef)
13212 - TYPE_CANONICAL (TYPE_ALIAS_SET is the same among variants)
13213 - by the alignment: TYPE_ALIGN and TYPE_USER_ALIGN
13214 - during LTO by TYPE_CONTEXT if type is TYPE_FILE_SCOPE_P
13215 this is necessary to make it possible to merge types form different TUs
13216 - arrays, pointers and references may have TREE_TYPE that is a variant
13217 of TREE_TYPE of their main variants.
13218 - aggregates may have new TYPE_FIELDS list that list variants of
13219 the main variant TYPE_FIELDS.
13220 - vector types may differ by TYPE_VECTOR_OPAQUE
13221 */
13222
13223 /* Convenience macro for matching individual fields. */
13224 #define verify_variant_match(flag) \
13225 do { \
13226 if (flag (tv) != flag (t)) \
13227 { \
13228 error ("type variant differs by %s", #flag); \
13229 debug_tree (tv); \
13230 return false; \
13231 } \
13232 } while (false)
13233
13234 /* tree_base checks. */
13235
13236 verify_variant_match (TREE_CODE);
13237 /* FIXME: Ada builds non-artificial variants of artificial types. */
13238 #if 0
13239 if (TYPE_ARTIFICIAL (tv))
13240 verify_variant_match (TYPE_ARTIFICIAL);
13241 #endif
13242 if (POINTER_TYPE_P (tv))
13243 verify_variant_match (TYPE_REF_CAN_ALIAS_ALL);
13244 /* FIXME: TYPE_SIZES_GIMPLIFIED may differs for Ada build. */
13245 verify_variant_match (TYPE_UNSIGNED);
13246 verify_variant_match (TYPE_PACKED);
13247 if (TREE_CODE (t) == REFERENCE_TYPE)
13248 verify_variant_match (TYPE_REF_IS_RVALUE);
13249 if (AGGREGATE_TYPE_P (t))
13250 verify_variant_match (TYPE_REVERSE_STORAGE_ORDER);
13251 else
13252 verify_variant_match (TYPE_SATURATING);
13253 /* FIXME: This check trigger during libstdc++ build. */
13254 #if 0
13255 if (RECORD_OR_UNION_TYPE_P (t) && COMPLETE_TYPE_P (t))
13256 verify_variant_match (TYPE_FINAL_P);
13257 #endif
13258
13259 /* tree_type_common checks. */
13260
13261 if (COMPLETE_TYPE_P (t))
13262 {
13263 verify_variant_match (TYPE_MODE);
13264 if (TREE_CODE (TYPE_SIZE (t)) != PLACEHOLDER_EXPR
13265 && TREE_CODE (TYPE_SIZE (tv)) != PLACEHOLDER_EXPR)
13266 verify_variant_match (TYPE_SIZE);
13267 if (TREE_CODE (TYPE_SIZE_UNIT (t)) != PLACEHOLDER_EXPR
13268 && TREE_CODE (TYPE_SIZE_UNIT (tv)) != PLACEHOLDER_EXPR
13269 && TYPE_SIZE_UNIT (t) != TYPE_SIZE_UNIT (tv))
13270 {
13271 gcc_assert (!operand_equal_p (TYPE_SIZE_UNIT (t),
13272 TYPE_SIZE_UNIT (tv), 0));
13273 error ("type variant has different %<TYPE_SIZE_UNIT%>");
13274 debug_tree (tv);
13275 error ("type variant%'s %<TYPE_SIZE_UNIT%>");
13276 debug_tree (TYPE_SIZE_UNIT (tv));
13277 error ("type%'s %<TYPE_SIZE_UNIT%>");
13278 debug_tree (TYPE_SIZE_UNIT (t));
13279 return false;
13280 }
13281 verify_variant_match (TYPE_NEEDS_CONSTRUCTING);
13282 }
13283 verify_variant_match (TYPE_PRECISION);
13284 if (RECORD_OR_UNION_TYPE_P (t))
13285 verify_variant_match (TYPE_TRANSPARENT_AGGR);
13286 else if (TREE_CODE (t) == ARRAY_TYPE)
13287 verify_variant_match (TYPE_NONALIASED_COMPONENT);
13288 /* During LTO we merge variant lists from diferent translation units
13289 that may differ BY TYPE_CONTEXT that in turn may point
13290 to TRANSLATION_UNIT_DECL.
13291 Ada also builds variants of types with different TYPE_CONTEXT. */
13292 #if 0
13293 if (!in_lto_p || !TYPE_FILE_SCOPE_P (t))
13294 verify_variant_match (TYPE_CONTEXT);
13295 #endif
13296 if (TREE_CODE (t) == ARRAY_TYPE || TREE_CODE (t) == INTEGER_TYPE)
13297 verify_variant_match (TYPE_STRING_FLAG);
13298 if (TREE_CODE (t) == RECORD_TYPE || TREE_CODE (t) == UNION_TYPE)
13299 verify_variant_match (TYPE_CXX_ODR_P);
13300 if (TYPE_ALIAS_SET_KNOWN_P (t))
13301 {
13302 error ("type variant with %<TYPE_ALIAS_SET_KNOWN_P%>");
13303 debug_tree (tv);
13304 return false;
13305 }
13306
13307 /* tree_type_non_common checks. */
13308
13309 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
13310 and dangle the pointer from time to time. */
13311 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_VFIELD (t) != TYPE_VFIELD (tv)
13312 && (in_lto_p || !TYPE_VFIELD (tv)
13313 || TREE_CODE (TYPE_VFIELD (tv)) != TREE_LIST))
13314 {
13315 error ("type variant has different %<TYPE_VFIELD%>");
13316 debug_tree (tv);
13317 return false;
13318 }
13319 if ((TREE_CODE (t) == ENUMERAL_TYPE && COMPLETE_TYPE_P (t))
13320 || TREE_CODE (t) == INTEGER_TYPE
13321 || TREE_CODE (t) == BOOLEAN_TYPE
13322 || TREE_CODE (t) == REAL_TYPE
13323 || TREE_CODE (t) == FIXED_POINT_TYPE)
13324 {
13325 verify_variant_match (TYPE_MAX_VALUE);
13326 verify_variant_match (TYPE_MIN_VALUE);
13327 }
13328 if (TREE_CODE (t) == METHOD_TYPE)
13329 verify_variant_match (TYPE_METHOD_BASETYPE);
13330 if (TREE_CODE (t) == OFFSET_TYPE)
13331 verify_variant_match (TYPE_OFFSET_BASETYPE);
13332 if (TREE_CODE (t) == ARRAY_TYPE)
13333 verify_variant_match (TYPE_ARRAY_MAX_SIZE);
13334 /* FIXME: Be lax and allow TYPE_BINFO to be missing in variant types
13335 or even type's main variant. This is needed to make bootstrap pass
13336 and the bug seems new in GCC 5.
13337 C++ FE should be updated to make this consistent and we should check
13338 that TYPE_BINFO is always NULL for !COMPLETE_TYPE_P and otherwise there
13339 is a match with main variant.
13340
13341 Also disable the check for Java for now because of parser hack that builds
13342 first an dummy BINFO and then sometimes replace it by real BINFO in some
13343 of the copies. */
13344 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t) && TYPE_BINFO (tv)
13345 && TYPE_BINFO (t) != TYPE_BINFO (tv)
13346 /* FIXME: Java sometimes keep dump TYPE_BINFOs on variant types.
13347 Since there is no cheap way to tell C++/Java type w/o LTO, do checking
13348 at LTO time only. */
13349 && (in_lto_p && odr_type_p (t)))
13350 {
13351 error ("type variant has different %<TYPE_BINFO%>");
13352 debug_tree (tv);
13353 error ("type variant%'s %<TYPE_BINFO%>");
13354 debug_tree (TYPE_BINFO (tv));
13355 error ("type%'s %<TYPE_BINFO%>");
13356 debug_tree (TYPE_BINFO (t));
13357 return false;
13358 }
13359
13360 /* Check various uses of TYPE_VALUES_RAW. */
13361 if (TREE_CODE (t) == ENUMERAL_TYPE
13362 && TYPE_VALUES (t))
13363 verify_variant_match (TYPE_VALUES);
13364 else if (TREE_CODE (t) == ARRAY_TYPE)
13365 verify_variant_match (TYPE_DOMAIN);
13366 /* Permit incomplete variants of complete type. While FEs may complete
13367 all variants, this does not happen for C++ templates in all cases. */
13368 else if (RECORD_OR_UNION_TYPE_P (t)
13369 && COMPLETE_TYPE_P (t)
13370 && TYPE_FIELDS (t) != TYPE_FIELDS (tv))
13371 {
13372 tree f1, f2;
13373
13374 /* Fortran builds qualified variants as new records with items of
13375 qualified type. Verify that they looks same. */
13376 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (tv);
13377 f1 && f2;
13378 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
13379 if (TREE_CODE (f1) != FIELD_DECL || TREE_CODE (f2) != FIELD_DECL
13380 || (TYPE_MAIN_VARIANT (TREE_TYPE (f1))
13381 != TYPE_MAIN_VARIANT (TREE_TYPE (f2))
13382 /* FIXME: gfc_nonrestricted_type builds all types as variants
13383 with exception of pointer types. It deeply copies the type
13384 which means that we may end up with a variant type
13385 referring non-variant pointer. We may change it to
13386 produce types as variants, too, like
13387 objc_get_protocol_qualified_type does. */
13388 && !POINTER_TYPE_P (TREE_TYPE (f1)))
13389 || DECL_FIELD_OFFSET (f1) != DECL_FIELD_OFFSET (f2)
13390 || DECL_FIELD_BIT_OFFSET (f1) != DECL_FIELD_BIT_OFFSET (f2))
13391 break;
13392 if (f1 || f2)
13393 {
13394 error ("type variant has different %<TYPE_FIELDS%>");
13395 debug_tree (tv);
13396 error ("first mismatch is field");
13397 debug_tree (f1);
13398 error ("and field");
13399 debug_tree (f2);
13400 return false;
13401 }
13402 }
13403 else if ((TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE))
13404 verify_variant_match (TYPE_ARG_TYPES);
13405 /* For C++ the qualified variant of array type is really an array type
13406 of qualified TREE_TYPE.
13407 objc builds variants of pointer where pointer to type is a variant, too
13408 in objc_get_protocol_qualified_type. */
13409 if (TREE_TYPE (t) != TREE_TYPE (tv)
13410 && ((TREE_CODE (t) != ARRAY_TYPE
13411 && !POINTER_TYPE_P (t))
13412 || TYPE_MAIN_VARIANT (TREE_TYPE (t))
13413 != TYPE_MAIN_VARIANT (TREE_TYPE (tv))))
13414 {
13415 error ("type variant has different %<TREE_TYPE%>");
13416 debug_tree (tv);
13417 error ("type variant%'s %<TREE_TYPE%>");
13418 debug_tree (TREE_TYPE (tv));
13419 error ("type%'s %<TREE_TYPE%>");
13420 debug_tree (TREE_TYPE (t));
13421 return false;
13422 }
13423 if (type_with_alias_set_p (t)
13424 && !gimple_canonical_types_compatible_p (t, tv, false))
13425 {
13426 error ("type is not compatible with its variant");
13427 debug_tree (tv);
13428 error ("type variant%'s %<TREE_TYPE%>");
13429 debug_tree (TREE_TYPE (tv));
13430 error ("type%'s %<TREE_TYPE%>");
13431 debug_tree (TREE_TYPE (t));
13432 return false;
13433 }
13434 return true;
13435 #undef verify_variant_match
13436 }
13437
13438
13439 /* The TYPE_CANONICAL merging machinery. It should closely resemble
13440 the middle-end types_compatible_p function. It needs to avoid
13441 claiming types are different for types that should be treated
13442 the same with respect to TBAA. Canonical types are also used
13443 for IL consistency checks via the useless_type_conversion_p
13444 predicate which does not handle all type kinds itself but falls
13445 back to pointer-comparison of TYPE_CANONICAL for aggregates
13446 for example. */
13447
13448 /* Return true if TYPE_UNSIGNED of TYPE should be ignored for canonical
13449 type calculation because we need to allow inter-operability between signed
13450 and unsigned variants. */
13451
13452 bool
13453 type_with_interoperable_signedness (const_tree type)
13454 {
13455 /* Fortran standard require C_SIGNED_CHAR to be interoperable with both
13456 signed char and unsigned char. Similarly fortran FE builds
13457 C_SIZE_T as signed type, while C defines it unsigned. */
13458
13459 return tree_code_for_canonical_type_merging (TREE_CODE (type))
13460 == INTEGER_TYPE
13461 && (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node)
13462 || TYPE_PRECISION (type) == TYPE_PRECISION (size_type_node));
13463 }
13464
13465 /* Return true iff T1 and T2 are structurally identical for what
13466 TBAA is concerned.
13467 This function is used both by lto.cc canonical type merging and by the
13468 verifier. If TRUST_TYPE_CANONICAL we do not look into structure of types
13469 that have TYPE_CANONICAL defined and assume them equivalent. This is useful
13470 only for LTO because only in these cases TYPE_CANONICAL equivalence
13471 correspond to one defined by gimple_canonical_types_compatible_p. */
13472
13473 bool
13474 gimple_canonical_types_compatible_p (const_tree t1, const_tree t2,
13475 bool trust_type_canonical)
13476 {
13477 /* Type variants should be same as the main variant. When not doing sanity
13478 checking to verify this fact, go to main variants and save some work. */
13479 if (trust_type_canonical)
13480 {
13481 t1 = TYPE_MAIN_VARIANT (t1);
13482 t2 = TYPE_MAIN_VARIANT (t2);
13483 }
13484
13485 /* Check first for the obvious case of pointer identity. */
13486 if (t1 == t2)
13487 return true;
13488
13489 /* Check that we have two types to compare. */
13490 if (t1 == NULL_TREE || t2 == NULL_TREE)
13491 return false;
13492
13493 /* We consider complete types always compatible with incomplete type.
13494 This does not make sense for canonical type calculation and thus we
13495 need to ensure that we are never called on it.
13496
13497 FIXME: For more correctness the function probably should have three modes
13498 1) mode assuming that types are complete mathcing their structure
13499 2) mode allowing incomplete types but producing equivalence classes
13500 and thus ignoring all info from complete types
13501 3) mode allowing incomplete types to match complete but checking
13502 compatibility between complete types.
13503
13504 1 and 2 can be used for canonical type calculation. 3 is the real
13505 definition of type compatibility that can be used i.e. for warnings during
13506 declaration merging. */
13507
13508 gcc_assert (!trust_type_canonical
13509 || (type_with_alias_set_p (t1) && type_with_alias_set_p (t2)));
13510
13511 /* If the types have been previously registered and found equal
13512 they still are. */
13513
13514 if (TYPE_CANONICAL (t1) && TYPE_CANONICAL (t2)
13515 && trust_type_canonical)
13516 {
13517 /* Do not use TYPE_CANONICAL of pointer types. For LTO streamed types
13518 they are always NULL, but they are set to non-NULL for types
13519 constructed by build_pointer_type and variants. In this case the
13520 TYPE_CANONICAL is more fine grained than the equivalnce we test (where
13521 all pointers are considered equal. Be sure to not return false
13522 negatives. */
13523 gcc_checking_assert (canonical_type_used_p (t1)
13524 && canonical_type_used_p (t2));
13525 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
13526 }
13527
13528 /* For types where we do ODR based TBAA the canonical type is always
13529 set correctly, so we know that types are different if their
13530 canonical types does not match. */
13531 if (trust_type_canonical
13532 && (odr_type_p (t1) && odr_based_tbaa_p (t1))
13533 != (odr_type_p (t2) && odr_based_tbaa_p (t2)))
13534 return false;
13535
13536 /* Can't be the same type if the types don't have the same code. */
13537 enum tree_code code = tree_code_for_canonical_type_merging (TREE_CODE (t1));
13538 if (code != tree_code_for_canonical_type_merging (TREE_CODE (t2)))
13539 return false;
13540
13541 /* Qualifiers do not matter for canonical type comparison purposes. */
13542
13543 /* Void types and nullptr types are always the same. */
13544 if (TREE_CODE (t1) == VOID_TYPE
13545 || TREE_CODE (t1) == NULLPTR_TYPE)
13546 return true;
13547
13548 /* Can't be the same type if they have different mode. */
13549 if (TYPE_MODE (t1) != TYPE_MODE (t2))
13550 return false;
13551
13552 /* Non-aggregate types can be handled cheaply. */
13553 if (INTEGRAL_TYPE_P (t1)
13554 || SCALAR_FLOAT_TYPE_P (t1)
13555 || FIXED_POINT_TYPE_P (t1)
13556 || TREE_CODE (t1) == VECTOR_TYPE
13557 || TREE_CODE (t1) == COMPLEX_TYPE
13558 || TREE_CODE (t1) == OFFSET_TYPE
13559 || POINTER_TYPE_P (t1))
13560 {
13561 /* Can't be the same type if they have different recision. */
13562 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
13563 return false;
13564
13565 /* In some cases the signed and unsigned types are required to be
13566 inter-operable. */
13567 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2)
13568 && !type_with_interoperable_signedness (t1))
13569 return false;
13570
13571 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
13572 interoperable with "signed char". Unless all frontends are revisited
13573 to agree on these types, we must ignore the flag completely. */
13574
13575 /* Fortran standard define C_PTR type that is compatible with every
13576 C pointer. For this reason we need to glob all pointers into one.
13577 Still pointers in different address spaces are not compatible. */
13578 if (POINTER_TYPE_P (t1))
13579 {
13580 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
13581 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
13582 return false;
13583 }
13584
13585 /* Tail-recurse to components. */
13586 if (TREE_CODE (t1) == VECTOR_TYPE
13587 || TREE_CODE (t1) == COMPLEX_TYPE)
13588 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
13589 TREE_TYPE (t2),
13590 trust_type_canonical);
13591
13592 return true;
13593 }
13594
13595 /* Do type-specific comparisons. */
13596 switch (TREE_CODE (t1))
13597 {
13598 case ARRAY_TYPE:
13599 /* Array types are the same if the element types are the same and
13600 the number of elements are the same. */
13601 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13602 trust_type_canonical)
13603 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
13604 || TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2)
13605 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
13606 return false;
13607 else
13608 {
13609 tree i1 = TYPE_DOMAIN (t1);
13610 tree i2 = TYPE_DOMAIN (t2);
13611
13612 /* For an incomplete external array, the type domain can be
13613 NULL_TREE. Check this condition also. */
13614 if (i1 == NULL_TREE && i2 == NULL_TREE)
13615 return true;
13616 else if (i1 == NULL_TREE || i2 == NULL_TREE)
13617 return false;
13618 else
13619 {
13620 tree min1 = TYPE_MIN_VALUE (i1);
13621 tree min2 = TYPE_MIN_VALUE (i2);
13622 tree max1 = TYPE_MAX_VALUE (i1);
13623 tree max2 = TYPE_MAX_VALUE (i2);
13624
13625 /* The minimum/maximum values have to be the same. */
13626 if ((min1 == min2
13627 || (min1 && min2
13628 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
13629 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
13630 || operand_equal_p (min1, min2, 0))))
13631 && (max1 == max2
13632 || (max1 && max2
13633 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
13634 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
13635 || operand_equal_p (max1, max2, 0)))))
13636 return true;
13637 else
13638 return false;
13639 }
13640 }
13641
13642 case METHOD_TYPE:
13643 case FUNCTION_TYPE:
13644 /* Function types are the same if the return type and arguments types
13645 are the same. */
13646 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13647 trust_type_canonical))
13648 return false;
13649
13650 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
13651 return true;
13652 else
13653 {
13654 tree parms1, parms2;
13655
13656 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
13657 parms1 && parms2;
13658 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
13659 {
13660 if (!gimple_canonical_types_compatible_p
13661 (TREE_VALUE (parms1), TREE_VALUE (parms2),
13662 trust_type_canonical))
13663 return false;
13664 }
13665
13666 if (parms1 || parms2)
13667 return false;
13668
13669 return true;
13670 }
13671
13672 case RECORD_TYPE:
13673 case UNION_TYPE:
13674 case QUAL_UNION_TYPE:
13675 {
13676 tree f1, f2;
13677
13678 /* Don't try to compare variants of an incomplete type, before
13679 TYPE_FIELDS has been copied around. */
13680 if (!COMPLETE_TYPE_P (t1) && !COMPLETE_TYPE_P (t2))
13681 return true;
13682
13683
13684 if (TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2))
13685 return false;
13686
13687 /* For aggregate types, all the fields must be the same. */
13688 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
13689 f1 || f2;
13690 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
13691 {
13692 /* Skip non-fields and zero-sized fields. */
13693 while (f1 && (TREE_CODE (f1) != FIELD_DECL
13694 || (DECL_SIZE (f1)
13695 && integer_zerop (DECL_SIZE (f1)))))
13696 f1 = TREE_CHAIN (f1);
13697 while (f2 && (TREE_CODE (f2) != FIELD_DECL
13698 || (DECL_SIZE (f2)
13699 && integer_zerop (DECL_SIZE (f2)))))
13700 f2 = TREE_CHAIN (f2);
13701 if (!f1 || !f2)
13702 break;
13703 /* The fields must have the same name, offset and type. */
13704 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
13705 || !gimple_compare_field_offset (f1, f2)
13706 || !gimple_canonical_types_compatible_p
13707 (TREE_TYPE (f1), TREE_TYPE (f2),
13708 trust_type_canonical))
13709 return false;
13710 }
13711
13712 /* If one aggregate has more fields than the other, they
13713 are not the same. */
13714 if (f1 || f2)
13715 return false;
13716
13717 return true;
13718 }
13719
13720 default:
13721 /* Consider all types with language specific trees in them mutually
13722 compatible. This is executed only from verify_type and false
13723 positives can be tolerated. */
13724 gcc_assert (!in_lto_p);
13725 return true;
13726 }
13727 }
13728
13729 /* For OPAQUE_TYPE T, it should have only size and alignment information
13730 and its mode should be of class MODE_OPAQUE. This function verifies
13731 these properties of T match TV which is the main variant of T and TC
13732 which is the canonical of T. */
13733
13734 static void
13735 verify_opaque_type (const_tree t, tree tv, tree tc)
13736 {
13737 gcc_assert (OPAQUE_TYPE_P (t));
13738 gcc_assert (tv && tv == TYPE_MAIN_VARIANT (tv));
13739 gcc_assert (tc && tc == TYPE_CANONICAL (tc));
13740
13741 /* For an opaque type T1, check if some of its properties match
13742 the corresponding ones of the other opaque type T2, emit some
13743 error messages for those inconsistent ones. */
13744 auto check_properties_for_opaque_type = [](const_tree t1, tree t2,
13745 const char *kind_msg)
13746 {
13747 if (!OPAQUE_TYPE_P (t2))
13748 {
13749 error ("type %s is not an opaque type", kind_msg);
13750 debug_tree (t2);
13751 return;
13752 }
13753 if (!OPAQUE_MODE_P (TYPE_MODE (t2)))
13754 {
13755 error ("type %s is not with opaque mode", kind_msg);
13756 debug_tree (t2);
13757 return;
13758 }
13759 if (TYPE_MODE (t1) != TYPE_MODE (t2))
13760 {
13761 error ("type %s differs by %<TYPE_MODE%>", kind_msg);
13762 debug_tree (t2);
13763 return;
13764 }
13765 poly_uint64 t1_size = tree_to_poly_uint64 (TYPE_SIZE (t1));
13766 poly_uint64 t2_size = tree_to_poly_uint64 (TYPE_SIZE (t2));
13767 if (maybe_ne (t1_size, t2_size))
13768 {
13769 error ("type %s differs by %<TYPE_SIZE%>", kind_msg);
13770 debug_tree (t2);
13771 return;
13772 }
13773 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
13774 {
13775 error ("type %s differs by %<TYPE_ALIGN%>", kind_msg);
13776 debug_tree (t2);
13777 return;
13778 }
13779 if (TYPE_USER_ALIGN (t1) != TYPE_USER_ALIGN (t2))
13780 {
13781 error ("type %s differs by %<TYPE_USER_ALIGN%>", kind_msg);
13782 debug_tree (t2);
13783 return;
13784 }
13785 };
13786
13787 if (t != tv)
13788 check_properties_for_opaque_type (t, tv, "variant");
13789
13790 if (t != tc)
13791 check_properties_for_opaque_type (t, tc, "canonical");
13792 }
13793
13794 /* Verify type T. */
13795
13796 void
13797 verify_type (const_tree t)
13798 {
13799 bool error_found = false;
13800 tree mv = TYPE_MAIN_VARIANT (t);
13801 tree ct = TYPE_CANONICAL (t);
13802
13803 if (OPAQUE_TYPE_P (t))
13804 {
13805 verify_opaque_type (t, mv, ct);
13806 return;
13807 }
13808
13809 if (!mv)
13810 {
13811 error ("main variant is not defined");
13812 error_found = true;
13813 }
13814 else if (mv != TYPE_MAIN_VARIANT (mv))
13815 {
13816 error ("%<TYPE_MAIN_VARIANT%> has different %<TYPE_MAIN_VARIANT%>");
13817 debug_tree (mv);
13818 error_found = true;
13819 }
13820 else if (t != mv && !verify_type_variant (t, mv))
13821 error_found = true;
13822
13823 if (!ct)
13824 ;
13825 else if (TYPE_CANONICAL (ct) != ct)
13826 {
13827 error ("%<TYPE_CANONICAL%> has different %<TYPE_CANONICAL%>");
13828 debug_tree (ct);
13829 error_found = true;
13830 }
13831 /* Method and function types cannot be used to address memory and thus
13832 TYPE_CANONICAL really matters only for determining useless conversions.
13833
13834 FIXME: C++ FE produce declarations of builtin functions that are not
13835 compatible with main variants. */
13836 else if (TREE_CODE (t) == FUNCTION_TYPE)
13837 ;
13838 else if (t != ct
13839 /* FIXME: gimple_canonical_types_compatible_p cannot compare types
13840 with variably sized arrays because their sizes possibly
13841 gimplified to different variables. */
13842 && !variably_modified_type_p (ct, NULL)
13843 && !gimple_canonical_types_compatible_p (t, ct, false)
13844 && COMPLETE_TYPE_P (t))
13845 {
13846 error ("%<TYPE_CANONICAL%> is not compatible");
13847 debug_tree (ct);
13848 error_found = true;
13849 }
13850
13851 if (COMPLETE_TYPE_P (t) && TYPE_CANONICAL (t)
13852 && TYPE_MODE (t) != TYPE_MODE (TYPE_CANONICAL (t)))
13853 {
13854 error ("%<TYPE_MODE%> of %<TYPE_CANONICAL%> is not compatible");
13855 debug_tree (ct);
13856 error_found = true;
13857 }
13858 if (TYPE_MAIN_VARIANT (t) == t && ct && TYPE_MAIN_VARIANT (ct) != ct)
13859 {
13860 error ("%<TYPE_CANONICAL%> of main variant is not main variant");
13861 debug_tree (ct);
13862 debug_tree (TYPE_MAIN_VARIANT (ct));
13863 error_found = true;
13864 }
13865
13866
13867 /* Check various uses of TYPE_MIN_VALUE_RAW. */
13868 if (RECORD_OR_UNION_TYPE_P (t))
13869 {
13870 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
13871 and danagle the pointer from time to time. */
13872 if (TYPE_VFIELD (t)
13873 && TREE_CODE (TYPE_VFIELD (t)) != FIELD_DECL
13874 && TREE_CODE (TYPE_VFIELD (t)) != TREE_LIST)
13875 {
13876 error ("%<TYPE_VFIELD%> is not %<FIELD_DECL%> nor %<TREE_LIST%>");
13877 debug_tree (TYPE_VFIELD (t));
13878 error_found = true;
13879 }
13880 }
13881 else if (TREE_CODE (t) == POINTER_TYPE)
13882 {
13883 if (TYPE_NEXT_PTR_TO (t)
13884 && TREE_CODE (TYPE_NEXT_PTR_TO (t)) != POINTER_TYPE)
13885 {
13886 error ("%<TYPE_NEXT_PTR_TO%> is not %<POINTER_TYPE%>");
13887 debug_tree (TYPE_NEXT_PTR_TO (t));
13888 error_found = true;
13889 }
13890 }
13891 else if (TREE_CODE (t) == REFERENCE_TYPE)
13892 {
13893 if (TYPE_NEXT_REF_TO (t)
13894 && TREE_CODE (TYPE_NEXT_REF_TO (t)) != REFERENCE_TYPE)
13895 {
13896 error ("%<TYPE_NEXT_REF_TO%> is not %<REFERENCE_TYPE%>");
13897 debug_tree (TYPE_NEXT_REF_TO (t));
13898 error_found = true;
13899 }
13900 }
13901 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
13902 || TREE_CODE (t) == FIXED_POINT_TYPE)
13903 {
13904 /* FIXME: The following check should pass:
13905 useless_type_conversion_p (const_cast <tree> (t),
13906 TREE_TYPE (TYPE_MIN_VALUE (t))
13907 but does not for C sizetypes in LTO. */
13908 }
13909
13910 /* Check various uses of TYPE_MAXVAL_RAW. */
13911 if (RECORD_OR_UNION_TYPE_P (t))
13912 {
13913 if (!TYPE_BINFO (t))
13914 ;
13915 else if (TREE_CODE (TYPE_BINFO (t)) != TREE_BINFO)
13916 {
13917 error ("%<TYPE_BINFO%> is not %<TREE_BINFO%>");
13918 debug_tree (TYPE_BINFO (t));
13919 error_found = true;
13920 }
13921 else if (TREE_TYPE (TYPE_BINFO (t)) != TYPE_MAIN_VARIANT (t))
13922 {
13923 error ("%<TYPE_BINFO%> type is not %<TYPE_MAIN_VARIANT%>");
13924 debug_tree (TREE_TYPE (TYPE_BINFO (t)));
13925 error_found = true;
13926 }
13927 }
13928 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
13929 {
13930 if (TYPE_METHOD_BASETYPE (t)
13931 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != RECORD_TYPE
13932 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != UNION_TYPE)
13933 {
13934 error ("%<TYPE_METHOD_BASETYPE%> is not record nor union");
13935 debug_tree (TYPE_METHOD_BASETYPE (t));
13936 error_found = true;
13937 }
13938 }
13939 else if (TREE_CODE (t) == OFFSET_TYPE)
13940 {
13941 if (TYPE_OFFSET_BASETYPE (t)
13942 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != RECORD_TYPE
13943 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != UNION_TYPE)
13944 {
13945 error ("%<TYPE_OFFSET_BASETYPE%> is not record nor union");
13946 debug_tree (TYPE_OFFSET_BASETYPE (t));
13947 error_found = true;
13948 }
13949 }
13950 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
13951 || TREE_CODE (t) == FIXED_POINT_TYPE)
13952 {
13953 /* FIXME: The following check should pass:
13954 useless_type_conversion_p (const_cast <tree> (t),
13955 TREE_TYPE (TYPE_MAX_VALUE (t))
13956 but does not for C sizetypes in LTO. */
13957 }
13958 else if (TREE_CODE (t) == ARRAY_TYPE)
13959 {
13960 if (TYPE_ARRAY_MAX_SIZE (t)
13961 && TREE_CODE (TYPE_ARRAY_MAX_SIZE (t)) != INTEGER_CST)
13962 {
13963 error ("%<TYPE_ARRAY_MAX_SIZE%> not %<INTEGER_CST%>");
13964 debug_tree (TYPE_ARRAY_MAX_SIZE (t));
13965 error_found = true;
13966 }
13967 }
13968 else if (TYPE_MAX_VALUE_RAW (t))
13969 {
13970 error ("%<TYPE_MAX_VALUE_RAW%> non-NULL");
13971 debug_tree (TYPE_MAX_VALUE_RAW (t));
13972 error_found = true;
13973 }
13974
13975 if (TYPE_LANG_SLOT_1 (t) && in_lto_p)
13976 {
13977 error ("%<TYPE_LANG_SLOT_1 (binfo)%> field is non-NULL");
13978 debug_tree (TYPE_LANG_SLOT_1 (t));
13979 error_found = true;
13980 }
13981
13982 /* Check various uses of TYPE_VALUES_RAW. */
13983 if (TREE_CODE (t) == ENUMERAL_TYPE)
13984 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
13985 {
13986 tree value = TREE_VALUE (l);
13987 tree name = TREE_PURPOSE (l);
13988
13989 /* C FE porduce INTEGER_CST of INTEGER_TYPE, while C++ FE uses
13990 CONST_DECL of ENUMERAL TYPE. */
13991 if (TREE_CODE (value) != INTEGER_CST && TREE_CODE (value) != CONST_DECL)
13992 {
13993 error ("enum value is not %<CONST_DECL%> or %<INTEGER_CST%>");
13994 debug_tree (value);
13995 debug_tree (name);
13996 error_found = true;
13997 }
13998 if (TREE_CODE (TREE_TYPE (value)) != INTEGER_TYPE
13999 && TREE_CODE (TREE_TYPE (value)) != BOOLEAN_TYPE
14000 && !useless_type_conversion_p (const_cast <tree> (t), TREE_TYPE (value)))
14001 {
14002 error ("enum value type is not %<INTEGER_TYPE%> nor convertible "
14003 "to the enum");
14004 debug_tree (value);
14005 debug_tree (name);
14006 error_found = true;
14007 }
14008 if (TREE_CODE (name) != IDENTIFIER_NODE)
14009 {
14010 error ("enum value name is not %<IDENTIFIER_NODE%>");
14011 debug_tree (value);
14012 debug_tree (name);
14013 error_found = true;
14014 }
14015 }
14016 else if (TREE_CODE (t) == ARRAY_TYPE)
14017 {
14018 if (TYPE_DOMAIN (t) && TREE_CODE (TYPE_DOMAIN (t)) != INTEGER_TYPE)
14019 {
14020 error ("array %<TYPE_DOMAIN%> is not integer type");
14021 debug_tree (TYPE_DOMAIN (t));
14022 error_found = true;
14023 }
14024 }
14025 else if (RECORD_OR_UNION_TYPE_P (t))
14026 {
14027 if (TYPE_FIELDS (t) && !COMPLETE_TYPE_P (t) && in_lto_p)
14028 {
14029 error ("%<TYPE_FIELDS%> defined in incomplete type");
14030 error_found = true;
14031 }
14032 for (tree fld = TYPE_FIELDS (t); fld; fld = TREE_CHAIN (fld))
14033 {
14034 /* TODO: verify properties of decls. */
14035 if (TREE_CODE (fld) == FIELD_DECL)
14036 ;
14037 else if (TREE_CODE (fld) == TYPE_DECL)
14038 ;
14039 else if (TREE_CODE (fld) == CONST_DECL)
14040 ;
14041 else if (VAR_P (fld))
14042 ;
14043 else if (TREE_CODE (fld) == TEMPLATE_DECL)
14044 ;
14045 else if (TREE_CODE (fld) == USING_DECL)
14046 ;
14047 else if (TREE_CODE (fld) == FUNCTION_DECL)
14048 ;
14049 else
14050 {
14051 error ("wrong tree in %<TYPE_FIELDS%> list");
14052 debug_tree (fld);
14053 error_found = true;
14054 }
14055 }
14056 }
14057 else if (TREE_CODE (t) == INTEGER_TYPE
14058 || TREE_CODE (t) == BOOLEAN_TYPE
14059 || TREE_CODE (t) == OFFSET_TYPE
14060 || TREE_CODE (t) == REFERENCE_TYPE
14061 || TREE_CODE (t) == NULLPTR_TYPE
14062 || TREE_CODE (t) == POINTER_TYPE)
14063 {
14064 if (TYPE_CACHED_VALUES_P (t) != (TYPE_CACHED_VALUES (t) != NULL))
14065 {
14066 error ("%<TYPE_CACHED_VALUES_P%> is %i while %<TYPE_CACHED_VALUES%> "
14067 "is %p",
14068 TYPE_CACHED_VALUES_P (t), (void *)TYPE_CACHED_VALUES (t));
14069 error_found = true;
14070 }
14071 else if (TYPE_CACHED_VALUES_P (t) && TREE_CODE (TYPE_CACHED_VALUES (t)) != TREE_VEC)
14072 {
14073 error ("%<TYPE_CACHED_VALUES%> is not %<TREE_VEC%>");
14074 debug_tree (TYPE_CACHED_VALUES (t));
14075 error_found = true;
14076 }
14077 /* Verify just enough of cache to ensure that no one copied it to new type.
14078 All copying should go by copy_node that should clear it. */
14079 else if (TYPE_CACHED_VALUES_P (t))
14080 {
14081 int i;
14082 for (i = 0; i < TREE_VEC_LENGTH (TYPE_CACHED_VALUES (t)); i++)
14083 if (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)
14084 && TREE_TYPE (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)) != t)
14085 {
14086 error ("wrong %<TYPE_CACHED_VALUES%> entry");
14087 debug_tree (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i));
14088 error_found = true;
14089 break;
14090 }
14091 }
14092 }
14093 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
14094 for (tree l = TYPE_ARG_TYPES (t); l; l = TREE_CHAIN (l))
14095 {
14096 /* C++ FE uses TREE_PURPOSE to store initial values. */
14097 if (TREE_PURPOSE (l) && in_lto_p)
14098 {
14099 error ("%<TREE_PURPOSE%> is non-NULL in %<TYPE_ARG_TYPES%> list");
14100 debug_tree (l);
14101 error_found = true;
14102 }
14103 if (!TYPE_P (TREE_VALUE (l)))
14104 {
14105 error ("wrong entry in %<TYPE_ARG_TYPES%> list");
14106 debug_tree (l);
14107 error_found = true;
14108 }
14109 }
14110 else if (!is_lang_specific (t) && TYPE_VALUES_RAW (t))
14111 {
14112 error ("%<TYPE_VALUES_RAW%> field is non-NULL");
14113 debug_tree (TYPE_VALUES_RAW (t));
14114 error_found = true;
14115 }
14116 if (TREE_CODE (t) != INTEGER_TYPE
14117 && TREE_CODE (t) != BOOLEAN_TYPE
14118 && TREE_CODE (t) != OFFSET_TYPE
14119 && TREE_CODE (t) != REFERENCE_TYPE
14120 && TREE_CODE (t) != NULLPTR_TYPE
14121 && TREE_CODE (t) != POINTER_TYPE
14122 && TYPE_CACHED_VALUES_P (t))
14123 {
14124 error ("%<TYPE_CACHED_VALUES_P%> is set while it should not be");
14125 error_found = true;
14126 }
14127
14128 /* ipa-devirt makes an assumption that TYPE_METHOD_BASETYPE is always
14129 TYPE_MAIN_VARIANT and it would be odd to add methods only to variatns
14130 of a type. */
14131 if (TREE_CODE (t) == METHOD_TYPE
14132 && TYPE_MAIN_VARIANT (TYPE_METHOD_BASETYPE (t)) != TYPE_METHOD_BASETYPE (t))
14133 {
14134 error ("%<TYPE_METHOD_BASETYPE%> is not main variant");
14135 error_found = true;
14136 }
14137
14138 if (error_found)
14139 {
14140 debug_tree (const_cast <tree> (t));
14141 internal_error ("%qs failed", __func__);
14142 }
14143 }
14144
14145
14146 /* Return 1 if ARG interpreted as signed in its precision is known to be
14147 always positive or 2 if ARG is known to be always negative, or 3 if
14148 ARG may be positive or negative. */
14149
14150 int
14151 get_range_pos_neg (tree arg)
14152 {
14153 if (arg == error_mark_node)
14154 return 3;
14155
14156 int prec = TYPE_PRECISION (TREE_TYPE (arg));
14157 int cnt = 0;
14158 if (TREE_CODE (arg) == INTEGER_CST)
14159 {
14160 wide_int w = wi::sext (wi::to_wide (arg), prec);
14161 if (wi::neg_p (w))
14162 return 2;
14163 else
14164 return 1;
14165 }
14166 while (CONVERT_EXPR_P (arg)
14167 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
14168 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg, 0))) <= prec)
14169 {
14170 arg = TREE_OPERAND (arg, 0);
14171 /* Narrower value zero extended into wider type
14172 will always result in positive values. */
14173 if (TYPE_UNSIGNED (TREE_TYPE (arg))
14174 && TYPE_PRECISION (TREE_TYPE (arg)) < prec)
14175 return 1;
14176 prec = TYPE_PRECISION (TREE_TYPE (arg));
14177 if (++cnt > 30)
14178 return 3;
14179 }
14180
14181 if (TREE_CODE (arg) != SSA_NAME)
14182 return 3;
14183 value_range r;
14184 while (!get_global_range_query ()->range_of_expr (r, arg) || r.kind () != VR_RANGE)
14185 {
14186 gimple *g = SSA_NAME_DEF_STMT (arg);
14187 if (is_gimple_assign (g)
14188 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (g)))
14189 {
14190 tree t = gimple_assign_rhs1 (g);
14191 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
14192 && TYPE_PRECISION (TREE_TYPE (t)) <= prec)
14193 {
14194 if (TYPE_UNSIGNED (TREE_TYPE (t))
14195 && TYPE_PRECISION (TREE_TYPE (t)) < prec)
14196 return 1;
14197 prec = TYPE_PRECISION (TREE_TYPE (t));
14198 arg = t;
14199 if (++cnt > 30)
14200 return 3;
14201 continue;
14202 }
14203 }
14204 return 3;
14205 }
14206 if (TYPE_UNSIGNED (TREE_TYPE (arg)))
14207 {
14208 /* For unsigned values, the "positive" range comes
14209 below the "negative" range. */
14210 if (!wi::neg_p (wi::sext (r.upper_bound (), prec), SIGNED))
14211 return 1;
14212 if (wi::neg_p (wi::sext (r.lower_bound (), prec), SIGNED))
14213 return 2;
14214 }
14215 else
14216 {
14217 if (!wi::neg_p (wi::sext (r.lower_bound (), prec), SIGNED))
14218 return 1;
14219 if (wi::neg_p (wi::sext (r.upper_bound (), prec), SIGNED))
14220 return 2;
14221 }
14222 return 3;
14223 }
14224
14225
14226
14227
14228 /* Return true if ARG is marked with the nonnull attribute in the
14229 current function signature. */
14230
14231 bool
14232 nonnull_arg_p (const_tree arg)
14233 {
14234 tree t, attrs, fntype;
14235 unsigned HOST_WIDE_INT arg_num;
14236
14237 gcc_assert (TREE_CODE (arg) == PARM_DECL
14238 && (POINTER_TYPE_P (TREE_TYPE (arg))
14239 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE));
14240
14241 /* The static chain decl is always non null. */
14242 if (arg == cfun->static_chain_decl)
14243 return true;
14244
14245 /* THIS argument of method is always non-NULL. */
14246 if (TREE_CODE (TREE_TYPE (cfun->decl)) == METHOD_TYPE
14247 && arg == DECL_ARGUMENTS (cfun->decl)
14248 && flag_delete_null_pointer_checks)
14249 return true;
14250
14251 /* Values passed by reference are always non-NULL. */
14252 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
14253 && flag_delete_null_pointer_checks)
14254 return true;
14255
14256 fntype = TREE_TYPE (cfun->decl);
14257 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
14258 {
14259 attrs = lookup_attribute ("nonnull", attrs);
14260
14261 /* If "nonnull" wasn't specified, we know nothing about the argument. */
14262 if (attrs == NULL_TREE)
14263 return false;
14264
14265 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
14266 if (TREE_VALUE (attrs) == NULL_TREE)
14267 return true;
14268
14269 /* Get the position number for ARG in the function signature. */
14270 for (arg_num = 1, t = DECL_ARGUMENTS (cfun->decl);
14271 t;
14272 t = DECL_CHAIN (t), arg_num++)
14273 {
14274 if (t == arg)
14275 break;
14276 }
14277
14278 gcc_assert (t == arg);
14279
14280 /* Now see if ARG_NUM is mentioned in the nonnull list. */
14281 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
14282 {
14283 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
14284 return true;
14285 }
14286 }
14287
14288 return false;
14289 }
14290
14291 /* Combine LOC and BLOCK to a combined adhoc loc, retaining any range
14292 information. */
14293
14294 location_t
14295 set_block (location_t loc, tree block)
14296 {
14297 location_t pure_loc = get_pure_location (loc);
14298 source_range src_range = get_range_from_loc (line_table, loc);
14299 unsigned discriminator = get_discriminator_from_loc (line_table, loc);
14300 return COMBINE_LOCATION_DATA (line_table, pure_loc, src_range, block, discriminator);
14301 }
14302
14303 location_t
14304 set_source_range (tree expr, location_t start, location_t finish)
14305 {
14306 source_range src_range;
14307 src_range.m_start = start;
14308 src_range.m_finish = finish;
14309 return set_source_range (expr, src_range);
14310 }
14311
14312 location_t
14313 set_source_range (tree expr, source_range src_range)
14314 {
14315 if (!EXPR_P (expr))
14316 return UNKNOWN_LOCATION;
14317
14318 location_t expr_location = EXPR_LOCATION (expr);
14319 location_t pure_loc = get_pure_location (expr_location);
14320 unsigned discriminator = get_discriminator_from_loc (expr_location);
14321 location_t adhoc = COMBINE_LOCATION_DATA (line_table,
14322 pure_loc,
14323 src_range,
14324 NULL,
14325 discriminator);
14326 SET_EXPR_LOCATION (expr, adhoc);
14327 return adhoc;
14328 }
14329
14330 /* Return EXPR, potentially wrapped with a node expression LOC,
14331 if !CAN_HAVE_LOCATION_P (expr).
14332
14333 NON_LVALUE_EXPR is used for wrapping constants, apart from STRING_CST.
14334 VIEW_CONVERT_EXPR is used for wrapping non-constants and STRING_CST.
14335
14336 Wrapper nodes can be identified using location_wrapper_p. */
14337
14338 tree
14339 maybe_wrap_with_location (tree expr, location_t loc)
14340 {
14341 if (expr == NULL)
14342 return NULL;
14343 if (loc == UNKNOWN_LOCATION)
14344 return expr;
14345 if (CAN_HAVE_LOCATION_P (expr))
14346 return expr;
14347 /* We should only be adding wrappers for constants and for decls,
14348 or for some exceptional tree nodes (e.g. BASELINK in the C++ FE). */
14349 gcc_assert (CONSTANT_CLASS_P (expr)
14350 || DECL_P (expr)
14351 || EXCEPTIONAL_CLASS_P (expr));
14352
14353 /* For now, don't add wrappers to exceptional tree nodes, to minimize
14354 any impact of the wrapper nodes. */
14355 if (EXCEPTIONAL_CLASS_P (expr))
14356 return expr;
14357
14358 /* Compiler-generated temporary variables don't need a wrapper. */
14359 if (DECL_P (expr) && DECL_ARTIFICIAL (expr) && DECL_IGNORED_P (expr))
14360 return expr;
14361
14362 /* If any auto_suppress_location_wrappers are active, don't create
14363 wrappers. */
14364 if (suppress_location_wrappers > 0)
14365 return expr;
14366
14367 tree_code code
14368 = (((CONSTANT_CLASS_P (expr) && TREE_CODE (expr) != STRING_CST)
14369 || (TREE_CODE (expr) == CONST_DECL && !TREE_STATIC (expr)))
14370 ? NON_LVALUE_EXPR : VIEW_CONVERT_EXPR);
14371 tree wrapper = build1_loc (loc, code, TREE_TYPE (expr), expr);
14372 /* Mark this node as being a wrapper. */
14373 EXPR_LOCATION_WRAPPER_P (wrapper) = 1;
14374 return wrapper;
14375 }
14376
14377 int suppress_location_wrappers;
14378
14379 /* Return the name of combined function FN, for debugging purposes. */
14380
14381 const char *
14382 combined_fn_name (combined_fn fn)
14383 {
14384 if (builtin_fn_p (fn))
14385 {
14386 tree fndecl = builtin_decl_explicit (as_builtin_fn (fn));
14387 return IDENTIFIER_POINTER (DECL_NAME (fndecl));
14388 }
14389 else
14390 return internal_fn_name (as_internal_fn (fn));
14391 }
14392
14393 /* Return a bitmap with a bit set corresponding to each argument in
14394 a function call type FNTYPE declared with attribute nonnull,
14395 or null if none of the function's argument are nonnull. The caller
14396 must free the bitmap. */
14397
14398 bitmap
14399 get_nonnull_args (const_tree fntype)
14400 {
14401 if (fntype == NULL_TREE)
14402 return NULL;
14403
14404 bitmap argmap = NULL;
14405 if (TREE_CODE (fntype) == METHOD_TYPE)
14406 {
14407 /* The this pointer in C++ non-static member functions is
14408 implicitly nonnull whether or not it's declared as such. */
14409 argmap = BITMAP_ALLOC (NULL);
14410 bitmap_set_bit (argmap, 0);
14411 }
14412
14413 tree attrs = TYPE_ATTRIBUTES (fntype);
14414 if (!attrs)
14415 return argmap;
14416
14417 /* A function declaration can specify multiple attribute nonnull,
14418 each with zero or more arguments. The loop below creates a bitmap
14419 representing a union of all the arguments. An empty (but non-null)
14420 bitmap means that all arguments have been declaraed nonnull. */
14421 for ( ; attrs; attrs = TREE_CHAIN (attrs))
14422 {
14423 attrs = lookup_attribute ("nonnull", attrs);
14424 if (!attrs)
14425 break;
14426
14427 if (!argmap)
14428 argmap = BITMAP_ALLOC (NULL);
14429
14430 if (!TREE_VALUE (attrs))
14431 {
14432 /* Clear the bitmap in case a previous attribute nonnull
14433 set it and this one overrides it for all arguments. */
14434 bitmap_clear (argmap);
14435 return argmap;
14436 }
14437
14438 /* Iterate over the indices of the format arguments declared nonnull
14439 and set a bit for each. */
14440 for (tree idx = TREE_VALUE (attrs); idx; idx = TREE_CHAIN (idx))
14441 {
14442 unsigned int val = TREE_INT_CST_LOW (TREE_VALUE (idx)) - 1;
14443 bitmap_set_bit (argmap, val);
14444 }
14445 }
14446
14447 return argmap;
14448 }
14449
14450 /* Returns true if TYPE is a type where it and all of its subobjects
14451 (recursively) are of structure, union, or array type. */
14452
14453 bool
14454 is_empty_type (const_tree type)
14455 {
14456 if (RECORD_OR_UNION_TYPE_P (type))
14457 {
14458 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
14459 if (TREE_CODE (field) == FIELD_DECL
14460 && !DECL_PADDING_P (field)
14461 && !is_empty_type (TREE_TYPE (field)))
14462 return false;
14463 return true;
14464 }
14465 else if (TREE_CODE (type) == ARRAY_TYPE)
14466 return (integer_minus_onep (array_type_nelts (type))
14467 || TYPE_DOMAIN (type) == NULL_TREE
14468 || is_empty_type (TREE_TYPE (type)));
14469 return false;
14470 }
14471
14472 /* Implement TARGET_EMPTY_RECORD_P. Return true if TYPE is an empty type
14473 that shouldn't be passed via stack. */
14474
14475 bool
14476 default_is_empty_record (const_tree type)
14477 {
14478 if (!abi_version_at_least (12))
14479 return false;
14480
14481 if (type == error_mark_node)
14482 return false;
14483
14484 if (TREE_ADDRESSABLE (type))
14485 return false;
14486
14487 return is_empty_type (TYPE_MAIN_VARIANT (type));
14488 }
14489
14490 /* Determine whether TYPE is a structure with a flexible array member,
14491 or a union containing such a structure (possibly recursively). */
14492
14493 bool
14494 flexible_array_type_p (const_tree type)
14495 {
14496 tree x, last;
14497 switch (TREE_CODE (type))
14498 {
14499 case RECORD_TYPE:
14500 last = NULL_TREE;
14501 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
14502 if (TREE_CODE (x) == FIELD_DECL)
14503 last = x;
14504 if (last == NULL_TREE)
14505 return false;
14506 if (TREE_CODE (TREE_TYPE (last)) == ARRAY_TYPE
14507 && TYPE_SIZE (TREE_TYPE (last)) == NULL_TREE
14508 && TYPE_DOMAIN (TREE_TYPE (last)) != NULL_TREE
14509 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (last))) == NULL_TREE)
14510 return true;
14511 return false;
14512 case UNION_TYPE:
14513 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
14514 {
14515 if (TREE_CODE (x) == FIELD_DECL
14516 && flexible_array_type_p (TREE_TYPE (x)))
14517 return true;
14518 }
14519 return false;
14520 default:
14521 return false;
14522 }
14523 }
14524
14525 /* Like int_size_in_bytes, but handle empty records specially. */
14526
14527 HOST_WIDE_INT
14528 arg_int_size_in_bytes (const_tree type)
14529 {
14530 return TYPE_EMPTY_P (type) ? 0 : int_size_in_bytes (type);
14531 }
14532
14533 /* Like size_in_bytes, but handle empty records specially. */
14534
14535 tree
14536 arg_size_in_bytes (const_tree type)
14537 {
14538 return TYPE_EMPTY_P (type) ? size_zero_node : size_in_bytes (type);
14539 }
14540
14541 /* Return true if an expression with CODE has to have the same result type as
14542 its first operand. */
14543
14544 bool
14545 expr_type_first_operand_type_p (tree_code code)
14546 {
14547 switch (code)
14548 {
14549 case NEGATE_EXPR:
14550 case ABS_EXPR:
14551 case BIT_NOT_EXPR:
14552 case PAREN_EXPR:
14553 case CONJ_EXPR:
14554
14555 case PLUS_EXPR:
14556 case MINUS_EXPR:
14557 case MULT_EXPR:
14558 case TRUNC_DIV_EXPR:
14559 case CEIL_DIV_EXPR:
14560 case FLOOR_DIV_EXPR:
14561 case ROUND_DIV_EXPR:
14562 case TRUNC_MOD_EXPR:
14563 case CEIL_MOD_EXPR:
14564 case FLOOR_MOD_EXPR:
14565 case ROUND_MOD_EXPR:
14566 case RDIV_EXPR:
14567 case EXACT_DIV_EXPR:
14568 case MIN_EXPR:
14569 case MAX_EXPR:
14570 case BIT_IOR_EXPR:
14571 case BIT_XOR_EXPR:
14572 case BIT_AND_EXPR:
14573
14574 case LSHIFT_EXPR:
14575 case RSHIFT_EXPR:
14576 case LROTATE_EXPR:
14577 case RROTATE_EXPR:
14578 return true;
14579
14580 default:
14581 return false;
14582 }
14583 }
14584
14585 /* Return a typenode for the "standard" C type with a given name. */
14586 tree
14587 get_typenode_from_name (const char *name)
14588 {
14589 if (name == NULL || *name == '\0')
14590 return NULL_TREE;
14591
14592 if (strcmp (name, "char") == 0)
14593 return char_type_node;
14594 if (strcmp (name, "unsigned char") == 0)
14595 return unsigned_char_type_node;
14596 if (strcmp (name, "signed char") == 0)
14597 return signed_char_type_node;
14598
14599 if (strcmp (name, "short int") == 0)
14600 return short_integer_type_node;
14601 if (strcmp (name, "short unsigned int") == 0)
14602 return short_unsigned_type_node;
14603
14604 if (strcmp (name, "int") == 0)
14605 return integer_type_node;
14606 if (strcmp (name, "unsigned int") == 0)
14607 return unsigned_type_node;
14608
14609 if (strcmp (name, "long int") == 0)
14610 return long_integer_type_node;
14611 if (strcmp (name, "long unsigned int") == 0)
14612 return long_unsigned_type_node;
14613
14614 if (strcmp (name, "long long int") == 0)
14615 return long_long_integer_type_node;
14616 if (strcmp (name, "long long unsigned int") == 0)
14617 return long_long_unsigned_type_node;
14618
14619 gcc_unreachable ();
14620 }
14621
14622 /* List of pointer types used to declare builtins before we have seen their
14623 real declaration.
14624
14625 Keep the size up to date in tree.h ! */
14626 const builtin_structptr_type builtin_structptr_types[6] =
14627 {
14628 { fileptr_type_node, ptr_type_node, "FILE" },
14629 { const_tm_ptr_type_node, const_ptr_type_node, "tm" },
14630 { fenv_t_ptr_type_node, ptr_type_node, "fenv_t" },
14631 { const_fenv_t_ptr_type_node, const_ptr_type_node, "fenv_t" },
14632 { fexcept_t_ptr_type_node, ptr_type_node, "fexcept_t" },
14633 { const_fexcept_t_ptr_type_node, const_ptr_type_node, "fexcept_t" }
14634 };
14635
14636 /* Return the maximum object size. */
14637
14638 tree
14639 max_object_size (void)
14640 {
14641 /* To do: Make this a configurable parameter. */
14642 return TYPE_MAX_VALUE (ptrdiff_type_node);
14643 }
14644
14645 /* A wrapper around TARGET_VERIFY_TYPE_CONTEXT that makes the silent_p
14646 parameter default to false and that weeds out error_mark_node. */
14647
14648 bool
14649 verify_type_context (location_t loc, type_context_kind context,
14650 const_tree type, bool silent_p)
14651 {
14652 if (type == error_mark_node)
14653 return true;
14654
14655 gcc_assert (TYPE_P (type));
14656 return (!targetm.verify_type_context
14657 || targetm.verify_type_context (loc, context, type, silent_p));
14658 }
14659
14660 /* Return true if NEW_ASM and DELETE_ASM name a valid pair of new and
14661 delete operators. Return false if they may or may not name such
14662 a pair and, when nonnull, set *PCERTAIN to true if they certainly
14663 do not. */
14664
14665 bool
14666 valid_new_delete_pair_p (tree new_asm, tree delete_asm,
14667 bool *pcertain /* = NULL */)
14668 {
14669 bool certain;
14670 if (!pcertain)
14671 pcertain = &certain;
14672
14673 const char *new_name = IDENTIFIER_POINTER (new_asm);
14674 const char *delete_name = IDENTIFIER_POINTER (delete_asm);
14675 unsigned int new_len = IDENTIFIER_LENGTH (new_asm);
14676 unsigned int delete_len = IDENTIFIER_LENGTH (delete_asm);
14677
14678 /* The following failures are due to invalid names so they're not
14679 considered certain mismatches. */
14680 *pcertain = false;
14681
14682 if (new_len < 5 || delete_len < 6)
14683 return false;
14684 if (new_name[0] == '_')
14685 ++new_name, --new_len;
14686 if (new_name[0] == '_')
14687 ++new_name, --new_len;
14688 if (delete_name[0] == '_')
14689 ++delete_name, --delete_len;
14690 if (delete_name[0] == '_')
14691 ++delete_name, --delete_len;
14692 if (new_len < 4 || delete_len < 5)
14693 return false;
14694
14695 /* The following failures are due to names of user-defined operators
14696 so they're also not considered certain mismatches. */
14697
14698 /* *_len is now just the length after initial underscores. */
14699 if (new_name[0] != 'Z' || new_name[1] != 'n')
14700 return false;
14701 if (delete_name[0] != 'Z' || delete_name[1] != 'd')
14702 return false;
14703
14704 /* The following failures are certain mismatches. */
14705 *pcertain = true;
14706
14707 /* _Znw must match _Zdl, _Zna must match _Zda. */
14708 if ((new_name[2] != 'w' || delete_name[2] != 'l')
14709 && (new_name[2] != 'a' || delete_name[2] != 'a'))
14710 return false;
14711 /* 'j', 'm' and 'y' correspond to size_t. */
14712 if (new_name[3] != 'j' && new_name[3] != 'm' && new_name[3] != 'y')
14713 return false;
14714 if (delete_name[3] != 'P' || delete_name[4] != 'v')
14715 return false;
14716 if (new_len == 4
14717 || (new_len == 18 && !memcmp (new_name + 4, "RKSt9nothrow_t", 14)))
14718 {
14719 /* _ZnXY or _ZnXYRKSt9nothrow_t matches
14720 _ZdXPv, _ZdXPvY and _ZdXPvRKSt9nothrow_t. */
14721 if (delete_len == 5)
14722 return true;
14723 if (delete_len == 6 && delete_name[5] == new_name[3])
14724 return true;
14725 if (delete_len == 19 && !memcmp (delete_name + 5, "RKSt9nothrow_t", 14))
14726 return true;
14727 }
14728 else if ((new_len == 19 && !memcmp (new_name + 4, "St11align_val_t", 15))
14729 || (new_len == 33
14730 && !memcmp (new_name + 4, "St11align_val_tRKSt9nothrow_t", 29)))
14731 {
14732 /* _ZnXYSt11align_val_t or _ZnXYSt11align_val_tRKSt9nothrow_t matches
14733 _ZdXPvSt11align_val_t or _ZdXPvYSt11align_val_t or or
14734 _ZdXPvSt11align_val_tRKSt9nothrow_t. */
14735 if (delete_len == 20 && !memcmp (delete_name + 5, "St11align_val_t", 15))
14736 return true;
14737 if (delete_len == 21
14738 && delete_name[5] == new_name[3]
14739 && !memcmp (delete_name + 6, "St11align_val_t", 15))
14740 return true;
14741 if (delete_len == 34
14742 && !memcmp (delete_name + 5, "St11align_val_tRKSt9nothrow_t", 29))
14743 return true;
14744 }
14745
14746 /* The negative result is conservative. */
14747 *pcertain = false;
14748 return false;
14749 }
14750
14751 /* Return the zero-based number corresponding to the argument being
14752 deallocated if FNDECL is a deallocation function or an out-of-bounds
14753 value if it isn't. */
14754
14755 unsigned
14756 fndecl_dealloc_argno (tree fndecl)
14757 {
14758 /* A call to operator delete isn't recognized as one to a built-in. */
14759 if (DECL_IS_OPERATOR_DELETE_P (fndecl))
14760 {
14761 if (DECL_IS_REPLACEABLE_OPERATOR (fndecl))
14762 return 0;
14763
14764 /* Avoid placement delete that's not been inlined. */
14765 tree fname = DECL_ASSEMBLER_NAME (fndecl);
14766 if (id_equal (fname, "_ZdlPvS_") // ordinary form
14767 || id_equal (fname, "_ZdaPvS_")) // array form
14768 return UINT_MAX;
14769 return 0;
14770 }
14771
14772 /* TODO: Handle user-defined functions with attribute malloc? Handle
14773 known non-built-ins like fopen? */
14774 if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
14775 {
14776 switch (DECL_FUNCTION_CODE (fndecl))
14777 {
14778 case BUILT_IN_FREE:
14779 case BUILT_IN_REALLOC:
14780 return 0;
14781 default:
14782 break;
14783 }
14784 return UINT_MAX;
14785 }
14786
14787 tree attrs = DECL_ATTRIBUTES (fndecl);
14788 if (!attrs)
14789 return UINT_MAX;
14790
14791 for (tree atfree = attrs;
14792 (atfree = lookup_attribute ("*dealloc", atfree));
14793 atfree = TREE_CHAIN (atfree))
14794 {
14795 tree alloc = TREE_VALUE (atfree);
14796 if (!alloc)
14797 continue;
14798
14799 tree pos = TREE_CHAIN (alloc);
14800 if (!pos)
14801 return 0;
14802
14803 pos = TREE_VALUE (pos);
14804 return TREE_INT_CST_LOW (pos) - 1;
14805 }
14806
14807 return UINT_MAX;
14808 }
14809
14810 /* If EXPR refers to a character array or pointer declared attribute
14811 nonstring, return a decl for that array or pointer and set *REF
14812 to the referenced enclosing object or pointer. Otherwise return
14813 null. */
14814
14815 tree
14816 get_attr_nonstring_decl (tree expr, tree *ref)
14817 {
14818 tree decl = expr;
14819 tree var = NULL_TREE;
14820 if (TREE_CODE (decl) == SSA_NAME)
14821 {
14822 gimple *def = SSA_NAME_DEF_STMT (decl);
14823
14824 if (is_gimple_assign (def))
14825 {
14826 tree_code code = gimple_assign_rhs_code (def);
14827 if (code == ADDR_EXPR
14828 || code == COMPONENT_REF
14829 || code == VAR_DECL)
14830 decl = gimple_assign_rhs1 (def);
14831 }
14832 else
14833 var = SSA_NAME_VAR (decl);
14834 }
14835
14836 if (TREE_CODE (decl) == ADDR_EXPR)
14837 decl = TREE_OPERAND (decl, 0);
14838
14839 /* To simplify calling code, store the referenced DECL regardless of
14840 the attribute determined below, but avoid storing the SSA_NAME_VAR
14841 obtained above (it's not useful for dataflow purposes). */
14842 if (ref)
14843 *ref = decl;
14844
14845 /* Use the SSA_NAME_VAR that was determined above to see if it's
14846 declared nonstring. Otherwise drill down into the referenced
14847 DECL. */
14848 if (var)
14849 decl = var;
14850 else if (TREE_CODE (decl) == ARRAY_REF)
14851 decl = TREE_OPERAND (decl, 0);
14852 else if (TREE_CODE (decl) == COMPONENT_REF)
14853 decl = TREE_OPERAND (decl, 1);
14854 else if (TREE_CODE (decl) == MEM_REF)
14855 return get_attr_nonstring_decl (TREE_OPERAND (decl, 0), ref);
14856
14857 if (DECL_P (decl)
14858 && lookup_attribute ("nonstring", DECL_ATTRIBUTES (decl)))
14859 return decl;
14860
14861 return NULL_TREE;
14862 }
14863
14864 /* Return length of attribute names string,
14865 if arglist chain > 1, -1 otherwise. */
14866
14867 int
14868 get_target_clone_attr_len (tree arglist)
14869 {
14870 tree arg;
14871 int str_len_sum = 0;
14872 int argnum = 0;
14873
14874 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
14875 {
14876 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
14877 size_t len = strlen (str);
14878 str_len_sum += len + 1;
14879 for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
14880 argnum++;
14881 argnum++;
14882 }
14883 if (argnum <= 1)
14884 return -1;
14885 return str_len_sum;
14886 }
14887
14888 void
14889 tree_cc_finalize (void)
14890 {
14891 clear_nonstandard_integer_type_cache ();
14892 }
14893
14894 #if CHECKING_P
14895
14896 namespace selftest {
14897
14898 /* Selftests for tree. */
14899
14900 /* Verify that integer constants are sane. */
14901
14902 static void
14903 test_integer_constants ()
14904 {
14905 ASSERT_TRUE (integer_type_node != NULL);
14906 ASSERT_TRUE (build_int_cst (integer_type_node, 0) != NULL);
14907
14908 tree type = integer_type_node;
14909
14910 tree zero = build_zero_cst (type);
14911 ASSERT_EQ (INTEGER_CST, TREE_CODE (zero));
14912 ASSERT_EQ (type, TREE_TYPE (zero));
14913
14914 tree one = build_int_cst (type, 1);
14915 ASSERT_EQ (INTEGER_CST, TREE_CODE (one));
14916 ASSERT_EQ (type, TREE_TYPE (zero));
14917 }
14918
14919 /* Verify identifiers. */
14920
14921 static void
14922 test_identifiers ()
14923 {
14924 tree identifier = get_identifier ("foo");
14925 ASSERT_EQ (3, IDENTIFIER_LENGTH (identifier));
14926 ASSERT_STREQ ("foo", IDENTIFIER_POINTER (identifier));
14927 }
14928
14929 /* Verify LABEL_DECL. */
14930
14931 static void
14932 test_labels ()
14933 {
14934 tree identifier = get_identifier ("err");
14935 tree label_decl = build_decl (UNKNOWN_LOCATION, LABEL_DECL,
14936 identifier, void_type_node);
14937 ASSERT_EQ (-1, LABEL_DECL_UID (label_decl));
14938 ASSERT_FALSE (FORCED_LABEL (label_decl));
14939 }
14940
14941 /* Return a new VECTOR_CST node whose type is TYPE and whose values
14942 are given by VALS. */
14943
14944 static tree
14945 build_vector (tree type, const vec<tree> &vals MEM_STAT_DECL)
14946 {
14947 gcc_assert (known_eq (vals.length (), TYPE_VECTOR_SUBPARTS (type)));
14948 tree_vector_builder builder (type, vals.length (), 1);
14949 builder.splice (vals);
14950 return builder.build ();
14951 }
14952
14953 /* Check that VECTOR_CST ACTUAL contains the elements in EXPECTED. */
14954
14955 static void
14956 check_vector_cst (const vec<tree> &expected, tree actual)
14957 {
14958 ASSERT_KNOWN_EQ (expected.length (),
14959 TYPE_VECTOR_SUBPARTS (TREE_TYPE (actual)));
14960 for (unsigned int i = 0; i < expected.length (); ++i)
14961 ASSERT_EQ (wi::to_wide (expected[i]),
14962 wi::to_wide (vector_cst_elt (actual, i)));
14963 }
14964
14965 /* Check that VECTOR_CST ACTUAL contains NPATTERNS duplicated elements,
14966 and that its elements match EXPECTED. */
14967
14968 static void
14969 check_vector_cst_duplicate (const vec<tree> &expected, tree actual,
14970 unsigned int npatterns)
14971 {
14972 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
14973 ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (actual));
14974 ASSERT_EQ (npatterns, vector_cst_encoded_nelts (actual));
14975 ASSERT_TRUE (VECTOR_CST_DUPLICATE_P (actual));
14976 ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
14977 check_vector_cst (expected, actual);
14978 }
14979
14980 /* Check that VECTOR_CST ACTUAL contains NPATTERNS foreground elements
14981 and NPATTERNS background elements, and that its elements match
14982 EXPECTED. */
14983
14984 static void
14985 check_vector_cst_fill (const vec<tree> &expected, tree actual,
14986 unsigned int npatterns)
14987 {
14988 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
14989 ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (actual));
14990 ASSERT_EQ (2 * npatterns, vector_cst_encoded_nelts (actual));
14991 ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
14992 ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
14993 check_vector_cst (expected, actual);
14994 }
14995
14996 /* Check that VECTOR_CST ACTUAL contains NPATTERNS stepped patterns,
14997 and that its elements match EXPECTED. */
14998
14999 static void
15000 check_vector_cst_stepped (const vec<tree> &expected, tree actual,
15001 unsigned int npatterns)
15002 {
15003 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
15004 ASSERT_EQ (3, VECTOR_CST_NELTS_PER_PATTERN (actual));
15005 ASSERT_EQ (3 * npatterns, vector_cst_encoded_nelts (actual));
15006 ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
15007 ASSERT_TRUE (VECTOR_CST_STEPPED_P (actual));
15008 check_vector_cst (expected, actual);
15009 }
15010
15011 /* Test the creation of VECTOR_CSTs. */
15012
15013 static void
15014 test_vector_cst_patterns (ALONE_CXX_MEM_STAT_INFO)
15015 {
15016 auto_vec<tree, 8> elements (8);
15017 elements.quick_grow (8);
15018 tree element_type = build_nonstandard_integer_type (16, true);
15019 tree vector_type = build_vector_type (element_type, 8);
15020
15021 /* Test a simple linear series with a base of 0 and a step of 1:
15022 { 0, 1, 2, 3, 4, 5, 6, 7 }. */
15023 for (unsigned int i = 0; i < 8; ++i)
15024 elements[i] = build_int_cst (element_type, i);
15025 tree vector = build_vector (vector_type, elements PASS_MEM_STAT);
15026 check_vector_cst_stepped (elements, vector, 1);
15027
15028 /* Try the same with the first element replaced by 100:
15029 { 100, 1, 2, 3, 4, 5, 6, 7 }. */
15030 elements[0] = build_int_cst (element_type, 100);
15031 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15032 check_vector_cst_stepped (elements, vector, 1);
15033
15034 /* Try a series that wraps around.
15035 { 100, 65531, 65532, 65533, 65534, 65535, 0, 1 }. */
15036 for (unsigned int i = 1; i < 8; ++i)
15037 elements[i] = build_int_cst (element_type, (65530 + i) & 0xffff);
15038 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15039 check_vector_cst_stepped (elements, vector, 1);
15040
15041 /* Try a downward series:
15042 { 100, 79, 78, 77, 76, 75, 75, 73 }. */
15043 for (unsigned int i = 1; i < 8; ++i)
15044 elements[i] = build_int_cst (element_type, 80 - i);
15045 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15046 check_vector_cst_stepped (elements, vector, 1);
15047
15048 /* Try two interleaved series with different bases and steps:
15049 { 100, 53, 66, 206, 62, 212, 58, 218 }. */
15050 elements[1] = build_int_cst (element_type, 53);
15051 for (unsigned int i = 2; i < 8; i += 2)
15052 {
15053 elements[i] = build_int_cst (element_type, 70 - i * 2);
15054 elements[i + 1] = build_int_cst (element_type, 200 + i * 3);
15055 }
15056 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15057 check_vector_cst_stepped (elements, vector, 2);
15058
15059 /* Try a duplicated value:
15060 { 100, 100, 100, 100, 100, 100, 100, 100 }. */
15061 for (unsigned int i = 1; i < 8; ++i)
15062 elements[i] = elements[0];
15063 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15064 check_vector_cst_duplicate (elements, vector, 1);
15065
15066 /* Try an interleaved duplicated value:
15067 { 100, 55, 100, 55, 100, 55, 100, 55 }. */
15068 elements[1] = build_int_cst (element_type, 55);
15069 for (unsigned int i = 2; i < 8; ++i)
15070 elements[i] = elements[i - 2];
15071 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15072 check_vector_cst_duplicate (elements, vector, 2);
15073
15074 /* Try a duplicated value with 2 exceptions
15075 { 41, 97, 100, 55, 100, 55, 100, 55 }. */
15076 elements[0] = build_int_cst (element_type, 41);
15077 elements[1] = build_int_cst (element_type, 97);
15078 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15079 check_vector_cst_fill (elements, vector, 2);
15080
15081 /* Try with and without a step
15082 { 41, 97, 100, 21, 100, 35, 100, 49 }. */
15083 for (unsigned int i = 3; i < 8; i += 2)
15084 elements[i] = build_int_cst (element_type, i * 7);
15085 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15086 check_vector_cst_stepped (elements, vector, 2);
15087
15088 /* Try a fully-general constant:
15089 { 41, 97, 100, 21, 100, 9990, 100, 49 }. */
15090 elements[5] = build_int_cst (element_type, 9990);
15091 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15092 check_vector_cst_fill (elements, vector, 4);
15093 }
15094
15095 /* Verify that STRIP_NOPS (NODE) is EXPECTED.
15096 Helper function for test_location_wrappers, to deal with STRIP_NOPS
15097 modifying its argument in-place. */
15098
15099 static void
15100 check_strip_nops (tree node, tree expected)
15101 {
15102 STRIP_NOPS (node);
15103 ASSERT_EQ (expected, node);
15104 }
15105
15106 /* Verify location wrappers. */
15107
15108 static void
15109 test_location_wrappers ()
15110 {
15111 location_t loc = BUILTINS_LOCATION;
15112
15113 ASSERT_EQ (NULL_TREE, maybe_wrap_with_location (NULL_TREE, loc));
15114
15115 /* Wrapping a constant. */
15116 tree int_cst = build_int_cst (integer_type_node, 42);
15117 ASSERT_FALSE (CAN_HAVE_LOCATION_P (int_cst));
15118 ASSERT_FALSE (location_wrapper_p (int_cst));
15119
15120 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
15121 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
15122 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_int_cst));
15123 ASSERT_EQ (int_cst, tree_strip_any_location_wrapper (wrapped_int_cst));
15124
15125 /* We shouldn't add wrapper nodes for UNKNOWN_LOCATION. */
15126 ASSERT_EQ (int_cst, maybe_wrap_with_location (int_cst, UNKNOWN_LOCATION));
15127
15128 /* We shouldn't add wrapper nodes for nodes that CAN_HAVE_LOCATION_P. */
15129 tree cast = build1 (NOP_EXPR, char_type_node, int_cst);
15130 ASSERT_TRUE (CAN_HAVE_LOCATION_P (cast));
15131 ASSERT_EQ (cast, maybe_wrap_with_location (cast, loc));
15132
15133 /* Wrapping a STRING_CST. */
15134 tree string_cst = build_string (4, "foo");
15135 ASSERT_FALSE (CAN_HAVE_LOCATION_P (string_cst));
15136 ASSERT_FALSE (location_wrapper_p (string_cst));
15137
15138 tree wrapped_string_cst = maybe_wrap_with_location (string_cst, loc);
15139 ASSERT_TRUE (location_wrapper_p (wrapped_string_cst));
15140 ASSERT_EQ (VIEW_CONVERT_EXPR, TREE_CODE (wrapped_string_cst));
15141 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_string_cst));
15142 ASSERT_EQ (string_cst, tree_strip_any_location_wrapper (wrapped_string_cst));
15143
15144
15145 /* Wrapping a variable. */
15146 tree int_var = build_decl (UNKNOWN_LOCATION, VAR_DECL,
15147 get_identifier ("some_int_var"),
15148 integer_type_node);
15149 ASSERT_FALSE (CAN_HAVE_LOCATION_P (int_var));
15150 ASSERT_FALSE (location_wrapper_p (int_var));
15151
15152 tree wrapped_int_var = maybe_wrap_with_location (int_var, loc);
15153 ASSERT_TRUE (location_wrapper_p (wrapped_int_var));
15154 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_int_var));
15155 ASSERT_EQ (int_var, tree_strip_any_location_wrapper (wrapped_int_var));
15156
15157 /* Verify that "reinterpret_cast<int>(some_int_var)" is not a location
15158 wrapper. */
15159 tree r_cast = build1 (NON_LVALUE_EXPR, integer_type_node, int_var);
15160 ASSERT_FALSE (location_wrapper_p (r_cast));
15161 ASSERT_EQ (r_cast, tree_strip_any_location_wrapper (r_cast));
15162
15163 /* Verify that STRIP_NOPS removes wrappers. */
15164 check_strip_nops (wrapped_int_cst, int_cst);
15165 check_strip_nops (wrapped_string_cst, string_cst);
15166 check_strip_nops (wrapped_int_var, int_var);
15167 }
15168
15169 /* Test various tree predicates. Verify that location wrappers don't
15170 affect the results. */
15171
15172 static void
15173 test_predicates ()
15174 {
15175 /* Build various constants and wrappers around them. */
15176
15177 location_t loc = BUILTINS_LOCATION;
15178
15179 tree i_0 = build_int_cst (integer_type_node, 0);
15180 tree wr_i_0 = maybe_wrap_with_location (i_0, loc);
15181
15182 tree i_1 = build_int_cst (integer_type_node, 1);
15183 tree wr_i_1 = maybe_wrap_with_location (i_1, loc);
15184
15185 tree i_m1 = build_int_cst (integer_type_node, -1);
15186 tree wr_i_m1 = maybe_wrap_with_location (i_m1, loc);
15187
15188 tree f_0 = build_real_from_int_cst (float_type_node, i_0);
15189 tree wr_f_0 = maybe_wrap_with_location (f_0, loc);
15190 tree f_1 = build_real_from_int_cst (float_type_node, i_1);
15191 tree wr_f_1 = maybe_wrap_with_location (f_1, loc);
15192 tree f_m1 = build_real_from_int_cst (float_type_node, i_m1);
15193 tree wr_f_m1 = maybe_wrap_with_location (f_m1, loc);
15194
15195 tree c_i_0 = build_complex (NULL_TREE, i_0, i_0);
15196 tree c_i_1 = build_complex (NULL_TREE, i_1, i_0);
15197 tree c_i_m1 = build_complex (NULL_TREE, i_m1, i_0);
15198
15199 tree c_f_0 = build_complex (NULL_TREE, f_0, f_0);
15200 tree c_f_1 = build_complex (NULL_TREE, f_1, f_0);
15201 tree c_f_m1 = build_complex (NULL_TREE, f_m1, f_0);
15202
15203 /* TODO: vector constants. */
15204
15205 /* Test integer_onep. */
15206 ASSERT_FALSE (integer_onep (i_0));
15207 ASSERT_FALSE (integer_onep (wr_i_0));
15208 ASSERT_TRUE (integer_onep (i_1));
15209 ASSERT_TRUE (integer_onep (wr_i_1));
15210 ASSERT_FALSE (integer_onep (i_m1));
15211 ASSERT_FALSE (integer_onep (wr_i_m1));
15212 ASSERT_FALSE (integer_onep (f_0));
15213 ASSERT_FALSE (integer_onep (wr_f_0));
15214 ASSERT_FALSE (integer_onep (f_1));
15215 ASSERT_FALSE (integer_onep (wr_f_1));
15216 ASSERT_FALSE (integer_onep (f_m1));
15217 ASSERT_FALSE (integer_onep (wr_f_m1));
15218 ASSERT_FALSE (integer_onep (c_i_0));
15219 ASSERT_TRUE (integer_onep (c_i_1));
15220 ASSERT_FALSE (integer_onep (c_i_m1));
15221 ASSERT_FALSE (integer_onep (c_f_0));
15222 ASSERT_FALSE (integer_onep (c_f_1));
15223 ASSERT_FALSE (integer_onep (c_f_m1));
15224
15225 /* Test integer_zerop. */
15226 ASSERT_TRUE (integer_zerop (i_0));
15227 ASSERT_TRUE (integer_zerop (wr_i_0));
15228 ASSERT_FALSE (integer_zerop (i_1));
15229 ASSERT_FALSE (integer_zerop (wr_i_1));
15230 ASSERT_FALSE (integer_zerop (i_m1));
15231 ASSERT_FALSE (integer_zerop (wr_i_m1));
15232 ASSERT_FALSE (integer_zerop (f_0));
15233 ASSERT_FALSE (integer_zerop (wr_f_0));
15234 ASSERT_FALSE (integer_zerop (f_1));
15235 ASSERT_FALSE (integer_zerop (wr_f_1));
15236 ASSERT_FALSE (integer_zerop (f_m1));
15237 ASSERT_FALSE (integer_zerop (wr_f_m1));
15238 ASSERT_TRUE (integer_zerop (c_i_0));
15239 ASSERT_FALSE (integer_zerop (c_i_1));
15240 ASSERT_FALSE (integer_zerop (c_i_m1));
15241 ASSERT_FALSE (integer_zerop (c_f_0));
15242 ASSERT_FALSE (integer_zerop (c_f_1));
15243 ASSERT_FALSE (integer_zerop (c_f_m1));
15244
15245 /* Test integer_all_onesp. */
15246 ASSERT_FALSE (integer_all_onesp (i_0));
15247 ASSERT_FALSE (integer_all_onesp (wr_i_0));
15248 ASSERT_FALSE (integer_all_onesp (i_1));
15249 ASSERT_FALSE (integer_all_onesp (wr_i_1));
15250 ASSERT_TRUE (integer_all_onesp (i_m1));
15251 ASSERT_TRUE (integer_all_onesp (wr_i_m1));
15252 ASSERT_FALSE (integer_all_onesp (f_0));
15253 ASSERT_FALSE (integer_all_onesp (wr_f_0));
15254 ASSERT_FALSE (integer_all_onesp (f_1));
15255 ASSERT_FALSE (integer_all_onesp (wr_f_1));
15256 ASSERT_FALSE (integer_all_onesp (f_m1));
15257 ASSERT_FALSE (integer_all_onesp (wr_f_m1));
15258 ASSERT_FALSE (integer_all_onesp (c_i_0));
15259 ASSERT_FALSE (integer_all_onesp (c_i_1));
15260 ASSERT_FALSE (integer_all_onesp (c_i_m1));
15261 ASSERT_FALSE (integer_all_onesp (c_f_0));
15262 ASSERT_FALSE (integer_all_onesp (c_f_1));
15263 ASSERT_FALSE (integer_all_onesp (c_f_m1));
15264
15265 /* Test integer_minus_onep. */
15266 ASSERT_FALSE (integer_minus_onep (i_0));
15267 ASSERT_FALSE (integer_minus_onep (wr_i_0));
15268 ASSERT_FALSE (integer_minus_onep (i_1));
15269 ASSERT_FALSE (integer_minus_onep (wr_i_1));
15270 ASSERT_TRUE (integer_minus_onep (i_m1));
15271 ASSERT_TRUE (integer_minus_onep (wr_i_m1));
15272 ASSERT_FALSE (integer_minus_onep (f_0));
15273 ASSERT_FALSE (integer_minus_onep (wr_f_0));
15274 ASSERT_FALSE (integer_minus_onep (f_1));
15275 ASSERT_FALSE (integer_minus_onep (wr_f_1));
15276 ASSERT_FALSE (integer_minus_onep (f_m1));
15277 ASSERT_FALSE (integer_minus_onep (wr_f_m1));
15278 ASSERT_FALSE (integer_minus_onep (c_i_0));
15279 ASSERT_FALSE (integer_minus_onep (c_i_1));
15280 ASSERT_TRUE (integer_minus_onep (c_i_m1));
15281 ASSERT_FALSE (integer_minus_onep (c_f_0));
15282 ASSERT_FALSE (integer_minus_onep (c_f_1));
15283 ASSERT_FALSE (integer_minus_onep (c_f_m1));
15284
15285 /* Test integer_each_onep. */
15286 ASSERT_FALSE (integer_each_onep (i_0));
15287 ASSERT_FALSE (integer_each_onep (wr_i_0));
15288 ASSERT_TRUE (integer_each_onep (i_1));
15289 ASSERT_TRUE (integer_each_onep (wr_i_1));
15290 ASSERT_FALSE (integer_each_onep (i_m1));
15291 ASSERT_FALSE (integer_each_onep (wr_i_m1));
15292 ASSERT_FALSE (integer_each_onep (f_0));
15293 ASSERT_FALSE (integer_each_onep (wr_f_0));
15294 ASSERT_FALSE (integer_each_onep (f_1));
15295 ASSERT_FALSE (integer_each_onep (wr_f_1));
15296 ASSERT_FALSE (integer_each_onep (f_m1));
15297 ASSERT_FALSE (integer_each_onep (wr_f_m1));
15298 ASSERT_FALSE (integer_each_onep (c_i_0));
15299 ASSERT_FALSE (integer_each_onep (c_i_1));
15300 ASSERT_FALSE (integer_each_onep (c_i_m1));
15301 ASSERT_FALSE (integer_each_onep (c_f_0));
15302 ASSERT_FALSE (integer_each_onep (c_f_1));
15303 ASSERT_FALSE (integer_each_onep (c_f_m1));
15304
15305 /* Test integer_truep. */
15306 ASSERT_FALSE (integer_truep (i_0));
15307 ASSERT_FALSE (integer_truep (wr_i_0));
15308 ASSERT_TRUE (integer_truep (i_1));
15309 ASSERT_TRUE (integer_truep (wr_i_1));
15310 ASSERT_FALSE (integer_truep (i_m1));
15311 ASSERT_FALSE (integer_truep (wr_i_m1));
15312 ASSERT_FALSE (integer_truep (f_0));
15313 ASSERT_FALSE (integer_truep (wr_f_0));
15314 ASSERT_FALSE (integer_truep (f_1));
15315 ASSERT_FALSE (integer_truep (wr_f_1));
15316 ASSERT_FALSE (integer_truep (f_m1));
15317 ASSERT_FALSE (integer_truep (wr_f_m1));
15318 ASSERT_FALSE (integer_truep (c_i_0));
15319 ASSERT_TRUE (integer_truep (c_i_1));
15320 ASSERT_FALSE (integer_truep (c_i_m1));
15321 ASSERT_FALSE (integer_truep (c_f_0));
15322 ASSERT_FALSE (integer_truep (c_f_1));
15323 ASSERT_FALSE (integer_truep (c_f_m1));
15324
15325 /* Test integer_nonzerop. */
15326 ASSERT_FALSE (integer_nonzerop (i_0));
15327 ASSERT_FALSE (integer_nonzerop (wr_i_0));
15328 ASSERT_TRUE (integer_nonzerop (i_1));
15329 ASSERT_TRUE (integer_nonzerop (wr_i_1));
15330 ASSERT_TRUE (integer_nonzerop (i_m1));
15331 ASSERT_TRUE (integer_nonzerop (wr_i_m1));
15332 ASSERT_FALSE (integer_nonzerop (f_0));
15333 ASSERT_FALSE (integer_nonzerop (wr_f_0));
15334 ASSERT_FALSE (integer_nonzerop (f_1));
15335 ASSERT_FALSE (integer_nonzerop (wr_f_1));
15336 ASSERT_FALSE (integer_nonzerop (f_m1));
15337 ASSERT_FALSE (integer_nonzerop (wr_f_m1));
15338 ASSERT_FALSE (integer_nonzerop (c_i_0));
15339 ASSERT_TRUE (integer_nonzerop (c_i_1));
15340 ASSERT_TRUE (integer_nonzerop (c_i_m1));
15341 ASSERT_FALSE (integer_nonzerop (c_f_0));
15342 ASSERT_FALSE (integer_nonzerop (c_f_1));
15343 ASSERT_FALSE (integer_nonzerop (c_f_m1));
15344
15345 /* Test real_zerop. */
15346 ASSERT_FALSE (real_zerop (i_0));
15347 ASSERT_FALSE (real_zerop (wr_i_0));
15348 ASSERT_FALSE (real_zerop (i_1));
15349 ASSERT_FALSE (real_zerop (wr_i_1));
15350 ASSERT_FALSE (real_zerop (i_m1));
15351 ASSERT_FALSE (real_zerop (wr_i_m1));
15352 ASSERT_TRUE (real_zerop (f_0));
15353 ASSERT_TRUE (real_zerop (wr_f_0));
15354 ASSERT_FALSE (real_zerop (f_1));
15355 ASSERT_FALSE (real_zerop (wr_f_1));
15356 ASSERT_FALSE (real_zerop (f_m1));
15357 ASSERT_FALSE (real_zerop (wr_f_m1));
15358 ASSERT_FALSE (real_zerop (c_i_0));
15359 ASSERT_FALSE (real_zerop (c_i_1));
15360 ASSERT_FALSE (real_zerop (c_i_m1));
15361 ASSERT_TRUE (real_zerop (c_f_0));
15362 ASSERT_FALSE (real_zerop (c_f_1));
15363 ASSERT_FALSE (real_zerop (c_f_m1));
15364
15365 /* Test real_onep. */
15366 ASSERT_FALSE (real_onep (i_0));
15367 ASSERT_FALSE (real_onep (wr_i_0));
15368 ASSERT_FALSE (real_onep (i_1));
15369 ASSERT_FALSE (real_onep (wr_i_1));
15370 ASSERT_FALSE (real_onep (i_m1));
15371 ASSERT_FALSE (real_onep (wr_i_m1));
15372 ASSERT_FALSE (real_onep (f_0));
15373 ASSERT_FALSE (real_onep (wr_f_0));
15374 ASSERT_TRUE (real_onep (f_1));
15375 ASSERT_TRUE (real_onep (wr_f_1));
15376 ASSERT_FALSE (real_onep (f_m1));
15377 ASSERT_FALSE (real_onep (wr_f_m1));
15378 ASSERT_FALSE (real_onep (c_i_0));
15379 ASSERT_FALSE (real_onep (c_i_1));
15380 ASSERT_FALSE (real_onep (c_i_m1));
15381 ASSERT_FALSE (real_onep (c_f_0));
15382 ASSERT_TRUE (real_onep (c_f_1));
15383 ASSERT_FALSE (real_onep (c_f_m1));
15384
15385 /* Test real_minus_onep. */
15386 ASSERT_FALSE (real_minus_onep (i_0));
15387 ASSERT_FALSE (real_minus_onep (wr_i_0));
15388 ASSERT_FALSE (real_minus_onep (i_1));
15389 ASSERT_FALSE (real_minus_onep (wr_i_1));
15390 ASSERT_FALSE (real_minus_onep (i_m1));
15391 ASSERT_FALSE (real_minus_onep (wr_i_m1));
15392 ASSERT_FALSE (real_minus_onep (f_0));
15393 ASSERT_FALSE (real_minus_onep (wr_f_0));
15394 ASSERT_FALSE (real_minus_onep (f_1));
15395 ASSERT_FALSE (real_minus_onep (wr_f_1));
15396 ASSERT_TRUE (real_minus_onep (f_m1));
15397 ASSERT_TRUE (real_minus_onep (wr_f_m1));
15398 ASSERT_FALSE (real_minus_onep (c_i_0));
15399 ASSERT_FALSE (real_minus_onep (c_i_1));
15400 ASSERT_FALSE (real_minus_onep (c_i_m1));
15401 ASSERT_FALSE (real_minus_onep (c_f_0));
15402 ASSERT_FALSE (real_minus_onep (c_f_1));
15403 ASSERT_TRUE (real_minus_onep (c_f_m1));
15404
15405 /* Test zerop. */
15406 ASSERT_TRUE (zerop (i_0));
15407 ASSERT_TRUE (zerop (wr_i_0));
15408 ASSERT_FALSE (zerop (i_1));
15409 ASSERT_FALSE (zerop (wr_i_1));
15410 ASSERT_FALSE (zerop (i_m1));
15411 ASSERT_FALSE (zerop (wr_i_m1));
15412 ASSERT_TRUE (zerop (f_0));
15413 ASSERT_TRUE (zerop (wr_f_0));
15414 ASSERT_FALSE (zerop (f_1));
15415 ASSERT_FALSE (zerop (wr_f_1));
15416 ASSERT_FALSE (zerop (f_m1));
15417 ASSERT_FALSE (zerop (wr_f_m1));
15418 ASSERT_TRUE (zerop (c_i_0));
15419 ASSERT_FALSE (zerop (c_i_1));
15420 ASSERT_FALSE (zerop (c_i_m1));
15421 ASSERT_TRUE (zerop (c_f_0));
15422 ASSERT_FALSE (zerop (c_f_1));
15423 ASSERT_FALSE (zerop (c_f_m1));
15424
15425 /* Test tree_expr_nonnegative_p. */
15426 ASSERT_TRUE (tree_expr_nonnegative_p (i_0));
15427 ASSERT_TRUE (tree_expr_nonnegative_p (wr_i_0));
15428 ASSERT_TRUE (tree_expr_nonnegative_p (i_1));
15429 ASSERT_TRUE (tree_expr_nonnegative_p (wr_i_1));
15430 ASSERT_FALSE (tree_expr_nonnegative_p (i_m1));
15431 ASSERT_FALSE (tree_expr_nonnegative_p (wr_i_m1));
15432 ASSERT_TRUE (tree_expr_nonnegative_p (f_0));
15433 ASSERT_TRUE (tree_expr_nonnegative_p (wr_f_0));
15434 ASSERT_TRUE (tree_expr_nonnegative_p (f_1));
15435 ASSERT_TRUE (tree_expr_nonnegative_p (wr_f_1));
15436 ASSERT_FALSE (tree_expr_nonnegative_p (f_m1));
15437 ASSERT_FALSE (tree_expr_nonnegative_p (wr_f_m1));
15438 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_0));
15439 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_1));
15440 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_m1));
15441 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_0));
15442 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_1));
15443 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_m1));
15444
15445 /* Test tree_expr_nonzero_p. */
15446 ASSERT_FALSE (tree_expr_nonzero_p (i_0));
15447 ASSERT_FALSE (tree_expr_nonzero_p (wr_i_0));
15448 ASSERT_TRUE (tree_expr_nonzero_p (i_1));
15449 ASSERT_TRUE (tree_expr_nonzero_p (wr_i_1));
15450 ASSERT_TRUE (tree_expr_nonzero_p (i_m1));
15451 ASSERT_TRUE (tree_expr_nonzero_p (wr_i_m1));
15452
15453 /* Test integer_valued_real_p. */
15454 ASSERT_FALSE (integer_valued_real_p (i_0));
15455 ASSERT_TRUE (integer_valued_real_p (f_0));
15456 ASSERT_TRUE (integer_valued_real_p (wr_f_0));
15457 ASSERT_TRUE (integer_valued_real_p (f_1));
15458 ASSERT_TRUE (integer_valued_real_p (wr_f_1));
15459
15460 /* Test integer_pow2p. */
15461 ASSERT_FALSE (integer_pow2p (i_0));
15462 ASSERT_TRUE (integer_pow2p (i_1));
15463 ASSERT_TRUE (integer_pow2p (wr_i_1));
15464
15465 /* Test uniform_integer_cst_p. */
15466 ASSERT_TRUE (uniform_integer_cst_p (i_0));
15467 ASSERT_TRUE (uniform_integer_cst_p (wr_i_0));
15468 ASSERT_TRUE (uniform_integer_cst_p (i_1));
15469 ASSERT_TRUE (uniform_integer_cst_p (wr_i_1));
15470 ASSERT_TRUE (uniform_integer_cst_p (i_m1));
15471 ASSERT_TRUE (uniform_integer_cst_p (wr_i_m1));
15472 ASSERT_FALSE (uniform_integer_cst_p (f_0));
15473 ASSERT_FALSE (uniform_integer_cst_p (wr_f_0));
15474 ASSERT_FALSE (uniform_integer_cst_p (f_1));
15475 ASSERT_FALSE (uniform_integer_cst_p (wr_f_1));
15476 ASSERT_FALSE (uniform_integer_cst_p (f_m1));
15477 ASSERT_FALSE (uniform_integer_cst_p (wr_f_m1));
15478 ASSERT_FALSE (uniform_integer_cst_p (c_i_0));
15479 ASSERT_FALSE (uniform_integer_cst_p (c_i_1));
15480 ASSERT_FALSE (uniform_integer_cst_p (c_i_m1));
15481 ASSERT_FALSE (uniform_integer_cst_p (c_f_0));
15482 ASSERT_FALSE (uniform_integer_cst_p (c_f_1));
15483 ASSERT_FALSE (uniform_integer_cst_p (c_f_m1));
15484 }
15485
15486 /* Check that string escaping works correctly. */
15487
15488 static void
15489 test_escaped_strings (void)
15490 {
15491 int saved_cutoff;
15492 escaped_string msg;
15493
15494 msg.escape (NULL);
15495 /* ASSERT_STREQ does not accept NULL as a valid test
15496 result, so we have to use ASSERT_EQ instead. */
15497 ASSERT_EQ (NULL, (const char *) msg);
15498
15499 msg.escape ("");
15500 ASSERT_STREQ ("", (const char *) msg);
15501
15502 msg.escape ("foobar");
15503 ASSERT_STREQ ("foobar", (const char *) msg);
15504
15505 /* Ensure that we have -fmessage-length set to 0. */
15506 saved_cutoff = pp_line_cutoff (global_dc->printer);
15507 pp_line_cutoff (global_dc->printer) = 0;
15508
15509 msg.escape ("foo\nbar");
15510 ASSERT_STREQ ("foo\\nbar", (const char *) msg);
15511
15512 msg.escape ("\a\b\f\n\r\t\v");
15513 ASSERT_STREQ ("\\a\\b\\f\\n\\r\\t\\v", (const char *) msg);
15514
15515 /* Now repeat the tests with -fmessage-length set to 5. */
15516 pp_line_cutoff (global_dc->printer) = 5;
15517
15518 /* Note that the newline is not translated into an escape. */
15519 msg.escape ("foo\nbar");
15520 ASSERT_STREQ ("foo\nbar", (const char *) msg);
15521
15522 msg.escape ("\a\b\f\n\r\t\v");
15523 ASSERT_STREQ ("\\a\\b\\f\n\\r\\t\\v", (const char *) msg);
15524
15525 /* Restore the original message length setting. */
15526 pp_line_cutoff (global_dc->printer) = saved_cutoff;
15527 }
15528
15529 /* Run all of the selftests within this file. */
15530
15531 void
15532 tree_cc_tests ()
15533 {
15534 test_integer_constants ();
15535 test_identifiers ();
15536 test_labels ();
15537 test_vector_cst_patterns ();
15538 test_location_wrappers ();
15539 test_predicates ();
15540 test_escaped_strings ();
15541 }
15542
15543 } // namespace selftest
15544
15545 #endif /* CHECKING_P */
15546
15547 #include "gt-tree.h"