svn merge -r 218679:218997 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / gcc / tree.c
blobc7482b7605a6334e87fa9c959aad236b79285783
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2014 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 occasionally
28 calls language-dependent routines defined (for C) in typecheck.c. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "flags.h"
35 #include "tree.h"
36 #include "stor-layout.h"
37 #include "calls.h"
38 #include "attribs.h"
39 #include "varasm.h"
40 #include "tm_p.h"
41 #include "hashtab.h"
42 #include "hash-set.h"
43 #include "vec.h"
44 #include "machmode.h"
45 #include "hard-reg-set.h"
46 #include "input.h"
47 #include "function.h"
48 #include "obstack.h"
49 #include "toplev.h" /* get_random_seed */
50 #include "inchash.h"
51 #include "filenames.h"
52 #include "output.h"
53 #include "target.h"
54 #include "common/common-target.h"
55 #include "langhooks.h"
56 #include "tree-inline.h"
57 #include "tree-iterator.h"
58 #include "predict.h"
59 #include "dominance.h"
60 #include "cfg.h"
61 #include "basic-block.h"
62 #include "bitmap.h"
63 #include "tree-ssa-alias.h"
64 #include "internal-fn.h"
65 #include "gimple-expr.h"
66 #include "is-a.h"
67 #include "gimple.h"
68 #include "gimple-iterator.h"
69 #include "gimplify.h"
70 #include "gimple-ssa.h"
71 #include "hash-map.h"
72 #include "plugin-api.h"
73 #include "ipa-ref.h"
74 #include "cgraph.h"
75 #include "tree-phinodes.h"
76 #include "stringpool.h"
77 #include "tree-ssanames.h"
78 #include "expr.h"
79 #include "tree-dfa.h"
80 #include "params.h"
81 #include "tree-pass.h"
82 #include "langhooks-def.h"
83 #include "diagnostic.h"
84 #include "tree-diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "except.h"
87 #include "debug.h"
88 #include "intl.h"
89 #include "wide-int.h"
90 #include "builtins.h"
92 /* Tree code classes. */
94 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
95 #define END_OF_BASE_TREE_CODES tcc_exceptional,
97 const enum tree_code_class tree_code_type[] = {
98 #include "all-tree.def"
101 #undef DEFTREECODE
102 #undef END_OF_BASE_TREE_CODES
104 /* Table indexed by tree code giving number of expression
105 operands beyond the fixed part of the node structure.
106 Not used for types or decls. */
108 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
109 #define END_OF_BASE_TREE_CODES 0,
111 const unsigned char tree_code_length[] = {
112 #include "all-tree.def"
115 #undef DEFTREECODE
116 #undef END_OF_BASE_TREE_CODES
118 /* Names of tree components.
119 Used for printing out the tree and error messages. */
120 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
121 #define END_OF_BASE_TREE_CODES "@dummy",
123 static const char *const tree_code_name[] = {
124 #include "all-tree.def"
127 #undef DEFTREECODE
128 #undef END_OF_BASE_TREE_CODES
130 /* Each tree code class has an associated string representation.
131 These must correspond to the tree_code_class entries. */
133 const char *const tree_code_class_strings[] =
135 "exceptional",
136 "constant",
137 "type",
138 "declaration",
139 "reference",
140 "comparison",
141 "unary",
142 "binary",
143 "statement",
144 "vl_exp",
145 "expression"
148 /* obstack.[ch] explicitly declined to prototype this. */
149 extern int _obstack_allocated_p (struct obstack *h, void *obj);
151 /* Statistics-gathering stuff. */
153 static int tree_code_counts[MAX_TREE_CODES];
154 int tree_node_counts[(int) all_kinds];
155 int tree_node_sizes[(int) all_kinds];
157 /* Keep in sync with tree.h:enum tree_node_kind. */
158 static const char * const tree_node_kind_names[] = {
159 "decls",
160 "types",
161 "blocks",
162 "stmts",
163 "refs",
164 "exprs",
165 "constants",
166 "identifiers",
167 "vecs",
168 "binfos",
169 "ssa names",
170 "constructors",
171 "random kinds",
172 "lang_decl kinds",
173 "lang_type kinds",
174 "omp clauses",
177 /* Unique id for next decl created. */
178 static GTY(()) int next_decl_uid;
179 /* Unique id for next type created. */
180 static GTY(()) int next_type_uid = 1;
181 /* Unique id for next debug decl created. Use negative numbers,
182 to catch erroneous uses. */
183 static GTY(()) int next_debug_decl_uid;
185 /* Since we cannot rehash a type after it is in the table, we have to
186 keep the hash code. */
188 struct GTY((for_user)) type_hash {
189 unsigned long hash;
190 tree type;
193 /* Initial size of the hash table (rounded to next prime). */
194 #define TYPE_HASH_INITIAL_SIZE 1000
196 struct type_cache_hasher : ggc_cache_hasher<type_hash *>
198 static hashval_t hash (type_hash *t) { return t->hash; }
199 static bool equal (type_hash *a, type_hash *b);
201 static void
202 handle_cache_entry (type_hash *&t)
204 extern void gt_ggc_mx (type_hash *&);
205 if (t == HTAB_DELETED_ENTRY || t == HTAB_EMPTY_ENTRY)
206 return;
207 else if (ggc_marked_p (t->type))
208 gt_ggc_mx (t);
209 else
210 t = static_cast<type_hash *> (HTAB_DELETED_ENTRY);
214 /* Now here is the hash table. When recording a type, it is added to
215 the slot whose index is the hash code. Note that the hash table is
216 used for several kinds of types (function types, array types and
217 array index range types, for now). While all these live in the
218 same table, they are completely independent, and the hash code is
219 computed differently for each of these. */
221 static GTY ((cache)) hash_table<type_cache_hasher> *type_hash_table;
223 /* Hash table and temporary node for larger integer const values. */
224 static GTY (()) tree int_cst_node;
226 struct int_cst_hasher : ggc_cache_hasher<tree>
228 static hashval_t hash (tree t);
229 static bool equal (tree x, tree y);
232 static GTY ((cache)) hash_table<int_cst_hasher> *int_cst_hash_table;
234 /* Hash table for optimization flags and target option flags. Use the same
235 hash table for both sets of options. Nodes for building the current
236 optimization and target option nodes. The assumption is most of the time
237 the options created will already be in the hash table, so we avoid
238 allocating and freeing up a node repeatably. */
239 static GTY (()) tree cl_optimization_node;
240 static GTY (()) tree cl_target_option_node;
242 struct cl_option_hasher : ggc_cache_hasher<tree>
244 static hashval_t hash (tree t);
245 static bool equal (tree x, tree y);
248 static GTY ((cache)) hash_table<cl_option_hasher> *cl_option_hash_table;
250 /* General tree->tree mapping structure for use in hash tables. */
253 static GTY ((cache))
254 hash_table<tree_decl_map_cache_hasher> *debug_expr_for_decl;
256 static GTY ((cache))
257 hash_table<tree_decl_map_cache_hasher> *value_expr_for_decl;
259 struct tree_vec_map_cache_hasher : ggc_cache_hasher<tree_vec_map *>
261 static hashval_t hash (tree_vec_map *m) { return DECL_UID (m->base.from); }
263 static bool
264 equal (tree_vec_map *a, tree_vec_map *b)
266 return a->base.from == b->base.from;
269 static void
270 handle_cache_entry (tree_vec_map *&m)
272 extern void gt_ggc_mx (tree_vec_map *&);
273 if (m == HTAB_EMPTY_ENTRY || m == HTAB_DELETED_ENTRY)
274 return;
275 else if (ggc_marked_p (m->base.from))
276 gt_ggc_mx (m);
277 else
278 m = static_cast<tree_vec_map *> (HTAB_DELETED_ENTRY);
282 static GTY ((cache))
283 hash_table<tree_vec_map_cache_hasher> *debug_args_for_decl;
285 static void set_type_quals (tree, int);
286 static void print_type_hash_statistics (void);
287 static void print_debug_expr_statistics (void);
288 static void print_value_expr_statistics (void);
289 static void type_hash_list (const_tree, inchash::hash &);
290 static void attribute_hash_list (const_tree, inchash::hash &);
292 tree global_trees[TI_MAX];
293 tree integer_types[itk_none];
295 bool int_n_enabled_p[NUM_INT_N_ENTS];
296 struct int_n_trees_t int_n_trees [NUM_INT_N_ENTS];
298 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
300 /* Number of operands for each OpenMP clause. */
301 unsigned const char omp_clause_num_ops[] =
303 0, /* OMP_CLAUSE_ERROR */
304 1, /* OMP_CLAUSE_PRIVATE */
305 1, /* OMP_CLAUSE_SHARED */
306 1, /* OMP_CLAUSE_FIRSTPRIVATE */
307 2, /* OMP_CLAUSE_LASTPRIVATE */
308 4, /* OMP_CLAUSE_REDUCTION */
309 1, /* OMP_CLAUSE_COPYIN */
310 1, /* OMP_CLAUSE_COPYPRIVATE */
311 3, /* OMP_CLAUSE_LINEAR */
312 2, /* OMP_CLAUSE_ALIGNED */
313 1, /* OMP_CLAUSE_DEPEND */
314 1, /* OMP_CLAUSE_UNIFORM */
315 2, /* OMP_CLAUSE_FROM */
316 2, /* OMP_CLAUSE_TO */
317 2, /* OMP_CLAUSE_MAP */
318 2, /* OMP_CLAUSE__CACHE_ */
319 1, /* OMP_CLAUSE_DEVICE_RESIDENT */
320 1, /* OMP_CLAUSE_USE_DEVICE */
321 2, /* OMP_CLAUSE_GANG */
322 1, /* OMP_CLAUSE_ASYNC */
323 1, /* OMP_CLAUSE_WAIT */
324 0, /* OMP_CLAUSE_AUTO */
325 0, /* OMP_CLAUSE_SEQ */
326 1, /* OMP_CLAUSE__LOOPTEMP_ */
327 1, /* OMP_CLAUSE_IF */
328 1, /* OMP_CLAUSE_NUM_THREADS */
329 1, /* OMP_CLAUSE_SCHEDULE */
330 0, /* OMP_CLAUSE_NOWAIT */
331 0, /* OMP_CLAUSE_ORDERED */
332 0, /* OMP_CLAUSE_DEFAULT */
333 3, /* OMP_CLAUSE_COLLAPSE */
334 0, /* OMP_CLAUSE_UNTIED */
335 1, /* OMP_CLAUSE_FINAL */
336 0, /* OMP_CLAUSE_MERGEABLE */
337 1, /* OMP_CLAUSE_DEVICE */
338 1, /* OMP_CLAUSE_DIST_SCHEDULE */
339 0, /* OMP_CLAUSE_INBRANCH */
340 0, /* OMP_CLAUSE_NOTINBRANCH */
341 1, /* OMP_CLAUSE_NUM_TEAMS */
342 1, /* OMP_CLAUSE_THREAD_LIMIT */
343 0, /* OMP_CLAUSE_PROC_BIND */
344 1, /* OMP_CLAUSE_SAFELEN */
345 1, /* OMP_CLAUSE_SIMDLEN */
346 0, /* OMP_CLAUSE_FOR */
347 0, /* OMP_CLAUSE_PARALLEL */
348 0, /* OMP_CLAUSE_SECTIONS */
349 0, /* OMP_CLAUSE_TASKGROUP */
350 1, /* OMP_CLAUSE__SIMDUID_ */
351 1, /* OMP_CLAUSE__CILK_FOR_COUNT_ */
352 0, /* OMP_CLAUSE_INDEPENDENT */
353 1, /* OMP_CLAUSE_WORKER */
354 1, /* OMP_CLAUSE_VECTOR */
355 1, /* OMP_CLAUSE_NUM_GANGS */
356 1, /* OMP_CLAUSE_NUM_WORKERS */
357 1, /* OMP_CLAUSE_VECTOR_LENGTH */
360 const char * const omp_clause_code_name[] =
362 "error_clause",
363 "private",
364 "shared",
365 "firstprivate",
366 "lastprivate",
367 "reduction",
368 "copyin",
369 "copyprivate",
370 "linear",
371 "aligned",
372 "depend",
373 "uniform",
374 "from",
375 "to",
376 "map",
377 "_cache_",
378 "device_resident",
379 "use_device",
380 "gang",
381 "async",
382 "wait",
383 "auto",
384 "seq",
385 "_looptemp_",
386 "if",
387 "num_threads",
388 "schedule",
389 "nowait",
390 "ordered",
391 "default",
392 "collapse",
393 "untied",
394 "final",
395 "mergeable",
396 "device",
397 "dist_schedule",
398 "inbranch",
399 "notinbranch",
400 "num_teams",
401 "thread_limit",
402 "proc_bind",
403 "safelen",
404 "simdlen",
405 "for",
406 "parallel",
407 "sections",
408 "taskgroup",
409 "_simduid_",
410 "_Cilk_for_count_",
411 "independent",
412 "worker",
413 "vector",
414 "num_gangs",
415 "num_workers",
416 "vector_length"
420 /* Return the tree node structure used by tree code CODE. */
422 static inline enum tree_node_structure_enum
423 tree_node_structure_for_code (enum tree_code code)
425 switch (TREE_CODE_CLASS (code))
427 case tcc_declaration:
429 switch (code)
431 case FIELD_DECL:
432 return TS_FIELD_DECL;
433 case PARM_DECL:
434 return TS_PARM_DECL;
435 case VAR_DECL:
436 return TS_VAR_DECL;
437 case LABEL_DECL:
438 return TS_LABEL_DECL;
439 case RESULT_DECL:
440 return TS_RESULT_DECL;
441 case DEBUG_EXPR_DECL:
442 return TS_DECL_WRTL;
443 case CONST_DECL:
444 return TS_CONST_DECL;
445 case TYPE_DECL:
446 return TS_TYPE_DECL;
447 case FUNCTION_DECL:
448 return TS_FUNCTION_DECL;
449 case TRANSLATION_UNIT_DECL:
450 return TS_TRANSLATION_UNIT_DECL;
451 default:
452 return TS_DECL_NON_COMMON;
455 case tcc_type:
456 return TS_TYPE_NON_COMMON;
457 case tcc_reference:
458 case tcc_comparison:
459 case tcc_unary:
460 case tcc_binary:
461 case tcc_expression:
462 case tcc_statement:
463 case tcc_vl_exp:
464 return TS_EXP;
465 default: /* tcc_constant and tcc_exceptional */
466 break;
468 switch (code)
470 /* tcc_constant cases. */
471 case VOID_CST: return TS_TYPED;
472 case INTEGER_CST: return TS_INT_CST;
473 case REAL_CST: return TS_REAL_CST;
474 case FIXED_CST: return TS_FIXED_CST;
475 case COMPLEX_CST: return TS_COMPLEX;
476 case VECTOR_CST: return TS_VECTOR;
477 case STRING_CST: return TS_STRING;
478 /* tcc_exceptional cases. */
479 case ERROR_MARK: return TS_COMMON;
480 case IDENTIFIER_NODE: return TS_IDENTIFIER;
481 case TREE_LIST: return TS_LIST;
482 case TREE_VEC: return TS_VEC;
483 case SSA_NAME: return TS_SSA_NAME;
484 case PLACEHOLDER_EXPR: return TS_COMMON;
485 case STATEMENT_LIST: return TS_STATEMENT_LIST;
486 case BLOCK: return TS_BLOCK;
487 case CONSTRUCTOR: return TS_CONSTRUCTOR;
488 case TREE_BINFO: return TS_BINFO;
489 case OMP_CLAUSE: return TS_OMP_CLAUSE;
490 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
491 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
493 default:
494 gcc_unreachable ();
499 /* Initialize tree_contains_struct to describe the hierarchy of tree
500 nodes. */
502 static void
503 initialize_tree_contains_struct (void)
505 unsigned i;
507 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
509 enum tree_code code;
510 enum tree_node_structure_enum ts_code;
512 code = (enum tree_code) i;
513 ts_code = tree_node_structure_for_code (code);
515 /* Mark the TS structure itself. */
516 tree_contains_struct[code][ts_code] = 1;
518 /* Mark all the structures that TS is derived from. */
519 switch (ts_code)
521 case TS_TYPED:
522 case TS_BLOCK:
523 MARK_TS_BASE (code);
524 break;
526 case TS_COMMON:
527 case TS_INT_CST:
528 case TS_REAL_CST:
529 case TS_FIXED_CST:
530 case TS_VECTOR:
531 case TS_STRING:
532 case TS_COMPLEX:
533 case TS_SSA_NAME:
534 case TS_CONSTRUCTOR:
535 case TS_EXP:
536 case TS_STATEMENT_LIST:
537 MARK_TS_TYPED (code);
538 break;
540 case TS_IDENTIFIER:
541 case TS_DECL_MINIMAL:
542 case TS_TYPE_COMMON:
543 case TS_LIST:
544 case TS_VEC:
545 case TS_BINFO:
546 case TS_OMP_CLAUSE:
547 case TS_OPTIMIZATION:
548 case TS_TARGET_OPTION:
549 MARK_TS_COMMON (code);
550 break;
552 case TS_TYPE_WITH_LANG_SPECIFIC:
553 MARK_TS_TYPE_COMMON (code);
554 break;
556 case TS_TYPE_NON_COMMON:
557 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
558 break;
560 case TS_DECL_COMMON:
561 MARK_TS_DECL_MINIMAL (code);
562 break;
564 case TS_DECL_WRTL:
565 case TS_CONST_DECL:
566 MARK_TS_DECL_COMMON (code);
567 break;
569 case TS_DECL_NON_COMMON:
570 MARK_TS_DECL_WITH_VIS (code);
571 break;
573 case TS_DECL_WITH_VIS:
574 case TS_PARM_DECL:
575 case TS_LABEL_DECL:
576 case TS_RESULT_DECL:
577 MARK_TS_DECL_WRTL (code);
578 break;
580 case TS_FIELD_DECL:
581 MARK_TS_DECL_COMMON (code);
582 break;
584 case TS_VAR_DECL:
585 MARK_TS_DECL_WITH_VIS (code);
586 break;
588 case TS_TYPE_DECL:
589 case TS_FUNCTION_DECL:
590 MARK_TS_DECL_NON_COMMON (code);
591 break;
593 case TS_TRANSLATION_UNIT_DECL:
594 MARK_TS_DECL_COMMON (code);
595 break;
597 default:
598 gcc_unreachable ();
602 /* Basic consistency checks for attributes used in fold. */
603 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
604 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
605 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
606 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
607 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
608 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
609 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
610 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
611 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
612 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
613 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
614 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
615 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
616 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
617 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
618 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
619 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
620 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
621 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
622 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
623 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
624 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
625 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
626 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
627 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
628 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
629 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
630 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
631 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
632 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
633 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
634 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
635 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
636 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
637 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
638 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
639 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
640 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
641 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_MINIMAL]);
642 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_COMMON]);
646 /* Init tree.c. */
648 void
649 init_ttree (void)
651 /* Initialize the hash table of types. */
652 type_hash_table
653 = hash_table<type_cache_hasher>::create_ggc (TYPE_HASH_INITIAL_SIZE);
655 debug_expr_for_decl
656 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
658 value_expr_for_decl
659 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
661 int_cst_hash_table = hash_table<int_cst_hasher>::create_ggc (1024);
663 int_cst_node = make_int_cst (1, 1);
665 cl_option_hash_table = hash_table<cl_option_hasher>::create_ggc (64);
667 cl_optimization_node = make_node (OPTIMIZATION_NODE);
668 cl_target_option_node = make_node (TARGET_OPTION_NODE);
670 /* Initialize the tree_contains_struct array. */
671 initialize_tree_contains_struct ();
672 lang_hooks.init_ts ();
676 /* The name of the object as the assembler will see it (but before any
677 translations made by ASM_OUTPUT_LABELREF). Often this is the same
678 as DECL_NAME. It is an IDENTIFIER_NODE. */
679 tree
680 decl_assembler_name (tree decl)
682 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
683 lang_hooks.set_decl_assembler_name (decl);
684 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
687 /* When the target supports COMDAT groups, this indicates which group the
688 DECL is associated with. This can be either an IDENTIFIER_NODE or a
689 decl, in which case its DECL_ASSEMBLER_NAME identifies the group. */
690 tree
691 decl_comdat_group (const_tree node)
693 struct symtab_node *snode = symtab_node::get (node);
694 if (!snode)
695 return NULL;
696 return snode->get_comdat_group ();
699 /* Likewise, but make sure it's been reduced to an IDENTIFIER_NODE. */
700 tree
701 decl_comdat_group_id (const_tree node)
703 struct symtab_node *snode = symtab_node::get (node);
704 if (!snode)
705 return NULL;
706 return snode->get_comdat_group_id ();
709 /* When the target supports named section, return its name as IDENTIFIER_NODE
710 or NULL if it is in no section. */
711 const char *
712 decl_section_name (const_tree node)
714 struct symtab_node *snode = symtab_node::get (node);
715 if (!snode)
716 return NULL;
717 return snode->get_section ();
720 /* Set section section name of NODE to VALUE (that is expected to
721 be identifier node) */
722 void
723 set_decl_section_name (tree node, const char *value)
725 struct symtab_node *snode;
727 if (value == NULL)
729 snode = symtab_node::get (node);
730 if (!snode)
731 return;
733 else if (TREE_CODE (node) == VAR_DECL)
734 snode = varpool_node::get_create (node);
735 else
736 snode = cgraph_node::get_create (node);
737 snode->set_section (value);
740 /* Return TLS model of a variable NODE. */
741 enum tls_model
742 decl_tls_model (const_tree node)
744 struct varpool_node *snode = varpool_node::get (node);
745 if (!snode)
746 return TLS_MODEL_NONE;
747 return snode->tls_model;
750 /* Set TLS model of variable NODE to MODEL. */
751 void
752 set_decl_tls_model (tree node, enum tls_model model)
754 struct varpool_node *vnode;
756 if (model == TLS_MODEL_NONE)
758 vnode = varpool_node::get (node);
759 if (!vnode)
760 return;
762 else
763 vnode = varpool_node::get_create (node);
764 vnode->tls_model = model;
767 /* Compute the number of bytes occupied by a tree with code CODE.
768 This function cannot be used for nodes that have variable sizes,
769 including TREE_VEC, INTEGER_CST, STRING_CST, and CALL_EXPR. */
770 size_t
771 tree_code_size (enum tree_code code)
773 switch (TREE_CODE_CLASS (code))
775 case tcc_declaration: /* A decl node */
777 switch (code)
779 case FIELD_DECL:
780 return sizeof (struct tree_field_decl);
781 case PARM_DECL:
782 return sizeof (struct tree_parm_decl);
783 case VAR_DECL:
784 return sizeof (struct tree_var_decl);
785 case LABEL_DECL:
786 return sizeof (struct tree_label_decl);
787 case RESULT_DECL:
788 return sizeof (struct tree_result_decl);
789 case CONST_DECL:
790 return sizeof (struct tree_const_decl);
791 case TYPE_DECL:
792 return sizeof (struct tree_type_decl);
793 case FUNCTION_DECL:
794 return sizeof (struct tree_function_decl);
795 case DEBUG_EXPR_DECL:
796 return sizeof (struct tree_decl_with_rtl);
797 case TRANSLATION_UNIT_DECL:
798 return sizeof (struct tree_translation_unit_decl);
799 case NAMESPACE_DECL:
800 case IMPORTED_DECL:
801 case NAMELIST_DECL:
802 return sizeof (struct tree_decl_non_common);
803 default:
804 return lang_hooks.tree_size (code);
808 case tcc_type: /* a type node */
809 return sizeof (struct tree_type_non_common);
811 case tcc_reference: /* a reference */
812 case tcc_expression: /* an expression */
813 case tcc_statement: /* an expression with side effects */
814 case tcc_comparison: /* a comparison expression */
815 case tcc_unary: /* a unary arithmetic expression */
816 case tcc_binary: /* a binary arithmetic expression */
817 return (sizeof (struct tree_exp)
818 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
820 case tcc_constant: /* a constant */
821 switch (code)
823 case VOID_CST: return sizeof (struct tree_typed);
824 case INTEGER_CST: gcc_unreachable ();
825 case REAL_CST: return sizeof (struct tree_real_cst);
826 case FIXED_CST: return sizeof (struct tree_fixed_cst);
827 case COMPLEX_CST: return sizeof (struct tree_complex);
828 case VECTOR_CST: return sizeof (struct tree_vector);
829 case STRING_CST: gcc_unreachable ();
830 default:
831 return lang_hooks.tree_size (code);
834 case tcc_exceptional: /* something random, like an identifier. */
835 switch (code)
837 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
838 case TREE_LIST: return sizeof (struct tree_list);
840 case ERROR_MARK:
841 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
843 case TREE_VEC:
844 case OMP_CLAUSE: gcc_unreachable ();
846 case SSA_NAME: return sizeof (struct tree_ssa_name);
848 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
849 case BLOCK: return sizeof (struct tree_block);
850 case CONSTRUCTOR: return sizeof (struct tree_constructor);
851 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
852 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
854 default:
855 return lang_hooks.tree_size (code);
858 default:
859 gcc_unreachable ();
863 /* Compute the number of bytes occupied by NODE. This routine only
864 looks at TREE_CODE, except for those nodes that have variable sizes. */
865 size_t
866 tree_size (const_tree node)
868 const enum tree_code code = TREE_CODE (node);
869 switch (code)
871 case INTEGER_CST:
872 return (sizeof (struct tree_int_cst)
873 + (TREE_INT_CST_EXT_NUNITS (node) - 1) * sizeof (HOST_WIDE_INT));
875 case TREE_BINFO:
876 return (offsetof (struct tree_binfo, base_binfos)
877 + vec<tree, va_gc>
878 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
880 case TREE_VEC:
881 return (sizeof (struct tree_vec)
882 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
884 case VECTOR_CST:
885 return (sizeof (struct tree_vector)
886 + (TYPE_VECTOR_SUBPARTS (TREE_TYPE (node)) - 1) * sizeof (tree));
888 case STRING_CST:
889 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
891 case OMP_CLAUSE:
892 return (sizeof (struct tree_omp_clause)
893 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
894 * sizeof (tree));
896 default:
897 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
898 return (sizeof (struct tree_exp)
899 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
900 else
901 return tree_code_size (code);
905 /* Record interesting allocation statistics for a tree node with CODE
906 and LENGTH. */
908 static void
909 record_node_allocation_statistics (enum tree_code code ATTRIBUTE_UNUSED,
910 size_t length ATTRIBUTE_UNUSED)
912 enum tree_code_class type = TREE_CODE_CLASS (code);
913 tree_node_kind kind;
915 if (!GATHER_STATISTICS)
916 return;
918 switch (type)
920 case tcc_declaration: /* A decl node */
921 kind = d_kind;
922 break;
924 case tcc_type: /* a type node */
925 kind = t_kind;
926 break;
928 case tcc_statement: /* an expression with side effects */
929 kind = s_kind;
930 break;
932 case tcc_reference: /* a reference */
933 kind = r_kind;
934 break;
936 case tcc_expression: /* an expression */
937 case tcc_comparison: /* a comparison expression */
938 case tcc_unary: /* a unary arithmetic expression */
939 case tcc_binary: /* a binary arithmetic expression */
940 kind = e_kind;
941 break;
943 case tcc_constant: /* a constant */
944 kind = c_kind;
945 break;
947 case tcc_exceptional: /* something random, like an identifier. */
948 switch (code)
950 case IDENTIFIER_NODE:
951 kind = id_kind;
952 break;
954 case TREE_VEC:
955 kind = vec_kind;
956 break;
958 case TREE_BINFO:
959 kind = binfo_kind;
960 break;
962 case SSA_NAME:
963 kind = ssa_name_kind;
964 break;
966 case BLOCK:
967 kind = b_kind;
968 break;
970 case CONSTRUCTOR:
971 kind = constr_kind;
972 break;
974 case OMP_CLAUSE:
975 kind = omp_clause_kind;
976 break;
978 default:
979 kind = x_kind;
980 break;
982 break;
984 case tcc_vl_exp:
985 kind = e_kind;
986 break;
988 default:
989 gcc_unreachable ();
992 tree_code_counts[(int) code]++;
993 tree_node_counts[(int) kind]++;
994 tree_node_sizes[(int) kind] += length;
997 /* Allocate and return a new UID from the DECL_UID namespace. */
1000 allocate_decl_uid (void)
1002 return next_decl_uid++;
1005 /* Return a newly allocated node of code CODE. For decl and type
1006 nodes, some other fields are initialized. The rest of the node is
1007 initialized to zero. This function cannot be used for TREE_VEC,
1008 INTEGER_CST or OMP_CLAUSE nodes, which is enforced by asserts in
1009 tree_code_size.
1011 Achoo! I got a code in the node. */
1013 tree
1014 make_node_stat (enum tree_code code MEM_STAT_DECL)
1016 tree t;
1017 enum tree_code_class type = TREE_CODE_CLASS (code);
1018 size_t length = tree_code_size (code);
1020 record_node_allocation_statistics (code, length);
1022 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1023 TREE_SET_CODE (t, code);
1025 switch (type)
1027 case tcc_statement:
1028 TREE_SIDE_EFFECTS (t) = 1;
1029 break;
1031 case tcc_declaration:
1032 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1034 if (code == FUNCTION_DECL)
1036 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
1037 DECL_MODE (t) = FUNCTION_MODE;
1039 else
1040 DECL_ALIGN (t) = 1;
1042 DECL_SOURCE_LOCATION (t) = input_location;
1043 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
1044 DECL_UID (t) = --next_debug_decl_uid;
1045 else
1047 DECL_UID (t) = allocate_decl_uid ();
1048 SET_DECL_PT_UID (t, -1);
1050 if (TREE_CODE (t) == LABEL_DECL)
1051 LABEL_DECL_UID (t) = -1;
1053 break;
1055 case tcc_type:
1056 TYPE_UID (t) = next_type_uid++;
1057 TYPE_ALIGN (t) = BITS_PER_UNIT;
1058 TYPE_USER_ALIGN (t) = 0;
1059 TYPE_MAIN_VARIANT (t) = t;
1060 TYPE_CANONICAL (t) = t;
1062 /* Default to no attributes for type, but let target change that. */
1063 TYPE_ATTRIBUTES (t) = NULL_TREE;
1064 targetm.set_default_type_attributes (t);
1066 /* We have not yet computed the alias set for this type. */
1067 TYPE_ALIAS_SET (t) = -1;
1068 break;
1070 case tcc_constant:
1071 TREE_CONSTANT (t) = 1;
1072 break;
1074 case tcc_expression:
1075 switch (code)
1077 case INIT_EXPR:
1078 case MODIFY_EXPR:
1079 case VA_ARG_EXPR:
1080 case PREDECREMENT_EXPR:
1081 case PREINCREMENT_EXPR:
1082 case POSTDECREMENT_EXPR:
1083 case POSTINCREMENT_EXPR:
1084 /* All of these have side-effects, no matter what their
1085 operands are. */
1086 TREE_SIDE_EFFECTS (t) = 1;
1087 break;
1089 default:
1090 break;
1092 break;
1094 default:
1095 /* Other classes need no special treatment. */
1096 break;
1099 return t;
1102 /* Return a new node with the same contents as NODE except that its
1103 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
1105 tree
1106 copy_node_stat (tree node MEM_STAT_DECL)
1108 tree t;
1109 enum tree_code code = TREE_CODE (node);
1110 size_t length;
1112 gcc_assert (code != STATEMENT_LIST);
1114 length = tree_size (node);
1115 record_node_allocation_statistics (code, length);
1116 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1117 memcpy (t, node, length);
1119 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
1120 TREE_CHAIN (t) = 0;
1121 TREE_ASM_WRITTEN (t) = 0;
1122 TREE_VISITED (t) = 0;
1124 if (TREE_CODE_CLASS (code) == tcc_declaration)
1126 if (code == DEBUG_EXPR_DECL)
1127 DECL_UID (t) = --next_debug_decl_uid;
1128 else
1130 DECL_UID (t) = allocate_decl_uid ();
1131 if (DECL_PT_UID_SET_P (node))
1132 SET_DECL_PT_UID (t, DECL_PT_UID (node));
1134 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
1135 && DECL_HAS_VALUE_EXPR_P (node))
1137 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
1138 DECL_HAS_VALUE_EXPR_P (t) = 1;
1140 /* DECL_DEBUG_EXPR is copied explicitely by callers. */
1141 if (TREE_CODE (node) == VAR_DECL)
1143 DECL_HAS_DEBUG_EXPR_P (t) = 0;
1144 t->decl_with_vis.symtab_node = NULL;
1146 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
1148 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
1149 DECL_HAS_INIT_PRIORITY_P (t) = 1;
1151 if (TREE_CODE (node) == FUNCTION_DECL)
1153 DECL_STRUCT_FUNCTION (t) = NULL;
1154 t->decl_with_vis.symtab_node = NULL;
1157 else if (TREE_CODE_CLASS (code) == tcc_type)
1159 TYPE_UID (t) = next_type_uid++;
1160 /* The following is so that the debug code for
1161 the copy is different from the original type.
1162 The two statements usually duplicate each other
1163 (because they clear fields of the same union),
1164 but the optimizer should catch that. */
1165 TYPE_SYMTAB_POINTER (t) = 0;
1166 TYPE_SYMTAB_ADDRESS (t) = 0;
1168 /* Do not copy the values cache. */
1169 if (TYPE_CACHED_VALUES_P (t))
1171 TYPE_CACHED_VALUES_P (t) = 0;
1172 TYPE_CACHED_VALUES (t) = NULL_TREE;
1176 return t;
1179 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1180 For example, this can copy a list made of TREE_LIST nodes. */
1182 tree
1183 copy_list (tree list)
1185 tree head;
1186 tree prev, next;
1188 if (list == 0)
1189 return 0;
1191 head = prev = copy_node (list);
1192 next = TREE_CHAIN (list);
1193 while (next)
1195 TREE_CHAIN (prev) = copy_node (next);
1196 prev = TREE_CHAIN (prev);
1197 next = TREE_CHAIN (next);
1199 return head;
1203 /* Return the value that TREE_INT_CST_EXT_NUNITS should have for an
1204 INTEGER_CST with value CST and type TYPE. */
1206 static unsigned int
1207 get_int_cst_ext_nunits (tree type, const wide_int &cst)
1209 gcc_checking_assert (cst.get_precision () == TYPE_PRECISION (type));
1210 /* We need an extra zero HWI if CST is an unsigned integer with its
1211 upper bit set, and if CST occupies a whole number of HWIs. */
1212 if (TYPE_UNSIGNED (type)
1213 && wi::neg_p (cst)
1214 && (cst.get_precision () % HOST_BITS_PER_WIDE_INT) == 0)
1215 return cst.get_precision () / HOST_BITS_PER_WIDE_INT + 1;
1216 return cst.get_len ();
1219 /* Return a new INTEGER_CST with value CST and type TYPE. */
1221 static tree
1222 build_new_int_cst (tree type, const wide_int &cst)
1224 unsigned int len = cst.get_len ();
1225 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1226 tree nt = make_int_cst (len, ext_len);
1228 if (len < ext_len)
1230 --ext_len;
1231 TREE_INT_CST_ELT (nt, ext_len) = 0;
1232 for (unsigned int i = len; i < ext_len; ++i)
1233 TREE_INT_CST_ELT (nt, i) = -1;
1235 else if (TYPE_UNSIGNED (type)
1236 && cst.get_precision () < len * HOST_BITS_PER_WIDE_INT)
1238 len--;
1239 TREE_INT_CST_ELT (nt, len)
1240 = zext_hwi (cst.elt (len),
1241 cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1244 for (unsigned int i = 0; i < len; i++)
1245 TREE_INT_CST_ELT (nt, i) = cst.elt (i);
1246 TREE_TYPE (nt) = type;
1247 return nt;
1250 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1252 tree
1253 build_int_cst (tree type, HOST_WIDE_INT low)
1255 /* Support legacy code. */
1256 if (!type)
1257 type = integer_type_node;
1259 return wide_int_to_tree (type, wi::shwi (low, TYPE_PRECISION (type)));
1262 tree
1263 build_int_cstu (tree type, unsigned HOST_WIDE_INT cst)
1265 return wide_int_to_tree (type, wi::uhwi (cst, TYPE_PRECISION (type)));
1268 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1270 tree
1271 build_int_cst_type (tree type, HOST_WIDE_INT low)
1273 gcc_assert (type);
1274 return wide_int_to_tree (type, wi::shwi (low, TYPE_PRECISION (type)));
1277 /* Constructs tree in type TYPE from with value given by CST. Signedness
1278 of CST is assumed to be the same as the signedness of TYPE. */
1280 tree
1281 double_int_to_tree (tree type, double_int cst)
1283 return wide_int_to_tree (type, widest_int::from (cst, TYPE_SIGN (type)));
1286 /* We force the wide_int CST to the range of the type TYPE by sign or
1287 zero extending it. OVERFLOWABLE indicates if we are interested in
1288 overflow of the value, when >0 we are only interested in signed
1289 overflow, for <0 we are interested in any overflow. OVERFLOWED
1290 indicates whether overflow has already occurred. CONST_OVERFLOWED
1291 indicates whether constant overflow has already occurred. We force
1292 T's value to be within range of T's type (by setting to 0 or 1 all
1293 the bits outside the type's range). We set TREE_OVERFLOWED if,
1294 OVERFLOWED is nonzero,
1295 or OVERFLOWABLE is >0 and signed overflow occurs
1296 or OVERFLOWABLE is <0 and any overflow occurs
1297 We return a new tree node for the extended wide_int. The node
1298 is shared if no overflow flags are set. */
1301 tree
1302 force_fit_type (tree type, const wide_int_ref &cst,
1303 int overflowable, bool overflowed)
1305 signop sign = TYPE_SIGN (type);
1307 /* If we need to set overflow flags, return a new unshared node. */
1308 if (overflowed || !wi::fits_to_tree_p (cst, type))
1310 if (overflowed
1311 || overflowable < 0
1312 || (overflowable > 0 && sign == SIGNED))
1314 wide_int tmp = wide_int::from (cst, TYPE_PRECISION (type), sign);
1315 tree t = build_new_int_cst (type, tmp);
1316 TREE_OVERFLOW (t) = 1;
1317 return t;
1321 /* Else build a shared node. */
1322 return wide_int_to_tree (type, cst);
1325 /* These are the hash table functions for the hash table of INTEGER_CST
1326 nodes of a sizetype. */
1328 /* Return the hash code code X, an INTEGER_CST. */
1330 hashval_t
1331 int_cst_hasher::hash (tree x)
1333 const_tree const t = x;
1334 hashval_t code = TYPE_UID (TREE_TYPE (t));
1335 int i;
1337 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
1338 code ^= TREE_INT_CST_ELT (t, i);
1340 return code;
1343 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1344 is the same as that given by *Y, which is the same. */
1346 bool
1347 int_cst_hasher::equal (tree x, tree y)
1349 const_tree const xt = x;
1350 const_tree const yt = y;
1352 if (TREE_TYPE (xt) != TREE_TYPE (yt)
1353 || TREE_INT_CST_NUNITS (xt) != TREE_INT_CST_NUNITS (yt)
1354 || TREE_INT_CST_EXT_NUNITS (xt) != TREE_INT_CST_EXT_NUNITS (yt))
1355 return false;
1357 for (int i = 0; i < TREE_INT_CST_NUNITS (xt); i++)
1358 if (TREE_INT_CST_ELT (xt, i) != TREE_INT_CST_ELT (yt, i))
1359 return false;
1361 return true;
1364 /* Create an INT_CST node of TYPE and value CST.
1365 The returned node is always shared. For small integers we use a
1366 per-type vector cache, for larger ones we use a single hash table.
1367 The value is extended from its precision according to the sign of
1368 the type to be a multiple of HOST_BITS_PER_WIDE_INT. This defines
1369 the upper bits and ensures that hashing and value equality based
1370 upon the underlying HOST_WIDE_INTs works without masking. */
1372 tree
1373 wide_int_to_tree (tree type, const wide_int_ref &pcst)
1375 tree t;
1376 int ix = -1;
1377 int limit = 0;
1379 gcc_assert (type);
1380 unsigned int prec = TYPE_PRECISION (type);
1381 signop sgn = TYPE_SIGN (type);
1383 /* Verify that everything is canonical. */
1384 int l = pcst.get_len ();
1385 if (l > 1)
1387 if (pcst.elt (l - 1) == 0)
1388 gcc_checking_assert (pcst.elt (l - 2) < 0);
1389 if (pcst.elt (l - 1) == (HOST_WIDE_INT) -1)
1390 gcc_checking_assert (pcst.elt (l - 2) >= 0);
1393 wide_int cst = wide_int::from (pcst, prec, sgn);
1394 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1396 if (ext_len == 1)
1398 /* We just need to store a single HOST_WIDE_INT. */
1399 HOST_WIDE_INT hwi;
1400 if (TYPE_UNSIGNED (type))
1401 hwi = cst.to_uhwi ();
1402 else
1403 hwi = cst.to_shwi ();
1405 switch (TREE_CODE (type))
1407 case NULLPTR_TYPE:
1408 gcc_assert (hwi == 0);
1409 /* Fallthru. */
1411 case POINTER_TYPE:
1412 case REFERENCE_TYPE:
1413 case POINTER_BOUNDS_TYPE:
1414 /* Cache NULL pointer and zero bounds. */
1415 if (hwi == 0)
1417 limit = 1;
1418 ix = 0;
1420 break;
1422 case BOOLEAN_TYPE:
1423 /* Cache false or true. */
1424 limit = 2;
1425 if (hwi < 2)
1426 ix = hwi;
1427 break;
1429 case INTEGER_TYPE:
1430 case OFFSET_TYPE:
1431 if (TYPE_SIGN (type) == UNSIGNED)
1433 /* Cache [0, N). */
1434 limit = INTEGER_SHARE_LIMIT;
1435 if (IN_RANGE (hwi, 0, INTEGER_SHARE_LIMIT - 1))
1436 ix = hwi;
1438 else
1440 /* Cache [-1, N). */
1441 limit = INTEGER_SHARE_LIMIT + 1;
1442 if (IN_RANGE (hwi, -1, INTEGER_SHARE_LIMIT - 1))
1443 ix = hwi + 1;
1445 break;
1447 case ENUMERAL_TYPE:
1448 break;
1450 default:
1451 gcc_unreachable ();
1454 if (ix >= 0)
1456 /* Look for it in the type's vector of small shared ints. */
1457 if (!TYPE_CACHED_VALUES_P (type))
1459 TYPE_CACHED_VALUES_P (type) = 1;
1460 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1463 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1464 if (t)
1465 /* Make sure no one is clobbering the shared constant. */
1466 gcc_checking_assert (TREE_TYPE (t) == type
1467 && TREE_INT_CST_NUNITS (t) == 1
1468 && TREE_INT_CST_OFFSET_NUNITS (t) == 1
1469 && TREE_INT_CST_EXT_NUNITS (t) == 1
1470 && TREE_INT_CST_ELT (t, 0) == hwi);
1471 else
1473 /* Create a new shared int. */
1474 t = build_new_int_cst (type, cst);
1475 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1478 else
1480 /* Use the cache of larger shared ints, using int_cst_node as
1481 a temporary. */
1483 TREE_INT_CST_ELT (int_cst_node, 0) = hwi;
1484 TREE_TYPE (int_cst_node) = type;
1486 tree *slot = int_cst_hash_table->find_slot (int_cst_node, INSERT);
1487 t = *slot;
1488 if (!t)
1490 /* Insert this one into the hash table. */
1491 t = int_cst_node;
1492 *slot = t;
1493 /* Make a new node for next time round. */
1494 int_cst_node = make_int_cst (1, 1);
1498 else
1500 /* The value either hashes properly or we drop it on the floor
1501 for the gc to take care of. There will not be enough of them
1502 to worry about. */
1504 tree nt = build_new_int_cst (type, cst);
1505 tree *slot = int_cst_hash_table->find_slot (nt, INSERT);
1506 t = *slot;
1507 if (!t)
1509 /* Insert this one into the hash table. */
1510 t = nt;
1511 *slot = t;
1515 return t;
1518 void
1519 cache_integer_cst (tree t)
1521 tree type = TREE_TYPE (t);
1522 int ix = -1;
1523 int limit = 0;
1524 int prec = TYPE_PRECISION (type);
1526 gcc_assert (!TREE_OVERFLOW (t));
1528 switch (TREE_CODE (type))
1530 case NULLPTR_TYPE:
1531 gcc_assert (integer_zerop (t));
1532 /* Fallthru. */
1534 case POINTER_TYPE:
1535 case REFERENCE_TYPE:
1536 /* Cache NULL pointer. */
1537 if (integer_zerop (t))
1539 limit = 1;
1540 ix = 0;
1542 break;
1544 case BOOLEAN_TYPE:
1545 /* Cache false or true. */
1546 limit = 2;
1547 if (wi::ltu_p (t, 2))
1548 ix = TREE_INT_CST_ELT (t, 0);
1549 break;
1551 case INTEGER_TYPE:
1552 case OFFSET_TYPE:
1553 if (TYPE_UNSIGNED (type))
1555 /* Cache 0..N */
1556 limit = INTEGER_SHARE_LIMIT;
1558 /* This is a little hokie, but if the prec is smaller than
1559 what is necessary to hold INTEGER_SHARE_LIMIT, then the
1560 obvious test will not get the correct answer. */
1561 if (prec < HOST_BITS_PER_WIDE_INT)
1563 if (tree_to_uhwi (t) < (unsigned HOST_WIDE_INT) INTEGER_SHARE_LIMIT)
1564 ix = tree_to_uhwi (t);
1566 else if (wi::ltu_p (t, INTEGER_SHARE_LIMIT))
1567 ix = tree_to_uhwi (t);
1569 else
1571 /* Cache -1..N */
1572 limit = INTEGER_SHARE_LIMIT + 1;
1574 if (integer_minus_onep (t))
1575 ix = 0;
1576 else if (!wi::neg_p (t))
1578 if (prec < HOST_BITS_PER_WIDE_INT)
1580 if (tree_to_shwi (t) < INTEGER_SHARE_LIMIT)
1581 ix = tree_to_shwi (t) + 1;
1583 else if (wi::ltu_p (t, INTEGER_SHARE_LIMIT))
1584 ix = tree_to_shwi (t) + 1;
1587 break;
1589 case ENUMERAL_TYPE:
1590 break;
1592 default:
1593 gcc_unreachable ();
1596 if (ix >= 0)
1598 /* Look for it in the type's vector of small shared ints. */
1599 if (!TYPE_CACHED_VALUES_P (type))
1601 TYPE_CACHED_VALUES_P (type) = 1;
1602 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1605 gcc_assert (TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) == NULL_TREE);
1606 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1608 else
1610 /* Use the cache of larger shared ints. */
1611 tree *slot = int_cst_hash_table->find_slot (t, INSERT);
1612 /* If there is already an entry for the number verify it's the
1613 same. */
1614 if (*slot)
1615 gcc_assert (wi::eq_p (tree (*slot), t));
1616 else
1617 /* Otherwise insert this one into the hash table. */
1618 *slot = t;
1623 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1624 and the rest are zeros. */
1626 tree
1627 build_low_bits_mask (tree type, unsigned bits)
1629 gcc_assert (bits <= TYPE_PRECISION (type));
1631 return wide_int_to_tree (type, wi::mask (bits, false,
1632 TYPE_PRECISION (type)));
1635 /* Checks that X is integer constant that can be expressed in (unsigned)
1636 HOST_WIDE_INT without loss of precision. */
1638 bool
1639 cst_and_fits_in_hwi (const_tree x)
1641 if (TREE_CODE (x) != INTEGER_CST)
1642 return false;
1644 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1645 return false;
1647 return TREE_INT_CST_NUNITS (x) == 1;
1650 /* Build a newly constructed TREE_VEC node of length LEN. */
1652 tree
1653 make_vector_stat (unsigned len MEM_STAT_DECL)
1655 tree t;
1656 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1658 record_node_allocation_statistics (VECTOR_CST, length);
1660 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1662 TREE_SET_CODE (t, VECTOR_CST);
1663 TREE_CONSTANT (t) = 1;
1665 return t;
1668 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1669 are in a list pointed to by VALS. */
1671 tree
1672 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1674 int over = 0;
1675 unsigned cnt = 0;
1676 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1677 TREE_TYPE (v) = type;
1679 /* Iterate through elements and check for overflow. */
1680 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1682 tree value = vals[cnt];
1684 VECTOR_CST_ELT (v, cnt) = value;
1686 /* Don't crash if we get an address constant. */
1687 if (!CONSTANT_CLASS_P (value))
1688 continue;
1690 over |= TREE_OVERFLOW (value);
1693 TREE_OVERFLOW (v) = over;
1694 return v;
1697 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1698 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1700 tree
1701 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1703 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1704 unsigned HOST_WIDE_INT idx;
1705 tree value;
1707 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1708 vec[idx] = value;
1709 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1710 vec[idx] = build_zero_cst (TREE_TYPE (type));
1712 return build_vector (type, vec);
1715 /* Build a vector of type VECTYPE where all the elements are SCs. */
1716 tree
1717 build_vector_from_val (tree vectype, tree sc)
1719 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1721 if (sc == error_mark_node)
1722 return sc;
1724 /* Verify that the vector type is suitable for SC. Note that there
1725 is some inconsistency in the type-system with respect to restrict
1726 qualifications of pointers. Vector types always have a main-variant
1727 element type and the qualification is applied to the vector-type.
1728 So TREE_TYPE (vector-type) does not return a properly qualified
1729 vector element-type. */
1730 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1731 TREE_TYPE (vectype)));
1733 if (CONSTANT_CLASS_P (sc))
1735 tree *v = XALLOCAVEC (tree, nunits);
1736 for (i = 0; i < nunits; ++i)
1737 v[i] = sc;
1738 return build_vector (vectype, v);
1740 else
1742 vec<constructor_elt, va_gc> *v;
1743 vec_alloc (v, nunits);
1744 for (i = 0; i < nunits; ++i)
1745 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1746 return build_constructor (vectype, v);
1750 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1751 are in the vec pointed to by VALS. */
1752 tree
1753 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1755 tree c = make_node (CONSTRUCTOR);
1756 unsigned int i;
1757 constructor_elt *elt;
1758 bool constant_p = true;
1759 bool side_effects_p = false;
1761 TREE_TYPE (c) = type;
1762 CONSTRUCTOR_ELTS (c) = vals;
1764 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1766 /* Mostly ctors will have elts that don't have side-effects, so
1767 the usual case is to scan all the elements. Hence a single
1768 loop for both const and side effects, rather than one loop
1769 each (with early outs). */
1770 if (!TREE_CONSTANT (elt->value))
1771 constant_p = false;
1772 if (TREE_SIDE_EFFECTS (elt->value))
1773 side_effects_p = true;
1776 TREE_SIDE_EFFECTS (c) = side_effects_p;
1777 TREE_CONSTANT (c) = constant_p;
1779 return c;
1782 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1783 INDEX and VALUE. */
1784 tree
1785 build_constructor_single (tree type, tree index, tree value)
1787 vec<constructor_elt, va_gc> *v;
1788 constructor_elt elt = {index, value};
1790 vec_alloc (v, 1);
1791 v->quick_push (elt);
1793 return build_constructor (type, v);
1797 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1798 are in a list pointed to by VALS. */
1799 tree
1800 build_constructor_from_list (tree type, tree vals)
1802 tree t;
1803 vec<constructor_elt, va_gc> *v = NULL;
1805 if (vals)
1807 vec_alloc (v, list_length (vals));
1808 for (t = vals; t; t = TREE_CHAIN (t))
1809 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1812 return build_constructor (type, v);
1815 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
1816 of elements, provided as index/value pairs. */
1818 tree
1819 build_constructor_va (tree type, int nelts, ...)
1821 vec<constructor_elt, va_gc> *v = NULL;
1822 va_list p;
1824 va_start (p, nelts);
1825 vec_alloc (v, nelts);
1826 while (nelts--)
1828 tree index = va_arg (p, tree);
1829 tree value = va_arg (p, tree);
1830 CONSTRUCTOR_APPEND_ELT (v, index, value);
1832 va_end (p);
1833 return build_constructor (type, v);
1836 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1838 tree
1839 build_fixed (tree type, FIXED_VALUE_TYPE f)
1841 tree v;
1842 FIXED_VALUE_TYPE *fp;
1844 v = make_node (FIXED_CST);
1845 fp = ggc_alloc<fixed_value> ();
1846 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1848 TREE_TYPE (v) = type;
1849 TREE_FIXED_CST_PTR (v) = fp;
1850 return v;
1853 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1855 tree
1856 build_real (tree type, REAL_VALUE_TYPE d)
1858 tree v;
1859 REAL_VALUE_TYPE *dp;
1860 int overflow = 0;
1862 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1863 Consider doing it via real_convert now. */
1865 v = make_node (REAL_CST);
1866 dp = ggc_alloc<real_value> ();
1867 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1869 TREE_TYPE (v) = type;
1870 TREE_REAL_CST_PTR (v) = dp;
1871 TREE_OVERFLOW (v) = overflow;
1872 return v;
1875 /* Return a new REAL_CST node whose type is TYPE
1876 and whose value is the integer value of the INTEGER_CST node I. */
1878 REAL_VALUE_TYPE
1879 real_value_from_int_cst (const_tree type, const_tree i)
1881 REAL_VALUE_TYPE d;
1883 /* Clear all bits of the real value type so that we can later do
1884 bitwise comparisons to see if two values are the same. */
1885 memset (&d, 0, sizeof d);
1887 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode, i,
1888 TYPE_SIGN (TREE_TYPE (i)));
1889 return d;
1892 /* Given a tree representing an integer constant I, return a tree
1893 representing the same value as a floating-point constant of type TYPE. */
1895 tree
1896 build_real_from_int_cst (tree type, const_tree i)
1898 tree v;
1899 int overflow = TREE_OVERFLOW (i);
1901 v = build_real (type, real_value_from_int_cst (type, i));
1903 TREE_OVERFLOW (v) |= overflow;
1904 return v;
1907 /* Return a newly constructed STRING_CST node whose value is
1908 the LEN characters at STR.
1909 Note that for a C string literal, LEN should include the trailing NUL.
1910 The TREE_TYPE is not initialized. */
1912 tree
1913 build_string (int len, const char *str)
1915 tree s;
1916 size_t length;
1918 /* Do not waste bytes provided by padding of struct tree_string. */
1919 length = len + offsetof (struct tree_string, str) + 1;
1921 record_node_allocation_statistics (STRING_CST, length);
1923 s = (tree) ggc_internal_alloc (length);
1925 memset (s, 0, sizeof (struct tree_typed));
1926 TREE_SET_CODE (s, STRING_CST);
1927 TREE_CONSTANT (s) = 1;
1928 TREE_STRING_LENGTH (s) = len;
1929 memcpy (s->string.str, str, len);
1930 s->string.str[len] = '\0';
1932 return s;
1935 /* Return a newly constructed COMPLEX_CST node whose value is
1936 specified by the real and imaginary parts REAL and IMAG.
1937 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1938 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1940 tree
1941 build_complex (tree type, tree real, tree imag)
1943 tree t = make_node (COMPLEX_CST);
1945 TREE_REALPART (t) = real;
1946 TREE_IMAGPART (t) = imag;
1947 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1948 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1949 return t;
1952 /* Return a constant of arithmetic type TYPE which is the
1953 multiplicative identity of the set TYPE. */
1955 tree
1956 build_one_cst (tree type)
1958 switch (TREE_CODE (type))
1960 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1961 case POINTER_TYPE: case REFERENCE_TYPE:
1962 case OFFSET_TYPE:
1963 return build_int_cst (type, 1);
1965 case REAL_TYPE:
1966 return build_real (type, dconst1);
1968 case FIXED_POINT_TYPE:
1969 /* We can only generate 1 for accum types. */
1970 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1971 return build_fixed (type, FCONST1 (TYPE_MODE (type)));
1973 case VECTOR_TYPE:
1975 tree scalar = build_one_cst (TREE_TYPE (type));
1977 return build_vector_from_val (type, scalar);
1980 case COMPLEX_TYPE:
1981 return build_complex (type,
1982 build_one_cst (TREE_TYPE (type)),
1983 build_zero_cst (TREE_TYPE (type)));
1985 default:
1986 gcc_unreachable ();
1990 /* Return an integer of type TYPE containing all 1's in as much precision as
1991 it contains, or a complex or vector whose subparts are such integers. */
1993 tree
1994 build_all_ones_cst (tree type)
1996 if (TREE_CODE (type) == COMPLEX_TYPE)
1998 tree scalar = build_all_ones_cst (TREE_TYPE (type));
1999 return build_complex (type, scalar, scalar);
2001 else
2002 return build_minus_one_cst (type);
2005 /* Return a constant of arithmetic type TYPE which is the
2006 opposite of the multiplicative identity of the set TYPE. */
2008 tree
2009 build_minus_one_cst (tree type)
2011 switch (TREE_CODE (type))
2013 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2014 case POINTER_TYPE: case REFERENCE_TYPE:
2015 case OFFSET_TYPE:
2016 return build_int_cst (type, -1);
2018 case REAL_TYPE:
2019 return build_real (type, dconstm1);
2021 case FIXED_POINT_TYPE:
2022 /* We can only generate 1 for accum types. */
2023 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2024 return build_fixed (type, fixed_from_double_int (double_int_minus_one,
2025 TYPE_MODE (type)));
2027 case VECTOR_TYPE:
2029 tree scalar = build_minus_one_cst (TREE_TYPE (type));
2031 return build_vector_from_val (type, scalar);
2034 case COMPLEX_TYPE:
2035 return build_complex (type,
2036 build_minus_one_cst (TREE_TYPE (type)),
2037 build_zero_cst (TREE_TYPE (type)));
2039 default:
2040 gcc_unreachable ();
2044 /* Build 0 constant of type TYPE. This is used by constructor folding
2045 and thus the constant should be represented in memory by
2046 zero(es). */
2048 tree
2049 build_zero_cst (tree type)
2051 switch (TREE_CODE (type))
2053 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2054 case POINTER_TYPE: case REFERENCE_TYPE:
2055 case OFFSET_TYPE: case NULLPTR_TYPE:
2056 return build_int_cst (type, 0);
2058 case REAL_TYPE:
2059 return build_real (type, dconst0);
2061 case FIXED_POINT_TYPE:
2062 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
2064 case VECTOR_TYPE:
2066 tree scalar = build_zero_cst (TREE_TYPE (type));
2068 return build_vector_from_val (type, scalar);
2071 case COMPLEX_TYPE:
2073 tree zero = build_zero_cst (TREE_TYPE (type));
2075 return build_complex (type, zero, zero);
2078 default:
2079 if (!AGGREGATE_TYPE_P (type))
2080 return fold_convert (type, integer_zero_node);
2081 return build_constructor (type, NULL);
2086 /* Build a BINFO with LEN language slots. */
2088 tree
2089 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
2091 tree t;
2092 size_t length = (offsetof (struct tree_binfo, base_binfos)
2093 + vec<tree, va_gc>::embedded_size (base_binfos));
2095 record_node_allocation_statistics (TREE_BINFO, length);
2097 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
2099 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
2101 TREE_SET_CODE (t, TREE_BINFO);
2103 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
2105 return t;
2108 /* Create a CASE_LABEL_EXPR tree node and return it. */
2110 tree
2111 build_case_label (tree low_value, tree high_value, tree label_decl)
2113 tree t = make_node (CASE_LABEL_EXPR);
2115 TREE_TYPE (t) = void_type_node;
2116 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
2118 CASE_LOW (t) = low_value;
2119 CASE_HIGH (t) = high_value;
2120 CASE_LABEL (t) = label_decl;
2121 CASE_CHAIN (t) = NULL_TREE;
2123 return t;
2126 /* Build a newly constructed INTEGER_CST node. LEN and EXT_LEN are the
2127 values of TREE_INT_CST_NUNITS and TREE_INT_CST_EXT_NUNITS respectively.
2128 The latter determines the length of the HOST_WIDE_INT vector. */
2130 tree
2131 make_int_cst_stat (int len, int ext_len MEM_STAT_DECL)
2133 tree t;
2134 int length = ((ext_len - 1) * sizeof (HOST_WIDE_INT)
2135 + sizeof (struct tree_int_cst));
2137 gcc_assert (len);
2138 record_node_allocation_statistics (INTEGER_CST, length);
2140 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2142 TREE_SET_CODE (t, INTEGER_CST);
2143 TREE_INT_CST_NUNITS (t) = len;
2144 TREE_INT_CST_EXT_NUNITS (t) = ext_len;
2145 /* to_offset can only be applied to trees that are offset_int-sized
2146 or smaller. EXT_LEN is correct if it fits, otherwise the constant
2147 must be exactly the precision of offset_int and so LEN is correct. */
2148 if (ext_len <= OFFSET_INT_ELTS)
2149 TREE_INT_CST_OFFSET_NUNITS (t) = ext_len;
2150 else
2151 TREE_INT_CST_OFFSET_NUNITS (t) = len;
2153 TREE_CONSTANT (t) = 1;
2155 return t;
2158 /* Build a newly constructed TREE_VEC node of length LEN. */
2160 tree
2161 make_tree_vec_stat (int len MEM_STAT_DECL)
2163 tree t;
2164 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2166 record_node_allocation_statistics (TREE_VEC, length);
2168 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2170 TREE_SET_CODE (t, TREE_VEC);
2171 TREE_VEC_LENGTH (t) = len;
2173 return t;
2176 /* Grow a TREE_VEC node to new length LEN. */
2178 tree
2179 grow_tree_vec_stat (tree v, int len MEM_STAT_DECL)
2181 gcc_assert (TREE_CODE (v) == TREE_VEC);
2183 int oldlen = TREE_VEC_LENGTH (v);
2184 gcc_assert (len > oldlen);
2186 int oldlength = (oldlen - 1) * sizeof (tree) + sizeof (struct tree_vec);
2187 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2189 record_node_allocation_statistics (TREE_VEC, length - oldlength);
2191 v = (tree) ggc_realloc (v, length PASS_MEM_STAT);
2193 TREE_VEC_LENGTH (v) = len;
2195 return v;
2198 /* Return 1 if EXPR is the integer constant zero or a complex constant
2199 of zero. */
2202 integer_zerop (const_tree expr)
2204 STRIP_NOPS (expr);
2206 switch (TREE_CODE (expr))
2208 case INTEGER_CST:
2209 return wi::eq_p (expr, 0);
2210 case COMPLEX_CST:
2211 return (integer_zerop (TREE_REALPART (expr))
2212 && integer_zerop (TREE_IMAGPART (expr)));
2213 case VECTOR_CST:
2215 unsigned i;
2216 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2217 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
2218 return false;
2219 return true;
2221 default:
2222 return false;
2226 /* Return 1 if EXPR is the integer constant one or the corresponding
2227 complex constant. */
2230 integer_onep (const_tree expr)
2232 STRIP_NOPS (expr);
2234 switch (TREE_CODE (expr))
2236 case INTEGER_CST:
2237 return wi::eq_p (wi::to_widest (expr), 1);
2238 case COMPLEX_CST:
2239 return (integer_onep (TREE_REALPART (expr))
2240 && integer_zerop (TREE_IMAGPART (expr)));
2241 case VECTOR_CST:
2243 unsigned i;
2244 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2245 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
2246 return false;
2247 return true;
2249 default:
2250 return false;
2254 /* Return 1 if EXPR is the integer constant one. For complex and vector,
2255 return 1 if every piece is the integer constant one. */
2258 integer_each_onep (const_tree expr)
2260 STRIP_NOPS (expr);
2262 if (TREE_CODE (expr) == COMPLEX_CST)
2263 return (integer_onep (TREE_REALPART (expr))
2264 && integer_onep (TREE_IMAGPART (expr)));
2265 else
2266 return integer_onep (expr);
2269 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
2270 it contains, or a complex or vector whose subparts are such integers. */
2273 integer_all_onesp (const_tree expr)
2275 STRIP_NOPS (expr);
2277 if (TREE_CODE (expr) == COMPLEX_CST
2278 && integer_all_onesp (TREE_REALPART (expr))
2279 && integer_all_onesp (TREE_IMAGPART (expr)))
2280 return 1;
2282 else if (TREE_CODE (expr) == VECTOR_CST)
2284 unsigned i;
2285 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2286 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
2287 return 0;
2288 return 1;
2291 else if (TREE_CODE (expr) != INTEGER_CST)
2292 return 0;
2294 return wi::max_value (TYPE_PRECISION (TREE_TYPE (expr)), UNSIGNED) == expr;
2297 /* Return 1 if EXPR is the integer constant minus one. */
2300 integer_minus_onep (const_tree expr)
2302 STRIP_NOPS (expr);
2304 if (TREE_CODE (expr) == COMPLEX_CST)
2305 return (integer_all_onesp (TREE_REALPART (expr))
2306 && integer_zerop (TREE_IMAGPART (expr)));
2307 else
2308 return integer_all_onesp (expr);
2311 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
2312 one bit on). */
2315 integer_pow2p (const_tree expr)
2317 STRIP_NOPS (expr);
2319 if (TREE_CODE (expr) == COMPLEX_CST
2320 && integer_pow2p (TREE_REALPART (expr))
2321 && integer_zerop (TREE_IMAGPART (expr)))
2322 return 1;
2324 if (TREE_CODE (expr) != INTEGER_CST)
2325 return 0;
2327 return wi::popcount (expr) == 1;
2330 /* Return 1 if EXPR is an integer constant other than zero or a
2331 complex constant other than zero. */
2334 integer_nonzerop (const_tree expr)
2336 STRIP_NOPS (expr);
2338 return ((TREE_CODE (expr) == INTEGER_CST
2339 && !wi::eq_p (expr, 0))
2340 || (TREE_CODE (expr) == COMPLEX_CST
2341 && (integer_nonzerop (TREE_REALPART (expr))
2342 || integer_nonzerop (TREE_IMAGPART (expr)))));
2345 /* Return 1 if EXPR is the integer constant one. For vector,
2346 return 1 if every piece is the integer constant minus one
2347 (representing the value TRUE). */
2350 integer_truep (const_tree expr)
2352 STRIP_NOPS (expr);
2354 if (TREE_CODE (expr) == VECTOR_CST)
2355 return integer_all_onesp (expr);
2356 return integer_onep (expr);
2359 /* Return 1 if EXPR is the fixed-point constant zero. */
2362 fixed_zerop (const_tree expr)
2364 return (TREE_CODE (expr) == FIXED_CST
2365 && TREE_FIXED_CST (expr).data.is_zero ());
2368 /* Return the power of two represented by a tree node known to be a
2369 power of two. */
2372 tree_log2 (const_tree expr)
2374 STRIP_NOPS (expr);
2376 if (TREE_CODE (expr) == COMPLEX_CST)
2377 return tree_log2 (TREE_REALPART (expr));
2379 return wi::exact_log2 (expr);
2382 /* Similar, but return the largest integer Y such that 2 ** Y is less
2383 than or equal to EXPR. */
2386 tree_floor_log2 (const_tree expr)
2388 STRIP_NOPS (expr);
2390 if (TREE_CODE (expr) == COMPLEX_CST)
2391 return tree_log2 (TREE_REALPART (expr));
2393 return wi::floor_log2 (expr);
2396 /* Return number of known trailing zero bits in EXPR, or, if the value of
2397 EXPR is known to be zero, the precision of it's type. */
2399 unsigned int
2400 tree_ctz (const_tree expr)
2402 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
2403 && !POINTER_TYPE_P (TREE_TYPE (expr)))
2404 return 0;
2406 unsigned int ret1, ret2, prec = TYPE_PRECISION (TREE_TYPE (expr));
2407 switch (TREE_CODE (expr))
2409 case INTEGER_CST:
2410 ret1 = wi::ctz (expr);
2411 return MIN (ret1, prec);
2412 case SSA_NAME:
2413 ret1 = wi::ctz (get_nonzero_bits (expr));
2414 return MIN (ret1, prec);
2415 case PLUS_EXPR:
2416 case MINUS_EXPR:
2417 case BIT_IOR_EXPR:
2418 case BIT_XOR_EXPR:
2419 case MIN_EXPR:
2420 case MAX_EXPR:
2421 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2422 if (ret1 == 0)
2423 return ret1;
2424 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2425 return MIN (ret1, ret2);
2426 case POINTER_PLUS_EXPR:
2427 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2428 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2429 /* Second operand is sizetype, which could be in theory
2430 wider than pointer's precision. Make sure we never
2431 return more than prec. */
2432 ret2 = MIN (ret2, prec);
2433 return MIN (ret1, ret2);
2434 case BIT_AND_EXPR:
2435 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2436 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2437 return MAX (ret1, ret2);
2438 case MULT_EXPR:
2439 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2440 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2441 return MIN (ret1 + ret2, prec);
2442 case LSHIFT_EXPR:
2443 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2444 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2445 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2447 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2448 return MIN (ret1 + ret2, prec);
2450 return ret1;
2451 case RSHIFT_EXPR:
2452 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2453 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2455 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2456 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2457 if (ret1 > ret2)
2458 return ret1 - ret2;
2460 return 0;
2461 case TRUNC_DIV_EXPR:
2462 case CEIL_DIV_EXPR:
2463 case FLOOR_DIV_EXPR:
2464 case ROUND_DIV_EXPR:
2465 case EXACT_DIV_EXPR:
2466 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
2467 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) == 1)
2469 int l = tree_log2 (TREE_OPERAND (expr, 1));
2470 if (l >= 0)
2472 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2473 ret2 = l;
2474 if (ret1 > ret2)
2475 return ret1 - ret2;
2478 return 0;
2479 CASE_CONVERT:
2480 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2481 if (ret1 && ret1 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2482 ret1 = prec;
2483 return MIN (ret1, prec);
2484 case SAVE_EXPR:
2485 return tree_ctz (TREE_OPERAND (expr, 0));
2486 case COND_EXPR:
2487 ret1 = tree_ctz (TREE_OPERAND (expr, 1));
2488 if (ret1 == 0)
2489 return 0;
2490 ret2 = tree_ctz (TREE_OPERAND (expr, 2));
2491 return MIN (ret1, ret2);
2492 case COMPOUND_EXPR:
2493 return tree_ctz (TREE_OPERAND (expr, 1));
2494 case ADDR_EXPR:
2495 ret1 = get_pointer_alignment (CONST_CAST_TREE (expr));
2496 if (ret1 > BITS_PER_UNIT)
2498 ret1 = ctz_hwi (ret1 / BITS_PER_UNIT);
2499 return MIN (ret1, prec);
2501 return 0;
2502 default:
2503 return 0;
2507 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
2508 decimal float constants, so don't return 1 for them. */
2511 real_zerop (const_tree expr)
2513 STRIP_NOPS (expr);
2515 switch (TREE_CODE (expr))
2517 case REAL_CST:
2518 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
2519 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2520 case COMPLEX_CST:
2521 return real_zerop (TREE_REALPART (expr))
2522 && real_zerop (TREE_IMAGPART (expr));
2523 case VECTOR_CST:
2525 unsigned i;
2526 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2527 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2528 return false;
2529 return true;
2531 default:
2532 return false;
2536 /* Return 1 if EXPR is the real constant one in real or complex form.
2537 Trailing zeroes matter for decimal float constants, so don't return
2538 1 for them. */
2541 real_onep (const_tree expr)
2543 STRIP_NOPS (expr);
2545 switch (TREE_CODE (expr))
2547 case REAL_CST:
2548 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
2549 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2550 case COMPLEX_CST:
2551 return real_onep (TREE_REALPART (expr))
2552 && real_zerop (TREE_IMAGPART (expr));
2553 case VECTOR_CST:
2555 unsigned i;
2556 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2557 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2558 return false;
2559 return true;
2561 default:
2562 return false;
2566 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2567 matter for decimal float constants, so don't return 1 for them. */
2570 real_minus_onep (const_tree expr)
2572 STRIP_NOPS (expr);
2574 switch (TREE_CODE (expr))
2576 case REAL_CST:
2577 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2578 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2579 case COMPLEX_CST:
2580 return real_minus_onep (TREE_REALPART (expr))
2581 && real_zerop (TREE_IMAGPART (expr));
2582 case VECTOR_CST:
2584 unsigned i;
2585 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2586 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2587 return false;
2588 return true;
2590 default:
2591 return false;
2595 /* Nonzero if EXP is a constant or a cast of a constant. */
2598 really_constant_p (const_tree exp)
2600 /* This is not quite the same as STRIP_NOPS. It does more. */
2601 while (CONVERT_EXPR_P (exp)
2602 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2603 exp = TREE_OPERAND (exp, 0);
2604 return TREE_CONSTANT (exp);
2607 /* Return first list element whose TREE_VALUE is ELEM.
2608 Return 0 if ELEM is not in LIST. */
2610 tree
2611 value_member (tree elem, tree list)
2613 while (list)
2615 if (elem == TREE_VALUE (list))
2616 return list;
2617 list = TREE_CHAIN (list);
2619 return NULL_TREE;
2622 /* Return first list element whose TREE_PURPOSE is ELEM.
2623 Return 0 if ELEM is not in LIST. */
2625 tree
2626 purpose_member (const_tree elem, tree list)
2628 while (list)
2630 if (elem == TREE_PURPOSE (list))
2631 return list;
2632 list = TREE_CHAIN (list);
2634 return NULL_TREE;
2637 /* Return true if ELEM is in V. */
2639 bool
2640 vec_member (const_tree elem, vec<tree, va_gc> *v)
2642 unsigned ix;
2643 tree t;
2644 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2645 if (elem == t)
2646 return true;
2647 return false;
2650 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2651 NULL_TREE. */
2653 tree
2654 chain_index (int idx, tree chain)
2656 for (; chain && idx > 0; --idx)
2657 chain = TREE_CHAIN (chain);
2658 return chain;
2661 /* Return nonzero if ELEM is part of the chain CHAIN. */
2664 chain_member (const_tree elem, const_tree chain)
2666 while (chain)
2668 if (elem == chain)
2669 return 1;
2670 chain = DECL_CHAIN (chain);
2673 return 0;
2676 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2677 We expect a null pointer to mark the end of the chain.
2678 This is the Lisp primitive `length'. */
2681 list_length (const_tree t)
2683 const_tree p = t;
2684 #ifdef ENABLE_TREE_CHECKING
2685 const_tree q = t;
2686 #endif
2687 int len = 0;
2689 while (p)
2691 p = TREE_CHAIN (p);
2692 #ifdef ENABLE_TREE_CHECKING
2693 if (len % 2)
2694 q = TREE_CHAIN (q);
2695 gcc_assert (p != q);
2696 #endif
2697 len++;
2700 return len;
2703 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2704 UNION_TYPE TYPE, or NULL_TREE if none. */
2706 tree
2707 first_field (const_tree type)
2709 tree t = TYPE_FIELDS (type);
2710 while (t && TREE_CODE (t) != FIELD_DECL)
2711 t = TREE_CHAIN (t);
2712 return t;
2715 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2716 by modifying the last node in chain 1 to point to chain 2.
2717 This is the Lisp primitive `nconc'. */
2719 tree
2720 chainon (tree op1, tree op2)
2722 tree t1;
2724 if (!op1)
2725 return op2;
2726 if (!op2)
2727 return op1;
2729 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2730 continue;
2731 TREE_CHAIN (t1) = op2;
2733 #ifdef ENABLE_TREE_CHECKING
2735 tree t2;
2736 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2737 gcc_assert (t2 != t1);
2739 #endif
2741 return op1;
2744 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2746 tree
2747 tree_last (tree chain)
2749 tree next;
2750 if (chain)
2751 while ((next = TREE_CHAIN (chain)))
2752 chain = next;
2753 return chain;
2756 /* Reverse the order of elements in the chain T,
2757 and return the new head of the chain (old last element). */
2759 tree
2760 nreverse (tree t)
2762 tree prev = 0, decl, next;
2763 for (decl = t; decl; decl = next)
2765 /* We shouldn't be using this function to reverse BLOCK chains; we
2766 have blocks_nreverse for that. */
2767 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2768 next = TREE_CHAIN (decl);
2769 TREE_CHAIN (decl) = prev;
2770 prev = decl;
2772 return prev;
2775 /* Return a newly created TREE_LIST node whose
2776 purpose and value fields are PARM and VALUE. */
2778 tree
2779 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2781 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2782 TREE_PURPOSE (t) = parm;
2783 TREE_VALUE (t) = value;
2784 return t;
2787 /* Build a chain of TREE_LIST nodes from a vector. */
2789 tree
2790 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2792 tree ret = NULL_TREE;
2793 tree *pp = &ret;
2794 unsigned int i;
2795 tree t;
2796 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2798 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2799 pp = &TREE_CHAIN (*pp);
2801 return ret;
2804 /* Return a newly created TREE_LIST node whose
2805 purpose and value fields are PURPOSE and VALUE
2806 and whose TREE_CHAIN is CHAIN. */
2808 tree
2809 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2811 tree node;
2813 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
2814 memset (node, 0, sizeof (struct tree_common));
2816 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2818 TREE_SET_CODE (node, TREE_LIST);
2819 TREE_CHAIN (node) = chain;
2820 TREE_PURPOSE (node) = purpose;
2821 TREE_VALUE (node) = value;
2822 return node;
2825 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2826 trees. */
2828 vec<tree, va_gc> *
2829 ctor_to_vec (tree ctor)
2831 vec<tree, va_gc> *vec;
2832 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2833 unsigned int ix;
2834 tree val;
2836 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2837 vec->quick_push (val);
2839 return vec;
2842 /* Return the size nominally occupied by an object of type TYPE
2843 when it resides in memory. The value is measured in units of bytes,
2844 and its data type is that normally used for type sizes
2845 (which is the first type created by make_signed_type or
2846 make_unsigned_type). */
2848 tree
2849 size_in_bytes (const_tree type)
2851 tree t;
2853 if (type == error_mark_node)
2854 return integer_zero_node;
2856 type = TYPE_MAIN_VARIANT (type);
2857 t = TYPE_SIZE_UNIT (type);
2859 if (t == 0)
2861 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2862 return size_zero_node;
2865 return t;
2868 /* Return the size of TYPE (in bytes) as a wide integer
2869 or return -1 if the size can vary or is larger than an integer. */
2871 HOST_WIDE_INT
2872 int_size_in_bytes (const_tree type)
2874 tree t;
2876 if (type == error_mark_node)
2877 return 0;
2879 type = TYPE_MAIN_VARIANT (type);
2880 t = TYPE_SIZE_UNIT (type);
2882 if (t && tree_fits_uhwi_p (t))
2883 return TREE_INT_CST_LOW (t);
2884 else
2885 return -1;
2888 /* Return the maximum size of TYPE (in bytes) as a wide integer
2889 or return -1 if the size can vary or is larger than an integer. */
2891 HOST_WIDE_INT
2892 max_int_size_in_bytes (const_tree type)
2894 HOST_WIDE_INT size = -1;
2895 tree size_tree;
2897 /* If this is an array type, check for a possible MAX_SIZE attached. */
2899 if (TREE_CODE (type) == ARRAY_TYPE)
2901 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2903 if (size_tree && tree_fits_uhwi_p (size_tree))
2904 size = tree_to_uhwi (size_tree);
2907 /* If we still haven't been able to get a size, see if the language
2908 can compute a maximum size. */
2910 if (size == -1)
2912 size_tree = lang_hooks.types.max_size (type);
2914 if (size_tree && tree_fits_uhwi_p (size_tree))
2915 size = tree_to_uhwi (size_tree);
2918 return size;
2921 /* Return the bit position of FIELD, in bits from the start of the record.
2922 This is a tree of type bitsizetype. */
2924 tree
2925 bit_position (const_tree field)
2927 return bit_from_pos (DECL_FIELD_OFFSET (field),
2928 DECL_FIELD_BIT_OFFSET (field));
2931 /* Return the byte position of FIELD, in bytes from the start of the record.
2932 This is a tree of type sizetype. */
2934 tree
2935 byte_position (const_tree field)
2937 return byte_from_pos (DECL_FIELD_OFFSET (field),
2938 DECL_FIELD_BIT_OFFSET (field));
2941 /* Likewise, but return as an integer. It must be representable in
2942 that way (since it could be a signed value, we don't have the
2943 option of returning -1 like int_size_in_byte can. */
2945 HOST_WIDE_INT
2946 int_byte_position (const_tree field)
2948 return tree_to_shwi (byte_position (field));
2951 /* Return the strictest alignment, in bits, that T is known to have. */
2953 unsigned int
2954 expr_align (const_tree t)
2956 unsigned int align0, align1;
2958 switch (TREE_CODE (t))
2960 CASE_CONVERT: case NON_LVALUE_EXPR:
2961 /* If we have conversions, we know that the alignment of the
2962 object must meet each of the alignments of the types. */
2963 align0 = expr_align (TREE_OPERAND (t, 0));
2964 align1 = TYPE_ALIGN (TREE_TYPE (t));
2965 return MAX (align0, align1);
2967 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2968 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2969 case CLEANUP_POINT_EXPR:
2970 /* These don't change the alignment of an object. */
2971 return expr_align (TREE_OPERAND (t, 0));
2973 case COND_EXPR:
2974 /* The best we can do is say that the alignment is the least aligned
2975 of the two arms. */
2976 align0 = expr_align (TREE_OPERAND (t, 1));
2977 align1 = expr_align (TREE_OPERAND (t, 2));
2978 return MIN (align0, align1);
2980 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2981 meaningfully, it's always 1. */
2982 case LABEL_DECL: case CONST_DECL:
2983 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2984 case FUNCTION_DECL:
2985 gcc_assert (DECL_ALIGN (t) != 0);
2986 return DECL_ALIGN (t);
2988 default:
2989 break;
2992 /* Otherwise take the alignment from that of the type. */
2993 return TYPE_ALIGN (TREE_TYPE (t));
2996 /* Return, as a tree node, the number of elements for TYPE (which is an
2997 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2999 tree
3000 array_type_nelts (const_tree type)
3002 tree index_type, min, max;
3004 /* If they did it with unspecified bounds, then we should have already
3005 given an error about it before we got here. */
3006 if (! TYPE_DOMAIN (type))
3007 return error_mark_node;
3009 index_type = TYPE_DOMAIN (type);
3010 min = TYPE_MIN_VALUE (index_type);
3011 max = TYPE_MAX_VALUE (index_type);
3013 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
3014 if (!max)
3015 return error_mark_node;
3017 return (integer_zerop (min)
3018 ? max
3019 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
3022 /* If arg is static -- a reference to an object in static storage -- then
3023 return the object. This is not the same as the C meaning of `static'.
3024 If arg isn't static, return NULL. */
3026 tree
3027 staticp (tree arg)
3029 switch (TREE_CODE (arg))
3031 case FUNCTION_DECL:
3032 /* Nested functions are static, even though taking their address will
3033 involve a trampoline as we unnest the nested function and create
3034 the trampoline on the tree level. */
3035 return arg;
3037 case VAR_DECL:
3038 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3039 && ! DECL_THREAD_LOCAL_P (arg)
3040 && ! DECL_DLLIMPORT_P (arg)
3041 ? arg : NULL);
3043 case CONST_DECL:
3044 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3045 ? arg : NULL);
3047 case CONSTRUCTOR:
3048 return TREE_STATIC (arg) ? arg : NULL;
3050 case LABEL_DECL:
3051 case STRING_CST:
3052 return arg;
3054 case COMPONENT_REF:
3055 /* If the thing being referenced is not a field, then it is
3056 something language specific. */
3057 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
3059 /* If we are referencing a bitfield, we can't evaluate an
3060 ADDR_EXPR at compile time and so it isn't a constant. */
3061 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
3062 return NULL;
3064 return staticp (TREE_OPERAND (arg, 0));
3066 case BIT_FIELD_REF:
3067 return NULL;
3069 case INDIRECT_REF:
3070 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
3072 case ARRAY_REF:
3073 case ARRAY_RANGE_REF:
3074 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
3075 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
3076 return staticp (TREE_OPERAND (arg, 0));
3077 else
3078 return NULL;
3080 case COMPOUND_LITERAL_EXPR:
3081 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
3083 default:
3084 return NULL;
3091 /* Return whether OP is a DECL whose address is function-invariant. */
3093 bool
3094 decl_address_invariant_p (const_tree op)
3096 /* The conditions below are slightly less strict than the one in
3097 staticp. */
3099 switch (TREE_CODE (op))
3101 case PARM_DECL:
3102 case RESULT_DECL:
3103 case LABEL_DECL:
3104 case FUNCTION_DECL:
3105 return true;
3107 case VAR_DECL:
3108 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3109 || DECL_THREAD_LOCAL_P (op)
3110 || DECL_CONTEXT (op) == current_function_decl
3111 || decl_function_context (op) == current_function_decl)
3112 return true;
3113 break;
3115 case CONST_DECL:
3116 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3117 || decl_function_context (op) == current_function_decl)
3118 return true;
3119 break;
3121 default:
3122 break;
3125 return false;
3128 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
3130 bool
3131 decl_address_ip_invariant_p (const_tree op)
3133 /* The conditions below are slightly less strict than the one in
3134 staticp. */
3136 switch (TREE_CODE (op))
3138 case LABEL_DECL:
3139 case FUNCTION_DECL:
3140 case STRING_CST:
3141 return true;
3143 case VAR_DECL:
3144 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
3145 && !DECL_DLLIMPORT_P (op))
3146 || DECL_THREAD_LOCAL_P (op))
3147 return true;
3148 break;
3150 case CONST_DECL:
3151 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
3152 return true;
3153 break;
3155 default:
3156 break;
3159 return false;
3163 /* Return true if T is function-invariant (internal function, does
3164 not handle arithmetic; that's handled in skip_simple_arithmetic and
3165 tree_invariant_p). */
3167 static bool tree_invariant_p (tree t);
3169 static bool
3170 tree_invariant_p_1 (tree t)
3172 tree op;
3174 if (TREE_CONSTANT (t)
3175 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
3176 return true;
3178 switch (TREE_CODE (t))
3180 case SAVE_EXPR:
3181 return true;
3183 case ADDR_EXPR:
3184 op = TREE_OPERAND (t, 0);
3185 while (handled_component_p (op))
3187 switch (TREE_CODE (op))
3189 case ARRAY_REF:
3190 case ARRAY_RANGE_REF:
3191 if (!tree_invariant_p (TREE_OPERAND (op, 1))
3192 || TREE_OPERAND (op, 2) != NULL_TREE
3193 || TREE_OPERAND (op, 3) != NULL_TREE)
3194 return false;
3195 break;
3197 case COMPONENT_REF:
3198 if (TREE_OPERAND (op, 2) != NULL_TREE)
3199 return false;
3200 break;
3202 default:;
3204 op = TREE_OPERAND (op, 0);
3207 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
3209 default:
3210 break;
3213 return false;
3216 /* Return true if T is function-invariant. */
3218 static bool
3219 tree_invariant_p (tree t)
3221 tree inner = skip_simple_arithmetic (t);
3222 return tree_invariant_p_1 (inner);
3225 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
3226 Do this to any expression which may be used in more than one place,
3227 but must be evaluated only once.
3229 Normally, expand_expr would reevaluate the expression each time.
3230 Calling save_expr produces something that is evaluated and recorded
3231 the first time expand_expr is called on it. Subsequent calls to
3232 expand_expr just reuse the recorded value.
3234 The call to expand_expr that generates code that actually computes
3235 the value is the first call *at compile time*. Subsequent calls
3236 *at compile time* generate code to use the saved value.
3237 This produces correct result provided that *at run time* control
3238 always flows through the insns made by the first expand_expr
3239 before reaching the other places where the save_expr was evaluated.
3240 You, the caller of save_expr, must make sure this is so.
3242 Constants, and certain read-only nodes, are returned with no
3243 SAVE_EXPR because that is safe. Expressions containing placeholders
3244 are not touched; see tree.def for an explanation of what these
3245 are used for. */
3247 tree
3248 save_expr (tree expr)
3250 tree t = fold (expr);
3251 tree inner;
3253 /* If the tree evaluates to a constant, then we don't want to hide that
3254 fact (i.e. this allows further folding, and direct checks for constants).
3255 However, a read-only object that has side effects cannot be bypassed.
3256 Since it is no problem to reevaluate literals, we just return the
3257 literal node. */
3258 inner = skip_simple_arithmetic (t);
3259 if (TREE_CODE (inner) == ERROR_MARK)
3260 return inner;
3262 if (tree_invariant_p_1 (inner))
3263 return t;
3265 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
3266 it means that the size or offset of some field of an object depends on
3267 the value within another field.
3269 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
3270 and some variable since it would then need to be both evaluated once and
3271 evaluated more than once. Front-ends must assure this case cannot
3272 happen by surrounding any such subexpressions in their own SAVE_EXPR
3273 and forcing evaluation at the proper time. */
3274 if (contains_placeholder_p (inner))
3275 return t;
3277 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
3278 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
3280 /* This expression might be placed ahead of a jump to ensure that the
3281 value was computed on both sides of the jump. So make sure it isn't
3282 eliminated as dead. */
3283 TREE_SIDE_EFFECTS (t) = 1;
3284 return t;
3287 /* Look inside EXPR into any simple arithmetic operations. Return the
3288 outermost non-arithmetic or non-invariant node. */
3290 tree
3291 skip_simple_arithmetic (tree expr)
3293 /* We don't care about whether this can be used as an lvalue in this
3294 context. */
3295 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3296 expr = TREE_OPERAND (expr, 0);
3298 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
3299 a constant, it will be more efficient to not make another SAVE_EXPR since
3300 it will allow better simplification and GCSE will be able to merge the
3301 computations if they actually occur. */
3302 while (true)
3304 if (UNARY_CLASS_P (expr))
3305 expr = TREE_OPERAND (expr, 0);
3306 else if (BINARY_CLASS_P (expr))
3308 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
3309 expr = TREE_OPERAND (expr, 0);
3310 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
3311 expr = TREE_OPERAND (expr, 1);
3312 else
3313 break;
3315 else
3316 break;
3319 return expr;
3322 /* Look inside EXPR into simple arithmetic operations involving constants.
3323 Return the outermost non-arithmetic or non-constant node. */
3325 tree
3326 skip_simple_constant_arithmetic (tree expr)
3328 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3329 expr = TREE_OPERAND (expr, 0);
3331 while (true)
3333 if (UNARY_CLASS_P (expr))
3334 expr = TREE_OPERAND (expr, 0);
3335 else if (BINARY_CLASS_P (expr))
3337 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3338 expr = TREE_OPERAND (expr, 0);
3339 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3340 expr = TREE_OPERAND (expr, 1);
3341 else
3342 break;
3344 else
3345 break;
3348 return expr;
3351 /* Return which tree structure is used by T. */
3353 enum tree_node_structure_enum
3354 tree_node_structure (const_tree t)
3356 const enum tree_code code = TREE_CODE (t);
3357 return tree_node_structure_for_code (code);
3360 /* Set various status flags when building a CALL_EXPR object T. */
3362 static void
3363 process_call_operands (tree t)
3365 bool side_effects = TREE_SIDE_EFFECTS (t);
3366 bool read_only = false;
3367 int i = call_expr_flags (t);
3369 /* Calls have side-effects, except those to const or pure functions. */
3370 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
3371 side_effects = true;
3372 /* Propagate TREE_READONLY of arguments for const functions. */
3373 if (i & ECF_CONST)
3374 read_only = true;
3376 if (!side_effects || read_only)
3377 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
3379 tree op = TREE_OPERAND (t, i);
3380 if (op && TREE_SIDE_EFFECTS (op))
3381 side_effects = true;
3382 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
3383 read_only = false;
3386 TREE_SIDE_EFFECTS (t) = side_effects;
3387 TREE_READONLY (t) = read_only;
3390 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
3391 size or offset that depends on a field within a record. */
3393 bool
3394 contains_placeholder_p (const_tree exp)
3396 enum tree_code code;
3398 if (!exp)
3399 return 0;
3401 code = TREE_CODE (exp);
3402 if (code == PLACEHOLDER_EXPR)
3403 return 1;
3405 switch (TREE_CODE_CLASS (code))
3407 case tcc_reference:
3408 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
3409 position computations since they will be converted into a
3410 WITH_RECORD_EXPR involving the reference, which will assume
3411 here will be valid. */
3412 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3414 case tcc_exceptional:
3415 if (code == TREE_LIST)
3416 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
3417 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
3418 break;
3420 case tcc_unary:
3421 case tcc_binary:
3422 case tcc_comparison:
3423 case tcc_expression:
3424 switch (code)
3426 case COMPOUND_EXPR:
3427 /* Ignoring the first operand isn't quite right, but works best. */
3428 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
3430 case COND_EXPR:
3431 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3432 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
3433 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
3435 case SAVE_EXPR:
3436 /* The save_expr function never wraps anything containing
3437 a PLACEHOLDER_EXPR. */
3438 return 0;
3440 default:
3441 break;
3444 switch (TREE_CODE_LENGTH (code))
3446 case 1:
3447 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3448 case 2:
3449 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3450 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
3451 default:
3452 return 0;
3455 case tcc_vl_exp:
3456 switch (code)
3458 case CALL_EXPR:
3460 const_tree arg;
3461 const_call_expr_arg_iterator iter;
3462 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
3463 if (CONTAINS_PLACEHOLDER_P (arg))
3464 return 1;
3465 return 0;
3467 default:
3468 return 0;
3471 default:
3472 return 0;
3474 return 0;
3477 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3478 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3479 field positions. */
3481 static bool
3482 type_contains_placeholder_1 (const_tree type)
3484 /* If the size contains a placeholder or the parent type (component type in
3485 the case of arrays) type involves a placeholder, this type does. */
3486 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3487 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3488 || (!POINTER_TYPE_P (type)
3489 && TREE_TYPE (type)
3490 && type_contains_placeholder_p (TREE_TYPE (type))))
3491 return true;
3493 /* Now do type-specific checks. Note that the last part of the check above
3494 greatly limits what we have to do below. */
3495 switch (TREE_CODE (type))
3497 case VOID_TYPE:
3498 case POINTER_BOUNDS_TYPE:
3499 case COMPLEX_TYPE:
3500 case ENUMERAL_TYPE:
3501 case BOOLEAN_TYPE:
3502 case POINTER_TYPE:
3503 case OFFSET_TYPE:
3504 case REFERENCE_TYPE:
3505 case METHOD_TYPE:
3506 case FUNCTION_TYPE:
3507 case VECTOR_TYPE:
3508 case NULLPTR_TYPE:
3509 return false;
3511 case INTEGER_TYPE:
3512 case REAL_TYPE:
3513 case FIXED_POINT_TYPE:
3514 /* Here we just check the bounds. */
3515 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3516 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3518 case ARRAY_TYPE:
3519 /* We have already checked the component type above, so just check the
3520 domain type. */
3521 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3523 case RECORD_TYPE:
3524 case UNION_TYPE:
3525 case QUAL_UNION_TYPE:
3527 tree field;
3529 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3530 if (TREE_CODE (field) == FIELD_DECL
3531 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3532 || (TREE_CODE (type) == QUAL_UNION_TYPE
3533 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3534 || type_contains_placeholder_p (TREE_TYPE (field))))
3535 return true;
3537 return false;
3540 default:
3541 gcc_unreachable ();
3545 /* Wrapper around above function used to cache its result. */
3547 bool
3548 type_contains_placeholder_p (tree type)
3550 bool result;
3552 /* If the contains_placeholder_bits field has been initialized,
3553 then we know the answer. */
3554 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3555 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3557 /* Indicate that we've seen this type node, and the answer is false.
3558 This is what we want to return if we run into recursion via fields. */
3559 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3561 /* Compute the real value. */
3562 result = type_contains_placeholder_1 (type);
3564 /* Store the real value. */
3565 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3567 return result;
3570 /* Push tree EXP onto vector QUEUE if it is not already present. */
3572 static void
3573 push_without_duplicates (tree exp, vec<tree> *queue)
3575 unsigned int i;
3576 tree iter;
3578 FOR_EACH_VEC_ELT (*queue, i, iter)
3579 if (simple_cst_equal (iter, exp) == 1)
3580 break;
3582 if (!iter)
3583 queue->safe_push (exp);
3586 /* Given a tree EXP, find all occurrences of references to fields
3587 in a PLACEHOLDER_EXPR and place them in vector REFS without
3588 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3589 we assume here that EXP contains only arithmetic expressions
3590 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3591 argument list. */
3593 void
3594 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3596 enum tree_code code = TREE_CODE (exp);
3597 tree inner;
3598 int i;
3600 /* We handle TREE_LIST and COMPONENT_REF separately. */
3601 if (code == TREE_LIST)
3603 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3604 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3606 else if (code == COMPONENT_REF)
3608 for (inner = TREE_OPERAND (exp, 0);
3609 REFERENCE_CLASS_P (inner);
3610 inner = TREE_OPERAND (inner, 0))
3613 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3614 push_without_duplicates (exp, refs);
3615 else
3616 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3618 else
3619 switch (TREE_CODE_CLASS (code))
3621 case tcc_constant:
3622 break;
3624 case tcc_declaration:
3625 /* Variables allocated to static storage can stay. */
3626 if (!TREE_STATIC (exp))
3627 push_without_duplicates (exp, refs);
3628 break;
3630 case tcc_expression:
3631 /* This is the pattern built in ada/make_aligning_type. */
3632 if (code == ADDR_EXPR
3633 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3635 push_without_duplicates (exp, refs);
3636 break;
3639 /* Fall through... */
3641 case tcc_exceptional:
3642 case tcc_unary:
3643 case tcc_binary:
3644 case tcc_comparison:
3645 case tcc_reference:
3646 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3647 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3648 break;
3650 case tcc_vl_exp:
3651 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3652 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3653 break;
3655 default:
3656 gcc_unreachable ();
3660 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3661 return a tree with all occurrences of references to F in a
3662 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3663 CONST_DECLs. Note that we assume here that EXP contains only
3664 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3665 occurring only in their argument list. */
3667 tree
3668 substitute_in_expr (tree exp, tree f, tree r)
3670 enum tree_code code = TREE_CODE (exp);
3671 tree op0, op1, op2, op3;
3672 tree new_tree;
3674 /* We handle TREE_LIST and COMPONENT_REF separately. */
3675 if (code == TREE_LIST)
3677 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3678 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3679 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3680 return exp;
3682 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3684 else if (code == COMPONENT_REF)
3686 tree inner;
3688 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3689 and it is the right field, replace it with R. */
3690 for (inner = TREE_OPERAND (exp, 0);
3691 REFERENCE_CLASS_P (inner);
3692 inner = TREE_OPERAND (inner, 0))
3695 /* The field. */
3696 op1 = TREE_OPERAND (exp, 1);
3698 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3699 return r;
3701 /* If this expression hasn't been completed let, leave it alone. */
3702 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3703 return exp;
3705 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3706 if (op0 == TREE_OPERAND (exp, 0))
3707 return exp;
3709 new_tree
3710 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3712 else
3713 switch (TREE_CODE_CLASS (code))
3715 case tcc_constant:
3716 return exp;
3718 case tcc_declaration:
3719 if (exp == f)
3720 return r;
3721 else
3722 return exp;
3724 case tcc_expression:
3725 if (exp == f)
3726 return r;
3728 /* Fall through... */
3730 case tcc_exceptional:
3731 case tcc_unary:
3732 case tcc_binary:
3733 case tcc_comparison:
3734 case tcc_reference:
3735 switch (TREE_CODE_LENGTH (code))
3737 case 0:
3738 return exp;
3740 case 1:
3741 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3742 if (op0 == TREE_OPERAND (exp, 0))
3743 return exp;
3745 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3746 break;
3748 case 2:
3749 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3750 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3752 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3753 return exp;
3755 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3756 break;
3758 case 3:
3759 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3760 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3761 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3763 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3764 && op2 == TREE_OPERAND (exp, 2))
3765 return exp;
3767 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3768 break;
3770 case 4:
3771 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3772 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3773 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3774 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3776 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3777 && op2 == TREE_OPERAND (exp, 2)
3778 && op3 == TREE_OPERAND (exp, 3))
3779 return exp;
3781 new_tree
3782 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3783 break;
3785 default:
3786 gcc_unreachable ();
3788 break;
3790 case tcc_vl_exp:
3792 int i;
3794 new_tree = NULL_TREE;
3796 /* If we are trying to replace F with a constant, inline back
3797 functions which do nothing else than computing a value from
3798 the arguments they are passed. This makes it possible to
3799 fold partially or entirely the replacement expression. */
3800 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3802 tree t = maybe_inline_call_in_expr (exp);
3803 if (t)
3804 return SUBSTITUTE_IN_EXPR (t, f, r);
3807 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3809 tree op = TREE_OPERAND (exp, i);
3810 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3811 if (new_op != op)
3813 if (!new_tree)
3814 new_tree = copy_node (exp);
3815 TREE_OPERAND (new_tree, i) = new_op;
3819 if (new_tree)
3821 new_tree = fold (new_tree);
3822 if (TREE_CODE (new_tree) == CALL_EXPR)
3823 process_call_operands (new_tree);
3825 else
3826 return exp;
3828 break;
3830 default:
3831 gcc_unreachable ();
3834 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3836 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3837 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3839 return new_tree;
3842 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3843 for it within OBJ, a tree that is an object or a chain of references. */
3845 tree
3846 substitute_placeholder_in_expr (tree exp, tree obj)
3848 enum tree_code code = TREE_CODE (exp);
3849 tree op0, op1, op2, op3;
3850 tree new_tree;
3852 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3853 in the chain of OBJ. */
3854 if (code == PLACEHOLDER_EXPR)
3856 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3857 tree elt;
3859 for (elt = obj; elt != 0;
3860 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3861 || TREE_CODE (elt) == COND_EXPR)
3862 ? TREE_OPERAND (elt, 1)
3863 : (REFERENCE_CLASS_P (elt)
3864 || UNARY_CLASS_P (elt)
3865 || BINARY_CLASS_P (elt)
3866 || VL_EXP_CLASS_P (elt)
3867 || EXPRESSION_CLASS_P (elt))
3868 ? TREE_OPERAND (elt, 0) : 0))
3869 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3870 return elt;
3872 for (elt = obj; elt != 0;
3873 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3874 || TREE_CODE (elt) == COND_EXPR)
3875 ? TREE_OPERAND (elt, 1)
3876 : (REFERENCE_CLASS_P (elt)
3877 || UNARY_CLASS_P (elt)
3878 || BINARY_CLASS_P (elt)
3879 || VL_EXP_CLASS_P (elt)
3880 || EXPRESSION_CLASS_P (elt))
3881 ? TREE_OPERAND (elt, 0) : 0))
3882 if (POINTER_TYPE_P (TREE_TYPE (elt))
3883 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3884 == need_type))
3885 return fold_build1 (INDIRECT_REF, need_type, elt);
3887 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3888 survives until RTL generation, there will be an error. */
3889 return exp;
3892 /* TREE_LIST is special because we need to look at TREE_VALUE
3893 and TREE_CHAIN, not TREE_OPERANDS. */
3894 else if (code == TREE_LIST)
3896 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3897 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3898 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3899 return exp;
3901 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3903 else
3904 switch (TREE_CODE_CLASS (code))
3906 case tcc_constant:
3907 case tcc_declaration:
3908 return exp;
3910 case tcc_exceptional:
3911 case tcc_unary:
3912 case tcc_binary:
3913 case tcc_comparison:
3914 case tcc_expression:
3915 case tcc_reference:
3916 case tcc_statement:
3917 switch (TREE_CODE_LENGTH (code))
3919 case 0:
3920 return exp;
3922 case 1:
3923 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3924 if (op0 == TREE_OPERAND (exp, 0))
3925 return exp;
3927 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3928 break;
3930 case 2:
3931 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3932 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3934 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3935 return exp;
3937 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3938 break;
3940 case 3:
3941 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3942 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3943 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3945 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3946 && op2 == TREE_OPERAND (exp, 2))
3947 return exp;
3949 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3950 break;
3952 case 4:
3953 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3954 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3955 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3956 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3958 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3959 && op2 == TREE_OPERAND (exp, 2)
3960 && op3 == TREE_OPERAND (exp, 3))
3961 return exp;
3963 new_tree
3964 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3965 break;
3967 default:
3968 gcc_unreachable ();
3970 break;
3972 case tcc_vl_exp:
3974 int i;
3976 new_tree = NULL_TREE;
3978 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3980 tree op = TREE_OPERAND (exp, i);
3981 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
3982 if (new_op != op)
3984 if (!new_tree)
3985 new_tree = copy_node (exp);
3986 TREE_OPERAND (new_tree, i) = new_op;
3990 if (new_tree)
3992 new_tree = fold (new_tree);
3993 if (TREE_CODE (new_tree) == CALL_EXPR)
3994 process_call_operands (new_tree);
3996 else
3997 return exp;
3999 break;
4001 default:
4002 gcc_unreachable ();
4005 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4007 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4008 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4010 return new_tree;
4014 /* Subroutine of stabilize_reference; this is called for subtrees of
4015 references. Any expression with side-effects must be put in a SAVE_EXPR
4016 to ensure that it is only evaluated once.
4018 We don't put SAVE_EXPR nodes around everything, because assigning very
4019 simple expressions to temporaries causes us to miss good opportunities
4020 for optimizations. Among other things, the opportunity to fold in the
4021 addition of a constant into an addressing mode often gets lost, e.g.
4022 "y[i+1] += x;". In general, we take the approach that we should not make
4023 an assignment unless we are forced into it - i.e., that any non-side effect
4024 operator should be allowed, and that cse should take care of coalescing
4025 multiple utterances of the same expression should that prove fruitful. */
4027 static tree
4028 stabilize_reference_1 (tree e)
4030 tree result;
4031 enum tree_code code = TREE_CODE (e);
4033 /* We cannot ignore const expressions because it might be a reference
4034 to a const array but whose index contains side-effects. But we can
4035 ignore things that are actual constant or that already have been
4036 handled by this function. */
4038 if (tree_invariant_p (e))
4039 return e;
4041 switch (TREE_CODE_CLASS (code))
4043 case tcc_exceptional:
4044 case tcc_type:
4045 case tcc_declaration:
4046 case tcc_comparison:
4047 case tcc_statement:
4048 case tcc_expression:
4049 case tcc_reference:
4050 case tcc_vl_exp:
4051 /* If the expression has side-effects, then encase it in a SAVE_EXPR
4052 so that it will only be evaluated once. */
4053 /* The reference (r) and comparison (<) classes could be handled as
4054 below, but it is generally faster to only evaluate them once. */
4055 if (TREE_SIDE_EFFECTS (e))
4056 return save_expr (e);
4057 return e;
4059 case tcc_constant:
4060 /* Constants need no processing. In fact, we should never reach
4061 here. */
4062 return e;
4064 case tcc_binary:
4065 /* Division is slow and tends to be compiled with jumps,
4066 especially the division by powers of 2 that is often
4067 found inside of an array reference. So do it just once. */
4068 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
4069 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
4070 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
4071 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
4072 return save_expr (e);
4073 /* Recursively stabilize each operand. */
4074 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
4075 stabilize_reference_1 (TREE_OPERAND (e, 1)));
4076 break;
4078 case tcc_unary:
4079 /* Recursively stabilize each operand. */
4080 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
4081 break;
4083 default:
4084 gcc_unreachable ();
4087 TREE_TYPE (result) = TREE_TYPE (e);
4088 TREE_READONLY (result) = TREE_READONLY (e);
4089 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
4090 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
4092 return result;
4095 /* Stabilize a reference so that we can use it any number of times
4096 without causing its operands to be evaluated more than once.
4097 Returns the stabilized reference. This works by means of save_expr,
4098 so see the caveats in the comments about save_expr.
4100 Also allows conversion expressions whose operands are references.
4101 Any other kind of expression is returned unchanged. */
4103 tree
4104 stabilize_reference (tree ref)
4106 tree result;
4107 enum tree_code code = TREE_CODE (ref);
4109 switch (code)
4111 case VAR_DECL:
4112 case PARM_DECL:
4113 case RESULT_DECL:
4114 /* No action is needed in this case. */
4115 return ref;
4117 CASE_CONVERT:
4118 case FLOAT_EXPR:
4119 case FIX_TRUNC_EXPR:
4120 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
4121 break;
4123 case INDIRECT_REF:
4124 result = build_nt (INDIRECT_REF,
4125 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
4126 break;
4128 case COMPONENT_REF:
4129 result = build_nt (COMPONENT_REF,
4130 stabilize_reference (TREE_OPERAND (ref, 0)),
4131 TREE_OPERAND (ref, 1), NULL_TREE);
4132 break;
4134 case BIT_FIELD_REF:
4135 result = build_nt (BIT_FIELD_REF,
4136 stabilize_reference (TREE_OPERAND (ref, 0)),
4137 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
4138 break;
4140 case ARRAY_REF:
4141 result = build_nt (ARRAY_REF,
4142 stabilize_reference (TREE_OPERAND (ref, 0)),
4143 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4144 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4145 break;
4147 case ARRAY_RANGE_REF:
4148 result = build_nt (ARRAY_RANGE_REF,
4149 stabilize_reference (TREE_OPERAND (ref, 0)),
4150 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4151 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4152 break;
4154 case COMPOUND_EXPR:
4155 /* We cannot wrap the first expression in a SAVE_EXPR, as then
4156 it wouldn't be ignored. This matters when dealing with
4157 volatiles. */
4158 return stabilize_reference_1 (ref);
4160 /* If arg isn't a kind of lvalue we recognize, make no change.
4161 Caller should recognize the error for an invalid lvalue. */
4162 default:
4163 return ref;
4165 case ERROR_MARK:
4166 return error_mark_node;
4169 TREE_TYPE (result) = TREE_TYPE (ref);
4170 TREE_READONLY (result) = TREE_READONLY (ref);
4171 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
4172 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
4174 return result;
4177 /* Low-level constructors for expressions. */
4179 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
4180 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
4182 void
4183 recompute_tree_invariant_for_addr_expr (tree t)
4185 tree node;
4186 bool tc = true, se = false;
4188 /* We started out assuming this address is both invariant and constant, but
4189 does not have side effects. Now go down any handled components and see if
4190 any of them involve offsets that are either non-constant or non-invariant.
4191 Also check for side-effects.
4193 ??? Note that this code makes no attempt to deal with the case where
4194 taking the address of something causes a copy due to misalignment. */
4196 #define UPDATE_FLAGS(NODE) \
4197 do { tree _node = (NODE); \
4198 if (_node && !TREE_CONSTANT (_node)) tc = false; \
4199 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
4201 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
4202 node = TREE_OPERAND (node, 0))
4204 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
4205 array reference (probably made temporarily by the G++ front end),
4206 so ignore all the operands. */
4207 if ((TREE_CODE (node) == ARRAY_REF
4208 || TREE_CODE (node) == ARRAY_RANGE_REF)
4209 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
4211 UPDATE_FLAGS (TREE_OPERAND (node, 1));
4212 if (TREE_OPERAND (node, 2))
4213 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4214 if (TREE_OPERAND (node, 3))
4215 UPDATE_FLAGS (TREE_OPERAND (node, 3));
4217 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
4218 FIELD_DECL, apparently. The G++ front end can put something else
4219 there, at least temporarily. */
4220 else if (TREE_CODE (node) == COMPONENT_REF
4221 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
4223 if (TREE_OPERAND (node, 2))
4224 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4228 node = lang_hooks.expr_to_decl (node, &tc, &se);
4230 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4231 the address, since &(*a)->b is a form of addition. If it's a constant, the
4232 address is constant too. If it's a decl, its address is constant if the
4233 decl is static. Everything else is not constant and, furthermore,
4234 taking the address of a volatile variable is not volatile. */
4235 if (TREE_CODE (node) == INDIRECT_REF
4236 || TREE_CODE (node) == MEM_REF)
4237 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4238 else if (CONSTANT_CLASS_P (node))
4240 else if (DECL_P (node))
4241 tc &= (staticp (node) != NULL_TREE);
4242 else
4244 tc = false;
4245 se |= TREE_SIDE_EFFECTS (node);
4249 TREE_CONSTANT (t) = tc;
4250 TREE_SIDE_EFFECTS (t) = se;
4251 #undef UPDATE_FLAGS
4254 /* Build an expression of code CODE, data type TYPE, and operands as
4255 specified. Expressions and reference nodes can be created this way.
4256 Constants, decls, types and misc nodes cannot be.
4258 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4259 enough for all extant tree codes. */
4261 tree
4262 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
4264 tree t;
4266 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4268 t = make_node_stat (code PASS_MEM_STAT);
4269 TREE_TYPE (t) = tt;
4271 return t;
4274 tree
4275 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4277 int length = sizeof (struct tree_exp);
4278 tree t;
4280 record_node_allocation_statistics (code, length);
4282 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4284 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4286 memset (t, 0, sizeof (struct tree_common));
4288 TREE_SET_CODE (t, code);
4290 TREE_TYPE (t) = type;
4291 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4292 TREE_OPERAND (t, 0) = node;
4293 if (node && !TYPE_P (node))
4295 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4296 TREE_READONLY (t) = TREE_READONLY (node);
4299 if (TREE_CODE_CLASS (code) == tcc_statement)
4300 TREE_SIDE_EFFECTS (t) = 1;
4301 else switch (code)
4303 case VA_ARG_EXPR:
4304 /* All of these have side-effects, no matter what their
4305 operands are. */
4306 TREE_SIDE_EFFECTS (t) = 1;
4307 TREE_READONLY (t) = 0;
4308 break;
4310 case INDIRECT_REF:
4311 /* Whether a dereference is readonly has nothing to do with whether
4312 its operand is readonly. */
4313 TREE_READONLY (t) = 0;
4314 break;
4316 case ADDR_EXPR:
4317 if (node)
4318 recompute_tree_invariant_for_addr_expr (t);
4319 break;
4321 default:
4322 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4323 && node && !TYPE_P (node)
4324 && TREE_CONSTANT (node))
4325 TREE_CONSTANT (t) = 1;
4326 if (TREE_CODE_CLASS (code) == tcc_reference
4327 && node && TREE_THIS_VOLATILE (node))
4328 TREE_THIS_VOLATILE (t) = 1;
4329 break;
4332 return t;
4335 #define PROCESS_ARG(N) \
4336 do { \
4337 TREE_OPERAND (t, N) = arg##N; \
4338 if (arg##N &&!TYPE_P (arg##N)) \
4340 if (TREE_SIDE_EFFECTS (arg##N)) \
4341 side_effects = 1; \
4342 if (!TREE_READONLY (arg##N) \
4343 && !CONSTANT_CLASS_P (arg##N)) \
4344 (void) (read_only = 0); \
4345 if (!TREE_CONSTANT (arg##N)) \
4346 (void) (constant = 0); \
4348 } while (0)
4350 tree
4351 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
4353 bool constant, read_only, side_effects;
4354 tree t;
4356 gcc_assert (TREE_CODE_LENGTH (code) == 2);
4358 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
4359 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
4360 /* When sizetype precision doesn't match that of pointers
4361 we need to be able to build explicit extensions or truncations
4362 of the offset argument. */
4363 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
4364 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
4365 && TREE_CODE (arg1) == INTEGER_CST);
4367 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
4368 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
4369 && ptrofftype_p (TREE_TYPE (arg1)));
4371 t = make_node_stat (code PASS_MEM_STAT);
4372 TREE_TYPE (t) = tt;
4374 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
4375 result based on those same flags for the arguments. But if the
4376 arguments aren't really even `tree' expressions, we shouldn't be trying
4377 to do this. */
4379 /* Expressions without side effects may be constant if their
4380 arguments are as well. */
4381 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4382 || TREE_CODE_CLASS (code) == tcc_binary);
4383 read_only = 1;
4384 side_effects = TREE_SIDE_EFFECTS (t);
4386 PROCESS_ARG (0);
4387 PROCESS_ARG (1);
4389 TREE_READONLY (t) = read_only;
4390 TREE_CONSTANT (t) = constant;
4391 TREE_SIDE_EFFECTS (t) = side_effects;
4392 TREE_THIS_VOLATILE (t)
4393 = (TREE_CODE_CLASS (code) == tcc_reference
4394 && arg0 && TREE_THIS_VOLATILE (arg0));
4396 return t;
4400 tree
4401 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4402 tree arg2 MEM_STAT_DECL)
4404 bool constant, read_only, side_effects;
4405 tree t;
4407 gcc_assert (TREE_CODE_LENGTH (code) == 3);
4408 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4410 t = make_node_stat (code PASS_MEM_STAT);
4411 TREE_TYPE (t) = tt;
4413 read_only = 1;
4415 /* As a special exception, if COND_EXPR has NULL branches, we
4416 assume that it is a gimple statement and always consider
4417 it to have side effects. */
4418 if (code == COND_EXPR
4419 && tt == void_type_node
4420 && arg1 == NULL_TREE
4421 && arg2 == NULL_TREE)
4422 side_effects = true;
4423 else
4424 side_effects = TREE_SIDE_EFFECTS (t);
4426 PROCESS_ARG (0);
4427 PROCESS_ARG (1);
4428 PROCESS_ARG (2);
4430 if (code == COND_EXPR)
4431 TREE_READONLY (t) = read_only;
4433 TREE_SIDE_EFFECTS (t) = side_effects;
4434 TREE_THIS_VOLATILE (t)
4435 = (TREE_CODE_CLASS (code) == tcc_reference
4436 && arg0 && TREE_THIS_VOLATILE (arg0));
4438 return t;
4441 tree
4442 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4443 tree arg2, tree arg3 MEM_STAT_DECL)
4445 bool constant, read_only, side_effects;
4446 tree t;
4448 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4450 t = make_node_stat (code PASS_MEM_STAT);
4451 TREE_TYPE (t) = tt;
4453 side_effects = TREE_SIDE_EFFECTS (t);
4455 PROCESS_ARG (0);
4456 PROCESS_ARG (1);
4457 PROCESS_ARG (2);
4458 PROCESS_ARG (3);
4460 TREE_SIDE_EFFECTS (t) = side_effects;
4461 TREE_THIS_VOLATILE (t)
4462 = (TREE_CODE_CLASS (code) == tcc_reference
4463 && arg0 && TREE_THIS_VOLATILE (arg0));
4465 return t;
4468 tree
4469 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4470 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4472 bool constant, read_only, side_effects;
4473 tree t;
4475 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4477 t = make_node_stat (code PASS_MEM_STAT);
4478 TREE_TYPE (t) = tt;
4480 side_effects = TREE_SIDE_EFFECTS (t);
4482 PROCESS_ARG (0);
4483 PROCESS_ARG (1);
4484 PROCESS_ARG (2);
4485 PROCESS_ARG (3);
4486 PROCESS_ARG (4);
4488 TREE_SIDE_EFFECTS (t) = side_effects;
4489 TREE_THIS_VOLATILE (t)
4490 = (TREE_CODE_CLASS (code) == tcc_reference
4491 && arg0 && TREE_THIS_VOLATILE (arg0));
4493 return t;
4496 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4497 on the pointer PTR. */
4499 tree
4500 build_simple_mem_ref_loc (location_t loc, tree ptr)
4502 HOST_WIDE_INT offset = 0;
4503 tree ptype = TREE_TYPE (ptr);
4504 tree tem;
4505 /* For convenience allow addresses that collapse to a simple base
4506 and offset. */
4507 if (TREE_CODE (ptr) == ADDR_EXPR
4508 && (handled_component_p (TREE_OPERAND (ptr, 0))
4509 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4511 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4512 gcc_assert (ptr);
4513 ptr = build_fold_addr_expr (ptr);
4514 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4516 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4517 ptr, build_int_cst (ptype, offset));
4518 SET_EXPR_LOCATION (tem, loc);
4519 return tem;
4522 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4524 offset_int
4525 mem_ref_offset (const_tree t)
4527 return offset_int::from (TREE_OPERAND (t, 1), SIGNED);
4530 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4531 offsetted by OFFSET units. */
4533 tree
4534 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4536 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4537 build_fold_addr_expr (base),
4538 build_int_cst (ptr_type_node, offset));
4539 tree addr = build1 (ADDR_EXPR, type, ref);
4540 recompute_tree_invariant_for_addr_expr (addr);
4541 return addr;
4544 /* Similar except don't specify the TREE_TYPE
4545 and leave the TREE_SIDE_EFFECTS as 0.
4546 It is permissible for arguments to be null,
4547 or even garbage if their values do not matter. */
4549 tree
4550 build_nt (enum tree_code code, ...)
4552 tree t;
4553 int length;
4554 int i;
4555 va_list p;
4557 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4559 va_start (p, code);
4561 t = make_node (code);
4562 length = TREE_CODE_LENGTH (code);
4564 for (i = 0; i < length; i++)
4565 TREE_OPERAND (t, i) = va_arg (p, tree);
4567 va_end (p);
4568 return t;
4571 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4572 tree vec. */
4574 tree
4575 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4577 tree ret, t;
4578 unsigned int ix;
4580 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4581 CALL_EXPR_FN (ret) = fn;
4582 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4583 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4584 CALL_EXPR_ARG (ret, ix) = t;
4585 return ret;
4588 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4589 We do NOT enter this node in any sort of symbol table.
4591 LOC is the location of the decl.
4593 layout_decl is used to set up the decl's storage layout.
4594 Other slots are initialized to 0 or null pointers. */
4596 tree
4597 build_decl_stat (location_t loc, enum tree_code code, tree name,
4598 tree type MEM_STAT_DECL)
4600 tree t;
4602 t = make_node_stat (code PASS_MEM_STAT);
4603 DECL_SOURCE_LOCATION (t) = loc;
4605 /* if (type == error_mark_node)
4606 type = integer_type_node; */
4607 /* That is not done, deliberately, so that having error_mark_node
4608 as the type can suppress useless errors in the use of this variable. */
4610 DECL_NAME (t) = name;
4611 TREE_TYPE (t) = type;
4613 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4614 layout_decl (t, 0);
4616 return t;
4619 /* Builds and returns function declaration with NAME and TYPE. */
4621 tree
4622 build_fn_decl (const char *name, tree type)
4624 tree id = get_identifier (name);
4625 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4627 DECL_EXTERNAL (decl) = 1;
4628 TREE_PUBLIC (decl) = 1;
4629 DECL_ARTIFICIAL (decl) = 1;
4630 TREE_NOTHROW (decl) = 1;
4632 return decl;
4635 vec<tree, va_gc> *all_translation_units;
4637 /* Builds a new translation-unit decl with name NAME, queues it in the
4638 global list of translation-unit decls and returns it. */
4640 tree
4641 build_translation_unit_decl (tree name)
4643 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4644 name, NULL_TREE);
4645 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4646 vec_safe_push (all_translation_units, tu);
4647 return tu;
4651 /* BLOCK nodes are used to represent the structure of binding contours
4652 and declarations, once those contours have been exited and their contents
4653 compiled. This information is used for outputting debugging info. */
4655 tree
4656 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4658 tree block = make_node (BLOCK);
4660 BLOCK_VARS (block) = vars;
4661 BLOCK_SUBBLOCKS (block) = subblocks;
4662 BLOCK_SUPERCONTEXT (block) = supercontext;
4663 BLOCK_CHAIN (block) = chain;
4664 return block;
4668 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4670 LOC is the location to use in tree T. */
4672 void
4673 protected_set_expr_location (tree t, location_t loc)
4675 if (CAN_HAVE_LOCATION_P (t))
4676 SET_EXPR_LOCATION (t, loc);
4679 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4680 is ATTRIBUTE. */
4682 tree
4683 build_decl_attribute_variant (tree ddecl, tree attribute)
4685 DECL_ATTRIBUTES (ddecl) = attribute;
4686 return ddecl;
4689 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4690 is ATTRIBUTE and its qualifiers are QUALS.
4692 Record such modified types already made so we don't make duplicates. */
4694 tree
4695 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4697 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4699 inchash::hash hstate;
4700 tree ntype;
4701 int i;
4702 tree t;
4703 enum tree_code code = TREE_CODE (ttype);
4705 /* Building a distinct copy of a tagged type is inappropriate; it
4706 causes breakage in code that expects there to be a one-to-one
4707 relationship between a struct and its fields.
4708 build_duplicate_type is another solution (as used in
4709 handle_transparent_union_attribute), but that doesn't play well
4710 with the stronger C++ type identity model. */
4711 if (TREE_CODE (ttype) == RECORD_TYPE
4712 || TREE_CODE (ttype) == UNION_TYPE
4713 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4714 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4716 warning (OPT_Wattributes,
4717 "ignoring attributes applied to %qT after definition",
4718 TYPE_MAIN_VARIANT (ttype));
4719 return build_qualified_type (ttype, quals);
4722 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4723 ntype = build_distinct_type_copy (ttype);
4725 TYPE_ATTRIBUTES (ntype) = attribute;
4727 hstate.add_int (code);
4728 if (TREE_TYPE (ntype))
4729 hstate.add_object (TYPE_HASH (TREE_TYPE (ntype)));
4730 attribute_hash_list (attribute, hstate);
4732 switch (TREE_CODE (ntype))
4734 case FUNCTION_TYPE:
4735 type_hash_list (TYPE_ARG_TYPES (ntype), hstate);
4736 break;
4737 case ARRAY_TYPE:
4738 if (TYPE_DOMAIN (ntype))
4739 hstate.add_object (TYPE_HASH (TYPE_DOMAIN (ntype)));
4740 break;
4741 case INTEGER_TYPE:
4742 t = TYPE_MAX_VALUE (ntype);
4743 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
4744 hstate.add_object (TREE_INT_CST_ELT (t, i));
4745 break;
4746 case REAL_TYPE:
4747 case FIXED_POINT_TYPE:
4749 unsigned int precision = TYPE_PRECISION (ntype);
4750 hstate.add_object (precision);
4752 break;
4753 default:
4754 break;
4757 ntype = type_hash_canon (hstate.end(), ntype);
4759 /* If the target-dependent attributes make NTYPE different from
4760 its canonical type, we will need to use structural equality
4761 checks for this type. */
4762 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4763 || !comp_type_attributes (ntype, ttype))
4764 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4765 else if (TYPE_CANONICAL (ntype) == ntype)
4766 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4768 ttype = build_qualified_type (ntype, quals);
4770 else if (TYPE_QUALS (ttype) != quals)
4771 ttype = build_qualified_type (ttype, quals);
4773 return ttype;
4776 /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
4777 the same. */
4779 static bool
4780 omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
4782 tree cl1, cl2;
4783 for (cl1 = clauses1, cl2 = clauses2;
4784 cl1 && cl2;
4785 cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
4787 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
4788 return false;
4789 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
4791 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
4792 OMP_CLAUSE_DECL (cl2)) != 1)
4793 return false;
4795 switch (OMP_CLAUSE_CODE (cl1))
4797 case OMP_CLAUSE_ALIGNED:
4798 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
4799 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
4800 return false;
4801 break;
4802 case OMP_CLAUSE_LINEAR:
4803 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
4804 OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
4805 return false;
4806 break;
4807 case OMP_CLAUSE_SIMDLEN:
4808 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
4809 OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
4810 return false;
4811 default:
4812 break;
4815 return true;
4818 /* Compare two constructor-element-type constants. Return 1 if the lists
4819 are known to be equal; otherwise return 0. */
4821 static bool
4822 simple_cst_list_equal (const_tree l1, const_tree l2)
4824 while (l1 != NULL_TREE && l2 != NULL_TREE)
4826 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
4827 return false;
4829 l1 = TREE_CHAIN (l1);
4830 l2 = TREE_CHAIN (l2);
4833 return l1 == l2;
4836 /* Compare two attributes for their value identity. Return true if the
4837 attribute values are known to be equal; otherwise return false.
4840 static bool
4841 attribute_value_equal (const_tree attr1, const_tree attr2)
4843 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4844 return true;
4846 if (TREE_VALUE (attr1) != NULL_TREE
4847 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4848 && TREE_VALUE (attr2) != NULL
4849 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4850 return (simple_cst_list_equal (TREE_VALUE (attr1),
4851 TREE_VALUE (attr2)) == 1);
4853 if ((flag_openmp || flag_openmp_simd)
4854 && TREE_VALUE (attr1) && TREE_VALUE (attr2)
4855 && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
4856 && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
4857 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
4858 TREE_VALUE (attr2));
4860 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4863 /* Return 0 if the attributes for two types are incompatible, 1 if they
4864 are compatible, and 2 if they are nearly compatible (which causes a
4865 warning to be generated). */
4867 comp_type_attributes (const_tree type1, const_tree type2)
4869 const_tree a1 = TYPE_ATTRIBUTES (type1);
4870 const_tree a2 = TYPE_ATTRIBUTES (type2);
4871 const_tree a;
4873 if (a1 == a2)
4874 return 1;
4875 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4877 const struct attribute_spec *as;
4878 const_tree attr;
4880 as = lookup_attribute_spec (get_attribute_name (a));
4881 if (!as || as->affects_type_identity == false)
4882 continue;
4884 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4885 if (!attr || !attribute_value_equal (a, attr))
4886 break;
4888 if (!a)
4890 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
4892 const struct attribute_spec *as;
4894 as = lookup_attribute_spec (get_attribute_name (a));
4895 if (!as || as->affects_type_identity == false)
4896 continue;
4898 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
4899 break;
4900 /* We don't need to compare trees again, as we did this
4901 already in first loop. */
4903 /* All types - affecting identity - are equal, so
4904 there is no need to call target hook for comparison. */
4905 if (!a)
4906 return 1;
4908 /* As some type combinations - like default calling-convention - might
4909 be compatible, we have to call the target hook to get the final result. */
4910 return targetm.comp_type_attributes (type1, type2);
4913 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4914 is ATTRIBUTE.
4916 Record such modified types already made so we don't make duplicates. */
4918 tree
4919 build_type_attribute_variant (tree ttype, tree attribute)
4921 return build_type_attribute_qual_variant (ttype, attribute,
4922 TYPE_QUALS (ttype));
4926 /* Reset the expression *EXPR_P, a size or position.
4928 ??? We could reset all non-constant sizes or positions. But it's cheap
4929 enough to not do so and refrain from adding workarounds to dwarf2out.c.
4931 We need to reset self-referential sizes or positions because they cannot
4932 be gimplified and thus can contain a CALL_EXPR after the gimplification
4933 is finished, which will run afoul of LTO streaming. And they need to be
4934 reset to something essentially dummy but not constant, so as to preserve
4935 the properties of the object they are attached to. */
4937 static inline void
4938 free_lang_data_in_one_sizepos (tree *expr_p)
4940 tree expr = *expr_p;
4941 if (CONTAINS_PLACEHOLDER_P (expr))
4942 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
4946 /* Reset all the fields in a binfo node BINFO. We only keep
4947 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
4949 static void
4950 free_lang_data_in_binfo (tree binfo)
4952 unsigned i;
4953 tree t;
4955 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
4957 BINFO_VIRTUALS (binfo) = NULL_TREE;
4958 BINFO_BASE_ACCESSES (binfo) = NULL;
4959 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
4960 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
4962 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
4963 free_lang_data_in_binfo (t);
4967 /* Reset all language specific information still present in TYPE. */
4969 static void
4970 free_lang_data_in_type (tree type)
4972 gcc_assert (TYPE_P (type));
4974 /* Give the FE a chance to remove its own data first. */
4975 lang_hooks.free_lang_data (type);
4977 TREE_LANG_FLAG_0 (type) = 0;
4978 TREE_LANG_FLAG_1 (type) = 0;
4979 TREE_LANG_FLAG_2 (type) = 0;
4980 TREE_LANG_FLAG_3 (type) = 0;
4981 TREE_LANG_FLAG_4 (type) = 0;
4982 TREE_LANG_FLAG_5 (type) = 0;
4983 TREE_LANG_FLAG_6 (type) = 0;
4985 if (TREE_CODE (type) == FUNCTION_TYPE)
4987 /* Remove the const and volatile qualifiers from arguments. The
4988 C++ front end removes them, but the C front end does not,
4989 leading to false ODR violation errors when merging two
4990 instances of the same function signature compiled by
4991 different front ends. */
4992 tree p;
4994 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
4996 tree arg_type = TREE_VALUE (p);
4998 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
5000 int quals = TYPE_QUALS (arg_type)
5001 & ~TYPE_QUAL_CONST
5002 & ~TYPE_QUAL_VOLATILE;
5003 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
5004 free_lang_data_in_type (TREE_VALUE (p));
5009 /* Remove members that are not actually FIELD_DECLs from the field
5010 list of an aggregate. These occur in C++. */
5011 if (RECORD_OR_UNION_TYPE_P (type))
5013 tree prev, member;
5015 /* Note that TYPE_FIELDS can be shared across distinct
5016 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
5017 to be removed, we cannot set its TREE_CHAIN to NULL.
5018 Otherwise, we would not be able to find all the other fields
5019 in the other instances of this TREE_TYPE.
5021 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
5022 prev = NULL_TREE;
5023 member = TYPE_FIELDS (type);
5024 while (member)
5026 if (TREE_CODE (member) == FIELD_DECL
5027 || TREE_CODE (member) == TYPE_DECL)
5029 if (prev)
5030 TREE_CHAIN (prev) = member;
5031 else
5032 TYPE_FIELDS (type) = member;
5033 prev = member;
5036 member = TREE_CHAIN (member);
5039 if (prev)
5040 TREE_CHAIN (prev) = NULL_TREE;
5041 else
5042 TYPE_FIELDS (type) = NULL_TREE;
5044 TYPE_METHODS (type) = NULL_TREE;
5045 if (TYPE_BINFO (type))
5047 free_lang_data_in_binfo (TYPE_BINFO (type));
5048 if ((!BINFO_VTABLE (TYPE_BINFO (type))
5049 || !flag_devirtualize)
5050 && (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
5051 || debug_info_level != DINFO_LEVEL_NONE))
5052 TYPE_BINFO (type) = NULL;
5055 else
5057 /* For non-aggregate types, clear out the language slot (which
5058 overloads TYPE_BINFO). */
5059 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
5061 if (INTEGRAL_TYPE_P (type)
5062 || SCALAR_FLOAT_TYPE_P (type)
5063 || FIXED_POINT_TYPE_P (type))
5065 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
5066 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
5070 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
5071 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
5073 if (TYPE_CONTEXT (type)
5074 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
5076 tree ctx = TYPE_CONTEXT (type);
5079 ctx = BLOCK_SUPERCONTEXT (ctx);
5081 while (ctx && TREE_CODE (ctx) == BLOCK);
5082 TYPE_CONTEXT (type) = ctx;
5087 /* Return true if DECL may need an assembler name to be set. */
5089 static inline bool
5090 need_assembler_name_p (tree decl)
5092 /* We use DECL_ASSEMBLER_NAME to hold mangled type names for One Definition Rule
5093 merging. */
5094 if (flag_lto_odr_type_mering
5095 && TREE_CODE (decl) == TYPE_DECL
5096 && DECL_NAME (decl)
5097 && decl == TYPE_NAME (TREE_TYPE (decl))
5098 && !is_lang_specific (TREE_TYPE (decl))
5099 && AGGREGATE_TYPE_P (TREE_TYPE (decl))
5100 && !variably_modified_type_p (TREE_TYPE (decl), NULL_TREE)
5101 && !type_in_anonymous_namespace_p (TREE_TYPE (decl)))
5102 return !DECL_ASSEMBLER_NAME_SET_P (decl);
5103 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
5104 if (TREE_CODE (decl) != FUNCTION_DECL
5105 && TREE_CODE (decl) != VAR_DECL)
5106 return false;
5108 /* If DECL already has its assembler name set, it does not need a
5109 new one. */
5110 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
5111 || DECL_ASSEMBLER_NAME_SET_P (decl))
5112 return false;
5114 /* Abstract decls do not need an assembler name. */
5115 if (DECL_ABSTRACT_P (decl))
5116 return false;
5118 /* For VAR_DECLs, only static, public and external symbols need an
5119 assembler name. */
5120 if (TREE_CODE (decl) == VAR_DECL
5121 && !TREE_STATIC (decl)
5122 && !TREE_PUBLIC (decl)
5123 && !DECL_EXTERNAL (decl))
5124 return false;
5126 if (TREE_CODE (decl) == FUNCTION_DECL)
5128 /* Do not set assembler name on builtins. Allow RTL expansion to
5129 decide whether to expand inline or via a regular call. */
5130 if (DECL_BUILT_IN (decl)
5131 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
5132 return false;
5134 /* Functions represented in the callgraph need an assembler name. */
5135 if (cgraph_node::get (decl) != NULL)
5136 return true;
5138 /* Unused and not public functions don't need an assembler name. */
5139 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
5140 return false;
5143 return true;
5147 /* Reset all language specific information still present in symbol
5148 DECL. */
5150 static void
5151 free_lang_data_in_decl (tree decl)
5153 gcc_assert (DECL_P (decl));
5155 /* Give the FE a chance to remove its own data first. */
5156 lang_hooks.free_lang_data (decl);
5158 TREE_LANG_FLAG_0 (decl) = 0;
5159 TREE_LANG_FLAG_1 (decl) = 0;
5160 TREE_LANG_FLAG_2 (decl) = 0;
5161 TREE_LANG_FLAG_3 (decl) = 0;
5162 TREE_LANG_FLAG_4 (decl) = 0;
5163 TREE_LANG_FLAG_5 (decl) = 0;
5164 TREE_LANG_FLAG_6 (decl) = 0;
5166 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
5167 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
5168 if (TREE_CODE (decl) == FIELD_DECL)
5170 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
5171 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
5172 DECL_QUALIFIER (decl) = NULL_TREE;
5175 if (TREE_CODE (decl) == FUNCTION_DECL)
5177 struct cgraph_node *node;
5178 if (!(node = cgraph_node::get (decl))
5179 || (!node->definition && !node->clones))
5181 if (node)
5182 node->release_body ();
5183 else
5185 release_function_body (decl);
5186 DECL_ARGUMENTS (decl) = NULL;
5187 DECL_RESULT (decl) = NULL;
5188 DECL_INITIAL (decl) = error_mark_node;
5191 if (gimple_has_body_p (decl))
5193 tree t;
5195 /* If DECL has a gimple body, then the context for its
5196 arguments must be DECL. Otherwise, it doesn't really
5197 matter, as we will not be emitting any code for DECL. In
5198 general, there may be other instances of DECL created by
5199 the front end and since PARM_DECLs are generally shared,
5200 their DECL_CONTEXT changes as the replicas of DECL are
5201 created. The only time where DECL_CONTEXT is important
5202 is for the FUNCTION_DECLs that have a gimple body (since
5203 the PARM_DECL will be used in the function's body). */
5204 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
5205 DECL_CONTEXT (t) = decl;
5206 if (!DECL_FUNCTION_SPECIFIC_TARGET (decl))
5207 DECL_FUNCTION_SPECIFIC_TARGET (decl)
5208 = target_option_default_node;
5209 if (!DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl))
5210 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl)
5211 = optimization_default_node;
5214 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
5215 At this point, it is not needed anymore. */
5216 DECL_SAVED_TREE (decl) = NULL_TREE;
5218 /* Clear the abstract origin if it refers to a method. Otherwise
5219 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
5220 origin will not be output correctly. */
5221 if (DECL_ABSTRACT_ORIGIN (decl)
5222 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
5223 && RECORD_OR_UNION_TYPE_P
5224 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
5225 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
5227 /* Sometimes the C++ frontend doesn't manage to transform a temporary
5228 DECL_VINDEX referring to itself into a vtable slot number as it
5229 should. Happens with functions that are copied and then forgotten
5230 about. Just clear it, it won't matter anymore. */
5231 if (DECL_VINDEX (decl) && !tree_fits_shwi_p (DECL_VINDEX (decl)))
5232 DECL_VINDEX (decl) = NULL_TREE;
5234 else if (TREE_CODE (decl) == VAR_DECL)
5236 if ((DECL_EXTERNAL (decl)
5237 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
5238 || (decl_function_context (decl) && !TREE_STATIC (decl)))
5239 DECL_INITIAL (decl) = NULL_TREE;
5241 else if (TREE_CODE (decl) == TYPE_DECL
5242 || TREE_CODE (decl) == FIELD_DECL)
5243 DECL_INITIAL (decl) = NULL_TREE;
5244 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
5245 && DECL_INITIAL (decl)
5246 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
5248 /* Strip builtins from the translation-unit BLOCK. We still have targets
5249 without builtin_decl_explicit support and also builtins are shared
5250 nodes and thus we can't use TREE_CHAIN in multiple lists. */
5251 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
5252 while (*nextp)
5254 tree var = *nextp;
5255 if (TREE_CODE (var) == FUNCTION_DECL
5256 && DECL_BUILT_IN (var))
5257 *nextp = TREE_CHAIN (var);
5258 else
5259 nextp = &TREE_CHAIN (var);
5265 /* Data used when collecting DECLs and TYPEs for language data removal. */
5267 struct free_lang_data_d
5269 /* Worklist to avoid excessive recursion. */
5270 vec<tree> worklist;
5272 /* Set of traversed objects. Used to avoid duplicate visits. */
5273 hash_set<tree> *pset;
5275 /* Array of symbols to process with free_lang_data_in_decl. */
5276 vec<tree> decls;
5278 /* Array of types to process with free_lang_data_in_type. */
5279 vec<tree> types;
5283 /* Save all language fields needed to generate proper debug information
5284 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
5286 static void
5287 save_debug_info_for_decl (tree t)
5289 /*struct saved_debug_info_d *sdi;*/
5291 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
5293 /* FIXME. Partial implementation for saving debug info removed. */
5297 /* Save all language fields needed to generate proper debug information
5298 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
5300 static void
5301 save_debug_info_for_type (tree t)
5303 /*struct saved_debug_info_d *sdi;*/
5305 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
5307 /* FIXME. Partial implementation for saving debug info removed. */
5311 /* Add type or decl T to one of the list of tree nodes that need their
5312 language data removed. The lists are held inside FLD. */
5314 static void
5315 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
5317 if (DECL_P (t))
5319 fld->decls.safe_push (t);
5320 if (debug_info_level > DINFO_LEVEL_TERSE)
5321 save_debug_info_for_decl (t);
5323 else if (TYPE_P (t))
5325 fld->types.safe_push (t);
5326 if (debug_info_level > DINFO_LEVEL_TERSE)
5327 save_debug_info_for_type (t);
5329 else
5330 gcc_unreachable ();
5333 /* Push tree node T into FLD->WORKLIST. */
5335 static inline void
5336 fld_worklist_push (tree t, struct free_lang_data_d *fld)
5338 if (t && !is_lang_specific (t) && !fld->pset->contains (t))
5339 fld->worklist.safe_push ((t));
5343 /* Operand callback helper for free_lang_data_in_node. *TP is the
5344 subtree operand being considered. */
5346 static tree
5347 find_decls_types_r (tree *tp, int *ws, void *data)
5349 tree t = *tp;
5350 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
5352 if (TREE_CODE (t) == TREE_LIST)
5353 return NULL_TREE;
5355 /* Language specific nodes will be removed, so there is no need
5356 to gather anything under them. */
5357 if (is_lang_specific (t))
5359 *ws = 0;
5360 return NULL_TREE;
5363 if (DECL_P (t))
5365 /* Note that walk_tree does not traverse every possible field in
5366 decls, so we have to do our own traversals here. */
5367 add_tree_to_fld_list (t, fld);
5369 fld_worklist_push (DECL_NAME (t), fld);
5370 fld_worklist_push (DECL_CONTEXT (t), fld);
5371 fld_worklist_push (DECL_SIZE (t), fld);
5372 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
5374 /* We are going to remove everything under DECL_INITIAL for
5375 TYPE_DECLs. No point walking them. */
5376 if (TREE_CODE (t) != TYPE_DECL)
5377 fld_worklist_push (DECL_INITIAL (t), fld);
5379 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
5380 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
5382 if (TREE_CODE (t) == FUNCTION_DECL)
5384 fld_worklist_push (DECL_ARGUMENTS (t), fld);
5385 fld_worklist_push (DECL_RESULT (t), fld);
5387 else if (TREE_CODE (t) == TYPE_DECL)
5389 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
5391 else if (TREE_CODE (t) == FIELD_DECL)
5393 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
5394 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
5395 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
5396 fld_worklist_push (DECL_FCONTEXT (t), fld);
5399 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
5400 && DECL_HAS_VALUE_EXPR_P (t))
5401 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
5403 if (TREE_CODE (t) != FIELD_DECL
5404 && TREE_CODE (t) != TYPE_DECL)
5405 fld_worklist_push (TREE_CHAIN (t), fld);
5406 *ws = 0;
5408 else if (TYPE_P (t))
5410 /* Note that walk_tree does not traverse every possible field in
5411 types, so we have to do our own traversals here. */
5412 add_tree_to_fld_list (t, fld);
5414 if (!RECORD_OR_UNION_TYPE_P (t))
5415 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
5416 fld_worklist_push (TYPE_SIZE (t), fld);
5417 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
5418 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
5419 fld_worklist_push (TYPE_POINTER_TO (t), fld);
5420 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
5421 fld_worklist_push (TYPE_NAME (t), fld);
5422 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
5423 them and thus do not and want not to reach unused pointer types
5424 this way. */
5425 if (!POINTER_TYPE_P (t))
5426 fld_worklist_push (TYPE_MINVAL (t), fld);
5427 if (!RECORD_OR_UNION_TYPE_P (t))
5428 fld_worklist_push (TYPE_MAXVAL (t), fld);
5429 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
5430 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
5431 do not and want not to reach unused variants this way. */
5432 if (TYPE_CONTEXT (t))
5434 tree ctx = TYPE_CONTEXT (t);
5435 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
5436 So push that instead. */
5437 while (ctx && TREE_CODE (ctx) == BLOCK)
5438 ctx = BLOCK_SUPERCONTEXT (ctx);
5439 fld_worklist_push (ctx, fld);
5441 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
5442 and want not to reach unused types this way. */
5444 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
5446 unsigned i;
5447 tree tem;
5448 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
5449 fld_worklist_push (TREE_TYPE (tem), fld);
5450 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
5451 if (tem
5452 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
5453 && TREE_CODE (tem) == TREE_LIST)
5456 fld_worklist_push (TREE_VALUE (tem), fld);
5457 tem = TREE_CHAIN (tem);
5459 while (tem);
5461 if (RECORD_OR_UNION_TYPE_P (t))
5463 tree tem;
5464 /* Push all TYPE_FIELDS - there can be interleaving interesting
5465 and non-interesting things. */
5466 tem = TYPE_FIELDS (t);
5467 while (tem)
5469 if (TREE_CODE (tem) == FIELD_DECL
5470 || TREE_CODE (tem) == TYPE_DECL)
5471 fld_worklist_push (tem, fld);
5472 tem = TREE_CHAIN (tem);
5476 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5477 *ws = 0;
5479 else if (TREE_CODE (t) == BLOCK)
5481 tree tem;
5482 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5483 fld_worklist_push (tem, fld);
5484 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5485 fld_worklist_push (tem, fld);
5486 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5489 if (TREE_CODE (t) != IDENTIFIER_NODE
5490 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5491 fld_worklist_push (TREE_TYPE (t), fld);
5493 return NULL_TREE;
5497 /* Find decls and types in T. */
5499 static void
5500 find_decls_types (tree t, struct free_lang_data_d *fld)
5502 while (1)
5504 if (!fld->pset->contains (t))
5505 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5506 if (fld->worklist.is_empty ())
5507 break;
5508 t = fld->worklist.pop ();
5512 /* Translate all the types in LIST with the corresponding runtime
5513 types. */
5515 static tree
5516 get_eh_types_for_runtime (tree list)
5518 tree head, prev;
5520 if (list == NULL_TREE)
5521 return NULL_TREE;
5523 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5524 prev = head;
5525 list = TREE_CHAIN (list);
5526 while (list)
5528 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5529 TREE_CHAIN (prev) = n;
5530 prev = TREE_CHAIN (prev);
5531 list = TREE_CHAIN (list);
5534 return head;
5538 /* Find decls and types referenced in EH region R and store them in
5539 FLD->DECLS and FLD->TYPES. */
5541 static void
5542 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5544 switch (r->type)
5546 case ERT_CLEANUP:
5547 break;
5549 case ERT_TRY:
5551 eh_catch c;
5553 /* The types referenced in each catch must first be changed to the
5554 EH types used at runtime. This removes references to FE types
5555 in the region. */
5556 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5558 c->type_list = get_eh_types_for_runtime (c->type_list);
5559 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5562 break;
5564 case ERT_ALLOWED_EXCEPTIONS:
5565 r->u.allowed.type_list
5566 = get_eh_types_for_runtime (r->u.allowed.type_list);
5567 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5568 break;
5570 case ERT_MUST_NOT_THROW:
5571 walk_tree (&r->u.must_not_throw.failure_decl,
5572 find_decls_types_r, fld, fld->pset);
5573 break;
5578 /* Find decls and types referenced in cgraph node N and store them in
5579 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5580 look for *every* kind of DECL and TYPE node reachable from N,
5581 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5582 NAMESPACE_DECLs, etc). */
5584 static void
5585 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5587 basic_block bb;
5588 struct function *fn;
5589 unsigned ix;
5590 tree t;
5592 find_decls_types (n->decl, fld);
5594 if (!gimple_has_body_p (n->decl))
5595 return;
5597 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5599 fn = DECL_STRUCT_FUNCTION (n->decl);
5601 /* Traverse locals. */
5602 FOR_EACH_LOCAL_DECL (fn, ix, t)
5603 find_decls_types (t, fld);
5605 /* Traverse EH regions in FN. */
5607 eh_region r;
5608 FOR_ALL_EH_REGION_FN (r, fn)
5609 find_decls_types_in_eh_region (r, fld);
5612 /* Traverse every statement in FN. */
5613 FOR_EACH_BB_FN (bb, fn)
5615 gphi_iterator psi;
5616 gimple_stmt_iterator si;
5617 unsigned i;
5619 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
5621 gphi *phi = psi.phi ();
5623 for (i = 0; i < gimple_phi_num_args (phi); i++)
5625 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5626 find_decls_types (*arg_p, fld);
5630 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5632 gimple stmt = gsi_stmt (si);
5634 if (is_gimple_call (stmt))
5635 find_decls_types (gimple_call_fntype (stmt), fld);
5637 for (i = 0; i < gimple_num_ops (stmt); i++)
5639 tree arg = gimple_op (stmt, i);
5640 find_decls_types (arg, fld);
5647 /* Find decls and types referenced in varpool node N and store them in
5648 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5649 look for *every* kind of DECL and TYPE node reachable from N,
5650 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5651 NAMESPACE_DECLs, etc). */
5653 static void
5654 find_decls_types_in_var (varpool_node *v, struct free_lang_data_d *fld)
5656 find_decls_types (v->decl, fld);
5659 /* If T needs an assembler name, have one created for it. */
5661 void
5662 assign_assembler_name_if_neeeded (tree t)
5664 if (need_assembler_name_p (t))
5666 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5667 diagnostics that use input_location to show locus
5668 information. The problem here is that, at this point,
5669 input_location is generally anchored to the end of the file
5670 (since the parser is long gone), so we don't have a good
5671 position to pin it to.
5673 To alleviate this problem, this uses the location of T's
5674 declaration. Examples of this are
5675 testsuite/g++.dg/template/cond2.C and
5676 testsuite/g++.dg/template/pr35240.C. */
5677 location_t saved_location = input_location;
5678 input_location = DECL_SOURCE_LOCATION (t);
5680 decl_assembler_name (t);
5682 input_location = saved_location;
5687 /* Free language specific information for every operand and expression
5688 in every node of the call graph. This process operates in three stages:
5690 1- Every callgraph node and varpool node is traversed looking for
5691 decls and types embedded in them. This is a more exhaustive
5692 search than that done by find_referenced_vars, because it will
5693 also collect individual fields, decls embedded in types, etc.
5695 2- All the decls found are sent to free_lang_data_in_decl.
5697 3- All the types found are sent to free_lang_data_in_type.
5699 The ordering between decls and types is important because
5700 free_lang_data_in_decl sets assembler names, which includes
5701 mangling. So types cannot be freed up until assembler names have
5702 been set up. */
5704 static void
5705 free_lang_data_in_cgraph (void)
5707 struct cgraph_node *n;
5708 varpool_node *v;
5709 struct free_lang_data_d fld;
5710 tree t;
5711 unsigned i;
5712 alias_pair *p;
5714 /* Initialize sets and arrays to store referenced decls and types. */
5715 fld.pset = new hash_set<tree>;
5716 fld.worklist.create (0);
5717 fld.decls.create (100);
5718 fld.types.create (100);
5720 /* Find decls and types in the body of every function in the callgraph. */
5721 FOR_EACH_FUNCTION (n)
5722 find_decls_types_in_node (n, &fld);
5724 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5725 find_decls_types (p->decl, &fld);
5727 /* Find decls and types in every varpool symbol. */
5728 FOR_EACH_VARIABLE (v)
5729 find_decls_types_in_var (v, &fld);
5731 /* Set the assembler name on every decl found. We need to do this
5732 now because free_lang_data_in_decl will invalidate data needed
5733 for mangling. This breaks mangling on interdependent decls. */
5734 FOR_EACH_VEC_ELT (fld.decls, i, t)
5735 assign_assembler_name_if_neeeded (t);
5737 /* Traverse every decl found freeing its language data. */
5738 FOR_EACH_VEC_ELT (fld.decls, i, t)
5739 free_lang_data_in_decl (t);
5741 /* Traverse every type found freeing its language data. */
5742 FOR_EACH_VEC_ELT (fld.types, i, t)
5743 free_lang_data_in_type (t);
5745 delete fld.pset;
5746 fld.worklist.release ();
5747 fld.decls.release ();
5748 fld.types.release ();
5752 /* Free resources that are used by FE but are not needed once they are done. */
5754 static unsigned
5755 free_lang_data (void)
5757 unsigned i;
5759 /* If we are the LTO frontend we have freed lang-specific data already. */
5760 if (in_lto_p
5761 || (!flag_generate_lto && !flag_generate_offload))
5762 return 0;
5764 /* Allocate and assign alias sets to the standard integer types
5765 while the slots are still in the way the frontends generated them. */
5766 for (i = 0; i < itk_none; ++i)
5767 if (integer_types[i])
5768 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5770 /* Traverse the IL resetting language specific information for
5771 operands, expressions, etc. */
5772 free_lang_data_in_cgraph ();
5774 /* Create gimple variants for common types. */
5775 ptrdiff_type_node = integer_type_node;
5776 fileptr_type_node = ptr_type_node;
5778 /* Reset some langhooks. Do not reset types_compatible_p, it may
5779 still be used indirectly via the get_alias_set langhook. */
5780 lang_hooks.dwarf_name = lhd_dwarf_name;
5781 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5782 /* We do not want the default decl_assembler_name implementation,
5783 rather if we have fixed everything we want a wrapper around it
5784 asserting that all non-local symbols already got their assembler
5785 name and only produce assembler names for local symbols. Or rather
5786 make sure we never call decl_assembler_name on local symbols and
5787 devise a separate, middle-end private scheme for it. */
5789 /* Reset diagnostic machinery. */
5790 tree_diagnostics_defaults (global_dc);
5792 return 0;
5796 namespace {
5798 const pass_data pass_data_ipa_free_lang_data =
5800 SIMPLE_IPA_PASS, /* type */
5801 "*free_lang_data", /* name */
5802 OPTGROUP_NONE, /* optinfo_flags */
5803 TV_IPA_FREE_LANG_DATA, /* tv_id */
5804 0, /* properties_required */
5805 0, /* properties_provided */
5806 0, /* properties_destroyed */
5807 0, /* todo_flags_start */
5808 0, /* todo_flags_finish */
5811 class pass_ipa_free_lang_data : public simple_ipa_opt_pass
5813 public:
5814 pass_ipa_free_lang_data (gcc::context *ctxt)
5815 : simple_ipa_opt_pass (pass_data_ipa_free_lang_data, ctxt)
5818 /* opt_pass methods: */
5819 virtual unsigned int execute (function *) { return free_lang_data (); }
5821 }; // class pass_ipa_free_lang_data
5823 } // anon namespace
5825 simple_ipa_opt_pass *
5826 make_pass_ipa_free_lang_data (gcc::context *ctxt)
5828 return new pass_ipa_free_lang_data (ctxt);
5831 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
5832 ATTR_NAME. Also used internally by remove_attribute(). */
5833 bool
5834 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
5836 size_t ident_len = IDENTIFIER_LENGTH (ident);
5838 if (ident_len == attr_len)
5840 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
5841 return true;
5843 else if (ident_len == attr_len + 4)
5845 /* There is the possibility that ATTR is 'text' and IDENT is
5846 '__text__'. */
5847 const char *p = IDENTIFIER_POINTER (ident);
5848 if (p[0] == '_' && p[1] == '_'
5849 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5850 && strncmp (attr_name, p + 2, attr_len) == 0)
5851 return true;
5854 return false;
5857 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
5858 of ATTR_NAME, and LIST is not NULL_TREE. */
5859 tree
5860 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
5862 while (list)
5864 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5866 if (ident_len == attr_len)
5868 if (!strcmp (attr_name,
5869 IDENTIFIER_POINTER (get_attribute_name (list))))
5870 break;
5872 /* TODO: If we made sure that attributes were stored in the
5873 canonical form without '__...__' (ie, as in 'text' as opposed
5874 to '__text__') then we could avoid the following case. */
5875 else if (ident_len == attr_len + 4)
5877 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5878 if (p[0] == '_' && p[1] == '_'
5879 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5880 && strncmp (attr_name, p + 2, attr_len) == 0)
5881 break;
5883 list = TREE_CHAIN (list);
5886 return list;
5889 /* Given an attribute name ATTR_NAME and a list of attributes LIST,
5890 return a pointer to the attribute's list first element if the attribute
5891 starts with ATTR_NAME. ATTR_NAME must be in the form 'text' (not
5892 '__text__'). */
5894 tree
5895 private_lookup_attribute_by_prefix (const char *attr_name, size_t attr_len,
5896 tree list)
5898 while (list)
5900 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5902 if (attr_len > ident_len)
5904 list = TREE_CHAIN (list);
5905 continue;
5908 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5910 if (strncmp (attr_name, p, attr_len) == 0)
5911 break;
5913 /* TODO: If we made sure that attributes were stored in the
5914 canonical form without '__...__' (ie, as in 'text' as opposed
5915 to '__text__') then we could avoid the following case. */
5916 if (p[0] == '_' && p[1] == '_' &&
5917 strncmp (attr_name, p + 2, attr_len) == 0)
5918 break;
5920 list = TREE_CHAIN (list);
5923 return list;
5927 /* A variant of lookup_attribute() that can be used with an identifier
5928 as the first argument, and where the identifier can be either
5929 'text' or '__text__'.
5931 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
5932 return a pointer to the attribute's list element if the attribute
5933 is part of the list, or NULL_TREE if not found. If the attribute
5934 appears more than once, this only returns the first occurrence; the
5935 TREE_CHAIN of the return value should be passed back in if further
5936 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
5937 can be in the form 'text' or '__text__'. */
5938 static tree
5939 lookup_ident_attribute (tree attr_identifier, tree list)
5941 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
5943 while (list)
5945 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
5946 == IDENTIFIER_NODE);
5948 /* Identifiers can be compared directly for equality. */
5949 if (attr_identifier == get_attribute_name (list))
5950 break;
5952 /* If they are not equal, they may still be one in the form
5953 'text' while the other one is in the form '__text__'. TODO:
5954 If we were storing attributes in normalized 'text' form, then
5955 this could all go away and we could take full advantage of
5956 the fact that we're comparing identifiers. :-) */
5958 size_t attr_len = IDENTIFIER_LENGTH (attr_identifier);
5959 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5961 if (ident_len == attr_len + 4)
5963 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5964 const char *q = IDENTIFIER_POINTER (attr_identifier);
5965 if (p[0] == '_' && p[1] == '_'
5966 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5967 && strncmp (q, p + 2, attr_len) == 0)
5968 break;
5970 else if (ident_len + 4 == attr_len)
5972 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5973 const char *q = IDENTIFIER_POINTER (attr_identifier);
5974 if (q[0] == '_' && q[1] == '_'
5975 && q[attr_len - 2] == '_' && q[attr_len - 1] == '_'
5976 && strncmp (q + 2, p, ident_len) == 0)
5977 break;
5980 list = TREE_CHAIN (list);
5983 return list;
5986 /* Remove any instances of attribute ATTR_NAME in LIST and return the
5987 modified list. */
5989 tree
5990 remove_attribute (const char *attr_name, tree list)
5992 tree *p;
5993 size_t attr_len = strlen (attr_name);
5995 gcc_checking_assert (attr_name[0] != '_');
5997 for (p = &list; *p; )
5999 tree l = *p;
6000 /* TODO: If we were storing attributes in normalized form, here
6001 we could use a simple strcmp(). */
6002 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
6003 *p = TREE_CHAIN (l);
6004 else
6005 p = &TREE_CHAIN (l);
6008 return list;
6011 /* Return an attribute list that is the union of a1 and a2. */
6013 tree
6014 merge_attributes (tree a1, tree a2)
6016 tree attributes;
6018 /* Either one unset? Take the set one. */
6020 if ((attributes = a1) == 0)
6021 attributes = a2;
6023 /* One that completely contains the other? Take it. */
6025 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
6027 if (attribute_list_contained (a2, a1))
6028 attributes = a2;
6029 else
6031 /* Pick the longest list, and hang on the other list. */
6033 if (list_length (a1) < list_length (a2))
6034 attributes = a2, a2 = a1;
6036 for (; a2 != 0; a2 = TREE_CHAIN (a2))
6038 tree a;
6039 for (a = lookup_ident_attribute (get_attribute_name (a2),
6040 attributes);
6041 a != NULL_TREE && !attribute_value_equal (a, a2);
6042 a = lookup_ident_attribute (get_attribute_name (a2),
6043 TREE_CHAIN (a)))
6045 if (a == NULL_TREE)
6047 a1 = copy_node (a2);
6048 TREE_CHAIN (a1) = attributes;
6049 attributes = a1;
6054 return attributes;
6057 /* Given types T1 and T2, merge their attributes and return
6058 the result. */
6060 tree
6061 merge_type_attributes (tree t1, tree t2)
6063 return merge_attributes (TYPE_ATTRIBUTES (t1),
6064 TYPE_ATTRIBUTES (t2));
6067 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
6068 the result. */
6070 tree
6071 merge_decl_attributes (tree olddecl, tree newdecl)
6073 return merge_attributes (DECL_ATTRIBUTES (olddecl),
6074 DECL_ATTRIBUTES (newdecl));
6077 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
6079 /* Specialization of merge_decl_attributes for various Windows targets.
6081 This handles the following situation:
6083 __declspec (dllimport) int foo;
6084 int foo;
6086 The second instance of `foo' nullifies the dllimport. */
6088 tree
6089 merge_dllimport_decl_attributes (tree old, tree new_tree)
6091 tree a;
6092 int delete_dllimport_p = 1;
6094 /* What we need to do here is remove from `old' dllimport if it doesn't
6095 appear in `new'. dllimport behaves like extern: if a declaration is
6096 marked dllimport and a definition appears later, then the object
6097 is not dllimport'd. We also remove a `new' dllimport if the old list
6098 contains dllexport: dllexport always overrides dllimport, regardless
6099 of the order of declaration. */
6100 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
6101 delete_dllimport_p = 0;
6102 else if (DECL_DLLIMPORT_P (new_tree)
6103 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
6105 DECL_DLLIMPORT_P (new_tree) = 0;
6106 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
6107 "dllimport ignored", new_tree);
6109 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
6111 /* Warn about overriding a symbol that has already been used, e.g.:
6112 extern int __attribute__ ((dllimport)) foo;
6113 int* bar () {return &foo;}
6114 int foo;
6116 if (TREE_USED (old))
6118 warning (0, "%q+D redeclared without dllimport attribute "
6119 "after being referenced with dll linkage", new_tree);
6120 /* If we have used a variable's address with dllimport linkage,
6121 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
6122 decl may already have had TREE_CONSTANT computed.
6123 We still remove the attribute so that assembler code refers
6124 to '&foo rather than '_imp__foo'. */
6125 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
6126 DECL_DLLIMPORT_P (new_tree) = 1;
6129 /* Let an inline definition silently override the external reference,
6130 but otherwise warn about attribute inconsistency. */
6131 else if (TREE_CODE (new_tree) == VAR_DECL
6132 || !DECL_DECLARED_INLINE_P (new_tree))
6133 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
6134 "previous dllimport ignored", new_tree);
6136 else
6137 delete_dllimport_p = 0;
6139 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
6141 if (delete_dllimport_p)
6142 a = remove_attribute ("dllimport", a);
6144 return a;
6147 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
6148 struct attribute_spec.handler. */
6150 tree
6151 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
6152 bool *no_add_attrs)
6154 tree node = *pnode;
6155 bool is_dllimport;
6157 /* These attributes may apply to structure and union types being created,
6158 but otherwise should pass to the declaration involved. */
6159 if (!DECL_P (node))
6161 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
6162 | (int) ATTR_FLAG_ARRAY_NEXT))
6164 *no_add_attrs = true;
6165 return tree_cons (name, args, NULL_TREE);
6167 if (TREE_CODE (node) == RECORD_TYPE
6168 || TREE_CODE (node) == UNION_TYPE)
6170 node = TYPE_NAME (node);
6171 if (!node)
6172 return NULL_TREE;
6174 else
6176 warning (OPT_Wattributes, "%qE attribute ignored",
6177 name);
6178 *no_add_attrs = true;
6179 return NULL_TREE;
6183 if (TREE_CODE (node) != FUNCTION_DECL
6184 && TREE_CODE (node) != VAR_DECL
6185 && TREE_CODE (node) != TYPE_DECL)
6187 *no_add_attrs = true;
6188 warning (OPT_Wattributes, "%qE attribute ignored",
6189 name);
6190 return NULL_TREE;
6193 if (TREE_CODE (node) == TYPE_DECL
6194 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
6195 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
6197 *no_add_attrs = true;
6198 warning (OPT_Wattributes, "%qE attribute ignored",
6199 name);
6200 return NULL_TREE;
6203 is_dllimport = is_attribute_p ("dllimport", name);
6205 /* Report error on dllimport ambiguities seen now before they cause
6206 any damage. */
6207 if (is_dllimport)
6209 /* Honor any target-specific overrides. */
6210 if (!targetm.valid_dllimport_attribute_p (node))
6211 *no_add_attrs = true;
6213 else if (TREE_CODE (node) == FUNCTION_DECL
6214 && DECL_DECLARED_INLINE_P (node))
6216 warning (OPT_Wattributes, "inline function %q+D declared as "
6217 " dllimport: attribute ignored", node);
6218 *no_add_attrs = true;
6220 /* Like MS, treat definition of dllimported variables and
6221 non-inlined functions on declaration as syntax errors. */
6222 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
6224 error ("function %q+D definition is marked dllimport", node);
6225 *no_add_attrs = true;
6228 else if (TREE_CODE (node) == VAR_DECL)
6230 if (DECL_INITIAL (node))
6232 error ("variable %q+D definition is marked dllimport",
6233 node);
6234 *no_add_attrs = true;
6237 /* `extern' needn't be specified with dllimport.
6238 Specify `extern' now and hope for the best. Sigh. */
6239 DECL_EXTERNAL (node) = 1;
6240 /* Also, implicitly give dllimport'd variables declared within
6241 a function global scope, unless declared static. */
6242 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
6243 TREE_PUBLIC (node) = 1;
6246 if (*no_add_attrs == false)
6247 DECL_DLLIMPORT_P (node) = 1;
6249 else if (TREE_CODE (node) == FUNCTION_DECL
6250 && DECL_DECLARED_INLINE_P (node)
6251 && flag_keep_inline_dllexport)
6252 /* An exported function, even if inline, must be emitted. */
6253 DECL_EXTERNAL (node) = 0;
6255 /* Report error if symbol is not accessible at global scope. */
6256 if (!TREE_PUBLIC (node)
6257 && (TREE_CODE (node) == VAR_DECL
6258 || TREE_CODE (node) == FUNCTION_DECL))
6260 error ("external linkage required for symbol %q+D because of "
6261 "%qE attribute", node, name);
6262 *no_add_attrs = true;
6265 /* A dllexport'd entity must have default visibility so that other
6266 program units (shared libraries or the main executable) can see
6267 it. A dllimport'd entity must have default visibility so that
6268 the linker knows that undefined references within this program
6269 unit can be resolved by the dynamic linker. */
6270 if (!*no_add_attrs)
6272 if (DECL_VISIBILITY_SPECIFIED (node)
6273 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
6274 error ("%qE implies default visibility, but %qD has already "
6275 "been declared with a different visibility",
6276 name, node);
6277 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
6278 DECL_VISIBILITY_SPECIFIED (node) = 1;
6281 return NULL_TREE;
6284 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
6286 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
6287 of the various TYPE_QUAL values. */
6289 static void
6290 set_type_quals (tree type, int type_quals)
6292 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
6293 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
6294 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
6295 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
6296 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
6299 /* Returns true iff unqualified CAND and BASE are equivalent. */
6301 bool
6302 check_base_type (const_tree cand, const_tree base)
6304 return (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) == TYPE_ALIGN (base)
6309 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6310 TYPE_ATTRIBUTES (base)));
6313 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
6315 bool
6316 check_qualified_type (const_tree cand, const_tree base, int type_quals)
6318 return (TYPE_QUALS (cand) == type_quals
6319 && check_base_type (cand, base));
6322 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
6324 static bool
6325 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
6327 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
6328 && TYPE_NAME (cand) == TYPE_NAME (base)
6329 /* Apparently this is needed for Objective-C. */
6330 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6331 /* Check alignment. */
6332 && TYPE_ALIGN (cand) == align
6333 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6334 TYPE_ATTRIBUTES (base)));
6337 /* This function checks to see if TYPE matches the size one of the built-in
6338 atomic types, and returns that core atomic type. */
6340 static tree
6341 find_atomic_core_type (tree type)
6343 tree base_atomic_type;
6345 /* Only handle complete types. */
6346 if (TYPE_SIZE (type) == NULL_TREE)
6347 return NULL_TREE;
6349 HOST_WIDE_INT type_size = tree_to_uhwi (TYPE_SIZE (type));
6350 switch (type_size)
6352 case 8:
6353 base_atomic_type = atomicQI_type_node;
6354 break;
6356 case 16:
6357 base_atomic_type = atomicHI_type_node;
6358 break;
6360 case 32:
6361 base_atomic_type = atomicSI_type_node;
6362 break;
6364 case 64:
6365 base_atomic_type = atomicDI_type_node;
6366 break;
6368 case 128:
6369 base_atomic_type = atomicTI_type_node;
6370 break;
6372 default:
6373 base_atomic_type = NULL_TREE;
6376 return base_atomic_type;
6379 /* Return a version of the TYPE, qualified as indicated by the
6380 TYPE_QUALS, if one exists. If no qualified version exists yet,
6381 return NULL_TREE. */
6383 tree
6384 get_qualified_type (tree type, int type_quals)
6386 tree t;
6388 if (TYPE_QUALS (type) == type_quals)
6389 return type;
6391 /* Search the chain of variants to see if there is already one there just
6392 like the one we need to have. If so, use that existing one. We must
6393 preserve the TYPE_NAME, since there is code that depends on this. */
6394 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6395 if (check_qualified_type (t, type, type_quals))
6396 return t;
6398 return NULL_TREE;
6401 /* Like get_qualified_type, but creates the type if it does not
6402 exist. This function never returns NULL_TREE. */
6404 tree
6405 build_qualified_type (tree type, int type_quals)
6407 tree t;
6409 /* See if we already have the appropriate qualified variant. */
6410 t = get_qualified_type (type, type_quals);
6412 /* If not, build it. */
6413 if (!t)
6415 t = build_variant_type_copy (type);
6416 set_type_quals (t, type_quals);
6418 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
6420 /* See if this object can map to a basic atomic type. */
6421 tree atomic_type = find_atomic_core_type (type);
6422 if (atomic_type)
6424 /* Ensure the alignment of this type is compatible with
6425 the required alignment of the atomic type. */
6426 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
6427 TYPE_ALIGN (t) = TYPE_ALIGN (atomic_type);
6431 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6432 /* Propagate structural equality. */
6433 SET_TYPE_STRUCTURAL_EQUALITY (t);
6434 else if (TYPE_CANONICAL (type) != type)
6435 /* Build the underlying canonical type, since it is different
6436 from TYPE. */
6438 tree c = build_qualified_type (TYPE_CANONICAL (type), type_quals);
6439 TYPE_CANONICAL (t) = TYPE_CANONICAL (c);
6441 else
6442 /* T is its own canonical type. */
6443 TYPE_CANONICAL (t) = t;
6447 return t;
6450 /* Create a variant of type T with alignment ALIGN. */
6452 tree
6453 build_aligned_type (tree type, unsigned int align)
6455 tree t;
6457 if (TYPE_PACKED (type)
6458 || TYPE_ALIGN (type) == align)
6459 return type;
6461 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6462 if (check_aligned_type (t, type, align))
6463 return t;
6465 t = build_variant_type_copy (type);
6466 TYPE_ALIGN (t) = align;
6468 return t;
6471 /* Create a new distinct copy of TYPE. The new type is made its own
6472 MAIN_VARIANT. If TYPE requires structural equality checks, the
6473 resulting type requires structural equality checks; otherwise, its
6474 TYPE_CANONICAL points to itself. */
6476 tree
6477 build_distinct_type_copy (tree type)
6479 tree t = copy_node (type);
6481 TYPE_POINTER_TO (t) = 0;
6482 TYPE_REFERENCE_TO (t) = 0;
6484 /* Set the canonical type either to a new equivalence class, or
6485 propagate the need for structural equality checks. */
6486 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6487 SET_TYPE_STRUCTURAL_EQUALITY (t);
6488 else
6489 TYPE_CANONICAL (t) = t;
6491 /* Make it its own variant. */
6492 TYPE_MAIN_VARIANT (t) = t;
6493 TYPE_NEXT_VARIANT (t) = 0;
6495 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
6496 whose TREE_TYPE is not t. This can also happen in the Ada
6497 frontend when using subtypes. */
6499 return t;
6502 /* Create a new variant of TYPE, equivalent but distinct. This is so
6503 the caller can modify it. TYPE_CANONICAL for the return type will
6504 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
6505 are considered equal by the language itself (or that both types
6506 require structural equality checks). */
6508 tree
6509 build_variant_type_copy (tree type)
6511 tree t, m = TYPE_MAIN_VARIANT (type);
6513 t = build_distinct_type_copy (type);
6515 /* Since we're building a variant, assume that it is a non-semantic
6516 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
6517 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
6519 /* Add the new type to the chain of variants of TYPE. */
6520 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
6521 TYPE_NEXT_VARIANT (m) = t;
6522 TYPE_MAIN_VARIANT (t) = m;
6524 return t;
6527 /* Return true if the from tree in both tree maps are equal. */
6530 tree_map_base_eq (const void *va, const void *vb)
6532 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6533 *const b = (const struct tree_map_base *) vb;
6534 return (a->from == b->from);
6537 /* Hash a from tree in a tree_base_map. */
6539 unsigned int
6540 tree_map_base_hash (const void *item)
6542 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6545 /* Return true if this tree map structure is marked for garbage collection
6546 purposes. We simply return true if the from tree is marked, so that this
6547 structure goes away when the from tree goes away. */
6550 tree_map_base_marked_p (const void *p)
6552 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6555 /* Hash a from tree in a tree_map. */
6557 unsigned int
6558 tree_map_hash (const void *item)
6560 return (((const struct tree_map *) item)->hash);
6563 /* Hash a from tree in a tree_decl_map. */
6565 unsigned int
6566 tree_decl_map_hash (const void *item)
6568 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6571 /* Return the initialization priority for DECL. */
6573 priority_type
6574 decl_init_priority_lookup (tree decl)
6576 symtab_node *snode = symtab_node::get (decl);
6578 if (!snode)
6579 return DEFAULT_INIT_PRIORITY;
6580 return
6581 snode->get_init_priority ();
6584 /* Return the finalization priority for DECL. */
6586 priority_type
6587 decl_fini_priority_lookup (tree decl)
6589 cgraph_node *node = cgraph_node::get (decl);
6591 if (!node)
6592 return DEFAULT_INIT_PRIORITY;
6593 return
6594 node->get_fini_priority ();
6597 /* Set the initialization priority for DECL to PRIORITY. */
6599 void
6600 decl_init_priority_insert (tree decl, priority_type priority)
6602 struct symtab_node *snode;
6604 if (priority == DEFAULT_INIT_PRIORITY)
6606 snode = symtab_node::get (decl);
6607 if (!snode)
6608 return;
6610 else if (TREE_CODE (decl) == VAR_DECL)
6611 snode = varpool_node::get_create (decl);
6612 else
6613 snode = cgraph_node::get_create (decl);
6614 snode->set_init_priority (priority);
6617 /* Set the finalization priority for DECL to PRIORITY. */
6619 void
6620 decl_fini_priority_insert (tree decl, priority_type priority)
6622 struct cgraph_node *node;
6624 if (priority == DEFAULT_INIT_PRIORITY)
6626 node = cgraph_node::get (decl);
6627 if (!node)
6628 return;
6630 else
6631 node = cgraph_node::get_create (decl);
6632 node->set_fini_priority (priority);
6635 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6637 static void
6638 print_debug_expr_statistics (void)
6640 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6641 (long) debug_expr_for_decl->size (),
6642 (long) debug_expr_for_decl->elements (),
6643 debug_expr_for_decl->collisions ());
6646 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6648 static void
6649 print_value_expr_statistics (void)
6651 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6652 (long) value_expr_for_decl->size (),
6653 (long) value_expr_for_decl->elements (),
6654 value_expr_for_decl->collisions ());
6657 /* Lookup a debug expression for FROM, and return it if we find one. */
6659 tree
6660 decl_debug_expr_lookup (tree from)
6662 struct tree_decl_map *h, in;
6663 in.base.from = from;
6665 h = debug_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6666 if (h)
6667 return h->to;
6668 return NULL_TREE;
6671 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6673 void
6674 decl_debug_expr_insert (tree from, tree to)
6676 struct tree_decl_map *h;
6678 h = ggc_alloc<tree_decl_map> ();
6679 h->base.from = from;
6680 h->to = to;
6681 *debug_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6684 /* Lookup a value expression for FROM, and return it if we find one. */
6686 tree
6687 decl_value_expr_lookup (tree from)
6689 struct tree_decl_map *h, in;
6690 in.base.from = from;
6692 h = value_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6693 if (h)
6694 return h->to;
6695 return NULL_TREE;
6698 /* Insert a mapping FROM->TO in the value expression hashtable. */
6700 void
6701 decl_value_expr_insert (tree from, tree to)
6703 struct tree_decl_map *h;
6705 h = ggc_alloc<tree_decl_map> ();
6706 h->base.from = from;
6707 h->to = to;
6708 *value_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6711 /* Lookup a vector of debug arguments for FROM, and return it if we
6712 find one. */
6714 vec<tree, va_gc> **
6715 decl_debug_args_lookup (tree from)
6717 struct tree_vec_map *h, in;
6719 if (!DECL_HAS_DEBUG_ARGS_P (from))
6720 return NULL;
6721 gcc_checking_assert (debug_args_for_decl != NULL);
6722 in.base.from = from;
6723 h = debug_args_for_decl->find_with_hash (&in, DECL_UID (from));
6724 if (h)
6725 return &h->to;
6726 return NULL;
6729 /* Insert a mapping FROM->empty vector of debug arguments in the value
6730 expression hashtable. */
6732 vec<tree, va_gc> **
6733 decl_debug_args_insert (tree from)
6735 struct tree_vec_map *h;
6736 tree_vec_map **loc;
6738 if (DECL_HAS_DEBUG_ARGS_P (from))
6739 return decl_debug_args_lookup (from);
6740 if (debug_args_for_decl == NULL)
6741 debug_args_for_decl = hash_table<tree_vec_map_cache_hasher>::create_ggc (64);
6742 h = ggc_alloc<tree_vec_map> ();
6743 h->base.from = from;
6744 h->to = NULL;
6745 loc = debug_args_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT);
6746 *loc = h;
6747 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6748 return &h->to;
6751 /* Hashing of types so that we don't make duplicates.
6752 The entry point is `type_hash_canon'. */
6754 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6755 with types in the TREE_VALUE slots), by adding the hash codes
6756 of the individual types. */
6758 static void
6759 type_hash_list (const_tree list, inchash::hash &hstate)
6761 const_tree tail;
6763 for (tail = list; tail; tail = TREE_CHAIN (tail))
6764 if (TREE_VALUE (tail) != error_mark_node)
6765 hstate.add_object (TYPE_HASH (TREE_VALUE (tail)));
6768 /* These are the Hashtable callback functions. */
6770 /* Returns true iff the types are equivalent. */
6772 bool
6773 type_cache_hasher::equal (type_hash *a, type_hash *b)
6775 /* First test the things that are the same for all types. */
6776 if (a->hash != b->hash
6777 || TREE_CODE (a->type) != TREE_CODE (b->type)
6778 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6779 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6780 TYPE_ATTRIBUTES (b->type))
6781 || (TREE_CODE (a->type) != COMPLEX_TYPE
6782 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6783 return 0;
6785 /* Be careful about comparing arrays before and after the element type
6786 has been completed; don't compare TYPE_ALIGN unless both types are
6787 complete. */
6788 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6789 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6790 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6791 return 0;
6793 switch (TREE_CODE (a->type))
6795 case VOID_TYPE:
6796 case COMPLEX_TYPE:
6797 case POINTER_TYPE:
6798 case REFERENCE_TYPE:
6799 case NULLPTR_TYPE:
6800 return 1;
6802 case VECTOR_TYPE:
6803 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6805 case ENUMERAL_TYPE:
6806 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6807 && !(TYPE_VALUES (a->type)
6808 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6809 && TYPE_VALUES (b->type)
6810 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6811 && type_list_equal (TYPE_VALUES (a->type),
6812 TYPE_VALUES (b->type))))
6813 return 0;
6815 /* ... fall through ... */
6817 case INTEGER_TYPE:
6818 case REAL_TYPE:
6819 case BOOLEAN_TYPE:
6820 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
6821 return false;
6822 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6823 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6824 TYPE_MAX_VALUE (b->type)))
6825 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6826 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6827 TYPE_MIN_VALUE (b->type))));
6829 case FIXED_POINT_TYPE:
6830 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6832 case OFFSET_TYPE:
6833 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6835 case METHOD_TYPE:
6836 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6837 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6838 || (TYPE_ARG_TYPES (a->type)
6839 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6840 && TYPE_ARG_TYPES (b->type)
6841 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6842 && type_list_equal (TYPE_ARG_TYPES (a->type),
6843 TYPE_ARG_TYPES (b->type)))))
6844 break;
6845 return 0;
6846 case ARRAY_TYPE:
6847 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6849 case RECORD_TYPE:
6850 case UNION_TYPE:
6851 case QUAL_UNION_TYPE:
6852 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6853 || (TYPE_FIELDS (a->type)
6854 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6855 && TYPE_FIELDS (b->type)
6856 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6857 && type_list_equal (TYPE_FIELDS (a->type),
6858 TYPE_FIELDS (b->type))));
6860 case FUNCTION_TYPE:
6861 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6862 || (TYPE_ARG_TYPES (a->type)
6863 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6864 && TYPE_ARG_TYPES (b->type)
6865 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6866 && type_list_equal (TYPE_ARG_TYPES (a->type),
6867 TYPE_ARG_TYPES (b->type))))
6868 break;
6869 return 0;
6871 default:
6872 return 0;
6875 if (lang_hooks.types.type_hash_eq != NULL)
6876 return lang_hooks.types.type_hash_eq (a->type, b->type);
6878 return 1;
6881 /* Given TYPE, and HASHCODE its hash code, return the canonical
6882 object for an identical type if one already exists.
6883 Otherwise, return TYPE, and record it as the canonical object.
6885 To use this function, first create a type of the sort you want.
6886 Then compute its hash code from the fields of the type that
6887 make it different from other similar types.
6888 Then call this function and use the value. */
6890 tree
6891 type_hash_canon (unsigned int hashcode, tree type)
6893 type_hash in;
6894 type_hash **loc;
6896 /* The hash table only contains main variants, so ensure that's what we're
6897 being passed. */
6898 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6900 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6901 must call that routine before comparing TYPE_ALIGNs. */
6902 layout_type (type);
6904 in.hash = hashcode;
6905 in.type = type;
6907 loc = type_hash_table->find_slot_with_hash (&in, hashcode, INSERT);
6908 if (*loc)
6910 tree t1 = ((type_hash *) *loc)->type;
6911 gcc_assert (TYPE_MAIN_VARIANT (t1) == t1);
6912 if (GATHER_STATISTICS)
6914 tree_code_counts[(int) TREE_CODE (type)]--;
6915 tree_node_counts[(int) t_kind]--;
6916 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
6918 return t1;
6920 else
6922 struct type_hash *h;
6924 h = ggc_alloc<type_hash> ();
6925 h->hash = hashcode;
6926 h->type = type;
6927 *loc = h;
6929 return type;
6933 static void
6934 print_type_hash_statistics (void)
6936 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6937 (long) type_hash_table->size (),
6938 (long) type_hash_table->elements (),
6939 type_hash_table->collisions ());
6942 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
6943 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
6944 by adding the hash codes of the individual attributes. */
6946 static void
6947 attribute_hash_list (const_tree list, inchash::hash &hstate)
6949 const_tree tail;
6951 for (tail = list; tail; tail = TREE_CHAIN (tail))
6952 /* ??? Do we want to add in TREE_VALUE too? */
6953 hstate.add_object (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)));
6956 /* Given two lists of attributes, return true if list l2 is
6957 equivalent to l1. */
6960 attribute_list_equal (const_tree l1, const_tree l2)
6962 if (l1 == l2)
6963 return 1;
6965 return attribute_list_contained (l1, l2)
6966 && attribute_list_contained (l2, l1);
6969 /* Given two lists of attributes, return true if list L2 is
6970 completely contained within L1. */
6971 /* ??? This would be faster if attribute names were stored in a canonicalized
6972 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
6973 must be used to show these elements are equivalent (which they are). */
6974 /* ??? It's not clear that attributes with arguments will always be handled
6975 correctly. */
6978 attribute_list_contained (const_tree l1, const_tree l2)
6980 const_tree t1, t2;
6982 /* First check the obvious, maybe the lists are identical. */
6983 if (l1 == l2)
6984 return 1;
6986 /* Maybe the lists are similar. */
6987 for (t1 = l1, t2 = l2;
6988 t1 != 0 && t2 != 0
6989 && get_attribute_name (t1) == get_attribute_name (t2)
6990 && TREE_VALUE (t1) == TREE_VALUE (t2);
6991 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6994 /* Maybe the lists are equal. */
6995 if (t1 == 0 && t2 == 0)
6996 return 1;
6998 for (; t2 != 0; t2 = TREE_CHAIN (t2))
7000 const_tree attr;
7001 /* This CONST_CAST is okay because lookup_attribute does not
7002 modify its argument and the return value is assigned to a
7003 const_tree. */
7004 for (attr = lookup_ident_attribute (get_attribute_name (t2),
7005 CONST_CAST_TREE (l1));
7006 attr != NULL_TREE && !attribute_value_equal (t2, attr);
7007 attr = lookup_ident_attribute (get_attribute_name (t2),
7008 TREE_CHAIN (attr)))
7011 if (attr == NULL_TREE)
7012 return 0;
7015 return 1;
7018 /* Given two lists of types
7019 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
7020 return 1 if the lists contain the same types in the same order.
7021 Also, the TREE_PURPOSEs must match. */
7024 type_list_equal (const_tree l1, const_tree l2)
7026 const_tree t1, t2;
7028 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7029 if (TREE_VALUE (t1) != TREE_VALUE (t2)
7030 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
7031 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
7032 && (TREE_TYPE (TREE_PURPOSE (t1))
7033 == TREE_TYPE (TREE_PURPOSE (t2))))))
7034 return 0;
7036 return t1 == t2;
7039 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
7040 given by TYPE. If the argument list accepts variable arguments,
7041 then this function counts only the ordinary arguments. */
7044 type_num_arguments (const_tree type)
7046 int i = 0;
7047 tree t;
7049 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
7050 /* If the function does not take a variable number of arguments,
7051 the last element in the list will have type `void'. */
7052 if (VOID_TYPE_P (TREE_VALUE (t)))
7053 break;
7054 else
7055 ++i;
7057 return i;
7060 /* Nonzero if integer constants T1 and T2
7061 represent the same constant value. */
7064 tree_int_cst_equal (const_tree t1, const_tree t2)
7066 if (t1 == t2)
7067 return 1;
7069 if (t1 == 0 || t2 == 0)
7070 return 0;
7072 if (TREE_CODE (t1) == INTEGER_CST
7073 && TREE_CODE (t2) == INTEGER_CST
7074 && wi::to_widest (t1) == wi::to_widest (t2))
7075 return 1;
7077 return 0;
7080 /* Return true if T is an INTEGER_CST whose numerical value (extended
7081 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
7083 bool
7084 tree_fits_shwi_p (const_tree t)
7086 return (t != NULL_TREE
7087 && TREE_CODE (t) == INTEGER_CST
7088 && wi::fits_shwi_p (wi::to_widest (t)));
7091 /* Return true if T is an INTEGER_CST whose numerical value (extended
7092 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
7094 bool
7095 tree_fits_uhwi_p (const_tree t)
7097 return (t != NULL_TREE
7098 && TREE_CODE (t) == INTEGER_CST
7099 && wi::fits_uhwi_p (wi::to_widest (t)));
7102 /* T is an INTEGER_CST whose numerical value (extended according to
7103 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
7104 HOST_WIDE_INT. */
7106 HOST_WIDE_INT
7107 tree_to_shwi (const_tree t)
7109 gcc_assert (tree_fits_shwi_p (t));
7110 return TREE_INT_CST_LOW (t);
7113 /* T is an INTEGER_CST whose numerical value (extended according to
7114 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
7115 HOST_WIDE_INT. */
7117 unsigned HOST_WIDE_INT
7118 tree_to_uhwi (const_tree t)
7120 gcc_assert (tree_fits_uhwi_p (t));
7121 return TREE_INT_CST_LOW (t);
7124 /* Return the most significant (sign) bit of T. */
7127 tree_int_cst_sign_bit (const_tree t)
7129 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
7131 return wi::extract_uhwi (t, bitno, 1);
7134 /* Return an indication of the sign of the integer constant T.
7135 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
7136 Note that -1 will never be returned if T's type is unsigned. */
7139 tree_int_cst_sgn (const_tree t)
7141 if (wi::eq_p (t, 0))
7142 return 0;
7143 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
7144 return 1;
7145 else if (wi::neg_p (t))
7146 return -1;
7147 else
7148 return 1;
7151 /* Return the minimum number of bits needed to represent VALUE in a
7152 signed or unsigned type, UNSIGNEDP says which. */
7154 unsigned int
7155 tree_int_cst_min_precision (tree value, signop sgn)
7157 /* If the value is negative, compute its negative minus 1. The latter
7158 adjustment is because the absolute value of the largest negative value
7159 is one larger than the largest positive value. This is equivalent to
7160 a bit-wise negation, so use that operation instead. */
7162 if (tree_int_cst_sgn (value) < 0)
7163 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
7165 /* Return the number of bits needed, taking into account the fact
7166 that we need one more bit for a signed than unsigned type.
7167 If value is 0 or -1, the minimum precision is 1 no matter
7168 whether unsignedp is true or false. */
7170 if (integer_zerop (value))
7171 return 1;
7172 else
7173 return tree_floor_log2 (value) + 1 + (sgn == SIGNED ? 1 : 0) ;
7176 /* Return truthvalue of whether T1 is the same tree structure as T2.
7177 Return 1 if they are the same.
7178 Return 0 if they are understandably different.
7179 Return -1 if either contains tree structure not understood by
7180 this function. */
7183 simple_cst_equal (const_tree t1, const_tree t2)
7185 enum tree_code code1, code2;
7186 int cmp;
7187 int i;
7189 if (t1 == t2)
7190 return 1;
7191 if (t1 == 0 || t2 == 0)
7192 return 0;
7194 code1 = TREE_CODE (t1);
7195 code2 = TREE_CODE (t2);
7197 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
7199 if (CONVERT_EXPR_CODE_P (code2)
7200 || code2 == NON_LVALUE_EXPR)
7201 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7202 else
7203 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
7206 else if (CONVERT_EXPR_CODE_P (code2)
7207 || code2 == NON_LVALUE_EXPR)
7208 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
7210 if (code1 != code2)
7211 return 0;
7213 switch (code1)
7215 case INTEGER_CST:
7216 return wi::to_widest (t1) == wi::to_widest (t2);
7218 case REAL_CST:
7219 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
7221 case FIXED_CST:
7222 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
7224 case STRING_CST:
7225 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
7226 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
7227 TREE_STRING_LENGTH (t1)));
7229 case CONSTRUCTOR:
7231 unsigned HOST_WIDE_INT idx;
7232 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
7233 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
7235 if (vec_safe_length (v1) != vec_safe_length (v2))
7236 return false;
7238 for (idx = 0; idx < vec_safe_length (v1); ++idx)
7239 /* ??? Should we handle also fields here? */
7240 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
7241 return false;
7242 return true;
7245 case SAVE_EXPR:
7246 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7248 case CALL_EXPR:
7249 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
7250 if (cmp <= 0)
7251 return cmp;
7252 if (call_expr_nargs (t1) != call_expr_nargs (t2))
7253 return 0;
7255 const_tree arg1, arg2;
7256 const_call_expr_arg_iterator iter1, iter2;
7257 for (arg1 = first_const_call_expr_arg (t1, &iter1),
7258 arg2 = first_const_call_expr_arg (t2, &iter2);
7259 arg1 && arg2;
7260 arg1 = next_const_call_expr_arg (&iter1),
7261 arg2 = next_const_call_expr_arg (&iter2))
7263 cmp = simple_cst_equal (arg1, arg2);
7264 if (cmp <= 0)
7265 return cmp;
7267 return arg1 == arg2;
7270 case TARGET_EXPR:
7271 /* Special case: if either target is an unallocated VAR_DECL,
7272 it means that it's going to be unified with whatever the
7273 TARGET_EXPR is really supposed to initialize, so treat it
7274 as being equivalent to anything. */
7275 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
7276 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
7277 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
7278 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
7279 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
7280 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
7281 cmp = 1;
7282 else
7283 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7285 if (cmp <= 0)
7286 return cmp;
7288 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
7290 case WITH_CLEANUP_EXPR:
7291 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7292 if (cmp <= 0)
7293 return cmp;
7295 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
7297 case COMPONENT_REF:
7298 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
7299 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7301 return 0;
7303 case VAR_DECL:
7304 case PARM_DECL:
7305 case CONST_DECL:
7306 case FUNCTION_DECL:
7307 return 0;
7309 default:
7310 break;
7313 /* This general rule works for most tree codes. All exceptions should be
7314 handled above. If this is a language-specific tree code, we can't
7315 trust what might be in the operand, so say we don't know
7316 the situation. */
7317 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
7318 return -1;
7320 switch (TREE_CODE_CLASS (code1))
7322 case tcc_unary:
7323 case tcc_binary:
7324 case tcc_comparison:
7325 case tcc_expression:
7326 case tcc_reference:
7327 case tcc_statement:
7328 cmp = 1;
7329 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
7331 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
7332 if (cmp <= 0)
7333 return cmp;
7336 return cmp;
7338 default:
7339 return -1;
7343 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
7344 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
7345 than U, respectively. */
7348 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
7350 if (tree_int_cst_sgn (t) < 0)
7351 return -1;
7352 else if (!tree_fits_uhwi_p (t))
7353 return 1;
7354 else if (TREE_INT_CST_LOW (t) == u)
7355 return 0;
7356 else if (TREE_INT_CST_LOW (t) < u)
7357 return -1;
7358 else
7359 return 1;
7362 /* Return true if SIZE represents a constant size that is in bounds of
7363 what the middle-end and the backend accepts (covering not more than
7364 half of the address-space). */
7366 bool
7367 valid_constant_size_p (const_tree size)
7369 if (! tree_fits_uhwi_p (size)
7370 || TREE_OVERFLOW (size)
7371 || tree_int_cst_sign_bit (size) != 0)
7372 return false;
7373 return true;
7376 /* Return the precision of the type, or for a complex or vector type the
7377 precision of the type of its elements. */
7379 unsigned int
7380 element_precision (const_tree type)
7382 enum tree_code code = TREE_CODE (type);
7383 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
7384 type = TREE_TYPE (type);
7386 return TYPE_PRECISION (type);
7389 /* Return true if CODE represents an associative tree code. Otherwise
7390 return false. */
7391 bool
7392 associative_tree_code (enum tree_code code)
7394 switch (code)
7396 case BIT_IOR_EXPR:
7397 case BIT_AND_EXPR:
7398 case BIT_XOR_EXPR:
7399 case PLUS_EXPR:
7400 case MULT_EXPR:
7401 case MIN_EXPR:
7402 case MAX_EXPR:
7403 return true;
7405 default:
7406 break;
7408 return false;
7411 /* Return true if CODE represents a commutative tree code. Otherwise
7412 return false. */
7413 bool
7414 commutative_tree_code (enum tree_code code)
7416 switch (code)
7418 case PLUS_EXPR:
7419 case MULT_EXPR:
7420 case MULT_HIGHPART_EXPR:
7421 case MIN_EXPR:
7422 case MAX_EXPR:
7423 case BIT_IOR_EXPR:
7424 case BIT_XOR_EXPR:
7425 case BIT_AND_EXPR:
7426 case NE_EXPR:
7427 case EQ_EXPR:
7428 case UNORDERED_EXPR:
7429 case ORDERED_EXPR:
7430 case UNEQ_EXPR:
7431 case LTGT_EXPR:
7432 case TRUTH_AND_EXPR:
7433 case TRUTH_XOR_EXPR:
7434 case TRUTH_OR_EXPR:
7435 case WIDEN_MULT_EXPR:
7436 case VEC_WIDEN_MULT_HI_EXPR:
7437 case VEC_WIDEN_MULT_LO_EXPR:
7438 case VEC_WIDEN_MULT_EVEN_EXPR:
7439 case VEC_WIDEN_MULT_ODD_EXPR:
7440 return true;
7442 default:
7443 break;
7445 return false;
7448 /* Return true if CODE represents a ternary tree code for which the
7449 first two operands are commutative. Otherwise return false. */
7450 bool
7451 commutative_ternary_tree_code (enum tree_code code)
7453 switch (code)
7455 case WIDEN_MULT_PLUS_EXPR:
7456 case WIDEN_MULT_MINUS_EXPR:
7457 case DOT_PROD_EXPR:
7458 case FMA_EXPR:
7459 return true;
7461 default:
7462 break;
7464 return false;
7467 namespace inchash
7470 /* Generate a hash value for an expression. This can be used iteratively
7471 by passing a previous result as the HSTATE argument.
7473 This function is intended to produce the same hash for expressions which
7474 would compare equal using operand_equal_p. */
7475 void
7476 add_expr (const_tree t, inchash::hash &hstate)
7478 int i;
7479 enum tree_code code;
7480 enum tree_code_class tclass;
7482 if (t == NULL_TREE)
7484 hstate.merge_hash (0);
7485 return;
7488 code = TREE_CODE (t);
7490 switch (code)
7492 /* Alas, constants aren't shared, so we can't rely on pointer
7493 identity. */
7494 case VOID_CST:
7495 hstate.merge_hash (0);
7496 return;
7497 case INTEGER_CST:
7498 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
7499 hstate.add_wide_int (TREE_INT_CST_ELT (t, i));
7500 return;
7501 case REAL_CST:
7503 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7504 hstate.merge_hash (val2);
7505 return;
7507 case FIXED_CST:
7509 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7510 hstate.merge_hash (val2);
7511 return;
7513 case STRING_CST:
7514 hstate.add ((const void *) TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
7515 return;
7516 case COMPLEX_CST:
7517 inchash::add_expr (TREE_REALPART (t), hstate);
7518 inchash::add_expr (TREE_IMAGPART (t), hstate);
7519 return;
7520 case VECTOR_CST:
7522 unsigned i;
7523 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7524 inchash::add_expr (VECTOR_CST_ELT (t, i), hstate);
7525 return;
7527 case SSA_NAME:
7528 /* We can just compare by pointer. */
7529 hstate.add_wide_int (SSA_NAME_VERSION (t));
7530 return;
7531 case PLACEHOLDER_EXPR:
7532 /* The node itself doesn't matter. */
7533 return;
7534 case TREE_LIST:
7535 /* A list of expressions, for a CALL_EXPR or as the elements of a
7536 VECTOR_CST. */
7537 for (; t; t = TREE_CHAIN (t))
7538 inchash::add_expr (TREE_VALUE (t), hstate);
7539 return;
7540 case CONSTRUCTOR:
7542 unsigned HOST_WIDE_INT idx;
7543 tree field, value;
7544 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7546 inchash::add_expr (field, hstate);
7547 inchash::add_expr (value, hstate);
7549 return;
7551 case FUNCTION_DECL:
7552 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7553 Otherwise nodes that compare equal according to operand_equal_p might
7554 get different hash codes. However, don't do this for machine specific
7555 or front end builtins, since the function code is overloaded in those
7556 cases. */
7557 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7558 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7560 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7561 code = TREE_CODE (t);
7563 /* FALL THROUGH */
7564 default:
7565 tclass = TREE_CODE_CLASS (code);
7567 if (tclass == tcc_declaration)
7569 /* DECL's have a unique ID */
7570 hstate.add_wide_int (DECL_UID (t));
7572 else
7574 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7576 hstate.add_object (code);
7578 /* Don't hash the type, that can lead to having nodes which
7579 compare equal according to operand_equal_p, but which
7580 have different hash codes. */
7581 if (CONVERT_EXPR_CODE_P (code)
7582 || code == NON_LVALUE_EXPR)
7584 /* Make sure to include signness in the hash computation. */
7585 hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
7586 inchash::add_expr (TREE_OPERAND (t, 0), hstate);
7589 else if (commutative_tree_code (code))
7591 /* It's a commutative expression. We want to hash it the same
7592 however it appears. We do this by first hashing both operands
7593 and then rehashing based on the order of their independent
7594 hashes. */
7595 inchash::hash one, two;
7596 inchash::add_expr (TREE_OPERAND (t, 0), one);
7597 inchash::add_expr (TREE_OPERAND (t, 1), two);
7598 hstate.add_commutative (one, two);
7600 else
7601 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7602 inchash::add_expr (TREE_OPERAND (t, i), hstate);
7604 return;
7610 /* Constructors for pointer, array and function types.
7611 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7612 constructed by language-dependent code, not here.) */
7614 /* Construct, lay out and return the type of pointers to TO_TYPE with
7615 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7616 reference all of memory. If such a type has already been
7617 constructed, reuse it. */
7619 tree
7620 build_pointer_type_for_mode (tree to_type, machine_mode mode,
7621 bool can_alias_all)
7623 tree t;
7625 if (to_type == error_mark_node)
7626 return error_mark_node;
7628 /* If the pointed-to type has the may_alias attribute set, force
7629 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7630 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7631 can_alias_all = true;
7633 /* In some cases, languages will have things that aren't a POINTER_TYPE
7634 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7635 In that case, return that type without regard to the rest of our
7636 operands.
7638 ??? This is a kludge, but consistent with the way this function has
7639 always operated and there doesn't seem to be a good way to avoid this
7640 at the moment. */
7641 if (TYPE_POINTER_TO (to_type) != 0
7642 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7643 return TYPE_POINTER_TO (to_type);
7645 /* First, if we already have a type for pointers to TO_TYPE and it's
7646 the proper mode, use it. */
7647 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7648 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7649 return t;
7651 t = make_node (POINTER_TYPE);
7653 TREE_TYPE (t) = to_type;
7654 SET_TYPE_MODE (t, mode);
7655 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7656 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7657 TYPE_POINTER_TO (to_type) = t;
7659 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7660 SET_TYPE_STRUCTURAL_EQUALITY (t);
7661 else if (TYPE_CANONICAL (to_type) != to_type)
7662 TYPE_CANONICAL (t)
7663 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7664 mode, can_alias_all);
7666 /* Lay out the type. This function has many callers that are concerned
7667 with expression-construction, and this simplifies them all. */
7668 layout_type (t);
7670 return t;
7673 /* By default build pointers in ptr_mode. */
7675 tree
7676 build_pointer_type (tree to_type)
7678 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7679 : TYPE_ADDR_SPACE (to_type);
7680 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7681 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7684 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7686 tree
7687 build_reference_type_for_mode (tree to_type, machine_mode mode,
7688 bool can_alias_all)
7690 tree t;
7692 if (to_type == error_mark_node)
7693 return error_mark_node;
7695 /* If the pointed-to type has the may_alias attribute set, force
7696 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7697 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7698 can_alias_all = true;
7700 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7701 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7702 In that case, return that type without regard to the rest of our
7703 operands.
7705 ??? This is a kludge, but consistent with the way this function has
7706 always operated and there doesn't seem to be a good way to avoid this
7707 at the moment. */
7708 if (TYPE_REFERENCE_TO (to_type) != 0
7709 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7710 return TYPE_REFERENCE_TO (to_type);
7712 /* First, if we already have a type for pointers to TO_TYPE and it's
7713 the proper mode, use it. */
7714 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7715 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7716 return t;
7718 t = make_node (REFERENCE_TYPE);
7720 TREE_TYPE (t) = to_type;
7721 SET_TYPE_MODE (t, mode);
7722 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7723 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7724 TYPE_REFERENCE_TO (to_type) = t;
7726 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7727 SET_TYPE_STRUCTURAL_EQUALITY (t);
7728 else if (TYPE_CANONICAL (to_type) != to_type)
7729 TYPE_CANONICAL (t)
7730 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7731 mode, can_alias_all);
7733 layout_type (t);
7735 return t;
7739 /* Build the node for the type of references-to-TO_TYPE by default
7740 in ptr_mode. */
7742 tree
7743 build_reference_type (tree to_type)
7745 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7746 : TYPE_ADDR_SPACE (to_type);
7747 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7748 return build_reference_type_for_mode (to_type, pointer_mode, false);
7751 #define MAX_INT_CACHED_PREC \
7752 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7753 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7755 /* Builds a signed or unsigned integer type of precision PRECISION.
7756 Used for C bitfields whose precision does not match that of
7757 built-in target types. */
7758 tree
7759 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7760 int unsignedp)
7762 tree itype, ret;
7764 if (unsignedp)
7765 unsignedp = MAX_INT_CACHED_PREC + 1;
7767 if (precision <= MAX_INT_CACHED_PREC)
7769 itype = nonstandard_integer_type_cache[precision + unsignedp];
7770 if (itype)
7771 return itype;
7774 itype = make_node (INTEGER_TYPE);
7775 TYPE_PRECISION (itype) = precision;
7777 if (unsignedp)
7778 fixup_unsigned_type (itype);
7779 else
7780 fixup_signed_type (itype);
7782 ret = itype;
7783 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (itype)))
7784 ret = type_hash_canon (tree_to_uhwi (TYPE_MAX_VALUE (itype)), itype);
7785 if (precision <= MAX_INT_CACHED_PREC)
7786 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7788 return ret;
7791 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7792 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7793 is true, reuse such a type that has already been constructed. */
7795 static tree
7796 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7798 tree itype = make_node (INTEGER_TYPE);
7799 inchash::hash hstate;
7801 TREE_TYPE (itype) = type;
7803 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7804 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7806 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7807 SET_TYPE_MODE (itype, TYPE_MODE (type));
7808 TYPE_SIZE (itype) = TYPE_SIZE (type);
7809 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7810 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7811 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7813 if (!shared)
7814 return itype;
7816 if ((TYPE_MIN_VALUE (itype)
7817 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7818 || (TYPE_MAX_VALUE (itype)
7819 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7821 /* Since we cannot reliably merge this type, we need to compare it using
7822 structural equality checks. */
7823 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7824 return itype;
7827 inchash::add_expr (TYPE_MIN_VALUE (itype), hstate);
7828 inchash::add_expr (TYPE_MAX_VALUE (itype), hstate);
7829 hstate.merge_hash (TYPE_HASH (type));
7830 itype = type_hash_canon (hstate.end (), itype);
7832 return itype;
7835 /* Wrapper around build_range_type_1 with SHARED set to true. */
7837 tree
7838 build_range_type (tree type, tree lowval, tree highval)
7840 return build_range_type_1 (type, lowval, highval, true);
7843 /* Wrapper around build_range_type_1 with SHARED set to false. */
7845 tree
7846 build_nonshared_range_type (tree type, tree lowval, tree highval)
7848 return build_range_type_1 (type, lowval, highval, false);
7851 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7852 MAXVAL should be the maximum value in the domain
7853 (one less than the length of the array).
7855 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7856 We don't enforce this limit, that is up to caller (e.g. language front end).
7857 The limit exists because the result is a signed type and we don't handle
7858 sizes that use more than one HOST_WIDE_INT. */
7860 tree
7861 build_index_type (tree maxval)
7863 return build_range_type (sizetype, size_zero_node, maxval);
7866 /* Return true if the debug information for TYPE, a subtype, should be emitted
7867 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7868 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7869 debug info and doesn't reflect the source code. */
7871 bool
7872 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7874 tree base_type = TREE_TYPE (type), low, high;
7876 /* Subrange types have a base type which is an integral type. */
7877 if (!INTEGRAL_TYPE_P (base_type))
7878 return false;
7880 /* Get the real bounds of the subtype. */
7881 if (lang_hooks.types.get_subrange_bounds)
7882 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7883 else
7885 low = TYPE_MIN_VALUE (type);
7886 high = TYPE_MAX_VALUE (type);
7889 /* If the type and its base type have the same representation and the same
7890 name, then the type is not a subrange but a copy of the base type. */
7891 if ((TREE_CODE (base_type) == INTEGER_TYPE
7892 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7893 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7894 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7895 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type))
7896 && TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (base_type))
7897 return false;
7899 if (lowval)
7900 *lowval = low;
7901 if (highval)
7902 *highval = high;
7903 return true;
7906 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7907 and number of elements specified by the range of values of INDEX_TYPE.
7908 If SHARED is true, reuse such a type that has already been constructed. */
7910 static tree
7911 build_array_type_1 (tree elt_type, tree index_type, bool shared)
7913 tree t;
7915 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7917 error ("arrays of functions are not meaningful");
7918 elt_type = integer_type_node;
7921 t = make_node (ARRAY_TYPE);
7922 TREE_TYPE (t) = elt_type;
7923 TYPE_DOMAIN (t) = index_type;
7924 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7925 layout_type (t);
7927 /* If the element type is incomplete at this point we get marked for
7928 structural equality. Do not record these types in the canonical
7929 type hashtable. */
7930 if (TYPE_STRUCTURAL_EQUALITY_P (t))
7931 return t;
7933 if (shared)
7935 inchash::hash hstate;
7936 hstate.add_object (TYPE_HASH (elt_type));
7937 if (index_type)
7938 hstate.add_object (TYPE_HASH (index_type));
7939 t = type_hash_canon (hstate.end (), t);
7942 if (TYPE_CANONICAL (t) == t)
7944 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7945 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7946 SET_TYPE_STRUCTURAL_EQUALITY (t);
7947 else if (TYPE_CANONICAL (elt_type) != elt_type
7948 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7949 TYPE_CANONICAL (t)
7950 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7951 index_type
7952 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7953 shared);
7956 return t;
7959 /* Wrapper around build_array_type_1 with SHARED set to true. */
7961 tree
7962 build_array_type (tree elt_type, tree index_type)
7964 return build_array_type_1 (elt_type, index_type, true);
7967 /* Wrapper around build_array_type_1 with SHARED set to false. */
7969 tree
7970 build_nonshared_array_type (tree elt_type, tree index_type)
7972 return build_array_type_1 (elt_type, index_type, false);
7975 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7976 sizetype. */
7978 tree
7979 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
7981 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7984 /* Recursively examines the array elements of TYPE, until a non-array
7985 element type is found. */
7987 tree
7988 strip_array_types (tree type)
7990 while (TREE_CODE (type) == ARRAY_TYPE)
7991 type = TREE_TYPE (type);
7993 return type;
7996 /* Computes the canonical argument types from the argument type list
7997 ARGTYPES.
7999 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
8000 on entry to this function, or if any of the ARGTYPES are
8001 structural.
8003 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
8004 true on entry to this function, or if any of the ARGTYPES are
8005 non-canonical.
8007 Returns a canonical argument list, which may be ARGTYPES when the
8008 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
8009 true) or would not differ from ARGTYPES. */
8011 static tree
8012 maybe_canonicalize_argtypes (tree argtypes,
8013 bool *any_structural_p,
8014 bool *any_noncanonical_p)
8016 tree arg;
8017 bool any_noncanonical_argtypes_p = false;
8019 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
8021 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
8022 /* Fail gracefully by stating that the type is structural. */
8023 *any_structural_p = true;
8024 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
8025 *any_structural_p = true;
8026 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
8027 || TREE_PURPOSE (arg))
8028 /* If the argument has a default argument, we consider it
8029 non-canonical even though the type itself is canonical.
8030 That way, different variants of function and method types
8031 with default arguments will all point to the variant with
8032 no defaults as their canonical type. */
8033 any_noncanonical_argtypes_p = true;
8036 if (*any_structural_p)
8037 return argtypes;
8039 if (any_noncanonical_argtypes_p)
8041 /* Build the canonical list of argument types. */
8042 tree canon_argtypes = NULL_TREE;
8043 bool is_void = false;
8045 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
8047 if (arg == void_list_node)
8048 is_void = true;
8049 else
8050 canon_argtypes = tree_cons (NULL_TREE,
8051 TYPE_CANONICAL (TREE_VALUE (arg)),
8052 canon_argtypes);
8055 canon_argtypes = nreverse (canon_argtypes);
8056 if (is_void)
8057 canon_argtypes = chainon (canon_argtypes, void_list_node);
8059 /* There is a non-canonical type. */
8060 *any_noncanonical_p = true;
8061 return canon_argtypes;
8064 /* The canonical argument types are the same as ARGTYPES. */
8065 return argtypes;
8068 /* Construct, lay out and return
8069 the type of functions returning type VALUE_TYPE
8070 given arguments of types ARG_TYPES.
8071 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
8072 are data type nodes for the arguments of the function.
8073 If such a type has already been constructed, reuse it. */
8075 tree
8076 build_function_type (tree value_type, tree arg_types)
8078 tree t;
8079 inchash::hash hstate;
8080 bool any_structural_p, any_noncanonical_p;
8081 tree canon_argtypes;
8083 if (TREE_CODE (value_type) == FUNCTION_TYPE)
8085 error ("function return type cannot be function");
8086 value_type = integer_type_node;
8089 /* Make a node of the sort we want. */
8090 t = make_node (FUNCTION_TYPE);
8091 TREE_TYPE (t) = value_type;
8092 TYPE_ARG_TYPES (t) = arg_types;
8094 /* If we already have such a type, use the old one. */
8095 hstate.add_object (TYPE_HASH (value_type));
8096 type_hash_list (arg_types, hstate);
8097 t = type_hash_canon (hstate.end (), t);
8099 /* Set up the canonical type. */
8100 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
8101 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
8102 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
8103 &any_structural_p,
8104 &any_noncanonical_p);
8105 if (any_structural_p)
8106 SET_TYPE_STRUCTURAL_EQUALITY (t);
8107 else if (any_noncanonical_p)
8108 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
8109 canon_argtypes);
8111 if (!COMPLETE_TYPE_P (t))
8112 layout_type (t);
8113 return t;
8116 /* Build a function type. The RETURN_TYPE is the type returned by the
8117 function. If VAARGS is set, no void_type_node is appended to the
8118 the list. ARGP must be always be terminated be a NULL_TREE. */
8120 static tree
8121 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
8123 tree t, args, last;
8125 t = va_arg (argp, tree);
8126 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
8127 args = tree_cons (NULL_TREE, t, args);
8129 if (vaargs)
8131 last = args;
8132 if (args != NULL_TREE)
8133 args = nreverse (args);
8134 gcc_assert (last != void_list_node);
8136 else if (args == NULL_TREE)
8137 args = void_list_node;
8138 else
8140 last = args;
8141 args = nreverse (args);
8142 TREE_CHAIN (last) = void_list_node;
8144 args = build_function_type (return_type, args);
8146 return args;
8149 /* Build a function type. The RETURN_TYPE is the type returned by the
8150 function. If additional arguments are provided, they are
8151 additional argument types. The list of argument types must always
8152 be terminated by NULL_TREE. */
8154 tree
8155 build_function_type_list (tree return_type, ...)
8157 tree args;
8158 va_list p;
8160 va_start (p, return_type);
8161 args = build_function_type_list_1 (false, return_type, p);
8162 va_end (p);
8163 return args;
8166 /* Build a variable argument function type. The RETURN_TYPE is the
8167 type returned by the function. If additional arguments are provided,
8168 they are additional argument types. The list of argument types must
8169 always be terminated by NULL_TREE. */
8171 tree
8172 build_varargs_function_type_list (tree return_type, ...)
8174 tree args;
8175 va_list p;
8177 va_start (p, return_type);
8178 args = build_function_type_list_1 (true, return_type, p);
8179 va_end (p);
8181 return args;
8184 /* Build a function type. RETURN_TYPE is the type returned by the
8185 function; VAARGS indicates whether the function takes varargs. The
8186 function takes N named arguments, the types of which are provided in
8187 ARG_TYPES. */
8189 static tree
8190 build_function_type_array_1 (bool vaargs, tree return_type, int n,
8191 tree *arg_types)
8193 int i;
8194 tree t = vaargs ? NULL_TREE : void_list_node;
8196 for (i = n - 1; i >= 0; i--)
8197 t = tree_cons (NULL_TREE, arg_types[i], t);
8199 return build_function_type (return_type, t);
8202 /* Build a function type. RETURN_TYPE is the type returned by the
8203 function. The function takes N named arguments, the types of which
8204 are provided in ARG_TYPES. */
8206 tree
8207 build_function_type_array (tree return_type, int n, tree *arg_types)
8209 return build_function_type_array_1 (false, return_type, n, arg_types);
8212 /* Build a variable argument function type. RETURN_TYPE is the type
8213 returned by the function. The function takes N named arguments, the
8214 types of which are provided in ARG_TYPES. */
8216 tree
8217 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8219 return build_function_type_array_1 (true, return_type, n, arg_types);
8222 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8223 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8224 for the method. An implicit additional parameter (of type
8225 pointer-to-BASETYPE) is added to the ARGTYPES. */
8227 tree
8228 build_method_type_directly (tree basetype,
8229 tree rettype,
8230 tree argtypes)
8232 tree t;
8233 tree ptype;
8234 inchash::hash hstate;
8235 bool any_structural_p, any_noncanonical_p;
8236 tree canon_argtypes;
8238 /* Make a node of the sort we want. */
8239 t = make_node (METHOD_TYPE);
8241 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8242 TREE_TYPE (t) = rettype;
8243 ptype = build_pointer_type (basetype);
8245 /* The actual arglist for this function includes a "hidden" argument
8246 which is "this". Put it into the list of argument types. */
8247 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8248 TYPE_ARG_TYPES (t) = argtypes;
8250 /* If we already have such a type, use the old one. */
8251 hstate.add_object (TYPE_HASH (basetype));
8252 hstate.add_object (TYPE_HASH (rettype));
8253 type_hash_list (argtypes, hstate);
8254 t = type_hash_canon (hstate.end (), t);
8256 /* Set up the canonical type. */
8257 any_structural_p
8258 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8259 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8260 any_noncanonical_p
8261 = (TYPE_CANONICAL (basetype) != basetype
8262 || TYPE_CANONICAL (rettype) != rettype);
8263 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8264 &any_structural_p,
8265 &any_noncanonical_p);
8266 if (any_structural_p)
8267 SET_TYPE_STRUCTURAL_EQUALITY (t);
8268 else if (any_noncanonical_p)
8269 TYPE_CANONICAL (t)
8270 = build_method_type_directly (TYPE_CANONICAL (basetype),
8271 TYPE_CANONICAL (rettype),
8272 canon_argtypes);
8273 if (!COMPLETE_TYPE_P (t))
8274 layout_type (t);
8276 return t;
8279 /* Construct, lay out and return the type of methods belonging to class
8280 BASETYPE and whose arguments and values are described by TYPE.
8281 If that type exists already, reuse it.
8282 TYPE must be a FUNCTION_TYPE node. */
8284 tree
8285 build_method_type (tree basetype, tree type)
8287 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8289 return build_method_type_directly (basetype,
8290 TREE_TYPE (type),
8291 TYPE_ARG_TYPES (type));
8294 /* Construct, lay out and return the type of offsets to a value
8295 of type TYPE, within an object of type BASETYPE.
8296 If a suitable offset type exists already, reuse it. */
8298 tree
8299 build_offset_type (tree basetype, tree type)
8301 tree t;
8302 inchash::hash hstate;
8304 /* Make a node of the sort we want. */
8305 t = make_node (OFFSET_TYPE);
8307 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8308 TREE_TYPE (t) = type;
8310 /* If we already have such a type, use the old one. */
8311 hstate.add_object (TYPE_HASH (basetype));
8312 hstate.add_object (TYPE_HASH (type));
8313 t = type_hash_canon (hstate.end (), t);
8315 if (!COMPLETE_TYPE_P (t))
8316 layout_type (t);
8318 if (TYPE_CANONICAL (t) == t)
8320 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8321 || TYPE_STRUCTURAL_EQUALITY_P (type))
8322 SET_TYPE_STRUCTURAL_EQUALITY (t);
8323 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8324 || TYPE_CANONICAL (type) != type)
8325 TYPE_CANONICAL (t)
8326 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8327 TYPE_CANONICAL (type));
8330 return t;
8333 /* Create a complex type whose components are COMPONENT_TYPE. */
8335 tree
8336 build_complex_type (tree component_type)
8338 tree t;
8339 inchash::hash hstate;
8341 gcc_assert (INTEGRAL_TYPE_P (component_type)
8342 || SCALAR_FLOAT_TYPE_P (component_type)
8343 || FIXED_POINT_TYPE_P (component_type));
8345 /* Make a node of the sort we want. */
8346 t = make_node (COMPLEX_TYPE);
8348 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8350 /* If we already have such a type, use the old one. */
8351 hstate.add_object (TYPE_HASH (component_type));
8352 t = type_hash_canon (hstate.end (), t);
8354 if (!COMPLETE_TYPE_P (t))
8355 layout_type (t);
8357 if (TYPE_CANONICAL (t) == t)
8359 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8360 SET_TYPE_STRUCTURAL_EQUALITY (t);
8361 else if (TYPE_CANONICAL (component_type) != component_type)
8362 TYPE_CANONICAL (t)
8363 = build_complex_type (TYPE_CANONICAL (component_type));
8366 /* We need to create a name, since complex is a fundamental type. */
8367 if (! TYPE_NAME (t))
8369 const char *name;
8370 if (component_type == char_type_node)
8371 name = "complex char";
8372 else if (component_type == signed_char_type_node)
8373 name = "complex signed char";
8374 else if (component_type == unsigned_char_type_node)
8375 name = "complex unsigned char";
8376 else if (component_type == short_integer_type_node)
8377 name = "complex short int";
8378 else if (component_type == short_unsigned_type_node)
8379 name = "complex short unsigned int";
8380 else if (component_type == integer_type_node)
8381 name = "complex int";
8382 else if (component_type == unsigned_type_node)
8383 name = "complex unsigned int";
8384 else if (component_type == long_integer_type_node)
8385 name = "complex long int";
8386 else if (component_type == long_unsigned_type_node)
8387 name = "complex long unsigned int";
8388 else if (component_type == long_long_integer_type_node)
8389 name = "complex long long int";
8390 else if (component_type == long_long_unsigned_type_node)
8391 name = "complex long long unsigned int";
8392 else
8393 name = 0;
8395 if (name != 0)
8396 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8397 get_identifier (name), t);
8400 return build_qualified_type (t, TYPE_QUALS (component_type));
8403 /* If TYPE is a real or complex floating-point type and the target
8404 does not directly support arithmetic on TYPE then return the wider
8405 type to be used for arithmetic on TYPE. Otherwise, return
8406 NULL_TREE. */
8408 tree
8409 excess_precision_type (tree type)
8411 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8413 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8414 switch (TREE_CODE (type))
8416 case REAL_TYPE:
8417 switch (flt_eval_method)
8419 case 1:
8420 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8421 return double_type_node;
8422 break;
8423 case 2:
8424 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8425 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8426 return long_double_type_node;
8427 break;
8428 default:
8429 gcc_unreachable ();
8431 break;
8432 case COMPLEX_TYPE:
8433 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8434 return NULL_TREE;
8435 switch (flt_eval_method)
8437 case 1:
8438 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8439 return complex_double_type_node;
8440 break;
8441 case 2:
8442 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8443 || (TYPE_MODE (TREE_TYPE (type))
8444 == TYPE_MODE (double_type_node)))
8445 return complex_long_double_type_node;
8446 break;
8447 default:
8448 gcc_unreachable ();
8450 break;
8451 default:
8452 break;
8455 return NULL_TREE;
8458 /* Return OP, stripped of any conversions to wider types as much as is safe.
8459 Converting the value back to OP's type makes a value equivalent to OP.
8461 If FOR_TYPE is nonzero, we return a value which, if converted to
8462 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8464 OP must have integer, real or enumeral type. Pointers are not allowed!
8466 There are some cases where the obvious value we could return
8467 would regenerate to OP if converted to OP's type,
8468 but would not extend like OP to wider types.
8469 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8470 For example, if OP is (unsigned short)(signed char)-1,
8471 we avoid returning (signed char)-1 if FOR_TYPE is int,
8472 even though extending that to an unsigned short would regenerate OP,
8473 since the result of extending (signed char)-1 to (int)
8474 is different from (int) OP. */
8476 tree
8477 get_unwidened (tree op, tree for_type)
8479 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8480 tree type = TREE_TYPE (op);
8481 unsigned final_prec
8482 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8483 int uns
8484 = (for_type != 0 && for_type != type
8485 && final_prec > TYPE_PRECISION (type)
8486 && TYPE_UNSIGNED (type));
8487 tree win = op;
8489 while (CONVERT_EXPR_P (op))
8491 int bitschange;
8493 /* TYPE_PRECISION on vector types has different meaning
8494 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8495 so avoid them here. */
8496 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8497 break;
8499 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8500 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8502 /* Truncations are many-one so cannot be removed.
8503 Unless we are later going to truncate down even farther. */
8504 if (bitschange < 0
8505 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8506 break;
8508 /* See what's inside this conversion. If we decide to strip it,
8509 we will set WIN. */
8510 op = TREE_OPERAND (op, 0);
8512 /* If we have not stripped any zero-extensions (uns is 0),
8513 we can strip any kind of extension.
8514 If we have previously stripped a zero-extension,
8515 only zero-extensions can safely be stripped.
8516 Any extension can be stripped if the bits it would produce
8517 are all going to be discarded later by truncating to FOR_TYPE. */
8519 if (bitschange > 0)
8521 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8522 win = op;
8523 /* TYPE_UNSIGNED says whether this is a zero-extension.
8524 Let's avoid computing it if it does not affect WIN
8525 and if UNS will not be needed again. */
8526 if ((uns
8527 || CONVERT_EXPR_P (op))
8528 && TYPE_UNSIGNED (TREE_TYPE (op)))
8530 uns = 1;
8531 win = op;
8536 /* If we finally reach a constant see if it fits in for_type and
8537 in that case convert it. */
8538 if (for_type
8539 && TREE_CODE (win) == INTEGER_CST
8540 && TREE_TYPE (win) != for_type
8541 && int_fits_type_p (win, for_type))
8542 win = fold_convert (for_type, win);
8544 return win;
8547 /* Return OP or a simpler expression for a narrower value
8548 which can be sign-extended or zero-extended to give back OP.
8549 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8550 or 0 if the value should be sign-extended. */
8552 tree
8553 get_narrower (tree op, int *unsignedp_ptr)
8555 int uns = 0;
8556 int first = 1;
8557 tree win = op;
8558 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8560 while (TREE_CODE (op) == NOP_EXPR)
8562 int bitschange
8563 = (TYPE_PRECISION (TREE_TYPE (op))
8564 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8566 /* Truncations are many-one so cannot be removed. */
8567 if (bitschange < 0)
8568 break;
8570 /* See what's inside this conversion. If we decide to strip it,
8571 we will set WIN. */
8573 if (bitschange > 0)
8575 op = TREE_OPERAND (op, 0);
8576 /* An extension: the outermost one can be stripped,
8577 but remember whether it is zero or sign extension. */
8578 if (first)
8579 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8580 /* Otherwise, if a sign extension has been stripped,
8581 only sign extensions can now be stripped;
8582 if a zero extension has been stripped, only zero-extensions. */
8583 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8584 break;
8585 first = 0;
8587 else /* bitschange == 0 */
8589 /* A change in nominal type can always be stripped, but we must
8590 preserve the unsignedness. */
8591 if (first)
8592 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8593 first = 0;
8594 op = TREE_OPERAND (op, 0);
8595 /* Keep trying to narrow, but don't assign op to win if it
8596 would turn an integral type into something else. */
8597 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8598 continue;
8601 win = op;
8604 if (TREE_CODE (op) == COMPONENT_REF
8605 /* Since type_for_size always gives an integer type. */
8606 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8607 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8608 /* Ensure field is laid out already. */
8609 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8610 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
8612 unsigned HOST_WIDE_INT innerprec
8613 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
8614 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8615 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8616 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8618 /* We can get this structure field in a narrower type that fits it,
8619 but the resulting extension to its nominal type (a fullword type)
8620 must satisfy the same conditions as for other extensions.
8622 Do this only for fields that are aligned (not bit-fields),
8623 because when bit-field insns will be used there is no
8624 advantage in doing this. */
8626 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8627 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8628 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8629 && type != 0)
8631 if (first)
8632 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8633 win = fold_convert (type, op);
8637 *unsignedp_ptr = uns;
8638 return win;
8641 /* Returns true if integer constant C has a value that is permissible
8642 for type TYPE (an INTEGER_TYPE). */
8644 bool
8645 int_fits_type_p (const_tree c, const_tree type)
8647 tree type_low_bound, type_high_bound;
8648 bool ok_for_low_bound, ok_for_high_bound;
8649 signop sgn_c = TYPE_SIGN (TREE_TYPE (c));
8651 retry:
8652 type_low_bound = TYPE_MIN_VALUE (type);
8653 type_high_bound = TYPE_MAX_VALUE (type);
8655 /* If at least one bound of the type is a constant integer, we can check
8656 ourselves and maybe make a decision. If no such decision is possible, but
8657 this type is a subtype, try checking against that. Otherwise, use
8658 fits_to_tree_p, which checks against the precision.
8660 Compute the status for each possibly constant bound, and return if we see
8661 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8662 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8663 for "constant known to fit". */
8665 /* Check if c >= type_low_bound. */
8666 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8668 if (tree_int_cst_lt (c, type_low_bound))
8669 return false;
8670 ok_for_low_bound = true;
8672 else
8673 ok_for_low_bound = false;
8675 /* Check if c <= type_high_bound. */
8676 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8678 if (tree_int_cst_lt (type_high_bound, c))
8679 return false;
8680 ok_for_high_bound = true;
8682 else
8683 ok_for_high_bound = false;
8685 /* If the constant fits both bounds, the result is known. */
8686 if (ok_for_low_bound && ok_for_high_bound)
8687 return true;
8689 /* Perform some generic filtering which may allow making a decision
8690 even if the bounds are not constant. First, negative integers
8691 never fit in unsigned types, */
8692 if (TYPE_UNSIGNED (type) && sgn_c == SIGNED && wi::neg_p (c))
8693 return false;
8695 /* Second, narrower types always fit in wider ones. */
8696 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8697 return true;
8699 /* Third, unsigned integers with top bit set never fit signed types. */
8700 if (!TYPE_UNSIGNED (type) && sgn_c == UNSIGNED)
8702 int prec = GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (c))) - 1;
8703 if (prec < TYPE_PRECISION (TREE_TYPE (c)))
8705 /* When a tree_cst is converted to a wide-int, the precision
8706 is taken from the type. However, if the precision of the
8707 mode underneath the type is smaller than that, it is
8708 possible that the value will not fit. The test below
8709 fails if any bit is set between the sign bit of the
8710 underlying mode and the top bit of the type. */
8711 if (wi::ne_p (wi::zext (c, prec - 1), c))
8712 return false;
8714 else if (wi::neg_p (c))
8715 return false;
8718 /* If we haven't been able to decide at this point, there nothing more we
8719 can check ourselves here. Look at the base type if we have one and it
8720 has the same precision. */
8721 if (TREE_CODE (type) == INTEGER_TYPE
8722 && TREE_TYPE (type) != 0
8723 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8725 type = TREE_TYPE (type);
8726 goto retry;
8729 /* Or to fits_to_tree_p, if nothing else. */
8730 return wi::fits_to_tree_p (c, type);
8733 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8734 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8735 represented (assuming two's-complement arithmetic) within the bit
8736 precision of the type are returned instead. */
8738 void
8739 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8741 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8742 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8743 wi::to_mpz (TYPE_MIN_VALUE (type), min, TYPE_SIGN (type));
8744 else
8746 if (TYPE_UNSIGNED (type))
8747 mpz_set_ui (min, 0);
8748 else
8750 wide_int mn = wi::min_value (TYPE_PRECISION (type), SIGNED);
8751 wi::to_mpz (mn, min, SIGNED);
8755 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8756 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8757 wi::to_mpz (TYPE_MAX_VALUE (type), max, TYPE_SIGN (type));
8758 else
8760 wide_int mn = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
8761 wi::to_mpz (mn, max, TYPE_SIGN (type));
8765 /* Return true if VAR is an automatic variable defined in function FN. */
8767 bool
8768 auto_var_in_fn_p (const_tree var, const_tree fn)
8770 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8771 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8772 || TREE_CODE (var) == PARM_DECL)
8773 && ! TREE_STATIC (var))
8774 || TREE_CODE (var) == LABEL_DECL
8775 || TREE_CODE (var) == RESULT_DECL));
8778 /* Subprogram of following function. Called by walk_tree.
8780 Return *TP if it is an automatic variable or parameter of the
8781 function passed in as DATA. */
8783 static tree
8784 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8786 tree fn = (tree) data;
8788 if (TYPE_P (*tp))
8789 *walk_subtrees = 0;
8791 else if (DECL_P (*tp)
8792 && auto_var_in_fn_p (*tp, fn))
8793 return *tp;
8795 return NULL_TREE;
8798 /* Returns true if T is, contains, or refers to a type with variable
8799 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8800 arguments, but not the return type. If FN is nonzero, only return
8801 true if a modifier of the type or position of FN is a variable or
8802 parameter inside FN.
8804 This concept is more general than that of C99 'variably modified types':
8805 in C99, a struct type is never variably modified because a VLA may not
8806 appear as a structure member. However, in GNU C code like:
8808 struct S { int i[f()]; };
8810 is valid, and other languages may define similar constructs. */
8812 bool
8813 variably_modified_type_p (tree type, tree fn)
8815 tree t;
8817 /* Test if T is either variable (if FN is zero) or an expression containing
8818 a variable in FN. If TYPE isn't gimplified, return true also if
8819 gimplify_one_sizepos would gimplify the expression into a local
8820 variable. */
8821 #define RETURN_TRUE_IF_VAR(T) \
8822 do { tree _t = (T); \
8823 if (_t != NULL_TREE \
8824 && _t != error_mark_node \
8825 && TREE_CODE (_t) != INTEGER_CST \
8826 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8827 && (!fn \
8828 || (!TYPE_SIZES_GIMPLIFIED (type) \
8829 && !is_gimple_sizepos (_t)) \
8830 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8831 return true; } while (0)
8833 if (type == error_mark_node)
8834 return false;
8836 /* If TYPE itself has variable size, it is variably modified. */
8837 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8838 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8840 switch (TREE_CODE (type))
8842 case POINTER_TYPE:
8843 case REFERENCE_TYPE:
8844 case VECTOR_TYPE:
8845 if (variably_modified_type_p (TREE_TYPE (type), fn))
8846 return true;
8847 break;
8849 case FUNCTION_TYPE:
8850 case METHOD_TYPE:
8851 /* If TYPE is a function type, it is variably modified if the
8852 return type is variably modified. */
8853 if (variably_modified_type_p (TREE_TYPE (type), fn))
8854 return true;
8855 break;
8857 case INTEGER_TYPE:
8858 case REAL_TYPE:
8859 case FIXED_POINT_TYPE:
8860 case ENUMERAL_TYPE:
8861 case BOOLEAN_TYPE:
8862 /* Scalar types are variably modified if their end points
8863 aren't constant. */
8864 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8865 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8866 break;
8868 case RECORD_TYPE:
8869 case UNION_TYPE:
8870 case QUAL_UNION_TYPE:
8871 /* We can't see if any of the fields are variably-modified by the
8872 definition we normally use, since that would produce infinite
8873 recursion via pointers. */
8874 /* This is variably modified if some field's type is. */
8875 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8876 if (TREE_CODE (t) == FIELD_DECL)
8878 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8879 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8880 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8882 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8883 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8885 break;
8887 case ARRAY_TYPE:
8888 /* Do not call ourselves to avoid infinite recursion. This is
8889 variably modified if the element type is. */
8890 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8891 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8892 break;
8894 default:
8895 break;
8898 /* The current language may have other cases to check, but in general,
8899 all other types are not variably modified. */
8900 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8902 #undef RETURN_TRUE_IF_VAR
8905 /* Given a DECL or TYPE, return the scope in which it was declared, or
8906 NULL_TREE if there is no containing scope. */
8908 tree
8909 get_containing_scope (const_tree t)
8911 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8914 /* Return the innermost context enclosing DECL that is
8915 a FUNCTION_DECL, or zero if none. */
8917 tree
8918 decl_function_context (const_tree decl)
8920 tree context;
8922 if (TREE_CODE (decl) == ERROR_MARK)
8923 return 0;
8925 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8926 where we look up the function at runtime. Such functions always take
8927 a first argument of type 'pointer to real context'.
8929 C++ should really be fixed to use DECL_CONTEXT for the real context,
8930 and use something else for the "virtual context". */
8931 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8932 context
8933 = TYPE_MAIN_VARIANT
8934 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8935 else
8936 context = DECL_CONTEXT (decl);
8938 while (context && TREE_CODE (context) != FUNCTION_DECL)
8940 if (TREE_CODE (context) == BLOCK)
8941 context = BLOCK_SUPERCONTEXT (context);
8942 else
8943 context = get_containing_scope (context);
8946 return context;
8949 /* Return the innermost context enclosing DECL that is
8950 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8951 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8953 tree
8954 decl_type_context (const_tree decl)
8956 tree context = DECL_CONTEXT (decl);
8958 while (context)
8959 switch (TREE_CODE (context))
8961 case NAMESPACE_DECL:
8962 case TRANSLATION_UNIT_DECL:
8963 return NULL_TREE;
8965 case RECORD_TYPE:
8966 case UNION_TYPE:
8967 case QUAL_UNION_TYPE:
8968 return context;
8970 case TYPE_DECL:
8971 case FUNCTION_DECL:
8972 context = DECL_CONTEXT (context);
8973 break;
8975 case BLOCK:
8976 context = BLOCK_SUPERCONTEXT (context);
8977 break;
8979 default:
8980 gcc_unreachable ();
8983 return NULL_TREE;
8986 /* CALL is a CALL_EXPR. Return the declaration for the function
8987 called, or NULL_TREE if the called function cannot be
8988 determined. */
8990 tree
8991 get_callee_fndecl (const_tree call)
8993 tree addr;
8995 if (call == error_mark_node)
8996 return error_mark_node;
8998 /* It's invalid to call this function with anything but a
8999 CALL_EXPR. */
9000 gcc_assert (TREE_CODE (call) == CALL_EXPR);
9002 /* The first operand to the CALL is the address of the function
9003 called. */
9004 addr = CALL_EXPR_FN (call);
9006 /* If there is no function, return early. */
9007 if (addr == NULL_TREE)
9008 return NULL_TREE;
9010 STRIP_NOPS (addr);
9012 /* If this is a readonly function pointer, extract its initial value. */
9013 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
9014 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
9015 && DECL_INITIAL (addr))
9016 addr = DECL_INITIAL (addr);
9018 /* If the address is just `&f' for some function `f', then we know
9019 that `f' is being called. */
9020 if (TREE_CODE (addr) == ADDR_EXPR
9021 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
9022 return TREE_OPERAND (addr, 0);
9024 /* We couldn't figure out what was being called. */
9025 return NULL_TREE;
9028 /* Print debugging information about tree nodes generated during the compile,
9029 and any language-specific information. */
9031 void
9032 dump_tree_statistics (void)
9034 if (GATHER_STATISTICS)
9036 int i;
9037 int total_nodes, total_bytes;
9038 fprintf (stderr, "Kind Nodes Bytes\n");
9039 fprintf (stderr, "---------------------------------------\n");
9040 total_nodes = total_bytes = 0;
9041 for (i = 0; i < (int) all_kinds; i++)
9043 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
9044 tree_node_counts[i], tree_node_sizes[i]);
9045 total_nodes += tree_node_counts[i];
9046 total_bytes += tree_node_sizes[i];
9048 fprintf (stderr, "---------------------------------------\n");
9049 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
9050 fprintf (stderr, "---------------------------------------\n");
9051 fprintf (stderr, "Code Nodes\n");
9052 fprintf (stderr, "----------------------------\n");
9053 for (i = 0; i < (int) MAX_TREE_CODES; i++)
9054 fprintf (stderr, "%-20s %7d\n", get_tree_code_name ((enum tree_code) i),
9055 tree_code_counts[i]);
9056 fprintf (stderr, "----------------------------\n");
9057 ssanames_print_statistics ();
9058 phinodes_print_statistics ();
9060 else
9061 fprintf (stderr, "(No per-node statistics)\n");
9063 print_type_hash_statistics ();
9064 print_debug_expr_statistics ();
9065 print_value_expr_statistics ();
9066 lang_hooks.print_statistics ();
9069 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
9071 /* Generate a crc32 of a byte. */
9073 static unsigned
9074 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
9076 unsigned ix;
9078 for (ix = bits; ix--; value <<= 1)
9080 unsigned feedback;
9082 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
9083 chksum <<= 1;
9084 chksum ^= feedback;
9086 return chksum;
9089 /* Generate a crc32 of a 32-bit unsigned. */
9091 unsigned
9092 crc32_unsigned (unsigned chksum, unsigned value)
9094 return crc32_unsigned_bits (chksum, value, 32);
9097 /* Generate a crc32 of a byte. */
9099 unsigned
9100 crc32_byte (unsigned chksum, char byte)
9102 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
9105 /* Generate a crc32 of a string. */
9107 unsigned
9108 crc32_string (unsigned chksum, const char *string)
9112 chksum = crc32_byte (chksum, *string);
9114 while (*string++);
9115 return chksum;
9118 /* P is a string that will be used in a symbol. Mask out any characters
9119 that are not valid in that context. */
9121 void
9122 clean_symbol_name (char *p)
9124 for (; *p; p++)
9125 if (! (ISALNUM (*p)
9126 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
9127 || *p == '$'
9128 #endif
9129 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
9130 || *p == '.'
9131 #endif
9133 *p = '_';
9136 /* Generate a name for a special-purpose function.
9137 The generated name may need to be unique across the whole link.
9138 Changes to this function may also require corresponding changes to
9139 xstrdup_mask_random.
9140 TYPE is some string to identify the purpose of this function to the
9141 linker or collect2; it must start with an uppercase letter,
9142 one of:
9143 I - for constructors
9144 D - for destructors
9145 N - for C++ anonymous namespaces
9146 F - for DWARF unwind frame information. */
9148 tree
9149 get_file_function_name (const char *type)
9151 char *buf;
9152 const char *p;
9153 char *q;
9155 /* If we already have a name we know to be unique, just use that. */
9156 if (first_global_object_name)
9157 p = q = ASTRDUP (first_global_object_name);
9158 /* If the target is handling the constructors/destructors, they
9159 will be local to this file and the name is only necessary for
9160 debugging purposes.
9161 We also assign sub_I and sub_D sufixes to constructors called from
9162 the global static constructors. These are always local. */
9163 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9164 || (strncmp (type, "sub_", 4) == 0
9165 && (type[4] == 'I' || type[4] == 'D')))
9167 const char *file = main_input_filename;
9168 if (! file)
9169 file = LOCATION_FILE (input_location);
9170 /* Just use the file's basename, because the full pathname
9171 might be quite long. */
9172 p = q = ASTRDUP (lbasename (file));
9174 else
9176 /* Otherwise, the name must be unique across the entire link.
9177 We don't have anything that we know to be unique to this translation
9178 unit, so use what we do have and throw in some randomness. */
9179 unsigned len;
9180 const char *name = weak_global_object_name;
9181 const char *file = main_input_filename;
9183 if (! name)
9184 name = "";
9185 if (! file)
9186 file = LOCATION_FILE (input_location);
9188 len = strlen (file);
9189 q = (char *) alloca (9 + 17 + len + 1);
9190 memcpy (q, file, len + 1);
9192 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9193 crc32_string (0, name), get_random_seed (false));
9195 p = q;
9198 clean_symbol_name (q);
9199 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9200 + strlen (type));
9202 /* Set up the name of the file-level functions we may need.
9203 Use a global object (which is already required to be unique over
9204 the program) rather than the file name (which imposes extra
9205 constraints). */
9206 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9208 return get_identifier (buf);
9211 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9213 /* Complain that the tree code of NODE does not match the expected 0
9214 terminated list of trailing codes. The trailing code list can be
9215 empty, for a more vague error message. FILE, LINE, and FUNCTION
9216 are of the caller. */
9218 void
9219 tree_check_failed (const_tree node, const char *file,
9220 int line, const char *function, ...)
9222 va_list args;
9223 const char *buffer;
9224 unsigned length = 0;
9225 enum tree_code code;
9227 va_start (args, function);
9228 while ((code = (enum tree_code) va_arg (args, int)))
9229 length += 4 + strlen (get_tree_code_name (code));
9230 va_end (args);
9231 if (length)
9233 char *tmp;
9234 va_start (args, function);
9235 length += strlen ("expected ");
9236 buffer = tmp = (char *) alloca (length);
9237 length = 0;
9238 while ((code = (enum tree_code) va_arg (args, int)))
9240 const char *prefix = length ? " or " : "expected ";
9242 strcpy (tmp + length, prefix);
9243 length += strlen (prefix);
9244 strcpy (tmp + length, get_tree_code_name (code));
9245 length += strlen (get_tree_code_name (code));
9247 va_end (args);
9249 else
9250 buffer = "unexpected node";
9252 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9253 buffer, get_tree_code_name (TREE_CODE (node)),
9254 function, trim_filename (file), line);
9257 /* Complain that the tree code of NODE does match the expected 0
9258 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9259 the caller. */
9261 void
9262 tree_not_check_failed (const_tree node, const char *file,
9263 int line, const char *function, ...)
9265 va_list args;
9266 char *buffer;
9267 unsigned length = 0;
9268 enum tree_code code;
9270 va_start (args, function);
9271 while ((code = (enum tree_code) va_arg (args, int)))
9272 length += 4 + strlen (get_tree_code_name (code));
9273 va_end (args);
9274 va_start (args, function);
9275 buffer = (char *) alloca (length);
9276 length = 0;
9277 while ((code = (enum tree_code) va_arg (args, int)))
9279 if (length)
9281 strcpy (buffer + length, " or ");
9282 length += 4;
9284 strcpy (buffer + length, get_tree_code_name (code));
9285 length += strlen (get_tree_code_name (code));
9287 va_end (args);
9289 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9290 buffer, get_tree_code_name (TREE_CODE (node)),
9291 function, trim_filename (file), line);
9294 /* Similar to tree_check_failed, except that we check for a class of tree
9295 code, given in CL. */
9297 void
9298 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9299 const char *file, int line, const char *function)
9301 internal_error
9302 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9303 TREE_CODE_CLASS_STRING (cl),
9304 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9305 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9308 /* Similar to tree_check_failed, except that instead of specifying a
9309 dozen codes, use the knowledge that they're all sequential. */
9311 void
9312 tree_range_check_failed (const_tree node, const char *file, int line,
9313 const char *function, enum tree_code c1,
9314 enum tree_code c2)
9316 char *buffer;
9317 unsigned length = 0;
9318 unsigned int c;
9320 for (c = c1; c <= c2; ++c)
9321 length += 4 + strlen (get_tree_code_name ((enum tree_code) c));
9323 length += strlen ("expected ");
9324 buffer = (char *) alloca (length);
9325 length = 0;
9327 for (c = c1; c <= c2; ++c)
9329 const char *prefix = length ? " or " : "expected ";
9331 strcpy (buffer + length, prefix);
9332 length += strlen (prefix);
9333 strcpy (buffer + length, get_tree_code_name ((enum tree_code) c));
9334 length += strlen (get_tree_code_name ((enum tree_code) c));
9337 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9338 buffer, get_tree_code_name (TREE_CODE (node)),
9339 function, trim_filename (file), line);
9343 /* Similar to tree_check_failed, except that we check that a tree does
9344 not have the specified code, given in CL. */
9346 void
9347 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9348 const char *file, int line, const char *function)
9350 internal_error
9351 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9352 TREE_CODE_CLASS_STRING (cl),
9353 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9354 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9358 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9360 void
9361 omp_clause_check_failed (const_tree node, const char *file, int line,
9362 const char *function, enum omp_clause_code code)
9364 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9365 omp_clause_code_name[code], get_tree_code_name (TREE_CODE (node)),
9366 function, trim_filename (file), line);
9370 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9372 void
9373 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9374 const char *function, enum omp_clause_code c1,
9375 enum omp_clause_code c2)
9377 char *buffer;
9378 unsigned length = 0;
9379 unsigned int c;
9381 for (c = c1; c <= c2; ++c)
9382 length += 4 + strlen (omp_clause_code_name[c]);
9384 length += strlen ("expected ");
9385 buffer = (char *) alloca (length);
9386 length = 0;
9388 for (c = c1; c <= c2; ++c)
9390 const char *prefix = length ? " or " : "expected ";
9392 strcpy (buffer + length, prefix);
9393 length += strlen (prefix);
9394 strcpy (buffer + length, omp_clause_code_name[c]);
9395 length += strlen (omp_clause_code_name[c]);
9398 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9399 buffer, omp_clause_code_name[TREE_CODE (node)],
9400 function, trim_filename (file), line);
9404 #undef DEFTREESTRUCT
9405 #define DEFTREESTRUCT(VAL, NAME) NAME,
9407 static const char *ts_enum_names[] = {
9408 #include "treestruct.def"
9410 #undef DEFTREESTRUCT
9412 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9414 /* Similar to tree_class_check_failed, except that we check for
9415 whether CODE contains the tree structure identified by EN. */
9417 void
9418 tree_contains_struct_check_failed (const_tree node,
9419 const enum tree_node_structure_enum en,
9420 const char *file, int line,
9421 const char *function)
9423 internal_error
9424 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9425 TS_ENUM_NAME (en),
9426 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9430 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9431 (dynamically sized) vector. */
9433 void
9434 tree_int_cst_elt_check_failed (int idx, int len, const char *file, int line,
9435 const char *function)
9437 internal_error
9438 ("tree check: accessed elt %d of tree_int_cst with %d elts in %s, at %s:%d",
9439 idx + 1, len, function, trim_filename (file), line);
9442 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9443 (dynamically sized) vector. */
9445 void
9446 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9447 const char *function)
9449 internal_error
9450 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9451 idx + 1, len, function, trim_filename (file), line);
9454 /* Similar to above, except that the check is for the bounds of the operand
9455 vector of an expression node EXP. */
9457 void
9458 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9459 int line, const char *function)
9461 enum tree_code code = TREE_CODE (exp);
9462 internal_error
9463 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9464 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9465 function, trim_filename (file), line);
9468 /* Similar to above, except that the check is for the number of
9469 operands of an OMP_CLAUSE node. */
9471 void
9472 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9473 int line, const char *function)
9475 internal_error
9476 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9477 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9478 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9479 trim_filename (file), line);
9481 #endif /* ENABLE_TREE_CHECKING */
9483 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9484 and mapped to the machine mode MODE. Initialize its fields and build
9485 the information necessary for debugging output. */
9487 static tree
9488 make_vector_type (tree innertype, int nunits, machine_mode mode)
9490 tree t;
9491 inchash::hash hstate;
9493 t = make_node (VECTOR_TYPE);
9494 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9495 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9496 SET_TYPE_MODE (t, mode);
9498 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9499 SET_TYPE_STRUCTURAL_EQUALITY (t);
9500 else if (TYPE_CANONICAL (innertype) != innertype
9501 || mode != VOIDmode)
9502 TYPE_CANONICAL (t)
9503 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9505 layout_type (t);
9507 hstate.add_wide_int (VECTOR_TYPE);
9508 hstate.add_wide_int (nunits);
9509 hstate.add_wide_int (mode);
9510 hstate.add_object (TYPE_HASH (TREE_TYPE (t)));
9511 t = type_hash_canon (hstate.end (), t);
9513 /* We have built a main variant, based on the main variant of the
9514 inner type. Use it to build the variant we return. */
9515 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9516 && TREE_TYPE (t) != innertype)
9517 return build_type_attribute_qual_variant (t,
9518 TYPE_ATTRIBUTES (innertype),
9519 TYPE_QUALS (innertype));
9521 return t;
9524 static tree
9525 make_or_reuse_type (unsigned size, int unsignedp)
9527 int i;
9529 if (size == INT_TYPE_SIZE)
9530 return unsignedp ? unsigned_type_node : integer_type_node;
9531 if (size == CHAR_TYPE_SIZE)
9532 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9533 if (size == SHORT_TYPE_SIZE)
9534 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9535 if (size == LONG_TYPE_SIZE)
9536 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9537 if (size == LONG_LONG_TYPE_SIZE)
9538 return (unsignedp ? long_long_unsigned_type_node
9539 : long_long_integer_type_node);
9541 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9542 if (size == int_n_data[i].bitsize
9543 && int_n_enabled_p[i])
9544 return (unsignedp ? int_n_trees[i].unsigned_type
9545 : int_n_trees[i].signed_type);
9547 if (unsignedp)
9548 return make_unsigned_type (size);
9549 else
9550 return make_signed_type (size);
9553 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9555 static tree
9556 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9558 if (satp)
9560 if (size == SHORT_FRACT_TYPE_SIZE)
9561 return unsignedp ? sat_unsigned_short_fract_type_node
9562 : sat_short_fract_type_node;
9563 if (size == FRACT_TYPE_SIZE)
9564 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9565 if (size == LONG_FRACT_TYPE_SIZE)
9566 return unsignedp ? sat_unsigned_long_fract_type_node
9567 : sat_long_fract_type_node;
9568 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9569 return unsignedp ? sat_unsigned_long_long_fract_type_node
9570 : sat_long_long_fract_type_node;
9572 else
9574 if (size == SHORT_FRACT_TYPE_SIZE)
9575 return unsignedp ? unsigned_short_fract_type_node
9576 : short_fract_type_node;
9577 if (size == FRACT_TYPE_SIZE)
9578 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9579 if (size == LONG_FRACT_TYPE_SIZE)
9580 return unsignedp ? unsigned_long_fract_type_node
9581 : long_fract_type_node;
9582 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9583 return unsignedp ? unsigned_long_long_fract_type_node
9584 : long_long_fract_type_node;
9587 return make_fract_type (size, unsignedp, satp);
9590 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9592 static tree
9593 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9595 if (satp)
9597 if (size == SHORT_ACCUM_TYPE_SIZE)
9598 return unsignedp ? sat_unsigned_short_accum_type_node
9599 : sat_short_accum_type_node;
9600 if (size == ACCUM_TYPE_SIZE)
9601 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9602 if (size == LONG_ACCUM_TYPE_SIZE)
9603 return unsignedp ? sat_unsigned_long_accum_type_node
9604 : sat_long_accum_type_node;
9605 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9606 return unsignedp ? sat_unsigned_long_long_accum_type_node
9607 : sat_long_long_accum_type_node;
9609 else
9611 if (size == SHORT_ACCUM_TYPE_SIZE)
9612 return unsignedp ? unsigned_short_accum_type_node
9613 : short_accum_type_node;
9614 if (size == ACCUM_TYPE_SIZE)
9615 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9616 if (size == LONG_ACCUM_TYPE_SIZE)
9617 return unsignedp ? unsigned_long_accum_type_node
9618 : long_accum_type_node;
9619 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9620 return unsignedp ? unsigned_long_long_accum_type_node
9621 : long_long_accum_type_node;
9624 return make_accum_type (size, unsignedp, satp);
9628 /* Create an atomic variant node for TYPE. This routine is called
9629 during initialization of data types to create the 5 basic atomic
9630 types. The generic build_variant_type function requires these to
9631 already be set up in order to function properly, so cannot be
9632 called from there. If ALIGN is non-zero, then ensure alignment is
9633 overridden to this value. */
9635 static tree
9636 build_atomic_base (tree type, unsigned int align)
9638 tree t;
9640 /* Make sure its not already registered. */
9641 if ((t = get_qualified_type (type, TYPE_QUAL_ATOMIC)))
9642 return t;
9644 t = build_variant_type_copy (type);
9645 set_type_quals (t, TYPE_QUAL_ATOMIC);
9647 if (align)
9648 TYPE_ALIGN (t) = align;
9650 return t;
9653 /* Create nodes for all integer types (and error_mark_node) using the sizes
9654 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9655 SHORT_DOUBLE specifies whether double should be of the same precision
9656 as float. */
9658 void
9659 build_common_tree_nodes (bool signed_char, bool short_double)
9661 int i;
9663 error_mark_node = make_node (ERROR_MARK);
9664 TREE_TYPE (error_mark_node) = error_mark_node;
9666 initialize_sizetypes ();
9668 /* Define both `signed char' and `unsigned char'. */
9669 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9670 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9671 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9672 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9674 /* Define `char', which is like either `signed char' or `unsigned char'
9675 but not the same as either. */
9676 char_type_node
9677 = (signed_char
9678 ? make_signed_type (CHAR_TYPE_SIZE)
9679 : make_unsigned_type (CHAR_TYPE_SIZE));
9680 TYPE_STRING_FLAG (char_type_node) = 1;
9682 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9683 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9684 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9685 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9686 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9687 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9688 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9689 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9691 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9693 int_n_trees[i].signed_type = make_signed_type (int_n_data[i].bitsize);
9694 int_n_trees[i].unsigned_type = make_unsigned_type (int_n_data[i].bitsize);
9695 TYPE_SIZE (int_n_trees[i].signed_type) = bitsize_int (int_n_data[i].bitsize);
9696 TYPE_SIZE (int_n_trees[i].unsigned_type) = bitsize_int (int_n_data[i].bitsize);
9698 if (int_n_data[i].bitsize > LONG_LONG_TYPE_SIZE
9699 && int_n_enabled_p[i])
9701 integer_types[itk_intN_0 + i * 2] = int_n_trees[i].signed_type;
9702 integer_types[itk_unsigned_intN_0 + i * 2] = int_n_trees[i].unsigned_type;
9706 /* Define a boolean type. This type only represents boolean values but
9707 may be larger than char depending on the value of BOOL_TYPE_SIZE. */
9708 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9709 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9710 TYPE_PRECISION (boolean_type_node) = 1;
9711 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9713 /* Define what type to use for size_t. */
9714 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9715 size_type_node = unsigned_type_node;
9716 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9717 size_type_node = long_unsigned_type_node;
9718 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9719 size_type_node = long_long_unsigned_type_node;
9720 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9721 size_type_node = short_unsigned_type_node;
9722 else
9724 int i;
9726 size_type_node = NULL_TREE;
9727 for (i = 0; i < NUM_INT_N_ENTS; i++)
9728 if (int_n_enabled_p[i])
9730 char name[50];
9731 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
9733 if (strcmp (name, SIZE_TYPE) == 0)
9735 size_type_node = int_n_trees[i].unsigned_type;
9738 if (size_type_node == NULL_TREE)
9739 gcc_unreachable ();
9742 /* Fill in the rest of the sized types. Reuse existing type nodes
9743 when possible. */
9744 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9745 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9746 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9747 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9748 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9750 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9751 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9752 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9753 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9754 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9756 /* Don't call build_qualified type for atomics. That routine does
9757 special processing for atomics, and until they are initialized
9758 it's better not to make that call.
9760 Check to see if there is a target override for atomic types. */
9762 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
9763 targetm.atomic_align_for_mode (QImode));
9764 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
9765 targetm.atomic_align_for_mode (HImode));
9766 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
9767 targetm.atomic_align_for_mode (SImode));
9768 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
9769 targetm.atomic_align_for_mode (DImode));
9770 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
9771 targetm.atomic_align_for_mode (TImode));
9773 access_public_node = get_identifier ("public");
9774 access_protected_node = get_identifier ("protected");
9775 access_private_node = get_identifier ("private");
9777 /* Define these next since types below may used them. */
9778 integer_zero_node = build_int_cst (integer_type_node, 0);
9779 integer_one_node = build_int_cst (integer_type_node, 1);
9780 integer_three_node = build_int_cst (integer_type_node, 3);
9781 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9783 size_zero_node = size_int (0);
9784 size_one_node = size_int (1);
9785 bitsize_zero_node = bitsize_int (0);
9786 bitsize_one_node = bitsize_int (1);
9787 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9789 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9790 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9792 void_type_node = make_node (VOID_TYPE);
9793 layout_type (void_type_node);
9795 pointer_bounds_type_node = targetm.chkp_bound_type ();
9797 /* We are not going to have real types in C with less than byte alignment,
9798 so we might as well not have any types that claim to have it. */
9799 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9800 TYPE_USER_ALIGN (void_type_node) = 0;
9802 void_node = make_node (VOID_CST);
9803 TREE_TYPE (void_node) = void_type_node;
9805 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9806 layout_type (TREE_TYPE (null_pointer_node));
9808 ptr_type_node = build_pointer_type (void_type_node);
9809 const_ptr_type_node
9810 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9811 fileptr_type_node = ptr_type_node;
9813 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
9815 float_type_node = make_node (REAL_TYPE);
9816 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9817 layout_type (float_type_node);
9819 double_type_node = make_node (REAL_TYPE);
9820 if (short_double)
9821 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9822 else
9823 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9824 layout_type (double_type_node);
9826 long_double_type_node = make_node (REAL_TYPE);
9827 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9828 layout_type (long_double_type_node);
9830 float_ptr_type_node = build_pointer_type (float_type_node);
9831 double_ptr_type_node = build_pointer_type (double_type_node);
9832 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9833 integer_ptr_type_node = build_pointer_type (integer_type_node);
9835 /* Fixed size integer types. */
9836 uint16_type_node = make_or_reuse_type (16, 1);
9837 uint32_type_node = make_or_reuse_type (32, 1);
9838 uint64_type_node = make_or_reuse_type (64, 1);
9840 /* Decimal float types. */
9841 dfloat32_type_node = make_node (REAL_TYPE);
9842 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9843 layout_type (dfloat32_type_node);
9844 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9845 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9847 dfloat64_type_node = make_node (REAL_TYPE);
9848 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9849 layout_type (dfloat64_type_node);
9850 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9851 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9853 dfloat128_type_node = make_node (REAL_TYPE);
9854 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9855 layout_type (dfloat128_type_node);
9856 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9857 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9859 complex_integer_type_node = build_complex_type (integer_type_node);
9860 complex_float_type_node = build_complex_type (float_type_node);
9861 complex_double_type_node = build_complex_type (double_type_node);
9862 complex_long_double_type_node = build_complex_type (long_double_type_node);
9864 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9865 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9866 sat_ ## KIND ## _type_node = \
9867 make_sat_signed_ ## KIND ## _type (SIZE); \
9868 sat_unsigned_ ## KIND ## _type_node = \
9869 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9870 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9871 unsigned_ ## KIND ## _type_node = \
9872 make_unsigned_ ## KIND ## _type (SIZE);
9874 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9875 sat_ ## WIDTH ## KIND ## _type_node = \
9876 make_sat_signed_ ## KIND ## _type (SIZE); \
9877 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9878 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9879 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9880 unsigned_ ## WIDTH ## KIND ## _type_node = \
9881 make_unsigned_ ## KIND ## _type (SIZE);
9883 /* Make fixed-point type nodes based on four different widths. */
9884 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9885 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9886 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9887 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9888 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9890 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9891 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9892 NAME ## _type_node = \
9893 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9894 u ## NAME ## _type_node = \
9895 make_or_reuse_unsigned_ ## KIND ## _type \
9896 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9897 sat_ ## NAME ## _type_node = \
9898 make_or_reuse_sat_signed_ ## KIND ## _type \
9899 (GET_MODE_BITSIZE (MODE ## mode)); \
9900 sat_u ## NAME ## _type_node = \
9901 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9902 (GET_MODE_BITSIZE (U ## MODE ## mode));
9904 /* Fixed-point type and mode nodes. */
9905 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9906 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9907 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9908 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9909 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9910 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9911 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9912 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9913 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9914 MAKE_FIXED_MODE_NODE (accum, da, DA)
9915 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9918 tree t = targetm.build_builtin_va_list ();
9920 /* Many back-ends define record types without setting TYPE_NAME.
9921 If we copied the record type here, we'd keep the original
9922 record type without a name. This breaks name mangling. So,
9923 don't copy record types and let c_common_nodes_and_builtins()
9924 declare the type to be __builtin_va_list. */
9925 if (TREE_CODE (t) != RECORD_TYPE)
9926 t = build_variant_type_copy (t);
9928 va_list_type_node = t;
9932 /* Modify DECL for given flags.
9933 TM_PURE attribute is set only on types, so the function will modify
9934 DECL's type when ECF_TM_PURE is used. */
9936 void
9937 set_call_expr_flags (tree decl, int flags)
9939 if (flags & ECF_NOTHROW)
9940 TREE_NOTHROW (decl) = 1;
9941 if (flags & ECF_CONST)
9942 TREE_READONLY (decl) = 1;
9943 if (flags & ECF_PURE)
9944 DECL_PURE_P (decl) = 1;
9945 if (flags & ECF_LOOPING_CONST_OR_PURE)
9946 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9947 if (flags & ECF_NOVOPS)
9948 DECL_IS_NOVOPS (decl) = 1;
9949 if (flags & ECF_NORETURN)
9950 TREE_THIS_VOLATILE (decl) = 1;
9951 if (flags & ECF_MALLOC)
9952 DECL_IS_MALLOC (decl) = 1;
9953 if (flags & ECF_RETURNS_TWICE)
9954 DECL_IS_RETURNS_TWICE (decl) = 1;
9955 if (flags & ECF_LEAF)
9956 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9957 NULL, DECL_ATTRIBUTES (decl));
9958 if ((flags & ECF_TM_PURE) && flag_tm)
9959 apply_tm_attr (decl, get_identifier ("transaction_pure"));
9960 /* Looping const or pure is implied by noreturn.
9961 There is currently no way to declare looping const or looping pure alone. */
9962 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9963 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9967 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9969 static void
9970 local_define_builtin (const char *name, tree type, enum built_in_function code,
9971 const char *library_name, int ecf_flags)
9973 tree decl;
9975 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9976 library_name, NULL_TREE);
9977 set_call_expr_flags (decl, ecf_flags);
9979 set_builtin_decl (code, decl, true);
9982 /* Call this function after instantiating all builtins that the language
9983 front end cares about. This will build the rest of the builtins
9984 and internal functions that are relied upon by the tree optimizers and
9985 the middle-end. */
9987 void
9988 build_common_builtin_nodes (void)
9990 tree tmp, ftype;
9991 int ecf_flags;
9993 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9995 ftype = build_function_type (void_type_node, void_list_node);
9996 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
9997 "__builtin_unreachable",
9998 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9999 | ECF_CONST);
10002 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
10003 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10005 ftype = build_function_type_list (ptr_type_node,
10006 ptr_type_node, const_ptr_type_node,
10007 size_type_node, NULL_TREE);
10009 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
10010 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
10011 "memcpy", ECF_NOTHROW | ECF_LEAF);
10012 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10013 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
10014 "memmove", ECF_NOTHROW | ECF_LEAF);
10017 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
10019 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
10020 const_ptr_type_node, size_type_node,
10021 NULL_TREE);
10022 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
10023 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10026 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
10028 ftype = build_function_type_list (ptr_type_node,
10029 ptr_type_node, integer_type_node,
10030 size_type_node, NULL_TREE);
10031 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
10032 "memset", ECF_NOTHROW | ECF_LEAF);
10035 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
10037 ftype = build_function_type_list (ptr_type_node,
10038 size_type_node, NULL_TREE);
10039 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
10040 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
10043 ftype = build_function_type_list (ptr_type_node, size_type_node,
10044 size_type_node, NULL_TREE);
10045 local_define_builtin ("__builtin_alloca_with_align", ftype,
10046 BUILT_IN_ALLOCA_WITH_ALIGN, "alloca",
10047 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
10049 /* If we're checking the stack, `alloca' can throw. */
10050 if (flag_stack_check)
10052 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
10053 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
10056 ftype = build_function_type_list (void_type_node,
10057 ptr_type_node, ptr_type_node,
10058 ptr_type_node, NULL_TREE);
10059 local_define_builtin ("__builtin_init_trampoline", ftype,
10060 BUILT_IN_INIT_TRAMPOLINE,
10061 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
10062 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
10063 BUILT_IN_INIT_HEAP_TRAMPOLINE,
10064 "__builtin_init_heap_trampoline",
10065 ECF_NOTHROW | ECF_LEAF);
10067 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
10068 local_define_builtin ("__builtin_adjust_trampoline", ftype,
10069 BUILT_IN_ADJUST_TRAMPOLINE,
10070 "__builtin_adjust_trampoline",
10071 ECF_CONST | ECF_NOTHROW);
10073 ftype = build_function_type_list (void_type_node,
10074 ptr_type_node, ptr_type_node, NULL_TREE);
10075 local_define_builtin ("__builtin_nonlocal_goto", ftype,
10076 BUILT_IN_NONLOCAL_GOTO,
10077 "__builtin_nonlocal_goto",
10078 ECF_NORETURN | ECF_NOTHROW);
10080 ftype = build_function_type_list (void_type_node,
10081 ptr_type_node, ptr_type_node, NULL_TREE);
10082 local_define_builtin ("__builtin_setjmp_setup", ftype,
10083 BUILT_IN_SETJMP_SETUP,
10084 "__builtin_setjmp_setup", ECF_NOTHROW);
10086 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10087 local_define_builtin ("__builtin_setjmp_receiver", ftype,
10088 BUILT_IN_SETJMP_RECEIVER,
10089 "__builtin_setjmp_receiver", ECF_NOTHROW | ECF_LEAF);
10091 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
10092 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
10093 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
10095 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10096 local_define_builtin ("__builtin_stack_restore", ftype,
10097 BUILT_IN_STACK_RESTORE,
10098 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
10100 /* If there's a possibility that we might use the ARM EABI, build the
10101 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
10102 if (targetm.arm_eabi_unwinder)
10104 ftype = build_function_type_list (void_type_node, NULL_TREE);
10105 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
10106 BUILT_IN_CXA_END_CLEANUP,
10107 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
10110 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10111 local_define_builtin ("__builtin_unwind_resume", ftype,
10112 BUILT_IN_UNWIND_RESUME,
10113 ((targetm_common.except_unwind_info (&global_options)
10114 == UI_SJLJ)
10115 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
10116 ECF_NORETURN);
10118 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
10120 ftype = build_function_type_list (ptr_type_node, integer_type_node,
10121 NULL_TREE);
10122 local_define_builtin ("__builtin_return_address", ftype,
10123 BUILT_IN_RETURN_ADDRESS,
10124 "__builtin_return_address",
10125 ECF_NOTHROW);
10128 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
10129 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10131 ftype = build_function_type_list (void_type_node, ptr_type_node,
10132 ptr_type_node, NULL_TREE);
10133 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
10134 local_define_builtin ("__cyg_profile_func_enter", ftype,
10135 BUILT_IN_PROFILE_FUNC_ENTER,
10136 "__cyg_profile_func_enter", 0);
10137 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10138 local_define_builtin ("__cyg_profile_func_exit", ftype,
10139 BUILT_IN_PROFILE_FUNC_EXIT,
10140 "__cyg_profile_func_exit", 0);
10143 /* The exception object and filter values from the runtime. The argument
10144 must be zero before exception lowering, i.e. from the front end. After
10145 exception lowering, it will be the region number for the exception
10146 landing pad. These functions are PURE instead of CONST to prevent
10147 them from being hoisted past the exception edge that will initialize
10148 its value in the landing pad. */
10149 ftype = build_function_type_list (ptr_type_node,
10150 integer_type_node, NULL_TREE);
10151 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
10152 /* Only use TM_PURE if we we have TM language support. */
10153 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
10154 ecf_flags |= ECF_TM_PURE;
10155 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
10156 "__builtin_eh_pointer", ecf_flags);
10158 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
10159 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
10160 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
10161 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10163 ftype = build_function_type_list (void_type_node,
10164 integer_type_node, integer_type_node,
10165 NULL_TREE);
10166 local_define_builtin ("__builtin_eh_copy_values", ftype,
10167 BUILT_IN_EH_COPY_VALUES,
10168 "__builtin_eh_copy_values", ECF_NOTHROW);
10170 /* Complex multiplication and division. These are handled as builtins
10171 rather than optabs because emit_library_call_value doesn't support
10172 complex. Further, we can do slightly better with folding these
10173 beasties if the real and complex parts of the arguments are separate. */
10175 int mode;
10177 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10179 char mode_name_buf[4], *q;
10180 const char *p;
10181 enum built_in_function mcode, dcode;
10182 tree type, inner_type;
10183 const char *prefix = "__";
10185 if (targetm.libfunc_gnu_prefix)
10186 prefix = "__gnu_";
10188 type = lang_hooks.types.type_for_mode ((machine_mode) mode, 0);
10189 if (type == NULL)
10190 continue;
10191 inner_type = TREE_TYPE (type);
10193 ftype = build_function_type_list (type, inner_type, inner_type,
10194 inner_type, inner_type, NULL_TREE);
10196 mcode = ((enum built_in_function)
10197 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10198 dcode = ((enum built_in_function)
10199 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10201 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10202 *q = TOLOWER (*p);
10203 *q = '\0';
10205 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10206 NULL);
10207 local_define_builtin (built_in_names[mcode], ftype, mcode,
10208 built_in_names[mcode],
10209 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10211 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10212 NULL);
10213 local_define_builtin (built_in_names[dcode], ftype, dcode,
10214 built_in_names[dcode],
10215 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10219 init_internal_fns ();
10222 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10223 better way.
10225 If we requested a pointer to a vector, build up the pointers that
10226 we stripped off while looking for the inner type. Similarly for
10227 return values from functions.
10229 The argument TYPE is the top of the chain, and BOTTOM is the
10230 new type which we will point to. */
10232 tree
10233 reconstruct_complex_type (tree type, tree bottom)
10235 tree inner, outer;
10237 if (TREE_CODE (type) == POINTER_TYPE)
10239 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10240 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10241 TYPE_REF_CAN_ALIAS_ALL (type));
10243 else if (TREE_CODE (type) == REFERENCE_TYPE)
10245 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10246 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10247 TYPE_REF_CAN_ALIAS_ALL (type));
10249 else if (TREE_CODE (type) == ARRAY_TYPE)
10251 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10252 outer = build_array_type (inner, TYPE_DOMAIN (type));
10254 else if (TREE_CODE (type) == FUNCTION_TYPE)
10256 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10257 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
10259 else if (TREE_CODE (type) == METHOD_TYPE)
10261 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10262 /* The build_method_type_directly() routine prepends 'this' to argument list,
10263 so we must compensate by getting rid of it. */
10264 outer
10265 = build_method_type_directly
10266 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10267 inner,
10268 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10270 else if (TREE_CODE (type) == OFFSET_TYPE)
10272 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10273 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10275 else
10276 return bottom;
10278 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10279 TYPE_QUALS (type));
10282 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10283 the inner type. */
10284 tree
10285 build_vector_type_for_mode (tree innertype, machine_mode mode)
10287 int nunits;
10289 switch (GET_MODE_CLASS (mode))
10291 case MODE_VECTOR_INT:
10292 case MODE_VECTOR_FLOAT:
10293 case MODE_VECTOR_FRACT:
10294 case MODE_VECTOR_UFRACT:
10295 case MODE_VECTOR_ACCUM:
10296 case MODE_VECTOR_UACCUM:
10297 nunits = GET_MODE_NUNITS (mode);
10298 break;
10300 case MODE_INT:
10301 /* Check that there are no leftover bits. */
10302 gcc_assert (GET_MODE_BITSIZE (mode)
10303 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10305 nunits = GET_MODE_BITSIZE (mode)
10306 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10307 break;
10309 default:
10310 gcc_unreachable ();
10313 return make_vector_type (innertype, nunits, mode);
10316 /* Similarly, but takes the inner type and number of units, which must be
10317 a power of two. */
10319 tree
10320 build_vector_type (tree innertype, int nunits)
10322 return make_vector_type (innertype, nunits, VOIDmode);
10325 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10327 tree
10328 build_opaque_vector_type (tree innertype, int nunits)
10330 tree t = make_vector_type (innertype, nunits, VOIDmode);
10331 tree cand;
10332 /* We always build the non-opaque variant before the opaque one,
10333 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10334 cand = TYPE_NEXT_VARIANT (t);
10335 if (cand
10336 && TYPE_VECTOR_OPAQUE (cand)
10337 && check_qualified_type (cand, t, TYPE_QUALS (t)))
10338 return cand;
10339 /* Othewise build a variant type and make sure to queue it after
10340 the non-opaque type. */
10341 cand = build_distinct_type_copy (t);
10342 TYPE_VECTOR_OPAQUE (cand) = true;
10343 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10344 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10345 TYPE_NEXT_VARIANT (t) = cand;
10346 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10347 return cand;
10351 /* Given an initializer INIT, return TRUE if INIT is zero or some
10352 aggregate of zeros. Otherwise return FALSE. */
10353 bool
10354 initializer_zerop (const_tree init)
10356 tree elt;
10358 STRIP_NOPS (init);
10360 switch (TREE_CODE (init))
10362 case INTEGER_CST:
10363 return integer_zerop (init);
10365 case REAL_CST:
10366 /* ??? Note that this is not correct for C4X float formats. There,
10367 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10368 negative exponent. */
10369 return real_zerop (init)
10370 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10372 case FIXED_CST:
10373 return fixed_zerop (init);
10375 case COMPLEX_CST:
10376 return integer_zerop (init)
10377 || (real_zerop (init)
10378 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10379 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10381 case VECTOR_CST:
10383 unsigned i;
10384 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10385 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10386 return false;
10387 return true;
10390 case CONSTRUCTOR:
10392 unsigned HOST_WIDE_INT idx;
10394 if (TREE_CLOBBER_P (init))
10395 return false;
10396 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10397 if (!initializer_zerop (elt))
10398 return false;
10399 return true;
10402 case STRING_CST:
10404 int i;
10406 /* We need to loop through all elements to handle cases like
10407 "\0" and "\0foobar". */
10408 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10409 if (TREE_STRING_POINTER (init)[i] != '\0')
10410 return false;
10412 return true;
10415 default:
10416 return false;
10420 /* Check if vector VEC consists of all the equal elements and
10421 that the number of elements corresponds to the type of VEC.
10422 The function returns first element of the vector
10423 or NULL_TREE if the vector is not uniform. */
10424 tree
10425 uniform_vector_p (const_tree vec)
10427 tree first, t;
10428 unsigned i;
10430 if (vec == NULL_TREE)
10431 return NULL_TREE;
10433 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10435 if (TREE_CODE (vec) == VECTOR_CST)
10437 first = VECTOR_CST_ELT (vec, 0);
10438 for (i = 1; i < VECTOR_CST_NELTS (vec); ++i)
10439 if (!operand_equal_p (first, VECTOR_CST_ELT (vec, i), 0))
10440 return NULL_TREE;
10442 return first;
10445 else if (TREE_CODE (vec) == CONSTRUCTOR)
10447 first = error_mark_node;
10449 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10451 if (i == 0)
10453 first = t;
10454 continue;
10456 if (!operand_equal_p (first, t, 0))
10457 return NULL_TREE;
10459 if (i != TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)))
10460 return NULL_TREE;
10462 return first;
10465 return NULL_TREE;
10468 /* Build an empty statement at location LOC. */
10470 tree
10471 build_empty_stmt (location_t loc)
10473 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10474 SET_EXPR_LOCATION (t, loc);
10475 return t;
10479 /* Build an OpenMP clause with code CODE. LOC is the location of the
10480 clause. */
10482 tree
10483 build_omp_clause (location_t loc, enum omp_clause_code code)
10485 tree t;
10486 int size, length;
10488 length = omp_clause_num_ops[code];
10489 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10491 record_node_allocation_statistics (OMP_CLAUSE, size);
10493 t = (tree) ggc_internal_alloc (size);
10494 memset (t, 0, size);
10495 TREE_SET_CODE (t, OMP_CLAUSE);
10496 OMP_CLAUSE_SET_CODE (t, code);
10497 OMP_CLAUSE_LOCATION (t) = loc;
10499 return t;
10502 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10503 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10504 Except for the CODE and operand count field, other storage for the
10505 object is initialized to zeros. */
10507 tree
10508 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10510 tree t;
10511 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10513 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10514 gcc_assert (len >= 1);
10516 record_node_allocation_statistics (code, length);
10518 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10520 TREE_SET_CODE (t, code);
10522 /* Can't use TREE_OPERAND to store the length because if checking is
10523 enabled, it will try to check the length before we store it. :-P */
10524 t->exp.operands[0] = build_int_cst (sizetype, len);
10526 return t;
10529 /* Helper function for build_call_* functions; build a CALL_EXPR with
10530 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10531 the argument slots. */
10533 static tree
10534 build_call_1 (tree return_type, tree fn, int nargs)
10536 tree t;
10538 t = build_vl_exp (CALL_EXPR, nargs + 3);
10539 TREE_TYPE (t) = return_type;
10540 CALL_EXPR_FN (t) = fn;
10541 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10543 return t;
10546 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10547 FN and a null static chain slot. NARGS is the number of call arguments
10548 which are specified as "..." arguments. */
10550 tree
10551 build_call_nary (tree return_type, tree fn, int nargs, ...)
10553 tree ret;
10554 va_list args;
10555 va_start (args, nargs);
10556 ret = build_call_valist (return_type, fn, nargs, args);
10557 va_end (args);
10558 return ret;
10561 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10562 FN and a null static chain slot. NARGS is the number of call arguments
10563 which are specified as a va_list ARGS. */
10565 tree
10566 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10568 tree t;
10569 int i;
10571 t = build_call_1 (return_type, fn, nargs);
10572 for (i = 0; i < nargs; i++)
10573 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10574 process_call_operands (t);
10575 return t;
10578 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10579 FN and a null static chain slot. NARGS is the number of call arguments
10580 which are specified as a tree array ARGS. */
10582 tree
10583 build_call_array_loc (location_t loc, tree return_type, tree fn,
10584 int nargs, const tree *args)
10586 tree t;
10587 int i;
10589 t = build_call_1 (return_type, fn, nargs);
10590 for (i = 0; i < nargs; i++)
10591 CALL_EXPR_ARG (t, i) = args[i];
10592 process_call_operands (t);
10593 SET_EXPR_LOCATION (t, loc);
10594 return t;
10597 /* Like build_call_array, but takes a vec. */
10599 tree
10600 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10602 tree ret, t;
10603 unsigned int ix;
10605 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10606 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10607 CALL_EXPR_ARG (ret, ix) = t;
10608 process_call_operands (ret);
10609 return ret;
10612 /* Conveniently construct a function call expression. FNDECL names the
10613 function to be called and N arguments are passed in the array
10614 ARGARRAY. */
10616 tree
10617 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
10619 tree fntype = TREE_TYPE (fndecl);
10620 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10622 return fold_build_call_array_loc (loc, TREE_TYPE (fntype), fn, n, argarray);
10625 /* Conveniently construct a function call expression. FNDECL names the
10626 function to be called and the arguments are passed in the vector
10627 VEC. */
10629 tree
10630 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
10632 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
10633 vec_safe_address (vec));
10637 /* Conveniently construct a function call expression. FNDECL names the
10638 function to be called, N is the number of arguments, and the "..."
10639 parameters are the argument expressions. */
10641 tree
10642 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
10644 va_list ap;
10645 tree *argarray = XALLOCAVEC (tree, n);
10646 int i;
10648 va_start (ap, n);
10649 for (i = 0; i < n; i++)
10650 argarray[i] = va_arg (ap, tree);
10651 va_end (ap);
10652 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10655 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
10656 varargs macros aren't supported by all bootstrap compilers. */
10658 tree
10659 build_call_expr (tree fndecl, int n, ...)
10661 va_list ap;
10662 tree *argarray = XALLOCAVEC (tree, n);
10663 int i;
10665 va_start (ap, n);
10666 for (i = 0; i < n; i++)
10667 argarray[i] = va_arg (ap, tree);
10668 va_end (ap);
10669 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
10672 /* Build internal call expression. This is just like CALL_EXPR, except
10673 its CALL_EXPR_FN is NULL. It will get gimplified later into ordinary
10674 internal function. */
10676 tree
10677 build_call_expr_internal_loc (location_t loc, enum internal_fn ifn,
10678 tree type, int n, ...)
10680 va_list ap;
10681 int i;
10683 tree fn = build_call_1 (type, NULL_TREE, n);
10684 va_start (ap, n);
10685 for (i = 0; i < n; i++)
10686 CALL_EXPR_ARG (fn, i) = va_arg (ap, tree);
10687 va_end (ap);
10688 SET_EXPR_LOCATION (fn, loc);
10689 CALL_EXPR_IFN (fn) = ifn;
10690 return fn;
10693 /* Create a new constant string literal and return a char* pointer to it.
10694 The STRING_CST value is the LEN characters at STR. */
10695 tree
10696 build_string_literal (int len, const char *str)
10698 tree t, elem, index, type;
10700 t = build_string (len, str);
10701 elem = build_type_variant (char_type_node, 1, 0);
10702 index = build_index_type (size_int (len - 1));
10703 type = build_array_type (elem, index);
10704 TREE_TYPE (t) = type;
10705 TREE_CONSTANT (t) = 1;
10706 TREE_READONLY (t) = 1;
10707 TREE_STATIC (t) = 1;
10709 type = build_pointer_type (elem);
10710 t = build1 (ADDR_EXPR, type,
10711 build4 (ARRAY_REF, elem,
10712 t, integer_zero_node, NULL_TREE, NULL_TREE));
10713 return t;
10718 /* Return true if T (assumed to be a DECL) must be assigned a memory
10719 location. */
10721 bool
10722 needs_to_live_in_memory (const_tree t)
10724 return (TREE_ADDRESSABLE (t)
10725 || is_global_var (t)
10726 || (TREE_CODE (t) == RESULT_DECL
10727 && !DECL_BY_REFERENCE (t)
10728 && aggregate_value_p (t, current_function_decl)));
10731 /* Return value of a constant X and sign-extend it. */
10733 HOST_WIDE_INT
10734 int_cst_value (const_tree x)
10736 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10737 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10739 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10740 gcc_assert (cst_and_fits_in_hwi (x));
10742 if (bits < HOST_BITS_PER_WIDE_INT)
10744 bool negative = ((val >> (bits - 1)) & 1) != 0;
10745 if (negative)
10746 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10747 else
10748 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10751 return val;
10754 /* If TYPE is an integral or pointer type, return an integer type with
10755 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10756 if TYPE is already an integer type of signedness UNSIGNEDP. */
10758 tree
10759 signed_or_unsigned_type_for (int unsignedp, tree type)
10761 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10762 return type;
10764 if (TREE_CODE (type) == VECTOR_TYPE)
10766 tree inner = TREE_TYPE (type);
10767 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10768 if (!inner2)
10769 return NULL_TREE;
10770 if (inner == inner2)
10771 return type;
10772 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10775 if (!INTEGRAL_TYPE_P (type)
10776 && !POINTER_TYPE_P (type)
10777 && TREE_CODE (type) != OFFSET_TYPE)
10778 return NULL_TREE;
10780 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10783 /* If TYPE is an integral or pointer type, return an integer type with
10784 the same precision which is unsigned, or itself if TYPE is already an
10785 unsigned integer type. */
10787 tree
10788 unsigned_type_for (tree type)
10790 return signed_or_unsigned_type_for (1, type);
10793 /* If TYPE is an integral or pointer type, return an integer type with
10794 the same precision which is signed, or itself if TYPE is already a
10795 signed integer type. */
10797 tree
10798 signed_type_for (tree type)
10800 return signed_or_unsigned_type_for (0, type);
10803 /* If TYPE is a vector type, return a signed integer vector type with the
10804 same width and number of subparts. Otherwise return boolean_type_node. */
10806 tree
10807 truth_type_for (tree type)
10809 if (TREE_CODE (type) == VECTOR_TYPE)
10811 tree elem = lang_hooks.types.type_for_size
10812 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10813 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10815 else
10816 return boolean_type_node;
10819 /* Returns the largest value obtainable by casting something in INNER type to
10820 OUTER type. */
10822 tree
10823 upper_bound_in_type (tree outer, tree inner)
10825 unsigned int det = 0;
10826 unsigned oprec = TYPE_PRECISION (outer);
10827 unsigned iprec = TYPE_PRECISION (inner);
10828 unsigned prec;
10830 /* Compute a unique number for every combination. */
10831 det |= (oprec > iprec) ? 4 : 0;
10832 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10833 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10835 /* Determine the exponent to use. */
10836 switch (det)
10838 case 0:
10839 case 1:
10840 /* oprec <= iprec, outer: signed, inner: don't care. */
10841 prec = oprec - 1;
10842 break;
10843 case 2:
10844 case 3:
10845 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10846 prec = oprec;
10847 break;
10848 case 4:
10849 /* oprec > iprec, outer: signed, inner: signed. */
10850 prec = iprec - 1;
10851 break;
10852 case 5:
10853 /* oprec > iprec, outer: signed, inner: unsigned. */
10854 prec = iprec;
10855 break;
10856 case 6:
10857 /* oprec > iprec, outer: unsigned, inner: signed. */
10858 prec = oprec;
10859 break;
10860 case 7:
10861 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10862 prec = iprec;
10863 break;
10864 default:
10865 gcc_unreachable ();
10868 return wide_int_to_tree (outer,
10869 wi::mask (prec, false, TYPE_PRECISION (outer)));
10872 /* Returns the smallest value obtainable by casting something in INNER type to
10873 OUTER type. */
10875 tree
10876 lower_bound_in_type (tree outer, tree inner)
10878 unsigned oprec = TYPE_PRECISION (outer);
10879 unsigned iprec = TYPE_PRECISION (inner);
10881 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10882 and obtain 0. */
10883 if (TYPE_UNSIGNED (outer)
10884 /* If we are widening something of an unsigned type, OUTER type
10885 contains all values of INNER type. In particular, both INNER
10886 and OUTER types have zero in common. */
10887 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10888 return build_int_cst (outer, 0);
10889 else
10891 /* If we are widening a signed type to another signed type, we
10892 want to obtain -2^^(iprec-1). If we are keeping the
10893 precision or narrowing to a signed type, we want to obtain
10894 -2^(oprec-1). */
10895 unsigned prec = oprec > iprec ? iprec : oprec;
10896 return wide_int_to_tree (outer,
10897 wi::mask (prec - 1, true,
10898 TYPE_PRECISION (outer)));
10902 /* Return nonzero if two operands that are suitable for PHI nodes are
10903 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10904 SSA_NAME or invariant. Note that this is strictly an optimization.
10905 That is, callers of this function can directly call operand_equal_p
10906 and get the same result, only slower. */
10909 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10911 if (arg0 == arg1)
10912 return 1;
10913 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10914 return 0;
10915 return operand_equal_p (arg0, arg1, 0);
10918 /* Returns number of zeros at the end of binary representation of X. */
10920 tree
10921 num_ending_zeros (const_tree x)
10923 return build_int_cst (TREE_TYPE (x), wi::ctz (x));
10927 #define WALK_SUBTREE(NODE) \
10928 do \
10930 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10931 if (result) \
10932 return result; \
10934 while (0)
10936 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10937 be walked whenever a type is seen in the tree. Rest of operands and return
10938 value are as for walk_tree. */
10940 static tree
10941 walk_type_fields (tree type, walk_tree_fn func, void *data,
10942 hash_set<tree> *pset, walk_tree_lh lh)
10944 tree result = NULL_TREE;
10946 switch (TREE_CODE (type))
10948 case POINTER_TYPE:
10949 case REFERENCE_TYPE:
10950 case VECTOR_TYPE:
10951 /* We have to worry about mutually recursive pointers. These can't
10952 be written in C. They can in Ada. It's pathological, but
10953 there's an ACATS test (c38102a) that checks it. Deal with this
10954 by checking if we're pointing to another pointer, that one
10955 points to another pointer, that one does too, and we have no htab.
10956 If so, get a hash table. We check three levels deep to avoid
10957 the cost of the hash table if we don't need one. */
10958 if (POINTER_TYPE_P (TREE_TYPE (type))
10959 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10960 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10961 && !pset)
10963 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10964 func, data);
10965 if (result)
10966 return result;
10968 break;
10971 /* ... fall through ... */
10973 case COMPLEX_TYPE:
10974 WALK_SUBTREE (TREE_TYPE (type));
10975 break;
10977 case METHOD_TYPE:
10978 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10980 /* Fall through. */
10982 case FUNCTION_TYPE:
10983 WALK_SUBTREE (TREE_TYPE (type));
10985 tree arg;
10987 /* We never want to walk into default arguments. */
10988 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10989 WALK_SUBTREE (TREE_VALUE (arg));
10991 break;
10993 case ARRAY_TYPE:
10994 /* Don't follow this nodes's type if a pointer for fear that
10995 we'll have infinite recursion. If we have a PSET, then we
10996 need not fear. */
10997 if (pset
10998 || (!POINTER_TYPE_P (TREE_TYPE (type))
10999 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
11000 WALK_SUBTREE (TREE_TYPE (type));
11001 WALK_SUBTREE (TYPE_DOMAIN (type));
11002 break;
11004 case OFFSET_TYPE:
11005 WALK_SUBTREE (TREE_TYPE (type));
11006 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
11007 break;
11009 default:
11010 break;
11013 return NULL_TREE;
11016 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
11017 called with the DATA and the address of each sub-tree. If FUNC returns a
11018 non-NULL value, the traversal is stopped, and the value returned by FUNC
11019 is returned. If PSET is non-NULL it is used to record the nodes visited,
11020 and to avoid visiting a node more than once. */
11022 tree
11023 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
11024 hash_set<tree> *pset, walk_tree_lh lh)
11026 enum tree_code code;
11027 int walk_subtrees;
11028 tree result;
11030 #define WALK_SUBTREE_TAIL(NODE) \
11031 do \
11033 tp = & (NODE); \
11034 goto tail_recurse; \
11036 while (0)
11038 tail_recurse:
11039 /* Skip empty subtrees. */
11040 if (!*tp)
11041 return NULL_TREE;
11043 /* Don't walk the same tree twice, if the user has requested
11044 that we avoid doing so. */
11045 if (pset && pset->add (*tp))
11046 return NULL_TREE;
11048 /* Call the function. */
11049 walk_subtrees = 1;
11050 result = (*func) (tp, &walk_subtrees, data);
11052 /* If we found something, return it. */
11053 if (result)
11054 return result;
11056 code = TREE_CODE (*tp);
11058 /* Even if we didn't, FUNC may have decided that there was nothing
11059 interesting below this point in the tree. */
11060 if (!walk_subtrees)
11062 /* But we still need to check our siblings. */
11063 if (code == TREE_LIST)
11064 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11065 else if (code == OMP_CLAUSE)
11066 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11067 else
11068 return NULL_TREE;
11071 if (lh)
11073 result = (*lh) (tp, &walk_subtrees, func, data, pset);
11074 if (result || !walk_subtrees)
11075 return result;
11078 switch (code)
11080 case ERROR_MARK:
11081 case IDENTIFIER_NODE:
11082 case INTEGER_CST:
11083 case REAL_CST:
11084 case FIXED_CST:
11085 case VECTOR_CST:
11086 case STRING_CST:
11087 case BLOCK:
11088 case PLACEHOLDER_EXPR:
11089 case SSA_NAME:
11090 case FIELD_DECL:
11091 case RESULT_DECL:
11092 /* None of these have subtrees other than those already walked
11093 above. */
11094 break;
11096 case TREE_LIST:
11097 WALK_SUBTREE (TREE_VALUE (*tp));
11098 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11099 break;
11101 case TREE_VEC:
11103 int len = TREE_VEC_LENGTH (*tp);
11105 if (len == 0)
11106 break;
11108 /* Walk all elements but the first. */
11109 while (--len)
11110 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
11112 /* Now walk the first one as a tail call. */
11113 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
11116 case COMPLEX_CST:
11117 WALK_SUBTREE (TREE_REALPART (*tp));
11118 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
11120 case CONSTRUCTOR:
11122 unsigned HOST_WIDE_INT idx;
11123 constructor_elt *ce;
11125 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*tp), idx, &ce);
11126 idx++)
11127 WALK_SUBTREE (ce->value);
11129 break;
11131 case SAVE_EXPR:
11132 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
11134 case BIND_EXPR:
11136 tree decl;
11137 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
11139 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11140 into declarations that are just mentioned, rather than
11141 declared; they don't really belong to this part of the tree.
11142 And, we can see cycles: the initializer for a declaration
11143 can refer to the declaration itself. */
11144 WALK_SUBTREE (DECL_INITIAL (decl));
11145 WALK_SUBTREE (DECL_SIZE (decl));
11146 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11148 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
11151 case STATEMENT_LIST:
11153 tree_stmt_iterator i;
11154 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
11155 WALK_SUBTREE (*tsi_stmt_ptr (i));
11157 break;
11159 case OMP_CLAUSE:
11160 switch (OMP_CLAUSE_CODE (*tp))
11162 case OMP_CLAUSE_GANG:
11163 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11164 /* FALLTHRU */
11166 case OMP_CLAUSE_DEVICE_RESIDENT:
11167 case OMP_CLAUSE_USE_DEVICE:
11168 case OMP_CLAUSE_ASYNC:
11169 case OMP_CLAUSE_WAIT:
11170 case OMP_CLAUSE_WORKER:
11171 case OMP_CLAUSE_VECTOR:
11172 case OMP_CLAUSE_NUM_GANGS:
11173 case OMP_CLAUSE_NUM_WORKERS:
11174 case OMP_CLAUSE_VECTOR_LENGTH:
11175 case OMP_CLAUSE_PRIVATE:
11176 case OMP_CLAUSE_SHARED:
11177 case OMP_CLAUSE_FIRSTPRIVATE:
11178 case OMP_CLAUSE_COPYIN:
11179 case OMP_CLAUSE_COPYPRIVATE:
11180 case OMP_CLAUSE_FINAL:
11181 case OMP_CLAUSE_IF:
11182 case OMP_CLAUSE_NUM_THREADS:
11183 case OMP_CLAUSE_SCHEDULE:
11184 case OMP_CLAUSE_UNIFORM:
11185 case OMP_CLAUSE_DEPEND:
11186 case OMP_CLAUSE_NUM_TEAMS:
11187 case OMP_CLAUSE_THREAD_LIMIT:
11188 case OMP_CLAUSE_DEVICE:
11189 case OMP_CLAUSE_DIST_SCHEDULE:
11190 case OMP_CLAUSE_SAFELEN:
11191 case OMP_CLAUSE_SIMDLEN:
11192 case OMP_CLAUSE__LOOPTEMP_:
11193 case OMP_CLAUSE__SIMDUID_:
11194 case OMP_CLAUSE__CILK_FOR_COUNT_:
11195 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
11196 /* FALLTHRU */
11198 case OMP_CLAUSE_INDEPENDENT:
11199 case OMP_CLAUSE_NOWAIT:
11200 case OMP_CLAUSE_ORDERED:
11201 case OMP_CLAUSE_DEFAULT:
11202 case OMP_CLAUSE_UNTIED:
11203 case OMP_CLAUSE_MERGEABLE:
11204 case OMP_CLAUSE_PROC_BIND:
11205 case OMP_CLAUSE_INBRANCH:
11206 case OMP_CLAUSE_NOTINBRANCH:
11207 case OMP_CLAUSE_FOR:
11208 case OMP_CLAUSE_PARALLEL:
11209 case OMP_CLAUSE_SECTIONS:
11210 case OMP_CLAUSE_TASKGROUP:
11211 case OMP_CLAUSE_AUTO:
11212 case OMP_CLAUSE_SEQ:
11213 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11215 case OMP_CLAUSE_LASTPRIVATE:
11216 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11217 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
11218 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11220 case OMP_CLAUSE_COLLAPSE:
11222 int i;
11223 for (i = 0; i < 3; i++)
11224 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11225 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11228 case OMP_CLAUSE_LINEAR:
11229 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11230 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STEP (*tp));
11231 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STMT (*tp));
11232 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11234 case OMP_CLAUSE_ALIGNED:
11235 case OMP_CLAUSE_FROM:
11236 case OMP_CLAUSE_TO:
11237 case OMP_CLAUSE_MAP:
11238 case OMP_CLAUSE__CACHE_:
11239 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11240 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11241 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11243 case OMP_CLAUSE_REDUCTION:
11245 int i;
11246 for (i = 0; i < 4; i++)
11247 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11248 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11251 default:
11252 gcc_unreachable ();
11254 break;
11256 case TARGET_EXPR:
11258 int i, len;
11260 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11261 But, we only want to walk once. */
11262 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11263 for (i = 0; i < len; ++i)
11264 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11265 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11268 case DECL_EXPR:
11269 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11270 defining. We only want to walk into these fields of a type in this
11271 case and not in the general case of a mere reference to the type.
11273 The criterion is as follows: if the field can be an expression, it
11274 must be walked only here. This should be in keeping with the fields
11275 that are directly gimplified in gimplify_type_sizes in order for the
11276 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11277 variable-sized types.
11279 Note that DECLs get walked as part of processing the BIND_EXPR. */
11280 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11282 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11283 if (TREE_CODE (*type_p) == ERROR_MARK)
11284 return NULL_TREE;
11286 /* Call the function for the type. See if it returns anything or
11287 doesn't want us to continue. If we are to continue, walk both
11288 the normal fields and those for the declaration case. */
11289 result = (*func) (type_p, &walk_subtrees, data);
11290 if (result || !walk_subtrees)
11291 return result;
11293 /* But do not walk a pointed-to type since it may itself need to
11294 be walked in the declaration case if it isn't anonymous. */
11295 if (!POINTER_TYPE_P (*type_p))
11297 result = walk_type_fields (*type_p, func, data, pset, lh);
11298 if (result)
11299 return result;
11302 /* If this is a record type, also walk the fields. */
11303 if (RECORD_OR_UNION_TYPE_P (*type_p))
11305 tree field;
11307 for (field = TYPE_FIELDS (*type_p); field;
11308 field = DECL_CHAIN (field))
11310 /* We'd like to look at the type of the field, but we can
11311 easily get infinite recursion. So assume it's pointed
11312 to elsewhere in the tree. Also, ignore things that
11313 aren't fields. */
11314 if (TREE_CODE (field) != FIELD_DECL)
11315 continue;
11317 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11318 WALK_SUBTREE (DECL_SIZE (field));
11319 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11320 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11321 WALK_SUBTREE (DECL_QUALIFIER (field));
11325 /* Same for scalar types. */
11326 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11327 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11328 || TREE_CODE (*type_p) == INTEGER_TYPE
11329 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11330 || TREE_CODE (*type_p) == REAL_TYPE)
11332 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11333 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11336 WALK_SUBTREE (TYPE_SIZE (*type_p));
11337 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11339 /* FALLTHRU */
11341 default:
11342 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11344 int i, len;
11346 /* Walk over all the sub-trees of this operand. */
11347 len = TREE_OPERAND_LENGTH (*tp);
11349 /* Go through the subtrees. We need to do this in forward order so
11350 that the scope of a FOR_EXPR is handled properly. */
11351 if (len)
11353 for (i = 0; i < len - 1; ++i)
11354 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11355 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11358 /* If this is a type, walk the needed fields in the type. */
11359 else if (TYPE_P (*tp))
11360 return walk_type_fields (*tp, func, data, pset, lh);
11361 break;
11364 /* We didn't find what we were looking for. */
11365 return NULL_TREE;
11367 #undef WALK_SUBTREE_TAIL
11369 #undef WALK_SUBTREE
11371 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11373 tree
11374 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11375 walk_tree_lh lh)
11377 tree result;
11379 hash_set<tree> pset;
11380 result = walk_tree_1 (tp, func, data, &pset, lh);
11381 return result;
11385 tree
11386 tree_block (tree t)
11388 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11390 if (IS_EXPR_CODE_CLASS (c))
11391 return LOCATION_BLOCK (t->exp.locus);
11392 gcc_unreachable ();
11393 return NULL;
11396 void
11397 tree_set_block (tree t, tree b)
11399 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11401 if (IS_EXPR_CODE_CLASS (c))
11403 if (b)
11404 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
11405 else
11406 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
11408 else
11409 gcc_unreachable ();
11412 /* Create a nameless artificial label and put it in the current
11413 function context. The label has a location of LOC. Returns the
11414 newly created label. */
11416 tree
11417 create_artificial_label (location_t loc)
11419 tree lab = build_decl (loc,
11420 LABEL_DECL, NULL_TREE, void_type_node);
11422 DECL_ARTIFICIAL (lab) = 1;
11423 DECL_IGNORED_P (lab) = 1;
11424 DECL_CONTEXT (lab) = current_function_decl;
11425 return lab;
11428 /* Given a tree, try to return a useful variable name that we can use
11429 to prefix a temporary that is being assigned the value of the tree.
11430 I.E. given <temp> = &A, return A. */
11432 const char *
11433 get_name (tree t)
11435 tree stripped_decl;
11437 stripped_decl = t;
11438 STRIP_NOPS (stripped_decl);
11439 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11440 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11441 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11443 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11444 if (!name)
11445 return NULL;
11446 return IDENTIFIER_POINTER (name);
11448 else
11450 switch (TREE_CODE (stripped_decl))
11452 case ADDR_EXPR:
11453 return get_name (TREE_OPERAND (stripped_decl, 0));
11454 default:
11455 return NULL;
11460 /* Return true if TYPE has a variable argument list. */
11462 bool
11463 stdarg_p (const_tree fntype)
11465 function_args_iterator args_iter;
11466 tree n = NULL_TREE, t;
11468 if (!fntype)
11469 return false;
11471 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
11473 n = t;
11476 return n != NULL_TREE && n != void_type_node;
11479 /* Return true if TYPE has a prototype. */
11481 bool
11482 prototype_p (tree fntype)
11484 tree t;
11486 gcc_assert (fntype != NULL_TREE);
11488 t = TYPE_ARG_TYPES (fntype);
11489 return (t != NULL_TREE);
11492 /* If BLOCK is inlined from an __attribute__((__artificial__))
11493 routine, return pointer to location from where it has been
11494 called. */
11495 location_t *
11496 block_nonartificial_location (tree block)
11498 location_t *ret = NULL;
11500 while (block && TREE_CODE (block) == BLOCK
11501 && BLOCK_ABSTRACT_ORIGIN (block))
11503 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11505 while (TREE_CODE (ao) == BLOCK
11506 && BLOCK_ABSTRACT_ORIGIN (ao)
11507 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11508 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11510 if (TREE_CODE (ao) == FUNCTION_DECL)
11512 /* If AO is an artificial inline, point RET to the
11513 call site locus at which it has been inlined and continue
11514 the loop, in case AO's caller is also an artificial
11515 inline. */
11516 if (DECL_DECLARED_INLINE_P (ao)
11517 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11518 ret = &BLOCK_SOURCE_LOCATION (block);
11519 else
11520 break;
11522 else if (TREE_CODE (ao) != BLOCK)
11523 break;
11525 block = BLOCK_SUPERCONTEXT (block);
11527 return ret;
11531 /* If EXP is inlined from an __attribute__((__artificial__))
11532 function, return the location of the original call expression. */
11534 location_t
11535 tree_nonartificial_location (tree exp)
11537 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11539 if (loc)
11540 return *loc;
11541 else
11542 return EXPR_LOCATION (exp);
11546 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11547 nodes. */
11549 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11551 hashval_t
11552 cl_option_hasher::hash (tree x)
11554 const_tree const t = x;
11555 const char *p;
11556 size_t i;
11557 size_t len = 0;
11558 hashval_t hash = 0;
11560 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11562 p = (const char *)TREE_OPTIMIZATION (t);
11563 len = sizeof (struct cl_optimization);
11566 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11567 return cl_target_option_hash (TREE_TARGET_OPTION (t));
11569 else
11570 gcc_unreachable ();
11572 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11573 something else. */
11574 for (i = 0; i < len; i++)
11575 if (p[i])
11576 hash = (hash << 4) ^ ((i << 2) | p[i]);
11578 return hash;
11581 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11582 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11583 same. */
11585 bool
11586 cl_option_hasher::equal (tree x, tree y)
11588 const_tree const xt = x;
11589 const_tree const yt = y;
11590 const char *xp;
11591 const char *yp;
11592 size_t len;
11594 if (TREE_CODE (xt) != TREE_CODE (yt))
11595 return 0;
11597 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11599 xp = (const char *)TREE_OPTIMIZATION (xt);
11600 yp = (const char *)TREE_OPTIMIZATION (yt);
11601 len = sizeof (struct cl_optimization);
11604 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11606 return cl_target_option_eq (TREE_TARGET_OPTION (xt),
11607 TREE_TARGET_OPTION (yt));
11610 else
11611 gcc_unreachable ();
11613 return (memcmp (xp, yp, len) == 0);
11616 /* Build an OPTIMIZATION_NODE based on the options in OPTS. */
11618 tree
11619 build_optimization_node (struct gcc_options *opts)
11621 tree t;
11623 /* Use the cache of optimization nodes. */
11625 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11626 opts);
11628 tree *slot = cl_option_hash_table->find_slot (cl_optimization_node, INSERT);
11629 t = *slot;
11630 if (!t)
11632 /* Insert this one into the hash table. */
11633 t = cl_optimization_node;
11634 *slot = t;
11636 /* Make a new node for next time round. */
11637 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11640 return t;
11643 /* Build a TARGET_OPTION_NODE based on the options in OPTS. */
11645 tree
11646 build_target_option_node (struct gcc_options *opts)
11648 tree t;
11650 /* Use the cache of optimization nodes. */
11652 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11653 opts);
11655 tree *slot = cl_option_hash_table->find_slot (cl_target_option_node, INSERT);
11656 t = *slot;
11657 if (!t)
11659 /* Insert this one into the hash table. */
11660 t = cl_target_option_node;
11661 *slot = t;
11663 /* Make a new node for next time round. */
11664 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11667 return t;
11670 /* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
11671 so that they aren't saved during PCH writing. */
11673 void
11674 prepare_target_option_nodes_for_pch (void)
11676 hash_table<cl_option_hasher>::iterator iter = cl_option_hash_table->begin ();
11677 for (; iter != cl_option_hash_table->end (); ++iter)
11678 if (TREE_CODE (*iter) == TARGET_OPTION_NODE)
11679 TREE_TARGET_GLOBALS (*iter) = NULL;
11682 /* Determine the "ultimate origin" of a block. The block may be an inlined
11683 instance of an inlined instance of a block which is local to an inline
11684 function, so we have to trace all of the way back through the origin chain
11685 to find out what sort of node actually served as the original seed for the
11686 given block. */
11688 tree
11689 block_ultimate_origin (const_tree block)
11691 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11693 /* BLOCK_ABSTRACT_ORIGIN can point to itself; ignore that if
11694 we're trying to output the abstract instance of this function. */
11695 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11696 return NULL_TREE;
11698 if (immediate_origin == NULL_TREE)
11699 return NULL_TREE;
11700 else
11702 tree ret_val;
11703 tree lookahead = immediate_origin;
11707 ret_val = lookahead;
11708 lookahead = (TREE_CODE (ret_val) == BLOCK
11709 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11711 while (lookahead != NULL && lookahead != ret_val);
11713 /* The block's abstract origin chain may not be the *ultimate* origin of
11714 the block. It could lead to a DECL that has an abstract origin set.
11715 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11716 will give us if it has one). Note that DECL's abstract origins are
11717 supposed to be the most distant ancestor (or so decl_ultimate_origin
11718 claims), so we don't need to loop following the DECL origins. */
11719 if (DECL_P (ret_val))
11720 return DECL_ORIGIN (ret_val);
11722 return ret_val;
11726 /* Return true iff conversion from INNER_TYPE to OUTER_TYPE generates
11727 no instruction. */
11729 bool
11730 tree_nop_conversion_p (const_tree outer_type, const_tree inner_type)
11732 /* Use precision rather then machine mode when we can, which gives
11733 the correct answer even for submode (bit-field) types. */
11734 if ((INTEGRAL_TYPE_P (outer_type)
11735 || POINTER_TYPE_P (outer_type)
11736 || TREE_CODE (outer_type) == OFFSET_TYPE)
11737 && (INTEGRAL_TYPE_P (inner_type)
11738 || POINTER_TYPE_P (inner_type)
11739 || TREE_CODE (inner_type) == OFFSET_TYPE))
11740 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11742 /* Otherwise fall back on comparing machine modes (e.g. for
11743 aggregate types, floats). */
11744 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11747 /* Return true iff conversion in EXP generates no instruction. Mark
11748 it inline so that we fully inline into the stripping functions even
11749 though we have two uses of this function. */
11751 static inline bool
11752 tree_nop_conversion (const_tree exp)
11754 tree outer_type, inner_type;
11756 if (!CONVERT_EXPR_P (exp)
11757 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11758 return false;
11759 if (TREE_OPERAND (exp, 0) == error_mark_node)
11760 return false;
11762 outer_type = TREE_TYPE (exp);
11763 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11765 if (!inner_type)
11766 return false;
11768 return tree_nop_conversion_p (outer_type, inner_type);
11771 /* Return true iff conversion in EXP generates no instruction. Don't
11772 consider conversions changing the signedness. */
11774 static bool
11775 tree_sign_nop_conversion (const_tree exp)
11777 tree outer_type, inner_type;
11779 if (!tree_nop_conversion (exp))
11780 return false;
11782 outer_type = TREE_TYPE (exp);
11783 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11785 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11786 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11789 /* Strip conversions from EXP according to tree_nop_conversion and
11790 return the resulting expression. */
11792 tree
11793 tree_strip_nop_conversions (tree exp)
11795 while (tree_nop_conversion (exp))
11796 exp = TREE_OPERAND (exp, 0);
11797 return exp;
11800 /* Strip conversions from EXP according to tree_sign_nop_conversion
11801 and return the resulting expression. */
11803 tree
11804 tree_strip_sign_nop_conversions (tree exp)
11806 while (tree_sign_nop_conversion (exp))
11807 exp = TREE_OPERAND (exp, 0);
11808 return exp;
11811 /* Avoid any floating point extensions from EXP. */
11812 tree
11813 strip_float_extensions (tree exp)
11815 tree sub, expt, subt;
11817 /* For floating point constant look up the narrowest type that can hold
11818 it properly and handle it like (type)(narrowest_type)constant.
11819 This way we can optimize for instance a=a*2.0 where "a" is float
11820 but 2.0 is double constant. */
11821 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11823 REAL_VALUE_TYPE orig;
11824 tree type = NULL;
11826 orig = TREE_REAL_CST (exp);
11827 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11828 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11829 type = float_type_node;
11830 else if (TYPE_PRECISION (TREE_TYPE (exp))
11831 > TYPE_PRECISION (double_type_node)
11832 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11833 type = double_type_node;
11834 if (type)
11835 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11838 if (!CONVERT_EXPR_P (exp))
11839 return exp;
11841 sub = TREE_OPERAND (exp, 0);
11842 subt = TREE_TYPE (sub);
11843 expt = TREE_TYPE (exp);
11845 if (!FLOAT_TYPE_P (subt))
11846 return exp;
11848 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11849 return exp;
11851 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11852 return exp;
11854 return strip_float_extensions (sub);
11857 /* Strip out all handled components that produce invariant
11858 offsets. */
11860 const_tree
11861 strip_invariant_refs (const_tree op)
11863 while (handled_component_p (op))
11865 switch (TREE_CODE (op))
11867 case ARRAY_REF:
11868 case ARRAY_RANGE_REF:
11869 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11870 || TREE_OPERAND (op, 2) != NULL_TREE
11871 || TREE_OPERAND (op, 3) != NULL_TREE)
11872 return NULL;
11873 break;
11875 case COMPONENT_REF:
11876 if (TREE_OPERAND (op, 2) != NULL_TREE)
11877 return NULL;
11878 break;
11880 default:;
11882 op = TREE_OPERAND (op, 0);
11885 return op;
11888 static GTY(()) tree gcc_eh_personality_decl;
11890 /* Return the GCC personality function decl. */
11892 tree
11893 lhd_gcc_personality (void)
11895 if (!gcc_eh_personality_decl)
11896 gcc_eh_personality_decl = build_personality_function ("gcc");
11897 return gcc_eh_personality_decl;
11900 /* TARGET is a call target of GIMPLE call statement
11901 (obtained by gimple_call_fn). Return true if it is
11902 OBJ_TYPE_REF representing an virtual call of C++ method.
11903 (As opposed to OBJ_TYPE_REF representing objc calls
11904 through a cast where middle-end devirtualization machinery
11905 can't apply.) */
11907 bool
11908 virtual_method_call_p (tree target)
11910 if (TREE_CODE (target) != OBJ_TYPE_REF)
11911 return false;
11912 tree t = TREE_TYPE (target);
11913 gcc_checking_assert (TREE_CODE (t) == POINTER_TYPE);
11914 t = TREE_TYPE (t);
11915 if (TREE_CODE (t) == FUNCTION_TYPE)
11916 return false;
11917 gcc_checking_assert (TREE_CODE (t) == METHOD_TYPE);
11918 /* If we do not have BINFO associated, it means that type was built
11919 without devirtualization enabled. Do not consider this a virtual
11920 call. */
11921 if (!TYPE_BINFO (obj_type_ref_class (target)))
11922 return false;
11923 return true;
11926 /* REF is OBJ_TYPE_REF, return the class the ref corresponds to. */
11928 tree
11929 obj_type_ref_class (tree ref)
11931 gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF);
11932 ref = TREE_TYPE (ref);
11933 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
11934 ref = TREE_TYPE (ref);
11935 /* We look for type THIS points to. ObjC also builds
11936 OBJ_TYPE_REF with non-method calls, Their first parameter
11937 ID however also corresponds to class type. */
11938 gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE
11939 || TREE_CODE (ref) == FUNCTION_TYPE);
11940 ref = TREE_VALUE (TYPE_ARG_TYPES (ref));
11941 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
11942 return TREE_TYPE (ref);
11945 /* Return true if T is in anonymous namespace. */
11947 bool
11948 type_in_anonymous_namespace_p (const_tree t)
11950 /* TREE_PUBLIC of TYPE_STUB_DECL may not be properly set for
11951 bulitin types; those have CONTEXT NULL. */
11952 if (!TYPE_CONTEXT (t))
11953 return false;
11954 return (TYPE_STUB_DECL (t) && !TREE_PUBLIC (TYPE_STUB_DECL (t)));
11957 /* Try to find a base info of BINFO that would have its field decl at offset
11958 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11959 found, return, otherwise return NULL_TREE. */
11961 tree
11962 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
11964 tree type = BINFO_TYPE (binfo);
11966 while (true)
11968 HOST_WIDE_INT pos, size;
11969 tree fld;
11970 int i;
11972 if (types_same_for_odr (type, expected_type))
11973 return binfo;
11974 if (offset < 0)
11975 return NULL_TREE;
11977 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
11979 if (TREE_CODE (fld) != FIELD_DECL || !DECL_ARTIFICIAL (fld))
11980 continue;
11982 pos = int_bit_position (fld);
11983 size = tree_to_uhwi (DECL_SIZE (fld));
11984 if (pos <= offset && (pos + size) > offset)
11985 break;
11987 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11988 return NULL_TREE;
11990 /* Offset 0 indicates the primary base, whose vtable contents are
11991 represented in the binfo for the derived class. */
11992 else if (offset != 0)
11994 tree base_binfo, binfo2 = binfo;
11996 /* Find BINFO corresponding to FLD. This is bit harder
11997 by a fact that in virtual inheritance we may need to walk down
11998 the non-virtual inheritance chain. */
11999 while (true)
12001 tree containing_binfo = NULL, found_binfo = NULL;
12002 for (i = 0; BINFO_BASE_ITERATE (binfo2, i, base_binfo); i++)
12003 if (types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
12005 found_binfo = base_binfo;
12006 break;
12008 else
12009 if ((tree_to_shwi (BINFO_OFFSET (base_binfo))
12010 - tree_to_shwi (BINFO_OFFSET (binfo)))
12011 * BITS_PER_UNIT < pos
12012 /* Rule out types with no virtual methods or we can get confused
12013 here by zero sized bases. */
12014 && TYPE_BINFO (BINFO_TYPE (base_binfo))
12015 && BINFO_VTABLE (TYPE_BINFO (BINFO_TYPE (base_binfo)))
12016 && (!containing_binfo
12017 || (tree_to_shwi (BINFO_OFFSET (containing_binfo))
12018 < tree_to_shwi (BINFO_OFFSET (base_binfo)))))
12019 containing_binfo = base_binfo;
12020 if (found_binfo)
12022 binfo = found_binfo;
12023 break;
12025 if (!containing_binfo)
12026 return NULL_TREE;
12027 binfo2 = containing_binfo;
12031 type = TREE_TYPE (fld);
12032 offset -= pos;
12036 /* Returns true if X is a typedef decl. */
12038 bool
12039 is_typedef_decl (tree x)
12041 return (x && TREE_CODE (x) == TYPE_DECL
12042 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
12045 /* Returns true iff TYPE is a type variant created for a typedef. */
12047 bool
12048 typedef_variant_p (tree type)
12050 return is_typedef_decl (TYPE_NAME (type));
12053 /* Warn about a use of an identifier which was marked deprecated. */
12054 void
12055 warn_deprecated_use (tree node, tree attr)
12057 const char *msg;
12059 if (node == 0 || !warn_deprecated_decl)
12060 return;
12062 if (!attr)
12064 if (DECL_P (node))
12065 attr = DECL_ATTRIBUTES (node);
12066 else if (TYPE_P (node))
12068 tree decl = TYPE_STUB_DECL (node);
12069 if (decl)
12070 attr = lookup_attribute ("deprecated",
12071 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
12075 if (attr)
12076 attr = lookup_attribute ("deprecated", attr);
12078 if (attr)
12079 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
12080 else
12081 msg = NULL;
12083 bool w;
12084 if (DECL_P (node))
12086 if (msg)
12087 w = warning (OPT_Wdeprecated_declarations,
12088 "%qD is deprecated: %s", node, msg);
12089 else
12090 w = warning (OPT_Wdeprecated_declarations,
12091 "%qD is deprecated", node);
12092 if (w)
12093 inform (DECL_SOURCE_LOCATION (node), "declared here");
12095 else if (TYPE_P (node))
12097 tree what = NULL_TREE;
12098 tree decl = TYPE_STUB_DECL (node);
12100 if (TYPE_NAME (node))
12102 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12103 what = TYPE_NAME (node);
12104 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12105 && DECL_NAME (TYPE_NAME (node)))
12106 what = DECL_NAME (TYPE_NAME (node));
12109 if (decl)
12111 if (what)
12113 if (msg)
12114 w = warning (OPT_Wdeprecated_declarations,
12115 "%qE is deprecated: %s", what, msg);
12116 else
12117 w = warning (OPT_Wdeprecated_declarations,
12118 "%qE is deprecated", what);
12120 else
12122 if (msg)
12123 w = warning (OPT_Wdeprecated_declarations,
12124 "type is deprecated: %s", msg);
12125 else
12126 w = warning (OPT_Wdeprecated_declarations,
12127 "type is deprecated");
12129 if (w)
12130 inform (DECL_SOURCE_LOCATION (decl), "declared here");
12132 else
12134 if (what)
12136 if (msg)
12137 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
12138 what, msg);
12139 else
12140 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
12142 else
12144 if (msg)
12145 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
12146 msg);
12147 else
12148 warning (OPT_Wdeprecated_declarations, "type is deprecated");
12154 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12155 somewhere in it. */
12157 bool
12158 contains_bitfld_component_ref_p (const_tree ref)
12160 while (handled_component_p (ref))
12162 if (TREE_CODE (ref) == COMPONENT_REF
12163 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12164 return true;
12165 ref = TREE_OPERAND (ref, 0);
12168 return false;
12171 /* Try to determine whether a TRY_CATCH expression can fall through.
12172 This is a subroutine of block_may_fallthru. */
12174 static bool
12175 try_catch_may_fallthru (const_tree stmt)
12177 tree_stmt_iterator i;
12179 /* If the TRY block can fall through, the whole TRY_CATCH can
12180 fall through. */
12181 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
12182 return true;
12184 i = tsi_start (TREE_OPERAND (stmt, 1));
12185 switch (TREE_CODE (tsi_stmt (i)))
12187 case CATCH_EXPR:
12188 /* We expect to see a sequence of CATCH_EXPR trees, each with a
12189 catch expression and a body. The whole TRY_CATCH may fall
12190 through iff any of the catch bodies falls through. */
12191 for (; !tsi_end_p (i); tsi_next (&i))
12193 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
12194 return true;
12196 return false;
12198 case EH_FILTER_EXPR:
12199 /* The exception filter expression only matters if there is an
12200 exception. If the exception does not match EH_FILTER_TYPES,
12201 we will execute EH_FILTER_FAILURE, and we will fall through
12202 if that falls through. If the exception does match
12203 EH_FILTER_TYPES, the stack unwinder will continue up the
12204 stack, so we will not fall through. We don't know whether we
12205 will throw an exception which matches EH_FILTER_TYPES or not,
12206 so we just ignore EH_FILTER_TYPES and assume that we might
12207 throw an exception which doesn't match. */
12208 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
12210 default:
12211 /* This case represents statements to be executed when an
12212 exception occurs. Those statements are implicitly followed
12213 by a RESX statement to resume execution after the exception.
12214 So in this case the TRY_CATCH never falls through. */
12215 return false;
12219 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
12220 need not be 100% accurate; simply be conservative and return true if we
12221 don't know. This is used only to avoid stupidly generating extra code.
12222 If we're wrong, we'll just delete the extra code later. */
12224 bool
12225 block_may_fallthru (const_tree block)
12227 /* This CONST_CAST is okay because expr_last returns its argument
12228 unmodified and we assign it to a const_tree. */
12229 const_tree stmt = expr_last (CONST_CAST_TREE (block));
12231 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
12233 case GOTO_EXPR:
12234 case RETURN_EXPR:
12235 /* Easy cases. If the last statement of the block implies
12236 control transfer, then we can't fall through. */
12237 return false;
12239 case SWITCH_EXPR:
12240 /* If SWITCH_LABELS is set, this is lowered, and represents a
12241 branch to a selected label and hence can not fall through.
12242 Otherwise SWITCH_BODY is set, and the switch can fall
12243 through. */
12244 return SWITCH_LABELS (stmt) == NULL_TREE;
12246 case COND_EXPR:
12247 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
12248 return true;
12249 return block_may_fallthru (COND_EXPR_ELSE (stmt));
12251 case BIND_EXPR:
12252 return block_may_fallthru (BIND_EXPR_BODY (stmt));
12254 case TRY_CATCH_EXPR:
12255 return try_catch_may_fallthru (stmt);
12257 case TRY_FINALLY_EXPR:
12258 /* The finally clause is always executed after the try clause,
12259 so if it does not fall through, then the try-finally will not
12260 fall through. Otherwise, if the try clause does not fall
12261 through, then when the finally clause falls through it will
12262 resume execution wherever the try clause was going. So the
12263 whole try-finally will only fall through if both the try
12264 clause and the finally clause fall through. */
12265 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
12266 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
12268 case MODIFY_EXPR:
12269 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
12270 stmt = TREE_OPERAND (stmt, 1);
12271 else
12272 return true;
12273 /* FALLTHRU */
12275 case CALL_EXPR:
12276 /* Functions that do not return do not fall through. */
12277 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
12279 case CLEANUP_POINT_EXPR:
12280 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12282 case TARGET_EXPR:
12283 return block_may_fallthru (TREE_OPERAND (stmt, 1));
12285 case ERROR_MARK:
12286 return true;
12288 default:
12289 return lang_hooks.block_may_fallthru (stmt);
12293 /* True if we are using EH to handle cleanups. */
12294 static bool using_eh_for_cleanups_flag = false;
12296 /* This routine is called from front ends to indicate eh should be used for
12297 cleanups. */
12298 void
12299 using_eh_for_cleanups (void)
12301 using_eh_for_cleanups_flag = true;
12304 /* Query whether EH is used for cleanups. */
12305 bool
12306 using_eh_for_cleanups_p (void)
12308 return using_eh_for_cleanups_flag;
12311 /* Wrapper for tree_code_name to ensure that tree code is valid */
12312 const char *
12313 get_tree_code_name (enum tree_code code)
12315 const char *invalid = "<invalid tree code>";
12317 if (code >= MAX_TREE_CODES)
12318 return invalid;
12320 return tree_code_name[code];
12323 /* Drops the TREE_OVERFLOW flag from T. */
12325 tree
12326 drop_tree_overflow (tree t)
12328 gcc_checking_assert (TREE_OVERFLOW (t));
12330 /* For tree codes with a sharing machinery re-build the result. */
12331 if (TREE_CODE (t) == INTEGER_CST)
12332 return wide_int_to_tree (TREE_TYPE (t), t);
12334 /* Otherwise, as all tcc_constants are possibly shared, copy the node
12335 and drop the flag. */
12336 t = copy_node (t);
12337 TREE_OVERFLOW (t) = 0;
12338 return t;
12341 /* Given a memory reference expression T, return its base address.
12342 The base address of a memory reference expression is the main
12343 object being referenced. For instance, the base address for
12344 'array[i].fld[j]' is 'array'. You can think of this as stripping
12345 away the offset part from a memory address.
12347 This function calls handled_component_p to strip away all the inner
12348 parts of the memory reference until it reaches the base object. */
12350 tree
12351 get_base_address (tree t)
12353 while (handled_component_p (t))
12354 t = TREE_OPERAND (t, 0);
12356 if ((TREE_CODE (t) == MEM_REF
12357 || TREE_CODE (t) == TARGET_MEM_REF)
12358 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
12359 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
12361 /* ??? Either the alias oracle or all callers need to properly deal
12362 with WITH_SIZE_EXPRs before we can look through those. */
12363 if (TREE_CODE (t) == WITH_SIZE_EXPR)
12364 return NULL_TREE;
12366 return t;
12369 /* Return the machine mode of T. For vectors, returns the mode of the
12370 inner type. The main use case is to feed the result to HONOR_NANS,
12371 avoiding the BLKmode that a direct TYPE_MODE (T) might return. */
12373 machine_mode
12374 element_mode (const_tree t)
12376 if (!TYPE_P (t))
12377 t = TREE_TYPE (t);
12378 if (VECTOR_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
12379 t = TREE_TYPE (t);
12380 return TYPE_MODE (t);
12383 #include "gt-tree.h"