Update changelog entry.
[official-gcc.git] / gcc / tree.c
blob39a924644144498726c5cf0f07da8cacb6d6e18c
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
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.
27 It is intended to be language-independent but can occasionally
28 calls language-dependent routines. */
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 "params.h"
58 #include "langhooks-def.h"
59 #include "tree-diagnostic.h"
60 #include "except.h"
61 #include "builtins.h"
62 #include "print-tree.h"
63 #include "ipa-utils.h"
64 #include "selftest.h"
65 #include "stringpool.h"
66 #include "attribs.h"
67 #include "rtl.h"
68 #include "regs.h"
69 #include "tree-vector-builder.h"
71 /* Tree code classes. */
73 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
74 #define END_OF_BASE_TREE_CODES tcc_exceptional,
76 const enum tree_code_class tree_code_type[] = {
77 #include "all-tree.def"
80 #undef DEFTREECODE
81 #undef END_OF_BASE_TREE_CODES
83 /* Table indexed by tree code giving number of expression
84 operands beyond the fixed part of the node structure.
85 Not used for types or decls. */
87 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
88 #define END_OF_BASE_TREE_CODES 0,
90 const unsigned char tree_code_length[] = {
91 #include "all-tree.def"
94 #undef DEFTREECODE
95 #undef END_OF_BASE_TREE_CODES
97 /* Names of tree components.
98 Used for printing out the tree and error messages. */
99 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
100 #define END_OF_BASE_TREE_CODES "@dummy",
102 static const char *const tree_code_name[] = {
103 #include "all-tree.def"
106 #undef DEFTREECODE
107 #undef END_OF_BASE_TREE_CODES
109 /* Each tree code class has an associated string representation.
110 These must correspond to the tree_code_class entries. */
112 const char *const tree_code_class_strings[] =
114 "exceptional",
115 "constant",
116 "type",
117 "declaration",
118 "reference",
119 "comparison",
120 "unary",
121 "binary",
122 "statement",
123 "vl_exp",
124 "expression"
127 /* obstack.[ch] explicitly declined to prototype this. */
128 extern int _obstack_allocated_p (struct obstack *h, void *obj);
130 /* Statistics-gathering stuff. */
132 static uint64_t tree_code_counts[MAX_TREE_CODES];
133 uint64_t tree_node_counts[(int) all_kinds];
134 uint64_t tree_node_sizes[(int) all_kinds];
136 /* Keep in sync with tree.h:enum tree_node_kind. */
137 static const char * const tree_node_kind_names[] = {
138 "decls",
139 "types",
140 "blocks",
141 "stmts",
142 "refs",
143 "exprs",
144 "constants",
145 "identifiers",
146 "vecs",
147 "binfos",
148 "ssa names",
149 "constructors",
150 "random kinds",
151 "lang_decl kinds",
152 "lang_type kinds",
153 "omp clauses",
156 /* Unique id for next decl created. */
157 static GTY(()) int next_decl_uid;
158 /* Unique id for next type created. */
159 static GTY(()) unsigned next_type_uid = 1;
160 /* Unique id for next debug decl created. Use negative numbers,
161 to catch erroneous uses. */
162 static GTY(()) int next_debug_decl_uid;
164 /* Since we cannot rehash a type after it is in the table, we have to
165 keep the hash code. */
167 struct GTY((for_user)) type_hash {
168 unsigned long hash;
169 tree type;
172 /* Initial size of the hash table (rounded to next prime). */
173 #define TYPE_HASH_INITIAL_SIZE 1000
175 struct type_cache_hasher : ggc_cache_ptr_hash<type_hash>
177 static hashval_t hash (type_hash *t) { return t->hash; }
178 static bool equal (type_hash *a, type_hash *b);
180 static int
181 keep_cache_entry (type_hash *&t)
183 return ggc_marked_p (t->type);
187 /* Now here is the hash table. When recording a type, it is added to
188 the slot whose index is the hash code. Note that the hash table is
189 used for several kinds of types (function types, array types and
190 array index range types, for now). While all these live in the
191 same table, they are completely independent, and the hash code is
192 computed differently for each of these. */
194 static GTY ((cache)) hash_table<type_cache_hasher> *type_hash_table;
196 /* Hash table and temporary node for larger integer const values. */
197 static GTY (()) tree int_cst_node;
199 struct int_cst_hasher : ggc_cache_ptr_hash<tree_node>
201 static hashval_t hash (tree t);
202 static bool equal (tree x, tree y);
205 static GTY ((cache)) hash_table<int_cst_hasher> *int_cst_hash_table;
207 /* Class and variable for making sure that there is a single POLY_INT_CST
208 for a given value. */
209 struct poly_int_cst_hasher : ggc_cache_ptr_hash<tree_node>
211 typedef std::pair<tree, const poly_wide_int *> compare_type;
212 static hashval_t hash (tree t);
213 static bool equal (tree x, const compare_type &y);
216 static GTY ((cache)) hash_table<poly_int_cst_hasher> *poly_int_cst_hash_table;
218 /* Hash table for optimization flags and target option flags. Use the same
219 hash table for both sets of options. Nodes for building the current
220 optimization and target option nodes. The assumption is most of the time
221 the options created will already be in the hash table, so we avoid
222 allocating and freeing up a node repeatably. */
223 static GTY (()) tree cl_optimization_node;
224 static GTY (()) tree cl_target_option_node;
226 struct cl_option_hasher : ggc_cache_ptr_hash<tree_node>
228 static hashval_t hash (tree t);
229 static bool equal (tree x, tree y);
232 static GTY ((cache)) hash_table<cl_option_hasher> *cl_option_hash_table;
234 /* General tree->tree mapping structure for use in hash tables. */
237 static GTY ((cache))
238 hash_table<tree_decl_map_cache_hasher> *debug_expr_for_decl;
240 static GTY ((cache))
241 hash_table<tree_decl_map_cache_hasher> *value_expr_for_decl;
243 struct tree_vec_map_cache_hasher : ggc_cache_ptr_hash<tree_vec_map>
245 static hashval_t hash (tree_vec_map *m) { return DECL_UID (m->base.from); }
247 static bool
248 equal (tree_vec_map *a, tree_vec_map *b)
250 return a->base.from == b->base.from;
253 static int
254 keep_cache_entry (tree_vec_map *&m)
256 return ggc_marked_p (m->base.from);
260 static GTY ((cache))
261 hash_table<tree_vec_map_cache_hasher> *debug_args_for_decl;
263 static void set_type_quals (tree, int);
264 static void print_type_hash_statistics (void);
265 static void print_debug_expr_statistics (void);
266 static void print_value_expr_statistics (void);
268 static tree build_array_type_1 (tree, tree, bool, bool);
270 tree global_trees[TI_MAX];
271 tree integer_types[itk_none];
273 bool int_n_enabled_p[NUM_INT_N_ENTS];
274 struct int_n_trees_t int_n_trees [NUM_INT_N_ENTS];
276 bool tree_contains_struct[MAX_TREE_CODES][64];
278 /* Number of operands for each OpenMP clause. */
279 unsigned const char omp_clause_num_ops[] =
281 0, /* OMP_CLAUSE_ERROR */
282 1, /* OMP_CLAUSE_PRIVATE */
283 1, /* OMP_CLAUSE_SHARED */
284 1, /* OMP_CLAUSE_FIRSTPRIVATE */
285 2, /* OMP_CLAUSE_LASTPRIVATE */
286 5, /* OMP_CLAUSE_REDUCTION */
287 5, /* OMP_CLAUSE_TASK_REDUCTION */
288 5, /* OMP_CLAUSE_IN_REDUCTION */
289 1, /* OMP_CLAUSE_COPYIN */
290 1, /* OMP_CLAUSE_COPYPRIVATE */
291 3, /* OMP_CLAUSE_LINEAR */
292 2, /* OMP_CLAUSE_ALIGNED */
293 1, /* OMP_CLAUSE_DEPEND */
294 1, /* OMP_CLAUSE_NONTEMPORAL */
295 1, /* OMP_CLAUSE_UNIFORM */
296 1, /* OMP_CLAUSE_TO_DECLARE */
297 1, /* OMP_CLAUSE_LINK */
298 2, /* OMP_CLAUSE_FROM */
299 2, /* OMP_CLAUSE_TO */
300 2, /* OMP_CLAUSE_MAP */
301 1, /* OMP_CLAUSE_USE_DEVICE_PTR */
302 1, /* OMP_CLAUSE_IS_DEVICE_PTR */
303 2, /* OMP_CLAUSE__CACHE_ */
304 2, /* OMP_CLAUSE_GANG */
305 1, /* OMP_CLAUSE_ASYNC */
306 1, /* OMP_CLAUSE_WAIT */
307 0, /* OMP_CLAUSE_AUTO */
308 0, /* OMP_CLAUSE_SEQ */
309 1, /* OMP_CLAUSE__LOOPTEMP_ */
310 1, /* OMP_CLAUSE__REDUCTEMP_ */
311 1, /* OMP_CLAUSE_IF */
312 1, /* OMP_CLAUSE_NUM_THREADS */
313 1, /* OMP_CLAUSE_SCHEDULE */
314 0, /* OMP_CLAUSE_NOWAIT */
315 1, /* OMP_CLAUSE_ORDERED */
316 0, /* OMP_CLAUSE_DEFAULT */
317 3, /* OMP_CLAUSE_COLLAPSE */
318 0, /* OMP_CLAUSE_UNTIED */
319 1, /* OMP_CLAUSE_FINAL */
320 0, /* OMP_CLAUSE_MERGEABLE */
321 1, /* OMP_CLAUSE_DEVICE */
322 1, /* OMP_CLAUSE_DIST_SCHEDULE */
323 0, /* OMP_CLAUSE_INBRANCH */
324 0, /* OMP_CLAUSE_NOTINBRANCH */
325 1, /* OMP_CLAUSE_NUM_TEAMS */
326 1, /* OMP_CLAUSE_THREAD_LIMIT */
327 0, /* OMP_CLAUSE_PROC_BIND */
328 1, /* OMP_CLAUSE_SAFELEN */
329 1, /* OMP_CLAUSE_SIMDLEN */
330 0, /* OMP_CLAUSE_FOR */
331 0, /* OMP_CLAUSE_PARALLEL */
332 0, /* OMP_CLAUSE_SECTIONS */
333 0, /* OMP_CLAUSE_TASKGROUP */
334 1, /* OMP_CLAUSE_PRIORITY */
335 1, /* OMP_CLAUSE_GRAINSIZE */
336 1, /* OMP_CLAUSE_NUM_TASKS */
337 0, /* OMP_CLAUSE_NOGROUP */
338 0, /* OMP_CLAUSE_THREADS */
339 0, /* OMP_CLAUSE_SIMD */
340 1, /* OMP_CLAUSE_HINT */
341 0, /* OMP_CLAUSE_DEFALTMAP */
342 1, /* OMP_CLAUSE__SIMDUID_ */
343 0, /* OMP_CLAUSE__SIMT_ */
344 0, /* OMP_CLAUSE_INDEPENDENT */
345 1, /* OMP_CLAUSE_WORKER */
346 1, /* OMP_CLAUSE_VECTOR */
347 1, /* OMP_CLAUSE_NUM_GANGS */
348 1, /* OMP_CLAUSE_NUM_WORKERS */
349 1, /* OMP_CLAUSE_VECTOR_LENGTH */
350 3, /* OMP_CLAUSE_TILE */
351 2, /* OMP_CLAUSE__GRIDDIM_ */
352 0, /* OMP_CLAUSE_IF_PRESENT */
353 0, /* OMP_CLAUSE_FINALIZE */
356 const char * const omp_clause_code_name[] =
358 "error_clause",
359 "private",
360 "shared",
361 "firstprivate",
362 "lastprivate",
363 "reduction",
364 "task_reduction",
365 "in_reduction",
366 "copyin",
367 "copyprivate",
368 "linear",
369 "aligned",
370 "depend",
371 "nontemporal",
372 "uniform",
373 "to",
374 "link",
375 "from",
376 "to",
377 "map",
378 "use_device_ptr",
379 "is_device_ptr",
380 "_cache_",
381 "gang",
382 "async",
383 "wait",
384 "auto",
385 "seq",
386 "_looptemp_",
387 "_reductemp_",
388 "if",
389 "num_threads",
390 "schedule",
391 "nowait",
392 "ordered",
393 "default",
394 "collapse",
395 "untied",
396 "final",
397 "mergeable",
398 "device",
399 "dist_schedule",
400 "inbranch",
401 "notinbranch",
402 "num_teams",
403 "thread_limit",
404 "proc_bind",
405 "safelen",
406 "simdlen",
407 "for",
408 "parallel",
409 "sections",
410 "taskgroup",
411 "priority",
412 "grainsize",
413 "num_tasks",
414 "nogroup",
415 "threads",
416 "simd",
417 "hint",
418 "defaultmap",
419 "_simduid_",
420 "_simt_",
421 "independent",
422 "worker",
423 "vector",
424 "num_gangs",
425 "num_workers",
426 "vector_length",
427 "tile",
428 "_griddim_",
429 "if_present",
430 "finalize",
434 /* Return the tree node structure used by tree code CODE. */
436 static inline enum tree_node_structure_enum
437 tree_node_structure_for_code (enum tree_code code)
439 switch (TREE_CODE_CLASS (code))
441 case tcc_declaration:
443 switch (code)
445 case FIELD_DECL:
446 return TS_FIELD_DECL;
447 case PARM_DECL:
448 return TS_PARM_DECL;
449 case VAR_DECL:
450 return TS_VAR_DECL;
451 case LABEL_DECL:
452 return TS_LABEL_DECL;
453 case RESULT_DECL:
454 return TS_RESULT_DECL;
455 case DEBUG_EXPR_DECL:
456 return TS_DECL_WRTL;
457 case CONST_DECL:
458 return TS_CONST_DECL;
459 case TYPE_DECL:
460 return TS_TYPE_DECL;
461 case FUNCTION_DECL:
462 return TS_FUNCTION_DECL;
463 case TRANSLATION_UNIT_DECL:
464 return TS_TRANSLATION_UNIT_DECL;
465 default:
466 return TS_DECL_NON_COMMON;
469 case tcc_type:
470 return TS_TYPE_NON_COMMON;
471 case tcc_reference:
472 case tcc_comparison:
473 case tcc_unary:
474 case tcc_binary:
475 case tcc_expression:
476 case tcc_statement:
477 case tcc_vl_exp:
478 return TS_EXP;
479 default: /* tcc_constant and tcc_exceptional */
480 break;
482 switch (code)
484 /* tcc_constant cases. */
485 case VOID_CST: return TS_TYPED;
486 case INTEGER_CST: return TS_INT_CST;
487 case POLY_INT_CST: return TS_POLY_INT_CST;
488 case REAL_CST: return TS_REAL_CST;
489 case FIXED_CST: return TS_FIXED_CST;
490 case COMPLEX_CST: return TS_COMPLEX;
491 case VECTOR_CST: return TS_VECTOR;
492 case STRING_CST: return TS_STRING;
493 /* tcc_exceptional cases. */
494 case ERROR_MARK: return TS_COMMON;
495 case IDENTIFIER_NODE: return TS_IDENTIFIER;
496 case TREE_LIST: return TS_LIST;
497 case TREE_VEC: return TS_VEC;
498 case SSA_NAME: return TS_SSA_NAME;
499 case PLACEHOLDER_EXPR: return TS_COMMON;
500 case STATEMENT_LIST: return TS_STATEMENT_LIST;
501 case BLOCK: return TS_BLOCK;
502 case CONSTRUCTOR: return TS_CONSTRUCTOR;
503 case TREE_BINFO: return TS_BINFO;
504 case OMP_CLAUSE: return TS_OMP_CLAUSE;
505 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
506 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
508 default:
509 gcc_unreachable ();
514 /* Initialize tree_contains_struct to describe the hierarchy of tree
515 nodes. */
517 static void
518 initialize_tree_contains_struct (void)
520 unsigned i;
522 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
524 enum tree_code code;
525 enum tree_node_structure_enum ts_code;
527 code = (enum tree_code) i;
528 ts_code = tree_node_structure_for_code (code);
530 /* Mark the TS structure itself. */
531 tree_contains_struct[code][ts_code] = 1;
533 /* Mark all the structures that TS is derived from. */
534 switch (ts_code)
536 case TS_TYPED:
537 case TS_BLOCK:
538 case TS_OPTIMIZATION:
539 case TS_TARGET_OPTION:
540 MARK_TS_BASE (code);
541 break;
543 case TS_COMMON:
544 case TS_INT_CST:
545 case TS_POLY_INT_CST:
546 case TS_REAL_CST:
547 case TS_FIXED_CST:
548 case TS_VECTOR:
549 case TS_STRING:
550 case TS_COMPLEX:
551 case TS_SSA_NAME:
552 case TS_CONSTRUCTOR:
553 case TS_EXP:
554 case TS_STATEMENT_LIST:
555 MARK_TS_TYPED (code);
556 break;
558 case TS_IDENTIFIER:
559 case TS_DECL_MINIMAL:
560 case TS_TYPE_COMMON:
561 case TS_LIST:
562 case TS_VEC:
563 case TS_BINFO:
564 case TS_OMP_CLAUSE:
565 MARK_TS_COMMON (code);
566 break;
568 case TS_TYPE_WITH_LANG_SPECIFIC:
569 MARK_TS_TYPE_COMMON (code);
570 break;
572 case TS_TYPE_NON_COMMON:
573 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
574 break;
576 case TS_DECL_COMMON:
577 MARK_TS_DECL_MINIMAL (code);
578 break;
580 case TS_DECL_WRTL:
581 case TS_CONST_DECL:
582 MARK_TS_DECL_COMMON (code);
583 break;
585 case TS_DECL_NON_COMMON:
586 MARK_TS_DECL_WITH_VIS (code);
587 break;
589 case TS_DECL_WITH_VIS:
590 case TS_PARM_DECL:
591 case TS_LABEL_DECL:
592 case TS_RESULT_DECL:
593 MARK_TS_DECL_WRTL (code);
594 break;
596 case TS_FIELD_DECL:
597 MARK_TS_DECL_COMMON (code);
598 break;
600 case TS_VAR_DECL:
601 MARK_TS_DECL_WITH_VIS (code);
602 break;
604 case TS_TYPE_DECL:
605 case TS_FUNCTION_DECL:
606 MARK_TS_DECL_NON_COMMON (code);
607 break;
609 case TS_TRANSLATION_UNIT_DECL:
610 MARK_TS_DECL_COMMON (code);
611 break;
613 default:
614 gcc_unreachable ();
618 /* Basic consistency checks for attributes used in fold. */
619 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
620 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
621 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
622 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
623 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
624 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
625 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
626 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
627 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
628 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
629 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
630 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
631 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
632 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
633 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
634 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
635 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
636 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
637 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
638 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
639 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
640 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
641 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
642 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
643 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
644 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
645 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
646 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
647 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
648 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
649 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
650 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
651 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
652 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
653 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
654 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
655 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
656 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
657 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_MINIMAL]);
658 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_COMMON]);
662 /* Init tree.c. */
664 void
665 init_ttree (void)
667 /* Initialize the hash table of types. */
668 type_hash_table
669 = hash_table<type_cache_hasher>::create_ggc (TYPE_HASH_INITIAL_SIZE);
671 debug_expr_for_decl
672 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
674 value_expr_for_decl
675 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
677 int_cst_hash_table = hash_table<int_cst_hasher>::create_ggc (1024);
679 poly_int_cst_hash_table = hash_table<poly_int_cst_hasher>::create_ggc (64);
681 int_cst_node = make_int_cst (1, 1);
683 cl_option_hash_table = hash_table<cl_option_hasher>::create_ggc (64);
685 cl_optimization_node = make_node (OPTIMIZATION_NODE);
686 cl_target_option_node = make_node (TARGET_OPTION_NODE);
688 /* Initialize the tree_contains_struct array. */
689 initialize_tree_contains_struct ();
690 lang_hooks.init_ts ();
694 /* The name of the object as the assembler will see it (but before any
695 translations made by ASM_OUTPUT_LABELREF). Often this is the same
696 as DECL_NAME. It is an IDENTIFIER_NODE. */
697 tree
698 decl_assembler_name (tree decl)
700 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
701 lang_hooks.set_decl_assembler_name (decl);
702 return DECL_ASSEMBLER_NAME_RAW (decl);
705 /* The DECL_ASSEMBLER_NAME_RAW of DECL is being explicitly set to NAME
706 (either of which may be NULL). Inform the FE, if this changes the
707 name. */
709 void
710 overwrite_decl_assembler_name (tree decl, tree name)
712 if (DECL_ASSEMBLER_NAME_RAW (decl) != name)
713 lang_hooks.overwrite_decl_assembler_name (decl, name);
716 /* When the target supports COMDAT groups, this indicates which group the
717 DECL is associated with. This can be either an IDENTIFIER_NODE or a
718 decl, in which case its DECL_ASSEMBLER_NAME identifies the group. */
719 tree
720 decl_comdat_group (const_tree node)
722 struct symtab_node *snode = symtab_node::get (node);
723 if (!snode)
724 return NULL;
725 return snode->get_comdat_group ();
728 /* Likewise, but make sure it's been reduced to an IDENTIFIER_NODE. */
729 tree
730 decl_comdat_group_id (const_tree node)
732 struct symtab_node *snode = symtab_node::get (node);
733 if (!snode)
734 return NULL;
735 return snode->get_comdat_group_id ();
738 /* When the target supports named section, return its name as IDENTIFIER_NODE
739 or NULL if it is in no section. */
740 const char *
741 decl_section_name (const_tree node)
743 struct symtab_node *snode = symtab_node::get (node);
744 if (!snode)
745 return NULL;
746 return snode->get_section ();
749 /* Set section name of NODE to VALUE (that is expected to be
750 identifier node) */
751 void
752 set_decl_section_name (tree node, const char *value)
754 struct symtab_node *snode;
756 if (value == NULL)
758 snode = symtab_node::get (node);
759 if (!snode)
760 return;
762 else if (VAR_P (node))
763 snode = varpool_node::get_create (node);
764 else
765 snode = cgraph_node::get_create (node);
766 snode->set_section (value);
769 /* Return TLS model of a variable NODE. */
770 enum tls_model
771 decl_tls_model (const_tree node)
773 struct varpool_node *snode = varpool_node::get (node);
774 if (!snode)
775 return TLS_MODEL_NONE;
776 return snode->tls_model;
779 /* Set TLS model of variable NODE to MODEL. */
780 void
781 set_decl_tls_model (tree node, enum tls_model model)
783 struct varpool_node *vnode;
785 if (model == TLS_MODEL_NONE)
787 vnode = varpool_node::get (node);
788 if (!vnode)
789 return;
791 else
792 vnode = varpool_node::get_create (node);
793 vnode->tls_model = model;
796 /* Compute the number of bytes occupied by a tree with code CODE.
797 This function cannot be used for nodes that have variable sizes,
798 including TREE_VEC, INTEGER_CST, STRING_CST, and CALL_EXPR. */
799 size_t
800 tree_code_size (enum tree_code code)
802 switch (TREE_CODE_CLASS (code))
804 case tcc_declaration: /* A decl node */
805 switch (code)
807 case FIELD_DECL: return sizeof (tree_field_decl);
808 case PARM_DECL: return sizeof (tree_parm_decl);
809 case VAR_DECL: return sizeof (tree_var_decl);
810 case LABEL_DECL: return sizeof (tree_label_decl);
811 case RESULT_DECL: return sizeof (tree_result_decl);
812 case CONST_DECL: return sizeof (tree_const_decl);
813 case TYPE_DECL: return sizeof (tree_type_decl);
814 case FUNCTION_DECL: return sizeof (tree_function_decl);
815 case DEBUG_EXPR_DECL: return sizeof (tree_decl_with_rtl);
816 case TRANSLATION_UNIT_DECL: return sizeof (tree_translation_unit_decl);
817 case NAMESPACE_DECL:
818 case IMPORTED_DECL:
819 case NAMELIST_DECL: return sizeof (tree_decl_non_common);
820 default:
821 gcc_checking_assert (code >= NUM_TREE_CODES);
822 return lang_hooks.tree_size (code);
825 case tcc_type: /* a type node */
826 switch (code)
828 case OFFSET_TYPE:
829 case ENUMERAL_TYPE:
830 case BOOLEAN_TYPE:
831 case INTEGER_TYPE:
832 case REAL_TYPE:
833 case POINTER_TYPE:
834 case REFERENCE_TYPE:
835 case NULLPTR_TYPE:
836 case FIXED_POINT_TYPE:
837 case COMPLEX_TYPE:
838 case VECTOR_TYPE:
839 case ARRAY_TYPE:
840 case RECORD_TYPE:
841 case UNION_TYPE:
842 case QUAL_UNION_TYPE:
843 case VOID_TYPE:
844 case FUNCTION_TYPE:
845 case METHOD_TYPE:
846 case LANG_TYPE: return sizeof (tree_type_non_common);
847 default:
848 gcc_checking_assert (code >= NUM_TREE_CODES);
849 return lang_hooks.tree_size (code);
852 case tcc_reference: /* a reference */
853 case tcc_expression: /* an expression */
854 case tcc_statement: /* an expression with side effects */
855 case tcc_comparison: /* a comparison expression */
856 case tcc_unary: /* a unary arithmetic expression */
857 case tcc_binary: /* a binary arithmetic expression */
858 return (sizeof (struct tree_exp)
859 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
861 case tcc_constant: /* a constant */
862 switch (code)
864 case VOID_CST: return sizeof (tree_typed);
865 case INTEGER_CST: gcc_unreachable ();
866 case POLY_INT_CST: return sizeof (tree_poly_int_cst);
867 case REAL_CST: return sizeof (tree_real_cst);
868 case FIXED_CST: return sizeof (tree_fixed_cst);
869 case COMPLEX_CST: return sizeof (tree_complex);
870 case VECTOR_CST: gcc_unreachable ();
871 case STRING_CST: gcc_unreachable ();
872 default:
873 gcc_checking_assert (code >= NUM_TREE_CODES);
874 return lang_hooks.tree_size (code);
877 case tcc_exceptional: /* something random, like an identifier. */
878 switch (code)
880 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
881 case TREE_LIST: return sizeof (tree_list);
883 case ERROR_MARK:
884 case PLACEHOLDER_EXPR: return sizeof (tree_common);
886 case TREE_VEC: gcc_unreachable ();
887 case OMP_CLAUSE: gcc_unreachable ();
889 case SSA_NAME: return sizeof (tree_ssa_name);
891 case STATEMENT_LIST: return sizeof (tree_statement_list);
892 case BLOCK: return sizeof (struct tree_block);
893 case CONSTRUCTOR: return sizeof (tree_constructor);
894 case OPTIMIZATION_NODE: return sizeof (tree_optimization_option);
895 case TARGET_OPTION_NODE: return sizeof (tree_target_option);
897 default:
898 gcc_checking_assert (code >= NUM_TREE_CODES);
899 return lang_hooks.tree_size (code);
902 default:
903 gcc_unreachable ();
907 /* Compute the number of bytes occupied by NODE. This routine only
908 looks at TREE_CODE, except for those nodes that have variable sizes. */
909 size_t
910 tree_size (const_tree node)
912 const enum tree_code code = TREE_CODE (node);
913 switch (code)
915 case INTEGER_CST:
916 return (sizeof (struct tree_int_cst)
917 + (TREE_INT_CST_EXT_NUNITS (node) - 1) * sizeof (HOST_WIDE_INT));
919 case TREE_BINFO:
920 return (offsetof (struct tree_binfo, base_binfos)
921 + vec<tree, va_gc>
922 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
924 case TREE_VEC:
925 return (sizeof (struct tree_vec)
926 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
928 case VECTOR_CST:
929 return (sizeof (struct tree_vector)
930 + (vector_cst_encoded_nelts (node) - 1) * sizeof (tree));
932 case STRING_CST:
933 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
935 case OMP_CLAUSE:
936 return (sizeof (struct tree_omp_clause)
937 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
938 * sizeof (tree));
940 default:
941 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
942 return (sizeof (struct tree_exp)
943 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
944 else
945 return tree_code_size (code);
949 /* Return tree node kind based on tree CODE. */
951 static tree_node_kind
952 get_stats_node_kind (enum tree_code code)
954 enum tree_code_class type = TREE_CODE_CLASS (code);
956 switch (type)
958 case tcc_declaration: /* A decl node */
959 return d_kind;
960 case tcc_type: /* a type node */
961 return t_kind;
962 case tcc_statement: /* an expression with side effects */
963 return s_kind;
964 case tcc_reference: /* a reference */
965 return r_kind;
966 case tcc_expression: /* an expression */
967 case tcc_comparison: /* a comparison expression */
968 case tcc_unary: /* a unary arithmetic expression */
969 case tcc_binary: /* a binary arithmetic expression */
970 return e_kind;
971 case tcc_constant: /* a constant */
972 return c_kind;
973 case tcc_exceptional: /* something random, like an identifier. */
974 switch (code)
976 case IDENTIFIER_NODE:
977 return id_kind;
978 case TREE_VEC:
979 return vec_kind;
980 case TREE_BINFO:
981 return binfo_kind;
982 case SSA_NAME:
983 return ssa_name_kind;
984 case BLOCK:
985 return b_kind;
986 case CONSTRUCTOR:
987 return constr_kind;
988 case OMP_CLAUSE:
989 return omp_clause_kind;
990 default:
991 return x_kind;
993 break;
994 case tcc_vl_exp:
995 return e_kind;
996 default:
997 gcc_unreachable ();
1001 /* Record interesting allocation statistics for a tree node with CODE
1002 and LENGTH. */
1004 static void
1005 record_node_allocation_statistics (enum tree_code code, size_t length)
1007 if (!GATHER_STATISTICS)
1008 return;
1010 tree_node_kind kind = get_stats_node_kind (code);
1012 tree_code_counts[(int) code]++;
1013 tree_node_counts[(int) kind]++;
1014 tree_node_sizes[(int) kind] += length;
1017 /* Allocate and return a new UID from the DECL_UID namespace. */
1020 allocate_decl_uid (void)
1022 return next_decl_uid++;
1025 /* Return a newly allocated node of code CODE. For decl and type
1026 nodes, some other fields are initialized. The rest of the node is
1027 initialized to zero. This function cannot be used for TREE_VEC,
1028 INTEGER_CST or OMP_CLAUSE nodes, which is enforced by asserts in
1029 tree_code_size.
1031 Achoo! I got a code in the node. */
1033 tree
1034 make_node (enum tree_code code MEM_STAT_DECL)
1036 tree t;
1037 enum tree_code_class type = TREE_CODE_CLASS (code);
1038 size_t length = tree_code_size (code);
1040 record_node_allocation_statistics (code, length);
1042 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1043 TREE_SET_CODE (t, code);
1045 switch (type)
1047 case tcc_statement:
1048 if (code != DEBUG_BEGIN_STMT)
1049 TREE_SIDE_EFFECTS (t) = 1;
1050 break;
1052 case tcc_declaration:
1053 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1055 if (code == FUNCTION_DECL)
1057 SET_DECL_ALIGN (t, FUNCTION_ALIGNMENT (FUNCTION_BOUNDARY));
1058 SET_DECL_MODE (t, FUNCTION_MODE);
1060 else
1061 SET_DECL_ALIGN (t, 1);
1063 DECL_SOURCE_LOCATION (t) = input_location;
1064 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
1065 DECL_UID (t) = --next_debug_decl_uid;
1066 else
1068 DECL_UID (t) = allocate_decl_uid ();
1069 SET_DECL_PT_UID (t, -1);
1071 if (TREE_CODE (t) == LABEL_DECL)
1072 LABEL_DECL_UID (t) = -1;
1074 break;
1076 case tcc_type:
1077 TYPE_UID (t) = next_type_uid++;
1078 SET_TYPE_ALIGN (t, BITS_PER_UNIT);
1079 TYPE_USER_ALIGN (t) = 0;
1080 TYPE_MAIN_VARIANT (t) = t;
1081 TYPE_CANONICAL (t) = t;
1083 /* Default to no attributes for type, but let target change that. */
1084 TYPE_ATTRIBUTES (t) = NULL_TREE;
1085 targetm.set_default_type_attributes (t);
1087 /* We have not yet computed the alias set for this type. */
1088 TYPE_ALIAS_SET (t) = -1;
1089 break;
1091 case tcc_constant:
1092 TREE_CONSTANT (t) = 1;
1093 break;
1095 case tcc_expression:
1096 switch (code)
1098 case INIT_EXPR:
1099 case MODIFY_EXPR:
1100 case VA_ARG_EXPR:
1101 case PREDECREMENT_EXPR:
1102 case PREINCREMENT_EXPR:
1103 case POSTDECREMENT_EXPR:
1104 case POSTINCREMENT_EXPR:
1105 /* All of these have side-effects, no matter what their
1106 operands are. */
1107 TREE_SIDE_EFFECTS (t) = 1;
1108 break;
1110 default:
1111 break;
1113 break;
1115 case tcc_exceptional:
1116 switch (code)
1118 case TARGET_OPTION_NODE:
1119 TREE_TARGET_OPTION(t)
1120 = ggc_cleared_alloc<struct cl_target_option> ();
1121 break;
1123 case OPTIMIZATION_NODE:
1124 TREE_OPTIMIZATION (t)
1125 = ggc_cleared_alloc<struct cl_optimization> ();
1126 break;
1128 default:
1129 break;
1131 break;
1133 default:
1134 /* Other classes need no special treatment. */
1135 break;
1138 return t;
1141 /* Free tree node. */
1143 void
1144 free_node (tree node)
1146 enum tree_code code = TREE_CODE (node);
1147 if (GATHER_STATISTICS)
1149 enum tree_node_kind kind = get_stats_node_kind (code);
1151 gcc_checking_assert (tree_code_counts[(int) TREE_CODE (node)] != 0);
1152 gcc_checking_assert (tree_node_counts[(int) kind] != 0);
1153 gcc_checking_assert (tree_node_sizes[(int) kind] >= tree_size (node));
1155 tree_code_counts[(int) TREE_CODE (node)]--;
1156 tree_node_counts[(int) kind]--;
1157 tree_node_sizes[(int) kind] -= tree_size (node);
1159 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1160 vec_free (CONSTRUCTOR_ELTS (node));
1161 else if (code == BLOCK)
1162 vec_free (BLOCK_NONLOCALIZED_VARS (node));
1163 else if (code == TREE_BINFO)
1164 vec_free (BINFO_BASE_ACCESSES (node));
1165 ggc_free (node);
1168 /* Return a new node with the same contents as NODE except that its
1169 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
1171 tree
1172 copy_node (tree node MEM_STAT_DECL)
1174 tree t;
1175 enum tree_code code = TREE_CODE (node);
1176 size_t length;
1178 gcc_assert (code != STATEMENT_LIST);
1180 length = tree_size (node);
1181 record_node_allocation_statistics (code, length);
1182 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1183 memcpy (t, node, length);
1185 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
1186 TREE_CHAIN (t) = 0;
1187 TREE_ASM_WRITTEN (t) = 0;
1188 TREE_VISITED (t) = 0;
1190 if (TREE_CODE_CLASS (code) == tcc_declaration)
1192 if (code == DEBUG_EXPR_DECL)
1193 DECL_UID (t) = --next_debug_decl_uid;
1194 else
1196 DECL_UID (t) = allocate_decl_uid ();
1197 if (DECL_PT_UID_SET_P (node))
1198 SET_DECL_PT_UID (t, DECL_PT_UID (node));
1200 if ((TREE_CODE (node) == PARM_DECL || VAR_P (node))
1201 && DECL_HAS_VALUE_EXPR_P (node))
1203 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
1204 DECL_HAS_VALUE_EXPR_P (t) = 1;
1206 /* DECL_DEBUG_EXPR is copied explicitely by callers. */
1207 if (VAR_P (node))
1209 DECL_HAS_DEBUG_EXPR_P (t) = 0;
1210 t->decl_with_vis.symtab_node = NULL;
1212 if (VAR_P (node) && DECL_HAS_INIT_PRIORITY_P (node))
1214 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
1215 DECL_HAS_INIT_PRIORITY_P (t) = 1;
1217 if (TREE_CODE (node) == FUNCTION_DECL)
1219 DECL_STRUCT_FUNCTION (t) = NULL;
1220 t->decl_with_vis.symtab_node = NULL;
1223 else if (TREE_CODE_CLASS (code) == tcc_type)
1225 TYPE_UID (t) = next_type_uid++;
1226 /* The following is so that the debug code for
1227 the copy is different from the original type.
1228 The two statements usually duplicate each other
1229 (because they clear fields of the same union),
1230 but the optimizer should catch that. */
1231 TYPE_SYMTAB_ADDRESS (t) = 0;
1232 TYPE_SYMTAB_DIE (t) = 0;
1234 /* Do not copy the values cache. */
1235 if (TYPE_CACHED_VALUES_P (t))
1237 TYPE_CACHED_VALUES_P (t) = 0;
1238 TYPE_CACHED_VALUES (t) = NULL_TREE;
1241 else if (code == TARGET_OPTION_NODE)
1243 TREE_TARGET_OPTION (t) = ggc_alloc<struct cl_target_option>();
1244 memcpy (TREE_TARGET_OPTION (t), TREE_TARGET_OPTION (node),
1245 sizeof (struct cl_target_option));
1247 else if (code == OPTIMIZATION_NODE)
1249 TREE_OPTIMIZATION (t) = ggc_alloc<struct cl_optimization>();
1250 memcpy (TREE_OPTIMIZATION (t), TREE_OPTIMIZATION (node),
1251 sizeof (struct cl_optimization));
1254 return t;
1257 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1258 For example, this can copy a list made of TREE_LIST nodes. */
1260 tree
1261 copy_list (tree list)
1263 tree head;
1264 tree prev, next;
1266 if (list == 0)
1267 return 0;
1269 head = prev = copy_node (list);
1270 next = TREE_CHAIN (list);
1271 while (next)
1273 TREE_CHAIN (prev) = copy_node (next);
1274 prev = TREE_CHAIN (prev);
1275 next = TREE_CHAIN (next);
1277 return head;
1281 /* Return the value that TREE_INT_CST_EXT_NUNITS should have for an
1282 INTEGER_CST with value CST and type TYPE. */
1284 static unsigned int
1285 get_int_cst_ext_nunits (tree type, const wide_int &cst)
1287 gcc_checking_assert (cst.get_precision () == TYPE_PRECISION (type));
1288 /* We need extra HWIs if CST is an unsigned integer with its
1289 upper bit set. */
1290 if (TYPE_UNSIGNED (type) && wi::neg_p (cst))
1291 return cst.get_precision () / HOST_BITS_PER_WIDE_INT + 1;
1292 return cst.get_len ();
1295 /* Return a new INTEGER_CST with value CST and type TYPE. */
1297 static tree
1298 build_new_int_cst (tree type, const wide_int &cst)
1300 unsigned int len = cst.get_len ();
1301 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1302 tree nt = make_int_cst (len, ext_len);
1304 if (len < ext_len)
1306 --ext_len;
1307 TREE_INT_CST_ELT (nt, ext_len)
1308 = zext_hwi (-1, cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1309 for (unsigned int i = len; i < ext_len; ++i)
1310 TREE_INT_CST_ELT (nt, i) = -1;
1312 else if (TYPE_UNSIGNED (type)
1313 && cst.get_precision () < len * HOST_BITS_PER_WIDE_INT)
1315 len--;
1316 TREE_INT_CST_ELT (nt, len)
1317 = zext_hwi (cst.elt (len),
1318 cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1321 for (unsigned int i = 0; i < len; i++)
1322 TREE_INT_CST_ELT (nt, i) = cst.elt (i);
1323 TREE_TYPE (nt) = type;
1324 return nt;
1327 /* Return a new POLY_INT_CST with coefficients COEFFS and type TYPE. */
1329 static tree
1330 build_new_poly_int_cst (tree type, tree (&coeffs)[NUM_POLY_INT_COEFFS]
1331 CXX_MEM_STAT_INFO)
1333 size_t length = sizeof (struct tree_poly_int_cst);
1334 record_node_allocation_statistics (POLY_INT_CST, length);
1336 tree t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1338 TREE_SET_CODE (t, POLY_INT_CST);
1339 TREE_CONSTANT (t) = 1;
1340 TREE_TYPE (t) = type;
1341 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1342 POLY_INT_CST_COEFF (t, i) = coeffs[i];
1343 return t;
1346 /* Create a constant tree that contains CST sign-extended to TYPE. */
1348 tree
1349 build_int_cst (tree type, poly_int64 cst)
1351 /* Support legacy code. */
1352 if (!type)
1353 type = integer_type_node;
1355 return wide_int_to_tree (type, wi::shwi (cst, TYPE_PRECISION (type)));
1358 /* Create a constant tree that contains CST zero-extended to TYPE. */
1360 tree
1361 build_int_cstu (tree type, poly_uint64 cst)
1363 return wide_int_to_tree (type, wi::uhwi (cst, TYPE_PRECISION (type)));
1366 /* Create a constant tree that contains CST sign-extended to TYPE. */
1368 tree
1369 build_int_cst_type (tree type, poly_int64 cst)
1371 gcc_assert (type);
1372 return wide_int_to_tree (type, wi::shwi (cst, TYPE_PRECISION (type)));
1375 /* Constructs tree in type TYPE from with value given by CST. Signedness
1376 of CST is assumed to be the same as the signedness of TYPE. */
1378 tree
1379 double_int_to_tree (tree type, double_int cst)
1381 return wide_int_to_tree (type, widest_int::from (cst, TYPE_SIGN (type)));
1384 /* We force the wide_int CST to the range of the type TYPE by sign or
1385 zero extending it. OVERFLOWABLE indicates if we are interested in
1386 overflow of the value, when >0 we are only interested in signed
1387 overflow, for <0 we are interested in any overflow. OVERFLOWED
1388 indicates whether overflow has already occurred. CONST_OVERFLOWED
1389 indicates whether constant overflow has already occurred. We force
1390 T's value to be within range of T's type (by setting to 0 or 1 all
1391 the bits outside the type's range). We set TREE_OVERFLOWED if,
1392 OVERFLOWED is nonzero,
1393 or OVERFLOWABLE is >0 and signed overflow occurs
1394 or OVERFLOWABLE is <0 and any overflow occurs
1395 We return a new tree node for the extended wide_int. The node
1396 is shared if no overflow flags are set. */
1399 tree
1400 force_fit_type (tree type, const poly_wide_int_ref &cst,
1401 int overflowable, bool overflowed)
1403 signop sign = TYPE_SIGN (type);
1405 /* If we need to set overflow flags, return a new unshared node. */
1406 if (overflowed || !wi::fits_to_tree_p (cst, type))
1408 if (overflowed
1409 || overflowable < 0
1410 || (overflowable > 0 && sign == SIGNED))
1412 poly_wide_int tmp = poly_wide_int::from (cst, TYPE_PRECISION (type),
1413 sign);
1414 tree t;
1415 if (tmp.is_constant ())
1416 t = build_new_int_cst (type, tmp.coeffs[0]);
1417 else
1419 tree coeffs[NUM_POLY_INT_COEFFS];
1420 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1422 coeffs[i] = build_new_int_cst (type, tmp.coeffs[i]);
1423 TREE_OVERFLOW (coeffs[i]) = 1;
1425 t = build_new_poly_int_cst (type, coeffs);
1427 TREE_OVERFLOW (t) = 1;
1428 return t;
1432 /* Else build a shared node. */
1433 return wide_int_to_tree (type, cst);
1436 /* These are the hash table functions for the hash table of INTEGER_CST
1437 nodes of a sizetype. */
1439 /* Return the hash code X, an INTEGER_CST. */
1441 hashval_t
1442 int_cst_hasher::hash (tree x)
1444 const_tree const t = x;
1445 hashval_t code = TYPE_UID (TREE_TYPE (t));
1446 int i;
1448 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
1449 code = iterative_hash_host_wide_int (TREE_INT_CST_ELT(t, i), code);
1451 return code;
1454 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1455 is the same as that given by *Y, which is the same. */
1457 bool
1458 int_cst_hasher::equal (tree x, tree y)
1460 const_tree const xt = x;
1461 const_tree const yt = y;
1463 if (TREE_TYPE (xt) != TREE_TYPE (yt)
1464 || TREE_INT_CST_NUNITS (xt) != TREE_INT_CST_NUNITS (yt)
1465 || TREE_INT_CST_EXT_NUNITS (xt) != TREE_INT_CST_EXT_NUNITS (yt))
1466 return false;
1468 for (int i = 0; i < TREE_INT_CST_NUNITS (xt); i++)
1469 if (TREE_INT_CST_ELT (xt, i) != TREE_INT_CST_ELT (yt, i))
1470 return false;
1472 return true;
1475 /* Create an INT_CST node of TYPE and value CST.
1476 The returned node is always shared. For small integers we use a
1477 per-type vector cache, for larger ones we use a single hash table.
1478 The value is extended from its precision according to the sign of
1479 the type to be a multiple of HOST_BITS_PER_WIDE_INT. This defines
1480 the upper bits and ensures that hashing and value equality based
1481 upon the underlying HOST_WIDE_INTs works without masking. */
1483 static tree
1484 wide_int_to_tree_1 (tree type, const wide_int_ref &pcst)
1486 tree t;
1487 int ix = -1;
1488 int limit = 0;
1490 gcc_assert (type);
1491 unsigned int prec = TYPE_PRECISION (type);
1492 signop sgn = TYPE_SIGN (type);
1494 /* Verify that everything is canonical. */
1495 int l = pcst.get_len ();
1496 if (l > 1)
1498 if (pcst.elt (l - 1) == 0)
1499 gcc_checking_assert (pcst.elt (l - 2) < 0);
1500 if (pcst.elt (l - 1) == HOST_WIDE_INT_M1)
1501 gcc_checking_assert (pcst.elt (l - 2) >= 0);
1504 wide_int cst = wide_int::from (pcst, prec, sgn);
1505 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1507 if (ext_len == 1)
1509 /* We just need to store a single HOST_WIDE_INT. */
1510 HOST_WIDE_INT hwi;
1511 if (TYPE_UNSIGNED (type))
1512 hwi = cst.to_uhwi ();
1513 else
1514 hwi = cst.to_shwi ();
1516 switch (TREE_CODE (type))
1518 case NULLPTR_TYPE:
1519 gcc_assert (hwi == 0);
1520 /* Fallthru. */
1522 case POINTER_TYPE:
1523 case REFERENCE_TYPE:
1524 /* Cache NULL pointer and zero bounds. */
1525 if (hwi == 0)
1527 limit = 1;
1528 ix = 0;
1530 break;
1532 case BOOLEAN_TYPE:
1533 /* Cache false or true. */
1534 limit = 2;
1535 if (IN_RANGE (hwi, 0, 1))
1536 ix = hwi;
1537 break;
1539 case INTEGER_TYPE:
1540 case OFFSET_TYPE:
1541 if (TYPE_SIGN (type) == UNSIGNED)
1543 /* Cache [0, N). */
1544 limit = INTEGER_SHARE_LIMIT;
1545 if (IN_RANGE (hwi, 0, INTEGER_SHARE_LIMIT - 1))
1546 ix = hwi;
1548 else
1550 /* Cache [-1, N). */
1551 limit = INTEGER_SHARE_LIMIT + 1;
1552 if (IN_RANGE (hwi, -1, INTEGER_SHARE_LIMIT - 1))
1553 ix = hwi + 1;
1555 break;
1557 case ENUMERAL_TYPE:
1558 break;
1560 default:
1561 gcc_unreachable ();
1564 if (ix >= 0)
1566 /* Look for it in the type's vector of small shared ints. */
1567 if (!TYPE_CACHED_VALUES_P (type))
1569 TYPE_CACHED_VALUES_P (type) = 1;
1570 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1573 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1574 if (t)
1575 /* Make sure no one is clobbering the shared constant. */
1576 gcc_checking_assert (TREE_TYPE (t) == type
1577 && TREE_INT_CST_NUNITS (t) == 1
1578 && TREE_INT_CST_OFFSET_NUNITS (t) == 1
1579 && TREE_INT_CST_EXT_NUNITS (t) == 1
1580 && TREE_INT_CST_ELT (t, 0) == hwi);
1581 else
1583 /* Create a new shared int. */
1584 t = build_new_int_cst (type, cst);
1585 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1588 else
1590 /* Use the cache of larger shared ints, using int_cst_node as
1591 a temporary. */
1593 TREE_INT_CST_ELT (int_cst_node, 0) = hwi;
1594 TREE_TYPE (int_cst_node) = type;
1596 tree *slot = int_cst_hash_table->find_slot (int_cst_node, INSERT);
1597 t = *slot;
1598 if (!t)
1600 /* Insert this one into the hash table. */
1601 t = int_cst_node;
1602 *slot = t;
1603 /* Make a new node for next time round. */
1604 int_cst_node = make_int_cst (1, 1);
1608 else
1610 /* The value either hashes properly or we drop it on the floor
1611 for the gc to take care of. There will not be enough of them
1612 to worry about. */
1614 tree nt = build_new_int_cst (type, cst);
1615 tree *slot = int_cst_hash_table->find_slot (nt, INSERT);
1616 t = *slot;
1617 if (!t)
1619 /* Insert this one into the hash table. */
1620 t = nt;
1621 *slot = t;
1623 else
1624 ggc_free (nt);
1627 return t;
1630 hashval_t
1631 poly_int_cst_hasher::hash (tree t)
1633 inchash::hash hstate;
1635 hstate.add_int (TYPE_UID (TREE_TYPE (t)));
1636 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1637 hstate.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t, i)));
1639 return hstate.end ();
1642 bool
1643 poly_int_cst_hasher::equal (tree x, const compare_type &y)
1645 if (TREE_TYPE (x) != y.first)
1646 return false;
1647 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1648 if (wi::to_wide (POLY_INT_CST_COEFF (x, i)) != y.second->coeffs[i])
1649 return false;
1650 return true;
1653 /* Build a POLY_INT_CST node with type TYPE and with the elements in VALUES.
1654 The elements must also have type TYPE. */
1656 tree
1657 build_poly_int_cst (tree type, const poly_wide_int_ref &values)
1659 unsigned int prec = TYPE_PRECISION (type);
1660 gcc_assert (prec <= values.coeffs[0].get_precision ());
1661 poly_wide_int c = poly_wide_int::from (values, prec, SIGNED);
1663 inchash::hash h;
1664 h.add_int (TYPE_UID (type));
1665 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1666 h.add_wide_int (c.coeffs[i]);
1667 poly_int_cst_hasher::compare_type comp (type, &c);
1668 tree *slot = poly_int_cst_hash_table->find_slot_with_hash (comp, h.end (),
1669 INSERT);
1670 if (*slot == NULL_TREE)
1672 tree coeffs[NUM_POLY_INT_COEFFS];
1673 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1674 coeffs[i] = wide_int_to_tree_1 (type, c.coeffs[i]);
1675 *slot = build_new_poly_int_cst (type, coeffs);
1677 return *slot;
1680 /* Create a constant tree with value VALUE in type TYPE. */
1682 tree
1683 wide_int_to_tree (tree type, const poly_wide_int_ref &value)
1685 if (value.is_constant ())
1686 return wide_int_to_tree_1 (type, value.coeffs[0]);
1687 return build_poly_int_cst (type, value);
1690 void
1691 cache_integer_cst (tree t)
1693 tree type = TREE_TYPE (t);
1694 int ix = -1;
1695 int limit = 0;
1696 int prec = TYPE_PRECISION (type);
1698 gcc_assert (!TREE_OVERFLOW (t));
1700 switch (TREE_CODE (type))
1702 case NULLPTR_TYPE:
1703 gcc_assert (integer_zerop (t));
1704 /* Fallthru. */
1706 case POINTER_TYPE:
1707 case REFERENCE_TYPE:
1708 /* Cache NULL pointer. */
1709 if (integer_zerop (t))
1711 limit = 1;
1712 ix = 0;
1714 break;
1716 case BOOLEAN_TYPE:
1717 /* Cache false or true. */
1718 limit = 2;
1719 if (wi::ltu_p (wi::to_wide (t), 2))
1720 ix = TREE_INT_CST_ELT (t, 0);
1721 break;
1723 case INTEGER_TYPE:
1724 case OFFSET_TYPE:
1725 if (TYPE_UNSIGNED (type))
1727 /* Cache 0..N */
1728 limit = INTEGER_SHARE_LIMIT;
1730 /* This is a little hokie, but if the prec is smaller than
1731 what is necessary to hold INTEGER_SHARE_LIMIT, then the
1732 obvious test will not get the correct answer. */
1733 if (prec < HOST_BITS_PER_WIDE_INT)
1735 if (tree_to_uhwi (t) < (unsigned HOST_WIDE_INT) INTEGER_SHARE_LIMIT)
1736 ix = tree_to_uhwi (t);
1738 else if (wi::ltu_p (wi::to_wide (t), INTEGER_SHARE_LIMIT))
1739 ix = tree_to_uhwi (t);
1741 else
1743 /* Cache -1..N */
1744 limit = INTEGER_SHARE_LIMIT + 1;
1746 if (integer_minus_onep (t))
1747 ix = 0;
1748 else if (!wi::neg_p (wi::to_wide (t)))
1750 if (prec < HOST_BITS_PER_WIDE_INT)
1752 if (tree_to_shwi (t) < INTEGER_SHARE_LIMIT)
1753 ix = tree_to_shwi (t) + 1;
1755 else if (wi::ltu_p (wi::to_wide (t), INTEGER_SHARE_LIMIT))
1756 ix = tree_to_shwi (t) + 1;
1759 break;
1761 case ENUMERAL_TYPE:
1762 break;
1764 default:
1765 gcc_unreachable ();
1768 if (ix >= 0)
1770 /* Look for it in the type's vector of small shared ints. */
1771 if (!TYPE_CACHED_VALUES_P (type))
1773 TYPE_CACHED_VALUES_P (type) = 1;
1774 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1777 gcc_assert (TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) == NULL_TREE);
1778 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1780 else
1782 /* Use the cache of larger shared ints. */
1783 tree *slot = int_cst_hash_table->find_slot (t, INSERT);
1784 /* If there is already an entry for the number verify it's the
1785 same. */
1786 if (*slot)
1787 gcc_assert (wi::to_wide (tree (*slot)) == wi::to_wide (t));
1788 else
1789 /* Otherwise insert this one into the hash table. */
1790 *slot = t;
1795 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1796 and the rest are zeros. */
1798 tree
1799 build_low_bits_mask (tree type, unsigned bits)
1801 gcc_assert (bits <= TYPE_PRECISION (type));
1803 return wide_int_to_tree (type, wi::mask (bits, false,
1804 TYPE_PRECISION (type)));
1807 /* Checks that X is integer constant that can be expressed in (unsigned)
1808 HOST_WIDE_INT without loss of precision. */
1810 bool
1811 cst_and_fits_in_hwi (const_tree x)
1813 return (TREE_CODE (x) == INTEGER_CST
1814 && (tree_fits_shwi_p (x) || tree_fits_uhwi_p (x)));
1817 /* Build a newly constructed VECTOR_CST with the given values of
1818 (VECTOR_CST_)LOG2_NPATTERNS and (VECTOR_CST_)NELTS_PER_PATTERN. */
1820 tree
1821 make_vector (unsigned log2_npatterns,
1822 unsigned int nelts_per_pattern MEM_STAT_DECL)
1824 gcc_assert (IN_RANGE (nelts_per_pattern, 1, 3));
1825 tree t;
1826 unsigned npatterns = 1 << log2_npatterns;
1827 unsigned encoded_nelts = npatterns * nelts_per_pattern;
1828 unsigned length = (sizeof (struct tree_vector)
1829 + (encoded_nelts - 1) * sizeof (tree));
1831 record_node_allocation_statistics (VECTOR_CST, length);
1833 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1835 TREE_SET_CODE (t, VECTOR_CST);
1836 TREE_CONSTANT (t) = 1;
1837 VECTOR_CST_LOG2_NPATTERNS (t) = log2_npatterns;
1838 VECTOR_CST_NELTS_PER_PATTERN (t) = nelts_per_pattern;
1840 return t;
1843 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1844 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1846 tree
1847 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1849 unsigned HOST_WIDE_INT idx, nelts;
1850 tree value;
1852 /* We can't construct a VECTOR_CST for a variable number of elements. */
1853 nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
1854 tree_vector_builder vec (type, nelts, 1);
1855 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1857 if (TREE_CODE (value) == VECTOR_CST)
1859 /* If NELTS is constant then this must be too. */
1860 unsigned int sub_nelts = VECTOR_CST_NELTS (value).to_constant ();
1861 for (unsigned i = 0; i < sub_nelts; ++i)
1862 vec.quick_push (VECTOR_CST_ELT (value, i));
1864 else
1865 vec.quick_push (value);
1867 while (vec.length () < nelts)
1868 vec.quick_push (build_zero_cst (TREE_TYPE (type)));
1870 return vec.build ();
1873 /* Build a vector of type VECTYPE where all the elements are SCs. */
1874 tree
1875 build_vector_from_val (tree vectype, tree sc)
1877 unsigned HOST_WIDE_INT i, nunits;
1879 if (sc == error_mark_node)
1880 return sc;
1882 /* Verify that the vector type is suitable for SC. Note that there
1883 is some inconsistency in the type-system with respect to restrict
1884 qualifications of pointers. Vector types always have a main-variant
1885 element type and the qualification is applied to the vector-type.
1886 So TREE_TYPE (vector-type) does not return a properly qualified
1887 vector element-type. */
1888 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1889 TREE_TYPE (vectype)));
1891 if (CONSTANT_CLASS_P (sc))
1893 tree_vector_builder v (vectype, 1, 1);
1894 v.quick_push (sc);
1895 return v.build ();
1897 else if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits))
1898 return fold_build1 (VEC_DUPLICATE_EXPR, vectype, sc);
1899 else
1901 vec<constructor_elt, va_gc> *v;
1902 vec_alloc (v, nunits);
1903 for (i = 0; i < nunits; ++i)
1904 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1905 return build_constructor (vectype, v);
1909 /* Build a vector series of type TYPE in which element I has the value
1910 BASE + I * STEP. The result is a constant if BASE and STEP are constant
1911 and a VEC_SERIES_EXPR otherwise. */
1913 tree
1914 build_vec_series (tree type, tree base, tree step)
1916 if (integer_zerop (step))
1917 return build_vector_from_val (type, base);
1918 if (TREE_CODE (base) == INTEGER_CST && TREE_CODE (step) == INTEGER_CST)
1920 tree_vector_builder builder (type, 1, 3);
1921 tree elt1 = wide_int_to_tree (TREE_TYPE (base),
1922 wi::to_wide (base) + wi::to_wide (step));
1923 tree elt2 = wide_int_to_tree (TREE_TYPE (base),
1924 wi::to_wide (elt1) + wi::to_wide (step));
1925 builder.quick_push (base);
1926 builder.quick_push (elt1);
1927 builder.quick_push (elt2);
1928 return builder.build ();
1930 return build2 (VEC_SERIES_EXPR, type, base, step);
1933 /* Return a vector with the same number of units and number of bits
1934 as VEC_TYPE, but in which the elements are a linear series of unsigned
1935 integers { BASE, BASE + STEP, BASE + STEP * 2, ... }. */
1937 tree
1938 build_index_vector (tree vec_type, poly_uint64 base, poly_uint64 step)
1940 tree index_vec_type = vec_type;
1941 tree index_elt_type = TREE_TYPE (vec_type);
1942 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vec_type);
1943 if (!INTEGRAL_TYPE_P (index_elt_type) || !TYPE_UNSIGNED (index_elt_type))
1945 index_elt_type = build_nonstandard_integer_type
1946 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (index_elt_type)), true);
1947 index_vec_type = build_vector_type (index_elt_type, nunits);
1950 tree_vector_builder v (index_vec_type, 1, 3);
1951 for (unsigned int i = 0; i < 3; ++i)
1952 v.quick_push (build_int_cstu (index_elt_type, base + i * step));
1953 return v.build ();
1956 /* Something has messed with the elements of CONSTRUCTOR C after it was built;
1957 calculate TREE_CONSTANT and TREE_SIDE_EFFECTS. */
1959 void
1960 recompute_constructor_flags (tree c)
1962 unsigned int i;
1963 tree val;
1964 bool constant_p = true;
1965 bool side_effects_p = false;
1966 vec<constructor_elt, va_gc> *vals = CONSTRUCTOR_ELTS (c);
1968 FOR_EACH_CONSTRUCTOR_VALUE (vals, i, val)
1970 /* Mostly ctors will have elts that don't have side-effects, so
1971 the usual case is to scan all the elements. Hence a single
1972 loop for both const and side effects, rather than one loop
1973 each (with early outs). */
1974 if (!TREE_CONSTANT (val))
1975 constant_p = false;
1976 if (TREE_SIDE_EFFECTS (val))
1977 side_effects_p = true;
1980 TREE_SIDE_EFFECTS (c) = side_effects_p;
1981 TREE_CONSTANT (c) = constant_p;
1984 /* Make sure that TREE_CONSTANT and TREE_SIDE_EFFECTS are correct for
1985 CONSTRUCTOR C. */
1987 void
1988 verify_constructor_flags (tree c)
1990 unsigned int i;
1991 tree val;
1992 bool constant_p = TREE_CONSTANT (c);
1993 bool side_effects_p = TREE_SIDE_EFFECTS (c);
1994 vec<constructor_elt, va_gc> *vals = CONSTRUCTOR_ELTS (c);
1996 FOR_EACH_CONSTRUCTOR_VALUE (vals, i, val)
1998 if (constant_p && !TREE_CONSTANT (val))
1999 internal_error ("non-constant element in constant CONSTRUCTOR");
2000 if (!side_effects_p && TREE_SIDE_EFFECTS (val))
2001 internal_error ("side-effects element in no-side-effects CONSTRUCTOR");
2005 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
2006 are in the vec pointed to by VALS. */
2007 tree
2008 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
2010 tree c = make_node (CONSTRUCTOR);
2012 TREE_TYPE (c) = type;
2013 CONSTRUCTOR_ELTS (c) = vals;
2015 recompute_constructor_flags (c);
2017 return c;
2020 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
2021 INDEX and VALUE. */
2022 tree
2023 build_constructor_single (tree type, tree index, tree value)
2025 vec<constructor_elt, va_gc> *v;
2026 constructor_elt elt = {index, value};
2028 vec_alloc (v, 1);
2029 v->quick_push (elt);
2031 return build_constructor (type, v);
2035 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
2036 are in a list pointed to by VALS. */
2037 tree
2038 build_constructor_from_list (tree type, tree vals)
2040 tree t;
2041 vec<constructor_elt, va_gc> *v = NULL;
2043 if (vals)
2045 vec_alloc (v, list_length (vals));
2046 for (t = vals; t; t = TREE_CHAIN (t))
2047 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
2050 return build_constructor (type, v);
2053 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
2054 of elements, provided as index/value pairs. */
2056 tree
2057 build_constructor_va (tree type, int nelts, ...)
2059 vec<constructor_elt, va_gc> *v = NULL;
2060 va_list p;
2062 va_start (p, nelts);
2063 vec_alloc (v, nelts);
2064 while (nelts--)
2066 tree index = va_arg (p, tree);
2067 tree value = va_arg (p, tree);
2068 CONSTRUCTOR_APPEND_ELT (v, index, value);
2070 va_end (p);
2071 return build_constructor (type, v);
2074 /* Return a node of type TYPE for which TREE_CLOBBER_P is true. */
2076 tree
2077 build_clobber (tree type)
2079 tree clobber = build_constructor (type, NULL);
2080 TREE_THIS_VOLATILE (clobber) = true;
2081 return clobber;
2084 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
2086 tree
2087 build_fixed (tree type, FIXED_VALUE_TYPE f)
2089 tree v;
2090 FIXED_VALUE_TYPE *fp;
2092 v = make_node (FIXED_CST);
2093 fp = ggc_alloc<fixed_value> ();
2094 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
2096 TREE_TYPE (v) = type;
2097 TREE_FIXED_CST_PTR (v) = fp;
2098 return v;
2101 /* Return a new REAL_CST node whose type is TYPE and value is D. */
2103 tree
2104 build_real (tree type, REAL_VALUE_TYPE d)
2106 tree v;
2107 REAL_VALUE_TYPE *dp;
2108 int overflow = 0;
2110 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
2111 Consider doing it via real_convert now. */
2113 v = make_node (REAL_CST);
2114 dp = ggc_alloc<real_value> ();
2115 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
2117 TREE_TYPE (v) = type;
2118 TREE_REAL_CST_PTR (v) = dp;
2119 TREE_OVERFLOW (v) = overflow;
2120 return v;
2123 /* Like build_real, but first truncate D to the type. */
2125 tree
2126 build_real_truncate (tree type, REAL_VALUE_TYPE d)
2128 return build_real (type, real_value_truncate (TYPE_MODE (type), d));
2131 /* Return a new REAL_CST node whose type is TYPE
2132 and whose value is the integer value of the INTEGER_CST node I. */
2134 REAL_VALUE_TYPE
2135 real_value_from_int_cst (const_tree type, const_tree i)
2137 REAL_VALUE_TYPE d;
2139 /* Clear all bits of the real value type so that we can later do
2140 bitwise comparisons to see if two values are the same. */
2141 memset (&d, 0, sizeof d);
2143 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode, wi::to_wide (i),
2144 TYPE_SIGN (TREE_TYPE (i)));
2145 return d;
2148 /* Given a tree representing an integer constant I, return a tree
2149 representing the same value as a floating-point constant of type TYPE. */
2151 tree
2152 build_real_from_int_cst (tree type, const_tree i)
2154 tree v;
2155 int overflow = TREE_OVERFLOW (i);
2157 v = build_real (type, real_value_from_int_cst (type, i));
2159 TREE_OVERFLOW (v) |= overflow;
2160 return v;
2163 /* Return a newly constructed STRING_CST node whose value is
2164 the LEN characters at STR.
2165 Note that for a C string literal, LEN should include the trailing NUL.
2166 The TREE_TYPE is not initialized. */
2168 tree
2169 build_string (int len, const char *str)
2171 tree s;
2172 size_t length;
2174 /* Do not waste bytes provided by padding of struct tree_string. */
2175 length = len + offsetof (struct tree_string, str) + 1;
2177 record_node_allocation_statistics (STRING_CST, length);
2179 s = (tree) ggc_internal_alloc (length);
2181 memset (s, 0, sizeof (struct tree_typed));
2182 TREE_SET_CODE (s, STRING_CST);
2183 TREE_CONSTANT (s) = 1;
2184 TREE_STRING_LENGTH (s) = len;
2185 memcpy (s->string.str, str, len);
2186 s->string.str[len] = '\0';
2188 return s;
2191 /* Return a newly constructed COMPLEX_CST node whose value is
2192 specified by the real and imaginary parts REAL and IMAG.
2193 Both REAL and IMAG should be constant nodes. TYPE, if specified,
2194 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
2196 tree
2197 build_complex (tree type, tree real, tree imag)
2199 tree t = make_node (COMPLEX_CST);
2201 TREE_REALPART (t) = real;
2202 TREE_IMAGPART (t) = imag;
2203 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
2204 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
2205 return t;
2208 /* Build a complex (inf +- 0i), such as for the result of cproj.
2209 TYPE is the complex tree type of the result. If NEG is true, the
2210 imaginary zero is negative. */
2212 tree
2213 build_complex_inf (tree type, bool neg)
2215 REAL_VALUE_TYPE rinf, rzero = dconst0;
2217 real_inf (&rinf);
2218 rzero.sign = neg;
2219 return build_complex (type, build_real (TREE_TYPE (type), rinf),
2220 build_real (TREE_TYPE (type), rzero));
2223 /* Return the constant 1 in type TYPE. If TYPE has several elements, each
2224 element is set to 1. In particular, this is 1 + i for complex types. */
2226 tree
2227 build_each_one_cst (tree type)
2229 if (TREE_CODE (type) == COMPLEX_TYPE)
2231 tree scalar = build_one_cst (TREE_TYPE (type));
2232 return build_complex (type, scalar, scalar);
2234 else
2235 return build_one_cst (type);
2238 /* Return a constant of arithmetic type TYPE which is the
2239 multiplicative identity of the set TYPE. */
2241 tree
2242 build_one_cst (tree type)
2244 switch (TREE_CODE (type))
2246 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2247 case POINTER_TYPE: case REFERENCE_TYPE:
2248 case OFFSET_TYPE:
2249 return build_int_cst (type, 1);
2251 case REAL_TYPE:
2252 return build_real (type, dconst1);
2254 case FIXED_POINT_TYPE:
2255 /* We can only generate 1 for accum types. */
2256 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2257 return build_fixed (type, FCONST1 (TYPE_MODE (type)));
2259 case VECTOR_TYPE:
2261 tree scalar = build_one_cst (TREE_TYPE (type));
2263 return build_vector_from_val (type, scalar);
2266 case COMPLEX_TYPE:
2267 return build_complex (type,
2268 build_one_cst (TREE_TYPE (type)),
2269 build_zero_cst (TREE_TYPE (type)));
2271 default:
2272 gcc_unreachable ();
2276 /* Return an integer of type TYPE containing all 1's in as much precision as
2277 it contains, or a complex or vector whose subparts are such integers. */
2279 tree
2280 build_all_ones_cst (tree type)
2282 if (TREE_CODE (type) == COMPLEX_TYPE)
2284 tree scalar = build_all_ones_cst (TREE_TYPE (type));
2285 return build_complex (type, scalar, scalar);
2287 else
2288 return build_minus_one_cst (type);
2291 /* Return a constant of arithmetic type TYPE which is the
2292 opposite of the multiplicative identity of the set TYPE. */
2294 tree
2295 build_minus_one_cst (tree type)
2297 switch (TREE_CODE (type))
2299 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2300 case POINTER_TYPE: case REFERENCE_TYPE:
2301 case OFFSET_TYPE:
2302 return build_int_cst (type, -1);
2304 case REAL_TYPE:
2305 return build_real (type, dconstm1);
2307 case FIXED_POINT_TYPE:
2308 /* We can only generate 1 for accum types. */
2309 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2310 return build_fixed (type,
2311 fixed_from_double_int (double_int_minus_one,
2312 SCALAR_TYPE_MODE (type)));
2314 case VECTOR_TYPE:
2316 tree scalar = build_minus_one_cst (TREE_TYPE (type));
2318 return build_vector_from_val (type, scalar);
2321 case COMPLEX_TYPE:
2322 return build_complex (type,
2323 build_minus_one_cst (TREE_TYPE (type)),
2324 build_zero_cst (TREE_TYPE (type)));
2326 default:
2327 gcc_unreachable ();
2331 /* Build 0 constant of type TYPE. This is used by constructor folding
2332 and thus the constant should be represented in memory by
2333 zero(es). */
2335 tree
2336 build_zero_cst (tree type)
2338 switch (TREE_CODE (type))
2340 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2341 case POINTER_TYPE: case REFERENCE_TYPE:
2342 case OFFSET_TYPE: case NULLPTR_TYPE:
2343 return build_int_cst (type, 0);
2345 case REAL_TYPE:
2346 return build_real (type, dconst0);
2348 case FIXED_POINT_TYPE:
2349 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
2351 case VECTOR_TYPE:
2353 tree scalar = build_zero_cst (TREE_TYPE (type));
2355 return build_vector_from_val (type, scalar);
2358 case COMPLEX_TYPE:
2360 tree zero = build_zero_cst (TREE_TYPE (type));
2362 return build_complex (type, zero, zero);
2365 default:
2366 if (!AGGREGATE_TYPE_P (type))
2367 return fold_convert (type, integer_zero_node);
2368 return build_constructor (type, NULL);
2373 /* Build a BINFO with LEN language slots. */
2375 tree
2376 make_tree_binfo (unsigned base_binfos MEM_STAT_DECL)
2378 tree t;
2379 size_t length = (offsetof (struct tree_binfo, base_binfos)
2380 + vec<tree, va_gc>::embedded_size (base_binfos));
2382 record_node_allocation_statistics (TREE_BINFO, length);
2384 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
2386 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
2388 TREE_SET_CODE (t, TREE_BINFO);
2390 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
2392 return t;
2395 /* Create a CASE_LABEL_EXPR tree node and return it. */
2397 tree
2398 build_case_label (tree low_value, tree high_value, tree label_decl)
2400 tree t = make_node (CASE_LABEL_EXPR);
2402 TREE_TYPE (t) = void_type_node;
2403 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
2405 CASE_LOW (t) = low_value;
2406 CASE_HIGH (t) = high_value;
2407 CASE_LABEL (t) = label_decl;
2408 CASE_CHAIN (t) = NULL_TREE;
2410 return t;
2413 /* Build a newly constructed INTEGER_CST node. LEN and EXT_LEN are the
2414 values of TREE_INT_CST_NUNITS and TREE_INT_CST_EXT_NUNITS respectively.
2415 The latter determines the length of the HOST_WIDE_INT vector. */
2417 tree
2418 make_int_cst (int len, int ext_len MEM_STAT_DECL)
2420 tree t;
2421 int length = ((ext_len - 1) * sizeof (HOST_WIDE_INT)
2422 + sizeof (struct tree_int_cst));
2424 gcc_assert (len);
2425 record_node_allocation_statistics (INTEGER_CST, length);
2427 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2429 TREE_SET_CODE (t, INTEGER_CST);
2430 TREE_INT_CST_NUNITS (t) = len;
2431 TREE_INT_CST_EXT_NUNITS (t) = ext_len;
2432 /* to_offset can only be applied to trees that are offset_int-sized
2433 or smaller. EXT_LEN is correct if it fits, otherwise the constant
2434 must be exactly the precision of offset_int and so LEN is correct. */
2435 if (ext_len <= OFFSET_INT_ELTS)
2436 TREE_INT_CST_OFFSET_NUNITS (t) = ext_len;
2437 else
2438 TREE_INT_CST_OFFSET_NUNITS (t) = len;
2440 TREE_CONSTANT (t) = 1;
2442 return t;
2445 /* Build a newly constructed TREE_VEC node of length LEN. */
2447 tree
2448 make_tree_vec (int len MEM_STAT_DECL)
2450 tree t;
2451 size_t length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2453 record_node_allocation_statistics (TREE_VEC, length);
2455 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2457 TREE_SET_CODE (t, TREE_VEC);
2458 TREE_VEC_LENGTH (t) = len;
2460 return t;
2463 /* Grow a TREE_VEC node to new length LEN. */
2465 tree
2466 grow_tree_vec (tree v, int len MEM_STAT_DECL)
2468 gcc_assert (TREE_CODE (v) == TREE_VEC);
2470 int oldlen = TREE_VEC_LENGTH (v);
2471 gcc_assert (len > oldlen);
2473 size_t oldlength = (oldlen - 1) * sizeof (tree) + sizeof (struct tree_vec);
2474 size_t length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2476 record_node_allocation_statistics (TREE_VEC, length - oldlength);
2478 v = (tree) ggc_realloc (v, length PASS_MEM_STAT);
2480 TREE_VEC_LENGTH (v) = len;
2482 return v;
2485 /* Return 1 if EXPR is the constant zero, whether it is integral, float or
2486 fixed, and scalar, complex or vector. */
2488 bool
2489 zerop (const_tree expr)
2491 return (integer_zerop (expr)
2492 || real_zerop (expr)
2493 || fixed_zerop (expr));
2496 /* Return 1 if EXPR is the integer constant zero or a complex constant
2497 of zero. */
2499 bool
2500 integer_zerop (const_tree expr)
2502 switch (TREE_CODE (expr))
2504 case INTEGER_CST:
2505 return wi::to_wide (expr) == 0;
2506 case COMPLEX_CST:
2507 return (integer_zerop (TREE_REALPART (expr))
2508 && integer_zerop (TREE_IMAGPART (expr)));
2509 case VECTOR_CST:
2510 return (VECTOR_CST_NPATTERNS (expr) == 1
2511 && VECTOR_CST_DUPLICATE_P (expr)
2512 && integer_zerop (VECTOR_CST_ENCODED_ELT (expr, 0)));
2513 default:
2514 return false;
2518 /* Return 1 if EXPR is the integer constant one or the corresponding
2519 complex constant. */
2521 bool
2522 integer_onep (const_tree expr)
2524 switch (TREE_CODE (expr))
2526 case INTEGER_CST:
2527 return wi::eq_p (wi::to_widest (expr), 1);
2528 case COMPLEX_CST:
2529 return (integer_onep (TREE_REALPART (expr))
2530 && integer_zerop (TREE_IMAGPART (expr)));
2531 case VECTOR_CST:
2532 return (VECTOR_CST_NPATTERNS (expr) == 1
2533 && VECTOR_CST_DUPLICATE_P (expr)
2534 && integer_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
2535 default:
2536 return false;
2540 /* Return 1 if EXPR is the integer constant one. For complex and vector,
2541 return 1 if every piece is the integer constant one. */
2543 bool
2544 integer_each_onep (const_tree expr)
2546 if (TREE_CODE (expr) == COMPLEX_CST)
2547 return (integer_onep (TREE_REALPART (expr))
2548 && integer_onep (TREE_IMAGPART (expr)));
2549 else
2550 return integer_onep (expr);
2553 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
2554 it contains, or a complex or vector whose subparts are such integers. */
2556 bool
2557 integer_all_onesp (const_tree expr)
2559 if (TREE_CODE (expr) == COMPLEX_CST
2560 && integer_all_onesp (TREE_REALPART (expr))
2561 && integer_all_onesp (TREE_IMAGPART (expr)))
2562 return true;
2564 else if (TREE_CODE (expr) == VECTOR_CST)
2565 return (VECTOR_CST_NPATTERNS (expr) == 1
2566 && VECTOR_CST_DUPLICATE_P (expr)
2567 && integer_all_onesp (VECTOR_CST_ENCODED_ELT (expr, 0)));
2569 else if (TREE_CODE (expr) != INTEGER_CST)
2570 return false;
2572 return (wi::max_value (TYPE_PRECISION (TREE_TYPE (expr)), UNSIGNED)
2573 == wi::to_wide (expr));
2576 /* Return 1 if EXPR is the integer constant minus one. */
2578 bool
2579 integer_minus_onep (const_tree expr)
2581 if (TREE_CODE (expr) == COMPLEX_CST)
2582 return (integer_all_onesp (TREE_REALPART (expr))
2583 && integer_zerop (TREE_IMAGPART (expr)));
2584 else
2585 return integer_all_onesp (expr);
2588 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
2589 one bit on). */
2591 bool
2592 integer_pow2p (const_tree expr)
2594 if (TREE_CODE (expr) == COMPLEX_CST
2595 && integer_pow2p (TREE_REALPART (expr))
2596 && integer_zerop (TREE_IMAGPART (expr)))
2597 return true;
2599 if (TREE_CODE (expr) != INTEGER_CST)
2600 return false;
2602 return wi::popcount (wi::to_wide (expr)) == 1;
2605 /* Return 1 if EXPR is an integer constant other than zero or a
2606 complex constant other than zero. */
2608 bool
2609 integer_nonzerop (const_tree expr)
2611 return ((TREE_CODE (expr) == INTEGER_CST
2612 && wi::to_wide (expr) != 0)
2613 || (TREE_CODE (expr) == COMPLEX_CST
2614 && (integer_nonzerop (TREE_REALPART (expr))
2615 || integer_nonzerop (TREE_IMAGPART (expr)))));
2618 /* Return 1 if EXPR is the integer constant one. For vector,
2619 return 1 if every piece is the integer constant minus one
2620 (representing the value TRUE). */
2622 bool
2623 integer_truep (const_tree expr)
2625 if (TREE_CODE (expr) == VECTOR_CST)
2626 return integer_all_onesp (expr);
2627 return integer_onep (expr);
2630 /* Return 1 if EXPR is the fixed-point constant zero. */
2632 bool
2633 fixed_zerop (const_tree expr)
2635 return (TREE_CODE (expr) == FIXED_CST
2636 && TREE_FIXED_CST (expr).data.is_zero ());
2639 /* Return the power of two represented by a tree node known to be a
2640 power of two. */
2643 tree_log2 (const_tree expr)
2645 if (TREE_CODE (expr) == COMPLEX_CST)
2646 return tree_log2 (TREE_REALPART (expr));
2648 return wi::exact_log2 (wi::to_wide (expr));
2651 /* Similar, but return the largest integer Y such that 2 ** Y is less
2652 than or equal to EXPR. */
2655 tree_floor_log2 (const_tree expr)
2657 if (TREE_CODE (expr) == COMPLEX_CST)
2658 return tree_log2 (TREE_REALPART (expr));
2660 return wi::floor_log2 (wi::to_wide (expr));
2663 /* Return number of known trailing zero bits in EXPR, or, if the value of
2664 EXPR is known to be zero, the precision of it's type. */
2666 unsigned int
2667 tree_ctz (const_tree expr)
2669 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
2670 && !POINTER_TYPE_P (TREE_TYPE (expr)))
2671 return 0;
2673 unsigned int ret1, ret2, prec = TYPE_PRECISION (TREE_TYPE (expr));
2674 switch (TREE_CODE (expr))
2676 case INTEGER_CST:
2677 ret1 = wi::ctz (wi::to_wide (expr));
2678 return MIN (ret1, prec);
2679 case SSA_NAME:
2680 ret1 = wi::ctz (get_nonzero_bits (expr));
2681 return MIN (ret1, prec);
2682 case PLUS_EXPR:
2683 case MINUS_EXPR:
2684 case BIT_IOR_EXPR:
2685 case BIT_XOR_EXPR:
2686 case MIN_EXPR:
2687 case MAX_EXPR:
2688 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2689 if (ret1 == 0)
2690 return ret1;
2691 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2692 return MIN (ret1, ret2);
2693 case POINTER_PLUS_EXPR:
2694 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2695 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2696 /* Second operand is sizetype, which could be in theory
2697 wider than pointer's precision. Make sure we never
2698 return more than prec. */
2699 ret2 = MIN (ret2, prec);
2700 return MIN (ret1, ret2);
2701 case BIT_AND_EXPR:
2702 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2703 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2704 return MAX (ret1, ret2);
2705 case MULT_EXPR:
2706 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2707 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2708 return MIN (ret1 + ret2, prec);
2709 case LSHIFT_EXPR:
2710 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2711 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2712 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2714 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2715 return MIN (ret1 + ret2, prec);
2717 return ret1;
2718 case RSHIFT_EXPR:
2719 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2720 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2722 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2723 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2724 if (ret1 > ret2)
2725 return ret1 - ret2;
2727 return 0;
2728 case TRUNC_DIV_EXPR:
2729 case CEIL_DIV_EXPR:
2730 case FLOOR_DIV_EXPR:
2731 case ROUND_DIV_EXPR:
2732 case EXACT_DIV_EXPR:
2733 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
2734 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) == 1)
2736 int l = tree_log2 (TREE_OPERAND (expr, 1));
2737 if (l >= 0)
2739 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2740 ret2 = l;
2741 if (ret1 > ret2)
2742 return ret1 - ret2;
2745 return 0;
2746 CASE_CONVERT:
2747 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2748 if (ret1 && ret1 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2749 ret1 = prec;
2750 return MIN (ret1, prec);
2751 case SAVE_EXPR:
2752 return tree_ctz (TREE_OPERAND (expr, 0));
2753 case COND_EXPR:
2754 ret1 = tree_ctz (TREE_OPERAND (expr, 1));
2755 if (ret1 == 0)
2756 return 0;
2757 ret2 = tree_ctz (TREE_OPERAND (expr, 2));
2758 return MIN (ret1, ret2);
2759 case COMPOUND_EXPR:
2760 return tree_ctz (TREE_OPERAND (expr, 1));
2761 case ADDR_EXPR:
2762 ret1 = get_pointer_alignment (CONST_CAST_TREE (expr));
2763 if (ret1 > BITS_PER_UNIT)
2765 ret1 = ctz_hwi (ret1 / BITS_PER_UNIT);
2766 return MIN (ret1, prec);
2768 return 0;
2769 default:
2770 return 0;
2774 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
2775 decimal float constants, so don't return 1 for them. */
2777 bool
2778 real_zerop (const_tree expr)
2780 switch (TREE_CODE (expr))
2782 case REAL_CST:
2783 return real_equal (&TREE_REAL_CST (expr), &dconst0)
2784 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2785 case COMPLEX_CST:
2786 return real_zerop (TREE_REALPART (expr))
2787 && real_zerop (TREE_IMAGPART (expr));
2788 case VECTOR_CST:
2790 /* Don't simply check for a duplicate because the predicate
2791 accepts both +0.0 and -0.0. */
2792 unsigned count = vector_cst_encoded_nelts (expr);
2793 for (unsigned int i = 0; i < count; ++i)
2794 if (!real_zerop (VECTOR_CST_ENCODED_ELT (expr, i)))
2795 return false;
2796 return true;
2798 default:
2799 return false;
2803 /* Return 1 if EXPR is the real constant one in real or complex form.
2804 Trailing zeroes matter for decimal float constants, so don't return
2805 1 for them. */
2807 bool
2808 real_onep (const_tree expr)
2810 switch (TREE_CODE (expr))
2812 case REAL_CST:
2813 return real_equal (&TREE_REAL_CST (expr), &dconst1)
2814 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2815 case COMPLEX_CST:
2816 return real_onep (TREE_REALPART (expr))
2817 && real_zerop (TREE_IMAGPART (expr));
2818 case VECTOR_CST:
2819 return (VECTOR_CST_NPATTERNS (expr) == 1
2820 && VECTOR_CST_DUPLICATE_P (expr)
2821 && real_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
2822 default:
2823 return false;
2827 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2828 matter for decimal float constants, so don't return 1 for them. */
2830 bool
2831 real_minus_onep (const_tree expr)
2833 switch (TREE_CODE (expr))
2835 case REAL_CST:
2836 return real_equal (&TREE_REAL_CST (expr), &dconstm1)
2837 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2838 case COMPLEX_CST:
2839 return real_minus_onep (TREE_REALPART (expr))
2840 && real_zerop (TREE_IMAGPART (expr));
2841 case VECTOR_CST:
2842 return (VECTOR_CST_NPATTERNS (expr) == 1
2843 && VECTOR_CST_DUPLICATE_P (expr)
2844 && real_minus_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
2845 default:
2846 return false;
2850 /* Nonzero if EXP is a constant or a cast of a constant. */
2852 bool
2853 really_constant_p (const_tree exp)
2855 /* This is not quite the same as STRIP_NOPS. It does more. */
2856 while (CONVERT_EXPR_P (exp)
2857 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2858 exp = TREE_OPERAND (exp, 0);
2859 return TREE_CONSTANT (exp);
2862 /* Return true if T holds a polynomial pointer difference, storing it in
2863 *VALUE if so. A true return means that T's precision is no greater
2864 than 64 bits, which is the largest address space we support, so *VALUE
2865 never loses precision. However, the signedness of the result does
2866 not necessarily match the signedness of T: sometimes an unsigned type
2867 like sizetype is used to encode a value that is actually negative. */
2869 bool
2870 ptrdiff_tree_p (const_tree t, poly_int64_pod *value)
2872 if (!t)
2873 return false;
2874 if (TREE_CODE (t) == INTEGER_CST)
2876 if (!cst_and_fits_in_hwi (t))
2877 return false;
2878 *value = int_cst_value (t);
2879 return true;
2881 if (POLY_INT_CST_P (t))
2883 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
2884 if (!cst_and_fits_in_hwi (POLY_INT_CST_COEFF (t, i)))
2885 return false;
2886 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
2887 value->coeffs[i] = int_cst_value (POLY_INT_CST_COEFF (t, i));
2888 return true;
2890 return false;
2893 poly_int64
2894 tree_to_poly_int64 (const_tree t)
2896 gcc_assert (tree_fits_poly_int64_p (t));
2897 if (POLY_INT_CST_P (t))
2898 return poly_int_cst_value (t).force_shwi ();
2899 return TREE_INT_CST_LOW (t);
2902 poly_uint64
2903 tree_to_poly_uint64 (const_tree t)
2905 gcc_assert (tree_fits_poly_uint64_p (t));
2906 if (POLY_INT_CST_P (t))
2907 return poly_int_cst_value (t).force_uhwi ();
2908 return TREE_INT_CST_LOW (t);
2911 /* Return first list element whose TREE_VALUE is ELEM.
2912 Return 0 if ELEM is not in LIST. */
2914 tree
2915 value_member (tree elem, tree list)
2917 while (list)
2919 if (elem == TREE_VALUE (list))
2920 return list;
2921 list = TREE_CHAIN (list);
2923 return NULL_TREE;
2926 /* Return first list element whose TREE_PURPOSE is ELEM.
2927 Return 0 if ELEM is not in LIST. */
2929 tree
2930 purpose_member (const_tree elem, tree list)
2932 while (list)
2934 if (elem == TREE_PURPOSE (list))
2935 return list;
2936 list = TREE_CHAIN (list);
2938 return NULL_TREE;
2941 /* Return true if ELEM is in V. */
2943 bool
2944 vec_member (const_tree elem, vec<tree, va_gc> *v)
2946 unsigned ix;
2947 tree t;
2948 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2949 if (elem == t)
2950 return true;
2951 return false;
2954 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2955 NULL_TREE. */
2957 tree
2958 chain_index (int idx, tree chain)
2960 for (; chain && idx > 0; --idx)
2961 chain = TREE_CHAIN (chain);
2962 return chain;
2965 /* Return nonzero if ELEM is part of the chain CHAIN. */
2967 bool
2968 chain_member (const_tree elem, const_tree chain)
2970 while (chain)
2972 if (elem == chain)
2973 return true;
2974 chain = DECL_CHAIN (chain);
2977 return false;
2980 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2981 We expect a null pointer to mark the end of the chain.
2982 This is the Lisp primitive `length'. */
2985 list_length (const_tree t)
2987 const_tree p = t;
2988 #ifdef ENABLE_TREE_CHECKING
2989 const_tree q = t;
2990 #endif
2991 int len = 0;
2993 while (p)
2995 p = TREE_CHAIN (p);
2996 #ifdef ENABLE_TREE_CHECKING
2997 if (len % 2)
2998 q = TREE_CHAIN (q);
2999 gcc_assert (p != q);
3000 #endif
3001 len++;
3004 return len;
3007 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
3008 UNION_TYPE TYPE, or NULL_TREE if none. */
3010 tree
3011 first_field (const_tree type)
3013 tree t = TYPE_FIELDS (type);
3014 while (t && TREE_CODE (t) != FIELD_DECL)
3015 t = TREE_CHAIN (t);
3016 return t;
3019 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
3020 by modifying the last node in chain 1 to point to chain 2.
3021 This is the Lisp primitive `nconc'. */
3023 tree
3024 chainon (tree op1, tree op2)
3026 tree t1;
3028 if (!op1)
3029 return op2;
3030 if (!op2)
3031 return op1;
3033 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
3034 continue;
3035 TREE_CHAIN (t1) = op2;
3037 #ifdef ENABLE_TREE_CHECKING
3039 tree t2;
3040 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
3041 gcc_assert (t2 != t1);
3043 #endif
3045 return op1;
3048 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
3050 tree
3051 tree_last (tree chain)
3053 tree next;
3054 if (chain)
3055 while ((next = TREE_CHAIN (chain)))
3056 chain = next;
3057 return chain;
3060 /* Reverse the order of elements in the chain T,
3061 and return the new head of the chain (old last element). */
3063 tree
3064 nreverse (tree t)
3066 tree prev = 0, decl, next;
3067 for (decl = t; decl; decl = next)
3069 /* We shouldn't be using this function to reverse BLOCK chains; we
3070 have blocks_nreverse for that. */
3071 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
3072 next = TREE_CHAIN (decl);
3073 TREE_CHAIN (decl) = prev;
3074 prev = decl;
3076 return prev;
3079 /* Return a newly created TREE_LIST node whose
3080 purpose and value fields are PARM and VALUE. */
3082 tree
3083 build_tree_list (tree parm, tree value MEM_STAT_DECL)
3085 tree t = make_node (TREE_LIST PASS_MEM_STAT);
3086 TREE_PURPOSE (t) = parm;
3087 TREE_VALUE (t) = value;
3088 return t;
3091 /* Build a chain of TREE_LIST nodes from a vector. */
3093 tree
3094 build_tree_list_vec (const vec<tree, va_gc> *vec MEM_STAT_DECL)
3096 tree ret = NULL_TREE;
3097 tree *pp = &ret;
3098 unsigned int i;
3099 tree t;
3100 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
3102 *pp = build_tree_list (NULL, t PASS_MEM_STAT);
3103 pp = &TREE_CHAIN (*pp);
3105 return ret;
3108 /* Return a newly created TREE_LIST node whose
3109 purpose and value fields are PURPOSE and VALUE
3110 and whose TREE_CHAIN is CHAIN. */
3112 tree
3113 tree_cons (tree purpose, tree value, tree chain MEM_STAT_DECL)
3115 tree node;
3117 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
3118 memset (node, 0, sizeof (struct tree_common));
3120 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
3122 TREE_SET_CODE (node, TREE_LIST);
3123 TREE_CHAIN (node) = chain;
3124 TREE_PURPOSE (node) = purpose;
3125 TREE_VALUE (node) = value;
3126 return node;
3129 /* Return the values of the elements of a CONSTRUCTOR as a vector of
3130 trees. */
3132 vec<tree, va_gc> *
3133 ctor_to_vec (tree ctor)
3135 vec<tree, va_gc> *vec;
3136 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
3137 unsigned int ix;
3138 tree val;
3140 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
3141 vec->quick_push (val);
3143 return vec;
3146 /* Return the size nominally occupied by an object of type TYPE
3147 when it resides in memory. The value is measured in units of bytes,
3148 and its data type is that normally used for type sizes
3149 (which is the first type created by make_signed_type or
3150 make_unsigned_type). */
3152 tree
3153 size_in_bytes_loc (location_t loc, const_tree type)
3155 tree t;
3157 if (type == error_mark_node)
3158 return integer_zero_node;
3160 type = TYPE_MAIN_VARIANT (type);
3161 t = TYPE_SIZE_UNIT (type);
3163 if (t == 0)
3165 lang_hooks.types.incomplete_type_error (loc, NULL_TREE, type);
3166 return size_zero_node;
3169 return t;
3172 /* Return the size of TYPE (in bytes) as a wide integer
3173 or return -1 if the size can vary or is larger than an integer. */
3175 HOST_WIDE_INT
3176 int_size_in_bytes (const_tree type)
3178 tree t;
3180 if (type == error_mark_node)
3181 return 0;
3183 type = TYPE_MAIN_VARIANT (type);
3184 t = TYPE_SIZE_UNIT (type);
3186 if (t && tree_fits_uhwi_p (t))
3187 return TREE_INT_CST_LOW (t);
3188 else
3189 return -1;
3192 /* Return the maximum size of TYPE (in bytes) as a wide integer
3193 or return -1 if the size can vary or is larger than an integer. */
3195 HOST_WIDE_INT
3196 max_int_size_in_bytes (const_tree type)
3198 HOST_WIDE_INT size = -1;
3199 tree size_tree;
3201 /* If this is an array type, check for a possible MAX_SIZE attached. */
3203 if (TREE_CODE (type) == ARRAY_TYPE)
3205 size_tree = TYPE_ARRAY_MAX_SIZE (type);
3207 if (size_tree && tree_fits_uhwi_p (size_tree))
3208 size = tree_to_uhwi (size_tree);
3211 /* If we still haven't been able to get a size, see if the language
3212 can compute a maximum size. */
3214 if (size == -1)
3216 size_tree = lang_hooks.types.max_size (type);
3218 if (size_tree && tree_fits_uhwi_p (size_tree))
3219 size = tree_to_uhwi (size_tree);
3222 return size;
3225 /* Return the bit position of FIELD, in bits from the start of the record.
3226 This is a tree of type bitsizetype. */
3228 tree
3229 bit_position (const_tree field)
3231 return bit_from_pos (DECL_FIELD_OFFSET (field),
3232 DECL_FIELD_BIT_OFFSET (field));
3235 /* Return the byte position of FIELD, in bytes from the start of the record.
3236 This is a tree of type sizetype. */
3238 tree
3239 byte_position (const_tree field)
3241 return byte_from_pos (DECL_FIELD_OFFSET (field),
3242 DECL_FIELD_BIT_OFFSET (field));
3245 /* Likewise, but return as an integer. It must be representable in
3246 that way (since it could be a signed value, we don't have the
3247 option of returning -1 like int_size_in_byte can. */
3249 HOST_WIDE_INT
3250 int_byte_position (const_tree field)
3252 return tree_to_shwi (byte_position (field));
3255 /* Return the strictest alignment, in bits, that T is known to have. */
3257 unsigned int
3258 expr_align (const_tree t)
3260 unsigned int align0, align1;
3262 switch (TREE_CODE (t))
3264 CASE_CONVERT: case NON_LVALUE_EXPR:
3265 /* If we have conversions, we know that the alignment of the
3266 object must meet each of the alignments of the types. */
3267 align0 = expr_align (TREE_OPERAND (t, 0));
3268 align1 = TYPE_ALIGN (TREE_TYPE (t));
3269 return MAX (align0, align1);
3271 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
3272 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
3273 case CLEANUP_POINT_EXPR:
3274 /* These don't change the alignment of an object. */
3275 return expr_align (TREE_OPERAND (t, 0));
3277 case COND_EXPR:
3278 /* The best we can do is say that the alignment is the least aligned
3279 of the two arms. */
3280 align0 = expr_align (TREE_OPERAND (t, 1));
3281 align1 = expr_align (TREE_OPERAND (t, 2));
3282 return MIN (align0, align1);
3284 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
3285 meaningfully, it's always 1. */
3286 case LABEL_DECL: case CONST_DECL:
3287 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
3288 case FUNCTION_DECL:
3289 gcc_assert (DECL_ALIGN (t) != 0);
3290 return DECL_ALIGN (t);
3292 default:
3293 break;
3296 /* Otherwise take the alignment from that of the type. */
3297 return TYPE_ALIGN (TREE_TYPE (t));
3300 /* Return, as a tree node, the number of elements for TYPE (which is an
3301 ARRAY_TYPE) minus one. This counts only elements of the top array. */
3303 tree
3304 array_type_nelts (const_tree type)
3306 tree index_type, min, max;
3308 /* If they did it with unspecified bounds, then we should have already
3309 given an error about it before we got here. */
3310 if (! TYPE_DOMAIN (type))
3311 return error_mark_node;
3313 index_type = TYPE_DOMAIN (type);
3314 min = TYPE_MIN_VALUE (index_type);
3315 max = TYPE_MAX_VALUE (index_type);
3317 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
3318 if (!max)
3319 return error_mark_node;
3321 return (integer_zerop (min)
3322 ? max
3323 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
3326 /* If arg is static -- a reference to an object in static storage -- then
3327 return the object. This is not the same as the C meaning of `static'.
3328 If arg isn't static, return NULL. */
3330 tree
3331 staticp (tree arg)
3333 switch (TREE_CODE (arg))
3335 case FUNCTION_DECL:
3336 /* Nested functions are static, even though taking their address will
3337 involve a trampoline as we unnest the nested function and create
3338 the trampoline on the tree level. */
3339 return arg;
3341 case VAR_DECL:
3342 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3343 && ! DECL_THREAD_LOCAL_P (arg)
3344 && ! DECL_DLLIMPORT_P (arg)
3345 ? arg : NULL);
3347 case CONST_DECL:
3348 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3349 ? arg : NULL);
3351 case CONSTRUCTOR:
3352 return TREE_STATIC (arg) ? arg : NULL;
3354 case LABEL_DECL:
3355 case STRING_CST:
3356 return arg;
3358 case COMPONENT_REF:
3359 /* If the thing being referenced is not a field, then it is
3360 something language specific. */
3361 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
3363 /* If we are referencing a bitfield, we can't evaluate an
3364 ADDR_EXPR at compile time and so it isn't a constant. */
3365 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
3366 return NULL;
3368 return staticp (TREE_OPERAND (arg, 0));
3370 case BIT_FIELD_REF:
3371 return NULL;
3373 case INDIRECT_REF:
3374 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
3376 case ARRAY_REF:
3377 case ARRAY_RANGE_REF:
3378 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
3379 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
3380 return staticp (TREE_OPERAND (arg, 0));
3381 else
3382 return NULL;
3384 case COMPOUND_LITERAL_EXPR:
3385 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
3387 default:
3388 return NULL;
3395 /* Return whether OP is a DECL whose address is function-invariant. */
3397 bool
3398 decl_address_invariant_p (const_tree op)
3400 /* The conditions below are slightly less strict than the one in
3401 staticp. */
3403 switch (TREE_CODE (op))
3405 case PARM_DECL:
3406 case RESULT_DECL:
3407 case LABEL_DECL:
3408 case FUNCTION_DECL:
3409 return true;
3411 case VAR_DECL:
3412 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3413 || DECL_THREAD_LOCAL_P (op)
3414 || DECL_CONTEXT (op) == current_function_decl
3415 || decl_function_context (op) == current_function_decl)
3416 return true;
3417 break;
3419 case CONST_DECL:
3420 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3421 || decl_function_context (op) == current_function_decl)
3422 return true;
3423 break;
3425 default:
3426 break;
3429 return false;
3432 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
3434 bool
3435 decl_address_ip_invariant_p (const_tree op)
3437 /* The conditions below are slightly less strict than the one in
3438 staticp. */
3440 switch (TREE_CODE (op))
3442 case LABEL_DECL:
3443 case FUNCTION_DECL:
3444 case STRING_CST:
3445 return true;
3447 case VAR_DECL:
3448 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
3449 && !DECL_DLLIMPORT_P (op))
3450 || DECL_THREAD_LOCAL_P (op))
3451 return true;
3452 break;
3454 case CONST_DECL:
3455 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
3456 return true;
3457 break;
3459 default:
3460 break;
3463 return false;
3467 /* Return true if T is function-invariant (internal function, does
3468 not handle arithmetic; that's handled in skip_simple_arithmetic and
3469 tree_invariant_p). */
3471 static bool
3472 tree_invariant_p_1 (tree t)
3474 tree op;
3476 if (TREE_CONSTANT (t)
3477 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
3478 return true;
3480 switch (TREE_CODE (t))
3482 case SAVE_EXPR:
3483 return true;
3485 case ADDR_EXPR:
3486 op = TREE_OPERAND (t, 0);
3487 while (handled_component_p (op))
3489 switch (TREE_CODE (op))
3491 case ARRAY_REF:
3492 case ARRAY_RANGE_REF:
3493 if (!tree_invariant_p (TREE_OPERAND (op, 1))
3494 || TREE_OPERAND (op, 2) != NULL_TREE
3495 || TREE_OPERAND (op, 3) != NULL_TREE)
3496 return false;
3497 break;
3499 case COMPONENT_REF:
3500 if (TREE_OPERAND (op, 2) != NULL_TREE)
3501 return false;
3502 break;
3504 default:;
3506 op = TREE_OPERAND (op, 0);
3509 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
3511 default:
3512 break;
3515 return false;
3518 /* Return true if T is function-invariant. */
3520 bool
3521 tree_invariant_p (tree t)
3523 tree inner = skip_simple_arithmetic (t);
3524 return tree_invariant_p_1 (inner);
3527 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
3528 Do this to any expression which may be used in more than one place,
3529 but must be evaluated only once.
3531 Normally, expand_expr would reevaluate the expression each time.
3532 Calling save_expr produces something that is evaluated and recorded
3533 the first time expand_expr is called on it. Subsequent calls to
3534 expand_expr just reuse the recorded value.
3536 The call to expand_expr that generates code that actually computes
3537 the value is the first call *at compile time*. Subsequent calls
3538 *at compile time* generate code to use the saved value.
3539 This produces correct result provided that *at run time* control
3540 always flows through the insns made by the first expand_expr
3541 before reaching the other places where the save_expr was evaluated.
3542 You, the caller of save_expr, must make sure this is so.
3544 Constants, and certain read-only nodes, are returned with no
3545 SAVE_EXPR because that is safe. Expressions containing placeholders
3546 are not touched; see tree.def for an explanation of what these
3547 are used for. */
3549 tree
3550 save_expr (tree expr)
3552 tree inner;
3554 /* If the tree evaluates to a constant, then we don't want to hide that
3555 fact (i.e. this allows further folding, and direct checks for constants).
3556 However, a read-only object that has side effects cannot be bypassed.
3557 Since it is no problem to reevaluate literals, we just return the
3558 literal node. */
3559 inner = skip_simple_arithmetic (expr);
3560 if (TREE_CODE (inner) == ERROR_MARK)
3561 return inner;
3563 if (tree_invariant_p_1 (inner))
3564 return expr;
3566 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
3567 it means that the size or offset of some field of an object depends on
3568 the value within another field.
3570 Note that it must not be the case that EXPR contains both a PLACEHOLDER_EXPR
3571 and some variable since it would then need to be both evaluated once and
3572 evaluated more than once. Front-ends must assure this case cannot
3573 happen by surrounding any such subexpressions in their own SAVE_EXPR
3574 and forcing evaluation at the proper time. */
3575 if (contains_placeholder_p (inner))
3576 return expr;
3578 expr = build1_loc (EXPR_LOCATION (expr), SAVE_EXPR, TREE_TYPE (expr), expr);
3580 /* This expression might be placed ahead of a jump to ensure that the
3581 value was computed on both sides of the jump. So make sure it isn't
3582 eliminated as dead. */
3583 TREE_SIDE_EFFECTS (expr) = 1;
3584 return expr;
3587 /* Look inside EXPR into any simple arithmetic operations. Return the
3588 outermost non-arithmetic or non-invariant node. */
3590 tree
3591 skip_simple_arithmetic (tree expr)
3593 /* We don't care about whether this can be used as an lvalue in this
3594 context. */
3595 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3596 expr = TREE_OPERAND (expr, 0);
3598 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
3599 a constant, it will be more efficient to not make another SAVE_EXPR since
3600 it will allow better simplification and GCSE will be able to merge the
3601 computations if they actually occur. */
3602 while (true)
3604 if (UNARY_CLASS_P (expr))
3605 expr = TREE_OPERAND (expr, 0);
3606 else if (BINARY_CLASS_P (expr))
3608 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
3609 expr = TREE_OPERAND (expr, 0);
3610 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
3611 expr = TREE_OPERAND (expr, 1);
3612 else
3613 break;
3615 else
3616 break;
3619 return expr;
3622 /* Look inside EXPR into simple arithmetic operations involving constants.
3623 Return the outermost non-arithmetic or non-constant node. */
3625 tree
3626 skip_simple_constant_arithmetic (tree expr)
3628 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3629 expr = TREE_OPERAND (expr, 0);
3631 while (true)
3633 if (UNARY_CLASS_P (expr))
3634 expr = TREE_OPERAND (expr, 0);
3635 else if (BINARY_CLASS_P (expr))
3637 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3638 expr = TREE_OPERAND (expr, 0);
3639 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3640 expr = TREE_OPERAND (expr, 1);
3641 else
3642 break;
3644 else
3645 break;
3648 return expr;
3651 /* Return which tree structure is used by T. */
3653 enum tree_node_structure_enum
3654 tree_node_structure (const_tree t)
3656 const enum tree_code code = TREE_CODE (t);
3657 return tree_node_structure_for_code (code);
3660 /* Set various status flags when building a CALL_EXPR object T. */
3662 static void
3663 process_call_operands (tree t)
3665 bool side_effects = TREE_SIDE_EFFECTS (t);
3666 bool read_only = false;
3667 int i = call_expr_flags (t);
3669 /* Calls have side-effects, except those to const or pure functions. */
3670 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
3671 side_effects = true;
3672 /* Propagate TREE_READONLY of arguments for const functions. */
3673 if (i & ECF_CONST)
3674 read_only = true;
3676 if (!side_effects || read_only)
3677 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
3679 tree op = TREE_OPERAND (t, i);
3680 if (op && TREE_SIDE_EFFECTS (op))
3681 side_effects = true;
3682 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
3683 read_only = false;
3686 TREE_SIDE_EFFECTS (t) = side_effects;
3687 TREE_READONLY (t) = read_only;
3690 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
3691 size or offset that depends on a field within a record. */
3693 bool
3694 contains_placeholder_p (const_tree exp)
3696 enum tree_code code;
3698 if (!exp)
3699 return 0;
3701 code = TREE_CODE (exp);
3702 if (code == PLACEHOLDER_EXPR)
3703 return 1;
3705 switch (TREE_CODE_CLASS (code))
3707 case tcc_reference:
3708 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
3709 position computations since they will be converted into a
3710 WITH_RECORD_EXPR involving the reference, which will assume
3711 here will be valid. */
3712 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3714 case tcc_exceptional:
3715 if (code == TREE_LIST)
3716 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
3717 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
3718 break;
3720 case tcc_unary:
3721 case tcc_binary:
3722 case tcc_comparison:
3723 case tcc_expression:
3724 switch (code)
3726 case COMPOUND_EXPR:
3727 /* Ignoring the first operand isn't quite right, but works best. */
3728 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
3730 case COND_EXPR:
3731 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3732 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
3733 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
3735 case SAVE_EXPR:
3736 /* The save_expr function never wraps anything containing
3737 a PLACEHOLDER_EXPR. */
3738 return 0;
3740 default:
3741 break;
3744 switch (TREE_CODE_LENGTH (code))
3746 case 1:
3747 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3748 case 2:
3749 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3750 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
3751 default:
3752 return 0;
3755 case tcc_vl_exp:
3756 switch (code)
3758 case CALL_EXPR:
3760 const_tree arg;
3761 const_call_expr_arg_iterator iter;
3762 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
3763 if (CONTAINS_PLACEHOLDER_P (arg))
3764 return 1;
3765 return 0;
3767 default:
3768 return 0;
3771 default:
3772 return 0;
3774 return 0;
3777 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3778 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3779 field positions. */
3781 static bool
3782 type_contains_placeholder_1 (const_tree type)
3784 /* If the size contains a placeholder or the parent type (component type in
3785 the case of arrays) type involves a placeholder, this type does. */
3786 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3787 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3788 || (!POINTER_TYPE_P (type)
3789 && TREE_TYPE (type)
3790 && type_contains_placeholder_p (TREE_TYPE (type))))
3791 return true;
3793 /* Now do type-specific checks. Note that the last part of the check above
3794 greatly limits what we have to do below. */
3795 switch (TREE_CODE (type))
3797 case VOID_TYPE:
3798 case COMPLEX_TYPE:
3799 case ENUMERAL_TYPE:
3800 case BOOLEAN_TYPE:
3801 case POINTER_TYPE:
3802 case OFFSET_TYPE:
3803 case REFERENCE_TYPE:
3804 case METHOD_TYPE:
3805 case FUNCTION_TYPE:
3806 case VECTOR_TYPE:
3807 case NULLPTR_TYPE:
3808 return false;
3810 case INTEGER_TYPE:
3811 case REAL_TYPE:
3812 case FIXED_POINT_TYPE:
3813 /* Here we just check the bounds. */
3814 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3815 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3817 case ARRAY_TYPE:
3818 /* We have already checked the component type above, so just check
3819 the domain type. Flexible array members have a null domain. */
3820 return TYPE_DOMAIN (type) ?
3821 type_contains_placeholder_p (TYPE_DOMAIN (type)) : false;
3823 case RECORD_TYPE:
3824 case UNION_TYPE:
3825 case QUAL_UNION_TYPE:
3827 tree field;
3829 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3830 if (TREE_CODE (field) == FIELD_DECL
3831 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3832 || (TREE_CODE (type) == QUAL_UNION_TYPE
3833 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3834 || type_contains_placeholder_p (TREE_TYPE (field))))
3835 return true;
3837 return false;
3840 default:
3841 gcc_unreachable ();
3845 /* Wrapper around above function used to cache its result. */
3847 bool
3848 type_contains_placeholder_p (tree type)
3850 bool result;
3852 /* If the contains_placeholder_bits field has been initialized,
3853 then we know the answer. */
3854 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3855 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3857 /* Indicate that we've seen this type node, and the answer is false.
3858 This is what we want to return if we run into recursion via fields. */
3859 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3861 /* Compute the real value. */
3862 result = type_contains_placeholder_1 (type);
3864 /* Store the real value. */
3865 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3867 return result;
3870 /* Push tree EXP onto vector QUEUE if it is not already present. */
3872 static void
3873 push_without_duplicates (tree exp, vec<tree> *queue)
3875 unsigned int i;
3876 tree iter;
3878 FOR_EACH_VEC_ELT (*queue, i, iter)
3879 if (simple_cst_equal (iter, exp) == 1)
3880 break;
3882 if (!iter)
3883 queue->safe_push (exp);
3886 /* Given a tree EXP, find all occurrences of references to fields
3887 in a PLACEHOLDER_EXPR and place them in vector REFS without
3888 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3889 we assume here that EXP contains only arithmetic expressions
3890 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3891 argument list. */
3893 void
3894 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3896 enum tree_code code = TREE_CODE (exp);
3897 tree inner;
3898 int i;
3900 /* We handle TREE_LIST and COMPONENT_REF separately. */
3901 if (code == TREE_LIST)
3903 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3904 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3906 else if (code == COMPONENT_REF)
3908 for (inner = TREE_OPERAND (exp, 0);
3909 REFERENCE_CLASS_P (inner);
3910 inner = TREE_OPERAND (inner, 0))
3913 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3914 push_without_duplicates (exp, refs);
3915 else
3916 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3918 else
3919 switch (TREE_CODE_CLASS (code))
3921 case tcc_constant:
3922 break;
3924 case tcc_declaration:
3925 /* Variables allocated to static storage can stay. */
3926 if (!TREE_STATIC (exp))
3927 push_without_duplicates (exp, refs);
3928 break;
3930 case tcc_expression:
3931 /* This is the pattern built in ada/make_aligning_type. */
3932 if (code == ADDR_EXPR
3933 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3935 push_without_duplicates (exp, refs);
3936 break;
3939 /* Fall through. */
3941 case tcc_exceptional:
3942 case tcc_unary:
3943 case tcc_binary:
3944 case tcc_comparison:
3945 case tcc_reference:
3946 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3947 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3948 break;
3950 case tcc_vl_exp:
3951 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3952 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3953 break;
3955 default:
3956 gcc_unreachable ();
3960 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3961 return a tree with all occurrences of references to F in a
3962 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3963 CONST_DECLs. Note that we assume here that EXP contains only
3964 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3965 occurring only in their argument list. */
3967 tree
3968 substitute_in_expr (tree exp, tree f, tree r)
3970 enum tree_code code = TREE_CODE (exp);
3971 tree op0, op1, op2, op3;
3972 tree new_tree;
3974 /* We handle TREE_LIST and COMPONENT_REF separately. */
3975 if (code == TREE_LIST)
3977 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3978 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3979 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3980 return exp;
3982 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3984 else if (code == COMPONENT_REF)
3986 tree inner;
3988 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3989 and it is the right field, replace it with R. */
3990 for (inner = TREE_OPERAND (exp, 0);
3991 REFERENCE_CLASS_P (inner);
3992 inner = TREE_OPERAND (inner, 0))
3995 /* The field. */
3996 op1 = TREE_OPERAND (exp, 1);
3998 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3999 return r;
4001 /* If this expression hasn't been completed let, leave it alone. */
4002 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
4003 return exp;
4005 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4006 if (op0 == TREE_OPERAND (exp, 0))
4007 return exp;
4009 new_tree
4010 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
4012 else
4013 switch (TREE_CODE_CLASS (code))
4015 case tcc_constant:
4016 return exp;
4018 case tcc_declaration:
4019 if (exp == f)
4020 return r;
4021 else
4022 return exp;
4024 case tcc_expression:
4025 if (exp == f)
4026 return r;
4028 /* Fall through. */
4030 case tcc_exceptional:
4031 case tcc_unary:
4032 case tcc_binary:
4033 case tcc_comparison:
4034 case tcc_reference:
4035 switch (TREE_CODE_LENGTH (code))
4037 case 0:
4038 return exp;
4040 case 1:
4041 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4042 if (op0 == TREE_OPERAND (exp, 0))
4043 return exp;
4045 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
4046 break;
4048 case 2:
4049 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4050 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4052 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
4053 return exp;
4055 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
4056 break;
4058 case 3:
4059 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4060 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4061 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
4063 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4064 && op2 == TREE_OPERAND (exp, 2))
4065 return exp;
4067 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
4068 break;
4070 case 4:
4071 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
4072 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
4073 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
4074 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
4076 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4077 && op2 == TREE_OPERAND (exp, 2)
4078 && op3 == TREE_OPERAND (exp, 3))
4079 return exp;
4081 new_tree
4082 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
4083 break;
4085 default:
4086 gcc_unreachable ();
4088 break;
4090 case tcc_vl_exp:
4092 int i;
4094 new_tree = NULL_TREE;
4096 /* If we are trying to replace F with a constant or with another
4097 instance of one of the arguments of the call, inline back
4098 functions which do nothing else than computing a value from
4099 the arguments they are passed. This makes it possible to
4100 fold partially or entirely the replacement expression. */
4101 if (code == CALL_EXPR)
4103 bool maybe_inline = false;
4104 if (CONSTANT_CLASS_P (r))
4105 maybe_inline = true;
4106 else
4107 for (i = 3; i < TREE_OPERAND_LENGTH (exp); i++)
4108 if (operand_equal_p (TREE_OPERAND (exp, i), r, 0))
4110 maybe_inline = true;
4111 break;
4113 if (maybe_inline)
4115 tree t = maybe_inline_call_in_expr (exp);
4116 if (t)
4117 return SUBSTITUTE_IN_EXPR (t, f, r);
4121 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4123 tree op = TREE_OPERAND (exp, i);
4124 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
4125 if (new_op != op)
4127 if (!new_tree)
4128 new_tree = copy_node (exp);
4129 TREE_OPERAND (new_tree, i) = new_op;
4133 if (new_tree)
4135 new_tree = fold (new_tree);
4136 if (TREE_CODE (new_tree) == CALL_EXPR)
4137 process_call_operands (new_tree);
4139 else
4140 return exp;
4142 break;
4144 default:
4145 gcc_unreachable ();
4148 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4150 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4151 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4153 return new_tree;
4156 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
4157 for it within OBJ, a tree that is an object or a chain of references. */
4159 tree
4160 substitute_placeholder_in_expr (tree exp, tree obj)
4162 enum tree_code code = TREE_CODE (exp);
4163 tree op0, op1, op2, op3;
4164 tree new_tree;
4166 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
4167 in the chain of OBJ. */
4168 if (code == PLACEHOLDER_EXPR)
4170 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
4171 tree elt;
4173 for (elt = obj; elt != 0;
4174 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
4175 || TREE_CODE (elt) == COND_EXPR)
4176 ? TREE_OPERAND (elt, 1)
4177 : (REFERENCE_CLASS_P (elt)
4178 || UNARY_CLASS_P (elt)
4179 || BINARY_CLASS_P (elt)
4180 || VL_EXP_CLASS_P (elt)
4181 || EXPRESSION_CLASS_P (elt))
4182 ? TREE_OPERAND (elt, 0) : 0))
4183 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
4184 return elt;
4186 for (elt = obj; elt != 0;
4187 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
4188 || TREE_CODE (elt) == COND_EXPR)
4189 ? TREE_OPERAND (elt, 1)
4190 : (REFERENCE_CLASS_P (elt)
4191 || UNARY_CLASS_P (elt)
4192 || BINARY_CLASS_P (elt)
4193 || VL_EXP_CLASS_P (elt)
4194 || EXPRESSION_CLASS_P (elt))
4195 ? TREE_OPERAND (elt, 0) : 0))
4196 if (POINTER_TYPE_P (TREE_TYPE (elt))
4197 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
4198 == need_type))
4199 return fold_build1 (INDIRECT_REF, need_type, elt);
4201 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
4202 survives until RTL generation, there will be an error. */
4203 return exp;
4206 /* TREE_LIST is special because we need to look at TREE_VALUE
4207 and TREE_CHAIN, not TREE_OPERANDS. */
4208 else if (code == TREE_LIST)
4210 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
4211 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
4212 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
4213 return exp;
4215 return tree_cons (TREE_PURPOSE (exp), op1, op0);
4217 else
4218 switch (TREE_CODE_CLASS (code))
4220 case tcc_constant:
4221 case tcc_declaration:
4222 return exp;
4224 case tcc_exceptional:
4225 case tcc_unary:
4226 case tcc_binary:
4227 case tcc_comparison:
4228 case tcc_expression:
4229 case tcc_reference:
4230 case tcc_statement:
4231 switch (TREE_CODE_LENGTH (code))
4233 case 0:
4234 return exp;
4236 case 1:
4237 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4238 if (op0 == TREE_OPERAND (exp, 0))
4239 return exp;
4241 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
4242 break;
4244 case 2:
4245 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4246 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4248 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
4249 return exp;
4251 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
4252 break;
4254 case 3:
4255 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4256 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4257 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
4259 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4260 && op2 == TREE_OPERAND (exp, 2))
4261 return exp;
4263 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
4264 break;
4266 case 4:
4267 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4268 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4269 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
4270 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
4272 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4273 && op2 == TREE_OPERAND (exp, 2)
4274 && op3 == TREE_OPERAND (exp, 3))
4275 return exp;
4277 new_tree
4278 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
4279 break;
4281 default:
4282 gcc_unreachable ();
4284 break;
4286 case tcc_vl_exp:
4288 int i;
4290 new_tree = NULL_TREE;
4292 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4294 tree op = TREE_OPERAND (exp, i);
4295 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
4296 if (new_op != op)
4298 if (!new_tree)
4299 new_tree = copy_node (exp);
4300 TREE_OPERAND (new_tree, i) = new_op;
4304 if (new_tree)
4306 new_tree = fold (new_tree);
4307 if (TREE_CODE (new_tree) == CALL_EXPR)
4308 process_call_operands (new_tree);
4310 else
4311 return exp;
4313 break;
4315 default:
4316 gcc_unreachable ();
4319 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4321 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4322 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4324 return new_tree;
4328 /* Subroutine of stabilize_reference; this is called for subtrees of
4329 references. Any expression with side-effects must be put in a SAVE_EXPR
4330 to ensure that it is only evaluated once.
4332 We don't put SAVE_EXPR nodes around everything, because assigning very
4333 simple expressions to temporaries causes us to miss good opportunities
4334 for optimizations. Among other things, the opportunity to fold in the
4335 addition of a constant into an addressing mode often gets lost, e.g.
4336 "y[i+1] += x;". In general, we take the approach that we should not make
4337 an assignment unless we are forced into it - i.e., that any non-side effect
4338 operator should be allowed, and that cse should take care of coalescing
4339 multiple utterances of the same expression should that prove fruitful. */
4341 static tree
4342 stabilize_reference_1 (tree e)
4344 tree result;
4345 enum tree_code code = TREE_CODE (e);
4347 /* We cannot ignore const expressions because it might be a reference
4348 to a const array but whose index contains side-effects. But we can
4349 ignore things that are actual constant or that already have been
4350 handled by this function. */
4352 if (tree_invariant_p (e))
4353 return e;
4355 switch (TREE_CODE_CLASS (code))
4357 case tcc_exceptional:
4358 /* Always wrap STATEMENT_LIST into SAVE_EXPR, even if it doesn't
4359 have side-effects. */
4360 if (code == STATEMENT_LIST)
4361 return save_expr (e);
4362 /* FALLTHRU */
4363 case tcc_type:
4364 case tcc_declaration:
4365 case tcc_comparison:
4366 case tcc_statement:
4367 case tcc_expression:
4368 case tcc_reference:
4369 case tcc_vl_exp:
4370 /* If the expression has side-effects, then encase it in a SAVE_EXPR
4371 so that it will only be evaluated once. */
4372 /* The reference (r) and comparison (<) classes could be handled as
4373 below, but it is generally faster to only evaluate them once. */
4374 if (TREE_SIDE_EFFECTS (e))
4375 return save_expr (e);
4376 return e;
4378 case tcc_constant:
4379 /* Constants need no processing. In fact, we should never reach
4380 here. */
4381 return e;
4383 case tcc_binary:
4384 /* Division is slow and tends to be compiled with jumps,
4385 especially the division by powers of 2 that is often
4386 found inside of an array reference. So do it just once. */
4387 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
4388 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
4389 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
4390 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
4391 return save_expr (e);
4392 /* Recursively stabilize each operand. */
4393 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
4394 stabilize_reference_1 (TREE_OPERAND (e, 1)));
4395 break;
4397 case tcc_unary:
4398 /* Recursively stabilize each operand. */
4399 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
4400 break;
4402 default:
4403 gcc_unreachable ();
4406 TREE_TYPE (result) = TREE_TYPE (e);
4407 TREE_READONLY (result) = TREE_READONLY (e);
4408 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
4409 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
4411 return result;
4414 /* Stabilize a reference so that we can use it any number of times
4415 without causing its operands to be evaluated more than once.
4416 Returns the stabilized reference. This works by means of save_expr,
4417 so see the caveats in the comments about save_expr.
4419 Also allows conversion expressions whose operands are references.
4420 Any other kind of expression is returned unchanged. */
4422 tree
4423 stabilize_reference (tree ref)
4425 tree result;
4426 enum tree_code code = TREE_CODE (ref);
4428 switch (code)
4430 case VAR_DECL:
4431 case PARM_DECL:
4432 case RESULT_DECL:
4433 /* No action is needed in this case. */
4434 return ref;
4436 CASE_CONVERT:
4437 case FLOAT_EXPR:
4438 case FIX_TRUNC_EXPR:
4439 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
4440 break;
4442 case INDIRECT_REF:
4443 result = build_nt (INDIRECT_REF,
4444 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
4445 break;
4447 case COMPONENT_REF:
4448 result = build_nt (COMPONENT_REF,
4449 stabilize_reference (TREE_OPERAND (ref, 0)),
4450 TREE_OPERAND (ref, 1), NULL_TREE);
4451 break;
4453 case BIT_FIELD_REF:
4454 result = build_nt (BIT_FIELD_REF,
4455 stabilize_reference (TREE_OPERAND (ref, 0)),
4456 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
4457 REF_REVERSE_STORAGE_ORDER (result) = REF_REVERSE_STORAGE_ORDER (ref);
4458 break;
4460 case ARRAY_REF:
4461 result = build_nt (ARRAY_REF,
4462 stabilize_reference (TREE_OPERAND (ref, 0)),
4463 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4464 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4465 break;
4467 case ARRAY_RANGE_REF:
4468 result = build_nt (ARRAY_RANGE_REF,
4469 stabilize_reference (TREE_OPERAND (ref, 0)),
4470 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4471 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4472 break;
4474 case COMPOUND_EXPR:
4475 /* We cannot wrap the first expression in a SAVE_EXPR, as then
4476 it wouldn't be ignored. This matters when dealing with
4477 volatiles. */
4478 return stabilize_reference_1 (ref);
4480 /* If arg isn't a kind of lvalue we recognize, make no change.
4481 Caller should recognize the error for an invalid lvalue. */
4482 default:
4483 return ref;
4485 case ERROR_MARK:
4486 return error_mark_node;
4489 TREE_TYPE (result) = TREE_TYPE (ref);
4490 TREE_READONLY (result) = TREE_READONLY (ref);
4491 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
4492 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
4494 return result;
4497 /* Low-level constructors for expressions. */
4499 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
4500 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
4502 void
4503 recompute_tree_invariant_for_addr_expr (tree t)
4505 tree node;
4506 bool tc = true, se = false;
4508 gcc_assert (TREE_CODE (t) == ADDR_EXPR);
4510 /* We started out assuming this address is both invariant and constant, but
4511 does not have side effects. Now go down any handled components and see if
4512 any of them involve offsets that are either non-constant or non-invariant.
4513 Also check for side-effects.
4515 ??? Note that this code makes no attempt to deal with the case where
4516 taking the address of something causes a copy due to misalignment. */
4518 #define UPDATE_FLAGS(NODE) \
4519 do { tree _node = (NODE); \
4520 if (_node && !TREE_CONSTANT (_node)) tc = false; \
4521 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
4523 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
4524 node = TREE_OPERAND (node, 0))
4526 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
4527 array reference (probably made temporarily by the G++ front end),
4528 so ignore all the operands. */
4529 if ((TREE_CODE (node) == ARRAY_REF
4530 || TREE_CODE (node) == ARRAY_RANGE_REF)
4531 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
4533 UPDATE_FLAGS (TREE_OPERAND (node, 1));
4534 if (TREE_OPERAND (node, 2))
4535 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4536 if (TREE_OPERAND (node, 3))
4537 UPDATE_FLAGS (TREE_OPERAND (node, 3));
4539 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
4540 FIELD_DECL, apparently. The G++ front end can put something else
4541 there, at least temporarily. */
4542 else if (TREE_CODE (node) == COMPONENT_REF
4543 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
4545 if (TREE_OPERAND (node, 2))
4546 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4550 node = lang_hooks.expr_to_decl (node, &tc, &se);
4552 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4553 the address, since &(*a)->b is a form of addition. If it's a constant, the
4554 address is constant too. If it's a decl, its address is constant if the
4555 decl is static. Everything else is not constant and, furthermore,
4556 taking the address of a volatile variable is not volatile. */
4557 if (TREE_CODE (node) == INDIRECT_REF
4558 || TREE_CODE (node) == MEM_REF)
4559 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4560 else if (CONSTANT_CLASS_P (node))
4562 else if (DECL_P (node))
4563 tc &= (staticp (node) != NULL_TREE);
4564 else
4566 tc = false;
4567 se |= TREE_SIDE_EFFECTS (node);
4571 TREE_CONSTANT (t) = tc;
4572 TREE_SIDE_EFFECTS (t) = se;
4573 #undef UPDATE_FLAGS
4576 /* Build an expression of code CODE, data type TYPE, and operands as
4577 specified. Expressions and reference nodes can be created this way.
4578 Constants, decls, types and misc nodes cannot be.
4580 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4581 enough for all extant tree codes. */
4583 tree
4584 build0 (enum tree_code code, tree tt MEM_STAT_DECL)
4586 tree t;
4588 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4590 t = make_node (code PASS_MEM_STAT);
4591 TREE_TYPE (t) = tt;
4593 return t;
4596 tree
4597 build1 (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4599 int length = sizeof (struct tree_exp);
4600 tree t;
4602 record_node_allocation_statistics (code, length);
4604 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4606 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4608 memset (t, 0, sizeof (struct tree_common));
4610 TREE_SET_CODE (t, code);
4612 TREE_TYPE (t) = type;
4613 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4614 TREE_OPERAND (t, 0) = node;
4615 if (node && !TYPE_P (node))
4617 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4618 TREE_READONLY (t) = TREE_READONLY (node);
4621 if (TREE_CODE_CLASS (code) == tcc_statement)
4623 if (code != DEBUG_BEGIN_STMT)
4624 TREE_SIDE_EFFECTS (t) = 1;
4626 else switch (code)
4628 case VA_ARG_EXPR:
4629 /* All of these have side-effects, no matter what their
4630 operands are. */
4631 TREE_SIDE_EFFECTS (t) = 1;
4632 TREE_READONLY (t) = 0;
4633 break;
4635 case INDIRECT_REF:
4636 /* Whether a dereference is readonly has nothing to do with whether
4637 its operand is readonly. */
4638 TREE_READONLY (t) = 0;
4639 break;
4641 case ADDR_EXPR:
4642 if (node)
4643 recompute_tree_invariant_for_addr_expr (t);
4644 break;
4646 default:
4647 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4648 && node && !TYPE_P (node)
4649 && TREE_CONSTANT (node))
4650 TREE_CONSTANT (t) = 1;
4651 if (TREE_CODE_CLASS (code) == tcc_reference
4652 && node && TREE_THIS_VOLATILE (node))
4653 TREE_THIS_VOLATILE (t) = 1;
4654 break;
4657 return t;
4660 #define PROCESS_ARG(N) \
4661 do { \
4662 TREE_OPERAND (t, N) = arg##N; \
4663 if (arg##N &&!TYPE_P (arg##N)) \
4665 if (TREE_SIDE_EFFECTS (arg##N)) \
4666 side_effects = 1; \
4667 if (!TREE_READONLY (arg##N) \
4668 && !CONSTANT_CLASS_P (arg##N)) \
4669 (void) (read_only = 0); \
4670 if (!TREE_CONSTANT (arg##N)) \
4671 (void) (constant = 0); \
4673 } while (0)
4675 tree
4676 build2 (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
4678 bool constant, read_only, side_effects, div_by_zero;
4679 tree t;
4681 gcc_assert (TREE_CODE_LENGTH (code) == 2);
4683 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
4684 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
4685 /* When sizetype precision doesn't match that of pointers
4686 we need to be able to build explicit extensions or truncations
4687 of the offset argument. */
4688 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
4689 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
4690 && TREE_CODE (arg1) == INTEGER_CST);
4692 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
4693 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
4694 && ptrofftype_p (TREE_TYPE (arg1)));
4696 t = make_node (code PASS_MEM_STAT);
4697 TREE_TYPE (t) = tt;
4699 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
4700 result based on those same flags for the arguments. But if the
4701 arguments aren't really even `tree' expressions, we shouldn't be trying
4702 to do this. */
4704 /* Expressions without side effects may be constant if their
4705 arguments are as well. */
4706 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4707 || TREE_CODE_CLASS (code) == tcc_binary);
4708 read_only = 1;
4709 side_effects = TREE_SIDE_EFFECTS (t);
4711 switch (code)
4713 case TRUNC_DIV_EXPR:
4714 case CEIL_DIV_EXPR:
4715 case FLOOR_DIV_EXPR:
4716 case ROUND_DIV_EXPR:
4717 case EXACT_DIV_EXPR:
4718 case CEIL_MOD_EXPR:
4719 case FLOOR_MOD_EXPR:
4720 case ROUND_MOD_EXPR:
4721 case TRUNC_MOD_EXPR:
4722 div_by_zero = integer_zerop (arg1);
4723 break;
4724 default:
4725 div_by_zero = false;
4728 PROCESS_ARG (0);
4729 PROCESS_ARG (1);
4731 TREE_SIDE_EFFECTS (t) = side_effects;
4732 if (code == MEM_REF)
4734 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
4736 tree o = TREE_OPERAND (arg0, 0);
4737 TREE_READONLY (t) = TREE_READONLY (o);
4738 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
4741 else
4743 TREE_READONLY (t) = read_only;
4744 /* Don't mark X / 0 as constant. */
4745 TREE_CONSTANT (t) = constant && !div_by_zero;
4746 TREE_THIS_VOLATILE (t)
4747 = (TREE_CODE_CLASS (code) == tcc_reference
4748 && arg0 && TREE_THIS_VOLATILE (arg0));
4751 return t;
4755 tree
4756 build3 (enum tree_code code, tree tt, tree arg0, tree arg1,
4757 tree arg2 MEM_STAT_DECL)
4759 bool constant, read_only, side_effects;
4760 tree t;
4762 gcc_assert (TREE_CODE_LENGTH (code) == 3);
4763 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4765 t = make_node (code PASS_MEM_STAT);
4766 TREE_TYPE (t) = tt;
4768 read_only = 1;
4770 /* As a special exception, if COND_EXPR has NULL branches, we
4771 assume that it is a gimple statement and always consider
4772 it to have side effects. */
4773 if (code == COND_EXPR
4774 && tt == void_type_node
4775 && arg1 == NULL_TREE
4776 && arg2 == NULL_TREE)
4777 side_effects = true;
4778 else
4779 side_effects = TREE_SIDE_EFFECTS (t);
4781 PROCESS_ARG (0);
4782 PROCESS_ARG (1);
4783 PROCESS_ARG (2);
4785 if (code == COND_EXPR)
4786 TREE_READONLY (t) = read_only;
4788 TREE_SIDE_EFFECTS (t) = side_effects;
4789 TREE_THIS_VOLATILE (t)
4790 = (TREE_CODE_CLASS (code) == tcc_reference
4791 && arg0 && TREE_THIS_VOLATILE (arg0));
4793 return t;
4796 tree
4797 build4 (enum tree_code code, tree tt, tree arg0, tree arg1,
4798 tree arg2, tree arg3 MEM_STAT_DECL)
4800 bool constant, read_only, side_effects;
4801 tree t;
4803 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4805 t = make_node (code PASS_MEM_STAT);
4806 TREE_TYPE (t) = tt;
4808 side_effects = TREE_SIDE_EFFECTS (t);
4810 PROCESS_ARG (0);
4811 PROCESS_ARG (1);
4812 PROCESS_ARG (2);
4813 PROCESS_ARG (3);
4815 TREE_SIDE_EFFECTS (t) = side_effects;
4816 TREE_THIS_VOLATILE (t)
4817 = (TREE_CODE_CLASS (code) == tcc_reference
4818 && arg0 && TREE_THIS_VOLATILE (arg0));
4820 return t;
4823 tree
4824 build5 (enum tree_code code, tree tt, tree arg0, tree arg1,
4825 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4827 bool constant, read_only, side_effects;
4828 tree t;
4830 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4832 t = make_node (code PASS_MEM_STAT);
4833 TREE_TYPE (t) = tt;
4835 side_effects = TREE_SIDE_EFFECTS (t);
4837 PROCESS_ARG (0);
4838 PROCESS_ARG (1);
4839 PROCESS_ARG (2);
4840 PROCESS_ARG (3);
4841 PROCESS_ARG (4);
4843 TREE_SIDE_EFFECTS (t) = side_effects;
4844 if (code == TARGET_MEM_REF)
4846 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
4848 tree o = TREE_OPERAND (arg0, 0);
4849 TREE_READONLY (t) = TREE_READONLY (o);
4850 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
4853 else
4854 TREE_THIS_VOLATILE (t)
4855 = (TREE_CODE_CLASS (code) == tcc_reference
4856 && arg0 && TREE_THIS_VOLATILE (arg0));
4858 return t;
4861 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4862 on the pointer PTR. */
4864 tree
4865 build_simple_mem_ref_loc (location_t loc, tree ptr)
4867 poly_int64 offset = 0;
4868 tree ptype = TREE_TYPE (ptr);
4869 tree tem;
4870 /* For convenience allow addresses that collapse to a simple base
4871 and offset. */
4872 if (TREE_CODE (ptr) == ADDR_EXPR
4873 && (handled_component_p (TREE_OPERAND (ptr, 0))
4874 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4876 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4877 gcc_assert (ptr);
4878 if (TREE_CODE (ptr) == MEM_REF)
4880 offset += mem_ref_offset (ptr).force_shwi ();
4881 ptr = TREE_OPERAND (ptr, 0);
4883 else
4884 ptr = build_fold_addr_expr (ptr);
4885 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4887 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4888 ptr, build_int_cst (ptype, offset));
4889 SET_EXPR_LOCATION (tem, loc);
4890 return tem;
4893 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4895 poly_offset_int
4896 mem_ref_offset (const_tree t)
4898 return poly_offset_int::from (wi::to_poly_wide (TREE_OPERAND (t, 1)),
4899 SIGNED);
4902 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4903 offsetted by OFFSET units. */
4905 tree
4906 build_invariant_address (tree type, tree base, poly_int64 offset)
4908 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4909 build_fold_addr_expr (base),
4910 build_int_cst (ptr_type_node, offset));
4911 tree addr = build1 (ADDR_EXPR, type, ref);
4912 recompute_tree_invariant_for_addr_expr (addr);
4913 return addr;
4916 /* Similar except don't specify the TREE_TYPE
4917 and leave the TREE_SIDE_EFFECTS as 0.
4918 It is permissible for arguments to be null,
4919 or even garbage if their values do not matter. */
4921 tree
4922 build_nt (enum tree_code code, ...)
4924 tree t;
4925 int length;
4926 int i;
4927 va_list p;
4929 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4931 va_start (p, code);
4933 t = make_node (code);
4934 length = TREE_CODE_LENGTH (code);
4936 for (i = 0; i < length; i++)
4937 TREE_OPERAND (t, i) = va_arg (p, tree);
4939 va_end (p);
4940 return t;
4943 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4944 tree vec. */
4946 tree
4947 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4949 tree ret, t;
4950 unsigned int ix;
4952 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4953 CALL_EXPR_FN (ret) = fn;
4954 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4955 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4956 CALL_EXPR_ARG (ret, ix) = t;
4957 return ret;
4960 /* Create a DECL_... node of code CODE, name NAME (if non-null)
4961 and data type TYPE.
4962 We do NOT enter this node in any sort of symbol table.
4964 LOC is the location of the decl.
4966 layout_decl is used to set up the decl's storage layout.
4967 Other slots are initialized to 0 or null pointers. */
4969 tree
4970 build_decl (location_t loc, enum tree_code code, tree name,
4971 tree type MEM_STAT_DECL)
4973 tree t;
4975 t = make_node (code PASS_MEM_STAT);
4976 DECL_SOURCE_LOCATION (t) = loc;
4978 /* if (type == error_mark_node)
4979 type = integer_type_node; */
4980 /* That is not done, deliberately, so that having error_mark_node
4981 as the type can suppress useless errors in the use of this variable. */
4983 DECL_NAME (t) = name;
4984 TREE_TYPE (t) = type;
4986 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4987 layout_decl (t, 0);
4989 return t;
4992 /* Builds and returns function declaration with NAME and TYPE. */
4994 tree
4995 build_fn_decl (const char *name, tree type)
4997 tree id = get_identifier (name);
4998 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
5000 DECL_EXTERNAL (decl) = 1;
5001 TREE_PUBLIC (decl) = 1;
5002 DECL_ARTIFICIAL (decl) = 1;
5003 TREE_NOTHROW (decl) = 1;
5005 return decl;
5008 vec<tree, va_gc> *all_translation_units;
5010 /* Builds a new translation-unit decl with name NAME, queues it in the
5011 global list of translation-unit decls and returns it. */
5013 tree
5014 build_translation_unit_decl (tree name)
5016 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
5017 name, NULL_TREE);
5018 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
5019 vec_safe_push (all_translation_units, tu);
5020 return tu;
5024 /* BLOCK nodes are used to represent the structure of binding contours
5025 and declarations, once those contours have been exited and their contents
5026 compiled. This information is used for outputting debugging info. */
5028 tree
5029 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
5031 tree block = make_node (BLOCK);
5033 BLOCK_VARS (block) = vars;
5034 BLOCK_SUBBLOCKS (block) = subblocks;
5035 BLOCK_SUPERCONTEXT (block) = supercontext;
5036 BLOCK_CHAIN (block) = chain;
5037 return block;
5041 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
5043 LOC is the location to use in tree T. */
5045 void
5046 protected_set_expr_location (tree t, location_t loc)
5048 if (CAN_HAVE_LOCATION_P (t))
5049 SET_EXPR_LOCATION (t, loc);
5052 /* Data used when collecting DECLs and TYPEs for language data removal. */
5054 struct free_lang_data_d
5056 free_lang_data_d () : decls (100), types (100) {}
5058 /* Worklist to avoid excessive recursion. */
5059 auto_vec<tree> worklist;
5061 /* Set of traversed objects. Used to avoid duplicate visits. */
5062 hash_set<tree> pset;
5064 /* Array of symbols to process with free_lang_data_in_decl. */
5065 auto_vec<tree> decls;
5067 /* Array of types to process with free_lang_data_in_type. */
5068 auto_vec<tree> types;
5072 /* Add type or decl T to one of the list of tree nodes that need their
5073 language data removed. The lists are held inside FLD. */
5075 static void
5076 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
5078 if (DECL_P (t))
5079 fld->decls.safe_push (t);
5080 else if (TYPE_P (t))
5081 fld->types.safe_push (t);
5082 else
5083 gcc_unreachable ();
5086 /* Push tree node T into FLD->WORKLIST. */
5088 static inline void
5089 fld_worklist_push (tree t, struct free_lang_data_d *fld)
5091 if (t && !is_lang_specific (t) && !fld->pset.contains (t))
5092 fld->worklist.safe_push ((t));
5097 /* Return simplified TYPE_NAME of TYPE. */
5099 static tree
5100 fld_simplified_type_name (tree type)
5102 if (!TYPE_NAME (type) || TREE_CODE (TYPE_NAME (type)) != TYPE_DECL)
5103 return TYPE_NAME (type);
5104 /* Drop TYPE_DECLs in TYPE_NAME in favor of the identifier in the
5105 TYPE_DECL if the type doesn't have linkage.
5106 this must match fld_ */
5107 if (type != TYPE_MAIN_VARIANT (type) || ! type_with_linkage_p (type))
5108 return DECL_NAME (TYPE_NAME (type));
5109 return TYPE_NAME (type);
5112 /* Do same comparsion as check_qualified_type skipping lang part of type
5113 and be more permissive about type names: we only care that names are
5114 same (for diagnostics) and that ODR names are the same.
5115 If INNER_TYPE is non-NULL, be sure that TREE_TYPE match it. */
5117 static bool
5118 fld_type_variant_equal_p (tree t, tree v, tree inner_type)
5120 if (TYPE_QUALS (t) != TYPE_QUALS (v)
5121 /* We want to match incomplete variants with complete types.
5122 In this case we need to ignore alignment. */
5123 || ((!RECORD_OR_UNION_TYPE_P (t) || COMPLETE_TYPE_P (v))
5124 && (TYPE_ALIGN (t) != TYPE_ALIGN (v)
5125 || TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (v)))
5126 || fld_simplified_type_name (t) != fld_simplified_type_name (v)
5127 || !attribute_list_equal (TYPE_ATTRIBUTES (t),
5128 TYPE_ATTRIBUTES (v))
5129 || (inner_type && TREE_TYPE (v) != inner_type))
5130 return false;
5132 return true;
5135 /* Find variant of FIRST that match T and create new one if necessary.
5136 Set TREE_TYPE to INNER_TYPE if non-NULL. */
5138 static tree
5139 fld_type_variant (tree first, tree t, struct free_lang_data_d *fld,
5140 tree inner_type = NULL)
5142 if (first == TYPE_MAIN_VARIANT (t))
5143 return t;
5144 for (tree v = first; v; v = TYPE_NEXT_VARIANT (v))
5145 if (fld_type_variant_equal_p (t, v, inner_type))
5146 return v;
5147 tree v = build_variant_type_copy (first);
5148 TYPE_READONLY (v) = TYPE_READONLY (t);
5149 TYPE_VOLATILE (v) = TYPE_VOLATILE (t);
5150 TYPE_ATOMIC (v) = TYPE_ATOMIC (t);
5151 TYPE_RESTRICT (v) = TYPE_RESTRICT (t);
5152 TYPE_ADDR_SPACE (v) = TYPE_ADDR_SPACE (t);
5153 TYPE_NAME (v) = TYPE_NAME (t);
5154 TYPE_ATTRIBUTES (v) = TYPE_ATTRIBUTES (t);
5155 TYPE_CANONICAL (v) = TYPE_CANONICAL (t);
5156 /* Variants of incomplete types should have alignment
5157 set to BITS_PER_UNIT. Do not copy the actual alignment. */
5158 if (!RECORD_OR_UNION_TYPE_P (v) || COMPLETE_TYPE_P (v))
5160 SET_TYPE_ALIGN (v, TYPE_ALIGN (t));
5161 TYPE_USER_ALIGN (v) = TYPE_USER_ALIGN (t);
5163 if (inner_type)
5164 TREE_TYPE (v) = inner_type;
5165 gcc_checking_assert (fld_type_variant_equal_p (t,v, inner_type));
5166 add_tree_to_fld_list (v, fld);
5167 return v;
5170 /* Map complete types to incomplete types. */
5172 static hash_map<tree, tree> *fld_incomplete_types;
5174 /* Map types to simplified types. */
5176 static hash_map<tree, tree> *fld_simplified_types;
5178 /* Produce variant of T whose TREE_TYPE is T2. If it is main variant,
5179 use MAP to prevent duplicates. */
5181 static tree
5182 fld_process_array_type (tree t, tree t2, hash_map<tree, tree> *map,
5183 struct free_lang_data_d *fld)
5185 if (TREE_TYPE (t) == t2)
5186 return t;
5188 if (TYPE_MAIN_VARIANT (t) != t)
5190 return fld_type_variant
5191 (fld_process_array_type (TYPE_MAIN_VARIANT (t),
5192 TYPE_MAIN_VARIANT (t2), map, fld),
5193 t, fld, t2);
5196 bool existed;
5197 tree &array
5198 = map->get_or_insert (t, &existed);
5199 if (!existed)
5201 array = build_array_type_1 (t2, TYPE_DOMAIN (t),
5202 TYPE_TYPELESS_STORAGE (t), false);
5203 TYPE_CANONICAL (array) = TYPE_CANONICAL (t);
5204 add_tree_to_fld_list (array, fld);
5206 return array;
5209 /* Return CTX after removal of contexts that are not relevant */
5211 static tree
5212 fld_decl_context (tree ctx)
5214 /* Variably modified types are needed for tree_is_indexable to decide
5215 whether the type needs to go to local or global section.
5216 This code is semi-broken but for now it is easiest to keep contexts
5217 as expected. */
5218 if (ctx && TYPE_P (ctx)
5219 && !variably_modified_type_p (ctx, NULL_TREE))
5221 while (ctx && TYPE_P (ctx))
5222 ctx = TYPE_CONTEXT (ctx);
5224 return ctx;
5227 /* For T being aggregate type try to turn it into a incomplete variant.
5228 Return T if no simplification is possible. */
5230 static tree
5231 fld_incomplete_type_of (tree t, struct free_lang_data_d *fld)
5233 if (!t)
5234 return NULL;
5235 if (POINTER_TYPE_P (t))
5237 tree t2 = fld_incomplete_type_of (TREE_TYPE (t), fld);
5238 if (t2 != TREE_TYPE (t))
5240 tree first;
5241 if (TREE_CODE (t) == POINTER_TYPE)
5242 first = build_pointer_type_for_mode (t2, TYPE_MODE (t),
5243 TYPE_REF_CAN_ALIAS_ALL (t));
5244 else
5245 first = build_reference_type_for_mode (t2, TYPE_MODE (t),
5246 TYPE_REF_CAN_ALIAS_ALL (t));
5247 gcc_assert (TYPE_CANONICAL (t2) != t2
5248 && TYPE_CANONICAL (t2) == TYPE_CANONICAL (TREE_TYPE (t)));
5249 add_tree_to_fld_list (first, fld);
5250 return fld_type_variant (first, t, fld);
5252 return t;
5254 if (TREE_CODE (t) == ARRAY_TYPE)
5255 return fld_process_array_type (t,
5256 fld_incomplete_type_of (TREE_TYPE (t), fld),
5257 fld_incomplete_types, fld);
5258 if ((!RECORD_OR_UNION_TYPE_P (t) && TREE_CODE (t) != ENUMERAL_TYPE)
5259 || !COMPLETE_TYPE_P (t))
5260 return t;
5261 if (TYPE_MAIN_VARIANT (t) == t)
5263 bool existed;
5264 tree &copy
5265 = fld_incomplete_types->get_or_insert (t, &existed);
5267 if (!existed)
5269 copy = build_distinct_type_copy (t);
5271 /* It is possible that type was not seen by free_lang_data yet. */
5272 add_tree_to_fld_list (copy, fld);
5273 TYPE_SIZE (copy) = NULL;
5274 TYPE_USER_ALIGN (copy) = 0;
5275 TYPE_SIZE_UNIT (copy) = NULL;
5276 TYPE_CANONICAL (copy) = TYPE_CANONICAL (t);
5277 TREE_ADDRESSABLE (copy) = 0;
5278 if (AGGREGATE_TYPE_P (t))
5280 SET_TYPE_MODE (copy, VOIDmode);
5281 SET_TYPE_ALIGN (copy, BITS_PER_UNIT);
5282 TYPE_TYPELESS_STORAGE (copy) = 0;
5283 TYPE_FIELDS (copy) = NULL;
5284 TYPE_BINFO (copy) = NULL;
5286 else
5287 TYPE_VALUES (copy) = NULL;
5289 /* Build copy of TYPE_DECL in TYPE_NAME if necessary.
5290 This is needed for ODR violation warnings to come out right (we
5291 want duplicate TYPE_DECLs whenever the type is duplicated because
5292 of ODR violation. Because lang data in the TYPE_DECL may not
5293 have been freed yet, rebuild it from scratch and copy relevant
5294 fields. */
5295 TYPE_NAME (copy) = fld_simplified_type_name (copy);
5296 tree name = TYPE_NAME (copy);
5298 if (name && TREE_CODE (name) == TYPE_DECL)
5300 gcc_checking_assert (TREE_TYPE (name) == t);
5301 tree name2 = build_decl (DECL_SOURCE_LOCATION (name), TYPE_DECL,
5302 DECL_NAME (name), copy);
5303 if (DECL_ASSEMBLER_NAME_SET_P (name))
5304 SET_DECL_ASSEMBLER_NAME (name2, DECL_ASSEMBLER_NAME (name));
5305 SET_DECL_ALIGN (name2, 0);
5306 DECL_CONTEXT (name2) = fld_decl_context
5307 (DECL_CONTEXT (name));
5308 TYPE_NAME (copy) = name2;
5311 return copy;
5313 return (fld_type_variant
5314 (fld_incomplete_type_of (TYPE_MAIN_VARIANT (t), fld), t, fld));
5317 /* Simplify type T for scenarios where we do not need complete pointer
5318 types. */
5320 static tree
5321 fld_simplified_type (tree t, struct free_lang_data_d *fld)
5323 if (!t)
5324 return t;
5325 if (POINTER_TYPE_P (t))
5326 return fld_incomplete_type_of (t, fld);
5327 if (TREE_CODE (t) == ARRAY_TYPE)
5328 return fld_process_array_type (t, fld_simplified_type (TREE_TYPE (t), fld),
5329 fld_simplified_types, fld);
5330 return t;
5333 /* Reset the expression *EXPR_P, a size or position.
5335 ??? We could reset all non-constant sizes or positions. But it's cheap
5336 enough to not do so and refrain from adding workarounds to dwarf2out.c.
5338 We need to reset self-referential sizes or positions because they cannot
5339 be gimplified and thus can contain a CALL_EXPR after the gimplification
5340 is finished, which will run afoul of LTO streaming. And they need to be
5341 reset to something essentially dummy but not constant, so as to preserve
5342 the properties of the object they are attached to. */
5344 static inline void
5345 free_lang_data_in_one_sizepos (tree *expr_p)
5347 tree expr = *expr_p;
5348 if (CONTAINS_PLACEHOLDER_P (expr))
5349 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
5353 /* Reset all the fields in a binfo node BINFO. We only keep
5354 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
5356 static void
5357 free_lang_data_in_binfo (tree binfo)
5359 unsigned i;
5360 tree t;
5362 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
5364 BINFO_VIRTUALS (binfo) = NULL_TREE;
5365 BINFO_BASE_ACCESSES (binfo) = NULL;
5366 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
5367 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
5368 BINFO_VPTR_FIELD (binfo) = NULL_TREE;
5370 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
5371 free_lang_data_in_binfo (t);
5375 /* Reset all language specific information still present in TYPE. */
5377 static void
5378 free_lang_data_in_type (tree type, struct free_lang_data_d *fld)
5380 gcc_assert (TYPE_P (type));
5382 /* Give the FE a chance to remove its own data first. */
5383 lang_hooks.free_lang_data (type);
5385 TREE_LANG_FLAG_0 (type) = 0;
5386 TREE_LANG_FLAG_1 (type) = 0;
5387 TREE_LANG_FLAG_2 (type) = 0;
5388 TREE_LANG_FLAG_3 (type) = 0;
5389 TREE_LANG_FLAG_4 (type) = 0;
5390 TREE_LANG_FLAG_5 (type) = 0;
5391 TREE_LANG_FLAG_6 (type) = 0;
5393 TYPE_NEEDS_CONSTRUCTING (type) = 0;
5395 if (TREE_CODE (type) == FUNCTION_TYPE)
5397 TREE_TYPE (type) = fld_simplified_type (TREE_TYPE (type), fld);
5398 /* Remove the const and volatile qualifiers from arguments. The
5399 C++ front end removes them, but the C front end does not,
5400 leading to false ODR violation errors when merging two
5401 instances of the same function signature compiled by
5402 different front ends. */
5403 for (tree p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
5405 TREE_VALUE (p) = fld_simplified_type (TREE_VALUE (p), fld);
5406 tree arg_type = TREE_VALUE (p);
5408 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
5410 int quals = TYPE_QUALS (arg_type)
5411 & ~TYPE_QUAL_CONST
5412 & ~TYPE_QUAL_VOLATILE;
5413 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
5414 free_lang_data_in_type (TREE_VALUE (p), fld);
5416 /* C++ FE uses TREE_PURPOSE to store initial values. */
5417 TREE_PURPOSE (p) = NULL;
5420 else if (TREE_CODE (type) == METHOD_TYPE)
5422 TREE_TYPE (type) = fld_simplified_type (TREE_TYPE (type), fld);
5423 for (tree p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
5425 /* C++ FE uses TREE_PURPOSE to store initial values. */
5426 TREE_VALUE (p) = fld_simplified_type (TREE_VALUE (p), fld);
5427 TREE_PURPOSE (p) = NULL;
5430 else if (RECORD_OR_UNION_TYPE_P (type))
5432 /* Remove members that are not FIELD_DECLs from the field list
5433 of an aggregate. These occur in C++. */
5434 for (tree *prev = &TYPE_FIELDS (type), member; (member = *prev);)
5435 if (TREE_CODE (member) == FIELD_DECL)
5436 prev = &DECL_CHAIN (member);
5437 else
5438 *prev = DECL_CHAIN (member);
5440 TYPE_VFIELD (type) = NULL_TREE;
5442 if (TYPE_BINFO (type))
5444 free_lang_data_in_binfo (TYPE_BINFO (type));
5445 /* We need to preserve link to bases and virtual table for all
5446 polymorphic types to make devirtualization machinery working. */
5447 if (!BINFO_VTABLE (TYPE_BINFO (type))
5448 || !flag_devirtualize)
5449 TYPE_BINFO (type) = NULL;
5452 else if (INTEGRAL_TYPE_P (type)
5453 || SCALAR_FLOAT_TYPE_P (type)
5454 || FIXED_POINT_TYPE_P (type))
5456 if (TREE_CODE (type) == ENUMERAL_TYPE)
5458 /* Type values are used only for C++ ODR checking. Drop them
5459 for all type variants and non-ODR types.
5460 For ODR types the data is freed in free_odr_warning_data. */
5461 if (TYPE_MAIN_VARIANT (type) != type
5462 || !type_with_linkage_p (type))
5463 TYPE_VALUES (type) = NULL;
5464 else
5465 /* Simplify representation by recording only values rather
5466 than const decls. */
5467 for (tree e = TYPE_VALUES (type); e; e = TREE_CHAIN (e))
5468 if (TREE_CODE (TREE_VALUE (e)) == CONST_DECL)
5469 TREE_VALUE (e) = DECL_INITIAL (TREE_VALUE (e));
5471 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
5472 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
5475 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
5477 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
5478 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
5480 if (TYPE_CONTEXT (type)
5481 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
5483 tree ctx = TYPE_CONTEXT (type);
5486 ctx = BLOCK_SUPERCONTEXT (ctx);
5488 while (ctx && TREE_CODE (ctx) == BLOCK);
5489 TYPE_CONTEXT (type) = ctx;
5492 TYPE_STUB_DECL (type) = NULL;
5493 TYPE_NAME (type) = fld_simplified_type_name (type);
5497 /* Return true if DECL may need an assembler name to be set. */
5499 static inline bool
5500 need_assembler_name_p (tree decl)
5502 /* We use DECL_ASSEMBLER_NAME to hold mangled type names for One Definition
5503 Rule merging. This makes type_odr_p to return true on those types during
5504 LTO and by comparing the mangled name, we can say what types are intended
5505 to be equivalent across compilation unit.
5507 We do not store names of type_in_anonymous_namespace_p.
5509 Record, union and enumeration type have linkage that allows use
5510 to check type_in_anonymous_namespace_p. We do not mangle compound types
5511 that always can be compared structurally.
5513 Similarly for builtin types, we compare properties of their main variant.
5514 A special case are integer types where mangling do make differences
5515 between char/signed char/unsigned char etc. Storing name for these makes
5516 e.g. -fno-signed-char/-fsigned-char mismatches to be handled well.
5517 See cp/mangle.c:write_builtin_type for details. */
5519 if (TREE_CODE (decl) == TYPE_DECL)
5521 if (flag_lto_odr_type_mering
5522 && DECL_NAME (decl)
5523 && decl == TYPE_NAME (TREE_TYPE (decl))
5524 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TREE_TYPE (decl)
5525 && !TYPE_ARTIFICIAL (TREE_TYPE (decl))
5526 && (type_with_linkage_p (TREE_TYPE (decl))
5527 || TREE_CODE (TREE_TYPE (decl)) == INTEGER_TYPE)
5528 && !variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5529 return !DECL_ASSEMBLER_NAME_SET_P (decl);
5530 return false;
5532 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
5533 if (!VAR_OR_FUNCTION_DECL_P (decl))
5534 return false;
5536 /* If DECL already has its assembler name set, it does not need a
5537 new one. */
5538 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
5539 || DECL_ASSEMBLER_NAME_SET_P (decl))
5540 return false;
5542 /* Abstract decls do not need an assembler name. */
5543 if (DECL_ABSTRACT_P (decl))
5544 return false;
5546 /* For VAR_DECLs, only static, public and external symbols need an
5547 assembler name. */
5548 if (VAR_P (decl)
5549 && !TREE_STATIC (decl)
5550 && !TREE_PUBLIC (decl)
5551 && !DECL_EXTERNAL (decl))
5552 return false;
5554 if (TREE_CODE (decl) == FUNCTION_DECL)
5556 /* Do not set assembler name on builtins. Allow RTL expansion to
5557 decide whether to expand inline or via a regular call. */
5558 if (fndecl_built_in_p (decl)
5559 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
5560 return false;
5562 /* Functions represented in the callgraph need an assembler name. */
5563 if (cgraph_node::get (decl) != NULL)
5564 return true;
5566 /* Unused and not public functions don't need an assembler name. */
5567 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
5568 return false;
5571 return true;
5575 /* Reset all language specific information still present in symbol
5576 DECL. */
5578 static void
5579 free_lang_data_in_decl (tree decl, struct free_lang_data_d *fld)
5581 gcc_assert (DECL_P (decl));
5583 /* Give the FE a chance to remove its own data first. */
5584 lang_hooks.free_lang_data (decl);
5586 TREE_LANG_FLAG_0 (decl) = 0;
5587 TREE_LANG_FLAG_1 (decl) = 0;
5588 TREE_LANG_FLAG_2 (decl) = 0;
5589 TREE_LANG_FLAG_3 (decl) = 0;
5590 TREE_LANG_FLAG_4 (decl) = 0;
5591 TREE_LANG_FLAG_5 (decl) = 0;
5592 TREE_LANG_FLAG_6 (decl) = 0;
5594 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
5595 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
5596 if (TREE_CODE (decl) == FIELD_DECL)
5598 DECL_FCONTEXT (decl) = NULL;
5599 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
5600 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
5601 DECL_QUALIFIER (decl) = NULL_TREE;
5604 if (TREE_CODE (decl) == FUNCTION_DECL)
5606 struct cgraph_node *node;
5607 /* Frontends do not set TREE_ADDRESSABLE on public variables even though
5608 the address may be taken in other unit, so this flag has no practical
5609 use for middle-end.
5611 It would make more sense if frontends set TREE_ADDRESSABLE to 0 only
5612 for public objects that indeed can not be adressed, but it is not
5613 the case. Set the flag to true so we do not get merge failures for
5614 i.e. virtual tables between units that take address of it and
5615 units that don't. */
5616 if (TREE_PUBLIC (decl))
5617 TREE_ADDRESSABLE (decl) = true;
5618 TREE_TYPE (decl) = fld_simplified_type (TREE_TYPE (decl), fld);
5619 if (!(node = cgraph_node::get (decl))
5620 || (!node->definition && !node->clones))
5622 if (node)
5623 node->release_body ();
5624 else
5626 release_function_body (decl);
5627 DECL_ARGUMENTS (decl) = NULL;
5628 DECL_RESULT (decl) = NULL;
5629 DECL_INITIAL (decl) = error_mark_node;
5632 if (gimple_has_body_p (decl) || (node && node->thunk.thunk_p))
5634 tree t;
5636 /* If DECL has a gimple body, then the context for its
5637 arguments must be DECL. Otherwise, it doesn't really
5638 matter, as we will not be emitting any code for DECL. In
5639 general, there may be other instances of DECL created by
5640 the front end and since PARM_DECLs are generally shared,
5641 their DECL_CONTEXT changes as the replicas of DECL are
5642 created. The only time where DECL_CONTEXT is important
5643 is for the FUNCTION_DECLs that have a gimple body (since
5644 the PARM_DECL will be used in the function's body). */
5645 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
5646 DECL_CONTEXT (t) = decl;
5647 if (!DECL_FUNCTION_SPECIFIC_TARGET (decl))
5648 DECL_FUNCTION_SPECIFIC_TARGET (decl)
5649 = target_option_default_node;
5650 if (!DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl))
5651 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl)
5652 = optimization_default_node;
5655 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
5656 At this point, it is not needed anymore. */
5657 DECL_SAVED_TREE (decl) = NULL_TREE;
5659 /* Clear the abstract origin if it refers to a method.
5660 Otherwise dwarf2out.c will ICE as we splice functions out of
5661 TYPE_FIELDS and thus the origin will not be output
5662 correctly. */
5663 if (DECL_ABSTRACT_ORIGIN (decl)
5664 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
5665 && RECORD_OR_UNION_TYPE_P
5666 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
5667 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
5669 DECL_VINDEX (decl) = NULL_TREE;
5671 else if (VAR_P (decl))
5673 /* See comment above why we set the flag for functoins. */
5674 if (TREE_PUBLIC (decl))
5675 TREE_ADDRESSABLE (decl) = true;
5676 if ((DECL_EXTERNAL (decl)
5677 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
5678 || (decl_function_context (decl) && !TREE_STATIC (decl)))
5679 DECL_INITIAL (decl) = NULL_TREE;
5681 else if (TREE_CODE (decl) == TYPE_DECL)
5683 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5684 DECL_VISIBILITY_SPECIFIED (decl) = 0;
5685 TREE_PUBLIC (decl) = 0;
5686 TREE_PRIVATE (decl) = 0;
5687 DECL_ARTIFICIAL (decl) = 0;
5688 TYPE_DECL_SUPPRESS_DEBUG (decl) = 0;
5689 DECL_INITIAL (decl) = NULL_TREE;
5690 DECL_ORIGINAL_TYPE (decl) = NULL_TREE;
5691 DECL_MODE (decl) = VOIDmode;
5692 SET_DECL_ALIGN (decl, 0);
5693 /* TREE_TYPE is cleared at WPA time in free_odr_warning_data. */
5695 else if (TREE_CODE (decl) == FIELD_DECL)
5697 TREE_TYPE (decl) = fld_simplified_type (TREE_TYPE (decl), fld);
5698 DECL_INITIAL (decl) = NULL_TREE;
5700 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
5701 && DECL_INITIAL (decl)
5702 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
5704 /* Strip builtins from the translation-unit BLOCK. We still have targets
5705 without builtin_decl_explicit support and also builtins are shared
5706 nodes and thus we can't use TREE_CHAIN in multiple lists. */
5707 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
5708 while (*nextp)
5710 tree var = *nextp;
5711 if (fndecl_built_in_p (var))
5712 *nextp = TREE_CHAIN (var);
5713 else
5714 nextp = &TREE_CHAIN (var);
5717 /* We need to keep field decls associated with their trees. Otherwise tree
5718 merging may merge some fileds and keep others disjoint wich in turn will
5719 not do well with TREE_CHAIN pointers linking them.
5721 Also do not drop containing types for virtual methods and tables because
5722 these are needed by devirtualization. */
5723 if (TREE_CODE (decl) != FIELD_DECL
5724 && ((TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
5725 || !DECL_VIRTUAL_P (decl)))
5726 DECL_CONTEXT (decl) = fld_decl_context (DECL_CONTEXT (decl));
5730 /* Operand callback helper for free_lang_data_in_node. *TP is the
5731 subtree operand being considered. */
5733 static tree
5734 find_decls_types_r (tree *tp, int *ws, void *data)
5736 tree t = *tp;
5737 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
5739 if (TREE_CODE (t) == TREE_LIST)
5740 return NULL_TREE;
5742 /* Language specific nodes will be removed, so there is no need
5743 to gather anything under them. */
5744 if (is_lang_specific (t))
5746 *ws = 0;
5747 return NULL_TREE;
5750 if (DECL_P (t))
5752 /* Note that walk_tree does not traverse every possible field in
5753 decls, so we have to do our own traversals here. */
5754 add_tree_to_fld_list (t, fld);
5756 fld_worklist_push (DECL_NAME (t), fld);
5757 fld_worklist_push (DECL_CONTEXT (t), fld);
5758 fld_worklist_push (DECL_SIZE (t), fld);
5759 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
5761 /* We are going to remove everything under DECL_INITIAL for
5762 TYPE_DECLs. No point walking them. */
5763 if (TREE_CODE (t) != TYPE_DECL)
5764 fld_worklist_push (DECL_INITIAL (t), fld);
5766 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
5767 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
5769 if (TREE_CODE (t) == FUNCTION_DECL)
5771 fld_worklist_push (DECL_ARGUMENTS (t), fld);
5772 fld_worklist_push (DECL_RESULT (t), fld);
5774 else if (TREE_CODE (t) == FIELD_DECL)
5776 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
5777 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
5778 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
5779 fld_worklist_push (DECL_FCONTEXT (t), fld);
5782 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
5783 && DECL_HAS_VALUE_EXPR_P (t))
5784 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
5786 if (TREE_CODE (t) != FIELD_DECL
5787 && TREE_CODE (t) != TYPE_DECL)
5788 fld_worklist_push (TREE_CHAIN (t), fld);
5789 *ws = 0;
5791 else if (TYPE_P (t))
5793 /* Note that walk_tree does not traverse every possible field in
5794 types, so we have to do our own traversals here. */
5795 add_tree_to_fld_list (t, fld);
5797 if (!RECORD_OR_UNION_TYPE_P (t))
5798 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
5799 fld_worklist_push (TYPE_SIZE (t), fld);
5800 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
5801 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
5802 fld_worklist_push (TYPE_POINTER_TO (t), fld);
5803 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
5804 fld_worklist_push (TYPE_NAME (t), fld);
5805 /* While we do not stream TYPE_POINTER_TO and TYPE_REFERENCE_TO
5806 lists, we may look types up in these lists and use them while
5807 optimizing the function body. Thus we need to free lang data
5808 in them. */
5809 if (TREE_CODE (t) == POINTER_TYPE)
5810 fld_worklist_push (TYPE_NEXT_PTR_TO (t), fld);
5811 if (TREE_CODE (t) == REFERENCE_TYPE)
5812 fld_worklist_push (TYPE_NEXT_REF_TO (t), fld);
5813 if (!POINTER_TYPE_P (t))
5814 fld_worklist_push (TYPE_MIN_VALUE_RAW (t), fld);
5815 /* TYPE_MAX_VALUE_RAW is TYPE_BINFO for record types. */
5816 if (!RECORD_OR_UNION_TYPE_P (t))
5817 fld_worklist_push (TYPE_MAX_VALUE_RAW (t), fld);
5818 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
5819 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
5820 do not and want not to reach unused variants this way. */
5821 if (TYPE_CONTEXT (t))
5823 tree ctx = TYPE_CONTEXT (t);
5824 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
5825 So push that instead. */
5826 while (ctx && TREE_CODE (ctx) == BLOCK)
5827 ctx = BLOCK_SUPERCONTEXT (ctx);
5828 fld_worklist_push (ctx, fld);
5830 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
5831 and want not to reach unused types this way. */
5833 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
5835 unsigned i;
5836 tree tem;
5837 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
5838 fld_worklist_push (TREE_TYPE (tem), fld);
5839 fld_worklist_push (BINFO_TYPE (TYPE_BINFO (t)), fld);
5840 fld_worklist_push (BINFO_VTABLE (TYPE_BINFO (t)), fld);
5842 if (RECORD_OR_UNION_TYPE_P (t))
5844 tree tem;
5845 /* Push all TYPE_FIELDS - there can be interleaving interesting
5846 and non-interesting things. */
5847 tem = TYPE_FIELDS (t);
5848 while (tem)
5850 if (TREE_CODE (tem) == FIELD_DECL)
5851 fld_worklist_push (tem, fld);
5852 tem = TREE_CHAIN (tem);
5855 if (FUNC_OR_METHOD_TYPE_P (t))
5856 fld_worklist_push (TYPE_METHOD_BASETYPE (t), fld);
5858 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5859 *ws = 0;
5861 else if (TREE_CODE (t) == BLOCK)
5863 for (tree *tem = &BLOCK_VARS (t); *tem; )
5865 if (TREE_CODE (*tem) != VAR_DECL
5866 || !auto_var_in_fn_p (*tem, DECL_CONTEXT (*tem)))
5868 gcc_assert (TREE_CODE (*tem) != RESULT_DECL
5869 && TREE_CODE (*tem) != PARM_DECL);
5870 *tem = TREE_CHAIN (*tem);
5872 else
5874 fld_worklist_push (*tem, fld);
5875 tem = &TREE_CHAIN (*tem);
5878 for (tree tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5879 fld_worklist_push (tem, fld);
5880 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5883 if (TREE_CODE (t) != IDENTIFIER_NODE
5884 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5885 fld_worklist_push (TREE_TYPE (t), fld);
5887 return NULL_TREE;
5891 /* Find decls and types in T. */
5893 static void
5894 find_decls_types (tree t, struct free_lang_data_d *fld)
5896 while (1)
5898 if (!fld->pset.contains (t))
5899 walk_tree (&t, find_decls_types_r, fld, &fld->pset);
5900 if (fld->worklist.is_empty ())
5901 break;
5902 t = fld->worklist.pop ();
5906 /* Translate all the types in LIST with the corresponding runtime
5907 types. */
5909 static tree
5910 get_eh_types_for_runtime (tree list)
5912 tree head, prev;
5914 if (list == NULL_TREE)
5915 return NULL_TREE;
5917 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5918 prev = head;
5919 list = TREE_CHAIN (list);
5920 while (list)
5922 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5923 TREE_CHAIN (prev) = n;
5924 prev = TREE_CHAIN (prev);
5925 list = TREE_CHAIN (list);
5928 return head;
5932 /* Find decls and types referenced in EH region R and store them in
5933 FLD->DECLS and FLD->TYPES. */
5935 static void
5936 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5938 switch (r->type)
5940 case ERT_CLEANUP:
5941 break;
5943 case ERT_TRY:
5945 eh_catch c;
5947 /* The types referenced in each catch must first be changed to the
5948 EH types used at runtime. This removes references to FE types
5949 in the region. */
5950 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5952 c->type_list = get_eh_types_for_runtime (c->type_list);
5953 walk_tree (&c->type_list, find_decls_types_r, fld, &fld->pset);
5956 break;
5958 case ERT_ALLOWED_EXCEPTIONS:
5959 r->u.allowed.type_list
5960 = get_eh_types_for_runtime (r->u.allowed.type_list);
5961 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, &fld->pset);
5962 break;
5964 case ERT_MUST_NOT_THROW:
5965 walk_tree (&r->u.must_not_throw.failure_decl,
5966 find_decls_types_r, fld, &fld->pset);
5967 break;
5972 /* Find decls and types referenced in cgraph node N and store them in
5973 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5974 look for *every* kind of DECL and TYPE node reachable from N,
5975 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5976 NAMESPACE_DECLs, etc). */
5978 static void
5979 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5981 basic_block bb;
5982 struct function *fn;
5983 unsigned ix;
5984 tree t;
5986 find_decls_types (n->decl, fld);
5988 if (!gimple_has_body_p (n->decl))
5989 return;
5991 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5993 fn = DECL_STRUCT_FUNCTION (n->decl);
5995 /* Traverse locals. */
5996 FOR_EACH_LOCAL_DECL (fn, ix, t)
5997 find_decls_types (t, fld);
5999 /* Traverse EH regions in FN. */
6001 eh_region r;
6002 FOR_ALL_EH_REGION_FN (r, fn)
6003 find_decls_types_in_eh_region (r, fld);
6006 /* Traverse every statement in FN. */
6007 FOR_EACH_BB_FN (bb, fn)
6009 gphi_iterator psi;
6010 gimple_stmt_iterator si;
6011 unsigned i;
6013 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
6015 gphi *phi = psi.phi ();
6017 for (i = 0; i < gimple_phi_num_args (phi); i++)
6019 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
6020 find_decls_types (*arg_p, fld);
6024 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6026 gimple *stmt = gsi_stmt (si);
6028 if (is_gimple_call (stmt))
6029 find_decls_types (gimple_call_fntype (stmt), fld);
6031 for (i = 0; i < gimple_num_ops (stmt); i++)
6033 tree arg = gimple_op (stmt, i);
6034 find_decls_types (arg, fld);
6041 /* Find decls and types referenced in varpool node N and store them in
6042 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
6043 look for *every* kind of DECL and TYPE node reachable from N,
6044 including those embedded inside types and decls (i.e,, TYPE_DECLs,
6045 NAMESPACE_DECLs, etc). */
6047 static void
6048 find_decls_types_in_var (varpool_node *v, struct free_lang_data_d *fld)
6050 find_decls_types (v->decl, fld);
6053 /* If T needs an assembler name, have one created for it. */
6055 void
6056 assign_assembler_name_if_needed (tree t)
6058 if (need_assembler_name_p (t))
6060 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
6061 diagnostics that use input_location to show locus
6062 information. The problem here is that, at this point,
6063 input_location is generally anchored to the end of the file
6064 (since the parser is long gone), so we don't have a good
6065 position to pin it to.
6067 To alleviate this problem, this uses the location of T's
6068 declaration. Examples of this are
6069 testsuite/g++.dg/template/cond2.C and
6070 testsuite/g++.dg/template/pr35240.C. */
6071 location_t saved_location = input_location;
6072 input_location = DECL_SOURCE_LOCATION (t);
6074 decl_assembler_name (t);
6076 input_location = saved_location;
6081 /* Free language specific information for every operand and expression
6082 in every node of the call graph. This process operates in three stages:
6084 1- Every callgraph node and varpool node is traversed looking for
6085 decls and types embedded in them. This is a more exhaustive
6086 search than that done by find_referenced_vars, because it will
6087 also collect individual fields, decls embedded in types, etc.
6089 2- All the decls found are sent to free_lang_data_in_decl.
6091 3- All the types found are sent to free_lang_data_in_type.
6093 The ordering between decls and types is important because
6094 free_lang_data_in_decl sets assembler names, which includes
6095 mangling. So types cannot be freed up until assembler names have
6096 been set up. */
6098 static void
6099 free_lang_data_in_cgraph (struct free_lang_data_d *fld)
6101 struct cgraph_node *n;
6102 varpool_node *v;
6103 tree t;
6104 unsigned i;
6105 alias_pair *p;
6107 /* Find decls and types in the body of every function in the callgraph. */
6108 FOR_EACH_FUNCTION (n)
6109 find_decls_types_in_node (n, fld);
6111 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
6112 find_decls_types (p->decl, fld);
6114 /* Find decls and types in every varpool symbol. */
6115 FOR_EACH_VARIABLE (v)
6116 find_decls_types_in_var (v, fld);
6118 /* Set the assembler name on every decl found. We need to do this
6119 now because free_lang_data_in_decl will invalidate data needed
6120 for mangling. This breaks mangling on interdependent decls. */
6121 FOR_EACH_VEC_ELT (fld->decls, i, t)
6122 assign_assembler_name_if_needed (t);
6124 /* Traverse every decl found freeing its language data. */
6125 FOR_EACH_VEC_ELT (fld->decls, i, t)
6126 free_lang_data_in_decl (t, fld);
6128 /* Traverse every type found freeing its language data. */
6129 FOR_EACH_VEC_ELT (fld->types, i, t)
6130 free_lang_data_in_type (t, fld);
6134 /* Free resources that are used by FE but are not needed once they are done. */
6136 static unsigned
6137 free_lang_data (void)
6139 unsigned i;
6140 struct free_lang_data_d fld;
6142 /* If we are the LTO frontend we have freed lang-specific data already. */
6143 if (in_lto_p
6144 || (!flag_generate_lto && !flag_generate_offload))
6145 return 0;
6147 fld_incomplete_types = new hash_map<tree, tree>;
6148 fld_simplified_types = new hash_map<tree, tree>;
6150 /* Provide a dummy TRANSLATION_UNIT_DECL if the FE failed to provide one. */
6151 if (vec_safe_is_empty (all_translation_units))
6152 build_translation_unit_decl (NULL_TREE);
6154 /* Allocate and assign alias sets to the standard integer types
6155 while the slots are still in the way the frontends generated them. */
6156 for (i = 0; i < itk_none; ++i)
6157 if (integer_types[i])
6158 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
6160 /* Traverse the IL resetting language specific information for
6161 operands, expressions, etc. */
6162 free_lang_data_in_cgraph (&fld);
6164 /* Create gimple variants for common types. */
6165 for (unsigned i = 0;
6166 i < sizeof (builtin_structptr_types) / sizeof (builtin_structptr_type);
6167 ++i)
6168 builtin_structptr_types[i].node = builtin_structptr_types[i].base;
6170 /* Reset some langhooks. Do not reset types_compatible_p, it may
6171 still be used indirectly via the get_alias_set langhook. */
6172 lang_hooks.dwarf_name = lhd_dwarf_name;
6173 lang_hooks.decl_printable_name = gimple_decl_printable_name;
6174 lang_hooks.gimplify_expr = lhd_gimplify_expr;
6175 lang_hooks.overwrite_decl_assembler_name = lhd_overwrite_decl_assembler_name;
6176 lang_hooks.print_xnode = lhd_print_tree_nothing;
6177 lang_hooks.print_decl = lhd_print_tree_nothing;
6178 lang_hooks.print_type = lhd_print_tree_nothing;
6179 lang_hooks.print_identifier = lhd_print_tree_nothing;
6181 lang_hooks.tree_inlining.var_mod_type_p = hook_bool_tree_tree_false;
6183 if (flag_checking)
6185 int i;
6186 tree t;
6188 FOR_EACH_VEC_ELT (fld.types, i, t)
6189 verify_type (t);
6192 /* We do not want the default decl_assembler_name implementation,
6193 rather if we have fixed everything we want a wrapper around it
6194 asserting that all non-local symbols already got their assembler
6195 name and only produce assembler names for local symbols. Or rather
6196 make sure we never call decl_assembler_name on local symbols and
6197 devise a separate, middle-end private scheme for it. */
6199 /* Reset diagnostic machinery. */
6200 tree_diagnostics_defaults (global_dc);
6202 rebuild_type_inheritance_graph ();
6204 delete fld_incomplete_types;
6205 delete fld_simplified_types;
6207 return 0;
6211 namespace {
6213 const pass_data pass_data_ipa_free_lang_data =
6215 SIMPLE_IPA_PASS, /* type */
6216 "*free_lang_data", /* name */
6217 OPTGROUP_NONE, /* optinfo_flags */
6218 TV_IPA_FREE_LANG_DATA, /* tv_id */
6219 0, /* properties_required */
6220 0, /* properties_provided */
6221 0, /* properties_destroyed */
6222 0, /* todo_flags_start */
6223 0, /* todo_flags_finish */
6226 class pass_ipa_free_lang_data : public simple_ipa_opt_pass
6228 public:
6229 pass_ipa_free_lang_data (gcc::context *ctxt)
6230 : simple_ipa_opt_pass (pass_data_ipa_free_lang_data, ctxt)
6233 /* opt_pass methods: */
6234 virtual unsigned int execute (function *) { return free_lang_data (); }
6236 }; // class pass_ipa_free_lang_data
6238 } // anon namespace
6240 simple_ipa_opt_pass *
6241 make_pass_ipa_free_lang_data (gcc::context *ctxt)
6243 return new pass_ipa_free_lang_data (ctxt);
6246 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
6247 of the various TYPE_QUAL values. */
6249 static void
6250 set_type_quals (tree type, int type_quals)
6252 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
6253 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
6254 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
6255 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
6256 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
6259 /* Returns true iff CAND and BASE have equivalent language-specific
6260 qualifiers. */
6262 bool
6263 check_lang_type (const_tree cand, const_tree base)
6265 if (lang_hooks.types.type_hash_eq == NULL)
6266 return true;
6267 /* type_hash_eq currently only applies to these types. */
6268 if (TREE_CODE (cand) != FUNCTION_TYPE
6269 && TREE_CODE (cand) != METHOD_TYPE)
6270 return true;
6271 return lang_hooks.types.type_hash_eq (cand, base);
6274 /* Returns true iff unqualified CAND and BASE are equivalent. */
6276 bool
6277 check_base_type (const_tree cand, const_tree base)
6279 return (TYPE_NAME (cand) == TYPE_NAME (base)
6280 /* Apparently this is needed for Objective-C. */
6281 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6282 /* Check alignment. */
6283 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
6284 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6285 TYPE_ATTRIBUTES (base)));
6288 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
6290 bool
6291 check_qualified_type (const_tree cand, const_tree base, int type_quals)
6293 return (TYPE_QUALS (cand) == type_quals
6294 && check_base_type (cand, base)
6295 && check_lang_type (cand, base));
6298 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
6300 static bool
6301 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
6303 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
6304 && TYPE_NAME (cand) == TYPE_NAME (base)
6305 /* Apparently this is needed for Objective-C. */
6306 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6307 /* Check alignment. */
6308 && TYPE_ALIGN (cand) == align
6309 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6310 TYPE_ATTRIBUTES (base))
6311 && check_lang_type (cand, base));
6314 /* This function checks to see if TYPE matches the size one of the built-in
6315 atomic types, and returns that core atomic type. */
6317 static tree
6318 find_atomic_core_type (tree type)
6320 tree base_atomic_type;
6322 /* Only handle complete types. */
6323 if (!tree_fits_uhwi_p (TYPE_SIZE (type)))
6324 return NULL_TREE;
6326 switch (tree_to_uhwi (TYPE_SIZE (type)))
6328 case 8:
6329 base_atomic_type = atomicQI_type_node;
6330 break;
6332 case 16:
6333 base_atomic_type = atomicHI_type_node;
6334 break;
6336 case 32:
6337 base_atomic_type = atomicSI_type_node;
6338 break;
6340 case 64:
6341 base_atomic_type = atomicDI_type_node;
6342 break;
6344 case 128:
6345 base_atomic_type = atomicTI_type_node;
6346 break;
6348 default:
6349 base_atomic_type = NULL_TREE;
6352 return base_atomic_type;
6355 /* Return a version of the TYPE, qualified as indicated by the
6356 TYPE_QUALS, if one exists. If no qualified version exists yet,
6357 return NULL_TREE. */
6359 tree
6360 get_qualified_type (tree type, int type_quals)
6362 tree t;
6364 if (TYPE_QUALS (type) == type_quals)
6365 return type;
6367 /* Search the chain of variants to see if there is already one there just
6368 like the one we need to have. If so, use that existing one. We must
6369 preserve the TYPE_NAME, since there is code that depends on this. */
6370 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6371 if (check_qualified_type (t, type, type_quals))
6372 return t;
6374 return NULL_TREE;
6377 /* Like get_qualified_type, but creates the type if it does not
6378 exist. This function never returns NULL_TREE. */
6380 tree
6381 build_qualified_type (tree type, int type_quals MEM_STAT_DECL)
6383 tree t;
6385 /* See if we already have the appropriate qualified variant. */
6386 t = get_qualified_type (type, type_quals);
6388 /* If not, build it. */
6389 if (!t)
6391 t = build_variant_type_copy (type PASS_MEM_STAT);
6392 set_type_quals (t, type_quals);
6394 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
6396 /* See if this object can map to a basic atomic type. */
6397 tree atomic_type = find_atomic_core_type (type);
6398 if (atomic_type)
6400 /* Ensure the alignment of this type is compatible with
6401 the required alignment of the atomic type. */
6402 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
6403 SET_TYPE_ALIGN (t, TYPE_ALIGN (atomic_type));
6407 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6408 /* Propagate structural equality. */
6409 SET_TYPE_STRUCTURAL_EQUALITY (t);
6410 else if (TYPE_CANONICAL (type) != type)
6411 /* Build the underlying canonical type, since it is different
6412 from TYPE. */
6414 tree c = build_qualified_type (TYPE_CANONICAL (type), type_quals);
6415 TYPE_CANONICAL (t) = TYPE_CANONICAL (c);
6417 else
6418 /* T is its own canonical type. */
6419 TYPE_CANONICAL (t) = t;
6423 return t;
6426 /* Create a variant of type T with alignment ALIGN. */
6428 tree
6429 build_aligned_type (tree type, unsigned int align)
6431 tree t;
6433 if (TYPE_PACKED (type)
6434 || TYPE_ALIGN (type) == align)
6435 return type;
6437 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6438 if (check_aligned_type (t, type, align))
6439 return t;
6441 t = build_variant_type_copy (type);
6442 SET_TYPE_ALIGN (t, align);
6443 TYPE_USER_ALIGN (t) = 1;
6445 return t;
6448 /* Create a new distinct copy of TYPE. The new type is made its own
6449 MAIN_VARIANT. If TYPE requires structural equality checks, the
6450 resulting type requires structural equality checks; otherwise, its
6451 TYPE_CANONICAL points to itself. */
6453 tree
6454 build_distinct_type_copy (tree type MEM_STAT_DECL)
6456 tree t = copy_node (type PASS_MEM_STAT);
6458 TYPE_POINTER_TO (t) = 0;
6459 TYPE_REFERENCE_TO (t) = 0;
6461 /* Set the canonical type either to a new equivalence class, or
6462 propagate the need for structural equality checks. */
6463 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6464 SET_TYPE_STRUCTURAL_EQUALITY (t);
6465 else
6466 TYPE_CANONICAL (t) = t;
6468 /* Make it its own variant. */
6469 TYPE_MAIN_VARIANT (t) = t;
6470 TYPE_NEXT_VARIANT (t) = 0;
6472 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
6473 whose TREE_TYPE is not t. This can also happen in the Ada
6474 frontend when using subtypes. */
6476 return t;
6479 /* Create a new variant of TYPE, equivalent but distinct. This is so
6480 the caller can modify it. TYPE_CANONICAL for the return type will
6481 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
6482 are considered equal by the language itself (or that both types
6483 require structural equality checks). */
6485 tree
6486 build_variant_type_copy (tree type MEM_STAT_DECL)
6488 tree t, m = TYPE_MAIN_VARIANT (type);
6490 t = build_distinct_type_copy (type PASS_MEM_STAT);
6492 /* Since we're building a variant, assume that it is a non-semantic
6493 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
6494 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
6495 /* Type variants have no alias set defined. */
6496 TYPE_ALIAS_SET (t) = -1;
6498 /* Add the new type to the chain of variants of TYPE. */
6499 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
6500 TYPE_NEXT_VARIANT (m) = t;
6501 TYPE_MAIN_VARIANT (t) = m;
6503 return t;
6506 /* Return true if the from tree in both tree maps are equal. */
6509 tree_map_base_eq (const void *va, const void *vb)
6511 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6512 *const b = (const struct tree_map_base *) vb;
6513 return (a->from == b->from);
6516 /* Hash a from tree in a tree_base_map. */
6518 unsigned int
6519 tree_map_base_hash (const void *item)
6521 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6524 /* Return true if this tree map structure is marked for garbage collection
6525 purposes. We simply return true if the from tree is marked, so that this
6526 structure goes away when the from tree goes away. */
6529 tree_map_base_marked_p (const void *p)
6531 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6534 /* Hash a from tree in a tree_map. */
6536 unsigned int
6537 tree_map_hash (const void *item)
6539 return (((const struct tree_map *) item)->hash);
6542 /* Hash a from tree in a tree_decl_map. */
6544 unsigned int
6545 tree_decl_map_hash (const void *item)
6547 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6550 /* Return the initialization priority for DECL. */
6552 priority_type
6553 decl_init_priority_lookup (tree decl)
6555 symtab_node *snode = symtab_node::get (decl);
6557 if (!snode)
6558 return DEFAULT_INIT_PRIORITY;
6559 return
6560 snode->get_init_priority ();
6563 /* Return the finalization priority for DECL. */
6565 priority_type
6566 decl_fini_priority_lookup (tree decl)
6568 cgraph_node *node = cgraph_node::get (decl);
6570 if (!node)
6571 return DEFAULT_INIT_PRIORITY;
6572 return
6573 node->get_fini_priority ();
6576 /* Set the initialization priority for DECL to PRIORITY. */
6578 void
6579 decl_init_priority_insert (tree decl, priority_type priority)
6581 struct symtab_node *snode;
6583 if (priority == DEFAULT_INIT_PRIORITY)
6585 snode = symtab_node::get (decl);
6586 if (!snode)
6587 return;
6589 else if (VAR_P (decl))
6590 snode = varpool_node::get_create (decl);
6591 else
6592 snode = cgraph_node::get_create (decl);
6593 snode->set_init_priority (priority);
6596 /* Set the finalization priority for DECL to PRIORITY. */
6598 void
6599 decl_fini_priority_insert (tree decl, priority_type priority)
6601 struct cgraph_node *node;
6603 if (priority == DEFAULT_INIT_PRIORITY)
6605 node = cgraph_node::get (decl);
6606 if (!node)
6607 return;
6609 else
6610 node = cgraph_node::get_create (decl);
6611 node->set_fini_priority (priority);
6614 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6616 static void
6617 print_debug_expr_statistics (void)
6619 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6620 (long) debug_expr_for_decl->size (),
6621 (long) debug_expr_for_decl->elements (),
6622 debug_expr_for_decl->collisions ());
6625 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6627 static void
6628 print_value_expr_statistics (void)
6630 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6631 (long) value_expr_for_decl->size (),
6632 (long) value_expr_for_decl->elements (),
6633 value_expr_for_decl->collisions ());
6636 /* Lookup a debug expression for FROM, and return it if we find one. */
6638 tree
6639 decl_debug_expr_lookup (tree from)
6641 struct tree_decl_map *h, in;
6642 in.base.from = from;
6644 h = debug_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6645 if (h)
6646 return h->to;
6647 return NULL_TREE;
6650 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6652 void
6653 decl_debug_expr_insert (tree from, tree to)
6655 struct tree_decl_map *h;
6657 h = ggc_alloc<tree_decl_map> ();
6658 h->base.from = from;
6659 h->to = to;
6660 *debug_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6663 /* Lookup a value expression for FROM, and return it if we find one. */
6665 tree
6666 decl_value_expr_lookup (tree from)
6668 struct tree_decl_map *h, in;
6669 in.base.from = from;
6671 h = value_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6672 if (h)
6673 return h->to;
6674 return NULL_TREE;
6677 /* Insert a mapping FROM->TO in the value expression hashtable. */
6679 void
6680 decl_value_expr_insert (tree from, tree to)
6682 struct tree_decl_map *h;
6684 h = ggc_alloc<tree_decl_map> ();
6685 h->base.from = from;
6686 h->to = to;
6687 *value_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6690 /* Lookup a vector of debug arguments for FROM, and return it if we
6691 find one. */
6693 vec<tree, va_gc> **
6694 decl_debug_args_lookup (tree from)
6696 struct tree_vec_map *h, in;
6698 if (!DECL_HAS_DEBUG_ARGS_P (from))
6699 return NULL;
6700 gcc_checking_assert (debug_args_for_decl != NULL);
6701 in.base.from = from;
6702 h = debug_args_for_decl->find_with_hash (&in, DECL_UID (from));
6703 if (h)
6704 return &h->to;
6705 return NULL;
6708 /* Insert a mapping FROM->empty vector of debug arguments in the value
6709 expression hashtable. */
6711 vec<tree, va_gc> **
6712 decl_debug_args_insert (tree from)
6714 struct tree_vec_map *h;
6715 tree_vec_map **loc;
6717 if (DECL_HAS_DEBUG_ARGS_P (from))
6718 return decl_debug_args_lookup (from);
6719 if (debug_args_for_decl == NULL)
6720 debug_args_for_decl = hash_table<tree_vec_map_cache_hasher>::create_ggc (64);
6721 h = ggc_alloc<tree_vec_map> ();
6722 h->base.from = from;
6723 h->to = NULL;
6724 loc = debug_args_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT);
6725 *loc = h;
6726 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6727 return &h->to;
6730 /* Hashing of types so that we don't make duplicates.
6731 The entry point is `type_hash_canon'. */
6733 /* Generate the default hash code for TYPE. This is designed for
6734 speed, rather than maximum entropy. */
6736 hashval_t
6737 type_hash_canon_hash (tree type)
6739 inchash::hash hstate;
6741 hstate.add_int (TREE_CODE (type));
6743 if (TREE_TYPE (type))
6744 hstate.add_object (TYPE_HASH (TREE_TYPE (type)));
6746 for (tree t = TYPE_ATTRIBUTES (type); t; t = TREE_CHAIN (t))
6747 /* Just the identifier is adequate to distinguish. */
6748 hstate.add_object (IDENTIFIER_HASH_VALUE (get_attribute_name (t)));
6750 switch (TREE_CODE (type))
6752 case METHOD_TYPE:
6753 hstate.add_object (TYPE_HASH (TYPE_METHOD_BASETYPE (type)));
6754 /* FALLTHROUGH. */
6755 case FUNCTION_TYPE:
6756 for (tree t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6757 if (TREE_VALUE (t) != error_mark_node)
6758 hstate.add_object (TYPE_HASH (TREE_VALUE (t)));
6759 break;
6761 case OFFSET_TYPE:
6762 hstate.add_object (TYPE_HASH (TYPE_OFFSET_BASETYPE (type)));
6763 break;
6765 case ARRAY_TYPE:
6767 if (TYPE_DOMAIN (type))
6768 hstate.add_object (TYPE_HASH (TYPE_DOMAIN (type)));
6769 if (!AGGREGATE_TYPE_P (TREE_TYPE (type)))
6771 unsigned typeless = TYPE_TYPELESS_STORAGE (type);
6772 hstate.add_object (typeless);
6775 break;
6777 case INTEGER_TYPE:
6779 tree t = TYPE_MAX_VALUE (type);
6780 if (!t)
6781 t = TYPE_MIN_VALUE (type);
6782 for (int i = 0; i < TREE_INT_CST_NUNITS (t); i++)
6783 hstate.add_object (TREE_INT_CST_ELT (t, i));
6784 break;
6787 case REAL_TYPE:
6788 case FIXED_POINT_TYPE:
6790 unsigned prec = TYPE_PRECISION (type);
6791 hstate.add_object (prec);
6792 break;
6795 case VECTOR_TYPE:
6796 hstate.add_poly_int (TYPE_VECTOR_SUBPARTS (type));
6797 break;
6799 default:
6800 break;
6803 return hstate.end ();
6806 /* These are the Hashtable callback functions. */
6808 /* Returns true iff the types are equivalent. */
6810 bool
6811 type_cache_hasher::equal (type_hash *a, type_hash *b)
6813 /* First test the things that are the same for all types. */
6814 if (a->hash != b->hash
6815 || TREE_CODE (a->type) != TREE_CODE (b->type)
6816 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6817 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6818 TYPE_ATTRIBUTES (b->type))
6819 || (TREE_CODE (a->type) != COMPLEX_TYPE
6820 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6821 return 0;
6823 /* Be careful about comparing arrays before and after the element type
6824 has been completed; don't compare TYPE_ALIGN unless both types are
6825 complete. */
6826 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6827 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6828 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6829 return 0;
6831 switch (TREE_CODE (a->type))
6833 case VOID_TYPE:
6834 case COMPLEX_TYPE:
6835 case POINTER_TYPE:
6836 case REFERENCE_TYPE:
6837 case NULLPTR_TYPE:
6838 return 1;
6840 case VECTOR_TYPE:
6841 return known_eq (TYPE_VECTOR_SUBPARTS (a->type),
6842 TYPE_VECTOR_SUBPARTS (b->type));
6844 case ENUMERAL_TYPE:
6845 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6846 && !(TYPE_VALUES (a->type)
6847 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6848 && TYPE_VALUES (b->type)
6849 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6850 && type_list_equal (TYPE_VALUES (a->type),
6851 TYPE_VALUES (b->type))))
6852 return 0;
6854 /* fall through */
6856 case INTEGER_TYPE:
6857 case REAL_TYPE:
6858 case BOOLEAN_TYPE:
6859 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
6860 return false;
6861 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6862 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6863 TYPE_MAX_VALUE (b->type)))
6864 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6865 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6866 TYPE_MIN_VALUE (b->type))));
6868 case FIXED_POINT_TYPE:
6869 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6871 case OFFSET_TYPE:
6872 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6874 case METHOD_TYPE:
6875 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6876 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6877 || (TYPE_ARG_TYPES (a->type)
6878 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6879 && TYPE_ARG_TYPES (b->type)
6880 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6881 && type_list_equal (TYPE_ARG_TYPES (a->type),
6882 TYPE_ARG_TYPES (b->type)))))
6883 break;
6884 return 0;
6885 case ARRAY_TYPE:
6886 /* Don't compare TYPE_TYPELESS_STORAGE flag on aggregates,
6887 where the flag should be inherited from the element type
6888 and can change after ARRAY_TYPEs are created; on non-aggregates
6889 compare it and hash it, scalars will never have that flag set
6890 and we need to differentiate between arrays created by different
6891 front-ends or middle-end created arrays. */
6892 return (TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type)
6893 && (AGGREGATE_TYPE_P (TREE_TYPE (a->type))
6894 || (TYPE_TYPELESS_STORAGE (a->type)
6895 == TYPE_TYPELESS_STORAGE (b->type))));
6897 case RECORD_TYPE:
6898 case UNION_TYPE:
6899 case QUAL_UNION_TYPE:
6900 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6901 || (TYPE_FIELDS (a->type)
6902 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6903 && TYPE_FIELDS (b->type)
6904 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6905 && type_list_equal (TYPE_FIELDS (a->type),
6906 TYPE_FIELDS (b->type))));
6908 case FUNCTION_TYPE:
6909 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6910 || (TYPE_ARG_TYPES (a->type)
6911 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6912 && TYPE_ARG_TYPES (b->type)
6913 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6914 && type_list_equal (TYPE_ARG_TYPES (a->type),
6915 TYPE_ARG_TYPES (b->type))))
6916 break;
6917 return 0;
6919 default:
6920 return 0;
6923 if (lang_hooks.types.type_hash_eq != NULL)
6924 return lang_hooks.types.type_hash_eq (a->type, b->type);
6926 return 1;
6929 /* Given TYPE, and HASHCODE its hash code, return the canonical
6930 object for an identical type if one already exists.
6931 Otherwise, return TYPE, and record it as the canonical object.
6933 To use this function, first create a type of the sort you want.
6934 Then compute its hash code from the fields of the type that
6935 make it different from other similar types.
6936 Then call this function and use the value. */
6938 tree
6939 type_hash_canon (unsigned int hashcode, tree type)
6941 type_hash in;
6942 type_hash **loc;
6944 /* The hash table only contains main variants, so ensure that's what we're
6945 being passed. */
6946 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6948 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6949 must call that routine before comparing TYPE_ALIGNs. */
6950 layout_type (type);
6952 in.hash = hashcode;
6953 in.type = type;
6955 loc = type_hash_table->find_slot_with_hash (&in, hashcode, INSERT);
6956 if (*loc)
6958 tree t1 = ((type_hash *) *loc)->type;
6959 gcc_assert (TYPE_MAIN_VARIANT (t1) == t1
6960 && t1 != type);
6961 if (TYPE_UID (type) + 1 == next_type_uid)
6962 --next_type_uid;
6963 /* Free also min/max values and the cache for integer
6964 types. This can't be done in free_node, as LTO frees
6965 those on its own. */
6966 if (TREE_CODE (type) == INTEGER_TYPE)
6968 if (TYPE_MIN_VALUE (type)
6969 && TREE_TYPE (TYPE_MIN_VALUE (type)) == type)
6971 /* Zero is always in TYPE_CACHED_VALUES. */
6972 if (! TYPE_UNSIGNED (type))
6973 int_cst_hash_table->remove_elt (TYPE_MIN_VALUE (type));
6974 ggc_free (TYPE_MIN_VALUE (type));
6976 if (TYPE_MAX_VALUE (type)
6977 && TREE_TYPE (TYPE_MAX_VALUE (type)) == type)
6979 int_cst_hash_table->remove_elt (TYPE_MAX_VALUE (type));
6980 ggc_free (TYPE_MAX_VALUE (type));
6982 if (TYPE_CACHED_VALUES_P (type))
6983 ggc_free (TYPE_CACHED_VALUES (type));
6985 free_node (type);
6986 return t1;
6988 else
6990 struct type_hash *h;
6992 h = ggc_alloc<type_hash> ();
6993 h->hash = hashcode;
6994 h->type = type;
6995 *loc = h;
6997 return type;
7001 static void
7002 print_type_hash_statistics (void)
7004 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
7005 (long) type_hash_table->size (),
7006 (long) type_hash_table->elements (),
7007 type_hash_table->collisions ());
7010 /* Given two lists of types
7011 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
7012 return 1 if the lists contain the same types in the same order.
7013 Also, the TREE_PURPOSEs must match. */
7015 bool
7016 type_list_equal (const_tree l1, const_tree l2)
7018 const_tree t1, t2;
7020 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7021 if (TREE_VALUE (t1) != TREE_VALUE (t2)
7022 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
7023 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
7024 && (TREE_TYPE (TREE_PURPOSE (t1))
7025 == TREE_TYPE (TREE_PURPOSE (t2))))))
7026 return false;
7028 return t1 == t2;
7031 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
7032 given by TYPE. If the argument list accepts variable arguments,
7033 then this function counts only the ordinary arguments. */
7036 type_num_arguments (const_tree fntype)
7038 int i = 0;
7040 for (tree t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
7041 /* If the function does not take a variable number of arguments,
7042 the last element in the list will have type `void'. */
7043 if (VOID_TYPE_P (TREE_VALUE (t)))
7044 break;
7045 else
7046 ++i;
7048 return i;
7051 /* Return the type of the function TYPE's argument ARGNO if known.
7052 For vararg function's where ARGNO refers to one of the variadic
7053 arguments return null. Otherwise, return a void_type_node for
7054 out-of-bounds ARGNO. */
7056 tree
7057 type_argument_type (const_tree fntype, unsigned argno)
7059 /* Treat zero the same as an out-of-bounds argument number. */
7060 if (!argno)
7061 return void_type_node;
7063 function_args_iterator iter;
7065 tree argtype;
7066 unsigned i = 1;
7067 FOREACH_FUNCTION_ARGS (fntype, argtype, iter)
7069 /* A vararg function's argument list ends in a null. Otherwise,
7070 an ordinary function's argument list ends with void. Return
7071 null if ARGNO refers to a vararg argument, void_type_node if
7072 it's out of bounds, and the formal argument type otherwise. */
7073 if (!argtype)
7074 break;
7076 if (i == argno || VOID_TYPE_P (argtype))
7077 return argtype;
7079 ++i;
7082 return NULL_TREE;
7085 /* Nonzero if integer constants T1 and T2
7086 represent the same constant value. */
7089 tree_int_cst_equal (const_tree t1, const_tree t2)
7091 if (t1 == t2)
7092 return 1;
7094 if (t1 == 0 || t2 == 0)
7095 return 0;
7097 if (TREE_CODE (t1) == INTEGER_CST
7098 && TREE_CODE (t2) == INTEGER_CST
7099 && wi::to_widest (t1) == wi::to_widest (t2))
7100 return 1;
7102 return 0;
7105 /* Return true if T is an INTEGER_CST whose numerical value (extended
7106 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
7108 bool
7109 tree_fits_shwi_p (const_tree t)
7111 return (t != NULL_TREE
7112 && TREE_CODE (t) == INTEGER_CST
7113 && wi::fits_shwi_p (wi::to_widest (t)));
7116 /* Return true if T is an INTEGER_CST or POLY_INT_CST whose numerical
7117 value (extended according to TYPE_UNSIGNED) fits in a poly_int64. */
7119 bool
7120 tree_fits_poly_int64_p (const_tree t)
7122 if (t == NULL_TREE)
7123 return false;
7124 if (POLY_INT_CST_P (t))
7126 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; i++)
7127 if (!wi::fits_shwi_p (wi::to_wide (POLY_INT_CST_COEFF (t, i))))
7128 return false;
7129 return true;
7131 return (TREE_CODE (t) == INTEGER_CST
7132 && wi::fits_shwi_p (wi::to_widest (t)));
7135 /* Return true if T is an INTEGER_CST whose numerical value (extended
7136 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
7138 bool
7139 tree_fits_uhwi_p (const_tree t)
7141 return (t != NULL_TREE
7142 && TREE_CODE (t) == INTEGER_CST
7143 && wi::fits_uhwi_p (wi::to_widest (t)));
7146 /* Return true if T is an INTEGER_CST or POLY_INT_CST whose numerical
7147 value (extended according to TYPE_UNSIGNED) fits in a poly_uint64. */
7149 bool
7150 tree_fits_poly_uint64_p (const_tree t)
7152 if (t == NULL_TREE)
7153 return false;
7154 if (POLY_INT_CST_P (t))
7156 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; i++)
7157 if (!wi::fits_uhwi_p (wi::to_widest (POLY_INT_CST_COEFF (t, i))))
7158 return false;
7159 return true;
7161 return (TREE_CODE (t) == INTEGER_CST
7162 && wi::fits_uhwi_p (wi::to_widest (t)));
7165 /* T is an INTEGER_CST whose numerical value (extended according to
7166 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
7167 HOST_WIDE_INT. */
7169 HOST_WIDE_INT
7170 tree_to_shwi (const_tree t)
7172 gcc_assert (tree_fits_shwi_p (t));
7173 return TREE_INT_CST_LOW (t);
7176 /* T is an INTEGER_CST whose numerical value (extended according to
7177 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
7178 HOST_WIDE_INT. */
7180 unsigned HOST_WIDE_INT
7181 tree_to_uhwi (const_tree t)
7183 gcc_assert (tree_fits_uhwi_p (t));
7184 return TREE_INT_CST_LOW (t);
7187 /* Return the most significant (sign) bit of T. */
7190 tree_int_cst_sign_bit (const_tree t)
7192 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
7194 return wi::extract_uhwi (wi::to_wide (t), bitno, 1);
7197 /* Return an indication of the sign of the integer constant T.
7198 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
7199 Note that -1 will never be returned if T's type is unsigned. */
7202 tree_int_cst_sgn (const_tree t)
7204 if (wi::to_wide (t) == 0)
7205 return 0;
7206 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
7207 return 1;
7208 else if (wi::neg_p (wi::to_wide (t)))
7209 return -1;
7210 else
7211 return 1;
7214 /* Return the minimum number of bits needed to represent VALUE in a
7215 signed or unsigned type, UNSIGNEDP says which. */
7217 unsigned int
7218 tree_int_cst_min_precision (tree value, signop sgn)
7220 /* If the value is negative, compute its negative minus 1. The latter
7221 adjustment is because the absolute value of the largest negative value
7222 is one larger than the largest positive value. This is equivalent to
7223 a bit-wise negation, so use that operation instead. */
7225 if (tree_int_cst_sgn (value) < 0)
7226 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
7228 /* Return the number of bits needed, taking into account the fact
7229 that we need one more bit for a signed than unsigned type.
7230 If value is 0 or -1, the minimum precision is 1 no matter
7231 whether unsignedp is true or false. */
7233 if (integer_zerop (value))
7234 return 1;
7235 else
7236 return tree_floor_log2 (value) + 1 + (sgn == SIGNED ? 1 : 0) ;
7239 /* Return truthvalue of whether T1 is the same tree structure as T2.
7240 Return 1 if they are the same.
7241 Return 0 if they are understandably different.
7242 Return -1 if either contains tree structure not understood by
7243 this function. */
7246 simple_cst_equal (const_tree t1, const_tree t2)
7248 enum tree_code code1, code2;
7249 int cmp;
7250 int i;
7252 if (t1 == t2)
7253 return 1;
7254 if (t1 == 0 || t2 == 0)
7255 return 0;
7257 code1 = TREE_CODE (t1);
7258 code2 = TREE_CODE (t2);
7260 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
7262 if (CONVERT_EXPR_CODE_P (code2)
7263 || code2 == NON_LVALUE_EXPR)
7264 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7265 else
7266 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
7269 else if (CONVERT_EXPR_CODE_P (code2)
7270 || code2 == NON_LVALUE_EXPR)
7271 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
7273 if (code1 != code2)
7274 return 0;
7276 switch (code1)
7278 case INTEGER_CST:
7279 return wi::to_widest (t1) == wi::to_widest (t2);
7281 case REAL_CST:
7282 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
7284 case FIXED_CST:
7285 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
7287 case STRING_CST:
7288 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
7289 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
7290 TREE_STRING_LENGTH (t1)));
7292 case CONSTRUCTOR:
7294 unsigned HOST_WIDE_INT idx;
7295 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
7296 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
7298 if (vec_safe_length (v1) != vec_safe_length (v2))
7299 return false;
7301 for (idx = 0; idx < vec_safe_length (v1); ++idx)
7302 /* ??? Should we handle also fields here? */
7303 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
7304 return false;
7305 return true;
7308 case SAVE_EXPR:
7309 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7311 case CALL_EXPR:
7312 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
7313 if (cmp <= 0)
7314 return cmp;
7315 if (call_expr_nargs (t1) != call_expr_nargs (t2))
7316 return 0;
7318 const_tree arg1, arg2;
7319 const_call_expr_arg_iterator iter1, iter2;
7320 for (arg1 = first_const_call_expr_arg (t1, &iter1),
7321 arg2 = first_const_call_expr_arg (t2, &iter2);
7322 arg1 && arg2;
7323 arg1 = next_const_call_expr_arg (&iter1),
7324 arg2 = next_const_call_expr_arg (&iter2))
7326 cmp = simple_cst_equal (arg1, arg2);
7327 if (cmp <= 0)
7328 return cmp;
7330 return arg1 == arg2;
7333 case TARGET_EXPR:
7334 /* Special case: if either target is an unallocated VAR_DECL,
7335 it means that it's going to be unified with whatever the
7336 TARGET_EXPR is really supposed to initialize, so treat it
7337 as being equivalent to anything. */
7338 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
7339 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
7340 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
7341 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
7342 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
7343 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
7344 cmp = 1;
7345 else
7346 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7348 if (cmp <= 0)
7349 return cmp;
7351 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
7353 case WITH_CLEANUP_EXPR:
7354 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7355 if (cmp <= 0)
7356 return cmp;
7358 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
7360 case COMPONENT_REF:
7361 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
7362 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7364 return 0;
7366 case VAR_DECL:
7367 case PARM_DECL:
7368 case CONST_DECL:
7369 case FUNCTION_DECL:
7370 return 0;
7372 default:
7373 if (POLY_INT_CST_P (t1))
7374 /* A false return means maybe_ne rather than known_ne. */
7375 return known_eq (poly_widest_int::from (poly_int_cst_value (t1),
7376 TYPE_SIGN (TREE_TYPE (t1))),
7377 poly_widest_int::from (poly_int_cst_value (t2),
7378 TYPE_SIGN (TREE_TYPE (t2))));
7379 break;
7382 /* This general rule works for most tree codes. All exceptions should be
7383 handled above. If this is a language-specific tree code, we can't
7384 trust what might be in the operand, so say we don't know
7385 the situation. */
7386 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
7387 return -1;
7389 switch (TREE_CODE_CLASS (code1))
7391 case tcc_unary:
7392 case tcc_binary:
7393 case tcc_comparison:
7394 case tcc_expression:
7395 case tcc_reference:
7396 case tcc_statement:
7397 cmp = 1;
7398 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
7400 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
7401 if (cmp <= 0)
7402 return cmp;
7405 return cmp;
7407 default:
7408 return -1;
7412 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
7413 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
7414 than U, respectively. */
7417 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
7419 if (tree_int_cst_sgn (t) < 0)
7420 return -1;
7421 else if (!tree_fits_uhwi_p (t))
7422 return 1;
7423 else if (TREE_INT_CST_LOW (t) == u)
7424 return 0;
7425 else if (TREE_INT_CST_LOW (t) < u)
7426 return -1;
7427 else
7428 return 1;
7431 /* Return true if SIZE represents a constant size that is in bounds of
7432 what the middle-end and the backend accepts (covering not more than
7433 half of the address-space). */
7435 bool
7436 valid_constant_size_p (const_tree size)
7438 if (POLY_INT_CST_P (size))
7440 if (TREE_OVERFLOW (size))
7441 return false;
7442 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
7443 if (!valid_constant_size_p (POLY_INT_CST_COEFF (size, i)))
7444 return false;
7445 return true;
7447 if (! tree_fits_uhwi_p (size)
7448 || TREE_OVERFLOW (size)
7449 || tree_int_cst_sign_bit (size) != 0)
7450 return false;
7451 return true;
7454 /* Return the precision of the type, or for a complex or vector type the
7455 precision of the type of its elements. */
7457 unsigned int
7458 element_precision (const_tree type)
7460 if (!TYPE_P (type))
7461 type = TREE_TYPE (type);
7462 enum tree_code code = TREE_CODE (type);
7463 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
7464 type = TREE_TYPE (type);
7466 return TYPE_PRECISION (type);
7469 /* Return true if CODE represents an associative tree code. Otherwise
7470 return false. */
7471 bool
7472 associative_tree_code (enum tree_code code)
7474 switch (code)
7476 case BIT_IOR_EXPR:
7477 case BIT_AND_EXPR:
7478 case BIT_XOR_EXPR:
7479 case PLUS_EXPR:
7480 case MULT_EXPR:
7481 case MIN_EXPR:
7482 case MAX_EXPR:
7483 return true;
7485 default:
7486 break;
7488 return false;
7491 /* Return true if CODE represents a commutative tree code. Otherwise
7492 return false. */
7493 bool
7494 commutative_tree_code (enum tree_code code)
7496 switch (code)
7498 case PLUS_EXPR:
7499 case MULT_EXPR:
7500 case MULT_HIGHPART_EXPR:
7501 case MIN_EXPR:
7502 case MAX_EXPR:
7503 case BIT_IOR_EXPR:
7504 case BIT_XOR_EXPR:
7505 case BIT_AND_EXPR:
7506 case NE_EXPR:
7507 case EQ_EXPR:
7508 case UNORDERED_EXPR:
7509 case ORDERED_EXPR:
7510 case UNEQ_EXPR:
7511 case LTGT_EXPR:
7512 case TRUTH_AND_EXPR:
7513 case TRUTH_XOR_EXPR:
7514 case TRUTH_OR_EXPR:
7515 case WIDEN_MULT_EXPR:
7516 case VEC_WIDEN_MULT_HI_EXPR:
7517 case VEC_WIDEN_MULT_LO_EXPR:
7518 case VEC_WIDEN_MULT_EVEN_EXPR:
7519 case VEC_WIDEN_MULT_ODD_EXPR:
7520 return true;
7522 default:
7523 break;
7525 return false;
7528 /* Return true if CODE represents a ternary tree code for which the
7529 first two operands are commutative. Otherwise return false. */
7530 bool
7531 commutative_ternary_tree_code (enum tree_code code)
7533 switch (code)
7535 case WIDEN_MULT_PLUS_EXPR:
7536 case WIDEN_MULT_MINUS_EXPR:
7537 case DOT_PROD_EXPR:
7538 return true;
7540 default:
7541 break;
7543 return false;
7546 /* Returns true if CODE can overflow. */
7548 bool
7549 operation_can_overflow (enum tree_code code)
7551 switch (code)
7553 case PLUS_EXPR:
7554 case MINUS_EXPR:
7555 case MULT_EXPR:
7556 case LSHIFT_EXPR:
7557 /* Can overflow in various ways. */
7558 return true;
7559 case TRUNC_DIV_EXPR:
7560 case EXACT_DIV_EXPR:
7561 case FLOOR_DIV_EXPR:
7562 case CEIL_DIV_EXPR:
7563 /* For INT_MIN / -1. */
7564 return true;
7565 case NEGATE_EXPR:
7566 case ABS_EXPR:
7567 /* For -INT_MIN. */
7568 return true;
7569 default:
7570 /* These operators cannot overflow. */
7571 return false;
7575 /* Returns true if CODE operating on operands of type TYPE doesn't overflow, or
7576 ftrapv doesn't generate trapping insns for CODE. */
7578 bool
7579 operation_no_trapping_overflow (tree type, enum tree_code code)
7581 gcc_checking_assert (ANY_INTEGRAL_TYPE_P (type));
7583 /* We don't generate instructions that trap on overflow for complex or vector
7584 types. */
7585 if (!INTEGRAL_TYPE_P (type))
7586 return true;
7588 if (!TYPE_OVERFLOW_TRAPS (type))
7589 return true;
7591 switch (code)
7593 case PLUS_EXPR:
7594 case MINUS_EXPR:
7595 case MULT_EXPR:
7596 case NEGATE_EXPR:
7597 case ABS_EXPR:
7598 /* These operators can overflow, and -ftrapv generates trapping code for
7599 these. */
7600 return false;
7601 case TRUNC_DIV_EXPR:
7602 case EXACT_DIV_EXPR:
7603 case FLOOR_DIV_EXPR:
7604 case CEIL_DIV_EXPR:
7605 case LSHIFT_EXPR:
7606 /* These operators can overflow, but -ftrapv does not generate trapping
7607 code for these. */
7608 return true;
7609 default:
7610 /* These operators cannot overflow. */
7611 return true;
7615 namespace inchash
7618 /* Generate a hash value for an expression. This can be used iteratively
7619 by passing a previous result as the HSTATE argument.
7621 This function is intended to produce the same hash for expressions which
7622 would compare equal using operand_equal_p. */
7623 void
7624 add_expr (const_tree t, inchash::hash &hstate, unsigned int flags)
7626 int i;
7627 enum tree_code code;
7628 enum tree_code_class tclass;
7630 if (t == NULL_TREE || t == error_mark_node)
7632 hstate.merge_hash (0);
7633 return;
7636 if (!(flags & OEP_ADDRESS_OF))
7637 STRIP_NOPS (t);
7639 code = TREE_CODE (t);
7641 switch (code)
7643 /* Alas, constants aren't shared, so we can't rely on pointer
7644 identity. */
7645 case VOID_CST:
7646 hstate.merge_hash (0);
7647 return;
7648 case INTEGER_CST:
7649 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
7650 for (i = 0; i < TREE_INT_CST_EXT_NUNITS (t); i++)
7651 hstate.add_hwi (TREE_INT_CST_ELT (t, i));
7652 return;
7653 case REAL_CST:
7655 unsigned int val2;
7656 if (!HONOR_SIGNED_ZEROS (t) && real_zerop (t))
7657 val2 = rvc_zero;
7658 else
7659 val2 = real_hash (TREE_REAL_CST_PTR (t));
7660 hstate.merge_hash (val2);
7661 return;
7663 case FIXED_CST:
7665 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7666 hstate.merge_hash (val2);
7667 return;
7669 case STRING_CST:
7670 hstate.add ((const void *) TREE_STRING_POINTER (t),
7671 TREE_STRING_LENGTH (t));
7672 return;
7673 case COMPLEX_CST:
7674 inchash::add_expr (TREE_REALPART (t), hstate, flags);
7675 inchash::add_expr (TREE_IMAGPART (t), hstate, flags);
7676 return;
7677 case VECTOR_CST:
7679 hstate.add_int (VECTOR_CST_NPATTERNS (t));
7680 hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t));
7681 unsigned int count = vector_cst_encoded_nelts (t);
7682 for (unsigned int i = 0; i < count; ++i)
7683 inchash::add_expr (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags);
7684 return;
7686 case SSA_NAME:
7687 /* We can just compare by pointer. */
7688 hstate.add_hwi (SSA_NAME_VERSION (t));
7689 return;
7690 case PLACEHOLDER_EXPR:
7691 /* The node itself doesn't matter. */
7692 return;
7693 case BLOCK:
7694 case OMP_CLAUSE:
7695 /* Ignore. */
7696 return;
7697 case TREE_LIST:
7698 /* A list of expressions, for a CALL_EXPR or as the elements of a
7699 VECTOR_CST. */
7700 for (; t; t = TREE_CHAIN (t))
7701 inchash::add_expr (TREE_VALUE (t), hstate, flags);
7702 return;
7703 case CONSTRUCTOR:
7705 unsigned HOST_WIDE_INT idx;
7706 tree field, value;
7707 flags &= ~OEP_ADDRESS_OF;
7708 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7710 inchash::add_expr (field, hstate, flags);
7711 inchash::add_expr (value, hstate, flags);
7713 return;
7715 case STATEMENT_LIST:
7717 tree_stmt_iterator i;
7718 for (i = tsi_start (CONST_CAST_TREE (t));
7719 !tsi_end_p (i); tsi_next (&i))
7720 inchash::add_expr (tsi_stmt (i), hstate, flags);
7721 return;
7723 case TREE_VEC:
7724 for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
7725 inchash::add_expr (TREE_VEC_ELT (t, i), hstate, flags);
7726 return;
7727 case IDENTIFIER_NODE:
7728 hstate.add_object (IDENTIFIER_HASH_VALUE (t));
7729 return;
7730 case FUNCTION_DECL:
7731 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7732 Otherwise nodes that compare equal according to operand_equal_p might
7733 get different hash codes. However, don't do this for machine specific
7734 or front end builtins, since the function code is overloaded in those
7735 cases. */
7736 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7737 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7739 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7740 code = TREE_CODE (t);
7742 /* FALL THROUGH */
7743 default:
7744 if (POLY_INT_CST_P (t))
7746 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
7747 hstate.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t, i)));
7748 return;
7750 tclass = TREE_CODE_CLASS (code);
7752 if (tclass == tcc_declaration)
7754 /* DECL's have a unique ID */
7755 hstate.add_hwi (DECL_UID (t));
7757 else if (tclass == tcc_comparison && !commutative_tree_code (code))
7759 /* For comparisons that can be swapped, use the lower
7760 tree code. */
7761 enum tree_code ccode = swap_tree_comparison (code);
7762 if (code < ccode)
7763 ccode = code;
7764 hstate.add_object (ccode);
7765 inchash::add_expr (TREE_OPERAND (t, ccode != code), hstate, flags);
7766 inchash::add_expr (TREE_OPERAND (t, ccode == code), hstate, flags);
7768 else if (CONVERT_EXPR_CODE_P (code))
7770 /* NOP_EXPR and CONVERT_EXPR are considered equal by
7771 operand_equal_p. */
7772 enum tree_code ccode = NOP_EXPR;
7773 hstate.add_object (ccode);
7775 /* Don't hash the type, that can lead to having nodes which
7776 compare equal according to operand_equal_p, but which
7777 have different hash codes. Make sure to include signedness
7778 in the hash computation. */
7779 hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
7780 inchash::add_expr (TREE_OPERAND (t, 0), hstate, flags);
7782 /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
7783 else if (code == MEM_REF
7784 && (flags & OEP_ADDRESS_OF) != 0
7785 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
7786 && DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
7787 && integer_zerop (TREE_OPERAND (t, 1)))
7788 inchash::add_expr (TREE_OPERAND (TREE_OPERAND (t, 0), 0),
7789 hstate, flags);
7790 /* Don't ICE on FE specific trees, or their arguments etc.
7791 during operand_equal_p hash verification. */
7792 else if (!IS_EXPR_CODE_CLASS (tclass))
7793 gcc_assert (flags & OEP_HASH_CHECK);
7794 else
7796 unsigned int sflags = flags;
7798 hstate.add_object (code);
7800 switch (code)
7802 case ADDR_EXPR:
7803 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
7804 flags |= OEP_ADDRESS_OF;
7805 sflags = flags;
7806 break;
7808 case INDIRECT_REF:
7809 case MEM_REF:
7810 case TARGET_MEM_REF:
7811 flags &= ~OEP_ADDRESS_OF;
7812 sflags = flags;
7813 break;
7815 case ARRAY_REF:
7816 case ARRAY_RANGE_REF:
7817 case COMPONENT_REF:
7818 case BIT_FIELD_REF:
7819 sflags &= ~OEP_ADDRESS_OF;
7820 break;
7822 case COND_EXPR:
7823 flags &= ~OEP_ADDRESS_OF;
7824 break;
7826 case WIDEN_MULT_PLUS_EXPR:
7827 case WIDEN_MULT_MINUS_EXPR:
7829 /* The multiplication operands are commutative. */
7830 inchash::hash one, two;
7831 inchash::add_expr (TREE_OPERAND (t, 0), one, flags);
7832 inchash::add_expr (TREE_OPERAND (t, 1), two, flags);
7833 hstate.add_commutative (one, two);
7834 inchash::add_expr (TREE_OPERAND (t, 2), two, flags);
7835 return;
7838 case CALL_EXPR:
7839 if (CALL_EXPR_FN (t) == NULL_TREE)
7840 hstate.add_int (CALL_EXPR_IFN (t));
7841 break;
7843 case TARGET_EXPR:
7844 /* For TARGET_EXPR, just hash on the TARGET_EXPR_SLOT.
7845 Usually different TARGET_EXPRs just should use
7846 different temporaries in their slots. */
7847 inchash::add_expr (TARGET_EXPR_SLOT (t), hstate, flags);
7848 return;
7850 default:
7851 break;
7854 /* Don't hash the type, that can lead to having nodes which
7855 compare equal according to operand_equal_p, but which
7856 have different hash codes. */
7857 if (code == NON_LVALUE_EXPR)
7859 /* Make sure to include signness in the hash computation. */
7860 hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
7861 inchash::add_expr (TREE_OPERAND (t, 0), hstate, flags);
7864 else if (commutative_tree_code (code))
7866 /* It's a commutative expression. We want to hash it the same
7867 however it appears. We do this by first hashing both operands
7868 and then rehashing based on the order of their independent
7869 hashes. */
7870 inchash::hash one, two;
7871 inchash::add_expr (TREE_OPERAND (t, 0), one, flags);
7872 inchash::add_expr (TREE_OPERAND (t, 1), two, flags);
7873 hstate.add_commutative (one, two);
7875 else
7876 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7877 inchash::add_expr (TREE_OPERAND (t, i), hstate,
7878 i == 0 ? flags : sflags);
7880 return;
7886 /* Constructors for pointer, array and function types.
7887 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7888 constructed by language-dependent code, not here.) */
7890 /* Construct, lay out and return the type of pointers to TO_TYPE with
7891 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7892 reference all of memory. If such a type has already been
7893 constructed, reuse it. */
7895 tree
7896 build_pointer_type_for_mode (tree to_type, machine_mode mode,
7897 bool can_alias_all)
7899 tree t;
7900 bool could_alias = can_alias_all;
7902 if (to_type == error_mark_node)
7903 return error_mark_node;
7905 /* If the pointed-to type has the may_alias attribute set, force
7906 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7907 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7908 can_alias_all = true;
7910 /* In some cases, languages will have things that aren't a POINTER_TYPE
7911 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7912 In that case, return that type without regard to the rest of our
7913 operands.
7915 ??? This is a kludge, but consistent with the way this function has
7916 always operated and there doesn't seem to be a good way to avoid this
7917 at the moment. */
7918 if (TYPE_POINTER_TO (to_type) != 0
7919 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7920 return TYPE_POINTER_TO (to_type);
7922 /* First, if we already have a type for pointers to TO_TYPE and it's
7923 the proper mode, use it. */
7924 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7925 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7926 return t;
7928 t = make_node (POINTER_TYPE);
7930 TREE_TYPE (t) = to_type;
7931 SET_TYPE_MODE (t, mode);
7932 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7933 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7934 TYPE_POINTER_TO (to_type) = t;
7936 /* During LTO we do not set TYPE_CANONICAL of pointers and references. */
7937 if (TYPE_STRUCTURAL_EQUALITY_P (to_type) || in_lto_p)
7938 SET_TYPE_STRUCTURAL_EQUALITY (t);
7939 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
7940 TYPE_CANONICAL (t)
7941 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7942 mode, false);
7944 /* Lay out the type. This function has many callers that are concerned
7945 with expression-construction, and this simplifies them all. */
7946 layout_type (t);
7948 return t;
7951 /* By default build pointers in ptr_mode. */
7953 tree
7954 build_pointer_type (tree to_type)
7956 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7957 : TYPE_ADDR_SPACE (to_type);
7958 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7959 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7962 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7964 tree
7965 build_reference_type_for_mode (tree to_type, machine_mode mode,
7966 bool can_alias_all)
7968 tree t;
7969 bool could_alias = can_alias_all;
7971 if (to_type == error_mark_node)
7972 return error_mark_node;
7974 /* If the pointed-to type has the may_alias attribute set, force
7975 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7976 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7977 can_alias_all = true;
7979 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7980 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7981 In that case, return that type without regard to the rest of our
7982 operands.
7984 ??? This is a kludge, but consistent with the way this function has
7985 always operated and there doesn't seem to be a good way to avoid this
7986 at the moment. */
7987 if (TYPE_REFERENCE_TO (to_type) != 0
7988 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7989 return TYPE_REFERENCE_TO (to_type);
7991 /* First, if we already have a type for pointers to TO_TYPE and it's
7992 the proper mode, use it. */
7993 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7994 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7995 return t;
7997 t = make_node (REFERENCE_TYPE);
7999 TREE_TYPE (t) = to_type;
8000 SET_TYPE_MODE (t, mode);
8001 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
8002 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
8003 TYPE_REFERENCE_TO (to_type) = t;
8005 /* During LTO we do not set TYPE_CANONICAL of pointers and references. */
8006 if (TYPE_STRUCTURAL_EQUALITY_P (to_type) || in_lto_p)
8007 SET_TYPE_STRUCTURAL_EQUALITY (t);
8008 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
8009 TYPE_CANONICAL (t)
8010 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
8011 mode, false);
8013 layout_type (t);
8015 return t;
8019 /* Build the node for the type of references-to-TO_TYPE by default
8020 in ptr_mode. */
8022 tree
8023 build_reference_type (tree to_type)
8025 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
8026 : TYPE_ADDR_SPACE (to_type);
8027 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
8028 return build_reference_type_for_mode (to_type, pointer_mode, false);
8031 #define MAX_INT_CACHED_PREC \
8032 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
8033 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
8035 /* Builds a signed or unsigned integer type of precision PRECISION.
8036 Used for C bitfields whose precision does not match that of
8037 built-in target types. */
8038 tree
8039 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
8040 int unsignedp)
8042 tree itype, ret;
8044 if (unsignedp)
8045 unsignedp = MAX_INT_CACHED_PREC + 1;
8047 if (precision <= MAX_INT_CACHED_PREC)
8049 itype = nonstandard_integer_type_cache[precision + unsignedp];
8050 if (itype)
8051 return itype;
8054 itype = make_node (INTEGER_TYPE);
8055 TYPE_PRECISION (itype) = precision;
8057 if (unsignedp)
8058 fixup_unsigned_type (itype);
8059 else
8060 fixup_signed_type (itype);
8062 ret = itype;
8064 inchash::hash hstate;
8065 inchash::add_expr (TYPE_MAX_VALUE (itype), hstate);
8066 ret = type_hash_canon (hstate.end (), itype);
8067 if (precision <= MAX_INT_CACHED_PREC)
8068 nonstandard_integer_type_cache[precision + unsignedp] = ret;
8070 return ret;
8073 #define MAX_BOOL_CACHED_PREC \
8074 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
8075 static GTY(()) tree nonstandard_boolean_type_cache[MAX_BOOL_CACHED_PREC + 1];
8077 /* Builds a boolean type of precision PRECISION.
8078 Used for boolean vectors to choose proper vector element size. */
8079 tree
8080 build_nonstandard_boolean_type (unsigned HOST_WIDE_INT precision)
8082 tree type;
8084 if (precision <= MAX_BOOL_CACHED_PREC)
8086 type = nonstandard_boolean_type_cache[precision];
8087 if (type)
8088 return type;
8091 type = make_node (BOOLEAN_TYPE);
8092 TYPE_PRECISION (type) = precision;
8093 fixup_signed_type (type);
8095 if (precision <= MAX_INT_CACHED_PREC)
8096 nonstandard_boolean_type_cache[precision] = type;
8098 return type;
8101 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
8102 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
8103 is true, reuse such a type that has already been constructed. */
8105 static tree
8106 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
8108 tree itype = make_node (INTEGER_TYPE);
8110 TREE_TYPE (itype) = type;
8112 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
8113 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
8115 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
8116 SET_TYPE_MODE (itype, TYPE_MODE (type));
8117 TYPE_SIZE (itype) = TYPE_SIZE (type);
8118 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
8119 SET_TYPE_ALIGN (itype, TYPE_ALIGN (type));
8120 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
8121 SET_TYPE_WARN_IF_NOT_ALIGN (itype, TYPE_WARN_IF_NOT_ALIGN (type));
8123 if (!shared)
8124 return itype;
8126 if ((TYPE_MIN_VALUE (itype)
8127 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
8128 || (TYPE_MAX_VALUE (itype)
8129 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
8131 /* Since we cannot reliably merge this type, we need to compare it using
8132 structural equality checks. */
8133 SET_TYPE_STRUCTURAL_EQUALITY (itype);
8134 return itype;
8137 hashval_t hash = type_hash_canon_hash (itype);
8138 itype = type_hash_canon (hash, itype);
8140 return itype;
8143 /* Wrapper around build_range_type_1 with SHARED set to true. */
8145 tree
8146 build_range_type (tree type, tree lowval, tree highval)
8148 return build_range_type_1 (type, lowval, highval, true);
8151 /* Wrapper around build_range_type_1 with SHARED set to false. */
8153 tree
8154 build_nonshared_range_type (tree type, tree lowval, tree highval)
8156 return build_range_type_1 (type, lowval, highval, false);
8159 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
8160 MAXVAL should be the maximum value in the domain
8161 (one less than the length of the array).
8163 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
8164 We don't enforce this limit, that is up to caller (e.g. language front end).
8165 The limit exists because the result is a signed type and we don't handle
8166 sizes that use more than one HOST_WIDE_INT. */
8168 tree
8169 build_index_type (tree maxval)
8171 return build_range_type (sizetype, size_zero_node, maxval);
8174 /* Return true if the debug information for TYPE, a subtype, should be emitted
8175 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
8176 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
8177 debug info and doesn't reflect the source code. */
8179 bool
8180 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
8182 tree base_type = TREE_TYPE (type), low, high;
8184 /* Subrange types have a base type which is an integral type. */
8185 if (!INTEGRAL_TYPE_P (base_type))
8186 return false;
8188 /* Get the real bounds of the subtype. */
8189 if (lang_hooks.types.get_subrange_bounds)
8190 lang_hooks.types.get_subrange_bounds (type, &low, &high);
8191 else
8193 low = TYPE_MIN_VALUE (type);
8194 high = TYPE_MAX_VALUE (type);
8197 /* If the type and its base type have the same representation and the same
8198 name, then the type is not a subrange but a copy of the base type. */
8199 if ((TREE_CODE (base_type) == INTEGER_TYPE
8200 || TREE_CODE (base_type) == BOOLEAN_TYPE)
8201 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
8202 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
8203 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type))
8204 && TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (base_type))
8205 return false;
8207 if (lowval)
8208 *lowval = low;
8209 if (highval)
8210 *highval = high;
8211 return true;
8214 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
8215 and number of elements specified by the range of values of INDEX_TYPE.
8216 If TYPELESS_STORAGE is true, TYPE_TYPELESS_STORAGE flag is set on the type.
8217 If SHARED is true, reuse such a type that has already been constructed. */
8219 static tree
8220 build_array_type_1 (tree elt_type, tree index_type, bool typeless_storage,
8221 bool shared)
8223 tree t;
8225 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
8227 error ("arrays of functions are not meaningful");
8228 elt_type = integer_type_node;
8231 t = make_node (ARRAY_TYPE);
8232 TREE_TYPE (t) = elt_type;
8233 TYPE_DOMAIN (t) = index_type;
8234 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
8235 TYPE_TYPELESS_STORAGE (t) = typeless_storage;
8236 layout_type (t);
8238 /* If the element type is incomplete at this point we get marked for
8239 structural equality. Do not record these types in the canonical
8240 type hashtable. */
8241 if (TYPE_STRUCTURAL_EQUALITY_P (t))
8242 return t;
8244 if (shared)
8246 hashval_t hash = type_hash_canon_hash (t);
8247 t = type_hash_canon (hash, t);
8250 if (TYPE_CANONICAL (t) == t)
8252 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
8253 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type))
8254 || in_lto_p)
8255 SET_TYPE_STRUCTURAL_EQUALITY (t);
8256 else if (TYPE_CANONICAL (elt_type) != elt_type
8257 || (index_type && TYPE_CANONICAL (index_type) != index_type))
8258 TYPE_CANONICAL (t)
8259 = build_array_type_1 (TYPE_CANONICAL (elt_type),
8260 index_type
8261 ? TYPE_CANONICAL (index_type) : NULL_TREE,
8262 typeless_storage, shared);
8265 return t;
8268 /* Wrapper around build_array_type_1 with SHARED set to true. */
8270 tree
8271 build_array_type (tree elt_type, tree index_type, bool typeless_storage)
8273 return build_array_type_1 (elt_type, index_type, typeless_storage, true);
8276 /* Wrapper around build_array_type_1 with SHARED set to false. */
8278 tree
8279 build_nonshared_array_type (tree elt_type, tree index_type)
8281 return build_array_type_1 (elt_type, index_type, false, false);
8284 /* Return a representation of ELT_TYPE[NELTS], using indices of type
8285 sizetype. */
8287 tree
8288 build_array_type_nelts (tree elt_type, poly_uint64 nelts)
8290 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
8293 /* Recursively examines the array elements of TYPE, until a non-array
8294 element type is found. */
8296 tree
8297 strip_array_types (tree type)
8299 while (TREE_CODE (type) == ARRAY_TYPE)
8300 type = TREE_TYPE (type);
8302 return type;
8305 /* Computes the canonical argument types from the argument type list
8306 ARGTYPES.
8308 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
8309 on entry to this function, or if any of the ARGTYPES are
8310 structural.
8312 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
8313 true on entry to this function, or if any of the ARGTYPES are
8314 non-canonical.
8316 Returns a canonical argument list, which may be ARGTYPES when the
8317 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
8318 true) or would not differ from ARGTYPES. */
8320 static tree
8321 maybe_canonicalize_argtypes (tree argtypes,
8322 bool *any_structural_p,
8323 bool *any_noncanonical_p)
8325 tree arg;
8326 bool any_noncanonical_argtypes_p = false;
8328 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
8330 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
8331 /* Fail gracefully by stating that the type is structural. */
8332 *any_structural_p = true;
8333 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
8334 *any_structural_p = true;
8335 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
8336 || TREE_PURPOSE (arg))
8337 /* If the argument has a default argument, we consider it
8338 non-canonical even though the type itself is canonical.
8339 That way, different variants of function and method types
8340 with default arguments will all point to the variant with
8341 no defaults as their canonical type. */
8342 any_noncanonical_argtypes_p = true;
8345 if (*any_structural_p)
8346 return argtypes;
8348 if (any_noncanonical_argtypes_p)
8350 /* Build the canonical list of argument types. */
8351 tree canon_argtypes = NULL_TREE;
8352 bool is_void = false;
8354 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
8356 if (arg == void_list_node)
8357 is_void = true;
8358 else
8359 canon_argtypes = tree_cons (NULL_TREE,
8360 TYPE_CANONICAL (TREE_VALUE (arg)),
8361 canon_argtypes);
8364 canon_argtypes = nreverse (canon_argtypes);
8365 if (is_void)
8366 canon_argtypes = chainon (canon_argtypes, void_list_node);
8368 /* There is a non-canonical type. */
8369 *any_noncanonical_p = true;
8370 return canon_argtypes;
8373 /* The canonical argument types are the same as ARGTYPES. */
8374 return argtypes;
8377 /* Construct, lay out and return
8378 the type of functions returning type VALUE_TYPE
8379 given arguments of types ARG_TYPES.
8380 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
8381 are data type nodes for the arguments of the function.
8382 If such a type has already been constructed, reuse it. */
8384 tree
8385 build_function_type (tree value_type, tree arg_types)
8387 tree t;
8388 inchash::hash hstate;
8389 bool any_structural_p, any_noncanonical_p;
8390 tree canon_argtypes;
8392 if (TREE_CODE (value_type) == FUNCTION_TYPE)
8394 error ("function return type cannot be function");
8395 value_type = integer_type_node;
8398 /* Make a node of the sort we want. */
8399 t = make_node (FUNCTION_TYPE);
8400 TREE_TYPE (t) = value_type;
8401 TYPE_ARG_TYPES (t) = arg_types;
8403 /* If we already have such a type, use the old one. */
8404 hashval_t hash = type_hash_canon_hash (t);
8405 t = type_hash_canon (hash, t);
8407 /* Set up the canonical type. */
8408 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
8409 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
8410 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
8411 &any_structural_p,
8412 &any_noncanonical_p);
8413 if (any_structural_p)
8414 SET_TYPE_STRUCTURAL_EQUALITY (t);
8415 else if (any_noncanonical_p)
8416 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
8417 canon_argtypes);
8419 if (!COMPLETE_TYPE_P (t))
8420 layout_type (t);
8421 return t;
8424 /* Build a function type. The RETURN_TYPE is the type returned by the
8425 function. If VAARGS is set, no void_type_node is appended to the
8426 list. ARGP must be always be terminated be a NULL_TREE. */
8428 static tree
8429 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
8431 tree t, args, last;
8433 t = va_arg (argp, tree);
8434 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
8435 args = tree_cons (NULL_TREE, t, args);
8437 if (vaargs)
8439 last = args;
8440 if (args != NULL_TREE)
8441 args = nreverse (args);
8442 gcc_assert (last != void_list_node);
8444 else if (args == NULL_TREE)
8445 args = void_list_node;
8446 else
8448 last = args;
8449 args = nreverse (args);
8450 TREE_CHAIN (last) = void_list_node;
8452 args = build_function_type (return_type, args);
8454 return args;
8457 /* Build a function type. The RETURN_TYPE is the type returned by the
8458 function. If additional arguments are provided, they are
8459 additional argument types. The list of argument types must always
8460 be terminated by NULL_TREE. */
8462 tree
8463 build_function_type_list (tree return_type, ...)
8465 tree args;
8466 va_list p;
8468 va_start (p, return_type);
8469 args = build_function_type_list_1 (false, return_type, p);
8470 va_end (p);
8471 return args;
8474 /* Build a variable argument function type. The RETURN_TYPE is the
8475 type returned by the function. If additional arguments are provided,
8476 they are additional argument types. The list of argument types must
8477 always be terminated by NULL_TREE. */
8479 tree
8480 build_varargs_function_type_list (tree return_type, ...)
8482 tree args;
8483 va_list p;
8485 va_start (p, return_type);
8486 args = build_function_type_list_1 (true, return_type, p);
8487 va_end (p);
8489 return args;
8492 /* Build a function type. RETURN_TYPE is the type returned by the
8493 function; VAARGS indicates whether the function takes varargs. The
8494 function takes N named arguments, the types of which are provided in
8495 ARG_TYPES. */
8497 static tree
8498 build_function_type_array_1 (bool vaargs, tree return_type, int n,
8499 tree *arg_types)
8501 int i;
8502 tree t = vaargs ? NULL_TREE : void_list_node;
8504 for (i = n - 1; i >= 0; i--)
8505 t = tree_cons (NULL_TREE, arg_types[i], t);
8507 return build_function_type (return_type, t);
8510 /* Build a function type. RETURN_TYPE is the type returned by the
8511 function. The function takes N named arguments, the types of which
8512 are provided in ARG_TYPES. */
8514 tree
8515 build_function_type_array (tree return_type, int n, tree *arg_types)
8517 return build_function_type_array_1 (false, return_type, n, arg_types);
8520 /* Build a variable argument function type. RETURN_TYPE is the type
8521 returned by the function. The function takes N named arguments, the
8522 types of which are provided in ARG_TYPES. */
8524 tree
8525 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8527 return build_function_type_array_1 (true, return_type, n, arg_types);
8530 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8531 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8532 for the method. An implicit additional parameter (of type
8533 pointer-to-BASETYPE) is added to the ARGTYPES. */
8535 tree
8536 build_method_type_directly (tree basetype,
8537 tree rettype,
8538 tree argtypes)
8540 tree t;
8541 tree ptype;
8542 bool any_structural_p, any_noncanonical_p;
8543 tree canon_argtypes;
8545 /* Make a node of the sort we want. */
8546 t = make_node (METHOD_TYPE);
8548 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8549 TREE_TYPE (t) = rettype;
8550 ptype = build_pointer_type (basetype);
8552 /* The actual arglist for this function includes a "hidden" argument
8553 which is "this". Put it into the list of argument types. */
8554 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8555 TYPE_ARG_TYPES (t) = argtypes;
8557 /* If we already have such a type, use the old one. */
8558 hashval_t hash = type_hash_canon_hash (t);
8559 t = type_hash_canon (hash, t);
8561 /* Set up the canonical type. */
8562 any_structural_p
8563 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8564 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8565 any_noncanonical_p
8566 = (TYPE_CANONICAL (basetype) != basetype
8567 || TYPE_CANONICAL (rettype) != rettype);
8568 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8569 &any_structural_p,
8570 &any_noncanonical_p);
8571 if (any_structural_p)
8572 SET_TYPE_STRUCTURAL_EQUALITY (t);
8573 else if (any_noncanonical_p)
8574 TYPE_CANONICAL (t)
8575 = build_method_type_directly (TYPE_CANONICAL (basetype),
8576 TYPE_CANONICAL (rettype),
8577 canon_argtypes);
8578 if (!COMPLETE_TYPE_P (t))
8579 layout_type (t);
8581 return t;
8584 /* Construct, lay out and return the type of methods belonging to class
8585 BASETYPE and whose arguments and values are described by TYPE.
8586 If that type exists already, reuse it.
8587 TYPE must be a FUNCTION_TYPE node. */
8589 tree
8590 build_method_type (tree basetype, tree type)
8592 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8594 return build_method_type_directly (basetype,
8595 TREE_TYPE (type),
8596 TYPE_ARG_TYPES (type));
8599 /* Construct, lay out and return the type of offsets to a value
8600 of type TYPE, within an object of type BASETYPE.
8601 If a suitable offset type exists already, reuse it. */
8603 tree
8604 build_offset_type (tree basetype, tree type)
8606 tree t;
8608 /* Make a node of the sort we want. */
8609 t = make_node (OFFSET_TYPE);
8611 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8612 TREE_TYPE (t) = type;
8614 /* If we already have such a type, use the old one. */
8615 hashval_t hash = type_hash_canon_hash (t);
8616 t = type_hash_canon (hash, t);
8618 if (!COMPLETE_TYPE_P (t))
8619 layout_type (t);
8621 if (TYPE_CANONICAL (t) == t)
8623 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8624 || TYPE_STRUCTURAL_EQUALITY_P (type))
8625 SET_TYPE_STRUCTURAL_EQUALITY (t);
8626 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8627 || TYPE_CANONICAL (type) != type)
8628 TYPE_CANONICAL (t)
8629 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8630 TYPE_CANONICAL (type));
8633 return t;
8636 /* Create a complex type whose components are COMPONENT_TYPE.
8638 If NAMED is true, the type is given a TYPE_NAME. We do not always
8639 do so because this creates a DECL node and thus make the DECL_UIDs
8640 dependent on the type canonicalization hashtable, which is GC-ed,
8641 so the DECL_UIDs would not be stable wrt garbage collection. */
8643 tree
8644 build_complex_type (tree component_type, bool named)
8646 gcc_assert (INTEGRAL_TYPE_P (component_type)
8647 || SCALAR_FLOAT_TYPE_P (component_type)
8648 || FIXED_POINT_TYPE_P (component_type));
8650 /* Make a node of the sort we want. */
8651 tree probe = make_node (COMPLEX_TYPE);
8653 TREE_TYPE (probe) = TYPE_MAIN_VARIANT (component_type);
8655 /* If we already have such a type, use the old one. */
8656 hashval_t hash = type_hash_canon_hash (probe);
8657 tree t = type_hash_canon (hash, probe);
8659 if (t == probe)
8661 /* We created a new type. The hash insertion will have laid
8662 out the type. We need to check the canonicalization and
8663 maybe set the name. */
8664 gcc_checking_assert (COMPLETE_TYPE_P (t)
8665 && !TYPE_NAME (t)
8666 && TYPE_CANONICAL (t) == t);
8668 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (t)))
8669 SET_TYPE_STRUCTURAL_EQUALITY (t);
8670 else if (TYPE_CANONICAL (TREE_TYPE (t)) != TREE_TYPE (t))
8671 TYPE_CANONICAL (t)
8672 = build_complex_type (TYPE_CANONICAL (TREE_TYPE (t)), named);
8674 /* We need to create a name, since complex is a fundamental type. */
8675 if (named)
8677 const char *name = NULL;
8679 if (TREE_TYPE (t) == char_type_node)
8680 name = "complex char";
8681 else if (TREE_TYPE (t) == signed_char_type_node)
8682 name = "complex signed char";
8683 else if (TREE_TYPE (t) == unsigned_char_type_node)
8684 name = "complex unsigned char";
8685 else if (TREE_TYPE (t) == short_integer_type_node)
8686 name = "complex short int";
8687 else if (TREE_TYPE (t) == short_unsigned_type_node)
8688 name = "complex short unsigned int";
8689 else if (TREE_TYPE (t) == integer_type_node)
8690 name = "complex int";
8691 else if (TREE_TYPE (t) == unsigned_type_node)
8692 name = "complex unsigned int";
8693 else if (TREE_TYPE (t) == long_integer_type_node)
8694 name = "complex long int";
8695 else if (TREE_TYPE (t) == long_unsigned_type_node)
8696 name = "complex long unsigned int";
8697 else if (TREE_TYPE (t) == long_long_integer_type_node)
8698 name = "complex long long int";
8699 else if (TREE_TYPE (t) == long_long_unsigned_type_node)
8700 name = "complex long long unsigned int";
8702 if (name != NULL)
8703 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8704 get_identifier (name), t);
8708 return build_qualified_type (t, TYPE_QUALS (component_type));
8711 /* If TYPE is a real or complex floating-point type and the target
8712 does not directly support arithmetic on TYPE then return the wider
8713 type to be used for arithmetic on TYPE. Otherwise, return
8714 NULL_TREE. */
8716 tree
8717 excess_precision_type (tree type)
8719 /* The target can give two different responses to the question of
8720 which excess precision mode it would like depending on whether we
8721 are in -fexcess-precision=standard or -fexcess-precision=fast. */
8723 enum excess_precision_type requested_type
8724 = (flag_excess_precision == EXCESS_PRECISION_FAST
8725 ? EXCESS_PRECISION_TYPE_FAST
8726 : EXCESS_PRECISION_TYPE_STANDARD);
8728 enum flt_eval_method target_flt_eval_method
8729 = targetm.c.excess_precision (requested_type);
8731 /* The target should not ask for unpredictable float evaluation (though
8732 it might advertise that implicitly the evaluation is unpredictable,
8733 but we don't care about that here, it will have been reported
8734 elsewhere). If it does ask for unpredictable evaluation, we have
8735 nothing to do here. */
8736 gcc_assert (target_flt_eval_method != FLT_EVAL_METHOD_UNPREDICTABLE);
8738 /* Nothing to do. The target has asked for all types we know about
8739 to be computed with their native precision and range. */
8740 if (target_flt_eval_method == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
8741 return NULL_TREE;
8743 /* The target will promote this type in a target-dependent way, so excess
8744 precision ought to leave it alone. */
8745 if (targetm.promoted_type (type) != NULL_TREE)
8746 return NULL_TREE;
8748 machine_mode float16_type_mode = (float16_type_node
8749 ? TYPE_MODE (float16_type_node)
8750 : VOIDmode);
8751 machine_mode float_type_mode = TYPE_MODE (float_type_node);
8752 machine_mode double_type_mode = TYPE_MODE (double_type_node);
8754 switch (TREE_CODE (type))
8756 case REAL_TYPE:
8758 machine_mode type_mode = TYPE_MODE (type);
8759 switch (target_flt_eval_method)
8761 case FLT_EVAL_METHOD_PROMOTE_TO_FLOAT:
8762 if (type_mode == float16_type_mode)
8763 return float_type_node;
8764 break;
8765 case FLT_EVAL_METHOD_PROMOTE_TO_DOUBLE:
8766 if (type_mode == float16_type_mode
8767 || type_mode == float_type_mode)
8768 return double_type_node;
8769 break;
8770 case FLT_EVAL_METHOD_PROMOTE_TO_LONG_DOUBLE:
8771 if (type_mode == float16_type_mode
8772 || type_mode == float_type_mode
8773 || type_mode == double_type_mode)
8774 return long_double_type_node;
8775 break;
8776 default:
8777 gcc_unreachable ();
8779 break;
8781 case COMPLEX_TYPE:
8783 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8784 return NULL_TREE;
8785 machine_mode type_mode = TYPE_MODE (TREE_TYPE (type));
8786 switch (target_flt_eval_method)
8788 case FLT_EVAL_METHOD_PROMOTE_TO_FLOAT:
8789 if (type_mode == float16_type_mode)
8790 return complex_float_type_node;
8791 break;
8792 case FLT_EVAL_METHOD_PROMOTE_TO_DOUBLE:
8793 if (type_mode == float16_type_mode
8794 || type_mode == float_type_mode)
8795 return complex_double_type_node;
8796 break;
8797 case FLT_EVAL_METHOD_PROMOTE_TO_LONG_DOUBLE:
8798 if (type_mode == float16_type_mode
8799 || type_mode == float_type_mode
8800 || type_mode == double_type_mode)
8801 return complex_long_double_type_node;
8802 break;
8803 default:
8804 gcc_unreachable ();
8806 break;
8808 default:
8809 break;
8812 return NULL_TREE;
8815 /* Return OP, stripped of any conversions to wider types as much as is safe.
8816 Converting the value back to OP's type makes a value equivalent to OP.
8818 If FOR_TYPE is nonzero, we return a value which, if converted to
8819 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8821 OP must have integer, real or enumeral type. Pointers are not allowed!
8823 There are some cases where the obvious value we could return
8824 would regenerate to OP if converted to OP's type,
8825 but would not extend like OP to wider types.
8826 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8827 For example, if OP is (unsigned short)(signed char)-1,
8828 we avoid returning (signed char)-1 if FOR_TYPE is int,
8829 even though extending that to an unsigned short would regenerate OP,
8830 since the result of extending (signed char)-1 to (int)
8831 is different from (int) OP. */
8833 tree
8834 get_unwidened (tree op, tree for_type)
8836 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8837 tree type = TREE_TYPE (op);
8838 unsigned final_prec
8839 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8840 int uns
8841 = (for_type != 0 && for_type != type
8842 && final_prec > TYPE_PRECISION (type)
8843 && TYPE_UNSIGNED (type));
8844 tree win = op;
8846 while (CONVERT_EXPR_P (op))
8848 int bitschange;
8850 /* TYPE_PRECISION on vector types has different meaning
8851 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8852 so avoid them here. */
8853 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8854 break;
8856 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8857 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8859 /* Truncations are many-one so cannot be removed.
8860 Unless we are later going to truncate down even farther. */
8861 if (bitschange < 0
8862 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8863 break;
8865 /* See what's inside this conversion. If we decide to strip it,
8866 we will set WIN. */
8867 op = TREE_OPERAND (op, 0);
8869 /* If we have not stripped any zero-extensions (uns is 0),
8870 we can strip any kind of extension.
8871 If we have previously stripped a zero-extension,
8872 only zero-extensions can safely be stripped.
8873 Any extension can be stripped if the bits it would produce
8874 are all going to be discarded later by truncating to FOR_TYPE. */
8876 if (bitschange > 0)
8878 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8879 win = op;
8880 /* TYPE_UNSIGNED says whether this is a zero-extension.
8881 Let's avoid computing it if it does not affect WIN
8882 and if UNS will not be needed again. */
8883 if ((uns
8884 || CONVERT_EXPR_P (op))
8885 && TYPE_UNSIGNED (TREE_TYPE (op)))
8887 uns = 1;
8888 win = op;
8893 /* If we finally reach a constant see if it fits in sth smaller and
8894 in that case convert it. */
8895 if (TREE_CODE (win) == INTEGER_CST)
8897 tree wtype = TREE_TYPE (win);
8898 unsigned prec = wi::min_precision (wi::to_wide (win), TYPE_SIGN (wtype));
8899 if (for_type)
8900 prec = MAX (prec, final_prec);
8901 if (prec < TYPE_PRECISION (wtype))
8903 tree t = lang_hooks.types.type_for_size (prec, TYPE_UNSIGNED (wtype));
8904 if (t && TYPE_PRECISION (t) < TYPE_PRECISION (wtype))
8905 win = fold_convert (t, win);
8909 return win;
8912 /* Return OP or a simpler expression for a narrower value
8913 which can be sign-extended or zero-extended to give back OP.
8914 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8915 or 0 if the value should be sign-extended. */
8917 tree
8918 get_narrower (tree op, int *unsignedp_ptr)
8920 int uns = 0;
8921 int first = 1;
8922 tree win = op;
8923 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8925 while (TREE_CODE (op) == NOP_EXPR)
8927 int bitschange
8928 = (TYPE_PRECISION (TREE_TYPE (op))
8929 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8931 /* Truncations are many-one so cannot be removed. */
8932 if (bitschange < 0)
8933 break;
8935 /* See what's inside this conversion. If we decide to strip it,
8936 we will set WIN. */
8938 if (bitschange > 0)
8940 op = TREE_OPERAND (op, 0);
8941 /* An extension: the outermost one can be stripped,
8942 but remember whether it is zero or sign extension. */
8943 if (first)
8944 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8945 /* Otherwise, if a sign extension has been stripped,
8946 only sign extensions can now be stripped;
8947 if a zero extension has been stripped, only zero-extensions. */
8948 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8949 break;
8950 first = 0;
8952 else /* bitschange == 0 */
8954 /* A change in nominal type can always be stripped, but we must
8955 preserve the unsignedness. */
8956 if (first)
8957 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8958 first = 0;
8959 op = TREE_OPERAND (op, 0);
8960 /* Keep trying to narrow, but don't assign op to win if it
8961 would turn an integral type into something else. */
8962 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8963 continue;
8966 win = op;
8969 if (TREE_CODE (op) == COMPONENT_REF
8970 /* Since type_for_size always gives an integer type. */
8971 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8972 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8973 /* Ensure field is laid out already. */
8974 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8975 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
8977 unsigned HOST_WIDE_INT innerprec
8978 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
8979 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8980 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8981 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8983 /* We can get this structure field in a narrower type that fits it,
8984 but the resulting extension to its nominal type (a fullword type)
8985 must satisfy the same conditions as for other extensions.
8987 Do this only for fields that are aligned (not bit-fields),
8988 because when bit-field insns will be used there is no
8989 advantage in doing this. */
8991 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8992 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8993 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8994 && type != 0)
8996 if (first)
8997 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8998 win = fold_convert (type, op);
9002 *unsignedp_ptr = uns;
9003 return win;
9006 /* Return true if integer constant C has a value that is permissible
9007 for TYPE, an integral type. */
9009 bool
9010 int_fits_type_p (const_tree c, const_tree type)
9012 tree type_low_bound, type_high_bound;
9013 bool ok_for_low_bound, ok_for_high_bound;
9014 signop sgn_c = TYPE_SIGN (TREE_TYPE (c));
9016 /* Non-standard boolean types can have arbitrary precision but various
9017 transformations assume that they can only take values 0 and +/-1. */
9018 if (TREE_CODE (type) == BOOLEAN_TYPE)
9019 return wi::fits_to_boolean_p (wi::to_wide (c), type);
9021 retry:
9022 type_low_bound = TYPE_MIN_VALUE (type);
9023 type_high_bound = TYPE_MAX_VALUE (type);
9025 /* If at least one bound of the type is a constant integer, we can check
9026 ourselves and maybe make a decision. If no such decision is possible, but
9027 this type is a subtype, try checking against that. Otherwise, use
9028 fits_to_tree_p, which checks against the precision.
9030 Compute the status for each possibly constant bound, and return if we see
9031 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
9032 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
9033 for "constant known to fit". */
9035 /* Check if c >= type_low_bound. */
9036 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
9038 if (tree_int_cst_lt (c, type_low_bound))
9039 return false;
9040 ok_for_low_bound = true;
9042 else
9043 ok_for_low_bound = false;
9045 /* Check if c <= type_high_bound. */
9046 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
9048 if (tree_int_cst_lt (type_high_bound, c))
9049 return false;
9050 ok_for_high_bound = true;
9052 else
9053 ok_for_high_bound = false;
9055 /* If the constant fits both bounds, the result is known. */
9056 if (ok_for_low_bound && ok_for_high_bound)
9057 return true;
9059 /* Perform some generic filtering which may allow making a decision
9060 even if the bounds are not constant. First, negative integers
9061 never fit in unsigned types, */
9062 if (TYPE_UNSIGNED (type) && sgn_c == SIGNED && wi::neg_p (wi::to_wide (c)))
9063 return false;
9065 /* Second, narrower types always fit in wider ones. */
9066 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
9067 return true;
9069 /* Third, unsigned integers with top bit set never fit signed types. */
9070 if (!TYPE_UNSIGNED (type) && sgn_c == UNSIGNED)
9072 int prec = GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (TREE_TYPE (c))) - 1;
9073 if (prec < TYPE_PRECISION (TREE_TYPE (c)))
9075 /* When a tree_cst is converted to a wide-int, the precision
9076 is taken from the type. However, if the precision of the
9077 mode underneath the type is smaller than that, it is
9078 possible that the value will not fit. The test below
9079 fails if any bit is set between the sign bit of the
9080 underlying mode and the top bit of the type. */
9081 if (wi::zext (wi::to_wide (c), prec - 1) != wi::to_wide (c))
9082 return false;
9084 else if (wi::neg_p (wi::to_wide (c)))
9085 return false;
9088 /* If we haven't been able to decide at this point, there nothing more we
9089 can check ourselves here. Look at the base type if we have one and it
9090 has the same precision. */
9091 if (TREE_CODE (type) == INTEGER_TYPE
9092 && TREE_TYPE (type) != 0
9093 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
9095 type = TREE_TYPE (type);
9096 goto retry;
9099 /* Or to fits_to_tree_p, if nothing else. */
9100 return wi::fits_to_tree_p (wi::to_wide (c), type);
9103 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
9104 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
9105 represented (assuming two's-complement arithmetic) within the bit
9106 precision of the type are returned instead. */
9108 void
9109 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
9111 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
9112 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
9113 wi::to_mpz (wi::to_wide (TYPE_MIN_VALUE (type)), min, TYPE_SIGN (type));
9114 else
9116 if (TYPE_UNSIGNED (type))
9117 mpz_set_ui (min, 0);
9118 else
9120 wide_int mn = wi::min_value (TYPE_PRECISION (type), SIGNED);
9121 wi::to_mpz (mn, min, SIGNED);
9125 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
9126 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
9127 wi::to_mpz (wi::to_wide (TYPE_MAX_VALUE (type)), max, TYPE_SIGN (type));
9128 else
9130 wide_int mn = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
9131 wi::to_mpz (mn, max, TYPE_SIGN (type));
9135 /* Return true if VAR is an automatic variable defined in function FN. */
9137 bool
9138 auto_var_in_fn_p (const_tree var, const_tree fn)
9140 return (DECL_P (var) && DECL_CONTEXT (var) == fn
9141 && ((((VAR_P (var) && ! DECL_EXTERNAL (var))
9142 || TREE_CODE (var) == PARM_DECL)
9143 && ! TREE_STATIC (var))
9144 || TREE_CODE (var) == LABEL_DECL
9145 || TREE_CODE (var) == RESULT_DECL));
9148 /* Subprogram of following function. Called by walk_tree.
9150 Return *TP if it is an automatic variable or parameter of the
9151 function passed in as DATA. */
9153 static tree
9154 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
9156 tree fn = (tree) data;
9158 if (TYPE_P (*tp))
9159 *walk_subtrees = 0;
9161 else if (DECL_P (*tp)
9162 && auto_var_in_fn_p (*tp, fn))
9163 return *tp;
9165 return NULL_TREE;
9168 /* Returns true if T is, contains, or refers to a type with variable
9169 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
9170 arguments, but not the return type. If FN is nonzero, only return
9171 true if a modifier of the type or position of FN is a variable or
9172 parameter inside FN.
9174 This concept is more general than that of C99 'variably modified types':
9175 in C99, a struct type is never variably modified because a VLA may not
9176 appear as a structure member. However, in GNU C code like:
9178 struct S { int i[f()]; };
9180 is valid, and other languages may define similar constructs. */
9182 bool
9183 variably_modified_type_p (tree type, tree fn)
9185 tree t;
9187 /* Test if T is either variable (if FN is zero) or an expression containing
9188 a variable in FN. If TYPE isn't gimplified, return true also if
9189 gimplify_one_sizepos would gimplify the expression into a local
9190 variable. */
9191 #define RETURN_TRUE_IF_VAR(T) \
9192 do { tree _t = (T); \
9193 if (_t != NULL_TREE \
9194 && _t != error_mark_node \
9195 && !CONSTANT_CLASS_P (_t) \
9196 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
9197 && (!fn \
9198 || (!TYPE_SIZES_GIMPLIFIED (type) \
9199 && (TREE_CODE (_t) != VAR_DECL \
9200 && !CONTAINS_PLACEHOLDER_P (_t))) \
9201 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
9202 return true; } while (0)
9204 if (type == error_mark_node)
9205 return false;
9207 /* If TYPE itself has variable size, it is variably modified. */
9208 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
9209 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
9211 switch (TREE_CODE (type))
9213 case POINTER_TYPE:
9214 case REFERENCE_TYPE:
9215 case VECTOR_TYPE:
9216 /* Ada can have pointer types refering to themselves indirectly. */
9217 if (TREE_VISITED (type))
9218 return false;
9219 TREE_VISITED (type) = true;
9220 if (variably_modified_type_p (TREE_TYPE (type), fn))
9222 TREE_VISITED (type) = false;
9223 return true;
9225 TREE_VISITED (type) = false;
9226 break;
9228 case FUNCTION_TYPE:
9229 case METHOD_TYPE:
9230 /* If TYPE is a function type, it is variably modified if the
9231 return type is variably modified. */
9232 if (variably_modified_type_p (TREE_TYPE (type), fn))
9233 return true;
9234 break;
9236 case INTEGER_TYPE:
9237 case REAL_TYPE:
9238 case FIXED_POINT_TYPE:
9239 case ENUMERAL_TYPE:
9240 case BOOLEAN_TYPE:
9241 /* Scalar types are variably modified if their end points
9242 aren't constant. */
9243 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
9244 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
9245 break;
9247 case RECORD_TYPE:
9248 case UNION_TYPE:
9249 case QUAL_UNION_TYPE:
9250 /* We can't see if any of the fields are variably-modified by the
9251 definition we normally use, since that would produce infinite
9252 recursion via pointers. */
9253 /* This is variably modified if some field's type is. */
9254 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
9255 if (TREE_CODE (t) == FIELD_DECL)
9257 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
9258 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
9259 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
9261 if (TREE_CODE (type) == QUAL_UNION_TYPE)
9262 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
9264 break;
9266 case ARRAY_TYPE:
9267 /* Do not call ourselves to avoid infinite recursion. This is
9268 variably modified if the element type is. */
9269 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
9270 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
9271 break;
9273 default:
9274 break;
9277 /* The current language may have other cases to check, but in general,
9278 all other types are not variably modified. */
9279 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
9281 #undef RETURN_TRUE_IF_VAR
9284 /* Given a DECL or TYPE, return the scope in which it was declared, or
9285 NULL_TREE if there is no containing scope. */
9287 tree
9288 get_containing_scope (const_tree t)
9290 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
9293 /* Returns the ultimate TRANSLATION_UNIT_DECL context of DECL or NULL. */
9295 const_tree
9296 get_ultimate_context (const_tree decl)
9298 while (decl && TREE_CODE (decl) != TRANSLATION_UNIT_DECL)
9300 if (TREE_CODE (decl) == BLOCK)
9301 decl = BLOCK_SUPERCONTEXT (decl);
9302 else
9303 decl = get_containing_scope (decl);
9305 return decl;
9308 /* Return the innermost context enclosing DECL that is
9309 a FUNCTION_DECL, or zero if none. */
9311 tree
9312 decl_function_context (const_tree decl)
9314 tree context;
9316 if (TREE_CODE (decl) == ERROR_MARK)
9317 return 0;
9319 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
9320 where we look up the function at runtime. Such functions always take
9321 a first argument of type 'pointer to real context'.
9323 C++ should really be fixed to use DECL_CONTEXT for the real context,
9324 and use something else for the "virtual context". */
9325 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
9326 context
9327 = TYPE_MAIN_VARIANT
9328 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
9329 else
9330 context = DECL_CONTEXT (decl);
9332 while (context && TREE_CODE (context) != FUNCTION_DECL)
9334 if (TREE_CODE (context) == BLOCK)
9335 context = BLOCK_SUPERCONTEXT (context);
9336 else
9337 context = get_containing_scope (context);
9340 return context;
9343 /* Return the innermost context enclosing DECL that is
9344 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
9345 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
9347 tree
9348 decl_type_context (const_tree decl)
9350 tree context = DECL_CONTEXT (decl);
9352 while (context)
9353 switch (TREE_CODE (context))
9355 case NAMESPACE_DECL:
9356 case TRANSLATION_UNIT_DECL:
9357 return NULL_TREE;
9359 case RECORD_TYPE:
9360 case UNION_TYPE:
9361 case QUAL_UNION_TYPE:
9362 return context;
9364 case TYPE_DECL:
9365 case FUNCTION_DECL:
9366 context = DECL_CONTEXT (context);
9367 break;
9369 case BLOCK:
9370 context = BLOCK_SUPERCONTEXT (context);
9371 break;
9373 default:
9374 gcc_unreachable ();
9377 return NULL_TREE;
9380 /* CALL is a CALL_EXPR. Return the declaration for the function
9381 called, or NULL_TREE if the called function cannot be
9382 determined. */
9384 tree
9385 get_callee_fndecl (const_tree call)
9387 tree addr;
9389 if (call == error_mark_node)
9390 return error_mark_node;
9392 /* It's invalid to call this function with anything but a
9393 CALL_EXPR. */
9394 gcc_assert (TREE_CODE (call) == CALL_EXPR);
9396 /* The first operand to the CALL is the address of the function
9397 called. */
9398 addr = CALL_EXPR_FN (call);
9400 /* If there is no function, return early. */
9401 if (addr == NULL_TREE)
9402 return NULL_TREE;
9404 STRIP_NOPS (addr);
9406 /* If this is a readonly function pointer, extract its initial value. */
9407 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
9408 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
9409 && DECL_INITIAL (addr))
9410 addr = DECL_INITIAL (addr);
9412 /* If the address is just `&f' for some function `f', then we know
9413 that `f' is being called. */
9414 if (TREE_CODE (addr) == ADDR_EXPR
9415 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
9416 return TREE_OPERAND (addr, 0);
9418 /* We couldn't figure out what was being called. */
9419 return NULL_TREE;
9422 /* If CALL_EXPR CALL calls a normal built-in function or an internal function,
9423 return the associated function code, otherwise return CFN_LAST. */
9425 combined_fn
9426 get_call_combined_fn (const_tree call)
9428 /* It's invalid to call this function with anything but a CALL_EXPR. */
9429 gcc_assert (TREE_CODE (call) == CALL_EXPR);
9431 if (!CALL_EXPR_FN (call))
9432 return as_combined_fn (CALL_EXPR_IFN (call));
9434 tree fndecl = get_callee_fndecl (call);
9435 if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
9436 return as_combined_fn (DECL_FUNCTION_CODE (fndecl));
9438 return CFN_LAST;
9441 /* Comparator of indices based on tree_node_counts. */
9443 static int
9444 tree_nodes_cmp (const void *p1, const void *p2)
9446 const unsigned *n1 = (const unsigned *)p1;
9447 const unsigned *n2 = (const unsigned *)p2;
9449 return tree_node_counts[*n1] - tree_node_counts[*n2];
9452 /* Comparator of indices based on tree_code_counts. */
9454 static int
9455 tree_codes_cmp (const void *p1, const void *p2)
9457 const unsigned *n1 = (const unsigned *)p1;
9458 const unsigned *n2 = (const unsigned *)p2;
9460 return tree_code_counts[*n1] - tree_code_counts[*n2];
9463 #define TREE_MEM_USAGE_SPACES 40
9465 /* Print debugging information about tree nodes generated during the compile,
9466 and any language-specific information. */
9468 void
9469 dump_tree_statistics (void)
9471 if (GATHER_STATISTICS)
9473 uint64_t total_nodes, total_bytes;
9474 fprintf (stderr, "\nKind Nodes Bytes\n");
9475 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9476 total_nodes = total_bytes = 0;
9479 auto_vec<unsigned> indices (all_kinds);
9480 for (unsigned i = 0; i < all_kinds; i++)
9481 indices.quick_push (i);
9482 indices.qsort (tree_nodes_cmp);
9484 for (unsigned i = 0; i < (int) all_kinds; i++)
9486 unsigned j = indices[i];
9487 fprintf (stderr, "%-20s %6" PRIu64 "%c %9" PRIu64 "%c\n",
9488 tree_node_kind_names[i], SIZE_AMOUNT (tree_node_counts[j]),
9489 SIZE_AMOUNT (tree_node_sizes[j]));
9490 total_nodes += tree_node_counts[j];
9491 total_bytes += tree_node_sizes[j];
9493 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9494 fprintf (stderr, "%-20s %6" PRIu64 "%c %9" PRIu64 "%c\n", "Total",
9495 SIZE_AMOUNT (total_nodes), SIZE_AMOUNT (total_bytes));
9496 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9500 fprintf (stderr, "Code Nodes\n");
9501 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9503 auto_vec<unsigned> indices (MAX_TREE_CODES);
9504 for (unsigned i = 0; i < MAX_TREE_CODES; i++)
9505 indices.quick_push (i);
9506 indices.qsort (tree_codes_cmp);
9508 for (unsigned i = 0; i < MAX_TREE_CODES; i++)
9510 unsigned j = indices[i];
9511 fprintf (stderr, "%-32s %6" PRIu64 "%c\n",
9512 get_tree_code_name ((enum tree_code) j),
9513 SIZE_AMOUNT (tree_code_counts[j]));
9515 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9516 fprintf (stderr, "\n");
9517 ssanames_print_statistics ();
9518 fprintf (stderr, "\n");
9519 phinodes_print_statistics ();
9520 fprintf (stderr, "\n");
9523 else
9524 fprintf (stderr, "(No per-node statistics)\n");
9526 print_type_hash_statistics ();
9527 print_debug_expr_statistics ();
9528 print_value_expr_statistics ();
9529 lang_hooks.print_statistics ();
9532 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
9534 /* Generate a crc32 of the low BYTES bytes of VALUE. */
9536 unsigned
9537 crc32_unsigned_n (unsigned chksum, unsigned value, unsigned bytes)
9539 /* This relies on the raw feedback's top 4 bits being zero. */
9540 #define FEEDBACK(X) ((X) * 0x04c11db7)
9541 #define SYNDROME(X) (FEEDBACK ((X) & 1) ^ FEEDBACK ((X) & 2) \
9542 ^ FEEDBACK ((X) & 4) ^ FEEDBACK ((X) & 8))
9543 static const unsigned syndromes[16] =
9545 SYNDROME(0x0), SYNDROME(0x1), SYNDROME(0x2), SYNDROME(0x3),
9546 SYNDROME(0x4), SYNDROME(0x5), SYNDROME(0x6), SYNDROME(0x7),
9547 SYNDROME(0x8), SYNDROME(0x9), SYNDROME(0xa), SYNDROME(0xb),
9548 SYNDROME(0xc), SYNDROME(0xd), SYNDROME(0xe), SYNDROME(0xf),
9550 #undef FEEDBACK
9551 #undef SYNDROME
9553 value <<= (32 - bytes * 8);
9554 for (unsigned ix = bytes * 2; ix--; value <<= 4)
9556 unsigned feedback = syndromes[((value ^ chksum) >> 28) & 0xf];
9558 chksum = (chksum << 4) ^ feedback;
9561 return chksum;
9564 /* Generate a crc32 of a string. */
9566 unsigned
9567 crc32_string (unsigned chksum, const char *string)
9570 chksum = crc32_byte (chksum, *string);
9571 while (*string++);
9572 return chksum;
9575 /* P is a string that will be used in a symbol. Mask out any characters
9576 that are not valid in that context. */
9578 void
9579 clean_symbol_name (char *p)
9581 for (; *p; p++)
9582 if (! (ISALNUM (*p)
9583 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
9584 || *p == '$'
9585 #endif
9586 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
9587 || *p == '.'
9588 #endif
9590 *p = '_';
9593 /* For anonymous aggregate types, we need some sort of name to
9594 hold on to. In practice, this should not appear, but it should
9595 not be harmful if it does. */
9596 bool
9597 anon_aggrname_p(const_tree id_node)
9599 #ifndef NO_DOT_IN_LABEL
9600 return (IDENTIFIER_POINTER (id_node)[0] == '.'
9601 && IDENTIFIER_POINTER (id_node)[1] == '_');
9602 #else /* NO_DOT_IN_LABEL */
9603 #ifndef NO_DOLLAR_IN_LABEL
9604 return (IDENTIFIER_POINTER (id_node)[0] == '$' \
9605 && IDENTIFIER_POINTER (id_node)[1] == '_');
9606 #else /* NO_DOLLAR_IN_LABEL */
9607 #define ANON_AGGRNAME_PREFIX "__anon_"
9608 return (!strncmp (IDENTIFIER_POINTER (id_node), ANON_AGGRNAME_PREFIX,
9609 sizeof (ANON_AGGRNAME_PREFIX) - 1));
9610 #endif /* NO_DOLLAR_IN_LABEL */
9611 #endif /* NO_DOT_IN_LABEL */
9614 /* Return a format for an anonymous aggregate name. */
9615 const char *
9616 anon_aggrname_format()
9618 #ifndef NO_DOT_IN_LABEL
9619 return "._%d";
9620 #else /* NO_DOT_IN_LABEL */
9621 #ifndef NO_DOLLAR_IN_LABEL
9622 return "$_%d";
9623 #else /* NO_DOLLAR_IN_LABEL */
9624 return "__anon_%d";
9625 #endif /* NO_DOLLAR_IN_LABEL */
9626 #endif /* NO_DOT_IN_LABEL */
9629 /* Generate a name for a special-purpose function.
9630 The generated name may need to be unique across the whole link.
9631 Changes to this function may also require corresponding changes to
9632 xstrdup_mask_random.
9633 TYPE is some string to identify the purpose of this function to the
9634 linker or collect2; it must start with an uppercase letter,
9635 one of:
9636 I - for constructors
9637 D - for destructors
9638 N - for C++ anonymous namespaces
9639 F - for DWARF unwind frame information. */
9641 tree
9642 get_file_function_name (const char *type)
9644 char *buf;
9645 const char *p;
9646 char *q;
9648 /* If we already have a name we know to be unique, just use that. */
9649 if (first_global_object_name)
9650 p = q = ASTRDUP (first_global_object_name);
9651 /* If the target is handling the constructors/destructors, they
9652 will be local to this file and the name is only necessary for
9653 debugging purposes.
9654 We also assign sub_I and sub_D sufixes to constructors called from
9655 the global static constructors. These are always local. */
9656 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9657 || (strncmp (type, "sub_", 4) == 0
9658 && (type[4] == 'I' || type[4] == 'D')))
9660 const char *file = main_input_filename;
9661 if (! file)
9662 file = LOCATION_FILE (input_location);
9663 /* Just use the file's basename, because the full pathname
9664 might be quite long. */
9665 p = q = ASTRDUP (lbasename (file));
9667 else
9669 /* Otherwise, the name must be unique across the entire link.
9670 We don't have anything that we know to be unique to this translation
9671 unit, so use what we do have and throw in some randomness. */
9672 unsigned len;
9673 const char *name = weak_global_object_name;
9674 const char *file = main_input_filename;
9676 if (! name)
9677 name = "";
9678 if (! file)
9679 file = LOCATION_FILE (input_location);
9681 len = strlen (file);
9682 q = (char *) alloca (9 + 19 + len + 1);
9683 memcpy (q, file, len + 1);
9685 snprintf (q + len, 9 + 19 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9686 crc32_string (0, name), get_random_seed (false));
9688 p = q;
9691 clean_symbol_name (q);
9692 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9693 + strlen (type));
9695 /* Set up the name of the file-level functions we may need.
9696 Use a global object (which is already required to be unique over
9697 the program) rather than the file name (which imposes extra
9698 constraints). */
9699 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9701 return get_identifier (buf);
9704 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9706 /* Complain that the tree code of NODE does not match the expected 0
9707 terminated list of trailing codes. The trailing code list can be
9708 empty, for a more vague error message. FILE, LINE, and FUNCTION
9709 are of the caller. */
9711 void
9712 tree_check_failed (const_tree node, const char *file,
9713 int line, const char *function, ...)
9715 va_list args;
9716 const char *buffer;
9717 unsigned length = 0;
9718 enum tree_code code;
9720 va_start (args, function);
9721 while ((code = (enum tree_code) va_arg (args, int)))
9722 length += 4 + strlen (get_tree_code_name (code));
9723 va_end (args);
9724 if (length)
9726 char *tmp;
9727 va_start (args, function);
9728 length += strlen ("expected ");
9729 buffer = tmp = (char *) alloca (length);
9730 length = 0;
9731 while ((code = (enum tree_code) va_arg (args, int)))
9733 const char *prefix = length ? " or " : "expected ";
9735 strcpy (tmp + length, prefix);
9736 length += strlen (prefix);
9737 strcpy (tmp + length, get_tree_code_name (code));
9738 length += strlen (get_tree_code_name (code));
9740 va_end (args);
9742 else
9743 buffer = "unexpected node";
9745 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9746 buffer, get_tree_code_name (TREE_CODE (node)),
9747 function, trim_filename (file), line);
9750 /* Complain that the tree code of NODE does match the expected 0
9751 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9752 the caller. */
9754 void
9755 tree_not_check_failed (const_tree node, const char *file,
9756 int line, const char *function, ...)
9758 va_list args;
9759 char *buffer;
9760 unsigned length = 0;
9761 enum tree_code code;
9763 va_start (args, function);
9764 while ((code = (enum tree_code) va_arg (args, int)))
9765 length += 4 + strlen (get_tree_code_name (code));
9766 va_end (args);
9767 va_start (args, function);
9768 buffer = (char *) alloca (length);
9769 length = 0;
9770 while ((code = (enum tree_code) va_arg (args, int)))
9772 if (length)
9774 strcpy (buffer + length, " or ");
9775 length += 4;
9777 strcpy (buffer + length, get_tree_code_name (code));
9778 length += strlen (get_tree_code_name (code));
9780 va_end (args);
9782 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9783 buffer, get_tree_code_name (TREE_CODE (node)),
9784 function, trim_filename (file), line);
9787 /* Similar to tree_check_failed, except that we check for a class of tree
9788 code, given in CL. */
9790 void
9791 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9792 const char *file, int line, const char *function)
9794 internal_error
9795 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9796 TREE_CODE_CLASS_STRING (cl),
9797 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9798 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9801 /* Similar to tree_check_failed, except that instead of specifying a
9802 dozen codes, use the knowledge that they're all sequential. */
9804 void
9805 tree_range_check_failed (const_tree node, const char *file, int line,
9806 const char *function, enum tree_code c1,
9807 enum tree_code c2)
9809 char *buffer;
9810 unsigned length = 0;
9811 unsigned int c;
9813 for (c = c1; c <= c2; ++c)
9814 length += 4 + strlen (get_tree_code_name ((enum tree_code) c));
9816 length += strlen ("expected ");
9817 buffer = (char *) alloca (length);
9818 length = 0;
9820 for (c = c1; c <= c2; ++c)
9822 const char *prefix = length ? " or " : "expected ";
9824 strcpy (buffer + length, prefix);
9825 length += strlen (prefix);
9826 strcpy (buffer + length, get_tree_code_name ((enum tree_code) c));
9827 length += strlen (get_tree_code_name ((enum tree_code) c));
9830 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9831 buffer, get_tree_code_name (TREE_CODE (node)),
9832 function, trim_filename (file), line);
9836 /* Similar to tree_check_failed, except that we check that a tree does
9837 not have the specified code, given in CL. */
9839 void
9840 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9841 const char *file, int line, const char *function)
9843 internal_error
9844 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9845 TREE_CODE_CLASS_STRING (cl),
9846 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9847 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9851 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9853 void
9854 omp_clause_check_failed (const_tree node, const char *file, int line,
9855 const char *function, enum omp_clause_code code)
9857 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9858 omp_clause_code_name[code], get_tree_code_name (TREE_CODE (node)),
9859 function, trim_filename (file), line);
9863 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9865 void
9866 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9867 const char *function, enum omp_clause_code c1,
9868 enum omp_clause_code c2)
9870 char *buffer;
9871 unsigned length = 0;
9872 unsigned int c;
9874 for (c = c1; c <= c2; ++c)
9875 length += 4 + strlen (omp_clause_code_name[c]);
9877 length += strlen ("expected ");
9878 buffer = (char *) alloca (length);
9879 length = 0;
9881 for (c = c1; c <= c2; ++c)
9883 const char *prefix = length ? " or " : "expected ";
9885 strcpy (buffer + length, prefix);
9886 length += strlen (prefix);
9887 strcpy (buffer + length, omp_clause_code_name[c]);
9888 length += strlen (omp_clause_code_name[c]);
9891 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9892 buffer, omp_clause_code_name[TREE_CODE (node)],
9893 function, trim_filename (file), line);
9897 #undef DEFTREESTRUCT
9898 #define DEFTREESTRUCT(VAL, NAME) NAME,
9900 static const char *ts_enum_names[] = {
9901 #include "treestruct.def"
9903 #undef DEFTREESTRUCT
9905 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9907 /* Similar to tree_class_check_failed, except that we check for
9908 whether CODE contains the tree structure identified by EN. */
9910 void
9911 tree_contains_struct_check_failed (const_tree node,
9912 const enum tree_node_structure_enum en,
9913 const char *file, int line,
9914 const char *function)
9916 internal_error
9917 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9918 TS_ENUM_NAME (en),
9919 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9923 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9924 (dynamically sized) vector. */
9926 void
9927 tree_int_cst_elt_check_failed (int idx, int len, const char *file, int line,
9928 const char *function)
9930 internal_error
9931 ("tree check: accessed elt %d of tree_int_cst with %d elts in %s, at %s:%d",
9932 idx + 1, len, function, trim_filename (file), line);
9935 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9936 (dynamically sized) vector. */
9938 void
9939 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9940 const char *function)
9942 internal_error
9943 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9944 idx + 1, len, function, trim_filename (file), line);
9947 /* Similar to above, except that the check is for the bounds of the operand
9948 vector of an expression node EXP. */
9950 void
9951 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9952 int line, const char *function)
9954 enum tree_code code = TREE_CODE (exp);
9955 internal_error
9956 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9957 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9958 function, trim_filename (file), line);
9961 /* Similar to above, except that the check is for the number of
9962 operands of an OMP_CLAUSE node. */
9964 void
9965 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9966 int line, const char *function)
9968 internal_error
9969 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9970 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9971 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9972 trim_filename (file), line);
9974 #endif /* ENABLE_TREE_CHECKING */
9976 /* Create a new vector type node holding NUNITS units of type INNERTYPE,
9977 and mapped to the machine mode MODE. Initialize its fields and build
9978 the information necessary for debugging output. */
9980 static tree
9981 make_vector_type (tree innertype, poly_int64 nunits, machine_mode mode)
9983 tree t;
9984 tree mv_innertype = TYPE_MAIN_VARIANT (innertype);
9986 t = make_node (VECTOR_TYPE);
9987 TREE_TYPE (t) = mv_innertype;
9988 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9989 SET_TYPE_MODE (t, mode);
9991 if (TYPE_STRUCTURAL_EQUALITY_P (mv_innertype) || in_lto_p)
9992 SET_TYPE_STRUCTURAL_EQUALITY (t);
9993 else if ((TYPE_CANONICAL (mv_innertype) != innertype
9994 || mode != VOIDmode)
9995 && !VECTOR_BOOLEAN_TYPE_P (t))
9996 TYPE_CANONICAL (t)
9997 = make_vector_type (TYPE_CANONICAL (mv_innertype), nunits, VOIDmode);
9999 layout_type (t);
10001 hashval_t hash = type_hash_canon_hash (t);
10002 t = type_hash_canon (hash, t);
10004 /* We have built a main variant, based on the main variant of the
10005 inner type. Use it to build the variant we return. */
10006 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
10007 && TREE_TYPE (t) != innertype)
10008 return build_type_attribute_qual_variant (t,
10009 TYPE_ATTRIBUTES (innertype),
10010 TYPE_QUALS (innertype));
10012 return t;
10015 static tree
10016 make_or_reuse_type (unsigned size, int unsignedp)
10018 int i;
10020 if (size == INT_TYPE_SIZE)
10021 return unsignedp ? unsigned_type_node : integer_type_node;
10022 if (size == CHAR_TYPE_SIZE)
10023 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
10024 if (size == SHORT_TYPE_SIZE)
10025 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
10026 if (size == LONG_TYPE_SIZE)
10027 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
10028 if (size == LONG_LONG_TYPE_SIZE)
10029 return (unsignedp ? long_long_unsigned_type_node
10030 : long_long_integer_type_node);
10032 for (i = 0; i < NUM_INT_N_ENTS; i ++)
10033 if (size == int_n_data[i].bitsize
10034 && int_n_enabled_p[i])
10035 return (unsignedp ? int_n_trees[i].unsigned_type
10036 : int_n_trees[i].signed_type);
10038 if (unsignedp)
10039 return make_unsigned_type (size);
10040 else
10041 return make_signed_type (size);
10044 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
10046 static tree
10047 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
10049 if (satp)
10051 if (size == SHORT_FRACT_TYPE_SIZE)
10052 return unsignedp ? sat_unsigned_short_fract_type_node
10053 : sat_short_fract_type_node;
10054 if (size == FRACT_TYPE_SIZE)
10055 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
10056 if (size == LONG_FRACT_TYPE_SIZE)
10057 return unsignedp ? sat_unsigned_long_fract_type_node
10058 : sat_long_fract_type_node;
10059 if (size == LONG_LONG_FRACT_TYPE_SIZE)
10060 return unsignedp ? sat_unsigned_long_long_fract_type_node
10061 : sat_long_long_fract_type_node;
10063 else
10065 if (size == SHORT_FRACT_TYPE_SIZE)
10066 return unsignedp ? unsigned_short_fract_type_node
10067 : short_fract_type_node;
10068 if (size == FRACT_TYPE_SIZE)
10069 return unsignedp ? unsigned_fract_type_node : fract_type_node;
10070 if (size == LONG_FRACT_TYPE_SIZE)
10071 return unsignedp ? unsigned_long_fract_type_node
10072 : long_fract_type_node;
10073 if (size == LONG_LONG_FRACT_TYPE_SIZE)
10074 return unsignedp ? unsigned_long_long_fract_type_node
10075 : long_long_fract_type_node;
10078 return make_fract_type (size, unsignedp, satp);
10081 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
10083 static tree
10084 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
10086 if (satp)
10088 if (size == SHORT_ACCUM_TYPE_SIZE)
10089 return unsignedp ? sat_unsigned_short_accum_type_node
10090 : sat_short_accum_type_node;
10091 if (size == ACCUM_TYPE_SIZE)
10092 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
10093 if (size == LONG_ACCUM_TYPE_SIZE)
10094 return unsignedp ? sat_unsigned_long_accum_type_node
10095 : sat_long_accum_type_node;
10096 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
10097 return unsignedp ? sat_unsigned_long_long_accum_type_node
10098 : sat_long_long_accum_type_node;
10100 else
10102 if (size == SHORT_ACCUM_TYPE_SIZE)
10103 return unsignedp ? unsigned_short_accum_type_node
10104 : short_accum_type_node;
10105 if (size == ACCUM_TYPE_SIZE)
10106 return unsignedp ? unsigned_accum_type_node : accum_type_node;
10107 if (size == LONG_ACCUM_TYPE_SIZE)
10108 return unsignedp ? unsigned_long_accum_type_node
10109 : long_accum_type_node;
10110 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
10111 return unsignedp ? unsigned_long_long_accum_type_node
10112 : long_long_accum_type_node;
10115 return make_accum_type (size, unsignedp, satp);
10119 /* Create an atomic variant node for TYPE. This routine is called
10120 during initialization of data types to create the 5 basic atomic
10121 types. The generic build_variant_type function requires these to
10122 already be set up in order to function properly, so cannot be
10123 called from there. If ALIGN is non-zero, then ensure alignment is
10124 overridden to this value. */
10126 static tree
10127 build_atomic_base (tree type, unsigned int align)
10129 tree t;
10131 /* Make sure its not already registered. */
10132 if ((t = get_qualified_type (type, TYPE_QUAL_ATOMIC)))
10133 return t;
10135 t = build_variant_type_copy (type);
10136 set_type_quals (t, TYPE_QUAL_ATOMIC);
10138 if (align)
10139 SET_TYPE_ALIGN (t, align);
10141 return t;
10144 /* Information about the _FloatN and _FloatNx types. This must be in
10145 the same order as the corresponding TI_* enum values. */
10146 const floatn_type_info floatn_nx_types[NUM_FLOATN_NX_TYPES] =
10148 { 16, false },
10149 { 32, false },
10150 { 64, false },
10151 { 128, false },
10152 { 32, true },
10153 { 64, true },
10154 { 128, true },
10158 /* Create nodes for all integer types (and error_mark_node) using the sizes
10159 of C datatypes. SIGNED_CHAR specifies whether char is signed. */
10161 void
10162 build_common_tree_nodes (bool signed_char)
10164 int i;
10166 error_mark_node = make_node (ERROR_MARK);
10167 TREE_TYPE (error_mark_node) = error_mark_node;
10169 initialize_sizetypes ();
10171 /* Define both `signed char' and `unsigned char'. */
10172 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
10173 TYPE_STRING_FLAG (signed_char_type_node) = 1;
10174 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
10175 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
10177 /* Define `char', which is like either `signed char' or `unsigned char'
10178 but not the same as either. */
10179 char_type_node
10180 = (signed_char
10181 ? make_signed_type (CHAR_TYPE_SIZE)
10182 : make_unsigned_type (CHAR_TYPE_SIZE));
10183 TYPE_STRING_FLAG (char_type_node) = 1;
10185 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
10186 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
10187 integer_type_node = make_signed_type (INT_TYPE_SIZE);
10188 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
10189 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
10190 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
10191 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
10192 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
10194 for (i = 0; i < NUM_INT_N_ENTS; i ++)
10196 int_n_trees[i].signed_type = make_signed_type (int_n_data[i].bitsize);
10197 int_n_trees[i].unsigned_type = make_unsigned_type (int_n_data[i].bitsize);
10199 if (int_n_enabled_p[i])
10201 integer_types[itk_intN_0 + i * 2] = int_n_trees[i].signed_type;
10202 integer_types[itk_unsigned_intN_0 + i * 2] = int_n_trees[i].unsigned_type;
10206 /* Define a boolean type. This type only represents boolean values but
10207 may be larger than char depending on the value of BOOL_TYPE_SIZE. */
10208 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
10209 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
10210 TYPE_PRECISION (boolean_type_node) = 1;
10211 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
10213 /* Define what type to use for size_t. */
10214 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
10215 size_type_node = unsigned_type_node;
10216 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
10217 size_type_node = long_unsigned_type_node;
10218 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
10219 size_type_node = long_long_unsigned_type_node;
10220 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
10221 size_type_node = short_unsigned_type_node;
10222 else
10224 int i;
10226 size_type_node = NULL_TREE;
10227 for (i = 0; i < NUM_INT_N_ENTS; i++)
10228 if (int_n_enabled_p[i])
10230 char name[50];
10231 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
10233 if (strcmp (name, SIZE_TYPE) == 0)
10235 size_type_node = int_n_trees[i].unsigned_type;
10238 if (size_type_node == NULL_TREE)
10239 gcc_unreachable ();
10242 /* Define what type to use for ptrdiff_t. */
10243 if (strcmp (PTRDIFF_TYPE, "int") == 0)
10244 ptrdiff_type_node = integer_type_node;
10245 else if (strcmp (PTRDIFF_TYPE, "long int") == 0)
10246 ptrdiff_type_node = long_integer_type_node;
10247 else if (strcmp (PTRDIFF_TYPE, "long long int") == 0)
10248 ptrdiff_type_node = long_long_integer_type_node;
10249 else if (strcmp (PTRDIFF_TYPE, "short int") == 0)
10250 ptrdiff_type_node = short_integer_type_node;
10251 else
10253 ptrdiff_type_node = NULL_TREE;
10254 for (int i = 0; i < NUM_INT_N_ENTS; i++)
10255 if (int_n_enabled_p[i])
10257 char name[50];
10258 sprintf (name, "__int%d", int_n_data[i].bitsize);
10259 if (strcmp (name, PTRDIFF_TYPE) == 0)
10260 ptrdiff_type_node = int_n_trees[i].signed_type;
10262 if (ptrdiff_type_node == NULL_TREE)
10263 gcc_unreachable ();
10266 /* Fill in the rest of the sized types. Reuse existing type nodes
10267 when possible. */
10268 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
10269 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
10270 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
10271 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
10272 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
10274 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
10275 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
10276 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
10277 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
10278 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
10280 /* Don't call build_qualified type for atomics. That routine does
10281 special processing for atomics, and until they are initialized
10282 it's better not to make that call.
10284 Check to see if there is a target override for atomic types. */
10286 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
10287 targetm.atomic_align_for_mode (QImode));
10288 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
10289 targetm.atomic_align_for_mode (HImode));
10290 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
10291 targetm.atomic_align_for_mode (SImode));
10292 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
10293 targetm.atomic_align_for_mode (DImode));
10294 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
10295 targetm.atomic_align_for_mode (TImode));
10297 access_public_node = get_identifier ("public");
10298 access_protected_node = get_identifier ("protected");
10299 access_private_node = get_identifier ("private");
10301 /* Define these next since types below may used them. */
10302 integer_zero_node = build_int_cst (integer_type_node, 0);
10303 integer_one_node = build_int_cst (integer_type_node, 1);
10304 integer_three_node = build_int_cst (integer_type_node, 3);
10305 integer_minus_one_node = build_int_cst (integer_type_node, -1);
10307 size_zero_node = size_int (0);
10308 size_one_node = size_int (1);
10309 bitsize_zero_node = bitsize_int (0);
10310 bitsize_one_node = bitsize_int (1);
10311 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
10313 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
10314 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
10316 void_type_node = make_node (VOID_TYPE);
10317 layout_type (void_type_node);
10319 /* We are not going to have real types in C with less than byte alignment,
10320 so we might as well not have any types that claim to have it. */
10321 SET_TYPE_ALIGN (void_type_node, BITS_PER_UNIT);
10322 TYPE_USER_ALIGN (void_type_node) = 0;
10324 void_node = make_node (VOID_CST);
10325 TREE_TYPE (void_node) = void_type_node;
10327 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
10328 layout_type (TREE_TYPE (null_pointer_node));
10330 ptr_type_node = build_pointer_type (void_type_node);
10331 const_ptr_type_node
10332 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
10333 for (unsigned i = 0;
10334 i < sizeof (builtin_structptr_types) / sizeof (builtin_structptr_type);
10335 ++i)
10336 builtin_structptr_types[i].node = builtin_structptr_types[i].base;
10338 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
10340 float_type_node = make_node (REAL_TYPE);
10341 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
10342 layout_type (float_type_node);
10344 double_type_node = make_node (REAL_TYPE);
10345 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
10346 layout_type (double_type_node);
10348 long_double_type_node = make_node (REAL_TYPE);
10349 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
10350 layout_type (long_double_type_node);
10352 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
10354 int n = floatn_nx_types[i].n;
10355 bool extended = floatn_nx_types[i].extended;
10356 scalar_float_mode mode;
10357 if (!targetm.floatn_mode (n, extended).exists (&mode))
10358 continue;
10359 int precision = GET_MODE_PRECISION (mode);
10360 /* Work around the rs6000 KFmode having precision 113 not
10361 128. */
10362 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
10363 gcc_assert (fmt->b == 2 && fmt->emin + fmt->emax == 3);
10364 int min_precision = fmt->p + ceil_log2 (fmt->emax - fmt->emin);
10365 if (!extended)
10366 gcc_assert (min_precision == n);
10367 if (precision < min_precision)
10368 precision = min_precision;
10369 FLOATN_NX_TYPE_NODE (i) = make_node (REAL_TYPE);
10370 TYPE_PRECISION (FLOATN_NX_TYPE_NODE (i)) = precision;
10371 layout_type (FLOATN_NX_TYPE_NODE (i));
10372 SET_TYPE_MODE (FLOATN_NX_TYPE_NODE (i), mode);
10375 float_ptr_type_node = build_pointer_type (float_type_node);
10376 double_ptr_type_node = build_pointer_type (double_type_node);
10377 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
10378 integer_ptr_type_node = build_pointer_type (integer_type_node);
10380 /* Fixed size integer types. */
10381 uint16_type_node = make_or_reuse_type (16, 1);
10382 uint32_type_node = make_or_reuse_type (32, 1);
10383 uint64_type_node = make_or_reuse_type (64, 1);
10385 /* Decimal float types. */
10386 dfloat32_type_node = make_node (REAL_TYPE);
10387 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
10388 SET_TYPE_MODE (dfloat32_type_node, SDmode);
10389 layout_type (dfloat32_type_node);
10390 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
10392 dfloat64_type_node = make_node (REAL_TYPE);
10393 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
10394 SET_TYPE_MODE (dfloat64_type_node, DDmode);
10395 layout_type (dfloat64_type_node);
10396 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
10398 dfloat128_type_node = make_node (REAL_TYPE);
10399 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
10400 SET_TYPE_MODE (dfloat128_type_node, TDmode);
10401 layout_type (dfloat128_type_node);
10402 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
10404 complex_integer_type_node = build_complex_type (integer_type_node, true);
10405 complex_float_type_node = build_complex_type (float_type_node, true);
10406 complex_double_type_node = build_complex_type (double_type_node, true);
10407 complex_long_double_type_node = build_complex_type (long_double_type_node,
10408 true);
10410 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
10412 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
10413 COMPLEX_FLOATN_NX_TYPE_NODE (i)
10414 = build_complex_type (FLOATN_NX_TYPE_NODE (i));
10417 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
10418 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
10419 sat_ ## KIND ## _type_node = \
10420 make_sat_signed_ ## KIND ## _type (SIZE); \
10421 sat_unsigned_ ## KIND ## _type_node = \
10422 make_sat_unsigned_ ## KIND ## _type (SIZE); \
10423 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
10424 unsigned_ ## KIND ## _type_node = \
10425 make_unsigned_ ## KIND ## _type (SIZE);
10427 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
10428 sat_ ## WIDTH ## KIND ## _type_node = \
10429 make_sat_signed_ ## KIND ## _type (SIZE); \
10430 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
10431 make_sat_unsigned_ ## KIND ## _type (SIZE); \
10432 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
10433 unsigned_ ## WIDTH ## KIND ## _type_node = \
10434 make_unsigned_ ## KIND ## _type (SIZE);
10436 /* Make fixed-point type nodes based on four different widths. */
10437 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
10438 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
10439 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
10440 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
10441 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
10443 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
10444 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
10445 NAME ## _type_node = \
10446 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
10447 u ## NAME ## _type_node = \
10448 make_or_reuse_unsigned_ ## KIND ## _type \
10449 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
10450 sat_ ## NAME ## _type_node = \
10451 make_or_reuse_sat_signed_ ## KIND ## _type \
10452 (GET_MODE_BITSIZE (MODE ## mode)); \
10453 sat_u ## NAME ## _type_node = \
10454 make_or_reuse_sat_unsigned_ ## KIND ## _type \
10455 (GET_MODE_BITSIZE (U ## MODE ## mode));
10457 /* Fixed-point type and mode nodes. */
10458 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
10459 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
10460 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
10461 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
10462 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
10463 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
10464 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
10465 MAKE_FIXED_MODE_NODE (accum, ha, HA)
10466 MAKE_FIXED_MODE_NODE (accum, sa, SA)
10467 MAKE_FIXED_MODE_NODE (accum, da, DA)
10468 MAKE_FIXED_MODE_NODE (accum, ta, TA)
10471 tree t = targetm.build_builtin_va_list ();
10473 /* Many back-ends define record types without setting TYPE_NAME.
10474 If we copied the record type here, we'd keep the original
10475 record type without a name. This breaks name mangling. So,
10476 don't copy record types and let c_common_nodes_and_builtins()
10477 declare the type to be __builtin_va_list. */
10478 if (TREE_CODE (t) != RECORD_TYPE)
10479 t = build_variant_type_copy (t);
10481 va_list_type_node = t;
10485 /* Modify DECL for given flags.
10486 TM_PURE attribute is set only on types, so the function will modify
10487 DECL's type when ECF_TM_PURE is used. */
10489 void
10490 set_call_expr_flags (tree decl, int flags)
10492 if (flags & ECF_NOTHROW)
10493 TREE_NOTHROW (decl) = 1;
10494 if (flags & ECF_CONST)
10495 TREE_READONLY (decl) = 1;
10496 if (flags & ECF_PURE)
10497 DECL_PURE_P (decl) = 1;
10498 if (flags & ECF_LOOPING_CONST_OR_PURE)
10499 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
10500 if (flags & ECF_NOVOPS)
10501 DECL_IS_NOVOPS (decl) = 1;
10502 if (flags & ECF_NORETURN)
10503 TREE_THIS_VOLATILE (decl) = 1;
10504 if (flags & ECF_MALLOC)
10505 DECL_IS_MALLOC (decl) = 1;
10506 if (flags & ECF_RETURNS_TWICE)
10507 DECL_IS_RETURNS_TWICE (decl) = 1;
10508 if (flags & ECF_LEAF)
10509 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
10510 NULL, DECL_ATTRIBUTES (decl));
10511 if (flags & ECF_COLD)
10512 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("cold"),
10513 NULL, DECL_ATTRIBUTES (decl));
10514 if (flags & ECF_RET1)
10515 DECL_ATTRIBUTES (decl)
10516 = tree_cons (get_identifier ("fn spec"),
10517 build_tree_list (NULL_TREE, build_string (1, "1")),
10518 DECL_ATTRIBUTES (decl));
10519 if ((flags & ECF_TM_PURE) && flag_tm)
10520 apply_tm_attr (decl, get_identifier ("transaction_pure"));
10521 /* Looping const or pure is implied by noreturn.
10522 There is currently no way to declare looping const or looping pure alone. */
10523 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
10524 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
10528 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
10530 static void
10531 local_define_builtin (const char *name, tree type, enum built_in_function code,
10532 const char *library_name, int ecf_flags)
10534 tree decl;
10536 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
10537 library_name, NULL_TREE);
10538 set_call_expr_flags (decl, ecf_flags);
10540 set_builtin_decl (code, decl, true);
10543 /* Call this function after instantiating all builtins that the language
10544 front end cares about. This will build the rest of the builtins
10545 and internal functions that are relied upon by the tree optimizers and
10546 the middle-end. */
10548 void
10549 build_common_builtin_nodes (void)
10551 tree tmp, ftype;
10552 int ecf_flags;
10554 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE)
10555 || !builtin_decl_explicit_p (BUILT_IN_ABORT))
10557 ftype = build_function_type (void_type_node, void_list_node);
10558 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
10559 local_define_builtin ("__builtin_unreachable", ftype,
10560 BUILT_IN_UNREACHABLE,
10561 "__builtin_unreachable",
10562 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
10563 | ECF_CONST | ECF_COLD);
10564 if (!builtin_decl_explicit_p (BUILT_IN_ABORT))
10565 local_define_builtin ("__builtin_abort", ftype, BUILT_IN_ABORT,
10566 "abort",
10567 ECF_LEAF | ECF_NORETURN | ECF_CONST | ECF_COLD);
10570 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
10571 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10573 ftype = build_function_type_list (ptr_type_node,
10574 ptr_type_node, const_ptr_type_node,
10575 size_type_node, NULL_TREE);
10577 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
10578 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
10579 "memcpy", ECF_NOTHROW | ECF_LEAF | ECF_RET1);
10580 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10581 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
10582 "memmove", ECF_NOTHROW | ECF_LEAF | ECF_RET1);
10585 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
10587 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
10588 const_ptr_type_node, size_type_node,
10589 NULL_TREE);
10590 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
10591 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10594 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
10596 ftype = build_function_type_list (ptr_type_node,
10597 ptr_type_node, integer_type_node,
10598 size_type_node, NULL_TREE);
10599 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
10600 "memset", ECF_NOTHROW | ECF_LEAF | ECF_RET1);
10603 /* If we're checking the stack, `alloca' can throw. */
10604 const int alloca_flags
10605 = ECF_MALLOC | ECF_LEAF | (flag_stack_check ? 0 : ECF_NOTHROW);
10607 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
10609 ftype = build_function_type_list (ptr_type_node,
10610 size_type_node, NULL_TREE);
10611 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
10612 "alloca", alloca_flags);
10615 ftype = build_function_type_list (ptr_type_node, size_type_node,
10616 size_type_node, NULL_TREE);
10617 local_define_builtin ("__builtin_alloca_with_align", ftype,
10618 BUILT_IN_ALLOCA_WITH_ALIGN,
10619 "__builtin_alloca_with_align",
10620 alloca_flags);
10622 ftype = build_function_type_list (ptr_type_node, size_type_node,
10623 size_type_node, size_type_node, NULL_TREE);
10624 local_define_builtin ("__builtin_alloca_with_align_and_max", ftype,
10625 BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX,
10626 "__builtin_alloca_with_align_and_max",
10627 alloca_flags);
10629 ftype = build_function_type_list (void_type_node,
10630 ptr_type_node, ptr_type_node,
10631 ptr_type_node, NULL_TREE);
10632 local_define_builtin ("__builtin_init_trampoline", ftype,
10633 BUILT_IN_INIT_TRAMPOLINE,
10634 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
10635 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
10636 BUILT_IN_INIT_HEAP_TRAMPOLINE,
10637 "__builtin_init_heap_trampoline",
10638 ECF_NOTHROW | ECF_LEAF);
10639 local_define_builtin ("__builtin_init_descriptor", ftype,
10640 BUILT_IN_INIT_DESCRIPTOR,
10641 "__builtin_init_descriptor", ECF_NOTHROW | ECF_LEAF);
10643 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
10644 local_define_builtin ("__builtin_adjust_trampoline", ftype,
10645 BUILT_IN_ADJUST_TRAMPOLINE,
10646 "__builtin_adjust_trampoline",
10647 ECF_CONST | ECF_NOTHROW);
10648 local_define_builtin ("__builtin_adjust_descriptor", ftype,
10649 BUILT_IN_ADJUST_DESCRIPTOR,
10650 "__builtin_adjust_descriptor",
10651 ECF_CONST | ECF_NOTHROW);
10653 ftype = build_function_type_list (void_type_node,
10654 ptr_type_node, ptr_type_node, NULL_TREE);
10655 local_define_builtin ("__builtin_nonlocal_goto", ftype,
10656 BUILT_IN_NONLOCAL_GOTO,
10657 "__builtin_nonlocal_goto",
10658 ECF_NORETURN | ECF_NOTHROW);
10660 ftype = build_function_type_list (void_type_node,
10661 ptr_type_node, ptr_type_node, NULL_TREE);
10662 local_define_builtin ("__builtin_setjmp_setup", ftype,
10663 BUILT_IN_SETJMP_SETUP,
10664 "__builtin_setjmp_setup", ECF_NOTHROW);
10666 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10667 local_define_builtin ("__builtin_setjmp_receiver", ftype,
10668 BUILT_IN_SETJMP_RECEIVER,
10669 "__builtin_setjmp_receiver", ECF_NOTHROW | ECF_LEAF);
10671 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
10672 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
10673 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
10675 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10676 local_define_builtin ("__builtin_stack_restore", ftype,
10677 BUILT_IN_STACK_RESTORE,
10678 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
10680 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
10681 const_ptr_type_node, size_type_node,
10682 NULL_TREE);
10683 local_define_builtin ("__builtin_memcmp_eq", ftype, BUILT_IN_MEMCMP_EQ,
10684 "__builtin_memcmp_eq",
10685 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10687 local_define_builtin ("__builtin_strncmp_eq", ftype, BUILT_IN_STRNCMP_EQ,
10688 "__builtin_strncmp_eq",
10689 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10691 local_define_builtin ("__builtin_strcmp_eq", ftype, BUILT_IN_STRCMP_EQ,
10692 "__builtin_strcmp_eq",
10693 ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10695 /* If there's a possibility that we might use the ARM EABI, build the
10696 alternate __cxa_end_cleanup node used to resume from C++. */
10697 if (targetm.arm_eabi_unwinder)
10699 ftype = build_function_type_list (void_type_node, NULL_TREE);
10700 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
10701 BUILT_IN_CXA_END_CLEANUP,
10702 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
10705 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10706 local_define_builtin ("__builtin_unwind_resume", ftype,
10707 BUILT_IN_UNWIND_RESUME,
10708 ((targetm_common.except_unwind_info (&global_options)
10709 == UI_SJLJ)
10710 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
10711 ECF_NORETURN);
10713 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
10715 ftype = build_function_type_list (ptr_type_node, integer_type_node,
10716 NULL_TREE);
10717 local_define_builtin ("__builtin_return_address", ftype,
10718 BUILT_IN_RETURN_ADDRESS,
10719 "__builtin_return_address",
10720 ECF_NOTHROW);
10723 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
10724 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10726 ftype = build_function_type_list (void_type_node, ptr_type_node,
10727 ptr_type_node, NULL_TREE);
10728 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
10729 local_define_builtin ("__cyg_profile_func_enter", ftype,
10730 BUILT_IN_PROFILE_FUNC_ENTER,
10731 "__cyg_profile_func_enter", 0);
10732 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10733 local_define_builtin ("__cyg_profile_func_exit", ftype,
10734 BUILT_IN_PROFILE_FUNC_EXIT,
10735 "__cyg_profile_func_exit", 0);
10738 /* The exception object and filter values from the runtime. The argument
10739 must be zero before exception lowering, i.e. from the front end. After
10740 exception lowering, it will be the region number for the exception
10741 landing pad. These functions are PURE instead of CONST to prevent
10742 them from being hoisted past the exception edge that will initialize
10743 its value in the landing pad. */
10744 ftype = build_function_type_list (ptr_type_node,
10745 integer_type_node, NULL_TREE);
10746 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
10747 /* Only use TM_PURE if we have TM language support. */
10748 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
10749 ecf_flags |= ECF_TM_PURE;
10750 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
10751 "__builtin_eh_pointer", ecf_flags);
10753 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
10754 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
10755 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
10756 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10758 ftype = build_function_type_list (void_type_node,
10759 integer_type_node, integer_type_node,
10760 NULL_TREE);
10761 local_define_builtin ("__builtin_eh_copy_values", ftype,
10762 BUILT_IN_EH_COPY_VALUES,
10763 "__builtin_eh_copy_values", ECF_NOTHROW);
10765 /* Complex multiplication and division. These are handled as builtins
10766 rather than optabs because emit_library_call_value doesn't support
10767 complex. Further, we can do slightly better with folding these
10768 beasties if the real and complex parts of the arguments are separate. */
10770 int mode;
10772 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10774 char mode_name_buf[4], *q;
10775 const char *p;
10776 enum built_in_function mcode, dcode;
10777 tree type, inner_type;
10778 const char *prefix = "__";
10780 if (targetm.libfunc_gnu_prefix)
10781 prefix = "__gnu_";
10783 type = lang_hooks.types.type_for_mode ((machine_mode) mode, 0);
10784 if (type == NULL)
10785 continue;
10786 inner_type = TREE_TYPE (type);
10788 ftype = build_function_type_list (type, inner_type, inner_type,
10789 inner_type, inner_type, NULL_TREE);
10791 mcode = ((enum built_in_function)
10792 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10793 dcode = ((enum built_in_function)
10794 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10796 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10797 *q = TOLOWER (*p);
10798 *q = '\0';
10800 /* For -ftrapping-math these should throw from a former
10801 -fnon-call-exception stmt. */
10802 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10803 NULL);
10804 local_define_builtin (built_in_names[mcode], ftype, mcode,
10805 built_in_names[mcode],
10806 ECF_CONST | ECF_LEAF);
10808 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10809 NULL);
10810 local_define_builtin (built_in_names[dcode], ftype, dcode,
10811 built_in_names[dcode],
10812 ECF_CONST | ECF_LEAF);
10816 init_internal_fns ();
10819 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10820 better way.
10822 If we requested a pointer to a vector, build up the pointers that
10823 we stripped off while looking for the inner type. Similarly for
10824 return values from functions.
10826 The argument TYPE is the top of the chain, and BOTTOM is the
10827 new type which we will point to. */
10829 tree
10830 reconstruct_complex_type (tree type, tree bottom)
10832 tree inner, outer;
10834 if (TREE_CODE (type) == POINTER_TYPE)
10836 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10837 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10838 TYPE_REF_CAN_ALIAS_ALL (type));
10840 else if (TREE_CODE (type) == REFERENCE_TYPE)
10842 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10843 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10844 TYPE_REF_CAN_ALIAS_ALL (type));
10846 else if (TREE_CODE (type) == ARRAY_TYPE)
10848 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10849 outer = build_array_type (inner, TYPE_DOMAIN (type));
10851 else if (TREE_CODE (type) == FUNCTION_TYPE)
10853 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10854 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
10856 else if (TREE_CODE (type) == METHOD_TYPE)
10858 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10859 /* The build_method_type_directly() routine prepends 'this' to argument list,
10860 so we must compensate by getting rid of it. */
10861 outer
10862 = build_method_type_directly
10863 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10864 inner,
10865 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10867 else if (TREE_CODE (type) == OFFSET_TYPE)
10869 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10870 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10872 else
10873 return bottom;
10875 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10876 TYPE_QUALS (type));
10879 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10880 the inner type. */
10881 tree
10882 build_vector_type_for_mode (tree innertype, machine_mode mode)
10884 poly_int64 nunits;
10885 unsigned int bitsize;
10887 switch (GET_MODE_CLASS (mode))
10889 case MODE_VECTOR_BOOL:
10890 case MODE_VECTOR_INT:
10891 case MODE_VECTOR_FLOAT:
10892 case MODE_VECTOR_FRACT:
10893 case MODE_VECTOR_UFRACT:
10894 case MODE_VECTOR_ACCUM:
10895 case MODE_VECTOR_UACCUM:
10896 nunits = GET_MODE_NUNITS (mode);
10897 break;
10899 case MODE_INT:
10900 /* Check that there are no leftover bits. */
10901 bitsize = GET_MODE_BITSIZE (as_a <scalar_int_mode> (mode));
10902 gcc_assert (bitsize % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10903 nunits = bitsize / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10904 break;
10906 default:
10907 gcc_unreachable ();
10910 return make_vector_type (innertype, nunits, mode);
10913 /* Similarly, but takes the inner type and number of units, which must be
10914 a power of two. */
10916 tree
10917 build_vector_type (tree innertype, poly_int64 nunits)
10919 return make_vector_type (innertype, nunits, VOIDmode);
10922 /* Build truth vector with specified length and number of units. */
10924 tree
10925 build_truth_vector_type (poly_uint64 nunits, poly_uint64 vector_size)
10927 machine_mode mask_mode
10928 = targetm.vectorize.get_mask_mode (nunits, vector_size).else_blk ();
10930 poly_uint64 vsize;
10931 if (mask_mode == BLKmode)
10932 vsize = vector_size * BITS_PER_UNIT;
10933 else
10934 vsize = GET_MODE_BITSIZE (mask_mode);
10936 unsigned HOST_WIDE_INT esize = vector_element_size (vsize, nunits);
10938 tree bool_type = build_nonstandard_boolean_type (esize);
10940 return make_vector_type (bool_type, nunits, mask_mode);
10943 /* Returns a vector type corresponding to a comparison of VECTYPE. */
10945 tree
10946 build_same_sized_truth_vector_type (tree vectype)
10948 if (VECTOR_BOOLEAN_TYPE_P (vectype))
10949 return vectype;
10951 poly_uint64 size = GET_MODE_SIZE (TYPE_MODE (vectype));
10953 if (known_eq (size, 0U))
10954 size = tree_to_uhwi (TYPE_SIZE_UNIT (vectype));
10956 return build_truth_vector_type (TYPE_VECTOR_SUBPARTS (vectype), size);
10959 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10961 tree
10962 build_opaque_vector_type (tree innertype, poly_int64 nunits)
10964 tree t = make_vector_type (innertype, nunits, VOIDmode);
10965 tree cand;
10966 /* We always build the non-opaque variant before the opaque one,
10967 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10968 cand = TYPE_NEXT_VARIANT (t);
10969 if (cand
10970 && TYPE_VECTOR_OPAQUE (cand)
10971 && check_qualified_type (cand, t, TYPE_QUALS (t)))
10972 return cand;
10973 /* Othewise build a variant type and make sure to queue it after
10974 the non-opaque type. */
10975 cand = build_distinct_type_copy (t);
10976 TYPE_VECTOR_OPAQUE (cand) = true;
10977 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10978 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10979 TYPE_NEXT_VARIANT (t) = cand;
10980 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10981 return cand;
10984 /* Return the value of element I of VECTOR_CST T as a wide_int. */
10986 wide_int
10987 vector_cst_int_elt (const_tree t, unsigned int i)
10989 /* First handle elements that are directly encoded. */
10990 unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
10991 if (i < encoded_nelts)
10992 return wi::to_wide (VECTOR_CST_ENCODED_ELT (t, i));
10994 /* Identify the pattern that contains element I and work out the index of
10995 the last encoded element for that pattern. */
10996 unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
10997 unsigned int pattern = i % npatterns;
10998 unsigned int count = i / npatterns;
10999 unsigned int final_i = encoded_nelts - npatterns + pattern;
11001 /* If there are no steps, the final encoded value is the right one. */
11002 if (!VECTOR_CST_STEPPED_P (t))
11003 return wi::to_wide (VECTOR_CST_ENCODED_ELT (t, final_i));
11005 /* Otherwise work out the value from the last two encoded elements. */
11006 tree v1 = VECTOR_CST_ENCODED_ELT (t, final_i - npatterns);
11007 tree v2 = VECTOR_CST_ENCODED_ELT (t, final_i);
11008 wide_int diff = wi::to_wide (v2) - wi::to_wide (v1);
11009 return wi::to_wide (v2) + (count - 2) * diff;
11012 /* Return the value of element I of VECTOR_CST T. */
11014 tree
11015 vector_cst_elt (const_tree t, unsigned int i)
11017 /* First handle elements that are directly encoded. */
11018 unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
11019 if (i < encoded_nelts)
11020 return VECTOR_CST_ENCODED_ELT (t, i);
11022 /* If there are no steps, the final encoded value is the right one. */
11023 if (!VECTOR_CST_STEPPED_P (t))
11025 /* Identify the pattern that contains element I and work out the index of
11026 the last encoded element for that pattern. */
11027 unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
11028 unsigned int pattern = i % npatterns;
11029 unsigned int final_i = encoded_nelts - npatterns + pattern;
11030 return VECTOR_CST_ENCODED_ELT (t, final_i);
11033 /* Otherwise work out the value from the last two encoded elements. */
11034 return wide_int_to_tree (TREE_TYPE (TREE_TYPE (t)),
11035 vector_cst_int_elt (t, i));
11038 /* Given an initializer INIT, return TRUE if INIT is zero or some
11039 aggregate of zeros. Otherwise return FALSE. If NONZERO is not
11040 null, set *NONZERO if and only if INIT is known not to be all
11041 zeros. The combination of return value of false and *NONZERO
11042 false implies that INIT may but need not be all zeros. Other
11043 combinations indicate definitive answers. */
11045 bool
11046 initializer_zerop (const_tree init, bool *nonzero /* = NULL */)
11048 bool dummy;
11049 if (!nonzero)
11050 nonzero = &dummy;
11052 /* Conservatively clear NONZERO and set it only if INIT is definitely
11053 not all zero. */
11054 *nonzero = false;
11056 STRIP_NOPS (init);
11058 unsigned HOST_WIDE_INT off = 0;
11060 switch (TREE_CODE (init))
11062 case INTEGER_CST:
11063 if (integer_zerop (init))
11064 return true;
11066 *nonzero = true;
11067 return false;
11069 case REAL_CST:
11070 /* ??? Note that this is not correct for C4X float formats. There,
11071 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
11072 negative exponent. */
11073 if (real_zerop (init)
11074 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init)))
11075 return true;
11077 *nonzero = true;
11078 return false;
11080 case FIXED_CST:
11081 if (fixed_zerop (init))
11082 return true;
11084 *nonzero = true;
11085 return false;
11087 case COMPLEX_CST:
11088 if (integer_zerop (init)
11089 || (real_zerop (init)
11090 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
11091 && !REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init)))))
11092 return true;
11094 *nonzero = true;
11095 return false;
11097 case VECTOR_CST:
11098 if (VECTOR_CST_NPATTERNS (init) == 1
11099 && VECTOR_CST_DUPLICATE_P (init)
11100 && initializer_zerop (VECTOR_CST_ENCODED_ELT (init, 0)))
11101 return true;
11103 *nonzero = true;
11104 return false;
11106 case CONSTRUCTOR:
11108 if (TREE_CLOBBER_P (init))
11109 return false;
11111 unsigned HOST_WIDE_INT idx;
11112 tree elt;
11114 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
11115 if (!initializer_zerop (elt, nonzero))
11116 return false;
11118 return true;
11121 case MEM_REF:
11123 tree arg = TREE_OPERAND (init, 0);
11124 if (TREE_CODE (arg) != ADDR_EXPR)
11125 return false;
11126 tree offset = TREE_OPERAND (init, 1);
11127 if (TREE_CODE (offset) != INTEGER_CST
11128 || !tree_fits_uhwi_p (offset))
11129 return false;
11130 off = tree_to_uhwi (offset);
11131 if (INT_MAX < off)
11132 return false;
11133 arg = TREE_OPERAND (arg, 0);
11134 if (TREE_CODE (arg) != STRING_CST)
11135 return false;
11136 init = arg;
11138 /* Fall through. */
11140 case STRING_CST:
11142 gcc_assert (off <= INT_MAX);
11144 int i = off;
11145 int n = TREE_STRING_LENGTH (init);
11146 if (n <= i)
11147 return false;
11149 /* We need to loop through all elements to handle cases like
11150 "\0" and "\0foobar". */
11151 for (i = 0; i < n; ++i)
11152 if (TREE_STRING_POINTER (init)[i] != '\0')
11154 *nonzero = true;
11155 return false;
11158 return true;
11161 default:
11162 return false;
11166 /* Check if vector VEC consists of all the equal elements and
11167 that the number of elements corresponds to the type of VEC.
11168 The function returns first element of the vector
11169 or NULL_TREE if the vector is not uniform. */
11170 tree
11171 uniform_vector_p (const_tree vec)
11173 tree first, t;
11174 unsigned HOST_WIDE_INT i, nelts;
11176 if (vec == NULL_TREE)
11177 return NULL_TREE;
11179 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
11181 if (TREE_CODE (vec) == VEC_DUPLICATE_EXPR)
11182 return TREE_OPERAND (vec, 0);
11184 else if (TREE_CODE (vec) == VECTOR_CST)
11186 if (VECTOR_CST_NPATTERNS (vec) == 1 && VECTOR_CST_DUPLICATE_P (vec))
11187 return VECTOR_CST_ENCODED_ELT (vec, 0);
11188 return NULL_TREE;
11191 else if (TREE_CODE (vec) == CONSTRUCTOR
11192 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)).is_constant (&nelts))
11194 first = error_mark_node;
11196 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
11198 if (i == 0)
11200 first = t;
11201 continue;
11203 if (!operand_equal_p (first, t, 0))
11204 return NULL_TREE;
11206 if (i != nelts)
11207 return NULL_TREE;
11209 return first;
11212 return NULL_TREE;
11215 /* Build an empty statement at location LOC. */
11217 tree
11218 build_empty_stmt (location_t loc)
11220 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
11221 SET_EXPR_LOCATION (t, loc);
11222 return t;
11226 /* Build an OpenMP clause with code CODE. LOC is the location of the
11227 clause. */
11229 tree
11230 build_omp_clause (location_t loc, enum omp_clause_code code)
11232 tree t;
11233 int size, length;
11235 length = omp_clause_num_ops[code];
11236 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
11238 record_node_allocation_statistics (OMP_CLAUSE, size);
11240 t = (tree) ggc_internal_alloc (size);
11241 memset (t, 0, size);
11242 TREE_SET_CODE (t, OMP_CLAUSE);
11243 OMP_CLAUSE_SET_CODE (t, code);
11244 OMP_CLAUSE_LOCATION (t) = loc;
11246 return t;
11249 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
11250 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
11251 Except for the CODE and operand count field, other storage for the
11252 object is initialized to zeros. */
11254 tree
11255 build_vl_exp (enum tree_code code, int len MEM_STAT_DECL)
11257 tree t;
11258 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
11260 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
11261 gcc_assert (len >= 1);
11263 record_node_allocation_statistics (code, length);
11265 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
11267 TREE_SET_CODE (t, code);
11269 /* Can't use TREE_OPERAND to store the length because if checking is
11270 enabled, it will try to check the length before we store it. :-P */
11271 t->exp.operands[0] = build_int_cst (sizetype, len);
11273 return t;
11276 /* Helper function for build_call_* functions; build a CALL_EXPR with
11277 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
11278 the argument slots. */
11280 static tree
11281 build_call_1 (tree return_type, tree fn, int nargs)
11283 tree t;
11285 t = build_vl_exp (CALL_EXPR, nargs + 3);
11286 TREE_TYPE (t) = return_type;
11287 CALL_EXPR_FN (t) = fn;
11288 CALL_EXPR_STATIC_CHAIN (t) = NULL;
11290 return t;
11293 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
11294 FN and a null static chain slot. NARGS is the number of call arguments
11295 which are specified as "..." arguments. */
11297 tree
11298 build_call_nary (tree return_type, tree fn, int nargs, ...)
11300 tree ret;
11301 va_list args;
11302 va_start (args, nargs);
11303 ret = build_call_valist (return_type, fn, nargs, args);
11304 va_end (args);
11305 return ret;
11308 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
11309 FN and a null static chain slot. NARGS is the number of call arguments
11310 which are specified as a va_list ARGS. */
11312 tree
11313 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
11315 tree t;
11316 int i;
11318 t = build_call_1 (return_type, fn, nargs);
11319 for (i = 0; i < nargs; i++)
11320 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
11321 process_call_operands (t);
11322 return t;
11325 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
11326 FN and a null static chain slot. NARGS is the number of call arguments
11327 which are specified as a tree array ARGS. */
11329 tree
11330 build_call_array_loc (location_t loc, tree return_type, tree fn,
11331 int nargs, const tree *args)
11333 tree t;
11334 int i;
11336 t = build_call_1 (return_type, fn, nargs);
11337 for (i = 0; i < nargs; i++)
11338 CALL_EXPR_ARG (t, i) = args[i];
11339 process_call_operands (t);
11340 SET_EXPR_LOCATION (t, loc);
11341 return t;
11344 /* Like build_call_array, but takes a vec. */
11346 tree
11347 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
11349 tree ret, t;
11350 unsigned int ix;
11352 ret = build_call_1 (return_type, fn, vec_safe_length (args));
11353 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
11354 CALL_EXPR_ARG (ret, ix) = t;
11355 process_call_operands (ret);
11356 return ret;
11359 /* Conveniently construct a function call expression. FNDECL names the
11360 function to be called and N arguments are passed in the array
11361 ARGARRAY. */
11363 tree
11364 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
11366 tree fntype = TREE_TYPE (fndecl);
11367 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11369 return fold_build_call_array_loc (loc, TREE_TYPE (fntype), fn, n, argarray);
11372 /* Conveniently construct a function call expression. FNDECL names the
11373 function to be called and the arguments are passed in the vector
11374 VEC. */
11376 tree
11377 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
11379 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
11380 vec_safe_address (vec));
11384 /* Conveniently construct a function call expression. FNDECL names the
11385 function to be called, N is the number of arguments, and the "..."
11386 parameters are the argument expressions. */
11388 tree
11389 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
11391 va_list ap;
11392 tree *argarray = XALLOCAVEC (tree, n);
11393 int i;
11395 va_start (ap, n);
11396 for (i = 0; i < n; i++)
11397 argarray[i] = va_arg (ap, tree);
11398 va_end (ap);
11399 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11402 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11403 varargs macros aren't supported by all bootstrap compilers. */
11405 tree
11406 build_call_expr (tree fndecl, int n, ...)
11408 va_list ap;
11409 tree *argarray = XALLOCAVEC (tree, n);
11410 int i;
11412 va_start (ap, n);
11413 for (i = 0; i < n; i++)
11414 argarray[i] = va_arg (ap, tree);
11415 va_end (ap);
11416 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
11419 /* Build an internal call to IFN, with arguments ARGS[0:N-1] and with return
11420 type TYPE. This is just like CALL_EXPR, except its CALL_EXPR_FN is NULL.
11421 It will get gimplified later into an ordinary internal function. */
11423 tree
11424 build_call_expr_internal_loc_array (location_t loc, internal_fn ifn,
11425 tree type, int n, const tree *args)
11427 tree t = build_call_1 (type, NULL_TREE, n);
11428 for (int i = 0; i < n; ++i)
11429 CALL_EXPR_ARG (t, i) = args[i];
11430 SET_EXPR_LOCATION (t, loc);
11431 CALL_EXPR_IFN (t) = ifn;
11432 return t;
11435 /* Build internal call expression. This is just like CALL_EXPR, except
11436 its CALL_EXPR_FN is NULL. It will get gimplified later into ordinary
11437 internal function. */
11439 tree
11440 build_call_expr_internal_loc (location_t loc, enum internal_fn ifn,
11441 tree type, int n, ...)
11443 va_list ap;
11444 tree *argarray = XALLOCAVEC (tree, n);
11445 int i;
11447 va_start (ap, n);
11448 for (i = 0; i < n; i++)
11449 argarray[i] = va_arg (ap, tree);
11450 va_end (ap);
11451 return build_call_expr_internal_loc_array (loc, ifn, type, n, argarray);
11454 /* Return a function call to FN, if the target is guaranteed to support it,
11455 or null otherwise.
11457 N is the number of arguments, passed in the "...", and TYPE is the
11458 type of the return value. */
11460 tree
11461 maybe_build_call_expr_loc (location_t loc, combined_fn fn, tree type,
11462 int n, ...)
11464 va_list ap;
11465 tree *argarray = XALLOCAVEC (tree, n);
11466 int i;
11468 va_start (ap, n);
11469 for (i = 0; i < n; i++)
11470 argarray[i] = va_arg (ap, tree);
11471 va_end (ap);
11472 if (internal_fn_p (fn))
11474 internal_fn ifn = as_internal_fn (fn);
11475 if (direct_internal_fn_p (ifn))
11477 tree_pair types = direct_internal_fn_types (ifn, type, argarray);
11478 if (!direct_internal_fn_supported_p (ifn, types,
11479 OPTIMIZE_FOR_BOTH))
11480 return NULL_TREE;
11482 return build_call_expr_internal_loc_array (loc, ifn, type, n, argarray);
11484 else
11486 tree fndecl = builtin_decl_implicit (as_builtin_fn (fn));
11487 if (!fndecl)
11488 return NULL_TREE;
11489 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11493 /* Return a function call to the appropriate builtin alloca variant.
11495 SIZE is the size to be allocated. ALIGN, if non-zero, is the requested
11496 alignment of the allocated area. MAX_SIZE, if non-negative, is an upper
11497 bound for SIZE in case it is not a fixed value. */
11499 tree
11500 build_alloca_call_expr (tree size, unsigned int align, HOST_WIDE_INT max_size)
11502 if (max_size >= 0)
11504 tree t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX);
11505 return
11506 build_call_expr (t, 3, size, size_int (align), size_int (max_size));
11508 else if (align > 0)
11510 tree t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
11511 return build_call_expr (t, 2, size, size_int (align));
11513 else
11515 tree t = builtin_decl_explicit (BUILT_IN_ALLOCA);
11516 return build_call_expr (t, 1, size);
11520 /* Create a new constant string literal and return a char* pointer to it.
11521 The STRING_CST value is the LEN characters at STR. */
11522 tree
11523 build_string_literal (int len, const char *str)
11525 tree t, elem, index, type;
11527 t = build_string (len, str);
11528 elem = build_type_variant (char_type_node, 1, 0);
11529 index = build_index_type (size_int (len - 1));
11530 type = build_array_type (elem, index);
11531 TREE_TYPE (t) = type;
11532 TREE_CONSTANT (t) = 1;
11533 TREE_READONLY (t) = 1;
11534 TREE_STATIC (t) = 1;
11536 type = build_pointer_type (elem);
11537 t = build1 (ADDR_EXPR, type,
11538 build4 (ARRAY_REF, elem,
11539 t, integer_zero_node, NULL_TREE, NULL_TREE));
11540 return t;
11545 /* Return true if T (assumed to be a DECL) must be assigned a memory
11546 location. */
11548 bool
11549 needs_to_live_in_memory (const_tree t)
11551 return (TREE_ADDRESSABLE (t)
11552 || is_global_var (t)
11553 || (TREE_CODE (t) == RESULT_DECL
11554 && !DECL_BY_REFERENCE (t)
11555 && aggregate_value_p (t, current_function_decl)));
11558 /* Return value of a constant X and sign-extend it. */
11560 HOST_WIDE_INT
11561 int_cst_value (const_tree x)
11563 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
11564 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
11566 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
11567 gcc_assert (cst_and_fits_in_hwi (x));
11569 if (bits < HOST_BITS_PER_WIDE_INT)
11571 bool negative = ((val >> (bits - 1)) & 1) != 0;
11572 if (negative)
11573 val |= HOST_WIDE_INT_M1U << (bits - 1) << 1;
11574 else
11575 val &= ~(HOST_WIDE_INT_M1U << (bits - 1) << 1);
11578 return val;
11581 /* If TYPE is an integral or pointer type, return an integer type with
11582 the same precision which is unsigned iff UNSIGNEDP is true, or itself
11583 if TYPE is already an integer type of signedness UNSIGNEDP. */
11585 tree
11586 signed_or_unsigned_type_for (int unsignedp, tree type)
11588 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) == unsignedp)
11589 return type;
11591 if (TREE_CODE (type) == VECTOR_TYPE)
11593 tree inner = TREE_TYPE (type);
11594 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
11595 if (!inner2)
11596 return NULL_TREE;
11597 if (inner == inner2)
11598 return type;
11599 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
11602 if (TREE_CODE (type) == COMPLEX_TYPE)
11604 tree inner = TREE_TYPE (type);
11605 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
11606 if (!inner2)
11607 return NULL_TREE;
11608 if (inner == inner2)
11609 return type;
11610 return build_complex_type (inner2);
11613 if (!INTEGRAL_TYPE_P (type)
11614 && !POINTER_TYPE_P (type)
11615 && TREE_CODE (type) != OFFSET_TYPE)
11616 return NULL_TREE;
11618 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
11621 /* If TYPE is an integral or pointer type, return an integer type with
11622 the same precision which is unsigned, or itself if TYPE is already an
11623 unsigned integer type. */
11625 tree
11626 unsigned_type_for (tree type)
11628 return signed_or_unsigned_type_for (1, type);
11631 /* If TYPE is an integral or pointer type, return an integer type with
11632 the same precision which is signed, or itself if TYPE is already a
11633 signed integer type. */
11635 tree
11636 signed_type_for (tree type)
11638 return signed_or_unsigned_type_for (0, type);
11641 /* If TYPE is a vector type, return a signed integer vector type with the
11642 same width and number of subparts. Otherwise return boolean_type_node. */
11644 tree
11645 truth_type_for (tree type)
11647 if (TREE_CODE (type) == VECTOR_TYPE)
11649 if (VECTOR_BOOLEAN_TYPE_P (type))
11650 return type;
11651 return build_truth_vector_type (TYPE_VECTOR_SUBPARTS (type),
11652 GET_MODE_SIZE (TYPE_MODE (type)));
11654 else
11655 return boolean_type_node;
11658 /* Returns the largest value obtainable by casting something in INNER type to
11659 OUTER type. */
11661 tree
11662 upper_bound_in_type (tree outer, tree inner)
11664 unsigned int det = 0;
11665 unsigned oprec = TYPE_PRECISION (outer);
11666 unsigned iprec = TYPE_PRECISION (inner);
11667 unsigned prec;
11669 /* Compute a unique number for every combination. */
11670 det |= (oprec > iprec) ? 4 : 0;
11671 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
11672 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
11674 /* Determine the exponent to use. */
11675 switch (det)
11677 case 0:
11678 case 1:
11679 /* oprec <= iprec, outer: signed, inner: don't care. */
11680 prec = oprec - 1;
11681 break;
11682 case 2:
11683 case 3:
11684 /* oprec <= iprec, outer: unsigned, inner: don't care. */
11685 prec = oprec;
11686 break;
11687 case 4:
11688 /* oprec > iprec, outer: signed, inner: signed. */
11689 prec = iprec - 1;
11690 break;
11691 case 5:
11692 /* oprec > iprec, outer: signed, inner: unsigned. */
11693 prec = iprec;
11694 break;
11695 case 6:
11696 /* oprec > iprec, outer: unsigned, inner: signed. */
11697 prec = oprec;
11698 break;
11699 case 7:
11700 /* oprec > iprec, outer: unsigned, inner: unsigned. */
11701 prec = iprec;
11702 break;
11703 default:
11704 gcc_unreachable ();
11707 return wide_int_to_tree (outer,
11708 wi::mask (prec, false, TYPE_PRECISION (outer)));
11711 /* Returns the smallest value obtainable by casting something in INNER type to
11712 OUTER type. */
11714 tree
11715 lower_bound_in_type (tree outer, tree inner)
11717 unsigned oprec = TYPE_PRECISION (outer);
11718 unsigned iprec = TYPE_PRECISION (inner);
11720 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
11721 and obtain 0. */
11722 if (TYPE_UNSIGNED (outer)
11723 /* If we are widening something of an unsigned type, OUTER type
11724 contains all values of INNER type. In particular, both INNER
11725 and OUTER types have zero in common. */
11726 || (oprec > iprec && TYPE_UNSIGNED (inner)))
11727 return build_int_cst (outer, 0);
11728 else
11730 /* If we are widening a signed type to another signed type, we
11731 want to obtain -2^^(iprec-1). If we are keeping the
11732 precision or narrowing to a signed type, we want to obtain
11733 -2^(oprec-1). */
11734 unsigned prec = oprec > iprec ? iprec : oprec;
11735 return wide_int_to_tree (outer,
11736 wi::mask (prec - 1, true,
11737 TYPE_PRECISION (outer)));
11741 /* Return nonzero if two operands that are suitable for PHI nodes are
11742 necessarily equal. Specifically, both ARG0 and ARG1 must be either
11743 SSA_NAME or invariant. Note that this is strictly an optimization.
11744 That is, callers of this function can directly call operand_equal_p
11745 and get the same result, only slower. */
11748 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
11750 if (arg0 == arg1)
11751 return 1;
11752 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
11753 return 0;
11754 return operand_equal_p (arg0, arg1, 0);
11757 /* Returns number of zeros at the end of binary representation of X. */
11759 tree
11760 num_ending_zeros (const_tree x)
11762 return build_int_cst (TREE_TYPE (x), wi::ctz (wi::to_wide (x)));
11766 #define WALK_SUBTREE(NODE) \
11767 do \
11769 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
11770 if (result) \
11771 return result; \
11773 while (0)
11775 /* This is a subroutine of walk_tree that walks field of TYPE that are to
11776 be walked whenever a type is seen in the tree. Rest of operands and return
11777 value are as for walk_tree. */
11779 static tree
11780 walk_type_fields (tree type, walk_tree_fn func, void *data,
11781 hash_set<tree> *pset, walk_tree_lh lh)
11783 tree result = NULL_TREE;
11785 switch (TREE_CODE (type))
11787 case POINTER_TYPE:
11788 case REFERENCE_TYPE:
11789 case VECTOR_TYPE:
11790 /* We have to worry about mutually recursive pointers. These can't
11791 be written in C. They can in Ada. It's pathological, but
11792 there's an ACATS test (c38102a) that checks it. Deal with this
11793 by checking if we're pointing to another pointer, that one
11794 points to another pointer, that one does too, and we have no htab.
11795 If so, get a hash table. We check three levels deep to avoid
11796 the cost of the hash table if we don't need one. */
11797 if (POINTER_TYPE_P (TREE_TYPE (type))
11798 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
11799 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
11800 && !pset)
11802 result = walk_tree_without_duplicates (&TREE_TYPE (type),
11803 func, data);
11804 if (result)
11805 return result;
11807 break;
11810 /* fall through */
11812 case COMPLEX_TYPE:
11813 WALK_SUBTREE (TREE_TYPE (type));
11814 break;
11816 case METHOD_TYPE:
11817 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
11819 /* Fall through. */
11821 case FUNCTION_TYPE:
11822 WALK_SUBTREE (TREE_TYPE (type));
11824 tree arg;
11826 /* We never want to walk into default arguments. */
11827 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
11828 WALK_SUBTREE (TREE_VALUE (arg));
11830 break;
11832 case ARRAY_TYPE:
11833 /* Don't follow this nodes's type if a pointer for fear that
11834 we'll have infinite recursion. If we have a PSET, then we
11835 need not fear. */
11836 if (pset
11837 || (!POINTER_TYPE_P (TREE_TYPE (type))
11838 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
11839 WALK_SUBTREE (TREE_TYPE (type));
11840 WALK_SUBTREE (TYPE_DOMAIN (type));
11841 break;
11843 case OFFSET_TYPE:
11844 WALK_SUBTREE (TREE_TYPE (type));
11845 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
11846 break;
11848 default:
11849 break;
11852 return NULL_TREE;
11855 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
11856 called with the DATA and the address of each sub-tree. If FUNC returns a
11857 non-NULL value, the traversal is stopped, and the value returned by FUNC
11858 is returned. If PSET is non-NULL it is used to record the nodes visited,
11859 and to avoid visiting a node more than once. */
11861 tree
11862 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
11863 hash_set<tree> *pset, walk_tree_lh lh)
11865 enum tree_code code;
11866 int walk_subtrees;
11867 tree result;
11869 #define WALK_SUBTREE_TAIL(NODE) \
11870 do \
11872 tp = & (NODE); \
11873 goto tail_recurse; \
11875 while (0)
11877 tail_recurse:
11878 /* Skip empty subtrees. */
11879 if (!*tp)
11880 return NULL_TREE;
11882 /* Don't walk the same tree twice, if the user has requested
11883 that we avoid doing so. */
11884 if (pset && pset->add (*tp))
11885 return NULL_TREE;
11887 /* Call the function. */
11888 walk_subtrees = 1;
11889 result = (*func) (tp, &walk_subtrees, data);
11891 /* If we found something, return it. */
11892 if (result)
11893 return result;
11895 code = TREE_CODE (*tp);
11897 /* Even if we didn't, FUNC may have decided that there was nothing
11898 interesting below this point in the tree. */
11899 if (!walk_subtrees)
11901 /* But we still need to check our siblings. */
11902 if (code == TREE_LIST)
11903 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11904 else if (code == OMP_CLAUSE)
11905 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11906 else
11907 return NULL_TREE;
11910 if (lh)
11912 result = (*lh) (tp, &walk_subtrees, func, data, pset);
11913 if (result || !walk_subtrees)
11914 return result;
11917 switch (code)
11919 case ERROR_MARK:
11920 case IDENTIFIER_NODE:
11921 case INTEGER_CST:
11922 case REAL_CST:
11923 case FIXED_CST:
11924 case VECTOR_CST:
11925 case STRING_CST:
11926 case BLOCK:
11927 case PLACEHOLDER_EXPR:
11928 case SSA_NAME:
11929 case FIELD_DECL:
11930 case RESULT_DECL:
11931 /* None of these have subtrees other than those already walked
11932 above. */
11933 break;
11935 case TREE_LIST:
11936 WALK_SUBTREE (TREE_VALUE (*tp));
11937 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11938 break;
11940 case TREE_VEC:
11942 int len = TREE_VEC_LENGTH (*tp);
11944 if (len == 0)
11945 break;
11947 /* Walk all elements but the first. */
11948 while (--len)
11949 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
11951 /* Now walk the first one as a tail call. */
11952 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
11955 case COMPLEX_CST:
11956 WALK_SUBTREE (TREE_REALPART (*tp));
11957 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
11959 case CONSTRUCTOR:
11961 unsigned HOST_WIDE_INT idx;
11962 constructor_elt *ce;
11964 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*tp), idx, &ce);
11965 idx++)
11966 WALK_SUBTREE (ce->value);
11968 break;
11970 case SAVE_EXPR:
11971 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
11973 case BIND_EXPR:
11975 tree decl;
11976 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
11978 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11979 into declarations that are just mentioned, rather than
11980 declared; they don't really belong to this part of the tree.
11981 And, we can see cycles: the initializer for a declaration
11982 can refer to the declaration itself. */
11983 WALK_SUBTREE (DECL_INITIAL (decl));
11984 WALK_SUBTREE (DECL_SIZE (decl));
11985 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11987 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
11990 case STATEMENT_LIST:
11992 tree_stmt_iterator i;
11993 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
11994 WALK_SUBTREE (*tsi_stmt_ptr (i));
11996 break;
11998 case OMP_CLAUSE:
11999 switch (OMP_CLAUSE_CODE (*tp))
12001 case OMP_CLAUSE_GANG:
12002 case OMP_CLAUSE__GRIDDIM_:
12003 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
12004 /* FALLTHRU */
12006 case OMP_CLAUSE_ASYNC:
12007 case OMP_CLAUSE_WAIT:
12008 case OMP_CLAUSE_WORKER:
12009 case OMP_CLAUSE_VECTOR:
12010 case OMP_CLAUSE_NUM_GANGS:
12011 case OMP_CLAUSE_NUM_WORKERS:
12012 case OMP_CLAUSE_VECTOR_LENGTH:
12013 case OMP_CLAUSE_PRIVATE:
12014 case OMP_CLAUSE_SHARED:
12015 case OMP_CLAUSE_FIRSTPRIVATE:
12016 case OMP_CLAUSE_COPYIN:
12017 case OMP_CLAUSE_COPYPRIVATE:
12018 case OMP_CLAUSE_FINAL:
12019 case OMP_CLAUSE_IF:
12020 case OMP_CLAUSE_NUM_THREADS:
12021 case OMP_CLAUSE_SCHEDULE:
12022 case OMP_CLAUSE_UNIFORM:
12023 case OMP_CLAUSE_DEPEND:
12024 case OMP_CLAUSE_NONTEMPORAL:
12025 case OMP_CLAUSE_NUM_TEAMS:
12026 case OMP_CLAUSE_THREAD_LIMIT:
12027 case OMP_CLAUSE_DEVICE:
12028 case OMP_CLAUSE_DIST_SCHEDULE:
12029 case OMP_CLAUSE_SAFELEN:
12030 case OMP_CLAUSE_SIMDLEN:
12031 case OMP_CLAUSE_ORDERED:
12032 case OMP_CLAUSE_PRIORITY:
12033 case OMP_CLAUSE_GRAINSIZE:
12034 case OMP_CLAUSE_NUM_TASKS:
12035 case OMP_CLAUSE_HINT:
12036 case OMP_CLAUSE_TO_DECLARE:
12037 case OMP_CLAUSE_LINK:
12038 case OMP_CLAUSE_USE_DEVICE_PTR:
12039 case OMP_CLAUSE_IS_DEVICE_PTR:
12040 case OMP_CLAUSE__LOOPTEMP_:
12041 case OMP_CLAUSE__REDUCTEMP_:
12042 case OMP_CLAUSE__SIMDUID_:
12043 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
12044 /* FALLTHRU */
12046 case OMP_CLAUSE_INDEPENDENT:
12047 case OMP_CLAUSE_NOWAIT:
12048 case OMP_CLAUSE_DEFAULT:
12049 case OMP_CLAUSE_UNTIED:
12050 case OMP_CLAUSE_MERGEABLE:
12051 case OMP_CLAUSE_PROC_BIND:
12052 case OMP_CLAUSE_INBRANCH:
12053 case OMP_CLAUSE_NOTINBRANCH:
12054 case OMP_CLAUSE_FOR:
12055 case OMP_CLAUSE_PARALLEL:
12056 case OMP_CLAUSE_SECTIONS:
12057 case OMP_CLAUSE_TASKGROUP:
12058 case OMP_CLAUSE_NOGROUP:
12059 case OMP_CLAUSE_THREADS:
12060 case OMP_CLAUSE_SIMD:
12061 case OMP_CLAUSE_DEFAULTMAP:
12062 case OMP_CLAUSE_AUTO:
12063 case OMP_CLAUSE_SEQ:
12064 case OMP_CLAUSE_TILE:
12065 case OMP_CLAUSE__SIMT_:
12066 case OMP_CLAUSE_IF_PRESENT:
12067 case OMP_CLAUSE_FINALIZE:
12068 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
12070 case OMP_CLAUSE_LASTPRIVATE:
12071 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
12072 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
12073 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
12075 case OMP_CLAUSE_COLLAPSE:
12077 int i;
12078 for (i = 0; i < 3; i++)
12079 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
12080 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
12083 case OMP_CLAUSE_LINEAR:
12084 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
12085 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STEP (*tp));
12086 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STMT (*tp));
12087 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
12089 case OMP_CLAUSE_ALIGNED:
12090 case OMP_CLAUSE_FROM:
12091 case OMP_CLAUSE_TO:
12092 case OMP_CLAUSE_MAP:
12093 case OMP_CLAUSE__CACHE_:
12094 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
12095 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
12096 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
12098 case OMP_CLAUSE_REDUCTION:
12099 case OMP_CLAUSE_TASK_REDUCTION:
12100 case OMP_CLAUSE_IN_REDUCTION:
12102 int i;
12103 for (i = 0; i < 5; i++)
12104 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
12105 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
12108 default:
12109 gcc_unreachable ();
12111 break;
12113 case TARGET_EXPR:
12115 int i, len;
12117 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
12118 But, we only want to walk once. */
12119 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
12120 for (i = 0; i < len; ++i)
12121 WALK_SUBTREE (TREE_OPERAND (*tp, i));
12122 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
12125 case DECL_EXPR:
12126 /* If this is a TYPE_DECL, walk into the fields of the type that it's
12127 defining. We only want to walk into these fields of a type in this
12128 case and not in the general case of a mere reference to the type.
12130 The criterion is as follows: if the field can be an expression, it
12131 must be walked only here. This should be in keeping with the fields
12132 that are directly gimplified in gimplify_type_sizes in order for the
12133 mark/copy-if-shared/unmark machinery of the gimplifier to work with
12134 variable-sized types.
12136 Note that DECLs get walked as part of processing the BIND_EXPR. */
12137 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
12139 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
12140 if (TREE_CODE (*type_p) == ERROR_MARK)
12141 return NULL_TREE;
12143 /* Call the function for the type. See if it returns anything or
12144 doesn't want us to continue. If we are to continue, walk both
12145 the normal fields and those for the declaration case. */
12146 result = (*func) (type_p, &walk_subtrees, data);
12147 if (result || !walk_subtrees)
12148 return result;
12150 /* But do not walk a pointed-to type since it may itself need to
12151 be walked in the declaration case if it isn't anonymous. */
12152 if (!POINTER_TYPE_P (*type_p))
12154 result = walk_type_fields (*type_p, func, data, pset, lh);
12155 if (result)
12156 return result;
12159 /* If this is a record type, also walk the fields. */
12160 if (RECORD_OR_UNION_TYPE_P (*type_p))
12162 tree field;
12164 for (field = TYPE_FIELDS (*type_p); field;
12165 field = DECL_CHAIN (field))
12167 /* We'd like to look at the type of the field, but we can
12168 easily get infinite recursion. So assume it's pointed
12169 to elsewhere in the tree. Also, ignore things that
12170 aren't fields. */
12171 if (TREE_CODE (field) != FIELD_DECL)
12172 continue;
12174 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
12175 WALK_SUBTREE (DECL_SIZE (field));
12176 WALK_SUBTREE (DECL_SIZE_UNIT (field));
12177 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
12178 WALK_SUBTREE (DECL_QUALIFIER (field));
12182 /* Same for scalar types. */
12183 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
12184 || TREE_CODE (*type_p) == ENUMERAL_TYPE
12185 || TREE_CODE (*type_p) == INTEGER_TYPE
12186 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
12187 || TREE_CODE (*type_p) == REAL_TYPE)
12189 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
12190 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
12193 WALK_SUBTREE (TYPE_SIZE (*type_p));
12194 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
12196 /* FALLTHRU */
12198 default:
12199 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
12201 int i, len;
12203 /* Walk over all the sub-trees of this operand. */
12204 len = TREE_OPERAND_LENGTH (*tp);
12206 /* Go through the subtrees. We need to do this in forward order so
12207 that the scope of a FOR_EXPR is handled properly. */
12208 if (len)
12210 for (i = 0; i < len - 1; ++i)
12211 WALK_SUBTREE (TREE_OPERAND (*tp, i));
12212 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
12215 /* If this is a type, walk the needed fields in the type. */
12216 else if (TYPE_P (*tp))
12217 return walk_type_fields (*tp, func, data, pset, lh);
12218 break;
12221 /* We didn't find what we were looking for. */
12222 return NULL_TREE;
12224 #undef WALK_SUBTREE_TAIL
12226 #undef WALK_SUBTREE
12228 /* Like walk_tree, but does not walk duplicate nodes more than once. */
12230 tree
12231 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
12232 walk_tree_lh lh)
12234 tree result;
12236 hash_set<tree> pset;
12237 result = walk_tree_1 (tp, func, data, &pset, lh);
12238 return result;
12242 tree
12243 tree_block (tree t)
12245 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
12247 if (IS_EXPR_CODE_CLASS (c))
12248 return LOCATION_BLOCK (t->exp.locus);
12249 gcc_unreachable ();
12250 return NULL;
12253 void
12254 tree_set_block (tree t, tree b)
12256 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
12258 if (IS_EXPR_CODE_CLASS (c))
12260 t->exp.locus = set_block (t->exp.locus, b);
12262 else
12263 gcc_unreachable ();
12266 /* Create a nameless artificial label and put it in the current
12267 function context. The label has a location of LOC. Returns the
12268 newly created label. */
12270 tree
12271 create_artificial_label (location_t loc)
12273 tree lab = build_decl (loc,
12274 LABEL_DECL, NULL_TREE, void_type_node);
12276 DECL_ARTIFICIAL (lab) = 1;
12277 DECL_IGNORED_P (lab) = 1;
12278 DECL_CONTEXT (lab) = current_function_decl;
12279 return lab;
12282 /* Given a tree, try to return a useful variable name that we can use
12283 to prefix a temporary that is being assigned the value of the tree.
12284 I.E. given <temp> = &A, return A. */
12286 const char *
12287 get_name (tree t)
12289 tree stripped_decl;
12291 stripped_decl = t;
12292 STRIP_NOPS (stripped_decl);
12293 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
12294 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
12295 else if (TREE_CODE (stripped_decl) == SSA_NAME)
12297 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
12298 if (!name)
12299 return NULL;
12300 return IDENTIFIER_POINTER (name);
12302 else
12304 switch (TREE_CODE (stripped_decl))
12306 case ADDR_EXPR:
12307 return get_name (TREE_OPERAND (stripped_decl, 0));
12308 default:
12309 return NULL;
12314 /* Return true if TYPE has a variable argument list. */
12316 bool
12317 stdarg_p (const_tree fntype)
12319 function_args_iterator args_iter;
12320 tree n = NULL_TREE, t;
12322 if (!fntype)
12323 return false;
12325 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
12327 n = t;
12330 return n != NULL_TREE && n != void_type_node;
12333 /* Return true if TYPE has a prototype. */
12335 bool
12336 prototype_p (const_tree fntype)
12338 tree t;
12340 gcc_assert (fntype != NULL_TREE);
12342 t = TYPE_ARG_TYPES (fntype);
12343 return (t != NULL_TREE);
12346 /* If BLOCK is inlined from an __attribute__((__artificial__))
12347 routine, return pointer to location from where it has been
12348 called. */
12349 location_t *
12350 block_nonartificial_location (tree block)
12352 location_t *ret = NULL;
12354 while (block && TREE_CODE (block) == BLOCK
12355 && BLOCK_ABSTRACT_ORIGIN (block))
12357 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
12358 if (TREE_CODE (ao) == FUNCTION_DECL)
12360 /* If AO is an artificial inline, point RET to the
12361 call site locus at which it has been inlined and continue
12362 the loop, in case AO's caller is also an artificial
12363 inline. */
12364 if (DECL_DECLARED_INLINE_P (ao)
12365 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
12366 ret = &BLOCK_SOURCE_LOCATION (block);
12367 else
12368 break;
12370 else if (TREE_CODE (ao) != BLOCK)
12371 break;
12373 block = BLOCK_SUPERCONTEXT (block);
12375 return ret;
12379 /* If EXP is inlined from an __attribute__((__artificial__))
12380 function, return the location of the original call expression. */
12382 location_t
12383 tree_nonartificial_location (tree exp)
12385 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
12387 if (loc)
12388 return *loc;
12389 else
12390 return EXPR_LOCATION (exp);
12394 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
12395 nodes. */
12397 /* Return the hash code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
12399 hashval_t
12400 cl_option_hasher::hash (tree x)
12402 const_tree const t = x;
12403 const char *p;
12404 size_t i;
12405 size_t len = 0;
12406 hashval_t hash = 0;
12408 if (TREE_CODE (t) == OPTIMIZATION_NODE)
12410 p = (const char *)TREE_OPTIMIZATION (t);
12411 len = sizeof (struct cl_optimization);
12414 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
12415 return cl_target_option_hash (TREE_TARGET_OPTION (t));
12417 else
12418 gcc_unreachable ();
12420 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
12421 something else. */
12422 for (i = 0; i < len; i++)
12423 if (p[i])
12424 hash = (hash << 4) ^ ((i << 2) | p[i]);
12426 return hash;
12429 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
12430 TARGET_OPTION tree node) is the same as that given by *Y, which is the
12431 same. */
12433 bool
12434 cl_option_hasher::equal (tree x, tree y)
12436 const_tree const xt = x;
12437 const_tree const yt = y;
12439 if (TREE_CODE (xt) != TREE_CODE (yt))
12440 return 0;
12442 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
12443 return cl_optimization_option_eq (TREE_OPTIMIZATION (xt),
12444 TREE_OPTIMIZATION (yt));
12445 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
12446 return cl_target_option_eq (TREE_TARGET_OPTION (xt),
12447 TREE_TARGET_OPTION (yt));
12448 else
12449 gcc_unreachable ();
12452 /* Build an OPTIMIZATION_NODE based on the options in OPTS. */
12454 tree
12455 build_optimization_node (struct gcc_options *opts)
12457 tree t;
12459 /* Use the cache of optimization nodes. */
12461 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
12462 opts);
12464 tree *slot = cl_option_hash_table->find_slot (cl_optimization_node, INSERT);
12465 t = *slot;
12466 if (!t)
12468 /* Insert this one into the hash table. */
12469 t = cl_optimization_node;
12470 *slot = t;
12472 /* Make a new node for next time round. */
12473 cl_optimization_node = make_node (OPTIMIZATION_NODE);
12476 return t;
12479 /* Build a TARGET_OPTION_NODE based on the options in OPTS. */
12481 tree
12482 build_target_option_node (struct gcc_options *opts)
12484 tree t;
12486 /* Use the cache of optimization nodes. */
12488 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
12489 opts);
12491 tree *slot = cl_option_hash_table->find_slot (cl_target_option_node, INSERT);
12492 t = *slot;
12493 if (!t)
12495 /* Insert this one into the hash table. */
12496 t = cl_target_option_node;
12497 *slot = t;
12499 /* Make a new node for next time round. */
12500 cl_target_option_node = make_node (TARGET_OPTION_NODE);
12503 return t;
12506 /* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
12507 so that they aren't saved during PCH writing. */
12509 void
12510 prepare_target_option_nodes_for_pch (void)
12512 hash_table<cl_option_hasher>::iterator iter = cl_option_hash_table->begin ();
12513 for (; iter != cl_option_hash_table->end (); ++iter)
12514 if (TREE_CODE (*iter) == TARGET_OPTION_NODE)
12515 TREE_TARGET_GLOBALS (*iter) = NULL;
12518 /* Determine the "ultimate origin" of a block. */
12520 tree
12521 block_ultimate_origin (const_tree block)
12523 tree origin = BLOCK_ABSTRACT_ORIGIN (block);
12525 if (origin == NULL_TREE)
12526 return NULL_TREE;
12527 else
12529 gcc_checking_assert ((DECL_P (origin)
12530 && DECL_ORIGIN (origin) == origin)
12531 || BLOCK_ORIGIN (origin) == origin);
12532 return origin;
12536 /* Return true iff conversion from INNER_TYPE to OUTER_TYPE generates
12537 no instruction. */
12539 bool
12540 tree_nop_conversion_p (const_tree outer_type, const_tree inner_type)
12542 /* Do not strip casts into or out of differing address spaces. */
12543 if (POINTER_TYPE_P (outer_type)
12544 && TYPE_ADDR_SPACE (TREE_TYPE (outer_type)) != ADDR_SPACE_GENERIC)
12546 if (!POINTER_TYPE_P (inner_type)
12547 || (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
12548 != TYPE_ADDR_SPACE (TREE_TYPE (inner_type))))
12549 return false;
12551 else if (POINTER_TYPE_P (inner_type)
12552 && TYPE_ADDR_SPACE (TREE_TYPE (inner_type)) != ADDR_SPACE_GENERIC)
12554 /* We already know that outer_type is not a pointer with
12555 a non-generic address space. */
12556 return false;
12559 /* Use precision rather then machine mode when we can, which gives
12560 the correct answer even for submode (bit-field) types. */
12561 if ((INTEGRAL_TYPE_P (outer_type)
12562 || POINTER_TYPE_P (outer_type)
12563 || TREE_CODE (outer_type) == OFFSET_TYPE)
12564 && (INTEGRAL_TYPE_P (inner_type)
12565 || POINTER_TYPE_P (inner_type)
12566 || TREE_CODE (inner_type) == OFFSET_TYPE))
12567 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
12569 /* Otherwise fall back on comparing machine modes (e.g. for
12570 aggregate types, floats). */
12571 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
12574 /* Return true iff conversion in EXP generates no instruction. Mark
12575 it inline so that we fully inline into the stripping functions even
12576 though we have two uses of this function. */
12578 static inline bool
12579 tree_nop_conversion (const_tree exp)
12581 tree outer_type, inner_type;
12583 if (location_wrapper_p (exp))
12584 return true;
12585 if (!CONVERT_EXPR_P (exp)
12586 && TREE_CODE (exp) != NON_LVALUE_EXPR)
12587 return false;
12588 if (TREE_OPERAND (exp, 0) == error_mark_node)
12589 return false;
12591 outer_type = TREE_TYPE (exp);
12592 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
12594 if (!inner_type)
12595 return false;
12597 return tree_nop_conversion_p (outer_type, inner_type);
12600 /* Return true iff conversion in EXP generates no instruction. Don't
12601 consider conversions changing the signedness. */
12603 static bool
12604 tree_sign_nop_conversion (const_tree exp)
12606 tree outer_type, inner_type;
12608 if (!tree_nop_conversion (exp))
12609 return false;
12611 outer_type = TREE_TYPE (exp);
12612 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
12614 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
12615 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
12618 /* Strip conversions from EXP according to tree_nop_conversion and
12619 return the resulting expression. */
12621 tree
12622 tree_strip_nop_conversions (tree exp)
12624 while (tree_nop_conversion (exp))
12625 exp = TREE_OPERAND (exp, 0);
12626 return exp;
12629 /* Strip conversions from EXP according to tree_sign_nop_conversion
12630 and return the resulting expression. */
12632 tree
12633 tree_strip_sign_nop_conversions (tree exp)
12635 while (tree_sign_nop_conversion (exp))
12636 exp = TREE_OPERAND (exp, 0);
12637 return exp;
12640 /* Avoid any floating point extensions from EXP. */
12641 tree
12642 strip_float_extensions (tree exp)
12644 tree sub, expt, subt;
12646 /* For floating point constant look up the narrowest type that can hold
12647 it properly and handle it like (type)(narrowest_type)constant.
12648 This way we can optimize for instance a=a*2.0 where "a" is float
12649 but 2.0 is double constant. */
12650 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
12652 REAL_VALUE_TYPE orig;
12653 tree type = NULL;
12655 orig = TREE_REAL_CST (exp);
12656 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
12657 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
12658 type = float_type_node;
12659 else if (TYPE_PRECISION (TREE_TYPE (exp))
12660 > TYPE_PRECISION (double_type_node)
12661 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
12662 type = double_type_node;
12663 if (type)
12664 return build_real_truncate (type, orig);
12667 if (!CONVERT_EXPR_P (exp))
12668 return exp;
12670 sub = TREE_OPERAND (exp, 0);
12671 subt = TREE_TYPE (sub);
12672 expt = TREE_TYPE (exp);
12674 if (!FLOAT_TYPE_P (subt))
12675 return exp;
12677 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
12678 return exp;
12680 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
12681 return exp;
12683 return strip_float_extensions (sub);
12686 /* Strip out all handled components that produce invariant
12687 offsets. */
12689 const_tree
12690 strip_invariant_refs (const_tree op)
12692 while (handled_component_p (op))
12694 switch (TREE_CODE (op))
12696 case ARRAY_REF:
12697 case ARRAY_RANGE_REF:
12698 if (!is_gimple_constant (TREE_OPERAND (op, 1))
12699 || TREE_OPERAND (op, 2) != NULL_TREE
12700 || TREE_OPERAND (op, 3) != NULL_TREE)
12701 return NULL;
12702 break;
12704 case COMPONENT_REF:
12705 if (TREE_OPERAND (op, 2) != NULL_TREE)
12706 return NULL;
12707 break;
12709 default:;
12711 op = TREE_OPERAND (op, 0);
12714 return op;
12717 static GTY(()) tree gcc_eh_personality_decl;
12719 /* Return the GCC personality function decl. */
12721 tree
12722 lhd_gcc_personality (void)
12724 if (!gcc_eh_personality_decl)
12725 gcc_eh_personality_decl = build_personality_function ("gcc");
12726 return gcc_eh_personality_decl;
12729 /* TARGET is a call target of GIMPLE call statement
12730 (obtained by gimple_call_fn). Return true if it is
12731 OBJ_TYPE_REF representing an virtual call of C++ method.
12732 (As opposed to OBJ_TYPE_REF representing objc calls
12733 through a cast where middle-end devirtualization machinery
12734 can't apply.) */
12736 bool
12737 virtual_method_call_p (const_tree target)
12739 if (TREE_CODE (target) != OBJ_TYPE_REF)
12740 return false;
12741 tree t = TREE_TYPE (target);
12742 gcc_checking_assert (TREE_CODE (t) == POINTER_TYPE);
12743 t = TREE_TYPE (t);
12744 if (TREE_CODE (t) == FUNCTION_TYPE)
12745 return false;
12746 gcc_checking_assert (TREE_CODE (t) == METHOD_TYPE);
12747 /* If we do not have BINFO associated, it means that type was built
12748 without devirtualization enabled. Do not consider this a virtual
12749 call. */
12750 if (!TYPE_BINFO (obj_type_ref_class (target)))
12751 return false;
12752 return true;
12755 /* REF is OBJ_TYPE_REF, return the class the ref corresponds to. */
12757 tree
12758 obj_type_ref_class (const_tree ref)
12760 gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF);
12761 ref = TREE_TYPE (ref);
12762 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
12763 ref = TREE_TYPE (ref);
12764 /* We look for type THIS points to. ObjC also builds
12765 OBJ_TYPE_REF with non-method calls, Their first parameter
12766 ID however also corresponds to class type. */
12767 gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE
12768 || TREE_CODE (ref) == FUNCTION_TYPE);
12769 ref = TREE_VALUE (TYPE_ARG_TYPES (ref));
12770 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
12771 return TREE_TYPE (ref);
12774 /* Lookup sub-BINFO of BINFO of TYPE at offset POS. */
12776 static tree
12777 lookup_binfo_at_offset (tree binfo, tree type, HOST_WIDE_INT pos)
12779 unsigned int i;
12780 tree base_binfo, b;
12782 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12783 if (pos == tree_to_shwi (BINFO_OFFSET (base_binfo))
12784 && types_same_for_odr (TREE_TYPE (base_binfo), type))
12785 return base_binfo;
12786 else if ((b = lookup_binfo_at_offset (base_binfo, type, pos)) != NULL)
12787 return b;
12788 return NULL;
12791 /* Try to find a base info of BINFO that would have its field decl at offset
12792 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
12793 found, return, otherwise return NULL_TREE. */
12795 tree
12796 get_binfo_at_offset (tree binfo, poly_int64 offset, tree expected_type)
12798 tree type = BINFO_TYPE (binfo);
12800 while (true)
12802 HOST_WIDE_INT pos, size;
12803 tree fld;
12804 int i;
12806 if (types_same_for_odr (type, expected_type))
12807 return binfo;
12808 if (maybe_lt (offset, 0))
12809 return NULL_TREE;
12811 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
12813 if (TREE_CODE (fld) != FIELD_DECL || !DECL_ARTIFICIAL (fld))
12814 continue;
12816 pos = int_bit_position (fld);
12817 size = tree_to_uhwi (DECL_SIZE (fld));
12818 if (known_in_range_p (offset, pos, size))
12819 break;
12821 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
12822 return NULL_TREE;
12824 /* Offset 0 indicates the primary base, whose vtable contents are
12825 represented in the binfo for the derived class. */
12826 else if (maybe_ne (offset, 0))
12828 tree found_binfo = NULL, base_binfo;
12829 /* Offsets in BINFO are in bytes relative to the whole structure
12830 while POS is in bits relative to the containing field. */
12831 int binfo_offset = (tree_to_shwi (BINFO_OFFSET (binfo)) + pos
12832 / BITS_PER_UNIT);
12834 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12835 if (tree_to_shwi (BINFO_OFFSET (base_binfo)) == binfo_offset
12836 && types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
12838 found_binfo = base_binfo;
12839 break;
12841 if (found_binfo)
12842 binfo = found_binfo;
12843 else
12844 binfo = lookup_binfo_at_offset (binfo, TREE_TYPE (fld),
12845 binfo_offset);
12848 type = TREE_TYPE (fld);
12849 offset -= pos;
12853 /* Returns true if X is a typedef decl. */
12855 bool
12856 is_typedef_decl (const_tree x)
12858 return (x && TREE_CODE (x) == TYPE_DECL
12859 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
12862 /* Returns true iff TYPE is a type variant created for a typedef. */
12864 bool
12865 typedef_variant_p (const_tree type)
12867 return is_typedef_decl (TYPE_NAME (type));
12870 /* A class to handle converting a string that might contain
12871 control characters, (eg newline, form-feed, etc), into one
12872 in which contains escape sequences instead. */
12874 class escaped_string
12876 public:
12877 escaped_string () { m_owned = false; m_str = NULL; };
12878 ~escaped_string () { if (m_owned) free (m_str); }
12879 operator const char *() const { return (const char *) m_str; }
12880 void escape (const char *);
12881 private:
12882 char *m_str;
12883 bool m_owned;
12886 /* PR 84195: Replace control characters in "unescaped" with their
12887 escaped equivalents. Allow newlines if -fmessage-length has
12888 been set to a non-zero value. This is done here, rather than
12889 where the attribute is recorded as the message length can
12890 change between these two locations. */
12892 void
12893 escaped_string::escape (const char *unescaped)
12895 char *escaped;
12896 size_t i, new_i, len;
12898 if (m_owned)
12899 free (m_str);
12901 m_str = const_cast<char *> (unescaped);
12902 m_owned = false;
12904 if (unescaped == NULL || *unescaped == 0)
12905 return;
12907 len = strlen (unescaped);
12908 escaped = NULL;
12909 new_i = 0;
12911 for (i = 0; i < len; i++)
12913 char c = unescaped[i];
12915 if (!ISCNTRL (c))
12917 if (escaped)
12918 escaped[new_i++] = c;
12919 continue;
12922 if (c != '\n' || !pp_is_wrapping_line (global_dc->printer))
12924 if (escaped == NULL)
12926 /* We only allocate space for a new string if we
12927 actually encounter a control character that
12928 needs replacing. */
12929 escaped = (char *) xmalloc (len * 2 + 1);
12930 strncpy (escaped, unescaped, i);
12931 new_i = i;
12934 escaped[new_i++] = '\\';
12936 switch (c)
12938 case '\a': escaped[new_i++] = 'a'; break;
12939 case '\b': escaped[new_i++] = 'b'; break;
12940 case '\f': escaped[new_i++] = 'f'; break;
12941 case '\n': escaped[new_i++] = 'n'; break;
12942 case '\r': escaped[new_i++] = 'r'; break;
12943 case '\t': escaped[new_i++] = 't'; break;
12944 case '\v': escaped[new_i++] = 'v'; break;
12945 default: escaped[new_i++] = '?'; break;
12948 else if (escaped)
12949 escaped[new_i++] = c;
12952 if (escaped)
12954 escaped[new_i] = 0;
12955 m_str = escaped;
12956 m_owned = true;
12960 /* Warn about a use of an identifier which was marked deprecated. Returns
12961 whether a warning was given. */
12963 bool
12964 warn_deprecated_use (tree node, tree attr)
12966 escaped_string msg;
12968 if (node == 0 || !warn_deprecated_decl)
12969 return false;
12971 if (!attr)
12973 if (DECL_P (node))
12974 attr = DECL_ATTRIBUTES (node);
12975 else if (TYPE_P (node))
12977 tree decl = TYPE_STUB_DECL (node);
12978 if (decl)
12979 attr = lookup_attribute ("deprecated",
12980 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
12984 if (attr)
12985 attr = lookup_attribute ("deprecated", attr);
12987 if (attr)
12988 msg.escape (TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
12990 bool w = false;
12991 if (DECL_P (node))
12993 auto_diagnostic_group d;
12994 if (msg)
12995 w = warning (OPT_Wdeprecated_declarations,
12996 "%qD is deprecated: %s", node, (const char *) msg);
12997 else
12998 w = warning (OPT_Wdeprecated_declarations,
12999 "%qD is deprecated", node);
13000 if (w)
13001 inform (DECL_SOURCE_LOCATION (node), "declared here");
13003 else if (TYPE_P (node))
13005 tree what = NULL_TREE;
13006 tree decl = TYPE_STUB_DECL (node);
13008 if (TYPE_NAME (node))
13010 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
13011 what = TYPE_NAME (node);
13012 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
13013 && DECL_NAME (TYPE_NAME (node)))
13014 what = DECL_NAME (TYPE_NAME (node));
13017 auto_diagnostic_group d;
13018 if (what)
13020 if (msg)
13021 w = warning (OPT_Wdeprecated_declarations,
13022 "%qE is deprecated: %s", what, (const char *) msg);
13023 else
13024 w = warning (OPT_Wdeprecated_declarations,
13025 "%qE is deprecated", what);
13027 else
13029 if (msg)
13030 w = warning (OPT_Wdeprecated_declarations,
13031 "type is deprecated: %s", (const char *) msg);
13032 else
13033 w = warning (OPT_Wdeprecated_declarations,
13034 "type is deprecated");
13037 if (w && decl)
13038 inform (DECL_SOURCE_LOCATION (decl), "declared here");
13041 return w;
13044 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
13045 somewhere in it. */
13047 bool
13048 contains_bitfld_component_ref_p (const_tree ref)
13050 while (handled_component_p (ref))
13052 if (TREE_CODE (ref) == COMPONENT_REF
13053 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
13054 return true;
13055 ref = TREE_OPERAND (ref, 0);
13058 return false;
13061 /* Try to determine whether a TRY_CATCH expression can fall through.
13062 This is a subroutine of block_may_fallthru. */
13064 static bool
13065 try_catch_may_fallthru (const_tree stmt)
13067 tree_stmt_iterator i;
13069 /* If the TRY block can fall through, the whole TRY_CATCH can
13070 fall through. */
13071 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
13072 return true;
13074 i = tsi_start (TREE_OPERAND (stmt, 1));
13075 switch (TREE_CODE (tsi_stmt (i)))
13077 case CATCH_EXPR:
13078 /* We expect to see a sequence of CATCH_EXPR trees, each with a
13079 catch expression and a body. The whole TRY_CATCH may fall
13080 through iff any of the catch bodies falls through. */
13081 for (; !tsi_end_p (i); tsi_next (&i))
13083 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
13084 return true;
13086 return false;
13088 case EH_FILTER_EXPR:
13089 /* The exception filter expression only matters if there is an
13090 exception. If the exception does not match EH_FILTER_TYPES,
13091 we will execute EH_FILTER_FAILURE, and we will fall through
13092 if that falls through. If the exception does match
13093 EH_FILTER_TYPES, the stack unwinder will continue up the
13094 stack, so we will not fall through. We don't know whether we
13095 will throw an exception which matches EH_FILTER_TYPES or not,
13096 so we just ignore EH_FILTER_TYPES and assume that we might
13097 throw an exception which doesn't match. */
13098 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
13100 default:
13101 /* This case represents statements to be executed when an
13102 exception occurs. Those statements are implicitly followed
13103 by a RESX statement to resume execution after the exception.
13104 So in this case the TRY_CATCH never falls through. */
13105 return false;
13109 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
13110 need not be 100% accurate; simply be conservative and return true if we
13111 don't know. This is used only to avoid stupidly generating extra code.
13112 If we're wrong, we'll just delete the extra code later. */
13114 bool
13115 block_may_fallthru (const_tree block)
13117 /* This CONST_CAST is okay because expr_last returns its argument
13118 unmodified and we assign it to a const_tree. */
13119 const_tree stmt = expr_last (CONST_CAST_TREE (block));
13121 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
13123 case GOTO_EXPR:
13124 case RETURN_EXPR:
13125 /* Easy cases. If the last statement of the block implies
13126 control transfer, then we can't fall through. */
13127 return false;
13129 case SWITCH_EXPR:
13130 /* If there is a default: label or case labels cover all possible
13131 SWITCH_COND values, then the SWITCH_EXPR will transfer control
13132 to some case label in all cases and all we care is whether the
13133 SWITCH_BODY falls through. */
13134 if (SWITCH_ALL_CASES_P (stmt))
13135 return block_may_fallthru (SWITCH_BODY (stmt));
13136 return true;
13138 case COND_EXPR:
13139 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
13140 return true;
13141 return block_may_fallthru (COND_EXPR_ELSE (stmt));
13143 case BIND_EXPR:
13144 return block_may_fallthru (BIND_EXPR_BODY (stmt));
13146 case TRY_CATCH_EXPR:
13147 return try_catch_may_fallthru (stmt);
13149 case TRY_FINALLY_EXPR:
13150 /* The finally clause is always executed after the try clause,
13151 so if it does not fall through, then the try-finally will not
13152 fall through. Otherwise, if the try clause does not fall
13153 through, then when the finally clause falls through it will
13154 resume execution wherever the try clause was going. So the
13155 whole try-finally will only fall through if both the try
13156 clause and the finally clause fall through. */
13157 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
13158 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
13160 case MODIFY_EXPR:
13161 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
13162 stmt = TREE_OPERAND (stmt, 1);
13163 else
13164 return true;
13165 /* FALLTHRU */
13167 case CALL_EXPR:
13168 /* Functions that do not return do not fall through. */
13169 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
13171 case CLEANUP_POINT_EXPR:
13172 return block_may_fallthru (TREE_OPERAND (stmt, 0));
13174 case TARGET_EXPR:
13175 return block_may_fallthru (TREE_OPERAND (stmt, 1));
13177 case ERROR_MARK:
13178 return true;
13180 default:
13181 return lang_hooks.block_may_fallthru (stmt);
13185 /* True if we are using EH to handle cleanups. */
13186 static bool using_eh_for_cleanups_flag = false;
13188 /* This routine is called from front ends to indicate eh should be used for
13189 cleanups. */
13190 void
13191 using_eh_for_cleanups (void)
13193 using_eh_for_cleanups_flag = true;
13196 /* Query whether EH is used for cleanups. */
13197 bool
13198 using_eh_for_cleanups_p (void)
13200 return using_eh_for_cleanups_flag;
13203 /* Wrapper for tree_code_name to ensure that tree code is valid */
13204 const char *
13205 get_tree_code_name (enum tree_code code)
13207 const char *invalid = "<invalid tree code>";
13209 if (code >= MAX_TREE_CODES)
13210 return invalid;
13212 return tree_code_name[code];
13215 /* Drops the TREE_OVERFLOW flag from T. */
13217 tree
13218 drop_tree_overflow (tree t)
13220 gcc_checking_assert (TREE_OVERFLOW (t));
13222 /* For tree codes with a sharing machinery re-build the result. */
13223 if (poly_int_tree_p (t))
13224 return wide_int_to_tree (TREE_TYPE (t), wi::to_poly_wide (t));
13226 /* For VECTOR_CST, remove the overflow bits from the encoded elements
13227 and canonicalize the result. */
13228 if (TREE_CODE (t) == VECTOR_CST)
13230 tree_vector_builder builder;
13231 builder.new_unary_operation (TREE_TYPE (t), t, true);
13232 unsigned int count = builder.encoded_nelts ();
13233 for (unsigned int i = 0; i < count; ++i)
13235 tree elt = VECTOR_CST_ELT (t, i);
13236 if (TREE_OVERFLOW (elt))
13237 elt = drop_tree_overflow (elt);
13238 builder.quick_push (elt);
13240 return builder.build ();
13243 /* Otherwise, as all tcc_constants are possibly shared, copy the node
13244 and drop the flag. */
13245 t = copy_node (t);
13246 TREE_OVERFLOW (t) = 0;
13248 /* For constants that contain nested constants, drop the flag
13249 from those as well. */
13250 if (TREE_CODE (t) == COMPLEX_CST)
13252 if (TREE_OVERFLOW (TREE_REALPART (t)))
13253 TREE_REALPART (t) = drop_tree_overflow (TREE_REALPART (t));
13254 if (TREE_OVERFLOW (TREE_IMAGPART (t)))
13255 TREE_IMAGPART (t) = drop_tree_overflow (TREE_IMAGPART (t));
13258 return t;
13261 /* Given a memory reference expression T, return its base address.
13262 The base address of a memory reference expression is the main
13263 object being referenced. For instance, the base address for
13264 'array[i].fld[j]' is 'array'. You can think of this as stripping
13265 away the offset part from a memory address.
13267 This function calls handled_component_p to strip away all the inner
13268 parts of the memory reference until it reaches the base object. */
13270 tree
13271 get_base_address (tree t)
13273 while (handled_component_p (t))
13274 t = TREE_OPERAND (t, 0);
13276 if ((TREE_CODE (t) == MEM_REF
13277 || TREE_CODE (t) == TARGET_MEM_REF)
13278 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
13279 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
13281 /* ??? Either the alias oracle or all callers need to properly deal
13282 with WITH_SIZE_EXPRs before we can look through those. */
13283 if (TREE_CODE (t) == WITH_SIZE_EXPR)
13284 return NULL_TREE;
13286 return t;
13289 /* Return a tree of sizetype representing the size, in bytes, of the element
13290 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
13292 tree
13293 array_ref_element_size (tree exp)
13295 tree aligned_size = TREE_OPERAND (exp, 3);
13296 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
13297 location_t loc = EXPR_LOCATION (exp);
13299 /* If a size was specified in the ARRAY_REF, it's the size measured
13300 in alignment units of the element type. So multiply by that value. */
13301 if (aligned_size)
13303 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
13304 sizetype from another type of the same width and signedness. */
13305 if (TREE_TYPE (aligned_size) != sizetype)
13306 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
13307 return size_binop_loc (loc, MULT_EXPR, aligned_size,
13308 size_int (TYPE_ALIGN_UNIT (elmt_type)));
13311 /* Otherwise, take the size from that of the element type. Substitute
13312 any PLACEHOLDER_EXPR that we have. */
13313 else
13314 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
13317 /* Return a tree representing the lower bound of the array mentioned in
13318 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
13320 tree
13321 array_ref_low_bound (tree exp)
13323 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
13325 /* If a lower bound is specified in EXP, use it. */
13326 if (TREE_OPERAND (exp, 2))
13327 return TREE_OPERAND (exp, 2);
13329 /* Otherwise, if there is a domain type and it has a lower bound, use it,
13330 substituting for a PLACEHOLDER_EXPR as needed. */
13331 if (domain_type && TYPE_MIN_VALUE (domain_type))
13332 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
13334 /* Otherwise, return a zero of the appropriate type. */
13335 return build_int_cst (TREE_TYPE (TREE_OPERAND (exp, 1)), 0);
13338 /* Return a tree representing the upper bound of the array mentioned in
13339 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
13341 tree
13342 array_ref_up_bound (tree exp)
13344 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
13346 /* If there is a domain type and it has an upper bound, use it, substituting
13347 for a PLACEHOLDER_EXPR as needed. */
13348 if (domain_type && TYPE_MAX_VALUE (domain_type))
13349 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
13351 /* Otherwise fail. */
13352 return NULL_TREE;
13355 /* Returns true if REF is an array reference or a component reference
13356 to an array at the end of a structure.
13357 If this is the case, the array may be allocated larger
13358 than its upper bound implies. */
13360 bool
13361 array_at_struct_end_p (tree ref)
13363 tree atype;
13365 if (TREE_CODE (ref) == ARRAY_REF
13366 || TREE_CODE (ref) == ARRAY_RANGE_REF)
13368 atype = TREE_TYPE (TREE_OPERAND (ref, 0));
13369 ref = TREE_OPERAND (ref, 0);
13371 else if (TREE_CODE (ref) == COMPONENT_REF
13372 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 1))) == ARRAY_TYPE)
13373 atype = TREE_TYPE (TREE_OPERAND (ref, 1));
13374 else
13375 return false;
13377 if (TREE_CODE (ref) == STRING_CST)
13378 return false;
13380 tree ref_to_array = ref;
13381 while (handled_component_p (ref))
13383 /* If the reference chain contains a component reference to a
13384 non-union type and there follows another field the reference
13385 is not at the end of a structure. */
13386 if (TREE_CODE (ref) == COMPONENT_REF)
13388 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
13390 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
13391 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
13392 nextf = DECL_CHAIN (nextf);
13393 if (nextf)
13394 return false;
13397 /* If we have a multi-dimensional array we do not consider
13398 a non-innermost dimension as flex array if the whole
13399 multi-dimensional array is at struct end.
13400 Same for an array of aggregates with a trailing array
13401 member. */
13402 else if (TREE_CODE (ref) == ARRAY_REF)
13403 return false;
13404 else if (TREE_CODE (ref) == ARRAY_RANGE_REF)
13406 /* If we view an underlying object as sth else then what we
13407 gathered up to now is what we have to rely on. */
13408 else if (TREE_CODE (ref) == VIEW_CONVERT_EXPR)
13409 break;
13410 else
13411 gcc_unreachable ();
13413 ref = TREE_OPERAND (ref, 0);
13416 /* The array now is at struct end. Treat flexible arrays as
13417 always subject to extend, even into just padding constrained by
13418 an underlying decl. */
13419 if (! TYPE_SIZE (atype)
13420 || ! TYPE_DOMAIN (atype)
13421 || ! TYPE_MAX_VALUE (TYPE_DOMAIN (atype)))
13422 return true;
13424 if (TREE_CODE (ref) == MEM_REF
13425 && TREE_CODE (TREE_OPERAND (ref, 0)) == ADDR_EXPR)
13426 ref = TREE_OPERAND (TREE_OPERAND (ref, 0), 0);
13428 /* If the reference is based on a declared entity, the size of the array
13429 is constrained by its given domain. (Do not trust commons PR/69368). */
13430 if (DECL_P (ref)
13431 && !(flag_unconstrained_commons
13432 && VAR_P (ref) && DECL_COMMON (ref))
13433 && DECL_SIZE_UNIT (ref)
13434 && TREE_CODE (DECL_SIZE_UNIT (ref)) == INTEGER_CST)
13436 /* Check whether the array domain covers all of the available
13437 padding. */
13438 poly_int64 offset;
13439 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (atype))) != INTEGER_CST
13440 || TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))) != INTEGER_CST
13441 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (atype))) != INTEGER_CST)
13442 return true;
13443 if (! get_addr_base_and_unit_offset (ref_to_array, &offset))
13444 return true;
13446 /* If at least one extra element fits it is a flexarray. */
13447 if (known_le ((wi::to_offset (TYPE_MAX_VALUE (TYPE_DOMAIN (atype)))
13448 - wi::to_offset (TYPE_MIN_VALUE (TYPE_DOMAIN (atype)))
13449 + 2)
13450 * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (atype))),
13451 wi::to_offset (DECL_SIZE_UNIT (ref)) - offset))
13452 return true;
13454 return false;
13457 return true;
13460 /* Return a tree representing the offset, in bytes, of the field referenced
13461 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
13463 tree
13464 component_ref_field_offset (tree exp)
13466 tree aligned_offset = TREE_OPERAND (exp, 2);
13467 tree field = TREE_OPERAND (exp, 1);
13468 location_t loc = EXPR_LOCATION (exp);
13470 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
13471 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
13472 value. */
13473 if (aligned_offset)
13475 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
13476 sizetype from another type of the same width and signedness. */
13477 if (TREE_TYPE (aligned_offset) != sizetype)
13478 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
13479 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
13480 size_int (DECL_OFFSET_ALIGN (field)
13481 / BITS_PER_UNIT));
13484 /* Otherwise, take the offset from that of the field. Substitute
13485 any PLACEHOLDER_EXPR that we have. */
13486 else
13487 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
13490 /* Return the machine mode of T. For vectors, returns the mode of the
13491 inner type. The main use case is to feed the result to HONOR_NANS,
13492 avoiding the BLKmode that a direct TYPE_MODE (T) might return. */
13494 machine_mode
13495 element_mode (const_tree t)
13497 if (!TYPE_P (t))
13498 t = TREE_TYPE (t);
13499 if (VECTOR_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
13500 t = TREE_TYPE (t);
13501 return TYPE_MODE (t);
13504 /* Vector types need to re-check the target flags each time we report
13505 the machine mode. We need to do this because attribute target can
13506 change the result of vector_mode_supported_p and have_regs_of_mode
13507 on a per-function basis. Thus the TYPE_MODE of a VECTOR_TYPE can
13508 change on a per-function basis. */
13509 /* ??? Possibly a better solution is to run through all the types
13510 referenced by a function and re-compute the TYPE_MODE once, rather
13511 than make the TYPE_MODE macro call a function. */
13513 machine_mode
13514 vector_type_mode (const_tree t)
13516 machine_mode mode;
13518 gcc_assert (TREE_CODE (t) == VECTOR_TYPE);
13520 mode = t->type_common.mode;
13521 if (VECTOR_MODE_P (mode)
13522 && (!targetm.vector_mode_supported_p (mode)
13523 || !have_regs_of_mode[mode]))
13525 scalar_int_mode innermode;
13527 /* For integers, try mapping it to a same-sized scalar mode. */
13528 if (is_int_mode (TREE_TYPE (t)->type_common.mode, &innermode))
13530 poly_int64 size = (TYPE_VECTOR_SUBPARTS (t)
13531 * GET_MODE_BITSIZE (innermode));
13532 scalar_int_mode mode;
13533 if (int_mode_for_size (size, 0).exists (&mode)
13534 && have_regs_of_mode[mode])
13535 return mode;
13538 return BLKmode;
13541 return mode;
13544 /* Verify that basic properties of T match TV and thus T can be a variant of
13545 TV. TV should be the more specified variant (i.e. the main variant). */
13547 static bool
13548 verify_type_variant (const_tree t, tree tv)
13550 /* Type variant can differ by:
13552 - TYPE_QUALS: TYPE_READONLY, TYPE_VOLATILE, TYPE_ATOMIC, TYPE_RESTRICT,
13553 ENCODE_QUAL_ADDR_SPACE.
13554 - main variant may be TYPE_COMPLETE_P and variant types !TYPE_COMPLETE_P
13555 in this case some values may not be set in the variant types
13556 (see TYPE_COMPLETE_P checks).
13557 - it is possible to have TYPE_ARTIFICIAL variant of non-artifical type
13558 - by TYPE_NAME and attributes (i.e. when variant originate by typedef)
13559 - TYPE_CANONICAL (TYPE_ALIAS_SET is the same among variants)
13560 - by the alignment: TYPE_ALIGN and TYPE_USER_ALIGN
13561 - during LTO by TYPE_CONTEXT if type is TYPE_FILE_SCOPE_P
13562 this is necessary to make it possible to merge types form different TUs
13563 - arrays, pointers and references may have TREE_TYPE that is a variant
13564 of TREE_TYPE of their main variants.
13565 - aggregates may have new TYPE_FIELDS list that list variants of
13566 the main variant TYPE_FIELDS.
13567 - vector types may differ by TYPE_VECTOR_OPAQUE
13570 /* Convenience macro for matching individual fields. */
13571 #define verify_variant_match(flag) \
13572 do { \
13573 if (flag (tv) != flag (t)) \
13575 error ("type variant differs by %s", #flag); \
13576 debug_tree (tv); \
13577 return false; \
13579 } while (false)
13581 /* tree_base checks. */
13583 verify_variant_match (TREE_CODE);
13584 /* FIXME: Ada builds non-artificial variants of artificial types. */
13585 if (TYPE_ARTIFICIAL (tv) && 0)
13586 verify_variant_match (TYPE_ARTIFICIAL);
13587 if (POINTER_TYPE_P (tv))
13588 verify_variant_match (TYPE_REF_CAN_ALIAS_ALL);
13589 /* FIXME: TYPE_SIZES_GIMPLIFIED may differs for Ada build. */
13590 verify_variant_match (TYPE_UNSIGNED);
13591 verify_variant_match (TYPE_PACKED);
13592 if (TREE_CODE (t) == REFERENCE_TYPE)
13593 verify_variant_match (TYPE_REF_IS_RVALUE);
13594 if (AGGREGATE_TYPE_P (t))
13595 verify_variant_match (TYPE_REVERSE_STORAGE_ORDER);
13596 else
13597 verify_variant_match (TYPE_SATURATING);
13598 /* FIXME: This check trigger during libstdc++ build. */
13599 if (RECORD_OR_UNION_TYPE_P (t) && COMPLETE_TYPE_P (t) && 0)
13600 verify_variant_match (TYPE_FINAL_P);
13602 /* tree_type_common checks. */
13604 if (COMPLETE_TYPE_P (t))
13606 verify_variant_match (TYPE_MODE);
13607 if (TREE_CODE (TYPE_SIZE (t)) != PLACEHOLDER_EXPR
13608 && TREE_CODE (TYPE_SIZE (tv)) != PLACEHOLDER_EXPR)
13609 verify_variant_match (TYPE_SIZE);
13610 if (TREE_CODE (TYPE_SIZE_UNIT (t)) != PLACEHOLDER_EXPR
13611 && TREE_CODE (TYPE_SIZE_UNIT (tv)) != PLACEHOLDER_EXPR
13612 && TYPE_SIZE_UNIT (t) != TYPE_SIZE_UNIT (tv))
13614 gcc_assert (!operand_equal_p (TYPE_SIZE_UNIT (t),
13615 TYPE_SIZE_UNIT (tv), 0));
13616 error ("type variant has different TYPE_SIZE_UNIT");
13617 debug_tree (tv);
13618 error ("type variant's TYPE_SIZE_UNIT");
13619 debug_tree (TYPE_SIZE_UNIT (tv));
13620 error ("type's TYPE_SIZE_UNIT");
13621 debug_tree (TYPE_SIZE_UNIT (t));
13622 return false;
13625 verify_variant_match (TYPE_PRECISION);
13626 verify_variant_match (TYPE_NEEDS_CONSTRUCTING);
13627 if (RECORD_OR_UNION_TYPE_P (t))
13628 verify_variant_match (TYPE_TRANSPARENT_AGGR);
13629 else if (TREE_CODE (t) == ARRAY_TYPE)
13630 verify_variant_match (TYPE_NONALIASED_COMPONENT);
13631 /* During LTO we merge variant lists from diferent translation units
13632 that may differ BY TYPE_CONTEXT that in turn may point
13633 to TRANSLATION_UNIT_DECL.
13634 Ada also builds variants of types with different TYPE_CONTEXT. */
13635 if ((!in_lto_p || !TYPE_FILE_SCOPE_P (t)) && 0)
13636 verify_variant_match (TYPE_CONTEXT);
13637 verify_variant_match (TYPE_STRING_FLAG);
13638 if (TYPE_ALIAS_SET_KNOWN_P (t))
13640 error ("type variant with TYPE_ALIAS_SET_KNOWN_P");
13641 debug_tree (tv);
13642 return false;
13645 /* tree_type_non_common checks. */
13647 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
13648 and dangle the pointer from time to time. */
13649 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_VFIELD (t) != TYPE_VFIELD (tv)
13650 && (in_lto_p || !TYPE_VFIELD (tv)
13651 || TREE_CODE (TYPE_VFIELD (tv)) != TREE_LIST))
13653 error ("type variant has different TYPE_VFIELD");
13654 debug_tree (tv);
13655 return false;
13657 if ((TREE_CODE (t) == ENUMERAL_TYPE && COMPLETE_TYPE_P (t))
13658 || TREE_CODE (t) == INTEGER_TYPE
13659 || TREE_CODE (t) == BOOLEAN_TYPE
13660 || TREE_CODE (t) == REAL_TYPE
13661 || TREE_CODE (t) == FIXED_POINT_TYPE)
13663 verify_variant_match (TYPE_MAX_VALUE);
13664 verify_variant_match (TYPE_MIN_VALUE);
13666 if (TREE_CODE (t) == METHOD_TYPE)
13667 verify_variant_match (TYPE_METHOD_BASETYPE);
13668 if (TREE_CODE (t) == OFFSET_TYPE)
13669 verify_variant_match (TYPE_OFFSET_BASETYPE);
13670 if (TREE_CODE (t) == ARRAY_TYPE)
13671 verify_variant_match (TYPE_ARRAY_MAX_SIZE);
13672 /* FIXME: Be lax and allow TYPE_BINFO to be missing in variant types
13673 or even type's main variant. This is needed to make bootstrap pass
13674 and the bug seems new in GCC 5.
13675 C++ FE should be updated to make this consistent and we should check
13676 that TYPE_BINFO is always NULL for !COMPLETE_TYPE_P and otherwise there
13677 is a match with main variant.
13679 Also disable the check for Java for now because of parser hack that builds
13680 first an dummy BINFO and then sometimes replace it by real BINFO in some
13681 of the copies. */
13682 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t) && TYPE_BINFO (tv)
13683 && TYPE_BINFO (t) != TYPE_BINFO (tv)
13684 /* FIXME: Java sometimes keep dump TYPE_BINFOs on variant types.
13685 Since there is no cheap way to tell C++/Java type w/o LTO, do checking
13686 at LTO time only. */
13687 && (in_lto_p && odr_type_p (t)))
13689 error ("type variant has different TYPE_BINFO");
13690 debug_tree (tv);
13691 error ("type variant's TYPE_BINFO");
13692 debug_tree (TYPE_BINFO (tv));
13693 error ("type's TYPE_BINFO");
13694 debug_tree (TYPE_BINFO (t));
13695 return false;
13698 /* Check various uses of TYPE_VALUES_RAW. */
13699 if (TREE_CODE (t) == ENUMERAL_TYPE
13700 && TYPE_VALUES (t))
13701 verify_variant_match (TYPE_VALUES);
13702 else if (TREE_CODE (t) == ARRAY_TYPE)
13703 verify_variant_match (TYPE_DOMAIN);
13704 /* Permit incomplete variants of complete type. While FEs may complete
13705 all variants, this does not happen for C++ templates in all cases. */
13706 else if (RECORD_OR_UNION_TYPE_P (t)
13707 && COMPLETE_TYPE_P (t)
13708 && TYPE_FIELDS (t) != TYPE_FIELDS (tv))
13710 tree f1, f2;
13712 /* Fortran builds qualified variants as new records with items of
13713 qualified type. Verify that they looks same. */
13714 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (tv);
13715 f1 && f2;
13716 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
13717 if (TREE_CODE (f1) != FIELD_DECL || TREE_CODE (f2) != FIELD_DECL
13718 || (TYPE_MAIN_VARIANT (TREE_TYPE (f1))
13719 != TYPE_MAIN_VARIANT (TREE_TYPE (f2))
13720 /* FIXME: gfc_nonrestricted_type builds all types as variants
13721 with exception of pointer types. It deeply copies the type
13722 which means that we may end up with a variant type
13723 referring non-variant pointer. We may change it to
13724 produce types as variants, too, like
13725 objc_get_protocol_qualified_type does. */
13726 && !POINTER_TYPE_P (TREE_TYPE (f1)))
13727 || DECL_FIELD_OFFSET (f1) != DECL_FIELD_OFFSET (f2)
13728 || DECL_FIELD_BIT_OFFSET (f1) != DECL_FIELD_BIT_OFFSET (f2))
13729 break;
13730 if (f1 || f2)
13732 error ("type variant has different TYPE_FIELDS");
13733 debug_tree (tv);
13734 error ("first mismatch is field");
13735 debug_tree (f1);
13736 error ("and field");
13737 debug_tree (f2);
13738 return false;
13741 else if ((TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE))
13742 verify_variant_match (TYPE_ARG_TYPES);
13743 /* For C++ the qualified variant of array type is really an array type
13744 of qualified TREE_TYPE.
13745 objc builds variants of pointer where pointer to type is a variant, too
13746 in objc_get_protocol_qualified_type. */
13747 if (TREE_TYPE (t) != TREE_TYPE (tv)
13748 && ((TREE_CODE (t) != ARRAY_TYPE
13749 && !POINTER_TYPE_P (t))
13750 || TYPE_MAIN_VARIANT (TREE_TYPE (t))
13751 != TYPE_MAIN_VARIANT (TREE_TYPE (tv))))
13753 error ("type variant has different TREE_TYPE");
13754 debug_tree (tv);
13755 error ("type variant's TREE_TYPE");
13756 debug_tree (TREE_TYPE (tv));
13757 error ("type's TREE_TYPE");
13758 debug_tree (TREE_TYPE (t));
13759 return false;
13761 if (type_with_alias_set_p (t)
13762 && !gimple_canonical_types_compatible_p (t, tv, false))
13764 error ("type is not compatible with its variant");
13765 debug_tree (tv);
13766 error ("type variant's TREE_TYPE");
13767 debug_tree (TREE_TYPE (tv));
13768 error ("type's TREE_TYPE");
13769 debug_tree (TREE_TYPE (t));
13770 return false;
13772 return true;
13773 #undef verify_variant_match
13777 /* The TYPE_CANONICAL merging machinery. It should closely resemble
13778 the middle-end types_compatible_p function. It needs to avoid
13779 claiming types are different for types that should be treated
13780 the same with respect to TBAA. Canonical types are also used
13781 for IL consistency checks via the useless_type_conversion_p
13782 predicate which does not handle all type kinds itself but falls
13783 back to pointer-comparison of TYPE_CANONICAL for aggregates
13784 for example. */
13786 /* Return true if TYPE_UNSIGNED of TYPE should be ignored for canonical
13787 type calculation because we need to allow inter-operability between signed
13788 and unsigned variants. */
13790 bool
13791 type_with_interoperable_signedness (const_tree type)
13793 /* Fortran standard require C_SIGNED_CHAR to be interoperable with both
13794 signed char and unsigned char. Similarly fortran FE builds
13795 C_SIZE_T as signed type, while C defines it unsigned. */
13797 return tree_code_for_canonical_type_merging (TREE_CODE (type))
13798 == INTEGER_TYPE
13799 && (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node)
13800 || TYPE_PRECISION (type) == TYPE_PRECISION (size_type_node));
13803 /* Return true iff T1 and T2 are structurally identical for what
13804 TBAA is concerned.
13805 This function is used both by lto.c canonical type merging and by the
13806 verifier. If TRUST_TYPE_CANONICAL we do not look into structure of types
13807 that have TYPE_CANONICAL defined and assume them equivalent. This is useful
13808 only for LTO because only in these cases TYPE_CANONICAL equivalence
13809 correspond to one defined by gimple_canonical_types_compatible_p. */
13811 bool
13812 gimple_canonical_types_compatible_p (const_tree t1, const_tree t2,
13813 bool trust_type_canonical)
13815 /* Type variants should be same as the main variant. When not doing sanity
13816 checking to verify this fact, go to main variants and save some work. */
13817 if (trust_type_canonical)
13819 t1 = TYPE_MAIN_VARIANT (t1);
13820 t2 = TYPE_MAIN_VARIANT (t2);
13823 /* Check first for the obvious case of pointer identity. */
13824 if (t1 == t2)
13825 return true;
13827 /* Check that we have two types to compare. */
13828 if (t1 == NULL_TREE || t2 == NULL_TREE)
13829 return false;
13831 /* We consider complete types always compatible with incomplete type.
13832 This does not make sense for canonical type calculation and thus we
13833 need to ensure that we are never called on it.
13835 FIXME: For more correctness the function probably should have three modes
13836 1) mode assuming that types are complete mathcing their structure
13837 2) mode allowing incomplete types but producing equivalence classes
13838 and thus ignoring all info from complete types
13839 3) mode allowing incomplete types to match complete but checking
13840 compatibility between complete types.
13842 1 and 2 can be used for canonical type calculation. 3 is the real
13843 definition of type compatibility that can be used i.e. for warnings during
13844 declaration merging. */
13846 gcc_assert (!trust_type_canonical
13847 || (type_with_alias_set_p (t1) && type_with_alias_set_p (t2)));
13848 /* If the types have been previously registered and found equal
13849 they still are. */
13851 if (TYPE_CANONICAL (t1) && TYPE_CANONICAL (t2)
13852 && trust_type_canonical)
13854 /* Do not use TYPE_CANONICAL of pointer types. For LTO streamed types
13855 they are always NULL, but they are set to non-NULL for types
13856 constructed by build_pointer_type and variants. In this case the
13857 TYPE_CANONICAL is more fine grained than the equivalnce we test (where
13858 all pointers are considered equal. Be sure to not return false
13859 negatives. */
13860 gcc_checking_assert (canonical_type_used_p (t1)
13861 && canonical_type_used_p (t2));
13862 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
13865 /* Can't be the same type if the types don't have the same code. */
13866 enum tree_code code = tree_code_for_canonical_type_merging (TREE_CODE (t1));
13867 if (code != tree_code_for_canonical_type_merging (TREE_CODE (t2)))
13868 return false;
13870 /* Qualifiers do not matter for canonical type comparison purposes. */
13872 /* Void types and nullptr types are always the same. */
13873 if (TREE_CODE (t1) == VOID_TYPE
13874 || TREE_CODE (t1) == NULLPTR_TYPE)
13875 return true;
13877 /* Can't be the same type if they have different mode. */
13878 if (TYPE_MODE (t1) != TYPE_MODE (t2))
13879 return false;
13881 /* Non-aggregate types can be handled cheaply. */
13882 if (INTEGRAL_TYPE_P (t1)
13883 || SCALAR_FLOAT_TYPE_P (t1)
13884 || FIXED_POINT_TYPE_P (t1)
13885 || TREE_CODE (t1) == VECTOR_TYPE
13886 || TREE_CODE (t1) == COMPLEX_TYPE
13887 || TREE_CODE (t1) == OFFSET_TYPE
13888 || POINTER_TYPE_P (t1))
13890 /* Can't be the same type if they have different recision. */
13891 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
13892 return false;
13894 /* In some cases the signed and unsigned types are required to be
13895 inter-operable. */
13896 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2)
13897 && !type_with_interoperable_signedness (t1))
13898 return false;
13900 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
13901 interoperable with "signed char". Unless all frontends are revisited
13902 to agree on these types, we must ignore the flag completely. */
13904 /* Fortran standard define C_PTR type that is compatible with every
13905 C pointer. For this reason we need to glob all pointers into one.
13906 Still pointers in different address spaces are not compatible. */
13907 if (POINTER_TYPE_P (t1))
13909 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
13910 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
13911 return false;
13914 /* Tail-recurse to components. */
13915 if (TREE_CODE (t1) == VECTOR_TYPE
13916 || TREE_CODE (t1) == COMPLEX_TYPE)
13917 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
13918 TREE_TYPE (t2),
13919 trust_type_canonical);
13921 return true;
13924 /* Do type-specific comparisons. */
13925 switch (TREE_CODE (t1))
13927 case ARRAY_TYPE:
13928 /* Array types are the same if the element types are the same and
13929 the number of elements are the same. */
13930 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13931 trust_type_canonical)
13932 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
13933 || TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2)
13934 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
13935 return false;
13936 else
13938 tree i1 = TYPE_DOMAIN (t1);
13939 tree i2 = TYPE_DOMAIN (t2);
13941 /* For an incomplete external array, the type domain can be
13942 NULL_TREE. Check this condition also. */
13943 if (i1 == NULL_TREE && i2 == NULL_TREE)
13944 return true;
13945 else if (i1 == NULL_TREE || i2 == NULL_TREE)
13946 return false;
13947 else
13949 tree min1 = TYPE_MIN_VALUE (i1);
13950 tree min2 = TYPE_MIN_VALUE (i2);
13951 tree max1 = TYPE_MAX_VALUE (i1);
13952 tree max2 = TYPE_MAX_VALUE (i2);
13954 /* The minimum/maximum values have to be the same. */
13955 if ((min1 == min2
13956 || (min1 && min2
13957 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
13958 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
13959 || operand_equal_p (min1, min2, 0))))
13960 && (max1 == max2
13961 || (max1 && max2
13962 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
13963 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
13964 || operand_equal_p (max1, max2, 0)))))
13965 return true;
13966 else
13967 return false;
13971 case METHOD_TYPE:
13972 case FUNCTION_TYPE:
13973 /* Function types are the same if the return type and arguments types
13974 are the same. */
13975 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13976 trust_type_canonical))
13977 return false;
13979 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
13980 return true;
13981 else
13983 tree parms1, parms2;
13985 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
13986 parms1 && parms2;
13987 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
13989 if (!gimple_canonical_types_compatible_p
13990 (TREE_VALUE (parms1), TREE_VALUE (parms2),
13991 trust_type_canonical))
13992 return false;
13995 if (parms1 || parms2)
13996 return false;
13998 return true;
14001 case RECORD_TYPE:
14002 case UNION_TYPE:
14003 case QUAL_UNION_TYPE:
14005 tree f1, f2;
14007 /* Don't try to compare variants of an incomplete type, before
14008 TYPE_FIELDS has been copied around. */
14009 if (!COMPLETE_TYPE_P (t1) && !COMPLETE_TYPE_P (t2))
14010 return true;
14013 if (TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2))
14014 return false;
14016 /* For aggregate types, all the fields must be the same. */
14017 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
14018 f1 || f2;
14019 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
14021 /* Skip non-fields and zero-sized fields. */
14022 while (f1 && (TREE_CODE (f1) != FIELD_DECL
14023 || (DECL_SIZE (f1)
14024 && integer_zerop (DECL_SIZE (f1)))))
14025 f1 = TREE_CHAIN (f1);
14026 while (f2 && (TREE_CODE (f2) != FIELD_DECL
14027 || (DECL_SIZE (f2)
14028 && integer_zerop (DECL_SIZE (f2)))))
14029 f2 = TREE_CHAIN (f2);
14030 if (!f1 || !f2)
14031 break;
14032 /* The fields must have the same name, offset and type. */
14033 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
14034 || !gimple_compare_field_offset (f1, f2)
14035 || !gimple_canonical_types_compatible_p
14036 (TREE_TYPE (f1), TREE_TYPE (f2),
14037 trust_type_canonical))
14038 return false;
14041 /* If one aggregate has more fields than the other, they
14042 are not the same. */
14043 if (f1 || f2)
14044 return false;
14046 return true;
14049 default:
14050 /* Consider all types with language specific trees in them mutually
14051 compatible. This is executed only from verify_type and false
14052 positives can be tolerated. */
14053 gcc_assert (!in_lto_p);
14054 return true;
14058 /* Verify type T. */
14060 void
14061 verify_type (const_tree t)
14063 bool error_found = false;
14064 tree mv = TYPE_MAIN_VARIANT (t);
14065 if (!mv)
14067 error ("Main variant is not defined");
14068 error_found = true;
14070 else if (mv != TYPE_MAIN_VARIANT (mv))
14072 error ("TYPE_MAIN_VARIANT has different TYPE_MAIN_VARIANT");
14073 debug_tree (mv);
14074 error_found = true;
14076 else if (t != mv && !verify_type_variant (t, mv))
14077 error_found = true;
14079 tree ct = TYPE_CANONICAL (t);
14080 if (!ct)
14082 else if (TYPE_CANONICAL (t) != ct)
14084 error ("TYPE_CANONICAL has different TYPE_CANONICAL");
14085 debug_tree (ct);
14086 error_found = true;
14088 /* Method and function types can not be used to address memory and thus
14089 TYPE_CANONICAL really matters only for determining useless conversions.
14091 FIXME: C++ FE produce declarations of builtin functions that are not
14092 compatible with main variants. */
14093 else if (TREE_CODE (t) == FUNCTION_TYPE)
14095 else if (t != ct
14096 /* FIXME: gimple_canonical_types_compatible_p can not compare types
14097 with variably sized arrays because their sizes possibly
14098 gimplified to different variables. */
14099 && !variably_modified_type_p (ct, NULL)
14100 && !gimple_canonical_types_compatible_p (t, ct, false)
14101 && COMPLETE_TYPE_P (t))
14103 error ("TYPE_CANONICAL is not compatible");
14104 debug_tree (ct);
14105 error_found = true;
14108 if (COMPLETE_TYPE_P (t) && TYPE_CANONICAL (t)
14109 && TYPE_MODE (t) != TYPE_MODE (TYPE_CANONICAL (t)))
14111 error ("TYPE_MODE of TYPE_CANONICAL is not compatible");
14112 debug_tree (ct);
14113 error_found = true;
14115 if (TYPE_MAIN_VARIANT (t) == t && ct && TYPE_MAIN_VARIANT (ct) != ct)
14117 error ("TYPE_CANONICAL of main variant is not main variant");
14118 debug_tree (ct);
14119 debug_tree (TYPE_MAIN_VARIANT (ct));
14120 error_found = true;
14124 /* Check various uses of TYPE_MIN_VALUE_RAW. */
14125 if (RECORD_OR_UNION_TYPE_P (t))
14127 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
14128 and danagle the pointer from time to time. */
14129 if (TYPE_VFIELD (t)
14130 && TREE_CODE (TYPE_VFIELD (t)) != FIELD_DECL
14131 && TREE_CODE (TYPE_VFIELD (t)) != TREE_LIST)
14133 error ("TYPE_VFIELD is not FIELD_DECL nor TREE_LIST");
14134 debug_tree (TYPE_VFIELD (t));
14135 error_found = true;
14138 else if (TREE_CODE (t) == POINTER_TYPE)
14140 if (TYPE_NEXT_PTR_TO (t)
14141 && TREE_CODE (TYPE_NEXT_PTR_TO (t)) != POINTER_TYPE)
14143 error ("TYPE_NEXT_PTR_TO is not POINTER_TYPE");
14144 debug_tree (TYPE_NEXT_PTR_TO (t));
14145 error_found = true;
14148 else if (TREE_CODE (t) == REFERENCE_TYPE)
14150 if (TYPE_NEXT_REF_TO (t)
14151 && TREE_CODE (TYPE_NEXT_REF_TO (t)) != REFERENCE_TYPE)
14153 error ("TYPE_NEXT_REF_TO is not REFERENCE_TYPE");
14154 debug_tree (TYPE_NEXT_REF_TO (t));
14155 error_found = true;
14158 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
14159 || TREE_CODE (t) == FIXED_POINT_TYPE)
14161 /* FIXME: The following check should pass:
14162 useless_type_conversion_p (const_cast <tree> (t),
14163 TREE_TYPE (TYPE_MIN_VALUE (t))
14164 but does not for C sizetypes in LTO. */
14167 /* Check various uses of TYPE_MAXVAL_RAW. */
14168 if (RECORD_OR_UNION_TYPE_P (t))
14170 if (!TYPE_BINFO (t))
14172 else if (TREE_CODE (TYPE_BINFO (t)) != TREE_BINFO)
14174 error ("TYPE_BINFO is not TREE_BINFO");
14175 debug_tree (TYPE_BINFO (t));
14176 error_found = true;
14178 else if (TREE_TYPE (TYPE_BINFO (t)) != TYPE_MAIN_VARIANT (t))
14180 error ("TYPE_BINFO type is not TYPE_MAIN_VARIANT");
14181 debug_tree (TREE_TYPE (TYPE_BINFO (t)));
14182 error_found = true;
14185 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
14187 if (TYPE_METHOD_BASETYPE (t)
14188 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != RECORD_TYPE
14189 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != UNION_TYPE)
14191 error ("TYPE_METHOD_BASETYPE is not record nor union");
14192 debug_tree (TYPE_METHOD_BASETYPE (t));
14193 error_found = true;
14196 else if (TREE_CODE (t) == OFFSET_TYPE)
14198 if (TYPE_OFFSET_BASETYPE (t)
14199 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != RECORD_TYPE
14200 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != UNION_TYPE)
14202 error ("TYPE_OFFSET_BASETYPE is not record nor union");
14203 debug_tree (TYPE_OFFSET_BASETYPE (t));
14204 error_found = true;
14207 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
14208 || TREE_CODE (t) == FIXED_POINT_TYPE)
14210 /* FIXME: The following check should pass:
14211 useless_type_conversion_p (const_cast <tree> (t),
14212 TREE_TYPE (TYPE_MAX_VALUE (t))
14213 but does not for C sizetypes in LTO. */
14215 else if (TREE_CODE (t) == ARRAY_TYPE)
14217 if (TYPE_ARRAY_MAX_SIZE (t)
14218 && TREE_CODE (TYPE_ARRAY_MAX_SIZE (t)) != INTEGER_CST)
14220 error ("TYPE_ARRAY_MAX_SIZE not INTEGER_CST");
14221 debug_tree (TYPE_ARRAY_MAX_SIZE (t));
14222 error_found = true;
14225 else if (TYPE_MAX_VALUE_RAW (t))
14227 error ("TYPE_MAX_VALUE_RAW non-NULL");
14228 debug_tree (TYPE_MAX_VALUE_RAW (t));
14229 error_found = true;
14232 if (TYPE_LANG_SLOT_1 (t) && in_lto_p)
14234 error ("TYPE_LANG_SLOT_1 (binfo) field is non-NULL");
14235 debug_tree (TYPE_LANG_SLOT_1 (t));
14236 error_found = true;
14239 /* Check various uses of TYPE_VALUES_RAW. */
14240 if (TREE_CODE (t) == ENUMERAL_TYPE)
14241 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
14243 tree value = TREE_VALUE (l);
14244 tree name = TREE_PURPOSE (l);
14246 /* C FE porduce INTEGER_CST of INTEGER_TYPE, while C++ FE uses
14247 CONST_DECL of ENUMERAL TYPE. */
14248 if (TREE_CODE (value) != INTEGER_CST && TREE_CODE (value) != CONST_DECL)
14250 error ("Enum value is not CONST_DECL or INTEGER_CST");
14251 debug_tree (value);
14252 debug_tree (name);
14253 error_found = true;
14255 if (TREE_CODE (TREE_TYPE (value)) != INTEGER_TYPE
14256 && !useless_type_conversion_p (const_cast <tree> (t), TREE_TYPE (value)))
14258 error ("Enum value type is not INTEGER_TYPE nor convertible to the enum");
14259 debug_tree (value);
14260 debug_tree (name);
14261 error_found = true;
14263 if (TREE_CODE (name) != IDENTIFIER_NODE)
14265 error ("Enum value name is not IDENTIFIER_NODE");
14266 debug_tree (value);
14267 debug_tree (name);
14268 error_found = true;
14271 else if (TREE_CODE (t) == ARRAY_TYPE)
14273 if (TYPE_DOMAIN (t) && TREE_CODE (TYPE_DOMAIN (t)) != INTEGER_TYPE)
14275 error ("Array TYPE_DOMAIN is not integer type");
14276 debug_tree (TYPE_DOMAIN (t));
14277 error_found = true;
14280 else if (RECORD_OR_UNION_TYPE_P (t))
14282 if (TYPE_FIELDS (t) && !COMPLETE_TYPE_P (t) && in_lto_p)
14284 error ("TYPE_FIELDS defined in incomplete type");
14285 error_found = true;
14287 for (tree fld = TYPE_FIELDS (t); fld; fld = TREE_CHAIN (fld))
14289 /* TODO: verify properties of decls. */
14290 if (TREE_CODE (fld) == FIELD_DECL)
14292 else if (TREE_CODE (fld) == TYPE_DECL)
14294 else if (TREE_CODE (fld) == CONST_DECL)
14296 else if (VAR_P (fld))
14298 else if (TREE_CODE (fld) == TEMPLATE_DECL)
14300 else if (TREE_CODE (fld) == USING_DECL)
14302 else if (TREE_CODE (fld) == FUNCTION_DECL)
14304 else
14306 error ("Wrong tree in TYPE_FIELDS list");
14307 debug_tree (fld);
14308 error_found = true;
14312 else if (TREE_CODE (t) == INTEGER_TYPE
14313 || TREE_CODE (t) == BOOLEAN_TYPE
14314 || TREE_CODE (t) == OFFSET_TYPE
14315 || TREE_CODE (t) == REFERENCE_TYPE
14316 || TREE_CODE (t) == NULLPTR_TYPE
14317 || TREE_CODE (t) == POINTER_TYPE)
14319 if (TYPE_CACHED_VALUES_P (t) != (TYPE_CACHED_VALUES (t) != NULL))
14321 error ("TYPE_CACHED_VALUES_P is %i while TYPE_CACHED_VALUES is %p",
14322 TYPE_CACHED_VALUES_P (t), (void *)TYPE_CACHED_VALUES (t));
14323 error_found = true;
14325 else if (TYPE_CACHED_VALUES_P (t) && TREE_CODE (TYPE_CACHED_VALUES (t)) != TREE_VEC)
14327 error ("TYPE_CACHED_VALUES is not TREE_VEC");
14328 debug_tree (TYPE_CACHED_VALUES (t));
14329 error_found = true;
14331 /* Verify just enough of cache to ensure that no one copied it to new type.
14332 All copying should go by copy_node that should clear it. */
14333 else if (TYPE_CACHED_VALUES_P (t))
14335 int i;
14336 for (i = 0; i < TREE_VEC_LENGTH (TYPE_CACHED_VALUES (t)); i++)
14337 if (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)
14338 && TREE_TYPE (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)) != t)
14340 error ("wrong TYPE_CACHED_VALUES entry");
14341 debug_tree (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i));
14342 error_found = true;
14343 break;
14347 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
14348 for (tree l = TYPE_ARG_TYPES (t); l; l = TREE_CHAIN (l))
14350 /* C++ FE uses TREE_PURPOSE to store initial values. */
14351 if (TREE_PURPOSE (l) && in_lto_p)
14353 error ("TREE_PURPOSE is non-NULL in TYPE_ARG_TYPES list");
14354 debug_tree (l);
14355 error_found = true;
14357 if (!TYPE_P (TREE_VALUE (l)))
14359 error ("Wrong entry in TYPE_ARG_TYPES list");
14360 debug_tree (l);
14361 error_found = true;
14364 else if (!is_lang_specific (t) && TYPE_VALUES_RAW (t))
14366 error ("TYPE_VALUES_RAW field is non-NULL");
14367 debug_tree (TYPE_VALUES_RAW (t));
14368 error_found = true;
14370 if (TREE_CODE (t) != INTEGER_TYPE
14371 && TREE_CODE (t) != BOOLEAN_TYPE
14372 && TREE_CODE (t) != OFFSET_TYPE
14373 && TREE_CODE (t) != REFERENCE_TYPE
14374 && TREE_CODE (t) != NULLPTR_TYPE
14375 && TREE_CODE (t) != POINTER_TYPE
14376 && TYPE_CACHED_VALUES_P (t))
14378 error ("TYPE_CACHED_VALUES_P is set while it should not");
14379 error_found = true;
14381 if (TYPE_STRING_FLAG (t)
14382 && TREE_CODE (t) != ARRAY_TYPE && TREE_CODE (t) != INTEGER_TYPE)
14384 error ("TYPE_STRING_FLAG is set on wrong type code");
14385 error_found = true;
14388 /* ipa-devirt makes an assumption that TYPE_METHOD_BASETYPE is always
14389 TYPE_MAIN_VARIANT and it would be odd to add methods only to variatns
14390 of a type. */
14391 if (TREE_CODE (t) == METHOD_TYPE
14392 && TYPE_MAIN_VARIANT (TYPE_METHOD_BASETYPE (t)) != TYPE_METHOD_BASETYPE (t))
14394 error ("TYPE_METHOD_BASETYPE is not main variant");
14395 error_found = true;
14398 if (error_found)
14400 debug_tree (const_cast <tree> (t));
14401 internal_error ("verify_type failed");
14406 /* Return 1 if ARG interpreted as signed in its precision is known to be
14407 always positive or 2 if ARG is known to be always negative, or 3 if
14408 ARG may be positive or negative. */
14411 get_range_pos_neg (tree arg)
14413 if (arg == error_mark_node)
14414 return 3;
14416 int prec = TYPE_PRECISION (TREE_TYPE (arg));
14417 int cnt = 0;
14418 if (TREE_CODE (arg) == INTEGER_CST)
14420 wide_int w = wi::sext (wi::to_wide (arg), prec);
14421 if (wi::neg_p (w))
14422 return 2;
14423 else
14424 return 1;
14426 while (CONVERT_EXPR_P (arg)
14427 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
14428 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg, 0))) <= prec)
14430 arg = TREE_OPERAND (arg, 0);
14431 /* Narrower value zero extended into wider type
14432 will always result in positive values. */
14433 if (TYPE_UNSIGNED (TREE_TYPE (arg))
14434 && TYPE_PRECISION (TREE_TYPE (arg)) < prec)
14435 return 1;
14436 prec = TYPE_PRECISION (TREE_TYPE (arg));
14437 if (++cnt > 30)
14438 return 3;
14441 if (TREE_CODE (arg) != SSA_NAME)
14442 return 3;
14443 wide_int arg_min, arg_max;
14444 while (get_range_info (arg, &arg_min, &arg_max) != VR_RANGE)
14446 gimple *g = SSA_NAME_DEF_STMT (arg);
14447 if (is_gimple_assign (g)
14448 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (g)))
14450 tree t = gimple_assign_rhs1 (g);
14451 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
14452 && TYPE_PRECISION (TREE_TYPE (t)) <= prec)
14454 if (TYPE_UNSIGNED (TREE_TYPE (t))
14455 && TYPE_PRECISION (TREE_TYPE (t)) < prec)
14456 return 1;
14457 prec = TYPE_PRECISION (TREE_TYPE (t));
14458 arg = t;
14459 if (++cnt > 30)
14460 return 3;
14461 continue;
14464 return 3;
14466 if (TYPE_UNSIGNED (TREE_TYPE (arg)))
14468 /* For unsigned values, the "positive" range comes
14469 below the "negative" range. */
14470 if (!wi::neg_p (wi::sext (arg_max, prec), SIGNED))
14471 return 1;
14472 if (wi::neg_p (wi::sext (arg_min, prec), SIGNED))
14473 return 2;
14475 else
14477 if (!wi::neg_p (wi::sext (arg_min, prec), SIGNED))
14478 return 1;
14479 if (wi::neg_p (wi::sext (arg_max, prec), SIGNED))
14480 return 2;
14482 return 3;
14488 /* Return true if ARG is marked with the nonnull attribute in the
14489 current function signature. */
14491 bool
14492 nonnull_arg_p (const_tree arg)
14494 tree t, attrs, fntype;
14495 unsigned HOST_WIDE_INT arg_num;
14497 gcc_assert (TREE_CODE (arg) == PARM_DECL
14498 && (POINTER_TYPE_P (TREE_TYPE (arg))
14499 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE));
14501 /* The static chain decl is always non null. */
14502 if (arg == cfun->static_chain_decl)
14503 return true;
14505 /* THIS argument of method is always non-NULL. */
14506 if (TREE_CODE (TREE_TYPE (cfun->decl)) == METHOD_TYPE
14507 && arg == DECL_ARGUMENTS (cfun->decl)
14508 && flag_delete_null_pointer_checks)
14509 return true;
14511 /* Values passed by reference are always non-NULL. */
14512 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
14513 && flag_delete_null_pointer_checks)
14514 return true;
14516 fntype = TREE_TYPE (cfun->decl);
14517 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
14519 attrs = lookup_attribute ("nonnull", attrs);
14521 /* If "nonnull" wasn't specified, we know nothing about the argument. */
14522 if (attrs == NULL_TREE)
14523 return false;
14525 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
14526 if (TREE_VALUE (attrs) == NULL_TREE)
14527 return true;
14529 /* Get the position number for ARG in the function signature. */
14530 for (arg_num = 1, t = DECL_ARGUMENTS (cfun->decl);
14532 t = DECL_CHAIN (t), arg_num++)
14534 if (t == arg)
14535 break;
14538 gcc_assert (t == arg);
14540 /* Now see if ARG_NUM is mentioned in the nonnull list. */
14541 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
14543 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
14544 return true;
14548 return false;
14551 /* Combine LOC and BLOCK to a combined adhoc loc, retaining any range
14552 information. */
14554 location_t
14555 set_block (location_t loc, tree block)
14557 location_t pure_loc = get_pure_location (loc);
14558 source_range src_range = get_range_from_loc (line_table, loc);
14559 return COMBINE_LOCATION_DATA (line_table, pure_loc, src_range, block);
14562 location_t
14563 set_source_range (tree expr, location_t start, location_t finish)
14565 source_range src_range;
14566 src_range.m_start = start;
14567 src_range.m_finish = finish;
14568 return set_source_range (expr, src_range);
14571 location_t
14572 set_source_range (tree expr, source_range src_range)
14574 if (!EXPR_P (expr))
14575 return UNKNOWN_LOCATION;
14577 location_t pure_loc = get_pure_location (EXPR_LOCATION (expr));
14578 location_t adhoc = COMBINE_LOCATION_DATA (line_table,
14579 pure_loc,
14580 src_range,
14581 NULL);
14582 SET_EXPR_LOCATION (expr, adhoc);
14583 return adhoc;
14586 /* Return EXPR, potentially wrapped with a node expression LOC,
14587 if !CAN_HAVE_LOCATION_P (expr).
14589 NON_LVALUE_EXPR is used for wrapping constants, apart from STRING_CST.
14590 VIEW_CONVERT_EXPR is used for wrapping non-constants and STRING_CST.
14592 Wrapper nodes can be identified using location_wrapper_p. */
14594 tree
14595 maybe_wrap_with_location (tree expr, location_t loc)
14597 if (expr == NULL)
14598 return NULL;
14599 if (loc == UNKNOWN_LOCATION)
14600 return expr;
14601 if (CAN_HAVE_LOCATION_P (expr))
14602 return expr;
14603 /* We should only be adding wrappers for constants and for decls,
14604 or for some exceptional tree nodes (e.g. BASELINK in the C++ FE). */
14605 gcc_assert (CONSTANT_CLASS_P (expr)
14606 || DECL_P (expr)
14607 || EXCEPTIONAL_CLASS_P (expr));
14609 /* For now, don't add wrappers to exceptional tree nodes, to minimize
14610 any impact of the wrapper nodes. */
14611 if (EXCEPTIONAL_CLASS_P (expr))
14612 return expr;
14614 tree_code code
14615 = (((CONSTANT_CLASS_P (expr) && TREE_CODE (expr) != STRING_CST)
14616 || (TREE_CODE (expr) == CONST_DECL && !TREE_STATIC (expr)))
14617 ? NON_LVALUE_EXPR : VIEW_CONVERT_EXPR);
14618 tree wrapper = build1_loc (loc, code, TREE_TYPE (expr), expr);
14619 /* Mark this node as being a wrapper. */
14620 EXPR_LOCATION_WRAPPER_P (wrapper) = 1;
14621 return wrapper;
14624 /* Return the name of combined function FN, for debugging purposes. */
14626 const char *
14627 combined_fn_name (combined_fn fn)
14629 if (builtin_fn_p (fn))
14631 tree fndecl = builtin_decl_explicit (as_builtin_fn (fn));
14632 return IDENTIFIER_POINTER (DECL_NAME (fndecl));
14634 else
14635 return internal_fn_name (as_internal_fn (fn));
14638 /* Return a bitmap with a bit set corresponding to each argument in
14639 a function call type FNTYPE declared with attribute nonnull,
14640 or null if none of the function's argument are nonnull. The caller
14641 must free the bitmap. */
14643 bitmap
14644 get_nonnull_args (const_tree fntype)
14646 if (fntype == NULL_TREE)
14647 return NULL;
14649 tree attrs = TYPE_ATTRIBUTES (fntype);
14650 if (!attrs)
14651 return NULL;
14653 bitmap argmap = NULL;
14655 /* A function declaration can specify multiple attribute nonnull,
14656 each with zero or more arguments. The loop below creates a bitmap
14657 representing a union of all the arguments. An empty (but non-null)
14658 bitmap means that all arguments have been declaraed nonnull. */
14659 for ( ; attrs; attrs = TREE_CHAIN (attrs))
14661 attrs = lookup_attribute ("nonnull", attrs);
14662 if (!attrs)
14663 break;
14665 if (!argmap)
14666 argmap = BITMAP_ALLOC (NULL);
14668 if (!TREE_VALUE (attrs))
14670 /* Clear the bitmap in case a previous attribute nonnull
14671 set it and this one overrides it for all arguments. */
14672 bitmap_clear (argmap);
14673 return argmap;
14676 /* Iterate over the indices of the format arguments declared nonnull
14677 and set a bit for each. */
14678 for (tree idx = TREE_VALUE (attrs); idx; idx = TREE_CHAIN (idx))
14680 unsigned int val = TREE_INT_CST_LOW (TREE_VALUE (idx)) - 1;
14681 bitmap_set_bit (argmap, val);
14685 return argmap;
14688 /* Returns true if TYPE is a type where it and all of its subobjects
14689 (recursively) are of structure, union, or array type. */
14691 static bool
14692 default_is_empty_type (tree type)
14694 if (RECORD_OR_UNION_TYPE_P (type))
14696 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
14697 if (TREE_CODE (field) == FIELD_DECL
14698 && !DECL_PADDING_P (field)
14699 && !default_is_empty_type (TREE_TYPE (field)))
14700 return false;
14701 return true;
14703 else if (TREE_CODE (type) == ARRAY_TYPE)
14704 return (integer_minus_onep (array_type_nelts (type))
14705 || TYPE_DOMAIN (type) == NULL_TREE
14706 || default_is_empty_type (TREE_TYPE (type)));
14707 return false;
14710 /* Implement TARGET_EMPTY_RECORD_P. Return true if TYPE is an empty type
14711 that shouldn't be passed via stack. */
14713 bool
14714 default_is_empty_record (const_tree type)
14716 if (!abi_version_at_least (12))
14717 return false;
14719 if (type == error_mark_node)
14720 return false;
14722 if (TREE_ADDRESSABLE (type))
14723 return false;
14725 return default_is_empty_type (TYPE_MAIN_VARIANT (type));
14728 /* Like int_size_in_bytes, but handle empty records specially. */
14730 HOST_WIDE_INT
14731 arg_int_size_in_bytes (const_tree type)
14733 return TYPE_EMPTY_P (type) ? 0 : int_size_in_bytes (type);
14736 /* Like size_in_bytes, but handle empty records specially. */
14738 tree
14739 arg_size_in_bytes (const_tree type)
14741 return TYPE_EMPTY_P (type) ? size_zero_node : size_in_bytes (type);
14744 /* Return true if an expression with CODE has to have the same result type as
14745 its first operand. */
14747 bool
14748 expr_type_first_operand_type_p (tree_code code)
14750 switch (code)
14752 case NEGATE_EXPR:
14753 case ABS_EXPR:
14754 case BIT_NOT_EXPR:
14755 case PAREN_EXPR:
14756 case CONJ_EXPR:
14758 case PLUS_EXPR:
14759 case MINUS_EXPR:
14760 case MULT_EXPR:
14761 case TRUNC_DIV_EXPR:
14762 case CEIL_DIV_EXPR:
14763 case FLOOR_DIV_EXPR:
14764 case ROUND_DIV_EXPR:
14765 case TRUNC_MOD_EXPR:
14766 case CEIL_MOD_EXPR:
14767 case FLOOR_MOD_EXPR:
14768 case ROUND_MOD_EXPR:
14769 case RDIV_EXPR:
14770 case EXACT_DIV_EXPR:
14771 case MIN_EXPR:
14772 case MAX_EXPR:
14773 case BIT_IOR_EXPR:
14774 case BIT_XOR_EXPR:
14775 case BIT_AND_EXPR:
14777 case LSHIFT_EXPR:
14778 case RSHIFT_EXPR:
14779 case LROTATE_EXPR:
14780 case RROTATE_EXPR:
14781 return true;
14783 default:
14784 return false;
14788 /* Return a typenode for the "standard" C type with a given name. */
14789 tree
14790 get_typenode_from_name (const char *name)
14792 if (name == NULL || *name == '\0')
14793 return NULL_TREE;
14795 if (strcmp (name, "char") == 0)
14796 return char_type_node;
14797 if (strcmp (name, "unsigned char") == 0)
14798 return unsigned_char_type_node;
14799 if (strcmp (name, "signed char") == 0)
14800 return signed_char_type_node;
14802 if (strcmp (name, "short int") == 0)
14803 return short_integer_type_node;
14804 if (strcmp (name, "short unsigned int") == 0)
14805 return short_unsigned_type_node;
14807 if (strcmp (name, "int") == 0)
14808 return integer_type_node;
14809 if (strcmp (name, "unsigned int") == 0)
14810 return unsigned_type_node;
14812 if (strcmp (name, "long int") == 0)
14813 return long_integer_type_node;
14814 if (strcmp (name, "long unsigned int") == 0)
14815 return long_unsigned_type_node;
14817 if (strcmp (name, "long long int") == 0)
14818 return long_long_integer_type_node;
14819 if (strcmp (name, "long long unsigned int") == 0)
14820 return long_long_unsigned_type_node;
14822 gcc_unreachable ();
14825 /* List of pointer types used to declare builtins before we have seen their
14826 real declaration.
14828 Keep the size up to date in tree.h ! */
14829 const builtin_structptr_type builtin_structptr_types[6] =
14831 { fileptr_type_node, ptr_type_node, "FILE" },
14832 { const_tm_ptr_type_node, const_ptr_type_node, "tm" },
14833 { fenv_t_ptr_type_node, ptr_type_node, "fenv_t" },
14834 { const_fenv_t_ptr_type_node, const_ptr_type_node, "fenv_t" },
14835 { fexcept_t_ptr_type_node, ptr_type_node, "fexcept_t" },
14836 { const_fexcept_t_ptr_type_node, const_ptr_type_node, "fexcept_t" }
14839 #if CHECKING_P
14841 namespace selftest {
14843 /* Selftests for tree. */
14845 /* Verify that integer constants are sane. */
14847 static void
14848 test_integer_constants ()
14850 ASSERT_TRUE (integer_type_node != NULL);
14851 ASSERT_TRUE (build_int_cst (integer_type_node, 0) != NULL);
14853 tree type = integer_type_node;
14855 tree zero = build_zero_cst (type);
14856 ASSERT_EQ (INTEGER_CST, TREE_CODE (zero));
14857 ASSERT_EQ (type, TREE_TYPE (zero));
14859 tree one = build_int_cst (type, 1);
14860 ASSERT_EQ (INTEGER_CST, TREE_CODE (one));
14861 ASSERT_EQ (type, TREE_TYPE (zero));
14864 /* Verify identifiers. */
14866 static void
14867 test_identifiers ()
14869 tree identifier = get_identifier ("foo");
14870 ASSERT_EQ (3, IDENTIFIER_LENGTH (identifier));
14871 ASSERT_STREQ ("foo", IDENTIFIER_POINTER (identifier));
14874 /* Verify LABEL_DECL. */
14876 static void
14877 test_labels ()
14879 tree identifier = get_identifier ("err");
14880 tree label_decl = build_decl (UNKNOWN_LOCATION, LABEL_DECL,
14881 identifier, void_type_node);
14882 ASSERT_EQ (-1, LABEL_DECL_UID (label_decl));
14883 ASSERT_FALSE (FORCED_LABEL (label_decl));
14886 /* Return a new VECTOR_CST node whose type is TYPE and whose values
14887 are given by VALS. */
14889 static tree
14890 build_vector (tree type, vec<tree> vals MEM_STAT_DECL)
14892 gcc_assert (known_eq (vals.length (), TYPE_VECTOR_SUBPARTS (type)));
14893 tree_vector_builder builder (type, vals.length (), 1);
14894 builder.splice (vals);
14895 return builder.build ();
14898 /* Check that VECTOR_CST ACTUAL contains the elements in EXPECTED. */
14900 static void
14901 check_vector_cst (vec<tree> expected, tree actual)
14903 ASSERT_KNOWN_EQ (expected.length (),
14904 TYPE_VECTOR_SUBPARTS (TREE_TYPE (actual)));
14905 for (unsigned int i = 0; i < expected.length (); ++i)
14906 ASSERT_EQ (wi::to_wide (expected[i]),
14907 wi::to_wide (vector_cst_elt (actual, i)));
14910 /* Check that VECTOR_CST ACTUAL contains NPATTERNS duplicated elements,
14911 and that its elements match EXPECTED. */
14913 static void
14914 check_vector_cst_duplicate (vec<tree> expected, tree actual,
14915 unsigned int npatterns)
14917 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
14918 ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (actual));
14919 ASSERT_EQ (npatterns, vector_cst_encoded_nelts (actual));
14920 ASSERT_TRUE (VECTOR_CST_DUPLICATE_P (actual));
14921 ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
14922 check_vector_cst (expected, actual);
14925 /* Check that VECTOR_CST ACTUAL contains NPATTERNS foreground elements
14926 and NPATTERNS background elements, and that its elements match
14927 EXPECTED. */
14929 static void
14930 check_vector_cst_fill (vec<tree> expected, tree actual,
14931 unsigned int npatterns)
14933 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
14934 ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (actual));
14935 ASSERT_EQ (2 * npatterns, vector_cst_encoded_nelts (actual));
14936 ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
14937 ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
14938 check_vector_cst (expected, actual);
14941 /* Check that VECTOR_CST ACTUAL contains NPATTERNS stepped patterns,
14942 and that its elements match EXPECTED. */
14944 static void
14945 check_vector_cst_stepped (vec<tree> expected, tree actual,
14946 unsigned int npatterns)
14948 ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
14949 ASSERT_EQ (3, VECTOR_CST_NELTS_PER_PATTERN (actual));
14950 ASSERT_EQ (3 * npatterns, vector_cst_encoded_nelts (actual));
14951 ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
14952 ASSERT_TRUE (VECTOR_CST_STEPPED_P (actual));
14953 check_vector_cst (expected, actual);
14956 /* Test the creation of VECTOR_CSTs. */
14958 static void
14959 test_vector_cst_patterns (ALONE_CXX_MEM_STAT_INFO)
14961 auto_vec<tree, 8> elements (8);
14962 elements.quick_grow (8);
14963 tree element_type = build_nonstandard_integer_type (16, true);
14964 tree vector_type = build_vector_type (element_type, 8);
14966 /* Test a simple linear series with a base of 0 and a step of 1:
14967 { 0, 1, 2, 3, 4, 5, 6, 7 }. */
14968 for (unsigned int i = 0; i < 8; ++i)
14969 elements[i] = build_int_cst (element_type, i);
14970 tree vector = build_vector (vector_type, elements PASS_MEM_STAT);
14971 check_vector_cst_stepped (elements, vector, 1);
14973 /* Try the same with the first element replaced by 100:
14974 { 100, 1, 2, 3, 4, 5, 6, 7 }. */
14975 elements[0] = build_int_cst (element_type, 100);
14976 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14977 check_vector_cst_stepped (elements, vector, 1);
14979 /* Try a series that wraps around.
14980 { 100, 65531, 65532, 65533, 65534, 65535, 0, 1 }. */
14981 for (unsigned int i = 1; i < 8; ++i)
14982 elements[i] = build_int_cst (element_type, (65530 + i) & 0xffff);
14983 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14984 check_vector_cst_stepped (elements, vector, 1);
14986 /* Try a downward series:
14987 { 100, 79, 78, 77, 76, 75, 75, 73 }. */
14988 for (unsigned int i = 1; i < 8; ++i)
14989 elements[i] = build_int_cst (element_type, 80 - i);
14990 vector = build_vector (vector_type, elements PASS_MEM_STAT);
14991 check_vector_cst_stepped (elements, vector, 1);
14993 /* Try two interleaved series with different bases and steps:
14994 { 100, 53, 66, 206, 62, 212, 58, 218 }. */
14995 elements[1] = build_int_cst (element_type, 53);
14996 for (unsigned int i = 2; i < 8; i += 2)
14998 elements[i] = build_int_cst (element_type, 70 - i * 2);
14999 elements[i + 1] = build_int_cst (element_type, 200 + i * 3);
15001 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15002 check_vector_cst_stepped (elements, vector, 2);
15004 /* Try a duplicated value:
15005 { 100, 100, 100, 100, 100, 100, 100, 100 }. */
15006 for (unsigned int i = 1; i < 8; ++i)
15007 elements[i] = elements[0];
15008 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15009 check_vector_cst_duplicate (elements, vector, 1);
15011 /* Try an interleaved duplicated value:
15012 { 100, 55, 100, 55, 100, 55, 100, 55 }. */
15013 elements[1] = build_int_cst (element_type, 55);
15014 for (unsigned int i = 2; i < 8; ++i)
15015 elements[i] = elements[i - 2];
15016 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15017 check_vector_cst_duplicate (elements, vector, 2);
15019 /* Try a duplicated value with 2 exceptions
15020 { 41, 97, 100, 55, 100, 55, 100, 55 }. */
15021 elements[0] = build_int_cst (element_type, 41);
15022 elements[1] = build_int_cst (element_type, 97);
15023 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15024 check_vector_cst_fill (elements, vector, 2);
15026 /* Try with and without a step
15027 { 41, 97, 100, 21, 100, 35, 100, 49 }. */
15028 for (unsigned int i = 3; i < 8; i += 2)
15029 elements[i] = build_int_cst (element_type, i * 7);
15030 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15031 check_vector_cst_stepped (elements, vector, 2);
15033 /* Try a fully-general constant:
15034 { 41, 97, 100, 21, 100, 9990, 100, 49 }. */
15035 elements[5] = build_int_cst (element_type, 9990);
15036 vector = build_vector (vector_type, elements PASS_MEM_STAT);
15037 check_vector_cst_fill (elements, vector, 4);
15040 /* Verify that STRIP_NOPS (NODE) is EXPECTED.
15041 Helper function for test_location_wrappers, to deal with STRIP_NOPS
15042 modifying its argument in-place. */
15044 static void
15045 check_strip_nops (tree node, tree expected)
15047 STRIP_NOPS (node);
15048 ASSERT_EQ (expected, node);
15051 /* Verify location wrappers. */
15053 static void
15054 test_location_wrappers ()
15056 location_t loc = BUILTINS_LOCATION;
15058 ASSERT_EQ (NULL_TREE, maybe_wrap_with_location (NULL_TREE, loc));
15060 /* Wrapping a constant. */
15061 tree int_cst = build_int_cst (integer_type_node, 42);
15062 ASSERT_FALSE (CAN_HAVE_LOCATION_P (int_cst));
15063 ASSERT_FALSE (location_wrapper_p (int_cst));
15065 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
15066 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
15067 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_int_cst));
15068 ASSERT_EQ (int_cst, tree_strip_any_location_wrapper (wrapped_int_cst));
15070 /* We shouldn't add wrapper nodes for UNKNOWN_LOCATION. */
15071 ASSERT_EQ (int_cst, maybe_wrap_with_location (int_cst, UNKNOWN_LOCATION));
15073 /* We shouldn't add wrapper nodes for nodes that CAN_HAVE_LOCATION_P. */
15074 tree cast = build1 (NOP_EXPR, char_type_node, int_cst);
15075 ASSERT_TRUE (CAN_HAVE_LOCATION_P (cast));
15076 ASSERT_EQ (cast, maybe_wrap_with_location (cast, loc));
15078 /* Wrapping a STRING_CST. */
15079 tree string_cst = build_string (4, "foo");
15080 ASSERT_FALSE (CAN_HAVE_LOCATION_P (string_cst));
15081 ASSERT_FALSE (location_wrapper_p (string_cst));
15083 tree wrapped_string_cst = maybe_wrap_with_location (string_cst, loc);
15084 ASSERT_TRUE (location_wrapper_p (wrapped_string_cst));
15085 ASSERT_EQ (VIEW_CONVERT_EXPR, TREE_CODE (wrapped_string_cst));
15086 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_string_cst));
15087 ASSERT_EQ (string_cst, tree_strip_any_location_wrapper (wrapped_string_cst));
15090 /* Wrapping a variable. */
15091 tree int_var = build_decl (UNKNOWN_LOCATION, VAR_DECL,
15092 get_identifier ("some_int_var"),
15093 integer_type_node);
15094 ASSERT_FALSE (CAN_HAVE_LOCATION_P (int_var));
15095 ASSERT_FALSE (location_wrapper_p (int_var));
15097 tree wrapped_int_var = maybe_wrap_with_location (int_var, loc);
15098 ASSERT_TRUE (location_wrapper_p (wrapped_int_var));
15099 ASSERT_EQ (loc, EXPR_LOCATION (wrapped_int_var));
15100 ASSERT_EQ (int_var, tree_strip_any_location_wrapper (wrapped_int_var));
15102 /* Verify that "reinterpret_cast<int>(some_int_var)" is not a location
15103 wrapper. */
15104 tree r_cast = build1 (NON_LVALUE_EXPR, integer_type_node, int_var);
15105 ASSERT_FALSE (location_wrapper_p (r_cast));
15106 ASSERT_EQ (r_cast, tree_strip_any_location_wrapper (r_cast));
15108 /* Verify that STRIP_NOPS removes wrappers. */
15109 check_strip_nops (wrapped_int_cst, int_cst);
15110 check_strip_nops (wrapped_string_cst, string_cst);
15111 check_strip_nops (wrapped_int_var, int_var);
15114 /* Check that string escaping works correctly. */
15116 static void
15117 test_escaped_strings (void)
15119 int saved_cutoff;
15120 escaped_string msg;
15122 msg.escape (NULL);
15123 /* ASSERT_STREQ does not accept NULL as a valid test
15124 result, so we have to use ASSERT_EQ instead. */
15125 ASSERT_EQ (NULL, (const char *) msg);
15127 msg.escape ("");
15128 ASSERT_STREQ ("", (const char *) msg);
15130 msg.escape ("foobar");
15131 ASSERT_STREQ ("foobar", (const char *) msg);
15133 /* Ensure that we have -fmessage-length set to 0. */
15134 saved_cutoff = pp_line_cutoff (global_dc->printer);
15135 pp_line_cutoff (global_dc->printer) = 0;
15137 msg.escape ("foo\nbar");
15138 ASSERT_STREQ ("foo\\nbar", (const char *) msg);
15140 msg.escape ("\a\b\f\n\r\t\v");
15141 ASSERT_STREQ ("\\a\\b\\f\\n\\r\\t\\v", (const char *) msg);
15143 /* Now repeat the tests with -fmessage-length set to 5. */
15144 pp_line_cutoff (global_dc->printer) = 5;
15146 /* Note that the newline is not translated into an escape. */
15147 msg.escape ("foo\nbar");
15148 ASSERT_STREQ ("foo\nbar", (const char *) msg);
15150 msg.escape ("\a\b\f\n\r\t\v");
15151 ASSERT_STREQ ("\\a\\b\\f\n\\r\\t\\v", (const char *) msg);
15153 /* Restore the original message length setting. */
15154 pp_line_cutoff (global_dc->printer) = saved_cutoff;
15157 /* Run all of the selftests within this file. */
15159 void
15160 tree_c_tests ()
15162 test_integer_constants ();
15163 test_identifiers ();
15164 test_labels ();
15165 test_vector_cst_patterns ();
15166 test_location_wrappers ();
15167 test_escaped_strings ();
15170 } // namespace selftest
15172 #endif /* CHECKING_P */
15174 #include "gt-tree.h"