gcc/
[official-gcc.git] / gcc / tree.c
blobd44bc180f518d8e2dc9a5adb2477908e15e09602
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file contains the low level primitives for operating on tree nodes,
21 including allocation, list operations, interning of identifiers,
22 construction of data type nodes and statement nodes,
23 and construction of type conversion nodes. It also contains
24 tables index by tree code that describe how to take apart
25 nodes of that code.
27 It is intended to be language-independent but can occasionally
28 calls language-dependent routines. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "flags.h"
35 #include "alias.h"
36 #include "symtab.h"
37 #include "tree.h"
38 #include "fold-const.h"
39 #include "stor-layout.h"
40 #include "calls.h"
41 #include "attribs.h"
42 #include "varasm.h"
43 #include "tm_p.h"
44 #include "hard-reg-set.h"
45 #include "function.h"
46 #include "obstack.h"
47 #include "toplev.h" /* get_random_seed */
48 #include "filenames.h"
49 #include "output.h"
50 #include "target.h"
51 #include "common/common-target.h"
52 #include "langhooks.h"
53 #include "tree-inline.h"
54 #include "tree-iterator.h"
55 #include "predict.h"
56 #include "dominance.h"
57 #include "cfg.h"
58 #include "basic-block.h"
59 #include "bitmap.h"
60 #include "tree-ssa-alias.h"
61 #include "internal-fn.h"
62 #include "gimple-expr.h"
63 #include "gimple.h"
64 #include "gimple-iterator.h"
65 #include "gimplify.h"
66 #include "gimple-ssa.h"
67 #include "plugin-api.h"
68 #include "ipa-ref.h"
69 #include "cgraph.h"
70 #include "tree-phinodes.h"
71 #include "stringpool.h"
72 #include "tree-ssanames.h"
73 #include "rtl.h"
74 #include "insn-config.h"
75 #include "expmed.h"
76 #include "dojump.h"
77 #include "explow.h"
78 #include "emit-rtl.h"
79 #include "stmt.h"
80 #include "expr.h"
81 #include "tree-dfa.h"
82 #include "params.h"
83 #include "tree-pass.h"
84 #include "langhooks-def.h"
85 #include "diagnostic.h"
86 #include "tree-diagnostic.h"
87 #include "tree-pretty-print.h"
88 #include "except.h"
89 #include "debug.h"
90 #include "intl.h"
91 #include "builtins.h"
92 #include "print-tree.h"
93 #include "ipa-utils.h"
95 /* Tree code classes. */
97 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
98 #define END_OF_BASE_TREE_CODES tcc_exceptional,
100 const enum tree_code_class tree_code_type[] = {
101 #include "all-tree.def"
104 #undef DEFTREECODE
105 #undef END_OF_BASE_TREE_CODES
107 /* Table indexed by tree code giving number of expression
108 operands beyond the fixed part of the node structure.
109 Not used for types or decls. */
111 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
112 #define END_OF_BASE_TREE_CODES 0,
114 const unsigned char tree_code_length[] = {
115 #include "all-tree.def"
118 #undef DEFTREECODE
119 #undef END_OF_BASE_TREE_CODES
121 /* Names of tree components.
122 Used for printing out the tree and error messages. */
123 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
124 #define END_OF_BASE_TREE_CODES "@dummy",
126 static const char *const tree_code_name[] = {
127 #include "all-tree.def"
130 #undef DEFTREECODE
131 #undef END_OF_BASE_TREE_CODES
133 /* Each tree code class has an associated string representation.
134 These must correspond to the tree_code_class entries. */
136 const char *const tree_code_class_strings[] =
138 "exceptional",
139 "constant",
140 "type",
141 "declaration",
142 "reference",
143 "comparison",
144 "unary",
145 "binary",
146 "statement",
147 "vl_exp",
148 "expression"
151 /* obstack.[ch] explicitly declined to prototype this. */
152 extern int _obstack_allocated_p (struct obstack *h, void *obj);
154 /* Statistics-gathering stuff. */
156 static int tree_code_counts[MAX_TREE_CODES];
157 int tree_node_counts[(int) all_kinds];
158 int tree_node_sizes[(int) all_kinds];
160 /* Keep in sync with tree.h:enum tree_node_kind. */
161 static const char * const tree_node_kind_names[] = {
162 "decls",
163 "types",
164 "blocks",
165 "stmts",
166 "refs",
167 "exprs",
168 "constants",
169 "identifiers",
170 "vecs",
171 "binfos",
172 "ssa names",
173 "constructors",
174 "random kinds",
175 "lang_decl kinds",
176 "lang_type kinds",
177 "omp clauses",
180 /* Unique id for next decl created. */
181 static GTY(()) int next_decl_uid;
182 /* Unique id for next type created. */
183 static GTY(()) int next_type_uid = 1;
184 /* Unique id for next debug decl created. Use negative numbers,
185 to catch erroneous uses. */
186 static GTY(()) int next_debug_decl_uid;
188 /* Since we cannot rehash a type after it is in the table, we have to
189 keep the hash code. */
191 struct GTY((for_user)) type_hash {
192 unsigned long hash;
193 tree type;
196 /* Initial size of the hash table (rounded to next prime). */
197 #define TYPE_HASH_INITIAL_SIZE 1000
199 struct type_cache_hasher : ggc_cache_hasher<type_hash *>
201 static hashval_t hash (type_hash *t) { return t->hash; }
202 static bool equal (type_hash *a, type_hash *b);
204 static void
205 handle_cache_entry (type_hash *&t)
207 extern void gt_ggc_mx (type_hash *&);
208 if (t == HTAB_DELETED_ENTRY || t == HTAB_EMPTY_ENTRY)
209 return;
210 else if (ggc_marked_p (t->type))
211 gt_ggc_mx (t);
212 else
213 t = static_cast<type_hash *> (HTAB_DELETED_ENTRY);
217 /* Now here is the hash table. When recording a type, it is added to
218 the slot whose index is the hash code. Note that the hash table is
219 used for several kinds of types (function types, array types and
220 array index range types, for now). While all these live in the
221 same table, they are completely independent, and the hash code is
222 computed differently for each of these. */
224 static GTY ((cache)) hash_table<type_cache_hasher> *type_hash_table;
226 /* Hash table and temporary node for larger integer const values. */
227 static GTY (()) tree int_cst_node;
229 struct int_cst_hasher : ggc_cache_hasher<tree>
231 static hashval_t hash (tree t);
232 static bool equal (tree x, tree y);
235 static GTY ((cache)) hash_table<int_cst_hasher> *int_cst_hash_table;
237 /* Hash table for optimization flags and target option flags. Use the same
238 hash table for both sets of options. Nodes for building the current
239 optimization and target option nodes. The assumption is most of the time
240 the options created will already be in the hash table, so we avoid
241 allocating and freeing up a node repeatably. */
242 static GTY (()) tree cl_optimization_node;
243 static GTY (()) tree cl_target_option_node;
245 struct cl_option_hasher : ggc_cache_hasher<tree>
247 static hashval_t hash (tree t);
248 static bool equal (tree x, tree y);
251 static GTY ((cache)) hash_table<cl_option_hasher> *cl_option_hash_table;
253 /* General tree->tree mapping structure for use in hash tables. */
256 static GTY ((cache))
257 hash_table<tree_decl_map_cache_hasher> *debug_expr_for_decl;
259 static GTY ((cache))
260 hash_table<tree_decl_map_cache_hasher> *value_expr_for_decl;
262 struct tree_vec_map_cache_hasher : ggc_cache_hasher<tree_vec_map *>
264 static hashval_t hash (tree_vec_map *m) { return DECL_UID (m->base.from); }
266 static bool
267 equal (tree_vec_map *a, tree_vec_map *b)
269 return a->base.from == b->base.from;
272 static void
273 handle_cache_entry (tree_vec_map *&m)
275 extern void gt_ggc_mx (tree_vec_map *&);
276 if (m == HTAB_EMPTY_ENTRY || m == HTAB_DELETED_ENTRY)
277 return;
278 else if (ggc_marked_p (m->base.from))
279 gt_ggc_mx (m);
280 else
281 m = static_cast<tree_vec_map *> (HTAB_DELETED_ENTRY);
285 static GTY ((cache))
286 hash_table<tree_vec_map_cache_hasher> *debug_args_for_decl;
288 static void set_type_quals (tree, int);
289 static void print_type_hash_statistics (void);
290 static void print_debug_expr_statistics (void);
291 static void print_value_expr_statistics (void);
292 static void type_hash_list (const_tree, inchash::hash &);
293 static void attribute_hash_list (const_tree, inchash::hash &);
295 tree global_trees[TI_MAX];
296 tree integer_types[itk_none];
298 bool int_n_enabled_p[NUM_INT_N_ENTS];
299 struct int_n_trees_t int_n_trees [NUM_INT_N_ENTS];
301 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
303 /* Number of operands for each OpenMP clause. */
304 unsigned const char omp_clause_num_ops[] =
306 0, /* OMP_CLAUSE_ERROR */
307 1, /* OMP_CLAUSE_PRIVATE */
308 1, /* OMP_CLAUSE_SHARED */
309 1, /* OMP_CLAUSE_FIRSTPRIVATE */
310 2, /* OMP_CLAUSE_LASTPRIVATE */
311 4, /* OMP_CLAUSE_REDUCTION */
312 1, /* OMP_CLAUSE_COPYIN */
313 1, /* OMP_CLAUSE_COPYPRIVATE */
314 3, /* OMP_CLAUSE_LINEAR */
315 2, /* OMP_CLAUSE_ALIGNED */
316 1, /* OMP_CLAUSE_DEPEND */
317 1, /* OMP_CLAUSE_UNIFORM */
318 2, /* OMP_CLAUSE_FROM */
319 2, /* OMP_CLAUSE_TO */
320 2, /* OMP_CLAUSE_MAP */
321 2, /* OMP_CLAUSE__CACHE_ */
322 1, /* OMP_CLAUSE_DEVICE_RESIDENT */
323 1, /* OMP_CLAUSE_USE_DEVICE */
324 2, /* OMP_CLAUSE_GANG */
325 1, /* OMP_CLAUSE_ASYNC */
326 1, /* OMP_CLAUSE_WAIT */
327 0, /* OMP_CLAUSE_AUTO */
328 0, /* OMP_CLAUSE_SEQ */
329 1, /* OMP_CLAUSE__LOOPTEMP_ */
330 1, /* OMP_CLAUSE_IF */
331 1, /* OMP_CLAUSE_NUM_THREADS */
332 1, /* OMP_CLAUSE_SCHEDULE */
333 0, /* OMP_CLAUSE_NOWAIT */
334 0, /* OMP_CLAUSE_ORDERED */
335 0, /* OMP_CLAUSE_DEFAULT */
336 3, /* OMP_CLAUSE_COLLAPSE */
337 0, /* OMP_CLAUSE_UNTIED */
338 1, /* OMP_CLAUSE_FINAL */
339 0, /* OMP_CLAUSE_MERGEABLE */
340 1, /* OMP_CLAUSE_DEVICE */
341 1, /* OMP_CLAUSE_DIST_SCHEDULE */
342 0, /* OMP_CLAUSE_INBRANCH */
343 0, /* OMP_CLAUSE_NOTINBRANCH */
344 1, /* OMP_CLAUSE_NUM_TEAMS */
345 1, /* OMP_CLAUSE_THREAD_LIMIT */
346 0, /* OMP_CLAUSE_PROC_BIND */
347 1, /* OMP_CLAUSE_SAFELEN */
348 1, /* OMP_CLAUSE_SIMDLEN */
349 0, /* OMP_CLAUSE_FOR */
350 0, /* OMP_CLAUSE_PARALLEL */
351 0, /* OMP_CLAUSE_SECTIONS */
352 0, /* OMP_CLAUSE_TASKGROUP */
353 1, /* OMP_CLAUSE__SIMDUID_ */
354 1, /* OMP_CLAUSE__CILK_FOR_COUNT_ */
355 0, /* OMP_CLAUSE_INDEPENDENT */
356 1, /* OMP_CLAUSE_WORKER */
357 1, /* OMP_CLAUSE_VECTOR */
358 1, /* OMP_CLAUSE_NUM_GANGS */
359 1, /* OMP_CLAUSE_NUM_WORKERS */
360 1, /* OMP_CLAUSE_VECTOR_LENGTH */
363 const char * const omp_clause_code_name[] =
365 "error_clause",
366 "private",
367 "shared",
368 "firstprivate",
369 "lastprivate",
370 "reduction",
371 "copyin",
372 "copyprivate",
373 "linear",
374 "aligned",
375 "depend",
376 "uniform",
377 "from",
378 "to",
379 "map",
380 "_cache_",
381 "device_resident",
382 "use_device",
383 "gang",
384 "async",
385 "wait",
386 "auto",
387 "seq",
388 "_looptemp_",
389 "if",
390 "num_threads",
391 "schedule",
392 "nowait",
393 "ordered",
394 "default",
395 "collapse",
396 "untied",
397 "final",
398 "mergeable",
399 "device",
400 "dist_schedule",
401 "inbranch",
402 "notinbranch",
403 "num_teams",
404 "thread_limit",
405 "proc_bind",
406 "safelen",
407 "simdlen",
408 "for",
409 "parallel",
410 "sections",
411 "taskgroup",
412 "_simduid_",
413 "_Cilk_for_count_",
414 "independent",
415 "worker",
416 "vector",
417 "num_gangs",
418 "num_workers",
419 "vector_length"
423 /* Return the tree node structure used by tree code CODE. */
425 static inline enum tree_node_structure_enum
426 tree_node_structure_for_code (enum tree_code code)
428 switch (TREE_CODE_CLASS (code))
430 case tcc_declaration:
432 switch (code)
434 case FIELD_DECL:
435 return TS_FIELD_DECL;
436 case PARM_DECL:
437 return TS_PARM_DECL;
438 case VAR_DECL:
439 return TS_VAR_DECL;
440 case LABEL_DECL:
441 return TS_LABEL_DECL;
442 case RESULT_DECL:
443 return TS_RESULT_DECL;
444 case DEBUG_EXPR_DECL:
445 return TS_DECL_WRTL;
446 case CONST_DECL:
447 return TS_CONST_DECL;
448 case TYPE_DECL:
449 return TS_TYPE_DECL;
450 case FUNCTION_DECL:
451 return TS_FUNCTION_DECL;
452 case TRANSLATION_UNIT_DECL:
453 return TS_TRANSLATION_UNIT_DECL;
454 default:
455 return TS_DECL_NON_COMMON;
458 case tcc_type:
459 return TS_TYPE_NON_COMMON;
460 case tcc_reference:
461 case tcc_comparison:
462 case tcc_unary:
463 case tcc_binary:
464 case tcc_expression:
465 case tcc_statement:
466 case tcc_vl_exp:
467 return TS_EXP;
468 default: /* tcc_constant and tcc_exceptional */
469 break;
471 switch (code)
473 /* tcc_constant cases. */
474 case VOID_CST: return TS_TYPED;
475 case INTEGER_CST: return TS_INT_CST;
476 case REAL_CST: return TS_REAL_CST;
477 case FIXED_CST: return TS_FIXED_CST;
478 case COMPLEX_CST: return TS_COMPLEX;
479 case VECTOR_CST: return TS_VECTOR;
480 case STRING_CST: return TS_STRING;
481 /* tcc_exceptional cases. */
482 case ERROR_MARK: return TS_COMMON;
483 case IDENTIFIER_NODE: return TS_IDENTIFIER;
484 case TREE_LIST: return TS_LIST;
485 case TREE_VEC: return TS_VEC;
486 case SSA_NAME: return TS_SSA_NAME;
487 case PLACEHOLDER_EXPR: return TS_COMMON;
488 case STATEMENT_LIST: return TS_STATEMENT_LIST;
489 case BLOCK: return TS_BLOCK;
490 case CONSTRUCTOR: return TS_CONSTRUCTOR;
491 case TREE_BINFO: return TS_BINFO;
492 case OMP_CLAUSE: return TS_OMP_CLAUSE;
493 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
494 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
496 default:
497 gcc_unreachable ();
502 /* Initialize tree_contains_struct to describe the hierarchy of tree
503 nodes. */
505 static void
506 initialize_tree_contains_struct (void)
508 unsigned i;
510 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
512 enum tree_code code;
513 enum tree_node_structure_enum ts_code;
515 code = (enum tree_code) i;
516 ts_code = tree_node_structure_for_code (code);
518 /* Mark the TS structure itself. */
519 tree_contains_struct[code][ts_code] = 1;
521 /* Mark all the structures that TS is derived from. */
522 switch (ts_code)
524 case TS_TYPED:
525 case TS_BLOCK:
526 MARK_TS_BASE (code);
527 break;
529 case TS_COMMON:
530 case TS_INT_CST:
531 case TS_REAL_CST:
532 case TS_FIXED_CST:
533 case TS_VECTOR:
534 case TS_STRING:
535 case TS_COMPLEX:
536 case TS_SSA_NAME:
537 case TS_CONSTRUCTOR:
538 case TS_EXP:
539 case TS_STATEMENT_LIST:
540 MARK_TS_TYPED (code);
541 break;
543 case TS_IDENTIFIER:
544 case TS_DECL_MINIMAL:
545 case TS_TYPE_COMMON:
546 case TS_LIST:
547 case TS_VEC:
548 case TS_BINFO:
549 case TS_OMP_CLAUSE:
550 case TS_OPTIMIZATION:
551 case TS_TARGET_OPTION:
552 MARK_TS_COMMON (code);
553 break;
555 case TS_TYPE_WITH_LANG_SPECIFIC:
556 MARK_TS_TYPE_COMMON (code);
557 break;
559 case TS_TYPE_NON_COMMON:
560 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
561 break;
563 case TS_DECL_COMMON:
564 MARK_TS_DECL_MINIMAL (code);
565 break;
567 case TS_DECL_WRTL:
568 case TS_CONST_DECL:
569 MARK_TS_DECL_COMMON (code);
570 break;
572 case TS_DECL_NON_COMMON:
573 MARK_TS_DECL_WITH_VIS (code);
574 break;
576 case TS_DECL_WITH_VIS:
577 case TS_PARM_DECL:
578 case TS_LABEL_DECL:
579 case TS_RESULT_DECL:
580 MARK_TS_DECL_WRTL (code);
581 break;
583 case TS_FIELD_DECL:
584 MARK_TS_DECL_COMMON (code);
585 break;
587 case TS_VAR_DECL:
588 MARK_TS_DECL_WITH_VIS (code);
589 break;
591 case TS_TYPE_DECL:
592 case TS_FUNCTION_DECL:
593 MARK_TS_DECL_NON_COMMON (code);
594 break;
596 case TS_TRANSLATION_UNIT_DECL:
597 MARK_TS_DECL_COMMON (code);
598 break;
600 default:
601 gcc_unreachable ();
605 /* Basic consistency checks for attributes used in fold. */
606 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
607 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
608 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
609 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
610 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
611 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
612 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
613 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
614 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
615 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
616 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
617 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
618 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
619 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
620 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
621 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
622 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
623 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
624 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
625 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
626 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
627 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
628 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
629 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
630 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
631 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
632 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
633 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
634 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
635 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
636 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
637 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
638 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
639 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
640 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
641 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
642 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
643 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
644 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_MINIMAL]);
645 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_COMMON]);
649 /* Init tree.c. */
651 void
652 init_ttree (void)
654 /* Initialize the hash table of types. */
655 type_hash_table
656 = hash_table<type_cache_hasher>::create_ggc (TYPE_HASH_INITIAL_SIZE);
658 debug_expr_for_decl
659 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
661 value_expr_for_decl
662 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
664 int_cst_hash_table = hash_table<int_cst_hasher>::create_ggc (1024);
666 int_cst_node = make_int_cst (1, 1);
668 cl_option_hash_table = hash_table<cl_option_hasher>::create_ggc (64);
670 cl_optimization_node = make_node (OPTIMIZATION_NODE);
671 cl_target_option_node = make_node (TARGET_OPTION_NODE);
673 /* Initialize the tree_contains_struct array. */
674 initialize_tree_contains_struct ();
675 lang_hooks.init_ts ();
679 /* The name of the object as the assembler will see it (but before any
680 translations made by ASM_OUTPUT_LABELREF). Often this is the same
681 as DECL_NAME. It is an IDENTIFIER_NODE. */
682 tree
683 decl_assembler_name (tree decl)
685 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
686 lang_hooks.set_decl_assembler_name (decl);
687 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
690 /* When the target supports COMDAT groups, this indicates which group the
691 DECL is associated with. This can be either an IDENTIFIER_NODE or a
692 decl, in which case its DECL_ASSEMBLER_NAME identifies the group. */
693 tree
694 decl_comdat_group (const_tree node)
696 struct symtab_node *snode = symtab_node::get (node);
697 if (!snode)
698 return NULL;
699 return snode->get_comdat_group ();
702 /* Likewise, but make sure it's been reduced to an IDENTIFIER_NODE. */
703 tree
704 decl_comdat_group_id (const_tree node)
706 struct symtab_node *snode = symtab_node::get (node);
707 if (!snode)
708 return NULL;
709 return snode->get_comdat_group_id ();
712 /* When the target supports named section, return its name as IDENTIFIER_NODE
713 or NULL if it is in no section. */
714 const char *
715 decl_section_name (const_tree node)
717 struct symtab_node *snode = symtab_node::get (node);
718 if (!snode)
719 return NULL;
720 return snode->get_section ();
723 /* Set section section name of NODE to VALUE (that is expected to
724 be identifier node) */
725 void
726 set_decl_section_name (tree node, const char *value)
728 struct symtab_node *snode;
730 if (value == NULL)
732 snode = symtab_node::get (node);
733 if (!snode)
734 return;
736 else if (TREE_CODE (node) == VAR_DECL)
737 snode = varpool_node::get_create (node);
738 else
739 snode = cgraph_node::get_create (node);
740 snode->set_section (value);
743 /* Return TLS model of a variable NODE. */
744 enum tls_model
745 decl_tls_model (const_tree node)
747 struct varpool_node *snode = varpool_node::get (node);
748 if (!snode)
749 return TLS_MODEL_NONE;
750 return snode->tls_model;
753 /* Set TLS model of variable NODE to MODEL. */
754 void
755 set_decl_tls_model (tree node, enum tls_model model)
757 struct varpool_node *vnode;
759 if (model == TLS_MODEL_NONE)
761 vnode = varpool_node::get (node);
762 if (!vnode)
763 return;
765 else
766 vnode = varpool_node::get_create (node);
767 vnode->tls_model = model;
770 /* Compute the number of bytes occupied by a tree with code CODE.
771 This function cannot be used for nodes that have variable sizes,
772 including TREE_VEC, INTEGER_CST, STRING_CST, and CALL_EXPR. */
773 size_t
774 tree_code_size (enum tree_code code)
776 switch (TREE_CODE_CLASS (code))
778 case tcc_declaration: /* A decl node */
780 switch (code)
782 case FIELD_DECL:
783 return sizeof (struct tree_field_decl);
784 case PARM_DECL:
785 return sizeof (struct tree_parm_decl);
786 case VAR_DECL:
787 return sizeof (struct tree_var_decl);
788 case LABEL_DECL:
789 return sizeof (struct tree_label_decl);
790 case RESULT_DECL:
791 return sizeof (struct tree_result_decl);
792 case CONST_DECL:
793 return sizeof (struct tree_const_decl);
794 case TYPE_DECL:
795 return sizeof (struct tree_type_decl);
796 case FUNCTION_DECL:
797 return sizeof (struct tree_function_decl);
798 case DEBUG_EXPR_DECL:
799 return sizeof (struct tree_decl_with_rtl);
800 case TRANSLATION_UNIT_DECL:
801 return sizeof (struct tree_translation_unit_decl);
802 case NAMESPACE_DECL:
803 case IMPORTED_DECL:
804 case NAMELIST_DECL:
805 return sizeof (struct tree_decl_non_common);
806 default:
807 return lang_hooks.tree_size (code);
811 case tcc_type: /* a type node */
812 return sizeof (struct tree_type_non_common);
814 case tcc_reference: /* a reference */
815 case tcc_expression: /* an expression */
816 case tcc_statement: /* an expression with side effects */
817 case tcc_comparison: /* a comparison expression */
818 case tcc_unary: /* a unary arithmetic expression */
819 case tcc_binary: /* a binary arithmetic expression */
820 return (sizeof (struct tree_exp)
821 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
823 case tcc_constant: /* a constant */
824 switch (code)
826 case VOID_CST: return sizeof (struct tree_typed);
827 case INTEGER_CST: gcc_unreachable ();
828 case REAL_CST: return sizeof (struct tree_real_cst);
829 case FIXED_CST: return sizeof (struct tree_fixed_cst);
830 case COMPLEX_CST: return sizeof (struct tree_complex);
831 case VECTOR_CST: return sizeof (struct tree_vector);
832 case STRING_CST: gcc_unreachable ();
833 default:
834 return lang_hooks.tree_size (code);
837 case tcc_exceptional: /* something random, like an identifier. */
838 switch (code)
840 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
841 case TREE_LIST: return sizeof (struct tree_list);
843 case ERROR_MARK:
844 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
846 case TREE_VEC:
847 case OMP_CLAUSE: gcc_unreachable ();
849 case SSA_NAME: return sizeof (struct tree_ssa_name);
851 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
852 case BLOCK: return sizeof (struct tree_block);
853 case CONSTRUCTOR: return sizeof (struct tree_constructor);
854 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
855 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
857 default:
858 return lang_hooks.tree_size (code);
861 default:
862 gcc_unreachable ();
866 /* Compute the number of bytes occupied by NODE. This routine only
867 looks at TREE_CODE, except for those nodes that have variable sizes. */
868 size_t
869 tree_size (const_tree node)
871 const enum tree_code code = TREE_CODE (node);
872 switch (code)
874 case INTEGER_CST:
875 return (sizeof (struct tree_int_cst)
876 + (TREE_INT_CST_EXT_NUNITS (node) - 1) * sizeof (HOST_WIDE_INT));
878 case TREE_BINFO:
879 return (offsetof (struct tree_binfo, base_binfos)
880 + vec<tree, va_gc>
881 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
883 case TREE_VEC:
884 return (sizeof (struct tree_vec)
885 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
887 case VECTOR_CST:
888 return (sizeof (struct tree_vector)
889 + (TYPE_VECTOR_SUBPARTS (TREE_TYPE (node)) - 1) * sizeof (tree));
891 case STRING_CST:
892 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
894 case OMP_CLAUSE:
895 return (sizeof (struct tree_omp_clause)
896 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
897 * sizeof (tree));
899 default:
900 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
901 return (sizeof (struct tree_exp)
902 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
903 else
904 return tree_code_size (code);
908 /* Record interesting allocation statistics for a tree node with CODE
909 and LENGTH. */
911 static void
912 record_node_allocation_statistics (enum tree_code code ATTRIBUTE_UNUSED,
913 size_t length ATTRIBUTE_UNUSED)
915 enum tree_code_class type = TREE_CODE_CLASS (code);
916 tree_node_kind kind;
918 if (!GATHER_STATISTICS)
919 return;
921 switch (type)
923 case tcc_declaration: /* A decl node */
924 kind = d_kind;
925 break;
927 case tcc_type: /* a type node */
928 kind = t_kind;
929 break;
931 case tcc_statement: /* an expression with side effects */
932 kind = s_kind;
933 break;
935 case tcc_reference: /* a reference */
936 kind = r_kind;
937 break;
939 case tcc_expression: /* an expression */
940 case tcc_comparison: /* a comparison expression */
941 case tcc_unary: /* a unary arithmetic expression */
942 case tcc_binary: /* a binary arithmetic expression */
943 kind = e_kind;
944 break;
946 case tcc_constant: /* a constant */
947 kind = c_kind;
948 break;
950 case tcc_exceptional: /* something random, like an identifier. */
951 switch (code)
953 case IDENTIFIER_NODE:
954 kind = id_kind;
955 break;
957 case TREE_VEC:
958 kind = vec_kind;
959 break;
961 case TREE_BINFO:
962 kind = binfo_kind;
963 break;
965 case SSA_NAME:
966 kind = ssa_name_kind;
967 break;
969 case BLOCK:
970 kind = b_kind;
971 break;
973 case CONSTRUCTOR:
974 kind = constr_kind;
975 break;
977 case OMP_CLAUSE:
978 kind = omp_clause_kind;
979 break;
981 default:
982 kind = x_kind;
983 break;
985 break;
987 case tcc_vl_exp:
988 kind = e_kind;
989 break;
991 default:
992 gcc_unreachable ();
995 tree_code_counts[(int) code]++;
996 tree_node_counts[(int) kind]++;
997 tree_node_sizes[(int) kind] += length;
1000 /* Allocate and return a new UID from the DECL_UID namespace. */
1003 allocate_decl_uid (void)
1005 return next_decl_uid++;
1008 /* Return a newly allocated node of code CODE. For decl and type
1009 nodes, some other fields are initialized. The rest of the node is
1010 initialized to zero. This function cannot be used for TREE_VEC,
1011 INTEGER_CST or OMP_CLAUSE nodes, which is enforced by asserts in
1012 tree_code_size.
1014 Achoo! I got a code in the node. */
1016 tree
1017 make_node_stat (enum tree_code code MEM_STAT_DECL)
1019 tree t;
1020 enum tree_code_class type = TREE_CODE_CLASS (code);
1021 size_t length = tree_code_size (code);
1023 record_node_allocation_statistics (code, length);
1025 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1026 TREE_SET_CODE (t, code);
1028 switch (type)
1030 case tcc_statement:
1031 TREE_SIDE_EFFECTS (t) = 1;
1032 break;
1034 case tcc_declaration:
1035 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1037 if (code == FUNCTION_DECL)
1039 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
1040 DECL_MODE (t) = FUNCTION_MODE;
1042 else
1043 DECL_ALIGN (t) = 1;
1045 DECL_SOURCE_LOCATION (t) = input_location;
1046 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
1047 DECL_UID (t) = --next_debug_decl_uid;
1048 else
1050 DECL_UID (t) = allocate_decl_uid ();
1051 SET_DECL_PT_UID (t, -1);
1053 if (TREE_CODE (t) == LABEL_DECL)
1054 LABEL_DECL_UID (t) = -1;
1056 break;
1058 case tcc_type:
1059 TYPE_UID (t) = next_type_uid++;
1060 TYPE_ALIGN (t) = BITS_PER_UNIT;
1061 TYPE_USER_ALIGN (t) = 0;
1062 TYPE_MAIN_VARIANT (t) = t;
1063 TYPE_CANONICAL (t) = t;
1065 /* Default to no attributes for type, but let target change that. */
1066 TYPE_ATTRIBUTES (t) = NULL_TREE;
1067 targetm.set_default_type_attributes (t);
1069 /* We have not yet computed the alias set for this type. */
1070 TYPE_ALIAS_SET (t) = -1;
1071 break;
1073 case tcc_constant:
1074 TREE_CONSTANT (t) = 1;
1075 break;
1077 case tcc_expression:
1078 switch (code)
1080 case INIT_EXPR:
1081 case MODIFY_EXPR:
1082 case VA_ARG_EXPR:
1083 case PREDECREMENT_EXPR:
1084 case PREINCREMENT_EXPR:
1085 case POSTDECREMENT_EXPR:
1086 case POSTINCREMENT_EXPR:
1087 /* All of these have side-effects, no matter what their
1088 operands are. */
1089 TREE_SIDE_EFFECTS (t) = 1;
1090 break;
1092 default:
1093 break;
1095 break;
1097 case tcc_exceptional:
1098 switch (code)
1100 case TARGET_OPTION_NODE:
1101 TREE_TARGET_OPTION(t)
1102 = ggc_cleared_alloc<struct cl_target_option> ();
1103 break;
1105 case OPTIMIZATION_NODE:
1106 TREE_OPTIMIZATION (t)
1107 = ggc_cleared_alloc<struct cl_optimization> ();
1108 break;
1110 default:
1111 break;
1113 break;
1115 default:
1116 /* Other classes need no special treatment. */
1117 break;
1120 return t;
1123 /* Return a new node with the same contents as NODE except that its
1124 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
1126 tree
1127 copy_node_stat (tree node MEM_STAT_DECL)
1129 tree t;
1130 enum tree_code code = TREE_CODE (node);
1131 size_t length;
1133 gcc_assert (code != STATEMENT_LIST);
1135 length = tree_size (node);
1136 record_node_allocation_statistics (code, length);
1137 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1138 memcpy (t, node, length);
1140 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
1141 TREE_CHAIN (t) = 0;
1142 TREE_ASM_WRITTEN (t) = 0;
1143 TREE_VISITED (t) = 0;
1145 if (TREE_CODE_CLASS (code) == tcc_declaration)
1147 if (code == DEBUG_EXPR_DECL)
1148 DECL_UID (t) = --next_debug_decl_uid;
1149 else
1151 DECL_UID (t) = allocate_decl_uid ();
1152 if (DECL_PT_UID_SET_P (node))
1153 SET_DECL_PT_UID (t, DECL_PT_UID (node));
1155 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
1156 && DECL_HAS_VALUE_EXPR_P (node))
1158 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
1159 DECL_HAS_VALUE_EXPR_P (t) = 1;
1161 /* DECL_DEBUG_EXPR is copied explicitely by callers. */
1162 if (TREE_CODE (node) == VAR_DECL)
1164 DECL_HAS_DEBUG_EXPR_P (t) = 0;
1165 t->decl_with_vis.symtab_node = NULL;
1167 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
1169 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
1170 DECL_HAS_INIT_PRIORITY_P (t) = 1;
1172 if (TREE_CODE (node) == FUNCTION_DECL)
1174 DECL_STRUCT_FUNCTION (t) = NULL;
1175 t->decl_with_vis.symtab_node = NULL;
1178 else if (TREE_CODE_CLASS (code) == tcc_type)
1180 TYPE_UID (t) = next_type_uid++;
1181 /* The following is so that the debug code for
1182 the copy is different from the original type.
1183 The two statements usually duplicate each other
1184 (because they clear fields of the same union),
1185 but the optimizer should catch that. */
1186 TYPE_SYMTAB_POINTER (t) = 0;
1187 TYPE_SYMTAB_ADDRESS (t) = 0;
1189 /* Do not copy the values cache. */
1190 if (TYPE_CACHED_VALUES_P (t))
1192 TYPE_CACHED_VALUES_P (t) = 0;
1193 TYPE_CACHED_VALUES (t) = NULL_TREE;
1196 else if (code == TARGET_OPTION_NODE)
1198 TREE_TARGET_OPTION (t) = ggc_alloc<struct cl_target_option>();
1199 memcpy (TREE_TARGET_OPTION (t), TREE_TARGET_OPTION (node),
1200 sizeof (struct cl_target_option));
1202 else if (code == OPTIMIZATION_NODE)
1204 TREE_OPTIMIZATION (t) = ggc_alloc<struct cl_optimization>();
1205 memcpy (TREE_OPTIMIZATION (t), TREE_OPTIMIZATION (node),
1206 sizeof (struct cl_optimization));
1209 return t;
1212 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1213 For example, this can copy a list made of TREE_LIST nodes. */
1215 tree
1216 copy_list (tree list)
1218 tree head;
1219 tree prev, next;
1221 if (list == 0)
1222 return 0;
1224 head = prev = copy_node (list);
1225 next = TREE_CHAIN (list);
1226 while (next)
1228 TREE_CHAIN (prev) = copy_node (next);
1229 prev = TREE_CHAIN (prev);
1230 next = TREE_CHAIN (next);
1232 return head;
1236 /* Return the value that TREE_INT_CST_EXT_NUNITS should have for an
1237 INTEGER_CST with value CST and type TYPE. */
1239 static unsigned int
1240 get_int_cst_ext_nunits (tree type, const wide_int &cst)
1242 gcc_checking_assert (cst.get_precision () == TYPE_PRECISION (type));
1243 /* We need an extra zero HWI if CST is an unsigned integer with its
1244 upper bit set, and if CST occupies a whole number of HWIs. */
1245 if (TYPE_UNSIGNED (type)
1246 && wi::neg_p (cst)
1247 && (cst.get_precision () % HOST_BITS_PER_WIDE_INT) == 0)
1248 return cst.get_precision () / HOST_BITS_PER_WIDE_INT + 1;
1249 return cst.get_len ();
1252 /* Return a new INTEGER_CST with value CST and type TYPE. */
1254 static tree
1255 build_new_int_cst (tree type, const wide_int &cst)
1257 unsigned int len = cst.get_len ();
1258 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1259 tree nt = make_int_cst (len, ext_len);
1261 if (len < ext_len)
1263 --ext_len;
1264 TREE_INT_CST_ELT (nt, ext_len) = 0;
1265 for (unsigned int i = len; i < ext_len; ++i)
1266 TREE_INT_CST_ELT (nt, i) = -1;
1268 else if (TYPE_UNSIGNED (type)
1269 && cst.get_precision () < len * HOST_BITS_PER_WIDE_INT)
1271 len--;
1272 TREE_INT_CST_ELT (nt, len)
1273 = zext_hwi (cst.elt (len),
1274 cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1277 for (unsigned int i = 0; i < len; i++)
1278 TREE_INT_CST_ELT (nt, i) = cst.elt (i);
1279 TREE_TYPE (nt) = type;
1280 return nt;
1283 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1285 tree
1286 build_int_cst (tree type, HOST_WIDE_INT low)
1288 /* Support legacy code. */
1289 if (!type)
1290 type = integer_type_node;
1292 return wide_int_to_tree (type, wi::shwi (low, TYPE_PRECISION (type)));
1295 tree
1296 build_int_cstu (tree type, unsigned HOST_WIDE_INT cst)
1298 return wide_int_to_tree (type, wi::uhwi (cst, TYPE_PRECISION (type)));
1301 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1303 tree
1304 build_int_cst_type (tree type, HOST_WIDE_INT low)
1306 gcc_assert (type);
1307 return wide_int_to_tree (type, wi::shwi (low, TYPE_PRECISION (type)));
1310 /* Constructs tree in type TYPE from with value given by CST. Signedness
1311 of CST is assumed to be the same as the signedness of TYPE. */
1313 tree
1314 double_int_to_tree (tree type, double_int cst)
1316 return wide_int_to_tree (type, widest_int::from (cst, TYPE_SIGN (type)));
1319 /* We force the wide_int CST to the range of the type TYPE by sign or
1320 zero extending it. OVERFLOWABLE indicates if we are interested in
1321 overflow of the value, when >0 we are only interested in signed
1322 overflow, for <0 we are interested in any overflow. OVERFLOWED
1323 indicates whether overflow has already occurred. CONST_OVERFLOWED
1324 indicates whether constant overflow has already occurred. We force
1325 T's value to be within range of T's type (by setting to 0 or 1 all
1326 the bits outside the type's range). We set TREE_OVERFLOWED if,
1327 OVERFLOWED is nonzero,
1328 or OVERFLOWABLE is >0 and signed overflow occurs
1329 or OVERFLOWABLE is <0 and any overflow occurs
1330 We return a new tree node for the extended wide_int. The node
1331 is shared if no overflow flags are set. */
1334 tree
1335 force_fit_type (tree type, const wide_int_ref &cst,
1336 int overflowable, bool overflowed)
1338 signop sign = TYPE_SIGN (type);
1340 /* If we need to set overflow flags, return a new unshared node. */
1341 if (overflowed || !wi::fits_to_tree_p (cst, type))
1343 if (overflowed
1344 || overflowable < 0
1345 || (overflowable > 0 && sign == SIGNED))
1347 wide_int tmp = wide_int::from (cst, TYPE_PRECISION (type), sign);
1348 tree t = build_new_int_cst (type, tmp);
1349 TREE_OVERFLOW (t) = 1;
1350 return t;
1354 /* Else build a shared node. */
1355 return wide_int_to_tree (type, cst);
1358 /* These are the hash table functions for the hash table of INTEGER_CST
1359 nodes of a sizetype. */
1361 /* Return the hash code code X, an INTEGER_CST. */
1363 hashval_t
1364 int_cst_hasher::hash (tree x)
1366 const_tree const t = x;
1367 hashval_t code = TYPE_UID (TREE_TYPE (t));
1368 int i;
1370 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
1371 code ^= TREE_INT_CST_ELT (t, i);
1373 return code;
1376 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1377 is the same as that given by *Y, which is the same. */
1379 bool
1380 int_cst_hasher::equal (tree x, tree y)
1382 const_tree const xt = x;
1383 const_tree const yt = y;
1385 if (TREE_TYPE (xt) != TREE_TYPE (yt)
1386 || TREE_INT_CST_NUNITS (xt) != TREE_INT_CST_NUNITS (yt)
1387 || TREE_INT_CST_EXT_NUNITS (xt) != TREE_INT_CST_EXT_NUNITS (yt))
1388 return false;
1390 for (int i = 0; i < TREE_INT_CST_NUNITS (xt); i++)
1391 if (TREE_INT_CST_ELT (xt, i) != TREE_INT_CST_ELT (yt, i))
1392 return false;
1394 return true;
1397 /* Create an INT_CST node of TYPE and value CST.
1398 The returned node is always shared. For small integers we use a
1399 per-type vector cache, for larger ones we use a single hash table.
1400 The value is extended from its precision according to the sign of
1401 the type to be a multiple of HOST_BITS_PER_WIDE_INT. This defines
1402 the upper bits and ensures that hashing and value equality based
1403 upon the underlying HOST_WIDE_INTs works without masking. */
1405 tree
1406 wide_int_to_tree (tree type, const wide_int_ref &pcst)
1408 tree t;
1409 int ix = -1;
1410 int limit = 0;
1412 gcc_assert (type);
1413 unsigned int prec = TYPE_PRECISION (type);
1414 signop sgn = TYPE_SIGN (type);
1416 /* Verify that everything is canonical. */
1417 int l = pcst.get_len ();
1418 if (l > 1)
1420 if (pcst.elt (l - 1) == 0)
1421 gcc_checking_assert (pcst.elt (l - 2) < 0);
1422 if (pcst.elt (l - 1) == (HOST_WIDE_INT) -1)
1423 gcc_checking_assert (pcst.elt (l - 2) >= 0);
1426 wide_int cst = wide_int::from (pcst, prec, sgn);
1427 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1429 if (ext_len == 1)
1431 /* We just need to store a single HOST_WIDE_INT. */
1432 HOST_WIDE_INT hwi;
1433 if (TYPE_UNSIGNED (type))
1434 hwi = cst.to_uhwi ();
1435 else
1436 hwi = cst.to_shwi ();
1438 switch (TREE_CODE (type))
1440 case NULLPTR_TYPE:
1441 gcc_assert (hwi == 0);
1442 /* Fallthru. */
1444 case POINTER_TYPE:
1445 case REFERENCE_TYPE:
1446 case POINTER_BOUNDS_TYPE:
1447 /* Cache NULL pointer and zero bounds. */
1448 if (hwi == 0)
1450 limit = 1;
1451 ix = 0;
1453 break;
1455 case BOOLEAN_TYPE:
1456 /* Cache false or true. */
1457 limit = 2;
1458 if (hwi < 2)
1459 ix = hwi;
1460 break;
1462 case INTEGER_TYPE:
1463 case OFFSET_TYPE:
1464 if (TYPE_SIGN (type) == UNSIGNED)
1466 /* Cache [0, N). */
1467 limit = INTEGER_SHARE_LIMIT;
1468 if (IN_RANGE (hwi, 0, INTEGER_SHARE_LIMIT - 1))
1469 ix = hwi;
1471 else
1473 /* Cache [-1, N). */
1474 limit = INTEGER_SHARE_LIMIT + 1;
1475 if (IN_RANGE (hwi, -1, INTEGER_SHARE_LIMIT - 1))
1476 ix = hwi + 1;
1478 break;
1480 case ENUMERAL_TYPE:
1481 break;
1483 default:
1484 gcc_unreachable ();
1487 if (ix >= 0)
1489 /* Look for it in the type's vector of small shared ints. */
1490 if (!TYPE_CACHED_VALUES_P (type))
1492 TYPE_CACHED_VALUES_P (type) = 1;
1493 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1496 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1497 if (t)
1498 /* Make sure no one is clobbering the shared constant. */
1499 gcc_checking_assert (TREE_TYPE (t) == type
1500 && TREE_INT_CST_NUNITS (t) == 1
1501 && TREE_INT_CST_OFFSET_NUNITS (t) == 1
1502 && TREE_INT_CST_EXT_NUNITS (t) == 1
1503 && TREE_INT_CST_ELT (t, 0) == hwi);
1504 else
1506 /* Create a new shared int. */
1507 t = build_new_int_cst (type, cst);
1508 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1511 else
1513 /* Use the cache of larger shared ints, using int_cst_node as
1514 a temporary. */
1516 TREE_INT_CST_ELT (int_cst_node, 0) = hwi;
1517 TREE_TYPE (int_cst_node) = type;
1519 tree *slot = int_cst_hash_table->find_slot (int_cst_node, INSERT);
1520 t = *slot;
1521 if (!t)
1523 /* Insert this one into the hash table. */
1524 t = int_cst_node;
1525 *slot = t;
1526 /* Make a new node for next time round. */
1527 int_cst_node = make_int_cst (1, 1);
1531 else
1533 /* The value either hashes properly or we drop it on the floor
1534 for the gc to take care of. There will not be enough of them
1535 to worry about. */
1537 tree nt = build_new_int_cst (type, cst);
1538 tree *slot = int_cst_hash_table->find_slot (nt, INSERT);
1539 t = *slot;
1540 if (!t)
1542 /* Insert this one into the hash table. */
1543 t = nt;
1544 *slot = t;
1548 return t;
1551 void
1552 cache_integer_cst (tree t)
1554 tree type = TREE_TYPE (t);
1555 int ix = -1;
1556 int limit = 0;
1557 int prec = TYPE_PRECISION (type);
1559 gcc_assert (!TREE_OVERFLOW (t));
1561 switch (TREE_CODE (type))
1563 case NULLPTR_TYPE:
1564 gcc_assert (integer_zerop (t));
1565 /* Fallthru. */
1567 case POINTER_TYPE:
1568 case REFERENCE_TYPE:
1569 /* Cache NULL pointer. */
1570 if (integer_zerop (t))
1572 limit = 1;
1573 ix = 0;
1575 break;
1577 case BOOLEAN_TYPE:
1578 /* Cache false or true. */
1579 limit = 2;
1580 if (wi::ltu_p (t, 2))
1581 ix = TREE_INT_CST_ELT (t, 0);
1582 break;
1584 case INTEGER_TYPE:
1585 case OFFSET_TYPE:
1586 if (TYPE_UNSIGNED (type))
1588 /* Cache 0..N */
1589 limit = INTEGER_SHARE_LIMIT;
1591 /* This is a little hokie, but if the prec is smaller than
1592 what is necessary to hold INTEGER_SHARE_LIMIT, then the
1593 obvious test will not get the correct answer. */
1594 if (prec < HOST_BITS_PER_WIDE_INT)
1596 if (tree_to_uhwi (t) < (unsigned HOST_WIDE_INT) INTEGER_SHARE_LIMIT)
1597 ix = tree_to_uhwi (t);
1599 else if (wi::ltu_p (t, INTEGER_SHARE_LIMIT))
1600 ix = tree_to_uhwi (t);
1602 else
1604 /* Cache -1..N */
1605 limit = INTEGER_SHARE_LIMIT + 1;
1607 if (integer_minus_onep (t))
1608 ix = 0;
1609 else if (!wi::neg_p (t))
1611 if (prec < HOST_BITS_PER_WIDE_INT)
1613 if (tree_to_shwi (t) < INTEGER_SHARE_LIMIT)
1614 ix = tree_to_shwi (t) + 1;
1616 else if (wi::ltu_p (t, INTEGER_SHARE_LIMIT))
1617 ix = tree_to_shwi (t) + 1;
1620 break;
1622 case ENUMERAL_TYPE:
1623 break;
1625 default:
1626 gcc_unreachable ();
1629 if (ix >= 0)
1631 /* Look for it in the type's vector of small shared ints. */
1632 if (!TYPE_CACHED_VALUES_P (type))
1634 TYPE_CACHED_VALUES_P (type) = 1;
1635 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1638 gcc_assert (TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) == NULL_TREE);
1639 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1641 else
1643 /* Use the cache of larger shared ints. */
1644 tree *slot = int_cst_hash_table->find_slot (t, INSERT);
1645 /* If there is already an entry for the number verify it's the
1646 same. */
1647 if (*slot)
1648 gcc_assert (wi::eq_p (tree (*slot), t));
1649 else
1650 /* Otherwise insert this one into the hash table. */
1651 *slot = t;
1656 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1657 and the rest are zeros. */
1659 tree
1660 build_low_bits_mask (tree type, unsigned bits)
1662 gcc_assert (bits <= TYPE_PRECISION (type));
1664 return wide_int_to_tree (type, wi::mask (bits, false,
1665 TYPE_PRECISION (type)));
1668 /* Checks that X is integer constant that can be expressed in (unsigned)
1669 HOST_WIDE_INT without loss of precision. */
1671 bool
1672 cst_and_fits_in_hwi (const_tree x)
1674 if (TREE_CODE (x) != INTEGER_CST)
1675 return false;
1677 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1678 return false;
1680 return TREE_INT_CST_NUNITS (x) == 1;
1683 /* Build a newly constructed VECTOR_CST node of length LEN. */
1685 tree
1686 make_vector_stat (unsigned len MEM_STAT_DECL)
1688 tree t;
1689 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1691 record_node_allocation_statistics (VECTOR_CST, length);
1693 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1695 TREE_SET_CODE (t, VECTOR_CST);
1696 TREE_CONSTANT (t) = 1;
1698 return t;
1701 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1702 are in a list pointed to by VALS. */
1704 tree
1705 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1707 int over = 0;
1708 unsigned cnt = 0;
1709 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1710 TREE_TYPE (v) = type;
1712 /* Iterate through elements and check for overflow. */
1713 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1715 tree value = vals[cnt];
1717 VECTOR_CST_ELT (v, cnt) = value;
1719 /* Don't crash if we get an address constant. */
1720 if (!CONSTANT_CLASS_P (value))
1721 continue;
1723 over |= TREE_OVERFLOW (value);
1726 TREE_OVERFLOW (v) = over;
1727 return v;
1730 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1731 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1733 tree
1734 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1736 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1737 unsigned HOST_WIDE_INT idx;
1738 tree value;
1740 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1741 vec[idx] = value;
1742 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1743 vec[idx] = build_zero_cst (TREE_TYPE (type));
1745 return build_vector (type, vec);
1748 /* Build a vector of type VECTYPE where all the elements are SCs. */
1749 tree
1750 build_vector_from_val (tree vectype, tree sc)
1752 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1754 if (sc == error_mark_node)
1755 return sc;
1757 /* Verify that the vector type is suitable for SC. Note that there
1758 is some inconsistency in the type-system with respect to restrict
1759 qualifications of pointers. Vector types always have a main-variant
1760 element type and the qualification is applied to the vector-type.
1761 So TREE_TYPE (vector-type) does not return a properly qualified
1762 vector element-type. */
1763 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1764 TREE_TYPE (vectype)));
1766 if (CONSTANT_CLASS_P (sc))
1768 tree *v = XALLOCAVEC (tree, nunits);
1769 for (i = 0; i < nunits; ++i)
1770 v[i] = sc;
1771 return build_vector (vectype, v);
1773 else
1775 vec<constructor_elt, va_gc> *v;
1776 vec_alloc (v, nunits);
1777 for (i = 0; i < nunits; ++i)
1778 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1779 return build_constructor (vectype, v);
1783 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1784 are in the vec pointed to by VALS. */
1785 tree
1786 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1788 tree c = make_node (CONSTRUCTOR);
1789 unsigned int i;
1790 constructor_elt *elt;
1791 bool constant_p = true;
1792 bool side_effects_p = false;
1794 TREE_TYPE (c) = type;
1795 CONSTRUCTOR_ELTS (c) = vals;
1797 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1799 /* Mostly ctors will have elts that don't have side-effects, so
1800 the usual case is to scan all the elements. Hence a single
1801 loop for both const and side effects, rather than one loop
1802 each (with early outs). */
1803 if (!TREE_CONSTANT (elt->value))
1804 constant_p = false;
1805 if (TREE_SIDE_EFFECTS (elt->value))
1806 side_effects_p = true;
1809 TREE_SIDE_EFFECTS (c) = side_effects_p;
1810 TREE_CONSTANT (c) = constant_p;
1812 return c;
1815 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1816 INDEX and VALUE. */
1817 tree
1818 build_constructor_single (tree type, tree index, tree value)
1820 vec<constructor_elt, va_gc> *v;
1821 constructor_elt elt = {index, value};
1823 vec_alloc (v, 1);
1824 v->quick_push (elt);
1826 return build_constructor (type, v);
1830 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1831 are in a list pointed to by VALS. */
1832 tree
1833 build_constructor_from_list (tree type, tree vals)
1835 tree t;
1836 vec<constructor_elt, va_gc> *v = NULL;
1838 if (vals)
1840 vec_alloc (v, list_length (vals));
1841 for (t = vals; t; t = TREE_CHAIN (t))
1842 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1845 return build_constructor (type, v);
1848 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
1849 of elements, provided as index/value pairs. */
1851 tree
1852 build_constructor_va (tree type, int nelts, ...)
1854 vec<constructor_elt, va_gc> *v = NULL;
1855 va_list p;
1857 va_start (p, nelts);
1858 vec_alloc (v, nelts);
1859 while (nelts--)
1861 tree index = va_arg (p, tree);
1862 tree value = va_arg (p, tree);
1863 CONSTRUCTOR_APPEND_ELT (v, index, value);
1865 va_end (p);
1866 return build_constructor (type, v);
1869 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1871 tree
1872 build_fixed (tree type, FIXED_VALUE_TYPE f)
1874 tree v;
1875 FIXED_VALUE_TYPE *fp;
1877 v = make_node (FIXED_CST);
1878 fp = ggc_alloc<fixed_value> ();
1879 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1881 TREE_TYPE (v) = type;
1882 TREE_FIXED_CST_PTR (v) = fp;
1883 return v;
1886 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1888 tree
1889 build_real (tree type, REAL_VALUE_TYPE d)
1891 tree v;
1892 REAL_VALUE_TYPE *dp;
1893 int overflow = 0;
1895 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1896 Consider doing it via real_convert now. */
1898 v = make_node (REAL_CST);
1899 dp = ggc_alloc<real_value> ();
1900 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1902 TREE_TYPE (v) = type;
1903 TREE_REAL_CST_PTR (v) = dp;
1904 TREE_OVERFLOW (v) = overflow;
1905 return v;
1908 /* Return a new REAL_CST node whose type is TYPE
1909 and whose value is the integer value of the INTEGER_CST node I. */
1911 REAL_VALUE_TYPE
1912 real_value_from_int_cst (const_tree type, const_tree i)
1914 REAL_VALUE_TYPE d;
1916 /* Clear all bits of the real value type so that we can later do
1917 bitwise comparisons to see if two values are the same. */
1918 memset (&d, 0, sizeof d);
1920 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode, i,
1921 TYPE_SIGN (TREE_TYPE (i)));
1922 return d;
1925 /* Given a tree representing an integer constant I, return a tree
1926 representing the same value as a floating-point constant of type TYPE. */
1928 tree
1929 build_real_from_int_cst (tree type, const_tree i)
1931 tree v;
1932 int overflow = TREE_OVERFLOW (i);
1934 v = build_real (type, real_value_from_int_cst (type, i));
1936 TREE_OVERFLOW (v) |= overflow;
1937 return v;
1940 /* Return a newly constructed STRING_CST node whose value is
1941 the LEN characters at STR.
1942 Note that for a C string literal, LEN should include the trailing NUL.
1943 The TREE_TYPE is not initialized. */
1945 tree
1946 build_string (int len, const char *str)
1948 tree s;
1949 size_t length;
1951 /* Do not waste bytes provided by padding of struct tree_string. */
1952 length = len + offsetof (struct tree_string, str) + 1;
1954 record_node_allocation_statistics (STRING_CST, length);
1956 s = (tree) ggc_internal_alloc (length);
1958 memset (s, 0, sizeof (struct tree_typed));
1959 TREE_SET_CODE (s, STRING_CST);
1960 TREE_CONSTANT (s) = 1;
1961 TREE_STRING_LENGTH (s) = len;
1962 memcpy (s->string.str, str, len);
1963 s->string.str[len] = '\0';
1965 return s;
1968 /* Return a newly constructed COMPLEX_CST node whose value is
1969 specified by the real and imaginary parts REAL and IMAG.
1970 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1971 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1973 tree
1974 build_complex (tree type, tree real, tree imag)
1976 tree t = make_node (COMPLEX_CST);
1978 TREE_REALPART (t) = real;
1979 TREE_IMAGPART (t) = imag;
1980 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1981 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1982 return t;
1985 /* Return a constant of arithmetic type TYPE which is the
1986 multiplicative identity of the set TYPE. */
1988 tree
1989 build_one_cst (tree type)
1991 switch (TREE_CODE (type))
1993 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1994 case POINTER_TYPE: case REFERENCE_TYPE:
1995 case OFFSET_TYPE:
1996 return build_int_cst (type, 1);
1998 case REAL_TYPE:
1999 return build_real (type, dconst1);
2001 case FIXED_POINT_TYPE:
2002 /* We can only generate 1 for accum types. */
2003 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2004 return build_fixed (type, FCONST1 (TYPE_MODE (type)));
2006 case VECTOR_TYPE:
2008 tree scalar = build_one_cst (TREE_TYPE (type));
2010 return build_vector_from_val (type, scalar);
2013 case COMPLEX_TYPE:
2014 return build_complex (type,
2015 build_one_cst (TREE_TYPE (type)),
2016 build_zero_cst (TREE_TYPE (type)));
2018 default:
2019 gcc_unreachable ();
2023 /* Return an integer of type TYPE containing all 1's in as much precision as
2024 it contains, or a complex or vector whose subparts are such integers. */
2026 tree
2027 build_all_ones_cst (tree type)
2029 if (TREE_CODE (type) == COMPLEX_TYPE)
2031 tree scalar = build_all_ones_cst (TREE_TYPE (type));
2032 return build_complex (type, scalar, scalar);
2034 else
2035 return build_minus_one_cst (type);
2038 /* Return a constant of arithmetic type TYPE which is the
2039 opposite of the multiplicative identity of the set TYPE. */
2041 tree
2042 build_minus_one_cst (tree type)
2044 switch (TREE_CODE (type))
2046 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2047 case POINTER_TYPE: case REFERENCE_TYPE:
2048 case OFFSET_TYPE:
2049 return build_int_cst (type, -1);
2051 case REAL_TYPE:
2052 return build_real (type, dconstm1);
2054 case FIXED_POINT_TYPE:
2055 /* We can only generate 1 for accum types. */
2056 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2057 return build_fixed (type, fixed_from_double_int (double_int_minus_one,
2058 TYPE_MODE (type)));
2060 case VECTOR_TYPE:
2062 tree scalar = build_minus_one_cst (TREE_TYPE (type));
2064 return build_vector_from_val (type, scalar);
2067 case COMPLEX_TYPE:
2068 return build_complex (type,
2069 build_minus_one_cst (TREE_TYPE (type)),
2070 build_zero_cst (TREE_TYPE (type)));
2072 default:
2073 gcc_unreachable ();
2077 /* Build 0 constant of type TYPE. This is used by constructor folding
2078 and thus the constant should be represented in memory by
2079 zero(es). */
2081 tree
2082 build_zero_cst (tree type)
2084 switch (TREE_CODE (type))
2086 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2087 case POINTER_TYPE: case REFERENCE_TYPE:
2088 case OFFSET_TYPE: case NULLPTR_TYPE:
2089 return build_int_cst (type, 0);
2091 case REAL_TYPE:
2092 return build_real (type, dconst0);
2094 case FIXED_POINT_TYPE:
2095 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
2097 case VECTOR_TYPE:
2099 tree scalar = build_zero_cst (TREE_TYPE (type));
2101 return build_vector_from_val (type, scalar);
2104 case COMPLEX_TYPE:
2106 tree zero = build_zero_cst (TREE_TYPE (type));
2108 return build_complex (type, zero, zero);
2111 default:
2112 if (!AGGREGATE_TYPE_P (type))
2113 return fold_convert (type, integer_zero_node);
2114 return build_constructor (type, NULL);
2119 /* Build a BINFO with LEN language slots. */
2121 tree
2122 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
2124 tree t;
2125 size_t length = (offsetof (struct tree_binfo, base_binfos)
2126 + vec<tree, va_gc>::embedded_size (base_binfos));
2128 record_node_allocation_statistics (TREE_BINFO, length);
2130 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
2132 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
2134 TREE_SET_CODE (t, TREE_BINFO);
2136 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
2138 return t;
2141 /* Create a CASE_LABEL_EXPR tree node and return it. */
2143 tree
2144 build_case_label (tree low_value, tree high_value, tree label_decl)
2146 tree t = make_node (CASE_LABEL_EXPR);
2148 TREE_TYPE (t) = void_type_node;
2149 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
2151 CASE_LOW (t) = low_value;
2152 CASE_HIGH (t) = high_value;
2153 CASE_LABEL (t) = label_decl;
2154 CASE_CHAIN (t) = NULL_TREE;
2156 return t;
2159 /* Build a newly constructed INTEGER_CST node. LEN and EXT_LEN are the
2160 values of TREE_INT_CST_NUNITS and TREE_INT_CST_EXT_NUNITS respectively.
2161 The latter determines the length of the HOST_WIDE_INT vector. */
2163 tree
2164 make_int_cst_stat (int len, int ext_len MEM_STAT_DECL)
2166 tree t;
2167 int length = ((ext_len - 1) * sizeof (HOST_WIDE_INT)
2168 + sizeof (struct tree_int_cst));
2170 gcc_assert (len);
2171 record_node_allocation_statistics (INTEGER_CST, length);
2173 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2175 TREE_SET_CODE (t, INTEGER_CST);
2176 TREE_INT_CST_NUNITS (t) = len;
2177 TREE_INT_CST_EXT_NUNITS (t) = ext_len;
2178 /* to_offset can only be applied to trees that are offset_int-sized
2179 or smaller. EXT_LEN is correct if it fits, otherwise the constant
2180 must be exactly the precision of offset_int and so LEN is correct. */
2181 if (ext_len <= OFFSET_INT_ELTS)
2182 TREE_INT_CST_OFFSET_NUNITS (t) = ext_len;
2183 else
2184 TREE_INT_CST_OFFSET_NUNITS (t) = len;
2186 TREE_CONSTANT (t) = 1;
2188 return t;
2191 /* Build a newly constructed TREE_VEC node of length LEN. */
2193 tree
2194 make_tree_vec_stat (int len MEM_STAT_DECL)
2196 tree t;
2197 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2199 record_node_allocation_statistics (TREE_VEC, length);
2201 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2203 TREE_SET_CODE (t, TREE_VEC);
2204 TREE_VEC_LENGTH (t) = len;
2206 return t;
2209 /* Grow a TREE_VEC node to new length LEN. */
2211 tree
2212 grow_tree_vec_stat (tree v, int len MEM_STAT_DECL)
2214 gcc_assert (TREE_CODE (v) == TREE_VEC);
2216 int oldlen = TREE_VEC_LENGTH (v);
2217 gcc_assert (len > oldlen);
2219 int oldlength = (oldlen - 1) * sizeof (tree) + sizeof (struct tree_vec);
2220 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2222 record_node_allocation_statistics (TREE_VEC, length - oldlength);
2224 v = (tree) ggc_realloc (v, length PASS_MEM_STAT);
2226 TREE_VEC_LENGTH (v) = len;
2228 return v;
2231 /* Return 1 if EXPR is the integer constant zero or a complex constant
2232 of zero. */
2235 integer_zerop (const_tree expr)
2237 STRIP_NOPS (expr);
2239 switch (TREE_CODE (expr))
2241 case INTEGER_CST:
2242 return wi::eq_p (expr, 0);
2243 case COMPLEX_CST:
2244 return (integer_zerop (TREE_REALPART (expr))
2245 && integer_zerop (TREE_IMAGPART (expr)));
2246 case VECTOR_CST:
2248 unsigned i;
2249 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2250 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
2251 return false;
2252 return true;
2254 default:
2255 return false;
2259 /* Return 1 if EXPR is the integer constant one or the corresponding
2260 complex constant. */
2263 integer_onep (const_tree expr)
2265 STRIP_NOPS (expr);
2267 switch (TREE_CODE (expr))
2269 case INTEGER_CST:
2270 return wi::eq_p (wi::to_widest (expr), 1);
2271 case COMPLEX_CST:
2272 return (integer_onep (TREE_REALPART (expr))
2273 && integer_zerop (TREE_IMAGPART (expr)));
2274 case VECTOR_CST:
2276 unsigned i;
2277 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2278 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
2279 return false;
2280 return true;
2282 default:
2283 return false;
2287 /* Return 1 if EXPR is the integer constant one. For complex and vector,
2288 return 1 if every piece is the integer constant one. */
2291 integer_each_onep (const_tree expr)
2293 STRIP_NOPS (expr);
2295 if (TREE_CODE (expr) == COMPLEX_CST)
2296 return (integer_onep (TREE_REALPART (expr))
2297 && integer_onep (TREE_IMAGPART (expr)));
2298 else
2299 return integer_onep (expr);
2302 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
2303 it contains, or a complex or vector whose subparts are such integers. */
2306 integer_all_onesp (const_tree expr)
2308 STRIP_NOPS (expr);
2310 if (TREE_CODE (expr) == COMPLEX_CST
2311 && integer_all_onesp (TREE_REALPART (expr))
2312 && integer_all_onesp (TREE_IMAGPART (expr)))
2313 return 1;
2315 else if (TREE_CODE (expr) == VECTOR_CST)
2317 unsigned i;
2318 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2319 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
2320 return 0;
2321 return 1;
2324 else if (TREE_CODE (expr) != INTEGER_CST)
2325 return 0;
2327 return wi::max_value (TYPE_PRECISION (TREE_TYPE (expr)), UNSIGNED) == expr;
2330 /* Return 1 if EXPR is the integer constant minus one. */
2333 integer_minus_onep (const_tree expr)
2335 STRIP_NOPS (expr);
2337 if (TREE_CODE (expr) == COMPLEX_CST)
2338 return (integer_all_onesp (TREE_REALPART (expr))
2339 && integer_zerop (TREE_IMAGPART (expr)));
2340 else
2341 return integer_all_onesp (expr);
2344 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
2345 one bit on). */
2348 integer_pow2p (const_tree expr)
2350 STRIP_NOPS (expr);
2352 if (TREE_CODE (expr) == COMPLEX_CST
2353 && integer_pow2p (TREE_REALPART (expr))
2354 && integer_zerop (TREE_IMAGPART (expr)))
2355 return 1;
2357 if (TREE_CODE (expr) != INTEGER_CST)
2358 return 0;
2360 return wi::popcount (expr) == 1;
2363 /* Return 1 if EXPR is an integer constant other than zero or a
2364 complex constant other than zero. */
2367 integer_nonzerop (const_tree expr)
2369 STRIP_NOPS (expr);
2371 return ((TREE_CODE (expr) == INTEGER_CST
2372 && !wi::eq_p (expr, 0))
2373 || (TREE_CODE (expr) == COMPLEX_CST
2374 && (integer_nonzerop (TREE_REALPART (expr))
2375 || integer_nonzerop (TREE_IMAGPART (expr)))));
2378 /* Return 1 if EXPR is the integer constant one. For vector,
2379 return 1 if every piece is the integer constant minus one
2380 (representing the value TRUE). */
2383 integer_truep (const_tree expr)
2385 STRIP_NOPS (expr);
2387 if (TREE_CODE (expr) == VECTOR_CST)
2388 return integer_all_onesp (expr);
2389 return integer_onep (expr);
2392 /* Return 1 if EXPR is the fixed-point constant zero. */
2395 fixed_zerop (const_tree expr)
2397 return (TREE_CODE (expr) == FIXED_CST
2398 && TREE_FIXED_CST (expr).data.is_zero ());
2401 /* Return the power of two represented by a tree node known to be a
2402 power of two. */
2405 tree_log2 (const_tree expr)
2407 STRIP_NOPS (expr);
2409 if (TREE_CODE (expr) == COMPLEX_CST)
2410 return tree_log2 (TREE_REALPART (expr));
2412 return wi::exact_log2 (expr);
2415 /* Similar, but return the largest integer Y such that 2 ** Y is less
2416 than or equal to EXPR. */
2419 tree_floor_log2 (const_tree expr)
2421 STRIP_NOPS (expr);
2423 if (TREE_CODE (expr) == COMPLEX_CST)
2424 return tree_log2 (TREE_REALPART (expr));
2426 return wi::floor_log2 (expr);
2429 /* Return number of known trailing zero bits in EXPR, or, if the value of
2430 EXPR is known to be zero, the precision of it's type. */
2432 unsigned int
2433 tree_ctz (const_tree expr)
2435 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
2436 && !POINTER_TYPE_P (TREE_TYPE (expr)))
2437 return 0;
2439 unsigned int ret1, ret2, prec = TYPE_PRECISION (TREE_TYPE (expr));
2440 switch (TREE_CODE (expr))
2442 case INTEGER_CST:
2443 ret1 = wi::ctz (expr);
2444 return MIN (ret1, prec);
2445 case SSA_NAME:
2446 ret1 = wi::ctz (get_nonzero_bits (expr));
2447 return MIN (ret1, prec);
2448 case PLUS_EXPR:
2449 case MINUS_EXPR:
2450 case BIT_IOR_EXPR:
2451 case BIT_XOR_EXPR:
2452 case MIN_EXPR:
2453 case MAX_EXPR:
2454 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2455 if (ret1 == 0)
2456 return ret1;
2457 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2458 return MIN (ret1, ret2);
2459 case POINTER_PLUS_EXPR:
2460 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2461 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2462 /* Second operand is sizetype, which could be in theory
2463 wider than pointer's precision. Make sure we never
2464 return more than prec. */
2465 ret2 = MIN (ret2, prec);
2466 return MIN (ret1, ret2);
2467 case BIT_AND_EXPR:
2468 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2469 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2470 return MAX (ret1, ret2);
2471 case MULT_EXPR:
2472 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2473 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2474 return MIN (ret1 + ret2, prec);
2475 case LSHIFT_EXPR:
2476 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2477 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2478 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2480 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2481 return MIN (ret1 + ret2, prec);
2483 return ret1;
2484 case RSHIFT_EXPR:
2485 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2486 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2488 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2489 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2490 if (ret1 > ret2)
2491 return ret1 - ret2;
2493 return 0;
2494 case TRUNC_DIV_EXPR:
2495 case CEIL_DIV_EXPR:
2496 case FLOOR_DIV_EXPR:
2497 case ROUND_DIV_EXPR:
2498 case EXACT_DIV_EXPR:
2499 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
2500 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) == 1)
2502 int l = tree_log2 (TREE_OPERAND (expr, 1));
2503 if (l >= 0)
2505 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2506 ret2 = l;
2507 if (ret1 > ret2)
2508 return ret1 - ret2;
2511 return 0;
2512 CASE_CONVERT:
2513 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2514 if (ret1 && ret1 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2515 ret1 = prec;
2516 return MIN (ret1, prec);
2517 case SAVE_EXPR:
2518 return tree_ctz (TREE_OPERAND (expr, 0));
2519 case COND_EXPR:
2520 ret1 = tree_ctz (TREE_OPERAND (expr, 1));
2521 if (ret1 == 0)
2522 return 0;
2523 ret2 = tree_ctz (TREE_OPERAND (expr, 2));
2524 return MIN (ret1, ret2);
2525 case COMPOUND_EXPR:
2526 return tree_ctz (TREE_OPERAND (expr, 1));
2527 case ADDR_EXPR:
2528 ret1 = get_pointer_alignment (CONST_CAST_TREE (expr));
2529 if (ret1 > BITS_PER_UNIT)
2531 ret1 = ctz_hwi (ret1 / BITS_PER_UNIT);
2532 return MIN (ret1, prec);
2534 return 0;
2535 default:
2536 return 0;
2540 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
2541 decimal float constants, so don't return 1 for them. */
2544 real_zerop (const_tree expr)
2546 STRIP_NOPS (expr);
2548 switch (TREE_CODE (expr))
2550 case REAL_CST:
2551 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
2552 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2553 case COMPLEX_CST:
2554 return real_zerop (TREE_REALPART (expr))
2555 && real_zerop (TREE_IMAGPART (expr));
2556 case VECTOR_CST:
2558 unsigned i;
2559 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2560 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2561 return false;
2562 return true;
2564 default:
2565 return false;
2569 /* Return 1 if EXPR is the real constant one in real or complex form.
2570 Trailing zeroes matter for decimal float constants, so don't return
2571 1 for them. */
2574 real_onep (const_tree expr)
2576 STRIP_NOPS (expr);
2578 switch (TREE_CODE (expr))
2580 case REAL_CST:
2581 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
2582 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2583 case COMPLEX_CST:
2584 return real_onep (TREE_REALPART (expr))
2585 && real_zerop (TREE_IMAGPART (expr));
2586 case VECTOR_CST:
2588 unsigned i;
2589 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2590 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2591 return false;
2592 return true;
2594 default:
2595 return false;
2599 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2600 matter for decimal float constants, so don't return 1 for them. */
2603 real_minus_onep (const_tree expr)
2605 STRIP_NOPS (expr);
2607 switch (TREE_CODE (expr))
2609 case REAL_CST:
2610 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2611 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2612 case COMPLEX_CST:
2613 return real_minus_onep (TREE_REALPART (expr))
2614 && real_zerop (TREE_IMAGPART (expr));
2615 case VECTOR_CST:
2617 unsigned i;
2618 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2619 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2620 return false;
2621 return true;
2623 default:
2624 return false;
2628 /* Nonzero if EXP is a constant or a cast of a constant. */
2631 really_constant_p (const_tree exp)
2633 /* This is not quite the same as STRIP_NOPS. It does more. */
2634 while (CONVERT_EXPR_P (exp)
2635 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2636 exp = TREE_OPERAND (exp, 0);
2637 return TREE_CONSTANT (exp);
2640 /* Return first list element whose TREE_VALUE is ELEM.
2641 Return 0 if ELEM is not in LIST. */
2643 tree
2644 value_member (tree elem, tree list)
2646 while (list)
2648 if (elem == TREE_VALUE (list))
2649 return list;
2650 list = TREE_CHAIN (list);
2652 return NULL_TREE;
2655 /* Return first list element whose TREE_PURPOSE is ELEM.
2656 Return 0 if ELEM is not in LIST. */
2658 tree
2659 purpose_member (const_tree elem, tree list)
2661 while (list)
2663 if (elem == TREE_PURPOSE (list))
2664 return list;
2665 list = TREE_CHAIN (list);
2667 return NULL_TREE;
2670 /* Return true if ELEM is in V. */
2672 bool
2673 vec_member (const_tree elem, vec<tree, va_gc> *v)
2675 unsigned ix;
2676 tree t;
2677 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2678 if (elem == t)
2679 return true;
2680 return false;
2683 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2684 NULL_TREE. */
2686 tree
2687 chain_index (int idx, tree chain)
2689 for (; chain && idx > 0; --idx)
2690 chain = TREE_CHAIN (chain);
2691 return chain;
2694 /* Return nonzero if ELEM is part of the chain CHAIN. */
2697 chain_member (const_tree elem, const_tree chain)
2699 while (chain)
2701 if (elem == chain)
2702 return 1;
2703 chain = DECL_CHAIN (chain);
2706 return 0;
2709 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2710 We expect a null pointer to mark the end of the chain.
2711 This is the Lisp primitive `length'. */
2714 list_length (const_tree t)
2716 const_tree p = t;
2717 #ifdef ENABLE_TREE_CHECKING
2718 const_tree q = t;
2719 #endif
2720 int len = 0;
2722 while (p)
2724 p = TREE_CHAIN (p);
2725 #ifdef ENABLE_TREE_CHECKING
2726 if (len % 2)
2727 q = TREE_CHAIN (q);
2728 gcc_assert (p != q);
2729 #endif
2730 len++;
2733 return len;
2736 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2737 UNION_TYPE TYPE, or NULL_TREE if none. */
2739 tree
2740 first_field (const_tree type)
2742 tree t = TYPE_FIELDS (type);
2743 while (t && TREE_CODE (t) != FIELD_DECL)
2744 t = TREE_CHAIN (t);
2745 return t;
2748 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2749 by modifying the last node in chain 1 to point to chain 2.
2750 This is the Lisp primitive `nconc'. */
2752 tree
2753 chainon (tree op1, tree op2)
2755 tree t1;
2757 if (!op1)
2758 return op2;
2759 if (!op2)
2760 return op1;
2762 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2763 continue;
2764 TREE_CHAIN (t1) = op2;
2766 #ifdef ENABLE_TREE_CHECKING
2768 tree t2;
2769 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2770 gcc_assert (t2 != t1);
2772 #endif
2774 return op1;
2777 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2779 tree
2780 tree_last (tree chain)
2782 tree next;
2783 if (chain)
2784 while ((next = TREE_CHAIN (chain)))
2785 chain = next;
2786 return chain;
2789 /* Reverse the order of elements in the chain T,
2790 and return the new head of the chain (old last element). */
2792 tree
2793 nreverse (tree t)
2795 tree prev = 0, decl, next;
2796 for (decl = t; decl; decl = next)
2798 /* We shouldn't be using this function to reverse BLOCK chains; we
2799 have blocks_nreverse for that. */
2800 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2801 next = TREE_CHAIN (decl);
2802 TREE_CHAIN (decl) = prev;
2803 prev = decl;
2805 return prev;
2808 /* Return a newly created TREE_LIST node whose
2809 purpose and value fields are PARM and VALUE. */
2811 tree
2812 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2814 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2815 TREE_PURPOSE (t) = parm;
2816 TREE_VALUE (t) = value;
2817 return t;
2820 /* Build a chain of TREE_LIST nodes from a vector. */
2822 tree
2823 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2825 tree ret = NULL_TREE;
2826 tree *pp = &ret;
2827 unsigned int i;
2828 tree t;
2829 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2831 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2832 pp = &TREE_CHAIN (*pp);
2834 return ret;
2837 /* Return a newly created TREE_LIST node whose
2838 purpose and value fields are PURPOSE and VALUE
2839 and whose TREE_CHAIN is CHAIN. */
2841 tree
2842 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2844 tree node;
2846 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
2847 memset (node, 0, sizeof (struct tree_common));
2849 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2851 TREE_SET_CODE (node, TREE_LIST);
2852 TREE_CHAIN (node) = chain;
2853 TREE_PURPOSE (node) = purpose;
2854 TREE_VALUE (node) = value;
2855 return node;
2858 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2859 trees. */
2861 vec<tree, va_gc> *
2862 ctor_to_vec (tree ctor)
2864 vec<tree, va_gc> *vec;
2865 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2866 unsigned int ix;
2867 tree val;
2869 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2870 vec->quick_push (val);
2872 return vec;
2875 /* Return the size nominally occupied by an object of type TYPE
2876 when it resides in memory. The value is measured in units of bytes,
2877 and its data type is that normally used for type sizes
2878 (which is the first type created by make_signed_type or
2879 make_unsigned_type). */
2881 tree
2882 size_in_bytes (const_tree type)
2884 tree t;
2886 if (type == error_mark_node)
2887 return integer_zero_node;
2889 type = TYPE_MAIN_VARIANT (type);
2890 t = TYPE_SIZE_UNIT (type);
2892 if (t == 0)
2894 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2895 return size_zero_node;
2898 return t;
2901 /* Return the size of TYPE (in bytes) as a wide integer
2902 or return -1 if the size can vary or is larger than an integer. */
2904 HOST_WIDE_INT
2905 int_size_in_bytes (const_tree type)
2907 tree t;
2909 if (type == error_mark_node)
2910 return 0;
2912 type = TYPE_MAIN_VARIANT (type);
2913 t = TYPE_SIZE_UNIT (type);
2915 if (t && tree_fits_uhwi_p (t))
2916 return TREE_INT_CST_LOW (t);
2917 else
2918 return -1;
2921 /* Return the maximum size of TYPE (in bytes) as a wide integer
2922 or return -1 if the size can vary or is larger than an integer. */
2924 HOST_WIDE_INT
2925 max_int_size_in_bytes (const_tree type)
2927 HOST_WIDE_INT size = -1;
2928 tree size_tree;
2930 /* If this is an array type, check for a possible MAX_SIZE attached. */
2932 if (TREE_CODE (type) == ARRAY_TYPE)
2934 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2936 if (size_tree && tree_fits_uhwi_p (size_tree))
2937 size = tree_to_uhwi (size_tree);
2940 /* If we still haven't been able to get a size, see if the language
2941 can compute a maximum size. */
2943 if (size == -1)
2945 size_tree = lang_hooks.types.max_size (type);
2947 if (size_tree && tree_fits_uhwi_p (size_tree))
2948 size = tree_to_uhwi (size_tree);
2951 return size;
2954 /* Return the bit position of FIELD, in bits from the start of the record.
2955 This is a tree of type bitsizetype. */
2957 tree
2958 bit_position (const_tree field)
2960 return bit_from_pos (DECL_FIELD_OFFSET (field),
2961 DECL_FIELD_BIT_OFFSET (field));
2964 /* Return the byte position of FIELD, in bytes from the start of the record.
2965 This is a tree of type sizetype. */
2967 tree
2968 byte_position (const_tree field)
2970 return byte_from_pos (DECL_FIELD_OFFSET (field),
2971 DECL_FIELD_BIT_OFFSET (field));
2974 /* Likewise, but return as an integer. It must be representable in
2975 that way (since it could be a signed value, we don't have the
2976 option of returning -1 like int_size_in_byte can. */
2978 HOST_WIDE_INT
2979 int_byte_position (const_tree field)
2981 return tree_to_shwi (byte_position (field));
2984 /* Return the strictest alignment, in bits, that T is known to have. */
2986 unsigned int
2987 expr_align (const_tree t)
2989 unsigned int align0, align1;
2991 switch (TREE_CODE (t))
2993 CASE_CONVERT: case NON_LVALUE_EXPR:
2994 /* If we have conversions, we know that the alignment of the
2995 object must meet each of the alignments of the types. */
2996 align0 = expr_align (TREE_OPERAND (t, 0));
2997 align1 = TYPE_ALIGN (TREE_TYPE (t));
2998 return MAX (align0, align1);
3000 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
3001 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
3002 case CLEANUP_POINT_EXPR:
3003 /* These don't change the alignment of an object. */
3004 return expr_align (TREE_OPERAND (t, 0));
3006 case COND_EXPR:
3007 /* The best we can do is say that the alignment is the least aligned
3008 of the two arms. */
3009 align0 = expr_align (TREE_OPERAND (t, 1));
3010 align1 = expr_align (TREE_OPERAND (t, 2));
3011 return MIN (align0, align1);
3013 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
3014 meaningfully, it's always 1. */
3015 case LABEL_DECL: case CONST_DECL:
3016 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
3017 case FUNCTION_DECL:
3018 gcc_assert (DECL_ALIGN (t) != 0);
3019 return DECL_ALIGN (t);
3021 default:
3022 break;
3025 /* Otherwise take the alignment from that of the type. */
3026 return TYPE_ALIGN (TREE_TYPE (t));
3029 /* Return, as a tree node, the number of elements for TYPE (which is an
3030 ARRAY_TYPE) minus one. This counts only elements of the top array. */
3032 tree
3033 array_type_nelts (const_tree type)
3035 tree index_type, min, max;
3037 /* If they did it with unspecified bounds, then we should have already
3038 given an error about it before we got here. */
3039 if (! TYPE_DOMAIN (type))
3040 return error_mark_node;
3042 index_type = TYPE_DOMAIN (type);
3043 min = TYPE_MIN_VALUE (index_type);
3044 max = TYPE_MAX_VALUE (index_type);
3046 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
3047 if (!max)
3048 return error_mark_node;
3050 return (integer_zerop (min)
3051 ? max
3052 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
3055 /* If arg is static -- a reference to an object in static storage -- then
3056 return the object. This is not the same as the C meaning of `static'.
3057 If arg isn't static, return NULL. */
3059 tree
3060 staticp (tree arg)
3062 switch (TREE_CODE (arg))
3064 case FUNCTION_DECL:
3065 /* Nested functions are static, even though taking their address will
3066 involve a trampoline as we unnest the nested function and create
3067 the trampoline on the tree level. */
3068 return arg;
3070 case VAR_DECL:
3071 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3072 && ! DECL_THREAD_LOCAL_P (arg)
3073 && ! DECL_DLLIMPORT_P (arg)
3074 ? arg : NULL);
3076 case CONST_DECL:
3077 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3078 ? arg : NULL);
3080 case CONSTRUCTOR:
3081 return TREE_STATIC (arg) ? arg : NULL;
3083 case LABEL_DECL:
3084 case STRING_CST:
3085 return arg;
3087 case COMPONENT_REF:
3088 /* If the thing being referenced is not a field, then it is
3089 something language specific. */
3090 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
3092 /* If we are referencing a bitfield, we can't evaluate an
3093 ADDR_EXPR at compile time and so it isn't a constant. */
3094 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
3095 return NULL;
3097 return staticp (TREE_OPERAND (arg, 0));
3099 case BIT_FIELD_REF:
3100 return NULL;
3102 case INDIRECT_REF:
3103 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
3105 case ARRAY_REF:
3106 case ARRAY_RANGE_REF:
3107 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
3108 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
3109 return staticp (TREE_OPERAND (arg, 0));
3110 else
3111 return NULL;
3113 case COMPOUND_LITERAL_EXPR:
3114 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
3116 default:
3117 return NULL;
3124 /* Return whether OP is a DECL whose address is function-invariant. */
3126 bool
3127 decl_address_invariant_p (const_tree op)
3129 /* The conditions below are slightly less strict than the one in
3130 staticp. */
3132 switch (TREE_CODE (op))
3134 case PARM_DECL:
3135 case RESULT_DECL:
3136 case LABEL_DECL:
3137 case FUNCTION_DECL:
3138 return true;
3140 case VAR_DECL:
3141 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3142 || DECL_THREAD_LOCAL_P (op)
3143 || DECL_CONTEXT (op) == current_function_decl
3144 || decl_function_context (op) == current_function_decl)
3145 return true;
3146 break;
3148 case CONST_DECL:
3149 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3150 || decl_function_context (op) == current_function_decl)
3151 return true;
3152 break;
3154 default:
3155 break;
3158 return false;
3161 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
3163 bool
3164 decl_address_ip_invariant_p (const_tree op)
3166 /* The conditions below are slightly less strict than the one in
3167 staticp. */
3169 switch (TREE_CODE (op))
3171 case LABEL_DECL:
3172 case FUNCTION_DECL:
3173 case STRING_CST:
3174 return true;
3176 case VAR_DECL:
3177 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
3178 && !DECL_DLLIMPORT_P (op))
3179 || DECL_THREAD_LOCAL_P (op))
3180 return true;
3181 break;
3183 case CONST_DECL:
3184 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
3185 return true;
3186 break;
3188 default:
3189 break;
3192 return false;
3196 /* Return true if T is function-invariant (internal function, does
3197 not handle arithmetic; that's handled in skip_simple_arithmetic and
3198 tree_invariant_p). */
3200 static bool tree_invariant_p (tree t);
3202 static bool
3203 tree_invariant_p_1 (tree t)
3205 tree op;
3207 if (TREE_CONSTANT (t)
3208 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
3209 return true;
3211 switch (TREE_CODE (t))
3213 case SAVE_EXPR:
3214 return true;
3216 case ADDR_EXPR:
3217 op = TREE_OPERAND (t, 0);
3218 while (handled_component_p (op))
3220 switch (TREE_CODE (op))
3222 case ARRAY_REF:
3223 case ARRAY_RANGE_REF:
3224 if (!tree_invariant_p (TREE_OPERAND (op, 1))
3225 || TREE_OPERAND (op, 2) != NULL_TREE
3226 || TREE_OPERAND (op, 3) != NULL_TREE)
3227 return false;
3228 break;
3230 case COMPONENT_REF:
3231 if (TREE_OPERAND (op, 2) != NULL_TREE)
3232 return false;
3233 break;
3235 default:;
3237 op = TREE_OPERAND (op, 0);
3240 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
3242 default:
3243 break;
3246 return false;
3249 /* Return true if T is function-invariant. */
3251 static bool
3252 tree_invariant_p (tree t)
3254 tree inner = skip_simple_arithmetic (t);
3255 return tree_invariant_p_1 (inner);
3258 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
3259 Do this to any expression which may be used in more than one place,
3260 but must be evaluated only once.
3262 Normally, expand_expr would reevaluate the expression each time.
3263 Calling save_expr produces something that is evaluated and recorded
3264 the first time expand_expr is called on it. Subsequent calls to
3265 expand_expr just reuse the recorded value.
3267 The call to expand_expr that generates code that actually computes
3268 the value is the first call *at compile time*. Subsequent calls
3269 *at compile time* generate code to use the saved value.
3270 This produces correct result provided that *at run time* control
3271 always flows through the insns made by the first expand_expr
3272 before reaching the other places where the save_expr was evaluated.
3273 You, the caller of save_expr, must make sure this is so.
3275 Constants, and certain read-only nodes, are returned with no
3276 SAVE_EXPR because that is safe. Expressions containing placeholders
3277 are not touched; see tree.def for an explanation of what these
3278 are used for. */
3280 tree
3281 save_expr (tree expr)
3283 tree t = fold (expr);
3284 tree inner;
3286 /* If the tree evaluates to a constant, then we don't want to hide that
3287 fact (i.e. this allows further folding, and direct checks for constants).
3288 However, a read-only object that has side effects cannot be bypassed.
3289 Since it is no problem to reevaluate literals, we just return the
3290 literal node. */
3291 inner = skip_simple_arithmetic (t);
3292 if (TREE_CODE (inner) == ERROR_MARK)
3293 return inner;
3295 if (tree_invariant_p_1 (inner))
3296 return t;
3298 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
3299 it means that the size or offset of some field of an object depends on
3300 the value within another field.
3302 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
3303 and some variable since it would then need to be both evaluated once and
3304 evaluated more than once. Front-ends must assure this case cannot
3305 happen by surrounding any such subexpressions in their own SAVE_EXPR
3306 and forcing evaluation at the proper time. */
3307 if (contains_placeholder_p (inner))
3308 return t;
3310 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
3311 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
3313 /* This expression might be placed ahead of a jump to ensure that the
3314 value was computed on both sides of the jump. So make sure it isn't
3315 eliminated as dead. */
3316 TREE_SIDE_EFFECTS (t) = 1;
3317 return t;
3320 /* Look inside EXPR into any simple arithmetic operations. Return the
3321 outermost non-arithmetic or non-invariant node. */
3323 tree
3324 skip_simple_arithmetic (tree expr)
3326 /* We don't care about whether this can be used as an lvalue in this
3327 context. */
3328 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3329 expr = TREE_OPERAND (expr, 0);
3331 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
3332 a constant, it will be more efficient to not make another SAVE_EXPR since
3333 it will allow better simplification and GCSE will be able to merge the
3334 computations if they actually occur. */
3335 while (true)
3337 if (UNARY_CLASS_P (expr))
3338 expr = TREE_OPERAND (expr, 0);
3339 else if (BINARY_CLASS_P (expr))
3341 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
3342 expr = TREE_OPERAND (expr, 0);
3343 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
3344 expr = TREE_OPERAND (expr, 1);
3345 else
3346 break;
3348 else
3349 break;
3352 return expr;
3355 /* Look inside EXPR into simple arithmetic operations involving constants.
3356 Return the outermost non-arithmetic or non-constant node. */
3358 tree
3359 skip_simple_constant_arithmetic (tree expr)
3361 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3362 expr = TREE_OPERAND (expr, 0);
3364 while (true)
3366 if (UNARY_CLASS_P (expr))
3367 expr = TREE_OPERAND (expr, 0);
3368 else if (BINARY_CLASS_P (expr))
3370 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3371 expr = TREE_OPERAND (expr, 0);
3372 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3373 expr = TREE_OPERAND (expr, 1);
3374 else
3375 break;
3377 else
3378 break;
3381 return expr;
3384 /* Return which tree structure is used by T. */
3386 enum tree_node_structure_enum
3387 tree_node_structure (const_tree t)
3389 const enum tree_code code = TREE_CODE (t);
3390 return tree_node_structure_for_code (code);
3393 /* Set various status flags when building a CALL_EXPR object T. */
3395 static void
3396 process_call_operands (tree t)
3398 bool side_effects = TREE_SIDE_EFFECTS (t);
3399 bool read_only = false;
3400 int i = call_expr_flags (t);
3402 /* Calls have side-effects, except those to const or pure functions. */
3403 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
3404 side_effects = true;
3405 /* Propagate TREE_READONLY of arguments for const functions. */
3406 if (i & ECF_CONST)
3407 read_only = true;
3409 if (!side_effects || read_only)
3410 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
3412 tree op = TREE_OPERAND (t, i);
3413 if (op && TREE_SIDE_EFFECTS (op))
3414 side_effects = true;
3415 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
3416 read_only = false;
3419 TREE_SIDE_EFFECTS (t) = side_effects;
3420 TREE_READONLY (t) = read_only;
3423 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
3424 size or offset that depends on a field within a record. */
3426 bool
3427 contains_placeholder_p (const_tree exp)
3429 enum tree_code code;
3431 if (!exp)
3432 return 0;
3434 code = TREE_CODE (exp);
3435 if (code == PLACEHOLDER_EXPR)
3436 return 1;
3438 switch (TREE_CODE_CLASS (code))
3440 case tcc_reference:
3441 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
3442 position computations since they will be converted into a
3443 WITH_RECORD_EXPR involving the reference, which will assume
3444 here will be valid. */
3445 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3447 case tcc_exceptional:
3448 if (code == TREE_LIST)
3449 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
3450 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
3451 break;
3453 case tcc_unary:
3454 case tcc_binary:
3455 case tcc_comparison:
3456 case tcc_expression:
3457 switch (code)
3459 case COMPOUND_EXPR:
3460 /* Ignoring the first operand isn't quite right, but works best. */
3461 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
3463 case COND_EXPR:
3464 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3465 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
3466 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
3468 case SAVE_EXPR:
3469 /* The save_expr function never wraps anything containing
3470 a PLACEHOLDER_EXPR. */
3471 return 0;
3473 default:
3474 break;
3477 switch (TREE_CODE_LENGTH (code))
3479 case 1:
3480 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3481 case 2:
3482 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3483 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
3484 default:
3485 return 0;
3488 case tcc_vl_exp:
3489 switch (code)
3491 case CALL_EXPR:
3493 const_tree arg;
3494 const_call_expr_arg_iterator iter;
3495 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
3496 if (CONTAINS_PLACEHOLDER_P (arg))
3497 return 1;
3498 return 0;
3500 default:
3501 return 0;
3504 default:
3505 return 0;
3507 return 0;
3510 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3511 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3512 field positions. */
3514 static bool
3515 type_contains_placeholder_1 (const_tree type)
3517 /* If the size contains a placeholder or the parent type (component type in
3518 the case of arrays) type involves a placeholder, this type does. */
3519 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3520 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3521 || (!POINTER_TYPE_P (type)
3522 && TREE_TYPE (type)
3523 && type_contains_placeholder_p (TREE_TYPE (type))))
3524 return true;
3526 /* Now do type-specific checks. Note that the last part of the check above
3527 greatly limits what we have to do below. */
3528 switch (TREE_CODE (type))
3530 case VOID_TYPE:
3531 case POINTER_BOUNDS_TYPE:
3532 case COMPLEX_TYPE:
3533 case ENUMERAL_TYPE:
3534 case BOOLEAN_TYPE:
3535 case POINTER_TYPE:
3536 case OFFSET_TYPE:
3537 case REFERENCE_TYPE:
3538 case METHOD_TYPE:
3539 case FUNCTION_TYPE:
3540 case VECTOR_TYPE:
3541 case NULLPTR_TYPE:
3542 return false;
3544 case INTEGER_TYPE:
3545 case REAL_TYPE:
3546 case FIXED_POINT_TYPE:
3547 /* Here we just check the bounds. */
3548 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3549 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3551 case ARRAY_TYPE:
3552 /* We have already checked the component type above, so just check the
3553 domain type. */
3554 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3556 case RECORD_TYPE:
3557 case UNION_TYPE:
3558 case QUAL_UNION_TYPE:
3560 tree field;
3562 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3563 if (TREE_CODE (field) == FIELD_DECL
3564 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3565 || (TREE_CODE (type) == QUAL_UNION_TYPE
3566 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3567 || type_contains_placeholder_p (TREE_TYPE (field))))
3568 return true;
3570 return false;
3573 default:
3574 gcc_unreachable ();
3578 /* Wrapper around above function used to cache its result. */
3580 bool
3581 type_contains_placeholder_p (tree type)
3583 bool result;
3585 /* If the contains_placeholder_bits field has been initialized,
3586 then we know the answer. */
3587 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3588 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3590 /* Indicate that we've seen this type node, and the answer is false.
3591 This is what we want to return if we run into recursion via fields. */
3592 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3594 /* Compute the real value. */
3595 result = type_contains_placeholder_1 (type);
3597 /* Store the real value. */
3598 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3600 return result;
3603 /* Push tree EXP onto vector QUEUE if it is not already present. */
3605 static void
3606 push_without_duplicates (tree exp, vec<tree> *queue)
3608 unsigned int i;
3609 tree iter;
3611 FOR_EACH_VEC_ELT (*queue, i, iter)
3612 if (simple_cst_equal (iter, exp) == 1)
3613 break;
3615 if (!iter)
3616 queue->safe_push (exp);
3619 /* Given a tree EXP, find all occurrences of references to fields
3620 in a PLACEHOLDER_EXPR and place them in vector REFS without
3621 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3622 we assume here that EXP contains only arithmetic expressions
3623 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3624 argument list. */
3626 void
3627 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3629 enum tree_code code = TREE_CODE (exp);
3630 tree inner;
3631 int i;
3633 /* We handle TREE_LIST and COMPONENT_REF separately. */
3634 if (code == TREE_LIST)
3636 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3637 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3639 else if (code == COMPONENT_REF)
3641 for (inner = TREE_OPERAND (exp, 0);
3642 REFERENCE_CLASS_P (inner);
3643 inner = TREE_OPERAND (inner, 0))
3646 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3647 push_without_duplicates (exp, refs);
3648 else
3649 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3651 else
3652 switch (TREE_CODE_CLASS (code))
3654 case tcc_constant:
3655 break;
3657 case tcc_declaration:
3658 /* Variables allocated to static storage can stay. */
3659 if (!TREE_STATIC (exp))
3660 push_without_duplicates (exp, refs);
3661 break;
3663 case tcc_expression:
3664 /* This is the pattern built in ada/make_aligning_type. */
3665 if (code == ADDR_EXPR
3666 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3668 push_without_duplicates (exp, refs);
3669 break;
3672 /* Fall through... */
3674 case tcc_exceptional:
3675 case tcc_unary:
3676 case tcc_binary:
3677 case tcc_comparison:
3678 case tcc_reference:
3679 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3680 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3681 break;
3683 case tcc_vl_exp:
3684 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3685 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3686 break;
3688 default:
3689 gcc_unreachable ();
3693 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3694 return a tree with all occurrences of references to F in a
3695 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3696 CONST_DECLs. Note that we assume here that EXP contains only
3697 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3698 occurring only in their argument list. */
3700 tree
3701 substitute_in_expr (tree exp, tree f, tree r)
3703 enum tree_code code = TREE_CODE (exp);
3704 tree op0, op1, op2, op3;
3705 tree new_tree;
3707 /* We handle TREE_LIST and COMPONENT_REF separately. */
3708 if (code == TREE_LIST)
3710 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3711 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3712 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3713 return exp;
3715 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3717 else if (code == COMPONENT_REF)
3719 tree inner;
3721 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3722 and it is the right field, replace it with R. */
3723 for (inner = TREE_OPERAND (exp, 0);
3724 REFERENCE_CLASS_P (inner);
3725 inner = TREE_OPERAND (inner, 0))
3728 /* The field. */
3729 op1 = TREE_OPERAND (exp, 1);
3731 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3732 return r;
3734 /* If this expression hasn't been completed let, leave it alone. */
3735 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3736 return exp;
3738 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3739 if (op0 == TREE_OPERAND (exp, 0))
3740 return exp;
3742 new_tree
3743 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3745 else
3746 switch (TREE_CODE_CLASS (code))
3748 case tcc_constant:
3749 return exp;
3751 case tcc_declaration:
3752 if (exp == f)
3753 return r;
3754 else
3755 return exp;
3757 case tcc_expression:
3758 if (exp == f)
3759 return r;
3761 /* Fall through... */
3763 case tcc_exceptional:
3764 case tcc_unary:
3765 case tcc_binary:
3766 case tcc_comparison:
3767 case tcc_reference:
3768 switch (TREE_CODE_LENGTH (code))
3770 case 0:
3771 return exp;
3773 case 1:
3774 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3775 if (op0 == TREE_OPERAND (exp, 0))
3776 return exp;
3778 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3779 break;
3781 case 2:
3782 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3783 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3785 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3786 return exp;
3788 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3789 break;
3791 case 3:
3792 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3793 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3794 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3796 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3797 && op2 == TREE_OPERAND (exp, 2))
3798 return exp;
3800 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3801 break;
3803 case 4:
3804 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3805 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3806 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3807 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3809 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3810 && op2 == TREE_OPERAND (exp, 2)
3811 && op3 == TREE_OPERAND (exp, 3))
3812 return exp;
3814 new_tree
3815 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3816 break;
3818 default:
3819 gcc_unreachable ();
3821 break;
3823 case tcc_vl_exp:
3825 int i;
3827 new_tree = NULL_TREE;
3829 /* If we are trying to replace F with a constant, inline back
3830 functions which do nothing else than computing a value from
3831 the arguments they are passed. This makes it possible to
3832 fold partially or entirely the replacement expression. */
3833 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3835 tree t = maybe_inline_call_in_expr (exp);
3836 if (t)
3837 return SUBSTITUTE_IN_EXPR (t, f, r);
3840 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3842 tree op = TREE_OPERAND (exp, i);
3843 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3844 if (new_op != op)
3846 if (!new_tree)
3847 new_tree = copy_node (exp);
3848 TREE_OPERAND (new_tree, i) = new_op;
3852 if (new_tree)
3854 new_tree = fold (new_tree);
3855 if (TREE_CODE (new_tree) == CALL_EXPR)
3856 process_call_operands (new_tree);
3858 else
3859 return exp;
3861 break;
3863 default:
3864 gcc_unreachable ();
3867 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3869 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3870 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3872 return new_tree;
3875 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3876 for it within OBJ, a tree that is an object or a chain of references. */
3878 tree
3879 substitute_placeholder_in_expr (tree exp, tree obj)
3881 enum tree_code code = TREE_CODE (exp);
3882 tree op0, op1, op2, op3;
3883 tree new_tree;
3885 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3886 in the chain of OBJ. */
3887 if (code == PLACEHOLDER_EXPR)
3889 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3890 tree elt;
3892 for (elt = obj; elt != 0;
3893 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3894 || TREE_CODE (elt) == COND_EXPR)
3895 ? TREE_OPERAND (elt, 1)
3896 : (REFERENCE_CLASS_P (elt)
3897 || UNARY_CLASS_P (elt)
3898 || BINARY_CLASS_P (elt)
3899 || VL_EXP_CLASS_P (elt)
3900 || EXPRESSION_CLASS_P (elt))
3901 ? TREE_OPERAND (elt, 0) : 0))
3902 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3903 return elt;
3905 for (elt = obj; elt != 0;
3906 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3907 || TREE_CODE (elt) == COND_EXPR)
3908 ? TREE_OPERAND (elt, 1)
3909 : (REFERENCE_CLASS_P (elt)
3910 || UNARY_CLASS_P (elt)
3911 || BINARY_CLASS_P (elt)
3912 || VL_EXP_CLASS_P (elt)
3913 || EXPRESSION_CLASS_P (elt))
3914 ? TREE_OPERAND (elt, 0) : 0))
3915 if (POINTER_TYPE_P (TREE_TYPE (elt))
3916 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3917 == need_type))
3918 return fold_build1 (INDIRECT_REF, need_type, elt);
3920 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3921 survives until RTL generation, there will be an error. */
3922 return exp;
3925 /* TREE_LIST is special because we need to look at TREE_VALUE
3926 and TREE_CHAIN, not TREE_OPERANDS. */
3927 else if (code == TREE_LIST)
3929 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3930 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3931 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3932 return exp;
3934 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3936 else
3937 switch (TREE_CODE_CLASS (code))
3939 case tcc_constant:
3940 case tcc_declaration:
3941 return exp;
3943 case tcc_exceptional:
3944 case tcc_unary:
3945 case tcc_binary:
3946 case tcc_comparison:
3947 case tcc_expression:
3948 case tcc_reference:
3949 case tcc_statement:
3950 switch (TREE_CODE_LENGTH (code))
3952 case 0:
3953 return exp;
3955 case 1:
3956 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3957 if (op0 == TREE_OPERAND (exp, 0))
3958 return exp;
3960 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3961 break;
3963 case 2:
3964 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3965 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3967 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3968 return exp;
3970 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3971 break;
3973 case 3:
3974 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3975 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3976 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3978 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3979 && op2 == TREE_OPERAND (exp, 2))
3980 return exp;
3982 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3983 break;
3985 case 4:
3986 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3987 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3988 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3989 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3991 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3992 && op2 == TREE_OPERAND (exp, 2)
3993 && op3 == TREE_OPERAND (exp, 3))
3994 return exp;
3996 new_tree
3997 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3998 break;
4000 default:
4001 gcc_unreachable ();
4003 break;
4005 case tcc_vl_exp:
4007 int i;
4009 new_tree = NULL_TREE;
4011 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4013 tree op = TREE_OPERAND (exp, i);
4014 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
4015 if (new_op != op)
4017 if (!new_tree)
4018 new_tree = copy_node (exp);
4019 TREE_OPERAND (new_tree, i) = new_op;
4023 if (new_tree)
4025 new_tree = fold (new_tree);
4026 if (TREE_CODE (new_tree) == CALL_EXPR)
4027 process_call_operands (new_tree);
4029 else
4030 return exp;
4032 break;
4034 default:
4035 gcc_unreachable ();
4038 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4040 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4041 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4043 return new_tree;
4047 /* Subroutine of stabilize_reference; this is called for subtrees of
4048 references. Any expression with side-effects must be put in a SAVE_EXPR
4049 to ensure that it is only evaluated once.
4051 We don't put SAVE_EXPR nodes around everything, because assigning very
4052 simple expressions to temporaries causes us to miss good opportunities
4053 for optimizations. Among other things, the opportunity to fold in the
4054 addition of a constant into an addressing mode often gets lost, e.g.
4055 "y[i+1] += x;". In general, we take the approach that we should not make
4056 an assignment unless we are forced into it - i.e., that any non-side effect
4057 operator should be allowed, and that cse should take care of coalescing
4058 multiple utterances of the same expression should that prove fruitful. */
4060 static tree
4061 stabilize_reference_1 (tree e)
4063 tree result;
4064 enum tree_code code = TREE_CODE (e);
4066 /* We cannot ignore const expressions because it might be a reference
4067 to a const array but whose index contains side-effects. But we can
4068 ignore things that are actual constant or that already have been
4069 handled by this function. */
4071 if (tree_invariant_p (e))
4072 return e;
4074 switch (TREE_CODE_CLASS (code))
4076 case tcc_exceptional:
4077 case tcc_type:
4078 case tcc_declaration:
4079 case tcc_comparison:
4080 case tcc_statement:
4081 case tcc_expression:
4082 case tcc_reference:
4083 case tcc_vl_exp:
4084 /* If the expression has side-effects, then encase it in a SAVE_EXPR
4085 so that it will only be evaluated once. */
4086 /* The reference (r) and comparison (<) classes could be handled as
4087 below, but it is generally faster to only evaluate them once. */
4088 if (TREE_SIDE_EFFECTS (e))
4089 return save_expr (e);
4090 return e;
4092 case tcc_constant:
4093 /* Constants need no processing. In fact, we should never reach
4094 here. */
4095 return e;
4097 case tcc_binary:
4098 /* Division is slow and tends to be compiled with jumps,
4099 especially the division by powers of 2 that is often
4100 found inside of an array reference. So do it just once. */
4101 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
4102 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
4103 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
4104 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
4105 return save_expr (e);
4106 /* Recursively stabilize each operand. */
4107 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
4108 stabilize_reference_1 (TREE_OPERAND (e, 1)));
4109 break;
4111 case tcc_unary:
4112 /* Recursively stabilize each operand. */
4113 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
4114 break;
4116 default:
4117 gcc_unreachable ();
4120 TREE_TYPE (result) = TREE_TYPE (e);
4121 TREE_READONLY (result) = TREE_READONLY (e);
4122 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
4123 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
4125 return result;
4128 /* Stabilize a reference so that we can use it any number of times
4129 without causing its operands to be evaluated more than once.
4130 Returns the stabilized reference. This works by means of save_expr,
4131 so see the caveats in the comments about save_expr.
4133 Also allows conversion expressions whose operands are references.
4134 Any other kind of expression is returned unchanged. */
4136 tree
4137 stabilize_reference (tree ref)
4139 tree result;
4140 enum tree_code code = TREE_CODE (ref);
4142 switch (code)
4144 case VAR_DECL:
4145 case PARM_DECL:
4146 case RESULT_DECL:
4147 /* No action is needed in this case. */
4148 return ref;
4150 CASE_CONVERT:
4151 case FLOAT_EXPR:
4152 case FIX_TRUNC_EXPR:
4153 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
4154 break;
4156 case INDIRECT_REF:
4157 result = build_nt (INDIRECT_REF,
4158 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
4159 break;
4161 case COMPONENT_REF:
4162 result = build_nt (COMPONENT_REF,
4163 stabilize_reference (TREE_OPERAND (ref, 0)),
4164 TREE_OPERAND (ref, 1), NULL_TREE);
4165 break;
4167 case BIT_FIELD_REF:
4168 result = build_nt (BIT_FIELD_REF,
4169 stabilize_reference (TREE_OPERAND (ref, 0)),
4170 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
4171 break;
4173 case ARRAY_REF:
4174 result = build_nt (ARRAY_REF,
4175 stabilize_reference (TREE_OPERAND (ref, 0)),
4176 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4177 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4178 break;
4180 case ARRAY_RANGE_REF:
4181 result = build_nt (ARRAY_RANGE_REF,
4182 stabilize_reference (TREE_OPERAND (ref, 0)),
4183 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4184 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4185 break;
4187 case COMPOUND_EXPR:
4188 /* We cannot wrap the first expression in a SAVE_EXPR, as then
4189 it wouldn't be ignored. This matters when dealing with
4190 volatiles. */
4191 return stabilize_reference_1 (ref);
4193 /* If arg isn't a kind of lvalue we recognize, make no change.
4194 Caller should recognize the error for an invalid lvalue. */
4195 default:
4196 return ref;
4198 case ERROR_MARK:
4199 return error_mark_node;
4202 TREE_TYPE (result) = TREE_TYPE (ref);
4203 TREE_READONLY (result) = TREE_READONLY (ref);
4204 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
4205 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
4207 return result;
4210 /* Low-level constructors for expressions. */
4212 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
4213 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
4215 void
4216 recompute_tree_invariant_for_addr_expr (tree t)
4218 tree node;
4219 bool tc = true, se = false;
4221 /* We started out assuming this address is both invariant and constant, but
4222 does not have side effects. Now go down any handled components and see if
4223 any of them involve offsets that are either non-constant or non-invariant.
4224 Also check for side-effects.
4226 ??? Note that this code makes no attempt to deal with the case where
4227 taking the address of something causes a copy due to misalignment. */
4229 #define UPDATE_FLAGS(NODE) \
4230 do { tree _node = (NODE); \
4231 if (_node && !TREE_CONSTANT (_node)) tc = false; \
4232 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
4234 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
4235 node = TREE_OPERAND (node, 0))
4237 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
4238 array reference (probably made temporarily by the G++ front end),
4239 so ignore all the operands. */
4240 if ((TREE_CODE (node) == ARRAY_REF
4241 || TREE_CODE (node) == ARRAY_RANGE_REF)
4242 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
4244 UPDATE_FLAGS (TREE_OPERAND (node, 1));
4245 if (TREE_OPERAND (node, 2))
4246 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4247 if (TREE_OPERAND (node, 3))
4248 UPDATE_FLAGS (TREE_OPERAND (node, 3));
4250 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
4251 FIELD_DECL, apparently. The G++ front end can put something else
4252 there, at least temporarily. */
4253 else if (TREE_CODE (node) == COMPONENT_REF
4254 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
4256 if (TREE_OPERAND (node, 2))
4257 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4261 node = lang_hooks.expr_to_decl (node, &tc, &se);
4263 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4264 the address, since &(*a)->b is a form of addition. If it's a constant, the
4265 address is constant too. If it's a decl, its address is constant if the
4266 decl is static. Everything else is not constant and, furthermore,
4267 taking the address of a volatile variable is not volatile. */
4268 if (TREE_CODE (node) == INDIRECT_REF
4269 || TREE_CODE (node) == MEM_REF)
4270 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4271 else if (CONSTANT_CLASS_P (node))
4273 else if (DECL_P (node))
4274 tc &= (staticp (node) != NULL_TREE);
4275 else
4277 tc = false;
4278 se |= TREE_SIDE_EFFECTS (node);
4282 TREE_CONSTANT (t) = tc;
4283 TREE_SIDE_EFFECTS (t) = se;
4284 #undef UPDATE_FLAGS
4287 /* Build an expression of code CODE, data type TYPE, and operands as
4288 specified. Expressions and reference nodes can be created this way.
4289 Constants, decls, types and misc nodes cannot be.
4291 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4292 enough for all extant tree codes. */
4294 tree
4295 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
4297 tree t;
4299 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4301 t = make_node_stat (code PASS_MEM_STAT);
4302 TREE_TYPE (t) = tt;
4304 return t;
4307 tree
4308 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4310 int length = sizeof (struct tree_exp);
4311 tree t;
4313 record_node_allocation_statistics (code, length);
4315 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4317 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4319 memset (t, 0, sizeof (struct tree_common));
4321 TREE_SET_CODE (t, code);
4323 TREE_TYPE (t) = type;
4324 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4325 TREE_OPERAND (t, 0) = node;
4326 if (node && !TYPE_P (node))
4328 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4329 TREE_READONLY (t) = TREE_READONLY (node);
4332 if (TREE_CODE_CLASS (code) == tcc_statement)
4333 TREE_SIDE_EFFECTS (t) = 1;
4334 else switch (code)
4336 case VA_ARG_EXPR:
4337 /* All of these have side-effects, no matter what their
4338 operands are. */
4339 TREE_SIDE_EFFECTS (t) = 1;
4340 TREE_READONLY (t) = 0;
4341 break;
4343 case INDIRECT_REF:
4344 /* Whether a dereference is readonly has nothing to do with whether
4345 its operand is readonly. */
4346 TREE_READONLY (t) = 0;
4347 break;
4349 case ADDR_EXPR:
4350 if (node)
4351 recompute_tree_invariant_for_addr_expr (t);
4352 break;
4354 default:
4355 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4356 && node && !TYPE_P (node)
4357 && TREE_CONSTANT (node))
4358 TREE_CONSTANT (t) = 1;
4359 if (TREE_CODE_CLASS (code) == tcc_reference
4360 && node && TREE_THIS_VOLATILE (node))
4361 TREE_THIS_VOLATILE (t) = 1;
4362 break;
4365 return t;
4368 #define PROCESS_ARG(N) \
4369 do { \
4370 TREE_OPERAND (t, N) = arg##N; \
4371 if (arg##N &&!TYPE_P (arg##N)) \
4373 if (TREE_SIDE_EFFECTS (arg##N)) \
4374 side_effects = 1; \
4375 if (!TREE_READONLY (arg##N) \
4376 && !CONSTANT_CLASS_P (arg##N)) \
4377 (void) (read_only = 0); \
4378 if (!TREE_CONSTANT (arg##N)) \
4379 (void) (constant = 0); \
4381 } while (0)
4383 tree
4384 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
4386 bool constant, read_only, side_effects;
4387 tree t;
4389 gcc_assert (TREE_CODE_LENGTH (code) == 2);
4391 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
4392 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
4393 /* When sizetype precision doesn't match that of pointers
4394 we need to be able to build explicit extensions or truncations
4395 of the offset argument. */
4396 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
4397 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
4398 && TREE_CODE (arg1) == INTEGER_CST);
4400 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
4401 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
4402 && ptrofftype_p (TREE_TYPE (arg1)));
4404 t = make_node_stat (code PASS_MEM_STAT);
4405 TREE_TYPE (t) = tt;
4407 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
4408 result based on those same flags for the arguments. But if the
4409 arguments aren't really even `tree' expressions, we shouldn't be trying
4410 to do this. */
4412 /* Expressions without side effects may be constant if their
4413 arguments are as well. */
4414 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4415 || TREE_CODE_CLASS (code) == tcc_binary);
4416 read_only = 1;
4417 side_effects = TREE_SIDE_EFFECTS (t);
4419 PROCESS_ARG (0);
4420 PROCESS_ARG (1);
4422 TREE_SIDE_EFFECTS (t) = side_effects;
4423 if (code == MEM_REF)
4425 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
4427 tree o = TREE_OPERAND (arg0, 0);
4428 TREE_READONLY (t) = TREE_READONLY (o);
4429 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
4432 else
4434 TREE_READONLY (t) = read_only;
4435 TREE_CONSTANT (t) = constant;
4436 TREE_THIS_VOLATILE (t)
4437 = (TREE_CODE_CLASS (code) == tcc_reference
4438 && arg0 && TREE_THIS_VOLATILE (arg0));
4441 return t;
4445 tree
4446 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4447 tree arg2 MEM_STAT_DECL)
4449 bool constant, read_only, side_effects;
4450 tree t;
4452 gcc_assert (TREE_CODE_LENGTH (code) == 3);
4453 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4455 t = make_node_stat (code PASS_MEM_STAT);
4456 TREE_TYPE (t) = tt;
4458 read_only = 1;
4460 /* As a special exception, if COND_EXPR has NULL branches, we
4461 assume that it is a gimple statement and always consider
4462 it to have side effects. */
4463 if (code == COND_EXPR
4464 && tt == void_type_node
4465 && arg1 == NULL_TREE
4466 && arg2 == NULL_TREE)
4467 side_effects = true;
4468 else
4469 side_effects = TREE_SIDE_EFFECTS (t);
4471 PROCESS_ARG (0);
4472 PROCESS_ARG (1);
4473 PROCESS_ARG (2);
4475 if (code == COND_EXPR)
4476 TREE_READONLY (t) = read_only;
4478 TREE_SIDE_EFFECTS (t) = side_effects;
4479 TREE_THIS_VOLATILE (t)
4480 = (TREE_CODE_CLASS (code) == tcc_reference
4481 && arg0 && TREE_THIS_VOLATILE (arg0));
4483 return t;
4486 tree
4487 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4488 tree arg2, tree arg3 MEM_STAT_DECL)
4490 bool constant, read_only, side_effects;
4491 tree t;
4493 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4495 t = make_node_stat (code PASS_MEM_STAT);
4496 TREE_TYPE (t) = tt;
4498 side_effects = TREE_SIDE_EFFECTS (t);
4500 PROCESS_ARG (0);
4501 PROCESS_ARG (1);
4502 PROCESS_ARG (2);
4503 PROCESS_ARG (3);
4505 TREE_SIDE_EFFECTS (t) = side_effects;
4506 TREE_THIS_VOLATILE (t)
4507 = (TREE_CODE_CLASS (code) == tcc_reference
4508 && arg0 && TREE_THIS_VOLATILE (arg0));
4510 return t;
4513 tree
4514 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4515 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4517 bool constant, read_only, side_effects;
4518 tree t;
4520 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4522 t = make_node_stat (code PASS_MEM_STAT);
4523 TREE_TYPE (t) = tt;
4525 side_effects = TREE_SIDE_EFFECTS (t);
4527 PROCESS_ARG (0);
4528 PROCESS_ARG (1);
4529 PROCESS_ARG (2);
4530 PROCESS_ARG (3);
4531 PROCESS_ARG (4);
4533 TREE_SIDE_EFFECTS (t) = side_effects;
4534 if (code == TARGET_MEM_REF)
4536 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
4538 tree o = TREE_OPERAND (arg0, 0);
4539 TREE_READONLY (t) = TREE_READONLY (o);
4540 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
4543 else
4544 TREE_THIS_VOLATILE (t)
4545 = (TREE_CODE_CLASS (code) == tcc_reference
4546 && arg0 && TREE_THIS_VOLATILE (arg0));
4548 return t;
4551 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4552 on the pointer PTR. */
4554 tree
4555 build_simple_mem_ref_loc (location_t loc, tree ptr)
4557 HOST_WIDE_INT offset = 0;
4558 tree ptype = TREE_TYPE (ptr);
4559 tree tem;
4560 /* For convenience allow addresses that collapse to a simple base
4561 and offset. */
4562 if (TREE_CODE (ptr) == ADDR_EXPR
4563 && (handled_component_p (TREE_OPERAND (ptr, 0))
4564 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4566 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4567 gcc_assert (ptr);
4568 ptr = build_fold_addr_expr (ptr);
4569 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4571 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4572 ptr, build_int_cst (ptype, offset));
4573 SET_EXPR_LOCATION (tem, loc);
4574 return tem;
4577 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4579 offset_int
4580 mem_ref_offset (const_tree t)
4582 return offset_int::from (TREE_OPERAND (t, 1), SIGNED);
4585 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4586 offsetted by OFFSET units. */
4588 tree
4589 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4591 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4592 build_fold_addr_expr (base),
4593 build_int_cst (ptr_type_node, offset));
4594 tree addr = build1 (ADDR_EXPR, type, ref);
4595 recompute_tree_invariant_for_addr_expr (addr);
4596 return addr;
4599 /* Similar except don't specify the TREE_TYPE
4600 and leave the TREE_SIDE_EFFECTS as 0.
4601 It is permissible for arguments to be null,
4602 or even garbage if their values do not matter. */
4604 tree
4605 build_nt (enum tree_code code, ...)
4607 tree t;
4608 int length;
4609 int i;
4610 va_list p;
4612 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4614 va_start (p, code);
4616 t = make_node (code);
4617 length = TREE_CODE_LENGTH (code);
4619 for (i = 0; i < length; i++)
4620 TREE_OPERAND (t, i) = va_arg (p, tree);
4622 va_end (p);
4623 return t;
4626 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4627 tree vec. */
4629 tree
4630 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4632 tree ret, t;
4633 unsigned int ix;
4635 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4636 CALL_EXPR_FN (ret) = fn;
4637 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4638 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4639 CALL_EXPR_ARG (ret, ix) = t;
4640 return ret;
4643 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4644 We do NOT enter this node in any sort of symbol table.
4646 LOC is the location of the decl.
4648 layout_decl is used to set up the decl's storage layout.
4649 Other slots are initialized to 0 or null pointers. */
4651 tree
4652 build_decl_stat (location_t loc, enum tree_code code, tree name,
4653 tree type MEM_STAT_DECL)
4655 tree t;
4657 t = make_node_stat (code PASS_MEM_STAT);
4658 DECL_SOURCE_LOCATION (t) = loc;
4660 /* if (type == error_mark_node)
4661 type = integer_type_node; */
4662 /* That is not done, deliberately, so that having error_mark_node
4663 as the type can suppress useless errors in the use of this variable. */
4665 DECL_NAME (t) = name;
4666 TREE_TYPE (t) = type;
4668 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4669 layout_decl (t, 0);
4671 return t;
4674 /* Builds and returns function declaration with NAME and TYPE. */
4676 tree
4677 build_fn_decl (const char *name, tree type)
4679 tree id = get_identifier (name);
4680 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4682 DECL_EXTERNAL (decl) = 1;
4683 TREE_PUBLIC (decl) = 1;
4684 DECL_ARTIFICIAL (decl) = 1;
4685 TREE_NOTHROW (decl) = 1;
4687 return decl;
4690 vec<tree, va_gc> *all_translation_units;
4692 /* Builds a new translation-unit decl with name NAME, queues it in the
4693 global list of translation-unit decls and returns it. */
4695 tree
4696 build_translation_unit_decl (tree name)
4698 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4699 name, NULL_TREE);
4700 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4701 vec_safe_push (all_translation_units, tu);
4702 return tu;
4706 /* BLOCK nodes are used to represent the structure of binding contours
4707 and declarations, once those contours have been exited and their contents
4708 compiled. This information is used for outputting debugging info. */
4710 tree
4711 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4713 tree block = make_node (BLOCK);
4715 BLOCK_VARS (block) = vars;
4716 BLOCK_SUBBLOCKS (block) = subblocks;
4717 BLOCK_SUPERCONTEXT (block) = supercontext;
4718 BLOCK_CHAIN (block) = chain;
4719 return block;
4723 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4725 LOC is the location to use in tree T. */
4727 void
4728 protected_set_expr_location (tree t, location_t loc)
4730 if (CAN_HAVE_LOCATION_P (t))
4731 SET_EXPR_LOCATION (t, loc);
4734 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4735 is ATTRIBUTE. */
4737 tree
4738 build_decl_attribute_variant (tree ddecl, tree attribute)
4740 DECL_ATTRIBUTES (ddecl) = attribute;
4741 return ddecl;
4744 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4745 is ATTRIBUTE and its qualifiers are QUALS.
4747 Record such modified types already made so we don't make duplicates. */
4749 tree
4750 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4752 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4754 inchash::hash hstate;
4755 tree ntype;
4756 int i;
4757 tree t;
4758 enum tree_code code = TREE_CODE (ttype);
4760 /* Building a distinct copy of a tagged type is inappropriate; it
4761 causes breakage in code that expects there to be a one-to-one
4762 relationship between a struct and its fields.
4763 build_duplicate_type is another solution (as used in
4764 handle_transparent_union_attribute), but that doesn't play well
4765 with the stronger C++ type identity model. */
4766 if (TREE_CODE (ttype) == RECORD_TYPE
4767 || TREE_CODE (ttype) == UNION_TYPE
4768 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4769 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4771 warning (OPT_Wattributes,
4772 "ignoring attributes applied to %qT after definition",
4773 TYPE_MAIN_VARIANT (ttype));
4774 return build_qualified_type (ttype, quals);
4777 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4778 ntype = build_distinct_type_copy (ttype);
4780 TYPE_ATTRIBUTES (ntype) = attribute;
4782 hstate.add_int (code);
4783 if (TREE_TYPE (ntype))
4784 hstate.add_object (TYPE_HASH (TREE_TYPE (ntype)));
4785 attribute_hash_list (attribute, hstate);
4787 switch (TREE_CODE (ntype))
4789 case FUNCTION_TYPE:
4790 type_hash_list (TYPE_ARG_TYPES (ntype), hstate);
4791 break;
4792 case ARRAY_TYPE:
4793 if (TYPE_DOMAIN (ntype))
4794 hstate.add_object (TYPE_HASH (TYPE_DOMAIN (ntype)));
4795 break;
4796 case INTEGER_TYPE:
4797 t = TYPE_MAX_VALUE (ntype);
4798 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
4799 hstate.add_object (TREE_INT_CST_ELT (t, i));
4800 break;
4801 case REAL_TYPE:
4802 case FIXED_POINT_TYPE:
4804 unsigned int precision = TYPE_PRECISION (ntype);
4805 hstate.add_object (precision);
4807 break;
4808 default:
4809 break;
4812 ntype = type_hash_canon (hstate.end(), ntype);
4814 /* If the target-dependent attributes make NTYPE different from
4815 its canonical type, we will need to use structural equality
4816 checks for this type. */
4817 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4818 || !comp_type_attributes (ntype, ttype))
4819 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4820 else if (TYPE_CANONICAL (ntype) == ntype)
4821 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4823 ttype = build_qualified_type (ntype, quals);
4825 else if (TYPE_QUALS (ttype) != quals)
4826 ttype = build_qualified_type (ttype, quals);
4828 return ttype;
4831 /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
4832 the same. */
4834 static bool
4835 omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
4837 tree cl1, cl2;
4838 for (cl1 = clauses1, cl2 = clauses2;
4839 cl1 && cl2;
4840 cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
4842 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
4843 return false;
4844 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
4846 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
4847 OMP_CLAUSE_DECL (cl2)) != 1)
4848 return false;
4850 switch (OMP_CLAUSE_CODE (cl1))
4852 case OMP_CLAUSE_ALIGNED:
4853 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
4854 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
4855 return false;
4856 break;
4857 case OMP_CLAUSE_LINEAR:
4858 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
4859 OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
4860 return false;
4861 break;
4862 case OMP_CLAUSE_SIMDLEN:
4863 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
4864 OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
4865 return false;
4866 default:
4867 break;
4870 return true;
4873 /* Compare two constructor-element-type constants. Return 1 if the lists
4874 are known to be equal; otherwise return 0. */
4876 static bool
4877 simple_cst_list_equal (const_tree l1, const_tree l2)
4879 while (l1 != NULL_TREE && l2 != NULL_TREE)
4881 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
4882 return false;
4884 l1 = TREE_CHAIN (l1);
4885 l2 = TREE_CHAIN (l2);
4888 return l1 == l2;
4891 /* Compare two identifier nodes representing attributes. Either one may
4892 be in wrapped __ATTR__ form. Return true if they are the same, false
4893 otherwise. */
4895 static bool
4896 cmp_attrib_identifiers (const_tree attr1, const_tree attr2)
4898 /* Make sure we're dealing with IDENTIFIER_NODEs. */
4899 gcc_checking_assert (TREE_CODE (attr1) == IDENTIFIER_NODE
4900 && TREE_CODE (attr2) == IDENTIFIER_NODE);
4902 /* Identifiers can be compared directly for equality. */
4903 if (attr1 == attr2)
4904 return true;
4906 /* If they are not equal, they may still be one in the form
4907 'text' while the other one is in the form '__text__'. TODO:
4908 If we were storing attributes in normalized 'text' form, then
4909 this could all go away and we could take full advantage of
4910 the fact that we're comparing identifiers. :-) */
4911 const size_t attr1_len = IDENTIFIER_LENGTH (attr1);
4912 const size_t attr2_len = IDENTIFIER_LENGTH (attr2);
4914 if (attr2_len == attr1_len + 4)
4916 const char *p = IDENTIFIER_POINTER (attr2);
4917 const char *q = IDENTIFIER_POINTER (attr1);
4918 if (p[0] == '_' && p[1] == '_'
4919 && p[attr2_len - 2] == '_' && p[attr2_len - 1] == '_'
4920 && strncmp (q, p + 2, attr1_len) == 0)
4921 return true;;
4923 else if (attr2_len + 4 == attr1_len)
4925 const char *p = IDENTIFIER_POINTER (attr2);
4926 const char *q = IDENTIFIER_POINTER (attr1);
4927 if (q[0] == '_' && q[1] == '_'
4928 && q[attr1_len - 2] == '_' && q[attr1_len - 1] == '_'
4929 && strncmp (q + 2, p, attr2_len) == 0)
4930 return true;
4933 return false;
4936 /* Compare two attributes for their value identity. Return true if the
4937 attribute values are known to be equal; otherwise return false. */
4939 bool
4940 attribute_value_equal (const_tree attr1, const_tree attr2)
4942 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4943 return true;
4945 if (TREE_VALUE (attr1) != NULL_TREE
4946 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4947 && TREE_VALUE (attr2) != NULL_TREE
4948 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4950 /* Handle attribute format. */
4951 if (is_attribute_p ("format", TREE_PURPOSE (attr1)))
4953 attr1 = TREE_VALUE (attr1);
4954 attr2 = TREE_VALUE (attr2);
4955 /* Compare the archetypes (printf/scanf/strftime/...). */
4956 if (!cmp_attrib_identifiers (TREE_VALUE (attr1),
4957 TREE_VALUE (attr2)))
4958 return false;
4959 /* Archetypes are the same. Compare the rest. */
4960 return (simple_cst_list_equal (TREE_CHAIN (attr1),
4961 TREE_CHAIN (attr2)) == 1);
4963 return (simple_cst_list_equal (TREE_VALUE (attr1),
4964 TREE_VALUE (attr2)) == 1);
4967 if ((flag_openmp || flag_openmp_simd)
4968 && TREE_VALUE (attr1) && TREE_VALUE (attr2)
4969 && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
4970 && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
4971 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
4972 TREE_VALUE (attr2));
4974 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4977 /* Return 0 if the attributes for two types are incompatible, 1 if they
4978 are compatible, and 2 if they are nearly compatible (which causes a
4979 warning to be generated). */
4981 comp_type_attributes (const_tree type1, const_tree type2)
4983 const_tree a1 = TYPE_ATTRIBUTES (type1);
4984 const_tree a2 = TYPE_ATTRIBUTES (type2);
4985 const_tree a;
4987 if (a1 == a2)
4988 return 1;
4989 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4991 const struct attribute_spec *as;
4992 const_tree attr;
4994 as = lookup_attribute_spec (get_attribute_name (a));
4995 if (!as || as->affects_type_identity == false)
4996 continue;
4998 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4999 if (!attr || !attribute_value_equal (a, attr))
5000 break;
5002 if (!a)
5004 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
5006 const struct attribute_spec *as;
5008 as = lookup_attribute_spec (get_attribute_name (a));
5009 if (!as || as->affects_type_identity == false)
5010 continue;
5012 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
5013 break;
5014 /* We don't need to compare trees again, as we did this
5015 already in first loop. */
5017 /* All types - affecting identity - are equal, so
5018 there is no need to call target hook for comparison. */
5019 if (!a)
5020 return 1;
5022 /* As some type combinations - like default calling-convention - might
5023 be compatible, we have to call the target hook to get the final result. */
5024 return targetm.comp_type_attributes (type1, type2);
5027 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
5028 is ATTRIBUTE.
5030 Record such modified types already made so we don't make duplicates. */
5032 tree
5033 build_type_attribute_variant (tree ttype, tree attribute)
5035 return build_type_attribute_qual_variant (ttype, attribute,
5036 TYPE_QUALS (ttype));
5040 /* Reset the expression *EXPR_P, a size or position.
5042 ??? We could reset all non-constant sizes or positions. But it's cheap
5043 enough to not do so and refrain from adding workarounds to dwarf2out.c.
5045 We need to reset self-referential sizes or positions because they cannot
5046 be gimplified and thus can contain a CALL_EXPR after the gimplification
5047 is finished, which will run afoul of LTO streaming. And they need to be
5048 reset to something essentially dummy but not constant, so as to preserve
5049 the properties of the object they are attached to. */
5051 static inline void
5052 free_lang_data_in_one_sizepos (tree *expr_p)
5054 tree expr = *expr_p;
5055 if (CONTAINS_PLACEHOLDER_P (expr))
5056 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
5060 /* Reset all the fields in a binfo node BINFO. We only keep
5061 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
5063 static void
5064 free_lang_data_in_binfo (tree binfo)
5066 unsigned i;
5067 tree t;
5069 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
5071 BINFO_VIRTUALS (binfo) = NULL_TREE;
5072 BINFO_BASE_ACCESSES (binfo) = NULL;
5073 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
5074 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
5076 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
5077 free_lang_data_in_binfo (t);
5081 /* Reset all language specific information still present in TYPE. */
5083 static void
5084 free_lang_data_in_type (tree type)
5086 gcc_assert (TYPE_P (type));
5088 /* Give the FE a chance to remove its own data first. */
5089 lang_hooks.free_lang_data (type);
5091 TREE_LANG_FLAG_0 (type) = 0;
5092 TREE_LANG_FLAG_1 (type) = 0;
5093 TREE_LANG_FLAG_2 (type) = 0;
5094 TREE_LANG_FLAG_3 (type) = 0;
5095 TREE_LANG_FLAG_4 (type) = 0;
5096 TREE_LANG_FLAG_5 (type) = 0;
5097 TREE_LANG_FLAG_6 (type) = 0;
5099 if (TREE_CODE (type) == FUNCTION_TYPE)
5101 /* Remove the const and volatile qualifiers from arguments. The
5102 C++ front end removes them, but the C front end does not,
5103 leading to false ODR violation errors when merging two
5104 instances of the same function signature compiled by
5105 different front ends. */
5106 tree p;
5108 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
5110 tree arg_type = TREE_VALUE (p);
5112 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
5114 int quals = TYPE_QUALS (arg_type)
5115 & ~TYPE_QUAL_CONST
5116 & ~TYPE_QUAL_VOLATILE;
5117 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
5118 free_lang_data_in_type (TREE_VALUE (p));
5120 /* C++ FE uses TREE_PURPOSE to store initial values. */
5121 TREE_PURPOSE (p) = NULL;
5123 /* Java uses TYPE_MINVAL for TYPE_ARGUMENT_SIGNATURE. */
5124 TYPE_MINVAL (type) = NULL;
5126 if (TREE_CODE (type) == METHOD_TYPE)
5128 tree p;
5130 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
5132 /* C++ FE uses TREE_PURPOSE to store initial values. */
5133 TREE_PURPOSE (p) = NULL;
5135 /* Java uses TYPE_MINVAL for TYPE_ARGUMENT_SIGNATURE. */
5136 TYPE_MINVAL (type) = NULL;
5139 /* Remove members that are not actually FIELD_DECLs from the field
5140 list of an aggregate. These occur in C++. */
5141 if (RECORD_OR_UNION_TYPE_P (type))
5143 tree prev, member;
5145 /* Note that TYPE_FIELDS can be shared across distinct
5146 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
5147 to be removed, we cannot set its TREE_CHAIN to NULL.
5148 Otherwise, we would not be able to find all the other fields
5149 in the other instances of this TREE_TYPE.
5151 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
5152 prev = NULL_TREE;
5153 member = TYPE_FIELDS (type);
5154 while (member)
5156 if (TREE_CODE (member) == FIELD_DECL
5157 || TREE_CODE (member) == TYPE_DECL)
5159 if (prev)
5160 TREE_CHAIN (prev) = member;
5161 else
5162 TYPE_FIELDS (type) = member;
5163 prev = member;
5166 member = TREE_CHAIN (member);
5169 if (prev)
5170 TREE_CHAIN (prev) = NULL_TREE;
5171 else
5172 TYPE_FIELDS (type) = NULL_TREE;
5174 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
5175 and danagle the pointer from time to time. */
5176 if (TYPE_VFIELD (type) && TREE_CODE (TYPE_VFIELD (type)) != FIELD_DECL)
5177 TYPE_VFIELD (type) = NULL_TREE;
5179 /* Remove TYPE_METHODS list. While it would be nice to keep it
5180 to enable ODR warnings about different method lists, doing so
5181 seems to impractically increase size of LTO data streamed.
5182 Keep the infrmation if TYPE_METHODS was non-NULL. This is used
5183 by function.c and pretty printers. */
5184 if (TYPE_METHODS (type))
5185 TYPE_METHODS (type) = error_mark_node;
5186 if (TYPE_BINFO (type))
5188 free_lang_data_in_binfo (TYPE_BINFO (type));
5189 /* We need to preserve link to bases and virtual table for all
5190 polymorphic types to make devirtualization machinery working.
5191 Debug output cares only about bases, but output also
5192 virtual table pointers so merging of -fdevirtualize and
5193 -fno-devirtualize units is easier. */
5194 if ((!BINFO_VTABLE (TYPE_BINFO (type))
5195 || !flag_devirtualize)
5196 && ((!BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
5197 && !BINFO_VTABLE (TYPE_BINFO (type)))
5198 || debug_info_level != DINFO_LEVEL_NONE))
5199 TYPE_BINFO (type) = NULL;
5202 else
5204 /* For non-aggregate types, clear out the language slot (which
5205 overloads TYPE_BINFO). */
5206 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
5208 if (INTEGRAL_TYPE_P (type)
5209 || SCALAR_FLOAT_TYPE_P (type)
5210 || FIXED_POINT_TYPE_P (type))
5212 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
5213 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
5217 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
5218 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
5220 if (TYPE_CONTEXT (type)
5221 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
5223 tree ctx = TYPE_CONTEXT (type);
5226 ctx = BLOCK_SUPERCONTEXT (ctx);
5228 while (ctx && TREE_CODE (ctx) == BLOCK);
5229 TYPE_CONTEXT (type) = ctx;
5234 /* Return true if DECL may need an assembler name to be set. */
5236 static inline bool
5237 need_assembler_name_p (tree decl)
5239 /* We use DECL_ASSEMBLER_NAME to hold mangled type names for One Definition
5240 Rule merging. This makes type_odr_p to return true on those types during
5241 LTO and by comparing the mangled name, we can say what types are intended
5242 to be equivalent across compilation unit.
5244 We do not store names of type_in_anonymous_namespace_p.
5246 Record, union and enumeration type have linkage that allows use
5247 to check type_in_anonymous_namespace_p. We do not mangle compound types
5248 that always can be compared structurally.
5250 Similarly for builtin types, we compare properties of their main variant.
5251 A special case are integer types where mangling do make differences
5252 between char/signed char/unsigned char etc. Storing name for these makes
5253 e.g. -fno-signed-char/-fsigned-char mismatches to be handled well.
5254 See cp/mangle.c:write_builtin_type for details. */
5256 if (flag_lto_odr_type_mering
5257 && TREE_CODE (decl) == TYPE_DECL
5258 && DECL_NAME (decl)
5259 && decl == TYPE_NAME (TREE_TYPE (decl))
5260 && !TYPE_ARTIFICIAL (TREE_TYPE (decl))
5261 && (type_with_linkage_p (TREE_TYPE (decl))
5262 || TREE_CODE (TREE_TYPE (decl)) == INTEGER_TYPE)
5263 && !variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5264 return !DECL_ASSEMBLER_NAME_SET_P (decl);
5265 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
5266 if (TREE_CODE (decl) != FUNCTION_DECL
5267 && TREE_CODE (decl) != VAR_DECL)
5268 return false;
5270 /* If DECL already has its assembler name set, it does not need a
5271 new one. */
5272 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
5273 || DECL_ASSEMBLER_NAME_SET_P (decl))
5274 return false;
5276 /* Abstract decls do not need an assembler name. */
5277 if (DECL_ABSTRACT_P (decl))
5278 return false;
5280 /* For VAR_DECLs, only static, public and external symbols need an
5281 assembler name. */
5282 if (TREE_CODE (decl) == VAR_DECL
5283 && !TREE_STATIC (decl)
5284 && !TREE_PUBLIC (decl)
5285 && !DECL_EXTERNAL (decl))
5286 return false;
5288 if (TREE_CODE (decl) == FUNCTION_DECL)
5290 /* Do not set assembler name on builtins. Allow RTL expansion to
5291 decide whether to expand inline or via a regular call. */
5292 if (DECL_BUILT_IN (decl)
5293 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
5294 return false;
5296 /* Functions represented in the callgraph need an assembler name. */
5297 if (cgraph_node::get (decl) != NULL)
5298 return true;
5300 /* Unused and not public functions don't need an assembler name. */
5301 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
5302 return false;
5305 return true;
5309 /* Reset all language specific information still present in symbol
5310 DECL. */
5312 static void
5313 free_lang_data_in_decl (tree decl)
5315 gcc_assert (DECL_P (decl));
5317 /* Give the FE a chance to remove its own data first. */
5318 lang_hooks.free_lang_data (decl);
5320 TREE_LANG_FLAG_0 (decl) = 0;
5321 TREE_LANG_FLAG_1 (decl) = 0;
5322 TREE_LANG_FLAG_2 (decl) = 0;
5323 TREE_LANG_FLAG_3 (decl) = 0;
5324 TREE_LANG_FLAG_4 (decl) = 0;
5325 TREE_LANG_FLAG_5 (decl) = 0;
5326 TREE_LANG_FLAG_6 (decl) = 0;
5328 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
5329 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
5330 if (TREE_CODE (decl) == FIELD_DECL)
5332 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
5333 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
5334 DECL_QUALIFIER (decl) = NULL_TREE;
5337 if (TREE_CODE (decl) == FUNCTION_DECL)
5339 struct cgraph_node *node;
5340 if (!(node = cgraph_node::get (decl))
5341 || (!node->definition && !node->clones))
5343 if (node)
5344 node->release_body ();
5345 else
5347 release_function_body (decl);
5348 DECL_ARGUMENTS (decl) = NULL;
5349 DECL_RESULT (decl) = NULL;
5350 DECL_INITIAL (decl) = error_mark_node;
5353 if (gimple_has_body_p (decl))
5355 tree t;
5357 /* If DECL has a gimple body, then the context for its
5358 arguments must be DECL. Otherwise, it doesn't really
5359 matter, as we will not be emitting any code for DECL. In
5360 general, there may be other instances of DECL created by
5361 the front end and since PARM_DECLs are generally shared,
5362 their DECL_CONTEXT changes as the replicas of DECL are
5363 created. The only time where DECL_CONTEXT is important
5364 is for the FUNCTION_DECLs that have a gimple body (since
5365 the PARM_DECL will be used in the function's body). */
5366 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
5367 DECL_CONTEXT (t) = decl;
5368 if (!DECL_FUNCTION_SPECIFIC_TARGET (decl))
5369 DECL_FUNCTION_SPECIFIC_TARGET (decl)
5370 = target_option_default_node;
5371 if (!DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl))
5372 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl)
5373 = optimization_default_node;
5376 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
5377 At this point, it is not needed anymore. */
5378 DECL_SAVED_TREE (decl) = NULL_TREE;
5380 /* Clear the abstract origin if it refers to a method. Otherwise
5381 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
5382 origin will not be output correctly. */
5383 if (DECL_ABSTRACT_ORIGIN (decl)
5384 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
5385 && RECORD_OR_UNION_TYPE_P
5386 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
5387 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
5389 /* Sometimes the C++ frontend doesn't manage to transform a temporary
5390 DECL_VINDEX referring to itself into a vtable slot number as it
5391 should. Happens with functions that are copied and then forgotten
5392 about. Just clear it, it won't matter anymore. */
5393 if (DECL_VINDEX (decl) && !tree_fits_shwi_p (DECL_VINDEX (decl)))
5394 DECL_VINDEX (decl) = NULL_TREE;
5396 else if (TREE_CODE (decl) == VAR_DECL)
5398 if ((DECL_EXTERNAL (decl)
5399 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
5400 || (decl_function_context (decl) && !TREE_STATIC (decl)))
5401 DECL_INITIAL (decl) = NULL_TREE;
5403 else if (TREE_CODE (decl) == TYPE_DECL
5404 || TREE_CODE (decl) == FIELD_DECL)
5405 DECL_INITIAL (decl) = NULL_TREE;
5406 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
5407 && DECL_INITIAL (decl)
5408 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
5410 /* Strip builtins from the translation-unit BLOCK. We still have targets
5411 without builtin_decl_explicit support and also builtins are shared
5412 nodes and thus we can't use TREE_CHAIN in multiple lists. */
5413 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
5414 while (*nextp)
5416 tree var = *nextp;
5417 if (TREE_CODE (var) == FUNCTION_DECL
5418 && DECL_BUILT_IN (var))
5419 *nextp = TREE_CHAIN (var);
5420 else
5421 nextp = &TREE_CHAIN (var);
5427 /* Data used when collecting DECLs and TYPEs for language data removal. */
5429 struct free_lang_data_d
5431 /* Worklist to avoid excessive recursion. */
5432 vec<tree> worklist;
5434 /* Set of traversed objects. Used to avoid duplicate visits. */
5435 hash_set<tree> *pset;
5437 /* Array of symbols to process with free_lang_data_in_decl. */
5438 vec<tree> decls;
5440 /* Array of types to process with free_lang_data_in_type. */
5441 vec<tree> types;
5445 /* Save all language fields needed to generate proper debug information
5446 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
5448 static void
5449 save_debug_info_for_decl (tree t)
5451 /*struct saved_debug_info_d *sdi;*/
5453 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
5455 /* FIXME. Partial implementation for saving debug info removed. */
5459 /* Save all language fields needed to generate proper debug information
5460 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
5462 static void
5463 save_debug_info_for_type (tree t)
5465 /*struct saved_debug_info_d *sdi;*/
5467 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
5469 /* FIXME. Partial implementation for saving debug info removed. */
5473 /* Add type or decl T to one of the list of tree nodes that need their
5474 language data removed. The lists are held inside FLD. */
5476 static void
5477 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
5479 if (DECL_P (t))
5481 fld->decls.safe_push (t);
5482 if (debug_info_level > DINFO_LEVEL_TERSE)
5483 save_debug_info_for_decl (t);
5485 else if (TYPE_P (t))
5487 fld->types.safe_push (t);
5488 if (debug_info_level > DINFO_LEVEL_TERSE)
5489 save_debug_info_for_type (t);
5491 else
5492 gcc_unreachable ();
5495 /* Push tree node T into FLD->WORKLIST. */
5497 static inline void
5498 fld_worklist_push (tree t, struct free_lang_data_d *fld)
5500 if (t && !is_lang_specific (t) && !fld->pset->contains (t))
5501 fld->worklist.safe_push ((t));
5505 /* Operand callback helper for free_lang_data_in_node. *TP is the
5506 subtree operand being considered. */
5508 static tree
5509 find_decls_types_r (tree *tp, int *ws, void *data)
5511 tree t = *tp;
5512 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
5514 if (TREE_CODE (t) == TREE_LIST)
5515 return NULL_TREE;
5517 /* Language specific nodes will be removed, so there is no need
5518 to gather anything under them. */
5519 if (is_lang_specific (t))
5521 *ws = 0;
5522 return NULL_TREE;
5525 if (DECL_P (t))
5527 /* Note that walk_tree does not traverse every possible field in
5528 decls, so we have to do our own traversals here. */
5529 add_tree_to_fld_list (t, fld);
5531 fld_worklist_push (DECL_NAME (t), fld);
5532 fld_worklist_push (DECL_CONTEXT (t), fld);
5533 fld_worklist_push (DECL_SIZE (t), fld);
5534 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
5536 /* We are going to remove everything under DECL_INITIAL for
5537 TYPE_DECLs. No point walking them. */
5538 if (TREE_CODE (t) != TYPE_DECL)
5539 fld_worklist_push (DECL_INITIAL (t), fld);
5541 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
5542 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
5544 if (TREE_CODE (t) == FUNCTION_DECL)
5546 fld_worklist_push (DECL_ARGUMENTS (t), fld);
5547 fld_worklist_push (DECL_RESULT (t), fld);
5549 else if (TREE_CODE (t) == TYPE_DECL)
5551 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
5553 else if (TREE_CODE (t) == FIELD_DECL)
5555 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
5556 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
5557 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
5558 fld_worklist_push (DECL_FCONTEXT (t), fld);
5561 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
5562 && DECL_HAS_VALUE_EXPR_P (t))
5563 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
5565 if (TREE_CODE (t) != FIELD_DECL
5566 && TREE_CODE (t) != TYPE_DECL)
5567 fld_worklist_push (TREE_CHAIN (t), fld);
5568 *ws = 0;
5570 else if (TYPE_P (t))
5572 /* Note that walk_tree does not traverse every possible field in
5573 types, so we have to do our own traversals here. */
5574 add_tree_to_fld_list (t, fld);
5576 if (!RECORD_OR_UNION_TYPE_P (t))
5577 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
5578 fld_worklist_push (TYPE_SIZE (t), fld);
5579 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
5580 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
5581 fld_worklist_push (TYPE_POINTER_TO (t), fld);
5582 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
5583 fld_worklist_push (TYPE_NAME (t), fld);
5584 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
5585 them and thus do not and want not to reach unused pointer types
5586 this way. */
5587 if (!POINTER_TYPE_P (t))
5588 fld_worklist_push (TYPE_MINVAL (t), fld);
5589 if (!RECORD_OR_UNION_TYPE_P (t))
5590 fld_worklist_push (TYPE_MAXVAL (t), fld);
5591 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
5592 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
5593 do not and want not to reach unused variants this way. */
5594 if (TYPE_CONTEXT (t))
5596 tree ctx = TYPE_CONTEXT (t);
5597 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
5598 So push that instead. */
5599 while (ctx && TREE_CODE (ctx) == BLOCK)
5600 ctx = BLOCK_SUPERCONTEXT (ctx);
5601 fld_worklist_push (ctx, fld);
5603 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
5604 and want not to reach unused types this way. */
5606 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
5608 unsigned i;
5609 tree tem;
5610 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
5611 fld_worklist_push (TREE_TYPE (tem), fld);
5612 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
5613 if (tem
5614 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
5615 && TREE_CODE (tem) == TREE_LIST)
5618 fld_worklist_push (TREE_VALUE (tem), fld);
5619 tem = TREE_CHAIN (tem);
5621 while (tem);
5623 if (RECORD_OR_UNION_TYPE_P (t))
5625 tree tem;
5626 /* Push all TYPE_FIELDS - there can be interleaving interesting
5627 and non-interesting things. */
5628 tem = TYPE_FIELDS (t);
5629 while (tem)
5631 if (TREE_CODE (tem) == FIELD_DECL
5632 || TREE_CODE (tem) == TYPE_DECL)
5633 fld_worklist_push (tem, fld);
5634 tem = TREE_CHAIN (tem);
5638 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5639 *ws = 0;
5641 else if (TREE_CODE (t) == BLOCK)
5643 tree tem;
5644 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5645 fld_worklist_push (tem, fld);
5646 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5647 fld_worklist_push (tem, fld);
5648 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5651 if (TREE_CODE (t) != IDENTIFIER_NODE
5652 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5653 fld_worklist_push (TREE_TYPE (t), fld);
5655 return NULL_TREE;
5659 /* Find decls and types in T. */
5661 static void
5662 find_decls_types (tree t, struct free_lang_data_d *fld)
5664 while (1)
5666 if (!fld->pset->contains (t))
5667 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5668 if (fld->worklist.is_empty ())
5669 break;
5670 t = fld->worklist.pop ();
5674 /* Translate all the types in LIST with the corresponding runtime
5675 types. */
5677 static tree
5678 get_eh_types_for_runtime (tree list)
5680 tree head, prev;
5682 if (list == NULL_TREE)
5683 return NULL_TREE;
5685 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5686 prev = head;
5687 list = TREE_CHAIN (list);
5688 while (list)
5690 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5691 TREE_CHAIN (prev) = n;
5692 prev = TREE_CHAIN (prev);
5693 list = TREE_CHAIN (list);
5696 return head;
5700 /* Find decls and types referenced in EH region R and store them in
5701 FLD->DECLS and FLD->TYPES. */
5703 static void
5704 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5706 switch (r->type)
5708 case ERT_CLEANUP:
5709 break;
5711 case ERT_TRY:
5713 eh_catch c;
5715 /* The types referenced in each catch must first be changed to the
5716 EH types used at runtime. This removes references to FE types
5717 in the region. */
5718 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5720 c->type_list = get_eh_types_for_runtime (c->type_list);
5721 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5724 break;
5726 case ERT_ALLOWED_EXCEPTIONS:
5727 r->u.allowed.type_list
5728 = get_eh_types_for_runtime (r->u.allowed.type_list);
5729 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5730 break;
5732 case ERT_MUST_NOT_THROW:
5733 walk_tree (&r->u.must_not_throw.failure_decl,
5734 find_decls_types_r, fld, fld->pset);
5735 break;
5740 /* Find decls and types referenced in cgraph node N and store them in
5741 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5742 look for *every* kind of DECL and TYPE node reachable from N,
5743 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5744 NAMESPACE_DECLs, etc). */
5746 static void
5747 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5749 basic_block bb;
5750 struct function *fn;
5751 unsigned ix;
5752 tree t;
5754 find_decls_types (n->decl, fld);
5756 if (!gimple_has_body_p (n->decl))
5757 return;
5759 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5761 fn = DECL_STRUCT_FUNCTION (n->decl);
5763 /* Traverse locals. */
5764 FOR_EACH_LOCAL_DECL (fn, ix, t)
5765 find_decls_types (t, fld);
5767 /* Traverse EH regions in FN. */
5769 eh_region r;
5770 FOR_ALL_EH_REGION_FN (r, fn)
5771 find_decls_types_in_eh_region (r, fld);
5774 /* Traverse every statement in FN. */
5775 FOR_EACH_BB_FN (bb, fn)
5777 gphi_iterator psi;
5778 gimple_stmt_iterator si;
5779 unsigned i;
5781 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
5783 gphi *phi = psi.phi ();
5785 for (i = 0; i < gimple_phi_num_args (phi); i++)
5787 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5788 find_decls_types (*arg_p, fld);
5792 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5794 gimple stmt = gsi_stmt (si);
5796 if (is_gimple_call (stmt))
5797 find_decls_types (gimple_call_fntype (stmt), fld);
5799 for (i = 0; i < gimple_num_ops (stmt); i++)
5801 tree arg = gimple_op (stmt, i);
5802 find_decls_types (arg, fld);
5809 /* Find decls and types referenced in varpool node N and store them in
5810 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5811 look for *every* kind of DECL and TYPE node reachable from N,
5812 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5813 NAMESPACE_DECLs, etc). */
5815 static void
5816 find_decls_types_in_var (varpool_node *v, struct free_lang_data_d *fld)
5818 find_decls_types (v->decl, fld);
5821 /* If T needs an assembler name, have one created for it. */
5823 void
5824 assign_assembler_name_if_neeeded (tree t)
5826 if (need_assembler_name_p (t))
5828 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5829 diagnostics that use input_location to show locus
5830 information. The problem here is that, at this point,
5831 input_location is generally anchored to the end of the file
5832 (since the parser is long gone), so we don't have a good
5833 position to pin it to.
5835 To alleviate this problem, this uses the location of T's
5836 declaration. Examples of this are
5837 testsuite/g++.dg/template/cond2.C and
5838 testsuite/g++.dg/template/pr35240.C. */
5839 location_t saved_location = input_location;
5840 input_location = DECL_SOURCE_LOCATION (t);
5842 decl_assembler_name (t);
5844 input_location = saved_location;
5849 /* Free language specific information for every operand and expression
5850 in every node of the call graph. This process operates in three stages:
5852 1- Every callgraph node and varpool node is traversed looking for
5853 decls and types embedded in them. This is a more exhaustive
5854 search than that done by find_referenced_vars, because it will
5855 also collect individual fields, decls embedded in types, etc.
5857 2- All the decls found are sent to free_lang_data_in_decl.
5859 3- All the types found are sent to free_lang_data_in_type.
5861 The ordering between decls and types is important because
5862 free_lang_data_in_decl sets assembler names, which includes
5863 mangling. So types cannot be freed up until assembler names have
5864 been set up. */
5866 static void
5867 free_lang_data_in_cgraph (void)
5869 struct cgraph_node *n;
5870 varpool_node *v;
5871 struct free_lang_data_d fld;
5872 tree t;
5873 unsigned i;
5874 alias_pair *p;
5876 /* Initialize sets and arrays to store referenced decls and types. */
5877 fld.pset = new hash_set<tree>;
5878 fld.worklist.create (0);
5879 fld.decls.create (100);
5880 fld.types.create (100);
5882 /* Find decls and types in the body of every function in the callgraph. */
5883 FOR_EACH_FUNCTION (n)
5884 find_decls_types_in_node (n, &fld);
5886 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5887 find_decls_types (p->decl, &fld);
5889 /* Find decls and types in every varpool symbol. */
5890 FOR_EACH_VARIABLE (v)
5891 find_decls_types_in_var (v, &fld);
5893 /* Set the assembler name on every decl found. We need to do this
5894 now because free_lang_data_in_decl will invalidate data needed
5895 for mangling. This breaks mangling on interdependent decls. */
5896 FOR_EACH_VEC_ELT (fld.decls, i, t)
5897 assign_assembler_name_if_neeeded (t);
5899 /* Traverse every decl found freeing its language data. */
5900 FOR_EACH_VEC_ELT (fld.decls, i, t)
5901 free_lang_data_in_decl (t);
5903 /* Traverse every type found freeing its language data. */
5904 FOR_EACH_VEC_ELT (fld.types, i, t)
5905 free_lang_data_in_type (t);
5906 #ifdef ENABLE_CHECKING
5907 FOR_EACH_VEC_ELT (fld.types, i, t)
5908 verify_type (t);
5909 #endif
5911 delete fld.pset;
5912 fld.worklist.release ();
5913 fld.decls.release ();
5914 fld.types.release ();
5918 /* Free resources that are used by FE but are not needed once they are done. */
5920 static unsigned
5921 free_lang_data (void)
5923 unsigned i;
5925 /* If we are the LTO frontend we have freed lang-specific data already. */
5926 if (in_lto_p
5927 || (!flag_generate_lto && !flag_generate_offload))
5928 return 0;
5930 /* Allocate and assign alias sets to the standard integer types
5931 while the slots are still in the way the frontends generated them. */
5932 for (i = 0; i < itk_none; ++i)
5933 if (integer_types[i])
5934 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5936 /* Traverse the IL resetting language specific information for
5937 operands, expressions, etc. */
5938 free_lang_data_in_cgraph ();
5940 /* Create gimple variants for common types. */
5941 ptrdiff_type_node = integer_type_node;
5942 fileptr_type_node = ptr_type_node;
5944 /* Reset some langhooks. Do not reset types_compatible_p, it may
5945 still be used indirectly via the get_alias_set langhook. */
5946 lang_hooks.dwarf_name = lhd_dwarf_name;
5947 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5948 lang_hooks.gimplify_expr = lhd_gimplify_expr;
5950 /* We do not want the default decl_assembler_name implementation,
5951 rather if we have fixed everything we want a wrapper around it
5952 asserting that all non-local symbols already got their assembler
5953 name and only produce assembler names for local symbols. Or rather
5954 make sure we never call decl_assembler_name on local symbols and
5955 devise a separate, middle-end private scheme for it. */
5957 /* Reset diagnostic machinery. */
5958 tree_diagnostics_defaults (global_dc);
5960 return 0;
5964 namespace {
5966 const pass_data pass_data_ipa_free_lang_data =
5968 SIMPLE_IPA_PASS, /* type */
5969 "*free_lang_data", /* name */
5970 OPTGROUP_NONE, /* optinfo_flags */
5971 TV_IPA_FREE_LANG_DATA, /* tv_id */
5972 0, /* properties_required */
5973 0, /* properties_provided */
5974 0, /* properties_destroyed */
5975 0, /* todo_flags_start */
5976 0, /* todo_flags_finish */
5979 class pass_ipa_free_lang_data : public simple_ipa_opt_pass
5981 public:
5982 pass_ipa_free_lang_data (gcc::context *ctxt)
5983 : simple_ipa_opt_pass (pass_data_ipa_free_lang_data, ctxt)
5986 /* opt_pass methods: */
5987 virtual unsigned int execute (function *) { return free_lang_data (); }
5989 }; // class pass_ipa_free_lang_data
5991 } // anon namespace
5993 simple_ipa_opt_pass *
5994 make_pass_ipa_free_lang_data (gcc::context *ctxt)
5996 return new pass_ipa_free_lang_data (ctxt);
5999 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
6000 ATTR_NAME. Also used internally by remove_attribute(). */
6001 bool
6002 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
6004 size_t ident_len = IDENTIFIER_LENGTH (ident);
6006 if (ident_len == attr_len)
6008 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
6009 return true;
6011 else if (ident_len == attr_len + 4)
6013 /* There is the possibility that ATTR is 'text' and IDENT is
6014 '__text__'. */
6015 const char *p = IDENTIFIER_POINTER (ident);
6016 if (p[0] == '_' && p[1] == '_'
6017 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
6018 && strncmp (attr_name, p + 2, attr_len) == 0)
6019 return true;
6022 return false;
6025 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
6026 of ATTR_NAME, and LIST is not NULL_TREE. */
6027 tree
6028 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
6030 while (list)
6032 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
6034 if (ident_len == attr_len)
6036 if (!strcmp (attr_name,
6037 IDENTIFIER_POINTER (get_attribute_name (list))))
6038 break;
6040 /* TODO: If we made sure that attributes were stored in the
6041 canonical form without '__...__' (ie, as in 'text' as opposed
6042 to '__text__') then we could avoid the following case. */
6043 else if (ident_len == attr_len + 4)
6045 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
6046 if (p[0] == '_' && p[1] == '_'
6047 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
6048 && strncmp (attr_name, p + 2, attr_len) == 0)
6049 break;
6051 list = TREE_CHAIN (list);
6054 return list;
6057 /* Given an attribute name ATTR_NAME and a list of attributes LIST,
6058 return a pointer to the attribute's list first element if the attribute
6059 starts with ATTR_NAME. ATTR_NAME must be in the form 'text' (not
6060 '__text__'). */
6062 tree
6063 private_lookup_attribute_by_prefix (const char *attr_name, size_t attr_len,
6064 tree list)
6066 while (list)
6068 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
6070 if (attr_len > ident_len)
6072 list = TREE_CHAIN (list);
6073 continue;
6076 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
6078 if (strncmp (attr_name, p, attr_len) == 0)
6079 break;
6081 /* TODO: If we made sure that attributes were stored in the
6082 canonical form without '__...__' (ie, as in 'text' as opposed
6083 to '__text__') then we could avoid the following case. */
6084 if (p[0] == '_' && p[1] == '_' &&
6085 strncmp (attr_name, p + 2, attr_len) == 0)
6086 break;
6088 list = TREE_CHAIN (list);
6091 return list;
6095 /* A variant of lookup_attribute() that can be used with an identifier
6096 as the first argument, and where the identifier can be either
6097 'text' or '__text__'.
6099 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
6100 return a pointer to the attribute's list element if the attribute
6101 is part of the list, or NULL_TREE if not found. If the attribute
6102 appears more than once, this only returns the first occurrence; the
6103 TREE_CHAIN of the return value should be passed back in if further
6104 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
6105 can be in the form 'text' or '__text__'. */
6106 static tree
6107 lookup_ident_attribute (tree attr_identifier, tree list)
6109 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
6111 while (list)
6113 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
6114 == IDENTIFIER_NODE);
6116 if (cmp_attrib_identifiers (attr_identifier,
6117 get_attribute_name (list)))
6118 /* Found it. */
6119 break;
6120 list = TREE_CHAIN (list);
6123 return list;
6126 /* Remove any instances of attribute ATTR_NAME in LIST and return the
6127 modified list. */
6129 tree
6130 remove_attribute (const char *attr_name, tree list)
6132 tree *p;
6133 size_t attr_len = strlen (attr_name);
6135 gcc_checking_assert (attr_name[0] != '_');
6137 for (p = &list; *p; )
6139 tree l = *p;
6140 /* TODO: If we were storing attributes in normalized form, here
6141 we could use a simple strcmp(). */
6142 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
6143 *p = TREE_CHAIN (l);
6144 else
6145 p = &TREE_CHAIN (l);
6148 return list;
6151 /* Return an attribute list that is the union of a1 and a2. */
6153 tree
6154 merge_attributes (tree a1, tree a2)
6156 tree attributes;
6158 /* Either one unset? Take the set one. */
6160 if ((attributes = a1) == 0)
6161 attributes = a2;
6163 /* One that completely contains the other? Take it. */
6165 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
6167 if (attribute_list_contained (a2, a1))
6168 attributes = a2;
6169 else
6171 /* Pick the longest list, and hang on the other list. */
6173 if (list_length (a1) < list_length (a2))
6174 attributes = a2, a2 = a1;
6176 for (; a2 != 0; a2 = TREE_CHAIN (a2))
6178 tree a;
6179 for (a = lookup_ident_attribute (get_attribute_name (a2),
6180 attributes);
6181 a != NULL_TREE && !attribute_value_equal (a, a2);
6182 a = lookup_ident_attribute (get_attribute_name (a2),
6183 TREE_CHAIN (a)))
6185 if (a == NULL_TREE)
6187 a1 = copy_node (a2);
6188 TREE_CHAIN (a1) = attributes;
6189 attributes = a1;
6194 return attributes;
6197 /* Given types T1 and T2, merge their attributes and return
6198 the result. */
6200 tree
6201 merge_type_attributes (tree t1, tree t2)
6203 return merge_attributes (TYPE_ATTRIBUTES (t1),
6204 TYPE_ATTRIBUTES (t2));
6207 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
6208 the result. */
6210 tree
6211 merge_decl_attributes (tree olddecl, tree newdecl)
6213 return merge_attributes (DECL_ATTRIBUTES (olddecl),
6214 DECL_ATTRIBUTES (newdecl));
6217 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
6219 /* Specialization of merge_decl_attributes for various Windows targets.
6221 This handles the following situation:
6223 __declspec (dllimport) int foo;
6224 int foo;
6226 The second instance of `foo' nullifies the dllimport. */
6228 tree
6229 merge_dllimport_decl_attributes (tree old, tree new_tree)
6231 tree a;
6232 int delete_dllimport_p = 1;
6234 /* What we need to do here is remove from `old' dllimport if it doesn't
6235 appear in `new'. dllimport behaves like extern: if a declaration is
6236 marked dllimport and a definition appears later, then the object
6237 is not dllimport'd. We also remove a `new' dllimport if the old list
6238 contains dllexport: dllexport always overrides dllimport, regardless
6239 of the order of declaration. */
6240 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
6241 delete_dllimport_p = 0;
6242 else if (DECL_DLLIMPORT_P (new_tree)
6243 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
6245 DECL_DLLIMPORT_P (new_tree) = 0;
6246 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
6247 "dllimport ignored", new_tree);
6249 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
6251 /* Warn about overriding a symbol that has already been used, e.g.:
6252 extern int __attribute__ ((dllimport)) foo;
6253 int* bar () {return &foo;}
6254 int foo;
6256 if (TREE_USED (old))
6258 warning (0, "%q+D redeclared without dllimport attribute "
6259 "after being referenced with dll linkage", new_tree);
6260 /* If we have used a variable's address with dllimport linkage,
6261 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
6262 decl may already have had TREE_CONSTANT computed.
6263 We still remove the attribute so that assembler code refers
6264 to '&foo rather than '_imp__foo'. */
6265 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
6266 DECL_DLLIMPORT_P (new_tree) = 1;
6269 /* Let an inline definition silently override the external reference,
6270 but otherwise warn about attribute inconsistency. */
6271 else if (TREE_CODE (new_tree) == VAR_DECL
6272 || !DECL_DECLARED_INLINE_P (new_tree))
6273 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
6274 "previous dllimport ignored", new_tree);
6276 else
6277 delete_dllimport_p = 0;
6279 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
6281 if (delete_dllimport_p)
6282 a = remove_attribute ("dllimport", a);
6284 return a;
6287 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
6288 struct attribute_spec.handler. */
6290 tree
6291 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
6292 bool *no_add_attrs)
6294 tree node = *pnode;
6295 bool is_dllimport;
6297 /* These attributes may apply to structure and union types being created,
6298 but otherwise should pass to the declaration involved. */
6299 if (!DECL_P (node))
6301 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
6302 | (int) ATTR_FLAG_ARRAY_NEXT))
6304 *no_add_attrs = true;
6305 return tree_cons (name, args, NULL_TREE);
6307 if (TREE_CODE (node) == RECORD_TYPE
6308 || TREE_CODE (node) == UNION_TYPE)
6310 node = TYPE_NAME (node);
6311 if (!node)
6312 return NULL_TREE;
6314 else
6316 warning (OPT_Wattributes, "%qE attribute ignored",
6317 name);
6318 *no_add_attrs = true;
6319 return NULL_TREE;
6323 if (TREE_CODE (node) != FUNCTION_DECL
6324 && TREE_CODE (node) != VAR_DECL
6325 && TREE_CODE (node) != TYPE_DECL)
6327 *no_add_attrs = true;
6328 warning (OPT_Wattributes, "%qE attribute ignored",
6329 name);
6330 return NULL_TREE;
6333 if (TREE_CODE (node) == TYPE_DECL
6334 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
6335 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
6337 *no_add_attrs = true;
6338 warning (OPT_Wattributes, "%qE attribute ignored",
6339 name);
6340 return NULL_TREE;
6343 is_dllimport = is_attribute_p ("dllimport", name);
6345 /* Report error on dllimport ambiguities seen now before they cause
6346 any damage. */
6347 if (is_dllimport)
6349 /* Honor any target-specific overrides. */
6350 if (!targetm.valid_dllimport_attribute_p (node))
6351 *no_add_attrs = true;
6353 else if (TREE_CODE (node) == FUNCTION_DECL
6354 && DECL_DECLARED_INLINE_P (node))
6356 warning (OPT_Wattributes, "inline function %q+D declared as "
6357 " dllimport: attribute ignored", node);
6358 *no_add_attrs = true;
6360 /* Like MS, treat definition of dllimported variables and
6361 non-inlined functions on declaration as syntax errors. */
6362 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
6364 error ("function %q+D definition is marked dllimport", node);
6365 *no_add_attrs = true;
6368 else if (TREE_CODE (node) == VAR_DECL)
6370 if (DECL_INITIAL (node))
6372 error ("variable %q+D definition is marked dllimport",
6373 node);
6374 *no_add_attrs = true;
6377 /* `extern' needn't be specified with dllimport.
6378 Specify `extern' now and hope for the best. Sigh. */
6379 DECL_EXTERNAL (node) = 1;
6380 /* Also, implicitly give dllimport'd variables declared within
6381 a function global scope, unless declared static. */
6382 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
6383 TREE_PUBLIC (node) = 1;
6386 if (*no_add_attrs == false)
6387 DECL_DLLIMPORT_P (node) = 1;
6389 else if (TREE_CODE (node) == FUNCTION_DECL
6390 && DECL_DECLARED_INLINE_P (node)
6391 && flag_keep_inline_dllexport)
6392 /* An exported function, even if inline, must be emitted. */
6393 DECL_EXTERNAL (node) = 0;
6395 /* Report error if symbol is not accessible at global scope. */
6396 if (!TREE_PUBLIC (node)
6397 && (TREE_CODE (node) == VAR_DECL
6398 || TREE_CODE (node) == FUNCTION_DECL))
6400 error ("external linkage required for symbol %q+D because of "
6401 "%qE attribute", node, name);
6402 *no_add_attrs = true;
6405 /* A dllexport'd entity must have default visibility so that other
6406 program units (shared libraries or the main executable) can see
6407 it. A dllimport'd entity must have default visibility so that
6408 the linker knows that undefined references within this program
6409 unit can be resolved by the dynamic linker. */
6410 if (!*no_add_attrs)
6412 if (DECL_VISIBILITY_SPECIFIED (node)
6413 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
6414 error ("%qE implies default visibility, but %qD has already "
6415 "been declared with a different visibility",
6416 name, node);
6417 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
6418 DECL_VISIBILITY_SPECIFIED (node) = 1;
6421 return NULL_TREE;
6424 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
6426 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
6427 of the various TYPE_QUAL values. */
6429 static void
6430 set_type_quals (tree type, int type_quals)
6432 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
6433 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
6434 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
6435 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
6436 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
6439 /* Returns true iff unqualified CAND and BASE are equivalent. */
6441 bool
6442 check_base_type (const_tree cand, const_tree base)
6444 return (TYPE_NAME (cand) == TYPE_NAME (base)
6445 /* Apparently this is needed for Objective-C. */
6446 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6447 /* Check alignment. */
6448 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
6449 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6450 TYPE_ATTRIBUTES (base)));
6453 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
6455 bool
6456 check_qualified_type (const_tree cand, const_tree base, int type_quals)
6458 return (TYPE_QUALS (cand) == type_quals
6459 && check_base_type (cand, base));
6462 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
6464 static bool
6465 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
6467 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
6468 && TYPE_NAME (cand) == TYPE_NAME (base)
6469 /* Apparently this is needed for Objective-C. */
6470 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6471 /* Check alignment. */
6472 && TYPE_ALIGN (cand) == align
6473 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6474 TYPE_ATTRIBUTES (base)));
6477 /* This function checks to see if TYPE matches the size one of the built-in
6478 atomic types, and returns that core atomic type. */
6480 static tree
6481 find_atomic_core_type (tree type)
6483 tree base_atomic_type;
6485 /* Only handle complete types. */
6486 if (TYPE_SIZE (type) == NULL_TREE)
6487 return NULL_TREE;
6489 HOST_WIDE_INT type_size = tree_to_uhwi (TYPE_SIZE (type));
6490 switch (type_size)
6492 case 8:
6493 base_atomic_type = atomicQI_type_node;
6494 break;
6496 case 16:
6497 base_atomic_type = atomicHI_type_node;
6498 break;
6500 case 32:
6501 base_atomic_type = atomicSI_type_node;
6502 break;
6504 case 64:
6505 base_atomic_type = atomicDI_type_node;
6506 break;
6508 case 128:
6509 base_atomic_type = atomicTI_type_node;
6510 break;
6512 default:
6513 base_atomic_type = NULL_TREE;
6516 return base_atomic_type;
6519 /* Return a version of the TYPE, qualified as indicated by the
6520 TYPE_QUALS, if one exists. If no qualified version exists yet,
6521 return NULL_TREE. */
6523 tree
6524 get_qualified_type (tree type, int type_quals)
6526 tree t;
6528 if (TYPE_QUALS (type) == type_quals)
6529 return type;
6531 /* Search the chain of variants to see if there is already one there just
6532 like the one we need to have. If so, use that existing one. We must
6533 preserve the TYPE_NAME, since there is code that depends on this. */
6534 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6535 if (check_qualified_type (t, type, type_quals))
6536 return t;
6538 return NULL_TREE;
6541 /* Like get_qualified_type, but creates the type if it does not
6542 exist. This function never returns NULL_TREE. */
6544 tree
6545 build_qualified_type (tree type, int type_quals)
6547 tree t;
6549 /* See if we already have the appropriate qualified variant. */
6550 t = get_qualified_type (type, type_quals);
6552 /* If not, build it. */
6553 if (!t)
6555 t = build_variant_type_copy (type);
6556 set_type_quals (t, type_quals);
6558 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
6560 /* See if this object can map to a basic atomic type. */
6561 tree atomic_type = find_atomic_core_type (type);
6562 if (atomic_type)
6564 /* Ensure the alignment of this type is compatible with
6565 the required alignment of the atomic type. */
6566 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
6567 TYPE_ALIGN (t) = TYPE_ALIGN (atomic_type);
6571 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6572 /* Propagate structural equality. */
6573 SET_TYPE_STRUCTURAL_EQUALITY (t);
6574 else if (TYPE_CANONICAL (type) != type)
6575 /* Build the underlying canonical type, since it is different
6576 from TYPE. */
6578 tree c = build_qualified_type (TYPE_CANONICAL (type), type_quals);
6579 TYPE_CANONICAL (t) = TYPE_CANONICAL (c);
6581 else
6582 /* T is its own canonical type. */
6583 TYPE_CANONICAL (t) = t;
6587 return t;
6590 /* Create a variant of type T with alignment ALIGN. */
6592 tree
6593 build_aligned_type (tree type, unsigned int align)
6595 tree t;
6597 if (TYPE_PACKED (type)
6598 || TYPE_ALIGN (type) == align)
6599 return type;
6601 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6602 if (check_aligned_type (t, type, align))
6603 return t;
6605 t = build_variant_type_copy (type);
6606 TYPE_ALIGN (t) = align;
6608 return t;
6611 /* Create a new distinct copy of TYPE. The new type is made its own
6612 MAIN_VARIANT. If TYPE requires structural equality checks, the
6613 resulting type requires structural equality checks; otherwise, its
6614 TYPE_CANONICAL points to itself. */
6616 tree
6617 build_distinct_type_copy (tree type)
6619 tree t = copy_node (type);
6621 TYPE_POINTER_TO (t) = 0;
6622 TYPE_REFERENCE_TO (t) = 0;
6624 /* Set the canonical type either to a new equivalence class, or
6625 propagate the need for structural equality checks. */
6626 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6627 SET_TYPE_STRUCTURAL_EQUALITY (t);
6628 else
6629 TYPE_CANONICAL (t) = t;
6631 /* Make it its own variant. */
6632 TYPE_MAIN_VARIANT (t) = t;
6633 TYPE_NEXT_VARIANT (t) = 0;
6635 /* We do not record methods in type copies nor variants
6636 so we do not need to keep them up to date when new method
6637 is inserted. */
6638 if (RECORD_OR_UNION_TYPE_P (t))
6639 TYPE_METHODS (t) = NULL_TREE;
6641 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
6642 whose TREE_TYPE is not t. This can also happen in the Ada
6643 frontend when using subtypes. */
6645 return t;
6648 /* Create a new variant of TYPE, equivalent but distinct. This is so
6649 the caller can modify it. TYPE_CANONICAL for the return type will
6650 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
6651 are considered equal by the language itself (or that both types
6652 require structural equality checks). */
6654 tree
6655 build_variant_type_copy (tree type)
6657 tree t, m = TYPE_MAIN_VARIANT (type);
6659 t = build_distinct_type_copy (type);
6661 /* Since we're building a variant, assume that it is a non-semantic
6662 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
6663 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
6665 /* Add the new type to the chain of variants of TYPE. */
6666 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
6667 TYPE_NEXT_VARIANT (m) = t;
6668 TYPE_MAIN_VARIANT (t) = m;
6670 return t;
6673 /* Return true if the from tree in both tree maps are equal. */
6676 tree_map_base_eq (const void *va, const void *vb)
6678 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6679 *const b = (const struct tree_map_base *) vb;
6680 return (a->from == b->from);
6683 /* Hash a from tree in a tree_base_map. */
6685 unsigned int
6686 tree_map_base_hash (const void *item)
6688 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6691 /* Return true if this tree map structure is marked for garbage collection
6692 purposes. We simply return true if the from tree is marked, so that this
6693 structure goes away when the from tree goes away. */
6696 tree_map_base_marked_p (const void *p)
6698 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6701 /* Hash a from tree in a tree_map. */
6703 unsigned int
6704 tree_map_hash (const void *item)
6706 return (((const struct tree_map *) item)->hash);
6709 /* Hash a from tree in a tree_decl_map. */
6711 unsigned int
6712 tree_decl_map_hash (const void *item)
6714 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6717 /* Return the initialization priority for DECL. */
6719 priority_type
6720 decl_init_priority_lookup (tree decl)
6722 symtab_node *snode = symtab_node::get (decl);
6724 if (!snode)
6725 return DEFAULT_INIT_PRIORITY;
6726 return
6727 snode->get_init_priority ();
6730 /* Return the finalization priority for DECL. */
6732 priority_type
6733 decl_fini_priority_lookup (tree decl)
6735 cgraph_node *node = cgraph_node::get (decl);
6737 if (!node)
6738 return DEFAULT_INIT_PRIORITY;
6739 return
6740 node->get_fini_priority ();
6743 /* Set the initialization priority for DECL to PRIORITY. */
6745 void
6746 decl_init_priority_insert (tree decl, priority_type priority)
6748 struct symtab_node *snode;
6750 if (priority == DEFAULT_INIT_PRIORITY)
6752 snode = symtab_node::get (decl);
6753 if (!snode)
6754 return;
6756 else if (TREE_CODE (decl) == VAR_DECL)
6757 snode = varpool_node::get_create (decl);
6758 else
6759 snode = cgraph_node::get_create (decl);
6760 snode->set_init_priority (priority);
6763 /* Set the finalization priority for DECL to PRIORITY. */
6765 void
6766 decl_fini_priority_insert (tree decl, priority_type priority)
6768 struct cgraph_node *node;
6770 if (priority == DEFAULT_INIT_PRIORITY)
6772 node = cgraph_node::get (decl);
6773 if (!node)
6774 return;
6776 else
6777 node = cgraph_node::get_create (decl);
6778 node->set_fini_priority (priority);
6781 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6783 static void
6784 print_debug_expr_statistics (void)
6786 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6787 (long) debug_expr_for_decl->size (),
6788 (long) debug_expr_for_decl->elements (),
6789 debug_expr_for_decl->collisions ());
6792 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6794 static void
6795 print_value_expr_statistics (void)
6797 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6798 (long) value_expr_for_decl->size (),
6799 (long) value_expr_for_decl->elements (),
6800 value_expr_for_decl->collisions ());
6803 /* Lookup a debug expression for FROM, and return it if we find one. */
6805 tree
6806 decl_debug_expr_lookup (tree from)
6808 struct tree_decl_map *h, in;
6809 in.base.from = from;
6811 h = debug_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6812 if (h)
6813 return h->to;
6814 return NULL_TREE;
6817 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6819 void
6820 decl_debug_expr_insert (tree from, tree to)
6822 struct tree_decl_map *h;
6824 h = ggc_alloc<tree_decl_map> ();
6825 h->base.from = from;
6826 h->to = to;
6827 *debug_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6830 /* Lookup a value expression for FROM, and return it if we find one. */
6832 tree
6833 decl_value_expr_lookup (tree from)
6835 struct tree_decl_map *h, in;
6836 in.base.from = from;
6838 h = value_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6839 if (h)
6840 return h->to;
6841 return NULL_TREE;
6844 /* Insert a mapping FROM->TO in the value expression hashtable. */
6846 void
6847 decl_value_expr_insert (tree from, tree to)
6849 struct tree_decl_map *h;
6851 h = ggc_alloc<tree_decl_map> ();
6852 h->base.from = from;
6853 h->to = to;
6854 *value_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6857 /* Lookup a vector of debug arguments for FROM, and return it if we
6858 find one. */
6860 vec<tree, va_gc> **
6861 decl_debug_args_lookup (tree from)
6863 struct tree_vec_map *h, in;
6865 if (!DECL_HAS_DEBUG_ARGS_P (from))
6866 return NULL;
6867 gcc_checking_assert (debug_args_for_decl != NULL);
6868 in.base.from = from;
6869 h = debug_args_for_decl->find_with_hash (&in, DECL_UID (from));
6870 if (h)
6871 return &h->to;
6872 return NULL;
6875 /* Insert a mapping FROM->empty vector of debug arguments in the value
6876 expression hashtable. */
6878 vec<tree, va_gc> **
6879 decl_debug_args_insert (tree from)
6881 struct tree_vec_map *h;
6882 tree_vec_map **loc;
6884 if (DECL_HAS_DEBUG_ARGS_P (from))
6885 return decl_debug_args_lookup (from);
6886 if (debug_args_for_decl == NULL)
6887 debug_args_for_decl = hash_table<tree_vec_map_cache_hasher>::create_ggc (64);
6888 h = ggc_alloc<tree_vec_map> ();
6889 h->base.from = from;
6890 h->to = NULL;
6891 loc = debug_args_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT);
6892 *loc = h;
6893 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6894 return &h->to;
6897 /* Hashing of types so that we don't make duplicates.
6898 The entry point is `type_hash_canon'. */
6900 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6901 with types in the TREE_VALUE slots), by adding the hash codes
6902 of the individual types. */
6904 static void
6905 type_hash_list (const_tree list, inchash::hash &hstate)
6907 const_tree tail;
6909 for (tail = list; tail; tail = TREE_CHAIN (tail))
6910 if (TREE_VALUE (tail) != error_mark_node)
6911 hstate.add_object (TYPE_HASH (TREE_VALUE (tail)));
6914 /* These are the Hashtable callback functions. */
6916 /* Returns true iff the types are equivalent. */
6918 bool
6919 type_cache_hasher::equal (type_hash *a, type_hash *b)
6921 /* First test the things that are the same for all types. */
6922 if (a->hash != b->hash
6923 || TREE_CODE (a->type) != TREE_CODE (b->type)
6924 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6925 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6926 TYPE_ATTRIBUTES (b->type))
6927 || (TREE_CODE (a->type) != COMPLEX_TYPE
6928 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6929 return 0;
6931 /* Be careful about comparing arrays before and after the element type
6932 has been completed; don't compare TYPE_ALIGN unless both types are
6933 complete. */
6934 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6935 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6936 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6937 return 0;
6939 switch (TREE_CODE (a->type))
6941 case VOID_TYPE:
6942 case COMPLEX_TYPE:
6943 case POINTER_TYPE:
6944 case REFERENCE_TYPE:
6945 case NULLPTR_TYPE:
6946 return 1;
6948 case VECTOR_TYPE:
6949 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6951 case ENUMERAL_TYPE:
6952 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6953 && !(TYPE_VALUES (a->type)
6954 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6955 && TYPE_VALUES (b->type)
6956 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6957 && type_list_equal (TYPE_VALUES (a->type),
6958 TYPE_VALUES (b->type))))
6959 return 0;
6961 /* ... fall through ... */
6963 case INTEGER_TYPE:
6964 case REAL_TYPE:
6965 case BOOLEAN_TYPE:
6966 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
6967 return false;
6968 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6969 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6970 TYPE_MAX_VALUE (b->type)))
6971 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6972 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6973 TYPE_MIN_VALUE (b->type))));
6975 case FIXED_POINT_TYPE:
6976 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6978 case OFFSET_TYPE:
6979 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6981 case METHOD_TYPE:
6982 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6983 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6984 || (TYPE_ARG_TYPES (a->type)
6985 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6986 && TYPE_ARG_TYPES (b->type)
6987 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6988 && type_list_equal (TYPE_ARG_TYPES (a->type),
6989 TYPE_ARG_TYPES (b->type)))))
6990 break;
6991 return 0;
6992 case ARRAY_TYPE:
6993 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6995 case RECORD_TYPE:
6996 case UNION_TYPE:
6997 case QUAL_UNION_TYPE:
6998 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6999 || (TYPE_FIELDS (a->type)
7000 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
7001 && TYPE_FIELDS (b->type)
7002 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
7003 && type_list_equal (TYPE_FIELDS (a->type),
7004 TYPE_FIELDS (b->type))));
7006 case FUNCTION_TYPE:
7007 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
7008 || (TYPE_ARG_TYPES (a->type)
7009 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
7010 && TYPE_ARG_TYPES (b->type)
7011 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
7012 && type_list_equal (TYPE_ARG_TYPES (a->type),
7013 TYPE_ARG_TYPES (b->type))))
7014 break;
7015 return 0;
7017 default:
7018 return 0;
7021 if (lang_hooks.types.type_hash_eq != NULL)
7022 return lang_hooks.types.type_hash_eq (a->type, b->type);
7024 return 1;
7027 /* Given TYPE, and HASHCODE its hash code, return the canonical
7028 object for an identical type if one already exists.
7029 Otherwise, return TYPE, and record it as the canonical object.
7031 To use this function, first create a type of the sort you want.
7032 Then compute its hash code from the fields of the type that
7033 make it different from other similar types.
7034 Then call this function and use the value. */
7036 tree
7037 type_hash_canon (unsigned int hashcode, tree type)
7039 type_hash in;
7040 type_hash **loc;
7042 /* The hash table only contains main variants, so ensure that's what we're
7043 being passed. */
7044 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
7046 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
7047 must call that routine before comparing TYPE_ALIGNs. */
7048 layout_type (type);
7050 in.hash = hashcode;
7051 in.type = type;
7053 loc = type_hash_table->find_slot_with_hash (&in, hashcode, INSERT);
7054 if (*loc)
7056 tree t1 = ((type_hash *) *loc)->type;
7057 gcc_assert (TYPE_MAIN_VARIANT (t1) == t1);
7058 if (GATHER_STATISTICS)
7060 tree_code_counts[(int) TREE_CODE (type)]--;
7061 tree_node_counts[(int) t_kind]--;
7062 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
7064 return t1;
7066 else
7068 struct type_hash *h;
7070 h = ggc_alloc<type_hash> ();
7071 h->hash = hashcode;
7072 h->type = type;
7073 *loc = h;
7075 return type;
7079 static void
7080 print_type_hash_statistics (void)
7082 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
7083 (long) type_hash_table->size (),
7084 (long) type_hash_table->elements (),
7085 type_hash_table->collisions ());
7088 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
7089 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
7090 by adding the hash codes of the individual attributes. */
7092 static void
7093 attribute_hash_list (const_tree list, inchash::hash &hstate)
7095 const_tree tail;
7097 for (tail = list; tail; tail = TREE_CHAIN (tail))
7098 /* ??? Do we want to add in TREE_VALUE too? */
7099 hstate.add_object (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)));
7102 /* Given two lists of attributes, return true if list l2 is
7103 equivalent to l1. */
7106 attribute_list_equal (const_tree l1, const_tree l2)
7108 if (l1 == l2)
7109 return 1;
7111 return attribute_list_contained (l1, l2)
7112 && attribute_list_contained (l2, l1);
7115 /* Given two lists of attributes, return true if list L2 is
7116 completely contained within L1. */
7117 /* ??? This would be faster if attribute names were stored in a canonicalized
7118 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
7119 must be used to show these elements are equivalent (which they are). */
7120 /* ??? It's not clear that attributes with arguments will always be handled
7121 correctly. */
7124 attribute_list_contained (const_tree l1, const_tree l2)
7126 const_tree t1, t2;
7128 /* First check the obvious, maybe the lists are identical. */
7129 if (l1 == l2)
7130 return 1;
7132 /* Maybe the lists are similar. */
7133 for (t1 = l1, t2 = l2;
7134 t1 != 0 && t2 != 0
7135 && get_attribute_name (t1) == get_attribute_name (t2)
7136 && TREE_VALUE (t1) == TREE_VALUE (t2);
7137 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7140 /* Maybe the lists are equal. */
7141 if (t1 == 0 && t2 == 0)
7142 return 1;
7144 for (; t2 != 0; t2 = TREE_CHAIN (t2))
7146 const_tree attr;
7147 /* This CONST_CAST is okay because lookup_attribute does not
7148 modify its argument and the return value is assigned to a
7149 const_tree. */
7150 for (attr = lookup_ident_attribute (get_attribute_name (t2),
7151 CONST_CAST_TREE (l1));
7152 attr != NULL_TREE && !attribute_value_equal (t2, attr);
7153 attr = lookup_ident_attribute (get_attribute_name (t2),
7154 TREE_CHAIN (attr)))
7157 if (attr == NULL_TREE)
7158 return 0;
7161 return 1;
7164 /* Given two lists of types
7165 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
7166 return 1 if the lists contain the same types in the same order.
7167 Also, the TREE_PURPOSEs must match. */
7170 type_list_equal (const_tree l1, const_tree l2)
7172 const_tree t1, t2;
7174 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7175 if (TREE_VALUE (t1) != TREE_VALUE (t2)
7176 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
7177 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
7178 && (TREE_TYPE (TREE_PURPOSE (t1))
7179 == TREE_TYPE (TREE_PURPOSE (t2))))))
7180 return 0;
7182 return t1 == t2;
7185 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
7186 given by TYPE. If the argument list accepts variable arguments,
7187 then this function counts only the ordinary arguments. */
7190 type_num_arguments (const_tree type)
7192 int i = 0;
7193 tree t;
7195 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
7196 /* If the function does not take a variable number of arguments,
7197 the last element in the list will have type `void'. */
7198 if (VOID_TYPE_P (TREE_VALUE (t)))
7199 break;
7200 else
7201 ++i;
7203 return i;
7206 /* Nonzero if integer constants T1 and T2
7207 represent the same constant value. */
7210 tree_int_cst_equal (const_tree t1, const_tree t2)
7212 if (t1 == t2)
7213 return 1;
7215 if (t1 == 0 || t2 == 0)
7216 return 0;
7218 if (TREE_CODE (t1) == INTEGER_CST
7219 && TREE_CODE (t2) == INTEGER_CST
7220 && wi::to_widest (t1) == wi::to_widest (t2))
7221 return 1;
7223 return 0;
7226 /* Return true if T is an INTEGER_CST whose numerical value (extended
7227 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
7229 bool
7230 tree_fits_shwi_p (const_tree t)
7232 return (t != NULL_TREE
7233 && TREE_CODE (t) == INTEGER_CST
7234 && wi::fits_shwi_p (wi::to_widest (t)));
7237 /* Return true if T is an INTEGER_CST whose numerical value (extended
7238 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
7240 bool
7241 tree_fits_uhwi_p (const_tree t)
7243 return (t != NULL_TREE
7244 && TREE_CODE (t) == INTEGER_CST
7245 && wi::fits_uhwi_p (wi::to_widest (t)));
7248 /* T is an INTEGER_CST whose numerical value (extended according to
7249 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
7250 HOST_WIDE_INT. */
7252 HOST_WIDE_INT
7253 tree_to_shwi (const_tree t)
7255 gcc_assert (tree_fits_shwi_p (t));
7256 return TREE_INT_CST_LOW (t);
7259 /* T is an INTEGER_CST whose numerical value (extended according to
7260 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
7261 HOST_WIDE_INT. */
7263 unsigned HOST_WIDE_INT
7264 tree_to_uhwi (const_tree t)
7266 gcc_assert (tree_fits_uhwi_p (t));
7267 return TREE_INT_CST_LOW (t);
7270 /* Return the most significant (sign) bit of T. */
7273 tree_int_cst_sign_bit (const_tree t)
7275 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
7277 return wi::extract_uhwi (t, bitno, 1);
7280 /* Return an indication of the sign of the integer constant T.
7281 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
7282 Note that -1 will never be returned if T's type is unsigned. */
7285 tree_int_cst_sgn (const_tree t)
7287 if (wi::eq_p (t, 0))
7288 return 0;
7289 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
7290 return 1;
7291 else if (wi::neg_p (t))
7292 return -1;
7293 else
7294 return 1;
7297 /* Return the minimum number of bits needed to represent VALUE in a
7298 signed or unsigned type, UNSIGNEDP says which. */
7300 unsigned int
7301 tree_int_cst_min_precision (tree value, signop sgn)
7303 /* If the value is negative, compute its negative minus 1. The latter
7304 adjustment is because the absolute value of the largest negative value
7305 is one larger than the largest positive value. This is equivalent to
7306 a bit-wise negation, so use that operation instead. */
7308 if (tree_int_cst_sgn (value) < 0)
7309 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
7311 /* Return the number of bits needed, taking into account the fact
7312 that we need one more bit for a signed than unsigned type.
7313 If value is 0 or -1, the minimum precision is 1 no matter
7314 whether unsignedp is true or false. */
7316 if (integer_zerop (value))
7317 return 1;
7318 else
7319 return tree_floor_log2 (value) + 1 + (sgn == SIGNED ? 1 : 0) ;
7322 /* Return truthvalue of whether T1 is the same tree structure as T2.
7323 Return 1 if they are the same.
7324 Return 0 if they are understandably different.
7325 Return -1 if either contains tree structure not understood by
7326 this function. */
7329 simple_cst_equal (const_tree t1, const_tree t2)
7331 enum tree_code code1, code2;
7332 int cmp;
7333 int i;
7335 if (t1 == t2)
7336 return 1;
7337 if (t1 == 0 || t2 == 0)
7338 return 0;
7340 code1 = TREE_CODE (t1);
7341 code2 = TREE_CODE (t2);
7343 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
7345 if (CONVERT_EXPR_CODE_P (code2)
7346 || code2 == NON_LVALUE_EXPR)
7347 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7348 else
7349 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
7352 else if (CONVERT_EXPR_CODE_P (code2)
7353 || code2 == NON_LVALUE_EXPR)
7354 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
7356 if (code1 != code2)
7357 return 0;
7359 switch (code1)
7361 case INTEGER_CST:
7362 return wi::to_widest (t1) == wi::to_widest (t2);
7364 case REAL_CST:
7365 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
7367 case FIXED_CST:
7368 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
7370 case STRING_CST:
7371 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
7372 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
7373 TREE_STRING_LENGTH (t1)));
7375 case CONSTRUCTOR:
7377 unsigned HOST_WIDE_INT idx;
7378 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
7379 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
7381 if (vec_safe_length (v1) != vec_safe_length (v2))
7382 return false;
7384 for (idx = 0; idx < vec_safe_length (v1); ++idx)
7385 /* ??? Should we handle also fields here? */
7386 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
7387 return false;
7388 return true;
7391 case SAVE_EXPR:
7392 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7394 case CALL_EXPR:
7395 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
7396 if (cmp <= 0)
7397 return cmp;
7398 if (call_expr_nargs (t1) != call_expr_nargs (t2))
7399 return 0;
7401 const_tree arg1, arg2;
7402 const_call_expr_arg_iterator iter1, iter2;
7403 for (arg1 = first_const_call_expr_arg (t1, &iter1),
7404 arg2 = first_const_call_expr_arg (t2, &iter2);
7405 arg1 && arg2;
7406 arg1 = next_const_call_expr_arg (&iter1),
7407 arg2 = next_const_call_expr_arg (&iter2))
7409 cmp = simple_cst_equal (arg1, arg2);
7410 if (cmp <= 0)
7411 return cmp;
7413 return arg1 == arg2;
7416 case TARGET_EXPR:
7417 /* Special case: if either target is an unallocated VAR_DECL,
7418 it means that it's going to be unified with whatever the
7419 TARGET_EXPR is really supposed to initialize, so treat it
7420 as being equivalent to anything. */
7421 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
7422 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
7423 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
7424 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
7425 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
7426 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
7427 cmp = 1;
7428 else
7429 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7431 if (cmp <= 0)
7432 return cmp;
7434 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
7436 case WITH_CLEANUP_EXPR:
7437 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7438 if (cmp <= 0)
7439 return cmp;
7441 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
7443 case COMPONENT_REF:
7444 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
7445 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7447 return 0;
7449 case VAR_DECL:
7450 case PARM_DECL:
7451 case CONST_DECL:
7452 case FUNCTION_DECL:
7453 return 0;
7455 default:
7456 break;
7459 /* This general rule works for most tree codes. All exceptions should be
7460 handled above. If this is a language-specific tree code, we can't
7461 trust what might be in the operand, so say we don't know
7462 the situation. */
7463 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
7464 return -1;
7466 switch (TREE_CODE_CLASS (code1))
7468 case tcc_unary:
7469 case tcc_binary:
7470 case tcc_comparison:
7471 case tcc_expression:
7472 case tcc_reference:
7473 case tcc_statement:
7474 cmp = 1;
7475 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
7477 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
7478 if (cmp <= 0)
7479 return cmp;
7482 return cmp;
7484 default:
7485 return -1;
7489 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
7490 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
7491 than U, respectively. */
7494 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
7496 if (tree_int_cst_sgn (t) < 0)
7497 return -1;
7498 else if (!tree_fits_uhwi_p (t))
7499 return 1;
7500 else if (TREE_INT_CST_LOW (t) == u)
7501 return 0;
7502 else if (TREE_INT_CST_LOW (t) < u)
7503 return -1;
7504 else
7505 return 1;
7508 /* Return true if SIZE represents a constant size that is in bounds of
7509 what the middle-end and the backend accepts (covering not more than
7510 half of the address-space). */
7512 bool
7513 valid_constant_size_p (const_tree size)
7515 if (! tree_fits_uhwi_p (size)
7516 || TREE_OVERFLOW (size)
7517 || tree_int_cst_sign_bit (size) != 0)
7518 return false;
7519 return true;
7522 /* Return the precision of the type, or for a complex or vector type the
7523 precision of the type of its elements. */
7525 unsigned int
7526 element_precision (const_tree type)
7528 enum tree_code code = TREE_CODE (type);
7529 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
7530 type = TREE_TYPE (type);
7532 return TYPE_PRECISION (type);
7535 /* Return true if CODE represents an associative tree code. Otherwise
7536 return false. */
7537 bool
7538 associative_tree_code (enum tree_code code)
7540 switch (code)
7542 case BIT_IOR_EXPR:
7543 case BIT_AND_EXPR:
7544 case BIT_XOR_EXPR:
7545 case PLUS_EXPR:
7546 case MULT_EXPR:
7547 case MIN_EXPR:
7548 case MAX_EXPR:
7549 return true;
7551 default:
7552 break;
7554 return false;
7557 /* Return true if CODE represents a commutative tree code. Otherwise
7558 return false. */
7559 bool
7560 commutative_tree_code (enum tree_code code)
7562 switch (code)
7564 case PLUS_EXPR:
7565 case MULT_EXPR:
7566 case MULT_HIGHPART_EXPR:
7567 case MIN_EXPR:
7568 case MAX_EXPR:
7569 case BIT_IOR_EXPR:
7570 case BIT_XOR_EXPR:
7571 case BIT_AND_EXPR:
7572 case NE_EXPR:
7573 case EQ_EXPR:
7574 case UNORDERED_EXPR:
7575 case ORDERED_EXPR:
7576 case UNEQ_EXPR:
7577 case LTGT_EXPR:
7578 case TRUTH_AND_EXPR:
7579 case TRUTH_XOR_EXPR:
7580 case TRUTH_OR_EXPR:
7581 case WIDEN_MULT_EXPR:
7582 case VEC_WIDEN_MULT_HI_EXPR:
7583 case VEC_WIDEN_MULT_LO_EXPR:
7584 case VEC_WIDEN_MULT_EVEN_EXPR:
7585 case VEC_WIDEN_MULT_ODD_EXPR:
7586 return true;
7588 default:
7589 break;
7591 return false;
7594 /* Return true if CODE represents a ternary tree code for which the
7595 first two operands are commutative. Otherwise return false. */
7596 bool
7597 commutative_ternary_tree_code (enum tree_code code)
7599 switch (code)
7601 case WIDEN_MULT_PLUS_EXPR:
7602 case WIDEN_MULT_MINUS_EXPR:
7603 case DOT_PROD_EXPR:
7604 case FMA_EXPR:
7605 return true;
7607 default:
7608 break;
7610 return false;
7613 namespace inchash
7616 /* Generate a hash value for an expression. This can be used iteratively
7617 by passing a previous result as the HSTATE argument.
7619 This function is intended to produce the same hash for expressions which
7620 would compare equal using operand_equal_p. */
7621 void
7622 add_expr (const_tree t, inchash::hash &hstate)
7624 int i;
7625 enum tree_code code;
7626 enum tree_code_class tclass;
7628 if (t == NULL_TREE)
7630 hstate.merge_hash (0);
7631 return;
7634 code = TREE_CODE (t);
7636 switch (code)
7638 /* Alas, constants aren't shared, so we can't rely on pointer
7639 identity. */
7640 case VOID_CST:
7641 hstate.merge_hash (0);
7642 return;
7643 case INTEGER_CST:
7644 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
7645 hstate.add_wide_int (TREE_INT_CST_ELT (t, i));
7646 return;
7647 case REAL_CST:
7649 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7650 hstate.merge_hash (val2);
7651 return;
7653 case FIXED_CST:
7655 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7656 hstate.merge_hash (val2);
7657 return;
7659 case STRING_CST:
7660 hstate.add ((const void *) TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
7661 return;
7662 case COMPLEX_CST:
7663 inchash::add_expr (TREE_REALPART (t), hstate);
7664 inchash::add_expr (TREE_IMAGPART (t), hstate);
7665 return;
7666 case VECTOR_CST:
7668 unsigned i;
7669 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7670 inchash::add_expr (VECTOR_CST_ELT (t, i), hstate);
7671 return;
7673 case SSA_NAME:
7674 /* We can just compare by pointer. */
7675 hstate.add_wide_int (SSA_NAME_VERSION (t));
7676 return;
7677 case PLACEHOLDER_EXPR:
7678 /* The node itself doesn't matter. */
7679 return;
7680 case TREE_LIST:
7681 /* A list of expressions, for a CALL_EXPR or as the elements of a
7682 VECTOR_CST. */
7683 for (; t; t = TREE_CHAIN (t))
7684 inchash::add_expr (TREE_VALUE (t), hstate);
7685 return;
7686 case CONSTRUCTOR:
7688 unsigned HOST_WIDE_INT idx;
7689 tree field, value;
7690 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7692 inchash::add_expr (field, hstate);
7693 inchash::add_expr (value, hstate);
7695 return;
7697 case FUNCTION_DECL:
7698 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7699 Otherwise nodes that compare equal according to operand_equal_p might
7700 get different hash codes. However, don't do this for machine specific
7701 or front end builtins, since the function code is overloaded in those
7702 cases. */
7703 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7704 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7706 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7707 code = TREE_CODE (t);
7709 /* FALL THROUGH */
7710 default:
7711 tclass = TREE_CODE_CLASS (code);
7713 if (tclass == tcc_declaration)
7715 /* DECL's have a unique ID */
7716 hstate.add_wide_int (DECL_UID (t));
7718 else
7720 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7722 hstate.add_object (code);
7724 /* Don't hash the type, that can lead to having nodes which
7725 compare equal according to operand_equal_p, but which
7726 have different hash codes. */
7727 if (CONVERT_EXPR_CODE_P (code)
7728 || code == NON_LVALUE_EXPR)
7730 /* Make sure to include signness in the hash computation. */
7731 hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
7732 inchash::add_expr (TREE_OPERAND (t, 0), hstate);
7735 else if (commutative_tree_code (code))
7737 /* It's a commutative expression. We want to hash it the same
7738 however it appears. We do this by first hashing both operands
7739 and then rehashing based on the order of their independent
7740 hashes. */
7741 inchash::hash one, two;
7742 inchash::add_expr (TREE_OPERAND (t, 0), one);
7743 inchash::add_expr (TREE_OPERAND (t, 1), two);
7744 hstate.add_commutative (one, two);
7746 else
7747 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7748 inchash::add_expr (TREE_OPERAND (t, i), hstate);
7750 return;
7756 /* Constructors for pointer, array and function types.
7757 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7758 constructed by language-dependent code, not here.) */
7760 /* Construct, lay out and return the type of pointers to TO_TYPE with
7761 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7762 reference all of memory. If such a type has already been
7763 constructed, reuse it. */
7765 tree
7766 build_pointer_type_for_mode (tree to_type, machine_mode mode,
7767 bool can_alias_all)
7769 tree t;
7770 bool could_alias = can_alias_all;
7772 if (to_type == error_mark_node)
7773 return error_mark_node;
7775 /* If the pointed-to type has the may_alias attribute set, force
7776 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7777 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7778 can_alias_all = true;
7780 /* In some cases, languages will have things that aren't a POINTER_TYPE
7781 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7782 In that case, return that type without regard to the rest of our
7783 operands.
7785 ??? This is a kludge, but consistent with the way this function has
7786 always operated and there doesn't seem to be a good way to avoid this
7787 at the moment. */
7788 if (TYPE_POINTER_TO (to_type) != 0
7789 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7790 return TYPE_POINTER_TO (to_type);
7792 /* First, if we already have a type for pointers to TO_TYPE and it's
7793 the proper mode, use it. */
7794 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7795 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7796 return t;
7798 t = make_node (POINTER_TYPE);
7800 TREE_TYPE (t) = to_type;
7801 SET_TYPE_MODE (t, mode);
7802 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7803 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7804 TYPE_POINTER_TO (to_type) = t;
7806 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7807 SET_TYPE_STRUCTURAL_EQUALITY (t);
7808 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
7809 TYPE_CANONICAL (t)
7810 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7811 mode, false);
7813 /* Lay out the type. This function has many callers that are concerned
7814 with expression-construction, and this simplifies them all. */
7815 layout_type (t);
7817 return t;
7820 /* By default build pointers in ptr_mode. */
7822 tree
7823 build_pointer_type (tree to_type)
7825 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7826 : TYPE_ADDR_SPACE (to_type);
7827 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7828 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7831 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7833 tree
7834 build_reference_type_for_mode (tree to_type, machine_mode mode,
7835 bool can_alias_all)
7837 tree t;
7838 bool could_alias = can_alias_all;
7840 if (to_type == error_mark_node)
7841 return error_mark_node;
7843 /* If the pointed-to type has the may_alias attribute set, force
7844 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7845 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7846 can_alias_all = true;
7848 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7849 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7850 In that case, return that type without regard to the rest of our
7851 operands.
7853 ??? This is a kludge, but consistent with the way this function has
7854 always operated and there doesn't seem to be a good way to avoid this
7855 at the moment. */
7856 if (TYPE_REFERENCE_TO (to_type) != 0
7857 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7858 return TYPE_REFERENCE_TO (to_type);
7860 /* First, if we already have a type for pointers to TO_TYPE and it's
7861 the proper mode, use it. */
7862 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7863 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7864 return t;
7866 t = make_node (REFERENCE_TYPE);
7868 TREE_TYPE (t) = to_type;
7869 SET_TYPE_MODE (t, mode);
7870 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7871 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7872 TYPE_REFERENCE_TO (to_type) = t;
7874 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7875 SET_TYPE_STRUCTURAL_EQUALITY (t);
7876 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
7877 TYPE_CANONICAL (t)
7878 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7879 mode, false);
7881 layout_type (t);
7883 return t;
7887 /* Build the node for the type of references-to-TO_TYPE by default
7888 in ptr_mode. */
7890 tree
7891 build_reference_type (tree to_type)
7893 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7894 : TYPE_ADDR_SPACE (to_type);
7895 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7896 return build_reference_type_for_mode (to_type, pointer_mode, false);
7899 #define MAX_INT_CACHED_PREC \
7900 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7901 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7903 /* Builds a signed or unsigned integer type of precision PRECISION.
7904 Used for C bitfields whose precision does not match that of
7905 built-in target types. */
7906 tree
7907 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7908 int unsignedp)
7910 tree itype, ret;
7912 if (unsignedp)
7913 unsignedp = MAX_INT_CACHED_PREC + 1;
7915 if (precision <= MAX_INT_CACHED_PREC)
7917 itype = nonstandard_integer_type_cache[precision + unsignedp];
7918 if (itype)
7919 return itype;
7922 itype = make_node (INTEGER_TYPE);
7923 TYPE_PRECISION (itype) = precision;
7925 if (unsignedp)
7926 fixup_unsigned_type (itype);
7927 else
7928 fixup_signed_type (itype);
7930 ret = itype;
7931 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (itype)))
7932 ret = type_hash_canon (tree_to_uhwi (TYPE_MAX_VALUE (itype)), itype);
7933 if (precision <= MAX_INT_CACHED_PREC)
7934 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7936 return ret;
7939 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7940 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7941 is true, reuse such a type that has already been constructed. */
7943 static tree
7944 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7946 tree itype = make_node (INTEGER_TYPE);
7947 inchash::hash hstate;
7949 TREE_TYPE (itype) = type;
7951 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7952 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7954 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7955 SET_TYPE_MODE (itype, TYPE_MODE (type));
7956 TYPE_SIZE (itype) = TYPE_SIZE (type);
7957 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7958 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7959 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7961 if (!shared)
7962 return itype;
7964 if ((TYPE_MIN_VALUE (itype)
7965 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7966 || (TYPE_MAX_VALUE (itype)
7967 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7969 /* Since we cannot reliably merge this type, we need to compare it using
7970 structural equality checks. */
7971 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7972 return itype;
7975 inchash::add_expr (TYPE_MIN_VALUE (itype), hstate);
7976 inchash::add_expr (TYPE_MAX_VALUE (itype), hstate);
7977 hstate.merge_hash (TYPE_HASH (type));
7978 itype = type_hash_canon (hstate.end (), itype);
7980 return itype;
7983 /* Wrapper around build_range_type_1 with SHARED set to true. */
7985 tree
7986 build_range_type (tree type, tree lowval, tree highval)
7988 return build_range_type_1 (type, lowval, highval, true);
7991 /* Wrapper around build_range_type_1 with SHARED set to false. */
7993 tree
7994 build_nonshared_range_type (tree type, tree lowval, tree highval)
7996 return build_range_type_1 (type, lowval, highval, false);
7999 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
8000 MAXVAL should be the maximum value in the domain
8001 (one less than the length of the array).
8003 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
8004 We don't enforce this limit, that is up to caller (e.g. language front end).
8005 The limit exists because the result is a signed type and we don't handle
8006 sizes that use more than one HOST_WIDE_INT. */
8008 tree
8009 build_index_type (tree maxval)
8011 return build_range_type (sizetype, size_zero_node, maxval);
8014 /* Return true if the debug information for TYPE, a subtype, should be emitted
8015 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
8016 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
8017 debug info and doesn't reflect the source code. */
8019 bool
8020 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
8022 tree base_type = TREE_TYPE (type), low, high;
8024 /* Subrange types have a base type which is an integral type. */
8025 if (!INTEGRAL_TYPE_P (base_type))
8026 return false;
8028 /* Get the real bounds of the subtype. */
8029 if (lang_hooks.types.get_subrange_bounds)
8030 lang_hooks.types.get_subrange_bounds (type, &low, &high);
8031 else
8033 low = TYPE_MIN_VALUE (type);
8034 high = TYPE_MAX_VALUE (type);
8037 /* If the type and its base type have the same representation and the same
8038 name, then the type is not a subrange but a copy of the base type. */
8039 if ((TREE_CODE (base_type) == INTEGER_TYPE
8040 || TREE_CODE (base_type) == BOOLEAN_TYPE)
8041 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
8042 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
8043 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type))
8044 && TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (base_type))
8045 return false;
8047 if (lowval)
8048 *lowval = low;
8049 if (highval)
8050 *highval = high;
8051 return true;
8054 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
8055 and number of elements specified by the range of values of INDEX_TYPE.
8056 If SHARED is true, reuse such a type that has already been constructed. */
8058 static tree
8059 build_array_type_1 (tree elt_type, tree index_type, bool shared)
8061 tree t;
8063 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
8065 error ("arrays of functions are not meaningful");
8066 elt_type = integer_type_node;
8069 t = make_node (ARRAY_TYPE);
8070 TREE_TYPE (t) = elt_type;
8071 TYPE_DOMAIN (t) = index_type;
8072 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
8073 layout_type (t);
8075 /* If the element type is incomplete at this point we get marked for
8076 structural equality. Do not record these types in the canonical
8077 type hashtable. */
8078 if (TYPE_STRUCTURAL_EQUALITY_P (t))
8079 return t;
8081 if (shared)
8083 inchash::hash hstate;
8084 hstate.add_object (TYPE_HASH (elt_type));
8085 if (index_type)
8086 hstate.add_object (TYPE_HASH (index_type));
8087 t = type_hash_canon (hstate.end (), t);
8090 if (TYPE_CANONICAL (t) == t)
8092 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
8093 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
8094 SET_TYPE_STRUCTURAL_EQUALITY (t);
8095 else if (TYPE_CANONICAL (elt_type) != elt_type
8096 || (index_type && TYPE_CANONICAL (index_type) != index_type))
8097 TYPE_CANONICAL (t)
8098 = build_array_type_1 (TYPE_CANONICAL (elt_type),
8099 index_type
8100 ? TYPE_CANONICAL (index_type) : NULL_TREE,
8101 shared);
8104 return t;
8107 /* Wrapper around build_array_type_1 with SHARED set to true. */
8109 tree
8110 build_array_type (tree elt_type, tree index_type)
8112 return build_array_type_1 (elt_type, index_type, true);
8115 /* Wrapper around build_array_type_1 with SHARED set to false. */
8117 tree
8118 build_nonshared_array_type (tree elt_type, tree index_type)
8120 return build_array_type_1 (elt_type, index_type, false);
8123 /* Return a representation of ELT_TYPE[NELTS], using indices of type
8124 sizetype. */
8126 tree
8127 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
8129 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
8132 /* Recursively examines the array elements of TYPE, until a non-array
8133 element type is found. */
8135 tree
8136 strip_array_types (tree type)
8138 while (TREE_CODE (type) == ARRAY_TYPE)
8139 type = TREE_TYPE (type);
8141 return type;
8144 /* Computes the canonical argument types from the argument type list
8145 ARGTYPES.
8147 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
8148 on entry to this function, or if any of the ARGTYPES are
8149 structural.
8151 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
8152 true on entry to this function, or if any of the ARGTYPES are
8153 non-canonical.
8155 Returns a canonical argument list, which may be ARGTYPES when the
8156 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
8157 true) or would not differ from ARGTYPES. */
8159 static tree
8160 maybe_canonicalize_argtypes (tree argtypes,
8161 bool *any_structural_p,
8162 bool *any_noncanonical_p)
8164 tree arg;
8165 bool any_noncanonical_argtypes_p = false;
8167 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
8169 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
8170 /* Fail gracefully by stating that the type is structural. */
8171 *any_structural_p = true;
8172 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
8173 *any_structural_p = true;
8174 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
8175 || TREE_PURPOSE (arg))
8176 /* If the argument has a default argument, we consider it
8177 non-canonical even though the type itself is canonical.
8178 That way, different variants of function and method types
8179 with default arguments will all point to the variant with
8180 no defaults as their canonical type. */
8181 any_noncanonical_argtypes_p = true;
8184 if (*any_structural_p)
8185 return argtypes;
8187 if (any_noncanonical_argtypes_p)
8189 /* Build the canonical list of argument types. */
8190 tree canon_argtypes = NULL_TREE;
8191 bool is_void = false;
8193 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
8195 if (arg == void_list_node)
8196 is_void = true;
8197 else
8198 canon_argtypes = tree_cons (NULL_TREE,
8199 TYPE_CANONICAL (TREE_VALUE (arg)),
8200 canon_argtypes);
8203 canon_argtypes = nreverse (canon_argtypes);
8204 if (is_void)
8205 canon_argtypes = chainon (canon_argtypes, void_list_node);
8207 /* There is a non-canonical type. */
8208 *any_noncanonical_p = true;
8209 return canon_argtypes;
8212 /* The canonical argument types are the same as ARGTYPES. */
8213 return argtypes;
8216 /* Construct, lay out and return
8217 the type of functions returning type VALUE_TYPE
8218 given arguments of types ARG_TYPES.
8219 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
8220 are data type nodes for the arguments of the function.
8221 If such a type has already been constructed, reuse it. */
8223 tree
8224 build_function_type (tree value_type, tree arg_types)
8226 tree t;
8227 inchash::hash hstate;
8228 bool any_structural_p, any_noncanonical_p;
8229 tree canon_argtypes;
8231 if (TREE_CODE (value_type) == FUNCTION_TYPE)
8233 error ("function return type cannot be function");
8234 value_type = integer_type_node;
8237 /* Make a node of the sort we want. */
8238 t = make_node (FUNCTION_TYPE);
8239 TREE_TYPE (t) = value_type;
8240 TYPE_ARG_TYPES (t) = arg_types;
8242 /* If we already have such a type, use the old one. */
8243 hstate.add_object (TYPE_HASH (value_type));
8244 type_hash_list (arg_types, hstate);
8245 t = type_hash_canon (hstate.end (), t);
8247 /* Set up the canonical type. */
8248 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
8249 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
8250 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
8251 &any_structural_p,
8252 &any_noncanonical_p);
8253 if (any_structural_p)
8254 SET_TYPE_STRUCTURAL_EQUALITY (t);
8255 else if (any_noncanonical_p)
8256 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
8257 canon_argtypes);
8259 if (!COMPLETE_TYPE_P (t))
8260 layout_type (t);
8261 return t;
8264 /* Build a function type. The RETURN_TYPE is the type returned by the
8265 function. If VAARGS is set, no void_type_node is appended to the
8266 the list. ARGP must be always be terminated be a NULL_TREE. */
8268 static tree
8269 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
8271 tree t, args, last;
8273 t = va_arg (argp, tree);
8274 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
8275 args = tree_cons (NULL_TREE, t, args);
8277 if (vaargs)
8279 last = args;
8280 if (args != NULL_TREE)
8281 args = nreverse (args);
8282 gcc_assert (last != void_list_node);
8284 else if (args == NULL_TREE)
8285 args = void_list_node;
8286 else
8288 last = args;
8289 args = nreverse (args);
8290 TREE_CHAIN (last) = void_list_node;
8292 args = build_function_type (return_type, args);
8294 return args;
8297 /* Build a function type. The RETURN_TYPE is the type returned by the
8298 function. If additional arguments are provided, they are
8299 additional argument types. The list of argument types must always
8300 be terminated by NULL_TREE. */
8302 tree
8303 build_function_type_list (tree return_type, ...)
8305 tree args;
8306 va_list p;
8308 va_start (p, return_type);
8309 args = build_function_type_list_1 (false, return_type, p);
8310 va_end (p);
8311 return args;
8314 /* Build a variable argument function type. The RETURN_TYPE is the
8315 type returned by the function. If additional arguments are provided,
8316 they are additional argument types. The list of argument types must
8317 always be terminated by NULL_TREE. */
8319 tree
8320 build_varargs_function_type_list (tree return_type, ...)
8322 tree args;
8323 va_list p;
8325 va_start (p, return_type);
8326 args = build_function_type_list_1 (true, return_type, p);
8327 va_end (p);
8329 return args;
8332 /* Build a function type. RETURN_TYPE is the type returned by the
8333 function; VAARGS indicates whether the function takes varargs. The
8334 function takes N named arguments, the types of which are provided in
8335 ARG_TYPES. */
8337 static tree
8338 build_function_type_array_1 (bool vaargs, tree return_type, int n,
8339 tree *arg_types)
8341 int i;
8342 tree t = vaargs ? NULL_TREE : void_list_node;
8344 for (i = n - 1; i >= 0; i--)
8345 t = tree_cons (NULL_TREE, arg_types[i], t);
8347 return build_function_type (return_type, t);
8350 /* Build a function type. RETURN_TYPE is the type returned by the
8351 function. The function takes N named arguments, the types of which
8352 are provided in ARG_TYPES. */
8354 tree
8355 build_function_type_array (tree return_type, int n, tree *arg_types)
8357 return build_function_type_array_1 (false, return_type, n, arg_types);
8360 /* Build a variable argument function type. RETURN_TYPE is the type
8361 returned by the function. The function takes N named arguments, the
8362 types of which are provided in ARG_TYPES. */
8364 tree
8365 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8367 return build_function_type_array_1 (true, return_type, n, arg_types);
8370 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8371 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8372 for the method. An implicit additional parameter (of type
8373 pointer-to-BASETYPE) is added to the ARGTYPES. */
8375 tree
8376 build_method_type_directly (tree basetype,
8377 tree rettype,
8378 tree argtypes)
8380 tree t;
8381 tree ptype;
8382 inchash::hash hstate;
8383 bool any_structural_p, any_noncanonical_p;
8384 tree canon_argtypes;
8386 /* Make a node of the sort we want. */
8387 t = make_node (METHOD_TYPE);
8389 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8390 TREE_TYPE (t) = rettype;
8391 ptype = build_pointer_type (basetype);
8393 /* The actual arglist for this function includes a "hidden" argument
8394 which is "this". Put it into the list of argument types. */
8395 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8396 TYPE_ARG_TYPES (t) = argtypes;
8398 /* If we already have such a type, use the old one. */
8399 hstate.add_object (TYPE_HASH (basetype));
8400 hstate.add_object (TYPE_HASH (rettype));
8401 type_hash_list (argtypes, hstate);
8402 t = type_hash_canon (hstate.end (), t);
8404 /* Set up the canonical type. */
8405 any_structural_p
8406 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8407 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8408 any_noncanonical_p
8409 = (TYPE_CANONICAL (basetype) != basetype
8410 || TYPE_CANONICAL (rettype) != rettype);
8411 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8412 &any_structural_p,
8413 &any_noncanonical_p);
8414 if (any_structural_p)
8415 SET_TYPE_STRUCTURAL_EQUALITY (t);
8416 else if (any_noncanonical_p)
8417 TYPE_CANONICAL (t)
8418 = build_method_type_directly (TYPE_CANONICAL (basetype),
8419 TYPE_CANONICAL (rettype),
8420 canon_argtypes);
8421 if (!COMPLETE_TYPE_P (t))
8422 layout_type (t);
8424 return t;
8427 /* Construct, lay out and return the type of methods belonging to class
8428 BASETYPE and whose arguments and values are described by TYPE.
8429 If that type exists already, reuse it.
8430 TYPE must be a FUNCTION_TYPE node. */
8432 tree
8433 build_method_type (tree basetype, tree type)
8435 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8437 return build_method_type_directly (basetype,
8438 TREE_TYPE (type),
8439 TYPE_ARG_TYPES (type));
8442 /* Construct, lay out and return the type of offsets to a value
8443 of type TYPE, within an object of type BASETYPE.
8444 If a suitable offset type exists already, reuse it. */
8446 tree
8447 build_offset_type (tree basetype, tree type)
8449 tree t;
8450 inchash::hash hstate;
8452 /* Make a node of the sort we want. */
8453 t = make_node (OFFSET_TYPE);
8455 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8456 TREE_TYPE (t) = type;
8458 /* If we already have such a type, use the old one. */
8459 hstate.add_object (TYPE_HASH (basetype));
8460 hstate.add_object (TYPE_HASH (type));
8461 t = type_hash_canon (hstate.end (), t);
8463 if (!COMPLETE_TYPE_P (t))
8464 layout_type (t);
8466 if (TYPE_CANONICAL (t) == t)
8468 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8469 || TYPE_STRUCTURAL_EQUALITY_P (type))
8470 SET_TYPE_STRUCTURAL_EQUALITY (t);
8471 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8472 || TYPE_CANONICAL (type) != type)
8473 TYPE_CANONICAL (t)
8474 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8475 TYPE_CANONICAL (type));
8478 return t;
8481 /* Create a complex type whose components are COMPONENT_TYPE. */
8483 tree
8484 build_complex_type (tree component_type)
8486 tree t;
8487 inchash::hash hstate;
8489 gcc_assert (INTEGRAL_TYPE_P (component_type)
8490 || SCALAR_FLOAT_TYPE_P (component_type)
8491 || FIXED_POINT_TYPE_P (component_type));
8493 /* Make a node of the sort we want. */
8494 t = make_node (COMPLEX_TYPE);
8496 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8498 /* If we already have such a type, use the old one. */
8499 hstate.add_object (TYPE_HASH (component_type));
8500 t = type_hash_canon (hstate.end (), t);
8502 if (!COMPLETE_TYPE_P (t))
8503 layout_type (t);
8505 if (TYPE_CANONICAL (t) == t)
8507 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8508 SET_TYPE_STRUCTURAL_EQUALITY (t);
8509 else if (TYPE_CANONICAL (component_type) != component_type)
8510 TYPE_CANONICAL (t)
8511 = build_complex_type (TYPE_CANONICAL (component_type));
8514 /* We need to create a name, since complex is a fundamental type. */
8515 if (! TYPE_NAME (t))
8517 const char *name;
8518 if (component_type == char_type_node)
8519 name = "complex char";
8520 else if (component_type == signed_char_type_node)
8521 name = "complex signed char";
8522 else if (component_type == unsigned_char_type_node)
8523 name = "complex unsigned char";
8524 else if (component_type == short_integer_type_node)
8525 name = "complex short int";
8526 else if (component_type == short_unsigned_type_node)
8527 name = "complex short unsigned int";
8528 else if (component_type == integer_type_node)
8529 name = "complex int";
8530 else if (component_type == unsigned_type_node)
8531 name = "complex unsigned int";
8532 else if (component_type == long_integer_type_node)
8533 name = "complex long int";
8534 else if (component_type == long_unsigned_type_node)
8535 name = "complex long unsigned int";
8536 else if (component_type == long_long_integer_type_node)
8537 name = "complex long long int";
8538 else if (component_type == long_long_unsigned_type_node)
8539 name = "complex long long unsigned int";
8540 else
8541 name = 0;
8543 if (name != 0)
8544 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8545 get_identifier (name), t);
8548 return build_qualified_type (t, TYPE_QUALS (component_type));
8551 /* If TYPE is a real or complex floating-point type and the target
8552 does not directly support arithmetic on TYPE then return the wider
8553 type to be used for arithmetic on TYPE. Otherwise, return
8554 NULL_TREE. */
8556 tree
8557 excess_precision_type (tree type)
8559 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8561 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8562 switch (TREE_CODE (type))
8564 case REAL_TYPE:
8565 switch (flt_eval_method)
8567 case 1:
8568 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8569 return double_type_node;
8570 break;
8571 case 2:
8572 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8573 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8574 return long_double_type_node;
8575 break;
8576 default:
8577 gcc_unreachable ();
8579 break;
8580 case COMPLEX_TYPE:
8581 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8582 return NULL_TREE;
8583 switch (flt_eval_method)
8585 case 1:
8586 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8587 return complex_double_type_node;
8588 break;
8589 case 2:
8590 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8591 || (TYPE_MODE (TREE_TYPE (type))
8592 == TYPE_MODE (double_type_node)))
8593 return complex_long_double_type_node;
8594 break;
8595 default:
8596 gcc_unreachable ();
8598 break;
8599 default:
8600 break;
8603 return NULL_TREE;
8606 /* Return OP, stripped of any conversions to wider types as much as is safe.
8607 Converting the value back to OP's type makes a value equivalent to OP.
8609 If FOR_TYPE is nonzero, we return a value which, if converted to
8610 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8612 OP must have integer, real or enumeral type. Pointers are not allowed!
8614 There are some cases where the obvious value we could return
8615 would regenerate to OP if converted to OP's type,
8616 but would not extend like OP to wider types.
8617 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8618 For example, if OP is (unsigned short)(signed char)-1,
8619 we avoid returning (signed char)-1 if FOR_TYPE is int,
8620 even though extending that to an unsigned short would regenerate OP,
8621 since the result of extending (signed char)-1 to (int)
8622 is different from (int) OP. */
8624 tree
8625 get_unwidened (tree op, tree for_type)
8627 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8628 tree type = TREE_TYPE (op);
8629 unsigned final_prec
8630 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8631 int uns
8632 = (for_type != 0 && for_type != type
8633 && final_prec > TYPE_PRECISION (type)
8634 && TYPE_UNSIGNED (type));
8635 tree win = op;
8637 while (CONVERT_EXPR_P (op))
8639 int bitschange;
8641 /* TYPE_PRECISION on vector types has different meaning
8642 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8643 so avoid them here. */
8644 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8645 break;
8647 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8648 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8650 /* Truncations are many-one so cannot be removed.
8651 Unless we are later going to truncate down even farther. */
8652 if (bitschange < 0
8653 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8654 break;
8656 /* See what's inside this conversion. If we decide to strip it,
8657 we will set WIN. */
8658 op = TREE_OPERAND (op, 0);
8660 /* If we have not stripped any zero-extensions (uns is 0),
8661 we can strip any kind of extension.
8662 If we have previously stripped a zero-extension,
8663 only zero-extensions can safely be stripped.
8664 Any extension can be stripped if the bits it would produce
8665 are all going to be discarded later by truncating to FOR_TYPE. */
8667 if (bitschange > 0)
8669 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8670 win = op;
8671 /* TYPE_UNSIGNED says whether this is a zero-extension.
8672 Let's avoid computing it if it does not affect WIN
8673 and if UNS will not be needed again. */
8674 if ((uns
8675 || CONVERT_EXPR_P (op))
8676 && TYPE_UNSIGNED (TREE_TYPE (op)))
8678 uns = 1;
8679 win = op;
8684 /* If we finally reach a constant see if it fits in for_type and
8685 in that case convert it. */
8686 if (for_type
8687 && TREE_CODE (win) == INTEGER_CST
8688 && TREE_TYPE (win) != for_type
8689 && int_fits_type_p (win, for_type))
8690 win = fold_convert (for_type, win);
8692 return win;
8695 /* Return OP or a simpler expression for a narrower value
8696 which can be sign-extended or zero-extended to give back OP.
8697 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8698 or 0 if the value should be sign-extended. */
8700 tree
8701 get_narrower (tree op, int *unsignedp_ptr)
8703 int uns = 0;
8704 int first = 1;
8705 tree win = op;
8706 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8708 while (TREE_CODE (op) == NOP_EXPR)
8710 int bitschange
8711 = (TYPE_PRECISION (TREE_TYPE (op))
8712 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8714 /* Truncations are many-one so cannot be removed. */
8715 if (bitschange < 0)
8716 break;
8718 /* See what's inside this conversion. If we decide to strip it,
8719 we will set WIN. */
8721 if (bitschange > 0)
8723 op = TREE_OPERAND (op, 0);
8724 /* An extension: the outermost one can be stripped,
8725 but remember whether it is zero or sign extension. */
8726 if (first)
8727 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8728 /* Otherwise, if a sign extension has been stripped,
8729 only sign extensions can now be stripped;
8730 if a zero extension has been stripped, only zero-extensions. */
8731 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8732 break;
8733 first = 0;
8735 else /* bitschange == 0 */
8737 /* A change in nominal type can always be stripped, but we must
8738 preserve the unsignedness. */
8739 if (first)
8740 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8741 first = 0;
8742 op = TREE_OPERAND (op, 0);
8743 /* Keep trying to narrow, but don't assign op to win if it
8744 would turn an integral type into something else. */
8745 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8746 continue;
8749 win = op;
8752 if (TREE_CODE (op) == COMPONENT_REF
8753 /* Since type_for_size always gives an integer type. */
8754 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8755 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8756 /* Ensure field is laid out already. */
8757 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8758 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
8760 unsigned HOST_WIDE_INT innerprec
8761 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
8762 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8763 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8764 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8766 /* We can get this structure field in a narrower type that fits it,
8767 but the resulting extension to its nominal type (a fullword type)
8768 must satisfy the same conditions as for other extensions.
8770 Do this only for fields that are aligned (not bit-fields),
8771 because when bit-field insns will be used there is no
8772 advantage in doing this. */
8774 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8775 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8776 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8777 && type != 0)
8779 if (first)
8780 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8781 win = fold_convert (type, op);
8785 *unsignedp_ptr = uns;
8786 return win;
8789 /* Returns true if integer constant C has a value that is permissible
8790 for type TYPE (an INTEGER_TYPE). */
8792 bool
8793 int_fits_type_p (const_tree c, const_tree type)
8795 tree type_low_bound, type_high_bound;
8796 bool ok_for_low_bound, ok_for_high_bound;
8797 signop sgn_c = TYPE_SIGN (TREE_TYPE (c));
8799 retry:
8800 type_low_bound = TYPE_MIN_VALUE (type);
8801 type_high_bound = TYPE_MAX_VALUE (type);
8803 /* If at least one bound of the type is a constant integer, we can check
8804 ourselves and maybe make a decision. If no such decision is possible, but
8805 this type is a subtype, try checking against that. Otherwise, use
8806 fits_to_tree_p, which checks against the precision.
8808 Compute the status for each possibly constant bound, and return if we see
8809 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8810 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8811 for "constant known to fit". */
8813 /* Check if c >= type_low_bound. */
8814 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8816 if (tree_int_cst_lt (c, type_low_bound))
8817 return false;
8818 ok_for_low_bound = true;
8820 else
8821 ok_for_low_bound = false;
8823 /* Check if c <= type_high_bound. */
8824 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8826 if (tree_int_cst_lt (type_high_bound, c))
8827 return false;
8828 ok_for_high_bound = true;
8830 else
8831 ok_for_high_bound = false;
8833 /* If the constant fits both bounds, the result is known. */
8834 if (ok_for_low_bound && ok_for_high_bound)
8835 return true;
8837 /* Perform some generic filtering which may allow making a decision
8838 even if the bounds are not constant. First, negative integers
8839 never fit in unsigned types, */
8840 if (TYPE_UNSIGNED (type) && sgn_c == SIGNED && wi::neg_p (c))
8841 return false;
8843 /* Second, narrower types always fit in wider ones. */
8844 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8845 return true;
8847 /* Third, unsigned integers with top bit set never fit signed types. */
8848 if (!TYPE_UNSIGNED (type) && sgn_c == UNSIGNED)
8850 int prec = GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (c))) - 1;
8851 if (prec < TYPE_PRECISION (TREE_TYPE (c)))
8853 /* When a tree_cst is converted to a wide-int, the precision
8854 is taken from the type. However, if the precision of the
8855 mode underneath the type is smaller than that, it is
8856 possible that the value will not fit. The test below
8857 fails if any bit is set between the sign bit of the
8858 underlying mode and the top bit of the type. */
8859 if (wi::ne_p (wi::zext (c, prec - 1), c))
8860 return false;
8862 else if (wi::neg_p (c))
8863 return false;
8866 /* If we haven't been able to decide at this point, there nothing more we
8867 can check ourselves here. Look at the base type if we have one and it
8868 has the same precision. */
8869 if (TREE_CODE (type) == INTEGER_TYPE
8870 && TREE_TYPE (type) != 0
8871 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8873 type = TREE_TYPE (type);
8874 goto retry;
8877 /* Or to fits_to_tree_p, if nothing else. */
8878 return wi::fits_to_tree_p (c, type);
8881 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8882 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8883 represented (assuming two's-complement arithmetic) within the bit
8884 precision of the type are returned instead. */
8886 void
8887 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8889 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8890 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8891 wi::to_mpz (TYPE_MIN_VALUE (type), min, TYPE_SIGN (type));
8892 else
8894 if (TYPE_UNSIGNED (type))
8895 mpz_set_ui (min, 0);
8896 else
8898 wide_int mn = wi::min_value (TYPE_PRECISION (type), SIGNED);
8899 wi::to_mpz (mn, min, SIGNED);
8903 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8904 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8905 wi::to_mpz (TYPE_MAX_VALUE (type), max, TYPE_SIGN (type));
8906 else
8908 wide_int mn = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
8909 wi::to_mpz (mn, max, TYPE_SIGN (type));
8913 /* Return true if VAR is an automatic variable defined in function FN. */
8915 bool
8916 auto_var_in_fn_p (const_tree var, const_tree fn)
8918 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8919 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8920 || TREE_CODE (var) == PARM_DECL)
8921 && ! TREE_STATIC (var))
8922 || TREE_CODE (var) == LABEL_DECL
8923 || TREE_CODE (var) == RESULT_DECL));
8926 /* Subprogram of following function. Called by walk_tree.
8928 Return *TP if it is an automatic variable or parameter of the
8929 function passed in as DATA. */
8931 static tree
8932 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8934 tree fn = (tree) data;
8936 if (TYPE_P (*tp))
8937 *walk_subtrees = 0;
8939 else if (DECL_P (*tp)
8940 && auto_var_in_fn_p (*tp, fn))
8941 return *tp;
8943 return NULL_TREE;
8946 /* Returns true if T is, contains, or refers to a type with variable
8947 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8948 arguments, but not the return type. If FN is nonzero, only return
8949 true if a modifier of the type or position of FN is a variable or
8950 parameter inside FN.
8952 This concept is more general than that of C99 'variably modified types':
8953 in C99, a struct type is never variably modified because a VLA may not
8954 appear as a structure member. However, in GNU C code like:
8956 struct S { int i[f()]; };
8958 is valid, and other languages may define similar constructs. */
8960 bool
8961 variably_modified_type_p (tree type, tree fn)
8963 tree t;
8965 /* Test if T is either variable (if FN is zero) or an expression containing
8966 a variable in FN. If TYPE isn't gimplified, return true also if
8967 gimplify_one_sizepos would gimplify the expression into a local
8968 variable. */
8969 #define RETURN_TRUE_IF_VAR(T) \
8970 do { tree _t = (T); \
8971 if (_t != NULL_TREE \
8972 && _t != error_mark_node \
8973 && TREE_CODE (_t) != INTEGER_CST \
8974 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8975 && (!fn \
8976 || (!TYPE_SIZES_GIMPLIFIED (type) \
8977 && !is_gimple_sizepos (_t)) \
8978 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8979 return true; } while (0)
8981 if (type == error_mark_node)
8982 return false;
8984 /* If TYPE itself has variable size, it is variably modified. */
8985 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8986 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8988 switch (TREE_CODE (type))
8990 case POINTER_TYPE:
8991 case REFERENCE_TYPE:
8992 case VECTOR_TYPE:
8993 if (variably_modified_type_p (TREE_TYPE (type), fn))
8994 return true;
8995 break;
8997 case FUNCTION_TYPE:
8998 case METHOD_TYPE:
8999 /* If TYPE is a function type, it is variably modified if the
9000 return type is variably modified. */
9001 if (variably_modified_type_p (TREE_TYPE (type), fn))
9002 return true;
9003 break;
9005 case INTEGER_TYPE:
9006 case REAL_TYPE:
9007 case FIXED_POINT_TYPE:
9008 case ENUMERAL_TYPE:
9009 case BOOLEAN_TYPE:
9010 /* Scalar types are variably modified if their end points
9011 aren't constant. */
9012 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
9013 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
9014 break;
9016 case RECORD_TYPE:
9017 case UNION_TYPE:
9018 case QUAL_UNION_TYPE:
9019 /* We can't see if any of the fields are variably-modified by the
9020 definition we normally use, since that would produce infinite
9021 recursion via pointers. */
9022 /* This is variably modified if some field's type is. */
9023 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
9024 if (TREE_CODE (t) == FIELD_DECL)
9026 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
9027 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
9028 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
9030 if (TREE_CODE (type) == QUAL_UNION_TYPE)
9031 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
9033 break;
9035 case ARRAY_TYPE:
9036 /* Do not call ourselves to avoid infinite recursion. This is
9037 variably modified if the element type is. */
9038 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
9039 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
9040 break;
9042 default:
9043 break;
9046 /* The current language may have other cases to check, but in general,
9047 all other types are not variably modified. */
9048 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
9050 #undef RETURN_TRUE_IF_VAR
9053 /* Given a DECL or TYPE, return the scope in which it was declared, or
9054 NULL_TREE if there is no containing scope. */
9056 tree
9057 get_containing_scope (const_tree t)
9059 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
9062 /* Return the innermost context enclosing DECL that is
9063 a FUNCTION_DECL, or zero if none. */
9065 tree
9066 decl_function_context (const_tree decl)
9068 tree context;
9070 if (TREE_CODE (decl) == ERROR_MARK)
9071 return 0;
9073 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
9074 where we look up the function at runtime. Such functions always take
9075 a first argument of type 'pointer to real context'.
9077 C++ should really be fixed to use DECL_CONTEXT for the real context,
9078 and use something else for the "virtual context". */
9079 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
9080 context
9081 = TYPE_MAIN_VARIANT
9082 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
9083 else
9084 context = DECL_CONTEXT (decl);
9086 while (context && TREE_CODE (context) != FUNCTION_DECL)
9088 if (TREE_CODE (context) == BLOCK)
9089 context = BLOCK_SUPERCONTEXT (context);
9090 else
9091 context = get_containing_scope (context);
9094 return context;
9097 /* Return the innermost context enclosing DECL that is
9098 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
9099 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
9101 tree
9102 decl_type_context (const_tree decl)
9104 tree context = DECL_CONTEXT (decl);
9106 while (context)
9107 switch (TREE_CODE (context))
9109 case NAMESPACE_DECL:
9110 case TRANSLATION_UNIT_DECL:
9111 return NULL_TREE;
9113 case RECORD_TYPE:
9114 case UNION_TYPE:
9115 case QUAL_UNION_TYPE:
9116 return context;
9118 case TYPE_DECL:
9119 case FUNCTION_DECL:
9120 context = DECL_CONTEXT (context);
9121 break;
9123 case BLOCK:
9124 context = BLOCK_SUPERCONTEXT (context);
9125 break;
9127 default:
9128 gcc_unreachable ();
9131 return NULL_TREE;
9134 /* CALL is a CALL_EXPR. Return the declaration for the function
9135 called, or NULL_TREE if the called function cannot be
9136 determined. */
9138 tree
9139 get_callee_fndecl (const_tree call)
9141 tree addr;
9143 if (call == error_mark_node)
9144 return error_mark_node;
9146 /* It's invalid to call this function with anything but a
9147 CALL_EXPR. */
9148 gcc_assert (TREE_CODE (call) == CALL_EXPR);
9150 /* The first operand to the CALL is the address of the function
9151 called. */
9152 addr = CALL_EXPR_FN (call);
9154 /* If there is no function, return early. */
9155 if (addr == NULL_TREE)
9156 return NULL_TREE;
9158 STRIP_NOPS (addr);
9160 /* If this is a readonly function pointer, extract its initial value. */
9161 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
9162 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
9163 && DECL_INITIAL (addr))
9164 addr = DECL_INITIAL (addr);
9166 /* If the address is just `&f' for some function `f', then we know
9167 that `f' is being called. */
9168 if (TREE_CODE (addr) == ADDR_EXPR
9169 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
9170 return TREE_OPERAND (addr, 0);
9172 /* We couldn't figure out what was being called. */
9173 return NULL_TREE;
9176 #define TREE_MEM_USAGE_SPACES 40
9178 /* Print debugging information about tree nodes generated during the compile,
9179 and any language-specific information. */
9181 void
9182 dump_tree_statistics (void)
9184 if (GATHER_STATISTICS)
9186 int i;
9187 int total_nodes, total_bytes;
9188 fprintf (stderr, "\nKind Nodes Bytes\n");
9189 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9190 total_nodes = total_bytes = 0;
9191 for (i = 0; i < (int) all_kinds; i++)
9193 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
9194 tree_node_counts[i], tree_node_sizes[i]);
9195 total_nodes += tree_node_counts[i];
9196 total_bytes += tree_node_sizes[i];
9198 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9199 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
9200 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9201 fprintf (stderr, "Code Nodes\n");
9202 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9203 for (i = 0; i < (int) MAX_TREE_CODES; i++)
9204 fprintf (stderr, "%-32s %7d\n", get_tree_code_name ((enum tree_code) i),
9205 tree_code_counts[i]);
9206 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9207 fprintf (stderr, "\n");
9208 ssanames_print_statistics ();
9209 fprintf (stderr, "\n");
9210 phinodes_print_statistics ();
9211 fprintf (stderr, "\n");
9213 else
9214 fprintf (stderr, "(No per-node statistics)\n");
9216 print_type_hash_statistics ();
9217 print_debug_expr_statistics ();
9218 print_value_expr_statistics ();
9219 lang_hooks.print_statistics ();
9222 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
9224 /* Generate a crc32 of a byte. */
9226 static unsigned
9227 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
9229 unsigned ix;
9231 for (ix = bits; ix--; value <<= 1)
9233 unsigned feedback;
9235 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
9236 chksum <<= 1;
9237 chksum ^= feedback;
9239 return chksum;
9242 /* Generate a crc32 of a 32-bit unsigned. */
9244 unsigned
9245 crc32_unsigned (unsigned chksum, unsigned value)
9247 return crc32_unsigned_bits (chksum, value, 32);
9250 /* Generate a crc32 of a byte. */
9252 unsigned
9253 crc32_byte (unsigned chksum, char byte)
9255 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
9258 /* Generate a crc32 of a string. */
9260 unsigned
9261 crc32_string (unsigned chksum, const char *string)
9265 chksum = crc32_byte (chksum, *string);
9267 while (*string++);
9268 return chksum;
9271 /* P is a string that will be used in a symbol. Mask out any characters
9272 that are not valid in that context. */
9274 void
9275 clean_symbol_name (char *p)
9277 for (; *p; p++)
9278 if (! (ISALNUM (*p)
9279 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
9280 || *p == '$'
9281 #endif
9282 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
9283 || *p == '.'
9284 #endif
9286 *p = '_';
9289 /* For anonymous aggregate types, we need some sort of name to
9290 hold on to. In practice, this should not appear, but it should
9291 not be harmful if it does. */
9292 bool
9293 anon_aggrname_p(const_tree id_node)
9295 #ifndef NO_DOT_IN_LABEL
9296 return (IDENTIFIER_POINTER (id_node)[0] == '.'
9297 && IDENTIFIER_POINTER (id_node)[1] == '_');
9298 #else /* NO_DOT_IN_LABEL */
9299 #ifndef NO_DOLLAR_IN_LABEL
9300 return (IDENTIFIER_POINTER (id_node)[0] == '$' \
9301 && IDENTIFIER_POINTER (id_node)[1] == '_');
9302 #else /* NO_DOLLAR_IN_LABEL */
9303 #define ANON_AGGRNAME_PREFIX "__anon_"
9304 return (!strncmp (IDENTIFIER_POINTER (id_node), ANON_AGGRNAME_PREFIX,
9305 sizeof (ANON_AGGRNAME_PREFIX) - 1));
9306 #endif /* NO_DOLLAR_IN_LABEL */
9307 #endif /* NO_DOT_IN_LABEL */
9310 /* Return a format for an anonymous aggregate name. */
9311 const char *
9312 anon_aggrname_format()
9314 #ifndef NO_DOT_IN_LABEL
9315 return "._%d";
9316 #else /* NO_DOT_IN_LABEL */
9317 #ifndef NO_DOLLAR_IN_LABEL
9318 return "$_%d";
9319 #else /* NO_DOLLAR_IN_LABEL */
9320 return "__anon_%d";
9321 #endif /* NO_DOLLAR_IN_LABEL */
9322 #endif /* NO_DOT_IN_LABEL */
9325 /* Generate a name for a special-purpose function.
9326 The generated name may need to be unique across the whole link.
9327 Changes to this function may also require corresponding changes to
9328 xstrdup_mask_random.
9329 TYPE is some string to identify the purpose of this function to the
9330 linker or collect2; it must start with an uppercase letter,
9331 one of:
9332 I - for constructors
9333 D - for destructors
9334 N - for C++ anonymous namespaces
9335 F - for DWARF unwind frame information. */
9337 tree
9338 get_file_function_name (const char *type)
9340 char *buf;
9341 const char *p;
9342 char *q;
9344 /* If we already have a name we know to be unique, just use that. */
9345 if (first_global_object_name)
9346 p = q = ASTRDUP (first_global_object_name);
9347 /* If the target is handling the constructors/destructors, they
9348 will be local to this file and the name is only necessary for
9349 debugging purposes.
9350 We also assign sub_I and sub_D sufixes to constructors called from
9351 the global static constructors. These are always local. */
9352 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9353 || (strncmp (type, "sub_", 4) == 0
9354 && (type[4] == 'I' || type[4] == 'D')))
9356 const char *file = main_input_filename;
9357 if (! file)
9358 file = LOCATION_FILE (input_location);
9359 /* Just use the file's basename, because the full pathname
9360 might be quite long. */
9361 p = q = ASTRDUP (lbasename (file));
9363 else
9365 /* Otherwise, the name must be unique across the entire link.
9366 We don't have anything that we know to be unique to this translation
9367 unit, so use what we do have and throw in some randomness. */
9368 unsigned len;
9369 const char *name = weak_global_object_name;
9370 const char *file = main_input_filename;
9372 if (! name)
9373 name = "";
9374 if (! file)
9375 file = LOCATION_FILE (input_location);
9377 len = strlen (file);
9378 q = (char *) alloca (9 + 17 + len + 1);
9379 memcpy (q, file, len + 1);
9381 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9382 crc32_string (0, name), get_random_seed (false));
9384 p = q;
9387 clean_symbol_name (q);
9388 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9389 + strlen (type));
9391 /* Set up the name of the file-level functions we may need.
9392 Use a global object (which is already required to be unique over
9393 the program) rather than the file name (which imposes extra
9394 constraints). */
9395 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9397 return get_identifier (buf);
9400 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9402 /* Complain that the tree code of NODE does not match the expected 0
9403 terminated list of trailing codes. The trailing code list can be
9404 empty, for a more vague error message. FILE, LINE, and FUNCTION
9405 are of the caller. */
9407 void
9408 tree_check_failed (const_tree node, const char *file,
9409 int line, const char *function, ...)
9411 va_list args;
9412 const char *buffer;
9413 unsigned length = 0;
9414 enum tree_code code;
9416 va_start (args, function);
9417 while ((code = (enum tree_code) va_arg (args, int)))
9418 length += 4 + strlen (get_tree_code_name (code));
9419 va_end (args);
9420 if (length)
9422 char *tmp;
9423 va_start (args, function);
9424 length += strlen ("expected ");
9425 buffer = tmp = (char *) alloca (length);
9426 length = 0;
9427 while ((code = (enum tree_code) va_arg (args, int)))
9429 const char *prefix = length ? " or " : "expected ";
9431 strcpy (tmp + length, prefix);
9432 length += strlen (prefix);
9433 strcpy (tmp + length, get_tree_code_name (code));
9434 length += strlen (get_tree_code_name (code));
9436 va_end (args);
9438 else
9439 buffer = "unexpected node";
9441 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9442 buffer, get_tree_code_name (TREE_CODE (node)),
9443 function, trim_filename (file), line);
9446 /* Complain that the tree code of NODE does match the expected 0
9447 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9448 the caller. */
9450 void
9451 tree_not_check_failed (const_tree node, const char *file,
9452 int line, const char *function, ...)
9454 va_list args;
9455 char *buffer;
9456 unsigned length = 0;
9457 enum tree_code code;
9459 va_start (args, function);
9460 while ((code = (enum tree_code) va_arg (args, int)))
9461 length += 4 + strlen (get_tree_code_name (code));
9462 va_end (args);
9463 va_start (args, function);
9464 buffer = (char *) alloca (length);
9465 length = 0;
9466 while ((code = (enum tree_code) va_arg (args, int)))
9468 if (length)
9470 strcpy (buffer + length, " or ");
9471 length += 4;
9473 strcpy (buffer + length, get_tree_code_name (code));
9474 length += strlen (get_tree_code_name (code));
9476 va_end (args);
9478 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9479 buffer, get_tree_code_name (TREE_CODE (node)),
9480 function, trim_filename (file), line);
9483 /* Similar to tree_check_failed, except that we check for a class of tree
9484 code, given in CL. */
9486 void
9487 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9488 const char *file, int line, const char *function)
9490 internal_error
9491 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9492 TREE_CODE_CLASS_STRING (cl),
9493 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9494 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9497 /* Similar to tree_check_failed, except that instead of specifying a
9498 dozen codes, use the knowledge that they're all sequential. */
9500 void
9501 tree_range_check_failed (const_tree node, const char *file, int line,
9502 const char *function, enum tree_code c1,
9503 enum tree_code c2)
9505 char *buffer;
9506 unsigned length = 0;
9507 unsigned int c;
9509 for (c = c1; c <= c2; ++c)
9510 length += 4 + strlen (get_tree_code_name ((enum tree_code) c));
9512 length += strlen ("expected ");
9513 buffer = (char *) alloca (length);
9514 length = 0;
9516 for (c = c1; c <= c2; ++c)
9518 const char *prefix = length ? " or " : "expected ";
9520 strcpy (buffer + length, prefix);
9521 length += strlen (prefix);
9522 strcpy (buffer + length, get_tree_code_name ((enum tree_code) c));
9523 length += strlen (get_tree_code_name ((enum tree_code) c));
9526 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9527 buffer, get_tree_code_name (TREE_CODE (node)),
9528 function, trim_filename (file), line);
9532 /* Similar to tree_check_failed, except that we check that a tree does
9533 not have the specified code, given in CL. */
9535 void
9536 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9537 const char *file, int line, const char *function)
9539 internal_error
9540 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9541 TREE_CODE_CLASS_STRING (cl),
9542 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9543 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9547 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9549 void
9550 omp_clause_check_failed (const_tree node, const char *file, int line,
9551 const char *function, enum omp_clause_code code)
9553 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9554 omp_clause_code_name[code], get_tree_code_name (TREE_CODE (node)),
9555 function, trim_filename (file), line);
9559 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9561 void
9562 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9563 const char *function, enum omp_clause_code c1,
9564 enum omp_clause_code c2)
9566 char *buffer;
9567 unsigned length = 0;
9568 unsigned int c;
9570 for (c = c1; c <= c2; ++c)
9571 length += 4 + strlen (omp_clause_code_name[c]);
9573 length += strlen ("expected ");
9574 buffer = (char *) alloca (length);
9575 length = 0;
9577 for (c = c1; c <= c2; ++c)
9579 const char *prefix = length ? " or " : "expected ";
9581 strcpy (buffer + length, prefix);
9582 length += strlen (prefix);
9583 strcpy (buffer + length, omp_clause_code_name[c]);
9584 length += strlen (omp_clause_code_name[c]);
9587 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9588 buffer, omp_clause_code_name[TREE_CODE (node)],
9589 function, trim_filename (file), line);
9593 #undef DEFTREESTRUCT
9594 #define DEFTREESTRUCT(VAL, NAME) NAME,
9596 static const char *ts_enum_names[] = {
9597 #include "treestruct.def"
9599 #undef DEFTREESTRUCT
9601 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9603 /* Similar to tree_class_check_failed, except that we check for
9604 whether CODE contains the tree structure identified by EN. */
9606 void
9607 tree_contains_struct_check_failed (const_tree node,
9608 const enum tree_node_structure_enum en,
9609 const char *file, int line,
9610 const char *function)
9612 internal_error
9613 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9614 TS_ENUM_NAME (en),
9615 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9619 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9620 (dynamically sized) vector. */
9622 void
9623 tree_int_cst_elt_check_failed (int idx, int len, const char *file, int line,
9624 const char *function)
9626 internal_error
9627 ("tree check: accessed elt %d of tree_int_cst with %d elts in %s, at %s:%d",
9628 idx + 1, len, function, trim_filename (file), line);
9631 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9632 (dynamically sized) vector. */
9634 void
9635 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9636 const char *function)
9638 internal_error
9639 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9640 idx + 1, len, function, trim_filename (file), line);
9643 /* Similar to above, except that the check is for the bounds of the operand
9644 vector of an expression node EXP. */
9646 void
9647 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9648 int line, const char *function)
9650 enum tree_code code = TREE_CODE (exp);
9651 internal_error
9652 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9653 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9654 function, trim_filename (file), line);
9657 /* Similar to above, except that the check is for the number of
9658 operands of an OMP_CLAUSE node. */
9660 void
9661 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9662 int line, const char *function)
9664 internal_error
9665 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9666 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9667 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9668 trim_filename (file), line);
9670 #endif /* ENABLE_TREE_CHECKING */
9672 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9673 and mapped to the machine mode MODE. Initialize its fields and build
9674 the information necessary for debugging output. */
9676 static tree
9677 make_vector_type (tree innertype, int nunits, machine_mode mode)
9679 tree t;
9680 inchash::hash hstate;
9682 t = make_node (VECTOR_TYPE);
9683 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9684 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9685 SET_TYPE_MODE (t, mode);
9687 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9688 SET_TYPE_STRUCTURAL_EQUALITY (t);
9689 else if (TYPE_CANONICAL (innertype) != innertype
9690 || mode != VOIDmode)
9691 TYPE_CANONICAL (t)
9692 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9694 layout_type (t);
9696 hstate.add_wide_int (VECTOR_TYPE);
9697 hstate.add_wide_int (nunits);
9698 hstate.add_wide_int (mode);
9699 hstate.add_object (TYPE_HASH (TREE_TYPE (t)));
9700 t = type_hash_canon (hstate.end (), t);
9702 /* We have built a main variant, based on the main variant of the
9703 inner type. Use it to build the variant we return. */
9704 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9705 && TREE_TYPE (t) != innertype)
9706 return build_type_attribute_qual_variant (t,
9707 TYPE_ATTRIBUTES (innertype),
9708 TYPE_QUALS (innertype));
9710 return t;
9713 static tree
9714 make_or_reuse_type (unsigned size, int unsignedp)
9716 int i;
9718 if (size == INT_TYPE_SIZE)
9719 return unsignedp ? unsigned_type_node : integer_type_node;
9720 if (size == CHAR_TYPE_SIZE)
9721 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9722 if (size == SHORT_TYPE_SIZE)
9723 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9724 if (size == LONG_TYPE_SIZE)
9725 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9726 if (size == LONG_LONG_TYPE_SIZE)
9727 return (unsignedp ? long_long_unsigned_type_node
9728 : long_long_integer_type_node);
9730 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9731 if (size == int_n_data[i].bitsize
9732 && int_n_enabled_p[i])
9733 return (unsignedp ? int_n_trees[i].unsigned_type
9734 : int_n_trees[i].signed_type);
9736 if (unsignedp)
9737 return make_unsigned_type (size);
9738 else
9739 return make_signed_type (size);
9742 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9744 static tree
9745 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9747 if (satp)
9749 if (size == SHORT_FRACT_TYPE_SIZE)
9750 return unsignedp ? sat_unsigned_short_fract_type_node
9751 : sat_short_fract_type_node;
9752 if (size == FRACT_TYPE_SIZE)
9753 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9754 if (size == LONG_FRACT_TYPE_SIZE)
9755 return unsignedp ? sat_unsigned_long_fract_type_node
9756 : sat_long_fract_type_node;
9757 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9758 return unsignedp ? sat_unsigned_long_long_fract_type_node
9759 : sat_long_long_fract_type_node;
9761 else
9763 if (size == SHORT_FRACT_TYPE_SIZE)
9764 return unsignedp ? unsigned_short_fract_type_node
9765 : short_fract_type_node;
9766 if (size == FRACT_TYPE_SIZE)
9767 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9768 if (size == LONG_FRACT_TYPE_SIZE)
9769 return unsignedp ? unsigned_long_fract_type_node
9770 : long_fract_type_node;
9771 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9772 return unsignedp ? unsigned_long_long_fract_type_node
9773 : long_long_fract_type_node;
9776 return make_fract_type (size, unsignedp, satp);
9779 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9781 static tree
9782 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9784 if (satp)
9786 if (size == SHORT_ACCUM_TYPE_SIZE)
9787 return unsignedp ? sat_unsigned_short_accum_type_node
9788 : sat_short_accum_type_node;
9789 if (size == ACCUM_TYPE_SIZE)
9790 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9791 if (size == LONG_ACCUM_TYPE_SIZE)
9792 return unsignedp ? sat_unsigned_long_accum_type_node
9793 : sat_long_accum_type_node;
9794 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9795 return unsignedp ? sat_unsigned_long_long_accum_type_node
9796 : sat_long_long_accum_type_node;
9798 else
9800 if (size == SHORT_ACCUM_TYPE_SIZE)
9801 return unsignedp ? unsigned_short_accum_type_node
9802 : short_accum_type_node;
9803 if (size == ACCUM_TYPE_SIZE)
9804 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9805 if (size == LONG_ACCUM_TYPE_SIZE)
9806 return unsignedp ? unsigned_long_accum_type_node
9807 : long_accum_type_node;
9808 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9809 return unsignedp ? unsigned_long_long_accum_type_node
9810 : long_long_accum_type_node;
9813 return make_accum_type (size, unsignedp, satp);
9817 /* Create an atomic variant node for TYPE. This routine is called
9818 during initialization of data types to create the 5 basic atomic
9819 types. The generic build_variant_type function requires these to
9820 already be set up in order to function properly, so cannot be
9821 called from there. If ALIGN is non-zero, then ensure alignment is
9822 overridden to this value. */
9824 static tree
9825 build_atomic_base (tree type, unsigned int align)
9827 tree t;
9829 /* Make sure its not already registered. */
9830 if ((t = get_qualified_type (type, TYPE_QUAL_ATOMIC)))
9831 return t;
9833 t = build_variant_type_copy (type);
9834 set_type_quals (t, TYPE_QUAL_ATOMIC);
9836 if (align)
9837 TYPE_ALIGN (t) = align;
9839 return t;
9842 /* Create nodes for all integer types (and error_mark_node) using the sizes
9843 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9844 SHORT_DOUBLE specifies whether double should be of the same precision
9845 as float. */
9847 void
9848 build_common_tree_nodes (bool signed_char, bool short_double)
9850 int i;
9852 error_mark_node = make_node (ERROR_MARK);
9853 TREE_TYPE (error_mark_node) = error_mark_node;
9855 initialize_sizetypes ();
9857 /* Define both `signed char' and `unsigned char'. */
9858 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9859 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9860 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9861 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9863 /* Define `char', which is like either `signed char' or `unsigned char'
9864 but not the same as either. */
9865 char_type_node
9866 = (signed_char
9867 ? make_signed_type (CHAR_TYPE_SIZE)
9868 : make_unsigned_type (CHAR_TYPE_SIZE));
9869 TYPE_STRING_FLAG (char_type_node) = 1;
9871 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9872 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9873 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9874 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9875 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9876 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9877 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9878 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9880 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9882 int_n_trees[i].signed_type = make_signed_type (int_n_data[i].bitsize);
9883 int_n_trees[i].unsigned_type = make_unsigned_type (int_n_data[i].bitsize);
9884 TYPE_SIZE (int_n_trees[i].signed_type) = bitsize_int (int_n_data[i].bitsize);
9885 TYPE_SIZE (int_n_trees[i].unsigned_type) = bitsize_int (int_n_data[i].bitsize);
9887 if (int_n_data[i].bitsize > LONG_LONG_TYPE_SIZE
9888 && int_n_enabled_p[i])
9890 integer_types[itk_intN_0 + i * 2] = int_n_trees[i].signed_type;
9891 integer_types[itk_unsigned_intN_0 + i * 2] = int_n_trees[i].unsigned_type;
9895 /* Define a boolean type. This type only represents boolean values but
9896 may be larger than char depending on the value of BOOL_TYPE_SIZE. */
9897 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9898 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9899 TYPE_PRECISION (boolean_type_node) = 1;
9900 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9902 /* Define what type to use for size_t. */
9903 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9904 size_type_node = unsigned_type_node;
9905 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9906 size_type_node = long_unsigned_type_node;
9907 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9908 size_type_node = long_long_unsigned_type_node;
9909 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9910 size_type_node = short_unsigned_type_node;
9911 else
9913 int i;
9915 size_type_node = NULL_TREE;
9916 for (i = 0; i < NUM_INT_N_ENTS; i++)
9917 if (int_n_enabled_p[i])
9919 char name[50];
9920 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
9922 if (strcmp (name, SIZE_TYPE) == 0)
9924 size_type_node = int_n_trees[i].unsigned_type;
9927 if (size_type_node == NULL_TREE)
9928 gcc_unreachable ();
9931 /* Fill in the rest of the sized types. Reuse existing type nodes
9932 when possible. */
9933 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9934 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9935 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9936 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9937 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9939 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9940 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9941 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9942 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9943 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9945 /* Don't call build_qualified type for atomics. That routine does
9946 special processing for atomics, and until they are initialized
9947 it's better not to make that call.
9949 Check to see if there is a target override for atomic types. */
9951 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
9952 targetm.atomic_align_for_mode (QImode));
9953 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
9954 targetm.atomic_align_for_mode (HImode));
9955 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
9956 targetm.atomic_align_for_mode (SImode));
9957 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
9958 targetm.atomic_align_for_mode (DImode));
9959 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
9960 targetm.atomic_align_for_mode (TImode));
9962 access_public_node = get_identifier ("public");
9963 access_protected_node = get_identifier ("protected");
9964 access_private_node = get_identifier ("private");
9966 /* Define these next since types below may used them. */
9967 integer_zero_node = build_int_cst (integer_type_node, 0);
9968 integer_one_node = build_int_cst (integer_type_node, 1);
9969 integer_three_node = build_int_cst (integer_type_node, 3);
9970 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9972 size_zero_node = size_int (0);
9973 size_one_node = size_int (1);
9974 bitsize_zero_node = bitsize_int (0);
9975 bitsize_one_node = bitsize_int (1);
9976 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9978 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9979 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9981 void_type_node = make_node (VOID_TYPE);
9982 layout_type (void_type_node);
9984 pointer_bounds_type_node = targetm.chkp_bound_type ();
9986 /* We are not going to have real types in C with less than byte alignment,
9987 so we might as well not have any types that claim to have it. */
9988 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9989 TYPE_USER_ALIGN (void_type_node) = 0;
9991 void_node = make_node (VOID_CST);
9992 TREE_TYPE (void_node) = void_type_node;
9994 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9995 layout_type (TREE_TYPE (null_pointer_node));
9997 ptr_type_node = build_pointer_type (void_type_node);
9998 const_ptr_type_node
9999 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
10000 fileptr_type_node = ptr_type_node;
10002 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
10004 float_type_node = make_node (REAL_TYPE);
10005 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
10006 layout_type (float_type_node);
10008 double_type_node = make_node (REAL_TYPE);
10009 if (short_double)
10010 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
10011 else
10012 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
10013 layout_type (double_type_node);
10015 long_double_type_node = make_node (REAL_TYPE);
10016 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
10017 layout_type (long_double_type_node);
10019 float_ptr_type_node = build_pointer_type (float_type_node);
10020 double_ptr_type_node = build_pointer_type (double_type_node);
10021 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
10022 integer_ptr_type_node = build_pointer_type (integer_type_node);
10024 /* Fixed size integer types. */
10025 uint16_type_node = make_or_reuse_type (16, 1);
10026 uint32_type_node = make_or_reuse_type (32, 1);
10027 uint64_type_node = make_or_reuse_type (64, 1);
10029 /* Decimal float types. */
10030 dfloat32_type_node = make_node (REAL_TYPE);
10031 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
10032 layout_type (dfloat32_type_node);
10033 SET_TYPE_MODE (dfloat32_type_node, SDmode);
10034 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
10036 dfloat64_type_node = make_node (REAL_TYPE);
10037 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
10038 layout_type (dfloat64_type_node);
10039 SET_TYPE_MODE (dfloat64_type_node, DDmode);
10040 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
10042 dfloat128_type_node = make_node (REAL_TYPE);
10043 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
10044 layout_type (dfloat128_type_node);
10045 SET_TYPE_MODE (dfloat128_type_node, TDmode);
10046 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
10048 complex_integer_type_node = build_complex_type (integer_type_node);
10049 complex_float_type_node = build_complex_type (float_type_node);
10050 complex_double_type_node = build_complex_type (double_type_node);
10051 complex_long_double_type_node = build_complex_type (long_double_type_node);
10053 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
10054 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
10055 sat_ ## KIND ## _type_node = \
10056 make_sat_signed_ ## KIND ## _type (SIZE); \
10057 sat_unsigned_ ## KIND ## _type_node = \
10058 make_sat_unsigned_ ## KIND ## _type (SIZE); \
10059 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
10060 unsigned_ ## KIND ## _type_node = \
10061 make_unsigned_ ## KIND ## _type (SIZE);
10063 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
10064 sat_ ## WIDTH ## KIND ## _type_node = \
10065 make_sat_signed_ ## KIND ## _type (SIZE); \
10066 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
10067 make_sat_unsigned_ ## KIND ## _type (SIZE); \
10068 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
10069 unsigned_ ## WIDTH ## KIND ## _type_node = \
10070 make_unsigned_ ## KIND ## _type (SIZE);
10072 /* Make fixed-point type nodes based on four different widths. */
10073 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
10074 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
10075 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
10076 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
10077 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
10079 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
10080 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
10081 NAME ## _type_node = \
10082 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
10083 u ## NAME ## _type_node = \
10084 make_or_reuse_unsigned_ ## KIND ## _type \
10085 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
10086 sat_ ## NAME ## _type_node = \
10087 make_or_reuse_sat_signed_ ## KIND ## _type \
10088 (GET_MODE_BITSIZE (MODE ## mode)); \
10089 sat_u ## NAME ## _type_node = \
10090 make_or_reuse_sat_unsigned_ ## KIND ## _type \
10091 (GET_MODE_BITSIZE (U ## MODE ## mode));
10093 /* Fixed-point type and mode nodes. */
10094 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
10095 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
10096 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
10097 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
10098 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
10099 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
10100 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
10101 MAKE_FIXED_MODE_NODE (accum, ha, HA)
10102 MAKE_FIXED_MODE_NODE (accum, sa, SA)
10103 MAKE_FIXED_MODE_NODE (accum, da, DA)
10104 MAKE_FIXED_MODE_NODE (accum, ta, TA)
10107 tree t = targetm.build_builtin_va_list ();
10109 /* Many back-ends define record types without setting TYPE_NAME.
10110 If we copied the record type here, we'd keep the original
10111 record type without a name. This breaks name mangling. So,
10112 don't copy record types and let c_common_nodes_and_builtins()
10113 declare the type to be __builtin_va_list. */
10114 if (TREE_CODE (t) != RECORD_TYPE)
10115 t = build_variant_type_copy (t);
10117 va_list_type_node = t;
10121 /* Modify DECL for given flags.
10122 TM_PURE attribute is set only on types, so the function will modify
10123 DECL's type when ECF_TM_PURE is used. */
10125 void
10126 set_call_expr_flags (tree decl, int flags)
10128 if (flags & ECF_NOTHROW)
10129 TREE_NOTHROW (decl) = 1;
10130 if (flags & ECF_CONST)
10131 TREE_READONLY (decl) = 1;
10132 if (flags & ECF_PURE)
10133 DECL_PURE_P (decl) = 1;
10134 if (flags & ECF_LOOPING_CONST_OR_PURE)
10135 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
10136 if (flags & ECF_NOVOPS)
10137 DECL_IS_NOVOPS (decl) = 1;
10138 if (flags & ECF_NORETURN)
10139 TREE_THIS_VOLATILE (decl) = 1;
10140 if (flags & ECF_MALLOC)
10141 DECL_IS_MALLOC (decl) = 1;
10142 if (flags & ECF_RETURNS_TWICE)
10143 DECL_IS_RETURNS_TWICE (decl) = 1;
10144 if (flags & ECF_LEAF)
10145 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
10146 NULL, DECL_ATTRIBUTES (decl));
10147 if ((flags & ECF_TM_PURE) && flag_tm)
10148 apply_tm_attr (decl, get_identifier ("transaction_pure"));
10149 /* Looping const or pure is implied by noreturn.
10150 There is currently no way to declare looping const or looping pure alone. */
10151 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
10152 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
10156 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
10158 static void
10159 local_define_builtin (const char *name, tree type, enum built_in_function code,
10160 const char *library_name, int ecf_flags)
10162 tree decl;
10164 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
10165 library_name, NULL_TREE);
10166 set_call_expr_flags (decl, ecf_flags);
10168 set_builtin_decl (code, decl, true);
10171 /* Call this function after instantiating all builtins that the language
10172 front end cares about. This will build the rest of the builtins
10173 and internal functions that are relied upon by the tree optimizers and
10174 the middle-end. */
10176 void
10177 build_common_builtin_nodes (void)
10179 tree tmp, ftype;
10180 int ecf_flags;
10182 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
10184 ftype = build_function_type (void_type_node, void_list_node);
10185 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
10186 "__builtin_unreachable",
10187 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
10188 | ECF_CONST);
10191 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
10192 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10194 ftype = build_function_type_list (ptr_type_node,
10195 ptr_type_node, const_ptr_type_node,
10196 size_type_node, NULL_TREE);
10198 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
10199 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
10200 "memcpy", ECF_NOTHROW | ECF_LEAF);
10201 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10202 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
10203 "memmove", ECF_NOTHROW | ECF_LEAF);
10206 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
10208 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
10209 const_ptr_type_node, size_type_node,
10210 NULL_TREE);
10211 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
10212 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10215 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
10217 ftype = build_function_type_list (ptr_type_node,
10218 ptr_type_node, integer_type_node,
10219 size_type_node, NULL_TREE);
10220 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
10221 "memset", ECF_NOTHROW | ECF_LEAF);
10224 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
10226 ftype = build_function_type_list (ptr_type_node,
10227 size_type_node, NULL_TREE);
10228 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
10229 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
10232 ftype = build_function_type_list (ptr_type_node, size_type_node,
10233 size_type_node, NULL_TREE);
10234 local_define_builtin ("__builtin_alloca_with_align", ftype,
10235 BUILT_IN_ALLOCA_WITH_ALIGN,
10236 "__builtin_alloca_with_align",
10237 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
10239 /* If we're checking the stack, `alloca' can throw. */
10240 if (flag_stack_check)
10242 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
10243 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
10246 ftype = build_function_type_list (void_type_node,
10247 ptr_type_node, ptr_type_node,
10248 ptr_type_node, NULL_TREE);
10249 local_define_builtin ("__builtin_init_trampoline", ftype,
10250 BUILT_IN_INIT_TRAMPOLINE,
10251 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
10252 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
10253 BUILT_IN_INIT_HEAP_TRAMPOLINE,
10254 "__builtin_init_heap_trampoline",
10255 ECF_NOTHROW | ECF_LEAF);
10257 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
10258 local_define_builtin ("__builtin_adjust_trampoline", ftype,
10259 BUILT_IN_ADJUST_TRAMPOLINE,
10260 "__builtin_adjust_trampoline",
10261 ECF_CONST | ECF_NOTHROW);
10263 ftype = build_function_type_list (void_type_node,
10264 ptr_type_node, ptr_type_node, NULL_TREE);
10265 local_define_builtin ("__builtin_nonlocal_goto", ftype,
10266 BUILT_IN_NONLOCAL_GOTO,
10267 "__builtin_nonlocal_goto",
10268 ECF_NORETURN | ECF_NOTHROW);
10270 ftype = build_function_type_list (void_type_node,
10271 ptr_type_node, ptr_type_node, NULL_TREE);
10272 local_define_builtin ("__builtin_setjmp_setup", ftype,
10273 BUILT_IN_SETJMP_SETUP,
10274 "__builtin_setjmp_setup", ECF_NOTHROW);
10276 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10277 local_define_builtin ("__builtin_setjmp_receiver", ftype,
10278 BUILT_IN_SETJMP_RECEIVER,
10279 "__builtin_setjmp_receiver", ECF_NOTHROW | ECF_LEAF);
10281 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
10282 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
10283 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
10285 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10286 local_define_builtin ("__builtin_stack_restore", ftype,
10287 BUILT_IN_STACK_RESTORE,
10288 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
10290 /* If there's a possibility that we might use the ARM EABI, build the
10291 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
10292 if (targetm.arm_eabi_unwinder)
10294 ftype = build_function_type_list (void_type_node, NULL_TREE);
10295 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
10296 BUILT_IN_CXA_END_CLEANUP,
10297 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
10300 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10301 local_define_builtin ("__builtin_unwind_resume", ftype,
10302 BUILT_IN_UNWIND_RESUME,
10303 ((targetm_common.except_unwind_info (&global_options)
10304 == UI_SJLJ)
10305 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
10306 ECF_NORETURN);
10308 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
10310 ftype = build_function_type_list (ptr_type_node, integer_type_node,
10311 NULL_TREE);
10312 local_define_builtin ("__builtin_return_address", ftype,
10313 BUILT_IN_RETURN_ADDRESS,
10314 "__builtin_return_address",
10315 ECF_NOTHROW);
10318 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
10319 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10321 ftype = build_function_type_list (void_type_node, ptr_type_node,
10322 ptr_type_node, NULL_TREE);
10323 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
10324 local_define_builtin ("__cyg_profile_func_enter", ftype,
10325 BUILT_IN_PROFILE_FUNC_ENTER,
10326 "__cyg_profile_func_enter", 0);
10327 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10328 local_define_builtin ("__cyg_profile_func_exit", ftype,
10329 BUILT_IN_PROFILE_FUNC_EXIT,
10330 "__cyg_profile_func_exit", 0);
10333 /* The exception object and filter values from the runtime. The argument
10334 must be zero before exception lowering, i.e. from the front end. After
10335 exception lowering, it will be the region number for the exception
10336 landing pad. These functions are PURE instead of CONST to prevent
10337 them from being hoisted past the exception edge that will initialize
10338 its value in the landing pad. */
10339 ftype = build_function_type_list (ptr_type_node,
10340 integer_type_node, NULL_TREE);
10341 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
10342 /* Only use TM_PURE if we we have TM language support. */
10343 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
10344 ecf_flags |= ECF_TM_PURE;
10345 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
10346 "__builtin_eh_pointer", ecf_flags);
10348 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
10349 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
10350 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
10351 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10353 ftype = build_function_type_list (void_type_node,
10354 integer_type_node, integer_type_node,
10355 NULL_TREE);
10356 local_define_builtin ("__builtin_eh_copy_values", ftype,
10357 BUILT_IN_EH_COPY_VALUES,
10358 "__builtin_eh_copy_values", ECF_NOTHROW);
10360 /* Complex multiplication and division. These are handled as builtins
10361 rather than optabs because emit_library_call_value doesn't support
10362 complex. Further, we can do slightly better with folding these
10363 beasties if the real and complex parts of the arguments are separate. */
10365 int mode;
10367 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10369 char mode_name_buf[4], *q;
10370 const char *p;
10371 enum built_in_function mcode, dcode;
10372 tree type, inner_type;
10373 const char *prefix = "__";
10375 if (targetm.libfunc_gnu_prefix)
10376 prefix = "__gnu_";
10378 type = lang_hooks.types.type_for_mode ((machine_mode) mode, 0);
10379 if (type == NULL)
10380 continue;
10381 inner_type = TREE_TYPE (type);
10383 ftype = build_function_type_list (type, inner_type, inner_type,
10384 inner_type, inner_type, NULL_TREE);
10386 mcode = ((enum built_in_function)
10387 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10388 dcode = ((enum built_in_function)
10389 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10391 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10392 *q = TOLOWER (*p);
10393 *q = '\0';
10395 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10396 NULL);
10397 local_define_builtin (built_in_names[mcode], ftype, mcode,
10398 built_in_names[mcode],
10399 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10401 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10402 NULL);
10403 local_define_builtin (built_in_names[dcode], ftype, dcode,
10404 built_in_names[dcode],
10405 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10409 init_internal_fns ();
10412 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10413 better way.
10415 If we requested a pointer to a vector, build up the pointers that
10416 we stripped off while looking for the inner type. Similarly for
10417 return values from functions.
10419 The argument TYPE is the top of the chain, and BOTTOM is the
10420 new type which we will point to. */
10422 tree
10423 reconstruct_complex_type (tree type, tree bottom)
10425 tree inner, outer;
10427 if (TREE_CODE (type) == POINTER_TYPE)
10429 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10430 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10431 TYPE_REF_CAN_ALIAS_ALL (type));
10433 else if (TREE_CODE (type) == REFERENCE_TYPE)
10435 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10436 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10437 TYPE_REF_CAN_ALIAS_ALL (type));
10439 else if (TREE_CODE (type) == ARRAY_TYPE)
10441 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10442 outer = build_array_type (inner, TYPE_DOMAIN (type));
10444 else if (TREE_CODE (type) == FUNCTION_TYPE)
10446 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10447 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
10449 else if (TREE_CODE (type) == METHOD_TYPE)
10451 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10452 /* The build_method_type_directly() routine prepends 'this' to argument list,
10453 so we must compensate by getting rid of it. */
10454 outer
10455 = build_method_type_directly
10456 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10457 inner,
10458 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10460 else if (TREE_CODE (type) == OFFSET_TYPE)
10462 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10463 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10465 else
10466 return bottom;
10468 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10469 TYPE_QUALS (type));
10472 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10473 the inner type. */
10474 tree
10475 build_vector_type_for_mode (tree innertype, machine_mode mode)
10477 int nunits;
10479 switch (GET_MODE_CLASS (mode))
10481 case MODE_VECTOR_INT:
10482 case MODE_VECTOR_FLOAT:
10483 case MODE_VECTOR_FRACT:
10484 case MODE_VECTOR_UFRACT:
10485 case MODE_VECTOR_ACCUM:
10486 case MODE_VECTOR_UACCUM:
10487 nunits = GET_MODE_NUNITS (mode);
10488 break;
10490 case MODE_INT:
10491 /* Check that there are no leftover bits. */
10492 gcc_assert (GET_MODE_BITSIZE (mode)
10493 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10495 nunits = GET_MODE_BITSIZE (mode)
10496 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10497 break;
10499 default:
10500 gcc_unreachable ();
10503 return make_vector_type (innertype, nunits, mode);
10506 /* Similarly, but takes the inner type and number of units, which must be
10507 a power of two. */
10509 tree
10510 build_vector_type (tree innertype, int nunits)
10512 return make_vector_type (innertype, nunits, VOIDmode);
10515 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10517 tree
10518 build_opaque_vector_type (tree innertype, int nunits)
10520 tree t = make_vector_type (innertype, nunits, VOIDmode);
10521 tree cand;
10522 /* We always build the non-opaque variant before the opaque one,
10523 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10524 cand = TYPE_NEXT_VARIANT (t);
10525 if (cand
10526 && TYPE_VECTOR_OPAQUE (cand)
10527 && check_qualified_type (cand, t, TYPE_QUALS (t)))
10528 return cand;
10529 /* Othewise build a variant type and make sure to queue it after
10530 the non-opaque type. */
10531 cand = build_distinct_type_copy (t);
10532 TYPE_VECTOR_OPAQUE (cand) = true;
10533 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10534 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10535 TYPE_NEXT_VARIANT (t) = cand;
10536 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10537 return cand;
10541 /* Given an initializer INIT, return TRUE if INIT is zero or some
10542 aggregate of zeros. Otherwise return FALSE. */
10543 bool
10544 initializer_zerop (const_tree init)
10546 tree elt;
10548 STRIP_NOPS (init);
10550 switch (TREE_CODE (init))
10552 case INTEGER_CST:
10553 return integer_zerop (init);
10555 case REAL_CST:
10556 /* ??? Note that this is not correct for C4X float formats. There,
10557 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10558 negative exponent. */
10559 return real_zerop (init)
10560 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10562 case FIXED_CST:
10563 return fixed_zerop (init);
10565 case COMPLEX_CST:
10566 return integer_zerop (init)
10567 || (real_zerop (init)
10568 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10569 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10571 case VECTOR_CST:
10573 unsigned i;
10574 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10575 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10576 return false;
10577 return true;
10580 case CONSTRUCTOR:
10582 unsigned HOST_WIDE_INT idx;
10584 if (TREE_CLOBBER_P (init))
10585 return false;
10586 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10587 if (!initializer_zerop (elt))
10588 return false;
10589 return true;
10592 case STRING_CST:
10594 int i;
10596 /* We need to loop through all elements to handle cases like
10597 "\0" and "\0foobar". */
10598 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10599 if (TREE_STRING_POINTER (init)[i] != '\0')
10600 return false;
10602 return true;
10605 default:
10606 return false;
10610 /* Check if vector VEC consists of all the equal elements and
10611 that the number of elements corresponds to the type of VEC.
10612 The function returns first element of the vector
10613 or NULL_TREE if the vector is not uniform. */
10614 tree
10615 uniform_vector_p (const_tree vec)
10617 tree first, t;
10618 unsigned i;
10620 if (vec == NULL_TREE)
10621 return NULL_TREE;
10623 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10625 if (TREE_CODE (vec) == VECTOR_CST)
10627 first = VECTOR_CST_ELT (vec, 0);
10628 for (i = 1; i < VECTOR_CST_NELTS (vec); ++i)
10629 if (!operand_equal_p (first, VECTOR_CST_ELT (vec, i), 0))
10630 return NULL_TREE;
10632 return first;
10635 else if (TREE_CODE (vec) == CONSTRUCTOR)
10637 first = error_mark_node;
10639 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10641 if (i == 0)
10643 first = t;
10644 continue;
10646 if (!operand_equal_p (first, t, 0))
10647 return NULL_TREE;
10649 if (i != TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)))
10650 return NULL_TREE;
10652 return first;
10655 return NULL_TREE;
10658 /* Build an empty statement at location LOC. */
10660 tree
10661 build_empty_stmt (location_t loc)
10663 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10664 SET_EXPR_LOCATION (t, loc);
10665 return t;
10669 /* Build an OpenMP clause with code CODE. LOC is the location of the
10670 clause. */
10672 tree
10673 build_omp_clause (location_t loc, enum omp_clause_code code)
10675 tree t;
10676 int size, length;
10678 length = omp_clause_num_ops[code];
10679 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10681 record_node_allocation_statistics (OMP_CLAUSE, size);
10683 t = (tree) ggc_internal_alloc (size);
10684 memset (t, 0, size);
10685 TREE_SET_CODE (t, OMP_CLAUSE);
10686 OMP_CLAUSE_SET_CODE (t, code);
10687 OMP_CLAUSE_LOCATION (t) = loc;
10689 return t;
10692 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10693 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10694 Except for the CODE and operand count field, other storage for the
10695 object is initialized to zeros. */
10697 tree
10698 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10700 tree t;
10701 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10703 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10704 gcc_assert (len >= 1);
10706 record_node_allocation_statistics (code, length);
10708 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10710 TREE_SET_CODE (t, code);
10712 /* Can't use TREE_OPERAND to store the length because if checking is
10713 enabled, it will try to check the length before we store it. :-P */
10714 t->exp.operands[0] = build_int_cst (sizetype, len);
10716 return t;
10719 /* Helper function for build_call_* functions; build a CALL_EXPR with
10720 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10721 the argument slots. */
10723 static tree
10724 build_call_1 (tree return_type, tree fn, int nargs)
10726 tree t;
10728 t = build_vl_exp (CALL_EXPR, nargs + 3);
10729 TREE_TYPE (t) = return_type;
10730 CALL_EXPR_FN (t) = fn;
10731 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10733 return t;
10736 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10737 FN and a null static chain slot. NARGS is the number of call arguments
10738 which are specified as "..." arguments. */
10740 tree
10741 build_call_nary (tree return_type, tree fn, int nargs, ...)
10743 tree ret;
10744 va_list args;
10745 va_start (args, nargs);
10746 ret = build_call_valist (return_type, fn, nargs, args);
10747 va_end (args);
10748 return ret;
10751 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10752 FN and a null static chain slot. NARGS is the number of call arguments
10753 which are specified as a va_list ARGS. */
10755 tree
10756 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10758 tree t;
10759 int i;
10761 t = build_call_1 (return_type, fn, nargs);
10762 for (i = 0; i < nargs; i++)
10763 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10764 process_call_operands (t);
10765 return t;
10768 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10769 FN and a null static chain slot. NARGS is the number of call arguments
10770 which are specified as a tree array ARGS. */
10772 tree
10773 build_call_array_loc (location_t loc, tree return_type, tree fn,
10774 int nargs, const tree *args)
10776 tree t;
10777 int i;
10779 t = build_call_1 (return_type, fn, nargs);
10780 for (i = 0; i < nargs; i++)
10781 CALL_EXPR_ARG (t, i) = args[i];
10782 process_call_operands (t);
10783 SET_EXPR_LOCATION (t, loc);
10784 return t;
10787 /* Like build_call_array, but takes a vec. */
10789 tree
10790 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10792 tree ret, t;
10793 unsigned int ix;
10795 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10796 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10797 CALL_EXPR_ARG (ret, ix) = t;
10798 process_call_operands (ret);
10799 return ret;
10802 /* Conveniently construct a function call expression. FNDECL names the
10803 function to be called and N arguments are passed in the array
10804 ARGARRAY. */
10806 tree
10807 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
10809 tree fntype = TREE_TYPE (fndecl);
10810 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10812 return fold_build_call_array_loc (loc, TREE_TYPE (fntype), fn, n, argarray);
10815 /* Conveniently construct a function call expression. FNDECL names the
10816 function to be called and the arguments are passed in the vector
10817 VEC. */
10819 tree
10820 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
10822 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
10823 vec_safe_address (vec));
10827 /* Conveniently construct a function call expression. FNDECL names the
10828 function to be called, N is the number of arguments, and the "..."
10829 parameters are the argument expressions. */
10831 tree
10832 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
10834 va_list ap;
10835 tree *argarray = XALLOCAVEC (tree, n);
10836 int i;
10838 va_start (ap, n);
10839 for (i = 0; i < n; i++)
10840 argarray[i] = va_arg (ap, tree);
10841 va_end (ap);
10842 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10845 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
10846 varargs macros aren't supported by all bootstrap compilers. */
10848 tree
10849 build_call_expr (tree fndecl, int n, ...)
10851 va_list ap;
10852 tree *argarray = XALLOCAVEC (tree, n);
10853 int i;
10855 va_start (ap, n);
10856 for (i = 0; i < n; i++)
10857 argarray[i] = va_arg (ap, tree);
10858 va_end (ap);
10859 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
10862 /* Build internal call expression. This is just like CALL_EXPR, except
10863 its CALL_EXPR_FN is NULL. It will get gimplified later into ordinary
10864 internal function. */
10866 tree
10867 build_call_expr_internal_loc (location_t loc, enum internal_fn ifn,
10868 tree type, int n, ...)
10870 va_list ap;
10871 int i;
10873 tree fn = build_call_1 (type, NULL_TREE, n);
10874 va_start (ap, n);
10875 for (i = 0; i < n; i++)
10876 CALL_EXPR_ARG (fn, i) = va_arg (ap, tree);
10877 va_end (ap);
10878 SET_EXPR_LOCATION (fn, loc);
10879 CALL_EXPR_IFN (fn) = ifn;
10880 return fn;
10883 /* Create a new constant string literal and return a char* pointer to it.
10884 The STRING_CST value is the LEN characters at STR. */
10885 tree
10886 build_string_literal (int len, const char *str)
10888 tree t, elem, index, type;
10890 t = build_string (len, str);
10891 elem = build_type_variant (char_type_node, 1, 0);
10892 index = build_index_type (size_int (len - 1));
10893 type = build_array_type (elem, index);
10894 TREE_TYPE (t) = type;
10895 TREE_CONSTANT (t) = 1;
10896 TREE_READONLY (t) = 1;
10897 TREE_STATIC (t) = 1;
10899 type = build_pointer_type (elem);
10900 t = build1 (ADDR_EXPR, type,
10901 build4 (ARRAY_REF, elem,
10902 t, integer_zero_node, NULL_TREE, NULL_TREE));
10903 return t;
10908 /* Return true if T (assumed to be a DECL) must be assigned a memory
10909 location. */
10911 bool
10912 needs_to_live_in_memory (const_tree t)
10914 return (TREE_ADDRESSABLE (t)
10915 || is_global_var (t)
10916 || (TREE_CODE (t) == RESULT_DECL
10917 && !DECL_BY_REFERENCE (t)
10918 && aggregate_value_p (t, current_function_decl)));
10921 /* Return value of a constant X and sign-extend it. */
10923 HOST_WIDE_INT
10924 int_cst_value (const_tree x)
10926 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10927 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10929 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10930 gcc_assert (cst_and_fits_in_hwi (x));
10932 if (bits < HOST_BITS_PER_WIDE_INT)
10934 bool negative = ((val >> (bits - 1)) & 1) != 0;
10935 if (negative)
10936 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10937 else
10938 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10941 return val;
10944 /* If TYPE is an integral or pointer type, return an integer type with
10945 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10946 if TYPE is already an integer type of signedness UNSIGNEDP. */
10948 tree
10949 signed_or_unsigned_type_for (int unsignedp, tree type)
10951 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10952 return type;
10954 if (TREE_CODE (type) == VECTOR_TYPE)
10956 tree inner = TREE_TYPE (type);
10957 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10958 if (!inner2)
10959 return NULL_TREE;
10960 if (inner == inner2)
10961 return type;
10962 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10965 if (!INTEGRAL_TYPE_P (type)
10966 && !POINTER_TYPE_P (type)
10967 && TREE_CODE (type) != OFFSET_TYPE)
10968 return NULL_TREE;
10970 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10973 /* If TYPE is an integral or pointer type, return an integer type with
10974 the same precision which is unsigned, or itself if TYPE is already an
10975 unsigned integer type. */
10977 tree
10978 unsigned_type_for (tree type)
10980 return signed_or_unsigned_type_for (1, type);
10983 /* If TYPE is an integral or pointer type, return an integer type with
10984 the same precision which is signed, or itself if TYPE is already a
10985 signed integer type. */
10987 tree
10988 signed_type_for (tree type)
10990 return signed_or_unsigned_type_for (0, type);
10993 /* If TYPE is a vector type, return a signed integer vector type with the
10994 same width and number of subparts. Otherwise return boolean_type_node. */
10996 tree
10997 truth_type_for (tree type)
10999 if (TREE_CODE (type) == VECTOR_TYPE)
11001 tree elem = lang_hooks.types.type_for_size
11002 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
11003 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
11005 else
11006 return boolean_type_node;
11009 /* Returns the largest value obtainable by casting something in INNER type to
11010 OUTER type. */
11012 tree
11013 upper_bound_in_type (tree outer, tree inner)
11015 unsigned int det = 0;
11016 unsigned oprec = TYPE_PRECISION (outer);
11017 unsigned iprec = TYPE_PRECISION (inner);
11018 unsigned prec;
11020 /* Compute a unique number for every combination. */
11021 det |= (oprec > iprec) ? 4 : 0;
11022 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
11023 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
11025 /* Determine the exponent to use. */
11026 switch (det)
11028 case 0:
11029 case 1:
11030 /* oprec <= iprec, outer: signed, inner: don't care. */
11031 prec = oprec - 1;
11032 break;
11033 case 2:
11034 case 3:
11035 /* oprec <= iprec, outer: unsigned, inner: don't care. */
11036 prec = oprec;
11037 break;
11038 case 4:
11039 /* oprec > iprec, outer: signed, inner: signed. */
11040 prec = iprec - 1;
11041 break;
11042 case 5:
11043 /* oprec > iprec, outer: signed, inner: unsigned. */
11044 prec = iprec;
11045 break;
11046 case 6:
11047 /* oprec > iprec, outer: unsigned, inner: signed. */
11048 prec = oprec;
11049 break;
11050 case 7:
11051 /* oprec > iprec, outer: unsigned, inner: unsigned. */
11052 prec = iprec;
11053 break;
11054 default:
11055 gcc_unreachable ();
11058 return wide_int_to_tree (outer,
11059 wi::mask (prec, false, TYPE_PRECISION (outer)));
11062 /* Returns the smallest value obtainable by casting something in INNER type to
11063 OUTER type. */
11065 tree
11066 lower_bound_in_type (tree outer, tree inner)
11068 unsigned oprec = TYPE_PRECISION (outer);
11069 unsigned iprec = TYPE_PRECISION (inner);
11071 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
11072 and obtain 0. */
11073 if (TYPE_UNSIGNED (outer)
11074 /* If we are widening something of an unsigned type, OUTER type
11075 contains all values of INNER type. In particular, both INNER
11076 and OUTER types have zero in common. */
11077 || (oprec > iprec && TYPE_UNSIGNED (inner)))
11078 return build_int_cst (outer, 0);
11079 else
11081 /* If we are widening a signed type to another signed type, we
11082 want to obtain -2^^(iprec-1). If we are keeping the
11083 precision or narrowing to a signed type, we want to obtain
11084 -2^(oprec-1). */
11085 unsigned prec = oprec > iprec ? iprec : oprec;
11086 return wide_int_to_tree (outer,
11087 wi::mask (prec - 1, true,
11088 TYPE_PRECISION (outer)));
11092 /* Return nonzero if two operands that are suitable for PHI nodes are
11093 necessarily equal. Specifically, both ARG0 and ARG1 must be either
11094 SSA_NAME or invariant. Note that this is strictly an optimization.
11095 That is, callers of this function can directly call operand_equal_p
11096 and get the same result, only slower. */
11099 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
11101 if (arg0 == arg1)
11102 return 1;
11103 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
11104 return 0;
11105 return operand_equal_p (arg0, arg1, 0);
11108 /* Returns number of zeros at the end of binary representation of X. */
11110 tree
11111 num_ending_zeros (const_tree x)
11113 return build_int_cst (TREE_TYPE (x), wi::ctz (x));
11117 #define WALK_SUBTREE(NODE) \
11118 do \
11120 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
11121 if (result) \
11122 return result; \
11124 while (0)
11126 /* This is a subroutine of walk_tree that walks field of TYPE that are to
11127 be walked whenever a type is seen in the tree. Rest of operands and return
11128 value are as for walk_tree. */
11130 static tree
11131 walk_type_fields (tree type, walk_tree_fn func, void *data,
11132 hash_set<tree> *pset, walk_tree_lh lh)
11134 tree result = NULL_TREE;
11136 switch (TREE_CODE (type))
11138 case POINTER_TYPE:
11139 case REFERENCE_TYPE:
11140 case VECTOR_TYPE:
11141 /* We have to worry about mutually recursive pointers. These can't
11142 be written in C. They can in Ada. It's pathological, but
11143 there's an ACATS test (c38102a) that checks it. Deal with this
11144 by checking if we're pointing to another pointer, that one
11145 points to another pointer, that one does too, and we have no htab.
11146 If so, get a hash table. We check three levels deep to avoid
11147 the cost of the hash table if we don't need one. */
11148 if (POINTER_TYPE_P (TREE_TYPE (type))
11149 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
11150 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
11151 && !pset)
11153 result = walk_tree_without_duplicates (&TREE_TYPE (type),
11154 func, data);
11155 if (result)
11156 return result;
11158 break;
11161 /* ... fall through ... */
11163 case COMPLEX_TYPE:
11164 WALK_SUBTREE (TREE_TYPE (type));
11165 break;
11167 case METHOD_TYPE:
11168 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
11170 /* Fall through. */
11172 case FUNCTION_TYPE:
11173 WALK_SUBTREE (TREE_TYPE (type));
11175 tree arg;
11177 /* We never want to walk into default arguments. */
11178 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
11179 WALK_SUBTREE (TREE_VALUE (arg));
11181 break;
11183 case ARRAY_TYPE:
11184 /* Don't follow this nodes's type if a pointer for fear that
11185 we'll have infinite recursion. If we have a PSET, then we
11186 need not fear. */
11187 if (pset
11188 || (!POINTER_TYPE_P (TREE_TYPE (type))
11189 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
11190 WALK_SUBTREE (TREE_TYPE (type));
11191 WALK_SUBTREE (TYPE_DOMAIN (type));
11192 break;
11194 case OFFSET_TYPE:
11195 WALK_SUBTREE (TREE_TYPE (type));
11196 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
11197 break;
11199 default:
11200 break;
11203 return NULL_TREE;
11206 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
11207 called with the DATA and the address of each sub-tree. If FUNC returns a
11208 non-NULL value, the traversal is stopped, and the value returned by FUNC
11209 is returned. If PSET is non-NULL it is used to record the nodes visited,
11210 and to avoid visiting a node more than once. */
11212 tree
11213 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
11214 hash_set<tree> *pset, walk_tree_lh lh)
11216 enum tree_code code;
11217 int walk_subtrees;
11218 tree result;
11220 #define WALK_SUBTREE_TAIL(NODE) \
11221 do \
11223 tp = & (NODE); \
11224 goto tail_recurse; \
11226 while (0)
11228 tail_recurse:
11229 /* Skip empty subtrees. */
11230 if (!*tp)
11231 return NULL_TREE;
11233 /* Don't walk the same tree twice, if the user has requested
11234 that we avoid doing so. */
11235 if (pset && pset->add (*tp))
11236 return NULL_TREE;
11238 /* Call the function. */
11239 walk_subtrees = 1;
11240 result = (*func) (tp, &walk_subtrees, data);
11242 /* If we found something, return it. */
11243 if (result)
11244 return result;
11246 code = TREE_CODE (*tp);
11248 /* Even if we didn't, FUNC may have decided that there was nothing
11249 interesting below this point in the tree. */
11250 if (!walk_subtrees)
11252 /* But we still need to check our siblings. */
11253 if (code == TREE_LIST)
11254 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11255 else if (code == OMP_CLAUSE)
11256 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11257 else
11258 return NULL_TREE;
11261 if (lh)
11263 result = (*lh) (tp, &walk_subtrees, func, data, pset);
11264 if (result || !walk_subtrees)
11265 return result;
11268 switch (code)
11270 case ERROR_MARK:
11271 case IDENTIFIER_NODE:
11272 case INTEGER_CST:
11273 case REAL_CST:
11274 case FIXED_CST:
11275 case VECTOR_CST:
11276 case STRING_CST:
11277 case BLOCK:
11278 case PLACEHOLDER_EXPR:
11279 case SSA_NAME:
11280 case FIELD_DECL:
11281 case RESULT_DECL:
11282 /* None of these have subtrees other than those already walked
11283 above. */
11284 break;
11286 case TREE_LIST:
11287 WALK_SUBTREE (TREE_VALUE (*tp));
11288 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11289 break;
11291 case TREE_VEC:
11293 int len = TREE_VEC_LENGTH (*tp);
11295 if (len == 0)
11296 break;
11298 /* Walk all elements but the first. */
11299 while (--len)
11300 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
11302 /* Now walk the first one as a tail call. */
11303 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
11306 case COMPLEX_CST:
11307 WALK_SUBTREE (TREE_REALPART (*tp));
11308 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
11310 case CONSTRUCTOR:
11312 unsigned HOST_WIDE_INT idx;
11313 constructor_elt *ce;
11315 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*tp), idx, &ce);
11316 idx++)
11317 WALK_SUBTREE (ce->value);
11319 break;
11321 case SAVE_EXPR:
11322 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
11324 case BIND_EXPR:
11326 tree decl;
11327 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
11329 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11330 into declarations that are just mentioned, rather than
11331 declared; they don't really belong to this part of the tree.
11332 And, we can see cycles: the initializer for a declaration
11333 can refer to the declaration itself. */
11334 WALK_SUBTREE (DECL_INITIAL (decl));
11335 WALK_SUBTREE (DECL_SIZE (decl));
11336 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11338 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
11341 case STATEMENT_LIST:
11343 tree_stmt_iterator i;
11344 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
11345 WALK_SUBTREE (*tsi_stmt_ptr (i));
11347 break;
11349 case OMP_CLAUSE:
11350 switch (OMP_CLAUSE_CODE (*tp))
11352 case OMP_CLAUSE_GANG:
11353 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11354 /* FALLTHRU */
11356 case OMP_CLAUSE_DEVICE_RESIDENT:
11357 case OMP_CLAUSE_USE_DEVICE:
11358 case OMP_CLAUSE_ASYNC:
11359 case OMP_CLAUSE_WAIT:
11360 case OMP_CLAUSE_WORKER:
11361 case OMP_CLAUSE_VECTOR:
11362 case OMP_CLAUSE_NUM_GANGS:
11363 case OMP_CLAUSE_NUM_WORKERS:
11364 case OMP_CLAUSE_VECTOR_LENGTH:
11365 case OMP_CLAUSE_PRIVATE:
11366 case OMP_CLAUSE_SHARED:
11367 case OMP_CLAUSE_FIRSTPRIVATE:
11368 case OMP_CLAUSE_COPYIN:
11369 case OMP_CLAUSE_COPYPRIVATE:
11370 case OMP_CLAUSE_FINAL:
11371 case OMP_CLAUSE_IF:
11372 case OMP_CLAUSE_NUM_THREADS:
11373 case OMP_CLAUSE_SCHEDULE:
11374 case OMP_CLAUSE_UNIFORM:
11375 case OMP_CLAUSE_DEPEND:
11376 case OMP_CLAUSE_NUM_TEAMS:
11377 case OMP_CLAUSE_THREAD_LIMIT:
11378 case OMP_CLAUSE_DEVICE:
11379 case OMP_CLAUSE_DIST_SCHEDULE:
11380 case OMP_CLAUSE_SAFELEN:
11381 case OMP_CLAUSE_SIMDLEN:
11382 case OMP_CLAUSE__LOOPTEMP_:
11383 case OMP_CLAUSE__SIMDUID_:
11384 case OMP_CLAUSE__CILK_FOR_COUNT_:
11385 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
11386 /* FALLTHRU */
11388 case OMP_CLAUSE_INDEPENDENT:
11389 case OMP_CLAUSE_NOWAIT:
11390 case OMP_CLAUSE_ORDERED:
11391 case OMP_CLAUSE_DEFAULT:
11392 case OMP_CLAUSE_UNTIED:
11393 case OMP_CLAUSE_MERGEABLE:
11394 case OMP_CLAUSE_PROC_BIND:
11395 case OMP_CLAUSE_INBRANCH:
11396 case OMP_CLAUSE_NOTINBRANCH:
11397 case OMP_CLAUSE_FOR:
11398 case OMP_CLAUSE_PARALLEL:
11399 case OMP_CLAUSE_SECTIONS:
11400 case OMP_CLAUSE_TASKGROUP:
11401 case OMP_CLAUSE_AUTO:
11402 case OMP_CLAUSE_SEQ:
11403 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11405 case OMP_CLAUSE_LASTPRIVATE:
11406 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11407 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
11408 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11410 case OMP_CLAUSE_COLLAPSE:
11412 int i;
11413 for (i = 0; i < 3; i++)
11414 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11415 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11418 case OMP_CLAUSE_LINEAR:
11419 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11420 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STEP (*tp));
11421 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STMT (*tp));
11422 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11424 case OMP_CLAUSE_ALIGNED:
11425 case OMP_CLAUSE_FROM:
11426 case OMP_CLAUSE_TO:
11427 case OMP_CLAUSE_MAP:
11428 case OMP_CLAUSE__CACHE_:
11429 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11430 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11431 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11433 case OMP_CLAUSE_REDUCTION:
11435 int i;
11436 for (i = 0; i < 4; i++)
11437 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11438 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11441 default:
11442 gcc_unreachable ();
11444 break;
11446 case TARGET_EXPR:
11448 int i, len;
11450 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11451 But, we only want to walk once. */
11452 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11453 for (i = 0; i < len; ++i)
11454 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11455 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11458 case DECL_EXPR:
11459 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11460 defining. We only want to walk into these fields of a type in this
11461 case and not in the general case of a mere reference to the type.
11463 The criterion is as follows: if the field can be an expression, it
11464 must be walked only here. This should be in keeping with the fields
11465 that are directly gimplified in gimplify_type_sizes in order for the
11466 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11467 variable-sized types.
11469 Note that DECLs get walked as part of processing the BIND_EXPR. */
11470 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11472 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11473 if (TREE_CODE (*type_p) == ERROR_MARK)
11474 return NULL_TREE;
11476 /* Call the function for the type. See if it returns anything or
11477 doesn't want us to continue. If we are to continue, walk both
11478 the normal fields and those for the declaration case. */
11479 result = (*func) (type_p, &walk_subtrees, data);
11480 if (result || !walk_subtrees)
11481 return result;
11483 /* But do not walk a pointed-to type since it may itself need to
11484 be walked in the declaration case if it isn't anonymous. */
11485 if (!POINTER_TYPE_P (*type_p))
11487 result = walk_type_fields (*type_p, func, data, pset, lh);
11488 if (result)
11489 return result;
11492 /* If this is a record type, also walk the fields. */
11493 if (RECORD_OR_UNION_TYPE_P (*type_p))
11495 tree field;
11497 for (field = TYPE_FIELDS (*type_p); field;
11498 field = DECL_CHAIN (field))
11500 /* We'd like to look at the type of the field, but we can
11501 easily get infinite recursion. So assume it's pointed
11502 to elsewhere in the tree. Also, ignore things that
11503 aren't fields. */
11504 if (TREE_CODE (field) != FIELD_DECL)
11505 continue;
11507 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11508 WALK_SUBTREE (DECL_SIZE (field));
11509 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11510 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11511 WALK_SUBTREE (DECL_QUALIFIER (field));
11515 /* Same for scalar types. */
11516 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11517 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11518 || TREE_CODE (*type_p) == INTEGER_TYPE
11519 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11520 || TREE_CODE (*type_p) == REAL_TYPE)
11522 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11523 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11526 WALK_SUBTREE (TYPE_SIZE (*type_p));
11527 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11529 /* FALLTHRU */
11531 default:
11532 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11534 int i, len;
11536 /* Walk over all the sub-trees of this operand. */
11537 len = TREE_OPERAND_LENGTH (*tp);
11539 /* Go through the subtrees. We need to do this in forward order so
11540 that the scope of a FOR_EXPR is handled properly. */
11541 if (len)
11543 for (i = 0; i < len - 1; ++i)
11544 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11545 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11548 /* If this is a type, walk the needed fields in the type. */
11549 else if (TYPE_P (*tp))
11550 return walk_type_fields (*tp, func, data, pset, lh);
11551 break;
11554 /* We didn't find what we were looking for. */
11555 return NULL_TREE;
11557 #undef WALK_SUBTREE_TAIL
11559 #undef WALK_SUBTREE
11561 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11563 tree
11564 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11565 walk_tree_lh lh)
11567 tree result;
11569 hash_set<tree> pset;
11570 result = walk_tree_1 (tp, func, data, &pset, lh);
11571 return result;
11575 tree
11576 tree_block (tree t)
11578 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11580 if (IS_EXPR_CODE_CLASS (c))
11581 return LOCATION_BLOCK (t->exp.locus);
11582 gcc_unreachable ();
11583 return NULL;
11586 void
11587 tree_set_block (tree t, tree b)
11589 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11591 if (IS_EXPR_CODE_CLASS (c))
11593 if (b)
11594 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
11595 else
11596 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
11598 else
11599 gcc_unreachable ();
11602 /* Create a nameless artificial label and put it in the current
11603 function context. The label has a location of LOC. Returns the
11604 newly created label. */
11606 tree
11607 create_artificial_label (location_t loc)
11609 tree lab = build_decl (loc,
11610 LABEL_DECL, NULL_TREE, void_type_node);
11612 DECL_ARTIFICIAL (lab) = 1;
11613 DECL_IGNORED_P (lab) = 1;
11614 DECL_CONTEXT (lab) = current_function_decl;
11615 return lab;
11618 /* Given a tree, try to return a useful variable name that we can use
11619 to prefix a temporary that is being assigned the value of the tree.
11620 I.E. given <temp> = &A, return A. */
11622 const char *
11623 get_name (tree t)
11625 tree stripped_decl;
11627 stripped_decl = t;
11628 STRIP_NOPS (stripped_decl);
11629 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11630 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11631 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11633 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11634 if (!name)
11635 return NULL;
11636 return IDENTIFIER_POINTER (name);
11638 else
11640 switch (TREE_CODE (stripped_decl))
11642 case ADDR_EXPR:
11643 return get_name (TREE_OPERAND (stripped_decl, 0));
11644 default:
11645 return NULL;
11650 /* Return true if TYPE has a variable argument list. */
11652 bool
11653 stdarg_p (const_tree fntype)
11655 function_args_iterator args_iter;
11656 tree n = NULL_TREE, t;
11658 if (!fntype)
11659 return false;
11661 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
11663 n = t;
11666 return n != NULL_TREE && n != void_type_node;
11669 /* Return true if TYPE has a prototype. */
11671 bool
11672 prototype_p (const_tree fntype)
11674 tree t;
11676 gcc_assert (fntype != NULL_TREE);
11678 t = TYPE_ARG_TYPES (fntype);
11679 return (t != NULL_TREE);
11682 /* If BLOCK is inlined from an __attribute__((__artificial__))
11683 routine, return pointer to location from where it has been
11684 called. */
11685 location_t *
11686 block_nonartificial_location (tree block)
11688 location_t *ret = NULL;
11690 while (block && TREE_CODE (block) == BLOCK
11691 && BLOCK_ABSTRACT_ORIGIN (block))
11693 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11695 while (TREE_CODE (ao) == BLOCK
11696 && BLOCK_ABSTRACT_ORIGIN (ao)
11697 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11698 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11700 if (TREE_CODE (ao) == FUNCTION_DECL)
11702 /* If AO is an artificial inline, point RET to the
11703 call site locus at which it has been inlined and continue
11704 the loop, in case AO's caller is also an artificial
11705 inline. */
11706 if (DECL_DECLARED_INLINE_P (ao)
11707 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11708 ret = &BLOCK_SOURCE_LOCATION (block);
11709 else
11710 break;
11712 else if (TREE_CODE (ao) != BLOCK)
11713 break;
11715 block = BLOCK_SUPERCONTEXT (block);
11717 return ret;
11721 /* If EXP is inlined from an __attribute__((__artificial__))
11722 function, return the location of the original call expression. */
11724 location_t
11725 tree_nonartificial_location (tree exp)
11727 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11729 if (loc)
11730 return *loc;
11731 else
11732 return EXPR_LOCATION (exp);
11736 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11737 nodes. */
11739 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11741 hashval_t
11742 cl_option_hasher::hash (tree x)
11744 const_tree const t = x;
11745 const char *p;
11746 size_t i;
11747 size_t len = 0;
11748 hashval_t hash = 0;
11750 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11752 p = (const char *)TREE_OPTIMIZATION (t);
11753 len = sizeof (struct cl_optimization);
11756 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11757 return cl_target_option_hash (TREE_TARGET_OPTION (t));
11759 else
11760 gcc_unreachable ();
11762 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11763 something else. */
11764 for (i = 0; i < len; i++)
11765 if (p[i])
11766 hash = (hash << 4) ^ ((i << 2) | p[i]);
11768 return hash;
11771 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11772 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11773 same. */
11775 bool
11776 cl_option_hasher::equal (tree x, tree y)
11778 const_tree const xt = x;
11779 const_tree const yt = y;
11780 const char *xp;
11781 const char *yp;
11782 size_t len;
11784 if (TREE_CODE (xt) != TREE_CODE (yt))
11785 return 0;
11787 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11789 xp = (const char *)TREE_OPTIMIZATION (xt);
11790 yp = (const char *)TREE_OPTIMIZATION (yt);
11791 len = sizeof (struct cl_optimization);
11794 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11796 return cl_target_option_eq (TREE_TARGET_OPTION (xt),
11797 TREE_TARGET_OPTION (yt));
11800 else
11801 gcc_unreachable ();
11803 return (memcmp (xp, yp, len) == 0);
11806 /* Build an OPTIMIZATION_NODE based on the options in OPTS. */
11808 tree
11809 build_optimization_node (struct gcc_options *opts)
11811 tree t;
11813 /* Use the cache of optimization nodes. */
11815 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11816 opts);
11818 tree *slot = cl_option_hash_table->find_slot (cl_optimization_node, INSERT);
11819 t = *slot;
11820 if (!t)
11822 /* Insert this one into the hash table. */
11823 t = cl_optimization_node;
11824 *slot = t;
11826 /* Make a new node for next time round. */
11827 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11830 return t;
11833 /* Build a TARGET_OPTION_NODE based on the options in OPTS. */
11835 tree
11836 build_target_option_node (struct gcc_options *opts)
11838 tree t;
11840 /* Use the cache of optimization nodes. */
11842 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11843 opts);
11845 tree *slot = cl_option_hash_table->find_slot (cl_target_option_node, INSERT);
11846 t = *slot;
11847 if (!t)
11849 /* Insert this one into the hash table. */
11850 t = cl_target_option_node;
11851 *slot = t;
11853 /* Make a new node for next time round. */
11854 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11857 return t;
11860 /* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
11861 so that they aren't saved during PCH writing. */
11863 void
11864 prepare_target_option_nodes_for_pch (void)
11866 hash_table<cl_option_hasher>::iterator iter = cl_option_hash_table->begin ();
11867 for (; iter != cl_option_hash_table->end (); ++iter)
11868 if (TREE_CODE (*iter) == TARGET_OPTION_NODE)
11869 TREE_TARGET_GLOBALS (*iter) = NULL;
11872 /* Determine the "ultimate origin" of a block. The block may be an inlined
11873 instance of an inlined instance of a block which is local to an inline
11874 function, so we have to trace all of the way back through the origin chain
11875 to find out what sort of node actually served as the original seed for the
11876 given block. */
11878 tree
11879 block_ultimate_origin (const_tree block)
11881 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11883 /* BLOCK_ABSTRACT_ORIGIN can point to itself; ignore that if
11884 we're trying to output the abstract instance of this function. */
11885 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11886 return NULL_TREE;
11888 if (immediate_origin == NULL_TREE)
11889 return NULL_TREE;
11890 else
11892 tree ret_val;
11893 tree lookahead = immediate_origin;
11897 ret_val = lookahead;
11898 lookahead = (TREE_CODE (ret_val) == BLOCK
11899 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11901 while (lookahead != NULL && lookahead != ret_val);
11903 /* The block's abstract origin chain may not be the *ultimate* origin of
11904 the block. It could lead to a DECL that has an abstract origin set.
11905 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11906 will give us if it has one). Note that DECL's abstract origins are
11907 supposed to be the most distant ancestor (or so decl_ultimate_origin
11908 claims), so we don't need to loop following the DECL origins. */
11909 if (DECL_P (ret_val))
11910 return DECL_ORIGIN (ret_val);
11912 return ret_val;
11916 /* Return true iff conversion from INNER_TYPE to OUTER_TYPE generates
11917 no instruction. */
11919 bool
11920 tree_nop_conversion_p (const_tree outer_type, const_tree inner_type)
11922 /* Use precision rather then machine mode when we can, which gives
11923 the correct answer even for submode (bit-field) types. */
11924 if ((INTEGRAL_TYPE_P (outer_type)
11925 || POINTER_TYPE_P (outer_type)
11926 || TREE_CODE (outer_type) == OFFSET_TYPE)
11927 && (INTEGRAL_TYPE_P (inner_type)
11928 || POINTER_TYPE_P (inner_type)
11929 || TREE_CODE (inner_type) == OFFSET_TYPE))
11930 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11932 /* Otherwise fall back on comparing machine modes (e.g. for
11933 aggregate types, floats). */
11934 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11937 /* Return true iff conversion in EXP generates no instruction. Mark
11938 it inline so that we fully inline into the stripping functions even
11939 though we have two uses of this function. */
11941 static inline bool
11942 tree_nop_conversion (const_tree exp)
11944 tree outer_type, inner_type;
11946 if (!CONVERT_EXPR_P (exp)
11947 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11948 return false;
11949 if (TREE_OPERAND (exp, 0) == error_mark_node)
11950 return false;
11952 outer_type = TREE_TYPE (exp);
11953 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11955 if (!inner_type)
11956 return false;
11958 return tree_nop_conversion_p (outer_type, inner_type);
11961 /* Return true iff conversion in EXP generates no instruction. Don't
11962 consider conversions changing the signedness. */
11964 static bool
11965 tree_sign_nop_conversion (const_tree exp)
11967 tree outer_type, inner_type;
11969 if (!tree_nop_conversion (exp))
11970 return false;
11972 outer_type = TREE_TYPE (exp);
11973 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11975 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11976 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11979 /* Strip conversions from EXP according to tree_nop_conversion and
11980 return the resulting expression. */
11982 tree
11983 tree_strip_nop_conversions (tree exp)
11985 while (tree_nop_conversion (exp))
11986 exp = TREE_OPERAND (exp, 0);
11987 return exp;
11990 /* Strip conversions from EXP according to tree_sign_nop_conversion
11991 and return the resulting expression. */
11993 tree
11994 tree_strip_sign_nop_conversions (tree exp)
11996 while (tree_sign_nop_conversion (exp))
11997 exp = TREE_OPERAND (exp, 0);
11998 return exp;
12001 /* Avoid any floating point extensions from EXP. */
12002 tree
12003 strip_float_extensions (tree exp)
12005 tree sub, expt, subt;
12007 /* For floating point constant look up the narrowest type that can hold
12008 it properly and handle it like (type)(narrowest_type)constant.
12009 This way we can optimize for instance a=a*2.0 where "a" is float
12010 but 2.0 is double constant. */
12011 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
12013 REAL_VALUE_TYPE orig;
12014 tree type = NULL;
12016 orig = TREE_REAL_CST (exp);
12017 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
12018 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
12019 type = float_type_node;
12020 else if (TYPE_PRECISION (TREE_TYPE (exp))
12021 > TYPE_PRECISION (double_type_node)
12022 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
12023 type = double_type_node;
12024 if (type)
12025 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
12028 if (!CONVERT_EXPR_P (exp))
12029 return exp;
12031 sub = TREE_OPERAND (exp, 0);
12032 subt = TREE_TYPE (sub);
12033 expt = TREE_TYPE (exp);
12035 if (!FLOAT_TYPE_P (subt))
12036 return exp;
12038 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
12039 return exp;
12041 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
12042 return exp;
12044 return strip_float_extensions (sub);
12047 /* Strip out all handled components that produce invariant
12048 offsets. */
12050 const_tree
12051 strip_invariant_refs (const_tree op)
12053 while (handled_component_p (op))
12055 switch (TREE_CODE (op))
12057 case ARRAY_REF:
12058 case ARRAY_RANGE_REF:
12059 if (!is_gimple_constant (TREE_OPERAND (op, 1))
12060 || TREE_OPERAND (op, 2) != NULL_TREE
12061 || TREE_OPERAND (op, 3) != NULL_TREE)
12062 return NULL;
12063 break;
12065 case COMPONENT_REF:
12066 if (TREE_OPERAND (op, 2) != NULL_TREE)
12067 return NULL;
12068 break;
12070 default:;
12072 op = TREE_OPERAND (op, 0);
12075 return op;
12078 static GTY(()) tree gcc_eh_personality_decl;
12080 /* Return the GCC personality function decl. */
12082 tree
12083 lhd_gcc_personality (void)
12085 if (!gcc_eh_personality_decl)
12086 gcc_eh_personality_decl = build_personality_function ("gcc");
12087 return gcc_eh_personality_decl;
12090 /* TARGET is a call target of GIMPLE call statement
12091 (obtained by gimple_call_fn). Return true if it is
12092 OBJ_TYPE_REF representing an virtual call of C++ method.
12093 (As opposed to OBJ_TYPE_REF representing objc calls
12094 through a cast where middle-end devirtualization machinery
12095 can't apply.) */
12097 bool
12098 virtual_method_call_p (const_tree target)
12100 if (TREE_CODE (target) != OBJ_TYPE_REF)
12101 return false;
12102 tree t = TREE_TYPE (target);
12103 gcc_checking_assert (TREE_CODE (t) == POINTER_TYPE);
12104 t = TREE_TYPE (t);
12105 if (TREE_CODE (t) == FUNCTION_TYPE)
12106 return false;
12107 gcc_checking_assert (TREE_CODE (t) == METHOD_TYPE);
12108 /* If we do not have BINFO associated, it means that type was built
12109 without devirtualization enabled. Do not consider this a virtual
12110 call. */
12111 if (!TYPE_BINFO (obj_type_ref_class (target)))
12112 return false;
12113 return true;
12116 /* REF is OBJ_TYPE_REF, return the class the ref corresponds to. */
12118 tree
12119 obj_type_ref_class (const_tree ref)
12121 gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF);
12122 ref = TREE_TYPE (ref);
12123 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
12124 ref = TREE_TYPE (ref);
12125 /* We look for type THIS points to. ObjC also builds
12126 OBJ_TYPE_REF with non-method calls, Their first parameter
12127 ID however also corresponds to class type. */
12128 gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE
12129 || TREE_CODE (ref) == FUNCTION_TYPE);
12130 ref = TREE_VALUE (TYPE_ARG_TYPES (ref));
12131 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
12132 return TREE_TYPE (ref);
12135 /* Lookup sub-BINFO of BINFO of TYPE at offset POS. */
12137 static tree
12138 lookup_binfo_at_offset (tree binfo, tree type, HOST_WIDE_INT pos)
12140 unsigned int i;
12141 tree base_binfo, b;
12143 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12144 if (pos == tree_to_shwi (BINFO_OFFSET (base_binfo))
12145 && types_same_for_odr (TREE_TYPE (base_binfo), type))
12146 return base_binfo;
12147 else if ((b = lookup_binfo_at_offset (base_binfo, type, pos)) != NULL)
12148 return b;
12149 return NULL;
12152 /* Try to find a base info of BINFO that would have its field decl at offset
12153 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
12154 found, return, otherwise return NULL_TREE. */
12156 tree
12157 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
12159 tree type = BINFO_TYPE (binfo);
12161 while (true)
12163 HOST_WIDE_INT pos, size;
12164 tree fld;
12165 int i;
12167 if (types_same_for_odr (type, expected_type))
12168 return binfo;
12169 if (offset < 0)
12170 return NULL_TREE;
12172 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
12174 if (TREE_CODE (fld) != FIELD_DECL || !DECL_ARTIFICIAL (fld))
12175 continue;
12177 pos = int_bit_position (fld);
12178 size = tree_to_uhwi (DECL_SIZE (fld));
12179 if (pos <= offset && (pos + size) > offset)
12180 break;
12182 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
12183 return NULL_TREE;
12185 /* Offset 0 indicates the primary base, whose vtable contents are
12186 represented in the binfo for the derived class. */
12187 else if (offset != 0)
12189 tree found_binfo = NULL, base_binfo;
12190 /* Offsets in BINFO are in bytes relative to the whole structure
12191 while POS is in bits relative to the containing field. */
12192 int binfo_offset = (tree_to_shwi (BINFO_OFFSET (binfo)) + pos
12193 / BITS_PER_UNIT);
12195 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12196 if (tree_to_shwi (BINFO_OFFSET (base_binfo)) == binfo_offset
12197 && types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
12199 found_binfo = base_binfo;
12200 break;
12202 if (found_binfo)
12203 binfo = found_binfo;
12204 else
12205 binfo = lookup_binfo_at_offset (binfo, TREE_TYPE (fld),
12206 binfo_offset);
12209 type = TREE_TYPE (fld);
12210 offset -= pos;
12214 /* Returns true if X is a typedef decl. */
12216 bool
12217 is_typedef_decl (const_tree x)
12219 return (x && TREE_CODE (x) == TYPE_DECL
12220 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
12223 /* Returns true iff TYPE is a type variant created for a typedef. */
12225 bool
12226 typedef_variant_p (const_tree type)
12228 return is_typedef_decl (TYPE_NAME (type));
12231 /* Warn about a use of an identifier which was marked deprecated. */
12232 void
12233 warn_deprecated_use (tree node, tree attr)
12235 const char *msg;
12237 if (node == 0 || !warn_deprecated_decl)
12238 return;
12240 if (!attr)
12242 if (DECL_P (node))
12243 attr = DECL_ATTRIBUTES (node);
12244 else if (TYPE_P (node))
12246 tree decl = TYPE_STUB_DECL (node);
12247 if (decl)
12248 attr = lookup_attribute ("deprecated",
12249 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
12253 if (attr)
12254 attr = lookup_attribute ("deprecated", attr);
12256 if (attr)
12257 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
12258 else
12259 msg = NULL;
12261 bool w;
12262 if (DECL_P (node))
12264 if (msg)
12265 w = warning (OPT_Wdeprecated_declarations,
12266 "%qD is deprecated: %s", node, msg);
12267 else
12268 w = warning (OPT_Wdeprecated_declarations,
12269 "%qD is deprecated", node);
12270 if (w)
12271 inform (DECL_SOURCE_LOCATION (node), "declared here");
12273 else if (TYPE_P (node))
12275 tree what = NULL_TREE;
12276 tree decl = TYPE_STUB_DECL (node);
12278 if (TYPE_NAME (node))
12280 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12281 what = TYPE_NAME (node);
12282 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12283 && DECL_NAME (TYPE_NAME (node)))
12284 what = DECL_NAME (TYPE_NAME (node));
12287 if (decl)
12289 if (what)
12291 if (msg)
12292 w = warning (OPT_Wdeprecated_declarations,
12293 "%qE is deprecated: %s", what, msg);
12294 else
12295 w = warning (OPT_Wdeprecated_declarations,
12296 "%qE is deprecated", what);
12298 else
12300 if (msg)
12301 w = warning (OPT_Wdeprecated_declarations,
12302 "type is deprecated: %s", msg);
12303 else
12304 w = warning (OPT_Wdeprecated_declarations,
12305 "type is deprecated");
12307 if (w)
12308 inform (DECL_SOURCE_LOCATION (decl), "declared here");
12310 else
12312 if (what)
12314 if (msg)
12315 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
12316 what, msg);
12317 else
12318 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
12320 else
12322 if (msg)
12323 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
12324 msg);
12325 else
12326 warning (OPT_Wdeprecated_declarations, "type is deprecated");
12332 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12333 somewhere in it. */
12335 bool
12336 contains_bitfld_component_ref_p (const_tree ref)
12338 while (handled_component_p (ref))
12340 if (TREE_CODE (ref) == COMPONENT_REF
12341 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12342 return true;
12343 ref = TREE_OPERAND (ref, 0);
12346 return false;
12349 /* Try to determine whether a TRY_CATCH expression can fall through.
12350 This is a subroutine of block_may_fallthru. */
12352 static bool
12353 try_catch_may_fallthru (const_tree stmt)
12355 tree_stmt_iterator i;
12357 /* If the TRY block can fall through, the whole TRY_CATCH can
12358 fall through. */
12359 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
12360 return true;
12362 i = tsi_start (TREE_OPERAND (stmt, 1));
12363 switch (TREE_CODE (tsi_stmt (i)))
12365 case CATCH_EXPR:
12366 /* We expect to see a sequence of CATCH_EXPR trees, each with a
12367 catch expression and a body. The whole TRY_CATCH may fall
12368 through iff any of the catch bodies falls through. */
12369 for (; !tsi_end_p (i); tsi_next (&i))
12371 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
12372 return true;
12374 return false;
12376 case EH_FILTER_EXPR:
12377 /* The exception filter expression only matters if there is an
12378 exception. If the exception does not match EH_FILTER_TYPES,
12379 we will execute EH_FILTER_FAILURE, and we will fall through
12380 if that falls through. If the exception does match
12381 EH_FILTER_TYPES, the stack unwinder will continue up the
12382 stack, so we will not fall through. We don't know whether we
12383 will throw an exception which matches EH_FILTER_TYPES or not,
12384 so we just ignore EH_FILTER_TYPES and assume that we might
12385 throw an exception which doesn't match. */
12386 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
12388 default:
12389 /* This case represents statements to be executed when an
12390 exception occurs. Those statements are implicitly followed
12391 by a RESX statement to resume execution after the exception.
12392 So in this case the TRY_CATCH never falls through. */
12393 return false;
12397 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
12398 need not be 100% accurate; simply be conservative and return true if we
12399 don't know. This is used only to avoid stupidly generating extra code.
12400 If we're wrong, we'll just delete the extra code later. */
12402 bool
12403 block_may_fallthru (const_tree block)
12405 /* This CONST_CAST is okay because expr_last returns its argument
12406 unmodified and we assign it to a const_tree. */
12407 const_tree stmt = expr_last (CONST_CAST_TREE (block));
12409 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
12411 case GOTO_EXPR:
12412 case RETURN_EXPR:
12413 /* Easy cases. If the last statement of the block implies
12414 control transfer, then we can't fall through. */
12415 return false;
12417 case SWITCH_EXPR:
12418 /* If SWITCH_LABELS is set, this is lowered, and represents a
12419 branch to a selected label and hence can not fall through.
12420 Otherwise SWITCH_BODY is set, and the switch can fall
12421 through. */
12422 return SWITCH_LABELS (stmt) == NULL_TREE;
12424 case COND_EXPR:
12425 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
12426 return true;
12427 return block_may_fallthru (COND_EXPR_ELSE (stmt));
12429 case BIND_EXPR:
12430 return block_may_fallthru (BIND_EXPR_BODY (stmt));
12432 case TRY_CATCH_EXPR:
12433 return try_catch_may_fallthru (stmt);
12435 case TRY_FINALLY_EXPR:
12436 /* The finally clause is always executed after the try clause,
12437 so if it does not fall through, then the try-finally will not
12438 fall through. Otherwise, if the try clause does not fall
12439 through, then when the finally clause falls through it will
12440 resume execution wherever the try clause was going. So the
12441 whole try-finally will only fall through if both the try
12442 clause and the finally clause fall through. */
12443 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
12444 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
12446 case MODIFY_EXPR:
12447 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
12448 stmt = TREE_OPERAND (stmt, 1);
12449 else
12450 return true;
12451 /* FALLTHRU */
12453 case CALL_EXPR:
12454 /* Functions that do not return do not fall through. */
12455 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
12457 case CLEANUP_POINT_EXPR:
12458 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12460 case TARGET_EXPR:
12461 return block_may_fallthru (TREE_OPERAND (stmt, 1));
12463 case ERROR_MARK:
12464 return true;
12466 default:
12467 return lang_hooks.block_may_fallthru (stmt);
12471 /* True if we are using EH to handle cleanups. */
12472 static bool using_eh_for_cleanups_flag = false;
12474 /* This routine is called from front ends to indicate eh should be used for
12475 cleanups. */
12476 void
12477 using_eh_for_cleanups (void)
12479 using_eh_for_cleanups_flag = true;
12482 /* Query whether EH is used for cleanups. */
12483 bool
12484 using_eh_for_cleanups_p (void)
12486 return using_eh_for_cleanups_flag;
12489 /* Wrapper for tree_code_name to ensure that tree code is valid */
12490 const char *
12491 get_tree_code_name (enum tree_code code)
12493 const char *invalid = "<invalid tree code>";
12495 if (code >= MAX_TREE_CODES)
12496 return invalid;
12498 return tree_code_name[code];
12501 /* Drops the TREE_OVERFLOW flag from T. */
12503 tree
12504 drop_tree_overflow (tree t)
12506 gcc_checking_assert (TREE_OVERFLOW (t));
12508 /* For tree codes with a sharing machinery re-build the result. */
12509 if (TREE_CODE (t) == INTEGER_CST)
12510 return wide_int_to_tree (TREE_TYPE (t), t);
12512 /* Otherwise, as all tcc_constants are possibly shared, copy the node
12513 and drop the flag. */
12514 t = copy_node (t);
12515 TREE_OVERFLOW (t) = 0;
12516 return t;
12519 /* Given a memory reference expression T, return its base address.
12520 The base address of a memory reference expression is the main
12521 object being referenced. For instance, the base address for
12522 'array[i].fld[j]' is 'array'. You can think of this as stripping
12523 away the offset part from a memory address.
12525 This function calls handled_component_p to strip away all the inner
12526 parts of the memory reference until it reaches the base object. */
12528 tree
12529 get_base_address (tree t)
12531 while (handled_component_p (t))
12532 t = TREE_OPERAND (t, 0);
12534 if ((TREE_CODE (t) == MEM_REF
12535 || TREE_CODE (t) == TARGET_MEM_REF)
12536 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
12537 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
12539 /* ??? Either the alias oracle or all callers need to properly deal
12540 with WITH_SIZE_EXPRs before we can look through those. */
12541 if (TREE_CODE (t) == WITH_SIZE_EXPR)
12542 return NULL_TREE;
12544 return t;
12547 /* Return a tree of sizetype representing the size, in bytes, of the element
12548 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12550 tree
12551 array_ref_element_size (tree exp)
12553 tree aligned_size = TREE_OPERAND (exp, 3);
12554 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
12555 location_t loc = EXPR_LOCATION (exp);
12557 /* If a size was specified in the ARRAY_REF, it's the size measured
12558 in alignment units of the element type. So multiply by that value. */
12559 if (aligned_size)
12561 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
12562 sizetype from another type of the same width and signedness. */
12563 if (TREE_TYPE (aligned_size) != sizetype)
12564 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
12565 return size_binop_loc (loc, MULT_EXPR, aligned_size,
12566 size_int (TYPE_ALIGN_UNIT (elmt_type)));
12569 /* Otherwise, take the size from that of the element type. Substitute
12570 any PLACEHOLDER_EXPR that we have. */
12571 else
12572 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
12575 /* Return a tree representing the lower bound of the array mentioned in
12576 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12578 tree
12579 array_ref_low_bound (tree exp)
12581 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12583 /* If a lower bound is specified in EXP, use it. */
12584 if (TREE_OPERAND (exp, 2))
12585 return TREE_OPERAND (exp, 2);
12587 /* Otherwise, if there is a domain type and it has a lower bound, use it,
12588 substituting for a PLACEHOLDER_EXPR as needed. */
12589 if (domain_type && TYPE_MIN_VALUE (domain_type))
12590 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
12592 /* Otherwise, return a zero of the appropriate type. */
12593 return build_int_cst (TREE_TYPE (TREE_OPERAND (exp, 1)), 0);
12596 /* Return a tree representing the upper bound of the array mentioned in
12597 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12599 tree
12600 array_ref_up_bound (tree exp)
12602 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12604 /* If there is a domain type and it has an upper bound, use it, substituting
12605 for a PLACEHOLDER_EXPR as needed. */
12606 if (domain_type && TYPE_MAX_VALUE (domain_type))
12607 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
12609 /* Otherwise fail. */
12610 return NULL_TREE;
12613 /* Returns true if REF is an array reference to an array at the end of
12614 a structure. If this is the case, the array may be allocated larger
12615 than its upper bound implies. */
12617 bool
12618 array_at_struct_end_p (tree ref)
12620 if (TREE_CODE (ref) != ARRAY_REF
12621 && TREE_CODE (ref) != ARRAY_RANGE_REF)
12622 return false;
12624 while (handled_component_p (ref))
12626 /* If the reference chain contains a component reference to a
12627 non-union type and there follows another field the reference
12628 is not at the end of a structure. */
12629 if (TREE_CODE (ref) == COMPONENT_REF
12630 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
12632 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
12633 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
12634 nextf = DECL_CHAIN (nextf);
12635 if (nextf)
12636 return false;
12639 ref = TREE_OPERAND (ref, 0);
12642 /* If the reference is based on a declared entity, the size of the array
12643 is constrained by its given domain. */
12644 if (DECL_P (ref))
12645 return false;
12647 return true;
12650 /* Return a tree representing the offset, in bytes, of the field referenced
12651 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
12653 tree
12654 component_ref_field_offset (tree exp)
12656 tree aligned_offset = TREE_OPERAND (exp, 2);
12657 tree field = TREE_OPERAND (exp, 1);
12658 location_t loc = EXPR_LOCATION (exp);
12660 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
12661 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
12662 value. */
12663 if (aligned_offset)
12665 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
12666 sizetype from another type of the same width and signedness. */
12667 if (TREE_TYPE (aligned_offset) != sizetype)
12668 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
12669 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
12670 size_int (DECL_OFFSET_ALIGN (field)
12671 / BITS_PER_UNIT));
12674 /* Otherwise, take the offset from that of the field. Substitute
12675 any PLACEHOLDER_EXPR that we have. */
12676 else
12677 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
12680 /* Return the machine mode of T. For vectors, returns the mode of the
12681 inner type. The main use case is to feed the result to HONOR_NANS,
12682 avoiding the BLKmode that a direct TYPE_MODE (T) might return. */
12684 machine_mode
12685 element_mode (const_tree t)
12687 if (!TYPE_P (t))
12688 t = TREE_TYPE (t);
12689 if (VECTOR_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
12690 t = TREE_TYPE (t);
12691 return TYPE_MODE (t);
12695 /* Veirfy that basic properties of T match TV and thus T can be a variant of
12696 TV. TV should be the more specified variant (i.e. the main variant). */
12698 static bool
12699 verify_type_variant (const_tree t, tree tv)
12701 /* Type variant can differ by:
12703 - TYPE_QUALS: TYPE_READONLY, TYPE_VOLATILE, TYPE_ATOMIC, TYPE_RESTRICT,
12704 ENCODE_QUAL_ADDR_SPACE.
12705 - main variant may be TYPE_COMPLETE_P and variant types !TYPE_COMPLETE_P
12706 in this case some values may not be set in the variant types
12707 (see TYPE_COMPLETE_P checks).
12708 - it is possible to have TYPE_ARTIFICIAL variant of non-artifical type
12709 - by TYPE_NAME and attributes (i.e. when variant originate by typedef)
12710 - TYPE_CANONICAL (TYPE_ALIAS_SET is the same among variants)
12711 - by the alignment: TYPE_ALIGN and TYPE_USER_ALIGN
12712 - during LTO by TYPE_CONTEXT if type is TYPE_FILE_SCOPE_P
12713 this is necessary to make it possible to merge types form different TUs
12714 - arrays, pointers and references may have TREE_TYPE that is a variant
12715 of TREE_TYPE of their main variants.
12716 - aggregates may have new TYPE_FIELDS list that list variants of
12717 the main variant TYPE_FIELDS.
12718 - vector types may differ by TYPE_VECTOR_OPAQUE
12719 - TYPE_METHODS is always NULL for vairant types and maintained for
12720 main variant only.
12723 /* Convenience macro for matching individual fields. */
12724 #define verify_variant_match(flag) \
12725 do { \
12726 if (flag (tv) != flag (t)) \
12728 error ("type variant differs by " #flag "."); \
12729 debug_tree (tv); \
12730 return false; \
12732 } while (false)
12734 /* tree_base checks. */
12736 verify_variant_match (TREE_CODE);
12737 /* FIXME: Ada builds non-artificial variants of artificial types. */
12738 if (TYPE_ARTIFICIAL (tv) && 0)
12739 verify_variant_match (TYPE_ARTIFICIAL);
12740 if (POINTER_TYPE_P (tv))
12741 verify_variant_match (TYPE_REF_CAN_ALIAS_ALL);
12742 /* FIXME: TYPE_SIZES_GIMPLIFIED may differs for Ada build. */
12743 verify_variant_match (TYPE_UNSIGNED);
12744 verify_variant_match (TYPE_ALIGN_OK);
12745 verify_variant_match (TYPE_PACKED);
12746 if (TREE_CODE (t) == REFERENCE_TYPE)
12747 verify_variant_match (TYPE_REF_IS_RVALUE);
12748 verify_variant_match (TYPE_SATURATING);
12749 /* FIXME: This check trigger during libstdc++ build. */
12750 if (RECORD_OR_UNION_TYPE_P (t) && COMPLETE_TYPE_P (t) && 0)
12751 verify_variant_match (TYPE_FINAL_P);
12753 /* tree_type_common checks. */
12755 if (COMPLETE_TYPE_P (t))
12757 verify_variant_match (TYPE_SIZE);
12758 verify_variant_match (TYPE_MODE);
12759 if (TYPE_SIZE_UNIT (t) != TYPE_SIZE_UNIT (tv)
12760 /* FIXME: ideally we should compare pointer equality, but java FE
12761 produce variants where size is INTEGER_CST of different type (int
12762 wrt size_type) during libjava biuld. */
12763 && !operand_equal_p (TYPE_SIZE_UNIT (t), TYPE_SIZE_UNIT (tv), 0))
12765 error ("type variant has different TYPE_SIZE_UNIT");
12766 debug_tree (tv);
12767 error ("type variant's TYPE_SIZE_UNIT");
12768 debug_tree (TYPE_SIZE_UNIT (tv));
12769 error ("type's TYPE_SIZE_UNIT");
12770 debug_tree (TYPE_SIZE_UNIT (t));
12771 return false;
12774 verify_variant_match (TYPE_PRECISION);
12775 verify_variant_match (TYPE_NEEDS_CONSTRUCTING);
12776 if (RECORD_OR_UNION_TYPE_P (t))
12777 verify_variant_match (TYPE_TRANSPARENT_AGGR);
12778 else if (TREE_CODE (t) == ARRAY_TYPE)
12779 verify_variant_match (TYPE_NONALIASED_COMPONENT);
12780 /* During LTO we merge variant lists from diferent translation units
12781 that may differ BY TYPE_CONTEXT that in turn may point
12782 to TRANSLATION_UNIT_DECL.
12783 Ada also builds variants of types with different TYPE_CONTEXT. */
12784 if ((!in_lto_p || !TYPE_FILE_SCOPE_P (t)) && 0)
12785 verify_variant_match (TYPE_CONTEXT);
12786 verify_variant_match (TYPE_STRING_FLAG);
12787 if (TYPE_ALIAS_SET_KNOWN_P (t) && TYPE_ALIAS_SET_KNOWN_P (tv))
12788 verify_variant_match (TYPE_ALIAS_SET);
12790 /* tree_type_non_common checks. */
12792 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
12793 and dangle the pointer from time to time. */
12794 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_VFIELD (t) != TYPE_VFIELD (tv)
12795 && (in_lto_p || !TYPE_VFIELD (tv)
12796 || TREE_CODE (TYPE_VFIELD (tv)) != TREE_LIST))
12798 error ("type variant has different TYPE_VFIELD");
12799 debug_tree (tv);
12800 return false;
12802 if ((TREE_CODE (t) == ENUMERAL_TYPE && COMPLETE_TYPE_P (t))
12803 || TREE_CODE (t) == INTEGER_TYPE
12804 || TREE_CODE (t) == BOOLEAN_TYPE
12805 || TREE_CODE (t) == REAL_TYPE
12806 || TREE_CODE (t) == FIXED_POINT_TYPE)
12808 verify_variant_match (TYPE_MAX_VALUE);
12809 verify_variant_match (TYPE_MIN_VALUE);
12811 if (TREE_CODE (t) == METHOD_TYPE)
12812 verify_variant_match (TYPE_METHOD_BASETYPE);
12813 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_METHODS (t))
12815 error ("type variant has TYPE_METHODS");
12816 debug_tree (tv);
12817 return false;
12819 if (TREE_CODE (t) == OFFSET_TYPE)
12820 verify_variant_match (TYPE_OFFSET_BASETYPE);
12821 if (TREE_CODE (t) == ARRAY_TYPE)
12822 verify_variant_match (TYPE_ARRAY_MAX_SIZE);
12823 /* FIXME: Be lax and allow TYPE_BINFO to be missing in variant types
12824 or even type's main variant. This is needed to make bootstrap pass
12825 and the bug seems new in GCC 5.
12826 C++ FE should be updated to make this consistent and we should check
12827 that TYPE_BINFO is always NULL for !COMPLETE_TYPE_P and otherwise there
12828 is a match with main variant.
12830 Also disable the check for Java for now because of parser hack that builds
12831 first an dummy BINFO and then sometimes replace it by real BINFO in some
12832 of the copies. */
12833 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t) && TYPE_BINFO (tv)
12834 && TYPE_BINFO (t) != TYPE_BINFO (tv)
12835 /* FIXME: Java sometimes keep dump TYPE_BINFOs on variant types.
12836 Since there is no cheap way to tell C++/Java type w/o LTO, do checking
12837 at LTO time only. */
12838 && (in_lto_p && odr_type_p (t)))
12840 error ("type variant has different TYPE_BINFO");
12841 debug_tree (tv);
12842 error ("type variant's TYPE_BINFO");
12843 debug_tree (TYPE_BINFO (tv));
12844 error ("type's TYPE_BINFO");
12845 debug_tree (TYPE_BINFO (t));
12846 return false;
12849 /* Check various uses of TYPE_VALUES_RAW. */
12850 if (TREE_CODE (t) == ENUMERAL_TYPE)
12851 verify_variant_match (TYPE_VALUES);
12852 else if (TREE_CODE (t) == ARRAY_TYPE)
12853 verify_variant_match (TYPE_DOMAIN);
12854 /* Permit incomplete variants of complete type. While FEs may complete
12855 all variants, this does not happen for C++ templates in all cases. */
12856 else if (RECORD_OR_UNION_TYPE_P (t)
12857 && COMPLETE_TYPE_P (t)
12858 && TYPE_FIELDS (t) != TYPE_FIELDS (tv))
12860 tree f1, f2;
12862 /* Fortran builds qualified variants as new records with items of
12863 qualified type. Verify that they looks same. */
12864 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (tv);
12865 f1 && f2;
12866 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
12867 if (TREE_CODE (f1) != FIELD_DECL || TREE_CODE (f2) != FIELD_DECL
12868 || (TYPE_MAIN_VARIANT (TREE_TYPE (f1))
12869 != TYPE_MAIN_VARIANT (TREE_TYPE (f2))
12870 /* FIXME: gfc_nonrestricted_type builds all types as variants
12871 with exception of pointer types. It deeply copies the type
12872 which means that we may end up with a variant type
12873 referring non-variant pointer. We may change it to
12874 produce types as variants, too, like
12875 objc_get_protocol_qualified_type does. */
12876 && !POINTER_TYPE_P (TREE_TYPE (f1)))
12877 || DECL_FIELD_OFFSET (f1) != DECL_FIELD_OFFSET (f2)
12878 || DECL_FIELD_BIT_OFFSET (f1) != DECL_FIELD_BIT_OFFSET (f2))
12879 break;
12880 if (f1 || f2)
12882 error ("type variant has different TYPE_FIELDS");
12883 debug_tree (tv);
12884 error ("first mismatch is field");
12885 debug_tree (f1);
12886 error ("and field");
12887 debug_tree (f2);
12888 return false;
12891 else if ((TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE))
12892 verify_variant_match (TYPE_ARG_TYPES);
12893 /* For C++ the qualified variant of array type is really an array type
12894 of qualified TREE_TYPE.
12895 objc builds variants of pointer where pointer to type is a variant, too
12896 in objc_get_protocol_qualified_type. */
12897 if (TREE_TYPE (t) != TREE_TYPE (tv)
12898 && ((TREE_CODE (t) != ARRAY_TYPE
12899 && !POINTER_TYPE_P (t))
12900 || TYPE_MAIN_VARIANT (TREE_TYPE (t))
12901 != TYPE_MAIN_VARIANT (TREE_TYPE (tv))))
12903 error ("type variant has different TREE_TYPE");
12904 debug_tree (tv);
12905 error ("type variant's TREE_TYPE");
12906 debug_tree (TREE_TYPE (tv));
12907 error ("type's TREE_TYPE");
12908 debug_tree (TREE_TYPE (t));
12909 return false;
12911 if (type_with_alias_set_p (t)
12912 && !gimple_canonical_types_compatible_p (t, tv, false))
12914 error ("type is not compatible with its vairant");
12915 debug_tree (tv);
12916 error ("type variant's TREE_TYPE");
12917 debug_tree (TREE_TYPE (tv));
12918 error ("type's TREE_TYPE");
12919 debug_tree (TREE_TYPE (t));
12920 return false;
12922 return true;
12923 #undef verify_variant_match
12927 /* The TYPE_CANONICAL merging machinery. It should closely resemble
12928 the middle-end types_compatible_p function. It needs to avoid
12929 claiming types are different for types that should be treated
12930 the same with respect to TBAA. Canonical types are also used
12931 for IL consistency checks via the useless_type_conversion_p
12932 predicate which does not handle all type kinds itself but falls
12933 back to pointer-comparison of TYPE_CANONICAL for aggregates
12934 for example. */
12936 /* Return true iff T1 and T2 are structurally identical for what
12937 TBAA is concerned.
12938 This function is used both by lto.c canonical type merging and by the
12939 verifier. If TRUST_TYPE_CANONICAL we do not look into structure of types
12940 that have TYPE_CANONICAL defined and assume them equivalent. */
12942 bool
12943 gimple_canonical_types_compatible_p (const_tree t1, const_tree t2,
12944 bool trust_type_canonical)
12946 /* Type variants should be same as the main variant. When not doing sanity
12947 checking to verify this fact, go to main variants and save some work. */
12948 if (trust_type_canonical)
12950 t1 = TYPE_MAIN_VARIANT (t1);
12951 t2 = TYPE_MAIN_VARIANT (t2);
12954 /* Check first for the obvious case of pointer identity. */
12955 if (t1 == t2)
12956 return true;
12958 /* Check that we have two types to compare. */
12959 if (t1 == NULL_TREE || t2 == NULL_TREE)
12960 return false;
12962 /* We consider complete types always compatible with incomplete type.
12963 This does not make sense for canonical type calculation and thus we
12964 need to ensure that we are never called on it.
12966 FIXME: For more correctness the function probably should have three modes
12967 1) mode assuming that types are complete mathcing their structure
12968 2) mode allowing incomplete types but producing equivalence classes
12969 and thus ignoring all info from complete types
12970 3) mode allowing incomplete types to match complete but checking
12971 compatibility between complete types.
12973 1 and 2 can be used for canonical type calculation. 3 is the real
12974 definition of type compatibility that can be used i.e. for warnings during
12975 declaration merging. */
12977 gcc_assert (!trust_type_canonical
12978 || (type_with_alias_set_p (t1) && type_with_alias_set_p (t2)));
12979 /* If the types have been previously registered and found equal
12980 they still are. */
12981 if (TYPE_CANONICAL (t1) && TYPE_CANONICAL (t2)
12982 && trust_type_canonical)
12983 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
12985 /* Can't be the same type if the types don't have the same code. */
12986 if (tree_code_for_canonical_type_merging (TREE_CODE (t1))
12987 != tree_code_for_canonical_type_merging (TREE_CODE (t2)))
12988 return false;
12990 /* Qualifiers do not matter for canonical type comparison purposes. */
12992 /* Void types and nullptr types are always the same. */
12993 if (TREE_CODE (t1) == VOID_TYPE
12994 || TREE_CODE (t1) == NULLPTR_TYPE)
12995 return true;
12997 /* Can't be the same type if they have different mode. */
12998 if (TYPE_MODE (t1) != TYPE_MODE (t2))
12999 return false;
13001 /* Non-aggregate types can be handled cheaply. */
13002 if (INTEGRAL_TYPE_P (t1)
13003 || SCALAR_FLOAT_TYPE_P (t1)
13004 || FIXED_POINT_TYPE_P (t1)
13005 || TREE_CODE (t1) == VECTOR_TYPE
13006 || TREE_CODE (t1) == COMPLEX_TYPE
13007 || TREE_CODE (t1) == OFFSET_TYPE
13008 || POINTER_TYPE_P (t1))
13010 /* Can't be the same type if they have different sign or precision. */
13011 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
13012 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
13013 return false;
13015 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
13016 interoperable with "signed char". Unless all frontends are revisited
13017 to agree on these types, we must ignore the flag completely. */
13019 /* Fortran standard define C_PTR type that is compatible with every
13020 C pointer. For this reason we need to glob all pointers into one.
13021 Still pointers in different address spaces are not compatible. */
13022 if (POINTER_TYPE_P (t1))
13024 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
13025 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
13026 return false;
13029 /* Tail-recurse to components. */
13030 if (TREE_CODE (t1) == VECTOR_TYPE
13031 || TREE_CODE (t1) == COMPLEX_TYPE)
13032 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
13033 TREE_TYPE (t2),
13034 trust_type_canonical);
13036 return true;
13039 /* Do type-specific comparisons. */
13040 switch (TREE_CODE (t1))
13042 case ARRAY_TYPE:
13043 /* Array types are the same if the element types are the same and
13044 the number of elements are the same. */
13045 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13046 trust_type_canonical)
13047 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
13048 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
13049 return false;
13050 else
13052 tree i1 = TYPE_DOMAIN (t1);
13053 tree i2 = TYPE_DOMAIN (t2);
13055 /* For an incomplete external array, the type domain can be
13056 NULL_TREE. Check this condition also. */
13057 if (i1 == NULL_TREE && i2 == NULL_TREE)
13058 return true;
13059 else if (i1 == NULL_TREE || i2 == NULL_TREE)
13060 return false;
13061 else
13063 tree min1 = TYPE_MIN_VALUE (i1);
13064 tree min2 = TYPE_MIN_VALUE (i2);
13065 tree max1 = TYPE_MAX_VALUE (i1);
13066 tree max2 = TYPE_MAX_VALUE (i2);
13068 /* The minimum/maximum values have to be the same. */
13069 if ((min1 == min2
13070 || (min1 && min2
13071 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
13072 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
13073 || operand_equal_p (min1, min2, 0))))
13074 && (max1 == max2
13075 || (max1 && max2
13076 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
13077 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
13078 || operand_equal_p (max1, max2, 0)))))
13079 return true;
13080 else
13081 return false;
13085 case METHOD_TYPE:
13086 case FUNCTION_TYPE:
13087 /* Function types are the same if the return type and arguments types
13088 are the same. */
13089 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13090 trust_type_canonical))
13091 return false;
13093 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
13094 return true;
13095 else
13097 tree parms1, parms2;
13099 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
13100 parms1 && parms2;
13101 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
13103 if (!gimple_canonical_types_compatible_p
13104 (TREE_VALUE (parms1), TREE_VALUE (parms2),
13105 trust_type_canonical))
13106 return false;
13109 if (parms1 || parms2)
13110 return false;
13112 return true;
13115 case RECORD_TYPE:
13116 case UNION_TYPE:
13117 case QUAL_UNION_TYPE:
13119 tree f1, f2;
13121 /* For aggregate types, all the fields must be the same. */
13122 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
13123 f1 || f2;
13124 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
13126 /* Skip non-fields. */
13127 while (f1 && TREE_CODE (f1) != FIELD_DECL)
13128 f1 = TREE_CHAIN (f1);
13129 while (f2 && TREE_CODE (f2) != FIELD_DECL)
13130 f2 = TREE_CHAIN (f2);
13131 if (!f1 || !f2)
13132 break;
13133 /* The fields must have the same name, offset and type. */
13134 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
13135 || !gimple_compare_field_offset (f1, f2)
13136 || !gimple_canonical_types_compatible_p
13137 (TREE_TYPE (f1), TREE_TYPE (f2),
13138 trust_type_canonical))
13139 return false;
13142 /* If one aggregate has more fields than the other, they
13143 are not the same. */
13144 if (f1 || f2)
13145 return false;
13147 return true;
13150 default:
13151 /* Consider all types with language specific trees in them mutually
13152 compatible. This is executed only from verify_type and false
13153 positives can be tolerated. */
13154 gcc_assert (!in_lto_p);
13155 return true;
13159 /* Verify type T. */
13161 void
13162 verify_type (const_tree t)
13164 bool error_found = false;
13165 tree mv = TYPE_MAIN_VARIANT (t);
13166 if (!mv)
13168 error ("Main variant is not defined");
13169 error_found = true;
13171 else if (mv != TYPE_MAIN_VARIANT (mv))
13173 error ("TYPE_MAIN_VARIANT has different TYPE_MAIN_VARIANT");
13174 debug_tree (mv);
13175 error_found = true;
13177 else if (t != mv && !verify_type_variant (t, mv))
13178 error_found = true;
13180 tree ct = TYPE_CANONICAL (t);
13181 if (!ct)
13183 else if (TYPE_CANONICAL (t) != ct)
13185 error ("TYPE_CANONICAL has different TYPE_CANONICAL");
13186 debug_tree (ct);
13187 error_found = true;
13189 /* Method and function types can not be used to address memory and thus
13190 TYPE_CANONICAL really matters only for determining useless conversions.
13192 FIXME: C++ FE produce declarations of builtin functions that are not
13193 compatible with main variants. */
13194 else if (TREE_CODE (t) == FUNCTION_TYPE)
13196 else if (t != ct
13197 /* FIXME: gimple_canonical_types_compatible_p can not compare types
13198 with variably sized arrays because their sizes possibly
13199 gimplified to different variables. */
13200 && !variably_modified_type_p (ct, NULL)
13201 && !gimple_canonical_types_compatible_p (t, ct, false))
13203 error ("TYPE_CANONICAL is not compatible");
13204 debug_tree (ct);
13205 error_found = true;
13209 /* Check various uses of TYPE_MINVAL. */
13210 if (RECORD_OR_UNION_TYPE_P (t))
13212 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
13213 and danagle the pointer from time to time. */
13214 if (TYPE_VFIELD (t)
13215 && TREE_CODE (TYPE_VFIELD (t)) != FIELD_DECL
13216 && TREE_CODE (TYPE_VFIELD (t)) != TREE_LIST)
13218 error ("TYPE_VFIELD is not FIELD_DECL nor TREE_LIST");
13219 debug_tree (TYPE_VFIELD (t));
13220 error_found = true;
13223 else if (TREE_CODE (t) == POINTER_TYPE)
13225 if (TYPE_NEXT_PTR_TO (t)
13226 && TREE_CODE (TYPE_NEXT_PTR_TO (t)) != POINTER_TYPE)
13228 error ("TYPE_NEXT_PTR_TO is not POINTER_TYPE");
13229 debug_tree (TYPE_NEXT_PTR_TO (t));
13230 error_found = true;
13233 else if (TREE_CODE (t) == REFERENCE_TYPE)
13235 if (TYPE_NEXT_REF_TO (t)
13236 && TREE_CODE (TYPE_NEXT_REF_TO (t)) != REFERENCE_TYPE)
13238 error ("TYPE_NEXT_REF_TO is not REFERENCE_TYPE");
13239 debug_tree (TYPE_NEXT_REF_TO (t));
13240 error_found = true;
13243 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
13244 || TREE_CODE (t) == FIXED_POINT_TYPE)
13246 /* FIXME: The following check should pass:
13247 useless_type_conversion_p (const_cast <tree> (t),
13248 TREE_TYPE (TYPE_MIN_VALUE (t))
13249 but does not for C sizetypes in LTO. */
13251 /* Java uses TYPE_MINVAL for TYPE_ARGUMENT_SIGNATURE. */
13252 else if (TYPE_MINVAL (t)
13253 && ((TREE_CODE (t) != METHOD_TYPE && TREE_CODE (t) != FUNCTION_TYPE)
13254 || in_lto_p))
13256 error ("TYPE_MINVAL non-NULL");
13257 debug_tree (TYPE_MINVAL (t));
13258 error_found = true;
13261 /* Check various uses of TYPE_MAXVAL. */
13262 if (RECORD_OR_UNION_TYPE_P (t))
13264 if (TYPE_METHODS (t) && TREE_CODE (TYPE_METHODS (t)) != FUNCTION_DECL
13265 && TREE_CODE (TYPE_METHODS (t)) != TEMPLATE_DECL
13266 && TYPE_METHODS (t) != error_mark_node)
13268 error ("TYPE_METHODS is not FUNCTION_DECL, TEMPLATE_DECL nor error_mark_node");
13269 debug_tree (TYPE_METHODS (t));
13270 error_found = true;
13273 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
13275 if (TYPE_METHOD_BASETYPE (t)
13276 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != RECORD_TYPE
13277 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != UNION_TYPE)
13279 error ("TYPE_METHOD_BASETYPE is not record nor union");
13280 debug_tree (TYPE_METHOD_BASETYPE (t));
13281 error_found = true;
13284 else if (TREE_CODE (t) == OFFSET_TYPE)
13286 if (TYPE_OFFSET_BASETYPE (t)
13287 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != RECORD_TYPE
13288 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != UNION_TYPE)
13290 error ("TYPE_OFFSET_BASETYPE is not record nor union");
13291 debug_tree (TYPE_OFFSET_BASETYPE (t));
13292 error_found = true;
13295 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
13296 || TREE_CODE (t) == FIXED_POINT_TYPE)
13298 /* FIXME: The following check should pass:
13299 useless_type_conversion_p (const_cast <tree> (t),
13300 TREE_TYPE (TYPE_MAX_VALUE (t))
13301 but does not for C sizetypes in LTO. */
13303 else if (TREE_CODE (t) == ARRAY_TYPE)
13305 if (TYPE_ARRAY_MAX_SIZE (t)
13306 && TREE_CODE (TYPE_ARRAY_MAX_SIZE (t)) != INTEGER_CST)
13308 error ("TYPE_ARRAY_MAX_SIZE not INTEGER_CST");
13309 debug_tree (TYPE_ARRAY_MAX_SIZE (t));
13310 error_found = true;
13313 else if (TYPE_MAXVAL (t))
13315 error ("TYPE_MAXVAL non-NULL");
13316 debug_tree (TYPE_MAXVAL (t));
13317 error_found = true;
13320 /* Check various uses of TYPE_BINFO. */
13321 if (RECORD_OR_UNION_TYPE_P (t))
13323 if (!TYPE_BINFO (t))
13325 else if (TREE_CODE (TYPE_BINFO (t)) != TREE_BINFO)
13327 error ("TYPE_BINFO is not TREE_BINFO");
13328 debug_tree (TYPE_BINFO (t));
13329 error_found = true;
13331 /* FIXME: Java builds invalid empty binfos that do not have
13332 TREE_TYPE set. */
13333 else if (TREE_TYPE (TYPE_BINFO (t)) != TYPE_MAIN_VARIANT (t) && 0)
13335 error ("TYPE_BINFO type is not TYPE_MAIN_VARIANT");
13336 debug_tree (TREE_TYPE (TYPE_BINFO (t)));
13337 error_found = true;
13340 else if (TYPE_LANG_SLOT_1 (t) && in_lto_p)
13342 error ("TYPE_LANG_SLOT_1 (binfo) field is non-NULL");
13343 debug_tree (TYPE_LANG_SLOT_1 (t));
13344 error_found = true;
13347 /* Check various uses of TYPE_VALUES_RAW. */
13348 if (TREE_CODE (t) == ENUMERAL_TYPE)
13349 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
13351 tree value = TREE_VALUE (l);
13352 tree name = TREE_PURPOSE (l);
13354 /* C FE porduce INTEGER_CST of INTEGER_TYPE, while C++ FE uses
13355 CONST_DECL of ENUMERAL TYPE. */
13356 if (TREE_CODE (value) != INTEGER_CST && TREE_CODE (value) != CONST_DECL)
13358 error ("Enum value is not CONST_DECL or INTEGER_CST");
13359 debug_tree (value);
13360 debug_tree (name);
13361 error_found = true;
13363 if (TREE_CODE (TREE_TYPE (value)) != INTEGER_TYPE
13364 && !useless_type_conversion_p (const_cast <tree> (t), TREE_TYPE (value)))
13366 error ("Enum value type is not INTEGER_TYPE nor convertible to the enum");
13367 debug_tree (value);
13368 debug_tree (name);
13369 error_found = true;
13371 if (TREE_CODE (name) != IDENTIFIER_NODE)
13373 error ("Enum value name is not IDENTIFIER_NODE");
13374 debug_tree (value);
13375 debug_tree (name);
13376 error_found = true;
13379 else if (TREE_CODE (t) == ARRAY_TYPE)
13381 if (TYPE_DOMAIN (t) && TREE_CODE (TYPE_DOMAIN (t)) != INTEGER_TYPE)
13383 error ("Array TYPE_DOMAIN is not integer type");
13384 debug_tree (TYPE_DOMAIN (t));
13385 error_found = true;
13388 else if (RECORD_OR_UNION_TYPE_P (t))
13389 for (tree fld = TYPE_FIELDS (t); fld; fld = TREE_CHAIN (fld))
13391 /* TODO: verify properties of decls. */
13392 if (TREE_CODE (fld) == FIELD_DECL)
13394 else if (TREE_CODE (fld) == TYPE_DECL)
13396 else if (TREE_CODE (fld) == CONST_DECL)
13398 else if (TREE_CODE (fld) == VAR_DECL)
13400 else if (TREE_CODE (fld) == TEMPLATE_DECL)
13402 else if (TREE_CODE (fld) == USING_DECL)
13404 else
13406 error ("Wrong tree in TYPE_FIELDS list");
13407 debug_tree (fld);
13408 error_found = true;
13411 else if (TREE_CODE (t) == INTEGER_TYPE
13412 || TREE_CODE (t) == BOOLEAN_TYPE
13413 || TREE_CODE (t) == OFFSET_TYPE
13414 || TREE_CODE (t) == REFERENCE_TYPE
13415 || TREE_CODE (t) == NULLPTR_TYPE
13416 || TREE_CODE (t) == POINTER_TYPE)
13418 if (TYPE_CACHED_VALUES_P (t) != (TYPE_CACHED_VALUES (t) != NULL))
13420 error ("TYPE_CACHED_VALUES_P is %i while TYPE_CACHED_VALUES is %p",
13421 TYPE_CACHED_VALUES_P (t), (void *)TYPE_CACHED_VALUES (t));
13422 error_found = true;
13424 else if (TYPE_CACHED_VALUES_P (t) && TREE_CODE (TYPE_CACHED_VALUES (t)) != TREE_VEC)
13426 error ("TYPE_CACHED_VALUES is not TREE_VEC");
13427 debug_tree (TYPE_CACHED_VALUES (t));
13428 error_found = true;
13430 /* Verify just enough of cache to ensure that no one copied it to new type.
13431 All copying should go by copy_node that should clear it. */
13432 else if (TYPE_CACHED_VALUES_P (t))
13434 int i;
13435 for (i = 0; i < TREE_VEC_LENGTH (TYPE_CACHED_VALUES (t)); i++)
13436 if (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)
13437 && TREE_TYPE (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)) != t)
13439 error ("wrong TYPE_CACHED_VALUES entry");
13440 debug_tree (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i));
13441 error_found = true;
13442 break;
13446 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
13447 for (tree l = TYPE_ARG_TYPES (t); l; l = TREE_CHAIN (l))
13449 /* C++ FE uses TREE_PURPOSE to store initial values. */
13450 if (TREE_PURPOSE (l) && in_lto_p)
13452 error ("TREE_PURPOSE is non-NULL in TYPE_ARG_TYPES list");
13453 debug_tree (l);
13454 error_found = true;
13456 if (!TYPE_P (TREE_VALUE (l)))
13458 error ("Wrong entry in TYPE_ARG_TYPES list");
13459 debug_tree (l);
13460 error_found = true;
13463 else if (!is_lang_specific (t) && TYPE_VALUES_RAW (t))
13465 error ("TYPE_VALUES_RAW field is non-NULL");
13466 debug_tree (TYPE_VALUES_RAW (t));
13467 error_found = true;
13469 if (TREE_CODE (t) != INTEGER_TYPE
13470 && TREE_CODE (t) != BOOLEAN_TYPE
13471 && TREE_CODE (t) != OFFSET_TYPE
13472 && TREE_CODE (t) != REFERENCE_TYPE
13473 && TREE_CODE (t) != NULLPTR_TYPE
13474 && TREE_CODE (t) != POINTER_TYPE
13475 && TYPE_CACHED_VALUES_P (t))
13477 error ("TYPE_CACHED_VALUES_P is set while it should not");
13478 error_found = true;
13480 if (TYPE_STRING_FLAG (t)
13481 && TREE_CODE (t) != ARRAY_TYPE && TREE_CODE (t) != INTEGER_TYPE)
13483 error ("TYPE_STRING_FLAG is set on wrong type code");
13484 error_found = true;
13486 else if (TYPE_STRING_FLAG (t))
13488 const_tree b = t;
13489 if (TREE_CODE (b) == ARRAY_TYPE)
13490 b = TREE_TYPE (t);
13491 /* Java builds arrays with TYPE_STRING_FLAG of promoted_char_type
13492 that is 32bits. */
13493 if (TREE_CODE (b) != INTEGER_TYPE)
13495 error ("TYPE_STRING_FLAG is set on type that does not look like "
13496 "char nor array of chars");
13497 error_found = true;
13501 /* ipa-devirt makes an assumption that TYPE_METHOD_BASETYPE is always
13502 TYPE_MAIN_VARIANT and it would be odd to add methods only to variatns
13503 of a type. */
13504 if (TREE_CODE (t) == METHOD_TYPE
13505 && TYPE_MAIN_VARIANT (TYPE_METHOD_BASETYPE (t)) != TYPE_METHOD_BASETYPE (t))
13507 error ("TYPE_METHOD_BASETYPE is not main variant");
13508 error_found = true;
13511 if (error_found)
13513 debug_tree (const_cast <tree> (t));
13514 internal_error ("verify_type failed");
13518 #include "gt-tree.h"