Debian Codegraph @

gcc/tree.cc

raw
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2025 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 /* Names of tree components.
78 Used for printing out the tree and error messages. */
79 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
80 #define END_OF_BASE_TREE_CODES "@dummy",
81
82 static const char *const tree_code_name[] = {
83 #include "all-tree.def"
84 };
85
86 #undef DEFTREECODE
87 #undef END_OF_BASE_TREE_CODES
88
89 /* Each tree code class has an associated string representation.
90 These must correspond to the tree_code_class entries. */
91
92 const char *const tree_code_class_strings[] =
93 {
94 "exceptional",
95 "constant",
96 "type",
97 "declaration",
98 "reference",
99 "comparison",
100 "unary",
101 "binary",
102 "statement",
103 "vl_exp",
104 "expression"
105 };
106
107 /* obstack.[ch] explicitly declined to prototype this. */
108 extern int _obstack_allocated_p (struct obstack *h, void *obj);
109
110 /* Statistics-gathering stuff. */
111
112 static uint64_t tree_code_counts[MAX_TREE_CODES];
113 uint64_t tree_node_counts[(int) all_kinds];
114 uint64_t tree_node_sizes[(int) all_kinds];
115
116 /* Keep in sync with tree.h:enum tree_node_kind. */
117 static const char * const tree_node_kind_names[] = {
118 "decls",
119 "types",
120 "blocks",
121 "stmts",
122 "refs",
123 "exprs",
124 "constants",
125 "identifiers",
126 "vecs",
127 "binfos",
128 "ssa names",
129 "constructors",
130 "random kinds",
131 "lang_decl kinds",
132 "lang_type kinds",
133 "omp clauses",
134 };
135
136 /* Unique id for next decl created. */
137 static GTY(()) int next_decl_uid;
138 /* Unique id for next type created. */
139 static GTY(()) unsigned next_type_uid = 1;
140 /* Unique id for next debug decl created. Use negative numbers,
141 to catch erroneous uses. */
142 static GTY(()) int next_debug_decl_uid;
143
144 /* Since we cannot rehash a type after it is in the table, we have to
145 keep the hash code. */
146
147 struct GTY((for_user)) type_hash {
148 unsigned long hash;
149 tree type;
150 };
151
152 /* Initial size of the hash table (rounded to next prime). */
153 #define TYPE_HASH_INITIAL_SIZE 1000
154
155 struct type_cache_hasher : ggc_cache_ptr_hash<type_hash>
156 {
157 static hashval_t hash (type_hash *t) { return t->hash; }
158 static bool equal (type_hash *a, type_hash *b);
159
160 static int
161 keep_cache_entry (type_hash *&t)
162 {
163 return ggc_marked_p (t->type);
164 }
165 };
166
167 /* Now here is the hash table. When recording a type, it is added to
168 the slot whose index is the hash code. Note that the hash table is
169 used for several kinds of types (function types, array types and
170 array index range types, for now). While all these live in the
171 same table, they are completely independent, and the hash code is
172 computed differently for each of these. */
173
174 static GTY ((cache)) hash_table<type_cache_hasher> *type_hash_table;
175
176 /* Hash table and temporary node for larger integer const values. */
177 static GTY (()) tree int_cst_node;
178
179 struct int_cst_hasher : ggc_cache_ptr_hash<tree_node>
180 {
181 static hashval_t hash (tree t);
182 static bool equal (tree x, tree y);
183 };
184
185 static GTY ((cache)) hash_table<int_cst_hasher> *int_cst_hash_table;
186
187 /* Class and variable for making sure that there is a single POLY_INT_CST
188 for a given value. */
189 struct poly_int_cst_hasher : ggc_cache_ptr_hash<tree_node>
190 {
191 typedef std::pair<tree, const poly_wide_int *> compare_type;
192 static hashval_t hash (tree t);
193 static bool equal (tree x, const compare_type &y);
194 };
195
196 static GTY ((cache)) hash_table<poly_int_cst_hasher> *poly_int_cst_hash_table;
197
198 /* Hash table for optimization flags and target option flags. Use the same
199 hash table for both sets of options. Nodes for building the current
200 optimization and target option nodes. The assumption is most of the time
201 the options created will already be in the hash table, so we avoid
202 allocating and freeing up a node repeatably. */
203 static GTY (()) tree cl_optimization_node;
204 static GTY (()) tree cl_target_option_node;
205
206 struct cl_option_hasher : ggc_cache_ptr_hash<tree_node>
207 {
208 static hashval_t hash (tree t);
209 static bool equal (tree x, tree y);
210 };
211
212 static GTY ((cache)) hash_table<cl_option_hasher> *cl_option_hash_table;
213
214 struct gt_value_expr_mark_data {
215 hash_set<tree> pset;
216 auto_vec<tree, 16> to_mark;
217 };
218
219 /* Callback called through walk_tree_1 to discover DECL_HAS_VALUE_EXPR_P
220 VAR_DECLs which weren't marked yet, in that case marks them and
221 walks their DECL_VALUE_EXPR expressions. */
222
223 static tree
224 gt_value_expr_mark_2 (tree *tp, int *, void *data)
225 {
226 tree t = *tp;
227 if (VAR_P (t) && DECL_HAS_VALUE_EXPR_P (t) && !ggc_marked_p (t))
228 {
229 tree dve = DECL_VALUE_EXPR (t);
230 gt_value_expr_mark_data *d = (gt_value_expr_mark_data *) data;
231 walk_tree_1 (&dve, gt_value_expr_mark_2, data, &d->pset, NULL);
232 d->to_mark.safe_push (t);
233 }
234 return NULL_TREE;
235 }
236
237 /* Callback called through traverse_noresize on the
238 value_expr_for_decl hash table. */
239
240 int
241 gt_value_expr_mark_1 (tree_decl_map **e, gt_value_expr_mark_data *data)
242 {
243 if (ggc_marked_p ((*e)->base.from))
244 walk_tree_1 (&(*e)->to, gt_value_expr_mark_2, data, &data->pset, NULL);
245 return 1;
246 }
247
248 /* The value_expr_for_decl hash table can have mappings for trees
249 which are only referenced from mappings of other trees in the
250 same table, see PR118790. Without this routine, gt_cleare_cache
251 could clear hash table slot of a tree which isn't marked but
252 will be marked when processing later hash table slot of another
253 tree which is marked. This function marks with the above
254 helpers marks all the not yet marked DECL_HAS_VALUE_EXPR_P
255 VAR_DECLs mentioned in DECL_VALUE_EXPR expressions of marked
256 trees and in that case also recurses on their DECL_VALUE_EXPR. */
257
258 void
259 gt_value_expr_mark (hash_table<tree_decl_map_cache_hasher> *h)
260 {
261 if (!h)
262 return;
263
264 gt_value_expr_mark_data data;
265 h->traverse_noresize<gt_value_expr_mark_data *,
266 gt_value_expr_mark_1> (&data);
267 for (auto v : data.to_mark)
268 gt_ggc_mx (v);
269 }
270
271 /* General tree->tree mapping structure for use in hash tables. */
272
273
274 static GTY ((cache))
275 hash_table<tree_decl_map_cache_hasher> *debug_expr_for_decl;
276
277 static GTY ((cache ("gt_value_expr_mark")))
278 hash_table<tree_decl_map_cache_hasher> *value_expr_for_decl;
279
280 static GTY ((cache))
281 hash_table<tree_vec_map_cache_hasher> *debug_args_for_decl;
282
283 static void set_type_quals (tree, int);
284 static void print_type_hash_statistics (void);
285 static void print_debug_expr_statistics (void);
286 static void print_value_expr_statistics (void);
287
288 tree global_trees[TI_MAX];
289 tree integer_types[itk_none];
290
291 bool int_n_enabled_p[NUM_INT_N_ENTS];
292 struct int_n_trees_t int_n_trees [NUM_INT_N_ENTS];
293
294 bool tree_contains_struct[MAX_TREE_CODES][64];
295
296 /* Number of operands for each OMP clause. */
297 unsigned const char omp_clause_num_ops[] =
298 {
299 0, /* OMP_CLAUSE_ERROR */
300 1, /* OMP_CLAUSE_PRIVATE */
301 1, /* OMP_CLAUSE_SHARED */
302 1, /* OMP_CLAUSE_FIRSTPRIVATE */
303 2, /* OMP_CLAUSE_LASTPRIVATE */
304 5, /* OMP_CLAUSE_REDUCTION */
305 5, /* OMP_CLAUSE_TASK_REDUCTION */
306 5, /* OMP_CLAUSE_IN_REDUCTION */
307 1, /* OMP_CLAUSE_COPYIN */
308 1, /* OMP_CLAUSE_COPYPRIVATE */
309 3, /* OMP_CLAUSE_LINEAR */
310 1, /* OMP_CLAUSE_AFFINITY */
311 2, /* OMP_CLAUSE_ALIGNED */
312 3, /* OMP_CLAUSE_ALLOCATE */
313 1, /* OMP_CLAUSE_DEPEND */
314 1, /* OMP_CLAUSE_NONTEMPORAL */
315 1, /* OMP_CLAUSE_UNIFORM */
316 1, /* OMP_CLAUSE_ENTER */
317 1, /* OMP_CLAUSE_LINK */
318 1, /* OMP_CLAUSE_DETACH */
319 1, /* OMP_CLAUSE_USE_DEVICE_PTR */
320 1, /* OMP_CLAUSE_USE_DEVICE_ADDR */
321 1, /* OMP_CLAUSE_IS_DEVICE_PTR */
322 1, /* OMP_CLAUSE_INCLUSIVE */
323 1, /* OMP_CLAUSE_EXCLUSIVE */
324 2, /* OMP_CLAUSE_FROM */
325 2, /* OMP_CLAUSE_TO */
326 2, /* OMP_CLAUSE_MAP */
327 1, /* OMP_CLAUSE_HAS_DEVICE_ADDR */
328 1, /* OMP_CLAUSE_DOACROSS */
329 2, /* OMP_CLAUSE__CACHE_ */
330 1, /* OMP_CLAUSE_DESTROY */
331 2, /* OMP_CLAUSE_INIT */
332 1, /* OMP_CLAUSE_USE */
333 1, /* OMP_CLAUSE_INTEROP */
334 2, /* OMP_CLAUSE_GANG */
335 1, /* OMP_CLAUSE_ASYNC */
336 1, /* OMP_CLAUSE_WAIT */
337 0, /* OMP_CLAUSE_AUTO */
338 0, /* OMP_CLAUSE_SEQ */
339 1, /* OMP_CLAUSE__LOOPTEMP_ */
340 1, /* OMP_CLAUSE__REDUCTEMP_ */
341 1, /* OMP_CLAUSE__CONDTEMP_ */
342 1, /* OMP_CLAUSE__SCANTEMP_ */
343 1, /* OMP_CLAUSE_IF */
344 1, /* OMP_CLAUSE_SELF */
345 1, /* OMP_CLAUSE_NUM_THREADS */
346 1, /* OMP_CLAUSE_SCHEDULE */
347 0, /* OMP_CLAUSE_NOWAIT */
348 1, /* OMP_CLAUSE_ORDERED */
349 0, /* OMP_CLAUSE_DEFAULT */
350 3, /* OMP_CLAUSE_COLLAPSE */
351 0, /* OMP_CLAUSE_UNTIED */
352 1, /* OMP_CLAUSE_FINAL */
353 0, /* OMP_CLAUSE_MERGEABLE */
354 1, /* OMP_CLAUSE_DEVICE */
355 1, /* OMP_CLAUSE_DIST_SCHEDULE */
356 0, /* OMP_CLAUSE_INBRANCH */
357 0, /* OMP_CLAUSE_NOTINBRANCH */
358 2, /* OMP_CLAUSE_NUM_TEAMS */
359 1, /* OMP_CLAUSE_THREAD_LIMIT */
360 0, /* OMP_CLAUSE_PROC_BIND */
361 1, /* OMP_CLAUSE_SAFELEN */
362 1, /* OMP_CLAUSE_SIMDLEN */
363 0, /* OMP_CLAUSE_DEVICE_TYPE */
364 0, /* OMP_CLAUSE_FOR */
365 0, /* OMP_CLAUSE_PARALLEL */
366 0, /* OMP_CLAUSE_SECTIONS */
367 0, /* OMP_CLAUSE_TASKGROUP */
368 1, /* OMP_CLAUSE_PRIORITY */
369 1, /* OMP_CLAUSE_GRAINSIZE */
370 1, /* OMP_CLAUSE_NUM_TASKS */
371 0, /* OMP_CLAUSE_NOGROUP */
372 0, /* OMP_CLAUSE_THREADS */
373 0, /* OMP_CLAUSE_SIMD */
374 1, /* OMP_CLAUSE_HINT */
375 0, /* OMP_CLAUSE_DEFAULTMAP */
376 0, /* OMP_CLAUSE_ORDER */
377 0, /* OMP_CLAUSE_BIND */
378 1, /* OMP_CLAUSE_FILTER */
379 1, /* OMP_CLAUSE_INDIRECT */
380 1, /* OMP_CLAUSE_PARTIAL */
381 0, /* OMP_CLAUSE_FULL */
382 1, /* OMP_CLAUSE_SIZES */
383 1, /* OMP_CLAUSE__SIMDUID_ */
384 0, /* OMP_CLAUSE__SIMT_ */
385 0, /* OMP_CLAUSE_INDEPENDENT */
386 1, /* OMP_CLAUSE_WORKER */
387 1, /* OMP_CLAUSE_VECTOR */
388 1, /* OMP_CLAUSE_NUM_GANGS */
389 1, /* OMP_CLAUSE_NUM_WORKERS */
390 1, /* OMP_CLAUSE_VECTOR_LENGTH */
391 3, /* OMP_CLAUSE_TILE */
392 0, /* OMP_CLAUSE_IF_PRESENT */
393 0, /* OMP_CLAUSE_FINALIZE */
394 0, /* OMP_CLAUSE_NOHOST */
395 1, /* OMP_CLAUSE_NOVARIANTS */
396 1, /* OMP_CLAUSE_NOCONTEXT */
397 };
398
399 const char * const omp_clause_code_name[] =
400 {
401 "error_clause",
402 "private",
403 "shared",
404 "firstprivate",
405 "lastprivate",
406 "reduction",
407 "task_reduction",
408 "in_reduction",
409 "copyin",
410 "copyprivate",
411 "linear",
412 "affinity",
413 "aligned",
414 "allocate",
415 "depend",
416 "nontemporal",
417 "uniform",
418 "enter",
419 "link",
420 "detach",
421 "use_device_ptr",
422 "use_device_addr",
423 "is_device_ptr",
424 "inclusive",
425 "exclusive",
426 "from",
427 "to",
428 "map",
429 "has_device_addr",
430 "doacross",
431 "_cache_",
432 "destroy",
433 "init",
434 "use",
435 "interop",
436 "gang",
437 "async",
438 "wait",
439 "auto",
440 "seq",
441 "_looptemp_",
442 "_reductemp_",
443 "_condtemp_",
444 "_scantemp_",
445 "if",
446 "self",
447 "num_threads",
448 "schedule",
449 "nowait",
450 "ordered",
451 "default",
452 "collapse",
453 "untied",
454 "final",
455 "mergeable",
456 "device",
457 "dist_schedule",
458 "inbranch",
459 "notinbranch",
460 "num_teams",
461 "thread_limit",
462 "proc_bind",
463 "safelen",
464 "simdlen",
465 "device_type",
466 "for",
467 "parallel",
468 "sections",
469 "taskgroup",
470 "priority",
471 "grainsize",
472 "num_tasks",
473 "nogroup",
474 "threads",
475 "simd",
476 "hint",
477 "defaultmap",
478 "order",
479 "bind",
480 "filter",
481 "indirect",
482 "partial",
483 "full",
484 "sizes",
485 "_simduid_",
486 "_simt_",
487 "independent",
488 "worker",
489 "vector",
490 "num_gangs",
491 "num_workers",
492 "vector_length",
493 "tile",
494 "if_present",
495 "finalize",
496 "nohost",
497 "novariants",
498 "nocontext",
499 };
500
501 /* Unless specific to OpenACC, we tend to internally maintain OpenMP-centric
502 clause names, but for use in diagnostics etc. would like to use the "user"
503 clause names. */
504
505 const char *
506 user_omp_clause_code_name (tree clause, bool oacc)
507 {
508 /* For OpenACC, the 'OMP_CLAUSE_MAP_KIND' of an 'OMP_CLAUSE_MAP' is used to
509 distinguish clauses as seen by the user. See also where front ends do
510 'build_omp_clause' with 'OMP_CLAUSE_MAP'. */
511 if (oacc && OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP)
512 switch (OMP_CLAUSE_MAP_KIND (clause))
513 {
514 case GOMP_MAP_FORCE_ALLOC:
515 case GOMP_MAP_ALLOC: return "create";
516 case GOMP_MAP_FORCE_TO:
517 case GOMP_MAP_TO: return "copyin";
518 case GOMP_MAP_FORCE_FROM:
519 case GOMP_MAP_FROM: return "copyout";
520 case GOMP_MAP_FORCE_TOFROM:
521 case GOMP_MAP_TOFROM: return "copy";
522 case GOMP_MAP_RELEASE: return "delete";
523 case GOMP_MAP_FORCE_PRESENT: return "present";
524 case GOMP_MAP_ATTACH: return "attach";
525 case GOMP_MAP_FORCE_DETACH:
526 case GOMP_MAP_DETACH: return "detach";
527 case GOMP_MAP_DEVICE_RESIDENT: return "device_resident";
528 case GOMP_MAP_LINK: return "link";
529 case GOMP_MAP_FORCE_DEVICEPTR: return "deviceptr";
530 default: break;
531 }
532
533 return omp_clause_code_name[OMP_CLAUSE_CODE (clause)];
534 }
535
536
537 /* Return the tree node structure used by tree code CODE. */
538
539 static inline enum tree_node_structure_enum
540 tree_node_structure_for_code (enum tree_code code)
541 {
542 switch (TREE_CODE_CLASS (code))
543 {
544 case tcc_declaration:
545 switch (code)
546 {
547 case CONST_DECL: return TS_CONST_DECL;
548 case DEBUG_EXPR_DECL: return TS_DECL_WRTL;
549 case FIELD_DECL: return TS_FIELD_DECL;
550 case FUNCTION_DECL: return TS_FUNCTION_DECL;
551 case LABEL_DECL: return TS_LABEL_DECL;
552 case PARM_DECL: return TS_PARM_DECL;
553 case RESULT_DECL: return TS_RESULT_DECL;
554 case TRANSLATION_UNIT_DECL: return TS_TRANSLATION_UNIT_DECL;
555 case TYPE_DECL: return TS_TYPE_DECL;
556 case VAR_DECL: return TS_VAR_DECL;
557 default: return TS_DECL_NON_COMMON;
558 }
559
560 case tcc_type: return TS_TYPE_NON_COMMON;
561
562 case tcc_binary:
563 case tcc_comparison:
564 case tcc_expression:
565 case tcc_reference:
566 case tcc_statement:
567 case tcc_unary:
568 case tcc_vl_exp: return TS_EXP;
569
570 default: /* tcc_constant and tcc_exceptional */
571 break;
572 }
573
574 switch (code)
575 {
576 /* tcc_constant cases. */
577 case COMPLEX_CST: return TS_COMPLEX;
578 case FIXED_CST: return TS_FIXED_CST;
579 case INTEGER_CST: return TS_INT_CST;
580 case POLY_INT_CST: return TS_POLY_INT_CST;
581 case REAL_CST: return TS_REAL_CST;
582 case STRING_CST: return TS_STRING;
583 case VECTOR_CST: return TS_VECTOR;
584 case VOID_CST: return TS_TYPED;
585 case RAW_DATA_CST: return TS_RAW_DATA_CST;
586
587 /* tcc_exceptional cases. */
588 case BLOCK: return TS_BLOCK;
589 case CONSTRUCTOR: return TS_CONSTRUCTOR;
590 case ERROR_MARK: return TS_COMMON;
591 case IDENTIFIER_NODE: return TS_IDENTIFIER;
592 case OMP_CLAUSE: return TS_OMP_CLAUSE;
593 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
594 case PLACEHOLDER_EXPR: return TS_COMMON;
595 case SSA_NAME: return TS_SSA_NAME;
596 case STATEMENT_LIST: return TS_STATEMENT_LIST;
597 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
598 case TREE_BINFO: return TS_BINFO;
599 case TREE_LIST: return TS_LIST;
600 case TREE_VEC: return TS_VEC;
601
602 default:
603 gcc_unreachable ();
604 }
605 }
606
607
608 /* Initialize tree_contains_struct to describe the hierarchy of tree
609 nodes. */
610
611 static void
612 initialize_tree_contains_struct (void)
613 {
614 unsigned i;
615
616 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
617 {
618 enum tree_code code;
619 enum tree_node_structure_enum ts_code;
620
621 code = (enum tree_code) i;
622 ts_code = tree_node_structure_for_code (code);
623
624 /* Mark the TS structure itself. */
625 tree_contains_struct[code][ts_code] = 1;
626
627 /* Mark all the structures that TS is derived from. */
628 switch (ts_code)
629 {
630 case TS_TYPED:
631 case TS_BLOCK:
632 case TS_OPTIMIZATION:
633 case TS_TARGET_OPTION:
634 MARK_TS_BASE (code);
635 break;
636
637 case TS_COMMON:
638 case TS_INT_CST:
639 case TS_POLY_INT_CST:
640 case TS_REAL_CST:
641 case TS_FIXED_CST:
642 case TS_VECTOR:
643 case TS_STRING:
644 case TS_RAW_DATA_CST:
645 case TS_COMPLEX:
646 case TS_SSA_NAME:
647 case TS_CONSTRUCTOR:
648 case TS_EXP:
649 case TS_STATEMENT_LIST:
650 MARK_TS_TYPED (code);
651 break;
652
653 case TS_IDENTIFIER:
654 case TS_DECL_MINIMAL:
655 case TS_TYPE_COMMON:
656 case TS_LIST:
657 case TS_VEC:
658 case TS_BINFO:
659 case TS_OMP_CLAUSE:
660 MARK_TS_COMMON (code);
661 break;
662
663 case TS_TYPE_WITH_LANG_SPECIFIC:
664 MARK_TS_TYPE_COMMON (code);
665 break;
666
667 case TS_TYPE_NON_COMMON:
668 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
669 break;
670
671 case TS_DECL_COMMON:
672 MARK_TS_DECL_MINIMAL (code);
673 break;
674
675 case TS_DECL_WRTL:
676 case TS_CONST_DECL:
677 MARK_TS_DECL_COMMON (code);
678 break;
679
680 case TS_DECL_NON_COMMON:
681 MARK_TS_DECL_WITH_VIS (code);
682 break;
683
684 case TS_DECL_WITH_VIS:
685 case TS_PARM_DECL:
686 case TS_LABEL_DECL:
687 case TS_RESULT_DECL:
688 MARK_TS_DECL_WRTL (code);
689 break;
690
691 case TS_FIELD_DECL:
692 MARK_TS_DECL_COMMON (code);
693 break;
694
695 case TS_VAR_DECL:
696 MARK_TS_DECL_WITH_VIS (code);
697 break;
698
699 case TS_TYPE_DECL:
700 case TS_FUNCTION_DECL:
701 MARK_TS_DECL_NON_COMMON (code);
702 break;
703
704 case TS_TRANSLATION_UNIT_DECL:
705 MARK_TS_DECL_COMMON (code);
706 break;
707
708 default:
709 gcc_unreachable ();
710 }
711 }
712
713 /* Basic consistency checks for attributes used in fold. */
714 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
715 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
716 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
717 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
718 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
719 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
720 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
721 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
722 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
723 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
724 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
725 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
726 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
727 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
728 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
729 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
730 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
731 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
732 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
733 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
734 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
735 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
736 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
737 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
738 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
739 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
740 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
741 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
742 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
743 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
744 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
745 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
746 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
747 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
748 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
749 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
750 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
751 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
752 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_MINIMAL]);
753 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_COMMON]);
754 }
755
756
757 /* Init tree.cc. */
758
759 void
760 init_ttree (void)
761 {
762 /* Initialize the hash table of types. */
763 type_hash_table
764 = hash_table<type_cache_hasher>::create_ggc (TYPE_HASH_INITIAL_SIZE);
765
766 debug_expr_for_decl
767 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
768
769 value_expr_for_decl
770 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
771
772 int_cst_hash_table = hash_table<int_cst_hasher>::create_ggc (1024);
773
774 poly_int_cst_hash_table = hash_table<poly_int_cst_hasher>::create_ggc (64);
775
776 int_cst_node = make_int_cst (1, 1);
777
778 cl_option_hash_table = hash_table<cl_option_hasher>::create_ggc (64);
779
780 cl_optimization_node = make_node (OPTIMIZATION_NODE);
781 cl_target_option_node = make_node (TARGET_OPTION_NODE);
782
783 /* Initialize the tree_contains_struct array. */
784 initialize_tree_contains_struct ();
785 lang_hooks.init_ts ();
786 }
787
788
789 /* The name of the object as the assembler will see it (but before any
790 translations made by ASM_OUTPUT_LABELREF). Often this is the same
791 as DECL_NAME. It is an IDENTIFIER_NODE. */
792 tree
793 decl_assembler_name (tree decl)
794 {
795 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
796 lang_hooks.set_decl_assembler_name (decl);
797 return DECL_ASSEMBLER_NAME_RAW (decl);
798 }
799
800 /* The DECL_ASSEMBLER_NAME_RAW of DECL is being explicitly set to NAME
801 (either of which may be NULL). Inform the FE, if this changes the
802 name. */
803
804 void
805 overwrite_decl_assembler_name (tree decl, tree name)
806 {
807 if (DECL_ASSEMBLER_NAME_RAW (decl) != name)
808 lang_hooks.overwrite_decl_assembler_name (decl, name);
809 }
810
811 /* Return true if DECL may need an assembler name to be set. */
812
813 static inline bool
814 need_assembler_name_p (tree decl)
815 {
816 /* We use DECL_ASSEMBLER_NAME to hold mangled type names for One Definition
817 Rule merging. This makes type_odr_p to return true on those types during
818 LTO and by comparing the mangled name, we can say what types are intended
819 to be equivalent across compilation unit.
820
821 We do not store names of type_in_anonymous_namespace_p.
822
823 Record, union and enumeration type have linkage that allows use
824 to check type_in_anonymous_namespace_p. We do not mangle compound types
825 that always can be compared structurally.
826
827 Similarly for builtin types, we compare properties of their main variant.
828 A special case are integer types where mangling do make differences
829 between char/signed char/unsigned char etc. Storing name for these makes
830 e.g. -fno-signed-char/-fsigned-char mismatches to be handled well.
831 See cp/mangle.cc:write_builtin_type for details. */
832
833 if (TREE_CODE (decl) == TYPE_DECL)
834 {
835 if (DECL_NAME (decl)
836 && decl == TYPE_NAME (TREE_TYPE (decl))
837 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TREE_TYPE (decl)
838 && !TYPE_ARTIFICIAL (TREE_TYPE (decl))
839 && ((TREE_CODE (TREE_TYPE (decl)) != RECORD_TYPE
840 && TREE_CODE (TREE_TYPE (decl)) != UNION_TYPE)
841 || TYPE_CXX_ODR_P (TREE_TYPE (decl)))
842 && (type_with_linkage_p (TREE_TYPE (decl))
843 || TREE_CODE (TREE_TYPE (decl)) == INTEGER_TYPE)
844 && !variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
845 return !DECL_ASSEMBLER_NAME_SET_P (decl);
846 return false;
847 }
848 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
849 if (!VAR_OR_FUNCTION_DECL_P (decl))
850 return false;
851
852 /* If DECL already has its assembler name set, it does not need a
853 new one. */
854 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
855 || DECL_ASSEMBLER_NAME_SET_P (decl))
856 return false;
857
858 /* Abstract decls do not need an assembler name, except they
859 can be looked up by autofdo. */
860 if (DECL_ABSTRACT_P (decl) && !flag_auto_profile)
861 return false;
862
863 /* For VAR_DECLs, only static, public and external symbols need an
864 assembler name. */
865 if (VAR_P (decl)
866 && !TREE_STATIC (decl)
867 && !TREE_PUBLIC (decl)
868 && !DECL_EXTERNAL (decl))
869 return false;
870
871 if (TREE_CODE (decl) == FUNCTION_DECL)
872 {
873 /* Do not set assembler name on builtins. Allow RTL expansion to
874 decide whether to expand inline or via a regular call. */
875 if (fndecl_built_in_p (decl)
876 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
877 return false;
878
879 /* Functions represented in the callgraph need an assembler name. */
880 if (cgraph_node::get (decl) != NULL)
881 return true;
882
883 /* Unused and not public functions don't need an assembler name. */
884 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
885 return false;
886 }
887
888 return true;
889 }
890
891 /* If T needs an assembler name, have one created for it. */
892
893 void
894 assign_assembler_name_if_needed (tree t)
895 {
896 if (need_assembler_name_p (t))
897 {
898 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
899 diagnostics that use input_location to show locus
900 information. The problem here is that, at this point,
901 input_location is generally anchored to the end of the file
902 (since the parser is long gone), so we don't have a good
903 position to pin it to.
904
905 To alleviate this problem, this uses the location of T's
906 declaration. Examples of this are
907 testsuite/g++.dg/template/cond2.C and
908 testsuite/g++.dg/template/pr35240.C. */
909 location_t saved_location = input_location;
910 input_location = DECL_SOURCE_LOCATION (t);
911
912 decl_assembler_name (t);
913
914 input_location = saved_location;
915 }
916 }
917
918 /* When the target supports COMDAT groups, this indicates which group the
919 DECL is associated with. This can be either an IDENTIFIER_NODE or a
920 decl, in which case its DECL_ASSEMBLER_NAME identifies the group. */
921 tree
922 decl_comdat_group (const_tree node)
923 {
924 struct symtab_node *snode = symtab_node::get (node);
925 if (!snode)
926 return NULL;
927 return snode->get_comdat_group ();
928 }
929
930 /* Likewise, but make sure it's been reduced to an IDENTIFIER_NODE. */
931 tree
932 decl_comdat_group_id (const_tree node)
933 {
934 struct symtab_node *snode = symtab_node::get (node);
935 if (!snode)
936 return NULL;
937 return snode->get_comdat_group_id ();
938 }
939
940 /* When the target supports named section, return its name as IDENTIFIER_NODE
941 or NULL if it is in no section. */
942 const char *
943 decl_section_name (const_tree node)
944 {
945 struct symtab_node *snode = symtab_node::get (node);
946 if (!snode)
947 return NULL;
948 return snode->get_section ();
949 }
950
951 /* Set section name of NODE to VALUE (that is expected to be
952 identifier node) */
953 void
954 set_decl_section_name (tree node, const char *value)
955 {
956 struct symtab_node *snode;
957
958 if (value == NULL)
959 {
960 snode = symtab_node::get (node);
961 if (!snode)
962 return;
963 }
964 else if (VAR_P (node))
965 snode = varpool_node::get_create (node);
966 else
967 snode = cgraph_node::get_create (node);
968 snode->set_section (value);
969 }
970
971 /* Set section name of NODE to match the section name of OTHER.
972
973 set_decl_section_name (decl, other) is equivalent to
974 set_decl_section_name (decl, DECL_SECTION_NAME (other)), but possibly more
975 efficient. */
976 void
977 set_decl_section_name (tree decl, const_tree other)
978 {
979 struct symtab_node *other_node = symtab_node::get (other);
980 if (other_node)
981 {
982 struct symtab_node *decl_node;
983 if (VAR_P (decl))
984 decl_node = varpool_node::get_create (decl);
985 else
986 decl_node = cgraph_node::get_create (decl);
987 decl_node->set_section (*other_node);
988 }
989 else
990 {
991 struct symtab_node *decl_node = symtab_node::get (decl);
992 if (!decl_node)
993 return;
994 decl_node->set_section (NULL);
995 }
996 }
997
998 /* Return TLS model of a variable NODE. */
999 enum tls_model
1000 decl_tls_model (const_tree node)
1001 {
1002 struct varpool_node *snode = varpool_node::get (node);
1003 if (!snode)
1004 return TLS_MODEL_NONE;
1005 return snode->tls_model;
1006 }
1007
1008 /* Set TLS model of variable NODE to MODEL. */
1009 void
1010 set_decl_tls_model (tree node, enum tls_model model)
1011 {
1012 struct varpool_node *vnode;
1013
1014 if (model == TLS_MODEL_NONE)
1015 {
1016 vnode = varpool_node::get (node);
1017 if (!vnode)
1018 return;
1019 }
1020 else
1021 vnode = varpool_node::get_create (node);
1022 vnode->tls_model = model;
1023 }
1024
1025 /* Compute the number of bytes occupied by a tree with code CODE.
1026 This function cannot be used for nodes that have variable sizes,
1027 including TREE_VEC, INTEGER_CST, STRING_CST, and CALL_EXPR. */
1028 size_t
1029 tree_code_size (enum tree_code code)
1030 {
1031 switch (TREE_CODE_CLASS (code))
1032 {
1033 case tcc_declaration: /* A decl node */
1034 switch (code)
1035 {
1036 case FIELD_DECL: return sizeof (tree_field_decl);
1037 case PARM_DECL: return sizeof (tree_parm_decl);
1038 case VAR_DECL: return sizeof (tree_var_decl);
1039 case LABEL_DECL: return sizeof (tree_label_decl);
1040 case RESULT_DECL: return sizeof (tree_result_decl);
1041 case CONST_DECL: return sizeof (tree_const_decl);
1042 case TYPE_DECL: return sizeof (tree_type_decl);
1043 case FUNCTION_DECL: return sizeof (tree_function_decl);
1044 case DEBUG_EXPR_DECL: return sizeof (tree_decl_with_rtl);
1045 case TRANSLATION_UNIT_DECL: return sizeof (tree_translation_unit_decl);
1046 case NAMESPACE_DECL:
1047 case IMPORTED_DECL:
1048 case NAMELIST_DECL: return sizeof (tree_decl_non_common);
1049 default:
1050 gcc_checking_assert (code >= NUM_TREE_CODES);
1051 return lang_hooks.tree_size (code);
1052 }
1053
1054 case tcc_type: /* a type node */
1055 switch (code)
1056 {
1057 case OFFSET_TYPE:
1058 case ENUMERAL_TYPE:
1059 case BOOLEAN_TYPE:
1060 case INTEGER_TYPE:
1061 case REAL_TYPE:
1062 case OPAQUE_TYPE:
1063 case POINTER_TYPE:
1064 case REFERENCE_TYPE:
1065 case NULLPTR_TYPE:
1066 case FIXED_POINT_TYPE:
1067 case COMPLEX_TYPE:
1068 case VECTOR_TYPE:
1069 case ARRAY_TYPE:
1070 case RECORD_TYPE:
1071 case UNION_TYPE:
1072 case QUAL_UNION_TYPE:
1073 case VOID_TYPE:
1074 case FUNCTION_TYPE:
1075 case METHOD_TYPE:
1076 case BITINT_TYPE:
1077 case LANG_TYPE: return sizeof (tree_type_non_common);
1078 default:
1079 gcc_checking_assert (code >= NUM_TREE_CODES);
1080 return lang_hooks.tree_size (code);
1081 }
1082
1083 case tcc_reference: /* a reference */
1084 case tcc_expression: /* an expression */
1085 case tcc_statement: /* an expression with side effects */
1086 case tcc_comparison: /* a comparison expression */
1087 case tcc_unary: /* a unary arithmetic expression */
1088 case tcc_binary: /* a binary arithmetic expression */
1089 return (sizeof (struct tree_exp)
1090 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
1091
1092 case tcc_constant: /* a constant */
1093 switch (code)
1094 {
1095 case VOID_CST: return sizeof (tree_typed);
1096 case INTEGER_CST: gcc_unreachable ();
1097 case POLY_INT_CST: return sizeof (tree_poly_int_cst);
1098 case REAL_CST: return sizeof (tree_real_cst);
1099 case FIXED_CST: return sizeof (tree_fixed_cst);
1100 case COMPLEX_CST: return sizeof (tree_complex);
1101 case RAW_DATA_CST: return sizeof (tree_raw_data);
1102 case VECTOR_CST: gcc_unreachable ();
1103 case STRING_CST: gcc_unreachable ();
1104 default:
1105 gcc_checking_assert (code >= NUM_TREE_CODES);
1106 return lang_hooks.tree_size (code);
1107 }
1108
1109 case tcc_exceptional: /* something random, like an identifier. */
1110 switch (code)
1111 {
1112 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
1113 case TREE_LIST: return sizeof (tree_list);
1114
1115 case ERROR_MARK:
1116 case PLACEHOLDER_EXPR: return sizeof (tree_common);
1117
1118 case TREE_VEC: gcc_unreachable ();
1119 case OMP_CLAUSE: gcc_unreachable ();
1120
1121 case SSA_NAME: return sizeof (tree_ssa_name);
1122
1123 case STATEMENT_LIST: return sizeof (tree_statement_list);
1124 case BLOCK: return sizeof (struct tree_block);
1125 case CONSTRUCTOR: return sizeof (tree_constructor);
1126 case OPTIMIZATION_NODE: return sizeof (tree_optimization_option);
1127 case TARGET_OPTION_NODE: return sizeof (tree_target_option);
1128
1129 default:
1130 gcc_checking_assert (code >= NUM_TREE_CODES);
1131 return lang_hooks.tree_size (code);
1132 }
1133
1134 default:
1135 gcc_unreachable ();
1136 }
1137 }
1138
1139 /* Compute the number of bytes occupied by NODE. This routine only
1140 looks at TREE_CODE, except for those nodes that have variable sizes. */
1141 size_t
1142 tree_size (const_tree node)
1143 {
1144 const enum tree_code code = TREE_CODE (node);
1145 switch (code)
1146 {
1147 case INTEGER_CST:
1148 return (sizeof (struct tree_int_cst)
1149 + (TREE_INT_CST_EXT_NUNITS (node) - 1) * sizeof (HOST_WIDE_INT));
1150
1151 case TREE_BINFO:
1152 return (offsetof (struct tree_binfo, base_binfos)
1153 + vec<tree, va_gc>
1154 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
1155
1156 case TREE_VEC:
1157 return (sizeof (struct tree_vec)
1158 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
1159
1160 case VECTOR_CST:
1161 return (sizeof (struct tree_vector)
1162 + (vector_cst_encoded_nelts (node) - 1) * sizeof (tree));
1163
1164 case STRING_CST:
1165 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
1166
1167 case OMP_CLAUSE:
1168 return (sizeof (struct tree_omp_clause)
1169 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
1170 * sizeof (tree));
1171
1172 default:
1173 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
1174 return (sizeof (struct tree_exp)
1175 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
1176 else
1177 return tree_code_size (code);
1178 }
1179 }
1180
1181 /* Return tree node kind based on tree CODE. */
1182
1183 static tree_node_kind
1184 get_stats_node_kind (enum tree_code code)
1185 {
1186 enum tree_code_class type = TREE_CODE_CLASS (code);
1187
1188 switch (type)
1189 {
1190 case tcc_declaration: /* A decl node */
1191 return d_kind;
1192 case tcc_type: /* a type node */
1193 return t_kind;
1194 case tcc_statement: /* an expression with side effects */
1195 return s_kind;
1196 case tcc_reference: /* a reference */
1197 return r_kind;
1198 case tcc_expression: /* an expression */
1199 case tcc_comparison: /* a comparison expression */
1200 case tcc_unary: /* a unary arithmetic expression */
1201 case tcc_binary: /* a binary arithmetic expression */
1202 return e_kind;
1203 case tcc_constant: /* a constant */
1204 return c_kind;
1205 case tcc_exceptional: /* something random, like an identifier. */
1206 switch (code)
1207 {
1208 case IDENTIFIER_NODE:
1209 return id_kind;
1210 case TREE_VEC:
1211 return vec_kind;
1212 case TREE_BINFO:
1213 return binfo_kind;
1214 case SSA_NAME:
1215 return ssa_name_kind;
1216 case BLOCK:
1217 return b_kind;
1218 case CONSTRUCTOR:
1219 return constr_kind;
1220 case OMP_CLAUSE:
1221 return omp_clause_kind;
1222 default:
1223 return x_kind;
1224 }
1225 break;
1226 case tcc_vl_exp:
1227 return e_kind;
1228 default:
1229 gcc_unreachable ();
1230 }
1231 }
1232
1233 /* Record interesting allocation statistics for a tree node with CODE
1234 and LENGTH. */
1235
1236 static void
1237 record_node_allocation_statistics (enum tree_code code, size_t length)
1238 {
1239 if (!GATHER_STATISTICS)
1240 return;
1241
1242 tree_node_kind kind = get_stats_node_kind (code);
1243
1244 tree_code_counts[(int) code]++;
1245 tree_node_counts[(int) kind]++;
1246 tree_node_sizes[(int) kind] += length;
1247 }
1248
1249 /* Allocate and return a new UID from the DECL_UID namespace. */
1250
1251 int
1252 allocate_decl_uid (void)
1253 {
1254 return next_decl_uid++;
1255 }
1256
1257 /* Return a newly allocated node of code CODE. For decl and type
1258 nodes, some other fields are initialized. The rest of the node is
1259 initialized to zero. This function cannot be used for TREE_VEC,
1260 INTEGER_CST or OMP_CLAUSE nodes, which is enforced by asserts in
1261 tree_code_size.
1262
1263 Achoo! I got a code in the node. */
1264
1265 tree
1266 make_node (enum tree_code code MEM_STAT_DECL)
1267 {
1268 tree t;
1269 enum tree_code_class type = TREE_CODE_CLASS (code);
1270 size_t length = tree_code_size (code);
1271
1272 record_node_allocation_statistics (code, length);
1273
1274 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1275 TREE_SET_CODE (t, code);
1276
1277 switch (type)
1278 {
1279 case tcc_statement:
1280 if (code != DEBUG_BEGIN_STMT)
1281 TREE_SIDE_EFFECTS (t) = 1;
1282 break;
1283
1284 case tcc_declaration:
1285 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1286 {
1287 if (code == FUNCTION_DECL)
1288 {
1289 SET_DECL_ALIGN (t, FUNCTION_ALIGNMENT (FUNCTION_BOUNDARY));
1290 SET_DECL_MODE (t, FUNCTION_MODE);
1291 }
1292 else
1293 SET_DECL_ALIGN (t, 1);
1294 }
1295 DECL_SOURCE_LOCATION (t) = input_location;
1296 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
1297 DECL_UID (t) = --next_debug_decl_uid;
1298 else
1299 {
1300 DECL_UID (t) = allocate_decl_uid ();
1301 SET_DECL_PT_UID (t, -1);
1302 }
1303 if (TREE_CODE (t) == LABEL_DECL)
1304 LABEL_DECL_UID (t) = -1;
1305
1306 break;
1307
1308 case tcc_type:
1309 TYPE_UID (t) = next_type_uid++;
1310 SET_TYPE_ALIGN (t, BITS_PER_UNIT);
1311 TYPE_USER_ALIGN (t) = 0;
1312 TYPE_MAIN_VARIANT (t) = t;
1313 TYPE_CANONICAL (t) = t;
1314
1315 /* Default to no attributes for type, but let target change that. */
1316 TYPE_ATTRIBUTES (t) = NULL_TREE;
1317 targetm.set_default_type_attributes (t);
1318
1319 /* We have not yet computed the alias set for this type. */
1320 TYPE_ALIAS_SET (t) = -1;
1321 break;
1322
1323 case tcc_constant:
1324 TREE_CONSTANT (t) = 1;
1325 break;
1326
1327 case tcc_expression:
1328 switch (code)
1329 {
1330 case INIT_EXPR:
1331 case MODIFY_EXPR:
1332 case VA_ARG_EXPR:
1333 case PREDECREMENT_EXPR:
1334 case PREINCREMENT_EXPR:
1335 case POSTDECREMENT_EXPR:
1336 case POSTINCREMENT_EXPR:
1337 /* All of these have side-effects, no matter what their
1338 operands are. */
1339 TREE_SIDE_EFFECTS (t) = 1;
1340 break;
1341
1342 default:
1343 break;
1344 }
1345 break;
1346
1347 case tcc_exceptional:
1348 switch (code)
1349 {
1350 case TARGET_OPTION_NODE:
1351 TREE_TARGET_OPTION(t)
1352 = ggc_cleared_alloc<struct cl_target_option> ();
1353 break;
1354
1355 case OPTIMIZATION_NODE:
1356 TREE_OPTIMIZATION (t)
1357 = ggc_cleared_alloc<struct cl_optimization> ();
1358 break;
1359
1360 default:
1361 break;
1362 }
1363 break;
1364
1365 default:
1366 /* Other classes need no special treatment. */
1367 break;
1368 }
1369
1370 return t;
1371 }
1372
1373 /* Free tree node. */
1374
1375 void
1376 free_node (tree node)
1377 {
1378 enum tree_code code = TREE_CODE (node);
1379 if (GATHER_STATISTICS)
1380 {
1381 enum tree_node_kind kind = get_stats_node_kind (code);
1382
1383 gcc_checking_assert (tree_code_counts[(int) TREE_CODE (node)] != 0);
1384 gcc_checking_assert (tree_node_counts[(int) kind] != 0);
1385 gcc_checking_assert (tree_node_sizes[(int) kind] >= tree_size (node));
1386
1387 tree_code_counts[(int) TREE_CODE (node)]--;
1388 tree_node_counts[(int) kind]--;
1389 tree_node_sizes[(int) kind] -= tree_size (node);
1390 }
1391 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1392 vec_free (CONSTRUCTOR_ELTS (node));
1393 else if (code == BLOCK)
1394 vec_free (BLOCK_NONLOCALIZED_VARS (node));
1395 else if (code == TREE_BINFO)
1396 vec_free (BINFO_BASE_ACCESSES (node));
1397 else if (code == OPTIMIZATION_NODE)
1398 cl_optimization_option_free (TREE_OPTIMIZATION (node));
1399 else if (code == TARGET_OPTION_NODE)
1400 cl_target_option_free (TREE_TARGET_OPTION (node));
1401 ggc_free (node);
1402 }
1403
1404 /* Return a new node with the same contents as NODE except that its
1405 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
1406
1407 tree
1408 copy_node (tree node MEM_STAT_DECL)
1409 {
1410 tree t;
1411 enum tree_code code = TREE_CODE (node);
1412 size_t length;
1413
1414 gcc_assert (code != STATEMENT_LIST);
1415
1416 length = tree_size (node);
1417 record_node_allocation_statistics (code, length);
1418 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1419 memcpy (t, node, length);
1420
1421 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
1422 TREE_CHAIN (t) = 0;
1423 TREE_ASM_WRITTEN (t) = 0;
1424 TREE_VISITED (t) = 0;
1425
1426 if (TREE_CODE_CLASS (code) == tcc_declaration)
1427 {
1428 if (code == DEBUG_EXPR_DECL)
1429 DECL_UID (t) = --next_debug_decl_uid;
1430 else
1431 {
1432 DECL_UID (t) = allocate_decl_uid ();
1433 if (DECL_PT_UID_SET_P (node))
1434 SET_DECL_PT_UID (t, DECL_PT_UID (node));
1435 }
1436 if ((TREE_CODE (node) == PARM_DECL || VAR_P (node))
1437 && DECL_HAS_VALUE_EXPR_P (node))
1438 {
1439 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
1440 DECL_HAS_VALUE_EXPR_P (t) = 1;
1441 }
1442 /* DECL_DEBUG_EXPR is copied explicitly by callers. */
1443 if (VAR_P (node))
1444 {
1445 DECL_HAS_DEBUG_EXPR_P (t) = 0;
1446 t->decl_with_vis.symtab_node = NULL;
1447 }
1448 if (VAR_P (node) && DECL_HAS_INIT_PRIORITY_P (node))
1449 {
1450 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
1451 DECL_HAS_INIT_PRIORITY_P (t) = 1;
1452 }
1453 if (TREE_CODE (node) == FUNCTION_DECL)
1454 {
1455 DECL_STRUCT_FUNCTION (t) = NULL;
1456 t->decl_with_vis.symtab_node = NULL;
1457 }
1458 }
1459 else if (TREE_CODE_CLASS (code) == tcc_type)
1460 {
1461 TYPE_UID (t) = next_type_uid++;
1462 /* The following is so that the debug code for
1463 the copy is different from the original type.
1464 The two statements usually duplicate each other
1465 (because they clear fields of the same union),
1466 but the optimizer should catch that. */
1467 TYPE_SYMTAB_ADDRESS (t) = 0;
1468 TYPE_SYMTAB_DIE (t) = 0;
1469
1470 /* Do not copy the values cache. */
1471 if (TYPE_CACHED_VALUES_P (t))
1472 {
1473 TYPE_CACHED_VALUES_P (t) = 0;
1474 TYPE_CACHED_VALUES (t) = NULL_TREE;
1475 }
1476 }
1477 else if (code == TARGET_OPTION_NODE)
1478 {
1479 TREE_TARGET_OPTION (t) = ggc_alloc<struct cl_target_option>();
1480 memcpy (TREE_TARGET_OPTION (t), TREE_TARGET_OPTION (node),
1481 sizeof (struct cl_target_option));
1482 }
1483 else if (code == OPTIMIZATION_NODE)
1484 {
1485 TREE_OPTIMIZATION (t) = ggc_alloc<struct cl_optimization>();
1486 memcpy (TREE_OPTIMIZATION (t), TREE_OPTIMIZATION (node),
1487 sizeof (struct cl_optimization));
1488 }
1489
1490 return t;
1491 }
1492
1493 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1494 For example, this can copy a list made of TREE_LIST nodes. */
1495
1496 tree
1497 copy_list (tree list)
1498 {
1499 tree head;
1500 tree prev, next;
1501
1502 if (list == 0)
1503 return 0;
1504
1505 head = prev = copy_node (list);
1506 next = TREE_CHAIN (list);
1507 while (next)
1508 {
1509 TREE_CHAIN (prev) = copy_node (next);
1510 prev = TREE_CHAIN (prev);
1511 next = TREE_CHAIN (next);
1512 }
1513 return head;
1514 }
1515
1516
1517 /* Return the value that TREE_INT_CST_EXT_NUNITS should have for an
1518 INTEGER_CST with value CST and type TYPE. */
1519
1520 static unsigned int
1521 get_int_cst_ext_nunits (tree type, const wide_int &cst)
1522 {
1523 gcc_checking_assert (cst.get_precision () == TYPE_PRECISION (type));
1524 /* We need extra HWIs if CST is an unsigned integer with its
1525 upper bit set. */
1526 if (TYPE_UNSIGNED (type) && wi::neg_p (cst))
1527 return cst.get_precision () / HOST_BITS_PER_WIDE_INT + 1;
1528 return cst.get_len ();
1529 }
1530
1531 /* Return a new INTEGER_CST with value CST and type TYPE. */
1532
1533 static tree
1534 build_new_int_cst (tree type, const wide_int &cst)
1535 {
1536 unsigned int len = cst.get_len ();
1537 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1538 tree nt = make_int_cst (len, ext_len);
1539
1540 if (len < ext_len)
1541 {
1542 --ext_len;
1543 TREE_INT_CST_ELT (nt, ext_len)
1544 = zext_hwi (-1, cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1545 for (unsigned int i = len; i < ext_len; ++i)
1546 TREE_INT_CST_ELT (nt, i) = -1;
1547 }
1548 else if (TYPE_UNSIGNED (type)
1549 && cst.get_precision () < len * HOST_BITS_PER_WIDE_INT)
1550 {
1551 len--;
1552 TREE_INT_CST_ELT (nt, len)
1553 = zext_hwi (cst.elt (len),
1554 cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1555 }
1556
1557 for (unsigned int i = 0; i < len; i++)
1558 TREE_INT_CST_ELT (nt, i) = cst.elt (i);
1559 TREE_TYPE (nt) = type;
1560 return nt;
1561 }
1562
1563 /* Return a new POLY_INT_CST with coefficients COEFFS and type TYPE. */
1564
1565 static tree
1566 build_new_poly_int_cst (tree type, tree (&coeffs)[NUM_POLY_INT_COEFFS]
1567 CXX_MEM_STAT_INFO)
1568 {
1569 size_t length = sizeof (struct tree_poly_int_cst);
1570 record_node_allocation_statistics (POLY_INT_CST, length);
1571
1572 tree t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1573
1574 TREE_SET_CODE (t, POLY_INT_CST);
1575 TREE_CONSTANT (t) = 1;
1576 TREE_TYPE (t) = type;
1577 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1578 POLY_INT_CST_COEFF (t, i) = coeffs[i];
1579 return t;
1580 }
1581
1582 /* Create a constant tree that contains CST sign-extended to TYPE. */
1583
1584 tree
1585 build_int_cst (tree type, poly_int64 cst)
1586 {
1587 /* Support legacy code. */
1588 if (!type)
1589 type = integer_type_node;
1590
1591 return wide_int_to_tree (type, wi::shwi (cst, TYPE_PRECISION (type)));
1592 }
1593
1594 /* Create a constant tree that contains CST zero-extended to TYPE. */
1595
1596 tree
1597 build_int_cstu (tree type, poly_uint64 cst)
1598 {
1599 return wide_int_to_tree (type, wi::uhwi (cst, TYPE_PRECISION (type)));
1600 }
1601
1602 /* Create a constant tree that contains CST sign-extended to TYPE. */
1603
1604 tree
1605 build_int_cst_type (tree type, poly_int64 cst)
1606 {
1607 gcc_assert (type);
1608 return wide_int_to_tree (type, wi::shwi (cst, TYPE_PRECISION (type)));
1609 }
1610
1611 /* Constructs tree in type TYPE from with value given by CST. Signedness
1612 of CST is assumed to be the same as the signedness of TYPE. */
1613
1614 tree
1615 double_int_to_tree (tree type, double_int cst)
1616 {
1617 return wide_int_to_tree (type, widest_int::from (cst, TYPE_SIGN (type)));
1618 }
1619
1620 /* We force the wide_int CST to the range of the type TYPE by sign or
1621 zero extending it. OVERFLOWABLE indicates if we are interested in
1622 overflow of the value, when >0 we are only interested in signed
1623 overflow, for <0 we are interested in any overflow. OVERFLOWED
1624 indicates whether overflow has already occurred. CONST_OVERFLOWED
1625 indicates whether constant overflow has already occurred. We force
1626 T's value to be within range of T's type (by setting to 0 or 1 all
1627 the bits outside the type's range). We set TREE_OVERFLOWED if,
1628 OVERFLOWED is nonzero,
1629 or OVERFLOWABLE is >0 and signed overflow occurs
1630 or OVERFLOWABLE is <0 and any overflow occurs
1631 We return a new tree node for the extended wide_int. The node
1632 is shared if no overflow flags are set. */
1633
1634
1635 tree
1636 force_fit_type (tree type, const poly_wide_int_ref &cst,
1637 int overflowable, bool overflowed)
1638 {
1639 signop sign = TYPE_SIGN (type);
1640
1641 /* If we need to set overflow flags, return a new unshared node. */
1642 if (overflowed || !wi::fits_to_tree_p (cst, type))
1643 {
1644 if (overflowed
1645 || overflowable < 0
1646 || (overflowable > 0 && sign == SIGNED))
1647 {
1648 poly_wide_int tmp = poly_wide_int::from (cst, TYPE_PRECISION (type),
1649 sign);
1650 tree t;
1651 if (tmp.is_constant ())
1652 t = build_new_int_cst (type, tmp.coeffs[0]);
1653 else
1654 {
1655 tree coeffs[NUM_POLY_INT_COEFFS];
1656 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1657 {
1658 coeffs[i] = build_new_int_cst (type, tmp.coeffs[i]);
1659 TREE_OVERFLOW (coeffs[i]) = 1;
1660 }
1661 t = build_new_poly_int_cst (type, coeffs);
1662 }
1663 TREE_OVERFLOW (t) = 1;
1664 return t;
1665 }
1666 }
1667
1668 /* Else build a shared node. */
1669 return wide_int_to_tree (type, cst);
1670 }
1671
1672 /* These are the hash table functions for the hash table of INTEGER_CST
1673 nodes of a sizetype. */
1674
1675 /* Return the hash code X, an INTEGER_CST. */
1676
1677 hashval_t
1678 int_cst_hasher::hash (tree x)
1679 {
1680 const_tree const t = x;
1681 hashval_t code = TYPE_UID (TREE_TYPE (t));
1682 int i;
1683
1684 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
1685 code = iterative_hash_host_wide_int (TREE_INT_CST_ELT(t, i), code);
1686
1687 return code;
1688 }
1689
1690 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1691 is the same as that given by *Y, which is the same. */
1692
1693 bool
1694 int_cst_hasher::equal (tree x, tree y)
1695 {
1696 const_tree const xt = x;
1697 const_tree const yt = y;
1698
1699 if (TREE_TYPE (xt) != TREE_TYPE (yt)
1700 || TREE_INT_CST_NUNITS (xt) != TREE_INT_CST_NUNITS (yt)
1701 || TREE_INT_CST_EXT_NUNITS (xt) != TREE_INT_CST_EXT_NUNITS (yt))
1702 return false;
1703
1704 for (int i = 0; i < TREE_INT_CST_NUNITS (xt); i++)
1705 if (TREE_INT_CST_ELT (xt, i) != TREE_INT_CST_ELT (yt, i))
1706 return false;
1707
1708 return true;
1709 }
1710
1711 /* Cache wide_int CST into the TYPE_CACHED_VALUES cache for TYPE.
1712 SLOT is the slot entry to store it in, and MAX_SLOTS is the maximum
1713 number of slots that can be cached for the type. */
1714
1715 static inline tree
1716 cache_wide_int_in_type_cache (tree type, const wide_int &cst,
1717 int slot, int max_slots)
1718 {
1719 gcc_checking_assert (slot >= 0);
1720 /* Initialize cache. */
1721 if (!TYPE_CACHED_VALUES_P (type))
1722 {
1723 TYPE_CACHED_VALUES_P (type) = 1;
1724 TYPE_CACHED_VALUES (type) = make_tree_vec (max_slots);
1725 }
1726 tree t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), slot);
1727 if (!t)
1728 {
1729 /* Create a new shared int. */
1730 t = build_new_int_cst (type, cst);
1731 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), slot) = t;
1732 }
1733 return t;
1734 }
1735
1736 /* Create an INT_CST node of TYPE and value CST.
1737 The returned node is always shared. For small integers we use a
1738 per-type vector cache, for larger ones we use a single hash table.
1739 The value is extended from its precision according to the sign of
1740 the type to be a multiple of HOST_BITS_PER_WIDE_INT. This defines
1741 the upper bits and ensures that hashing and value equality based
1742 upon the underlying HOST_WIDE_INTs works without masking. */
1743
1744 static tree
1745 wide_int_to_tree_1 (tree type, const wide_int_ref &pcst)
1746 {
1747 tree t;
1748 int ix = -1;
1749 int limit = 0;
1750
1751 gcc_assert (type);
1752 unsigned int prec = TYPE_PRECISION (type);
1753 signop sgn = TYPE_SIGN (type);
1754
1755 /* Verify that everything is canonical. */
1756 int l = pcst.get_len ();
1757 if (l > 1)
1758 {
1759 if (pcst.elt (l - 1) == 0)
1760 gcc_checking_assert (pcst.elt (l - 2) < 0);
1761 if (pcst.elt (l - 1) == HOST_WIDE_INT_M1)
1762 gcc_checking_assert (pcst.elt (l - 2) >= 0);
1763 }
1764
1765 wide_int cst = wide_int::from (pcst, prec, sgn);
1766 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1767
1768 enum tree_code code = TREE_CODE (type);
1769 if (code == POINTER_TYPE || code == REFERENCE_TYPE)
1770 {
1771 /* Cache NULL pointer and zero bounds. */
1772 if (cst == 0)
1773 ix = 0;
1774 /* Cache upper bounds of pointers. */
1775 else if (cst == wi::max_value (prec, sgn))
1776 ix = 1;
1777 /* Cache 1 which is used for a non-zero range. */
1778 else if (cst == 1)
1779 ix = 2;
1780
1781 if (ix >= 0)
1782 {
1783 t = cache_wide_int_in_type_cache (type, cst, ix, 3);
1784 /* Make sure no one is clobbering the shared constant. */
1785 gcc_checking_assert (TREE_TYPE (t) == type
1786 && cst == wi::to_wide (t));
1787 return t;
1788 }
1789 }
1790 if (ext_len == 1)
1791 {
1792 /* We just need to store a single HOST_WIDE_INT. */
1793 HOST_WIDE_INT hwi;
1794 if (TYPE_UNSIGNED (type))
1795 hwi = cst.to_uhwi ();
1796 else
1797 hwi = cst.to_shwi ();
1798
1799 switch (code)
1800 {
1801 case NULLPTR_TYPE:
1802 gcc_assert (hwi == 0);
1803 /* Fallthru. */
1804
1805 case POINTER_TYPE:
1806 case REFERENCE_TYPE:
1807 /* Ignore pointers, as they were already handled above. */
1808 break;
1809
1810 case BOOLEAN_TYPE:
1811 /* Cache false or true. */
1812 limit = 2;
1813 if (IN_RANGE (hwi, 0, 1))
1814 ix = hwi;
1815 break;
1816
1817 case INTEGER_TYPE:
1818 case OFFSET_TYPE:
1819 case BITINT_TYPE:
1820 if (TYPE_SIGN (type) == UNSIGNED)
1821 {
1822 /* Cache [0, N). */
1823 limit = param_integer_share_limit;
1824 if (IN_RANGE (hwi, 0, param_integer_share_limit - 1))
1825 ix = hwi;
1826 }
1827 else
1828 {
1829 /* Cache [-1, N). */
1830 limit = param_integer_share_limit + 1;
1831 if (IN_RANGE (hwi, -1, param_integer_share_limit - 1))
1832 ix = hwi + 1;
1833 }
1834 break;
1835
1836 case ENUMERAL_TYPE:
1837 break;
1838
1839 default:
1840 gcc_unreachable ();
1841 }
1842
1843 if (ix >= 0)
1844 {
1845 t = cache_wide_int_in_type_cache (type, cst, ix, limit);
1846 /* Make sure no one is clobbering the shared constant. */
1847 gcc_checking_assert (TREE_TYPE (t) == type
1848 && TREE_INT_CST_NUNITS (t) == 1
1849 && TREE_INT_CST_EXT_NUNITS (t) == 1
1850 && TREE_INT_CST_ELT (t, 0) == hwi);
1851 return t;
1852 }
1853 else
1854 {
1855 /* Use the cache of larger shared ints, using int_cst_node as
1856 a temporary. */
1857
1858 TREE_INT_CST_ELT (int_cst_node, 0) = hwi;
1859 TREE_TYPE (int_cst_node) = type;
1860
1861 tree *slot = int_cst_hash_table->find_slot (int_cst_node, INSERT);
1862 t = *slot;
1863 if (!t)
1864 {
1865 /* Insert this one into the hash table. */
1866 t = int_cst_node;
1867 *slot = t;
1868 /* Make a new node for next time round. */
1869 int_cst_node = make_int_cst (1, 1);
1870 }
1871 }
1872 }
1873 else
1874 {
1875 /* The value either hashes properly or we drop it on the floor
1876 for the gc to take care of. There will not be enough of them
1877 to worry about. */
1878
1879 tree nt = build_new_int_cst (type, cst);
1880 tree *slot = int_cst_hash_table->find_slot (nt, INSERT);
1881 t = *slot;
1882 if (!t)
1883 {
1884 /* Insert this one into the hash table. */
1885 t = nt;
1886 *slot = t;
1887 }
1888 else
1889 ggc_free (nt);
1890 }
1891
1892 return t;
1893 }
1894
1895 hashval_t
1896 poly_int_cst_hasher::hash (tree t)
1897 {
1898 inchash::hash hstate;
1899
1900 hstate.add_int (TYPE_UID (TREE_TYPE (t)));
1901 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1902 hstate.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t, i)));
1903
1904 return hstate.end ();
1905 }
1906
1907 bool
1908 poly_int_cst_hasher::equal (tree x, const compare_type &y)
1909 {
1910 if (TREE_TYPE (x) != y.first)
1911 return false;
1912 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1913 if (wi::to_wide (POLY_INT_CST_COEFF (x, i)) != y.second->coeffs[i])
1914 return false;
1915 return true;
1916 }
1917
1918 /* Build a POLY_INT_CST node with type TYPE and with the elements in VALUES.
1919 The elements must also have type TYPE. */
1920
1921 tree
1922 build_poly_int_cst (tree type, const poly_wide_int_ref &values)
1923 {
1924 unsigned int prec = TYPE_PRECISION (type);
1925 gcc_assert (prec <= values.coeffs[0].get_precision ());
1926 poly_wide_int c = poly_wide_int::from (values, prec, SIGNED);
1927
1928 inchash::hash h;
1929 h.add_int (TYPE_UID (type));
1930 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1931 h.add_wide_int (c.coeffs[i]);
1932 poly_int_cst_hasher::compare_type comp (type, &c);
1933 tree *slot = poly_int_cst_hash_table->find_slot_with_hash (comp, h.end (),
1934 INSERT);
1935 if (*slot == NULL_TREE)
1936 {
1937 tree coeffs[NUM_POLY_INT_COEFFS];
1938 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1939 coeffs[i] = wide_int_to_tree_1 (type, c.coeffs[i]);
1940 *slot = build_new_poly_int_cst (type, coeffs);
1941 }
1942 return *slot;
1943 }
1944
1945 /* Create a constant tree with value VALUE in type TYPE. */
1946
1947 tree
1948 wide_int_to_tree (tree type, const poly_wide_int_ref &value)
1949 {
1950 if (value.is_constant ())
1951 return wide_int_to_tree_1 (type, value.coeffs[0]);
1952 return build_poly_int_cst (type, value);
1953 }
1954
1955 /* Insert INTEGER_CST T into a cache of integer constants. And return
1956 the cached constant (which may or may not be T). If MIGHT_DUPLICATE
1957 is false, and T falls into the type's 'smaller values' range, there
1958 cannot be an existing entry. Otherwise, if MIGHT_DUPLICATE is true,
1959 or the value is large, should an existing entry exist, it is
1960 returned (rather than inserting T). */
1961
1962 tree
1963 cache_integer_cst (tree t, bool might_duplicate ATTRIBUTE_UNUSED)
1964 {
1965 tree type = TREE_TYPE (t);
1966 int ix = -1;
1967 int limit = 0;
1968 int prec = TYPE_PRECISION (type);
1969
1970 gcc_assert (!TREE_OVERFLOW (t));
1971
1972 /* The caching indices here must match those in
1973 wide_int_to_type_1. */
1974 switch (TREE_CODE (type))
1975 {
1976 case NULLPTR_TYPE:
1977 gcc_checking_assert (integer_zerop (t));
1978 /* Fallthru. */
1979
1980 case POINTER_TYPE:
1981 case REFERENCE_TYPE:
1982 {
1983 if (integer_zerop (t))
1984 ix = 0;
1985 else if (integer_onep (t))
1986 ix = 2;
1987
1988 if (ix >= 0)
1989 limit = 3;
1990 }
1991 break;
1992
1993 case BOOLEAN_TYPE:
1994 /* Cache false or true. */
1995 limit = 2;
1996 if (wi::ltu_p (wi::to_wide (t), 2))
1997 ix = TREE_INT_CST_ELT (t, 0);
1998 break;
1999
2000 case INTEGER_TYPE:
2001 case OFFSET_TYPE:
2002 case BITINT_TYPE:
2003 if (TYPE_UNSIGNED (type))
2004 {
2005 /* Cache 0..N */
2006 limit = param_integer_share_limit;
2007
2008 /* This is a little hokie, but if the prec is smaller than
2009 what is necessary to hold param_integer_share_limit, then the
2010 obvious test will not get the correct answer. */
2011 if (prec < HOST_BITS_PER_WIDE_INT)
2012 {
2013 if (tree_to_uhwi (t)
2014 < (unsigned HOST_WIDE_INT) param_integer_share_limit)
2015 ix = tree_to_uhwi (t);
2016 }
2017 else if (wi::ltu_p (wi::to_wide (t), param_integer_share_limit))
2018 ix = tree_to_uhwi (t);
2019 }
2020 else
2021 {
2022 /* Cache -1..N */
2023 limit = param_integer_share_limit + 1;
2024
2025 if (integer_minus_onep (t))
2026 ix = 0;
2027 else if (!wi::neg_p (wi::to_wide (t)))
2028 {
2029 if (prec < HOST_BITS_PER_WIDE_INT)
2030 {
2031 if (tree_to_shwi (t) < param_integer_share_limit)
2032 ix = tree_to_shwi (t) + 1;
2033 }
2034 else if (wi::ltu_p (wi::to_wide (t), param_integer_share_limit))
2035 ix = tree_to_shwi (t) + 1;
2036 }
2037 }
2038 break;
2039
2040 case ENUMERAL_TYPE:
2041 /* The slot used by TYPE_CACHED_VALUES is used for the enum
2042 members. */
2043 break;
2044
2045 default:
2046 gcc_unreachable ();
2047 }
2048
2049 if (ix >= 0)
2050 {
2051 /* Look for it in the type's vector of small shared ints. */
2052 if (!TYPE_CACHED_VALUES_P (type))
2053 {
2054 TYPE_CACHED_VALUES_P (type) = 1;
2055 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
2056 }
2057
2058 if (tree r = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix))
2059 {
2060 gcc_checking_assert (might_duplicate);
2061 t = r;
2062 }
2063 else
2064 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
2065 }
2066 else
2067 {
2068 /* Use the cache of larger shared ints. */
2069 tree *slot = int_cst_hash_table->find_slot (t, INSERT);
2070 if (tree r = *slot)
2071 {
2072 /* If there is already an entry for the number verify it's the
2073 same value. */
2074 gcc_checking_assert (wi::to_wide (tree (r)) == wi::to_wide (t));
2075 /* And return the cached value. */
2076 t = r;
2077 }
2078 else
2079 /* Otherwise insert this one into the hash table. */
2080 *slot = t;
2081 }
2082
2083 return t;
2084 }
2085
2086
2087 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
2088 and the rest are zeros. */
2089
2090 tree
2091 build_low_bits_mask (tree type, unsigned bits)
2092 {
2093 gcc_assert (bits <= TYPE_PRECISION (type));
2094
2095 return wide_int_to_tree (type, wi::mask (bits, false,
2096 TYPE_PRECISION (type)));
2097 }
2098
2099 /* Checks that X is integer constant that can be expressed in (unsigned)
2100 HOST_WIDE_INT without loss of precision. */
2101
2102 bool
2103 cst_and_fits_in_hwi (const_tree x)
2104 {
2105 return (TREE_CODE (x) == INTEGER_CST
2106 && (tree_fits_shwi_p (x) || tree_fits_uhwi_p (x)));
2107 }
2108
2109 /* Build a newly constructed VECTOR_CST with the given values of
2110 (VECTOR_CST_)LOG2_NPATTERNS and (VECTOR_CST_)NELTS_PER_PATTERN. */
2111
2112 tree
2113 make_vector (unsigned log2_npatterns,
2114 unsigned int nelts_per_pattern MEM_STAT_DECL)
2115 {
2116 gcc_assert (IN_RANGE (nelts_per_pattern, 1, 3));
2117 tree t;
2118 unsigned npatterns = 1 << log2_npatterns;
2119 unsigned encoded_nelts = npatterns * nelts_per_pattern;
2120 unsigned length = (sizeof (struct tree_vector)
2121 + (encoded_nelts - 1) * sizeof (tree));
2122
2123 record_node_allocation_statistics (VECTOR_CST, length);
2124
2125 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2126
2127 TREE_SET_CODE (t, VECTOR_CST);
2128 TREE_CONSTANT (t) = 1;
2129 VECTOR_CST_LOG2_NPATTERNS (t) = log2_npatterns;
2130 VECTOR_CST_NELTS_PER_PATTERN (t) = nelts_per_pattern;
2131
2132 return t;
2133 }
2134
2135 /* Return a new VECTOR_CST node whose type is TYPE and whose values
2136 are extracted from V, a vector of CONSTRUCTOR_ELT. */
2137
2138 tree
2139 build_vector_from_ctor (tree type, const vec<constructor_elt, va_gc> *v)
2140 {
2141 if (vec_safe_length (v) == 0)
2142 return build_zero_cst (type);
2143
2144 unsigned HOST_WIDE_INT idx, nelts, step = 1;
2145 tree value;
2146
2147 /* If the vector is a VLA, build a VLA constant vector. */
2148 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
2149 {
2150 nelts = constant_lower_bound (TYPE_VECTOR_SUBPARTS (type));
2151 gcc_assert (vec_safe_length (v) <= nelts);
2152 step = 2;
2153 }
2154
2155 tree_vector_builder vec (type, nelts, step);
2156 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
2157 {
2158 if (TREE_CODE (value) == VECTOR_CST)
2159 {
2160 /* If NELTS is constant then this must be too. */
2161 unsigned int sub_nelts = VECTOR_CST_NELTS (value).to_constant ();
2162 for (unsigned i = 0; i < sub_nelts; ++i)
2163 vec.quick_push (VECTOR_CST_ELT (value, i));
2164 }
2165 else
2166 vec.quick_push (value);
2167 }
2168 while (vec.length () < nelts * step)
2169 vec.quick_push (build_zero_cst (TREE_TYPE (type)));
2170
2171 return vec.build ();
2172 }
2173
2174 /* Build a vector of type VECTYPE where all the elements are SCs. */
2175 tree
2176 build_vector_from_val (tree vectype, tree sc)
2177 {
2178 unsigned HOST_WIDE_INT i, nunits;
2179
2180 if (sc == error_mark_node)
2181 return sc;
2182
2183 /* Verify that the vector type is suitable for SC. Note that there
2184 is some inconsistency in the type-system with respect to restrict
2185 qualifications of pointers. Vector types always have a main-variant
2186 element type and the qualification is applied to the vector-type.
2187 So TREE_TYPE (vector-type) does not return a properly qualified
2188 vector element-type. */
2189 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
2190 TREE_TYPE (vectype)));
2191
2192 if (CONSTANT_CLASS_P (sc))
2193 {
2194 tree_vector_builder v (vectype, 1, 1);
2195 v.quick_push (sc);
2196 return v.build ();
2197 }
2198 else if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits))
2199 return fold_build1 (VEC_DUPLICATE_EXPR, vectype, sc);
2200 else
2201 {
2202 vec<constructor_elt, va_gc> *v;
2203 vec_alloc (v, nunits);
2204 for (i = 0; i < nunits; ++i)
2205 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
2206 return build_constructor (vectype, v);
2207 }
2208 }
2209
2210 /* If TYPE is not a vector type, just return SC, otherwise return
2211 build_vector_from_val (TYPE, SC). */
2212
2213 tree
2214 build_uniform_cst (tree type, tree sc)
2215 {
2216 if (!VECTOR_TYPE_P (type))
2217 return sc;
2218
2219 return build_vector_from_val (type, sc);
2220 }
2221
2222 /* Build a vector series of type TYPE in which element I has the value
2223 BASE + I * STEP. The result is a constant if BASE and STEP are constant
2224 and a VEC_SERIES_EXPR otherwise. */
2225
2226 tree
2227 build_vec_series (tree type, tree base, tree step)
2228 {
2229 if (integer_zerop (step))
2230 return build_vector_from_val (type, base);
2231 if (TREE_CODE (base) == INTEGER_CST && TREE_CODE (step) == INTEGER_CST)
2232 {
2233 tree_vector_builder builder (type, 1, 3);
2234 tree elt1 = wide_int_to_tree (TREE_TYPE (base),
2235 wi::to_wide (base) + wi::to_wide (step));
2236 tree elt2 = wide_int_to_tree (TREE_TYPE (base),
2237 wi::to_wide (elt1) + wi::to_wide (step));
2238 builder.quick_push (base);
2239 builder.quick_push (elt1);
2240 builder.quick_push (elt2);
2241 return builder.build ();
2242 }
2243 return build2 (VEC_SERIES_EXPR, type, base, step);
2244 }
2245
2246 /* Return a vector with the same number of units and number of bits
2247 as VEC_TYPE, but in which the elements are a linear series of unsigned
2248 integers { BASE, BASE + STEP, BASE + STEP * 2, ... }. */
2249
2250 tree
2251 build_index_vector (tree vec_type, poly_uint64 base, poly_uint64 step)
2252 {
2253 tree index_vec_type = vec_type;
2254 tree index_elt_type = TREE_TYPE (vec_type);
2255 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vec_type);
2256 if (!INTEGRAL_TYPE_P (index_elt_type) || !TYPE_UNSIGNED (index_elt_type))
2257 {
2258 index_elt_type = build_nonstandard_integer_type
2259 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (index_elt_type)), true);
2260 index_vec_type = build_vector_type (index_elt_type, nunits);
2261 }
2262
2263 tree_vector_builder v (index_vec_type, 1, 3);
2264 for (unsigned int i = 0; i < 3; ++i)
2265 v.quick_push (build_int_cstu (index_elt_type, base + i * step));
2266 return v.build ();
2267 }
2268
2269 /* Return a VECTOR_CST of type VEC_TYPE in which the first NUM_A
2270 elements are A and the rest are B. */
2271
2272 tree
2273 build_vector_a_then_b (tree vec_type, unsigned int num_a, tree a, tree b)
2274 {
2275 gcc_assert (known_le (num_a, TYPE_VECTOR_SUBPARTS (vec_type)));
2276 unsigned int count = constant_lower_bound (TYPE_VECTOR_SUBPARTS (vec_type));
2277 /* Optimize the constant case. */
2278 if ((count & 1) == 0 && TYPE_VECTOR_SUBPARTS (vec_type).is_constant ())
2279 count /= 2;
2280 tree_vector_builder builder (vec_type, count, 2);
2281 for (unsigned int i = 0; i < count * 2; ++i)
2282 builder.quick_push (i < num_a ? a : b);
2283 return builder.build ();
2284 }
2285
2286 /* Something has messed with the elements of CONSTRUCTOR C after it was built;
2287 calculate TREE_CONSTANT and TREE_SIDE_EFFECTS. */
2288
2289 void
2290 recompute_constructor_flags (tree c)
2291 {
2292 unsigned int i;
2293 tree val;
2294 bool constant_p = true;
2295 bool side_effects_p = false;
2296 vec<constructor_elt, va_gc> *vals = CONSTRUCTOR_ELTS (c);
2297
2298 FOR_EACH_CONSTRUCTOR_VALUE (vals, i, val)
2299 {
2300 /* Mostly ctors will have elts that don't have side-effects, so
2301 the usual case is to scan all the elements. Hence a single
2302 loop for both const and side effects, rather than one loop
2303 each (with early outs). */
2304 if (!TREE_CONSTANT (val))
2305 constant_p = false;
2306 if (TREE_SIDE_EFFECTS (val))
2307 side_effects_p = true;
2308 }
2309
2310 TREE_SIDE_EFFECTS (c) = side_effects_p;
2311 TREE_CONSTANT (c) = constant_p;
2312 }
2313
2314 /* Make sure that TREE_CONSTANT and TREE_SIDE_EFFECTS are correct for
2315 CONSTRUCTOR C. */
2316
2317 void
2318 verify_constructor_flags (tree c)
2319 {
2320 unsigned int i;
2321 tree val;
2322 bool constant_p = TREE_CONSTANT (c);
2323 bool side_effects_p = TREE_SIDE_EFFECTS (c);
2324 vec<constructor_elt, va_gc> *vals = CONSTRUCTOR_ELTS (c);
2325
2326 FOR_EACH_CONSTRUCTOR_VALUE (vals, i, val)
2327 {
2328 if (constant_p && !TREE_CONSTANT (val))
2329 internal_error ("non-constant element in constant CONSTRUCTOR");
2330 if (!side_effects_p && TREE_SIDE_EFFECTS (val))
2331 internal_error ("side-effects element in no-side-effects CONSTRUCTOR");
2332 }
2333 }
2334
2335 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
2336 are in the vec pointed to by VALS. */
2337 tree
2338 build_constructor (tree type, vec<constructor_elt, va_gc> *vals MEM_STAT_DECL)
2339 {
2340 tree c = make_node (CONSTRUCTOR PASS_MEM_STAT);
2341
2342 TREE_TYPE (c) = type;
2343 CONSTRUCTOR_ELTS (c) = vals;
2344
2345 recompute_constructor_flags (c);
2346
2347 return c;
2348 }
2349
2350 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
2351 INDEX and VALUE. */
2352 tree
2353 build_constructor_single (tree type, tree index, tree value)
2354 {
2355 vec<constructor_elt, va_gc> *v;
2356 constructor_elt elt = {index, value};
2357
2358 vec_alloc (v, 1);
2359 v->quick_push (elt);
2360
2361 return build_constructor (type, v);
2362 }
2363
2364
2365 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
2366 are in a list pointed to by VALS. */
2367 tree
2368 build_constructor_from_list (tree type, tree vals)
2369 {
2370 tree t;
2371 vec<constructor_elt, va_gc> *v = NULL;
2372
2373 if (vals)
2374 {
2375 vec_alloc (v, list_length (vals));
2376 for (t = vals; t; t = TREE_CHAIN (t))
2377 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
2378 }
2379
2380 return build_constructor (type, v);
2381 }
2382
2383 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
2384 are in a vector pointed to by VALS. Note that the TREE_PURPOSE
2385 fields in the constructor remain null. */
2386
2387 tree
2388 build_constructor_from_vec (tree type, const vec<tree, va_gc> *vals)
2389 {
2390 vec<constructor_elt, va_gc> *v = NULL;
2391
2392 for (tree t : vals)
2393 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, t);
2394
2395 return build_constructor (type, v);
2396 }
2397
2398 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
2399 of elements, provided as index/value pairs. */
2400
2401 tree
2402 build_constructor_va (tree type, int nelts, ...)
2403 {
2404 vec<constructor_elt, va_gc> *v = NULL;
2405 va_list p;
2406
2407 va_start (p, nelts);
2408 vec_alloc (v, nelts);
2409 while (nelts--)
2410 {
2411 tree index = va_arg (p, tree);
2412 tree value = va_arg (p, tree);
2413 CONSTRUCTOR_APPEND_ELT (v, index, value);
2414 }
2415 va_end (p);
2416 return build_constructor (type, v);
2417 }
2418
2419 /* Return a node of type TYPE for which TREE_CLOBBER_P is true. */
2420
2421 tree
2422 build_clobber (tree type, enum clobber_kind kind)
2423 {
2424 tree clobber = build_constructor (type, NULL);
2425 TREE_THIS_VOLATILE (clobber) = true;
2426 CLOBBER_KIND (clobber) = kind;
2427 return clobber;
2428 }
2429
2430 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
2431
2432 tree
2433 build_fixed (tree type, FIXED_VALUE_TYPE f)
2434 {
2435 tree v;
2436 FIXED_VALUE_TYPE *fp;
2437
2438 v = make_node (FIXED_CST);
2439 fp = ggc_alloc<fixed_value> ();
2440 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
2441
2442 TREE_TYPE (v) = type;
2443 TREE_FIXED_CST_PTR (v) = fp;
2444 return v;
2445 }
2446
2447 /* Return a new REAL_CST node whose type is TYPE and value is D. */
2448
2449 tree
2450 build_real (tree type, REAL_VALUE_TYPE d)
2451 {
2452 tree v;
2453 int overflow = 0;
2454
2455 /* dconst{0,1,2,m1,half} are used in various places in
2456 the middle-end and optimizers, allow them here
2457 even for decimal floating point types as an exception
2458 by converting them to decimal. */
2459 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type))
2460 && (d.cl == rvc_normal || d.cl == rvc_zero)
2461 && !d.decimal)
2462 {
2463 if (memcmp (&d, &dconst1, sizeof (d)) == 0)
2464 decimal_real_from_string (&d, "1");
2465 else if (memcmp (&d, &dconst2, sizeof (d)) == 0)
2466 decimal_real_from_string (&d, "2");
2467 else if (memcmp (&d, &dconstm1, sizeof (d)) == 0)
2468 decimal_real_from_string (&d, "-1");
2469 else if (memcmp (&d, &dconsthalf, sizeof (d)) == 0)
2470 decimal_real_from_string (&d, "0.5");
2471 else if (memcmp (&d, &dconst0, sizeof (d)) == 0)
2472 {
2473 /* Make sure to give zero the minimum quantum exponent for
2474 the type (which corresponds to all bits zero). */
2475 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
2476 char buf[16];
2477 sprintf (buf, "0e%d", fmt->emin - fmt->p);
2478 decimal_real_from_string (&d, buf);
2479 }
2480 else
2481 gcc_unreachable ();
2482 }
2483
2484 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
2485 Consider doing it via real_convert now. */
2486
2487 v = make_node (REAL_CST);
2488 TREE_TYPE (v) = type;
2489 memcpy (TREE_REAL_CST_PTR (v), &d, sizeof (REAL_VALUE_TYPE));
2490 TREE_OVERFLOW (v) = overflow;
2491 return v;
2492 }
2493
2494 /* Like build_real, but first truncate D to the type. */
2495
2496 tree
2497 build_real_truncate (tree type, REAL_VALUE_TYPE d)
2498 {
2499 return build_real (type, real_value_truncate (TYPE_MODE (type), d));
2500 }
2501
2502 /* Return a new REAL_CST node whose type is TYPE
2503 and whose value is the integer value of the INTEGER_CST node I. */
2504
2505 REAL_VALUE_TYPE
2506 real_value_from_int_cst (const_tree type, const_tree i)
2507 {
2508 REAL_VALUE_TYPE d;
2509
2510 /* Clear all bits of the real value type so that we can later do
2511 bitwise comparisons to see if two values are the same. */
2512 memset (&d, 0, sizeof d);
2513
2514 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode, wi::to_wide (i),
2515 TYPE_SIGN (TREE_TYPE (i)));
2516 return d;
2517 }
2518
2519 /* Given a tree representing an integer constant I, return a tree
2520 representing the same value as a floating-point constant of type TYPE. */
2521
2522 tree
2523 build_real_from_int_cst (tree type, const_tree i)
2524 {
2525 tree v;
2526 int overflow = TREE_OVERFLOW (i);
2527
2528 v = build_real (type, real_value_from_int_cst (type, i));
2529
2530 TREE_OVERFLOW (v) |= overflow;
2531 return v;
2532 }
2533
2534 /* Return a new REAL_CST node whose type is TYPE
2535 and whose value is the integer value I which has sign SGN. */
2536
2537 tree
2538 build_real_from_wide (tree type, const wide_int_ref &i, signop sgn)
2539 {
2540 REAL_VALUE_TYPE d;
2541
2542 /* Clear all bits of the real value type so that we can later do
2543 bitwise comparisons to see if two values are the same. */
2544 memset (&d, 0, sizeof d);
2545
2546 real_from_integer (&d, TYPE_MODE (type), i, sgn);
2547 return build_real (type, d);
2548 }
2549
2550 /* Return a newly constructed STRING_CST node whose value is the LEN
2551 characters at STR when STR is nonnull, or all zeros otherwise.
2552 Note that for a C string literal, LEN should include the trailing NUL.
2553 The TREE_TYPE is not initialized. */
2554
2555 tree
2556 build_string (unsigned len, const char *str /*= NULL */)
2557 {
2558 /* Do not waste bytes provided by padding of struct tree_string. */
2559 unsigned size = len + offsetof (struct tree_string, str) + 1;
2560
2561 record_node_allocation_statistics (STRING_CST, size);
2562
2563 tree s = (tree) ggc_internal_alloc (size);
2564
2565 memset (s, 0, sizeof (struct tree_typed));
2566 TREE_SET_CODE (s, STRING_CST);
2567 TREE_CONSTANT (s) = 1;
2568 TREE_STRING_LENGTH (s) = len;
2569 if (str)
2570 memcpy (s->string.str, str, len);
2571 else
2572 memset (s->string.str, 0, len);
2573 s->string.str[len] = '\0';
2574
2575 return s;
2576 }
2577
2578 /* Return a newly constructed COMPLEX_CST node whose value is
2579 specified by the real and imaginary parts REAL and IMAG.
2580 Both REAL and IMAG should be constant nodes. TYPE, if specified,
2581 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
2582
2583 tree
2584 build_complex (tree type, tree real, tree imag)
2585 {
2586 gcc_assert (CONSTANT_CLASS_P (real));
2587 gcc_assert (CONSTANT_CLASS_P (imag));
2588
2589 tree t = make_node (COMPLEX_CST);
2590
2591 TREE_REALPART (t) = real;
2592 TREE_IMAGPART (t) = imag;
2593 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
2594 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
2595 return t;
2596 }
2597
2598 /* Build a complex (inf +- 0i), such as for the result of cproj.
2599 TYPE is the complex tree type of the result. If NEG is true, the
2600 imaginary zero is negative. */
2601
2602 tree
2603 build_complex_inf (tree type, bool neg)
2604 {
2605 REAL_VALUE_TYPE rzero = dconst0;
2606
2607 rzero.sign = neg;
2608 return build_complex (type, build_real (TREE_TYPE (type), dconstinf),
2609 build_real (TREE_TYPE (type), rzero));
2610 }
2611
2612 /* Return the constant 1 in type TYPE. If TYPE has several elements, each
2613 element is set to 1. In particular, this is 1 + i for complex types. */
2614
2615 tree
2616 build_each_one_cst (tree type)
2617 {
2618 if (TREE_CODE (type) == COMPLEX_TYPE)
2619 {
2620 tree scalar = build_one_cst (TREE_TYPE (type));
2621 return build_complex (type, scalar, scalar);
2622 }
2623 else
2624 return build_one_cst (type);
2625 }
2626
2627 /* Return a constant of arithmetic type TYPE which is the
2628 multiplicative identity of the set TYPE. */
2629
2630 tree
2631 build_one_cst (tree type)
2632 {
2633 switch (TREE_CODE (type))
2634 {
2635 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2636 case POINTER_TYPE: case REFERENCE_TYPE:
2637 case OFFSET_TYPE: case BITINT_TYPE:
2638 return build_int_cst (type, 1);
2639
2640 case REAL_TYPE:
2641 return build_real (type, dconst1);
2642
2643 case FIXED_POINT_TYPE:
2644 /* We can only generate 1 for accum types. */
2645 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2646 return build_fixed (type, FCONST1 (TYPE_MODE (type)));
2647
2648 case VECTOR_TYPE:
2649 {
2650 tree scalar = build_one_cst (TREE_TYPE (type));
2651
2652 return build_vector_from_val (type, scalar);
2653 }
2654
2655 case COMPLEX_TYPE:
2656 return build_complex (type,
2657 build_one_cst (TREE_TYPE (type)),
2658 build_zero_cst (TREE_TYPE (type)));
2659
2660 default:
2661 gcc_unreachable ();
2662 }
2663 }
2664
2665 /* Return an integer of type TYPE containing all 1's in as much precision as
2666 it contains, or a complex or vector whose subparts are such integers. */
2667
2668 tree
2669 build_all_ones_cst (tree type)
2670 {
2671 if (TREE_CODE (type) == COMPLEX_TYPE)
2672 {
2673 tree scalar = build_all_ones_cst (TREE_TYPE (type));
2674 return build_complex (type, scalar, scalar);
2675 }
2676 else
2677 return build_minus_one_cst (type);
2678 }
2679
2680 /* Return a constant of arithmetic type TYPE which is the
2681 opposite of the multiplicative identity of the set TYPE. */
2682
2683 tree
2684 build_minus_one_cst (tree type)
2685 {
2686 switch (TREE_CODE (type))
2687 {
2688 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2689 case POINTER_TYPE: case REFERENCE_TYPE:
2690 case OFFSET_TYPE: case BITINT_TYPE:
2691 return build_int_cst (type, -1);
2692
2693 case REAL_TYPE:
2694 return build_real (type, dconstm1);
2695
2696 case FIXED_POINT_TYPE:
2697 /* We can only generate 1 for accum types. */
2698 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2699 return build_fixed (type,
2700 fixed_from_double_int (double_int_minus_one,
2701 SCALAR_TYPE_MODE (type)));
2702
2703 case VECTOR_TYPE:
2704 {
2705 tree scalar = build_minus_one_cst (TREE_TYPE (type));
2706
2707 return build_vector_from_val (type, scalar);
2708 }
2709
2710 case COMPLEX_TYPE:
2711 return build_complex (type,
2712 build_minus_one_cst (TREE_TYPE (type)),
2713 build_zero_cst (TREE_TYPE (type)));
2714
2715 default:
2716 gcc_unreachable ();
2717 }
2718 }
2719
2720 /* Build 0 constant of type TYPE. This is used by constructor folding
2721 and thus the constant should be represented in memory by
2722 zero(es). */
2723
2724 tree
2725 build_zero_cst (tree type)
2726 {
2727 switch (TREE_CODE (type))
2728 {
2729 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2730 case POINTER_TYPE: case REFERENCE_TYPE:
2731 case OFFSET_TYPE: case NULLPTR_TYPE: case BITINT_TYPE:
2732 return build_int_cst (type, 0);
2733
2734 case REAL_TYPE:
2735 return build_real (type, dconst0);
2736
2737 case FIXED_POINT_TYPE:
2738 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
2739
2740 case VECTOR_TYPE:
2741 {
2742 tree scalar = build_zero_cst (TREE_TYPE (type));
2743
2744 return build_vector_from_val (type, scalar);
2745 }
2746
2747 case COMPLEX_TYPE:
2748 {
2749 tree zero = build_zero_cst (TREE_TYPE (type));
2750
2751 return build_complex (type, zero, zero);
2752 }
2753
2754 default:
2755 if (!AGGREGATE_TYPE_P (type))
2756 return fold_convert (type, integer_zero_node);
2757 return build_constructor (type, NULL);
2758 }
2759 }
2760
2761 /* Build a constant of integer type TYPE, made of VALUE's bits replicated
2762 every WIDTH bits to fit TYPE's precision. */
2763
2764 tree
2765 build_replicated_int_cst (tree type, unsigned int width, HOST_WIDE_INT value)
2766 {
2767 int n = ((TYPE_PRECISION (type) + HOST_BITS_PER_WIDE_INT - 1)
2768 / HOST_BITS_PER_WIDE_INT);
2769 unsigned HOST_WIDE_INT low, mask;
2770 HOST_WIDE_INT a[WIDE_INT_MAX_INL_ELTS];
2771 int i;
2772
2773 gcc_assert (n && n <= WIDE_INT_MAX_INL_ELTS);
2774
2775 if (width == HOST_BITS_PER_WIDE_INT)
2776 low = value;
2777 else
2778 {
2779 mask = (HOST_WIDE_INT_1U << width) - 1;
2780 low = (unsigned HOST_WIDE_INT) ~0 / mask * (value & mask);
2781 }
2782
2783 for (i = 0; i < n; i++)
2784 a[i] = low;
2785
2786 gcc_assert (TYPE_PRECISION (type) <= MAX_BITSIZE_MODE_ANY_INT);
2787 return wide_int_to_tree (type, wide_int::from_array (a, n,
2788 TYPE_PRECISION (type)));
2789 }
2790
2791 /* If floating-point type TYPE has an IEEE-style sign bit, return an
2792 unsigned constant in which only the sign bit is set. Return null
2793 otherwise. */
2794
2795 tree
2796 sign_mask_for (tree type)
2797 {
2798 /* Avoid having to choose between a real-only sign and a pair of signs.
2799 This could be relaxed if the choice becomes obvious later. */
2800 if (TREE_CODE (type) == COMPLEX_TYPE)
2801 return NULL_TREE;
2802
2803 auto eltmode = as_a<scalar_float_mode> (element_mode (type));
2804 auto bits = REAL_MODE_FORMAT (eltmode)->ieee_bits;
2805 if (!bits || !pow2p_hwi (bits))
2806 return NULL_TREE;
2807
2808 tree inttype = unsigned_type_for (type);
2809 if (!inttype)
2810 return NULL_TREE;
2811
2812 auto mask = wi::set_bit_in_zero (bits - 1, bits);
2813 if (VECTOR_TYPE_P (inttype))
2814 {
2815 tree elt = wide_int_to_tree (TREE_TYPE (inttype), mask);
2816 return build_vector_from_val (inttype, elt);
2817 }
2818 return wide_int_to_tree (inttype, mask);
2819 }
2820
2821 /* Build a BINFO with LEN language slots. */
2822
2823 tree
2824 make_tree_binfo (unsigned base_binfos MEM_STAT_DECL)
2825 {
2826 tree t;
2827 size_t length = (offsetof (struct tree_binfo, base_binfos)
2828 + vec<tree, va_gc>::embedded_size (base_binfos));
2829
2830 record_node_allocation_statistics (TREE_BINFO, length);
2831
2832 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
2833
2834 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
2835
2836 TREE_SET_CODE (t, TREE_BINFO);
2837
2838 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
2839
2840 return t;
2841 }
2842
2843 /* Create a CASE_LABEL_EXPR tree node and return it. */
2844
2845 tree
2846 build_case_label (tree low_value, tree high_value, tree label_decl)
2847 {
2848 tree t = make_node (CASE_LABEL_EXPR);
2849
2850 TREE_TYPE (t) = void_type_node;
2851 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
2852
2853 CASE_LOW (t) = low_value;
2854 CASE_HIGH (t) = high_value;
2855 CASE_LABEL (t) = label_decl;
2856 CASE_CHAIN (t) = NULL_TREE;
2857
2858 return t;
2859 }
2860
2861 /* Build a newly constructed INTEGER_CST node. LEN and EXT_LEN are the
2862 values of TREE_INT_CST_NUNITS and TREE_INT_CST_EXT_NUNITS respectively.
2863 The latter determines the length of the HOST_WIDE_INT vector. */
2864
2865 tree
2866 make_int_cst (int len, int ext_len MEM_STAT_DECL)
2867 {
2868 tree t;
2869 int length = ((ext_len - 1) * sizeof (HOST_WIDE_INT)
2870 + sizeof (struct tree_int_cst));
2871
2872 gcc_assert (len);
2873 record_node_allocation_statistics (INTEGER_CST, length);
2874
2875 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2876
2877 TREE_SET_CODE (t, INTEGER_CST);
2878 TREE_INT_CST_NUNITS (t) = len;
2879 TREE_INT_CST_EXT_NUNITS (t) = ext_len;
2880 TREE_CONSTANT (t) = 1;
2881
2882 return t;
2883 }
2884
2885 /* Build a newly constructed TREE_VEC node of length LEN. */
2886
2887 tree
2888 make_tree_vec (int len MEM_STAT_DECL)
2889 {
2890 tree t;
2891 size_t length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2892
2893 record_node_allocation_statistics (TREE_VEC, length);
2894
2895 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2896
2897 TREE_SET_CODE (t, TREE_VEC);
2898 TREE_VEC_LENGTH (t) = len;
2899
2900 return t;
2901 }
2902
2903 /* Grow a TREE_VEC node to new length LEN. */
2904
2905 tree
2906 grow_tree_vec (tree v, int len MEM_STAT_DECL)
2907 {
2908 gcc_assert (TREE_CODE (v) == TREE_VEC);
2909
2910 int oldlen = TREE_VEC_LENGTH (v);
2911 gcc_assert (len > oldlen);
2912
2913 size_t oldlength = (oldlen - 1) * sizeof (tree) + sizeof (struct tree_vec);
2914 size_t length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2915
2916 record_node_allocation_statistics (TREE_VEC, length - oldlength);
2917
2918 v = (tree) ggc_realloc (v, length PASS_MEM_STAT);
2919
2920 TREE_VEC_LENGTH (v) = len;
2921
2922 return v;
2923 }
2924
2925 /* Return true if EXPR is the constant zero, whether it is integral, float or
2926 fixed, and scalar, complex or vector. */
2927
2928 bool
2929 zerop (const_tree expr)
2930 {
2931 return (integer_zerop (expr)
2932 || real_zerop (expr)
2933 || fixed_zerop (expr));
2934 }
2935
2936 /* Return true if EXPR is the integer constant zero or a complex constant
2937 of zero, or a location wrapper for such a constant. */
2938
2939 bool
2940 integer_zerop (const_tree expr)
2941 {
2942 STRIP_ANY_LOCATION_WRAPPER (expr);
2943
2944 switch (TREE_CODE (expr))
2945 {
2946 case INTEGER_CST:
2947 return wi::to_wide (expr) == 0;
2948 case COMPLEX_CST:
2949 return (integer_zerop (TREE_REALPART (expr))
2950 && integer_zerop (TREE_IMAGPART (expr)));
2951 case VECTOR_CST:
2952 return (VECTOR_CST_NPATTERNS (expr) == 1
2953 && VECTOR_CST_DUPLICATE_P (expr)
2954 && integer_zerop (VECTOR_CST_ENCODED_ELT (expr, 0)));
2955 default:
2956 return false;
2957 }
2958 }
2959
2960 /* Return true if EXPR is the integer constant one or the corresponding
2961 complex constant, or a location wrapper for such a constant. */
2962
2963 bool
2964 integer_onep (const_tree expr)
2965 {
2966 STRIP_ANY_LOCATION_WRAPPER (expr);
2967
2968 switch (TREE_CODE (expr))
2969 {
2970 case INTEGER_CST:
2971 return wi::eq_p (wi::to_widest (expr), 1);
2972 case COMPLEX_CST:
2973 return (integer_onep (TREE_REALPART (expr))
2974 && integer_zerop (TREE_IMAGPART (expr)));
2975 case VECTOR_CST:
2976 return (VECTOR_CST_NPATTERNS (expr) == 1
2977 && VECTOR_CST_DUPLICATE_P (expr)
2978 && integer_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
2979 default:
2980 return false;
2981 }
2982 }
2983
2984 /* Return true if EXPR is the integer constant one. For complex and vector,
2985 return true if every piece is the integer constant one.
2986 Also return true for location wrappers for such a constant. */
2987
2988 bool
2989 integer_each_onep (const_tree expr)
2990 {
2991 STRIP_ANY_LOCATION_WRAPPER (expr);
2992
2993 if (TREE_CODE (expr) == COMPLEX_CST)
2994 return (integer_onep (TREE_REALPART (expr))
2995 && integer_onep (TREE_IMAGPART (expr)));
2996 else
2997 return integer_onep (expr);
2998 }
2999
3000 /* Return true if EXPR is an integer containing all 1's in as much precision
3001 as it contains, or a complex or vector whose subparts are such integers,
3002 or a location wrapper for such a constant. */
3003
3004 bool
3005 integer_all_onesp (const_tree expr)
3006 {
3007 STRIP_ANY_LOCATION_WRAPPER (expr);
3008
3009 if (TREE_CODE (expr) == COMPLEX_CST
3010 && integer_all_onesp (TREE_REALPART (expr))
3011 && integer_all_onesp (TREE_IMAGPART (expr)))
3012 return true;
3013
3014 else if (TREE_CODE (expr) == VECTOR_CST)
3015 return (VECTOR_CST_NPATTERNS (expr) == 1
3016 && VECTOR_CST_DUPLICATE_P (expr)
3017 && integer_all_onesp (VECTOR_CST_ENCODED_ELT (expr, 0)));
3018
3019 else if (TREE_CODE (expr) != INTEGER_CST)
3020 return false;
3021
3022 return (wi::max_value (TYPE_PRECISION (TREE_TYPE (expr)), UNSIGNED)
3023 == wi::to_wide (expr));
3024 }
3025
3026 /* Return true if EXPR is the integer constant minus one, or a location
3027 wrapper for such a constant. */
3028
3029 bool
3030 integer_minus_onep (const_tree expr)
3031 {
3032 STRIP_ANY_LOCATION_WRAPPER (expr);
3033
3034 if (TREE_CODE (expr) == COMPLEX_CST)
3035 return (integer_all_onesp (TREE_REALPART (expr))
3036 && integer_zerop (TREE_IMAGPART (expr)));
3037 else
3038 return integer_all_onesp (expr);
3039 }
3040
3041 /* Return true if EXPR is an integer constant that is a power of 2 (i.e., has
3042 only one bit on), or a location wrapper for such a constant. */
3043
3044 bool
3045 integer_pow2p (const_tree expr)
3046 {
3047 STRIP_ANY_LOCATION_WRAPPER (expr);
3048
3049 if (TREE_CODE (expr) == COMPLEX_CST
3050 && integer_pow2p (TREE_REALPART (expr))
3051 && integer_zerop (TREE_IMAGPART (expr)))
3052 return true;
3053
3054 if (TREE_CODE (expr) != INTEGER_CST)
3055 return false;
3056
3057 return wi::popcount (wi::to_wide (expr)) == 1;
3058 }
3059
3060 /* Return true if EXPR is an integer constant other than zero or a
3061 complex constant other than zero, or a location wrapper for such a
3062 constant. */
3063
3064 bool
3065 integer_nonzerop (const_tree expr)
3066 {
3067 STRIP_ANY_LOCATION_WRAPPER (expr);
3068
3069 return ((TREE_CODE (expr) == INTEGER_CST
3070 && wi::to_wide (expr) != 0)
3071 || (TREE_CODE (expr) == COMPLEX_CST
3072 && (integer_nonzerop (TREE_REALPART (expr))
3073 || integer_nonzerop (TREE_IMAGPART (expr)))));
3074 }
3075
3076 /* Return true if EXPR is the integer constant one. For vector,
3077 return true if every piece is the integer constant minus one
3078 (representing the value TRUE).
3079 Also return true for location wrappers for such a constant. */
3080
3081 bool
3082 integer_truep (const_tree expr)
3083 {
3084 STRIP_ANY_LOCATION_WRAPPER (expr);
3085
3086 if (TREE_CODE (expr) == VECTOR_CST)
3087 return integer_all_onesp (expr);
3088 return integer_onep (expr);
3089 }
3090
3091 /* Return true if EXPR is the fixed-point constant zero, or a location wrapper
3092 for such a constant. */
3093
3094 bool
3095 fixed_zerop (const_tree expr)
3096 {
3097 STRIP_ANY_LOCATION_WRAPPER (expr);
3098
3099 return (TREE_CODE (expr) == FIXED_CST
3100 && TREE_FIXED_CST (expr).data.is_zero ());
3101 }
3102
3103 /* Return the power of two represented by a tree node known to be a
3104 power of two. */
3105
3106 int
3107 tree_log2 (const_tree expr)
3108 {
3109 if (TREE_CODE (expr) == COMPLEX_CST)
3110 return tree_log2 (TREE_REALPART (expr));
3111
3112 return wi::exact_log2 (wi::to_wide (expr));
3113 }
3114
3115 /* Similar, but return the largest integer Y such that 2 ** Y is less
3116 than or equal to EXPR. */
3117
3118 int
3119 tree_floor_log2 (const_tree expr)
3120 {
3121 if (TREE_CODE (expr) == COMPLEX_CST)
3122 return tree_log2 (TREE_REALPART (expr));
3123
3124 return wi::floor_log2 (wi::to_wide (expr));
3125 }
3126
3127 /* Return number of known trailing zero bits in EXPR, or, if the value of
3128 EXPR is known to be zero, the precision of it's type. */
3129
3130 unsigned int
3131 tree_ctz (const_tree expr)
3132 {
3133 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
3134 && !POINTER_TYPE_P (TREE_TYPE (expr)))
3135 return 0;
3136
3137 unsigned int ret1, ret2, prec = TYPE_PRECISION (TREE_TYPE (expr));
3138 switch (TREE_CODE (expr))
3139 {
3140 case INTEGER_CST:
3141 ret1 = wi::ctz (wi::to_wide (expr));
3142 return MIN (ret1, prec);
3143 case SSA_NAME:
3144 ret1 = wi::ctz (get_nonzero_bits (expr));
3145 return MIN (ret1, prec);
3146 case PLUS_EXPR:
3147 case MINUS_EXPR:
3148 case BIT_IOR_EXPR:
3149 case BIT_XOR_EXPR:
3150 case MIN_EXPR:
3151 case MAX_EXPR:
3152 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3153 if (ret1 == 0)
3154 return ret1;
3155 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
3156 return MIN (ret1, ret2);
3157 case POINTER_PLUS_EXPR:
3158 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3159 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
3160 /* Second operand is sizetype, which could be in theory
3161 wider than pointer's precision. Make sure we never
3162 return more than prec. */
3163 ret2 = MIN (ret2, prec);
3164 return MIN (ret1, ret2);
3165 case BIT_AND_EXPR:
3166 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3167 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
3168 return MAX (ret1, ret2);
3169 case MULT_EXPR:
3170 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3171 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
3172 return MIN (ret1 + ret2, prec);
3173 case LSHIFT_EXPR:
3174 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3175 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
3176 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
3177 {
3178 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
3179 return MIN (ret1 + ret2, prec);
3180 }
3181 return ret1;
3182 case RSHIFT_EXPR:
3183 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
3184 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
3185 {
3186 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3187 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
3188 if (ret1 > ret2)
3189 return ret1 - ret2;
3190 }
3191 return 0;
3192 case TRUNC_DIV_EXPR:
3193 case CEIL_DIV_EXPR:
3194 case FLOOR_DIV_EXPR:
3195 case ROUND_DIV_EXPR:
3196 case EXACT_DIV_EXPR:
3197 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
3198 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) == 1)
3199 {
3200 int l = tree_log2 (TREE_OPERAND (expr, 1));
3201 if (l >= 0)
3202 {
3203 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3204 ret2 = l;
3205 if (ret1 > ret2)
3206 return ret1 - ret2;
3207 }
3208 }
3209 return 0;
3210 CASE_CONVERT:
3211 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
3212 if (ret1 && ret1 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
3213 ret1 = prec;
3214 return MIN (ret1, prec);
3215 case SAVE_EXPR:
3216 return tree_ctz (TREE_OPERAND (expr, 0));
3217 case COND_EXPR:
3218 ret1 = tree_ctz (TREE_OPERAND (expr, 1));
3219 if (ret1 == 0)
3220 return 0;
3221 ret2 = tree_ctz (TREE_OPERAND (expr, 2));
3222 return MIN (ret1, ret2);
3223 case COMPOUND_EXPR:
3224 return tree_ctz (TREE_OPERAND (expr, 1));
3225 case ADDR_EXPR:
3226 ret1 = get_pointer_alignment (CONST_CAST_TREE (expr));
3227 if (ret1 > BITS_PER_UNIT)
3228 {
3229 ret1 = ctz_hwi (ret1 / BITS_PER_UNIT);
3230 return MIN (ret1, prec);
3231 }
3232 return 0;
3233 default:
3234 return 0;
3235 }
3236 }
3237
3238 /* Return true if EXPR is the real constant zero. Trailing zeroes matter for
3239 decimal float constants, so don't return true for them.
3240 Also return true for location wrappers around such a constant. */
3241
3242 bool
3243 real_zerop (const_tree expr)
3244 {
3245 STRIP_ANY_LOCATION_WRAPPER (expr);
3246
3247 switch (TREE_CODE (expr))
3248 {
3249 case REAL_CST:
3250 return real_equal (&TREE_REAL_CST (expr), &dconst0)
3251 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
3252 case COMPLEX_CST:
3253 return real_zerop (TREE_REALPART (expr))
3254 && real_zerop (TREE_IMAGPART (expr));
3255 case VECTOR_CST:
3256 {
3257 /* Don't simply check for a duplicate because the predicate
3258 accepts both +0.0 and -0.0. */
3259 unsigned count = vector_cst_encoded_nelts (expr);
3260 for (unsigned int i = 0; i < count; ++i)
3261 if (!real_zerop (VECTOR_CST_ENCODED_ELT (expr, i)))
3262 return false;
3263 return true;
3264 }
3265 default:
3266 return false;
3267 }
3268 }
3269
3270 /* Return true if EXPR is the real constant one in real or complex form.
3271 Trailing zeroes matter for decimal float constants, so don't return
3272 true for them.
3273 Also return true for location wrappers around such a constant. */
3274
3275 bool
3276 real_onep (const_tree expr)
3277 {
3278 STRIP_ANY_LOCATION_WRAPPER (expr);
3279
3280 switch (TREE_CODE (expr))
3281 {
3282 case REAL_CST:
3283 return real_equal (&TREE_REAL_CST (expr), &dconst1)
3284 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
3285 case COMPLEX_CST:
3286 return real_onep (TREE_REALPART (expr))
3287 && real_zerop (TREE_IMAGPART (expr));
3288 case VECTOR_CST:
3289 return (VECTOR_CST_NPATTERNS (expr) == 1
3290 && VECTOR_CST_DUPLICATE_P (expr)
3291 && real_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
3292 default:
3293 return false;
3294 }
3295 }
3296
3297 /* Return true if EXPR is the real constant minus one. Trailing zeroes
3298 matter for decimal float constants, so don't return true for them.
3299 Also return true for location wrappers around such a constant. */
3300
3301 bool
3302 real_minus_onep (const_tree expr)
3303 {
3304 STRIP_ANY_LOCATION_WRAPPER (expr);
3305
3306 switch (TREE_CODE (expr))
3307 {
3308 case REAL_CST:
3309 return real_equal (&TREE_REAL_CST (expr), &dconstm1)
3310 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
3311 case COMPLEX_CST:
3312 return real_minus_onep (TREE_REALPART (expr))
3313 && real_zerop (TREE_IMAGPART (expr));
3314 case VECTOR_CST:
3315 return (VECTOR_CST_NPATTERNS (expr) == 1
3316 && VECTOR_CST_DUPLICATE_P (expr)
3317 && real_minus_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
3318 default:
3319 return false;
3320 }
3321 }
3322
3323 /* Return true if T could be a floating point zero. */
3324
3325 bool
3326 real_maybe_zerop (const_tree expr)
3327 {
3328 switch (TREE_CODE (expr))
3329 {
3330 case REAL_CST:
3331 /* Can't use real_zerop here, as it always returns false for decimal
3332 floats. And can't use TREE_REAL_CST (expr).cl == rvc_zero
3333 either, as decimal zeros are rvc_normal. */
3334 return real_equal (&TREE_REAL_CST (expr), &dconst0);
3335 case COMPLEX_CST:
3336 return (real_maybe_zerop (TREE_REALPART (expr))
3337 || real_maybe_zerop (TREE_IMAGPART (expr)));
3338 case VECTOR_CST:
3339 {
3340 unsigned count = vector_cst_encoded_nelts (expr);
3341 for (unsigned int i = 0; i < count; ++i)
3342 if (real_maybe_zerop (VECTOR_CST_ENCODED_ELT (expr, i)))
3343 return true;
3344 return false;
3345 }
3346 default:
3347 /* Perhaps for SSA_NAMEs we could query frange. */
3348 return true;
3349 }
3350 }
3351
3352 /* True if EXP is a constant or a cast of a constant. */
3353
3354 bool
3355 really_constant_p (const_tree exp)
3356 {
3357 /* This is not quite the same as STRIP_NOPS. It does more. */
3358 while (CONVERT_EXPR_P (exp)
3359 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3360 exp = TREE_OPERAND (exp, 0);
3361 return TREE_CONSTANT (exp);
3362 }
3363
3364 /* Return true if T holds a polynomial pointer difference, storing it in
3365 *VALUE if so. A true return means that T's precision is no greater
3366 than 64 bits, which is the largest address space we support, so *VALUE
3367 never loses precision. However, the signedness of the result does
3368 not necessarily match the signedness of T: sometimes an unsigned type
3369 like sizetype is used to encode a value that is actually negative. */
3370
3371 bool
3372 ptrdiff_tree_p (const_tree t, poly_int64 *value)
3373 {
3374 if (!t)
3375 return false;
3376 if (TREE_CODE (t) == INTEGER_CST)
3377 {
3378 if (!cst_and_fits_in_hwi (t))
3379 return false;
3380 *value = int_cst_value (t);
3381 return true;
3382 }
3383 if (POLY_INT_CST_P (t))
3384 {
3385 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
3386 if (!cst_and_fits_in_hwi (POLY_INT_CST_COEFF (t, i)))
3387 return false;
3388 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
3389 value->coeffs[i] = int_cst_value (POLY_INT_CST_COEFF (t, i));
3390 return true;
3391 }
3392 return false;
3393 }
3394
3395 poly_int64
3396 tree_to_poly_int64 (const_tree t)
3397 {
3398 gcc_assert (tree_fits_poly_int64_p (t));
3399 if (POLY_INT_CST_P (t))
3400 return poly_int_cst_value (t).force_shwi ();
3401 return TREE_INT_CST_LOW (t);
3402 }
3403
3404 poly_uint64
3405 tree_to_poly_uint64 (const_tree t)
3406 {
3407 gcc_assert (tree_fits_poly_uint64_p (t));
3408 if (POLY_INT_CST_P (t))
3409 return poly_int_cst_value (t).force_uhwi ();
3410 return TREE_INT_CST_LOW (t);
3411 }
3412
3413 /* Return first list element whose TREE_VALUE is ELEM.
3414 Return 0 if ELEM is not in LIST. */
3415
3416 tree
3417 value_member (tree elem, tree list)
3418 {
3419 while (list)
3420 {
3421 if (elem == TREE_VALUE (list))
3422 return list;
3423 list = TREE_CHAIN (list);
3424 }
3425 return NULL_TREE;
3426 }
3427
3428 /* Return first list element whose TREE_PURPOSE is ELEM.
3429 Return 0 if ELEM is not in LIST. */
3430
3431 tree
3432 purpose_member (const_tree elem, tree list)
3433 {
3434 while (list)
3435 {
3436 if (elem == TREE_PURPOSE (list))
3437 return list;
3438 list = TREE_CHAIN (list);
3439 }
3440 return NULL_TREE;
3441 }
3442
3443 /* Return true if ELEM is in V. */
3444
3445 bool
3446 vec_member (const_tree elem, vec<tree, va_gc> *v)
3447 {
3448 unsigned ix;
3449 tree t;
3450 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
3451 if (elem == t)
3452 return true;
3453 return false;
3454 }
3455
3456 /* Returns element number IDX (zero-origin) of chain CHAIN, or
3457 NULL_TREE. */
3458
3459 tree
3460 chain_index (int idx, tree chain)
3461 {
3462 for (; chain && idx > 0; --idx)
3463 chain = TREE_CHAIN (chain);
3464 return chain;
3465 }
3466
3467 /* Return true if ELEM is part of the chain CHAIN. */
3468
3469 bool
3470 chain_member (const_tree elem, const_tree chain)
3471 {
3472 while (chain)
3473 {
3474 if (elem == chain)
3475 return true;
3476 chain = DECL_CHAIN (chain);
3477 }
3478
3479 return false;
3480 }
3481
3482 /* Return the length of a chain of nodes chained through TREE_CHAIN.
3483 We expect a null pointer to mark the end of the chain.
3484 This is the Lisp primitive `length'. */
3485
3486 int
3487 list_length (const_tree t)
3488 {
3489 const_tree p = t;
3490 #ifdef ENABLE_TREE_CHECKING
3491 const_tree q = t;
3492 #endif
3493 int len = 0;
3494
3495 while (p)
3496 {
3497 p = TREE_CHAIN (p);
3498 #ifdef ENABLE_TREE_CHECKING
3499 if (len % 2)
3500 q = TREE_CHAIN (q);
3501 gcc_assert (p != q);
3502 #endif
3503 len++;
3504 }
3505
3506 return len;
3507 }
3508
3509 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
3510 UNION_TYPE TYPE, or NULL_TREE if none. */
3511
3512 tree
3513 first_field (const_tree type)
3514 {
3515 tree t = TYPE_FIELDS (type);
3516 while (t && TREE_CODE (t) != FIELD_DECL)
3517 t = TREE_CHAIN (t);
3518 return t;
3519 }
3520
3521 /* Returns the last FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
3522 UNION_TYPE TYPE, or NULL_TREE if none. */
3523
3524 tree
3525 last_field (const_tree type)
3526 {
3527 tree last = NULL_TREE;
3528
3529 for (tree fld = TYPE_FIELDS (type); fld; fld = TREE_CHAIN (fld))
3530 {
3531 if (TREE_CODE (fld) != FIELD_DECL)
3532 continue;
3533
3534 last = fld;
3535 }
3536
3537 return last;
3538 }
3539
3540 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
3541 by modifying the last node in chain 1 to point to chain 2.
3542 This is the Lisp primitive `nconc'. */
3543
3544 tree
3545 chainon (tree op1, tree op2)
3546 {
3547 tree t1;
3548
3549 if (!op1)
3550 return op2;
3551 if (!op2)
3552 return op1;
3553
3554 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
3555 continue;
3556 TREE_CHAIN (t1) = op2;
3557
3558 #ifdef ENABLE_TREE_CHECKING
3559 {
3560 tree t2;
3561 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
3562 gcc_assert (t2 != t1);
3563 }
3564 #endif
3565
3566 return op1;
3567 }
3568
3569 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
3570
3571 tree
3572 tree_last (tree chain)
3573 {
3574 tree next;
3575 if (chain)
3576 while ((next = TREE_CHAIN (chain)))
3577 chain = next;
3578 return chain;
3579 }
3580
3581 /* Reverse the order of elements in the chain T,
3582 and return the new head of the chain (old last element). */
3583
3584 tree
3585 nreverse (tree t)
3586 {
3587 tree prev = 0, decl, next;
3588 for (decl = t; decl; decl = next)
3589 {
3590 /* We shouldn't be using this function to reverse BLOCK chains; we
3591 have blocks_nreverse for that. */
3592 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
3593 next = TREE_CHAIN (decl);
3594 TREE_CHAIN (decl) = prev;
3595 prev = decl;
3596 }
3597 return prev;
3598 }
3599
3600 /* Return a newly created TREE_LIST node whose
3601 purpose and value fields are PARM and VALUE. */
3602
3603 tree
3604 build_tree_list (tree parm, tree value MEM_STAT_DECL)
3605 {
3606 tree t = make_node (TREE_LIST PASS_MEM_STAT);
3607 TREE_PURPOSE (t) = parm;
3608 TREE_VALUE (t) = value;
3609 return t;
3610 }
3611
3612 /* Build a chain of TREE_LIST nodes from a vector. */
3613
3614 tree
3615 build_tree_list_vec (const vec<tree, va_gc> *vec MEM_STAT_DECL)
3616 {
3617 tree ret = NULL_TREE;
3618 tree *pp = &ret;
3619 unsigned int i;
3620 tree t;
3621 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
3622 {
3623 *pp = build_tree_list (NULL, t PASS_MEM_STAT);
3624 pp = &TREE_CHAIN (*pp);
3625 }
3626 return ret;
3627 }
3628
3629 /* Return a newly created TREE_LIST node whose
3630 purpose and value fields are PURPOSE and VALUE
3631 and whose TREE_CHAIN is CHAIN. */
3632
3633 tree
3634 tree_cons (tree purpose, tree value, tree chain MEM_STAT_DECL)
3635 {
3636 tree node;
3637
3638 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
3639 memset (node, 0, sizeof (struct tree_common));
3640
3641 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
3642
3643 TREE_SET_CODE (node, TREE_LIST);
3644 TREE_CHAIN (node) = chain;
3645 TREE_PURPOSE (node) = purpose;
3646 TREE_VALUE (node) = value;
3647 return node;
3648 }
3649
3650 /* Return the values of the elements of a CONSTRUCTOR as a vector of
3651 trees. */
3652
3653 vec<tree, va_gc> *
3654 ctor_to_vec (tree ctor)
3655 {
3656 vec<tree, va_gc> *vec;
3657 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
3658 unsigned int ix;
3659 tree val;
3660
3661 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
3662 vec->quick_push (val);
3663
3664 return vec;
3665 }
3666
3667 /* Return the size nominally occupied by an object of type TYPE
3668 when it resides in memory. The value is measured in units of bytes,
3669 and its data type is that normally used for type sizes
3670 (which is the first type created by make_signed_type or
3671 make_unsigned_type). */
3672
3673 tree
3674 size_in_bytes_loc (location_t loc, const_tree type)
3675 {
3676 tree t;
3677
3678 if (type == error_mark_node)
3679 return integer_zero_node;
3680
3681 type = TYPE_MAIN_VARIANT (type);
3682 t = TYPE_SIZE_UNIT (type);
3683
3684 if (t == 0)
3685 {
3686 lang_hooks.types.incomplete_type_error (loc, NULL_TREE, type);
3687 return size_zero_node;
3688 }
3689
3690 return t;
3691 }
3692
3693 /* Return the size of TYPE (in bytes) as a wide integer
3694 or return -1 if the size can vary or is larger than an integer. */
3695
3696 HOST_WIDE_INT
3697 int_size_in_bytes (const_tree type)
3698 {
3699 tree t;
3700
3701 if (type == error_mark_node)
3702 return 0;
3703
3704 type = TYPE_MAIN_VARIANT (type);
3705 t = TYPE_SIZE_UNIT (type);
3706
3707 if (t && tree_fits_uhwi_p (t))
3708 return TREE_INT_CST_LOW (t);
3709 else
3710 return -1;
3711 }
3712
3713 /* Return the maximum size of TYPE (in bytes) as a wide integer
3714 or return -1 if the size can vary or is larger than an integer. */
3715
3716 HOST_WIDE_INT
3717 max_int_size_in_bytes (const_tree type)
3718 {
3719 HOST_WIDE_INT size = -1;
3720 tree size_tree;
3721
3722 /* If this is an array type, check for a possible MAX_SIZE attached. */
3723
3724 if (TREE_CODE (type) == ARRAY_TYPE)
3725 {
3726 size_tree = TYPE_ARRAY_MAX_SIZE (type);
3727
3728 if (size_tree && tree_fits_uhwi_p (size_tree))
3729 size = tree_to_uhwi (size_tree);
3730 }
3731
3732 /* If we still haven't been able to get a size, see if the language
3733 can compute a maximum size. */
3734
3735 if (size == -1)
3736 {
3737 size_tree = lang_hooks.types.max_size (type);
3738
3739 if (size_tree && tree_fits_uhwi_p (size_tree))
3740 size = tree_to_uhwi (size_tree);
3741 }
3742
3743 return size;
3744 }
3745
3746 /* Return the bit position of FIELD, in bits from the start of the record.
3747 This is a tree of type bitsizetype. */
3748
3749 tree
3750 bit_position (const_tree field)
3751 {
3752 return bit_from_pos (DECL_FIELD_OFFSET (field),
3753 DECL_FIELD_BIT_OFFSET (field));
3754 }
3755
3756 /* Return the byte position of FIELD, in bytes from the start of the record.
3757 This is a tree of type sizetype. */
3758
3759 tree
3760 byte_position (const_tree field)
3761 {
3762 return byte_from_pos (DECL_FIELD_OFFSET (field),
3763 DECL_FIELD_BIT_OFFSET (field));
3764 }
3765
3766 /* Likewise, but return as an integer. It must be representable in
3767 that way (since it could be a signed value, we don't have the
3768 option of returning -1 like int_size_in_byte can. */
3769
3770 HOST_WIDE_INT
3771 int_byte_position (const_tree field)
3772 {
3773 return tree_to_shwi (byte_position (field));
3774 }
3775
3776 /* Return, as a tree node, the number of elements for TYPE (which is an
3777 ARRAY_TYPE) minus one. This counts only elements of the top array. */
3778
3779 tree
3780 array_type_nelts_minus_one (const_tree type)
3781 {
3782 tree index_type, min, max;
3783
3784 /* If they did it with unspecified bounds, then we should have already
3785 given an error about it before we got here. */
3786 if (! TYPE_DOMAIN (type))
3787 return error_mark_node;
3788
3789 index_type = TYPE_DOMAIN (type);
3790 min = TYPE_MIN_VALUE (index_type);
3791 max = TYPE_MAX_VALUE (index_type);
3792
3793 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
3794 if (!max)
3795 {
3796 /* zero sized arrays are represented from C FE as complete types with
3797 NULL TYPE_MAX_VALUE and zero TYPE_SIZE, while C++ FE represents
3798 them as min 0, max -1. */
3799 if (COMPLETE_TYPE_P (type)
3800 && integer_zerop (TYPE_SIZE (type))
3801 && integer_zerop (min))
3802 return build_int_cst (TREE_TYPE (min), -1);
3803
3804 return error_mark_node;
3805 }
3806
3807 return (integer_zerop (min)
3808 ? max
3809 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
3810 }
3811
3812 /* Return, as an INTEGER_CST node, the number of elements for TYPE
3813 (which is an ARRAY_TYPE). This counts only elements of the top
3814 array. */
3815
3816 tree
3817 array_type_nelts_top (tree type)
3818 {
3819 return fold_build2_loc (input_location,
3820 PLUS_EXPR, sizetype,
3821 array_type_nelts_minus_one (type),
3822 size_one_node);
3823 }
3824
3825 /* If arg is static -- a reference to an object in static storage -- then
3826 return the object. This is not the same as the C meaning of `static'.
3827 If arg isn't static, return NULL. */
3828
3829 tree
3830 staticp (tree arg)
3831 {
3832 switch (TREE_CODE (arg))
3833 {
3834 case FUNCTION_DECL:
3835 /* Nested functions are static, even though taking their address will
3836 involve a trampoline as we unnest the nested function and create
3837 the trampoline on the tree level. */
3838 return arg;
3839
3840 case VAR_DECL:
3841 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3842 && ! DECL_THREAD_LOCAL_P (arg)
3843 && ! DECL_DLLIMPORT_P (arg)
3844 ? arg : NULL);
3845
3846 case CONST_DECL:
3847 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3848 ? arg : NULL);
3849
3850 case CONSTRUCTOR:
3851 return TREE_STATIC (arg) ? arg : NULL;
3852
3853 case LABEL_DECL:
3854 case STRING_CST:
3855 return arg;
3856
3857 case COMPONENT_REF:
3858 /* If the thing being referenced is not a field, then it is
3859 something language specific. */
3860 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
3861
3862 /* If we are referencing a bitfield, we can't evaluate an
3863 ADDR_EXPR at compile time and so it isn't a constant. */
3864 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
3865 return NULL;
3866
3867 return staticp (TREE_OPERAND (arg, 0));
3868
3869 case BIT_FIELD_REF:
3870 return NULL;
3871
3872 case INDIRECT_REF:
3873 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
3874
3875 case ARRAY_REF:
3876 case ARRAY_RANGE_REF:
3877 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
3878 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
3879 return staticp (TREE_OPERAND (arg, 0));
3880 else
3881 return NULL;
3882
3883 case COMPOUND_LITERAL_EXPR:
3884 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
3885
3886 default:
3887 return NULL;
3888 }
3889 }
3890
3891
3892
3893
3894 /* Return whether OP is a DECL whose address is function-invariant. */
3895
3896 bool
3897 decl_address_invariant_p (const_tree op)
3898 {
3899 /* The conditions below are slightly less strict than the one in
3900 staticp. */
3901
3902 switch (TREE_CODE (op))
3903 {
3904 case PARM_DECL:
3905 case RESULT_DECL:
3906 case LABEL_DECL:
3907 case FUNCTION_DECL:
3908 return true;
3909
3910 case VAR_DECL:
3911 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3912 || DECL_THREAD_LOCAL_P (op)
3913 || DECL_CONTEXT (op) == current_function_decl
3914 || decl_function_context (op) == current_function_decl)
3915 return true;
3916 break;
3917
3918 case CONST_DECL:
3919 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3920 || decl_function_context (op) == current_function_decl)
3921 return true;
3922 break;
3923
3924 default:
3925 break;
3926 }
3927
3928 return false;
3929 }
3930
3931 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
3932
3933 bool
3934 decl_address_ip_invariant_p (const_tree op)
3935 {
3936 /* The conditions below are slightly less strict than the one in
3937 staticp. */
3938
3939 switch (TREE_CODE (op))
3940 {
3941 case LABEL_DECL:
3942 case FUNCTION_DECL:
3943 case STRING_CST:
3944 return true;
3945
3946 case VAR_DECL:
3947 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
3948 && !DECL_DLLIMPORT_P (op))
3949 || DECL_THREAD_LOCAL_P (op))
3950 return true;
3951 break;
3952
3953 case CONST_DECL:
3954 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
3955 return true;
3956 break;
3957
3958 default:
3959 break;
3960 }
3961
3962 return false;
3963 }
3964
3965
3966 /* Return true if T is function-invariant (internal function, does
3967 not handle arithmetic; that's handled in skip_simple_arithmetic and
3968 tree_invariant_p). */
3969
3970 static bool
3971 tree_invariant_p_1 (tree t)
3972 {
3973 tree op;
3974
3975 if (TREE_CONSTANT (t)
3976 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
3977 return true;
3978
3979 switch (TREE_CODE (t))
3980 {
3981 case SAVE_EXPR:
3982 case TARGET_EXPR:
3983 return true;
3984
3985 case ADDR_EXPR:
3986 op = TREE_OPERAND (t, 0);
3987 while (handled_component_p (op))
3988 {
3989 switch (TREE_CODE (op))
3990 {
3991 case ARRAY_REF:
3992 case ARRAY_RANGE_REF:
3993 if (!tree_invariant_p (TREE_OPERAND (op, 1))
3994 || TREE_OPERAND (op, 2) != NULL_TREE
3995 || TREE_OPERAND (op, 3) != NULL_TREE)
3996 return false;
3997 break;
3998
3999 case COMPONENT_REF:
4000 if (TREE_OPERAND (op, 2) != NULL_TREE)
4001 return false;
4002 break;
4003
4004 default:;
4005 }
4006 op = TREE_OPERAND (op, 0);
4007 }
4008
4009 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
4010
4011 default:
4012 break;
4013 }
4014
4015 return false;
4016 }
4017
4018 /* Return true if T is function-invariant. */
4019
4020 bool
4021 tree_invariant_p (tree t)
4022 {
4023 tree inner = skip_simple_arithmetic (t);
4024 return tree_invariant_p_1 (inner);
4025 }
4026
4027 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
4028 Do this to any expression which may be used in more than one place,
4029 but must be evaluated only once.
4030
4031 Normally, expand_expr would reevaluate the expression each time.
4032 Calling save_expr produces something that is evaluated and recorded
4033 the first time expand_expr is called on it. Subsequent calls to
4034 expand_expr just reuse the recorded value.
4035
4036 The call to expand_expr that generates code that actually computes
4037 the value is the first call *at compile time*. Subsequent calls
4038 *at compile time* generate code to use the saved value.
4039 This produces correct result provided that *at run time* control
4040 always flows through the insns made by the first expand_expr
4041 before reaching the other places where the save_expr was evaluated.
4042 You, the caller of save_expr, must make sure this is so.
4043
4044 Constants, and certain read-only nodes, are returned with no
4045 SAVE_EXPR because that is safe. Expressions containing placeholders
4046 are not touched; see tree.def for an explanation of what these
4047 are used for. */
4048
4049 tree
4050 save_expr (tree expr)
4051 {
4052 tree inner;
4053
4054 /* If the tree evaluates to a constant, then we don't want to hide that
4055 fact (i.e. this allows further folding, and direct checks for constants).
4056 However, a read-only object that has side effects cannot be bypassed.
4057 Since it is no problem to reevaluate literals, we just return the
4058 literal node. */
4059 inner = skip_simple_arithmetic (expr);
4060 if (TREE_CODE (inner) == ERROR_MARK)
4061 return inner;
4062
4063 if (tree_invariant_p_1 (inner))
4064 return expr;
4065
4066 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
4067 it means that the size or offset of some field of an object depends on
4068 the value within another field.
4069
4070 Note that it must not be the case that EXPR contains both a PLACEHOLDER_EXPR
4071 and some variable since it would then need to be both evaluated once and
4072 evaluated more than once. Front-ends must assure this case cannot
4073 happen by surrounding any such subexpressions in their own SAVE_EXPR
4074 and forcing evaluation at the proper time. */
4075 if (contains_placeholder_p (inner))
4076 return expr;
4077
4078 expr = build1_loc (EXPR_LOCATION (expr), SAVE_EXPR, TREE_TYPE (expr), expr);
4079
4080 /* This expression might be placed ahead of a jump to ensure that the
4081 value was computed on both sides of the jump. So make sure it isn't
4082 eliminated as dead. */
4083 TREE_SIDE_EFFECTS (expr) = 1;
4084 return expr;
4085 }
4086
4087 /* Look inside EXPR into any simple arithmetic operations. Return the
4088 outermost non-arithmetic or non-invariant node. */
4089
4090 tree
4091 skip_simple_arithmetic (tree expr)
4092 {
4093 /* We don't care about whether this can be used as an lvalue in this
4094 context. */
4095 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
4096 expr = TREE_OPERAND (expr, 0);
4097
4098 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
4099 a constant, it will be more efficient to not make another SAVE_EXPR since
4100 it will allow better simplification and GCSE will be able to merge the
4101 computations if they actually occur. */
4102 while (true)
4103 {
4104 if (UNARY_CLASS_P (expr))
4105 expr = TREE_OPERAND (expr, 0);
4106 else if (BINARY_CLASS_P (expr))
4107 {
4108 /* Before commutative binary operands are canonicalized,
4109 it is quite common to have constants in the first operand.
4110 Check for that common case first so that we don't walk
4111 large expressions with tree_invariant_p unnecessarily.
4112 This can still have terrible compile time complexity,
4113 we should limit the depth of the tree_invariant_p and
4114 skip_simple_arithmetic recursion. */
4115 if ((TREE_CONSTANT (TREE_OPERAND (expr, 0))
4116 || (TREE_READONLY (TREE_OPERAND (expr, 0))
4117 && !TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0))))
4118 && tree_invariant_p (TREE_OPERAND (expr, 0)))
4119 expr = TREE_OPERAND (expr, 1);
4120 else if (tree_invariant_p (TREE_OPERAND (expr, 1)))
4121 expr = TREE_OPERAND (expr, 0);
4122 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
4123 expr = TREE_OPERAND (expr, 1);
4124 else
4125 break;
4126 }
4127 else
4128 break;
4129 }
4130
4131 return expr;
4132 }
4133
4134 /* Look inside EXPR into simple arithmetic operations involving constants.
4135 Return the outermost non-arithmetic or non-constant node. */
4136
4137 tree
4138 skip_simple_constant_arithmetic (tree expr)
4139 {
4140 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
4141 expr = TREE_OPERAND (expr, 0);
4142
4143 while (true)
4144 {
4145 if (UNARY_CLASS_P (expr))
4146 expr = TREE_OPERAND (expr, 0);
4147 else if (BINARY_CLASS_P (expr))
4148 {
4149 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
4150 expr = TREE_OPERAND (expr, 0);
4151 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
4152 expr = TREE_OPERAND (expr, 1);
4153 else
4154 break;
4155 }
4156 else
4157 break;
4158 }
4159
4160 return expr;
4161 }
4162
4163 /* Return which tree structure is used by T. */
4164
4165 enum tree_node_structure_enum
4166 tree_node_structure (const_tree t)
4167 {
4168 const enum tree_code code = TREE_CODE (t);
4169 return tree_node_structure_for_code (code);
4170 }
4171
4172 /* Set various status flags when building a CALL_EXPR object T. */
4173
4174 static void
4175 process_call_operands (tree t)
4176 {
4177 bool side_effects = TREE_SIDE_EFFECTS (t);
4178 bool read_only = false;
4179 int i = call_expr_flags (t);
4180
4181 /* Calls have side-effects, except those to const or pure functions. */
4182 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
4183 side_effects = true;
4184 /* Propagate TREE_READONLY of arguments for const functions. */
4185 if (i & ECF_CONST)
4186 read_only = true;
4187
4188 if (!side_effects || read_only)
4189 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
4190 {
4191 tree op = TREE_OPERAND (t, i);
4192 if (op && TREE_SIDE_EFFECTS (op))
4193 side_effects = true;
4194 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
4195 read_only = false;
4196 }
4197
4198 TREE_SIDE_EFFECTS (t) = side_effects;
4199 TREE_READONLY (t) = read_only;
4200 }
4201
4202 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
4203 size or offset that depends on a field within a record. */
4204
4205 bool
4206 contains_placeholder_p (const_tree exp)
4207 {
4208 enum tree_code code;
4209
4210 if (!exp)
4211 return false;
4212
4213 code = TREE_CODE (exp);
4214 if (code == PLACEHOLDER_EXPR)
4215 return true;
4216
4217 switch (TREE_CODE_CLASS (code))
4218 {
4219 case tcc_reference:
4220 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
4221 position computations since they will be converted into a
4222 WITH_RECORD_EXPR involving the reference, which will assume
4223 here will be valid. */
4224 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
4225
4226 case tcc_exceptional:
4227 if (code == TREE_LIST)
4228 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
4229 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
4230 break;
4231
4232 case tcc_unary:
4233 case tcc_binary:
4234 case tcc_comparison:
4235 case tcc_expression:
4236 switch (code)
4237 {
4238 case COMPOUND_EXPR:
4239 /* Ignoring the first operand isn't quite right, but works best. */
4240 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
4241
4242 case COND_EXPR:
4243 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
4244 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
4245 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
4246
4247 case SAVE_EXPR:
4248 /* The save_expr function never wraps anything containing
4249 a PLACEHOLDER_EXPR. */
4250 return false;
4251
4252 default:
4253 break;
4254 }
4255
4256 switch (TREE_CODE_LENGTH (code))
4257 {
4258 case 1:
4259 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
4260 case 2:
4261 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
4262 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
4263 default:
4264 return false;
4265 }
4266
4267 case tcc_vl_exp:
4268 switch (code)
4269 {
4270 case CALL_EXPR:
4271 {
4272 const_tree arg;
4273 const_call_expr_arg_iterator iter;
4274 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
4275 if (CONTAINS_PLACEHOLDER_P (arg))
4276 return true;
4277 return false;
4278 }
4279 default:
4280 return false;
4281 }
4282
4283 default:
4284 return false;
4285 }
4286 return false;
4287 }
4288
4289 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
4290 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
4291 field positions. */
4292
4293 static bool
4294 type_contains_placeholder_1 (const_tree type)
4295 {
4296 /* If the size contains a placeholder or the parent type (component type in
4297 the case of arrays) type involves a placeholder, this type does. */
4298 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
4299 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
4300 || (!POINTER_TYPE_P (type)
4301 && TREE_TYPE (type)
4302 && type_contains_placeholder_p (TREE_TYPE (type))))
4303 return true;
4304
4305 /* Now do type-specific checks. Note that the last part of the check above
4306 greatly limits what we have to do below. */
4307 switch (TREE_CODE (type))
4308 {
4309 case VOID_TYPE:
4310 case OPAQUE_TYPE:
4311 case COMPLEX_TYPE:
4312 case ENUMERAL_TYPE:
4313 case BOOLEAN_TYPE:
4314 case POINTER_TYPE:
4315 case OFFSET_TYPE:
4316 case REFERENCE_TYPE:
4317 case METHOD_TYPE:
4318 case FUNCTION_TYPE:
4319 case VECTOR_TYPE:
4320 case NULLPTR_TYPE:
4321 return false;
4322
4323 case INTEGER_TYPE:
4324 case BITINT_TYPE:
4325 case REAL_TYPE:
4326 case FIXED_POINT_TYPE:
4327 /* Here we just check the bounds. */
4328 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
4329 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
4330
4331 case ARRAY_TYPE:
4332 /* We have already checked the component type above, so just check
4333 the domain type. Flexible array members have a null domain. */
4334 return TYPE_DOMAIN (type) ?
4335 type_contains_placeholder_p (TYPE_DOMAIN (type)) : false;
4336
4337 case RECORD_TYPE:
4338 case UNION_TYPE:
4339 case QUAL_UNION_TYPE:
4340 {
4341 tree field;
4342
4343 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4344 if (TREE_CODE (field) == FIELD_DECL
4345 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
4346 || (TREE_CODE (type) == QUAL_UNION_TYPE
4347 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
4348 || type_contains_placeholder_p (TREE_TYPE (field))))
4349 return true;
4350
4351 return false;
4352 }
4353
4354 default:
4355 gcc_unreachable ();
4356 }
4357 }
4358
4359 /* Wrapper around above function used to cache its result. */
4360
4361 bool
4362 type_contains_placeholder_p (tree type)
4363 {
4364 bool result;
4365
4366 /* If the contains_placeholder_bits field has been initialized,
4367 then we know the answer. */
4368 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
4369 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
4370
4371 /* Indicate that we've seen this type node, and the answer is false.
4372 This is what we want to return if we run into recursion via fields. */
4373 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
4374
4375 /* Compute the real value. */
4376 result = type_contains_placeholder_1 (type);
4377
4378 /* Store the real value. */
4379 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
4380
4381 return result;
4382 }
4383
4384 /* Push tree EXP onto vector QUEUE if it is not already present. */
4385
4386 static void
4387 push_without_duplicates (tree exp, vec<tree> *queue)
4388 {
4389 unsigned int i;
4390 tree iter;
4391
4392 FOR_EACH_VEC_ELT (*queue, i, iter)
4393 if (simple_cst_equal (iter, exp) == 1)
4394 break;
4395
4396 if (!iter)
4397 queue->safe_push (exp);
4398 }
4399
4400 /* Given a tree EXP, find all occurrences of references to fields
4401 in a PLACEHOLDER_EXPR and place them in vector REFS without
4402 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
4403 we assume here that EXP contains only arithmetic expressions
4404 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
4405 argument list. */
4406
4407 void
4408 find_placeholder_in_expr (tree exp, vec<tree> *refs)
4409 {
4410 enum tree_code code = TREE_CODE (exp);
4411 tree inner;
4412 int i;
4413
4414 /* We handle TREE_LIST and COMPONENT_REF separately. */
4415 if (code == TREE_LIST)
4416 {
4417 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
4418 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
4419 }
4420 else if (code == COMPONENT_REF)
4421 {
4422 for (inner = TREE_OPERAND (exp, 0);
4423 REFERENCE_CLASS_P (inner);
4424 inner = TREE_OPERAND (inner, 0))
4425 ;
4426
4427 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
4428 push_without_duplicates (exp, refs);
4429 else
4430 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
4431 }
4432 else
4433 switch (TREE_CODE_CLASS (code))
4434 {
4435 case tcc_constant:
4436 break;
4437
4438 case tcc_declaration:
4439 /* Variables allocated to static storage can stay. */
4440 if (!TREE_STATIC (exp))
4441 push_without_duplicates (exp, refs);
4442 break;
4443
4444 case tcc_expression:
4445 /* This is the pattern built in ada/make_aligning_type. */
4446 if (code == ADDR_EXPR
4447 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
4448 {
4449 push_without_duplicates (exp, refs);
4450 break;
4451 }
4452
4453 /* Fall through. */
4454
4455 case tcc_exceptional:
4456 case tcc_unary:
4457 case tcc_binary:
4458 case tcc_comparison:
4459 case tcc_reference:
4460 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
4461 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
4462 break;
4463
4464 case tcc_vl_exp:
4465 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4466 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
4467 break;
4468
4469 default:
4470 gcc_unreachable ();
4471 }
4472 }
4473
4474 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
4475 return a tree with all occurrences of references to F in a
4476 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
4477 CONST_DECLs. Note that we assume here that EXP contains only
4478 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
4479 occurring only in their argument list. */
4480
4481 tree
4482 substitute_in_expr (tree exp, tree f, tree r)
4483 {
4484 enum tree_code code = TREE_CODE (exp);
4485 tree op0, op1, op2, op3;
4486 tree new_tree;
4487
4488 /* We handle TREE_LIST and COMPONENT_REF separately. */
4489 if (code == TREE_LIST)
4490 {
4491 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
4492 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
4493 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
4494 return exp;
4495
4496 return tree_cons (TREE_PURPOSE (exp), op1, op0);
4497 }
4498 else if (code == COMPONENT_REF)
4499 {
4500 tree inner;
4501
4502 /* If this expression is getting a value from a PLACEHOLDER_EXPR
4503 and it is the right field, replace it with R. */
4504 for (inner = TREE_OPERAND (exp, 0);
4505 REFERENCE_CLASS_P (inner);
4506 inner = TREE_OPERAND (inner, 0))
4507 ;
4508
4509 /* The field. */
4510 op1 = TREE_OPERAND (exp, 1);
4511
4512 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
4513 return r;
4514
4515 /* If this expression hasn't been completed let, leave it alone. */
4516 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
4517 return exp;
4518
4519 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4520 if (op0 == TREE_OPERAND (exp, 0))
4521 return exp;
4522
4523 new_tree
4524 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
4525 }
4526 else
4527 switch (TREE_CODE_CLASS (code))
4528 {
4529 case tcc_constant:
4530 return exp;
4531
4532 case tcc_declaration:
4533 if (exp == f)
4534 return r;
4535 else
4536 return exp;
4537
4538 case tcc_expression:
4539 if (exp == f)
4540 return r;
4541
4542 /* Fall through. */
4543
4544 case tcc_exceptional:
4545 case tcc_unary:
4546 case tcc_binary:
4547 case tcc_comparison:
4548 case tcc_reference:
4549 switch (TREE_CODE_LENGTH (code))
4550 {
4551 case 0:
4552 return exp;
4553
4554 case 1:
4555 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4556 if (op0 == TREE_OPERAND (exp, 0))
4557 return exp;
4558
4559 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
4560 break;
4561
4562 case 2:
4563 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4564 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4565
4566 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
4567 return exp;
4568
4569 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
4570 break;
4571
4572 case 3:
4573 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4574 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4575 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
4576
4577 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4578 && op2 == TREE_OPERAND (exp, 2))
4579 return exp;
4580
4581 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
4582 break;
4583
4584 case 4:
4585 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4586 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4587 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
4588 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
4589
4590 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4591 && op2 == TREE_OPERAND (exp, 2)
4592 && op3 == TREE_OPERAND (exp, 3))
4593 return exp;
4594
4595 new_tree
4596 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
4597 break;
4598
4599 default:
4600 gcc_unreachable ();
4601 }
4602 break;
4603
4604 case tcc_vl_exp:
4605 {
4606 int i;
4607
4608 new_tree = NULL_TREE;
4609
4610 /* If we are trying to replace F with a constant or with another
4611 instance of one of the arguments of the call, inline back
4612 functions which do nothing else than computing a value from
4613 the arguments they are passed. This makes it possible to
4614 fold partially or entirely the replacement expression. */
4615 if (code == CALL_EXPR)
4616 {
4617 bool maybe_inline = false;
4618 if (CONSTANT_CLASS_P (r))
4619 maybe_inline = true;
4620 else
4621 for (i = 3; i < TREE_OPERAND_LENGTH (exp); i++)
4622 if (operand_equal_p (TREE_OPERAND (exp, i), r, 0))
4623 {
4624 maybe_inline = true;
4625 break;
4626 }
4627 if (maybe_inline)
4628 {
4629 tree t = maybe_inline_call_in_expr (exp);
4630 if (t)
4631 return SUBSTITUTE_IN_EXPR (t, f, r);
4632 }
4633 }
4634
4635 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4636 {
4637 tree op = TREE_OPERAND (exp, i);
4638 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
4639 if (new_op != op)
4640 {
4641 if (!new_tree)
4642 new_tree = copy_node (exp);
4643 TREE_OPERAND (new_tree, i) = new_op;
4644 }
4645 }
4646
4647 if (new_tree)
4648 {
4649 new_tree = fold (new_tree);
4650 if (TREE_CODE (new_tree) == CALL_EXPR)
4651 process_call_operands (new_tree);
4652 }
4653 else
4654 return exp;
4655 }
4656 break;
4657
4658 default:
4659 gcc_unreachable ();
4660 }
4661
4662 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4663
4664 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4665 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4666
4667 return new_tree;
4668 }
4669
4670 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
4671 for it within OBJ, a tree that is an object or a chain of references. */
4672
4673 tree
4674 substitute_placeholder_in_expr (tree exp, tree obj)
4675 {
4676 enum tree_code code = TREE_CODE (exp);
4677 tree op0, op1, op2, op3;
4678 tree new_tree;
4679
4680 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
4681 in the chain of OBJ. */
4682 if (code == PLACEHOLDER_EXPR)
4683 {
4684 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
4685 tree elt;
4686
4687 for (elt = obj; elt != 0;
4688 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
4689 || TREE_CODE (elt) == COND_EXPR)
4690 ? TREE_OPERAND (elt, 1)
4691 : (REFERENCE_CLASS_P (elt)
4692 || UNARY_CLASS_P (elt)
4693 || BINARY_CLASS_P (elt)
4694 || VL_EXP_CLASS_P (elt)
4695 || EXPRESSION_CLASS_P (elt))
4696 ? TREE_OPERAND (elt, 0) : 0))
4697 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
4698 return elt;
4699
4700 for (elt = obj; elt != 0;
4701 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
4702 || TREE_CODE (elt) == COND_EXPR)
4703 ? TREE_OPERAND (elt, 1)
4704 : (REFERENCE_CLASS_P (elt)
4705 || UNARY_CLASS_P (elt)
4706 || BINARY_CLASS_P (elt)
4707 || VL_EXP_CLASS_P (elt)
4708 || EXPRESSION_CLASS_P (elt))
4709 ? TREE_OPERAND (elt, 0) : 0))
4710 if (POINTER_TYPE_P (TREE_TYPE (elt))
4711 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
4712 == need_type))
4713 return fold_build1 (INDIRECT_REF, need_type, elt);
4714
4715 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
4716 survives until RTL generation, there will be an error. */
4717 return exp;
4718 }
4719
4720 /* TREE_LIST is special because we need to look at TREE_VALUE
4721 and TREE_CHAIN, not TREE_OPERANDS. */
4722 else if (code == TREE_LIST)
4723 {
4724 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
4725 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
4726 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
4727 return exp;
4728
4729 return tree_cons (TREE_PURPOSE (exp), op1, op0);
4730 }
4731 else
4732 switch (TREE_CODE_CLASS (code))
4733 {
4734 case tcc_constant:
4735 case tcc_declaration:
4736 return exp;
4737
4738 case tcc_exceptional:
4739 case tcc_unary:
4740 case tcc_binary:
4741 case tcc_comparison:
4742 case tcc_expression:
4743 case tcc_reference:
4744 case tcc_statement:
4745 switch (TREE_CODE_LENGTH (code))
4746 {
4747 case 0:
4748 return exp;
4749
4750 case 1:
4751 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4752 if (op0 == TREE_OPERAND (exp, 0))
4753 return exp;
4754
4755 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
4756 break;
4757
4758 case 2:
4759 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4760 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4761
4762 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
4763 return exp;
4764
4765 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
4766 break;
4767
4768 case 3:
4769 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4770 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4771 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
4772
4773 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4774 && op2 == TREE_OPERAND (exp, 2))
4775 return exp;
4776
4777 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
4778 break;
4779
4780 case 4:
4781 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4782 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4783 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
4784 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
4785
4786 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4787 && op2 == TREE_OPERAND (exp, 2)
4788 && op3 == TREE_OPERAND (exp, 3))
4789 return exp;
4790
4791 new_tree
4792 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
4793 break;
4794
4795 default:
4796 gcc_unreachable ();
4797 }
4798 break;
4799
4800 case tcc_vl_exp:
4801 {
4802 int i;
4803
4804 new_tree = NULL_TREE;
4805
4806 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4807 {
4808 tree op = TREE_OPERAND (exp, i);
4809 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
4810 if (new_op != op)
4811 {
4812 if (!new_tree)
4813 new_tree = copy_node (exp);
4814 TREE_OPERAND (new_tree, i) = new_op;
4815 }
4816 }
4817
4818 if (new_tree)
4819 {
4820 new_tree = fold (new_tree);
4821 if (TREE_CODE (new_tree) == CALL_EXPR)
4822 process_call_operands (new_tree);
4823 }
4824 else
4825 return exp;
4826 }
4827 break;
4828
4829 default:
4830 gcc_unreachable ();
4831 }
4832
4833 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4834
4835 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4836 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4837
4838 return new_tree;
4839 }
4840
4841
4842 /* Subroutine of stabilize_reference; this is called for subtrees of
4843 references. Any expression with side-effects must be put in a SAVE_EXPR
4844 to ensure that it is only evaluated once.
4845
4846 We don't put SAVE_EXPR nodes around everything, because assigning very
4847 simple expressions to temporaries causes us to miss good opportunities
4848 for optimizations. Among other things, the opportunity to fold in the
4849 addition of a constant into an addressing mode often gets lost, e.g.
4850 "y[i+1] += x;". In general, we take the approach that we should not make
4851 an assignment unless we are forced into it - i.e., that any non-side effect
4852 operator should be allowed, and that cse should take care of coalescing
4853 multiple utterances of the same expression should that prove fruitful. */
4854
4855 static tree
4856 stabilize_reference_1 (tree e)
4857 {
4858 tree result;
4859 enum tree_code code = TREE_CODE (e);
4860
4861 /* We cannot ignore const expressions because it might be a reference
4862 to a const array but whose index contains side-effects. But we can
4863 ignore things that are actual constant or that already have been
4864 handled by this function. */
4865
4866 if (tree_invariant_p (e))
4867 return e;
4868
4869 switch (TREE_CODE_CLASS (code))
4870 {
4871 case tcc_exceptional:
4872 /* Always wrap STATEMENT_LIST into SAVE_EXPR, even if it doesn't
4873 have side-effects. */
4874 if (code == STATEMENT_LIST)
4875 return save_expr (e);
4876 /* FALLTHRU */
4877 case tcc_type:
4878 case tcc_declaration:
4879 case tcc_comparison:
4880 case tcc_statement:
4881 case tcc_expression:
4882 case tcc_reference:
4883 case tcc_vl_exp:
4884 /* If the expression has side-effects, then encase it in a SAVE_EXPR
4885 so that it will only be evaluated once. */
4886 /* The reference (r) and comparison (<) classes could be handled as
4887 below, but it is generally faster to only evaluate them once. */
4888 if (TREE_SIDE_EFFECTS (e))
4889 return save_expr (e);
4890 return e;
4891
4892 case tcc_constant:
4893 /* Constants need no processing. In fact, we should never reach
4894 here. */
4895 return e;
4896
4897 case tcc_binary:
4898 /* Division is slow and tends to be compiled with jumps,
4899 especially the division by powers of 2 that is often
4900 found inside of an array reference. So do it just once. */
4901 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
4902 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
4903 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
4904 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
4905 return save_expr (e);
4906 /* Recursively stabilize each operand. */
4907 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
4908 stabilize_reference_1 (TREE_OPERAND (e, 1)));
4909 break;
4910
4911 case tcc_unary:
4912 /* Recursively stabilize each operand. */
4913 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
4914 break;
4915
4916 default:
4917 gcc_unreachable ();
4918 }
4919
4920 TREE_TYPE (result) = TREE_TYPE (e);
4921 TREE_READONLY (result) = TREE_READONLY (e);
4922 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
4923 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
4924
4925 return result;
4926 }
4927
4928 /* Stabilize a reference so that we can use it any number of times
4929 without causing its operands to be evaluated more than once.
4930 Returns the stabilized reference. This works by means of save_expr,
4931 so see the caveats in the comments about save_expr.
4932
4933 Also allows conversion expressions whose operands are references.
4934 Any other kind of expression is returned unchanged. */
4935
4936 tree
4937 stabilize_reference (tree ref)
4938 {
4939 tree result;
4940 enum tree_code code = TREE_CODE (ref);
4941
4942 switch (code)
4943 {
4944 case VAR_DECL:
4945 case PARM_DECL:
4946 case RESULT_DECL:
4947 /* No action is needed in this case. */
4948 return ref;
4949
4950 CASE_CONVERT:
4951 case FLOAT_EXPR:
4952 case FIX_TRUNC_EXPR:
4953 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
4954 break;
4955
4956 case INDIRECT_REF:
4957 result = build_nt (INDIRECT_REF,
4958 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
4959 break;
4960
4961 case COMPONENT_REF:
4962 result = build_nt (COMPONENT_REF,
4963 stabilize_reference (TREE_OPERAND (ref, 0)),
4964 TREE_OPERAND (ref, 1), NULL_TREE);
4965 break;
4966
4967 case BIT_FIELD_REF:
4968 result = build_nt (BIT_FIELD_REF,
4969 stabilize_reference (TREE_OPERAND (ref, 0)),
4970 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
4971 REF_REVERSE_STORAGE_ORDER (result) = REF_REVERSE_STORAGE_ORDER (ref);
4972 break;
4973
4974 case ARRAY_REF:
4975 result = build_nt (ARRAY_REF,
4976 stabilize_reference (TREE_OPERAND (ref, 0)),
4977 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4978 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4979 break;
4980
4981 case ARRAY_RANGE_REF:
4982 result = build_nt (ARRAY_RANGE_REF,
4983 stabilize_reference (TREE_OPERAND (ref, 0)),
4984 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4985 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4986 break;
4987
4988 case COMPOUND_EXPR:
4989 /* We cannot wrap the first expression in a SAVE_EXPR, as then
4990 it wouldn't be ignored. This matters when dealing with
4991 volatiles. */
4992 return stabilize_reference_1 (ref);
4993
4994 /* If arg isn't a kind of lvalue we recognize, make no change.
4995 Caller should recognize the error for an invalid lvalue. */
4996 default:
4997 return ref;
4998
4999 case ERROR_MARK:
5000 return error_mark_node;
5001 }
5002
5003 TREE_TYPE (result) = TREE_TYPE (ref);
5004 TREE_READONLY (result) = TREE_READONLY (ref);
5005 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
5006 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
5007 protected_set_expr_location (result, EXPR_LOCATION (ref));
5008
5009 return result;
5010 }
5011
5012 /* Low-level constructors for expressions. */
5013
5014 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
5015 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
5016
5017 void
5018 recompute_tree_invariant_for_addr_expr (tree t)
5019 {
5020 tree node;
5021 bool tc = true, se = false;
5022
5023 gcc_assert (TREE_CODE (t) == ADDR_EXPR);
5024
5025 /* We started out assuming this address is both invariant and constant, but
5026 does not have side effects. Now go down any handled components and see if
5027 any of them involve offsets that are either non-constant or non-invariant.
5028 Also check for side-effects.
5029
5030 ??? Note that this code makes no attempt to deal with the case where
5031 taking the address of something causes a copy due to misalignment. */
5032
5033 #define UPDATE_FLAGS(NODE) \
5034 do { tree _node = (NODE); \
5035 if (_node && !TREE_CONSTANT (_node)) tc = false; \
5036 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
5037
5038 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
5039 node = TREE_OPERAND (node, 0))
5040 {
5041 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
5042 array reference (probably made temporarily by the G++ front end),
5043 so ignore all the operands. */
5044 if ((TREE_CODE (node) == ARRAY_REF
5045 || TREE_CODE (node) == ARRAY_RANGE_REF)
5046 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
5047 {
5048 UPDATE_FLAGS (TREE_OPERAND (node, 1));
5049 if (TREE_OPERAND (node, 2))
5050 UPDATE_FLAGS (TREE_OPERAND (node, 2));
5051 if (TREE_OPERAND (node, 3))
5052 UPDATE_FLAGS (TREE_OPERAND (node, 3));
5053 }
5054 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
5055 FIELD_DECL, apparently. The G++ front end can put something else
5056 there, at least temporarily. */
5057 else if (TREE_CODE (node) == COMPONENT_REF
5058 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
5059 {
5060 if (TREE_OPERAND (node, 2))
5061 UPDATE_FLAGS (TREE_OPERAND (node, 2));
5062 }
5063 }
5064
5065 node = lang_hooks.expr_to_decl (node, &tc, &se);
5066
5067 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
5068 the address, since &(*a)->b is a form of addition. If it's a constant, the
5069 address is constant too. If it's a decl, its address is constant if the
5070 decl is static. Everything else is not constant and, furthermore,
5071 taking the address of a volatile variable is not volatile. */
5072 if (INDIRECT_REF_P (node)
5073 || TREE_CODE (node) == MEM_REF)
5074 UPDATE_FLAGS (TREE_OPERAND (node, 0));
5075 else if (CONSTANT_CLASS_P (node))
5076 ;
5077 else if (DECL_P (node))
5078 tc &= (staticp (node) != NULL_TREE);
5079 else
5080 {
5081 tc = false;
5082 se |= TREE_SIDE_EFFECTS (node);
5083 }
5084
5085
5086 TREE_CONSTANT (t) = tc;
5087 TREE_SIDE_EFFECTS (t) = se;
5088 #undef UPDATE_FLAGS
5089 }
5090
5091 /* Build an expression of code CODE, data type TYPE, and operands as
5092 specified. Expressions and reference nodes can be created this way.
5093 Constants, decls, types and misc nodes cannot be.
5094
5095 We define 5 non-variadic functions, from 0 to 4 arguments. This is
5096 enough for all extant tree codes. */
5097
5098 tree
5099 build0 (enum tree_code code, tree tt MEM_STAT_DECL)
5100 {
5101 tree t;
5102
5103 gcc_assert (TREE_CODE_LENGTH (code) == 0);
5104
5105 t = make_node (code PASS_MEM_STAT);
5106 TREE_TYPE (t) = tt;
5107
5108 return t;
5109 }
5110
5111 tree
5112 build1 (enum tree_code code, tree type, tree node MEM_STAT_DECL)
5113 {
5114 int length = sizeof (struct tree_exp);
5115 tree t;
5116
5117 record_node_allocation_statistics (code, length);
5118
5119 gcc_assert (TREE_CODE_LENGTH (code) == 1);
5120
5121 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
5122
5123 memset (t, 0, sizeof (struct tree_common));
5124
5125 TREE_SET_CODE (t, code);
5126
5127 TREE_TYPE (t) = type;
5128 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
5129 TREE_OPERAND (t, 0) = node;
5130 if (node && !TYPE_P (node))
5131 {
5132 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
5133 TREE_READONLY (t) = TREE_READONLY (node);
5134 }
5135
5136 if (TREE_CODE_CLASS (code) == tcc_statement)
5137 {
5138 if (code != DEBUG_BEGIN_STMT)
5139 TREE_SIDE_EFFECTS (t) = 1;
5140 }
5141 else switch (code)
5142 {
5143 case VA_ARG_EXPR:
5144 /* All of these have side-effects, no matter what their
5145 operands are. */
5146 TREE_SIDE_EFFECTS (t) = 1;
5147 TREE_READONLY (t) = 0;
5148 break;
5149
5150 case INDIRECT_REF:
5151 /* Whether a dereference is readonly has nothing to do with whether
5152 its operand is readonly. */
5153 TREE_READONLY (t) = 0;
5154 break;
5155
5156 case ADDR_EXPR:
5157 if (node)
5158 recompute_tree_invariant_for_addr_expr (t);
5159 break;
5160
5161 default:
5162 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
5163 && node && !TYPE_P (node)
5164 && TREE_CONSTANT (node))
5165 TREE_CONSTANT (t) = 1;
5166 if (TREE_CODE_CLASS (code) == tcc_reference
5167 && node && TREE_THIS_VOLATILE (node))
5168 TREE_THIS_VOLATILE (t) = 1;
5169 break;
5170 }
5171
5172 return t;
5173 }
5174
5175 #define PROCESS_ARG(N) \
5176 do { \
5177 TREE_OPERAND (t, N) = arg##N; \
5178 if (arg##N &&!TYPE_P (arg##N)) \
5179 { \
5180 if (TREE_SIDE_EFFECTS (arg##N)) \
5181 side_effects = 1; \
5182 if (!TREE_READONLY (arg##N) \
5183 && !CONSTANT_CLASS_P (arg##N)) \
5184 (void) (read_only = 0); \
5185 if (!TREE_CONSTANT (arg##N)) \
5186 (void) (constant = 0); \
5187 } \
5188 } while (0)
5189
5190 tree
5191 build2 (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
5192 {
5193 bool constant, read_only, side_effects, div_by_zero;
5194 tree t;
5195
5196 gcc_assert (TREE_CODE_LENGTH (code) == 2);
5197
5198 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
5199 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
5200 /* When sizetype precision doesn't match that of pointers
5201 we need to be able to build explicit extensions or truncations
5202 of the offset argument. */
5203 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
5204 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
5205 && TREE_CODE (arg1) == INTEGER_CST);
5206
5207 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
5208 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
5209 && ptrofftype_p (TREE_TYPE (arg1)));
5210
5211 t = make_node (code PASS_MEM_STAT);
5212 TREE_TYPE (t) = tt;
5213
5214 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
5215 result based on those same flags for the arguments. But if the
5216 arguments aren't really even `tree' expressions, we shouldn't be trying
5217 to do this. */
5218
5219 /* Expressions without side effects may be constant if their
5220 arguments are as well. */
5221 constant = (TREE_CODE_CLASS (code) == tcc_comparison
5222 || TREE_CODE_CLASS (code) == tcc_binary);
5223 read_only = 1;
5224 side_effects = TREE_SIDE_EFFECTS (t);
5225
5226 switch (code)
5227 {
5228 case TRUNC_DIV_EXPR:
5229 case CEIL_DIV_EXPR:
5230 case FLOOR_DIV_EXPR:
5231 case ROUND_DIV_EXPR:
5232 case EXACT_DIV_EXPR:
5233 case CEIL_MOD_EXPR:
5234 case FLOOR_MOD_EXPR:
5235 case ROUND_MOD_EXPR:
5236 case TRUNC_MOD_EXPR:
5237 div_by_zero = integer_zerop (arg1);
5238 break;
5239 default:
5240 div_by_zero = false;
5241 }
5242
5243 PROCESS_ARG (0);
5244 PROCESS_ARG (1);
5245
5246 TREE_SIDE_EFFECTS (t) = side_effects;
5247 if (code == MEM_REF)
5248 {
5249 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
5250 {
5251 tree o = TREE_OPERAND (arg0, 0);
5252 TREE_READONLY (t) = TREE_READONLY (o);
5253 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
5254 }
5255 }
5256 else
5257 {
5258 TREE_READONLY (t) = read_only;
5259 /* Don't mark X / 0 as constant. */
5260 TREE_CONSTANT (t) = constant && !div_by_zero;
5261 TREE_THIS_VOLATILE (t)
5262 = (TREE_CODE_CLASS (code) == tcc_reference
5263 && arg0 && TREE_THIS_VOLATILE (arg0));
5264 }
5265
5266 return t;
5267 }
5268
5269
5270 tree
5271 build3 (enum tree_code code, tree tt, tree arg0, tree arg1,
5272 tree arg2 MEM_STAT_DECL)
5273 {
5274 bool constant, read_only, side_effects;
5275 tree t;
5276
5277 gcc_assert (TREE_CODE_LENGTH (code) == 3);
5278 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
5279
5280 t = make_node (code PASS_MEM_STAT);
5281 TREE_TYPE (t) = tt;
5282
5283 read_only = 1;
5284
5285 /* As a special exception, if COND_EXPR has NULL branches, we
5286 assume that it is a gimple statement and always consider
5287 it to have side effects. */
5288 if (code == COND_EXPR
5289 && tt == void_type_node
5290 && arg1 == NULL_TREE
5291 && arg2 == NULL_TREE)
5292 side_effects = true;
5293 else
5294 side_effects = TREE_SIDE_EFFECTS (t);
5295
5296 PROCESS_ARG (0);
5297 PROCESS_ARG (1);
5298 PROCESS_ARG (2);
5299
5300 if (code == COND_EXPR)
5301 TREE_READONLY (t) = read_only;
5302
5303 TREE_SIDE_EFFECTS (t) = side_effects;
5304 TREE_THIS_VOLATILE (t)
5305 = (TREE_CODE_CLASS (code) == tcc_reference
5306 && arg0 && TREE_THIS_VOLATILE (arg0));
5307
5308 return t;
5309 }
5310
5311 tree
5312 build4 (enum tree_code code, tree tt, tree arg0, tree arg1,
5313 tree arg2, tree arg3 MEM_STAT_DECL)
5314 {
5315 bool constant, read_only, side_effects;
5316 tree t;
5317
5318 gcc_assert (TREE_CODE_LENGTH (code) == 4);
5319
5320 t = make_node (code PASS_MEM_STAT);
5321 TREE_TYPE (t) = tt;
5322
5323 side_effects = TREE_SIDE_EFFECTS (t);
5324
5325 PROCESS_ARG (0);
5326 PROCESS_ARG (1);
5327 PROCESS_ARG (2);
5328 PROCESS_ARG (3);
5329
5330 TREE_SIDE_EFFECTS (t) = side_effects;
5331 TREE_THIS_VOLATILE (t)
5332 = (TREE_CODE_CLASS (code) == tcc_reference
5333 && arg0 && TREE_THIS_VOLATILE (arg0));
5334
5335 return t;
5336 }
5337
5338 tree
5339 build5 (enum tree_code code, tree tt, tree arg0, tree arg1,
5340 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
5341 {
5342 bool constant, read_only, side_effects;
5343 tree t;
5344
5345 gcc_assert (TREE_CODE_LENGTH (code) == 5);
5346
5347 t = make_node (code PASS_MEM_STAT);
5348 TREE_TYPE (t) = tt;
5349
5350 side_effects = TREE_SIDE_EFFECTS (t);
5351
5352 PROCESS_ARG (0);
5353 PROCESS_ARG (1);
5354 PROCESS_ARG (2);
5355 PROCESS_ARG (3);
5356 PROCESS_ARG (4);
5357
5358 TREE_SIDE_EFFECTS (t) = side_effects;
5359 if (code == TARGET_MEM_REF)
5360 {
5361 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
5362 {
5363 tree o = TREE_OPERAND (arg0, 0);
5364 TREE_READONLY (t) = TREE_READONLY (o);
5365 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
5366 }
5367 }
5368 else
5369 TREE_THIS_VOLATILE (t)
5370 = (TREE_CODE_CLASS (code) == tcc_reference
5371 && arg0 && TREE_THIS_VOLATILE (arg0));
5372
5373 return t;
5374 }
5375
5376 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
5377 on the pointer PTR. */
5378
5379 tree
5380 build_simple_mem_ref_loc (location_t loc, tree ptr)
5381 {
5382 poly_int64 offset = 0;
5383 tree ptype = TREE_TYPE (ptr);
5384 tree tem;
5385 /* For convenience allow addresses that collapse to a simple base
5386 and offset. */
5387 if (TREE_CODE (ptr) == ADDR_EXPR
5388 && (handled_component_p (TREE_OPERAND (ptr, 0))
5389 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
5390 {
5391 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
5392 gcc_assert (ptr);
5393 if (TREE_CODE (ptr) == MEM_REF)
5394 {
5395 offset += mem_ref_offset (ptr).force_shwi ();
5396 ptr = TREE_OPERAND (ptr, 0);
5397 }
5398 else
5399 ptr = build_fold_addr_expr (ptr);
5400 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
5401 }
5402 tem = build2 (MEM_REF, TREE_TYPE (ptype),
5403 ptr, build_int_cst (ptype, offset));
5404 SET_EXPR_LOCATION (tem, loc);
5405 return tem;
5406 }
5407
5408 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
5409
5410 poly_offset_int
5411 mem_ref_offset (const_tree t)
5412 {
5413 return poly_offset_int::from (wi::to_poly_wide (TREE_OPERAND (t, 1)),
5414 SIGNED);
5415 }
5416
5417 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
5418 offsetted by OFFSET units. */
5419
5420 tree
5421 build_invariant_address (tree type, tree base, poly_int64 offset)
5422 {
5423 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
5424 build_fold_addr_expr (base),
5425 build_int_cst (ptr_type_node, offset));
5426 tree addr = build1 (ADDR_EXPR, type, ref);
5427 recompute_tree_invariant_for_addr_expr (addr);
5428 return addr;
5429 }
5430
5431 /* Similar except don't specify the TREE_TYPE
5432 and leave the TREE_SIDE_EFFECTS as 0.
5433 It is permissible for arguments to be null,
5434 or even garbage if their values do not matter. */
5435
5436 tree
5437 build_nt (enum tree_code code, ...)
5438 {
5439 tree t;
5440 int length;
5441 int i;
5442 va_list p;
5443
5444 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
5445
5446 va_start (p, code);
5447
5448 t = make_node (code);
5449 length = TREE_CODE_LENGTH (code);
5450
5451 for (i = 0; i < length; i++)
5452 TREE_OPERAND (t, i) = va_arg (p, tree);
5453
5454 va_end (p);
5455 return t;
5456 }
5457
5458 /* Similar to build_nt, but for creating a CALL_EXPR object with a
5459 tree vec. */
5460
5461 tree
5462 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
5463 {
5464 tree ret, t;
5465 unsigned int ix;
5466
5467 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
5468 CALL_EXPR_FN (ret) = fn;
5469 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
5470 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
5471 CALL_EXPR_ARG (ret, ix) = t;
5472 return ret;
5473 }
5474
5475 /* Create a DECL_... node of code CODE, name NAME (if non-null)
5476 and data type TYPE.
5477 We do NOT enter this node in any sort of symbol table.
5478
5479 LOC is the location of the decl.
5480
5481 layout_decl is used to set up the decl's storage layout.
5482 Other slots are initialized to 0 or null pointers. */
5483
5484 tree
5485 build_decl (location_t loc, enum tree_code code, tree name,
5486 tree type MEM_STAT_DECL)
5487 {
5488 tree t;
5489
5490 t = make_node (code PASS_MEM_STAT);
5491 DECL_SOURCE_LOCATION (t) = loc;
5492
5493 /* if (type == error_mark_node)
5494 type = integer_type_node; */
5495 /* That is not done, deliberately, so that having error_mark_node
5496 as the type can suppress useless errors in the use of this variable. */
5497
5498 DECL_NAME (t) = name;
5499 TREE_TYPE (t) = type;
5500
5501 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
5502 layout_decl (t, 0);
5503
5504 return t;
5505 }
5506
5507 /* Create and return a DEBUG_EXPR_DECL node of the given TYPE. */
5508
5509 tree
5510 build_debug_expr_decl (tree type)
5511 {
5512 tree vexpr = make_node (DEBUG_EXPR_DECL);
5513 DECL_ARTIFICIAL (vexpr) = 1;
5514 TREE_TYPE (vexpr) = type;
5515 SET_DECL_MODE (vexpr, TYPE_MODE (type));
5516 return vexpr;
5517 }
5518
5519 /* Builds and returns function declaration with NAME and TYPE. */
5520
5521 tree
5522 build_fn_decl (const char *name, tree type)
5523 {
5524 tree id = get_identifier (name);
5525 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
5526
5527 DECL_EXTERNAL (decl) = 1;
5528 TREE_PUBLIC (decl) = 1;
5529 DECL_ARTIFICIAL (decl) = 1;
5530 TREE_NOTHROW (decl) = 1;
5531
5532 return decl;
5533 }
5534
5535 vec<tree, va_gc> *all_translation_units;
5536
5537 /* Builds a new translation-unit decl with name NAME, queues it in the
5538 global list of translation-unit decls and returns it. */
5539
5540 tree
5541 build_translation_unit_decl (tree name)
5542 {
5543 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
5544 name, NULL_TREE);
5545 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
5546 vec_safe_push (all_translation_units, tu);
5547 return tu;
5548 }
5549
5550
5551 /* BLOCK nodes are used to represent the structure of binding contours
5552 and declarations, once those contours have been exited and their contents
5553 compiled. This information is used for outputting debugging info. */
5554
5555 tree
5556 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
5557 {
5558 tree block = make_node (BLOCK);
5559
5560 BLOCK_VARS (block) = vars;
5561 BLOCK_SUBBLOCKS (block) = subblocks;
5562 BLOCK_SUPERCONTEXT (block) = supercontext;
5563 BLOCK_CHAIN (block) = chain;
5564 return block;
5565 }
5566
5567
5568 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
5569
5570 LOC is the location to use in tree T. */
5571
5572 void
5573 protected_set_expr_location (tree t, location_t loc)
5574 {
5575 if (CAN_HAVE_LOCATION_P (t))
5576 SET_EXPR_LOCATION (t, loc);
5577 else if (t && TREE_CODE (t) == STATEMENT_LIST)
5578 {
5579 t = expr_single (t);
5580 if (t && CAN_HAVE_LOCATION_P (t))
5581 SET_EXPR_LOCATION (t, loc);
5582 }
5583 }
5584
5585 /* Like PROTECTED_SET_EXPR_LOCATION, but only do that if T has
5586 UNKNOWN_LOCATION. */
5587
5588 void
5589 protected_set_expr_location_if_unset (tree t, location_t loc)
5590 {
5591 t = expr_single (t);
5592 if (t && !EXPR_HAS_LOCATION (t))
5593 protected_set_expr_location (t, loc);
5594 }
5595
5596 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
5597 of the various TYPE_QUAL values. */
5598
5599 static void
5600 set_type_quals (tree type, int type_quals)
5601 {
5602 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
5603 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
5604 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
5605 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
5606 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
5607 }
5608
5609 /* Returns true iff CAND and BASE have equivalent language-specific
5610 qualifiers. */
5611
5612 bool
5613 check_lang_type (const_tree cand, const_tree base)
5614 {
5615 if (lang_hooks.types.type_hash_eq == NULL)
5616 return true;
5617 /* type_hash_eq currently only applies to these types. */
5618 if (TREE_CODE (cand) != FUNCTION_TYPE
5619 && TREE_CODE (cand) != METHOD_TYPE)
5620 return true;
5621 return lang_hooks.types.type_hash_eq (cand, base);
5622 }
5623
5624 /* This function checks to see if TYPE matches the size one of the built-in
5625 atomic types, and returns that core atomic type. */
5626
5627 static tree
5628 find_atomic_core_type (const_tree type)
5629 {
5630 tree base_atomic_type;
5631
5632 /* Only handle complete types. */
5633 if (!tree_fits_uhwi_p (TYPE_SIZE (type)))
5634 return NULL_TREE;
5635
5636 switch (tree_to_uhwi (TYPE_SIZE (type)))
5637 {
5638 case 8:
5639 base_atomic_type = atomicQI_type_node;
5640 break;
5641
5642 case 16:
5643 base_atomic_type = atomicHI_type_node;
5644 break;
5645
5646 case 32:
5647 base_atomic_type = atomicSI_type_node;
5648 break;
5649
5650 case 64:
5651 base_atomic_type = atomicDI_type_node;
5652 break;
5653
5654 case 128:
5655 base_atomic_type = atomicTI_type_node;
5656 break;
5657
5658 default:
5659 base_atomic_type = NULL_TREE;
5660 }
5661
5662 return base_atomic_type;
5663 }
5664
5665 /* Returns true iff unqualified CAND and BASE are equivalent. */
5666
5667 bool
5668 check_base_type (const_tree cand, const_tree base)
5669 {
5670 if (TYPE_NAME (cand) != TYPE_NAME (base)
5671 /* Apparently this is needed for Objective-C. */
5672 || TYPE_CONTEXT (cand) != TYPE_CONTEXT (base)
5673 || !attribute_list_equal (TYPE_ATTRIBUTES (cand),
5674 TYPE_ATTRIBUTES (base)))
5675 return false;
5676 /* Check alignment. */
5677 if (TYPE_ALIGN (cand) == TYPE_ALIGN (base)
5678 && TYPE_USER_ALIGN (cand) == TYPE_USER_ALIGN (base))
5679 return true;
5680 /* Atomic types increase minimal alignment. We must to do so as well
5681 or we get duplicated canonical types. See PR88686. */
5682 if ((TYPE_QUALS (cand) & TYPE_QUAL_ATOMIC))
5683 {
5684 /* See if this object can map to a basic atomic type. */
5685 tree atomic_type = find_atomic_core_type (cand);
5686 if (atomic_type && TYPE_ALIGN (atomic_type) == TYPE_ALIGN (cand))
5687 return true;
5688 }
5689 return false;
5690 }
5691
5692 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
5693
5694 bool
5695 check_qualified_type (const_tree cand, const_tree base, int type_quals)
5696 {
5697 return (TYPE_QUALS (cand) == type_quals
5698 && check_base_type (cand, base)
5699 && check_lang_type (cand, base));
5700 }
5701
5702 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
5703
5704 static bool
5705 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
5706 {
5707 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
5708 && TYPE_NAME (cand) == TYPE_NAME (base)
5709 /* Apparently this is needed for Objective-C. */
5710 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5711 /* Check alignment. */
5712 && TYPE_ALIGN (cand) == align
5713 /* Check this is a user-aligned type as build_aligned_type
5714 would create. */
5715 && TYPE_USER_ALIGN (cand)
5716 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5717 TYPE_ATTRIBUTES (base))
5718 && check_lang_type (cand, base));
5719 }
5720
5721 /* Return a version of the TYPE, qualified as indicated by the
5722 TYPE_QUALS, if one exists. If no qualified version exists yet,
5723 return NULL_TREE. */
5724
5725 tree
5726 get_qualified_type (tree type, int type_quals)
5727 {
5728 if (TYPE_QUALS (type) == type_quals)
5729 return type;
5730
5731 tree mv = TYPE_MAIN_VARIANT (type);
5732 if (check_qualified_type (mv, type, type_quals))
5733 return mv;
5734
5735 /* Search the chain of variants to see if there is already one there just
5736 like the one we need to have. If so, use that existing one. We must
5737 preserve the TYPE_NAME, since there is code that depends on this. */
5738 for (tree *tp = &TYPE_NEXT_VARIANT (mv); *tp; tp = &TYPE_NEXT_VARIANT (*tp))
5739 if (check_qualified_type (*tp, type, type_quals))
5740 {
5741 /* Put the found variant at the head of the variant list so
5742 frequently searched variants get found faster. The C++ FE
5743 benefits greatly from this. */
5744 tree t = *tp;
5745 *tp = TYPE_NEXT_VARIANT (t);
5746 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
5747 TYPE_NEXT_VARIANT (mv) = t;
5748 return t;
5749 }
5750
5751 return NULL_TREE;
5752 }
5753
5754 /* Like get_qualified_type, but creates the type if it does not
5755 exist. This function never returns NULL_TREE. */
5756
5757 tree
5758 build_qualified_type (tree type, int type_quals MEM_STAT_DECL)
5759 {
5760 tree t;
5761
5762 /* See if we already have the appropriate qualified variant. */
5763 t = get_qualified_type (type, type_quals);
5764
5765 /* If not, build it. */
5766 if (!t)
5767 {
5768 t = build_variant_type_copy (type PASS_MEM_STAT);
5769 set_type_quals (t, type_quals);
5770
5771 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
5772 {
5773 /* See if this object can map to a basic atomic type. */
5774 tree atomic_type = find_atomic_core_type (type);
5775 if (atomic_type)
5776 {
5777 /* Ensure the alignment of this type is compatible with
5778 the required alignment of the atomic type. */
5779 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
5780 SET_TYPE_ALIGN (t, TYPE_ALIGN (atomic_type));
5781 }
5782 }
5783
5784 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5785 /* Propagate structural equality. */
5786 SET_TYPE_STRUCTURAL_EQUALITY (t);
5787 else if (TYPE_CANONICAL (type) != type)
5788 /* Build the underlying canonical type, since it is different
5789 from TYPE. */
5790 {
5791 tree c = build_qualified_type (TYPE_CANONICAL (type), type_quals);
5792 TYPE_CANONICAL (t) = TYPE_CANONICAL (c);
5793 }
5794 else
5795 /* T is its own canonical type. */
5796 TYPE_CANONICAL (t) = t;
5797
5798 }
5799
5800 return t;
5801 }
5802
5803 /* Create a variant of type T with alignment ALIGN which
5804 is measured in bits. */
5805
5806 tree
5807 build_aligned_type (tree type, unsigned int align)
5808 {
5809 tree t;
5810
5811 if (TYPE_PACKED (type)
5812 || TYPE_ALIGN (type) == align)
5813 return type;
5814
5815 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5816 if (check_aligned_type (t, type, align))
5817 return t;
5818
5819 t = build_variant_type_copy (type);
5820 SET_TYPE_ALIGN (t, align);
5821 TYPE_USER_ALIGN (t) = 1;
5822
5823 return t;
5824 }
5825
5826 /* Create a new distinct copy of TYPE. The new type is made its own
5827 MAIN_VARIANT. If TYPE requires structural equality checks, the
5828 resulting type requires structural equality checks; otherwise, its
5829 TYPE_CANONICAL points to itself. */
5830
5831 tree
5832 build_distinct_type_copy (tree type MEM_STAT_DECL)
5833 {
5834 tree t = copy_node (type PASS_MEM_STAT);
5835
5836 TYPE_POINTER_TO (t) = 0;
5837 TYPE_REFERENCE_TO (t) = 0;
5838
5839 /* Set the canonical type either to a new equivalence class, or
5840 propagate the need for structural equality checks. */
5841 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5842 SET_TYPE_STRUCTURAL_EQUALITY (t);
5843 else
5844 TYPE_CANONICAL (t) = t;
5845
5846 /* Make it its own variant. */
5847 TYPE_MAIN_VARIANT (t) = t;
5848 TYPE_NEXT_VARIANT (t) = 0;
5849
5850 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
5851 whose TREE_TYPE is not t. This can also happen in the Ada
5852 frontend when using subtypes. */
5853
5854 return t;
5855 }
5856
5857 /* Create a new variant of TYPE, equivalent but distinct. This is so
5858 the caller can modify it. TYPE_CANONICAL for the return type will
5859 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
5860 are considered equal by the language itself (or that both types
5861 require structural equality checks). */
5862
5863 tree
5864 build_variant_type_copy (tree type MEM_STAT_DECL)
5865 {
5866 tree t, m = TYPE_MAIN_VARIANT (type);
5867
5868 t = build_distinct_type_copy (type PASS_MEM_STAT);
5869
5870 /* Since we're building a variant, assume that it is a non-semantic
5871 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
5872 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
5873 /* Type variants have no alias set defined. */
5874 TYPE_ALIAS_SET (t) = -1;
5875
5876 /* Add the new type to the chain of variants of TYPE. */
5877 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
5878 TYPE_NEXT_VARIANT (m) = t;
5879 TYPE_MAIN_VARIANT (t) = m;
5880
5881 return t;
5882 }
5883
5884 /* Return true if the from tree in both tree maps are equal. */
5885
5886 int
5887 tree_map_base_eq (const void *va, const void *vb)
5888 {
5889 const struct tree_map_base *const a = (const struct tree_map_base *) va,
5890 *const b = (const struct tree_map_base *) vb;
5891 return (a->from == b->from);
5892 }
5893
5894 /* Hash a from tree in a tree_base_map. */
5895
5896 unsigned int
5897 tree_map_base_hash (const void *item)
5898 {
5899 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
5900 }
5901
5902 /* Return true if this tree map structure is marked for garbage collection
5903 purposes. We simply return true if the from tree is marked, so that this
5904 structure goes away when the from tree goes away. */
5905
5906 bool
5907 tree_map_base_marked_p (const void *p)
5908 {
5909 return ggc_marked_p (((const struct tree_map_base *) p)->from);
5910 }
5911
5912 /* Hash a from tree in a tree_map. */
5913
5914 unsigned int
5915 tree_map_hash (const void *item)
5916 {
5917 return (((const struct tree_map *) item)->hash);
5918 }
5919
5920 /* Hash a from tree in a tree_decl_map. */
5921
5922 unsigned int
5923 tree_decl_map_hash (const void *item)
5924 {
5925 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
5926 }
5927
5928 /* Return the initialization priority for DECL. */
5929
5930 priority_type
5931 decl_init_priority_lookup (tree decl)
5932 {
5933 symtab_node *snode = symtab_node::get (decl);
5934
5935 if (!snode)
5936 return DEFAULT_INIT_PRIORITY;
5937 return
5938 snode->get_init_priority ();
5939 }
5940
5941 /* Return the finalization priority for DECL. */
5942
5943 priority_type
5944 decl_fini_priority_lookup (tree decl)
5945 {
5946 cgraph_node *node = cgraph_node::get (decl);
5947
5948 if (!node)
5949 return DEFAULT_INIT_PRIORITY;
5950 return
5951 node->get_fini_priority ();
5952 }
5953
5954 /* Set the initialization priority for DECL to PRIORITY. */
5955
5956 void
5957 decl_init_priority_insert (tree decl, priority_type priority)
5958 {
5959 struct symtab_node *snode;
5960
5961 if (priority == DEFAULT_INIT_PRIORITY)
5962 {
5963 snode = symtab_node::get (decl);
5964 if (!snode)
5965 return;
5966 }
5967 else if (VAR_P (decl))
5968 snode = varpool_node::get_create (decl);
5969 else
5970 snode = cgraph_node::get_create (decl);
5971 snode->set_init_priority (priority);
5972 }
5973
5974 /* Set the finalization priority for DECL to PRIORITY. */
5975
5976 void
5977 decl_fini_priority_insert (tree decl, priority_type priority)
5978 {
5979 struct cgraph_node *node;
5980
5981 if (priority == DEFAULT_INIT_PRIORITY)
5982 {
5983 node = cgraph_node::get (decl);
5984 if (!node)
5985 return;
5986 }
5987 else
5988 node = cgraph_node::get_create (decl);
5989 node->set_fini_priority (priority);
5990 }
5991
5992 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
5993
5994 static void
5995 print_debug_expr_statistics (void)
5996 {
5997 fprintf (stderr, "DECL_DEBUG_EXPR hash: size " HOST_SIZE_T_PRINT_DEC ", "
5998 HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n",
5999 (fmt_size_t) debug_expr_for_decl->size (),
6000 (fmt_size_t) debug_expr_for_decl->elements (),
6001 debug_expr_for_decl->collisions ());
6002 }
6003
6004 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6005
6006 static void
6007 print_value_expr_statistics (void)
6008 {
6009 fprintf (stderr, "DECL_VALUE_EXPR hash: size " HOST_SIZE_T_PRINT_DEC ", "
6010 HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n",
6011 (fmt_size_t) value_expr_for_decl->size (),
6012 (fmt_size_t) value_expr_for_decl->elements (),
6013 value_expr_for_decl->collisions ());
6014 }
6015
6016 /* Lookup a debug expression for FROM, and return it if we find one. */
6017
6018 tree
6019 decl_debug_expr_lookup (tree from)
6020 {
6021 struct tree_decl_map *h, in;
6022 in.base.from = from;
6023
6024 h = debug_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6025 if (h)
6026 return h->to;
6027 return NULL_TREE;
6028 }
6029
6030 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6031
6032 void
6033 decl_debug_expr_insert (tree from, tree to)
6034 {
6035 struct tree_decl_map *h;
6036
6037 h = ggc_alloc<tree_decl_map> ();
6038 h->base.from = from;
6039 h->to = to;
6040 *debug_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6041 }
6042
6043 /* Lookup a value expression for FROM, and return it if we find one. */
6044
6045 tree
6046 decl_value_expr_lookup (tree from)
6047 {
6048 struct tree_decl_map *h, in;
6049 in.base.from = from;
6050
6051 h = value_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6052 if (h)
6053 return h->to;
6054 return NULL_TREE;
6055 }
6056
6057 /* Insert a mapping FROM->TO in the value expression hashtable. */
6058
6059 void
6060 decl_value_expr_insert (tree from, tree to)
6061 {
6062 struct tree_decl_map *h;
6063
6064 /* Uses of FROM shouldn't look like they happen at the location of TO. */
6065 to = protected_set_expr_location_unshare (to, UNKNOWN_LOCATION);
6066
6067 h = ggc_alloc<tree_decl_map> ();
6068 h->base.from = from;
6069 h->to = to;
6070 *value_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6071 }
6072
6073 /* Lookup a vector of debug arguments for FROM, and return it if we
6074 find one. */
6075
6076 vec<tree, va_gc> **
6077 decl_debug_args_lookup (tree from)
6078 {
6079 struct tree_vec_map *h, in;
6080
6081 if (!DECL_HAS_DEBUG_ARGS_P (from))
6082 return NULL;
6083 gcc_checking_assert (debug_args_for_decl != NULL);
6084 in.base.from = from;
6085 h = debug_args_for_decl->find_with_hash (&in, DECL_UID (from));
6086 if (h)
6087 return &h->to;
6088 return NULL;
6089 }
6090
6091 /* Insert a mapping FROM->empty vector of debug arguments in the value
6092 expression hashtable. */
6093
6094 vec<tree, va_gc> **
6095 decl_debug_args_insert (tree from)
6096 {
6097 struct tree_vec_map *h;
6098 tree_vec_map **loc;
6099
6100 if (DECL_HAS_DEBUG_ARGS_P (from))
6101 return decl_debug_args_lookup (from);
6102 if (debug_args_for_decl == NULL)
6103 debug_args_for_decl = hash_table<tree_vec_map_cache_hasher>::create_ggc (64);
6104 h = ggc_alloc<tree_vec_map> ();
6105 h->base.from = from;
6106 h->to = NULL;
6107 loc = debug_args_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT);
6108 *loc = h;
6109 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6110 return &h->to;
6111 }
6112
6113 /* Hashing of types so that we don't make duplicates.
6114 The entry point is `type_hash_canon'. */
6115
6116 /* Generate the default hash code for TYPE. This is designed for
6117 speed, rather than maximum entropy. */
6118
6119 hashval_t
6120 type_hash_canon_hash (tree type)
6121 {
6122 inchash::hash hstate;
6123
6124 hstate.add_int (TREE_CODE (type));
6125
6126 hstate.add_flag (TYPE_STRUCTURAL_EQUALITY_P (type));
6127
6128 if (TREE_TYPE (type))
6129 hstate.add_object (TYPE_HASH (TREE_TYPE (type)));
6130
6131 for (tree t = TYPE_ATTRIBUTES (type); t; t = TREE_CHAIN (t))
6132 /* Just the identifier is adequate to distinguish. */
6133 hstate.add_object (IDENTIFIER_HASH_VALUE (get_attribute_name (t)));
6134
6135 switch (TREE_CODE (type))
6136 {
6137 case METHOD_TYPE:
6138 hstate.add_object (TYPE_HASH (TYPE_METHOD_BASETYPE (type)));
6139 /* FALLTHROUGH. */
6140 case FUNCTION_TYPE:
6141 for (tree t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6142 if (TREE_VALUE (t) != error_mark_node)
6143 hstate.add_object (TYPE_HASH (TREE_VALUE (t)));
6144 break;
6145
6146 case OFFSET_TYPE:
6147 hstate.add_object (TYPE_HASH (TYPE_OFFSET_BASETYPE (type)));
6148 break;
6149
6150 case ARRAY_TYPE:
6151 {
6152 if (TYPE_DOMAIN (type))
6153 hstate.add_object (TYPE_HASH (TYPE_DOMAIN (type)));
6154 if (!AGGREGATE_TYPE_P (TREE_TYPE (type)))
6155 {
6156 unsigned typeless = TYPE_TYPELESS_STORAGE (type);
6157 hstate.add_object (typeless);
6158 }
6159 }
6160 break;
6161
6162 case INTEGER_TYPE:
6163 {
6164 tree t = TYPE_MAX_VALUE (type);
6165 if (!t)
6166 t = TYPE_MIN_VALUE (type);
6167 for (int i = 0; i < TREE_INT_CST_NUNITS (t); i++)
6168 hstate.add_object (TREE_INT_CST_ELT (t, i));
6169 break;
6170 }
6171
6172 case BITINT_TYPE:
6173 {
6174 unsigned prec = TYPE_PRECISION (type);
6175 unsigned uns = TYPE_UNSIGNED (type);
6176 hstate.add_object (prec);
6177 hstate.add_int (uns);
6178 break;
6179 }
6180
6181 case REAL_TYPE:
6182 case FIXED_POINT_TYPE:
6183 {
6184 unsigned prec = TYPE_PRECISION (type);
6185 hstate.add_object (prec);
6186 break;
6187 }
6188
6189 case VECTOR_TYPE:
6190 hstate.add_poly_int (TYPE_VECTOR_SUBPARTS (type));
6191 break;
6192
6193 case REFERENCE_TYPE:
6194 hstate.add_flag (TYPE_REF_IS_RVALUE (type));
6195 break;
6196
6197 default:
6198 break;
6199 }
6200
6201 return hstate.end ();
6202 }
6203
6204 /* These are the Hashtable callback functions. */
6205
6206 /* Returns true iff the types are equivalent. */
6207
6208 bool
6209 type_cache_hasher::equal (type_hash *a, type_hash *b)
6210 {
6211 /* First test the things that are the same for all types. */
6212 if (a->hash != b->hash
6213 || TREE_CODE (a->type) != TREE_CODE (b->type)
6214 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6215 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6216 TYPE_ATTRIBUTES (b->type))
6217 || (TREE_CODE (a->type) != COMPLEX_TYPE
6218 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6219 return false;
6220
6221 /* Be careful about comparing arrays before and after the element type
6222 has been completed; don't compare TYPE_ALIGN unless both types are
6223 complete. */
6224 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6225 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6226 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6227 return false;
6228
6229 if (TYPE_STRUCTURAL_EQUALITY_P (a->type)
6230 != TYPE_STRUCTURAL_EQUALITY_P (b->type))
6231 return false;
6232
6233 switch (TREE_CODE (a->type))
6234 {
6235 case VOID_TYPE:
6236 case OPAQUE_TYPE:
6237 case COMPLEX_TYPE:
6238 case POINTER_TYPE:
6239 case NULLPTR_TYPE:
6240 return true;
6241
6242 case VECTOR_TYPE:
6243 return known_eq (TYPE_VECTOR_SUBPARTS (a->type),
6244 TYPE_VECTOR_SUBPARTS (b->type));
6245
6246 case ENUMERAL_TYPE:
6247 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6248 && !(TYPE_VALUES (a->type)
6249 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6250 && TYPE_VALUES (b->type)
6251 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6252 && type_list_equal (TYPE_VALUES (a->type),
6253 TYPE_VALUES (b->type))))
6254 return false;
6255
6256 /* fall through */
6257
6258 case INTEGER_TYPE:
6259 case REAL_TYPE:
6260 case BOOLEAN_TYPE:
6261 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
6262 return false;
6263 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6264 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6265 TYPE_MAX_VALUE (b->type)))
6266 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6267 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6268 TYPE_MIN_VALUE (b->type))));
6269
6270 case BITINT_TYPE:
6271 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
6272 return false;
6273 return TYPE_UNSIGNED (a->type) == TYPE_UNSIGNED (b->type);
6274
6275 case FIXED_POINT_TYPE:
6276 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6277
6278 case OFFSET_TYPE:
6279 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6280
6281 case METHOD_TYPE:
6282 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6283 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6284 || (TYPE_ARG_TYPES (a->type)
6285 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6286 && TYPE_ARG_TYPES (b->type)
6287 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6288 && type_list_equal (TYPE_ARG_TYPES (a->type),
6289 TYPE_ARG_TYPES (b->type)))))
6290 break;
6291 return false;
6292 case ARRAY_TYPE:
6293 /* Don't compare TYPE_TYPELESS_STORAGE flag on aggregates,
6294 where the flag should be inherited from the element type
6295 and can change after ARRAY_TYPEs are created; on non-aggregates
6296 compare it and hash it, scalars will never have that flag set
6297 and we need to differentiate between arrays created by different
6298 front-ends or middle-end created arrays. */
6299 return (TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type)
6300 && (AGGREGATE_TYPE_P (TREE_TYPE (a->type))
6301 || (TYPE_TYPELESS_STORAGE (a->type)
6302 == TYPE_TYPELESS_STORAGE (b->type))));
6303
6304 case RECORD_TYPE:
6305 case UNION_TYPE:
6306 case QUAL_UNION_TYPE:
6307 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6308 || (TYPE_FIELDS (a->type)
6309 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6310 && TYPE_FIELDS (b->type)
6311 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6312 && type_list_equal (TYPE_FIELDS (a->type),
6313 TYPE_FIELDS (b->type))));
6314
6315 case FUNCTION_TYPE:
6316 if ((TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6317 && (TYPE_NO_NAMED_ARGS_STDARG_P (a->type)
6318 == TYPE_NO_NAMED_ARGS_STDARG_P (b->type)))
6319 || (TYPE_ARG_TYPES (a->type)
6320 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6321 && TYPE_ARG_TYPES (b->type)
6322 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6323 && type_list_equal (TYPE_ARG_TYPES (a->type),
6324 TYPE_ARG_TYPES (b->type))))
6325 break;
6326 return false;
6327
6328 case REFERENCE_TYPE:
6329 return TYPE_REF_IS_RVALUE (a->type) == TYPE_REF_IS_RVALUE (b->type);
6330
6331 default:
6332 return false;
6333 }
6334
6335 if (lang_hooks.types.type_hash_eq != NULL)
6336 return lang_hooks.types.type_hash_eq (a->type, b->type);
6337
6338 return true;
6339 }
6340
6341 /* Given TYPE, and HASHCODE its hash code, return the canonical
6342 object for an identical type if one already exists.
6343 Otherwise, return TYPE, and record it as the canonical object.
6344
6345 To use this function, first create a type of the sort you want.
6346 Then compute its hash code from the fields of the type that
6347 make it different from other similar types.
6348 Then call this function and use the value. */
6349
6350 tree
6351 type_hash_canon (unsigned int hashcode, tree type)
6352 {
6353 type_hash in;
6354 type_hash **loc;
6355
6356 /* The hash table only contains main variants, so ensure that's what we're
6357 being passed. */
6358 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6359
6360 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6361 must call that routine before comparing TYPE_ALIGNs. */
6362 layout_type (type);
6363
6364 in.hash = hashcode;
6365 in.type = type;
6366
6367 loc = type_hash_table->find_slot_with_hash (&in, hashcode, INSERT);
6368 if (*loc)
6369 {
6370 tree t1 = ((type_hash *) *loc)->type;
6371 gcc_assert (TYPE_MAIN_VARIANT (t1) == t1
6372 && t1 != type);
6373 if (TYPE_UID (type) + 1 == next_type_uid)
6374 --next_type_uid;
6375 /* Free also min/max values and the cache for integer
6376 types. This can't be done in free_node, as LTO frees
6377 those on its own. */
6378 if (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == BITINT_TYPE)
6379 {
6380 if (TYPE_MIN_VALUE (type)
6381 && TREE_TYPE (TYPE_MIN_VALUE (type)) == type)
6382 {
6383 /* Zero is always in TYPE_CACHED_VALUES. */
6384 if (! TYPE_UNSIGNED (type))
6385 int_cst_hash_table->remove_elt (TYPE_MIN_VALUE (type));
6386 ggc_free (TYPE_MIN_VALUE (type));
6387 }
6388 if (TYPE_MAX_VALUE (type)
6389 && TREE_TYPE (TYPE_MAX_VALUE (type)) == type)
6390 {
6391 int_cst_hash_table->remove_elt (TYPE_MAX_VALUE (type));
6392 ggc_free (TYPE_MAX_VALUE (type));
6393 }
6394 if (TYPE_CACHED_VALUES_P (type))
6395 ggc_free (TYPE_CACHED_VALUES (type));
6396 }
6397 free_node (type);
6398 return t1;
6399 }
6400 else
6401 {
6402 struct type_hash *h;
6403
6404 h = ggc_alloc<type_hash> ();
6405 h->hash = hashcode;
6406 h->type = type;
6407 *loc = h;
6408
6409 return type;
6410 }
6411 }
6412
6413 static void
6414 print_type_hash_statistics (void)
6415 {
6416 fprintf (stderr, "Type hash: size " HOST_SIZE_T_PRINT_DEC ", "
6417 HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n",
6418 (fmt_size_t) type_hash_table->size (),
6419 (fmt_size_t) type_hash_table->elements (),
6420 type_hash_table->collisions ());
6421 }
6422
6423 /* Given two lists of types
6424 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6425 return 1 if the lists contain the same types in the same order.
6426 Also, the TREE_PURPOSEs must match. */
6427
6428 bool
6429 type_list_equal (const_tree l1, const_tree l2)
6430 {
6431 const_tree t1, t2;
6432
6433 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6434 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6435 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6436 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6437 && (TREE_TYPE (TREE_PURPOSE (t1))
6438 == TREE_TYPE (TREE_PURPOSE (t2))))))
6439 return false;
6440
6441 return t1 == t2;
6442 }
6443
6444 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6445 given by TYPE. If the argument list accepts variable arguments,
6446 then this function counts only the ordinary arguments. */
6447
6448 int
6449 type_num_arguments (const_tree fntype)
6450 {
6451 int i = 0;
6452
6453 for (tree t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
6454 /* If the function does not take a variable number of arguments,
6455 the last element in the list will have type `void'. */
6456 if (VOID_TYPE_P (TREE_VALUE (t)))
6457 break;
6458 else
6459 ++i;
6460
6461 return i;
6462 }
6463
6464 /* Return the type of the function TYPE's argument ARGNO if known.
6465 For vararg function's where ARGNO refers to one of the variadic
6466 arguments return null. Otherwise, return a void_type_node for
6467 out-of-bounds ARGNO. */
6468
6469 tree
6470 type_argument_type (const_tree fntype, unsigned argno)
6471 {
6472 /* Treat zero the same as an out-of-bounds argument number. */
6473 if (!argno)
6474 return void_type_node;
6475
6476 function_args_iterator iter;
6477
6478 tree argtype;
6479 unsigned i = 1;
6480 FOREACH_FUNCTION_ARGS (fntype, argtype, iter)
6481 {
6482 /* A vararg function's argument list ends in a null. Otherwise,
6483 an ordinary function's argument list ends with void. Return
6484 null if ARGNO refers to a vararg argument, void_type_node if
6485 it's out of bounds, and the formal argument type otherwise. */
6486 if (!argtype)
6487 break;
6488
6489 if (i == argno || VOID_TYPE_P (argtype))
6490 return argtype;
6491
6492 ++i;
6493 }
6494
6495 return NULL_TREE;
6496 }
6497
6498 /* True if integer constants T1 and T2
6499 represent the same constant value. */
6500
6501 bool
6502 tree_int_cst_equal (const_tree t1, const_tree t2)
6503 {
6504 if (t1 == t2)
6505 return true;
6506
6507 if (t1 == 0 || t2 == 0)
6508 return false;
6509
6510 STRIP_ANY_LOCATION_WRAPPER (t1);
6511 STRIP_ANY_LOCATION_WRAPPER (t2);
6512
6513 if (TREE_CODE (t1) == INTEGER_CST
6514 && TREE_CODE (t2) == INTEGER_CST
6515 && wi::to_widest (t1) == wi::to_widest (t2))
6516 return true;
6517
6518 return false;
6519 }
6520
6521 /* Return true if T is an INTEGER_CST whose numerical value (extended
6522 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
6523
6524 bool
6525 tree_fits_shwi_p (const_tree t)
6526 {
6527 return (t != NULL_TREE
6528 && TREE_CODE (t) == INTEGER_CST
6529 && wi::fits_shwi_p (wi::to_widest (t)));
6530 }
6531
6532 /* Return true if T is an INTEGER_CST or POLY_INT_CST whose numerical
6533 value (extended according to TYPE_UNSIGNED) fits in a poly_int64. */
6534
6535 bool
6536 tree_fits_poly_int64_p (const_tree t)
6537 {
6538 if (t == NULL_TREE)
6539 return false;
6540 if (POLY_INT_CST_P (t))
6541 {
6542 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; i++)
6543 if (!wi::fits_shwi_p (wi::to_wide (POLY_INT_CST_COEFF (t, i))))
6544 return false;
6545 return true;
6546 }
6547 return (TREE_CODE (t) == INTEGER_CST
6548 && wi::fits_shwi_p (wi::to_widest (t)));
6549 }
6550
6551 /* Return true if T is an INTEGER_CST whose numerical value (extended
6552 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
6553
6554 bool
6555 tree_fits_uhwi_p (const_tree t)
6556 {
6557 return (t != NULL_TREE
6558 && TREE_CODE (t) == INTEGER_CST
6559 && wi::fits_uhwi_p (wi::to_widest (t)));
6560 }
6561
6562 /* Return true if T is an INTEGER_CST or POLY_INT_CST whose numerical
6563 value (extended according to TYPE_UNSIGNED) fits in a poly_uint64. */
6564
6565 bool
6566 tree_fits_poly_uint64_p (const_tree t)
6567 {
6568 if (t == NULL_TREE)
6569 return false;
6570 if (POLY_INT_CST_P (t))
6571 {
6572 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; i++)
6573 if (!wi::fits_uhwi_p (wi::to_widest (POLY_INT_CST_COEFF (t, i))))
6574 return false;
6575 return true;
6576 }
6577 return (TREE_CODE (t) == INTEGER_CST
6578 && wi::fits_uhwi_p (wi::to_widest (t)));
6579 }
6580
6581 /* T is an INTEGER_CST whose numerical value (extended according to
6582 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
6583 HOST_WIDE_INT. */
6584
6585 HOST_WIDE_INT
6586 tree_to_shwi (const_tree t)
6587 {
6588 gcc_assert (tree_fits_shwi_p (t));
6589 return TREE_INT_CST_LOW (t);
6590 }
6591
6592 /* T is an INTEGER_CST whose numerical value (extended according to
6593 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
6594 HOST_WIDE_INT. */
6595
6596 unsigned HOST_WIDE_INT
6597 tree_to_uhwi (const_tree t)
6598 {
6599 gcc_assert (tree_fits_uhwi_p (t));
6600 return TREE_INT_CST_LOW (t);
6601 }
6602
6603 /* Return the most significant (sign) bit of T. */
6604
6605 int
6606 tree_int_cst_sign_bit (const_tree t)
6607 {
6608 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
6609
6610 return wi::extract_uhwi (wi::to_wide (t), bitno, 1);
6611 }
6612
6613 /* Return an indication of the sign of the integer constant T.
6614 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
6615 Note that -1 will never be returned if T's type is unsigned. */
6616
6617 int
6618 tree_int_cst_sgn (const_tree t)
6619 {
6620 if (wi::to_wide (t) == 0)
6621 return 0;
6622 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
6623 return 1;
6624 else if (wi::neg_p (wi::to_wide (t)))
6625 return -1;
6626 else
6627 return 1;
6628 }
6629
6630 /* Return the minimum number of bits needed to represent VALUE in a
6631 signed or unsigned type, UNSIGNEDP says which. */
6632
6633 unsigned int
6634 tree_int_cst_min_precision (tree value, signop sgn)
6635 {
6636 /* If the value is negative, compute its negative minus 1. The latter
6637 adjustment is because the absolute value of the largest negative value
6638 is one larger than the largest positive value. This is equivalent to
6639 a bit-wise negation, so use that operation instead. */
6640
6641 if (tree_int_cst_sgn (value) < 0)
6642 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
6643
6644 /* Return the number of bits needed, taking into account the fact
6645 that we need one more bit for a signed than unsigned type.
6646 If value is 0 or -1, the minimum precision is 1 no matter
6647 whether unsignedp is true or false. */
6648
6649 if (integer_zerop (value))
6650 return 1;
6651 else
6652 return tree_floor_log2 (value) + 1 + (sgn == SIGNED ? 1 : 0) ;
6653 }
6654
6655 /* Return truthvalue of whether T1 is the same tree structure as T2.
6656 Return 1 if they are the same.
6657 Return 0 if they are understandably different.
6658 Return -1 if either contains tree structure not understood by
6659 this function. */
6660
6661 int
6662 simple_cst_equal (const_tree t1, const_tree t2)
6663 {
6664 enum tree_code code1, code2;
6665 int cmp;
6666 int i;
6667
6668 if (t1 == t2)
6669 return 1;
6670 if (t1 == 0 || t2 == 0)
6671 return 0;
6672
6673 /* For location wrappers to be the same, they must be at the same
6674 source location (and wrap the same thing). */
6675 if (location_wrapper_p (t1) && location_wrapper_p (t2))
6676 {
6677 if (EXPR_LOCATION (t1) != EXPR_LOCATION (t2))
6678 return 0;
6679 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6680 }
6681
6682 code1 = TREE_CODE (t1);
6683 code2 = TREE_CODE (t2);
6684
6685 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
6686 {
6687 if (CONVERT_EXPR_CODE_P (code2)
6688 || code2 == NON_LVALUE_EXPR)
6689 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6690 else
6691 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
6692 }
6693
6694 else if (CONVERT_EXPR_CODE_P (code2)
6695 || code2 == NON_LVALUE_EXPR)
6696 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
6697
6698 if (code1 != code2)
6699 return 0;
6700
6701 switch (code1)
6702 {
6703 case INTEGER_CST:
6704 return wi::to_widest (t1) == wi::to_widest (t2);
6705
6706 case REAL_CST:
6707 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
6708
6709 case FIXED_CST:
6710 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
6711
6712 case STRING_CST:
6713 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
6714 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
6715 TREE_STRING_LENGTH (t1)));
6716
6717 case CONSTRUCTOR:
6718 {
6719 unsigned HOST_WIDE_INT idx;
6720 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
6721 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
6722
6723 if (vec_safe_length (v1) != vec_safe_length (v2))
6724 return 0;
6725
6726 for (idx = 0; idx < vec_safe_length (v1); ++idx)
6727 {
6728 if ((*v1)[idx].index
6729 && TREE_CODE ((*v1)[idx].index) == FIELD_DECL)
6730 {
6731 if ((*v1)[idx].index != (*v2)[idx].index)
6732 return 0;
6733 }
6734 else
6735 {
6736 cmp = simple_cst_equal ((*v1)[idx].index, (*v2)[idx].index);
6737 if (cmp <= 0)
6738 return cmp;
6739 }
6740 cmp = simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value);
6741 if (cmp <= 0)
6742 return cmp;
6743 }
6744 return 1;
6745 }
6746
6747 case SAVE_EXPR:
6748 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6749
6750 case CALL_EXPR:
6751 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
6752 if (cmp <= 0)
6753 return cmp;
6754 if (call_expr_nargs (t1) != call_expr_nargs (t2))
6755 return 0;
6756 {
6757 const_tree arg1, arg2;
6758 const_call_expr_arg_iterator iter1, iter2;
6759 for (arg1 = first_const_call_expr_arg (t1, &iter1),
6760 arg2 = first_const_call_expr_arg (t2, &iter2);
6761 arg1 && arg2;
6762 arg1 = next_const_call_expr_arg (&iter1),
6763 arg2 = next_const_call_expr_arg (&iter2))
6764 {
6765 cmp = simple_cst_equal (arg1, arg2);
6766 if (cmp <= 0)
6767 return cmp;
6768 }
6769 return arg1 == arg2;
6770 }
6771
6772 case TARGET_EXPR:
6773 /* Special case: if either target is an unallocated VAR_DECL,
6774 it means that it's going to be unified with whatever the
6775 TARGET_EXPR is really supposed to initialize, so treat it
6776 as being equivalent to anything. */
6777 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
6778 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
6779 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
6780 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
6781 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
6782 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
6783 cmp = 1;
6784 else
6785 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6786
6787 if (cmp <= 0)
6788 return cmp;
6789
6790 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
6791
6792 case WITH_CLEANUP_EXPR:
6793 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6794 if (cmp <= 0)
6795 return cmp;
6796
6797 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
6798
6799 case COMPONENT_REF:
6800 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
6801 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6802
6803 return 0;
6804
6805 case VAR_DECL:
6806 case PARM_DECL:
6807 case CONST_DECL:
6808 case FUNCTION_DECL:
6809 return 0;
6810
6811 default:
6812 if (POLY_INT_CST_P (t1))
6813 /* A false return means maybe_ne rather than known_ne. */
6814 return known_eq (poly_widest_int::from (poly_int_cst_value (t1),
6815 TYPE_SIGN (TREE_TYPE (t1))),
6816 poly_widest_int::from (poly_int_cst_value (t2),
6817 TYPE_SIGN (TREE_TYPE (t2))));
6818 break;
6819 }
6820
6821 /* This general rule works for most tree codes. All exceptions should be
6822 handled above. If this is a language-specific tree code, we can't
6823 trust what might be in the operand, so say we don't know
6824 the situation. */
6825 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
6826 return -1;
6827
6828 switch (TREE_CODE_CLASS (code1))
6829 {
6830 case tcc_unary:
6831 case tcc_binary:
6832 case tcc_comparison:
6833 case tcc_expression:
6834 case tcc_reference:
6835 case tcc_statement:
6836 cmp = 1;
6837 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
6838 {
6839 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
6840 if (cmp <= 0)
6841 return cmp;
6842 }
6843
6844 return cmp;
6845
6846 default:
6847 return -1;
6848 }
6849 }
6850
6851 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
6852 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
6853 than U, respectively. */
6854
6855 int
6856 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
6857 {
6858 if (tree_int_cst_sgn (t) < 0)
6859 return -1;
6860 else if (!tree_fits_uhwi_p (t))
6861 return 1;
6862 else if (TREE_INT_CST_LOW (t) == u)
6863 return 0;
6864 else if (TREE_INT_CST_LOW (t) < u)
6865 return -1;
6866 else
6867 return 1;
6868 }
6869
6870 /* Return true if SIZE represents a constant size that is in bounds of
6871 what the middle-end and the backend accepts (covering not more than
6872 half of the address-space).
6873 When PERR is non-null, set *PERR on failure to the description of
6874 why SIZE is not valid. */
6875
6876 bool
6877 valid_constant_size_p (const_tree size, cst_size_error *perr /* = NULL */)
6878 {
6879 if (POLY_INT_CST_P (size))
6880 {
6881 if (TREE_OVERFLOW (size))
6882 return false;
6883 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
6884 if (!valid_constant_size_p (POLY_INT_CST_COEFF (size, i)))
6885 return false;
6886 return true;
6887 }
6888
6889 cst_size_error error;
6890 if (!perr)
6891 perr = &error;
6892
6893 if (TREE_CODE (size) != INTEGER_CST)
6894 {
6895 *perr = cst_size_not_constant;
6896 return false;
6897 }
6898
6899 if (TREE_OVERFLOW_P (size))
6900 {
6901 *perr = cst_size_overflow;
6902 return false;
6903 }
6904
6905 if (tree_int_cst_sgn (size) < 0)
6906 {
6907 *perr = cst_size_negative;
6908 return false;
6909 }
6910 if (!tree_fits_uhwi_p (size)
6911 || (wi::to_widest (TYPE_MAX_VALUE (sizetype))
6912 < wi::to_widest (size) * 2))
6913 {
6914 *perr = cst_size_too_big;
6915 return false;
6916 }
6917
6918 return true;
6919 }
6920
6921 /* Return the precision of the type, or for a complex or vector type the
6922 precision of the type of its elements. */
6923
6924 unsigned int
6925 element_precision (const_tree type)
6926 {
6927 if (!TYPE_P (type))
6928 type = TREE_TYPE (type);
6929 enum tree_code code = TREE_CODE (type);
6930 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
6931 type = TREE_TYPE (type);
6932
6933 return TYPE_PRECISION (type);
6934 }
6935
6936 /* Return true if CODE represents an associative tree code. Otherwise
6937 return false. */
6938 bool
6939 associative_tree_code (enum tree_code code)
6940 {
6941 switch (code)
6942 {
6943 case BIT_IOR_EXPR:
6944 case BIT_AND_EXPR:
6945 case BIT_XOR_EXPR:
6946 case PLUS_EXPR:
6947 case MULT_EXPR:
6948 case MIN_EXPR:
6949 case MAX_EXPR:
6950 return true;
6951
6952 default:
6953 break;
6954 }
6955 return false;
6956 }
6957
6958 /* Return true if CODE represents a commutative tree code. Otherwise
6959 return false. */
6960 bool
6961 commutative_tree_code (enum tree_code code)
6962 {
6963 switch (code)
6964 {
6965 case PLUS_EXPR:
6966 case MULT_EXPR:
6967 case MULT_HIGHPART_EXPR:
6968 case MIN_EXPR:
6969 case MAX_EXPR:
6970 case BIT_IOR_EXPR:
6971 case BIT_XOR_EXPR:
6972 case BIT_AND_EXPR:
6973 case NE_EXPR:
6974 case EQ_EXPR:
6975 case UNORDERED_EXPR:
6976 case ORDERED_EXPR:
6977 case UNEQ_EXPR:
6978 case LTGT_EXPR:
6979 case TRUTH_AND_EXPR:
6980 case TRUTH_XOR_EXPR:
6981 case TRUTH_OR_EXPR:
6982 case WIDEN_MULT_EXPR:
6983 case VEC_WIDEN_MULT_HI_EXPR:
6984 case VEC_WIDEN_MULT_LO_EXPR:
6985 case VEC_WIDEN_MULT_EVEN_EXPR:
6986 case VEC_WIDEN_MULT_ODD_EXPR:
6987 return true;
6988
6989 default:
6990 break;
6991 }
6992 return false;
6993 }
6994
6995 /* Return true if CODE represents a ternary tree code for which the
6996 first two operands are commutative. Otherwise return false. */
6997 bool
6998 commutative_ternary_tree_code (enum tree_code code)
6999 {
7000 switch (code)
7001 {
7002 case WIDEN_MULT_PLUS_EXPR:
7003 case WIDEN_MULT_MINUS_EXPR:
7004 case DOT_PROD_EXPR:
7005 return true;
7006
7007 default:
7008 break;
7009 }
7010 return false;
7011 }
7012
7013 /* Returns true if CODE can overflow. */
7014
7015 bool
7016 operation_can_overflow (enum tree_code code)
7017 {
7018 switch (code)
7019 {
7020 case PLUS_EXPR:
7021 case MINUS_EXPR:
7022 case MULT_EXPR:
7023 case LSHIFT_EXPR:
7024 /* Can overflow in various ways. */
7025 return true;
7026 case TRUNC_DIV_EXPR:
7027 case EXACT_DIV_EXPR:
7028 case FLOOR_DIV_EXPR:
7029 case CEIL_DIV_EXPR:
7030 /* For INT_MIN / -1. */
7031 return true;
7032 case NEGATE_EXPR:
7033 case ABS_EXPR:
7034 /* For -INT_MIN. */
7035 return true;
7036 default:
7037 /* These operators cannot overflow. */
7038 return false;
7039 }
7040 }
7041
7042 /* Returns true if CODE operating on operands of type TYPE doesn't overflow, or
7043 ftrapv doesn't generate trapping insns for CODE. */
7044
7045 bool
7046 operation_no_trapping_overflow (tree type, enum tree_code code)
7047 {
7048 gcc_checking_assert (ANY_INTEGRAL_TYPE_P (type));
7049
7050 /* We don't generate instructions that trap on overflow for complex or vector
7051 types. */
7052 if (!INTEGRAL_TYPE_P (type))
7053 return true;
7054
7055 if (!TYPE_OVERFLOW_TRAPS (type))
7056 return true;
7057
7058 switch (code)
7059 {
7060 case PLUS_EXPR:
7061 case MINUS_EXPR:
7062 case MULT_EXPR:
7063 case NEGATE_EXPR:
7064 case ABS_EXPR:
7065 /* These operators can overflow, and -ftrapv generates trapping code for
7066 these. */
7067 return false;
7068 case TRUNC_DIV_EXPR:
7069 case EXACT_DIV_EXPR:
7070 case FLOOR_DIV_EXPR:
7071 case CEIL_DIV_EXPR:
7072 case LSHIFT_EXPR:
7073 /* These operators can overflow, but -ftrapv does not generate trapping
7074 code for these. */
7075 return true;
7076 default:
7077 /* These operators cannot overflow. */
7078 return true;
7079 }
7080 }
7081
7082 /* Constructors for pointer, array and function types.
7083 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7084 constructed by language-dependent code, not here.) */
7085
7086 /* Construct, lay out and return the type of pointers to TO_TYPE with
7087 mode MODE. If MODE is VOIDmode, a pointer mode for the address
7088 space of TO_TYPE will be picked. If CAN_ALIAS_ALL is TRUE,
7089 indicate this type can reference all of memory. If such a type has
7090 already been constructed, reuse it. */
7091
7092 tree
7093 build_pointer_type_for_mode (tree to_type, machine_mode mode,
7094 bool can_alias_all)
7095 {
7096 tree t;
7097 bool could_alias = can_alias_all;
7098
7099 if (to_type == error_mark_node)
7100 return error_mark_node;
7101
7102 if (mode == VOIDmode)
7103 {
7104 addr_space_t as = TYPE_ADDR_SPACE (to_type);
7105 mode = targetm.addr_space.pointer_mode (as);
7106 }
7107
7108 /* If the pointed-to type has the may_alias attribute set, force
7109 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7110 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7111 can_alias_all = true;
7112
7113 /* In some cases, languages will have things that aren't a POINTER_TYPE
7114 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7115 In that case, return that type without regard to the rest of our
7116 operands.
7117
7118 ??? This is a kludge, but consistent with the way this function has
7119 always operated and there doesn't seem to be a good way to avoid this
7120 at the moment. */
7121 if (TYPE_POINTER_TO (to_type) != 0
7122 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7123 return TYPE_POINTER_TO (to_type);
7124
7125 /* First, if we already have a type for pointers to TO_TYPE and it's
7126 the proper mode, use it. */
7127 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7128 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7129 return t;
7130
7131 t = make_node (POINTER_TYPE);
7132
7133 TREE_TYPE (t) = to_type;
7134 SET_TYPE_MODE (t, mode);
7135 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7136 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7137 TYPE_POINTER_TO (to_type) = t;
7138
7139 /* During LTO we do not set TYPE_CANONICAL of pointers and references. */
7140 if (TYPE_STRUCTURAL_EQUALITY_P (to_type) || in_lto_p)
7141 SET_TYPE_STRUCTURAL_EQUALITY (t);
7142 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
7143 TYPE_CANONICAL (t)
7144 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7145 mode, false);
7146
7147 /* Lay out the type. This function has many callers that are concerned
7148 with expression-construction, and this simplifies them all. */
7149 layout_type (t);
7150
7151 return t;
7152 }
7153
7154 /* By default build pointers in ptr_mode. */
7155
7156 tree
7157 build_pointer_type (tree to_type)
7158 {
7159 return build_pointer_type_for_mode (to_type, VOIDmode, false);
7160 }
7161
7162 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7163
7164 tree
7165 build_reference_type_for_mode (tree to_type, machine_mode mode,
7166 bool can_alias_all)
7167 {
7168 tree t;
7169 bool could_alias = can_alias_all;
7170
7171 if (to_type == error_mark_node)
7172 return error_mark_node;
7173
7174 if (mode == VOIDmode)
7175 {
7176 addr_space_t as = TYPE_ADDR_SPACE (to_type);
7177 mode = targetm.addr_space.pointer_mode (as);
7178 }
7179
7180 /* If the pointed-to type has the may_alias attribute set, force
7181 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7182 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7183 can_alias_all = true;
7184
7185 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7186 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7187 In that case, return that type without regard to the rest of our
7188 operands.
7189
7190 ??? This is a kludge, but consistent with the way this function has
7191 always operated and there doesn't seem to be a good way to avoid this
7192 at the moment. */
7193 if (TYPE_REFERENCE_TO (to_type) != 0
7194 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7195 return TYPE_REFERENCE_TO (to_type);
7196
7197 /* First, if we already have a type for pointers to TO_TYPE and it's
7198 the proper mode, use it. */
7199 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7200 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7201 return t;
7202
7203 t = make_node (REFERENCE_TYPE);
7204
7205 TREE_TYPE (t) = to_type;
7206 SET_TYPE_MODE (t, mode);
7207 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7208 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7209 TYPE_REFERENCE_TO (to_type) = t;
7210
7211 /* During LTO we do not set TYPE_CANONICAL of pointers and references. */
7212 if (TYPE_STRUCTURAL_EQUALITY_P (to_type) || in_lto_p)
7213 SET_TYPE_STRUCTURAL_EQUALITY (t);
7214 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
7215 TYPE_CANONICAL (t)
7216 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7217 mode, false);
7218
7219 layout_type (t);
7220
7221 return t;
7222 }
7223
7224
7225 /* Build the node for the type of references-to-TO_TYPE by default
7226 in ptr_mode. */
7227
7228 tree
7229 build_reference_type (tree to_type)
7230 {
7231 return build_reference_type_for_mode (to_type, VOIDmode, false);
7232 }
7233
7234 #define MAX_INT_CACHED_PREC \
7235 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7236 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7237
7238 static void
7239 clear_nonstandard_integer_type_cache (void)
7240 {
7241 for (size_t i = 0 ; i < 2 * MAX_INT_CACHED_PREC + 2 ; i++)
7242 {
7243 nonstandard_integer_type_cache[i] = NULL;
7244 }
7245 }
7246
7247 /* Builds a signed or unsigned integer type of precision PRECISION.
7248 Used for C bitfields whose precision does not match that of
7249 built-in target types. */
7250 tree
7251 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7252 int unsignedp)
7253 {
7254 tree itype, ret;
7255
7256 if (unsignedp)
7257 unsignedp = MAX_INT_CACHED_PREC + 1;
7258
7259 if (precision <= MAX_INT_CACHED_PREC)
7260 {
7261 itype = nonstandard_integer_type_cache[precision + unsignedp];
7262 if (itype)
7263 return itype;
7264 }
7265
7266 itype = make_node (INTEGER_TYPE);
7267 TYPE_PRECISION (itype) = precision;
7268
7269 if (unsignedp)
7270 fixup_unsigned_type (itype);
7271 else
7272 fixup_signed_type (itype);
7273
7274 inchash::hash hstate;
7275 inchash::add_expr (TYPE_MAX_VALUE (itype), hstate);
7276 ret = type_hash_canon (hstate.end (), itype);
7277 if (precision <= MAX_INT_CACHED_PREC)
7278 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7279
7280 return ret;
7281 }
7282
7283 #define MAX_BOOL_CACHED_PREC \
7284 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7285 static GTY(()) tree nonstandard_boolean_type_cache[MAX_BOOL_CACHED_PREC + 1];
7286
7287 /* Builds a boolean type of precision PRECISION.
7288 Used for boolean vectors to choose proper vector element size. */
7289 tree
7290 build_nonstandard_boolean_type (unsigned HOST_WIDE_INT precision)
7291 {
7292 tree type;
7293
7294 if (precision <= MAX_BOOL_CACHED_PREC)
7295 {
7296 type = nonstandard_boolean_type_cache[precision];
7297 if (type)
7298 return type;
7299 }
7300
7301 type = make_node (BOOLEAN_TYPE);
7302 TYPE_PRECISION (type) = precision;
7303 fixup_signed_type (type);
7304
7305 if (precision <= MAX_INT_CACHED_PREC)
7306 nonstandard_boolean_type_cache[precision] = type;
7307
7308 return type;
7309 }
7310
7311 static GTY(()) vec<tree, va_gc> *bitint_type_cache;
7312
7313 /* Builds a signed or unsigned _BitInt(PRECISION) type. */
7314 tree
7315 build_bitint_type (unsigned HOST_WIDE_INT precision, int unsignedp)
7316 {
7317 tree itype, ret;
7318
7319 gcc_checking_assert (precision >= 1 + !unsignedp);
7320
7321 if (unsignedp)
7322 unsignedp = MAX_INT_CACHED_PREC + 1;
7323
7324 if (bitint_type_cache == NULL)
7325 vec_safe_grow_cleared (bitint_type_cache, 2 * MAX_INT_CACHED_PREC + 2);
7326
7327 if (precision <= MAX_INT_CACHED_PREC)
7328 {
7329 itype = (*bitint_type_cache)[precision + unsignedp];
7330 if (itype)
7331 return itype;
7332 }
7333
7334 itype = make_node (BITINT_TYPE);
7335 TYPE_PRECISION (itype) = precision;
7336
7337 if (unsignedp)
7338 fixup_unsigned_type (itype);
7339 else
7340 fixup_signed_type (itype);
7341
7342 inchash::hash hstate;
7343 inchash::add_expr (TYPE_MAX_VALUE (itype), hstate);
7344 ret = type_hash_canon (hstate.end (), itype);
7345 if (precision <= MAX_INT_CACHED_PREC)
7346 (*bitint_type_cache)[precision + unsignedp] = ret;
7347
7348 return ret;
7349 }
7350
7351 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7352 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7353 is true, reuse such a type that has already been constructed. */
7354
7355 static tree
7356 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7357 {
7358 tree itype = make_node (INTEGER_TYPE);
7359
7360 TREE_TYPE (itype) = type;
7361
7362 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7363 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7364
7365 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7366 SET_TYPE_MODE (itype, TYPE_MODE (type));
7367 TYPE_SIZE (itype) = TYPE_SIZE (type);
7368 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7369 SET_TYPE_ALIGN (itype, TYPE_ALIGN (type));
7370 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7371 SET_TYPE_WARN_IF_NOT_ALIGN (itype, TYPE_WARN_IF_NOT_ALIGN (type));
7372
7373 if (!shared)
7374 return itype;
7375
7376 if ((TYPE_MIN_VALUE (itype)
7377 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7378 || (TYPE_MAX_VALUE (itype)
7379 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7380 {
7381 /* Since we cannot reliably merge this type, we need to compare it using
7382 structural equality checks. */
7383 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7384 return itype;
7385 }
7386
7387 hashval_t hash = type_hash_canon_hash (itype);
7388 itype = type_hash_canon (hash, itype);
7389
7390 return itype;
7391 }
7392
7393 /* Wrapper around build_range_type_1 with SHARED set to true. */
7394
7395 tree
7396 build_range_type (tree type, tree lowval, tree highval)
7397 {
7398 return build_range_type_1 (type, lowval, highval, true);
7399 }
7400
7401 /* Wrapper around build_range_type_1 with SHARED set to false. */
7402
7403 tree
7404 build_nonshared_range_type (tree type, tree lowval, tree highval)
7405 {
7406 return build_range_type_1 (type, lowval, highval, false);
7407 }
7408
7409 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7410 MAXVAL should be the maximum value in the domain
7411 (one less than the length of the array).
7412
7413 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7414 We don't enforce this limit, that is up to caller (e.g. language front end).
7415 The limit exists because the result is a signed type and we don't handle
7416 sizes that use more than one HOST_WIDE_INT. */
7417
7418 tree
7419 build_index_type (tree maxval)
7420 {
7421 return build_range_type (sizetype, size_zero_node, maxval);
7422 }
7423
7424 /* Return true if the debug information for TYPE, a subtype, should be emitted
7425 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7426 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7427 debug info and doesn't reflect the source code. */
7428
7429 bool
7430 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7431 {
7432 tree base_type = TREE_TYPE (type), low, high;
7433
7434 /* Subrange types have a base type which is an integral type. */
7435 if (!INTEGRAL_TYPE_P (base_type))
7436 return false;
7437
7438 /* Get the real bounds of the subtype. */
7439 if (lang_hooks.types.get_subrange_bounds)
7440 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7441 else
7442 {
7443 low = TYPE_MIN_VALUE (type);
7444 high = TYPE_MAX_VALUE (type);
7445 }
7446
7447 /* If the type and its base type have the same representation and the same
7448 name, then the type is not a subrange but a copy of the base type. */
7449 if ((TREE_CODE (base_type) == INTEGER_TYPE
7450 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7451 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7452 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7453 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type))
7454 && TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (base_type))
7455 return false;
7456
7457 if (lowval)
7458 *lowval = low;
7459 if (highval)
7460 *highval = high;
7461 return true;
7462 }
7463
7464 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7465 and number of elements specified by the range of values of INDEX_TYPE.
7466 If TYPELESS_STORAGE is true, TYPE_TYPELESS_STORAGE flag is set on the type.
7467 If SHARED is true, reuse such a type that has already been constructed.
7468 If SET_CANONICAL is true, compute TYPE_CANONICAL from the element type. */
7469
7470 tree
7471 build_array_type_1 (tree elt_type, tree index_type, bool typeless_storage,
7472 bool shared, bool set_canonical)
7473 {
7474 tree t;
7475
7476 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7477 {
7478 error ("arrays of functions are not meaningful");
7479 elt_type = integer_type_node;
7480 }
7481
7482 t = make_node (ARRAY_TYPE);
7483 TREE_TYPE (t) = elt_type;
7484 TYPE_DOMAIN (t) = index_type;
7485 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7486 TYPE_TYPELESS_STORAGE (t) = typeless_storage;
7487
7488 /* Set TYPE_STRUCTURAL_EQUALITY_P. */
7489 if (set_canonical
7490 && (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7491 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type))
7492 || in_lto_p))
7493 SET_TYPE_STRUCTURAL_EQUALITY (t);
7494
7495 layout_type (t);
7496
7497 if (shared)
7498 {
7499 hashval_t hash = type_hash_canon_hash (t);
7500 tree probe_type = t;
7501 t = type_hash_canon (hash, t);
7502 if (t != probe_type)
7503 return t;
7504 }
7505
7506 if (TYPE_CANONICAL (t) == t && set_canonical)
7507 {
7508 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7509 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type))
7510 || in_lto_p)
7511 gcc_unreachable ();
7512 else if (TYPE_CANONICAL (elt_type) != elt_type
7513 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7514 TYPE_CANONICAL (t)
7515 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7516 index_type
7517 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7518 typeless_storage, shared, set_canonical);
7519 }
7520
7521 return t;
7522 }
7523
7524 /* Wrapper around build_array_type_1 with SHARED set to true. */
7525
7526 tree
7527 build_array_type (tree elt_type, tree index_type, bool typeless_storage)
7528 {
7529 return
7530 build_array_type_1 (elt_type, index_type, typeless_storage, true, true);
7531 }
7532
7533 /* Wrapper around build_array_type_1 with SHARED set to false. */
7534
7535 tree
7536 build_nonshared_array_type (tree elt_type, tree index_type)
7537 {
7538 return build_array_type_1 (elt_type, index_type, false, false, true);
7539 }
7540
7541 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7542 sizetype. */
7543
7544 tree
7545 build_array_type_nelts (tree elt_type, poly_uint64 nelts)
7546 {
7547 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7548 }
7549
7550 /* Computes the canonical argument types from the argument type list
7551 ARGTYPES.
7552
7553 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7554 on entry to this function, or if any of the ARGTYPES are
7555 structural.
7556
7557 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7558 true on entry to this function, or if any of the ARGTYPES are
7559 non-canonical.
7560
7561 Returns a canonical argument list, which may be ARGTYPES when the
7562 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7563 true) or would not differ from ARGTYPES. */
7564
7565 static tree
7566 maybe_canonicalize_argtypes (tree argtypes,
7567 bool *any_structural_p,
7568 bool *any_noncanonical_p)
7569 {
7570 tree arg;
7571 bool any_noncanonical_argtypes_p = false;
7572
7573 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7574 {
7575 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7576 /* Fail gracefully by stating that the type is structural. */
7577 *any_structural_p = true;
7578 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7579 *any_structural_p = true;
7580 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7581 || TREE_PURPOSE (arg))
7582 /* If the argument has a default argument, we consider it
7583 non-canonical even though the type itself is canonical.
7584 That way, different variants of function and method types
7585 with default arguments will all point to the variant with
7586 no defaults as their canonical type. */
7587 any_noncanonical_argtypes_p = true;
7588 }
7589
7590 if (*any_structural_p)
7591 return argtypes;
7592
7593 if (any_noncanonical_argtypes_p)
7594 {
7595 /* Build the canonical list of argument types. */
7596 tree canon_argtypes = NULL_TREE;
7597 bool is_void = false;
7598
7599 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7600 {
7601 if (arg == void_list_node)
7602 is_void = true;
7603 else
7604 canon_argtypes = tree_cons (NULL_TREE,
7605 TYPE_CANONICAL (TREE_VALUE (arg)),
7606 canon_argtypes);
7607 }
7608
7609 canon_argtypes = nreverse (canon_argtypes);
7610 if (is_void)
7611 canon_argtypes = chainon (canon_argtypes, void_list_node);
7612
7613 /* There is a non-canonical type. */
7614 *any_noncanonical_p = true;
7615 return canon_argtypes;
7616 }
7617
7618 /* The canonical argument types are the same as ARGTYPES. */
7619 return argtypes;
7620 }
7621
7622 /* Construct, lay out and return
7623 the type of functions returning type VALUE_TYPE
7624 given arguments of types ARG_TYPES.
7625 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7626 are data type nodes for the arguments of the function.
7627 NO_NAMED_ARGS_STDARG_P is true if this is a prototyped
7628 variable-arguments function with (...) prototype (no named arguments).
7629 If such a type has already been constructed, reuse it. */
7630
7631 tree
7632 build_function_type (tree value_type, tree arg_types,
7633 bool no_named_args_stdarg_p)
7634 {
7635 tree t;
7636 inchash::hash hstate;
7637 bool any_structural_p, any_noncanonical_p;
7638 tree canon_argtypes;
7639
7640 gcc_assert (arg_types != error_mark_node);
7641
7642 if (TREE_CODE (value_type) == FUNCTION_TYPE)
7643 {
7644 error ("function return type cannot be function");
7645 value_type = integer_type_node;
7646 }
7647
7648 /* Make a node of the sort we want. */
7649 t = make_node (FUNCTION_TYPE);
7650 TREE_TYPE (t) = value_type;
7651 TYPE_ARG_TYPES (t) = arg_types;
7652 if (no_named_args_stdarg_p)
7653 {
7654 gcc_assert (arg_types == NULL_TREE);
7655 TYPE_NO_NAMED_ARGS_STDARG_P (t) = 1;
7656 }
7657
7658 /* Set up the canonical type. */
7659 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7660 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7661 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
7662 &any_structural_p,
7663 &any_noncanonical_p);
7664 /* Set TYPE_STRUCTURAL_EQUALITY_P early. */
7665 if (any_structural_p)
7666 SET_TYPE_STRUCTURAL_EQUALITY (t);
7667
7668 /* If we already have such a type, use the old one. */
7669 hashval_t hash = type_hash_canon_hash (t);
7670 tree probe_type = t;
7671 t = type_hash_canon (hash, t);
7672 if (t != probe_type)
7673 return t;
7674
7675 if (any_structural_p)
7676 gcc_assert (TYPE_STRUCTURAL_EQUALITY_P (t));
7677 else if (any_noncanonical_p)
7678 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
7679 canon_argtypes);
7680
7681 if (!COMPLETE_TYPE_P (t))
7682 layout_type (t);
7683 return t;
7684 }
7685
7686 /* Build a function type. The RETURN_TYPE is the type returned by the
7687 function. If VAARGS is set, no void_type_node is appended to the
7688 list. ARGP must be always be terminated be a NULL_TREE. */
7689
7690 static tree
7691 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
7692 {
7693 tree t, args, last;
7694
7695 t = va_arg (argp, tree);
7696 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
7697 args = tree_cons (NULL_TREE, t, args);
7698
7699 if (vaargs)
7700 {
7701 last = args;
7702 if (args != NULL_TREE)
7703 args = nreverse (args);
7704 gcc_assert (last != void_list_node);
7705 }
7706 else if (args == NULL_TREE)
7707 args = void_list_node;
7708 else
7709 {
7710 last = args;
7711 args = nreverse (args);
7712 TREE_CHAIN (last) = void_list_node;
7713 }
7714 args = build_function_type (return_type, args, vaargs && args == NULL_TREE);
7715
7716 return args;
7717 }
7718
7719 /* Build a function type. The RETURN_TYPE is the type returned by the
7720 function. If additional arguments are provided, they are
7721 additional argument types. The list of argument types must always
7722 be terminated by NULL_TREE. */
7723
7724 tree
7725 build_function_type_list (tree return_type, ...)
7726 {
7727 tree args;
7728 va_list p;
7729
7730 va_start (p, return_type);
7731 args = build_function_type_list_1 (false, return_type, p);
7732 va_end (p);
7733 return args;
7734 }
7735
7736 /* Build a variable argument function type. The RETURN_TYPE is the
7737 type returned by the function. If additional arguments are provided,
7738 they are additional argument types. The list of argument types must
7739 always be terminated by NULL_TREE. */
7740
7741 tree
7742 build_varargs_function_type_list (tree return_type, ...)
7743 {
7744 tree args;
7745 va_list p;
7746
7747 va_start (p, return_type);
7748 args = build_function_type_list_1 (true, return_type, p);
7749 va_end (p);
7750
7751 return args;
7752 }
7753
7754 /* Build a function type. RETURN_TYPE is the type returned by the
7755 function; VAARGS indicates whether the function takes varargs. The
7756 function takes N named arguments, the types of which are provided in
7757 ARG_TYPES. */
7758
7759 static tree
7760 build_function_type_array_1 (bool vaargs, tree return_type, int n,
7761 tree *arg_types)
7762 {
7763 int i;
7764 tree t = vaargs ? NULL_TREE : void_list_node;
7765
7766 for (i = n - 1; i >= 0; i--)
7767 t = tree_cons (NULL_TREE, arg_types[i], t);
7768
7769 return build_function_type (return_type, t, vaargs && n == 0);
7770 }
7771
7772 /* Build a function type. RETURN_TYPE is the type returned by the
7773 function. The function takes N named arguments, the types of which
7774 are provided in ARG_TYPES. */
7775
7776 tree
7777 build_function_type_array (tree return_type, int n, tree *arg_types)
7778 {
7779 return build_function_type_array_1 (false, return_type, n, arg_types);
7780 }
7781
7782 /* Build a variable argument function type. RETURN_TYPE is the type
7783 returned by the function. The function takes N named arguments, the
7784 types of which are provided in ARG_TYPES. */
7785
7786 tree
7787 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
7788 {
7789 return build_function_type_array_1 (true, return_type, n, arg_types);
7790 }
7791
7792 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
7793 and ARGTYPES (a TREE_LIST) are the return type and arguments types
7794 for the method. An implicit additional parameter (of type
7795 pointer-to-BASETYPE) is added to the ARGTYPES. */
7796
7797 tree
7798 build_method_type_directly (tree basetype,
7799 tree rettype,
7800 tree argtypes)
7801 {
7802 tree t;
7803 tree ptype;
7804 bool any_structural_p, any_noncanonical_p;
7805 tree canon_argtypes;
7806
7807 /* Make a node of the sort we want. */
7808 t = make_node (METHOD_TYPE);
7809
7810 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7811 TREE_TYPE (t) = rettype;
7812 ptype = build_pointer_type (basetype);
7813
7814 /* The actual arglist for this function includes a "hidden" argument
7815 which is "this". Put it into the list of argument types. */
7816 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
7817 TYPE_ARG_TYPES (t) = argtypes;
7818
7819 /* Set up the canonical type. */
7820 any_structural_p
7821 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7822 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
7823 any_noncanonical_p
7824 = (TYPE_CANONICAL (basetype) != basetype
7825 || TYPE_CANONICAL (rettype) != rettype);
7826 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
7827 &any_structural_p,
7828 &any_noncanonical_p);
7829
7830 /* Set TYPE_STRUCTURAL_EQUALITY_P early. */
7831 if (any_structural_p)
7832 SET_TYPE_STRUCTURAL_EQUALITY (t);
7833
7834 /* If we already have such a type, use the old one. */
7835 hashval_t hash = type_hash_canon_hash (t);
7836 tree probe_type = t;
7837 t = type_hash_canon (hash, t);
7838 if (t != probe_type)
7839 return t;
7840
7841 if (any_structural_p)
7842 gcc_assert (TYPE_STRUCTURAL_EQUALITY_P (t));
7843 else if (any_noncanonical_p)
7844 TYPE_CANONICAL (t)
7845 = build_method_type_directly (TYPE_CANONICAL (basetype),
7846 TYPE_CANONICAL (rettype),
7847 canon_argtypes);
7848 if (!COMPLETE_TYPE_P (t))
7849 layout_type (t);
7850
7851 return t;
7852 }
7853
7854 /* Construct, lay out and return the type of methods belonging to class
7855 BASETYPE and whose arguments and values are described by TYPE.
7856 If that type exists already, reuse it.
7857 TYPE must be a FUNCTION_TYPE node. */
7858
7859 tree
7860 build_method_type (tree basetype, tree type)
7861 {
7862 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
7863
7864 return build_method_type_directly (basetype,
7865 TREE_TYPE (type),
7866 TYPE_ARG_TYPES (type));
7867 }
7868
7869 /* Construct, lay out and return the type of offsets to a value
7870 of type TYPE, within an object of type BASETYPE.
7871 If a suitable offset type exists already, reuse it. */
7872
7873 tree
7874 build_offset_type (tree basetype, tree type)
7875 {
7876 tree t;
7877
7878 /* Make a node of the sort we want. */
7879 t = make_node (OFFSET_TYPE);
7880
7881 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7882 TREE_TYPE (t) = type;
7883 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7884 || TYPE_STRUCTURAL_EQUALITY_P (type))
7885 SET_TYPE_STRUCTURAL_EQUALITY (t);
7886
7887 /* If we already have such a type, use the old one. */
7888 hashval_t hash = type_hash_canon_hash (t);
7889 tree probe_type = t;
7890 t = type_hash_canon (hash, t);
7891 if (t != probe_type)
7892 return t;
7893
7894 if (!COMPLETE_TYPE_P (t))
7895 layout_type (t);
7896
7897 if (TYPE_CANONICAL (t) == t)
7898 {
7899 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7900 || TYPE_STRUCTURAL_EQUALITY_P (type))
7901 gcc_unreachable ();
7902 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
7903 || TYPE_CANONICAL (type) != type)
7904 TYPE_CANONICAL (t)
7905 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
7906 TYPE_CANONICAL (type));
7907 }
7908
7909 return t;
7910 }
7911
7912 /* Create a complex type whose components are COMPONENT_TYPE.
7913
7914 If NAMED is true, the type is given a TYPE_NAME. We do not always
7915 do so because this creates a DECL node and thus make the DECL_UIDs
7916 dependent on the type canonicalization hashtable, which is GC-ed,
7917 so the DECL_UIDs would not be stable wrt garbage collection. */
7918
7919 tree
7920 build_complex_type (tree component_type, bool named)
7921 {
7922 gcc_assert (INTEGRAL_TYPE_P (component_type)
7923 || SCALAR_FLOAT_TYPE_P (component_type)
7924 || FIXED_POINT_TYPE_P (component_type));
7925
7926 /* Make a node of the sort we want. */
7927 tree probe = make_node (COMPLEX_TYPE);
7928
7929 TREE_TYPE (probe) = TYPE_MAIN_VARIANT (component_type);
7930 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (probe)))
7931 SET_TYPE_STRUCTURAL_EQUALITY (probe);
7932
7933 /* If we already have such a type, use the old one. */
7934 hashval_t hash = type_hash_canon_hash (probe);
7935 tree t = type_hash_canon (hash, probe);
7936
7937 if (t == probe)
7938 {
7939 /* We created a new type. The hash insertion will have laid
7940 out the type. We need to check the canonicalization and
7941 maybe set the name. */
7942 gcc_checking_assert (COMPLETE_TYPE_P (t)
7943 && !TYPE_NAME (t));
7944
7945 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (t)))
7946 ;
7947 else if (TYPE_CANONICAL (TREE_TYPE (t)) != TREE_TYPE (t))
7948 TYPE_CANONICAL (t)
7949 = build_complex_type (TYPE_CANONICAL (TREE_TYPE (t)), named);
7950
7951 /* We need to create a name, since complex is a fundamental type. */
7952 if (named)
7953 {
7954 const char *name = NULL;
7955
7956 if (TREE_TYPE (t) == char_type_node)
7957 name = "complex char";
7958 else if (TREE_TYPE (t) == signed_char_type_node)
7959 name = "complex signed char";
7960 else if (TREE_TYPE (t) == unsigned_char_type_node)
7961 name = "complex unsigned char";
7962 else if (TREE_TYPE (t) == short_integer_type_node)
7963 name = "complex short int";
7964 else if (TREE_TYPE (t) == short_unsigned_type_node)
7965 name = "complex short unsigned int";
7966 else if (TREE_TYPE (t) == integer_type_node)
7967 name = "complex int";
7968 else if (TREE_TYPE (t) == unsigned_type_node)
7969 name = "complex unsigned int";
7970 else if (TREE_TYPE (t) == long_integer_type_node)
7971 name = "complex long int";
7972 else if (TREE_TYPE (t) == long_unsigned_type_node)
7973 name = "complex long unsigned int";
7974 else if (TREE_TYPE (t) == long_long_integer_type_node)
7975 name = "complex long long int";
7976 else if (TREE_TYPE (t) == long_long_unsigned_type_node)
7977 name = "complex long long unsigned int";
7978
7979 if (name != NULL)
7980 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
7981 get_identifier (name), t);
7982 }
7983 }
7984
7985 return build_qualified_type (t, TYPE_QUALS (component_type));
7986 }
7987
7988 /* If TYPE is a real or complex floating-point type and the target
7989 does not directly support arithmetic on TYPE then return the wider
7990 type to be used for arithmetic on TYPE. Otherwise, return
7991 NULL_TREE. */
7992
7993 tree
7994 excess_precision_type (tree type)
7995 {
7996 /* The target can give two different responses to the question of
7997 which excess precision mode it would like depending on whether we
7998 are in -fexcess-precision=standard or -fexcess-precision=fast. */
7999
8000 enum excess_precision_type requested_type
8001 = (flag_excess_precision == EXCESS_PRECISION_FAST
8002 ? EXCESS_PRECISION_TYPE_FAST
8003 : (flag_excess_precision == EXCESS_PRECISION_FLOAT16
8004 ? EXCESS_PRECISION_TYPE_FLOAT16 : EXCESS_PRECISION_TYPE_STANDARD));
8005
8006 enum flt_eval_method target_flt_eval_method
8007 = targetm.c.excess_precision (requested_type);
8008
8009 /* The target should not ask for unpredictable float evaluation (though
8010 it might advertise that implicitly the evaluation is unpredictable,
8011 but we don't care about that here, it will have been reported
8012 elsewhere). If it does ask for unpredictable evaluation, we have
8013 nothing to do here. */
8014 gcc_assert (target_flt_eval_method != FLT_EVAL_METHOD_UNPREDICTABLE);
8015
8016 /* Nothing to do. The target has asked for all types we know about
8017 to be computed with their native precision and range. */
8018 if (target_flt_eval_method == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
8019 return NULL_TREE;
8020
8021 /* The target will promote this type in a target-dependent way, so excess
8022 precision ought to leave it alone. */
8023 if (targetm.promoted_type (type) != NULL_TREE)
8024 return NULL_TREE;
8025
8026 machine_mode float16_type_mode = (float16_type_node
8027 ? TYPE_MODE (float16_type_node)
8028 : VOIDmode);
8029 machine_mode bfloat16_type_mode = (bfloat16_type_node
8030 ? TYPE_MODE (bfloat16_type_node)
8031 : VOIDmode);
8032 machine_mode float_type_mode = TYPE_MODE (float_type_node);
8033 machine_mode double_type_mode = TYPE_MODE (double_type_node);
8034
8035 switch (TREE_CODE (type))
8036 {
8037 case REAL_TYPE:
8038 {
8039 machine_mode type_mode = TYPE_MODE (type);
8040 switch (target_flt_eval_method)
8041 {
8042 case FLT_EVAL_METHOD_PROMOTE_TO_FLOAT:
8043 if (type_mode == float16_type_mode
8044 || type_mode == bfloat16_type_mode)
8045 return float_type_node;
8046 break;
8047 case FLT_EVAL_METHOD_PROMOTE_TO_DOUBLE:
8048 if (type_mode == float16_type_mode
8049 || type_mode == bfloat16_type_mode
8050 || type_mode == float_type_mode)
8051 return double_type_node;
8052 break;
8053 case FLT_EVAL_METHOD_PROMOTE_TO_LONG_DOUBLE:
8054 if (type_mode == float16_type_mode
8055 || type_mode == bfloat16_type_mode
8056 || type_mode == float_type_mode
8057 || type_mode == double_type_mode)
8058 return long_double_type_node;
8059 break;
8060 default:
8061 gcc_unreachable ();
8062 }
8063 break;
8064 }
8065 case COMPLEX_TYPE:
8066 {
8067 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8068 return NULL_TREE;
8069 machine_mode type_mode = TYPE_MODE (TREE_TYPE (type));
8070 switch (target_flt_eval_method)
8071 {
8072 case FLT_EVAL_METHOD_PROMOTE_TO_FLOAT:
8073 if (type_mode == float16_type_mode
8074 || type_mode == bfloat16_type_mode)
8075 return complex_float_type_node;
8076 break;
8077 case FLT_EVAL_METHOD_PROMOTE_TO_DOUBLE:
8078 if (type_mode == float16_type_mode
8079 || type_mode == bfloat16_type_mode
8080 || type_mode == float_type_mode)
8081 return complex_double_type_node;
8082 break;
8083 case FLT_EVAL_METHOD_PROMOTE_TO_LONG_DOUBLE:
8084 if (type_mode == float16_type_mode
8085 || type_mode == bfloat16_type_mode
8086 || type_mode == float_type_mode
8087 || type_mode == double_type_mode)
8088 return complex_long_double_type_node;
8089 break;
8090 default:
8091 gcc_unreachable ();
8092 }
8093 break;
8094 }
8095 default:
8096 break;
8097 }
8098
8099 return NULL_TREE;
8100 }
8101
8102 /* Return OP, stripped of any conversions to wider types as much as is safe.
8103 Converting the value back to OP's type makes a value equivalent to OP.
8104
8105 If FOR_TYPE is nonzero, we return a value which, if converted to
8106 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8107
8108 OP must have integer, real or enumeral type. Pointers are not allowed!
8109
8110 There are some cases where the obvious value we could return
8111 would regenerate to OP if converted to OP's type,
8112 but would not extend like OP to wider types.
8113 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8114 For example, if OP is (unsigned short)(signed char)-1,
8115 we avoid returning (signed char)-1 if FOR_TYPE is int,
8116 even though extending that to an unsigned short would regenerate OP,
8117 since the result of extending (signed char)-1 to (int)
8118 is different from (int) OP. */
8119
8120 tree
8121 get_unwidened (tree op, tree for_type)
8122 {
8123 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8124 tree type = TREE_TYPE (op);
8125 unsigned final_prec
8126 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8127 int uns
8128 = (for_type != 0 && for_type != type
8129 && final_prec > TYPE_PRECISION (type)
8130 && TYPE_UNSIGNED (type));
8131 tree win = op;
8132
8133 while (CONVERT_EXPR_P (op))
8134 {
8135 int bitschange;
8136
8137 /* TYPE_PRECISION on vector types has different meaning
8138 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8139 so avoid them here. */
8140 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8141 break;
8142
8143 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8144 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8145
8146 /* Truncations are many-one so cannot be removed.
8147 Unless we are later going to truncate down even farther. */
8148 if (bitschange < 0
8149 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8150 break;
8151
8152 /* See what's inside this conversion. If we decide to strip it,
8153 we will set WIN. */
8154 op = TREE_OPERAND (op, 0);
8155
8156 /* If we have not stripped any zero-extensions (uns is 0),
8157 we can strip any kind of extension.
8158 If we have previously stripped a zero-extension,
8159 only zero-extensions can safely be stripped.
8160 Any extension can be stripped if the bits it would produce
8161 are all going to be discarded later by truncating to FOR_TYPE. */
8162
8163 if (bitschange > 0)
8164 {
8165 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8166 win = op;
8167 /* TYPE_UNSIGNED says whether this is a zero-extension.
8168 Let's avoid computing it if it does not affect WIN
8169 and if UNS will not be needed again. */
8170 if ((uns
8171 || CONVERT_EXPR_P (op))
8172 && TYPE_UNSIGNED (TREE_TYPE (op)))
8173 {
8174 uns = 1;
8175 win = op;
8176 }
8177 }
8178 }
8179
8180 /* If we finally reach a constant see if it fits in sth smaller and
8181 in that case convert it. */
8182 if (TREE_CODE (win) == INTEGER_CST)
8183 {
8184 tree wtype = TREE_TYPE (win);
8185 unsigned prec = wi::min_precision (wi::to_wide (win), TYPE_SIGN (wtype));
8186 if (for_type)
8187 prec = MAX (prec, final_prec);
8188 if (prec < TYPE_PRECISION (wtype))
8189 {
8190 tree t = lang_hooks.types.type_for_size (prec, TYPE_UNSIGNED (wtype));
8191 if (t && TYPE_PRECISION (t) < TYPE_PRECISION (wtype))
8192 win = fold_convert (t, win);
8193 }
8194 }
8195
8196 return win;
8197 }
8198
8199 /* Return OP or a simpler expression for a narrower value
8200 which can be sign-extended or zero-extended to give back OP.
8201 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8202 or 0 if the value should be sign-extended. */
8203
8204 tree
8205 get_narrower (tree op, int *unsignedp_ptr)
8206 {
8207 int uns = 0;
8208 bool first = true;
8209 tree win = op;
8210 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8211
8212 if (TREE_CODE (op) == COMPOUND_EXPR)
8213 {
8214 do
8215 op = TREE_OPERAND (op, 1);
8216 while (TREE_CODE (op) == COMPOUND_EXPR);
8217 tree ret = get_narrower (op, unsignedp_ptr);
8218 if (ret == op)
8219 return win;
8220 auto_vec <tree, 16> v;
8221 unsigned int i;
8222 for (op = win; TREE_CODE (op) == COMPOUND_EXPR;
8223 op = TREE_OPERAND (op, 1))
8224 v.safe_push (op);
8225 FOR_EACH_VEC_ELT_REVERSE (v, i, op)
8226 ret = build2_loc (EXPR_LOCATION (op), COMPOUND_EXPR,
8227 TREE_TYPE (ret), TREE_OPERAND (op, 0),
8228 ret);
8229 return ret;
8230 }
8231 while (TREE_CODE (op) == NOP_EXPR)
8232 {
8233 int bitschange
8234 = (TYPE_PRECISION (TREE_TYPE (op))
8235 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8236
8237 /* Truncations are many-one so cannot be removed. */
8238 if (bitschange < 0)
8239 break;
8240
8241 /* See what's inside this conversion. If we decide to strip it,
8242 we will set WIN. */
8243
8244 if (bitschange > 0)
8245 {
8246 op = TREE_OPERAND (op, 0);
8247 /* An extension: the outermost one can be stripped,
8248 but remember whether it is zero or sign extension. */
8249 if (first)
8250 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8251 /* Otherwise, if a sign extension has been stripped,
8252 only sign extensions can now be stripped;
8253 if a zero extension has been stripped, only zero-extensions. */
8254 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8255 break;
8256 first = false;
8257 }
8258 else /* bitschange == 0 */
8259 {
8260 /* A change in nominal type can always be stripped, but we must
8261 preserve the unsignedness. */
8262 if (first)
8263 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8264 first = false;
8265 op = TREE_OPERAND (op, 0);
8266 /* Keep trying to narrow, but don't assign op to win if it
8267 would turn an integral type into something else. */
8268 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8269 continue;
8270 }
8271
8272 win = op;
8273 }
8274
8275 if (TREE_CODE (op) == COMPONENT_REF
8276 /* Since type_for_size always gives an integer type. */
8277 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8278 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8279 /* Ensure field is laid out already. */
8280 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8281 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
8282 {
8283 unsigned HOST_WIDE_INT innerprec
8284 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
8285 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8286 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8287 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8288
8289 /* We can get this structure field in a narrower type that fits it,
8290 but the resulting extension to its nominal type (a fullword type)
8291 must satisfy the same conditions as for other extensions.
8292
8293 Do this only for fields that are aligned (not bit-fields),
8294 because when bit-field insns will be used there is no
8295 advantage in doing this. */
8296
8297 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8298 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8299 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8300 && type != 0)
8301 {
8302 if (first)
8303 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8304 win = fold_convert (type, op);
8305 }
8306 }
8307
8308 *unsignedp_ptr = uns;
8309 return win;
8310 }
8311
8312 /* Return true if integer constant C has a value that is permissible
8313 for TYPE, an integral type. */
8314
8315 bool
8316 int_fits_type_p (const_tree c, const_tree type)
8317 {
8318 tree type_low_bound, type_high_bound;
8319 bool ok_for_low_bound, ok_for_high_bound;
8320 signop sgn_c = TYPE_SIGN (TREE_TYPE (c));
8321
8322 /* Non-standard boolean types can have arbitrary precision but various
8323 transformations assume that they can only take values 0 and +/-1. */
8324 if (TREE_CODE (type) == BOOLEAN_TYPE)
8325 return wi::fits_to_boolean_p (wi::to_wide (c), type);
8326
8327 retry:
8328 type_low_bound = TYPE_MIN_VALUE (type);
8329 type_high_bound = TYPE_MAX_VALUE (type);
8330
8331 /* If at least one bound of the type is a constant integer, we can check
8332 ourselves and maybe make a decision. If no such decision is possible, but
8333 this type is a subtype, try checking against that. Otherwise, use
8334 fits_to_tree_p, which checks against the precision.
8335
8336 Compute the status for each possibly constant bound, and return if we see
8337 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8338 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8339 for "constant known to fit". */
8340
8341 /* Check if c >= type_low_bound. */
8342 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8343 {
8344 if (tree_int_cst_lt (c, type_low_bound))
8345 return false;
8346 ok_for_low_bound = true;
8347 }
8348 else
8349 ok_for_low_bound = false;
8350
8351 /* Check if c <= type_high_bound. */
8352 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8353 {
8354 if (tree_int_cst_lt (type_high_bound, c))
8355 return false;
8356 ok_for_high_bound = true;
8357 }
8358 else
8359 ok_for_high_bound = false;
8360
8361 /* If the constant fits both bounds, the result is known. */
8362 if (ok_for_low_bound && ok_for_high_bound)
8363 return true;
8364
8365 /* Perform some generic filtering which may allow making a decision
8366 even if the bounds are not constant. First, negative integers
8367 never fit in unsigned types, */
8368 if (TYPE_UNSIGNED (type) && sgn_c == SIGNED && wi::neg_p (wi::to_wide (c)))
8369 return false;
8370
8371 /* Second, narrower types always fit in wider ones. */
8372 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8373 return true;
8374
8375 /* Third, unsigned integers with top bit set never fit signed types. */
8376 if (!TYPE_UNSIGNED (type) && sgn_c == UNSIGNED)
8377 {
8378 int prec = GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (TREE_TYPE (c))) - 1;
8379 if (prec < TYPE_PRECISION (TREE_TYPE (c)))
8380 {
8381 /* When a tree_cst is converted to a wide-int, the precision
8382 is taken from the type. However, if the precision of the
8383 mode underneath the type is smaller than that, it is
8384 possible that the value will not fit. The test below
8385 fails if any bit is set between the sign bit of the
8386 underlying mode and the top bit of the type. */
8387 if (wi::zext (wi::to_wide (c), prec - 1) != wi::to_wide (c))
8388 return false;
8389 }
8390 else if (wi::neg_p (wi::to_wide (c)))
8391 return false;
8392 }
8393
8394 /* If we haven't been able to decide at this point, there nothing more we
8395 can check ourselves here. Look at the base type if we have one and it
8396 has the same precision. */
8397 if (TREE_CODE (type) == INTEGER_TYPE
8398 && TREE_TYPE (type) != 0
8399 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8400 {
8401 type = TREE_TYPE (type);
8402 goto retry;
8403 }
8404
8405 /* Or to fits_to_tree_p, if nothing else. */
8406 return wi::fits_to_tree_p (wi::to_wide (c), type);
8407 }
8408
8409 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8410 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8411 represented (assuming two's-complement arithmetic) within the bit
8412 precision of the type are returned instead. */
8413
8414 void
8415 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8416 {
8417 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8418 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8419 wi::to_mpz (wi::to_wide (TYPE_MIN_VALUE (type)), min, TYPE_SIGN (type));
8420 else
8421 {
8422 if (TYPE_UNSIGNED (type))
8423 mpz_set_ui (min, 0);
8424 else
8425 {
8426 wide_int mn = wi::min_value (TYPE_PRECISION (type), SIGNED);
8427 wi::to_mpz (mn, min, SIGNED);
8428 }
8429 }
8430
8431 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8432 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8433 wi::to_mpz (wi::to_wide (TYPE_MAX_VALUE (type)), max, TYPE_SIGN (type));
8434 else
8435 {
8436 wide_int mn = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
8437 wi::to_mpz (mn, max, TYPE_SIGN (type));
8438 }
8439 }
8440
8441 /* Return true if VAR is an automatic variable. */
8442
8443 bool
8444 auto_var_p (const_tree var)
8445 {
8446 return ((((VAR_P (var) && ! DECL_EXTERNAL (var))
8447 || TREE_CODE (var) == PARM_DECL)
8448 && ! TREE_STATIC (var))
8449 || TREE_CODE (var) == RESULT_DECL);
8450 }
8451
8452 /* Return true if VAR is an automatic variable defined in function FN. */
8453
8454 bool
8455 auto_var_in_fn_p (const_tree var, const_tree fn)
8456 {
8457 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8458 && (auto_var_p (var)
8459 || TREE_CODE (var) == LABEL_DECL));
8460 }
8461
8462 /* Subprogram of following function. Called by walk_tree.
8463
8464 Return *TP if it is an automatic variable or parameter of the
8465 function passed in as DATA. */
8466
8467 static tree
8468 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8469 {
8470 tree fn = (tree) data;
8471
8472 if (TYPE_P (*tp))
8473 *walk_subtrees = 0;
8474
8475 else if (DECL_P (*tp)
8476 && auto_var_in_fn_p (*tp, fn))
8477 return *tp;
8478
8479 return NULL_TREE;
8480 }
8481
8482 /* Returns true if T is, contains, or refers to a type with variable
8483 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8484 arguments, but not the return type. If FN is nonzero, only return
8485 true if a modifier of the type or position of FN is a variable or
8486 parameter inside FN.
8487
8488 This concept is more general than that of C99 'variably modified types':
8489 in C99, a struct type is never variably modified because a VLA may not
8490 appear as a structure member. However, in GNU C code like:
8491
8492 struct S { int i[f()]; };
8493
8494 is valid, and other languages may define similar constructs. */
8495
8496 bool
8497 variably_modified_type_p (tree type, tree fn)
8498 {
8499 tree t;
8500
8501 /* Test if T is either variable (if FN is zero) or an expression containing
8502 a variable in FN. If TYPE isn't gimplified, return true also if
8503 gimplify_one_sizepos would gimplify the expression into a local
8504 variable. */
8505 #define RETURN_TRUE_IF_VAR(T) \
8506 do { tree _t = (T); \
8507 if (_t != NULL_TREE \
8508 && _t != error_mark_node \
8509 && !CONSTANT_CLASS_P (_t) \
8510 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8511 && (!fn \
8512 || (!TYPE_SIZES_GIMPLIFIED (type) \
8513 && (TREE_CODE (_t) != VAR_DECL \
8514 && !CONTAINS_PLACEHOLDER_P (_t))) \
8515 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8516 return true; } while (0)
8517
8518 if (type == error_mark_node)
8519 return false;
8520
8521 /* If TYPE itself has variable size, it is variably modified. */
8522 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8523 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8524
8525 switch (TREE_CODE (type))
8526 {
8527 case POINTER_TYPE:
8528 case REFERENCE_TYPE:
8529 case VECTOR_TYPE:
8530 /* Ada can have pointer types refering to themselves indirectly. */
8531 if (TREE_VISITED (type))
8532 return false;
8533 TREE_VISITED (type) = true;
8534 if (variably_modified_type_p (TREE_TYPE (type), fn))
8535 {
8536 TREE_VISITED (type) = false;
8537 return true;
8538 }
8539 TREE_VISITED (type) = false;
8540 break;
8541
8542 case FUNCTION_TYPE:
8543 case METHOD_TYPE:
8544 /* If TYPE is a function type, it is variably modified if the
8545 return type is variably modified. */
8546 if (variably_modified_type_p (TREE_TYPE (type), fn))
8547 return true;
8548 break;
8549
8550 case INTEGER_TYPE:
8551 case REAL_TYPE:
8552 case FIXED_POINT_TYPE:
8553 case ENUMERAL_TYPE:
8554 case BOOLEAN_TYPE:
8555 /* Scalar types are variably modified if their end points
8556 aren't constant. */
8557 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8558 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8559 break;
8560
8561 case RECORD_TYPE:
8562 case UNION_TYPE:
8563 case QUAL_UNION_TYPE:
8564 /* We can't see if any of the fields are variably-modified by the
8565 definition we normally use, since that would produce infinite
8566 recursion via pointers. */
8567 /* This is variably modified if some field's type is. */
8568 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8569 if (TREE_CODE (t) == FIELD_DECL)
8570 {
8571 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8572 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8573 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8574
8575 /* If the type is a qualified union, then the DECL_QUALIFIER
8576 of fields can also be an expression containing a variable. */
8577 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8578 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8579
8580 /* If the field is a qualified union, then it's only a container
8581 for what's inside so we look into it. That's necessary in LTO
8582 mode because the sizes of the field tested above have been set
8583 to PLACEHOLDER_EXPRs by free_lang_data. */
8584 if (TREE_CODE (TREE_TYPE (t)) == QUAL_UNION_TYPE
8585 && variably_modified_type_p (TREE_TYPE (t), fn))
8586 return true;
8587 }
8588 break;
8589
8590 case ARRAY_TYPE:
8591 /* Do not call ourselves to avoid infinite recursion. This is
8592 variably modified if the element type is. */
8593 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8594 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8595 break;
8596
8597 default:
8598 break;
8599 }
8600
8601 /* The current language may have other cases to check, but in general,
8602 all other types are not variably modified. */
8603 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8604
8605 #undef RETURN_TRUE_IF_VAR
8606 }
8607
8608 /* Given a DECL or TYPE, return the scope in which it was declared, or
8609 NULL_TREE if there is no containing scope. */
8610
8611 tree
8612 get_containing_scope (const_tree t)
8613 {
8614 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8615 }
8616
8617 /* Returns the ultimate TRANSLATION_UNIT_DECL context of DECL or NULL. */
8618
8619 const_tree
8620 get_ultimate_context (const_tree decl)
8621 {
8622 while (decl && TREE_CODE (decl) != TRANSLATION_UNIT_DECL)
8623 {
8624 if (TREE_CODE (decl) == BLOCK)
8625 decl = BLOCK_SUPERCONTEXT (decl);
8626 else
8627 decl = get_containing_scope (decl);
8628 }
8629 return decl;
8630 }
8631
8632 /* Return the innermost context enclosing DECL that is
8633 a FUNCTION_DECL, or zero if none. */
8634
8635 tree
8636 decl_function_context (const_tree decl)
8637 {
8638 tree context;
8639
8640 if (TREE_CODE (decl) == ERROR_MARK)
8641 return 0;
8642
8643 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8644 where we look up the function at runtime. Such functions always take
8645 a first argument of type 'pointer to real context'.
8646
8647 C++ should really be fixed to use DECL_CONTEXT for the real context,
8648 and use something else for the "virtual context". */
8649 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
8650 context
8651 = TYPE_MAIN_VARIANT
8652 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8653 else
8654 context = DECL_CONTEXT (decl);
8655
8656 while (context && TREE_CODE (context) != FUNCTION_DECL)
8657 {
8658 if (TREE_CODE (context) == BLOCK)
8659 context = BLOCK_SUPERCONTEXT (context);
8660 else
8661 context = get_containing_scope (context);
8662 }
8663
8664 return context;
8665 }
8666
8667 /* Return the innermost context enclosing DECL that is
8668 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8669 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8670
8671 tree
8672 decl_type_context (const_tree decl)
8673 {
8674 tree context = DECL_CONTEXT (decl);
8675
8676 while (context)
8677 switch (TREE_CODE (context))
8678 {
8679 case NAMESPACE_DECL:
8680 case TRANSLATION_UNIT_DECL:
8681 return NULL_TREE;
8682
8683 case RECORD_TYPE:
8684 case UNION_TYPE:
8685 case QUAL_UNION_TYPE:
8686 return context;
8687
8688 case TYPE_DECL:
8689 case FUNCTION_DECL:
8690 context = DECL_CONTEXT (context);
8691 break;
8692
8693 case BLOCK:
8694 context = BLOCK_SUPERCONTEXT (context);
8695 break;
8696
8697 default:
8698 gcc_unreachable ();
8699 }
8700
8701 return NULL_TREE;
8702 }
8703
8704 /* CALL is a CALL_EXPR. Return the declaration for the function
8705 called, or NULL_TREE if the called function cannot be
8706 determined. */
8707
8708 tree
8709 get_callee_fndecl (const_tree call)
8710 {
8711 tree addr;
8712
8713 if (call == error_mark_node)
8714 return error_mark_node;
8715
8716 /* It's invalid to call this function with anything but a
8717 CALL_EXPR. */
8718 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8719
8720 /* The first operand to the CALL is the address of the function
8721 called. */
8722 addr = CALL_EXPR_FN (call);
8723
8724 /* If there is no function, return early. */
8725 if (addr == NULL_TREE)
8726 return NULL_TREE;
8727
8728 STRIP_NOPS (addr);
8729
8730 /* If this is a readonly function pointer, extract its initial value. */
8731 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8732 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8733 && DECL_INITIAL (addr))
8734 addr = DECL_INITIAL (addr);
8735
8736 /* If the address is just `&f' for some function `f', then we know
8737 that `f' is being called. */
8738 if (TREE_CODE (addr) == ADDR_EXPR
8739 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8740 return TREE_OPERAND (addr, 0);
8741
8742 /* We couldn't figure out what was being called. */
8743 return NULL_TREE;
8744 }
8745
8746 /* Return true when STMTs arguments and return value match those of FNDECL,
8747 a decl of a builtin function. */
8748
8749 static bool
8750 tree_builtin_call_types_compatible_p (const_tree call, tree fndecl)
8751 {
8752 gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) != NOT_BUILT_IN);
8753
8754 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
8755 if (tree decl = builtin_decl_explicit (DECL_FUNCTION_CODE (fndecl)))
8756 fndecl = decl;
8757
8758 bool gimple_form = (cfun && (cfun->curr_properties & PROP_gimple)) != 0;
8759 if (gimple_form
8760 ? !useless_type_conversion_p (TREE_TYPE (call),
8761 TREE_TYPE (TREE_TYPE (fndecl)))
8762 : (TYPE_MAIN_VARIANT (TREE_TYPE (call))
8763 != TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))))
8764 return false;
8765
8766 tree targs = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
8767 unsigned nargs = call_expr_nargs (call);
8768 for (unsigned i = 0; i < nargs; ++i, targs = TREE_CHAIN (targs))
8769 {
8770 /* Variadic args follow. */
8771 if (!targs)
8772 return true;
8773 tree arg = CALL_EXPR_ARG (call, i);
8774 tree type = TREE_VALUE (targs);
8775 if (gimple_form
8776 ? !useless_type_conversion_p (type, TREE_TYPE (arg))
8777 : TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (TREE_TYPE (arg)))
8778 {
8779 /* For pointer arguments be more forgiving, e.g. due to
8780 FILE * vs. fileptr_type_node, or say char * vs. const char *
8781 differences etc. */
8782 if (!gimple_form
8783 && POINTER_TYPE_P (type)
8784 && POINTER_TYPE_P (TREE_TYPE (arg))
8785 && tree_nop_conversion_p (type, TREE_TYPE (arg)))
8786 continue;
8787 /* char/short integral arguments are promoted to int
8788 by several frontends if targetm.calls.promote_prototypes
8789 is true. Allow such promotion too. */
8790 if (INTEGRAL_TYPE_P (type)
8791 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
8792 && INTEGRAL_TYPE_P (TREE_TYPE (arg))
8793 && !TYPE_UNSIGNED (TREE_TYPE (arg))
8794 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl))
8795 && (gimple_form
8796 ? useless_type_conversion_p (integer_type_node,
8797 TREE_TYPE (arg))
8798 : tree_nop_conversion_p (integer_type_node,
8799 TREE_TYPE (arg))))
8800 continue;
8801 return false;
8802 }
8803 }
8804 if (targs && !VOID_TYPE_P (TREE_VALUE (targs)))
8805 return false;
8806 return true;
8807 }
8808
8809 /* If CALL_EXPR CALL calls a normal built-in function or an internal function,
8810 return the associated function code, otherwise return CFN_LAST. */
8811
8812 combined_fn
8813 get_call_combined_fn (const_tree call)
8814 {
8815 /* It's invalid to call this function with anything but a CALL_EXPR. */
8816 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8817
8818 if (!CALL_EXPR_FN (call))
8819 return as_combined_fn (CALL_EXPR_IFN (call));
8820
8821 tree fndecl = get_callee_fndecl (call);
8822 if (fndecl
8823 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
8824 && tree_builtin_call_types_compatible_p (call, fndecl))
8825 return as_combined_fn (DECL_FUNCTION_CODE (fndecl));
8826
8827 return CFN_LAST;
8828 }
8829
8830 /* Comparator of indices based on tree_node_counts. */
8831
8832 static int
8833 tree_nodes_cmp (const void *p1, const void *p2)
8834 {
8835 const unsigned *n1 = (const unsigned *)p1;
8836 const unsigned *n2 = (const unsigned *)p2;
8837
8838 return tree_node_counts[*n1] - tree_node_counts[*n2];
8839 }
8840
8841 /* Comparator of indices based on tree_code_counts. */
8842
8843 static int
8844 tree_codes_cmp (const void *p1, const void *p2)
8845 {
8846 const unsigned *n1 = (const unsigned *)p1;
8847 const unsigned *n2 = (const unsigned *)p2;
8848
8849 return tree_code_counts[*n1] - tree_code_counts[*n2];
8850 }
8851
8852 #define TREE_MEM_USAGE_SPACES 40
8853
8854 /* Print debugging information about tree nodes generated during the compile,
8855 and any language-specific information. */
8856
8857 void
8858 dump_tree_statistics (void)
8859 {
8860 if (GATHER_STATISTICS)
8861 {
8862 uint64_t total_nodes, total_bytes;
8863 fprintf (stderr, "\nKind Nodes Bytes\n");
8864 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8865 total_nodes = total_bytes = 0;
8866
8867 {
8868 auto_vec<unsigned> indices (all_kinds);
8869 for (unsigned i = 0; i < all_kinds; i++)
8870 indices.quick_push (i);
8871 indices.qsort (tree_nodes_cmp);
8872
8873 for (unsigned i = 0; i < (int) all_kinds; i++)
8874 {
8875 unsigned j = indices[i];
8876 fprintf (stderr, "%-20s %6" PRIu64 "%c %9" PRIu64 "%c\n",
8877 tree_node_kind_names[j], SIZE_AMOUNT (tree_node_counts[j]),
8878 SIZE_AMOUNT (tree_node_sizes[j]));
8879 total_nodes += tree_node_counts[j];
8880 total_bytes += tree_node_sizes[j];
8881 }
8882 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8883 fprintf (stderr, "%-20s %6" PRIu64 "%c %9" PRIu64 "%c\n", "Total",
8884 SIZE_AMOUNT (total_nodes), SIZE_AMOUNT (total_bytes));
8885 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8886 }
8887
8888 {
8889 fprintf (stderr, "Code Nodes\n");
8890 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8891
8892 auto_vec<unsigned> indices (MAX_TREE_CODES);
8893 for (unsigned i = 0; i < MAX_TREE_CODES; i++)
8894 indices.quick_push (i);
8895 indices.qsort (tree_codes_cmp);
8896
8897 for (unsigned i = 0; i < MAX_TREE_CODES; i++)
8898 {
8899 unsigned j = indices[i];
8900 fprintf (stderr, "%-32s %6" PRIu64 "%c\n",
8901 get_tree_code_name ((enum tree_code) j),
8902 SIZE_AMOUNT (tree_code_counts[j]));
8903 }
8904 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
8905 fprintf (stderr, "\n");
8906 ssanames_print_statistics ();
8907 fprintf (stderr, "\n");
8908 phinodes_print_statistics ();
8909 fprintf (stderr, "\n");
8910 }
8911 }
8912 else
8913 fprintf (stderr, "(No per-node statistics)\n");
8914
8915 print_type_hash_statistics ();
8916 print_debug_expr_statistics ();
8917 print_value_expr_statistics ();
8918 lang_hooks.print_statistics ();
8919 }
8920
8921 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8922
8923 /* Generate a crc32 of the low BYTES bytes of VALUE. */
8924
8925 unsigned
8926 crc32_unsigned_n (unsigned chksum, unsigned value, unsigned bytes)
8927 {
8928 /* This relies on the raw feedback's top 4 bits being zero. */
8929 #define FEEDBACK(X) ((X) * 0x04c11db7)
8930 #define SYNDROME(X) (FEEDBACK ((X) & 1) ^ FEEDBACK ((X) & 2) \
8931 ^ FEEDBACK ((X) & 4) ^ FEEDBACK ((X) & 8))
8932 static const unsigned syndromes[16] =
8933 {
8934 SYNDROME(0x0), SYNDROME(0x1), SYNDROME(0x2), SYNDROME(0x3),
8935 SYNDROME(0x4), SYNDROME(0x5), SYNDROME(0x6), SYNDROME(0x7),
8936 SYNDROME(0x8), SYNDROME(0x9), SYNDROME(0xa), SYNDROME(0xb),
8937 SYNDROME(0xc), SYNDROME(0xd), SYNDROME(0xe), SYNDROME(0xf),
8938 };
8939 #undef FEEDBACK
8940 #undef SYNDROME
8941
8942 value <<= (32 - bytes * 8);
8943 for (unsigned ix = bytes * 2; ix--; value <<= 4)
8944 {
8945 unsigned feedback = syndromes[((value ^ chksum) >> 28) & 0xf];
8946
8947 chksum = (chksum << 4) ^ feedback;
8948 }
8949
8950 return chksum;
8951 }
8952
8953 /* Generate a crc32 of a string. */
8954
8955 unsigned
8956 crc32_string (unsigned chksum, const char *string)
8957 {
8958 do
8959 chksum = crc32_byte (chksum, *string);
8960 while (*string++);
8961 return chksum;
8962 }
8963
8964 /* P is a string that will be used in a symbol. Mask out any characters
8965 that are not valid in that context. */
8966
8967 void
8968 clean_symbol_name (char *p)
8969 {
8970 for (; *p; p++)
8971 if (! (ISALNUM (*p)
8972 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
8973 || *p == '$'
8974 #endif
8975 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
8976 || *p == '.'
8977 #endif
8978 ))
8979 *p = '_';
8980 }
8981
8982 static GTY(()) unsigned anon_cnt = 0; /* Saved for PCH. */
8983
8984 /* Create a unique anonymous identifier. The identifier is still a
8985 valid assembly label. */
8986
8987 tree
8988 make_anon_name ()
8989 {
8990 const char *fmt =
8991 #if !defined (NO_DOT_IN_LABEL)
8992 "."
8993 #elif !defined (NO_DOLLAR_IN_LABEL)
8994 "$"
8995 #else
8996 "_"
8997 #endif
8998 "_anon_%d";
8999
9000 char buf[24];
9001 int len = snprintf (buf, sizeof (buf), fmt, anon_cnt++);
9002 gcc_checking_assert (len < int (sizeof (buf)));
9003
9004 tree id = get_identifier_with_length (buf, len);
9005 IDENTIFIER_ANON_P (id) = true;
9006
9007 return id;
9008 }
9009
9010 /* Generate a name for a special-purpose function.
9011 The generated name may need to be unique across the whole link.
9012 Changes to this function may also require corresponding changes to
9013 xstrdup_mask_random.
9014 TYPE is some string to identify the purpose of this function to the
9015 linker or collect2; it must start with an uppercase letter,
9016 one of:
9017 I - for constructors
9018 D - for destructors
9019 N - for C++ anonymous namespaces
9020 F - for DWARF unwind frame information. */
9021
9022 tree
9023 get_file_function_name (const char *type)
9024 {
9025 char *buf;
9026 const char *p;
9027 char *q;
9028
9029 /* If we already have a name we know to be unique, just use that. */
9030 if (first_global_object_name)
9031 p = q = ASTRDUP (first_global_object_name);
9032 /* If the target is handling the constructors/destructors, they
9033 will be local to this file and the name is only necessary for
9034 debugging purposes.
9035 We also assign sub_I and sub_D sufixes to constructors called from
9036 the global static constructors. These are always local.
9037 OpenMP "declare target" offloaded constructors/destructors use "off_I" and
9038 "off_D" for the same purpose. */
9039 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9040 || ((startswith (type, "sub_") || startswith (type, "off_"))
9041 && (type[4] == 'I' || type[4] == 'D')))
9042 {
9043 const char *file = main_input_filename;
9044 if (! file)
9045 file = LOCATION_FILE (input_location);
9046 /* Just use the file's basename, because the full pathname
9047 might be quite long. */
9048 p = q = ASTRDUP (lbasename (file));
9049 }
9050 else
9051 {
9052 /* Otherwise, the name must be unique across the entire link.
9053 We don't have anything that we know to be unique to this translation
9054 unit, so use what we do have and throw in some randomness. */
9055 unsigned len;
9056 const char *name = weak_global_object_name;
9057 const char *file = main_input_filename;
9058
9059 if (! name)
9060 name = "";
9061 if (! file)
9062 file = LOCATION_FILE (input_location);
9063
9064 len = strlen (file);
9065 q = (char *) alloca (9 + 19 + len + 1);
9066 memcpy (q, file, len + 1);
9067
9068 snprintf (q + len, 9 + 19 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9069 crc32_string (0, name), get_random_seed (false));
9070
9071 p = q;
9072 }
9073
9074 clean_symbol_name (q);
9075 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9076 + strlen (type));
9077
9078 /* Set up the name of the file-level functions we may need.
9079 Use a global object (which is already required to be unique over
9080 the program) rather than the file name (which imposes extra
9081 constraints). */
9082 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9083
9084 return get_identifier (buf);
9085 }
9086
9087 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9088
9089 /* Complain that the tree code of NODE does not match the expected 0
9090 terminated list of trailing codes. The trailing code list can be
9091 empty, for a more vague error message. FILE, LINE, and FUNCTION
9092 are of the caller. */
9093
9094 void
9095 tree_check_failed (const_tree node, const char *file,
9096 int line, const char *function, ...)
9097 {
9098 va_list args;
9099 const char *buffer;
9100 unsigned length = 0;
9101 enum tree_code code;
9102
9103 va_start (args, function);
9104 while ((code = (enum tree_code) va_arg (args, int)))
9105 length += 4 + strlen (get_tree_code_name (code));
9106 va_end (args);
9107 if (length)
9108 {
9109 char *tmp;
9110 va_start (args, function);
9111 length += strlen ("expected ");
9112 buffer = tmp = (char *) alloca (length);
9113 length = 0;
9114 while ((code = (enum tree_code) va_arg (args, int)))
9115 {
9116 const char *prefix = length ? " or " : "expected ";
9117
9118 strcpy (tmp + length, prefix);
9119 length += strlen (prefix);
9120 strcpy (tmp + length, get_tree_code_name (code));
9121 length += strlen (get_tree_code_name (code));
9122 }
9123 va_end (args);
9124 }
9125 else
9126 buffer = "unexpected node";
9127
9128 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9129 buffer, get_tree_code_name (TREE_CODE (node)),
9130 function, trim_filename (file), line);
9131 }
9132
9133 /* Complain that the tree code of NODE does match the expected 0
9134 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9135 the caller. */
9136
9137 void
9138 tree_not_check_failed (const_tree node, const char *file,
9139 int line, const char *function, ...)
9140 {
9141 va_list args;
9142 char *buffer;
9143 unsigned length = 0;
9144 enum tree_code code;
9145
9146 va_start (args, function);
9147 while ((code = (enum tree_code) va_arg (args, int)))
9148 length += 4 + strlen (get_tree_code_name (code));
9149 va_end (args);
9150 va_start (args, function);
9151 buffer = (char *) alloca (length);
9152 length = 0;
9153 while ((code = (enum tree_code) va_arg (args, int)))
9154 {
9155 if (length)
9156 {
9157 strcpy (buffer + length, " or ");
9158 length += 4;
9159 }
9160 strcpy (buffer + length, get_tree_code_name (code));
9161 length += strlen (get_tree_code_name (code));
9162 }
9163 va_end (args);
9164
9165 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9166 buffer, get_tree_code_name (TREE_CODE (node)),
9167 function, trim_filename (file), line);
9168 }
9169
9170 /* Similar to tree_check_failed, except that we check for a class of tree
9171 code, given in CL. */
9172
9173 void
9174 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9175 const char *file, int line, const char *function)
9176 {
9177 internal_error
9178 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9179 TREE_CODE_CLASS_STRING (cl),
9180 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9181 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9182 }
9183
9184 /* Similar to tree_check_failed, except that instead of specifying a
9185 dozen codes, use the knowledge that they're all sequential. */
9186
9187 void
9188 tree_range_check_failed (const_tree node, const char *file, int line,
9189 const char *function, enum tree_code c1,
9190 enum tree_code c2)
9191 {
9192 char *buffer;
9193 unsigned length = 0;
9194 unsigned int c;
9195
9196 for (c = c1; c <= c2; ++c)
9197 length += 4 + strlen (get_tree_code_name ((enum tree_code) c));
9198
9199 length += strlen ("expected ");
9200 buffer = (char *) alloca (length);
9201 length = 0;
9202
9203 for (c = c1; c <= c2; ++c)
9204 {
9205 const char *prefix = length ? " or " : "expected ";
9206
9207 strcpy (buffer + length, prefix);
9208 length += strlen (prefix);
9209 strcpy (buffer + length, get_tree_code_name ((enum tree_code) c));
9210 length += strlen (get_tree_code_name ((enum tree_code) c));
9211 }
9212
9213 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9214 buffer, get_tree_code_name (TREE_CODE (node)),
9215 function, trim_filename (file), line);
9216 }
9217
9218
9219 /* Similar to tree_check_failed, except that we check that a tree does
9220 not have the specified code, given in CL. */
9221
9222 void
9223 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9224 const char *file, int line, const char *function)
9225 {
9226 internal_error
9227 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9228 TREE_CODE_CLASS_STRING (cl),
9229 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9230 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9231 }
9232
9233
9234 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9235
9236 void
9237 omp_clause_check_failed (const_tree node, const char *file, int line,
9238 const char *function, enum omp_clause_code code)
9239 {
9240 internal_error ("tree check: expected %<omp_clause %s%>, have %qs "
9241 "in %s, at %s:%d",
9242 omp_clause_code_name[code],
9243 get_tree_code_name (TREE_CODE (node)),
9244 function, trim_filename (file), line);
9245 }
9246
9247
9248 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9249
9250 void
9251 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9252 const char *function, enum omp_clause_code c1,
9253 enum omp_clause_code c2)
9254 {
9255 char *buffer;
9256 unsigned length = 0;
9257 unsigned int c;
9258
9259 for (c = c1; c <= c2; ++c)
9260 length += 4 + strlen (omp_clause_code_name[c]);
9261
9262 length += strlen ("expected ");
9263 buffer = (char *) alloca (length);
9264 length = 0;
9265
9266 for (c = c1; c <= c2; ++c)
9267 {
9268 const char *prefix = length ? " or " : "expected ";
9269
9270 strcpy (buffer + length, prefix);
9271 length += strlen (prefix);
9272 strcpy (buffer + length, omp_clause_code_name[c]);
9273 length += strlen (omp_clause_code_name[c]);
9274 }
9275
9276 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9277 buffer, omp_clause_code_name[TREE_CODE (node)],
9278 function, trim_filename (file), line);
9279 }
9280
9281
9282 #undef DEFTREESTRUCT
9283 #define DEFTREESTRUCT(VAL, NAME) NAME,
9284
9285 static const char *ts_enum_names[] = {
9286 #include "treestruct.def"
9287 };
9288 #undef DEFTREESTRUCT
9289
9290 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9291
9292 /* Similar to tree_class_check_failed, except that we check for
9293 whether CODE contains the tree structure identified by EN. */
9294
9295 void
9296 tree_contains_struct_check_failed (const_tree node,
9297 const enum tree_node_structure_enum en,
9298 const char *file, int line,
9299 const char *function)
9300 {
9301 internal_error
9302 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9303 TS_ENUM_NAME (en),
9304 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9305 }
9306
9307
9308 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9309 (dynamically sized) vector. */
9310
9311 void
9312 tree_int_cst_elt_check_failed (int idx, int len, const char *file, int line,
9313 const char *function)
9314 {
9315 internal_error
9316 ("tree check: accessed elt %d of %<tree_int_cst%> with %d elts in %s, "
9317 "at %s:%d",
9318 idx + 1, len, function, trim_filename (file), line);
9319 }
9320
9321 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9322 (dynamically sized) vector. */
9323
9324 void
9325 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9326 const char *function)
9327 {
9328 internal_error
9329 ("tree check: accessed elt %d of %<tree_vec%> with %d elts in %s, at %s:%d",
9330 idx + 1, len, function, trim_filename (file), line);
9331 }
9332
9333 /* Similar to above, except that the check is for the bounds of the operand
9334 vector of an expression node EXP. */
9335
9336 void
9337 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9338 int line, const char *function)
9339 {
9340 enum tree_code code = TREE_CODE (exp);
9341 internal_error
9342 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9343 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9344 function, trim_filename (file), line);
9345 }
9346
9347 /* Similar to above, except that the check is for the number of
9348 operands of an OMP_CLAUSE node. */
9349
9350 void
9351 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9352 int line, const char *function)
9353 {
9354 internal_error
9355 ("tree check: accessed operand %d of %<omp_clause %s%> with %d operands "
9356 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9357 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9358 trim_filename (file), line);
9359 }
9360 #endif /* ENABLE_TREE_CHECKING */
9361
9362 /* Create a new vector type node holding NUNITS units of type INNERTYPE,
9363 and mapped to the machine mode MODE. Initialize its fields and build
9364 the information necessary for debugging output. */
9365
9366 static tree
9367 make_vector_type (tree innertype, poly_int64 nunits, machine_mode mode)
9368 {
9369 tree t;
9370 tree mv_innertype = TYPE_MAIN_VARIANT (innertype);
9371
9372 t = make_node (VECTOR_TYPE);
9373 TREE_TYPE (t) = mv_innertype;
9374 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9375 SET_TYPE_MODE (t, mode);
9376
9377 if (TYPE_STRUCTURAL_EQUALITY_P (mv_innertype) || in_lto_p)
9378 SET_TYPE_STRUCTURAL_EQUALITY (t);
9379 else if ((TYPE_CANONICAL (mv_innertype) != innertype
9380 || mode != VOIDmode)
9381 && !VECTOR_BOOLEAN_TYPE_P (t))
9382 TYPE_CANONICAL (t)
9383 = make_vector_type (TYPE_CANONICAL (mv_innertype), nunits, VOIDmode);
9384
9385 layout_type (t);
9386
9387 hashval_t hash = type_hash_canon_hash (t);
9388 t = type_hash_canon (hash, t);
9389
9390 /* We have built a main variant, based on the main variant of the
9391 inner type. Use it to build the variant we return. */
9392 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9393 && TREE_TYPE (t) != innertype)
9394 return build_type_attribute_qual_variant (t,
9395 TYPE_ATTRIBUTES (innertype),
9396 TYPE_QUALS (innertype));
9397
9398 return t;
9399 }
9400
9401 static tree
9402 make_or_reuse_type (unsigned size, int unsignedp)
9403 {
9404 int i;
9405
9406 if (size == INT_TYPE_SIZE)
9407 return unsignedp ? unsigned_type_node : integer_type_node;
9408 if (size == CHAR_TYPE_SIZE)
9409 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9410 if (size == SHORT_TYPE_SIZE)
9411 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9412 if (size == LONG_TYPE_SIZE)
9413 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9414 if (size == LONG_LONG_TYPE_SIZE)
9415 return (unsignedp ? long_long_unsigned_type_node
9416 : long_long_integer_type_node);
9417
9418 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9419 if (size == int_n_data[i].bitsize
9420 && int_n_enabled_p[i])
9421 return (unsignedp ? int_n_trees[i].unsigned_type
9422 : int_n_trees[i].signed_type);
9423
9424 if (unsignedp)
9425 return make_unsigned_type (size);
9426 else
9427 return make_signed_type (size);
9428 }
9429
9430 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9431
9432 static tree
9433 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9434 {
9435 if (satp)
9436 {
9437 if (size == SHORT_FRACT_TYPE_SIZE)
9438 return unsignedp ? sat_unsigned_short_fract_type_node
9439 : sat_short_fract_type_node;
9440 if (size == FRACT_TYPE_SIZE)
9441 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9442 if (size == LONG_FRACT_TYPE_SIZE)
9443 return unsignedp ? sat_unsigned_long_fract_type_node
9444 : sat_long_fract_type_node;
9445 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9446 return unsignedp ? sat_unsigned_long_long_fract_type_node
9447 : sat_long_long_fract_type_node;
9448 }
9449 else
9450 {
9451 if (size == SHORT_FRACT_TYPE_SIZE)
9452 return unsignedp ? unsigned_short_fract_type_node
9453 : short_fract_type_node;
9454 if (size == FRACT_TYPE_SIZE)
9455 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9456 if (size == LONG_FRACT_TYPE_SIZE)
9457 return unsignedp ? unsigned_long_fract_type_node
9458 : long_fract_type_node;
9459 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9460 return unsignedp ? unsigned_long_long_fract_type_node
9461 : long_long_fract_type_node;
9462 }
9463
9464 return make_fract_type (size, unsignedp, satp);
9465 }
9466
9467 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9468
9469 static tree
9470 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9471 {
9472 if (satp)
9473 {
9474 if (size == SHORT_ACCUM_TYPE_SIZE)
9475 return unsignedp ? sat_unsigned_short_accum_type_node
9476 : sat_short_accum_type_node;
9477 if (size == ACCUM_TYPE_SIZE)
9478 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9479 if (size == LONG_ACCUM_TYPE_SIZE)
9480 return unsignedp ? sat_unsigned_long_accum_type_node
9481 : sat_long_accum_type_node;
9482 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9483 return unsignedp ? sat_unsigned_long_long_accum_type_node
9484 : sat_long_long_accum_type_node;
9485 }
9486 else
9487 {
9488 if (size == SHORT_ACCUM_TYPE_SIZE)
9489 return unsignedp ? unsigned_short_accum_type_node
9490 : short_accum_type_node;
9491 if (size == ACCUM_TYPE_SIZE)
9492 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9493 if (size == LONG_ACCUM_TYPE_SIZE)
9494 return unsignedp ? unsigned_long_accum_type_node
9495 : long_accum_type_node;
9496 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9497 return unsignedp ? unsigned_long_long_accum_type_node
9498 : long_long_accum_type_node;
9499 }
9500
9501 return make_accum_type (size, unsignedp, satp);
9502 }
9503
9504
9505 /* Create an atomic variant node for TYPE. This routine is called
9506 during initialization of data types to create the 5 basic atomic
9507 types. The generic build_variant_type function requires these to
9508 already be set up in order to function properly, so cannot be
9509 called from there. If ALIGN is non-zero, then ensure alignment is
9510 overridden to this value. */
9511
9512 static tree
9513 build_atomic_base (tree type, unsigned int align)
9514 {
9515 tree t;
9516
9517 /* Make sure its not already registered. */
9518 if ((t = get_qualified_type (type, TYPE_QUAL_ATOMIC)))
9519 return t;
9520
9521 t = build_variant_type_copy (type);
9522 set_type_quals (t, TYPE_QUAL_ATOMIC);
9523
9524 if (align)
9525 SET_TYPE_ALIGN (t, align);
9526
9527 return t;
9528 }
9529
9530 /* Information about the _FloatN and _FloatNx types. This must be in
9531 the same order as the corresponding TI_* enum values. */
9532 const floatn_type_info floatn_nx_types[NUM_FLOATN_NX_TYPES] =
9533 {
9534 { 16, false },
9535 { 32, false },
9536 { 64, false },
9537 { 128, false },
9538 { 32, true },
9539 { 64, true },
9540 { 128, true },
9541 };
9542
9543
9544 /* Create nodes for all integer types (and error_mark_node) using the sizes
9545 of C datatypes. SIGNED_CHAR specifies whether char is signed. */
9546
9547 void
9548 build_common_tree_nodes (bool signed_char)
9549 {
9550 int i;
9551
9552 error_mark_node = make_node (ERROR_MARK);
9553 TREE_TYPE (error_mark_node) = error_mark_node;
9554
9555 initialize_sizetypes ();
9556
9557 /* Define both `signed char' and `unsigned char'. */
9558 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9559 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9560 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9561 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9562
9563 /* Define `char', which is like either `signed char' or `unsigned char'
9564 but not the same as either. */
9565 char_type_node
9566 = (signed_char
9567 ? make_signed_type (CHAR_TYPE_SIZE)
9568 : make_unsigned_type (CHAR_TYPE_SIZE));
9569 TYPE_STRING_FLAG (char_type_node) = 1;
9570
9571 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9572 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9573 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9574 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9575 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9576 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9577 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9578 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9579
9580 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9581 {
9582 int_n_trees[i].signed_type = make_signed_type (int_n_data[i].bitsize);
9583 int_n_trees[i].unsigned_type = make_unsigned_type (int_n_data[i].bitsize);
9584
9585 if (int_n_enabled_p[i])
9586 {
9587 integer_types[itk_intN_0 + i * 2] = int_n_trees[i].signed_type;
9588 integer_types[itk_unsigned_intN_0 + i * 2] = int_n_trees[i].unsigned_type;
9589 }
9590 }
9591
9592 /* Define a boolean type. This type only represents boolean values but
9593 may be larger than char depending on the value of BOOL_TYPE_SIZE. */
9594 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9595 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9596 TYPE_PRECISION (boolean_type_node) = 1;
9597 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9598
9599 /* Define what type to use for size_t. */
9600 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9601 size_type_node = unsigned_type_node;
9602 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9603 size_type_node = long_unsigned_type_node;
9604 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9605 size_type_node = long_long_unsigned_type_node;
9606 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9607 size_type_node = short_unsigned_type_node;
9608 else
9609 {
9610 int i;
9611
9612 size_type_node = NULL_TREE;
9613 for (i = 0; i < NUM_INT_N_ENTS; i++)
9614 if (int_n_enabled_p[i])
9615 {
9616 char name[50], altname[50];
9617 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
9618 sprintf (altname, "__int%d__ unsigned", int_n_data[i].bitsize);
9619
9620 if (strcmp (name, SIZE_TYPE) == 0
9621 || strcmp (altname, SIZE_TYPE) == 0)
9622 {
9623 size_type_node = int_n_trees[i].unsigned_type;
9624 }
9625 }
9626 if (size_type_node == NULL_TREE)
9627 gcc_unreachable ();
9628 }
9629
9630 /* Define what type to use for ptrdiff_t. */
9631 if (strcmp (PTRDIFF_TYPE, "int") == 0)
9632 ptrdiff_type_node = integer_type_node;
9633 else if (strcmp (PTRDIFF_TYPE, "long int") == 0)
9634 ptrdiff_type_node = long_integer_type_node;
9635 else if (strcmp (PTRDIFF_TYPE, "long long int") == 0)
9636 ptrdiff_type_node = long_long_integer_type_node;
9637 else if (strcmp (PTRDIFF_TYPE, "short int") == 0)
9638 ptrdiff_type_node = short_integer_type_node;
9639 else
9640 {
9641 ptrdiff_type_node = NULL_TREE;
9642 for (int i = 0; i < NUM_INT_N_ENTS; i++)
9643 if (int_n_enabled_p[i])
9644 {
9645 char name[50], altname[50];
9646 sprintf (name, "__int%d", int_n_data[i].bitsize);
9647 sprintf (altname, "__int%d__", int_n_data[i].bitsize);
9648
9649 if (strcmp (name, PTRDIFF_TYPE) == 0
9650 || strcmp (altname, PTRDIFF_TYPE) == 0)
9651 ptrdiff_type_node = int_n_trees[i].signed_type;
9652 }
9653 if (ptrdiff_type_node == NULL_TREE)
9654 gcc_unreachable ();
9655 }
9656
9657 /* Fill in the rest of the sized types. Reuse existing type nodes
9658 when possible. */
9659 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9660 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9661 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9662 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9663 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9664
9665 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9666 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9667 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9668 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9669 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9670
9671 /* Don't call build_qualified type for atomics. That routine does
9672 special processing for atomics, and until they are initialized
9673 it's better not to make that call.
9674
9675 Check to see if there is a target override for atomic types. */
9676
9677 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
9678 targetm.atomic_align_for_mode (QImode));
9679 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
9680 targetm.atomic_align_for_mode (HImode));
9681 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
9682 targetm.atomic_align_for_mode (SImode));
9683 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
9684 targetm.atomic_align_for_mode (DImode));
9685 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
9686 targetm.atomic_align_for_mode (TImode));
9687
9688 access_public_node = get_identifier ("public");
9689 access_protected_node = get_identifier ("protected");
9690 access_private_node = get_identifier ("private");
9691
9692 /* Define these next since types below may used them. */
9693 integer_zero_node = build_int_cst (integer_type_node, 0);
9694 integer_one_node = build_int_cst (integer_type_node, 1);
9695 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9696
9697 size_zero_node = size_int (0);
9698 size_one_node = size_int (1);
9699 bitsize_zero_node = bitsize_int (0);
9700 bitsize_one_node = bitsize_int (1);
9701 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9702
9703 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9704 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9705
9706 void_type_node = make_node (VOID_TYPE);
9707 layout_type (void_type_node);
9708
9709 /* We are not going to have real types in C with less than byte alignment,
9710 so we might as well not have any types that claim to have it. */
9711 SET_TYPE_ALIGN (void_type_node, BITS_PER_UNIT);
9712 TYPE_USER_ALIGN (void_type_node) = 0;
9713
9714 void_node = make_node (VOID_CST);
9715 TREE_TYPE (void_node) = void_type_node;
9716
9717 void_list_node = build_tree_list (NULL_TREE, void_type_node);
9718
9719 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9720 layout_type (TREE_TYPE (null_pointer_node));
9721
9722 ptr_type_node = build_pointer_type (void_type_node);
9723 const_ptr_type_node
9724 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9725 for (unsigned i = 0; i < ARRAY_SIZE (builtin_structptr_types); ++i)
9726 builtin_structptr_types[i].node = builtin_structptr_types[i].base;
9727
9728 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
9729
9730 float_type_node = make_node (REAL_TYPE);
9731 machine_mode float_type_mode
9732 = targetm.c.mode_for_floating_type (TI_FLOAT_TYPE);
9733 SET_TYPE_MODE (float_type_node, float_type_mode);
9734 TYPE_PRECISION (float_type_node)
9735 = GET_MODE_PRECISION (float_type_mode).to_constant ();
9736 layout_type (float_type_node);
9737
9738 double_type_node = make_node (REAL_TYPE);
9739 machine_mode double_type_mode
9740 = targetm.c.mode_for_floating_type (TI_DOUBLE_TYPE);
9741 SET_TYPE_MODE (double_type_node, double_type_mode);
9742 TYPE_PRECISION (double_type_node)
9743 = GET_MODE_PRECISION (double_type_mode).to_constant ();
9744 layout_type (double_type_node);
9745
9746 long_double_type_node = make_node (REAL_TYPE);
9747 machine_mode long_double_type_mode
9748 = targetm.c.mode_for_floating_type (TI_LONG_DOUBLE_TYPE);
9749 SET_TYPE_MODE (long_double_type_node, long_double_type_mode);
9750 TYPE_PRECISION (long_double_type_node)
9751 = GET_MODE_PRECISION (long_double_type_mode).to_constant ();
9752 layout_type (long_double_type_node);
9753
9754 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
9755 {
9756 int n = floatn_nx_types[i].n;
9757 bool extended = floatn_nx_types[i].extended;
9758 scalar_float_mode mode;
9759 if (!targetm.floatn_mode (n, extended).exists (&mode))
9760 continue;
9761 int precision = GET_MODE_PRECISION (mode);
9762 FLOATN_NX_TYPE_NODE (i) = make_node (REAL_TYPE);
9763 TYPE_PRECISION (FLOATN_NX_TYPE_NODE (i)) = precision;
9764 layout_type (FLOATN_NX_TYPE_NODE (i));
9765 SET_TYPE_MODE (FLOATN_NX_TYPE_NODE (i), mode);
9766 }
9767 float128t_type_node = float128_type_node;
9768 #ifdef HAVE_BFmode
9769 if (REAL_MODE_FORMAT (BFmode) == &arm_bfloat_half_format
9770 && targetm.scalar_mode_supported_p (BFmode)
9771 && targetm.libgcc_floating_mode_supported_p (BFmode))
9772 {
9773 bfloat16_type_node = make_node (REAL_TYPE);
9774 TYPE_PRECISION (bfloat16_type_node) = GET_MODE_PRECISION (BFmode);
9775 layout_type (bfloat16_type_node);
9776 SET_TYPE_MODE (bfloat16_type_node, BFmode);
9777 }
9778 #endif
9779
9780 float_ptr_type_node = build_pointer_type (float_type_node);
9781 double_ptr_type_node = build_pointer_type (double_type_node);
9782 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9783 integer_ptr_type_node = build_pointer_type (integer_type_node);
9784
9785 /* Fixed size integer types. */
9786 uint16_type_node = make_or_reuse_type (16, 1);
9787 uint32_type_node = make_or_reuse_type (32, 1);
9788 uint64_type_node = make_or_reuse_type (64, 1);
9789 if (targetm.scalar_mode_supported_p (TImode))
9790 uint128_type_node = make_or_reuse_type (128, 1);
9791
9792 /* Decimal float types. */
9793 if (targetm.decimal_float_supported_p ())
9794 {
9795 dfloat32_type_node = make_node (REAL_TYPE);
9796 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9797 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9798 layout_type (dfloat32_type_node);
9799
9800 dfloat64_type_node = make_node (REAL_TYPE);
9801 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9802 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9803 layout_type (dfloat64_type_node);
9804
9805 dfloat128_type_node = make_node (REAL_TYPE);
9806 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9807 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9808 layout_type (dfloat128_type_node);
9809
9810 dfloat64x_type_node = make_node (REAL_TYPE);
9811 TYPE_PRECISION (dfloat64x_type_node) = DECIMAL128_TYPE_SIZE;
9812 SET_TYPE_MODE (dfloat64x_type_node, TDmode);
9813 layout_type (dfloat64x_type_node);
9814 }
9815
9816 complex_integer_type_node = build_complex_type (integer_type_node, true);
9817 complex_float_type_node = build_complex_type (float_type_node, true);
9818 complex_double_type_node = build_complex_type (double_type_node, true);
9819 complex_long_double_type_node = build_complex_type (long_double_type_node,
9820 true);
9821
9822 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
9823 {
9824 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
9825 COMPLEX_FLOATN_NX_TYPE_NODE (i)
9826 = build_complex_type (FLOATN_NX_TYPE_NODE (i));
9827 }
9828
9829 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9830 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9831 sat_ ## KIND ## _type_node = \
9832 make_sat_signed_ ## KIND ## _type (SIZE); \
9833 sat_unsigned_ ## KIND ## _type_node = \
9834 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9835 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9836 unsigned_ ## KIND ## _type_node = \
9837 make_unsigned_ ## KIND ## _type (SIZE);
9838
9839 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9840 sat_ ## WIDTH ## KIND ## _type_node = \
9841 make_sat_signed_ ## KIND ## _type (SIZE); \
9842 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9843 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9844 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9845 unsigned_ ## WIDTH ## KIND ## _type_node = \
9846 make_unsigned_ ## KIND ## _type (SIZE);
9847
9848 /* Make fixed-point type nodes based on four different widths. */
9849 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9850 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9851 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9852 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9853 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9854
9855 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9856 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9857 NAME ## _type_node = \
9858 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9859 u ## NAME ## _type_node = \
9860 make_or_reuse_unsigned_ ## KIND ## _type \
9861 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9862 sat_ ## NAME ## _type_node = \
9863 make_or_reuse_sat_signed_ ## KIND ## _type \
9864 (GET_MODE_BITSIZE (MODE ## mode)); \
9865 sat_u ## NAME ## _type_node = \
9866 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9867 (GET_MODE_BITSIZE (U ## MODE ## mode));
9868
9869 /* Fixed-point type and mode nodes. */
9870 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9871 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9872 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9873 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9874 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9875 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9876 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9877 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9878 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9879 MAKE_FIXED_MODE_NODE (accum, da, DA)
9880 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9881
9882 {
9883 tree t = targetm.build_builtin_va_list ();
9884
9885 /* Many back-ends define record types without setting TYPE_NAME.
9886 If we copied the record type here, we'd keep the original
9887 record type without a name. This breaks name mangling. So,
9888 don't copy record types and let c_common_nodes_and_builtins()
9889 declare the type to be __builtin_va_list. */
9890 if (TREE_CODE (t) != RECORD_TYPE)
9891 t = build_variant_type_copy (t);
9892
9893 va_list_type_node = t;
9894 }
9895
9896 /* SCEV analyzer global shared trees. */
9897 chrec_dont_know = make_node (SCEV_NOT_KNOWN);
9898 TREE_TYPE (chrec_dont_know) = void_type_node;
9899 chrec_known = make_node (SCEV_KNOWN);
9900 TREE_TYPE (chrec_known) = void_type_node;
9901 }
9902
9903 /* Modify DECL for given flags.
9904 TM_PURE attribute is set only on types, so the function will modify
9905 DECL's type when ECF_TM_PURE is used. */
9906
9907 void
9908 set_call_expr_flags (tree decl, int flags)
9909 {
9910 if (flags & ECF_NOTHROW)
9911 TREE_NOTHROW (decl) = 1;
9912 if (flags & ECF_CONST)
9913 TREE_READONLY (decl) = 1;
9914 if (flags & ECF_PURE)
9915 DECL_PURE_P (decl) = 1;
9916 if (flags & ECF_LOOPING_CONST_OR_PURE)
9917 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9918 if (flags & ECF_NOVOPS)
9919 DECL_IS_NOVOPS (decl) = 1;
9920 if (flags & ECF_NORETURN)
9921 TREE_THIS_VOLATILE (decl) = 1;
9922 if (flags & ECF_MALLOC)
9923 DECL_IS_MALLOC (decl) = 1;
9924 if (flags & ECF_RETURNS_TWICE)
9925 DECL_IS_RETURNS_TWICE (decl) = 1;
9926 if (flags & ECF_LEAF)
9927 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9928 NULL, DECL_ATTRIBUTES (decl));
9929 if (flags & ECF_COLD)
9930 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("cold"),
9931 NULL, DECL_ATTRIBUTES (decl));
9932 if (flags & ECF_RET1)
9933 DECL_ATTRIBUTES (decl)
9934 = tree_cons (get_identifier ("fn spec"),
9935 build_tree_list (NULL_TREE, build_string (2, "1 ")),
9936 DECL_ATTRIBUTES (decl));
9937 if ((flags & ECF_TM_PURE) && flag_tm)
9938 apply_tm_attr (decl, get_identifier ("transaction_pure"));
9939 if ((flags & ECF_XTHROW))
9940 DECL_ATTRIBUTES (decl)
9941 = tree_cons (get_identifier ("expected_throw"),
9942 NULL, DECL_ATTRIBUTES (decl));
9943 /* Looping const or pure is implied by noreturn.
9944 There is currently no way to declare looping const or looping pure alone. */
9945 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9946 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9947 }
9948
9949
9950 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9951
9952 static void
9953 local_define_builtin (const char *name, tree type, enum built_in_function code,
9954 const char *library_name, int ecf_flags)
9955 {
9956 tree decl;
9957
9958 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9959 library_name, NULL_TREE);
9960 set_call_expr_flags (decl, ecf_flags);
9961
9962 set_builtin_decl (code, decl, true);
9963 }
9964
9965 /* Call this function after instantiating all builtins that the language
9966 front end cares about. This will build the rest of the builtins
9967 and internal functions that are relied upon by the tree optimizers and
9968 the middle-end. */
9969
9970 void
9971 build_common_builtin_nodes (void)
9972 {
9973 tree tmp, ftype;
9974 int ecf_flags;
9975
9976 if (!builtin_decl_explicit_p (BUILT_IN_CLEAR_PADDING))
9977 {
9978 ftype = build_function_type_list (void_type_node,
9979 ptr_type_node,
9980 ptr_type_node,
9981 NULL_TREE);
9982 local_define_builtin ("__builtin_clear_padding", ftype,
9983 BUILT_IN_CLEAR_PADDING,
9984 "__builtin_clear_padding",
9985 ECF_LEAF | ECF_NOTHROW);
9986 }
9987
9988 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE)
9989 || !builtin_decl_explicit_p (BUILT_IN_TRAP)
9990 || !builtin_decl_explicit_p (BUILT_IN_UNREACHABLE_TRAP)
9991 || !builtin_decl_explicit_p (BUILT_IN_ABORT))
9992 {
9993 ftype = build_function_type (void_type_node, void_list_node);
9994 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9995 local_define_builtin ("__builtin_unreachable", ftype,
9996 BUILT_IN_UNREACHABLE,
9997 "__builtin_unreachable",
9998 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9999 | ECF_CONST | ECF_COLD);
10000 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE_TRAP))
10001 local_define_builtin ("__builtin_unreachable trap", ftype,
10002 BUILT_IN_UNREACHABLE_TRAP,
10003 "__builtin_unreachable trap",
10004 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
10005 | ECF_CONST | ECF_COLD);
10006 if (!builtin_decl_explicit_p (BUILT_IN_ABORT))
10007 local_define_builtin ("__builtin_abort", ftype, BUILT_IN_ABORT,
10008 "abort",
10009 ECF_LEAF | ECF_NORETURN | ECF_CONST | ECF_COLD);
10010 if (!builtin_decl_explicit_p (BUILT_IN_TRAP))
10011 local_define_builtin ("__builtin_trap", ftype, BUILT_IN_TRAP,
10012 "__builtin_trap",
10013 ECF_NORETURN | ECF_NOTHROW | ECF_LEAF | ECF_COLD);
10014 }
10015
10016 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
10017 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10018 {
10019 ftype = build_function_type_list (ptr_type_node,
10020 ptr_type_node, const_ptr_type_node,
10021 size_type_node, NULL_TREE);
10022
10023 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
10024 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
10025 "memcpy", ECF_NOTHROW | ECF_LEAF);
10026 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10027 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
10028 "memmove", ECF_NOTHROW | ECF_LEAF);
10029 }
10030
10031 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
10032 {
10033 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
10034 const_ptr_type_node, size_type_node,
10035 NULL_TREE);
10036 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
10037 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10038 }
10039
10040 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
10041 {
10042 ftype = build_function_type_list (ptr_type_node,
10043 ptr_type_node, integer_type_node,
10044 size_type_node, NULL_TREE);
10045 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
10046 "memset", ECF_NOTHROW | ECF_LEAF);
10047 }
10048
10049 /* If we're checking the stack, `alloca' can throw. */
10050 const int alloca_flags
10051 = ECF_MALLOC | ECF_LEAF | (flag_stack_check ? 0 : ECF_NOTHROW);
10052
10053 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
10054 {
10055 ftype = build_function_type_list (ptr_type_node,
10056 size_type_node, NULL_TREE);
10057 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
10058 "alloca", alloca_flags);
10059 }
10060
10061 ftype = build_function_type_list (ptr_type_node, size_type_node,
10062 size_type_node, NULL_TREE);
10063 local_define_builtin ("__builtin_alloca_with_align", ftype,
10064 BUILT_IN_ALLOCA_WITH_ALIGN,
10065 "__builtin_alloca_with_align",
10066 alloca_flags);
10067
10068 ftype = build_function_type_list (ptr_type_node, size_type_node,
10069 size_type_node, size_type_node, NULL_TREE);
10070 local_define_builtin ("__builtin_alloca_with_align_and_max", ftype,
10071 BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX,
10072 "__builtin_alloca_with_align_and_max",
10073 alloca_flags);
10074
10075 ftype = build_function_type_list (void_type_node,
10076 ptr_type_node, ptr_type_node,
10077 ptr_type_node, NULL_TREE);
10078 local_define_builtin ("__builtin_init_trampoline", ftype,
10079 BUILT_IN_INIT_TRAMPOLINE,
10080 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
10081 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
10082 BUILT_IN_INIT_HEAP_TRAMPOLINE,
10083 "__builtin_init_heap_trampoline",
10084 ECF_NOTHROW | ECF_LEAF);
10085 local_define_builtin ("__builtin_init_descriptor", ftype,
10086 BUILT_IN_INIT_DESCRIPTOR,
10087 "__builtin_init_descriptor", ECF_NOTHROW | ECF_LEAF);
10088
10089 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
10090 local_define_builtin ("__builtin_adjust_trampoline", ftype,
10091 BUILT_IN_ADJUST_TRAMPOLINE,
10092 "__builtin_adjust_trampoline",
10093 ECF_CONST | ECF_NOTHROW);
10094 local_define_builtin ("__builtin_adjust_descriptor", ftype,
10095 BUILT_IN_ADJUST_DESCRIPTOR,
10096 "__builtin_adjust_descriptor",
10097 ECF_CONST | ECF_NOTHROW);
10098
10099 ftype = build_function_type_list (void_type_node,
10100 ptr_type_node, ptr_type_node, NULL_TREE);
10101 if (!builtin_decl_explicit_p (BUILT_IN_CLEAR_CACHE))
10102 local_define_builtin ("__builtin___clear_cache", ftype,
10103 BUILT_IN_CLEAR_CACHE,
10104 "__clear_cache",
10105 ECF_NOTHROW);
10106
10107 local_define_builtin ("__builtin_nonlocal_goto", ftype,
10108 BUILT_IN_NONLOCAL_GOTO,
10109 "__builtin_nonlocal_goto",
10110 ECF_NORETURN | ECF_NOTHROW);
10111
10112 tree ptr_ptr_type_node = build_pointer_type (ptr_type_node);
10113
10114 if (!builtin_decl_explicit_p (BUILT_IN_GCC_NESTED_PTR_CREATED))
10115 {
10116 ftype = build_function_type_list (void_type_node,
10117 ptr_type_node, // void *chain
10118 ptr_type_node, // void *func
10119 ptr_ptr_type_node, // void **dst
10120 NULL_TREE);
10121 local_define_builtin ("__builtin___gcc_nested_func_ptr_created", ftype,
10122 BUILT_IN_GCC_NESTED_PTR_CREATED,
10123 "__gcc_nested_func_ptr_created", ECF_NOTHROW);
10124 }
10125
10126 if (!builtin_decl_explicit_p (BUILT_IN_GCC_NESTED_PTR_DELETED))
10127 {
10128 ftype = build_function_type_list (void_type_node, NULL_TREE);
10129 local_define_builtin ("__builtin___gcc_nested_func_ptr_deleted", ftype,
10130 BUILT_IN_GCC_NESTED_PTR_DELETED,
10131 "__gcc_nested_func_ptr_deleted", ECF_NOTHROW);
10132 }
10133
10134 ftype = build_function_type_list (void_type_node,
10135 ptr_type_node, ptr_type_node, NULL_TREE);
10136 local_define_builtin ("__builtin_setjmp_setup", ftype,
10137 BUILT_IN_SETJMP_SETUP,
10138 "__builtin_setjmp_setup", ECF_NOTHROW);
10139
10140 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10141 local_define_builtin ("__builtin_setjmp_receiver", ftype,
10142 BUILT_IN_SETJMP_RECEIVER,
10143 "__builtin_setjmp_receiver", ECF_NOTHROW | ECF_LEAF);
10144
10145 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
10146 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
10147 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
10148
10149 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10150 local_define_builtin ("__builtin_stack_restore", ftype,
10151 BUILT_IN_STACK_RESTORE,
10152 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
10153
10154 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
10155 const_ptr_type_node, size_type_node,
10156 NULL_TREE);
10157 local_define_builtin ("__builtin_memcmp_eq", ftype, BUILT_IN_MEMCMP_EQ,
10158 "__builtin_memcmp_eq",
10159 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10160
10161 local_define_builtin ("__builtin_strncmp_eq", ftype, BUILT_IN_STRNCMP_EQ,
10162 "__builtin_strncmp_eq",
10163 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10164
10165 local_define_builtin ("__builtin_strcmp_eq", ftype, BUILT_IN_STRCMP_EQ,
10166 "__builtin_strcmp_eq",
10167 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10168
10169 /* If there's a possibility that we might use the ARM EABI, build the
10170 alternate __cxa_end_cleanup node used to resume from C++. */
10171 if (targetm.arm_eabi_unwinder)
10172 {
10173 ftype = build_function_type_list (void_type_node, NULL_TREE);
10174 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
10175 BUILT_IN_CXA_END_CLEANUP,
10176 "__cxa_end_cleanup",
10177 ECF_NORETURN | ECF_XTHROW | ECF_LEAF);
10178 }
10179
10180 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10181 local_define_builtin ("__builtin_unwind_resume", ftype,
10182 BUILT_IN_UNWIND_RESUME,
10183 ((targetm_common.except_unwind_info (&global_options)
10184 == UI_SJLJ)
10185 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
10186 ECF_NORETURN | ECF_XTHROW);
10187
10188 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
10189 {
10190 ftype = build_function_type_list (ptr_type_node, integer_type_node,
10191 NULL_TREE);
10192 local_define_builtin ("__builtin_return_address", ftype,
10193 BUILT_IN_RETURN_ADDRESS,
10194 "__builtin_return_address",
10195 ECF_NOTHROW);
10196 }
10197
10198 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
10199 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10200 {
10201 ftype = build_function_type_list (void_type_node, ptr_type_node,
10202 ptr_type_node, NULL_TREE);
10203 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
10204 local_define_builtin ("__cyg_profile_func_enter", ftype,
10205 BUILT_IN_PROFILE_FUNC_ENTER,
10206 "__cyg_profile_func_enter", 0);
10207 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10208 local_define_builtin ("__cyg_profile_func_exit", ftype,
10209 BUILT_IN_PROFILE_FUNC_EXIT,
10210 "__cyg_profile_func_exit", 0);
10211 }
10212
10213 /* The exception object and filter values from the runtime. The argument
10214 must be zero before exception lowering, i.e. from the front end. After
10215 exception lowering, it will be the region number for the exception
10216 landing pad. These functions are PURE instead of CONST to prevent
10217 them from being hoisted past the exception edge that will initialize
10218 its value in the landing pad. */
10219 ftype = build_function_type_list (ptr_type_node,
10220 integer_type_node, NULL_TREE);
10221 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
10222 /* Only use TM_PURE if we have TM language support. */
10223 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
10224 ecf_flags |= ECF_TM_PURE;
10225 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
10226 "__builtin_eh_pointer", ecf_flags);
10227
10228 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
10229 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
10230 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
10231 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10232
10233 ftype = build_function_type_list (void_type_node,
10234 integer_type_node, integer_type_node,
10235 NULL_TREE);
10236 local_define_builtin ("__builtin_eh_copy_values", ftype,
10237 BUILT_IN_EH_COPY_VALUES,
10238 "__builtin_eh_copy_values", ECF_NOTHROW);
10239
10240 /* Complex multiplication and division. These are handled as builtins
10241 rather than optabs because emit_library_call_value doesn't support
10242 complex. Further, we can do slightly better with folding these
10243 beasties if the real and complex parts of the arguments are separate. */
10244 {
10245 int mode;
10246
10247 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10248 {
10249 char mode_name_buf[4], *q;
10250 const char *p;
10251 enum built_in_function mcode, dcode;
10252 tree type, inner_type;
10253 const char *prefix = "__";
10254
10255 if (targetm.libfunc_gnu_prefix)
10256 prefix = "__gnu_";
10257
10258 type = lang_hooks.types.type_for_mode ((machine_mode) mode, 0);
10259 if (type == NULL)
10260 continue;
10261 inner_type = TREE_TYPE (type);
10262
10263 ftype = build_function_type_list (type, inner_type, inner_type,
10264 inner_type, inner_type, NULL_TREE);
10265
10266 mcode = ((enum built_in_function)
10267 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10268 dcode = ((enum built_in_function)
10269 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10270
10271 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10272 *q = TOLOWER (*p);
10273 *q = '\0';
10274
10275 /* For -ftrapping-math these should throw from a former
10276 -fnon-call-exception stmt. */
10277 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10278 NULL);
10279 local_define_builtin (built_in_names[mcode], ftype, mcode,
10280 built_in_names[mcode],
10281 ECF_CONST | ECF_LEAF);
10282
10283 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10284 NULL);
10285 local_define_builtin (built_in_names[dcode], ftype, dcode,
10286 built_in_names[dcode],
10287 ECF_CONST | ECF_LEAF);
10288 }
10289 }
10290
10291 init_internal_fns ();
10292 }
10293
10294 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10295 better way.
10296
10297 If we requested a pointer to a vector, build up the pointers that
10298 we stripped off while looking for the inner type. Similarly for
10299 return values from functions.
10300
10301 The argument TYPE is the top of the chain, and BOTTOM is the
10302 new type which we will point to. */
10303
10304 tree
10305 reconstruct_complex_type (tree type, tree bottom)
10306 {
10307 tree inner, outer;
10308
10309 if (TREE_CODE (type) == POINTER_TYPE)
10310 {
10311 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10312 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10313 TYPE_REF_CAN_ALIAS_ALL (type));
10314 }
10315 else if (TREE_CODE (type) == REFERENCE_TYPE)
10316 {
10317 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10318 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10319 TYPE_REF_CAN_ALIAS_ALL (type));
10320 }
10321 else if (TREE_CODE (type) == ARRAY_TYPE)
10322 {
10323 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10324 outer = build_array_type (inner, TYPE_DOMAIN (type));
10325 }
10326 else if (TREE_CODE (type) == FUNCTION_TYPE)
10327 {
10328 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10329 outer = build_function_type (inner, TYPE_ARG_TYPES (type),
10330 TYPE_NO_NAMED_ARGS_STDARG_P (type));
10331 }
10332 else if (TREE_CODE (type) == METHOD_TYPE)
10333 {
10334 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10335 /* The build_method_type_directly() routine prepends 'this' to argument list,
10336 so we must compensate by getting rid of it. */
10337 outer
10338 = build_method_type_directly
10339 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10340 inner,
10341 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10342 }
10343 else if (TREE_CODE (type) == OFFSET_TYPE)
10344 {
10345 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10346 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10347 }
10348 else
10349 return bottom;
10350
10351 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10352 TYPE_QUALS (type));
10353 }
10354
10355 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10356 the inner type. */
10357 tree
10358 build_vector_type_for_mode (tree innertype, machine_mode mode)
10359 {
10360 poly_int64 nunits;
10361 unsigned int bitsize;
10362
10363 switch (GET_MODE_CLASS (mode))
10364 {
10365 case MODE_VECTOR_BOOL:
10366 case MODE_VECTOR_INT:
10367 case MODE_VECTOR_FLOAT:
10368 case MODE_VECTOR_FRACT:
10369 case MODE_VECTOR_UFRACT:
10370 case MODE_VECTOR_ACCUM:
10371 case MODE_VECTOR_UACCUM:
10372 nunits = GET_MODE_NUNITS (mode);
10373 break;
10374
10375 case MODE_INT:
10376 /* Check that there are no leftover bits. */
10377 bitsize = GET_MODE_BITSIZE (as_a <scalar_int_mode> (mode));
10378 gcc_assert (bitsize % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10379 nunits = bitsize / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10380 break;
10381
10382 default:
10383 gcc_unreachable ();
10384 }
10385
10386 return make_vector_type (innertype, nunits, mode);
10387 }
10388
10389 /* Similarly, but takes the inner type and number of units, which must be
10390 a power of two. */
10391
10392 tree
10393 build_vector_type (tree innertype, poly_int64 nunits)
10394 {
10395 return make_vector_type (innertype, nunits, VOIDmode);
10396 }
10397
10398 /* Build a truth vector with NUNITS units, giving it mode MASK_MODE. */
10399
10400 tree
10401 build_truth_vector_type_for_mode (poly_uint64 nunits, machine_mode mask_mode)
10402 {
10403 gcc_assert (mask_mode != BLKmode);
10404
10405 unsigned HOST_WIDE_INT esize;
10406 if (VECTOR_MODE_P (mask_mode))
10407 {
10408 poly_uint64 vsize = GET_MODE_PRECISION (mask_mode);
10409 esize = vector_element_size (vsize, nunits);
10410 }
10411 else
10412 esize = 1;
10413
10414 tree bool_type = build_nonstandard_boolean_type (esize);
10415
10416 return make_vector_type (bool_type, nunits, mask_mode);
10417 }
10418
10419 /* Build a vector type that holds one boolean result for each element of
10420 vector type VECTYPE. The public interface for this operation is
10421 truth_type_for. */
10422
10423 static tree
10424 build_truth_vector_type_for (tree vectype)
10425 {
10426 machine_mode vector_mode = TYPE_MODE (vectype);
10427 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
10428
10429 machine_mode mask_mode;
10430 if (VECTOR_MODE_P (vector_mode)
10431 && targetm.vectorize.get_mask_mode (vector_mode).exists (&mask_mode))
10432 return build_truth_vector_type_for_mode (nunits, mask_mode);
10433
10434 poly_uint64 vsize = tree_to_poly_uint64 (TYPE_SIZE (vectype));
10435 unsigned HOST_WIDE_INT esize = vector_element_size (vsize, nunits);
10436 tree bool_type = build_nonstandard_boolean_type (esize);
10437
10438 return make_vector_type (bool_type, nunits, VOIDmode);
10439 }
10440
10441 /* Like build_vector_type, but builds a variant type with TYPE_VECTOR_OPAQUE
10442 set. */
10443
10444 tree
10445 build_opaque_vector_type (tree innertype, poly_int64 nunits)
10446 {
10447 tree t = make_vector_type (innertype, nunits, VOIDmode);
10448 tree cand;
10449 /* We always build the non-opaque variant before the opaque one,
10450 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10451 cand = TYPE_NEXT_VARIANT (t);
10452 if (cand
10453 && TYPE_VECTOR_OPAQUE (cand)
10454 && check_qualified_type (cand, t, TYPE_QUALS (t)))
10455 return cand;
10456 /* Othewise build a variant type and make sure to queue it after
10457 the non-opaque type. */
10458 cand = build_distinct_type_copy (t);
10459 TYPE_VECTOR_OPAQUE (cand) = true;
10460 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10461 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10462 TYPE_NEXT_VARIANT (t) = cand;
10463 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10464 /* Type variants have no alias set defined. */
10465 TYPE_ALIAS_SET (cand) = -1;
10466 return cand;
10467 }
10468
10469 /* Return the value of element I of VECTOR_CST T as a wide_int. */
10470
10471 static poly_wide_int
10472 vector_cst_int_elt (const_tree t, unsigned int i)
10473 {
10474 /* First handle elements that are directly encoded. */
10475 unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
10476 if (i < encoded_nelts)
10477 return wi::to_poly_wide (VECTOR_CST_ENCODED_ELT (t, i));
10478
10479 /* Identify the pattern that contains element I and work out the index of
10480 the last encoded element for that pattern. */
10481 unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
10482 unsigned int pattern = i % npatterns;
10483 unsigned int count = i / npatterns;
10484 unsigned int final_i = encoded_nelts - npatterns + pattern;
10485
10486 /* If there are no steps, the final encoded value is the right one. */
10487 if (!VECTOR_CST_STEPPED_P (t))
10488 return wi::to_poly_wide (VECTOR_CST_ENCODED_ELT (t, final_i));
10489
10490 /* Otherwise work out the value from the last two encoded elements. */
10491 tree v1 = VECTOR_CST_ENCODED_ELT (t, final_i - npatterns);
10492 tree v2 = VECTOR_CST_ENCODED_ELT (t, final_i);
10493 poly_wide_int diff = wi::to_poly_wide (v2) - wi::to_poly_wide (v1);
10494 return wi::to_poly_wide (v2) + (count - 2) * diff;
10495 }
10496
10497 /* Return the value of element I of VECTOR_CST T. */
10498
10499 tree
10500 vector_cst_elt (const_tree t, unsigned int i)
10501 {
10502 /* First handle elements that are directly encoded. */
10503 unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
10504 if (i < encoded_nelts)
10505 return VECTOR_CST_ENCODED_ELT (t, i);
10506
10507 /* If there are no steps, the final encoded value is the right one. */
10508 if (!VECTOR_CST_STEPPED_P (t))
10509 {
10510 /* Identify the pattern that contains element I and work out the index of
10511 the last encoded element for that pattern. */
10512 unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
10513 unsigned int pattern = i % npatterns;
10514 unsigned int final_i = encoded_nelts - npatterns + pattern;
10515 return VECTOR_CST_ENCODED_ELT (t, final_i);
10516 }
10517
10518 /* Otherwise work out the value from the last two encoded elements. */
10519 return wide_int_to_tree (TREE_TYPE (TREE_TYPE (t)),
10520 vector_cst_int_elt (t, i));
10521 }
10522
10523 /* Given an initializer INIT, return TRUE if INIT is zero or some
10524 aggregate of zeros. Otherwise return FALSE. If NONZERO is not
10525 null, set *NONZERO if and only if INIT is known not to be all
10526 zeros. The combination of return value of false and *NONZERO
10527 false implies that INIT may but need not be all zeros. Other
10528 combinations indicate definitive answers. */
10529
10530 bool
10531 initializer_zerop (const_tree init, bool *nonzero /* = NULL */)
10532 {
10533 bool dummy;
10534 if (!nonzero)
10535 nonzero = &dummy;
10536
10537 /* Conservatively clear NONZERO and set it only if INIT is definitely
10538 not all zero. */
10539 *nonzero = false;
10540
10541 STRIP_NOPS (init);
10542
10543 unsigned HOST_WIDE_INT off = 0;
10544
10545 switch (TREE_CODE (init))
10546 {
10547 case INTEGER_CST:
10548 if (integer_zerop (init))
10549 return true;
10550
10551 *nonzero = true;
10552 return false;
10553
10554 case REAL_CST:
10555 /* ??? Note that this is not correct for C4X float formats. There,
10556 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10557 negative exponent. */
10558 if (real_zerop (init)
10559 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init)))
10560 return true;
10561
10562 *nonzero = true;
10563 return false;
10564
10565 case FIXED_CST:
10566 if (fixed_zerop (init))
10567 return true;
10568
10569 *nonzero = true;
10570 return false;
10571
10572 case COMPLEX_CST:
10573 if (integer_zerop (init)
10574 || (real_zerop (init)
10575 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10576 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init)))))
10577 return true;
10578
10579 *nonzero = true;
10580 return false;
10581
10582 case VECTOR_CST:
10583 if (VECTOR_CST_NPATTERNS (init) == 1
10584 && VECTOR_CST_DUPLICATE_P (init)
10585 && initializer_zerop (VECTOR_CST_ENCODED_ELT (init, 0)))
10586 return true;
10587
10588 *nonzero = true;
10589 return false;
10590
10591 case RAW_DATA_CST:
10592 for (unsigned int i = 0; i < (unsigned int) RAW_DATA_LENGTH (init); ++i)
10593 if (RAW_DATA_POINTER (init)[i])
10594 {
10595 *nonzero = true;
10596 return false;
10597 }
10598 return true;
10599
10600 case CONSTRUCTOR:
10601 {
10602 if (TREE_CLOBBER_P (init))
10603 return false;
10604
10605 unsigned HOST_WIDE_INT idx;
10606 tree elt;
10607
10608 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10609 if (!initializer_zerop (elt, nonzero))
10610 return false;
10611
10612 return true;
10613 }
10614
10615 case MEM_REF:
10616 {
10617 tree arg = TREE_OPERAND (init, 0);
10618 if (TREE_CODE (arg) != ADDR_EXPR)
10619 return false;
10620 tree offset = TREE_OPERAND (init, 1);
10621 if (TREE_CODE (offset) != INTEGER_CST
10622 || !tree_fits_uhwi_p (offset))
10623 return false;
10624 off = tree_to_uhwi (offset);
10625 if (INT_MAX < off)
10626 return false;
10627 arg = TREE_OPERAND (arg, 0);
10628 if (TREE_CODE (arg) != STRING_CST)
10629 return false;
10630 init = arg;
10631 }
10632 /* Fall through. */
10633
10634 case STRING_CST:
10635 {
10636 gcc_assert (off <= INT_MAX);
10637
10638 int i = off;
10639 int n = TREE_STRING_LENGTH (init);
10640 if (n <= i)
10641 return false;
10642
10643 /* We need to loop through all elements to handle cases like
10644 "\0" and "\0foobar". */
10645 for (i = 0; i < n; ++i)
10646 if (TREE_STRING_POINTER (init)[i] != '\0')
10647 {
10648 *nonzero = true;
10649 return false;
10650 }
10651
10652 return true;
10653 }
10654
10655 default:
10656 return false;
10657 }
10658 }
10659
10660 /* Return true if EXPR is an initializer expression in which every element
10661 is a constant that is numerically equal to 0 or 1. The elements do not
10662 need to be equal to each other. */
10663
10664 bool
10665 initializer_each_zero_or_onep (const_tree expr)
10666 {
10667 STRIP_ANY_LOCATION_WRAPPER (expr);
10668
10669 switch (TREE_CODE (expr))
10670 {
10671 case INTEGER_CST:
10672 return integer_zerop (expr) || integer_onep (expr);
10673
10674 case REAL_CST:
10675 return real_zerop (expr) || real_onep (expr);
10676
10677 case VECTOR_CST:
10678 {
10679 unsigned HOST_WIDE_INT nelts = vector_cst_encoded_nelts (expr);
10680 if (VECTOR_CST_STEPPED_P (expr)
10681 && !TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr)).is_constant (&nelts))
10682 return false;
10683
10684 for (unsigned int i = 0; i < nelts; ++i)
10685 {
10686 tree elt = vector_cst_elt (expr, i);
10687 if (!initializer_each_zero_or_onep (elt))
10688 return false;
10689 }
10690
10691 return true;
10692 }
10693
10694 default:
10695 return false;
10696 }
10697 }
10698
10699 /* Check if vector VEC consists of all the equal elements and
10700 that the number of elements corresponds to the type of VEC.
10701 The function returns first element of the vector
10702 or NULL_TREE if the vector is not uniform. */
10703 tree
10704 uniform_vector_p (const_tree vec)
10705 {
10706 tree first, t;
10707 unsigned HOST_WIDE_INT i, nelts;
10708
10709 if (vec == NULL_TREE)
10710 return NULL_TREE;
10711
10712 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10713
10714 if (TREE_CODE (vec) == VEC_DUPLICATE_EXPR)
10715 return TREE_OPERAND (vec, 0);
10716
10717 else if (TREE_CODE (vec) == VECTOR_CST)
10718 {
10719 if (VECTOR_CST_NPATTERNS (vec) == 1 && VECTOR_CST_DUPLICATE_P (vec))
10720 return VECTOR_CST_ENCODED_ELT (vec, 0);
10721 return NULL_TREE;
10722 }
10723
10724 else if (TREE_CODE (vec) == CONSTRUCTOR
10725 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)).is_constant (&nelts))
10726 {
10727 first = error_mark_node;
10728
10729 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10730 {
10731 if (i == 0)
10732 {
10733 first = t;
10734 continue;
10735 }
10736 if (!operand_equal_p (first, t, 0))
10737 return NULL_TREE;
10738 }
10739 if (i != nelts)
10740 return NULL_TREE;
10741
10742 if (TREE_CODE (first) == CONSTRUCTOR || TREE_CODE (first) == VECTOR_CST)
10743 return uniform_vector_p (first);
10744 return first;
10745 }
10746
10747 return NULL_TREE;
10748 }
10749
10750 /* If the argument is INTEGER_CST, return it. If the argument is vector
10751 with all elements the same INTEGER_CST, return that INTEGER_CST. Otherwise
10752 return NULL_TREE.
10753 Look through location wrappers. */
10754
10755 tree
10756 uniform_integer_cst_p (tree t)
10757 {
10758 STRIP_ANY_LOCATION_WRAPPER (t);
10759
10760 if (TREE_CODE (t) == INTEGER_CST)
10761 return t;
10762
10763 if (VECTOR_TYPE_P (TREE_TYPE (t)))
10764 {
10765 t = uniform_vector_p (t);
10766 if (t && TREE_CODE (t) == INTEGER_CST)
10767 return t;
10768 }
10769
10770 return NULL_TREE;
10771 }
10772
10773 /* Checks to see if T is a constant or a constant vector and if each element E
10774 adheres to ~E + 1 == pow2 then return ~E otherwise NULL_TREE. */
10775
10776 tree
10777 bitmask_inv_cst_vector_p (tree t)
10778 {
10779
10780 tree_code code = TREE_CODE (t);
10781 tree type = TREE_TYPE (t);
10782
10783 if (!INTEGRAL_TYPE_P (type)
10784 && !VECTOR_INTEGER_TYPE_P (type))
10785 return NULL_TREE;
10786
10787 unsigned HOST_WIDE_INT nelts = 1;
10788 tree cst;
10789 unsigned int idx = 0;
10790 bool uniform = uniform_integer_cst_p (t);
10791 tree newtype = unsigned_type_for (type);
10792 tree_vector_builder builder;
10793 if (code == INTEGER_CST)
10794 cst = t;
10795 else
10796 {
10797 if (!VECTOR_CST_NELTS (t).is_constant (&nelts))
10798 return NULL_TREE;
10799
10800 cst = vector_cst_elt (t, 0);
10801 builder.new_vector (newtype, nelts, 1);
10802 }
10803
10804 tree ty = unsigned_type_for (TREE_TYPE (cst));
10805
10806 do
10807 {
10808 if (idx > 0)
10809 cst = vector_cst_elt (t, idx);
10810 wide_int icst = wi::to_wide (cst);
10811 wide_int inv = wi::bit_not (icst);
10812 icst = wi::add (1, inv);
10813 if (wi::popcount (icst) != 1)
10814 return NULL_TREE;
10815
10816 tree newcst = wide_int_to_tree (ty, inv);
10817
10818 if (uniform)
10819 return build_uniform_cst (newtype, newcst);
10820
10821 builder.quick_push (newcst);
10822 }
10823 while (++idx < nelts);
10824
10825 return builder.build ();
10826 }
10827
10828 /* If VECTOR_CST T has a single nonzero element, return the index of that
10829 element, otherwise return -1. */
10830
10831 int
10832 single_nonzero_element (const_tree t)
10833 {
10834 unsigned HOST_WIDE_INT nelts;
10835 unsigned int repeat_nelts;
10836 if (VECTOR_CST_NELTS (t).is_constant (&nelts))
10837 repeat_nelts = nelts;
10838 else if (VECTOR_CST_NELTS_PER_PATTERN (t) == 2)
10839 {
10840 nelts = vector_cst_encoded_nelts (t);
10841 repeat_nelts = VECTOR_CST_NPATTERNS (t);
10842 }
10843 else
10844 return -1;
10845
10846 int res = -1;
10847 for (unsigned int i = 0; i < nelts; ++i)
10848 {
10849 tree elt = vector_cst_elt (t, i);
10850 if (!integer_zerop (elt) && !real_zerop (elt))
10851 {
10852 if (res >= 0 || i >= repeat_nelts)
10853 return -1;
10854 res = i;
10855 }
10856 }
10857 return res;
10858 }
10859
10860 /* Build an empty statement at location LOC. */
10861
10862 tree
10863 build_empty_stmt (location_t loc)
10864 {
10865 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10866 SET_EXPR_LOCATION (t, loc);
10867 return t;
10868 }
10869
10870
10871 /* Build an OMP clause with code CODE. LOC is the location of the
10872 clause. */
10873
10874 tree
10875 build_omp_clause (location_t loc, enum omp_clause_code code)
10876 {
10877 tree t;
10878 int size, length;
10879
10880 length = omp_clause_num_ops[code];
10881 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10882
10883 record_node_allocation_statistics (OMP_CLAUSE, size);
10884
10885 t = (tree) ggc_internal_alloc (size);
10886 memset (t, 0, size);
10887 TREE_SET_CODE (t, OMP_CLAUSE);
10888 OMP_CLAUSE_SET_CODE (t, code);
10889 OMP_CLAUSE_LOCATION (t) = loc;
10890
10891 return t;
10892 }
10893
10894 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10895 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10896 Except for the CODE and operand count field, other storage for the
10897 object is initialized to zeros. */
10898
10899 tree
10900 build_vl_exp (enum tree_code code, int len MEM_STAT_DECL)
10901 {
10902 tree t;
10903 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10904
10905 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10906 gcc_assert (len >= 1);
10907
10908 record_node_allocation_statistics (code, length);
10909
10910 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10911
10912 TREE_SET_CODE (t, code);
10913
10914 /* Can't use TREE_OPERAND to store the length because if checking is
10915 enabled, it will try to check the length before we store it. :-P */
10916 t->exp.operands[0] = build_int_cst (sizetype, len);
10917
10918 return t;
10919 }
10920
10921 /* Helper function for build_call_* functions; build a CALL_EXPR with
10922 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10923 the argument slots. */
10924
10925 static tree
10926 build_call_1 (tree return_type, tree fn, int nargs)
10927 {
10928 tree t;
10929
10930 t = build_vl_exp (CALL_EXPR, nargs + 3);
10931 TREE_TYPE (t) = return_type;
10932 CALL_EXPR_FN (t) = fn;
10933 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10934
10935 return t;
10936 }
10937
10938 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10939 FN and a null static chain slot. NARGS is the number of call arguments
10940 which are specified as "..." arguments. */
10941
10942 tree
10943 build_call_nary (tree return_type, tree fn, int nargs, ...)
10944 {
10945 tree ret;
10946 va_list args;
10947 va_start (args, nargs);
10948 ret = build_call_valist (return_type, fn, nargs, args);
10949 va_end (args);
10950 return ret;
10951 }
10952
10953 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10954 FN and a null static chain slot. NARGS is the number of call arguments
10955 which are specified as a va_list ARGS. */
10956
10957 tree
10958 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10959 {
10960 tree t;
10961 int i;
10962
10963 t = build_call_1 (return_type, fn, nargs);
10964 for (i = 0; i < nargs; i++)
10965 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10966 process_call_operands (t);
10967 return t;
10968 }
10969
10970 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10971 FN and a null static chain slot. NARGS is the number of call arguments
10972 which are specified as a tree array ARGS. */
10973
10974 tree
10975 build_call_array_loc (location_t loc, tree return_type, tree fn,
10976 int nargs, const tree *args)
10977 {
10978 tree t;
10979 int i;
10980
10981 t = build_call_1 (return_type, fn, nargs);
10982 for (i = 0; i < nargs; i++)
10983 CALL_EXPR_ARG (t, i) = args[i];
10984 process_call_operands (t);
10985 SET_EXPR_LOCATION (t, loc);
10986 return t;
10987 }
10988
10989 /* Like build_call_array, but takes a vec. */
10990
10991 tree
10992 build_call_vec (tree return_type, tree fn, const vec<tree, va_gc> *args)
10993 {
10994 tree ret, t;
10995 unsigned int ix;
10996
10997 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10998 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10999 CALL_EXPR_ARG (ret, ix) = t;
11000 process_call_operands (ret);
11001 return ret;
11002 }
11003
11004 /* Conveniently construct a function call expression. FNDECL names the
11005 function to be called and N arguments are passed in the array
11006 ARGARRAY. */
11007
11008 tree
11009 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
11010 {
11011 tree fntype = TREE_TYPE (fndecl);
11012 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11013
11014 return fold_build_call_array_loc (loc, TREE_TYPE (fntype), fn, n, argarray);
11015 }
11016
11017 /* Conveniently construct a function call expression. FNDECL names the
11018 function to be called and the arguments are passed in the vector
11019 VEC. */
11020
11021 tree
11022 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
11023 {
11024 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
11025 vec_safe_address (vec));
11026 }
11027
11028
11029 /* Conveniently construct a function call expression. FNDECL names the
11030 function to be called, N is the number of arguments, and the "..."
11031 parameters are the argument expressions. */
11032
11033 tree
11034 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
11035 {
11036 va_list ap;
11037 tree *argarray = XALLOCAVEC (tree, n);
11038 int i;
11039
11040 va_start (ap, n);
11041 for (i = 0; i < n; i++)
11042 argarray[i] = va_arg (ap, tree);
11043 va_end (ap);
11044 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11045 }
11046
11047 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11048 varargs macros aren't supported by all bootstrap compilers. */
11049
11050 tree
11051 build_call_expr (tree fndecl, int n, ...)
11052 {
11053 va_list ap;
11054 tree *argarray = XALLOCAVEC (tree, n);
11055 int i;
11056
11057 va_start (ap, n);
11058 for (i = 0; i < n; i++)
11059 argarray[i] = va_arg (ap, tree);
11060 va_end (ap);
11061 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
11062 }
11063
11064 /* Build an internal call to IFN, with arguments ARGS[0:N-1] and with return
11065 type TYPE. This is just like CALL_EXPR, except its CALL_EXPR_FN is NULL.
11066 It will get gimplified later into an ordinary internal function. */
11067
11068 tree
11069 build_call_expr_internal_loc_array (location_t loc, internal_fn ifn,
11070 tree type, int n, const tree *args)
11071 {
11072 tree t = build_call_1 (type, NULL_TREE, n);
11073 for (int i = 0; i < n; ++i)
11074 CALL_EXPR_ARG (t, i) = args[i];
11075 SET_EXPR_LOCATION (t, loc);
11076 CALL_EXPR_IFN (t) = ifn;
11077 process_call_operands (t);
11078 return t;
11079 }
11080
11081 /* Build internal call expression. This is just like CALL_EXPR, except
11082 its CALL_EXPR_FN is NULL. It will get gimplified later into ordinary
11083 internal function. */
11084
11085 tree
11086 build_call_expr_internal_loc (location_t loc, enum internal_fn ifn,
11087 tree type, int n, ...)
11088 {
11089 va_list ap;
11090 tree *argarray = XALLOCAVEC (tree, n);
11091 int i;
11092
11093 va_start (ap, n);
11094 for (i = 0; i < n; i++)
11095 argarray[i] = va_arg (ap, tree);
11096 va_end (ap);
11097 return build_call_expr_internal_loc_array (loc, ifn, type, n, argarray);
11098 }
11099
11100 /* Return a function call to FN, if the target is guaranteed to support it,
11101 or null otherwise.
11102
11103 N is the number of arguments, passed in the "...", and TYPE is the
11104 type of the return value. */
11105
11106 tree
11107 maybe_build_call_expr_loc (location_t loc, combined_fn fn, tree type,
11108 int n, ...)
11109 {
11110 va_list ap;
11111 tree *argarray = XALLOCAVEC (tree, n);
11112 int i;
11113
11114 va_start (ap, n);
11115 for (i = 0; i < n; i++)
11116 argarray[i] = va_arg (ap, tree);
11117 va_end (ap);
11118 if (internal_fn_p (fn))
11119 {
11120 internal_fn ifn = as_internal_fn (fn);
11121 if (direct_internal_fn_p (ifn))
11122 {
11123 tree_pair types = direct_internal_fn_types (ifn, type, argarray);
11124 if (!direct_internal_fn_supported_p (ifn, types,
11125 OPTIMIZE_FOR_BOTH))
11126 return NULL_TREE;
11127 }
11128 return build_call_expr_internal_loc_array (loc, ifn, type, n, argarray);
11129 }
11130 else
11131 {
11132 tree fndecl = builtin_decl_implicit (as_builtin_fn (fn));
11133 if (!fndecl)
11134 return NULL_TREE;
11135 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11136 }
11137 }
11138
11139 /* Return a function call to the appropriate builtin alloca variant.
11140
11141 SIZE is the size to be allocated. ALIGN, if non-zero, is the requested
11142 alignment of the allocated area. MAX_SIZE, if non-negative, is an upper
11143 bound for SIZE in case it is not a fixed value. */
11144
11145 tree
11146 build_alloca_call_expr (tree size, unsigned int align, HOST_WIDE_INT max_size)
11147 {
11148 if (max_size >= 0)
11149 {
11150 tree t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX);
11151 return
11152 build_call_expr (t, 3, size, size_int (align), size_int (max_size));
11153 }
11154 else if (align > 0)
11155 {
11156 tree t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
11157 return build_call_expr (t, 2, size, size_int (align));
11158 }
11159 else
11160 {
11161 tree t = builtin_decl_explicit (BUILT_IN_ALLOCA);
11162 return build_call_expr (t, 1, size);
11163 }
11164 }
11165
11166 /* The built-in decl to use to mark code points believed to be unreachable.
11167 Typically __builtin_unreachable, but __builtin_trap if
11168 -fsanitize=unreachable -fsanitize-trap=unreachable. If only
11169 -fsanitize=unreachable, we rely on sanopt to replace calls with the
11170 appropriate ubsan function. When building a call directly, use
11171 {gimple_},build_builtin_unreachable instead. */
11172
11173 tree
11174 builtin_decl_unreachable ()
11175 {
11176 enum built_in_function fncode = BUILT_IN_UNREACHABLE;
11177
11178 if (sanitize_flags_p (SANITIZE_UNREACHABLE)
11179 ? (flag_sanitize_trap & SANITIZE_UNREACHABLE)
11180 : flag_unreachable_traps)
11181 fncode = BUILT_IN_UNREACHABLE_TRAP;
11182 /* For non-trapping sanitize, we will rewrite __builtin_unreachable () later,
11183 in the sanopt pass. */
11184
11185 return builtin_decl_explicit (fncode);
11186 }
11187
11188 /* Build a call to __builtin_unreachable, possibly rewritten by
11189 -fsanitize=unreachable. Use this rather than the above when practical. */
11190
11191 tree
11192 build_builtin_unreachable (location_t loc)
11193 {
11194 tree data = NULL_TREE;
11195 tree fn = sanitize_unreachable_fn (&data, loc);
11196 return build_call_expr_loc (loc, fn, data != NULL_TREE, data);
11197 }
11198
11199 /* Create a new constant string literal of type ELTYPE[SIZE] (or LEN
11200 if SIZE == -1) and return a tree node representing char* pointer to
11201 it as an ADDR_EXPR (ARRAY_REF (ELTYPE, ...)). When STR is nonnull
11202 the STRING_CST value is the LEN bytes at STR (the representation
11203 of the string, which may be wide). Otherwise it's all zeros. */
11204
11205 tree
11206 build_string_literal (unsigned len, const char *str /* = NULL */,
11207 tree eltype /* = char_type_node */,
11208 unsigned HOST_WIDE_INT size /* = -1 */)
11209 {
11210 tree t = build_string (len, str);
11211 /* Set the maximum valid index based on the string length or SIZE. */
11212 unsigned HOST_WIDE_INT maxidx
11213 = (size == HOST_WIDE_INT_M1U ? len : size) - 1;
11214
11215 tree index = build_index_type (size_int (maxidx));
11216 eltype = build_type_variant (eltype, 1, 0);
11217 tree type = build_array_type (eltype, index);
11218 TREE_TYPE (t) = type;
11219 TREE_CONSTANT (t) = 1;
11220 TREE_READONLY (t) = 1;
11221 TREE_STATIC (t) = 1;
11222
11223 type = build_pointer_type (eltype);
11224 t = build1 (ADDR_EXPR, type,
11225 build4 (ARRAY_REF, eltype,
11226 t, integer_zero_node, NULL_TREE, NULL_TREE));
11227 return t;
11228 }
11229
11230
11231
11232 /* Return true if T (assumed to be a DECL) must be assigned a memory
11233 location. */
11234
11235 bool
11236 needs_to_live_in_memory (const_tree t)
11237 {
11238 return (TREE_ADDRESSABLE (t)
11239 || is_global_var (t)
11240 || (TREE_CODE (t) == RESULT_DECL
11241 && !DECL_BY_REFERENCE (t)
11242 && aggregate_value_p (t, current_function_decl)));
11243 }
11244
11245 /* Return value of a constant X and sign-extend it. */
11246
11247 HOST_WIDE_INT
11248 int_cst_value (const_tree x)
11249 {
11250 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
11251 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
11252
11253 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
11254 gcc_assert (cst_and_fits_in_hwi (x));
11255
11256 if (bits < HOST_BITS_PER_WIDE_INT)
11257 {
11258 bool negative = ((val >> (bits - 1)) & 1) != 0;
11259 if (negative)
11260 val |= HOST_WIDE_INT_M1U << (bits - 1) << 1;
11261 else
11262 val &= ~(HOST_WIDE_INT_M1U << (bits - 1) << 1);
11263 }
11264
11265 return val;
11266 }
11267
11268 /* If TYPE is an integral or pointer type, return an integer type with
11269 the same precision which is unsigned iff UNSIGNEDP is true, or itself
11270 if TYPE is already an integer type of signedness UNSIGNEDP.
11271 If TYPE is a floating-point type, return an integer type with the same
11272 bitsize and with the signedness given by UNSIGNEDP; this is useful
11273 when doing bit-level operations on a floating-point value. */
11274
11275 tree
11276 signed_or_unsigned_type_for (int unsignedp, tree type)
11277 {
11278 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) == unsignedp)
11279 return type;
11280
11281 if (TREE_CODE (type) == VECTOR_TYPE)
11282 {
11283 tree inner = TREE_TYPE (type);
11284 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
11285 if (!inner2)
11286 return NULL_TREE;
11287 if (inner == inner2)
11288 return type;
11289 machine_mode new_mode;
11290 if (VECTOR_MODE_P (TYPE_MODE (type))
11291 && related_int_vector_mode (TYPE_MODE (type)).exists (&new_mode))
11292 return build_vector_type_for_mode (inner2, new_mode);
11293 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
11294 }
11295
11296 if (TREE_CODE (type) == COMPLEX_TYPE)
11297 {
11298 tree inner = TREE_TYPE (type);
11299 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
11300 if (!inner2)
11301 return NULL_TREE;
11302 if (inner == inner2)
11303 return type;
11304 return build_complex_type (inner2);
11305 }
11306
11307 unsigned int bits;
11308 if (INTEGRAL_TYPE_P (type)
11309 || POINTER_TYPE_P (type)
11310 || TREE_CODE (type) == OFFSET_TYPE)
11311 bits = TYPE_PRECISION (type);
11312 else if (TREE_CODE (type) == REAL_TYPE)
11313 bits = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (type));
11314 else
11315 return NULL_TREE;
11316
11317 if (TREE_CODE (type) == BITINT_TYPE && (unsignedp || bits > 1))
11318 return build_bitint_type (bits, unsignedp);
11319 return build_nonstandard_integer_type (bits, unsignedp);
11320 }
11321
11322 /* If TYPE is an integral or pointer type, return an integer type with
11323 the same precision which is unsigned, or itself if TYPE is already an
11324 unsigned integer type. If TYPE is a floating-point type, return an
11325 unsigned integer type with the same bitsize as TYPE. */
11326
11327 tree
11328 unsigned_type_for (tree type)
11329 {
11330 return signed_or_unsigned_type_for (1, type);
11331 }
11332
11333 /* If TYPE is an integral or pointer type, return an integer type with
11334 the same precision which is signed, or itself if TYPE is already a
11335 signed integer type. If TYPE is a floating-point type, return a
11336 signed integer type with the same bitsize as TYPE. */
11337
11338 tree
11339 signed_type_for (tree type)
11340 {
11341 return signed_or_unsigned_type_for (0, type);
11342 }
11343
11344 /* - For VECTOR_TYPEs:
11345 - The truth type must be a VECTOR_BOOLEAN_TYPE.
11346 - The number of elements must match (known_eq).
11347 - targetm.vectorize.get_mask_mode exists, and exactly
11348 the same mode as the truth type.
11349 - Otherwise, the truth type must be a BOOLEAN_TYPE
11350 or useless_type_conversion_p to BOOLEAN_TYPE. */
11351 bool
11352 is_truth_type_for (tree type, tree truth_type)
11353 {
11354 machine_mode mask_mode = TYPE_MODE (truth_type);
11355 machine_mode vmode = TYPE_MODE (type);
11356 machine_mode tmask_mode;
11357
11358 if (TREE_CODE (type) == VECTOR_TYPE)
11359 {
11360 if (VECTOR_BOOLEAN_TYPE_P (truth_type)
11361 && known_eq (TYPE_VECTOR_SUBPARTS (type),
11362 TYPE_VECTOR_SUBPARTS (truth_type))
11363 && targetm.vectorize.get_mask_mode (vmode).exists (&tmask_mode)
11364 && tmask_mode == mask_mode)
11365 return true;
11366
11367 return false;
11368 }
11369
11370 return useless_type_conversion_p (boolean_type_node, truth_type);
11371 }
11372
11373 /* If TYPE is a vector type, return a signed integer vector type with the
11374 same width and number of subparts. Otherwise return boolean_type_node. */
11375
11376 tree
11377 truth_type_for (tree type)
11378 {
11379 if (TREE_CODE (type) == VECTOR_TYPE)
11380 {
11381 if (VECTOR_BOOLEAN_TYPE_P (type))
11382 return type;
11383 return build_truth_vector_type_for (type);
11384 }
11385 else
11386 return boolean_type_node;
11387 }
11388
11389 /* Returns the largest value obtainable by casting something in INNER type to
11390 OUTER type. */
11391
11392 tree
11393 upper_bound_in_type (tree outer, tree inner)
11394 {
11395 unsigned int det = 0;
11396 unsigned oprec = TYPE_PRECISION (outer);
11397 unsigned iprec = TYPE_PRECISION (inner);
11398 unsigned prec;
11399
11400 /* Compute a unique number for every combination. */
11401 det |= (oprec > iprec) ? 4 : 0;
11402 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
11403 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
11404
11405 /* Determine the exponent to use. */
11406 switch (det)
11407 {
11408 case 0:
11409 case 1:
11410 /* oprec <= iprec, outer: signed, inner: don't care. */
11411 prec = oprec - 1;
11412 break;
11413 case 2:
11414 case 3:
11415 /* oprec <= iprec, outer: unsigned, inner: don't care. */
11416 prec = oprec;
11417 break;
11418 case 4:
11419 /* oprec > iprec, outer: signed, inner: signed. */
11420 prec = iprec - 1;
11421 break;
11422 case 5:
11423 /* oprec > iprec, outer: signed, inner: unsigned. */
11424 prec = iprec;
11425 break;
11426 case 6:
11427 /* oprec > iprec, outer: unsigned, inner: signed. */
11428 prec = oprec;
11429 break;
11430 case 7:
11431 /* oprec > iprec, outer: unsigned, inner: unsigned. */
11432 prec = iprec;
11433 break;
11434 default:
11435 gcc_unreachable ();
11436 }
11437
11438 return wide_int_to_tree (outer,
11439 wi::mask (prec, false, TYPE_PRECISION (outer)));
11440 }
11441
11442 /* Returns the smallest value obtainable by casting something in INNER type to
11443 OUTER type. */
11444
11445 tree
11446 lower_bound_in_type (tree outer, tree inner)
11447 {
11448 unsigned oprec = TYPE_PRECISION (outer);
11449 unsigned iprec = TYPE_PRECISION (inner);
11450
11451 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
11452 and obtain 0. */
11453 if (TYPE_UNSIGNED (outer)
11454 /* If we are widening something of an unsigned type, OUTER type
11455 contains all values of INNER type. In particular, both INNER
11456 and OUTER types have zero in common. */
11457 || (oprec > iprec && TYPE_UNSIGNED (inner)))
11458 return build_int_cst (outer, 0);
11459 else
11460 {
11461 /* If we are widening a signed type to another signed type, we
11462 want to obtain -2^^(iprec-1). If we are keeping the
11463 precision or narrowing to a signed type, we want to obtain
11464 -2^(oprec-1). */
11465 unsigned prec = oprec > iprec ? iprec : oprec;
11466 return wide_int_to_tree (outer,
11467 wi::mask (prec - 1, true,
11468 TYPE_PRECISION (outer)));
11469 }
11470 }
11471
11472 /* Return true if two operands that are suitable for PHI nodes are
11473 necessarily equal. Specifically, both ARG0 and ARG1 must be either
11474 SSA_NAME or invariant. Note that this is strictly an optimization.
11475 That is, callers of this function can directly call operand_equal_p
11476 and get the same result, only slower. */
11477
11478 bool
11479 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
11480 {
11481 if (arg0 == arg1)
11482 return true;
11483 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
11484 return false;
11485 return operand_equal_p (arg0, arg1, 0);
11486 }
11487
11488 /* Returns number of zeros at the end of binary representation of X. */
11489
11490 tree
11491 num_ending_zeros (const_tree x)
11492 {
11493 return build_int_cst (TREE_TYPE (x), wi::ctz (wi::to_wide (x)));
11494 }
11495
11496
11497 #define WALK_SUBTREE(NODE) \
11498 do \
11499 { \
11500 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
11501 if (result) \
11502 return result; \
11503 } \
11504 while (0)
11505
11506 /* This is a subroutine of walk_tree that walks field of TYPE that are to
11507 be walked whenever a type is seen in the tree. Rest of operands and return
11508 value are as for walk_tree. */
11509
11510 static tree
11511 walk_type_fields (tree type, walk_tree_fn func, void *data,
11512 hash_set<tree> *pset, walk_tree_lh lh)
11513 {
11514 tree result = NULL_TREE;
11515
11516 switch (TREE_CODE (type))
11517 {
11518 case POINTER_TYPE:
11519 case REFERENCE_TYPE:
11520 case VECTOR_TYPE:
11521 /* We have to worry about mutually recursive pointers. These can't
11522 be written in C. They can in Ada. It's pathological, but
11523 there's an ACATS test (c38102a) that checks it. Deal with this
11524 by checking if we're pointing to another pointer, that one
11525 points to another pointer, that one does too, and we have no htab.
11526 If so, get a hash table. We check three levels deep to avoid
11527 the cost of the hash table if we don't need one. */
11528 if (POINTER_TYPE_P (TREE_TYPE (type))
11529 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
11530 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
11531 && !pset)
11532 {
11533 result = walk_tree_without_duplicates (&TREE_TYPE (type),
11534 func, data);
11535 if (result)
11536 return result;
11537
11538 break;
11539 }
11540
11541 /* fall through */
11542
11543 case COMPLEX_TYPE:
11544 WALK_SUBTREE (TREE_TYPE (type));
11545 break;
11546
11547 case METHOD_TYPE:
11548 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
11549
11550 /* Fall through. */
11551
11552 case FUNCTION_TYPE:
11553 WALK_SUBTREE (TREE_TYPE (type));
11554 {
11555 tree arg;
11556
11557 /* We never want to walk into default arguments. */
11558 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
11559 WALK_SUBTREE (TREE_VALUE (arg));
11560 }
11561 break;
11562
11563 case ARRAY_TYPE:
11564 /* Don't follow this nodes's type if a pointer for fear that
11565 we'll have infinite recursion. If we have a PSET, then we
11566 need not fear. */
11567 if (pset
11568 || (!POINTER_TYPE_P (TREE_TYPE (type))
11569 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
11570 WALK_SUBTREE (TREE_TYPE (type));
11571 WALK_SUBTREE (TYPE_DOMAIN (type));
11572 break;
11573
11574 case OFFSET_TYPE:
11575 WALK_SUBTREE (TREE_TYPE (type));
11576 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
11577 break;
11578
11579 default:
11580 break;
11581 }
11582
11583 return NULL_TREE;
11584 }
11585
11586 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
11587 called with the DATA and the address of each sub-tree. If FUNC returns a
11588 non-NULL value, the traversal is stopped, and the value returned by FUNC
11589 is returned. If PSET is non-NULL it is used to record the nodes visited,
11590 and to avoid visiting a node more than once. */
11591
11592 tree
11593 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
11594 hash_set<tree> *pset, walk_tree_lh lh)
11595 {
11596 #define WALK_SUBTREE_TAIL(NODE) \
11597 do \
11598 { \
11599 tp = & (NODE); \
11600 goto tail_recurse; \
11601 } \
11602 while (0)
11603
11604 tail_recurse:
11605 /* Skip empty subtrees. */
11606 if (!*tp)
11607 return NULL_TREE;
11608
11609 /* Don't walk the same tree twice, if the user has requested
11610 that we avoid doing so. */
11611 if (pset && pset->add (*tp))
11612 return NULL_TREE;
11613
11614 /* Call the function. */
11615 int walk_subtrees = 1;
11616 tree result = (*func) (tp, &walk_subtrees, data);
11617
11618 /* If we found something, return it. */
11619 if (result)
11620 return result;
11621
11622 tree t = *tp;
11623 tree_code code = TREE_CODE (t);
11624
11625 /* Even if we didn't, FUNC may have decided that there was nothing
11626 interesting below this point in the tree. */
11627 if (!walk_subtrees)
11628 {
11629 /* But we still need to check our siblings. */
11630 if (code == TREE_LIST)
11631 WALK_SUBTREE_TAIL (TREE_CHAIN (t));
11632 else if (code == OMP_CLAUSE)
11633 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (t));
11634 else
11635 return NULL_TREE;
11636 }
11637
11638 if (lh)
11639 {
11640 result = (*lh) (tp, &walk_subtrees, func, data, pset);
11641 if (result || !walk_subtrees)
11642 return result;
11643 }
11644
11645 switch (code)
11646 {
11647 case ERROR_MARK:
11648 case IDENTIFIER_NODE:
11649 case INTEGER_CST:
11650 case REAL_CST:
11651 case FIXED_CST:
11652 case STRING_CST:
11653 case BLOCK:
11654 case PLACEHOLDER_EXPR:
11655 case SSA_NAME:
11656 case FIELD_DECL:
11657 case RESULT_DECL:
11658 /* None of these have subtrees other than those already walked
11659 above. */
11660 break;
11661
11662 case TREE_LIST:
11663 WALK_SUBTREE (TREE_VALUE (t));
11664 WALK_SUBTREE_TAIL (TREE_CHAIN (t));
11665
11666 case TREE_VEC:
11667 {
11668 int len = TREE_VEC_LENGTH (t);
11669
11670 if (len == 0)
11671 break;
11672
11673 /* Walk all elements but the last. */
11674 for (int i = 0; i < len - 1; ++i)
11675 WALK_SUBTREE (TREE_VEC_ELT (t, i));
11676
11677 /* Now walk the last one as a tail call. */
11678 WALK_SUBTREE_TAIL (TREE_VEC_ELT (t, len - 1));
11679 }
11680
11681 case VECTOR_CST:
11682 {
11683 unsigned len = vector_cst_encoded_nelts (t);
11684 if (len == 0)
11685 break;
11686 /* Walk all elements but the last. */
11687 for (unsigned i = 0; i < len - 1; ++i)
11688 WALK_SUBTREE (VECTOR_CST_ENCODED_ELT (t, i));
11689 /* Now walk the last one as a tail call. */
11690 WALK_SUBTREE_TAIL (VECTOR_CST_ENCODED_ELT (t, len - 1));
11691 }
11692
11693 case COMPLEX_CST:
11694 WALK_SUBTREE (TREE_REALPART (t));
11695 WALK_SUBTREE_TAIL (TREE_IMAGPART (t));
11696
11697 case CONSTRUCTOR:
11698 {
11699 unsigned HOST_WIDE_INT idx;
11700 constructor_elt *ce;
11701
11702 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce);
11703 idx++)
11704 WALK_SUBTREE (ce->value);
11705 }
11706 break;
11707
11708 case SAVE_EXPR:
11709 WALK_SUBTREE_TAIL (TREE_OPERAND (t, 0));
11710
11711 case BIND_EXPR:
11712 {
11713 tree decl;
11714 for (decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
11715 {
11716 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11717 into declarations that are just mentioned, rather than
11718 declared; they don't really belong to this part of the tree.
11719 And, we can see cycles: the initializer for a declaration
11720 can refer to the declaration itself. */
11721 WALK_SUBTREE (DECL_INITIAL (decl));
11722 WALK_SUBTREE (DECL_SIZE (decl));
11723 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11724 }
11725 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (t));
11726 }
11727
11728 case STATEMENT_LIST:
11729 {
11730 tree_stmt_iterator i;
11731 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
11732 WALK_SUBTREE (*tsi_stmt_ptr (i));
11733 }
11734 break;
11735
11736 case OMP_CLAUSE:
11737 {
11738 int len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
11739 for (int i = 0; i < len; i++)
11740 WALK_SUBTREE (OMP_CLAUSE_OPERAND (t, i));
11741 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (t));
11742 }
11743
11744 case TARGET_EXPR:
11745 {
11746 int i, len;
11747
11748 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11749 But, we only want to walk once. */
11750 len = (TREE_OPERAND (t, 3) == TREE_OPERAND (t, 1)) ? 2 : 3;
11751 for (i = 0; i < len; ++i)
11752 WALK_SUBTREE (TREE_OPERAND (t, i));
11753 WALK_SUBTREE_TAIL (TREE_OPERAND (t, len));
11754 }
11755
11756 case DECL_EXPR:
11757 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11758 defining. We only want to walk into these fields of a type in this
11759 case and not in the general case of a mere reference to the type.
11760
11761 The criterion is as follows: if the field can be an expression, it
11762 must be walked only here. This should be in keeping with the fields
11763 that are directly gimplified in gimplify_type_sizes in order for the
11764 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11765 variable-sized types.
11766
11767 Note that DECLs get walked as part of processing the BIND_EXPR. */
11768 if (TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
11769 {
11770 /* Call the function for the decl so e.g. copy_tree_body_r can
11771 replace it with the remapped one. */
11772 result = (*func) (&DECL_EXPR_DECL (t), &walk_subtrees, data);
11773 if (result || !walk_subtrees)
11774 return result;
11775
11776 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (t));
11777 if (TREE_CODE (*type_p) == ERROR_MARK)
11778 return NULL_TREE;
11779
11780 /* Call the function for the type. See if it returns anything or
11781 doesn't want us to continue. If we are to continue, walk both
11782 the normal fields and those for the declaration case. */
11783 result = (*func) (type_p, &walk_subtrees, data);
11784 if (result || !walk_subtrees)
11785 return result;
11786
11787 tree type = *type_p;
11788
11789 /* But do not walk a pointed-to type since it may itself need to
11790 be walked in the declaration case if it isn't anonymous. */
11791 if (!POINTER_TYPE_P (type))
11792 {
11793 result = walk_type_fields (type, func, data, pset, lh);
11794 if (result)
11795 return result;
11796 }
11797
11798 /* If this is a record type, also walk the fields. */
11799 if (RECORD_OR_UNION_TYPE_P (type))
11800 {
11801 tree field;
11802
11803 for (field = TYPE_FIELDS (type); field;
11804 field = DECL_CHAIN (field))
11805 {
11806 /* We'd like to look at the type of the field, but we can
11807 easily get infinite recursion. So assume it's pointed
11808 to elsewhere in the tree. Also, ignore things that
11809 aren't fields. */
11810 if (TREE_CODE (field) != FIELD_DECL)
11811 continue;
11812
11813 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11814 WALK_SUBTREE (DECL_SIZE (field));
11815 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11816 if (TREE_CODE (type) == QUAL_UNION_TYPE)
11817 WALK_SUBTREE (DECL_QUALIFIER (field));
11818 }
11819 }
11820
11821 /* Same for scalar types. */
11822 else if (TREE_CODE (type) == BOOLEAN_TYPE
11823 || TREE_CODE (type) == ENUMERAL_TYPE
11824 || TREE_CODE (type) == INTEGER_TYPE
11825 || TREE_CODE (type) == FIXED_POINT_TYPE
11826 || TREE_CODE (type) == REAL_TYPE)
11827 {
11828 WALK_SUBTREE (TYPE_MIN_VALUE (type));
11829 WALK_SUBTREE (TYPE_MAX_VALUE (type));
11830 }
11831
11832 WALK_SUBTREE (TYPE_SIZE (type));
11833 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (type));
11834 }
11835 /* FALLTHRU */
11836
11837 default:
11838 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11839 {
11840 int i, len;
11841
11842 /* Walk over all the sub-trees of this operand. */
11843 len = TREE_OPERAND_LENGTH (t);
11844
11845 /* Go through the subtrees. We need to do this in forward order so
11846 that the scope of a FOR_EXPR is handled properly. */
11847 if (len)
11848 {
11849 for (i = 0; i < len - 1; ++i)
11850 WALK_SUBTREE (TREE_OPERAND (t, i));
11851 WALK_SUBTREE_TAIL (TREE_OPERAND (t, len - 1));
11852 }
11853 }
11854 /* If this is a type, walk the needed fields in the type. */
11855 else if (TYPE_P (t))
11856 return walk_type_fields (t, func, data, pset, lh);
11857 break;
11858 }
11859
11860 /* We didn't find what we were looking for. */
11861 return NULL_TREE;
11862
11863 #undef WALK_SUBTREE_TAIL
11864 }
11865 #undef WALK_SUBTREE
11866
11867 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11868
11869 tree
11870 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11871 walk_tree_lh lh)
11872 {
11873 tree result;
11874
11875 hash_set<tree> pset;
11876 result = walk_tree_1 (tp, func, data, &pset, lh);
11877 return result;
11878 }
11879
11880
11881 tree
11882 tree_block (tree t)
11883 {
11884 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11885
11886 if (IS_EXPR_CODE_CLASS (c))
11887 return LOCATION_BLOCK (t->exp.locus);
11888 gcc_unreachable ();
11889 return NULL;
11890 }
11891
11892 void
11893 tree_set_block (tree t, tree b)
11894 {
11895 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11896
11897 if (IS_EXPR_CODE_CLASS (c))
11898 {
11899 t->exp.locus = set_block (t->exp.locus, b);
11900 }
11901 else
11902 gcc_unreachable ();
11903 }
11904
11905 /* Create a nameless artificial label and put it in the current
11906 function context. The label has a location of LOC. Returns the
11907 newly created label. */
11908
11909 tree
11910 create_artificial_label (location_t loc)
11911 {
11912 tree lab = build_decl (loc,
11913 LABEL_DECL, NULL_TREE, void_type_node);
11914
11915 DECL_ARTIFICIAL (lab) = 1;
11916 DECL_IGNORED_P (lab) = 1;
11917 DECL_CONTEXT (lab) = current_function_decl;
11918 return lab;
11919 }
11920
11921 /* Given a tree, try to return a useful variable name that we can use
11922 to prefix a temporary that is being assigned the value of the tree.
11923 I.E. given <temp> = &A, return A. */
11924
11925 const char *
11926 get_name (tree t)
11927 {
11928 tree stripped_decl;
11929
11930 stripped_decl = t;
11931 STRIP_NOPS (stripped_decl);
11932 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11933 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11934 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11935 {
11936 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11937 if (!name)
11938 return NULL;
11939 return IDENTIFIER_POINTER (name);
11940 }
11941 else
11942 {
11943 switch (TREE_CODE (stripped_decl))
11944 {
11945 case ADDR_EXPR:
11946 return get_name (TREE_OPERAND (stripped_decl, 0));
11947 default:
11948 return NULL;
11949 }
11950 }
11951 }
11952
11953 /* Return true if TYPE has a variable argument list. */
11954
11955 bool
11956 stdarg_p (const_tree fntype)
11957 {
11958 function_args_iterator args_iter;
11959 tree n = NULL_TREE, t;
11960
11961 if (!fntype)
11962 return false;
11963
11964 if (TYPE_NO_NAMED_ARGS_STDARG_P (fntype))
11965 return true;
11966
11967 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
11968 {
11969 n = t;
11970 }
11971
11972 return n != NULL_TREE && n != void_type_node;
11973 }
11974
11975 /* Return true if TYPE has a prototype. */
11976
11977 bool
11978 prototype_p (const_tree fntype)
11979 {
11980 tree t;
11981
11982 gcc_assert (fntype != NULL_TREE);
11983
11984 if (TYPE_NO_NAMED_ARGS_STDARG_P (fntype))
11985 return true;
11986
11987 t = TYPE_ARG_TYPES (fntype);
11988 return (t != NULL_TREE);
11989 }
11990
11991 /* If BLOCK is inlined from an __attribute__((__artificial__))
11992 routine, return pointer to location from where it has been
11993 called. */
11994 location_t *
11995 block_nonartificial_location (tree block)
11996 {
11997 location_t *ret = NULL;
11998
11999 while (block && TREE_CODE (block) == BLOCK
12000 && BLOCK_ABSTRACT_ORIGIN (block))
12001 {
12002 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
12003 if (TREE_CODE (ao) == FUNCTION_DECL)
12004 {
12005 /* If AO is an artificial inline, point RET to the
12006 call site locus at which it has been inlined and continue
12007 the loop, in case AO's caller is also an artificial
12008 inline. */
12009 if (DECL_DECLARED_INLINE_P (ao)
12010 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
12011 ret = &BLOCK_SOURCE_LOCATION (block);
12012 else
12013 break;
12014 }
12015 else if (TREE_CODE (ao) != BLOCK)
12016 break;
12017
12018 block = BLOCK_SUPERCONTEXT (block);
12019 }
12020 return ret;
12021 }
12022
12023
12024 /* If EXP is inlined from an __attribute__((__artificial__))
12025 function, return the location of the original call expression. */
12026
12027 location_t
12028 tree_nonartificial_location (tree exp)
12029 {
12030 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
12031
12032 if (loc)
12033 return *loc;
12034 else
12035 return EXPR_LOCATION (exp);
12036 }
12037
12038 /* Return the location into which EXP has been inlined. Analogous
12039 to tree_nonartificial_location() above but not limited to artificial
12040 functions declared inline. If SYSTEM_HEADER is true, return
12041 the macro expansion point of the location if it's in a system header */
12042
12043 location_t
12044 tree_inlined_location (tree exp, bool system_header /* = true */)
12045 {
12046 location_t loc = UNKNOWN_LOCATION;
12047
12048 tree block = TREE_BLOCK (exp);
12049
12050 while (block && TREE_CODE (block) == BLOCK
12051 && BLOCK_ABSTRACT_ORIGIN (block))
12052 {
12053 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
12054 if (TREE_CODE (ao) == FUNCTION_DECL)
12055 loc = BLOCK_SOURCE_LOCATION (block);
12056 else if (TREE_CODE (ao) != BLOCK)
12057 break;
12058
12059 block = BLOCK_SUPERCONTEXT (block);
12060 }
12061
12062 if (loc == UNKNOWN_LOCATION)
12063 {
12064 loc = EXPR_LOCATION (exp);
12065 if (system_header)
12066 /* Only consider macro expansion when the block traversal failed
12067 to find a location. Otherwise it's not relevant. */
12068 return expansion_point_location_if_in_system_header (loc);
12069 }
12070
12071 return loc;
12072 }
12073
12074 /* These are the hash table functions for the hash table of OPTIMIZATION_NODE
12075 nodes. */
12076
12077 /* Return the hash code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
12078
12079 hashval_t
12080 cl_option_hasher::hash (tree x)
12081 {
12082 const_tree const t = x;
12083
12084 if (TREE_CODE (t) == OPTIMIZATION_NODE)
12085 return cl_optimization_hash (TREE_OPTIMIZATION (t));
12086 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
12087 return cl_target_option_hash (TREE_TARGET_OPTION (t));
12088 else
12089 gcc_unreachable ();
12090 }
12091
12092 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
12093 TARGET_OPTION tree node) is the same as that given by *Y, which is the
12094 same. */
12095
12096 bool
12097 cl_option_hasher::equal (tree x, tree y)
12098 {
12099 const_tree const xt = x;
12100 const_tree const yt = y;
12101
12102 if (TREE_CODE (xt) != TREE_CODE (yt))
12103 return false;
12104
12105 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
12106 return cl_optimization_option_eq (TREE_OPTIMIZATION (xt),
12107 TREE_OPTIMIZATION (yt));
12108 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
12109 return cl_target_option_eq (TREE_TARGET_OPTION (xt),
12110 TREE_TARGET_OPTION (yt));
12111 else
12112 gcc_unreachable ();
12113 }
12114
12115 /* Build an OPTIMIZATION_NODE based on the options in OPTS and OPTS_SET. */
12116
12117 tree
12118 build_optimization_node (struct gcc_options *opts,
12119 struct gcc_options *opts_set)
12120 {
12121 tree t;
12122
12123 /* Use the cache of optimization nodes. */
12124
12125 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
12126 opts, opts_set);
12127
12128 tree *slot = cl_option_hash_table->find_slot (cl_optimization_node, INSERT);
12129 t = *slot;
12130 if (!t)
12131 {
12132 /* Insert this one into the hash table. */
12133 t = cl_optimization_node;
12134 *slot = t;
12135
12136 /* Make a new node for next time round. */
12137 cl_optimization_node = make_node (OPTIMIZATION_NODE);
12138 }
12139
12140 return t;
12141 }
12142
12143 /* Build a TARGET_OPTION_NODE based on the options in OPTS and OPTS_SET. */
12144
12145 tree
12146 build_target_option_node (struct gcc_options *opts,
12147 struct gcc_options *opts_set)
12148 {
12149 tree t;
12150
12151 /* Use the cache of optimization nodes. */
12152
12153 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
12154 opts, opts_set);
12155
12156 tree *slot = cl_option_hash_table->find_slot (cl_target_option_node, INSERT);
12157 t = *slot;
12158 if (!t)
12159 {
12160 /* Insert this one into the hash table. */
12161 t = cl_target_option_node;
12162 *slot = t;
12163
12164 /* Make a new node for next time round. */
12165 cl_target_option_node = make_node (TARGET_OPTION_NODE);
12166 }
12167
12168 return t;
12169 }
12170
12171 /* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
12172 so that they aren't saved during PCH writing. */
12173
12174 void
12175 prepare_target_option_nodes_for_pch (void)
12176 {
12177 hash_table<cl_option_hasher>::iterator iter = cl_option_hash_table->begin ();
12178 for (; iter != cl_option_hash_table->end (); ++iter)
12179 if (TREE_CODE (*iter) == TARGET_OPTION_NODE)
12180 TREE_TARGET_GLOBALS (*iter) = NULL;
12181 }
12182
12183 /* Determine the "ultimate origin" of a block. */
12184
12185 tree
12186 block_ultimate_origin (const_tree block)
12187 {
12188 tree origin = BLOCK_ABSTRACT_ORIGIN (block);
12189
12190 if (origin == NULL_TREE)
12191 return NULL_TREE;
12192 else
12193 {
12194 gcc_checking_assert ((DECL_P (origin)
12195 && DECL_ORIGIN (origin) == origin)
12196 || BLOCK_ORIGIN (origin) == origin);
12197 return origin;
12198 }
12199 }
12200
12201 /* Return true iff conversion from INNER_TYPE to OUTER_TYPE generates
12202 no instruction. */
12203
12204 bool
12205 tree_nop_conversion_p (const_tree outer_type, const_tree inner_type)
12206 {
12207 /* Do not strip casts into or out of differing address spaces. */
12208 if (POINTER_TYPE_P (outer_type)
12209 && TYPE_ADDR_SPACE (TREE_TYPE (outer_type)) != ADDR_SPACE_GENERIC)
12210 {
12211 if (!POINTER_TYPE_P (inner_type)
12212 || (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
12213 != TYPE_ADDR_SPACE (TREE_TYPE (inner_type))))
12214 return false;
12215 }
12216 else if (POINTER_TYPE_P (inner_type)
12217 && TYPE_ADDR_SPACE (TREE_TYPE (inner_type)) != ADDR_SPACE_GENERIC)
12218 {
12219 /* We already know that outer_type is not a pointer with
12220 a non-generic address space. */
12221 return false;
12222 }
12223
12224 /* Use precision rather then machine mode when we can, which gives
12225 the correct answer even for submode (bit-field) types. */
12226 if ((INTEGRAL_TYPE_P (outer_type)
12227 || POINTER_TYPE_P (outer_type)
12228 || TREE_CODE (outer_type) == OFFSET_TYPE)
12229 && (INTEGRAL_TYPE_P (inner_type)
12230 || POINTER_TYPE_P (inner_type)
12231 || TREE_CODE (inner_type) == OFFSET_TYPE))
12232 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
12233
12234 /* Otherwise fall back on comparing machine modes (e.g. for
12235 aggregate types, floats). */
12236 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
12237 }
12238
12239 /* Return true iff conversion in EXP generates no instruction. Mark
12240 it inline so that we fully inline into the stripping functions even
12241 though we have two uses of this function. */
12242
12243 static inline bool
12244 tree_nop_conversion (const_tree exp)
12245 {
12246 tree outer_type, inner_type;
12247
12248 if (location_wrapper_p (exp))
12249 return true;
12250 if (!CONVERT_EXPR_P (exp)
12251 && TREE_CODE (exp) != NON_LVALUE_EXPR)
12252 return false;
12253
12254 outer_type = TREE_TYPE (exp);
12255 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
12256 if (!inner_type || inner_type == error_mark_node)
12257 return false;
12258
12259 return tree_nop_conversion_p (outer_type, inner_type);
12260 }
12261
12262 /* Return true iff conversion in EXP generates no instruction. Don't
12263 consider conversions changing the signedness. */
12264
12265 static bool
12266 tree_sign_nop_conversion (const_tree exp)
12267 {
12268 tree outer_type, inner_type;
12269
12270 if (!tree_nop_conversion (exp))
12271 return false;
12272
12273 outer_type = TREE_TYPE (exp);
12274 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
12275
12276 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
12277 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
12278 }
12279
12280 /* Strip conversions from EXP according to tree_nop_conversion and
12281 return the resulting expression. */
12282
12283 tree
12284 tree_strip_nop_conversions (tree exp)
12285 {
12286 while (tree_nop_conversion (exp))
12287 exp = TREE_OPERAND (exp, 0);
12288 return exp;
12289 }
12290
12291 /* Strip conversions from EXP according to tree_sign_nop_conversion
12292 and return the resulting expression. */
12293
12294 tree
12295 tree_strip_sign_nop_conversions (tree exp)
12296 {
12297 while (tree_sign_nop_conversion (exp))
12298 exp = TREE_OPERAND (exp, 0);
12299 return exp;
12300 }
12301
12302 /* Avoid any floating point extensions from EXP. */
12303 tree
12304 strip_float_extensions (tree exp)
12305 {
12306 tree sub, expt, subt;
12307
12308 /* For floating point constant look up the narrowest type that can hold
12309 it properly and handle it like (type)(narrowest_type)constant.
12310 This way we can optimize for instance a=a*2.0 where "a" is float
12311 but 2.0 is double constant. */
12312 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
12313 {
12314 REAL_VALUE_TYPE orig;
12315 tree type = NULL;
12316
12317 orig = TREE_REAL_CST (exp);
12318 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
12319 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
12320 type = float_type_node;
12321 else if (TYPE_PRECISION (TREE_TYPE (exp))
12322 > TYPE_PRECISION (double_type_node)
12323 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
12324 type = double_type_node;
12325 if (type)
12326 return build_real_truncate (type, orig);
12327 }
12328
12329 if (!CONVERT_EXPR_P (exp))
12330 return exp;
12331
12332 sub = TREE_OPERAND (exp, 0);
12333 subt = TREE_TYPE (sub);
12334 expt = TREE_TYPE (exp);
12335
12336 if (!FLOAT_TYPE_P (subt))
12337 return exp;
12338
12339 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
12340 return exp;
12341
12342 if (element_precision (subt) > element_precision (expt))
12343 return exp;
12344
12345 return strip_float_extensions (sub);
12346 }
12347
12348 /* Strip out all handled components that produce invariant
12349 offsets. */
12350
12351 const_tree
12352 strip_invariant_refs (const_tree op)
12353 {
12354 while (handled_component_p (op))
12355 {
12356 switch (TREE_CODE (op))
12357 {
12358 case ARRAY_REF:
12359 case ARRAY_RANGE_REF:
12360 if (!is_gimple_constant (TREE_OPERAND (op, 1))
12361 || TREE_OPERAND (op, 2) != NULL_TREE
12362 || TREE_OPERAND (op, 3) != NULL_TREE)
12363 return NULL;
12364 break;
12365
12366 case COMPONENT_REF:
12367 if (TREE_OPERAND (op, 2) != NULL_TREE)
12368 return NULL;
12369 break;
12370
12371 default:;
12372 }
12373 op = TREE_OPERAND (op, 0);
12374 }
12375
12376 return op;
12377 }
12378
12379 /* Strip handled components with zero offset from OP. */
12380
12381 tree
12382 strip_zero_offset_components (tree op)
12383 {
12384 while (TREE_CODE (op) == COMPONENT_REF
12385 && integer_zerop (DECL_FIELD_OFFSET (TREE_OPERAND (op, 1)))
12386 && integer_zerop (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (op, 1))))
12387 op = TREE_OPERAND (op, 0);
12388 return op;
12389 }
12390
12391 static GTY(()) tree gcc_eh_personality_decl;
12392
12393 /* Return the GCC personality function decl. */
12394
12395 tree
12396 lhd_gcc_personality (void)
12397 {
12398 if (!gcc_eh_personality_decl)
12399 gcc_eh_personality_decl = build_personality_function ("gcc");
12400 return gcc_eh_personality_decl;
12401 }
12402
12403 /* TARGET is a call target of GIMPLE call statement
12404 (obtained by gimple_call_fn). Return true if it is
12405 OBJ_TYPE_REF representing an virtual call of C++ method.
12406 (As opposed to OBJ_TYPE_REF representing objc calls
12407 through a cast where middle-end devirtualization machinery
12408 can't apply.) FOR_DUMP_P is true when being called from
12409 the dump routines. */
12410
12411 bool
12412 virtual_method_call_p (const_tree target, bool for_dump_p)
12413 {
12414 if (TREE_CODE (target) != OBJ_TYPE_REF)
12415 return false;
12416 tree t = TREE_TYPE (target);
12417 gcc_checking_assert (TREE_CODE (t) == POINTER_TYPE);
12418 t = TREE_TYPE (t);
12419 if (TREE_CODE (t) == FUNCTION_TYPE)
12420 return false;
12421 gcc_checking_assert (TREE_CODE (t) == METHOD_TYPE);
12422 /* If we do not have BINFO associated, it means that type was built
12423 without devirtualization enabled. Do not consider this a virtual
12424 call. */
12425 if (!TYPE_BINFO (obj_type_ref_class (target, for_dump_p)))
12426 return false;
12427 return true;
12428 }
12429
12430 /* Lookup sub-BINFO of BINFO of TYPE at offset POS. */
12431
12432 static tree
12433 lookup_binfo_at_offset (tree binfo, tree type, HOST_WIDE_INT pos)
12434 {
12435 unsigned int i;
12436 tree base_binfo, b;
12437
12438 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12439 if (pos == tree_to_shwi (BINFO_OFFSET (base_binfo))
12440 && types_same_for_odr (TREE_TYPE (base_binfo), type))
12441 return base_binfo;
12442 else if ((b = lookup_binfo_at_offset (base_binfo, type, pos)) != NULL)
12443 return b;
12444 return NULL;
12445 }
12446
12447 /* Try to find a base info of BINFO that would have its field decl at offset
12448 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
12449 found, return, otherwise return NULL_TREE. */
12450
12451 tree
12452 get_binfo_at_offset (tree binfo, poly_int64 offset, tree expected_type)
12453 {
12454 tree type = BINFO_TYPE (binfo);
12455
12456 while (true)
12457 {
12458 HOST_WIDE_INT pos, size;
12459 tree fld;
12460 int i;
12461
12462 if (types_same_for_odr (type, expected_type))
12463 return binfo;
12464 if (maybe_lt (offset, 0))
12465 return NULL_TREE;
12466
12467 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
12468 {
12469 if (TREE_CODE (fld) != FIELD_DECL || !DECL_ARTIFICIAL (fld))
12470 continue;
12471
12472 pos = int_bit_position (fld);
12473 size = tree_to_uhwi (DECL_SIZE (fld));
12474 if (known_in_range_p (offset, pos, size))
12475 break;
12476 }
12477 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
12478 return NULL_TREE;
12479
12480 /* Offset 0 indicates the primary base, whose vtable contents are
12481 represented in the binfo for the derived class. */
12482 else if (maybe_ne (offset, 0))
12483 {
12484 tree found_binfo = NULL, base_binfo;
12485 /* Offsets in BINFO are in bytes relative to the whole structure
12486 while POS is in bits relative to the containing field. */
12487 int binfo_offset = (tree_to_shwi (BINFO_OFFSET (binfo)) + pos
12488 / BITS_PER_UNIT);
12489
12490 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12491 if (tree_to_shwi (BINFO_OFFSET (base_binfo)) == binfo_offset
12492 && types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
12493 {
12494 found_binfo = base_binfo;
12495 break;
12496 }
12497 if (found_binfo)
12498 binfo = found_binfo;
12499 else
12500 binfo = lookup_binfo_at_offset (binfo, TREE_TYPE (fld),
12501 binfo_offset);
12502 }
12503
12504 type = TREE_TYPE (fld);
12505 offset -= pos;
12506 }
12507 }
12508
12509 /* PR 84195: Replace control characters in "unescaped" with their
12510 escaped equivalents. Allow newlines if -fmessage-length has
12511 been set to a non-zero value. This is done here, rather than
12512 where the attribute is recorded as the message length can
12513 change between these two locations. */
12514
12515 void
12516 escaped_string::escape (const char *unescaped)
12517 {
12518 char *escaped;
12519 size_t i, new_i, len;
12520
12521 if (m_owned)
12522 free (m_str);
12523
12524 m_str = const_cast<char *> (unescaped);
12525 m_owned = false;
12526
12527 if (unescaped == NULL || *unescaped == 0)
12528 return;
12529
12530 len = strlen (unescaped);
12531 escaped = NULL;
12532 new_i = 0;
12533
12534 for (i = 0; i < len; i++)
12535 {
12536 char c = unescaped[i];
12537
12538 if (!ISCNTRL (c))
12539 {
12540 if (escaped)
12541 escaped[new_i++] = c;
12542 continue;
12543 }
12544
12545 if (c != '\n' || !pp_is_wrapping_line (global_dc->get_reference_printer ()))
12546 {
12547 if (escaped == NULL)
12548 {
12549 /* We only allocate space for a new string if we
12550 actually encounter a control character that
12551 needs replacing. */
12552 escaped = (char *) xmalloc (len * 2 + 1);
12553 strncpy (escaped, unescaped, i);
12554 new_i = i;
12555 }
12556
12557 escaped[new_i++] = '\\';
12558
12559 switch (c)
12560 {
12561 case '\a': escaped[new_i++] = 'a'; break;
12562 case '\b': escaped[new_i++] = 'b'; break;
12563 case '\f': escaped[new_i++] = 'f'; break;
12564 case '\n': escaped[new_i++] = 'n'; break;
12565 case '\r': escaped[new_i++] = 'r'; break;
12566 case '\t': escaped[new_i++] = 't'; break;
12567 case '\v': escaped[new_i++] = 'v'; break;
12568 default: escaped[new_i++] = '?'; break;
12569 }
12570 }
12571 else if (escaped)
12572 escaped[new_i++] = c;
12573 }
12574
12575 if (escaped)
12576 {
12577 escaped[new_i] = 0;
12578 m_str = escaped;
12579 m_owned = true;
12580 }
12581 }
12582
12583 /* Warn about a use of an identifier which was marked deprecated. Returns
12584 whether a warning was given. */
12585
12586 bool
12587 warn_deprecated_use (tree node, tree attr)
12588 {
12589 escaped_string msg;
12590
12591 if (node == 0 || !warn_deprecated_decl)
12592 return false;
12593
12594 if (!attr)
12595 {
12596 if (DECL_P (node))
12597 attr = DECL_ATTRIBUTES (node);
12598 else if (TYPE_P (node))
12599 {
12600 tree decl = TYPE_STUB_DECL (node);
12601 if (decl)
12602 attr = TYPE_ATTRIBUTES (TREE_TYPE (decl));
12603 else if ((decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (node)))
12604 != NULL_TREE)
12605 {
12606 node = TREE_TYPE (decl);
12607 attr = TYPE_ATTRIBUTES (node);
12608 }
12609 }
12610 }
12611
12612 if (attr)
12613 attr = lookup_attribute ("deprecated", attr);
12614
12615 if (attr)
12616 msg.escape (TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
12617
12618 bool w = false;
12619 if (DECL_P (node))
12620 {
12621 auto_diagnostic_group d;
12622 if (msg)
12623 w = warning (OPT_Wdeprecated_declarations,
12624 "%qD is deprecated: %s", node, (const char *) msg);
12625 else
12626 w = warning (OPT_Wdeprecated_declarations,
12627 "%qD is deprecated", node);
12628 if (w)
12629 inform (DECL_SOURCE_LOCATION (node), "declared here");
12630 }
12631 else if (TYPE_P (node))
12632 {
12633 tree what = NULL_TREE;
12634 tree decl = TYPE_STUB_DECL (node);
12635
12636 if (TYPE_NAME (node))
12637 {
12638 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12639 what = TYPE_NAME (node);
12640 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12641 && DECL_NAME (TYPE_NAME (node)))
12642 what = DECL_NAME (TYPE_NAME (node));
12643 }
12644
12645 auto_diagnostic_group d;
12646 if (what)
12647 {
12648 if (msg)
12649 w = warning (OPT_Wdeprecated_declarations,
12650 "%qE is deprecated: %s", what, (const char *) msg);
12651 else
12652 w = warning (OPT_Wdeprecated_declarations,
12653 "%qE is deprecated", what);
12654 }
12655 else
12656 {
12657 if (msg)
12658 w = warning (OPT_Wdeprecated_declarations,
12659 "type is deprecated: %s", (const char *) msg);
12660 else
12661 w = warning (OPT_Wdeprecated_declarations,
12662 "type is deprecated");
12663 }
12664
12665 if (w && decl)
12666 inform (DECL_SOURCE_LOCATION (decl), "declared here");
12667 }
12668
12669 return w;
12670 }
12671
12672 /* Error out with an identifier which was marked 'unavailable'. */
12673 void
12674 error_unavailable_use (tree node, tree attr)
12675 {
12676 escaped_string msg;
12677
12678 if (node == 0)
12679 return;
12680
12681 if (!attr)
12682 {
12683 if (DECL_P (node))
12684 attr = DECL_ATTRIBUTES (node);
12685 else if (TYPE_P (node))
12686 {
12687 tree decl = TYPE_STUB_DECL (node);
12688 if (decl)
12689 attr = lookup_attribute ("unavailable",
12690 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
12691 }
12692 }
12693
12694 if (attr)
12695 attr = lookup_attribute ("unavailable", attr);
12696
12697 if (attr)
12698 msg.escape (TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
12699
12700 if (DECL_P (node))
12701 {
12702 auto_diagnostic_group d;
12703 if (msg)
12704 error ("%qD is unavailable: %s", node, (const char *) msg);
12705 else
12706 error ("%qD is unavailable", node);
12707 inform (DECL_SOURCE_LOCATION (node), "declared here");
12708 }
12709 else if (TYPE_P (node))
12710 {
12711 tree what = NULL_TREE;
12712 tree decl = TYPE_STUB_DECL (node);
12713
12714 if (TYPE_NAME (node))
12715 {
12716 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12717 what = TYPE_NAME (node);
12718 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12719 && DECL_NAME (TYPE_NAME (node)))
12720 what = DECL_NAME (TYPE_NAME (node));
12721 }
12722
12723 auto_diagnostic_group d;
12724 if (what)
12725 {
12726 if (msg)
12727 error ("%qE is unavailable: %s", what, (const char *) msg);
12728 else
12729 error ("%qE is unavailable", what);
12730 }
12731 else
12732 {
12733 if (msg)
12734 error ("type is unavailable: %s", (const char *) msg);
12735 else
12736 error ("type is unavailable");
12737 }
12738
12739 if (decl)
12740 inform (DECL_SOURCE_LOCATION (decl), "declared here");
12741 }
12742 }
12743
12744 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12745 somewhere in it. */
12746
12747 bool
12748 contains_bitfld_component_ref_p (const_tree ref)
12749 {
12750 while (handled_component_p (ref))
12751 {
12752 if (TREE_CODE (ref) == COMPONENT_REF
12753 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12754 return true;
12755 ref = TREE_OPERAND (ref, 0);
12756 }
12757
12758 return false;
12759 }
12760
12761 /* Try to determine whether a TRY_CATCH expression can fall through.
12762 This is a subroutine of block_may_fallthru. */
12763
12764 static bool
12765 try_catch_may_fallthru (const_tree stmt)
12766 {
12767 tree_stmt_iterator i;
12768
12769 /* If the TRY block can fall through, the whole TRY_CATCH can
12770 fall through. */
12771 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
12772 return true;
12773
12774 switch (TREE_CODE (TREE_OPERAND (stmt, 1)))
12775 {
12776 case CATCH_EXPR:
12777 /* See below. */
12778 return block_may_fallthru (CATCH_BODY (TREE_OPERAND (stmt, 1)));
12779
12780 case EH_FILTER_EXPR:
12781 /* See below. */
12782 return block_may_fallthru (EH_FILTER_FAILURE (TREE_OPERAND (stmt, 1)));
12783
12784 case STATEMENT_LIST:
12785 break;
12786
12787 default:
12788 /* See below. */
12789 return false;
12790 }
12791
12792 i = tsi_start (TREE_OPERAND (stmt, 1));
12793 switch (TREE_CODE (tsi_stmt (i)))
12794 {
12795 case CATCH_EXPR:
12796 /* We expect to see a sequence of CATCH_EXPR trees, each with a
12797 catch expression and a body. The whole TRY_CATCH may fall
12798 through iff any of the catch bodies falls through. */
12799 for (; !tsi_end_p (i); tsi_next (&i))
12800 {
12801 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
12802 return true;
12803 }
12804 return false;
12805
12806 case EH_FILTER_EXPR:
12807 /* The exception filter expression only matters if there is an
12808 exception. If the exception does not match EH_FILTER_TYPES,
12809 we will execute EH_FILTER_FAILURE, and we will fall through
12810 if that falls through. If the exception does match
12811 EH_FILTER_TYPES, the stack unwinder will continue up the
12812 stack, so we will not fall through. We don't know whether we
12813 will throw an exception which matches EH_FILTER_TYPES or not,
12814 so we just ignore EH_FILTER_TYPES and assume that we might
12815 throw an exception which doesn't match. */
12816 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
12817
12818 default:
12819 /* This case represents statements to be executed when an
12820 exception occurs. Those statements are implicitly followed
12821 by a RESX statement to resume execution after the exception.
12822 So in this case the TRY_CATCH never falls through. */
12823 return false;
12824 }
12825 }
12826
12827 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
12828 need not be 100% accurate; simply be conservative and return true if we
12829 don't know. This is used only to avoid stupidly generating extra code.
12830 If we're wrong, we'll just delete the extra code later. */
12831
12832 bool
12833 block_may_fallthru (const_tree block)
12834 {
12835 /* This CONST_CAST is okay because expr_last returns its argument
12836 unmodified and we assign it to a const_tree. */
12837 const_tree stmt = expr_last (CONST_CAST_TREE (block));
12838
12839 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
12840 {
12841 case GOTO_EXPR:
12842 case RETURN_EXPR:
12843 /* Easy cases. If the last statement of the block implies
12844 control transfer, then we can't fall through. */
12845 return false;
12846
12847 case SWITCH_EXPR:
12848 /* If there is a default: label or case labels cover all possible
12849 SWITCH_COND values, then the SWITCH_EXPR will transfer control
12850 to some case label in all cases and all we care is whether the
12851 SWITCH_BODY falls through. */
12852 if (SWITCH_ALL_CASES_P (stmt))
12853 return block_may_fallthru (SWITCH_BODY (stmt));
12854 return true;
12855
12856 case COND_EXPR:
12857 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
12858 return true;
12859 return block_may_fallthru (COND_EXPR_ELSE (stmt));
12860
12861 case BIND_EXPR:
12862 return block_may_fallthru (BIND_EXPR_BODY (stmt));
12863
12864 case TRY_CATCH_EXPR:
12865 return try_catch_may_fallthru (stmt);
12866
12867 case TRY_FINALLY_EXPR:
12868 /* The finally clause is always executed after the try clause,
12869 so if it does not fall through, then the try-finally will not
12870 fall through. Otherwise, if the try clause does not fall
12871 through, then when the finally clause falls through it will
12872 resume execution wherever the try clause was going. So the
12873 whole try-finally will only fall through if both the try
12874 clause and the finally clause fall through. */
12875 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
12876 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
12877
12878 case EH_ELSE_EXPR:
12879 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12880
12881 case MODIFY_EXPR:
12882 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
12883 stmt = TREE_OPERAND (stmt, 1);
12884 else
12885 return true;
12886 /* FALLTHRU */
12887
12888 case CALL_EXPR:
12889 /* Functions that do not return do not fall through. */
12890 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
12891
12892 case CLEANUP_POINT_EXPR:
12893 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12894
12895 case TARGET_EXPR:
12896 return block_may_fallthru (TREE_OPERAND (stmt, 1));
12897
12898 case ERROR_MARK:
12899 return true;
12900
12901 default:
12902 return lang_hooks.block_may_fallthru (stmt);
12903 }
12904 }
12905
12906 /* True if we are using EH to handle cleanups. */
12907 static bool using_eh_for_cleanups_flag = false;
12908
12909 /* This routine is called from front ends to indicate eh should be used for
12910 cleanups. */
12911 void
12912 using_eh_for_cleanups (void)
12913 {
12914 using_eh_for_cleanups_flag = true;
12915 }
12916
12917 /* Query whether EH is used for cleanups. */
12918 bool
12919 using_eh_for_cleanups_p (void)
12920 {
12921 return using_eh_for_cleanups_flag;
12922 }
12923
12924 /* Wrapper for tree_code_name to ensure that tree code is valid */
12925 const char *
12926 get_tree_code_name (enum tree_code code)
12927 {
12928 const char *invalid = "<invalid tree code>";
12929
12930 /* The tree_code enum promotes to signed, but we could be getting
12931 invalid values, so force an unsigned comparison. */
12932 if (unsigned (code) >= MAX_TREE_CODES)
12933 {
12934 if ((unsigned)code == 0xa5a5)
12935 return "ggc_freed";
12936 return invalid;
12937 }
12938
12939 return tree_code_name[code];
12940 }
12941
12942 /* Drops the TREE_OVERFLOW flag from T. */
12943
12944 tree
12945 drop_tree_overflow (tree t)
12946 {
12947 gcc_checking_assert (TREE_OVERFLOW (t));
12948
12949 /* For tree codes with a sharing machinery re-build the result. */
12950 if (poly_int_tree_p (t))
12951 return wide_int_to_tree (TREE_TYPE (t), wi::to_poly_wide (t));
12952
12953 /* For VECTOR_CST, remove the overflow bits from the encoded elements
12954 and canonicalize the result. */
12955 if (TREE_CODE (t) == VECTOR_CST)
12956 {
12957 tree_vector_builder builder;
12958 builder.new_unary_operation (TREE_TYPE (t), t, true);
12959 unsigned int count = builder.encoded_nelts ();
12960 for (unsigned int i = 0; i < count; ++i)
12961 {
12962 tree elt = VECTOR_CST_ELT (t, i);
12963 if (TREE_OVERFLOW (elt))
12964 elt = drop_tree_overflow (elt);
12965 builder.quick_push (elt);
12966 }
12967 return builder.build ();
12968 }
12969
12970 /* Otherwise, as all tcc_constants are possibly shared, copy the node
12971 and drop the flag. */
12972 t = copy_node (t);
12973 TREE_OVERFLOW (t) = 0;
12974
12975 /* For constants that contain nested constants, drop the flag
12976 from those as well. */
12977 if (TREE_CODE (t) == COMPLEX_CST)
12978 {
12979 if (TREE_OVERFLOW (TREE_REALPART (t)))
12980 TREE_REALPART (t) = drop_tree_overflow (TREE_REALPART (t));
12981 if (TREE_OVERFLOW (TREE_IMAGPART (t)))
12982 TREE_IMAGPART (t) = drop_tree_overflow (TREE_IMAGPART (t));
12983 }
12984
12985 return t;
12986 }
12987
12988 /* Given a memory reference expression T, return its base address.
12989 The base address of a memory reference expression is the main
12990 object being referenced. For instance, the base address for
12991 'array[i].fld[j]' is 'array'. You can think of this as stripping
12992 away the offset part from a memory address.
12993
12994 This function calls handled_component_p to strip away all the inner
12995 parts of the memory reference until it reaches the base object. */
12996
12997 tree
12998 get_base_address (tree t)
12999 {
13000 if (TREE_CODE (t) == WITH_SIZE_EXPR)
13001 t = TREE_OPERAND (t, 0);
13002 while (handled_component_p (t))
13003 t = TREE_OPERAND (t, 0);
13004
13005 if ((TREE_CODE (t) == MEM_REF
13006 || TREE_CODE (t) == TARGET_MEM_REF)
13007 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
13008 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
13009
13010 return t;
13011 }
13012
13013 /* Return a tree of sizetype representing the size, in bytes, of the element
13014 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
13015
13016 tree
13017 array_ref_element_size (tree exp)
13018 {
13019 tree aligned_size = TREE_OPERAND (exp, 3);
13020 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
13021 location_t loc = EXPR_LOCATION (exp);
13022
13023 /* If a size was specified in the ARRAY_REF, it's the size measured
13024 in alignment units of the element type. So multiply by that value. */
13025 if (aligned_size)
13026 {
13027 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
13028 sizetype from another type of the same width and signedness. */
13029 if (TREE_TYPE (aligned_size) != sizetype)
13030 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
13031 return size_binop_loc (loc, MULT_EXPR, aligned_size,
13032 size_int (TYPE_ALIGN_UNIT (elmt_type)));
13033 }
13034
13035 /* Otherwise, take the size from that of the element type. Substitute
13036 any PLACEHOLDER_EXPR that we have. */
13037 else
13038 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
13039 }
13040
13041 /* Return a tree representing the lower bound of the array mentioned in
13042 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
13043
13044 tree
13045 array_ref_low_bound (tree exp)
13046 {
13047 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
13048
13049 /* If a lower bound is specified in EXP, use it. */
13050 if (TREE_OPERAND (exp, 2))
13051 return TREE_OPERAND (exp, 2);
13052
13053 /* Otherwise, if there is a domain type and it has a lower bound, use it,
13054 substituting for a PLACEHOLDER_EXPR as needed. */
13055 if (domain_type && TYPE_MIN_VALUE (domain_type))
13056 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
13057
13058 /* Otherwise, return a zero of the appropriate type. */
13059 tree idxtype = TREE_TYPE (TREE_OPERAND (exp, 1));
13060 return (idxtype == error_mark_node
13061 ? integer_zero_node : build_int_cst (idxtype, 0));
13062 }
13063
13064 /* Return a tree representing the upper bound of the array mentioned in
13065 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
13066
13067 tree
13068 array_ref_up_bound (tree exp)
13069 {
13070 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
13071
13072 /* If there is a domain type and it has an upper bound, use it, substituting
13073 for a PLACEHOLDER_EXPR as needed. */
13074 if (domain_type && TYPE_MAX_VALUE (domain_type))
13075 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
13076
13077 /* Otherwise fail. */
13078 return NULL_TREE;
13079 }
13080
13081 /* Returns true if REF is an array reference, a component reference,
13082 or a memory reference to an array whose actual size might be larger
13083 than its upper bound implies, there are multiple cases:
13084 A. a ref to a flexible array member at the end of a structure;
13085 B. a ref to an array with a different type against the original decl;
13086 for example:
13087
13088 short a[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
13089 (*((char(*)[16])&a[0]))[i+8]
13090
13091 C. a ref to an array that was passed as a parameter;
13092 for example:
13093
13094 int test (uint8_t *p, uint32_t t[1][1], int n) {
13095 for (int i = 0; i < 4; i++, p++)
13096 t[i][0] = ...;
13097
13098 If non-null, set IS_TRAILING_ARRAY to true if the ref is the above case A.
13099 */
13100
13101 bool
13102 array_ref_flexible_size_p (tree ref, bool *is_trailing_array /* = NULL */)
13103 {
13104 /* The TYPE for this array referece. */
13105 tree atype = NULL_TREE;
13106 /* The FIELD_DECL for the array field in the containing structure. */
13107 tree afield_decl = NULL_TREE;
13108 /* Whether this array is the trailing array of a structure. */
13109 bool is_trailing_array_tmp = false;
13110 if (!is_trailing_array)
13111 is_trailing_array = &is_trailing_array_tmp;
13112
13113 if (TREE_CODE (ref) == ARRAY_REF
13114 || TREE_CODE (ref) == ARRAY_RANGE_REF)
13115 {
13116 atype = TREE_TYPE (TREE_OPERAND (ref, 0));
13117 ref = TREE_OPERAND (ref, 0);
13118 }
13119 else if (TREE_CODE (ref) == COMPONENT_REF
13120 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 1))) == ARRAY_TYPE)
13121 {
13122 atype = TREE_TYPE (TREE_OPERAND (ref, 1));
13123 afield_decl = TREE_OPERAND (ref, 1);
13124 }
13125 else if (TREE_CODE (ref) == MEM_REF)
13126 {
13127 tree arg = TREE_OPERAND (ref, 0);
13128 if (TREE_CODE (arg) == ADDR_EXPR)
13129 arg = TREE_OPERAND (arg, 0);
13130 tree argtype = TREE_TYPE (arg);
13131 if (TREE_CODE (argtype) == RECORD_TYPE)
13132 {
13133 if (tree fld = last_field (argtype))
13134 {
13135 atype = TREE_TYPE (fld);
13136 afield_decl = fld;
13137 if (TREE_CODE (atype) != ARRAY_TYPE)
13138 return false;
13139 if (VAR_P (arg) && DECL_SIZE (fld))
13140 return false;
13141 }
13142 else
13143 return false;
13144 }
13145 else
13146 return false;
13147 }
13148 else
13149 return false;
13150
13151 if (TREE_CODE (ref) == STRING_CST)
13152 return false;
13153
13154 tree ref_to_array = ref;
13155 while (handled_component_p (ref))
13156 {
13157 /* If the reference chain contains a component reference to a
13158 non-union type and there follows another field the reference
13159 is not at the end of a structure. */
13160 if (TREE_CODE (ref) == COMPONENT_REF)
13161 {
13162 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
13163 {
13164 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
13165 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
13166 nextf = DECL_CHAIN (nextf);
13167 if (nextf)
13168 return false;
13169 }
13170 }
13171 /* If we have a multi-dimensional array we do not consider
13172 a non-innermost dimension as flex array if the whole
13173 multi-dimensional array is at struct end.
13174 Same for an array of aggregates with a trailing array
13175 member. */
13176 else if (TREE_CODE (ref) == ARRAY_REF)
13177 return false;
13178 else if (TREE_CODE (ref) == ARRAY_RANGE_REF)
13179 ;
13180 /* If we view an underlying object as sth else then what we
13181 gathered up to now is what we have to rely on. */
13182 else if (TREE_CODE (ref) == VIEW_CONVERT_EXPR)
13183 break;
13184 else
13185 gcc_unreachable ();
13186
13187 ref = TREE_OPERAND (ref, 0);
13188 }
13189
13190 gcc_assert (!afield_decl
13191 || (afield_decl && TREE_CODE (afield_decl) == FIELD_DECL));
13192
13193 /* The array now is at struct end. Treat flexible array member as
13194 always subject to extend, even into just padding constrained by
13195 an underlying decl. */
13196 if (! TYPE_SIZE (atype)
13197 || ! TYPE_DOMAIN (atype)
13198 || ! TYPE_MAX_VALUE (TYPE_DOMAIN (atype)))
13199 {
13200 *is_trailing_array = afield_decl && TREE_CODE (afield_decl) == FIELD_DECL;
13201 return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
13202 }
13203
13204 /* If the reference is based on a declared entity, the size of the array
13205 is constrained by its given domain. (Do not trust commons PR/69368). */
13206 ref = get_base_address (ref);
13207 if (ref
13208 && DECL_P (ref)
13209 && !(flag_unconstrained_commons
13210 && VAR_P (ref) && DECL_COMMON (ref))
13211 && DECL_SIZE_UNIT (ref)
13212 && TREE_CODE (DECL_SIZE_UNIT (ref)) == INTEGER_CST)
13213 {
13214 /* If the object itself is the array it is not at struct end. */
13215 if (DECL_P (ref_to_array))
13216 return false;
13217
13218 /* Check whether the array domain covers all of the available
13219 padding. */
13220 poly_int64 offset;
13221 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (atype))) != INTEGER_CST
13222 || TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))) != INTEGER_CST
13223 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (atype))) != INTEGER_CST)
13224 {
13225 *is_trailing_array
13226 = afield_decl && TREE_CODE (afield_decl) == FIELD_DECL;
13227 return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
13228 }
13229 if (! get_addr_base_and_unit_offset (ref_to_array, &offset))
13230 {
13231 *is_trailing_array
13232 = afield_decl && TREE_CODE (afield_decl) == FIELD_DECL;
13233 return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
13234 }
13235
13236 /* If at least one extra element fits it is a flexarray. */
13237 if (known_le ((wi::to_offset (TYPE_MAX_VALUE (TYPE_DOMAIN (atype)))
13238 - wi::to_offset (TYPE_MIN_VALUE (TYPE_DOMAIN (atype)))
13239 + 2)
13240 * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (atype))),
13241 wi::to_offset (DECL_SIZE_UNIT (ref)) - offset))
13242 {
13243 *is_trailing_array
13244 = afield_decl && TREE_CODE (afield_decl) == FIELD_DECL;
13245 return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
13246 }
13247
13248 return false;
13249 }
13250
13251 *is_trailing_array = afield_decl && TREE_CODE (afield_decl) == FIELD_DECL;
13252 return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
13253 }
13254
13255
13256 /* Return a tree representing the offset, in bytes, of the field referenced
13257 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
13258
13259 tree
13260 component_ref_field_offset (tree exp)
13261 {
13262 tree aligned_offset = TREE_OPERAND (exp, 2);
13263 tree field = TREE_OPERAND (exp, 1);
13264 location_t loc = EXPR_LOCATION (exp);
13265
13266 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
13267 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
13268 value. */
13269 if (aligned_offset)
13270 {
13271 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
13272 sizetype from another type of the same width and signedness. */
13273 if (TREE_TYPE (aligned_offset) != sizetype)
13274 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
13275 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
13276 size_int (DECL_OFFSET_ALIGN (field)
13277 / BITS_PER_UNIT));
13278 }
13279
13280 /* Otherwise, take the offset from that of the field. Substitute
13281 any PLACEHOLDER_EXPR that we have. */
13282 else
13283 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
13284 }
13285
13286 /* Given the initializer INIT, return the initializer for the field
13287 DECL if it exists, otherwise null. Used to obtain the initializer
13288 for a flexible array member and determine its size. */
13289
13290 static tree
13291 get_initializer_for (tree init, tree decl)
13292 {
13293 STRIP_NOPS (init);
13294
13295 tree fld, fld_init;
13296 unsigned HOST_WIDE_INT i;
13297 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), i, fld, fld_init)
13298 {
13299 if (decl == fld)
13300 return fld_init;
13301
13302 if (TREE_CODE (fld) == CONSTRUCTOR)
13303 {
13304 fld_init = get_initializer_for (fld_init, decl);
13305 if (fld_init)
13306 return fld_init;
13307 }
13308 }
13309
13310 return NULL_TREE;
13311 }
13312
13313 /* Determines the special array member type for the array reference REF. */
13314 special_array_member
13315 component_ref_sam_type (tree ref)
13316 {
13317 special_array_member sam_type = special_array_member::none;
13318
13319 tree member = TREE_OPERAND (ref, 1);
13320 tree memsize = DECL_SIZE_UNIT (member);
13321 if (memsize)
13322 {
13323 tree memtype = TREE_TYPE (member);
13324 if (TREE_CODE (memtype) != ARRAY_TYPE)
13325 return sam_type;
13326
13327 bool trailing = false;
13328 (void) array_ref_flexible_size_p (ref, &trailing);
13329 bool zero_elts = integer_zerop (memsize);
13330 if (zero_elts && integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (memtype))))
13331 {
13332 /* If array element has zero size, verify if it is a flexible
13333 array member or zero length array. Clear zero_elts if
13334 it has one or more members or is a VLA member. */
13335 if (tree dom = TYPE_DOMAIN (memtype))
13336 if (tree min = TYPE_MIN_VALUE (dom))
13337 if (tree max = TYPE_MAX_VALUE (dom))
13338 if (TREE_CODE (min) != INTEGER_CST
13339 || TREE_CODE (max) != INTEGER_CST
13340 || !((integer_zerop (min) && integer_all_onesp (max))
13341 || tree_int_cst_lt (max, min)))
13342 zero_elts = false;
13343 }
13344 if (!trailing && !zero_elts)
13345 /* MEMBER is an interior array with more than one element. */
13346 return special_array_member::int_n;
13347
13348 if (zero_elts)
13349 {
13350 if (trailing)
13351 return special_array_member::trail_0;
13352 else
13353 return special_array_member::int_0;
13354 }
13355
13356 if (!zero_elts)
13357 if (tree dom = TYPE_DOMAIN (memtype))
13358 if (tree min = TYPE_MIN_VALUE (dom))
13359 if (tree max = TYPE_MAX_VALUE (dom))
13360 if (TREE_CODE (min) == INTEGER_CST
13361 && TREE_CODE (max) == INTEGER_CST)
13362 {
13363 offset_int minidx = wi::to_offset (min);
13364 offset_int maxidx = wi::to_offset (max);
13365 offset_int neltsm1 = maxidx - minidx;
13366 if (neltsm1 > 0)
13367 /* MEMBER is a trailing array with more than
13368 one elements. */
13369 return special_array_member::trail_n;
13370
13371 if (neltsm1 == 0)
13372 return special_array_member::trail_1;
13373 }
13374 }
13375
13376 return sam_type;
13377 }
13378
13379 /* Determines the size of the member referenced by the COMPONENT_REF
13380 REF, using its initializer expression if necessary in order to
13381 determine the size of an initialized flexible array member.
13382 If non-null, set *SAM to the type of special array member.
13383 Returns the size as sizetype (which might be zero for an object
13384 with an uninitialized flexible array member) or null if the size
13385 cannot be determined. */
13386
13387 tree
13388 component_ref_size (tree ref, special_array_member *sam /* = NULL */)
13389 {
13390 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
13391
13392 special_array_member sambuf;
13393 if (!sam)
13394 sam = &sambuf;
13395 *sam = component_ref_sam_type (ref);
13396
13397 /* The object/argument referenced by the COMPONENT_REF and its type. */
13398 tree arg = TREE_OPERAND (ref, 0);
13399 tree argtype = TREE_TYPE (arg);
13400 /* The referenced member. */
13401 tree member = TREE_OPERAND (ref, 1);
13402
13403 tree memsize = DECL_SIZE_UNIT (member);
13404 if (memsize)
13405 {
13406 tree memtype = TREE_TYPE (member);
13407 if (TREE_CODE (memtype) != ARRAY_TYPE)
13408 /* DECL_SIZE may be less than TYPE_SIZE in C++ when referring
13409 to the type of a class with a virtual base which doesn't
13410 reflect the size of the virtual's members (see pr97595).
13411 If that's the case fail for now and implement something
13412 more robust in the future. */
13413 return (tree_int_cst_equal (memsize, TYPE_SIZE_UNIT (memtype))
13414 ? memsize : NULL_TREE);
13415
13416 /* 2-or-more elements arrays are treated as normal arrays by default. */
13417 if (*sam == special_array_member::int_n
13418 || *sam == special_array_member::trail_n)
13419 return memsize;
13420
13421 tree afield_decl = TREE_OPERAND (ref, 1);
13422 gcc_assert (TREE_CODE (afield_decl) == FIELD_DECL);
13423 /* If the trailing array is a not a flexible array member, treat it as
13424 a normal array. */
13425 if (DECL_NOT_FLEXARRAY (afield_decl)
13426 && *sam != special_array_member::int_0)
13427 return memsize;
13428
13429 if (*sam == special_array_member::int_0)
13430 memsize = NULL_TREE;
13431
13432 /* For a reference to a flexible array member of a union
13433 use the size of the union instead of the size of the member. */
13434 if (TREE_CODE (argtype) == UNION_TYPE)
13435 memsize = TYPE_SIZE_UNIT (argtype);
13436 }
13437
13438 /* MEMBER is either a bona fide flexible array member, or a zero-elements
13439 array member, or an array of length one treated as such. */
13440
13441 /* If the reference is to a declared object and the member a true
13442 flexible array, try to determine its size from its initializer. */
13443 poly_int64 baseoff = 0;
13444 tree base = get_addr_base_and_unit_offset (ref, &baseoff);
13445 if (!base || !VAR_P (base))
13446 {
13447 if (*sam != special_array_member::int_0)
13448 return NULL_TREE;
13449
13450 if (TREE_CODE (arg) != COMPONENT_REF)
13451 return NULL_TREE;
13452
13453 base = arg;
13454 while (TREE_CODE (base) == COMPONENT_REF)
13455 base = TREE_OPERAND (base, 0);
13456 baseoff = tree_to_poly_int64 (byte_position (TREE_OPERAND (ref, 1)));
13457 }
13458
13459 /* BASE is the declared object of which MEMBER is either a member
13460 or that is cast to ARGTYPE (e.g., a char buffer used to store
13461 an ARGTYPE object). */
13462 tree basetype = TREE_TYPE (base);
13463
13464 /* Determine the base type of the referenced object. If it's
13465 the same as ARGTYPE and MEMBER has a known size, return it. */
13466 tree bt = basetype;
13467 if (*sam != special_array_member::int_0)
13468 while (TREE_CODE (bt) == ARRAY_TYPE)
13469 bt = TREE_TYPE (bt);
13470 bool typematch = useless_type_conversion_p (argtype, bt);
13471 if (memsize && typematch)
13472 return memsize;
13473
13474 memsize = NULL_TREE;
13475
13476 if (typematch)
13477 /* MEMBER is a true flexible array member. Compute its size from
13478 the initializer of the BASE object if it has one. */
13479 if (tree init = DECL_P (base) ? DECL_INITIAL (base) : NULL_TREE)
13480 if (init != error_mark_node)
13481 {
13482 init = get_initializer_for (init, member);
13483 if (init)
13484 {
13485 memsize = TYPE_SIZE_UNIT (TREE_TYPE (init));
13486 if (tree refsize = TYPE_SIZE_UNIT (argtype))
13487 {
13488 /* Use the larger of the initializer size and the tail
13489 padding in the enclosing struct. */
13490 poly_int64 rsz = tree_to_poly_int64 (refsize);
13491 rsz -= baseoff;
13492 if (known_lt (tree_to_poly_int64 (memsize), rsz))
13493 memsize = wide_int_to_tree (TREE_TYPE (memsize), rsz);
13494 }
13495
13496 baseoff = 0;
13497 }
13498 }
13499
13500 if (!memsize)
13501 {
13502 if (typematch)
13503 {
13504 if (DECL_P (base)
13505 && DECL_EXTERNAL (base)
13506 && bt == basetype
13507 && *sam != special_array_member::int_0)
13508 /* The size of a flexible array member of an extern struct
13509 with no initializer cannot be determined (it's defined
13510 in another translation unit and can have an initializer
13511 with an arbitrary number of elements). */
13512 return NULL_TREE;
13513
13514 /* Use the size of the base struct or, for interior zero-length
13515 arrays, the size of the enclosing type. */
13516 memsize = TYPE_SIZE_UNIT (bt);
13517 }
13518 else if (DECL_P (base))
13519 /* Use the size of the BASE object (possibly an array of some
13520 other type such as char used to store the struct). */
13521 memsize = DECL_SIZE_UNIT (base);
13522 else
13523 return NULL_TREE;
13524 }
13525
13526 /* If the flexible array member has a known size use the greater
13527 of it and the tail padding in the enclosing struct.
13528 Otherwise, when the size of the flexible array member is unknown
13529 and the referenced object is not a struct, use the size of its
13530 type when known. This detects sizes of array buffers when cast
13531 to struct types with flexible array members. */
13532 if (memsize)
13533 {
13534 if (!tree_fits_poly_int64_p (memsize))
13535 return NULL_TREE;
13536 poly_int64 memsz64 = memsize ? tree_to_poly_int64 (memsize) : 0;
13537 if (known_lt (baseoff, memsz64))
13538 {
13539 memsz64 -= baseoff;
13540 return wide_int_to_tree (TREE_TYPE (memsize), memsz64);
13541 }
13542 return size_zero_node;
13543 }
13544
13545 /* Return "don't know" for an external non-array object since its
13546 flexible array member can be initialized to have any number of
13547 elements. Otherwise, return zero because the flexible array
13548 member has no elements. */
13549 return (DECL_P (base)
13550 && DECL_EXTERNAL (base)
13551 && (!typematch
13552 || TREE_CODE (basetype) != ARRAY_TYPE)
13553 ? NULL_TREE : size_zero_node);
13554 }
13555
13556 /* Return true if the given node CALL is a call to a .ACCESS_WITH_SIZE
13557 function. */
13558 bool
13559 is_access_with_size_p (const_tree call)
13560 {
13561 if (TREE_CODE (call) != CALL_EXPR)
13562 return false;
13563 if (CALL_EXPR_IFN (call) == IFN_ACCESS_WITH_SIZE)
13564 return true;
13565 return false;
13566 }
13567
13568 /* Get the corresponding reference from the call to a .ACCESS_WITH_SIZE.
13569 * i.e the first argument of this call. Return NULL_TREE otherwise. */
13570 tree
13571 get_ref_from_access_with_size (tree call)
13572 {
13573 if (is_access_with_size_p (call))
13574 return CALL_EXPR_ARG (call, 0);
13575 return NULL_TREE;
13576 }
13577
13578 /* Return the machine mode of T. For vectors, returns the mode of the
13579 inner type. The main use case is to feed the result to HONOR_NANS,
13580 avoiding the BLKmode that a direct TYPE_MODE (T) might return. */
13581
13582 machine_mode
13583 element_mode (const_tree t)
13584 {
13585 if (!TYPE_P (t))
13586 t = TREE_TYPE (t);
13587 if (VECTOR_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
13588 t = TREE_TYPE (t);
13589 return TYPE_MODE (t);
13590 }
13591
13592 /* Vector types need to re-check the target flags each time we report
13593 the machine mode. We need to do this because attribute target can
13594 change the result of vector_mode_supported_p and have_regs_of_mode
13595 on a per-function basis. Thus the TYPE_MODE of a VECTOR_TYPE can
13596 change on a per-function basis. */
13597 /* ??? Possibly a better solution is to run through all the types
13598 referenced by a function and re-compute the TYPE_MODE once, rather
13599 than make the TYPE_MODE macro call a function. */
13600
13601 machine_mode
13602 vector_type_mode (const_tree t)
13603 {
13604 machine_mode mode;
13605
13606 gcc_assert (TREE_CODE (t) == VECTOR_TYPE);
13607
13608 mode = t->type_common.mode;
13609 if (VECTOR_MODE_P (mode)
13610 && (!targetm.vector_mode_supported_p (mode)
13611 || !have_regs_of_mode[mode]))
13612 {
13613 scalar_int_mode innermode;
13614
13615 /* For integers, try mapping it to a same-sized scalar mode. */
13616 if (is_int_mode (TREE_TYPE (t)->type_common.mode, &innermode))
13617 {
13618 poly_int64 size = (TYPE_VECTOR_SUBPARTS (t)
13619 * GET_MODE_BITSIZE (innermode));
13620 scalar_int_mode mode;
13621 if (int_mode_for_size (size, 0).exists (&mode)
13622 && have_regs_of_mode[mode])
13623 return mode;
13624 }
13625
13626 return BLKmode;
13627 }
13628
13629 return mode;
13630 }
13631
13632 /* Return the size in bits of each element of vector type TYPE. */
13633
13634 unsigned int
13635 vector_element_bits (const_tree type)
13636 {
13637 gcc_checking_assert (VECTOR_TYPE_P (type));
13638 if (VECTOR_BOOLEAN_TYPE_P (type))
13639 return TYPE_PRECISION (TREE_TYPE (type));
13640 return tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)));
13641 }
13642
13643 /* Calculate the size in bits of each element of vector type TYPE
13644 and return the result as a tree of type bitsizetype. */
13645
13646 tree
13647 vector_element_bits_tree (const_tree type)
13648 {
13649 gcc_checking_assert (VECTOR_TYPE_P (type));
13650 if (VECTOR_BOOLEAN_TYPE_P (type))
13651 return bitsize_int (vector_element_bits (type));
13652 return TYPE_SIZE (TREE_TYPE (type));
13653 }
13654
13655 /* Verify that basic properties of T match TV and thus T can be a variant of
13656 TV. TV should be the more specified variant (i.e. the main variant). */
13657
13658 static bool
13659 verify_type_variant (const_tree t, tree tv)
13660 {
13661 /* Type variant can differ by:
13662
13663 - TYPE_QUALS: TYPE_READONLY, TYPE_VOLATILE, TYPE_ATOMIC, TYPE_RESTRICT,
13664 ENCODE_QUAL_ADDR_SPACE.
13665 - main variant may be TYPE_COMPLETE_P and variant types !TYPE_COMPLETE_P
13666 in this case some values may not be set in the variant types
13667 (see TYPE_COMPLETE_P checks).
13668 - it is possible to have TYPE_ARTIFICIAL variant of non-artifical type
13669 - by TYPE_NAME and attributes (i.e. when variant originate by typedef)
13670 - TYPE_CANONICAL (TYPE_ALIAS_SET is the same among variants)
13671 - by the alignment: TYPE_ALIGN and TYPE_USER_ALIGN
13672 - during LTO by TYPE_CONTEXT if type is TYPE_FILE_SCOPE_P
13673 this is necessary to make it possible to merge types form different TUs
13674 - arrays, pointers and references may have TREE_TYPE that is a variant
13675 of TREE_TYPE of their main variants.
13676 - aggregates may have new TYPE_FIELDS list that list variants of
13677 the main variant TYPE_FIELDS.
13678 - vector types may differ by TYPE_VECTOR_OPAQUE
13679 */
13680
13681 /* Convenience macro for matching individual fields. */
13682 #define verify_variant_match(flag) \
13683 do { \
13684 if (flag (tv) != flag (t)) \
13685 { \
13686 error ("type variant differs by %s", #flag); \
13687 debug_tree (tv); \
13688 return false; \
13689 } \
13690 } while (false)
13691
13692 /* tree_base checks. */
13693
13694 verify_variant_match (TREE_CODE);
13695 /* FIXME: Ada builds non-artificial variants of artificial types. */
13696 #if 0
13697 if (TYPE_ARTIFICIAL (tv))
13698 verify_variant_match (TYPE_ARTIFICIAL);
13699 #endif
13700 if (POINTER_TYPE_P (tv))
13701 verify_variant_match (TYPE_REF_CAN_ALIAS_ALL);
13702 /* FIXME: TYPE_SIZES_GIMPLIFIED may differs for Ada build. */
13703 verify_variant_match (TYPE_UNSIGNED);
13704 verify_variant_match (TYPE_PACKED);
13705 if (TREE_CODE (t) == REFERENCE_TYPE)
13706 verify_variant_match (TYPE_REF_IS_RVALUE);
13707 if (AGGREGATE_TYPE_P (t))
13708 verify_variant_match (TYPE_REVERSE_STORAGE_ORDER);
13709 else
13710 verify_variant_match (TYPE_SATURATING);
13711 /* FIXME: This check trigger during libstdc++ build. */
13712 #if 0
13713 if (RECORD_OR_UNION_TYPE_P (t) && COMPLETE_TYPE_P (t))
13714 verify_variant_match (TYPE_FINAL_P);
13715 #endif
13716
13717 /* tree_type_common checks. */
13718
13719 if (COMPLETE_TYPE_P (t))
13720 {
13721 verify_variant_match (TYPE_MODE);
13722 if (TREE_CODE (TYPE_SIZE (t)) != PLACEHOLDER_EXPR
13723 && TREE_CODE (TYPE_SIZE (tv)) != PLACEHOLDER_EXPR)
13724 verify_variant_match (TYPE_SIZE);
13725 if (TREE_CODE (TYPE_SIZE_UNIT (t)) != PLACEHOLDER_EXPR
13726 && TREE_CODE (TYPE_SIZE_UNIT (tv)) != PLACEHOLDER_EXPR
13727 && TYPE_SIZE_UNIT (t) != TYPE_SIZE_UNIT (tv))
13728 {
13729 gcc_assert (!operand_equal_p (TYPE_SIZE_UNIT (t),
13730 TYPE_SIZE_UNIT (tv), 0));
13731 error ("type variant has different %<TYPE_SIZE_UNIT%>");
13732 debug_tree (tv);
13733 error ("type variant%'s %<TYPE_SIZE_UNIT%>");
13734 debug_tree (TYPE_SIZE_UNIT (tv));
13735 error ("type%'s %<TYPE_SIZE_UNIT%>");
13736 debug_tree (TYPE_SIZE_UNIT (t));
13737 return false;
13738 }
13739 verify_variant_match (TYPE_NEEDS_CONSTRUCTING);
13740 }
13741 verify_variant_match (TYPE_PRECISION_RAW);
13742 if (RECORD_OR_UNION_TYPE_P (t))
13743 verify_variant_match (TYPE_TRANSPARENT_AGGR);
13744 else if (TREE_CODE (t) == ARRAY_TYPE)
13745 verify_variant_match (TYPE_NONALIASED_COMPONENT);
13746 /* During LTO we merge variant lists from diferent translation units
13747 that may differ BY TYPE_CONTEXT that in turn may point
13748 to TRANSLATION_UNIT_DECL.
13749 Ada also builds variants of types with different TYPE_CONTEXT. */
13750 #if 0
13751 if (!in_lto_p || !TYPE_FILE_SCOPE_P (t))
13752 verify_variant_match (TYPE_CONTEXT);
13753 #endif
13754 if (TREE_CODE (t) == ARRAY_TYPE || TREE_CODE (t) == INTEGER_TYPE)
13755 verify_variant_match (TYPE_STRING_FLAG);
13756 if (TREE_CODE (t) == RECORD_TYPE || TREE_CODE (t) == UNION_TYPE)
13757 verify_variant_match (TYPE_CXX_ODR_P);
13758 if (TYPE_ALIAS_SET_KNOWN_P (t))
13759 {
13760 error ("type variant with %<TYPE_ALIAS_SET_KNOWN_P%>");
13761 debug_tree (tv);
13762 return false;
13763 }
13764
13765 /* tree_type_non_common checks. */
13766
13767 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
13768 and dangle the pointer from time to time. */
13769 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_VFIELD (t) != TYPE_VFIELD (tv)
13770 && (in_lto_p || !TYPE_VFIELD (tv)
13771 || TREE_CODE (TYPE_VFIELD (tv)) != TREE_LIST))
13772 {
13773 error ("type variant has different %<TYPE_VFIELD%>");
13774 debug_tree (tv);
13775 return false;
13776 }
13777 if ((TREE_CODE (t) == ENUMERAL_TYPE && COMPLETE_TYPE_P (t))
13778 || TREE_CODE (t) == INTEGER_TYPE
13779 || TREE_CODE (t) == BOOLEAN_TYPE
13780 || TREE_CODE (t) == BITINT_TYPE
13781 || SCALAR_FLOAT_TYPE_P (t)
13782 || FIXED_POINT_TYPE_P (t))
13783 {
13784 verify_variant_match (TYPE_MAX_VALUE);
13785 verify_variant_match (TYPE_MIN_VALUE);
13786 }
13787 if (TREE_CODE (t) == METHOD_TYPE)
13788 verify_variant_match (TYPE_METHOD_BASETYPE);
13789 if (TREE_CODE (t) == OFFSET_TYPE)
13790 verify_variant_match (TYPE_OFFSET_BASETYPE);
13791 if (TREE_CODE (t) == ARRAY_TYPE)
13792 verify_variant_match (TYPE_ARRAY_MAX_SIZE);
13793 /* FIXME: Be lax and allow TYPE_BINFO to be missing in variant types
13794 or even type's main variant. This is needed to make bootstrap pass
13795 and the bug seems new in GCC 5.
13796 C++ FE should be updated to make this consistent and we should check
13797 that TYPE_BINFO is always NULL for !COMPLETE_TYPE_P and otherwise there
13798 is a match with main variant.
13799
13800 Also disable the check for Java for now because of parser hack that builds
13801 first an dummy BINFO and then sometimes replace it by real BINFO in some
13802 of the copies. */
13803 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t) && TYPE_BINFO (tv)
13804 && TYPE_BINFO (t) != TYPE_BINFO (tv)
13805 /* FIXME: Java sometimes keep dump TYPE_BINFOs on variant types.
13806 Since there is no cheap way to tell C++/Java type w/o LTO, do checking
13807 at LTO time only. */
13808 && (in_lto_p && odr_type_p (t)))
13809 {
13810 error ("type variant has different %<TYPE_BINFO%>");
13811 debug_tree (tv);
13812 error ("type variant%'s %<TYPE_BINFO%>");
13813 debug_tree (TYPE_BINFO (tv));
13814 error ("type%'s %<TYPE_BINFO%>");
13815 debug_tree (TYPE_BINFO (t));
13816 return false;
13817 }
13818
13819 /* Check various uses of TYPE_VALUES_RAW. */
13820 if (TREE_CODE (t) == ENUMERAL_TYPE
13821 && TYPE_VALUES (t))
13822 verify_variant_match (TYPE_VALUES);
13823 else if (TREE_CODE (t) == ARRAY_TYPE)
13824 verify_variant_match (TYPE_DOMAIN);
13825 /* Permit incomplete variants of complete type. While FEs may complete
13826 all variants, this does not happen for C++ templates in all cases. */
13827 else if (RECORD_OR_UNION_TYPE_P (t)
13828 && COMPLETE_TYPE_P (t)
13829 && TYPE_FIELDS (t) != TYPE_FIELDS (tv))
13830 {
13831 tree f1, f2;
13832
13833 /* Fortran builds qualified variants as new records with items of
13834 qualified type. Verify that they looks same. */
13835 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (tv);
13836 f1 && f2;
13837 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
13838 if (TREE_CODE (f1) != FIELD_DECL || TREE_CODE (f2) != FIELD_DECL
13839 || (TYPE_MAIN_VARIANT (TREE_TYPE (f1))
13840 != TYPE_MAIN_VARIANT (TREE_TYPE (f2))
13841 /* FIXME: gfc_nonrestricted_type builds all types as variants
13842 with exception of pointer types. It deeply copies the type
13843 which means that we may end up with a variant type
13844 referring non-variant pointer. We may change it to
13845 produce types as variants, too, like
13846 objc_get_protocol_qualified_type does. */
13847 && !POINTER_TYPE_P (TREE_TYPE (f1)))
13848 || DECL_FIELD_OFFSET (f1) != DECL_FIELD_OFFSET (f2)
13849 || DECL_FIELD_BIT_OFFSET (f1) != DECL_FIELD_BIT_OFFSET (f2))
13850 break;
13851 if (f1 || f2)
13852 {
13853 error ("type variant has different %<TYPE_FIELDS%>");
13854 debug_tree (tv);
13855 error ("first mismatch is field");
13856 debug_tree (f1);
13857 error ("and field");
13858 debug_tree (f2);
13859 return false;
13860 }
13861 }
13862 else if (FUNC_OR_METHOD_TYPE_P (t))
13863 verify_variant_match (TYPE_ARG_TYPES);
13864 /* For C++ the qualified variant of array type is really an array type
13865 of qualified TREE_TYPE.
13866 objc builds variants of pointer where pointer to type is a variant, too
13867 in objc_get_protocol_qualified_type. */
13868 if (TREE_TYPE (t) != TREE_TYPE (tv)
13869 && ((TREE_CODE (t) != ARRAY_TYPE
13870 && !POINTER_TYPE_P (t))
13871 || TYPE_MAIN_VARIANT (TREE_TYPE (t))
13872 != TYPE_MAIN_VARIANT (TREE_TYPE (tv))))
13873 {
13874 error ("type variant has different %<TREE_TYPE%>");
13875 debug_tree (tv);
13876 error ("type variant%'s %<TREE_TYPE%>");
13877 debug_tree (TREE_TYPE (tv));
13878 error ("type%'s %<TREE_TYPE%>");
13879 debug_tree (TREE_TYPE (t));
13880 return false;
13881 }
13882 if (type_with_alias_set_p (t)
13883 && !gimple_canonical_types_compatible_p (t, tv, false))
13884 {
13885 error ("type is not compatible with its variant");
13886 debug_tree (tv);
13887 error ("type variant%'s %<TREE_TYPE%>");
13888 debug_tree (TREE_TYPE (tv));
13889 error ("type%'s %<TREE_TYPE%>");
13890 debug_tree (TREE_TYPE (t));
13891 return false;
13892 }
13893 return true;
13894 #undef verify_variant_match
13895 }
13896
13897
13898 /* The TYPE_CANONICAL merging machinery. It should closely resemble
13899 the middle-end types_compatible_p function. It needs to avoid
13900 claiming types are different for types that should be treated
13901 the same with respect to TBAA. Canonical types are also used
13902 for IL consistency checks via the useless_type_conversion_p
13903 predicate which does not handle all type kinds itself but falls
13904 back to pointer-comparison of TYPE_CANONICAL for aggregates
13905 for example. */
13906
13907 /* Return true if TYPE_UNSIGNED of TYPE should be ignored for canonical
13908 type calculation because we need to allow inter-operability between signed
13909 and unsigned variants. */
13910
13911 bool
13912 type_with_interoperable_signedness (const_tree type)
13913 {
13914 /* Fortran standard require C_SIGNED_CHAR to be interoperable with both
13915 signed char and unsigned char. Similarly fortran FE builds
13916 C_SIZE_T as signed type, while C defines it unsigned. */
13917
13918 return tree_code_for_canonical_type_merging (TREE_CODE (type))
13919 == INTEGER_TYPE
13920 && (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node)
13921 || TYPE_PRECISION (type) == TYPE_PRECISION (size_type_node));
13922 }
13923
13924 /* Return true iff T1 and T2 are structurally identical for what
13925 TBAA is concerned.
13926 This function is used both by lto.cc canonical type merging and by the
13927 verifier. If TRUST_TYPE_CANONICAL we do not look into structure of types
13928 that have TYPE_CANONICAL defined and assume them equivalent. This is useful
13929 only for LTO because only in these cases TYPE_CANONICAL equivalence
13930 correspond to one defined by gimple_canonical_types_compatible_p. */
13931
13932 bool
13933 gimple_canonical_types_compatible_p (const_tree t1, const_tree t2,
13934 bool trust_type_canonical)
13935 {
13936 /* Type variants should be same as the main variant. When not doing sanity
13937 checking to verify this fact, go to main variants and save some work. */
13938 if (trust_type_canonical)
13939 {
13940 t1 = TYPE_MAIN_VARIANT (t1);
13941 t2 = TYPE_MAIN_VARIANT (t2);
13942 }
13943
13944 /* Check first for the obvious case of pointer identity. */
13945 if (t1 == t2)
13946 return true;
13947
13948 /* Check that we have two types to compare. */
13949 if (t1 == NULL_TREE || t2 == NULL_TREE)
13950 return false;
13951
13952 /* We consider complete types always compatible with incomplete type.
13953 This does not make sense for canonical type calculation and thus we
13954 need to ensure that we are never called on it.
13955
13956 FIXME: For more correctness the function probably should have three modes
13957 1) mode assuming that types are complete mathcing their structure
13958 2) mode allowing incomplete types but producing equivalence classes
13959 and thus ignoring all info from complete types
13960 3) mode allowing incomplete types to match complete but checking
13961 compatibility between complete types.
13962
13963 1 and 2 can be used for canonical type calculation. 3 is the real
13964 definition of type compatibility that can be used i.e. for warnings during
13965 declaration merging. */
13966
13967 gcc_assert (!trust_type_canonical
13968 || (type_with_alias_set_p (t1) && type_with_alias_set_p (t2)));
13969
13970 /* If the types have been previously registered and found equal
13971 they still are. */
13972
13973 if (TYPE_CANONICAL (t1) && TYPE_CANONICAL (t2)
13974 && trust_type_canonical)
13975 {
13976 /* Do not use TYPE_CANONICAL of pointer types. For LTO streamed types
13977 they are always NULL, but they are set to non-NULL for types
13978 constructed by build_pointer_type and variants. In this case the
13979 TYPE_CANONICAL is more fine grained than the equivalnce we test (where
13980 all pointers are considered equal. Be sure to not return false
13981 negatives. */
13982 gcc_checking_assert (canonical_type_used_p (t1)
13983 && canonical_type_used_p (t2));
13984 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
13985 }
13986
13987 /* For types where we do ODR based TBAA the canonical type is always
13988 set correctly, so we know that types are different if their
13989 canonical types does not match. */
13990 if (trust_type_canonical
13991 && (odr_type_p (t1) && odr_based_tbaa_p (t1))
13992 != (odr_type_p (t2) && odr_based_tbaa_p (t2)))
13993 return false;
13994
13995 /* Can't be the same type if the types don't have the same code. */
13996 enum tree_code code = tree_code_for_canonical_type_merging (TREE_CODE (t1));
13997 if (code != tree_code_for_canonical_type_merging (TREE_CODE (t2)))
13998 return false;
13999
14000 /* Qualifiers do not matter for canonical type comparison purposes. */
14001
14002 /* Void types and nullptr types are always the same. */
14003 if (VOID_TYPE_P (t1)
14004 || TREE_CODE (t1) == NULLPTR_TYPE)
14005 return true;
14006
14007 /* Can't be compatible types if they have different mode. Because of
14008 flexible array members, we allow mismatching modes for structures or
14009 unions. */
14010 if (!RECORD_OR_UNION_TYPE_P (t1)
14011 && TREE_CODE (t1) != ARRAY_TYPE
14012 && TYPE_MODE (t1) != TYPE_MODE (t2))
14013 return false;
14014
14015 /* Non-aggregate types can be handled cheaply. */
14016 if (INTEGRAL_TYPE_P (t1)
14017 || SCALAR_FLOAT_TYPE_P (t1)
14018 || FIXED_POINT_TYPE_P (t1)
14019 || VECTOR_TYPE_P (t1)
14020 || TREE_CODE (t1) == COMPLEX_TYPE
14021 || TREE_CODE (t1) == OFFSET_TYPE
14022 || POINTER_TYPE_P (t1))
14023 {
14024 /* Can't be the same type if they have different precision. */
14025 if (TYPE_PRECISION_RAW (t1) != TYPE_PRECISION_RAW (t2))
14026 return false;
14027
14028 /* In some cases the signed and unsigned types are required to be
14029 inter-operable. */
14030 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2)
14031 && !type_with_interoperable_signedness (t1))
14032 return false;
14033
14034 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
14035 interoperable with "signed char". Unless all frontends are revisited
14036 to agree on these types, we must ignore the flag completely. */
14037
14038 /* Fortran standard define C_PTR type that is compatible with every
14039 C pointer. For this reason we need to glob all pointers into one.
14040 Still pointers in different address spaces are not compatible. */
14041 if (POINTER_TYPE_P (t1))
14042 {
14043 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
14044 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
14045 return false;
14046 }
14047
14048 /* Tail-recurse to components. */
14049 if (VECTOR_TYPE_P (t1)
14050 || TREE_CODE (t1) == COMPLEX_TYPE)
14051 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
14052 TREE_TYPE (t2),
14053 trust_type_canonical);
14054
14055 return true;
14056 }
14057
14058 /* Do type-specific comparisons. */
14059 switch (TREE_CODE (t1))
14060 {
14061 case ARRAY_TYPE:
14062 /* Array types are the same if the element types are the same and
14063 minimum and maximum index are the same. */
14064 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
14065 trust_type_canonical)
14066 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
14067 || TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2)
14068 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
14069 return false;
14070 else
14071 {
14072 tree i1 = TYPE_DOMAIN (t1);
14073 tree i2 = TYPE_DOMAIN (t2);
14074
14075 /* For an incomplete external array, the type domain can be
14076 NULL_TREE. Check this condition also. */
14077 if (i1 == NULL_TREE && i2 == NULL_TREE)
14078 return true;
14079 else if (i1 == NULL_TREE || i2 == NULL_TREE)
14080 return false;
14081 else
14082 {
14083 tree min1 = TYPE_MIN_VALUE (i1);
14084 tree min2 = TYPE_MIN_VALUE (i2);
14085 tree max1 = TYPE_MAX_VALUE (i1);
14086 tree max2 = TYPE_MAX_VALUE (i2);
14087
14088 /* The minimum/maximum values have to be the same. */
14089 if ((min1 == min2
14090 || (min1 && min2
14091 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
14092 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
14093 || operand_equal_p (min1, min2, 0))))
14094 && (max1 == max2
14095 || (max1 && max2
14096 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
14097 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
14098 || operand_equal_p (max1, max2, 0)))))
14099 return true;
14100 else
14101 return false;
14102 }
14103 }
14104
14105 case METHOD_TYPE:
14106 case FUNCTION_TYPE:
14107 /* Function types are the same if the return type and arguments types
14108 are the same. */
14109 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
14110 trust_type_canonical))
14111 return false;
14112
14113 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2)
14114 && (TYPE_NO_NAMED_ARGS_STDARG_P (t1)
14115 == TYPE_NO_NAMED_ARGS_STDARG_P (t2)))
14116 return true;
14117 else
14118 {
14119 tree parms1, parms2;
14120
14121 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
14122 parms1 && parms2;
14123 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
14124 {
14125 if (!gimple_canonical_types_compatible_p
14126 (TREE_VALUE (parms1), TREE_VALUE (parms2),
14127 trust_type_canonical))
14128 return false;
14129 }
14130
14131 if (parms1 || parms2)
14132 return false;
14133
14134 return true;
14135 }
14136
14137 case RECORD_TYPE:
14138 case UNION_TYPE:
14139 case QUAL_UNION_TYPE:
14140 {
14141 tree f1, f2;
14142
14143 /* Don't try to compare variants of an incomplete type, before
14144 TYPE_FIELDS has been copied around. */
14145 if (!COMPLETE_TYPE_P (t1) && !COMPLETE_TYPE_P (t2))
14146 return true;
14147
14148
14149 if (TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2))
14150 return false;
14151
14152 /* For aggregate types, all the fields must be the same. */
14153 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
14154 f1 || f2;
14155 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
14156 {
14157 /* Skip non-fields and zero-sized fields, except zero-sized
14158 arrays at the end. */
14159 while (f1 && (TREE_CODE (f1) != FIELD_DECL
14160 || (DECL_SIZE (f1)
14161 && integer_zerop (DECL_SIZE (f1))
14162 && (TREE_CHAIN (f1)
14163 || TREE_CODE (TREE_TYPE (f1))
14164 != ARRAY_TYPE))))
14165 f1 = TREE_CHAIN (f1);
14166 while (f2 && (TREE_CODE (f2) != FIELD_DECL
14167 || (DECL_SIZE (f2)
14168 && integer_zerop (DECL_SIZE (f2))
14169 && (TREE_CHAIN (f2)
14170 || TREE_CODE (TREE_TYPE (f2))
14171 != ARRAY_TYPE))))
14172 f2 = TREE_CHAIN (f2);
14173 if (!f1 || !f2)
14174 break;
14175
14176 tree t1 = TREE_TYPE (f1);
14177 tree t2 = TREE_TYPE (f2);
14178
14179 /* If the last element are arrays, we only compare the element
14180 types. */
14181 if (TREE_CHAIN (f1) == NULL_TREE && TREE_CODE (t1) == ARRAY_TYPE
14182 && TREE_CHAIN (f2) == NULL_TREE && TREE_CODE (t2) == ARRAY_TYPE)
14183 {
14184 /* If both arrays have zero size, this is a match. */
14185 if (DECL_SIZE (f1) && integer_zerop (DECL_SIZE (f1))
14186 && DECL_SIZE (f2) && integer_zerop (DECL_SIZE (f2)))
14187 return true;
14188
14189 t1 = TREE_TYPE (t1);
14190 t2 = TREE_TYPE (t2);
14191 }
14192
14193 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
14194 || !gimple_compare_field_offset (f1, f2)
14195 || !gimple_canonical_types_compatible_p
14196 (t1, t2, trust_type_canonical))
14197 return false;
14198 }
14199
14200 /* If one aggregate has more fields than the other, they
14201 are not the same. */
14202 if (f1 || f2)
14203 return false;
14204
14205 return true;
14206 }
14207
14208 default:
14209 /* Consider all types with language specific trees in them mutually
14210 compatible. This is executed only from verify_type and false
14211 positives can be tolerated. */
14212 gcc_assert (!in_lto_p);
14213 return true;
14214 }
14215 }
14216
14217 /* For OPAQUE_TYPE T, it should have only size and alignment information
14218 and its mode should be of class MODE_OPAQUE. This function verifies
14219 these properties of T match TV which is the main variant of T and TC
14220 which is the canonical of T. */
14221
14222 static void
14223 verify_opaque_type (const_tree t, tree tv, tree tc)
14224 {
14225 gcc_assert (OPAQUE_TYPE_P (t));
14226 gcc_assert (tv && tv == TYPE_MAIN_VARIANT (tv));
14227 gcc_assert (tc && tc == TYPE_CANONICAL (tc));
14228
14229 /* For an opaque type T1, check if some of its properties match
14230 the corresponding ones of the other opaque type T2, emit some
14231 error messages for those inconsistent ones. */
14232 auto check_properties_for_opaque_type = [](const_tree t1, tree t2,
14233 const char *kind_msg)
14234 {
14235 if (!OPAQUE_TYPE_P (t2))
14236 {
14237 error ("type %s is not an opaque type", kind_msg);
14238 debug_tree (t2);
14239 return;
14240 }
14241 if (!OPAQUE_MODE_P (TYPE_MODE (t2)))
14242 {
14243 error ("type %s is not with opaque mode", kind_msg);
14244 debug_tree (t2);
14245 return;
14246 }
14247 if (TYPE_MODE (t1) != TYPE_MODE (t2))
14248 {
14249 error ("type %s differs by %<TYPE_MODE%>", kind_msg);
14250 debug_tree (t2);
14251 return;
14252 }
14253 poly_uint64 t1_size = tree_to_poly_uint64 (TYPE_SIZE (t1));
14254 poly_uint64 t2_size = tree_to_poly_uint64 (TYPE_SIZE (t2));
14255 if (maybe_ne (t1_size, t2_size))
14256 {
14257 error ("type %s differs by %<TYPE_SIZE%>", kind_msg);
14258 debug_tree (t2);
14259 return;
14260 }
14261 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
14262 {
14263 error ("type %s differs by %<TYPE_ALIGN%>", kind_msg);
14264 debug_tree (t2);
14265 return;
14266 }
14267 if (TYPE_USER_ALIGN (t1) != TYPE_USER_ALIGN (t2))
14268 {
14269 error ("type %s differs by %<TYPE_USER_ALIGN%>", kind_msg);
14270 debug_tree (t2);
14271 return;
14272 }
14273 };
14274
14275 if (t != tv)
14276 check_properties_for_opaque_type (t, tv, "variant");
14277
14278 if (t != tc)
14279 check_properties_for_opaque_type (t, tc, "canonical");
14280 }
14281
14282 /* Verify type T. */
14283
14284 void
14285 verify_type (const_tree t)
14286 {
14287 bool error_found = false;
14288 tree mv = TYPE_MAIN_VARIANT (t);
14289 tree ct = TYPE_CANONICAL (t);
14290
14291 if (OPAQUE_TYPE_P (t))
14292 {
14293 verify_opaque_type (t, mv, ct);
14294 return;
14295 }
14296
14297 if (!mv)
14298 {
14299 error ("main variant is not defined");
14300 error_found = true;
14301 }
14302 else if (mv != TYPE_MAIN_VARIANT (mv))
14303 {
14304 error ("%<TYPE_MAIN_VARIANT%> has different %<TYPE_MAIN_VARIANT%>");
14305 debug_tree (mv);
14306 error_found = true;
14307 }
14308 else if (t != mv && !verify_type_variant (t, mv))
14309 error_found = true;
14310
14311 if (!ct)
14312 ;
14313 else if (TYPE_CANONICAL (ct) != ct)
14314 {
14315 error ("%<TYPE_CANONICAL%> has different %<TYPE_CANONICAL%>");
14316 debug_tree (ct);
14317 error_found = true;
14318 }
14319 /* Method and function types cannot be used to address memory and thus
14320 TYPE_CANONICAL really matters only for determining useless conversions.
14321
14322 FIXME: C++ FE produce declarations of builtin functions that are not
14323 compatible with main variants. */
14324 else if (TREE_CODE (t) == FUNCTION_TYPE)
14325 ;
14326 else if (t != ct
14327 /* FIXME: gimple_canonical_types_compatible_p cannot compare types
14328 with variably sized arrays because their sizes possibly
14329 gimplified to different variables. */
14330 && !variably_modified_type_p (ct, NULL)
14331 && !gimple_canonical_types_compatible_p (t, ct, false)
14332 && COMPLETE_TYPE_P (t))
14333 {
14334 error ("%<TYPE_CANONICAL%> is not compatible");
14335 debug_tree (ct);
14336 error_found = true;
14337 }
14338 if (COMPLETE_TYPE_P (t) && TYPE_CANONICAL (t)
14339 /* We allow a mismatch for structure or union because of
14340 flexible array members. */
14341 && !RECORD_OR_UNION_TYPE_P (t)
14342 && !RECORD_OR_UNION_TYPE_P (TYPE_CANONICAL (t))
14343 && TREE_CODE (t) != ARRAY_TYPE
14344 && TYPE_MODE (t) != TYPE_MODE (TYPE_CANONICAL (t)))
14345 {
14346 error ("%<TYPE_MODE%> of %<TYPE_CANONICAL%> is not compatible");
14347 debug_tree (ct);
14348 error_found = true;
14349 }
14350 if (TYPE_MAIN_VARIANT (t) == t && ct && TYPE_MAIN_VARIANT (ct) != ct)
14351 {
14352 /* This can happen when build_type_attribute_variant is called on
14353 C/C++ arrays of qualified types. volatile int[2] is unqualified
14354 ARRAY_TYPE with volatile int element type.
14355 TYPE_CANONICAL (volatile int) is itself and so is
14356 TYPE_CANONICAL (volatile int[2]). build_type_attribute_qual_variant
14357 creates a distinct type copy (so TYPE_MAIN_VARIANT is itself) and sets
14358 its TYPE_CANONICAL to the unqualified ARRAY_TYPE (so volatile int[2]).
14359 But this is not the TYPE_MAIN_VARIANT, which is int[2]. So, just
14360 verify that TYPE_MAIN_VARIANT (ct) is already the final type we
14361 need. */
14362 tree mvc = TYPE_MAIN_VARIANT (ct);
14363 if (TYPE_CANONICAL (mvc) != mvc)
14364 {
14365 error ("main variant of %<TYPE_CANONICAL%> of main variant is not"
14366 " its own %<TYPE_CANONICAL%>");
14367 debug_tree (ct);
14368 debug_tree (TYPE_MAIN_VARIANT (ct));
14369 error_found = true;
14370 }
14371 }
14372
14373
14374 /* Check various uses of TYPE_MIN_VALUE_RAW. */
14375 if (RECORD_OR_UNION_TYPE_P (t))
14376 {
14377 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
14378 and danagle the pointer from time to time. */
14379 if (TYPE_VFIELD (t)
14380 && TREE_CODE (TYPE_VFIELD (t)) != FIELD_DECL
14381 && TREE_CODE (TYPE_VFIELD (t)) != TREE_LIST)
14382 {
14383 error ("%<TYPE_VFIELD%> is not %<FIELD_DECL%> nor %<TREE_LIST%>");
14384 debug_tree (TYPE_VFIELD (t));
14385 error_found = true;
14386 }
14387 }
14388 else if (TREE_CODE (t) == POINTER_TYPE)
14389 {
14390 if (TYPE_NEXT_PTR_TO (t)
14391 && TREE_CODE (TYPE_NEXT_PTR_TO (t)) != POINTER_TYPE)
14392 {
14393 error ("%<TYPE_NEXT_PTR_TO%> is not %<POINTER_TYPE%>");
14394 debug_tree (TYPE_NEXT_PTR_TO (t));
14395 error_found = true;
14396 }
14397 }
14398 else if (TREE_CODE (t) == REFERENCE_TYPE)
14399 {
14400 if (TYPE_NEXT_REF_TO (t)
14401 && TREE_CODE (TYPE_NEXT_REF_TO (t)) != REFERENCE_TYPE)
14402 {
14403 error ("%<TYPE_NEXT_REF_TO%> is not %<REFERENCE_TYPE%>");
14404 debug_tree (TYPE_NEXT_REF_TO (t));
14405 error_found = true;
14406 }
14407 }
14408 else if (INTEGRAL_TYPE_P (t) || SCALAR_FLOAT_TYPE_P (t)
14409 || FIXED_POINT_TYPE_P (t))
14410 {
14411 /* FIXME: The following check should pass:
14412 useless_type_conversion_p (const_cast <tree> (t),
14413 TREE_TYPE (TYPE_MIN_VALUE (t))
14414 but does not for C sizetypes in LTO. */
14415 }
14416
14417 /* Check various uses of TYPE_MAXVAL_RAW. */
14418 if (RECORD_OR_UNION_TYPE_P (t))
14419 {
14420 if (!TYPE_BINFO (t))
14421 ;
14422 else if (TREE_CODE (TYPE_BINFO (t)) != TREE_BINFO)
14423 {
14424 error ("%<TYPE_BINFO%> is not %<TREE_BINFO%>");
14425 debug_tree (TYPE_BINFO (t));
14426 error_found = true;
14427 }
14428 else if (TREE_TYPE (TYPE_BINFO (t)) != TYPE_MAIN_VARIANT (t))
14429 {
14430 error ("%<TYPE_BINFO%> type is not %<TYPE_MAIN_VARIANT%>");
14431 debug_tree (TREE_TYPE (TYPE_BINFO (t)));
14432 error_found = true;
14433 }
14434 }
14435 else if (FUNC_OR_METHOD_TYPE_P (t))
14436 {
14437 if (TYPE_METHOD_BASETYPE (t)
14438 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != RECORD_TYPE
14439 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != UNION_TYPE)
14440 {
14441 error ("%<TYPE_METHOD_BASETYPE%> is not record nor union");
14442 debug_tree (TYPE_METHOD_BASETYPE (t));
14443 error_found = true;
14444 }
14445 }
14446 else if (TREE_CODE (t) == OFFSET_TYPE)
14447 {
14448 if (TYPE_OFFSET_BASETYPE (t)
14449 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != RECORD_TYPE
14450 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != UNION_TYPE)
14451 {
14452 error ("%<TYPE_OFFSET_BASETYPE%> is not record nor union");
14453 debug_tree (TYPE_OFFSET_BASETYPE (t));
14454 error_found = true;
14455 }
14456 }
14457 else if (INTEGRAL_TYPE_P (t) || SCALAR_FLOAT_TYPE_P (t)
14458 || FIXED_POINT_TYPE_P (t))
14459 {
14460 /* FIXME: The following check should pass:
14461 useless_type_conversion_p (const_cast <tree> (t),
14462 TREE_TYPE (TYPE_MAX_VALUE (t))
14463 but does not for C sizetypes in LTO. */
14464 }
14465 else if (TREE_CODE (t) == ARRAY_TYPE)
14466 {
14467 if (TYPE_ARRAY_MAX_SIZE (t)
14468 && TREE_CODE (TYPE_ARRAY_MAX_SIZE (t)) != INTEGER_CST)
14469 {
14470 error ("%<TYPE_ARRAY_MAX_SIZE%> not %<INTEGER_CST%>");
14471 debug_tree (TYPE_ARRAY_MAX_SIZE (t));
14472 error_found = true;
14473 }
14474 }
14475 else if (TYPE_MAX_VALUE_RAW (t))
14476 {
14477 error ("%<TYPE_MAX_VALUE_RAW%> non-NULL");
14478 debug_tree (TYPE_MAX_VALUE_RAW (t));
14479 error_found = true;
14480 }
14481
14482 if (TYPE_LANG_SLOT_1 (t) && in_lto_p)
14483 {
14484 error ("%<TYPE_LANG_SLOT_1 (binfo)%> field is non-NULL");
14485 debug_tree (TYPE_LANG_SLOT_1 (t));
14486 error_found = true;
14487 }
14488
14489 /* Check various uses of TYPE_VALUES_RAW. */
14490 if (TREE_CODE (t) == ENUMERAL_TYPE)
14491 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
14492 {
14493 tree value = TREE_VALUE (l);
14494 tree name = TREE_PURPOSE (l);
14495
14496 /* C FE porduce INTEGER_CST of INTEGER_TYPE, while C++ FE uses
14497 CONST_DECL of ENUMERAL TYPE. */
14498 if (TREE_CODE (value) != INTEGER_CST && TREE_CODE (value) != CONST_DECL)
14499 {
14500 error ("enum value is not %<CONST_DECL%> or %<INTEGER_CST%>");
14501 debug_tree (value);
14502 debug_tree (name);
14503 error_found = true;
14504 }
14505 if (TREE_CODE (TREE_TYPE (value)) != INTEGER_TYPE
14506 && TREE_CODE (TREE_TYPE (value)) != BOOLEAN_TYPE
14507 && !useless_type_conversion_p (const_cast <tree> (t), TREE_TYPE (value)))
14508 {
14509 error ("enum value type is not %<INTEGER_TYPE%> nor convertible "
14510 "to the enum");
14511 debug_tree (value);
14512 debug_tree (name);
14513 error_found = true;
14514 }
14515 if (TREE_CODE (name) != IDENTIFIER_NODE)
14516 {
14517 error ("enum value name is not %<IDENTIFIER_NODE%>");
14518 debug_tree (value);
14519 debug_tree (name);
14520 error_found = true;
14521 }
14522 }
14523 else if (TREE_CODE (t) == ARRAY_TYPE)
14524 {
14525 if (TYPE_DOMAIN (t) && TREE_CODE (TYPE_DOMAIN (t)) != INTEGER_TYPE)
14526 {
14527 error ("array %<TYPE_DOMAIN%> is not integer type");
14528 debug_tree (TYPE_DOMAIN (t));
14529 error_found = true;
14530 }
14531 }
14532 else if (RECORD_OR_UNION_TYPE_P (t))
14533 {
14534 if (TYPE_FIELDS (t) && !COMPLETE_TYPE_P (t) && in_lto_p)
14535 {
14536 error ("%<TYPE_FIELDS%> defined in incomplete type");
14537 error_found = true;
14538 }
14539 for (tree fld = TYPE_FIELDS (t); fld; fld = TREE_CHAIN (fld))
14540 {
14541 /* TODO: verify properties of decls. */
14542 if (TREE_CODE (fld) == FIELD_DECL)
14543 ;
14544 else if (TREE_CODE (fld) == TYPE_DECL)
14545 ;
14546 else if (TREE_CODE (fld) == CONST_DECL)
14547 ;
14548 else if (VAR_P (fld))
14549 ;
14550 else if (TREE_CODE (fld) == TEMPLATE_DECL)
14551 ;
14552 else if (TREE_CODE (fld) == USING_DECL)
14553 ;
14554 else if (TREE_CODE (fld) == FUNCTION_DECL)
14555 ;
14556 else
14557 {
14558 error ("wrong tree in %<TYPE_FIELDS%> list");
14559 debug_tree (fld);
14560 error_found = true;
14561 }
14562 }
14563 }
14564 else if (TREE_CODE (t) == INTEGER_TYPE
14565 || TREE_CODE (t) == BOOLEAN_TYPE
14566 || TREE_CODE (t) == BITINT_TYPE
14567 || TREE_CODE (t) == OFFSET_TYPE
14568 || TREE_CODE (t) == REFERENCE_TYPE
14569 || TREE_CODE (t) == NULLPTR_TYPE
14570 || TREE_CODE (t) == POINTER_TYPE)
14571 {
14572 if (TYPE_CACHED_VALUES_P (t) != (TYPE_CACHED_VALUES (t) != NULL))
14573 {
14574 error ("%<TYPE_CACHED_VALUES_P%> is %i while %<TYPE_CACHED_VALUES%> "
14575 "is %p",
14576 TYPE_CACHED_VALUES_P (t), (void *)TYPE_CACHED_VALUES (t));
14577 error_found = true;
14578 }
14579 else if (TYPE_CACHED_VALUES_P (t) && TREE_CODE (TYPE_CACHED_VALUES (t)) != TREE_VEC)
14580 {
14581 error ("%<TYPE_CACHED_VALUES%> is not %<TREE_VEC%>");
14582 debug_tree (TYPE_CACHED_VALUES (t));
14583 error_found = true;
14584 }
14585 /* Verify just enough of cache to ensure that no one copied it to new type.
14586 All copying should go by copy_node that should clear it. */
14587 else if (TYPE_CACHED_VALUES_P (t))
14588 {
14589 int i;
14590 for (i = 0; i < TREE_VEC_LENGTH (TYPE_CACHED_VALUES (t)); i++)
14591 if (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)
14592 && TREE_TYPE (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)) != t)
14593 {
14594 error ("wrong %<TYPE_CACHED_VALUES%> entry");
14595 debug_tree (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i));
14596 error_found = true;
14597 break;
14598 }
14599 }
14600 }
14601 else if (FUNC_OR_METHOD_TYPE_P (t))
14602 for (tree l = TYPE_ARG_TYPES (t); l; l = TREE_CHAIN (l))
14603 {
14604 /* C++ FE uses TREE_PURPOSE to store initial values. */
14605 if (TREE_PURPOSE (l) && in_lto_p)
14606 {
14607 error ("%<TREE_PURPOSE%> is non-NULL in %<TYPE_ARG_TYPES%> list");
14608 debug_tree (l);
14609 error_found = true;
14610 }
14611 if (!TYPE_P (TREE_VALUE (l)))
14612 {
14613 error ("wrong entry in %<TYPE_ARG_TYPES%> list");
14614 debug_tree (l);
14615 error_found = true;
14616 }
14617 }
14618 else if (!is_lang_specific (t) && TYPE_VALUES_RAW (t))
14619 {
14620 error ("%<TYPE_VALUES_RAW%> field is non-NULL");
14621 debug_tree (TYPE_VALUES_RAW (t));
14622 error_found = true;
14623 }
14624 if (TREE_CODE (t) != INTEGER_TYPE
14625 && TREE_CODE (t) != BOOLEAN_TYPE
14626 && TREE_CODE (t) != BITINT_TYPE
14627 && TREE_CODE (t) != OFFSET_TYPE
14628 && TREE_CODE (t) != REFERENCE_TYPE
14629 && TREE_CODE (t) != NULLPTR_TYPE
14630 && TREE_CODE (t) != POINTER_TYPE
14631 && TYPE_CACHED_VALUES_P (t))
14632 {
14633 error ("%<TYPE_CACHED_VALUES_P%> is set while it should not be");
14634 error_found = true;
14635 }
14636
14637 /* ipa-devirt makes an assumption that TYPE_METHOD_BASETYPE is always
14638 TYPE_MAIN_VARIANT and it would be odd to add methods only to variatns
14639 of a type. */
14640 if (TREE_CODE (t) == METHOD_TYPE
14641 && TYPE_MAIN_VARIANT (TYPE_METHOD_BASETYPE (t)) != TYPE_METHOD_BASETYPE (t))
14642 {
14643 error ("%<TYPE_METHOD_BASETYPE%> is not main variant");
14644 error_found = true;
14645 }
14646
14647 if (error_found)
14648 {
14649 debug_tree (const_cast <tree> (t));
14650 internal_error ("%qs failed", __func__);
14651 }
14652 }
14653
14654
14655 /* Return 1 if ARG interpreted as signed in its precision is known to be
14656 always non-negative or 2 if ARG is known to be always negative, or 3 if
14657 ARG may be non-negative or negative. */
14658
14659 int
14660 get_range_pos_neg (tree arg)
14661 {
14662 if (arg == error_mark_node)
14663 return 3;
14664
14665 int prec = TYPE_PRECISION (TREE_TYPE (arg));
14666 int cnt = 0;
14667 if (TREE_CODE (arg) == INTEGER_CST)
14668 {
14669 wide_int w = wi::sext (wi::to_wide (arg), prec);
14670 if (wi::neg_p (w))
14671 return 2;
14672 else
14673 return 1;
14674 }
14675 while (CONVERT_EXPR_P (arg)
14676 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
14677 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg, 0))) <= prec)
14678 {
14679 arg = TREE_OPERAND (arg, 0);
14680 /* Narrower value zero extended into wider type
14681 will always result in positive values. */
14682 if (TYPE_UNSIGNED (TREE_TYPE (arg))
14683 && TYPE_PRECISION (TREE_TYPE (arg)) < prec)
14684 return 1;
14685 prec = TYPE_PRECISION (TREE_TYPE (arg));
14686 if (++cnt > 30)
14687 return 3;
14688 }
14689
14690 if (TREE_CODE (arg) != SSA_NAME)
14691 return 3;
14692 int_range_max r;
14693 while (!get_global_range_query ()->range_of_expr (r, arg)
14694 || r.undefined_p () || r.varying_p ())
14695 {
14696 gimple *g = SSA_NAME_DEF_STMT (arg);
14697 if (is_gimple_assign (g)
14698 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (g)))
14699 {
14700 tree t = gimple_assign_rhs1 (g);
14701 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
14702 && TYPE_PRECISION (TREE_TYPE (t)) <= prec)
14703 {
14704 if (TYPE_UNSIGNED (TREE_TYPE (t))
14705 && TYPE_PRECISION (TREE_TYPE (t)) < prec)
14706 return 1;
14707 prec = TYPE_PRECISION (TREE_TYPE (t));
14708 arg = t;
14709 if (++cnt > 30)
14710 return 3;
14711 continue;
14712 }
14713 }
14714 return 3;
14715 }
14716 if (TYPE_UNSIGNED (TREE_TYPE (arg)))
14717 {
14718 /* For unsigned values, the "positive" range comes
14719 below the "negative" range. */
14720 if (!wi::neg_p (wi::sext (r.upper_bound (), prec), SIGNED))
14721 return 1;
14722 if (wi::neg_p (wi::sext (r.lower_bound (), prec), SIGNED))
14723 return 2;
14724 }
14725 else
14726 {
14727 if (!wi::neg_p (wi::sext (r.lower_bound (), prec), SIGNED))
14728 return 1;
14729 if (wi::neg_p (wi::sext (r.upper_bound (), prec), SIGNED))
14730 return 2;
14731 }
14732 return 3;
14733 }
14734
14735
14736
14737
14738 /* Return true if ARG is marked with the nonnull attribute in the
14739 current function signature. */
14740
14741 bool
14742 nonnull_arg_p (const_tree arg)
14743 {
14744 tree t, attrs, fntype;
14745 unsigned HOST_WIDE_INT arg_num;
14746
14747 gcc_assert (TREE_CODE (arg) == PARM_DECL
14748 && (POINTER_TYPE_P (TREE_TYPE (arg))
14749 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE));
14750
14751 /* The static chain decl is always non null. */
14752 if (arg == cfun->static_chain_decl)
14753 return true;
14754
14755 /* THIS argument of method is always non-NULL. */
14756 if (TREE_CODE (TREE_TYPE (cfun->decl)) == METHOD_TYPE
14757 && arg == DECL_ARGUMENTS (cfun->decl)
14758 && flag_delete_null_pointer_checks)
14759 return true;
14760
14761 /* Values passed by reference are always non-NULL. */
14762 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
14763 && flag_delete_null_pointer_checks)
14764 return true;
14765
14766 fntype = TREE_TYPE (cfun->decl);
14767 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
14768 {
14769 attrs = lookup_attribute ("nonnull", attrs);
14770
14771 /* If "nonnull" wasn't specified, we know nothing about the argument. */
14772 if (attrs == NULL_TREE)
14773 return false;
14774
14775 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
14776 if (TREE_VALUE (attrs) == NULL_TREE)
14777 return true;
14778
14779 /* Get the position number for ARG in the function signature. */
14780 for (arg_num = 1, t = DECL_ARGUMENTS (cfun->decl);
14781 t;
14782 t = DECL_CHAIN (t), arg_num++)
14783 {
14784 if (t == arg)
14785 break;
14786 }
14787
14788 gcc_assert (t == arg);
14789
14790 /* Now see if ARG_NUM is mentioned in the nonnull list. */
14791 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
14792 {
14793 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
14794 return true;
14795 }
14796 }
14797
14798 return false;
14799 }
14800
14801 /* Combine LOC and BLOCK to a combined adhoc loc, retaining any range
14802 information. */
14803
14804 location_t
14805 set_block (location_t loc, tree block)
14806 {
14807 location_t pure_loc = get_pure_location (loc);
14808 source_range src_range = get_range_from_loc (line_table, loc);
14809 unsigned discriminator = get_discriminator_from_loc (line_table, loc);
14810 return line_table->get_or_create_combined_loc (pure_loc, src_range, block,
14811 discriminator);
14812 }
14813
14814 location_t
14815 set_source_range (tree expr, location_t start, location_t finish)
14816 {
14817 source_range src_range;
14818 src_range.m_start = start;
14819 src_range.m_finish = finish;
14820 return set_source_range (expr, src_range);
14821 }
14822
14823 location_t
14824 set_source_range (tree expr, source_range src_range)
14825 {
14826 if (!EXPR_P (expr))
14827 return UNKNOWN_LOCATION;
14828
14829 location_t expr_location = EXPR_LOCATION (expr);
14830 location_t pure_loc = get_pure_location (expr_location);
14831 unsigned discriminator = get_discriminator_from_loc (expr_location);
14832 location_t adhoc = line_table->get_or_create_combined_loc (pure_loc,
14833 src_range,
14834 nullptr,
14835 discriminator);
14836 SET_EXPR_LOCATION (expr, adhoc);
14837 return adhoc;
14838 }
14839
14840 /* Return EXPR, potentially wrapped with a node expression LOC,
14841 if !CAN_HAVE_LOCATION_P (expr).
14842
14843 NON_LVALUE_EXPR is used for wrapping constants, apart from STRING_CST.
14844 VIEW_CONVERT_EXPR is used for wrapping non-constants and STRING_CST.
14845
14846 Wrapper nodes can be identified using location_wrapper_p. */
14847
14848 tree
14849 maybe_wrap_with_location (tree expr, location_t loc)
14850 {
14851 if (expr == NULL)
14852 return NULL;
14853 if (loc == UNKNOWN_LOCATION)
14854 return expr;
14855 if (CAN_HAVE_LOCATION_P (expr))
14856 return expr;
14857 /* We should only be adding wrappers for constants and for decls,
14858 or for some exceptional tree nodes (e.g. BASELINK in the C++ FE). */
14859 gcc_assert (CONSTANT_CLASS_P (expr)
14860 || DECL_P (expr)
14861 || EXCEPTIONAL_CLASS_P (expr));
14862
14863 /* For now, don't add wrappers to exceptional tree nodes, to minimize
14864 any impact of the wrapper nodes. */
14865 if (EXCEPTIONAL_CLASS_P (expr) || error_operand_p (expr))
14866 return expr;
14867
14868 /* Compiler-generated temporary variables don't need a wrapper. */
14869 if (DECL_P (expr) && DECL_ARTIFICIAL (expr) && DECL_IGNORED_P (expr))
14870 return expr;
14871
14872 /* If any auto_suppress_location_wrappers are active, don't create
14873 wrappers. */
14874 if (suppress_location_wrappers > 0)
14875 return expr;
14876
14877 tree_code code
14878 = (((CONSTANT_CLASS_P (expr) && TREE_CODE (expr) != STRING_CST)
14879 || (TREE_CODE (expr) == CONST_DECL && !TREE_STATIC (expr)))
14880 ? NON_LVALUE_EXPR : VIEW_CONVERT_EXPR);
14881 tree wrapper = build1_loc (loc, code, TREE_TYPE (expr), expr);
14882 /* Mark this node as being a wrapper. */
14883 EXPR_LOCATION_WRAPPER_P (wrapper) = 1;
14884 return wrapper;
14885 }
14886
14887 int suppress_location_wrappers;
14888
14889 /* Return the name of combined function FN, for debugging purposes. */
14890
14891 const char *
14892 combined_fn_name (combined_fn fn)
14893 {
14894 if (builtin_fn_p (fn))
14895 {
14896 tree fndecl = builtin_decl_explicit (as_builtin_fn (fn));
14897 return IDENTIFIER_POINTER (DECL_NAME (fndecl));
14898 }
14899 else
14900 return internal_fn_name (as_internal_fn (fn));
14901 }
14902
14903 /* Return a bitmap with a bit set corresponding to each argument in
14904 a function call type FNTYPE declared with attribute nonnull,
14905 or null if none of the function's argument are nonnull. The caller
14906 must free the bitmap. */
14907
14908 bitmap
14909 get_nonnull_args (const_tree fntype)
14910 {
14911 if (fntype == NULL_TREE)
14912 return NULL;
14913
14914 bitmap argmap = NULL;
14915 if (TREE_CODE (fntype) == METHOD_TYPE)
14916 {
14917 /* The this pointer in C++ non-static member functions is
14918 implicitly nonnull whether or not it's declared as such. */
14919 argmap = BITMAP_ALLOC (NULL);
14920 bitmap_set_bit (argmap, 0);
14921 }
14922
14923 tree attrs = TYPE_ATTRIBUTES (fntype);
14924 if (!attrs)
14925 return argmap;
14926
14927 /* A function declaration can specify multiple attribute nonnull,
14928 each with zero or more arguments. The loop below creates a bitmap
14929 representing a union of all the arguments. An empty (but non-null)
14930 bitmap means that all arguments have been declared nonnull. */
14931 for ( ; attrs; attrs = TREE_CHAIN (attrs))
14932 {
14933 attrs = lookup_attribute ("nonnull", attrs);
14934 if (!attrs)
14935 break;
14936
14937 if (!argmap)
14938 argmap = BITMAP_ALLOC (NULL);
14939
14940 if (!TREE_VALUE (attrs))
14941 {
14942 /* Clear the bitmap in case a previous attribute nonnull
14943 set it and this one overrides it for all arguments. */
14944 bitmap_clear (argmap);
14945 return argmap;
14946 }
14947
14948 /* Iterate over the indices of the format arguments declared nonnull
14949 and set a bit for each. */
14950 for (tree idx = TREE_VALUE (attrs); idx; idx = TREE_CHAIN (idx))
14951 {
14952 unsigned int val = TREE_INT_CST_LOW (TREE_VALUE (idx)) - 1;
14953 bitmap_set_bit (argmap, val);
14954 }
14955 }
14956
14957 return argmap;
14958 }
14959
14960 /* Returns true if TYPE is a type where it and all of its subobjects
14961 (recursively) are of structure, union, or array type. */
14962
14963 bool
14964 is_empty_type (const_tree type)
14965 {
14966 if (RECORD_OR_UNION_TYPE_P (type))
14967 {
14968 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
14969 if (TREE_CODE (field) == FIELD_DECL
14970 && !DECL_PADDING_P (field)
14971 && !is_empty_type (TREE_TYPE (field)))
14972 return false;
14973 return true;
14974 }
14975 else if (TREE_CODE (type) == ARRAY_TYPE)
14976 return (integer_minus_onep (array_type_nelts_minus_one (type))
14977 || TYPE_DOMAIN (type) == NULL_TREE
14978 || is_empty_type (TREE_TYPE (type)));
14979 return false;
14980 }
14981
14982 /* Implement TARGET_EMPTY_RECORD_P. Return true if TYPE is an empty type
14983 that shouldn't be passed via stack. */
14984
14985 bool
14986 default_is_empty_record (const_tree type)
14987 {
14988 if (!abi_version_at_least (12))
14989 return false;
14990
14991 if (type == error_mark_node)
14992 return false;
14993
14994 if (TREE_ADDRESSABLE (type))
14995 return false;
14996
14997 return is_empty_type (TYPE_MAIN_VARIANT (type));
14998 }
14999
15000 /* Determine whether TYPE is a structure with a flexible array member,
15001 or a union containing such a structure (possibly recursively). */
15002
15003 bool
15004 flexible_array_type_p (const_tree type)
15005 {
15006 tree x, last;
15007 switch (TREE_CODE (type))
15008 {
15009 case RECORD_TYPE:
15010 last = NULL_TREE;
15011 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
15012 if (TREE_CODE (x) == FIELD_DECL)
15013 last = x;
15014 if (last == NULL_TREE)
15015 return false;
15016 if (TREE_CODE (TREE_TYPE (last)) == ARRAY_TYPE
15017 && TYPE_SIZE (TREE_TYPE (last)) == NULL_TREE
15018 && TYPE_DOMAIN (TREE_TYPE (last)) != NULL_TREE
15019 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (last))) == NULL_TREE)
15020 return true;
15021 return false;
15022 case UNION_TYPE:
15023 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
15024 {
15025 if (TREE_CODE (x) == FIELD_DECL
15026 && flexible_array_type_p (TREE_TYPE (x)))
15027 return true;
15028 }
15029 return false;
15030 default:
15031 return false;
15032 }
15033 }
15034
15035 /* Like int_size_in_bytes, but handle empty records specially. */
15036
15037 HOST_WIDE_INT
15038 arg_int_size_in_bytes (const_tree type)
15039 {
15040 return TYPE_EMPTY_P (type) ? 0 : int_size_in_bytes (type);
15041 }
15042
15043 /* Like size_in_bytes, but handle empty records specially. */
15044
15045 tree
15046 arg_size_in_bytes (const_tree type)
15047 {
15048 return TYPE_EMPTY_P (type) ? size_zero_node : size_in_bytes (type);
15049 }
15050
15051 /* Return true if an expression with CODE has to have the same result type as
15052 its first operand. */
15053
15054 bool
15055 expr_type_first_operand_type_p (tree_code code)
15056 {
15057 switch (code)
15058 {
15059 case NEGATE_EXPR:
15060 case ABS_EXPR:
15061 case BIT_NOT_EXPR:
15062 case PAREN_EXPR:
15063 case CONJ_EXPR:
15064
15065 case PLUS_EXPR:
15066 case MINUS_EXPR:
15067 case MULT_EXPR:
15068 case TRUNC_DIV_EXPR:
15069 case CEIL_DIV_EXPR:
15070 case FLOOR_DIV_EXPR:
15071 case ROUND_DIV_EXPR:
15072 case TRUNC_MOD_EXPR:
15073 case CEIL_MOD_EXPR:
15074 case FLOOR_MOD_EXPR:
15075 case ROUND_MOD_EXPR:
15076 case RDIV_EXPR:
15077 case EXACT_DIV_EXPR:
15078 case MIN_EXPR:
15079 case MAX_EXPR:
15080 case BIT_IOR_EXPR:
15081 case BIT_XOR_EXPR:
15082 case BIT_AND_EXPR:
15083
15084 case LSHIFT_EXPR:
15085 case RSHIFT_EXPR:
15086 case LROTATE_EXPR:
15087 case RROTATE_EXPR:
15088 return true;
15089
15090 default:
15091 return false;
15092 }
15093 }
15094
15095 /* Return a typenode for the "standard" C type with a given name. */
15096 tree
15097 get_typenode_from_name (const char *name)
15098 {
15099 if (name == NULL || *name == '\0')
15100 return NULL_TREE;
15101
15102 if (strcmp (name, "char") == 0)
15103 return char_type_node;
15104 if (strcmp (name, "unsigned char") == 0)
15105 return unsigned_char_type_node;
15106 if (strcmp (name, "signed char") == 0)
15107 return signed_char_type_node;
15108
15109 if (strcmp (name, "short int") == 0)
15110 return short_integer_type_node;
15111 if (strcmp (name, "short unsigned int") == 0)
15112 return short_unsigned_type_node;
15113
15114 if (strcmp (name, "int") == 0)
15115 return integer_type_node;
15116 if (strcmp (name, "unsigned int") == 0)
15117 return unsigned_type_node;
15118
15119 if (strcmp (name, "long int") == 0)
15120 return long_integer_type_node;
15121 if (strcmp (name, "long unsigned int") == 0)
15122 return long_unsigned_type_node;
15123
15124 if (strcmp (name, "long long int") == 0)
15125 return long_long_integer_type_node;
15126 if (strcmp (name, "long long unsigned int") == 0)
15127 return long_long_unsigned_type_node;
15128
15129 gcc_unreachable ();
15130 }
15131
15132 /* List of pointer types used to declare builtins before we have seen their
15133 real declaration.
15134
15135 Keep the size up to date in tree.h ! */
15136 const builtin_structptr_type builtin_structptr_types[6] =
15137 {
15138 { fileptr_type_node, ptr_type_node, "FILE" },
15139 { const_tm_ptr_type_node, const_ptr_type_node, "tm" },
15140 { fenv_t_ptr_type_node, ptr_type_node, "fenv_t" },
15141 { const_fenv_t_ptr_type_node, const_ptr_type_node, "fenv_t" },
15142 { fexcept_t_ptr_type_node, ptr_type_node, "fexcept_t" },
15143 { const_fexcept_t_ptr_type_node, const_ptr_type_node, "fexcept_t" }
15144 };
15145
15146 /* Return the maximum object size. */
15147
15148 tree
15149 max_object_size (void)
15150 {
15151 /* To do: Make this a configurable parameter. */
15152 return TYPE_MAX_VALUE (ptrdiff_type_node);
15153 }
15154
15155 /* A wrapper around TARGET_VERIFY_TYPE_CONTEXT that makes the silent_p
15156 parameter default to false and that weeds out error_mark_node. */
15157
15158 bool
15159 verify_type_context (location_t loc, type_context_kind context,
15160 const_tree type, bool silent_p)
15161 {
15162 if (type == error_mark_node)
15163 return true;
15164
15165 gcc_assert (TYPE_P (type));
15166 return (!targetm.verify_type_context
15167 || targetm.verify_type_context (loc, context, type, silent_p));
15168 }
15169
15170 /* Return true if NEW_ASM and DELETE_ASM name a valid pair of new and
15171 delete operators. Return false if they may or may not name such
15172 a pair and, when nonnull, set *PCERTAIN to true if they certainly
15173 do not. */
15174
15175 bool
15176 valid_new_delete_pair_p (tree new_asm, tree delete_asm,
15177 bool *pcertain /* = NULL */)
15178 {
15179 bool certain;
15180 if (!pcertain)
15181 pcertain = &certain;
15182
15183 const char *new_name = IDENTIFIER_POINTER (new_asm);
15184 const char *delete_name = IDENTIFIER_POINTER (delete_asm);
15185 unsigned int new_len = IDENTIFIER_LENGTH (new_asm);
15186 unsigned int delete_len = IDENTIFIER_LENGTH (delete_asm);
15187
15188 /* The following failures are due to invalid names so they're not
15189 considered certain mismatches. */
15190 *pcertain = false;
15191
15192 if (new_len < 5 || delete_len < 6)
15193 return false;
15194 if (new_name[0] == '_')
15195 ++new_name, --new_len;
15196 if (new_name[0] == '_')
15197 ++new_name, --new_len;
15198 if (delete_name[0] == '_')
15199 ++delete_name, --delete_len;
15200 if (delete_name[0] == '_')
15201 ++delete_name, --delete_len;
15202 if (new_len < 4 || delete_len < 5)
15203 return false;
15204
15205 /* The following failures are due to names of user-defined operators
15206 so they're also not considered certain mismatches. */
15207
15208 /* *_len is now just the length after initial underscores. */
15209 if (new_name[0] != 'Z' || new_name[1] != 'n')
15210 return false;
15211 if (delete_name[0] != 'Z' || delete_name[1] != 'd')
15212 return false;
15213
15214 /* The following failures are certain mismatches. */
15215 *pcertain = true;
15216
15217 /* _Znw must match _Zdl, _Zna must match _Zda. */
15218 if ((new_name[2] != 'w' || delete_name[2] != 'l')
15219 && (new_name[2] != 'a' || delete_name[2] != 'a'))
15220 return false;
15221 if (new_name[3] == 'I' || delete_name[3] == 'I')
15222 {
15223 /* When ::operator new or ::operator delete are function templates,
15224 return uncertain mismatch, we need demangler in that case. */
15225 *pcertain = false;
15226 return false;
15227 }
15228 /* 'j', 'm' and 'y' correspond to size_t. */
15229 if (new_name[3] != 'j' && new_name[3] != 'm' && new_name[3] != 'y')
15230 return false;
15231 if (delete_name[3] != 'P' || delete_name[4] != 'v')
15232 return false;
15233 if (new_len == 4
15234 || (new_len == 18 && !memcmp (new_name + 4, "RKSt9nothrow_t", 14)))
15235 {
15236 /* _ZnXY or _ZnXYRKSt9nothrow_t matches
15237 _ZdXPv, _ZdXPvY and _ZdXPvRKSt9nothrow_t. */
15238 if (delete_len == 5)
15239 return true;
15240 if (delete_len == 6 && delete_name[5] == new_name[3])
15241 return true;
15242 if (delete_len == 19 && !memcmp (delete_name + 5, "RKSt9nothrow_t", 14))
15243 return true;
15244 }
15245 else if ((new_len == 19 && !memcmp (new_name + 4, "St11align_val_t", 15))
15246 || (new_len == 33
15247 && !memcmp (new_name + 4, "St11align_val_tRKSt9nothrow_t", 29)))
15248 {
15249 /* _ZnXYSt11align_val_t or _ZnXYSt11align_val_tRKSt9nothrow_t matches
15250 _ZdXPvSt11align_val_t or _ZdXPvYSt11align_val_t or
15251 _ZdXPvSt11align_val_tRKSt9nothrow_t. */
15252 if (delete_len == 20 && !memcmp (delete_name + 5, "St11align_val_t", 15))
15253 return true;
15254 if (delete_len == 21
15255 && delete_name[5] == new_name[3]
15256 && !memcmp (delete_name + 6, "St11align_val_t", 15))
15257 return true;
15258 if (delete_len == 34
15259 && !memcmp (delete_name + 5, "St11align_val_tRKSt9nothrow_t", 29))
15260 return true;
15261 }
15262
15263 /* The negative result is conservative. */
15264 *pcertain = false;
15265 return false;
15266 }
15267
15268 /* Return the zero-based number corresponding to the argument being
15269 deallocated if FNDECL is a deallocation function or an out-of-bounds
15270 value if it isn't. */
15271
15272 unsigned
15273 fndecl_dealloc_argno (tree fndecl)
15274 {
15275 /* A call to operator delete isn't recognized as one to a built-in. */
15276 if (DECL_IS_OPERATOR_DELETE_P (fndecl))
15277 {
15278 if (DECL_IS_REPLACEABLE_OPERATOR (fndecl))
15279 return 0;
15280
15281 /* Avoid placement delete that's not been inlined. */
15282 tree fname = DECL_ASSEMBLER_NAME (fndecl);
15283 if (id_equal (fname, "_ZdlPvS_") // ordinary form
15284 || id_equal (fname, "_ZdaPvS_")) // array form
15285 return UINT_MAX;
15286 return 0;
15287 }
15288
15289 /* TODO: Handle user-defined functions with attribute malloc? Handle
15290 known non-built-ins like fopen? */
15291 if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
15292 {
15293 switch (DECL_FUNCTION_CODE (fndecl))
15294 {
15295 case BUILT_IN_FREE:
15296 case BUILT_IN_REALLOC:
15297 case BUILT_IN_GOMP_FREE:
15298 case BUILT_IN_GOMP_REALLOC:
15299 return 0;
15300 default:
15301 break;
15302 }
15303 return UINT_MAX;
15304 }
15305
15306 tree attrs = DECL_ATTRIBUTES (fndecl);
15307 if (!attrs)
15308 return UINT_MAX;
15309
15310 for (tree atfree = attrs;
15311 (atfree = lookup_attribute ("*dealloc", atfree));
15312 atfree = TREE_CHAIN (atfree))
15313 {
15314 tree alloc = TREE_VALUE (atfree);
15315 if (!alloc)
15316 continue;
15317
15318 tree pos = TREE_CHAIN (alloc);
15319 if (!pos)
15320 return 0;
15321
15322 pos = TREE_VALUE (pos);
15323 return TREE_INT_CST_LOW (pos) - 1;
15324 }
15325
15326 return UINT_MAX;
15327 }
15328
15329 /* If EXPR refers to a character array or pointer declared attribute
15330 nonstring, return a decl for that array or pointer and set *REF
15331 to the referenced enclosing object or pointer. Otherwise return
15332 null. */
15333
15334 tree
15335 get_attr_nonstring_decl (tree expr, tree *ref)
15336 {
15337 tree decl = expr;
15338 tree var = NULL_TREE;
15339 if (TREE_CODE (decl) == SSA_NAME)
15340 {
15341 gimple *def = SSA_NAME_DEF_STMT (decl);
15342
15343 if (is_gimple_assign (def))
15344 {
15345 tree_code code = gimple_assign_rhs_code (def);
15346 if (code == ADDR_EXPR
15347 || code == COMPONENT_REF
15348 || code == VAR_DECL)
15349 decl = gimple_assign_rhs1 (def);
15350 }
15351 else
15352 var = SSA_NAME_VAR (decl);
15353 }
15354
15355 if (TREE_CODE (decl) == ADDR_EXPR)
15356 decl = TREE_OPERAND (decl, 0);
15357
15358 /* To simplify calling code, store the referenced DECL regardless of
15359 the attribute determined below, but avoid storing the SSA_NAME_VAR
15360 obtained above (it's not useful for dataflow purposes). */
15361 if (ref)
15362 *ref = decl;
15363
15364 /* Use the SSA_NAME_VAR that was determined above to see if it's
15365 declared nonstring. Otherwise drill down into the referenced
15366 DECL. */
15367 if (var)
15368 decl = var;
15369 else
15370 {
15371 while (TREE_CODE (decl) == ARRAY_REF)
15372 decl = TREE_OPERAND (decl, 0);
15373 if (TREE_CODE (decl) == COMPONENT_REF)
15374 decl = TREE_OPERAND (decl, 1);
15375 else if (TREE_CODE (decl) == MEM_REF)
15376 return get_attr_nonstring_decl (TREE_OPERAND (decl, 0), ref);
15377 }
15378
15379 if (DECL_P (decl) && lookup_attribute ("nonstring", DECL_ATTRIBUTES (decl)))
15380 return decl;
15381
15382 return NULL_TREE;
15383 }
15384
15385 /* Return length of attribute names string,
15386 if arglist chain > 1, -1 otherwise. */
15387
15388 int
15389 get_target_clone_attr_len (tree arglist)
15390 {
15391 tree arg;
15392 int str_len_sum = 0;
15393 int argnum = 0;
15394
15395 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
15396 {
15397 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
15398 size_t len = strlen (str);
15399 str_len_sum += len + 1;
15400 for (const char *p = strchr (str, TARGET_CLONES_ATTR_SEPARATOR);
15401 p;
15402 p = strchr (p + 1, TARGET_CLONES_ATTR_SEPARATOR))
15403 argnum++;
15404 argnum++;
15405 }
15406 if (argnum <= 1)
15407 return -1;
15408 return str_len_sum;
15409 }
15410
15411 void
15412 tree_cc_finalize (void)
15413 {
15414 clear_nonstandard_integer_type_cache ();
15415 vec_free (bitint_type_cache);
15416 }
15417
15418 void
15419 gt_ggc_mx (tree_raw_data *x)
15420 {
15421 gt_ggc_m_9tree_node (x->typed.type);
15422 gt_ggc_m_9tree_node (x->owner);
15423 }
15424
15425 void
15426 gt_pch_nx (tree_raw_data *x)
15427 {
15428 gt_pch_n_9tree_node (x->typed.type);
15429 gt_pch_n_9tree_node (x->owner);
15430 }
15431
15432 /* For PCH we guarantee that RAW_DATA_CST's RAW_DATA_OWNER is a STRING_CST and
15433 RAW_DATA_POINTER points into it. We don't want to save/restore
15434 RAW_DATA_POINTER on its own but want to restore it pointing at the same
15435 offset of the STRING_CST as before. */
15436
15437 void
15438 gt_pch_nx (tree_raw_data *x, gt_pointer_operator op, void *cookie)
15439 {
15440 op (&x->typed.type, NULL, cookie);
15441 gcc_checking_assert (x->owner
15442 && TREE_CODE (x->owner) == STRING_CST
15443 && x->str >= TREE_STRING_POINTER (x->owner)
15444 && (x->str + x->length
15445 <= (TREE_STRING_POINTER (x->owner)
15446 + TREE_STRING_LENGTH (x->owner))));
15447 ptrdiff_t off = x->str - (const char *) (x->owner);
15448 tree owner = x->owner;
15449 op (&x->owner, NULL, cookie);
15450 x->owner = owner;
15451 /* The above op call relocates x->owner and remembers the address
15452 for relocation e.g. if the compiler is position independent.
15453 We then restore x->owner back to its previous value and call
15454 op again, for x->owner itself this just repeats (uselessly) what
15455 the first call did, but as the second argument is now non-NULL
15456 and different, it also arranges for &x->str to be noted for the
15457 PIE relocation. */
15458 op (&x->owner, &x->str, cookie);
15459 x->str = (const char *) (x->owner) + off;
15460 }
15461
15462 #if CHECKING_P
15463
15464 namespace selftest {
15465
15466 /* Selftests for tree. */
15467
15468 /* Verify that integer constants are sane. */
15469
15470 static void
15471 test_integer_constants ()
15472 {
15473 ASSERT_TRUE (integer_type_node != NULL);
15474 ASSERT_TRUE (build_int_cst (integer_type_node, 0) != NULL);
15475
15476 tree type = integer_type_node;
15477
15478 tree zero = build_zero_cst (type);
15479 ASSERT_EQ (INTEGER_CST, TREE_CODE (zero));
15480 ASSERT_EQ (type, TREE_TYPE (zero));
15481
15482 tree one = build_int_cst (type, 1);
15483 ASSERT_EQ (INTEGER_CST, TREE_CODE (one));
15484 ASSERT_EQ (type, TREE_TYPE (zero));
15485 }
15486
15487 /* Verify identifiers. */
15488
15489 static void
15490 test_identifiers ()
15491 {
15492 tree identifier = get_identifier ("foo");
15493 ASSERT_EQ (3, IDENTIFIER_LENGTH (identifier));
15494 ASSERT_STREQ ("foo", IDENTIFIER_POINTER (identifier));
15495 }
15496
15497 /* Verify LABEL_DECL. */
15498
15499 static void
15500 test_labels ()
15501 {
15502 tree identifier = get_identifier ("err");
15503 tree label_decl = build_decl (UNKNOWN_LOCATION, LABEL_DECL,
15504 identifier, void_type_node);
15505 ASSERT_EQ (-1, LABEL_DECL_UID (label_decl));
15506 ASSERT_FALSE (FORCED_LABEL (label_decl));
15507 }
15508
15509 /* Return a new VECTOR_CST node whose type is TYPE and whose values
15510 are given by VALS. */
15511
15512 static tree
15513 build_vector (tree type, const vec<tree> &vals MEM_STAT_DECL)
15514 {
15515 gcc_assert (known_eq (vals.length (), TYPE_VECTOR_SUBPARTS (type)));
15516 tree_vector_builder builder (type, vals.length (), 1);
15517 builder.splice (vals);
15518 return builder.build ();
15519 }
15520
15521 /* Check that VECTOR_CST ACTUAL contains the elements in EXPECTED. */
15522
15523 static void
15524 check_vector_cst (const vec<tree> &expected, tree actual)
15525 {
15526 ASSERT_KNOWN_EQ (expected.length (),
15527 TYPE_VECTOR_SUBPARTS (TREE_TYPE (actual)));
15528 for (unsigned int i = 0; i < expected.length (); ++i)
15529 ASSERT_EQ (wi::to_wide (expected[i]),
15530 wi::to_wide (vector_cst_elt (actual, i)));
15531 }
15532
15533 /* Check that VECTOR_CST ACTUAL contains NPATTERNS duplicated elements,
15534 and that its elements match EXPECTED. */
15535
15536 static void
15537 check_vector_cst_duplicate (const vec<tree> &expected, tree actual,
15538 unsigned int npatterns)
15539 {
15540 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
15541 ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (actual));
15542 ASSERT_EQ (npatterns, vector_cst_encoded_nelts (actual));
15543 ASSERT_TRUE (VECTOR_CST_DUPLICATE_P (actual));
15544 ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
15545 check_vector_cst (expected, actual);
15546 }
15547
15548 /* Check that VECTOR_CST ACTUAL contains NPATTERNS foreground elements
15549 and NPATTERNS background elements, and that its elements match
15550 EXPECTED. */
15551
15552 static void
15553 check_vector_cst_fill (const vec<tree> &expected, tree actual,
15554 unsigned int npatterns)
15555 {
15556 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
15557 ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (actual));
15558 ASSERT_EQ (2 * npatterns, vector_cst_encoded_nelts (actual));
15559 ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
15560 ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
15561 check_vector_cst (expected, actual);
15562 }
15563
15564 /* Check that VECTOR_CST ACTUAL contains NPATTERNS stepped patterns,
15565 and that its elements match EXPECTED. */
15566
15567 static void
15568 check_vector_cst_stepped (const vec<tree> &expected, tree actual,
15569 unsigned int npatterns)
15570 {
15571 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
15572 ASSERT_EQ (3, VECTOR_CST_NELTS_PER_PATTERN (actual));
15573 ASSERT_EQ (3 * npatterns, vector_cst_encoded_nelts (actual));
15574 ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
15575 ASSERT_TRUE (VECTOR_CST_STEPPED_P (actual));
15576 check_vector_cst (expected, actual);
15577 }
15578
15579 /* Test the creation of VECTOR_CSTs. */
15580
15581 static void
15582 test_vector_cst_patterns (ALONE_CXX_MEM_STAT_INFO)
15583 {
15584 auto_vec<tree, 8> elements (8);
15585 elements.quick_grow (8);
15586 tree element_type = build_nonstandard_integer_type (16, true);
15587 tree vector_type = build_vector_type (element_type, 8);
15588
15589 /* Test a simple linear series with a base of 0 and a step of 1:
15590 { 0, 1, 2, 3, 4, 5, 6, 7 }. */
15591 for (unsigned int i = 0; i < 8; ++i)
15592 elements[i] = build_int_cst (element_type, i);
15593 tree vector = build_vector (vector_type, elements PASS_MEM_STAT);
15594 check_vector_cst_stepped (elements, vector, 1);
15595
15596 /* Try the same with the first element replaced by 100:
15597 { 100, 1, 2, 3, 4, 5, 6, 7 }. */
15598 elements[0] = build_int_cst (element_type, 100);
15599 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15600 check_vector_cst_stepped (elements, vector, 1);
15601
15602 /* Try a series that wraps around.
15603 { 100, 65531, 65532, 65533, 65534, 65535, 0, 1 }. */
15604 for (unsigned int i = 1; i < 8; ++i)
15605 elements[i] = build_int_cst (element_type, (65530 + i) & 0xffff);
15606 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15607 check_vector_cst_stepped (elements, vector, 1);
15608
15609 /* Try a downward series:
15610 { 100, 79, 78, 77, 76, 75, 75, 73 }. */
15611 for (unsigned int i = 1; i < 8; ++i)
15612 elements[i] = build_int_cst (element_type, 80 - i);
15613 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15614 check_vector_cst_stepped (elements, vector, 1);
15615
15616 /* Try two interleaved series with different bases and steps:
15617 { 100, 53, 66, 206, 62, 212, 58, 218 }. */
15618 elements[1] = build_int_cst (element_type, 53);
15619 for (unsigned int i = 2; i < 8; i += 2)
15620 {
15621 elements[i] = build_int_cst (element_type, 70 - i * 2);
15622 elements[i + 1] = build_int_cst (element_type, 200 + i * 3);
15623 }
15624 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15625 check_vector_cst_stepped (elements, vector, 2);
15626
15627 /* Try a duplicated value:
15628 { 100, 100, 100, 100, 100, 100, 100, 100 }. */
15629 for (unsigned int i = 1; i < 8; ++i)
15630 elements[i] = elements[0];
15631 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15632 check_vector_cst_duplicate (elements, vector, 1);
15633
15634 /* Try an interleaved duplicated value:
15635 { 100, 55, 100, 55, 100, 55, 100, 55 }. */
15636 elements[1] = build_int_cst (element_type, 55);
15637 for (unsigned int i = 2; i < 8; ++i)
15638 elements[i] = elements[i - 2];
15639 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15640 check_vector_cst_duplicate (elements, vector, 2);
15641
15642 /* Try a duplicated value with 2 exceptions
15643 { 41, 97, 100, 55, 100, 55, 100, 55 }. */
15644 elements[0] = build_int_cst (element_type, 41);
15645 elements[1] = build_int_cst (element_type, 97);
15646 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15647 check_vector_cst_fill (elements, vector, 2);
15648
15649 /* Try with and without a step
15650 { 41, 97, 100, 21, 100, 35, 100, 49 }. */
15651 for (unsigned int i = 3; i < 8; i += 2)
15652 elements[i] = build_int_cst (element_type, i * 7);
15653 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15654 check_vector_cst_stepped (elements, vector, 2);
15655
15656 /* Try a fully-general constant:
15657 { 41, 97, 100, 21, 100, 9990, 100, 49 }. */
15658 elements[5] = build_int_cst (element_type, 9990);
15659 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15660 check_vector_cst_fill (elements, vector, 4);
15661 }
15662
15663 /* Verify that STRIP_NOPS (NODE) is EXPECTED.
15664 Helper function for test_location_wrappers, to deal with STRIP_NOPS
15665 modifying its argument in-place. */
15666
15667 static void
15668 check_strip_nops (tree node, tree expected)
15669 {
15670 STRIP_NOPS (node);
15671 ASSERT_EQ (expected, node);
15672 }
15673
15674 /* Verify location wrappers. */
15675
15676 static void
15677 test_location_wrappers ()
15678 {
15679 location_t loc = BUILTINS_LOCATION;
15680
15681 ASSERT_EQ (NULL_TREE, maybe_wrap_with_location (NULL_TREE, loc));
15682
15683 /* Wrapping a constant. */
15684 tree int_cst = build_int_cst (integer_type_node, 42);
15685 ASSERT_FALSE (CAN_HAVE_LOCATION_P (int_cst));
15686 ASSERT_FALSE (location_wrapper_p (int_cst));
15687
15688 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
15689 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
15690 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_int_cst));
15691 ASSERT_EQ (int_cst, tree_strip_any_location_wrapper (wrapped_int_cst));
15692
15693 /* We shouldn't add wrapper nodes for UNKNOWN_LOCATION. */
15694 ASSERT_EQ (int_cst, maybe_wrap_with_location (int_cst, UNKNOWN_LOCATION));
15695
15696 /* We shouldn't add wrapper nodes for nodes that CAN_HAVE_LOCATION_P. */
15697 tree cast = build1 (NOP_EXPR, char_type_node, int_cst);
15698 ASSERT_TRUE (CAN_HAVE_LOCATION_P (cast));
15699 ASSERT_EQ (cast, maybe_wrap_with_location (cast, loc));
15700
15701 /* Wrapping a STRING_CST. */
15702 tree string_cst = build_string (4, "foo");
15703 ASSERT_FALSE (CAN_HAVE_LOCATION_P (string_cst));
15704 ASSERT_FALSE (location_wrapper_p (string_cst));
15705
15706 tree wrapped_string_cst = maybe_wrap_with_location (string_cst, loc);
15707 ASSERT_TRUE (location_wrapper_p (wrapped_string_cst));
15708 ASSERT_EQ (VIEW_CONVERT_EXPR, TREE_CODE (wrapped_string_cst));
15709 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_string_cst));
15710 ASSERT_EQ (string_cst, tree_strip_any_location_wrapper (wrapped_string_cst));
15711
15712
15713 /* Wrapping a variable. */
15714 tree int_var = build_decl (UNKNOWN_LOCATION, VAR_DECL,
15715 get_identifier ("some_int_var"),
15716 integer_type_node);
15717 ASSERT_FALSE (CAN_HAVE_LOCATION_P (int_var));
15718 ASSERT_FALSE (location_wrapper_p (int_var));
15719
15720 tree wrapped_int_var = maybe_wrap_with_location (int_var, loc);
15721 ASSERT_TRUE (location_wrapper_p (wrapped_int_var));
15722 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_int_var));
15723 ASSERT_EQ (int_var, tree_strip_any_location_wrapper (wrapped_int_var));
15724
15725 /* Verify that "reinterpret_cast<int>(some_int_var)" is not a location
15726 wrapper. */
15727 tree r_cast = build1 (NON_LVALUE_EXPR, integer_type_node, int_var);
15728 ASSERT_FALSE (location_wrapper_p (r_cast));
15729 ASSERT_EQ (r_cast, tree_strip_any_location_wrapper (r_cast));
15730
15731 /* Verify that STRIP_NOPS removes wrappers. */
15732 check_strip_nops (wrapped_int_cst, int_cst);
15733 check_strip_nops (wrapped_string_cst, string_cst);
15734 check_strip_nops (wrapped_int_var, int_var);
15735 }
15736
15737 /* Test various tree predicates. Verify that location wrappers don't
15738 affect the results. */
15739
15740 static void
15741 test_predicates ()
15742 {
15743 /* Build various constants and wrappers around them. */
15744
15745 location_t loc = BUILTINS_LOCATION;
15746
15747 tree i_0 = build_int_cst (integer_type_node, 0);
15748 tree wr_i_0 = maybe_wrap_with_location (i_0, loc);
15749
15750 tree i_1 = build_int_cst (integer_type_node, 1);
15751 tree wr_i_1 = maybe_wrap_with_location (i_1, loc);
15752
15753 tree i_m1 = build_int_cst (integer_type_node, -1);
15754 tree wr_i_m1 = maybe_wrap_with_location (i_m1, loc);
15755
15756 tree f_0 = build_real_from_int_cst (float_type_node, i_0);
15757 tree wr_f_0 = maybe_wrap_with_location (f_0, loc);
15758 tree f_1 = build_real_from_int_cst (float_type_node, i_1);
15759 tree wr_f_1 = maybe_wrap_with_location (f_1, loc);
15760 tree f_m1 = build_real_from_int_cst (float_type_node, i_m1);
15761 tree wr_f_m1 = maybe_wrap_with_location (f_m1, loc);
15762
15763 tree c_i_0 = build_complex (NULL_TREE, i_0, i_0);
15764 tree c_i_1 = build_complex (NULL_TREE, i_1, i_0);
15765 tree c_i_m1 = build_complex (NULL_TREE, i_m1, i_0);
15766
15767 tree c_f_0 = build_complex (NULL_TREE, f_0, f_0);
15768 tree c_f_1 = build_complex (NULL_TREE, f_1, f_0);
15769 tree c_f_m1 = build_complex (NULL_TREE, f_m1, f_0);
15770
15771 /* TODO: vector constants. */
15772
15773 /* Test integer_onep. */
15774 ASSERT_FALSE (integer_onep (i_0));
15775 ASSERT_FALSE (integer_onep (wr_i_0));
15776 ASSERT_TRUE (integer_onep (i_1));
15777 ASSERT_TRUE (integer_onep (wr_i_1));
15778 ASSERT_FALSE (integer_onep (i_m1));
15779 ASSERT_FALSE (integer_onep (wr_i_m1));
15780 ASSERT_FALSE (integer_onep (f_0));
15781 ASSERT_FALSE (integer_onep (wr_f_0));
15782 ASSERT_FALSE (integer_onep (f_1));
15783 ASSERT_FALSE (integer_onep (wr_f_1));
15784 ASSERT_FALSE (integer_onep (f_m1));
15785 ASSERT_FALSE (integer_onep (wr_f_m1));
15786 ASSERT_FALSE (integer_onep (c_i_0));
15787 ASSERT_TRUE (integer_onep (c_i_1));
15788 ASSERT_FALSE (integer_onep (c_i_m1));
15789 ASSERT_FALSE (integer_onep (c_f_0));
15790 ASSERT_FALSE (integer_onep (c_f_1));
15791 ASSERT_FALSE (integer_onep (c_f_m1));
15792
15793 /* Test integer_zerop. */
15794 ASSERT_TRUE (integer_zerop (i_0));
15795 ASSERT_TRUE (integer_zerop (wr_i_0));
15796 ASSERT_FALSE (integer_zerop (i_1));
15797 ASSERT_FALSE (integer_zerop (wr_i_1));
15798 ASSERT_FALSE (integer_zerop (i_m1));
15799 ASSERT_FALSE (integer_zerop (wr_i_m1));
15800 ASSERT_FALSE (integer_zerop (f_0));
15801 ASSERT_FALSE (integer_zerop (wr_f_0));
15802 ASSERT_FALSE (integer_zerop (f_1));
15803 ASSERT_FALSE (integer_zerop (wr_f_1));
15804 ASSERT_FALSE (integer_zerop (f_m1));
15805 ASSERT_FALSE (integer_zerop (wr_f_m1));
15806 ASSERT_TRUE (integer_zerop (c_i_0));
15807 ASSERT_FALSE (integer_zerop (c_i_1));
15808 ASSERT_FALSE (integer_zerop (c_i_m1));
15809 ASSERT_FALSE (integer_zerop (c_f_0));
15810 ASSERT_FALSE (integer_zerop (c_f_1));
15811 ASSERT_FALSE (integer_zerop (c_f_m1));
15812
15813 /* Test integer_all_onesp. */
15814 ASSERT_FALSE (integer_all_onesp (i_0));
15815 ASSERT_FALSE (integer_all_onesp (wr_i_0));
15816 ASSERT_FALSE (integer_all_onesp (i_1));
15817 ASSERT_FALSE (integer_all_onesp (wr_i_1));
15818 ASSERT_TRUE (integer_all_onesp (i_m1));
15819 ASSERT_TRUE (integer_all_onesp (wr_i_m1));
15820 ASSERT_FALSE (integer_all_onesp (f_0));
15821 ASSERT_FALSE (integer_all_onesp (wr_f_0));
15822 ASSERT_FALSE (integer_all_onesp (f_1));
15823 ASSERT_FALSE (integer_all_onesp (wr_f_1));
15824 ASSERT_FALSE (integer_all_onesp (f_m1));
15825 ASSERT_FALSE (integer_all_onesp (wr_f_m1));
15826 ASSERT_FALSE (integer_all_onesp (c_i_0));
15827 ASSERT_FALSE (integer_all_onesp (c_i_1));
15828 ASSERT_FALSE (integer_all_onesp (c_i_m1));
15829 ASSERT_FALSE (integer_all_onesp (c_f_0));
15830 ASSERT_FALSE (integer_all_onesp (c_f_1));
15831 ASSERT_FALSE (integer_all_onesp (c_f_m1));
15832
15833 /* Test integer_minus_onep. */
15834 ASSERT_FALSE (integer_minus_onep (i_0));
15835 ASSERT_FALSE (integer_minus_onep (wr_i_0));
15836 ASSERT_FALSE (integer_minus_onep (i_1));
15837 ASSERT_FALSE (integer_minus_onep (wr_i_1));
15838 ASSERT_TRUE (integer_minus_onep (i_m1));
15839 ASSERT_TRUE (integer_minus_onep (wr_i_m1));
15840 ASSERT_FALSE (integer_minus_onep (f_0));
15841 ASSERT_FALSE (integer_minus_onep (wr_f_0));
15842 ASSERT_FALSE (integer_minus_onep (f_1));
15843 ASSERT_FALSE (integer_minus_onep (wr_f_1));
15844 ASSERT_FALSE (integer_minus_onep (f_m1));
15845 ASSERT_FALSE (integer_minus_onep (wr_f_m1));
15846 ASSERT_FALSE (integer_minus_onep (c_i_0));
15847 ASSERT_FALSE (integer_minus_onep (c_i_1));
15848 ASSERT_TRUE (integer_minus_onep (c_i_m1));
15849 ASSERT_FALSE (integer_minus_onep (c_f_0));
15850 ASSERT_FALSE (integer_minus_onep (c_f_1));
15851 ASSERT_FALSE (integer_minus_onep (c_f_m1));
15852
15853 /* Test integer_each_onep. */
15854 ASSERT_FALSE (integer_each_onep (i_0));
15855 ASSERT_FALSE (integer_each_onep (wr_i_0));
15856 ASSERT_TRUE (integer_each_onep (i_1));
15857 ASSERT_TRUE (integer_each_onep (wr_i_1));
15858 ASSERT_FALSE (integer_each_onep (i_m1));
15859 ASSERT_FALSE (integer_each_onep (wr_i_m1));
15860 ASSERT_FALSE (integer_each_onep (f_0));
15861 ASSERT_FALSE (integer_each_onep (wr_f_0));
15862 ASSERT_FALSE (integer_each_onep (f_1));
15863 ASSERT_FALSE (integer_each_onep (wr_f_1));
15864 ASSERT_FALSE (integer_each_onep (f_m1));
15865 ASSERT_FALSE (integer_each_onep (wr_f_m1));
15866 ASSERT_FALSE (integer_each_onep (c_i_0));
15867 ASSERT_FALSE (integer_each_onep (c_i_1));
15868 ASSERT_FALSE (integer_each_onep (c_i_m1));
15869 ASSERT_FALSE (integer_each_onep (c_f_0));
15870 ASSERT_FALSE (integer_each_onep (c_f_1));
15871 ASSERT_FALSE (integer_each_onep (c_f_m1));
15872
15873 /* Test integer_truep. */
15874 ASSERT_FALSE (integer_truep (i_0));
15875 ASSERT_FALSE (integer_truep (wr_i_0));
15876 ASSERT_TRUE (integer_truep (i_1));
15877 ASSERT_TRUE (integer_truep (wr_i_1));
15878 ASSERT_FALSE (integer_truep (i_m1));
15879 ASSERT_FALSE (integer_truep (wr_i_m1));
15880 ASSERT_FALSE (integer_truep (f_0));
15881 ASSERT_FALSE (integer_truep (wr_f_0));
15882 ASSERT_FALSE (integer_truep (f_1));
15883 ASSERT_FALSE (integer_truep (wr_f_1));
15884 ASSERT_FALSE (integer_truep (f_m1));
15885 ASSERT_FALSE (integer_truep (wr_f_m1));
15886 ASSERT_FALSE (integer_truep (c_i_0));
15887 ASSERT_TRUE (integer_truep (c_i_1));
15888 ASSERT_FALSE (integer_truep (c_i_m1));
15889 ASSERT_FALSE (integer_truep (c_f_0));
15890 ASSERT_FALSE (integer_truep (c_f_1));
15891 ASSERT_FALSE (integer_truep (c_f_m1));
15892
15893 /* Test integer_nonzerop. */
15894 ASSERT_FALSE (integer_nonzerop (i_0));
15895 ASSERT_FALSE (integer_nonzerop (wr_i_0));
15896 ASSERT_TRUE (integer_nonzerop (i_1));
15897 ASSERT_TRUE (integer_nonzerop (wr_i_1));
15898 ASSERT_TRUE (integer_nonzerop (i_m1));
15899 ASSERT_TRUE (integer_nonzerop (wr_i_m1));
15900 ASSERT_FALSE (integer_nonzerop (f_0));
15901 ASSERT_FALSE (integer_nonzerop (wr_f_0));
15902 ASSERT_FALSE (integer_nonzerop (f_1));
15903 ASSERT_FALSE (integer_nonzerop (wr_f_1));
15904 ASSERT_FALSE (integer_nonzerop (f_m1));
15905 ASSERT_FALSE (integer_nonzerop (wr_f_m1));
15906 ASSERT_FALSE (integer_nonzerop (c_i_0));
15907 ASSERT_TRUE (integer_nonzerop (c_i_1));
15908 ASSERT_TRUE (integer_nonzerop (c_i_m1));
15909 ASSERT_FALSE (integer_nonzerop (c_f_0));
15910 ASSERT_FALSE (integer_nonzerop (c_f_1));
15911 ASSERT_FALSE (integer_nonzerop (c_f_m1));
15912
15913 /* Test real_zerop. */
15914 ASSERT_FALSE (real_zerop (i_0));
15915 ASSERT_FALSE (real_zerop (wr_i_0));
15916 ASSERT_FALSE (real_zerop (i_1));
15917 ASSERT_FALSE (real_zerop (wr_i_1));
15918 ASSERT_FALSE (real_zerop (i_m1));
15919 ASSERT_FALSE (real_zerop (wr_i_m1));
15920 ASSERT_TRUE (real_zerop (f_0));
15921 ASSERT_TRUE (real_zerop (wr_f_0));
15922 ASSERT_FALSE (real_zerop (f_1));
15923 ASSERT_FALSE (real_zerop (wr_f_1));
15924 ASSERT_FALSE (real_zerop (f_m1));
15925 ASSERT_FALSE (real_zerop (wr_f_m1));
15926 ASSERT_FALSE (real_zerop (c_i_0));
15927 ASSERT_FALSE (real_zerop (c_i_1));
15928 ASSERT_FALSE (real_zerop (c_i_m1));
15929 ASSERT_TRUE (real_zerop (c_f_0));
15930 ASSERT_FALSE (real_zerop (c_f_1));
15931 ASSERT_FALSE (real_zerop (c_f_m1));
15932
15933 /* Test real_onep. */
15934 ASSERT_FALSE (real_onep (i_0));
15935 ASSERT_FALSE (real_onep (wr_i_0));
15936 ASSERT_FALSE (real_onep (i_1));
15937 ASSERT_FALSE (real_onep (wr_i_1));
15938 ASSERT_FALSE (real_onep (i_m1));
15939 ASSERT_FALSE (real_onep (wr_i_m1));
15940 ASSERT_FALSE (real_onep (f_0));
15941 ASSERT_FALSE (real_onep (wr_f_0));
15942 ASSERT_TRUE (real_onep (f_1));
15943 ASSERT_TRUE (real_onep (wr_f_1));
15944 ASSERT_FALSE (real_onep (f_m1));
15945 ASSERT_FALSE (real_onep (wr_f_m1));
15946 ASSERT_FALSE (real_onep (c_i_0));
15947 ASSERT_FALSE (real_onep (c_i_1));
15948 ASSERT_FALSE (real_onep (c_i_m1));
15949 ASSERT_FALSE (real_onep (c_f_0));
15950 ASSERT_TRUE (real_onep (c_f_1));
15951 ASSERT_FALSE (real_onep (c_f_m1));
15952
15953 /* Test real_minus_onep. */
15954 ASSERT_FALSE (real_minus_onep (i_0));
15955 ASSERT_FALSE (real_minus_onep (wr_i_0));
15956 ASSERT_FALSE (real_minus_onep (i_1));
15957 ASSERT_FALSE (real_minus_onep (wr_i_1));
15958 ASSERT_FALSE (real_minus_onep (i_m1));
15959 ASSERT_FALSE (real_minus_onep (wr_i_m1));
15960 ASSERT_FALSE (real_minus_onep (f_0));
15961 ASSERT_FALSE (real_minus_onep (wr_f_0));
15962 ASSERT_FALSE (real_minus_onep (f_1));
15963 ASSERT_FALSE (real_minus_onep (wr_f_1));
15964 ASSERT_TRUE (real_minus_onep (f_m1));
15965 ASSERT_TRUE (real_minus_onep (wr_f_m1));
15966 ASSERT_FALSE (real_minus_onep (c_i_0));
15967 ASSERT_FALSE (real_minus_onep (c_i_1));
15968 ASSERT_FALSE (real_minus_onep (c_i_m1));
15969 ASSERT_FALSE (real_minus_onep (c_f_0));
15970 ASSERT_FALSE (real_minus_onep (c_f_1));
15971 ASSERT_TRUE (real_minus_onep (c_f_m1));
15972
15973 /* Test zerop. */
15974 ASSERT_TRUE (zerop (i_0));
15975 ASSERT_TRUE (zerop (wr_i_0));
15976 ASSERT_FALSE (zerop (i_1));
15977 ASSERT_FALSE (zerop (wr_i_1));
15978 ASSERT_FALSE (zerop (i_m1));
15979 ASSERT_FALSE (zerop (wr_i_m1));
15980 ASSERT_TRUE (zerop (f_0));
15981 ASSERT_TRUE (zerop (wr_f_0));
15982 ASSERT_FALSE (zerop (f_1));
15983 ASSERT_FALSE (zerop (wr_f_1));
15984 ASSERT_FALSE (zerop (f_m1));
15985 ASSERT_FALSE (zerop (wr_f_m1));
15986 ASSERT_TRUE (zerop (c_i_0));
15987 ASSERT_FALSE (zerop (c_i_1));
15988 ASSERT_FALSE (zerop (c_i_m1));
15989 ASSERT_TRUE (zerop (c_f_0));
15990 ASSERT_FALSE (zerop (c_f_1));
15991 ASSERT_FALSE (zerop (c_f_m1));
15992
15993 /* Test tree_expr_nonnegative_p. */
15994 ASSERT_TRUE (tree_expr_nonnegative_p (i_0));
15995 ASSERT_TRUE (tree_expr_nonnegative_p (wr_i_0));
15996 ASSERT_TRUE (tree_expr_nonnegative_p (i_1));
15997 ASSERT_TRUE (tree_expr_nonnegative_p (wr_i_1));
15998 ASSERT_FALSE (tree_expr_nonnegative_p (i_m1));
15999 ASSERT_FALSE (tree_expr_nonnegative_p (wr_i_m1));
16000 ASSERT_TRUE (tree_expr_nonnegative_p (f_0));
16001 ASSERT_TRUE (tree_expr_nonnegative_p (wr_f_0));
16002 ASSERT_TRUE (tree_expr_nonnegative_p (f_1));
16003 ASSERT_TRUE (tree_expr_nonnegative_p (wr_f_1));
16004 ASSERT_FALSE (tree_expr_nonnegative_p (f_m1));
16005 ASSERT_FALSE (tree_expr_nonnegative_p (wr_f_m1));
16006 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_0));
16007 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_1));
16008 ASSERT_FALSE (tree_expr_nonnegative_p (c_i_m1));
16009 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_0));
16010 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_1));
16011 ASSERT_FALSE (tree_expr_nonnegative_p (c_f_m1));
16012
16013 /* Test tree_expr_nonzero_p. */
16014 ASSERT_FALSE (tree_expr_nonzero_p (i_0));
16015 ASSERT_FALSE (tree_expr_nonzero_p (wr_i_0));
16016 ASSERT_TRUE (tree_expr_nonzero_p (i_1));
16017 ASSERT_TRUE (tree_expr_nonzero_p (wr_i_1));
16018 ASSERT_TRUE (tree_expr_nonzero_p (i_m1));
16019 ASSERT_TRUE (tree_expr_nonzero_p (wr_i_m1));
16020
16021 /* Test integer_valued_real_p. */
16022 ASSERT_FALSE (integer_valued_real_p (i_0));
16023 ASSERT_TRUE (integer_valued_real_p (f_0));
16024 ASSERT_TRUE (integer_valued_real_p (wr_f_0));
16025 ASSERT_TRUE (integer_valued_real_p (f_1));
16026 ASSERT_TRUE (integer_valued_real_p (wr_f_1));
16027
16028 /* Test integer_pow2p. */
16029 ASSERT_FALSE (integer_pow2p (i_0));
16030 ASSERT_TRUE (integer_pow2p (i_1));
16031 ASSERT_TRUE (integer_pow2p (wr_i_1));
16032
16033 /* Test uniform_integer_cst_p. */
16034 ASSERT_TRUE (uniform_integer_cst_p (i_0));
16035 ASSERT_TRUE (uniform_integer_cst_p (wr_i_0));
16036 ASSERT_TRUE (uniform_integer_cst_p (i_1));
16037 ASSERT_TRUE (uniform_integer_cst_p (wr_i_1));
16038 ASSERT_TRUE (uniform_integer_cst_p (i_m1));
16039 ASSERT_TRUE (uniform_integer_cst_p (wr_i_m1));
16040 ASSERT_FALSE (uniform_integer_cst_p (f_0));
16041 ASSERT_FALSE (uniform_integer_cst_p (wr_f_0));
16042 ASSERT_FALSE (uniform_integer_cst_p (f_1));
16043 ASSERT_FALSE (uniform_integer_cst_p (wr_f_1));
16044 ASSERT_FALSE (uniform_integer_cst_p (f_m1));
16045 ASSERT_FALSE (uniform_integer_cst_p (wr_f_m1));
16046 ASSERT_FALSE (uniform_integer_cst_p (c_i_0));
16047 ASSERT_FALSE (uniform_integer_cst_p (c_i_1));
16048 ASSERT_FALSE (uniform_integer_cst_p (c_i_m1));
16049 ASSERT_FALSE (uniform_integer_cst_p (c_f_0));
16050 ASSERT_FALSE (uniform_integer_cst_p (c_f_1));
16051 ASSERT_FALSE (uniform_integer_cst_p (c_f_m1));
16052 }
16053
16054 /* Check that string escaping works correctly. */
16055
16056 static void
16057 test_escaped_strings (void)
16058 {
16059 int saved_cutoff;
16060 escaped_string msg;
16061
16062 msg.escape (NULL);
16063 /* ASSERT_STREQ does not accept NULL as a valid test
16064 result, so we have to use ASSERT_EQ instead. */
16065 ASSERT_EQ (NULL, (const char *) msg);
16066
16067 msg.escape ("");
16068 ASSERT_STREQ ("", (const char *) msg);
16069
16070 msg.escape ("foobar");
16071 ASSERT_STREQ ("foobar", (const char *) msg);
16072
16073 /* Ensure that we have -fmessage-length set to 0. */
16074 pretty_printer *pp = global_dc->get_reference_printer ();
16075 saved_cutoff = pp_line_cutoff (pp);
16076 pp_line_cutoff (pp) = 0;
16077
16078 msg.escape ("foo\nbar");
16079 ASSERT_STREQ ("foo\\nbar", (const char *) msg);
16080
16081 msg.escape ("\a\b\f\n\r\t\v");
16082 ASSERT_STREQ ("\\a\\b\\f\\n\\r\\t\\v", (const char *) msg);
16083
16084 /* Now repeat the tests with -fmessage-length set to 5. */
16085 pp_line_cutoff (pp) = 5;
16086
16087 /* Note that the newline is not translated into an escape. */
16088 msg.escape ("foo\nbar");
16089 ASSERT_STREQ ("foo\nbar", (const char *) msg);
16090
16091 msg.escape ("\a\b\f\n\r\t\v");
16092 ASSERT_STREQ ("\\a\\b\\f\n\\r\\t\\v", (const char *) msg);
16093
16094 /* Restore the original message length setting. */
16095 pp_line_cutoff (pp) = saved_cutoff;
16096 }
16097
16098 /* Run all of the selftests within this file. */
16099
16100 void
16101 tree_cc_tests ()
16102 {
16103 test_integer_constants ();
16104 test_identifiers ();
16105 test_labels ();
16106 test_vector_cst_patterns ();
16107 test_location_wrappers ();
16108 test_predicates ();
16109 test_escaped_strings ();
16110 }
16111
16112 } // namespace selftest
16113
16114 #endif /* CHECKING_P */
16115
16116 #include "gt-tree.h"