gcc/
[official-gcc.git] / gcc / tree.c
blob6628a387f621d767eecec19f1ce3130258e4b4f9
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 "cgraph.h"
68 #include "tree-phinodes.h"
69 #include "stringpool.h"
70 #include "tree-ssanames.h"
71 #include "rtl.h"
72 #include "insn-config.h"
73 #include "expmed.h"
74 #include "dojump.h"
75 #include "explow.h"
76 #include "emit-rtl.h"
77 #include "stmt.h"
78 #include "expr.h"
79 #include "tree-dfa.h"
80 #include "params.h"
81 #include "tree-pass.h"
82 #include "langhooks-def.h"
83 #include "diagnostic.h"
84 #include "tree-diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "except.h"
87 #include "debug.h"
88 #include "intl.h"
89 #include "builtins.h"
90 #include "print-tree.h"
91 #include "ipa-utils.h"
93 /* Tree code classes. */
95 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
96 #define END_OF_BASE_TREE_CODES tcc_exceptional,
98 const enum tree_code_class tree_code_type[] = {
99 #include "all-tree.def"
102 #undef DEFTREECODE
103 #undef END_OF_BASE_TREE_CODES
105 /* Table indexed by tree code giving number of expression
106 operands beyond the fixed part of the node structure.
107 Not used for types or decls. */
109 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
110 #define END_OF_BASE_TREE_CODES 0,
112 const unsigned char tree_code_length[] = {
113 #include "all-tree.def"
116 #undef DEFTREECODE
117 #undef END_OF_BASE_TREE_CODES
119 /* Names of tree components.
120 Used for printing out the tree and error messages. */
121 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
122 #define END_OF_BASE_TREE_CODES "@dummy",
124 static const char *const tree_code_name[] = {
125 #include "all-tree.def"
128 #undef DEFTREECODE
129 #undef END_OF_BASE_TREE_CODES
131 /* Each tree code class has an associated string representation.
132 These must correspond to the tree_code_class entries. */
134 const char *const tree_code_class_strings[] =
136 "exceptional",
137 "constant",
138 "type",
139 "declaration",
140 "reference",
141 "comparison",
142 "unary",
143 "binary",
144 "statement",
145 "vl_exp",
146 "expression"
149 /* obstack.[ch] explicitly declined to prototype this. */
150 extern int _obstack_allocated_p (struct obstack *h, void *obj);
152 /* Statistics-gathering stuff. */
154 static int tree_code_counts[MAX_TREE_CODES];
155 int tree_node_counts[(int) all_kinds];
156 int tree_node_sizes[(int) all_kinds];
158 /* Keep in sync with tree.h:enum tree_node_kind. */
159 static const char * const tree_node_kind_names[] = {
160 "decls",
161 "types",
162 "blocks",
163 "stmts",
164 "refs",
165 "exprs",
166 "constants",
167 "identifiers",
168 "vecs",
169 "binfos",
170 "ssa names",
171 "constructors",
172 "random kinds",
173 "lang_decl kinds",
174 "lang_type kinds",
175 "omp clauses",
178 /* Unique id for next decl created. */
179 static GTY(()) int next_decl_uid;
180 /* Unique id for next type created. */
181 static GTY(()) int next_type_uid = 1;
182 /* Unique id for next debug decl created. Use negative numbers,
183 to catch erroneous uses. */
184 static GTY(()) int next_debug_decl_uid;
186 /* Since we cannot rehash a type after it is in the table, we have to
187 keep the hash code. */
189 struct GTY((for_user)) type_hash {
190 unsigned long hash;
191 tree type;
194 /* Initial size of the hash table (rounded to next prime). */
195 #define TYPE_HASH_INITIAL_SIZE 1000
197 struct type_cache_hasher : ggc_cache_ptr_hash<type_hash>
199 static hashval_t hash (type_hash *t) { return t->hash; }
200 static bool equal (type_hash *a, type_hash *b);
202 static int
203 keep_cache_entry (type_hash *&t)
205 return ggc_marked_p (t->type);
209 /* Now here is the hash table. When recording a type, it is added to
210 the slot whose index is the hash code. Note that the hash table is
211 used for several kinds of types (function types, array types and
212 array index range types, for now). While all these live in the
213 same table, they are completely independent, and the hash code is
214 computed differently for each of these. */
216 static GTY ((cache)) hash_table<type_cache_hasher> *type_hash_table;
218 /* Hash table and temporary node for larger integer const values. */
219 static GTY (()) tree int_cst_node;
221 struct int_cst_hasher : ggc_cache_ptr_hash<tree_node>
223 static hashval_t hash (tree t);
224 static bool equal (tree x, tree y);
227 static GTY ((cache)) hash_table<int_cst_hasher> *int_cst_hash_table;
229 /* Hash table for optimization flags and target option flags. Use the same
230 hash table for both sets of options. Nodes for building the current
231 optimization and target option nodes. The assumption is most of the time
232 the options created will already be in the hash table, so we avoid
233 allocating and freeing up a node repeatably. */
234 static GTY (()) tree cl_optimization_node;
235 static GTY (()) tree cl_target_option_node;
237 struct cl_option_hasher : ggc_cache_ptr_hash<tree_node>
239 static hashval_t hash (tree t);
240 static bool equal (tree x, tree y);
243 static GTY ((cache)) hash_table<cl_option_hasher> *cl_option_hash_table;
245 /* General tree->tree mapping structure for use in hash tables. */
248 static GTY ((cache))
249 hash_table<tree_decl_map_cache_hasher> *debug_expr_for_decl;
251 static GTY ((cache))
252 hash_table<tree_decl_map_cache_hasher> *value_expr_for_decl;
254 struct tree_vec_map_cache_hasher : ggc_cache_ptr_hash<tree_vec_map>
256 static hashval_t hash (tree_vec_map *m) { return DECL_UID (m->base.from); }
258 static bool
259 equal (tree_vec_map *a, tree_vec_map *b)
261 return a->base.from == b->base.from;
264 static int
265 keep_cache_entry (tree_vec_map *&m)
267 return ggc_marked_p (m->base.from);
271 static GTY ((cache))
272 hash_table<tree_vec_map_cache_hasher> *debug_args_for_decl;
274 static void set_type_quals (tree, int);
275 static void print_type_hash_statistics (void);
276 static void print_debug_expr_statistics (void);
277 static void print_value_expr_statistics (void);
278 static void type_hash_list (const_tree, inchash::hash &);
279 static void attribute_hash_list (const_tree, inchash::hash &);
281 tree global_trees[TI_MAX];
282 tree integer_types[itk_none];
284 bool int_n_enabled_p[NUM_INT_N_ENTS];
285 struct int_n_trees_t int_n_trees [NUM_INT_N_ENTS];
287 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
289 /* Number of operands for each OpenMP clause. */
290 unsigned const char omp_clause_num_ops[] =
292 0, /* OMP_CLAUSE_ERROR */
293 1, /* OMP_CLAUSE_PRIVATE */
294 1, /* OMP_CLAUSE_SHARED */
295 1, /* OMP_CLAUSE_FIRSTPRIVATE */
296 2, /* OMP_CLAUSE_LASTPRIVATE */
297 4, /* OMP_CLAUSE_REDUCTION */
298 1, /* OMP_CLAUSE_COPYIN */
299 1, /* OMP_CLAUSE_COPYPRIVATE */
300 3, /* OMP_CLAUSE_LINEAR */
301 2, /* OMP_CLAUSE_ALIGNED */
302 1, /* OMP_CLAUSE_DEPEND */
303 1, /* OMP_CLAUSE_UNIFORM */
304 2, /* OMP_CLAUSE_FROM */
305 2, /* OMP_CLAUSE_TO */
306 2, /* OMP_CLAUSE_MAP */
307 2, /* OMP_CLAUSE__CACHE_ */
308 1, /* OMP_CLAUSE_DEVICE_RESIDENT */
309 1, /* OMP_CLAUSE_USE_DEVICE */
310 2, /* OMP_CLAUSE_GANG */
311 1, /* OMP_CLAUSE_ASYNC */
312 1, /* OMP_CLAUSE_WAIT */
313 0, /* OMP_CLAUSE_AUTO */
314 0, /* OMP_CLAUSE_SEQ */
315 1, /* OMP_CLAUSE__LOOPTEMP_ */
316 1, /* OMP_CLAUSE_IF */
317 1, /* OMP_CLAUSE_NUM_THREADS */
318 1, /* OMP_CLAUSE_SCHEDULE */
319 0, /* OMP_CLAUSE_NOWAIT */
320 0, /* OMP_CLAUSE_ORDERED */
321 0, /* OMP_CLAUSE_DEFAULT */
322 3, /* OMP_CLAUSE_COLLAPSE */
323 0, /* OMP_CLAUSE_UNTIED */
324 1, /* OMP_CLAUSE_FINAL */
325 0, /* OMP_CLAUSE_MERGEABLE */
326 1, /* OMP_CLAUSE_DEVICE */
327 1, /* OMP_CLAUSE_DIST_SCHEDULE */
328 0, /* OMP_CLAUSE_INBRANCH */
329 0, /* OMP_CLAUSE_NOTINBRANCH */
330 1, /* OMP_CLAUSE_NUM_TEAMS */
331 1, /* OMP_CLAUSE_THREAD_LIMIT */
332 0, /* OMP_CLAUSE_PROC_BIND */
333 1, /* OMP_CLAUSE_SAFELEN */
334 1, /* OMP_CLAUSE_SIMDLEN */
335 0, /* OMP_CLAUSE_FOR */
336 0, /* OMP_CLAUSE_PARALLEL */
337 0, /* OMP_CLAUSE_SECTIONS */
338 0, /* OMP_CLAUSE_TASKGROUP */
339 1, /* OMP_CLAUSE__SIMDUID_ */
340 1, /* OMP_CLAUSE__CILK_FOR_COUNT_ */
341 0, /* OMP_CLAUSE_INDEPENDENT */
342 1, /* OMP_CLAUSE_WORKER */
343 1, /* OMP_CLAUSE_VECTOR */
344 1, /* OMP_CLAUSE_NUM_GANGS */
345 1, /* OMP_CLAUSE_NUM_WORKERS */
346 1, /* OMP_CLAUSE_VECTOR_LENGTH */
349 const char * const omp_clause_code_name[] =
351 "error_clause",
352 "private",
353 "shared",
354 "firstprivate",
355 "lastprivate",
356 "reduction",
357 "copyin",
358 "copyprivate",
359 "linear",
360 "aligned",
361 "depend",
362 "uniform",
363 "from",
364 "to",
365 "map",
366 "_cache_",
367 "device_resident",
368 "use_device",
369 "gang",
370 "async",
371 "wait",
372 "auto",
373 "seq",
374 "_looptemp_",
375 "if",
376 "num_threads",
377 "schedule",
378 "nowait",
379 "ordered",
380 "default",
381 "collapse",
382 "untied",
383 "final",
384 "mergeable",
385 "device",
386 "dist_schedule",
387 "inbranch",
388 "notinbranch",
389 "num_teams",
390 "thread_limit",
391 "proc_bind",
392 "safelen",
393 "simdlen",
394 "for",
395 "parallel",
396 "sections",
397 "taskgroup",
398 "_simduid_",
399 "_Cilk_for_count_",
400 "independent",
401 "worker",
402 "vector",
403 "num_gangs",
404 "num_workers",
405 "vector_length"
409 /* Return the tree node structure used by tree code CODE. */
411 static inline enum tree_node_structure_enum
412 tree_node_structure_for_code (enum tree_code code)
414 switch (TREE_CODE_CLASS (code))
416 case tcc_declaration:
418 switch (code)
420 case FIELD_DECL:
421 return TS_FIELD_DECL;
422 case PARM_DECL:
423 return TS_PARM_DECL;
424 case VAR_DECL:
425 return TS_VAR_DECL;
426 case LABEL_DECL:
427 return TS_LABEL_DECL;
428 case RESULT_DECL:
429 return TS_RESULT_DECL;
430 case DEBUG_EXPR_DECL:
431 return TS_DECL_WRTL;
432 case CONST_DECL:
433 return TS_CONST_DECL;
434 case TYPE_DECL:
435 return TS_TYPE_DECL;
436 case FUNCTION_DECL:
437 return TS_FUNCTION_DECL;
438 case TRANSLATION_UNIT_DECL:
439 return TS_TRANSLATION_UNIT_DECL;
440 default:
441 return TS_DECL_NON_COMMON;
444 case tcc_type:
445 return TS_TYPE_NON_COMMON;
446 case tcc_reference:
447 case tcc_comparison:
448 case tcc_unary:
449 case tcc_binary:
450 case tcc_expression:
451 case tcc_statement:
452 case tcc_vl_exp:
453 return TS_EXP;
454 default: /* tcc_constant and tcc_exceptional */
455 break;
457 switch (code)
459 /* tcc_constant cases. */
460 case VOID_CST: return TS_TYPED;
461 case INTEGER_CST: return TS_INT_CST;
462 case REAL_CST: return TS_REAL_CST;
463 case FIXED_CST: return TS_FIXED_CST;
464 case COMPLEX_CST: return TS_COMPLEX;
465 case VECTOR_CST: return TS_VECTOR;
466 case STRING_CST: return TS_STRING;
467 /* tcc_exceptional cases. */
468 case ERROR_MARK: return TS_COMMON;
469 case IDENTIFIER_NODE: return TS_IDENTIFIER;
470 case TREE_LIST: return TS_LIST;
471 case TREE_VEC: return TS_VEC;
472 case SSA_NAME: return TS_SSA_NAME;
473 case PLACEHOLDER_EXPR: return TS_COMMON;
474 case STATEMENT_LIST: return TS_STATEMENT_LIST;
475 case BLOCK: return TS_BLOCK;
476 case CONSTRUCTOR: return TS_CONSTRUCTOR;
477 case TREE_BINFO: return TS_BINFO;
478 case OMP_CLAUSE: return TS_OMP_CLAUSE;
479 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
480 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
482 default:
483 gcc_unreachable ();
488 /* Initialize tree_contains_struct to describe the hierarchy of tree
489 nodes. */
491 static void
492 initialize_tree_contains_struct (void)
494 unsigned i;
496 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
498 enum tree_code code;
499 enum tree_node_structure_enum ts_code;
501 code = (enum tree_code) i;
502 ts_code = tree_node_structure_for_code (code);
504 /* Mark the TS structure itself. */
505 tree_contains_struct[code][ts_code] = 1;
507 /* Mark all the structures that TS is derived from. */
508 switch (ts_code)
510 case TS_TYPED:
511 case TS_BLOCK:
512 MARK_TS_BASE (code);
513 break;
515 case TS_COMMON:
516 case TS_INT_CST:
517 case TS_REAL_CST:
518 case TS_FIXED_CST:
519 case TS_VECTOR:
520 case TS_STRING:
521 case TS_COMPLEX:
522 case TS_SSA_NAME:
523 case TS_CONSTRUCTOR:
524 case TS_EXP:
525 case TS_STATEMENT_LIST:
526 MARK_TS_TYPED (code);
527 break;
529 case TS_IDENTIFIER:
530 case TS_DECL_MINIMAL:
531 case TS_TYPE_COMMON:
532 case TS_LIST:
533 case TS_VEC:
534 case TS_BINFO:
535 case TS_OMP_CLAUSE:
536 case TS_OPTIMIZATION:
537 case TS_TARGET_OPTION:
538 MARK_TS_COMMON (code);
539 break;
541 case TS_TYPE_WITH_LANG_SPECIFIC:
542 MARK_TS_TYPE_COMMON (code);
543 break;
545 case TS_TYPE_NON_COMMON:
546 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
547 break;
549 case TS_DECL_COMMON:
550 MARK_TS_DECL_MINIMAL (code);
551 break;
553 case TS_DECL_WRTL:
554 case TS_CONST_DECL:
555 MARK_TS_DECL_COMMON (code);
556 break;
558 case TS_DECL_NON_COMMON:
559 MARK_TS_DECL_WITH_VIS (code);
560 break;
562 case TS_DECL_WITH_VIS:
563 case TS_PARM_DECL:
564 case TS_LABEL_DECL:
565 case TS_RESULT_DECL:
566 MARK_TS_DECL_WRTL (code);
567 break;
569 case TS_FIELD_DECL:
570 MARK_TS_DECL_COMMON (code);
571 break;
573 case TS_VAR_DECL:
574 MARK_TS_DECL_WITH_VIS (code);
575 break;
577 case TS_TYPE_DECL:
578 case TS_FUNCTION_DECL:
579 MARK_TS_DECL_NON_COMMON (code);
580 break;
582 case TS_TRANSLATION_UNIT_DECL:
583 MARK_TS_DECL_COMMON (code);
584 break;
586 default:
587 gcc_unreachable ();
591 /* Basic consistency checks for attributes used in fold. */
592 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
593 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
594 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
595 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
596 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
597 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
598 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
599 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
600 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
601 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
602 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
603 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
604 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
605 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
606 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
607 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
608 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
609 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
610 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
611 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
612 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
613 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
614 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
615 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
616 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
617 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
618 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
619 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
620 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
621 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
622 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
623 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
624 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
625 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
626 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
627 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
628 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
629 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
630 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_MINIMAL]);
631 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_COMMON]);
635 /* Init tree.c. */
637 void
638 init_ttree (void)
640 /* Initialize the hash table of types. */
641 type_hash_table
642 = hash_table<type_cache_hasher>::create_ggc (TYPE_HASH_INITIAL_SIZE);
644 debug_expr_for_decl
645 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
647 value_expr_for_decl
648 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
650 int_cst_hash_table = hash_table<int_cst_hasher>::create_ggc (1024);
652 int_cst_node = make_int_cst (1, 1);
654 cl_option_hash_table = hash_table<cl_option_hasher>::create_ggc (64);
656 cl_optimization_node = make_node (OPTIMIZATION_NODE);
657 cl_target_option_node = make_node (TARGET_OPTION_NODE);
659 /* Initialize the tree_contains_struct array. */
660 initialize_tree_contains_struct ();
661 lang_hooks.init_ts ();
665 /* The name of the object as the assembler will see it (but before any
666 translations made by ASM_OUTPUT_LABELREF). Often this is the same
667 as DECL_NAME. It is an IDENTIFIER_NODE. */
668 tree
669 decl_assembler_name (tree decl)
671 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
672 lang_hooks.set_decl_assembler_name (decl);
673 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
676 /* When the target supports COMDAT groups, this indicates which group the
677 DECL is associated with. This can be either an IDENTIFIER_NODE or a
678 decl, in which case its DECL_ASSEMBLER_NAME identifies the group. */
679 tree
680 decl_comdat_group (const_tree node)
682 struct symtab_node *snode = symtab_node::get (node);
683 if (!snode)
684 return NULL;
685 return snode->get_comdat_group ();
688 /* Likewise, but make sure it's been reduced to an IDENTIFIER_NODE. */
689 tree
690 decl_comdat_group_id (const_tree node)
692 struct symtab_node *snode = symtab_node::get (node);
693 if (!snode)
694 return NULL;
695 return snode->get_comdat_group_id ();
698 /* When the target supports named section, return its name as IDENTIFIER_NODE
699 or NULL if it is in no section. */
700 const char *
701 decl_section_name (const_tree node)
703 struct symtab_node *snode = symtab_node::get (node);
704 if (!snode)
705 return NULL;
706 return snode->get_section ();
709 /* Set section section name of NODE to VALUE (that is expected to
710 be identifier node) */
711 void
712 set_decl_section_name (tree node, const char *value)
714 struct symtab_node *snode;
716 if (value == NULL)
718 snode = symtab_node::get (node);
719 if (!snode)
720 return;
722 else if (TREE_CODE (node) == VAR_DECL)
723 snode = varpool_node::get_create (node);
724 else
725 snode = cgraph_node::get_create (node);
726 snode->set_section (value);
729 /* Return TLS model of a variable NODE. */
730 enum tls_model
731 decl_tls_model (const_tree node)
733 struct varpool_node *snode = varpool_node::get (node);
734 if (!snode)
735 return TLS_MODEL_NONE;
736 return snode->tls_model;
739 /* Set TLS model of variable NODE to MODEL. */
740 void
741 set_decl_tls_model (tree node, enum tls_model model)
743 struct varpool_node *vnode;
745 if (model == TLS_MODEL_NONE)
747 vnode = varpool_node::get (node);
748 if (!vnode)
749 return;
751 else
752 vnode = varpool_node::get_create (node);
753 vnode->tls_model = model;
756 /* Compute the number of bytes occupied by a tree with code CODE.
757 This function cannot be used for nodes that have variable sizes,
758 including TREE_VEC, INTEGER_CST, STRING_CST, and CALL_EXPR. */
759 size_t
760 tree_code_size (enum tree_code code)
762 switch (TREE_CODE_CLASS (code))
764 case tcc_declaration: /* A decl node */
766 switch (code)
768 case FIELD_DECL:
769 return sizeof (struct tree_field_decl);
770 case PARM_DECL:
771 return sizeof (struct tree_parm_decl);
772 case VAR_DECL:
773 return sizeof (struct tree_var_decl);
774 case LABEL_DECL:
775 return sizeof (struct tree_label_decl);
776 case RESULT_DECL:
777 return sizeof (struct tree_result_decl);
778 case CONST_DECL:
779 return sizeof (struct tree_const_decl);
780 case TYPE_DECL:
781 return sizeof (struct tree_type_decl);
782 case FUNCTION_DECL:
783 return sizeof (struct tree_function_decl);
784 case DEBUG_EXPR_DECL:
785 return sizeof (struct tree_decl_with_rtl);
786 case TRANSLATION_UNIT_DECL:
787 return sizeof (struct tree_translation_unit_decl);
788 case NAMESPACE_DECL:
789 case IMPORTED_DECL:
790 case NAMELIST_DECL:
791 return sizeof (struct tree_decl_non_common);
792 default:
793 return lang_hooks.tree_size (code);
797 case tcc_type: /* a type node */
798 return sizeof (struct tree_type_non_common);
800 case tcc_reference: /* a reference */
801 case tcc_expression: /* an expression */
802 case tcc_statement: /* an expression with side effects */
803 case tcc_comparison: /* a comparison expression */
804 case tcc_unary: /* a unary arithmetic expression */
805 case tcc_binary: /* a binary arithmetic expression */
806 return (sizeof (struct tree_exp)
807 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
809 case tcc_constant: /* a constant */
810 switch (code)
812 case VOID_CST: return sizeof (struct tree_typed);
813 case INTEGER_CST: gcc_unreachable ();
814 case REAL_CST: return sizeof (struct tree_real_cst);
815 case FIXED_CST: return sizeof (struct tree_fixed_cst);
816 case COMPLEX_CST: return sizeof (struct tree_complex);
817 case VECTOR_CST: return sizeof (struct tree_vector);
818 case STRING_CST: gcc_unreachable ();
819 default:
820 return lang_hooks.tree_size (code);
823 case tcc_exceptional: /* something random, like an identifier. */
824 switch (code)
826 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
827 case TREE_LIST: return sizeof (struct tree_list);
829 case ERROR_MARK:
830 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
832 case TREE_VEC:
833 case OMP_CLAUSE: gcc_unreachable ();
835 case SSA_NAME: return sizeof (struct tree_ssa_name);
837 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
838 case BLOCK: return sizeof (struct tree_block);
839 case CONSTRUCTOR: return sizeof (struct tree_constructor);
840 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
841 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
843 default:
844 return lang_hooks.tree_size (code);
847 default:
848 gcc_unreachable ();
852 /* Compute the number of bytes occupied by NODE. This routine only
853 looks at TREE_CODE, except for those nodes that have variable sizes. */
854 size_t
855 tree_size (const_tree node)
857 const enum tree_code code = TREE_CODE (node);
858 switch (code)
860 case INTEGER_CST:
861 return (sizeof (struct tree_int_cst)
862 + (TREE_INT_CST_EXT_NUNITS (node) - 1) * sizeof (HOST_WIDE_INT));
864 case TREE_BINFO:
865 return (offsetof (struct tree_binfo, base_binfos)
866 + vec<tree, va_gc>
867 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
869 case TREE_VEC:
870 return (sizeof (struct tree_vec)
871 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
873 case VECTOR_CST:
874 return (sizeof (struct tree_vector)
875 + (TYPE_VECTOR_SUBPARTS (TREE_TYPE (node)) - 1) * sizeof (tree));
877 case STRING_CST:
878 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
880 case OMP_CLAUSE:
881 return (sizeof (struct tree_omp_clause)
882 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
883 * sizeof (tree));
885 default:
886 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
887 return (sizeof (struct tree_exp)
888 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
889 else
890 return tree_code_size (code);
894 /* Record interesting allocation statistics for a tree node with CODE
895 and LENGTH. */
897 static void
898 record_node_allocation_statistics (enum tree_code code ATTRIBUTE_UNUSED,
899 size_t length ATTRIBUTE_UNUSED)
901 enum tree_code_class type = TREE_CODE_CLASS (code);
902 tree_node_kind kind;
904 if (!GATHER_STATISTICS)
905 return;
907 switch (type)
909 case tcc_declaration: /* A decl node */
910 kind = d_kind;
911 break;
913 case tcc_type: /* a type node */
914 kind = t_kind;
915 break;
917 case tcc_statement: /* an expression with side effects */
918 kind = s_kind;
919 break;
921 case tcc_reference: /* a reference */
922 kind = r_kind;
923 break;
925 case tcc_expression: /* an expression */
926 case tcc_comparison: /* a comparison expression */
927 case tcc_unary: /* a unary arithmetic expression */
928 case tcc_binary: /* a binary arithmetic expression */
929 kind = e_kind;
930 break;
932 case tcc_constant: /* a constant */
933 kind = c_kind;
934 break;
936 case tcc_exceptional: /* something random, like an identifier. */
937 switch (code)
939 case IDENTIFIER_NODE:
940 kind = id_kind;
941 break;
943 case TREE_VEC:
944 kind = vec_kind;
945 break;
947 case TREE_BINFO:
948 kind = binfo_kind;
949 break;
951 case SSA_NAME:
952 kind = ssa_name_kind;
953 break;
955 case BLOCK:
956 kind = b_kind;
957 break;
959 case CONSTRUCTOR:
960 kind = constr_kind;
961 break;
963 case OMP_CLAUSE:
964 kind = omp_clause_kind;
965 break;
967 default:
968 kind = x_kind;
969 break;
971 break;
973 case tcc_vl_exp:
974 kind = e_kind;
975 break;
977 default:
978 gcc_unreachable ();
981 tree_code_counts[(int) code]++;
982 tree_node_counts[(int) kind]++;
983 tree_node_sizes[(int) kind] += length;
986 /* Allocate and return a new UID from the DECL_UID namespace. */
989 allocate_decl_uid (void)
991 return next_decl_uid++;
994 /* Return a newly allocated node of code CODE. For decl and type
995 nodes, some other fields are initialized. The rest of the node is
996 initialized to zero. This function cannot be used for TREE_VEC,
997 INTEGER_CST or OMP_CLAUSE nodes, which is enforced by asserts in
998 tree_code_size.
1000 Achoo! I got a code in the node. */
1002 tree
1003 make_node_stat (enum tree_code code MEM_STAT_DECL)
1005 tree t;
1006 enum tree_code_class type = TREE_CODE_CLASS (code);
1007 size_t length = tree_code_size (code);
1009 record_node_allocation_statistics (code, length);
1011 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1012 TREE_SET_CODE (t, code);
1014 switch (type)
1016 case tcc_statement:
1017 TREE_SIDE_EFFECTS (t) = 1;
1018 break;
1020 case tcc_declaration:
1021 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1023 if (code == FUNCTION_DECL)
1025 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
1026 DECL_MODE (t) = FUNCTION_MODE;
1028 else
1029 DECL_ALIGN (t) = 1;
1031 DECL_SOURCE_LOCATION (t) = input_location;
1032 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
1033 DECL_UID (t) = --next_debug_decl_uid;
1034 else
1036 DECL_UID (t) = allocate_decl_uid ();
1037 SET_DECL_PT_UID (t, -1);
1039 if (TREE_CODE (t) == LABEL_DECL)
1040 LABEL_DECL_UID (t) = -1;
1042 break;
1044 case tcc_type:
1045 TYPE_UID (t) = next_type_uid++;
1046 TYPE_ALIGN (t) = BITS_PER_UNIT;
1047 TYPE_USER_ALIGN (t) = 0;
1048 TYPE_MAIN_VARIANT (t) = t;
1049 TYPE_CANONICAL (t) = t;
1051 /* Default to no attributes for type, but let target change that. */
1052 TYPE_ATTRIBUTES (t) = NULL_TREE;
1053 targetm.set_default_type_attributes (t);
1055 /* We have not yet computed the alias set for this type. */
1056 TYPE_ALIAS_SET (t) = -1;
1057 break;
1059 case tcc_constant:
1060 TREE_CONSTANT (t) = 1;
1061 break;
1063 case tcc_expression:
1064 switch (code)
1066 case INIT_EXPR:
1067 case MODIFY_EXPR:
1068 case VA_ARG_EXPR:
1069 case PREDECREMENT_EXPR:
1070 case PREINCREMENT_EXPR:
1071 case POSTDECREMENT_EXPR:
1072 case POSTINCREMENT_EXPR:
1073 /* All of these have side-effects, no matter what their
1074 operands are. */
1075 TREE_SIDE_EFFECTS (t) = 1;
1076 break;
1078 default:
1079 break;
1081 break;
1083 case tcc_exceptional:
1084 switch (code)
1086 case TARGET_OPTION_NODE:
1087 TREE_TARGET_OPTION(t)
1088 = ggc_cleared_alloc<struct cl_target_option> ();
1089 break;
1091 case OPTIMIZATION_NODE:
1092 TREE_OPTIMIZATION (t)
1093 = ggc_cleared_alloc<struct cl_optimization> ();
1094 break;
1096 default:
1097 break;
1099 break;
1101 default:
1102 /* Other classes need no special treatment. */
1103 break;
1106 return t;
1109 /* Return a new node with the same contents as NODE except that its
1110 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
1112 tree
1113 copy_node_stat (tree node MEM_STAT_DECL)
1115 tree t;
1116 enum tree_code code = TREE_CODE (node);
1117 size_t length;
1119 gcc_assert (code != STATEMENT_LIST);
1121 length = tree_size (node);
1122 record_node_allocation_statistics (code, length);
1123 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1124 memcpy (t, node, length);
1126 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
1127 TREE_CHAIN (t) = 0;
1128 TREE_ASM_WRITTEN (t) = 0;
1129 TREE_VISITED (t) = 0;
1131 if (TREE_CODE_CLASS (code) == tcc_declaration)
1133 if (code == DEBUG_EXPR_DECL)
1134 DECL_UID (t) = --next_debug_decl_uid;
1135 else
1137 DECL_UID (t) = allocate_decl_uid ();
1138 if (DECL_PT_UID_SET_P (node))
1139 SET_DECL_PT_UID (t, DECL_PT_UID (node));
1141 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
1142 && DECL_HAS_VALUE_EXPR_P (node))
1144 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
1145 DECL_HAS_VALUE_EXPR_P (t) = 1;
1147 /* DECL_DEBUG_EXPR is copied explicitely by callers. */
1148 if (TREE_CODE (node) == VAR_DECL)
1150 DECL_HAS_DEBUG_EXPR_P (t) = 0;
1151 t->decl_with_vis.symtab_node = NULL;
1153 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
1155 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
1156 DECL_HAS_INIT_PRIORITY_P (t) = 1;
1158 if (TREE_CODE (node) == FUNCTION_DECL)
1160 DECL_STRUCT_FUNCTION (t) = NULL;
1161 t->decl_with_vis.symtab_node = NULL;
1164 else if (TREE_CODE_CLASS (code) == tcc_type)
1166 TYPE_UID (t) = next_type_uid++;
1167 /* The following is so that the debug code for
1168 the copy is different from the original type.
1169 The two statements usually duplicate each other
1170 (because they clear fields of the same union),
1171 but the optimizer should catch that. */
1172 TYPE_SYMTAB_POINTER (t) = 0;
1173 TYPE_SYMTAB_ADDRESS (t) = 0;
1175 /* Do not copy the values cache. */
1176 if (TYPE_CACHED_VALUES_P (t))
1178 TYPE_CACHED_VALUES_P (t) = 0;
1179 TYPE_CACHED_VALUES (t) = NULL_TREE;
1182 else if (code == TARGET_OPTION_NODE)
1184 TREE_TARGET_OPTION (t) = ggc_alloc<struct cl_target_option>();
1185 memcpy (TREE_TARGET_OPTION (t), TREE_TARGET_OPTION (node),
1186 sizeof (struct cl_target_option));
1188 else if (code == OPTIMIZATION_NODE)
1190 TREE_OPTIMIZATION (t) = ggc_alloc<struct cl_optimization>();
1191 memcpy (TREE_OPTIMIZATION (t), TREE_OPTIMIZATION (node),
1192 sizeof (struct cl_optimization));
1195 return t;
1198 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1199 For example, this can copy a list made of TREE_LIST nodes. */
1201 tree
1202 copy_list (tree list)
1204 tree head;
1205 tree prev, next;
1207 if (list == 0)
1208 return 0;
1210 head = prev = copy_node (list);
1211 next = TREE_CHAIN (list);
1212 while (next)
1214 TREE_CHAIN (prev) = copy_node (next);
1215 prev = TREE_CHAIN (prev);
1216 next = TREE_CHAIN (next);
1218 return head;
1222 /* Return the value that TREE_INT_CST_EXT_NUNITS should have for an
1223 INTEGER_CST with value CST and type TYPE. */
1225 static unsigned int
1226 get_int_cst_ext_nunits (tree type, const wide_int &cst)
1228 gcc_checking_assert (cst.get_precision () == TYPE_PRECISION (type));
1229 /* We need an extra zero HWI if CST is an unsigned integer with its
1230 upper bit set, and if CST occupies a whole number of HWIs. */
1231 if (TYPE_UNSIGNED (type)
1232 && wi::neg_p (cst)
1233 && (cst.get_precision () % HOST_BITS_PER_WIDE_INT) == 0)
1234 return cst.get_precision () / HOST_BITS_PER_WIDE_INT + 1;
1235 return cst.get_len ();
1238 /* Return a new INTEGER_CST with value CST and type TYPE. */
1240 static tree
1241 build_new_int_cst (tree type, const wide_int &cst)
1243 unsigned int len = cst.get_len ();
1244 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1245 tree nt = make_int_cst (len, ext_len);
1247 if (len < ext_len)
1249 --ext_len;
1250 TREE_INT_CST_ELT (nt, ext_len) = 0;
1251 for (unsigned int i = len; i < ext_len; ++i)
1252 TREE_INT_CST_ELT (nt, i) = -1;
1254 else if (TYPE_UNSIGNED (type)
1255 && cst.get_precision () < len * HOST_BITS_PER_WIDE_INT)
1257 len--;
1258 TREE_INT_CST_ELT (nt, len)
1259 = zext_hwi (cst.elt (len),
1260 cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1263 for (unsigned int i = 0; i < len; i++)
1264 TREE_INT_CST_ELT (nt, i) = cst.elt (i);
1265 TREE_TYPE (nt) = type;
1266 return nt;
1269 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1271 tree
1272 build_int_cst (tree type, HOST_WIDE_INT low)
1274 /* Support legacy code. */
1275 if (!type)
1276 type = integer_type_node;
1278 return wide_int_to_tree (type, wi::shwi (low, TYPE_PRECISION (type)));
1281 tree
1282 build_int_cstu (tree type, unsigned HOST_WIDE_INT cst)
1284 return wide_int_to_tree (type, wi::uhwi (cst, TYPE_PRECISION (type)));
1287 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1289 tree
1290 build_int_cst_type (tree type, HOST_WIDE_INT low)
1292 gcc_assert (type);
1293 return wide_int_to_tree (type, wi::shwi (low, TYPE_PRECISION (type)));
1296 /* Constructs tree in type TYPE from with value given by CST. Signedness
1297 of CST is assumed to be the same as the signedness of TYPE. */
1299 tree
1300 double_int_to_tree (tree type, double_int cst)
1302 return wide_int_to_tree (type, widest_int::from (cst, TYPE_SIGN (type)));
1305 /* We force the wide_int CST to the range of the type TYPE by sign or
1306 zero extending it. OVERFLOWABLE indicates if we are interested in
1307 overflow of the value, when >0 we are only interested in signed
1308 overflow, for <0 we are interested in any overflow. OVERFLOWED
1309 indicates whether overflow has already occurred. CONST_OVERFLOWED
1310 indicates whether constant overflow has already occurred. We force
1311 T's value to be within range of T's type (by setting to 0 or 1 all
1312 the bits outside the type's range). We set TREE_OVERFLOWED if,
1313 OVERFLOWED is nonzero,
1314 or OVERFLOWABLE is >0 and signed overflow occurs
1315 or OVERFLOWABLE is <0 and any overflow occurs
1316 We return a new tree node for the extended wide_int. The node
1317 is shared if no overflow flags are set. */
1320 tree
1321 force_fit_type (tree type, const wide_int_ref &cst,
1322 int overflowable, bool overflowed)
1324 signop sign = TYPE_SIGN (type);
1326 /* If we need to set overflow flags, return a new unshared node. */
1327 if (overflowed || !wi::fits_to_tree_p (cst, type))
1329 if (overflowed
1330 || overflowable < 0
1331 || (overflowable > 0 && sign == SIGNED))
1333 wide_int tmp = wide_int::from (cst, TYPE_PRECISION (type), sign);
1334 tree t = build_new_int_cst (type, tmp);
1335 TREE_OVERFLOW (t) = 1;
1336 return t;
1340 /* Else build a shared node. */
1341 return wide_int_to_tree (type, cst);
1344 /* These are the hash table functions for the hash table of INTEGER_CST
1345 nodes of a sizetype. */
1347 /* Return the hash code code X, an INTEGER_CST. */
1349 hashval_t
1350 int_cst_hasher::hash (tree x)
1352 const_tree const t = x;
1353 hashval_t code = TYPE_UID (TREE_TYPE (t));
1354 int i;
1356 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
1357 code ^= TREE_INT_CST_ELT (t, i);
1359 return code;
1362 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1363 is the same as that given by *Y, which is the same. */
1365 bool
1366 int_cst_hasher::equal (tree x, tree y)
1368 const_tree const xt = x;
1369 const_tree const yt = y;
1371 if (TREE_TYPE (xt) != TREE_TYPE (yt)
1372 || TREE_INT_CST_NUNITS (xt) != TREE_INT_CST_NUNITS (yt)
1373 || TREE_INT_CST_EXT_NUNITS (xt) != TREE_INT_CST_EXT_NUNITS (yt))
1374 return false;
1376 for (int i = 0; i < TREE_INT_CST_NUNITS (xt); i++)
1377 if (TREE_INT_CST_ELT (xt, i) != TREE_INT_CST_ELT (yt, i))
1378 return false;
1380 return true;
1383 /* Create an INT_CST node of TYPE and value CST.
1384 The returned node is always shared. For small integers we use a
1385 per-type vector cache, for larger ones we use a single hash table.
1386 The value is extended from its precision according to the sign of
1387 the type to be a multiple of HOST_BITS_PER_WIDE_INT. This defines
1388 the upper bits and ensures that hashing and value equality based
1389 upon the underlying HOST_WIDE_INTs works without masking. */
1391 tree
1392 wide_int_to_tree (tree type, const wide_int_ref &pcst)
1394 tree t;
1395 int ix = -1;
1396 int limit = 0;
1398 gcc_assert (type);
1399 unsigned int prec = TYPE_PRECISION (type);
1400 signop sgn = TYPE_SIGN (type);
1402 /* Verify that everything is canonical. */
1403 int l = pcst.get_len ();
1404 if (l > 1)
1406 if (pcst.elt (l - 1) == 0)
1407 gcc_checking_assert (pcst.elt (l - 2) < 0);
1408 if (pcst.elt (l - 1) == (HOST_WIDE_INT) -1)
1409 gcc_checking_assert (pcst.elt (l - 2) >= 0);
1412 wide_int cst = wide_int::from (pcst, prec, sgn);
1413 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1415 if (ext_len == 1)
1417 /* We just need to store a single HOST_WIDE_INT. */
1418 HOST_WIDE_INT hwi;
1419 if (TYPE_UNSIGNED (type))
1420 hwi = cst.to_uhwi ();
1421 else
1422 hwi = cst.to_shwi ();
1424 switch (TREE_CODE (type))
1426 case NULLPTR_TYPE:
1427 gcc_assert (hwi == 0);
1428 /* Fallthru. */
1430 case POINTER_TYPE:
1431 case REFERENCE_TYPE:
1432 case POINTER_BOUNDS_TYPE:
1433 /* Cache NULL pointer and zero bounds. */
1434 if (hwi == 0)
1436 limit = 1;
1437 ix = 0;
1439 break;
1441 case BOOLEAN_TYPE:
1442 /* Cache false or true. */
1443 limit = 2;
1444 if (hwi < 2)
1445 ix = hwi;
1446 break;
1448 case INTEGER_TYPE:
1449 case OFFSET_TYPE:
1450 if (TYPE_SIGN (type) == UNSIGNED)
1452 /* Cache [0, N). */
1453 limit = INTEGER_SHARE_LIMIT;
1454 if (IN_RANGE (hwi, 0, INTEGER_SHARE_LIMIT - 1))
1455 ix = hwi;
1457 else
1459 /* Cache [-1, N). */
1460 limit = INTEGER_SHARE_LIMIT + 1;
1461 if (IN_RANGE (hwi, -1, INTEGER_SHARE_LIMIT - 1))
1462 ix = hwi + 1;
1464 break;
1466 case ENUMERAL_TYPE:
1467 break;
1469 default:
1470 gcc_unreachable ();
1473 if (ix >= 0)
1475 /* Look for it in the type's vector of small shared ints. */
1476 if (!TYPE_CACHED_VALUES_P (type))
1478 TYPE_CACHED_VALUES_P (type) = 1;
1479 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1482 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1483 if (t)
1484 /* Make sure no one is clobbering the shared constant. */
1485 gcc_checking_assert (TREE_TYPE (t) == type
1486 && TREE_INT_CST_NUNITS (t) == 1
1487 && TREE_INT_CST_OFFSET_NUNITS (t) == 1
1488 && TREE_INT_CST_EXT_NUNITS (t) == 1
1489 && TREE_INT_CST_ELT (t, 0) == hwi);
1490 else
1492 /* Create a new shared int. */
1493 t = build_new_int_cst (type, cst);
1494 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1497 else
1499 /* Use the cache of larger shared ints, using int_cst_node as
1500 a temporary. */
1502 TREE_INT_CST_ELT (int_cst_node, 0) = hwi;
1503 TREE_TYPE (int_cst_node) = type;
1505 tree *slot = int_cst_hash_table->find_slot (int_cst_node, INSERT);
1506 t = *slot;
1507 if (!t)
1509 /* Insert this one into the hash table. */
1510 t = int_cst_node;
1511 *slot = t;
1512 /* Make a new node for next time round. */
1513 int_cst_node = make_int_cst (1, 1);
1517 else
1519 /* The value either hashes properly or we drop it on the floor
1520 for the gc to take care of. There will not be enough of them
1521 to worry about. */
1523 tree nt = build_new_int_cst (type, cst);
1524 tree *slot = int_cst_hash_table->find_slot (nt, INSERT);
1525 t = *slot;
1526 if (!t)
1528 /* Insert this one into the hash table. */
1529 t = nt;
1530 *slot = t;
1534 return t;
1537 void
1538 cache_integer_cst (tree t)
1540 tree type = TREE_TYPE (t);
1541 int ix = -1;
1542 int limit = 0;
1543 int prec = TYPE_PRECISION (type);
1545 gcc_assert (!TREE_OVERFLOW (t));
1547 switch (TREE_CODE (type))
1549 case NULLPTR_TYPE:
1550 gcc_assert (integer_zerop (t));
1551 /* Fallthru. */
1553 case POINTER_TYPE:
1554 case REFERENCE_TYPE:
1555 /* Cache NULL pointer. */
1556 if (integer_zerop (t))
1558 limit = 1;
1559 ix = 0;
1561 break;
1563 case BOOLEAN_TYPE:
1564 /* Cache false or true. */
1565 limit = 2;
1566 if (wi::ltu_p (t, 2))
1567 ix = TREE_INT_CST_ELT (t, 0);
1568 break;
1570 case INTEGER_TYPE:
1571 case OFFSET_TYPE:
1572 if (TYPE_UNSIGNED (type))
1574 /* Cache 0..N */
1575 limit = INTEGER_SHARE_LIMIT;
1577 /* This is a little hokie, but if the prec is smaller than
1578 what is necessary to hold INTEGER_SHARE_LIMIT, then the
1579 obvious test will not get the correct answer. */
1580 if (prec < HOST_BITS_PER_WIDE_INT)
1582 if (tree_to_uhwi (t) < (unsigned HOST_WIDE_INT) INTEGER_SHARE_LIMIT)
1583 ix = tree_to_uhwi (t);
1585 else if (wi::ltu_p (t, INTEGER_SHARE_LIMIT))
1586 ix = tree_to_uhwi (t);
1588 else
1590 /* Cache -1..N */
1591 limit = INTEGER_SHARE_LIMIT + 1;
1593 if (integer_minus_onep (t))
1594 ix = 0;
1595 else if (!wi::neg_p (t))
1597 if (prec < HOST_BITS_PER_WIDE_INT)
1599 if (tree_to_shwi (t) < INTEGER_SHARE_LIMIT)
1600 ix = tree_to_shwi (t) + 1;
1602 else if (wi::ltu_p (t, INTEGER_SHARE_LIMIT))
1603 ix = tree_to_shwi (t) + 1;
1606 break;
1608 case ENUMERAL_TYPE:
1609 break;
1611 default:
1612 gcc_unreachable ();
1615 if (ix >= 0)
1617 /* Look for it in the type's vector of small shared ints. */
1618 if (!TYPE_CACHED_VALUES_P (type))
1620 TYPE_CACHED_VALUES_P (type) = 1;
1621 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1624 gcc_assert (TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) == NULL_TREE);
1625 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1627 else
1629 /* Use the cache of larger shared ints. */
1630 tree *slot = int_cst_hash_table->find_slot (t, INSERT);
1631 /* If there is already an entry for the number verify it's the
1632 same. */
1633 if (*slot)
1634 gcc_assert (wi::eq_p (tree (*slot), t));
1635 else
1636 /* Otherwise insert this one into the hash table. */
1637 *slot = t;
1642 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1643 and the rest are zeros. */
1645 tree
1646 build_low_bits_mask (tree type, unsigned bits)
1648 gcc_assert (bits <= TYPE_PRECISION (type));
1650 return wide_int_to_tree (type, wi::mask (bits, false,
1651 TYPE_PRECISION (type)));
1654 /* Checks that X is integer constant that can be expressed in (unsigned)
1655 HOST_WIDE_INT without loss of precision. */
1657 bool
1658 cst_and_fits_in_hwi (const_tree x)
1660 if (TREE_CODE (x) != INTEGER_CST)
1661 return false;
1663 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1664 return false;
1666 return TREE_INT_CST_NUNITS (x) == 1;
1669 /* Build a newly constructed VECTOR_CST node of length LEN. */
1671 tree
1672 make_vector_stat (unsigned len MEM_STAT_DECL)
1674 tree t;
1675 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1677 record_node_allocation_statistics (VECTOR_CST, length);
1679 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1681 TREE_SET_CODE (t, VECTOR_CST);
1682 TREE_CONSTANT (t) = 1;
1684 return t;
1687 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1688 are in a list pointed to by VALS. */
1690 tree
1691 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1693 int over = 0;
1694 unsigned cnt = 0;
1695 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1696 TREE_TYPE (v) = type;
1698 /* Iterate through elements and check for overflow. */
1699 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1701 tree value = vals[cnt];
1703 VECTOR_CST_ELT (v, cnt) = value;
1705 /* Don't crash if we get an address constant. */
1706 if (!CONSTANT_CLASS_P (value))
1707 continue;
1709 over |= TREE_OVERFLOW (value);
1712 TREE_OVERFLOW (v) = over;
1713 return v;
1716 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1717 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1719 tree
1720 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1722 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1723 unsigned HOST_WIDE_INT idx;
1724 tree value;
1726 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1727 vec[idx] = value;
1728 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1729 vec[idx] = build_zero_cst (TREE_TYPE (type));
1731 return build_vector (type, vec);
1734 /* Build a vector of type VECTYPE where all the elements are SCs. */
1735 tree
1736 build_vector_from_val (tree vectype, tree sc)
1738 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1740 if (sc == error_mark_node)
1741 return sc;
1743 /* Verify that the vector type is suitable for SC. Note that there
1744 is some inconsistency in the type-system with respect to restrict
1745 qualifications of pointers. Vector types always have a main-variant
1746 element type and the qualification is applied to the vector-type.
1747 So TREE_TYPE (vector-type) does not return a properly qualified
1748 vector element-type. */
1749 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1750 TREE_TYPE (vectype)));
1752 if (CONSTANT_CLASS_P (sc))
1754 tree *v = XALLOCAVEC (tree, nunits);
1755 for (i = 0; i < nunits; ++i)
1756 v[i] = sc;
1757 return build_vector (vectype, v);
1759 else
1761 vec<constructor_elt, va_gc> *v;
1762 vec_alloc (v, nunits);
1763 for (i = 0; i < nunits; ++i)
1764 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1765 return build_constructor (vectype, v);
1769 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1770 are in the vec pointed to by VALS. */
1771 tree
1772 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1774 tree c = make_node (CONSTRUCTOR);
1775 unsigned int i;
1776 constructor_elt *elt;
1777 bool constant_p = true;
1778 bool side_effects_p = false;
1780 TREE_TYPE (c) = type;
1781 CONSTRUCTOR_ELTS (c) = vals;
1783 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1785 /* Mostly ctors will have elts that don't have side-effects, so
1786 the usual case is to scan all the elements. Hence a single
1787 loop for both const and side effects, rather than one loop
1788 each (with early outs). */
1789 if (!TREE_CONSTANT (elt->value))
1790 constant_p = false;
1791 if (TREE_SIDE_EFFECTS (elt->value))
1792 side_effects_p = true;
1795 TREE_SIDE_EFFECTS (c) = side_effects_p;
1796 TREE_CONSTANT (c) = constant_p;
1798 return c;
1801 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1802 INDEX and VALUE. */
1803 tree
1804 build_constructor_single (tree type, tree index, tree value)
1806 vec<constructor_elt, va_gc> *v;
1807 constructor_elt elt = {index, value};
1809 vec_alloc (v, 1);
1810 v->quick_push (elt);
1812 return build_constructor (type, v);
1816 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1817 are in a list pointed to by VALS. */
1818 tree
1819 build_constructor_from_list (tree type, tree vals)
1821 tree t;
1822 vec<constructor_elt, va_gc> *v = NULL;
1824 if (vals)
1826 vec_alloc (v, list_length (vals));
1827 for (t = vals; t; t = TREE_CHAIN (t))
1828 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1831 return build_constructor (type, v);
1834 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
1835 of elements, provided as index/value pairs. */
1837 tree
1838 build_constructor_va (tree type, int nelts, ...)
1840 vec<constructor_elt, va_gc> *v = NULL;
1841 va_list p;
1843 va_start (p, nelts);
1844 vec_alloc (v, nelts);
1845 while (nelts--)
1847 tree index = va_arg (p, tree);
1848 tree value = va_arg (p, tree);
1849 CONSTRUCTOR_APPEND_ELT (v, index, value);
1851 va_end (p);
1852 return build_constructor (type, v);
1855 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1857 tree
1858 build_fixed (tree type, FIXED_VALUE_TYPE f)
1860 tree v;
1861 FIXED_VALUE_TYPE *fp;
1863 v = make_node (FIXED_CST);
1864 fp = ggc_alloc<fixed_value> ();
1865 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1867 TREE_TYPE (v) = type;
1868 TREE_FIXED_CST_PTR (v) = fp;
1869 return v;
1872 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1874 tree
1875 build_real (tree type, REAL_VALUE_TYPE d)
1877 tree v;
1878 REAL_VALUE_TYPE *dp;
1879 int overflow = 0;
1881 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1882 Consider doing it via real_convert now. */
1884 v = make_node (REAL_CST);
1885 dp = ggc_alloc<real_value> ();
1886 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1888 TREE_TYPE (v) = type;
1889 TREE_REAL_CST_PTR (v) = dp;
1890 TREE_OVERFLOW (v) = overflow;
1891 return v;
1894 /* Return a new REAL_CST node whose type is TYPE
1895 and whose value is the integer value of the INTEGER_CST node I. */
1897 REAL_VALUE_TYPE
1898 real_value_from_int_cst (const_tree type, const_tree i)
1900 REAL_VALUE_TYPE d;
1902 /* Clear all bits of the real value type so that we can later do
1903 bitwise comparisons to see if two values are the same. */
1904 memset (&d, 0, sizeof d);
1906 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode, i,
1907 TYPE_SIGN (TREE_TYPE (i)));
1908 return d;
1911 /* Given a tree representing an integer constant I, return a tree
1912 representing the same value as a floating-point constant of type TYPE. */
1914 tree
1915 build_real_from_int_cst (tree type, const_tree i)
1917 tree v;
1918 int overflow = TREE_OVERFLOW (i);
1920 v = build_real (type, real_value_from_int_cst (type, i));
1922 TREE_OVERFLOW (v) |= overflow;
1923 return v;
1926 /* Return a newly constructed STRING_CST node whose value is
1927 the LEN characters at STR.
1928 Note that for a C string literal, LEN should include the trailing NUL.
1929 The TREE_TYPE is not initialized. */
1931 tree
1932 build_string (int len, const char *str)
1934 tree s;
1935 size_t length;
1937 /* Do not waste bytes provided by padding of struct tree_string. */
1938 length = len + offsetof (struct tree_string, str) + 1;
1940 record_node_allocation_statistics (STRING_CST, length);
1942 s = (tree) ggc_internal_alloc (length);
1944 memset (s, 0, sizeof (struct tree_typed));
1945 TREE_SET_CODE (s, STRING_CST);
1946 TREE_CONSTANT (s) = 1;
1947 TREE_STRING_LENGTH (s) = len;
1948 memcpy (s->string.str, str, len);
1949 s->string.str[len] = '\0';
1951 return s;
1954 /* Return a newly constructed COMPLEX_CST node whose value is
1955 specified by the real and imaginary parts REAL and IMAG.
1956 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1957 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1959 tree
1960 build_complex (tree type, tree real, tree imag)
1962 tree t = make_node (COMPLEX_CST);
1964 TREE_REALPART (t) = real;
1965 TREE_IMAGPART (t) = imag;
1966 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1967 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1968 return t;
1971 /* Return a constant of arithmetic type TYPE which is the
1972 multiplicative identity of the set TYPE. */
1974 tree
1975 build_one_cst (tree type)
1977 switch (TREE_CODE (type))
1979 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1980 case POINTER_TYPE: case REFERENCE_TYPE:
1981 case OFFSET_TYPE:
1982 return build_int_cst (type, 1);
1984 case REAL_TYPE:
1985 return build_real (type, dconst1);
1987 case FIXED_POINT_TYPE:
1988 /* We can only generate 1 for accum types. */
1989 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1990 return build_fixed (type, FCONST1 (TYPE_MODE (type)));
1992 case VECTOR_TYPE:
1994 tree scalar = build_one_cst (TREE_TYPE (type));
1996 return build_vector_from_val (type, scalar);
1999 case COMPLEX_TYPE:
2000 return build_complex (type,
2001 build_one_cst (TREE_TYPE (type)),
2002 build_zero_cst (TREE_TYPE (type)));
2004 default:
2005 gcc_unreachable ();
2009 /* Return an integer of type TYPE containing all 1's in as much precision as
2010 it contains, or a complex or vector whose subparts are such integers. */
2012 tree
2013 build_all_ones_cst (tree type)
2015 if (TREE_CODE (type) == COMPLEX_TYPE)
2017 tree scalar = build_all_ones_cst (TREE_TYPE (type));
2018 return build_complex (type, scalar, scalar);
2020 else
2021 return build_minus_one_cst (type);
2024 /* Return a constant of arithmetic type TYPE which is the
2025 opposite of the multiplicative identity of the set TYPE. */
2027 tree
2028 build_minus_one_cst (tree type)
2030 switch (TREE_CODE (type))
2032 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2033 case POINTER_TYPE: case REFERENCE_TYPE:
2034 case OFFSET_TYPE:
2035 return build_int_cst (type, -1);
2037 case REAL_TYPE:
2038 return build_real (type, dconstm1);
2040 case FIXED_POINT_TYPE:
2041 /* We can only generate 1 for accum types. */
2042 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2043 return build_fixed (type, fixed_from_double_int (double_int_minus_one,
2044 TYPE_MODE (type)));
2046 case VECTOR_TYPE:
2048 tree scalar = build_minus_one_cst (TREE_TYPE (type));
2050 return build_vector_from_val (type, scalar);
2053 case COMPLEX_TYPE:
2054 return build_complex (type,
2055 build_minus_one_cst (TREE_TYPE (type)),
2056 build_zero_cst (TREE_TYPE (type)));
2058 default:
2059 gcc_unreachable ();
2063 /* Build 0 constant of type TYPE. This is used by constructor folding
2064 and thus the constant should be represented in memory by
2065 zero(es). */
2067 tree
2068 build_zero_cst (tree type)
2070 switch (TREE_CODE (type))
2072 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2073 case POINTER_TYPE: case REFERENCE_TYPE:
2074 case OFFSET_TYPE: case NULLPTR_TYPE:
2075 return build_int_cst (type, 0);
2077 case REAL_TYPE:
2078 return build_real (type, dconst0);
2080 case FIXED_POINT_TYPE:
2081 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
2083 case VECTOR_TYPE:
2085 tree scalar = build_zero_cst (TREE_TYPE (type));
2087 return build_vector_from_val (type, scalar);
2090 case COMPLEX_TYPE:
2092 tree zero = build_zero_cst (TREE_TYPE (type));
2094 return build_complex (type, zero, zero);
2097 default:
2098 if (!AGGREGATE_TYPE_P (type))
2099 return fold_convert (type, integer_zero_node);
2100 return build_constructor (type, NULL);
2105 /* Build a BINFO with LEN language slots. */
2107 tree
2108 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
2110 tree t;
2111 size_t length = (offsetof (struct tree_binfo, base_binfos)
2112 + vec<tree, va_gc>::embedded_size (base_binfos));
2114 record_node_allocation_statistics (TREE_BINFO, length);
2116 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
2118 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
2120 TREE_SET_CODE (t, TREE_BINFO);
2122 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
2124 return t;
2127 /* Create a CASE_LABEL_EXPR tree node and return it. */
2129 tree
2130 build_case_label (tree low_value, tree high_value, tree label_decl)
2132 tree t = make_node (CASE_LABEL_EXPR);
2134 TREE_TYPE (t) = void_type_node;
2135 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
2137 CASE_LOW (t) = low_value;
2138 CASE_HIGH (t) = high_value;
2139 CASE_LABEL (t) = label_decl;
2140 CASE_CHAIN (t) = NULL_TREE;
2142 return t;
2145 /* Build a newly constructed INTEGER_CST node. LEN and EXT_LEN are the
2146 values of TREE_INT_CST_NUNITS and TREE_INT_CST_EXT_NUNITS respectively.
2147 The latter determines the length of the HOST_WIDE_INT vector. */
2149 tree
2150 make_int_cst_stat (int len, int ext_len MEM_STAT_DECL)
2152 tree t;
2153 int length = ((ext_len - 1) * sizeof (HOST_WIDE_INT)
2154 + sizeof (struct tree_int_cst));
2156 gcc_assert (len);
2157 record_node_allocation_statistics (INTEGER_CST, length);
2159 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2161 TREE_SET_CODE (t, INTEGER_CST);
2162 TREE_INT_CST_NUNITS (t) = len;
2163 TREE_INT_CST_EXT_NUNITS (t) = ext_len;
2164 /* to_offset can only be applied to trees that are offset_int-sized
2165 or smaller. EXT_LEN is correct if it fits, otherwise the constant
2166 must be exactly the precision of offset_int and so LEN is correct. */
2167 if (ext_len <= OFFSET_INT_ELTS)
2168 TREE_INT_CST_OFFSET_NUNITS (t) = ext_len;
2169 else
2170 TREE_INT_CST_OFFSET_NUNITS (t) = len;
2172 TREE_CONSTANT (t) = 1;
2174 return t;
2177 /* Build a newly constructed TREE_VEC node of length LEN. */
2179 tree
2180 make_tree_vec_stat (int len MEM_STAT_DECL)
2182 tree t;
2183 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2185 record_node_allocation_statistics (TREE_VEC, length);
2187 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2189 TREE_SET_CODE (t, TREE_VEC);
2190 TREE_VEC_LENGTH (t) = len;
2192 return t;
2195 /* Grow a TREE_VEC node to new length LEN. */
2197 tree
2198 grow_tree_vec_stat (tree v, int len MEM_STAT_DECL)
2200 gcc_assert (TREE_CODE (v) == TREE_VEC);
2202 int oldlen = TREE_VEC_LENGTH (v);
2203 gcc_assert (len > oldlen);
2205 int oldlength = (oldlen - 1) * sizeof (tree) + sizeof (struct tree_vec);
2206 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2208 record_node_allocation_statistics (TREE_VEC, length - oldlength);
2210 v = (tree) ggc_realloc (v, length PASS_MEM_STAT);
2212 TREE_VEC_LENGTH (v) = len;
2214 return v;
2217 /* Return 1 if EXPR is the integer constant zero or a complex constant
2218 of zero. */
2221 integer_zerop (const_tree expr)
2223 STRIP_NOPS (expr);
2225 switch (TREE_CODE (expr))
2227 case INTEGER_CST:
2228 return wi::eq_p (expr, 0);
2229 case COMPLEX_CST:
2230 return (integer_zerop (TREE_REALPART (expr))
2231 && integer_zerop (TREE_IMAGPART (expr)));
2232 case VECTOR_CST:
2234 unsigned i;
2235 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2236 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
2237 return false;
2238 return true;
2240 default:
2241 return false;
2245 /* Return 1 if EXPR is the integer constant one or the corresponding
2246 complex constant. */
2249 integer_onep (const_tree expr)
2251 STRIP_NOPS (expr);
2253 switch (TREE_CODE (expr))
2255 case INTEGER_CST:
2256 return wi::eq_p (wi::to_widest (expr), 1);
2257 case COMPLEX_CST:
2258 return (integer_onep (TREE_REALPART (expr))
2259 && integer_zerop (TREE_IMAGPART (expr)));
2260 case VECTOR_CST:
2262 unsigned i;
2263 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2264 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
2265 return false;
2266 return true;
2268 default:
2269 return false;
2273 /* Return 1 if EXPR is the integer constant one. For complex and vector,
2274 return 1 if every piece is the integer constant one. */
2277 integer_each_onep (const_tree expr)
2279 STRIP_NOPS (expr);
2281 if (TREE_CODE (expr) == COMPLEX_CST)
2282 return (integer_onep (TREE_REALPART (expr))
2283 && integer_onep (TREE_IMAGPART (expr)));
2284 else
2285 return integer_onep (expr);
2288 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
2289 it contains, or a complex or vector whose subparts are such integers. */
2292 integer_all_onesp (const_tree expr)
2294 STRIP_NOPS (expr);
2296 if (TREE_CODE (expr) == COMPLEX_CST
2297 && integer_all_onesp (TREE_REALPART (expr))
2298 && integer_all_onesp (TREE_IMAGPART (expr)))
2299 return 1;
2301 else if (TREE_CODE (expr) == VECTOR_CST)
2303 unsigned i;
2304 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2305 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
2306 return 0;
2307 return 1;
2310 else if (TREE_CODE (expr) != INTEGER_CST)
2311 return 0;
2313 return wi::max_value (TYPE_PRECISION (TREE_TYPE (expr)), UNSIGNED) == expr;
2316 /* Return 1 if EXPR is the integer constant minus one. */
2319 integer_minus_onep (const_tree expr)
2321 STRIP_NOPS (expr);
2323 if (TREE_CODE (expr) == COMPLEX_CST)
2324 return (integer_all_onesp (TREE_REALPART (expr))
2325 && integer_zerop (TREE_IMAGPART (expr)));
2326 else
2327 return integer_all_onesp (expr);
2330 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
2331 one bit on). */
2334 integer_pow2p (const_tree expr)
2336 STRIP_NOPS (expr);
2338 if (TREE_CODE (expr) == COMPLEX_CST
2339 && integer_pow2p (TREE_REALPART (expr))
2340 && integer_zerop (TREE_IMAGPART (expr)))
2341 return 1;
2343 if (TREE_CODE (expr) != INTEGER_CST)
2344 return 0;
2346 return wi::popcount (expr) == 1;
2349 /* Return 1 if EXPR is an integer constant other than zero or a
2350 complex constant other than zero. */
2353 integer_nonzerop (const_tree expr)
2355 STRIP_NOPS (expr);
2357 return ((TREE_CODE (expr) == INTEGER_CST
2358 && !wi::eq_p (expr, 0))
2359 || (TREE_CODE (expr) == COMPLEX_CST
2360 && (integer_nonzerop (TREE_REALPART (expr))
2361 || integer_nonzerop (TREE_IMAGPART (expr)))));
2364 /* Return 1 if EXPR is the integer constant one. For vector,
2365 return 1 if every piece is the integer constant minus one
2366 (representing the value TRUE). */
2369 integer_truep (const_tree expr)
2371 STRIP_NOPS (expr);
2373 if (TREE_CODE (expr) == VECTOR_CST)
2374 return integer_all_onesp (expr);
2375 return integer_onep (expr);
2378 /* Return 1 if EXPR is the fixed-point constant zero. */
2381 fixed_zerop (const_tree expr)
2383 return (TREE_CODE (expr) == FIXED_CST
2384 && TREE_FIXED_CST (expr).data.is_zero ());
2387 /* Return the power of two represented by a tree node known to be a
2388 power of two. */
2391 tree_log2 (const_tree expr)
2393 STRIP_NOPS (expr);
2395 if (TREE_CODE (expr) == COMPLEX_CST)
2396 return tree_log2 (TREE_REALPART (expr));
2398 return wi::exact_log2 (expr);
2401 /* Similar, but return the largest integer Y such that 2 ** Y is less
2402 than or equal to EXPR. */
2405 tree_floor_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::floor_log2 (expr);
2415 /* Return number of known trailing zero bits in EXPR, or, if the value of
2416 EXPR is known to be zero, the precision of it's type. */
2418 unsigned int
2419 tree_ctz (const_tree expr)
2421 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
2422 && !POINTER_TYPE_P (TREE_TYPE (expr)))
2423 return 0;
2425 unsigned int ret1, ret2, prec = TYPE_PRECISION (TREE_TYPE (expr));
2426 switch (TREE_CODE (expr))
2428 case INTEGER_CST:
2429 ret1 = wi::ctz (expr);
2430 return MIN (ret1, prec);
2431 case SSA_NAME:
2432 ret1 = wi::ctz (get_nonzero_bits (expr));
2433 return MIN (ret1, prec);
2434 case PLUS_EXPR:
2435 case MINUS_EXPR:
2436 case BIT_IOR_EXPR:
2437 case BIT_XOR_EXPR:
2438 case MIN_EXPR:
2439 case MAX_EXPR:
2440 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2441 if (ret1 == 0)
2442 return ret1;
2443 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2444 return MIN (ret1, ret2);
2445 case POINTER_PLUS_EXPR:
2446 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2447 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2448 /* Second operand is sizetype, which could be in theory
2449 wider than pointer's precision. Make sure we never
2450 return more than prec. */
2451 ret2 = MIN (ret2, prec);
2452 return MIN (ret1, ret2);
2453 case BIT_AND_EXPR:
2454 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2455 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2456 return MAX (ret1, ret2);
2457 case MULT_EXPR:
2458 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2459 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2460 return MIN (ret1 + ret2, prec);
2461 case LSHIFT_EXPR:
2462 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2463 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2464 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2466 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2467 return MIN (ret1 + ret2, prec);
2469 return ret1;
2470 case RSHIFT_EXPR:
2471 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2472 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2474 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2475 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2476 if (ret1 > ret2)
2477 return ret1 - ret2;
2479 return 0;
2480 case TRUNC_DIV_EXPR:
2481 case CEIL_DIV_EXPR:
2482 case FLOOR_DIV_EXPR:
2483 case ROUND_DIV_EXPR:
2484 case EXACT_DIV_EXPR:
2485 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
2486 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) == 1)
2488 int l = tree_log2 (TREE_OPERAND (expr, 1));
2489 if (l >= 0)
2491 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2492 ret2 = l;
2493 if (ret1 > ret2)
2494 return ret1 - ret2;
2497 return 0;
2498 CASE_CONVERT:
2499 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2500 if (ret1 && ret1 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2501 ret1 = prec;
2502 return MIN (ret1, prec);
2503 case SAVE_EXPR:
2504 return tree_ctz (TREE_OPERAND (expr, 0));
2505 case COND_EXPR:
2506 ret1 = tree_ctz (TREE_OPERAND (expr, 1));
2507 if (ret1 == 0)
2508 return 0;
2509 ret2 = tree_ctz (TREE_OPERAND (expr, 2));
2510 return MIN (ret1, ret2);
2511 case COMPOUND_EXPR:
2512 return tree_ctz (TREE_OPERAND (expr, 1));
2513 case ADDR_EXPR:
2514 ret1 = get_pointer_alignment (CONST_CAST_TREE (expr));
2515 if (ret1 > BITS_PER_UNIT)
2517 ret1 = ctz_hwi (ret1 / BITS_PER_UNIT);
2518 return MIN (ret1, prec);
2520 return 0;
2521 default:
2522 return 0;
2526 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
2527 decimal float constants, so don't return 1 for them. */
2530 real_zerop (const_tree expr)
2532 STRIP_NOPS (expr);
2534 switch (TREE_CODE (expr))
2536 case REAL_CST:
2537 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
2538 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2539 case COMPLEX_CST:
2540 return real_zerop (TREE_REALPART (expr))
2541 && real_zerop (TREE_IMAGPART (expr));
2542 case VECTOR_CST:
2544 unsigned i;
2545 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2546 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2547 return false;
2548 return true;
2550 default:
2551 return false;
2555 /* Return 1 if EXPR is the real constant one in real or complex form.
2556 Trailing zeroes matter for decimal float constants, so don't return
2557 1 for them. */
2560 real_onep (const_tree expr)
2562 STRIP_NOPS (expr);
2564 switch (TREE_CODE (expr))
2566 case REAL_CST:
2567 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
2568 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2569 case COMPLEX_CST:
2570 return real_onep (TREE_REALPART (expr))
2571 && real_zerop (TREE_IMAGPART (expr));
2572 case VECTOR_CST:
2574 unsigned i;
2575 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2576 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2577 return false;
2578 return true;
2580 default:
2581 return false;
2585 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2586 matter for decimal float constants, so don't return 1 for them. */
2589 real_minus_onep (const_tree expr)
2591 STRIP_NOPS (expr);
2593 switch (TREE_CODE (expr))
2595 case REAL_CST:
2596 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2597 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2598 case COMPLEX_CST:
2599 return real_minus_onep (TREE_REALPART (expr))
2600 && real_zerop (TREE_IMAGPART (expr));
2601 case VECTOR_CST:
2603 unsigned i;
2604 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2605 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2606 return false;
2607 return true;
2609 default:
2610 return false;
2614 /* Nonzero if EXP is a constant or a cast of a constant. */
2617 really_constant_p (const_tree exp)
2619 /* This is not quite the same as STRIP_NOPS. It does more. */
2620 while (CONVERT_EXPR_P (exp)
2621 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2622 exp = TREE_OPERAND (exp, 0);
2623 return TREE_CONSTANT (exp);
2626 /* Return first list element whose TREE_VALUE is ELEM.
2627 Return 0 if ELEM is not in LIST. */
2629 tree
2630 value_member (tree elem, tree list)
2632 while (list)
2634 if (elem == TREE_VALUE (list))
2635 return list;
2636 list = TREE_CHAIN (list);
2638 return NULL_TREE;
2641 /* Return first list element whose TREE_PURPOSE is ELEM.
2642 Return 0 if ELEM is not in LIST. */
2644 tree
2645 purpose_member (const_tree elem, tree list)
2647 while (list)
2649 if (elem == TREE_PURPOSE (list))
2650 return list;
2651 list = TREE_CHAIN (list);
2653 return NULL_TREE;
2656 /* Return true if ELEM is in V. */
2658 bool
2659 vec_member (const_tree elem, vec<tree, va_gc> *v)
2661 unsigned ix;
2662 tree t;
2663 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2664 if (elem == t)
2665 return true;
2666 return false;
2669 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2670 NULL_TREE. */
2672 tree
2673 chain_index (int idx, tree chain)
2675 for (; chain && idx > 0; --idx)
2676 chain = TREE_CHAIN (chain);
2677 return chain;
2680 /* Return nonzero if ELEM is part of the chain CHAIN. */
2683 chain_member (const_tree elem, const_tree chain)
2685 while (chain)
2687 if (elem == chain)
2688 return 1;
2689 chain = DECL_CHAIN (chain);
2692 return 0;
2695 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2696 We expect a null pointer to mark the end of the chain.
2697 This is the Lisp primitive `length'. */
2700 list_length (const_tree t)
2702 const_tree p = t;
2703 #ifdef ENABLE_TREE_CHECKING
2704 const_tree q = t;
2705 #endif
2706 int len = 0;
2708 while (p)
2710 p = TREE_CHAIN (p);
2711 #ifdef ENABLE_TREE_CHECKING
2712 if (len % 2)
2713 q = TREE_CHAIN (q);
2714 gcc_assert (p != q);
2715 #endif
2716 len++;
2719 return len;
2722 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2723 UNION_TYPE TYPE, or NULL_TREE if none. */
2725 tree
2726 first_field (const_tree type)
2728 tree t = TYPE_FIELDS (type);
2729 while (t && TREE_CODE (t) != FIELD_DECL)
2730 t = TREE_CHAIN (t);
2731 return t;
2734 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2735 by modifying the last node in chain 1 to point to chain 2.
2736 This is the Lisp primitive `nconc'. */
2738 tree
2739 chainon (tree op1, tree op2)
2741 tree t1;
2743 if (!op1)
2744 return op2;
2745 if (!op2)
2746 return op1;
2748 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2749 continue;
2750 TREE_CHAIN (t1) = op2;
2752 #ifdef ENABLE_TREE_CHECKING
2754 tree t2;
2755 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2756 gcc_assert (t2 != t1);
2758 #endif
2760 return op1;
2763 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2765 tree
2766 tree_last (tree chain)
2768 tree next;
2769 if (chain)
2770 while ((next = TREE_CHAIN (chain)))
2771 chain = next;
2772 return chain;
2775 /* Reverse the order of elements in the chain T,
2776 and return the new head of the chain (old last element). */
2778 tree
2779 nreverse (tree t)
2781 tree prev = 0, decl, next;
2782 for (decl = t; decl; decl = next)
2784 /* We shouldn't be using this function to reverse BLOCK chains; we
2785 have blocks_nreverse for that. */
2786 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2787 next = TREE_CHAIN (decl);
2788 TREE_CHAIN (decl) = prev;
2789 prev = decl;
2791 return prev;
2794 /* Return a newly created TREE_LIST node whose
2795 purpose and value fields are PARM and VALUE. */
2797 tree
2798 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2800 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2801 TREE_PURPOSE (t) = parm;
2802 TREE_VALUE (t) = value;
2803 return t;
2806 /* Build a chain of TREE_LIST nodes from a vector. */
2808 tree
2809 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2811 tree ret = NULL_TREE;
2812 tree *pp = &ret;
2813 unsigned int i;
2814 tree t;
2815 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2817 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2818 pp = &TREE_CHAIN (*pp);
2820 return ret;
2823 /* Return a newly created TREE_LIST node whose
2824 purpose and value fields are PURPOSE and VALUE
2825 and whose TREE_CHAIN is CHAIN. */
2827 tree
2828 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2830 tree node;
2832 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
2833 memset (node, 0, sizeof (struct tree_common));
2835 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2837 TREE_SET_CODE (node, TREE_LIST);
2838 TREE_CHAIN (node) = chain;
2839 TREE_PURPOSE (node) = purpose;
2840 TREE_VALUE (node) = value;
2841 return node;
2844 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2845 trees. */
2847 vec<tree, va_gc> *
2848 ctor_to_vec (tree ctor)
2850 vec<tree, va_gc> *vec;
2851 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2852 unsigned int ix;
2853 tree val;
2855 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2856 vec->quick_push (val);
2858 return vec;
2861 /* Return the size nominally occupied by an object of type TYPE
2862 when it resides in memory. The value is measured in units of bytes,
2863 and its data type is that normally used for type sizes
2864 (which is the first type created by make_signed_type or
2865 make_unsigned_type). */
2867 tree
2868 size_in_bytes (const_tree type)
2870 tree t;
2872 if (type == error_mark_node)
2873 return integer_zero_node;
2875 type = TYPE_MAIN_VARIANT (type);
2876 t = TYPE_SIZE_UNIT (type);
2878 if (t == 0)
2880 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2881 return size_zero_node;
2884 return t;
2887 /* Return the size of TYPE (in bytes) as a wide integer
2888 or return -1 if the size can vary or is larger than an integer. */
2890 HOST_WIDE_INT
2891 int_size_in_bytes (const_tree type)
2893 tree t;
2895 if (type == error_mark_node)
2896 return 0;
2898 type = TYPE_MAIN_VARIANT (type);
2899 t = TYPE_SIZE_UNIT (type);
2901 if (t && tree_fits_uhwi_p (t))
2902 return TREE_INT_CST_LOW (t);
2903 else
2904 return -1;
2907 /* Return the maximum size of TYPE (in bytes) as a wide integer
2908 or return -1 if the size can vary or is larger than an integer. */
2910 HOST_WIDE_INT
2911 max_int_size_in_bytes (const_tree type)
2913 HOST_WIDE_INT size = -1;
2914 tree size_tree;
2916 /* If this is an array type, check for a possible MAX_SIZE attached. */
2918 if (TREE_CODE (type) == ARRAY_TYPE)
2920 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2922 if (size_tree && tree_fits_uhwi_p (size_tree))
2923 size = tree_to_uhwi (size_tree);
2926 /* If we still haven't been able to get a size, see if the language
2927 can compute a maximum size. */
2929 if (size == -1)
2931 size_tree = lang_hooks.types.max_size (type);
2933 if (size_tree && tree_fits_uhwi_p (size_tree))
2934 size = tree_to_uhwi (size_tree);
2937 return size;
2940 /* Return the bit position of FIELD, in bits from the start of the record.
2941 This is a tree of type bitsizetype. */
2943 tree
2944 bit_position (const_tree field)
2946 return bit_from_pos (DECL_FIELD_OFFSET (field),
2947 DECL_FIELD_BIT_OFFSET (field));
2950 /* Return the byte position of FIELD, in bytes from the start of the record.
2951 This is a tree of type sizetype. */
2953 tree
2954 byte_position (const_tree field)
2956 return byte_from_pos (DECL_FIELD_OFFSET (field),
2957 DECL_FIELD_BIT_OFFSET (field));
2960 /* Likewise, but return as an integer. It must be representable in
2961 that way (since it could be a signed value, we don't have the
2962 option of returning -1 like int_size_in_byte can. */
2964 HOST_WIDE_INT
2965 int_byte_position (const_tree field)
2967 return tree_to_shwi (byte_position (field));
2970 /* Return the strictest alignment, in bits, that T is known to have. */
2972 unsigned int
2973 expr_align (const_tree t)
2975 unsigned int align0, align1;
2977 switch (TREE_CODE (t))
2979 CASE_CONVERT: case NON_LVALUE_EXPR:
2980 /* If we have conversions, we know that the alignment of the
2981 object must meet each of the alignments of the types. */
2982 align0 = expr_align (TREE_OPERAND (t, 0));
2983 align1 = TYPE_ALIGN (TREE_TYPE (t));
2984 return MAX (align0, align1);
2986 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2987 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2988 case CLEANUP_POINT_EXPR:
2989 /* These don't change the alignment of an object. */
2990 return expr_align (TREE_OPERAND (t, 0));
2992 case COND_EXPR:
2993 /* The best we can do is say that the alignment is the least aligned
2994 of the two arms. */
2995 align0 = expr_align (TREE_OPERAND (t, 1));
2996 align1 = expr_align (TREE_OPERAND (t, 2));
2997 return MIN (align0, align1);
2999 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
3000 meaningfully, it's always 1. */
3001 case LABEL_DECL: case CONST_DECL:
3002 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
3003 case FUNCTION_DECL:
3004 gcc_assert (DECL_ALIGN (t) != 0);
3005 return DECL_ALIGN (t);
3007 default:
3008 break;
3011 /* Otherwise take the alignment from that of the type. */
3012 return TYPE_ALIGN (TREE_TYPE (t));
3015 /* Return, as a tree node, the number of elements for TYPE (which is an
3016 ARRAY_TYPE) minus one. This counts only elements of the top array. */
3018 tree
3019 array_type_nelts (const_tree type)
3021 tree index_type, min, max;
3023 /* If they did it with unspecified bounds, then we should have already
3024 given an error about it before we got here. */
3025 if (! TYPE_DOMAIN (type))
3026 return error_mark_node;
3028 index_type = TYPE_DOMAIN (type);
3029 min = TYPE_MIN_VALUE (index_type);
3030 max = TYPE_MAX_VALUE (index_type);
3032 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
3033 if (!max)
3034 return error_mark_node;
3036 return (integer_zerop (min)
3037 ? max
3038 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
3041 /* If arg is static -- a reference to an object in static storage -- then
3042 return the object. This is not the same as the C meaning of `static'.
3043 If arg isn't static, return NULL. */
3045 tree
3046 staticp (tree arg)
3048 switch (TREE_CODE (arg))
3050 case FUNCTION_DECL:
3051 /* Nested functions are static, even though taking their address will
3052 involve a trampoline as we unnest the nested function and create
3053 the trampoline on the tree level. */
3054 return arg;
3056 case VAR_DECL:
3057 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3058 && ! DECL_THREAD_LOCAL_P (arg)
3059 && ! DECL_DLLIMPORT_P (arg)
3060 ? arg : NULL);
3062 case CONST_DECL:
3063 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3064 ? arg : NULL);
3066 case CONSTRUCTOR:
3067 return TREE_STATIC (arg) ? arg : NULL;
3069 case LABEL_DECL:
3070 case STRING_CST:
3071 return arg;
3073 case COMPONENT_REF:
3074 /* If the thing being referenced is not a field, then it is
3075 something language specific. */
3076 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
3078 /* If we are referencing a bitfield, we can't evaluate an
3079 ADDR_EXPR at compile time and so it isn't a constant. */
3080 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
3081 return NULL;
3083 return staticp (TREE_OPERAND (arg, 0));
3085 case BIT_FIELD_REF:
3086 return NULL;
3088 case INDIRECT_REF:
3089 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
3091 case ARRAY_REF:
3092 case ARRAY_RANGE_REF:
3093 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
3094 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
3095 return staticp (TREE_OPERAND (arg, 0));
3096 else
3097 return NULL;
3099 case COMPOUND_LITERAL_EXPR:
3100 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
3102 default:
3103 return NULL;
3110 /* Return whether OP is a DECL whose address is function-invariant. */
3112 bool
3113 decl_address_invariant_p (const_tree op)
3115 /* The conditions below are slightly less strict than the one in
3116 staticp. */
3118 switch (TREE_CODE (op))
3120 case PARM_DECL:
3121 case RESULT_DECL:
3122 case LABEL_DECL:
3123 case FUNCTION_DECL:
3124 return true;
3126 case VAR_DECL:
3127 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3128 || DECL_THREAD_LOCAL_P (op)
3129 || DECL_CONTEXT (op) == current_function_decl
3130 || decl_function_context (op) == current_function_decl)
3131 return true;
3132 break;
3134 case CONST_DECL:
3135 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3136 || decl_function_context (op) == current_function_decl)
3137 return true;
3138 break;
3140 default:
3141 break;
3144 return false;
3147 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
3149 bool
3150 decl_address_ip_invariant_p (const_tree op)
3152 /* The conditions below are slightly less strict than the one in
3153 staticp. */
3155 switch (TREE_CODE (op))
3157 case LABEL_DECL:
3158 case FUNCTION_DECL:
3159 case STRING_CST:
3160 return true;
3162 case VAR_DECL:
3163 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
3164 && !DECL_DLLIMPORT_P (op))
3165 || DECL_THREAD_LOCAL_P (op))
3166 return true;
3167 break;
3169 case CONST_DECL:
3170 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
3171 return true;
3172 break;
3174 default:
3175 break;
3178 return false;
3182 /* Return true if T is function-invariant (internal function, does
3183 not handle arithmetic; that's handled in skip_simple_arithmetic and
3184 tree_invariant_p). */
3186 static bool tree_invariant_p (tree t);
3188 static bool
3189 tree_invariant_p_1 (tree t)
3191 tree op;
3193 if (TREE_CONSTANT (t)
3194 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
3195 return true;
3197 switch (TREE_CODE (t))
3199 case SAVE_EXPR:
3200 return true;
3202 case ADDR_EXPR:
3203 op = TREE_OPERAND (t, 0);
3204 while (handled_component_p (op))
3206 switch (TREE_CODE (op))
3208 case ARRAY_REF:
3209 case ARRAY_RANGE_REF:
3210 if (!tree_invariant_p (TREE_OPERAND (op, 1))
3211 || TREE_OPERAND (op, 2) != NULL_TREE
3212 || TREE_OPERAND (op, 3) != NULL_TREE)
3213 return false;
3214 break;
3216 case COMPONENT_REF:
3217 if (TREE_OPERAND (op, 2) != NULL_TREE)
3218 return false;
3219 break;
3221 default:;
3223 op = TREE_OPERAND (op, 0);
3226 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
3228 default:
3229 break;
3232 return false;
3235 /* Return true if T is function-invariant. */
3237 static bool
3238 tree_invariant_p (tree t)
3240 tree inner = skip_simple_arithmetic (t);
3241 return tree_invariant_p_1 (inner);
3244 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
3245 Do this to any expression which may be used in more than one place,
3246 but must be evaluated only once.
3248 Normally, expand_expr would reevaluate the expression each time.
3249 Calling save_expr produces something that is evaluated and recorded
3250 the first time expand_expr is called on it. Subsequent calls to
3251 expand_expr just reuse the recorded value.
3253 The call to expand_expr that generates code that actually computes
3254 the value is the first call *at compile time*. Subsequent calls
3255 *at compile time* generate code to use the saved value.
3256 This produces correct result provided that *at run time* control
3257 always flows through the insns made by the first expand_expr
3258 before reaching the other places where the save_expr was evaluated.
3259 You, the caller of save_expr, must make sure this is so.
3261 Constants, and certain read-only nodes, are returned with no
3262 SAVE_EXPR because that is safe. Expressions containing placeholders
3263 are not touched; see tree.def for an explanation of what these
3264 are used for. */
3266 tree
3267 save_expr (tree expr)
3269 tree t = fold (expr);
3270 tree inner;
3272 /* If the tree evaluates to a constant, then we don't want to hide that
3273 fact (i.e. this allows further folding, and direct checks for constants).
3274 However, a read-only object that has side effects cannot be bypassed.
3275 Since it is no problem to reevaluate literals, we just return the
3276 literal node. */
3277 inner = skip_simple_arithmetic (t);
3278 if (TREE_CODE (inner) == ERROR_MARK)
3279 return inner;
3281 if (tree_invariant_p_1 (inner))
3282 return t;
3284 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
3285 it means that the size or offset of some field of an object depends on
3286 the value within another field.
3288 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
3289 and some variable since it would then need to be both evaluated once and
3290 evaluated more than once. Front-ends must assure this case cannot
3291 happen by surrounding any such subexpressions in their own SAVE_EXPR
3292 and forcing evaluation at the proper time. */
3293 if (contains_placeholder_p (inner))
3294 return t;
3296 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
3297 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
3299 /* This expression might be placed ahead of a jump to ensure that the
3300 value was computed on both sides of the jump. So make sure it isn't
3301 eliminated as dead. */
3302 TREE_SIDE_EFFECTS (t) = 1;
3303 return t;
3306 /* Look inside EXPR into any simple arithmetic operations. Return the
3307 outermost non-arithmetic or non-invariant node. */
3309 tree
3310 skip_simple_arithmetic (tree expr)
3312 /* We don't care about whether this can be used as an lvalue in this
3313 context. */
3314 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3315 expr = TREE_OPERAND (expr, 0);
3317 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
3318 a constant, it will be more efficient to not make another SAVE_EXPR since
3319 it will allow better simplification and GCSE will be able to merge the
3320 computations if they actually occur. */
3321 while (true)
3323 if (UNARY_CLASS_P (expr))
3324 expr = TREE_OPERAND (expr, 0);
3325 else if (BINARY_CLASS_P (expr))
3327 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
3328 expr = TREE_OPERAND (expr, 0);
3329 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
3330 expr = TREE_OPERAND (expr, 1);
3331 else
3332 break;
3334 else
3335 break;
3338 return expr;
3341 /* Look inside EXPR into simple arithmetic operations involving constants.
3342 Return the outermost non-arithmetic or non-constant node. */
3344 tree
3345 skip_simple_constant_arithmetic (tree expr)
3347 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3348 expr = TREE_OPERAND (expr, 0);
3350 while (true)
3352 if (UNARY_CLASS_P (expr))
3353 expr = TREE_OPERAND (expr, 0);
3354 else if (BINARY_CLASS_P (expr))
3356 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3357 expr = TREE_OPERAND (expr, 0);
3358 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3359 expr = TREE_OPERAND (expr, 1);
3360 else
3361 break;
3363 else
3364 break;
3367 return expr;
3370 /* Return which tree structure is used by T. */
3372 enum tree_node_structure_enum
3373 tree_node_structure (const_tree t)
3375 const enum tree_code code = TREE_CODE (t);
3376 return tree_node_structure_for_code (code);
3379 /* Set various status flags when building a CALL_EXPR object T. */
3381 static void
3382 process_call_operands (tree t)
3384 bool side_effects = TREE_SIDE_EFFECTS (t);
3385 bool read_only = false;
3386 int i = call_expr_flags (t);
3388 /* Calls have side-effects, except those to const or pure functions. */
3389 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
3390 side_effects = true;
3391 /* Propagate TREE_READONLY of arguments for const functions. */
3392 if (i & ECF_CONST)
3393 read_only = true;
3395 if (!side_effects || read_only)
3396 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
3398 tree op = TREE_OPERAND (t, i);
3399 if (op && TREE_SIDE_EFFECTS (op))
3400 side_effects = true;
3401 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
3402 read_only = false;
3405 TREE_SIDE_EFFECTS (t) = side_effects;
3406 TREE_READONLY (t) = read_only;
3409 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
3410 size or offset that depends on a field within a record. */
3412 bool
3413 contains_placeholder_p (const_tree exp)
3415 enum tree_code code;
3417 if (!exp)
3418 return 0;
3420 code = TREE_CODE (exp);
3421 if (code == PLACEHOLDER_EXPR)
3422 return 1;
3424 switch (TREE_CODE_CLASS (code))
3426 case tcc_reference:
3427 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
3428 position computations since they will be converted into a
3429 WITH_RECORD_EXPR involving the reference, which will assume
3430 here will be valid. */
3431 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3433 case tcc_exceptional:
3434 if (code == TREE_LIST)
3435 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
3436 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
3437 break;
3439 case tcc_unary:
3440 case tcc_binary:
3441 case tcc_comparison:
3442 case tcc_expression:
3443 switch (code)
3445 case COMPOUND_EXPR:
3446 /* Ignoring the first operand isn't quite right, but works best. */
3447 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
3449 case COND_EXPR:
3450 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3451 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
3452 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
3454 case SAVE_EXPR:
3455 /* The save_expr function never wraps anything containing
3456 a PLACEHOLDER_EXPR. */
3457 return 0;
3459 default:
3460 break;
3463 switch (TREE_CODE_LENGTH (code))
3465 case 1:
3466 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3467 case 2:
3468 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3469 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
3470 default:
3471 return 0;
3474 case tcc_vl_exp:
3475 switch (code)
3477 case CALL_EXPR:
3479 const_tree arg;
3480 const_call_expr_arg_iterator iter;
3481 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
3482 if (CONTAINS_PLACEHOLDER_P (arg))
3483 return 1;
3484 return 0;
3486 default:
3487 return 0;
3490 default:
3491 return 0;
3493 return 0;
3496 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3497 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3498 field positions. */
3500 static bool
3501 type_contains_placeholder_1 (const_tree type)
3503 /* If the size contains a placeholder or the parent type (component type in
3504 the case of arrays) type involves a placeholder, this type does. */
3505 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3506 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3507 || (!POINTER_TYPE_P (type)
3508 && TREE_TYPE (type)
3509 && type_contains_placeholder_p (TREE_TYPE (type))))
3510 return true;
3512 /* Now do type-specific checks. Note that the last part of the check above
3513 greatly limits what we have to do below. */
3514 switch (TREE_CODE (type))
3516 case VOID_TYPE:
3517 case POINTER_BOUNDS_TYPE:
3518 case COMPLEX_TYPE:
3519 case ENUMERAL_TYPE:
3520 case BOOLEAN_TYPE:
3521 case POINTER_TYPE:
3522 case OFFSET_TYPE:
3523 case REFERENCE_TYPE:
3524 case METHOD_TYPE:
3525 case FUNCTION_TYPE:
3526 case VECTOR_TYPE:
3527 case NULLPTR_TYPE:
3528 return false;
3530 case INTEGER_TYPE:
3531 case REAL_TYPE:
3532 case FIXED_POINT_TYPE:
3533 /* Here we just check the bounds. */
3534 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3535 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3537 case ARRAY_TYPE:
3538 /* We have already checked the component type above, so just check the
3539 domain type. */
3540 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3542 case RECORD_TYPE:
3543 case UNION_TYPE:
3544 case QUAL_UNION_TYPE:
3546 tree field;
3548 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3549 if (TREE_CODE (field) == FIELD_DECL
3550 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3551 || (TREE_CODE (type) == QUAL_UNION_TYPE
3552 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3553 || type_contains_placeholder_p (TREE_TYPE (field))))
3554 return true;
3556 return false;
3559 default:
3560 gcc_unreachable ();
3564 /* Wrapper around above function used to cache its result. */
3566 bool
3567 type_contains_placeholder_p (tree type)
3569 bool result;
3571 /* If the contains_placeholder_bits field has been initialized,
3572 then we know the answer. */
3573 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3574 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3576 /* Indicate that we've seen this type node, and the answer is false.
3577 This is what we want to return if we run into recursion via fields. */
3578 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3580 /* Compute the real value. */
3581 result = type_contains_placeholder_1 (type);
3583 /* Store the real value. */
3584 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3586 return result;
3589 /* Push tree EXP onto vector QUEUE if it is not already present. */
3591 static void
3592 push_without_duplicates (tree exp, vec<tree> *queue)
3594 unsigned int i;
3595 tree iter;
3597 FOR_EACH_VEC_ELT (*queue, i, iter)
3598 if (simple_cst_equal (iter, exp) == 1)
3599 break;
3601 if (!iter)
3602 queue->safe_push (exp);
3605 /* Given a tree EXP, find all occurrences of references to fields
3606 in a PLACEHOLDER_EXPR and place them in vector REFS without
3607 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3608 we assume here that EXP contains only arithmetic expressions
3609 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3610 argument list. */
3612 void
3613 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3615 enum tree_code code = TREE_CODE (exp);
3616 tree inner;
3617 int i;
3619 /* We handle TREE_LIST and COMPONENT_REF separately. */
3620 if (code == TREE_LIST)
3622 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3623 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3625 else if (code == COMPONENT_REF)
3627 for (inner = TREE_OPERAND (exp, 0);
3628 REFERENCE_CLASS_P (inner);
3629 inner = TREE_OPERAND (inner, 0))
3632 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3633 push_without_duplicates (exp, refs);
3634 else
3635 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3637 else
3638 switch (TREE_CODE_CLASS (code))
3640 case tcc_constant:
3641 break;
3643 case tcc_declaration:
3644 /* Variables allocated to static storage can stay. */
3645 if (!TREE_STATIC (exp))
3646 push_without_duplicates (exp, refs);
3647 break;
3649 case tcc_expression:
3650 /* This is the pattern built in ada/make_aligning_type. */
3651 if (code == ADDR_EXPR
3652 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3654 push_without_duplicates (exp, refs);
3655 break;
3658 /* Fall through... */
3660 case tcc_exceptional:
3661 case tcc_unary:
3662 case tcc_binary:
3663 case tcc_comparison:
3664 case tcc_reference:
3665 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3666 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3667 break;
3669 case tcc_vl_exp:
3670 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3671 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3672 break;
3674 default:
3675 gcc_unreachable ();
3679 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3680 return a tree with all occurrences of references to F in a
3681 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3682 CONST_DECLs. Note that we assume here that EXP contains only
3683 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3684 occurring only in their argument list. */
3686 tree
3687 substitute_in_expr (tree exp, tree f, tree r)
3689 enum tree_code code = TREE_CODE (exp);
3690 tree op0, op1, op2, op3;
3691 tree new_tree;
3693 /* We handle TREE_LIST and COMPONENT_REF separately. */
3694 if (code == TREE_LIST)
3696 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3697 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3698 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3699 return exp;
3701 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3703 else if (code == COMPONENT_REF)
3705 tree inner;
3707 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3708 and it is the right field, replace it with R. */
3709 for (inner = TREE_OPERAND (exp, 0);
3710 REFERENCE_CLASS_P (inner);
3711 inner = TREE_OPERAND (inner, 0))
3714 /* The field. */
3715 op1 = TREE_OPERAND (exp, 1);
3717 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3718 return r;
3720 /* If this expression hasn't been completed let, leave it alone. */
3721 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3722 return exp;
3724 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3725 if (op0 == TREE_OPERAND (exp, 0))
3726 return exp;
3728 new_tree
3729 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3731 else
3732 switch (TREE_CODE_CLASS (code))
3734 case tcc_constant:
3735 return exp;
3737 case tcc_declaration:
3738 if (exp == f)
3739 return r;
3740 else
3741 return exp;
3743 case tcc_expression:
3744 if (exp == f)
3745 return r;
3747 /* Fall through... */
3749 case tcc_exceptional:
3750 case tcc_unary:
3751 case tcc_binary:
3752 case tcc_comparison:
3753 case tcc_reference:
3754 switch (TREE_CODE_LENGTH (code))
3756 case 0:
3757 return exp;
3759 case 1:
3760 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3761 if (op0 == TREE_OPERAND (exp, 0))
3762 return exp;
3764 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3765 break;
3767 case 2:
3768 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3769 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3771 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3772 return exp;
3774 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3775 break;
3777 case 3:
3778 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3779 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3780 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3782 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3783 && op2 == TREE_OPERAND (exp, 2))
3784 return exp;
3786 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3787 break;
3789 case 4:
3790 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3791 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3792 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3793 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3795 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3796 && op2 == TREE_OPERAND (exp, 2)
3797 && op3 == TREE_OPERAND (exp, 3))
3798 return exp;
3800 new_tree
3801 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3802 break;
3804 default:
3805 gcc_unreachable ();
3807 break;
3809 case tcc_vl_exp:
3811 int i;
3813 new_tree = NULL_TREE;
3815 /* If we are trying to replace F with a constant, inline back
3816 functions which do nothing else than computing a value from
3817 the arguments they are passed. This makes it possible to
3818 fold partially or entirely the replacement expression. */
3819 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3821 tree t = maybe_inline_call_in_expr (exp);
3822 if (t)
3823 return SUBSTITUTE_IN_EXPR (t, f, r);
3826 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3828 tree op = TREE_OPERAND (exp, i);
3829 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3830 if (new_op != op)
3832 if (!new_tree)
3833 new_tree = copy_node (exp);
3834 TREE_OPERAND (new_tree, i) = new_op;
3838 if (new_tree)
3840 new_tree = fold (new_tree);
3841 if (TREE_CODE (new_tree) == CALL_EXPR)
3842 process_call_operands (new_tree);
3844 else
3845 return exp;
3847 break;
3849 default:
3850 gcc_unreachable ();
3853 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3855 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3856 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3858 return new_tree;
3861 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3862 for it within OBJ, a tree that is an object or a chain of references. */
3864 tree
3865 substitute_placeholder_in_expr (tree exp, tree obj)
3867 enum tree_code code = TREE_CODE (exp);
3868 tree op0, op1, op2, op3;
3869 tree new_tree;
3871 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3872 in the chain of OBJ. */
3873 if (code == PLACEHOLDER_EXPR)
3875 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3876 tree elt;
3878 for (elt = obj; elt != 0;
3879 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3880 || TREE_CODE (elt) == COND_EXPR)
3881 ? TREE_OPERAND (elt, 1)
3882 : (REFERENCE_CLASS_P (elt)
3883 || UNARY_CLASS_P (elt)
3884 || BINARY_CLASS_P (elt)
3885 || VL_EXP_CLASS_P (elt)
3886 || EXPRESSION_CLASS_P (elt))
3887 ? TREE_OPERAND (elt, 0) : 0))
3888 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3889 return elt;
3891 for (elt = obj; elt != 0;
3892 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3893 || TREE_CODE (elt) == COND_EXPR)
3894 ? TREE_OPERAND (elt, 1)
3895 : (REFERENCE_CLASS_P (elt)
3896 || UNARY_CLASS_P (elt)
3897 || BINARY_CLASS_P (elt)
3898 || VL_EXP_CLASS_P (elt)
3899 || EXPRESSION_CLASS_P (elt))
3900 ? TREE_OPERAND (elt, 0) : 0))
3901 if (POINTER_TYPE_P (TREE_TYPE (elt))
3902 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3903 == need_type))
3904 return fold_build1 (INDIRECT_REF, need_type, elt);
3906 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3907 survives until RTL generation, there will be an error. */
3908 return exp;
3911 /* TREE_LIST is special because we need to look at TREE_VALUE
3912 and TREE_CHAIN, not TREE_OPERANDS. */
3913 else if (code == TREE_LIST)
3915 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3916 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3917 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3918 return exp;
3920 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3922 else
3923 switch (TREE_CODE_CLASS (code))
3925 case tcc_constant:
3926 case tcc_declaration:
3927 return exp;
3929 case tcc_exceptional:
3930 case tcc_unary:
3931 case tcc_binary:
3932 case tcc_comparison:
3933 case tcc_expression:
3934 case tcc_reference:
3935 case tcc_statement:
3936 switch (TREE_CODE_LENGTH (code))
3938 case 0:
3939 return exp;
3941 case 1:
3942 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3943 if (op0 == TREE_OPERAND (exp, 0))
3944 return exp;
3946 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3947 break;
3949 case 2:
3950 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3951 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3953 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3954 return exp;
3956 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3957 break;
3959 case 3:
3960 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3961 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3962 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3964 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3965 && op2 == TREE_OPERAND (exp, 2))
3966 return exp;
3968 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3969 break;
3971 case 4:
3972 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3973 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3974 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3975 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3977 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3978 && op2 == TREE_OPERAND (exp, 2)
3979 && op3 == TREE_OPERAND (exp, 3))
3980 return exp;
3982 new_tree
3983 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3984 break;
3986 default:
3987 gcc_unreachable ();
3989 break;
3991 case tcc_vl_exp:
3993 int i;
3995 new_tree = NULL_TREE;
3997 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3999 tree op = TREE_OPERAND (exp, i);
4000 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
4001 if (new_op != op)
4003 if (!new_tree)
4004 new_tree = copy_node (exp);
4005 TREE_OPERAND (new_tree, i) = new_op;
4009 if (new_tree)
4011 new_tree = fold (new_tree);
4012 if (TREE_CODE (new_tree) == CALL_EXPR)
4013 process_call_operands (new_tree);
4015 else
4016 return exp;
4018 break;
4020 default:
4021 gcc_unreachable ();
4024 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4026 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4027 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4029 return new_tree;
4033 /* Subroutine of stabilize_reference; this is called for subtrees of
4034 references. Any expression with side-effects must be put in a SAVE_EXPR
4035 to ensure that it is only evaluated once.
4037 We don't put SAVE_EXPR nodes around everything, because assigning very
4038 simple expressions to temporaries causes us to miss good opportunities
4039 for optimizations. Among other things, the opportunity to fold in the
4040 addition of a constant into an addressing mode often gets lost, e.g.
4041 "y[i+1] += x;". In general, we take the approach that we should not make
4042 an assignment unless we are forced into it - i.e., that any non-side effect
4043 operator should be allowed, and that cse should take care of coalescing
4044 multiple utterances of the same expression should that prove fruitful. */
4046 static tree
4047 stabilize_reference_1 (tree e)
4049 tree result;
4050 enum tree_code code = TREE_CODE (e);
4052 /* We cannot ignore const expressions because it might be a reference
4053 to a const array but whose index contains side-effects. But we can
4054 ignore things that are actual constant or that already have been
4055 handled by this function. */
4057 if (tree_invariant_p (e))
4058 return e;
4060 switch (TREE_CODE_CLASS (code))
4062 case tcc_exceptional:
4063 case tcc_type:
4064 case tcc_declaration:
4065 case tcc_comparison:
4066 case tcc_statement:
4067 case tcc_expression:
4068 case tcc_reference:
4069 case tcc_vl_exp:
4070 /* If the expression has side-effects, then encase it in a SAVE_EXPR
4071 so that it will only be evaluated once. */
4072 /* The reference (r) and comparison (<) classes could be handled as
4073 below, but it is generally faster to only evaluate them once. */
4074 if (TREE_SIDE_EFFECTS (e))
4075 return save_expr (e);
4076 return e;
4078 case tcc_constant:
4079 /* Constants need no processing. In fact, we should never reach
4080 here. */
4081 return e;
4083 case tcc_binary:
4084 /* Division is slow and tends to be compiled with jumps,
4085 especially the division by powers of 2 that is often
4086 found inside of an array reference. So do it just once. */
4087 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
4088 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
4089 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
4090 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
4091 return save_expr (e);
4092 /* Recursively stabilize each operand. */
4093 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
4094 stabilize_reference_1 (TREE_OPERAND (e, 1)));
4095 break;
4097 case tcc_unary:
4098 /* Recursively stabilize each operand. */
4099 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
4100 break;
4102 default:
4103 gcc_unreachable ();
4106 TREE_TYPE (result) = TREE_TYPE (e);
4107 TREE_READONLY (result) = TREE_READONLY (e);
4108 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
4109 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
4111 return result;
4114 /* Stabilize a reference so that we can use it any number of times
4115 without causing its operands to be evaluated more than once.
4116 Returns the stabilized reference. This works by means of save_expr,
4117 so see the caveats in the comments about save_expr.
4119 Also allows conversion expressions whose operands are references.
4120 Any other kind of expression is returned unchanged. */
4122 tree
4123 stabilize_reference (tree ref)
4125 tree result;
4126 enum tree_code code = TREE_CODE (ref);
4128 switch (code)
4130 case VAR_DECL:
4131 case PARM_DECL:
4132 case RESULT_DECL:
4133 /* No action is needed in this case. */
4134 return ref;
4136 CASE_CONVERT:
4137 case FLOAT_EXPR:
4138 case FIX_TRUNC_EXPR:
4139 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
4140 break;
4142 case INDIRECT_REF:
4143 result = build_nt (INDIRECT_REF,
4144 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
4145 break;
4147 case COMPONENT_REF:
4148 result = build_nt (COMPONENT_REF,
4149 stabilize_reference (TREE_OPERAND (ref, 0)),
4150 TREE_OPERAND (ref, 1), NULL_TREE);
4151 break;
4153 case BIT_FIELD_REF:
4154 result = build_nt (BIT_FIELD_REF,
4155 stabilize_reference (TREE_OPERAND (ref, 0)),
4156 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
4157 break;
4159 case ARRAY_REF:
4160 result = build_nt (ARRAY_REF,
4161 stabilize_reference (TREE_OPERAND (ref, 0)),
4162 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4163 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4164 break;
4166 case ARRAY_RANGE_REF:
4167 result = build_nt (ARRAY_RANGE_REF,
4168 stabilize_reference (TREE_OPERAND (ref, 0)),
4169 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4170 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4171 break;
4173 case COMPOUND_EXPR:
4174 /* We cannot wrap the first expression in a SAVE_EXPR, as then
4175 it wouldn't be ignored. This matters when dealing with
4176 volatiles. */
4177 return stabilize_reference_1 (ref);
4179 /* If arg isn't a kind of lvalue we recognize, make no change.
4180 Caller should recognize the error for an invalid lvalue. */
4181 default:
4182 return ref;
4184 case ERROR_MARK:
4185 return error_mark_node;
4188 TREE_TYPE (result) = TREE_TYPE (ref);
4189 TREE_READONLY (result) = TREE_READONLY (ref);
4190 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
4191 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
4193 return result;
4196 /* Low-level constructors for expressions. */
4198 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
4199 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
4201 void
4202 recompute_tree_invariant_for_addr_expr (tree t)
4204 tree node;
4205 bool tc = true, se = false;
4207 /* We started out assuming this address is both invariant and constant, but
4208 does not have side effects. Now go down any handled components and see if
4209 any of them involve offsets that are either non-constant or non-invariant.
4210 Also check for side-effects.
4212 ??? Note that this code makes no attempt to deal with the case where
4213 taking the address of something causes a copy due to misalignment. */
4215 #define UPDATE_FLAGS(NODE) \
4216 do { tree _node = (NODE); \
4217 if (_node && !TREE_CONSTANT (_node)) tc = false; \
4218 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
4220 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
4221 node = TREE_OPERAND (node, 0))
4223 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
4224 array reference (probably made temporarily by the G++ front end),
4225 so ignore all the operands. */
4226 if ((TREE_CODE (node) == ARRAY_REF
4227 || TREE_CODE (node) == ARRAY_RANGE_REF)
4228 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
4230 UPDATE_FLAGS (TREE_OPERAND (node, 1));
4231 if (TREE_OPERAND (node, 2))
4232 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4233 if (TREE_OPERAND (node, 3))
4234 UPDATE_FLAGS (TREE_OPERAND (node, 3));
4236 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
4237 FIELD_DECL, apparently. The G++ front end can put something else
4238 there, at least temporarily. */
4239 else if (TREE_CODE (node) == COMPONENT_REF
4240 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
4242 if (TREE_OPERAND (node, 2))
4243 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4247 node = lang_hooks.expr_to_decl (node, &tc, &se);
4249 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4250 the address, since &(*a)->b is a form of addition. If it's a constant, the
4251 address is constant too. If it's a decl, its address is constant if the
4252 decl is static. Everything else is not constant and, furthermore,
4253 taking the address of a volatile variable is not volatile. */
4254 if (TREE_CODE (node) == INDIRECT_REF
4255 || TREE_CODE (node) == MEM_REF)
4256 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4257 else if (CONSTANT_CLASS_P (node))
4259 else if (DECL_P (node))
4260 tc &= (staticp (node) != NULL_TREE);
4261 else
4263 tc = false;
4264 se |= TREE_SIDE_EFFECTS (node);
4268 TREE_CONSTANT (t) = tc;
4269 TREE_SIDE_EFFECTS (t) = se;
4270 #undef UPDATE_FLAGS
4273 /* Build an expression of code CODE, data type TYPE, and operands as
4274 specified. Expressions and reference nodes can be created this way.
4275 Constants, decls, types and misc nodes cannot be.
4277 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4278 enough for all extant tree codes. */
4280 tree
4281 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
4283 tree t;
4285 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4287 t = make_node_stat (code PASS_MEM_STAT);
4288 TREE_TYPE (t) = tt;
4290 return t;
4293 tree
4294 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4296 int length = sizeof (struct tree_exp);
4297 tree t;
4299 record_node_allocation_statistics (code, length);
4301 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4303 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4305 memset (t, 0, sizeof (struct tree_common));
4307 TREE_SET_CODE (t, code);
4309 TREE_TYPE (t) = type;
4310 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4311 TREE_OPERAND (t, 0) = node;
4312 if (node && !TYPE_P (node))
4314 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4315 TREE_READONLY (t) = TREE_READONLY (node);
4318 if (TREE_CODE_CLASS (code) == tcc_statement)
4319 TREE_SIDE_EFFECTS (t) = 1;
4320 else switch (code)
4322 case VA_ARG_EXPR:
4323 /* All of these have side-effects, no matter what their
4324 operands are. */
4325 TREE_SIDE_EFFECTS (t) = 1;
4326 TREE_READONLY (t) = 0;
4327 break;
4329 case INDIRECT_REF:
4330 /* Whether a dereference is readonly has nothing to do with whether
4331 its operand is readonly. */
4332 TREE_READONLY (t) = 0;
4333 break;
4335 case ADDR_EXPR:
4336 if (node)
4337 recompute_tree_invariant_for_addr_expr (t);
4338 break;
4340 default:
4341 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4342 && node && !TYPE_P (node)
4343 && TREE_CONSTANT (node))
4344 TREE_CONSTANT (t) = 1;
4345 if (TREE_CODE_CLASS (code) == tcc_reference
4346 && node && TREE_THIS_VOLATILE (node))
4347 TREE_THIS_VOLATILE (t) = 1;
4348 break;
4351 return t;
4354 #define PROCESS_ARG(N) \
4355 do { \
4356 TREE_OPERAND (t, N) = arg##N; \
4357 if (arg##N &&!TYPE_P (arg##N)) \
4359 if (TREE_SIDE_EFFECTS (arg##N)) \
4360 side_effects = 1; \
4361 if (!TREE_READONLY (arg##N) \
4362 && !CONSTANT_CLASS_P (arg##N)) \
4363 (void) (read_only = 0); \
4364 if (!TREE_CONSTANT (arg##N)) \
4365 (void) (constant = 0); \
4367 } while (0)
4369 tree
4370 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
4372 bool constant, read_only, side_effects;
4373 tree t;
4375 gcc_assert (TREE_CODE_LENGTH (code) == 2);
4377 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
4378 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
4379 /* When sizetype precision doesn't match that of pointers
4380 we need to be able to build explicit extensions or truncations
4381 of the offset argument. */
4382 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
4383 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
4384 && TREE_CODE (arg1) == INTEGER_CST);
4386 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
4387 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
4388 && ptrofftype_p (TREE_TYPE (arg1)));
4390 t = make_node_stat (code PASS_MEM_STAT);
4391 TREE_TYPE (t) = tt;
4393 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
4394 result based on those same flags for the arguments. But if the
4395 arguments aren't really even `tree' expressions, we shouldn't be trying
4396 to do this. */
4398 /* Expressions without side effects may be constant if their
4399 arguments are as well. */
4400 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4401 || TREE_CODE_CLASS (code) == tcc_binary);
4402 read_only = 1;
4403 side_effects = TREE_SIDE_EFFECTS (t);
4405 PROCESS_ARG (0);
4406 PROCESS_ARG (1);
4408 TREE_SIDE_EFFECTS (t) = side_effects;
4409 if (code == MEM_REF)
4411 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
4413 tree o = TREE_OPERAND (arg0, 0);
4414 TREE_READONLY (t) = TREE_READONLY (o);
4415 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
4418 else
4420 TREE_READONLY (t) = read_only;
4421 TREE_CONSTANT (t) = constant;
4422 TREE_THIS_VOLATILE (t)
4423 = (TREE_CODE_CLASS (code) == tcc_reference
4424 && arg0 && TREE_THIS_VOLATILE (arg0));
4427 return t;
4431 tree
4432 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4433 tree arg2 MEM_STAT_DECL)
4435 bool constant, read_only, side_effects;
4436 tree t;
4438 gcc_assert (TREE_CODE_LENGTH (code) == 3);
4439 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4441 t = make_node_stat (code PASS_MEM_STAT);
4442 TREE_TYPE (t) = tt;
4444 read_only = 1;
4446 /* As a special exception, if COND_EXPR has NULL branches, we
4447 assume that it is a gimple statement and always consider
4448 it to have side effects. */
4449 if (code == COND_EXPR
4450 && tt == void_type_node
4451 && arg1 == NULL_TREE
4452 && arg2 == NULL_TREE)
4453 side_effects = true;
4454 else
4455 side_effects = TREE_SIDE_EFFECTS (t);
4457 PROCESS_ARG (0);
4458 PROCESS_ARG (1);
4459 PROCESS_ARG (2);
4461 if (code == COND_EXPR)
4462 TREE_READONLY (t) = read_only;
4464 TREE_SIDE_EFFECTS (t) = side_effects;
4465 TREE_THIS_VOLATILE (t)
4466 = (TREE_CODE_CLASS (code) == tcc_reference
4467 && arg0 && TREE_THIS_VOLATILE (arg0));
4469 return t;
4472 tree
4473 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4474 tree arg2, tree arg3 MEM_STAT_DECL)
4476 bool constant, read_only, side_effects;
4477 tree t;
4479 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4481 t = make_node_stat (code PASS_MEM_STAT);
4482 TREE_TYPE (t) = tt;
4484 side_effects = TREE_SIDE_EFFECTS (t);
4486 PROCESS_ARG (0);
4487 PROCESS_ARG (1);
4488 PROCESS_ARG (2);
4489 PROCESS_ARG (3);
4491 TREE_SIDE_EFFECTS (t) = side_effects;
4492 TREE_THIS_VOLATILE (t)
4493 = (TREE_CODE_CLASS (code) == tcc_reference
4494 && arg0 && TREE_THIS_VOLATILE (arg0));
4496 return t;
4499 tree
4500 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4501 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4503 bool constant, read_only, side_effects;
4504 tree t;
4506 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4508 t = make_node_stat (code PASS_MEM_STAT);
4509 TREE_TYPE (t) = tt;
4511 side_effects = TREE_SIDE_EFFECTS (t);
4513 PROCESS_ARG (0);
4514 PROCESS_ARG (1);
4515 PROCESS_ARG (2);
4516 PROCESS_ARG (3);
4517 PROCESS_ARG (4);
4519 TREE_SIDE_EFFECTS (t) = side_effects;
4520 if (code == TARGET_MEM_REF)
4522 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
4524 tree o = TREE_OPERAND (arg0, 0);
4525 TREE_READONLY (t) = TREE_READONLY (o);
4526 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
4529 else
4530 TREE_THIS_VOLATILE (t)
4531 = (TREE_CODE_CLASS (code) == tcc_reference
4532 && arg0 && TREE_THIS_VOLATILE (arg0));
4534 return t;
4537 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4538 on the pointer PTR. */
4540 tree
4541 build_simple_mem_ref_loc (location_t loc, tree ptr)
4543 HOST_WIDE_INT offset = 0;
4544 tree ptype = TREE_TYPE (ptr);
4545 tree tem;
4546 /* For convenience allow addresses that collapse to a simple base
4547 and offset. */
4548 if (TREE_CODE (ptr) == ADDR_EXPR
4549 && (handled_component_p (TREE_OPERAND (ptr, 0))
4550 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4552 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4553 gcc_assert (ptr);
4554 ptr = build_fold_addr_expr (ptr);
4555 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4557 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4558 ptr, build_int_cst (ptype, offset));
4559 SET_EXPR_LOCATION (tem, loc);
4560 return tem;
4563 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4565 offset_int
4566 mem_ref_offset (const_tree t)
4568 return offset_int::from (TREE_OPERAND (t, 1), SIGNED);
4571 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4572 offsetted by OFFSET units. */
4574 tree
4575 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4577 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4578 build_fold_addr_expr (base),
4579 build_int_cst (ptr_type_node, offset));
4580 tree addr = build1 (ADDR_EXPR, type, ref);
4581 recompute_tree_invariant_for_addr_expr (addr);
4582 return addr;
4585 /* Similar except don't specify the TREE_TYPE
4586 and leave the TREE_SIDE_EFFECTS as 0.
4587 It is permissible for arguments to be null,
4588 or even garbage if their values do not matter. */
4590 tree
4591 build_nt (enum tree_code code, ...)
4593 tree t;
4594 int length;
4595 int i;
4596 va_list p;
4598 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4600 va_start (p, code);
4602 t = make_node (code);
4603 length = TREE_CODE_LENGTH (code);
4605 for (i = 0; i < length; i++)
4606 TREE_OPERAND (t, i) = va_arg (p, tree);
4608 va_end (p);
4609 return t;
4612 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4613 tree vec. */
4615 tree
4616 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4618 tree ret, t;
4619 unsigned int ix;
4621 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4622 CALL_EXPR_FN (ret) = fn;
4623 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4624 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4625 CALL_EXPR_ARG (ret, ix) = t;
4626 return ret;
4629 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4630 We do NOT enter this node in any sort of symbol table.
4632 LOC is the location of the decl.
4634 layout_decl is used to set up the decl's storage layout.
4635 Other slots are initialized to 0 or null pointers. */
4637 tree
4638 build_decl_stat (location_t loc, enum tree_code code, tree name,
4639 tree type MEM_STAT_DECL)
4641 tree t;
4643 t = make_node_stat (code PASS_MEM_STAT);
4644 DECL_SOURCE_LOCATION (t) = loc;
4646 /* if (type == error_mark_node)
4647 type = integer_type_node; */
4648 /* That is not done, deliberately, so that having error_mark_node
4649 as the type can suppress useless errors in the use of this variable. */
4651 DECL_NAME (t) = name;
4652 TREE_TYPE (t) = type;
4654 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4655 layout_decl (t, 0);
4657 return t;
4660 /* Builds and returns function declaration with NAME and TYPE. */
4662 tree
4663 build_fn_decl (const char *name, tree type)
4665 tree id = get_identifier (name);
4666 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4668 DECL_EXTERNAL (decl) = 1;
4669 TREE_PUBLIC (decl) = 1;
4670 DECL_ARTIFICIAL (decl) = 1;
4671 TREE_NOTHROW (decl) = 1;
4673 return decl;
4676 vec<tree, va_gc> *all_translation_units;
4678 /* Builds a new translation-unit decl with name NAME, queues it in the
4679 global list of translation-unit decls and returns it. */
4681 tree
4682 build_translation_unit_decl (tree name)
4684 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4685 name, NULL_TREE);
4686 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4687 vec_safe_push (all_translation_units, tu);
4688 return tu;
4692 /* BLOCK nodes are used to represent the structure of binding contours
4693 and declarations, once those contours have been exited and their contents
4694 compiled. This information is used for outputting debugging info. */
4696 tree
4697 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4699 tree block = make_node (BLOCK);
4701 BLOCK_VARS (block) = vars;
4702 BLOCK_SUBBLOCKS (block) = subblocks;
4703 BLOCK_SUPERCONTEXT (block) = supercontext;
4704 BLOCK_CHAIN (block) = chain;
4705 return block;
4709 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4711 LOC is the location to use in tree T. */
4713 void
4714 protected_set_expr_location (tree t, location_t loc)
4716 if (CAN_HAVE_LOCATION_P (t))
4717 SET_EXPR_LOCATION (t, loc);
4720 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4721 is ATTRIBUTE. */
4723 tree
4724 build_decl_attribute_variant (tree ddecl, tree attribute)
4726 DECL_ATTRIBUTES (ddecl) = attribute;
4727 return ddecl;
4730 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4731 is ATTRIBUTE and its qualifiers are QUALS.
4733 Record such modified types already made so we don't make duplicates. */
4735 tree
4736 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4738 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4740 inchash::hash hstate;
4741 tree ntype;
4742 int i;
4743 tree t;
4744 enum tree_code code = TREE_CODE (ttype);
4746 /* Building a distinct copy of a tagged type is inappropriate; it
4747 causes breakage in code that expects there to be a one-to-one
4748 relationship between a struct and its fields.
4749 build_duplicate_type is another solution (as used in
4750 handle_transparent_union_attribute), but that doesn't play well
4751 with the stronger C++ type identity model. */
4752 if (TREE_CODE (ttype) == RECORD_TYPE
4753 || TREE_CODE (ttype) == UNION_TYPE
4754 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4755 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4757 warning (OPT_Wattributes,
4758 "ignoring attributes applied to %qT after definition",
4759 TYPE_MAIN_VARIANT (ttype));
4760 return build_qualified_type (ttype, quals);
4763 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4764 ntype = build_distinct_type_copy (ttype);
4766 TYPE_ATTRIBUTES (ntype) = attribute;
4768 hstate.add_int (code);
4769 if (TREE_TYPE (ntype))
4770 hstate.add_object (TYPE_HASH (TREE_TYPE (ntype)));
4771 attribute_hash_list (attribute, hstate);
4773 switch (TREE_CODE (ntype))
4775 case FUNCTION_TYPE:
4776 type_hash_list (TYPE_ARG_TYPES (ntype), hstate);
4777 break;
4778 case ARRAY_TYPE:
4779 if (TYPE_DOMAIN (ntype))
4780 hstate.add_object (TYPE_HASH (TYPE_DOMAIN (ntype)));
4781 break;
4782 case INTEGER_TYPE:
4783 t = TYPE_MAX_VALUE (ntype);
4784 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
4785 hstate.add_object (TREE_INT_CST_ELT (t, i));
4786 break;
4787 case REAL_TYPE:
4788 case FIXED_POINT_TYPE:
4790 unsigned int precision = TYPE_PRECISION (ntype);
4791 hstate.add_object (precision);
4793 break;
4794 default:
4795 break;
4798 ntype = type_hash_canon (hstate.end(), ntype);
4800 /* If the target-dependent attributes make NTYPE different from
4801 its canonical type, we will need to use structural equality
4802 checks for this type. */
4803 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4804 || !comp_type_attributes (ntype, ttype))
4805 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4806 else if (TYPE_CANONICAL (ntype) == ntype)
4807 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4809 ttype = build_qualified_type (ntype, quals);
4811 else if (TYPE_QUALS (ttype) != quals)
4812 ttype = build_qualified_type (ttype, quals);
4814 return ttype;
4817 /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
4818 the same. */
4820 static bool
4821 omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
4823 tree cl1, cl2;
4824 for (cl1 = clauses1, cl2 = clauses2;
4825 cl1 && cl2;
4826 cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
4828 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
4829 return false;
4830 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
4832 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
4833 OMP_CLAUSE_DECL (cl2)) != 1)
4834 return false;
4836 switch (OMP_CLAUSE_CODE (cl1))
4838 case OMP_CLAUSE_ALIGNED:
4839 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
4840 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
4841 return false;
4842 break;
4843 case OMP_CLAUSE_LINEAR:
4844 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
4845 OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
4846 return false;
4847 break;
4848 case OMP_CLAUSE_SIMDLEN:
4849 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
4850 OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
4851 return false;
4852 default:
4853 break;
4856 return true;
4859 /* Compare two constructor-element-type constants. Return 1 if the lists
4860 are known to be equal; otherwise return 0. */
4862 static bool
4863 simple_cst_list_equal (const_tree l1, const_tree l2)
4865 while (l1 != NULL_TREE && l2 != NULL_TREE)
4867 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
4868 return false;
4870 l1 = TREE_CHAIN (l1);
4871 l2 = TREE_CHAIN (l2);
4874 return l1 == l2;
4877 /* Compare two identifier nodes representing attributes. Either one may
4878 be in wrapped __ATTR__ form. Return true if they are the same, false
4879 otherwise. */
4881 static bool
4882 cmp_attrib_identifiers (const_tree attr1, const_tree attr2)
4884 /* Make sure we're dealing with IDENTIFIER_NODEs. */
4885 gcc_checking_assert (TREE_CODE (attr1) == IDENTIFIER_NODE
4886 && TREE_CODE (attr2) == IDENTIFIER_NODE);
4888 /* Identifiers can be compared directly for equality. */
4889 if (attr1 == attr2)
4890 return true;
4892 /* If they are not equal, they may still be one in the form
4893 'text' while the other one is in the form '__text__'. TODO:
4894 If we were storing attributes in normalized 'text' form, then
4895 this could all go away and we could take full advantage of
4896 the fact that we're comparing identifiers. :-) */
4897 const size_t attr1_len = IDENTIFIER_LENGTH (attr1);
4898 const size_t attr2_len = IDENTIFIER_LENGTH (attr2);
4900 if (attr2_len == attr1_len + 4)
4902 const char *p = IDENTIFIER_POINTER (attr2);
4903 const char *q = IDENTIFIER_POINTER (attr1);
4904 if (p[0] == '_' && p[1] == '_'
4905 && p[attr2_len - 2] == '_' && p[attr2_len - 1] == '_'
4906 && strncmp (q, p + 2, attr1_len) == 0)
4907 return true;;
4909 else if (attr2_len + 4 == attr1_len)
4911 const char *p = IDENTIFIER_POINTER (attr2);
4912 const char *q = IDENTIFIER_POINTER (attr1);
4913 if (q[0] == '_' && q[1] == '_'
4914 && q[attr1_len - 2] == '_' && q[attr1_len - 1] == '_'
4915 && strncmp (q + 2, p, attr2_len) == 0)
4916 return true;
4919 return false;
4922 /* Compare two attributes for their value identity. Return true if the
4923 attribute values are known to be equal; otherwise return false. */
4925 bool
4926 attribute_value_equal (const_tree attr1, const_tree attr2)
4928 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4929 return true;
4931 if (TREE_VALUE (attr1) != NULL_TREE
4932 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4933 && TREE_VALUE (attr2) != NULL_TREE
4934 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4936 /* Handle attribute format. */
4937 if (is_attribute_p ("format", TREE_PURPOSE (attr1)))
4939 attr1 = TREE_VALUE (attr1);
4940 attr2 = TREE_VALUE (attr2);
4941 /* Compare the archetypes (printf/scanf/strftime/...). */
4942 if (!cmp_attrib_identifiers (TREE_VALUE (attr1),
4943 TREE_VALUE (attr2)))
4944 return false;
4945 /* Archetypes are the same. Compare the rest. */
4946 return (simple_cst_list_equal (TREE_CHAIN (attr1),
4947 TREE_CHAIN (attr2)) == 1);
4949 return (simple_cst_list_equal (TREE_VALUE (attr1),
4950 TREE_VALUE (attr2)) == 1);
4953 if ((flag_openmp || flag_openmp_simd)
4954 && TREE_VALUE (attr1) && TREE_VALUE (attr2)
4955 && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
4956 && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
4957 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
4958 TREE_VALUE (attr2));
4960 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4963 /* Return 0 if the attributes for two types are incompatible, 1 if they
4964 are compatible, and 2 if they are nearly compatible (which causes a
4965 warning to be generated). */
4967 comp_type_attributes (const_tree type1, const_tree type2)
4969 const_tree a1 = TYPE_ATTRIBUTES (type1);
4970 const_tree a2 = TYPE_ATTRIBUTES (type2);
4971 const_tree a;
4973 if (a1 == a2)
4974 return 1;
4975 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4977 const struct attribute_spec *as;
4978 const_tree attr;
4980 as = lookup_attribute_spec (get_attribute_name (a));
4981 if (!as || as->affects_type_identity == false)
4982 continue;
4984 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4985 if (!attr || !attribute_value_equal (a, attr))
4986 break;
4988 if (!a)
4990 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
4992 const struct attribute_spec *as;
4994 as = lookup_attribute_spec (get_attribute_name (a));
4995 if (!as || as->affects_type_identity == false)
4996 continue;
4998 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
4999 break;
5000 /* We don't need to compare trees again, as we did this
5001 already in first loop. */
5003 /* All types - affecting identity - are equal, so
5004 there is no need to call target hook for comparison. */
5005 if (!a)
5006 return 1;
5008 /* As some type combinations - like default calling-convention - might
5009 be compatible, we have to call the target hook to get the final result. */
5010 return targetm.comp_type_attributes (type1, type2);
5013 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
5014 is ATTRIBUTE.
5016 Record such modified types already made so we don't make duplicates. */
5018 tree
5019 build_type_attribute_variant (tree ttype, tree attribute)
5021 return build_type_attribute_qual_variant (ttype, attribute,
5022 TYPE_QUALS (ttype));
5026 /* Reset the expression *EXPR_P, a size or position.
5028 ??? We could reset all non-constant sizes or positions. But it's cheap
5029 enough to not do so and refrain from adding workarounds to dwarf2out.c.
5031 We need to reset self-referential sizes or positions because they cannot
5032 be gimplified and thus can contain a CALL_EXPR after the gimplification
5033 is finished, which will run afoul of LTO streaming. And they need to be
5034 reset to something essentially dummy but not constant, so as to preserve
5035 the properties of the object they are attached to. */
5037 static inline void
5038 free_lang_data_in_one_sizepos (tree *expr_p)
5040 tree expr = *expr_p;
5041 if (CONTAINS_PLACEHOLDER_P (expr))
5042 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
5046 /* Reset all the fields in a binfo node BINFO. We only keep
5047 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
5049 static void
5050 free_lang_data_in_binfo (tree binfo)
5052 unsigned i;
5053 tree t;
5055 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
5057 BINFO_VIRTUALS (binfo) = NULL_TREE;
5058 BINFO_BASE_ACCESSES (binfo) = NULL;
5059 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
5060 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
5062 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
5063 free_lang_data_in_binfo (t);
5067 /* Reset all language specific information still present in TYPE. */
5069 static void
5070 free_lang_data_in_type (tree type)
5072 gcc_assert (TYPE_P (type));
5074 /* Give the FE a chance to remove its own data first. */
5075 lang_hooks.free_lang_data (type);
5077 TREE_LANG_FLAG_0 (type) = 0;
5078 TREE_LANG_FLAG_1 (type) = 0;
5079 TREE_LANG_FLAG_2 (type) = 0;
5080 TREE_LANG_FLAG_3 (type) = 0;
5081 TREE_LANG_FLAG_4 (type) = 0;
5082 TREE_LANG_FLAG_5 (type) = 0;
5083 TREE_LANG_FLAG_6 (type) = 0;
5085 if (TREE_CODE (type) == FUNCTION_TYPE)
5087 /* Remove the const and volatile qualifiers from arguments. The
5088 C++ front end removes them, but the C front end does not,
5089 leading to false ODR violation errors when merging two
5090 instances of the same function signature compiled by
5091 different front ends. */
5092 tree p;
5094 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
5096 tree arg_type = TREE_VALUE (p);
5098 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
5100 int quals = TYPE_QUALS (arg_type)
5101 & ~TYPE_QUAL_CONST
5102 & ~TYPE_QUAL_VOLATILE;
5103 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
5104 free_lang_data_in_type (TREE_VALUE (p));
5106 /* C++ FE uses TREE_PURPOSE to store initial values. */
5107 TREE_PURPOSE (p) = NULL;
5109 /* Java uses TYPE_MINVAL for TYPE_ARGUMENT_SIGNATURE. */
5110 TYPE_MINVAL (type) = NULL;
5112 if (TREE_CODE (type) == METHOD_TYPE)
5114 tree p;
5116 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
5118 /* C++ FE uses TREE_PURPOSE to store initial values. */
5119 TREE_PURPOSE (p) = NULL;
5121 /* Java uses TYPE_MINVAL for TYPE_ARGUMENT_SIGNATURE. */
5122 TYPE_MINVAL (type) = NULL;
5125 /* Remove members that are not actually FIELD_DECLs from the field
5126 list of an aggregate. These occur in C++. */
5127 if (RECORD_OR_UNION_TYPE_P (type))
5129 tree prev, member;
5131 /* Note that TYPE_FIELDS can be shared across distinct
5132 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
5133 to be removed, we cannot set its TREE_CHAIN to NULL.
5134 Otherwise, we would not be able to find all the other fields
5135 in the other instances of this TREE_TYPE.
5137 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
5138 prev = NULL_TREE;
5139 member = TYPE_FIELDS (type);
5140 while (member)
5142 if (TREE_CODE (member) == FIELD_DECL
5143 || TREE_CODE (member) == TYPE_DECL)
5145 if (prev)
5146 TREE_CHAIN (prev) = member;
5147 else
5148 TYPE_FIELDS (type) = member;
5149 prev = member;
5152 member = TREE_CHAIN (member);
5155 if (prev)
5156 TREE_CHAIN (prev) = NULL_TREE;
5157 else
5158 TYPE_FIELDS (type) = NULL_TREE;
5160 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
5161 and danagle the pointer from time to time. */
5162 if (TYPE_VFIELD (type) && TREE_CODE (TYPE_VFIELD (type)) != FIELD_DECL)
5163 TYPE_VFIELD (type) = NULL_TREE;
5165 /* Remove TYPE_METHODS list. While it would be nice to keep it
5166 to enable ODR warnings about different method lists, doing so
5167 seems to impractically increase size of LTO data streamed.
5168 Keep the infrmation if TYPE_METHODS was non-NULL. This is used
5169 by function.c and pretty printers. */
5170 if (TYPE_METHODS (type))
5171 TYPE_METHODS (type) = error_mark_node;
5172 if (TYPE_BINFO (type))
5174 free_lang_data_in_binfo (TYPE_BINFO (type));
5175 /* We need to preserve link to bases and virtual table for all
5176 polymorphic types to make devirtualization machinery working.
5177 Debug output cares only about bases, but output also
5178 virtual table pointers so merging of -fdevirtualize and
5179 -fno-devirtualize units is easier. */
5180 if ((!BINFO_VTABLE (TYPE_BINFO (type))
5181 || !flag_devirtualize)
5182 && ((!BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
5183 && !BINFO_VTABLE (TYPE_BINFO (type)))
5184 || debug_info_level != DINFO_LEVEL_NONE))
5185 TYPE_BINFO (type) = NULL;
5188 else
5190 /* For non-aggregate types, clear out the language slot (which
5191 overloads TYPE_BINFO). */
5192 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
5194 if (INTEGRAL_TYPE_P (type)
5195 || SCALAR_FLOAT_TYPE_P (type)
5196 || FIXED_POINT_TYPE_P (type))
5198 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
5199 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
5203 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
5204 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
5206 if (TYPE_CONTEXT (type)
5207 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
5209 tree ctx = TYPE_CONTEXT (type);
5212 ctx = BLOCK_SUPERCONTEXT (ctx);
5214 while (ctx && TREE_CODE (ctx) == BLOCK);
5215 TYPE_CONTEXT (type) = ctx;
5220 /* Return true if DECL may need an assembler name to be set. */
5222 static inline bool
5223 need_assembler_name_p (tree decl)
5225 /* We use DECL_ASSEMBLER_NAME to hold mangled type names for One Definition
5226 Rule merging. This makes type_odr_p to return true on those types during
5227 LTO and by comparing the mangled name, we can say what types are intended
5228 to be equivalent across compilation unit.
5230 We do not store names of type_in_anonymous_namespace_p.
5232 Record, union and enumeration type have linkage that allows use
5233 to check type_in_anonymous_namespace_p. We do not mangle compound types
5234 that always can be compared structurally.
5236 Similarly for builtin types, we compare properties of their main variant.
5237 A special case are integer types where mangling do make differences
5238 between char/signed char/unsigned char etc. Storing name for these makes
5239 e.g. -fno-signed-char/-fsigned-char mismatches to be handled well.
5240 See cp/mangle.c:write_builtin_type for details. */
5242 if (flag_lto_odr_type_mering
5243 && TREE_CODE (decl) == TYPE_DECL
5244 && DECL_NAME (decl)
5245 && decl == TYPE_NAME (TREE_TYPE (decl))
5246 && !TYPE_ARTIFICIAL (TREE_TYPE (decl))
5247 && (type_with_linkage_p (TREE_TYPE (decl))
5248 || TREE_CODE (TREE_TYPE (decl)) == INTEGER_TYPE)
5249 && !variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5250 return !DECL_ASSEMBLER_NAME_SET_P (decl);
5251 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
5252 if (TREE_CODE (decl) != FUNCTION_DECL
5253 && TREE_CODE (decl) != VAR_DECL)
5254 return false;
5256 /* If DECL already has its assembler name set, it does not need a
5257 new one. */
5258 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
5259 || DECL_ASSEMBLER_NAME_SET_P (decl))
5260 return false;
5262 /* Abstract decls do not need an assembler name. */
5263 if (DECL_ABSTRACT_P (decl))
5264 return false;
5266 /* For VAR_DECLs, only static, public and external symbols need an
5267 assembler name. */
5268 if (TREE_CODE (decl) == VAR_DECL
5269 && !TREE_STATIC (decl)
5270 && !TREE_PUBLIC (decl)
5271 && !DECL_EXTERNAL (decl))
5272 return false;
5274 if (TREE_CODE (decl) == FUNCTION_DECL)
5276 /* Do not set assembler name on builtins. Allow RTL expansion to
5277 decide whether to expand inline or via a regular call. */
5278 if (DECL_BUILT_IN (decl)
5279 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
5280 return false;
5282 /* Functions represented in the callgraph need an assembler name. */
5283 if (cgraph_node::get (decl) != NULL)
5284 return true;
5286 /* Unused and not public functions don't need an assembler name. */
5287 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
5288 return false;
5291 return true;
5295 /* Reset all language specific information still present in symbol
5296 DECL. */
5298 static void
5299 free_lang_data_in_decl (tree decl)
5301 gcc_assert (DECL_P (decl));
5303 /* Give the FE a chance to remove its own data first. */
5304 lang_hooks.free_lang_data (decl);
5306 TREE_LANG_FLAG_0 (decl) = 0;
5307 TREE_LANG_FLAG_1 (decl) = 0;
5308 TREE_LANG_FLAG_2 (decl) = 0;
5309 TREE_LANG_FLAG_3 (decl) = 0;
5310 TREE_LANG_FLAG_4 (decl) = 0;
5311 TREE_LANG_FLAG_5 (decl) = 0;
5312 TREE_LANG_FLAG_6 (decl) = 0;
5314 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
5315 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
5316 if (TREE_CODE (decl) == FIELD_DECL)
5318 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
5319 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
5320 DECL_QUALIFIER (decl) = NULL_TREE;
5323 if (TREE_CODE (decl) == FUNCTION_DECL)
5325 struct cgraph_node *node;
5326 if (!(node = cgraph_node::get (decl))
5327 || (!node->definition && !node->clones))
5329 if (node)
5330 node->release_body ();
5331 else
5333 release_function_body (decl);
5334 DECL_ARGUMENTS (decl) = NULL;
5335 DECL_RESULT (decl) = NULL;
5336 DECL_INITIAL (decl) = error_mark_node;
5339 if (gimple_has_body_p (decl))
5341 tree t;
5343 /* If DECL has a gimple body, then the context for its
5344 arguments must be DECL. Otherwise, it doesn't really
5345 matter, as we will not be emitting any code for DECL. In
5346 general, there may be other instances of DECL created by
5347 the front end and since PARM_DECLs are generally shared,
5348 their DECL_CONTEXT changes as the replicas of DECL are
5349 created. The only time where DECL_CONTEXT is important
5350 is for the FUNCTION_DECLs that have a gimple body (since
5351 the PARM_DECL will be used in the function's body). */
5352 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
5353 DECL_CONTEXT (t) = decl;
5354 if (!DECL_FUNCTION_SPECIFIC_TARGET (decl))
5355 DECL_FUNCTION_SPECIFIC_TARGET (decl)
5356 = target_option_default_node;
5357 if (!DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl))
5358 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl)
5359 = optimization_default_node;
5362 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
5363 At this point, it is not needed anymore. */
5364 DECL_SAVED_TREE (decl) = NULL_TREE;
5366 /* Clear the abstract origin if it refers to a method. Otherwise
5367 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
5368 origin will not be output correctly. */
5369 if (DECL_ABSTRACT_ORIGIN (decl)
5370 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
5371 && RECORD_OR_UNION_TYPE_P
5372 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
5373 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
5375 /* Sometimes the C++ frontend doesn't manage to transform a temporary
5376 DECL_VINDEX referring to itself into a vtable slot number as it
5377 should. Happens with functions that are copied and then forgotten
5378 about. Just clear it, it won't matter anymore. */
5379 if (DECL_VINDEX (decl) && !tree_fits_shwi_p (DECL_VINDEX (decl)))
5380 DECL_VINDEX (decl) = NULL_TREE;
5382 else if (TREE_CODE (decl) == VAR_DECL)
5384 if ((DECL_EXTERNAL (decl)
5385 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
5386 || (decl_function_context (decl) && !TREE_STATIC (decl)))
5387 DECL_INITIAL (decl) = NULL_TREE;
5389 else if (TREE_CODE (decl) == TYPE_DECL
5390 || TREE_CODE (decl) == FIELD_DECL)
5391 DECL_INITIAL (decl) = NULL_TREE;
5392 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
5393 && DECL_INITIAL (decl)
5394 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
5396 /* Strip builtins from the translation-unit BLOCK. We still have targets
5397 without builtin_decl_explicit support and also builtins are shared
5398 nodes and thus we can't use TREE_CHAIN in multiple lists. */
5399 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
5400 while (*nextp)
5402 tree var = *nextp;
5403 if (TREE_CODE (var) == FUNCTION_DECL
5404 && DECL_BUILT_IN (var))
5405 *nextp = TREE_CHAIN (var);
5406 else
5407 nextp = &TREE_CHAIN (var);
5413 /* Data used when collecting DECLs and TYPEs for language data removal. */
5415 struct free_lang_data_d
5417 /* Worklist to avoid excessive recursion. */
5418 vec<tree> worklist;
5420 /* Set of traversed objects. Used to avoid duplicate visits. */
5421 hash_set<tree> *pset;
5423 /* Array of symbols to process with free_lang_data_in_decl. */
5424 vec<tree> decls;
5426 /* Array of types to process with free_lang_data_in_type. */
5427 vec<tree> types;
5431 /* Save all language fields needed to generate proper debug information
5432 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
5434 static void
5435 save_debug_info_for_decl (tree t)
5437 /*struct saved_debug_info_d *sdi;*/
5439 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
5441 /* FIXME. Partial implementation for saving debug info removed. */
5445 /* Save all language fields needed to generate proper debug information
5446 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
5448 static void
5449 save_debug_info_for_type (tree t)
5451 /*struct saved_debug_info_d *sdi;*/
5453 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
5455 /* FIXME. Partial implementation for saving debug info removed. */
5459 /* Add type or decl T to one of the list of tree nodes that need their
5460 language data removed. The lists are held inside FLD. */
5462 static void
5463 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
5465 if (DECL_P (t))
5467 fld->decls.safe_push (t);
5468 if (debug_info_level > DINFO_LEVEL_TERSE)
5469 save_debug_info_for_decl (t);
5471 else if (TYPE_P (t))
5473 fld->types.safe_push (t);
5474 if (debug_info_level > DINFO_LEVEL_TERSE)
5475 save_debug_info_for_type (t);
5477 else
5478 gcc_unreachable ();
5481 /* Push tree node T into FLD->WORKLIST. */
5483 static inline void
5484 fld_worklist_push (tree t, struct free_lang_data_d *fld)
5486 if (t && !is_lang_specific (t) && !fld->pset->contains (t))
5487 fld->worklist.safe_push ((t));
5491 /* Operand callback helper for free_lang_data_in_node. *TP is the
5492 subtree operand being considered. */
5494 static tree
5495 find_decls_types_r (tree *tp, int *ws, void *data)
5497 tree t = *tp;
5498 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
5500 if (TREE_CODE (t) == TREE_LIST)
5501 return NULL_TREE;
5503 /* Language specific nodes will be removed, so there is no need
5504 to gather anything under them. */
5505 if (is_lang_specific (t))
5507 *ws = 0;
5508 return NULL_TREE;
5511 if (DECL_P (t))
5513 /* Note that walk_tree does not traverse every possible field in
5514 decls, so we have to do our own traversals here. */
5515 add_tree_to_fld_list (t, fld);
5517 fld_worklist_push (DECL_NAME (t), fld);
5518 fld_worklist_push (DECL_CONTEXT (t), fld);
5519 fld_worklist_push (DECL_SIZE (t), fld);
5520 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
5522 /* We are going to remove everything under DECL_INITIAL for
5523 TYPE_DECLs. No point walking them. */
5524 if (TREE_CODE (t) != TYPE_DECL)
5525 fld_worklist_push (DECL_INITIAL (t), fld);
5527 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
5528 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
5530 if (TREE_CODE (t) == FUNCTION_DECL)
5532 fld_worklist_push (DECL_ARGUMENTS (t), fld);
5533 fld_worklist_push (DECL_RESULT (t), fld);
5535 else if (TREE_CODE (t) == TYPE_DECL)
5537 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
5539 else if (TREE_CODE (t) == FIELD_DECL)
5541 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
5542 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
5543 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
5544 fld_worklist_push (DECL_FCONTEXT (t), fld);
5547 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
5548 && DECL_HAS_VALUE_EXPR_P (t))
5549 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
5551 if (TREE_CODE (t) != FIELD_DECL
5552 && TREE_CODE (t) != TYPE_DECL)
5553 fld_worklist_push (TREE_CHAIN (t), fld);
5554 *ws = 0;
5556 else if (TYPE_P (t))
5558 /* Note that walk_tree does not traverse every possible field in
5559 types, so we have to do our own traversals here. */
5560 add_tree_to_fld_list (t, fld);
5562 if (!RECORD_OR_UNION_TYPE_P (t))
5563 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
5564 fld_worklist_push (TYPE_SIZE (t), fld);
5565 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
5566 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
5567 fld_worklist_push (TYPE_POINTER_TO (t), fld);
5568 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
5569 fld_worklist_push (TYPE_NAME (t), fld);
5570 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
5571 them and thus do not and want not to reach unused pointer types
5572 this way. */
5573 if (!POINTER_TYPE_P (t))
5574 fld_worklist_push (TYPE_MINVAL (t), fld);
5575 if (!RECORD_OR_UNION_TYPE_P (t))
5576 fld_worklist_push (TYPE_MAXVAL (t), fld);
5577 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
5578 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
5579 do not and want not to reach unused variants this way. */
5580 if (TYPE_CONTEXT (t))
5582 tree ctx = TYPE_CONTEXT (t);
5583 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
5584 So push that instead. */
5585 while (ctx && TREE_CODE (ctx) == BLOCK)
5586 ctx = BLOCK_SUPERCONTEXT (ctx);
5587 fld_worklist_push (ctx, fld);
5589 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
5590 and want not to reach unused types this way. */
5592 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
5594 unsigned i;
5595 tree tem;
5596 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
5597 fld_worklist_push (TREE_TYPE (tem), fld);
5598 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
5599 if (tem
5600 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
5601 && TREE_CODE (tem) == TREE_LIST)
5604 fld_worklist_push (TREE_VALUE (tem), fld);
5605 tem = TREE_CHAIN (tem);
5607 while (tem);
5609 if (RECORD_OR_UNION_TYPE_P (t))
5611 tree tem;
5612 /* Push all TYPE_FIELDS - there can be interleaving interesting
5613 and non-interesting things. */
5614 tem = TYPE_FIELDS (t);
5615 while (tem)
5617 if (TREE_CODE (tem) == FIELD_DECL
5618 || TREE_CODE (tem) == TYPE_DECL)
5619 fld_worklist_push (tem, fld);
5620 tem = TREE_CHAIN (tem);
5624 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5625 *ws = 0;
5627 else if (TREE_CODE (t) == BLOCK)
5629 tree tem;
5630 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5631 fld_worklist_push (tem, fld);
5632 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5633 fld_worklist_push (tem, fld);
5634 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5637 if (TREE_CODE (t) != IDENTIFIER_NODE
5638 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5639 fld_worklist_push (TREE_TYPE (t), fld);
5641 return NULL_TREE;
5645 /* Find decls and types in T. */
5647 static void
5648 find_decls_types (tree t, struct free_lang_data_d *fld)
5650 while (1)
5652 if (!fld->pset->contains (t))
5653 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5654 if (fld->worklist.is_empty ())
5655 break;
5656 t = fld->worklist.pop ();
5660 /* Translate all the types in LIST with the corresponding runtime
5661 types. */
5663 static tree
5664 get_eh_types_for_runtime (tree list)
5666 tree head, prev;
5668 if (list == NULL_TREE)
5669 return NULL_TREE;
5671 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5672 prev = head;
5673 list = TREE_CHAIN (list);
5674 while (list)
5676 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5677 TREE_CHAIN (prev) = n;
5678 prev = TREE_CHAIN (prev);
5679 list = TREE_CHAIN (list);
5682 return head;
5686 /* Find decls and types referenced in EH region R and store them in
5687 FLD->DECLS and FLD->TYPES. */
5689 static void
5690 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5692 switch (r->type)
5694 case ERT_CLEANUP:
5695 break;
5697 case ERT_TRY:
5699 eh_catch c;
5701 /* The types referenced in each catch must first be changed to the
5702 EH types used at runtime. This removes references to FE types
5703 in the region. */
5704 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5706 c->type_list = get_eh_types_for_runtime (c->type_list);
5707 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5710 break;
5712 case ERT_ALLOWED_EXCEPTIONS:
5713 r->u.allowed.type_list
5714 = get_eh_types_for_runtime (r->u.allowed.type_list);
5715 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5716 break;
5718 case ERT_MUST_NOT_THROW:
5719 walk_tree (&r->u.must_not_throw.failure_decl,
5720 find_decls_types_r, fld, fld->pset);
5721 break;
5726 /* Find decls and types referenced in cgraph node N and store them in
5727 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5728 look for *every* kind of DECL and TYPE node reachable from N,
5729 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5730 NAMESPACE_DECLs, etc). */
5732 static void
5733 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5735 basic_block bb;
5736 struct function *fn;
5737 unsigned ix;
5738 tree t;
5740 find_decls_types (n->decl, fld);
5742 if (!gimple_has_body_p (n->decl))
5743 return;
5745 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5747 fn = DECL_STRUCT_FUNCTION (n->decl);
5749 /* Traverse locals. */
5750 FOR_EACH_LOCAL_DECL (fn, ix, t)
5751 find_decls_types (t, fld);
5753 /* Traverse EH regions in FN. */
5755 eh_region r;
5756 FOR_ALL_EH_REGION_FN (r, fn)
5757 find_decls_types_in_eh_region (r, fld);
5760 /* Traverse every statement in FN. */
5761 FOR_EACH_BB_FN (bb, fn)
5763 gphi_iterator psi;
5764 gimple_stmt_iterator si;
5765 unsigned i;
5767 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
5769 gphi *phi = psi.phi ();
5771 for (i = 0; i < gimple_phi_num_args (phi); i++)
5773 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5774 find_decls_types (*arg_p, fld);
5778 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5780 gimple stmt = gsi_stmt (si);
5782 if (is_gimple_call (stmt))
5783 find_decls_types (gimple_call_fntype (stmt), fld);
5785 for (i = 0; i < gimple_num_ops (stmt); i++)
5787 tree arg = gimple_op (stmt, i);
5788 find_decls_types (arg, fld);
5795 /* Find decls and types referenced in varpool node N and store them in
5796 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5797 look for *every* kind of DECL and TYPE node reachable from N,
5798 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5799 NAMESPACE_DECLs, etc). */
5801 static void
5802 find_decls_types_in_var (varpool_node *v, struct free_lang_data_d *fld)
5804 find_decls_types (v->decl, fld);
5807 /* If T needs an assembler name, have one created for it. */
5809 void
5810 assign_assembler_name_if_neeeded (tree t)
5812 if (need_assembler_name_p (t))
5814 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5815 diagnostics that use input_location to show locus
5816 information. The problem here is that, at this point,
5817 input_location is generally anchored to the end of the file
5818 (since the parser is long gone), so we don't have a good
5819 position to pin it to.
5821 To alleviate this problem, this uses the location of T's
5822 declaration. Examples of this are
5823 testsuite/g++.dg/template/cond2.C and
5824 testsuite/g++.dg/template/pr35240.C. */
5825 location_t saved_location = input_location;
5826 input_location = DECL_SOURCE_LOCATION (t);
5828 decl_assembler_name (t);
5830 input_location = saved_location;
5835 /* Free language specific information for every operand and expression
5836 in every node of the call graph. This process operates in three stages:
5838 1- Every callgraph node and varpool node is traversed looking for
5839 decls and types embedded in them. This is a more exhaustive
5840 search than that done by find_referenced_vars, because it will
5841 also collect individual fields, decls embedded in types, etc.
5843 2- All the decls found are sent to free_lang_data_in_decl.
5845 3- All the types found are sent to free_lang_data_in_type.
5847 The ordering between decls and types is important because
5848 free_lang_data_in_decl sets assembler names, which includes
5849 mangling. So types cannot be freed up until assembler names have
5850 been set up. */
5852 static void
5853 free_lang_data_in_cgraph (void)
5855 struct cgraph_node *n;
5856 varpool_node *v;
5857 struct free_lang_data_d fld;
5858 tree t;
5859 unsigned i;
5860 alias_pair *p;
5862 /* Initialize sets and arrays to store referenced decls and types. */
5863 fld.pset = new hash_set<tree>;
5864 fld.worklist.create (0);
5865 fld.decls.create (100);
5866 fld.types.create (100);
5868 /* Find decls and types in the body of every function in the callgraph. */
5869 FOR_EACH_FUNCTION (n)
5870 find_decls_types_in_node (n, &fld);
5872 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5873 find_decls_types (p->decl, &fld);
5875 /* Find decls and types in every varpool symbol. */
5876 FOR_EACH_VARIABLE (v)
5877 find_decls_types_in_var (v, &fld);
5879 /* Set the assembler name on every decl found. We need to do this
5880 now because free_lang_data_in_decl will invalidate data needed
5881 for mangling. This breaks mangling on interdependent decls. */
5882 FOR_EACH_VEC_ELT (fld.decls, i, t)
5883 assign_assembler_name_if_neeeded (t);
5885 /* Traverse every decl found freeing its language data. */
5886 FOR_EACH_VEC_ELT (fld.decls, i, t)
5887 free_lang_data_in_decl (t);
5889 /* Traverse every type found freeing its language data. */
5890 FOR_EACH_VEC_ELT (fld.types, i, t)
5891 free_lang_data_in_type (t);
5892 #ifdef ENABLE_CHECKING
5893 FOR_EACH_VEC_ELT (fld.types, i, t)
5894 verify_type (t);
5895 #endif
5897 delete fld.pset;
5898 fld.worklist.release ();
5899 fld.decls.release ();
5900 fld.types.release ();
5904 /* Free resources that are used by FE but are not needed once they are done. */
5906 static unsigned
5907 free_lang_data (void)
5909 unsigned i;
5911 /* If we are the LTO frontend we have freed lang-specific data already. */
5912 if (in_lto_p
5913 || (!flag_generate_lto && !flag_generate_offload))
5914 return 0;
5916 /* Allocate and assign alias sets to the standard integer types
5917 while the slots are still in the way the frontends generated them. */
5918 for (i = 0; i < itk_none; ++i)
5919 if (integer_types[i])
5920 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5922 /* Traverse the IL resetting language specific information for
5923 operands, expressions, etc. */
5924 free_lang_data_in_cgraph ();
5926 /* Create gimple variants for common types. */
5927 ptrdiff_type_node = integer_type_node;
5928 fileptr_type_node = ptr_type_node;
5930 /* Reset some langhooks. Do not reset types_compatible_p, it may
5931 still be used indirectly via the get_alias_set langhook. */
5932 lang_hooks.dwarf_name = lhd_dwarf_name;
5933 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5934 lang_hooks.gimplify_expr = lhd_gimplify_expr;
5936 /* We do not want the default decl_assembler_name implementation,
5937 rather if we have fixed everything we want a wrapper around it
5938 asserting that all non-local symbols already got their assembler
5939 name and only produce assembler names for local symbols. Or rather
5940 make sure we never call decl_assembler_name on local symbols and
5941 devise a separate, middle-end private scheme for it. */
5943 /* Reset diagnostic machinery. */
5944 tree_diagnostics_defaults (global_dc);
5946 return 0;
5950 namespace {
5952 const pass_data pass_data_ipa_free_lang_data =
5954 SIMPLE_IPA_PASS, /* type */
5955 "*free_lang_data", /* name */
5956 OPTGROUP_NONE, /* optinfo_flags */
5957 TV_IPA_FREE_LANG_DATA, /* tv_id */
5958 0, /* properties_required */
5959 0, /* properties_provided */
5960 0, /* properties_destroyed */
5961 0, /* todo_flags_start */
5962 0, /* todo_flags_finish */
5965 class pass_ipa_free_lang_data : public simple_ipa_opt_pass
5967 public:
5968 pass_ipa_free_lang_data (gcc::context *ctxt)
5969 : simple_ipa_opt_pass (pass_data_ipa_free_lang_data, ctxt)
5972 /* opt_pass methods: */
5973 virtual unsigned int execute (function *) { return free_lang_data (); }
5975 }; // class pass_ipa_free_lang_data
5977 } // anon namespace
5979 simple_ipa_opt_pass *
5980 make_pass_ipa_free_lang_data (gcc::context *ctxt)
5982 return new pass_ipa_free_lang_data (ctxt);
5985 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
5986 ATTR_NAME. Also used internally by remove_attribute(). */
5987 bool
5988 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
5990 size_t ident_len = IDENTIFIER_LENGTH (ident);
5992 if (ident_len == attr_len)
5994 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
5995 return true;
5997 else if (ident_len == attr_len + 4)
5999 /* There is the possibility that ATTR is 'text' and IDENT is
6000 '__text__'. */
6001 const char *p = IDENTIFIER_POINTER (ident);
6002 if (p[0] == '_' && p[1] == '_'
6003 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
6004 && strncmp (attr_name, p + 2, attr_len) == 0)
6005 return true;
6008 return false;
6011 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
6012 of ATTR_NAME, and LIST is not NULL_TREE. */
6013 tree
6014 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
6016 while (list)
6018 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
6020 if (ident_len == attr_len)
6022 if (!strcmp (attr_name,
6023 IDENTIFIER_POINTER (get_attribute_name (list))))
6024 break;
6026 /* TODO: If we made sure that attributes were stored in the
6027 canonical form without '__...__' (ie, as in 'text' as opposed
6028 to '__text__') then we could avoid the following case. */
6029 else if (ident_len == attr_len + 4)
6031 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
6032 if (p[0] == '_' && p[1] == '_'
6033 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
6034 && strncmp (attr_name, p + 2, attr_len) == 0)
6035 break;
6037 list = TREE_CHAIN (list);
6040 return list;
6043 /* Given an attribute name ATTR_NAME and a list of attributes LIST,
6044 return a pointer to the attribute's list first element if the attribute
6045 starts with ATTR_NAME. ATTR_NAME must be in the form 'text' (not
6046 '__text__'). */
6048 tree
6049 private_lookup_attribute_by_prefix (const char *attr_name, size_t attr_len,
6050 tree list)
6052 while (list)
6054 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
6056 if (attr_len > ident_len)
6058 list = TREE_CHAIN (list);
6059 continue;
6062 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
6064 if (strncmp (attr_name, p, attr_len) == 0)
6065 break;
6067 /* TODO: If we made sure that attributes were stored in the
6068 canonical form without '__...__' (ie, as in 'text' as opposed
6069 to '__text__') then we could avoid the following case. */
6070 if (p[0] == '_' && p[1] == '_' &&
6071 strncmp (attr_name, p + 2, attr_len) == 0)
6072 break;
6074 list = TREE_CHAIN (list);
6077 return list;
6081 /* A variant of lookup_attribute() that can be used with an identifier
6082 as the first argument, and where the identifier can be either
6083 'text' or '__text__'.
6085 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
6086 return a pointer to the attribute's list element if the attribute
6087 is part of the list, or NULL_TREE if not found. If the attribute
6088 appears more than once, this only returns the first occurrence; the
6089 TREE_CHAIN of the return value should be passed back in if further
6090 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
6091 can be in the form 'text' or '__text__'. */
6092 static tree
6093 lookup_ident_attribute (tree attr_identifier, tree list)
6095 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
6097 while (list)
6099 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
6100 == IDENTIFIER_NODE);
6102 if (cmp_attrib_identifiers (attr_identifier,
6103 get_attribute_name (list)))
6104 /* Found it. */
6105 break;
6106 list = TREE_CHAIN (list);
6109 return list;
6112 /* Remove any instances of attribute ATTR_NAME in LIST and return the
6113 modified list. */
6115 tree
6116 remove_attribute (const char *attr_name, tree list)
6118 tree *p;
6119 size_t attr_len = strlen (attr_name);
6121 gcc_checking_assert (attr_name[0] != '_');
6123 for (p = &list; *p; )
6125 tree l = *p;
6126 /* TODO: If we were storing attributes in normalized form, here
6127 we could use a simple strcmp(). */
6128 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
6129 *p = TREE_CHAIN (l);
6130 else
6131 p = &TREE_CHAIN (l);
6134 return list;
6137 /* Return an attribute list that is the union of a1 and a2. */
6139 tree
6140 merge_attributes (tree a1, tree a2)
6142 tree attributes;
6144 /* Either one unset? Take the set one. */
6146 if ((attributes = a1) == 0)
6147 attributes = a2;
6149 /* One that completely contains the other? Take it. */
6151 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
6153 if (attribute_list_contained (a2, a1))
6154 attributes = a2;
6155 else
6157 /* Pick the longest list, and hang on the other list. */
6159 if (list_length (a1) < list_length (a2))
6160 attributes = a2, a2 = a1;
6162 for (; a2 != 0; a2 = TREE_CHAIN (a2))
6164 tree a;
6165 for (a = lookup_ident_attribute (get_attribute_name (a2),
6166 attributes);
6167 a != NULL_TREE && !attribute_value_equal (a, a2);
6168 a = lookup_ident_attribute (get_attribute_name (a2),
6169 TREE_CHAIN (a)))
6171 if (a == NULL_TREE)
6173 a1 = copy_node (a2);
6174 TREE_CHAIN (a1) = attributes;
6175 attributes = a1;
6180 return attributes;
6183 /* Given types T1 and T2, merge their attributes and return
6184 the result. */
6186 tree
6187 merge_type_attributes (tree t1, tree t2)
6189 return merge_attributes (TYPE_ATTRIBUTES (t1),
6190 TYPE_ATTRIBUTES (t2));
6193 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
6194 the result. */
6196 tree
6197 merge_decl_attributes (tree olddecl, tree newdecl)
6199 return merge_attributes (DECL_ATTRIBUTES (olddecl),
6200 DECL_ATTRIBUTES (newdecl));
6203 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
6205 /* Specialization of merge_decl_attributes for various Windows targets.
6207 This handles the following situation:
6209 __declspec (dllimport) int foo;
6210 int foo;
6212 The second instance of `foo' nullifies the dllimport. */
6214 tree
6215 merge_dllimport_decl_attributes (tree old, tree new_tree)
6217 tree a;
6218 int delete_dllimport_p = 1;
6220 /* What we need to do here is remove from `old' dllimport if it doesn't
6221 appear in `new'. dllimport behaves like extern: if a declaration is
6222 marked dllimport and a definition appears later, then the object
6223 is not dllimport'd. We also remove a `new' dllimport if the old list
6224 contains dllexport: dllexport always overrides dllimport, regardless
6225 of the order of declaration. */
6226 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
6227 delete_dllimport_p = 0;
6228 else if (DECL_DLLIMPORT_P (new_tree)
6229 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
6231 DECL_DLLIMPORT_P (new_tree) = 0;
6232 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
6233 "dllimport ignored", new_tree);
6235 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
6237 /* Warn about overriding a symbol that has already been used, e.g.:
6238 extern int __attribute__ ((dllimport)) foo;
6239 int* bar () {return &foo;}
6240 int foo;
6242 if (TREE_USED (old))
6244 warning (0, "%q+D redeclared without dllimport attribute "
6245 "after being referenced with dll linkage", new_tree);
6246 /* If we have used a variable's address with dllimport linkage,
6247 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
6248 decl may already have had TREE_CONSTANT computed.
6249 We still remove the attribute so that assembler code refers
6250 to '&foo rather than '_imp__foo'. */
6251 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
6252 DECL_DLLIMPORT_P (new_tree) = 1;
6255 /* Let an inline definition silently override the external reference,
6256 but otherwise warn about attribute inconsistency. */
6257 else if (TREE_CODE (new_tree) == VAR_DECL
6258 || !DECL_DECLARED_INLINE_P (new_tree))
6259 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
6260 "previous dllimport ignored", new_tree);
6262 else
6263 delete_dllimport_p = 0;
6265 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
6267 if (delete_dllimport_p)
6268 a = remove_attribute ("dllimport", a);
6270 return a;
6273 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
6274 struct attribute_spec.handler. */
6276 tree
6277 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
6278 bool *no_add_attrs)
6280 tree node = *pnode;
6281 bool is_dllimport;
6283 /* These attributes may apply to structure and union types being created,
6284 but otherwise should pass to the declaration involved. */
6285 if (!DECL_P (node))
6287 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
6288 | (int) ATTR_FLAG_ARRAY_NEXT))
6290 *no_add_attrs = true;
6291 return tree_cons (name, args, NULL_TREE);
6293 if (TREE_CODE (node) == RECORD_TYPE
6294 || TREE_CODE (node) == UNION_TYPE)
6296 node = TYPE_NAME (node);
6297 if (!node)
6298 return NULL_TREE;
6300 else
6302 warning (OPT_Wattributes, "%qE attribute ignored",
6303 name);
6304 *no_add_attrs = true;
6305 return NULL_TREE;
6309 if (TREE_CODE (node) != FUNCTION_DECL
6310 && TREE_CODE (node) != VAR_DECL
6311 && TREE_CODE (node) != TYPE_DECL)
6313 *no_add_attrs = true;
6314 warning (OPT_Wattributes, "%qE attribute ignored",
6315 name);
6316 return NULL_TREE;
6319 if (TREE_CODE (node) == TYPE_DECL
6320 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
6321 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
6323 *no_add_attrs = true;
6324 warning (OPT_Wattributes, "%qE attribute ignored",
6325 name);
6326 return NULL_TREE;
6329 is_dllimport = is_attribute_p ("dllimport", name);
6331 /* Report error on dllimport ambiguities seen now before they cause
6332 any damage. */
6333 if (is_dllimport)
6335 /* Honor any target-specific overrides. */
6336 if (!targetm.valid_dllimport_attribute_p (node))
6337 *no_add_attrs = true;
6339 else if (TREE_CODE (node) == FUNCTION_DECL
6340 && DECL_DECLARED_INLINE_P (node))
6342 warning (OPT_Wattributes, "inline function %q+D declared as "
6343 " dllimport: attribute ignored", node);
6344 *no_add_attrs = true;
6346 /* Like MS, treat definition of dllimported variables and
6347 non-inlined functions on declaration as syntax errors. */
6348 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
6350 error ("function %q+D definition is marked dllimport", node);
6351 *no_add_attrs = true;
6354 else if (TREE_CODE (node) == VAR_DECL)
6356 if (DECL_INITIAL (node))
6358 error ("variable %q+D definition is marked dllimport",
6359 node);
6360 *no_add_attrs = true;
6363 /* `extern' needn't be specified with dllimport.
6364 Specify `extern' now and hope for the best. Sigh. */
6365 DECL_EXTERNAL (node) = 1;
6366 /* Also, implicitly give dllimport'd variables declared within
6367 a function global scope, unless declared static. */
6368 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
6369 TREE_PUBLIC (node) = 1;
6372 if (*no_add_attrs == false)
6373 DECL_DLLIMPORT_P (node) = 1;
6375 else if (TREE_CODE (node) == FUNCTION_DECL
6376 && DECL_DECLARED_INLINE_P (node)
6377 && flag_keep_inline_dllexport)
6378 /* An exported function, even if inline, must be emitted. */
6379 DECL_EXTERNAL (node) = 0;
6381 /* Report error if symbol is not accessible at global scope. */
6382 if (!TREE_PUBLIC (node)
6383 && (TREE_CODE (node) == VAR_DECL
6384 || TREE_CODE (node) == FUNCTION_DECL))
6386 error ("external linkage required for symbol %q+D because of "
6387 "%qE attribute", node, name);
6388 *no_add_attrs = true;
6391 /* A dllexport'd entity must have default visibility so that other
6392 program units (shared libraries or the main executable) can see
6393 it. A dllimport'd entity must have default visibility so that
6394 the linker knows that undefined references within this program
6395 unit can be resolved by the dynamic linker. */
6396 if (!*no_add_attrs)
6398 if (DECL_VISIBILITY_SPECIFIED (node)
6399 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
6400 error ("%qE implies default visibility, but %qD has already "
6401 "been declared with a different visibility",
6402 name, node);
6403 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
6404 DECL_VISIBILITY_SPECIFIED (node) = 1;
6407 return NULL_TREE;
6410 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
6412 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
6413 of the various TYPE_QUAL values. */
6415 static void
6416 set_type_quals (tree type, int type_quals)
6418 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
6419 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
6420 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
6421 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
6422 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
6425 /* Returns true iff unqualified CAND and BASE are equivalent. */
6427 bool
6428 check_base_type (const_tree cand, const_tree base)
6430 return (TYPE_NAME (cand) == TYPE_NAME (base)
6431 /* Apparently this is needed for Objective-C. */
6432 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6433 /* Check alignment. */
6434 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
6435 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6436 TYPE_ATTRIBUTES (base)));
6439 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
6441 bool
6442 check_qualified_type (const_tree cand, const_tree base, int type_quals)
6444 return (TYPE_QUALS (cand) == type_quals
6445 && check_base_type (cand, base));
6448 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
6450 static bool
6451 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
6453 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
6454 && TYPE_NAME (cand) == TYPE_NAME (base)
6455 /* Apparently this is needed for Objective-C. */
6456 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6457 /* Check alignment. */
6458 && TYPE_ALIGN (cand) == align
6459 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6460 TYPE_ATTRIBUTES (base)));
6463 /* This function checks to see if TYPE matches the size one of the built-in
6464 atomic types, and returns that core atomic type. */
6466 static tree
6467 find_atomic_core_type (tree type)
6469 tree base_atomic_type;
6471 /* Only handle complete types. */
6472 if (TYPE_SIZE (type) == NULL_TREE)
6473 return NULL_TREE;
6475 HOST_WIDE_INT type_size = tree_to_uhwi (TYPE_SIZE (type));
6476 switch (type_size)
6478 case 8:
6479 base_atomic_type = atomicQI_type_node;
6480 break;
6482 case 16:
6483 base_atomic_type = atomicHI_type_node;
6484 break;
6486 case 32:
6487 base_atomic_type = atomicSI_type_node;
6488 break;
6490 case 64:
6491 base_atomic_type = atomicDI_type_node;
6492 break;
6494 case 128:
6495 base_atomic_type = atomicTI_type_node;
6496 break;
6498 default:
6499 base_atomic_type = NULL_TREE;
6502 return base_atomic_type;
6505 /* Return a version of the TYPE, qualified as indicated by the
6506 TYPE_QUALS, if one exists. If no qualified version exists yet,
6507 return NULL_TREE. */
6509 tree
6510 get_qualified_type (tree type, int type_quals)
6512 tree t;
6514 if (TYPE_QUALS (type) == type_quals)
6515 return type;
6517 /* Search the chain of variants to see if there is already one there just
6518 like the one we need to have. If so, use that existing one. We must
6519 preserve the TYPE_NAME, since there is code that depends on this. */
6520 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6521 if (check_qualified_type (t, type, type_quals))
6522 return t;
6524 return NULL_TREE;
6527 /* Like get_qualified_type, but creates the type if it does not
6528 exist. This function never returns NULL_TREE. */
6530 tree
6531 build_qualified_type (tree type, int type_quals)
6533 tree t;
6535 /* See if we already have the appropriate qualified variant. */
6536 t = get_qualified_type (type, type_quals);
6538 /* If not, build it. */
6539 if (!t)
6541 t = build_variant_type_copy (type);
6542 set_type_quals (t, type_quals);
6544 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
6546 /* See if this object can map to a basic atomic type. */
6547 tree atomic_type = find_atomic_core_type (type);
6548 if (atomic_type)
6550 /* Ensure the alignment of this type is compatible with
6551 the required alignment of the atomic type. */
6552 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
6553 TYPE_ALIGN (t) = TYPE_ALIGN (atomic_type);
6557 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6558 /* Propagate structural equality. */
6559 SET_TYPE_STRUCTURAL_EQUALITY (t);
6560 else if (TYPE_CANONICAL (type) != type)
6561 /* Build the underlying canonical type, since it is different
6562 from TYPE. */
6564 tree c = build_qualified_type (TYPE_CANONICAL (type), type_quals);
6565 TYPE_CANONICAL (t) = TYPE_CANONICAL (c);
6567 else
6568 /* T is its own canonical type. */
6569 TYPE_CANONICAL (t) = t;
6573 return t;
6576 /* Create a variant of type T with alignment ALIGN. */
6578 tree
6579 build_aligned_type (tree type, unsigned int align)
6581 tree t;
6583 if (TYPE_PACKED (type)
6584 || TYPE_ALIGN (type) == align)
6585 return type;
6587 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6588 if (check_aligned_type (t, type, align))
6589 return t;
6591 t = build_variant_type_copy (type);
6592 TYPE_ALIGN (t) = align;
6594 return t;
6597 /* Create a new distinct copy of TYPE. The new type is made its own
6598 MAIN_VARIANT. If TYPE requires structural equality checks, the
6599 resulting type requires structural equality checks; otherwise, its
6600 TYPE_CANONICAL points to itself. */
6602 tree
6603 build_distinct_type_copy (tree type)
6605 tree t = copy_node (type);
6607 TYPE_POINTER_TO (t) = 0;
6608 TYPE_REFERENCE_TO (t) = 0;
6610 /* Set the canonical type either to a new equivalence class, or
6611 propagate the need for structural equality checks. */
6612 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6613 SET_TYPE_STRUCTURAL_EQUALITY (t);
6614 else
6615 TYPE_CANONICAL (t) = t;
6617 /* Make it its own variant. */
6618 TYPE_MAIN_VARIANT (t) = t;
6619 TYPE_NEXT_VARIANT (t) = 0;
6621 /* We do not record methods in type copies nor variants
6622 so we do not need to keep them up to date when new method
6623 is inserted. */
6624 if (RECORD_OR_UNION_TYPE_P (t))
6625 TYPE_METHODS (t) = NULL_TREE;
6627 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
6628 whose TREE_TYPE is not t. This can also happen in the Ada
6629 frontend when using subtypes. */
6631 return t;
6634 /* Create a new variant of TYPE, equivalent but distinct. This is so
6635 the caller can modify it. TYPE_CANONICAL for the return type will
6636 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
6637 are considered equal by the language itself (or that both types
6638 require structural equality checks). */
6640 tree
6641 build_variant_type_copy (tree type)
6643 tree t, m = TYPE_MAIN_VARIANT (type);
6645 t = build_distinct_type_copy (type);
6647 /* Since we're building a variant, assume that it is a non-semantic
6648 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
6649 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
6651 /* Add the new type to the chain of variants of TYPE. */
6652 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
6653 TYPE_NEXT_VARIANT (m) = t;
6654 TYPE_MAIN_VARIANT (t) = m;
6656 return t;
6659 /* Return true if the from tree in both tree maps are equal. */
6662 tree_map_base_eq (const void *va, const void *vb)
6664 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6665 *const b = (const struct tree_map_base *) vb;
6666 return (a->from == b->from);
6669 /* Hash a from tree in a tree_base_map. */
6671 unsigned int
6672 tree_map_base_hash (const void *item)
6674 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6677 /* Return true if this tree map structure is marked for garbage collection
6678 purposes. We simply return true if the from tree is marked, so that this
6679 structure goes away when the from tree goes away. */
6682 tree_map_base_marked_p (const void *p)
6684 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6687 /* Hash a from tree in a tree_map. */
6689 unsigned int
6690 tree_map_hash (const void *item)
6692 return (((const struct tree_map *) item)->hash);
6695 /* Hash a from tree in a tree_decl_map. */
6697 unsigned int
6698 tree_decl_map_hash (const void *item)
6700 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6703 /* Return the initialization priority for DECL. */
6705 priority_type
6706 decl_init_priority_lookup (tree decl)
6708 symtab_node *snode = symtab_node::get (decl);
6710 if (!snode)
6711 return DEFAULT_INIT_PRIORITY;
6712 return
6713 snode->get_init_priority ();
6716 /* Return the finalization priority for DECL. */
6718 priority_type
6719 decl_fini_priority_lookup (tree decl)
6721 cgraph_node *node = cgraph_node::get (decl);
6723 if (!node)
6724 return DEFAULT_INIT_PRIORITY;
6725 return
6726 node->get_fini_priority ();
6729 /* Set the initialization priority for DECL to PRIORITY. */
6731 void
6732 decl_init_priority_insert (tree decl, priority_type priority)
6734 struct symtab_node *snode;
6736 if (priority == DEFAULT_INIT_PRIORITY)
6738 snode = symtab_node::get (decl);
6739 if (!snode)
6740 return;
6742 else if (TREE_CODE (decl) == VAR_DECL)
6743 snode = varpool_node::get_create (decl);
6744 else
6745 snode = cgraph_node::get_create (decl);
6746 snode->set_init_priority (priority);
6749 /* Set the finalization priority for DECL to PRIORITY. */
6751 void
6752 decl_fini_priority_insert (tree decl, priority_type priority)
6754 struct cgraph_node *node;
6756 if (priority == DEFAULT_INIT_PRIORITY)
6758 node = cgraph_node::get (decl);
6759 if (!node)
6760 return;
6762 else
6763 node = cgraph_node::get_create (decl);
6764 node->set_fini_priority (priority);
6767 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6769 static void
6770 print_debug_expr_statistics (void)
6772 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6773 (long) debug_expr_for_decl->size (),
6774 (long) debug_expr_for_decl->elements (),
6775 debug_expr_for_decl->collisions ());
6778 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6780 static void
6781 print_value_expr_statistics (void)
6783 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6784 (long) value_expr_for_decl->size (),
6785 (long) value_expr_for_decl->elements (),
6786 value_expr_for_decl->collisions ());
6789 /* Lookup a debug expression for FROM, and return it if we find one. */
6791 tree
6792 decl_debug_expr_lookup (tree from)
6794 struct tree_decl_map *h, in;
6795 in.base.from = from;
6797 h = debug_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6798 if (h)
6799 return h->to;
6800 return NULL_TREE;
6803 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6805 void
6806 decl_debug_expr_insert (tree from, tree to)
6808 struct tree_decl_map *h;
6810 h = ggc_alloc<tree_decl_map> ();
6811 h->base.from = from;
6812 h->to = to;
6813 *debug_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6816 /* Lookup a value expression for FROM, and return it if we find one. */
6818 tree
6819 decl_value_expr_lookup (tree from)
6821 struct tree_decl_map *h, in;
6822 in.base.from = from;
6824 h = value_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6825 if (h)
6826 return h->to;
6827 return NULL_TREE;
6830 /* Insert a mapping FROM->TO in the value expression hashtable. */
6832 void
6833 decl_value_expr_insert (tree from, tree to)
6835 struct tree_decl_map *h;
6837 h = ggc_alloc<tree_decl_map> ();
6838 h->base.from = from;
6839 h->to = to;
6840 *value_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6843 /* Lookup a vector of debug arguments for FROM, and return it if we
6844 find one. */
6846 vec<tree, va_gc> **
6847 decl_debug_args_lookup (tree from)
6849 struct tree_vec_map *h, in;
6851 if (!DECL_HAS_DEBUG_ARGS_P (from))
6852 return NULL;
6853 gcc_checking_assert (debug_args_for_decl != NULL);
6854 in.base.from = from;
6855 h = debug_args_for_decl->find_with_hash (&in, DECL_UID (from));
6856 if (h)
6857 return &h->to;
6858 return NULL;
6861 /* Insert a mapping FROM->empty vector of debug arguments in the value
6862 expression hashtable. */
6864 vec<tree, va_gc> **
6865 decl_debug_args_insert (tree from)
6867 struct tree_vec_map *h;
6868 tree_vec_map **loc;
6870 if (DECL_HAS_DEBUG_ARGS_P (from))
6871 return decl_debug_args_lookup (from);
6872 if (debug_args_for_decl == NULL)
6873 debug_args_for_decl = hash_table<tree_vec_map_cache_hasher>::create_ggc (64);
6874 h = ggc_alloc<tree_vec_map> ();
6875 h->base.from = from;
6876 h->to = NULL;
6877 loc = debug_args_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT);
6878 *loc = h;
6879 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6880 return &h->to;
6883 /* Hashing of types so that we don't make duplicates.
6884 The entry point is `type_hash_canon'. */
6886 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6887 with types in the TREE_VALUE slots), by adding the hash codes
6888 of the individual types. */
6890 static void
6891 type_hash_list (const_tree list, inchash::hash &hstate)
6893 const_tree tail;
6895 for (tail = list; tail; tail = TREE_CHAIN (tail))
6896 if (TREE_VALUE (tail) != error_mark_node)
6897 hstate.add_object (TYPE_HASH (TREE_VALUE (tail)));
6900 /* These are the Hashtable callback functions. */
6902 /* Returns true iff the types are equivalent. */
6904 bool
6905 type_cache_hasher::equal (type_hash *a, type_hash *b)
6907 /* First test the things that are the same for all types. */
6908 if (a->hash != b->hash
6909 || TREE_CODE (a->type) != TREE_CODE (b->type)
6910 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6911 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6912 TYPE_ATTRIBUTES (b->type))
6913 || (TREE_CODE (a->type) != COMPLEX_TYPE
6914 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6915 return 0;
6917 /* Be careful about comparing arrays before and after the element type
6918 has been completed; don't compare TYPE_ALIGN unless both types are
6919 complete. */
6920 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6921 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6922 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6923 return 0;
6925 switch (TREE_CODE (a->type))
6927 case VOID_TYPE:
6928 case COMPLEX_TYPE:
6929 case POINTER_TYPE:
6930 case REFERENCE_TYPE:
6931 case NULLPTR_TYPE:
6932 return 1;
6934 case VECTOR_TYPE:
6935 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6937 case ENUMERAL_TYPE:
6938 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6939 && !(TYPE_VALUES (a->type)
6940 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6941 && TYPE_VALUES (b->type)
6942 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6943 && type_list_equal (TYPE_VALUES (a->type),
6944 TYPE_VALUES (b->type))))
6945 return 0;
6947 /* ... fall through ... */
6949 case INTEGER_TYPE:
6950 case REAL_TYPE:
6951 case BOOLEAN_TYPE:
6952 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
6953 return false;
6954 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6955 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6956 TYPE_MAX_VALUE (b->type)))
6957 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6958 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6959 TYPE_MIN_VALUE (b->type))));
6961 case FIXED_POINT_TYPE:
6962 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6964 case OFFSET_TYPE:
6965 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6967 case METHOD_TYPE:
6968 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6969 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6970 || (TYPE_ARG_TYPES (a->type)
6971 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6972 && TYPE_ARG_TYPES (b->type)
6973 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6974 && type_list_equal (TYPE_ARG_TYPES (a->type),
6975 TYPE_ARG_TYPES (b->type)))))
6976 break;
6977 return 0;
6978 case ARRAY_TYPE:
6979 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6981 case RECORD_TYPE:
6982 case UNION_TYPE:
6983 case QUAL_UNION_TYPE:
6984 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6985 || (TYPE_FIELDS (a->type)
6986 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6987 && TYPE_FIELDS (b->type)
6988 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6989 && type_list_equal (TYPE_FIELDS (a->type),
6990 TYPE_FIELDS (b->type))));
6992 case FUNCTION_TYPE:
6993 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6994 || (TYPE_ARG_TYPES (a->type)
6995 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6996 && TYPE_ARG_TYPES (b->type)
6997 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6998 && type_list_equal (TYPE_ARG_TYPES (a->type),
6999 TYPE_ARG_TYPES (b->type))))
7000 break;
7001 return 0;
7003 default:
7004 return 0;
7007 if (lang_hooks.types.type_hash_eq != NULL)
7008 return lang_hooks.types.type_hash_eq (a->type, b->type);
7010 return 1;
7013 /* Given TYPE, and HASHCODE its hash code, return the canonical
7014 object for an identical type if one already exists.
7015 Otherwise, return TYPE, and record it as the canonical object.
7017 To use this function, first create a type of the sort you want.
7018 Then compute its hash code from the fields of the type that
7019 make it different from other similar types.
7020 Then call this function and use the value. */
7022 tree
7023 type_hash_canon (unsigned int hashcode, tree type)
7025 type_hash in;
7026 type_hash **loc;
7028 /* The hash table only contains main variants, so ensure that's what we're
7029 being passed. */
7030 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
7032 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
7033 must call that routine before comparing TYPE_ALIGNs. */
7034 layout_type (type);
7036 in.hash = hashcode;
7037 in.type = type;
7039 loc = type_hash_table->find_slot_with_hash (&in, hashcode, INSERT);
7040 if (*loc)
7042 tree t1 = ((type_hash *) *loc)->type;
7043 gcc_assert (TYPE_MAIN_VARIANT (t1) == t1);
7044 if (GATHER_STATISTICS)
7046 tree_code_counts[(int) TREE_CODE (type)]--;
7047 tree_node_counts[(int) t_kind]--;
7048 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
7050 return t1;
7052 else
7054 struct type_hash *h;
7056 h = ggc_alloc<type_hash> ();
7057 h->hash = hashcode;
7058 h->type = type;
7059 *loc = h;
7061 return type;
7065 static void
7066 print_type_hash_statistics (void)
7068 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
7069 (long) type_hash_table->size (),
7070 (long) type_hash_table->elements (),
7071 type_hash_table->collisions ());
7074 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
7075 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
7076 by adding the hash codes of the individual attributes. */
7078 static void
7079 attribute_hash_list (const_tree list, inchash::hash &hstate)
7081 const_tree tail;
7083 for (tail = list; tail; tail = TREE_CHAIN (tail))
7084 /* ??? Do we want to add in TREE_VALUE too? */
7085 hstate.add_object (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)));
7088 /* Given two lists of attributes, return true if list l2 is
7089 equivalent to l1. */
7092 attribute_list_equal (const_tree l1, const_tree l2)
7094 if (l1 == l2)
7095 return 1;
7097 return attribute_list_contained (l1, l2)
7098 && attribute_list_contained (l2, l1);
7101 /* Given two lists of attributes, return true if list L2 is
7102 completely contained within L1. */
7103 /* ??? This would be faster if attribute names were stored in a canonicalized
7104 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
7105 must be used to show these elements are equivalent (which they are). */
7106 /* ??? It's not clear that attributes with arguments will always be handled
7107 correctly. */
7110 attribute_list_contained (const_tree l1, const_tree l2)
7112 const_tree t1, t2;
7114 /* First check the obvious, maybe the lists are identical. */
7115 if (l1 == l2)
7116 return 1;
7118 /* Maybe the lists are similar. */
7119 for (t1 = l1, t2 = l2;
7120 t1 != 0 && t2 != 0
7121 && get_attribute_name (t1) == get_attribute_name (t2)
7122 && TREE_VALUE (t1) == TREE_VALUE (t2);
7123 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7126 /* Maybe the lists are equal. */
7127 if (t1 == 0 && t2 == 0)
7128 return 1;
7130 for (; t2 != 0; t2 = TREE_CHAIN (t2))
7132 const_tree attr;
7133 /* This CONST_CAST is okay because lookup_attribute does not
7134 modify its argument and the return value is assigned to a
7135 const_tree. */
7136 for (attr = lookup_ident_attribute (get_attribute_name (t2),
7137 CONST_CAST_TREE (l1));
7138 attr != NULL_TREE && !attribute_value_equal (t2, attr);
7139 attr = lookup_ident_attribute (get_attribute_name (t2),
7140 TREE_CHAIN (attr)))
7143 if (attr == NULL_TREE)
7144 return 0;
7147 return 1;
7150 /* Given two lists of types
7151 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
7152 return 1 if the lists contain the same types in the same order.
7153 Also, the TREE_PURPOSEs must match. */
7156 type_list_equal (const_tree l1, const_tree l2)
7158 const_tree t1, t2;
7160 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7161 if (TREE_VALUE (t1) != TREE_VALUE (t2)
7162 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
7163 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
7164 && (TREE_TYPE (TREE_PURPOSE (t1))
7165 == TREE_TYPE (TREE_PURPOSE (t2))))))
7166 return 0;
7168 return t1 == t2;
7171 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
7172 given by TYPE. If the argument list accepts variable arguments,
7173 then this function counts only the ordinary arguments. */
7176 type_num_arguments (const_tree type)
7178 int i = 0;
7179 tree t;
7181 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
7182 /* If the function does not take a variable number of arguments,
7183 the last element in the list will have type `void'. */
7184 if (VOID_TYPE_P (TREE_VALUE (t)))
7185 break;
7186 else
7187 ++i;
7189 return i;
7192 /* Nonzero if integer constants T1 and T2
7193 represent the same constant value. */
7196 tree_int_cst_equal (const_tree t1, const_tree t2)
7198 if (t1 == t2)
7199 return 1;
7201 if (t1 == 0 || t2 == 0)
7202 return 0;
7204 if (TREE_CODE (t1) == INTEGER_CST
7205 && TREE_CODE (t2) == INTEGER_CST
7206 && wi::to_widest (t1) == wi::to_widest (t2))
7207 return 1;
7209 return 0;
7212 /* Return true if T is an INTEGER_CST whose numerical value (extended
7213 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
7215 bool
7216 tree_fits_shwi_p (const_tree t)
7218 return (t != NULL_TREE
7219 && TREE_CODE (t) == INTEGER_CST
7220 && wi::fits_shwi_p (wi::to_widest (t)));
7223 /* Return true if T is an INTEGER_CST whose numerical value (extended
7224 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
7226 bool
7227 tree_fits_uhwi_p (const_tree t)
7229 return (t != NULL_TREE
7230 && TREE_CODE (t) == INTEGER_CST
7231 && wi::fits_uhwi_p (wi::to_widest (t)));
7234 /* T is an INTEGER_CST whose numerical value (extended according to
7235 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
7236 HOST_WIDE_INT. */
7238 HOST_WIDE_INT
7239 tree_to_shwi (const_tree t)
7241 gcc_assert (tree_fits_shwi_p (t));
7242 return TREE_INT_CST_LOW (t);
7245 /* T is an INTEGER_CST whose numerical value (extended according to
7246 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
7247 HOST_WIDE_INT. */
7249 unsigned HOST_WIDE_INT
7250 tree_to_uhwi (const_tree t)
7252 gcc_assert (tree_fits_uhwi_p (t));
7253 return TREE_INT_CST_LOW (t);
7256 /* Return the most significant (sign) bit of T. */
7259 tree_int_cst_sign_bit (const_tree t)
7261 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
7263 return wi::extract_uhwi (t, bitno, 1);
7266 /* Return an indication of the sign of the integer constant T.
7267 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
7268 Note that -1 will never be returned if T's type is unsigned. */
7271 tree_int_cst_sgn (const_tree t)
7273 if (wi::eq_p (t, 0))
7274 return 0;
7275 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
7276 return 1;
7277 else if (wi::neg_p (t))
7278 return -1;
7279 else
7280 return 1;
7283 /* Return the minimum number of bits needed to represent VALUE in a
7284 signed or unsigned type, UNSIGNEDP says which. */
7286 unsigned int
7287 tree_int_cst_min_precision (tree value, signop sgn)
7289 /* If the value is negative, compute its negative minus 1. The latter
7290 adjustment is because the absolute value of the largest negative value
7291 is one larger than the largest positive value. This is equivalent to
7292 a bit-wise negation, so use that operation instead. */
7294 if (tree_int_cst_sgn (value) < 0)
7295 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
7297 /* Return the number of bits needed, taking into account the fact
7298 that we need one more bit for a signed than unsigned type.
7299 If value is 0 or -1, the minimum precision is 1 no matter
7300 whether unsignedp is true or false. */
7302 if (integer_zerop (value))
7303 return 1;
7304 else
7305 return tree_floor_log2 (value) + 1 + (sgn == SIGNED ? 1 : 0) ;
7308 /* Return truthvalue of whether T1 is the same tree structure as T2.
7309 Return 1 if they are the same.
7310 Return 0 if they are understandably different.
7311 Return -1 if either contains tree structure not understood by
7312 this function. */
7315 simple_cst_equal (const_tree t1, const_tree t2)
7317 enum tree_code code1, code2;
7318 int cmp;
7319 int i;
7321 if (t1 == t2)
7322 return 1;
7323 if (t1 == 0 || t2 == 0)
7324 return 0;
7326 code1 = TREE_CODE (t1);
7327 code2 = TREE_CODE (t2);
7329 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
7331 if (CONVERT_EXPR_CODE_P (code2)
7332 || code2 == NON_LVALUE_EXPR)
7333 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7334 else
7335 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
7338 else if (CONVERT_EXPR_CODE_P (code2)
7339 || code2 == NON_LVALUE_EXPR)
7340 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
7342 if (code1 != code2)
7343 return 0;
7345 switch (code1)
7347 case INTEGER_CST:
7348 return wi::to_widest (t1) == wi::to_widest (t2);
7350 case REAL_CST:
7351 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
7353 case FIXED_CST:
7354 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
7356 case STRING_CST:
7357 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
7358 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
7359 TREE_STRING_LENGTH (t1)));
7361 case CONSTRUCTOR:
7363 unsigned HOST_WIDE_INT idx;
7364 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
7365 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
7367 if (vec_safe_length (v1) != vec_safe_length (v2))
7368 return false;
7370 for (idx = 0; idx < vec_safe_length (v1); ++idx)
7371 /* ??? Should we handle also fields here? */
7372 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
7373 return false;
7374 return true;
7377 case SAVE_EXPR:
7378 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7380 case CALL_EXPR:
7381 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
7382 if (cmp <= 0)
7383 return cmp;
7384 if (call_expr_nargs (t1) != call_expr_nargs (t2))
7385 return 0;
7387 const_tree arg1, arg2;
7388 const_call_expr_arg_iterator iter1, iter2;
7389 for (arg1 = first_const_call_expr_arg (t1, &iter1),
7390 arg2 = first_const_call_expr_arg (t2, &iter2);
7391 arg1 && arg2;
7392 arg1 = next_const_call_expr_arg (&iter1),
7393 arg2 = next_const_call_expr_arg (&iter2))
7395 cmp = simple_cst_equal (arg1, arg2);
7396 if (cmp <= 0)
7397 return cmp;
7399 return arg1 == arg2;
7402 case TARGET_EXPR:
7403 /* Special case: if either target is an unallocated VAR_DECL,
7404 it means that it's going to be unified with whatever the
7405 TARGET_EXPR is really supposed to initialize, so treat it
7406 as being equivalent to anything. */
7407 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
7408 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
7409 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
7410 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
7411 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
7412 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
7413 cmp = 1;
7414 else
7415 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7417 if (cmp <= 0)
7418 return cmp;
7420 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
7422 case WITH_CLEANUP_EXPR:
7423 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7424 if (cmp <= 0)
7425 return cmp;
7427 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
7429 case COMPONENT_REF:
7430 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
7431 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7433 return 0;
7435 case VAR_DECL:
7436 case PARM_DECL:
7437 case CONST_DECL:
7438 case FUNCTION_DECL:
7439 return 0;
7441 default:
7442 break;
7445 /* This general rule works for most tree codes. All exceptions should be
7446 handled above. If this is a language-specific tree code, we can't
7447 trust what might be in the operand, so say we don't know
7448 the situation. */
7449 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
7450 return -1;
7452 switch (TREE_CODE_CLASS (code1))
7454 case tcc_unary:
7455 case tcc_binary:
7456 case tcc_comparison:
7457 case tcc_expression:
7458 case tcc_reference:
7459 case tcc_statement:
7460 cmp = 1;
7461 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
7463 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
7464 if (cmp <= 0)
7465 return cmp;
7468 return cmp;
7470 default:
7471 return -1;
7475 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
7476 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
7477 than U, respectively. */
7480 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
7482 if (tree_int_cst_sgn (t) < 0)
7483 return -1;
7484 else if (!tree_fits_uhwi_p (t))
7485 return 1;
7486 else if (TREE_INT_CST_LOW (t) == u)
7487 return 0;
7488 else if (TREE_INT_CST_LOW (t) < u)
7489 return -1;
7490 else
7491 return 1;
7494 /* Return true if SIZE represents a constant size that is in bounds of
7495 what the middle-end and the backend accepts (covering not more than
7496 half of the address-space). */
7498 bool
7499 valid_constant_size_p (const_tree size)
7501 if (! tree_fits_uhwi_p (size)
7502 || TREE_OVERFLOW (size)
7503 || tree_int_cst_sign_bit (size) != 0)
7504 return false;
7505 return true;
7508 /* Return the precision of the type, or for a complex or vector type the
7509 precision of the type of its elements. */
7511 unsigned int
7512 element_precision (const_tree type)
7514 enum tree_code code = TREE_CODE (type);
7515 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
7516 type = TREE_TYPE (type);
7518 return TYPE_PRECISION (type);
7521 /* Return true if CODE represents an associative tree code. Otherwise
7522 return false. */
7523 bool
7524 associative_tree_code (enum tree_code code)
7526 switch (code)
7528 case BIT_IOR_EXPR:
7529 case BIT_AND_EXPR:
7530 case BIT_XOR_EXPR:
7531 case PLUS_EXPR:
7532 case MULT_EXPR:
7533 case MIN_EXPR:
7534 case MAX_EXPR:
7535 return true;
7537 default:
7538 break;
7540 return false;
7543 /* Return true if CODE represents a commutative tree code. Otherwise
7544 return false. */
7545 bool
7546 commutative_tree_code (enum tree_code code)
7548 switch (code)
7550 case PLUS_EXPR:
7551 case MULT_EXPR:
7552 case MULT_HIGHPART_EXPR:
7553 case MIN_EXPR:
7554 case MAX_EXPR:
7555 case BIT_IOR_EXPR:
7556 case BIT_XOR_EXPR:
7557 case BIT_AND_EXPR:
7558 case NE_EXPR:
7559 case EQ_EXPR:
7560 case UNORDERED_EXPR:
7561 case ORDERED_EXPR:
7562 case UNEQ_EXPR:
7563 case LTGT_EXPR:
7564 case TRUTH_AND_EXPR:
7565 case TRUTH_XOR_EXPR:
7566 case TRUTH_OR_EXPR:
7567 case WIDEN_MULT_EXPR:
7568 case VEC_WIDEN_MULT_HI_EXPR:
7569 case VEC_WIDEN_MULT_LO_EXPR:
7570 case VEC_WIDEN_MULT_EVEN_EXPR:
7571 case VEC_WIDEN_MULT_ODD_EXPR:
7572 return true;
7574 default:
7575 break;
7577 return false;
7580 /* Return true if CODE represents a ternary tree code for which the
7581 first two operands are commutative. Otherwise return false. */
7582 bool
7583 commutative_ternary_tree_code (enum tree_code code)
7585 switch (code)
7587 case WIDEN_MULT_PLUS_EXPR:
7588 case WIDEN_MULT_MINUS_EXPR:
7589 case DOT_PROD_EXPR:
7590 case FMA_EXPR:
7591 return true;
7593 default:
7594 break;
7596 return false;
7599 namespace inchash
7602 /* Generate a hash value for an expression. This can be used iteratively
7603 by passing a previous result as the HSTATE argument.
7605 This function is intended to produce the same hash for expressions which
7606 would compare equal using operand_equal_p. */
7607 void
7608 add_expr (const_tree t, inchash::hash &hstate)
7610 int i;
7611 enum tree_code code;
7612 enum tree_code_class tclass;
7614 if (t == NULL_TREE)
7616 hstate.merge_hash (0);
7617 return;
7620 code = TREE_CODE (t);
7622 switch (code)
7624 /* Alas, constants aren't shared, so we can't rely on pointer
7625 identity. */
7626 case VOID_CST:
7627 hstate.merge_hash (0);
7628 return;
7629 case INTEGER_CST:
7630 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
7631 hstate.add_wide_int (TREE_INT_CST_ELT (t, i));
7632 return;
7633 case REAL_CST:
7635 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7636 hstate.merge_hash (val2);
7637 return;
7639 case FIXED_CST:
7641 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7642 hstate.merge_hash (val2);
7643 return;
7645 case STRING_CST:
7646 hstate.add ((const void *) TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
7647 return;
7648 case COMPLEX_CST:
7649 inchash::add_expr (TREE_REALPART (t), hstate);
7650 inchash::add_expr (TREE_IMAGPART (t), hstate);
7651 return;
7652 case VECTOR_CST:
7654 unsigned i;
7655 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7656 inchash::add_expr (VECTOR_CST_ELT (t, i), hstate);
7657 return;
7659 case SSA_NAME:
7660 /* We can just compare by pointer. */
7661 hstate.add_wide_int (SSA_NAME_VERSION (t));
7662 return;
7663 case PLACEHOLDER_EXPR:
7664 /* The node itself doesn't matter. */
7665 return;
7666 case TREE_LIST:
7667 /* A list of expressions, for a CALL_EXPR or as the elements of a
7668 VECTOR_CST. */
7669 for (; t; t = TREE_CHAIN (t))
7670 inchash::add_expr (TREE_VALUE (t), hstate);
7671 return;
7672 case CONSTRUCTOR:
7674 unsigned HOST_WIDE_INT idx;
7675 tree field, value;
7676 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7678 inchash::add_expr (field, hstate);
7679 inchash::add_expr (value, hstate);
7681 return;
7683 case FUNCTION_DECL:
7684 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7685 Otherwise nodes that compare equal according to operand_equal_p might
7686 get different hash codes. However, don't do this for machine specific
7687 or front end builtins, since the function code is overloaded in those
7688 cases. */
7689 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7690 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7692 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7693 code = TREE_CODE (t);
7695 /* FALL THROUGH */
7696 default:
7697 tclass = TREE_CODE_CLASS (code);
7699 if (tclass == tcc_declaration)
7701 /* DECL's have a unique ID */
7702 hstate.add_wide_int (DECL_UID (t));
7704 else
7706 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7708 hstate.add_object (code);
7710 /* Don't hash the type, that can lead to having nodes which
7711 compare equal according to operand_equal_p, but which
7712 have different hash codes. */
7713 if (CONVERT_EXPR_CODE_P (code)
7714 || code == NON_LVALUE_EXPR)
7716 /* Make sure to include signness in the hash computation. */
7717 hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
7718 inchash::add_expr (TREE_OPERAND (t, 0), hstate);
7721 else if (commutative_tree_code (code))
7723 /* It's a commutative expression. We want to hash it the same
7724 however it appears. We do this by first hashing both operands
7725 and then rehashing based on the order of their independent
7726 hashes. */
7727 inchash::hash one, two;
7728 inchash::add_expr (TREE_OPERAND (t, 0), one);
7729 inchash::add_expr (TREE_OPERAND (t, 1), two);
7730 hstate.add_commutative (one, two);
7732 else
7733 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7734 inchash::add_expr (TREE_OPERAND (t, i), hstate);
7736 return;
7742 /* Constructors for pointer, array and function types.
7743 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7744 constructed by language-dependent code, not here.) */
7746 /* Construct, lay out and return the type of pointers to TO_TYPE with
7747 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7748 reference all of memory. If such a type has already been
7749 constructed, reuse it. */
7751 tree
7752 build_pointer_type_for_mode (tree to_type, machine_mode mode,
7753 bool can_alias_all)
7755 tree t;
7756 bool could_alias = can_alias_all;
7758 if (to_type == error_mark_node)
7759 return error_mark_node;
7761 /* If the pointed-to type has the may_alias attribute set, force
7762 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7763 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7764 can_alias_all = true;
7766 /* In some cases, languages will have things that aren't a POINTER_TYPE
7767 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7768 In that case, return that type without regard to the rest of our
7769 operands.
7771 ??? This is a kludge, but consistent with the way this function has
7772 always operated and there doesn't seem to be a good way to avoid this
7773 at the moment. */
7774 if (TYPE_POINTER_TO (to_type) != 0
7775 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7776 return TYPE_POINTER_TO (to_type);
7778 /* First, if we already have a type for pointers to TO_TYPE and it's
7779 the proper mode, use it. */
7780 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7781 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7782 return t;
7784 t = make_node (POINTER_TYPE);
7786 TREE_TYPE (t) = to_type;
7787 SET_TYPE_MODE (t, mode);
7788 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7789 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7790 TYPE_POINTER_TO (to_type) = t;
7792 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7793 SET_TYPE_STRUCTURAL_EQUALITY (t);
7794 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
7795 TYPE_CANONICAL (t)
7796 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7797 mode, false);
7799 /* Lay out the type. This function has many callers that are concerned
7800 with expression-construction, and this simplifies them all. */
7801 layout_type (t);
7803 return t;
7806 /* By default build pointers in ptr_mode. */
7808 tree
7809 build_pointer_type (tree to_type)
7811 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7812 : TYPE_ADDR_SPACE (to_type);
7813 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7814 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7817 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7819 tree
7820 build_reference_type_for_mode (tree to_type, machine_mode mode,
7821 bool can_alias_all)
7823 tree t;
7824 bool could_alias = can_alias_all;
7826 if (to_type == error_mark_node)
7827 return error_mark_node;
7829 /* If the pointed-to type has the may_alias attribute set, force
7830 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7831 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7832 can_alias_all = true;
7834 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7835 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7836 In that case, return that type without regard to the rest of our
7837 operands.
7839 ??? This is a kludge, but consistent with the way this function has
7840 always operated and there doesn't seem to be a good way to avoid this
7841 at the moment. */
7842 if (TYPE_REFERENCE_TO (to_type) != 0
7843 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7844 return TYPE_REFERENCE_TO (to_type);
7846 /* First, if we already have a type for pointers to TO_TYPE and it's
7847 the proper mode, use it. */
7848 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7849 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7850 return t;
7852 t = make_node (REFERENCE_TYPE);
7854 TREE_TYPE (t) = to_type;
7855 SET_TYPE_MODE (t, mode);
7856 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7857 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7858 TYPE_REFERENCE_TO (to_type) = t;
7860 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7861 SET_TYPE_STRUCTURAL_EQUALITY (t);
7862 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
7863 TYPE_CANONICAL (t)
7864 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7865 mode, false);
7867 layout_type (t);
7869 return t;
7873 /* Build the node for the type of references-to-TO_TYPE by default
7874 in ptr_mode. */
7876 tree
7877 build_reference_type (tree to_type)
7879 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7880 : TYPE_ADDR_SPACE (to_type);
7881 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7882 return build_reference_type_for_mode (to_type, pointer_mode, false);
7885 #define MAX_INT_CACHED_PREC \
7886 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7887 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7889 /* Builds a signed or unsigned integer type of precision PRECISION.
7890 Used for C bitfields whose precision does not match that of
7891 built-in target types. */
7892 tree
7893 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7894 int unsignedp)
7896 tree itype, ret;
7898 if (unsignedp)
7899 unsignedp = MAX_INT_CACHED_PREC + 1;
7901 if (precision <= MAX_INT_CACHED_PREC)
7903 itype = nonstandard_integer_type_cache[precision + unsignedp];
7904 if (itype)
7905 return itype;
7908 itype = make_node (INTEGER_TYPE);
7909 TYPE_PRECISION (itype) = precision;
7911 if (unsignedp)
7912 fixup_unsigned_type (itype);
7913 else
7914 fixup_signed_type (itype);
7916 ret = itype;
7917 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (itype)))
7918 ret = type_hash_canon (tree_to_uhwi (TYPE_MAX_VALUE (itype)), itype);
7919 if (precision <= MAX_INT_CACHED_PREC)
7920 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7922 return ret;
7925 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7926 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7927 is true, reuse such a type that has already been constructed. */
7929 static tree
7930 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7932 tree itype = make_node (INTEGER_TYPE);
7933 inchash::hash hstate;
7935 TREE_TYPE (itype) = type;
7937 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7938 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7940 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7941 SET_TYPE_MODE (itype, TYPE_MODE (type));
7942 TYPE_SIZE (itype) = TYPE_SIZE (type);
7943 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7944 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7945 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7947 if (!shared)
7948 return itype;
7950 if ((TYPE_MIN_VALUE (itype)
7951 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7952 || (TYPE_MAX_VALUE (itype)
7953 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7955 /* Since we cannot reliably merge this type, we need to compare it using
7956 structural equality checks. */
7957 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7958 return itype;
7961 inchash::add_expr (TYPE_MIN_VALUE (itype), hstate);
7962 inchash::add_expr (TYPE_MAX_VALUE (itype), hstate);
7963 hstate.merge_hash (TYPE_HASH (type));
7964 itype = type_hash_canon (hstate.end (), itype);
7966 return itype;
7969 /* Wrapper around build_range_type_1 with SHARED set to true. */
7971 tree
7972 build_range_type (tree type, tree lowval, tree highval)
7974 return build_range_type_1 (type, lowval, highval, true);
7977 /* Wrapper around build_range_type_1 with SHARED set to false. */
7979 tree
7980 build_nonshared_range_type (tree type, tree lowval, tree highval)
7982 return build_range_type_1 (type, lowval, highval, false);
7985 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7986 MAXVAL should be the maximum value in the domain
7987 (one less than the length of the array).
7989 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7990 We don't enforce this limit, that is up to caller (e.g. language front end).
7991 The limit exists because the result is a signed type and we don't handle
7992 sizes that use more than one HOST_WIDE_INT. */
7994 tree
7995 build_index_type (tree maxval)
7997 return build_range_type (sizetype, size_zero_node, maxval);
8000 /* Return true if the debug information for TYPE, a subtype, should be emitted
8001 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
8002 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
8003 debug info and doesn't reflect the source code. */
8005 bool
8006 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
8008 tree base_type = TREE_TYPE (type), low, high;
8010 /* Subrange types have a base type which is an integral type. */
8011 if (!INTEGRAL_TYPE_P (base_type))
8012 return false;
8014 /* Get the real bounds of the subtype. */
8015 if (lang_hooks.types.get_subrange_bounds)
8016 lang_hooks.types.get_subrange_bounds (type, &low, &high);
8017 else
8019 low = TYPE_MIN_VALUE (type);
8020 high = TYPE_MAX_VALUE (type);
8023 /* If the type and its base type have the same representation and the same
8024 name, then the type is not a subrange but a copy of the base type. */
8025 if ((TREE_CODE (base_type) == INTEGER_TYPE
8026 || TREE_CODE (base_type) == BOOLEAN_TYPE)
8027 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
8028 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
8029 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type))
8030 && TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (base_type))
8031 return false;
8033 if (lowval)
8034 *lowval = low;
8035 if (highval)
8036 *highval = high;
8037 return true;
8040 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
8041 and number of elements specified by the range of values of INDEX_TYPE.
8042 If SHARED is true, reuse such a type that has already been constructed. */
8044 static tree
8045 build_array_type_1 (tree elt_type, tree index_type, bool shared)
8047 tree t;
8049 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
8051 error ("arrays of functions are not meaningful");
8052 elt_type = integer_type_node;
8055 t = make_node (ARRAY_TYPE);
8056 TREE_TYPE (t) = elt_type;
8057 TYPE_DOMAIN (t) = index_type;
8058 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
8059 layout_type (t);
8061 /* If the element type is incomplete at this point we get marked for
8062 structural equality. Do not record these types in the canonical
8063 type hashtable. */
8064 if (TYPE_STRUCTURAL_EQUALITY_P (t))
8065 return t;
8067 if (shared)
8069 inchash::hash hstate;
8070 hstate.add_object (TYPE_HASH (elt_type));
8071 if (index_type)
8072 hstate.add_object (TYPE_HASH (index_type));
8073 t = type_hash_canon (hstate.end (), t);
8076 if (TYPE_CANONICAL (t) == t)
8078 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
8079 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
8080 SET_TYPE_STRUCTURAL_EQUALITY (t);
8081 else if (TYPE_CANONICAL (elt_type) != elt_type
8082 || (index_type && TYPE_CANONICAL (index_type) != index_type))
8083 TYPE_CANONICAL (t)
8084 = build_array_type_1 (TYPE_CANONICAL (elt_type),
8085 index_type
8086 ? TYPE_CANONICAL (index_type) : NULL_TREE,
8087 shared);
8090 return t;
8093 /* Wrapper around build_array_type_1 with SHARED set to true. */
8095 tree
8096 build_array_type (tree elt_type, tree index_type)
8098 return build_array_type_1 (elt_type, index_type, true);
8101 /* Wrapper around build_array_type_1 with SHARED set to false. */
8103 tree
8104 build_nonshared_array_type (tree elt_type, tree index_type)
8106 return build_array_type_1 (elt_type, index_type, false);
8109 /* Return a representation of ELT_TYPE[NELTS], using indices of type
8110 sizetype. */
8112 tree
8113 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
8115 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
8118 /* Recursively examines the array elements of TYPE, until a non-array
8119 element type is found. */
8121 tree
8122 strip_array_types (tree type)
8124 while (TREE_CODE (type) == ARRAY_TYPE)
8125 type = TREE_TYPE (type);
8127 return type;
8130 /* Computes the canonical argument types from the argument type list
8131 ARGTYPES.
8133 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
8134 on entry to this function, or if any of the ARGTYPES are
8135 structural.
8137 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
8138 true on entry to this function, or if any of the ARGTYPES are
8139 non-canonical.
8141 Returns a canonical argument list, which may be ARGTYPES when the
8142 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
8143 true) or would not differ from ARGTYPES. */
8145 static tree
8146 maybe_canonicalize_argtypes (tree argtypes,
8147 bool *any_structural_p,
8148 bool *any_noncanonical_p)
8150 tree arg;
8151 bool any_noncanonical_argtypes_p = false;
8153 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
8155 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
8156 /* Fail gracefully by stating that the type is structural. */
8157 *any_structural_p = true;
8158 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
8159 *any_structural_p = true;
8160 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
8161 || TREE_PURPOSE (arg))
8162 /* If the argument has a default argument, we consider it
8163 non-canonical even though the type itself is canonical.
8164 That way, different variants of function and method types
8165 with default arguments will all point to the variant with
8166 no defaults as their canonical type. */
8167 any_noncanonical_argtypes_p = true;
8170 if (*any_structural_p)
8171 return argtypes;
8173 if (any_noncanonical_argtypes_p)
8175 /* Build the canonical list of argument types. */
8176 tree canon_argtypes = NULL_TREE;
8177 bool is_void = false;
8179 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
8181 if (arg == void_list_node)
8182 is_void = true;
8183 else
8184 canon_argtypes = tree_cons (NULL_TREE,
8185 TYPE_CANONICAL (TREE_VALUE (arg)),
8186 canon_argtypes);
8189 canon_argtypes = nreverse (canon_argtypes);
8190 if (is_void)
8191 canon_argtypes = chainon (canon_argtypes, void_list_node);
8193 /* There is a non-canonical type. */
8194 *any_noncanonical_p = true;
8195 return canon_argtypes;
8198 /* The canonical argument types are the same as ARGTYPES. */
8199 return argtypes;
8202 /* Construct, lay out and return
8203 the type of functions returning type VALUE_TYPE
8204 given arguments of types ARG_TYPES.
8205 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
8206 are data type nodes for the arguments of the function.
8207 If such a type has already been constructed, reuse it. */
8209 tree
8210 build_function_type (tree value_type, tree arg_types)
8212 tree t;
8213 inchash::hash hstate;
8214 bool any_structural_p, any_noncanonical_p;
8215 tree canon_argtypes;
8217 if (TREE_CODE (value_type) == FUNCTION_TYPE)
8219 error ("function return type cannot be function");
8220 value_type = integer_type_node;
8223 /* Make a node of the sort we want. */
8224 t = make_node (FUNCTION_TYPE);
8225 TREE_TYPE (t) = value_type;
8226 TYPE_ARG_TYPES (t) = arg_types;
8228 /* If we already have such a type, use the old one. */
8229 hstate.add_object (TYPE_HASH (value_type));
8230 type_hash_list (arg_types, hstate);
8231 t = type_hash_canon (hstate.end (), t);
8233 /* Set up the canonical type. */
8234 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
8235 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
8236 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
8237 &any_structural_p,
8238 &any_noncanonical_p);
8239 if (any_structural_p)
8240 SET_TYPE_STRUCTURAL_EQUALITY (t);
8241 else if (any_noncanonical_p)
8242 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
8243 canon_argtypes);
8245 if (!COMPLETE_TYPE_P (t))
8246 layout_type (t);
8247 return t;
8250 /* Build a function type. The RETURN_TYPE is the type returned by the
8251 function. If VAARGS is set, no void_type_node is appended to the
8252 the list. ARGP must be always be terminated be a NULL_TREE. */
8254 static tree
8255 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
8257 tree t, args, last;
8259 t = va_arg (argp, tree);
8260 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
8261 args = tree_cons (NULL_TREE, t, args);
8263 if (vaargs)
8265 last = args;
8266 if (args != NULL_TREE)
8267 args = nreverse (args);
8268 gcc_assert (last != void_list_node);
8270 else if (args == NULL_TREE)
8271 args = void_list_node;
8272 else
8274 last = args;
8275 args = nreverse (args);
8276 TREE_CHAIN (last) = void_list_node;
8278 args = build_function_type (return_type, args);
8280 return args;
8283 /* Build a function type. The RETURN_TYPE is the type returned by the
8284 function. If additional arguments are provided, they are
8285 additional argument types. The list of argument types must always
8286 be terminated by NULL_TREE. */
8288 tree
8289 build_function_type_list (tree return_type, ...)
8291 tree args;
8292 va_list p;
8294 va_start (p, return_type);
8295 args = build_function_type_list_1 (false, return_type, p);
8296 va_end (p);
8297 return args;
8300 /* Build a variable argument function type. The RETURN_TYPE is the
8301 type returned by the function. If additional arguments are provided,
8302 they are additional argument types. The list of argument types must
8303 always be terminated by NULL_TREE. */
8305 tree
8306 build_varargs_function_type_list (tree return_type, ...)
8308 tree args;
8309 va_list p;
8311 va_start (p, return_type);
8312 args = build_function_type_list_1 (true, return_type, p);
8313 va_end (p);
8315 return args;
8318 /* Build a function type. RETURN_TYPE is the type returned by the
8319 function; VAARGS indicates whether the function takes varargs. The
8320 function takes N named arguments, the types of which are provided in
8321 ARG_TYPES. */
8323 static tree
8324 build_function_type_array_1 (bool vaargs, tree return_type, int n,
8325 tree *arg_types)
8327 int i;
8328 tree t = vaargs ? NULL_TREE : void_list_node;
8330 for (i = n - 1; i >= 0; i--)
8331 t = tree_cons (NULL_TREE, arg_types[i], t);
8333 return build_function_type (return_type, t);
8336 /* Build a function type. RETURN_TYPE is the type returned by the
8337 function. The function takes N named arguments, the types of which
8338 are provided in ARG_TYPES. */
8340 tree
8341 build_function_type_array (tree return_type, int n, tree *arg_types)
8343 return build_function_type_array_1 (false, return_type, n, arg_types);
8346 /* Build a variable argument function type. RETURN_TYPE is the type
8347 returned by the function. The function takes N named arguments, the
8348 types of which are provided in ARG_TYPES. */
8350 tree
8351 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8353 return build_function_type_array_1 (true, return_type, n, arg_types);
8356 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8357 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8358 for the method. An implicit additional parameter (of type
8359 pointer-to-BASETYPE) is added to the ARGTYPES. */
8361 tree
8362 build_method_type_directly (tree basetype,
8363 tree rettype,
8364 tree argtypes)
8366 tree t;
8367 tree ptype;
8368 inchash::hash hstate;
8369 bool any_structural_p, any_noncanonical_p;
8370 tree canon_argtypes;
8372 /* Make a node of the sort we want. */
8373 t = make_node (METHOD_TYPE);
8375 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8376 TREE_TYPE (t) = rettype;
8377 ptype = build_pointer_type (basetype);
8379 /* The actual arglist for this function includes a "hidden" argument
8380 which is "this". Put it into the list of argument types. */
8381 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8382 TYPE_ARG_TYPES (t) = argtypes;
8384 /* If we already have such a type, use the old one. */
8385 hstate.add_object (TYPE_HASH (basetype));
8386 hstate.add_object (TYPE_HASH (rettype));
8387 type_hash_list (argtypes, hstate);
8388 t = type_hash_canon (hstate.end (), t);
8390 /* Set up the canonical type. */
8391 any_structural_p
8392 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8393 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8394 any_noncanonical_p
8395 = (TYPE_CANONICAL (basetype) != basetype
8396 || TYPE_CANONICAL (rettype) != rettype);
8397 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8398 &any_structural_p,
8399 &any_noncanonical_p);
8400 if (any_structural_p)
8401 SET_TYPE_STRUCTURAL_EQUALITY (t);
8402 else if (any_noncanonical_p)
8403 TYPE_CANONICAL (t)
8404 = build_method_type_directly (TYPE_CANONICAL (basetype),
8405 TYPE_CANONICAL (rettype),
8406 canon_argtypes);
8407 if (!COMPLETE_TYPE_P (t))
8408 layout_type (t);
8410 return t;
8413 /* Construct, lay out and return the type of methods belonging to class
8414 BASETYPE and whose arguments and values are described by TYPE.
8415 If that type exists already, reuse it.
8416 TYPE must be a FUNCTION_TYPE node. */
8418 tree
8419 build_method_type (tree basetype, tree type)
8421 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8423 return build_method_type_directly (basetype,
8424 TREE_TYPE (type),
8425 TYPE_ARG_TYPES (type));
8428 /* Construct, lay out and return the type of offsets to a value
8429 of type TYPE, within an object of type BASETYPE.
8430 If a suitable offset type exists already, reuse it. */
8432 tree
8433 build_offset_type (tree basetype, tree type)
8435 tree t;
8436 inchash::hash hstate;
8438 /* Make a node of the sort we want. */
8439 t = make_node (OFFSET_TYPE);
8441 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8442 TREE_TYPE (t) = type;
8444 /* If we already have such a type, use the old one. */
8445 hstate.add_object (TYPE_HASH (basetype));
8446 hstate.add_object (TYPE_HASH (type));
8447 t = type_hash_canon (hstate.end (), t);
8449 if (!COMPLETE_TYPE_P (t))
8450 layout_type (t);
8452 if (TYPE_CANONICAL (t) == t)
8454 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8455 || TYPE_STRUCTURAL_EQUALITY_P (type))
8456 SET_TYPE_STRUCTURAL_EQUALITY (t);
8457 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8458 || TYPE_CANONICAL (type) != type)
8459 TYPE_CANONICAL (t)
8460 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8461 TYPE_CANONICAL (type));
8464 return t;
8467 /* Create a complex type whose components are COMPONENT_TYPE. */
8469 tree
8470 build_complex_type (tree component_type)
8472 tree t;
8473 inchash::hash hstate;
8475 gcc_assert (INTEGRAL_TYPE_P (component_type)
8476 || SCALAR_FLOAT_TYPE_P (component_type)
8477 || FIXED_POINT_TYPE_P (component_type));
8479 /* Make a node of the sort we want. */
8480 t = make_node (COMPLEX_TYPE);
8482 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8484 /* If we already have such a type, use the old one. */
8485 hstate.add_object (TYPE_HASH (component_type));
8486 t = type_hash_canon (hstate.end (), t);
8488 if (!COMPLETE_TYPE_P (t))
8489 layout_type (t);
8491 if (TYPE_CANONICAL (t) == t)
8493 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8494 SET_TYPE_STRUCTURAL_EQUALITY (t);
8495 else if (TYPE_CANONICAL (component_type) != component_type)
8496 TYPE_CANONICAL (t)
8497 = build_complex_type (TYPE_CANONICAL (component_type));
8500 /* We need to create a name, since complex is a fundamental type. */
8501 if (! TYPE_NAME (t))
8503 const char *name;
8504 if (component_type == char_type_node)
8505 name = "complex char";
8506 else if (component_type == signed_char_type_node)
8507 name = "complex signed char";
8508 else if (component_type == unsigned_char_type_node)
8509 name = "complex unsigned char";
8510 else if (component_type == short_integer_type_node)
8511 name = "complex short int";
8512 else if (component_type == short_unsigned_type_node)
8513 name = "complex short unsigned int";
8514 else if (component_type == integer_type_node)
8515 name = "complex int";
8516 else if (component_type == unsigned_type_node)
8517 name = "complex unsigned int";
8518 else if (component_type == long_integer_type_node)
8519 name = "complex long int";
8520 else if (component_type == long_unsigned_type_node)
8521 name = "complex long unsigned int";
8522 else if (component_type == long_long_integer_type_node)
8523 name = "complex long long int";
8524 else if (component_type == long_long_unsigned_type_node)
8525 name = "complex long long unsigned int";
8526 else
8527 name = 0;
8529 if (name != 0)
8530 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8531 get_identifier (name), t);
8534 return build_qualified_type (t, TYPE_QUALS (component_type));
8537 /* If TYPE is a real or complex floating-point type and the target
8538 does not directly support arithmetic on TYPE then return the wider
8539 type to be used for arithmetic on TYPE. Otherwise, return
8540 NULL_TREE. */
8542 tree
8543 excess_precision_type (tree type)
8545 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8547 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8548 switch (TREE_CODE (type))
8550 case REAL_TYPE:
8551 switch (flt_eval_method)
8553 case 1:
8554 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8555 return double_type_node;
8556 break;
8557 case 2:
8558 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8559 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8560 return long_double_type_node;
8561 break;
8562 default:
8563 gcc_unreachable ();
8565 break;
8566 case COMPLEX_TYPE:
8567 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8568 return NULL_TREE;
8569 switch (flt_eval_method)
8571 case 1:
8572 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8573 return complex_double_type_node;
8574 break;
8575 case 2:
8576 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8577 || (TYPE_MODE (TREE_TYPE (type))
8578 == TYPE_MODE (double_type_node)))
8579 return complex_long_double_type_node;
8580 break;
8581 default:
8582 gcc_unreachable ();
8584 break;
8585 default:
8586 break;
8589 return NULL_TREE;
8592 /* Return OP, stripped of any conversions to wider types as much as is safe.
8593 Converting the value back to OP's type makes a value equivalent to OP.
8595 If FOR_TYPE is nonzero, we return a value which, if converted to
8596 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8598 OP must have integer, real or enumeral type. Pointers are not allowed!
8600 There are some cases where the obvious value we could return
8601 would regenerate to OP if converted to OP's type,
8602 but would not extend like OP to wider types.
8603 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8604 For example, if OP is (unsigned short)(signed char)-1,
8605 we avoid returning (signed char)-1 if FOR_TYPE is int,
8606 even though extending that to an unsigned short would regenerate OP,
8607 since the result of extending (signed char)-1 to (int)
8608 is different from (int) OP. */
8610 tree
8611 get_unwidened (tree op, tree for_type)
8613 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8614 tree type = TREE_TYPE (op);
8615 unsigned final_prec
8616 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8617 int uns
8618 = (for_type != 0 && for_type != type
8619 && final_prec > TYPE_PRECISION (type)
8620 && TYPE_UNSIGNED (type));
8621 tree win = op;
8623 while (CONVERT_EXPR_P (op))
8625 int bitschange;
8627 /* TYPE_PRECISION on vector types has different meaning
8628 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8629 so avoid them here. */
8630 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8631 break;
8633 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8634 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8636 /* Truncations are many-one so cannot be removed.
8637 Unless we are later going to truncate down even farther. */
8638 if (bitschange < 0
8639 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8640 break;
8642 /* See what's inside this conversion. If we decide to strip it,
8643 we will set WIN. */
8644 op = TREE_OPERAND (op, 0);
8646 /* If we have not stripped any zero-extensions (uns is 0),
8647 we can strip any kind of extension.
8648 If we have previously stripped a zero-extension,
8649 only zero-extensions can safely be stripped.
8650 Any extension can be stripped if the bits it would produce
8651 are all going to be discarded later by truncating to FOR_TYPE. */
8653 if (bitschange > 0)
8655 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8656 win = op;
8657 /* TYPE_UNSIGNED says whether this is a zero-extension.
8658 Let's avoid computing it if it does not affect WIN
8659 and if UNS will not be needed again. */
8660 if ((uns
8661 || CONVERT_EXPR_P (op))
8662 && TYPE_UNSIGNED (TREE_TYPE (op)))
8664 uns = 1;
8665 win = op;
8670 /* If we finally reach a constant see if it fits in for_type and
8671 in that case convert it. */
8672 if (for_type
8673 && TREE_CODE (win) == INTEGER_CST
8674 && TREE_TYPE (win) != for_type
8675 && int_fits_type_p (win, for_type))
8676 win = fold_convert (for_type, win);
8678 return win;
8681 /* Return OP or a simpler expression for a narrower value
8682 which can be sign-extended or zero-extended to give back OP.
8683 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8684 or 0 if the value should be sign-extended. */
8686 tree
8687 get_narrower (tree op, int *unsignedp_ptr)
8689 int uns = 0;
8690 int first = 1;
8691 tree win = op;
8692 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8694 while (TREE_CODE (op) == NOP_EXPR)
8696 int bitschange
8697 = (TYPE_PRECISION (TREE_TYPE (op))
8698 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8700 /* Truncations are many-one so cannot be removed. */
8701 if (bitschange < 0)
8702 break;
8704 /* See what's inside this conversion. If we decide to strip it,
8705 we will set WIN. */
8707 if (bitschange > 0)
8709 op = TREE_OPERAND (op, 0);
8710 /* An extension: the outermost one can be stripped,
8711 but remember whether it is zero or sign extension. */
8712 if (first)
8713 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8714 /* Otherwise, if a sign extension has been stripped,
8715 only sign extensions can now be stripped;
8716 if a zero extension has been stripped, only zero-extensions. */
8717 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8718 break;
8719 first = 0;
8721 else /* bitschange == 0 */
8723 /* A change in nominal type can always be stripped, but we must
8724 preserve the unsignedness. */
8725 if (first)
8726 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8727 first = 0;
8728 op = TREE_OPERAND (op, 0);
8729 /* Keep trying to narrow, but don't assign op to win if it
8730 would turn an integral type into something else. */
8731 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8732 continue;
8735 win = op;
8738 if (TREE_CODE (op) == COMPONENT_REF
8739 /* Since type_for_size always gives an integer type. */
8740 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8741 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8742 /* Ensure field is laid out already. */
8743 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8744 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
8746 unsigned HOST_WIDE_INT innerprec
8747 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
8748 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8749 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8750 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8752 /* We can get this structure field in a narrower type that fits it,
8753 but the resulting extension to its nominal type (a fullword type)
8754 must satisfy the same conditions as for other extensions.
8756 Do this only for fields that are aligned (not bit-fields),
8757 because when bit-field insns will be used there is no
8758 advantage in doing this. */
8760 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8761 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8762 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8763 && type != 0)
8765 if (first)
8766 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8767 win = fold_convert (type, op);
8771 *unsignedp_ptr = uns;
8772 return win;
8775 /* Returns true if integer constant C has a value that is permissible
8776 for type TYPE (an INTEGER_TYPE). */
8778 bool
8779 int_fits_type_p (const_tree c, const_tree type)
8781 tree type_low_bound, type_high_bound;
8782 bool ok_for_low_bound, ok_for_high_bound;
8783 signop sgn_c = TYPE_SIGN (TREE_TYPE (c));
8785 retry:
8786 type_low_bound = TYPE_MIN_VALUE (type);
8787 type_high_bound = TYPE_MAX_VALUE (type);
8789 /* If at least one bound of the type is a constant integer, we can check
8790 ourselves and maybe make a decision. If no such decision is possible, but
8791 this type is a subtype, try checking against that. Otherwise, use
8792 fits_to_tree_p, which checks against the precision.
8794 Compute the status for each possibly constant bound, and return if we see
8795 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8796 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8797 for "constant known to fit". */
8799 /* Check if c >= type_low_bound. */
8800 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8802 if (tree_int_cst_lt (c, type_low_bound))
8803 return false;
8804 ok_for_low_bound = true;
8806 else
8807 ok_for_low_bound = false;
8809 /* Check if c <= type_high_bound. */
8810 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8812 if (tree_int_cst_lt (type_high_bound, c))
8813 return false;
8814 ok_for_high_bound = true;
8816 else
8817 ok_for_high_bound = false;
8819 /* If the constant fits both bounds, the result is known. */
8820 if (ok_for_low_bound && ok_for_high_bound)
8821 return true;
8823 /* Perform some generic filtering which may allow making a decision
8824 even if the bounds are not constant. First, negative integers
8825 never fit in unsigned types, */
8826 if (TYPE_UNSIGNED (type) && sgn_c == SIGNED && wi::neg_p (c))
8827 return false;
8829 /* Second, narrower types always fit in wider ones. */
8830 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8831 return true;
8833 /* Third, unsigned integers with top bit set never fit signed types. */
8834 if (!TYPE_UNSIGNED (type) && sgn_c == UNSIGNED)
8836 int prec = GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (c))) - 1;
8837 if (prec < TYPE_PRECISION (TREE_TYPE (c)))
8839 /* When a tree_cst is converted to a wide-int, the precision
8840 is taken from the type. However, if the precision of the
8841 mode underneath the type is smaller than that, it is
8842 possible that the value will not fit. The test below
8843 fails if any bit is set between the sign bit of the
8844 underlying mode and the top bit of the type. */
8845 if (wi::ne_p (wi::zext (c, prec - 1), c))
8846 return false;
8848 else if (wi::neg_p (c))
8849 return false;
8852 /* If we haven't been able to decide at this point, there nothing more we
8853 can check ourselves here. Look at the base type if we have one and it
8854 has the same precision. */
8855 if (TREE_CODE (type) == INTEGER_TYPE
8856 && TREE_TYPE (type) != 0
8857 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8859 type = TREE_TYPE (type);
8860 goto retry;
8863 /* Or to fits_to_tree_p, if nothing else. */
8864 return wi::fits_to_tree_p (c, type);
8867 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8868 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8869 represented (assuming two's-complement arithmetic) within the bit
8870 precision of the type are returned instead. */
8872 void
8873 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8875 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8876 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8877 wi::to_mpz (TYPE_MIN_VALUE (type), min, TYPE_SIGN (type));
8878 else
8880 if (TYPE_UNSIGNED (type))
8881 mpz_set_ui (min, 0);
8882 else
8884 wide_int mn = wi::min_value (TYPE_PRECISION (type), SIGNED);
8885 wi::to_mpz (mn, min, SIGNED);
8889 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8890 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8891 wi::to_mpz (TYPE_MAX_VALUE (type), max, TYPE_SIGN (type));
8892 else
8894 wide_int mn = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
8895 wi::to_mpz (mn, max, TYPE_SIGN (type));
8899 /* Return true if VAR is an automatic variable defined in function FN. */
8901 bool
8902 auto_var_in_fn_p (const_tree var, const_tree fn)
8904 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8905 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8906 || TREE_CODE (var) == PARM_DECL)
8907 && ! TREE_STATIC (var))
8908 || TREE_CODE (var) == LABEL_DECL
8909 || TREE_CODE (var) == RESULT_DECL));
8912 /* Subprogram of following function. Called by walk_tree.
8914 Return *TP if it is an automatic variable or parameter of the
8915 function passed in as DATA. */
8917 static tree
8918 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8920 tree fn = (tree) data;
8922 if (TYPE_P (*tp))
8923 *walk_subtrees = 0;
8925 else if (DECL_P (*tp)
8926 && auto_var_in_fn_p (*tp, fn))
8927 return *tp;
8929 return NULL_TREE;
8932 /* Returns true if T is, contains, or refers to a type with variable
8933 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8934 arguments, but not the return type. If FN is nonzero, only return
8935 true if a modifier of the type or position of FN is a variable or
8936 parameter inside FN.
8938 This concept is more general than that of C99 'variably modified types':
8939 in C99, a struct type is never variably modified because a VLA may not
8940 appear as a structure member. However, in GNU C code like:
8942 struct S { int i[f()]; };
8944 is valid, and other languages may define similar constructs. */
8946 bool
8947 variably_modified_type_p (tree type, tree fn)
8949 tree t;
8951 /* Test if T is either variable (if FN is zero) or an expression containing
8952 a variable in FN. If TYPE isn't gimplified, return true also if
8953 gimplify_one_sizepos would gimplify the expression into a local
8954 variable. */
8955 #define RETURN_TRUE_IF_VAR(T) \
8956 do { tree _t = (T); \
8957 if (_t != NULL_TREE \
8958 && _t != error_mark_node \
8959 && TREE_CODE (_t) != INTEGER_CST \
8960 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8961 && (!fn \
8962 || (!TYPE_SIZES_GIMPLIFIED (type) \
8963 && !is_gimple_sizepos (_t)) \
8964 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8965 return true; } while (0)
8967 if (type == error_mark_node)
8968 return false;
8970 /* If TYPE itself has variable size, it is variably modified. */
8971 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8972 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8974 switch (TREE_CODE (type))
8976 case POINTER_TYPE:
8977 case REFERENCE_TYPE:
8978 case VECTOR_TYPE:
8979 if (variably_modified_type_p (TREE_TYPE (type), fn))
8980 return true;
8981 break;
8983 case FUNCTION_TYPE:
8984 case METHOD_TYPE:
8985 /* If TYPE is a function type, it is variably modified if the
8986 return type is variably modified. */
8987 if (variably_modified_type_p (TREE_TYPE (type), fn))
8988 return true;
8989 break;
8991 case INTEGER_TYPE:
8992 case REAL_TYPE:
8993 case FIXED_POINT_TYPE:
8994 case ENUMERAL_TYPE:
8995 case BOOLEAN_TYPE:
8996 /* Scalar types are variably modified if their end points
8997 aren't constant. */
8998 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8999 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
9000 break;
9002 case RECORD_TYPE:
9003 case UNION_TYPE:
9004 case QUAL_UNION_TYPE:
9005 /* We can't see if any of the fields are variably-modified by the
9006 definition we normally use, since that would produce infinite
9007 recursion via pointers. */
9008 /* This is variably modified if some field's type is. */
9009 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
9010 if (TREE_CODE (t) == FIELD_DECL)
9012 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
9013 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
9014 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
9016 if (TREE_CODE (type) == QUAL_UNION_TYPE)
9017 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
9019 break;
9021 case ARRAY_TYPE:
9022 /* Do not call ourselves to avoid infinite recursion. This is
9023 variably modified if the element type is. */
9024 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
9025 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
9026 break;
9028 default:
9029 break;
9032 /* The current language may have other cases to check, but in general,
9033 all other types are not variably modified. */
9034 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
9036 #undef RETURN_TRUE_IF_VAR
9039 /* Given a DECL or TYPE, return the scope in which it was declared, or
9040 NULL_TREE if there is no containing scope. */
9042 tree
9043 get_containing_scope (const_tree t)
9045 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
9048 /* Return the innermost context enclosing DECL that is
9049 a FUNCTION_DECL, or zero if none. */
9051 tree
9052 decl_function_context (const_tree decl)
9054 tree context;
9056 if (TREE_CODE (decl) == ERROR_MARK)
9057 return 0;
9059 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
9060 where we look up the function at runtime. Such functions always take
9061 a first argument of type 'pointer to real context'.
9063 C++ should really be fixed to use DECL_CONTEXT for the real context,
9064 and use something else for the "virtual context". */
9065 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
9066 context
9067 = TYPE_MAIN_VARIANT
9068 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
9069 else
9070 context = DECL_CONTEXT (decl);
9072 while (context && TREE_CODE (context) != FUNCTION_DECL)
9074 if (TREE_CODE (context) == BLOCK)
9075 context = BLOCK_SUPERCONTEXT (context);
9076 else
9077 context = get_containing_scope (context);
9080 return context;
9083 /* Return the innermost context enclosing DECL that is
9084 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
9085 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
9087 tree
9088 decl_type_context (const_tree decl)
9090 tree context = DECL_CONTEXT (decl);
9092 while (context)
9093 switch (TREE_CODE (context))
9095 case NAMESPACE_DECL:
9096 case TRANSLATION_UNIT_DECL:
9097 return NULL_TREE;
9099 case RECORD_TYPE:
9100 case UNION_TYPE:
9101 case QUAL_UNION_TYPE:
9102 return context;
9104 case TYPE_DECL:
9105 case FUNCTION_DECL:
9106 context = DECL_CONTEXT (context);
9107 break;
9109 case BLOCK:
9110 context = BLOCK_SUPERCONTEXT (context);
9111 break;
9113 default:
9114 gcc_unreachable ();
9117 return NULL_TREE;
9120 /* CALL is a CALL_EXPR. Return the declaration for the function
9121 called, or NULL_TREE if the called function cannot be
9122 determined. */
9124 tree
9125 get_callee_fndecl (const_tree call)
9127 tree addr;
9129 if (call == error_mark_node)
9130 return error_mark_node;
9132 /* It's invalid to call this function with anything but a
9133 CALL_EXPR. */
9134 gcc_assert (TREE_CODE (call) == CALL_EXPR);
9136 /* The first operand to the CALL is the address of the function
9137 called. */
9138 addr = CALL_EXPR_FN (call);
9140 /* If there is no function, return early. */
9141 if (addr == NULL_TREE)
9142 return NULL_TREE;
9144 STRIP_NOPS (addr);
9146 /* If this is a readonly function pointer, extract its initial value. */
9147 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
9148 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
9149 && DECL_INITIAL (addr))
9150 addr = DECL_INITIAL (addr);
9152 /* If the address is just `&f' for some function `f', then we know
9153 that `f' is being called. */
9154 if (TREE_CODE (addr) == ADDR_EXPR
9155 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
9156 return TREE_OPERAND (addr, 0);
9158 /* We couldn't figure out what was being called. */
9159 return NULL_TREE;
9162 #define TREE_MEM_USAGE_SPACES 40
9164 /* Print debugging information about tree nodes generated during the compile,
9165 and any language-specific information. */
9167 void
9168 dump_tree_statistics (void)
9170 if (GATHER_STATISTICS)
9172 int i;
9173 int total_nodes, total_bytes;
9174 fprintf (stderr, "\nKind Nodes Bytes\n");
9175 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9176 total_nodes = total_bytes = 0;
9177 for (i = 0; i < (int) all_kinds; i++)
9179 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
9180 tree_node_counts[i], tree_node_sizes[i]);
9181 total_nodes += tree_node_counts[i];
9182 total_bytes += tree_node_sizes[i];
9184 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9185 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
9186 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9187 fprintf (stderr, "Code Nodes\n");
9188 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9189 for (i = 0; i < (int) MAX_TREE_CODES; i++)
9190 fprintf (stderr, "%-32s %7d\n", get_tree_code_name ((enum tree_code) i),
9191 tree_code_counts[i]);
9192 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9193 fprintf (stderr, "\n");
9194 ssanames_print_statistics ();
9195 fprintf (stderr, "\n");
9196 phinodes_print_statistics ();
9197 fprintf (stderr, "\n");
9199 else
9200 fprintf (stderr, "(No per-node statistics)\n");
9202 print_type_hash_statistics ();
9203 print_debug_expr_statistics ();
9204 print_value_expr_statistics ();
9205 lang_hooks.print_statistics ();
9208 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
9210 /* Generate a crc32 of a byte. */
9212 static unsigned
9213 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
9215 unsigned ix;
9217 for (ix = bits; ix--; value <<= 1)
9219 unsigned feedback;
9221 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
9222 chksum <<= 1;
9223 chksum ^= feedback;
9225 return chksum;
9228 /* Generate a crc32 of a 32-bit unsigned. */
9230 unsigned
9231 crc32_unsigned (unsigned chksum, unsigned value)
9233 return crc32_unsigned_bits (chksum, value, 32);
9236 /* Generate a crc32 of a byte. */
9238 unsigned
9239 crc32_byte (unsigned chksum, char byte)
9241 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
9244 /* Generate a crc32 of a string. */
9246 unsigned
9247 crc32_string (unsigned chksum, const char *string)
9251 chksum = crc32_byte (chksum, *string);
9253 while (*string++);
9254 return chksum;
9257 /* P is a string that will be used in a symbol. Mask out any characters
9258 that are not valid in that context. */
9260 void
9261 clean_symbol_name (char *p)
9263 for (; *p; p++)
9264 if (! (ISALNUM (*p)
9265 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
9266 || *p == '$'
9267 #endif
9268 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
9269 || *p == '.'
9270 #endif
9272 *p = '_';
9275 /* For anonymous aggregate types, we need some sort of name to
9276 hold on to. In practice, this should not appear, but it should
9277 not be harmful if it does. */
9278 bool
9279 anon_aggrname_p(const_tree id_node)
9281 #ifndef NO_DOT_IN_LABEL
9282 return (IDENTIFIER_POINTER (id_node)[0] == '.'
9283 && IDENTIFIER_POINTER (id_node)[1] == '_');
9284 #else /* NO_DOT_IN_LABEL */
9285 #ifndef NO_DOLLAR_IN_LABEL
9286 return (IDENTIFIER_POINTER (id_node)[0] == '$' \
9287 && IDENTIFIER_POINTER (id_node)[1] == '_');
9288 #else /* NO_DOLLAR_IN_LABEL */
9289 #define ANON_AGGRNAME_PREFIX "__anon_"
9290 return (!strncmp (IDENTIFIER_POINTER (id_node), ANON_AGGRNAME_PREFIX,
9291 sizeof (ANON_AGGRNAME_PREFIX) - 1));
9292 #endif /* NO_DOLLAR_IN_LABEL */
9293 #endif /* NO_DOT_IN_LABEL */
9296 /* Return a format for an anonymous aggregate name. */
9297 const char *
9298 anon_aggrname_format()
9300 #ifndef NO_DOT_IN_LABEL
9301 return "._%d";
9302 #else /* NO_DOT_IN_LABEL */
9303 #ifndef NO_DOLLAR_IN_LABEL
9304 return "$_%d";
9305 #else /* NO_DOLLAR_IN_LABEL */
9306 return "__anon_%d";
9307 #endif /* NO_DOLLAR_IN_LABEL */
9308 #endif /* NO_DOT_IN_LABEL */
9311 /* Generate a name for a special-purpose function.
9312 The generated name may need to be unique across the whole link.
9313 Changes to this function may also require corresponding changes to
9314 xstrdup_mask_random.
9315 TYPE is some string to identify the purpose of this function to the
9316 linker or collect2; it must start with an uppercase letter,
9317 one of:
9318 I - for constructors
9319 D - for destructors
9320 N - for C++ anonymous namespaces
9321 F - for DWARF unwind frame information. */
9323 tree
9324 get_file_function_name (const char *type)
9326 char *buf;
9327 const char *p;
9328 char *q;
9330 /* If we already have a name we know to be unique, just use that. */
9331 if (first_global_object_name)
9332 p = q = ASTRDUP (first_global_object_name);
9333 /* If the target is handling the constructors/destructors, they
9334 will be local to this file and the name is only necessary for
9335 debugging purposes.
9336 We also assign sub_I and sub_D sufixes to constructors called from
9337 the global static constructors. These are always local. */
9338 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9339 || (strncmp (type, "sub_", 4) == 0
9340 && (type[4] == 'I' || type[4] == 'D')))
9342 const char *file = main_input_filename;
9343 if (! file)
9344 file = LOCATION_FILE (input_location);
9345 /* Just use the file's basename, because the full pathname
9346 might be quite long. */
9347 p = q = ASTRDUP (lbasename (file));
9349 else
9351 /* Otherwise, the name must be unique across the entire link.
9352 We don't have anything that we know to be unique to this translation
9353 unit, so use what we do have and throw in some randomness. */
9354 unsigned len;
9355 const char *name = weak_global_object_name;
9356 const char *file = main_input_filename;
9358 if (! name)
9359 name = "";
9360 if (! file)
9361 file = LOCATION_FILE (input_location);
9363 len = strlen (file);
9364 q = (char *) alloca (9 + 17 + len + 1);
9365 memcpy (q, file, len + 1);
9367 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9368 crc32_string (0, name), get_random_seed (false));
9370 p = q;
9373 clean_symbol_name (q);
9374 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9375 + strlen (type));
9377 /* Set up the name of the file-level functions we may need.
9378 Use a global object (which is already required to be unique over
9379 the program) rather than the file name (which imposes extra
9380 constraints). */
9381 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9383 return get_identifier (buf);
9386 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9388 /* Complain that the tree code of NODE does not match the expected 0
9389 terminated list of trailing codes. The trailing code list can be
9390 empty, for a more vague error message. FILE, LINE, and FUNCTION
9391 are of the caller. */
9393 void
9394 tree_check_failed (const_tree node, const char *file,
9395 int line, const char *function, ...)
9397 va_list args;
9398 const char *buffer;
9399 unsigned length = 0;
9400 enum tree_code code;
9402 va_start (args, function);
9403 while ((code = (enum tree_code) va_arg (args, int)))
9404 length += 4 + strlen (get_tree_code_name (code));
9405 va_end (args);
9406 if (length)
9408 char *tmp;
9409 va_start (args, function);
9410 length += strlen ("expected ");
9411 buffer = tmp = (char *) alloca (length);
9412 length = 0;
9413 while ((code = (enum tree_code) va_arg (args, int)))
9415 const char *prefix = length ? " or " : "expected ";
9417 strcpy (tmp + length, prefix);
9418 length += strlen (prefix);
9419 strcpy (tmp + length, get_tree_code_name (code));
9420 length += strlen (get_tree_code_name (code));
9422 va_end (args);
9424 else
9425 buffer = "unexpected node";
9427 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9428 buffer, get_tree_code_name (TREE_CODE (node)),
9429 function, trim_filename (file), line);
9432 /* Complain that the tree code of NODE does match the expected 0
9433 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9434 the caller. */
9436 void
9437 tree_not_check_failed (const_tree node, const char *file,
9438 int line, const char *function, ...)
9440 va_list args;
9441 char *buffer;
9442 unsigned length = 0;
9443 enum tree_code code;
9445 va_start (args, function);
9446 while ((code = (enum tree_code) va_arg (args, int)))
9447 length += 4 + strlen (get_tree_code_name (code));
9448 va_end (args);
9449 va_start (args, function);
9450 buffer = (char *) alloca (length);
9451 length = 0;
9452 while ((code = (enum tree_code) va_arg (args, int)))
9454 if (length)
9456 strcpy (buffer + length, " or ");
9457 length += 4;
9459 strcpy (buffer + length, get_tree_code_name (code));
9460 length += strlen (get_tree_code_name (code));
9462 va_end (args);
9464 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9465 buffer, get_tree_code_name (TREE_CODE (node)),
9466 function, trim_filename (file), line);
9469 /* Similar to tree_check_failed, except that we check for a class of tree
9470 code, given in CL. */
9472 void
9473 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9474 const char *file, int line, const char *function)
9476 internal_error
9477 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9478 TREE_CODE_CLASS_STRING (cl),
9479 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9480 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9483 /* Similar to tree_check_failed, except that instead of specifying a
9484 dozen codes, use the knowledge that they're all sequential. */
9486 void
9487 tree_range_check_failed (const_tree node, const char *file, int line,
9488 const char *function, enum tree_code c1,
9489 enum tree_code c2)
9491 char *buffer;
9492 unsigned length = 0;
9493 unsigned int c;
9495 for (c = c1; c <= c2; ++c)
9496 length += 4 + strlen (get_tree_code_name ((enum tree_code) c));
9498 length += strlen ("expected ");
9499 buffer = (char *) alloca (length);
9500 length = 0;
9502 for (c = c1; c <= c2; ++c)
9504 const char *prefix = length ? " or " : "expected ";
9506 strcpy (buffer + length, prefix);
9507 length += strlen (prefix);
9508 strcpy (buffer + length, get_tree_code_name ((enum tree_code) c));
9509 length += strlen (get_tree_code_name ((enum tree_code) c));
9512 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9513 buffer, get_tree_code_name (TREE_CODE (node)),
9514 function, trim_filename (file), line);
9518 /* Similar to tree_check_failed, except that we check that a tree does
9519 not have the specified code, given in CL. */
9521 void
9522 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9523 const char *file, int line, const char *function)
9525 internal_error
9526 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9527 TREE_CODE_CLASS_STRING (cl),
9528 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9529 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9533 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9535 void
9536 omp_clause_check_failed (const_tree node, const char *file, int line,
9537 const char *function, enum omp_clause_code code)
9539 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9540 omp_clause_code_name[code], get_tree_code_name (TREE_CODE (node)),
9541 function, trim_filename (file), line);
9545 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9547 void
9548 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9549 const char *function, enum omp_clause_code c1,
9550 enum omp_clause_code c2)
9552 char *buffer;
9553 unsigned length = 0;
9554 unsigned int c;
9556 for (c = c1; c <= c2; ++c)
9557 length += 4 + strlen (omp_clause_code_name[c]);
9559 length += strlen ("expected ");
9560 buffer = (char *) alloca (length);
9561 length = 0;
9563 for (c = c1; c <= c2; ++c)
9565 const char *prefix = length ? " or " : "expected ";
9567 strcpy (buffer + length, prefix);
9568 length += strlen (prefix);
9569 strcpy (buffer + length, omp_clause_code_name[c]);
9570 length += strlen (omp_clause_code_name[c]);
9573 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9574 buffer, omp_clause_code_name[TREE_CODE (node)],
9575 function, trim_filename (file), line);
9579 #undef DEFTREESTRUCT
9580 #define DEFTREESTRUCT(VAL, NAME) NAME,
9582 static const char *ts_enum_names[] = {
9583 #include "treestruct.def"
9585 #undef DEFTREESTRUCT
9587 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9589 /* Similar to tree_class_check_failed, except that we check for
9590 whether CODE contains the tree structure identified by EN. */
9592 void
9593 tree_contains_struct_check_failed (const_tree node,
9594 const enum tree_node_structure_enum en,
9595 const char *file, int line,
9596 const char *function)
9598 internal_error
9599 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9600 TS_ENUM_NAME (en),
9601 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9605 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9606 (dynamically sized) vector. */
9608 void
9609 tree_int_cst_elt_check_failed (int idx, int len, const char *file, int line,
9610 const char *function)
9612 internal_error
9613 ("tree check: accessed elt %d of tree_int_cst with %d elts in %s, at %s:%d",
9614 idx + 1, len, function, trim_filename (file), line);
9617 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9618 (dynamically sized) vector. */
9620 void
9621 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9622 const char *function)
9624 internal_error
9625 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9626 idx + 1, len, function, trim_filename (file), line);
9629 /* Similar to above, except that the check is for the bounds of the operand
9630 vector of an expression node EXP. */
9632 void
9633 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9634 int line, const char *function)
9636 enum tree_code code = TREE_CODE (exp);
9637 internal_error
9638 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9639 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9640 function, trim_filename (file), line);
9643 /* Similar to above, except that the check is for the number of
9644 operands of an OMP_CLAUSE node. */
9646 void
9647 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9648 int line, const char *function)
9650 internal_error
9651 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9652 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9653 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9654 trim_filename (file), line);
9656 #endif /* ENABLE_TREE_CHECKING */
9658 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9659 and mapped to the machine mode MODE. Initialize its fields and build
9660 the information necessary for debugging output. */
9662 static tree
9663 make_vector_type (tree innertype, int nunits, machine_mode mode)
9665 tree t;
9666 inchash::hash hstate;
9668 t = make_node (VECTOR_TYPE);
9669 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9670 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9671 SET_TYPE_MODE (t, mode);
9673 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9674 SET_TYPE_STRUCTURAL_EQUALITY (t);
9675 else if (TYPE_CANONICAL (innertype) != innertype
9676 || mode != VOIDmode)
9677 TYPE_CANONICAL (t)
9678 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9680 layout_type (t);
9682 hstate.add_wide_int (VECTOR_TYPE);
9683 hstate.add_wide_int (nunits);
9684 hstate.add_wide_int (mode);
9685 hstate.add_object (TYPE_HASH (TREE_TYPE (t)));
9686 t = type_hash_canon (hstate.end (), t);
9688 /* We have built a main variant, based on the main variant of the
9689 inner type. Use it to build the variant we return. */
9690 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9691 && TREE_TYPE (t) != innertype)
9692 return build_type_attribute_qual_variant (t,
9693 TYPE_ATTRIBUTES (innertype),
9694 TYPE_QUALS (innertype));
9696 return t;
9699 static tree
9700 make_or_reuse_type (unsigned size, int unsignedp)
9702 int i;
9704 if (size == INT_TYPE_SIZE)
9705 return unsignedp ? unsigned_type_node : integer_type_node;
9706 if (size == CHAR_TYPE_SIZE)
9707 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9708 if (size == SHORT_TYPE_SIZE)
9709 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9710 if (size == LONG_TYPE_SIZE)
9711 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9712 if (size == LONG_LONG_TYPE_SIZE)
9713 return (unsignedp ? long_long_unsigned_type_node
9714 : long_long_integer_type_node);
9716 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9717 if (size == int_n_data[i].bitsize
9718 && int_n_enabled_p[i])
9719 return (unsignedp ? int_n_trees[i].unsigned_type
9720 : int_n_trees[i].signed_type);
9722 if (unsignedp)
9723 return make_unsigned_type (size);
9724 else
9725 return make_signed_type (size);
9728 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9730 static tree
9731 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9733 if (satp)
9735 if (size == SHORT_FRACT_TYPE_SIZE)
9736 return unsignedp ? sat_unsigned_short_fract_type_node
9737 : sat_short_fract_type_node;
9738 if (size == FRACT_TYPE_SIZE)
9739 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9740 if (size == LONG_FRACT_TYPE_SIZE)
9741 return unsignedp ? sat_unsigned_long_fract_type_node
9742 : sat_long_fract_type_node;
9743 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9744 return unsignedp ? sat_unsigned_long_long_fract_type_node
9745 : sat_long_long_fract_type_node;
9747 else
9749 if (size == SHORT_FRACT_TYPE_SIZE)
9750 return unsignedp ? unsigned_short_fract_type_node
9751 : short_fract_type_node;
9752 if (size == FRACT_TYPE_SIZE)
9753 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9754 if (size == LONG_FRACT_TYPE_SIZE)
9755 return unsignedp ? unsigned_long_fract_type_node
9756 : long_fract_type_node;
9757 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9758 return unsignedp ? unsigned_long_long_fract_type_node
9759 : long_long_fract_type_node;
9762 return make_fract_type (size, unsignedp, satp);
9765 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9767 static tree
9768 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9770 if (satp)
9772 if (size == SHORT_ACCUM_TYPE_SIZE)
9773 return unsignedp ? sat_unsigned_short_accum_type_node
9774 : sat_short_accum_type_node;
9775 if (size == ACCUM_TYPE_SIZE)
9776 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9777 if (size == LONG_ACCUM_TYPE_SIZE)
9778 return unsignedp ? sat_unsigned_long_accum_type_node
9779 : sat_long_accum_type_node;
9780 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9781 return unsignedp ? sat_unsigned_long_long_accum_type_node
9782 : sat_long_long_accum_type_node;
9784 else
9786 if (size == SHORT_ACCUM_TYPE_SIZE)
9787 return unsignedp ? unsigned_short_accum_type_node
9788 : short_accum_type_node;
9789 if (size == ACCUM_TYPE_SIZE)
9790 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9791 if (size == LONG_ACCUM_TYPE_SIZE)
9792 return unsignedp ? unsigned_long_accum_type_node
9793 : long_accum_type_node;
9794 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9795 return unsignedp ? unsigned_long_long_accum_type_node
9796 : long_long_accum_type_node;
9799 return make_accum_type (size, unsignedp, satp);
9803 /* Create an atomic variant node for TYPE. This routine is called
9804 during initialization of data types to create the 5 basic atomic
9805 types. The generic build_variant_type function requires these to
9806 already be set up in order to function properly, so cannot be
9807 called from there. If ALIGN is non-zero, then ensure alignment is
9808 overridden to this value. */
9810 static tree
9811 build_atomic_base (tree type, unsigned int align)
9813 tree t;
9815 /* Make sure its not already registered. */
9816 if ((t = get_qualified_type (type, TYPE_QUAL_ATOMIC)))
9817 return t;
9819 t = build_variant_type_copy (type);
9820 set_type_quals (t, TYPE_QUAL_ATOMIC);
9822 if (align)
9823 TYPE_ALIGN (t) = align;
9825 return t;
9828 /* Create nodes for all integer types (and error_mark_node) using the sizes
9829 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9830 SHORT_DOUBLE specifies whether double should be of the same precision
9831 as float. */
9833 void
9834 build_common_tree_nodes (bool signed_char, bool short_double)
9836 int i;
9838 error_mark_node = make_node (ERROR_MARK);
9839 TREE_TYPE (error_mark_node) = error_mark_node;
9841 initialize_sizetypes ();
9843 /* Define both `signed char' and `unsigned char'. */
9844 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9845 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9846 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9847 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9849 /* Define `char', which is like either `signed char' or `unsigned char'
9850 but not the same as either. */
9851 char_type_node
9852 = (signed_char
9853 ? make_signed_type (CHAR_TYPE_SIZE)
9854 : make_unsigned_type (CHAR_TYPE_SIZE));
9855 TYPE_STRING_FLAG (char_type_node) = 1;
9857 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9858 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9859 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9860 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9861 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9862 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9863 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9864 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9866 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9868 int_n_trees[i].signed_type = make_signed_type (int_n_data[i].bitsize);
9869 int_n_trees[i].unsigned_type = make_unsigned_type (int_n_data[i].bitsize);
9870 TYPE_SIZE (int_n_trees[i].signed_type) = bitsize_int (int_n_data[i].bitsize);
9871 TYPE_SIZE (int_n_trees[i].unsigned_type) = bitsize_int (int_n_data[i].bitsize);
9873 if (int_n_data[i].bitsize > LONG_LONG_TYPE_SIZE
9874 && int_n_enabled_p[i])
9876 integer_types[itk_intN_0 + i * 2] = int_n_trees[i].signed_type;
9877 integer_types[itk_unsigned_intN_0 + i * 2] = int_n_trees[i].unsigned_type;
9881 /* Define a boolean type. This type only represents boolean values but
9882 may be larger than char depending on the value of BOOL_TYPE_SIZE. */
9883 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9884 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9885 TYPE_PRECISION (boolean_type_node) = 1;
9886 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9888 /* Define what type to use for size_t. */
9889 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9890 size_type_node = unsigned_type_node;
9891 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9892 size_type_node = long_unsigned_type_node;
9893 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9894 size_type_node = long_long_unsigned_type_node;
9895 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9896 size_type_node = short_unsigned_type_node;
9897 else
9899 int i;
9901 size_type_node = NULL_TREE;
9902 for (i = 0; i < NUM_INT_N_ENTS; i++)
9903 if (int_n_enabled_p[i])
9905 char name[50];
9906 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
9908 if (strcmp (name, SIZE_TYPE) == 0)
9910 size_type_node = int_n_trees[i].unsigned_type;
9913 if (size_type_node == NULL_TREE)
9914 gcc_unreachable ();
9917 /* Fill in the rest of the sized types. Reuse existing type nodes
9918 when possible. */
9919 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9920 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9921 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9922 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9923 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9925 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9926 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9927 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9928 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9929 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9931 /* Don't call build_qualified type for atomics. That routine does
9932 special processing for atomics, and until they are initialized
9933 it's better not to make that call.
9935 Check to see if there is a target override for atomic types. */
9937 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
9938 targetm.atomic_align_for_mode (QImode));
9939 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
9940 targetm.atomic_align_for_mode (HImode));
9941 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
9942 targetm.atomic_align_for_mode (SImode));
9943 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
9944 targetm.atomic_align_for_mode (DImode));
9945 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
9946 targetm.atomic_align_for_mode (TImode));
9948 access_public_node = get_identifier ("public");
9949 access_protected_node = get_identifier ("protected");
9950 access_private_node = get_identifier ("private");
9952 /* Define these next since types below may used them. */
9953 integer_zero_node = build_int_cst (integer_type_node, 0);
9954 integer_one_node = build_int_cst (integer_type_node, 1);
9955 integer_three_node = build_int_cst (integer_type_node, 3);
9956 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9958 size_zero_node = size_int (0);
9959 size_one_node = size_int (1);
9960 bitsize_zero_node = bitsize_int (0);
9961 bitsize_one_node = bitsize_int (1);
9962 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9964 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9965 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9967 void_type_node = make_node (VOID_TYPE);
9968 layout_type (void_type_node);
9970 pointer_bounds_type_node = targetm.chkp_bound_type ();
9972 /* We are not going to have real types in C with less than byte alignment,
9973 so we might as well not have any types that claim to have it. */
9974 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9975 TYPE_USER_ALIGN (void_type_node) = 0;
9977 void_node = make_node (VOID_CST);
9978 TREE_TYPE (void_node) = void_type_node;
9980 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9981 layout_type (TREE_TYPE (null_pointer_node));
9983 ptr_type_node = build_pointer_type (void_type_node);
9984 const_ptr_type_node
9985 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9986 fileptr_type_node = ptr_type_node;
9988 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
9990 float_type_node = make_node (REAL_TYPE);
9991 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9992 layout_type (float_type_node);
9994 double_type_node = make_node (REAL_TYPE);
9995 if (short_double)
9996 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9997 else
9998 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9999 layout_type (double_type_node);
10001 long_double_type_node = make_node (REAL_TYPE);
10002 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
10003 layout_type (long_double_type_node);
10005 float_ptr_type_node = build_pointer_type (float_type_node);
10006 double_ptr_type_node = build_pointer_type (double_type_node);
10007 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
10008 integer_ptr_type_node = build_pointer_type (integer_type_node);
10010 /* Fixed size integer types. */
10011 uint16_type_node = make_or_reuse_type (16, 1);
10012 uint32_type_node = make_or_reuse_type (32, 1);
10013 uint64_type_node = make_or_reuse_type (64, 1);
10015 /* Decimal float types. */
10016 dfloat32_type_node = make_node (REAL_TYPE);
10017 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
10018 layout_type (dfloat32_type_node);
10019 SET_TYPE_MODE (dfloat32_type_node, SDmode);
10020 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
10022 dfloat64_type_node = make_node (REAL_TYPE);
10023 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
10024 layout_type (dfloat64_type_node);
10025 SET_TYPE_MODE (dfloat64_type_node, DDmode);
10026 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
10028 dfloat128_type_node = make_node (REAL_TYPE);
10029 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
10030 layout_type (dfloat128_type_node);
10031 SET_TYPE_MODE (dfloat128_type_node, TDmode);
10032 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
10034 complex_integer_type_node = build_complex_type (integer_type_node);
10035 complex_float_type_node = build_complex_type (float_type_node);
10036 complex_double_type_node = build_complex_type (double_type_node);
10037 complex_long_double_type_node = build_complex_type (long_double_type_node);
10039 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
10040 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
10041 sat_ ## KIND ## _type_node = \
10042 make_sat_signed_ ## KIND ## _type (SIZE); \
10043 sat_unsigned_ ## KIND ## _type_node = \
10044 make_sat_unsigned_ ## KIND ## _type (SIZE); \
10045 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
10046 unsigned_ ## KIND ## _type_node = \
10047 make_unsigned_ ## KIND ## _type (SIZE);
10049 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
10050 sat_ ## WIDTH ## KIND ## _type_node = \
10051 make_sat_signed_ ## KIND ## _type (SIZE); \
10052 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
10053 make_sat_unsigned_ ## KIND ## _type (SIZE); \
10054 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
10055 unsigned_ ## WIDTH ## KIND ## _type_node = \
10056 make_unsigned_ ## KIND ## _type (SIZE);
10058 /* Make fixed-point type nodes based on four different widths. */
10059 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
10060 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
10061 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
10062 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
10063 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
10065 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
10066 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
10067 NAME ## _type_node = \
10068 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
10069 u ## NAME ## _type_node = \
10070 make_or_reuse_unsigned_ ## KIND ## _type \
10071 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
10072 sat_ ## NAME ## _type_node = \
10073 make_or_reuse_sat_signed_ ## KIND ## _type \
10074 (GET_MODE_BITSIZE (MODE ## mode)); \
10075 sat_u ## NAME ## _type_node = \
10076 make_or_reuse_sat_unsigned_ ## KIND ## _type \
10077 (GET_MODE_BITSIZE (U ## MODE ## mode));
10079 /* Fixed-point type and mode nodes. */
10080 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
10081 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
10082 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
10083 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
10084 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
10085 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
10086 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
10087 MAKE_FIXED_MODE_NODE (accum, ha, HA)
10088 MAKE_FIXED_MODE_NODE (accum, sa, SA)
10089 MAKE_FIXED_MODE_NODE (accum, da, DA)
10090 MAKE_FIXED_MODE_NODE (accum, ta, TA)
10093 tree t = targetm.build_builtin_va_list ();
10095 /* Many back-ends define record types without setting TYPE_NAME.
10096 If we copied the record type here, we'd keep the original
10097 record type without a name. This breaks name mangling. So,
10098 don't copy record types and let c_common_nodes_and_builtins()
10099 declare the type to be __builtin_va_list. */
10100 if (TREE_CODE (t) != RECORD_TYPE)
10101 t = build_variant_type_copy (t);
10103 va_list_type_node = t;
10107 /* Modify DECL for given flags.
10108 TM_PURE attribute is set only on types, so the function will modify
10109 DECL's type when ECF_TM_PURE is used. */
10111 void
10112 set_call_expr_flags (tree decl, int flags)
10114 if (flags & ECF_NOTHROW)
10115 TREE_NOTHROW (decl) = 1;
10116 if (flags & ECF_CONST)
10117 TREE_READONLY (decl) = 1;
10118 if (flags & ECF_PURE)
10119 DECL_PURE_P (decl) = 1;
10120 if (flags & ECF_LOOPING_CONST_OR_PURE)
10121 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
10122 if (flags & ECF_NOVOPS)
10123 DECL_IS_NOVOPS (decl) = 1;
10124 if (flags & ECF_NORETURN)
10125 TREE_THIS_VOLATILE (decl) = 1;
10126 if (flags & ECF_MALLOC)
10127 DECL_IS_MALLOC (decl) = 1;
10128 if (flags & ECF_RETURNS_TWICE)
10129 DECL_IS_RETURNS_TWICE (decl) = 1;
10130 if (flags & ECF_LEAF)
10131 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
10132 NULL, DECL_ATTRIBUTES (decl));
10133 if ((flags & ECF_TM_PURE) && flag_tm)
10134 apply_tm_attr (decl, get_identifier ("transaction_pure"));
10135 /* Looping const or pure is implied by noreturn.
10136 There is currently no way to declare looping const or looping pure alone. */
10137 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
10138 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
10142 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
10144 static void
10145 local_define_builtin (const char *name, tree type, enum built_in_function code,
10146 const char *library_name, int ecf_flags)
10148 tree decl;
10150 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
10151 library_name, NULL_TREE);
10152 set_call_expr_flags (decl, ecf_flags);
10154 set_builtin_decl (code, decl, true);
10157 /* Call this function after instantiating all builtins that the language
10158 front end cares about. This will build the rest of the builtins
10159 and internal functions that are relied upon by the tree optimizers and
10160 the middle-end. */
10162 void
10163 build_common_builtin_nodes (void)
10165 tree tmp, ftype;
10166 int ecf_flags;
10168 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
10170 ftype = build_function_type (void_type_node, void_list_node);
10171 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
10172 "__builtin_unreachable",
10173 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
10174 | ECF_CONST);
10177 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
10178 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10180 ftype = build_function_type_list (ptr_type_node,
10181 ptr_type_node, const_ptr_type_node,
10182 size_type_node, NULL_TREE);
10184 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
10185 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
10186 "memcpy", ECF_NOTHROW | ECF_LEAF);
10187 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10188 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
10189 "memmove", ECF_NOTHROW | ECF_LEAF);
10192 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
10194 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
10195 const_ptr_type_node, size_type_node,
10196 NULL_TREE);
10197 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
10198 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10201 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
10203 ftype = build_function_type_list (ptr_type_node,
10204 ptr_type_node, integer_type_node,
10205 size_type_node, NULL_TREE);
10206 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
10207 "memset", ECF_NOTHROW | ECF_LEAF);
10210 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
10212 ftype = build_function_type_list (ptr_type_node,
10213 size_type_node, NULL_TREE);
10214 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
10215 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
10218 ftype = build_function_type_list (ptr_type_node, size_type_node,
10219 size_type_node, NULL_TREE);
10220 local_define_builtin ("__builtin_alloca_with_align", ftype,
10221 BUILT_IN_ALLOCA_WITH_ALIGN,
10222 "__builtin_alloca_with_align",
10223 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
10225 /* If we're checking the stack, `alloca' can throw. */
10226 if (flag_stack_check)
10228 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
10229 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
10232 ftype = build_function_type_list (void_type_node,
10233 ptr_type_node, ptr_type_node,
10234 ptr_type_node, NULL_TREE);
10235 local_define_builtin ("__builtin_init_trampoline", ftype,
10236 BUILT_IN_INIT_TRAMPOLINE,
10237 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
10238 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
10239 BUILT_IN_INIT_HEAP_TRAMPOLINE,
10240 "__builtin_init_heap_trampoline",
10241 ECF_NOTHROW | ECF_LEAF);
10243 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
10244 local_define_builtin ("__builtin_adjust_trampoline", ftype,
10245 BUILT_IN_ADJUST_TRAMPOLINE,
10246 "__builtin_adjust_trampoline",
10247 ECF_CONST | ECF_NOTHROW);
10249 ftype = build_function_type_list (void_type_node,
10250 ptr_type_node, ptr_type_node, NULL_TREE);
10251 local_define_builtin ("__builtin_nonlocal_goto", ftype,
10252 BUILT_IN_NONLOCAL_GOTO,
10253 "__builtin_nonlocal_goto",
10254 ECF_NORETURN | ECF_NOTHROW);
10256 ftype = build_function_type_list (void_type_node,
10257 ptr_type_node, ptr_type_node, NULL_TREE);
10258 local_define_builtin ("__builtin_setjmp_setup", ftype,
10259 BUILT_IN_SETJMP_SETUP,
10260 "__builtin_setjmp_setup", ECF_NOTHROW);
10262 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10263 local_define_builtin ("__builtin_setjmp_receiver", ftype,
10264 BUILT_IN_SETJMP_RECEIVER,
10265 "__builtin_setjmp_receiver", ECF_NOTHROW | ECF_LEAF);
10267 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
10268 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
10269 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
10271 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10272 local_define_builtin ("__builtin_stack_restore", ftype,
10273 BUILT_IN_STACK_RESTORE,
10274 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
10276 /* If there's a possibility that we might use the ARM EABI, build the
10277 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
10278 if (targetm.arm_eabi_unwinder)
10280 ftype = build_function_type_list (void_type_node, NULL_TREE);
10281 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
10282 BUILT_IN_CXA_END_CLEANUP,
10283 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
10286 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10287 local_define_builtin ("__builtin_unwind_resume", ftype,
10288 BUILT_IN_UNWIND_RESUME,
10289 ((targetm_common.except_unwind_info (&global_options)
10290 == UI_SJLJ)
10291 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
10292 ECF_NORETURN);
10294 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
10296 ftype = build_function_type_list (ptr_type_node, integer_type_node,
10297 NULL_TREE);
10298 local_define_builtin ("__builtin_return_address", ftype,
10299 BUILT_IN_RETURN_ADDRESS,
10300 "__builtin_return_address",
10301 ECF_NOTHROW);
10304 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
10305 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10307 ftype = build_function_type_list (void_type_node, ptr_type_node,
10308 ptr_type_node, NULL_TREE);
10309 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
10310 local_define_builtin ("__cyg_profile_func_enter", ftype,
10311 BUILT_IN_PROFILE_FUNC_ENTER,
10312 "__cyg_profile_func_enter", 0);
10313 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10314 local_define_builtin ("__cyg_profile_func_exit", ftype,
10315 BUILT_IN_PROFILE_FUNC_EXIT,
10316 "__cyg_profile_func_exit", 0);
10319 /* The exception object and filter values from the runtime. The argument
10320 must be zero before exception lowering, i.e. from the front end. After
10321 exception lowering, it will be the region number for the exception
10322 landing pad. These functions are PURE instead of CONST to prevent
10323 them from being hoisted past the exception edge that will initialize
10324 its value in the landing pad. */
10325 ftype = build_function_type_list (ptr_type_node,
10326 integer_type_node, NULL_TREE);
10327 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
10328 /* Only use TM_PURE if we we have TM language support. */
10329 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
10330 ecf_flags |= ECF_TM_PURE;
10331 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
10332 "__builtin_eh_pointer", ecf_flags);
10334 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
10335 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
10336 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
10337 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10339 ftype = build_function_type_list (void_type_node,
10340 integer_type_node, integer_type_node,
10341 NULL_TREE);
10342 local_define_builtin ("__builtin_eh_copy_values", ftype,
10343 BUILT_IN_EH_COPY_VALUES,
10344 "__builtin_eh_copy_values", ECF_NOTHROW);
10346 /* Complex multiplication and division. These are handled as builtins
10347 rather than optabs because emit_library_call_value doesn't support
10348 complex. Further, we can do slightly better with folding these
10349 beasties if the real and complex parts of the arguments are separate. */
10351 int mode;
10353 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10355 char mode_name_buf[4], *q;
10356 const char *p;
10357 enum built_in_function mcode, dcode;
10358 tree type, inner_type;
10359 const char *prefix = "__";
10361 if (targetm.libfunc_gnu_prefix)
10362 prefix = "__gnu_";
10364 type = lang_hooks.types.type_for_mode ((machine_mode) mode, 0);
10365 if (type == NULL)
10366 continue;
10367 inner_type = TREE_TYPE (type);
10369 ftype = build_function_type_list (type, inner_type, inner_type,
10370 inner_type, inner_type, NULL_TREE);
10372 mcode = ((enum built_in_function)
10373 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10374 dcode = ((enum built_in_function)
10375 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10377 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10378 *q = TOLOWER (*p);
10379 *q = '\0';
10381 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10382 NULL);
10383 local_define_builtin (built_in_names[mcode], ftype, mcode,
10384 built_in_names[mcode],
10385 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10387 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10388 NULL);
10389 local_define_builtin (built_in_names[dcode], ftype, dcode,
10390 built_in_names[dcode],
10391 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10395 init_internal_fns ();
10398 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10399 better way.
10401 If we requested a pointer to a vector, build up the pointers that
10402 we stripped off while looking for the inner type. Similarly for
10403 return values from functions.
10405 The argument TYPE is the top of the chain, and BOTTOM is the
10406 new type which we will point to. */
10408 tree
10409 reconstruct_complex_type (tree type, tree bottom)
10411 tree inner, outer;
10413 if (TREE_CODE (type) == POINTER_TYPE)
10415 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10416 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10417 TYPE_REF_CAN_ALIAS_ALL (type));
10419 else if (TREE_CODE (type) == REFERENCE_TYPE)
10421 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10422 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10423 TYPE_REF_CAN_ALIAS_ALL (type));
10425 else if (TREE_CODE (type) == ARRAY_TYPE)
10427 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10428 outer = build_array_type (inner, TYPE_DOMAIN (type));
10430 else if (TREE_CODE (type) == FUNCTION_TYPE)
10432 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10433 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
10435 else if (TREE_CODE (type) == METHOD_TYPE)
10437 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10438 /* The build_method_type_directly() routine prepends 'this' to argument list,
10439 so we must compensate by getting rid of it. */
10440 outer
10441 = build_method_type_directly
10442 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10443 inner,
10444 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10446 else if (TREE_CODE (type) == OFFSET_TYPE)
10448 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10449 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10451 else
10452 return bottom;
10454 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10455 TYPE_QUALS (type));
10458 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10459 the inner type. */
10460 tree
10461 build_vector_type_for_mode (tree innertype, machine_mode mode)
10463 int nunits;
10465 switch (GET_MODE_CLASS (mode))
10467 case MODE_VECTOR_INT:
10468 case MODE_VECTOR_FLOAT:
10469 case MODE_VECTOR_FRACT:
10470 case MODE_VECTOR_UFRACT:
10471 case MODE_VECTOR_ACCUM:
10472 case MODE_VECTOR_UACCUM:
10473 nunits = GET_MODE_NUNITS (mode);
10474 break;
10476 case MODE_INT:
10477 /* Check that there are no leftover bits. */
10478 gcc_assert (GET_MODE_BITSIZE (mode)
10479 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10481 nunits = GET_MODE_BITSIZE (mode)
10482 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10483 break;
10485 default:
10486 gcc_unreachable ();
10489 return make_vector_type (innertype, nunits, mode);
10492 /* Similarly, but takes the inner type and number of units, which must be
10493 a power of two. */
10495 tree
10496 build_vector_type (tree innertype, int nunits)
10498 return make_vector_type (innertype, nunits, VOIDmode);
10501 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10503 tree
10504 build_opaque_vector_type (tree innertype, int nunits)
10506 tree t = make_vector_type (innertype, nunits, VOIDmode);
10507 tree cand;
10508 /* We always build the non-opaque variant before the opaque one,
10509 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10510 cand = TYPE_NEXT_VARIANT (t);
10511 if (cand
10512 && TYPE_VECTOR_OPAQUE (cand)
10513 && check_qualified_type (cand, t, TYPE_QUALS (t)))
10514 return cand;
10515 /* Othewise build a variant type and make sure to queue it after
10516 the non-opaque type. */
10517 cand = build_distinct_type_copy (t);
10518 TYPE_VECTOR_OPAQUE (cand) = true;
10519 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10520 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10521 TYPE_NEXT_VARIANT (t) = cand;
10522 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10523 return cand;
10527 /* Given an initializer INIT, return TRUE if INIT is zero or some
10528 aggregate of zeros. Otherwise return FALSE. */
10529 bool
10530 initializer_zerop (const_tree init)
10532 tree elt;
10534 STRIP_NOPS (init);
10536 switch (TREE_CODE (init))
10538 case INTEGER_CST:
10539 return integer_zerop (init);
10541 case REAL_CST:
10542 /* ??? Note that this is not correct for C4X float formats. There,
10543 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10544 negative exponent. */
10545 return real_zerop (init)
10546 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10548 case FIXED_CST:
10549 return fixed_zerop (init);
10551 case COMPLEX_CST:
10552 return integer_zerop (init)
10553 || (real_zerop (init)
10554 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10555 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10557 case VECTOR_CST:
10559 unsigned i;
10560 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10561 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10562 return false;
10563 return true;
10566 case CONSTRUCTOR:
10568 unsigned HOST_WIDE_INT idx;
10570 if (TREE_CLOBBER_P (init))
10571 return false;
10572 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10573 if (!initializer_zerop (elt))
10574 return false;
10575 return true;
10578 case STRING_CST:
10580 int i;
10582 /* We need to loop through all elements to handle cases like
10583 "\0" and "\0foobar". */
10584 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10585 if (TREE_STRING_POINTER (init)[i] != '\0')
10586 return false;
10588 return true;
10591 default:
10592 return false;
10596 /* Check if vector VEC consists of all the equal elements and
10597 that the number of elements corresponds to the type of VEC.
10598 The function returns first element of the vector
10599 or NULL_TREE if the vector is not uniform. */
10600 tree
10601 uniform_vector_p (const_tree vec)
10603 tree first, t;
10604 unsigned i;
10606 if (vec == NULL_TREE)
10607 return NULL_TREE;
10609 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10611 if (TREE_CODE (vec) == VECTOR_CST)
10613 first = VECTOR_CST_ELT (vec, 0);
10614 for (i = 1; i < VECTOR_CST_NELTS (vec); ++i)
10615 if (!operand_equal_p (first, VECTOR_CST_ELT (vec, i), 0))
10616 return NULL_TREE;
10618 return first;
10621 else if (TREE_CODE (vec) == CONSTRUCTOR)
10623 first = error_mark_node;
10625 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10627 if (i == 0)
10629 first = t;
10630 continue;
10632 if (!operand_equal_p (first, t, 0))
10633 return NULL_TREE;
10635 if (i != TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)))
10636 return NULL_TREE;
10638 return first;
10641 return NULL_TREE;
10644 /* Build an empty statement at location LOC. */
10646 tree
10647 build_empty_stmt (location_t loc)
10649 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10650 SET_EXPR_LOCATION (t, loc);
10651 return t;
10655 /* Build an OpenMP clause with code CODE. LOC is the location of the
10656 clause. */
10658 tree
10659 build_omp_clause (location_t loc, enum omp_clause_code code)
10661 tree t;
10662 int size, length;
10664 length = omp_clause_num_ops[code];
10665 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10667 record_node_allocation_statistics (OMP_CLAUSE, size);
10669 t = (tree) ggc_internal_alloc (size);
10670 memset (t, 0, size);
10671 TREE_SET_CODE (t, OMP_CLAUSE);
10672 OMP_CLAUSE_SET_CODE (t, code);
10673 OMP_CLAUSE_LOCATION (t) = loc;
10675 return t;
10678 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10679 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10680 Except for the CODE and operand count field, other storage for the
10681 object is initialized to zeros. */
10683 tree
10684 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10686 tree t;
10687 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10689 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10690 gcc_assert (len >= 1);
10692 record_node_allocation_statistics (code, length);
10694 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10696 TREE_SET_CODE (t, code);
10698 /* Can't use TREE_OPERAND to store the length because if checking is
10699 enabled, it will try to check the length before we store it. :-P */
10700 t->exp.operands[0] = build_int_cst (sizetype, len);
10702 return t;
10705 /* Helper function for build_call_* functions; build a CALL_EXPR with
10706 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10707 the argument slots. */
10709 static tree
10710 build_call_1 (tree return_type, tree fn, int nargs)
10712 tree t;
10714 t = build_vl_exp (CALL_EXPR, nargs + 3);
10715 TREE_TYPE (t) = return_type;
10716 CALL_EXPR_FN (t) = fn;
10717 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10719 return t;
10722 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10723 FN and a null static chain slot. NARGS is the number of call arguments
10724 which are specified as "..." arguments. */
10726 tree
10727 build_call_nary (tree return_type, tree fn, int nargs, ...)
10729 tree ret;
10730 va_list args;
10731 va_start (args, nargs);
10732 ret = build_call_valist (return_type, fn, nargs, args);
10733 va_end (args);
10734 return ret;
10737 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10738 FN and a null static chain slot. NARGS is the number of call arguments
10739 which are specified as a va_list ARGS. */
10741 tree
10742 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10744 tree t;
10745 int i;
10747 t = build_call_1 (return_type, fn, nargs);
10748 for (i = 0; i < nargs; i++)
10749 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10750 process_call_operands (t);
10751 return t;
10754 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10755 FN and a null static chain slot. NARGS is the number of call arguments
10756 which are specified as a tree array ARGS. */
10758 tree
10759 build_call_array_loc (location_t loc, tree return_type, tree fn,
10760 int nargs, const tree *args)
10762 tree t;
10763 int i;
10765 t = build_call_1 (return_type, fn, nargs);
10766 for (i = 0; i < nargs; i++)
10767 CALL_EXPR_ARG (t, i) = args[i];
10768 process_call_operands (t);
10769 SET_EXPR_LOCATION (t, loc);
10770 return t;
10773 /* Like build_call_array, but takes a vec. */
10775 tree
10776 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10778 tree ret, t;
10779 unsigned int ix;
10781 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10782 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10783 CALL_EXPR_ARG (ret, ix) = t;
10784 process_call_operands (ret);
10785 return ret;
10788 /* Conveniently construct a function call expression. FNDECL names the
10789 function to be called and N arguments are passed in the array
10790 ARGARRAY. */
10792 tree
10793 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
10795 tree fntype = TREE_TYPE (fndecl);
10796 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10798 return fold_build_call_array_loc (loc, TREE_TYPE (fntype), fn, n, argarray);
10801 /* Conveniently construct a function call expression. FNDECL names the
10802 function to be called and the arguments are passed in the vector
10803 VEC. */
10805 tree
10806 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
10808 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
10809 vec_safe_address (vec));
10813 /* Conveniently construct a function call expression. FNDECL names the
10814 function to be called, N is the number of arguments, and the "..."
10815 parameters are the argument expressions. */
10817 tree
10818 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
10820 va_list ap;
10821 tree *argarray = XALLOCAVEC (tree, n);
10822 int i;
10824 va_start (ap, n);
10825 for (i = 0; i < n; i++)
10826 argarray[i] = va_arg (ap, tree);
10827 va_end (ap);
10828 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10831 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
10832 varargs macros aren't supported by all bootstrap compilers. */
10834 tree
10835 build_call_expr (tree fndecl, int n, ...)
10837 va_list ap;
10838 tree *argarray = XALLOCAVEC (tree, n);
10839 int i;
10841 va_start (ap, n);
10842 for (i = 0; i < n; i++)
10843 argarray[i] = va_arg (ap, tree);
10844 va_end (ap);
10845 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
10848 /* Build internal call expression. This is just like CALL_EXPR, except
10849 its CALL_EXPR_FN is NULL. It will get gimplified later into ordinary
10850 internal function. */
10852 tree
10853 build_call_expr_internal_loc (location_t loc, enum internal_fn ifn,
10854 tree type, int n, ...)
10856 va_list ap;
10857 int i;
10859 tree fn = build_call_1 (type, NULL_TREE, n);
10860 va_start (ap, n);
10861 for (i = 0; i < n; i++)
10862 CALL_EXPR_ARG (fn, i) = va_arg (ap, tree);
10863 va_end (ap);
10864 SET_EXPR_LOCATION (fn, loc);
10865 CALL_EXPR_IFN (fn) = ifn;
10866 return fn;
10869 /* Create a new constant string literal and return a char* pointer to it.
10870 The STRING_CST value is the LEN characters at STR. */
10871 tree
10872 build_string_literal (int len, const char *str)
10874 tree t, elem, index, type;
10876 t = build_string (len, str);
10877 elem = build_type_variant (char_type_node, 1, 0);
10878 index = build_index_type (size_int (len - 1));
10879 type = build_array_type (elem, index);
10880 TREE_TYPE (t) = type;
10881 TREE_CONSTANT (t) = 1;
10882 TREE_READONLY (t) = 1;
10883 TREE_STATIC (t) = 1;
10885 type = build_pointer_type (elem);
10886 t = build1 (ADDR_EXPR, type,
10887 build4 (ARRAY_REF, elem,
10888 t, integer_zero_node, NULL_TREE, NULL_TREE));
10889 return t;
10894 /* Return true if T (assumed to be a DECL) must be assigned a memory
10895 location. */
10897 bool
10898 needs_to_live_in_memory (const_tree t)
10900 return (TREE_ADDRESSABLE (t)
10901 || is_global_var (t)
10902 || (TREE_CODE (t) == RESULT_DECL
10903 && !DECL_BY_REFERENCE (t)
10904 && aggregate_value_p (t, current_function_decl)));
10907 /* Return value of a constant X and sign-extend it. */
10909 HOST_WIDE_INT
10910 int_cst_value (const_tree x)
10912 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10913 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10915 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10916 gcc_assert (cst_and_fits_in_hwi (x));
10918 if (bits < HOST_BITS_PER_WIDE_INT)
10920 bool negative = ((val >> (bits - 1)) & 1) != 0;
10921 if (negative)
10922 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10923 else
10924 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10927 return val;
10930 /* If TYPE is an integral or pointer type, return an integer type with
10931 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10932 if TYPE is already an integer type of signedness UNSIGNEDP. */
10934 tree
10935 signed_or_unsigned_type_for (int unsignedp, tree type)
10937 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10938 return type;
10940 if (TREE_CODE (type) == VECTOR_TYPE)
10942 tree inner = TREE_TYPE (type);
10943 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10944 if (!inner2)
10945 return NULL_TREE;
10946 if (inner == inner2)
10947 return type;
10948 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10951 if (!INTEGRAL_TYPE_P (type)
10952 && !POINTER_TYPE_P (type)
10953 && TREE_CODE (type) != OFFSET_TYPE)
10954 return NULL_TREE;
10956 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10959 /* If TYPE is an integral or pointer type, return an integer type with
10960 the same precision which is unsigned, or itself if TYPE is already an
10961 unsigned integer type. */
10963 tree
10964 unsigned_type_for (tree type)
10966 return signed_or_unsigned_type_for (1, type);
10969 /* If TYPE is an integral or pointer type, return an integer type with
10970 the same precision which is signed, or itself if TYPE is already a
10971 signed integer type. */
10973 tree
10974 signed_type_for (tree type)
10976 return signed_or_unsigned_type_for (0, type);
10979 /* If TYPE is a vector type, return a signed integer vector type with the
10980 same width and number of subparts. Otherwise return boolean_type_node. */
10982 tree
10983 truth_type_for (tree type)
10985 if (TREE_CODE (type) == VECTOR_TYPE)
10987 tree elem = lang_hooks.types.type_for_size
10988 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10989 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10991 else
10992 return boolean_type_node;
10995 /* Returns the largest value obtainable by casting something in INNER type to
10996 OUTER type. */
10998 tree
10999 upper_bound_in_type (tree outer, tree inner)
11001 unsigned int det = 0;
11002 unsigned oprec = TYPE_PRECISION (outer);
11003 unsigned iprec = TYPE_PRECISION (inner);
11004 unsigned prec;
11006 /* Compute a unique number for every combination. */
11007 det |= (oprec > iprec) ? 4 : 0;
11008 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
11009 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
11011 /* Determine the exponent to use. */
11012 switch (det)
11014 case 0:
11015 case 1:
11016 /* oprec <= iprec, outer: signed, inner: don't care. */
11017 prec = oprec - 1;
11018 break;
11019 case 2:
11020 case 3:
11021 /* oprec <= iprec, outer: unsigned, inner: don't care. */
11022 prec = oprec;
11023 break;
11024 case 4:
11025 /* oprec > iprec, outer: signed, inner: signed. */
11026 prec = iprec - 1;
11027 break;
11028 case 5:
11029 /* oprec > iprec, outer: signed, inner: unsigned. */
11030 prec = iprec;
11031 break;
11032 case 6:
11033 /* oprec > iprec, outer: unsigned, inner: signed. */
11034 prec = oprec;
11035 break;
11036 case 7:
11037 /* oprec > iprec, outer: unsigned, inner: unsigned. */
11038 prec = iprec;
11039 break;
11040 default:
11041 gcc_unreachable ();
11044 return wide_int_to_tree (outer,
11045 wi::mask (prec, false, TYPE_PRECISION (outer)));
11048 /* Returns the smallest value obtainable by casting something in INNER type to
11049 OUTER type. */
11051 tree
11052 lower_bound_in_type (tree outer, tree inner)
11054 unsigned oprec = TYPE_PRECISION (outer);
11055 unsigned iprec = TYPE_PRECISION (inner);
11057 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
11058 and obtain 0. */
11059 if (TYPE_UNSIGNED (outer)
11060 /* If we are widening something of an unsigned type, OUTER type
11061 contains all values of INNER type. In particular, both INNER
11062 and OUTER types have zero in common. */
11063 || (oprec > iprec && TYPE_UNSIGNED (inner)))
11064 return build_int_cst (outer, 0);
11065 else
11067 /* If we are widening a signed type to another signed type, we
11068 want to obtain -2^^(iprec-1). If we are keeping the
11069 precision or narrowing to a signed type, we want to obtain
11070 -2^(oprec-1). */
11071 unsigned prec = oprec > iprec ? iprec : oprec;
11072 return wide_int_to_tree (outer,
11073 wi::mask (prec - 1, true,
11074 TYPE_PRECISION (outer)));
11078 /* Return nonzero if two operands that are suitable for PHI nodes are
11079 necessarily equal. Specifically, both ARG0 and ARG1 must be either
11080 SSA_NAME or invariant. Note that this is strictly an optimization.
11081 That is, callers of this function can directly call operand_equal_p
11082 and get the same result, only slower. */
11085 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
11087 if (arg0 == arg1)
11088 return 1;
11089 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
11090 return 0;
11091 return operand_equal_p (arg0, arg1, 0);
11094 /* Returns number of zeros at the end of binary representation of X. */
11096 tree
11097 num_ending_zeros (const_tree x)
11099 return build_int_cst (TREE_TYPE (x), wi::ctz (x));
11103 #define WALK_SUBTREE(NODE) \
11104 do \
11106 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
11107 if (result) \
11108 return result; \
11110 while (0)
11112 /* This is a subroutine of walk_tree that walks field of TYPE that are to
11113 be walked whenever a type is seen in the tree. Rest of operands and return
11114 value are as for walk_tree. */
11116 static tree
11117 walk_type_fields (tree type, walk_tree_fn func, void *data,
11118 hash_set<tree> *pset, walk_tree_lh lh)
11120 tree result = NULL_TREE;
11122 switch (TREE_CODE (type))
11124 case POINTER_TYPE:
11125 case REFERENCE_TYPE:
11126 case VECTOR_TYPE:
11127 /* We have to worry about mutually recursive pointers. These can't
11128 be written in C. They can in Ada. It's pathological, but
11129 there's an ACATS test (c38102a) that checks it. Deal with this
11130 by checking if we're pointing to another pointer, that one
11131 points to another pointer, that one does too, and we have no htab.
11132 If so, get a hash table. We check three levels deep to avoid
11133 the cost of the hash table if we don't need one. */
11134 if (POINTER_TYPE_P (TREE_TYPE (type))
11135 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
11136 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
11137 && !pset)
11139 result = walk_tree_without_duplicates (&TREE_TYPE (type),
11140 func, data);
11141 if (result)
11142 return result;
11144 break;
11147 /* ... fall through ... */
11149 case COMPLEX_TYPE:
11150 WALK_SUBTREE (TREE_TYPE (type));
11151 break;
11153 case METHOD_TYPE:
11154 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
11156 /* Fall through. */
11158 case FUNCTION_TYPE:
11159 WALK_SUBTREE (TREE_TYPE (type));
11161 tree arg;
11163 /* We never want to walk into default arguments. */
11164 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
11165 WALK_SUBTREE (TREE_VALUE (arg));
11167 break;
11169 case ARRAY_TYPE:
11170 /* Don't follow this nodes's type if a pointer for fear that
11171 we'll have infinite recursion. If we have a PSET, then we
11172 need not fear. */
11173 if (pset
11174 || (!POINTER_TYPE_P (TREE_TYPE (type))
11175 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
11176 WALK_SUBTREE (TREE_TYPE (type));
11177 WALK_SUBTREE (TYPE_DOMAIN (type));
11178 break;
11180 case OFFSET_TYPE:
11181 WALK_SUBTREE (TREE_TYPE (type));
11182 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
11183 break;
11185 default:
11186 break;
11189 return NULL_TREE;
11192 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
11193 called with the DATA and the address of each sub-tree. If FUNC returns a
11194 non-NULL value, the traversal is stopped, and the value returned by FUNC
11195 is returned. If PSET is non-NULL it is used to record the nodes visited,
11196 and to avoid visiting a node more than once. */
11198 tree
11199 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
11200 hash_set<tree> *pset, walk_tree_lh lh)
11202 enum tree_code code;
11203 int walk_subtrees;
11204 tree result;
11206 #define WALK_SUBTREE_TAIL(NODE) \
11207 do \
11209 tp = & (NODE); \
11210 goto tail_recurse; \
11212 while (0)
11214 tail_recurse:
11215 /* Skip empty subtrees. */
11216 if (!*tp)
11217 return NULL_TREE;
11219 /* Don't walk the same tree twice, if the user has requested
11220 that we avoid doing so. */
11221 if (pset && pset->add (*tp))
11222 return NULL_TREE;
11224 /* Call the function. */
11225 walk_subtrees = 1;
11226 result = (*func) (tp, &walk_subtrees, data);
11228 /* If we found something, return it. */
11229 if (result)
11230 return result;
11232 code = TREE_CODE (*tp);
11234 /* Even if we didn't, FUNC may have decided that there was nothing
11235 interesting below this point in the tree. */
11236 if (!walk_subtrees)
11238 /* But we still need to check our siblings. */
11239 if (code == TREE_LIST)
11240 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11241 else if (code == OMP_CLAUSE)
11242 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11243 else
11244 return NULL_TREE;
11247 if (lh)
11249 result = (*lh) (tp, &walk_subtrees, func, data, pset);
11250 if (result || !walk_subtrees)
11251 return result;
11254 switch (code)
11256 case ERROR_MARK:
11257 case IDENTIFIER_NODE:
11258 case INTEGER_CST:
11259 case REAL_CST:
11260 case FIXED_CST:
11261 case VECTOR_CST:
11262 case STRING_CST:
11263 case BLOCK:
11264 case PLACEHOLDER_EXPR:
11265 case SSA_NAME:
11266 case FIELD_DECL:
11267 case RESULT_DECL:
11268 /* None of these have subtrees other than those already walked
11269 above. */
11270 break;
11272 case TREE_LIST:
11273 WALK_SUBTREE (TREE_VALUE (*tp));
11274 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11275 break;
11277 case TREE_VEC:
11279 int len = TREE_VEC_LENGTH (*tp);
11281 if (len == 0)
11282 break;
11284 /* Walk all elements but the first. */
11285 while (--len)
11286 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
11288 /* Now walk the first one as a tail call. */
11289 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
11292 case COMPLEX_CST:
11293 WALK_SUBTREE (TREE_REALPART (*tp));
11294 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
11296 case CONSTRUCTOR:
11298 unsigned HOST_WIDE_INT idx;
11299 constructor_elt *ce;
11301 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*tp), idx, &ce);
11302 idx++)
11303 WALK_SUBTREE (ce->value);
11305 break;
11307 case SAVE_EXPR:
11308 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
11310 case BIND_EXPR:
11312 tree decl;
11313 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
11315 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11316 into declarations that are just mentioned, rather than
11317 declared; they don't really belong to this part of the tree.
11318 And, we can see cycles: the initializer for a declaration
11319 can refer to the declaration itself. */
11320 WALK_SUBTREE (DECL_INITIAL (decl));
11321 WALK_SUBTREE (DECL_SIZE (decl));
11322 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11324 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
11327 case STATEMENT_LIST:
11329 tree_stmt_iterator i;
11330 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
11331 WALK_SUBTREE (*tsi_stmt_ptr (i));
11333 break;
11335 case OMP_CLAUSE:
11336 switch (OMP_CLAUSE_CODE (*tp))
11338 case OMP_CLAUSE_GANG:
11339 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11340 /* FALLTHRU */
11342 case OMP_CLAUSE_DEVICE_RESIDENT:
11343 case OMP_CLAUSE_USE_DEVICE:
11344 case OMP_CLAUSE_ASYNC:
11345 case OMP_CLAUSE_WAIT:
11346 case OMP_CLAUSE_WORKER:
11347 case OMP_CLAUSE_VECTOR:
11348 case OMP_CLAUSE_NUM_GANGS:
11349 case OMP_CLAUSE_NUM_WORKERS:
11350 case OMP_CLAUSE_VECTOR_LENGTH:
11351 case OMP_CLAUSE_PRIVATE:
11352 case OMP_CLAUSE_SHARED:
11353 case OMP_CLAUSE_FIRSTPRIVATE:
11354 case OMP_CLAUSE_COPYIN:
11355 case OMP_CLAUSE_COPYPRIVATE:
11356 case OMP_CLAUSE_FINAL:
11357 case OMP_CLAUSE_IF:
11358 case OMP_CLAUSE_NUM_THREADS:
11359 case OMP_CLAUSE_SCHEDULE:
11360 case OMP_CLAUSE_UNIFORM:
11361 case OMP_CLAUSE_DEPEND:
11362 case OMP_CLAUSE_NUM_TEAMS:
11363 case OMP_CLAUSE_THREAD_LIMIT:
11364 case OMP_CLAUSE_DEVICE:
11365 case OMP_CLAUSE_DIST_SCHEDULE:
11366 case OMP_CLAUSE_SAFELEN:
11367 case OMP_CLAUSE_SIMDLEN:
11368 case OMP_CLAUSE__LOOPTEMP_:
11369 case OMP_CLAUSE__SIMDUID_:
11370 case OMP_CLAUSE__CILK_FOR_COUNT_:
11371 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
11372 /* FALLTHRU */
11374 case OMP_CLAUSE_INDEPENDENT:
11375 case OMP_CLAUSE_NOWAIT:
11376 case OMP_CLAUSE_ORDERED:
11377 case OMP_CLAUSE_DEFAULT:
11378 case OMP_CLAUSE_UNTIED:
11379 case OMP_CLAUSE_MERGEABLE:
11380 case OMP_CLAUSE_PROC_BIND:
11381 case OMP_CLAUSE_INBRANCH:
11382 case OMP_CLAUSE_NOTINBRANCH:
11383 case OMP_CLAUSE_FOR:
11384 case OMP_CLAUSE_PARALLEL:
11385 case OMP_CLAUSE_SECTIONS:
11386 case OMP_CLAUSE_TASKGROUP:
11387 case OMP_CLAUSE_AUTO:
11388 case OMP_CLAUSE_SEQ:
11389 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11391 case OMP_CLAUSE_LASTPRIVATE:
11392 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11393 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
11394 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11396 case OMP_CLAUSE_COLLAPSE:
11398 int i;
11399 for (i = 0; i < 3; i++)
11400 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11401 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11404 case OMP_CLAUSE_LINEAR:
11405 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11406 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STEP (*tp));
11407 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STMT (*tp));
11408 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11410 case OMP_CLAUSE_ALIGNED:
11411 case OMP_CLAUSE_FROM:
11412 case OMP_CLAUSE_TO:
11413 case OMP_CLAUSE_MAP:
11414 case OMP_CLAUSE__CACHE_:
11415 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11416 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11417 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11419 case OMP_CLAUSE_REDUCTION:
11421 int i;
11422 for (i = 0; i < 4; i++)
11423 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11424 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11427 default:
11428 gcc_unreachable ();
11430 break;
11432 case TARGET_EXPR:
11434 int i, len;
11436 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11437 But, we only want to walk once. */
11438 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11439 for (i = 0; i < len; ++i)
11440 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11441 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11444 case DECL_EXPR:
11445 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11446 defining. We only want to walk into these fields of a type in this
11447 case and not in the general case of a mere reference to the type.
11449 The criterion is as follows: if the field can be an expression, it
11450 must be walked only here. This should be in keeping with the fields
11451 that are directly gimplified in gimplify_type_sizes in order for the
11452 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11453 variable-sized types.
11455 Note that DECLs get walked as part of processing the BIND_EXPR. */
11456 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11458 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11459 if (TREE_CODE (*type_p) == ERROR_MARK)
11460 return NULL_TREE;
11462 /* Call the function for the type. See if it returns anything or
11463 doesn't want us to continue. If we are to continue, walk both
11464 the normal fields and those for the declaration case. */
11465 result = (*func) (type_p, &walk_subtrees, data);
11466 if (result || !walk_subtrees)
11467 return result;
11469 /* But do not walk a pointed-to type since it may itself need to
11470 be walked in the declaration case if it isn't anonymous. */
11471 if (!POINTER_TYPE_P (*type_p))
11473 result = walk_type_fields (*type_p, func, data, pset, lh);
11474 if (result)
11475 return result;
11478 /* If this is a record type, also walk the fields. */
11479 if (RECORD_OR_UNION_TYPE_P (*type_p))
11481 tree field;
11483 for (field = TYPE_FIELDS (*type_p); field;
11484 field = DECL_CHAIN (field))
11486 /* We'd like to look at the type of the field, but we can
11487 easily get infinite recursion. So assume it's pointed
11488 to elsewhere in the tree. Also, ignore things that
11489 aren't fields. */
11490 if (TREE_CODE (field) != FIELD_DECL)
11491 continue;
11493 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11494 WALK_SUBTREE (DECL_SIZE (field));
11495 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11496 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11497 WALK_SUBTREE (DECL_QUALIFIER (field));
11501 /* Same for scalar types. */
11502 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11503 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11504 || TREE_CODE (*type_p) == INTEGER_TYPE
11505 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11506 || TREE_CODE (*type_p) == REAL_TYPE)
11508 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11509 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11512 WALK_SUBTREE (TYPE_SIZE (*type_p));
11513 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11515 /* FALLTHRU */
11517 default:
11518 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11520 int i, len;
11522 /* Walk over all the sub-trees of this operand. */
11523 len = TREE_OPERAND_LENGTH (*tp);
11525 /* Go through the subtrees. We need to do this in forward order so
11526 that the scope of a FOR_EXPR is handled properly. */
11527 if (len)
11529 for (i = 0; i < len - 1; ++i)
11530 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11531 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11534 /* If this is a type, walk the needed fields in the type. */
11535 else if (TYPE_P (*tp))
11536 return walk_type_fields (*tp, func, data, pset, lh);
11537 break;
11540 /* We didn't find what we were looking for. */
11541 return NULL_TREE;
11543 #undef WALK_SUBTREE_TAIL
11545 #undef WALK_SUBTREE
11547 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11549 tree
11550 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11551 walk_tree_lh lh)
11553 tree result;
11555 hash_set<tree> pset;
11556 result = walk_tree_1 (tp, func, data, &pset, lh);
11557 return result;
11561 tree
11562 tree_block (tree t)
11564 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11566 if (IS_EXPR_CODE_CLASS (c))
11567 return LOCATION_BLOCK (t->exp.locus);
11568 gcc_unreachable ();
11569 return NULL;
11572 void
11573 tree_set_block (tree t, tree b)
11575 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11577 if (IS_EXPR_CODE_CLASS (c))
11579 if (b)
11580 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
11581 else
11582 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
11584 else
11585 gcc_unreachable ();
11588 /* Create a nameless artificial label and put it in the current
11589 function context. The label has a location of LOC. Returns the
11590 newly created label. */
11592 tree
11593 create_artificial_label (location_t loc)
11595 tree lab = build_decl (loc,
11596 LABEL_DECL, NULL_TREE, void_type_node);
11598 DECL_ARTIFICIAL (lab) = 1;
11599 DECL_IGNORED_P (lab) = 1;
11600 DECL_CONTEXT (lab) = current_function_decl;
11601 return lab;
11604 /* Given a tree, try to return a useful variable name that we can use
11605 to prefix a temporary that is being assigned the value of the tree.
11606 I.E. given <temp> = &A, return A. */
11608 const char *
11609 get_name (tree t)
11611 tree stripped_decl;
11613 stripped_decl = t;
11614 STRIP_NOPS (stripped_decl);
11615 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11616 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11617 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11619 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11620 if (!name)
11621 return NULL;
11622 return IDENTIFIER_POINTER (name);
11624 else
11626 switch (TREE_CODE (stripped_decl))
11628 case ADDR_EXPR:
11629 return get_name (TREE_OPERAND (stripped_decl, 0));
11630 default:
11631 return NULL;
11636 /* Return true if TYPE has a variable argument list. */
11638 bool
11639 stdarg_p (const_tree fntype)
11641 function_args_iterator args_iter;
11642 tree n = NULL_TREE, t;
11644 if (!fntype)
11645 return false;
11647 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
11649 n = t;
11652 return n != NULL_TREE && n != void_type_node;
11655 /* Return true if TYPE has a prototype. */
11657 bool
11658 prototype_p (const_tree fntype)
11660 tree t;
11662 gcc_assert (fntype != NULL_TREE);
11664 t = TYPE_ARG_TYPES (fntype);
11665 return (t != NULL_TREE);
11668 /* If BLOCK is inlined from an __attribute__((__artificial__))
11669 routine, return pointer to location from where it has been
11670 called. */
11671 location_t *
11672 block_nonartificial_location (tree block)
11674 location_t *ret = NULL;
11676 while (block && TREE_CODE (block) == BLOCK
11677 && BLOCK_ABSTRACT_ORIGIN (block))
11679 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11681 while (TREE_CODE (ao) == BLOCK
11682 && BLOCK_ABSTRACT_ORIGIN (ao)
11683 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11684 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11686 if (TREE_CODE (ao) == FUNCTION_DECL)
11688 /* If AO is an artificial inline, point RET to the
11689 call site locus at which it has been inlined and continue
11690 the loop, in case AO's caller is also an artificial
11691 inline. */
11692 if (DECL_DECLARED_INLINE_P (ao)
11693 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11694 ret = &BLOCK_SOURCE_LOCATION (block);
11695 else
11696 break;
11698 else if (TREE_CODE (ao) != BLOCK)
11699 break;
11701 block = BLOCK_SUPERCONTEXT (block);
11703 return ret;
11707 /* If EXP is inlined from an __attribute__((__artificial__))
11708 function, return the location of the original call expression. */
11710 location_t
11711 tree_nonartificial_location (tree exp)
11713 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11715 if (loc)
11716 return *loc;
11717 else
11718 return EXPR_LOCATION (exp);
11722 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11723 nodes. */
11725 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11727 hashval_t
11728 cl_option_hasher::hash (tree x)
11730 const_tree const t = x;
11731 const char *p;
11732 size_t i;
11733 size_t len = 0;
11734 hashval_t hash = 0;
11736 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11738 p = (const char *)TREE_OPTIMIZATION (t);
11739 len = sizeof (struct cl_optimization);
11742 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11743 return cl_target_option_hash (TREE_TARGET_OPTION (t));
11745 else
11746 gcc_unreachable ();
11748 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11749 something else. */
11750 for (i = 0; i < len; i++)
11751 if (p[i])
11752 hash = (hash << 4) ^ ((i << 2) | p[i]);
11754 return hash;
11757 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11758 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11759 same. */
11761 bool
11762 cl_option_hasher::equal (tree x, tree y)
11764 const_tree const xt = x;
11765 const_tree const yt = y;
11766 const char *xp;
11767 const char *yp;
11768 size_t len;
11770 if (TREE_CODE (xt) != TREE_CODE (yt))
11771 return 0;
11773 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11775 xp = (const char *)TREE_OPTIMIZATION (xt);
11776 yp = (const char *)TREE_OPTIMIZATION (yt);
11777 len = sizeof (struct cl_optimization);
11780 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11782 return cl_target_option_eq (TREE_TARGET_OPTION (xt),
11783 TREE_TARGET_OPTION (yt));
11786 else
11787 gcc_unreachable ();
11789 return (memcmp (xp, yp, len) == 0);
11792 /* Build an OPTIMIZATION_NODE based on the options in OPTS. */
11794 tree
11795 build_optimization_node (struct gcc_options *opts)
11797 tree t;
11799 /* Use the cache of optimization nodes. */
11801 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11802 opts);
11804 tree *slot = cl_option_hash_table->find_slot (cl_optimization_node, INSERT);
11805 t = *slot;
11806 if (!t)
11808 /* Insert this one into the hash table. */
11809 t = cl_optimization_node;
11810 *slot = t;
11812 /* Make a new node for next time round. */
11813 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11816 return t;
11819 /* Build a TARGET_OPTION_NODE based on the options in OPTS. */
11821 tree
11822 build_target_option_node (struct gcc_options *opts)
11824 tree t;
11826 /* Use the cache of optimization nodes. */
11828 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11829 opts);
11831 tree *slot = cl_option_hash_table->find_slot (cl_target_option_node, INSERT);
11832 t = *slot;
11833 if (!t)
11835 /* Insert this one into the hash table. */
11836 t = cl_target_option_node;
11837 *slot = t;
11839 /* Make a new node for next time round. */
11840 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11843 return t;
11846 /* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
11847 so that they aren't saved during PCH writing. */
11849 void
11850 prepare_target_option_nodes_for_pch (void)
11852 hash_table<cl_option_hasher>::iterator iter = cl_option_hash_table->begin ();
11853 for (; iter != cl_option_hash_table->end (); ++iter)
11854 if (TREE_CODE (*iter) == TARGET_OPTION_NODE)
11855 TREE_TARGET_GLOBALS (*iter) = NULL;
11858 /* Determine the "ultimate origin" of a block. The block may be an inlined
11859 instance of an inlined instance of a block which is local to an inline
11860 function, so we have to trace all of the way back through the origin chain
11861 to find out what sort of node actually served as the original seed for the
11862 given block. */
11864 tree
11865 block_ultimate_origin (const_tree block)
11867 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11869 /* BLOCK_ABSTRACT_ORIGIN can point to itself; ignore that if
11870 we're trying to output the abstract instance of this function. */
11871 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11872 return NULL_TREE;
11874 if (immediate_origin == NULL_TREE)
11875 return NULL_TREE;
11876 else
11878 tree ret_val;
11879 tree lookahead = immediate_origin;
11883 ret_val = lookahead;
11884 lookahead = (TREE_CODE (ret_val) == BLOCK
11885 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11887 while (lookahead != NULL && lookahead != ret_val);
11889 /* The block's abstract origin chain may not be the *ultimate* origin of
11890 the block. It could lead to a DECL that has an abstract origin set.
11891 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11892 will give us if it has one). Note that DECL's abstract origins are
11893 supposed to be the most distant ancestor (or so decl_ultimate_origin
11894 claims), so we don't need to loop following the DECL origins. */
11895 if (DECL_P (ret_val))
11896 return DECL_ORIGIN (ret_val);
11898 return ret_val;
11902 /* Return true iff conversion from INNER_TYPE to OUTER_TYPE generates
11903 no instruction. */
11905 bool
11906 tree_nop_conversion_p (const_tree outer_type, const_tree inner_type)
11908 /* Use precision rather then machine mode when we can, which gives
11909 the correct answer even for submode (bit-field) types. */
11910 if ((INTEGRAL_TYPE_P (outer_type)
11911 || POINTER_TYPE_P (outer_type)
11912 || TREE_CODE (outer_type) == OFFSET_TYPE)
11913 && (INTEGRAL_TYPE_P (inner_type)
11914 || POINTER_TYPE_P (inner_type)
11915 || TREE_CODE (inner_type) == OFFSET_TYPE))
11916 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11918 /* Otherwise fall back on comparing machine modes (e.g. for
11919 aggregate types, floats). */
11920 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11923 /* Return true iff conversion in EXP generates no instruction. Mark
11924 it inline so that we fully inline into the stripping functions even
11925 though we have two uses of this function. */
11927 static inline bool
11928 tree_nop_conversion (const_tree exp)
11930 tree outer_type, inner_type;
11932 if (!CONVERT_EXPR_P (exp)
11933 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11934 return false;
11935 if (TREE_OPERAND (exp, 0) == error_mark_node)
11936 return false;
11938 outer_type = TREE_TYPE (exp);
11939 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11941 if (!inner_type)
11942 return false;
11944 return tree_nop_conversion_p (outer_type, inner_type);
11947 /* Return true iff conversion in EXP generates no instruction. Don't
11948 consider conversions changing the signedness. */
11950 static bool
11951 tree_sign_nop_conversion (const_tree exp)
11953 tree outer_type, inner_type;
11955 if (!tree_nop_conversion (exp))
11956 return false;
11958 outer_type = TREE_TYPE (exp);
11959 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11961 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11962 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11965 /* Strip conversions from EXP according to tree_nop_conversion and
11966 return the resulting expression. */
11968 tree
11969 tree_strip_nop_conversions (tree exp)
11971 while (tree_nop_conversion (exp))
11972 exp = TREE_OPERAND (exp, 0);
11973 return exp;
11976 /* Strip conversions from EXP according to tree_sign_nop_conversion
11977 and return the resulting expression. */
11979 tree
11980 tree_strip_sign_nop_conversions (tree exp)
11982 while (tree_sign_nop_conversion (exp))
11983 exp = TREE_OPERAND (exp, 0);
11984 return exp;
11987 /* Avoid any floating point extensions from EXP. */
11988 tree
11989 strip_float_extensions (tree exp)
11991 tree sub, expt, subt;
11993 /* For floating point constant look up the narrowest type that can hold
11994 it properly and handle it like (type)(narrowest_type)constant.
11995 This way we can optimize for instance a=a*2.0 where "a" is float
11996 but 2.0 is double constant. */
11997 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11999 REAL_VALUE_TYPE orig;
12000 tree type = NULL;
12002 orig = TREE_REAL_CST (exp);
12003 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
12004 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
12005 type = float_type_node;
12006 else if (TYPE_PRECISION (TREE_TYPE (exp))
12007 > TYPE_PRECISION (double_type_node)
12008 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
12009 type = double_type_node;
12010 if (type)
12011 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
12014 if (!CONVERT_EXPR_P (exp))
12015 return exp;
12017 sub = TREE_OPERAND (exp, 0);
12018 subt = TREE_TYPE (sub);
12019 expt = TREE_TYPE (exp);
12021 if (!FLOAT_TYPE_P (subt))
12022 return exp;
12024 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
12025 return exp;
12027 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
12028 return exp;
12030 return strip_float_extensions (sub);
12033 /* Strip out all handled components that produce invariant
12034 offsets. */
12036 const_tree
12037 strip_invariant_refs (const_tree op)
12039 while (handled_component_p (op))
12041 switch (TREE_CODE (op))
12043 case ARRAY_REF:
12044 case ARRAY_RANGE_REF:
12045 if (!is_gimple_constant (TREE_OPERAND (op, 1))
12046 || TREE_OPERAND (op, 2) != NULL_TREE
12047 || TREE_OPERAND (op, 3) != NULL_TREE)
12048 return NULL;
12049 break;
12051 case COMPONENT_REF:
12052 if (TREE_OPERAND (op, 2) != NULL_TREE)
12053 return NULL;
12054 break;
12056 default:;
12058 op = TREE_OPERAND (op, 0);
12061 return op;
12064 static GTY(()) tree gcc_eh_personality_decl;
12066 /* Return the GCC personality function decl. */
12068 tree
12069 lhd_gcc_personality (void)
12071 if (!gcc_eh_personality_decl)
12072 gcc_eh_personality_decl = build_personality_function ("gcc");
12073 return gcc_eh_personality_decl;
12076 /* TARGET is a call target of GIMPLE call statement
12077 (obtained by gimple_call_fn). Return true if it is
12078 OBJ_TYPE_REF representing an virtual call of C++ method.
12079 (As opposed to OBJ_TYPE_REF representing objc calls
12080 through a cast where middle-end devirtualization machinery
12081 can't apply.) */
12083 bool
12084 virtual_method_call_p (const_tree target)
12086 if (TREE_CODE (target) != OBJ_TYPE_REF)
12087 return false;
12088 tree t = TREE_TYPE (target);
12089 gcc_checking_assert (TREE_CODE (t) == POINTER_TYPE);
12090 t = TREE_TYPE (t);
12091 if (TREE_CODE (t) == FUNCTION_TYPE)
12092 return false;
12093 gcc_checking_assert (TREE_CODE (t) == METHOD_TYPE);
12094 /* If we do not have BINFO associated, it means that type was built
12095 without devirtualization enabled. Do not consider this a virtual
12096 call. */
12097 if (!TYPE_BINFO (obj_type_ref_class (target)))
12098 return false;
12099 return true;
12102 /* REF is OBJ_TYPE_REF, return the class the ref corresponds to. */
12104 tree
12105 obj_type_ref_class (const_tree ref)
12107 gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF);
12108 ref = TREE_TYPE (ref);
12109 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
12110 ref = TREE_TYPE (ref);
12111 /* We look for type THIS points to. ObjC also builds
12112 OBJ_TYPE_REF with non-method calls, Their first parameter
12113 ID however also corresponds to class type. */
12114 gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE
12115 || TREE_CODE (ref) == FUNCTION_TYPE);
12116 ref = TREE_VALUE (TYPE_ARG_TYPES (ref));
12117 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
12118 return TREE_TYPE (ref);
12121 /* Lookup sub-BINFO of BINFO of TYPE at offset POS. */
12123 static tree
12124 lookup_binfo_at_offset (tree binfo, tree type, HOST_WIDE_INT pos)
12126 unsigned int i;
12127 tree base_binfo, b;
12129 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12130 if (pos == tree_to_shwi (BINFO_OFFSET (base_binfo))
12131 && types_same_for_odr (TREE_TYPE (base_binfo), type))
12132 return base_binfo;
12133 else if ((b = lookup_binfo_at_offset (base_binfo, type, pos)) != NULL)
12134 return b;
12135 return NULL;
12138 /* Try to find a base info of BINFO that would have its field decl at offset
12139 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
12140 found, return, otherwise return NULL_TREE. */
12142 tree
12143 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
12145 tree type = BINFO_TYPE (binfo);
12147 while (true)
12149 HOST_WIDE_INT pos, size;
12150 tree fld;
12151 int i;
12153 if (types_same_for_odr (type, expected_type))
12154 return binfo;
12155 if (offset < 0)
12156 return NULL_TREE;
12158 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
12160 if (TREE_CODE (fld) != FIELD_DECL || !DECL_ARTIFICIAL (fld))
12161 continue;
12163 pos = int_bit_position (fld);
12164 size = tree_to_uhwi (DECL_SIZE (fld));
12165 if (pos <= offset && (pos + size) > offset)
12166 break;
12168 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
12169 return NULL_TREE;
12171 /* Offset 0 indicates the primary base, whose vtable contents are
12172 represented in the binfo for the derived class. */
12173 else if (offset != 0)
12175 tree found_binfo = NULL, base_binfo;
12176 /* Offsets in BINFO are in bytes relative to the whole structure
12177 while POS is in bits relative to the containing field. */
12178 int binfo_offset = (tree_to_shwi (BINFO_OFFSET (binfo)) + pos
12179 / BITS_PER_UNIT);
12181 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12182 if (tree_to_shwi (BINFO_OFFSET (base_binfo)) == binfo_offset
12183 && types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
12185 found_binfo = base_binfo;
12186 break;
12188 if (found_binfo)
12189 binfo = found_binfo;
12190 else
12191 binfo = lookup_binfo_at_offset (binfo, TREE_TYPE (fld),
12192 binfo_offset);
12195 type = TREE_TYPE (fld);
12196 offset -= pos;
12200 /* Returns true if X is a typedef decl. */
12202 bool
12203 is_typedef_decl (const_tree x)
12205 return (x && TREE_CODE (x) == TYPE_DECL
12206 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
12209 /* Returns true iff TYPE is a type variant created for a typedef. */
12211 bool
12212 typedef_variant_p (const_tree type)
12214 return is_typedef_decl (TYPE_NAME (type));
12217 /* Warn about a use of an identifier which was marked deprecated. */
12218 void
12219 warn_deprecated_use (tree node, tree attr)
12221 const char *msg;
12223 if (node == 0 || !warn_deprecated_decl)
12224 return;
12226 if (!attr)
12228 if (DECL_P (node))
12229 attr = DECL_ATTRIBUTES (node);
12230 else if (TYPE_P (node))
12232 tree decl = TYPE_STUB_DECL (node);
12233 if (decl)
12234 attr = lookup_attribute ("deprecated",
12235 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
12239 if (attr)
12240 attr = lookup_attribute ("deprecated", attr);
12242 if (attr)
12243 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
12244 else
12245 msg = NULL;
12247 bool w;
12248 if (DECL_P (node))
12250 if (msg)
12251 w = warning (OPT_Wdeprecated_declarations,
12252 "%qD is deprecated: %s", node, msg);
12253 else
12254 w = warning (OPT_Wdeprecated_declarations,
12255 "%qD is deprecated", node);
12256 if (w)
12257 inform (DECL_SOURCE_LOCATION (node), "declared here");
12259 else if (TYPE_P (node))
12261 tree what = NULL_TREE;
12262 tree decl = TYPE_STUB_DECL (node);
12264 if (TYPE_NAME (node))
12266 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12267 what = TYPE_NAME (node);
12268 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12269 && DECL_NAME (TYPE_NAME (node)))
12270 what = DECL_NAME (TYPE_NAME (node));
12273 if (decl)
12275 if (what)
12277 if (msg)
12278 w = warning (OPT_Wdeprecated_declarations,
12279 "%qE is deprecated: %s", what, msg);
12280 else
12281 w = warning (OPT_Wdeprecated_declarations,
12282 "%qE is deprecated", what);
12284 else
12286 if (msg)
12287 w = warning (OPT_Wdeprecated_declarations,
12288 "type is deprecated: %s", msg);
12289 else
12290 w = warning (OPT_Wdeprecated_declarations,
12291 "type is deprecated");
12293 if (w)
12294 inform (DECL_SOURCE_LOCATION (decl), "declared here");
12296 else
12298 if (what)
12300 if (msg)
12301 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
12302 what, msg);
12303 else
12304 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
12306 else
12308 if (msg)
12309 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
12310 msg);
12311 else
12312 warning (OPT_Wdeprecated_declarations, "type is deprecated");
12318 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12319 somewhere in it. */
12321 bool
12322 contains_bitfld_component_ref_p (const_tree ref)
12324 while (handled_component_p (ref))
12326 if (TREE_CODE (ref) == COMPONENT_REF
12327 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12328 return true;
12329 ref = TREE_OPERAND (ref, 0);
12332 return false;
12335 /* Try to determine whether a TRY_CATCH expression can fall through.
12336 This is a subroutine of block_may_fallthru. */
12338 static bool
12339 try_catch_may_fallthru (const_tree stmt)
12341 tree_stmt_iterator i;
12343 /* If the TRY block can fall through, the whole TRY_CATCH can
12344 fall through. */
12345 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
12346 return true;
12348 i = tsi_start (TREE_OPERAND (stmt, 1));
12349 switch (TREE_CODE (tsi_stmt (i)))
12351 case CATCH_EXPR:
12352 /* We expect to see a sequence of CATCH_EXPR trees, each with a
12353 catch expression and a body. The whole TRY_CATCH may fall
12354 through iff any of the catch bodies falls through. */
12355 for (; !tsi_end_p (i); tsi_next (&i))
12357 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
12358 return true;
12360 return false;
12362 case EH_FILTER_EXPR:
12363 /* The exception filter expression only matters if there is an
12364 exception. If the exception does not match EH_FILTER_TYPES,
12365 we will execute EH_FILTER_FAILURE, and we will fall through
12366 if that falls through. If the exception does match
12367 EH_FILTER_TYPES, the stack unwinder will continue up the
12368 stack, so we will not fall through. We don't know whether we
12369 will throw an exception which matches EH_FILTER_TYPES or not,
12370 so we just ignore EH_FILTER_TYPES and assume that we might
12371 throw an exception which doesn't match. */
12372 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
12374 default:
12375 /* This case represents statements to be executed when an
12376 exception occurs. Those statements are implicitly followed
12377 by a RESX statement to resume execution after the exception.
12378 So in this case the TRY_CATCH never falls through. */
12379 return false;
12383 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
12384 need not be 100% accurate; simply be conservative and return true if we
12385 don't know. This is used only to avoid stupidly generating extra code.
12386 If we're wrong, we'll just delete the extra code later. */
12388 bool
12389 block_may_fallthru (const_tree block)
12391 /* This CONST_CAST is okay because expr_last returns its argument
12392 unmodified and we assign it to a const_tree. */
12393 const_tree stmt = expr_last (CONST_CAST_TREE (block));
12395 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
12397 case GOTO_EXPR:
12398 case RETURN_EXPR:
12399 /* Easy cases. If the last statement of the block implies
12400 control transfer, then we can't fall through. */
12401 return false;
12403 case SWITCH_EXPR:
12404 /* If SWITCH_LABELS is set, this is lowered, and represents a
12405 branch to a selected label and hence can not fall through.
12406 Otherwise SWITCH_BODY is set, and the switch can fall
12407 through. */
12408 return SWITCH_LABELS (stmt) == NULL_TREE;
12410 case COND_EXPR:
12411 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
12412 return true;
12413 return block_may_fallthru (COND_EXPR_ELSE (stmt));
12415 case BIND_EXPR:
12416 return block_may_fallthru (BIND_EXPR_BODY (stmt));
12418 case TRY_CATCH_EXPR:
12419 return try_catch_may_fallthru (stmt);
12421 case TRY_FINALLY_EXPR:
12422 /* The finally clause is always executed after the try clause,
12423 so if it does not fall through, then the try-finally will not
12424 fall through. Otherwise, if the try clause does not fall
12425 through, then when the finally clause falls through it will
12426 resume execution wherever the try clause was going. So the
12427 whole try-finally will only fall through if both the try
12428 clause and the finally clause fall through. */
12429 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
12430 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
12432 case MODIFY_EXPR:
12433 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
12434 stmt = TREE_OPERAND (stmt, 1);
12435 else
12436 return true;
12437 /* FALLTHRU */
12439 case CALL_EXPR:
12440 /* Functions that do not return do not fall through. */
12441 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
12443 case CLEANUP_POINT_EXPR:
12444 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12446 case TARGET_EXPR:
12447 return block_may_fallthru (TREE_OPERAND (stmt, 1));
12449 case ERROR_MARK:
12450 return true;
12452 default:
12453 return lang_hooks.block_may_fallthru (stmt);
12457 /* True if we are using EH to handle cleanups. */
12458 static bool using_eh_for_cleanups_flag = false;
12460 /* This routine is called from front ends to indicate eh should be used for
12461 cleanups. */
12462 void
12463 using_eh_for_cleanups (void)
12465 using_eh_for_cleanups_flag = true;
12468 /* Query whether EH is used for cleanups. */
12469 bool
12470 using_eh_for_cleanups_p (void)
12472 return using_eh_for_cleanups_flag;
12475 /* Wrapper for tree_code_name to ensure that tree code is valid */
12476 const char *
12477 get_tree_code_name (enum tree_code code)
12479 const char *invalid = "<invalid tree code>";
12481 if (code >= MAX_TREE_CODES)
12482 return invalid;
12484 return tree_code_name[code];
12487 /* Drops the TREE_OVERFLOW flag from T. */
12489 tree
12490 drop_tree_overflow (tree t)
12492 gcc_checking_assert (TREE_OVERFLOW (t));
12494 /* For tree codes with a sharing machinery re-build the result. */
12495 if (TREE_CODE (t) == INTEGER_CST)
12496 return wide_int_to_tree (TREE_TYPE (t), t);
12498 /* Otherwise, as all tcc_constants are possibly shared, copy the node
12499 and drop the flag. */
12500 t = copy_node (t);
12501 TREE_OVERFLOW (t) = 0;
12502 return t;
12505 /* Given a memory reference expression T, return its base address.
12506 The base address of a memory reference expression is the main
12507 object being referenced. For instance, the base address for
12508 'array[i].fld[j]' is 'array'. You can think of this as stripping
12509 away the offset part from a memory address.
12511 This function calls handled_component_p to strip away all the inner
12512 parts of the memory reference until it reaches the base object. */
12514 tree
12515 get_base_address (tree t)
12517 while (handled_component_p (t))
12518 t = TREE_OPERAND (t, 0);
12520 if ((TREE_CODE (t) == MEM_REF
12521 || TREE_CODE (t) == TARGET_MEM_REF)
12522 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
12523 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
12525 /* ??? Either the alias oracle or all callers need to properly deal
12526 with WITH_SIZE_EXPRs before we can look through those. */
12527 if (TREE_CODE (t) == WITH_SIZE_EXPR)
12528 return NULL_TREE;
12530 return t;
12533 /* Return a tree of sizetype representing the size, in bytes, of the element
12534 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12536 tree
12537 array_ref_element_size (tree exp)
12539 tree aligned_size = TREE_OPERAND (exp, 3);
12540 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
12541 location_t loc = EXPR_LOCATION (exp);
12543 /* If a size was specified in the ARRAY_REF, it's the size measured
12544 in alignment units of the element type. So multiply by that value. */
12545 if (aligned_size)
12547 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
12548 sizetype from another type of the same width and signedness. */
12549 if (TREE_TYPE (aligned_size) != sizetype)
12550 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
12551 return size_binop_loc (loc, MULT_EXPR, aligned_size,
12552 size_int (TYPE_ALIGN_UNIT (elmt_type)));
12555 /* Otherwise, take the size from that of the element type. Substitute
12556 any PLACEHOLDER_EXPR that we have. */
12557 else
12558 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
12561 /* Return a tree representing the lower bound of the array mentioned in
12562 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12564 tree
12565 array_ref_low_bound (tree exp)
12567 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12569 /* If a lower bound is specified in EXP, use it. */
12570 if (TREE_OPERAND (exp, 2))
12571 return TREE_OPERAND (exp, 2);
12573 /* Otherwise, if there is a domain type and it has a lower bound, use it,
12574 substituting for a PLACEHOLDER_EXPR as needed. */
12575 if (domain_type && TYPE_MIN_VALUE (domain_type))
12576 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
12578 /* Otherwise, return a zero of the appropriate type. */
12579 return build_int_cst (TREE_TYPE (TREE_OPERAND (exp, 1)), 0);
12582 /* Return a tree representing the upper bound of the array mentioned in
12583 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12585 tree
12586 array_ref_up_bound (tree exp)
12588 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12590 /* If there is a domain type and it has an upper bound, use it, substituting
12591 for a PLACEHOLDER_EXPR as needed. */
12592 if (domain_type && TYPE_MAX_VALUE (domain_type))
12593 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
12595 /* Otherwise fail. */
12596 return NULL_TREE;
12599 /* Returns true if REF is an array reference to an array at the end of
12600 a structure. If this is the case, the array may be allocated larger
12601 than its upper bound implies. */
12603 bool
12604 array_at_struct_end_p (tree ref)
12606 if (TREE_CODE (ref) != ARRAY_REF
12607 && TREE_CODE (ref) != ARRAY_RANGE_REF)
12608 return false;
12610 while (handled_component_p (ref))
12612 /* If the reference chain contains a component reference to a
12613 non-union type and there follows another field the reference
12614 is not at the end of a structure. */
12615 if (TREE_CODE (ref) == COMPONENT_REF
12616 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
12618 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
12619 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
12620 nextf = DECL_CHAIN (nextf);
12621 if (nextf)
12622 return false;
12625 ref = TREE_OPERAND (ref, 0);
12628 /* If the reference is based on a declared entity, the size of the array
12629 is constrained by its given domain. */
12630 if (DECL_P (ref))
12631 return false;
12633 return true;
12636 /* Return a tree representing the offset, in bytes, of the field referenced
12637 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
12639 tree
12640 component_ref_field_offset (tree exp)
12642 tree aligned_offset = TREE_OPERAND (exp, 2);
12643 tree field = TREE_OPERAND (exp, 1);
12644 location_t loc = EXPR_LOCATION (exp);
12646 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
12647 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
12648 value. */
12649 if (aligned_offset)
12651 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
12652 sizetype from another type of the same width and signedness. */
12653 if (TREE_TYPE (aligned_offset) != sizetype)
12654 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
12655 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
12656 size_int (DECL_OFFSET_ALIGN (field)
12657 / BITS_PER_UNIT));
12660 /* Otherwise, take the offset from that of the field. Substitute
12661 any PLACEHOLDER_EXPR that we have. */
12662 else
12663 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
12666 /* Return the machine mode of T. For vectors, returns the mode of the
12667 inner type. The main use case is to feed the result to HONOR_NANS,
12668 avoiding the BLKmode that a direct TYPE_MODE (T) might return. */
12670 machine_mode
12671 element_mode (const_tree t)
12673 if (!TYPE_P (t))
12674 t = TREE_TYPE (t);
12675 if (VECTOR_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
12676 t = TREE_TYPE (t);
12677 return TYPE_MODE (t);
12681 /* Veirfy that basic properties of T match TV and thus T can be a variant of
12682 TV. TV should be the more specified variant (i.e. the main variant). */
12684 static bool
12685 verify_type_variant (const_tree t, tree tv)
12687 /* Type variant can differ by:
12689 - TYPE_QUALS: TYPE_READONLY, TYPE_VOLATILE, TYPE_ATOMIC, TYPE_RESTRICT,
12690 ENCODE_QUAL_ADDR_SPACE.
12691 - main variant may be TYPE_COMPLETE_P and variant types !TYPE_COMPLETE_P
12692 in this case some values may not be set in the variant types
12693 (see TYPE_COMPLETE_P checks).
12694 - it is possible to have TYPE_ARTIFICIAL variant of non-artifical type
12695 - by TYPE_NAME and attributes (i.e. when variant originate by typedef)
12696 - TYPE_CANONICAL (TYPE_ALIAS_SET is the same among variants)
12697 - by the alignment: TYPE_ALIGN and TYPE_USER_ALIGN
12698 - during LTO by TYPE_CONTEXT if type is TYPE_FILE_SCOPE_P
12699 this is necessary to make it possible to merge types form different TUs
12700 - arrays, pointers and references may have TREE_TYPE that is a variant
12701 of TREE_TYPE of their main variants.
12702 - aggregates may have new TYPE_FIELDS list that list variants of
12703 the main variant TYPE_FIELDS.
12704 - vector types may differ by TYPE_VECTOR_OPAQUE
12705 - TYPE_METHODS is always NULL for vairant types and maintained for
12706 main variant only.
12709 /* Convenience macro for matching individual fields. */
12710 #define verify_variant_match(flag) \
12711 do { \
12712 if (flag (tv) != flag (t)) \
12714 error ("type variant differs by " #flag "."); \
12715 debug_tree (tv); \
12716 return false; \
12718 } while (false)
12720 /* tree_base checks. */
12722 verify_variant_match (TREE_CODE);
12723 /* FIXME: Ada builds non-artificial variants of artificial types. */
12724 if (TYPE_ARTIFICIAL (tv) && 0)
12725 verify_variant_match (TYPE_ARTIFICIAL);
12726 if (POINTER_TYPE_P (tv))
12727 verify_variant_match (TYPE_REF_CAN_ALIAS_ALL);
12728 /* FIXME: TYPE_SIZES_GIMPLIFIED may differs for Ada build. */
12729 verify_variant_match (TYPE_UNSIGNED);
12730 verify_variant_match (TYPE_ALIGN_OK);
12731 verify_variant_match (TYPE_PACKED);
12732 if (TREE_CODE (t) == REFERENCE_TYPE)
12733 verify_variant_match (TYPE_REF_IS_RVALUE);
12734 verify_variant_match (TYPE_SATURATING);
12735 /* FIXME: This check trigger during libstdc++ build. */
12736 if (RECORD_OR_UNION_TYPE_P (t) && COMPLETE_TYPE_P (t) && 0)
12737 verify_variant_match (TYPE_FINAL_P);
12739 /* tree_type_common checks. */
12741 if (COMPLETE_TYPE_P (t))
12743 verify_variant_match (TYPE_SIZE);
12744 verify_variant_match (TYPE_MODE);
12745 if (TYPE_SIZE_UNIT (t) != TYPE_SIZE_UNIT (tv)
12746 /* FIXME: ideally we should compare pointer equality, but java FE
12747 produce variants where size is INTEGER_CST of different type (int
12748 wrt size_type) during libjava biuld. */
12749 && !operand_equal_p (TYPE_SIZE_UNIT (t), TYPE_SIZE_UNIT (tv), 0))
12751 error ("type variant has different TYPE_SIZE_UNIT");
12752 debug_tree (tv);
12753 error ("type variant's TYPE_SIZE_UNIT");
12754 debug_tree (TYPE_SIZE_UNIT (tv));
12755 error ("type's TYPE_SIZE_UNIT");
12756 debug_tree (TYPE_SIZE_UNIT (t));
12757 return false;
12760 verify_variant_match (TYPE_PRECISION);
12761 verify_variant_match (TYPE_NEEDS_CONSTRUCTING);
12762 if (RECORD_OR_UNION_TYPE_P (t))
12763 verify_variant_match (TYPE_TRANSPARENT_AGGR);
12764 else if (TREE_CODE (t) == ARRAY_TYPE)
12765 verify_variant_match (TYPE_NONALIASED_COMPONENT);
12766 /* During LTO we merge variant lists from diferent translation units
12767 that may differ BY TYPE_CONTEXT that in turn may point
12768 to TRANSLATION_UNIT_DECL.
12769 Ada also builds variants of types with different TYPE_CONTEXT. */
12770 if ((!in_lto_p || !TYPE_FILE_SCOPE_P (t)) && 0)
12771 verify_variant_match (TYPE_CONTEXT);
12772 verify_variant_match (TYPE_STRING_FLAG);
12773 if (TYPE_ALIAS_SET_KNOWN_P (t) && TYPE_ALIAS_SET_KNOWN_P (tv))
12774 verify_variant_match (TYPE_ALIAS_SET);
12776 /* tree_type_non_common checks. */
12778 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
12779 and dangle the pointer from time to time. */
12780 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_VFIELD (t) != TYPE_VFIELD (tv)
12781 && (in_lto_p || !TYPE_VFIELD (tv)
12782 || TREE_CODE (TYPE_VFIELD (tv)) != TREE_LIST))
12784 error ("type variant has different TYPE_VFIELD");
12785 debug_tree (tv);
12786 return false;
12788 if ((TREE_CODE (t) == ENUMERAL_TYPE && COMPLETE_TYPE_P (t))
12789 || TREE_CODE (t) == INTEGER_TYPE
12790 || TREE_CODE (t) == BOOLEAN_TYPE
12791 || TREE_CODE (t) == REAL_TYPE
12792 || TREE_CODE (t) == FIXED_POINT_TYPE)
12794 verify_variant_match (TYPE_MAX_VALUE);
12795 verify_variant_match (TYPE_MIN_VALUE);
12797 if (TREE_CODE (t) == METHOD_TYPE)
12798 verify_variant_match (TYPE_METHOD_BASETYPE);
12799 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_METHODS (t))
12801 error ("type variant has TYPE_METHODS");
12802 debug_tree (tv);
12803 return false;
12805 if (TREE_CODE (t) == OFFSET_TYPE)
12806 verify_variant_match (TYPE_OFFSET_BASETYPE);
12807 if (TREE_CODE (t) == ARRAY_TYPE)
12808 verify_variant_match (TYPE_ARRAY_MAX_SIZE);
12809 /* FIXME: Be lax and allow TYPE_BINFO to be missing in variant types
12810 or even type's main variant. This is needed to make bootstrap pass
12811 and the bug seems new in GCC 5.
12812 C++ FE should be updated to make this consistent and we should check
12813 that TYPE_BINFO is always NULL for !COMPLETE_TYPE_P and otherwise there
12814 is a match with main variant.
12816 Also disable the check for Java for now because of parser hack that builds
12817 first an dummy BINFO and then sometimes replace it by real BINFO in some
12818 of the copies. */
12819 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t) && TYPE_BINFO (tv)
12820 && TYPE_BINFO (t) != TYPE_BINFO (tv)
12821 /* FIXME: Java sometimes keep dump TYPE_BINFOs on variant types.
12822 Since there is no cheap way to tell C++/Java type w/o LTO, do checking
12823 at LTO time only. */
12824 && (in_lto_p && odr_type_p (t)))
12826 error ("type variant has different TYPE_BINFO");
12827 debug_tree (tv);
12828 error ("type variant's TYPE_BINFO");
12829 debug_tree (TYPE_BINFO (tv));
12830 error ("type's TYPE_BINFO");
12831 debug_tree (TYPE_BINFO (t));
12832 return false;
12835 /* Check various uses of TYPE_VALUES_RAW. */
12836 if (TREE_CODE (t) == ENUMERAL_TYPE)
12837 verify_variant_match (TYPE_VALUES);
12838 else if (TREE_CODE (t) == ARRAY_TYPE)
12839 verify_variant_match (TYPE_DOMAIN);
12840 /* Permit incomplete variants of complete type. While FEs may complete
12841 all variants, this does not happen for C++ templates in all cases. */
12842 else if (RECORD_OR_UNION_TYPE_P (t)
12843 && COMPLETE_TYPE_P (t)
12844 && TYPE_FIELDS (t) != TYPE_FIELDS (tv))
12846 tree f1, f2;
12848 /* Fortran builds qualified variants as new records with items of
12849 qualified type. Verify that they looks same. */
12850 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (tv);
12851 f1 && f2;
12852 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
12853 if (TREE_CODE (f1) != FIELD_DECL || TREE_CODE (f2) != FIELD_DECL
12854 || (TYPE_MAIN_VARIANT (TREE_TYPE (f1))
12855 != TYPE_MAIN_VARIANT (TREE_TYPE (f2))
12856 /* FIXME: gfc_nonrestricted_type builds all types as variants
12857 with exception of pointer types. It deeply copies the type
12858 which means that we may end up with a variant type
12859 referring non-variant pointer. We may change it to
12860 produce types as variants, too, like
12861 objc_get_protocol_qualified_type does. */
12862 && !POINTER_TYPE_P (TREE_TYPE (f1)))
12863 || DECL_FIELD_OFFSET (f1) != DECL_FIELD_OFFSET (f2)
12864 || DECL_FIELD_BIT_OFFSET (f1) != DECL_FIELD_BIT_OFFSET (f2))
12865 break;
12866 if (f1 || f2)
12868 error ("type variant has different TYPE_FIELDS");
12869 debug_tree (tv);
12870 error ("first mismatch is field");
12871 debug_tree (f1);
12872 error ("and field");
12873 debug_tree (f2);
12874 return false;
12877 else if ((TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE))
12878 verify_variant_match (TYPE_ARG_TYPES);
12879 /* For C++ the qualified variant of array type is really an array type
12880 of qualified TREE_TYPE.
12881 objc builds variants of pointer where pointer to type is a variant, too
12882 in objc_get_protocol_qualified_type. */
12883 if (TREE_TYPE (t) != TREE_TYPE (tv)
12884 && ((TREE_CODE (t) != ARRAY_TYPE
12885 && !POINTER_TYPE_P (t))
12886 || TYPE_MAIN_VARIANT (TREE_TYPE (t))
12887 != TYPE_MAIN_VARIANT (TREE_TYPE (tv))))
12889 error ("type variant has different TREE_TYPE");
12890 debug_tree (tv);
12891 error ("type variant's TREE_TYPE");
12892 debug_tree (TREE_TYPE (tv));
12893 error ("type's TREE_TYPE");
12894 debug_tree (TREE_TYPE (t));
12895 return false;
12897 if (type_with_alias_set_p (t)
12898 && !gimple_canonical_types_compatible_p (t, tv, false))
12900 error ("type is not compatible with its vairant");
12901 debug_tree (tv);
12902 error ("type variant's TREE_TYPE");
12903 debug_tree (TREE_TYPE (tv));
12904 error ("type's TREE_TYPE");
12905 debug_tree (TREE_TYPE (t));
12906 return false;
12908 return true;
12909 #undef verify_variant_match
12913 /* The TYPE_CANONICAL merging machinery. It should closely resemble
12914 the middle-end types_compatible_p function. It needs to avoid
12915 claiming types are different for types that should be treated
12916 the same with respect to TBAA. Canonical types are also used
12917 for IL consistency checks via the useless_type_conversion_p
12918 predicate which does not handle all type kinds itself but falls
12919 back to pointer-comparison of TYPE_CANONICAL for aggregates
12920 for example. */
12922 /* Return true iff T1 and T2 are structurally identical for what
12923 TBAA is concerned.
12924 This function is used both by lto.c canonical type merging and by the
12925 verifier. If TRUST_TYPE_CANONICAL we do not look into structure of types
12926 that have TYPE_CANONICAL defined and assume them equivalent. */
12928 bool
12929 gimple_canonical_types_compatible_p (const_tree t1, const_tree t2,
12930 bool trust_type_canonical)
12932 /* Type variants should be same as the main variant. When not doing sanity
12933 checking to verify this fact, go to main variants and save some work. */
12934 if (trust_type_canonical)
12936 t1 = TYPE_MAIN_VARIANT (t1);
12937 t2 = TYPE_MAIN_VARIANT (t2);
12940 /* Check first for the obvious case of pointer identity. */
12941 if (t1 == t2)
12942 return true;
12944 /* Check that we have two types to compare. */
12945 if (t1 == NULL_TREE || t2 == NULL_TREE)
12946 return false;
12948 /* We consider complete types always compatible with incomplete type.
12949 This does not make sense for canonical type calculation and thus we
12950 need to ensure that we are never called on it.
12952 FIXME: For more correctness the function probably should have three modes
12953 1) mode assuming that types are complete mathcing their structure
12954 2) mode allowing incomplete types but producing equivalence classes
12955 and thus ignoring all info from complete types
12956 3) mode allowing incomplete types to match complete but checking
12957 compatibility between complete types.
12959 1 and 2 can be used for canonical type calculation. 3 is the real
12960 definition of type compatibility that can be used i.e. for warnings during
12961 declaration merging. */
12963 gcc_assert (!trust_type_canonical
12964 || (type_with_alias_set_p (t1) && type_with_alias_set_p (t2)));
12965 /* If the types have been previously registered and found equal
12966 they still are. */
12967 if (TYPE_CANONICAL (t1) && TYPE_CANONICAL (t2)
12968 && trust_type_canonical)
12969 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
12971 /* Can't be the same type if the types don't have the same code. */
12972 if (tree_code_for_canonical_type_merging (TREE_CODE (t1))
12973 != tree_code_for_canonical_type_merging (TREE_CODE (t2)))
12974 return false;
12976 /* Qualifiers do not matter for canonical type comparison purposes. */
12978 /* Void types and nullptr types are always the same. */
12979 if (TREE_CODE (t1) == VOID_TYPE
12980 || TREE_CODE (t1) == NULLPTR_TYPE)
12981 return true;
12983 /* Can't be the same type if they have different mode. */
12984 if (TYPE_MODE (t1) != TYPE_MODE (t2))
12985 return false;
12987 /* Non-aggregate types can be handled cheaply. */
12988 if (INTEGRAL_TYPE_P (t1)
12989 || SCALAR_FLOAT_TYPE_P (t1)
12990 || FIXED_POINT_TYPE_P (t1)
12991 || TREE_CODE (t1) == VECTOR_TYPE
12992 || TREE_CODE (t1) == COMPLEX_TYPE
12993 || TREE_CODE (t1) == OFFSET_TYPE
12994 || POINTER_TYPE_P (t1))
12996 /* Can't be the same type if they have different sign or precision. */
12997 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
12998 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
12999 return false;
13001 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
13002 interoperable with "signed char". Unless all frontends are revisited
13003 to agree on these types, we must ignore the flag completely. */
13005 /* Fortran standard define C_PTR type that is compatible with every
13006 C pointer. For this reason we need to glob all pointers into one.
13007 Still pointers in different address spaces are not compatible. */
13008 if (POINTER_TYPE_P (t1))
13010 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
13011 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
13012 return false;
13015 /* Tail-recurse to components. */
13016 if (TREE_CODE (t1) == VECTOR_TYPE
13017 || TREE_CODE (t1) == COMPLEX_TYPE)
13018 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
13019 TREE_TYPE (t2),
13020 trust_type_canonical);
13022 return true;
13025 /* Do type-specific comparisons. */
13026 switch (TREE_CODE (t1))
13028 case ARRAY_TYPE:
13029 /* Array types are the same if the element types are the same and
13030 the number of elements are the same. */
13031 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13032 trust_type_canonical)
13033 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
13034 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
13035 return false;
13036 else
13038 tree i1 = TYPE_DOMAIN (t1);
13039 tree i2 = TYPE_DOMAIN (t2);
13041 /* For an incomplete external array, the type domain can be
13042 NULL_TREE. Check this condition also. */
13043 if (i1 == NULL_TREE && i2 == NULL_TREE)
13044 return true;
13045 else if (i1 == NULL_TREE || i2 == NULL_TREE)
13046 return false;
13047 else
13049 tree min1 = TYPE_MIN_VALUE (i1);
13050 tree min2 = TYPE_MIN_VALUE (i2);
13051 tree max1 = TYPE_MAX_VALUE (i1);
13052 tree max2 = TYPE_MAX_VALUE (i2);
13054 /* The minimum/maximum values have to be the same. */
13055 if ((min1 == min2
13056 || (min1 && min2
13057 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
13058 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
13059 || operand_equal_p (min1, min2, 0))))
13060 && (max1 == max2
13061 || (max1 && max2
13062 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
13063 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
13064 || operand_equal_p (max1, max2, 0)))))
13065 return true;
13066 else
13067 return false;
13071 case METHOD_TYPE:
13072 case FUNCTION_TYPE:
13073 /* Function types are the same if the return type and arguments types
13074 are the same. */
13075 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13076 trust_type_canonical))
13077 return false;
13079 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
13080 return true;
13081 else
13083 tree parms1, parms2;
13085 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
13086 parms1 && parms2;
13087 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
13089 if (!gimple_canonical_types_compatible_p
13090 (TREE_VALUE (parms1), TREE_VALUE (parms2),
13091 trust_type_canonical))
13092 return false;
13095 if (parms1 || parms2)
13096 return false;
13098 return true;
13101 case RECORD_TYPE:
13102 case UNION_TYPE:
13103 case QUAL_UNION_TYPE:
13105 tree f1, f2;
13107 /* For aggregate types, all the fields must be the same. */
13108 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
13109 f1 || f2;
13110 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
13112 /* Skip non-fields. */
13113 while (f1 && TREE_CODE (f1) != FIELD_DECL)
13114 f1 = TREE_CHAIN (f1);
13115 while (f2 && TREE_CODE (f2) != FIELD_DECL)
13116 f2 = TREE_CHAIN (f2);
13117 if (!f1 || !f2)
13118 break;
13119 /* The fields must have the same name, offset and type. */
13120 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
13121 || !gimple_compare_field_offset (f1, f2)
13122 || !gimple_canonical_types_compatible_p
13123 (TREE_TYPE (f1), TREE_TYPE (f2),
13124 trust_type_canonical))
13125 return false;
13128 /* If one aggregate has more fields than the other, they
13129 are not the same. */
13130 if (f1 || f2)
13131 return false;
13133 return true;
13136 default:
13137 /* Consider all types with language specific trees in them mutually
13138 compatible. This is executed only from verify_type and false
13139 positives can be tolerated. */
13140 gcc_assert (!in_lto_p);
13141 return true;
13145 /* Verify type T. */
13147 void
13148 verify_type (const_tree t)
13150 bool error_found = false;
13151 tree mv = TYPE_MAIN_VARIANT (t);
13152 if (!mv)
13154 error ("Main variant is not defined");
13155 error_found = true;
13157 else if (mv != TYPE_MAIN_VARIANT (mv))
13159 error ("TYPE_MAIN_VARIANT has different TYPE_MAIN_VARIANT");
13160 debug_tree (mv);
13161 error_found = true;
13163 else if (t != mv && !verify_type_variant (t, mv))
13164 error_found = true;
13166 tree ct = TYPE_CANONICAL (t);
13167 if (!ct)
13169 else if (TYPE_CANONICAL (t) != ct)
13171 error ("TYPE_CANONICAL has different TYPE_CANONICAL");
13172 debug_tree (ct);
13173 error_found = true;
13175 /* Method and function types can not be used to address memory and thus
13176 TYPE_CANONICAL really matters only for determining useless conversions.
13178 FIXME: C++ FE produce declarations of builtin functions that are not
13179 compatible with main variants. */
13180 else if (TREE_CODE (t) == FUNCTION_TYPE)
13182 else if (t != ct
13183 /* FIXME: gimple_canonical_types_compatible_p can not compare types
13184 with variably sized arrays because their sizes possibly
13185 gimplified to different variables. */
13186 && !variably_modified_type_p (ct, NULL)
13187 && !gimple_canonical_types_compatible_p (t, ct, false))
13189 error ("TYPE_CANONICAL is not compatible");
13190 debug_tree (ct);
13191 error_found = true;
13195 /* Check various uses of TYPE_MINVAL. */
13196 if (RECORD_OR_UNION_TYPE_P (t))
13198 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
13199 and danagle the pointer from time to time. */
13200 if (TYPE_VFIELD (t)
13201 && TREE_CODE (TYPE_VFIELD (t)) != FIELD_DECL
13202 && TREE_CODE (TYPE_VFIELD (t)) != TREE_LIST)
13204 error ("TYPE_VFIELD is not FIELD_DECL nor TREE_LIST");
13205 debug_tree (TYPE_VFIELD (t));
13206 error_found = true;
13209 else if (TREE_CODE (t) == POINTER_TYPE)
13211 if (TYPE_NEXT_PTR_TO (t)
13212 && TREE_CODE (TYPE_NEXT_PTR_TO (t)) != POINTER_TYPE)
13214 error ("TYPE_NEXT_PTR_TO is not POINTER_TYPE");
13215 debug_tree (TYPE_NEXT_PTR_TO (t));
13216 error_found = true;
13219 else if (TREE_CODE (t) == REFERENCE_TYPE)
13221 if (TYPE_NEXT_REF_TO (t)
13222 && TREE_CODE (TYPE_NEXT_REF_TO (t)) != REFERENCE_TYPE)
13224 error ("TYPE_NEXT_REF_TO is not REFERENCE_TYPE");
13225 debug_tree (TYPE_NEXT_REF_TO (t));
13226 error_found = true;
13229 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
13230 || TREE_CODE (t) == FIXED_POINT_TYPE)
13232 /* FIXME: The following check should pass:
13233 useless_type_conversion_p (const_cast <tree> (t),
13234 TREE_TYPE (TYPE_MIN_VALUE (t))
13235 but does not for C sizetypes in LTO. */
13237 /* Java uses TYPE_MINVAL for TYPE_ARGUMENT_SIGNATURE. */
13238 else if (TYPE_MINVAL (t)
13239 && ((TREE_CODE (t) != METHOD_TYPE && TREE_CODE (t) != FUNCTION_TYPE)
13240 || in_lto_p))
13242 error ("TYPE_MINVAL non-NULL");
13243 debug_tree (TYPE_MINVAL (t));
13244 error_found = true;
13247 /* Check various uses of TYPE_MAXVAL. */
13248 if (RECORD_OR_UNION_TYPE_P (t))
13250 if (TYPE_METHODS (t) && TREE_CODE (TYPE_METHODS (t)) != FUNCTION_DECL
13251 && TREE_CODE (TYPE_METHODS (t)) != TEMPLATE_DECL
13252 && TYPE_METHODS (t) != error_mark_node)
13254 error ("TYPE_METHODS is not FUNCTION_DECL, TEMPLATE_DECL nor error_mark_node");
13255 debug_tree (TYPE_METHODS (t));
13256 error_found = true;
13259 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
13261 if (TYPE_METHOD_BASETYPE (t)
13262 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != RECORD_TYPE
13263 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != UNION_TYPE)
13265 error ("TYPE_METHOD_BASETYPE is not record nor union");
13266 debug_tree (TYPE_METHOD_BASETYPE (t));
13267 error_found = true;
13270 else if (TREE_CODE (t) == OFFSET_TYPE)
13272 if (TYPE_OFFSET_BASETYPE (t)
13273 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != RECORD_TYPE
13274 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != UNION_TYPE)
13276 error ("TYPE_OFFSET_BASETYPE is not record nor union");
13277 debug_tree (TYPE_OFFSET_BASETYPE (t));
13278 error_found = true;
13281 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
13282 || TREE_CODE (t) == FIXED_POINT_TYPE)
13284 /* FIXME: The following check should pass:
13285 useless_type_conversion_p (const_cast <tree> (t),
13286 TREE_TYPE (TYPE_MAX_VALUE (t))
13287 but does not for C sizetypes in LTO. */
13289 else if (TREE_CODE (t) == ARRAY_TYPE)
13291 if (TYPE_ARRAY_MAX_SIZE (t)
13292 && TREE_CODE (TYPE_ARRAY_MAX_SIZE (t)) != INTEGER_CST)
13294 error ("TYPE_ARRAY_MAX_SIZE not INTEGER_CST");
13295 debug_tree (TYPE_ARRAY_MAX_SIZE (t));
13296 error_found = true;
13299 else if (TYPE_MAXVAL (t))
13301 error ("TYPE_MAXVAL non-NULL");
13302 debug_tree (TYPE_MAXVAL (t));
13303 error_found = true;
13306 /* Check various uses of TYPE_BINFO. */
13307 if (RECORD_OR_UNION_TYPE_P (t))
13309 if (!TYPE_BINFO (t))
13311 else if (TREE_CODE (TYPE_BINFO (t)) != TREE_BINFO)
13313 error ("TYPE_BINFO is not TREE_BINFO");
13314 debug_tree (TYPE_BINFO (t));
13315 error_found = true;
13317 /* FIXME: Java builds invalid empty binfos that do not have
13318 TREE_TYPE set. */
13319 else if (TREE_TYPE (TYPE_BINFO (t)) != TYPE_MAIN_VARIANT (t) && 0)
13321 error ("TYPE_BINFO type is not TYPE_MAIN_VARIANT");
13322 debug_tree (TREE_TYPE (TYPE_BINFO (t)));
13323 error_found = true;
13326 else if (TYPE_LANG_SLOT_1 (t) && in_lto_p)
13328 error ("TYPE_LANG_SLOT_1 (binfo) field is non-NULL");
13329 debug_tree (TYPE_LANG_SLOT_1 (t));
13330 error_found = true;
13333 /* Check various uses of TYPE_VALUES_RAW. */
13334 if (TREE_CODE (t) == ENUMERAL_TYPE)
13335 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
13337 tree value = TREE_VALUE (l);
13338 tree name = TREE_PURPOSE (l);
13340 /* C FE porduce INTEGER_CST of INTEGER_TYPE, while C++ FE uses
13341 CONST_DECL of ENUMERAL TYPE. */
13342 if (TREE_CODE (value) != INTEGER_CST && TREE_CODE (value) != CONST_DECL)
13344 error ("Enum value is not CONST_DECL or INTEGER_CST");
13345 debug_tree (value);
13346 debug_tree (name);
13347 error_found = true;
13349 if (TREE_CODE (TREE_TYPE (value)) != INTEGER_TYPE
13350 && !useless_type_conversion_p (const_cast <tree> (t), TREE_TYPE (value)))
13352 error ("Enum value type is not INTEGER_TYPE nor convertible to the enum");
13353 debug_tree (value);
13354 debug_tree (name);
13355 error_found = true;
13357 if (TREE_CODE (name) != IDENTIFIER_NODE)
13359 error ("Enum value name is not IDENTIFIER_NODE");
13360 debug_tree (value);
13361 debug_tree (name);
13362 error_found = true;
13365 else if (TREE_CODE (t) == ARRAY_TYPE)
13367 if (TYPE_DOMAIN (t) && TREE_CODE (TYPE_DOMAIN (t)) != INTEGER_TYPE)
13369 error ("Array TYPE_DOMAIN is not integer type");
13370 debug_tree (TYPE_DOMAIN (t));
13371 error_found = true;
13374 else if (RECORD_OR_UNION_TYPE_P (t))
13375 for (tree fld = TYPE_FIELDS (t); fld; fld = TREE_CHAIN (fld))
13377 /* TODO: verify properties of decls. */
13378 if (TREE_CODE (fld) == FIELD_DECL)
13380 else if (TREE_CODE (fld) == TYPE_DECL)
13382 else if (TREE_CODE (fld) == CONST_DECL)
13384 else if (TREE_CODE (fld) == VAR_DECL)
13386 else if (TREE_CODE (fld) == TEMPLATE_DECL)
13388 else if (TREE_CODE (fld) == USING_DECL)
13390 else
13392 error ("Wrong tree in TYPE_FIELDS list");
13393 debug_tree (fld);
13394 error_found = true;
13397 else if (TREE_CODE (t) == INTEGER_TYPE
13398 || TREE_CODE (t) == BOOLEAN_TYPE
13399 || TREE_CODE (t) == OFFSET_TYPE
13400 || TREE_CODE (t) == REFERENCE_TYPE
13401 || TREE_CODE (t) == NULLPTR_TYPE
13402 || TREE_CODE (t) == POINTER_TYPE)
13404 if (TYPE_CACHED_VALUES_P (t) != (TYPE_CACHED_VALUES (t) != NULL))
13406 error ("TYPE_CACHED_VALUES_P is %i while TYPE_CACHED_VALUES is %p",
13407 TYPE_CACHED_VALUES_P (t), (void *)TYPE_CACHED_VALUES (t));
13408 error_found = true;
13410 else if (TYPE_CACHED_VALUES_P (t) && TREE_CODE (TYPE_CACHED_VALUES (t)) != TREE_VEC)
13412 error ("TYPE_CACHED_VALUES is not TREE_VEC");
13413 debug_tree (TYPE_CACHED_VALUES (t));
13414 error_found = true;
13416 /* Verify just enough of cache to ensure that no one copied it to new type.
13417 All copying should go by copy_node that should clear it. */
13418 else if (TYPE_CACHED_VALUES_P (t))
13420 int i;
13421 for (i = 0; i < TREE_VEC_LENGTH (TYPE_CACHED_VALUES (t)); i++)
13422 if (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)
13423 && TREE_TYPE (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)) != t)
13425 error ("wrong TYPE_CACHED_VALUES entry");
13426 debug_tree (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i));
13427 error_found = true;
13428 break;
13432 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
13433 for (tree l = TYPE_ARG_TYPES (t); l; l = TREE_CHAIN (l))
13435 /* C++ FE uses TREE_PURPOSE to store initial values. */
13436 if (TREE_PURPOSE (l) && in_lto_p)
13438 error ("TREE_PURPOSE is non-NULL in TYPE_ARG_TYPES list");
13439 debug_tree (l);
13440 error_found = true;
13442 if (!TYPE_P (TREE_VALUE (l)))
13444 error ("Wrong entry in TYPE_ARG_TYPES list");
13445 debug_tree (l);
13446 error_found = true;
13449 else if (!is_lang_specific (t) && TYPE_VALUES_RAW (t))
13451 error ("TYPE_VALUES_RAW field is non-NULL");
13452 debug_tree (TYPE_VALUES_RAW (t));
13453 error_found = true;
13455 if (TREE_CODE (t) != INTEGER_TYPE
13456 && TREE_CODE (t) != BOOLEAN_TYPE
13457 && TREE_CODE (t) != OFFSET_TYPE
13458 && TREE_CODE (t) != REFERENCE_TYPE
13459 && TREE_CODE (t) != NULLPTR_TYPE
13460 && TREE_CODE (t) != POINTER_TYPE
13461 && TYPE_CACHED_VALUES_P (t))
13463 error ("TYPE_CACHED_VALUES_P is set while it should not");
13464 error_found = true;
13466 if (TYPE_STRING_FLAG (t)
13467 && TREE_CODE (t) != ARRAY_TYPE && TREE_CODE (t) != INTEGER_TYPE)
13469 error ("TYPE_STRING_FLAG is set on wrong type code");
13470 error_found = true;
13472 else if (TYPE_STRING_FLAG (t))
13474 const_tree b = t;
13475 if (TREE_CODE (b) == ARRAY_TYPE)
13476 b = TREE_TYPE (t);
13477 /* Java builds arrays with TYPE_STRING_FLAG of promoted_char_type
13478 that is 32bits. */
13479 if (TREE_CODE (b) != INTEGER_TYPE)
13481 error ("TYPE_STRING_FLAG is set on type that does not look like "
13482 "char nor array of chars");
13483 error_found = true;
13487 /* ipa-devirt makes an assumption that TYPE_METHOD_BASETYPE is always
13488 TYPE_MAIN_VARIANT and it would be odd to add methods only to variatns
13489 of a type. */
13490 if (TREE_CODE (t) == METHOD_TYPE
13491 && TYPE_MAIN_VARIANT (TYPE_METHOD_BASETYPE (t)) != TYPE_METHOD_BASETYPE (t))
13493 error ("TYPE_METHOD_BASETYPE is not main variant");
13494 error_found = true;
13497 if (error_found)
13499 debug_tree (const_cast <tree> (t));
13500 internal_error ("verify_type failed");
13504 #include "gt-tree.h"